Rename OPENSSL_CTX prefix to OSSL_LIB_CTX
[openssl.git] / crypto / property / property.c
1 /*
2  * Copyright 2019-2020 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 "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"
23
24 /*
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.
28  */
29 #define IMPL_CACHE_FLUSH_THRESHOLD  500
30
31 typedef struct {
32     void *method;
33     int (*up_ref)(void *);
34     void (*free)(void *);
35 } METHOD;
36
37 typedef struct {
38     const OSSL_PROVIDER *provider;
39     OSSL_PROPERTY_LIST *properties;
40     METHOD method;
41 } IMPLEMENTATION;
42
43 DEFINE_STACK_OF(IMPLEMENTATION)
44
45 typedef struct {
46     const char *query;
47     METHOD method;
48     char body[1];
49 } QUERY;
50
51 DEFINE_LHASH_OF(QUERY);
52
53 typedef struct {
54     int nid;
55     STACK_OF(IMPLEMENTATION) *impls;
56     LHASH_OF(QUERY) *cache;
57 } ALGORITHM;
58
59 struct ossl_method_store_st {
60     OSSL_LIB_CTX *ctx;
61     size_t nelem;
62     SPARSE_ARRAY_OF(ALGORITHM) *algs;
63     int need_flush;
64     CRYPTO_RWLOCK *lock;
65 };
66
67 typedef struct {
68     LHASH_OF(QUERY) *cache;
69     size_t nelem;
70     uint32_t seed;
71 } IMPL_CACHE_FLUSH;
72
73 DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
74
75 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
76
77 /* Global properties are stored per library context */
78 static void ossl_ctx_global_properties_free(void *vstore)
79 {
80     OSSL_PROPERTY_LIST **plp = vstore;
81
82     if (plp != NULL) {
83         ossl_property_free(*plp);
84         OPENSSL_free(plp);
85     }
86 }
87
88 static void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)
89 {
90     return OPENSSL_zalloc(sizeof(OSSL_PROPERTY_LIST **));
91 }
92
93
94 static const OSSL_LIB_CTX_METHOD ossl_ctx_global_properties_method = {
95     ossl_ctx_global_properties_new,
96     ossl_ctx_global_properties_free,
97 };
98
99 OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
100                                                 int loadconfig)
101 {
102 #ifndef FIPS_MODULE
103     if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
104         return NULL;
105 #endif
106     return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
107                                  &ossl_ctx_global_properties_method);
108 }
109
110 static int ossl_method_up_ref(METHOD *method)
111 {
112     return (*method->up_ref)(method->method);
113 }
114
115 static void ossl_method_free(METHOD *method)
116 {
117     (*method->free)(method->method);
118 }
119
120 int ossl_property_read_lock(OSSL_METHOD_STORE *p)
121 {
122     return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
123 }
124
125 int ossl_property_write_lock(OSSL_METHOD_STORE *p)
126 {
127     return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
128 }
129
130 int ossl_property_unlock(OSSL_METHOD_STORE *p)
131 {
132     return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
133 }
134
135 static unsigned long query_hash(const QUERY *a)
136 {
137     return OPENSSL_LH_strhash(a->query);
138 }
139
140 static int query_cmp(const QUERY *a, const QUERY *b)
141 {
142     return strcmp(a->query, b->query);
143 }
144
145 static void impl_free(IMPLEMENTATION *impl)
146 {
147     if (impl != NULL) {
148         ossl_method_free(&impl->method);
149         OPENSSL_free(impl);
150     }
151 }
152
153 static void impl_cache_free(QUERY *elem)
154 {
155     if (elem != NULL) {
156         ossl_method_free(&elem->method);
157         OPENSSL_free(elem);
158     }
159 }
160
161 static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a)
162 {
163     if (a != NULL) {
164         sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
165         lh_QUERY_doall(a->cache, &impl_cache_free);
166         lh_QUERY_free(a->cache);
167         OPENSSL_free(a);
168     }
169 }
170
171 /*
172  * The OSSL_LIB_CTX param here allows access to underlying property data needed
173  * for computation
174  */
175 OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx)
176 {
177     OSSL_METHOD_STORE *res;
178
179     res = OPENSSL_zalloc(sizeof(*res));
180     if (res != NULL) {
181         res->ctx = ctx;
182         if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL) {
183             OPENSSL_free(res);
184             return NULL;
185         }
186         if ((res->lock = CRYPTO_THREAD_lock_new()) == NULL) {
187             ossl_sa_ALGORITHM_free(res->algs);
188             OPENSSL_free(res);
189             return NULL;
190         }
191     }
192     return res;
193 }
194
195 void ossl_method_store_free(OSSL_METHOD_STORE *store)
196 {
197     if (store != NULL) {
198         ossl_sa_ALGORITHM_doall(store->algs, &alg_cleanup);
199         ossl_sa_ALGORITHM_free(store->algs);
200         CRYPTO_THREAD_lock_free(store->lock);
201         OPENSSL_free(store);
202     }
203 }
204
205 static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
206 {
207     return ossl_sa_ALGORITHM_get(store->algs, nid);
208 }
209
210 static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
211 {
212         return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
213 }
214
215 int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
216                           int nid, const char *properties, void *method,
217                           int (*method_up_ref)(void *),
218                           void (*method_destruct)(void *))
219 {
220     ALGORITHM *alg = NULL;
221     IMPLEMENTATION *impl;
222     int ret = 0;
223     int i;
224
225     if (nid <= 0 || method == NULL || store == NULL)
226         return 0;
227     if (properties == NULL)
228         properties = "";
229
230     /* Create new entry */
231     impl = OPENSSL_malloc(sizeof(*impl));
232     if (impl == NULL)
233         return 0;
234     impl->method.method = method;
235     impl->method.up_ref = method_up_ref;
236     impl->method.free = method_destruct;
237     if (!ossl_method_up_ref(&impl->method)) {
238         OPENSSL_free(impl);
239         return 0;
240     }
241     impl->provider = prov;
242
243     /*
244      * Insert into the hash table if required.
245      *
246      * A write lock is used unconditionally because we wend our way down to the
247      * property string code which isn't locking friendly.
248      */
249     ossl_property_write_lock(store);
250     ossl_method_cache_flush(store, nid);
251     if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
252         impl->properties = ossl_parse_property(store->ctx, properties);
253         if (impl->properties == NULL)
254             goto err;
255         ossl_prop_defn_set(store->ctx, properties, impl->properties);
256     }
257
258     alg = ossl_method_store_retrieve(store, nid);
259     if (alg == NULL) {
260         if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
261                 || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
262                 || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
263             goto err;
264         alg->nid = nid;
265         if (!ossl_method_store_insert(store, alg))
266             goto err;
267     }
268
269     /* Push onto stack if there isn't one there already */
270     for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
271         const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);
272
273         if (tmpimpl->provider == impl->provider
274             && tmpimpl->properties == impl->properties)
275             break;
276     }
277     if (i == sk_IMPLEMENTATION_num(alg->impls)
278         && sk_IMPLEMENTATION_push(alg->impls, impl))
279         ret = 1;
280     ossl_property_unlock(store);
281     if (ret == 0)
282         impl_free(impl);
283     return ret;
284
285 err:
286     ossl_property_unlock(store);
287     alg_cleanup(0, alg);
288     impl_free(impl);
289     return 0;
290 }
291
292 int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
293                              const void *method)
294 {
295     ALGORITHM *alg = NULL;
296     int i;
297
298     if (nid <= 0 || method == NULL || store == NULL)
299         return 0;
300
301     ossl_property_write_lock(store);
302     ossl_method_cache_flush(store, nid);
303     alg = ossl_method_store_retrieve(store, nid);
304     if (alg == NULL) {
305         ossl_property_unlock(store);
306         return 0;
307     }
308
309     /*
310      * A sorting find then a delete could be faster but these stacks should be
311      * relatively small, so we avoid the overhead.  Sorting could also surprise
312      * users when result orderings change (even though they are not guaranteed).
313      */
314     for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
315         IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
316
317         if (impl->method.method == method) {
318             impl_free(impl);
319             (void)sk_IMPLEMENTATION_delete(alg->impls, i);
320             ossl_property_unlock(store);
321             return 1;
322         }
323     }
324     ossl_property_unlock(store);
325     return 0;
326 }
327
328 int ossl_method_store_fetch(OSSL_METHOD_STORE *store, int nid,
329                             const char *prop_query,
330                             void **method)
331 {
332     OSSL_PROPERTY_LIST **plp;
333     ALGORITHM *alg;
334     IMPLEMENTATION *impl;
335     OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;
336     METHOD *best_method = NULL;
337     int ret = 0;
338     int j, best = -1, score, optional;
339
340 #ifndef FIPS_MODULE
341     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
342 #endif
343
344     if (nid <= 0 || method == NULL || store == NULL)
345         return 0;
346
347     /*
348      * This only needs to be a read lock, because queries never create property
349      * names or value and thus don't modify any of the property string layer.
350      */
351     ossl_property_read_lock(store);
352     alg = ossl_method_store_retrieve(store, nid);
353     if (alg == NULL) {
354         ossl_property_unlock(store);
355         return 0;
356     }
357
358     if (prop_query != NULL)
359         p2 = pq = ossl_parse_query(store->ctx, prop_query);
360     plp = ossl_ctx_global_properties(store->ctx, 1);
361     if (plp != NULL && *plp != NULL) {
362         if (pq == NULL) {
363             pq = *plp;
364         } else {
365             p2 = ossl_property_merge(pq, *plp);
366             ossl_property_free(pq);
367             if (p2 == NULL)
368                 goto fin;
369             pq = p2;
370         }
371     }
372
373     if (pq == NULL) {
374         if ((impl = sk_IMPLEMENTATION_value(alg->impls, 0)) != NULL) {
375             best_method = &impl->method;
376             ret = 1;
377         }
378         goto fin;
379     }
380     optional = ossl_property_has_optional(pq);
381     for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
382         impl = sk_IMPLEMENTATION_value(alg->impls, j);
383         score = ossl_property_match_count(pq, impl->properties);
384         if (score > best) {
385             best_method = &impl->method;
386             best = score;
387             ret = 1;
388             if (!optional)
389                 goto fin;
390         }
391     }
392 fin:
393     if (ret && ossl_method_up_ref(best_method))
394         *method = best_method->method;
395     else
396         ret = 0;
397     ossl_property_unlock(store);
398     ossl_property_free(p2);
399     return ret;
400 }
401
402 static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
403 {
404     SPARSE_ARRAY_OF(ALGORITHM) *algs = arg;
405
406     lh_QUERY_doall(alg->cache, &impl_cache_free);
407     if (algs != NULL) {
408         sk_IMPLEMENTATION_pop_free(alg->impls, &impl_free);
409         lh_QUERY_free(alg->cache);
410         OPENSSL_free(alg);
411         ossl_sa_ALGORITHM_set(algs, idx, NULL);
412     } else {
413         lh_QUERY_flush(alg->cache);
414     }
415 }
416
417 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
418 {
419     ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
420
421     if (alg != NULL) {
422         store->nelem -= lh_QUERY_num_items(alg->cache);
423         impl_cache_flush_alg(0, alg, NULL);
424     }
425 }
426
427 void ossl_method_store_flush_cache(OSSL_METHOD_STORE *store, int all)
428 {
429     void *arg = (all != 0 ? store->algs : NULL);
430
431     ossl_property_write_lock(store);
432     ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_alg, arg);
433     store->nelem = 0;
434     ossl_property_unlock(store);
435 }
436
437 IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
438
439 /*
440  * Flush an element from the query cache (perhaps).
441  *
442  * In order to avoid taking a write lock or using atomic operations
443  * to keep accurate least recently used (LRU) or least frequently used
444  * (LFU) information, the procedure used here is to stochastically
445  * flush approximately half the cache.
446  *
447  * This procedure isn't ideal, LRU or LFU would be better.  However,
448  * in normal operation, reaching a full cache would be unexpected.
449  * It means that no steady state of algorithm queries has been reached.
450  * That is, it is most likely an attack of some form.  A suboptimal clearance
451  * strategy that doesn't degrade performance of the normal case is
452  * preferable to a more refined approach that imposes a performance
453  * impact.
454  */
455 static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
456 {
457     uint32_t n;
458
459     /*
460      * Implement the 32 bit xorshift as suggested by George Marsaglia in:
461      *      https://doi.org/10.18637/jss.v008.i14
462      *
463      * This is a very fast PRNG so there is no need to extract bits one at a
464      * time and use the entire value each time.
465      */
466     n = state->seed;
467     n ^= n << 13;
468     n ^= n >> 17;
469     n ^= n << 5;
470     state->seed = n;
471
472     if ((n & 1) != 0)
473         impl_cache_free(lh_QUERY_delete(state->cache, c));
474     else
475         state->nelem++;
476 }
477
478 static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
479                                      void *v)
480 {
481     IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
482
483     state->cache = alg->cache;
484     lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
485                                     state);
486 }
487
488 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
489 {
490     IMPL_CACHE_FLUSH state;
491
492     state.nelem = 0;
493     if ((state.seed = OPENSSL_rdtsc()) == 0)
494         state.seed = 1;
495     store->need_flush = 0;
496     ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
497     store->nelem = state.nelem;
498 }
499
500 int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, int nid,
501                                 const char *prop_query, void **method)
502 {
503     ALGORITHM *alg;
504     QUERY elem, *r;
505     int res = 0;
506
507     if (nid <= 0 || store == NULL)
508         return 0;
509
510     ossl_property_read_lock(store);
511     alg = ossl_method_store_retrieve(store, nid);
512     if (alg == NULL)
513         goto err;
514
515     elem.query = prop_query != NULL ? prop_query : "";
516     r = lh_QUERY_retrieve(alg->cache, &elem);
517     if (r == NULL)
518         goto err;
519     if (ossl_method_up_ref(&r->method)) {
520         *method = r->method.method;
521         res = 1;
522     }
523 err:
524     ossl_property_unlock(store);
525     return res;
526 }
527
528 int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, int nid,
529                                 const char *prop_query, void *method,
530                                 int (*method_up_ref)(void *),
531                                 void (*method_destruct)(void *))
532 {
533     QUERY elem, *old, *p = NULL;
534     ALGORITHM *alg;
535     size_t len;
536     int res = 1;
537
538     if (nid <= 0 || store == NULL)
539         return 0;
540     if (prop_query == NULL)
541         return 1;
542
543     ossl_property_write_lock(store);
544     if (store->need_flush)
545         ossl_method_cache_flush_some(store);
546     alg = ossl_method_store_retrieve(store, nid);
547     if (alg == NULL)
548         goto err;
549
550     if (method == NULL) {
551         elem.query = prop_query;
552         if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) {
553             impl_cache_free(old);
554             store->nelem--;
555         }
556         goto end;
557     }
558     p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
559     if (p != NULL) {
560         p->query = p->body;
561         p->method.method = method;
562         p->method.up_ref = method_up_ref;
563         p->method.free = method_destruct;
564         if (!ossl_method_up_ref(&p->method))
565             goto err;
566         memcpy((char *)p->query, prop_query, len + 1);
567         if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
568             impl_cache_free(old);
569             goto end;
570         }
571         if (!lh_QUERY_error(alg->cache)) {
572             if (++store->nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
573                 store->need_flush = 1;
574             goto end;
575         }
576         ossl_method_free(&p->method);
577     }
578 err:
579     res = 0;
580     OPENSSL_free(p);
581 end:
582     ossl_property_unlock(store);
583     return res;
584 }