From f4a64515fca92f110ff043f56f80c410ac47cf3e Mon Sep 17 00:00:00 2001 From: tb Date: Wed, 13 Dec 2023 23:28:47 +0000 Subject: [PATCH] Simplify OBJ_obj2nid() Continue with OBJ_bsearch_() elimination. OBJ_obj2nid() first checks if the object identifier passed in has a nid and if so, it returns that. Otherwise, it looks into the global hash of added objects (of course without locking) for a match and then returns the nid thereof. As a last attempt, it searches the table of built-in object identifiers. The last two steps can be cleaned up and simplified quite a bit by using C99 initializers, bsearch() and an appropriate comparison function. Then it becomes obvious that bsearch() already returns a pointer to the nid we're looking for, so there is no point in converting that into its corresponding obj and returning the nid thereof. ok jsing --- lib/libcrypto/objects/obj_dat.c | 67 ++++++++++++++------------------- 1 file changed, 28 insertions(+), 39 deletions(-) diff --git a/lib/libcrypto/objects/obj_dat.c b/lib/libcrypto/objects/obj_dat.c index f2a6515b273..76e7b22fb84 100644 --- a/lib/libcrypto/objects/obj_dat.c +++ b/lib/libcrypto/objects/obj_dat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: obj_dat.c,v 1.62 2023/11/27 11:52:32 tb Exp $ */ +/* $OpenBSD: obj_dat.c,v 1.63 2023/12/13 23:28:47 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -59,6 +59,7 @@ #include #include #include +#include #include #include @@ -80,9 +81,6 @@ static unsigned int *OBJ_bsearch_sn(const ASN1_OBJECT * *key, unsigned int const static int ln_cmp_BSEARCH_CMP_FN(const void *, const void *); static int ln_cmp(const ASN1_OBJECT * const *, unsigned int const *); static unsigned int *OBJ_bsearch_ln(const ASN1_OBJECT * *key, unsigned int const *base, int num); -static int obj_cmp_BSEARCH_CMP_FN(const void *, const void *); -static int obj_cmp(const ASN1_OBJECT * const *, unsigned int const *); -static unsigned int *OBJ_bsearch_obj(const ASN1_OBJECT * *key, unsigned int const *base, int num); #define ADDED_DATA 0 #define ADDED_SNAME 1 @@ -417,51 +415,42 @@ OBJ_nid2ln(int n) LCRYPTO_ALIAS(OBJ_nid2ln); static int -obj_cmp(const ASN1_OBJECT * const *ap, const unsigned int *bp) +obj_objs_cmp(const void *aobj, const void *b) { - const ASN1_OBJECT *a = *ap; - const ASN1_OBJECT *b = &nid_objs[*bp]; + const unsigned int *nid = b; - return OBJ_cmp(a, b); -} - -static int -obj_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_) -{ - const ASN1_OBJECT * const *a = a_; - unsigned int const *b = b_; - return obj_cmp(a, b); -} - -static unsigned int * -OBJ_bsearch_obj(const ASN1_OBJECT * *key, unsigned int const *base, int num) -{ - return (unsigned int *)OBJ_bsearch_(key, base, num, sizeof(unsigned int), - obj_cmp_BSEARCH_CMP_FN); + return OBJ_cmp(aobj, &nid_objs[*nid]); } int -OBJ_obj2nid(const ASN1_OBJECT *a) +OBJ_obj2nid(const ASN1_OBJECT *aobj) { - const unsigned int *op; - ADDED_OBJ ad, *adp; + const unsigned int *nid; - if (a == NULL || a->length == 0) - return (NID_undef); - if (a->nid != NID_undef) - return (a->nid); + if (aobj == NULL || aobj->length == 0) + return NID_undef; + + if (aobj->nid != NID_undef) + return aobj->nid; + /* XXX - locking. OpenSSL 3 moved this after built-in object lookup. */ if (added != NULL) { - ad.type = ADDED_DATA; - ad.obj=(ASN1_OBJECT *)a; /* XXX: ugly but harmless */ - adp = lh_ADDED_OBJ_retrieve(added, &ad); - if (adp != NULL) - return (adp->obj->nid); + ADDED_OBJ needle = { + .type = ADDED_DATA, + .obj = (ASN1_OBJECT *)aobj, + }; + ADDED_OBJ *found; + + if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL) + return found->obj->nid; } - op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ); - if (op == NULL) - return (NID_undef); - return (nid_objs[*op].nid); + + /* obj_objs holds built-in obj NIDs in ascending OBJ_cmp() order. */ + nid = bsearch(aobj, obj_objs, NUM_OBJ, sizeof(unsigned int), obj_objs_cmp); + if (nid != NULL) + return *nid; + + return NID_undef; } LCRYPTO_ALIAS(OBJ_obj2nid); -- 2.20.1