1

I'm trying to generate an ECDHE key using OpenSSL 1.0.2a on Windows and have the following sample code:

#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ecdh.h>

int main()
{
    OpenSSL_add_all_algorithms(); ERR_load_crypto_strings();

    EVP_PKEY_CTX* parameters_context = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
    EVP_PKEY* cparameters = nullptr;
    EVP_PKEY* private_key = nullptr;

    if (EVP_PKEY_paramgen_init(parameters_context) != 1) { return 1; }
    if (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(parameters_context, NID_sect571k1) != 1) { return 1; }
    if (EVP_PKEY_paramgen(parameters_context, &cparameters) != 1) { return 1; }

    EVP_PKEY_CTX* key_generation_context = EVP_PKEY_CTX_new(cparameters, NULL);

    if (!key_generation_context) { return 1; }
    if (EVP_PKEY_keygen_init(key_generation_context) != 1) { return 1; }        
    if (EVP_PKEY_keygen(key_generation_context, &private_key) != 1) { return 1; }

    BIO* bio = BIO_new(BIO_s_mem());
    PEM_write_bio_PUBKEY(bio, private_key); // <== This is where things go wrong.

    ERR_free_strings(); EVP_cleanup(); CRYPTO_cleanup_all_ex_data();
}

I tested the said code on other platforms (OSX and Debian Linux, using gcc) and it seems works fine (no errors reported under valgrind).

When I run it on Windows, it always fails on this line:

PEM_write_bio_PUBKEY(bio, private_key);

And I get this "nice" error screen:

heap error

I'm at loss figuring out what is wrong: from the many tutorials and documentation pages I could find, this seems to be the right way of doing things.

Before I spend another day trying to figure out what's wrong, I figured it might smarter to ask the community: is this the right way of generating and writing an ECDHE key as PEM format with OpenSSL ?

ereOn
  • 53,676
  • 39
  • 161
  • 238
  • This code looks OK to me. Where did you get OpenSSL for Windows? Did you build it yourself? If it was Thomas Hruska's Win32 OpenSSL, did you install the required C Runtime? – jww Mar 23 '15 at 01:24
  • @jww I built it myself. I used the same runtime and multi-threaded link settings as for my sample code. The weird thing is that I'm using OpenSSL actively in my project it works perfectly fine for everything else (RSA, AES encryption in GCM mode, X509 certificate generation and so on...). It's really just the ECDHE related operations that seem to crash badly. – ereOn Mar 23 '15 at 02:24
  • In that case, you might consider building OpenSSL on windows without ASM enabled. Do so by configuring with `no-asm`. There's also a `enable-ec_nistp_64_gcc_128` option that affects ECDHE, but you probably are not using it. If you continue to experience the crash, you might consider filing a bug (that's what it sounds like to me). – jww Mar 23 '15 at 02:28
  • @jww I already specified `no-asm` when I built it sadly. I'm building a static library if that makes any difference. Thanks for the feedback and the confirmation about my code: I guess I'll have no choice but to refactor that sample into an even more self-contained archive and file a bug. – ereOn Mar 23 '15 at 02:31
  • 1
    For the record, I created [a bug](http://rt.openssl.org/Ticket/Display.html?id=3765) on OpenSSL request tracker. – ereOn Mar 23 '15 at 21:03
  • Perfect, thanks. I think they are weary of me filing bugs. One of the devs told me I had filed something like 25% of the bugs after the migration to the new bug tracker. – jww Mar 23 '15 at 21:43

1 Answers1

0

It was indeed a bug in OpenSSL.

From the OpenSSL-dev mailing-list:

On Tue, Mar 31, 2015, ****** ******* wrote:

>

if (!combine) *pval = NULL;

I'd suggest deleting the two lines above. The structure should be cleared without this and the above line is wrong for non pointer fields anyway.

Steve. -- Dr Stephen N. Henson. OpenSSL project core developer. Commercial tech support now available see: http://www.openssl.org

See also this other question for details.

Community
  • 1
  • 1
ereOn
  • 53,676
  • 39
  • 161
  • 238