Coverage Report

Created: 2025-06-13 06:58

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