Search code examples
c++visual-studio-2010openssl

PEM_read_X509() exits with code 0


I am trying to use OpenSSL in Visual Studio 2010 to read a .pem file and parse an x509 certificate. I got my code example form This tutorial the certificate is formatted in base 64, is named 'secondtry.pem' and looks like this:

-----BEGIN CERTIFICATE-----
MIIDHjCCAtygAwIBAgIEIDJHfjALBgcqhkjOOAQDBQAwYTELMAkGA1UEBhMCVVMxCzAJBgNVBAgT
AkZMMRIwEAYDVQQHEwlNZWxib3VybmUxDjAMBgNVBAoTBU1vbnRoMQwwCgYDVQQLEwNEYXkxEzAR
BgNVBAMTCkp1bHkgRWlnaHQwHhcNMTUwNzA4MTMwNDA2WhcNMTUxMDA2MTMwNDA2WjBhMQswCQYD
VQQGEwJVUzELMAkGA1UECBMCRkwxEjAQBgNVBAcTCU1lbGJvdXJuZTEOMAwGA1UEChMFTW9udGgx
DDAKBgNVBAsTA0RheTETMBEGA1UEAxMKSnVseSBFaWdodDCCAbcwggEsBgcqhkjOOAQBMIIBHwKB
gQD9f1OBHXUSKVLfSpwu7OTn9hG3UjzvRADDHj+AtlEmaUVdQCJR+1k9jVj6v8X1ujD2y5tVbNeB
O4AdNG/yZmC3a5lQpaSfn+gEexAiwk+7qdf+t8Yb+DtX58aophUPBPuD9tPFHsMCNVQTWhaRMvZ1
864rYdcq7/IiAxmd0UgBxwIVAJdgUI8VIwvMspK5gqLrhAvwWBz1AoGBAPfhoIXWmz3ey7yrXDa4
V7l5lK+7+jrqgvlXTAs9B4JnUVlXjrrUWU/mcQcQgYC0SRZxI+hMKBYTt88JMozIpuE8FnqLVHyN
KOCjrh4rs6Z1kW6jfwv6ITVi8ftiegEkO8yk8b6oUZCJqIPf4VrlnwaSi2ZegHtVJWQBTDv+z0kq
A4GEAAKBgDH20wAdrFDjcp2hJm2M9y/tm+VCnQP5sL8knITNrDoJXrj6NEkDNkjIlJrXbrPpWuM+
uNbmFOZQAusHNX4gyRfoJCWRAIyOAQ5RsUOEb7isdjnxplbRipFF81NQXJ4XVsZ8wzTZ5quUFhq8
TWOb7Nw6GuaM9BA5tcQZgPMNrZo9oyEwHzAdBgNVHQ4EFgQUtPDQywlbEA/oCiMwN7OXBaxYqoow
CwYHKoZIzjgEAwUAAy8AMCwCFE8buS4tUi3zdlKJzZrnjmFVp8jrAhRnGgZ5/sxU9cTg+1IWZPHx
kBMc7A==
-----END CERTIFICATE-----

I have a function to open the .pem file and assign it to an X509 object for later parsing. Here is the function:

X509* openPemFile(char* filename)
{
    X509* cert;
    FILE* certfile = fopen(filename, "rb");
    if(!certfile)
    {
        fprintf( stdout, "Unable to open file %s\n", filename);
        return NULL;
    }


    PEM_read_X509(certfile, &cert, 0, NULL);

    if(!cert)
    {
        fprintf(stdout, "Unable to parse certificate in: %s\n", filename);
        fclose(certfile);
        return NULL;
    }

    return cert;
}

I am calling the function like so:

X509* cert = openPemFile("secondtry.pem");

the file opens without error, but when my code gets to the line:

PEM_read_X509(certfile, &cert, 0, NULL);

it exits with "Native' has exited with code 0 (0x0)."

I have tried calling the PEM_read_x509 function several different ways:

 X509* cert = PEM_read_X509(certfile, NULL, NULL, NULL);
    PEM_read_X509(certfile, &cert, NULL, NULL);

I have also tried opening the file with the 'r' flag instead of 'rb'. I have also trawled google and SO for several hours. All of this to no avail. There is no error and the function does not return NULL, it just exits with code 0. How can I get this to work properly so that I end up with a X509 object?


Solution

  • Well, I still don't know why the PEM_read_x509 function doesn't work, but I believe that I have found a workaround.

    X509* openPemFile(char* filename)
    {
    
        X509* cert = X509_new();
        BIO* bio_cert = BIO_new_file(filename, "rb");
        PEM_read_bio_X509(bio_cert, &cert, NULL, NULL);
        return cert;
    }
    

    This returns a certificate that I can get data from. The two main differences are the initializing of cert with a new() call, and the use of the PEM_read_bio_X509 function. I'm not sure why, but this gives me a useable certificate object. I tried the pervious function, the PEM_read_X509, and it did not work, even with the initialized object.