decoder_process: data_structure can be NULL
[openssl.git] / crypto / encode_decode / decoder_lib.c
1 /*
2  * Copyright 2020-2021 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/passphrase.h"
20 #include "internal/bio.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 static int decoder_process(const OSSL_PARAM params[], void *arg);
38
39 int OSSL_DECODER_from_bio(OSSL_DECODER_CTX *ctx, BIO *in)
40 {
41     struct decoder_process_data_st data;
42     int ok = 0;
43     BIO *new_bio = NULL;
44
45     if (BIO_tell(in) < 0) {
46         new_bio = BIO_new(BIO_f_readbuffer());
47         if (new_bio == NULL)
48             return 0;
49         in = BIO_push(new_bio, in);
50     }
51     memset(&data, 0, sizeof(data));
52     data.ctx = ctx;
53     data.bio = in;
54
55     /* Enable passphrase caching */
56     (void)ossl_pw_enable_passphrase_caching(&ctx->pwdata);
57
58     ok = decoder_process(NULL, &data);
59
60     /* Clear any internally cached passphrase */
61     (void)ossl_pw_clear_passphrase_cache(&ctx->pwdata);
62
63     if (new_bio != NULL) {
64         BIO_pop(new_bio);
65         BIO_free(new_bio);
66     }
67     return ok;
68 }
69
70 #ifndef OPENSSL_NO_STDIO
71 static BIO *bio_from_file(FILE *fp)
72 {
73     BIO *b;
74
75     if ((b = BIO_new(BIO_s_file())) == NULL) {
76         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB);
77         return NULL;
78     }
79     BIO_set_fp(b, fp, BIO_NOCLOSE);
80     return b;
81 }
82
83 int OSSL_DECODER_from_fp(OSSL_DECODER_CTX *ctx, FILE *fp)
84 {
85     BIO *b = bio_from_file(fp);
86     int ret = 0;
87
88     if (b != NULL)
89         ret = OSSL_DECODER_from_bio(ctx, b);
90
91     BIO_free(b);
92     return ret;
93 }
94 #endif
95
96 int OSSL_DECODER_from_data(OSSL_DECODER_CTX *ctx, const unsigned char **pdata,
97                            size_t *pdata_len)
98 {
99     BIO *membio;
100     int ret = 0;
101
102     if (pdata == NULL || *pdata == NULL || pdata_len == NULL) {
103         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
104         return 0;
105     }
106
107     membio = BIO_new_mem_buf(*pdata, (int)*pdata_len);
108     if (OSSL_DECODER_from_bio(ctx, membio)) {
109         *pdata_len = (size_t)BIO_get_mem_data(membio, pdata);
110         ret = 1;
111     }
112     BIO_free(membio);
113
114     return ret;
115 }
116
117 int OSSL_DECODER_CTX_set_selection(OSSL_DECODER_CTX *ctx, int selection)
118 {
119     if (!ossl_assert(ctx != NULL)) {
120         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
121         return 0;
122     }
123
124     /*
125      * 0 is a valid selection, and means that the caller leaves
126      * it to code to discover what the selection is.
127      */
128     ctx->selection = selection;
129     return 1;
130 }
131
132 int OSSL_DECODER_CTX_set_input_type(OSSL_DECODER_CTX *ctx,
133                                     const char *input_type)
134 {
135     if (!ossl_assert(ctx != NULL)) {
136         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
137         return 0;
138     }
139
140     /*
141      * NULL is a valid starting input type, and means that the caller leaves
142      * it to code to discover what the starting input type is.
143      */
144     ctx->start_input_type = input_type;
145     return 1;
146 }
147
148 int OSSL_DECODER_CTX_set_input_structure(OSSL_DECODER_CTX *ctx,
149                                          const char *input_structure)
150 {
151     if (!ossl_assert(ctx != NULL)) {
152         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
153         return 0;
154     }
155
156     /*
157      * NULL is a valid starting input type, and means that the caller leaves
158      * it to code to discover what the starting input type is.
159      */
160     ctx->input_structure = input_structure;
161     return 1;
162 }
163
164 OSSL_DECODER_INSTANCE *ossl_decoder_instance_new(OSSL_DECODER *decoder,
165                                                  void *decoderctx)
166 {
167     OSSL_DECODER_INSTANCE *decoder_inst = NULL;
168     OSSL_PARAM params[3];
169
170     if (!ossl_assert(decoder != NULL)) {
171         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
172         return 0;
173     }
174
175     if (decoder->get_params == NULL) {
176         ERR_raise(ERR_LIB_OSSL_DECODER,
177                   OSSL_DECODER_R_MISSING_GET_PARAMS);
178         return 0;
179     }
180
181     if ((decoder_inst = OPENSSL_zalloc(sizeof(*decoder_inst))) == NULL) {
182         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
183         return 0;
184     }
185     if (!OSSL_DECODER_up_ref(decoder)) {
186         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
187         goto err;
188     }
189
190     /* Cache the input type for this decoder */
191     params[0] =
192         OSSL_PARAM_construct_utf8_ptr(OSSL_DECODER_PARAM_INPUT_TYPE,
193                                       (char **)&decoder_inst->input_type, 0);
194     params[1] =
195         OSSL_PARAM_construct_utf8_ptr(OSSL_DECODER_PARAM_INPUT_STRUCTURE,
196                                       (char **)&decoder_inst->input_structure,
197                                       0);
198     params[2] = OSSL_PARAM_construct_end();
199
200     if (!decoder->get_params(params)
201         || !OSSL_PARAM_modified(&params[0]))
202         goto err;
203
204     decoder_inst->flag_input_structure_was_set =
205         OSSL_PARAM_modified(&params[1]);
206     decoder_inst->decoder = decoder;
207     decoder_inst->decoderctx = decoderctx;
208     return decoder_inst;
209  err:
210     ossl_decoder_instance_free(decoder_inst);
211     return NULL;
212 }
213
214 void ossl_decoder_instance_free(OSSL_DECODER_INSTANCE *decoder_inst)
215 {
216     if (decoder_inst != NULL) {
217         if (decoder_inst->decoder != NULL)
218             decoder_inst->decoder->freectx(decoder_inst->decoderctx);
219         decoder_inst->decoderctx = NULL;
220         OSSL_DECODER_free(decoder_inst->decoder);
221         decoder_inst->decoder = NULL;
222         OPENSSL_free(decoder_inst);
223     }
224 }
225
226 int ossl_decoder_ctx_add_decoder_inst(OSSL_DECODER_CTX *ctx,
227                                       OSSL_DECODER_INSTANCE *di)
228 {
229     int ok;
230
231     if (ctx->decoder_insts == NULL
232         && (ctx->decoder_insts =
233             sk_OSSL_DECODER_INSTANCE_new_null()) == NULL) {
234         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
235         return 0;
236     }
237
238     ok = (sk_OSSL_DECODER_INSTANCE_push(ctx->decoder_insts, di) > 0);
239     if (ok) {
240         OSSL_TRACE_BEGIN(DECODER) {
241             BIO_printf(trc_out,
242                        "(ctx %p) Added decoder instance %p (decoder %p) with:\n",
243                        (void *)ctx, (void *)di, (void *)di->decoder);
244             BIO_printf(trc_out,
245                        "    input type: %s, input structure: %s\n",
246                        di->input_type, di->input_structure);
247         } OSSL_TRACE_END(DECODER);
248     }
249     return ok;
250 }
251
252 int OSSL_DECODER_CTX_add_decoder(OSSL_DECODER_CTX *ctx, OSSL_DECODER *decoder)
253 {
254     OSSL_DECODER_INSTANCE *decoder_inst = NULL;
255     const OSSL_PROVIDER *prov = NULL;
256     void *decoderctx = NULL;
257     void *provctx = NULL;
258
259     if (!ossl_assert(ctx != NULL) || !ossl_assert(decoder != NULL)) {
260         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
261         return 0;
262     }
263
264     prov = OSSL_DECODER_provider(decoder);
265     provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
266
267     if ((decoderctx = decoder->newctx(provctx)) == NULL
268         || (decoder_inst =
269             ossl_decoder_instance_new(decoder, decoderctx)) == NULL)
270         goto err;
271     /* Avoid double free of decoderctx on further errors */
272     decoderctx = NULL;
273
274     if (!ossl_decoder_ctx_add_decoder_inst(ctx, decoder_inst))
275         goto err;
276
277     return 1;
278  err:
279     ossl_decoder_instance_free(decoder_inst);
280     if (decoderctx != NULL)
281         decoder->freectx(decoderctx);
282     return 0;
283 }
284
285 int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx,
286                                OSSL_LIB_CTX *libctx, const char *propq)
287 {
288     /*
289      * This function goes through existing decoder methods in
290      * |ctx->decoder_insts|, and tries to fetch new decoders that produce
291      * what the existing ones want as input, and push those newly fetched
292      * decoders on top of the same stack.
293      * Then it does the same again, but looping over the newly fetched
294      * decoders, until there are no more decoders to be fetched, or
295      * when we have done this 10 times.
296      *
297      * we do this with sliding windows on the stack by keeping track of indexes
298      * and of the end.
299      *
300      * +----------------+
301      * |   DER to RSA   | <--- w_prev_start
302      * +----------------+
303      * |   DER to DSA   |
304      * +----------------+
305      * |   DER to DH    |
306      * +----------------+
307      * |   PEM to DER   | <--- w_prev_end, w_new_start
308      * +----------------+
309      *                    <--- w_new_end
310      */
311     size_t w_prev_start, w_prev_end; /* "previous" decoders */
312     size_t w_new_start, w_new_end;   /* "new" decoders */
313     size_t count = 0; /* Calculates how many were added in each iteration */
314     size_t depth = 0; /* Counts the number of iterations */
315
316     if (!ossl_assert(ctx != NULL)) {
317         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
318         return 0;
319     }
320
321     /*
322      * If there is no stack of OSSL_DECODER_INSTANCE, we have nothing
323      * more to add.  That's fine.
324      */
325     if (ctx->decoder_insts == NULL)
326         return 1;
327
328     w_prev_start = 0;
329     w_prev_end = sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
330     do {
331         size_t i;
332
333         w_new_start = w_new_end = w_prev_end;
334
335         for (i = w_prev_start; i < w_prev_end; i++) {
336             OSSL_DECODER_INSTANCE *decoder_inst =
337                 sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
338             const char *input_type =
339                 OSSL_DECODER_INSTANCE_get_input_type(decoder_inst);
340             OSSL_DECODER *decoder = NULL;
341
342             /*
343              * If the caller has specified what the initial input should be,
344              * and the decoder implementation we're looking at has that
345              * input type, there's no point adding on more implementations
346              * on top of this one, so we don't.
347              */
348             if (ctx->start_input_type != NULL
349                 && strcasecmp(ctx->start_input_type, input_type) == 0)
350                 continue;
351
352             ERR_set_mark();
353             decoder = OSSL_DECODER_fetch(libctx, input_type, propq);
354             ERR_pop_to_mark();
355
356             if (decoder != NULL) {
357                 size_t j;
358
359                 /*
360                  * Check that we don't already have this decoder in our
361                  * stack We only need to check among the newly added ones.
362                  */
363                 for (j = w_new_start; j < w_new_end; j++) {
364                     OSSL_DECODER_INSTANCE *check_inst =
365                         sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, j);
366
367                     if (decoder == check_inst->decoder) {
368                         /* We found it, so drop the new fetch */
369                         OSSL_DECODER_free(decoder);
370                         decoder = NULL;
371                         break;
372                     }
373                 }
374             }
375
376             if (decoder == NULL)
377                 continue;
378
379             /*
380              * Apart from keeping w_new_end up to date, We don't care about
381              * errors here.  If it doesn't collect, then it doesn't...
382              */
383             if (OSSL_DECODER_CTX_add_decoder(ctx, decoder)) /* ref++ */
384                 w_new_end++;
385             OSSL_DECODER_free(decoder); /* ref-- */
386         }
387         /* How many were added in this iteration */
388         count = w_new_end - w_new_start;
389
390         /* Slide the "previous decoder" windows */
391         w_prev_start = w_new_start;
392         w_prev_end = w_new_end;
393
394         depth++;
395     } while (count != 0 && depth <= 10);
396
397     return 1;
398 }
399
400 int OSSL_DECODER_CTX_get_num_decoders(OSSL_DECODER_CTX *ctx)
401 {
402     if (ctx == NULL || ctx->decoder_insts == NULL)
403         return 0;
404     return sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
405 }
406
407 int OSSL_DECODER_CTX_set_construct(OSSL_DECODER_CTX *ctx,
408                                    OSSL_DECODER_CONSTRUCT *construct)
409 {
410     if (!ossl_assert(ctx != NULL)) {
411         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
412         return 0;
413     }
414     ctx->construct = construct;
415     return 1;
416 }
417
418 int OSSL_DECODER_CTX_set_construct_data(OSSL_DECODER_CTX *ctx,
419                                         void *construct_data)
420 {
421     if (!ossl_assert(ctx != NULL)) {
422         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
423         return 0;
424     }
425     ctx->construct_data = construct_data;
426     return 1;
427 }
428
429 int OSSL_DECODER_CTX_set_cleanup(OSSL_DECODER_CTX *ctx,
430                                  OSSL_DECODER_CLEANUP *cleanup)
431 {
432     if (!ossl_assert(ctx != NULL)) {
433         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
434         return 0;
435     }
436     ctx->cleanup = cleanup;
437     return 1;
438 }
439
440 OSSL_DECODER_CONSTRUCT *
441 OSSL_DECODER_CTX_get_construct(OSSL_DECODER_CTX *ctx)
442 {
443     if (ctx == NULL)
444         return NULL;
445     return ctx->construct;
446 }
447
448 void *OSSL_DECODER_CTX_get_construct_data(OSSL_DECODER_CTX *ctx)
449 {
450     if (ctx == NULL)
451         return NULL;
452     return ctx->construct_data;
453 }
454
455 OSSL_DECODER_CLEANUP *
456 OSSL_DECODER_CTX_get_cleanup(OSSL_DECODER_CTX *ctx)
457 {
458     if (ctx == NULL)
459         return NULL;
460     return ctx->cleanup;
461 }
462
463 int OSSL_DECODER_export(OSSL_DECODER_INSTANCE *decoder_inst,
464                         void *reference, size_t reference_sz,
465                         OSSL_CALLBACK *export_cb, void *export_cbarg)
466 {
467     OSSL_DECODER *decoder = NULL;
468     void *decoderctx = NULL;
469
470     if (!(ossl_assert(decoder_inst != NULL)
471           && ossl_assert(reference != NULL)
472           && ossl_assert(export_cb != NULL)
473           && ossl_assert(export_cbarg != NULL))) {
474         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
475         return 0;
476     }
477
478     decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
479     decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
480     return decoder->export_object(decoderctx, reference, reference_sz,
481                                   export_cb, export_cbarg);
482 }
483
484 OSSL_DECODER *
485 OSSL_DECODER_INSTANCE_get_decoder(OSSL_DECODER_INSTANCE *decoder_inst)
486 {
487     if (decoder_inst == NULL)
488         return NULL;
489     return decoder_inst->decoder;
490 }
491
492 void *
493 OSSL_DECODER_INSTANCE_get_decoder_ctx(OSSL_DECODER_INSTANCE *decoder_inst)
494 {
495     if (decoder_inst == NULL)
496         return NULL;
497     return decoder_inst->decoderctx;
498 }
499
500 const char *
501 OSSL_DECODER_INSTANCE_get_input_type(OSSL_DECODER_INSTANCE *decoder_inst)
502 {
503     if (decoder_inst == NULL)
504         return NULL;
505     return decoder_inst->input_type;
506 }
507
508 const char *
509 OSSL_DECODER_INSTANCE_get_input_structure(OSSL_DECODER_INSTANCE *decoder_inst,
510                                           int *was_set)
511 {
512     if (decoder_inst == NULL)
513         return NULL;
514     *was_set = decoder_inst->flag_input_structure_was_set;
515     return decoder_inst->input_structure;
516 }
517
518 static int decoder_process(const OSSL_PARAM params[], void *arg)
519 {
520     struct decoder_process_data_st *data = arg;
521     OSSL_DECODER_CTX *ctx = data->ctx;
522     OSSL_DECODER_INSTANCE *decoder_inst = NULL;
523     OSSL_DECODER *decoder = NULL;
524     OSSL_CORE_BIO *cbio = NULL;
525     BIO *bio = data->bio;
526     long loc;
527     size_t i;
528     int err, lib, reason, ok = 0;
529     /* For recursions */
530     struct decoder_process_data_st new_data;
531     const char *data_type = NULL;
532     const char *data_structure = NULL;
533
534     memset(&new_data, 0, sizeof(new_data));
535     new_data.ctx = data->ctx;
536     new_data.recursion = data->recursion + 1;
537
538 #define LEVEL_STR ">>>>>>>>>>>>>>>>"
539 #define LEVEL (new_data.recursion < sizeof(LEVEL_STR)                   \
540                ? &LEVEL_STR[sizeof(LEVEL_STR) - new_data.recursion - 1] \
541                : LEVEL_STR "...")
542
543     if (params == NULL) {
544         /* First iteration, where we prepare for what is to come */
545
546         OSSL_TRACE_BEGIN(DECODER) {
547             BIO_printf(trc_out,
548                        "(ctx %p) starting to walk the decoder chain\n",
549                        (void *)new_data.ctx);
550         } OSSL_TRACE_END(DECODER);
551
552         data->current_decoder_inst_index =
553             OSSL_DECODER_CTX_get_num_decoders(ctx);
554
555         bio = data->bio;
556     } else {
557         const OSSL_PARAM *p;
558         const char *trace_data_structure;
559
560         decoder_inst =
561             sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts,
562                                            data->current_decoder_inst_index);
563         decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
564
565         if (ctx->construct != NULL
566             && ctx->construct(decoder_inst, params, ctx->construct_data)) {
567             ok = 1;
568             goto end;
569         }
570
571         /* The constructor didn't return success */
572
573         /*
574          * so we try to use the object we got and feed it to any next
575          * decoder that will take it.  Object references are not
576          * allowed for this.
577          * If this data isn't present, decoding has failed.
578          */
579
580         p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA);
581         if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
582             goto end;
583         new_data.bio = BIO_new_mem_buf(p->data, (int)p->data_size);
584         if (new_data.bio == NULL)
585             goto end;
586         bio = new_data.bio;
587
588         /* Get the data type if there is one */
589         p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE);
590         if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &data_type))
591             goto end;
592
593         /* Get the data structure if there is one */
594         p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_STRUCTURE);
595         if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &data_structure))
596             goto end;
597
598         /*
599          * If the data structure is "type-specific" and the data type is
600          * given, we drop the data structure.  The reasoning is that the
601          * data type is already enough to find the applicable next decoder,
602          * so an additional "type-specific" data structure is extraneous.
603          *
604          * Furthermore, if the OSSL_DECODER caller asked for a type specific
605          * structure under another name, such as "DH", we get a mismatch
606          * if the data structure we just received is "type-specific".
607          * There's only so much you can do without infusing this code with
608          * too special knowledge.
609          */
610         trace_data_structure = data_structure;
611         if (data_type != NULL && data_structure != NULL
612             && strcasecmp(data_structure, "type-specific") == 0)
613             data_structure = NULL;
614
615         OSSL_TRACE_BEGIN(DECODER) {
616             BIO_printf(trc_out,
617                        "(ctx %p) %s incoming from previous decoder (%p):\n"
618                        "    data type: %s, data structure: %s%s\n",
619                        (void *)new_data.ctx, LEVEL, (void *)decoder,
620                        data_type, trace_data_structure,
621                        (trace_data_structure == data_structure
622                         ? "" : " (dropped)"));
623         } OSSL_TRACE_END(DECODER);
624     }
625
626     /*
627      * If we have no more decoders to look through at this point,
628      * we failed
629      */
630     if (data->current_decoder_inst_index == 0)
631         goto end;
632
633     if ((loc = BIO_tell(bio)) < 0) {
634         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB);
635         goto end;
636     }
637
638     if ((cbio = ossl_core_bio_new_from_bio(bio)) == NULL) {
639         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
640         goto end;
641     }
642
643     for (i = data->current_decoder_inst_index; i-- > 0;) {
644         OSSL_DECODER_INSTANCE *new_decoder_inst =
645             sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
646         OSSL_DECODER *new_decoder =
647             OSSL_DECODER_INSTANCE_get_decoder(new_decoder_inst);
648         void *new_decoderctx =
649             OSSL_DECODER_INSTANCE_get_decoder_ctx(new_decoder_inst);
650         const char *new_input_type =
651             OSSL_DECODER_INSTANCE_get_input_type(new_decoder_inst);
652         int n_i_s_was_set = 0;   /* We don't care here */
653         const char *new_input_structure =
654             OSSL_DECODER_INSTANCE_get_input_structure(new_decoder_inst,
655                                                       &n_i_s_was_set);
656
657         OSSL_TRACE_BEGIN(DECODER) {
658             BIO_printf(trc_out,
659                        "(ctx %p) %s [%u] Considering decoder instance %p, which has:\n"
660                        "    input type: %s, input structure: %s, decoder: %p\n",
661                        (void *)new_data.ctx, LEVEL, (unsigned int)i,
662                        (void *)new_decoder_inst, new_input_type,
663                        new_input_structure, (void *)new_decoder);
664         } OSSL_TRACE_END(DECODER);
665
666         /*
667          * If |decoder| is NULL, it means we've just started, and the caller
668          * may have specified what it expects the initial input to be.  If
669          * that's the case, we do this extra check.
670          */
671         if (decoder == NULL && ctx->start_input_type != NULL
672             && strcasecmp(ctx->start_input_type, new_input_type) != 0) {
673             OSSL_TRACE_BEGIN(DECODER) {
674                 BIO_printf(trc_out,
675                            "(ctx %p) %s [%u] the start input type '%s' doesn't match the input type of the considered decoder, skipping...\n",
676                            (void *)new_data.ctx, LEVEL, (unsigned int)i,
677                            ctx->start_input_type);
678             } OSSL_TRACE_END(DECODER);
679             continue;
680         }
681
682         /*
683          * If we have a previous decoder, we check that the input type
684          * of the next to be used matches the type of this previous one.
685          * |new_input_type| holds the value of the "input-type" parameter
686          * for the decoder we're currently considering.
687          */
688         if (decoder != NULL && !OSSL_DECODER_is_a(decoder, new_input_type)) {
689             OSSL_TRACE_BEGIN(DECODER) {
690                 BIO_printf(trc_out,
691                            "(ctx %p) %s [%u] the input type doesn't match the name of the previous decoder (%p), skipping...\n",
692                            (void *)new_data.ctx, LEVEL, (unsigned int)i,
693                            (void *)decoder);
694             } OSSL_TRACE_END(DECODER);
695             continue;
696         }
697
698         /*
699          * If the previous decoder gave us a data type, we check to see
700          * if that matches the decoder we're currently considering.
701          */
702         if (data_type != NULL && !OSSL_DECODER_is_a(new_decoder, data_type)) {
703             OSSL_TRACE_BEGIN(DECODER) {
704                 BIO_printf(trc_out,
705                            "(ctx %p) %s [%u] the previous decoder's data type doesn't match the name of the considered decoder, skipping...\n",
706                            (void *)new_data.ctx, LEVEL, (unsigned int)i);
707             } OSSL_TRACE_END(DECODER);
708             continue;
709         }
710
711         /*
712          * If the previous decoder gave us a data structure name, we check
713          * to see that it matches the input data structure of the decoder
714          * we're currently considering.
715          */
716         if (data_structure != NULL
717             && (new_input_structure == NULL
718                 || strcasecmp(data_structure, new_input_structure) != 0)) {
719             OSSL_TRACE_BEGIN(DECODER) {
720                 BIO_printf(trc_out,
721                            "(ctx %p) %s [%u] the previous decoder's data structure doesn't match the input structure of the considered decoder, skipping...\n",
722                            (void *)new_data.ctx, LEVEL, (unsigned int)i);
723             } OSSL_TRACE_END(DECODER);
724             continue;
725         }
726
727         /*
728          * Checking the return value of BIO_reset() or BIO_seek() is unsafe.
729          * Furthermore, BIO_reset() is unsafe to use if the source BIO happens
730          * to be a BIO_s_mem(), because the earlier BIO_tell() gives us zero
731          * no matter where we are in the underlying buffer we're reading from.
732          *
733          * So, we simply do a BIO_seek(), and use BIO_tell() that we're back
734          * at the same position.  This is a best effort attempt, but BIO_seek()
735          * and BIO_tell() should come as a pair...
736          */
737         (void)BIO_seek(bio, loc);
738         if (BIO_tell(bio) != loc)
739             goto end;
740
741         /* Recurse */
742         OSSL_TRACE_BEGIN(DECODER) {
743             BIO_printf(trc_out,
744                        "(ctx %p) %s [%u] Running decoder instance %p\n",
745                        (void *)new_data.ctx, LEVEL, (unsigned int)i,
746                        (void *)new_decoder_inst);
747         } OSSL_TRACE_END(DECODER);
748
749         new_data.current_decoder_inst_index = i;
750         ok = new_decoder->decode(new_decoderctx, cbio,
751                                  new_data.ctx->selection,
752                                  decoder_process, &new_data,
753                                  ossl_pw_passphrase_callback_dec,
754                                  &new_data.ctx->pwdata);
755
756         OSSL_TRACE_BEGIN(DECODER) {
757             BIO_printf(trc_out,
758                        "(ctx %p) %s [%u] Running decoder instance %p => %d\n",
759                        (void *)new_data.ctx, LEVEL, (unsigned int)i,
760                        (void *)new_decoder_inst, ok);
761         } OSSL_TRACE_END(DECODER);
762
763         if (ok)
764             break;
765
766         /*
767          * These errors are assumed to come from ossl_store_handle_load_result()
768          * in crypto/store/store_result.c.  They are currently considered fatal
769          * errors, so we preserve them in the error queue and stop.
770          */
771         err = ERR_peek_last_error();
772         lib = ERR_GET_LIB(err);
773         reason = ERR_GET_REASON(err);
774         if ((lib == ERR_LIB_EVP
775              && reason == EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM)
776 #ifndef OPENSSL_NO_EC
777             || (lib == ERR_LIB_EC && reason == EC_R_UNKNOWN_GROUP)
778 #endif
779             || (lib == ERR_LIB_X509 && reason == X509_R_UNSUPPORTED_ALGORITHM)
780             || (lib == ERR_LIB_PKCS12
781                 && reason == PKCS12_R_PKCS12_CIPHERFINAL_ERROR))
782             goto end;
783     }
784
785  end:
786     ossl_core_bio_free(cbio);
787     BIO_free(new_data.bio);
788     return ok;
789 }