Coverage Report

Created: 2025-06-13 06:58

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