2010-05-27 52 views
2

我有一個DER證書,我從中檢索公鑰在無符號字符緩衝區如下,它是正確的方式獲取?從字符緩衝區x509(PKCS7)EVP_PKEY

pStoredPublicKey = X509_get_pubkey(x509); 
if(pStoredPublicKey == NULL) 
{ 
     printf(": publicKey is NULL\n"); 
} 
if(pStoredPublicKey->type == EVP_PKEY_RSA) { 
     RSA *x = pStoredPublicKey->pkey.rsa; 
     bn = x->n; 
} 
else if(pStoredPublicKey->type == EVP_PKEY_DSA) { 

} 
else if(pStoredPublicKey->type == EVP_PKEY_EC) { 
} 
else { 
     printf(" : Unkown publicKey\n"); 
} 
//extracts the bytes from public key & convert into unsigned char buffer 
buf_len = (size_t) BN_num_bytes (bn); 
key = (unsigned char *)malloc (buf_len); 
n = BN_bn2bin (bn, (unsigned char *) key); 
for (i = 0; i < n; i++) 
{ 
     printf("%02x\n", (unsigned char) key[i]); 
} 
keyLen = EVP_PKEY_size(pStoredPublicKey); 
EVP_PKEY_free(pStoredPublicKey); 

,與此無符號的字符緩衝區,如何獲得回來RSA的EVP_PKEY? OR我可以透過以下???,

EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, unsigned char **pp, long length); 
int i2d_PublicKey(EVP_PKEY *a, unsigned char **pp); 

回答

7

以下OpenSSL的API適用於無符號字符緩衝區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); 

而且,下面的作品轉換EVP_PKEY爲unsigned char緩衝區。

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]); 
} 

感謝-opensid