Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/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 <openssl/trace.h>
23
#include "internal/thread_once.h"
24
#include "crypto/lhash.h"
25
#include "crypto/sparse_array.h"
26
#include "property_local.h"
27
#include "crypto/context.h"
28
29
/*
30
 * The number of elements in the query cache before we initiate a flush.
31
 * If reducing this, also ensure the stochastic test in test/property_test.c
32
 * isn't likely to fail.
33
 */
34
8.05k
#define IMPL_CACHE_FLUSH_THRESHOLD 500
35
36
typedef struct {
37
    void *method;
38
    int (*up_ref)(void *);
39
    void (*free)(void *);
40
} METHOD;
41
42
typedef struct {
43
    const OSSL_PROVIDER *provider;
44
    OSSL_PROPERTY_LIST *properties;
45
    METHOD method;
46
} IMPLEMENTATION;
47
48
DEFINE_STACK_OF(IMPLEMENTATION)
49
50
typedef struct {
51
    const OSSL_PROVIDER *provider;
52
    const char *query;
53
    METHOD method;
54
    char body[1];
55
} QUERY;
56
57
DEFINE_LHASH_OF_EX(QUERY);
58
59
typedef struct {
60
    int nid;
61
    STACK_OF(IMPLEMENTATION) *impls;
62
    LHASH_OF(QUERY) *cache;
63
} ALGORITHM;
64
65
struct ossl_method_store_st {
66
    OSSL_LIB_CTX *ctx;
67
    SPARSE_ARRAY_OF(ALGORITHM) * algs;
68
    /*
69
     * Lock to protect the |algs| array from concurrent writing, when
70
     * individual implementations or queries are inserted.  This is used
71
     * by the appropriate functions here.
72
     */
73
    CRYPTO_RWLOCK *lock;
74
    /*
75
     * Lock to reserve the whole store.  This is used when fetching a set
76
     * of algorithms, via these functions, found in crypto/core_fetch.c:
77
     * ossl_method_construct_reserve_store()
78
     * ossl_method_construct_unreserve_store()
79
     */
80
    CRYPTO_RWLOCK *biglock;
81
82
    /* query cache specific values */
83
84
    /* Count of the query cache entries for all algs */
85
    size_t cache_nelem;
86
87
    /* Flag: 1 if query cache entries for all algs need flushing */
88
    int cache_need_flush;
89
};
90
91
typedef struct {
92
    LHASH_OF(QUERY) *cache;
93
    size_t nelem;
94
    uint32_t seed;
95
    unsigned char using_global_seed;
96
} IMPL_CACHE_FLUSH;
97
98
DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
99
100
DEFINE_STACK_OF(ALGORITHM)
101
102
typedef struct ossl_global_properties_st {
103
    OSSL_PROPERTY_LIST *list;
104
#ifndef FIPS_MODULE
105
    unsigned int no_mirrored : 1;
106
#endif
107
} OSSL_GLOBAL_PROPERTIES;
108
109
static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
110
    ALGORITHM *alg);
111
static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
112
113
/* Global properties are stored per library context */
114
void ossl_ctx_global_properties_free(void *vglobp)
115
196
{
116
196
    OSSL_GLOBAL_PROPERTIES *globp = vglobp;
117
118
196
    if (globp != NULL) {
119
196
        ossl_property_free(globp->list);
120
196
        OPENSSL_free(globp);
121
196
    }
122
196
}
123
124
void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)
125
378
{
126
378
    return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES));
127
378
}
128
129
OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
130
    ossl_unused int loadconfig)
131
19.4k
{
132
19.4k
    OSSL_GLOBAL_PROPERTIES *globp;
133
134
19.4k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
135
19.4k
    if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
136
0
        return NULL;
137
19.4k
#endif
138
19.4k
    globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
139
140
19.4k
    return globp != NULL ? &globp->list : NULL;
141
19.4k
}
142
143
#ifndef FIPS_MODULE
144
int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx)
145
0
{
146
0
    OSSL_GLOBAL_PROPERTIES *globp
147
0
        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
148
149
0
    return globp != NULL && globp->no_mirrored ? 1 : 0;
150
0
}
151
152
void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx)
153
0
{
154
0
    OSSL_GLOBAL_PROPERTIES *globp
155
0
        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
156
157
0
    if (globp != NULL)
158
0
        globp->no_mirrored = 1;
159
0
}
160
#endif
161
162
static int ossl_method_up_ref(METHOD *method)
163
24.6M
{
164
24.6M
    return (*method->up_ref)(method->method);
165
24.6M
}
166
167
static void ossl_method_free(METHOD *method)
168
23.1k
{
169
23.1k
    (*method->free)(method->method);
170
23.1k
}
171
172
static __owur int ossl_property_read_lock(OSSL_METHOD_STORE *p)
173
27.4M
{
174
27.4M
    return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
175
27.4M
}
176
177
static __owur int ossl_property_write_lock(OSSL_METHOD_STORE *p)
178
33.4k
{
179
33.4k
    return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
180
33.4k
}
181
182
static int ossl_property_unlock(OSSL_METHOD_STORE *p)
183
27.4M
{
184
27.4M
    return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
185
27.4M
}
186
187
static unsigned long query_hash(const QUERY *a)
188
24.6M
{
189
24.6M
    return OPENSSL_LH_strhash(a->query);
190
24.6M
}
191
192
static int query_cmp(const QUERY *a, const QUERY *b)
193
24.6M
{
194
24.6M
    int res = strcmp(a->query, b->query);
195
196
24.6M
    if (res == 0 && a->provider != NULL && b->provider != NULL)
197
222k
        res = b->provider > a->provider ? 1
198
222k
            : b->provider < a->provider ? -1
199
222k
                                        : 0;
200
24.6M
    return res;
201
24.6M
}
202
203
static void impl_free(IMPLEMENTATION *impl)
204
16.9k
{
205
16.9k
    if (impl != NULL) {
206
16.9k
        ossl_method_free(&impl->method);
207
16.9k
        OPENSSL_free(impl);
208
16.9k
    }
209
16.9k
}
210
211
static void impl_cache_free(QUERY *elem)
212
6.16k
{
213
6.16k
    if (elem != NULL) {
214
6.16k
        ossl_method_free(&elem->method);
215
6.16k
        OPENSSL_free(elem);
216
6.16k
    }
217
6.16k
}
218
219
static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)
220
5.01k
{
221
5.01k
    lh_QUERY_doall(alg->cache, &impl_cache_free);
222
5.01k
    lh_QUERY_flush(alg->cache);
223
5.01k
}
224
225
static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a, void *arg)
226
13.1k
{
227
13.1k
    OSSL_METHOD_STORE *store = arg;
228
229
13.1k
    if (a != NULL) {
230
13.1k
        sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
231
13.1k
        lh_QUERY_doall(a->cache, &impl_cache_free);
232
13.1k
        lh_QUERY_free(a->cache);
233
13.1k
        OPENSSL_free(a);
234
13.1k
    }
235
13.1k
    if (store != NULL)
236
13.1k
        ossl_sa_ALGORITHM_set(store->algs, idx, NULL);
237
13.1k
}
238
239
/*
240
 * The OSSL_LIB_CTX param here allows access to underlying property data needed
241
 * for computation
242
 */
243
OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx)
244
1.25k
{
245
1.25k
    OSSL_METHOD_STORE *res;
246
247
1.25k
    res = OPENSSL_zalloc(sizeof(*res));
248
1.25k
    if (res != NULL) {
249
1.25k
        res->ctx = ctx;
250
1.25k
        if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL
251
1.25k
            || (res->lock = CRYPTO_THREAD_lock_new()) == NULL
252
1.25k
            || (res->biglock = CRYPTO_THREAD_lock_new()) == NULL) {
253
0
            ossl_method_store_free(res);
254
0
            return NULL;
255
0
        }
256
1.25k
    }
257
1.25k
    return res;
258
1.25k
}
259
260
void ossl_method_store_free(OSSL_METHOD_STORE *store)
261
827
{
262
827
    if (store != NULL) {
263
827
        if (store->algs != NULL)
264
827
            ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup, store);
265
827
        ossl_sa_ALGORITHM_free(store->algs);
266
827
        CRYPTO_THREAD_lock_free(store->lock);
267
827
        CRYPTO_THREAD_lock_free(store->biglock);
268
827
        OPENSSL_free(store);
269
827
    }
270
827
}
271
272
int ossl_method_lock_store(OSSL_METHOD_STORE *store)
273
7.02M
{
274
7.02M
    return store != NULL ? CRYPTO_THREAD_write_lock(store->biglock) : 0;
275
7.02M
}
276
277
int ossl_method_unlock_store(OSSL_METHOD_STORE *store)
278
7.02M
{
279
7.02M
    return store != NULL ? CRYPTO_THREAD_unlock(store->biglock) : 0;
280
7.02M
}
281
282
static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
283
26.3M
{
284
26.3M
    return ossl_sa_ALGORITHM_get(store->algs, nid);
285
26.3M
}
286
287
static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
288
19.5k
{
289
19.5k
    return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
290
19.5k
}
291
292
/**
293
 * @brief Adds a method to the specified method store.
294
 *
295
 * This function adds a new method to the provided method store, associating it
296
 * with a specified id, properties, and provider. The method is stored with
297
 * reference count and destruction callbacks.
298
 *
299
 * @param store Pointer to the OSSL_METHOD_STORE where the method will be added.
300
 *              Must be non-null.
301
 * @param prov Pointer to the OSSL_PROVIDER for the provider of the method.
302
 *             Must be non-null.
303
 * @param nid (identifier) associated with the method, must be > 0
304
 * @param properties String containing properties of the method.
305
 * @param method Pointer to the method to be added.
306
 * @param method_up_ref Function pointer for incrementing the method ref count.
307
 * @param method_destruct Function pointer for destroying the method.
308
 *
309
 * @return 1 if the method is successfully added, 0 on failure.
310
 *
311
 * If tracing is enabled, a message is printed indicating that the method is
312
 * being added to the method store.
313
 *
314
 * NOTE: The nid parameter here is _not_ a nid in the sense of the NID_* macros.
315
 * It is an internal unique identifier.
316
 */
317
int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
318
    int nid, const char *properties, void *method,
319
    int (*method_up_ref)(void *),
320
    void (*method_destruct)(void *))
321
17.2k
{
322
17.2k
    ALGORITHM *alg = NULL;
323
17.2k
    IMPLEMENTATION *impl;
324
17.2k
    int ret = 0;
325
17.2k
    int i;
326
327
17.2k
    if (nid <= 0 || method == NULL || store == NULL)
328
0
        return 0;
329
330
17.2k
    if (properties == NULL)
331
0
        properties = "";
332
333
17.2k
    if (!ossl_assert(prov != NULL))
334
0
        return 0;
335
336
    /* Create new entry */
337
17.2k
    impl = OPENSSL_malloc(sizeof(*impl));
338
17.2k
    if (impl == NULL)
339
0
        return 0;
340
17.2k
    impl->method.method = method;
341
17.2k
    impl->method.up_ref = method_up_ref;
342
17.2k
    impl->method.free = method_destruct;
343
17.2k
    if (!ossl_method_up_ref(&impl->method)) {
344
0
        OPENSSL_free(impl);
345
0
        return 0;
346
0
    }
347
17.2k
    impl->provider = prov;
348
349
    /* Insert into the hash table if required */
350
17.2k
    if (!ossl_property_write_lock(store)) {
351
0
        impl_free(impl);
352
0
        return 0;
353
0
    }
354
355
    /*
356
     * Flush the alg cache of any implementation that already exists
357
     * for this id.
358
     * This is done to ensure that on the next lookup we go through the
359
     * provider comparison in ossl_method_store_fetch.  If we don't do this
360
     * then this new method won't be given a chance to get selected.
361
     * NOTE: This doesn't actually remove the method from the backing store
362
     * It just ensures that we query the backing store when (re)-adding a
363
     * method to the algorithm cache, in case the one selected by the next
364
     * query selects a different implementation
365
     */
366
17.2k
    ossl_method_cache_flush(store, nid);
367
368
    /*
369
     * Parse the properties associated with this method, and convert it to a
370
     * property list stored against the implementation for later comparison
371
     * during fetch operations
372
     */
373
17.2k
    if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
374
954
        impl->properties = ossl_parse_property(store->ctx, properties);
375
954
        if (impl->properties == NULL)
376
0
            goto err;
377
954
        if (!ossl_prop_defn_set(store->ctx, properties, &impl->properties)) {
378
0
            ossl_property_free(impl->properties);
379
0
            impl->properties = NULL;
380
0
            goto err;
381
0
        }
382
954
    }
383
384
    /*
385
     * Check if we have an algorithm cache already for this nid.  If so use
386
     * it, otherwise, create it, and insert it into the store
387
     */
388
17.2k
    alg = ossl_method_store_retrieve(store, nid);
389
17.2k
    if (alg == NULL) {
390
13.8k
        if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
391
13.8k
            || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
392
13.8k
            || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
393
0
            goto err;
394
13.8k
        alg->nid = nid;
395
13.8k
        if (!ossl_method_store_insert(store, alg))
396
0
            goto err;
397
13.8k
        OSSL_TRACE2(QUERY, "Inserted an alg with nid %d into the store %p\n", nid, (void *)store);
398
13.8k
    }
399
400
    /* Push onto stack if there isn't one there already */
401
28.5k
    for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
402
11.2k
        const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);
403
404
11.2k
        if (tmpimpl->provider == impl->provider
405
11.2k
            && tmpimpl->properties == impl->properties)
406
0
            break;
407
11.2k
    }
408
409
17.2k
    if (i == sk_IMPLEMENTATION_num(alg->impls)
410
17.2k
        && sk_IMPLEMENTATION_push(alg->impls, impl)) {
411
17.2k
        ret = 1;
412
17.2k
#ifndef FIPS_MODULE
413
17.2k
        OSSL_TRACE_BEGIN(QUERY)
414
0
        {
415
0
            BIO_printf(trc_out, "Adding to method store "
416
0
                                "nid: %d\nproperties: %s\nprovider: %s\n",
417
0
                nid, properties,
418
0
                ossl_provider_name(prov) == NULL ? "none" : ossl_provider_name(prov));
419
0
        }
420
17.2k
        OSSL_TRACE_END(QUERY);
421
17.2k
#endif
422
17.2k
    }
423
17.2k
    ossl_property_unlock(store);
424
17.2k
    if (ret == 0)
425
0
        impl_free(impl);
426
17.2k
    return ret;
427
428
0
err:
429
0
    ossl_property_unlock(store);
430
0
    alg_cleanup(0, alg, NULL);
431
0
    impl_free(impl);
432
0
    return 0;
433
17.2k
}
434
435
int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
436
    const void *method)
437
0
{
438
0
    ALGORITHM *alg = NULL;
439
0
    int i;
440
441
0
    if (nid <= 0 || method == NULL || store == NULL)
442
0
        return 0;
443
444
0
    if (!ossl_property_write_lock(store))
445
0
        return 0;
446
0
    ossl_method_cache_flush(store, nid);
447
0
    alg = ossl_method_store_retrieve(store, nid);
448
0
    if (alg == NULL) {
449
0
        ossl_property_unlock(store);
450
0
        return 0;
451
0
    }
452
453
    /*
454
     * A sorting find then a delete could be faster but these stacks should be
455
     * relatively small, so we avoid the overhead.  Sorting could also surprise
456
     * users when result orderings change (even though they are not guaranteed).
457
     */
458
0
    for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
459
0
        IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
460
461
0
        if (impl->method.method == method) {
462
0
            impl_free(impl);
463
0
            (void)sk_IMPLEMENTATION_delete(alg->impls, i);
464
0
            ossl_property_unlock(store);
465
0
            return 1;
466
0
        }
467
0
    }
468
0
    ossl_property_unlock(store);
469
0
    return 0;
470
0
}
471
472
struct alg_cleanup_by_provider_data_st {
473
    OSSL_METHOD_STORE *store;
474
    const OSSL_PROVIDER *prov;
475
};
476
477
/**
478
 * @brief Cleans up implementations of an algorithm associated with a provider.
479
 *
480
 * This function removes all implementations of a specified algorithm that are
481
 * associated with a given provider. The function walks through the stack of
482
 * implementations backwards to handle deletions without affecting indexing.
483
 *
484
 * @param idx Index of the algorithm (unused in this function).
485
 * @param alg Pointer to the ALGORITHM structure containing the implementations.
486
 * @param arg Pointer to the data containing the provider information.
487
 *
488
 * If tracing is enabled, messages are printed indicating the removal of each
489
 * implementation and its properties. If any implementation is removed, the
490
 * associated cache is flushed.
491
 */
492
static void
493
alg_cleanup_by_provider(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
494
0
{
495
0
    struct alg_cleanup_by_provider_data_st *data = arg;
496
0
    int i, count;
497
498
    /*
499
     * We walk the stack backwards, to avoid having to deal with stack shifts
500
     * caused by deletion
501
     */
502
0
    for (count = 0, i = sk_IMPLEMENTATION_num(alg->impls); i-- > 0;) {
503
0
        IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
504
505
0
        if (impl->provider == data->prov) {
506
0
#ifndef FIPS_MODULE
507
0
            OSSL_TRACE_BEGIN(QUERY)
508
0
            {
509
0
                char buf[512];
510
0
                size_t size;
511
512
0
                size = ossl_property_list_to_string(NULL, impl->properties, buf,
513
0
                    sizeof(buf));
514
0
                BIO_printf(trc_out, "Removing implementation from "
515
0
                                    "query cache\nproperties %s\nprovider %s\n",
516
0
                    size == 0 ? "none" : buf,
517
0
                    ossl_provider_name(impl->provider) == NULL ? "none" : ossl_provider_name(impl->provider));
518
0
            }
519
0
            OSSL_TRACE_END(QUERY);
520
0
#endif
521
522
0
            (void)sk_IMPLEMENTATION_delete(alg->impls, i);
523
0
            count++;
524
0
            impl_free(impl);
525
0
        }
526
0
    }
527
528
    /*
529
     * If we removed any implementation, we also clear the whole associated
530
     * cache, 'cause that's the sensible thing to do.
531
     * There's no point flushing the cache entries where we didn't remove
532
     * any implementation, though.
533
     */
534
0
    if (count > 0)
535
0
        ossl_method_cache_flush_alg(data->store, alg);
536
0
}
537
538
int ossl_method_store_remove_all_provided(OSSL_METHOD_STORE *store,
539
    const OSSL_PROVIDER *prov)
540
0
{
541
0
    struct alg_cleanup_by_provider_data_st data;
542
543
0
    if (!ossl_property_write_lock(store))
544
0
        return 0;
545
0
    data.prov = prov;
546
0
    data.store = store;
547
0
    ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup_by_provider, &data);
548
0
    ossl_property_unlock(store);
549
0
    return 1;
550
0
}
551
552
static void alg_do_one(ALGORITHM *alg, IMPLEMENTATION *impl,
553
    void (*fn)(int id, void *method, void *fnarg),
554
    void *fnarg)
555
82.8M
{
556
82.8M
    fn(alg->nid, impl->method.method, fnarg);
557
82.8M
}
558
559
static void alg_copy(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
560
37.9M
{
561
37.9M
    STACK_OF(ALGORITHM) *newalg = arg;
562
563
37.9M
    alg = OPENSSL_memdup(alg, sizeof(ALGORITHM));
564
37.9M
    if (alg == NULL)
565
0
        return;
566
567
37.9M
    alg->impls = sk_IMPLEMENTATION_dup(alg->impls);
568
569
37.9M
    (void)sk_ALGORITHM_push(newalg, alg);
570
37.9M
}
571
572
static void del_tmpalg(ALGORITHM *alg)
573
37.9M
{
574
37.9M
    sk_IMPLEMENTATION_free(alg->impls);
575
37.9M
    OPENSSL_free(alg);
576
37.9M
}
577
578
void ossl_method_store_do_all(OSSL_METHOD_STORE *store,
579
    void (*fn)(int id, void *method, void *fnarg),
580
    void *fnarg)
581
946k
{
582
946k
    int i, j;
583
946k
    int numalgs, numimps;
584
946k
    STACK_OF(ALGORITHM) *tmpalgs;
585
946k
    ALGORITHM *alg;
586
587
946k
    if (store != NULL) {
588
589
946k
        if (!ossl_property_read_lock(store))
590
0
            return;
591
592
946k
        tmpalgs = sk_ALGORITHM_new_reserve(NULL,
593
946k
            (int)ossl_sa_ALGORITHM_num(store->algs));
594
946k
        if (tmpalgs == NULL) {
595
0
            ossl_property_unlock(store);
596
0
            return;
597
0
        }
598
599
946k
        ossl_sa_ALGORITHM_doall_arg(store->algs, alg_copy, tmpalgs);
600
946k
        ossl_property_unlock(store);
601
946k
        numalgs = sk_ALGORITHM_num(tmpalgs);
602
37.2M
        for (i = 0; i < numalgs; i++) {
603
36.3M
            alg = sk_ALGORITHM_value(tmpalgs, i);
604
36.3M
            numimps = sk_IMPLEMENTATION_num(alg->impls);
605
110M
            for (j = 0; j < numimps; j++)
606
74.5M
                alg_do_one(alg, sk_IMPLEMENTATION_value(alg->impls, j), fn, fnarg);
607
36.3M
        }
608
946k
        sk_ALGORITHM_pop_free(tmpalgs, del_tmpalg);
609
946k
    }
610
946k
}
611
612
/**
613
 * @brief Fetches a method from the method store matching the given properties.
614
 *
615
 * This function searches the method store for an implementation of a specified
616
 * method, identified by its id (nid), and matching the given property query. If
617
 * successful, it returns the method and its associated provider.
618
 *
619
 * @param store Pointer to the OSSL_METHOD_STORE from which to fetch the method.
620
 *              Must be non-null.
621
 * @param nid (identifier) of the method to be fetched. Must be > 0
622
 * @param prop_query String containing the property query to match against.
623
 * @param prov_rw Pointer to the OSSL_PROVIDER to restrict the search to, or
624
 *                to receive the matched provider.
625
 * @param method Pointer to receive the fetched method. Must be non-null.
626
 *
627
 * @return 1 if the method is successfully fetched, 0 on failure.
628
 *
629
 * If tracing is enabled, a message is printed indicating the property query and
630
 * the resolved provider.
631
 *
632
 * NOTE: The nid parameter here is _not_ a NID in the sense of the NID_* macros.
633
 * It is a unique internal identifier value.
634
 */
635
int ossl_method_store_fetch(OSSL_METHOD_STORE *store,
636
    int nid, const char *prop_query,
637
    const OSSL_PROVIDER **prov_rw, void **method)
638
459k
{
639
459k
    OSSL_PROPERTY_LIST **plp;
640
459k
    ALGORITHM *alg;
641
459k
    IMPLEMENTATION *impl, *best_impl = NULL;
642
459k
    OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;
643
459k
    const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL;
644
459k
    int ret = 0;
645
459k
    int j, best = -1, score, optional;
646
647
459k
    if (nid <= 0 || method == NULL || store == NULL)
648
0
        return 0;
649
650
459k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
651
459k
    if (ossl_lib_ctx_is_default(store->ctx)
652
455k
        && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
653
0
        return 0;
654
459k
#endif
655
656
    /* This only needs to be a read lock, because the query won't create anything */
657
459k
    if (!ossl_property_read_lock(store))
658
0
        return 0;
659
660
459k
    OSSL_TRACE2(QUERY, "Retrieving by nid %d from store %p\n", nid, (void *)store);
661
459k
    alg = ossl_method_store_retrieve(store, nid);
662
459k
    if (alg == NULL) {
663
453k
        ossl_property_unlock(store);
664
453k
        OSSL_TRACE2(QUERY, "Failed to retrieve by nid %d from store %p\n", nid, (void *)store);
665
453k
        return 0;
666
453k
    }
667
5.82k
    OSSL_TRACE2(QUERY, "Retrieved by nid %d from store %p\n", nid, (void *)store);
668
669
    /*
670
     * If a property query string is provided, convert it to an
671
     * OSSL_PROPERTY_LIST structure
672
     */
673
5.82k
    if (prop_query != NULL)
674
5.82k
        p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);
675
676
    /*
677
     * If the library context has default properties specified
678
     * then merge those with the properties passed to this function
679
     */
680
5.82k
    plp = ossl_ctx_global_properties(store->ctx, 0);
681
5.82k
    if (plp != NULL && *plp != NULL) {
682
0
        if (pq == NULL) {
683
0
            pq = *plp;
684
0
        } else {
685
0
            p2 = ossl_property_merge(pq, *plp);
686
0
            ossl_property_free(pq);
687
0
            if (p2 == NULL)
688
0
                goto fin;
689
0
            pq = p2;
690
0
        }
691
0
    }
692
693
    /*
694
     * Search for a provider that provides this implementation.
695
     * If the requested provider is NULL, then any provider will do,
696
     * otherwise we should try to find the one that matches the requested
697
     * provider.  Note that providers are given implicit preference via the
698
     * ordering of the implementation stack
699
     */
700
5.82k
    if (pq == NULL) {
701
3.71k
        for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
702
3.71k
            if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
703
3.71k
                && (prov == NULL || impl->provider == prov)) {
704
3.71k
                best_impl = impl;
705
3.71k
                ret = 1;
706
3.71k
                break;
707
3.71k
            }
708
3.71k
        }
709
3.71k
        goto fin;
710
3.71k
    }
711
712
    /*
713
     * If there are optional properties specified
714
     * then run the search again, and select the provider that matches the
715
     * most options
716
     */
717
2.10k
    optional = ossl_property_has_optional(pq);
718
2.40k
    for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
719
2.10k
        if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL
720
2.10k
            && (prov == NULL || impl->provider == prov)) {
721
2.10k
            score = ossl_property_match_count(pq, impl->properties);
722
2.10k
            if (score > best) {
723
1.94k
                best_impl = impl;
724
1.94k
                best = score;
725
1.94k
                ret = 1;
726
1.94k
                if (!optional)
727
1.81k
                    goto fin;
728
1.94k
            }
729
2.10k
        }
730
2.10k
    }
731
5.82k
fin:
732
5.82k
    if (ret && ossl_method_up_ref(&best_impl->method)) {
733
5.66k
        *method = best_impl->method.method;
734
5.66k
        if (prov_rw != NULL)
735
5.66k
            *prov_rw = best_impl->provider;
736
5.66k
    } else {
737
163
        ret = 0;
738
163
    }
739
740
5.82k
#ifndef FIPS_MODULE
741
5.82k
    OSSL_TRACE_BEGIN(QUERY)
742
0
    {
743
0
        char buf[512];
744
0
        size_t size;
745
746
0
        size = ossl_property_list_to_string(NULL, pq, buf, 512);
747
0
        BIO_printf(trc_out, "method store query with properties %s "
748
0
                            "resolves to provider %s\n",
749
0
            size == 0 ? "none" : buf,
750
0
            best_impl == NULL ? "none" : ossl_provider_name(best_impl->provider));
751
0
    }
752
5.82k
    OSSL_TRACE_END(QUERY);
753
5.82k
#endif
754
755
5.82k
    ossl_property_unlock(store);
756
5.82k
    ossl_property_free(p2);
757
5.82k
    return ret;
758
2.10k
}
759
760
static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store,
761
    ALGORITHM *alg)
762
5.01k
{
763
5.01k
    store->cache_nelem -= lh_QUERY_num_items(alg->cache);
764
5.01k
    impl_cache_flush_alg(0, alg);
765
5.01k
}
766
767
static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
768
24.5k
{
769
24.5k
    ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
770
771
24.5k
    if (alg != NULL)
772
5.01k
        ossl_method_cache_flush_alg(store, alg);
773
24.5k
}
774
775
int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store)
776
440
{
777
440
    if (!ossl_property_write_lock(store))
778
0
        return 0;
779
440
    ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
780
440
    store->cache_nelem = 0;
781
440
    ossl_property_unlock(store);
782
440
    return 1;
783
440
}
784
785
IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
786
787
/*
788
 * Flush an element from the query cache (perhaps).
789
 *
790
 * In order to avoid taking a write lock or using atomic operations
791
 * to keep accurate least recently used (LRU) or least frequently used
792
 * (LFU) information, the procedure used here is to stochastically
793
 * flush approximately half the cache.
794
 *
795
 * This procedure isn't ideal, LRU or LFU would be better.  However,
796
 * in normal operation, reaching a full cache would be unexpected.
797
 * It means that no steady state of algorithm queries has been reached.
798
 * That is, it is most likely an attack of some form.  A suboptimal clearance
799
 * strategy that doesn't degrade performance of the normal case is
800
 * preferable to a more refined approach that imposes a performance
801
 * impact.
802
 */
803
static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
804
9.17k
{
805
9.17k
    uint32_t n;
806
807
    /*
808
     * Implement the 32 bit xorshift as suggested by George Marsaglia in:
809
     *      https://doi.org/10.18637/jss.v008.i14
810
     *
811
     * This is a very fast PRNG so there is no need to extract bits one at a
812
     * time and use the entire value each time.
813
     */
814
9.17k
    n = state->seed;
815
9.17k
    n ^= n << 13;
816
9.17k
    n ^= n >> 17;
817
9.17k
    n ^= n << 5;
818
9.17k
    state->seed = n;
819
820
9.17k
    if ((n & 1) != 0)
821
4.54k
        impl_cache_free(lh_QUERY_delete(state->cache, c));
822
4.62k
    else
823
4.62k
        state->nelem++;
824
9.17k
}
825
826
static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
827
    void *v)
828
4.53k
{
829
4.53k
    IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
830
4.53k
    unsigned long orig_down_load = lh_QUERY_get_down_load(alg->cache);
831
832
4.53k
    state->cache = alg->cache;
833
4.53k
    lh_QUERY_set_down_load(alg->cache, 0);
834
4.53k
    lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
835
4.53k
        state);
836
4.53k
    lh_QUERY_set_down_load(alg->cache, orig_down_load);
837
4.53k
}
838
839
static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
840
31
{
841
31
    IMPL_CACHE_FLUSH state;
842
31
    static TSAN_QUALIFIER uint32_t global_seed = 1;
843
844
31
    state.nelem = 0;
845
31
    state.using_global_seed = 0;
846
31
    if ((state.seed = OPENSSL_rdtsc()) == 0) {
847
        /* If there is no timer available, seed another way */
848
0
        state.using_global_seed = 1;
849
0
        state.seed = tsan_load(&global_seed);
850
0
    }
851
31
    store->cache_need_flush = 0;
852
31
    ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
853
31
    store->cache_nelem = state.nelem;
854
    /* Without a timer, update the global seed */
855
31
    if (state.using_global_seed)
856
31
        tsan_add(&global_seed, state.seed);
857
31
}
858
859
int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
860
    int nid, const char *prop_query, void **method)
861
25.4M
{
862
25.4M
    ALGORITHM *alg;
863
25.4M
    QUERY elem, *r;
864
25.4M
    int res = 0;
865
866
25.4M
    if (nid <= 0 || store == NULL || prop_query == NULL)
867
0
        return 0;
868
869
25.4M
    if (!ossl_property_read_lock(store))
870
0
        return 0;
871
25.4M
    alg = ossl_method_store_retrieve(store, nid);
872
25.4M
    if (alg == NULL)
873
818k
        goto err;
874
875
24.6M
    elem.query = prop_query;
876
24.6M
    elem.provider = prov;
877
24.6M
    r = lh_QUERY_retrieve(alg->cache, &elem);
878
24.6M
    if (r == NULL)
879
7.88k
        goto err;
880
24.6M
    if (ossl_method_up_ref(&r->method)) {
881
24.6M
        *method = r->method.method;
882
24.6M
        res = 1;
883
24.6M
    }
884
25.4M
err:
885
25.4M
    ossl_property_unlock(store);
886
25.4M
    return res;
887
24.6M
}
888
889
int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
890
    int nid, const char *prop_query, void *method,
891
    int (*method_up_ref)(void *),
892
    void (*method_destruct)(void *))
893
8.05k
{
894
8.05k
    QUERY elem, *old, *p = NULL;
895
8.05k
    ALGORITHM *alg;
896
8.05k
    size_t len;
897
8.05k
    int res = 1;
898
899
8.05k
    if (nid <= 0 || store == NULL || prop_query == NULL)
900
0
        return 0;
901
902
8.05k
    if (!ossl_assert(prov != NULL))
903
0
        return 0;
904
905
8.05k
    if (!ossl_property_write_lock(store))
906
0
        return 0;
907
8.05k
    if (store->cache_need_flush)
908
31
        ossl_method_cache_flush_some(store);
909
8.05k
    alg = ossl_method_store_retrieve(store, nid);
910
8.05k
    if (alg == NULL)
911
0
        goto err;
912
913
8.05k
    if (method == NULL) {
914
0
        elem.query = prop_query;
915
0
        elem.provider = prov;
916
0
        if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) {
917
0
            impl_cache_free(old);
918
0
            store->cache_nelem--;
919
0
        }
920
0
        goto end;
921
0
    }
922
8.05k
    p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
923
8.05k
    if (p != NULL) {
924
8.05k
        p->query = p->body;
925
8.05k
        p->provider = prov;
926
8.05k
        p->method.method = method;
927
8.05k
        p->method.up_ref = method_up_ref;
928
8.05k
        p->method.free = method_destruct;
929
8.05k
        if (!ossl_method_up_ref(&p->method))
930
0
            goto err;
931
8.05k
        memcpy((char *)p->query, prop_query, len + 1);
932
8.05k
        if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
933
0
            impl_cache_free(old);
934
0
            goto end;
935
0
        }
936
8.05k
        if (!lh_QUERY_error(alg->cache)) {
937
8.05k
            if (++store->cache_nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
938
31
                store->cache_need_flush = 1;
939
8.05k
            goto end;
940
8.05k
        }
941
0
        ossl_method_free(&p->method);
942
0
    }
943
0
err:
944
0
    res = 0;
945
0
    OPENSSL_free(p);
946
8.05k
end:
947
8.05k
    ossl_property_unlock(store);
948
8.05k
    return res;
949
0
}