2 * Copyright 2019-2022 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"
25 #include "crypto/context.h"
28 * The number of elements in the query cache before we initiate a flush.
29 * If reducing this, also ensure the stochastic test in test/property_test.c
30 * isn't likely to fail.
32 #define IMPL_CACHE_FLUSH_THRESHOLD 500
36 int (*up_ref
)(void *);
41 const OSSL_PROVIDER
*provider
;
42 OSSL_PROPERTY_LIST
*properties
;
46 DEFINE_STACK_OF(IMPLEMENTATION
)
49 const OSSL_PROVIDER
*provider
;
55 DEFINE_LHASH_OF_EX(QUERY
);
59 STACK_OF(IMPLEMENTATION
) *impls
;
60 LHASH_OF(QUERY
) *cache
;
63 struct ossl_method_store_st
{
65 SPARSE_ARRAY_OF(ALGORITHM
) *algs
;
68 /* query cache specific values */
70 /* Count of the query cache entries for all algs */
73 /* Flag: 1 if query cache entries for all algs need flushing */
78 LHASH_OF(QUERY
) *cache
;
83 DEFINE_SPARSE_ARRAY_OF(ALGORITHM
);
85 typedef struct ossl_global_properties_st
{
86 OSSL_PROPERTY_LIST
*list
;
88 unsigned int no_mirrored
: 1;
90 } OSSL_GLOBAL_PROPERTIES
;
92 static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE
*store
,
94 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
);
96 /* Global properties are stored per library context */
97 void ossl_ctx_global_properties_free(void *vglobp
)
99 OSSL_GLOBAL_PROPERTIES
*globp
= vglobp
;
102 ossl_property_free(globp
->list
);
107 void *ossl_ctx_global_properties_new(OSSL_LIB_CTX
*ctx
)
109 return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES
));
112 OSSL_PROPERTY_LIST
**ossl_ctx_global_properties(OSSL_LIB_CTX
*libctx
,
115 OSSL_GLOBAL_PROPERTIES
*globp
;
118 if (loadconfig
&& !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
))
121 globp
= ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_GLOBAL_PROPERTIES
);
123 return globp
!= NULL
? &globp
->list
: NULL
;
127 int ossl_global_properties_no_mirrored(OSSL_LIB_CTX
*libctx
)
129 OSSL_GLOBAL_PROPERTIES
*globp
130 = ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_GLOBAL_PROPERTIES
);
132 return globp
!= NULL
&& globp
->no_mirrored
? 1 : 0;
135 void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX
*libctx
)
137 OSSL_GLOBAL_PROPERTIES
*globp
138 = ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_GLOBAL_PROPERTIES
);
141 globp
->no_mirrored
= 1;
145 static int ossl_method_up_ref(METHOD
*method
)
147 return (*method
->up_ref
)(method
->method
);
150 static void ossl_method_free(METHOD
*method
)
152 (*method
->free
)(method
->method
);
155 static __owur
int ossl_property_read_lock(OSSL_METHOD_STORE
*p
)
157 return p
!= NULL
? CRYPTO_THREAD_read_lock(p
->lock
) : 0;
160 static __owur
int ossl_property_write_lock(OSSL_METHOD_STORE
*p
)
162 return p
!= NULL
? CRYPTO_THREAD_write_lock(p
->lock
) : 0;
165 static int ossl_property_unlock(OSSL_METHOD_STORE
*p
)
167 return p
!= 0 ? CRYPTO_THREAD_unlock(p
->lock
) : 0;
170 static unsigned long query_hash(const QUERY
*a
)
172 return OPENSSL_LH_strhash(a
->query
);
175 static int query_cmp(const QUERY
*a
, const QUERY
*b
)
177 int res
= strcmp(a
->query
, b
->query
);
179 if (res
== 0 && a
->provider
!= NULL
&& b
->provider
!= NULL
)
180 res
= b
->provider
> a
->provider
? 1
181 : b
->provider
< a
->provider
? -1
186 static void impl_free(IMPLEMENTATION
*impl
)
189 ossl_method_free(&impl
->method
);
194 static void impl_cache_free(QUERY
*elem
)
197 ossl_method_free(&elem
->method
);
202 static void impl_cache_flush_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
)
204 lh_QUERY_doall(alg
->cache
, &impl_cache_free
);
205 lh_QUERY_flush(alg
->cache
);
208 static void alg_cleanup(ossl_uintmax_t idx
, ALGORITHM
*a
, void *arg
)
210 OSSL_METHOD_STORE
*store
= arg
;
213 sk_IMPLEMENTATION_pop_free(a
->impls
, &impl_free
);
214 lh_QUERY_doall(a
->cache
, &impl_cache_free
);
215 lh_QUERY_free(a
->cache
);
219 ossl_sa_ALGORITHM_set(store
->algs
, idx
, NULL
);
223 * The OSSL_LIB_CTX param here allows access to underlying property data needed
226 OSSL_METHOD_STORE
*ossl_method_store_new(OSSL_LIB_CTX
*ctx
)
228 OSSL_METHOD_STORE
*res
;
230 res
= OPENSSL_zalloc(sizeof(*res
));
233 if ((res
->algs
= ossl_sa_ALGORITHM_new()) == NULL
) {
237 if ((res
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
238 ossl_sa_ALGORITHM_free(res
->algs
);
246 void ossl_method_store_free(OSSL_METHOD_STORE
*store
)
249 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &alg_cleanup
, store
);
250 ossl_sa_ALGORITHM_free(store
->algs
);
251 CRYPTO_THREAD_lock_free(store
->lock
);
256 static ALGORITHM
*ossl_method_store_retrieve(OSSL_METHOD_STORE
*store
, int nid
)
258 return ossl_sa_ALGORITHM_get(store
->algs
, nid
);
261 static int ossl_method_store_insert(OSSL_METHOD_STORE
*store
, ALGORITHM
*alg
)
263 return ossl_sa_ALGORITHM_set(store
->algs
, alg
->nid
, alg
);
266 int ossl_method_store_add(OSSL_METHOD_STORE
*store
, const OSSL_PROVIDER
*prov
,
267 int nid
, const char *properties
, void *method
,
268 int (*method_up_ref
)(void *),
269 void (*method_destruct
)(void *))
271 ALGORITHM
*alg
= NULL
;
272 IMPLEMENTATION
*impl
;
276 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
278 if (properties
== NULL
)
281 if (!ossl_assert(prov
!= NULL
))
284 /* Create new entry */
285 impl
= OPENSSL_malloc(sizeof(*impl
));
288 impl
->method
.method
= method
;
289 impl
->method
.up_ref
= method_up_ref
;
290 impl
->method
.free
= method_destruct
;
291 if (!ossl_method_up_ref(&impl
->method
)) {
295 impl
->provider
= prov
;
297 /* Insert into the hash table if required */
298 if (!ossl_property_write_lock(store
)) {
302 ossl_method_cache_flush(store
, nid
);
303 if ((impl
->properties
= ossl_prop_defn_get(store
->ctx
, properties
)) == NULL
) {
304 impl
->properties
= ossl_parse_property(store
->ctx
, properties
);
305 if (impl
->properties
== NULL
)
307 if (!ossl_prop_defn_set(store
->ctx
, properties
, impl
->properties
)) {
308 ossl_property_free(impl
->properties
);
309 impl
->properties
= NULL
;
314 alg
= ossl_method_store_retrieve(store
, nid
);
316 if ((alg
= OPENSSL_zalloc(sizeof(*alg
))) == NULL
317 || (alg
->impls
= sk_IMPLEMENTATION_new_null()) == NULL
318 || (alg
->cache
= lh_QUERY_new(&query_hash
, &query_cmp
)) == NULL
)
321 if (!ossl_method_store_insert(store
, alg
))
325 /* Push onto stack if there isn't one there already */
326 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
327 const IMPLEMENTATION
*tmpimpl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
329 if (tmpimpl
->provider
== impl
->provider
330 && tmpimpl
->properties
== impl
->properties
)
333 if (i
== sk_IMPLEMENTATION_num(alg
->impls
)
334 && sk_IMPLEMENTATION_push(alg
->impls
, impl
))
336 ossl_property_unlock(store
);
342 ossl_property_unlock(store
);
343 alg_cleanup(0, alg
, NULL
);
348 int ossl_method_store_remove(OSSL_METHOD_STORE
*store
, int nid
,
351 ALGORITHM
*alg
= NULL
;
354 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
357 if (!ossl_property_write_lock(store
))
359 ossl_method_cache_flush(store
, nid
);
360 alg
= ossl_method_store_retrieve(store
, nid
);
362 ossl_property_unlock(store
);
367 * A sorting find then a delete could be faster but these stacks should be
368 * relatively small, so we avoid the overhead. Sorting could also surprise
369 * users when result orderings change (even though they are not guaranteed).
371 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
372 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
374 if (impl
->method
.method
== method
) {
376 (void)sk_IMPLEMENTATION_delete(alg
->impls
, i
);
377 ossl_property_unlock(store
);
381 ossl_property_unlock(store
);
385 struct alg_cleanup_by_provider_data_st
{
386 OSSL_METHOD_STORE
*store
;
387 const OSSL_PROVIDER
*prov
;
391 alg_cleanup_by_provider(ossl_uintmax_t idx
, ALGORITHM
*alg
, void *arg
)
393 struct alg_cleanup_by_provider_data_st
*data
= arg
;
397 * We walk the stack backwards, to avoid having to deal with stack shifts
400 for (count
= 0, i
= sk_IMPLEMENTATION_num(alg
->impls
); i
-- > 0;) {
401 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
403 if (impl
->provider
== data
->prov
) {
405 (void)sk_IMPLEMENTATION_delete(alg
->impls
, i
);
411 * If we removed any implementation, we also clear the whole associated
412 * cache, 'cause that's the sensible thing to do.
413 * There's no point flushing the cache entries where we didn't remove
414 * any implementation, though.
417 ossl_method_cache_flush_alg(data
->store
, alg
);
420 int ossl_method_store_remove_all_provided(OSSL_METHOD_STORE
*store
,
421 const OSSL_PROVIDER
*prov
)
423 struct alg_cleanup_by_provider_data_st data
;
425 if (!ossl_property_write_lock(store
))
429 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &alg_cleanup_by_provider
, &data
);
430 ossl_property_unlock(store
);
434 static void alg_do_one(ALGORITHM
*alg
, IMPLEMENTATION
*impl
,
435 void (*fn
)(int id
, void *method
, void *fnarg
),
438 fn(alg
->nid
, impl
->method
.method
, fnarg
);
441 struct alg_do_each_data_st
{
442 void (*fn
)(int id
, void *method
, void *fnarg
);
446 static void alg_do_each(ossl_uintmax_t idx
, ALGORITHM
*alg
, void *arg
)
448 struct alg_do_each_data_st
*data
= arg
;
449 int i
, end
= sk_IMPLEMENTATION_num(alg
->impls
);
451 for (i
= 0; i
< end
; i
++) {
452 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
454 alg_do_one(alg
, impl
, data
->fn
, data
->fnarg
);
458 void ossl_method_store_do_all(OSSL_METHOD_STORE
*store
,
459 void (*fn
)(int id
, void *method
, void *fnarg
),
462 struct alg_do_each_data_st data
;
467 ossl_sa_ALGORITHM_doall_arg(store
->algs
, alg_do_each
, &data
);
470 int ossl_method_store_fetch(OSSL_METHOD_STORE
*store
,
471 int nid
, const char *prop_query
,
472 const OSSL_PROVIDER
**prov_rw
, void **method
)
474 OSSL_PROPERTY_LIST
**plp
;
476 IMPLEMENTATION
*impl
, *best_impl
= NULL
;
477 OSSL_PROPERTY_LIST
*pq
= NULL
, *p2
= NULL
;
478 const OSSL_PROVIDER
*prov
= prov_rw
!= NULL
? *prov_rw
: NULL
;
480 int j
, best
= -1, score
, optional
;
483 if (!OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
))
487 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
490 /* This only needs to be a read lock, because the query won't create anything */
491 if (!ossl_property_read_lock(store
))
493 alg
= ossl_method_store_retrieve(store
, nid
);
495 ossl_property_unlock(store
);
499 if (prop_query
!= NULL
)
500 p2
= pq
= ossl_parse_query(store
->ctx
, prop_query
, 0);
501 plp
= ossl_ctx_global_properties(store
->ctx
, 0);
502 if (plp
!= NULL
&& *plp
!= NULL
) {
506 p2
= ossl_property_merge(pq
, *plp
);
507 ossl_property_free(pq
);
515 for (j
= 0; j
< sk_IMPLEMENTATION_num(alg
->impls
); j
++) {
516 if ((impl
= sk_IMPLEMENTATION_value(alg
->impls
, j
)) != NULL
517 && (prov
== NULL
|| impl
->provider
== prov
)) {
525 optional
= ossl_property_has_optional(pq
);
526 for (j
= 0; j
< sk_IMPLEMENTATION_num(alg
->impls
); j
++) {
527 if ((impl
= sk_IMPLEMENTATION_value(alg
->impls
, j
)) != NULL
528 && (prov
== NULL
|| impl
->provider
== prov
)) {
529 score
= ossl_property_match_count(pq
, impl
->properties
);
540 if (ret
&& ossl_method_up_ref(&best_impl
->method
)) {
541 *method
= best_impl
->method
.method
;
543 *prov_rw
= best_impl
->provider
;
547 ossl_property_unlock(store
);
548 ossl_property_free(p2
);
552 static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE
*store
,
555 store
->cache_nelem
-= lh_QUERY_num_items(alg
->cache
);
556 impl_cache_flush_alg(0, alg
);
559 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
)
561 ALGORITHM
*alg
= ossl_method_store_retrieve(store
, nid
);
564 ossl_method_cache_flush_alg(store
, alg
);
567 int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE
*store
)
569 if (!ossl_property_write_lock(store
))
571 ossl_sa_ALGORITHM_doall(store
->algs
, &impl_cache_flush_alg
);
572 store
->cache_nelem
= 0;
573 ossl_property_unlock(store
);
577 IMPLEMENT_LHASH_DOALL_ARG(QUERY
, IMPL_CACHE_FLUSH
);
580 * Flush an element from the query cache (perhaps).
582 * In order to avoid taking a write lock or using atomic operations
583 * to keep accurate least recently used (LRU) or least frequently used
584 * (LFU) information, the procedure used here is to stochastically
585 * flush approximately half the cache.
587 * This procedure isn't ideal, LRU or LFU would be better. However,
588 * in normal operation, reaching a full cache would be unexpected.
589 * It means that no steady state of algorithm queries has been reached.
590 * That is, it is most likely an attack of some form. A suboptimal clearance
591 * strategy that doesn't degrade performance of the normal case is
592 * preferable to a more refined approach that imposes a performance
595 static void impl_cache_flush_cache(QUERY
*c
, IMPL_CACHE_FLUSH
*state
)
600 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
601 * https://doi.org/10.18637/jss.v008.i14
603 * This is a very fast PRNG so there is no need to extract bits one at a
604 * time and use the entire value each time.
613 impl_cache_free(lh_QUERY_delete(state
->cache
, c
));
618 static void impl_cache_flush_one_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
,
621 IMPL_CACHE_FLUSH
*state
= (IMPL_CACHE_FLUSH
*)v
;
623 state
->cache
= alg
->cache
;
624 lh_QUERY_doall_IMPL_CACHE_FLUSH(state
->cache
, &impl_cache_flush_cache
,
628 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE
*store
)
630 IMPL_CACHE_FLUSH state
;
633 if ((state
.seed
= OPENSSL_rdtsc()) == 0)
635 store
->cache_need_flush
= 0;
636 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_one_alg
, &state
);
637 store
->cache_nelem
= state
.nelem
;
640 int ossl_method_store_cache_get(OSSL_METHOD_STORE
*store
, OSSL_PROVIDER
*prov
,
641 int nid
, const char *prop_query
, void **method
)
647 if (nid
<= 0 || store
== NULL
|| prop_query
== NULL
)
650 if (!ossl_property_read_lock(store
))
652 alg
= ossl_method_store_retrieve(store
, nid
);
656 elem
.query
= prop_query
;
657 elem
.provider
= prov
;
658 r
= lh_QUERY_retrieve(alg
->cache
, &elem
);
661 if (ossl_method_up_ref(&r
->method
)) {
662 *method
= r
->method
.method
;
666 ossl_property_unlock(store
);
670 int ossl_method_store_cache_set(OSSL_METHOD_STORE
*store
, OSSL_PROVIDER
*prov
,
671 int nid
, const char *prop_query
, void *method
,
672 int (*method_up_ref
)(void *),
673 void (*method_destruct
)(void *))
675 QUERY elem
, *old
, *p
= NULL
;
680 if (nid
<= 0 || store
== NULL
|| prop_query
== NULL
)
683 if (!ossl_assert(prov
!= NULL
))
686 if (!ossl_property_write_lock(store
))
688 if (store
->cache_need_flush
)
689 ossl_method_cache_flush_some(store
);
690 alg
= ossl_method_store_retrieve(store
, nid
);
694 if (method
== NULL
) {
695 elem
.query
= prop_query
;
696 elem
.provider
= prov
;
697 if ((old
= lh_QUERY_delete(alg
->cache
, &elem
)) != NULL
) {
698 impl_cache_free(old
);
699 store
->cache_nelem
--;
703 p
= OPENSSL_malloc(sizeof(*p
) + (len
= strlen(prop_query
)));
707 p
->method
.method
= method
;
708 p
->method
.up_ref
= method_up_ref
;
709 p
->method
.free
= method_destruct
;
710 if (!ossl_method_up_ref(&p
->method
))
712 memcpy((char *)p
->query
, prop_query
, len
+ 1);
713 if ((old
= lh_QUERY_insert(alg
->cache
, p
)) != NULL
) {
714 impl_cache_free(old
);
717 if (!lh_QUERY_error(alg
->cache
)) {
718 if (++store
->cache_nelem
>= IMPL_CACHE_FLUSH_THRESHOLD
)
719 store
->cache_need_flush
= 1;
722 ossl_method_free(&p
->method
);
728 ossl_property_unlock(store
);