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