Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/encode_decode/decoder_meth.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2025 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 <openssl/core.h>
11
#include <openssl/core_dispatch.h>
12
#include <openssl/decoder.h>
13
#include <openssl/ui.h>
14
#include "internal/core.h"
15
#include "internal/namemap.h"
16
#include "internal/property.h"
17
#include "internal/provider.h"
18
#include "crypto/decoder.h"
19
#include "encoder_local.h"
20
#include "crypto/context.h"
21
22
/*
23
 * Decoder can have multiple names, separated with colons in a name string
24
 */
25
7.17k
#define NAME_SEPARATOR ':'
26
27
static void ossl_decoder_free(void *data)
28
1.67k
{
29
1.67k
    OSSL_DECODER_free(data);
30
1.67k
}
31
32
static int ossl_decoder_up_ref(void *data)
33
2.50k
{
34
2.50k
    return OSSL_DECODER_up_ref(data);
35
2.50k
}
36
37
/* Simple method structure constructor and destructor */
38
static OSSL_DECODER *ossl_decoder_new(void)
39
3.34k
{
40
3.34k
    OSSL_DECODER *decoder = NULL;
41
42
3.34k
    if ((decoder = OPENSSL_zalloc(sizeof(*decoder))) == NULL)
43
0
        return NULL;
44
3.34k
    if (!CRYPTO_NEW_REF(&decoder->base.refcnt, 1)) {
45
0
        OSSL_DECODER_free(decoder);
46
0
        return NULL;
47
0
    }
48
49
3.34k
    return decoder;
50
3.34k
}
51
52
int OSSL_DECODER_up_ref(OSSL_DECODER *decoder)
53
25.4M
{
54
25.4M
    int ref = 0;
55
56
25.4M
    CRYPTO_UP_REF(&decoder->base.refcnt, &ref);
57
25.4M
    return 1;
58
25.4M
}
59
60
void OSSL_DECODER_free(OSSL_DECODER *decoder)
61
25.4M
{
62
25.4M
    int ref = 0;
63
64
25.4M
    if (decoder == NULL)
65
0
        return;
66
67
25.4M
    CRYPTO_DOWN_REF(&decoder->base.refcnt, &ref);
68
25.4M
    if (ref > 0)
69
25.4M
        return;
70
2.75k
    OPENSSL_free(decoder->base.name);
71
2.75k
    ossl_property_free(decoder->base.parsed_propdef);
72
2.75k
    ossl_provider_free(decoder->base.prov);
73
2.75k
    CRYPTO_FREE_REF(&decoder->base.refcnt);
74
2.75k
    OPENSSL_free(decoder);
75
2.75k
}
76
77
/* Data to be passed through ossl_method_construct() */
78
struct decoder_data_st {
79
    OSSL_LIB_CTX *libctx;
80
    int id; /* For get_decoder_from_store() */
81
    const char *names; /* For get_decoder_from_store() */
82
    const char *propquery; /* For get_decoder_from_store() */
83
84
    OSSL_METHOD_STORE *tmp_store; /* For get_tmp_decoder_store() */
85
86
    unsigned int flag_construct_error_occurred : 1;
87
};
88
89
/*
90
 * Generic routines to fetch / create DECODER methods with
91
 * ossl_method_construct()
92
 */
93
94
/* Temporary decoder method store, constructor and destructor */
95
static void *get_tmp_decoder_store(void *data)
96
0
{
97
0
    struct decoder_data_st *methdata = data;
98
99
0
    if (methdata->tmp_store == NULL)
100
0
        methdata->tmp_store = ossl_method_store_new(methdata->libctx);
101
0
    return methdata->tmp_store;
102
0
}
103
104
static void dealloc_tmp_decoder_store(void *store)
105
523k
{
106
523k
    if (store != NULL)
107
0
        ossl_method_store_free(store);
108
523k
}
109
110
/* Get the permanent decoder store */
111
static OSSL_METHOD_STORE *get_decoder_store(OSSL_LIB_CTX *libctx)
112
2.89M
{
113
2.89M
    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_STORE_INDEX);
114
2.89M
}
115
116
static int reserve_decoder_store(void *store, void *data)
117
920k
{
118
920k
    struct decoder_data_st *methdata = data;
119
120
920k
    if (store == NULL
121
920k
        && (store = get_decoder_store(methdata->libctx)) == NULL)
122
0
        return 0;
123
124
920k
    return ossl_method_lock_store(store);
125
920k
}
126
127
static int unreserve_decoder_store(void *store, void *data)
128
920k
{
129
920k
    struct decoder_data_st *methdata = data;
130
131
920k
    if (store == NULL
132
920k
        && (store = get_decoder_store(methdata->libctx)) == NULL)
133
0
        return 0;
134
135
920k
    return ossl_method_unlock_store(store);
136
920k
}
137
138
/* Get decoder methods from a store, or put one in */
139
static void *get_decoder_from_store(void *store, const OSSL_PROVIDER **prov,
140
    void *data)
141
523k
{
142
523k
    struct decoder_data_st *methdata = data;
143
523k
    void *method = NULL;
144
523k
    int id;
145
146
    /*
147
     * get_decoder_from_store() is only called to try and get the method
148
     * that OSSL_DECODER_fetch() is asking for, and the name or name id are
149
     * passed via methdata.
150
     */
151
523k
    if ((id = methdata->id) == 0 && methdata->names != NULL) {
152
0
        OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
153
0
        const char *names = methdata->names;
154
0
        const char *q = strchr(names, NAME_SEPARATOR);
155
0
        size_t l = (q == NULL ? strlen(names) : (size_t)(q - names));
156
157
0
        if (namemap == 0)
158
0
            return NULL;
159
0
        id = ossl_namemap_name2num_n(namemap, names, l);
160
0
    }
161
162
523k
    if (id == 0)
163
523k
        return NULL;
164
165
0
    if (store == NULL
166
0
        && (store = get_decoder_store(methdata->libctx)) == NULL)
167
0
        return NULL;
168
169
0
    if (!ossl_method_store_fetch(store, id, methdata->propquery, prov, &method))
170
0
        return NULL;
171
0
    return method;
172
0
}
173
174
static int put_decoder_in_store(void *store, void *method,
175
    const OSSL_PROVIDER *prov,
176
    const char *names, const char *propdef,
177
    void *data)
178
3.58k
{
179
3.58k
    struct decoder_data_st *methdata = data;
180
3.58k
    OSSL_NAMEMAP *namemap;
181
3.58k
    int id;
182
3.58k
    size_t l = 0;
183
184
    /*
185
     * put_decoder_in_store() is only called with an OSSL_DECODER method that
186
     * was successfully created by construct_decoder() below, which means that
187
     * all the names should already be stored in the namemap with the same
188
     * numeric identity, so just use the first to get that identity.
189
     */
190
3.58k
    if (names != NULL) {
191
3.58k
        const char *q = strchr(names, NAME_SEPARATOR);
192
193
3.58k
        l = (q == NULL ? strlen(names) : (size_t)(q - names));
194
3.58k
    }
195
196
3.58k
    if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
197
3.58k
        || (id = ossl_namemap_name2num_n(namemap, names, l)) == 0)
198
0
        return 0;
199
200
3.58k
    if (store == NULL && (store = get_decoder_store(methdata->libctx)) == NULL)
201
0
        return 0;
202
203
3.58k
    return ossl_method_store_add(store, prov, id, propdef, method,
204
3.58k
        ossl_decoder_up_ref,
205
3.58k
        ossl_decoder_free);
206
3.58k
}
207
208
/* Create and populate a decoder method */
209
void *ossl_decoder_from_algorithm(int id, const OSSL_ALGORITHM *algodef,
210
    OSSL_PROVIDER *prov)
211
3.58k
{
212
3.58k
    OSSL_DECODER *decoder = NULL;
213
3.58k
    const OSSL_DISPATCH *fns = algodef->implementation;
214
3.58k
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
215
216
3.58k
    if ((decoder = ossl_decoder_new()) == NULL)
217
0
        return NULL;
218
3.58k
    decoder->base.id = id;
219
3.58k
    if ((decoder->base.name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
220
0
        OSSL_DECODER_free(decoder);
221
0
        return NULL;
222
0
    }
223
3.58k
    decoder->base.algodef = algodef;
224
3.58k
    if ((decoder->base.parsed_propdef
225
3.58k
            = ossl_parse_property(libctx, algodef->property_definition))
226
3.58k
        == NULL) {
227
0
        OSSL_DECODER_free(decoder);
228
0
        return NULL;
229
0
    }
230
231
27.6k
    for (; fns->function_id != 0; fns++) {
232
24.0k
        switch (fns->function_id) {
233
3.58k
        case OSSL_FUNC_DECODER_NEWCTX:
234
3.58k
            if (decoder->newctx == NULL)
235
3.58k
                decoder->newctx = OSSL_FUNC_decoder_newctx(fns);
236
3.58k
            break;
237
3.58k
        case OSSL_FUNC_DECODER_FREECTX:
238
3.58k
            if (decoder->freectx == NULL)
239
3.58k
                decoder->freectx = OSSL_FUNC_decoder_freectx(fns);
240
3.58k
            break;
241
0
        case OSSL_FUNC_DECODER_GET_PARAMS:
242
0
            if (decoder->get_params == NULL)
243
0
                decoder->get_params = OSSL_FUNC_decoder_get_params(fns);
244
0
            break;
245
0
        case OSSL_FUNC_DECODER_GETTABLE_PARAMS:
246
0
            if (decoder->gettable_params == NULL)
247
0
                decoder->gettable_params = OSSL_FUNC_decoder_gettable_params(fns);
248
0
            break;
249
3.21k
        case OSSL_FUNC_DECODER_SET_CTX_PARAMS:
250
3.21k
            if (decoder->set_ctx_params == NULL)
251
3.21k
                decoder->set_ctx_params = OSSL_FUNC_decoder_set_ctx_params(fns);
252
3.21k
            break;
253
3.21k
        case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS:
254
3.21k
            if (decoder->settable_ctx_params == NULL)
255
3.21k
                decoder->settable_ctx_params = OSSL_FUNC_decoder_settable_ctx_params(fns);
256
3.21k
            break;
257
3.40k
        case OSSL_FUNC_DECODER_DOES_SELECTION:
258
3.40k
            if (decoder->does_selection == NULL)
259
3.40k
                decoder->does_selection = OSSL_FUNC_decoder_does_selection(fns);
260
3.40k
            break;
261
3.58k
        case OSSL_FUNC_DECODER_DECODE:
262
3.58k
            if (decoder->decode == NULL)
263
3.58k
                decoder->decode = OSSL_FUNC_decoder_decode(fns);
264
3.58k
            break;
265
3.40k
        case OSSL_FUNC_DECODER_EXPORT_OBJECT:
266
3.40k
            if (decoder->export_object == NULL)
267
3.40k
                decoder->export_object = OSSL_FUNC_decoder_export_object(fns);
268
3.40k
            break;
269
24.0k
        }
270
24.0k
    }
271
    /*
272
     * Try to check that the method is sensible.
273
     * If you have a constructor, you must have a destructor and vice versa.
274
     * You must have at least one of the encoding driver functions.
275
     */
276
3.58k
    if (!((decoder->newctx == NULL && decoder->freectx == NULL)
277
3.58k
            || (decoder->newctx != NULL && decoder->freectx != NULL))
278
3.58k
        || decoder->decode == NULL) {
279
0
        OSSL_DECODER_free(decoder);
280
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
281
0
        return NULL;
282
0
    }
283
284
3.58k
    if (prov != NULL && !ossl_provider_up_ref(prov)) {
285
0
        OSSL_DECODER_free(decoder);
286
0
        return NULL;
287
0
    }
288
289
3.58k
    decoder->base.prov = prov;
290
3.58k
    return decoder;
291
3.58k
}
292
293
/*
294
 * The core fetching functionality passes the names of the implementation.
295
 * This function is responsible to getting an identity number for them,
296
 * then call ossl_decoder_from_algorithm() with that identity number.
297
 */
298
static void *construct_decoder(const OSSL_ALGORITHM *algodef,
299
    OSSL_PROVIDER *prov, void *data)
300
3.58k
{
301
    /*
302
     * This function is only called if get_decoder_from_store() returned
303
     * NULL, so it's safe to say that of all the spots to create a new
304
     * namemap entry, this is it.  Should the name already exist there, we
305
     * know that ossl_namemap_add() will return its corresponding number.
306
     */
307
3.58k
    struct decoder_data_st *methdata = data;
308
3.58k
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
309
3.58k
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
310
3.58k
    const char *names = algodef->algorithm_names;
311
3.58k
    int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
312
3.58k
    void *method = NULL;
313
314
3.58k
    if (id != 0)
315
3.58k
        method = ossl_decoder_from_algorithm(id, algodef, prov);
316
317
    /*
318
     * Flag to indicate that there was actual construction errors.  This
319
     * helps inner_evp_generic_fetch() determine what error it should
320
     * record on inaccessible algorithms.
321
     */
322
3.58k
    if (method == NULL)
323
0
        methdata->flag_construct_error_occurred = 1;
324
325
3.58k
    return method;
326
3.58k
}
327
328
/* Intermediary function to avoid ugly casts, used below */
329
static void destruct_decoder(void *method, void *data)
330
3.58k
{
331
3.58k
    OSSL_DECODER_free(method);
332
3.58k
}
333
334
static int up_ref_decoder(void *method)
335
0
{
336
0
    return OSSL_DECODER_up_ref(method);
337
0
}
338
339
static void free_decoder(void *method)
340
0
{
341
0
    OSSL_DECODER_free(method);
342
0
}
343
344
/* Fetching support.  Can fetch by numeric identity or by name */
345
static OSSL_DECODER *
346
inner_ossl_decoder_fetch(struct decoder_data_st *methdata,
347
    const char *name, const char *properties)
348
17.4k
{
349
17.4k
    OSSL_METHOD_STORE *store = get_decoder_store(methdata->libctx);
350
17.4k
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
351
17.4k
    const char *const propq = properties != NULL ? properties : "";
352
17.4k
    void *method = NULL;
353
17.4k
    int unsupported, id;
354
355
17.4k
    if (store == NULL || namemap == NULL) {
356
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_INVALID_ARGUMENT);
357
0
        return NULL;
358
0
    }
359
360
17.4k
    id = name != NULL ? ossl_namemap_name2num(namemap, name) : 0;
361
362
    /*
363
     * If we haven't found the name yet, chances are that the algorithm to
364
     * be fetched is unsupported.
365
     */
366
17.4k
    unsupported = id == 0;
367
368
17.4k
    if (id == 0
369
17.4k
        || !ossl_method_store_cache_get(store, NULL, id, propq, &method)) {
370
17.4k
        OSSL_METHOD_CONSTRUCT_METHOD mcm = {
371
17.4k
            get_tmp_decoder_store,
372
17.4k
            reserve_decoder_store,
373
17.4k
            unreserve_decoder_store,
374
17.4k
            get_decoder_from_store,
375
17.4k
            put_decoder_in_store,
376
17.4k
            construct_decoder,
377
17.4k
            destruct_decoder
378
17.4k
        };
379
17.4k
        OSSL_PROVIDER *prov = NULL;
380
381
17.4k
        methdata->id = id;
382
17.4k
        methdata->names = name;
383
17.4k
        methdata->propquery = propq;
384
17.4k
        methdata->flag_construct_error_occurred = 0;
385
17.4k
        if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_DECODER,
386
17.4k
                 &prov, 0 /* !force_cache */,
387
17.4k
                 &mcm, methdata))
388
17.4k
            != NULL) {
389
            /*
390
             * If construction did create a method for us, we know that
391
             * there is a correct name_id and meth_id, since those have
392
             * already been calculated in get_decoder_from_store() and
393
             * put_decoder_in_store() above.
394
             */
395
0
            if (id == 0 && name != NULL)
396
0
                id = ossl_namemap_name2num(namemap, name);
397
0
            if (id != 0)
398
0
                ossl_method_store_cache_set(store, prov, id, propq, method,
399
0
                    up_ref_decoder, free_decoder);
400
0
        }
401
402
        /*
403
         * If we never were in the constructor, the algorithm to be fetched
404
         * is unsupported.
405
         */
406
17.4k
        unsupported = !methdata->flag_construct_error_occurred;
407
17.4k
    }
408
409
17.4k
    if ((id != 0 || name != NULL) && method == NULL) {
410
0
        int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
411
412
0
        if (name == NULL)
413
0
            name = ossl_namemap_num2name(namemap, id, 0);
414
0
        ERR_raise_data(ERR_LIB_OSSL_DECODER, code,
415
0
            "%s, Name (%s : %d), Properties (%s)",
416
0
            ossl_lib_ctx_get_descriptor(methdata->libctx),
417
0
            name == NULL ? "<null>" : name, id,
418
0
            properties == NULL ? "<null>" : properties);
419
0
    }
420
421
17.4k
    return method;
422
17.4k
}
423
424
OSSL_DECODER *OSSL_DECODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
425
    const char *properties)
426
0
{
427
0
    struct decoder_data_st methdata;
428
0
    void *method;
429
430
0
    methdata.libctx = libctx;
431
0
    methdata.tmp_store = NULL;
432
0
    method = inner_ossl_decoder_fetch(&methdata, name, properties);
433
0
    dealloc_tmp_decoder_store(methdata.tmp_store);
434
0
    return method;
435
0
}
436
437
int ossl_decoder_store_cache_flush(OSSL_LIB_CTX *libctx)
438
136
{
439
136
    OSSL_METHOD_STORE *store = get_decoder_store(libctx);
440
441
136
    if (store != NULL)
442
136
        return ossl_method_store_cache_flush_all(store);
443
0
    return 1;
444
136
}
445
446
int ossl_decoder_store_remove_all_provided(const OSSL_PROVIDER *prov)
447
0
{
448
0
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
449
0
    OSSL_METHOD_STORE *store = get_decoder_store(libctx);
450
451
0
    if (store != NULL)
452
0
        return ossl_method_store_remove_all_provided(store, prov);
453
0
    return 1;
454
0
}
455
456
/*
457
 * Library of basic method functions
458
 */
459
460
const OSSL_PROVIDER *OSSL_DECODER_get0_provider(const OSSL_DECODER *decoder)
461
204M
{
462
204M
    if (!ossl_assert(decoder != NULL)) {
463
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
464
0
        return 0;
465
0
    }
466
467
204M
    return decoder->base.prov;
468
204M
}
469
470
const char *OSSL_DECODER_get0_properties(const OSSL_DECODER *decoder)
471
0
{
472
0
    if (!ossl_assert(decoder != NULL)) {
473
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
474
0
        return 0;
475
0
    }
476
477
0
    return decoder->base.algodef->property_definition;
478
0
}
479
480
const OSSL_PROPERTY_LIST *
481
ossl_decoder_parsed_properties(const OSSL_DECODER *decoder)
482
3.42M
{
483
3.42M
    if (!ossl_assert(decoder != NULL)) {
484
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
485
0
        return 0;
486
0
    }
487
488
3.42M
    return decoder->base.parsed_propdef;
489
3.42M
}
490
491
int ossl_decoder_get_number(const OSSL_DECODER *decoder)
492
5.18M
{
493
5.18M
    if (!ossl_assert(decoder != NULL)) {
494
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
495
0
        return 0;
496
0
    }
497
498
5.18M
    return decoder->base.id;
499
5.18M
}
500
501
const char *OSSL_DECODER_get0_name(const OSSL_DECODER *decoder)
502
0
{
503
0
    return decoder->base.name;
504
0
}
505
506
const char *OSSL_DECODER_get0_description(const OSSL_DECODER *decoder)
507
0
{
508
0
    return decoder->base.algodef->algorithm_description;
509
0
}
510
511
int OSSL_DECODER_is_a(const OSSL_DECODER *decoder, const char *name)
512
231M
{
513
231M
    if (decoder->base.prov != NULL) {
514
231M
        OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
515
231M
        OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
516
517
231M
        return ossl_namemap_name2num(namemap, name) == decoder->base.id;
518
231M
    }
519
0
    return 0;
520
231M
}
521
522
static int resolve_name(OSSL_DECODER *decoder, const char *name)
523
4.78M
{
524
4.78M
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
525
4.78M
    OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
526
527
4.78M
    return ossl_namemap_name2num(namemap, name);
528
4.78M
}
529
530
int ossl_decoder_fast_is_a(OSSL_DECODER *decoder, const char *name, int *id_cache)
531
5.48M
{
532
5.48M
    int id = *id_cache;
533
534
5.48M
    if (id <= 0)
535
4.78M
        *id_cache = id = resolve_name(decoder, name);
536
537
5.48M
    return id > 0 && ossl_decoder_get_number(decoder) == id;
538
5.48M
}
539
540
struct do_one_data_st {
541
    void (*user_fn)(OSSL_DECODER *decoder, void *arg);
542
    void *user_arg;
543
};
544
545
static void do_one(ossl_unused int id, void *method, void *arg)
546
21.3M
{
547
21.3M
    struct do_one_data_st *data = arg;
548
549
21.3M
    data->user_fn(method, data->user_arg);
550
21.3M
}
551
552
void OSSL_DECODER_do_all_provided(OSSL_LIB_CTX *libctx,
553
    void (*user_fn)(OSSL_DECODER *decoder,
554
        void *arg),
555
    void *user_arg)
556
523k
{
557
523k
    struct decoder_data_st methdata;
558
523k
    struct do_one_data_st data;
559
560
523k
    methdata.libctx = libctx;
561
523k
    methdata.tmp_store = NULL;
562
523k
    (void)inner_ossl_decoder_fetch(&methdata, NULL, NULL /* properties */);
563
564
523k
    data.user_fn = user_fn;
565
523k
    data.user_arg = user_arg;
566
523k
    if (methdata.tmp_store != NULL)
567
0
        ossl_method_store_do_all(methdata.tmp_store, &do_one, &data);
568
523k
    ossl_method_store_do_all(get_decoder_store(libctx), &do_one, &data);
569
523k
    dealloc_tmp_decoder_store(methdata.tmp_store);
570
523k
}
571
572
int OSSL_DECODER_names_do_all(const OSSL_DECODER *decoder,
573
    void (*fn)(const char *name, void *data),
574
    void *data)
575
0
{
576
0
    if (decoder == NULL)
577
0
        return 0;
578
579
0
    if (decoder->base.prov != NULL) {
580
0
        OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
581
0
        OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
582
583
0
        return ossl_namemap_doall_names(namemap, decoder->base.id, fn, data);
584
0
    }
585
586
0
    return 1;
587
0
}
588
589
const OSSL_PARAM *
590
OSSL_DECODER_gettable_params(OSSL_DECODER *decoder)
591
0
{
592
0
    if (decoder != NULL && decoder->gettable_params != NULL) {
593
0
        void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
594
595
0
        return decoder->gettable_params(provctx);
596
0
    }
597
0
    return NULL;
598
0
}
599
600
int OSSL_DECODER_get_params(OSSL_DECODER *decoder, OSSL_PARAM params[])
601
0
{
602
0
    if (decoder != NULL && decoder->get_params != NULL)
603
0
        return decoder->get_params(params);
604
0
    return 0;
605
0
}
606
607
const OSSL_PARAM *
608
OSSL_DECODER_settable_ctx_params(OSSL_DECODER *decoder)
609
0
{
610
0
    if (decoder != NULL && decoder->settable_ctx_params != NULL) {
611
0
        void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
612
613
0
        return decoder->settable_ctx_params(provctx);
614
0
    }
615
0
    return NULL;
616
0
}
617
618
/*
619
 * Decoder context support
620
 */
621
622
/*
623
 * |encoder| value NULL is valid, and signifies that there is no decoder.
624
 * This is useful to provide fallback mechanisms.
625
 *  Functions that want to verify if there is a decoder can do so with
626
 * OSSL_DECODER_CTX_get_decoder()
627
 */
628
OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void)
629
1.09M
{
630
1.09M
    OSSL_DECODER_CTX *ctx;
631
632
1.09M
    ctx = OPENSSL_zalloc(sizeof(*ctx));
633
1.09M
    return ctx;
634
1.09M
}
635
636
int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx,
637
    const OSSL_PARAM params[])
638
0
{
639
0
    int ok = 1;
640
0
    int i;
641
0
    int l;
642
643
0
    if (!ossl_assert(ctx != NULL)) {
644
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
645
0
        return 0;
646
0
    }
647
648
0
    if (ctx->decoder_insts == NULL)
649
0
        return 1;
650
651
0
    l = OSSL_DECODER_CTX_get_num_decoders(ctx);
652
0
    for (i = 0; i < l; i++) {
653
0
        OSSL_DECODER_INSTANCE *decoder_inst = sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
654
0
        OSSL_DECODER *decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
655
0
        OSSL_DECODER *decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
656
657
0
        if (decoderctx == NULL || decoder->set_ctx_params == NULL)
658
0
            continue;
659
0
        if (!decoder->set_ctx_params(decoderctx, params))
660
0
            ok = 0;
661
0
    }
662
0
    return ok;
663
0
}
664
665
void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx)
666
2.31M
{
667
2.31M
    if (ctx != NULL) {
668
1.35M
        if (ctx->cleanup != NULL)
669
1.24M
            ctx->cleanup(ctx->construct_data);
670
1.35M
        sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts,
671
1.35M
            ossl_decoder_instance_free);
672
1.35M
        ossl_pw_clear_passphrase_data(&ctx->pwdata);
673
1.35M
        OPENSSL_free(ctx);
674
1.35M
    }
675
2.31M
}