Skip to content

Commit a80ae97

Browse files
committed
Only report provided ciphers in openssl_get_cipher_methods()
With OpenSSL 3 ciphers may be registered, but not provided. Make sure that openssl_get_cipher_methods() only returns provided ciphers, so that "in_array openssl_get_cipher_methods" style checks continue working as expected.
1 parent 9695936 commit a80ae97

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

ext/openssl/openssl.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6765,6 +6765,31 @@ PHP_FUNCTION(openssl_get_md_methods)
67656765
}
67666766
/* }}} */
67676767

6768+
#if PHP_OPENSSL_API_VERSION >= 0x30000
6769+
static void php_openssl_add_cipher_name(const char *name, void *arg)
6770+
{
6771+
size_t len = strlen(name);
6772+
zend_string *str = zend_string_alloc(len, 0);
6773+
zend_str_tolower_copy(ZSTR_VAL(str), name, len);
6774+
add_next_index_str((zval*)arg, str);
6775+
}
6776+
6777+
static void php_openssl_add_cipher_or_alias(EVP_CIPHER *cipher, void *arg)
6778+
{
6779+
EVP_CIPHER_names_do_all(cipher, php_openssl_add_cipher_name, arg);
6780+
}
6781+
6782+
static void php_openssl_add_cipher(EVP_CIPHER *cipher, void *arg)
6783+
{
6784+
php_openssl_add_cipher_name(EVP_CIPHER_get0_name(cipher), arg);
6785+
}
6786+
6787+
static int php_openssl_compare_func(Bucket *a, Bucket *b)
6788+
{
6789+
return string_compare_function(&a->val, &b->val);
6790+
}
6791+
#endif
6792+
67686793
/* {{{ Return array of available cipher algorithms */
67696794
PHP_FUNCTION(openssl_get_cipher_methods)
67706795
{
@@ -6774,9 +6799,16 @@ PHP_FUNCTION(openssl_get_cipher_methods)
67746799
RETURN_THROWS();
67756800
}
67766801
array_init(return_value);
6802+
#if PHP_OPENSSL_API_VERSION >= 0x30000
6803+
EVP_CIPHER_do_all_provided(NULL,
6804+
aliases ? php_openssl_add_cipher_or_alias : php_openssl_add_cipher,
6805+
return_value);
6806+
zend_hash_sort(Z_ARRVAL_P(return_value), php_openssl_compare_func, 1);
6807+
#else
67776808
OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
6778-
aliases ? php_openssl_add_method_or_alias: php_openssl_add_method,
6809+
aliases ? php_openssl_add_method_or_alias : php_openssl_add_method,
67796810
return_value);
6811+
#endif
67806812
}
67816813
/* }}} */
67826814

ext/openssl/php_openssl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ extern zend_module_entry openssl_module_entry;
3737
/* OpenSSL version check */
3838
#if OPENSSL_VERSION_NUMBER < 0x10100000L
3939
#define PHP_OPENSSL_API_VERSION 0x10002
40-
#else
40+
#elif OPENSSL_VERSION_NUMBER < 0x30000000L
4141
#define PHP_OPENSSL_API_VERSION 0x10100
42+
#else
43+
#define PHP_OPENSSL_API_VERSION 0x30000
4244
#endif
4345
#endif
4446

0 commit comments

Comments
 (0)