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 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
);
79 /* Global properties are stored per library context */
80 static void ossl_ctx_global_properties_free(void *vstore
)
82 OSSL_PROPERTY_LIST
**plp
= vstore
;
85 ossl_property_free(*plp
);
90 static void *ossl_ctx_global_properties_new(OSSL_LIB_CTX
*ctx
)
92 return OPENSSL_zalloc(sizeof(OSSL_PROPERTY_LIST
**));
96 static const OSSL_LIB_CTX_METHOD ossl_ctx_global_properties_method
= {
97 OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY
,
98 ossl_ctx_global_properties_new
,
99 ossl_ctx_global_properties_free
,
102 OSSL_PROPERTY_LIST
**ossl_ctx_global_properties(OSSL_LIB_CTX
*libctx
,
106 if (loadconfig
&& !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
))
109 return ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_GLOBAL_PROPERTIES
,
110 &ossl_ctx_global_properties_method
);
113 static int ossl_method_up_ref(METHOD
*method
)
115 return (*method
->up_ref
)(method
->method
);
118 static void ossl_method_free(METHOD
*method
)
120 (*method
->free
)(method
->method
);
123 static __owur
int ossl_property_read_lock(OSSL_METHOD_STORE
*p
)
125 return p
!= NULL
? CRYPTO_THREAD_read_lock(p
->lock
) : 0;
128 static __owur
int ossl_property_write_lock(OSSL_METHOD_STORE
*p
)
130 return p
!= NULL
? CRYPTO_THREAD_write_lock(p
->lock
) : 0;
133 static int ossl_property_unlock(OSSL_METHOD_STORE
*p
)
135 return p
!= 0 ? CRYPTO_THREAD_unlock(p
->lock
) : 0;
138 static unsigned long query_hash(const QUERY
*a
)
140 return OPENSSL_LH_strhash(a
->query
);
143 static int query_cmp(const QUERY
*a
, const QUERY
*b
)
145 return strcmp(a
->query
, b
->query
);
148 static void impl_free(IMPLEMENTATION
*impl
)
151 ossl_method_free(&impl
->method
);
156 static void impl_cache_free(QUERY
*elem
)
159 ossl_method_free(&elem
->method
);
164 static void alg_cleanup(ossl_uintmax_t idx
, ALGORITHM
*a
)
167 sk_IMPLEMENTATION_pop_free(a
->impls
, &impl_free
);
168 lh_QUERY_doall(a
->cache
, &impl_cache_free
);
169 lh_QUERY_free(a
->cache
);
175 * The OSSL_LIB_CTX param here allows access to underlying property data needed
178 OSSL_METHOD_STORE
*ossl_method_store_new(OSSL_LIB_CTX
*ctx
)
180 OSSL_METHOD_STORE
*res
;
182 res
= OPENSSL_zalloc(sizeof(*res
));
185 if ((res
->algs
= ossl_sa_ALGORITHM_new()) == NULL
) {
189 if ((res
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
190 ossl_sa_ALGORITHM_free(res
->algs
);
198 void ossl_method_store_free(OSSL_METHOD_STORE
*store
)
201 ossl_sa_ALGORITHM_doall(store
->algs
, &alg_cleanup
);
202 ossl_sa_ALGORITHM_free(store
->algs
);
203 CRYPTO_THREAD_lock_free(store
->lock
);
208 static ALGORITHM
*ossl_method_store_retrieve(OSSL_METHOD_STORE
*store
, int nid
)
210 return ossl_sa_ALGORITHM_get(store
->algs
, nid
);
213 static int ossl_method_store_insert(OSSL_METHOD_STORE
*store
, ALGORITHM
*alg
)
215 return ossl_sa_ALGORITHM_set(store
->algs
, alg
->nid
, alg
);
218 int ossl_method_store_add(OSSL_METHOD_STORE
*store
, const OSSL_PROVIDER
*prov
,
219 int nid
, const char *properties
, void *method
,
220 int (*method_up_ref
)(void *),
221 void (*method_destruct
)(void *))
223 ALGORITHM
*alg
= NULL
;
224 IMPLEMENTATION
*impl
;
228 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
230 if (properties
== NULL
)
233 /* Create new entry */
234 impl
= OPENSSL_malloc(sizeof(*impl
));
237 impl
->method
.method
= method
;
238 impl
->method
.up_ref
= method_up_ref
;
239 impl
->method
.free
= method_destruct
;
240 if (!ossl_method_up_ref(&impl
->method
)) {
244 impl
->provider
= prov
;
247 * Insert into the hash table if required.
249 * A write lock is used unconditionally because we wend our way down to the
250 * property string code which isn't locking friendly.
252 if (!ossl_property_write_lock(store
)) {
256 ossl_method_cache_flush(store
, nid
);
257 if ((impl
->properties
= ossl_prop_defn_get(store
->ctx
, properties
)) == NULL
) {
258 impl
->properties
= ossl_parse_property(store
->ctx
, properties
);
259 if (impl
->properties
== NULL
)
261 ossl_prop_defn_set(store
->ctx
, properties
, impl
->properties
);
264 alg
= ossl_method_store_retrieve(store
, nid
);
266 if ((alg
= OPENSSL_zalloc(sizeof(*alg
))) == NULL
267 || (alg
->impls
= sk_IMPLEMENTATION_new_null()) == NULL
268 || (alg
->cache
= lh_QUERY_new(&query_hash
, &query_cmp
)) == NULL
)
271 if (!ossl_method_store_insert(store
, alg
))
275 /* Push onto stack if there isn't one there already */
276 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
277 const IMPLEMENTATION
*tmpimpl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
279 if (tmpimpl
->provider
== impl
->provider
280 && tmpimpl
->properties
== impl
->properties
)
283 if (i
== sk_IMPLEMENTATION_num(alg
->impls
)
284 && sk_IMPLEMENTATION_push(alg
->impls
, impl
))
286 ossl_property_unlock(store
);
292 ossl_property_unlock(store
);
298 int ossl_method_store_remove(OSSL_METHOD_STORE
*store
, int nid
,
301 ALGORITHM
*alg
= NULL
;
304 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
307 if (!ossl_property_write_lock(store
))
309 ossl_method_cache_flush(store
, nid
);
310 alg
= ossl_method_store_retrieve(store
, nid
);
312 ossl_property_unlock(store
);
317 * A sorting find then a delete could be faster but these stacks should be
318 * relatively small, so we avoid the overhead. Sorting could also surprise
319 * users when result orderings change (even though they are not guaranteed).
321 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
322 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
324 if (impl
->method
.method
== method
) {
326 (void)sk_IMPLEMENTATION_delete(alg
->impls
, i
);
327 ossl_property_unlock(store
);
331 ossl_property_unlock(store
);
335 int ossl_method_store_fetch(OSSL_METHOD_STORE
*store
, int nid
,
336 const char *prop_query
,
339 OSSL_PROPERTY_LIST
**plp
;
341 IMPLEMENTATION
*impl
;
342 OSSL_PROPERTY_LIST
*pq
= NULL
, *p2
= NULL
;
343 METHOD
*best_method
= NULL
;
345 int j
, best
= -1, score
, optional
;
348 if (!OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
))
352 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
356 * This only needs to be a read lock, because queries never create property
357 * names or value and thus don't modify any of the property string layer.
359 if (!ossl_property_read_lock(store
))
361 alg
= ossl_method_store_retrieve(store
, nid
);
363 ossl_property_unlock(store
);
367 if (prop_query
!= NULL
)
368 p2
= pq
= ossl_parse_query(store
->ctx
, prop_query
, 0);
369 plp
= ossl_ctx_global_properties(store
->ctx
, 0);
370 if (plp
!= NULL
&& *plp
!= NULL
) {
374 p2
= ossl_property_merge(pq
, *plp
);
375 ossl_property_free(pq
);
383 if ((impl
= sk_IMPLEMENTATION_value(alg
->impls
, 0)) != NULL
) {
384 best_method
= &impl
->method
;
389 optional
= ossl_property_has_optional(pq
);
390 for (j
= 0; j
< sk_IMPLEMENTATION_num(alg
->impls
); j
++) {
391 impl
= sk_IMPLEMENTATION_value(alg
->impls
, j
);
392 score
= ossl_property_match_count(pq
, impl
->properties
);
394 best_method
= &impl
->method
;
402 if (ret
&& ossl_method_up_ref(best_method
))
403 *method
= best_method
->method
;
406 ossl_property_unlock(store
);
407 ossl_property_free(p2
);
411 static void impl_cache_flush_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
, void *arg
)
413 SPARSE_ARRAY_OF(ALGORITHM
) *algs
= arg
;
415 lh_QUERY_doall(alg
->cache
, &impl_cache_free
);
417 sk_IMPLEMENTATION_pop_free(alg
->impls
, &impl_free
);
418 lh_QUERY_free(alg
->cache
);
420 ossl_sa_ALGORITHM_set(algs
, idx
, NULL
);
422 lh_QUERY_flush(alg
->cache
);
426 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
)
428 ALGORITHM
*alg
= ossl_method_store_retrieve(store
, nid
);
431 ossl_provider_clear_all_operation_bits(store
->ctx
);
432 store
->nelem
-= lh_QUERY_num_items(alg
->cache
);
433 impl_cache_flush_alg(0, alg
, NULL
);
437 int ossl_method_store_flush_cache(OSSL_METHOD_STORE
*store
, int all
)
439 void *arg
= (all
!= 0 ? store
->algs
: NULL
);
441 if (!ossl_property_write_lock(store
))
443 ossl_provider_clear_all_operation_bits(store
->ctx
);
444 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_alg
, arg
);
446 ossl_property_unlock(store
);
450 IMPLEMENT_LHASH_DOALL_ARG(QUERY
, IMPL_CACHE_FLUSH
);
453 * Flush an element from the query cache (perhaps).
455 * In order to avoid taking a write lock or using atomic operations
456 * to keep accurate least recently used (LRU) or least frequently used
457 * (LFU) information, the procedure used here is to stochastically
458 * flush approximately half the cache.
460 * This procedure isn't ideal, LRU or LFU would be better. However,
461 * in normal operation, reaching a full cache would be unexpected.
462 * It means that no steady state of algorithm queries has been reached.
463 * That is, it is most likely an attack of some form. A suboptimal clearance
464 * strategy that doesn't degrade performance of the normal case is
465 * preferable to a more refined approach that imposes a performance
468 static void impl_cache_flush_cache(QUERY
*c
, IMPL_CACHE_FLUSH
*state
)
473 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
474 * https://doi.org/10.18637/jss.v008.i14
476 * This is a very fast PRNG so there is no need to extract bits one at a
477 * time and use the entire value each time.
486 impl_cache_free(lh_QUERY_delete(state
->cache
, c
));
491 static void impl_cache_flush_one_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
,
494 IMPL_CACHE_FLUSH
*state
= (IMPL_CACHE_FLUSH
*)v
;
496 state
->cache
= alg
->cache
;
497 lh_QUERY_doall_IMPL_CACHE_FLUSH(state
->cache
, &impl_cache_flush_cache
,
501 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE
*store
)
503 IMPL_CACHE_FLUSH state
;
506 if ((state
.seed
= OPENSSL_rdtsc()) == 0)
508 ossl_provider_clear_all_operation_bits(store
->ctx
);
509 store
->need_flush
= 0;
510 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_one_alg
, &state
);
511 store
->nelem
= state
.nelem
;
514 int ossl_method_store_cache_get(OSSL_METHOD_STORE
*store
, int nid
,
515 const char *prop_query
, void **method
)
521 if (nid
<= 0 || store
== NULL
)
524 if (!ossl_property_read_lock(store
))
526 alg
= ossl_method_store_retrieve(store
, nid
);
530 elem
.query
= prop_query
!= NULL
? prop_query
: "";
531 r
= lh_QUERY_retrieve(alg
->cache
, &elem
);
534 if (ossl_method_up_ref(&r
->method
)) {
535 *method
= r
->method
.method
;
539 ossl_property_unlock(store
);
543 int ossl_method_store_cache_set(OSSL_METHOD_STORE
*store
, int nid
,
544 const char *prop_query
, void *method
,
545 int (*method_up_ref
)(void *),
546 void (*method_destruct
)(void *))
548 QUERY elem
, *old
, *p
= NULL
;
553 if (nid
<= 0 || store
== NULL
)
555 if (prop_query
== NULL
)
558 if (!ossl_property_write_lock(store
))
560 if (store
->need_flush
)
561 ossl_method_cache_flush_some(store
);
562 alg
= ossl_method_store_retrieve(store
, nid
);
566 if (method
== NULL
) {
567 elem
.query
= prop_query
;
568 if ((old
= lh_QUERY_delete(alg
->cache
, &elem
)) != NULL
) {
569 impl_cache_free(old
);
574 p
= OPENSSL_malloc(sizeof(*p
) + (len
= strlen(prop_query
)));
577 p
->method
.method
= method
;
578 p
->method
.up_ref
= method_up_ref
;
579 p
->method
.free
= method_destruct
;
580 if (!ossl_method_up_ref(&p
->method
))
582 memcpy((char *)p
->query
, prop_query
, len
+ 1);
583 if ((old
= lh_QUERY_insert(alg
->cache
, p
)) != NULL
) {
584 impl_cache_free(old
);
587 if (!lh_QUERY_error(alg
->cache
)) {
588 if (++store
->nelem
>= IMPL_CACHE_FLUSH_THRESHOLD
)
589 store
->need_flush
= 1;
592 ossl_method_free(&p
->method
);
598 ossl_property_unlock(store
);