2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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
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"
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.
31 #define IMPL_CACHE_FLUSH_THRESHOLD 500
35 int (*up_ref
)(void *);
40 const OSSL_PROVIDER
*provider
;
41 OSSL_PROPERTY_LIST
*properties
;
45 DEFINE_STACK_OF(IMPLEMENTATION
)
53 DEFINE_LHASH_OF(QUERY
);
57 STACK_OF(IMPLEMENTATION
) *impls
;
58 LHASH_OF(QUERY
) *cache
;
61 struct ossl_method_store_st
{
64 SPARSE_ARRAY_OF(ALGORITHM
) *algs
;
70 LHASH_OF(QUERY
) *cache
;
75 DEFINE_SPARSE_ARRAY_OF(ALGORITHM
);
77 typedef struct ossl_global_properties_st
{
78 OSSL_PROPERTY_LIST
*list
;
80 unsigned int no_mirrored
: 1;
82 } OSSL_GLOBAL_PROPERTIES
;
84 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
);
86 /* Global properties are stored per library context */
87 static void ossl_ctx_global_properties_free(void *vglobp
)
89 OSSL_GLOBAL_PROPERTIES
*globp
= vglobp
;
92 ossl_property_free(globp
->list
);
97 static void *ossl_ctx_global_properties_new(OSSL_LIB_CTX
*ctx
)
99 return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES
));
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
,
108 OSSL_PROPERTY_LIST
**ossl_ctx_global_properties(OSSL_LIB_CTX
*libctx
,
111 OSSL_GLOBAL_PROPERTIES
*globp
;
114 if (loadconfig
&& !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
))
117 globp
= ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_GLOBAL_PROPERTIES
,
118 &ossl_ctx_global_properties_method
);
124 int ossl_global_properties_no_mirrored(OSSL_LIB_CTX
*libctx
)
126 OSSL_GLOBAL_PROPERTIES
*globp
127 = ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_GLOBAL_PROPERTIES
,
128 &ossl_ctx_global_properties_method
);
130 return globp
->no_mirrored
? 1 : 0;
133 void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX
*libctx
)
135 OSSL_GLOBAL_PROPERTIES
*globp
136 = ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_GLOBAL_PROPERTIES
,
137 &ossl_ctx_global_properties_method
);
139 globp
->no_mirrored
= 1;
143 static int ossl_method_up_ref(METHOD
*method
)
145 return (*method
->up_ref
)(method
->method
);
148 static void ossl_method_free(METHOD
*method
)
150 (*method
->free
)(method
->method
);
153 static __owur
int ossl_property_read_lock(OSSL_METHOD_STORE
*p
)
155 return p
!= NULL
? CRYPTO_THREAD_read_lock(p
->lock
) : 0;
158 static __owur
int ossl_property_write_lock(OSSL_METHOD_STORE
*p
)
160 return p
!= NULL
? CRYPTO_THREAD_write_lock(p
->lock
) : 0;
163 static int ossl_property_unlock(OSSL_METHOD_STORE
*p
)
165 return p
!= 0 ? CRYPTO_THREAD_unlock(p
->lock
) : 0;
168 static unsigned long query_hash(const QUERY
*a
)
170 return OPENSSL_LH_strhash(a
->query
);
173 static int query_cmp(const QUERY
*a
, const QUERY
*b
)
175 return strcmp(a
->query
, b
->query
);
178 static void impl_free(IMPLEMENTATION
*impl
)
181 ossl_method_free(&impl
->method
);
186 static void impl_cache_free(QUERY
*elem
)
189 ossl_method_free(&elem
->method
);
194 static void alg_cleanup(ossl_uintmax_t idx
, ALGORITHM
*a
)
197 sk_IMPLEMENTATION_pop_free(a
->impls
, &impl_free
);
198 lh_QUERY_doall(a
->cache
, &impl_cache_free
);
199 lh_QUERY_free(a
->cache
);
205 * The OSSL_LIB_CTX param here allows access to underlying property data needed
208 OSSL_METHOD_STORE
*ossl_method_store_new(OSSL_LIB_CTX
*ctx
)
210 OSSL_METHOD_STORE
*res
;
212 res
= OPENSSL_zalloc(sizeof(*res
));
215 if ((res
->algs
= ossl_sa_ALGORITHM_new()) == NULL
) {
219 if ((res
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
220 ossl_sa_ALGORITHM_free(res
->algs
);
228 void ossl_method_store_free(OSSL_METHOD_STORE
*store
)
231 ossl_sa_ALGORITHM_doall(store
->algs
, &alg_cleanup
);
232 ossl_sa_ALGORITHM_free(store
->algs
);
233 CRYPTO_THREAD_lock_free(store
->lock
);
238 static ALGORITHM
*ossl_method_store_retrieve(OSSL_METHOD_STORE
*store
, int nid
)
240 return ossl_sa_ALGORITHM_get(store
->algs
, nid
);
243 static int ossl_method_store_insert(OSSL_METHOD_STORE
*store
, ALGORITHM
*alg
)
245 return ossl_sa_ALGORITHM_set(store
->algs
, alg
->nid
, alg
);
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 *))
253 ALGORITHM
*alg
= NULL
;
254 IMPLEMENTATION
*impl
;
258 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
260 if (properties
== NULL
)
263 /* Create new entry */
264 impl
= OPENSSL_malloc(sizeof(*impl
));
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
)) {
274 impl
->provider
= prov
;
276 /* Insert into the hash table if required */
277 if (!ossl_property_write_lock(store
)) {
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
)
286 ossl_prop_defn_set(store
->ctx
, properties
, impl
->properties
);
289 alg
= ossl_method_store_retrieve(store
, nid
);
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
)
296 if (!ossl_method_store_insert(store
, alg
))
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
);
304 if (tmpimpl
->provider
== impl
->provider
305 && tmpimpl
->properties
== impl
->properties
)
308 if (i
== sk_IMPLEMENTATION_num(alg
->impls
)
309 && sk_IMPLEMENTATION_push(alg
->impls
, impl
))
311 ossl_property_unlock(store
);
317 ossl_property_unlock(store
);
323 int ossl_method_store_remove(OSSL_METHOD_STORE
*store
, int nid
,
326 ALGORITHM
*alg
= NULL
;
329 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
332 if (!ossl_property_write_lock(store
))
334 ossl_method_cache_flush(store
, nid
);
335 alg
= ossl_method_store_retrieve(store
, nid
);
337 ossl_property_unlock(store
);
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).
346 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
347 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
349 if (impl
->method
.method
== method
) {
351 (void)sk_IMPLEMENTATION_delete(alg
->impls
, i
);
352 ossl_property_unlock(store
);
356 ossl_property_unlock(store
);
360 static void alg_do_one(ALGORITHM
*alg
, IMPLEMENTATION
*impl
,
361 void (*fn
)(int id
, void *method
, void *fnarg
),
364 fn(alg
->nid
, impl
->method
.method
, fnarg
);
367 struct alg_do_each_data_st
{
368 void (*fn
)(int id
, void *method
, void *fnarg
);
372 static void alg_do_each(ossl_uintmax_t idx
, ALGORITHM
*alg
, void *arg
)
374 struct alg_do_each_data_st
*data
= arg
;
375 int i
, end
= sk_IMPLEMENTATION_num(alg
->impls
);
377 for (i
= 0; i
< end
; i
++) {
378 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
380 alg_do_one(alg
, impl
, data
->fn
, data
->fnarg
);
384 void ossl_method_store_do_all(OSSL_METHOD_STORE
*store
,
385 void (*fn
)(int id
, void *method
, void *fnarg
),
388 struct alg_do_each_data_st data
;
393 ossl_sa_ALGORITHM_doall_arg(store
->algs
, alg_do_each
, &data
);
396 int ossl_method_store_fetch(OSSL_METHOD_STORE
*store
, int nid
,
397 const char *prop_query
,
400 OSSL_PROPERTY_LIST
**plp
;
402 IMPLEMENTATION
*impl
;
403 OSSL_PROPERTY_LIST
*pq
= NULL
, *p2
= NULL
;
404 METHOD
*best_method
= NULL
;
406 int j
, best
= -1, score
, optional
;
409 if (!OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
))
413 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
416 /* This only needs to be a read lock, because the query won't create anything */
417 if (!ossl_property_read_lock(store
))
419 alg
= ossl_method_store_retrieve(store
, nid
);
421 ossl_property_unlock(store
);
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
) {
432 p2
= ossl_property_merge(pq
, *plp
);
433 ossl_property_free(pq
);
441 if ((impl
= sk_IMPLEMENTATION_value(alg
->impls
, 0)) != NULL
) {
442 best_method
= &impl
->method
;
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
);
452 best_method
= &impl
->method
;
460 if (ret
&& ossl_method_up_ref(best_method
))
461 *method
= best_method
->method
;
464 ossl_property_unlock(store
);
465 ossl_property_free(p2
);
469 static void impl_cache_flush_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
, void *arg
)
471 SPARSE_ARRAY_OF(ALGORITHM
) *algs
= arg
;
473 lh_QUERY_doall(alg
->cache
, &impl_cache_free
);
475 sk_IMPLEMENTATION_pop_free(alg
->impls
, &impl_free
);
476 lh_QUERY_free(alg
->cache
);
478 ossl_sa_ALGORITHM_set(algs
, idx
, NULL
);
480 lh_QUERY_flush(alg
->cache
);
484 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
)
486 ALGORITHM
*alg
= ossl_method_store_retrieve(store
, nid
);
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
);
495 int ossl_method_store_flush_cache(OSSL_METHOD_STORE
*store
, int all
)
497 void *arg
= (all
!= 0 ? store
->algs
: NULL
);
499 if (!ossl_property_write_lock(store
))
501 ossl_provider_clear_all_operation_bits(store
->ctx
);
502 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_alg
, arg
);
504 ossl_property_unlock(store
);
508 IMPLEMENT_LHASH_DOALL_ARG(QUERY
, IMPL_CACHE_FLUSH
);
511 * Flush an element from the query cache (perhaps).
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.
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
526 static void impl_cache_flush_cache(QUERY
*c
, IMPL_CACHE_FLUSH
*state
)
531 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
532 * https://doi.org/10.18637/jss.v008.i14
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.
544 impl_cache_free(lh_QUERY_delete(state
->cache
, c
));
549 static void impl_cache_flush_one_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
,
552 IMPL_CACHE_FLUSH
*state
= (IMPL_CACHE_FLUSH
*)v
;
554 state
->cache
= alg
->cache
;
555 lh_QUERY_doall_IMPL_CACHE_FLUSH(state
->cache
, &impl_cache_flush_cache
,
559 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE
*store
)
561 IMPL_CACHE_FLUSH state
;
564 if ((state
.seed
= OPENSSL_rdtsc()) == 0)
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
;
572 int ossl_method_store_cache_get(OSSL_METHOD_STORE
*store
, int nid
,
573 const char *prop_query
, void **method
)
579 if (nid
<= 0 || store
== NULL
)
582 if (!ossl_property_read_lock(store
))
584 alg
= ossl_method_store_retrieve(store
, nid
);
588 elem
.query
= prop_query
!= NULL
? prop_query
: "";
589 r
= lh_QUERY_retrieve(alg
->cache
, &elem
);
592 if (ossl_method_up_ref(&r
->method
)) {
593 *method
= r
->method
.method
;
597 ossl_property_unlock(store
);
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 *))
606 QUERY elem
, *old
, *p
= NULL
;
611 if (nid
<= 0 || store
== NULL
)
613 if (prop_query
== NULL
)
616 if (!ossl_property_write_lock(store
))
618 if (store
->need_flush
)
619 ossl_method_cache_flush_some(store
);
620 alg
= ossl_method_store_retrieve(store
, nid
);
624 if (method
== NULL
) {
625 elem
.query
= prop_query
;
626 if ((old
= lh_QUERY_delete(alg
->cache
, &elem
)) != NULL
) {
627 impl_cache_free(old
);
632 p
= OPENSSL_malloc(sizeof(*p
) + (len
= strlen(prop_query
)));
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
))
640 memcpy((char *)p
->query
, prop_query
, len
+ 1);
641 if ((old
= lh_QUERY_insert(alg
->cache
, p
)) != NULL
) {
642 impl_cache_free(old
);
645 if (!lh_QUERY_error(alg
->cache
)) {
646 if (++store
->nelem
>= IMPL_CACHE_FLUSH_THRESHOLD
)
647 store
->need_flush
= 1;
650 ossl_method_free(&p
->method
);
656 ossl_property_unlock(store
);