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
;
277 * Insert into the hash table if required.
279 * A write lock is used unconditionally because we wend our way down to the
280 * property string code which isn't locking friendly.
282 if (!ossl_property_write_lock(store
)) {
286 ossl_method_cache_flush(store
, nid
);
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
)
291 ossl_prop_defn_set(store
->ctx
, properties
, impl
->properties
);
294 alg
= ossl_method_store_retrieve(store
, nid
);
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
)
301 if (!ossl_method_store_insert(store
, alg
))
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
);
309 if (tmpimpl
->provider
== impl
->provider
310 && tmpimpl
->properties
== impl
->properties
)
313 if (i
== sk_IMPLEMENTATION_num(alg
->impls
)
314 && sk_IMPLEMENTATION_push(alg
->impls
, impl
))
316 ossl_property_unlock(store
);
322 ossl_property_unlock(store
);
328 int ossl_method_store_remove(OSSL_METHOD_STORE
*store
, int nid
,
331 ALGORITHM
*alg
= NULL
;
334 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
337 if (!ossl_property_write_lock(store
))
339 ossl_method_cache_flush(store
, nid
);
340 alg
= ossl_method_store_retrieve(store
, nid
);
342 ossl_property_unlock(store
);
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).
351 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
352 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
354 if (impl
->method
.method
== method
) {
356 (void)sk_IMPLEMENTATION_delete(alg
->impls
, i
);
357 ossl_property_unlock(store
);
361 ossl_property_unlock(store
);
365 static void alg_do_one(ALGORITHM
*alg
, IMPLEMENTATION
*impl
,
366 void (*fn
)(int id
, void *method
, void *fnarg
),
369 fn(alg
->nid
, impl
->method
.method
, fnarg
);
372 struct alg_do_each_data_st
{
373 void (*fn
)(int id
, void *method
, void *fnarg
);
377 static void alg_do_each(ossl_uintmax_t idx
, ALGORITHM
*alg
, void *arg
)
379 struct alg_do_each_data_st
*data
= arg
;
380 int i
, end
= sk_IMPLEMENTATION_num(alg
->impls
);
382 for (i
= 0; i
< end
; i
++) {
383 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
385 alg_do_one(alg
, impl
, data
->fn
, data
->fnarg
);
389 void ossl_method_store_do_all(OSSL_METHOD_STORE
*store
,
390 void (*fn
)(int id
, void *method
, void *fnarg
),
393 struct alg_do_each_data_st data
;
398 ossl_sa_ALGORITHM_doall_arg(store
->algs
, alg_do_each
, &data
);
401 int ossl_method_store_fetch(OSSL_METHOD_STORE
*store
, int nid
,
402 const char *prop_query
,
405 OSSL_PROPERTY_LIST
**plp
;
407 IMPLEMENTATION
*impl
;
408 OSSL_PROPERTY_LIST
*pq
= NULL
, *p2
= NULL
;
409 METHOD
*best_method
= NULL
;
411 int j
, best
= -1, score
, optional
;
414 if (!OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
))
418 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
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.
425 if (!ossl_property_read_lock(store
))
427 alg
= ossl_method_store_retrieve(store
, nid
);
429 ossl_property_unlock(store
);
433 if (prop_query
!= NULL
)
434 p2
= pq
= ossl_parse_query(store
->ctx
, prop_query
, 0);
435 plp
= ossl_ctx_global_properties(store
->ctx
, 0);
436 if (plp
!= NULL
&& *plp
!= NULL
) {
440 p2
= ossl_property_merge(pq
, *plp
);
441 ossl_property_free(pq
);
449 if ((impl
= sk_IMPLEMENTATION_value(alg
->impls
, 0)) != NULL
) {
450 best_method
= &impl
->method
;
455 optional
= ossl_property_has_optional(pq
);
456 for (j
= 0; j
< sk_IMPLEMENTATION_num(alg
->impls
); j
++) {
457 impl
= sk_IMPLEMENTATION_value(alg
->impls
, j
);
458 score
= ossl_property_match_count(pq
, impl
->properties
);
460 best_method
= &impl
->method
;
468 if (ret
&& ossl_method_up_ref(best_method
))
469 *method
= best_method
->method
;
472 ossl_property_unlock(store
);
473 ossl_property_free(p2
);
477 static void impl_cache_flush_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
, void *arg
)
479 SPARSE_ARRAY_OF(ALGORITHM
) *algs
= arg
;
481 lh_QUERY_doall(alg
->cache
, &impl_cache_free
);
483 sk_IMPLEMENTATION_pop_free(alg
->impls
, &impl_free
);
484 lh_QUERY_free(alg
->cache
);
486 ossl_sa_ALGORITHM_set(algs
, idx
, NULL
);
488 lh_QUERY_flush(alg
->cache
);
492 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
)
494 ALGORITHM
*alg
= ossl_method_store_retrieve(store
, nid
);
497 ossl_provider_clear_all_operation_bits(store
->ctx
);
498 store
->nelem
-= lh_QUERY_num_items(alg
->cache
);
499 impl_cache_flush_alg(0, alg
, NULL
);
503 int ossl_method_store_flush_cache(OSSL_METHOD_STORE
*store
, int all
)
505 void *arg
= (all
!= 0 ? store
->algs
: NULL
);
507 if (!ossl_property_write_lock(store
))
509 ossl_provider_clear_all_operation_bits(store
->ctx
);
510 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_alg
, arg
);
512 ossl_property_unlock(store
);
516 IMPLEMENT_LHASH_DOALL_ARG(QUERY
, IMPL_CACHE_FLUSH
);
519 * Flush an element from the query cache (perhaps).
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.
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
534 static void impl_cache_flush_cache(QUERY
*c
, IMPL_CACHE_FLUSH
*state
)
539 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
540 * https://doi.org/10.18637/jss.v008.i14
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.
552 impl_cache_free(lh_QUERY_delete(state
->cache
, c
));
557 static void impl_cache_flush_one_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
,
560 IMPL_CACHE_FLUSH
*state
= (IMPL_CACHE_FLUSH
*)v
;
562 state
->cache
= alg
->cache
;
563 lh_QUERY_doall_IMPL_CACHE_FLUSH(state
->cache
, &impl_cache_flush_cache
,
567 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE
*store
)
569 IMPL_CACHE_FLUSH state
;
572 if ((state
.seed
= OPENSSL_rdtsc()) == 0)
574 ossl_provider_clear_all_operation_bits(store
->ctx
);
575 store
->need_flush
= 0;
576 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_one_alg
, &state
);
577 store
->nelem
= state
.nelem
;
580 int ossl_method_store_cache_get(OSSL_METHOD_STORE
*store
, int nid
,
581 const char *prop_query
, void **method
)
587 if (nid
<= 0 || store
== NULL
)
590 if (!ossl_property_read_lock(store
))
592 alg
= ossl_method_store_retrieve(store
, nid
);
596 elem
.query
= prop_query
!= NULL
? prop_query
: "";
597 r
= lh_QUERY_retrieve(alg
->cache
, &elem
);
600 if (ossl_method_up_ref(&r
->method
)) {
601 *method
= r
->method
.method
;
605 ossl_property_unlock(store
);
609 int ossl_method_store_cache_set(OSSL_METHOD_STORE
*store
, int nid
,
610 const char *prop_query
, void *method
,
611 int (*method_up_ref
)(void *),
612 void (*method_destruct
)(void *))
614 QUERY elem
, *old
, *p
= NULL
;
619 if (nid
<= 0 || store
== NULL
)
621 if (prop_query
== NULL
)
624 if (!ossl_property_write_lock(store
))
626 if (store
->need_flush
)
627 ossl_method_cache_flush_some(store
);
628 alg
= ossl_method_store_retrieve(store
, nid
);
632 if (method
== NULL
) {
633 elem
.query
= prop_query
;
634 if ((old
= lh_QUERY_delete(alg
->cache
, &elem
)) != NULL
) {
635 impl_cache_free(old
);
640 p
= OPENSSL_malloc(sizeof(*p
) + (len
= strlen(prop_query
)));
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
))
648 memcpy((char *)p
->query
, prop_query
, len
+ 1);
649 if ((old
= lh_QUERY_insert(alg
->cache
, p
)) != NULL
) {
650 impl_cache_free(old
);
653 if (!lh_QUERY_error(alg
->cache
)) {
654 if (++store
->nelem
>= IMPL_CACHE_FLUSH_THRESHOLD
)
655 store
->need_flush
= 1;
658 ossl_method_free(&p
->method
);
664 ossl_property_unlock(store
);