]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/property/property.c
Load the config file by default
[thirdparty/openssl.git] / crypto / property / property.c
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 *method;
30 void (*method_destruct)(void *);
31 } IMPLEMENTATION;
32
33 DEFINE_STACK_OF(IMPLEMENTATION)
34
35 typedef struct {
36 const char *query;
37 void *method;
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 OPENSSL_CTX *ctx;
51 size_t nelem;
52 SPARSE_ARRAY_OF(ALGORITHM) *algs;
53 OSSL_PROPERTY_LIST *global_properties;
54 int need_flush;
55 unsigned int nbits;
56 unsigned char rand_bits[(IMPL_CACHE_FLUSH_THRESHOLD + 7) / 8];
57 CRYPTO_RWLOCK *lock;
58 };
59
60 typedef struct {
61 LHASH_OF(QUERY) *cache;
62 size_t nelem;
63 uint32_t seed;
64 } IMPL_CACHE_FLUSH;
65
66 DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
67
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);
70
71 int ossl_property_read_lock(OSSL_METHOD_STORE *p)
72 {
73 return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
74 }
75
76 int ossl_property_write_lock(OSSL_METHOD_STORE *p)
77 {
78 return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
79 }
80
81 int ossl_property_unlock(OSSL_METHOD_STORE *p)
82 {
83 return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
84 }
85
86 static openssl_ctx_run_once_fn do_method_store_init;
87 int do_method_store_init(OPENSSL_CTX *ctx)
88 {
89 return ossl_property_parse_init(ctx);
90 }
91
92 static unsigned long query_hash(const QUERY *a)
93 {
94 return OPENSSL_LH_strhash(a->query);
95 }
96
97 static int query_cmp(const QUERY *a, const QUERY *b)
98 {
99 return strcmp(a->query, b->query);
100 }
101
102 static void impl_free(IMPLEMENTATION *impl)
103 {
104 if (impl != NULL) {
105 if (impl->method_destruct)
106 impl->method_destruct(impl->method);
107 OPENSSL_free(impl);
108 }
109 }
110
111 static void impl_cache_free(QUERY *elem)
112 {
113 OPENSSL_free(elem);
114 }
115
116 static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a)
117 {
118 if (a != NULL) {
119 sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
120 lh_QUERY_doall(a->cache, &impl_cache_free);
121 lh_QUERY_free(a->cache);
122 OPENSSL_free(a);
123 }
124 }
125
126 /*
127 * The OPENSSL_CTX param here allows access to underlying property data needed
128 * for computation
129 */
130 OSSL_METHOD_STORE *ossl_method_store_new(OPENSSL_CTX *ctx)
131 {
132 OSSL_METHOD_STORE *res;
133
134 if (!openssl_ctx_run_once(ctx,
135 OPENSSL_CTX_METHOD_STORE_RUN_ONCE_INDEX,
136 do_method_store_init))
137 return NULL;
138
139 res = OPENSSL_zalloc(sizeof(*res));
140 if (res != NULL) {
141 res->ctx = ctx;
142 if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL) {
143 OPENSSL_free(res);
144 return NULL;
145 }
146 if ((res->lock = CRYPTO_THREAD_lock_new()) == NULL) {
147 OPENSSL_free(res->algs);
148 OPENSSL_free(res);
149 return NULL;
150 }
151 }
152 return res;
153 }
154
155 void ossl_method_store_free(OSSL_METHOD_STORE *store)
156 {
157 if (store != NULL) {
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);
162 OPENSSL_free(store);
163 }
164 }
165
166 static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
167 {
168 return ossl_sa_ALGORITHM_get(store->algs, nid);
169 }
170
171 static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
172 {
173 return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
174 }
175
176 int ossl_method_store_add(OSSL_METHOD_STORE *store,
177 int nid, const char *properties,
178 void *method, void (*method_destruct)(void *))
179 {
180 ALGORITHM *alg = NULL;
181 IMPLEMENTATION *impl;
182 int ret = 0;
183
184 if (nid <= 0 || method == NULL || store == NULL)
185 return 0;
186 if (properties == NULL)
187 properties = "";
188
189 /* Create new entry */
190 impl = OPENSSL_malloc(sizeof(*impl));
191 if (impl == NULL)
192 return 0;
193 impl->method = method;
194 impl->method_destruct = method_destruct;
195
196 /*
197 * Insert into the hash table if required.
198 *
199 * A write lock is used unconditionally because we wend our way down to the
200 * property string code which isn't locking friendly.
201 */
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)
207 goto err;
208 ossl_prop_defn_set(store->ctx, properties, impl->properties);
209 }
210
211 alg = ossl_method_store_retrieve(store, nid);
212 if (alg == NULL) {
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)
216 goto err;
217 alg->nid = nid;
218 if (!ossl_method_store_insert(store, alg))
219 goto err;
220 }
221
222 /* Push onto stack */
223 if (sk_IMPLEMENTATION_push(alg->impls, impl))
224 ret = 1;
225 ossl_property_unlock(store);
226 if (ret == 0)
227 impl_free(impl);
228 return ret;
229
230 err:
231 ossl_property_unlock(store);
232 alg_cleanup(0, alg);
233 impl_free(impl);
234 return 0;
235 }
236
237 int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
238 const void *method)
239 {
240 ALGORITHM *alg = NULL;
241 int i;
242
243 if (nid <= 0 || method == NULL || store == NULL)
244 return 0;
245
246 ossl_property_write_lock(store);
247 ossl_method_cache_flush(store, nid);
248 alg = ossl_method_store_retrieve(store, nid);
249 if (alg == NULL) {
250 ossl_property_unlock(store);
251 return 0;
252 }
253
254 /*
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).
258 */
259 for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
260 IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
261
262 if (impl->method == method) {
263 sk_IMPLEMENTATION_delete(alg->impls, i);
264 ossl_property_unlock(store);
265 impl_free(impl);
266 return 1;
267 }
268 }
269 ossl_property_unlock(store);
270 return 0;
271 }
272
273 int ossl_method_store_fetch(OSSL_METHOD_STORE *store, int nid,
274 const char *prop_query, void **method)
275 {
276 ALGORITHM *alg;
277 IMPLEMENTATION *impl;
278 OSSL_PROPERTY_LIST *pq = NULL, *p2;
279 int ret = 0;
280 int j, best = -1, score, optional;
281
282 #ifndef FIPS_MODE
283 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
284 #endif
285
286 if (nid <= 0 || method == NULL || store == NULL)
287 return 0;
288
289 /*
290 * This only needs to be a read lock, because queries never create property
291 * names or value and thus don't modify any of the property string layer.
292 */
293 ossl_property_read_lock(store);
294 alg = ossl_method_store_retrieve(store, nid);
295 if (alg == NULL) {
296 ossl_property_unlock(store);
297 return 0;
298 }
299
300 if (prop_query == NULL) {
301 if ((impl = sk_IMPLEMENTATION_value(alg->impls, 0)) != NULL) {
302 *method = impl->method;
303 ret = 1;
304 }
305 goto fin;
306 }
307 pq = ossl_parse_query(store->ctx, prop_query);
308 if (pq == NULL)
309 goto fin;
310 if (store->global_properties != NULL) {
311 p2 = ossl_property_merge(pq, store->global_properties);
312 if (p2 == NULL)
313 goto fin;
314 ossl_property_free(pq);
315 pq = p2;
316 }
317 optional = ossl_property_has_optional(pq);
318 for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
319 impl = sk_IMPLEMENTATION_value(alg->impls, j);
320 score = ossl_property_match_count(pq, impl->properties);
321 if (score > best) {
322 *method = impl->method;
323 ret = 1;
324 if (!optional)
325 goto fin;
326 best = score;
327 }
328 }
329 fin:
330 ossl_property_unlock(store);
331 ossl_property_free(pq);
332 return ret;
333 }
334
335 int ossl_method_store_set_global_properties(OSSL_METHOD_STORE *store,
336 const char *prop_query) {
337 int ret = 0;
338
339 if (store == NULL)
340 return 1;
341
342 ossl_property_write_lock(store);
343 ossl_method_cache_flush_all(store);
344 if (prop_query == NULL) {
345 ossl_property_free(store->global_properties);
346 store->global_properties = NULL;
347 ossl_property_unlock(store);
348 return 1;
349 }
350 store->global_properties = ossl_parse_query(store->ctx, prop_query);
351 ret = store->global_properties != NULL;
352 ossl_property_unlock(store);
353 return ret;
354 }
355
356 static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)
357 {
358 lh_QUERY_doall(alg->cache, &impl_cache_free);
359 lh_QUERY_flush(alg->cache);
360 }
361
362 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
363 {
364 ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
365
366 if (alg != NULL) {
367 store->nelem -= lh_QUERY_num_items(alg->cache);
368 impl_cache_flush_alg(0, alg);
369 }
370 }
371
372 static void ossl_method_cache_flush_all(OSSL_METHOD_STORE *store)
373 {
374 ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
375 store->nelem = 0;
376 }
377
378 IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
379
380 /*
381 * Flush an element from the query cache (perhaps).
382 *
383 * In order to avoid taking a write lock or using atomic operations
384 * to keep accurate least recently used (LRU) or least frequently used
385 * (LFU) information, the procedure used here is to stochastically
386 * flush approximately half the cache.
387 *
388 * This procedure isn't ideal, LRU or LFU would be better. However,
389 * in normal operation, reaching a full cache would be unexpected.
390 * It means that no steady state of algorithm queries has been reached.
391 * That is, it is most likely an attack of some form. A suboptimal clearance
392 * strategy that doesn't degrade performance of the normal case is
393 * preferable to a more refined approach that imposes a performance
394 * impact.
395 */
396 static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
397 {
398 uint32_t n;
399
400 /*
401 * Implement the 32 bit xorshift as suggested by George Marsaglia in:
402 * https://doi.org/10.18637/jss.v008.i14
403 *
404 * This is a very fast PRNG so there is no need to extract bits one at a
405 * time and use the entire value each time.
406 */
407 n = state->seed;
408 n ^= n << 13;
409 n ^= n >> 17;
410 n ^= n << 5;
411 state->seed = n;
412
413 if ((n & 1) != 0)
414 OPENSSL_free(lh_QUERY_delete(state->cache, c));
415 else
416 state->nelem++;
417 }
418
419 static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
420 void *v)
421 {
422 IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
423
424 state->cache = alg->cache;
425 lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
426 state);
427 }
428
429 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
430 {
431 IMPL_CACHE_FLUSH state;
432
433 state.nelem = 0;
434 if ((state.seed = OPENSSL_rdtsc()) == 0)
435 state.seed = 1;
436 store->need_flush = 0;
437 ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
438 store->nelem = state.nelem;
439 }
440
441 int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, int nid,
442 const char *prop_query, void **method)
443 {
444 ALGORITHM *alg;
445 QUERY elem, *r;
446
447 if (nid <= 0 || store == NULL)
448 return 0;
449
450 ossl_property_read_lock(store);
451 alg = ossl_method_store_retrieve(store, nid);
452 if (alg == NULL) {
453 ossl_property_unlock(store);
454 return 0;
455 }
456
457 elem.query = prop_query != NULL ? prop_query : "";
458 r = lh_QUERY_retrieve(alg->cache, &elem);
459 if (r == NULL) {
460 ossl_property_unlock(store);
461 return 0;
462 }
463 *method = r->method;
464 ossl_property_unlock(store);
465 return 1;
466 }
467
468 int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, int nid,
469 const char *prop_query, void *method)
470 {
471 QUERY elem, *old, *p = NULL;
472 ALGORITHM *alg;
473 size_t len;
474
475 if (nid <= 0 || store == NULL)
476 return 0;
477 if (prop_query == NULL)
478 return 1;
479
480 ossl_property_write_lock(store);
481 if (store->need_flush)
482 ossl_method_cache_flush_some(store);
483 alg = ossl_method_store_retrieve(store, nid);
484 if (alg == NULL) {
485 ossl_property_unlock(store);
486 return 0;
487 }
488
489 if (method == NULL) {
490 elem.query = prop_query;
491 if (lh_QUERY_delete(alg->cache, &elem) != NULL)
492 store->nelem--;
493 ossl_property_unlock(store);
494 return 1;
495 }
496 p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
497 if (p != NULL) {
498 p->query = p->body;
499 p->method = method;
500 memcpy((char *)p->query, prop_query, len + 1);
501 if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
502 OPENSSL_free(old);
503 ossl_property_unlock(store);
504 return 1;
505 }
506 if (!lh_QUERY_error(alg->cache)) {
507 if (++store->nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
508 store->need_flush = 1;
509 ossl_property_unlock(store);
510 return 1;
511 }
512 }
513 ossl_property_unlock(store);
514 OPENSSL_free(p);
515 return 0;
516 }