Skip to content

Print deprecation when openssl_encrypt truncates passphrase #9302

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
4 changes: 4 additions & 0 deletions ext/openssl/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -7324,6 +7324,10 @@ static int php_openssl_cipher_init(const EVP_CIPHER *cipher_type,
} else {
if (password_len > key_len && !EVP_CIPHER_CTX_set_key_length(cipher_ctx, password_len)) {
php_openssl_store_errors();
php_error_docref(NULL, E_DEPRECATED, "Passphrase is too long and will be truncated to %d characters", key_len);
if (EG(exception)) {
return FAILURE;
}
}
key = (unsigned char*)*ppassword;
}
Expand Down
36 changes: 36 additions & 0 deletions ext/openssl/tests/gh9026.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Bug GH-9026: openssl_encrypt() silently truncates passphrase
--EXTENSIONS--
openssl
--FILE--
<?php

$cipher = "aes-128-ctr";
$data = "test";
$passphrase = "KeyLengthIs16_12";
$iv = str_repeat("\x00", openssl_cipher_iv_length($cipher));
$flags = 0;

$m1 = openssl_encrypt(
$data,
$cipher,
$passphrase,
$flags,
$iv
);

$passphrase .= "3";
$m2 = openssl_encrypt(
$data,
$cipher,
$passphrase,
$flags,
$iv
);

var_dump($m1 === $m2);

?>
--EXPECTF--
Deprecated: openssl_encrypt(): Passphrase is too long and will be truncated to 16 characters in %s on line %d
bool(true)
34 changes: 34 additions & 0 deletions ext/openssl/tests/gh9026_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Bug GH-9026: openssl_encrypt() passphrase too long with error handler
--EXTENSIONS--
openssl
--FILE--
<?php

function err_handler($errno, $errstr, $errfile, $errline) {
throw new Exception($errstr);
}

set_error_handler("err_handler", E_DEPRECATED);

$cipher = "aes-128-ctr";
$data = "test";
$passphrase = "KeyLengthIs16_123";
$iv = str_repeat("\x00", openssl_cipher_iv_length($cipher));
$flags = 0;

try {
var_dump(openssl_encrypt(
$data,
$cipher,
$passphrase,
$flags,
$iv
));
} catch (Exception $e) {
echo $e->getMessage();
}

?>
--EXPECT--
openssl_encrypt(): Passphrase is too long and will be truncated to 16 characters
4 changes: 4 additions & 0 deletions ext/openssl/tests/openssl_error_string_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ $private_key_file_with_pass = "file://" .__DIR__ . "/private_rsa_2048_pass_php.k
$data = "test";
$method = "AES-128-ECB";
$enc_key = str_repeat('x', 40);
// Suppress passphrase truncation deprecation
$error_reporting = error_reporting();
error_reporting($error_reporting ^ E_DEPRECATED);
// error because password is longer then key length and
// EVP_CIPHER_CTX_set_key_length fails for AES
openssl_encrypt($data, $method, $enc_key);
Expand All @@ -84,6 +87,7 @@ var_dump(openssl_error_string());
for ($i = 0; $i < 20; $i++) {
openssl_encrypt($data, $method, $enc_key);
}
error_reporting($error_reporting);
$error_queue_size = 0;
while (($enc_error_new = openssl_error_string()) !== false) {
if ($enc_error_new !== $enc_error) {
Expand Down