[英]C++ OpenSSL RSA_free give Segmentation fault if EVP_PKEY_free is also used
I'm writing a small program to test an old issue on RSA.我正在编写一个小程序来测试 RSA 上的一个老问题。 I need to access the prime factors of the modulus.
我需要访问模数的主要因素。 So my code is
所以我的代码是
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;
}
Having both EVP_PKEY_free(pkey);
同时拥有
EVP_PKEY_free(pkey);
and RSA_free(rsa);
和
RSA_free(rsa);
gives给
`Segmentation fault (core dumped)`
commenting one of them is working fine, except may be something is not freed.评论其中一个工作正常,除了可能没有释放某些东西。 Changed the order but did not worked.
更改了顺序,但没有奏效。
See the documentation for EVP_PKEY_assign_RSA
here:在此处查看
EVP_PKEY_assign_RSA
的文档:
https://www.openssl.org/docs/man1.1.1/man3/EVP_PKEY_assign_RSA.html https://www.openssl.org/docs/man1.1.1/man3/EVP_PKEY_assign_RSA.html
As stated on that page:如该页面所述:
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_assign_DSA()、EVP_PKEY_assign_DH()、EVP_PKEY_assign_EC_KEY()、EVP_PKEY_assign_POLY1305() 和 EVP_PKEY_assign_SIPHASH() 也将引用的密钥设置为密钥,但是当这些密钥在内部被释放时,这些使用将被释放的密钥。
So in other words ownership of the key assigned via the EVP_PKEY_assign_RSA()
call is transferred to the EVP_PKEY.因此,换句话说,通过
EVP_PKEY_assign_RSA()
调用分配的密钥的所有权将转移到 EVP_PKEY。 When you free the EVP_PKEY
it also frees the underlying RSA
key.当您释放
EVP_PKEY
它也会释放底层RSA
密钥。 So, once you've successfully called EVP_PKEY_assign_RSA()
you must not call RSA_free()
on the underlying key or a double-free may result.因此,一旦您成功调用了
EVP_PKEY_assign_RSA()
,就不能在底层密钥上调用RSA_free()
否则可能会导致双重释放。
I'm writing a small program to test an old issue on RSA.我正在编写一个小程序来测试RSA上的旧问题。 I need to access the prime factors of the modulus.
我需要访问模数的质因数。 So my code is
所以我的代码是
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;
}
Having both EVP_PKEY_free(pkey);
同时拥有
EVP_PKEY_free(pkey);
and RSA_free(rsa);
和
RSA_free(rsa);
gives给
`Segmentation fault (core dumped)`
commenting one of them is working fine, except may be something is not freed.评论其中之一工作正常,除非可能无法释放某些内容。 Changed the order but did not worked.
更改了顺序,但是没有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.