1 diff -ruNp php-5.3.29.orig/acinclude.m4 php-5.3.29/acinclude.m4
2 --- php-5.3.29.orig/acinclude.m4 2014-08-13 21:22:50.000000000 +0200
3 +++ php-5.3.29/acinclude.m4 2019-02-05 23:42:46.539242592 +0100
4 @@ -2331,8 +2331,10 @@ AC_DEFUN([PHP_SETUP_OPENSSL],[
5 AC_MSG_ERROR([OpenSSL version 0.9.6 or greater required.])
8 - if test -n "$OPENSSL_LIBS" && test -n "$OPENSSL_INCS"; then
9 + if test -n "$OPENSSL_LIBS"; then
10 PHP_EVAL_LIBLINE($OPENSSL_LIBS, $1)
12 + if test -n "$OPENSSL_INCS"; then
13 PHP_EVAL_INCLINE($OPENSSL_INCS)
16 diff -ruNp php-5.3.29.orig/ext/openssl/openssl.c php-5.3.29/ext/openssl/openssl.c
17 --- php-5.3.29.orig/ext/openssl/openssl.c 2014-08-13 21:22:50.000000000 +0200
18 +++ php-5.3.29/ext/openssl/openssl.c 2019-02-05 23:45:52.821213335 +0100
24 +#if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
25 +#define PHP_OPENSSL_RAND_ADD_TIME() ((void) 0)
27 +#define PHP_OPENSSL_RAND_ADD_TIME() php_openssl_rand_add_timeval()
30 /* FIXME: Use the openssl constants instead of
31 * enum. It is now impossible to match real values
32 * against php constants. Also sorry to break the
33 @@ -881,11 +888,6 @@ static int php_openssl_load_rand_file(co
36 file = RAND_file_name(buffer, sizeof(buffer));
37 - } else if (RAND_egd(file) > 0) {
38 - /* if the given filename is an EGD socket, don't
39 - * write anything back to it */
43 if (file == NULL || !RAND_load_file(file, -1)) {
44 if (RAND_status() == 0) {
45 @@ -939,9 +941,11 @@ static EVP_MD * php_openssl_get_evp_md_f
46 mdtype = (EVP_MD *) EVP_md2();
49 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
50 case OPENSSL_ALGO_DSS1:
51 mdtype = (EVP_MD *) EVP_dss1();
57 @@ -989,14 +993,17 @@ PHP_MINIT_FUNCTION(openssl)
58 le_x509 = zend_register_list_destructors_ex(php_x509_free, NULL, "OpenSSL X.509", module_number);
59 le_csr = zend_register_list_destructors_ex(php_csr_free, NULL, "OpenSSL X.509 CSR", module_number);
61 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
62 + OPENSSL_config(NULL);
64 OpenSSL_add_all_ciphers();
65 OpenSSL_add_all_digests();
66 OpenSSL_add_all_algorithms();
68 - ERR_load_ERR_strings();
69 - ERR_load_crypto_strings();
70 - ERR_load_EVP_strings();
71 + SSL_load_error_strings();
73 + OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL);
76 /* register a resource id number with OpenSSL so that we can map SSL -> stream structures in
77 * OpenSSL callbacks */
78 @@ -1344,6 +1351,7 @@ static int openssl_x509v3_subjectAltName
81 const X509V3_EXT_METHOD *method = NULL;
82 + ASN1_OCTET_STRING *extension_data;
84 const unsigned char *p;
86 @@ -1352,8 +1360,9 @@ static int openssl_x509v3_subjectAltName
90 - p = extension->value->data;
91 - length = extension->value->length;
92 + extension_data = X509_EXTENSION_get_data(extension);
93 + p = extension_data->data;
94 + length = extension_data->length;
96 names = (GENERAL_NAMES*)(ASN1_item_d2i(NULL, &p, length,
97 ASN1_ITEM_ptr(method->it)));
98 @@ -1416,6 +1425,8 @@ PHP_FUNCTION(openssl_x509_parse)
101 X509_EXTENSION *extension;
102 + X509_NAME *subject_name;
107 @@ -1430,12 +1441,12 @@ PHP_FUNCTION(openssl_x509_parse)
109 array_init(return_value);
112 - add_assoc_string(return_value, "name", cert->name, 1);
114 -/* add_assoc_bool(return_value, "valid", cert->valid); */
115 + subject_name = X509_get_subject_name(cert);
116 + cert_name = X509_NAME_oneline(subject_name, NULL, 0);
117 + add_assoc_string(return_value, "name", cert_name, 1);
118 + OPENSSL_free(cert_name);
120 - add_assoc_name_entry(return_value, "subject", X509_get_subject_name(cert), useshortnames TSRMLS_CC);
121 + add_assoc_name_entry(return_value, "subject", subject_name, useshortnames TSRMLS_CC);
122 /* hash as used in CA directories to lookup cert by subject name */
125 @@ -2921,13 +2932,20 @@ static int php_openssl_is_private_key(EV
127 assert(pkey != NULL);
129 - switch (pkey->type) {
130 + switch (EVP_PKEY_id(pkey)) {
134 - assert(pkey->pkey.rsa != NULL);
135 - if (pkey->pkey.rsa != NULL && (NULL == pkey->pkey.rsa->p || NULL == pkey->pkey.rsa->q)) {
138 + RSA *rsa = EVP_PKEY_get0_RSA(pkey);
140 + const BIGNUM *p, *q;
142 + RSA_get0_factors(rsa, &p, &q);
143 + if (p == NULL || q == NULL) {
150 @@ -2937,19 +2955,41 @@ static int php_openssl_is_private_key(EV
154 - assert(pkey->pkey.dsa != NULL);
156 + DSA *dsa = EVP_PKEY_get0_DSA(pkey);
158 + const BIGNUM *p, *q, *g, *pub_key, *priv_key;
160 + DSA_get0_pqg(dsa, &p, &q, &g);
161 + if (p == NULL || q == NULL) {
165 - if (NULL == pkey->pkey.dsa->p || NULL == pkey->pkey.dsa->q || NULL == pkey->pkey.dsa->priv_key){
167 + DSA_get0_key(dsa, &pub_key, &priv_key);
168 + if (priv_key == NULL) {
177 - assert(pkey->pkey.dh != NULL);
179 + DH *dh = EVP_PKEY_get0_DH(pkey);
181 + const BIGNUM *p, *q, *g, *pub_key, *priv_key;
183 + DH_get0_pqg(dh, &p, &q, &g);
188 - if (NULL == pkey->pkey.dh->p || NULL == pkey->pkey.dh->priv_key) {
190 + DH_get0_key(dh, &pub_key, &priv_key);
191 + if (priv_key == NULL) {
198 @@ -2961,6 +3001,31 @@ static int php_openssl_is_private_key(EV
202 +#define OPENSSL_GET_BN(_array, _bn, _name) do { \
203 + if (_bn != NULL) { \
204 + int len = BN_num_bytes(_bn); \
205 + char *str = emalloc(len + 1); \
206 + BN_bn2bin(_bn, (unsigned char*)str); \
208 + add_assoc_stringl(_array, #_name, str, len, 0); \
212 +#define OPENSSL_PKEY_GET_BN(_type, _name) OPENSSL_GET_BN(_type, _name, _name)
214 +#define OPENSSL_PKEY_SET_BN(_data, _name) do { \
216 + if (zend_hash_find(Z_ARRVAL_PP(_data), #_name, sizeof(#_name), (void**)bn) == SUCCESS && \
217 + Z_TYPE_PP(bn) == IS_STRING) { \
218 + _name = BN_bin2bn( \
219 + (unsigned char*)Z_STRVAL_PP(bn), \
220 + (int)Z_STRLEN_PP(bn), NULL); \
227 #define OPENSSL_PKEY_GET_BN(_type, _name) do { \
228 if (pkey->pkey._type->_name != NULL) { \
229 int len = BN_num_bytes(pkey->pkey._type->_name); \
230 @@ -2980,7 +3045,144 @@ static int php_openssl_is_private_key(EV
231 Z_STRLEN_PP(bn), NULL); \
236 +/* {{{ php_openssl_pkey_init_rsa */
237 +zend_bool php_openssl_pkey_init_and_assign_rsa(EVP_PKEY *pkey, RSA *rsa, zval **data)
239 + BIGNUM *n, *e, *d, *p, *q, *dmp1, *dmq1, *iqmp;
241 + OPENSSL_PKEY_SET_BN(data, n);
242 + OPENSSL_PKEY_SET_BN(data, e);
243 + OPENSSL_PKEY_SET_BN(data, d);
244 + if (!n || !d || !RSA_set0_key(rsa, n, e, d)) {
248 + OPENSSL_PKEY_SET_BN(data, p);
249 + OPENSSL_PKEY_SET_BN(data, q);
250 + if ((p || q) && !RSA_set0_factors(rsa, p, q)) {
254 + OPENSSL_PKEY_SET_BN(data, dmp1);
255 + OPENSSL_PKEY_SET_BN(data, dmq1);
256 + OPENSSL_PKEY_SET_BN(data, iqmp);
257 + if ((dmp1 || dmq1 || iqmp) && !RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp)) {
261 + if (!EVP_PKEY_assign_RSA(pkey, rsa)) {
268 +/* {{{ php_openssl_pkey_init_dsa */
269 +zend_bool php_openssl_pkey_init_dsa(DSA *dsa, zval **data)
271 + BIGNUM *p, *q, *g, *priv_key, *pub_key;
272 + const BIGNUM *priv_key_const, *pub_key_const;
274 + OPENSSL_PKEY_SET_BN(data, p);
275 + OPENSSL_PKEY_SET_BN(data, q);
276 + OPENSSL_PKEY_SET_BN(data, g);
277 + if (!p || !q || !g || !DSA_set0_pqg(dsa, p, q, g)) {
280 + OPENSSL_PKEY_SET_BN(data, pub_key);
281 + OPENSSL_PKEY_SET_BN(data, priv_key);
283 + return DSA_set0_key(dsa, pub_key, priv_key);
287 + PHP_OPENSSL_RAND_ADD_TIME();
288 + if (!DSA_generate_key(dsa)) {
291 + /* if BN_mod_exp return -1, then DSA_generate_key succeed for failed key
292 + * so we need to double check that public key is created */
293 + DSA_get0_key(dsa, &pub_key_const, &priv_key_const);
294 + if (!pub_key_const || BN_is_zero(pub_key_const)) {
302 +/* {{{ php_openssl_dh_pub_from_priv */
303 +static BIGNUM *php_openssl_dh_pub_from_priv(BIGNUM *priv_key, BIGNUM *g, BIGNUM *p)
305 + BIGNUM *pub_key, *priv_key_const_time;
308 + pub_key = BN_new();
309 + if (pub_key == NULL) {
313 + priv_key_const_time = BN_new();
314 + if (priv_key_const_time == NULL) {
318 + ctx = BN_CTX_new();
321 + BN_free(priv_key_const_time);
325 + BN_with_flags(priv_key_const_time, priv_key, BN_FLG_CONSTTIME);
327 + if (!BN_mod_exp_mont(pub_key, g, priv_key_const_time, p, ctx, NULL)) {
332 + BN_free(priv_key_const_time);
339 +/* {{{ php_openssl_pkey_init_dh */
340 +zend_bool php_openssl_pkey_init_dh(DH *dh, zval **data)
342 + BIGNUM *p, *q, *g, *priv_key, *pub_key;
344 + OPENSSL_PKEY_SET_BN(data, p);
345 + OPENSSL_PKEY_SET_BN(data, q);
346 + OPENSSL_PKEY_SET_BN(data, g);
347 + if (!p || !g || !DH_set0_pqg(dh, p, q, g)) {
350 + OPENSSL_PKEY_SET_BN(data, priv_key);
351 + OPENSSL_PKEY_SET_BN(data, pub_key);
353 + return DH_set0_key(dh, pub_key, priv_key);
356 + pub_key = php_openssl_dh_pub_from_priv(priv_key, g, p);
357 + if (pub_key == NULL) {
360 + return DH_set0_key(dh, pub_key, priv_key);
364 + PHP_OPENSSL_RAND_ADD_TIME();
365 + if (!DH_generate_key(dh)) {
373 /* {{{ proto resource openssl_pkey_new([array configargs])
374 Generates a new private key */
375 @@ -3004,18 +3206,8 @@ PHP_FUNCTION(openssl_pkey_new)
377 RSA *rsa = RSA_new();
379 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, n);
380 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, e);
381 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, d);
382 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, p);
383 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, q);
384 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, dmp1);
385 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, dmq1);
386 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, iqmp);
387 - if (rsa->n && rsa->d) {
388 - if (EVP_PKEY_assign_RSA(pkey, rsa)) {
389 - RETURN_RESOURCE(zend_list_insert(pkey, le_key));
391 + if (php_openssl_pkey_init_and_assign_rsa(pkey, rsa, data)) {
392 + RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
396 @@ -3028,15 +3220,7 @@ PHP_FUNCTION(openssl_pkey_new)
398 DSA *dsa = DSA_new();
400 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, p);
401 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, q);
402 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, g);
403 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, priv_key);
404 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, pub_key);
405 - if (dsa->p && dsa->q && dsa->g) {
406 - if (!dsa->priv_key && !dsa->pub_key) {
407 - DSA_generate_key(dsa);
409 + if (php_openssl_pkey_init_dsa(dsa, data)) {
410 if (EVP_PKEY_assign_DSA(pkey, dsa)) {
411 RETURN_RESOURCE(zend_list_insert(pkey, le_key));
413 @@ -3052,14 +3236,7 @@ PHP_FUNCTION(openssl_pkey_new)
417 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, p);
418 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, g);
419 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, priv_key);
420 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, pub_key);
421 - if (dh->p && dh->g) {
422 - if (!dh->pub_key) {
423 - DH_generate_key(dh);
425 + if (php_openssl_pkey_init_dh(dh, data)) {
426 if (EVP_PKEY_assign_DH(pkey, dh)) {
427 RETURN_RESOURCE(zend_list_insert(pkey, le_key));
429 @@ -3289,63 +3466,82 @@ PHP_FUNCTION(openssl_pkey_get_details)
430 /*TODO: Use the real values once the openssl constants are used
431 * See the enum at the top of this file
433 - switch (EVP_PKEY_type(pkey->type)) {
434 + switch (EVP_PKEY_base_id(pkey)) {
437 - ktype = OPENSSL_KEYTYPE_RSA;
439 - if (pkey->pkey.rsa != NULL) {
442 - ALLOC_INIT_ZVAL(rsa);
444 - OPENSSL_PKEY_GET_BN(rsa, n);
445 - OPENSSL_PKEY_GET_BN(rsa, e);
446 - OPENSSL_PKEY_GET_BN(rsa, d);
447 - OPENSSL_PKEY_GET_BN(rsa, p);
448 - OPENSSL_PKEY_GET_BN(rsa, q);
449 - OPENSSL_PKEY_GET_BN(rsa, dmp1);
450 - OPENSSL_PKEY_GET_BN(rsa, dmq1);
451 - OPENSSL_PKEY_GET_BN(rsa, iqmp);
452 - add_assoc_zval(return_value, "rsa", rsa);
454 + RSA *rsa = EVP_PKEY_get0_RSA(pkey);
455 + ktype = OPENSSL_KEYTYPE_RSA;
459 + const BIGNUM *n, *e, *d, *p, *q, *dmp1, *dmq1, *iqmp;
461 + RSA_get0_key(rsa, &n, &e, &d);
462 + RSA_get0_factors(rsa, &p, &q);
463 + RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
465 + ALLOC_INIT_ZVAL(z_rsa);
467 + OPENSSL_PKEY_GET_BN(z_rsa, n);
468 + OPENSSL_PKEY_GET_BN(z_rsa, e);
469 + OPENSSL_PKEY_GET_BN(z_rsa, d);
470 + OPENSSL_PKEY_GET_BN(z_rsa, p);
471 + OPENSSL_PKEY_GET_BN(z_rsa, q);
472 + OPENSSL_PKEY_GET_BN(z_rsa, dmp1);
473 + OPENSSL_PKEY_GET_BN(z_rsa, dmq1);
474 + OPENSSL_PKEY_GET_BN(z_rsa, iqmp);
475 + add_assoc_zval(return_value, "rsa", z_rsa);
484 - ktype = OPENSSL_KEYTYPE_DSA;
486 - if (pkey->pkey.dsa != NULL) {
489 - ALLOC_INIT_ZVAL(dsa);
491 - OPENSSL_PKEY_GET_BN(dsa, p);
492 - OPENSSL_PKEY_GET_BN(dsa, q);
493 - OPENSSL_PKEY_GET_BN(dsa, g);
494 - OPENSSL_PKEY_GET_BN(dsa, priv_key);
495 - OPENSSL_PKEY_GET_BN(dsa, pub_key);
496 - add_assoc_zval(return_value, "dsa", dsa);
498 + DSA *dsa = EVP_PKEY_get0_DSA(pkey);
499 + ktype = OPENSSL_KEYTYPE_DSA;
503 + const BIGNUM *p, *q, *g, *priv_key, *pub_key;
505 + DSA_get0_pqg(dsa, &p, &q, &g);
506 + DSA_get0_key(dsa, &pub_key, &priv_key);
508 + ALLOC_INIT_ZVAL(z_dsa);
510 + OPENSSL_PKEY_GET_BN(z_dsa, p);
511 + OPENSSL_PKEY_GET_BN(z_dsa, q);
512 + OPENSSL_PKEY_GET_BN(z_dsa, g);
513 + OPENSSL_PKEY_GET_BN(z_dsa, priv_key);
514 + OPENSSL_PKEY_GET_BN(z_dsa, pub_key);
515 + add_assoc_zval(return_value, "dsa", z_dsa);
521 - ktype = OPENSSL_KEYTYPE_DH;
523 - if (pkey->pkey.dh != NULL) {
526 - ALLOC_INIT_ZVAL(dh);
528 - OPENSSL_PKEY_GET_BN(dh, p);
529 - OPENSSL_PKEY_GET_BN(dh, g);
530 - OPENSSL_PKEY_GET_BN(dh, priv_key);
531 - OPENSSL_PKEY_GET_BN(dh, pub_key);
532 - add_assoc_zval(return_value, "dh", dh);
534 + DH *dh = EVP_PKEY_get0_DH(pkey);
535 + ktype = OPENSSL_KEYTYPE_DH;
539 + const BIGNUM *p, *q, *g, *priv_key, *pub_key;
541 + DH_get0_pqg(dh, &p, &q, &g);
542 + DH_get0_key(dh, &pub_key, &priv_key);
544 + ALLOC_INIT_ZVAL(z_dh);
546 + OPENSSL_PKEY_GET_BN(z_dh, p);
547 + OPENSSL_PKEY_GET_BN(z_dh, g);
548 + OPENSSL_PKEY_GET_BN(z_dh, priv_key);
549 + OPENSSL_PKEY_GET_BN(z_dh, pub_key);
550 + add_assoc_zval(return_value, "dh", z_dh);
557 @@ -3844,13 +4040,13 @@ PHP_FUNCTION(openssl_private_encrypt)
558 cryptedlen = EVP_PKEY_size(pkey);
559 cryptedbuf = emalloc(cryptedlen + 1);
561 - switch (pkey->type) {
562 + switch (EVP_PKEY_id(pkey)) {
565 successful = (RSA_private_encrypt(data_len,
566 (unsigned char *)data,
569 + EVP_PKEY_get0_RSA(pkey),
570 padding) == cryptedlen);
573 @@ -3902,13 +4098,13 @@ PHP_FUNCTION(openssl_private_decrypt)
574 cryptedlen = EVP_PKEY_size(pkey);
575 crypttemp = emalloc(cryptedlen + 1);
577 - switch (pkey->type) {
578 + switch (EVP_PKEY_id(pkey)) {
581 cryptedlen = RSA_private_decrypt(data_len,
582 (unsigned char *)data,
585 + EVP_PKEY_get0_RSA(pkey),
587 if (cryptedlen != -1) {
588 cryptedbuf = emalloc(cryptedlen + 1);
589 @@ -3967,13 +4163,13 @@ PHP_FUNCTION(openssl_public_encrypt)
590 cryptedlen = EVP_PKEY_size(pkey);
591 cryptedbuf = emalloc(cryptedlen + 1);
593 - switch (pkey->type) {
594 + switch (EVP_PKEY_id(pkey)) {
597 successful = (RSA_public_encrypt(data_len,
598 (unsigned char *)data,
601 + EVP_PKEY_get0_RSA(pkey),
602 padding) == cryptedlen);
605 @@ -4026,13 +4222,13 @@ PHP_FUNCTION(openssl_public_decrypt)
606 cryptedlen = EVP_PKEY_size(pkey);
607 crypttemp = emalloc(cryptedlen + 1);
609 - switch (pkey->type) {
610 + switch (EVP_PKEY_id(pkey)) {
613 cryptedlen = RSA_public_decrypt(data_len,
614 (unsigned char *)data,
617 + EVP_PKEY_get0_RSA(pkey),
619 if (cryptedlen != -1) {
620 cryptedbuf = emalloc(cryptedlen + 1);
621 @@ -4096,7 +4292,7 @@ PHP_FUNCTION(openssl_sign)
622 long keyresource = -1;
626 + EVP_MD_CTX *md_ctx;
628 long signature_algo = OPENSSL_ALGO_SHA1;
629 const EVP_MD *mdtype;
630 @@ -4129,9 +4325,11 @@ PHP_FUNCTION(openssl_sign)
631 siglen = EVP_PKEY_size(pkey);
632 sigbuf = emalloc(siglen + 1);
634 - EVP_SignInit(&md_ctx, mdtype);
635 - EVP_SignUpdate(&md_ctx, data, data_len);
636 - if (EVP_SignFinal (&md_ctx, sigbuf,(unsigned int *)&siglen, pkey)) {
637 + md_ctx = EVP_MD_CTX_create();
638 + if (md_ctx != NULL &&
639 + EVP_SignInit(md_ctx, mdtype) &&
640 + EVP_SignUpdate(md_ctx, data, data_len) &&
641 + EVP_SignFinal (md_ctx, sigbuf,(unsigned int *)&siglen, pkey)) {
642 zval_dtor(signature);
643 sigbuf[siglen] = '\0';
644 ZVAL_STRINGL(signature, (char *)sigbuf, siglen, 0);
645 @@ -4140,7 +4338,7 @@ PHP_FUNCTION(openssl_sign)
649 - EVP_MD_CTX_cleanup(&md_ctx);
650 + EVP_MD_CTX_destroy(md_ctx);
651 if (keyresource == -1) {
654 @@ -4154,7 +4352,7 @@ PHP_FUNCTION(openssl_verify)
659 + EVP_MD_CTX *md_ctx;
660 const EVP_MD *mdtype;
661 long keyresource = -1;
662 char * data; int data_len;
663 @@ -4188,10 +4386,13 @@ PHP_FUNCTION(openssl_verify)
667 - EVP_VerifyInit (&md_ctx, mdtype);
668 - EVP_VerifyUpdate (&md_ctx, data, data_len);
669 - err = EVP_VerifyFinal (&md_ctx, (unsigned char *)signature, signature_len, pkey);
670 - EVP_MD_CTX_cleanup(&md_ctx);
671 + md_ctx = EVP_MD_CTX_create();
672 + if (md_ctx != NULL) {
673 + EVP_VerifyInit (md_ctx, mdtype);
674 + EVP_VerifyUpdate (md_ctx, data, data_len);
675 + err = EVP_VerifyFinal (md_ctx, (unsigned char *)signature, signature_len, pkey);
677 + EVP_MD_CTX_destroy(md_ctx);
679 if (keyresource == -1) {
681 @@ -4215,7 +4416,7 @@ PHP_FUNCTION(openssl_seal)
684 const EVP_CIPHER *cipher;
685 - EVP_CIPHER_CTX ctx;
686 + EVP_CIPHER_CTX *ctx;
688 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szza/|s", &data, &data_len, &sealdata, &ekeys, &pubkeys, &method, &method_len) == FAILURE) {
690 @@ -4261,7 +4462,9 @@ PHP_FUNCTION(openssl_seal)
694 - if (!EVP_EncryptInit(&ctx,cipher,NULL,NULL)) {
695 + ctx = EVP_CIPHER_CTX_new();
696 + if (ctx == NULL || !EVP_EncryptInit(ctx,cipher,NULL,NULL)) {
697 + EVP_CIPHER_CTX_free(ctx);
701 @@ -4272,15 +4475,16 @@ PHP_FUNCTION(openssl_seal)
702 iv = ivlen ? emalloc(ivlen + 1) : NULL;
704 /* allocate one byte extra to make room for \0 */
705 - buf = emalloc(data_len + EVP_CIPHER_CTX_block_size(&ctx));
706 + buf = emalloc(data_len + EVP_CIPHER_CTX_block_size(ctx));
708 - if (!EVP_SealInit(&ctx, cipher, eks, eksl, NULL, pkeys, nkeys) || !EVP_SealUpdate(&ctx, buf, &len1, (unsigned char *)data, data_len)) {
709 + if (!EVP_SealInit(ctx, cipher, eks, eksl, NULL, pkeys, nkeys) || !EVP_SealUpdate(ctx, buf, &len1, (unsigned char *)data, data_len)) {
712 + EVP_CIPHER_CTX_free(ctx);
716 - EVP_SealFinal(&ctx, buf + len1, &len2);
717 + EVP_SealFinal(ctx, buf + len1, &len2);
719 if (len1 + len2 > 0) {
721 @@ -4309,6 +4513,7 @@ PHP_FUNCTION(openssl_seal)
724 RETVAL_LONG(len1 + len2);
725 + EVP_CIPHER_CTX_free(ctx);
728 for (i=0; i<nkeys; i++) {
729 @@ -4335,7 +4540,7 @@ PHP_FUNCTION(openssl_open)
732 long keyresource = -1;
733 - EVP_CIPHER_CTX ctx;
734 + EVP_CIPHER_CTX *ctx;
735 char * data; int data_len;
736 char * ekey; int ekey_len;
738 @@ -4364,28 +4569,22 @@ PHP_FUNCTION(openssl_open)
740 buf = emalloc(data_len + 1);
742 - if (EVP_OpenInit(&ctx, cipher, (unsigned char *)ekey, ekey_len, NULL, pkey) && EVP_OpenUpdate(&ctx, buf, &len1, (unsigned char *)data, data_len)) {
743 - if (!EVP_OpenFinal(&ctx, buf + len1, &len2) || (len1 + len2 == 0)) {
745 - if (keyresource == -1) {
746 - EVP_PKEY_free(pkey);
750 + ctx = EVP_CIPHER_CTX_new();
751 + if (ctx != NULL && EVP_OpenInit(ctx, cipher, (unsigned char *)ekey, ekey_len, NULL, pkey) &&
752 + EVP_OpenUpdate(ctx, buf, &len1, (unsigned char *)data, data_len) &&
753 + EVP_OpenFinal(ctx, buf + len1, &len2) && (len1 + len2 > 0)) {
754 + zval_dtor(opendata);
755 + buf[len1 + len2] = '\0';
756 + ZVAL_STRINGL(opendata, erealloc(buf, len1 + len2 + 1), len1 + len2, 0);
760 - if (keyresource == -1) {
761 - EVP_PKEY_free(pkey);
767 if (keyresource == -1) {
770 - zval_dtor(opendata);
771 - buf[len1 + len2] = '\0';
772 - ZVAL_STRINGL(opendata, erealloc(buf, len1 + len2 + 1), len1 + len2, 0);
774 + EVP_CIPHER_CTX_free(ctx);
778 @@ -4690,7 +4889,7 @@ PHP_FUNCTION(openssl_digest)
780 int data_len, method_len;
781 const EVP_MD *mdtype;
783 + EVP_MD_CTX *md_ctx;
785 unsigned char *sigbuf;
787 @@ -4706,9 +4905,11 @@ PHP_FUNCTION(openssl_digest)
788 siglen = EVP_MD_size(mdtype);
789 sigbuf = emalloc(siglen + 1);
791 - EVP_DigestInit(&md_ctx, mdtype);
792 - EVP_DigestUpdate(&md_ctx, (unsigned char *)data, data_len);
793 - if (EVP_DigestFinal (&md_ctx, (unsigned char *)sigbuf, (unsigned int *)&siglen)) {
794 + md_ctx = EVP_MD_CTX_create();
795 + if (md_ctx != NULL &&
796 + EVP_DigestInit(md_ctx, mdtype) &&
797 + EVP_DigestUpdate(md_ctx, (unsigned char *)data, data_len) &&
798 + EVP_DigestFinal (md_ctx, (unsigned char *)sigbuf, (unsigned int *)&siglen)) {
800 sigbuf[siglen] = '\0';
801 RETVAL_STRINGL((char *)sigbuf, siglen, 0);
802 @@ -4724,6 +4925,7 @@ PHP_FUNCTION(openssl_digest)
806 + EVP_MD_CTX_destroy(md_ctx);
810 @@ -4769,7 +4971,7 @@ PHP_FUNCTION(openssl_encrypt)
811 char *data, *method, *password, *iv = "";
812 int data_len, method_len, password_len, iv_len = 0, max_iv_len;
813 const EVP_CIPHER *cipher_type;
814 - EVP_CIPHER_CTX cipher_ctx;
815 + EVP_CIPHER_CTX *cipher_ctx;
816 int i = 0, outlen, keylen;
817 unsigned char *outbuf, *key;
819 @@ -4783,6 +4985,12 @@ PHP_FUNCTION(openssl_encrypt)
823 + cipher_ctx = EVP_CIPHER_CTX_new();
825 + php_error_docref(NULL, E_WARNING, "Failed to create cipher context");
829 keylen = EVP_CIPHER_key_length(cipher_type);
830 if (keylen > password_len) {
831 key = emalloc(keylen);
832 @@ -4801,16 +5009,16 @@ PHP_FUNCTION(openssl_encrypt)
833 outlen = data_len + EVP_CIPHER_block_size(cipher_type);
834 outbuf = emalloc(outlen + 1);
836 - EVP_EncryptInit(&cipher_ctx, cipher_type, NULL, NULL);
837 + EVP_EncryptInit(cipher_ctx, cipher_type, NULL, NULL);
838 if (password_len > keylen) {
839 - EVP_CIPHER_CTX_set_key_length(&cipher_ctx, password_len);
840 + EVP_CIPHER_CTX_set_key_length(cipher_ctx, password_len);
842 - EVP_EncryptInit_ex(&cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
843 + EVP_EncryptInit_ex(cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
845 - EVP_EncryptUpdate(&cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
846 + EVP_EncryptUpdate(cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
849 - if (EVP_EncryptFinal(&cipher_ctx, (unsigned char *)outbuf + i, &i)) {
850 + if (EVP_EncryptFinal(cipher_ctx, (unsigned char *)outbuf + i, &i)) {
853 outbuf[outlen] = '\0';
854 @@ -4833,7 +5041,7 @@ PHP_FUNCTION(openssl_encrypt)
858 - EVP_CIPHER_CTX_cleanup(&cipher_ctx);
859 + EVP_CIPHER_CTX_free(cipher_ctx);
863 @@ -4845,7 +5053,7 @@ PHP_FUNCTION(openssl_decrypt)
864 char *data, *method, *password, *iv = "";
865 int data_len, method_len, password_len, iv_len = 0;
866 const EVP_CIPHER *cipher_type;
867 - EVP_CIPHER_CTX cipher_ctx;
868 + EVP_CIPHER_CTX *cipher_ctx;
869 int i, outlen, keylen;
870 unsigned char *outbuf, *key;
872 @@ -4867,10 +5075,17 @@ PHP_FUNCTION(openssl_decrypt)
876 + cipher_ctx = EVP_CIPHER_CTX_new();
878 + php_error_docref(NULL, E_WARNING, "Failed to create cipher context");
883 base64_str = (char*)php_base64_decode((unsigned char*)data, data_len, &base64_str_len);
885 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to base64 decode the input");
886 + EVP_CIPHER_CTX_free(cipher_ctx);
889 data_len = base64_str_len;
890 @@ -4891,14 +5106,14 @@ PHP_FUNCTION(openssl_decrypt)
891 outlen = data_len + EVP_CIPHER_block_size(cipher_type);
892 outbuf = emalloc(outlen + 1);
894 - EVP_DecryptInit(&cipher_ctx, cipher_type, NULL, NULL);
895 + EVP_DecryptInit(cipher_ctx, cipher_type, NULL, NULL);
896 if (password_len > keylen) {
897 - EVP_CIPHER_CTX_set_key_length(&cipher_ctx, password_len);
898 + EVP_CIPHER_CTX_set_key_length(cipher_ctx, password_len);
900 - EVP_DecryptInit_ex(&cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
901 - EVP_DecryptUpdate(&cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
902 + EVP_DecryptInit_ex(cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
903 + EVP_DecryptUpdate(cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
905 - if (EVP_DecryptFinal(&cipher_ctx, (unsigned char *)outbuf + i, &i)) {
906 + if (EVP_DecryptFinal(cipher_ctx, (unsigned char *)outbuf + i, &i)) {
908 outbuf[outlen] = '\0';
909 RETVAL_STRINGL((char *)outbuf, outlen, 0);
910 @@ -4915,7 +5130,7 @@ PHP_FUNCTION(openssl_decrypt)
914 - EVP_CIPHER_CTX_cleanup(&cipher_ctx);
915 + EVP_CIPHER_CTX_free(cipher_ctx);
919 @@ -4953,6 +5168,7 @@ PHP_FUNCTION(openssl_dh_compute_key)
927 @@ -4961,15 +5177,18 @@ PHP_FUNCTION(openssl_dh_compute_key)
928 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sr", &pub_str, &pub_len, &key) == FAILURE) {
931 - ZEND_FETCH_RESOURCE(pkey, EVP_PKEY *, &key, -1, "OpenSSL key", le_key);
932 - if (!pkey || EVP_PKEY_type(pkey->type) != EVP_PKEY_DH || !pkey->pkey.dh) {
933 + if (EVP_PKEY_base_id(pkey) != EVP_PKEY_DH) {
936 + dh = EVP_PKEY_get0_DH(pkey);
941 pub = BN_bin2bn((unsigned char*)pub_str, pub_len, NULL);
943 - data = emalloc(DH_size(pkey->pkey.dh) + 1);
944 - len = DH_compute_key((unsigned char*)data, pub, pkey->pkey.dh);
945 + data = emalloc(DH_size(dh) + 1);
946 + len = DH_compute_key((unsigned char*)data, pub, dh);
950 diff -ruNp php-5.3.29.orig/ext/openssl/xp_ssl.c php-5.3.29/ext/openssl/xp_ssl.c
951 --- php-5.3.29.orig/ext/openssl/xp_ssl.c 2014-08-13 21:22:50.000000000 +0200
952 +++ php-5.3.29/ext/openssl/xp_ssl.c 2019-02-05 23:42:46.543242570 +0100
953 @@ -338,9 +338,14 @@ static inline int php_openssl_setup_cryp
956 case STREAM_CRYPTO_METHOD_SSLv3_CLIENT:
957 +#ifdef OPENSSL_NO_SSL3
958 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "SSLv3 support is not compiled into the OpenSSL library PHP is linked against");
961 sslsock->is_client = 1;
962 method = SSLv3_client_method();
965 case STREAM_CRYPTO_METHOD_TLS_CLIENT:
966 sslsock->is_client = 1;
967 method = TLSv1_client_method();
968 @@ -350,9 +355,14 @@ static inline int php_openssl_setup_cryp
969 method = SSLv23_server_method();
971 case STREAM_CRYPTO_METHOD_SSLv3_SERVER:
972 +#ifdef OPENSSL_NO_SSL3
973 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "SSLv3 support is not compiled into the OpenSSL library PHP is linked against");
976 sslsock->is_client = 0;
977 method = SSLv3_server_method();
980 case STREAM_CRYPTO_METHOD_SSLv2_SERVER:
981 #ifdef OPENSSL_NO_SSL2
982 php_error_docref(NULL TSRMLS_CC, E_WARNING, "SSLv2 support is not compiled into the OpenSSL library PHP is linked against");