2 * Copyright 2019-2020 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/property.h"
16 #include "crypto/ctype.h"
17 #include <openssl/lhash.h>
18 #include <openssl/rand.h>
19 #include "internal/thread_once.h"
20 #include "crypto/lhash.h"
21 #include "crypto/sparse_array.h"
22 #include "property_local.h"
25 * The number of elements in the query cache before we initiate a flush.
26 * If reducing this, also ensure the stochastic test in test/property_test.c
27 * isn't likely to fail.
29 #define IMPL_CACHE_FLUSH_THRESHOLD 500
33 int (*up_ref
)(void *);
38 const OSSL_PROVIDER
*provider
;
39 OSSL_PROPERTY_LIST
*properties
;
43 DEFINE_STACK_OF(IMPLEMENTATION
)
51 DEFINE_LHASH_OF(QUERY
);
55 STACK_OF(IMPLEMENTATION
) *impls
;
56 LHASH_OF(QUERY
) *cache
;
59 struct ossl_method_store_st
{
62 SPARSE_ARRAY_OF(ALGORITHM
) *algs
;
68 LHASH_OF(QUERY
) *cache
;
73 DEFINE_SPARSE_ARRAY_OF(ALGORITHM
);
75 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
);
77 /* Global properties are stored per library context */
78 static void ossl_ctx_global_properties_free(void *vstore
)
80 OSSL_PROPERTY_LIST
**plp
= vstore
;
83 ossl_property_free(*plp
);
88 static void *ossl_ctx_global_properties_new(OSSL_LIB_CTX
*ctx
)
90 return OPENSSL_zalloc(sizeof(OSSL_PROPERTY_LIST
**));
94 static const OSSL_LIB_CTX_METHOD ossl_ctx_global_properties_method
= {
95 ossl_ctx_global_properties_new
,
96 ossl_ctx_global_properties_free
,
99 OSSL_PROPERTY_LIST
**ossl_ctx_global_properties(OSSL_LIB_CTX
*libctx
,
103 if (loadconfig
&& !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
))
106 return ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_GLOBAL_PROPERTIES
,
107 &ossl_ctx_global_properties_method
);
110 static int ossl_method_up_ref(METHOD
*method
)
112 return (*method
->up_ref
)(method
->method
);
115 static void ossl_method_free(METHOD
*method
)
117 (*method
->free
)(method
->method
);
120 int ossl_property_read_lock(OSSL_METHOD_STORE
*p
)
122 return p
!= NULL
? CRYPTO_THREAD_read_lock(p
->lock
) : 0;
125 int ossl_property_write_lock(OSSL_METHOD_STORE
*p
)
127 return p
!= NULL
? CRYPTO_THREAD_write_lock(p
->lock
) : 0;
130 int ossl_property_unlock(OSSL_METHOD_STORE
*p
)
132 return p
!= 0 ? CRYPTO_THREAD_unlock(p
->lock
) : 0;
135 static unsigned long query_hash(const QUERY
*a
)
137 return OPENSSL_LH_strhash(a
->query
);
140 static int query_cmp(const QUERY
*a
, const QUERY
*b
)
142 return strcmp(a
->query
, b
->query
);
145 static void impl_free(IMPLEMENTATION
*impl
)
148 ossl_method_free(&impl
->method
);
153 static void impl_cache_free(QUERY
*elem
)
156 ossl_method_free(&elem
->method
);
161 static void alg_cleanup(ossl_uintmax_t idx
, ALGORITHM
*a
)
164 sk_IMPLEMENTATION_pop_free(a
->impls
, &impl_free
);
165 lh_QUERY_doall(a
->cache
, &impl_cache_free
);
166 lh_QUERY_free(a
->cache
);
172 * The OSSL_LIB_CTX param here allows access to underlying property data needed
175 OSSL_METHOD_STORE
*ossl_method_store_new(OSSL_LIB_CTX
*ctx
)
177 OSSL_METHOD_STORE
*res
;
179 res
= OPENSSL_zalloc(sizeof(*res
));
182 if ((res
->algs
= ossl_sa_ALGORITHM_new()) == NULL
) {
186 if ((res
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
187 ossl_sa_ALGORITHM_free(res
->algs
);
195 void ossl_method_store_free(OSSL_METHOD_STORE
*store
)
198 ossl_sa_ALGORITHM_doall(store
->algs
, &alg_cleanup
);
199 ossl_sa_ALGORITHM_free(store
->algs
);
200 CRYPTO_THREAD_lock_free(store
->lock
);
205 static ALGORITHM
*ossl_method_store_retrieve(OSSL_METHOD_STORE
*store
, int nid
)
207 return ossl_sa_ALGORITHM_get(store
->algs
, nid
);
210 static int ossl_method_store_insert(OSSL_METHOD_STORE
*store
, ALGORITHM
*alg
)
212 return ossl_sa_ALGORITHM_set(store
->algs
, alg
->nid
, alg
);
215 int ossl_method_store_add(OSSL_METHOD_STORE
*store
, const OSSL_PROVIDER
*prov
,
216 int nid
, const char *properties
, void *method
,
217 int (*method_up_ref
)(void *),
218 void (*method_destruct
)(void *))
220 ALGORITHM
*alg
= NULL
;
221 IMPLEMENTATION
*impl
;
225 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
227 if (properties
== NULL
)
230 /* Create new entry */
231 impl
= OPENSSL_malloc(sizeof(*impl
));
234 impl
->method
.method
= method
;
235 impl
->method
.up_ref
= method_up_ref
;
236 impl
->method
.free
= method_destruct
;
237 if (!ossl_method_up_ref(&impl
->method
)) {
241 impl
->provider
= prov
;
244 * Insert into the hash table if required.
246 * A write lock is used unconditionally because we wend our way down to the
247 * property string code which isn't locking friendly.
249 ossl_property_write_lock(store
);
250 ossl_method_cache_flush(store
, nid
);
251 if ((impl
->properties
= ossl_prop_defn_get(store
->ctx
, properties
)) == NULL
) {
252 impl
->properties
= ossl_parse_property(store
->ctx
, properties
);
253 if (impl
->properties
== NULL
)
255 ossl_prop_defn_set(store
->ctx
, properties
, impl
->properties
);
258 alg
= ossl_method_store_retrieve(store
, nid
);
260 if ((alg
= OPENSSL_zalloc(sizeof(*alg
))) == NULL
261 || (alg
->impls
= sk_IMPLEMENTATION_new_null()) == NULL
262 || (alg
->cache
= lh_QUERY_new(&query_hash
, &query_cmp
)) == NULL
)
265 if (!ossl_method_store_insert(store
, alg
))
269 /* Push onto stack if there isn't one there already */
270 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
271 const IMPLEMENTATION
*tmpimpl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
273 if (tmpimpl
->provider
== impl
->provider
274 && tmpimpl
->properties
== impl
->properties
)
277 if (i
== sk_IMPLEMENTATION_num(alg
->impls
)
278 && sk_IMPLEMENTATION_push(alg
->impls
, impl
))
280 ossl_property_unlock(store
);
286 ossl_property_unlock(store
);
292 int ossl_method_store_remove(OSSL_METHOD_STORE
*store
, int nid
,
295 ALGORITHM
*alg
= NULL
;
298 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
301 ossl_property_write_lock(store
);
302 ossl_method_cache_flush(store
, nid
);
303 alg
= ossl_method_store_retrieve(store
, nid
);
305 ossl_property_unlock(store
);
310 * A sorting find then a delete could be faster but these stacks should be
311 * relatively small, so we avoid the overhead. Sorting could also surprise
312 * users when result orderings change (even though they are not guaranteed).
314 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
315 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
317 if (impl
->method
.method
== method
) {
319 (void)sk_IMPLEMENTATION_delete(alg
->impls
, i
);
320 ossl_property_unlock(store
);
324 ossl_property_unlock(store
);
328 int ossl_method_store_fetch(OSSL_METHOD_STORE
*store
, int nid
,
329 const char *prop_query
,
332 OSSL_PROPERTY_LIST
**plp
;
334 IMPLEMENTATION
*impl
;
335 OSSL_PROPERTY_LIST
*pq
= NULL
, *p2
= NULL
;
336 METHOD
*best_method
= NULL
;
338 int j
, best
= -1, score
, optional
;
341 if (!OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
))
345 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
349 * This only needs to be a read lock, because queries never create property
350 * names or value and thus don't modify any of the property string layer.
352 ossl_property_read_lock(store
);
353 alg
= ossl_method_store_retrieve(store
, nid
);
355 ossl_property_unlock(store
);
359 if (prop_query
!= NULL
)
360 p2
= pq
= ossl_parse_query(store
->ctx
, prop_query
);
361 plp
= ossl_ctx_global_properties(store
->ctx
, 0);
362 if (plp
!= NULL
&& *plp
!= NULL
) {
366 p2
= ossl_property_merge(pq
, *plp
);
367 ossl_property_free(pq
);
375 if ((impl
= sk_IMPLEMENTATION_value(alg
->impls
, 0)) != NULL
) {
376 best_method
= &impl
->method
;
381 optional
= ossl_property_has_optional(pq
);
382 for (j
= 0; j
< sk_IMPLEMENTATION_num(alg
->impls
); j
++) {
383 impl
= sk_IMPLEMENTATION_value(alg
->impls
, j
);
384 score
= ossl_property_match_count(pq
, impl
->properties
);
386 best_method
= &impl
->method
;
394 if (ret
&& ossl_method_up_ref(best_method
))
395 *method
= best_method
->method
;
398 ossl_property_unlock(store
);
399 ossl_property_free(p2
);
403 static void impl_cache_flush_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
, void *arg
)
405 SPARSE_ARRAY_OF(ALGORITHM
) *algs
= arg
;
407 lh_QUERY_doall(alg
->cache
, &impl_cache_free
);
409 sk_IMPLEMENTATION_pop_free(alg
->impls
, &impl_free
);
410 lh_QUERY_free(alg
->cache
);
412 ossl_sa_ALGORITHM_set(algs
, idx
, NULL
);
414 lh_QUERY_flush(alg
->cache
);
418 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
)
420 ALGORITHM
*alg
= ossl_method_store_retrieve(store
, nid
);
423 store
->nelem
-= lh_QUERY_num_items(alg
->cache
);
424 impl_cache_flush_alg(0, alg
, NULL
);
428 void ossl_method_store_flush_cache(OSSL_METHOD_STORE
*store
, int all
)
430 void *arg
= (all
!= 0 ? store
->algs
: NULL
);
432 ossl_property_write_lock(store
);
433 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_alg
, arg
);
435 ossl_property_unlock(store
);
438 IMPLEMENT_LHASH_DOALL_ARG(QUERY
, IMPL_CACHE_FLUSH
);
441 * Flush an element from the query cache (perhaps).
443 * In order to avoid taking a write lock or using atomic operations
444 * to keep accurate least recently used (LRU) or least frequently used
445 * (LFU) information, the procedure used here is to stochastically
446 * flush approximately half the cache.
448 * This procedure isn't ideal, LRU or LFU would be better. However,
449 * in normal operation, reaching a full cache would be unexpected.
450 * It means that no steady state of algorithm queries has been reached.
451 * That is, it is most likely an attack of some form. A suboptimal clearance
452 * strategy that doesn't degrade performance of the normal case is
453 * preferable to a more refined approach that imposes a performance
456 static void impl_cache_flush_cache(QUERY
*c
, IMPL_CACHE_FLUSH
*state
)
461 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
462 * https://doi.org/10.18637/jss.v008.i14
464 * This is a very fast PRNG so there is no need to extract bits one at a
465 * time and use the entire value each time.
474 impl_cache_free(lh_QUERY_delete(state
->cache
, c
));
479 static void impl_cache_flush_one_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
,
482 IMPL_CACHE_FLUSH
*state
= (IMPL_CACHE_FLUSH
*)v
;
484 state
->cache
= alg
->cache
;
485 lh_QUERY_doall_IMPL_CACHE_FLUSH(state
->cache
, &impl_cache_flush_cache
,
489 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE
*store
)
491 IMPL_CACHE_FLUSH state
;
494 if ((state
.seed
= OPENSSL_rdtsc()) == 0)
496 store
->need_flush
= 0;
497 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_one_alg
, &state
);
498 store
->nelem
= state
.nelem
;
501 int ossl_method_store_cache_get(OSSL_METHOD_STORE
*store
, int nid
,
502 const char *prop_query
, void **method
)
508 if (nid
<= 0 || store
== NULL
)
511 ossl_property_read_lock(store
);
512 alg
= ossl_method_store_retrieve(store
, nid
);
516 elem
.query
= prop_query
!= NULL
? prop_query
: "";
517 r
= lh_QUERY_retrieve(alg
->cache
, &elem
);
520 if (ossl_method_up_ref(&r
->method
)) {
521 *method
= r
->method
.method
;
525 ossl_property_unlock(store
);
529 int ossl_method_store_cache_set(OSSL_METHOD_STORE
*store
, int nid
,
530 const char *prop_query
, void *method
,
531 int (*method_up_ref
)(void *),
532 void (*method_destruct
)(void *))
534 QUERY elem
, *old
, *p
= NULL
;
539 if (nid
<= 0 || store
== NULL
)
541 if (prop_query
== NULL
)
544 ossl_property_write_lock(store
);
545 if (store
->need_flush
)
546 ossl_method_cache_flush_some(store
);
547 alg
= ossl_method_store_retrieve(store
, nid
);
551 if (method
== NULL
) {
552 elem
.query
= prop_query
;
553 if ((old
= lh_QUERY_delete(alg
->cache
, &elem
)) != NULL
) {
554 impl_cache_free(old
);
559 p
= OPENSSL_malloc(sizeof(*p
) + (len
= strlen(prop_query
)));
562 p
->method
.method
= method
;
563 p
->method
.up_ref
= method_up_ref
;
564 p
->method
.free
= method_destruct
;
565 if (!ossl_method_up_ref(&p
->method
))
567 memcpy((char *)p
->query
, prop_query
, len
+ 1);
568 if ((old
= lh_QUERY_insert(alg
->cache
, p
)) != NULL
) {
569 impl_cache_free(old
);
572 if (!lh_QUERY_error(alg
->cache
)) {
573 if (++store
->nelem
>= IMPL_CACHE_FLUSH_THRESHOLD
)
574 store
->need_flush
= 1;
577 ossl_method_free(&p
->method
);
583 ossl_property_unlock(store
);