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