user18743773
user18743773

Reputation: 21

Exception when trying to get data from SSL certificate

I am trying to decode a certificate my server is sending for each of the objects I enroll. I am trying to get the serial and the validity date for the objects. I have my certificate stored in a std::string called certStr and the code I have is:

OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();

BIO* input(BIO_new(BIO_s_mem()));
BIO_puts(input, certStr.c_str());

// Create an openssl certificate from the BIO
X509* cert(PEM_read_bio_X509(input, NULL, NULL, NULL));

// Create a BIO to hold info from the cert
BIO* output_bio(BIO_new(BIO_s_mem()));

ASN1_INTEGER* bs = X509_get_serialNumber(cert);

and I am getting an exception

EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000008

with that last X509_get_serialNumber function. Do you know what could I be doing wrong? I am pretty new to OpenSSL, so I may have done something stupid that I am not realizing. I am also following this and this code snippets that supposedly do what I am trying to do.

Upvotes: 0

Views: 156

Answers (1)

user18743773
user18743773

Reputation: 21

Okay, I found the problem. The server is passing me a PKCS7 certificate, not a PEM certificate, so first I will have to extract the cert from the PKCS7 and then parse the certificate

Upvotes: 1

Related Questions