Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/evp/evp_fetch.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2024 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 "internal/cryptlib.h"
15
#include "internal/thread_once.h"
16
#include "internal/property.h"
17
#include "internal/core.h"
18
#include "internal/provider.h"
19
#include "internal/namemap.h"
20
#include "internal/property.h"
21
#include "crypto/evp.h" /* evp_local.h needs it */
22
#include "evp_local.h"
23
24
1.83M
#define NAME_SEPARATOR ':'
25
26
static void evp_method_store_free(void *vstore)
27
12
{
28
12
    ossl_method_store_free(vstore);
29
12
}
30
31
static void *evp_method_store_new(OSSL_LIB_CTX *ctx)
32
12
{
33
12
    return ossl_method_store_new(ctx);
34
12
}
35
36
static const OSSL_LIB_CTX_METHOD evp_method_store_method = {
37
    /* We want evp_method_store to be cleaned up before the provider store */
38
    OSSL_LIB_CTX_METHOD_PRIORITY_2,
39
    evp_method_store_new,
40
    evp_method_store_free,
41
};
42
43
/* Data to be passed through ossl_method_construct() */
44
struct evp_method_data_st {
45
    OSSL_LIB_CTX *libctx;
46
    int operation_id; /* For get_evp_method_from_store() */
47
    int name_id; /* For get_evp_method_from_store() */
48
    const char *names; /* For get_evp_method_from_store() */
49
    const char *propquery; /* For get_evp_method_from_store() */
50
51
    OSSL_METHOD_STORE *tmp_store; /* For get_tmp_evp_method_store() */
52
53
    unsigned int flag_construct_error_occurred : 1;
54
55
    void *(*method_from_algorithm)(int name_id, const OSSL_ALGORITHM *,
56
        OSSL_PROVIDER *);
57
    int (*refcnt_up_method)(void *method);
58
    void (*destruct_method)(void *method);
59
};
60
61
/*
62
 * Generic routines to fetch / create EVP methods with ossl_method_construct()
63
 */
64
static void *get_tmp_evp_method_store(void *data)
65
0
{
66
0
    struct evp_method_data_st *methdata = data;
67
68
0
    if (methdata->tmp_store == NULL)
69
0
        methdata->tmp_store = ossl_method_store_new(methdata->libctx);
70
0
    return methdata->tmp_store;
71
0
}
72
73
static void dealloc_tmp_evp_method_store(void *store)
74
27.5M
{
75
27.5M
    if (store != NULL)
76
0
        ossl_method_store_free(store);
77
27.5M
}
78
79
static OSSL_METHOD_STORE *get_evp_method_store(OSSL_LIB_CTX *libctx)
80
40.1M
{
81
40.1M
    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX,
82
40.1M
        &evp_method_store_method);
83
40.1M
}
84
85
static int reserve_evp_method_store(void *store, void *data)
86
5.73M
{
87
5.73M
    struct evp_method_data_st *methdata = data;
88
89
5.73M
    if (store == NULL
90
5.73M
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
91
0
        return 0;
92
93
5.73M
    return ossl_method_lock_store(store);
94
5.73M
}
95
96
static int unreserve_evp_method_store(void *store, void *data)
97
5.73M
{
98
5.73M
    struct evp_method_data_st *methdata = data;
99
100
5.73M
    if (store == NULL
101
5.73M
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
102
0
        return 0;
103
104
5.73M
    return ossl_method_unlock_store(store);
105
5.73M
}
106
107
/*
108
 * To identify the method in the EVP method store, we mix the name identity
109
 * with the operation identity, under the assumption that we don't have more
110
 * than 2^23 names or more than 2^8 operation types.
111
 *
112
 * The resulting identity is a 31-bit integer, composed like this:
113
 *
114
 * +---------23 bits--------+-8 bits-+
115
 * |      name identity     | op id  |
116
 * +------------------------+--------+
117
 *
118
 * We limit this composite number to 31 bits, thus leaving the top uint32_t
119
 * bit always zero, to avoid negative sign extension when downshifting after
120
 * this number happens to be passed to an int (which happens as soon as it's
121
 * passed to ossl_method_store_cache_set(), and it's in that form that it
122
 * gets passed along to filter_on_operation_id(), defined further down.
123
 */
124
53.9M
#define METHOD_ID_OPERATION_MASK 0x000000FF
125
#define METHOD_ID_OPERATION_MAX ((1 << 8) - 1)
126
26.3M
#define METHOD_ID_NAME_MASK 0x7FFFFF00
127
26.3M
#define METHOD_ID_NAME_OFFSET 8
128
#define METHOD_ID_NAME_MAX ((1 << 23) - 1)
129
static uint32_t evp_method_id(int name_id, unsigned int operation_id)
130
26.3M
{
131
26.3M
    if (!ossl_assert(name_id > 0 && name_id <= METHOD_ID_NAME_MAX)
132
26.3M
        || !ossl_assert(operation_id > 0
133
26.3M
            && operation_id <= METHOD_ID_OPERATION_MAX))
134
0
        return 0;
135
26.3M
    return (((name_id << METHOD_ID_NAME_OFFSET) & METHOD_ID_NAME_MASK)
136
26.3M
        | (operation_id & METHOD_ID_OPERATION_MASK));
137
26.3M
}
138
139
static void *get_evp_method_from_store(void *store, const OSSL_PROVIDER **prov,
140
    void *data)
141
2.90M
{
142
2.90M
    struct evp_method_data_st *methdata = data;
143
2.90M
    void *method = NULL;
144
2.90M
    int name_id = 0;
145
2.90M
    uint32_t meth_id;
146
147
    /*
148
     * get_evp_method_from_store() is only called to try and get the method
149
     * that evp_generic_fetch() is asking for, and the operation id as well
150
     * as the name or name id are passed via methdata.
151
     */
152
2.90M
    if ((name_id = methdata->name_id) == 0 && methdata->names != NULL) {
153
1.80M
        OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
154
1.80M
        const char *names = methdata->names;
155
1.80M
        const char *q = strchr(names, NAME_SEPARATOR);
156
1.80M
        size_t l = (q == NULL ? strlen(names) : (size_t)(q - names));
157
158
1.80M
        if (namemap == 0)
159
0
            return NULL;
160
1.80M
        name_id = ossl_namemap_name2num_n(namemap, names, l);
161
1.80M
    }
162
163
2.90M
    if (name_id == 0
164
826k
        || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
165
2.08M
        return NULL;
166
167
826k
    if (store == NULL
168
826k
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
169
0
        return NULL;
170
171
826k
    if (!ossl_method_store_fetch(store, meth_id, methdata->propquery, prov,
172
826k
            &method))
173
818k
        return NULL;
174
8.12k
    return method;
175
826k
}
176
177
static int put_evp_method_in_store(void *store, void *method,
178
    const OSSL_PROVIDER *prov,
179
    const char *names, const char *propdef,
180
    void *data)
181
17.9k
{
182
17.9k
    struct evp_method_data_st *methdata = data;
183
17.9k
    OSSL_NAMEMAP *namemap;
184
17.9k
    int name_id;
185
17.9k
    uint32_t meth_id;
186
17.9k
    size_t l = 0;
187
188
    /*
189
     * put_evp_method_in_store() is only called with an EVP method that was
190
     * successfully created by construct_method() below, which means that
191
     * all the names should already be stored in the namemap with the same
192
     * numeric identity, so just use the first to get that identity.
193
     */
194
17.9k
    if (names != NULL) {
195
17.9k
        const char *q = strchr(names, NAME_SEPARATOR);
196
197
17.9k
        l = (q == NULL ? strlen(names) : (size_t)(q - names));
198
17.9k
    }
199
200
17.9k
    if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
201
17.9k
        || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0
202
17.9k
        || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
203
0
        return 0;
204
205
17.9k
    if (store == NULL
206
17.9k
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
207
0
        return 0;
208
209
17.9k
    return ossl_method_store_add(store, prov, meth_id, propdef, method,
210
17.9k
        methdata->refcnt_up_method,
211
17.9k
        methdata->destruct_method);
212
17.9k
}
213
214
/*
215
 * The core fetching functionality passes the name of the implementation.
216
 * This function is responsible to getting an identity number for it.
217
 */
218
static void *construct_evp_method(const OSSL_ALGORITHM *algodef,
219
    OSSL_PROVIDER *prov, void *data)
220
17.9k
{
221
    /*
222
     * This function is only called if get_evp_method_from_store() returned
223
     * NULL, so it's safe to say that of all the spots to create a new
224
     * namemap entry, this is it.  Should the name already exist there, we
225
     * know that ossl_namemap_add_name() will return its corresponding
226
     * number.
227
     */
228
17.9k
    struct evp_method_data_st *methdata = data;
229
17.9k
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
230
17.9k
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
231
17.9k
    const char *names = algodef->algorithm_names;
232
17.9k
    int name_id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
233
17.9k
    void *method;
234
235
17.9k
    if (name_id == 0)
236
0
        return NULL;
237
238
17.9k
    method = methdata->method_from_algorithm(name_id, algodef, prov);
239
240
    /*
241
     * Flag to indicate that there was actual construction errors.  This
242
     * helps inner_evp_generic_fetch() determine what error it should
243
     * record on inaccessible algorithms.
244
     */
245
17.9k
    if (method == NULL)
246
0
        methdata->flag_construct_error_occurred = 1;
247
248
17.9k
    return method;
249
17.9k
}
250
251
static void destruct_evp_method(void *method, void *data)
252
17.9k
{
253
17.9k
    struct evp_method_data_st *methdata = data;
254
255
17.9k
    methdata->destruct_method(method);
256
17.9k
}
257
258
static void *
259
inner_evp_generic_fetch(struct evp_method_data_st *methdata,
260
    OSSL_PROVIDER *prov, int operation_id,
261
    int name_id, const char *name,
262
    const char *properties,
263
    void *(*new_method)(int name_id,
264
        const OSSL_ALGORITHM *algodef,
265
        OSSL_PROVIDER *prov),
266
    int (*up_ref_method)(void *),
267
    void (*free_method)(void *))
268
1.84M
{
269
1.84M
    OSSL_METHOD_STORE *store = get_evp_method_store(methdata->libctx);
270
1.84M
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
271
1.84M
    const char *const propq = properties != NULL ? properties : "";
272
1.84M
    uint32_t meth_id = 0;
273
1.84M
    void *method = NULL;
274
1.84M
    int unsupported = 0;
275
276
1.84M
    if (store == NULL || namemap == NULL) {
277
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
278
0
        return NULL;
279
0
    }
280
281
    /*
282
     * If there's ever an operation_id == 0 passed, we have an internal
283
     * programming error.
284
     */
285
1.84M
    if (!ossl_assert(operation_id > 0)) {
286
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
287
0
        return NULL;
288
0
    }
289
290
    /*
291
     * If we have been passed both a name_id and a name, we have an
292
     * internal programming error.
293
     */
294
1.84M
    if (!ossl_assert(name_id == 0 || name == NULL)) {
295
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
296
0
        return NULL;
297
0
    }
298
299
    /* If we haven't received a name id yet, try to get one for the name */
300
1.84M
    if (name_id == 0 && name != NULL)
301
1.58M
        name_id = ossl_namemap_name2num(namemap, name);
302
303
    /*
304
     * If we have a name id, calculate a method id with evp_method_id().
305
     *
306
     * evp_method_id returns 0 if we have too many operations (more than
307
     * about 2^8) or too many names (more than about 2^24).  In that case,
308
     * we can't create any new method.
309
     * For all intents and purposes, this is an internal error.
310
     */
311
1.84M
    if (name_id != 0 && (meth_id = evp_method_id(name_id, operation_id)) == 0) {
312
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
313
0
        return NULL;
314
0
    }
315
316
    /*
317
     * If we haven't found the name yet, chances are that the algorithm to
318
     * be fetched is unsupported.
319
     */
320
1.84M
    if (name_id == 0)
321
388k
        unsupported = 1;
322
323
1.84M
    if (meth_id == 0
324
1.46M
        || !ossl_method_store_cache_get(store, prov, meth_id, propq, &method)) {
325
445k
        OSSL_METHOD_CONSTRUCT_METHOD mcm = {
326
445k
            get_tmp_evp_method_store,
327
445k
            reserve_evp_method_store,
328
445k
            unreserve_evp_method_store,
329
445k
            get_evp_method_from_store,
330
445k
            put_evp_method_in_store,
331
445k
            construct_evp_method,
332
445k
            destruct_evp_method
333
445k
        };
334
335
445k
        methdata->operation_id = operation_id;
336
445k
        methdata->name_id = name_id;
337
445k
        methdata->names = name;
338
445k
        methdata->propquery = propq;
339
445k
        methdata->method_from_algorithm = new_method;
340
445k
        methdata->refcnt_up_method = up_ref_method;
341
445k
        methdata->destruct_method = free_method;
342
445k
        methdata->flag_construct_error_occurred = 0;
343
445k
        if ((method = ossl_method_construct(methdata->libctx, operation_id,
344
445k
                 &prov, 0 /* !force_cache */,
345
445k
                 &mcm, methdata))
346
445k
            != NULL) {
347
            /*
348
             * If construction did create a method for us, we know that
349
             * there is a correct name_id and meth_id, since those have
350
             * already been calculated in get_evp_method_from_store() and
351
             * put_evp_method_in_store() above.
352
             * Note that there is a corner case here, in which, if a user
353
             * passes a name of the form name1:name2:..., then the construction
354
             * will create a method against all names, but the lookup will fail
355
             * as ossl_namemap_name2num treats the name string as a single name
356
             * rather than introducing new features where in the EVP_<obj>_fetch
357
             * parses the string and querys for each, return an error.
358
             */
359
153
            if (name_id == 0)
360
9
                name_id = ossl_namemap_name2num(namemap, name);
361
153
            if (name_id == 0) {
362
0
                ERR_raise_data(ERR_LIB_EVP, ERR_R_FETCH_FAILED,
363
0
                    "Algorithm %s cannot be found", name);
364
0
                free_method(method);
365
0
                method = NULL;
366
153
            } else {
367
153
                meth_id = evp_method_id(name_id, operation_id);
368
153
                if (meth_id != 0)
369
153
                    ossl_method_store_cache_set(store, prov, meth_id, propq,
370
153
                        method, up_ref_method, free_method);
371
153
            }
372
153
        }
373
374
        /*
375
         * If we never were in the constructor, the algorithm to be fetched
376
         * is unsupported.
377
         */
378
445k
        unsupported = !methdata->flag_construct_error_occurred;
379
445k
    }
380
381
1.84M
    if ((name_id != 0 || name != NULL) && method == NULL) {
382
183k
        int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
383
384
183k
        if (name == NULL)
385
0
            name = ossl_namemap_num2name(namemap, name_id, 0);
386
183k
        ERR_raise_data(ERR_LIB_EVP, code,
387
183k
            "%s, Algorithm (%s : %d), Properties (%s)",
388
183k
            ossl_lib_ctx_get_descriptor(methdata->libctx),
389
183k
            name == NULL ? "<null>" : name, name_id,
390
183k
            properties == NULL ? "<null>" : properties);
391
183k
    }
392
393
1.84M
    return method;
394
1.84M
}
395
396
void *evp_generic_fetch(OSSL_LIB_CTX *libctx, int operation_id,
397
    const char *name, const char *properties,
398
    void *(*new_method)(int name_id,
399
        const OSSL_ALGORITHM *algodef,
400
        OSSL_PROVIDER *prov),
401
    int (*up_ref_method)(void *),
402
    void (*free_method)(void *))
403
27.0M
{
404
27.0M
    struct evp_method_data_st methdata;
405
27.0M
    void *method;
406
407
27.0M
    methdata.libctx = libctx;
408
27.0M
    methdata.tmp_store = NULL;
409
27.0M
    method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
410
27.0M
        0, name, properties,
411
27.0M
        new_method, up_ref_method, free_method);
412
27.0M
    dealloc_tmp_evp_method_store(methdata.tmp_store);
413
27.0M
    return method;
414
27.0M
}
415
416
/*
417
 * evp_generic_fetch_by_number() is special, and only returns methods for
418
 * already known names, i.e. it refuses to work if no name_id can be found
419
 * (it's considered an internal programming error).
420
 * This is meant to be used when one method needs to fetch an associated
421
 * method.
422
 */
423
void *evp_generic_fetch_by_number(OSSL_LIB_CTX *libctx, int operation_id,
424
    int name_id, const char *properties,
425
    void *(*new_method)(int name_id,
426
        const OSSL_ALGORITHM *algodef,
427
        OSSL_PROVIDER *prov),
428
    int (*up_ref_method)(void *),
429
    void (*free_method)(void *))
430
0
{
431
0
    struct evp_method_data_st methdata;
432
0
    void *method;
433
434
0
    methdata.libctx = libctx;
435
0
    methdata.tmp_store = NULL;
436
0
    method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
437
0
        name_id, NULL, properties,
438
0
        new_method, up_ref_method, free_method);
439
0
    dealloc_tmp_evp_method_store(methdata.tmp_store);
440
0
    return method;
441
0
}
442
443
/*
444
 * evp_generic_fetch_from_prov() is special, and only returns methods from
445
 * the given provider.
446
 * This is meant to be used when one method needs to fetch an associated
447
 * method.
448
 */
449
void *evp_generic_fetch_from_prov(OSSL_PROVIDER *prov, int operation_id,
450
    const char *name, const char *properties,
451
    void *(*new_method)(int name_id,
452
        const OSSL_ALGORITHM *algodef,
453
        OSSL_PROVIDER *prov),
454
    int (*up_ref_method)(void *),
455
    void (*free_method)(void *))
456
218k
{
457
218k
    struct evp_method_data_st methdata;
458
218k
    void *method;
459
460
218k
    methdata.libctx = ossl_provider_libctx(prov);
461
218k
    methdata.tmp_store = NULL;
462
218k
    method = inner_evp_generic_fetch(&methdata, prov, operation_id,
463
218k
        0, name, properties,
464
218k
        new_method, up_ref_method, free_method);
465
218k
    dealloc_tmp_evp_method_store(methdata.tmp_store);
466
218k
    return method;
467
218k
}
468
469
int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx)
470
136
{
471
136
    OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
472
473
136
    if (store != NULL)
474
136
        return ossl_method_store_cache_flush_all(store);
475
0
    return 1;
476
136
}
477
478
int evp_method_store_remove_all_provided(const OSSL_PROVIDER *prov)
479
0
{
480
0
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
481
0
    OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
482
483
0
    if (store != NULL)
484
0
        return ossl_method_store_remove_all_provided(store, prov);
485
0
    return 1;
486
0
}
487
488
static int evp_set_parsed_default_properties(OSSL_LIB_CTX *libctx,
489
    OSSL_PROPERTY_LIST *def_prop,
490
    int loadconfig,
491
    int mirrored)
492
0
{
493
0
    OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
494
0
    OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
495
496
0
    if (plp != NULL && store != NULL) {
497
0
#ifndef FIPS_MODULE
498
0
        char *propstr = NULL;
499
0
        size_t strsz;
500
501
0
        if (mirrored) {
502
0
            if (ossl_global_properties_no_mirrored(libctx))
503
0
                return 0;
504
0
        } else {
505
            /*
506
             * These properties have been explicitly set on this libctx, so
507
             * don't allow any mirroring from a parent libctx.
508
             */
509
0
            ossl_global_properties_stop_mirroring(libctx);
510
0
        }
511
512
0
        strsz = ossl_property_list_to_string(libctx, def_prop, NULL, 0);
513
0
        if (strsz > 0)
514
0
            propstr = OPENSSL_malloc(strsz);
515
0
        if (propstr == NULL) {
516
0
            ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
517
0
            return 0;
518
0
        }
519
0
        if (ossl_property_list_to_string(libctx, def_prop, propstr,
520
0
                strsz)
521
0
            == 0) {
522
0
            OPENSSL_free(propstr);
523
0
            ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
524
0
            return 0;
525
0
        }
526
0
        ossl_provider_default_props_update(libctx, propstr);
527
0
        OPENSSL_free(propstr);
528
0
#endif
529
0
        ossl_property_free(*plp);
530
0
        *plp = def_prop;
531
0
        if (store != NULL)
532
0
            return ossl_method_store_cache_flush_all(store);
533
0
    }
534
0
    ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
535
0
    return 0;
536
0
}
537
538
int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
539
    int loadconfig, int mirrored)
540
0
{
541
0
    OSSL_PROPERTY_LIST *pl = NULL;
542
543
0
    if (propq != NULL && (pl = ossl_parse_query(libctx, propq, 1)) == NULL) {
544
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
545
0
        return 0;
546
0
    }
547
0
    if (!evp_set_parsed_default_properties(libctx, pl, loadconfig, mirrored)) {
548
0
        ossl_property_free(pl);
549
0
        return 0;
550
0
    }
551
0
    return 1;
552
0
}
553
554
int EVP_set_default_properties(OSSL_LIB_CTX *libctx, const char *propq)
555
0
{
556
0
    return evp_set_default_properties_int(libctx, propq, 1, 0);
557
0
}
558
559
static int evp_default_properties_merge(OSSL_LIB_CTX *libctx, const char *propq,
560
    int loadconfig)
561
0
{
562
0
    OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
563
0
    OSSL_PROPERTY_LIST *pl1, *pl2;
564
565
0
    if (propq == NULL)
566
0
        return 1;
567
0
    if (plp == NULL || *plp == NULL)
568
0
        return evp_set_default_properties_int(libctx, propq, 0, 0);
569
0
    if ((pl1 = ossl_parse_query(libctx, propq, 1)) == NULL) {
570
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
571
0
        return 0;
572
0
    }
573
0
    pl2 = ossl_property_merge(pl1, *plp);
574
0
    ossl_property_free(pl1);
575
0
    if (pl2 == NULL) {
576
0
        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
577
0
        return 0;
578
0
    }
579
0
    if (!evp_set_parsed_default_properties(libctx, pl2, 0, 0)) {
580
0
        ossl_property_free(pl2);
581
0
        return 0;
582
0
    }
583
0
    return 1;
584
0
}
585
586
static int evp_default_property_is_enabled(OSSL_LIB_CTX *libctx,
587
    const char *prop_name)
588
0
{
589
0
    OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, 1);
590
591
0
    return plp != NULL && ossl_property_is_enabled(libctx, prop_name, *plp);
592
0
}
593
594
int EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX *libctx)
595
0
{
596
0
    return evp_default_property_is_enabled(libctx, "fips");
597
0
}
598
599
int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable,
600
    int loadconfig)
601
0
{
602
0
    const char *query = (enable != 0) ? "fips=yes" : "-fips";
603
604
0
    return evp_default_properties_merge(libctx, query, loadconfig);
605
0
}
606
607
int EVP_default_properties_enable_fips(OSSL_LIB_CTX *libctx, int enable)
608
0
{
609
0
    return evp_default_properties_enable_fips_int(libctx, enable, 1);
610
0
}
611
612
char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig)
613
0
{
614
0
    OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
615
0
    char *propstr = NULL;
616
0
    size_t sz;
617
618
0
    if (plp == NULL)
619
0
        return OPENSSL_strdup("");
620
621
0
    sz = ossl_property_list_to_string(libctx, *plp, NULL, 0);
622
0
    if (sz == 0) {
623
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
624
0
        return NULL;
625
0
    }
626
627
0
    propstr = OPENSSL_malloc(sz);
628
0
    if (propstr == NULL) {
629
0
        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
630
0
        return NULL;
631
0
    }
632
0
    if (ossl_property_list_to_string(libctx, *plp, propstr, sz) == 0) {
633
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
634
0
        OPENSSL_free(propstr);
635
0
        return NULL;
636
0
    }
637
0
    return propstr;
638
0
}
639
640
struct filter_data_st {
641
    int operation_id;
642
    void (*user_fn)(void *method, void *arg);
643
    void *user_arg;
644
};
645
646
static void filter_on_operation_id(int id, void *method, void *arg)
647
27.6M
{
648
27.6M
    struct filter_data_st *data = arg;
649
650
27.6M
    if ((id & METHOD_ID_OPERATION_MASK) == data->operation_id)
651
5.24M
        data->user_fn(method, data->user_arg);
652
27.6M
}
653
654
void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
655
    void (*user_fn)(void *method, void *arg),
656
    void *user_arg,
657
    void *(*new_method)(int name_id,
658
        const OSSL_ALGORITHM *algodef,
659
        OSSL_PROVIDER *prov),
660
    int (*up_ref_method)(void *),
661
    void (*free_method)(void *))
662
278k
{
663
278k
    struct evp_method_data_st methdata;
664
278k
    struct filter_data_st data;
665
666
278k
    methdata.libctx = libctx;
667
278k
    methdata.tmp_store = NULL;
668
278k
    (void)inner_evp_generic_fetch(&methdata, NULL, operation_id, 0, NULL, NULL,
669
278k
        new_method, up_ref_method, free_method);
670
671
278k
    data.operation_id = operation_id;
672
278k
    data.user_fn = user_fn;
673
278k
    data.user_arg = user_arg;
674
278k
    if (methdata.tmp_store != NULL)
675
0
        ossl_method_store_do_all(methdata.tmp_store, &filter_on_operation_id,
676
0
            &data);
677
278k
    ossl_method_store_do_all(get_evp_method_store(libctx),
678
278k
        &filter_on_operation_id, &data);
679
278k
    dealloc_tmp_evp_method_store(methdata.tmp_store);
680
278k
}
681
682
int evp_is_a(OSSL_PROVIDER *prov, int number,
683
    const char *legacy_name, const char *name)
684
9.99M
{
685
    /*
686
     * For a |prov| that is NULL, the library context will be NULL
687
     */
688
9.99M
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
689
9.99M
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
690
691
9.99M
    if (prov == NULL)
692
401k
        number = ossl_namemap_name2num(namemap, legacy_name);
693
9.99M
    return ossl_namemap_name2num(namemap, name) == number;
694
9.99M
}
695
696
int evp_names_do_all(OSSL_PROVIDER *prov, int number,
697
    void (*fn)(const char *name, void *data),
698
    void *data)
699
2.57M
{
700
2.57M
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
701
2.57M
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
702
703
2.57M
    return ossl_namemap_doall_names(namemap, number, fn, data);
704
2.57M
}