Coverage Report

Created: 2023-06-08 06:40

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