Coverage Report

Created: 2026-10-16 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/property/property.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2026 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 <openssl/provider.h>
16
#include "internal/property.h"
17
#include "internal/provider.h"
18
#include "internal/tsan_assist.h"
19
#include "internal/threads_common.h"
20
#include "internal/list.h"
21
#include "internal/time.h"
22
#include <openssl/lhash.h>
23
#include <openssl/rand.h>
24
#include <openssl/trace.h>
25
#include "crypto/sparse_array.h"
26
#include "property_local.h"
27
#include "crypto/context.h"
28
29
/*
30
 * The shard count was determined through performance testing with the evp_fetch
31
 * tool on an Intel Xeon Gold 6248R CPU @ 3.00GHz. Testing showed that 4 shards
32
 * delivered the best performance for 16 or
33
 * more threads, and close to best performance at below 16 threads.
34
 */
35
#ifndef NUM_SHARDS_BITS
36
1.65M
#define NUM_SHARDS_BITS 2
37
#endif
38
830k
#define NUM_SHARDS (1 << NUM_SHARDS_BITS)
39
40
#ifndef MAX_CACHE_LINES_BITS
41
830k
#define MAX_CACHE_LINES_BITS 3
42
#endif
43
830k
#define MAX_CACHE_LINES (1 << MAX_CACHE_LINES_BITS)
44
45
typedef struct {
46
    void *method;
47
    int (*up_ref)(void *);
48
    void (*free)(void *);
49
} METHOD;
50
51
typedef struct {
52
    int archived;
53
    const OSSL_PROVIDER *provider;
54
    OSSL_PROPERTY_LIST *properties;
55
    METHOD method;
56
} IMPLEMENTATION;
57
58
DEFINE_STACK_OF(IMPLEMENTATION)
59
60
typedef struct query_st {
61
    struct query_st *next; /* list pointer for lookup table */
62
    void *saptr; /* pointer to our owning STORED_ALGORITHM */
63
    int nid; /* nid of this query */
64
    int archived; /* Mark entry as no longer findable */
65
    OSSL_PROVIDER *prov; /*provider this belongs to */
66
    char *prop_query; /* query string */
67
    METHOD method; /* METHOD for this query */
68
} QUERY;
69
70
typedef struct {
71
    int nid;
72
    STACK_OF(IMPLEMENTATION) *impls;
73
} ALGORITHM;
74
75
typedef struct {
76
    SPARSE_ARRAY_OF(ALGORITHM) *algs;
77
78
    QUERY *cache_lists[MAX_CACHE_LINES];
79
    QUERY *archive;
80
81
    /*
82
     * Lock to protect each shard of |algs| from concurrent writing,
83
     * when individual implementations or queries are inserted.  This is used
84
     * by the appropriate functions here.
85
     */
86
    CRYPTO_RWLOCK *lock;
87
    CRYPTO_RWLOCK *alock;
88
89
    /* query cache specific values */
90
91
} STORED_ALGORITHMS;
92
93
static int ossl_method_store_atomic_insert_to_list(STORED_ALGORITHMS *sa, QUERY *new);
94
static int ossl_method_store_atomic_archive(STORED_ALGORITHMS *sa, QUERY *old);
95
static QUERY *ossl_method_store_atomic_find_in_list(STORED_ALGORITHMS *sa, int nid,
96
    OSSL_PROVIDER *prov, const char *prop_query);
97
static void ossl_cache_lists_flush(STORED_ALGORITHMS *sa);
98
static void ossl_cache_lists_free(STORED_ALGORITHMS *sa);
99
static void ossl_method_store_atomic_clean_archive(STORED_ALGORITHMS *sa);
100
101
struct ossl_method_store_st {
102
    OSSL_LIB_CTX *ctx;
103
    STORED_ALGORITHMS *algs;
104
    /*
105
     * Lock to reserve the whole store.  This is used when fetching a set
106
     * of algorithms, via these functions, found in crypto/core_fetch.c:
107
     * ossl_method_construct_reserve_store()
108
     * ossl_method_construct_unreserve_store()
109
     */
110
    CRYPTO_RWLOCK *biglock;
111
};
112
113
DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
114
115
DEFINE_STACK_OF(ALGORITHM)
116
117
typedef struct ossl_global_properties_st {
118
    OSSL_PROPERTY_LIST *list;
119
#ifndef FIPS_MODULE
120
    unsigned int no_mirrored : 1;
121
#endif
122
} OSSL_GLOBAL_PROPERTIES;
123
124
829k
#define stored_algs_shard(store, nid) (&(store)->algs[(nid) & (NUM_SHARDS - 1)])
125
126
static void ossl_method_cache_flush_alg(STORED_ALGORITHMS *sa,
127
    ALGORITHM *alg);
128
static void ossl_method_cache_flush(STORED_ALGORITHMS *sa, int nid);
129
130
static ossl_inline QUERY *QUERY_new(size_t prop_query_len)
131
50
{
132
    /*
133
     * allocate a new QUERY with the associated property query buffer
134
     * immediately following it
135
     */
136
50
    QUERY *new = OPENSSL_malloc(sizeof(QUERY) + prop_query_len + 1);
137
50
    if (new != NULL)
138
50
        new->prop_query = (char *)(new + 1);
139
50
    return new;
140
50
}
141
142
static ossl_inline void QUERY_free(QUERY *q)
143
0
{
144
    /*
145
     * because we allocate the QUERY with its property query string
146
     * as one contiguous chunk, this frees both
147
     */
148
0
    OPENSSL_free(q);
149
0
}
150
151
/* Global properties are stored per library context */
152
void ossl_ctx_global_properties_free(void *vglobp)
153
0
{
154
0
    OSSL_GLOBAL_PROPERTIES *globp = vglobp;
155
156
0
    if (globp != NULL) {
157
0
        ossl_property_free(globp->list);
158
0
        OPENSSL_free(globp);
159
0
    }
160
0
}
161
162
void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx)
163
3
{
164
3
    return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES));
165
3
}
166
167
OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx,
168
    ossl_unused int loadconfig)
169
165
{
170
165
    OSSL_GLOBAL_PROPERTIES *globp;
171
172
165
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
173
165
    if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
174
0
        return NULL;
175
165
#endif
176
165
    globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
177
178
165
    return globp != NULL ? &globp->list : NULL;
179
165
}
180
181
#ifndef FIPS_MODULE
182
int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx)
183
1
{
184
1
    OSSL_GLOBAL_PROPERTIES *globp
185
1
        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
186
187
1
    return globp != NULL && globp->no_mirrored ? 1 : 0;
188
1
}
189
190
void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx)
191
0
{
192
0
    OSSL_GLOBAL_PROPERTIES *globp
193
0
        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES);
194
195
0
    if (globp != NULL)
196
0
        globp->no_mirrored = 1;
197
0
}
198
#endif
199
200
static int ossl_method_up_ref(METHOD *method)
201
84
{
202
84
    return (*method->up_ref)(method->method);
203
84
}
204
205
static void ossl_method_free(METHOD *method)
206
0
{
207
0
    (*method->free)(method->method);
208
0
}
209
210
static __owur int ossl_property_read_lock(STORED_ALGORITHMS *p)
211
171
{
212
171
    return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
213
171
}
214
215
static __owur int ossl_property_write_lock(STORED_ALGORITHMS *p)
216
102
{
217
102
    return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
218
102
}
219
220
static int ossl_property_unlock(STORED_ALGORITHMS *p)
221
273
{
222
273
    return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
223
273
}
224
225
static void impl_free(IMPLEMENTATION *impl)
226
0
{
227
0
    if (impl != NULL) {
228
0
        ossl_method_free(&impl->method);
229
0
        OPENSSL_free(impl);
230
0
    }
231
0
}
232
233
static ossl_inline void impl_cache_free_unlinked(QUERY *elem)
234
0
{
235
0
    if (elem != NULL) {
236
0
        ossl_method_free(&elem->method);
237
0
        QUERY_free(elem);
238
0
    }
239
0
}
240
241
static void impl_cache_flush_alg(ALGORITHM *alg, STORED_ALGORITHMS *sa)
242
1
{
243
1
    QUERY *q;
244
1
    int i;
245
246
    /*
247
     * Instead of iterating over the hashtable with the
248
     * ossl_ht_foreach_until function, we just traverse the
249
     * linked list, as it much faster this way, as we avoid having
250
     * to visit lots of potentially empty nodes
251
     */
252
9
    for (i = 0; i < MAX_CACHE_LINES; i++) {
253
8
        if (!CRYPTO_atomic_load_ptr((void **)&sa->cache_lists[i], (void **)&q, sa->alock))
254
0
            return;
255
8
        while (q != NULL) {
256
0
            if (q->nid == alg->nid)
257
0
                ossl_method_store_atomic_archive(sa, q);
258
0
            if (!CRYPTO_atomic_load_ptr((void **)&q->next, (void **)&q, sa->alock))
259
0
                return;
260
0
        }
261
8
    }
262
1
}
263
264
static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a, void *arg)
265
0
{
266
0
    STORED_ALGORITHMS *sa = arg;
267
268
0
    if (a != NULL) {
269
0
        sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
270
0
        OPENSSL_free(a);
271
0
    }
272
0
    if (sa != NULL)
273
0
        ossl_sa_ALGORITHM_set(sa->algs, idx, NULL);
274
0
}
275
276
static void stored_algs_free(STORED_ALGORITHMS *sa)
277
0
{
278
0
    if (sa == NULL)
279
0
        return;
280
281
0
    for (int i = 0; i < NUM_SHARDS; ++i) {
282
0
        ossl_sa_ALGORITHM_doall_arg(sa[i].algs, &alg_cleanup, &sa[i]);
283
0
        ossl_sa_ALGORITHM_free(sa[i].algs);
284
0
        ossl_cache_lists_free(&sa[i]);
285
0
        CRYPTO_THREAD_lock_free(sa[i].lock);
286
0
        CRYPTO_THREAD_lock_free(sa[i].alock);
287
0
    }
288
289
0
    OPENSSL_free(sa);
290
0
}
291
292
static STORED_ALGORITHMS *stored_algs_new(OSSL_LIB_CTX *ctx)
293
12
{
294
12
    STORED_ALGORITHMS *ret;
295
296
12
    ret = OPENSSL_calloc(NUM_SHARDS, sizeof(STORED_ALGORITHMS));
297
12
    if (ret == NULL)
298
0
        return NULL;
299
300
60
    for (int i = 0; i < NUM_SHARDS; ++i) {
301
48
        ret[i].algs = ossl_sa_ALGORITHM_new();
302
48
        if (ret[i].algs == NULL)
303
0
            goto err;
304
305
48
        ret[i].lock = CRYPTO_THREAD_lock_new();
306
48
        if (ret[i].lock == NULL)
307
0
            goto err;
308
48
        ret[i].alock = CRYPTO_THREAD_lock_new();
309
48
        if (ret[i].alock == NULL)
310
0
            goto err;
311
48
    }
312
313
12
    return ret;
314
315
0
err:
316
0
    stored_algs_free(ret);
317
318
0
    return NULL;
319
12
}
320
321
/*
322
 * The OSSL_LIB_CTX param here allows access to underlying property data needed
323
 * for computation
324
 */
325
OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx)
326
12
{
327
12
    OSSL_METHOD_STORE *res;
328
329
12
    res = OPENSSL_zalloc(sizeof(*res));
330
12
    if (res != NULL) {
331
12
        res->ctx = ctx;
332
12
        if ((res->algs = stored_algs_new(ctx)) == NULL
333
12
            || (res->biglock = CRYPTO_THREAD_lock_new()) == NULL) {
334
0
            ossl_method_store_free(res);
335
0
            return NULL;
336
0
        }
337
12
    }
338
12
    return res;
339
12
}
340
341
void ossl_method_store_free(OSSL_METHOD_STORE *store)
342
0
{
343
0
    if (store == NULL)
344
0
        return;
345
346
0
    stored_algs_free(store->algs);
347
0
    CRYPTO_THREAD_lock_free(store->biglock);
348
0
    OPENSSL_free(store);
349
0
}
350
351
int ossl_method_lock_store(OSSL_METHOD_STORE *store)
352
934
{
353
934
    return store != NULL ? CRYPTO_THREAD_write_lock(store->biglock) : 0;
354
934
}
355
356
int ossl_method_unlock_store(OSSL_METHOD_STORE *store)
357
934
{
358
934
    return store != NULL ? CRYPTO_THREAD_unlock(store->biglock) : 0;
359
934
}
360
361
static ALGORITHM *ossl_method_store_retrieve(STORED_ALGORITHMS *sa, int nid)
362
239
{
363
239
    return ossl_sa_ALGORITHM_get(sa->algs, nid);
364
239
}
365
366
static int ossl_method_store_insert(STORED_ALGORITHMS *sa, ALGORITHM *alg)
367
33
{
368
33
    return ossl_sa_ALGORITHM_set(sa->algs, alg->nid, alg);
369
33
}
370
371
/**
372
 * @brief Adds a method to the specified method store.
373
 *
374
 * This function adds a new method to the provided method store, associating it
375
 * with a specified id, properties, and provider. The method is stored with
376
 * reference count and destruction callbacks.
377
 *
378
 * @param store Pointer to the OSSL_METHOD_STORE where the method will be added.
379
 *              Must be non-null.
380
 * @param prov Pointer to the OSSL_PROVIDER for the provider of the method.
381
 *             Must be non-null.
382
 * @param nid (identifier) associated with the method, must be > 0
383
 * @param properties String containing properties of the method.
384
 * @param method Pointer to the method to be added.
385
 * @param method_up_ref Function pointer for incrementing the method ref count.
386
 * @param method_destruct Function pointer for destroying the method.
387
 *
388
 * @return 1 if the method is successfully added, 0 on failure.
389
 *
390
 * If tracing is enabled, a message is printed indicating that the method is
391
 * being added to the method store.
392
 *
393
 * NOTE: The nid parameter here is _not_ a nid in the sense of the NID_* macros.
394
 * It is an internal unique identifier.
395
 */
396
int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
397
    int nid, const char *properties, void *method,
398
    int (*method_up_ref)(void *),
399
    void (*method_destruct)(void *))
400
34
{
401
34
    STORED_ALGORITHMS *sa;
402
34
    ALGORITHM *alg = NULL;
403
34
    IMPLEMENTATION *impl;
404
34
    int ret = 0;
405
34
    int i;
406
407
34
    if (nid <= 0 || method == NULL || store == NULL)
408
0
        return 0;
409
410
34
    if (properties == NULL)
411
0
        properties = "";
412
413
34
    if (!ossl_assert(prov != NULL))
414
0
        return 0;
415
416
    /* Create new entry */
417
34
    impl = OPENSSL_malloc(sizeof(*impl));
418
34
    if (impl == NULL)
419
0
        return 0;
420
34
    impl->archived = 0;
421
34
    impl->method.method = method;
422
34
    impl->method.up_ref = method_up_ref;
423
34
    impl->method.free = method_destruct;
424
34
    if (!ossl_method_up_ref(&impl->method)) {
425
0
        OPENSSL_free(impl);
426
0
        return 0;
427
0
    }
428
34
    impl->provider = prov;
429
430
34
    sa = stored_algs_shard(store, nid);
431
432
    /* Insert into the hash table if required */
433
34
    if (!ossl_property_write_lock(sa)) {
434
0
        impl_free(impl);
435
0
        return 0;
436
0
    }
437
438
    /*
439
     * Flush the alg cache of any implementation that already exists
440
     * for this id.
441
     * This is done to ensure that on the next lookup we go through the
442
     * provider comparison in ossl_method_store_fetch.  If we don't do this
443
     * then this new method won't be given a chance to get selected.
444
     * NOTE: This doesn't actually remove the method from the backing store
445
     * It just ensures that we query the backing store when (re)-adding a
446
     * method to the algorithm cache, in case the one selected by the next
447
     * query selects a different implementation
448
     */
449
34
    ossl_method_cache_flush(sa, nid);
450
451
    /*
452
     * Parse the properties associated with this method, and convert it to a
453
     * property list stored against the implementation for later comparison
454
     * during fetch operations
455
     */
456
34
    if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
457
2
        impl->properties = ossl_parse_property(store->ctx, properties);
458
2
        if (impl->properties == NULL)
459
0
            goto err;
460
2
        if (!ossl_prop_defn_set(store->ctx, properties, &impl->properties)) {
461
0
            ossl_property_free(impl->properties);
462
0
            impl->properties = NULL;
463
0
            goto err;
464
0
        }
465
2
    }
466
467
    /*
468
     * Check if we have an algorithm cache already for this nid.  If so use
469
     * it, otherwise, create it, and insert it into the store
470
     */
471
34
    alg = ossl_method_store_retrieve(sa, nid);
472
34
    if (alg == NULL) {
473
33
        if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
474
33
            || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL)
475
0
            goto err;
476
33
        alg->nid = nid;
477
33
        if (!ossl_method_store_insert(sa, alg))
478
0
            goto err;
479
33
        OSSL_TRACE2(QUERY, "Inserted an alg with nid %d into the stored algorithms %p\n",
480
33
            nid, (void *)sa);
481
33
    }
482
483
    /* Push onto stack if there isn't one there already */
484
35
    for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
485
1
        const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);
486
487
1
        if (tmpimpl->archived == 1)
488
0
            continue;
489
490
1
        if (tmpimpl->provider == impl->provider
491
0
            && tmpimpl->properties == impl->properties)
492
0
            break;
493
1
    }
494
495
34
    if (i == sk_IMPLEMENTATION_num(alg->impls)
496
34
        && sk_IMPLEMENTATION_push(alg->impls, impl)) {
497
34
        ret = 1;
498
34
#ifndef FIPS_MODULE
499
34
        OSSL_TRACE_BEGIN(QUERY)
500
0
        {
501
0
            BIO_printf(trc_out, "Adding to method store "
502
0
                                "nid: %d\nproperties: %s\nprovider: %s\n",
503
0
                nid, properties,
504
0
                ossl_provider_name(prov) == NULL ? "none" : ossl_provider_name(prov));
505
0
        }
506
34
        OSSL_TRACE_END(QUERY);
507
34
#endif
508
34
    }
509
34
    ossl_property_unlock(sa);
510
34
    if (ret == 0)
511
0
        impl_free(impl);
512
34
    return ret;
513
514
0
err:
515
0
    ossl_property_unlock(sa);
516
0
    alg_cleanup(0, alg, NULL);
517
0
    impl_free(impl);
518
0
    return 0;
519
34
}
520
521
int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
522
    const void *method)
523
0
{
524
0
    ALGORITHM *alg = NULL;
525
0
    STORED_ALGORITHMS *sa;
526
0
    int i;
527
528
0
    if (nid <= 0 || method == NULL || store == NULL)
529
0
        return 0;
530
531
0
    sa = stored_algs_shard(store, nid);
532
0
    if (!ossl_property_write_lock(sa))
533
0
        return 0;
534
0
    ossl_method_cache_flush(sa, nid);
535
0
    ossl_method_store_atomic_clean_archive(sa);
536
0
    alg = ossl_method_store_retrieve(sa, nid);
537
0
    if (alg == NULL) {
538
0
        ossl_property_unlock(sa);
539
0
        return 0;
540
0
    }
541
542
    /*
543
     * A sorting find then a delete could be faster but these stacks should be
544
     * relatively small, so we avoid the overhead.  Sorting could also surprise
545
     * users when result orderings change (even though they are not guaranteed).
546
     */
547
0
    for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
548
0
        IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
549
550
0
        if (impl->method.method == method) {
551
0
            impl_free(impl);
552
0
            (void)sk_IMPLEMENTATION_delete(alg->impls, i);
553
0
            ossl_property_unlock(sa);
554
0
            return 1;
555
0
        }
556
0
    }
557
0
    ossl_property_unlock(sa);
558
0
    return 0;
559
0
}
560
561
struct alg_cleanup_by_provider_data_st {
562
    STORED_ALGORITHMS *sa;
563
    const OSSL_PROVIDER *prov;
564
};
565
566
/**
567
 * @brief Cleans up implementations of an algorithm associated with a provider.
568
 *
569
 * This function removes all implementations of a specified algorithm that are
570
 * associated with a given provider. The function walks through the stack of
571
 * implementations backwards to handle deletions without affecting indexing.
572
 *
573
 * @param idx Index of the algorithm (unused in this function).
574
 * @param alg Pointer to the ALGORITHM structure containing the implementations.
575
 * @param arg Pointer to the data containing the provider information.
576
 *
577
 * If tracing is enabled, messages are printed indicating the removal of each
578
 * implementation and its properties. If any implementation is removed, the
579
 * associated cache is flushed.
580
 */
581
static void
582
alg_cleanup_by_provider(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
583
0
{
584
0
    struct alg_cleanup_by_provider_data_st *data = arg;
585
0
    int i, count;
586
587
    /*
588
     * We walk the stack backwards, to avoid having to deal with stack shifts
589
     * caused by deletion
590
     */
591
0
    for (count = 0, i = sk_IMPLEMENTATION_num(alg->impls); i-- > 0;) {
592
0
        IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
593
594
0
        if (impl->provider == data->prov) {
595
0
#ifndef FIPS_MODULE
596
0
            OSSL_TRACE_BEGIN(QUERY)
597
0
            {
598
0
                char buf[512];
599
0
                size_t size;
600
601
0
                size = ossl_property_list_to_string(NULL, impl->properties, buf,
602
0
                    sizeof(buf));
603
0
                BIO_printf(trc_out, "Removing implementation from "
604
0
                                    "query cache\nproperties %s\nprovider %s\n",
605
0
                    size == 0 ? "none" : buf,
606
0
                    ossl_provider_name(impl->provider) == NULL ? "none" : ossl_provider_name(impl->provider));
607
0
            }
608
0
            OSSL_TRACE_END(QUERY);
609
0
#endif
610
0
            impl->archived = 1;
611
0
            count++;
612
0
        }
613
0
    }
614
615
    /*
616
     * If we removed any implementation, we also clear the whole associated
617
     * cache, 'cause that's the sensible thing to do.
618
     * There's no point flushing the cache entries where we didn't remove
619
     * any implementation, though.
620
     */
621
0
    if (count > 0)
622
0
        ossl_method_cache_flush_alg(data->sa, alg);
623
0
}
624
625
int ossl_method_store_remove_all_provided(OSSL_METHOD_STORE *store,
626
    const OSSL_PROVIDER *prov)
627
0
{
628
0
    struct alg_cleanup_by_provider_data_st data;
629
630
0
    for (int k = 0; k < NUM_SHARDS; ++k) {
631
0
        STORED_ALGORITHMS *sa = &store->algs[k];
632
633
0
        if (!ossl_property_write_lock(sa))
634
0
            return 0;
635
0
        data.prov = prov;
636
0
        data.sa = sa;
637
0
        ossl_sa_ALGORITHM_doall_arg(sa->algs, &alg_cleanup_by_provider, &data);
638
0
        ossl_method_store_atomic_clean_archive(sa);
639
0
        ossl_property_unlock(sa);
640
0
    }
641
0
    return 1;
642
0
}
643
644
static void alg_do_one(ALGORITHM *alg, IMPLEMENTATION *impl,
645
    void (*fn)(int id, void *method, void *fnarg),
646
    void *fnarg)
647
0
{
648
0
    fn(alg->nid, impl->method.method, fnarg);
649
0
}
650
651
static void alg_copy(ossl_uintmax_t idx, ALGORITHM *alg, void *arg)
652
0
{
653
0
    STACK_OF(ALGORITHM) *newalg = arg;
654
0
    int i;
655
0
    IMPLEMENTATION *impl;
656
657
0
    alg = OPENSSL_memdup(alg, sizeof(ALGORITHM));
658
0
    if (alg == NULL)
659
0
        return;
660
661
0
    alg->impls = sk_IMPLEMENTATION_dup(alg->impls);
662
663
    /*
664
     * Remove any archived items while we're under lock
665
     * note we don't have to free the implementation here
666
     * as its still tracked in the alg struct we're cloning from
667
     */
668
0
    for (i = sk_IMPLEMENTATION_num(alg->impls); i-- > 0;) {
669
0
        impl = sk_IMPLEMENTATION_value(alg->impls, i);
670
0
        if (impl->archived == 1)
671
0
            (void)sk_IMPLEMENTATION_delete(alg->impls, i);
672
0
    }
673
674
0
    (void)sk_ALGORITHM_push(newalg, alg);
675
0
}
676
677
static void del_tmpalg(ALGORITHM *alg)
678
0
{
679
0
    sk_IMPLEMENTATION_free(alg->impls);
680
0
    OPENSSL_free(alg);
681
0
}
682
683
void ossl_method_store_do_all(OSSL_METHOD_STORE *store,
684
    void (*fn)(int id, void *method, void *fnarg),
685
    void *fnarg)
686
0
{
687
0
    int i, j;
688
0
    int numalgs, numimps;
689
0
    STACK_OF(ALGORITHM) *tmpalgs;
690
0
    ALGORITHM *alg;
691
692
0
    if (store == NULL)
693
0
        return;
694
695
0
    for (int k = 0; k < NUM_SHARDS; ++k) {
696
0
        STORED_ALGORITHMS *sa = &store->algs[k];
697
698
0
        if (!ossl_property_read_lock(sa))
699
0
            return;
700
701
0
        tmpalgs = sk_ALGORITHM_new_reserve(NULL,
702
0
            (int)ossl_sa_ALGORITHM_num(sa->algs));
703
0
        if (tmpalgs == NULL) {
704
0
            ossl_property_unlock(sa);
705
0
            return;
706
0
        }
707
708
0
        ossl_sa_ALGORITHM_doall_arg(sa->algs, alg_copy, tmpalgs);
709
0
        ossl_property_unlock(sa);
710
0
        numalgs = sk_ALGORITHM_num(tmpalgs);
711
0
        for (i = 0; i < numalgs; i++) {
712
0
            alg = sk_ALGORITHM_value(tmpalgs, i);
713
0
            numimps = sk_IMPLEMENTATION_num(alg->impls);
714
0
            for (j = 0; j < numimps; j++)
715
0
                alg_do_one(alg, sk_IMPLEMENTATION_value(alg->impls, j), fn, fnarg);
716
0
        }
717
0
        sk_ALGORITHM_pop_free(tmpalgs, del_tmpalg);
718
0
    }
719
0
}
720
721
/**
722
 * @brief Fetches a method from the method store matching the given properties.
723
 *
724
 * This function searches the method store for an implementation of a specified
725
 * method, identified by its id (nid), and matching the given property query. If
726
 * successful, it returns the method and its associated provider.
727
 *
728
 * @param store Pointer to the OSSL_METHOD_STORE from which to fetch the method.
729
 *              Must be non-null.
730
 * @param nid (identifier) of the method to be fetched. Must be > 0
731
 * @param prop_query String containing the property query to match against.
732
 * @param prov_rw Pointer to the OSSL_PROVIDER to restrict the search to, or
733
 *                to receive the matched provider.
734
 * @param method Pointer to receive the fetched method. Must be non-null.
735
 *
736
 * @return 1 if the method is successfully fetched, 0 on failure.
737
 *
738
 * If tracing is enabled, a message is printed indicating the property query and
739
 * the resolved provider.
740
 *
741
 * NOTE: The nid parameter here is _not_ a NID in the sense of the NID_* macros.
742
 * It is a unique internal identifier value.
743
 */
744
int ossl_method_store_fetch(OSSL_METHOD_STORE *store,
745
    int nid, const char *prop_query,
746
    const OSSL_PROVIDER **prov_rw, void **method)
747
196
{
748
196
    OSSL_PROPERTY_LIST **plp;
749
196
    ALGORITHM *alg;
750
196
    IMPLEMENTATION *impl, *best_impl = NULL;
751
196
    OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL;
752
196
    const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL;
753
196
    int ret = 0;
754
196
    int j, best = -1, score, optional;
755
196
    STORED_ALGORITHMS *sa;
756
757
196
    if (nid <= 0 || method == NULL || store == NULL)
758
25
        return 0;
759
760
171
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
761
171
    if (ossl_lib_ctx_is_default(store->ctx)
762
0
        && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
763
0
        return 0;
764
171
#endif
765
766
171
    sa = stored_algs_shard(store, nid);
767
768
    /* This only needs to be a read lock, because the query won't create anything */
769
171
    if (!ossl_property_read_lock(sa))
770
0
        return 0;
771
772
171
    OSSL_TRACE2(QUERY, "Retrieving by nid %d from stored algorithms %p\n",
773
171
        nid, (void *)sa);
774
171
    alg = ossl_method_store_retrieve(sa, nid);
775
171
    if (alg == NULL) {
776
8
        ossl_property_unlock(sa);
777
8
        OSSL_TRACE2(QUERY, "Failed to retrieve by nid %d from stored algorithms %p\n",
778
8
            nid, (void *)sa);
779
8
        return 0;
780
8
    }
781
163
    OSSL_TRACE2(QUERY, "Retrieved by nid %d from stored algorithms %p\n",
782
163
        nid, (void *)sa);
783
784
    /*
785
     * If a property query string is provided, convert it to an
786
     * OSSL_PROPERTY_LIST structure
787
     */
788
163
    if (prop_query != NULL)
789
163
        p2 = pq = ossl_parse_query(store->ctx, prop_query, 0);
790
791
    /*
792
     * If the library context has default properties specified
793
     * then merge those with the properties passed to this function
794
     */
795
163
    plp = ossl_ctx_global_properties(store->ctx, 0);
796
163
    if (plp != NULL && *plp != NULL) {
797
0
        if (pq == NULL) {
798
0
            pq = *plp;
799
0
        } else {
800
0
            p2 = ossl_property_merge(pq, *plp);
801
0
            ossl_property_free(pq);
802
0
            if (p2 == NULL)
803
0
                goto fin;
804
0
            pq = p2;
805
0
        }
806
0
    }
807
808
    /*
809
     * Search for a provider that provides this implementation.
810
     * If the requested provider is NULL, then any provider will do,
811
     * otherwise we should try to find the one that matches the requested
812
     * provider.  Note that providers are given implicit preference via the
813
     * ordering of the implementation stack
814
     */
815
163
    if (pq == NULL) {
816
0
        for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
817
0
            impl = sk_IMPLEMENTATION_value(alg->impls, j);
818
0
            if (impl == NULL || impl->archived == 1)
819
0
                continue;
820
821
0
            if (prov == NULL || impl->provider == prov) {
822
0
                best_impl = impl;
823
0
                ret = 1;
824
0
                break;
825
0
            }
826
0
        }
827
0
        goto fin;
828
0
    }
829
830
    /*
831
     * If there are optional properties specified
832
     * then run the search again, and select the provider that matches the
833
     * most options
834
     */
835
163
    optional = ossl_property_has_optional(pq);
836
163
    for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
837
163
        impl = sk_IMPLEMENTATION_value(alg->impls, j);
838
163
        if (impl != NULL
839
163
            && impl->archived == 0
840
163
            && (prov == NULL || impl->provider == prov)) {
841
163
            score = ossl_property_match_count(pq, impl->properties);
842
163
            if (score > best) {
843
163
                best_impl = impl;
844
163
                best = score;
845
163
                ret = 1;
846
163
                if (!optional)
847
163
                    goto fin;
848
163
            }
849
163
        }
850
163
    }
851
163
fin:
852
163
    if (ret) {
853
163
        *method = best_impl->method.method;
854
163
        if (prov_rw != NULL)
855
163
            *prov_rw = best_impl->provider;
856
#ifdef OPENSSL_NO_CACHED_FETCH
857
        if (!ossl_method_up_ref(&best_impl->method)) {
858
            ret = 0;
859
            *method = NULL;
860
            if (prov_rw != NULL)
861
                *prov_rw = NULL;
862
        }
863
#endif
864
163
    } else {
865
0
        ret = 0;
866
0
    }
867
868
163
#ifndef FIPS_MODULE
869
163
    OSSL_TRACE_BEGIN(QUERY)
870
0
    {
871
0
        char buf[512];
872
0
        size_t size;
873
874
0
        size = ossl_property_list_to_string(NULL, pq, buf, 512);
875
0
        BIO_printf(trc_out, "method store query with properties %s "
876
0
                            "resolves to provider %s\n",
877
0
            size == 0 ? "none" : buf,
878
0
            best_impl == NULL ? "none" : ossl_provider_name(best_impl->provider));
879
0
    }
880
163
    OSSL_TRACE_END(QUERY);
881
163
#endif
882
883
163
    ossl_property_unlock(sa);
884
163
    ossl_property_free(p2);
885
163
    return ret;
886
163
}
887
888
static void ossl_method_cache_flush_alg(STORED_ALGORITHMS *sa,
889
    ALGORITHM *alg)
890
1
{
891
1
    impl_cache_flush_alg(alg, sa);
892
1
}
893
894
static void ossl_method_cache_flush(STORED_ALGORITHMS *sa, int nid)
895
34
{
896
34
    ALGORITHM *alg = ossl_method_store_retrieve(sa, nid);
897
898
34
    if (alg != NULL)
899
1
        ossl_method_cache_flush_alg(sa, alg);
900
34
}
901
902
static void ossl_cache_lists_flush(STORED_ALGORITHMS *sa)
903
68
{
904
68
    int i;
905
68
    QUERY *idx, *idxn;
906
907
612
    for (i = 0; i < MAX_CACHE_LINES; i++) {
908
544
        if (!CRYPTO_atomic_load_ptr((void **)&sa->cache_lists[i], (void **)&idx, sa->alock))
909
0
            break;
910
544
        while (idx != NULL) {
911
0
            if (!CRYPTO_atomic_load_ptr((void **)&idx->next, (void **)&idxn, sa->alock))
912
0
                break;
913
0
            ossl_method_store_atomic_archive(sa, idx);
914
0
            idx = idxn;
915
0
        }
916
544
    }
917
68
}
918
919
static void ossl_cache_lists_free(STORED_ALGORITHMS *sa)
920
0
{
921
0
    int i;
922
0
    QUERY *idx, *idxn;
923
924
0
    for (i = 0; i < MAX_CACHE_LINES; i++) {
925
0
        if (!CRYPTO_atomic_load_ptr((void **)&sa->cache_lists[i], (void **)&idx, sa->alock))
926
0
            return;
927
0
        while (idx != NULL) {
928
0
            if (!CRYPTO_atomic_load_ptr((void **)&idx->next, (void **)&idxn, sa->alock))
929
0
                return;
930
0
            impl_cache_free_unlinked(idx);
931
0
            idx = idxn;
932
0
        }
933
0
    }
934
935
0
    if (!CRYPTO_atomic_load_ptr((void **)&sa->archive, (void **)&idx, sa->alock))
936
0
        return;
937
0
    while (idx != NULL) {
938
0
        if (!CRYPTO_atomic_load_ptr((void **)&idx->next, (void **)&idxn, sa->alock))
939
0
            return;
940
0
        impl_cache_free_unlinked(idx);
941
0
        idx = idxn;
942
0
    }
943
0
}
944
945
int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store)
946
17
{
947
85
    for (int i = 0; i < NUM_SHARDS; ++i) {
948
68
        STORED_ALGORITHMS *sa = &store->algs[i];
949
950
68
        if (!ossl_property_write_lock(sa))
951
0
            return 0;
952
68
        ossl_cache_lists_flush(sa);
953
68
        ossl_method_store_atomic_clean_archive(sa);
954
68
        ossl_property_unlock(sa);
955
68
    }
956
957
17
    return 1;
958
17
}
959
960
static ossl_inline int ossl_method_store_cache_get_atomic(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
961
    int nid, const char *prop_query, STORED_ALGORITHMS *sa, void **method)
962
829k
{
963
829k
    QUERY *r = NULL;
964
829k
    int res = 0;
965
966
829k
    r = ossl_method_store_atomic_find_in_list(sa, nid, prov, prop_query);
967
968
829k
    if (r != NULL) {
969
829k
        *method = r->method.method;
970
829k
        res = 1;
971
#ifdef OPENSSL_NO_CACHED_FETCH
972
        if (!ossl_method_up_ref(&r->method)) {
973
            *method = NULL;
974
            res = 0;
975
        }
976
#endif
977
829k
    }
978
979
829k
    return res;
980
829k
}
981
982
int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
983
    int nid, const char *prop_query, void **method)
984
829k
{
985
829k
    int ret;
986
829k
    STORED_ALGORITHMS *sa;
987
988
829k
    if (nid <= 0 || store == NULL || prop_query == NULL)
989
0
        return 0;
990
991
829k
    sa = stored_algs_shard(store, nid);
992
993
    /*
994
     * Do an atomic linked list walk to search for our entry
995
     */
996
829k
    ret = ossl_method_store_cache_get_atomic(store, prov, nid, prop_query, sa,
997
829k
        method);
998
999
829k
    return ret;
1000
829k
}
1001
1002
static int ossl_method_store_atomic_archive(STORED_ALGORITHMS *sa, QUERY *old)
1003
0
{
1004
0
    if (!CRYPTO_atomic_store_int(&old->archived, 1, sa->alock))
1005
0
        return 0;
1006
0
    return 1;
1007
0
}
1008
1009
static ossl_inline int ossl_method_store_put_in_archive(STORED_ALGORITHMS *sa, QUERY *old)
1010
0
{
1011
    /*
1012
     * point the item we're removing's next pointer to the top of the archive list
1013
     * Note: We're writing to the old->next here which is shared, so that's suspicious, but
1014
     * because we've already removed old from the cache_list in ossl_method_store_clean_archive
1015
     * this is safe for the following reasons:
1016
     * 1) the clean path is done under a write lock, so sa->archive is guaranteed stable
1017
     * 2) any concurrent reader (ie ossl_method_store_cache_set|get, if visiting the old node
1018
     * while we're moving it, will either read the true next value (pointing to the next element
1019
     * in the cache_list), or the one we write here (the next list in the archive)
1020
     *
1021
     * Reading the true next value is fine, as that's the normal traversal anyway.
1022
     * Reading the next pointer as pointing into the archive list is not great, but in the worst
1023
     * case this results in a transient failed cache lookup, which just means a temporary slow path
1024
     * retrieval of an algorithm.
1025
     */
1026
0
    if (!CRYPTO_atomic_load_ptr((void **)&sa->archive, (void **)&old->next, sa->alock))
1027
0
        return 0;
1028
    /*
1029
     * And update the head of the archive list to be our new entry
1030
     */
1031
0
    if (!CRYPTO_atomic_store_ptr((void **)&sa->archive, (void **)&old, sa->alock))
1032
0
        return 0;
1033
0
    return 1;
1034
0
}
1035
1036
/*
1037
 * Migrate archived items to the archive list.  Must be done with the property write
1038
 * lock held
1039
 */
1040
static void ossl_method_store_atomic_clean_archive(STORED_ALGORITHMS *sa)
1041
68
{
1042
68
    QUERY *idx, *idxn, *tmp;
1043
68
    int archived;
1044
68
    int i;
1045
68
    int lock_failed;
1046
1047
    /*
1048
     * For each of our linked lists
1049
     */
1050
612
    for (i = 0; i < MAX_CACHE_LINES; i++) {
1051
544
    restart_list:
1052
        /*
1053
         * Get the head of the list
1054
         */
1055
544
        if (!CRYPTO_atomic_load_ptr((void **)&sa->cache_lists[i], (void **)&idx, sa->alock))
1056
0
            continue;
1057
        /*
1058
         * If its NULL, the list is currently empty, move on to the next one
1059
         */
1060
544
        if (idx == NULL)
1061
544
            continue;
1062
        /*
1063
         * Get its archived value
1064
         */
1065
0
        if (!CRYPTO_atomic_load_int(&idx->archived, &archived, sa->alock))
1066
0
            continue;
1067
        /*
1068
         * Also fetch its next pointer to idxn
1069
         */
1070
0
        if (!CRYPTO_atomic_load_ptr((void **)&idx->next, (void **)&idxn, sa->alock))
1071
0
            continue;
1072
        /*
1073
         * If its been archived, we want to move it to the archive list
1074
         */
1075
0
        if (archived == 1) {
1076
            /*
1077
             * We know this is the current list head we're working with
1078
             * so store the next pointer to be the new list head
1079
             */
1080
0
            if (!CRYPTO_atomic_cmp_exch_ptr((void **)&sa->cache_lists[i], (void **)&idx, idxn, sa->alock,
1081
0
                    &lock_failed)) {
1082
0
                if (lock_failed)
1083
0
                    continue;
1084
0
                else
1085
0
                    goto restart_list;
1086
0
            }
1087
1088
0
            if (!ossl_method_store_put_in_archive(sa, idx))
1089
0
                continue;
1090
0
            goto restart_list;
1091
0
        }
1092
1093
        /*
1094
         * At this point our state is:
1095
         * idx - points to an element in cache_lists[i]
1096
         * idxn points to the next entry (i.e. idx->next)
1097
         */
1098
0
        while (idx != NULL) {
1099
            /*
1100
             * We know idx isn't archived, so we start looking at idxn
1101
             */
1102
0
            if (idxn != NULL) {
1103
                /*
1104
                 * if its not NULL, see if its archived
1105
                 */
1106
0
                if (!CRYPTO_atomic_load_int(&idxn->archived, &archived, sa->alock))
1107
0
                    break;
1108
                /*
1109
                 * If it is, remove it
1110
                 */
1111
0
                if (archived == 1) {
1112
                    /*
1113
                     * Start by making idx skip idxn in the list
1114
                     * First load the expected next value of idx->next
1115
                     */
1116
0
                    if (!CRYPTO_atomic_load_ptr((void **)&idx->next, (void **)&tmp, sa->alock))
1117
0
                        break;
1118
1119
                    /*
1120
                     * Now compare the value of idx->next to what we just loaded to tmp above
1121
                     * if they match, we can safely update idx->next to skip the idxn entry
1122
                     * by pointing idx->next to idxn->next.
1123
                     * If the comparison fails, then we need to start the list traversal over again.
1124
                     * Note: This should never happen, as once an item is in the list, this is the
1125
                     * only path in which an in-list item has its next pointer mutated, and this
1126
                     * occurs under a write lock, but we should be safe here
1127
                     */
1128
0
                    if (!CRYPTO_atomic_cmp_exch_ptr((void **)&idx->next,
1129
0
                            (void **)&tmp, (void *)idxn->next,
1130
0
                            sa->alock, &lock_failed)) {
1131
0
                        if (lock_failed)
1132
0
                            break;
1133
                        /*
1134
                         * The list was mutated while we were trying to mutate it
1135
                         * Normally we would just use the reloaded value of tmp here to re-attempt
1136
                         * the removal, but since idx was changed underneath us, we don't know where
1137
                         * we are in the list anymore.  Its safer to just restart the whole traversal
1138
                         */
1139
0
                        goto restart_list;
1140
0
                    }
1141
1142
0
                    if (!ossl_method_store_put_in_archive(sa, idxn))
1143
0
                        break;
1144
1145
                    /*
1146
                     * Idx just got a new next pointer above, so just update idxn, so we are sure that idx
1147
                     * still isn't archived
1148
                     */
1149
0
                    if (!CRYPTO_atomic_load_ptr((void **)&idx->next, (void **)&idxn, sa->alock))
1150
0
                        break;
1151
0
                } else {
1152
                    /*
1153
                     * idxn wasn't archived, so we need to advance both pointers here
1154
                     */
1155
0
                    idx = idxn;
1156
0
                    if (!CRYPTO_atomic_load_ptr((void **)&idx->next, (void **)&idxn, sa->alock))
1157
0
                        break;
1158
0
                }
1159
0
            } else {
1160
                /*
1161
                 * idxn is NULL, that means we're at the end of the list.
1162
                 * Just advance idx to idxn and the loop will break on the next iteration
1163
                 */
1164
0
                idx = idxn;
1165
0
            }
1166
0
        }
1167
0
    }
1168
68
}
1169
1170
static QUERY *ossl_method_store_atomic_find_in_list(STORED_ALGORITHMS *sa, int nid,
1171
    OSSL_PROVIDER *prov, const char *prop_query)
1172
829k
{
1173
829k
    int nididx = (nid >> NUM_SHARDS_BITS) & (MAX_CACHE_LINES - 1);
1174
829k
    int archived;
1175
829k
    QUERY *idx;
1176
829k
    QUERY *ret = NULL;
1177
1178
829k
    if (!CRYPTO_atomic_load_ptr((void **)&sa->cache_lists[nididx], (void **)&idx, sa->alock))
1179
0
        goto out;
1180
1181
26.9M
    while (idx != NULL) {
1182
26.9M
        if (!CRYPTO_atomic_load_int(&idx->archived, &archived, sa->alock))
1183
0
            goto out;
1184
26.9M
        if (archived == 0 && idx->nid == nid && idx->prov == prov
1185
829k
            && (strcmp(idx->prop_query, prop_query) == 0)) {
1186
829k
            ret = idx;
1187
829k
            break;
1188
829k
        }
1189
26.1M
        if (!CRYPTO_atomic_load_ptr((void **)&idx->next, (void **)&idx, sa->alock))
1190
0
            goto out;
1191
26.1M
    }
1192
829k
out:
1193
829k
    return ret;
1194
829k
}
1195
1196
static int ossl_method_store_atomic_insert_to_list(STORED_ALGORITHMS *sa, QUERY *new)
1197
50
{
1198
50
    int nid = (new->nid >> NUM_SHARDS_BITS) & (MAX_CACHE_LINES - 1);
1199
50
    QUERY *headptr;
1200
50
    int ret = 0;
1201
50
    int lock_failed;
1202
1203
50
    if (!CRYPTO_atomic_load_ptr((void **)&sa->cache_lists[nid], (void **)&headptr, sa->alock))
1204
0
        goto out;
1205
50
try_again:
1206
50
    if (!CRYPTO_atomic_store_ptr((void **)&new->next, (void **)&headptr, sa->alock))
1207
0
        goto out;
1208
50
    if (!CRYPTO_atomic_cmp_exch_ptr((void **)&sa->cache_lists[nid], (void **)&headptr, new, sa->alock,
1209
50
            &lock_failed)) {
1210
0
        if (lock_failed == 1)
1211
0
            goto out;
1212
0
        goto try_again;
1213
0
    }
1214
50
    ret = 1;
1215
50
out:
1216
50
    return ret;
1217
50
}
1218
1219
static ossl_inline int ossl_method_store_cache_set_atomic(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
1220
    int nid, const char *prop_query, STORED_ALGORITHMS *sa, void *method,
1221
    int (*method_up_ref)(void *),
1222
    void (*method_destruct)(void *))
1223
25
{
1224
25
    QUERY *p = NULL;
1225
25
    int res = 1;
1226
1227
25
    if (method == NULL) {
1228
0
        p = ossl_method_store_atomic_find_in_list(sa, nid, prov, prop_query);
1229
0
        if (p != NULL)
1230
0
            ossl_method_store_atomic_archive(sa, p);
1231
0
        goto end;
1232
0
    }
1233
1234
25
    p = ossl_method_store_atomic_find_in_list(sa, nid, prov, prop_query);
1235
25
    if (p != NULL)
1236
0
        ossl_method_store_atomic_archive(sa, p);
1237
1238
25
    p = QUERY_new(strlen(prop_query));
1239
25
    if (p != NULL) {
1240
25
        TSAN_BENIGN(p, "Unpublished value is safe on subsequent read");
1241
25
        p->saptr = sa;
1242
25
        p->nid = nid;
1243
25
        p->prov = prov;
1244
25
        p->archived = 0;
1245
25
        strcpy(p->prop_query, prop_query);
1246
25
        p->method.method = method;
1247
25
        p->method.up_ref = method_up_ref;
1248
25
        p->method.free = method_destruct;
1249
25
        if (!ossl_method_up_ref(&p->method))
1250
0
            goto err;
1251
1252
25
        if (!ossl_method_store_atomic_insert_to_list(sa, p)) {
1253
0
            ossl_method_free(&p->method);
1254
0
            goto err;
1255
0
        }
1256
1257
        /*
1258
         * We also want to add this method into the cache against a key computed
1259
         * _only_ from nid and property query.  This lets us match in the event
1260
         * someone does a lookup against a NULL provider (i.e. the "any provided
1261
         * alg will do" match).
1262
         *
1263
         * Only insert it if no NULL-provider entry exists yet for this nid and
1264
         * property query.  The first provider to cache this nid owns that
1265
         * entry, which is the provider ossl_method_store_fetch picks by
1266
         * implementation order.
1267
         */
1268
25
        if (ossl_method_store_atomic_find_in_list(sa, nid, NULL, prop_query) == NULL) {
1269
25
            p = QUERY_new(strlen(prop_query));
1270
25
            if (p == NULL)
1271
0
                goto err;
1272
25
            TSAN_BENIGN(p, "Unpublished value is safe on subsequent read");
1273
25
            p->saptr = sa;
1274
25
            p->nid = nid;
1275
25
            p->prov = NULL;
1276
25
            p->archived = 0;
1277
25
            strcpy(p->prop_query, prop_query);
1278
25
            p->method.method = method;
1279
25
            p->method.up_ref = method_up_ref;
1280
25
            p->method.free = method_destruct;
1281
25
            if (!ossl_method_up_ref(&p->method))
1282
0
                goto err;
1283
25
            if (!ossl_method_store_atomic_insert_to_list(sa, p)) {
1284
0
                ossl_method_free(&p->method);
1285
0
                goto err;
1286
0
            }
1287
25
        }
1288
1289
25
        goto end;
1290
25
    }
1291
0
err:
1292
0
    res = 0;
1293
0
    QUERY_free(p);
1294
25
end:
1295
25
    return res;
1296
0
}
1297
1298
int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov,
1299
    int nid, const char *prop_query, void *method,
1300
    int (*method_up_ref)(void *),
1301
    void (*method_destruct)(void *))
1302
25
{
1303
25
    STORED_ALGORITHMS *sa;
1304
25
    int res = 1;
1305
1306
25
    if (nid <= 0 || store == NULL || prop_query == NULL)
1307
0
        return 0;
1308
1309
25
    if (!ossl_assert(prov != NULL))
1310
0
        return 0;
1311
1312
25
    sa = stored_algs_shard(store, nid);
1313
1314
    /*
1315
     * Do an atomic insert into the appropriate cache linked list
1316
     */
1317
25
    res = ossl_method_store_cache_set_atomic(store, prov, nid, prop_query, sa, method,
1318
25
        method_up_ref, method_destruct);
1319
1320
25
    return res;
1321
25
}