2 * Copyright 2019 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 "internal/ctype.h"
17 #include <openssl/lhash.h>
18 #include <openssl/rand.h>
19 #include "internal/thread_once.h"
20 #include "internal/lhash.h"
21 #include "internal/sparse_array.h"
22 #include "property_lcl.h"
24 /* The number of elements in the query cache before we initiate a flush */
25 #define IMPL_CACHE_FLUSH_THRESHOLD 500
28 OSSL_PROPERTY_LIST
*properties
;
30 void (*method_destruct
)(void *);
33 DEFINE_STACK_OF(IMPLEMENTATION
)
41 DEFINE_LHASH_OF(QUERY
);
45 STACK_OF(IMPLEMENTATION
) *impls
;
46 LHASH_OF(QUERY
) *cache
;
49 struct ossl_method_store_st
{
52 SPARSE_ARRAY_OF(ALGORITHM
) *algs
;
53 OSSL_PROPERTY_LIST
*global_properties
;
56 unsigned char rand_bits
[(IMPL_CACHE_FLUSH_THRESHOLD
+ 7) / 8];
61 LHASH_OF(QUERY
) *cache
;
66 DEFINE_SPARSE_ARRAY_OF(ALGORITHM
);
68 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
);
69 static void ossl_method_cache_flush_all(OSSL_METHOD_STORE
*c
);
71 int ossl_property_read_lock(OSSL_METHOD_STORE
*p
)
73 return p
!= NULL
? CRYPTO_THREAD_read_lock(p
->lock
) : 0;
76 int ossl_property_write_lock(OSSL_METHOD_STORE
*p
)
78 return p
!= NULL
? CRYPTO_THREAD_write_lock(p
->lock
) : 0;
81 int ossl_property_unlock(OSSL_METHOD_STORE
*p
)
83 return p
!= 0 ? CRYPTO_THREAD_unlock(p
->lock
) : 0;
86 static openssl_ctx_run_once_fn do_method_store_init
;
87 int do_method_store_init(OPENSSL_CTX
*ctx
)
89 return ossl_property_parse_init(ctx
);
92 static unsigned long query_hash(const QUERY
*a
)
94 return OPENSSL_LH_strhash(a
->query
);
97 static int query_cmp(const QUERY
*a
, const QUERY
*b
)
99 return strcmp(a
->query
, b
->query
);
102 static void impl_free(IMPLEMENTATION
*impl
)
105 if (impl
->method_destruct
)
106 impl
->method_destruct(impl
->method
);
111 static void impl_cache_free(QUERY
*elem
)
116 static void alg_cleanup(ossl_uintmax_t idx
, ALGORITHM
*a
)
119 sk_IMPLEMENTATION_pop_free(a
->impls
, &impl_free
);
120 lh_QUERY_doall(a
->cache
, &impl_cache_free
);
121 lh_QUERY_free(a
->cache
);
127 * The OPENSSL_CTX param here allows access to underlying property data needed
130 OSSL_METHOD_STORE
*ossl_method_store_new(OPENSSL_CTX
*ctx
)
132 OSSL_METHOD_STORE
*res
;
134 if (!openssl_ctx_run_once(ctx
,
135 OPENSSL_CTX_METHOD_STORE_RUN_ONCE_INDEX
,
136 do_method_store_init
))
139 res
= OPENSSL_zalloc(sizeof(*res
));
142 if ((res
->algs
= ossl_sa_ALGORITHM_new()) == NULL
) {
146 if ((res
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
147 OPENSSL_free(res
->algs
);
155 void ossl_method_store_free(OSSL_METHOD_STORE
*store
)
158 ossl_sa_ALGORITHM_doall(store
->algs
, &alg_cleanup
);
159 ossl_sa_ALGORITHM_free(store
->algs
);
160 ossl_property_free(store
->global_properties
);
161 CRYPTO_THREAD_lock_free(store
->lock
);
166 static ALGORITHM
*ossl_method_store_retrieve(OSSL_METHOD_STORE
*store
, int nid
)
168 return ossl_sa_ALGORITHM_get(store
->algs
, nid
);
171 static int ossl_method_store_insert(OSSL_METHOD_STORE
*store
, ALGORITHM
*alg
)
173 return ossl_sa_ALGORITHM_set(store
->algs
, alg
->nid
, alg
);
176 int ossl_method_store_add(OSSL_METHOD_STORE
*store
,
177 int nid
, const char *properties
, void *method
,
178 int (*method_up_ref
)(void *),
179 void (*method_destruct
)(void *))
181 ALGORITHM
*alg
= NULL
;
182 IMPLEMENTATION
*impl
;
185 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
187 if (properties
== NULL
)
190 /* Create new entry */
191 impl
= OPENSSL_malloc(sizeof(*impl
));
194 if (method_up_ref
!= NULL
&& !method_up_ref(method
))
196 impl
->method
= method
;
197 impl
->method_destruct
= method_destruct
;
200 * Insert into the hash table if required.
202 * A write lock is used unconditionally because we wend our way down to the
203 * property string code which isn't locking friendly.
205 ossl_property_write_lock(store
);
206 ossl_method_cache_flush(store
, nid
);
207 if ((impl
->properties
= ossl_prop_defn_get(store
->ctx
, properties
)) == NULL
) {
208 impl
->properties
= ossl_parse_property(store
->ctx
, properties
);
209 if (impl
->properties
== NULL
)
211 ossl_prop_defn_set(store
->ctx
, properties
, impl
->properties
);
214 alg
= ossl_method_store_retrieve(store
, nid
);
216 if ((alg
= OPENSSL_zalloc(sizeof(*alg
))) == NULL
217 || (alg
->impls
= sk_IMPLEMENTATION_new_null()) == NULL
218 || (alg
->cache
= lh_QUERY_new(&query_hash
, &query_cmp
)) == NULL
)
221 if (!ossl_method_store_insert(store
, alg
))
225 /* Push onto stack */
226 if (sk_IMPLEMENTATION_push(alg
->impls
, impl
))
228 ossl_property_unlock(store
);
234 ossl_property_unlock(store
);
240 int ossl_method_store_remove(OSSL_METHOD_STORE
*store
, int nid
,
243 ALGORITHM
*alg
= NULL
;
246 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
249 ossl_property_write_lock(store
);
250 ossl_method_cache_flush(store
, nid
);
251 alg
= ossl_method_store_retrieve(store
, nid
);
253 ossl_property_unlock(store
);
258 * A sorting find then a delete could be faster but these stacks should be
259 * relatively small, so we avoid the overhead. Sorting could also surprise
260 * users when result orderings change (even though they are not guaranteed).
262 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
263 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
265 if (impl
->method
== method
) {
266 sk_IMPLEMENTATION_delete(alg
->impls
, i
);
267 ossl_property_unlock(store
);
272 ossl_property_unlock(store
);
276 int ossl_method_store_fetch(OSSL_METHOD_STORE
*store
, int nid
,
277 const char *prop_query
, void **method
)
280 IMPLEMENTATION
*impl
;
281 OSSL_PROPERTY_LIST
*pq
= NULL
, *p2
;
283 int j
, best
= -1, score
, optional
;
286 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
);
289 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
293 * This only needs to be a read lock, because queries never create property
294 * names or value and thus don't modify any of the property string layer.
296 ossl_property_read_lock(store
);
297 alg
= ossl_method_store_retrieve(store
, nid
);
299 ossl_property_unlock(store
);
303 if (prop_query
== NULL
) {
304 if ((impl
= sk_IMPLEMENTATION_value(alg
->impls
, 0)) != NULL
) {
305 *method
= impl
->method
;
310 pq
= ossl_parse_query(store
->ctx
, prop_query
);
313 if (store
->global_properties
!= NULL
) {
314 p2
= ossl_property_merge(pq
, store
->global_properties
);
317 ossl_property_free(pq
);
320 optional
= ossl_property_has_optional(pq
);
321 for (j
= 0; j
< sk_IMPLEMENTATION_num(alg
->impls
); j
++) {
322 impl
= sk_IMPLEMENTATION_value(alg
->impls
, j
);
323 score
= ossl_property_match_count(pq
, impl
->properties
);
325 *method
= impl
->method
;
333 ossl_property_unlock(store
);
334 ossl_property_free(pq
);
338 int ossl_method_store_set_global_properties(OSSL_METHOD_STORE
*store
,
339 const char *prop_query
) {
345 ossl_property_write_lock(store
);
346 ossl_method_cache_flush_all(store
);
347 if (prop_query
== NULL
) {
348 ossl_property_free(store
->global_properties
);
349 store
->global_properties
= NULL
;
350 ossl_property_unlock(store
);
353 store
->global_properties
= ossl_parse_query(store
->ctx
, prop_query
);
354 ret
= store
->global_properties
!= NULL
;
355 ossl_property_unlock(store
);
359 static void impl_cache_flush_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
)
361 lh_QUERY_doall(alg
->cache
, &impl_cache_free
);
362 lh_QUERY_flush(alg
->cache
);
365 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
)
367 ALGORITHM
*alg
= ossl_method_store_retrieve(store
, nid
);
370 store
->nelem
-= lh_QUERY_num_items(alg
->cache
);
371 impl_cache_flush_alg(0, alg
);
375 static void ossl_method_cache_flush_all(OSSL_METHOD_STORE
*store
)
377 ossl_sa_ALGORITHM_doall(store
->algs
, &impl_cache_flush_alg
);
381 IMPLEMENT_LHASH_DOALL_ARG(QUERY
, IMPL_CACHE_FLUSH
);
384 * Flush an element from the query cache (perhaps).
386 * In order to avoid taking a write lock or using atomic operations
387 * to keep accurate least recently used (LRU) or least frequently used
388 * (LFU) information, the procedure used here is to stochastically
389 * flush approximately half the cache.
391 * This procedure isn't ideal, LRU or LFU would be better. However,
392 * in normal operation, reaching a full cache would be unexpected.
393 * It means that no steady state of algorithm queries has been reached.
394 * That is, it is most likely an attack of some form. A suboptimal clearance
395 * strategy that doesn't degrade performance of the normal case is
396 * preferable to a more refined approach that imposes a performance
399 static void impl_cache_flush_cache(QUERY
*c
, IMPL_CACHE_FLUSH
*state
)
404 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
405 * https://doi.org/10.18637/jss.v008.i14
407 * This is a very fast PRNG so there is no need to extract bits one at a
408 * time and use the entire value each time.
417 OPENSSL_free(lh_QUERY_delete(state
->cache
, c
));
422 static void impl_cache_flush_one_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
,
425 IMPL_CACHE_FLUSH
*state
= (IMPL_CACHE_FLUSH
*)v
;
427 state
->cache
= alg
->cache
;
428 lh_QUERY_doall_IMPL_CACHE_FLUSH(state
->cache
, &impl_cache_flush_cache
,
432 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE
*store
)
434 IMPL_CACHE_FLUSH state
;
437 if ((state
.seed
= OPENSSL_rdtsc()) == 0)
439 store
->need_flush
= 0;
440 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_one_alg
, &state
);
441 store
->nelem
= state
.nelem
;
444 int ossl_method_store_cache_get(OSSL_METHOD_STORE
*store
, int nid
,
445 const char *prop_query
, void **method
)
450 if (nid
<= 0 || store
== NULL
)
453 ossl_property_read_lock(store
);
454 alg
= ossl_method_store_retrieve(store
, nid
);
456 ossl_property_unlock(store
);
460 elem
.query
= prop_query
!= NULL
? prop_query
: "";
461 r
= lh_QUERY_retrieve(alg
->cache
, &elem
);
463 ossl_property_unlock(store
);
467 ossl_property_unlock(store
);
471 int ossl_method_store_cache_set(OSSL_METHOD_STORE
*store
, int nid
,
472 const char *prop_query
, void *method
)
474 QUERY elem
, *old
, *p
= NULL
;
478 if (nid
<= 0 || store
== NULL
)
480 if (prop_query
== NULL
)
483 ossl_property_write_lock(store
);
484 if (store
->need_flush
)
485 ossl_method_cache_flush_some(store
);
486 alg
= ossl_method_store_retrieve(store
, nid
);
488 ossl_property_unlock(store
);
492 if (method
== NULL
) {
493 elem
.query
= prop_query
;
494 if (lh_QUERY_delete(alg
->cache
, &elem
) != NULL
)
496 ossl_property_unlock(store
);
499 p
= OPENSSL_malloc(sizeof(*p
) + (len
= strlen(prop_query
)));
503 memcpy((char *)p
->query
, prop_query
, len
+ 1);
504 if ((old
= lh_QUERY_insert(alg
->cache
, p
)) != NULL
) {
506 ossl_property_unlock(store
);
509 if (!lh_QUERY_error(alg
->cache
)) {
510 if (++store
->nelem
>= IMPL_CACHE_FLUSH_THRESHOLD
)
511 store
->need_flush
= 1;
512 ossl_property_unlock(store
);
516 ossl_property_unlock(store
);