Coverage Report

Created: 2025-04-22 06:18

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