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