1 /*
2 * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
4 *
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
9 */
10
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdarg.h>
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
27 /*
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.
31 */
32 #define IMPL_CACHE_FLUSH_THRESHOLD 500
33
34 typedef struct {
35 void *method;
36 int (*up_ref)(void *);
37 void (*free)(void *);
38 } METHOD;
39
40 typedef struct {
41 const OSSL_PROVIDER *provider;
42 OSSL_PROPERTY_LIST *properties;
43 METHOD method;
44 } IMPLEMENTATION;
45
46 DEFINE_STACK_OF(IMPLEMENTATION)
47
48 typedef struct {
49 const OSSL_PROVIDER *provider;
50 const char *query;
51 METHOD method;
52 char body[1];
53 } QUERY;
54
55 DEFINE_LHASH_OF(QUERY);
56
57 typedef struct {
58 int nid;
59 STACK_OF(IMPLEMENTATION) *impls;
60 LHASH_OF(QUERY) *cache;
61 } ALGORITHM;
62
63 struct ossl_method_store_st {
64 OSSL_LIB_CTX *ctx;
65 SPARSE_ARRAY_OF(ALGORITHM) *algs;
66 /*
67 * Lock to protect the |algs| array from concurrent writing, when
68 * individual implementations or queries are inserted. This is used
69 * by the appropriate functions here.
70 */
71 CRYPTO_RWLOCK *lock;
72 /*
73 * Lock to reserve the whole store. This is used when fetching a set
74 * of algorithms, via these functions, found in crypto/core_fetch.c:
75 * ossl_method_construct_reserve_store()
76 * ossl_method_construct_unreserve_store()
77 */
78 CRYPTO_RWLOCK *biglock;
79
80 /* query cache specific values */
81
82 /* Count of the query cache entries for all algs */
83 size_t cache_nelem;
84
85 /* Flag: 1 if query cache entries for all algs need flushing */
86 int cache_need_flush;
87 };
88
89 typedef struct {
90 LHASH_OF(QUERY) *cache;
91 size_t nelem;
92 uint32_t seed;
93 unsigned char using_global_seed;
94 } IMPL_CACHE_FLUSH;
95
96 DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
97
98 DEFINE_STACK_OF(ALGORITHM)
99
100 typedef struct ossl_global_properties_st {
101 OSSL_PROPERTY_LIST *list;
102 #ifndef FIPS_MODULE
103 unsigned int no_mirrored : 1;
104 #endif
105 } OSSL_GLOBAL_PROPERTIES;
106
107 static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
108 ALGORITHM *alg);
109 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
110
111 /* Global properties are stored per library context */
ossl_ctx_global_properties_free(void * vglobp)112 static void ossl_ctx_global_properties_free(void *vglobp)
113 {
114 OSSL_GLOBAL_PROPERTIES *globp = vglobp;
115
116 if (globp != NULL) {
117 ossl_property_free(globp->list);
118 OPENSSL_free(globp);
119 }
120 }
121
ossl_ctx_global_properties_new(OSSL_LIB_CTX * ctx)122 static void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)
123 {
124 return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES));
125 }
126
127 static const OSSL_LIB_CTX_METHOD ossl_ctx_global_properties_method = {
128 OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
129 ossl_ctx_global_properties_new,
130 ossl_ctx_global_properties_free,
131 };
132
ossl_ctx_global_properties(OSSL_LIB_CTX * libctx,ossl_unused int loadconfig)133 OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
134 ossl_unused int loadconfig)
135 {
136 OSSL_GLOBAL_PROPERTIES *globp;
137
138 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
139 if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
140 return NULL;
141 #endif
142 globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
143 &ossl_ctx_global_properties_method);
144
145 return globp != NULL ? &globp->list : NULL;
146 }
147
148 #ifndef FIPS_MODULE
ossl_global_properties_no_mirrored(OSSL_LIB_CTX * libctx)149 int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx)
150 {
151 OSSL_GLOBAL_PROPERTIES *globp
152 = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
153 &ossl_ctx_global_properties_method);
154
155 return globp != NULL && globp->no_mirrored ? 1 : 0;
156 }
157
ossl_global_properties_stop_mirroring(OSSL_LIB_CTX * libctx)158 void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx)
159 {
160 OSSL_GLOBAL_PROPERTIES *globp
161 = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
162 &ossl_ctx_global_properties_method);
163
164 if (globp != NULL)
165 globp->no_mirrored = 1;
166 }
167 #endif
168
ossl_method_up_ref(METHOD * method)169 static int ossl_method_up_ref(METHOD *method)
170 {
171 return (*method->up_ref)(method->method);
172 }
173
ossl_method_free(METHOD * method)174 static void ossl_method_free(METHOD *method)
175 {
176 (*method->free)(method->method);
177 }
178
ossl_property_read_lock(OSSL_METHOD_STORE * p)179 static __owur int ossl_property_read_lock(OSSL_METHOD_STORE *p)
180 {
181 return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
182 }
183
ossl_property_write_lock(OSSL_METHOD_STORE * p)184 static __owur int ossl_property_write_lock(OSSL_METHOD_STORE *p)
185 {
186 return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
187 }
188
ossl_property_unlock(OSSL_METHOD_STORE * p)189 static int ossl_property_unlock(OSSL_METHOD_STORE *p)
190 {
191 return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
192 }
193
query_hash(const QUERY * a)194 static unsigned long query_hash(const QUERY *a)
195 {
196 return OPENSSL_LH_strhash(a->query);
197 }
198
query_cmp(const QUERY * a,const QUERY * b)199 static int query_cmp(const QUERY *a, const QUERY *b)
200 {
201 int res = strcmp(a->query, b->query);
202
203 if (res == 0 && a->provider != NULL && b->provider != NULL)
204 res = b->provider > a->provider ? 1
205 : b->provider < a->provider ? -1
206 : 0;
207 return res;
208 }
209
impl_free(IMPLEMENTATION * impl)210 static void impl_free(IMPLEMENTATION *impl)
211 {
212 if (impl != NULL) {
213 ossl_method_free(&impl->method);
214 OPENSSL_free(impl);
215 }
216 }
217
impl_cache_free(QUERY * elem)218 static void impl_cache_free(QUERY *elem)
219 {
220 if (elem != NULL) {
221 ossl_method_free(&elem->method);
222 OPENSSL_free(elem);
223 }
224 }
225
impl_cache_flush_alg(ossl_uintmax_t idx,ALGORITHM * alg)226 static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)
227 {
228 lh_QUERY_doall(alg->cache, &impl_cache_free);
229 lh_QUERY_flush(alg->cache);
230 }
231
alg_cleanup(ossl_uintmax_t idx,ALGORITHM * a,void * arg)232 static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a, void *arg)
233 {
234 OSSL_METHOD_STORE *store = arg;
235
236 if (a != NULL) {
237 sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
238 lh_QUERY_doall(a->cache, &impl_cache_free);
239 lh_QUERY_free(a->cache);
240 OPENSSL_free(a);
241 }
242 if (store != NULL)
243 ossl_sa_ALGORITHM_set(store->algs, idx, NULL);
244 }
245
246 /*
247 * The OSSL_LIB_CTX param here allows access to underlying property data needed
248 * for computation
249 */
ossl_method_store_new(OSSL_LIB_CTX * ctx)250 OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx)
251 {
252 OSSL_METHOD_STORE *res;
253
254 res = OPENSSL_zalloc(sizeof(*res));
255 if (res != NULL) {
256 res->ctx = ctx;
257 if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL
258 || (res->lock = CRYPTO_THREAD_lock_new()) == NULL
259 || (res->biglock = CRYPTO_THREAD_lock_new()) == NULL) {
260 ossl_method_store_free(res);
261 return NULL;
262 }
263 }
264 return res;
265 }
266
ossl_method_store_free(OSSL_METHOD_STORE * store)267 void ossl_method_store_free(OSSL_METHOD_STORE *store)
268 {
269 if (store != NULL) {
270 if (store->algs != NULL)
271 ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup, store);
272 ossl_sa_ALGORITHM_free(store->algs);
273 CRYPTO_THREAD_lock_free(store->lock);
274 CRYPTO_THREAD_lock_free(store->biglock);
275 OPENSSL_free(store);
276 }
277 }
278
ossl_method_lock_store(OSSL_METHOD_STORE * store)279 int ossl_method_lock_store(OSSL_METHOD_STORE *store)
280 {
281 return store != NULL ? CRYPTO_THREAD_write_lock(store->biglock) : 0;
282 }
283
ossl_method_unlock_store(OSSL_METHOD_STORE * store)284 int ossl_method_unlock_store(OSSL_METHOD_STORE *store)
285 {
286 return store != NULL ? CRYPTO_THREAD_unlock(store->biglock) : 0;
287 }
288
ossl_method_store_retrieve(OSSL_METHOD_STORE * store,int nid)289 static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
290 {
291 return ossl_sa_ALGORITHM_get(store->algs, nid);
292 }
293
ossl_method_store_insert(OSSL_METHOD_STORE * store,ALGORITHM * alg)294 static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
295 {
296 return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
297 }
298
ossl_method_store_add(OSSL_METHOD_STORE * store,const OSSL_PROVIDER * prov,int nid,const char * properties,void * method,int (* method_up_ref)(void *),void (* method_destruct)(void *))299 int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
300 int nid, const char *properties, void *method,
301 int (*method_up_ref)(void *),
302 void (*method_destruct)(void *))
303 {
304 ALGORITHM *alg = NULL;
305 IMPLEMENTATION *impl;
306 int ret = 0;
307 int i;
308
309 if (nid <= 0 || method == NULL || store == NULL)
310 return 0;
311 if (properties == NULL)
312 properties = "";
313
314 if (!ossl_assert(prov != NULL))
315 return 0;
316
317 /* Create new entry */
318 impl = OPENSSL_malloc(sizeof(*impl));
319 if (impl == NULL)
320 return 0;
321 impl->method.method = method;
322 impl->method.up_ref = method_up_ref;
323 impl->method.free = method_destruct;
324 if (!ossl_method_up_ref(&impl->method)) {
325 OPENSSL_free(impl);
326 return 0;
327 }
328 impl->provider = prov;
329
330 /* Insert into the hash table if required */
331 if (!ossl_property_write_lock(store)) {
332 OPENSSL_free(impl);
333 return 0;
334 }
335 ossl_method_cache_flush(store, nid);
336 if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
337 impl->properties = ossl_parse_property(store->ctx, properties);
338 if (impl->properties == NULL)
339 goto err;
340 if (!ossl_prop_defn_set(store->ctx, properties, &impl->properties)) {
341 ossl_property_free(impl->properties);
342 impl->properties = NULL;
343 goto err;
344 }
345 }
346
347 alg = ossl_method_store_retrieve(store, nid);
348 if (alg == NULL) {
349 if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
350 || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
351 || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
352 goto err;
353 alg->nid = nid;
354 if (!ossl_method_store_insert(store, alg))
355 goto err;
356 }
357
358 /* Push onto stack if there isn't one there already */
359 for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
360 const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);
361
362 if (tmpimpl->provider == impl->provider
363 && tmpimpl->properties == impl->properties)
364 break;
365 }
366 if (i == sk_IMPLEMENTATION_num(alg->impls)
367 && sk_IMPLEMENTATION_push(alg->impls, impl))
368 ret = 1;
369 ossl_property_unlock(store);
370 if (ret == 0)
371 impl_free(impl);
372 return ret;
373
374 err:
375 ossl_property_unlock(store);
376 alg_cleanup(0, alg, NULL);
377 impl_free(impl);
378 return 0;
379 }
380
ossl_method_store_remove(OSSL_METHOD_STORE * store,int nid,const void * method)381 int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
382 const void *method)
383 {
384 ALGORITHM *alg = NULL;
385 int i;
386
387 if (nid <= 0 || method == NULL || store == NULL)
388 return 0;
389
390 if (!ossl_property_write_lock(store))
391 return 0;
392 ossl_method_cache_flush(store, nid);
393 alg = ossl_method_store_retrieve(store, nid);
394 if (alg == NULL) {
395 ossl_property_unlock(store);
396 return 0;
397 }
398
399 /*
400 * A sorting find then a delete could be faster but these stacks should be
401 * relatively small, so we avoid the overhead. Sorting could also surprise
402 * users when result orderings change (even though they are not guaranteed).
403 */
404 for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
405 IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
406
407 if (impl->method.method == method) {
408 impl_free(impl);
409 (void)sk_IMPLEMENTATION_delete(alg->impls, i);
410 ossl_property_unlock(store);
411 return 1;
412 }
413 }
414 ossl_property_unlock(store);
415 return 0;
416 }
417
418 struct alg_cleanup_by_provider_data_st {
419 OSSL_METHOD_STORE *store;
420 const OSSL_PROVIDER *prov;
421 };
422
423 static void
alg_cleanup_by_provider(ossl_uintmax_t idx,ALGORITHM * alg,void * arg)424 alg_cleanup_by_provider(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
425 {
426 struct alg_cleanup_by_provider_data_st *data = arg;
427 int i, count;
428
429 /*
430 * We walk the stack backwards, to avoid having to deal with stack shifts
431 * caused by deletion
432 */
433 for (count = 0, i = sk_IMPLEMENTATION_num(alg->impls); i-- > 0;) {
434 IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
435
436 if (impl->provider == data->prov) {
437 impl_free(impl);
438 (void)sk_IMPLEMENTATION_delete(alg->impls, i);
439 count++;
440 }
441 }
442
443 /*
444 * If we removed any implementation, we also clear the whole associated
445 * cache, 'cause that's the sensible thing to do.
446 * There's no point flushing the cache entries where we didn't remove
447 * any implementation, though.
448 */
449 if (count > 0)
450 ossl_method_cache_flush_alg(data->store, alg);
451 }
452
ossl_method_store_remove_all_provided(OSSL_METHOD_STORE * store,const OSSL_PROVIDER * prov)453 int ossl_method_store_remove_all_provided(OSSL_METHOD_STORE *store,
454 const OSSL_PROVIDER *prov)
455 {
456 struct alg_cleanup_by_provider_data_st data;
457
458 if (!ossl_property_write_lock(store))
459 return 0;
460 data.prov = prov;
461 data.store = store;
462 ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup_by_provider, &data);
463 ossl_property_unlock(store);
464 return 1;
465 }
466
alg_do_one(ALGORITHM * alg,IMPLEMENTATION * impl,void (* fn)(int id,void * method,void * fnarg),void * fnarg)467 static void alg_do_one(ALGORITHM *alg, IMPLEMENTATION *impl,
468 void (*fn)(int id, void *method, void *fnarg),
469 void *fnarg)
470 {
471 fn(alg->nid, impl->method.method, fnarg);
472 }
473
alg_copy(ossl_uintmax_t idx,ALGORITHM * alg,void * arg)474 static void alg_copy(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
475 {
476 STACK_OF(ALGORITHM) *newalg = arg;
477
478 (void)sk_ALGORITHM_push(newalg, alg);
479 }
480
ossl_method_store_do_all(OSSL_METHOD_STORE * store,void (* fn)(int id,void * method,void * fnarg),void * fnarg)481 void ossl_method_store_do_all(OSSL_METHOD_STORE *store,
482 void (*fn)(int id, void *method, void *fnarg),
483 void *fnarg)
484 {
485 int i, j;
486 int numalgs, numimps;
487 STACK_OF(ALGORITHM) *tmpalgs;
488 ALGORITHM *alg;
489
490 if (store != NULL) {
491
492 if (!ossl_property_read_lock(store))
493 return;
494
495 tmpalgs = sk_ALGORITHM_new_reserve(NULL,
496 ossl_sa_ALGORITHM_num(store->algs));
497 if (tmpalgs == NULL) {
498 ossl_property_unlock(store);
499 return;
500 }
501
502 ossl_sa_ALGORITHM_doall_arg(store->algs, alg_copy, tmpalgs);
503 ossl_property_unlock(store);
504 numalgs = sk_ALGORITHM_num(tmpalgs);
505 for (i = 0; i < numalgs; i++) {
506 alg = sk_ALGORITHM_value(tmpalgs, i);
507 numimps = sk_IMPLEMENTATION_num(alg->impls);
508 for (j = 0; j < numimps; j++)
509 alg_do_one(alg, sk_IMPLEMENTATION_value(alg->impls, j), fn, fnarg);
510 }
511 sk_ALGORITHM_free(tmpalgs);
512 }
513 }
514
ossl_method_store_fetch(OSSL_METHOD_STORE * store,int nid,const char * prop_query,const OSSL_PROVIDER ** prov_rw,void ** method)515 int ossl_method_store_fetch(OSSL_METHOD_STORE *store,
516 int nid, const char *prop_query,
517 const OSSL_PROVIDER **prov_rw, void **method)
518 {
519 OSSL_PROPERTY_LIST **plp;
520 ALGORITHM *alg;
521 IMPLEMENTATION *impl, *best_impl = NULL;
522 OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;
523 const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL;
524 int ret = 0;
525 int j, best = -1, score, optional;
526
527 if (nid <= 0 || method == NULL || store == NULL)
528 return 0;
529
530 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
531 if (ossl_lib_ctx_is_default(store->ctx)
532 && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
533 return 0;
534 #endif
535
536 /* This only needs to be a read lock, because the query won't create anything */
537 if (!ossl_property_read_lock(store))
538 return 0;
539 alg = ossl_method_store_retrieve(store, nid);
540 if (alg == NULL) {
541 ossl_property_unlock(store);
542 return 0;
543 }
544
545 if (prop_query != NULL)
546 p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);
547 plp = ossl_ctx_global_properties(store->ctx, 0);
548 if (plp != NULL && *plp != NULL) {
549 if (pq == NULL) {
550 pq = *plp;
551 } else {
552 p2 = ossl_property_merge(pq, *plp);
553 ossl_property_free(pq);
554 if (p2 == NULL)
555 goto fin;
556 pq = p2;
557 }
558 }
559
560 if (pq == NULL) {
561 for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
562 if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
563 && (prov == NULL || impl->provider == prov)) {
564 best_impl = impl;
565 ret = 1;
566 break;
567 }
568 }
569 goto fin;
570 }
571 optional = ossl_property_has_optional(pq);
572 for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
573 if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
574 && (prov == NULL || impl->provider == prov)) {
575 score = ossl_property_match_count(pq, impl->properties);
576 if (score > best) {
577 best_impl = impl;
578 best = score;
579 ret = 1;
580 if (!optional)
581 goto fin;
582 }
583 }
584 }
585 fin:
586 if (ret && ossl_method_up_ref(&best_impl->method)) {
587 *method = best_impl->method.method;
588 if (prov_rw != NULL)
589 *prov_rw = best_impl->provider;
590 } else {
591 ret = 0;
592 }
593 ossl_property_unlock(store);
594 ossl_property_free(p2);
595 return ret;
596 }
597
ossl_method_cache_flush_alg(OSSL_METHOD_STORE * store,ALGORITHM * alg)598 static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
599 ALGORITHM *alg)
600 {
601 store->cache_nelem -= lh_QUERY_num_items(alg->cache);
602 impl_cache_flush_alg(0, alg);
603 }
604
ossl_method_cache_flush(OSSL_METHOD_STORE * store,int nid)605 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
606 {
607 ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
608
609 if (alg != NULL)
610 ossl_method_cache_flush_alg(store, alg);
611 }
612
ossl_method_store_cache_flush_all(OSSL_METHOD_STORE * store)613 int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store)
614 {
615 if (!ossl_property_write_lock(store))
616 return 0;
617 ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
618 store->cache_nelem = 0;
619 ossl_property_unlock(store);
620 return 1;
621 }
622
623 IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
624
625 /*
626 * Flush an element from the query cache (perhaps).
627 *
628 * In order to avoid taking a write lock or using atomic operations
629 * to keep accurate least recently used (LRU) or least frequently used
630 * (LFU) information, the procedure used here is to stochastically
631 * flush approximately half the cache.
632 *
633 * This procedure isn't ideal, LRU or LFU would be better. However,
634 * in normal operation, reaching a full cache would be unexpected.
635 * It means that no steady state of algorithm queries has been reached.
636 * That is, it is most likely an attack of some form. A suboptimal clearance
637 * strategy that doesn't degrade performance of the normal case is
638 * preferable to a more refined approach that imposes a performance
639 * impact.
640 */
impl_cache_flush_cache(QUERY * c,IMPL_CACHE_FLUSH * state)641 static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
642 {
643 uint32_t n;
644
645 /*
646 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
647 * https://doi.org/10.18637/jss.v008.i14
648 *
649 * This is a very fast PRNG so there is no need to extract bits one at a
650 * time and use the entire value each time.
651 */
652 n = state->seed;
653 n ^= n << 13;
654 n ^= n >> 17;
655 n ^= n << 5;
656 state->seed = n;
657
658 if ((n & 1) != 0)
659 impl_cache_free(lh_QUERY_delete(state->cache, c));
660 else
661 state->nelem++;
662 }
663
impl_cache_flush_one_alg(ossl_uintmax_t idx,ALGORITHM * alg,void * v)664 static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
665 void *v)
666 {
667 IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
668 unsigned long orig_down_load = lh_QUERY_get_down_load(alg->cache);
669
670 state->cache = alg->cache;
671 lh_QUERY_set_down_load(alg->cache, 0);
672 lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
673 state);
674 lh_QUERY_set_down_load(alg->cache, orig_down_load);
675 }
676
ossl_method_cache_flush_some(OSSL_METHOD_STORE * store)677 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
678 {
679 IMPL_CACHE_FLUSH state;
680 static TSAN_QUALIFIER uint32_t global_seed = 1;
681
682 state.nelem = 0;
683 state.using_global_seed = 0;
684 if ((state.seed = OPENSSL_rdtsc()) == 0) {
685 /* If there is no timer available, seed another way */
686 state.using_global_seed = 1;
687 state.seed = tsan_load(&global_seed);
688 }
689 store->cache_need_flush = 0;
690 ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
691 store->cache_nelem = state.nelem;
692 /* Without a timer, update the global seed */
693 if (state.using_global_seed)
694 tsan_store(&global_seed, state.seed);
695 }
696
ossl_method_store_cache_get(OSSL_METHOD_STORE * store,OSSL_PROVIDER * prov,int nid,const char * prop_query,void ** method)697 int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
698 int nid, const char *prop_query, void **method)
699 {
700 ALGORITHM *alg;
701 QUERY elem, *r;
702 int res = 0;
703
704 if (nid <= 0 || store == NULL || prop_query == NULL)
705 return 0;
706
707 if (!ossl_property_read_lock(store))
708 return 0;
709 alg = ossl_method_store_retrieve(store, nid);
710 if (alg == NULL)
711 goto err;
712
713 elem.query = prop_query;
714 elem.provider = prov;
715 r = lh_QUERY_retrieve(alg->cache, &elem);
716 if (r == NULL)
717 goto err;
718 if (ossl_method_up_ref(&r->method)) {
719 *method = r->method.method;
720 res = 1;
721 }
722 err:
723 ossl_property_unlock(store);
724 return res;
725 }
726
ossl_method_store_cache_set(OSSL_METHOD_STORE * store,OSSL_PROVIDER * prov,int nid,const char * prop_query,void * method,int (* method_up_ref)(void *),void (* method_destruct)(void *))727 int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
728 int nid, const char *prop_query, void *method,
729 int (*method_up_ref)(void *),
730 void (*method_destruct)(void *))
731 {
732 QUERY elem, *old, *p = NULL;
733 ALGORITHM *alg;
734 size_t len;
735 int res = 1;
736
737 if (nid <= 0 || store == NULL || prop_query == NULL)
738 return 0;
739
740 if (!ossl_assert(prov != NULL))
741 return 0;
742
743 if (!ossl_property_write_lock(store))
744 return 0;
745 if (store->cache_need_flush)
746 ossl_method_cache_flush_some(store);
747 alg = ossl_method_store_retrieve(store, nid);
748 if (alg == NULL)
749 goto err;
750
751 if (method == NULL) {
752 elem.query = prop_query;
753 elem.provider = prov;
754 if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) {
755 impl_cache_free(old);
756 store->cache_nelem--;
757 }
758 goto end;
759 }
760 p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
761 if (p != NULL) {
762 p->query = p->body;
763 p->provider = prov;
764 p->method.method = method;
765 p->method.up_ref = method_up_ref;
766 p->method.free = method_destruct;
767 if (!ossl_method_up_ref(&p->method))
768 goto err;
769 memcpy((char *)p->query, prop_query, len + 1);
770 if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
771 impl_cache_free(old);
772 goto end;
773 }
774 if (!lh_QUERY_error(alg->cache)) {
775 if (++store->cache_nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
776 store->cache_need_flush = 1;
777 goto end;
778 }
779 ossl_method_free(&p->method);
780 }
781 err:
782 res = 0;
783 OPENSSL_free(p);
784 end:
785 ossl_property_unlock(store);
786 return res;
787 }
788