Coverage Report

Created: 2023-06-08 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
47
#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
                                                int loadconfig)
133
47
{
134
47
    OSSL_GLOBAL_PROPERTIES *globp;
135
136
47
#ifndef FIPS_MODULE
137
47
    if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
138
0
        return NULL;
139
47
#endif
140
47
    globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
141
47
                                  &ossl_ctx_global_properties_method);
142
143
47
    return globp != NULL ? &globp->list : NULL;
144
47
}
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
761k
{
169
761k
    return (*method->up_ref)(method->method);
170
761k
}
171
172
static void ossl_method_free(METHOD *method)
173
296
{
174
296
    (*method->free)(method->method);
175
296
}
176
177
static __owur int ossl_property_read_lock(OSSL_METHOD_STORE *p)
178
833k
{
179
833k
    return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
180
833k
}
181
182
static __owur int ossl_property_write_lock(OSSL_METHOD_STORE *p)
183
304
{
184
304
    return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
185
304
}
186
187
static int ossl_property_unlock(OSSL_METHOD_STORE *p)
188
833k
{
189
833k
    return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
190
833k
}
191
192
static unsigned long query_hash(const QUERY *a)
193
761k
{
194
761k
    return OPENSSL_LH_strhash(a->query);
195
761k
}
196
197
static int query_cmp(const QUERY *a, const QUERY *b)
198
761k
{
199
761k
    int res = strcmp(a->query, b->query);
200
201
761k
    if (res == 0 && a->provider != NULL && b->provider != NULL)
202
4.38k
        res = b->provider > a->provider ? 1
203
4.38k
            : b->provider < a->provider ? -1
204
4.38k
            : 0;
205
761k
    return res;
206
761k
}
207
208
static void impl_free(IMPLEMENTATION *impl)
209
249
{
210
249
    if (impl != NULL) {
211
249
        ossl_method_free(&impl->method);
212
249
        OPENSSL_free(impl);
213
249
    }
214
249
}
215
216
static void impl_cache_free(QUERY *elem)
217
47
{
218
47
    if (elem != NULL) {
219
47
        ossl_method_free(&elem->method);
220
47
        OPENSSL_free(elem);
221
47
    }
222
47
}
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
222
{
232
222
    OSSL_METHOD_STORE *store = arg;
233
234
222
    if (a != NULL) {
235
222
        sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
236
222
        lh_QUERY_doall(a->cache, &impl_cache_free);
237
222
        lh_QUERY_free(a->cache);
238
222
        OPENSSL_free(a);
239
222
    }
240
222
    if (store != NULL)
241
222
        ossl_sa_ALGORITHM_set(store->algs, idx, NULL);
242
222
}
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
8
{
250
8
    OSSL_METHOD_STORE *res;
251
252
8
    res = OPENSSL_zalloc(sizeof(*res));
253
8
    if (res != NULL) {
254
8
        res->ctx = ctx;
255
8
        if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL
256
8
            || (res->lock = CRYPTO_THREAD_lock_new()) == NULL
257
8
            || (res->biglock = CRYPTO_THREAD_lock_new()) == NULL) {
258
0
            ossl_method_store_free(res);
259
0
            return NULL;
260
0
        }
261
8
    }
262
8
    return res;
263
8
}
264
265
void ossl_method_store_free(OSSL_METHOD_STORE *store)
266
8
{
267
8
    if (store != NULL) {
268
8
        if (store->algs != NULL)
269
8
            ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup, store);
270
8
        ossl_sa_ALGORITHM_free(store->algs);
271
8
        CRYPTO_THREAD_lock_free(store->lock);
272
8
        CRYPTO_THREAD_lock_free(store->biglock);
273
8
        OPENSSL_free(store);
274
8
    }
275
8
}
276
277
int ossl_method_lock_store(OSSL_METHOD_STORE *store)
278
254k
{
279
254k
    return store != NULL ? CRYPTO_THREAD_write_lock(store->biglock) : 0;
280
254k
}
281
282
int ossl_method_unlock_store(OSSL_METHOD_STORE *store)
283
254k
{
284
254k
    return store != NULL ? CRYPTO_THREAD_unlock(store->biglock) : 0;
285
254k
}
286
287
static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
288
833k
{
289
833k
    return ossl_sa_ALGORITHM_get(store->algs, nid);
290
833k
}
291
292
static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
293
222
{
294
222
    return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
295
222
}
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
249
{
302
249
    ALGORITHM *alg = NULL;
303
249
    IMPLEMENTATION *impl;
304
249
    int ret = 0;
305
249
    int i;
306
307
249
    if (nid <= 0 || method == NULL || store == NULL)
308
0
        return 0;
309
249
    if (properties == NULL)
310
0
        properties = "";
311
312
249
    if (!ossl_assert(prov != NULL))
313
0
        return 0;
314
315
    /* Create new entry */
316
249
    impl = OPENSSL_malloc(sizeof(*impl));
317
249
    if (impl == NULL)
318
0
        return 0;
319
249
    impl->method.method = method;
320
249
    impl->method.up_ref = method_up_ref;
321
249
    impl->method.free = method_destruct;
322
249
    if (!ossl_method_up_ref(&impl->method)) {
323
0
        OPENSSL_free(impl);
324
0
        return 0;
325
0
    }
326
249
    impl->provider = prov;
327
328
    /* Insert into the hash table if required */
329
249
    if (!ossl_property_write_lock(store)) {
330
0
        OPENSSL_free(impl);
331
0
        return 0;
332
0
    }
333
249
    ossl_method_cache_flush(store, nid);
334
249
    if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
335
16
        impl->properties = ossl_parse_property(store->ctx, properties);
336
16
        if (impl->properties == NULL)
337
0
            goto err;
338
16
        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
16
    }
344
345
249
    alg = ossl_method_store_retrieve(store, nid);
346
249
    if (alg == NULL) {
347
222
        if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
348
222
                || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
349
222
                || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
350
0
            goto err;
351
222
        alg->nid = nid;
352
222
        if (!ossl_method_store_insert(store, alg))
353
0
            goto err;
354
222
    }
355
356
    /* Push onto stack if there isn't one there already */
357
306
    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
249
    if (i == sk_IMPLEMENTATION_num(alg->impls)
365
249
        && sk_IMPLEMENTATION_push(alg->impls, impl))
366
249
        ret = 1;
367
249
    ossl_property_unlock(store);
368
249
    if (ret == 0)
369
0
        impl_free(impl);
370
249
    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
249
}
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
1.21M
{
469
1.21M
    fn(alg->nid, impl->method.method, fnarg);
470
1.21M
}
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
999k
{
479
999k
    struct alg_do_each_data_st *data = arg;
480
999k
    int i, end = sk_IMPLEMENTATION_num(alg->impls);
481
482
2.21M
    for (i = 0; i < end; i++) {
483
1.21M
        IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
484
485
1.21M
        alg_do_one(alg, impl, data->fn, data->fnarg);
486
1.21M
    }
487
999k
}
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
12.2k
{
493
12.2k
    struct alg_do_each_data_st data;
494
495
12.2k
    data.fn = fn;
496
12.2k
    data.fnarg = fnarg;
497
12.2k
    if (store != NULL)
498
12.2k
        ossl_sa_ALGORITHM_doall_arg(store->algs, alg_do_each, &data);
499
12.2k
}
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
35.9k
{
505
35.9k
    OSSL_PROPERTY_LIST **plp;
506
35.9k
    ALGORITHM *alg;
507
35.9k
    IMPLEMENTATION *impl, *best_impl = NULL;
508
35.9k
    OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;
509
35.9k
    const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL;
510
35.9k
    int ret = 0;
511
35.9k
    int j, best = -1, score, optional;
512
513
35.9k
    if (nid <= 0 || method == NULL || store == NULL)
514
0
        return 0;
515
516
35.9k
#ifndef FIPS_MODULE
517
35.9k
    if (ossl_lib_ctx_is_default(store->ctx)
518
35.9k
            && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
519
0
        return 0;
520
35.9k
#endif
521
522
    /* This only needs to be a read lock, because the query won't create anything */
523
35.9k
    if (!ossl_property_read_lock(store))
524
0
        return 0;
525
35.9k
    alg = ossl_method_store_retrieve(store, nid);
526
35.9k
    if (alg == NULL) {
527
35.9k
        ossl_property_unlock(store);
528
35.9k
        return 0;
529
35.9k
    }
530
531
47
    if (prop_query != NULL)
532
47
        p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);
533
47
    plp = ossl_ctx_global_properties(store->ctx, 0);
534
47
    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
47
    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
47
    optional = ossl_property_has_optional(pq);
558
47
    for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
559
47
        if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
560
47
            && (prov == NULL || impl->provider == prov)) {
561
47
            score = ossl_property_match_count(pq, impl->properties);
562
47
            if (score > best) {
563
47
                best_impl = impl;
564
47
                best = score;
565
47
                ret = 1;
566
47
                if (!optional)
567
47
                    goto fin;
568
47
            }
569
47
        }
570
47
    }
571
47
fin:
572
47
    if (ret && ossl_method_up_ref(&best_impl->method)) {
573
47
        *method = best_impl->method.method;
574
47
        if (prov_rw != NULL)
575
47
            *prov_rw = best_impl->provider;
576
47
    } else {
577
0
        ret = 0;
578
0
    }
579
47
    ossl_property_unlock(store);
580
47
    ossl_property_free(p2);
581
47
    return ret;
582
47
}
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
249
{
593
249
    ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
594
595
249
    if (alg != NULL)
596
27
        ossl_method_cache_flush_alg(store, alg);
597
249
}
598
599
int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store)
600
8
{
601
8
    if (!ossl_property_write_lock(store))
602
0
        return 0;
603
8
    ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
604
8
    store->cache_nelem = 0;
605
8
    ossl_property_unlock(store);
606
8
    return 1;
607
8
}
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
797k
{
683
797k
    ALGORITHM *alg;
684
797k
    QUERY elem, *r;
685
797k
    int res = 0;
686
687
797k
    if (nid <= 0 || store == NULL || prop_query == NULL)
688
0
        return 0;
689
690
797k
    if (!ossl_property_read_lock(store))
691
0
        return 0;
692
797k
    alg = ossl_method_store_retrieve(store, nid);
693
797k
    if (alg == NULL)
694
35.9k
        goto err;
695
696
761k
    elem.query = prop_query;
697
761k
    elem.provider = prov;
698
761k
    r = lh_QUERY_retrieve(alg->cache, &elem);
699
761k
    if (r == NULL)
700
39
        goto err;
701
761k
    if (ossl_method_up_ref(&r->method)) {
702
761k
        *method = r->method.method;
703
761k
        res = 1;
704
761k
    }
705
797k
err:
706
797k
    ossl_property_unlock(store);
707
797k
    return res;
708
761k
}
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
47
{
715
47
    QUERY elem, *old, *p = NULL;
716
47
    ALGORITHM *alg;
717
47
    size_t len;
718
47
    int res = 1;
719
720
47
    if (nid <= 0 || store == NULL || prop_query == NULL)
721
0
        return 0;
722
723
47
    if (!ossl_assert(prov != NULL))
724
0
        return 0;
725
726
47
    if (!ossl_property_write_lock(store))
727
0
        return 0;
728
47
    if (store->cache_need_flush)
729
0
        ossl_method_cache_flush_some(store);
730
47
    alg = ossl_method_store_retrieve(store, nid);
731
47
    if (alg == NULL)
732
0
        goto err;
733
734
47
    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
47
    p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
744
47
    if (p != NULL) {
745
47
        p->query = p->body;
746
47
        p->provider = prov;
747
47
        p->method.method = method;
748
47
        p->method.up_ref = method_up_ref;
749
47
        p->method.free = method_destruct;
750
47
        if (!ossl_method_up_ref(&p->method))
751
0
            goto err;
752
47
        memcpy((char *)p->query, prop_query, len + 1);
753
47
        if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
754
0
            impl_cache_free(old);
755
0
            goto end;
756
0
        }
757
47
        if (!lh_QUERY_error(alg->cache)) {
758
47
            if (++store->cache_nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
759
0
                store->cache_need_flush = 1;
760
47
            goto end;
761
47
        }
762
0
        ossl_method_free(&p->method);
763
0
    }
764
0
err:
765
0
    res = 0;
766
0
    OPENSSL_free(p);
767
47
end:
768
47
    ossl_property_unlock(store);
769
47
    return res;
770
0
}