Coverage Report

Created: 2026-09-17 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/encode_decode/decoder_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2026 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_names.h>
11
#include <openssl/bio.h>
12
#include <openssl/params.h>
13
#include <openssl/provider.h>
14
#include <openssl/evperr.h>
15
#include <openssl/ecerr.h>
16
#include <openssl/pkcs12err.h>
17
#include <openssl/x509err.h>
18
#include <openssl/trace.h>
19
#include "internal/bio.h"
20
#include "internal/provider.h"
21
#include "internal/namemap.h"
22
#include "crypto/decoder.h"
23
#include "encoder_local.h"
24
#include "internal/e_os.h"
25
26
struct decoder_process_data_st {
27
    OSSL_DECODER_CTX *ctx;
28
29
    /* Current BIO */
30
    BIO *bio;
31
32
    /* Index of the current decoder instance to be processed */
33
    int current_decoder_inst_index;
34
    /* For tracing, count recursion level */
35
    int recursion;
36
37
    /*-
38
     * Flags
39
     */
40
    unsigned int flag_next_level_called : 1;
41
    unsigned int flag_construct_called : 1;
42
    unsigned int flag_input_structure_checked : 1;
43
};
44
45
static int decoder_process(const OSSL_PARAM params[], void *arg);
46
47
int OSSL_DECODER_from_bio(OSSL_DECODER_CTX *ctx, BIO *in)
48
0
{
49
0
    struct decoder_process_data_st data;
50
0
    int ok = 0;
51
0
    BIO *new_bio = NULL;
52
0
    unsigned long lasterr;
53
54
0
    if (in == NULL) {
55
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
56
0
        return 0;
57
0
    }
58
59
0
    if (OSSL_DECODER_CTX_get_num_decoders(ctx) == 0) {
60
0
        ERR_raise_data(ERR_LIB_OSSL_DECODER, OSSL_DECODER_R_DECODER_NOT_FOUND,
61
0
            "No decoders were found. For standard decoders you need "
62
0
            "at least one of the default or base providers "
63
0
            "available. Did you forget to load them?");
64
0
        return 0;
65
0
    }
66
67
0
    lasterr = ERR_peek_last_error();
68
69
0
    if (BIO_tell(in) < 0) {
70
0
        new_bio = BIO_new(BIO_f_readbuffer());
71
0
        if (new_bio == NULL)
72
0
            return 0;
73
0
        in = BIO_push(new_bio, in);
74
0
    }
75
0
    memset(&data, 0, sizeof(data));
76
0
    data.ctx = ctx;
77
0
    data.bio = in;
78
79
    /* Enable passphrase caching */
80
0
    (void)ossl_pw_enable_passphrase_caching(&ctx->pwdata);
81
82
0
    ok = decoder_process(NULL, &data);
83
84
0
    if (!data.flag_construct_called) {
85
0
        const char *spaces
86
0
            = ctx->start_input_type != NULL && ctx->input_structure != NULL
87
0
            ? " "
88
0
            : "";
89
0
        const char *input_type_label
90
0
            = ctx->start_input_type != NULL ? "Input type: " : "";
91
0
        const char *input_structure_label
92
0
            = ctx->input_structure != NULL ? "Input structure: " : "";
93
0
        const char *comma
94
0
            = ctx->start_input_type != NULL && ctx->input_structure != NULL
95
0
            ? ", "
96
0
            : "";
97
0
        const char *input_type
98
0
            = ctx->start_input_type != NULL ? ctx->start_input_type : "";
99
0
        const char *input_structure
100
0
            = ctx->input_structure != NULL ? ctx->input_structure : "";
101
102
0
        if (ERR_peek_last_error() == lasterr || ERR_peek_error() == 0)
103
            /* Prevent spurious decoding error but add at least something */
104
0
            ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_UNSUPPORTED,
105
0
                "No supported data to decode. %s%s%s%s%s%s",
106
0
                spaces, input_type_label, input_type, comma,
107
0
                input_structure_label, input_structure);
108
0
        ok = 0;
109
0
    }
110
111
    /* Clear any internally cached passphrase */
112
0
    (void)ossl_pw_clear_passphrase_cache(&ctx->pwdata);
113
114
0
    if (new_bio != NULL) {
115
0
        BIO_pop(new_bio);
116
0
        BIO_free(new_bio);
117
0
    }
118
0
    return ok;
119
0
}
120
121
#ifndef OPENSSL_NO_STDIO
122
static BIO *bio_from_file(FILE *fp)
123
0
{
124
0
    BIO *b;
125
126
0
    if ((b = BIO_new(BIO_s_file())) == NULL) {
127
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB);
128
0
        return NULL;
129
0
    }
130
0
    BIO_set_fp(b, fp, BIO_NOCLOSE);
131
0
    return b;
132
0
}
133
134
int OSSL_DECODER_from_fp(OSSL_DECODER_CTX *ctx, FILE *fp)
135
0
{
136
0
    BIO *b = bio_from_file(fp);
137
0
    int ret = 0;
138
139
0
    if (b != NULL)
140
0
        ret = OSSL_DECODER_from_bio(ctx, b);
141
142
0
    BIO_free(b);
143
0
    return ret;
144
0
}
145
#endif
146
147
int OSSL_DECODER_from_data(OSSL_DECODER_CTX *ctx, const unsigned char **pdata,
148
    size_t *pdata_len)
149
0
{
150
0
    BIO *membio;
151
0
    int ret = 0;
152
153
0
    if (pdata == NULL || *pdata == NULL || pdata_len == NULL) {
154
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
155
0
        return 0;
156
0
    }
157
158
0
    membio = BIO_new_mem_buf(*pdata, (int)*pdata_len);
159
0
    if (OSSL_DECODER_from_bio(ctx, membio)) {
160
0
        *pdata_len = (size_t)BIO_get_mem_data(membio, pdata);
161
0
        ret = 1;
162
0
    }
163
0
    BIO_free(membio);
164
165
0
    return ret;
166
0
}
167
168
int OSSL_DECODER_CTX_set_selection(OSSL_DECODER_CTX *ctx, int selection)
169
0
{
170
0
    if (ctx == NULL) {
171
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
172
0
        return 0;
173
0
    }
174
175
0
    if (ctx->frozen != 0) {
176
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
177
0
        return 0;
178
0
    }
179
180
    /*
181
     * 0 is a valid selection, and means that the caller leaves
182
     * it to code to discover what the selection is.
183
     */
184
0
    ctx->selection = selection;
185
0
    return 1;
186
0
}
187
188
int OSSL_DECODER_CTX_set_input_type(OSSL_DECODER_CTX *ctx,
189
    const char *input_type)
190
0
{
191
0
    if (ctx == NULL) {
192
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
193
0
        return 0;
194
0
    }
195
196
0
    if (ctx->frozen != 0) {
197
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
198
0
        return 0;
199
0
    }
200
201
    /*
202
     * NULL is a valid starting input type, and means that the caller leaves
203
     * it to code to discover what the starting input type is.
204
     */
205
0
    ctx->start_input_type = input_type;
206
0
    return 1;
207
0
}
208
209
int OSSL_DECODER_CTX_set_input_structure(OSSL_DECODER_CTX *ctx,
210
    const char *input_structure)
211
0
{
212
0
    if (ctx == NULL) {
213
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
214
0
        return 0;
215
0
    }
216
217
0
    if (ctx->frozen != 0) {
218
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
219
0
        return 0;
220
0
    }
221
222
    /*
223
     * NULL is a valid starting input structure, and means that the caller
224
     * leaves it to code to discover what the starting input structure is.
225
     */
226
0
    ctx->input_structure = input_structure;
227
0
    return 1;
228
0
}
229
230
OSSL_DECODER_INSTANCE *
231
ossl_decoder_instance_new_forprov(OSSL_DECODER *decoder, void *provctx,
232
    const char *input_structure)
233
0
{
234
0
    OSSL_DECODER_INSTANCE *decoder_inst = NULL;
235
0
    void *decoderctx;
236
237
0
    if (!ossl_assert(decoder != NULL)) {
238
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
239
0
        return 0;
240
0
    }
241
242
0
    decoderctx = decoder->newctx(provctx);
243
0
    if (decoderctx == NULL)
244
0
        return 0;
245
0
    if (input_structure != NULL && decoder->set_ctx_params != NULL) {
246
0
        OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
247
248
0
        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
249
0
            (char *)input_structure, 0);
250
0
        if (!decoder->set_ctx_params(decoderctx, params)) {
251
0
            decoder->freectx(decoderctx);
252
0
            return 0;
253
0
        }
254
0
    }
255
0
    decoder_inst = ossl_decoder_instance_new(decoder, decoderctx);
256
0
    if (decoder_inst == NULL)
257
0
        decoder->freectx(decoderctx);
258
0
    return decoder_inst;
259
0
}
260
261
OSSL_DECODER_INSTANCE *ossl_decoder_instance_new(OSSL_DECODER *decoder,
262
    void *decoderctx)
263
0
{
264
0
    OSSL_DECODER_INSTANCE *decoder_inst = NULL;
265
0
    const OSSL_PROVIDER *prov;
266
0
    OSSL_LIB_CTX *libctx;
267
0
    const OSSL_PROPERTY_LIST *props;
268
0
    const OSSL_PROPERTY_DEFINITION *prop;
269
270
0
    if (!ossl_assert(decoder != NULL)) {
271
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
272
0
        return 0;
273
0
    }
274
275
0
    if ((decoder_inst = OPENSSL_zalloc(sizeof(*decoder_inst))) == NULL)
276
0
        return 0;
277
278
0
    prov = OSSL_DECODER_get0_provider(decoder);
279
0
    libctx = ossl_provider_libctx(prov);
280
0
    props = ossl_decoder_parsed_properties(decoder);
281
0
    if (props == NULL) {
282
0
        ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
283
0
            "there are no property definitions with decoder %s",
284
0
            OSSL_DECODER_get0_name(decoder));
285
0
        goto err;
286
0
    }
287
288
    /* The "input" property is mandatory */
289
0
    prop = ossl_property_find_property(props, libctx, "input");
290
0
    decoder_inst->input_type = ossl_property_get_string_value(libctx, prop);
291
0
    decoder_inst->input_type_id = 0;
292
0
    if (decoder_inst->input_type == NULL) {
293
0
        ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
294
0
            "the mandatory 'input' property is missing "
295
0
            "for decoder %s (properties: %s)",
296
0
            OSSL_DECODER_get0_name(decoder),
297
0
            OSSL_DECODER_get0_properties(decoder));
298
0
        goto err;
299
0
    }
300
301
    /* The "structure" property is optional */
302
0
    prop = ossl_property_find_property(props, libctx, "structure");
303
0
    if (prop != NULL) {
304
0
        decoder_inst->input_structure
305
0
            = ossl_property_get_string_value(libctx, prop);
306
0
    }
307
308
0
    if (!OSSL_DECODER_up_ref(decoder)) {
309
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
310
0
        goto err;
311
0
    }
312
0
    decoder_inst->decoder = decoder;
313
0
    decoder_inst->decoderctx = decoderctx;
314
0
    return decoder_inst;
315
0
err:
316
0
    ossl_decoder_instance_free(decoder_inst);
317
0
    return NULL;
318
0
}
319
320
void ossl_decoder_instance_free(OSSL_DECODER_INSTANCE *decoder_inst)
321
0
{
322
0
    if (decoder_inst != NULL) {
323
0
        if (decoder_inst->decoder != NULL)
324
0
            decoder_inst->decoder->freectx(decoder_inst->decoderctx);
325
0
        decoder_inst->decoderctx = NULL;
326
0
        OSSL_DECODER_free(decoder_inst->decoder);
327
0
        decoder_inst->decoder = NULL;
328
0
        OPENSSL_free(decoder_inst);
329
0
    }
330
0
}
331
332
OSSL_DECODER_INSTANCE *ossl_decoder_instance_dup(const OSSL_DECODER_INSTANCE *src)
333
0
{
334
0
    OSSL_DECODER_INSTANCE *dest;
335
0
    const OSSL_PROVIDER *prov;
336
0
    void *provctx;
337
338
0
    if ((dest = OPENSSL_zalloc(sizeof(*dest))) == NULL)
339
0
        return NULL;
340
341
0
    *dest = *src;
342
0
    if (!OSSL_DECODER_up_ref(dest->decoder)) {
343
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
344
0
        goto err;
345
0
    }
346
0
    prov = OSSL_DECODER_get0_provider(dest->decoder);
347
0
    provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
348
349
0
    dest->decoderctx = dest->decoder->newctx(provctx);
350
0
    if (dest->decoderctx == NULL) {
351
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
352
0
        OSSL_DECODER_free(dest->decoder);
353
0
        goto err;
354
0
    }
355
356
0
    return dest;
357
358
0
err:
359
0
    OPENSSL_free(dest);
360
0
    return NULL;
361
0
}
362
363
void ossl_decoder_ctx_set_harderr(OSSL_DECODER_CTX *ctx)
364
0
{
365
0
    ctx->harderr = 1;
366
0
}
367
368
int ossl_decoder_ctx_get_harderr(const OSSL_DECODER_CTX *ctx)
369
0
{
370
0
    return ctx->harderr;
371
0
}
372
373
int ossl_decoder_ctx_add_decoder_inst(OSSL_DECODER_CTX *ctx,
374
    OSSL_DECODER_INSTANCE *di)
375
0
{
376
0
    int ok;
377
378
0
    if (ctx->decoder_insts == NULL
379
0
        && (ctx->decoder_insts = sk_OSSL_DECODER_INSTANCE_new_null()) == NULL) {
380
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
381
0
        return 0;
382
0
    }
383
384
0
    ok = (sk_OSSL_DECODER_INSTANCE_push(ctx->decoder_insts, di) > 0);
385
0
    if (ok) {
386
0
        OSSL_TRACE_BEGIN(DECODER)
387
0
        {
388
0
            BIO_printf(trc_out,
389
0
                "(ctx %p) Added decoder instance %p for decoder %p\n"
390
0
                "    %s with %s (%s)\n",
391
0
                (void *)ctx, (void *)di, (void *)di->decoder,
392
0
                OSSL_DECODER_get0_name(di->decoder),
393
0
                OSSL_DECODER_get0_properties(di->decoder),
394
0
                OSSL_DECODER_get0_description(di->decoder));
395
0
        }
396
0
        OSSL_TRACE_END(DECODER);
397
0
    }
398
0
    return ok;
399
0
}
400
401
int OSSL_DECODER_CTX_add_decoder(OSSL_DECODER_CTX *ctx, OSSL_DECODER *decoder)
402
0
{
403
0
    OSSL_DECODER_INSTANCE *decoder_inst = NULL;
404
0
    const OSSL_PROVIDER *prov = NULL;
405
0
    void *decoderctx = NULL;
406
0
    void *provctx = NULL;
407
408
0
    if (ctx == NULL || decoder == NULL) {
409
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
410
0
        return 0;
411
0
    }
412
413
0
    if (ctx->frozen != 0) {
414
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
415
0
        return 0;
416
0
    }
417
418
0
    prov = OSSL_DECODER_get0_provider(decoder);
419
0
    provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
420
421
0
    if ((decoderctx = decoder->newctx(provctx)) == NULL
422
0
        || (decoder_inst = ossl_decoder_instance_new(decoder, decoderctx)) == NULL)
423
0
        goto err;
424
    /* Avoid double free of decoderctx on further errors */
425
0
    decoderctx = NULL;
426
427
0
    if (!ossl_decoder_ctx_add_decoder_inst(ctx, decoder_inst))
428
0
        goto err;
429
430
0
    return 1;
431
0
err:
432
0
    ossl_decoder_instance_free(decoder_inst);
433
0
    if (decoderctx != NULL)
434
0
        decoder->freectx(decoderctx);
435
0
    return 0;
436
0
}
437
438
struct collect_extra_decoder_data_st {
439
    OSSL_DECODER_CTX *ctx;
440
    const char *output_type;
441
    int output_type_id;
442
443
    /*
444
     * 0 to check that the decoder's input type is the same as the decoder name
445
     * 1 to check that the decoder's input type differs from the decoder name
446
     */
447
    enum { IS_SAME = 0,
448
        IS_DIFFERENT = 1 } type_check;
449
    int w_prev_start, w_prev_end; /* "previous" decoders */
450
    int w_new_start, w_new_end; /* "new" decoders */
451
};
452
453
DEFINE_STACK_OF(OSSL_DECODER)
454
455
static void collect_all_decoders(OSSL_DECODER *decoder, void *arg)
456
0
{
457
0
    STACK_OF(OSSL_DECODER) *skdecoders = arg;
458
459
0
    if (OSSL_DECODER_up_ref(decoder)
460
0
        && !sk_OSSL_DECODER_push(skdecoders, decoder))
461
0
        OSSL_DECODER_free(decoder);
462
0
}
463
464
static void collect_extra_decoder(OSSL_DECODER *decoder, void *arg)
465
0
{
466
0
    struct collect_extra_decoder_data_st *data = arg;
467
0
    int j;
468
0
    const OSSL_PROVIDER *prov = OSSL_DECODER_get0_provider(decoder);
469
0
    void *provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
470
471
0
    if (ossl_decoder_fast_is_a(decoder, data->output_type, &data->output_type_id)) {
472
0
        void *decoderctx = NULL;
473
0
        OSSL_DECODER_INSTANCE *di = NULL;
474
475
0
        OSSL_TRACE_BEGIN(DECODER)
476
0
        {
477
0
            BIO_printf(trc_out,
478
0
                "(ctx %p) [%d] Checking out decoder %p:\n"
479
0
                "    %s with %s (%s)\n",
480
0
                (void *)data->ctx, data->type_check, (void *)decoder,
481
0
                OSSL_DECODER_get0_name(decoder),
482
0
                OSSL_DECODER_get0_properties(decoder),
483
0
                OSSL_DECODER_get0_description(decoder));
484
0
        }
485
0
        OSSL_TRACE_END(DECODER);
486
487
        /*
488
         * Check that we don't already have this decoder in our stack,
489
         * starting with the previous windows but also looking at what
490
         * we have added in the current window.
491
         */
492
0
        for (j = data->w_prev_start; j < data->w_new_end; j++) {
493
0
            OSSL_DECODER_INSTANCE *check_inst = sk_OSSL_DECODER_INSTANCE_value(data->ctx->decoder_insts, j);
494
495
0
            if (decoder->base.algodef == check_inst->decoder->base.algodef) {
496
                /* We found it, so don't do anything more */
497
0
                OSSL_TRACE_BEGIN(DECODER)
498
0
                {
499
0
                    BIO_printf(trc_out,
500
0
                        "    REJECTED: already exists in the chain\n");
501
0
                }
502
0
                OSSL_TRACE_END(DECODER);
503
0
                return;
504
0
            }
505
0
        }
506
507
0
        if ((decoderctx = decoder->newctx(provctx)) == NULL)
508
0
            return;
509
510
0
        if (decoder->set_ctx_params != NULL
511
0
            && data->ctx->input_structure != NULL) {
512
0
            OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
513
0
            const char *str = data->ctx->input_structure;
514
515
0
            params[0] = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
516
0
                (char *)str, 0);
517
0
            if (!decoder->set_ctx_params(decoderctx, params)) {
518
0
                decoder->freectx(decoderctx);
519
0
                return;
520
0
            }
521
0
        }
522
523
0
        if ((di = ossl_decoder_instance_new(decoder, decoderctx)) == NULL) {
524
0
            decoder->freectx(decoderctx);
525
0
            return;
526
0
        }
527
528
0
        switch (data->type_check) {
529
0
        case IS_SAME:
530
            /* If it differs, this is not a decoder to add for now. */
531
0
            if (!ossl_decoder_fast_is_a(decoder,
532
0
                    OSSL_DECODER_INSTANCE_get_input_type(di),
533
0
                    &di->input_type_id)) {
534
0
                ossl_decoder_instance_free(di);
535
0
                OSSL_TRACE_BEGIN(DECODER)
536
0
                {
537
0
                    BIO_printf(trc_out,
538
0
                        "    REJECTED: input type doesn't match output type\n");
539
0
                }
540
0
                OSSL_TRACE_END(DECODER);
541
0
                return;
542
0
            }
543
0
            break;
544
0
        case IS_DIFFERENT:
545
            /* If it's the same, this is not a decoder to add for now. */
546
0
            if (ossl_decoder_fast_is_a(decoder,
547
0
                    OSSL_DECODER_INSTANCE_get_input_type(di),
548
0
                    &di->input_type_id)) {
549
0
                ossl_decoder_instance_free(di);
550
0
                OSSL_TRACE_BEGIN(DECODER)
551
0
                {
552
0
                    BIO_printf(trc_out,
553
0
                        "    REJECTED: input type matches output type\n");
554
0
                }
555
0
                OSSL_TRACE_END(DECODER);
556
0
                return;
557
0
            }
558
0
            break;
559
0
        }
560
561
        /*
562
         * Apart from keeping w_new_end up to date, We don't care about
563
         * errors here.  If it doesn't collect, then it doesn't...
564
         */
565
0
        if (!ossl_decoder_ctx_add_decoder_inst(data->ctx, di)) {
566
0
            ossl_decoder_instance_free(di);
567
0
            return;
568
0
        }
569
570
0
        data->w_new_end++;
571
0
    }
572
0
}
573
574
static int decoder_sk_cmp(const OSSL_DECODER_INSTANCE *const *a,
575
    const OSSL_DECODER_INSTANCE *const *b)
576
0
{
577
0
    if ((*a)->score == (*b)->score)
578
0
        return (*a)->order - (*b)->order;
579
0
    return (*a)->score - (*b)->score;
580
0
}
581
582
int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx,
583
    OSSL_LIB_CTX *libctx, const char *propq)
584
0
{
585
    /*
586
     * This function goes through existing decoder methods in
587
     * |ctx->decoder_insts|, and tries to fetch new decoders that produce
588
     * what the existing ones want as input, and push those newly fetched
589
     * decoders on top of the same stack.
590
     * Then it does the same again, but looping over the newly fetched
591
     * decoders, until there are no more decoders to be fetched, or
592
     * when we have done this 10 times.
593
     *
594
     * we do this with sliding windows on the stack by keeping track of indexes
595
     * and of the end.
596
     *
597
     * +----------------+
598
     * |   DER to RSA   | <--- w_prev_start
599
     * +----------------+
600
     * |   DER to DSA   |
601
     * +----------------+
602
     * |   DER to DH    |
603
     * +----------------+
604
     * |   PEM to DER   | <--- w_prev_end, w_new_start
605
     * +----------------+
606
     *                    <--- w_new_end
607
     */
608
0
    struct collect_extra_decoder_data_st data;
609
0
    size_t depth = 0; /* Counts the number of iterations */
610
0
    size_t count; /* Calculates how many were added in each iteration */
611
0
    int numdecoders;
612
0
    STACK_OF(OSSL_DECODER) *skdecoders;
613
614
0
    if (ctx == NULL) {
615
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
616
0
        return 0;
617
0
    }
618
619
0
    if (ctx->frozen != 0) {
620
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
621
0
        return 0;
622
0
    }
623
624
    /*
625
     * If there is no stack of OSSL_DECODER_INSTANCE, we have nothing
626
     * more to add.  That's fine.
627
     */
628
0
    if (ctx->decoder_insts == NULL)
629
0
        return 1;
630
631
0
    OSSL_TRACE_BEGIN(DECODER)
632
0
    {
633
0
        BIO_printf(trc_out, "(ctx %p) Looking for extra decoders\n",
634
0
            (void *)ctx);
635
0
    }
636
0
    OSSL_TRACE_END(DECODER);
637
638
0
    skdecoders = sk_OSSL_DECODER_new_null();
639
0
    if (skdecoders == NULL) {
640
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
641
0
        return 0;
642
0
    }
643
0
    OSSL_DECODER_do_all_provided(libctx, collect_all_decoders, skdecoders);
644
0
    numdecoders = sk_OSSL_DECODER_num(skdecoders);
645
646
    /*
647
     * If there are provided or default properties, sort the initial decoder list
648
     * by property matching score so that the highest scored provider is selected
649
     * first.
650
     */
651
0
    if (propq != NULL || ossl_ctx_global_properties(libctx, 0) != NULL) {
652
0
        int num_decoder_insts = sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
653
0
        int i;
654
0
        OSSL_DECODER_INSTANCE *di;
655
0
        sk_OSSL_DECODER_INSTANCE_compfunc old_cmp = sk_OSSL_DECODER_INSTANCE_set_cmp_func(ctx->decoder_insts, decoder_sk_cmp);
656
657
0
        for (i = 0; i < num_decoder_insts; i++) {
658
0
            di = sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
659
0
            di->order = i;
660
0
        }
661
0
        sk_OSSL_DECODER_INSTANCE_sort(ctx->decoder_insts);
662
0
        sk_OSSL_DECODER_INSTANCE_set_cmp_func(ctx->decoder_insts, old_cmp);
663
0
    }
664
665
0
    memset(&data, 0, sizeof(data));
666
0
    data.ctx = ctx;
667
0
    data.w_prev_start = 0;
668
0
    data.w_prev_end = sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
669
0
    do {
670
0
        int i, j;
671
672
0
        data.w_new_start = data.w_new_end = data.w_prev_end;
673
674
        /*
675
         * Two iterations:
676
         * 0.  All decoders that have the same name as their input type.
677
         *     This allows for decoders that unwrap some data in a specific
678
         *     encoding, and pass the result on with the same encoding.
679
         * 1.  All decoders that a different name than their input type.
680
         */
681
0
        for (data.type_check = IS_SAME;
682
0
            data.type_check <= IS_DIFFERENT;
683
0
            data.type_check++) {
684
0
            for (i = data.w_prev_start; i < data.w_prev_end; i++) {
685
0
                OSSL_DECODER_INSTANCE *decoder_inst = sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
686
687
0
                data.output_type
688
0
                    = OSSL_DECODER_INSTANCE_get_input_type(decoder_inst);
689
690
0
                data.output_type_id = 0;
691
692
0
                for (j = 0; j < numdecoders; j++)
693
0
                    collect_extra_decoder(sk_OSSL_DECODER_value(skdecoders, j),
694
0
                        &data);
695
0
            }
696
0
        }
697
        /* How many were added in this iteration */
698
0
        count = data.w_new_end - data.w_new_start;
699
700
        /* Slide the "previous decoder" windows */
701
0
        data.w_prev_start = data.w_new_start;
702
0
        data.w_prev_end = data.w_new_end;
703
704
0
        depth++;
705
0
    } while (count != 0 && depth <= 10);
706
707
0
    sk_OSSL_DECODER_pop_free(skdecoders, OSSL_DECODER_free);
708
0
    return 1;
709
0
}
710
711
int OSSL_DECODER_CTX_get_num_decoders(OSSL_DECODER_CTX *ctx)
712
0
{
713
0
    if (ctx == NULL || ctx->decoder_insts == NULL)
714
0
        return 0;
715
0
    return sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
716
0
}
717
718
int OSSL_DECODER_CTX_set_construct(OSSL_DECODER_CTX *ctx,
719
    OSSL_DECODER_CONSTRUCT *construct)
720
0
{
721
0
    if (ctx == NULL) {
722
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
723
0
        return 0;
724
0
    }
725
726
0
    if (ctx->frozen != 0) {
727
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
728
0
        return 0;
729
0
    }
730
731
0
    ctx->construct = construct;
732
0
    return 1;
733
0
}
734
735
int OSSL_DECODER_CTX_set_construct_data(OSSL_DECODER_CTX *ctx,
736
    void *construct_data)
737
0
{
738
0
    if (ctx == NULL) {
739
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
740
0
        return 0;
741
0
    }
742
743
0
    if (ctx->frozen != 0) {
744
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
745
0
        return 0;
746
0
    }
747
748
0
    ctx->construct_data = construct_data;
749
0
    return 1;
750
0
}
751
752
int OSSL_DECODER_CTX_set_cleanup(OSSL_DECODER_CTX *ctx,
753
    OSSL_DECODER_CLEANUP *cleanup)
754
0
{
755
0
    if (ctx == NULL) {
756
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
757
0
        return 0;
758
0
    }
759
760
0
    if (ctx->frozen != 0) {
761
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
762
0
        return 0;
763
0
    }
764
765
0
    ctx->cleanup = cleanup;
766
0
    return 1;
767
0
}
768
769
OSSL_DECODER_CONSTRUCT *
770
OSSL_DECODER_CTX_get_construct(OSSL_DECODER_CTX *ctx)
771
0
{
772
0
    if (ctx == NULL)
773
0
        return NULL;
774
0
    return ctx->construct;
775
0
}
776
777
void *OSSL_DECODER_CTX_get_construct_data(OSSL_DECODER_CTX *ctx)
778
0
{
779
0
    if (ctx == NULL)
780
0
        return NULL;
781
0
    return ctx->construct_data;
782
0
}
783
784
OSSL_DECODER_CLEANUP *
785
OSSL_DECODER_CTX_get_cleanup(OSSL_DECODER_CTX *ctx)
786
0
{
787
0
    if (ctx == NULL)
788
0
        return NULL;
789
0
    return ctx->cleanup;
790
0
}
791
792
int OSSL_DECODER_export(OSSL_DECODER_INSTANCE *decoder_inst,
793
    void *reference, size_t reference_sz,
794
    OSSL_CALLBACK *export_cb, void *export_cbarg)
795
0
{
796
0
    OSSL_DECODER *decoder = NULL;
797
0
    void *decoderctx = NULL;
798
799
0
    if (decoder_inst == NULL || reference == NULL
800
0
        || export_cb == NULL || export_cbarg == NULL) {
801
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
802
0
        return 0;
803
0
    }
804
805
0
    decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
806
0
    decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
807
0
    return decoder->export_object(decoderctx, reference, reference_sz,
808
0
        export_cb, export_cbarg);
809
0
}
810
811
OSSL_DECODER *
812
OSSL_DECODER_INSTANCE_get_decoder(OSSL_DECODER_INSTANCE *decoder_inst)
813
0
{
814
0
    if (decoder_inst == NULL)
815
0
        return NULL;
816
0
    return decoder_inst->decoder;
817
0
}
818
819
void *
820
OSSL_DECODER_INSTANCE_get_decoder_ctx(OSSL_DECODER_INSTANCE *decoder_inst)
821
0
{
822
0
    if (decoder_inst == NULL)
823
0
        return NULL;
824
0
    return decoder_inst->decoderctx;
825
0
}
826
827
const char *
828
OSSL_DECODER_INSTANCE_get_input_type(OSSL_DECODER_INSTANCE *decoder_inst)
829
0
{
830
0
    if (decoder_inst == NULL)
831
0
        return NULL;
832
0
    return decoder_inst->input_type;
833
0
}
834
835
const char *
836
OSSL_DECODER_INSTANCE_get_input_structure(OSSL_DECODER_INSTANCE *decoder_inst,
837
    int *was_set)
838
0
{
839
0
    if (decoder_inst == NULL)
840
0
        return NULL;
841
0
    *was_set = decoder_inst->flag_input_structure_was_set;
842
0
    return decoder_inst->input_structure;
843
0
}
844
845
static int decoder_process(const OSSL_PARAM params[], void *arg)
846
0
{
847
0
    struct decoder_process_data_st *data = arg;
848
0
    OSSL_DECODER_CTX *ctx = data->ctx;
849
0
    OSSL_DECODER_INSTANCE *decoder_inst = NULL;
850
0
    OSSL_DECODER *decoder = NULL;
851
0
    OSSL_CORE_BIO *cbio = NULL;
852
0
    BIO *bio = data->bio;
853
0
    long loc;
854
0
    int i;
855
0
    int ok = 0;
856
    /* For recursions */
857
0
    struct decoder_process_data_st new_data;
858
0
    const char *data_type = NULL;
859
0
    const char *data_structure = NULL;
860
    /* Saved to restore on return, mutated in PEM->DER transition. */
861
0
    const char *start_input_type = ctx->start_input_type;
862
863
    /*
864
     * This is an indicator up the call stack that something was indeed
865
     * decoded, leading to a recursive call of this function.
866
     */
867
0
    data->flag_next_level_called = 1;
868
869
0
    memset(&new_data, 0, sizeof(new_data));
870
0
    new_data.ctx = data->ctx;
871
0
    new_data.recursion = data->recursion + 1;
872
873
0
#define LEVEL_STR ">>>>>>>>>>>>>>>>"
874
0
#define LEVEL ((size_t)new_data.recursion < sizeof(LEVEL_STR)    \
875
0
        ? &LEVEL_STR[sizeof(LEVEL_STR) - new_data.recursion - 1] \
876
0
        : LEVEL_STR "...")
877
878
0
    if (params == NULL) {
879
        /* First iteration, where we prepare for what is to come */
880
881
0
        OSSL_TRACE_BEGIN(DECODER)
882
0
        {
883
0
            BIO_printf(trc_out,
884
0
                "(ctx %p) starting to walk the decoder chain\n",
885
0
                (void *)new_data.ctx);
886
0
        }
887
0
        OSSL_TRACE_END(DECODER);
888
889
0
        data->current_decoder_inst_index = OSSL_DECODER_CTX_get_num_decoders(ctx);
890
891
0
        bio = data->bio;
892
0
    } else {
893
0
        const OSSL_PARAM *p;
894
0
        const char *trace_data_structure;
895
896
0
        decoder_inst = sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts,
897
0
            data->current_decoder_inst_index);
898
0
        decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
899
900
0
        data->flag_construct_called = 0;
901
0
        if (ctx->construct != NULL) {
902
0
            int rv;
903
904
0
            OSSL_TRACE_BEGIN(DECODER)
905
0
            {
906
0
                BIO_printf(trc_out,
907
0
                    "(ctx %p) %s Running constructor\n",
908
0
                    (void *)new_data.ctx, LEVEL);
909
0
            }
910
0
            OSSL_TRACE_END(DECODER);
911
912
0
            rv = ctx->construct(decoder_inst, params, ctx->construct_data);
913
914
0
            OSSL_TRACE_BEGIN(DECODER)
915
0
            {
916
0
                BIO_printf(trc_out,
917
0
                    "(ctx %p) %s Running constructor => %d\n",
918
0
                    (void *)new_data.ctx, LEVEL, rv);
919
0
            }
920
0
            OSSL_TRACE_END(DECODER);
921
922
0
            ok = (rv > 0);
923
0
            if (ok) {
924
0
                data->flag_construct_called = 1;
925
0
                goto end;
926
0
            }
927
0
        }
928
929
        /* The constructor didn't return success */
930
931
        /*
932
         * so we try to use the object we got and feed it to any next
933
         * decoder that will take it.  Object references are not
934
         * allowed for this.
935
         * If this data isn't present, decoding has failed.
936
         */
937
938
0
        p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA);
939
0
        if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
940
0
            goto end;
941
0
        new_data.bio = BIO_new_mem_buf(p->data, (int)p->data_size);
942
0
        if (new_data.bio == NULL)
943
0
            goto end;
944
0
        bio = new_data.bio;
945
946
        /* Get the data type if there is one */
947
0
        p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE);
948
0
        if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &data_type))
949
0
            goto end;
950
951
        /* Get the data structure if there is one */
952
0
        p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_STRUCTURE);
953
0
        if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &data_structure))
954
0
            goto end;
955
956
        /* Get the new input type if there is one */
957
0
        p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_INPUT_TYPE);
958
0
        if (p != NULL) {
959
0
            if (!OSSL_PARAM_get_utf8_string_ptr(p, &ctx->start_input_type))
960
0
                goto end;
961
            /*
962
             * When switching PKCS8 from PEM to DER we decrypt the data if needed
963
             * and then determine the algorithm OID.  Likewise, with SPKI, only
964
             * this time sans decryption.
965
             */
966
0
            if (ctx->input_structure != NULL
967
0
                && (OPENSSL_strcasecmp(ctx->input_structure, "SubjectPublicKeyInfo") == 0
968
0
                    || OPENSSL_strcasecmp(data_structure, "PrivateKeyInfo") == 0
969
0
                    || OPENSSL_strcasecmp(ctx->input_structure, "PrivateKeyInfo") == 0))
970
0
                data->flag_input_structure_checked = 1;
971
0
        }
972
973
        /*
974
         * If the data structure is "type-specific" and the data type is
975
         * given, we drop the data structure.  The reasoning is that the
976
         * data type is already enough to find the applicable next decoder,
977
         * so an additional "type-specific" data structure is extraneous.
978
         *
979
         * Furthermore, if the OSSL_DECODER caller asked for a type specific
980
         * structure under another name, such as "DH", we get a mismatch
981
         * if the data structure we just received is "type-specific".
982
         * There's only so much you can do without infusing this code with
983
         * too special knowledge.
984
         */
985
0
        trace_data_structure = data_structure;
986
0
        if (data_type != NULL && data_structure != NULL
987
0
            && OPENSSL_strcasecmp(data_structure, "type-specific") == 0)
988
0
            data_structure = NULL;
989
990
0
        OSSL_TRACE_BEGIN(DECODER)
991
0
        {
992
0
            BIO_printf(trc_out,
993
0
                "(ctx %p) %s incoming from previous decoder (%p %s):\n"
994
0
                "    data type: %s, data structure: %s%s\n",
995
0
                (void *)new_data.ctx, LEVEL, (void *)decoder,
996
0
                OSSL_DECODER_get0_description(decoder),
997
0
                data_type, trace_data_structure,
998
0
                (trace_data_structure == data_structure
999
0
                        ? ""
1000
0
                        : " (dropped)"));
1001
0
        }
1002
0
        OSSL_TRACE_END(DECODER);
1003
0
    }
1004
1005
    /*
1006
     * If we have no more decoders to look through at this point,
1007
     * we failed
1008
     */
1009
0
    if (data->current_decoder_inst_index == 0)
1010
0
        goto end;
1011
1012
0
    if ((loc = BIO_tell(bio)) < 0) {
1013
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB);
1014
0
        goto end;
1015
0
    }
1016
1017
0
    if ((cbio = ossl_core_bio_new_from_bio(bio)) == NULL) {
1018
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB);
1019
0
        goto end;
1020
0
    }
1021
1022
0
    for (i = data->current_decoder_inst_index; i-- > 0;) {
1023
0
        OSSL_DECODER_INSTANCE *new_decoder_inst = sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
1024
0
        OSSL_DECODER *new_decoder = OSSL_DECODER_INSTANCE_get_decoder(new_decoder_inst);
1025
0
        const char *new_decoder_name = NULL;
1026
0
        void *new_decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(new_decoder_inst);
1027
0
        const char *new_input_type = OSSL_DECODER_INSTANCE_get_input_type(new_decoder_inst);
1028
0
        int n_i_s_was_set = 0; /* We don't care here */
1029
0
        const char *new_input_structure = OSSL_DECODER_INSTANCE_get_input_structure(new_decoder_inst,
1030
0
            &n_i_s_was_set);
1031
1032
0
        OSSL_TRACE_BEGIN(DECODER)
1033
0
        {
1034
0
            new_decoder_name = OSSL_DECODER_get0_name(new_decoder);
1035
0
            BIO_printf(trc_out,
1036
0
                "(ctx %p) %s [%u] Considering decoder instance %p (decoder %p %s):\n"
1037
0
                "    %s with %s\n",
1038
0
                (void *)new_data.ctx, LEVEL, (unsigned int)i,
1039
0
                (void *)new_decoder_inst, (void *)new_decoder,
1040
0
                OSSL_DECODER_get0_description(new_decoder),
1041
0
                new_decoder_name,
1042
0
                OSSL_DECODER_get0_properties(new_decoder));
1043
0
        }
1044
0
        OSSL_TRACE_END(DECODER);
1045
1046
        /*
1047
         * If |decoder| is NULL, it means we've just started, and the caller
1048
         * may have specified what it expects the initial input to be.  If
1049
         * that's the case, we do this extra check.
1050
         */
1051
0
        if (decoder == NULL && ctx->start_input_type != NULL
1052
0
            && OPENSSL_strcasecmp(ctx->start_input_type, new_input_type) != 0) {
1053
0
            OSSL_TRACE_BEGIN(DECODER)
1054
0
            {
1055
0
                BIO_printf(trc_out,
1056
0
                    "(ctx %p) %s [%u] the start input type '%s' doesn't match the input type of the considered decoder, skipping...\n",
1057
0
                    (void *)new_data.ctx, LEVEL, (unsigned int)i,
1058
0
                    ctx->start_input_type);
1059
0
            }
1060
0
            OSSL_TRACE_END(DECODER);
1061
0
            continue;
1062
0
        }
1063
1064
        /*
1065
         * If we have a previous decoder, we check that the input type
1066
         * of the next to be used matches the type of this previous one.
1067
         * |new_input_type| holds the value of the "input-type" parameter
1068
         * for the decoder we're currently considering.
1069
         */
1070
0
        if (decoder != NULL && !ossl_decoder_fast_is_a(decoder, new_input_type, &new_decoder_inst->input_type_id)) {
1071
0
            OSSL_TRACE_BEGIN(DECODER)
1072
0
            {
1073
0
                BIO_printf(trc_out,
1074
0
                    "(ctx %p) %s [%u] the input type doesn't match the name of the previous decoder (%p %s), skipping...\n",
1075
0
                    (void *)new_data.ctx, LEVEL, (unsigned int)i,
1076
0
                    (void *)decoder,
1077
0
                    OSSL_DECODER_get0_description(decoder));
1078
0
            }
1079
0
            OSSL_TRACE_END(DECODER);
1080
0
            continue;
1081
0
        }
1082
1083
        /*
1084
         * If the previous decoder gave us a data type, we check to see
1085
         * if that matches the decoder we're currently considering.
1086
         */
1087
0
        if (data_type != NULL && !OSSL_DECODER_is_a(new_decoder, data_type)) {
1088
0
            OSSL_TRACE_BEGIN(DECODER)
1089
0
            {
1090
0
                BIO_printf(trc_out,
1091
0
                    "(ctx %p) %s [%u] the previous decoder's data type doesn't match the name of the considered decoder, skipping...\n",
1092
0
                    (void *)new_data.ctx, LEVEL, (unsigned int)i);
1093
0
            }
1094
0
            OSSL_TRACE_END(DECODER);
1095
0
            continue;
1096
0
        }
1097
1098
        /*
1099
         * If the previous decoder gave us a data structure name, we check
1100
         * to see that it matches the input data structure of the decoder
1101
         * we're currently considering.
1102
         */
1103
0
        if (data_structure != NULL
1104
0
            && (new_input_structure == NULL
1105
0
                || OPENSSL_strcasecmp(data_structure,
1106
0
                       new_input_structure)
1107
0
                    != 0)) {
1108
0
            OSSL_TRACE_BEGIN(DECODER)
1109
0
            {
1110
0
                BIO_printf(trc_out,
1111
0
                    "(ctx %p) %s [%u] the previous decoder's data structure doesn't match the input structure of the considered decoder, skipping...\n",
1112
0
                    (void *)new_data.ctx, LEVEL, (unsigned int)i);
1113
0
            }
1114
0
            OSSL_TRACE_END(DECODER);
1115
0
            continue;
1116
0
        }
1117
1118
        /*
1119
         * If the decoder we're currently considering specifies a structure,
1120
         * and this check hasn't already been done earlier in this chain of
1121
         * decoder_process() calls, check that it matches the user provided
1122
         * input structure, if one is given.
1123
         */
1124
0
        if (!data->flag_input_structure_checked
1125
0
            && ctx->input_structure != NULL
1126
0
            && new_input_structure != NULL) {
1127
0
            data->flag_input_structure_checked = 1;
1128
0
            if (OPENSSL_strcasecmp(new_input_structure,
1129
0
                    ctx->input_structure)
1130
0
                != 0) {
1131
0
                OSSL_TRACE_BEGIN(DECODER)
1132
0
                {
1133
0
                    BIO_printf(trc_out,
1134
0
                        "(ctx %p) %s [%u] the previous decoder's data structure doesn't match the input structure given by the user, skipping...\n",
1135
0
                        (void *)new_data.ctx, LEVEL, (unsigned int)i);
1136
0
                }
1137
0
                OSSL_TRACE_END(DECODER);
1138
0
                continue;
1139
0
            }
1140
0
        }
1141
1142
        /*
1143
         * Checking the return value of BIO_reset() or BIO_seek() is unsafe.
1144
         * Furthermore, BIO_reset() is unsafe to use if the source BIO happens
1145
         * to be a BIO_s_mem(), because the earlier BIO_tell() gives us zero
1146
         * no matter where we are in the underlying buffer we're reading from.
1147
         *
1148
         * So, we simply do a BIO_seek(), and use BIO_tell() that we're back
1149
         * at the same position.  This is a best effort attempt, but BIO_seek()
1150
         * and BIO_tell() should come as a pair...
1151
         */
1152
0
        (void)BIO_seek(bio, loc);
1153
0
        if (BIO_tell(bio) != loc)
1154
0
            goto end;
1155
1156
        /* Recurse */
1157
0
        OSSL_TRACE_BEGIN(DECODER)
1158
0
        {
1159
0
            BIO_printf(trc_out,
1160
0
                "(ctx %p) %s [%u] Running decoder instance %s (%p)\n",
1161
0
                (void *)new_data.ctx, LEVEL, (unsigned int)i,
1162
0
                new_decoder_name, (void *)new_decoder_inst);
1163
0
        }
1164
0
        OSSL_TRACE_END(DECODER);
1165
1166
        /*
1167
         * We only care about errors reported from decoder implementations
1168
         * if it returns false (i.e. there was a fatal error).
1169
         */
1170
0
        ERR_set_mark();
1171
1172
0
        new_data.current_decoder_inst_index = i;
1173
0
        new_data.flag_input_structure_checked
1174
0
            = data->flag_input_structure_checked;
1175
0
        ok = new_decoder->decode(new_decoderctx, cbio,
1176
0
            new_data.ctx->selection,
1177
0
            decoder_process, &new_data,
1178
0
            ossl_pw_passphrase_callback_dec,
1179
0
            &new_data.ctx->pwdata);
1180
1181
0
        OSSL_TRACE_BEGIN(DECODER)
1182
0
        {
1183
0
            BIO_printf(trc_out,
1184
0
                "(ctx %p) %s [%u] Running decoder instance %s (%p) => %d"
1185
0
                " (recursed further: %s, construct called: %s)\n",
1186
0
                (void *)new_data.ctx, LEVEL, (unsigned int)i,
1187
0
                new_decoder_name, (void *)new_decoder_inst, ok,
1188
0
                new_data.flag_next_level_called ? "yes" : "no",
1189
0
                new_data.flag_construct_called ? "yes" : "no");
1190
0
        }
1191
0
        OSSL_TRACE_END(DECODER);
1192
1193
0
        data->flag_construct_called = new_data.flag_construct_called;
1194
1195
        /* Break on error or if we tried to construct an object already */
1196
0
        if (!ok || data->flag_construct_called) {
1197
0
            ERR_clear_last_mark();
1198
0
            break;
1199
0
        }
1200
0
        ERR_pop_to_mark();
1201
1202
        /*
1203
         * Break if the decoder implementation that we called recursed, since
1204
         * that indicates that it successfully decoded something.
1205
         */
1206
0
        if (new_data.flag_next_level_called)
1207
0
            break;
1208
0
    }
1209
1210
0
end:
1211
0
    ossl_core_bio_free(cbio);
1212
0
    BIO_free(new_data.bio);
1213
0
    ctx->start_input_type = start_input_type;
1214
0
    return ok;
1215
0
}