Property: naming and manual clarifiations
[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     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->method_destruct)
124             impl->method_destruct(impl->method);
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 *method, void (*method_destruct)(void *))
189 {
190     ALGORITHM *alg = NULL;
191     IMPLEMENTATION *impl;
192     int ret = 0;
193
194     if (nid <= 0 || method == NULL || store == NULL)
195         return 0;
196     if (properties == NULL)
197         properties = "";
198
199     /* Create new entry */
200     impl = OPENSSL_malloc(sizeof(*impl));
201     if (impl == NULL)
202         return 0;
203     impl->method = method;
204     impl->method_destruct = method_destruct;
205
206     /*
207      * Insert into the hash table if required.
208      *
209      * A write lock is used unconditionally because we wend our way down to the
210      * property string code which isn't locking friendly.
211      */
212     ossl_property_write_lock(store);
213     ossl_method_cache_flush(store, nid);
214     if ((impl->properties = ossl_prop_defn_get(properties)) == NULL) {
215         if ((impl->properties = ossl_parse_property(properties)) == NULL)
216             goto err;
217         ossl_prop_defn_set(properties, impl->properties);
218     }
219
220     alg = ossl_method_store_retrieve(store, nid);
221     if (alg == NULL) {
222         if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
223                 || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
224                 || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
225             goto err;
226         alg->nid = nid;
227         if (!ossl_method_store_insert(store, alg))
228             goto err;
229     }
230
231     /* Push onto stack */
232     if (sk_IMPLEMENTATION_push(alg->impls, impl))
233         ret = 1;
234     ossl_property_unlock(store);
235     if (ret == 0)
236         impl_free(impl);
237     return ret;
238
239 err:
240     ossl_property_unlock(store);
241     alg_cleanup(0, alg);
242     impl_free(impl);
243     return 0;
244 }
245
246 int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
247                              const void *method)
248 {
249     ALGORITHM *alg = NULL;
250     int i;
251
252     if (nid <= 0 || method == NULL || store == NULL)
253         return 0;
254
255     ossl_property_write_lock(store);
256     ossl_method_cache_flush(store, nid);
257     alg = ossl_method_store_retrieve(store, nid);
258     if (alg == NULL) {
259         ossl_property_unlock(store);
260         return 0;
261     }
262
263     /*
264      * A sorting find then a delete could be faster but these stacks should be
265      * relatively small, so we avoid the overhead.  Sorting could also surprise
266      * users when result orderings change (even though they are not guaranteed).
267      */
268     for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
269         IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
270
271         if (impl->method == method) {
272             sk_IMPLEMENTATION_delete(alg->impls, i);
273             ossl_property_unlock(store);
274             impl_free(impl);
275             return 1;
276         }
277     }
278     ossl_property_unlock(store);
279     return 0;
280 }
281
282 int ossl_method_store_fetch(OSSL_METHOD_STORE *store, int nid,
283                             const char *prop_query, void **method)
284 {
285     ALGORITHM *alg;
286     IMPLEMENTATION *impl;
287     OSSL_PROPERTY_LIST *pq = NULL, *p2;
288     int ret = 0;
289     int j;
290
291     if (nid <= 0 || method == NULL || store == NULL)
292         return 0;
293
294     /*
295      * This only needs to be a read lock, because queries never create property
296      * names or value and thus don't modify any of the property string layer.
297      */
298     ossl_property_read_lock(store);
299     alg = ossl_method_store_retrieve(store, nid);
300     if (alg == NULL) {
301         ossl_property_unlock(store);
302         return 0;
303     }
304
305     if (prop_query == NULL) {
306         if ((impl = sk_IMPLEMENTATION_value(alg->impls, 0)) != NULL) {
307             *method = impl->method;
308             ret = 1;
309         }
310         goto fin;
311     }
312     pq = ossl_parse_query(prop_query);
313     if (pq == NULL)
314         goto fin;
315     if (store->global_properties != NULL) {
316         p2 = ossl_property_merge(pq, store->global_properties);
317         if (p2 == NULL)
318             goto fin;
319         ossl_property_free(pq);
320         pq = p2;
321     }
322     for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
323         impl = sk_IMPLEMENTATION_value(alg->impls, j);
324
325         if (ossl_property_match(pq, impl->properties)) {
326             *method = impl->method;
327             ret = 1;
328             goto fin;
329         }
330     }
331 fin:
332     ossl_property_unlock(store);
333     ossl_property_free(pq);
334     return ret;
335 }
336
337 int ossl_method_store_set_global_properties(OSSL_METHOD_STORE *store,
338                                             const char *prop_query) {
339     int ret = 0;
340
341     if (store == NULL)
342         return 1;
343
344     ossl_property_write_lock(store);
345     ossl_method_cache_flush_all(store);
346     if (prop_query == NULL) {
347         ossl_property_free(store->global_properties);
348         store->global_properties = NULL;
349         ossl_property_unlock(store);
350         return 1;
351     }
352     store->global_properties = ossl_parse_query(prop_query);
353     ret = store->global_properties != NULL;
354     ossl_property_unlock(store);
355     return ret;
356 }
357
358 static void impl_cache_flush_alg(size_t idx, ALGORITHM *alg)
359 {
360     lh_QUERY_doall(alg->cache, &impl_cache_free);
361     lh_QUERY_flush(alg->cache);
362 }
363
364 static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
365 {
366     ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
367
368     if (alg != NULL) {
369         store->nelem -= lh_QUERY_num_items(alg->cache);
370         impl_cache_flush_alg(0, alg);
371     }
372 }
373
374 static void ossl_method_cache_flush_all(OSSL_METHOD_STORE *store)
375 {
376     ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
377     store->nelem = 0;
378 }
379
380 IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
381
382 /*
383  * Flush an element from the query cache (perhaps).
384  *
385  * In order to avoid taking a write lock to keep accurate LRU information or
386  * using atomic operations to approximate similar, the procedure used here
387  * is to stochastically flush approximately half the cache.  Since generating
388  * random numbers is relatively expensive, we produce them in blocks and
389  * consume them as we go, saving generated bits between generations of flushes.
390  *
391  * This procedure isn't ideal, LRU would be better.  However, in normal
392  * operation, reaching a full cache would be quite unexpected.  It means
393  * that no steady state of algorithm queries has been reached.  I.e. it is most
394  * likely an attack of some form.  A suboptimal clearance strategy that doesn't
395  * degrade performance of the normal case is preferable to a more refined
396  * approach that imposes a performance impact.
397  */
398 static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
399 {
400     OSSL_METHOD_STORE *store = state->store;
401     unsigned int n;
402
403     if (store->nbits == 0) {
404         if (!RAND_bytes(store->rand_bits, sizeof(store->rand_bits)))
405             return;
406         store->nbits = sizeof(store->rand_bits) * 8;
407     }
408     n = --store->nbits;
409     if ((store->rand_bits[n >> 3] & (1 << (n & 7))) != 0)
410         OPENSSL_free(lh_QUERY_delete(state->cache, c));
411     else
412         state->nelem++;
413 }
414
415 static void impl_cache_flush_one_alg(size_t idx, ALGORITHM *alg, void *v)
416 {
417     IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
418
419     state->cache = alg->cache;
420     lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
421                                     state);
422 }
423
424 static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
425 {
426     IMPL_CACHE_FLUSH state;
427
428     state.nelem = 0;
429     state.store = store;
430     ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
431     store->need_flush = 0;
432     store->nelem = state.nelem;
433 }
434
435 int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, int nid,
436                                 const char *prop_query, void **method)
437 {
438     ALGORITHM *alg;
439     QUERY elem, *r;
440
441     if (nid <= 0 || store == NULL)
442         return 0;
443
444     ossl_property_read_lock(store);
445     alg = ossl_method_store_retrieve(store, nid);
446     if (alg == NULL) {
447         ossl_property_unlock(store);
448         return 0;
449     }
450
451     elem.query = prop_query;
452     r = lh_QUERY_retrieve(alg->cache, &elem);
453     if (r == NULL) {
454         ossl_property_unlock(store);
455         return 0;
456     }
457     *method = r->method;
458     ossl_property_unlock(store);
459     return 1;
460 }
461
462 int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, int nid,
463                                 const char *prop_query, void *method)
464 {
465     QUERY elem, *old, *p = NULL;
466     ALGORITHM *alg;
467     size_t len;
468
469     if (nid <= 0 || store == NULL)
470         return 0;
471     if (prop_query == NULL)
472         return 1;
473
474     ossl_property_write_lock(store);
475     if (store->need_flush)
476         ossl_method_cache_flush_some(store);
477     alg = ossl_method_store_retrieve(store, nid);
478     if (alg == NULL) {
479         ossl_property_unlock(store);
480         return 0;
481     }
482
483     if (method == NULL) {
484         elem.query = prop_query;
485         lh_QUERY_delete(alg->cache, &elem);
486         ossl_property_unlock(store);
487         return 1;
488     }
489     p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
490     if (p != NULL) {
491         p->query = p->body;
492         p->method = method;
493         memcpy((char *)p->query, prop_query, len + 1);
494         if ((old = lh_QUERY_insert(alg->cache, p)) != NULL)
495             OPENSSL_free(old);
496         if (old != NULL || !lh_QUERY_error(alg->cache)) {
497             store->nelem++;
498             if (store->nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
499                 store->need_flush = 1;
500             ossl_property_unlock(store);
501             return 1;
502         }
503     }
504     ossl_property_unlock(store);
505     OPENSSL_free(p);
506     return 0;
507 }