Coverage Report

Created: 2025-06-13 06:58

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