Coverage Report

Created: 2025-08-28 07:07

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