Skip to content

Fix #81724: openssl_cms_encrypt only allows specific ciphers #19459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions ext/openssl/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3201,6 +3201,7 @@ PHP_FUNCTION(openssl_cms_encrypt)
X509 * cert;
const EVP_CIPHER *cipher = NULL;
zend_long cipherid = PHP_OPENSSL_CIPHER_DEFAULT;
zend_string *cipher_str = NULL;
zend_string * strindex;
char * infilename = NULL;
size_t infilename_len;
Expand All @@ -3210,11 +3211,16 @@ PHP_FUNCTION(openssl_cms_encrypt)

RETVAL_FALSE;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "ppza!|lll", &infilename, &infilename_len,
&outfilename, &outfilename_len, &zrecipcerts, &zheaders, &flags, &encoding, &cipherid) == FAILURE) {
RETURN_THROWS();
}

ZEND_PARSE_PARAMETERS_START(4, 7)
Z_PARAM_PATH(infilename, infilename_len)
Z_PARAM_PATH(outfilename, outfilename_len)
Z_PARAM_ZVAL(zrecipcerts)
Z_PARAM_ARRAY_OR_NULL(zheaders)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(flags)
Z_PARAM_LONG(encoding)
Z_PARAM_STR_OR_LONG(cipher_str, cipherid)
ZEND_PARSE_PARAMETERS_END();

infile = php_openssl_bio_new_file(
infilename, infilename_len, 1, PHP_OPENSSL_BIO_MODE_R(flags));
Expand Down Expand Up @@ -3273,7 +3279,11 @@ PHP_FUNCTION(openssl_cms_encrypt)
}

/* sanity check the cipher */
cipher = php_openssl_get_evp_cipher_from_algo(cipherid);
if (cipher_str) {
cipher = php_openssl_get_evp_cipher_by_name(ZSTR_VAL(cipher_str));
} else {
cipher = php_openssl_get_evp_cipher_from_algo(cipherid);
}
if (cipher == NULL) {
/* shouldn't happen */
php_error_docref(NULL, E_WARNING, "Failed to get cipher");
Expand Down
2 changes: 1 addition & 1 deletion ext/openssl/openssl.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ function openssl_pkcs7_read(string $data, &$certificates): bool {}
function openssl_cms_verify(string $input_filename, int $flags = 0, ?string $certificates = null, array $ca_info = [], ?string $untrusted_certificates_filename = null, ?string $content = null, ?string $pk7 = null, ?string $sigfile = null, int $encoding = OPENSSL_ENCODING_SMIME): bool {}

/** @param OpenSSLCertificate|array|string $certificate */
function openssl_cms_encrypt(string $input_filename, string $output_filename, $certificate, ?array $headers, int $flags = 0, int $encoding = OPENSSL_ENCODING_SMIME, int $cipher_algo = OPENSSL_CIPHER_AES_128_CBC): bool {}
function openssl_cms_encrypt(string $input_filename, string $output_filename, $certificate, ?array $headers, int $flags = 0, int $encoding = OPENSSL_ENCODING_SMIME, string|int $cipher_algo = OPENSSL_CIPHER_AES_128_CBC): bool {}

/**
* @param OpenSSLAsymmetricKey|OpenSSLCertificate|array|string $private_key
Expand Down
4 changes: 2 additions & 2 deletions ext/openssl/openssl_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions ext/openssl/tests/openssl_cms_encrypt_auth_env.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
openssl_cms_encrypt() auth enveloped data tests
--EXTENSIONS--
openssl
--SKIPIF--
<?php
if (OPENSSL_VERSION_NUMBER < 0x30000000) die('skip For OpenSSL >= 3.0');
?>
--FILE--
<?php
$infile = __DIR__ . "/plain.txt";
$outfile = __DIR__ . "/openssl_cms_encrypt_auth_env.out1";
$outfile2 = __DIR__ . "/openssl_cms_encrypt_auth_env.out2";
$single_cert = "file://" . __DIR__ . "/cert.crt";
$privkey = "file://" . __DIR__ . "/private_rsa_1024.key";
$headers = array("test@test", "testing openssl_cms_encrypt()");
$cipher = "AES-128-GCM";

var_dump(openssl_cms_encrypt($infile, $outfile, $single_cert, $headers, cipher_algo: $cipher));
var_dump(openssl_cms_encrypt($infile, $outfile, openssl_x509_read($single_cert), $headers, cipher_algo: $cipher));
var_dump(openssl_cms_decrypt($outfile, $outfile2, $single_cert, $privkey));
readfile($outfile2);

?>
--CLEAN--
<?php
@unlink(__DIR__ . "/openssl_cms_encrypt_auth_env.out1");
@unlink(__DIR__ . "/openssl_cms_encrypt_auth_env.out2");
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
Now is the winter of our discontent.
21 changes: 10 additions & 11 deletions ext/openssl/tests/openssl_cms_encrypt_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ openssl
--FILE--
<?php
$infile = __DIR__ . "/plain.txt";
$outfile = tempnam(sys_get_temp_dir(), "cms_enc_basic");
if ($outfile === false)
die("failed to get a temporary filename!");
$outfile2 = $outfile . ".out";
$outfile3 = tempnam(sys_get_temp_dir(), "cms_enc_basic");
if ($outfile3 === false)
die("failed to get a temporary filename!");
$outfile = __DIR__ . "/openssl_cms_encrypt_basic.out1";
$outfile2 = __DIR__ . "/openssl_cms_encrypt_basic.out2";
$outfile3 = __DIR__ . "/openssl_cms_encrypt_basic.out3";
$single_cert = "file://" . __DIR__ . "/cert.crt";
$privkey = "file://" . __DIR__ . "/private_rsa_1024.key";
$wrongkey = "file://" . __DIR__ . "/private_rsa_2048.key";
Expand All @@ -27,7 +23,7 @@ var_dump(openssl_cms_encrypt($infile, $outfile, $single_cert, $headers, cipher_a
var_dump(openssl_cms_encrypt($infile, $outfile, openssl_x509_read($single_cert), $headers, cipher_algo: $cipher));
var_dump(openssl_cms_decrypt($outfile, $outfile2, $single_cert, $privkey));
readfile($outfile2);
var_dump(openssl_cms_encrypt($infile, $outfile, $single_cert, $assoc_headers, cipher_algo: $cipher));
var_dump(openssl_cms_encrypt($infile, $outfile, $single_cert, $assoc_headers, cipher_algo: "AES-128-CBC"));
var_dump(openssl_cms_encrypt($infile, $outfile, $single_cert, $empty_headers, cipher_algo: $cipher));
var_dump(openssl_cms_encrypt($wrong, $outfile, $single_cert, $headers, cipher_algo: $cipher));
var_dump(openssl_cms_encrypt($empty, $outfile, $single_cert, $headers, cipher_algo: $cipher));
Expand All @@ -40,11 +36,9 @@ var_dump(openssl_cms_encrypt($infile, $outfile3, $single_cert, $headers, flags:

if (file_exists($outfile)) {
echo "true\n";
unlink($outfile);
}
if (file_exists($outfile2)) {
echo "true\n";
unlink($outfile2);
}

if (file_exists($outfile3)) {
Expand All @@ -53,9 +47,14 @@ if (file_exists($outfile3)) {
echo "true\n";
}
unset($content);
unlink($outfile3);
}
?>
--CLEAN--
<?php
@unlink(__DIR__ . "/openssl_cms_encrypt_basic.out1");
@unlink(__DIR__ . "/openssl_cms_encrypt_basic.out2");
@unlink(__DIR__ . "/openssl_cms_encrypt_basic.out3");
?>
--EXPECT--
bool(true)
bool(true)
Expand Down