如果您拥有原始公钥,则必须使用正确的参数创建 EC_Key。
EVP_PKEY * get_peerkey(const unsigned char * buffer, size_t buffer_len)
{
EC_KEY *tempEcKey = NULL;
EVP_PKEY *peerkey = NULL;
// change this if another curve is required
tempEcKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
if(tempEcKey == NULL) {
handleErrors();
}
if(EC_KEY_oct2key(tempEcKey, buffer, buffer_len, NULL) != 1) {
handleErrors();
}
if(EC_KEY_check_key(tempEcKey) != 1) {
handleErrors();
}
peerkey = EVP_PKEY_new();
if(peerkey == NULL) {
handleErrors();
}
if(EVP_PKEY_assign_EC_KEY(peerkey, tempEcKey)!= 1) {
handleErrors();
}
return peerkey;
}
如果您将公钥作为 ASN.1 序列,则可以使用内部转换方法:
EVP_PKEY* get_peerkey(const unsigned char *buffer, size_t buffer_len)
{
EVP_PKEY *peerkey = NULL;
const unsigned char *helper = buffer;
// from "openssl/x509.h"
peerkey = d2i_PUBKEY(NULL, &helper, buffer_len);
if (!peerkey) {
handleErrors();
return NULL;
}
return peerkey;
}