@@ -369,11 +369,11 @@ int php_openssl_parse_config(struct php_x509_request * req, zval * optional_args
369
369
if (strcmp (req -> digest_name , "null" ) == 0 ) {
370
370
req -> digest = req -> md_alg = EVP_md_null ();
371
371
} else {
372
- req -> digest = req -> md_alg = EVP_get_digestbyname (req -> digest_name );
372
+ req -> digest = req -> md_alg = php_openssl_get_evp_md_by_name (req -> digest_name );
373
373
}
374
374
}
375
375
if (req -> md_alg == NULL ) {
376
- req -> md_alg = req -> digest = EVP_sha1 ( );
376
+ req -> md_alg = req -> digest = php_openssl_get_evp_md_by_name ( "sha1" );
377
377
php_openssl_store_errors ();
378
378
}
379
379
@@ -417,6 +417,10 @@ void php_openssl_dispose_config(struct php_x509_request * req)
417
417
NCONF_free (req -> req_config );
418
418
req -> req_config = NULL ;
419
419
}
420
+ if (req -> md_alg != NULL && req -> md_alg != EVP_md_null ()) {
421
+ php_openssl_release_evp_md (req -> md_alg );
422
+ }
423
+ php_openssl_release_evp_cipher (req -> priv_key_encrypt_cipher );
420
424
}
421
425
422
426
zend_result php_openssl_load_rand_file (const char * file , int * egdsocket , int * seeded )
@@ -469,92 +473,6 @@ zend_result php_openssl_write_rand_file(const char * file, int egdsocket, int se
469
473
return SUCCESS ;
470
474
}
471
475
472
- EVP_MD * php_openssl_get_evp_md_from_algo (zend_long algo ) {
473
- EVP_MD * mdtype ;
474
-
475
- switch (algo ) {
476
- case OPENSSL_ALGO_SHA1 :
477
- mdtype = (EVP_MD * ) EVP_sha1 ();
478
- break ;
479
- case OPENSSL_ALGO_MD5 :
480
- mdtype = (EVP_MD * ) EVP_md5 ();
481
- break ;
482
- #ifndef OPENSSL_NO_MD4
483
- case OPENSSL_ALGO_MD4 :
484
- mdtype = (EVP_MD * ) EVP_md4 ();
485
- break ;
486
- #endif
487
- #ifndef OPENSSL_NO_MD2
488
- case OPENSSL_ALGO_MD2 :
489
- mdtype = (EVP_MD * ) EVP_md2 ();
490
- break ;
491
- #endif
492
- case OPENSSL_ALGO_SHA224 :
493
- mdtype = (EVP_MD * ) EVP_sha224 ();
494
- break ;
495
- case OPENSSL_ALGO_SHA256 :
496
- mdtype = (EVP_MD * ) EVP_sha256 ();
497
- break ;
498
- case OPENSSL_ALGO_SHA384 :
499
- mdtype = (EVP_MD * ) EVP_sha384 ();
500
- break ;
501
- case OPENSSL_ALGO_SHA512 :
502
- mdtype = (EVP_MD * ) EVP_sha512 ();
503
- break ;
504
- #ifndef OPENSSL_NO_RMD160
505
- case OPENSSL_ALGO_RMD160 :
506
- mdtype = (EVP_MD * ) EVP_ripemd160 ();
507
- break ;
508
- #endif
509
- default :
510
- return NULL ;
511
- break ;
512
- }
513
- return mdtype ;
514
- }
515
-
516
- const EVP_CIPHER * php_openssl_get_evp_cipher_from_algo (zend_long algo ) {
517
- switch (algo ) {
518
- #ifndef OPENSSL_NO_RC2
519
- case PHP_OPENSSL_CIPHER_RC2_40 :
520
- return EVP_rc2_40_cbc ();
521
- break ;
522
- case PHP_OPENSSL_CIPHER_RC2_64 :
523
- return EVP_rc2_64_cbc ();
524
- break ;
525
- case PHP_OPENSSL_CIPHER_RC2_128 :
526
- return EVP_rc2_cbc ();
527
- break ;
528
- #endif
529
-
530
- #ifndef OPENSSL_NO_DES
531
- case PHP_OPENSSL_CIPHER_DES :
532
- return EVP_des_cbc ();
533
- break ;
534
- case PHP_OPENSSL_CIPHER_3DES :
535
- return EVP_des_ede3_cbc ();
536
- break ;
537
- #endif
538
-
539
- #ifndef OPENSSL_NO_AES
540
- case PHP_OPENSSL_CIPHER_AES_128_CBC :
541
- return EVP_aes_128_cbc ();
542
- break ;
543
- case PHP_OPENSSL_CIPHER_AES_192_CBC :
544
- return EVP_aes_192_cbc ();
545
- break ;
546
- case PHP_OPENSSL_CIPHER_AES_256_CBC :
547
- return EVP_aes_256_cbc ();
548
- break ;
549
- #endif
550
-
551
-
552
- default :
553
- return NULL ;
554
- break ;
555
- }
556
- }
557
-
558
476
void php_openssl_backend_init (void )
559
477
{
560
478
#ifdef LIBRESSL_VERSION_NUMBER
@@ -1931,14 +1849,15 @@ PHP_OPENSSL_API zend_string* php_openssl_encrypt(
1931
1849
PHP_OPENSSL_CHECK_LONG_TO_INT_NULL_RETURN (tag_len , tag_len );
1932
1850
1933
1851
1934
- cipher_type = EVP_get_cipherbyname (method );
1852
+ cipher_type = php_openssl_get_evp_cipher_by_name (method );
1935
1853
if (!cipher_type ) {
1936
1854
php_error_docref (NULL , E_WARNING , "Unknown cipher algorithm" );
1937
1855
return NULL ;
1938
1856
}
1939
1857
1940
1858
cipher_ctx = EVP_CIPHER_CTX_new ();
1941
1859
if (!cipher_ctx ) {
1860
+ php_openssl_release_evp_cipher (cipher_type );
1942
1861
php_error_docref (NULL , E_WARNING , "Failed to create cipher context" );
1943
1862
return NULL ;
1944
1863
}
@@ -1997,6 +1916,7 @@ PHP_OPENSSL_API zend_string* php_openssl_encrypt(
1997
1916
}
1998
1917
EVP_CIPHER_CTX_reset (cipher_ctx );
1999
1918
EVP_CIPHER_CTX_free (cipher_ctx );
1919
+ php_openssl_release_evp_cipher (cipher_type );
2000
1920
return outbuf ;
2001
1921
}
2002
1922
@@ -2023,14 +1943,15 @@ PHP_OPENSSL_API zend_string* php_openssl_decrypt(
2023
1943
PHP_OPENSSL_CHECK_SIZE_T_TO_INT_NULL_RETURN (tag_len , tag );
2024
1944
2025
1945
2026
- cipher_type = EVP_get_cipherbyname (method );
1946
+ cipher_type = php_openssl_get_evp_cipher_by_name (method );
2027
1947
if (!cipher_type ) {
2028
1948
php_error_docref (NULL , E_WARNING , "Unknown cipher algorithm" );
2029
1949
return NULL ;
2030
1950
}
2031
1951
2032
1952
cipher_ctx = EVP_CIPHER_CTX_new ();
2033
1953
if (!cipher_ctx ) {
1954
+ php_openssl_release_evp_cipher (cipher_type );
2034
1955
php_error_docref (NULL , E_WARNING , "Failed to create cipher context" );
2035
1956
return NULL ;
2036
1957
}
@@ -2076,14 +1997,15 @@ PHP_OPENSSL_API zend_string* php_openssl_decrypt(
2076
1997
}
2077
1998
EVP_CIPHER_CTX_reset (cipher_ctx );
2078
1999
EVP_CIPHER_CTX_free (cipher_ctx );
2000
+ php_openssl_release_evp_cipher (cipher_type );
2079
2001
return outbuf ;
2080
2002
}
2081
2003
2082
- const EVP_CIPHER * php_openssl_get_evp_cipher_by_name (const char * method )
2004
+ const EVP_CIPHER * php_openssl_get_evp_cipher_by_name_with_warning (const char * method )
2083
2005
{
2084
2006
const EVP_CIPHER * cipher_type ;
2085
2007
2086
- cipher_type = EVP_get_cipherbyname (method );
2008
+ cipher_type = php_openssl_get_evp_cipher_by_name (method );
2087
2009
if (!cipher_type ) {
2088
2010
php_error_docref (NULL , E_WARNING , "Unknown cipher algorithm" );
2089
2011
return NULL ;
@@ -2095,16 +2017,26 @@ const EVP_CIPHER *php_openssl_get_evp_cipher_by_name(const char *method)
2095
2017
2096
2018
PHP_OPENSSL_API zend_long php_openssl_cipher_iv_length (const char * method )
2097
2019
{
2098
- const EVP_CIPHER * cipher_type = php_openssl_get_evp_cipher_by_name (method );
2020
+ const EVP_CIPHER * cipher_type = php_openssl_get_evp_cipher_by_name_with_warning (method );
2021
+ if (cipher_type == NULL ) {
2022
+ return -1 ;
2023
+ }
2024
+ int iv_length = EVP_CIPHER_iv_length (cipher_type );
2025
+ php_openssl_release_evp_cipher (cipher_type );
2099
2026
2100
- return cipher_type == NULL ? -1 : EVP_CIPHER_iv_length ( cipher_type ) ;
2027
+ return iv_length ;
2101
2028
}
2102
2029
2103
2030
PHP_OPENSSL_API zend_long php_openssl_cipher_key_length (const char * method )
2104
2031
{
2105
- const EVP_CIPHER * cipher_type = php_openssl_get_evp_cipher_by_name (method );
2032
+ const EVP_CIPHER * cipher_type = php_openssl_get_evp_cipher_by_name_with_warning (method );
2033
+ if (cipher_type == NULL ) {
2034
+ return -1 ;
2035
+ }
2036
+ int key_length = EVP_CIPHER_key_length (cipher_type );
2037
+ php_openssl_release_evp_cipher (cipher_type );
2106
2038
2107
- return cipher_type == NULL ? -1 : EVP_CIPHER_key_length ( cipher_type ) ;
2039
+ return key_length ;
2108
2040
}
2109
2041
2110
2042
PHP_OPENSSL_API zend_string * php_openssl_random_pseudo_bytes (zend_long buffer_length )
0 commit comments