]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/property/property.c
Add deprecation macro for 3.1 and deprecate OPENSSL_LH_stats
[thirdparty/openssl.git] / crypto / property / property.c
1 /*
2 * Copyright 2019-2022 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 #include "crypto/context.h"
26
27 /*
28 * The number of elements in the query cache before we initiate a flush.
29 * If reducing this, also ensure the stochastic test in test/property_test.c
30 * isn't likely to fail.
31 */
32 #define IMPL_CACHE_FLUSH_THRESHOLD 500
33
34 typedef struct {
35 void *method;
36 int (*up_ref)(void *);
37 void (*free)(void *);
38 } METHOD;
39
40 typedef struct {
41 const OSSL_PROVIDER *provider;
42 OSSL_PROPERTY_LIST *properties;
43 METHOD method;
44 } IMPLEMENTATION;
45
46 DEFINE_STACK_OF(IMPLEMENTATION)
47
48 typedef struct {
49 const OSSL_PROVIDER *provider;
50 const char *query;
51 METHOD method;
52 char body[1];
53 } QUERY;
54
55 DEFINE_LHASH_OF_EX(QUERY);
56
57 typedef struct {
58 int nid;
59 STACK_OF(IMPLEMENTATION) *impls;
60 LHASH_OF(QUERY) *cache;
61 } ALGORITHM;
62
63 struct ossl_method_store_st {
64 OSSL_LIB_CTX *ctx;
65 SPARSE_ARRAY_OF(ALGORITHM) *algs;
66 CRYPTO_RWLOCK *lock;
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;
75 };
76
77 typedef struct {
78 LHASH_OF(QUERY) *cache;
79 size_t nelem;
80 uint32_t seed;
81 } IMPL_CACHE_FLUSH;
82
83 DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
84
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
92 static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
93 ALGORITHM *alg);
94 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
95
96 /* Global properties are stored per library context */
97 void ossl_ctx_global_properties_free(void *vglobp)
98 {
99 OSSL_GLOBAL_PROPERTIES *globp = vglobp;
100
101 if (globp != NULL) {
102 ossl_property_free(globp->list);
103 OPENSSL_free(globp);
104 }
105 }
106
107 void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)
108 {
109 return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES));
110 }
111
112 OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
113 int loadconfig)
114 {
115 OSSL_GLOBAL_PROPERTIES *globp;
116
117 #ifndef FIPS_MODULE
118 if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
119 return NULL;
120 #endif
121 globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
122
123 return globp != NULL ? &globp->list : NULL;
124 }
125
126 #ifndef FIPS_MODULE
127 int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx)
128 {
129 OSSL_GLOBAL_PROPERTIES *globp
130 = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
131
132 return globp != NULL && globp->no_mirrored ? 1 : 0;
133 }
134
135 void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx)
136 {
137 OSSL_GLOBAL_PROPERTIES *globp
138 = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
139
140 if (globp != NULL)
141 globp->no_mirrored = 1;
142 }
143 #endif
144
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
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
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
165 static int ossl_property_unlock(OSSL_METHOD_STORE *p)
166 {
167 return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
168 }
169
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 {
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
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
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
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
208 static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a, void *arg)
209 {
210 OSSL_METHOD_STORE *store = arg;
211
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 }
218 if (store != NULL)
219 ossl_sa_ALGORITHM_set(store->algs, idx, NULL);
220 }
221
222 /*
223 * The OSSL_LIB_CTX param here allows access to underlying property data needed
224 * for computation
225 */
226 OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx)
227 {
228 OSSL_METHOD_STORE *res;
229
230 res = OPENSSL_zalloc(sizeof(*res));
231 if (res != NULL) {
232 res->ctx = ctx;
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) {
238 ossl_sa_ALGORITHM_free(res->algs);
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) {
249 ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup, store);
250 ossl_sa_ALGORITHM_free(store->algs);
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
266 int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
267 int nid, const char *properties, void *method,
268 int (*method_up_ref)(void *),
269 void (*method_destruct)(void *))
270 {
271 ALGORITHM *alg = NULL;
272 IMPLEMENTATION *impl;
273 int ret = 0;
274 int i;
275
276 if (nid <= 0 || method == NULL || store == NULL)
277 return 0;
278 if (properties == NULL)
279 properties = "";
280
281 if (!ossl_assert(prov != NULL))
282 return 0;
283
284 /* Create new entry */
285 impl = OPENSSL_malloc(sizeof(*impl));
286 if (impl == NULL)
287 return 0;
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)) {
292 OPENSSL_free(impl);
293 return 0;
294 }
295 impl->provider = prov;
296
297 /* Insert into the hash table if required */
298 if (!ossl_property_write_lock(store)) {
299 OPENSSL_free(impl);
300 return 0;
301 }
302 ossl_method_cache_flush(store, nid);
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)
306 goto err;
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 }
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
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))
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);
343 alg_cleanup(0, alg, NULL);
344 impl_free(impl);
345 return 0;
346 }
347
348 int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
349 const void *method)
350 {
351 ALGORITHM *alg = NULL;
352 int i;
353
354 if (nid <= 0 || method == NULL || store == NULL)
355 return 0;
356
357 if (!ossl_property_write_lock(store))
358 return 0;
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
374 if (impl->method.method == method) {
375 impl_free(impl);
376 (void)sk_IMPLEMENTATION_delete(alg->impls, i);
377 ossl_property_unlock(store);
378 return 1;
379 }
380 }
381 ossl_property_unlock(store);
382 return 0;
383 }
384
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
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
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)
473 {
474 OSSL_PROPERTY_LIST **plp;
475 ALGORITHM *alg;
476 IMPLEMENTATION *impl, *best_impl = NULL;
477 OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;
478 const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL;
479 int ret = 0;
480 int j, best = -1, score, optional;
481
482 #ifndef FIPS_MODULE
483 if (!OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
484 return 0;
485 #endif
486
487 if (nid <= 0 || method == NULL || store == NULL)
488 return 0;
489
490 /* This only needs to be a read lock, because the query won't create anything */
491 if (!ossl_property_read_lock(store))
492 return 0;
493 alg = ossl_method_store_retrieve(store, nid);
494 if (alg == NULL) {
495 ossl_property_unlock(store);
496 return 0;
497 }
498
499 if (prop_query != NULL)
500 p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);
501 plp = ossl_ctx_global_properties(store->ctx, 0);
502 if (plp != NULL && *plp != NULL) {
503 if (pq == NULL) {
504 pq = *plp;
505 } else {
506 p2 = ossl_property_merge(pq, *plp);
507 ossl_property_free(pq);
508 if (p2 == NULL)
509 goto fin;
510 pq = p2;
511 }
512 }
513
514 if (pq == NULL) {
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 }
522 }
523 goto fin;
524 }
525 optional = ossl_property_has_optional(pq);
526 for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
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 }
537 }
538 }
539 fin:
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 {
545 ret = 0;
546 }
547 ossl_property_unlock(store);
548 ossl_property_free(p2);
549 return ret;
550 }
551
552 static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
553 ALGORITHM *alg)
554 {
555 store->cache_nelem -= lh_QUERY_num_items(alg->cache);
556 impl_cache_flush_alg(0, alg);
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
563 if (alg != NULL)
564 ossl_method_cache_flush_alg(store, alg);
565 }
566
567 int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store)
568 {
569 if (!ossl_property_write_lock(store))
570 return 0;
571 ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
572 store->cache_nelem = 0;
573 ossl_property_unlock(store);
574 return 1;
575 }
576
577 IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
578
579 /*
580 * Flush an element from the query cache (perhaps).
581 *
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.
586 *
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.
594 */
595 static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
596 {
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)
613 impl_cache_free(lh_QUERY_delete(state->cache, c));
614 else
615 state->nelem++;
616 }
617
618 static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
619 void *v)
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;
633 if ((state.seed = OPENSSL_rdtsc()) == 0)
634 state.seed = 1;
635 store->cache_need_flush = 0;
636 ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
637 store->cache_nelem = state.nelem;
638 }
639
640 int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
641 int nid, const char *prop_query, void **method)
642 {
643 ALGORITHM *alg;
644 QUERY elem, *r;
645 int res = 0;
646
647 if (nid <= 0 || store == NULL || prop_query == NULL)
648 return 0;
649
650 if (!ossl_property_read_lock(store))
651 return 0;
652 alg = ossl_method_store_retrieve(store, nid);
653 if (alg == NULL)
654 goto err;
655
656 elem.query = prop_query;
657 elem.provider = prov;
658 r = lh_QUERY_retrieve(alg->cache, &elem);
659 if (r == NULL)
660 goto err;
661 if (ossl_method_up_ref(&r->method)) {
662 *method = r->method.method;
663 res = 1;
664 }
665 err:
666 ossl_property_unlock(store);
667 return res;
668 }
669
670 int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
671 int nid, const char *prop_query, void *method,
672 int (*method_up_ref)(void *),
673 void (*method_destruct)(void *))
674 {
675 QUERY elem, *old, *p = NULL;
676 ALGORITHM *alg;
677 size_t len;
678 int res = 1;
679
680 if (nid <= 0 || store == NULL || prop_query == NULL)
681 return 0;
682
683 if (!ossl_assert(prov != NULL))
684 return 0;
685
686 if (!ossl_property_write_lock(store))
687 return 0;
688 if (store->cache_need_flush)
689 ossl_method_cache_flush_some(store);
690 alg = ossl_method_store_retrieve(store, nid);
691 if (alg == NULL)
692 goto err;
693
694 if (method == NULL) {
695 elem.query = prop_query;
696 elem.provider = prov;
697 if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) {
698 impl_cache_free(old);
699 store->cache_nelem--;
700 }
701 goto end;
702 }
703 p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
704 if (p != NULL) {
705 p->query = p->body;
706 p->provider = prov;
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;
712 memcpy((char *)p->query, prop_query, len + 1);
713 if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
714 impl_cache_free(old);
715 goto end;
716 }
717 if (!lh_QUERY_error(alg->cache)) {
718 if (++store->cache_nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
719 store->cache_need_flush = 1;
720 goto end;
721 }
722 ossl_method_free(&p->method);
723 }
724 err:
725 res = 0;
726 OPENSSL_free(p);
727 end:
728 ossl_property_unlock(store);
729 return res;
730 }