如果还使用 EVP_PKEY_free,则 C++ OpenSSL RSA_free 会出现分段错误
C++ OpenSSL RSA_free give Segmentation fault if EVP_PKEY_free is also used
我正在编写一个小程序来测试 RSA 上的旧问题。我需要访问模数的主要因素。所以我的代码是
int RSAKeyGen(int keySize) {
EVP_PKEY *pkey = EVP_PKEY_new();
BIGNUM *bn = BN_new();
BN_set_word(bn, RSA_F4);
RSA *rsa = RSA_new();
RSA_generate_key_ex(rsa, keySize, bn, NULL);
EVP_PKEY_assign_RSA(pkey, rsa);
const BIGNUM *p;
const BIGNUM *q;
RSA_get0_factors(rsa, &p,&q);
BN_print_fp(stdout, p);
puts("\n");
BN_print_fp(stdout, q);
RSA_free(rsa);
EVP_PKEY_free(pkey);
BN_free(bn);
return 0;
}
同时拥有 EVP_PKEY_free(pkey);
和 RSA_free(rsa);
得到
`Segmentation fault (core dumped)`
评论其中一个工作正常,除了可能没有释放某些东西。改了顺序还是不行。
- 仅其中一个就足以正常工作吗?
请在此处查看 EVP_PKEY_assign_RSA
的文档:
https://www.openssl.org/docs/man1.1.1/man3/EVP_PKEY_assign_RSA.html
如该页面所述:
EVP_PKEY_assign_RSA(), EVP_PKEY_assign_DSA(), EVP_PKEY_assign_DH(), EVP_PKEY_assign_EC_KEY(), EVP_PKEY_assign_POLY1305() and EVP_PKEY_assign_SIPHASH() also set the referenced key to key however these use the supplied key internally and so key will be freed when the parent pkey is freed.
换句话说,通过 EVP_PKEY_assign_RSA()
调用分配的密钥的所有权已转移到 EVP_PKEY。当您释放 EVP_PKEY
时,它也会释放底层的 RSA
密钥。因此,一旦您成功调用 EVP_PKEY_assign_RSA()
,您就不能在基础密钥上调用 RSA_free()
,否则可能会导致双重释放。
我正在编写一个小程序来测试 RSA 上的旧问题。我需要访问模数的主要因素。所以我的代码是
int RSAKeyGen(int keySize) {
EVP_PKEY *pkey = EVP_PKEY_new();
BIGNUM *bn = BN_new();
BN_set_word(bn, RSA_F4);
RSA *rsa = RSA_new();
RSA_generate_key_ex(rsa, keySize, bn, NULL);
EVP_PKEY_assign_RSA(pkey, rsa);
const BIGNUM *p;
const BIGNUM *q;
RSA_get0_factors(rsa, &p,&q);
BN_print_fp(stdout, p);
puts("\n");
BN_print_fp(stdout, q);
RSA_free(rsa);
EVP_PKEY_free(pkey);
BN_free(bn);
return 0;
}
同时拥有 EVP_PKEY_free(pkey);
和 RSA_free(rsa);
得到
`Segmentation fault (core dumped)`
评论其中一个工作正常,除了可能没有释放某些东西。改了顺序还是不行。
- 仅其中一个就足以正常工作吗?
请在此处查看 EVP_PKEY_assign_RSA
的文档:
https://www.openssl.org/docs/man1.1.1/man3/EVP_PKEY_assign_RSA.html
如该页面所述:
EVP_PKEY_assign_RSA(), EVP_PKEY_assign_DSA(), EVP_PKEY_assign_DH(), EVP_PKEY_assign_EC_KEY(), EVP_PKEY_assign_POLY1305() and EVP_PKEY_assign_SIPHASH() also set the referenced key to key however these use the supplied key internally and so key will be freed when the parent pkey is freed.
换句话说,通过 EVP_PKEY_assign_RSA()
调用分配的密钥的所有权已转移到 EVP_PKEY。当您释放 EVP_PKEY
时,它也会释放底层的 RSA
密钥。因此,一旦您成功调用 EVP_PKEY_assign_RSA()
,您就不能在基础密钥上调用 RSA_free()
,否则可能会导致双重释放。