2 * Copyright 2019-2023 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/core.h"
16 #include "internal/property.h"
17 #include "internal/provider.h"
18 #include "internal/tsan_assist.h"
19 #include "crypto/ctype.h"
20 #include <openssl/lhash.h>
21 #include <openssl/rand.h>
22 #include "internal/thread_once.h"
23 #include "crypto/lhash.h"
24 #include "crypto/sparse_array.h"
25 #include "property_local.h"
26 #include "crypto/context.h"
29 * The number of elements in the query cache before we initiate a flush.
30 * If reducing this, also ensure the stochastic test in test/property_test.c
31 * isn't likely to fail.
33 #define IMPL_CACHE_FLUSH_THRESHOLD 500
37 int (*up_ref)(void *);
42 const OSSL_PROVIDER *provider;
43 OSSL_PROPERTY_LIST *properties;
47 DEFINE_STACK_OF(IMPLEMENTATION)
50 const OSSL_PROVIDER *provider;
56 DEFINE_LHASH_OF_EX(QUERY);
60 STACK_OF(IMPLEMENTATION) *impls;
61 LHASH_OF(QUERY) *cache;
64 struct ossl_method_store_st {
66 SPARSE_ARRAY_OF(ALGORITHM) *algs;
68 * Lock to protect the |algs| array from concurrent writing, when
69 * individual implementations or queries are inserted. This is used
70 * by the appropriate functions here.
74 * Lock to reserve the whole store. This is used when fetching a set
75 * of algorithms, via these functions, found in crypto/core_fetch.c:
76 * ossl_method_construct_reserve_store()
77 * ossl_method_construct_unreserve_store()
79 CRYPTO_RWLOCK *biglock;
81 /* query cache specific values */
83 /* Count of the query cache entries for all algs */
86 /* Flag: 1 if query cache entries for all algs need flushing */
91 LHASH_OF(QUERY) *cache;
94 unsigned char using_global_seed;
97 DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
99 typedef struct ossl_global_properties_st {
100 OSSL_PROPERTY_LIST *list;
102 unsigned int no_mirrored : 1;
104 } OSSL_GLOBAL_PROPERTIES;
106 static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
108 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
110 /* Global properties are stored per library context */
111 void ossl_ctx_global_properties_free(void *vglobp)
113 OSSL_GLOBAL_PROPERTIES *globp = vglobp;
116 ossl_property_free(globp->list);
121 void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)
123 return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES));
126 OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
127 ossl_unused int loadconfig)
129 OSSL_GLOBAL_PROPERTIES *globp;
131 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
132 if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
135 globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
137 return globp != NULL ? &globp->list : NULL;
141 int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx)
143 OSSL_GLOBAL_PROPERTIES *globp
144 = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
146 return globp != NULL && globp->no_mirrored ? 1 : 0;
149 void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx)
151 OSSL_GLOBAL_PROPERTIES *globp
152 = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
155 globp->no_mirrored = 1;
159 static int ossl_method_up_ref(METHOD *method)
161 return (*method->up_ref)(method->method);
164 static void ossl_method_free(METHOD *method)
166 (*method->free)(method->method);
169 static __owur int ossl_property_read_lock(OSSL_METHOD_STORE *p)
171 return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
174 static __owur int ossl_property_write_lock(OSSL_METHOD_STORE *p)
176 return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
179 static int ossl_property_unlock(OSSL_METHOD_STORE *p)
181 return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
184 static unsigned long query_hash(const QUERY *a)
186 return OPENSSL_LH_strhash(a->query);
189 static int query_cmp(const QUERY *a, const QUERY *b)
191 int res = strcmp(a->query, b->query);
193 if (res == 0 && a->provider != NULL && b->provider != NULL)
194 res = b->provider > a->provider ? 1
195 : b->provider < a->provider ? -1
200 static void impl_free(IMPLEMENTATION *impl)
203 ossl_method_free(&impl->method);
208 static void impl_cache_free(QUERY *elem)
211 ossl_method_free(&elem->method);
216 static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)
218 lh_QUERY_doall(alg->cache, &impl_cache_free);
219 lh_QUERY_flush(alg->cache);
222 static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a, void *arg)
224 OSSL_METHOD_STORE *store = arg;
227 sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
228 lh_QUERY_doall(a->cache, &impl_cache_free);
229 lh_QUERY_free(a->cache);
233 ossl_sa_ALGORITHM_set(store->algs, idx, NULL);
237 * The OSSL_LIB_CTX param here allows access to underlying property data needed
240 OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx)
242 OSSL_METHOD_STORE *res;
244 res = OPENSSL_zalloc(sizeof(*res));
247 if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL
248 || (res->lock = CRYPTO_THREAD_lock_new()) == NULL
249 || (res->biglock = CRYPTO_THREAD_lock_new()) == NULL) {
250 ossl_method_store_free(res);
257 void ossl_method_store_free(OSSL_METHOD_STORE *store)
260 if (store->algs != NULL)
261 ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup, store);
262 ossl_sa_ALGORITHM_free(store->algs);
263 CRYPTO_THREAD_lock_free(store->lock);
264 CRYPTO_THREAD_lock_free(store->biglock);
269 int ossl_method_lock_store(OSSL_METHOD_STORE *store)
271 return store != NULL ? CRYPTO_THREAD_write_lock(store->biglock) : 0;
274 int ossl_method_unlock_store(OSSL_METHOD_STORE *store)
276 return store != NULL ? CRYPTO_THREAD_unlock(store->biglock) : 0;
279 static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
281 return ossl_sa_ALGORITHM_get(store->algs, nid);
284 static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
286 return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
289 int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
290 int nid, const char *properties, void *method,
291 int (*method_up_ref)(void *),
292 void (*method_destruct)(void *))
294 ALGORITHM *alg = NULL;
295 IMPLEMENTATION *impl;
299 if (nid <= 0 || method == NULL || store == NULL)
301 if (properties == NULL)
304 if (!ossl_assert(prov != NULL))
307 /* Create new entry */
308 impl = OPENSSL_malloc(sizeof(*impl));
311 impl->method.method = method;
312 impl->method.up_ref = method_up_ref;
313 impl->method.free = method_destruct;
314 if (!ossl_method_up_ref(&impl->method)) {
318 impl->provider = prov;
320 /* Insert into the hash table if required */
321 if (!ossl_property_write_lock(store)) {
325 ossl_method_cache_flush(store, nid);
326 if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
327 impl->properties = ossl_parse_property(store->ctx, properties);
328 if (impl->properties == NULL)
330 if (!ossl_prop_defn_set(store->ctx, properties, &impl->properties)) {
331 ossl_property_free(impl->properties);
332 impl->properties = NULL;
337 alg = ossl_method_store_retrieve(store, nid);
339 if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
340 || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
341 || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
344 if (!ossl_method_store_insert(store, alg))
348 /* Push onto stack if there isn't one there already */
349 for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
350 const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);
352 if (tmpimpl->provider == impl->provider
353 && tmpimpl->properties == impl->properties)
356 if (i == sk_IMPLEMENTATION_num(alg->impls)
357 && sk_IMPLEMENTATION_push(alg->impls, impl))
359 ossl_property_unlock(store);
365 ossl_property_unlock(store);
366 alg_cleanup(0, alg, NULL);
371 int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
374 ALGORITHM *alg = NULL;
377 if (nid <= 0 || method == NULL || store == NULL)
380 if (!ossl_property_write_lock(store))
382 ossl_method_cache_flush(store, nid);
383 alg = ossl_method_store_retrieve(store, nid);
385 ossl_property_unlock(store);
390 * A sorting find then a delete could be faster but these stacks should be
391 * relatively small, so we avoid the overhead. Sorting could also surprise
392 * users when result orderings change (even though they are not guaranteed).
394 for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
395 IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
397 if (impl->method.method == method) {
399 (void)sk_IMPLEMENTATION_delete(alg->impls, i);
400 ossl_property_unlock(store);
404 ossl_property_unlock(store);
408 struct alg_cleanup_by_provider_data_st {
409 OSSL_METHOD_STORE *store;
410 const OSSL_PROVIDER *prov;
414 alg_cleanup_by_provider(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
416 struct alg_cleanup_by_provider_data_st *data = arg;
420 * We walk the stack backwards, to avoid having to deal with stack shifts
423 for (count = 0, i = sk_IMPLEMENTATION_num(alg->impls); i-- > 0;) {
424 IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
426 if (impl->provider == data->prov) {
428 (void)sk_IMPLEMENTATION_delete(alg->impls, i);
434 * If we removed any implementation, we also clear the whole associated
435 * cache, 'cause that's the sensible thing to do.
436 * There's no point flushing the cache entries where we didn't remove
437 * any implementation, though.
440 ossl_method_cache_flush_alg(data->store, alg);
443 int ossl_method_store_remove_all_provided(OSSL_METHOD_STORE *store,
444 const OSSL_PROVIDER *prov)
446 struct alg_cleanup_by_provider_data_st data;
448 if (!ossl_property_write_lock(store))
452 ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup_by_provider, &data);
453 ossl_property_unlock(store);
457 static void alg_do_one(ALGORITHM *alg, IMPLEMENTATION *impl,
458 void (*fn)(int id, void *method, void *fnarg),
461 fn(alg->nid, impl->method.method, fnarg);
464 struct alg_do_each_data_st {
465 void (*fn)(int id, void *method, void *fnarg);
469 static void alg_do_each(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
471 struct alg_do_each_data_st *data = arg;
472 int i, end = sk_IMPLEMENTATION_num(alg->impls);
474 for (i = 0; i < end; i++) {
475 IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
477 alg_do_one(alg, impl, data->fn, data->fnarg);
481 void ossl_method_store_do_all(OSSL_METHOD_STORE *store,
482 void (*fn)(int id, void *method, void *fnarg),
485 struct alg_do_each_data_st data;
490 ossl_sa_ALGORITHM_doall_arg(store->algs, alg_do_each, &data);
493 int ossl_method_store_fetch(OSSL_METHOD_STORE *store,
494 int nid, const char *prop_query,
495 const OSSL_PROVIDER **prov_rw, void **method)
497 OSSL_PROPERTY_LIST **plp;
499 IMPLEMENTATION *impl, *best_impl = NULL;
500 OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;
501 const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL;
503 int j, best = -1, score, optional;
505 if (nid <= 0 || method == NULL || store == NULL)
508 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
509 if (ossl_lib_ctx_is_default(store->ctx)
510 && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
514 /* This only needs to be a read lock, because the query won't create anything */
515 if (!ossl_property_read_lock(store))
517 alg = ossl_method_store_retrieve(store, nid);
519 ossl_property_unlock(store);
523 if (prop_query != NULL)
524 p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);
525 plp = ossl_ctx_global_properties(store->ctx, 0);
526 if (plp != NULL && *plp != NULL) {
530 p2 = ossl_property_merge(pq, *plp);
531 ossl_property_free(pq);
539 for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
540 if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
541 && (prov == NULL || impl->provider == prov)) {
549 optional = ossl_property_has_optional(pq);
550 for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
551 if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
552 && (prov == NULL || impl->provider == prov)) {
553 score = ossl_property_match_count(pq, impl->properties);
564 if (ret && ossl_method_up_ref(&best_impl->method)) {
565 *method = best_impl->method.method;
567 *prov_rw = best_impl->provider;
571 ossl_property_unlock(store);
572 ossl_property_free(p2);
576 static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
579 store->cache_nelem -= lh_QUERY_num_items(alg->cache);
580 impl_cache_flush_alg(0, alg);
583 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
585 ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
588 ossl_method_cache_flush_alg(store, alg);
591 int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store)
593 if (!ossl_property_write_lock(store))
595 ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
596 store->cache_nelem = 0;
597 ossl_property_unlock(store);
601 IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
604 * Flush an element from the query cache (perhaps).
606 * In order to avoid taking a write lock or using atomic operations
607 * to keep accurate least recently used (LRU) or least frequently used
608 * (LFU) information, the procedure used here is to stochastically
609 * flush approximately half the cache.
611 * This procedure isn't ideal, LRU or LFU would be better. However,
612 * in normal operation, reaching a full cache would be unexpected.
613 * It means that no steady state of algorithm queries has been reached.
614 * That is, it is most likely an attack of some form. A suboptimal clearance
615 * strategy that doesn't degrade performance of the normal case is
616 * preferable to a more refined approach that imposes a performance
619 static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
624 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
625 * https://doi.org/10.18637/jss.v008.i14
627 * This is a very fast PRNG so there is no need to extract bits one at a
628 * time and use the entire value each time.
637 impl_cache_free(lh_QUERY_delete(state->cache, c));
642 static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
645 IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
647 state->cache = alg->cache;
648 lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
652 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
654 IMPL_CACHE_FLUSH state;
655 static TSAN_QUALIFIER uint32_t global_seed = 1;
658 state.using_global_seed = 0;
659 if ((state.seed = OPENSSL_rdtsc()) == 0) {
660 /* If there is no timer available, seed another way */
661 state.using_global_seed = 1;
662 state.seed = tsan_load(&global_seed);
664 store->cache_need_flush = 0;
665 ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
666 store->cache_nelem = state.nelem;
667 /* Without a timer, update the global seed */
668 if (state.using_global_seed)
669 tsan_add(&global_seed, state.seed);
672 int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
673 int nid, const char *prop_query, void **method)
679 if (nid <= 0 || store == NULL || prop_query == NULL)
682 if (!ossl_property_read_lock(store))
684 alg = ossl_method_store_retrieve(store, nid);
688 elem.query = prop_query;
689 elem.provider = prov;
690 r = lh_QUERY_retrieve(alg->cache, &elem);
693 if (ossl_method_up_ref(&r->method)) {
694 *method = r->method.method;
698 ossl_property_unlock(store);
702 int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
703 int nid, const char *prop_query, void *method,
704 int (*method_up_ref)(void *),
705 void (*method_destruct)(void *))
707 QUERY elem, *old, *p = NULL;
712 if (nid <= 0 || store == NULL || prop_query == NULL)
715 if (!ossl_assert(prov != NULL))
718 if (!ossl_property_write_lock(store))
720 if (store->cache_need_flush)
721 ossl_method_cache_flush_some(store);
722 alg = ossl_method_store_retrieve(store, nid);
726 if (method == NULL) {
727 elem.query = prop_query;
728 elem.provider = prov;
729 if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) {
730 impl_cache_free(old);
731 store->cache_nelem--;
735 p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
739 p->method.method = method;
740 p->method.up_ref = method_up_ref;
741 p->method.free = method_destruct;
742 if (!ossl_method_up_ref(&p->method))
744 memcpy((char *)p->query, prop_query, len + 1);
745 if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
746 impl_cache_free(old);
749 if (!lh_QUERY_error(alg->cache)) {
750 if (++store->cache_nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
751 store->cache_need_flush = 1;
754 ossl_method_free(&p->method);
760 ossl_property_unlock(store);