EVP_KEY在C中获取原始私钥

fkvaft9z  于 2022-12-11  发布在  其他
关注(0)|答案(1)|浏览(320)

祝你愉快,
我一直在尝试做一个简单的练习,我可以使用RSA和Openssl生成公钥和私钥,并打印它们。我的代码如下所示:

size_t private_key_len = KEY_LENGTH;
EVP_PKEY *pkey = EVP_RSA_gen(KEY_LENGTH);
if (pkey == NULL)
{
    fprintf(stderr, "error: rsa gen\n");
    ERR_print_errors_fp(stderr);
    return NULL;
}
unsigned char *private_key = calloc((KEY_LENGTH + 1),sizeof(unsigned char));
EVP_PKEY_get_raw_private_key(pkey, private_key, &private_key_len);
printf("%s\n",private_key);

所以通常它应该打印私钥,假设KEY_LENGTH是1024,但是它什么也不打印(由calloc初始化的零)。我也尝试过malloc,结果是类似的,唯一的区别是它打印0xBE。
所以基本上private_key数组是不填充的,我不知道为什么。
我错过了什么,使这工作?提前感谢!

c3frrgcw

c3frrgcw1#

Quoting the man page with emphasis changed:
EVP_PKEY_get_raw_private_key() fills the buffer provided by priv with raw private key data. The size of the priv buffer should be in *len on entry to the function, and on exit *len is updated with the number of bytes actually written. If the buffer priv is NULL then *len is populated with the number of bytes required to hold the key. The calling application is responsible for ensuring that the buffer is large enough to receive the private key data. This function only works for algorithms that support raw private keys. Currently this is: EVP_PKEY_HMAC, EVP_PKEY_POLY1305, EVP_PKEY_SIPHASH, EVP_PKEY_X25519, EVP_PKEY_ED25519, EVP_PKEY_X448 or EVP_PKEY_ED448.
Notice that RSA is not one of the supported algorithms.
You can "print" an RSA key either by converting each of its components (n,e,d,p,q,dp,dp,qinv) to printable form, which EVP_PKEY_print_private does for you, or getting the encoding of the whole key in PEM which is 'printable' in the sense of being printable and typable characters, but not the sense of being easily understood (or copied or created) by people, with PEM_write_PrivateKey or PEM_write_RSAPrivateKey .
Also, the value you pass to EVP_RSA_gen is in bits, but the size of displayed components of an RSA key (other than e, which is small) will be in hex or decimal digits or (mostly) base64 characters.

相关问题