Coverage Report

Created: 2024-07-27 06:39

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