Coverage Report

Created: 2025-06-13 06:56

/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
    size_t current_decoder_inst_index;
34
    /* For tracing, count recursion level */
35
    size_t 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
52.6k
{
49
52.6k
    struct decoder_process_data_st data;
50
52.6k
    int ok = 0;
51
52.6k
    BIO *new_bio = NULL;
52
52.6k
    unsigned long lasterr;
53
54
52.6k
    if (in == NULL) {
55
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
56
0
        return 0;
57
0
    }
58
59
52.6k
    if (OSSL_DECODER_CTX_get_num_decoders(ctx) == 0) {
60
4.25k
        ERR_raise_data(ERR_LIB_OSSL_DECODER, OSSL_DECODER_R_DECODER_NOT_FOUND,
61
4.25k
                       "No decoders were found. For standard decoders you need "
62
4.25k
                       "at least one of the default or base providers "
63
4.25k
                       "available. Did you forget to load them?");
64
4.25k
        return 0;
65
4.25k
    }
66
67
48.3k
    lasterr = ERR_peek_last_error();
68
69
48.3k
    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
48.3k
    memset(&data, 0, sizeof(data));
76
48.3k
    data.ctx = ctx;
77
48.3k
    data.bio = in;
78
79
    /* Enable passphrase caching */
80
48.3k
    (void)ossl_pw_enable_passphrase_caching(&ctx->pwdata);
81
82
48.3k
    ok = decoder_process(NULL, &data);
83
84
48.3k
    if (!data.flag_construct_called) {
85
23.8k
        const char *spaces
86
23.8k
            = ctx->start_input_type != NULL && ctx->input_structure != NULL
87
23.8k
            ? " " : "";
88
23.8k
        const char *input_type_label
89
23.8k
            = ctx->start_input_type != NULL ? "Input type: " : "";
90
23.8k
        const char *input_structure_label
91
23.8k
            = ctx->input_structure != NULL ? "Input structure: " : "";
92
23.8k
        const char *comma
93
23.8k
            = ctx->start_input_type != NULL && ctx->input_structure != NULL
94
23.8k
            ? ", " : "";
95
23.8k
        const char *input_type
96
23.8k
            = ctx->start_input_type != NULL ? ctx->start_input_type : "";
97
23.8k
        const char *input_structure
98
23.8k
            = ctx->input_structure != NULL ? ctx->input_structure : "";
99
100
23.8k
        if (ERR_peek_last_error() == lasterr || ERR_peek_error() == 0)
101
            /* Prevent spurious decoding error but add at least something */
102
23.8k
            ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_UNSUPPORTED,
103
23.8k
                           "No supported data to decode. %s%s%s%s%s%s",
104
23.8k
                           spaces, input_type_label, input_type, comma,
105
23.8k
                           input_structure_label, input_structure);
106
23.8k
        ok = 0;
107
23.8k
    }
108
109
    /* Clear any internally cached passphrase */
110
48.3k
    (void)ossl_pw_clear_passphrase_cache(&ctx->pwdata);
111
112
48.3k
    if (new_bio != NULL) {
113
0
        BIO_pop(new_bio);
114
0
        BIO_free(new_bio);
115
0
    }
116
48.3k
    return ok;
117
48.3k
}
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
52.6k
{
148
52.6k
    BIO *membio;
149
52.6k
    int ret = 0;
150
151
52.6k
    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
52.6k
    membio = BIO_new_mem_buf(*pdata, (int)*pdata_len);
157
52.6k
    if (OSSL_DECODER_from_bio(ctx, membio)) {
158
24.5k
        *pdata_len = (size_t)BIO_get_mem_data(membio, pdata);
159
24.5k
        ret = 1;
160
24.5k
    }
161
52.6k
    BIO_free(membio);
162
163
52.6k
    return ret;
164
52.6k
}
165
166
int OSSL_DECODER_CTX_set_selection(OSSL_DECODER_CTX *ctx, int selection)
167
965
{
168
965
    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
965
    ctx->selection = selection;
178
965
    return 1;
179
965
}
180
181
int OSSL_DECODER_CTX_set_input_type(OSSL_DECODER_CTX *ctx,
182
                                    const char *input_type)
183
53.5k
{
184
53.5k
    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
53.5k
    ctx->start_input_type = input_type;
194
53.5k
    return 1;
195
53.5k
}
196
197
int OSSL_DECODER_CTX_set_input_structure(OSSL_DECODER_CTX *ctx,
198
                                         const char *input_structure)
199
53.5k
{
200
53.5k
    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
53.5k
    ctx->input_structure = input_structure;
210
53.5k
    return 1;
211
53.5k
}
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
177
{
244
177
    OSSL_DECODER_INSTANCE *decoder_inst = NULL;
245
177
    const OSSL_PROVIDER *prov;
246
177
    OSSL_LIB_CTX *libctx;
247
177
    const OSSL_PROPERTY_LIST *props;
248
177
    const OSSL_PROPERTY_DEFINITION *prop;
249
250
177
    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
177
    if ((decoder_inst = OPENSSL_zalloc(sizeof(*decoder_inst))) == NULL)
256
0
        return 0;
257
258
177
    prov = OSSL_DECODER_get0_provider(decoder);
259
177
    libctx = ossl_provider_libctx(prov);
260
177
    props = ossl_decoder_parsed_properties(decoder);
261
177
    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
177
    prop = ossl_property_find_property(props, libctx, "input");
270
177
    decoder_inst->input_type = ossl_property_get_string_value(libctx, prop);
271
177
    decoder_inst->input_type_id = 0;
272
177
    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
177
    prop = ossl_property_find_property(props, libctx, "structure");
283
177
    if (prop != NULL) {
284
102
        decoder_inst->input_structure
285
102
            = ossl_property_get_string_value(libctx, prop);
286
102
    }
287
288
177
    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
177
    decoder_inst->decoder = decoder;
293
177
    decoder_inst->decoderctx = decoderctx;
294
177
    return decoder_inst;
295
0
 err:
296
0
    ossl_decoder_instance_free(decoder_inst);
297
0
    return NULL;
298
177
}
299
300
void ossl_decoder_instance_free(OSSL_DECODER_INSTANCE *decoder_inst)
301
240k
{
302
240k
    if (decoder_inst != NULL) {
303
240k
        if (decoder_inst->decoder != NULL)
304
240k
            decoder_inst->decoder->freectx(decoder_inst->decoderctx);
305
240k
        decoder_inst->decoderctx = NULL;
306
240k
        OSSL_DECODER_free(decoder_inst->decoder);
307
240k
        decoder_inst->decoder = NULL;
308
240k
        OPENSSL_free(decoder_inst);
309
240k
    }
310
240k
}
311
312
OSSL_DECODER_INSTANCE *ossl_decoder_instance_dup(const OSSL_DECODER_INSTANCE *src)
313
240k
{
314
240k
    OSSL_DECODER_INSTANCE *dest;
315
240k
    const OSSL_PROVIDER *prov;
316
240k
    void *provctx;
317
318
240k
    if ((dest = OPENSSL_zalloc(sizeof(*dest))) == NULL)
319
0
        return NULL;
320
321
240k
    *dest = *src;
322
240k
    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
240k
    prov = OSSL_DECODER_get0_provider(dest->decoder);
327
240k
    provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
328
329
240k
    dest->decoderctx = dest->decoder->newctx(provctx);
330
240k
    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
240k
    return dest;
337
338
0
 err:
339
0
    OPENSSL_free(dest);
340
0
    return NULL;
341
240k
}
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
133
{
356
133
    int ok;
357
358
133
    if (ctx->decoder_insts == NULL
359
133
        && (ctx->decoder_insts =
360
31
            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
133
    ok = (sk_OSSL_DECODER_INSTANCE_push(ctx->decoder_insts, di) > 0);
366
133
    if (ok) {
367
133
        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
133
        } OSSL_TRACE_END(DECODER);
375
133
    }
376
133
    return ok;
377
133
}
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
    size_t w_prev_start, w_prev_end; /* "previous" decoders */
423
    size_t 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
2.35k
{
430
2.35k
    STACK_OF(OSSL_DECODER) *skdecoders = arg;
431
432
2.35k
    if (OSSL_DECODER_up_ref(decoder)
433
2.35k
            && !sk_OSSL_DECODER_push(skdecoders, decoder))
434
0
        OSSL_DECODER_free(decoder);
435
2.35k
}
436
437
static void collect_extra_decoder(OSSL_DECODER *decoder, void *arg)
438
20.2k
{
439
20.2k
    struct collect_extra_decoder_data_st *data = arg;
440
20.2k
    size_t j;
441
20.2k
    const OSSL_PROVIDER *prov = OSSL_DECODER_get0_provider(decoder);
442
20.2k
    void *provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
443
444
20.2k
    if (ossl_decoder_fast_is_a(decoder, data->output_type, &data->output_type_id)) {
445
612
        void *decoderctx = NULL;
446
612
        OSSL_DECODER_INSTANCE *di = NULL;
447
448
612
        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
612
        } 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
1.58k
        for (j = data->w_prev_start; j < data->w_new_end; j++) {
463
1.45k
            OSSL_DECODER_INSTANCE *check_inst =
464
1.45k
                sk_OSSL_DECODER_INSTANCE_value(data->ctx->decoder_insts, j);
465
466
1.45k
            if (decoder->base.algodef == check_inst->decoder->base.algodef) {
467
                /* We found it, so don't do anything more */
468
479
                OSSL_TRACE_BEGIN(DECODER) {
469
0
                    BIO_printf(trc_out,
470
0
                               "    REJECTED: already exists in the chain\n");
471
479
                } OSSL_TRACE_END(DECODER);
472
479
                return;
473
479
            }
474
1.45k
        }
475
476
133
        if ((decoderctx = decoder->newctx(provctx)) == NULL)
477
0
            return;
478
479
133
        if (decoder->set_ctx_params != NULL
480
133
            && data->ctx->input_structure != NULL) {
481
133
            OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
482
133
            const char *str = data->ctx->input_structure;
483
484
133
            params[0] =
485
133
                OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
486
133
                                                 (char *)str, 0);
487
133
            if (!decoder->set_ctx_params(decoderctx, params)) {
488
0
                decoder->freectx(decoderctx);
489
0
                return;
490
0
            }
491
133
        }
492
493
133
        if ((di = ossl_decoder_instance_new(decoder, decoderctx)) == NULL) {
494
0
            decoder->freectx(decoderctx);
495
0
            return;
496
0
        }
497
498
133
        switch (data->type_check) {
499
102
        case IS_SAME:
500
            /* If it differs, this is not a decoder to add for now. */
501
102
            if (!ossl_decoder_fast_is_a(decoder,
502
102
                                        OSSL_DECODER_INSTANCE_get_input_type(di),
503
102
                                        &di->input_type_id)) {
504
40
                ossl_decoder_instance_free(di);
505
40
                OSSL_TRACE_BEGIN(DECODER) {
506
0
                    BIO_printf(trc_out,
507
0
                               "    REJECTED: input type doesn't match output type\n");
508
40
                } OSSL_TRACE_END(DECODER);
509
40
                return;
510
40
            }
511
62
            break;
512
62
        case IS_DIFFERENT:
513
            /* If it's the same, this is not a decoder to add for now. */
514
31
            if (ossl_decoder_fast_is_a(decoder,
515
31
                                       OSSL_DECODER_INSTANCE_get_input_type(di),
516
31
                                       &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
31
            break;
525
133
        }
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
93
        if (!ossl_decoder_ctx_add_decoder_inst(data->ctx, di)) {
532
0
            ossl_decoder_instance_free(di);
533
0
            return;
534
0
        }
535
536
93
        data->w_new_end++;
537
93
    }
538
20.2k
}
539
540
int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx,
541
                               OSSL_LIB_CTX *libctx, const char *propq)
542
965
{
543
    /*
544
     * This function goes through existing decoder methods in
545
     * |ctx->decoder_insts|, and tries to fetch new decoders that produce
546
     * what the existing ones want as input, and push those newly fetched
547
     * decoders on top of the same stack.
548
     * Then it does the same again, but looping over the newly fetched
549
     * decoders, until there are no more decoders to be fetched, or
550
     * when we have done this 10 times.
551
     *
552
     * we do this with sliding windows on the stack by keeping track of indexes
553
     * and of the end.
554
     *
555
     * +----------------+
556
     * |   DER to RSA   | <--- w_prev_start
557
     * +----------------+
558
     * |   DER to DSA   |
559
     * +----------------+
560
     * |   DER to DH    |
561
     * +----------------+
562
     * |   PEM to DER   | <--- w_prev_end, w_new_start
563
     * +----------------+
564
     *                    <--- w_new_end
565
     */
566
965
    struct collect_extra_decoder_data_st data;
567
965
    size_t depth = 0; /* Counts the number of iterations */
568
965
    size_t count; /* Calculates how many were added in each iteration */
569
965
    size_t numdecoders;
570
965
    STACK_OF(OSSL_DECODER) *skdecoders;
571
572
965
    if (!ossl_assert(ctx != NULL)) {
573
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
574
0
        return 0;
575
0
    }
576
577
    /*
578
     * If there is no stack of OSSL_DECODER_INSTANCE, we have nothing
579
     * more to add.  That's fine.
580
     */
581
965
    if (ctx->decoder_insts == NULL)
582
934
        return 1;
583
584
31
    OSSL_TRACE_BEGIN(DECODER) {
585
0
        BIO_printf(trc_out, "(ctx %p) Looking for extra decoders\n",
586
0
                   (void *)ctx);
587
31
    } OSSL_TRACE_END(DECODER);
588
589
590
31
    skdecoders = sk_OSSL_DECODER_new_null();
591
31
    if (skdecoders == NULL) {
592
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
593
0
        return 0;
594
0
    }
595
31
    OSSL_DECODER_do_all_provided(libctx, collect_all_decoders, skdecoders);
596
31
    numdecoders = sk_OSSL_DECODER_num(skdecoders);
597
598
31
    memset(&data, 0, sizeof(data));
599
31
    data.ctx = ctx;
600
31
    data.w_prev_start = 0;
601
31
    data.w_prev_end = sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
602
62
    do {
603
62
        size_t i, j;
604
605
62
        data.w_new_start = data.w_new_end = data.w_prev_end;
606
607
        /*
608
         * Two iterations:
609
         * 0.  All decoders that have the same name as their input type.
610
         *     This allows for decoders that unwrap some data in a specific
611
         *     encoding, and pass the result on with the same encoding.
612
         * 1.  All decoders that a different name than their input type.
613
         */
614
62
        for (data.type_check = IS_SAME;
615
186
             data.type_check <= IS_DIFFERENT;
616
124
             data.type_check++) {
617
390
            for (i = data.w_prev_start; i < data.w_prev_end; i++) {
618
266
                OSSL_DECODER_INSTANCE *decoder_inst =
619
266
                    sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
620
621
266
                data.output_type
622
266
                    = OSSL_DECODER_INSTANCE_get_input_type(decoder_inst);
623
624
266
                data.output_type_id = 0;
625
626
20.4k
                for (j = 0; j < numdecoders; j++)
627
20.2k
                    collect_extra_decoder(sk_OSSL_DECODER_value(skdecoders, j),
628
20.2k
                                          &data);
629
266
            }
630
124
        }
631
        /* How many were added in this iteration */
632
62
        count = data.w_new_end - data.w_new_start;
633
634
        /* Slide the "previous decoder" windows */
635
62
        data.w_prev_start = data.w_new_start;
636
62
        data.w_prev_end = data.w_new_end;
637
638
62
        depth++;
639
62
    } while (count != 0 && depth <= 10);
640
641
31
    sk_OSSL_DECODER_pop_free(skdecoders, OSSL_DECODER_free);
642
31
    return 1;
643
31
}
644
645
int OSSL_DECODER_CTX_get_num_decoders(OSSL_DECODER_CTX *ctx)
646
101k
{
647
101k
    if (ctx == NULL || ctx->decoder_insts == NULL)
648
5.19k
        return 0;
649
96.7k
    return sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
650
101k
}
651
652
int OSSL_DECODER_CTX_set_construct(OSSL_DECODER_CTX *ctx,
653
                                   OSSL_DECODER_CONSTRUCT *construct)
654
52.6k
{
655
52.6k
    if (!ossl_assert(ctx != NULL)) {
656
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
657
0
        return 0;
658
0
    }
659
52.6k
    ctx->construct = construct;
660
52.6k
    return 1;
661
52.6k
}
662
663
int OSSL_DECODER_CTX_set_construct_data(OSSL_DECODER_CTX *ctx,
664
                                        void *construct_data)
665
48.4k
{
666
48.4k
    if (!ossl_assert(ctx != NULL)) {
667
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
668
0
        return 0;
669
0
    }
670
48.4k
    ctx->construct_data = construct_data;
671
48.4k
    return 1;
672
48.4k
}
673
674
int OSSL_DECODER_CTX_set_cleanup(OSSL_DECODER_CTX *ctx,
675
                                 OSSL_DECODER_CLEANUP *cleanup)
676
52.6k
{
677
52.6k
    if (!ossl_assert(ctx != NULL)) {
678
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
679
0
        return 0;
680
0
    }
681
52.6k
    ctx->cleanup = cleanup;
682
52.6k
    return 1;
683
52.6k
}
684
685
OSSL_DECODER_CONSTRUCT *
686
OSSL_DECODER_CTX_get_construct(OSSL_DECODER_CTX *ctx)
687
52.6k
{
688
52.6k
    if (ctx == NULL)
689
0
        return NULL;
690
52.6k
    return ctx->construct;
691
52.6k
}
692
693
void *OSSL_DECODER_CTX_get_construct_data(OSSL_DECODER_CTX *ctx)
694
52.6k
{
695
52.6k
    if (ctx == NULL)
696
0
        return NULL;
697
52.6k
    return ctx->construct_data;
698
52.6k
}
699
700
OSSL_DECODER_CLEANUP *
701
OSSL_DECODER_CTX_get_cleanup(OSSL_DECODER_CTX *ctx)
702
52.6k
{
703
52.6k
    if (ctx == NULL)
704
0
        return NULL;
705
52.6k
    return ctx->cleanup;
706
52.6k
}
707
708
int OSSL_DECODER_export(OSSL_DECODER_INSTANCE *decoder_inst,
709
                        void *reference, size_t reference_sz,
710
                        OSSL_CALLBACK *export_cb, void *export_cbarg)
711
0
{
712
0
    OSSL_DECODER *decoder = NULL;
713
0
    void *decoderctx = NULL;
714
715
0
    if (!(ossl_assert(decoder_inst != NULL)
716
0
          && ossl_assert(reference != NULL)
717
0
          && ossl_assert(export_cb != NULL)
718
0
          && ossl_assert(export_cbarg != NULL))) {
719
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
720
0
        return 0;
721
0
    }
722
723
0
    decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
724
0
    decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
725
0
    return decoder->export_object(decoderctx, reference, reference_sz,
726
0
                                  export_cb, export_cbarg);
727
0
}
728
729
OSSL_DECODER *
730
OSSL_DECODER_INSTANCE_get_decoder(OSSL_DECODER_INSTANCE *decoder_inst)
731
385k
{
732
385k
    if (decoder_inst == NULL)
733
0
        return NULL;
734
385k
    return decoder_inst->decoder;
735
385k
}
736
737
void *
738
OSSL_DECODER_INSTANCE_get_decoder_ctx(OSSL_DECODER_INSTANCE *decoder_inst)
739
313k
{
740
313k
    if (decoder_inst == NULL)
741
0
        return NULL;
742
313k
    return decoder_inst->decoderctx;
743
313k
}
744
745
const char *
746
OSSL_DECODER_INSTANCE_get_input_type(OSSL_DECODER_INSTANCE *decoder_inst)
747
240k
{
748
240k
    if (decoder_inst == NULL)
749
0
        return NULL;
750
240k
    return decoder_inst->input_type;
751
240k
}
752
753
const char *
754
OSSL_DECODER_INSTANCE_get_input_structure(OSSL_DECODER_INSTANCE *decoder_inst,
755
                                          int *was_set)
756
240k
{
757
240k
    if (decoder_inst == NULL)
758
0
        return NULL;
759
240k
    *was_set = decoder_inst->flag_input_structure_was_set;
760
240k
    return decoder_inst->input_structure;
761
240k
}
762
763
static int decoder_process(const OSSL_PARAM params[], void *arg)
764
121k
{
765
121k
    struct decoder_process_data_st *data = arg;
766
121k
    OSSL_DECODER_CTX *ctx = data->ctx;
767
121k
    OSSL_DECODER_INSTANCE *decoder_inst = NULL;
768
121k
    OSSL_DECODER *decoder = NULL;
769
121k
    OSSL_CORE_BIO *cbio = NULL;
770
121k
    BIO *bio = data->bio;
771
121k
    long loc;
772
121k
    size_t i;
773
121k
    int ok = 0;
774
    /* For recursions */
775
121k
    struct decoder_process_data_st new_data;
776
121k
    const char *data_type = NULL;
777
121k
    const char *data_structure = NULL;
778
    /* Saved to restore on return, mutated in PEM->DER transition. */
779
121k
    const char *start_input_type = ctx->start_input_type;
780
781
    /*
782
     * This is an indicator up the call stack that something was indeed
783
     * decoded, leading to a recursive call of this function.
784
     */
785
121k
    data->flag_next_level_called = 1;
786
787
121k
    memset(&new_data, 0, sizeof(new_data));
788
121k
    new_data.ctx = data->ctx;
789
121k
    new_data.recursion = data->recursion + 1;
790
791
121k
#define LEVEL_STR ">>>>>>>>>>>>>>>>"
792
121k
#define LEVEL (new_data.recursion < sizeof(LEVEL_STR)                   \
793
0
               ? &LEVEL_STR[sizeof(LEVEL_STR) - new_data.recursion - 1] \
794
0
               : LEVEL_STR "...")
795
796
121k
    if (params == NULL) {
797
        /* First iteration, where we prepare for what is to come */
798
799
48.3k
        OSSL_TRACE_BEGIN(DECODER) {
800
0
            BIO_printf(trc_out,
801
0
                       "(ctx %p) starting to walk the decoder chain\n",
802
0
                       (void *)new_data.ctx);
803
48.3k
        } OSSL_TRACE_END(DECODER);
804
805
48.3k
        data->current_decoder_inst_index =
806
48.3k
            OSSL_DECODER_CTX_get_num_decoders(ctx);
807
808
48.3k
        bio = data->bio;
809
72.7k
    } else {
810
72.7k
        const OSSL_PARAM *p;
811
72.7k
        const char *trace_data_structure;
812
813
72.7k
        decoder_inst =
814
72.7k
            sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts,
815
72.7k
                                           data->current_decoder_inst_index);
816
72.7k
        decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
817
818
72.7k
        data->flag_construct_called = 0;
819
72.7k
        if (ctx->construct != NULL) {
820
72.7k
            int rv;
821
822
72.7k
            OSSL_TRACE_BEGIN(DECODER) {
823
0
                BIO_printf(trc_out,
824
0
                           "(ctx %p) %s Running constructor\n",
825
0
                           (void *)new_data.ctx, LEVEL);
826
72.7k
            } OSSL_TRACE_END(DECODER);
827
828
72.7k
            rv = ctx->construct(decoder_inst, params, ctx->construct_data);
829
830
72.7k
            OSSL_TRACE_BEGIN(DECODER) {
831
0
                BIO_printf(trc_out,
832
0
                           "(ctx %p) %s Running constructor => %d\n",
833
0
                           (void *)new_data.ctx, LEVEL, rv);
834
72.7k
            } OSSL_TRACE_END(DECODER);
835
836
72.7k
            ok = (rv > 0);
837
72.7k
            if (ok) {
838
24.5k
                data->flag_construct_called = 1;
839
24.5k
                goto end;
840
24.5k
            }
841
72.7k
        }
842
843
        /* The constructor didn't return success */
844
845
        /*
846
         * so we try to use the object we got and feed it to any next
847
         * decoder that will take it.  Object references are not
848
         * allowed for this.
849
         * If this data isn't present, decoding has failed.
850
         */
851
852
48.1k
        p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA);
853
48.1k
        if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
854
0
            goto end;
855
48.1k
        new_data.bio = BIO_new_mem_buf(p->data, (int)p->data_size);
856
48.1k
        if (new_data.bio == NULL)
857
0
            goto end;
858
48.1k
        bio = new_data.bio;
859
860
        /* Get the data type if there is one */
861
48.1k
        p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE);
862
48.1k
        if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &data_type))
863
0
            goto end;
864
865
        /* Get the data structure if there is one */
866
48.1k
        p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_STRUCTURE);
867
48.1k
        if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &data_structure))
868
0
            goto end;
869
870
        /* Get the new input type if there is one */
871
48.1k
        p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_INPUT_TYPE);
872
48.1k
        if (p != NULL) {
873
48.1k
            if (!OSSL_PARAM_get_utf8_string_ptr(p, &ctx->start_input_type))
874
0
                goto end;
875
            /*
876
             * When switching PKCS8 from PEM to DER we decrypt the data if needed
877
             * and then determine the algorithm OID.  Likewise, with SPKI, only
878
             * this time sans decryption.
879
             */
880
48.1k
            if (ctx->input_structure != NULL
881
48.1k
                && (OPENSSL_strcasecmp(ctx->input_structure, "SubjectPublicKeyInfo") == 0
882
48.1k
                    || OPENSSL_strcasecmp(data_structure, "PrivateKeyInfo") == 0
883
48.1k
                    || OPENSSL_strcasecmp(ctx->input_structure, "PrivateKeyInfo") == 0))
884
48.1k
                data->flag_input_structure_checked = 1;
885
48.1k
        }
886
887
        /*
888
         * If the data structure is "type-specific" and the data type is
889
         * given, we drop the data structure.  The reasoning is that the
890
         * data type is already enough to find the applicable next decoder,
891
         * so an additional "type-specific" data structure is extraneous.
892
         *
893
         * Furthermore, if the OSSL_DECODER caller asked for a type specific
894
         * structure under another name, such as "DH", we get a mismatch
895
         * if the data structure we just received is "type-specific".
896
         * There's only so much you can do without infusing this code with
897
         * too special knowledge.
898
         */
899
48.1k
        trace_data_structure = data_structure;
900
48.1k
        if (data_type != NULL && data_structure != NULL
901
48.1k
            && OPENSSL_strcasecmp(data_structure, "type-specific") == 0)
902
0
            data_structure = NULL;
903
904
48.1k
        OSSL_TRACE_BEGIN(DECODER) {
905
0
            BIO_printf(trc_out,
906
0
                       "(ctx %p) %s incoming from previous decoder (%p):\n"
907
0
                       "    data type: %s, data structure: %s%s\n",
908
0
                       (void *)new_data.ctx, LEVEL, (void *)decoder,
909
0
                       data_type, trace_data_structure,
910
0
                       (trace_data_structure == data_structure
911
0
                        ? "" : " (dropped)"));
912
48.1k
        } OSSL_TRACE_END(DECODER);
913
48.1k
    }
914
915
    /*
916
     * If we have no more decoders to look through at this point,
917
     * we failed
918
     */
919
96.5k
    if (data->current_decoder_inst_index == 0)
920
0
        goto end;
921
922
96.5k
    if ((loc = BIO_tell(bio)) < 0) {
923
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB);
924
0
        goto end;
925
0
    }
926
927
96.5k
    if ((cbio = ossl_core_bio_new_from_bio(bio)) == NULL) {
928
0
        ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB);
929
0
        goto end;
930
0
    }
931
932
264k
    for (i = data->current_decoder_inst_index; i-- > 0;) {
933
240k
        OSSL_DECODER_INSTANCE *new_decoder_inst =
934
240k
            sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
935
240k
        OSSL_DECODER *new_decoder =
936
240k
            OSSL_DECODER_INSTANCE_get_decoder(new_decoder_inst);
937
240k
        const char *new_decoder_name = NULL;
938
240k
        void *new_decoderctx =
939
240k
            OSSL_DECODER_INSTANCE_get_decoder_ctx(new_decoder_inst);
940
240k
        const char *new_input_type =
941
240k
            OSSL_DECODER_INSTANCE_get_input_type(new_decoder_inst);
942
240k
        int n_i_s_was_set = 0;   /* We don't care here */
943
240k
        const char *new_input_structure =
944
240k
            OSSL_DECODER_INSTANCE_get_input_structure(new_decoder_inst,
945
240k
                                                      &n_i_s_was_set);
946
947
240k
        OSSL_TRACE_BEGIN(DECODER) {
948
0
            new_decoder_name = OSSL_DECODER_get0_name(new_decoder);
949
0
            BIO_printf(trc_out,
950
0
                       "(ctx %p) %s [%u] Considering decoder instance %p (decoder %p):\n"
951
0
                       "    %s with %s\n",
952
0
                       (void *)new_data.ctx, LEVEL, (unsigned int)i,
953
0
                       (void *)new_decoder_inst, (void *)new_decoder,
954
0
                       new_decoder_name,
955
0
                       OSSL_DECODER_get0_properties(new_decoder));
956
240k
        } OSSL_TRACE_END(DECODER);
957
958
        /*
959
         * If |decoder| is NULL, it means we've just started, and the caller
960
         * may have specified what it expects the initial input to be.  If
961
         * that's the case, we do this extra check.
962
         */
963
240k
        if (decoder == NULL && ctx->start_input_type != NULL
964
240k
            && OPENSSL_strcasecmp(ctx->start_input_type, new_input_type) != 0) {
965
48.3k
            OSSL_TRACE_BEGIN(DECODER) {
966
0
                BIO_printf(trc_out,
967
0
                           "(ctx %p) %s [%u] the start input type '%s' doesn't match the input type of the considered decoder, skipping...\n",
968
0
                           (void *)new_data.ctx, LEVEL, (unsigned int)i,
969
0
                           ctx->start_input_type);
970
48.3k
            } OSSL_TRACE_END(DECODER);
971
48.3k
            continue;
972
48.3k
        }
973
974
        /*
975
         * If we have a previous decoder, we check that the input type
976
         * of the next to be used matches the type of this previous one.
977
         * |new_input_type| holds the value of the "input-type" parameter
978
         * for the decoder we're currently considering.
979
         */
980
191k
        if (decoder != NULL && !ossl_decoder_fast_is_a(decoder, new_input_type,
981
94.7k
                                                       &new_decoder_inst->input_type_id)) {
982
0
            OSSL_TRACE_BEGIN(DECODER) {
983
0
                BIO_printf(trc_out,
984
0
                           "(ctx %p) %s [%u] the input type doesn't match the name of the previous decoder (%p), skipping...\n",
985
0
                           (void *)new_data.ctx, LEVEL, (unsigned int)i,
986
0
                           (void *)decoder);
987
0
            } OSSL_TRACE_END(DECODER);
988
0
            continue;
989
0
        }
990
991
        /*
992
         * If the previous decoder gave us a data type, we check to see
993
         * if that matches the decoder we're currently considering.
994
         */
995
191k
        if (data_type != NULL && !OSSL_DECODER_is_a(new_decoder, data_type)) {
996
40.3k
            OSSL_TRACE_BEGIN(DECODER) {
997
0
                BIO_printf(trc_out,
998
0
                           "(ctx %p) %s [%u] the previous decoder's data type doesn't match the name of the considered decoder, skipping...\n",
999
0
                           (void *)new_data.ctx, LEVEL, (unsigned int)i);
1000
40.3k
            } OSSL_TRACE_END(DECODER);
1001
40.3k
            continue;
1002
40.3k
        }
1003
1004
        /*
1005
         * If the previous decoder gave us a data structure name, we check
1006
         * to see that it matches the input data structure of the decoder
1007
         * we're currently considering.
1008
         */
1009
151k
        if (data_structure != NULL
1010
151k
            && (new_input_structure == NULL
1011
54.3k
                || OPENSSL_strcasecmp(data_structure,
1012
54.3k
                                      new_input_structure) != 0)) {
1013
6.21k
            OSSL_TRACE_BEGIN(DECODER) {
1014
0
                BIO_printf(trc_out,
1015
0
                           "(ctx %p) %s [%u] the previous decoder's data structure doesn't match the input structure of the considered decoder, skipping...\n",
1016
0
                           (void *)new_data.ctx, LEVEL, (unsigned int)i);
1017
6.21k
            } OSSL_TRACE_END(DECODER);
1018
6.21k
            continue;
1019
6.21k
        }
1020
1021
        /*
1022
         * If the decoder we're currently considering specifies a structure,
1023
         * and this check hasn't already been done earlier in this chain of
1024
         * decoder_process() calls, check that it matches the user provided
1025
         * input structure, if one is given.
1026
         */
1027
145k
        if (!data->flag_input_structure_checked
1028
145k
            && ctx->input_structure != NULL
1029
145k
            && new_input_structure != NULL) {
1030
48.3k
            data->flag_input_structure_checked = 1;
1031
48.3k
            if (OPENSSL_strcasecmp(new_input_structure,
1032
48.3k
                                   ctx->input_structure) != 0) {
1033
48.3k
                OSSL_TRACE_BEGIN(DECODER) {
1034
0
                    BIO_printf(trc_out,
1035
0
                               "(ctx %p) %s [%u] the previous decoder's data structure doesn't match the input structure given by the user, skipping...\n",
1036
0
                               (void *)new_data.ctx, LEVEL, (unsigned int)i);
1037
48.3k
                } OSSL_TRACE_END(DECODER);
1038
48.3k
                continue;
1039
48.3k
            }
1040
48.3k
        }
1041
1042
        /*
1043
         * Checking the return value of BIO_reset() or BIO_seek() is unsafe.
1044
         * Furthermore, BIO_reset() is unsafe to use if the source BIO happens
1045
         * to be a BIO_s_mem(), because the earlier BIO_tell() gives us zero
1046
         * no matter where we are in the underlying buffer we're reading from.
1047
         *
1048
         * So, we simply do a BIO_seek(), and use BIO_tell() that we're back
1049
         * at the same position.  This is a best effort attempt, but BIO_seek()
1050
         * and BIO_tell() should come as a pair...
1051
         */
1052
96.9k
        (void)BIO_seek(bio, loc);
1053
96.9k
        if (BIO_tell(bio) != loc)
1054
0
            goto end;
1055
1056
        /* Recurse */
1057
96.9k
        OSSL_TRACE_BEGIN(DECODER) {
1058
0
            BIO_printf(trc_out,
1059
0
                       "(ctx %p) %s [%u] Running decoder instance %s (%p)\n",
1060
0
                       (void *)new_data.ctx, LEVEL, (unsigned int)i,
1061
0
                       new_decoder_name, (void *)new_decoder_inst);
1062
96.9k
        } OSSL_TRACE_END(DECODER);
1063
1064
        /*
1065
         * We only care about errors reported from decoder implementations
1066
         * if it returns false (i.e. there was a fatal error).
1067
         */
1068
96.9k
        ERR_set_mark();
1069
1070
96.9k
        new_data.current_decoder_inst_index = i;
1071
96.9k
        new_data.flag_input_structure_checked
1072
96.9k
            = data->flag_input_structure_checked;
1073
96.9k
        ok = new_decoder->decode(new_decoderctx, cbio,
1074
96.9k
                                 new_data.ctx->selection,
1075
96.9k
                                 decoder_process, &new_data,
1076
96.9k
                                 ossl_pw_passphrase_callback_dec,
1077
96.9k
                                 &new_data.ctx->pwdata);
1078
1079
96.9k
        OSSL_TRACE_BEGIN(DECODER) {
1080
0
            BIO_printf(trc_out,
1081
0
                       "(ctx %p) %s [%u] Running decoder instance %s (%p) => %d"
1082
0
                       " (recursed further: %s, construct called: %s)\n",
1083
0
                       (void *)new_data.ctx, LEVEL, (unsigned int)i,
1084
0
                       new_decoder_name, (void *)new_decoder_inst, ok,
1085
0
                       new_data.flag_next_level_called ? "yes" : "no",
1086
0
                       new_data.flag_construct_called ? "yes" : "no");
1087
96.9k
        } OSSL_TRACE_END(DECODER);
1088
1089
96.9k
        data->flag_construct_called = new_data.flag_construct_called;
1090
1091
        /* Break on error or if we tried to construct an object already */
1092
96.9k
        if (!ok || data->flag_construct_called) {
1093
49.1k
            ERR_clear_last_mark();
1094
49.1k
            break;
1095
49.1k
        }
1096
47.8k
        ERR_pop_to_mark();
1097
1098
        /*
1099
         * Break if the decoder implementation that we called recursed, since
1100
         * that indicates that it successfully decoded something.
1101
         */
1102
47.8k
        if (new_data.flag_next_level_called)
1103
23.6k
            break;
1104
47.8k
    }
1105
1106
121k
 end:
1107
121k
    ossl_core_bio_free(cbio);
1108
121k
    BIO_free(new_data.bio);
1109
121k
    ctx->start_input_type = start_input_type;
1110
121k
    return ok;
1111
96.5k
}