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