Coverage Report

Created: 2024-07-27 06:36

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