Skip to content

Commit f630a69

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Fix memory leaks when adding certificate to store fails Fix missing error propagation in openssl_x509_export_to_file() Fix memory leak on error path in openssl_open()
2 parents 5405e2b + 1c94175 commit f630a69

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

ext/openssl/openssl.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -562,14 +562,11 @@ PHP_FUNCTION(openssl_x509_export_to_file)
562562

563563
bio_out = BIO_new_file(file_path, PHP_OPENSSL_BIO_MODE_W(PKCS7_BINARY));
564564
if (bio_out) {
565-
if (!notext && !X509_print(bio_out, cert)) {
566-
php_openssl_store_errors();
567-
}
568-
if (!PEM_write_bio_X509(bio_out, cert)) {
565+
if ((notext || X509_print(bio_out, cert)) && PEM_write_bio_X509(bio_out, cert)) {
566+
RETVAL_TRUE;
567+
} else {
569568
php_openssl_store_errors();
570569
}
571-
572-
RETVAL_TRUE;
573570
} else {
574571
php_openssl_store_errors();
575572
php_error_docref(NULL, E_WARNING, "Error opening file %s", file_path);
@@ -4415,18 +4412,20 @@ PHP_FUNCTION(openssl_open)
44154412
cipher = php_openssl_get_evp_cipher_by_name(method);
44164413
if (!cipher) {
44174414
php_error_docref(NULL, E_WARNING, "Unknown cipher algorithm");
4418-
RETURN_FALSE;
4415+
RETVAL_FALSE;
4416+
goto out_pkey;
44194417
}
44204418

44214419
cipher_iv_len = EVP_CIPHER_iv_length(cipher);
44224420
if (cipher_iv_len > 0) {
44234421
if (!iv) {
44244422
zend_argument_value_error(6, "cannot be null for the chosen cipher algorithm");
4425-
RETURN_THROWS();
4423+
goto out_pkey;
44264424
}
44274425
if ((size_t)cipher_iv_len != iv_len) {
44284426
php_error_docref(NULL, E_WARNING, "IV length is invalid");
4429-
RETURN_FALSE;
4427+
RETVAL_FALSE;
4428+
goto out_pkey;
44304429
}
44314430
iv_buf = (unsigned char *)iv;
44324431
} else {
@@ -4448,8 +4447,9 @@ PHP_FUNCTION(openssl_open)
44484447
}
44494448

44504449
efree(buf);
4451-
EVP_PKEY_free(pkey);
44524450
EVP_CIPHER_CTX_free(ctx);
4451+
out_pkey:
4452+
EVP_PKEY_free(pkey);
44534453
}
44544454
/* }}} */
44554455

ext/openssl/xp_ssl.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,8 +857,9 @@ static long php_openssl_load_stream_cafile(X509_STORE *cert_store, const char *c
857857
buffer_active = 0;
858858
if (cert && X509_STORE_add_cert(cert_store, cert)) {
859859
++certs_added;
860-
X509_free(cert);
861860
}
861+
/* TODO: notify user when adding certificate failed? */
862+
X509_free(cert);
862863
goto cert_start;
863864
}
864865

0 commit comments

Comments
 (0)