2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
14 #include <openssl/crypto.h>
15 #include "internal/property.h"
16 #include "crypto/ctype.h"
17 #include <openssl/lhash.h>
18 #include <openssl/rand.h>
19 #include "internal/thread_once.h"
20 #include "crypto/lhash.h"
21 #include "crypto/sparse_array.h"
22 #include "property_lcl.h"
24 /* The number of elements in the query cache before we initiate a flush */
25 #define IMPL_CACHE_FLUSH_THRESHOLD 500
28 const OSSL_PROVIDER *provider;
29 OSSL_PROPERTY_LIST *properties;
31 void (*method_destruct)(void *);
34 DEFINE_STACK_OF(IMPLEMENTATION)
42 DEFINE_LHASH_OF(QUERY);
46 STACK_OF(IMPLEMENTATION) *impls;
47 LHASH_OF(QUERY) *cache;
50 struct ossl_method_store_st {
53 SPARSE_ARRAY_OF(ALGORITHM) *algs;
54 OSSL_PROPERTY_LIST *global_properties;
57 unsigned char rand_bits[(IMPL_CACHE_FLUSH_THRESHOLD + 7) / 8];
62 LHASH_OF(QUERY) *cache;
67 DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
69 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
70 static void ossl_method_cache_flush_all(OSSL_METHOD_STORE *c);
72 int ossl_property_read_lock(OSSL_METHOD_STORE *p)
74 return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
77 int ossl_property_write_lock(OSSL_METHOD_STORE *p)
79 return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
82 int ossl_property_unlock(OSSL_METHOD_STORE *p)
84 return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
87 static unsigned long query_hash(const QUERY *a)
89 return OPENSSL_LH_strhash(a->query);
92 static int query_cmp(const QUERY *a, const QUERY *b)
94 return strcmp(a->query, b->query);
97 static void impl_free(IMPLEMENTATION *impl)
100 if (impl->method_destruct)
101 impl->method_destruct(impl->method);
106 static void impl_cache_free(QUERY *elem)
111 static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a)
114 sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
115 lh_QUERY_doall(a->cache, &impl_cache_free);
116 lh_QUERY_free(a->cache);
122 * The OPENSSL_CTX param here allows access to underlying property data needed
125 OSSL_METHOD_STORE *ossl_method_store_new(OPENSSL_CTX *ctx)
127 OSSL_METHOD_STORE *res;
129 res = OPENSSL_zalloc(sizeof(*res));
132 if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL) {
136 if ((res->lock = CRYPTO_THREAD_lock_new()) == NULL) {
137 OPENSSL_free(res->algs);
145 void ossl_method_store_free(OSSL_METHOD_STORE *store)
148 ossl_sa_ALGORITHM_doall(store->algs, &alg_cleanup);
149 ossl_sa_ALGORITHM_free(store->algs);
150 ossl_property_free(store->global_properties);
151 CRYPTO_THREAD_lock_free(store->lock);
156 static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
158 return ossl_sa_ALGORITHM_get(store->algs, nid);
161 static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
163 return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
166 int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
167 int nid, const char *properties, void *method,
168 int (*method_up_ref)(void *),
169 void (*method_destruct)(void *))
171 ALGORITHM *alg = NULL;
172 IMPLEMENTATION *impl;
176 if (nid <= 0 || method == NULL || store == NULL)
178 if (properties == NULL)
181 /* Create new entry */
182 impl = OPENSSL_malloc(sizeof(*impl));
185 if (method_up_ref != NULL && !method_up_ref(method)) {
189 impl->provider = prov;
190 impl->method = method;
191 impl->method_destruct = method_destruct;
194 * Insert into the hash table if required.
196 * A write lock is used unconditionally because we wend our way down to the
197 * property string code which isn't locking friendly.
199 ossl_property_write_lock(store);
200 ossl_method_cache_flush(store, nid);
201 if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
202 impl->properties = ossl_parse_property(store->ctx, properties);
203 if (impl->properties == NULL)
205 ossl_prop_defn_set(store->ctx, properties, impl->properties);
208 alg = ossl_method_store_retrieve(store, nid);
210 if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
211 || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
212 || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
215 if (!ossl_method_store_insert(store, alg))
219 /* Push onto stack if there isn't one there already */
220 for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
221 const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);
223 if (tmpimpl->provider == impl->provider
224 && tmpimpl->properties == impl->properties)
227 if (i == sk_IMPLEMENTATION_num(alg->impls)
228 && sk_IMPLEMENTATION_push(alg->impls, impl))
230 ossl_property_unlock(store);
236 ossl_property_unlock(store);
242 int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
245 ALGORITHM *alg = NULL;
248 if (nid <= 0 || method == NULL || store == NULL)
251 ossl_property_write_lock(store);
252 ossl_method_cache_flush(store, nid);
253 alg = ossl_method_store_retrieve(store, nid);
255 ossl_property_unlock(store);
260 * A sorting find then a delete could be faster but these stacks should be
261 * relatively small, so we avoid the overhead. Sorting could also surprise
262 * users when result orderings change (even though they are not guaranteed).
264 for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
265 IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
267 if (impl->method == method) {
268 sk_IMPLEMENTATION_delete(alg->impls, i);
269 ossl_property_unlock(store);
274 ossl_property_unlock(store);
278 int ossl_method_store_fetch(OSSL_METHOD_STORE *store, int nid,
279 const char *prop_query, void **method)
282 IMPLEMENTATION *impl;
283 OSSL_PROPERTY_LIST *pq = NULL, *p2;
285 int j, best = -1, score, optional;
288 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
291 if (nid <= 0 || method == NULL || store == NULL)
295 * This only needs to be a read lock, because queries never create property
296 * names or value and thus don't modify any of the property string layer.
298 ossl_property_read_lock(store);
299 alg = ossl_method_store_retrieve(store, nid);
301 ossl_property_unlock(store);
305 if (prop_query == NULL) {
306 if ((impl = sk_IMPLEMENTATION_value(alg->impls, 0)) != NULL) {
307 *method = impl->method;
312 pq = ossl_parse_query(store->ctx, prop_query);
315 if (store->global_properties != NULL) {
316 p2 = ossl_property_merge(pq, store->global_properties);
319 ossl_property_free(pq);
322 optional = ossl_property_has_optional(pq);
323 for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
324 impl = sk_IMPLEMENTATION_value(alg->impls, j);
325 score = ossl_property_match_count(pq, impl->properties);
327 *method = impl->method;
335 ossl_property_unlock(store);
336 ossl_property_free(pq);
340 int ossl_method_store_set_global_properties(OSSL_METHOD_STORE *store,
341 const char *prop_query) {
347 ossl_property_write_lock(store);
348 ossl_method_cache_flush_all(store);
349 if (prop_query == NULL) {
350 ossl_property_free(store->global_properties);
351 store->global_properties = NULL;
352 ossl_property_unlock(store);
355 store->global_properties = ossl_parse_query(store->ctx, prop_query);
356 ret = store->global_properties != NULL;
357 ossl_property_unlock(store);
361 static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)
363 lh_QUERY_doall(alg->cache, &impl_cache_free);
364 lh_QUERY_flush(alg->cache);
367 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
369 ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
372 store->nelem -= lh_QUERY_num_items(alg->cache);
373 impl_cache_flush_alg(0, alg);
377 static void ossl_method_cache_flush_all(OSSL_METHOD_STORE *store)
379 ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
383 IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
386 * Flush an element from the query cache (perhaps).
388 * In order to avoid taking a write lock or using atomic operations
389 * to keep accurate least recently used (LRU) or least frequently used
390 * (LFU) information, the procedure used here is to stochastically
391 * flush approximately half the cache.
393 * This procedure isn't ideal, LRU or LFU would be better. However,
394 * in normal operation, reaching a full cache would be unexpected.
395 * It means that no steady state of algorithm queries has been reached.
396 * That is, it is most likely an attack of some form. A suboptimal clearance
397 * strategy that doesn't degrade performance of the normal case is
398 * preferable to a more refined approach that imposes a performance
401 static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
406 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
407 * https://doi.org/10.18637/jss.v008.i14
409 * This is a very fast PRNG so there is no need to extract bits one at a
410 * time and use the entire value each time.
419 OPENSSL_free(lh_QUERY_delete(state->cache, c));
424 static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
427 IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
429 state->cache = alg->cache;
430 lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
434 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
436 IMPL_CACHE_FLUSH state;
439 if ((state.seed = OPENSSL_rdtsc()) == 0)
441 store->need_flush = 0;
442 ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
443 store->nelem = state.nelem;
446 int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, int nid,
447 const char *prop_query, void **method)
452 if (nid <= 0 || store == NULL)
455 ossl_property_read_lock(store);
456 alg = ossl_method_store_retrieve(store, nid);
458 ossl_property_unlock(store);
462 elem.query = prop_query != NULL ? prop_query : "";
463 r = lh_QUERY_retrieve(alg->cache, &elem);
465 ossl_property_unlock(store);
469 ossl_property_unlock(store);
473 int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, int nid,
474 const char *prop_query, void *method)
476 QUERY elem, *old, *p = NULL;
480 if (nid <= 0 || store == NULL)
482 if (prop_query == NULL)
485 ossl_property_write_lock(store);
486 if (store->need_flush)
487 ossl_method_cache_flush_some(store);
488 alg = ossl_method_store_retrieve(store, nid);
490 ossl_property_unlock(store);
494 if (method == NULL) {
495 elem.query = prop_query;
496 if (lh_QUERY_delete(alg->cache, &elem) != NULL)
498 ossl_property_unlock(store);
501 p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
505 memcpy((char *)p->query, prop_query, len + 1);
506 if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
508 ossl_property_unlock(store);
511 if (!lh_QUERY_error(alg->cache)) {
512 if (++store->nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
513 store->need_flush = 1;
514 ossl_property_unlock(store);
518 ossl_property_unlock(store);