@@ -3531,6 +3531,44 @@ static int php_openssl_is_private_key(EVP_PKEY* pkey TSRMLS_DC)
3531
3531
} \
3532
3532
} while (0);
3533
3533
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
+ /* }}} */
3534
3572
3535
3573
/* {{{ proto resource openssl_pkey_new([array configargs])
3536
3574
Generates a new private key */
@@ -3583,10 +3621,7 @@ PHP_FUNCTION(openssl_pkey_new)
3583
3621
OPENSSL_PKEY_SET_BN (Z_ARRVAL_PP (data ), dsa , g );
3584
3622
OPENSSL_PKEY_SET_BN (Z_ARRVAL_PP (data ), dsa , priv_key );
3585
3623
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 )) {
3590
3625
if (EVP_PKEY_assign_DSA (pkey , dsa )) {
3591
3626
RETURN_RESOURCE (zend_list_insert (pkey , le_key TSRMLS_CC ));
3592
3627
}
@@ -3606,10 +3641,10 @@ PHP_FUNCTION(openssl_pkey_new)
3606
3641
OPENSSL_PKEY_SET_BN (Z_ARRVAL_PP (data ), dh , g );
3607
3642
OPENSSL_PKEY_SET_BN (Z_ARRVAL_PP (data ), dh , priv_key );
3608
3643
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
+ }
3613
3648
}
3614
3649
DH_free (dh );
3615
3650
}
0 commit comments