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 OSSL_METHOD_STORE
*store
;
62 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
,
178 void *method
, void (*method_destruct
)(void *))
180 ALGORITHM
*alg
= NULL
;
181 IMPLEMENTATION
*impl
;
184 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
186 if (properties
== NULL
)
189 /* Create new entry */
190 impl
= OPENSSL_malloc(sizeof(*impl
));
193 impl
->method
= method
;
194 impl
->method_destruct
= method_destruct
;
197 * Insert into the hash table if required.
199 * A write lock is used unconditionally because we wend our way down to the
200 * property string code which isn't locking friendly.
202 ossl_property_write_lock(store
);
203 ossl_method_cache_flush(store
, nid
);
204 if ((impl
->properties
= ossl_prop_defn_get(store
->ctx
, properties
)) == NULL
) {
205 impl
->properties
= ossl_parse_property(store
->ctx
, properties
);
206 if (impl
->properties
== NULL
)
208 ossl_prop_defn_set(store
->ctx
, properties
, impl
->properties
);
211 alg
= ossl_method_store_retrieve(store
, nid
);
213 if ((alg
= OPENSSL_zalloc(sizeof(*alg
))) == NULL
214 || (alg
->impls
= sk_IMPLEMENTATION_new_null()) == NULL
215 || (alg
->cache
= lh_QUERY_new(&query_hash
, &query_cmp
)) == NULL
)
218 if (!ossl_method_store_insert(store
, alg
))
222 /* Push onto stack */
223 if (sk_IMPLEMENTATION_push(alg
->impls
, impl
))
225 ossl_property_unlock(store
);
231 ossl_property_unlock(store
);
237 int ossl_method_store_remove(OSSL_METHOD_STORE
*store
, int nid
,
240 ALGORITHM
*alg
= NULL
;
243 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
246 ossl_property_write_lock(store
);
247 ossl_method_cache_flush(store
, nid
);
248 alg
= ossl_method_store_retrieve(store
, nid
);
250 ossl_property_unlock(store
);
255 * A sorting find then a delete could be faster but these stacks should be
256 * relatively small, so we avoid the overhead. Sorting could also surprise
257 * users when result orderings change (even though they are not guaranteed).
259 for (i
= 0; i
< sk_IMPLEMENTATION_num(alg
->impls
); i
++) {
260 IMPLEMENTATION
*impl
= sk_IMPLEMENTATION_value(alg
->impls
, i
);
262 if (impl
->method
== method
) {
263 sk_IMPLEMENTATION_delete(alg
->impls
, i
);
264 ossl_property_unlock(store
);
269 ossl_property_unlock(store
);
273 int ossl_method_store_fetch(OSSL_METHOD_STORE
*store
, int nid
,
274 const char *prop_query
, void **method
)
277 IMPLEMENTATION
*impl
;
278 OSSL_PROPERTY_LIST
*pq
= NULL
, *p2
;
280 int j
, best
= -1, score
, optional
;
282 if (nid
<= 0 || method
== NULL
|| store
== NULL
)
286 * This only needs to be a read lock, because queries never create property
287 * names or value and thus don't modify any of the property string layer.
289 ossl_property_read_lock(store
);
290 alg
= ossl_method_store_retrieve(store
, nid
);
292 ossl_property_unlock(store
);
296 if (prop_query
== NULL
) {
297 if ((impl
= sk_IMPLEMENTATION_value(alg
->impls
, 0)) != NULL
) {
298 *method
= impl
->method
;
303 pq
= ossl_parse_query(store
->ctx
, prop_query
);
306 if (store
->global_properties
!= NULL
) {
307 p2
= ossl_property_merge(pq
, store
->global_properties
);
310 ossl_property_free(pq
);
313 optional
= ossl_property_has_optional(pq
);
314 for (j
= 0; j
< sk_IMPLEMENTATION_num(alg
->impls
); j
++) {
315 impl
= sk_IMPLEMENTATION_value(alg
->impls
, j
);
316 score
= ossl_property_match_count(pq
, impl
->properties
);
318 *method
= impl
->method
;
326 ossl_property_unlock(store
);
327 ossl_property_free(pq
);
331 int ossl_method_store_set_global_properties(OSSL_METHOD_STORE
*store
,
332 const char *prop_query
) {
338 ossl_property_write_lock(store
);
339 ossl_method_cache_flush_all(store
);
340 if (prop_query
== NULL
) {
341 ossl_property_free(store
->global_properties
);
342 store
->global_properties
= NULL
;
343 ossl_property_unlock(store
);
346 store
->global_properties
= ossl_parse_query(store
->ctx
, prop_query
);
347 ret
= store
->global_properties
!= NULL
;
348 ossl_property_unlock(store
);
352 static void impl_cache_flush_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
)
354 lh_QUERY_doall(alg
->cache
, &impl_cache_free
);
355 lh_QUERY_flush(alg
->cache
);
358 static void ossl_method_cache_flush(OSSL_METHOD_STORE
*store
, int nid
)
360 ALGORITHM
*alg
= ossl_method_store_retrieve(store
, nid
);
363 store
->nelem
-= lh_QUERY_num_items(alg
->cache
);
364 impl_cache_flush_alg(0, alg
);
368 static void ossl_method_cache_flush_all(OSSL_METHOD_STORE
*store
)
370 ossl_sa_ALGORITHM_doall(store
->algs
, &impl_cache_flush_alg
);
374 IMPLEMENT_LHASH_DOALL_ARG(QUERY
, IMPL_CACHE_FLUSH
);
377 * Flush an element from the query cache (perhaps).
379 * In order to avoid taking a write lock to keep accurate LRU information or
380 * using atomic operations to approximate similar, the procedure used here
381 * is to stochastically flush approximately half the cache. Since generating
382 * random numbers is relatively expensive, we produce them in blocks and
383 * consume them as we go, saving generated bits between generations of flushes.
385 * This procedure isn't ideal, LRU would be better. However, in normal
386 * operation, reaching a full cache would be quite unexpected. It means
387 * that no steady state of algorithm queries has been reached. I.e. it is most
388 * likely an attack of some form. A suboptimal clearance strategy that doesn't
389 * degrade performance of the normal case is preferable to a more refined
390 * approach that imposes a performance impact.
392 static void impl_cache_flush_cache(QUERY
*c
, IMPL_CACHE_FLUSH
*state
)
394 #if !defined(FIPS_MODE)
395 /* TODO(3.0): No RAND_bytes yet in FIPS module. Add this back when available */
396 OSSL_METHOD_STORE
*store
= state
->store
;
399 if (store
->nbits
== 0) {
400 if (!RAND_bytes(store
->rand_bits
, sizeof(store
->rand_bits
)))
402 store
->nbits
= sizeof(store
->rand_bits
) * 8;
405 if ((store
->rand_bits
[n
>> 3] & (1 << (n
& 7))) != 0)
406 OPENSSL_free(lh_QUERY_delete(state
->cache
, c
));
409 #endif /* !defined(FIPS_MODE) */
412 static void impl_cache_flush_one_alg(ossl_uintmax_t idx
, ALGORITHM
*alg
,
415 IMPL_CACHE_FLUSH
*state
= (IMPL_CACHE_FLUSH
*)v
;
417 state
->cache
= alg
->cache
;
418 lh_QUERY_doall_IMPL_CACHE_FLUSH(state
->cache
, &impl_cache_flush_cache
,
422 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE
*store
)
424 IMPL_CACHE_FLUSH state
;
428 ossl_sa_ALGORITHM_doall_arg(store
->algs
, &impl_cache_flush_one_alg
, &state
);
429 store
->need_flush
= 0;
430 store
->nelem
= state
.nelem
;
433 int ossl_method_store_cache_get(OSSL_METHOD_STORE
*store
, int nid
,
434 const char *prop_query
, void **method
)
439 if (nid
<= 0 || store
== NULL
)
442 ossl_property_read_lock(store
);
443 alg
= ossl_method_store_retrieve(store
, nid
);
445 ossl_property_unlock(store
);
449 elem
.query
= prop_query
!= NULL
? prop_query
: "";
450 r
= lh_QUERY_retrieve(alg
->cache
, &elem
);
452 ossl_property_unlock(store
);
456 ossl_property_unlock(store
);
460 int ossl_method_store_cache_set(OSSL_METHOD_STORE
*store
, int nid
,
461 const char *prop_query
, void *method
)
463 QUERY elem
, *old
, *p
= NULL
;
467 if (nid
<= 0 || store
== NULL
)
469 if (prop_query
== NULL
)
472 ossl_property_write_lock(store
);
473 if (store
->need_flush
)
474 ossl_method_cache_flush_some(store
);
475 alg
= ossl_method_store_retrieve(store
, nid
);
477 ossl_property_unlock(store
);
481 if (method
== NULL
) {
482 elem
.query
= prop_query
;
483 lh_QUERY_delete(alg
->cache
, &elem
);
484 ossl_property_unlock(store
);
487 p
= OPENSSL_malloc(sizeof(*p
) + (len
= strlen(prop_query
)));
491 memcpy((char *)p
->query
, prop_query
, len
+ 1);
492 if ((old
= lh_QUERY_insert(alg
->cache
, p
)) != NULL
)
494 if (old
!= NULL
|| !lh_QUERY_error(alg
->cache
)) {
496 if (store
->nelem
>= IMPL_CACHE_FLUSH_THRESHOLD
)
497 store
->need_flush
= 1;
498 ossl_property_unlock(store
);
502 ossl_property_unlock(store
);