/src/openssl/crypto/property/property.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2019-2023 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  | 85  | #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 */  | 
111  |  | void ossl_ctx_global_properties_free(void *vglobp)  | 
112  | 16  | { | 
113  | 16  |     OSSL_GLOBAL_PROPERTIES *globp = vglobp;  | 
114  |  |  | 
115  | 16  |     if (globp != NULL) { | 
116  | 16  |         ossl_property_free(globp->list);  | 
117  | 16  |         OPENSSL_free(globp);  | 
118  | 16  |     }  | 
119  | 16  | }  | 
120  |  |  | 
121  |  | void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)  | 
122  | 16  | { | 
123  | 16  |     return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES));  | 
124  | 16  | }  | 
125  |  |  | 
126  |  | OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,  | 
127  |  |                                                 ossl_unused int loadconfig)  | 
128  | 85  | { | 
129  | 85  |     OSSL_GLOBAL_PROPERTIES *globp;  | 
130  |  |  | 
131  | 85  | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)  | 
132  | 85  |     if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))  | 
133  | 0  |         return NULL;  | 
134  | 85  | #endif  | 
135  | 85  |     globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);  | 
136  |  |  | 
137  | 85  |     return globp != NULL ? &globp->list : NULL;  | 
138  | 85  | }  | 
139  |  |  | 
140  |  | #ifndef FIPS_MODULE  | 
141  |  | int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx)  | 
142  | 0  | { | 
143  | 0  |     OSSL_GLOBAL_PROPERTIES *globp  | 
144  | 0  |         = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);  | 
145  |  | 
  | 
146  | 0  |     return globp != NULL && globp->no_mirrored ? 1 : 0;  | 
147  | 0  | }  | 
148  |  |  | 
149  |  | void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx)  | 
150  | 0  | { | 
151  | 0  |     OSSL_GLOBAL_PROPERTIES *globp  | 
152  | 0  |         = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);  | 
153  |  | 
  | 
154  | 0  |     if (globp != NULL)  | 
155  | 0  |         globp->no_mirrored = 1;  | 
156  | 0  | }  | 
157  |  | #endif  | 
158  |  |  | 
159  |  | static int ossl_method_up_ref(METHOD *method)  | 
160  | 85.0k  | { | 
161  | 85.0k  |     return (*method->up_ref)(method->method);  | 
162  | 85.0k  | }  | 
163  |  |  | 
164  |  | static void ossl_method_free(METHOD *method)  | 
165  | 2.67k  | { | 
166  | 2.67k  |     (*method->free)(method->method);  | 
167  | 2.67k  | }  | 
168  |  |  | 
169  |  | static __owur int ossl_property_read_lock(OSSL_METHOD_STORE *p)  | 
170  | 82.4k  | { | 
171  | 82.4k  |     return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;  | 
172  | 82.4k  | }  | 
173  |  |  | 
174  |  | static __owur int ossl_property_write_lock(OSSL_METHOD_STORE *p)  | 
175  | 2.67k  | { | 
176  | 2.67k  |     return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;  | 
177  | 2.67k  | }  | 
178  |  |  | 
179  |  | static int ossl_property_unlock(OSSL_METHOD_STORE *p)  | 
180  | 85.0k  | { | 
181  | 85.0k  |     return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;  | 
182  | 85.0k  | }  | 
183  |  |  | 
184  |  | static unsigned long query_hash(const QUERY *a)  | 
185  | 82.4k  | { | 
186  | 82.4k  |     return OPENSSL_LH_strhash(a->query);  | 
187  | 82.4k  | }  | 
188  |  |  | 
189  |  | static int query_cmp(const QUERY *a, const QUERY *b)  | 
190  | 82.2k  | { | 
191  | 82.2k  |     int res = strcmp(a->query, b->query);  | 
192  |  |  | 
193  | 82.2k  |     if (res == 0 && a->provider != NULL && b->provider != NULL)  | 
194  | 0  |         res = b->provider > a->provider ? 1  | 
195  | 0  |             : b->provider < a->provider ? -1  | 
196  | 0  |             : 0;  | 
197  | 82.2k  |     return res;  | 
198  | 82.2k  | }  | 
199  |  |  | 
200  |  | static void impl_free(IMPLEMENTATION *impl)  | 
201  | 2.59k  | { | 
202  | 2.59k  |     if (impl != NULL) { | 
203  | 2.59k  |         ossl_method_free(&impl->method);  | 
204  | 2.59k  |         OPENSSL_free(impl);  | 
205  | 2.59k  |     }  | 
206  | 2.59k  | }  | 
207  |  |  | 
208  |  | static void impl_cache_free(QUERY *elem)  | 
209  | 85  | { | 
210  | 85  |     if (elem != NULL) { | 
211  | 85  |         ossl_method_free(&elem->method);  | 
212  | 85  |         OPENSSL_free(elem);  | 
213  | 85  |     }  | 
214  | 85  | }  | 
215  |  |  | 
216  |  | static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)  | 
217  | 0  | { | 
218  | 0  |     lh_QUERY_doall(alg->cache, &impl_cache_free);  | 
219  | 0  |     lh_QUERY_flush(alg->cache);  | 
220  | 0  | }  | 
221  |  |  | 
222  |  | static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a, void *arg)  | 
223  | 2.59k  | { | 
224  | 2.59k  |     OSSL_METHOD_STORE *store = arg;  | 
225  |  |  | 
226  | 2.59k  |     if (a != NULL) { | 
227  | 2.59k  |         sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);  | 
228  | 2.59k  |         lh_QUERY_doall(a->cache, &impl_cache_free);  | 
229  | 2.59k  |         lh_QUERY_free(a->cache);  | 
230  | 2.59k  |         OPENSSL_free(a);  | 
231  | 2.59k  |     }  | 
232  | 2.59k  |     if (store != NULL)  | 
233  | 2.59k  |         ossl_sa_ALGORITHM_set(store->algs, idx, NULL);  | 
234  | 2.59k  | }  | 
235  |  |  | 
236  |  | /*  | 
237  |  |  * The OSSL_LIB_CTX param here allows access to underlying property data needed  | 
238  |  |  * for computation  | 
239  |  |  */  | 
240  |  | OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx)  | 
241  | 64  | { | 
242  | 64  |     OSSL_METHOD_STORE *res;  | 
243  |  |  | 
244  | 64  |     res = OPENSSL_zalloc(sizeof(*res));  | 
245  | 64  |     if (res != NULL) { | 
246  | 64  |         res->ctx = ctx;  | 
247  | 64  |         if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL  | 
248  | 64  |             || (res->lock = CRYPTO_THREAD_lock_new()) == NULL  | 
249  | 64  |             || (res->biglock = CRYPTO_THREAD_lock_new()) == NULL) { | 
250  | 0  |             ossl_method_store_free(res);  | 
251  | 0  |             return NULL;  | 
252  | 0  |         }  | 
253  | 64  |     }  | 
254  | 64  |     return res;  | 
255  | 64  | }  | 
256  |  |  | 
257  |  | void ossl_method_store_free(OSSL_METHOD_STORE *store)  | 
258  | 64  | { | 
259  | 64  |     if (store != NULL) { | 
260  | 64  |         if (store->algs != NULL)  | 
261  | 64  |             ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup, store);  | 
262  | 64  |         ossl_sa_ALGORITHM_free(store->algs);  | 
263  | 64  |         CRYPTO_THREAD_lock_free(store->lock);  | 
264  | 64  |         CRYPTO_THREAD_lock_free(store->biglock);  | 
265  | 64  |         OPENSSL_free(store);  | 
266  | 64  |     }  | 
267  | 64  | }  | 
268  |  |  | 
269  |  | int ossl_method_lock_store(OSSL_METHOD_STORE *store)  | 
270  | 85  | { | 
271  | 85  |     return store != NULL ? CRYPTO_THREAD_write_lock(store->biglock) : 0;  | 
272  | 85  | }  | 
273  |  |  | 
274  |  | int ossl_method_unlock_store(OSSL_METHOD_STORE *store)  | 
275  | 85  | { | 
276  | 85  |     return store != NULL ? CRYPTO_THREAD_unlock(store->biglock) : 0;  | 
277  | 85  | }  | 
278  |  |  | 
279  |  | static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)  | 
280  | 87.6k  | { | 
281  | 87.6k  |     return ossl_sa_ALGORITHM_get(store->algs, nid);  | 
282  | 87.6k  | }  | 
283  |  |  | 
284  |  | static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)  | 
285  | 2.59k  | { | 
286  | 2.59k  |     return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);  | 
287  | 2.59k  | }  | 
288  |  |  | 
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  | 2.59k  | { | 
294  | 2.59k  |     ALGORITHM *alg = NULL;  | 
295  | 2.59k  |     IMPLEMENTATION *impl;  | 
296  | 2.59k  |     int ret = 0;  | 
297  | 2.59k  |     int i;  | 
298  |  |  | 
299  | 2.59k  |     if (nid <= 0 || method == NULL || store == NULL)  | 
300  | 0  |         return 0;  | 
301  | 2.59k  |     if (properties == NULL)  | 
302  | 0  |         properties = "";  | 
303  |  |  | 
304  | 2.59k  |     if (!ossl_assert(prov != NULL))  | 
305  | 0  |         return 0;  | 
306  |  |  | 
307  |  |     /* Create new entry */  | 
308  | 2.59k  |     impl = OPENSSL_malloc(sizeof(*impl));  | 
309  | 2.59k  |     if (impl == NULL)  | 
310  | 0  |         return 0;  | 
311  | 2.59k  |     impl->method.method = method;  | 
312  | 2.59k  |     impl->method.up_ref = method_up_ref;  | 
313  | 2.59k  |     impl->method.free = method_destruct;  | 
314  | 2.59k  |     if (!ossl_method_up_ref(&impl->method)) { | 
315  | 0  |         OPENSSL_free(impl);  | 
316  | 0  |         return 0;  | 
317  | 0  |     }  | 
318  | 2.59k  |     impl->provider = prov;  | 
319  |  |  | 
320  |  |     /* Insert into the hash table if required */  | 
321  | 2.59k  |     if (!ossl_property_write_lock(store)) { | 
322  | 0  |         OPENSSL_free(impl);  | 
323  | 0  |         return 0;  | 
324  | 0  |     }  | 
325  | 2.59k  |     ossl_method_cache_flush(store, nid);  | 
326  | 2.59k  |     if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) { | 
327  | 16  |         impl->properties = ossl_parse_property(store->ctx, properties);  | 
328  | 16  |         if (impl->properties == NULL)  | 
329  | 0  |             goto err;  | 
330  | 16  |         if (!ossl_prop_defn_set(store->ctx, properties, &impl->properties)) { | 
331  | 0  |             ossl_property_free(impl->properties);  | 
332  | 0  |             impl->properties = NULL;  | 
333  | 0  |             goto err;  | 
334  | 0  |         }  | 
335  | 16  |     }  | 
336  |  |  | 
337  | 2.59k  |     alg = ossl_method_store_retrieve(store, nid);  | 
338  | 2.59k  |     if (alg == NULL) { | 
339  | 2.59k  |         if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL  | 
340  | 2.59k  |                 || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL  | 
341  | 2.59k  |                 || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)  | 
342  | 0  |             goto err;  | 
343  | 2.59k  |         alg->nid = nid;  | 
344  | 2.59k  |         if (!ossl_method_store_insert(store, alg))  | 
345  | 0  |             goto err;  | 
346  | 2.59k  |     }  | 
347  |  |  | 
348  |  |     /* Push onto stack if there isn't one there already */  | 
349  | 2.59k  |     for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) { | 
350  | 0  |         const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);  | 
351  |  | 
  | 
352  | 0  |         if (tmpimpl->provider == impl->provider  | 
353  | 0  |             && tmpimpl->properties == impl->properties)  | 
354  | 0  |             break;  | 
355  | 0  |     }  | 
356  | 2.59k  |     if (i == sk_IMPLEMENTATION_num(alg->impls)  | 
357  | 2.59k  |         && sk_IMPLEMENTATION_push(alg->impls, impl))  | 
358  | 2.59k  |         ret = 1;  | 
359  | 2.59k  |     ossl_property_unlock(store);  | 
360  | 2.59k  |     if (ret == 0)  | 
361  | 0  |         impl_free(impl);  | 
362  | 2.59k  |     return ret;  | 
363  |  |  | 
364  | 0  | err:  | 
365  | 0  |     ossl_property_unlock(store);  | 
366  | 0  |     alg_cleanup(0, alg, NULL);  | 
367  | 0  |     impl_free(impl);  | 
368  | 0  |     return 0;  | 
369  | 2.59k  | }  | 
370  |  |  | 
371  |  | int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,  | 
372  |  |                              const void *method)  | 
373  | 0  | { | 
374  | 0  |     ALGORITHM *alg = NULL;  | 
375  | 0  |     int i;  | 
376  |  | 
  | 
377  | 0  |     if (nid <= 0 || method == NULL || store == NULL)  | 
378  | 0  |         return 0;  | 
379  |  |  | 
380  | 0  |     if (!ossl_property_write_lock(store))  | 
381  | 0  |         return 0;  | 
382  | 0  |     ossl_method_cache_flush(store, nid);  | 
383  | 0  |     alg = ossl_method_store_retrieve(store, nid);  | 
384  | 0  |     if (alg == NULL) { | 
385  | 0  |         ossl_property_unlock(store);  | 
386  | 0  |         return 0;  | 
387  | 0  |     }  | 
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  | 0  |     for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) { | 
395  | 0  |         IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);  | 
396  |  | 
  | 
397  | 0  |         if (impl->method.method == method) { | 
398  | 0  |             impl_free(impl);  | 
399  | 0  |             (void)sk_IMPLEMENTATION_delete(alg->impls, i);  | 
400  | 0  |             ossl_property_unlock(store);  | 
401  | 0  |             return 1;  | 
402  | 0  |         }  | 
403  | 0  |     }  | 
404  | 0  |     ossl_property_unlock(store);  | 
405  | 0  |     return 0;  | 
406  | 0  | }  | 
407  |  |  | 
408  |  | struct alg_cleanup_by_provider_data_st { | 
409  |  |     OSSL_METHOD_STORE *store;  | 
410  |  |     const OSSL_PROVIDER *prov;  | 
411  |  | };  | 
412  |  |  | 
413  |  | static void  | 
414  |  | alg_cleanup_by_provider(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)  | 
415  | 0  | { | 
416  | 0  |     struct alg_cleanup_by_provider_data_st *data = arg;  | 
417  | 0  |     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  | 0  |     for (count = 0, i = sk_IMPLEMENTATION_num(alg->impls); i-- > 0;) { | 
424  | 0  |         IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);  | 
425  |  | 
  | 
426  | 0  |         if (impl->provider == data->prov) { | 
427  | 0  |             impl_free(impl);  | 
428  | 0  |             (void)sk_IMPLEMENTATION_delete(alg->impls, i);  | 
429  | 0  |             count++;  | 
430  | 0  |         }  | 
431  | 0  |     }  | 
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  | 0  |     if (count > 0)  | 
440  | 0  |         ossl_method_cache_flush_alg(data->store, alg);  | 
441  | 0  | }  | 
442  |  |  | 
443  |  | int ossl_method_store_remove_all_provided(OSSL_METHOD_STORE *store,  | 
444  |  |                                           const OSSL_PROVIDER *prov)  | 
445  | 0  | { | 
446  | 0  |     struct alg_cleanup_by_provider_data_st data;  | 
447  |  | 
  | 
448  | 0  |     if (!ossl_property_write_lock(store))  | 
449  | 0  |         return 0;  | 
450  | 0  |     data.prov = prov;  | 
451  | 0  |     data.store = store;  | 
452  | 0  |     ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup_by_provider, &data);  | 
453  | 0  |     ossl_property_unlock(store);  | 
454  | 0  |     return 1;  | 
455  | 0  | }  | 
456  |  |  | 
457  |  | static void alg_do_one(ALGORITHM *alg, IMPLEMENTATION *impl,  | 
458  |  |                        void (*fn)(int id, void *method, void *fnarg),  | 
459  |  |                        void *fnarg)  | 
460  | 0  | { | 
461  | 0  |     fn(alg->nid, impl->method.method, fnarg);  | 
462  | 0  | }  | 
463  |  |  | 
464  |  | struct alg_do_each_data_st { | 
465  |  |     void (*fn)(int id, void *method, void *fnarg);  | 
466  |  |     void *fnarg;  | 
467  |  | };  | 
468  |  |  | 
469  |  | static void alg_do_each(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)  | 
470  | 0  | { | 
471  | 0  |     struct alg_do_each_data_st *data = arg;  | 
472  | 0  |     int i, end = sk_IMPLEMENTATION_num(alg->impls);  | 
473  |  | 
  | 
474  | 0  |     for (i = 0; i < end; i++) { | 
475  | 0  |         IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);  | 
476  |  | 
  | 
477  | 0  |         alg_do_one(alg, impl, data->fn, data->fnarg);  | 
478  | 0  |     }  | 
479  | 0  | }  | 
480  |  |  | 
481  |  | void ossl_method_store_do_all(OSSL_METHOD_STORE *store,  | 
482  |  |                               void (*fn)(int id, void *method, void *fnarg),  | 
483  |  |                               void *fnarg)  | 
484  | 0  | { | 
485  | 0  |     struct alg_do_each_data_st data;  | 
486  |  | 
  | 
487  | 0  |     data.fn = fn;  | 
488  | 0  |     data.fnarg = fnarg;  | 
489  | 0  |     if (store != NULL)  | 
490  | 0  |         ossl_sa_ALGORITHM_doall_arg(store->algs, alg_do_each, &data);  | 
491  | 0  | }  | 
492  |  |  | 
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  | 85  | { | 
497  | 85  |     OSSL_PROPERTY_LIST **plp;  | 
498  | 85  |     ALGORITHM *alg;  | 
499  | 85  |     IMPLEMENTATION *impl, *best_impl = NULL;  | 
500  | 85  |     OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;  | 
501  | 85  |     const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL;  | 
502  | 85  |     int ret = 0;  | 
503  | 85  |     int j, best = -1, score, optional;  | 
504  |  |  | 
505  | 85  |     if (nid <= 0 || method == NULL || store == NULL)  | 
506  | 0  |         return 0;  | 
507  |  |  | 
508  | 85  | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)  | 
509  | 85  |     if (ossl_lib_ctx_is_default(store->ctx)  | 
510  | 85  |             && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))  | 
511  | 0  |         return 0;  | 
512  | 85  | #endif  | 
513  |  |  | 
514  |  |     /* This only needs to be a read lock, because the query won't create anything */  | 
515  | 85  |     if (!ossl_property_read_lock(store))  | 
516  | 0  |         return 0;  | 
517  | 85  |     alg = ossl_method_store_retrieve(store, nid);  | 
518  | 85  |     if (alg == NULL) { | 
519  | 0  |         ossl_property_unlock(store);  | 
520  | 0  |         return 0;  | 
521  | 0  |     }  | 
522  |  |  | 
523  | 85  |     if (prop_query != NULL)  | 
524  | 85  |         p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);  | 
525  | 85  |     plp = ossl_ctx_global_properties(store->ctx, 0);  | 
526  | 85  |     if (plp != NULL && *plp != NULL) { | 
527  | 0  |         if (pq == NULL) { | 
528  | 0  |             pq = *plp;  | 
529  | 0  |         } else { | 
530  | 0  |             p2 = ossl_property_merge(pq, *plp);  | 
531  | 0  |             ossl_property_free(pq);  | 
532  | 0  |             if (p2 == NULL)  | 
533  | 0  |                 goto fin;  | 
534  | 0  |             pq = p2;  | 
535  | 0  |         }  | 
536  | 0  |     }  | 
537  |  |  | 
538  | 85  |     if (pq == NULL) { | 
539  | 0  |         for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) { | 
540  | 0  |             if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL  | 
541  | 0  |                 && (prov == NULL || impl->provider == prov)) { | 
542  | 0  |                 best_impl = impl;  | 
543  | 0  |                 ret = 1;  | 
544  | 0  |                 break;  | 
545  | 0  |             }  | 
546  | 0  |         }  | 
547  | 0  |         goto fin;  | 
548  | 0  |     }  | 
549  | 85  |     optional = ossl_property_has_optional(pq);  | 
550  | 85  |     for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) { | 
551  | 85  |         if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL  | 
552  | 85  |             && (prov == NULL || impl->provider == prov)) { | 
553  | 85  |             score = ossl_property_match_count(pq, impl->properties);  | 
554  | 85  |             if (score > best) { | 
555  | 85  |                 best_impl = impl;  | 
556  | 85  |                 best = score;  | 
557  | 85  |                 ret = 1;  | 
558  | 85  |                 if (!optional)  | 
559  | 85  |                     goto fin;  | 
560  | 85  |             }  | 
561  | 85  |         }  | 
562  | 85  |     }  | 
563  | 85  | fin:  | 
564  | 85  |     if (ret && ossl_method_up_ref(&best_impl->method)) { | 
565  | 85  |         *method = best_impl->method.method;  | 
566  | 85  |         if (prov_rw != NULL)  | 
567  | 85  |             *prov_rw = best_impl->provider;  | 
568  | 85  |     } else { | 
569  | 0  |         ret = 0;  | 
570  | 0  |     }  | 
571  | 85  |     ossl_property_unlock(store);  | 
572  | 85  |     ossl_property_free(p2);  | 
573  | 85  |     return ret;  | 
574  | 85  | }  | 
575  |  |  | 
576  |  | static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,  | 
577  |  |                                         ALGORITHM *alg)  | 
578  | 0  | { | 
579  | 0  |     store->cache_nelem -= lh_QUERY_num_items(alg->cache);  | 
580  | 0  |     impl_cache_flush_alg(0, alg);  | 
581  | 0  | }  | 
582  |  |  | 
583  |  | static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)  | 
584  | 2.59k  | { | 
585  | 2.59k  |     ALGORITHM *alg = ossl_method_store_retrieve(store, nid);  | 
586  |  |  | 
587  | 2.59k  |     if (alg != NULL)  | 
588  | 0  |         ossl_method_cache_flush_alg(store, alg);  | 
589  | 2.59k  | }  | 
590  |  |  | 
591  |  | int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store)  | 
592  | 0  | { | 
593  | 0  |     if (!ossl_property_write_lock(store))  | 
594  | 0  |         return 0;  | 
595  | 0  |     ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);  | 
596  | 0  |     store->cache_nelem = 0;  | 
597  | 0  |     ossl_property_unlock(store);  | 
598  | 0  |     return 1;  | 
599  | 0  | }  | 
600  |  |  | 
601  |  | IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);  | 
602  |  |  | 
603  |  | /*  | 
604  |  |  * Flush an element from the query cache (perhaps).  | 
605  |  |  *  | 
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.  | 
610  |  |  *  | 
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  | 
617  |  |  * impact.  | 
618  |  |  */  | 
619  |  | static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)  | 
620  | 0  | { | 
621  | 0  |     uint32_t n;  | 
622  |  |  | 
623  |  |     /*  | 
624  |  |      * Implement the 32 bit xorshift as suggested by George Marsaglia in:  | 
625  |  |      *      https://doi.org/10.18637/jss.v008.i14  | 
626  |  |      *  | 
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.  | 
629  |  |      */  | 
630  | 0  |     n = state->seed;  | 
631  | 0  |     n ^= n << 13;  | 
632  | 0  |     n ^= n >> 17;  | 
633  | 0  |     n ^= n << 5;  | 
634  | 0  |     state->seed = n;  | 
635  |  | 
  | 
636  | 0  |     if ((n & 1) != 0)  | 
637  | 0  |         impl_cache_free(lh_QUERY_delete(state->cache, c));  | 
638  | 0  |     else  | 
639  | 0  |         state->nelem++;  | 
640  | 0  | }  | 
641  |  |  | 
642  |  | static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,  | 
643  |  |                                      void *v)  | 
644  | 0  | { | 
645  | 0  |     IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;  | 
646  |  | 
  | 
647  | 0  |     state->cache = alg->cache;  | 
648  | 0  |     lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,  | 
649  | 0  |                                     state);  | 
650  | 0  | }  | 
651  |  |  | 
652  |  | static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)  | 
653  | 0  | { | 
654  | 0  |     IMPL_CACHE_FLUSH state;  | 
655  | 0  |     static TSAN_QUALIFIER uint32_t global_seed = 1;  | 
656  |  | 
  | 
657  | 0  |     state.nelem = 0;  | 
658  | 0  |     state.using_global_seed = 0;  | 
659  | 0  |     if ((state.seed = OPENSSL_rdtsc()) == 0) { | 
660  |  |         /* If there is no timer available, seed another way */  | 
661  | 0  |         state.using_global_seed = 1;  | 
662  | 0  |         state.seed = tsan_load(&global_seed);  | 
663  | 0  |     }  | 
664  | 0  |     store->cache_need_flush = 0;  | 
665  | 0  |     ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);  | 
666  | 0  |     store->cache_nelem = state.nelem;  | 
667  |  |     /* Without a timer, update the global seed */  | 
668  | 0  |     if (state.using_global_seed)  | 
669  | 0  |         tsan_add(&global_seed, state.seed);  | 
670  | 0  | }  | 
671  |  |  | 
672  |  | int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,  | 
673  |  |                                 int nid, const char *prop_query, void **method)  | 
674  | 82.3k  | { | 
675  | 82.3k  |     ALGORITHM *alg;  | 
676  | 82.3k  |     QUERY elem, *r;  | 
677  | 82.3k  |     int res = 0;  | 
678  |  |  | 
679  | 82.3k  |     if (nid <= 0 || store == NULL || prop_query == NULL)  | 
680  | 0  |         return 0;  | 
681  |  |  | 
682  | 82.3k  |     if (!ossl_property_read_lock(store))  | 
683  | 0  |         return 0;  | 
684  | 82.3k  |     alg = ossl_method_store_retrieve(store, nid);  | 
685  | 82.3k  |     if (alg == NULL)  | 
686  | 16  |         goto err;  | 
687  |  |  | 
688  | 82.3k  |     elem.query = prop_query;  | 
689  | 82.3k  |     elem.provider = prov;  | 
690  | 82.3k  |     r = lh_QUERY_retrieve(alg->cache, &elem);  | 
691  | 82.3k  |     if (r == NULL)  | 
692  | 37  |         goto err;  | 
693  | 82.2k  |     if (ossl_method_up_ref(&r->method)) { | 
694  | 82.2k  |         *method = r->method.method;  | 
695  | 82.2k  |         res = 1;  | 
696  | 82.2k  |     }  | 
697  | 82.3k  | err:  | 
698  | 82.3k  |     ossl_property_unlock(store);  | 
699  | 82.3k  |     return res;  | 
700  | 82.2k  | }  | 
701  |  |  | 
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 *))  | 
706  | 85  | { | 
707  | 85  |     QUERY elem, *old, *p = NULL;  | 
708  | 85  |     ALGORITHM *alg;  | 
709  | 85  |     size_t len;  | 
710  | 85  |     int res = 1;  | 
711  |  |  | 
712  | 85  |     if (nid <= 0 || store == NULL || prop_query == NULL)  | 
713  | 0  |         return 0;  | 
714  |  |  | 
715  | 85  |     if (!ossl_assert(prov != NULL))  | 
716  | 0  |         return 0;  | 
717  |  |  | 
718  | 85  |     if (!ossl_property_write_lock(store))  | 
719  | 0  |         return 0;  | 
720  | 85  |     if (store->cache_need_flush)  | 
721  | 0  |         ossl_method_cache_flush_some(store);  | 
722  | 85  |     alg = ossl_method_store_retrieve(store, nid);  | 
723  | 85  |     if (alg == NULL)  | 
724  | 0  |         goto err;  | 
725  |  |  | 
726  | 85  |     if (method == NULL) { | 
727  | 0  |         elem.query = prop_query;  | 
728  | 0  |         elem.provider = prov;  | 
729  | 0  |         if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) { | 
730  | 0  |             impl_cache_free(old);  | 
731  | 0  |             store->cache_nelem--;  | 
732  | 0  |         }  | 
733  | 0  |         goto end;  | 
734  | 0  |     }  | 
735  | 85  |     p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));  | 
736  | 85  |     if (p != NULL) { | 
737  | 85  |         p->query = p->body;  | 
738  | 85  |         p->provider = prov;  | 
739  | 85  |         p->method.method = method;  | 
740  | 85  |         p->method.up_ref = method_up_ref;  | 
741  | 85  |         p->method.free = method_destruct;  | 
742  | 85  |         if (!ossl_method_up_ref(&p->method))  | 
743  | 0  |             goto err;  | 
744  | 85  |         memcpy((char *)p->query, prop_query, len + 1);  | 
745  | 85  |         if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) { | 
746  | 0  |             impl_cache_free(old);  | 
747  | 0  |             goto end;  | 
748  | 0  |         }  | 
749  | 85  |         if (!lh_QUERY_error(alg->cache)) { | 
750  | 85  |             if (++store->cache_nelem >= IMPL_CACHE_FLUSH_THRESHOLD)  | 
751  | 0  |                 store->cache_need_flush = 1;  | 
752  | 85  |             goto end;  | 
753  | 85  |         }  | 
754  | 0  |         ossl_method_free(&p->method);  | 
755  | 0  |     }  | 
756  | 0  | err:  | 
757  | 0  |     res = 0;  | 
758  | 0  |     OPENSSL_free(p);  | 
759  | 85  | end:  | 
760  | 85  |     ossl_property_unlock(store);  | 
761  | 85  |     return res;  | 
762  | 0  | }  |