Skip to content

Commit 178e646

Browse files
chincheta0815h-vn
authored andcommitted
Use RSA_generate_key_ex instead of deprecated RSA_generate_key
* Make use of RSA_generate_key_ex(). openssl 0.9.8 deprecated RSA_generate_key (see https://www.openssl.org/docs/man1.1.1/man3/RSA_generate_key.html) openssl 1.1.0 it is not possible anymore to generate the BN_GENCB structure directly (see https://www.openssl.org/docs/man1.1.1/man3/BN_GENCB_new.html) * RSA_generate_kex_ex(): add check for libreSSL see: #100 (comment) * SSLeay.xs: add error handling for RSA_new, BN_new and BN_GENCB calls. * Free more before calling croak.
1 parent 20ecb22 commit 178e646

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

Changes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
Revision history for Perl extension Net::SSLeay.
22

3+
??????? 2018-??-??
4+
- Net::SSLeay::RSA_generate_key() now prefers using
5+
RSA_generate_key_ex. This avois deprecated RSA_generate_key
6+
and allows removing the only Android specific code in
7+
SSLeay.xs. Fixes RT#127593. Thanks to Rouven Weiler.
8+
39
1.86_06 2018-09-29
410
- Net::SSLeay::read() and SSL_peek() now check SSL_get_error()
511
for SSL_ERROR_ZERO_RETURN for return values <= 0 to make

SSLeay.xs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5744,7 +5744,7 @@ SSL_set_tmp_rsa(ssl,rsa)
57445744

57455745
#endif
57465746

5747-
#ifdef __ANDROID__
5747+
#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
57485748

57495749
RSA *
57505750
RSA_generate_key(bits,ee,perl_cb=&PL_sv_undef,perl_data=&PL_sv_undef)
@@ -5755,24 +5755,50 @@ RSA_generate_key(bits,ee,perl_cb=&PL_sv_undef,perl_data=&PL_sv_undef)
57555755
PREINIT:
57565756
simple_cb_data_t* cb_data = NULL;
57575757
CODE:
5758-
/* Android does not have RSA_generate_key. This equivalent is contributed by Brian Fraser for Android */
5759-
/* but is not portable to old OpenSSLs where RSA_generate_key_ex is not available */
5758+
/* openssl 0.9.8 deprecated RSA_generate_key. */
5759+
/* This equivalent was contributed by Brian Fraser for Android, */
5760+
/* but was not portable to old OpenSSLs where RSA_generate_key_ex is not available. */
5761+
/* It should now be more versatile. */
5762+
/* as of openssl 1.1.0 it is not possible anymore to generate the BN_GENCB structure directly. */
5763+
/* instead BN_EGNCB_new() has to be used. */
57605764
int rc;
57615765
RSA * ret;
57625766
BIGNUM *e;
57635767
e = BN_new();
5768+
if(!e)
5769+
croak("Net::SSLeay: RSA_generate_key perl function could not create BN structure.\n");
57645770
BN_set_word(e, ee);
57655771
cb_data = simple_cb_data_new(perl_cb, perl_data);
5766-
BN_GENCB new_cb;
5767-
BN_GENCB_set_old(&new_cb, ssleay_RSA_generate_key_cb_invoke, cb_data);
57685772

57695773
ret = RSA_new();
5774+
if(!ret) {
5775+
simple_cb_data_free(cb_data);
5776+
BN_free(e);
5777+
croak("Net::SSLeay: RSA_generate_key perl function could not create RSA structure.\n");
5778+
}
5779+
#if (OPENSSL_VERSION_NUMBER >= 0x1010000fL && !defined(LIBRESSL_VERSION_NUMBER)) || (LIBRESSL_VERSION_NUMBER >= 0x2070000fL)
5780+
BN_GENCB *new_cb;
5781+
new_cb = BN_GENCB_new();
5782+
if(!new_cb) {
5783+
simple_cb_data_free(cb_data);
5784+
BN_free(e);
5785+
RSA_free(ret);
5786+
croak("Net::SSLeay: RSA_generate_key perl function could not create BN_GENCB structure.\n");
5787+
}
5788+
BN_GENCB_set_old(new_cb, ssleay_RSA_generate_key_cb_invoke, cb_data);
5789+
rc = RSA_generate_key_ex(ret, bits, e, new_cb);
5790+
BN_GENCB_free(new_cb);
5791+
#else
5792+
BN_GENCB new_cb;
5793+
BN_GENCB_set_old(&new_cb, ssleay_RSA_generate_key_cb_invoke, cb_data);
57705794
rc = RSA_generate_key_ex(ret, bits, e, &new_cb);
5771-
5772-
if (rc == -1 || ret == NULL)
5773-
croak("Couldn't generate RSA key");
5795+
#endif
57745796
simple_cb_data_free(cb_data);
57755797
BN_free(e);
5798+
if (rc == -1 || ret == NULL) {
5799+
if (ret) RSA_free(ret);
5800+
croak("Net::SSLeay: Couldn't generate RSA key");
5801+
}
57765802
e = NULL;
57775803
RETVAL = ret;
57785804
OUTPUT:

0 commit comments

Comments
 (0)