文章目录
前言
在将项目从 OpenSSL 1.1 升级到 OpenSSL 3.0 的过程中,开发者可能会遇到许多 API 的变化和弃用,导致开发者在实现某些功能时,找不到正确的实现方式。比如:如何实现将PKCS#1的公钥数据转换成EVP_PKEY结构。
一、问题分析
在一些场景中,我们需要将PKCS#1的相关数据转换成EVP_PKEY的格式。在openssl1.1中有相关的底层API
可以进行比较方便的转换,但在openssl3.0中,弃用了很多底层函数,这导致将PKCS#1格式的数据转换成
EVP_PKEY结构比较困难。
`在这里插入代码片`以下内容将分别介绍在openssl1.1和openssl3.0中如何进行转换
二、在openssl1.1下使用如下方式来实现将PKCS#1数据转换成EVP_PKEY结构
注:
- 以下实现可能存在释放不当的地方,使用时请自行测试,修改。
- 内存释放需要自行实现,示例实现见下文。
具体实现的代码如下:
int get_evp_public_key_rsa(const unsigned char *key, size_t keyLen, EVP_PKEY **evpKey)
{
if ((NULL == key) || (keyLen <= 0)) {
printf("param error\r\n");
return 0;
}
RSA* rsa = d2i_RSAPublicKey(NULL, (const unsigned char**)&key, (long)keyLen);
if (NULL == rsa) {
printf("d2i_RSAPublicKey : %ld, %s\r\n", ERR_get_error(), ERR_reason_error_string(ERR_get_error()));
return 0;
}
printf("d2i_RSAPublicKey success\r\n");
if (NULL == (*evpKey)) {
*evpKey = EVP_PKEY_new();
if (NULL == (*evpKey)) {
printf("EVP_PKEY_new error : %ld, %s\r\n", ERR_get_error(), ERR_reason_error_string(ERR_get_error()));
SZITRUS_OPENSSL_FREE(RSA, rsa);
return 0;
}
printf("EVP_PKEY_new success\r\n");
}
if (1 != EVP_PKEY_set1_RSA(*evpKey, rsa)) {
printf("EVP_PKEY_new error : %ld, %s\r\n", ERR_get_error(), ERR_reason_error_string(ERR_get_error()));
SZITRUS_OPENSSL_FREE(RSA, rsa);
SZITRUS_OPENSSL_FREE(EVP_PKEY, *evpKey);
*evpKey = NULL;
return 0;
}
printf("EVP_PKEY_set1_RSA success\r\n");
SZITRUS_OPENSSL_FREE(RSA, rsa);
return 1;
}
三、在openssl3.0下使用如下方式来实现将PKCS#1数据转换成EVP_PKEY结构
int get_evp_public_key_rsa_pkcs1(const unsigned char *key, size_t keyLen, EVP_PKEY **evpPublicKey)
{
if ((NULL == key) || (keyLen <= 0)) {
printf("param error\r\n");
return 0;
}
OSSL_DECODER_CTX *dctx = NULL;
unsigned char *data;
size_t dataLen = 0;
EVP_PKEY *tmpEvpPublicKey = NULL;
data = key;
dataLen = keyLen;
dctx = OSSL_DECODER_CTX_new_for_pkey(&tmpEvpPublicKey, "DER", "pkcs1", "RSA", OSSL_KEYMGMT_SELECT_PUBLIC_KEY, NULL, NULL);
if (dctx == NULL) {
printf("OSSL_DECODER_CTX_new_for_pkey error : %ld, %s\r\n", ERR_get_error(), ERR_reason_error_string(ERR_get_error()));
return 0;
}
if (1 == OSSL_DECODER_from_data(dctx, &data, &dataLen)) {
printf("OSSL_DECODER_from_data error : %ld, %s\r\n", ERR_get_error(), ERR_reason_error_string(ERR_get_error()));
WY_OPENSSL_FREE(OSSL_DECODER_CTX, dctx);
WY_OPENSSL_FREE(EVP_PKEY, tmpEvpPublicKey);
tmpEvpPublicKey = NULL;
return 0;
}
*evpPublicKey = EVP_PKEY_dup(tmpEvpPublicKey);
WY_OPENSSL_FREE(OSSL_DECODER_CTX, dctx);
WY_OPENSSL_FREE(EVP_PKEY, tmpEvpPublicKey);
return 1;
}
测试dome
#include <stdio.h>
#include "openssl/evp.h"
static const char rsa_pkcs1[275] = {
0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xe2, 0x3e,
0xa5, 0x2a, 0xd6, 0xcb, 0x6c, 0x02, 0x64, 0xcd, 0x89, 0xfa, 0xd5, 0x99, 0x72, 0x50, 0x7d, 0xb8,
0xb2, 0x27, 0xc9, 0x15, 0xda, 0xff, 0x10, 0x53, 0xdd, 0x69, 0xa5, 0xc4, 0x01, 0x02, 0xa9, 0xfe,
0x49, 0x71,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0f, 0x63, 0xe8, 0x59, 0x11, 0x7f, 0x11,
0x5e, 0xc3, 0x07, 0x92, 0x24, 0x57, 0x9d, 0x8c, 0x03, 0xf0, 0x69, 0xe0, 0xe6, 0x1d, 0x54, 0x00,
0x63, 0x22, 0x5a, 0x30, 0xf2, 0x84, 0x84, 0x2e, 0x90, 0xc7, 0x68, 0x8c, 0x94, 0x45, 0x02, 0x03,
0x01, 0x00, 0x01
};
int main()
{
int ret = 0;
char* publicKey = rsa_pkcs1;
size_t publicKeyLen = strlen(rsa_pkcs1);
EVP_PKEY *evpKey = NULL;
ret = get_evp_public_key_rsa_pkcs1(publicKey, publicKeyLen, &evpKey);
if (1 != ret) {
printf("get evp public key error, [%d]\r\n", ret);
return 1;
}
printf("get evp public key success\r\n");
WY_OPENSSL_FREE(EVP_PKEY, evpKey );
return 0;
}
四、openssl相关函数的内存释放示例
// openssl相关函数的内存释放示例
#define WY_OPENSSL_FREE(name, ptr) openssl_##name##_free(ptr)
void openssl_EVP_PKEY_free(EVP_PKEY *ptr)
{
if (NULL != ptr) {
EVP_PKEY_free(ptr);
ptr = NULL;
}
}
五、参考资料
六、总结
本文主要介绍在openssl3.0下如何将PKCS#1格式的公钥数据转换成EVP_PKEY结构
七、不足之处
转换出来的EVP_PKEY在使用该结构传入X509_check_private_key函数中会报错。