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(OPENSSL_CTX
*ctx
)
90 return OPENSSL_zalloc(sizeof(OSSL_PROPERTY_LIST
**));
94 static const OPENSSL_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(OPENSSL_CTX
*libctx
)
101 return openssl_ctx_get_data(libctx
, OPENSSL_CTX_GLOBAL_PROPERTIES
,
102 &ossl_ctx_global_properties_method
);
105 static int ossl_method_up_ref(METHOD
*method
)
107 return (*method
->up_ref
)(method
->method
);
110 static void ossl_method_free(METHOD
*method
)
112 (*method
->free
)(method
->method
);
115 int ossl_property_read_lock(OSSL_METHOD_STORE
*p
)
117 return p
!= NULL
? CRYPTO_THREAD_read_lock(p
->lock
) : 0;
120 int ossl_property_write_lock(OSSL_METHOD_STORE
*p
)
122 return p
!= NULL
? CRYPTO_THREAD_write_lock(p
->lock
) : 0;
125 int ossl_property_unlock(OSSL_METHOD_STORE
*p
)
127 return p
!= 0 ? CRYPTO_THREAD_unlock(p
->lock
) : 0;
130 static unsigned long query_hash(const QUERY
*a
)
132 return OPENSSL_LH_strhash(a
->query
);
135 static int query_cmp(const QUERY
*a
, const QUERY
*b
)
137 return strcmp(a
->query
, b
->query
);
140 static void impl_free(IMPLEMENTATION
*impl
)
143 ossl_method_free(&impl
->method
);
148 static void impl_cache_free(QUERY
*elem
)
151 ossl_method_free(&elem
->method
);
156 static void alg_cleanup(ossl_uintmax_t idx
, ALGORITHM
*a
)
159 sk_IMPLEMENTATION_pop_free(a
->impls
, &impl_free
);
160 lh_QUERY_doall(a
->cache
, &impl_cache_free
);
161 lh_QUERY_free(a
->cache
);
167 * The OPENSSL_CTX param here allows access to underlying property data needed
170 OSSL_METHOD_STORE
*ossl_method_store_new(OPENSSL_CTX
*ctx
)
172 OSSL_METHOD_STORE
*res
;
174 res
= OPENSSL_zalloc(sizeof(*res
));
177 if ((res
->algs
= ossl_sa_ALGORITHM_new()) == NULL
) {
181 if ((res
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
182 ossl_sa_ALGORITHM_free(res
->algs
);
190 void ossl_method_store_free(OSSL_METHOD_STORE
*store
)
193 ossl_sa_ALGORITHM_doall(store
->algs
, &alg_cleanup
);
194 ossl_sa_ALGORITHM_free(store
->algs
);
195 CRYPTO_THREAD_lock_free(store
->lock
);
200 static ALGORITHM
*ossl_method_store_retrieve(OSSL_METHOD_STORE
*store
, int nid
)
202 return ossl_sa_ALGORITHM_get(store
->algs
, nid
);
205 static int ossl_method_store_insert(OSSL_METHOD_STORE
*store
, ALGORITHM
*alg
)
207 return ossl_sa_ALGORITHM_set(store
->algs
, alg
->nid
, alg
);
210 int ossl_method_store_add(OSSL_METHOD_STORE
*store
, const OSSL_PROVIDER
*prov
,
211 int nid
, const char *properties
, void *method
,
212 int (*method_up_ref
)(void *),
213 void (*method_destruct
)(void *))
215 ALGORITHM
*alg
= NULL
;
216 IMPLEMENTATION
*impl
;
220 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
222 if (properties
== NULL
)
225 /* Create new entry */
226 impl
= OPENSSL_malloc(sizeof(*impl
));
229 impl
->method
.method
= method
;
230 impl
->method
.up_ref
= method_up_ref
;
231 impl
->method
.free
= method_destruct
;
232 if (!ossl_method_up_ref(&impl
->method
)) {
236 impl
->provider
= prov
;
239 * Insert into the hash table if required.
241 * A write lock is used unconditionally because we wend our way down to the
242 * property string code which isn't locking friendly.
244 ossl_property_write_lock(store
);
245 ossl_method_cache_flush(store
, nid
);
246 if ((impl
->properties
= ossl_prop_defn_get(store
->ctx
, properties
)) == NULL
) {
247 impl
->properties
= ossl_parse_property(store
->ctx
, properties
);
248 if (impl
->properties
== NULL
)
250 ossl_prop_defn_set(store
->ctx
, properties
, impl
->properties
);
253 alg
= ossl_method_store_retrieve(store
, nid
);
255 if ((alg
= OPENSSL_zalloc(sizeof(*alg
))) == NULL
256 || (alg
->impls
= sk_IMPLEMENTATION_new_null()) == NULL
257 || (alg
->cache
= lh_QUERY_new(&query_hash
, &query_cmp
)) == NULL
)
260 if (!ossl_method_store_insert(store
, alg
))
264 /* Push onto stack if there isn't one there already */
265 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
266 const IMPLEMENTATION
*tmpimpl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
268 if (tmpimpl
->provider
== impl
->provider
269 && tmpimpl
->properties
== impl
->properties
)
272 if (i
== sk_IMPLEMENTATION_num(alg
->impls
)
273 && sk_IMPLEMENTATION_push(alg
->impls
, impl
))
275 ossl_property_unlock(store
);
281 ossl_property_unlock(store
);
287 int ossl_method_store_remove(OSSL_METHOD_STORE
*store
, int nid
,
290 ALGORITHM
*alg
= NULL
;
293 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
296 ossl_property_write_lock(store
);
297 ossl_method_cache_flush(store
, nid
);
298 alg
= ossl_method_store_retrieve(store
, nid
);
300 ossl_property_unlock(store
);
305 * A sorting find then a delete could be faster but these stacks should be
306 * relatively small, so we avoid the overhead. Sorting could also surprise
307 * users when result orderings change (even though they are not guaranteed).
309 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
310 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
312 if (impl
->method
.method
== method
) {
314 sk_IMPLEMENTATION_delete(alg
->impls
, i
);
315 ossl_property_unlock(store
);
319 ossl_property_unlock(store
);
323 int ossl_method_store_fetch(OSSL_METHOD_STORE
*store
, int nid
,
324 const char *prop_query
,
327 OSSL_PROPERTY_LIST
**plp
;
329 IMPLEMENTATION
*impl
;
330 OSSL_PROPERTY_LIST
*pq
= NULL
, *p2
= NULL
;
331 METHOD
*best_method
= NULL
;
333 int j
, best
= -1, score
, optional
;
336 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
);
339 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
343 * This only needs to be a read lock, because queries never create property
344 * names or value and thus don't modify any of the property string layer.
346 ossl_property_read_lock(store
);
347 alg
= ossl_method_store_retrieve(store
, nid
);
349 ossl_property_unlock(store
);
353 if (prop_query
!= NULL
)
354 p2
= pq
= ossl_parse_query(store
->ctx
, prop_query
);
355 plp
= ossl_ctx_global_properties(store
->ctx
);
356 if (plp
!= NULL
&& *plp
!= NULL
) {
360 p2
= ossl_property_merge(pq
, *plp
);
363 ossl_property_free(pq
);
369 if ((impl
= sk_IMPLEMENTATION_value(alg
->impls
, 0)) != NULL
) {
370 best_method
= &impl
->method
;
375 optional
= ossl_property_has_optional(pq
);
376 for (j
= 0; j
< sk_IMPLEMENTATION_num(alg
->impls
); j
++) {
377 impl
= sk_IMPLEMENTATION_value(alg
->impls
, j
);
378 score
= ossl_property_match_count(pq
, impl
->properties
);
380 best_method
= &impl
->method
;
388 if (ret
&& ossl_method_up_ref(best_method
))
389 *method
= best_method
->method
;
392 ossl_property_unlock(store
);
393 ossl_property_free(p2
);
397 static void impl_cache_flush_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
)
399 lh_QUERY_doall(alg
->cache
, &impl_cache_free
);
400 lh_QUERY_flush(alg
->cache
);
403 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
)
405 ALGORITHM
*alg
= ossl_method_store_retrieve(store
, nid
);
408 store
->nelem
-= lh_QUERY_num_items(alg
->cache
);
409 impl_cache_flush_alg(0, alg
);
413 void ossl_method_store_flush_cache(OSSL_METHOD_STORE
*store
)
415 ossl_property_write_lock(store
);
416 ossl_sa_ALGORITHM_doall(store
->algs
, &impl_cache_flush_alg
);
418 ossl_property_unlock(store
);
421 IMPLEMENT_LHASH_DOALL_ARG(QUERY
, IMPL_CACHE_FLUSH
);
424 * Flush an element from the query cache (perhaps).
426 * In order to avoid taking a write lock or using atomic operations
427 * to keep accurate least recently used (LRU) or least frequently used
428 * (LFU) information, the procedure used here is to stochastically
429 * flush approximately half the cache.
431 * This procedure isn't ideal, LRU or LFU would be better. However,
432 * in normal operation, reaching a full cache would be unexpected.
433 * It means that no steady state of algorithm queries has been reached.
434 * That is, it is most likely an attack of some form. A suboptimal clearance
435 * strategy that doesn't degrade performance of the normal case is
436 * preferable to a more refined approach that imposes a performance
439 static void impl_cache_flush_cache(QUERY
*c
, IMPL_CACHE_FLUSH
*state
)
444 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
445 * https://doi.org/10.18637/jss.v008.i14
447 * This is a very fast PRNG so there is no need to extract bits one at a
448 * time and use the entire value each time.
457 impl_cache_free(lh_QUERY_delete(state
->cache
, c
));
462 static void impl_cache_flush_one_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
,
465 IMPL_CACHE_FLUSH
*state
= (IMPL_CACHE_FLUSH
*)v
;
467 state
->cache
= alg
->cache
;
468 lh_QUERY_doall_IMPL_CACHE_FLUSH(state
->cache
, &impl_cache_flush_cache
,
472 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE
*store
)
474 IMPL_CACHE_FLUSH state
;
477 if ((state
.seed
= OPENSSL_rdtsc()) == 0)
479 store
->need_flush
= 0;
480 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_one_alg
, &state
);
481 store
->nelem
= state
.nelem
;
484 int ossl_method_store_cache_get(OSSL_METHOD_STORE
*store
, int nid
,
485 const char *prop_query
, void **method
)
491 if (nid
<= 0 || store
== NULL
)
494 ossl_property_read_lock(store
);
495 alg
= ossl_method_store_retrieve(store
, nid
);
499 elem
.query
= prop_query
!= NULL
? prop_query
: "";
500 r
= lh_QUERY_retrieve(alg
->cache
, &elem
);
503 if (ossl_method_up_ref(&r
->method
)) {
504 *method
= r
->method
.method
;
508 ossl_property_unlock(store
);
512 int ossl_method_store_cache_set(OSSL_METHOD_STORE
*store
, int nid
,
513 const char *prop_query
, void *method
,
514 int (*method_up_ref
)(void *),
515 void (*method_destruct
)(void *))
517 QUERY elem
, *old
, *p
= NULL
;
522 if (nid
<= 0 || store
== NULL
)
524 if (prop_query
== NULL
)
527 ossl_property_write_lock(store
);
528 if (store
->need_flush
)
529 ossl_method_cache_flush_some(store
);
530 alg
= ossl_method_store_retrieve(store
, nid
);
534 if (method
== NULL
) {
535 elem
.query
= prop_query
;
536 if ((old
= lh_QUERY_delete(alg
->cache
, &elem
)) != NULL
) {
537 impl_cache_free(old
);
542 p
= OPENSSL_malloc(sizeof(*p
) + (len
= strlen(prop_query
)));
545 p
->method
.method
= method
;
546 p
->method
.up_ref
= method_up_ref
;
547 p
->method
.free
= method_destruct
;
548 if (!ossl_method_up_ref(&p
->method
))
550 memcpy((char *)p
->query
, prop_query
, len
+ 1);
551 if ((old
= lh_QUERY_insert(alg
->cache
, p
)) != NULL
) {
552 impl_cache_free(old
);
555 if (!lh_QUERY_error(alg
->cache
)) {
556 if (++store
->nelem
>= IMPL_CACHE_FLUSH_THRESHOLD
)
557 store
->need_flush
= 1;
560 ossl_method_free(&p
->method
);
566 ossl_property_unlock(store
);