Skip to content

Commit 6703b73

Browse files
committed
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.
1 parent 425b97e commit 6703b73

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed

ext/openssl/openssl.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4588,3 +4588,83 @@ PHP_FUNCTION(openssl_random_pseudo_bytes)
45884588
}
45894589
}
45904590
/* }}} */
4591+
4592+
/* {{{ Given an Object ID, or object short or long name, return an associative
4593+
array containing any known OID, short name, and long name, or false if the
4594+
object is not known.
4595+
4596+
Example:
4597+
4598+
var_dump( openssl_oid_lookup( "CN" ) );
4599+
var_dump( openssl_oid_lookup( "unstructuredAddress" ) );
4600+
var_dump( openssl_oid_lookup( "1.2.3.4.5" ) );
4601+
var_dump( openssl_oid_lookup( "junk" ) );
4602+
4603+
Produces;
4604+
4605+
array(3) {
4606+
["oid"]=>
4607+
string(7) "2.5.4.3"
4608+
["lname"]=>
4609+
string(10) "commonName"
4610+
["sname"]=>
4611+
string(2) "CN"
4612+
}
4613+
4614+
array(2) {
4615+
["oid"]=>
4616+
string(20) "1.2.840.113549.1.9.8"
4617+
["lname"]=>
4618+
string(19) "unstructuredAddress"
4619+
}
4620+
4621+
array(1) {
4622+
["oid"]=>
4623+
string(9) "1.2.3.4.5"
4624+
}
4625+
4626+
bool(false)
4627+
4628+
*/
4629+
PHP_FUNCTION(openssl_oid_lookup)
4630+
{
4631+
zend_string * txt;
4632+
ASN1_OBJECT *obj;
4633+
char buf[1024];
4634+
int nid;
4635+
4636+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &txt) == FAILURE) {
4637+
return;
4638+
}
4639+
4640+
obj = OBJ_txt2obj(ZSTR_VAL(txt), 0);
4641+
if (obj == NULL) {
4642+
RETURN_FALSE;
4643+
}
4644+
4645+
OBJ_obj2txt(buf, sizeof(buf)-1, obj, 1);
4646+
if (*buf == '\0') {
4647+
RETURN_FALSE;
4648+
}
4649+
4650+
array_init(return_value);
4651+
add_assoc_string(return_value, "oid", buf);
4652+
4653+
if ((nid = OBJ_obj2nid(obj)) != NID_undef) {
4654+
const char *l;
4655+
const char *s;
4656+
4657+
l = OBJ_nid2ln(nid);
4658+
if (l != NULL) {
4659+
add_assoc_string(return_value, "lname", (char *) l);
4660+
}
4661+
4662+
s = OBJ_nid2sn(nid);
4663+
if (s != NULL && (l == NULL || strcmp(s,l) != 0)) {
4664+
add_assoc_string(return_value, "sname", (char *) s);
4665+
}
4666+
}
4667+
4668+
ASN1_OBJECT_free(obj);
4669+
}
4670+
/* }}} */

ext/openssl/openssl.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,3 +699,5 @@ function openssl_get_cert_locations(): array {}
699699
function openssl_password_hash(string $algo, #[\SensitiveParameter] string $password, array $options = []): string {}
700700
function openssl_password_verify(string $algo, #[\SensitiveParameter] string $password, string $hash): bool {}
701701
#endif
702+
703+
function openssl_oid_lookup(string $txt): array {}

ext/openssl/openssl_arginfo.h

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
openssl_csr_new() attributes setting tests
3+
--EXTENSIONS--
4+
openssl
5+
--FILE--
6+
<?php
7+
var_dump(openssl_oid_lookup("CN"));
8+
var_dump(openssl_oid_lookup("unstructuredAddress"));
9+
var_dump(openssl_oid_lookup("1.2.3.4.5"));
10+
var_dump(openssl_oid_lookup("junk"));
11+
?>
12+
--EXPECTF--
13+
array(3) {
14+
["oid"]=>
15+
string(7) "2.5.4.3"
16+
["lname"]=>
17+
string(10) "commonName"
18+
["sname"]=>
19+
string(2) "CN"
20+
}
21+
array(2) {
22+
["oid"]=>
23+
string(20) "1.2.840.113549.1.9.8"
24+
["lname"]=>
25+
string(19) "unstructuredAddress"
26+
}
27+
array(1) {
28+
["oid"]=>
29+
string(9) "1.2.3.4.5"
30+
}
31+
bool(false)

0 commit comments

Comments
 (0)