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