2 * Copyright 2019-2022 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 "crypto/ctype.h"
19 #include <openssl/lhash.h>
20 #include <openssl/rand.h>
21 #include "internal/thread_once.h"
22 #include "crypto/lhash.h"
23 #include "crypto/sparse_array.h"
24 #include "property_local.h"
25 #include "crypto/context.h"
28 * The number of elements in the query cache before we initiate a flush.
29 * If reducing this, also ensure the stochastic test in test/property_test.c
30 * isn't likely to fail.
32 #define IMPL_CACHE_FLUSH_THRESHOLD 500
36 int (*up_ref)(void *);
41 const OSSL_PROVIDER *provider;
42 OSSL_PROPERTY_LIST *properties;
46 DEFINE_STACK_OF(IMPLEMENTATION)
49 const OSSL_PROVIDER *provider;
55 DEFINE_LHASH_OF(QUERY);
59 STACK_OF(IMPLEMENTATION) *impls;
60 LHASH_OF(QUERY) *cache;
63 struct ossl_method_store_st {
65 SPARSE_ARRAY_OF(ALGORITHM) *algs;
68 /* query cache specific values */
70 /* Count of the query cache entries for all algs */
73 /* Flag: 1 if query cache entries for all algs need flushing */
78 LHASH_OF(QUERY) *cache;
83 DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
85 typedef struct ossl_global_properties_st {
86 OSSL_PROPERTY_LIST *list;
88 unsigned int no_mirrored : 1;
90 } OSSL_GLOBAL_PROPERTIES;
92 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
94 /* Global properties are stored per library context */
95 void ossl_ctx_global_properties_free(void *vglobp)
97 OSSL_GLOBAL_PROPERTIES *globp = vglobp;
100 ossl_property_free(globp->list);
105 void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)
107 return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES));
110 OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
113 OSSL_GLOBAL_PROPERTIES *globp;
116 if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
119 globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
121 return globp != NULL ? &globp->list : NULL;
125 int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx)
127 OSSL_GLOBAL_PROPERTIES *globp
128 = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
130 return globp != NULL && globp->no_mirrored ? 1 : 0;
133 void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx)
135 OSSL_GLOBAL_PROPERTIES *globp
136 = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
139 globp->no_mirrored = 1;
143 static int ossl_method_up_ref(METHOD *method)
145 return (*method->up_ref)(method->method);
148 static void ossl_method_free(METHOD *method)
150 (*method->free)(method->method);
153 static __owur int ossl_property_read_lock(OSSL_METHOD_STORE *p)
155 return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
158 static __owur int ossl_property_write_lock(OSSL_METHOD_STORE *p)
160 return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
163 static int ossl_property_unlock(OSSL_METHOD_STORE *p)
165 return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
168 static unsigned long query_hash(const QUERY *a)
170 return OPENSSL_LH_strhash(a->query);
173 static int query_cmp(const QUERY *a, const QUERY *b)
175 int res = strcmp(a->query, b->query);
177 if (res == 0 && a->provider != NULL && b->provider != NULL)
178 res = b->provider > a->provider ? 1
179 : b->provider < a->provider ? -1
184 static void impl_free(IMPLEMENTATION *impl)
187 ossl_method_free(&impl->method);
192 static void impl_cache_free(QUERY *elem)
195 ossl_method_free(&elem->method);
200 static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a)
203 sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
204 lh_QUERY_doall(a->cache, &impl_cache_free);
205 lh_QUERY_free(a->cache);
211 * The OSSL_LIB_CTX param here allows access to underlying property data needed
214 OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx)
216 OSSL_METHOD_STORE *res;
218 res = OPENSSL_zalloc(sizeof(*res));
221 if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL) {
225 if ((res->lock = CRYPTO_THREAD_lock_new()) == NULL) {
226 ossl_sa_ALGORITHM_free(res->algs);
234 void ossl_method_store_free(OSSL_METHOD_STORE *store)
237 ossl_sa_ALGORITHM_doall(store->algs, &alg_cleanup);
238 ossl_sa_ALGORITHM_free(store->algs);
239 CRYPTO_THREAD_lock_free(store->lock);
244 static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
246 return ossl_sa_ALGORITHM_get(store->algs, nid);
249 static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
251 return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
254 int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
255 int nid, const char *properties, void *method,
256 int (*method_up_ref)(void *),
257 void (*method_destruct)(void *))
259 ALGORITHM *alg = NULL;
260 IMPLEMENTATION *impl;
264 if (nid <= 0 || method == NULL || store == NULL)
266 if (properties == NULL)
269 if (!ossl_assert(prov != NULL))
272 /* Create new entry */
273 impl = OPENSSL_malloc(sizeof(*impl));
276 impl->method.method = method;
277 impl->method.up_ref = method_up_ref;
278 impl->method.free = method_destruct;
279 if (!ossl_method_up_ref(&impl->method)) {
283 impl->provider = prov;
285 /* Insert into the hash table if required */
286 if (!ossl_property_write_lock(store)) {
290 ossl_method_cache_flush(store, nid);
291 if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
292 impl->properties = ossl_parse_property(store->ctx, properties);
293 if (impl->properties == NULL)
295 ossl_prop_defn_set(store->ctx, properties, impl->properties);
298 alg = ossl_method_store_retrieve(store, nid);
300 if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
301 || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
302 || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
305 if (!ossl_method_store_insert(store, alg))
309 /* Push onto stack if there isn't one there already */
310 for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
311 const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);
313 if (tmpimpl->provider == impl->provider
314 && tmpimpl->properties == impl->properties)
317 if (i == sk_IMPLEMENTATION_num(alg->impls)
318 && sk_IMPLEMENTATION_push(alg->impls, impl))
320 ossl_property_unlock(store);
326 ossl_property_unlock(store);
332 int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
335 ALGORITHM *alg = NULL;
338 if (nid <= 0 || method == NULL || store == NULL)
341 if (!ossl_property_write_lock(store))
343 ossl_method_cache_flush(store, nid);
344 alg = ossl_method_store_retrieve(store, nid);
346 ossl_property_unlock(store);
351 * A sorting find then a delete could be faster but these stacks should be
352 * relatively small, so we avoid the overhead. Sorting could also surprise
353 * users when result orderings change (even though they are not guaranteed).
355 for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
356 IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
358 if (impl->method.method == method) {
360 (void)sk_IMPLEMENTATION_delete(alg->impls, i);
361 ossl_property_unlock(store);
365 ossl_property_unlock(store);
369 static void alg_do_one(ALGORITHM *alg, IMPLEMENTATION *impl,
370 void (*fn)(int id, void *method, void *fnarg),
373 fn(alg->nid, impl->method.method, fnarg);
376 struct alg_do_each_data_st {
377 void (*fn)(int id, void *method, void *fnarg);
381 static void alg_do_each(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
383 struct alg_do_each_data_st *data = arg;
384 int i, end = sk_IMPLEMENTATION_num(alg->impls);
386 for (i = 0; i < end; i++) {
387 IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
389 alg_do_one(alg, impl, data->fn, data->fnarg);
393 void ossl_method_store_do_all(OSSL_METHOD_STORE *store,
394 void (*fn)(int id, void *method, void *fnarg),
397 struct alg_do_each_data_st data;
402 ossl_sa_ALGORITHM_doall_arg(store->algs, alg_do_each, &data);
405 int ossl_method_store_fetch(OSSL_METHOD_STORE *store,
406 int nid, const char *prop_query,
407 const OSSL_PROVIDER **prov_rw, void **method)
409 OSSL_PROPERTY_LIST **plp;
411 IMPLEMENTATION *impl, *best_impl = NULL;
412 OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;
413 const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL;
415 int j, best = -1, score, optional;
418 if (!OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
422 if (nid <= 0 || method == NULL || store == NULL)
425 /* This only needs to be a read lock, because the query won't create anything */
426 if (!ossl_property_read_lock(store))
428 alg = ossl_method_store_retrieve(store, nid);
430 ossl_property_unlock(store);
434 if (prop_query != NULL)
435 p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);
436 plp = ossl_ctx_global_properties(store->ctx, 0);
437 if (plp != NULL && *plp != NULL) {
441 p2 = ossl_property_merge(pq, *plp);
442 ossl_property_free(pq);
450 for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
451 if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
452 && (prov == NULL || impl->provider == prov)) {
460 optional = ossl_property_has_optional(pq);
461 for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
462 if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
463 && (prov == NULL || impl->provider == prov)) {
464 score = ossl_property_match_count(pq, impl->properties);
475 if (ret && ossl_method_up_ref(&best_impl->method)) {
476 *method = best_impl->method.method;
478 *prov_rw = best_impl->provider;
482 ossl_property_unlock(store);
483 ossl_property_free(p2);
487 static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)
489 lh_QUERY_doall(alg->cache, &impl_cache_free);
490 lh_QUERY_flush(alg->cache);
493 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
495 ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
498 store->cache_nelem -= lh_QUERY_num_items(alg->cache);
499 impl_cache_flush_alg(0, alg);
503 int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store)
505 if (!ossl_property_write_lock(store))
507 ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
508 store->cache_nelem = 0;
509 ossl_property_unlock(store);
513 IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
516 * Flush an element from the query cache (perhaps).
518 * In order to avoid taking a write lock or using atomic operations
519 * to keep accurate least recently used (LRU) or least frequently used
520 * (LFU) information, the procedure used here is to stochastically
521 * flush approximately half the cache.
523 * This procedure isn't ideal, LRU or LFU would be better. However,
524 * in normal operation, reaching a full cache would be unexpected.
525 * It means that no steady state of algorithm queries has been reached.
526 * That is, it is most likely an attack of some form. A suboptimal clearance
527 * strategy that doesn't degrade performance of the normal case is
528 * preferable to a more refined approach that imposes a performance
531 static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
536 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
537 * https://doi.org/10.18637/jss.v008.i14
539 * This is a very fast PRNG so there is no need to extract bits one at a
540 * time and use the entire value each time.
549 impl_cache_free(lh_QUERY_delete(state->cache, c));
554 static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
557 IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
559 state->cache = alg->cache;
560 lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
564 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
566 IMPL_CACHE_FLUSH state;
569 if ((state.seed = OPENSSL_rdtsc()) == 0)
571 store->cache_need_flush = 0;
572 ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
573 store->cache_nelem = state.nelem;
576 int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
577 int nid, const char *prop_query, void **method)
583 if (nid <= 0 || store == NULL || prop_query == NULL)
586 if (!ossl_property_read_lock(store))
588 alg = ossl_method_store_retrieve(store, nid);
592 elem.query = prop_query;
593 elem.provider = prov;
594 r = lh_QUERY_retrieve(alg->cache, &elem);
597 if (ossl_method_up_ref(&r->method)) {
598 *method = r->method.method;
602 ossl_property_unlock(store);
606 int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
607 int nid, const char *prop_query, void *method,
608 int (*method_up_ref)(void *),
609 void (*method_destruct)(void *))
611 QUERY elem, *old, *p = NULL;
616 if (nid <= 0 || store == NULL || prop_query == NULL)
619 if (!ossl_assert(prov != NULL))
622 if (!ossl_property_write_lock(store))
624 if (store->cache_need_flush)
625 ossl_method_cache_flush_some(store);
626 alg = ossl_method_store_retrieve(store, nid);
630 if (method == NULL) {
631 elem.query = prop_query;
632 elem.provider = prov;
633 if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) {
634 impl_cache_free(old);
635 store->cache_nelem--;
639 p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
643 p->method.method = method;
644 p->method.up_ref = method_up_ref;
645 p->method.free = method_destruct;
646 if (!ossl_method_up_ref(&p->method))
648 memcpy((char *)p->query, prop_query, len + 1);
649 if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
650 impl_cache_free(old);
653 if (!lh_QUERY_error(alg->cache)) {
654 if (++store->cache_nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
655 store->cache_need_flush = 1;
658 ossl_method_free(&p->method);
664 ossl_property_unlock(store);