1 /* $OpenBSD: x509_issuer_cache.c,v 1.1 2020/09/11 14:30:51 beck Exp $ */
3 * Copyright (c) 2020 Bob Beck <beck@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 /* x509_issuer_cache */
21 * The issuer cache is a cache of parent and child x509 certificate
22 * hashes with a signature validation result.
24 * Entries should only be added to the cache with a validation result
25 * from checking the public key math that "parent" signed "child".
27 * Finding an entry in the cache gets us the result of a previously
28 * performed validation of the signature of "parent" signing for the
29 * validity of "child". It allows us to skip doing the public key math
30 * when validating a certificate chain. It does not allow us to skip
31 * any other steps of validation (times, names, key usage, etc.)
37 #include "x509_issuer_cache.h"
40 x509_issuer_cmp(struct x509_issuer *x1, struct x509_issuer *x2)
43 if ((pcmp = memcmp(x1->parent_md, x2->parent_md, EVP_MAX_MD_SIZE)) != 0)
45 return memcmp(x1->child_md, x2->child_md, EVP_MAX_MD_SIZE);
48 static size_t x509_issuer_cache_count;
49 static size_t x509_issuer_cache_max = X509_ISSUER_CACHE_MAX;
50 static RB_HEAD(x509_issuer_tree, x509_issuer) x509_issuer_cache =
51 RB_INITIALIZER(&x509_issuer_cache);
52 static TAILQ_HEAD(lruqueue, x509_issuer) x509_issuer_lru =
53 TAILQ_HEAD_INITIALIZER(x509_issuer_lru);
54 static pthread_mutex_t x509_issuer_tree_mutex = PTHREAD_MUTEX_INITIALIZER;
56 RB_PROTOTYPE(x509_issuer_tree, x509_issuer, entry, x509_issuer_cmp);
57 RB_GENERATE(x509_issuer_tree, x509_issuer, entry, x509_issuer_cmp);
60 * Set the maximum number of cached entries. On additions to the cache
61 * the least recently used entries will be discarded so that the cache
62 * stays under the maximum number of entries. Setting a maximum of 0
66 x509_issuer_cache_set_max(size_t max)
68 if (pthread_mutex_lock(&x509_issuer_tree_mutex) != 0)
70 x509_issuer_cache_max = max;
71 (void) pthread_mutex_unlock(&x509_issuer_tree_mutex);
77 * Find a previous result of checking if parent signed child
80 * -1 : No entry exists in the cache. signature must be checked.
81 * 0 : The signature of parent signing child is invalid.
82 * 1 : The signature of parent signing child is valid.
85 x509_issuer_cache_find(unsigned char *parent_md, unsigned char *child_md)
87 struct x509_issuer candidate, *found;
90 memset(&candidate, 0, sizeof(candidate));
91 candidate.parent_md = parent_md;
92 candidate.child_md = child_md;
94 if (x509_issuer_cache_max == 0)
97 if (pthread_mutex_lock(&x509_issuer_tree_mutex) != 0)
99 if ((found = RB_FIND(x509_issuer_tree, &x509_issuer_cache,
100 &candidate)) != NULL) {
101 TAILQ_REMOVE(&x509_issuer_lru, found, queue);
102 TAILQ_INSERT_HEAD(&x509_issuer_lru, found, queue);
105 (void) pthread_mutex_unlock(&x509_issuer_tree_mutex);
111 * Attempt to add a validation result to the cache.
114 * 0: The signature of parent signing child is invalid.
115 * 1: The signature of parent signing child is valid.
117 * Previously added entries for the same parent and child are *not* replaced.
120 x509_issuer_cache_add(unsigned char *parent_md, unsigned char *child_md,
123 struct x509_issuer *new;
125 if (x509_issuer_cache_max == 0)
127 if (valid != 0 && valid != 1)
130 if ((new = calloc(1, sizeof(struct x509_issuer))) == NULL)
132 if ((new->parent_md = calloc(1, EVP_MAX_MD_SIZE)) == NULL)
134 memcpy(new->parent_md, parent_md, EVP_MAX_MD_SIZE);
135 if ((new->child_md = calloc(1, EVP_MAX_MD_SIZE)) == NULL)
137 memcpy(new->child_md, child_md, EVP_MAX_MD_SIZE);
141 if (pthread_mutex_lock(&x509_issuer_tree_mutex) != 0)
143 while (x509_issuer_cache_count >= x509_issuer_cache_max) {
144 struct x509_issuer *old;
145 if ((old = TAILQ_LAST(&x509_issuer_lru, lruqueue)) == NULL)
147 TAILQ_REMOVE(&x509_issuer_lru, old, queue);
148 RB_REMOVE(x509_issuer_tree, &x509_issuer_cache, old);
149 free(old->parent_md);
152 x509_issuer_cache_count--;
154 if (RB_INSERT(x509_issuer_tree, &x509_issuer_cache, new) == NULL) {
155 TAILQ_INSERT_HEAD(&x509_issuer_lru, new, queue);
156 x509_issuer_cache_count++;
160 (void) pthread_mutex_unlock(&x509_issuer_tree_mutex);
162 free(new->parent_md);