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_ctx_global_properties_new
,
98 ossl_ctx_global_properties_free
,
101 OSSL_PROPERTY_LIST
**ossl_ctx_global_properties(OSSL_LIB_CTX
*libctx
,
105 if (loadconfig
&& !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
))
108 return ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_GLOBAL_PROPERTIES
,
109 &ossl_ctx_global_properties_method
);
112 static int ossl_method_up_ref(METHOD
*method
)
114 return (*method
->up_ref
)(method
->method
);
117 static void ossl_method_free(METHOD
*method
)
119 (*method
->free
)(method
->method
);
122 static __owur
int ossl_property_read_lock(OSSL_METHOD_STORE
*p
)
124 return p
!= NULL
? CRYPTO_THREAD_read_lock(p
->lock
) : 0;
127 static __owur
int ossl_property_write_lock(OSSL_METHOD_STORE
*p
)
129 return p
!= NULL
? CRYPTO_THREAD_write_lock(p
->lock
) : 0;
132 static int ossl_property_unlock(OSSL_METHOD_STORE
*p
)
134 return p
!= 0 ? CRYPTO_THREAD_unlock(p
->lock
) : 0;
137 static unsigned long query_hash(const QUERY
*a
)
139 return OPENSSL_LH_strhash(a
->query
);
142 static int query_cmp(const QUERY
*a
, const QUERY
*b
)
144 return strcmp(a
->query
, b
->query
);
147 static void impl_free(IMPLEMENTATION
*impl
)
150 ossl_method_free(&impl
->method
);
155 static void impl_cache_free(QUERY
*elem
)
158 ossl_method_free(&elem
->method
);
163 static void alg_cleanup(ossl_uintmax_t idx
, ALGORITHM
*a
)
166 sk_IMPLEMENTATION_pop_free(a
->impls
, &impl_free
);
167 lh_QUERY_doall(a
->cache
, &impl_cache_free
);
168 lh_QUERY_free(a
->cache
);
174 * The OSSL_LIB_CTX param here allows access to underlying property data needed
177 OSSL_METHOD_STORE
*ossl_method_store_new(OSSL_LIB_CTX
*ctx
)
179 OSSL_METHOD_STORE
*res
;
181 res
= OPENSSL_zalloc(sizeof(*res
));
184 if ((res
->algs
= ossl_sa_ALGORITHM_new()) == NULL
) {
188 if ((res
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
189 ossl_sa_ALGORITHM_free(res
->algs
);
197 void ossl_method_store_free(OSSL_METHOD_STORE
*store
)
200 ossl_sa_ALGORITHM_doall(store
->algs
, &alg_cleanup
);
201 ossl_sa_ALGORITHM_free(store
->algs
);
202 CRYPTO_THREAD_lock_free(store
->lock
);
207 static ALGORITHM
*ossl_method_store_retrieve(OSSL_METHOD_STORE
*store
, int nid
)
209 return ossl_sa_ALGORITHM_get(store
->algs
, nid
);
212 static int ossl_method_store_insert(OSSL_METHOD_STORE
*store
, ALGORITHM
*alg
)
214 return ossl_sa_ALGORITHM_set(store
->algs
, alg
->nid
, alg
);
217 int ossl_method_store_add(OSSL_METHOD_STORE
*store
, const OSSL_PROVIDER
*prov
,
218 int nid
, const char *properties
, void *method
,
219 int (*method_up_ref
)(void *),
220 void (*method_destruct
)(void *))
222 ALGORITHM
*alg
= NULL
;
223 IMPLEMENTATION
*impl
;
227 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
229 if (properties
== NULL
)
232 /* Create new entry */
233 impl
= OPENSSL_malloc(sizeof(*impl
));
236 impl
->method
.method
= method
;
237 impl
->method
.up_ref
= method_up_ref
;
238 impl
->method
.free
= method_destruct
;
239 if (!ossl_method_up_ref(&impl
->method
)) {
243 impl
->provider
= prov
;
246 * Insert into the hash table if required.
248 * A write lock is used unconditionally because we wend our way down to the
249 * property string code which isn't locking friendly.
251 if (!ossl_property_write_lock(store
)) {
255 ossl_method_cache_flush(store
, nid
);
256 if ((impl
->properties
= ossl_prop_defn_get(store
->ctx
, properties
)) == NULL
) {
257 impl
->properties
= ossl_parse_property(store
->ctx
, properties
);
258 if (impl
->properties
== NULL
)
260 ossl_prop_defn_set(store
->ctx
, properties
, impl
->properties
);
263 alg
= ossl_method_store_retrieve(store
, nid
);
265 if ((alg
= OPENSSL_zalloc(sizeof(*alg
))) == NULL
266 || (alg
->impls
= sk_IMPLEMENTATION_new_null()) == NULL
267 || (alg
->cache
= lh_QUERY_new(&query_hash
, &query_cmp
)) == NULL
)
270 if (!ossl_method_store_insert(store
, alg
))
274 /* Push onto stack if there isn't one there already */
275 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
276 const IMPLEMENTATION
*tmpimpl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
278 if (tmpimpl
->provider
== impl
->provider
279 && tmpimpl
->properties
== impl
->properties
)
282 if (i
== sk_IMPLEMENTATION_num(alg
->impls
)
283 && sk_IMPLEMENTATION_push(alg
->impls
, impl
))
285 ossl_property_unlock(store
);
291 ossl_property_unlock(store
);
297 int ossl_method_store_remove(OSSL_METHOD_STORE
*store
, int nid
,
300 ALGORITHM
*alg
= NULL
;
303 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
306 if (!ossl_property_write_lock(store
))
308 ossl_method_cache_flush(store
, nid
);
309 alg
= ossl_method_store_retrieve(store
, nid
);
311 ossl_property_unlock(store
);
316 * A sorting find then a delete could be faster but these stacks should be
317 * relatively small, so we avoid the overhead. Sorting could also surprise
318 * users when result orderings change (even though they are not guaranteed).
320 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
321 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
323 if (impl
->method
.method
== method
) {
325 (void)sk_IMPLEMENTATION_delete(alg
->impls
, i
);
326 ossl_property_unlock(store
);
330 ossl_property_unlock(store
);
334 int ossl_method_store_fetch(OSSL_METHOD_STORE
*store
, int nid
,
335 const char *prop_query
,
338 OSSL_PROPERTY_LIST
**plp
;
340 IMPLEMENTATION
*impl
;
341 OSSL_PROPERTY_LIST
*pq
= NULL
, *p2
= NULL
;
342 METHOD
*best_method
= NULL
;
344 int j
, best
= -1, score
, optional
;
347 if (!OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
))
351 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
355 * This only needs to be a read lock, because queries never create property
356 * names or value and thus don't modify any of the property string layer.
358 if (!ossl_property_read_lock(store
))
360 alg
= ossl_method_store_retrieve(store
, nid
);
362 ossl_property_unlock(store
);
366 if (prop_query
!= NULL
)
367 p2
= pq
= ossl_parse_query(store
->ctx
, prop_query
, 0);
368 plp
= ossl_ctx_global_properties(store
->ctx
, 0);
369 if (plp
!= NULL
&& *plp
!= NULL
) {
373 p2
= ossl_property_merge(pq
, *plp
);
374 ossl_property_free(pq
);
382 if ((impl
= sk_IMPLEMENTATION_value(alg
->impls
, 0)) != NULL
) {
383 best_method
= &impl
->method
;
388 optional
= ossl_property_has_optional(pq
);
389 for (j
= 0; j
< sk_IMPLEMENTATION_num(alg
->impls
); j
++) {
390 impl
= sk_IMPLEMENTATION_value(alg
->impls
, j
);
391 score
= ossl_property_match_count(pq
, impl
->properties
);
393 best_method
= &impl
->method
;
401 if (ret
&& ossl_method_up_ref(best_method
))
402 *method
= best_method
->method
;
405 ossl_property_unlock(store
);
406 ossl_property_free(p2
);
410 static void impl_cache_flush_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
, void *arg
)
412 SPARSE_ARRAY_OF(ALGORITHM
) *algs
= arg
;
414 lh_QUERY_doall(alg
->cache
, &impl_cache_free
);
416 sk_IMPLEMENTATION_pop_free(alg
->impls
, &impl_free
);
417 lh_QUERY_free(alg
->cache
);
419 ossl_sa_ALGORITHM_set(algs
, idx
, NULL
);
421 lh_QUERY_flush(alg
->cache
);
425 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
)
427 ALGORITHM
*alg
= ossl_method_store_retrieve(store
, nid
);
430 ossl_provider_clear_all_operation_bits(store
->ctx
);
431 store
->nelem
-= lh_QUERY_num_items(alg
->cache
);
432 impl_cache_flush_alg(0, alg
, NULL
);
436 int ossl_method_store_flush_cache(OSSL_METHOD_STORE
*store
, int all
)
438 void *arg
= (all
!= 0 ? store
->algs
: NULL
);
440 if (!ossl_property_write_lock(store
))
442 ossl_provider_clear_all_operation_bits(store
->ctx
);
443 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_alg
, arg
);
445 ossl_property_unlock(store
);
449 IMPLEMENT_LHASH_DOALL_ARG(QUERY
, IMPL_CACHE_FLUSH
);
452 * Flush an element from the query cache (perhaps).
454 * In order to avoid taking a write lock or using atomic operations
455 * to keep accurate least recently used (LRU) or least frequently used
456 * (LFU) information, the procedure used here is to stochastically
457 * flush approximately half the cache.
459 * This procedure isn't ideal, LRU or LFU would be better. However,
460 * in normal operation, reaching a full cache would be unexpected.
461 * It means that no steady state of algorithm queries has been reached.
462 * That is, it is most likely an attack of some form. A suboptimal clearance
463 * strategy that doesn't degrade performance of the normal case is
464 * preferable to a more refined approach that imposes a performance
467 static void impl_cache_flush_cache(QUERY
*c
, IMPL_CACHE_FLUSH
*state
)
472 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
473 * https://doi.org/10.18637/jss.v008.i14
475 * This is a very fast PRNG so there is no need to extract bits one at a
476 * time and use the entire value each time.
485 impl_cache_free(lh_QUERY_delete(state
->cache
, c
));
490 static void impl_cache_flush_one_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
,
493 IMPL_CACHE_FLUSH
*state
= (IMPL_CACHE_FLUSH
*)v
;
495 state
->cache
= alg
->cache
;
496 lh_QUERY_doall_IMPL_CACHE_FLUSH(state
->cache
, &impl_cache_flush_cache
,
500 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE
*store
)
502 IMPL_CACHE_FLUSH state
;
505 if ((state
.seed
= OPENSSL_rdtsc()) == 0)
507 ossl_provider_clear_all_operation_bits(store
->ctx
);
508 store
->need_flush
= 0;
509 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_one_alg
, &state
);
510 store
->nelem
= state
.nelem
;
513 int ossl_method_store_cache_get(OSSL_METHOD_STORE
*store
, int nid
,
514 const char *prop_query
, void **method
)
520 if (nid
<= 0 || store
== NULL
)
523 if (!ossl_property_read_lock(store
))
525 alg
= ossl_method_store_retrieve(store
, nid
);
529 elem
.query
= prop_query
!= NULL
? prop_query
: "";
530 r
= lh_QUERY_retrieve(alg
->cache
, &elem
);
533 if (ossl_method_up_ref(&r
->method
)) {
534 *method
= r
->method
.method
;
538 ossl_property_unlock(store
);
542 int ossl_method_store_cache_set(OSSL_METHOD_STORE
*store
, int nid
,
543 const char *prop_query
, void *method
,
544 int (*method_up_ref
)(void *),
545 void (*method_destruct
)(void *))
547 QUERY elem
, *old
, *p
= NULL
;
552 if (nid
<= 0 || store
== NULL
)
554 if (prop_query
== NULL
)
557 if (!ossl_property_write_lock(store
))
559 if (store
->need_flush
)
560 ossl_method_cache_flush_some(store
);
561 alg
= ossl_method_store_retrieve(store
, nid
);
565 if (method
== NULL
) {
566 elem
.query
= prop_query
;
567 if ((old
= lh_QUERY_delete(alg
->cache
, &elem
)) != NULL
) {
568 impl_cache_free(old
);
573 p
= OPENSSL_malloc(sizeof(*p
) + (len
= strlen(prop_query
)));
576 p
->method
.method
= method
;
577 p
->method
.up_ref
= method_up_ref
;
578 p
->method
.free
= method_destruct
;
579 if (!ossl_method_up_ref(&p
->method
))
581 memcpy((char *)p
->query
, prop_query
, len
+ 1);
582 if ((old
= lh_QUERY_insert(alg
->cache
, p
)) != NULL
) {
583 impl_cache_free(old
);
586 if (!lh_QUERY_error(alg
->cache
)) {
587 if (++store
->nelem
>= IMPL_CACHE_FLUSH_THRESHOLD
)
588 store
->need_flush
= 1;
591 ossl_method_free(&p
->method
);
597 ossl_property_unlock(store
);