2010-05-27 2 views
2

다음과 같이 서명되지 않은 char 버퍼의 공개 키를 가져 오는 DER 인증서가 있습니다. 올바른 방법입니까?x509 (PKCS7)의 char 버퍼에서 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); 

이 서명되지 않은 char 버퍼로, 어떻게 RSA 용 EVP_PKEY를 얻을 수 있습니까? 또는 나는 부호 문자 버퍼로 변환 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); 

답변

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

에 따라 사용 가능 .

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