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