Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/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/decoder.h"
21
#include "crypto/evp.h"    /* evp_local.h needs it */
22
#include "evp_local.h"
23
24
888k
#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
13.4M
{
58
13.4M
    if (store != NULL)
59
0
        ossl_method_store_free(store);
60
13.4M
}
61
62
static OSSL_METHOD_STORE *get_evp_method_store(OSSL_LIB_CTX *libctx)
63
21.1M
{
64
21.1M
    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX);
65
21.1M
}
66
67
static int reserve_evp_method_store(void *store, void *data)
68
3.42M
{
69
3.42M
    struct evp_method_data_st *methdata = data;
70
71
3.42M
    if (store == NULL
72
3.42M
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
73
0
        return 0;
74
75
3.42M
    return ossl_method_lock_store(store);
76
3.42M
}
77
78
static int unreserve_evp_method_store(void *store, void *data)
79
3.42M
{
80
3.42M
    struct evp_method_data_st *methdata = data;
81
82
3.42M
    if (store == NULL
83
3.42M
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
84
0
        return 0;
85
86
3.42M
    return ossl_method_unlock_store(store);
87
3.42M
}
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
63.6M
#define METHOD_ID_OPERATION_MASK        0x000000FF
107
#define METHOD_ID_OPERATION_MAX         ((1 << 8) - 1)
108
12.4M
#define METHOD_ID_NAME_MASK             0x7FFFFF00
109
12.4M
#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
12.4M
{
113
12.4M
    if (!ossl_assert(name_id > 0 && name_id <= METHOD_ID_NAME_MAX)
114
12.4M
        || !ossl_assert(operation_id > 0
115
12.4M
                        && operation_id <= METHOD_ID_OPERATION_MAX))
116
0
        return 0;
117
12.4M
    return (((name_id << METHOD_ID_NAME_OFFSET) & METHOD_ID_NAME_MASK)
118
12.4M
            | (operation_id & METHOD_ID_OPERATION_MASK));
119
12.4M
}
120
121
static void *get_evp_method_from_store(void *store, const OSSL_PROVIDER **prov,
122
                                       void *data)
123
1.78M
{
124
1.78M
    struct evp_method_data_st *methdata = data;
125
1.78M
    void *method = NULL;
126
1.78M
    int name_id;
127
1.78M
    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
1.78M
    if ((name_id = methdata->name_id) == 0 && methdata->names != NULL) {
135
874k
        OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
136
874k
        const char *names = methdata->names;
137
874k
        const char *q = strchr(names, NAME_SEPARATOR);
138
874k
        size_t l = (q == NULL ? strlen(names) : (size_t)(q - names));
139
140
874k
        if (namemap == 0)
141
0
            return NULL;
142
874k
        name_id = ossl_namemap_name2num_n(namemap, names, l);
143
874k
    }
144
145
1.78M
    if (name_id == 0
146
1.78M
        || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
147
1.38M
        return NULL;
148
149
399k
    if (store == NULL
150
399k
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
151
0
        return NULL;
152
153
399k
    if (!ossl_method_store_fetch(store, meth_id, methdata->propquery, prov,
154
399k
                                 &method))
155
397k
        return NULL;
156
2.85k
    return method;
157
399k
}
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
6.99k
{
164
6.99k
    struct evp_method_data_st *methdata = data;
165
6.99k
    OSSL_NAMEMAP *namemap;
166
6.99k
    int name_id;
167
6.99k
    uint32_t meth_id;
168
6.99k
    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
6.99k
    if (names != NULL) {
177
6.99k
        const char *q = strchr(names, NAME_SEPARATOR);
178
179
6.99k
        l = (q == NULL ? strlen(names) : (size_t)(q - names));
180
6.99k
    }
181
182
6.99k
    if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
183
6.99k
        || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0
184
6.99k
        || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
185
0
        return 0;
186
187
6.99k
    if (store == NULL
188
6.99k
        && (store = get_evp_method_store(methdata->libctx)) == NULL)
189
0
        return 0;
190
191
6.99k
    return ossl_method_store_add(store, prov, meth_id, propdef, method,
192
6.99k
                                 methdata->refcnt_up_method,
193
6.99k
                                 methdata->destruct_method);
194
6.99k
}
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
6.99k
{
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
6.99k
    struct evp_method_data_st *methdata = data;
211
6.99k
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
212
6.99k
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
213
6.99k
    const char *names = algodef->algorithm_names;
214
6.99k
    int name_id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
215
6.99k
    void *method;
216
217
6.99k
    if (name_id == 0)
218
0
        return NULL;
219
220
6.99k
    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
6.99k
    if (method == NULL)
228
0
        methdata->flag_construct_error_occurred = 1;
229
230
6.99k
    return method;
231
6.99k
}
232
233
static void destruct_evp_method(void *method, void *data)
234
6.99k
{
235
6.99k
    struct evp_method_data_st *methdata = data;
236
237
6.99k
    methdata->destruct_method(method);
238
6.99k
}
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
5.95M
{
250
5.95M
    OSSL_METHOD_STORE *store = get_evp_method_store(methdata->libctx);
251
5.95M
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
252
5.95M
    const char *const propq = properties != NULL ? properties : "";
253
5.95M
    uint32_t meth_id = 0;
254
5.95M
    void *method = NULL;
255
5.95M
    int unsupported, name_id;
256
257
5.95M
    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
5.95M
    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
5.95M
    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
5.95M
    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
5.95M
    unsupported = name_id == 0;
292
293
5.95M
    if (meth_id == 0
294
5.95M
        || !ossl_method_store_cache_get(store, prov, meth_id, propq, &method)) {
295
819k
        OSSL_METHOD_CONSTRUCT_METHOD mcm = {
296
819k
            get_tmp_evp_method_store,
297
819k
            reserve_evp_method_store,
298
819k
            unreserve_evp_method_store,
299
819k
            get_evp_method_from_store,
300
819k
            put_evp_method_in_store,
301
819k
            construct_evp_method,
302
819k
            destruct_evp_method
303
819k
        };
304
305
819k
        methdata->operation_id = operation_id;
306
819k
        methdata->name_id = name_id;
307
819k
        methdata->names = name;
308
819k
        methdata->propquery = propq;
309
819k
        methdata->method_from_algorithm = new_method;
310
819k
        methdata->refcnt_up_method = up_ref_method;
311
819k
        methdata->destruct_method = free_method;
312
819k
        methdata->flag_construct_error_occurred = 0;
313
819k
        if ((method = ossl_method_construct(methdata->libctx, operation_id,
314
819k
                                            &prov, 0 /* !force_cache */,
315
819k
                                            &mcm, methdata)) != NULL) {
316
            /*
317
             * If construction did create a method for us, we know that
318
             * there is a correct name_id and meth_id, since those have
319
             * already been calculated in get_evp_method_from_store() and
320
             * put_evp_method_in_store() above.
321
             * Note that there is a corner case here, in which, if a user
322
             * passes a name of the form name1:name2:..., then the construction
323
             * will create a method against all names, but the lookup will fail
324
             * as ossl_namemap_name2num treats the name string as a single name
325
             * rather than introducing new features where in the EVP_<obj>_fetch
326
             * parses the string and querys for each, return an error.
327
             */
328
349
            if (name_id == 0)
329
19
                name_id = ossl_namemap_name2num(namemap, name);
330
349
            if (name_id == 0) {
331
0
                ERR_raise_data(ERR_LIB_EVP, ERR_R_FETCH_FAILED,
332
0
                               "Algorithm %s cannot be found", name);
333
0
                free_method(method);
334
0
                method = NULL;
335
349
            } else {
336
349
                meth_id = evp_method_id(name_id, operation_id);
337
349
                if (meth_id != 0)
338
349
                    ossl_method_store_cache_set(store, prov, meth_id, propq,
339
349
                                                method, up_ref_method, free_method);
340
349
            }
341
349
        }
342
343
        /*
344
         * If we never were in the constructor, the algorithm to be fetched
345
         * is unsupported.
346
         */
347
819k
        unsupported = !methdata->flag_construct_error_occurred;
348
819k
    }
349
350
5.95M
    if ((name_id != 0 || name != NULL) && method == NULL) {
351
585k
        int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
352
353
585k
        if (name == NULL)
354
0
            name = ossl_namemap_num2name(namemap, name_id, 0);
355
585k
        ERR_raise_data(ERR_LIB_EVP, code,
356
585k
                       "%s, Algorithm (%s : %d), Properties (%s)",
357
585k
                       ossl_lib_ctx_get_descriptor(methdata->libctx),
358
585k
                       name == NULL ? "<null>" : name, name_id,
359
585k
                       properties == NULL ? "<null>" : properties);
360
585k
    }
361
362
5.95M
    return method;
363
5.95M
}
364
365
void *evp_generic_fetch(OSSL_LIB_CTX *libctx, int operation_id,
366
                        const char *name, const char *properties,
367
                        void *(*new_method)(int name_id,
368
                                            const OSSL_ALGORITHM *algodef,
369
                                            OSSL_PROVIDER *prov),
370
                        int (*up_ref_method)(void *),
371
                        void (*free_method)(void *))
372
12.7M
{
373
12.7M
    struct evp_method_data_st methdata;
374
12.7M
    void *method;
375
376
12.7M
    methdata.libctx = libctx;
377
12.7M
    methdata.tmp_store = NULL;
378
12.7M
    method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
379
12.7M
                                     name, properties,
380
12.7M
                                     new_method, up_ref_method, free_method);
381
12.7M
    dealloc_tmp_evp_method_store(methdata.tmp_store);
382
12.7M
    return method;
383
12.7M
}
384
385
/*
386
 * evp_generic_fetch_from_prov() is special, and only returns methods from
387
 * the given provider.
388
 * This is meant to be used when one method needs to fetch an associated
389
 * method.
390
 */
391
void *evp_generic_fetch_from_prov(OSSL_PROVIDER *prov, int operation_id,
392
                                  const char *name, const char *properties,
393
                                  void *(*new_method)(int name_id,
394
                                                      const OSSL_ALGORITHM *algodef,
395
                                                      OSSL_PROVIDER *prov),
396
                                  int (*up_ref_method)(void *),
397
                                  void (*free_method)(void *))
398
122k
{
399
122k
    struct evp_method_data_st methdata;
400
122k
    void *method;
401
402
122k
    methdata.libctx = ossl_provider_libctx(prov);
403
122k
    methdata.tmp_store = NULL;
404
122k
    method = inner_evp_generic_fetch(&methdata, prov, operation_id,
405
122k
                                     name, properties,
406
122k
                                     new_method, up_ref_method, free_method);
407
122k
    dealloc_tmp_evp_method_store(methdata.tmp_store);
408
122k
    return method;
409
122k
}
410
411
int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx)
412
60
{
413
60
    OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
414
415
60
    if (store != NULL)
416
60
        return ossl_method_store_cache_flush_all(store);
417
0
    return 1;
418
60
}
419
420
int evp_method_store_remove_all_provided(const OSSL_PROVIDER *prov)
421
0
{
422
0
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
423
0
    OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
424
425
0
    if (store != NULL)
426
0
        return ossl_method_store_remove_all_provided(store, prov);
427
0
    return 1;
428
0
}
429
430
static int evp_set_parsed_default_properties(OSSL_LIB_CTX *libctx,
431
                                             OSSL_PROPERTY_LIST *def_prop,
432
                                             int loadconfig,
433
                                             int mirrored)
434
0
{
435
0
    OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
436
0
    OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
437
438
0
    if (plp != NULL && store != NULL) {
439
0
        int ret;
440
0
#ifndef FIPS_MODULE
441
0
        char *propstr = NULL;
442
0
        size_t strsz;
443
444
0
        if (mirrored) {
445
0
            if (ossl_global_properties_no_mirrored(libctx))
446
0
                return 0;
447
0
        } else {
448
            /*
449
             * These properties have been explicitly set on this libctx, so
450
             * don't allow any mirroring from a parent libctx.
451
             */
452
0
            ossl_global_properties_stop_mirroring(libctx);
453
0
        }
454
455
0
        strsz = ossl_property_list_to_string(libctx, def_prop, NULL, 0);
456
0
        if (strsz > 0)
457
0
            propstr = OPENSSL_malloc(strsz);
458
0
        if (propstr == NULL) {
459
0
            ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
460
0
            return 0;
461
0
        }
462
0
        if (ossl_property_list_to_string(libctx, def_prop, propstr,
463
0
                                         strsz) == 0) {
464
0
            OPENSSL_free(propstr);
465
0
            ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
466
0
            return 0;
467
0
        }
468
0
        ossl_provider_default_props_update(libctx, propstr);
469
0
        OPENSSL_free(propstr);
470
0
#endif
471
0
        ossl_property_free(*plp);
472
0
        *plp = def_prop;
473
474
0
        ret = ossl_method_store_cache_flush_all(store);
475
0
#ifndef FIPS_MODULE
476
0
        ossl_decoder_cache_flush(libctx);
477
0
#endif
478
0
        return ret;
479
0
    }
480
0
    ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
481
0
    return 0;
482
0
}
483
484
int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
485
                                   int loadconfig, int mirrored)
486
0
{
487
0
    OSSL_PROPERTY_LIST *pl = NULL;
488
489
0
    if (propq != NULL && (pl = ossl_parse_query(libctx, propq, 1)) == NULL) {
490
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
491
0
        return 0;
492
0
    }
493
0
    if (!evp_set_parsed_default_properties(libctx, pl, loadconfig, mirrored)) {
494
0
        ossl_property_free(pl);
495
0
        return 0;
496
0
    }
497
0
    return 1;
498
0
}
499
500
int EVP_set_default_properties(OSSL_LIB_CTX *libctx, const char *propq)
501
0
{
502
0
    return evp_set_default_properties_int(libctx, propq, 1, 0);
503
0
}
504
505
static int evp_default_properties_merge(OSSL_LIB_CTX *libctx, const char *propq,
506
                                        int loadconfig)
507
0
{
508
0
    OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
509
0
    OSSL_PROPERTY_LIST *pl1, *pl2;
510
511
0
    if (propq == NULL)
512
0
        return 1;
513
0
    if (plp == NULL || *plp == NULL)
514
0
        return evp_set_default_properties_int(libctx, propq, 0, 0);
515
0
    if ((pl1 = ossl_parse_query(libctx, propq, 1)) == NULL) {
516
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
517
0
        return 0;
518
0
    }
519
0
    pl2 = ossl_property_merge(pl1, *plp);
520
0
    ossl_property_free(pl1);
521
0
    if (pl2 == NULL) {
522
0
        ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
523
0
        return 0;
524
0
    }
525
0
    if (!evp_set_parsed_default_properties(libctx, pl2, 0, 0)) {
526
0
        ossl_property_free(pl2);
527
0
        return 0;
528
0
    }
529
0
    return 1;
530
0
}
531
532
static int evp_default_property_is_enabled(OSSL_LIB_CTX *libctx,
533
                                           const char *prop_name)
534
0
{
535
0
    OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, 1);
536
537
0
    return plp != NULL && ossl_property_is_enabled(libctx, prop_name, *plp);
538
0
}
539
540
int EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX *libctx)
541
0
{
542
0
    return evp_default_property_is_enabled(libctx, "fips");
543
0
}
544
545
int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable,
546
                                           int loadconfig)
547
0
{
548
0
    const char *query = (enable != 0) ? "fips=yes" : "-fips";
549
550
0
    return evp_default_properties_merge(libctx, query, loadconfig);
551
0
}
552
553
int EVP_default_properties_enable_fips(OSSL_LIB_CTX *libctx, int enable)
554
0
{
555
0
    return evp_default_properties_enable_fips_int(libctx, enable, 1);
556
0
}
557
558
char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig)
559
0
{
560
0
    OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
561
0
    char *propstr = NULL;
562
0
    size_t sz;
563
564
0
    if (plp == NULL)
565
0
        return OPENSSL_strdup("");
566
567
0
    sz = ossl_property_list_to_string(libctx, *plp, NULL, 0);
568
0
    if (sz == 0) {
569
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
570
0
        return NULL;
571
0
    }
572
573
0
    propstr = OPENSSL_malloc(sz);
574
0
    if (propstr == NULL)
575
0
        return NULL;
576
0
    if (ossl_property_list_to_string(libctx, *plp, propstr, sz) == 0) {
577
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
578
0
        OPENSSL_free(propstr);
579
0
        return NULL;
580
0
    }
581
0
    return propstr;
582
0
}
583
584
struct filter_data_st {
585
    int operation_id;
586
    void (*user_fn)(void *method, void *arg);
587
    void *user_arg;
588
};
589
590
static void filter_on_operation_id(int id, void *method, void *arg)
591
51.1M
{
592
51.1M
    struct filter_data_st *data = arg;
593
594
51.1M
    if ((id & METHOD_ID_OPERATION_MASK) == data->operation_id)
595
9.19M
        data->user_fn(method, data->user_arg);
596
51.1M
}
597
598
void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
599
                        void (*user_fn)(void *method, void *arg),
600
                        void *user_arg,
601
                        void *(*new_method)(int name_id,
602
                                            const OSSL_ALGORITHM *algodef,
603
                                            OSSL_PROVIDER *prov),
604
                        int (*up_ref_method)(void *),
605
                        void (*free_method)(void *))
606
506k
{
607
506k
    struct evp_method_data_st methdata;
608
506k
    struct filter_data_st data;
609
610
506k
    methdata.libctx = libctx;
611
506k
    methdata.tmp_store = NULL;
612
506k
    (void)inner_evp_generic_fetch(&methdata, NULL, operation_id, NULL, NULL,
613
506k
                                  new_method, up_ref_method, free_method);
614
615
506k
    data.operation_id = operation_id;
616
506k
    data.user_fn = user_fn;
617
506k
    data.user_arg = user_arg;
618
506k
    if (methdata.tmp_store != NULL)
619
0
        ossl_method_store_do_all(methdata.tmp_store, &filter_on_operation_id,
620
0
                                 &data);
621
506k
    ossl_method_store_do_all(get_evp_method_store(libctx),
622
506k
                             &filter_on_operation_id, &data);
623
506k
    dealloc_tmp_evp_method_store(methdata.tmp_store);
624
506k
}
625
626
int evp_is_a(OSSL_PROVIDER *prov, int number,
627
             const char *legacy_name, const char *name)
628
8.26M
{
629
    /*
630
     * For a |prov| that is NULL, the library context will be NULL
631
     */
632
8.26M
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
633
8.26M
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
634
635
8.26M
    if (prov == NULL)
636
285k
        number = ossl_namemap_name2num(namemap, legacy_name);
637
8.26M
    return ossl_namemap_name2num(namemap, name) == number;
638
8.26M
}
639
640
int evp_names_do_all(OSSL_PROVIDER *prov, int number,
641
                     void (*fn)(const char *name, void *data),
642
                     void *data)
643
3.15M
{
644
3.15M
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
645
3.15M
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
646
647
3.15M
    return ossl_namemap_doall_names(namemap, number, fn, data);
648
3.15M
}