]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/property/property.c
Instead of global data store it in an OPENSSL_CTX
[thirdparty/openssl.git] / crypto / property / property.c
CommitLineData
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
27typedef struct {
28 OSSL_PROPERTY_LIST *properties;
0a79572a
RL
29 void *method;
30 void (*method_destruct)(void *);
1bdbdaff
P
31} IMPLEMENTATION;
32
33DEFINE_STACK_OF(IMPLEMENTATION)
34
35typedef struct {
36 const char *query;
0a79572a 37 void *method;
1bdbdaff
P
38 char body[1];
39} QUERY;
40
41DEFINE_LHASH_OF(QUERY);
42
43typedef struct {
44 int nid;
45 STACK_OF(IMPLEMENTATION) *impls;
46 LHASH_OF(QUERY) *cache;
47} ALGORITHM;
48
49struct ossl_method_store_st {
1aedc35f 50 OPENSSL_CTX *ctx;
1bdbdaff
P
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
60typedef struct {
61 OSSL_METHOD_STORE *store;
62 LHASH_OF(QUERY) *cache;
63 size_t nelem;
64} IMPL_CACHE_FLUSH;
65
66DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
67
68static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
69static void ossl_method_cache_flush_all(OSSL_METHOD_STORE *c);
70
71int ossl_property_read_lock(OSSL_METHOD_STORE *p)
72{
73 return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
74}
75
76int ossl_property_write_lock(OSSL_METHOD_STORE *p)
77{
78 return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
79}
80
81int ossl_property_unlock(OSSL_METHOD_STORE *p)
82{
83 return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
84}
85
1aedc35f
MC
86static openssl_ctx_run_once_fn do_method_store_init;
87int do_method_store_init(OPENSSL_CTX *ctx)
1bdbdaff 88{
1aedc35f 89 return ossl_property_parse_init(ctx);
1bdbdaff
P
90}
91
92static unsigned long query_hash(const QUERY *a)
93{
94 return OPENSSL_LH_strhash(a->query);
95}
96
97static int query_cmp(const QUERY *a, const QUERY *b)
98{
99 return strcmp(a->query, b->query);
100}
101
102static void impl_free(IMPLEMENTATION *impl)
103{
104 if (impl != NULL) {
0a79572a
RL
105 if (impl->method_destruct)
106 impl->method_destruct(impl->method);
1bdbdaff
P
107 OPENSSL_free(impl);
108 }
109}
110
111static void impl_cache_free(QUERY *elem)
112{
113 OPENSSL_free(elem);
114}
115
8ab53b19 116static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a)
1bdbdaff
P
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
1aedc35f
MC
126/*
127 * The OPENSSL_CTX param here allows access to underlying property data needed
128 * for computation
129 */
130OSSL_METHOD_STORE *ossl_method_store_new(OPENSSL_CTX *ctx)
1bdbdaff 131{
909f2e59 132 OSSL_METHOD_STORE *res;
1bdbdaff 133
1aedc35f
MC
134 if (!openssl_ctx_run_once(ctx,
135 OPENSSL_CTX_METHOD_STORE_RUN_ONCE_INDEX,
136 do_method_store_init))
137 return NULL;
1bdbdaff 138
909f2e59 139 res = OPENSSL_zalloc(sizeof(*res));
1bdbdaff 140 if (res != NULL) {
1aedc35f 141 res->ctx = ctx;
1bdbdaff
P
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
155void 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
166static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
167{
168 return ossl_sa_ALGORITHM_get(store->algs, nid);
169}
170
171static 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
176int ossl_method_store_add(OSSL_METHOD_STORE *store,
177 int nid, const char *properties,
0a79572a 178 void *method, void (*method_destruct)(void *))
1bdbdaff
P
179{
180 ALGORITHM *alg = NULL;
181 IMPLEMENTATION *impl;
182 int ret = 0;
183
0a79572a 184 if (nid <= 0 || method == NULL || store == NULL)
1bdbdaff
P
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;
0a79572a
RL
193 impl->method = method;
194 impl->method_destruct = method_destruct;
1bdbdaff
P
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);
1aedc35f
MC
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)
1bdbdaff 207 goto err;
1aedc35f 208 ossl_prop_defn_set(store->ctx, properties, impl->properties);
1bdbdaff
P
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
230err:
231 ossl_property_unlock(store);
232 alg_cleanup(0, alg);
233 impl_free(impl);
234 return 0;
235}
236
237int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
0a79572a 238 const void *method)
1bdbdaff
P
239{
240 ALGORITHM *alg = NULL;
241 int i;
242
0a79572a 243 if (nid <= 0 || method == NULL || store == NULL)
1bdbdaff
P
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
0a79572a 262 if (impl->method == method) {
1bdbdaff
P
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
273int ossl_method_store_fetch(OSSL_METHOD_STORE *store, int nid,
0a79572a 274 const char *prop_query, void **method)
1bdbdaff
P
275{
276 ALGORITHM *alg;
277 IMPLEMENTATION *impl;
278 OSSL_PROPERTY_LIST *pq = NULL, *p2;
279 int ret = 0;
280 int j;
281
0a79572a 282 if (nid <= 0 || method == NULL || store == NULL)
1bdbdaff
P
283 return 0;
284
285 /*
286 * This only needs to be a read lock, because queries never create property
287 * names or value and thus don't modify any of the property string layer.
288 */
289 ossl_property_read_lock(store);
290 alg = ossl_method_store_retrieve(store, nid);
291 if (alg == NULL) {
292 ossl_property_unlock(store);
293 return 0;
294 }
295
296 if (prop_query == NULL) {
297 if ((impl = sk_IMPLEMENTATION_value(alg->impls, 0)) != NULL) {
0a79572a 298 *method = impl->method;
1bdbdaff
P
299 ret = 1;
300 }
301 goto fin;
302 }
1aedc35f 303 pq = ossl_parse_query(store->ctx, prop_query);
1bdbdaff
P
304 if (pq == NULL)
305 goto fin;
306 if (store->global_properties != NULL) {
307 p2 = ossl_property_merge(pq, store->global_properties);
308 if (p2 == NULL)
309 goto fin;
310 ossl_property_free(pq);
311 pq = p2;
312 }
313 for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
314 impl = sk_IMPLEMENTATION_value(alg->impls, j);
315
316 if (ossl_property_match(pq, impl->properties)) {
0a79572a 317 *method = impl->method;
1bdbdaff
P
318 ret = 1;
319 goto fin;
320 }
321 }
322fin:
323 ossl_property_unlock(store);
324 ossl_property_free(pq);
325 return ret;
326}
327
328int ossl_method_store_set_global_properties(OSSL_METHOD_STORE *store,
329 const char *prop_query) {
330 int ret = 0;
331
332 if (store == NULL)
333 return 1;
334
335 ossl_property_write_lock(store);
336 ossl_method_cache_flush_all(store);
337 if (prop_query == NULL) {
338 ossl_property_free(store->global_properties);
339 store->global_properties = NULL;
340 ossl_property_unlock(store);
341 return 1;
342 }
1aedc35f 343 store->global_properties = ossl_parse_query(store->ctx, prop_query);
1bdbdaff
P
344 ret = store->global_properties != NULL;
345 ossl_property_unlock(store);
346 return ret;
347}
348
8ab53b19 349static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)
1bdbdaff
P
350{
351 lh_QUERY_doall(alg->cache, &impl_cache_free);
352 lh_QUERY_flush(alg->cache);
353}
354
355static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
356{
357 ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
358
359 if (alg != NULL) {
360 store->nelem -= lh_QUERY_num_items(alg->cache);
361 impl_cache_flush_alg(0, alg);
362 }
363}
364
365static void ossl_method_cache_flush_all(OSSL_METHOD_STORE *store)
366{
367 ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
368 store->nelem = 0;
369}
370
371IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
372
373/*
374 * Flush an element from the query cache (perhaps).
375 *
376 * In order to avoid taking a write lock to keep accurate LRU information or
377 * using atomic operations to approximate similar, the procedure used here
378 * is to stochastically flush approximately half the cache. Since generating
379 * random numbers is relatively expensive, we produce them in blocks and
380 * consume them as we go, saving generated bits between generations of flushes.
381 *
382 * This procedure isn't ideal, LRU would be better. However, in normal
383 * operation, reaching a full cache would be quite unexpected. It means
384 * that no steady state of algorithm queries has been reached. I.e. it is most
385 * likely an attack of some form. A suboptimal clearance strategy that doesn't
386 * degrade performance of the normal case is preferable to a more refined
387 * approach that imposes a performance impact.
388 */
389static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
390{
391 OSSL_METHOD_STORE *store = state->store;
392 unsigned int n;
393
394 if (store->nbits == 0) {
395 if (!RAND_bytes(store->rand_bits, sizeof(store->rand_bits)))
396 return;
397 store->nbits = sizeof(store->rand_bits) * 8;
398 }
399 n = --store->nbits;
400 if ((store->rand_bits[n >> 3] & (1 << (n & 7))) != 0)
401 OPENSSL_free(lh_QUERY_delete(state->cache, c));
402 else
403 state->nelem++;
404}
405
8ab53b19
P
406static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
407 void *v)
1bdbdaff
P
408{
409 IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
410
411 state->cache = alg->cache;
412 lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
413 state);
414}
415
416static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
417{
418 IMPL_CACHE_FLUSH state;
419
420 state.nelem = 0;
421 state.store = store;
422 ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
423 store->need_flush = 0;
424 store->nelem = state.nelem;
425}
426
427int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, int nid,
0a79572a 428 const char *prop_query, void **method)
1bdbdaff
P
429{
430 ALGORITHM *alg;
431 QUERY elem, *r;
432
433 if (nid <= 0 || store == NULL)
434 return 0;
435
436 ossl_property_read_lock(store);
437 alg = ossl_method_store_retrieve(store, nid);
438 if (alg == NULL) {
439 ossl_property_unlock(store);
440 return 0;
441 }
442
1393722a 443 elem.query = prop_query != NULL ? prop_query : "";
1bdbdaff
P
444 r = lh_QUERY_retrieve(alg->cache, &elem);
445 if (r == NULL) {
446 ossl_property_unlock(store);
447 return 0;
448 }
0a79572a 449 *method = r->method;
1bdbdaff
P
450 ossl_property_unlock(store);
451 return 1;
452}
453
454int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, int nid,
0a79572a 455 const char *prop_query, void *method)
1bdbdaff
P
456{
457 QUERY elem, *old, *p = NULL;
458 ALGORITHM *alg;
459 size_t len;
460
461 if (nid <= 0 || store == NULL)
462 return 0;
463 if (prop_query == NULL)
464 return 1;
465
466 ossl_property_write_lock(store);
467 if (store->need_flush)
468 ossl_method_cache_flush_some(store);
469 alg = ossl_method_store_retrieve(store, nid);
470 if (alg == NULL) {
471 ossl_property_unlock(store);
472 return 0;
473 }
474
0a79572a 475 if (method == NULL) {
1bdbdaff
P
476 elem.query = prop_query;
477 lh_QUERY_delete(alg->cache, &elem);
478 ossl_property_unlock(store);
479 return 1;
480 }
481 p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
482 if (p != NULL) {
483 p->query = p->body;
0a79572a 484 p->method = method;
1bdbdaff
P
485 memcpy((char *)p->query, prop_query, len + 1);
486 if ((old = lh_QUERY_insert(alg->cache, p)) != NULL)
487 OPENSSL_free(old);
488 if (old != NULL || !lh_QUERY_error(alg->cache)) {
489 store->nelem++;
490 if (store->nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
491 store->need_flush = 1;
492 ossl_property_unlock(store);
493 return 1;
494 }
495 }
496 ossl_property_unlock(store);
497 OPENSSL_free(p);
498 return 0;
499}