summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortb <>2023-12-13 23:28:47 +0000
committertb <>2023-12-13 23:28:47 +0000
commitf9e4eb95614e9427e1bbb5caa4377d07c7fb4f0c (patch)
treefaf01ccc412436a5a29e820afbb97b6c664000be /src
parentf9e405c7b1b2503c07f69e9f3f6ffa8e43997514 (diff)
downloadopenbsd-f9e4eb95614e9427e1bbb5caa4377d07c7fb4f0c.tar.gz
openbsd-f9e4eb95614e9427e1bbb5caa4377d07c7fb4f0c.tar.bz2
openbsd-f9e4eb95614e9427e1bbb5caa4377d07c7fb4f0c.zip
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
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/objects/obj_dat.c67
1 files changed, 28 insertions, 39 deletions
diff --git a/src/lib/libcrypto/objects/obj_dat.c b/src/lib/libcrypto/objects/obj_dat.c
index f2a6515b27..76e7b22fb8 100644
--- a/src/lib/libcrypto/objects/obj_dat.c
+++ b/src/lib/libcrypto/objects/obj_dat.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: obj_dat.c,v 1.62 2023/11/27 11:52:32 tb Exp $ */ 1/* $OpenBSD: obj_dat.c,v 1.63 2023/12/13 23:28:47 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -59,6 +59,7 @@
59#include <ctype.h> 59#include <ctype.h>
60#include <limits.h> 60#include <limits.h>
61#include <stdio.h> 61#include <stdio.h>
62#include <stdlib.h>
62#include <string.h> 63#include <string.h>
63 64
64#include <openssl/opensslconf.h> 65#include <openssl/opensslconf.h>
@@ -80,9 +81,6 @@ static unsigned int *OBJ_bsearch_sn(const ASN1_OBJECT * *key, unsigned int const
80static int ln_cmp_BSEARCH_CMP_FN(const void *, const void *); 81static int ln_cmp_BSEARCH_CMP_FN(const void *, const void *);
81static int ln_cmp(const ASN1_OBJECT * const *, unsigned int const *); 82static int ln_cmp(const ASN1_OBJECT * const *, unsigned int const *);
82static unsigned int *OBJ_bsearch_ln(const ASN1_OBJECT * *key, unsigned int const *base, int num); 83static unsigned int *OBJ_bsearch_ln(const ASN1_OBJECT * *key, unsigned int const *base, int num);
83static int obj_cmp_BSEARCH_CMP_FN(const void *, const void *);
84static int obj_cmp(const ASN1_OBJECT * const *, unsigned int const *);
85static unsigned int *OBJ_bsearch_obj(const ASN1_OBJECT * *key, unsigned int const *base, int num);
86 84
87#define ADDED_DATA 0 85#define ADDED_DATA 0
88#define ADDED_SNAME 1 86#define ADDED_SNAME 1
@@ -417,51 +415,42 @@ OBJ_nid2ln(int n)
417LCRYPTO_ALIAS(OBJ_nid2ln); 415LCRYPTO_ALIAS(OBJ_nid2ln);
418 416
419static int 417static int
420obj_cmp(const ASN1_OBJECT * const *ap, const unsigned int *bp) 418obj_objs_cmp(const void *aobj, const void *b)
421{ 419{
422 const ASN1_OBJECT *a = *ap; 420 const unsigned int *nid = b;
423 const ASN1_OBJECT *b = &nid_objs[*bp];
424 421
425 return OBJ_cmp(a, b); 422 return OBJ_cmp(aobj, &nid_objs[*nid]);
426}
427
428static int
429obj_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
430{
431 const ASN1_OBJECT * const *a = a_;
432 unsigned int const *b = b_;
433 return obj_cmp(a, b);
434}
435
436static unsigned int *
437OBJ_bsearch_obj(const ASN1_OBJECT * *key, unsigned int const *base, int num)
438{
439 return (unsigned int *)OBJ_bsearch_(key, base, num, sizeof(unsigned int),
440 obj_cmp_BSEARCH_CMP_FN);
441} 423}
442 424
443int 425int
444OBJ_obj2nid(const ASN1_OBJECT *a) 426OBJ_obj2nid(const ASN1_OBJECT *aobj)
445{ 427{
446 const unsigned int *op; 428 const unsigned int *nid;
447 ADDED_OBJ ad, *adp;
448 429
449 if (a == NULL || a->length == 0) 430 if (aobj == NULL || aobj->length == 0)
450 return (NID_undef); 431 return NID_undef;
451 if (a->nid != NID_undef) 432
452 return (a->nid); 433 if (aobj->nid != NID_undef)
434 return aobj->nid;
453 435
436 /* XXX - locking. OpenSSL 3 moved this after built-in object lookup. */
454 if (added != NULL) { 437 if (added != NULL) {
455 ad.type = ADDED_DATA; 438 ADDED_OBJ needle = {
456 ad.obj=(ASN1_OBJECT *)a; /* XXX: ugly but harmless */ 439 .type = ADDED_DATA,
457 adp = lh_ADDED_OBJ_retrieve(added, &ad); 440 .obj = (ASN1_OBJECT *)aobj,
458 if (adp != NULL) 441 };
459 return (adp->obj->nid); 442 ADDED_OBJ *found;
443
444 if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL)
445 return found->obj->nid;
460 } 446 }
461 op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ); 447
462 if (op == NULL) 448 /* obj_objs holds built-in obj NIDs in ascending OBJ_cmp() order. */
463 return (NID_undef); 449 nid = bsearch(aobj, obj_objs, NUM_OBJ, sizeof(unsigned int), obj_objs_cmp);
464 return (nid_objs[*op].nid); 450 if (nid != NULL)
451 return *nid;
452
453 return NID_undef;
465} 454}
466LCRYPTO_ALIAS(OBJ_obj2nid); 455LCRYPTO_ALIAS(OBJ_obj2nid);
467 456