diff options
| author | tb <> | 2023-12-13 23:28:47 +0000 |
|---|---|---|
| committer | tb <> | 2023-12-13 23:28:47 +0000 |
| commit | 5485638c7c81842ea6a037084beba5d59aec9ba4 (patch) | |
| tree | faf01ccc412436a5a29e820afbb97b6c664000be /src/lib/libcrypto/objects/obj_dat.c | |
| parent | 6754a5670621a3f0b1bd0eda9d4f1a191a4cd016 (diff) | |
| download | openbsd-5485638c7c81842ea6a037084beba5d59aec9ba4.tar.gz openbsd-5485638c7c81842ea6a037084beba5d59aec9ba4.tar.bz2 openbsd-5485638c7c81842ea6a037084beba5d59aec9ba4.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/lib/libcrypto/objects/obj_dat.c')
| -rw-r--r-- | src/lib/libcrypto/objects/obj_dat.c | 67 |
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 | |||
| 80 | static int ln_cmp_BSEARCH_CMP_FN(const void *, const void *); | 81 | static int ln_cmp_BSEARCH_CMP_FN(const void *, const void *); |
| 81 | static int ln_cmp(const ASN1_OBJECT * const *, unsigned int const *); | 82 | static int ln_cmp(const ASN1_OBJECT * const *, unsigned int const *); |
| 82 | static unsigned int *OBJ_bsearch_ln(const ASN1_OBJECT * *key, unsigned int const *base, int num); | 83 | static unsigned int *OBJ_bsearch_ln(const ASN1_OBJECT * *key, unsigned int const *base, int num); |
| 83 | static int obj_cmp_BSEARCH_CMP_FN(const void *, const void *); | ||
| 84 | static int obj_cmp(const ASN1_OBJECT * const *, unsigned int const *); | ||
| 85 | static 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) | |||
| 417 | LCRYPTO_ALIAS(OBJ_nid2ln); | 415 | LCRYPTO_ALIAS(OBJ_nid2ln); |
| 418 | 416 | ||
| 419 | static int | 417 | static int |
| 420 | obj_cmp(const ASN1_OBJECT * const *ap, const unsigned int *bp) | 418 | obj_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 | |||
| 428 | static int | ||
| 429 | obj_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 | |||
| 436 | static unsigned int * | ||
| 437 | OBJ_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 | ||
| 443 | int | 425 | int |
| 444 | OBJ_obj2nid(const ASN1_OBJECT *a) | 426 | OBJ_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 | } |
| 466 | LCRYPTO_ALIAS(OBJ_obj2nid); | 455 | LCRYPTO_ALIAS(OBJ_obj2nid); |
| 467 | 456 | ||
