Coverage Report

Created: 2023-09-25 06:41

/src/openssl30/crypto/property/property.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2023 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/core.h"
16
#include "internal/property.h"
17
#include "internal/provider.h"
18
#include "internal/tsan_assist.h"
19
#include "crypto/ctype.h"
20
#include <openssl/lhash.h>
21
#include <openssl/rand.h>
22
#include "internal/thread_once.h"
23
#include "crypto/lhash.h"
24
#include "crypto/sparse_array.h"
25
#include "property_local.h"
26
27
/*
28
 * The number of elements in the query cache before we initiate a flush.
29
 * If reducing this, also ensure the stochastic test in test/property_test.c
30
 * isn't likely to fail.
31
 */
32
5
#define IMPL_CACHE_FLUSH_THRESHOLD  500
33
34
typedef struct {
35
    void *method;
36
    int (*up_ref)(void *);
37
    void (*free)(void *);
38
} METHOD;
39
40
typedef struct {
41
    const OSSL_PROVIDER *provider;
42
    OSSL_PROPERTY_LIST *properties;
43
    METHOD method;
44
} IMPLEMENTATION;
45
46
DEFINE_STACK_OF(IMPLEMENTATION)
47
48
typedef struct {
49
    const OSSL_PROVIDER *provider;
50
    const char *query;
51
    METHOD method;
52
    char body[1];
53
} QUERY;
54
55
DEFINE_LHASH_OF(QUERY);
56
57
typedef struct {
58
    int nid;
59
    STACK_OF(IMPLEMENTATION) *impls;
60
    LHASH_OF(QUERY) *cache;
61
} ALGORITHM;
62
63
struct ossl_method_store_st {
64
    OSSL_LIB_CTX *ctx;
65
    SPARSE_ARRAY_OF(ALGORITHM) *algs;
66
    /*
67
     * Lock to protect the |algs| array from concurrent writing, when
68
     * individual implementations or queries are inserted.  This is used
69
     * by the appropriate functions here.
70
     */
71
    CRYPTO_RWLOCK *lock;
72
    /*
73
     * Lock to reserve the whole store.  This is used when fetching a set
74
     * of algorithms, via these functions, found in crypto/core_fetch.c:
75
     * ossl_method_construct_reserve_store()
76
     * ossl_method_construct_unreserve_store()
77
     */
78
    CRYPTO_RWLOCK *biglock;
79
80
    /* query cache specific values */
81
82
    /* Count of the query cache entries for all algs */
83
    size_t cache_nelem;
84
85
    /* Flag: 1 if query cache entries for all algs need flushing */
86
    int cache_need_flush;
87
};
88
89
typedef struct {
90
    LHASH_OF(QUERY) *cache;
91
    size_t nelem;
92
    uint32_t seed;
93
    unsigned char using_global_seed;
94
} IMPL_CACHE_FLUSH;
95
96
DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
97
98
typedef struct ossl_global_properties_st {
99
    OSSL_PROPERTY_LIST *list;
100
#ifndef FIPS_MODULE
101
    unsigned int no_mirrored : 1;
102
#endif
103
} OSSL_GLOBAL_PROPERTIES;
104
105
static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
106
                                        ALGORITHM *alg);
107
static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
108
109
/* Global properties are stored per library context */
110
static void ossl_ctx_global_properties_free(void *vglobp)
111
1
{
112
1
    OSSL_GLOBAL_PROPERTIES *globp = vglobp;
113
114
1
    if (globp != NULL) {
115
1
        ossl_property_free(globp->list);
116
1
        OPENSSL_free(globp);
117
1
    }
118
1
}
119
120
static void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)
121
1
{
122
1
    return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES));
123
1
}
124
125
static const OSSL_LIB_CTX_METHOD ossl_ctx_global_properties_method = {
126
    OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
127
    ossl_ctx_global_properties_new,
128
    ossl_ctx_global_properties_free,
129
};
130
131
OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
132
                                                ossl_unused int loadconfig)
133
5
{
134
5
    OSSL_GLOBAL_PROPERTIES *globp;
135
136
5
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
137
5
    if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
138
0
        return NULL;
139
5
#endif
140
5
    globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
141
5
                                  &ossl_ctx_global_properties_method);
142
143
5
    return globp != NULL ? &globp->list : NULL;
144
5
}
145
146
#ifndef FIPS_MODULE
147
int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx)
148
0
{
149
0
    OSSL_GLOBAL_PROPERTIES *globp
150
0
        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
151
0
                                &ossl_ctx_global_properties_method);
152
153
0
    return globp != NULL && globp->no_mirrored ? 1 : 0;
154
0
}
155
156
void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx)
157
0
{
158
0
    OSSL_GLOBAL_PROPERTIES *globp
159
0
        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
160
0
                                &ossl_ctx_global_properties_method);
161
162
0
    if (globp != NULL)
163
0
        globp->no_mirrored = 1;
164
0
}
165
#endif
166
167
static int ossl_method_up_ref(METHOD *method)
168
21.0k
{
169
21.0k
    return (*method->up_ref)(method->method);
170
21.0k
}
171
172
static void ossl_method_free(METHOD *method)
173
213
{
174
213
    (*method->free)(method->method);
175
213
}
176
177
static __owur int ossl_property_read_lock(OSSL_METHOD_STORE *p)
178
20.7k
{
179
20.7k
    return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
180
20.7k
}
181
182
static __owur int ossl_property_write_lock(OSSL_METHOD_STORE *p)
183
213
{
184
213
    return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
185
213
}
186
187
static int ossl_property_unlock(OSSL_METHOD_STORE *p)
188
21.0k
{
189
21.0k
    return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
190
21.0k
}
191
192
static unsigned long query_hash(const QUERY *a)
193
20.7k
{
194
20.7k
    return OPENSSL_LH_strhash(a->query);
195
20.7k
}
196
197
static int query_cmp(const QUERY *a, const QUERY *b)
198
20.7k
{
199
20.7k
    int res = strcmp(a->query, b->query);
200
201
20.7k
    if (res == 0 && a->provider != NULL && b->provider != NULL)
202
0
        res = b->provider > a->provider ? 1
203
0
            : b->provider < a->provider ? -1
204
0
            : 0;
205
20.7k
    return res;
206
20.7k
}
207
208
static void impl_free(IMPLEMENTATION *impl)
209
208
{
210
208
    if (impl != NULL) {
211
208
        ossl_method_free(&impl->method);
212
208
        OPENSSL_free(impl);
213
208
    }
214
208
}
215
216
static void impl_cache_free(QUERY *elem)
217
5
{
218
5
    if (elem != NULL) {
219
5
        ossl_method_free(&elem->method);
220
5
        OPENSSL_free(elem);
221
5
    }
222
5
}
223
224
static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)
225
27
{
226
27
    lh_QUERY_doall(alg->cache, &impl_cache_free);
227
27
    lh_QUERY_flush(alg->cache);
228
27
}
229
230
static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a, void *arg)
231
181
{
232
181
    OSSL_METHOD_STORE *store = arg;
233
234
181
    if (a != NULL) {
235
181
        sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
236
181
        lh_QUERY_doall(a->cache, &impl_cache_free);
237
181
        lh_QUERY_free(a->cache);
238
181
        OPENSSL_free(a);
239
181
    }
240
181
    if (store != NULL)
241
181
        ossl_sa_ALGORITHM_set(store->algs, idx, NULL);
242
181
}
243
244
/*
245
 * The OSSL_LIB_CTX param here allows access to underlying property data needed
246
 * for computation
247
 */
248
OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx)
249
2
{
250
2
    OSSL_METHOD_STORE *res;
251
252
2
    res = OPENSSL_zalloc(sizeof(*res));
253
2
    if (res != NULL) {
254
2
        res->ctx = ctx;
255
2
        if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL
256
2
            || (res->lock = CRYPTO_THREAD_lock_new()) == NULL
257
2
            || (res->biglock = CRYPTO_THREAD_lock_new()) == NULL) {
258
0
            ossl_method_store_free(res);
259
0
            return NULL;
260
0
        }
261
2
    }
262
2
    return res;
263
2
}
264
265
void ossl_method_store_free(OSSL_METHOD_STORE *store)
266
2
{
267
2
    if (store != NULL) {
268
2
        if (store->algs != NULL)
269
2
            ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup, store);
270
2
        ossl_sa_ALGORITHM_free(store->algs);
271
2
        CRYPTO_THREAD_lock_free(store->lock);
272
2
        CRYPTO_THREAD_lock_free(store->biglock);
273
2
        OPENSSL_free(store);
274
2
    }
275
2
}
276
277
int ossl_method_lock_store(OSSL_METHOD_STORE *store)
278
168k
{
279
168k
    return store != NULL ? CRYPTO_THREAD_write_lock(store->biglock) : 0;
280
168k
}
281
282
int ossl_method_unlock_store(OSSL_METHOD_STORE *store)
283
168k
{
284
168k
    return store != NULL ? CRYPTO_THREAD_unlock(store->biglock) : 0;
285
168k
}
286
287
static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
288
21.2k
{
289
21.2k
    return ossl_sa_ALGORITHM_get(store->algs, nid);
290
21.2k
}
291
292
static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
293
181
{
294
181
    return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
295
181
}
296
297
int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
298
                          int nid, const char *properties, void *method,
299
                          int (*method_up_ref)(void *),
300
                          void (*method_destruct)(void *))
301
208
{
302
208
    ALGORITHM *alg = NULL;
303
208
    IMPLEMENTATION *impl;
304
208
    int ret = 0;
305
208
    int i;
306
307
208
    if (nid <= 0 || method == NULL || store == NULL)
308
0
        return 0;
309
208
    if (properties == NULL)
310
0
        properties = "";
311
312
208
    if (!ossl_assert(prov != NULL))
313
0
        return 0;
314
315
    /* Create new entry */
316
208
    impl = OPENSSL_malloc(sizeof(*impl));
317
208
    if (impl == NULL)
318
0
        return 0;
319
208
    impl->method.method = method;
320
208
    impl->method.up_ref = method_up_ref;
321
208
    impl->method.free = method_destruct;
322
208
    if (!ossl_method_up_ref(&impl->method)) {
323
0
        OPENSSL_free(impl);
324
0
        return 0;
325
0
    }
326
208
    impl->provider = prov;
327
328
    /* Insert into the hash table if required */
329
208
    if (!ossl_property_write_lock(store)) {
330
0
        OPENSSL_free(impl);
331
0
        return 0;
332
0
    }
333
208
    ossl_method_cache_flush(store, nid);
334
208
    if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
335
15
        impl->properties = ossl_parse_property(store->ctx, properties);
336
15
        if (impl->properties == NULL)
337
0
            goto err;
338
15
        if (!ossl_prop_defn_set(store->ctx, properties, &impl->properties)) {
339
0
            ossl_property_free(impl->properties);
340
0
            impl->properties = NULL;
341
0
            goto err;
342
0
        }
343
15
    }
344
345
208
    alg = ossl_method_store_retrieve(store, nid);
346
208
    if (alg == NULL) {
347
181
        if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
348
181
                || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
349
181
                || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
350
0
            goto err;
351
181
        alg->nid = nid;
352
181
        if (!ossl_method_store_insert(store, alg))
353
0
            goto err;
354
181
    }
355
356
    /* Push onto stack if there isn't one there already */
357
265
    for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
358
57
        const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);
359
360
57
        if (tmpimpl->provider == impl->provider
361
57
            && tmpimpl->properties == impl->properties)
362
0
            break;
363
57
    }
364
208
    if (i == sk_IMPLEMENTATION_num(alg->impls)
365
208
        && sk_IMPLEMENTATION_push(alg->impls, impl))
366
208
        ret = 1;
367
208
    ossl_property_unlock(store);
368
208
    if (ret == 0)
369
0
        impl_free(impl);
370
208
    return ret;
371
372
0
err:
373
0
    ossl_property_unlock(store);
374
0
    alg_cleanup(0, alg, NULL);
375
0
    impl_free(impl);
376
0
    return 0;
377
208
}
378
379
int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
380
                             const void *method)
381
0
{
382
0
    ALGORITHM *alg = NULL;
383
0
    int i;
384
385
0
    if (nid <= 0 || method == NULL || store == NULL)
386
0
        return 0;
387
388
0
    if (!ossl_property_write_lock(store))
389
0
        return 0;
390
0
    ossl_method_cache_flush(store, nid);
391
0
    alg = ossl_method_store_retrieve(store, nid);
392
0
    if (alg == NULL) {
393
0
        ossl_property_unlock(store);
394
0
        return 0;
395
0
    }
396
397
    /*
398
     * A sorting find then a delete could be faster but these stacks should be
399
     * relatively small, so we avoid the overhead.  Sorting could also surprise
400
     * users when result orderings change (even though they are not guaranteed).
401
     */
402
0
    for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
403
0
        IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
404
405
0
        if (impl->method.method == method) {
406
0
            impl_free(impl);
407
0
            (void)sk_IMPLEMENTATION_delete(alg->impls, i);
408
0
            ossl_property_unlock(store);
409
0
            return 1;
410
0
        }
411
0
    }
412
0
    ossl_property_unlock(store);
413
0
    return 0;
414
0
}
415
416
struct alg_cleanup_by_provider_data_st {
417
    OSSL_METHOD_STORE *store;
418
    const OSSL_PROVIDER *prov;
419
};
420
421
static void
422
alg_cleanup_by_provider(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
423
0
{
424
0
    struct alg_cleanup_by_provider_data_st *data = arg;
425
0
    int i, count;
426
427
    /*
428
     * We walk the stack backwards, to avoid having to deal with stack shifts
429
     * caused by deletion
430
     */
431
0
    for (count = 0, i = sk_IMPLEMENTATION_num(alg->impls); i-- > 0;) {
432
0
        IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
433
434
0
        if (impl->provider == data->prov) {
435
0
            impl_free(impl);
436
0
            (void)sk_IMPLEMENTATION_delete(alg->impls, i);
437
0
            count++;
438
0
        }
439
0
    }
440
441
    /*
442
     * If we removed any implementation, we also clear the whole associated
443
     * cache, 'cause that's the sensible thing to do.
444
     * There's no point flushing the cache entries where we didn't remove
445
     * any implementation, though.
446
     */
447
0
    if (count > 0)
448
0
        ossl_method_cache_flush_alg(data->store, alg);
449
0
}
450
451
int ossl_method_store_remove_all_provided(OSSL_METHOD_STORE *store,
452
                                          const OSSL_PROVIDER *prov)
453
0
{
454
0
    struct alg_cleanup_by_provider_data_st data;
455
456
0
    if (!ossl_property_write_lock(store))
457
0
        return 0;
458
0
    data.prov = prov;
459
0
    data.store = store;
460
0
    ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup_by_provider, &data);
461
0
    ossl_property_unlock(store);
462
0
    return 1;
463
0
}
464
465
static void alg_do_one(ALGORITHM *alg, IMPLEMENTATION *impl,
466
                       void (*fn)(int id, void *method, void *fnarg),
467
                       void *fnarg)
468
13.9M
{
469
13.9M
    fn(alg->nid, impl->method.method, fnarg);
470
13.9M
}
471
472
struct alg_do_each_data_st {
473
    void (*fn)(int id, void *method, void *fnarg);
474
    void *fnarg;
475
};
476
477
static void alg_do_each(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
478
10.9M
{
479
10.9M
    struct alg_do_each_data_st *data = arg;
480
10.9M
    int i, end = sk_IMPLEMENTATION_num(alg->impls);
481
482
24.9M
    for (i = 0; i < end; i++) {
483
13.9M
        IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
484
485
13.9M
        alg_do_one(alg, impl, data->fn, data->fnarg);
486
13.9M
    }
487
10.9M
}
488
489
void ossl_method_store_do_all(OSSL_METHOD_STORE *store,
490
                              void (*fn)(int id, void *method, void *fnarg),
491
                              void *fnarg)
492
168k
{
493
168k
    struct alg_do_each_data_st data;
494
495
168k
    data.fn = fn;
496
168k
    data.fnarg = fnarg;
497
168k
    if (store != NULL)
498
168k
        ossl_sa_ALGORITHM_doall_arg(store->algs, alg_do_each, &data);
499
168k
}
500
501
int ossl_method_store_fetch(OSSL_METHOD_STORE *store,
502
                            int nid, const char *prop_query,
503
                            const OSSL_PROVIDER **prov_rw, void **method)
504
5
{
505
5
    OSSL_PROPERTY_LIST **plp;
506
5
    ALGORITHM *alg;
507
5
    IMPLEMENTATION *impl, *best_impl = NULL;
508
5
    OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;
509
5
    const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL;
510
5
    int ret = 0;
511
5
    int j, best = -1, score, optional;
512
513
5
    if (nid <= 0 || method == NULL || store == NULL)
514
0
        return 0;
515
516
5
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
517
5
    if (ossl_lib_ctx_is_default(store->ctx)
518
5
            && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
519
0
        return 0;
520
5
#endif
521
522
    /* This only needs to be a read lock, because the query won't create anything */
523
5
    if (!ossl_property_read_lock(store))
524
0
        return 0;
525
5
    alg = ossl_method_store_retrieve(store, nid);
526
5
    if (alg == NULL) {
527
0
        ossl_property_unlock(store);
528
0
        return 0;
529
0
    }
530
531
5
    if (prop_query != NULL)
532
5
        p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);
533
5
    plp = ossl_ctx_global_properties(store->ctx, 0);
534
5
    if (plp != NULL && *plp != NULL) {
535
0
        if (pq == NULL) {
536
0
            pq = *plp;
537
0
        } else {
538
0
            p2 = ossl_property_merge(pq, *plp);
539
0
            ossl_property_free(pq);
540
0
            if (p2 == NULL)
541
0
                goto fin;
542
0
            pq = p2;
543
0
        }
544
0
    }
545
546
5
    if (pq == NULL) {
547
0
        for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
548
0
            if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
549
0
                && (prov == NULL || impl->provider == prov)) {
550
0
                best_impl = impl;
551
0
                ret = 1;
552
0
                break;
553
0
            }
554
0
        }
555
0
        goto fin;
556
0
    }
557
5
    optional = ossl_property_has_optional(pq);
558
5
    for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
559
5
        if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
560
5
            && (prov == NULL || impl->provider == prov)) {
561
5
            score = ossl_property_match_count(pq, impl->properties);
562
5
            if (score > best) {
563
5
                best_impl = impl;
564
5
                best = score;
565
5
                ret = 1;
566
5
                if (!optional)
567
5
                    goto fin;
568
5
            }
569
5
        }
570
5
    }
571
5
fin:
572
5
    if (ret && ossl_method_up_ref(&best_impl->method)) {
573
5
        *method = best_impl->method.method;
574
5
        if (prov_rw != NULL)
575
5
            *prov_rw = best_impl->provider;
576
5
    } else {
577
0
        ret = 0;
578
0
    }
579
5
    ossl_property_unlock(store);
580
5
    ossl_property_free(p2);
581
5
    return ret;
582
5
}
583
584
static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
585
                                        ALGORITHM *alg)
586
27
{
587
27
    store->cache_nelem -= lh_QUERY_num_items(alg->cache);
588
27
    impl_cache_flush_alg(0, alg);
589
27
}
590
591
static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
592
208
{
593
208
    ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
594
595
208
    if (alg != NULL)
596
27
        ossl_method_cache_flush_alg(store, alg);
597
208
}
598
599
int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store)
600
0
{
601
0
    if (!ossl_property_write_lock(store))
602
0
        return 0;
603
0
    ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
604
0
    store->cache_nelem = 0;
605
0
    ossl_property_unlock(store);
606
0
    return 1;
607
0
}
608
609
IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
610
611
/*
612
 * Flush an element from the query cache (perhaps).
613
 *
614
 * In order to avoid taking a write lock or using atomic operations
615
 * to keep accurate least recently used (LRU) or least frequently used
616
 * (LFU) information, the procedure used here is to stochastically
617
 * flush approximately half the cache.
618
 *
619
 * This procedure isn't ideal, LRU or LFU would be better.  However,
620
 * in normal operation, reaching a full cache would be unexpected.
621
 * It means that no steady state of algorithm queries has been reached.
622
 * That is, it is most likely an attack of some form.  A suboptimal clearance
623
 * strategy that doesn't degrade performance of the normal case is
624
 * preferable to a more refined approach that imposes a performance
625
 * impact.
626
 */
627
static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
628
0
{
629
0
    uint32_t n;
630
631
    /*
632
     * Implement the 32 bit xorshift as suggested by George Marsaglia in:
633
     *      https://doi.org/10.18637/jss.v008.i14
634
     *
635
     * This is a very fast PRNG so there is no need to extract bits one at a
636
     * time and use the entire value each time.
637
     */
638
0
    n = state->seed;
639
0
    n ^= n << 13;
640
0
    n ^= n >> 17;
641
0
    n ^= n << 5;
642
0
    state->seed = n;
643
644
0
    if ((n & 1) != 0)
645
0
        impl_cache_free(lh_QUERY_delete(state->cache, c));
646
0
    else
647
0
        state->nelem++;
648
0
}
649
650
static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
651
                                     void *v)
652
0
{
653
0
    IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
654
655
0
    state->cache = alg->cache;
656
0
    lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
657
0
                                    state);
658
0
}
659
660
static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
661
0
{
662
0
    IMPL_CACHE_FLUSH state;
663
0
    static TSAN_QUALIFIER uint32_t global_seed = 1;
664
665
0
    state.nelem = 0;
666
0
    state.using_global_seed = 0;
667
0
    if ((state.seed = OPENSSL_rdtsc()) == 0) {
668
        /* If there is no timer available, seed another way */
669
0
        state.using_global_seed = 1;
670
0
        state.seed = tsan_load(&global_seed);
671
0
    }
672
0
    store->cache_need_flush = 0;
673
0
    ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
674
0
    store->cache_nelem = state.nelem;
675
    /* Without a timer, update the global seed */
676
0
    if (state.using_global_seed)
677
0
        tsan_store(&global_seed, state.seed);
678
0
}
679
680
int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
681
                                int nid, const char *prop_query, void **method)
682
20.7k
{
683
20.7k
    ALGORITHM *alg;
684
20.7k
    QUERY elem, *r;
685
20.7k
    int res = 0;
686
687
20.7k
    if (nid <= 0 || store == NULL || prop_query == NULL)
688
0
        return 0;
689
690
20.7k
    if (!ossl_property_read_lock(store))
691
0
        return 0;
692
20.7k
    alg = ossl_method_store_retrieve(store, nid);
693
20.7k
    if (alg == NULL)
694
1
        goto err;
695
696
20.7k
    elem.query = prop_query;
697
20.7k
    elem.provider = prov;
698
20.7k
    r = lh_QUERY_retrieve(alg->cache, &elem);
699
20.7k
    if (r == NULL)
700
2
        goto err;
701
20.7k
    if (ossl_method_up_ref(&r->method)) {
702
20.7k
        *method = r->method.method;
703
20.7k
        res = 1;
704
20.7k
    }
705
20.7k
err:
706
20.7k
    ossl_property_unlock(store);
707
20.7k
    return res;
708
20.7k
}
709
710
int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
711
                                int nid, const char *prop_query, void *method,
712
                                int (*method_up_ref)(void *),
713
                                void (*method_destruct)(void *))
714
5
{
715
5
    QUERY elem, *old, *p = NULL;
716
5
    ALGORITHM *alg;
717
5
    size_t len;
718
5
    int res = 1;
719
720
5
    if (nid <= 0 || store == NULL || prop_query == NULL)
721
0
        return 0;
722
723
5
    if (!ossl_assert(prov != NULL))
724
0
        return 0;
725
726
5
    if (!ossl_property_write_lock(store))
727
0
        return 0;
728
5
    if (store->cache_need_flush)
729
0
        ossl_method_cache_flush_some(store);
730
5
    alg = ossl_method_store_retrieve(store, nid);
731
5
    if (alg == NULL)
732
0
        goto err;
733
734
5
    if (method == NULL) {
735
0
        elem.query = prop_query;
736
0
        elem.provider = prov;
737
0
        if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) {
738
0
            impl_cache_free(old);
739
0
            store->cache_nelem--;
740
0
        }
741
0
        goto end;
742
0
    }
743
5
    p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
744
5
    if (p != NULL) {
745
5
        p->query = p->body;
746
5
        p->provider = prov;
747
5
        p->method.method = method;
748
5
        p->method.up_ref = method_up_ref;
749
5
        p->method.free = method_destruct;
750
5
        if (!ossl_method_up_ref(&p->method))
751
0
            goto err;
752
5
        memcpy((char *)p->query, prop_query, len + 1);
753
5
        if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
754
0
            impl_cache_free(old);
755
0
            goto end;
756
0
        }
757
5
        if (!lh_QUERY_error(alg->cache)) {
758
5
            if (++store->cache_nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
759
0
                store->cache_need_flush = 1;
760
5
            goto end;
761
5
        }
762
0
        ossl_method_free(&p->method);
763
0
    }
764
0
err:
765
0
    res = 0;
766
0
    OPENSSL_free(p);
767
5
end:
768
5
    ossl_property_unlock(store);
769
5
    return res;
770
0
}