]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/property/property.c
Coverity 1463571: Null pointer dereferences (FORWARD_NULL)
[thirdparty/openssl.git] / crypto / property / property.c
CommitLineData
1bdbdaff 1/*
454afd98 2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
1bdbdaff
P
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"
25f2138b 16#include "crypto/ctype.h"
1bdbdaff
P
17#include <openssl/lhash.h>
18#include <openssl/rand.h>
19#include "internal/thread_once.h"
25f2138b
DMSP
20#include "crypto/lhash.h"
21#include "crypto/sparse_array.h"
706457b7 22#include "property_local.h"
1bdbdaff 23
e5ecfcc7
P
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
bdbf2df2
P
30
31typedef struct {
32 void *method;
33 int (*up_ref)(void *);
34 void (*free)(void *);
35} METHOD;
1bdbdaff
P
36
37typedef struct {
c1d56231 38 const OSSL_PROVIDER *provider;
1bdbdaff 39 OSSL_PROPERTY_LIST *properties;
bdbf2df2 40 METHOD method;
1bdbdaff
P
41} IMPLEMENTATION;
42
43DEFINE_STACK_OF(IMPLEMENTATION)
44
45typedef struct {
46 const char *query;
bdbf2df2 47 METHOD method;
1bdbdaff
P
48 char body[1];
49} QUERY;
50
51DEFINE_LHASH_OF(QUERY);
52
53typedef struct {
54 int nid;
55 STACK_OF(IMPLEMENTATION) *impls;
56 LHASH_OF(QUERY) *cache;
57} ALGORITHM;
58
59struct ossl_method_store_st {
1aedc35f 60 OPENSSL_CTX *ctx;
1bdbdaff
P
61 size_t nelem;
62 SPARSE_ARRAY_OF(ALGORITHM) *algs;
63 OSSL_PROPERTY_LIST *global_properties;
64 int need_flush;
1bdbdaff
P
65 CRYPTO_RWLOCK *lock;
66};
67
68typedef struct {
1bdbdaff
P
69 LHASH_OF(QUERY) *cache;
70 size_t nelem;
f06cf3c4 71 uint32_t seed;
1bdbdaff
P
72} IMPL_CACHE_FLUSH;
73
74DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
75
76static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
77static void ossl_method_cache_flush_all(OSSL_METHOD_STORE *c);
78
bdbf2df2
P
79static int ossl_method_up_ref(METHOD *method)
80{
81 return (*method->up_ref)(method->method);
82}
83
84static void ossl_method_free(METHOD *method)
85{
86 (*method->free)(method->method);
87}
88
1bdbdaff
P
89int ossl_property_read_lock(OSSL_METHOD_STORE *p)
90{
91 return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
92}
93
94int ossl_property_write_lock(OSSL_METHOD_STORE *p)
95{
96 return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
97}
98
99int ossl_property_unlock(OSSL_METHOD_STORE *p)
100{
101 return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
102}
103
1bdbdaff
P
104static unsigned long query_hash(const QUERY *a)
105{
106 return OPENSSL_LH_strhash(a->query);
107}
108
109static int query_cmp(const QUERY *a, const QUERY *b)
110{
111 return strcmp(a->query, b->query);
112}
113
114static void impl_free(IMPLEMENTATION *impl)
115{
116 if (impl != NULL) {
bdbf2df2 117 ossl_method_free(&impl->method);
1bdbdaff
P
118 OPENSSL_free(impl);
119 }
120}
121
122static void impl_cache_free(QUERY *elem)
123{
bdbf2df2
P
124 if (elem != NULL) {
125 ossl_method_free(&elem->method);
126 OPENSSL_free(elem);
127 }
1bdbdaff
P
128}
129
8ab53b19 130static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a)
1bdbdaff
P
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
1aedc35f
MC
140/*
141 * The OPENSSL_CTX param here allows access to underlying property data needed
142 * for computation
143 */
144OSSL_METHOD_STORE *ossl_method_store_new(OPENSSL_CTX *ctx)
1bdbdaff 145{
909f2e59 146 OSSL_METHOD_STORE *res;
1bdbdaff 147
909f2e59 148 res = OPENSSL_zalloc(sizeof(*res));
1bdbdaff 149 if (res != NULL) {
1aedc35f 150 res->ctx = ctx;
1bdbdaff
P
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) {
bdbf2df2 156 ossl_sa_ALGORITHM_free(res->algs);
1bdbdaff
P
157 OPENSSL_free(res);
158 return NULL;
159 }
160 }
161 return res;
162}
163
164void 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
175static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
176{
177 return ossl_sa_ALGORITHM_get(store->algs, nid);
178}
179
180static 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
c1d56231 185int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
b1d40ddf
RL
186 int nid, const char *properties, void *method,
187 int (*method_up_ref)(void *),
188 void (*method_destruct)(void *))
1bdbdaff
P
189{
190 ALGORITHM *alg = NULL;
191 IMPLEMENTATION *impl;
192 int ret = 0;
c1d56231 193 int i;
1bdbdaff 194
0a79572a 195 if (nid <= 0 || method == NULL || store == NULL)
1bdbdaff
P
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;
bdbf2df2
P
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)) {
c1d56231 208 OPENSSL_free(impl);
b1d40ddf 209 return 0;
c1d56231
RL
210 }
211 impl->provider = prov;
1bdbdaff
P
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);
1aedc35f
MC
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)
1bdbdaff 224 goto err;
1aedc35f 225 ossl_prop_defn_set(store->ctx, properties, impl->properties);
1bdbdaff
P
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
c1d56231
RL
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))
1bdbdaff
P
249 ret = 1;
250 ossl_property_unlock(store);
251 if (ret == 0)
252 impl_free(impl);
253 return ret;
254
255err:
256 ossl_property_unlock(store);
257 alg_cleanup(0, alg);
258 impl_free(impl);
259 return 0;
260}
261
262int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
0a79572a 263 const void *method)
1bdbdaff
P
264{
265 ALGORITHM *alg = NULL;
266 int i;
267
0a79572a 268 if (nid <= 0 || method == NULL || store == NULL)
1bdbdaff
P
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
bdbf2df2
P
287 if (impl->method.method == method) {
288 impl_free(impl);
1bdbdaff
P
289 sk_IMPLEMENTATION_delete(alg->impls, i);
290 ossl_property_unlock(store);
1bdbdaff
P
291 return 1;
292 }
293 }
294 ossl_property_unlock(store);
295 return 0;
296}
297
298int ossl_method_store_fetch(OSSL_METHOD_STORE *store, int nid,
0a79572a 299 const char *prop_query, void **method)
1bdbdaff
P
300{
301 ALGORITHM *alg;
302 IMPLEMENTATION *impl;
303 OSSL_PROPERTY_LIST *pq = NULL, *p2;
bdbf2df2 304 METHOD *best_method = NULL;
1bdbdaff 305 int ret = 0;
da89ac0b 306 int j, best = -1, score, optional;
1bdbdaff 307
f844f9eb 308#ifndef FIPS_MODULE
29dc6e00
MC
309 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
310#endif
311
0a79572a 312 if (nid <= 0 || method == NULL || store == NULL)
1bdbdaff
P
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) {
bdbf2df2 328 best_method = &impl->method;
1bdbdaff
P
329 ret = 1;
330 }
331 goto fin;
332 }
1aedc35f 333 pq = ossl_parse_query(store->ctx, prop_query);
1bdbdaff
P
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 }
da89ac0b 343 optional = ossl_property_has_optional(pq);
1bdbdaff
P
344 for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
345 impl = sk_IMPLEMENTATION_value(alg->impls, j);
da89ac0b
P
346 score = ossl_property_match_count(pq, impl->properties);
347 if (score > best) {
bdbf2df2
P
348 best_method = &impl->method;
349 best = score;
1bdbdaff 350 ret = 1;
da89ac0b
P
351 if (!optional)
352 goto fin;
1bdbdaff
P
353 }
354 }
355fin:
bdbf2df2
P
356 if (ret && ossl_method_up_ref(best_method))
357 *method = best_method->method;
358 else
359 ret = 0;
1bdbdaff
P
360 ossl_property_unlock(store);
361 ossl_property_free(pq);
362 return ret;
363}
364
e0624f0d
SL
365int 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
1bdbdaff 380int ossl_method_store_set_global_properties(OSSL_METHOD_STORE *store,
e0624f0d
SL
381 const char *prop_query)
382{
1bdbdaff
P
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);
e0624f0d
SL
390
391 ossl_property_free(store->global_properties);
392 store->global_properties = NULL;
393
1bdbdaff 394 if (prop_query == NULL) {
1bdbdaff
P
395 ossl_property_unlock(store);
396 return 1;
397 }
1aedc35f 398 store->global_properties = ossl_parse_query(store->ctx, prop_query);
1bdbdaff
P
399 ret = store->global_properties != NULL;
400 ossl_property_unlock(store);
401 return ret;
402}
403
e0624f0d
SL
404int 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
8ab53b19 443static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)
1bdbdaff
P
444{
445 lh_QUERY_doall(alg->cache, &impl_cache_free);
446 lh_QUERY_flush(alg->cache);
447}
448
449static 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
459static 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
465IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
466
467/*
468 * Flush an element from the query cache (perhaps).
469 *
f06cf3c4
P
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.
1bdbdaff 474 *
f06cf3c4
P
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.
1bdbdaff
P
482 */
483static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
484{
f06cf3c4
P
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)
bdbf2df2 501 impl_cache_free(lh_QUERY_delete(state->cache, c));
1bdbdaff
P
502 else
503 state->nelem++;
504}
505
8ab53b19
P
506static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
507 void *v)
1bdbdaff
P
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
516static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
517{
518 IMPL_CACHE_FLUSH state;
519
520 state.nelem = 0;
f06cf3c4
P
521 if ((state.seed = OPENSSL_rdtsc()) == 0)
522 state.seed = 1;
1bdbdaff 523 store->need_flush = 0;
e2e5abe4 524 ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
1bdbdaff
P
525 store->nelem = state.nelem;
526}
527
528int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, int nid,
0a79572a 529 const char *prop_query, void **method)
1bdbdaff
P
530{
531 ALGORITHM *alg;
532 QUERY elem, *r;
bdbf2df2 533 int res = 0;
1bdbdaff
P
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);
bdbf2df2
P
540 if (alg == NULL)
541 goto err;
1bdbdaff 542
1393722a 543 elem.query = prop_query != NULL ? prop_query : "";
1bdbdaff 544 r = lh_QUERY_retrieve(alg->cache, &elem);
bdbf2df2
P
545 if (r == NULL)
546 goto err;
547 if (ossl_method_up_ref(&r->method)) {
548 *method = r->method.method;
549 res = 1;
1bdbdaff 550 }
bdbf2df2 551err:
1bdbdaff 552 ossl_property_unlock(store);
bdbf2df2 553 return res;
1bdbdaff
P
554}
555
556int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, int nid,
bdbf2df2
P
557 const char *prop_query, void *method,
558 int (*method_up_ref)(void *),
559 void (*method_destruct)(void *))
1bdbdaff
P
560{
561 QUERY elem, *old, *p = NULL;
562 ALGORITHM *alg;
563 size_t len;
bdbf2df2 564 int res = 1;
1bdbdaff
P
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);
bdbf2df2
P
575 if (alg == NULL)
576 goto err;
1bdbdaff 577
0a79572a 578 if (method == NULL) {
1bdbdaff 579 elem.query = prop_query;
bdbf2df2
P
580 if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) {
581 impl_cache_free(old);
f06cf3c4 582 store->nelem--;
bdbf2df2
P
583 }
584 goto end;
1bdbdaff
P
585 }
586 p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
587 if (p != NULL) {
588 p->query = p->body;
bdbf2df2
P
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;
1bdbdaff 594 memcpy((char *)p->query, prop_query, len + 1);
f06cf3c4 595 if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
bdbf2df2
P
596 impl_cache_free(old);
597 goto end;
f06cf3c4
P
598 }
599 if (!lh_QUERY_error(alg->cache)) {
600 if (++store->nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
1bdbdaff 601 store->need_flush = 1;
bdbf2df2 602 goto end;
1bdbdaff 603 }
bdbf2df2 604 ossl_method_free(&p->method);
1bdbdaff 605 }
bdbf2df2
P
606err:
607 res = 0;
defd3ed8 608 OPENSSL_free(p);
bdbf2df2
P
609end:
610 ossl_property_unlock(store);
611 return res;
1bdbdaff 612}