Coverage Report

Created: 2025-12-31 06:58

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