diff --git a/Zend/Optimizer/zend_func_infos.h b/Zend/Optimizer/zend_func_infos.h index b7b118c710c53..c745ff864af91 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), diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c index 2c09b89e31200..d1ef7833355be 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -4588,3 +4588,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 94902a4acf0da..a6e22968eceb7 100644 --- a/ext/openssl/openssl.stub.php +++ b/ext/openssl/openssl.stub.php @@ -699,3 +699,9 @@ function openssl_get_cert_locations(): array {} function openssl_password_hash(string $algo, #[\SensitiveParameter] string $password, array $options = []): string {} function openssl_password_verify(string $algo, #[\SensitiveParameter] string $password, string $hash): bool {} #endif + +/** + * @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 796582c185bb6..a99bc2cc171e8 100644 --- a/ext/openssl/openssl_arginfo.h +++ b/ext/openssl/openssl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 8233a8abc8ab7145d905d0fa51478edfe1e55a06 */ + * Stub hash: 2288e86f8604335de4876d464b97b8ba52da30d5 */ 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) @@ -406,6 +406,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_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() + ZEND_FUNCTION(openssl_x509_export_to_file); ZEND_FUNCTION(openssl_x509_export); ZEND_FUNCTION(openssl_x509_fingerprint); @@ -473,6 +477,7 @@ ZEND_FUNCTION(openssl_get_cert_locations); ZEND_FUNCTION(openssl_password_hash); ZEND_FUNCTION(openssl_password_verify); #endif +ZEND_FUNCTION(openssl_oid_lookup); static const zend_function_entry ext_functions[] = { ZEND_FE(openssl_x509_export_to_file, arginfo_openssl_x509_export_to_file) @@ -545,6 +550,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 0000000000000..c3bc48e3761d6 --- /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)