/src/openssl/crypto/property/property.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2019-2025 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 <openssl/trace.h>  | 
23  |  | #include "internal/thread_once.h"  | 
24  |  | #include "crypto/lhash.h"  | 
25  |  | #include "crypto/sparse_array.h"  | 
26  |  | #include "property_local.h"  | 
27  |  | #include "crypto/context.h"  | 
28  |  |  | 
29  |  | /*  | 
30  |  |  * The number of elements in the query cache before we initiate a flush.  | 
31  |  |  * If reducing this, also ensure the stochastic test in test/property_test.c  | 
32  |  |  * isn't likely to fail.  | 
33  |  |  */  | 
34  | 85  | #define IMPL_CACHE_FLUSH_THRESHOLD  500  | 
35  |  |  | 
36  |  | typedef struct { | 
37  |  |     void *method;  | 
38  |  |     int (*up_ref)(void *);  | 
39  |  |     void (*free)(void *);  | 
40  |  | } METHOD;  | 
41  |  |  | 
42  |  | typedef struct { | 
43  |  |     const OSSL_PROVIDER *provider;  | 
44  |  |     OSSL_PROPERTY_LIST *properties;  | 
45  |  |     METHOD method;  | 
46  |  | } IMPLEMENTATION;  | 
47  |  |  | 
48  |  | DEFINE_STACK_OF(IMPLEMENTATION)  | 
49  |  |  | 
50  |  | typedef struct { | 
51  |  |     const OSSL_PROVIDER *provider;  | 
52  |  |     const char *query;  | 
53  |  |     METHOD method;  | 
54  |  |     char body[1];  | 
55  |  | } QUERY;  | 
56  |  |  | 
57  |  | DEFINE_LHASH_OF_EX(QUERY);  | 
58  |  |  | 
59  |  | typedef struct { | 
60  |  |     int nid;  | 
61  |  |     STACK_OF(IMPLEMENTATION) *impls;  | 
62  |  |     LHASH_OF(QUERY) *cache;  | 
63  |  | } ALGORITHM;  | 
64  |  |  | 
65  |  | struct ossl_method_store_st { | 
66  |  |     OSSL_LIB_CTX *ctx;  | 
67  |  |     SPARSE_ARRAY_OF(ALGORITHM) *algs;  | 
68  |  |     /*  | 
69  |  |      * Lock to protect the |algs| array from concurrent writing, when  | 
70  |  |      * individual implementations or queries are inserted.  This is used  | 
71  |  |      * by the appropriate functions here.  | 
72  |  |      */  | 
73  |  |     CRYPTO_RWLOCK *lock;  | 
74  |  |     /*  | 
75  |  |      * Lock to reserve the whole store.  This is used when fetching a set  | 
76  |  |      * of algorithms, via these functions, found in crypto/core_fetch.c:  | 
77  |  |      * ossl_method_construct_reserve_store()  | 
78  |  |      * ossl_method_construct_unreserve_store()  | 
79  |  |      */  | 
80  |  |     CRYPTO_RWLOCK *biglock;  | 
81  |  |  | 
82  |  |     /* query cache specific values */  | 
83  |  |  | 
84  |  |     /* Count of the query cache entries for all algs */  | 
85  |  |     size_t cache_nelem;  | 
86  |  |  | 
87  |  |     /* Flag: 1 if query cache entries for all algs need flushing */  | 
88  |  |     int cache_need_flush;  | 
89  |  | };  | 
90  |  |  | 
91  |  | typedef struct { | 
92  |  |     LHASH_OF(QUERY) *cache;  | 
93  |  |     size_t nelem;  | 
94  |  |     uint32_t seed;  | 
95  |  |     unsigned char using_global_seed;  | 
96  |  | } IMPL_CACHE_FLUSH;  | 
97  |  |  | 
98  |  | DEFINE_SPARSE_ARRAY_OF(ALGORITHM);  | 
99  |  |  | 
100  |  | DEFINE_STACK_OF(ALGORITHM)  | 
101  |  |  | 
102  |  | typedef struct ossl_global_properties_st { | 
103  |  |     OSSL_PROPERTY_LIST *list;  | 
104  |  | #ifndef FIPS_MODULE  | 
105  |  |     unsigned int no_mirrored : 1;  | 
106  |  | #endif  | 
107  |  | } OSSL_GLOBAL_PROPERTIES;  | 
108  |  |  | 
109  |  | static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,  | 
110  |  |                                         ALGORITHM *alg);  | 
111  |  | static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);  | 
112  |  |  | 
113  |  | /* Global properties are stored per library context */  | 
114  |  | void ossl_ctx_global_properties_free(void *vglobp)  | 
115  | 16  | { | 
116  | 16  |     OSSL_GLOBAL_PROPERTIES *globp = vglobp;  | 
117  |  |  | 
118  | 16  |     if (globp != NULL) { | 
119  | 16  |         ossl_property_free(globp->list);  | 
120  | 16  |         OPENSSL_free(globp);  | 
121  | 16  |     }  | 
122  | 16  | }  | 
123  |  |  | 
124  |  | void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)  | 
125  | 16  | { | 
126  | 16  |     return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES));  | 
127  | 16  | }  | 
128  |  |  | 
129  |  | OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,  | 
130  |  |                                                 ossl_unused int loadconfig)  | 
131  | 85  | { | 
132  | 85  |     OSSL_GLOBAL_PROPERTIES *globp;  | 
133  |  |  | 
134  | 85  | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)  | 
135  | 85  |     if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))  | 
136  | 0  |         return NULL;  | 
137  | 85  | #endif  | 
138  | 85  |     globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);  | 
139  |  |  | 
140  | 85  |     return globp != NULL ? &globp->list : NULL;  | 
141  | 85  | }  | 
142  |  |  | 
143  |  | #ifndef FIPS_MODULE  | 
144  |  | int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx)  | 
145  | 0  | { | 
146  | 0  |     OSSL_GLOBAL_PROPERTIES *globp  | 
147  | 0  |         = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);  | 
148  |  | 
  | 
149  | 0  |     return globp != NULL && globp->no_mirrored ? 1 : 0;  | 
150  | 0  | }  | 
151  |  |  | 
152  |  | void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx)  | 
153  | 0  | { | 
154  | 0  |     OSSL_GLOBAL_PROPERTIES *globp  | 
155  | 0  |         = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);  | 
156  |  | 
  | 
157  | 0  |     if (globp != NULL)  | 
158  | 0  |         globp->no_mirrored = 1;  | 
159  | 0  | }  | 
160  |  | #endif  | 
161  |  |  | 
162  |  | static int ossl_method_up_ref(METHOD *method)  | 
163  | 95.9k  | { | 
164  | 95.9k  |     return (*method->up_ref)(method->method);  | 
165  | 95.9k  | }  | 
166  |  |  | 
167  |  | static void ossl_method_free(METHOD *method)  | 
168  | 2.67k  | { | 
169  | 2.67k  |     (*method->free)(method->method);  | 
170  | 2.67k  | }  | 
171  |  |  | 
172  |  | static __owur int ossl_property_read_lock(OSSL_METHOD_STORE *p)  | 
173  | 93.2k  | { | 
174  | 93.2k  |     return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;  | 
175  | 93.2k  | }  | 
176  |  |  | 
177  |  | static __owur int ossl_property_write_lock(OSSL_METHOD_STORE *p)  | 
178  | 2.67k  | { | 
179  | 2.67k  |     return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;  | 
180  | 2.67k  | }  | 
181  |  |  | 
182  |  | static int ossl_property_unlock(OSSL_METHOD_STORE *p)  | 
183  | 95.9k  | { | 
184  | 95.9k  |     return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;  | 
185  | 95.9k  | }  | 
186  |  |  | 
187  |  | static unsigned long query_hash(const QUERY *a)  | 
188  | 93.2k  | { | 
189  | 93.2k  |     return OPENSSL_LH_strhash(a->query);  | 
190  | 93.2k  | }  | 
191  |  |  | 
192  |  | static int query_cmp(const QUERY *a, const QUERY *b)  | 
193  | 93.1k  | { | 
194  | 93.1k  |     int res = strcmp(a->query, b->query);  | 
195  |  |  | 
196  | 93.1k  |     if (res == 0 && a->provider != NULL && b->provider != NULL)  | 
197  | 32  |         res = b->provider > a->provider ? 1  | 
198  | 32  |             : b->provider < a->provider ? -1  | 
199  | 32  |             : 0;  | 
200  | 93.1k  |     return res;  | 
201  | 93.1k  | }  | 
202  |  |  | 
203  |  | static void impl_free(IMPLEMENTATION *impl)  | 
204  | 2.59k  | { | 
205  | 2.59k  |     if (impl != NULL) { | 
206  | 2.59k  |         ossl_method_free(&impl->method);  | 
207  | 2.59k  |         OPENSSL_free(impl);  | 
208  | 2.59k  |     }  | 
209  | 2.59k  | }  | 
210  |  |  | 
211  |  | static void impl_cache_free(QUERY *elem)  | 
212  | 85  | { | 
213  | 85  |     if (elem != NULL) { | 
214  | 85  |         ossl_method_free(&elem->method);  | 
215  | 85  |         OPENSSL_free(elem);  | 
216  | 85  |     }  | 
217  | 85  | }  | 
218  |  |  | 
219  |  | static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)  | 
220  | 0  | { | 
221  | 0  |     lh_QUERY_doall(alg->cache, &impl_cache_free);  | 
222  | 0  |     lh_QUERY_flush(alg->cache);  | 
223  | 0  | }  | 
224  |  |  | 
225  |  | static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a, void *arg)  | 
226  | 2.59k  | { | 
227  | 2.59k  |     OSSL_METHOD_STORE *store = arg;  | 
228  |  |  | 
229  | 2.59k  |     if (a != NULL) { | 
230  | 2.59k  |         sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);  | 
231  | 2.59k  |         lh_QUERY_doall(a->cache, &impl_cache_free);  | 
232  | 2.59k  |         lh_QUERY_free(a->cache);  | 
233  | 2.59k  |         OPENSSL_free(a);  | 
234  | 2.59k  |     }  | 
235  | 2.59k  |     if (store != NULL)  | 
236  | 2.59k  |         ossl_sa_ALGORITHM_set(store->algs, idx, NULL);  | 
237  | 2.59k  | }  | 
238  |  |  | 
239  |  | /*  | 
240  |  |  * The OSSL_LIB_CTX param here allows access to underlying property data needed  | 
241  |  |  * for computation  | 
242  |  |  */  | 
243  |  | OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx)  | 
244  | 64  | { | 
245  | 64  |     OSSL_METHOD_STORE *res;  | 
246  |  |  | 
247  | 64  |     res = OPENSSL_zalloc(sizeof(*res));  | 
248  | 64  |     if (res != NULL) { | 
249  | 64  |         res->ctx = ctx;  | 
250  | 64  |         if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL  | 
251  | 64  |             || (res->lock = CRYPTO_THREAD_lock_new()) == NULL  | 
252  | 64  |             || (res->biglock = CRYPTO_THREAD_lock_new()) == NULL) { | 
253  | 0  |             ossl_method_store_free(res);  | 
254  | 0  |             return NULL;  | 
255  | 0  |         }  | 
256  | 64  |     }  | 
257  | 64  |     return res;  | 
258  | 64  | }  | 
259  |  |  | 
260  |  | void ossl_method_store_free(OSSL_METHOD_STORE *store)  | 
261  | 64  | { | 
262  | 64  |     if (store != NULL) { | 
263  | 64  |         if (store->algs != NULL)  | 
264  | 64  |             ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup, store);  | 
265  | 64  |         ossl_sa_ALGORITHM_free(store->algs);  | 
266  | 64  |         CRYPTO_THREAD_lock_free(store->lock);  | 
267  | 64  |         CRYPTO_THREAD_lock_free(store->biglock);  | 
268  | 64  |         OPENSSL_free(store);  | 
269  | 64  |     }  | 
270  | 64  | }  | 
271  |  |  | 
272  |  | int ossl_method_lock_store(OSSL_METHOD_STORE *store)  | 
273  | 85  | { | 
274  | 85  |     return store != NULL ? CRYPTO_THREAD_write_lock(store->biglock) : 0;  | 
275  | 85  | }  | 
276  |  |  | 
277  |  | int ossl_method_unlock_store(OSSL_METHOD_STORE *store)  | 
278  | 85  | { | 
279  | 85  |     return store != NULL ? CRYPTO_THREAD_unlock(store->biglock) : 0;  | 
280  | 85  | }  | 
281  |  |  | 
282  |  | static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)  | 
283  | 98.5k  | { | 
284  | 98.5k  |     return ossl_sa_ALGORITHM_get(store->algs, nid);  | 
285  | 98.5k  | }  | 
286  |  |  | 
287  |  | static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)  | 
288  | 2.59k  | { | 
289  | 2.59k  |     return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);  | 
290  | 2.59k  | }  | 
291  |  |  | 
292  |  | /**  | 
293  |  |  * @brief Adds a method to the specified method store.  | 
294  |  |  *  | 
295  |  |  * This function adds a new method to the provided method store, associating it  | 
296  |  |  * with a specified id, properties, and provider. The method is stored with  | 
297  |  |  * reference count and destruction callbacks.  | 
298  |  |  *  | 
299  |  |  * @param store Pointer to the OSSL_METHOD_STORE where the method will be added.  | 
300  |  |  *              Must be non-null.  | 
301  |  |  * @param prov Pointer to the OSSL_PROVIDER for the provider of the method.  | 
302  |  |  *             Must be non-null.  | 
303  |  |  * @param nid (identifier) associated with the method, must be > 0  | 
304  |  |  * @param properties String containing properties of the method.  | 
305  |  |  * @param method Pointer to the method to be added.  | 
306  |  |  * @param method_up_ref Function pointer for incrementing the method ref count.  | 
307  |  |  * @param method_destruct Function pointer for destroying the method.  | 
308  |  |  *  | 
309  |  |  * @return 1 if the method is successfully added, 0 on failure.  | 
310  |  |  *  | 
311  |  |  * If tracing is enabled, a message is printed indicating that the method is  | 
312  |  |  * being added to the method store.  | 
313  |  |  *  | 
314  |  |  * NOTE: The nid parameter here is _not_ a nid in the sense of the NID_* macros.  | 
315  |  |  * It is an internal unique identifier.  | 
316  |  |  */  | 
317  |  | int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,  | 
318  |  |                           int nid, const char *properties, void *method,  | 
319  |  |                           int (*method_up_ref)(void *),  | 
320  |  |                           void (*method_destruct)(void *))  | 
321  | 2.59k  | { | 
322  | 2.59k  |     ALGORITHM *alg = NULL;  | 
323  | 2.59k  |     IMPLEMENTATION *impl;  | 
324  | 2.59k  |     int ret = 0;  | 
325  | 2.59k  |     int i;  | 
326  |  |  | 
327  | 2.59k  |     if (nid <= 0 || method == NULL || store == NULL)  | 
328  | 0  |         return 0;  | 
329  |  |  | 
330  | 2.59k  |     if (properties == NULL)  | 
331  | 0  |         properties = "";  | 
332  |  |  | 
333  | 2.59k  |     if (!ossl_assert(prov != NULL))  | 
334  | 0  |         return 0;  | 
335  |  |  | 
336  |  |     /* Create new entry */  | 
337  | 2.59k  |     impl = OPENSSL_malloc(sizeof(*impl));  | 
338  | 2.59k  |     if (impl == NULL)  | 
339  | 0  |         return 0;  | 
340  | 2.59k  |     impl->method.method = method;  | 
341  | 2.59k  |     impl->method.up_ref = method_up_ref;  | 
342  | 2.59k  |     impl->method.free = method_destruct;  | 
343  | 2.59k  |     if (!ossl_method_up_ref(&impl->method)) { | 
344  | 0  |         OPENSSL_free(impl);  | 
345  | 0  |         return 0;  | 
346  | 0  |     }  | 
347  | 2.59k  |     impl->provider = prov;  | 
348  |  |  | 
349  |  |     /* Insert into the hash table if required */  | 
350  | 2.59k  |     if (!ossl_property_write_lock(store)) { | 
351  | 0  |         impl_free(impl);  | 
352  | 0  |         return 0;  | 
353  | 0  |     }  | 
354  |  |  | 
355  |  |     /*  | 
356  |  |      * Flush the alg cache of any implementation that already exists  | 
357  |  |      * for this id.  | 
358  |  |      * This is done to ensure that on the next lookup we go through the  | 
359  |  |      * provider comparison in ossl_method_store_fetch.  If we don't do this  | 
360  |  |      * then this new method won't be given a chance to get selected.  | 
361  |  |      * NOTE: This doesn't actually remove the method from the backing store  | 
362  |  |      * It just ensures that we query the backing store when (re)-adding a  | 
363  |  |      * method to the algorithm cache, in case the one selected by the next  | 
364  |  |      * query selects a different implementation  | 
365  |  |      */  | 
366  | 2.59k  |     ossl_method_cache_flush(store, nid);  | 
367  |  |  | 
368  |  |     /*  | 
369  |  |      * Parse the properties associated with this method, and convert it to a  | 
370  |  |      * property list stored against the implementation for later comparison  | 
371  |  |      * during fetch operations  | 
372  |  |      */  | 
373  | 2.59k  |     if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) { | 
374  | 16  |         impl->properties = ossl_parse_property(store->ctx, properties);  | 
375  | 16  |         if (impl->properties == NULL)  | 
376  | 0  |             goto err;  | 
377  | 16  |         if (!ossl_prop_defn_set(store->ctx, properties, &impl->properties)) { | 
378  | 0  |             ossl_property_free(impl->properties);  | 
379  | 0  |             impl->properties = NULL;  | 
380  | 0  |             goto err;  | 
381  | 0  |         }  | 
382  | 16  |     }  | 
383  |  |  | 
384  |  |     /*  | 
385  |  |      * Check if we have an algorithm cache already for this nid.  If so use  | 
386  |  |      * it, otherwise, create it, and insert it into the store  | 
387  |  |      */  | 
388  | 2.59k  |     alg = ossl_method_store_retrieve(store, nid);  | 
389  | 2.59k  |     if (alg == NULL) { | 
390  | 2.59k  |         if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL  | 
391  | 2.59k  |                 || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL  | 
392  | 2.59k  |                 || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)  | 
393  | 0  |             goto err;  | 
394  | 2.59k  |         alg->nid = nid;  | 
395  | 2.59k  |         if (!ossl_method_store_insert(store, alg))  | 
396  | 0  |             goto err;  | 
397  | 2.59k  |         OSSL_TRACE2(QUERY, "Inserted an alg with nid %d into the store %p\n", nid, (void *)store);  | 
398  | 2.59k  |     }  | 
399  |  |  | 
400  |  |     /* Push onto stack if there isn't one there already */  | 
401  | 2.59k  |     for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) { | 
402  | 0  |         const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);  | 
403  |  | 
  | 
404  | 0  |         if (tmpimpl->provider == impl->provider  | 
405  | 0  |             && tmpimpl->properties == impl->properties)  | 
406  | 0  |             break;  | 
407  | 0  |     }  | 
408  |  |  | 
409  | 2.59k  |     if (i == sk_IMPLEMENTATION_num(alg->impls)  | 
410  | 2.59k  |         && sk_IMPLEMENTATION_push(alg->impls, impl)) { | 
411  | 2.59k  |         ret = 1;  | 
412  | 2.59k  | #ifndef FIPS_MODULE  | 
413  | 2.59k  |         OSSL_TRACE_BEGIN(QUERY) { | 
414  | 0  |             BIO_printf(trc_out, "Adding to method store "  | 
415  | 0  |                        "nid: %d\nproperties: %s\nprovider: %s\n",  | 
416  | 0  |                        nid, properties,  | 
417  | 0  |                        ossl_provider_name(prov) == NULL ? "none" :  | 
418  | 0  |                        ossl_provider_name(prov));  | 
419  | 2.59k  |         } OSSL_TRACE_END(QUERY);  | 
420  | 2.59k  | #endif  | 
421  | 2.59k  |     }  | 
422  | 2.59k  |     ossl_property_unlock(store);  | 
423  | 2.59k  |     if (ret == 0)  | 
424  | 0  |         impl_free(impl);  | 
425  | 2.59k  |     return ret;  | 
426  |  |  | 
427  | 0  | err:  | 
428  | 0  |     ossl_property_unlock(store);  | 
429  | 0  |     alg_cleanup(0, alg, NULL);  | 
430  | 0  |     impl_free(impl);  | 
431  | 0  |     return 0;  | 
432  | 2.59k  | }  | 
433  |  |  | 
434  |  | int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,  | 
435  |  |                              const void *method)  | 
436  | 0  | { | 
437  | 0  |     ALGORITHM *alg = NULL;  | 
438  | 0  |     int i;  | 
439  |  | 
  | 
440  | 0  |     if (nid <= 0 || method == NULL || store == NULL)  | 
441  | 0  |         return 0;  | 
442  |  |  | 
443  | 0  |     if (!ossl_property_write_lock(store))  | 
444  | 0  |         return 0;  | 
445  | 0  |     ossl_method_cache_flush(store, nid);  | 
446  | 0  |     alg = ossl_method_store_retrieve(store, nid);  | 
447  | 0  |     if (alg == NULL) { | 
448  | 0  |         ossl_property_unlock(store);  | 
449  | 0  |         return 0;  | 
450  | 0  |     }  | 
451  |  |  | 
452  |  |     /*  | 
453  |  |      * A sorting find then a delete could be faster but these stacks should be  | 
454  |  |      * relatively small, so we avoid the overhead.  Sorting could also surprise  | 
455  |  |      * users when result orderings change (even though they are not guaranteed).  | 
456  |  |      */  | 
457  | 0  |     for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) { | 
458  | 0  |         IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);  | 
459  |  | 
  | 
460  | 0  |         if (impl->method.method == method) { | 
461  | 0  |             impl_free(impl);  | 
462  | 0  |             (void)sk_IMPLEMENTATION_delete(alg->impls, i);  | 
463  | 0  |             ossl_property_unlock(store);  | 
464  | 0  |             return 1;  | 
465  | 0  |         }  | 
466  | 0  |     }  | 
467  | 0  |     ossl_property_unlock(store);  | 
468  | 0  |     return 0;  | 
469  | 0  | }  | 
470  |  |  | 
471  |  | struct alg_cleanup_by_provider_data_st { | 
472  |  |     OSSL_METHOD_STORE *store;  | 
473  |  |     const OSSL_PROVIDER *prov;  | 
474  |  | };  | 
475  |  |  | 
476  |  | /**  | 
477  |  |  * @brief Cleans up implementations of an algorithm associated with a provider.  | 
478  |  |  *  | 
479  |  |  * This function removes all implementations of a specified algorithm that are  | 
480  |  |  * associated with a given provider. The function walks through the stack of  | 
481  |  |  * implementations backwards to handle deletions without affecting indexing.  | 
482  |  |  *  | 
483  |  |  * @param idx Index of the algorithm (unused in this function).  | 
484  |  |  * @param alg Pointer to the ALGORITHM structure containing the implementations.  | 
485  |  |  * @param arg Pointer to the data containing the provider information.  | 
486  |  |  *  | 
487  |  |  * If tracing is enabled, messages are printed indicating the removal of each  | 
488  |  |  * implementation and its properties. If any implementation is removed, the  | 
489  |  |  * associated cache is flushed.  | 
490  |  |  */  | 
491  |  | static void  | 
492  |  | alg_cleanup_by_provider(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)  | 
493  | 0  | { | 
494  | 0  |     struct alg_cleanup_by_provider_data_st *data = arg;  | 
495  | 0  |     int i, count;  | 
496  |  |  | 
497  |  |     /*  | 
498  |  |      * We walk the stack backwards, to avoid having to deal with stack shifts  | 
499  |  |      * caused by deletion  | 
500  |  |      */  | 
501  | 0  |     for (count = 0, i = sk_IMPLEMENTATION_num(alg->impls); i-- > 0;) { | 
502  | 0  |         IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);  | 
503  |  | 
  | 
504  | 0  |         if (impl->provider == data->prov) { | 
505  | 0  | #ifndef FIPS_MODULE  | 
506  | 0  |             OSSL_TRACE_BEGIN(QUERY) { | 
507  | 0  |                 char buf[512];  | 
508  | 0  |                 size_t size;  | 
509  |  | 
  | 
510  | 0  |                 size = ossl_property_list_to_string(NULL, impl->properties, buf,  | 
511  | 0  |                                                     sizeof(buf));  | 
512  | 0  |                 BIO_printf(trc_out, "Removing implementation from "  | 
513  | 0  |                            "query cache\nproperties %s\nprovider %s\n",  | 
514  | 0  |                            size == 0 ? "none" : buf,  | 
515  | 0  |                            ossl_provider_name(impl->provider) == NULL ? "none" :  | 
516  | 0  |                            ossl_provider_name(impl->provider));  | 
517  | 0  |             } OSSL_TRACE_END(QUERY);  | 
518  | 0  | #endif  | 
519  |  | 
  | 
520  | 0  |             (void)sk_IMPLEMENTATION_delete(alg->impls, i);  | 
521  | 0  |             count++;  | 
522  | 0  |             impl_free(impl);  | 
523  | 0  |         }  | 
524  | 0  |     }  | 
525  |  |  | 
526  |  |     /*  | 
527  |  |      * If we removed any implementation, we also clear the whole associated  | 
528  |  |      * cache, 'cause that's the sensible thing to do.  | 
529  |  |      * There's no point flushing the cache entries where we didn't remove  | 
530  |  |      * any implementation, though.  | 
531  |  |      */  | 
532  | 0  |     if (count > 0)  | 
533  | 0  |         ossl_method_cache_flush_alg(data->store, alg);  | 
534  | 0  | }  | 
535  |  |  | 
536  |  | int ossl_method_store_remove_all_provided(OSSL_METHOD_STORE *store,  | 
537  |  |                                           const OSSL_PROVIDER *prov)  | 
538  | 0  | { | 
539  | 0  |     struct alg_cleanup_by_provider_data_st data;  | 
540  |  | 
  | 
541  | 0  |     if (!ossl_property_write_lock(store))  | 
542  | 0  |         return 0;  | 
543  | 0  |     data.prov = prov;  | 
544  | 0  |     data.store = store;  | 
545  | 0  |     ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup_by_provider, &data);  | 
546  | 0  |     ossl_property_unlock(store);  | 
547  | 0  |     return 1;  | 
548  | 0  | }  | 
549  |  |  | 
550  |  | static void alg_do_one(ALGORITHM *alg, IMPLEMENTATION *impl,  | 
551  |  |                        void (*fn)(int id, void *method, void *fnarg),  | 
552  |  |                        void *fnarg)  | 
553  | 0  | { | 
554  | 0  |     fn(alg->nid, impl->method.method, fnarg);  | 
555  | 0  | }  | 
556  |  |  | 
557  |  | static void alg_copy(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)  | 
558  | 0  | { | 
559  | 0  |     STACK_OF(ALGORITHM) *newalg = arg;  | 
560  |  | 
  | 
561  | 0  |     alg = OPENSSL_memdup(alg, sizeof(ALGORITHM));  | 
562  | 0  |     if (alg == NULL)  | 
563  | 0  |         return;  | 
564  |  |  | 
565  | 0  |     alg->impls = sk_IMPLEMENTATION_dup(alg->impls);  | 
566  |  | 
  | 
567  | 0  |     (void)sk_ALGORITHM_push(newalg, alg);  | 
568  | 0  | }  | 
569  |  |  | 
570  |  | static void del_tmpalg(ALGORITHM *alg)  | 
571  | 0  | { | 
572  | 0  |     sk_IMPLEMENTATION_free(alg->impls);  | 
573  | 0  |     OPENSSL_free(alg);  | 
574  | 0  | }  | 
575  |  |  | 
576  |  | void ossl_method_store_do_all(OSSL_METHOD_STORE *store,  | 
577  |  |                               void (*fn)(int id, void *method, void *fnarg),  | 
578  |  |                               void *fnarg)  | 
579  | 0  | { | 
580  | 0  |     int i, j;  | 
581  | 0  |     int numalgs, numimps;  | 
582  | 0  |     STACK_OF(ALGORITHM) *tmpalgs;  | 
583  | 0  |     ALGORITHM *alg;  | 
584  |  | 
  | 
585  | 0  |     if (store != NULL) { | 
586  |  | 
  | 
587  | 0  |         if (!ossl_property_read_lock(store))  | 
588  | 0  |             return;  | 
589  |  |  | 
590  | 0  |         tmpalgs = sk_ALGORITHM_new_reserve(NULL,  | 
591  | 0  |                                            (int)ossl_sa_ALGORITHM_num(store->algs));  | 
592  | 0  |         if (tmpalgs == NULL) { | 
593  | 0  |             ossl_property_unlock(store);  | 
594  | 0  |             return;  | 
595  | 0  |         }  | 
596  |  |  | 
597  | 0  |         ossl_sa_ALGORITHM_doall_arg(store->algs, alg_copy, tmpalgs);  | 
598  | 0  |         ossl_property_unlock(store);  | 
599  | 0  |         numalgs = sk_ALGORITHM_num(tmpalgs);  | 
600  | 0  |         for (i = 0; i < numalgs; i++) { | 
601  | 0  |             alg = sk_ALGORITHM_value(tmpalgs, i);  | 
602  | 0  |             numimps = sk_IMPLEMENTATION_num(alg->impls);  | 
603  | 0  |             for (j = 0; j < numimps; j++)  | 
604  | 0  |                 alg_do_one(alg, sk_IMPLEMENTATION_value(alg->impls, j), fn, fnarg);  | 
605  | 0  |         }  | 
606  | 0  |         sk_ALGORITHM_pop_free(tmpalgs, del_tmpalg);  | 
607  | 0  |     }  | 
608  | 0  | }  | 
609  |  |  | 
610  |  | /**  | 
611  |  |  * @brief Fetches a method from the method store matching the given properties.  | 
612  |  |  *  | 
613  |  |  * This function searches the method store for an implementation of a specified  | 
614  |  |  * method, identified by its id (nid), and matching the given property query. If  | 
615  |  |  * successful, it returns the method and its associated provider.  | 
616  |  |  *  | 
617  |  |  * @param store Pointer to the OSSL_METHOD_STORE from which to fetch the method.  | 
618  |  |  *              Must be non-null.  | 
619  |  |  * @param nid (identifier) of the method to be fetched. Must be > 0  | 
620  |  |  * @param prop_query String containing the property query to match against.  | 
621  |  |  * @param prov_rw Pointer to the OSSL_PROVIDER to restrict the search to, or  | 
622  |  |  *                to receive the matched provider.  | 
623  |  |  * @param method Pointer to receive the fetched method. Must be non-null.  | 
624  |  |  *  | 
625  |  |  * @return 1 if the method is successfully fetched, 0 on failure.  | 
626  |  |  *  | 
627  |  |  * If tracing is enabled, a message is printed indicating the property query and  | 
628  |  |  * the resolved provider.  | 
629  |  |  *  | 
630  |  |  * NOTE: The nid parameter here is _not_ a NID in the sense of the NID_* macros.  | 
631  |  |  * It is a unique internal identifier value.  | 
632  |  |  */  | 
633  |  | int ossl_method_store_fetch(OSSL_METHOD_STORE *store,  | 
634  |  |                             int nid, const char *prop_query,  | 
635  |  |                             const OSSL_PROVIDER **prov_rw, void **method)  | 
636  | 85  | { | 
637  | 85  |     OSSL_PROPERTY_LIST **plp;  | 
638  | 85  |     ALGORITHM *alg;  | 
639  | 85  |     IMPLEMENTATION *impl, *best_impl = NULL;  | 
640  | 85  |     OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;  | 
641  | 85  |     const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL;  | 
642  | 85  |     int ret = 0;  | 
643  | 85  |     int j, best = -1, score, optional;  | 
644  |  |  | 
645  | 85  |     if (nid <= 0 || method == NULL || store == NULL)  | 
646  | 0  |         return 0;  | 
647  |  |  | 
648  | 85  | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)  | 
649  | 85  |     if (ossl_lib_ctx_is_default(store->ctx)  | 
650  | 85  |             && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))  | 
651  | 0  |         return 0;  | 
652  | 85  | #endif  | 
653  |  |  | 
654  |  |     /* This only needs to be a read lock, because the query won't create anything */  | 
655  | 85  |     if (!ossl_property_read_lock(store))  | 
656  | 0  |         return 0;  | 
657  |  |  | 
658  | 85  |     OSSL_TRACE2(QUERY, "Retrieving by nid %d from store %p\n", nid, (void *)store);  | 
659  | 85  |     alg = ossl_method_store_retrieve(store, nid);  | 
660  | 85  |     if (alg == NULL) { | 
661  | 0  |         ossl_property_unlock(store);  | 
662  | 0  |         OSSL_TRACE2(QUERY, "Failed to retrieve by nid %d from store %p\n", nid, (void *)store);  | 
663  | 0  |         return 0;  | 
664  | 0  |     }  | 
665  | 85  |     OSSL_TRACE2(QUERY, "Retrieved by nid %d from store %p\n", nid, (void *)store);  | 
666  |  |  | 
667  |  |     /*  | 
668  |  |      * If a property query string is provided, convert it to an  | 
669  |  |      * OSSL_PROPERTY_LIST structure  | 
670  |  |      */  | 
671  | 85  |     if (prop_query != NULL)  | 
672  | 85  |         p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);  | 
673  |  |  | 
674  |  |     /*  | 
675  |  |      * If the library context has default properties specified  | 
676  |  |      * then merge those with the properties passed to this function  | 
677  |  |      */  | 
678  | 85  |     plp = ossl_ctx_global_properties(store->ctx, 0);  | 
679  | 85  |     if (plp != NULL && *plp != NULL) { | 
680  | 0  |         if (pq == NULL) { | 
681  | 0  |             pq = *plp;  | 
682  | 0  |         } else { | 
683  | 0  |             p2 = ossl_property_merge(pq, *plp);  | 
684  | 0  |             ossl_property_free(pq);  | 
685  | 0  |             if (p2 == NULL)  | 
686  | 0  |                 goto fin;  | 
687  | 0  |             pq = p2;  | 
688  | 0  |         }  | 
689  | 0  |     }  | 
690  |  |  | 
691  |  |     /*  | 
692  |  |      * Search for a provider that provides this implementation.  | 
693  |  |      * If the requested provider is NULL, then any provider will do,  | 
694  |  |      * otherwise we should try to find the one that matches the requested  | 
695  |  |      * provider.  Note that providers are given implicit preference via the  | 
696  |  |      * ordering of the implementation stack  | 
697  |  |      */  | 
698  | 85  |     if (pq == NULL) { | 
699  | 0  |         for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) { | 
700  | 0  |             if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL  | 
701  | 0  |                 && (prov == NULL || impl->provider == prov)) { | 
702  | 0  |                 best_impl = impl;  | 
703  | 0  |                 ret = 1;  | 
704  | 0  |                 break;  | 
705  | 0  |             }  | 
706  | 0  |         }  | 
707  | 0  |         goto fin;  | 
708  | 0  |     }  | 
709  |  |  | 
710  |  |     /*  | 
711  |  |      * If there are optional properties specified  | 
712  |  |      * then run the search again, and select the provider that matches the  | 
713  |  |      * most options  | 
714  |  |      */  | 
715  | 85  |     optional = ossl_property_has_optional(pq);  | 
716  | 85  |     for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) { | 
717  | 85  |         if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL  | 
718  | 85  |             && (prov == NULL || impl->provider == prov)) { | 
719  | 85  |             score = ossl_property_match_count(pq, impl->properties);  | 
720  | 85  |             if (score > best) { | 
721  | 85  |                 best_impl = impl;  | 
722  | 85  |                 best = score;  | 
723  | 85  |                 ret = 1;  | 
724  | 85  |                 if (!optional)  | 
725  | 85  |                     goto fin;  | 
726  | 85  |             }  | 
727  | 85  |         }  | 
728  | 85  |     }  | 
729  | 85  | fin:  | 
730  | 85  |     if (ret && ossl_method_up_ref(&best_impl->method)) { | 
731  | 85  |         *method = best_impl->method.method;  | 
732  | 85  |         if (prov_rw != NULL)  | 
733  | 85  |             *prov_rw = best_impl->provider;  | 
734  | 85  |     } else { | 
735  | 0  |         ret = 0;  | 
736  | 0  |     }  | 
737  |  |  | 
738  | 85  | #ifndef FIPS_MODULE  | 
739  | 85  |     OSSL_TRACE_BEGIN(QUERY) { | 
740  | 0  |         char buf[512];  | 
741  | 0  |         size_t size;  | 
742  |  | 
  | 
743  | 0  |         size = ossl_property_list_to_string(NULL, pq, buf, 512);  | 
744  | 0  |         BIO_printf(trc_out, "method store query with properties %s "  | 
745  | 0  |                    "resolves to provider %s\n",  | 
746  | 0  |                    size == 0 ? "none" : buf,  | 
747  | 0  |                    best_impl == NULL ? "none" :  | 
748  | 0  |                    ossl_provider_name(best_impl->provider));  | 
749  | 85  |     } OSSL_TRACE_END(QUERY);  | 
750  | 85  | #endif  | 
751  |  |  | 
752  | 85  |     ossl_property_unlock(store);  | 
753  | 85  |     ossl_property_free(p2);  | 
754  | 85  |     return ret;  | 
755  | 85  | }  | 
756  |  |  | 
757  |  | static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,  | 
758  |  |                                         ALGORITHM *alg)  | 
759  | 0  | { | 
760  | 0  |     store->cache_nelem -= lh_QUERY_num_items(alg->cache);  | 
761  | 0  |     impl_cache_flush_alg(0, alg);  | 
762  | 0  | }  | 
763  |  |  | 
764  |  | static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)  | 
765  | 2.59k  | { | 
766  | 2.59k  |     ALGORITHM *alg = ossl_method_store_retrieve(store, nid);  | 
767  |  |  | 
768  | 2.59k  |     if (alg != NULL)  | 
769  | 0  |         ossl_method_cache_flush_alg(store, alg);  | 
770  | 2.59k  | }  | 
771  |  |  | 
772  |  | int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store)  | 
773  | 0  | { | 
774  | 0  |     if (!ossl_property_write_lock(store))  | 
775  | 0  |         return 0;  | 
776  | 0  |     ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);  | 
777  | 0  |     store->cache_nelem = 0;  | 
778  | 0  |     ossl_property_unlock(store);  | 
779  | 0  |     return 1;  | 
780  | 0  | }  | 
781  |  |  | 
782  |  | IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);  | 
783  |  |  | 
784  |  | /*  | 
785  |  |  * Flush an element from the query cache (perhaps).  | 
786  |  |  *  | 
787  |  |  * In order to avoid taking a write lock or using atomic operations  | 
788  |  |  * to keep accurate least recently used (LRU) or least frequently used  | 
789  |  |  * (LFU) information, the procedure used here is to stochastically  | 
790  |  |  * flush approximately half the cache.  | 
791  |  |  *  | 
792  |  |  * This procedure isn't ideal, LRU or LFU would be better.  However,  | 
793  |  |  * in normal operation, reaching a full cache would be unexpected.  | 
794  |  |  * It means that no steady state of algorithm queries has been reached.  | 
795  |  |  * That is, it is most likely an attack of some form.  A suboptimal clearance  | 
796  |  |  * strategy that doesn't degrade performance of the normal case is  | 
797  |  |  * preferable to a more refined approach that imposes a performance  | 
798  |  |  * impact.  | 
799  |  |  */  | 
800  |  | static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)  | 
801  | 0  | { | 
802  | 0  |     uint32_t n;  | 
803  |  |  | 
804  |  |     /*  | 
805  |  |      * Implement the 32 bit xorshift as suggested by George Marsaglia in:  | 
806  |  |      *      https://doi.org/10.18637/jss.v008.i14  | 
807  |  |      *  | 
808  |  |      * This is a very fast PRNG so there is no need to extract bits one at a  | 
809  |  |      * time and use the entire value each time.  | 
810  |  |      */  | 
811  | 0  |     n = state->seed;  | 
812  | 0  |     n ^= n << 13;  | 
813  | 0  |     n ^= n >> 17;  | 
814  | 0  |     n ^= n << 5;  | 
815  | 0  |     state->seed = n;  | 
816  |  | 
  | 
817  | 0  |     if ((n & 1) != 0)  | 
818  | 0  |         impl_cache_free(lh_QUERY_delete(state->cache, c));  | 
819  | 0  |     else  | 
820  | 0  |         state->nelem++;  | 
821  | 0  | }  | 
822  |  |  | 
823  |  | static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,  | 
824  |  |                                      void *v)  | 
825  | 0  | { | 
826  | 0  |     IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;  | 
827  | 0  |     unsigned long orig_down_load = lh_QUERY_get_down_load(alg->cache);  | 
828  |  | 
  | 
829  | 0  |     state->cache = alg->cache;  | 
830  | 0  |     lh_QUERY_set_down_load(alg->cache, 0);  | 
831  | 0  |     lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,  | 
832  | 0  |                                     state);  | 
833  | 0  |     lh_QUERY_set_down_load(alg->cache, orig_down_load);  | 
834  | 0  | }  | 
835  |  |  | 
836  |  | static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)  | 
837  | 0  | { | 
838  | 0  |     IMPL_CACHE_FLUSH state;  | 
839  | 0  |     static TSAN_QUALIFIER uint32_t global_seed = 1;  | 
840  |  | 
  | 
841  | 0  |     state.nelem = 0;  | 
842  | 0  |     state.using_global_seed = 0;  | 
843  | 0  |     if ((state.seed = OPENSSL_rdtsc()) == 0) { | 
844  |  |         /* If there is no timer available, seed another way */  | 
845  | 0  |         state.using_global_seed = 1;  | 
846  | 0  |         state.seed = tsan_load(&global_seed);  | 
847  | 0  |     }  | 
848  | 0  |     store->cache_need_flush = 0;  | 
849  | 0  |     ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);  | 
850  | 0  |     store->cache_nelem = state.nelem;  | 
851  |  |     /* Without a timer, update the global seed */  | 
852  | 0  |     if (state.using_global_seed)  | 
853  | 0  |         tsan_add(&global_seed, state.seed);  | 
854  | 0  | }  | 
855  |  |  | 
856  |  | int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,  | 
857  |  |                                 int nid, const char *prop_query, void **method)  | 
858  | 93.2k  | { | 
859  | 93.2k  |     ALGORITHM *alg;  | 
860  | 93.2k  |     QUERY elem, *r;  | 
861  | 93.2k  |     int res = 0;  | 
862  |  |  | 
863  | 93.2k  |     if (nid <= 0 || store == NULL || prop_query == NULL)  | 
864  | 0  |         return 0;  | 
865  |  |  | 
866  | 93.2k  |     if (!ossl_property_read_lock(store))  | 
867  | 0  |         return 0;  | 
868  | 93.2k  |     alg = ossl_method_store_retrieve(store, nid);  | 
869  | 93.2k  |     if (alg == NULL)  | 
870  | 16  |         goto err;  | 
871  |  |  | 
872  | 93.1k  |     elem.query = prop_query;  | 
873  | 93.1k  |     elem.provider = prov;  | 
874  | 93.1k  |     r = lh_QUERY_retrieve(alg->cache, &elem);  | 
875  | 93.1k  |     if (r == NULL)  | 
876  | 37  |         goto err;  | 
877  | 93.1k  |     if (ossl_method_up_ref(&r->method)) { | 
878  | 93.1k  |         *method = r->method.method;  | 
879  | 93.1k  |         res = 1;  | 
880  | 93.1k  |     }  | 
881  | 93.2k  | err:  | 
882  | 93.2k  |     ossl_property_unlock(store);  | 
883  | 93.2k  |     return res;  | 
884  | 93.1k  | }  | 
885  |  |  | 
886  |  | int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,  | 
887  |  |                                 int nid, const char *prop_query, void *method,  | 
888  |  |                                 int (*method_up_ref)(void *),  | 
889  |  |                                 void (*method_destruct)(void *))  | 
890  | 85  | { | 
891  | 85  |     QUERY elem, *old, *p = NULL;  | 
892  | 85  |     ALGORITHM *alg;  | 
893  | 85  |     size_t len;  | 
894  | 85  |     int res = 1;  | 
895  |  |  | 
896  | 85  |     if (nid <= 0 || store == NULL || prop_query == NULL)  | 
897  | 0  |         return 0;  | 
898  |  |  | 
899  | 85  |     if (!ossl_assert(prov != NULL))  | 
900  | 0  |         return 0;  | 
901  |  |  | 
902  | 85  |     if (!ossl_property_write_lock(store))  | 
903  | 0  |         return 0;  | 
904  | 85  |     if (store->cache_need_flush)  | 
905  | 0  |         ossl_method_cache_flush_some(store);  | 
906  | 85  |     alg = ossl_method_store_retrieve(store, nid);  | 
907  | 85  |     if (alg == NULL)  | 
908  | 0  |         goto err;  | 
909  |  |  | 
910  | 85  |     if (method == NULL) { | 
911  | 0  |         elem.query = prop_query;  | 
912  | 0  |         elem.provider = prov;  | 
913  | 0  |         if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) { | 
914  | 0  |             impl_cache_free(old);  | 
915  | 0  |             store->cache_nelem--;  | 
916  | 0  |         }  | 
917  | 0  |         goto end;  | 
918  | 0  |     }  | 
919  | 85  |     p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));  | 
920  | 85  |     if (p != NULL) { | 
921  | 85  |         p->query = p->body;  | 
922  | 85  |         p->provider = prov;  | 
923  | 85  |         p->method.method = method;  | 
924  | 85  |         p->method.up_ref = method_up_ref;  | 
925  | 85  |         p->method.free = method_destruct;  | 
926  | 85  |         if (!ossl_method_up_ref(&p->method))  | 
927  | 0  |             goto err;  | 
928  | 85  |         memcpy((char *)p->query, prop_query, len + 1);  | 
929  | 85  |         if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) { | 
930  | 0  |             impl_cache_free(old);  | 
931  | 0  |             goto end;  | 
932  | 0  |         }  | 
933  | 85  |         if (!lh_QUERY_error(alg->cache)) { | 
934  | 85  |             if (++store->cache_nelem >= IMPL_CACHE_FLUSH_THRESHOLD)  | 
935  | 0  |                 store->cache_need_flush = 1;  | 
936  | 85  |             goto end;  | 
937  | 85  |         }  | 
938  | 0  |         ossl_method_free(&p->method);  | 
939  | 0  |     }  | 
940  | 0  | err:  | 
941  | 0  |     res = 0;  | 
942  | 0  |     OPENSSL_free(p);  | 
943  | 85  | end:  | 
944  | 85  |     ossl_property_unlock(store);  | 
945  | 85  |     return res;  | 
946  | 0  | }  |