I have a DER certificate from which I am retrieving the Public key in unsigned char buffer as following, is it the right way of getting?
pStoredPublicKey =
The following openssl API works for unsigned char buffer to EVP_PKEY,
EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, unsigned char **pp, long length);
int i2d_PublicKey(EVP_PKEY *a, unsigned char **pp);
And, the following works for Convert EVP_PKEY to unsigned char buffer.
int pkeyLen;
unsigned char *ucBuf, *uctempBuf;
pkeyLen = i2d_PublicKey(pkey, NULL);
ucBuf = (unsigned char *)malloc(pkeyLen+1);
uctempBuf = ucBuf;
i2d_PublicKey(pkey, &uctempBuf);
int ii;
for (ii = 0; ii < pkeyLen; ii++)
{
printf("%02x\n", (unsigned char) ucBuf[ii]);
}
Thanks-opensid
Convert EVP_PKEY to character buffer.
char *EVP_PKEY_to_PEM (EVP_PKEY *pkey)
{
BIO *bio = NULL;
char *pem = NULL;
if (NULL == pkey)
return NULL;
if ((bio = BIO_new(BIO_s_mem())) == NULL)
return NULL;
if (0 == PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL)){
BIO_free(bio);
return NULL;
}
pem = (char *) calloc(1, bio->num_write + 1);
BIO_read(bio, pem, bio->num_write);
BIO_free(bio);
return pem;
}