Skip to content

Commit 0e2447c

Browse files
committed
Fix bug #71915 (openssl_random_pseudo_bytes is not fork-safe)
Add time to the entropy before using RAND_bytes
1 parent 54310d9 commit 0e2447c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

NEWS

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

55
- OpenSSL:
6+
. Fixed bug #71915 (openssl_random_pseudo_bytes is not fork-safe).
7+
(Jakub Zelenka)
68
. Fixed bug #72336 (openssl_pkey_new does not fail for invalid DSA params).
79
(Jakub Zelenka)
810

ext/openssl/openssl.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,22 @@ static void php_openssl_dispose_config(struct php_x509_request * req TSRMLS_DC)
967967
}
968968
/* }}} */
969969

970+
#ifdef PHP_WIN32
971+
#define PHP_OPENSSL_RAND_ADD_TIME() ((void) 0)
972+
#else
973+
#define PHP_OPENSSL_RAND_ADD_TIME() php_openssl_rand_add_timeval()
974+
975+
static inline void php_openssl_rand_add_timeval() /* {{{ */
976+
{
977+
struct timeval tv;
978+
979+
gettimeofday(&tv, NULL);
980+
RAND_add(&tv, sizeof(tv), 0.0);
981+
}
982+
/* }}} */
983+
984+
#endif
985+
970986
static int php_openssl_load_rand_file(const char * file, int *egdsocket, int *seeded TSRMLS_DC) /* {{{ */
971987
{
972988
char buffer[MAXPATHLEN];
@@ -1010,6 +1026,7 @@ static int php_openssl_write_rand_file(const char * file, int egdsocket, int see
10101026
if (file == NULL) {
10111027
file = RAND_file_name(buffer, sizeof(buffer));
10121028
}
1029+
PHP_OPENSSL_RAND_ADD_TIME();
10131030
if (file == NULL || !RAND_write_file(file)) {
10141031
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to write random state");
10151032
return FAILURE;
@@ -3399,12 +3416,14 @@ static EVP_PKEY * php_openssl_generate_private_key(struct php_x509_request * req
33993416
if ((req->priv_key = EVP_PKEY_new()) != NULL) {
34003417
switch(req->priv_key_type) {
34013418
case OPENSSL_KEYTYPE_RSA:
3419+
PHP_OPENSSL_RAND_ADD_TIME();
34023420
if (EVP_PKEY_assign_RSA(req->priv_key, RSA_generate_key(req->priv_key_bits, 0x10001, NULL, NULL))) {
34033421
return_val = req->priv_key;
34043422
}
34053423
break;
34063424
#if !defined(NO_DSA) && defined(HAVE_DSA_DEFAULT_METHOD)
34073425
case OPENSSL_KEYTYPE_DSA:
3426+
PHP_OPENSSL_RAND_ADD_TIME();
34083427
{
34093428
DSA *dsapar = DSA_generate_parameters(req->priv_key_bits, NULL, 0, NULL, NULL, NULL, NULL);
34103429
if (dsapar) {
@@ -3422,6 +3441,7 @@ static EVP_PKEY * php_openssl_generate_private_key(struct php_x509_request * req
34223441
#endif
34233442
#if !defined(NO_DH)
34243443
case OPENSSL_KEYTYPE_DH:
3444+
PHP_OPENSSL_RAND_ADD_TIME();
34253445
{
34263446
DH *dhpar = DH_generate_parameters(req->priv_key_bits, 2, NULL, NULL);
34273447
int codes = 0;
@@ -3540,6 +3560,7 @@ zend_bool php_openssl_pkey_init_dsa(DSA *dsa)
35403560
if (dsa->priv_key || dsa->pub_key) {
35413561
return 1;
35423562
}
3563+
PHP_OPENSSL_RAND_ADD_TIME();
35433564
if (!DSA_generate_key(dsa)) {
35443565
return 0;
35453566
}
@@ -3562,6 +3583,7 @@ zend_bool php_openssl_pkey_init_dh(DH *dh)
35623583
if (dh->pub_key) {
35633584
return 1;
35643585
}
3586+
PHP_OPENSSL_RAND_ADD_TIME();
35653587
if (!DH_generate_key(dh)) {
35663588
return 0;
35673589
}
@@ -5461,6 +5483,7 @@ PHP_FUNCTION(openssl_random_pseudo_bytes)
54615483
RETURN_FALSE;
54625484
}
54635485
#else
5486+
PHP_OPENSSL_RAND_ADD_TIME();
54645487
if (RAND_bytes(buffer, buffer_length) <= 0) {
54655488
efree(buffer);
54665489
if (zstrong_result_returned) {

0 commit comments

Comments
 (0)