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