2 * Copyright 2019-2021 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"
27 * The number of elements in the query cache before we initiate a flush.
28 * If reducing this, also ensure the stochastic test in test/property_test.c
29 * isn't likely to fail.
31 #define IMPL_CACHE_FLUSH_THRESHOLD 500
35 int (*up_ref)(void *);
40 const OSSL_PROVIDER *provider;
41 OSSL_PROPERTY_LIST *properties;
45 DEFINE_STACK_OF(IMPLEMENTATION)
48 const OSSL_PROVIDER *provider;
54 DEFINE_LHASH_OF(QUERY);
58 STACK_OF(IMPLEMENTATION) *impls;
59 LHASH_OF(QUERY) *cache;
62 struct ossl_method_store_st {
65 SPARSE_ARRAY_OF(ALGORITHM) *algs;
71 LHASH_OF(QUERY) *cache;
76 DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
78 typedef struct ossl_global_properties_st {
79 OSSL_PROPERTY_LIST *list;
81 unsigned int no_mirrored : 1;
83 } OSSL_GLOBAL_PROPERTIES;
85 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
87 /* Global properties are stored per library context */
88 static void ossl_ctx_global_properties_free(void *vglobp)
90 OSSL_GLOBAL_PROPERTIES *globp = vglobp;
93 ossl_property_free(globp->list);
98 static void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)
100 return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES));
103 static const OSSL_LIB_CTX_METHOD ossl_ctx_global_properties_method = {
104 OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
105 ossl_ctx_global_properties_new,
106 ossl_ctx_global_properties_free,
109 OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
112 OSSL_GLOBAL_PROPERTIES *globp;
115 if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
118 globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
119 &ossl_ctx_global_properties_method);
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,
129 &ossl_ctx_global_properties_method);
131 return globp->no_mirrored ? 1 : 0;
134 void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx)
136 OSSL_GLOBAL_PROPERTIES *globp
137 = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
138 &ossl_ctx_global_properties_method);
140 globp->no_mirrored = 1;
144 static int ossl_method_up_ref(METHOD *method)
146 return (*method->up_ref)(method->method);
149 static void ossl_method_free(METHOD *method)
151 (*method->free)(method->method);
154 static __owur int ossl_property_read_lock(OSSL_METHOD_STORE *p)
156 return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
159 static __owur int ossl_property_write_lock(OSSL_METHOD_STORE *p)
161 return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
164 static int ossl_property_unlock(OSSL_METHOD_STORE *p)
166 return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
169 static unsigned long query_hash(const QUERY *a)
171 return OPENSSL_LH_strhash(a->query);
174 static int query_cmp(const QUERY *a, const QUERY *b)
176 int res = strcmp(a->query, b->query);
178 if (res == 0 && a->provider != NULL && b->provider != NULL)
179 res = b->provider > a->provider ? 1
180 : b->provider < a->provider ? -1
185 static void impl_free(IMPLEMENTATION *impl)
188 ossl_method_free(&impl->method);
193 static void impl_cache_free(QUERY *elem)
196 ossl_method_free(&elem->method);
201 static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a)
204 sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
205 lh_QUERY_doall(a->cache, &impl_cache_free);
206 lh_QUERY_free(a->cache);
212 * The OSSL_LIB_CTX param here allows access to underlying property data needed
215 OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx)
217 OSSL_METHOD_STORE *res;
219 res = OPENSSL_zalloc(sizeof(*res));
222 if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL) {
226 if ((res->lock = CRYPTO_THREAD_lock_new()) == NULL) {
227 ossl_sa_ALGORITHM_free(res->algs);
235 void ossl_method_store_free(OSSL_METHOD_STORE *store)
238 ossl_sa_ALGORITHM_doall(store->algs, &alg_cleanup);
239 ossl_sa_ALGORITHM_free(store->algs);
240 CRYPTO_THREAD_lock_free(store->lock);
245 static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
247 return ossl_sa_ALGORITHM_get(store->algs, nid);
250 static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
252 return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
255 int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
256 int nid, const char *properties, void *method,
257 int (*method_up_ref)(void *),
258 void (*method_destruct)(void *))
260 ALGORITHM *alg = NULL;
261 IMPLEMENTATION *impl;
265 if (nid <= 0 || method == NULL || store == NULL)
267 if (properties == NULL)
270 if (!ossl_assert(prov != NULL))
273 /* Create new entry */
274 impl = OPENSSL_malloc(sizeof(*impl));
277 impl->method.method = method;
278 impl->method.up_ref = method_up_ref;
279 impl->method.free = method_destruct;
280 if (!ossl_method_up_ref(&impl->method)) {
284 impl->provider = prov;
286 /* Insert into the hash table if required */
287 if (!ossl_property_write_lock(store)) {
291 ossl_method_cache_flush(store, nid);
292 if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
293 impl->properties = ossl_parse_property(store->ctx, properties);
294 if (impl->properties == NULL)
296 ossl_prop_defn_set(store->ctx, properties, impl->properties);
299 alg = ossl_method_store_retrieve(store, nid);
301 if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
302 || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
303 || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
306 if (!ossl_method_store_insert(store, alg))
310 /* Push onto stack if there isn't one there already */
311 for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
312 const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);
314 if (tmpimpl->provider == impl->provider
315 && tmpimpl->properties == impl->properties)
318 if (i == sk_IMPLEMENTATION_num(alg->impls)
319 && sk_IMPLEMENTATION_push(alg->impls, impl))
321 ossl_property_unlock(store);
327 ossl_property_unlock(store);
333 int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
336 ALGORITHM *alg = NULL;
339 if (nid <= 0 || method == NULL || store == NULL)
342 if (!ossl_property_write_lock(store))
344 ossl_method_cache_flush(store, nid);
345 alg = ossl_method_store_retrieve(store, nid);
347 ossl_property_unlock(store);
352 * A sorting find then a delete could be faster but these stacks should be
353 * relatively small, so we avoid the overhead. Sorting could also surprise
354 * users when result orderings change (even though they are not guaranteed).
356 for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
357 IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
359 if (impl->method.method == method) {
361 (void)sk_IMPLEMENTATION_delete(alg->impls, i);
362 ossl_property_unlock(store);
366 ossl_property_unlock(store);
370 static void alg_do_one(ALGORITHM *alg, IMPLEMENTATION *impl,
371 void (*fn)(int id, void *method, void *fnarg),
374 fn(alg->nid, impl->method.method, fnarg);
377 struct alg_do_each_data_st {
378 void (*fn)(int id, void *method, void *fnarg);
382 static void alg_do_each(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
384 struct alg_do_each_data_st *data = arg;
385 int i, end = sk_IMPLEMENTATION_num(alg->impls);
387 for (i = 0; i < end; i++) {
388 IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
390 alg_do_one(alg, impl, data->fn, data->fnarg);
394 void ossl_method_store_do_all(OSSL_METHOD_STORE *store,
395 void (*fn)(int id, void *method, void *fnarg),
398 struct alg_do_each_data_st data;
403 ossl_sa_ALGORITHM_doall_arg(store->algs, alg_do_each, &data);
406 int ossl_method_store_fetch(OSSL_METHOD_STORE *store,
407 int nid, const char *prop_query,
408 const OSSL_PROVIDER **prov_rw, void **method)
410 OSSL_PROPERTY_LIST **plp;
412 IMPLEMENTATION *impl, *best_impl = NULL;
413 OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;
414 const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL;
416 int j, best = -1, score, optional;
419 if (!OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
423 if (nid <= 0 || method == NULL || store == NULL)
426 /* This only needs to be a read lock, because the query won't create anything */
427 if (!ossl_property_read_lock(store))
429 alg = ossl_method_store_retrieve(store, nid);
431 ossl_property_unlock(store);
435 if (prop_query != NULL)
436 p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);
437 plp = ossl_ctx_global_properties(store->ctx, 0);
438 if (plp != NULL && *plp != NULL) {
442 p2 = ossl_property_merge(pq, *plp);
443 ossl_property_free(pq);
451 for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
452 if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
453 && (prov == NULL || impl->provider == prov)) {
461 optional = ossl_property_has_optional(pq);
462 for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
463 if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
464 && (prov == NULL || impl->provider == prov)) {
465 score = ossl_property_match_count(pq, impl->properties);
476 if (ret && ossl_method_up_ref(&best_impl->method)) {
477 *method = best_impl->method.method;
479 *prov_rw = best_impl->provider;
483 ossl_property_unlock(store);
484 ossl_property_free(p2);
488 static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
490 SPARSE_ARRAY_OF(ALGORITHM) *algs = arg;
492 lh_QUERY_doall(alg->cache, &impl_cache_free);
494 sk_IMPLEMENTATION_pop_free(alg->impls, &impl_free);
495 lh_QUERY_free(alg->cache);
497 ossl_sa_ALGORITHM_set(algs, idx, NULL);
499 lh_QUERY_flush(alg->cache);
503 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
505 ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
508 ossl_provider_clear_all_operation_bits(store->ctx);
509 store->nelem -= lh_QUERY_num_items(alg->cache);
510 impl_cache_flush_alg(0, alg, NULL);
514 int ossl_method_store_flush_cache(OSSL_METHOD_STORE *store, int all)
516 void *arg = (all != 0 ? store->algs : NULL);
518 if (!ossl_property_write_lock(store))
520 ossl_provider_clear_all_operation_bits(store->ctx);
521 ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_alg, arg);
523 ossl_property_unlock(store);
527 IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
530 * Flush an element from the query cache (perhaps).
532 * In order to avoid taking a write lock or using atomic operations
533 * to keep accurate least recently used (LRU) or least frequently used
534 * (LFU) information, the procedure used here is to stochastically
535 * flush approximately half the cache.
537 * This procedure isn't ideal, LRU or LFU would be better. However,
538 * in normal operation, reaching a full cache would be unexpected.
539 * It means that no steady state of algorithm queries has been reached.
540 * That is, it is most likely an attack of some form. A suboptimal clearance
541 * strategy that doesn't degrade performance of the normal case is
542 * preferable to a more refined approach that imposes a performance
545 static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
550 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
551 * https://doi.org/10.18637/jss.v008.i14
553 * This is a very fast PRNG so there is no need to extract bits one at a
554 * time and use the entire value each time.
563 impl_cache_free(lh_QUERY_delete(state->cache, c));
568 static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
571 IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
573 state->cache = alg->cache;
574 lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
578 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
580 IMPL_CACHE_FLUSH state;
583 if ((state.seed = OPENSSL_rdtsc()) == 0)
585 ossl_provider_clear_all_operation_bits(store->ctx);
586 store->need_flush = 0;
587 ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
588 store->nelem = state.nelem;
591 int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
592 int nid, const char *prop_query, void **method)
598 if (nid <= 0 || store == NULL)
601 if (!ossl_property_read_lock(store))
603 alg = ossl_method_store_retrieve(store, nid);
607 elem.query = prop_query != NULL ? prop_query : "";
608 elem.provider = prov;
609 r = lh_QUERY_retrieve(alg->cache, &elem);
612 if (ossl_method_up_ref(&r->method)) {
613 *method = r->method.method;
617 ossl_property_unlock(store);
621 int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
622 int nid, const char *prop_query, void *method,
623 int (*method_up_ref)(void *),
624 void (*method_destruct)(void *))
626 QUERY elem, *old, *p = NULL;
631 if (nid <= 0 || store == NULL)
633 if (prop_query == NULL)
636 if (!ossl_assert(prov != NULL))
639 if (!ossl_property_write_lock(store))
641 if (store->need_flush)
642 ossl_method_cache_flush_some(store);
643 alg = ossl_method_store_retrieve(store, nid);
647 if (method == NULL) {
648 elem.query = prop_query;
649 elem.provider = prov;
650 if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) {
651 impl_cache_free(old);
656 p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
660 p->method.method = method;
661 p->method.up_ref = method_up_ref;
662 p->method.free = method_destruct;
663 if (!ossl_method_up_ref(&p->method))
665 memcpy((char *)p->query, prop_query, len + 1);
666 if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
667 impl_cache_free(old);
670 if (!lh_QUERY_error(alg->cache)) {
671 if (++store->nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
672 store->need_flush = 1;
675 ossl_method_free(&p->method);
681 ossl_property_unlock(store);