diff options
author | tb <> | 2021-11-06 07:18:18 +0000 |
---|---|---|
committer | tb <> | 2021-11-06 07:18:18 +0000 |
commit | 34ed574dde4b0a9e58a08c73a0508915728f250d (patch) | |
tree | 14ea0b7dbd11bc7110c0eba3c8b6c7349650ac68 /src/lib | |
parent | 0e1c20985207b7f740c6431f7e845bad416e3e29 (diff) | |
download | openbsd-34ed574dde4b0a9e58a08c73a0508915728f250d.tar.gz openbsd-34ed574dde4b0a9e58a08c73a0508915728f250d.tar.bz2 openbsd-34ed574dde4b0a9e58a08c73a0508915728f250d.zip |
Refactor X509_STORE_get1_certs()
Split the retrieval of the certs in the store's cache that match the
desired subject into a separate function. This greatly simplifies
locking, error handling and the flow of the function.
with/ok jsing
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libcrypto/x509/x509_lu.c | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/src/lib/libcrypto/x509/x509_lu.c b/src/lib/libcrypto/x509/x509_lu.c index 9c18c16eeb..1a8c079fde 100644 --- a/src/lib/libcrypto/x509/x509_lu.c +++ b/src/lib/libcrypto/x509/x509_lu.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: x509_lu.c,v 1.48 2021/11/05 21:39:45 tb Exp $ */ | 1 | /* $OpenBSD: x509_lu.c,v 1.49 2021/11/06 07:18:18 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 | * |
@@ -532,41 +532,20 @@ X509_OBJECT_get0_X509_CRL(X509_OBJECT *xo) | |||
532 | return NULL; | 532 | return NULL; |
533 | } | 533 | } |
534 | 534 | ||
535 | STACK_OF(X509) * | 535 | static STACK_OF(X509) * |
536 | X509_STORE_get1_certs(X509_STORE_CTX *ctx, X509_NAME *name) | 536 | X509_get1_certs_from_cache(X509_STORE *store, X509_NAME *name) |
537 | { | 537 | { |
538 | X509_STORE *store = ctx->ctx; | 538 | STACK_OF(X509) *sk = NULL; |
539 | STACK_OF(X509) *sk; | ||
540 | X509 *x = NULL; | 539 | X509 *x = NULL; |
541 | X509_OBJECT *obj; | 540 | X509_OBJECT *obj; |
542 | int i, idx, cnt; | 541 | int i, idx, cnt; |
543 | 542 | ||
544 | if (store == NULL) | ||
545 | return NULL; | ||
546 | |||
547 | CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); | 543 | CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); |
548 | idx = x509_object_idx_cnt(store->objs, X509_LU_X509, name, &cnt); | ||
549 | if (idx >= 0) | ||
550 | goto found; | ||
551 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
552 | |||
553 | /* Nothing found: do lookup to possibly add new objects to cache. */ | ||
554 | obj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509, name); | ||
555 | if (obj == NULL) | ||
556 | return NULL; | ||
557 | |||
558 | X509_OBJECT_free(obj); | ||
559 | obj = NULL; | ||
560 | 544 | ||
561 | CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); | ||
562 | idx = x509_object_idx_cnt(store->objs, X509_LU_X509, name, &cnt); | 545 | idx = x509_object_idx_cnt(store->objs, X509_LU_X509, name, &cnt); |
563 | if (idx >= 0) | 546 | if (idx < 0) |
564 | goto found; | 547 | goto err; |
565 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
566 | |||
567 | return NULL; | ||
568 | 548 | ||
569 | found: | ||
570 | if ((sk = sk_X509_new_null()) == NULL) | 549 | if ((sk = sk_X509_new_null()) == NULL) |
571 | goto err; | 550 | goto err; |
572 | 551 | ||
@@ -583,15 +562,39 @@ X509_STORE_get1_certs(X509_STORE_CTX *ctx, X509_NAME *name) | |||
583 | } | 562 | } |
584 | 563 | ||
585 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | 564 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); |
565 | |||
586 | return sk; | 566 | return sk; |
587 | 567 | ||
588 | err: | 568 | err: |
589 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | 569 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); |
590 | sk_X509_pop_free(sk, X509_free); | 570 | sk_X509_pop_free(sk, X509_free); |
591 | X509_free(x); | 571 | X509_free(x); |
572 | |||
592 | return NULL; | 573 | return NULL; |
593 | } | 574 | } |
594 | 575 | ||
576 | STACK_OF(X509) * | ||
577 | X509_STORE_get1_certs(X509_STORE_CTX *ctx, X509_NAME *name) | ||
578 | { | ||
579 | X509_STORE *store = ctx->ctx; | ||
580 | STACK_OF(X509) *sk; | ||
581 | X509_OBJECT *obj; | ||
582 | |||
583 | if (store == NULL) | ||
584 | return NULL; | ||
585 | |||
586 | if ((sk = X509_get1_certs_from_cache(store, name)) != NULL) | ||
587 | return sk; | ||
588 | |||
589 | /* Nothing found: do lookup to possibly add new objects to cache. */ | ||
590 | obj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509, name); | ||
591 | if (obj == NULL) | ||
592 | return NULL; | ||
593 | X509_OBJECT_free(obj); | ||
594 | |||
595 | return X509_get1_certs_from_cache(store, name); | ||
596 | } | ||
597 | |||
595 | STACK_OF(X509_CRL) * | 598 | STACK_OF(X509_CRL) * |
596 | X509_STORE_get1_crls(X509_STORE_CTX *ctx, X509_NAME *name) | 599 | X509_STORE_get1_crls(X509_STORE_CTX *ctx, X509_NAME *name) |
597 | { | 600 | { |