Coverage Report

Created: 2025-12-31 06:58

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