]>
Commit | Line | Data |
---|---|---|
1bdbdaff P |
1 | /* |
2 | * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. | |
4 | * | |
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 | |
9 | */ | |
10 | ||
11 | #include <string.h> | |
12 | #include <stdio.h> | |
13 | #include <stdarg.h> | |
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" | |
23 | ||
24 | /* The number of elements in the query cache before we initiate a flush */ | |
25 | #define IMPL_CACHE_FLUSH_THRESHOLD 500 | |
26 | ||
27 | typedef struct { | |
28 | OSSL_PROPERTY_LIST *properties; | |
29 | void *implementation; | |
30 | void (*implementation_destruct)(void *); | |
31 | } IMPLEMENTATION; | |
32 | ||
33 | DEFINE_STACK_OF(IMPLEMENTATION) | |
34 | ||
35 | typedef struct { | |
36 | const char *query; | |
37 | void *result; | |
38 | char body[1]; | |
39 | } QUERY; | |
40 | ||
41 | DEFINE_LHASH_OF(QUERY); | |
42 | ||
43 | typedef struct { | |
44 | int nid; | |
45 | STACK_OF(IMPLEMENTATION) *impls; | |
46 | LHASH_OF(QUERY) *cache; | |
47 | } ALGORITHM; | |
48 | ||
49 | struct ossl_method_store_st { | |
50 | size_t nelem; | |
51 | SPARSE_ARRAY_OF(ALGORITHM) *algs; | |
52 | OSSL_PROPERTY_LIST *global_properties; | |
53 | int need_flush; | |
54 | unsigned int nbits; | |
55 | unsigned char rand_bits[(IMPL_CACHE_FLUSH_THRESHOLD + 7) / 8]; | |
56 | CRYPTO_RWLOCK *lock; | |
57 | }; | |
58 | ||
59 | typedef struct { | |
60 | OSSL_METHOD_STORE *store; | |
61 | LHASH_OF(QUERY) *cache; | |
62 | size_t nelem; | |
63 | } IMPL_CACHE_FLUSH; | |
64 | ||
65 | DEFINE_SPARSE_ARRAY_OF(ALGORITHM); | |
66 | ||
67 | static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid); | |
68 | static void ossl_method_cache_flush_all(OSSL_METHOD_STORE *c); | |
69 | ||
70 | int ossl_property_read_lock(OSSL_METHOD_STORE *p) | |
71 | { | |
72 | return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0; | |
73 | } | |
74 | ||
75 | int ossl_property_write_lock(OSSL_METHOD_STORE *p) | |
76 | { | |
77 | return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0; | |
78 | } | |
79 | ||
80 | int ossl_property_unlock(OSSL_METHOD_STORE *p) | |
81 | { | |
82 | return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0; | |
83 | } | |
84 | ||
85 | int ossl_method_store_init(void) | |
86 | { | |
87 | if (ossl_property_string_init() | |
88 | && ossl_prop_defn_init() | |
89 | && ossl_property_parse_init()) | |
90 | return 1; | |
91 | ||
92 | ossl_method_store_cleanup(); | |
93 | return 0; | |
94 | } | |
95 | ||
96 | void ossl_method_store_cleanup(void) | |
97 | { | |
98 | ossl_property_string_cleanup(); | |
99 | ossl_prop_defn_cleanup(); | |
100 | } | |
101 | ||
102 | static CRYPTO_ONCE method_store_init_flag = CRYPTO_ONCE_STATIC_INIT; | |
103 | DEFINE_RUN_ONCE_STATIC(do_method_store_init) | |
104 | { | |
105 | return OPENSSL_init_crypto(0, NULL) | |
106 | && ossl_method_store_init() | |
107 | && OPENSSL_atexit(&ossl_method_store_cleanup); | |
108 | } | |
109 | ||
110 | static unsigned long query_hash(const QUERY *a) | |
111 | { | |
112 | return OPENSSL_LH_strhash(a->query); | |
113 | } | |
114 | ||
115 | static int query_cmp(const QUERY *a, const QUERY *b) | |
116 | { | |
117 | return strcmp(a->query, b->query); | |
118 | } | |
119 | ||
120 | static void impl_free(IMPLEMENTATION *impl) | |
121 | { | |
122 | if (impl != NULL) { | |
123 | if (impl->implementation_destruct) | |
124 | impl->implementation_destruct(impl->implementation); | |
125 | OPENSSL_free(impl); | |
126 | } | |
127 | } | |
128 | ||
129 | static void impl_cache_free(QUERY *elem) | |
130 | { | |
131 | OPENSSL_free(elem); | |
132 | } | |
133 | ||
134 | static void alg_cleanup(size_t idx, ALGORITHM *a) | |
135 | { | |
136 | if (a != NULL) { | |
137 | sk_IMPLEMENTATION_pop_free(a->impls, &impl_free); | |
138 | lh_QUERY_doall(a->cache, &impl_cache_free); | |
139 | lh_QUERY_free(a->cache); | |
140 | OPENSSL_free(a); | |
141 | } | |
142 | } | |
143 | ||
144 | OSSL_METHOD_STORE *ossl_method_store_new(void) | |
145 | { | |
146 | OSSL_METHOD_STORE *res = OPENSSL_zalloc(sizeof(*res)); | |
147 | ||
148 | if (!RUN_ONCE(&method_store_init_flag, do_method_store_init)) | |
149 | return 0; | |
150 | ||
151 | if (res != NULL) { | |
152 | if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL) { | |
153 | OPENSSL_free(res); | |
154 | return NULL; | |
155 | } | |
156 | if ((res->lock = CRYPTO_THREAD_lock_new()) == NULL) { | |
157 | OPENSSL_free(res->algs); | |
158 | OPENSSL_free(res); | |
159 | return NULL; | |
160 | } | |
161 | } | |
162 | return res; | |
163 | } | |
164 | ||
165 | void ossl_method_store_free(OSSL_METHOD_STORE *store) | |
166 | { | |
167 | if (store != NULL) { | |
168 | ossl_sa_ALGORITHM_doall(store->algs, &alg_cleanup); | |
169 | ossl_sa_ALGORITHM_free(store->algs); | |
170 | ossl_property_free(store->global_properties); | |
171 | CRYPTO_THREAD_lock_free(store->lock); | |
172 | OPENSSL_free(store); | |
173 | } | |
174 | } | |
175 | ||
176 | static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid) | |
177 | { | |
178 | return ossl_sa_ALGORITHM_get(store->algs, nid); | |
179 | } | |
180 | ||
181 | static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg) | |
182 | { | |
183 | return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg); | |
184 | } | |
185 | ||
186 | int ossl_method_store_add(OSSL_METHOD_STORE *store, | |
187 | int nid, const char *properties, | |
188 | void *implementation, | |
189 | void (*implementation_destruct)(void *)) | |
190 | { | |
191 | ALGORITHM *alg = NULL; | |
192 | IMPLEMENTATION *impl; | |
193 | int ret = 0; | |
194 | ||
195 | if (nid <= 0 || implementation == NULL || store == NULL) | |
196 | return 0; | |
197 | if (properties == NULL) | |
198 | properties = ""; | |
199 | ||
200 | /* Create new entry */ | |
201 | impl = OPENSSL_malloc(sizeof(*impl)); | |
202 | if (impl == NULL) | |
203 | return 0; | |
204 | impl->implementation = implementation; | |
205 | impl->implementation_destruct = implementation_destruct; | |
206 | ||
207 | /* | |
208 | * Insert into the hash table if required. | |
209 | * | |
210 | * A write lock is used unconditionally because we wend our way down to the | |
211 | * property string code which isn't locking friendly. | |
212 | */ | |
213 | ossl_property_write_lock(store); | |
214 | ossl_method_cache_flush(store, nid); | |
215 | if ((impl->properties = ossl_prop_defn_get(properties)) == NULL) { | |
216 | if ((impl->properties = ossl_parse_property(properties)) == NULL) | |
217 | goto err; | |
218 | ossl_prop_defn_set(properties, impl->properties); | |
219 | } | |
220 | ||
221 | alg = ossl_method_store_retrieve(store, nid); | |
222 | if (alg == NULL) { | |
223 | if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL | |
224 | || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL | |
225 | || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL) | |
226 | goto err; | |
227 | alg->nid = nid; | |
228 | if (!ossl_method_store_insert(store, alg)) | |
229 | goto err; | |
230 | } | |
231 | ||
232 | /* Push onto stack */ | |
233 | if (sk_IMPLEMENTATION_push(alg->impls, impl)) | |
234 | ret = 1; | |
235 | ossl_property_unlock(store); | |
236 | if (ret == 0) | |
237 | impl_free(impl); | |
238 | return ret; | |
239 | ||
240 | err: | |
241 | ossl_property_unlock(store); | |
242 | alg_cleanup(0, alg); | |
243 | impl_free(impl); | |
244 | return 0; | |
245 | } | |
246 | ||
247 | int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid, | |
248 | const void *implementation) | |
249 | { | |
250 | ALGORITHM *alg = NULL; | |
251 | int i; | |
252 | ||
253 | if (nid <= 0 || implementation == NULL || store == NULL) | |
254 | return 0; | |
255 | ||
256 | ossl_property_write_lock(store); | |
257 | ossl_method_cache_flush(store, nid); | |
258 | alg = ossl_method_store_retrieve(store, nid); | |
259 | if (alg == NULL) { | |
260 | ossl_property_unlock(store); | |
261 | return 0; | |
262 | } | |
263 | ||
264 | /* | |
265 | * A sorting find then a delete could be faster but these stacks should be | |
266 | * relatively small, so we avoid the overhead. Sorting could also surprise | |
267 | * users when result orderings change (even though they are not guaranteed). | |
268 | */ | |
269 | for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) { | |
270 | IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i); | |
271 | ||
272 | if (impl->implementation == implementation) { | |
273 | sk_IMPLEMENTATION_delete(alg->impls, i); | |
274 | ossl_property_unlock(store); | |
275 | impl_free(impl); | |
276 | return 1; | |
277 | } | |
278 | } | |
279 | ossl_property_unlock(store); | |
280 | return 0; | |
281 | } | |
282 | ||
283 | int ossl_method_store_fetch(OSSL_METHOD_STORE *store, int nid, | |
284 | const char *prop_query, void **result) | |
285 | { | |
286 | ALGORITHM *alg; | |
287 | IMPLEMENTATION *impl; | |
288 | OSSL_PROPERTY_LIST *pq = NULL, *p2; | |
289 | int ret = 0; | |
290 | int j; | |
291 | ||
292 | if (nid <= 0 || result == NULL || store == NULL) | |
293 | return 0; | |
294 | ||
295 | /* | |
296 | * This only needs to be a read lock, because queries never create property | |
297 | * names or value and thus don't modify any of the property string layer. | |
298 | */ | |
299 | ossl_property_read_lock(store); | |
300 | alg = ossl_method_store_retrieve(store, nid); | |
301 | if (alg == NULL) { | |
302 | ossl_property_unlock(store); | |
303 | return 0; | |
304 | } | |
305 | ||
306 | if (prop_query == NULL) { | |
307 | if ((impl = sk_IMPLEMENTATION_value(alg->impls, 0)) != NULL) { | |
308 | *result = impl->implementation; | |
309 | ret = 1; | |
310 | } | |
311 | goto fin; | |
312 | } | |
313 | pq = ossl_parse_query(prop_query); | |
314 | if (pq == NULL) | |
315 | goto fin; | |
316 | if (store->global_properties != NULL) { | |
317 | p2 = ossl_property_merge(pq, store->global_properties); | |
318 | if (p2 == NULL) | |
319 | goto fin; | |
320 | ossl_property_free(pq); | |
321 | pq = p2; | |
322 | } | |
323 | for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) { | |
324 | impl = sk_IMPLEMENTATION_value(alg->impls, j); | |
325 | ||
326 | if (ossl_property_match(pq, impl->properties)) { | |
327 | *result = impl->implementation; | |
328 | ret = 1; | |
329 | goto fin; | |
330 | } | |
331 | } | |
332 | fin: | |
333 | ossl_property_unlock(store); | |
334 | ossl_property_free(pq); | |
335 | return ret; | |
336 | } | |
337 | ||
338 | int ossl_method_store_set_global_properties(OSSL_METHOD_STORE *store, | |
339 | const char *prop_query) { | |
340 | int ret = 0; | |
341 | ||
342 | if (store == NULL) | |
343 | return 1; | |
344 | ||
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); | |
351 | return 1; | |
352 | } | |
353 | store->global_properties = ossl_parse_query(prop_query); | |
354 | ret = store->global_properties != NULL; | |
355 | ossl_property_unlock(store); | |
356 | return ret; | |
357 | } | |
358 | ||
359 | static void impl_cache_flush_alg(size_t idx, ALGORITHM *alg) | |
360 | { | |
361 | lh_QUERY_doall(alg->cache, &impl_cache_free); | |
362 | lh_QUERY_flush(alg->cache); | |
363 | } | |
364 | ||
365 | static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid) | |
366 | { | |
367 | ALGORITHM *alg = ossl_method_store_retrieve(store, nid); | |
368 | ||
369 | if (alg != NULL) { | |
370 | store->nelem -= lh_QUERY_num_items(alg->cache); | |
371 | impl_cache_flush_alg(0, alg); | |
372 | } | |
373 | } | |
374 | ||
375 | static void ossl_method_cache_flush_all(OSSL_METHOD_STORE *store) | |
376 | { | |
377 | ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg); | |
378 | store->nelem = 0; | |
379 | } | |
380 | ||
381 | IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH); | |
382 | ||
383 | /* | |
384 | * Flush an element from the query cache (perhaps). | |
385 | * | |
386 | * In order to avoid taking a write lock to keep accurate LRU information or | |
387 | * using atomic operations to approximate similar, the procedure used here | |
388 | * is to stochastically flush approximately half the cache. Since generating | |
389 | * random numbers is relatively expensive, we produce them in blocks and | |
390 | * consume them as we go, saving generated bits between generations of flushes. | |
391 | * | |
392 | * This procedure isn't ideal, LRU would be better. However, in normal | |
393 | * operation, reaching a full cache would be quite unexpected. It means | |
394 | * that no steady state of algorithm queries has been reached. I.e. it is most | |
395 | * likely an attack of some form. A suboptimal clearance strategy that doesn't | |
396 | * degrade performance of the normal case is preferable to a more refined | |
397 | * approach that imposes a performance impact. | |
398 | */ | |
399 | static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state) | |
400 | { | |
401 | OSSL_METHOD_STORE *store = state->store; | |
402 | unsigned int n; | |
403 | ||
404 | if (store->nbits == 0) { | |
405 | if (!RAND_bytes(store->rand_bits, sizeof(store->rand_bits))) | |
406 | return; | |
407 | store->nbits = sizeof(store->rand_bits) * 8; | |
408 | } | |
409 | n = --store->nbits; | |
410 | if ((store->rand_bits[n >> 3] & (1 << (n & 7))) != 0) | |
411 | OPENSSL_free(lh_QUERY_delete(state->cache, c)); | |
412 | else | |
413 | state->nelem++; | |
414 | } | |
415 | ||
416 | static void impl_cache_flush_one_alg(size_t idx, ALGORITHM *alg, void *v) | |
417 | { | |
418 | IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v; | |
419 | ||
420 | state->cache = alg->cache; | |
421 | lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache, | |
422 | state); | |
423 | } | |
424 | ||
425 | static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store) | |
426 | { | |
427 | IMPL_CACHE_FLUSH state; | |
428 | ||
429 | state.nelem = 0; | |
430 | state.store = store; | |
431 | ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state); | |
432 | store->need_flush = 0; | |
433 | store->nelem = state.nelem; | |
434 | } | |
435 | ||
436 | int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, int nid, | |
437 | const char *prop_query, void **result) | |
438 | { | |
439 | ALGORITHM *alg; | |
440 | QUERY elem, *r; | |
441 | ||
442 | if (nid <= 0 || store == NULL) | |
443 | return 0; | |
444 | ||
445 | ossl_property_read_lock(store); | |
446 | alg = ossl_method_store_retrieve(store, nid); | |
447 | if (alg == NULL) { | |
448 | ossl_property_unlock(store); | |
449 | return 0; | |
450 | } | |
451 | ||
452 | elem.query = prop_query; | |
453 | r = lh_QUERY_retrieve(alg->cache, &elem); | |
454 | if (r == NULL) { | |
455 | ossl_property_unlock(store); | |
456 | return 0; | |
457 | } | |
458 | *result = r->result; | |
459 | ossl_property_unlock(store); | |
460 | return 1; | |
461 | } | |
462 | ||
463 | int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, int nid, | |
464 | const char *prop_query, void *result) | |
465 | { | |
466 | QUERY elem, *old, *p = NULL; | |
467 | ALGORITHM *alg; | |
468 | size_t len; | |
469 | ||
470 | if (nid <= 0 || store == NULL) | |
471 | return 0; | |
472 | if (prop_query == NULL) | |
473 | return 1; | |
474 | ||
475 | ossl_property_write_lock(store); | |
476 | if (store->need_flush) | |
477 | ossl_method_cache_flush_some(store); | |
478 | alg = ossl_method_store_retrieve(store, nid); | |
479 | if (alg == NULL) { | |
480 | ossl_property_unlock(store); | |
481 | return 0; | |
482 | } | |
483 | ||
484 | if (result == NULL) { | |
485 | elem.query = prop_query; | |
486 | lh_QUERY_delete(alg->cache, &elem); | |
487 | ossl_property_unlock(store); | |
488 | return 1; | |
489 | } | |
490 | p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query))); | |
491 | if (p != NULL) { | |
492 | p->query = p->body; | |
493 | p->result = result; | |
494 | memcpy((char *)p->query, prop_query, len + 1); | |
495 | if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) | |
496 | OPENSSL_free(old); | |
497 | if (old != NULL || !lh_QUERY_error(alg->cache)) { | |
498 | store->nelem++; | |
499 | if (store->nelem >= IMPL_CACHE_FLUSH_THRESHOLD) | |
500 | store->need_flush = 1; | |
501 | ossl_property_unlock(store); | |
502 | return 1; | |
503 | } | |
504 | } | |
505 | ossl_property_unlock(store); | |
506 | OPENSSL_free(p); | |
507 | return 0; | |
508 | } |