Coverage Report

Created: 2025-06-13 06:58

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