Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/evp/evp_fetch.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stddef.h>
11
#include <openssl/types.h>
12
#include <openssl/evp.h>
13
#include <openssl/core.h>
14
#include <openssl/kdf.h>
15
#include "internal/cryptlib.h"
16
#include "internal/thread_once.h"
17
#include "internal/property.h"
18
#include "internal/core.h"
19
#include "internal/provider.h"
20
#include "internal/namemap.h"
21
#include "crypto/decoder.h"
22
#include "crypto/evp.h" /* evp_local.h needs it */
23
#include "evp_local.h"
24
25
1.55M
#define NAME_SEPARATOR ':'
26
27
/* Data to be passed through ossl_method_construct() */
28
struct evp_method_data_st {
29
    OSSL_LIB_CTX *libctx;
30
    int operation_id; /* For get_evp_method_from_store() */
31
    int name_id; /* For get_evp_method_from_store() */
32
    const char *names; /* For get_evp_method_from_store() */
33
    const char *propquery; /* For get_evp_method_from_store() */
34
35
    OSSL_METHOD_STORE *tmp_store; /* For get_tmp_evp_method_store() */
36
37
    unsigned int flag_construct_error_occurred : 1;
38
39
    void *(*method_from_algorithm)(int name_id, const OSSL_ALGORITHM *,
40
        OSSL_PROVIDER *, int);
41
    int (*refcnt_up_method)(void *method);
42
    void (*destruct_method)(void *method);
43
};
44
45
/*
46
 * Generic routines to fetch / create EVP methods with ossl_method_construct()
47
 */
48
static void *get_tmp_evp_method_store(void *data)
49
0
{
50
0
    struct evp_method_data_st *methdata = data;
51
52
0
    if (methdata->tmp_store == NULL) {
53
0
        methdata->tmp_store = ossl_method_store_new(methdata->libctx);
54
0
        OSSL_TRACE1(QUERY, "Allocating a new tmp_store %p\n", (void *)methdata->tmp_store);
55
0
    } else {
56
0
        OSSL_TRACE1(QUERY, "Using the existing tmp_store %p\n", (void *)methdata->tmp_store);
57
0
    }
58
0
    return methdata->tmp_store;
59
0
}
60
61
static void dealloc_tmp_evp_method_store(void *store)
62
22.2M
{
63
22.2M
    OSSL_TRACE1(QUERY, "Deallocating the tmp_store %p\n", store);
64
22.2M
    if (store != NULL)
65
0
        ossl_method_store_free(store);
66
22.2M
}
67
68
static OSSL_METHOD_STORE *get_evp_method_store(OSSL_LIB_CTX *libctx)
69
31.7M
{
70
31.7M
    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX);
71
31.7M
}
72
73
static int reserve_evp_method_store(void *store, void *data)
74
4.41M
{
75
4.41M
    struct evp_method_data_st *methdata = data;
76
77
4.41M
    if (store == NULL
78
4.41M
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
79
0
        return 0;
80
81
4.41M
    return ossl_method_lock_store(store);
82
4.41M
}
83
84
static int unreserve_evp_method_store(void *store, void *data)
85
4.41M
{
86
4.41M
    struct evp_method_data_st *methdata = data;
87
88
4.41M
    if (store == NULL
89
4.41M
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
90
0
        return 0;
91
92
4.41M
    return ossl_method_unlock_store(store);
93
4.41M
}
94
95
/*
96
 * To identify the method in the EVP method store, we mix the name identity
97
 * with the operation identity, under the assumption that we don't have more
98
 * than 2^23 names or more than 2^8 operation types.
99
 *
100
 * The resulting identity is a 31-bit integer, composed like this:
101
 *
102
 * +---------23 bits--------+-8 bits-+
103
 * |      name identity     | op id  |
104
 * +------------------------+--------+
105
 *
106
 * We limit this composite number to 31 bits, thus leaving the top uint32_t
107
 * bit always zero, to avoid negative sign extension when downshifting after
108
 * this number happens to be passed to an int (which happens as soon as it's
109
 * passed to ossl_method_store_cache_set(), and it's in that form that it
110
 * gets passed along to filter_on_operation_id(), defined further down.
111
 */
112
24.2M
#define METHOD_ID_OPERATION_MASK 0x000000FF
113
#define METHOD_ID_OPERATION_MAX ((1 << 8) - 1)
114
21.4M
#define METHOD_ID_NAME_MASK 0x7FFFFF00
115
21.4M
#define METHOD_ID_NAME_OFFSET 8
116
#define METHOD_ID_NAME_MAX ((1 << 23) - 1)
117
static uint32_t evp_method_id(int name_id, unsigned int operation_id)
118
21.4M
{
119
21.4M
    if (!ossl_assert(name_id > 0 && name_id <= METHOD_ID_NAME_MAX)
120
21.4M
        || !ossl_assert(operation_id > 0
121
21.4M
            && operation_id <= METHOD_ID_OPERATION_MAX))
122
0
        return 0;
123
21.4M
    return (((name_id << METHOD_ID_NAME_OFFSET) & METHOD_ID_NAME_MASK)
124
21.4M
        | (operation_id & METHOD_ID_OPERATION_MASK));
125
21.4M
}
126
127
static void *get_evp_method_from_store(void *store, const OSSL_PROVIDER **prov,
128
    void *data)
129
2.22M
{
130
2.22M
    struct evp_method_data_st *methdata = data;
131
2.22M
    void *method = NULL;
132
2.22M
    int name_id;
133
2.22M
    uint32_t meth_id;
134
135
    /*
136
     * get_evp_method_from_store() is only called to try and get the method
137
     * that evp_generic_fetch() is asking for, and the operation id as well
138
     * as the name or name id are passed via methdata.
139
     */
140
2.22M
    if ((name_id = methdata->name_id) == 0 && methdata->names != NULL) {
141
1.51M
        OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
142
1.51M
        const char *names = methdata->names;
143
1.51M
        const char *q = strchr(names, NAME_SEPARATOR);
144
1.51M
        size_t l = (q == NULL ? strlen(names) : (size_t)(q - names));
145
146
1.51M
        if (namemap == 0)
147
0
            return NULL;
148
1.51M
        name_id = ossl_namemap_name2num_n(namemap, names, l);
149
1.51M
    }
150
151
2.22M
    if (name_id == 0
152
692k
        || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
153
1.52M
        return NULL;
154
155
692k
    if (store == NULL
156
692k
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
157
0
        return NULL;
158
159
692k
    if (!ossl_method_store_fetch(store, meth_id, methdata->propquery, prov,
160
692k
            &method))
161
683k
        return NULL;
162
9.49k
    return method;
163
692k
}
164
165
static int put_evp_method_in_store(void *store, void *method,
166
    const OSSL_PROVIDER *prov,
167
    const char *names, const char *propdef,
168
    void *data)
169
20.3k
{
170
20.3k
    struct evp_method_data_st *methdata = data;
171
20.3k
    OSSL_NAMEMAP *namemap;
172
20.3k
    int name_id;
173
20.3k
    uint32_t meth_id;
174
20.3k
    size_t l = 0;
175
176
    /*
177
     * put_evp_method_in_store() is only called with an EVP method that was
178
     * successfully created by construct_method() below, which means that
179
     * all the names should already be stored in the namemap with the same
180
     * numeric identity, so just use the first to get that identity.
181
     */
182
20.3k
    if (names != NULL) {
183
20.3k
        const char *q = strchr(names, NAME_SEPARATOR);
184
185
20.3k
        l = (q == NULL ? strlen(names) : (size_t)(q - names));
186
20.3k
    }
187
188
20.3k
    if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
189
20.3k
        || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0
190
20.3k
        || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
191
0
        return 0;
192
193
20.3k
    OSSL_TRACE1(QUERY, "put_evp_method_in_store: original store: %p\n", store);
194
20.3k
    if (store == NULL
195
20.3k
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
196
0
        return 0;
197
198
20.3k
    OSSL_TRACE5(QUERY,
199
20.3k
        "put_evp_method_in_store: "
200
20.3k
        "store: %p, names: %s, operation_id %d, method_id: %d, properties: %s\n",
201
20.3k
        store, names, methdata->operation_id, meth_id, propdef ? propdef : "<null>");
202
20.3k
    return ossl_method_store_add(store, prov, meth_id, propdef, method,
203
20.3k
        methdata->refcnt_up_method,
204
20.3k
        methdata->destruct_method);
205
20.3k
}
206
207
/*
208
 * The core fetching functionality passes the name of the implementation.
209
 * This function is responsible to getting an identity number for it.
210
 */
211
static void *construct_evp_method(const OSSL_ALGORITHM *algodef,
212
    OSSL_PROVIDER *prov, void *data, int no_store)
213
20.3k
{
214
    /*
215
     * This function is only called if get_evp_method_from_store() returned
216
     * NULL, so it's safe to say that of all the spots to create a new
217
     * namemap entry, this is it.  Should the name already exist there, we
218
     * know that ossl_namemap_add_name() will return its corresponding
219
     * number.
220
     */
221
20.3k
    struct evp_method_data_st *methdata = data;
222
20.3k
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
223
20.3k
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
224
20.3k
    const char *names = algodef->algorithm_names;
225
20.3k
    int name_id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
226
20.3k
    void *method;
227
228
20.3k
    if (name_id == 0)
229
0
        return NULL;
230
231
20.3k
    method = methdata->method_from_algorithm(name_id, algodef, prov, no_store);
232
233
    /*
234
     * Flag to indicate that there was actual construction errors.  This
235
     * helps inner_evp_generic_fetch() determine what error it should
236
     * record on inaccessible algorithms.
237
     */
238
20.3k
    if (method == NULL)
239
0
        methdata->flag_construct_error_occurred = 1;
240
241
20.3k
    return method;
242
20.3k
}
243
244
static void destruct_evp_method(void *method, void *data)
245
20.3k
{
246
20.3k
    struct evp_method_data_st *methdata = data;
247
248
20.3k
    methdata->destruct_method(method);
249
20.3k
}
250
251
static void *
252
inner_evp_generic_fetch(struct evp_method_data_st *methdata,
253
    OSSL_PROVIDER *prov, int operation_id,
254
    const char *name, ossl_unused const char *properties,
255
    void *(*new_method)(int name_id,
256
        const OSSL_ALGORITHM *algodef,
257
        OSSL_PROVIDER *prov, int no_store),
258
    int (*up_ref_method)(void *),
259
    void (*free_method)(void *))
260
4.72M
{
261
4.72M
    OSSL_METHOD_STORE *store = get_evp_method_store(methdata->libctx);
262
4.72M
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
263
#ifdef FIPS_MODULE
264
    /*
265
     * The FIPS provider has its own internal library context where only it
266
     * is loaded.  Consequently, property queries aren't relevant because
267
     * there is only one fetchable algorithm and it is assumed that the
268
     * FIPS-ness is handled by the using algorithm.
269
     */
270
    const char *const propq = "";
271
#else
272
4.72M
    const char *const propq = properties != NULL ? properties : "";
273
4.72M
#endif /* FIPS_MODULE */
274
4.72M
    uint32_t meth_id = 0;
275
4.72M
    void *method = NULL;
276
4.72M
    int unsupported, name_id;
277
4.72M
    int set_in_cache = 1;
278
4.72M
    void *tmp_method;
279
4.72M
    const OSSL_PROVIDER *tmp_prov = prov;
280
281
4.72M
    if (store == NULL || namemap == NULL) {
282
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
283
0
        return NULL;
284
0
    }
285
286
    /*
287
     * If there's ever an operation_id == 0 passed, we have an internal
288
     * programming error.
289
     */
290
4.72M
    if (!ossl_assert(operation_id > 0)) {
291
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
292
0
        return NULL;
293
0
    }
294
295
    /* If we haven't received a name id yet, try to get one for the name */
296
4.72M
    name_id = ossl_namemap_name2num(namemap, name);
297
298
    /*
299
     * If we have a name id, calculate a method id with evp_method_id().
300
     *
301
     * evp_method_id returns 0 if we have too many operations (more than
302
     * about 2^8) or too many names (more than about 2^24).  In that case,
303
     * we can't create any new method.
304
     * For all intents and purposes, this is an internal error.
305
     */
306
4.72M
    if (name_id != 0 && (meth_id = evp_method_id(name_id, operation_id)) == 0) {
307
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
308
0
        return NULL;
309
0
    }
310
311
    /*
312
     * If we haven't found the name yet, chances are that the algorithm to
313
     * be fetched is unsupported.
314
     */
315
4.72M
    unsupported = name_id == 0;
316
317
4.72M
    if (meth_id == 0
318
4.39M
        || !ossl_method_store_cache_get(store, prov, meth_id, propq, &method)) {
319
473k
        OSSL_METHOD_CONSTRUCT_METHOD mcm = {
320
473k
            get_tmp_evp_method_store,
321
473k
            reserve_evp_method_store,
322
473k
            unreserve_evp_method_store,
323
473k
            get_evp_method_from_store,
324
473k
            put_evp_method_in_store,
325
473k
            construct_evp_method,
326
473k
            destruct_evp_method
327
473k
        };
328
329
473k
        methdata->operation_id = operation_id;
330
473k
        methdata->name_id = name_id;
331
473k
        methdata->names = name;
332
473k
        methdata->propquery = propq;
333
473k
        methdata->method_from_algorithm = new_method;
334
473k
        methdata->refcnt_up_method = up_ref_method;
335
473k
        methdata->destruct_method = free_method;
336
473k
        methdata->flag_construct_error_occurred = 0;
337
473k
        if ((method = ossl_method_construct(methdata->libctx, operation_id,
338
473k
                 &prov, 0 /* !force_cache */,
339
473k
                 &mcm, methdata))
340
473k
            != NULL) {
341
            /*
342
             * If construction did create a method for us, we know that
343
             * there is a correct name_id and meth_id, since those have
344
             * already been calculated in get_evp_method_from_store() and
345
             * put_evp_method_in_store() above.
346
             * Note that there is a corner case here, in which, if a user
347
             * passes a name of the form name1:name2:..., then the construction
348
             * will create a method against all names, but the lookup will fail
349
             * as ossl_namemap_name2num treats the name string as a single name
350
             * rather than introducing new features where in the EVP_<obj>_fetch
351
             * parses the string and queries for each, return an error.
352
             */
353
1.98k
            if (name_id == 0)
354
70
                name_id = ossl_namemap_name2num(namemap, name);
355
1.98k
            if (name_id == 0) {
356
11
                ERR_raise_data(ERR_LIB_EVP, ERR_R_FETCH_FAILED,
357
11
                    "Algorithm %s cannot be found", name != NULL ? name : "<null>");
358
#ifdef OPENSSL_NO_CACHED_FETCH
359
                free_method(method);
360
#endif
361
11
                method = NULL;
362
1.97k
            } else {
363
1.97k
                meth_id = evp_method_id(name_id, operation_id);
364
                /*
365
                 * do not insert method to method store cache when provider
366
                 * did ask for not caching it. methods which are not to be
367
                 * cached end up in ->tmp_store when provider asks not
368
                 * to cache the result (see ossl_method_construct_reserve_store())
369
                 */
370
1.97k
                if (meth_id != 0) {
371
                    /*
372
                     * If the method doesn't exist in the tmp_store, either the tmp_store doesn't exist
373
                     * or the algorithm doesn't exist there, in either case, this is a cacheable entry
374
                     */
375
1.97k
                    if (!ossl_method_store_fetch(methdata->tmp_store, meth_id, propq, &tmp_prov, &tmp_method)) {
376
1.97k
                        set_in_cache = 1;
377
1.97k
                    } else {
378
                        /*
379
                         * We found a matching method in the temp store, don't cache this entry
380
                         */
381
0
                        if (tmp_method == method) {
382
0
                            set_in_cache = 0;
383
0
                        } else {
384
0
                            set_in_cache = 1;
385
0
                        }
386
387
#ifdef OPENSSL_NO_CACHED_FETCH
388
                        /*
389
                         * ossl_method_store_fetch takes a reference on the fetched method
390
                         * when using NO_CACHED_FETCH, so we need to free it here
391
                         */
392
                        free_method(tmp_method);
393
#endif
394
0
                    }
395
1.97k
                }
396
397
1.97k
                if (set_in_cache == 1) {
398
1.97k
                    ossl_method_store_cache_set(store, prov, meth_id, propq,
399
1.97k
                        method, up_ref_method, free_method);
400
1.97k
                } else {
401
0
#ifndef OPENSSL_NO_CACHED_FETCH
402
                    /*
403
                     * There is a corner case we need to handle here.  IF:
404
                     * 1) we are fetching an algorithm and plan to return it to the caller
405
                     * 2) The provider we fetched from requested no_cache
406
                     * Then we are in a situation in which this method that was constructed
407
                     * only lives in the tmp_store, and has a reference count of 1.
408
                     * On return from this function, that tmp_store is going to be deallocated,
409
                     * Which will drop the methods ref count to 0 and free it, after which the
410
                     * method will be returned to the called, as an already freed object.
411
                     *
412
                     * That's bad.  We need to grab an extra ref count on the method before returning
413
                     * so that the requestor via EVP_*_fetch has ownership.
414
                     *
415
                     * BUT we only want to do this in the event that the algorithm is uncached.
416
                     * Unfortunately, we don't know that here, because it was the provider that
417
                     * made that request.  However, each algorithm type does store that information
418
                     * so we have a path forward.  Based on the operation id, call the appropriate
419
                     * up_ref method.  That implementation knows how to query its algorithm type and
420
                     * decide if a reference needs to be taken here
421
                     */
422
0
                    switch (operation_id) {
423
0
                    case OSSL_OP_DIGEST:
424
0
                        EVP_MD_up_ref((EVP_MD *)method);
425
0
                        break;
426
0
                    case OSSL_OP_CIPHER:
427
0
                        EVP_CIPHER_up_ref((EVP_CIPHER *)method);
428
0
                        break;
429
0
                    case OSSL_OP_MAC:
430
0
                        EVP_MAC_up_ref((EVP_MAC *)method);
431
0
                        break;
432
0
                    case OSSL_OP_KDF:
433
0
                        EVP_KDF_up_ref((EVP_KDF *)method);
434
0
                        break;
435
0
                    case OSSL_OP_RAND:
436
0
                        EVP_RAND_up_ref((EVP_RAND *)method);
437
0
                        break;
438
0
                    case OSSL_OP_KEYMGMT:
439
0
                        EVP_KEYMGMT_up_ref((EVP_KEYMGMT *)method);
440
0
                        break;
441
0
                    case OSSL_OP_KEYEXCH:
442
0
                        EVP_KEYEXCH_up_ref((EVP_KEYEXCH *)method);
443
0
                        break;
444
0
                    case OSSL_OP_SIGNATURE:
445
0
                        EVP_SIGNATURE_up_ref((EVP_SIGNATURE *)method);
446
0
                        break;
447
0
                    case OSSL_OP_ASYM_CIPHER:
448
0
                        EVP_ASYM_CIPHER_up_ref((EVP_ASYM_CIPHER *)method);
449
0
                        break;
450
0
                    case OSSL_OP_KEM:
451
0
                        EVP_KEM_up_ref((EVP_KEM *)method);
452
0
                        break;
453
0
                    case OSSL_OP_SKEYMGMT:
454
0
                        EVP_SKEYMGMT_up_ref((EVP_SKEYMGMT *)method);
455
0
                        break;
456
0
                    default:
457
0
                        break;
458
0
                    }
459
0
#endif
460
0
                }
461
1.97k
            }
462
1.98k
        }
463
464
        /*
465
         * If we never were in the constructor, the algorithm to be fetched
466
         * is unsupported.
467
         */
468
473k
        unsupported = !methdata->flag_construct_error_occurred;
469
473k
    }
470
471
4.72M
    if ((name_id != 0 || name != NULL) && method == NULL) {
472
466k
        int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
473
474
466k
        if (name == NULL)
475
0
            name = ossl_namemap_num2name(namemap, name_id, 0);
476
466k
        ERR_raise_data(ERR_LIB_EVP, code,
477
466k
            "%s, Algorithm (%s : %d), Properties (%s)",
478
466k
            ossl_lib_ctx_get_descriptor(methdata->libctx),
479
466k
            name == NULL ? "<null>" : name, name_id,
480
466k
            properties == NULL ? "<null>" : properties);
481
4.25M
    } else {
482
4.25M
        OSSL_TRACE4(QUERY, "%s, Algorithm (%s : %d), Properties (%s)\n",
483
4.25M
            ossl_lib_ctx_get_descriptor(methdata->libctx),
484
4.25M
            name == NULL ? "<null>" : name, name_id,
485
4.25M
            properties == NULL ? "<null>" : properties);
486
4.25M
    }
487
488
4.72M
    return method;
489
4.72M
}
490
491
void *evp_generic_fetch(OSSL_LIB_CTX *libctx, int operation_id,
492
    const char *name, const char *properties,
493
    void *(*new_method)(int name_id,
494
        const OSSL_ALGORITHM *algodef,
495
        OSSL_PROVIDER *prov, int no_store),
496
    int (*up_ref_method)(void *),
497
    void (*free_method)(void *))
498
22.0M
{
499
22.0M
    struct evp_method_data_st methdata;
500
22.0M
    void *method;
501
502
22.0M
    methdata.libctx = libctx;
503
22.0M
    methdata.tmp_store = NULL;
504
22.0M
    method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
505
22.0M
        name, properties,
506
22.0M
        new_method, up_ref_method, free_method);
507
22.0M
    dealloc_tmp_evp_method_store(methdata.tmp_store);
508
22.0M
    return method;
509
22.0M
}
510
511
/*
512
 * evp_generic_fetch_from_prov() is special, and only returns methods from
513
 * the given provider.
514
 * This is meant to be used when one method needs to fetch an associated
515
 * method.
516
 */
517
void *evp_generic_fetch_from_prov(OSSL_PROVIDER *prov, int operation_id,
518
    const char *name, const char *properties,
519
    void *(*new_method)(int name_id,
520
        const OSSL_ALGORITHM *algodef,
521
        OSSL_PROVIDER *prov, int no_store),
522
    int (*up_ref_method)(void *),
523
    void (*free_method)(void *))
524
173k
{
525
173k
    struct evp_method_data_st methdata;
526
173k
    void *method;
527
528
173k
    methdata.libctx = ossl_provider_libctx(prov);
529
173k
    methdata.tmp_store = NULL;
530
173k
    method = inner_evp_generic_fetch(&methdata, prov, operation_id,
531
173k
        name, properties,
532
173k
        new_method, up_ref_method, free_method);
533
173k
    dealloc_tmp_evp_method_store(methdata.tmp_store);
534
173k
    return method;
535
173k
}
536
537
int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx)
538
140
{
539
140
    OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
540
541
140
    if (store != NULL)
542
140
        return ossl_method_store_cache_flush_all(store);
543
0
    return 1;
544
140
}
545
546
int evp_method_store_remove_all_provided(const OSSL_PROVIDER *prov)
547
0
{
548
0
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
549
0
    OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
550
551
0
    if (store != NULL)
552
0
        return ossl_method_store_remove_all_provided(store, prov);
553
0
    return 1;
554
0
}
555
556
static int evp_set_parsed_default_properties(OSSL_LIB_CTX *libctx,
557
    OSSL_PROPERTY_LIST *def_prop,
558
    int loadconfig,
559
    int mirrored)
560
0
{
561
0
    OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
562
0
    OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
563
564
0
    if (plp != NULL && store != NULL) {
565
0
        int ret;
566
0
#ifndef FIPS_MODULE
567
0
        char *propstr = NULL;
568
0
        size_t strsz;
569
570
0
        if (mirrored) {
571
0
            if (ossl_global_properties_no_mirrored(libctx))
572
0
                return 0;
573
0
        } else {
574
            /*
575
             * These properties have been explicitly set on this libctx, so
576
             * don't allow any mirroring from a parent libctx.
577
             */
578
0
            ossl_global_properties_stop_mirroring(libctx);
579
0
        }
580
581
0
        strsz = ossl_property_list_to_string(libctx, def_prop, NULL, 0);
582
0
        if (strsz > 0)
583
0
            propstr = OPENSSL_malloc(strsz);
584
0
        if (propstr == NULL) {
585
0
            ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
586
0
            return 0;
587
0
        }
588
0
        if (ossl_property_list_to_string(libctx, def_prop, propstr,
589
0
                strsz)
590
0
            == 0) {
591
0
            OPENSSL_free(propstr);
592
0
            ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
593
0
            return 0;
594
0
        }
595
0
        ossl_provider_default_props_update(libctx, propstr);
596
0
        OPENSSL_free(propstr);
597
0
#endif
598
0
        ossl_property_free(*plp);
599
0
        *plp = def_prop;
600
601
0
        ret = ossl_method_store_cache_flush_all(store);
602
0
#ifndef FIPS_MODULE
603
0
        ossl_decoder_cache_flush(libctx);
604
0
#endif
605
0
        return ret;
606
0
    }
607
0
    ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
608
0
    return 0;
609
0
}
610
611
int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
612
    int loadconfig, int mirrored)
613
0
{
614
0
    OSSL_PROPERTY_LIST *pl = NULL;
615
616
0
    if (propq != NULL && (pl = ossl_parse_query(libctx, propq, 1)) == NULL) {
617
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
618
0
        return 0;
619
0
    }
620
0
    if (!evp_set_parsed_default_properties(libctx, pl, loadconfig, mirrored)) {
621
0
        ossl_property_free(pl);
622
0
        return 0;
623
0
    }
624
0
    return 1;
625
0
}
626
627
int EVP_set_default_properties(OSSL_LIB_CTX *libctx, const char *propq)
628
0
{
629
0
    return evp_set_default_properties_int(libctx, propq, 1, 0);
630
0
}
631
632
static int evp_default_properties_merge(OSSL_LIB_CTX *libctx, const char *propq,
633
    int loadconfig)
634
0
{
635
0
    OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
636
0
    OSSL_PROPERTY_LIST *pl1, *pl2;
637
638
0
    if (propq == NULL)
639
0
        return 1;
640
0
    if (plp == NULL || *plp == NULL)
641
0
        return evp_set_default_properties_int(libctx, propq, 0, 0);
642
0
    if ((pl1 = ossl_parse_query(libctx, propq, 1)) == NULL) {
643
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
644
0
        return 0;
645
0
    }
646
0
    pl2 = ossl_property_merge(pl1, *plp);
647
0
    ossl_property_free(pl1);
648
0
    if (pl2 == NULL) {
649
0
        ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
650
0
        return 0;
651
0
    }
652
0
    if (!evp_set_parsed_default_properties(libctx, pl2, 0, 0)) {
653
0
        ossl_property_free(pl2);
654
0
        return 0;
655
0
    }
656
0
    return 1;
657
0
}
658
659
static int evp_default_property_is_enabled(OSSL_LIB_CTX *libctx,
660
    const char *prop_name)
661
0
{
662
0
    OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, 1);
663
664
0
    return plp != NULL && ossl_property_is_enabled(libctx, prop_name, *plp);
665
0
}
666
667
int EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX *libctx)
668
0
{
669
0
    return evp_default_property_is_enabled(libctx, "fips");
670
0
}
671
672
int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable,
673
    int loadconfig)
674
0
{
675
0
    const char *query = (enable != 0) ? "fips=yes" : "-fips";
676
677
0
    return evp_default_properties_merge(libctx, query, loadconfig);
678
0
}
679
680
int EVP_default_properties_enable_fips(OSSL_LIB_CTX *libctx, int enable)
681
0
{
682
0
    return evp_default_properties_enable_fips_int(libctx, enable, 1);
683
0
}
684
685
char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig)
686
0
{
687
0
    OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
688
0
    char *propstr = NULL;
689
0
    size_t sz;
690
691
0
    if (plp == NULL)
692
0
        return OPENSSL_strdup("");
693
694
0
    sz = ossl_property_list_to_string(libctx, *plp, NULL, 0);
695
0
    if (sz == 0) {
696
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
697
0
        return NULL;
698
0
    }
699
700
0
    propstr = OPENSSL_malloc(sz);
701
0
    if (propstr == NULL)
702
0
        return NULL;
703
0
    if (ossl_property_list_to_string(libctx, *plp, propstr, sz) == 0) {
704
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
705
0
        OPENSSL_free(propstr);
706
0
        return NULL;
707
0
    }
708
0
    return propstr;
709
0
}
710
711
char *EVP_get1_default_properties(OSSL_LIB_CTX *libctx)
712
0
{
713
0
    return evp_get_global_properties_str(libctx, ossl_lib_ctx_is_global_default(libctx));
714
0
}
715
716
struct filter_data_st {
717
    int operation_id;
718
    void (*user_fn)(void *method, void *arg);
719
    void *user_arg;
720
};
721
722
static void filter_on_operation_id(int id, void *method, void *arg)
723
2.80M
{
724
2.80M
    struct filter_data_st *data = arg;
725
726
2.80M
    if ((id & METHOD_ID_OPERATION_MASK) == data->operation_id)
727
633k
        data->user_fn(method, data->user_arg);
728
2.80M
}
729
730
void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
731
    void (*user_fn)(void *method, void *arg),
732
    void *user_arg,
733
    void *(*new_method)(int name_id,
734
        const OSSL_ALGORITHM *algodef,
735
        OSSL_PROVIDER *prov, int no_store),
736
    int (*up_ref_method)(void *),
737
    void (*free_method)(void *))
738
17.5k
{
739
17.5k
    struct evp_method_data_st methdata;
740
17.5k
    struct filter_data_st data;
741
742
17.5k
    methdata.libctx = libctx;
743
17.5k
    methdata.tmp_store = NULL;
744
17.5k
    (void)inner_evp_generic_fetch(&methdata, NULL, operation_id, NULL, NULL,
745
17.5k
        new_method, up_ref_method, free_method);
746
747
17.5k
    data.operation_id = operation_id;
748
17.5k
    data.user_fn = user_fn;
749
17.5k
    data.user_arg = user_arg;
750
17.5k
    if (methdata.tmp_store != NULL)
751
0
        ossl_method_store_do_all(methdata.tmp_store, &filter_on_operation_id,
752
0
            &data);
753
17.5k
    ossl_method_store_do_all(get_evp_method_store(libctx),
754
17.5k
        &filter_on_operation_id, &data);
755
17.5k
    dealloc_tmp_evp_method_store(methdata.tmp_store);
756
17.5k
}
757
758
int evp_is_a(OSSL_PROVIDER *prov, int number,
759
    const char *legacy_name, const char *name)
760
3.34M
{
761
    /*
762
     * For a |prov| that is NULL, the library context will be NULL
763
     */
764
3.34M
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
765
3.34M
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
766
767
3.34M
    if (prov == NULL)
768
248k
        number = ossl_namemap_name2num(namemap, legacy_name);
769
3.34M
    return ossl_namemap_name2num(namemap, name) == number;
770
3.34M
}
771
772
int evp_names_do_all(OSSL_PROVIDER *prov, int number,
773
    void (*fn)(const char *name, void *data),
774
    void *data)
775
1.04M
{
776
1.04M
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
777
1.04M
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
778
779
1.04M
    return ossl_namemap_doall_names(namemap, number, fn, data);
780
1.04M
}