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 "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
;
63 OSSL_PROPERTY_LIST
*global_properties
;
69 LHASH_OF(QUERY
) *cache
;
74 DEFINE_SPARSE_ARRAY_OF(ALGORITHM
);
76 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
);
77 static void ossl_method_cache_flush_all(OSSL_METHOD_STORE
*c
);
79 static int ossl_method_up_ref(METHOD
*method
)
81 return (*method
->up_ref
)(method
->method
);
84 static void ossl_method_free(METHOD
*method
)
86 (*method
->free
)(method
->method
);
89 int ossl_property_read_lock(OSSL_METHOD_STORE
*p
)
91 return p
!= NULL
? CRYPTO_THREAD_read_lock(p
->lock
) : 0;
94 int ossl_property_write_lock(OSSL_METHOD_STORE
*p
)
96 return p
!= NULL
? CRYPTO_THREAD_write_lock(p
->lock
) : 0;
99 int ossl_property_unlock(OSSL_METHOD_STORE
*p
)
101 return p
!= 0 ? CRYPTO_THREAD_unlock(p
->lock
) : 0;
104 static unsigned long query_hash(const QUERY
*a
)
106 return OPENSSL_LH_strhash(a
->query
);
109 static int query_cmp(const QUERY
*a
, const QUERY
*b
)
111 return strcmp(a
->query
, b
->query
);
114 static void impl_free(IMPLEMENTATION
*impl
)
117 ossl_method_free(&impl
->method
);
122 static void impl_cache_free(QUERY
*elem
)
125 ossl_method_free(&elem
->method
);
130 static void alg_cleanup(ossl_uintmax_t idx
, ALGORITHM
*a
)
133 sk_IMPLEMENTATION_pop_free(a
->impls
, &impl_free
);
134 lh_QUERY_doall(a
->cache
, &impl_cache_free
);
135 lh_QUERY_free(a
->cache
);
141 * The OPENSSL_CTX param here allows access to underlying property data needed
144 OSSL_METHOD_STORE
*ossl_method_store_new(OPENSSL_CTX
*ctx
)
146 OSSL_METHOD_STORE
*res
;
148 res
= OPENSSL_zalloc(sizeof(*res
));
151 if ((res
->algs
= ossl_sa_ALGORITHM_new()) == NULL
) {
155 if ((res
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
156 ossl_sa_ALGORITHM_free(res
->algs
);
164 void ossl_method_store_free(OSSL_METHOD_STORE
*store
)
167 ossl_sa_ALGORITHM_doall(store
->algs
, &alg_cleanup
);
168 ossl_sa_ALGORITHM_free(store
->algs
);
169 ossl_property_free(store
->global_properties
);
170 CRYPTO_THREAD_lock_free(store
->lock
);
175 static ALGORITHM
*ossl_method_store_retrieve(OSSL_METHOD_STORE
*store
, int nid
)
177 return ossl_sa_ALGORITHM_get(store
->algs
, nid
);
180 static int ossl_method_store_insert(OSSL_METHOD_STORE
*store
, ALGORITHM
*alg
)
182 return ossl_sa_ALGORITHM_set(store
->algs
, alg
->nid
, alg
);
185 int ossl_method_store_add(OSSL_METHOD_STORE
*store
, const OSSL_PROVIDER
*prov
,
186 int nid
, const char *properties
, void *method
,
187 int (*method_up_ref
)(void *),
188 void (*method_destruct
)(void *))
190 ALGORITHM
*alg
= NULL
;
191 IMPLEMENTATION
*impl
;
195 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
197 if (properties
== NULL
)
200 /* Create new entry */
201 impl
= OPENSSL_malloc(sizeof(*impl
));
204 impl
->method
.method
= method
;
205 impl
->method
.up_ref
= method_up_ref
;
206 impl
->method
.free
= method_destruct
;
207 if (!ossl_method_up_ref(&impl
->method
)) {
211 impl
->provider
= prov
;
214 * Insert into the hash table if required.
216 * A write lock is used unconditionally because we wend our way down to the
217 * property string code which isn't locking friendly.
219 ossl_property_write_lock(store
);
220 ossl_method_cache_flush(store
, nid
);
221 if ((impl
->properties
= ossl_prop_defn_get(store
->ctx
, properties
)) == NULL
) {
222 impl
->properties
= ossl_parse_property(store
->ctx
, properties
);
223 if (impl
->properties
== NULL
)
225 ossl_prop_defn_set(store
->ctx
, properties
, impl
->properties
);
228 alg
= ossl_method_store_retrieve(store
, nid
);
230 if ((alg
= OPENSSL_zalloc(sizeof(*alg
))) == NULL
231 || (alg
->impls
= sk_IMPLEMENTATION_new_null()) == NULL
232 || (alg
->cache
= lh_QUERY_new(&query_hash
, &query_cmp
)) == NULL
)
235 if (!ossl_method_store_insert(store
, alg
))
239 /* Push onto stack if there isn't one there already */
240 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
241 const IMPLEMENTATION
*tmpimpl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
243 if (tmpimpl
->provider
== impl
->provider
244 && tmpimpl
->properties
== impl
->properties
)
247 if (i
== sk_IMPLEMENTATION_num(alg
->impls
)
248 && sk_IMPLEMENTATION_push(alg
->impls
, impl
))
250 ossl_property_unlock(store
);
256 ossl_property_unlock(store
);
262 int ossl_method_store_remove(OSSL_METHOD_STORE
*store
, int nid
,
265 ALGORITHM
*alg
= NULL
;
268 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
271 ossl_property_write_lock(store
);
272 ossl_method_cache_flush(store
, nid
);
273 alg
= ossl_method_store_retrieve(store
, nid
);
275 ossl_property_unlock(store
);
280 * A sorting find then a delete could be faster but these stacks should be
281 * relatively small, so we avoid the overhead. Sorting could also surprise
282 * users when result orderings change (even though they are not guaranteed).
284 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
285 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
287 if (impl
->method
.method
== method
) {
289 sk_IMPLEMENTATION_delete(alg
->impls
, i
);
290 ossl_property_unlock(store
);
294 ossl_property_unlock(store
);
298 int ossl_method_store_fetch(OSSL_METHOD_STORE
*store
, int nid
,
299 const char *prop_query
, void **method
)
302 IMPLEMENTATION
*impl
;
303 OSSL_PROPERTY_LIST
*pq
= NULL
, *p2
;
304 METHOD
*best_method
= NULL
;
306 int j
, best
= -1, score
, optional
;
309 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
);
312 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
316 * This only needs to be a read lock, because queries never create property
317 * names or value and thus don't modify any of the property string layer.
319 ossl_property_read_lock(store
);
320 alg
= ossl_method_store_retrieve(store
, nid
);
322 ossl_property_unlock(store
);
326 if (prop_query
== NULL
) {
327 if ((impl
= sk_IMPLEMENTATION_value(alg
->impls
, 0)) != NULL
) {
328 best_method
= &impl
->method
;
333 pq
= ossl_parse_query(store
->ctx
, prop_query
);
336 if (store
->global_properties
!= NULL
) {
337 p2
= ossl_property_merge(pq
, store
->global_properties
);
340 ossl_property_free(pq
);
343 optional
= ossl_property_has_optional(pq
);
344 for (j
= 0; j
< sk_IMPLEMENTATION_num(alg
->impls
); j
++) {
345 impl
= sk_IMPLEMENTATION_value(alg
->impls
, j
);
346 score
= ossl_property_match_count(pq
, impl
->properties
);
348 best_method
= &impl
->method
;
356 if (ret
&& ossl_method_up_ref(best_method
))
357 *method
= best_method
->method
;
360 ossl_property_unlock(store
);
361 ossl_property_free(pq
);
365 int ossl_method_store_set_global_properties(OSSL_METHOD_STORE
*store
,
366 const char *prop_query
) {
372 ossl_property_write_lock(store
);
373 ossl_method_cache_flush_all(store
);
374 if (prop_query
== NULL
) {
375 ossl_property_free(store
->global_properties
);
376 store
->global_properties
= NULL
;
377 ossl_property_unlock(store
);
380 store
->global_properties
= ossl_parse_query(store
->ctx
, prop_query
);
381 ret
= store
->global_properties
!= NULL
;
382 ossl_property_unlock(store
);
386 static void impl_cache_flush_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
)
388 lh_QUERY_doall(alg
->cache
, &impl_cache_free
);
389 lh_QUERY_flush(alg
->cache
);
392 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
)
394 ALGORITHM
*alg
= ossl_method_store_retrieve(store
, nid
);
397 store
->nelem
-= lh_QUERY_num_items(alg
->cache
);
398 impl_cache_flush_alg(0, alg
);
402 static void ossl_method_cache_flush_all(OSSL_METHOD_STORE
*store
)
404 ossl_sa_ALGORITHM_doall(store
->algs
, &impl_cache_flush_alg
);
408 IMPLEMENT_LHASH_DOALL_ARG(QUERY
, IMPL_CACHE_FLUSH
);
411 * Flush an element from the query cache (perhaps).
413 * In order to avoid taking a write lock or using atomic operations
414 * to keep accurate least recently used (LRU) or least frequently used
415 * (LFU) information, the procedure used here is to stochastically
416 * flush approximately half the cache.
418 * This procedure isn't ideal, LRU or LFU would be better. However,
419 * in normal operation, reaching a full cache would be unexpected.
420 * It means that no steady state of algorithm queries has been reached.
421 * That is, it is most likely an attack of some form. A suboptimal clearance
422 * strategy that doesn't degrade performance of the normal case is
423 * preferable to a more refined approach that imposes a performance
426 static void impl_cache_flush_cache(QUERY
*c
, IMPL_CACHE_FLUSH
*state
)
431 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
432 * https://doi.org/10.18637/jss.v008.i14
434 * This is a very fast PRNG so there is no need to extract bits one at a
435 * time and use the entire value each time.
444 impl_cache_free(lh_QUERY_delete(state
->cache
, c
));
449 static void impl_cache_flush_one_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
,
452 IMPL_CACHE_FLUSH
*state
= (IMPL_CACHE_FLUSH
*)v
;
454 state
->cache
= alg
->cache
;
455 lh_QUERY_doall_IMPL_CACHE_FLUSH(state
->cache
, &impl_cache_flush_cache
,
459 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE
*store
)
461 IMPL_CACHE_FLUSH state
;
464 if ((state
.seed
= OPENSSL_rdtsc()) == 0)
466 store
->need_flush
= 0;
467 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_one_alg
, &state
);
468 store
->nelem
= state
.nelem
;
471 int ossl_method_store_cache_get(OSSL_METHOD_STORE
*store
, int nid
,
472 const char *prop_query
, void **method
)
478 if (nid
<= 0 || store
== NULL
)
481 ossl_property_read_lock(store
);
482 alg
= ossl_method_store_retrieve(store
, nid
);
486 elem
.query
= prop_query
!= NULL
? prop_query
: "";
487 r
= lh_QUERY_retrieve(alg
->cache
, &elem
);
490 if (ossl_method_up_ref(&r
->method
)) {
491 *method
= r
->method
.method
;
495 ossl_property_unlock(store
);
499 int ossl_method_store_cache_set(OSSL_METHOD_STORE
*store
, int nid
,
500 const char *prop_query
, void *method
,
501 int (*method_up_ref
)(void *),
502 void (*method_destruct
)(void *))
504 QUERY elem
, *old
, *p
= NULL
;
509 if (nid
<= 0 || store
== NULL
)
511 if (prop_query
== NULL
)
514 ossl_property_write_lock(store
);
515 if (store
->need_flush
)
516 ossl_method_cache_flush_some(store
);
517 alg
= ossl_method_store_retrieve(store
, nid
);
521 if (method
== NULL
) {
522 elem
.query
= prop_query
;
523 if ((old
= lh_QUERY_delete(alg
->cache
, &elem
)) != NULL
) {
524 impl_cache_free(old
);
529 p
= OPENSSL_malloc(sizeof(*p
) + (len
= strlen(prop_query
)));
532 p
->method
.method
= method
;
533 p
->method
.up_ref
= method_up_ref
;
534 p
->method
.free
= method_destruct
;
535 if (!ossl_method_up_ref(&p
->method
))
537 memcpy((char *)p
->query
, prop_query
, len
+ 1);
538 if ((old
= lh_QUERY_insert(alg
->cache
, p
)) != NULL
) {
539 impl_cache_free(old
);
542 if (!lh_QUERY_error(alg
->cache
)) {
543 if (++store
->nelem
>= IMPL_CACHE_FLUSH_THRESHOLD
)
544 store
->need_flush
= 1;
547 ossl_method_free(&p
->method
);
553 ossl_property_unlock(store
);