Commit 2d50c06de5f for php
commit 2d50c06de5ff038b71be6304b47021ac3be02572
Author: David Carlier <devnexen@gmail.com>
Date: Fri May 1 21:56:28 2026 +0100
ext/openssl: avoid redundant input/output copies.
Switch the PKCS12/PKCS7/CMS readers to BIO_new_mem_buf so the parsers
work on the PHP string directly. Also adds a missing NULL check in
pkcs12_read.
openssl_seal/openssl_open write straight into a zend_string instead
of an emalloc buffer that gets copied via zend_string_init.
Close GH-21926
diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c
index aa9abb6c3a4..4c2f6e2929b 100644
--- a/ext/openssl/openssl.c
+++ b/ext/openssl/openssl.c
@@ -1896,9 +1896,9 @@ PHP_FUNCTION(openssl_pkcs12_read)
PHP_OPENSSL_CHECK_SIZE_T_TO_INT(zp12_len, pkcs12, 1);
- bio_in = BIO_new(BIO_s_mem());
+ bio_in = BIO_new_mem_buf(zp12, (int)zp12_len);
- if (0 >= BIO_write(bio_in, zp12, (int)zp12_len)) {
+ if (bio_in == NULL) {
php_openssl_store_errors();
goto cleanup;
}
@@ -3188,12 +3188,8 @@ PHP_FUNCTION(openssl_pkcs7_read)
PHP_OPENSSL_CHECK_SIZE_T_TO_INT(p7b_len, p7b, 1);
- bio_in = BIO_new(BIO_s_mem());
+ bio_in = BIO_new_mem_buf(p7b, (int)p7b_len);
if (bio_in == NULL) {
- goto clean_exit;
- }
-
- if (0 >= BIO_write(bio_in, p7b, (int)p7b_len)) {
php_openssl_store_errors();
goto clean_exit;
}
@@ -3868,12 +3864,8 @@ PHP_FUNCTION(openssl_cms_read)
PHP_OPENSSL_CHECK_SIZE_T_TO_INT(p7b_len, p7b, 1);
- bio_in = BIO_new(BIO_s_mem());
+ bio_in = BIO_new_mem_buf(p7b, (int)p7b_len);
if (bio_in == NULL) {
- goto clean_exit;
- }
-
- if (0 >= BIO_write(bio_in, p7b, (int)p7b_len)) {
php_openssl_store_errors();
goto clean_exit;
}
@@ -4691,8 +4683,9 @@ PHP_FUNCTION(openssl_seal)
zval *pubkeys, *pubkey, *sealdata, *ekeys, *iv = NULL;
HashTable *pubkeysht;
EVP_PKEY **pkeys;
+ zend_string *buf;
int i, len1, len2, *eksl, nkeys, iv_len;
- unsigned char iv_buf[EVP_MAX_IV_LENGTH + 1], *buf = NULL, **eks;
+ unsigned char iv_buf[EVP_MAX_IV_LENGTH + 1], **eks;
char * data;
size_t data_len;
char *method;
@@ -4757,13 +4750,13 @@ PHP_FUNCTION(openssl_seal)
}
/* allocate one byte extra to make room for \0 */
- buf = emalloc(data_len + EVP_CIPHER_CTX_block_size(ctx));
+ buf = zend_string_alloc(data_len + EVP_CIPHER_CTX_block_size(ctx), false);
EVP_CIPHER_CTX_reset(ctx);
if (EVP_SealInit(ctx, cipher, eks, eksl, &iv_buf[0], pkeys, nkeys) <= 0 ||
- !EVP_SealUpdate(ctx, buf, &len1, (unsigned char *)data, (int)data_len) ||
- !EVP_SealFinal(ctx, buf + len1, &len2)) {
- efree(buf);
+ !EVP_SealUpdate(ctx, (unsigned char *)ZSTR_VAL(buf), &len1, (unsigned char *)data, (int)data_len) ||
+ !EVP_SealFinal(ctx, (unsigned char *)ZSTR_VAL(buf) + len1, &len2)) {
+ zend_string_release(buf);
EVP_CIPHER_CTX_free(ctx);
php_openssl_store_errors();
RETVAL_FALSE;
@@ -4771,8 +4764,9 @@ PHP_FUNCTION(openssl_seal)
}
if (len1 + len2 > 0) {
- ZEND_TRY_ASSIGN_REF_NEW_STR(sealdata, zend_string_init((char*)buf, len1 + len2, 0));
- efree(buf);
+ ZSTR_VAL(buf)[len1 + len2] = 0;
+ ZSTR_LEN(buf) = len1 + len2;
+ ZEND_TRY_ASSIGN_REF_NEW_STR(sealdata, buf);
ekeys = zend_try_array_init(ekeys);
if (!ekeys) {
@@ -4792,7 +4786,7 @@ PHP_FUNCTION(openssl_seal)
ZEND_TRY_ASSIGN_REF_NEW_STR(iv, zend_string_init((char*)iv_buf, iv_len, 0));
}
} else {
- efree(buf);
+ zend_string_release(buf);
}
RETVAL_LONG(len1 + len2);
EVP_CIPHER_CTX_free(ctx);
@@ -4817,9 +4811,10 @@ PHP_FUNCTION(openssl_seal)
PHP_FUNCTION(openssl_open)
{
zval *privkey, *opendata;
+ zend_string *buf;
EVP_PKEY *pkey;
int len1, len2, cipher_iv_len;
- unsigned char *buf, *iv_buf;
+ unsigned char *iv_buf;
EVP_CIPHER_CTX *ctx;
char * data;
size_t data_len;
@@ -4868,21 +4863,22 @@ PHP_FUNCTION(openssl_open)
iv_buf = NULL;
}
- buf = emalloc(data_len + 1);
+ buf = zend_string_alloc(data_len, false);
ctx = EVP_CIPHER_CTX_new();
if (ctx != NULL && EVP_OpenInit(ctx, cipher, (unsigned char *)ekey, (int)ekey_len, iv_buf, pkey) &&
- EVP_OpenUpdate(ctx, buf, &len1, (unsigned char *)data, (int)data_len) &&
- EVP_OpenFinal(ctx, buf + len1, &len2) && (len1 + len2 > 0)) {
- buf[len1 + len2] = '\0';
- ZEND_TRY_ASSIGN_REF_NEW_STR(opendata, zend_string_init((char*)buf, len1 + len2, 0));
+ EVP_OpenUpdate(ctx, (unsigned char *)ZSTR_VAL(buf), &len1, (unsigned char *)data, (int)data_len) &&
+ EVP_OpenFinal(ctx, (unsigned char *)ZSTR_VAL(buf) + len1, &len2) && (len1 + len2 > 0)) {
+ ZSTR_VAL(buf)[len1 + len2] = '\0';
+ ZSTR_LEN(buf) = len1 + len2;
+ ZEND_TRY_ASSIGN_REF_NEW_STR(opendata, buf);
RETVAL_TRUE;
} else {
php_openssl_store_errors();
+ zend_string_release(buf);
RETVAL_FALSE;
}
- efree(buf);
EVP_CIPHER_CTX_free(ctx);
out_pkey:
EVP_PKEY_free(pkey);