Coverage Report

Created: 2023-06-08 06:40

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