Skip to content

Commit 7f0d3f5

Browse files
committed
Fixed bug #81502
Allow $tag to be null. This is the value that openssl_encrypt() sets it to for non-AEAD ciphers, so we should also accept this as an input to openssl_decrypt(). Prior to PHP 8.1, null was accepted in weak mode due to the special treatment of null arguments to internal functions.
1 parent 7ad877c commit 7f0d3f5

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ PHP NEWS
2222
- PgSQL:
2323
. Fixed bug #81509 (pg_end_copy still expects a resource). (Matteo)
2424

25+
- OpenSSL:
26+
. Fixed bug #81502 ($tag argument of openssl_decrypt() should accept
27+
null/empty string). (Nikita)
28+
2529
- Standard:
2630
. Fixed bug #81491 (Incorrectly using libsodium for argon2 hashing).
2731
(Dan Pock)

ext/openssl/openssl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7614,7 +7614,7 @@ PHP_FUNCTION(openssl_decrypt)
76147614
size_t data_len, method_len, password_len, iv_len = 0, tag_len = 0, aad_len = 0;
76157615
zend_string *ret;
76167616

7617-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|lsss", &data, &data_len, &method, &method_len,
7617+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|lss!s", &data, &data_len, &method, &method_len,
76187618
&password, &password_len, &options, &iv, &iv_len, &tag, &tag_len, &aad, &aad_len) == FAILURE) {
76197619
RETURN_THROWS();
76207620
}

ext/openssl/openssl.stub.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ function openssl_digest(string $data, string $digest_algo, bool $binary = false)
236236
/** @param string $tag */
237237
function openssl_encrypt(string $data, string $cipher_algo, string $passphrase, int $options = 0, string $iv = "", &$tag = null, string $aad = "", int $tag_length = 16): string|false {}
238238

239-
function openssl_decrypt(string $data, string $cipher_algo, string $passphrase, int $options = 0, string $iv = "", string $tag = "", string $aad = ""): string|false {}
239+
function openssl_decrypt(string $data, string $cipher_algo, string $passphrase, int $options = 0, string $iv = "", ?string $tag = null, string $aad = ""): string|false {}
240240

241241
function openssl_cipher_iv_length(string $cipher_algo): int|false {}
242242

ext/openssl/openssl_arginfo.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 3ad007a0b29648c29051f9ee00fe43dd6f2a766d */
2+
* Stub hash: 320aca9647019329a42dc3e7937420610a8a4419 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_openssl_x509_export_to_file, 0, 2, _IS_BOOL, 0)
55
ZEND_ARG_OBJ_TYPE_MASK(0, certificate, OpenSSLCertificate, MAY_BE_STRING, NULL)
@@ -342,7 +342,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_openssl_decrypt, 0, 3, MAY_BE_ST
342342
ZEND_ARG_TYPE_INFO(0, passphrase, IS_STRING, 0)
343343
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_LONG, 0, "0")
344344
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, iv, IS_STRING, 0, "\"\"")
345-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, tag, IS_STRING, 0, "\"\"")
345+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, tag, IS_STRING, 1, "null")
346346
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, aad, IS_STRING, 0, "\"\"")
347347
ZEND_END_ARG_INFO()
348348

ext/openssl/tests/openssl_decrypt_basic.phpt

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ $encrypted = openssl_encrypt($padded_data, $method, $password, OPENSSL_RAW_DATA|
2525
$output = openssl_decrypt($encrypted, $method, $password, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv);
2626
var_dump(rtrim($output));
2727

28+
$output2 = openssl_decrypt($encrypted, $method, $password, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv, tag: '');
29+
var_dump($output2 === $output);
30+
$output3 = openssl_decrypt($encrypted, $method, $password, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv, tag: null);
31+
var_dump($output3 === $output);
32+
2833
if (in_array("bf-ecb", openssl_get_cipher_methods())) {
2934
// if we want to prefer variable length cipher setting
3035
$encrypted = openssl_encrypt($data, "bf-ecb", $password, OPENSSL_DONT_ZERO_PAD_KEY);
@@ -45,4 +50,6 @@ string(45) "openssl_encrypt() and openssl_decrypt() tests"
4550
string(45) "openssl_encrypt() and openssl_decrypt() tests"
4651
string(45) "openssl_encrypt() and openssl_decrypt() tests"
4752
bool(true)
53+
bool(true)
54+
bool(true)
4855
NULL

0 commit comments

Comments
 (0)