Coverage Report

Created: 2025-12-08 06:22

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