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