Search code examples
cvisual-studio-2015opensslpemelliptic-curve

Loading an Unencrypted PEM Public Key with OpenSSL libcrypt


I'm trying to load a SECP256R1 Public Key that I've created with OpenSSL into my C program. According to OpenSSL documentation the code below should work:

#include <stdio.h>

#include <openssl/pem.h>
#include <openssl/ec.h>

int main()
{
    EC_KEY *pk = NULL;
    FILE *in = fopen("pk.pem", "rt");

    if (!in)
        return 2;

    pk = PEM_read_EC_PUBKEY(in, NULL, NULL, NULL);

    fclose(in);
    return 0;
}

However, before returning from the PEM_read... call my program suddenly exits with a 1 code, with no further explanation. What can be the cause of this?

pk.pem:

-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEGQxkTNnZ3zp4NXgpnwhTkU2fzENB
RPXu3JJq1gx/CwZGh4mIWPbmHvd8eYUmffoPu4fPU6UxAQwGIH7+DEaKJw==
-----END PUBLIC KEY-----

sk.pem:

-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIElzeIrCIus5+2GIayg/i7VubOCQ1yqApAo2sau4dD1hoAoGCCqGSM49
AwEHoUQDQgAEGQxkTNnZ3zp4NXgpnwhTkU2fzENBRPXu3JJq1gx/CwZGh4mIWPbm
Hvd8eYUmffoPu4fPU6UxAQwGIH7+DEaKJw==
-----END EC PRIVATE KEY-----

Solution

  • The problem vanished simply by adding #include <openssl/applink.c> at the top. It's an issue with using OpenSSL on Windows, apparently.