Reputation: 813
I am getting a DER encoded certificate (after base64 decode) from an ldap search request and need to parse the public key from it. I am pretty sure that its possible with the openssl library. Unfortunately the API documentation is not very well-kept. Is there any example or other library to extract the information?
Upvotes: 2
Views: 4790
Reputation: 9395
Use d2i_X509 to get the certificate in X509 * structure. After that use X509_get_pubkey to get the public key. X509_get_pubkey will give you public key in EVP_PKEY * structure. I hope this must solve your purpose.
If your certificate is in PEM format (Base64 encoded wrapped by "-----BEGIN CERTIFICATE-----") , then you can also use PEM_read_X509 to get X509 * object directly.
Example:
//Get the X509 object.
//Say certificate is encoded in a file
X509 * xcert = PEM_read_X509(fp, NULL, NULL, NULL);
//or assuming DER encoded certificate in buf with length of buffer is buflen.
X509 * xcert = d2i_X509(NULL, buf, buflen);
//Get the public key.
EVP_PKEY * pubkey = X509_get_pubkey(xcert);
//later free this pubkey object when no longer required.
EVP_PKEY_free(pubkey);
Upvotes: 3