]>
Commit | Line | Data |
---|---|---|
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 | |
33 | typedef struct { | |
34 | void *method; | |
35 | int (*up_ref)(void *); | |
36 | void (*free)(void *); | |
37 | } METHOD; | |
1bdbdaff P |
38 | |
39 | typedef struct { | |
c1d56231 | 40 | const OSSL_PROVIDER *provider; |
1bdbdaff | 41 | OSSL_PROPERTY_LIST *properties; |
bdbf2df2 | 42 | METHOD method; |
1bdbdaff P |
43 | } IMPLEMENTATION; |
44 | ||
45 | DEFINE_STACK_OF(IMPLEMENTATION) | |
46 | ||
47 | typedef struct { | |
dc010ca6 | 48 | const OSSL_PROVIDER *provider; |
1bdbdaff | 49 | const char *query; |
bdbf2df2 | 50 | METHOD method; |
1bdbdaff P |
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 { | |
b4250010 | 63 | OSSL_LIB_CTX *ctx; |
1bdbdaff P |
64 | size_t nelem; |
65 | SPARSE_ARRAY_OF(ALGORITHM) *algs; | |
1bdbdaff | 66 | int need_flush; |
1bdbdaff P |
67 | CRYPTO_RWLOCK *lock; |
68 | }; | |
69 | ||
70 | typedef struct { | |
1bdbdaff P |
71 | LHASH_OF(QUERY) *cache; |
72 | size_t nelem; | |
f06cf3c4 | 73 | uint32_t seed; |
1bdbdaff P |
74 | } IMPL_CACHE_FLUSH; |
75 | ||
76 | DEFINE_SPARSE_ARRAY_OF(ALGORITHM); | |
77 | ||
b1c053ac MC |
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 | ||
1bdbdaff | 85 | static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid); |
f9e504e8 P |
86 | |
87 | /* Global properties are stored per library context */ | |
b1c053ac | 88 | static void ossl_ctx_global_properties_free(void *vglobp) |
f9e504e8 | 89 | { |
b1c053ac | 90 | OSSL_GLOBAL_PROPERTIES *globp = vglobp; |
f9e504e8 | 91 | |
b1c053ac MC |
92 | if (globp != NULL) { |
93 | ossl_property_free(globp->list); | |
94 | OPENSSL_free(globp); | |
f9e504e8 P |
95 | } |
96 | } | |
97 | ||
b4250010 | 98 | static void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx) |
f9e504e8 | 99 | { |
b1c053ac | 100 | return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES)); |
f9e504e8 P |
101 | } |
102 | ||
b4250010 | 103 | static const OSSL_LIB_CTX_METHOD ossl_ctx_global_properties_method = { |
a16d2174 | 104 | OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY, |
f9e504e8 P |
105 | ossl_ctx_global_properties_new, |
106 | ossl_ctx_global_properties_free, | |
107 | }; | |
108 | ||
b4250010 | 109 | OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx, |
e6c54619 | 110 | int loadconfig) |
f9e504e8 | 111 | { |
b1c053ac MC |
112 | OSSL_GLOBAL_PROPERTIES *globp; |
113 | ||
e6c54619 MC |
114 | #ifndef FIPS_MODULE |
115 | if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL)) | |
116 | return NULL; | |
117 | #endif | |
b1c053ac MC |
118 | globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES, |
119 | &ossl_ctx_global_properties_method); | |
120 | ||
ed5b26ce | 121 | return globp != NULL ? &globp->list : NULL; |
b1c053ac MC |
122 | } |
123 | ||
124 | #ifndef FIPS_MODULE | |
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 | ||
ed5b26ce | 131 | return globp != NULL && globp->no_mirrored ? 1 : 0; |
b1c053ac MC |
132 | } |
133 | ||
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 | ||
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 | ||
8ab53b19 | 202 | static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a) |
1bdbdaff P |
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 | ||
1aedc35f | 212 | /* |
b4250010 | 213 | * The OSSL_LIB_CTX param here allows access to underlying property data needed |
1aedc35f MC |
214 | * for computation |
215 | */ | |
b4250010 | 216 | OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx) |
1bdbdaff | 217 | { |
909f2e59 | 218 | OSSL_METHOD_STORE *res; |
1bdbdaff | 219 | |
909f2e59 | 220 | res = OPENSSL_zalloc(sizeof(*res)); |
1bdbdaff | 221 | if (res != NULL) { |
1aedc35f | 222 | res->ctx = ctx; |
1bdbdaff P |
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) { | |
bdbf2df2 | 228 | ossl_sa_ALGORITHM_free(res->algs); |
1bdbdaff P |
229 | OPENSSL_free(res); |
230 | return NULL; | |
231 | } | |
232 | } | |
233 | return res; | |
234 | } | |
235 | ||
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); | |
1bdbdaff P |
241 | CRYPTO_THREAD_lock_free(store->lock); |
242 | OPENSSL_free(store); | |
243 | } | |
244 | } | |
245 | ||
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 | ||
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 | ||
c1d56231 | 256 | int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov, |
b1d40ddf RL |
257 | int nid, const char *properties, void *method, |
258 | int (*method_up_ref)(void *), | |
259 | void (*method_destruct)(void *)) | |
1bdbdaff P |
260 | { |
261 | ALGORITHM *alg = NULL; | |
262 | IMPLEMENTATION *impl; | |
263 | int ret = 0; | |
c1d56231 | 264 | int i; |
1bdbdaff | 265 | |
0a79572a | 266 | if (nid <= 0 || method == NULL || store == NULL) |
1bdbdaff P |
267 | return 0; |
268 | if (properties == NULL) | |
269 | properties = ""; | |
270 | ||
dc010ca6 RL |
271 | if (!ossl_assert(prov != NULL)) |
272 | return 0; | |
273 | ||
1bdbdaff P |
274 | /* Create new entry */ |
275 | impl = OPENSSL_malloc(sizeof(*impl)); | |
276 | if (impl == NULL) | |
277 | return 0; | |
bdbf2df2 P |
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)) { | |
c1d56231 | 282 | OPENSSL_free(impl); |
b1d40ddf | 283 | return 0; |
c1d56231 RL |
284 | } |
285 | impl->provider = prov; | |
1bdbdaff | 286 | |
2fee3a77 | 287 | /* Insert into the hash table if required */ |
860ecfd7 P |
288 | if (!ossl_property_write_lock(store)) { |
289 | OPENSSL_free(impl); | |
290 | return 0; | |
291 | } | |
1bdbdaff | 292 | ossl_method_cache_flush(store, nid); |
1aedc35f MC |
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) | |
1bdbdaff | 296 | goto err; |
1aedc35f | 297 | ossl_prop_defn_set(store->ctx, properties, impl->properties); |
1bdbdaff P |
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 | ||
c1d56231 RL |
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)) | |
1bdbdaff P |
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 | ||
334 | int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid, | |
0a79572a | 335 | const void *method) |
1bdbdaff P |
336 | { |
337 | ALGORITHM *alg = NULL; | |
338 | int i; | |
339 | ||
0a79572a | 340 | if (nid <= 0 || method == NULL || store == NULL) |
1bdbdaff P |
341 | return 0; |
342 | ||
860ecfd7 P |
343 | if (!ossl_property_write_lock(store)) |
344 | return 0; | |
1bdbdaff P |
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 | ||
bdbf2df2 P |
360 | if (impl->method.method == method) { |
361 | impl_free(impl); | |
225c9660 | 362 | (void)sk_IMPLEMENTATION_delete(alg->impls, i); |
1bdbdaff | 363 | ossl_property_unlock(store); |
1bdbdaff P |
364 | return 1; |
365 | } | |
366 | } | |
367 | ossl_property_unlock(store); | |
368 | return 0; | |
369 | } | |
370 | ||
f0191d0b RL |
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 | ||
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 | ||
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 | ||
dc010ca6 RL |
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) | |
1bdbdaff | 410 | { |
6f924bb8 | 411 | OSSL_PROPERTY_LIST **plp; |
1bdbdaff | 412 | ALGORITHM *alg; |
dc010ca6 | 413 | IMPLEMENTATION *impl, *best_impl = NULL; |
f9e504e8 | 414 | OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL; |
dc010ca6 | 415 | const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL; |
1bdbdaff | 416 | int ret = 0; |
da89ac0b | 417 | int j, best = -1, score, optional; |
1bdbdaff | 418 | |
f844f9eb | 419 | #ifndef FIPS_MODULE |
07af9441 | 420 | if (!OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL)) |
d05bfc12 | 421 | return 0; |
29dc6e00 MC |
422 | #endif |
423 | ||
0a79572a | 424 | if (nid <= 0 || method == NULL || store == NULL) |
1bdbdaff P |
425 | return 0; |
426 | ||
2fee3a77 | 427 | /* This only needs to be a read lock, because the query won't create anything */ |
860ecfd7 P |
428 | if (!ossl_property_read_lock(store)) |
429 | return 0; | |
1bdbdaff P |
430 | alg = ossl_method_store_retrieve(store, nid); |
431 | if (alg == NULL) { | |
432 | ossl_property_unlock(store); | |
433 | return 0; | |
434 | } | |
435 | ||
6f924bb8 | 436 | if (prop_query != NULL) |
1e08f3ba | 437 | p2 = pq = ossl_parse_query(store->ctx, prop_query, 0); |
07af9441 | 438 | plp = ossl_ctx_global_properties(store->ctx, 0); |
f9e504e8 P |
439 | if (plp != NULL && *plp != NULL) { |
440 | if (pq == NULL) { | |
441 | pq = *plp; | |
442 | } else { | |
443 | p2 = ossl_property_merge(pq, *plp); | |
84ba665d | 444 | ossl_property_free(pq); |
f9e504e8 P |
445 | if (p2 == NULL) |
446 | goto fin; | |
f9e504e8 P |
447 | pq = p2; |
448 | } | |
449 | } | |
450 | ||
451 | if (pq == NULL) { | |
dc010ca6 RL |
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 | } | |
1bdbdaff P |
459 | } |
460 | goto fin; | |
461 | } | |
da89ac0b | 462 | optional = ossl_property_has_optional(pq); |
1bdbdaff | 463 | for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) { |
dc010ca6 RL |
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 | } | |
1bdbdaff P |
474 | } |
475 | } | |
476 | fin: | |
dc010ca6 RL |
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 { | |
bdbf2df2 | 482 | ret = 0; |
dc010ca6 | 483 | } |
1bdbdaff | 484 | ossl_property_unlock(store); |
f9e504e8 | 485 | ossl_property_free(p2); |
1bdbdaff P |
486 | return ret; |
487 | } | |
488 | ||
04cb5ec0 | 489 | static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg, void *arg) |
1bdbdaff | 490 | { |
04cb5ec0 SL |
491 | SPARSE_ARRAY_OF(ALGORITHM) *algs = arg; |
492 | ||
1bdbdaff | 493 | lh_QUERY_doall(alg->cache, &impl_cache_free); |
04cb5ec0 SL |
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 | } | |
1bdbdaff P |
502 | } |
503 | ||
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) { | |
0090e508 | 509 | ossl_provider_clear_all_operation_bits(store->ctx); |
1bdbdaff | 510 | store->nelem -= lh_QUERY_num_items(alg->cache); |
04cb5ec0 | 511 | impl_cache_flush_alg(0, alg, NULL); |
1bdbdaff P |
512 | } |
513 | } | |
514 | ||
860ecfd7 | 515 | int ossl_method_store_flush_cache(OSSL_METHOD_STORE *store, int all) |
1bdbdaff | 516 | { |
04cb5ec0 SL |
517 | void *arg = (all != 0 ? store->algs : NULL); |
518 | ||
860ecfd7 P |
519 | if (!ossl_property_write_lock(store)) |
520 | return 0; | |
0090e508 | 521 | ossl_provider_clear_all_operation_bits(store->ctx); |
04cb5ec0 | 522 | ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_alg, arg); |
1bdbdaff | 523 | store->nelem = 0; |
f9e504e8 | 524 | ossl_property_unlock(store); |
860ecfd7 | 525 | return 1; |
1bdbdaff P |
526 | } |
527 | ||
528 | IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH); | |
529 | ||
530 | /* | |
531 | * Flush an element from the query cache (perhaps). | |
532 | * | |
f06cf3c4 P |
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. | |
1bdbdaff | 537 | * |
f06cf3c4 P |
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. | |
1bdbdaff P |
545 | */ |
546 | static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state) | |
547 | { | |
f06cf3c4 P |
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) | |
bdbf2df2 | 564 | impl_cache_free(lh_QUERY_delete(state->cache, c)); |
1bdbdaff P |
565 | else |
566 | state->nelem++; | |
567 | } | |
568 | ||
8ab53b19 P |
569 | static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg, |
570 | void *v) | |
1bdbdaff P |
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 | ||
579 | static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store) | |
580 | { | |
581 | IMPL_CACHE_FLUSH state; | |
582 | ||
583 | state.nelem = 0; | |
f06cf3c4 P |
584 | if ((state.seed = OPENSSL_rdtsc()) == 0) |
585 | state.seed = 1; | |
0090e508 | 586 | ossl_provider_clear_all_operation_bits(store->ctx); |
1bdbdaff | 587 | store->need_flush = 0; |
e2e5abe4 | 588 | ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state); |
1bdbdaff P |
589 | store->nelem = state.nelem; |
590 | } | |
591 | ||
dc010ca6 RL |
592 | int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov, |
593 | int nid, const char *prop_query, void **method) | |
1bdbdaff P |
594 | { |
595 | ALGORITHM *alg; | |
596 | QUERY elem, *r; | |
bdbf2df2 | 597 | int res = 0; |
1bdbdaff | 598 | |
af788ad6 | 599 | if (nid <= 0 || store == NULL || prop_query == NULL) |
1bdbdaff P |
600 | return 0; |
601 | ||
860ecfd7 P |
602 | if (!ossl_property_read_lock(store)) |
603 | return 0; | |
1bdbdaff | 604 | alg = ossl_method_store_retrieve(store, nid); |
bdbf2df2 P |
605 | if (alg == NULL) |
606 | goto err; | |
1bdbdaff | 607 | |
af788ad6 | 608 | elem.query = prop_query; |
dc010ca6 | 609 | elem.provider = prov; |
1bdbdaff | 610 | r = lh_QUERY_retrieve(alg->cache, &elem); |
bdbf2df2 P |
611 | if (r == NULL) |
612 | goto err; | |
613 | if (ossl_method_up_ref(&r->method)) { | |
614 | *method = r->method.method; | |
615 | res = 1; | |
1bdbdaff | 616 | } |
bdbf2df2 | 617 | err: |
1bdbdaff | 618 | ossl_property_unlock(store); |
bdbf2df2 | 619 | return res; |
1bdbdaff P |
620 | } |
621 | ||
dc010ca6 RL |
622 | int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov, |
623 | int nid, const char *prop_query, void *method, | |
bdbf2df2 P |
624 | int (*method_up_ref)(void *), |
625 | void (*method_destruct)(void *)) | |
1bdbdaff P |
626 | { |
627 | QUERY elem, *old, *p = NULL; | |
628 | ALGORITHM *alg; | |
629 | size_t len; | |
bdbf2df2 | 630 | int res = 1; |
1bdbdaff | 631 | |
af788ad6 | 632 | if (nid <= 0 || store == NULL || prop_query == NULL) |
1bdbdaff | 633 | return 0; |
1bdbdaff | 634 | |
dc010ca6 RL |
635 | if (!ossl_assert(prov != NULL)) |
636 | return 0; | |
637 | ||
860ecfd7 P |
638 | if (!ossl_property_write_lock(store)) |
639 | return 0; | |
1bdbdaff P |
640 | if (store->need_flush) |
641 | ossl_method_cache_flush_some(store); | |
642 | alg = ossl_method_store_retrieve(store, nid); | |
bdbf2df2 P |
643 | if (alg == NULL) |
644 | goto err; | |
1bdbdaff | 645 | |
0a79572a | 646 | if (method == NULL) { |
1bdbdaff | 647 | elem.query = prop_query; |
dc010ca6 | 648 | elem.provider = prov; |
bdbf2df2 P |
649 | if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) { |
650 | impl_cache_free(old); | |
f06cf3c4 | 651 | store->nelem--; |
bdbf2df2 P |
652 | } |
653 | goto end; | |
1bdbdaff P |
654 | } |
655 | p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query))); | |
656 | if (p != NULL) { | |
657 | p->query = p->body; | |
dc010ca6 | 658 | p->provider = prov; |
bdbf2df2 P |
659 | p->method.method = method; |
660 | p->method.up_ref = method_up_ref; | |
661 | p->method.free = method_destruct; | |
662 | if (!ossl_method_up_ref(&p->method)) | |
663 | goto err; | |
1bdbdaff | 664 | memcpy((char *)p->query, prop_query, len + 1); |
f06cf3c4 | 665 | if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) { |
bdbf2df2 P |
666 | impl_cache_free(old); |
667 | goto end; | |
f06cf3c4 P |
668 | } |
669 | if (!lh_QUERY_error(alg->cache)) { | |
670 | if (++store->nelem >= IMPL_CACHE_FLUSH_THRESHOLD) | |
1bdbdaff | 671 | store->need_flush = 1; |
bdbf2df2 | 672 | goto end; |
1bdbdaff | 673 | } |
bdbf2df2 | 674 | ossl_method_free(&p->method); |
1bdbdaff | 675 | } |
bdbf2df2 P |
676 | err: |
677 | res = 0; | |
defd3ed8 | 678 | OPENSSL_free(p); |
bdbf2df2 P |
679 | end: |
680 | ossl_property_unlock(store); | |
681 | return res; | |
1bdbdaff | 682 | } |