Skip to content

Commit ce4869f

Browse files
committed
Fix bug #73478 (openssl_pkey_new() generates wrong pub/priv keys with Diffie Hellman)
1 parent 110a6cc commit ce4869f

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ PHP NEWS
2222
- Opcache:
2323
. Fixed bug #69090 (check cached files permissions)
2424

25+
- OpenSSL:
26+
. Fixed bug #73478 (openssl_pkey_new() generates wrong pub/priv keys with
27+
Diffie Hellman). (Jakub Zelenka)
28+
2529
- PCRE:
2630
. Fixed bug #73483 (Segmentation fault on pcre_replace_callback). (Laruence)
2731

ext/openssl/openssl.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4091,6 +4091,47 @@ zend_bool php_openssl_pkey_init_dsa(DSA *dsa, zval *data)
40914091
}
40924092
/* }}} */
40934093

4094+
/* {{{ php_openssl_dh_pub_from_priv */
4095+
static BIGNUM *php_openssl_dh_pub_from_priv(DH *dh, BIGNUM *priv_key, BIGNUM *g, BIGNUM *p)
4096+
{
4097+
BIGNUM *pub_key, *priv_key_const_time;
4098+
BN_CTX *ctx;
4099+
4100+
pub_key = BN_new();
4101+
if (pub_key == NULL) {
4102+
php_openssl_store_errors();
4103+
return NULL;
4104+
}
4105+
4106+
priv_key_const_time = BN_new();
4107+
if (priv_key_const_time == NULL) {
4108+
BN_free(pub_key);
4109+
php_openssl_store_errors();
4110+
return NULL;
4111+
}
4112+
ctx = BN_CTX_new();
4113+
if (priv_key_const_time == NULL) {
4114+
BN_free(pub_key);
4115+
BN_free(priv_key_const_time);
4116+
php_openssl_store_errors();
4117+
return NULL;
4118+
}
4119+
4120+
BN_with_flags(priv_key_const_time, priv_key, BN_FLG_CONSTTIME);
4121+
4122+
if (!BN_mod_exp_mont(pub_key, g, priv_key_const_time, p, ctx, NULL)) {
4123+
BN_free(pub_key);
4124+
php_openssl_store_errors();
4125+
pub_key = NULL;
4126+
}
4127+
4128+
BN_free(priv_key_const_time);
4129+
BN_CTX_free(ctx);
4130+
4131+
return pub_key;
4132+
}
4133+
/* }}} */
4134+
40944135
/* {{{ php_openssl_pkey_init_dh */
40954136
zend_bool php_openssl_pkey_init_dh(DH *dh, zval *data)
40964137
{
@@ -4108,6 +4149,13 @@ zend_bool php_openssl_pkey_init_dh(DH *dh, zval *data)
41084149
if (pub_key) {
41094150
return DH_set0_key(dh, pub_key, priv_key);
41104151
}
4152+
if (priv_key) {
4153+
pub_key = php_openssl_dh_pub_from_priv(dh, priv_key, g, p);
4154+
if (pub_key == NULL) {
4155+
return 0;
4156+
}
4157+
return DH_set0_key(dh, pub_key, priv_key);
4158+
}
41114159

41124160
/* generate key */
41134161
PHP_OPENSSL_RAND_ADD_TIME();

ext/openssl/tests/bug73478.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Bug #73478: openssl_pkey_new() generates wrong pub/priv keys with Diffie Hellman
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("openssl")) die("skip openssl not loaded");
6+
?>
7+
--FILE--
8+
<?php
9+
$details = [
10+
'p' => base64_decode('3Pk6C4g5cuwOGZiaxaLOMQ4dN3F+jZVxu3Yjcxhm5h73Wi4niYsFf5iRwuJ6Y5w/KbYIFFgc07LKOYbSaDcFV31FwuflLcgcehcYduXOp0sUSL/frxiCjv0lGfFOReOCZjSvGUnltTXMgppIO4p2Ij5dSQolfwW9/xby+yLFg6s='),
11+
'g' => base64_decode('Ag=='),
12+
'priv_key' => base64_decode('jUdcV++P/m7oUodWiqKqKXZVenHRuj92Ig6Fmzs7QlqVdUc5mNBxmEWjug+ObffanPpOeab/LyXwjNMzevtBz3tW4oROau++9EIMJVVQr8fW9zdYBJcYieC5l4t8nRj5/Uu/Z0G2rWVLBleSi28mqqNEvnUs7uxYxrar69lwQYs=')
13+
];
14+
15+
$opensslKeyResource = openssl_pkey_new(['dh' => $details]);
16+
$data = openssl_pkey_get_details($opensslKeyResource);
17+
18+
printf("Private key:\n%s\n", base64_encode($data['dh']['priv_key']));
19+
printf("Public key:\n%s\n", base64_encode($data['dh']['pub_key']));
20+
?>
21+
--EXPECT--
22+
Private key:
23+
jUdcV++P/m7oUodWiqKqKXZVenHRuj92Ig6Fmzs7QlqVdUc5mNBxmEWjug+ObffanPpOeab/LyXwjNMzevtBz3tW4oROau++9EIMJVVQr8fW9zdYBJcYieC5l4t8nRj5/Uu/Z0G2rWVLBleSi28mqqNEvnUs7uxYxrar69lwQYs=
24+
Public key:
25+
0DmJUe9dr02pAtVoGyLHdC+rfBU3mDCelKGPXRDFHofx6mFfN2gcZCmp/ab4ezDXfpIBOatpVdbn2fTNUGo64DtKE2WGTsZCl90RgrGUv8XW/4WDPXeE7g5u7KWHBG/LCE5+XsilE5P5/GIyqr9gsiudTmk+H/hiYZl9Smar9k0=

0 commit comments

Comments
 (0)