Skip to content

Add openssl_cipher_key_length function #9368

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
wants to merge 1 commit into from
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
52 changes: 42 additions & 10 deletions ext/openssl/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -7600,44 +7600,76 @@ PHP_FUNCTION(openssl_decrypt)
}
/* }}} */

PHP_OPENSSL_API zend_long php_openssl_cipher_iv_length(const char *method)
static inline const EVP_CIPHER *php_openssl_get_evp_cipher_by_name(const char *method)
{
const EVP_CIPHER *cipher_type;

cipher_type = EVP_get_cipherbyname(method);
if (!cipher_type) {
php_error_docref(NULL, E_WARNING, "Unknown cipher algorithm");
return -1;
return NULL;
}

return EVP_CIPHER_iv_length(cipher_type);
return cipher_type;
}

PHP_OPENSSL_API zend_long php_openssl_cipher_iv_length(const char *method)
{
const EVP_CIPHER *cipher_type = php_openssl_get_evp_cipher_by_name(method);

return cipher_type == NULL ? -1 : EVP_CIPHER_iv_length(cipher_type);
}

/* {{{ */
PHP_FUNCTION(openssl_cipher_iv_length)
{
char *method;
size_t method_len;
zend_string *method;
zend_long ret;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &method, &method_len) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &method) == FAILURE) {
RETURN_THROWS();
}

if (!method_len) {
if (ZSTR_LEN(method) == 0) {
zend_argument_value_error(1, "cannot be empty");
RETURN_THROWS();
}

/* Warning is emitted in php_openssl_cipher_iv_length */
if ((ret = php_openssl_cipher_iv_length(method)) == -1) {
if ((ret = php_openssl_cipher_iv_length(ZSTR_VAL(method))) == -1) {
RETURN_FALSE;
}

RETURN_LONG(ret);
}
/* }}} */

PHP_OPENSSL_API zend_long php_openssl_cipher_key_length(const char *method)
{
const EVP_CIPHER *cipher_type = php_openssl_get_evp_cipher_by_name(method);

return cipher_type == NULL ? -1 : EVP_CIPHER_key_length(cipher_type);
}

PHP_FUNCTION(openssl_cipher_key_length)
{
zend_string *method;
zend_long ret;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &method) == FAILURE) {
RETURN_THROWS();
}

if (ZSTR_LEN(method) == 0) {
zend_argument_value_error(1, "cannot be empty");
RETURN_THROWS();
}

/* Warning is emitted in php_openssl_cipher_key_length */
if ((ret = php_openssl_cipher_key_length(ZSTR_VAL(method))) == -1) {
RETURN_FALSE;
}

RETURN_LONG(ret);
}

PHP_OPENSSL_API zend_string* php_openssl_random_pseudo_bytes(zend_long buffer_length)
{
Expand Down
2 changes: 2 additions & 0 deletions ext/openssl/openssl.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ function openssl_decrypt(string $data, string $cipher_algo, #[\SensitiveParamete

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

function openssl_cipher_key_length(string $cipher_algo): int|false {}

function openssl_dh_compute_key(string $public_key, #[\SensitiveParameter] OpenSSLAsymmetricKey $private_key): string|false {}

/**
Expand Down
6 changes: 5 additions & 1 deletion ext/openssl/openssl_arginfo.h

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

1 change: 1 addition & 0 deletions ext/openssl/php_openssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ php_stream_transport_factory_func php_openssl_ssl_socket_factory;
void php_openssl_store_errors(void);

PHP_OPENSSL_API zend_long php_openssl_cipher_iv_length(const char *method);
PHP_OPENSSL_API zend_long php_openssl_cipher_key_length(const char *method);
PHP_OPENSSL_API zend_string* php_openssl_random_pseudo_bytes(zend_long length);
PHP_OPENSSL_API zend_string* php_openssl_encrypt(
const char *data, size_t data_len,
Expand Down
12 changes: 12 additions & 0 deletions ext/openssl/tests/openssl_cipher_iv_length_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
openssl_cipher_iv_length() basic test
--EXTENSIONS--
openssl
--FILE--
<?php

var_dump(openssl_cipher_iv_length('AES-128-CBC'));

?>
--EXPECT--
int(16)
21 changes: 21 additions & 0 deletions ext/openssl/tests/openssl_cipher_iv_length_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
openssl_cipher_iv_length() error test
--EXTENSIONS--
openssl
--FILE--
<?php

var_dump(openssl_cipher_iv_length('unknown'));

try {
var_dump(openssl_cipher_iv_length(''));
} catch (ValueError $e) {
echo $e->getMessage() . "\n";
}

?>
--EXPECTF--

Warning: openssl_cipher_iv_length(): Unknown cipher algorithm in %s on line %d
bool(false)
openssl_cipher_iv_length(): Argument #1 ($cipher_algo) cannot be empty
14 changes: 14 additions & 0 deletions ext/openssl/tests/openssl_cipher_key_length_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
openssl_cipher_key_length() basic test
--EXTENSIONS--
openssl
--FILE--
<?php

var_dump(openssl_cipher_key_length('AES-128-CBC'));
var_dump(openssl_cipher_key_length('AES-256-CBC'));

?>
--EXPECT--
int(16)
int(32)
21 changes: 21 additions & 0 deletions ext/openssl/tests/openssl_cipher_key_length_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
openssl_cipher_key_length() error test
--EXTENSIONS--
openssl
--FILE--
<?php

var_dump(openssl_cipher_key_length('unknown'));

try {
var_dump(openssl_cipher_key_length(''));
} catch (ValueError $e) {
echo $e->getMessage() . "\n";
}

?>
--EXPECTF--

Warning: openssl_cipher_key_length(): Unknown cipher algorithm in %s on line %d
bool(false)
openssl_cipher_key_length(): Argument #1 ($cipher_algo) cannot be empty