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