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