Coverage Report

Created: 2025-12-31 06:58

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