Skip to content

Commit 3e14942

Browse files
committed
Require $method parameter in openssl_seal/openssl_open
RC4 is considered insecure, and it's not possible to change the default of these functions. As such, require the method to be passed explicitly. Closes GH-6093.
1 parent 259af93 commit 3e14942

9 files changed

+38
-72
lines changed

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ PHP 8.0 UPGRADE NOTES
403403
. The openssl_pkey_free() function is deprecated and no longer has an effect,
404404
instead the OpenSSLAsymmetricKey instance is automatically destroyed if it is no
405405
longer referenced.
406+
. openssl_seal() and openssl_open() now require $method to be passed, as the
407+
previous default of "RC4" is considered insecure.
406408

407409
- PCRE:
408410
. When passing invalid escape sequences they are no longer interpreted as

ext/openssl/openssl.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6584,7 +6584,7 @@ PHP_FUNCTION(openssl_seal)
65846584
const EVP_CIPHER *cipher;
65856585
EVP_CIPHER_CTX *ctx;
65866586

6587-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "szza|sz", &data, &data_len,
6587+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "szzas|z", &data, &data_len,
65886588
&sealdata, &ekeys, &pubkeys, &method, &method_len, &iv) == FAILURE) {
65896589
RETURN_THROWS();
65906590
}
@@ -6598,14 +6598,10 @@ PHP_FUNCTION(openssl_seal)
65986598
RETURN_THROWS();
65996599
}
66006600

6601-
if (method) {
6602-
cipher = EVP_get_cipherbyname(method);
6603-
if (!cipher) {
6604-
php_error_docref(NULL, E_WARNING, "Unknown signature algorithm");
6605-
RETURN_FALSE;
6606-
}
6607-
} else {
6608-
cipher = EVP_rc4();
6601+
cipher = EVP_get_cipherbyname(method);
6602+
if (!cipher) {
6603+
php_error_docref(NULL, E_WARNING, "Unknown signature algorithm");
6604+
RETURN_FALSE;
66096605
}
66106606

66116607
iv_len = EVP_CIPHER_iv_length(cipher);
@@ -6715,7 +6711,7 @@ PHP_FUNCTION(openssl_open)
67156711
size_t method_len = 0, iv_len = 0;
67166712
const EVP_CIPHER *cipher;
67176713

6718-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "szsz|ss", &data, &data_len, &opendata,
6714+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "szszs|s", &data, &data_len, &opendata,
67196715
&ekey, &ekey_len, &privkey, &method, &method_len, &iv, &iv_len) == FAILURE) {
67206716
RETURN_THROWS();
67216717
}
@@ -6731,14 +6727,10 @@ PHP_FUNCTION(openssl_open)
67316727
RETURN_FALSE;
67326728
}
67336729

6734-
if (method) {
6735-
cipher = EVP_get_cipherbyname(method);
6736-
if (!cipher) {
6737-
php_error_docref(NULL, E_WARNING, "Unknown signature algorithm");
6738-
RETURN_FALSE;
6739-
}
6740-
} else {
6741-
cipher = EVP_rc4();
6730+
cipher = EVP_get_cipherbyname(method);
6731+
if (!cipher) {
6732+
php_error_docref(NULL, E_WARNING, "Unknown signature algorithm");
6733+
RETURN_FALSE;
67426734
}
67436735

67446736
cipher_iv_len = EVP_CIPHER_iv_length(cipher);

ext/openssl/openssl.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,13 @@ function openssl_verify(string $data, string $signature, $key, $method = OPENSSL
187187
* @param OpenSSLAsymmetricKey|OpenSSLCertificate|array|string $pubkeys
188188
* @param string $iv
189189
*/
190-
function openssl_seal(string $data, &$sealdata, &$ekeys, array $pubkeys, string $method = UNKNOWN, &$iv = UNKNOWN): int|false {}
190+
function openssl_seal(string $data, &$sealdata, &$ekeys, array $pubkeys, string $method, &$iv = UNKNOWN): int|false {}
191191

192192
/**
193193
* @param string $opendata
194194
* @param OpenSSLAsymmetricKey|OpenSSLCertificate|array|string $privkey
195195
*/
196-
function openssl_open(string $data, &$opendata, string $ekey, $privkey, string $method = UNKNOWN, string $iv = UNKNOWN): bool {}
196+
function openssl_open(string $data, &$opendata, string $ekey, $privkey, string $method, string $iv = UNKNOWN): bool {}
197197

198198
function openssl_get_md_methods(bool $aliases = false): array {}
199199

ext/openssl/openssl_arginfo.h

Lines changed: 3 additions & 3 deletions
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: 10a514c9947313694296c6ec9ec6f2fa8e6c850b */
2+
* Stub hash: 7f1066b832ce307914f641de5ed2c40ec10290ba */
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, x509, OpenSSLCertificate, MAY_BE_STRING, NULL)
@@ -272,7 +272,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_openssl_verify, 0, 3, MAY_BE_LON
272272
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, method, "OPENSSL_ALGO_SHA1")
273273
ZEND_END_ARG_INFO()
274274

275-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_openssl_seal, 0, 4, MAY_BE_LONG|MAY_BE_FALSE)
275+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_openssl_seal, 0, 5, MAY_BE_LONG|MAY_BE_FALSE)
276276
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
277277
ZEND_ARG_INFO(1, sealdata)
278278
ZEND_ARG_INFO(1, ekeys)
@@ -281,7 +281,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_openssl_seal, 0, 4, MAY_BE_LONG|
281281
ZEND_ARG_INFO(1, iv)
282282
ZEND_END_ARG_INFO()
283283

284-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_openssl_open, 0, 4, _IS_BOOL, 0)
284+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_openssl_open, 0, 5, _IS_BOOL, 0)
285285
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
286286
ZEND_ARG_INFO(1, opendata)
287287
ZEND_ARG_TYPE_INFO(0, ekey, IS_STRING, 0)

ext/openssl/tests/bug70395.phpt

Lines changed: 0 additions & 19 deletions
This file was deleted.

ext/openssl/tests/bug71475.phpt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ if (!extension_loaded("openssl")) die("skip openssl not loaded");
77
--FILE--
88
<?php
99
$_ = str_repeat("A", 512);
10-
openssl_seal($_, $_, $_, array_fill(0,64,0));
10+
try {
11+
openssl_seal($_, $_, $_, array_fill(0,64,0));
12+
} catch (TypeError $e) {
13+
echo $e->getMessage(), "\n";
14+
}
1115
?>
1216
DONE
13-
--EXPECTF--
14-
Warning: openssl_seal(): Not a public key (1th member of pubkeys) in %s%ebug71475.php on line %d
17+
--EXPECT--
18+
openssl_seal() expects at least 5 parameters, 4 given
1519
DONE

ext/openssl/tests/bug75307.phpt

Lines changed: 0 additions & 15 deletions
This file was deleted.

ext/openssl/tests/openssl_open_basic.phpt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ $data = "openssl_open() test";
88
$pub_key = "file://" . __DIR__ . "/public.key";
99
$priv_key = "file://" . __DIR__ . "/private_rsa_1024.key";
1010
$wrong = "wrong";
11+
$method = "RC4";
1112

12-
openssl_seal($data, $sealed, $ekeys, array($pub_key, $pub_key, $pub_key));
13-
openssl_open($sealed, $output, $ekeys[0], $priv_key);
13+
openssl_seal($data, $sealed, $ekeys, array($pub_key, $pub_key, $pub_key), $method);
14+
openssl_open($sealed, $output, $ekeys[0], $priv_key, $method);
1415
var_dump($output);
15-
openssl_open($sealed, $output2, $ekeys[1], $wrong);
16+
openssl_open($sealed, $output2, $ekeys[1], $wrong, $method);
1617
var_dump($output2);
17-
openssl_open($sealed, $output3, $ekeys[2], $priv_key);
18+
openssl_open($sealed, $output3, $ekeys[2], $priv_key, $method);
1819
var_dump($output3);
19-
openssl_open($sealed, $output4, $wrong, $priv_key);
20+
openssl_open($sealed, $output4, $wrong, $priv_key, $method);
2021
var_dump($output4);
2122
?>
2223
--EXPECTF--

ext/openssl/tests/openssl_seal_basic.phpt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ $a = 1;
99
$b = array(1);
1010
$c = array(1);
1111
$d = array(1);
12+
$method = "RC4";
1213

13-
var_dump(openssl_seal($a, $b, $c, $d));
14+
var_dump(openssl_seal($a, $b, $c, $d, $method));
1415

1516
try {
16-
var_dump(openssl_seal($a, $a, $a, array()));
17+
var_dump(openssl_seal($a, $a, $a, array(), $method));
1718
} catch (\ValueError $e) {
1819
echo $e->getMessage() . \PHP_EOL;
1920
}
@@ -23,17 +24,17 @@ $data = "openssl_open() test";
2324
$pub_key = "file://" . __DIR__ . "/public.key";
2425
$wrong = "wrong";
2526

26-
var_dump(openssl_seal($data, $sealed, $ekeys, array($pub_key))); // no output
27-
var_dump(openssl_seal($data, $sealed, $ekeys, array($pub_key, $pub_key))); // no output
28-
var_dump(openssl_seal($data, $sealed, $ekeys, array($pub_key, $wrong)));
27+
var_dump(openssl_seal($data, $sealed, $ekeys, array($pub_key), $method)); // no output
28+
var_dump(openssl_seal($data, $sealed, $ekeys, array($pub_key, $pub_key), $method)); // no output
29+
var_dump(openssl_seal($data, $sealed, $ekeys, array($pub_key, $wrong), $method));
2930

3031
try {
31-
var_dump(openssl_seal($data, $sealed, $ekeys, array()));
32+
var_dump(openssl_seal($data, $sealed, $ekeys, array(), $method));
3233
} catch (\ValueError $e) {
3334
echo $e->getMessage() . \PHP_EOL;
3435
}
3536

36-
var_dump(openssl_seal($data, $sealed, $ekeys, array($wrong)));
37+
var_dump(openssl_seal($data, $sealed, $ekeys, array($wrong), $method));
3738

3839
?>
3940
--EXPECTF--

0 commit comments

Comments
 (0)