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