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