Coverage Report

Created: 2025-12-10 06:24

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