From dfd76cc8fcc3df88069295b68a594c9a29f475d1 Mon Sep 17 00:00:00 2001 From: Steve Wall Date: Thu, 2 Oct 2025 10:25:40 -0400 Subject: [PATCH 1/8] Function to look up Object IDs Create a function to lookup and return an OID, short name, and long name, given a string containing any one of those, for all OIDs known to OpenSSL. --- ext/openssl/openssl.c | 80 +++++++++++++++++++ ext/openssl/openssl.stub.php | 2 + ext/openssl/openssl_arginfo.h | 8 +- .../tests/openssl_oid_lookup_basic.phpt | 31 +++++++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 ext/openssl/tests/openssl_oid_lookup_basic.phpt diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c index 80c0a8bab073..dc6769c67a7c 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -5106,3 +5106,83 @@ PHP_FUNCTION(openssl_random_pseudo_bytes) } } /* }}} */ + +/* {{{ Given an Object ID, or object short or long name, return an associative + array containing any known OID, short name, and long name, or false if the + object is not known. + + Example: + + var_dump( openssl_oid_lookup( "CN" ) ); + var_dump( openssl_oid_lookup( "unstructuredAddress" ) ); + var_dump( openssl_oid_lookup( "1.2.3.4.5" ) ); + var_dump( openssl_oid_lookup( "junk" ) ); + + Produces; + + array(3) { + ["oid"]=> + string(7) "2.5.4.3" + ["lname"]=> + string(10) "commonName" + ["sname"]=> + string(2) "CN" + } + + array(2) { + ["oid"]=> + string(20) "1.2.840.113549.1.9.8" + ["lname"]=> + string(19) "unstructuredAddress" + } + + array(1) { + ["oid"]=> + string(9) "1.2.3.4.5" + } + + bool(false) + +*/ +PHP_FUNCTION(openssl_oid_lookup) +{ + zend_string * txt; + ASN1_OBJECT *obj; + char buf[1024]; + int nid; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &txt) == FAILURE) { + return; + } + + obj = OBJ_txt2obj(ZSTR_VAL(txt), 0); + if (obj == NULL) { + RETURN_FALSE; + } + + OBJ_obj2txt(buf, sizeof(buf)-1, obj, 1); + if (*buf == '\0') { + RETURN_FALSE; + } + + array_init(return_value); + add_assoc_string(return_value, "oid", buf); + + if ((nid = OBJ_obj2nid(obj)) != NID_undef) { + const char *l; + const char *s; + + l = OBJ_nid2ln(nid); + if (l != NULL) { + add_assoc_string(return_value, "lname", (char *) l); + } + + s = OBJ_nid2sn(nid); + if (s != NULL && (l == NULL || strcmp(s,l) != 0)) { + add_assoc_string(return_value, "sname", (char *) s); + } + } + + ASN1_OBJECT_free(obj); +} +/* }}} */ diff --git a/ext/openssl/openssl.stub.php b/ext/openssl/openssl.stub.php index 3d3fa3ea634f..c87518c2222d 100644 --- a/ext/openssl/openssl.stub.php +++ b/ext/openssl/openssl.stub.php @@ -776,4 +776,6 @@ function openssl_password_hash(string $algo, #[\SensitiveParameter] string $pass function openssl_password_verify(string $algo, #[\SensitiveParameter] string $password, string $hash): bool {} #endif +function openssl_oid_lookup(string $txt): array {} + } diff --git a/ext/openssl/openssl_arginfo.h b/ext/openssl/openssl_arginfo.h index b1742dcb05f8..37e7930c7fc5 100644 --- a/ext/openssl/openssl_arginfo.h +++ b/ext/openssl/openssl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit openssl.stub.php instead. - * Stub hash: 7cad995b734d69f98d489edb97a7878a4ea8f47e */ + * Stub hash: 2d9777e4ef23023b9a907ea70e128a1fb700755f */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_openssl_x509_export_to_file, 0, 2, _IS_BOOL, 0) ZEND_ARG_OBJ_TYPE_MASK(0, certificate, OpenSSLCertificate, MAY_BE_STRING, NULL) @@ -408,6 +408,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_openssl_password_verify, 0, 3, _ ZEND_END_ARG_INFO() #endif +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_openssl_oid_lookup, 0, 1, IS_ARRAY, 0) + ZEND_ARG_TYPE_INFO(0, txt, IS_STRING, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Openssl_Psk___construct, 0, 0, 1) ZEND_ARG_TYPE_INFO(0, psk, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, identity, IS_STRING, 1, "null") @@ -513,6 +517,7 @@ ZEND_FUNCTION(openssl_get_cert_locations); ZEND_FUNCTION(openssl_password_hash); ZEND_FUNCTION(openssl_password_verify); #endif +ZEND_FUNCTION(openssl_oid_lookup); ZEND_METHOD(Openssl_Psk, __construct); ZEND_METHOD(Openssl_Session, export); ZEND_METHOD(Openssl_Session, import); @@ -597,6 +602,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(openssl_password_hash, arginfo_openssl_password_hash) ZEND_FE(openssl_password_verify, arginfo_openssl_password_verify) #endif + ZEND_FE(openssl_oid_lookup, arginfo_openssl_oid_lookup) ZEND_FE_END }; diff --git a/ext/openssl/tests/openssl_oid_lookup_basic.phpt b/ext/openssl/tests/openssl_oid_lookup_basic.phpt new file mode 100644 index 000000000000..c3bc48e3761d --- /dev/null +++ b/ext/openssl/tests/openssl_oid_lookup_basic.phpt @@ -0,0 +1,31 @@ +--TEST-- +openssl_csr_new() attributes setting tests +--EXTENSIONS-- +openssl +--FILE-- + +--EXPECTF-- +array(3) { + ["oid"]=> + string(7) "2.5.4.3" + ["lname"]=> + string(10) "commonName" + ["sname"]=> + string(2) "CN" +} +array(2) { + ["oid"]=> + string(20) "1.2.840.113549.1.9.8" + ["lname"]=> + string(19) "unstructuredAddress" +} +array(1) { + ["oid"]=> + string(9) "1.2.3.4.5" +} +bool(false) From c0bc307ad1d1dd2b4058d51ee4a47630395d7647 Mon Sep 17 00:00:00 2001 From: Steve Wall Date: Sun, 2 Nov 2025 21:01:47 -0500 Subject: [PATCH 2/8] Fix return type in stub --- ext/openssl/openssl.stub.php | 6 +++++- ext/openssl/openssl_arginfo.h | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ext/openssl/openssl.stub.php b/ext/openssl/openssl.stub.php index c87518c2222d..dad7334a5737 100644 --- a/ext/openssl/openssl.stub.php +++ b/ext/openssl/openssl.stub.php @@ -776,6 +776,10 @@ function openssl_password_hash(string $algo, #[\SensitiveParameter] string $pass function openssl_password_verify(string $algo, #[\SensitiveParameter] string $password, string $hash): bool {} #endif -function openssl_oid_lookup(string $txt): array {} +/** + * @return array|false + * @refcount 1 + */ +function openssl_oid_lookup(string $txt): array|false {} } diff --git a/ext/openssl/openssl_arginfo.h b/ext/openssl/openssl_arginfo.h index 37e7930c7fc5..4f34d3e7ca96 100644 --- a/ext/openssl/openssl_arginfo.h +++ b/ext/openssl/openssl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit openssl.stub.php instead. - * Stub hash: 2d9777e4ef23023b9a907ea70e128a1fb700755f */ + * Stub hash: fbca020939546a151cefe60c4bf58e4cbe7db4f0 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_openssl_x509_export_to_file, 0, 2, _IS_BOOL, 0) ZEND_ARG_OBJ_TYPE_MASK(0, certificate, OpenSSLCertificate, MAY_BE_STRING, NULL) @@ -408,7 +408,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_openssl_password_verify, 0, 3, _ ZEND_END_ARG_INFO() #endif -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_openssl_oid_lookup, 0, 1, IS_ARRAY, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_openssl_oid_lookup, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE) ZEND_ARG_TYPE_INFO(0, txt, IS_STRING, 0) ZEND_END_ARG_INFO() From 199adb4a8cfd43cf3dd8032892389924d2820f6b Mon Sep 17 00:00:00 2001 From: Steve Wall Date: Mon, 3 Nov 2025 10:40:29 -0500 Subject: [PATCH 3/8] update optimizer info --- Zend/Optimizer/zend_func_infos.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Zend/Optimizer/zend_func_infos.h b/Zend/Optimizer/zend_func_infos.h index 88d012149beb..dd353c2efcac 100644 --- a/Zend/Optimizer/zend_func_infos.h +++ b/Zend/Optimizer/zend_func_infos.h @@ -297,6 +297,7 @@ static const func_info_t func_infos[] = { F1("openssl_get_curve_names", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE), #endif F1("openssl_get_cert_locations", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_STRING), + F1("openssl_oid_lookup", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE), FN("pcntl_signal_get_handler", MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_OBJECT|MAY_BE_LONG), FN("preg_replace", MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_STRING|MAY_BE_NULL), FN("preg_filter", MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_STRING|MAY_BE_NULL), From 41b1a03ed6f86f99e97dd80db354b770d0d8f8fb Mon Sep 17 00:00:00 2001 From: Steve Wall Date: Wed, 10 Dec 2025 09:28:36 -0500 Subject: [PATCH 4/8] Remove example from comments --- ext/openssl/openssl.c | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c index dc6769c67a7c..f601ce471378 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -5110,39 +5110,6 @@ PHP_FUNCTION(openssl_random_pseudo_bytes) /* {{{ Given an Object ID, or object short or long name, return an associative array containing any known OID, short name, and long name, or false if the object is not known. - - Example: - - var_dump( openssl_oid_lookup( "CN" ) ); - var_dump( openssl_oid_lookup( "unstructuredAddress" ) ); - var_dump( openssl_oid_lookup( "1.2.3.4.5" ) ); - var_dump( openssl_oid_lookup( "junk" ) ); - - Produces; - - array(3) { - ["oid"]=> - string(7) "2.5.4.3" - ["lname"]=> - string(10) "commonName" - ["sname"]=> - string(2) "CN" - } - - array(2) { - ["oid"]=> - string(20) "1.2.840.113549.1.9.8" - ["lname"]=> - string(19) "unstructuredAddress" - } - - array(1) { - ["oid"]=> - string(9) "1.2.3.4.5" - } - - bool(false) - */ PHP_FUNCTION(openssl_oid_lookup) { From e2fb94acf3de142de5ad21d294bcd3dc773a849d Mon Sep 17 00:00:00 2001 From: Steve Wall Date: Tue, 13 Jan 2026 16:38:11 -0500 Subject: [PATCH 5/8] Address feedback on pull request --- ext/openssl/openssl.c | 26 +++++++++++-------- .../tests/openssl_oid_lookup_basic.phpt | 2 +- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c index f601ce471378..627f08363d37 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -5107,7 +5107,7 @@ PHP_FUNCTION(openssl_random_pseudo_bytes) } /* }}} */ -/* {{{ Given an Object ID, or object short or long name, return an associative +/* Given an Object ID, or object short or long name, return an associative array containing any known OID, short name, and long name, or false if the object is not known. */ @@ -5115,8 +5115,9 @@ PHP_FUNCTION(openssl_oid_lookup) { zend_string * txt; ASN1_OBJECT *obj; - char buf[1024]; + char buf[256]; int nid; + bool found = false; if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &txt) == FAILURE) { return; @@ -5127,13 +5128,12 @@ PHP_FUNCTION(openssl_oid_lookup) RETURN_FALSE; } - OBJ_obj2txt(buf, sizeof(buf)-1, obj, 1); - if (*buf == '\0') { - RETURN_FALSE; - } - array_init(return_value); - add_assoc_string(return_value, "oid", buf); + + if (OBJ_obj2txt(buf, sizeof(buf)-1, obj, 1) > 0 && *buf != '\0') { + add_assoc_string(return_value, "oid", buf); + found = TRUE; + } if ((nid = OBJ_obj2nid(obj)) != NID_undef) { const char *l; @@ -5142,14 +5142,18 @@ PHP_FUNCTION(openssl_oid_lookup) l = OBJ_nid2ln(nid); if (l != NULL) { add_assoc_string(return_value, "lname", (char *) l); + found = TRUE; } s = OBJ_nid2sn(nid); - if (s != NULL && (l == NULL || strcmp(s,l) != 0)) { + if (s != NULL) { add_assoc_string(return_value, "sname", (char *) s); + found = TRUE; } } - ASN1_OBJECT_free(obj); + + if (!found) { + RETURN_FALSE; + } } -/* }}} */ diff --git a/ext/openssl/tests/openssl_oid_lookup_basic.phpt b/ext/openssl/tests/openssl_oid_lookup_basic.phpt index c3bc48e3761d..17d726062a91 100644 --- a/ext/openssl/tests/openssl_oid_lookup_basic.phpt +++ b/ext/openssl/tests/openssl_oid_lookup_basic.phpt @@ -9,7 +9,7 @@ var_dump(openssl_oid_lookup("unstructuredAddress")); var_dump(openssl_oid_lookup("1.2.3.4.5")); var_dump(openssl_oid_lookup("junk")); ?> ---EXPECTF-- +--EXPECT-- array(3) { ["oid"]=> string(7) "2.5.4.3" From d09a05267cc0a70a7a2502f7575b1e76ab8e3af2 Mon Sep 17 00:00:00 2001 From: Steve Wall Date: Wed, 14 Jan 2026 09:14:10 -0500 Subject: [PATCH 6/8] `true`, not `TRUE` --- ext/openssl/openssl.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c index 627f08363d37..34483a845b7f 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -5132,7 +5132,7 @@ PHP_FUNCTION(openssl_oid_lookup) if (OBJ_obj2txt(buf, sizeof(buf)-1, obj, 1) > 0 && *buf != '\0') { add_assoc_string(return_value, "oid", buf); - found = TRUE; + found = true; } if ((nid = OBJ_obj2nid(obj)) != NID_undef) { @@ -5142,13 +5142,13 @@ PHP_FUNCTION(openssl_oid_lookup) l = OBJ_nid2ln(nid); if (l != NULL) { add_assoc_string(return_value, "lname", (char *) l); - found = TRUE; + found = true; } s = OBJ_nid2sn(nid); if (s != NULL) { add_assoc_string(return_value, "sname", (char *) s); - found = TRUE; + found = true; } } ASN1_OBJECT_free(obj); From 863abe9c81e57f4cd30be25e93a34cacaf971aaf Mon Sep 17 00:00:00 2001 From: Steve Wall Date: Wed, 14 Jan 2026 10:22:35 -0500 Subject: [PATCH 7/8] Fix test for change to sname --- ext/openssl/tests/openssl_oid_lookup_basic.phpt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ext/openssl/tests/openssl_oid_lookup_basic.phpt b/ext/openssl/tests/openssl_oid_lookup_basic.phpt index 17d726062a91..d04a132336bd 100644 --- a/ext/openssl/tests/openssl_oid_lookup_basic.phpt +++ b/ext/openssl/tests/openssl_oid_lookup_basic.phpt @@ -18,9 +18,11 @@ array(3) { ["sname"]=> string(2) "CN" } -array(2) { +array(3) { ["oid"]=> string(20) "1.2.840.113549.1.9.8" + ["sname"]=> + string(19) "unstructuredAddress" ["lname"]=> string(19) "unstructuredAddress" } From 46d55887360b1074b27264845af0946ce6843e05 Mon Sep 17 00:00:00 2001 From: Steve Wall Date: Wed, 14 Jan 2026 10:42:17 -0500 Subject: [PATCH 8/8] wrong order --- ext/openssl/tests/openssl_oid_lookup_basic.phpt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/openssl/tests/openssl_oid_lookup_basic.phpt b/ext/openssl/tests/openssl_oid_lookup_basic.phpt index d04a132336bd..ed452b3e7530 100644 --- a/ext/openssl/tests/openssl_oid_lookup_basic.phpt +++ b/ext/openssl/tests/openssl_oid_lookup_basic.phpt @@ -21,10 +21,10 @@ array(3) { array(3) { ["oid"]=> string(20) "1.2.840.113549.1.9.8" - ["sname"]=> - string(19) "unstructuredAddress" ["lname"]=> string(19) "unstructuredAddress" + ["sname"]=> + string(19) "unstructuredAddress" } array(1) { ["oid"]=>