1 diff -urNp -x '*.orig' php-5.6.40.org/ext/openssl/openssl.c php-5.6.40/ext/openssl/openssl.c
2 --- php-5.6.40.org/ext/openssl/openssl.c 2021-08-23 23:18:57.552489303 +0200
3 +++ php-5.6.40/ext/openssl/openssl.c 2021-08-23 23:18:58.475821680 +0200
4 @@ -531,6 +531,14 @@ zend_module_entry openssl_module_entry =
5 ZEND_GET_MODULE(openssl)
8 +/* {{{ OpenSSL compatibility functions and macros */
9 +#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined (LIBRESSL_VERSION_NUMBER)
10 +#define EVP_PKEY_get0_RSA(_pkey) _pkey->pkey.rsa
11 +#define EVP_PKEY_get0_DH(_pkey) _pkey->pkey.dh
12 +#define EVP_PKEY_get0_DSA(_pkey) _pkey->pkey.dsa
13 +#define EVP_PKEY_get0_EC_KEY(_pkey) _pkey->pkey.ec
19 @@ -1053,9 +1061,11 @@ static EVP_MD * php_openssl_get_evp_md_f
20 mdtype = (EVP_MD *) EVP_md2();
23 +#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined (LIBRESSL_VERSION_NUMBER)
24 case OPENSSL_ALGO_DSS1:
25 mdtype = (EVP_MD *) EVP_dss1();
28 #if OPENSSL_VERSION_NUMBER >= 0x0090708fL
29 case OPENSSL_ALGO_SHA224:
30 mdtype = (EVP_MD *) EVP_sha224();
31 @@ -1893,6 +1903,7 @@ static int openssl_x509v3_subjectAltName
34 const X509V3_EXT_METHOD *method = NULL;
35 + ASN1_OCTET_STRING *extension_data;
37 const unsigned char *p;
39 @@ -1901,8 +1912,9 @@ static int openssl_x509v3_subjectAltName
43 - p = extension->value->data;
44 - length = extension->value->length;
45 + extension_data = X509_EXTENSION_get_data(extension);
46 + p = extension_data->data;
47 + length = extension_data->length;
49 names = (GENERAL_NAMES*)(ASN1_item_d2i(NULL, &p, length,
50 ASN1_ITEM_ptr(method->it)));
51 @@ -1965,6 +1977,8 @@ PHP_FUNCTION(openssl_x509_parse)
54 X509_EXTENSION *extension;
55 + X509_NAME *subject_name;
60 @@ -1979,12 +1993,12 @@ PHP_FUNCTION(openssl_x509_parse)
62 array_init(return_value);
65 - add_assoc_string(return_value, "name", cert->name, 1);
67 -/* add_assoc_bool(return_value, "valid", cert->valid); */
68 + subject_name = X509_get_subject_name(cert);
69 + cert_name = X509_NAME_oneline(subject_name, NULL, 0);
70 + add_assoc_string(return_value, "name", cert_name, 1);
71 + OPENSSL_free(cert_name);
73 - add_assoc_name_entry(return_value, "subject", X509_get_subject_name(cert), useshortnames TSRMLS_CC);
74 + add_assoc_name_entry(return_value, "subject", subject_name, useshortnames TSRMLS_CC);
75 /* hash as used in CA directories to lookup cert by subject name */
78 @@ -2008,7 +2022,7 @@ PHP_FUNCTION(openssl_x509_parse)
79 add_assoc_string(return_value, "alias", tmpstr, 1);
82 - sig_nid = OBJ_obj2nid((cert)->sig_alg->algorithm);
83 + sig_nid = X509_get_signature_nid(cert);
84 add_assoc_string(return_value, "signatureTypeSN", (char*)OBJ_nid2sn(sig_nid), 1);
85 add_assoc_string(return_value, "signatureTypeLN", (char*)OBJ_nid2ln(sig_nid), 1);
86 add_assoc_long(return_value, "signatureTypeNID", sig_nid);
87 @@ -3482,13 +3496,20 @@ static int php_openssl_is_private_key(EV
91 - switch (pkey->type) {
92 + switch (EVP_PKEY_id(pkey)) {
96 - assert(pkey->pkey.rsa != NULL);
97 - if (pkey->pkey.rsa != NULL && (NULL == pkey->pkey.rsa->p || NULL == pkey->pkey.rsa->q)) {
100 + RSA *rsa = EVP_PKEY_get0_RSA(pkey);
102 + const BIGNUM *p, *q;
104 + RSA_get0_factors(rsa, &p, &q);
105 + if (p == NULL || q == NULL) {
112 @@ -3498,28 +3519,51 @@ static int php_openssl_is_private_key(EV
116 - assert(pkey->pkey.dsa != NULL);
118 + DSA *dsa = EVP_PKEY_get0_DSA(pkey);
120 + const BIGNUM *p, *q, *g, *pub_key, *priv_key;
122 + DSA_get0_pqg(dsa, &p, &q, &g);
123 + if (p == NULL || q == NULL) {
127 - if (NULL == pkey->pkey.dsa->p || NULL == pkey->pkey.dsa->q || NULL == pkey->pkey.dsa->priv_key){
129 + DSA_get0_key(dsa, &pub_key, &priv_key);
130 + if (priv_key == NULL) {
139 - assert(pkey->pkey.dh != NULL);
141 + DH *dh = EVP_PKEY_get0_DH(pkey);
143 + const BIGNUM *p, *q, *g, *pub_key, *priv_key;
145 + DH_get0_pqg(dh, &p, &q, &g);
150 - if (NULL == pkey->pkey.dh->p || NULL == pkey->pkey.dh->priv_key) {
152 + DH_get0_key(dh, &pub_key, &priv_key);
153 + if (priv_key == NULL) {
160 #ifdef HAVE_EVP_PKEY_EC
162 - assert(pkey->pkey.ec != NULL);
164 - if ( NULL == EC_KEY_get0_private_key(pkey->pkey.ec)) {
167 + EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
168 + if (ec != NULL && NULL == EC_KEY_get0_private_key(ec)) {
174 @@ -3531,42 +3575,89 @@ static int php_openssl_is_private_key(EV
178 -#define OPENSSL_PKEY_GET_BN(_type, _name) do { \
179 - if (pkey->pkey._type->_name != NULL) { \
180 - int len = BN_num_bytes(pkey->pkey._type->_name); \
181 - char *str = emalloc(len + 1); \
182 - BN_bn2bin(pkey->pkey._type->_name, (unsigned char*)str); \
184 - add_assoc_stringl(_type, #_name, str, len, 0); \
188 -#define OPENSSL_PKEY_SET_BN(_ht, _type, _name) do { \
190 - if (zend_hash_find(_ht, #_name, sizeof(#_name), (void**)&bn) == SUCCESS && \
191 - Z_TYPE_PP(bn) == IS_STRING) { \
192 - _type->_name = BN_bin2bn( \
193 - (unsigned char*)Z_STRVAL_PP(bn), \
194 - Z_STRLEN_PP(bn), NULL); \
197 +#define OPENSSL_GET_BN(_array, _bn, _name) do { \
198 + if (_bn != NULL) { \
199 + int len = BN_num_bytes(_bn); \
200 + char *str = emalloc(len + 1); \
201 + BN_bn2bin(_bn, (unsigned char*)str); \
203 + add_assoc_stringl(&_array, #_name, str, len, 0); \
207 +#define OPENSSL_PKEY_GET_BN(_type, _name) OPENSSL_GET_BN(_type, _name, _name)
209 +#define OPENSSL_PKEY_SET_BN(_data, _name) do { \
211 + if (zend_hash_find(Z_ARRVAL_PP(_data), #_name, sizeof(#_name), (void**)bn) == SUCCESS && \
212 + Z_TYPE_PP(bn) == IS_STRING) { \
213 + _name = BN_bin2bn( \
214 + (unsigned char*)Z_STRVAL_PP(bn), \
215 + (int)Z_STRLEN_PP(bn), NULL); \
221 +/* {{{ php_openssl_pkey_init_rsa */
222 +zend_bool php_openssl_pkey_init_and_assign_rsa(EVP_PKEY *pkey, RSA *rsa, zval **data)
224 + BIGNUM *n, *e, *d, *p, *q, *dmp1, *dmq1, *iqmp;
226 + OPENSSL_PKEY_SET_BN(data, n);
227 + OPENSSL_PKEY_SET_BN(data, e);
228 + OPENSSL_PKEY_SET_BN(data, d);
229 + if (!n || !d || !RSA_set0_key(rsa, n, e, d)) {
233 + OPENSSL_PKEY_SET_BN(data, p);
234 + OPENSSL_PKEY_SET_BN(data, q);
235 + if ((p || q) && !RSA_set0_factors(rsa, p, q)) {
239 + OPENSSL_PKEY_SET_BN(data, dmp1);
240 + OPENSSL_PKEY_SET_BN(data, dmq1);
241 + OPENSSL_PKEY_SET_BN(data, iqmp);
242 + if ((dmp1 || dmq1 || iqmp) && !RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp)) {
246 + if (!EVP_PKEY_assign_RSA(pkey, rsa)) {
253 /* {{{ php_openssl_pkey_init_dsa */
254 -zend_bool php_openssl_pkey_init_dsa(DSA *dsa)
255 +zend_bool php_openssl_pkey_init_dsa(DSA *dsa, zval **data)
257 - if (!dsa->p || !dsa->q || !dsa->g) {
258 + BIGNUM *p, *q, *g, *priv_key, *pub_key;
259 + const BIGNUM *priv_key_const, *pub_key_const;
261 + OPENSSL_PKEY_SET_BN(data, p);
262 + OPENSSL_PKEY_SET_BN(data, q);
263 + OPENSSL_PKEY_SET_BN(data, g);
264 + if (!p || !q || !g || !DSA_set0_pqg(dsa, p, q, g)) {
267 - if (dsa->priv_key || dsa->pub_key) {
269 + OPENSSL_PKEY_SET_BN(data, pub_key);
270 + OPENSSL_PKEY_SET_BN(data, priv_key);
272 + return DSA_set0_key(dsa, pub_key, priv_key);
276 PHP_OPENSSL_RAND_ADD_TIME();
277 if (!DSA_generate_key(dsa)) {
280 /* if BN_mod_exp return -1, then DSA_generate_key succeed for failed key
281 * so we need to double check that public key is created */
282 - if (!dsa->pub_key || BN_is_zero(dsa->pub_key)) {
283 + DSA_get0_key(dsa, &pub_key_const, &priv_key_const);
284 + if (!pub_key_const || BN_is_zero(pub_key_const)) {
288 @@ -3574,15 +3665,68 @@ zend_bool php_openssl_pkey_init_dsa(DSA
292 +/* {{{ php_openssl_dh_pub_from_priv */
293 +static BIGNUM *php_openssl_dh_pub_from_priv(BIGNUM *priv_key, BIGNUM *g, BIGNUM *p)
295 + BIGNUM *pub_key, *priv_key_const_time;
298 + pub_key = BN_new();
299 + if (pub_key == NULL) {
303 + priv_key_const_time = BN_new();
304 + if (priv_key_const_time == NULL) {
308 + ctx = BN_CTX_new();
311 + BN_free(priv_key_const_time);
315 + BN_with_flags(priv_key_const_time, priv_key, BN_FLG_CONSTTIME);
317 + if (!BN_mod_exp_mont(pub_key, g, priv_key_const_time, p, ctx, NULL)) {
322 + BN_free(priv_key_const_time);
329 /* {{{ php_openssl_pkey_init_dh */
330 -zend_bool php_openssl_pkey_init_dh(DH *dh)
331 +zend_bool php_openssl_pkey_init_dh(DH *dh, zval **data)
333 - if (!dh->p || !dh->g) {
334 + BIGNUM *p, *q, *g, *priv_key, *pub_key;
336 + OPENSSL_PKEY_SET_BN(data, p);
337 + OPENSSL_PKEY_SET_BN(data, q);
338 + OPENSSL_PKEY_SET_BN(data, g);
339 + if (!p || !g || !DH_set0_pqg(dh, p, q, g)) {
344 + OPENSSL_PKEY_SET_BN(data, priv_key);
345 + OPENSSL_PKEY_SET_BN(data, pub_key);
347 + return DH_set0_key(dh, pub_key, priv_key);
350 + pub_key = php_openssl_dh_pub_from_priv(priv_key, g, p);
351 + if (pub_key == NULL) {
354 + return DH_set0_key(dh, pub_key, priv_key);
358 PHP_OPENSSL_RAND_ADD_TIME();
359 if (!DH_generate_key(dh)) {
361 @@ -3614,18 +3758,8 @@ PHP_FUNCTION(openssl_pkey_new)
363 RSA *rsa = RSA_new();
365 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, n);
366 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, e);
367 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, d);
368 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, p);
369 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, q);
370 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, dmp1);
371 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, dmq1);
372 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, iqmp);
373 - if (rsa->n && rsa->d) {
374 - if (EVP_PKEY_assign_RSA(pkey, rsa)) {
375 - RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
377 + if (php_openssl_pkey_init_and_assign_rsa(pkey, rsa, data)) {
378 + RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
382 @@ -3638,12 +3772,7 @@ PHP_FUNCTION(openssl_pkey_new)
384 DSA *dsa = DSA_new();
386 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, p);
387 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, q);
388 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, g);
389 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, priv_key);
390 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, pub_key);
391 - if (php_openssl_pkey_init_dsa(dsa)) {
392 + if (php_openssl_pkey_init_dsa(dsa, data)) {
393 if (EVP_PKEY_assign_DSA(pkey, dsa)) {
394 RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
396 @@ -3659,11 +3788,7 @@ PHP_FUNCTION(openssl_pkey_new)
400 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, p);
401 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, g);
402 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, priv_key);
403 - OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, pub_key);
404 - if (php_openssl_pkey_init_dh(dh)) {
405 + if (php_openssl_pkey_init_dh(dh, data)) {
406 if (EVP_PKEY_assign_DH(pkey, dh)) {
407 RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
409 @@ -3738,7 +3863,7 @@ PHP_FUNCTION(openssl_pkey_export_to_file
413 - switch (EVP_PKEY_type(key->type)) {
414 + switch (EVP_PKEY_base_id(key)) {
415 #ifdef HAVE_EVP_PKEY_EC
417 pem_write = PEM_write_bio_ECPrivateKey(bio_out, EVP_PKEY_get1_EC_KEY(key), cipher, (unsigned char *)passphrase, passphrase_len, NULL, NULL);
418 @@ -3807,7 +3932,7 @@ PHP_FUNCTION(openssl_pkey_export)
422 - switch (EVP_PKEY_type(key->type)) {
423 + switch (EVP_PKEY_base_id(key)) {
424 #ifdef HAVE_EVP_PKEY_EC
426 pem_write = PEM_write_bio_ECPrivateKey(bio_out, EVP_PKEY_get1_EC_KEY(key), cipher, (unsigned char *)passphrase, passphrase_len, NULL, NULL);
427 @@ -3928,25 +4053,32 @@ PHP_FUNCTION(openssl_pkey_get_details)
428 /*TODO: Use the real values once the openssl constants are used
429 * See the enum at the top of this file
431 - switch (EVP_PKEY_type(pkey->type)) {
432 + switch (EVP_PKEY_base_id(pkey)) {
435 - ktype = OPENSSL_KEYTYPE_RSA;
437 - if (pkey->pkey.rsa != NULL) {
440 - ALLOC_INIT_ZVAL(rsa);
442 - OPENSSL_PKEY_GET_BN(rsa, n);
443 - OPENSSL_PKEY_GET_BN(rsa, e);
444 - OPENSSL_PKEY_GET_BN(rsa, d);
445 - OPENSSL_PKEY_GET_BN(rsa, p);
446 - OPENSSL_PKEY_GET_BN(rsa, q);
447 - OPENSSL_PKEY_GET_BN(rsa, dmp1);
448 - OPENSSL_PKEY_GET_BN(rsa, dmq1);
449 - OPENSSL_PKEY_GET_BN(rsa, iqmp);
450 - add_assoc_zval(return_value, "rsa", rsa);
452 + RSA *rsa = EVP_PKEY_get0_RSA(pkey);
453 + ktype = OPENSSL_KEYTYPE_RSA;
457 + const BIGNUM *n, *e, *d, *p, *q, *dmp1, *dmq1, *iqmp;
459 + RSA_get0_key(rsa, &n, &e, &d);
460 + RSA_get0_factors(rsa, &p, &q);
461 + RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
463 + array_init(&z_rsa);
464 + OPENSSL_PKEY_GET_BN(z_rsa, n);
465 + OPENSSL_PKEY_GET_BN(z_rsa, e);
466 + OPENSSL_PKEY_GET_BN(z_rsa, d);
467 + OPENSSL_PKEY_GET_BN(z_rsa, p);
468 + OPENSSL_PKEY_GET_BN(z_rsa, q);
469 + OPENSSL_PKEY_GET_BN(z_rsa, dmp1);
470 + OPENSSL_PKEY_GET_BN(z_rsa, dmq1);
471 + OPENSSL_PKEY_GET_BN(z_rsa, iqmp);
472 + add_assoc_zval(return_value, "rsa", &z_rsa);
477 @@ -3954,42 +4086,53 @@ PHP_FUNCTION(openssl_pkey_get_details)
481 - ktype = OPENSSL_KEYTYPE_DSA;
483 - if (pkey->pkey.dsa != NULL) {
486 - ALLOC_INIT_ZVAL(dsa);
488 - OPENSSL_PKEY_GET_BN(dsa, p);
489 - OPENSSL_PKEY_GET_BN(dsa, q);
490 - OPENSSL_PKEY_GET_BN(dsa, g);
491 - OPENSSL_PKEY_GET_BN(dsa, priv_key);
492 - OPENSSL_PKEY_GET_BN(dsa, pub_key);
493 - add_assoc_zval(return_value, "dsa", dsa);
495 + DSA *dsa = EVP_PKEY_get0_DSA(pkey);
496 + ktype = OPENSSL_KEYTYPE_DSA;
500 + const BIGNUM *p, *q, *g, *priv_key, *pub_key;
502 + DSA_get0_pqg(dsa, &p, &q, &g);
503 + DSA_get0_key(dsa, &pub_key, &priv_key);
505 + array_init(&z_dsa);
506 + OPENSSL_PKEY_GET_BN(z_dsa, p);
507 + OPENSSL_PKEY_GET_BN(z_dsa, q);
508 + OPENSSL_PKEY_GET_BN(z_dsa, g);
509 + OPENSSL_PKEY_GET_BN(z_dsa, priv_key);
510 + OPENSSL_PKEY_GET_BN(z_dsa, pub_key);
511 + add_assoc_zval(return_value, "dsa", &z_dsa);
517 - ktype = OPENSSL_KEYTYPE_DH;
519 - if (pkey->pkey.dh != NULL) {
522 - ALLOC_INIT_ZVAL(dh);
524 - OPENSSL_PKEY_GET_BN(dh, p);
525 - OPENSSL_PKEY_GET_BN(dh, g);
526 - OPENSSL_PKEY_GET_BN(dh, priv_key);
527 - OPENSSL_PKEY_GET_BN(dh, pub_key);
528 - add_assoc_zval(return_value, "dh", dh);
530 + DH *dh = EVP_PKEY_get0_DH(pkey);
531 + ktype = OPENSSL_KEYTYPE_DH;
535 + const BIGNUM *p, *q, *g, *priv_key, *pub_key;
537 + DH_get0_pqg(dh, &p, &q, &g);
538 + DH_get0_key(dh, &pub_key, &priv_key);
541 + OPENSSL_PKEY_GET_BN(z_dh, p);
542 + OPENSSL_PKEY_GET_BN(z_dh, g);
543 + OPENSSL_PKEY_GET_BN(z_dh, priv_key);
544 + OPENSSL_PKEY_GET_BN(z_dh, pub_key);
545 + add_assoc_zval(return_value, "dh", &z_dh);
550 #ifdef HAVE_EVP_PKEY_EC
552 ktype = OPENSSL_KEYTYPE_EC;
553 - if (pkey->pkey.ec != NULL) {
554 + if (EVP_PKEY_get0_EC_KEY(pkey) != NULL) {
556 const EC_GROUP *ec_group;
558 @@ -4546,13 +4689,13 @@ PHP_FUNCTION(openssl_private_encrypt)
559 cryptedlen = EVP_PKEY_size(pkey);
560 cryptedbuf = emalloc(cryptedlen + 1);
562 - switch (pkey->type) {
563 + switch (EVP_PKEY_id(pkey)) {
566 successful = (RSA_private_encrypt(data_len,
567 (unsigned char *)data,
570 + EVP_PKEY_get0_RSA(pkey),
571 padding) == cryptedlen);
574 @@ -4604,13 +4747,13 @@ PHP_FUNCTION(openssl_private_decrypt)
575 cryptedlen = EVP_PKEY_size(pkey);
576 crypttemp = emalloc(cryptedlen + 1);
578 - switch (pkey->type) {
579 + switch (EVP_PKEY_id(pkey)) {
582 cryptedlen = RSA_private_decrypt(data_len,
583 (unsigned char *)data,
586 + EVP_PKEY_get0_RSA(pkey),
588 if (cryptedlen != -1) {
589 cryptedbuf = emalloc(cryptedlen + 1);
590 @@ -4669,13 +4812,13 @@ PHP_FUNCTION(openssl_public_encrypt)
591 cryptedlen = EVP_PKEY_size(pkey);
592 cryptedbuf = emalloc(cryptedlen + 1);
594 - switch (pkey->type) {
595 + switch (EVP_PKEY_id(pkey)) {
598 successful = (RSA_public_encrypt(data_len,
599 (unsigned char *)data,
603 + EVP_PKEY_get0_RSA(pkey),
604 padding) == cryptedlen);
607 @@ -4728,13 +4871,13 @@ PHP_FUNCTION(openssl_public_decrypt)
608 cryptedlen = EVP_PKEY_size(pkey);
609 crypttemp = emalloc(cryptedlen + 1);
611 - switch (pkey->type) {
612 + switch (EVP_PKEY_id(pkey)) {
615 cryptedlen = RSA_public_decrypt(data_len,
616 (unsigned char *)data,
620 + EVP_PKEY_get0_RSA(pkey),
622 if (cryptedlen != -1) {
623 cryptedbuf = emalloc(cryptedlen + 1);
624 @@ -4798,7 +4941,7 @@ PHP_FUNCTION(openssl_sign)
625 long keyresource = -1;
629 + EVP_MD_CTX *md_ctx;
631 long signature_algo = OPENSSL_ALGO_SHA1;
632 const EVP_MD *mdtype;
633 @@ -4831,9 +4974,11 @@ PHP_FUNCTION(openssl_sign)
634 siglen = EVP_PKEY_size(pkey);
635 sigbuf = emalloc(siglen + 1);
637 - EVP_SignInit(&md_ctx, mdtype);
638 - EVP_SignUpdate(&md_ctx, data, data_len);
639 - if (EVP_SignFinal (&md_ctx, sigbuf,(unsigned int *)&siglen, pkey)) {
640 + md_ctx = EVP_MD_CTX_create();
641 + if (md_ctx != NULL &&
642 + EVP_SignInit(md_ctx, mdtype) &&
643 + EVP_SignUpdate(md_ctx, data, data_len) &&
644 + EVP_SignFinal(md_ctx, (unsigned char*)sigbuf, &siglen, pkey)) {
645 zval_dtor(signature);
646 sigbuf[siglen] = '\0';
647 ZVAL_STRINGL(signature, (char *)sigbuf, siglen, 0);
648 @@ -4842,7 +4987,7 @@ PHP_FUNCTION(openssl_sign)
652 - EVP_MD_CTX_cleanup(&md_ctx);
653 + EVP_MD_CTX_destroy(md_ctx);
654 if (keyresource == -1) {
657 @@ -4855,8 +5000,8 @@ PHP_FUNCTION(openssl_verify)
664 + EVP_MD_CTX *md_ctx;
665 const EVP_MD *mdtype;
666 long keyresource = -1;
667 char * data; int data_len;
668 @@ -4890,10 +5035,13 @@ PHP_FUNCTION(openssl_verify)
672 - EVP_VerifyInit (&md_ctx, mdtype);
673 - EVP_VerifyUpdate (&md_ctx, data, data_len);
674 - err = EVP_VerifyFinal (&md_ctx, (unsigned char *)signature, signature_len, pkey);
675 - EVP_MD_CTX_cleanup(&md_ctx);
676 + md_ctx = EVP_MD_CTX_create();
677 + if (md_ctx != NULL) {
678 + EVP_VerifyInit(md_ctx, mdtype);
679 + EVP_VerifyUpdate (md_ctx, data, data_len);
680 + err = EVP_VerifyFinal(md_ctx, (unsigned char *)signature, (unsigned int)signature_len, pkey);
682 + EVP_MD_CTX_destroy(md_ctx);
684 if (keyresource == -1) {
686 @@ -4917,7 +5065,7 @@ PHP_FUNCTION(openssl_seal)
689 const EVP_CIPHER *cipher;
690 - EVP_CIPHER_CTX ctx;
691 + EVP_CIPHER_CTX *ctx;
693 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szza/|s", &data, &data_len, &sealdata, &ekeys, &pubkeys, &method, &method_len) == FAILURE) {
695 @@ -4968,9 +5116,10 @@ PHP_FUNCTION(openssl_seal)
699 - if (!EVP_EncryptInit(&ctx,cipher,NULL,NULL)) {
700 + ctx = EVP_CIPHER_CTX_new();
701 + if (ctx == NULL || !EVP_EncryptInit(ctx,cipher,NULL,NULL)) {
702 + EVP_CIPHER_CTX_free(ctx);
704 - EVP_CIPHER_CTX_cleanup(&ctx);
708 @@ -4980,15 +5129,15 @@ PHP_FUNCTION(openssl_seal)
709 iv = ivlen ? emalloc(ivlen + 1) : NULL;
711 /* allocate one byte extra to make room for \0 */
712 - buf = emalloc(data_len + EVP_CIPHER_CTX_block_size(&ctx));
713 - EVP_CIPHER_CTX_cleanup(&ctx);
714 + buf = emalloc(data_len + EVP_CIPHER_CTX_block_size(ctx));
715 + EVP_CIPHER_CTX_cleanup(ctx);
717 - if (EVP_SealInit(&ctx, cipher, eks, eksl, NULL, pkeys, nkeys) <= 0 ||
718 - !EVP_SealUpdate(&ctx, buf, &len1, (unsigned char *)data, data_len) ||
719 - !EVP_SealFinal(&ctx, buf + len1, &len2)) {
720 + if (EVP_SealInit(ctx, cipher, eks, eksl, NULL, pkeys, nkeys) <= 0 ||
721 + !EVP_SealUpdate(ctx, buf, &len1, (unsigned char *)data, data_len) ||
722 + !EVP_SealFinal(ctx, buf + len1, &len2)) {
725 - EVP_CIPHER_CTX_cleanup(&ctx);
726 + EVP_CIPHER_CTX_free(ctx);
730 @@ -5019,7 +5168,7 @@ PHP_FUNCTION(openssl_seal)
733 RETVAL_LONG(len1 + len2);
734 - EVP_CIPHER_CTX_cleanup(&ctx);
735 + EVP_CIPHER_CTX_free(ctx);
738 for (i=0; i<nkeys; i++) {
739 @@ -5046,7 +5195,7 @@ PHP_FUNCTION(openssl_open)
742 long keyresource = -1;
743 - EVP_CIPHER_CTX ctx;
744 + EVP_CIPHER_CTX *ctx;
745 char * data; int data_len;
746 char * ekey; int ekey_len;
748 @@ -5072,27 +5221,26 @@ PHP_FUNCTION(openssl_open)
754 buf = emalloc(data_len + 1);
756 - if (EVP_OpenInit(&ctx, cipher, (unsigned char *)ekey, ekey_len, NULL, pkey) && EVP_OpenUpdate(&ctx, buf, &len1, (unsigned char *)data, data_len)) {
757 - if (!EVP_OpenFinal(&ctx, buf + len1, &len2) || (len1 + len2 == 0)) {
761 - zval_dtor(opendata);
762 - buf[len1 + len2] = '\0';
763 - ZVAL_STRINGL(opendata, erealloc(buf, len1 + len2 + 1), len1 + len2, 0);
766 + ctx = EVP_CIPHER_CTX_new();
767 + if (ctx != NULL && EVP_OpenInit(ctx, cipher, (unsigned char *)ekey, (int)ekey_len, NULL, pkey) &&
768 + EVP_OpenUpdate(ctx, buf, &len1, (unsigned char *)data, (int)data_len) &&
769 + EVP_OpenFinal(ctx, buf + len1, &len2) && (len1 + len2 > 0)) {
770 + zval_dtor(opendata);
771 + buf[len1 + len2] = '\0';
772 + ZVAL_STRINGL(opendata, erealloc(buf, len1 + len2 + 1), len1 + len2, 0);
780 if (keyresource == -1) {
783 - EVP_CIPHER_CTX_cleanup(&ctx);
784 + EVP_CIPHER_CTX_free(ctx);
788 @@ -5152,7 +5300,7 @@ PHP_FUNCTION(openssl_digest)
790 int data_len, method_len;
791 const EVP_MD *mdtype;
793 + EVP_MD_CTX *md_ctx;
795 unsigned char *sigbuf;
797 @@ -5168,9 +5316,10 @@ PHP_FUNCTION(openssl_digest)
798 siglen = EVP_MD_size(mdtype);
799 sigbuf = emalloc(siglen + 1);
801 - EVP_DigestInit(&md_ctx, mdtype);
802 - EVP_DigestUpdate(&md_ctx, (unsigned char *)data, data_len);
803 - if (EVP_DigestFinal (&md_ctx, (unsigned char *)sigbuf, (unsigned int *)&siglen)) {
804 + md_ctx = EVP_MD_CTX_create();
805 + if (EVP_DigestInit(md_ctx, mdtype) &&
806 + EVP_DigestUpdate(md_ctx, (unsigned char *)data, data_len) &&
807 + EVP_DigestFinal (md_ctx, (unsigned char *)sigbuf, &siglen)) {
809 sigbuf[siglen] = '\0';
810 RETVAL_STRINGL((char *)sigbuf, siglen, 0);
811 @@ -5186,6 +5335,8 @@ PHP_FUNCTION(openssl_digest)
816 + EVP_MD_CTX_destroy(md_ctx);
820 @@ -5231,7 +5382,7 @@ PHP_FUNCTION(openssl_encrypt)
821 char *data, *method, *password, *iv = "";
822 int data_len, method_len, password_len, iv_len = 0, max_iv_len;
823 const EVP_CIPHER *cipher_type;
824 - EVP_CIPHER_CTX cipher_ctx;
825 + EVP_CIPHER_CTX *cipher_ctx;
826 int i=0, outlen, keylen;
827 unsigned char *outbuf, *key;
829 @@ -5245,6 +5396,12 @@ PHP_FUNCTION(openssl_encrypt)
833 + cipher_ctx = EVP_CIPHER_CTX_new();
835 + php_error_docref(NULL, E_WARNING, "Failed to create cipher context");
839 keylen = EVP_CIPHER_key_length(cipher_type);
840 if (keylen > password_len) {
841 key = emalloc(keylen);
842 @@ -5263,19 +5420,19 @@ PHP_FUNCTION(openssl_encrypt)
843 outlen = data_len + EVP_CIPHER_block_size(cipher_type);
844 outbuf = safe_emalloc(outlen, 1, 1);
846 - EVP_EncryptInit(&cipher_ctx, cipher_type, NULL, NULL);
847 + EVP_EncryptInit(cipher_ctx, cipher_type, NULL, NULL);
848 if (password_len > keylen) {
849 - EVP_CIPHER_CTX_set_key_length(&cipher_ctx, password_len);
850 + EVP_CIPHER_CTX_set_key_length(cipher_ctx, password_len);
852 - EVP_EncryptInit_ex(&cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
853 + EVP_EncryptInit_ex(cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
854 if (options & OPENSSL_ZERO_PADDING) {
855 - EVP_CIPHER_CTX_set_padding(&cipher_ctx, 0);
856 + EVP_CIPHER_CTX_set_padding(cipher_ctx, 0);
859 - EVP_EncryptUpdate(&cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
860 + EVP_EncryptUpdate(cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
863 - if (EVP_EncryptFinal(&cipher_ctx, (unsigned char *)outbuf + i, &i)) {
864 + if (EVP_EncryptFinal(cipher_ctx, (unsigned char *)outbuf + i, &i)) {
866 if (options & OPENSSL_RAW_DATA) {
867 outbuf[outlen] = '\0';
868 @@ -5302,7 +5459,7 @@ PHP_FUNCTION(openssl_encrypt)
872 - EVP_CIPHER_CTX_cleanup(&cipher_ctx);
873 + EVP_CIPHER_CTX_free(cipher_ctx);
877 @@ -5314,7 +5471,7 @@ PHP_FUNCTION(openssl_decrypt)
878 char *data, *method, *password, *iv = "";
879 int data_len, method_len, password_len, iv_len = 0;
880 const EVP_CIPHER *cipher_type;
881 - EVP_CIPHER_CTX cipher_ctx;
882 + EVP_CIPHER_CTX *cipher_ctx;
883 int i, outlen, keylen;
884 unsigned char *outbuf, *key;
886 @@ -5336,10 +5493,17 @@ PHP_FUNCTION(openssl_decrypt)
890 + cipher_ctx = EVP_CIPHER_CTX_new();
892 + php_error_docref(NULL, E_WARNING, "Failed to create cipher context");
896 if (!(options & OPENSSL_RAW_DATA)) {
897 base64_str = (char*)php_base64_decode((unsigned char*)data, data_len, &base64_str_len);
899 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to base64 decode the input");
900 + EVP_CIPHER_CTX_free(cipher_ctx);
903 data_len = base64_str_len;
904 @@ -5360,17 +5524,17 @@ PHP_FUNCTION(openssl_decrypt)
905 outlen = data_len + EVP_CIPHER_block_size(cipher_type);
906 outbuf = emalloc(outlen + 1);
908 - EVP_DecryptInit(&cipher_ctx, cipher_type, NULL, NULL);
909 + EVP_DecryptInit(cipher_ctx, cipher_type, NULL, NULL);
910 if (password_len > keylen) {
911 - EVP_CIPHER_CTX_set_key_length(&cipher_ctx, password_len);
912 + EVP_CIPHER_CTX_set_key_length(cipher_ctx, password_len);
914 - EVP_DecryptInit_ex(&cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
915 + EVP_DecryptInit_ex(cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
916 if (options & OPENSSL_ZERO_PADDING) {
917 - EVP_CIPHER_CTX_set_padding(&cipher_ctx, 0);
918 + EVP_CIPHER_CTX_set_padding(cipher_ctx, 0);
920 - EVP_DecryptUpdate(&cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
921 + EVP_DecryptUpdate(cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
923 - if (EVP_DecryptFinal(&cipher_ctx, (unsigned char *)outbuf + i, &i)) {
924 + if (EVP_DecryptFinal(cipher_ctx, (unsigned char *)outbuf + i, &i)) {
926 outbuf[outlen] = '\0';
927 RETVAL_STRINGL((char *)outbuf, outlen, 0);
928 @@ -5387,7 +5551,7 @@ PHP_FUNCTION(openssl_decrypt)
932 - EVP_CIPHER_CTX_cleanup(&cipher_ctx);
933 + EVP_CIPHER_CTX_free(cipher_ctx);
937 @@ -5425,6 +5589,7 @@ PHP_FUNCTION(openssl_dh_compute_key)
945 @@ -5434,14 +5599,18 @@ PHP_FUNCTION(openssl_dh_compute_key)
948 ZEND_FETCH_RESOURCE(pkey, EVP_PKEY *, &key, -1, "OpenSSL key", le_key);
949 - if (!pkey || EVP_PKEY_type(pkey->type) != EVP_PKEY_DH || !pkey->pkey.dh) {
950 + if (EVP_PKEY_base_id(pkey) != EVP_PKEY_DH) {
953 + dh = EVP_PKEY_get0_DH(pkey);
958 pub = BN_bin2bn((unsigned char*)pub_str, pub_len, NULL);
960 - data = emalloc(DH_size(pkey->pkey.dh) + 1);
961 - len = DH_compute_key((unsigned char*)data, pub, pkey->pkey.dh);
962 + data = emalloc(DH_size(dh) + 1);
963 + len = DH_compute_key((unsigned char*)data, pub, dh);