OBJ_nid2obj(): Return UNDEF object instead of NULL for NID_undef
authorTomas Mraz <tomas@openssl.org>
Tue, 21 Mar 2023 10:36:56 +0000 (11:36 +0100)
committerTomas Mraz <tomas@openssl.org>
Thu, 23 Mar 2023 14:33:47 +0000 (15:33 +0100)
Fixes a regression from 3.0 from the obj creation refactoring.

Fixes #20555

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/20556)

crypto/objects/obj_dat.c
test/asn1_internal_test.c

index ad476136ae9bf0aeb19192dd98bd9d90ee6d958b..466783d47fa1e6039e2777c3b442bbb68045187d 100644 (file)
@@ -299,9 +299,8 @@ ASN1_OBJECT *OBJ_nid2obj(int n)
     ADDED_OBJ ad, *adp = NULL;
     ASN1_OBJECT ob;
 
-    if (n == NID_undef)
-        return NULL;
-    if (n >= 0 && n < NUM_NID && nid_objs[n].nid != NID_undef)
+    if (n == NID_undef
+        || (n > 0 && n < NUM_NID && nid_objs[n].nid != NID_undef))
         return (ASN1_OBJECT *)&(nid_objs[n]);
 
     ad.type = ADDED_NID;
index d97ca92129ffc40a3b43c25643df36c4b9a054ec..f91e21cb545063cdb0f2eeaddeaab5f069c48454 100644 (file)
@@ -254,6 +254,16 @@ static int test_obj_create(void)
     return 1;
 }
 
+static int test_obj_nid_undef(void)
+{
+    if (!TEST_ptr(OBJ_nid2obj(NID_undef))
+        || !TEST_ptr(OBJ_nid2sn(NID_undef))
+        || !TEST_ptr(OBJ_nid2ln(NID_undef)))
+        return 0;
+
+    return 1;
+}
+
 int setup_tests(void)
 {
     ADD_TEST(test_tbl_standard);
@@ -261,5 +271,6 @@ int setup_tests(void)
     ADD_TEST(test_empty_nonoptional_content);
     ADD_TEST(test_unicode_range);
     ADD_TEST(test_obj_create);
+    ADD_TEST(test_obj_nid_undef);
     return 1;
 }