Skip to content

Commit 54310d9

Browse files
committed
Fix bug #72336 (openssl_pkey_new does not fail for invalid DSA params)
1 parent 4056edd commit 54310d9

File tree

3 files changed

+71
-8
lines changed

3 files changed

+71
-8
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2016, PHP 5.6.24
44

5+
- OpenSSL:
6+
. Fixed bug #72336 (openssl_pkey_new does not fail for invalid DSA params).
7+
(Jakub Zelenka)
8+
59
23 Jun 2016, PHP 5.6.23
610

711
- GD:

ext/openssl/openssl.c

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3531,6 +3531,44 @@ static int php_openssl_is_private_key(EVP_PKEY* pkey TSRMLS_DC)
35313531
} \
35323532
} while (0);
35333533

3534+
/* {{{ php_openssl_pkey_init_dsa */
3535+
zend_bool php_openssl_pkey_init_dsa(DSA *dsa)
3536+
{
3537+
if (!dsa->p || !dsa->q || !dsa->g) {
3538+
return 0;
3539+
}
3540+
if (dsa->priv_key || dsa->pub_key) {
3541+
return 1;
3542+
}
3543+
if (!DSA_generate_key(dsa)) {
3544+
return 0;
3545+
}
3546+
/* if BN_mod_exp return -1, then DSA_generate_key succeed for failed key
3547+
* so we need to double check that public key is created */
3548+
if (!dsa->pub_key || BN_is_zero(dsa->pub_key)) {
3549+
return 0;
3550+
}
3551+
/* all good */
3552+
return 1;
3553+
}
3554+
/* }}} */
3555+
3556+
/* {{{ php_openssl_pkey_init_dh */
3557+
zend_bool php_openssl_pkey_init_dh(DH *dh)
3558+
{
3559+
if (!dh->p || !dh->g) {
3560+
return 0;
3561+
}
3562+
if (dh->pub_key) {
3563+
return 1;
3564+
}
3565+
if (!DH_generate_key(dh)) {
3566+
return 0;
3567+
}
3568+
/* all good */
3569+
return 1;
3570+
}
3571+
/* }}} */
35343572

35353573
/* {{{ proto resource openssl_pkey_new([array configargs])
35363574
Generates a new private key */
@@ -3583,10 +3621,7 @@ PHP_FUNCTION(openssl_pkey_new)
35833621
OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, g);
35843622
OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, priv_key);
35853623
OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, pub_key);
3586-
if (dsa->p && dsa->q && dsa->g) {
3587-
if (!dsa->priv_key && !dsa->pub_key) {
3588-
DSA_generate_key(dsa);
3589-
}
3624+
if (php_openssl_pkey_init_dsa(dsa)) {
35903625
if (EVP_PKEY_assign_DSA(pkey, dsa)) {
35913626
RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
35923627
}
@@ -3606,10 +3641,10 @@ PHP_FUNCTION(openssl_pkey_new)
36063641
OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, g);
36073642
OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, priv_key);
36083643
OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, pub_key);
3609-
if (dh->p && dh->g &&
3610-
(dh->pub_key || DH_generate_key(dh)) &&
3611-
EVP_PKEY_assign_DH(pkey, dh)) {
3612-
RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
3644+
if (php_openssl_pkey_init_dh(dh)) {
3645+
if (EVP_PKEY_assign_DH(pkey, dh)) {
3646+
RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
3647+
}
36133648
}
36143649
DH_free(dh);
36153650
}

ext/openssl/tests/bug72336.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Bug #72336 (openssl_pkey_new does not fail for invalid DSA params)
3+
--SKIPIF--
4+
<?php if (!extension_loaded("openssl")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$p = '00f8000ae45b2dacb47dd977d58b719d097bdf07cb2c17660ad898518c08' .
8+
'1a61659a16daadfaa406a0a994c743df5eda07e36bd0adcad921b77432ff' .
9+
'24ccc31e782d647e66768122b578857e9293df78387dc8b44af2a4a3f305' .
10+
'1f236b1000a3e31da489c6681b0031f7ec37c2e1091bdb698e7660f135b6' .
11+
'996def90090303b7ad';
12+
13+
$q = '009b3734fc9f7a4a9d6437ec314e0a78c2889af64b';
14+
15+
$g = '00b320300a0bc55b8f0ec6edc218e2185250f38fbb8291db8a89227f6e41' .
16+
'00d47d6ccb9c7d42fc43280ecc2ed386e81ff65bc5d6a2ae78db7372f5dc' .
17+
'f780f4558e7ed3dd0c96a1b40727ac56c5165aed700a3b63997893a1fb21' .
18+
'4e882221f0dd9604820dc34e2725dd6901c93e0ca56f6d76d495c332edc5' .
19+
'b81747c4c447a941f3';
20+
21+
var_dump(openssl_pkey_new(array('dsa' => array('p' => $p, 'q' => $q, 'g' => $g))));
22+
?>
23+
--EXPECT--
24+
bool(false)

0 commit comments

Comments
 (0)