Coverage Report

Created: 2023-06-08 06:40

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