From 373d5a653628d1899e524286acbc54484eb11d88 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 87440f68f06a..d6b89a4c70c7 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -5109,3 +5109,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 c7fdf82c7861..4efbd0907cec 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 */ #include "zend_attributes.h" #include "zend_constants.h" @@ -411,6 +411,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") @@ -516,6 +520,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); @@ -600,6 +605,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 7d6d2913822da29d0b2bb3d23a24e89f542ede5d 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 4efbd0907cec..84b05a6e3c4b 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 */ #include "zend_attributes.h" #include "zend_constants.h" @@ -411,7 +411,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 170321bc2262029ac8e5d9b3f71dc4afb6cf3cee 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 0add5a32f911..e3e36bee9b46 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 d08fb525b24e5a3b8d40078cecc9b297d0852148 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 d6b89a4c70c7..09872e66b80a 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -5113,39 +5113,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 f30133da4f1c453003656df6eafa9bae337cbc17 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 09872e66b80a..b397fc1d7062 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -5110,7 +5110,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. */ @@ -5118,8 +5118,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; @@ -5130,13 +5131,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; @@ -5145,14 +5145,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 b08bbacdc091cf2fa4262242da03b3baa4fa2545 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 b397fc1d7062..e9c438b3933c 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -5135,7 +5135,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) { @@ -5145,13 +5145,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 251214ac8dd8f2d12688beadd0e369de95cf4fff 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 42825abf7b64ebea284088aec67b128c4a6538b6 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"]=>