简体   繁体   English

如果还使用了 EVP_PKEY_free,C++ OpenSSL RSA_free 会出现分段错误

[英]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.更改了顺序,但没有奏效。

  • Does only one of them is enough to work correctly?只有其中之一就足以正常工作吗?

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.更改了顺序,但是没有用。

  • Does only one of them is enough to work correctly?仅其中之一足以正常工作吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 shared_ptr <EVP_PKEY> 使用EVP_PKEY_free作为自定义删除程序会导致堆损坏 - shared_ptr<EVP_PKEY> with EVP_PKEY_free as custom deleter causes heap corruption 使用free()时的分段错误 - segmentation fault when free() is used C ++“分段错误”或“free():无效指针”取决于输入(可重现) - C++ "Segmentation fault" or "free(): invalid pointer" depending on input (reproducible) 使用EVP_PKEY_assign_RSA()或EVP_PKEY_set1_RSA()后,RSA_size()和EVP_PKEY_size()出现段错误 - segmen fault for RSA_size() and EVP_PKEY_size() after use EVP_PKEY_assign_RSA() or EVP_PKEY_set1_RSA() SSL_free 是否也关闭对象的文件描述符? C++ OpenSSL - Does SSL_free also close the the object's file descriptors? C++ OpenSSL 尝试在OpenSSL中使用EVP功能时出现分段错误 - Segmentation fault when trying to use EVP functions in OpenSSL C ++ OpenSSL EVP_Digest使用RSA_R_BAD_PAD_BYTE_COUNT间歇性地验证rsa_pk1.c:103失败 - C++ OpenSSL EVP_DigestVerify intermittently failing rsa_pk1.c:103 with RSA_R_BAD_PAD_BYTE_COUNT C ++释放struct使用的所有内存 - C++ free all memory used by struct 用rsa evp签名数据的C ++代码 - c++ code for signing data with rsa evp 释放链表节点时出现分段错误 - Segmentation fault when free the linked list node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM