Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/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
2.83k
#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
3.04k
{
136
3.04k
    OSSL_GLOBAL_PROPERTIES *globp;
137
138
3.04k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
139
3.04k
    if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
140
0
        return NULL;
141
3.04k
#endif
142
3.04k
    globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES,
143
3.04k
                                  &ossl_ctx_global_properties_method);
144
145
3.04k
    return globp != NULL ? &globp->list : NULL;
146
3.04k
}
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
11.6M
{
171
11.6M
    return (*method->up_ref)(method->method);
172
11.6M
}
173
174
static void ossl_method_free(METHOD *method)
175
11.9k
{
176
11.9k
    (*method->free)(method->method);
177
11.9k
}
178
179
static __owur int ossl_property_read_lock(OSSL_METHOD_STORE *p)
180
13.9M
{
181
13.9M
    return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
182
13.9M
}
183
184
static __owur int ossl_property_write_lock(OSSL_METHOD_STORE *p)
185
13.1k
{
186
13.1k
    return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
187
13.1k
}
188
189
static int ossl_property_unlock(OSSL_METHOD_STORE *p)
190
14.0M
{
191
14.0M
    return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
192
14.0M
}
193
194
static unsigned long query_hash(const QUERY *a)
195
11.6M
{
196
11.6M
    return OPENSSL_LH_strhash(a->query);
197
11.6M
}
198
199
static int query_cmp(const QUERY *a, const QUERY *b)
200
11.6M
{
201
11.6M
    int res = strcmp(a->query, b->query);
202
203
11.6M
    if (res == 0 && a->provider != NULL && b->provider != NULL)
204
123k
        res = b->provider > a->provider ? 1
205
123k
            : b->provider < a->provider ? -1
206
123k
            : 0;
207
11.6M
    return res;
208
11.6M
}
209
210
static void impl_free(IMPLEMENTATION *impl)
211
9.49k
{
212
9.49k
    if (impl != NULL) {
213
9.49k
        ossl_method_free(&impl->method);
214
9.49k
        OPENSSL_free(impl);
215
9.49k
    }
216
9.49k
}
217
218
static void impl_cache_free(QUERY *elem)
219
2.47k
{
220
2.47k
    if (elem != NULL) {
221
2.47k
        ossl_method_free(&elem->method);
222
2.47k
        OPENSSL_free(elem);
223
2.47k
    }
224
2.47k
}
225
226
static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)
227
2.40k
{
228
2.40k
    lh_QUERY_doall(alg->cache, &impl_cache_free);
229
2.40k
    lh_QUERY_flush(alg->cache);
230
2.40k
}
231
232
static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a, void *arg)
233
7.09k
{
234
7.09k
    OSSL_METHOD_STORE *store = arg;
235
236
7.09k
    if (a != NULL) {
237
7.09k
        sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
238
7.09k
        lh_QUERY_doall(a->cache, &impl_cache_free);
239
7.09k
        lh_QUERY_free(a->cache);
240
7.09k
        OPENSSL_free(a);
241
7.09k
    }
242
7.09k
    if (store != NULL)
243
7.09k
        ossl_sa_ALGORITHM_set(store->algs, idx, NULL);
244
7.09k
}
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
647
{
252
647
    OSSL_METHOD_STORE *res;
253
254
647
    res = OPENSSL_zalloc(sizeof(*res));
255
647
    if (res != NULL) {
256
647
        res->ctx = ctx;
257
647
        if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL
258
647
            || (res->lock = CRYPTO_THREAD_lock_new()) == NULL
259
647
            || (res->biglock = CRYPTO_THREAD_lock_new()) == NULL) {
260
0
            ossl_method_store_free(res);
261
0
            return NULL;
262
0
        }
263
647
    }
264
647
    return res;
265
647
}
266
267
void ossl_method_store_free(OSSL_METHOD_STORE *store)
268
439
{
269
439
    if (store != NULL) {
270
439
        if (store->algs != NULL)
271
439
            ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup, store);
272
439
        ossl_sa_ALGORITHM_free(store->algs);
273
439
        CRYPTO_THREAD_lock_free(store->lock);
274
439
        CRYPTO_THREAD_lock_free(store->biglock);
275
439
        OPENSSL_free(store);
276
439
    }
277
439
}
278
279
int ossl_method_lock_store(OSSL_METHOD_STORE *store)
280
5.28M
{
281
5.28M
    return store != NULL ? CRYPTO_THREAD_write_lock(store->biglock) : 0;
282
5.28M
}
283
284
int ossl_method_unlock_store(OSSL_METHOD_STORE *store)
285
5.28M
{
286
5.28M
    return store != NULL ? CRYPTO_THREAD_unlock(store->biglock) : 0;
287
5.28M
}
288
289
static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
290
12.4M
{
291
12.4M
    return ossl_sa_ALGORITHM_get(store->algs, nid);
292
12.4M
}
293
294
static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
295
7.71k
{
296
7.71k
    return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
297
7.71k
}
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
4.35k
{
304
4.35k
    ALGORITHM *alg = NULL;
305
4.35k
    IMPLEMENTATION *impl;
306
4.35k
    int ret = 0;
307
4.35k
    int i;
308
309
4.35k
    if (nid <= 0 || method == NULL || store == NULL)
310
0
        return 0;
311
4.35k
    if (properties == NULL)
312
0
        properties = "";
313
314
4.35k
    if (!ossl_assert(prov != NULL))
315
0
        return 0;
316
317
    /* Create new entry */
318
4.35k
    impl = OPENSSL_malloc(sizeof(*impl));
319
4.35k
    if (impl == NULL)
320
0
        return 0;
321
4.35k
    impl->method.method = method;
322
4.35k
    impl->method.up_ref = method_up_ref;
323
4.35k
    impl->method.free = method_destruct;
324
4.35k
    if (!ossl_method_up_ref(&impl->method)) {
325
0
        OPENSSL_free(impl);
326
0
        return 0;
327
0
    }
328
4.35k
    impl->provider = prov;
329
330
    /* Insert into the hash table if required */
331
4.35k
    if (!ossl_property_write_lock(store)) {
332
0
        impl_free(impl);
333
0
        return 0;
334
0
    }
335
4.35k
    ossl_method_cache_flush(store, nid);
336
4.35k
    if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
337
624
        impl->properties = ossl_parse_property(store->ctx, properties);
338
624
        if (impl->properties == NULL)
339
0
            goto err;
340
624
        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
624
    }
346
347
4.35k
    alg = ossl_method_store_retrieve(store, nid);
348
4.35k
    if (alg == NULL) {
349
3.04k
        if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
350
3.04k
                || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
351
3.04k
                || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
352
0
            goto err;
353
3.04k
        alg->nid = nid;
354
3.04k
        if (!ossl_method_store_insert(store, alg))
355
0
            goto err;
356
3.04k
    }
357
358
    /* Push onto stack if there isn't one there already */
359
9.73k
    for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
360
5.38k
        const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);
361
362
5.38k
        if (tmpimpl->provider == impl->provider
363
5.38k
            && tmpimpl->properties == impl->properties)
364
0
            break;
365
5.38k
    }
366
4.35k
    if (i == sk_IMPLEMENTATION_num(alg->impls)
367
4.35k
        && sk_IMPLEMENTATION_push(alg->impls, impl))
368
4.35k
        ret = 1;
369
4.35k
    ossl_property_unlock(store);
370
4.35k
    if (ret == 0)
371
0
        impl_free(impl);
372
4.35k
    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
4.35k
}
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
103M
{
471
103M
    fn(alg->nid, impl->method.method, fnarg);
472
103M
}
473
474
static void alg_copy(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
475
64.4M
{
476
64.4M
    STACK_OF(ALGORITHM) *newalg = arg;
477
478
64.4M
    (void)sk_ALGORITHM_push(newalg, alg);
479
64.4M
}
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
1.56M
{
485
1.56M
    int i, j;
486
1.56M
    int numalgs, numimps;
487
1.56M
    STACK_OF(ALGORITHM) *tmpalgs;
488
1.56M
    ALGORITHM *alg;
489
490
1.56M
    if (store != NULL) {
491
492
1.56M
        if (!ossl_property_read_lock(store))
493
0
            return;
494
       
495
1.56M
        tmpalgs = sk_ALGORITHM_new_reserve(NULL,
496
1.56M
                                           ossl_sa_ALGORITHM_num(store->algs));
497
1.56M
        if (tmpalgs == NULL) {
498
0
            ossl_property_unlock(store);
499
0
            return;
500
0
        }
501
502
1.56M
        ossl_sa_ALGORITHM_doall_arg(store->algs, alg_copy, tmpalgs);
503
1.56M
        ossl_property_unlock(store);
504
1.56M
        numalgs = sk_ALGORITHM_num(tmpalgs);
505
66.0M
        for (i = 0; i < numalgs; i++) {
506
64.4M
            alg = sk_ALGORITHM_value(tmpalgs, i);
507
64.4M
            numimps = sk_IMPLEMENTATION_num(alg->impls);
508
167M
            for (j = 0; j < numimps; j++)
509
103M
                alg_do_one(alg, sk_IMPLEMENTATION_value(alg->impls, j), fn, fnarg);
510
64.4M
        }
511
1.56M
        sk_ALGORITHM_free(tmpalgs);
512
1.56M
    }
513
1.56M
}
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
241k
{
519
241k
    OSSL_PROPERTY_LIST **plp;
520
241k
    ALGORITHM *alg;
521
241k
    IMPLEMENTATION *impl, *best_impl = NULL;
522
241k
    OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;
523
241k
    const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL;
524
241k
    int ret = 0;
525
241k
    int j, best = -1, score, optional;
526
527
241k
    if (nid <= 0 || method == NULL || store == NULL)
528
0
        return 0;
529
530
241k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
531
241k
    if (ossl_lib_ctx_is_default(store->ctx)
532
241k
            && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
533
0
        return 0;
534
241k
#endif
535
536
    /* This only needs to be a read lock, because the query won't create anything */
537
241k
    if (!ossl_property_read_lock(store))
538
0
        return 0;
539
241k
    alg = ossl_method_store_retrieve(store, nid);
540
241k
    if (alg == NULL) {
541
241k
        ossl_property_unlock(store);
542
241k
        return 0;
543
241k
    }
544
545
499
    if (prop_query != NULL)
546
499
        p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);
547
499
    plp = ossl_ctx_global_properties(store->ctx, 0);
548
499
    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
499
    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
499
    optional = ossl_property_has_optional(pq);
572
499
    for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
573
499
        if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
574
499
            && (prov == NULL || impl->provider == prov)) {
575
499
            score = ossl_property_match_count(pq, impl->properties);
576
499
            if (score > best) {
577
499
                best_impl = impl;
578
499
                best = score;
579
499
                ret = 1;
580
499
                if (!optional)
581
499
                    goto fin;
582
499
            }
583
499
        }
584
499
    }
585
499
fin:
586
499
    if (ret && ossl_method_up_ref(&best_impl->method)) {
587
499
        *method = best_impl->method.method;
588
499
        if (prov_rw != NULL)
589
499
            *prov_rw = best_impl->provider;
590
499
    } else {
591
0
        ret = 0;
592
0
    }
593
499
    ossl_property_unlock(store);
594
499
    ossl_property_free(p2);
595
499
    return ret;
596
499
}
597
598
static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
599
                                        ALGORITHM *alg)
600
2.40k
{
601
2.40k
    store->cache_nelem -= lh_QUERY_num_items(alg->cache);
602
2.40k
    impl_cache_flush_alg(0, alg);
603
2.40k
}
604
605
static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
606
10.1k
{
607
10.1k
    ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
608
609
10.1k
    if (alg != NULL)
610
2.40k
        ossl_method_cache_flush_alg(store, alg);
611
10.1k
}
612
613
int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store)
614
240
{
615
240
    if (!ossl_property_write_lock(store))
616
0
        return 0;
617
240
    ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
618
240
    store->cache_nelem = 0;
619
240
    ossl_property_unlock(store);
620
240
    return 1;
621
240
}
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
3.00k
{
643
3.00k
    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
3.00k
    n = state->seed;
653
3.00k
    n ^= n << 13;
654
3.00k
    n ^= n >> 17;
655
3.00k
    n ^= n << 5;
656
3.00k
    state->seed = n;
657
658
3.00k
    if ((n & 1) != 0)
659
1.51k
        impl_cache_free(lh_QUERY_delete(state->cache, c));
660
1.48k
    else
661
1.48k
        state->nelem++;
662
3.00k
}
663
664
static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
665
                                     void *v)
666
1.83k
{
667
1.83k
    IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
668
1.83k
    unsigned long orig_down_load = lh_QUERY_get_down_load(alg->cache);
669
670
1.83k
    state->cache = alg->cache;
671
1.83k
    lh_QUERY_set_down_load(alg->cache, 0);
672
1.83k
    lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
673
1.83k
                                    state);
674
1.83k
    lh_QUERY_set_down_load(alg->cache, orig_down_load);
675
1.83k
}
676
677
static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
678
6
{
679
6
    IMPL_CACHE_FLUSH state;
680
6
    static TSAN_QUALIFIER uint32_t global_seed = 1;
681
682
6
    state.nelem = 0;
683
6
    state.using_global_seed = 0;
684
6
    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
6
    store->cache_need_flush = 0;
690
6
    ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
691
6
    store->cache_nelem = state.nelem;
692
    /* Without a timer, update the global seed */
693
6
    if (state.using_global_seed)
694
6
        tsan_store(&global_seed, state.seed);
695
6
}
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
12.0M
{
700
12.0M
    ALGORITHM *alg;
701
12.0M
    QUERY elem, *r;
702
12.0M
    int res = 0;
703
704
12.0M
    if (nid <= 0 || store == NULL || prop_query == NULL)
705
0
        return 0;
706
707
12.0M
    if (!ossl_property_read_lock(store))
708
0
        return 0;
709
12.0M
    alg = ossl_method_store_retrieve(store, nid);
710
12.0M
    if (alg == NULL)
711
396k
        goto err;
712
713
11.6M
    elem.query = prop_query;
714
11.6M
    elem.provider = prov;
715
11.6M
    r = lh_QUERY_retrieve(alg->cache, &elem);
716
11.6M
    if (r == NULL)
717
2.84k
        goto err;
718
11.6M
    if (ossl_method_up_ref(&r->method)) {
719
11.6M
        *method = r->method.method;
720
11.6M
        res = 1;
721
11.6M
    }
722
12.0M
err:
723
12.0M
    ossl_property_unlock(store);
724
12.0M
    return res;
725
11.6M
}
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
2.83k
{
732
2.83k
    QUERY elem, *old, *p = NULL;
733
2.83k
    ALGORITHM *alg;
734
2.83k
    size_t len;
735
2.83k
    int res = 1;
736
737
2.83k
    if (nid <= 0 || store == NULL || prop_query == NULL)
738
0
        return 0;
739
740
2.83k
    if (!ossl_assert(prov != NULL))
741
0
        return 0;
742
743
2.83k
    if (!ossl_property_write_lock(store))
744
0
        return 0;
745
2.83k
    if (store->cache_need_flush)
746
6
        ossl_method_cache_flush_some(store);
747
2.83k
    alg = ossl_method_store_retrieve(store, nid);
748
2.83k
    if (alg == NULL)
749
0
        goto err;
750
751
2.83k
    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
2.83k
    p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
761
2.83k
    if (p != NULL) {
762
2.83k
        p->query = p->body;
763
2.83k
        p->provider = prov;
764
2.83k
        p->method.method = method;
765
2.83k
        p->method.up_ref = method_up_ref;
766
2.83k
        p->method.free = method_destruct;
767
2.83k
        if (!ossl_method_up_ref(&p->method))
768
0
            goto err;
769
2.83k
        memcpy((char *)p->query, prop_query, len + 1);
770
2.83k
        if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
771
0
            impl_cache_free(old);
772
0
            goto end;
773
0
        }
774
2.83k
        if (!lh_QUERY_error(alg->cache)) {
775
2.83k
            if (++store->cache_nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
776
6
                store->cache_need_flush = 1;
777
2.83k
            goto end;
778
2.83k
        }
779
0
        ossl_method_free(&p->method);
780
0
    }
781
0
err:
782
0
    res = 0;
783
0
    OPENSSL_free(p);
784
2.83k
end:
785
2.83k
    ossl_property_unlock(store);
786
2.83k
    return res;
787
0
}