Coverage Report

Created: 2025-12-31 06:58

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