Coverage Report

Created: 2026-08-18 07:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/evp/evp_fetch.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2025 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
5.31k
#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
105k
{
63
105k
    OSSL_TRACE1(QUERY, "Deallocating the tmp_store %p\n", store);
64
105k
    if (store != NULL)
65
0
        ossl_method_store_free(store);
66
105k
}
67
68
static OSSL_METHOD_STORE *get_evp_method_store(OSSL_LIB_CTX *libctx)
69
108k
{
70
108k
    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX);
71
108k
}
72
73
static int reserve_evp_method_store(void *store, void *data)
74
85
{
75
85
    struct evp_method_data_st *methdata = data;
76
77
85
    if (store == NULL
78
85
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
79
0
        return 0;
80
81
85
    return ossl_method_lock_store(store);
82
85
}
83
84
static int unreserve_evp_method_store(void *store, void *data)
85
85
{
86
85
    struct evp_method_data_st *methdata = data;
87
88
85
    if (store == NULL
89
85
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
90
0
        return 0;
91
92
85
    return ossl_method_unlock_store(store);
93
85
}
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
108k
#define METHOD_ID_OPERATION_MASK 0x000000FF
113
#define METHOD_ID_OPERATION_MAX ((1 << 8) - 1)
114
108k
#define METHOD_ID_NAME_MASK 0x7FFFFF00
115
108k
#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
108k
{
119
108k
    if (!ossl_assert(name_id > 0 && name_id <= METHOD_ID_NAME_MAX)
120
108k
        || !ossl_assert(operation_id > 0
121
108k
            && operation_id <= METHOD_ID_OPERATION_MAX))
122
0
        return 0;
123
108k
    return (((name_id << METHOD_ID_NAME_OFFSET) & METHOD_ID_NAME_MASK)
124
108k
        | (operation_id & METHOD_ID_OPERATION_MASK));
125
108k
}
126
127
static void *get_evp_method_from_store(void *store, const OSSL_PROVIDER **prov,
128
    void *data)
129
85
{
130
85
    struct evp_method_data_st *methdata = data;
131
85
    void *method = NULL;
132
85
    int name_id;
133
85
    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
85
    if ((name_id = methdata->name_id) == 0 && methdata->names != NULL) {
141
32
        OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
142
32
        const char *names = methdata->names;
143
32
        const char *q = strchr(names, NAME_SEPARATOR);
144
32
        size_t l = (q == NULL ? strlen(names) : (size_t)(q - names));
145
146
32
        if (namemap == 0)
147
0
            return NULL;
148
32
        name_id = ossl_namemap_name2num_n(namemap, names, l);
149
32
    }
150
151
85
    if (name_id == 0
152
85
        || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
153
0
        return NULL;
154
155
85
    if (store == NULL
156
85
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
157
0
        return NULL;
158
159
85
    if (!ossl_method_store_fetch(store, meth_id, methdata->propquery, prov,
160
85
            &method))
161
0
        return NULL;
162
85
    return method;
163
85
}
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
2.64k
{
170
2.64k
    struct evp_method_data_st *methdata = data;
171
2.64k
    OSSL_NAMEMAP *namemap;
172
2.64k
    int name_id;
173
2.64k
    uint32_t meth_id;
174
2.64k
    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
2.64k
    if (names != NULL) {
183
2.64k
        const char *q = strchr(names, NAME_SEPARATOR);
184
185
2.64k
        l = (q == NULL ? strlen(names) : (size_t)(q - names));
186
2.64k
    }
187
188
2.64k
    if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
189
2.64k
        || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0
190
2.64k
        || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
191
0
        return 0;
192
193
2.64k
    OSSL_TRACE1(QUERY, "put_evp_method_in_store: original store: %p\n", store);
194
2.64k
    if (store == NULL
195
2.64k
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
196
0
        return 0;
197
198
2.64k
    OSSL_TRACE5(QUERY,
199
2.64k
        "put_evp_method_in_store: "
200
2.64k
        "store: %p, names: %s, operation_id %d, method_id: %d, properties: %s\n",
201
2.64k
        store, names, methdata->operation_id, meth_id, propdef ? propdef : "<null>");
202
2.64k
    return ossl_method_store_add(store, prov, meth_id, propdef, method,
203
2.64k
        methdata->refcnt_up_method,
204
2.64k
        methdata->destruct_method);
205
2.64k
}
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
2.64k
{
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
2.64k
    struct evp_method_data_st *methdata = data;
222
2.64k
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
223
2.64k
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
224
2.64k
    const char *names = algodef->algorithm_names;
225
2.64k
    int name_id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
226
2.64k
    void *method;
227
228
2.64k
    if (name_id == 0)
229
0
        return NULL;
230
231
2.64k
    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
2.64k
    if (method == NULL)
239
0
        methdata->flag_construct_error_occurred = 1;
240
241
2.64k
    return method;
242
2.64k
}
243
244
static void destruct_evp_method(void *method, void *data)
245
2.64k
{
246
2.64k
    struct evp_method_data_st *methdata = data;
247
248
2.64k
    methdata->destruct_method(method);
249
2.64k
}
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
105k
{
261
105k
    OSSL_METHOD_STORE *store = get_evp_method_store(methdata->libctx);
262
105k
    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
105k
    const char *const propq = properties != NULL ? properties : "";
273
105k
#endif /* FIPS_MODULE */
274
105k
    uint32_t meth_id = 0;
275
105k
    void *method = NULL;
276
105k
    int unsupported, name_id;
277
105k
    int set_in_cache = 1;
278
105k
    void *tmp_method;
279
105k
    const OSSL_PROVIDER *tmp_prov = prov;
280
281
105k
    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
105k
    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
105k
    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
105k
    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
105k
    unsupported = name_id == 0;
316
317
105k
    if (meth_id == 0
318
105k
        || !ossl_method_store_cache_get(store, prov, meth_id, propq, &method)) {
319
85
        OSSL_METHOD_CONSTRUCT_METHOD mcm = {
320
85
            get_tmp_evp_method_store,
321
85
            reserve_evp_method_store,
322
85
            unreserve_evp_method_store,
323
85
            get_evp_method_from_store,
324
85
            put_evp_method_in_store,
325
85
            construct_evp_method,
326
85
            destruct_evp_method
327
85
        };
328
329
85
        methdata->operation_id = operation_id;
330
85
        methdata->name_id = name_id;
331
85
        methdata->names = name;
332
85
        methdata->propquery = propq;
333
85
        methdata->method_from_algorithm = new_method;
334
85
        methdata->refcnt_up_method = up_ref_method;
335
85
        methdata->destruct_method = free_method;
336
85
        methdata->flag_construct_error_occurred = 0;
337
85
        if ((method = ossl_method_construct(methdata->libctx, operation_id,
338
85
                 &prov, 0 /* !force_cache */,
339
85
                 &mcm, methdata))
340
85
            != 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
85
            if (name_id == 0)
354
32
                name_id = ossl_namemap_name2num(namemap, name);
355
85
            if (name_id == 0) {
356
0
                ERR_raise_data(ERR_LIB_EVP, ERR_R_FETCH_FAILED,
357
0
                    "Algorithm %s cannot be found", name != NULL ? name : "<null>");
358
#ifdef OPENSSL_NO_CACHED_FETCH
359
                free_method(method);
360
#endif
361
0
                method = NULL;
362
85
            } else {
363
85
                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
85
                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
85
                    if (!ossl_method_store_fetch(methdata->tmp_store, meth_id, propq, &tmp_prov, &tmp_method)) {
376
85
                        set_in_cache = 1;
377
85
                    } 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
85
                }
396
397
85
                if (set_in_cache == 1) {
398
85
                    ossl_method_store_cache_set(store, prov, meth_id, propq,
399
85
                        method, up_ref_method, free_method);
400
85
                } 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
85
            }
462
85
        }
463
464
        /*
465
         * If we never were in the constructor, the algorithm to be fetched
466
         * is unsupported.
467
         */
468
85
        unsupported = !methdata->flag_construct_error_occurred;
469
85
    }
470
471
105k
    if ((name_id != 0 || name != NULL) && method == NULL) {
472
0
        int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
473
474
0
        if (name == NULL)
475
0
            name = ossl_namemap_num2name(namemap, name_id, 0);
476
0
        ERR_raise_data(ERR_LIB_EVP, code,
477
0
            "%s, Algorithm (%s : %d), Properties (%s)",
478
0
            ossl_lib_ctx_get_descriptor(methdata->libctx),
479
0
            name == NULL ? "<null>" : name, name_id,
480
0
            properties == NULL ? "<null>" : properties);
481
105k
    } else {
482
105k
        OSSL_TRACE4(QUERY, "%s, Algorithm (%s : %d), Properties (%s)\n",
483
105k
            ossl_lib_ctx_get_descriptor(methdata->libctx),
484
105k
            name == NULL ? "<null>" : name, name_id,
485
105k
            properties == NULL ? "<null>" : properties);
486
105k
    }
487
488
105k
    return method;
489
105k
}
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
105k
{
499
105k
    struct evp_method_data_st methdata;
500
105k
    void *method;
501
502
105k
    methdata.libctx = libctx;
503
105k
    methdata.tmp_store = NULL;
504
105k
    method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
505
105k
        name, properties,
506
105k
        new_method, up_ref_method, free_method);
507
105k
    dealloc_tmp_evp_method_store(methdata.tmp_store);
508
105k
    return method;
509
105k
}
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
0
{
525
0
    struct evp_method_data_st methdata;
526
0
    void *method;
527
528
0
    methdata.libctx = ossl_provider_libctx(prov);
529
0
    methdata.tmp_store = NULL;
530
0
    method = inner_evp_generic_fetch(&methdata, prov, operation_id,
531
0
        name, properties,
532
0
        new_method, up_ref_method, free_method);
533
0
    dealloc_tmp_evp_method_store(methdata.tmp_store);
534
0
    return method;
535
0
}
536
537
int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx)
538
0
{
539
0
    OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
540
541
0
    if (store != NULL)
542
0
        return ossl_method_store_cache_flush_all(store);
543
0
    return 1;
544
0
}
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
0
{
724
0
    struct filter_data_st *data = arg;
725
726
0
    if ((id & METHOD_ID_OPERATION_MASK) == data->operation_id)
727
0
        data->user_fn(method, data->user_arg);
728
0
}
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
0
{
739
0
    struct evp_method_data_st methdata;
740
0
    struct filter_data_st data;
741
742
0
    methdata.libctx = libctx;
743
0
    methdata.tmp_store = NULL;
744
0
    (void)inner_evp_generic_fetch(&methdata, NULL, operation_id, NULL, NULL,
745
0
        new_method, up_ref_method, free_method);
746
747
0
    data.operation_id = operation_id;
748
0
    data.user_fn = user_fn;
749
0
    data.user_arg = user_arg;
750
0
    if (methdata.tmp_store != NULL)
751
0
        ossl_method_store_do_all(methdata.tmp_store, &filter_on_operation_id,
752
0
            &data);
753
0
    ossl_method_store_do_all(get_evp_method_store(libctx),
754
0
        &filter_on_operation_id, &data);
755
0
    dealloc_tmp_evp_method_store(methdata.tmp_store);
756
0
}
757
758
int evp_is_a(OSSL_PROVIDER *prov, int number,
759
    const char *legacy_name, const char *name)
760
0
{
761
    /*
762
     * For a |prov| that is NULL, the library context will be NULL
763
     */
764
0
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
765
0
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
766
767
0
    if (prov == NULL)
768
0
        number = ossl_namemap_name2num(namemap, legacy_name);
769
0
    return ossl_namemap_name2num(namemap, name) == number;
770
0
}
771
772
int evp_names_do_all(OSSL_PROVIDER *prov, int number,
773
    void (*fn)(const char *name, void *data),
774
    void *data)
775
2.56k
{
776
2.56k
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
777
2.56k
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
778
779
2.56k
    return ossl_namemap_doall_names(namemap, number, fn, data);
780
2.56k
}