/src/openssl/crypto/encode_decode/decoder_lib.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2020-2025 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 | | int current_decoder_inst_index; |
34 | | /* For tracing, count recursion level */ |
35 | | int 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 (ctx == NULL) { |
169 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
170 | 0 | return 0; |
171 | 0 | } |
172 | | |
173 | 0 | if (ctx->finalized != 0) { |
174 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
175 | 0 | return 0; |
176 | 0 | } |
177 | | |
178 | | /* |
179 | | * 0 is a valid selection, and means that the caller leaves |
180 | | * it to code to discover what the selection is. |
181 | | */ |
182 | 0 | ctx->selection = selection; |
183 | 0 | return 1; |
184 | 0 | } |
185 | | |
186 | | int OSSL_DECODER_CTX_set_input_type(OSSL_DECODER_CTX *ctx, |
187 | | const char *input_type) |
188 | 0 | { |
189 | 0 | if (ctx == NULL) { |
190 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
191 | 0 | return 0; |
192 | 0 | } |
193 | | |
194 | 0 | if (ctx->finalized != 0) { |
195 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
196 | 0 | return 0; |
197 | 0 | } |
198 | | |
199 | | /* |
200 | | * NULL is a valid starting input type, and means that the caller leaves |
201 | | * it to code to discover what the starting input type is. |
202 | | */ |
203 | 0 | ctx->start_input_type = input_type; |
204 | 0 | return 1; |
205 | 0 | } |
206 | | |
207 | | int OSSL_DECODER_CTX_set_input_structure(OSSL_DECODER_CTX *ctx, |
208 | | const char *input_structure) |
209 | 0 | { |
210 | 0 | if (ctx == NULL) { |
211 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
212 | 0 | return 0; |
213 | 0 | } |
214 | | |
215 | 0 | if (ctx->finalized != 0) { |
216 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
217 | 0 | return 0; |
218 | 0 | } |
219 | | |
220 | | /* |
221 | | * NULL is a valid starting input structure, and means that the caller |
222 | | * leaves it to code to discover what the starting input structure is. |
223 | | */ |
224 | 0 | ctx->input_structure = input_structure; |
225 | 0 | return 1; |
226 | 0 | } |
227 | | |
228 | | OSSL_DECODER_INSTANCE * |
229 | | ossl_decoder_instance_new_forprov(OSSL_DECODER *decoder, void *provctx, |
230 | | const char *input_structure) |
231 | 0 | { |
232 | 0 | void *decoderctx; |
233 | |
|
234 | 0 | if (!ossl_assert(decoder != NULL)) { |
235 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
236 | 0 | return 0; |
237 | 0 | } |
238 | | |
239 | 0 | decoderctx = decoder->newctx(provctx); |
240 | 0 | if (decoderctx == NULL) |
241 | 0 | return 0; |
242 | 0 | if (input_structure != NULL && decoder->set_ctx_params != NULL) { |
243 | 0 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
244 | |
|
245 | 0 | params[0] = |
246 | 0 | OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, |
247 | 0 | (char *)input_structure, 0); |
248 | 0 | if (!decoder->set_ctx_params(decoderctx, params)) { |
249 | 0 | decoder->freectx(decoderctx); |
250 | 0 | return 0; |
251 | 0 | } |
252 | 0 | } |
253 | 0 | return ossl_decoder_instance_new(decoder, decoderctx); |
254 | 0 | } |
255 | | |
256 | | OSSL_DECODER_INSTANCE *ossl_decoder_instance_new(OSSL_DECODER *decoder, |
257 | | void *decoderctx) |
258 | 0 | { |
259 | 0 | OSSL_DECODER_INSTANCE *decoder_inst = NULL; |
260 | 0 | const OSSL_PROVIDER *prov; |
261 | 0 | OSSL_LIB_CTX *libctx; |
262 | 0 | const OSSL_PROPERTY_LIST *props; |
263 | 0 | const OSSL_PROPERTY_DEFINITION *prop; |
264 | |
|
265 | 0 | if (!ossl_assert(decoder != NULL)) { |
266 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
267 | 0 | return 0; |
268 | 0 | } |
269 | | |
270 | 0 | if ((decoder_inst = OPENSSL_zalloc(sizeof(*decoder_inst))) == NULL) |
271 | 0 | return 0; |
272 | | |
273 | 0 | prov = OSSL_DECODER_get0_provider(decoder); |
274 | 0 | libctx = ossl_provider_libctx(prov); |
275 | 0 | props = ossl_decoder_parsed_properties(decoder); |
276 | 0 | if (props == NULL) { |
277 | 0 | ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION, |
278 | 0 | "there are no property definitions with decoder %s", |
279 | 0 | OSSL_DECODER_get0_name(decoder)); |
280 | 0 | goto err; |
281 | 0 | } |
282 | | |
283 | | /* The "input" property is mandatory */ |
284 | 0 | prop = ossl_property_find_property(props, libctx, "input"); |
285 | 0 | decoder_inst->input_type = ossl_property_get_string_value(libctx, prop); |
286 | 0 | decoder_inst->input_type_id = 0; |
287 | 0 | if (decoder_inst->input_type == NULL) { |
288 | 0 | ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION, |
289 | 0 | "the mandatory 'input' property is missing " |
290 | 0 | "for decoder %s (properties: %s)", |
291 | 0 | OSSL_DECODER_get0_name(decoder), |
292 | 0 | OSSL_DECODER_get0_properties(decoder)); |
293 | 0 | goto err; |
294 | 0 | } |
295 | | |
296 | | /* The "structure" property is optional */ |
297 | 0 | prop = ossl_property_find_property(props, libctx, "structure"); |
298 | 0 | if (prop != NULL) { |
299 | 0 | decoder_inst->input_structure |
300 | 0 | = ossl_property_get_string_value(libctx, prop); |
301 | 0 | } |
302 | |
|
303 | 0 | if (!OSSL_DECODER_up_ref(decoder)) { |
304 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR); |
305 | 0 | goto err; |
306 | 0 | } |
307 | 0 | decoder_inst->decoder = decoder; |
308 | 0 | decoder_inst->decoderctx = decoderctx; |
309 | 0 | return decoder_inst; |
310 | 0 | err: |
311 | 0 | ossl_decoder_instance_free(decoder_inst); |
312 | 0 | return NULL; |
313 | 0 | } |
314 | | |
315 | | void ossl_decoder_instance_free(OSSL_DECODER_INSTANCE *decoder_inst) |
316 | 0 | { |
317 | 0 | if (decoder_inst != NULL) { |
318 | 0 | if (decoder_inst->decoder != NULL) |
319 | 0 | decoder_inst->decoder->freectx(decoder_inst->decoderctx); |
320 | 0 | decoder_inst->decoderctx = NULL; |
321 | 0 | OSSL_DECODER_free(decoder_inst->decoder); |
322 | 0 | decoder_inst->decoder = NULL; |
323 | 0 | OPENSSL_free(decoder_inst); |
324 | 0 | } |
325 | 0 | } |
326 | | |
327 | | OSSL_DECODER_INSTANCE *ossl_decoder_instance_dup(const OSSL_DECODER_INSTANCE *src) |
328 | 0 | { |
329 | 0 | OSSL_DECODER_INSTANCE *dest; |
330 | 0 | const OSSL_PROVIDER *prov; |
331 | 0 | void *provctx; |
332 | |
|
333 | 0 | if ((dest = OPENSSL_zalloc(sizeof(*dest))) == NULL) |
334 | 0 | return NULL; |
335 | | |
336 | 0 | *dest = *src; |
337 | 0 | if (!OSSL_DECODER_up_ref(dest->decoder)) { |
338 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR); |
339 | 0 | goto err; |
340 | 0 | } |
341 | 0 | prov = OSSL_DECODER_get0_provider(dest->decoder); |
342 | 0 | provctx = OSSL_PROVIDER_get0_provider_ctx(prov); |
343 | |
|
344 | 0 | dest->decoderctx = dest->decoder->newctx(provctx); |
345 | 0 | if (dest->decoderctx == NULL) { |
346 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR); |
347 | 0 | OSSL_DECODER_free(dest->decoder); |
348 | 0 | goto err; |
349 | 0 | } |
350 | | |
351 | 0 | return dest; |
352 | | |
353 | 0 | err: |
354 | 0 | OPENSSL_free(dest); |
355 | 0 | return NULL; |
356 | 0 | } |
357 | | |
358 | | void ossl_decoder_ctx_set_harderr(OSSL_DECODER_CTX *ctx) |
359 | 0 | { |
360 | 0 | ctx->harderr = 1; |
361 | 0 | } |
362 | | |
363 | | int ossl_decoder_ctx_get_harderr(const OSSL_DECODER_CTX *ctx) |
364 | 0 | { |
365 | 0 | return ctx->harderr; |
366 | 0 | } |
367 | | |
368 | | int ossl_decoder_ctx_add_decoder_inst(OSSL_DECODER_CTX *ctx, |
369 | | OSSL_DECODER_INSTANCE *di) |
370 | 0 | { |
371 | 0 | int ok; |
372 | |
|
373 | 0 | if (ctx->decoder_insts == NULL |
374 | 0 | && (ctx->decoder_insts = |
375 | 0 | sk_OSSL_DECODER_INSTANCE_new_null()) == NULL) { |
376 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB); |
377 | 0 | return 0; |
378 | 0 | } |
379 | | |
380 | 0 | ok = (sk_OSSL_DECODER_INSTANCE_push(ctx->decoder_insts, di) > 0); |
381 | 0 | if (ok) { |
382 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
383 | 0 | BIO_printf(trc_out, |
384 | 0 | "(ctx %p) Added decoder instance %p for decoder %p\n" |
385 | 0 | " %s with %s\n", |
386 | 0 | (void *)ctx, (void *)di, (void *)di->decoder, |
387 | 0 | OSSL_DECODER_get0_name(di->decoder), |
388 | 0 | OSSL_DECODER_get0_properties(di->decoder)); |
389 | 0 | } OSSL_TRACE_END(DECODER); |
390 | 0 | } |
391 | 0 | return ok; |
392 | 0 | } |
393 | | |
394 | | int OSSL_DECODER_CTX_add_decoder(OSSL_DECODER_CTX *ctx, OSSL_DECODER *decoder) |
395 | 0 | { |
396 | 0 | OSSL_DECODER_INSTANCE *decoder_inst = NULL; |
397 | 0 | const OSSL_PROVIDER *prov = NULL; |
398 | 0 | void *decoderctx = NULL; |
399 | 0 | void *provctx = NULL; |
400 | |
|
401 | 0 | if (ctx == NULL || decoder == NULL) { |
402 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
403 | 0 | return 0; |
404 | 0 | } |
405 | | |
406 | 0 | if (ctx->finalized != 0) { |
407 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
408 | 0 | return 0; |
409 | 0 | } |
410 | | |
411 | 0 | prov = OSSL_DECODER_get0_provider(decoder); |
412 | 0 | provctx = OSSL_PROVIDER_get0_provider_ctx(prov); |
413 | |
|
414 | 0 | if ((decoderctx = decoder->newctx(provctx)) == NULL |
415 | 0 | || (decoder_inst = |
416 | 0 | ossl_decoder_instance_new(decoder, decoderctx)) == NULL) |
417 | 0 | goto err; |
418 | | /* Avoid double free of decoderctx on further errors */ |
419 | 0 | decoderctx = NULL; |
420 | |
|
421 | 0 | if (!ossl_decoder_ctx_add_decoder_inst(ctx, decoder_inst)) |
422 | 0 | goto err; |
423 | | |
424 | 0 | return 1; |
425 | 0 | err: |
426 | 0 | ossl_decoder_instance_free(decoder_inst); |
427 | 0 | if (decoderctx != NULL) |
428 | 0 | decoder->freectx(decoderctx); |
429 | 0 | return 0; |
430 | 0 | } |
431 | | |
432 | | struct collect_extra_decoder_data_st { |
433 | | OSSL_DECODER_CTX *ctx; |
434 | | const char *output_type; |
435 | | int output_type_id; |
436 | | |
437 | | /* |
438 | | * 0 to check that the decoder's input type is the same as the decoder name |
439 | | * 1 to check that the decoder's input type differs from the decoder name |
440 | | */ |
441 | | enum { IS_SAME = 0, IS_DIFFERENT = 1 } type_check; |
442 | | int w_prev_start, w_prev_end; /* "previous" decoders */ |
443 | | int w_new_start, w_new_end; /* "new" decoders */ |
444 | | }; |
445 | | |
446 | | DEFINE_STACK_OF(OSSL_DECODER) |
447 | | |
448 | | static void collect_all_decoders(OSSL_DECODER *decoder, void *arg) |
449 | 0 | { |
450 | 0 | STACK_OF(OSSL_DECODER) *skdecoders = arg; |
451 | |
|
452 | 0 | if (OSSL_DECODER_up_ref(decoder) |
453 | 0 | && !sk_OSSL_DECODER_push(skdecoders, decoder)) |
454 | 0 | OSSL_DECODER_free(decoder); |
455 | 0 | } |
456 | | |
457 | | static void collect_extra_decoder(OSSL_DECODER *decoder, void *arg) |
458 | 0 | { |
459 | 0 | struct collect_extra_decoder_data_st *data = arg; |
460 | 0 | int j; |
461 | 0 | const OSSL_PROVIDER *prov = OSSL_DECODER_get0_provider(decoder); |
462 | 0 | void *provctx = OSSL_PROVIDER_get0_provider_ctx(prov); |
463 | |
|
464 | 0 | if (ossl_decoder_fast_is_a(decoder, data->output_type, &data->output_type_id)) { |
465 | 0 | void *decoderctx = NULL; |
466 | 0 | OSSL_DECODER_INSTANCE *di = NULL; |
467 | |
|
468 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
469 | 0 | BIO_printf(trc_out, |
470 | 0 | "(ctx %p) [%d] Checking out decoder %p:\n" |
471 | 0 | " %s with %s\n", |
472 | 0 | (void *)data->ctx, data->type_check, (void *)decoder, |
473 | 0 | OSSL_DECODER_get0_name(decoder), |
474 | 0 | OSSL_DECODER_get0_properties(decoder)); |
475 | 0 | } OSSL_TRACE_END(DECODER); |
476 | | |
477 | | /* |
478 | | * Check that we don't already have this decoder in our stack, |
479 | | * starting with the previous windows but also looking at what |
480 | | * we have added in the current window. |
481 | | */ |
482 | 0 | for (j = data->w_prev_start; j < data->w_new_end; j++) { |
483 | 0 | OSSL_DECODER_INSTANCE *check_inst = |
484 | 0 | sk_OSSL_DECODER_INSTANCE_value(data->ctx->decoder_insts, j); |
485 | |
|
486 | 0 | if (decoder->base.algodef == check_inst->decoder->base.algodef) { |
487 | | /* We found it, so don't do anything more */ |
488 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
489 | 0 | BIO_printf(trc_out, |
490 | 0 | " REJECTED: already exists in the chain\n"); |
491 | 0 | } OSSL_TRACE_END(DECODER); |
492 | 0 | return; |
493 | 0 | } |
494 | 0 | } |
495 | | |
496 | 0 | if ((decoderctx = decoder->newctx(provctx)) == NULL) |
497 | 0 | return; |
498 | | |
499 | 0 | if (decoder->set_ctx_params != NULL |
500 | 0 | && data->ctx->input_structure != NULL) { |
501 | 0 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
502 | 0 | const char *str = data->ctx->input_structure; |
503 | |
|
504 | 0 | params[0] = |
505 | 0 | OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, |
506 | 0 | (char *)str, 0); |
507 | 0 | if (!decoder->set_ctx_params(decoderctx, params)) { |
508 | 0 | decoder->freectx(decoderctx); |
509 | 0 | return; |
510 | 0 | } |
511 | 0 | } |
512 | | |
513 | 0 | if ((di = ossl_decoder_instance_new(decoder, decoderctx)) == NULL) { |
514 | 0 | decoder->freectx(decoderctx); |
515 | 0 | return; |
516 | 0 | } |
517 | | |
518 | 0 | switch (data->type_check) { |
519 | 0 | case IS_SAME: |
520 | | /* If it differs, this is not a decoder to add for now. */ |
521 | 0 | if (!ossl_decoder_fast_is_a(decoder, |
522 | 0 | OSSL_DECODER_INSTANCE_get_input_type(di), |
523 | 0 | &di->input_type_id)) { |
524 | 0 | ossl_decoder_instance_free(di); |
525 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
526 | 0 | BIO_printf(trc_out, |
527 | 0 | " REJECTED: input type doesn't match output type\n"); |
528 | 0 | } OSSL_TRACE_END(DECODER); |
529 | 0 | return; |
530 | 0 | } |
531 | 0 | break; |
532 | 0 | case IS_DIFFERENT: |
533 | | /* If it's the same, this is not a decoder to add for now. */ |
534 | 0 | if (ossl_decoder_fast_is_a(decoder, |
535 | 0 | OSSL_DECODER_INSTANCE_get_input_type(di), |
536 | 0 | &di->input_type_id)) { |
537 | 0 | ossl_decoder_instance_free(di); |
538 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
539 | 0 | BIO_printf(trc_out, |
540 | 0 | " REJECTED: input type matches output type\n"); |
541 | 0 | } OSSL_TRACE_END(DECODER); |
542 | 0 | return; |
543 | 0 | } |
544 | 0 | break; |
545 | 0 | } |
546 | | |
547 | | /* |
548 | | * Apart from keeping w_new_end up to date, We don't care about |
549 | | * errors here. If it doesn't collect, then it doesn't... |
550 | | */ |
551 | 0 | if (!ossl_decoder_ctx_add_decoder_inst(data->ctx, di)) { |
552 | 0 | ossl_decoder_instance_free(di); |
553 | 0 | return; |
554 | 0 | } |
555 | | |
556 | 0 | data->w_new_end++; |
557 | 0 | } |
558 | 0 | } |
559 | | |
560 | | static int decoder_sk_cmp(const OSSL_DECODER_INSTANCE *const *a, |
561 | | const OSSL_DECODER_INSTANCE *const *b) |
562 | 0 | { |
563 | 0 | if ((*a)->score == (*b)->score) |
564 | 0 | return (*a)->order - (*b)->order; |
565 | 0 | return (*a)->score - (*b)->score; |
566 | 0 | } |
567 | | |
568 | | int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx, |
569 | | OSSL_LIB_CTX *libctx, const char *propq) |
570 | 0 | { |
571 | | /* |
572 | | * This function goes through existing decoder methods in |
573 | | * |ctx->decoder_insts|, and tries to fetch new decoders that produce |
574 | | * what the existing ones want as input, and push those newly fetched |
575 | | * decoders on top of the same stack. |
576 | | * Then it does the same again, but looping over the newly fetched |
577 | | * decoders, until there are no more decoders to be fetched, or |
578 | | * when we have done this 10 times. |
579 | | * |
580 | | * we do this with sliding windows on the stack by keeping track of indexes |
581 | | * and of the end. |
582 | | * |
583 | | * +----------------+ |
584 | | * | DER to RSA | <--- w_prev_start |
585 | | * +----------------+ |
586 | | * | DER to DSA | |
587 | | * +----------------+ |
588 | | * | DER to DH | |
589 | | * +----------------+ |
590 | | * | PEM to DER | <--- w_prev_end, w_new_start |
591 | | * +----------------+ |
592 | | * <--- w_new_end |
593 | | */ |
594 | 0 | struct collect_extra_decoder_data_st data; |
595 | 0 | size_t depth = 0; /* Counts the number of iterations */ |
596 | 0 | size_t count; /* Calculates how many were added in each iteration */ |
597 | 0 | int numdecoders; |
598 | 0 | STACK_OF(OSSL_DECODER) *skdecoders; |
599 | |
|
600 | 0 | if (ctx == NULL) { |
601 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
602 | 0 | return 0; |
603 | 0 | } |
604 | | |
605 | 0 | if (ctx->finalized != 0) { |
606 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
607 | 0 | return 0; |
608 | 0 | } |
609 | | |
610 | | /* |
611 | | * If there is no stack of OSSL_DECODER_INSTANCE, we have nothing |
612 | | * more to add. That's fine. |
613 | | */ |
614 | 0 | if (ctx->decoder_insts == NULL) |
615 | 0 | return 1; |
616 | | |
617 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
618 | 0 | BIO_printf(trc_out, "(ctx %p) Looking for extra decoders\n", |
619 | 0 | (void *)ctx); |
620 | 0 | } OSSL_TRACE_END(DECODER); |
621 | | |
622 | |
|
623 | 0 | skdecoders = sk_OSSL_DECODER_new_null(); |
624 | 0 | if (skdecoders == NULL) { |
625 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB); |
626 | 0 | return 0; |
627 | 0 | } |
628 | 0 | OSSL_DECODER_do_all_provided(libctx, collect_all_decoders, skdecoders); |
629 | 0 | numdecoders = sk_OSSL_DECODER_num(skdecoders); |
630 | | |
631 | | /* |
632 | | * If there are provided or default properties, sort the initial decoder list |
633 | | * by property matching score so that the highest scored provider is selected |
634 | | * first. |
635 | | */ |
636 | 0 | if (propq != NULL || ossl_ctx_global_properties(libctx, 0) != NULL) { |
637 | 0 | int num_decoder_insts = sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts); |
638 | 0 | int i; |
639 | 0 | OSSL_DECODER_INSTANCE *di; |
640 | 0 | sk_OSSL_DECODER_INSTANCE_compfunc old_cmp = |
641 | 0 | sk_OSSL_DECODER_INSTANCE_set_cmp_func(ctx->decoder_insts, decoder_sk_cmp); |
642 | |
|
643 | 0 | for (i = 0; i < num_decoder_insts; i++) { |
644 | 0 | di = sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i); |
645 | 0 | di->order = i; |
646 | 0 | } |
647 | 0 | sk_OSSL_DECODER_INSTANCE_sort(ctx->decoder_insts); |
648 | 0 | sk_OSSL_DECODER_INSTANCE_set_cmp_func(ctx->decoder_insts, old_cmp); |
649 | 0 | } |
650 | |
|
651 | 0 | memset(&data, 0, sizeof(data)); |
652 | 0 | data.ctx = ctx; |
653 | 0 | data.w_prev_start = 0; |
654 | 0 | data.w_prev_end = sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts); |
655 | 0 | do { |
656 | 0 | int i, j; |
657 | |
|
658 | 0 | data.w_new_start = data.w_new_end = data.w_prev_end; |
659 | | |
660 | | /* |
661 | | * Two iterations: |
662 | | * 0. All decoders that have the same name as their input type. |
663 | | * This allows for decoders that unwrap some data in a specific |
664 | | * encoding, and pass the result on with the same encoding. |
665 | | * 1. All decoders that a different name than their input type. |
666 | | */ |
667 | 0 | for (data.type_check = IS_SAME; |
668 | 0 | data.type_check <= IS_DIFFERENT; |
669 | 0 | data.type_check++) { |
670 | 0 | for (i = data.w_prev_start; i < data.w_prev_end; i++) { |
671 | 0 | OSSL_DECODER_INSTANCE *decoder_inst = |
672 | 0 | sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i); |
673 | |
|
674 | 0 | data.output_type |
675 | 0 | = OSSL_DECODER_INSTANCE_get_input_type(decoder_inst); |
676 | |
|
677 | 0 | data.output_type_id = 0; |
678 | |
|
679 | 0 | for (j = 0; j < numdecoders; j++) |
680 | 0 | collect_extra_decoder(sk_OSSL_DECODER_value(skdecoders, j), |
681 | 0 | &data); |
682 | 0 | } |
683 | 0 | } |
684 | | /* How many were added in this iteration */ |
685 | 0 | count = data.w_new_end - data.w_new_start; |
686 | | |
687 | | /* Slide the "previous decoder" windows */ |
688 | 0 | data.w_prev_start = data.w_new_start; |
689 | 0 | data.w_prev_end = data.w_new_end; |
690 | |
|
691 | 0 | depth++; |
692 | 0 | } while (count != 0 && depth <= 10); |
693 | |
|
694 | 0 | sk_OSSL_DECODER_pop_free(skdecoders, OSSL_DECODER_free); |
695 | 0 | return 1; |
696 | 0 | } |
697 | | |
698 | | int OSSL_DECODER_CTX_get_num_decoders(OSSL_DECODER_CTX *ctx) |
699 | 0 | { |
700 | 0 | if (ctx == NULL || ctx->decoder_insts == NULL) |
701 | 0 | return 0; |
702 | 0 | return sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts); |
703 | 0 | } |
704 | | |
705 | | int OSSL_DECODER_CTX_set_construct(OSSL_DECODER_CTX *ctx, |
706 | | OSSL_DECODER_CONSTRUCT *construct) |
707 | 0 | { |
708 | 0 | if (ctx == NULL) { |
709 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
710 | 0 | return 0; |
711 | 0 | } |
712 | | |
713 | 0 | if (ctx->finalized != 0) { |
714 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
715 | 0 | return 0; |
716 | 0 | } |
717 | | |
718 | 0 | ctx->construct = construct; |
719 | 0 | return 1; |
720 | 0 | } |
721 | | |
722 | | int OSSL_DECODER_CTX_set_construct_data(OSSL_DECODER_CTX *ctx, |
723 | | void *construct_data) |
724 | 0 | { |
725 | 0 | if (ctx == NULL) { |
726 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
727 | 0 | return 0; |
728 | 0 | } |
729 | | |
730 | 0 | if (ctx->finalized != 0) { |
731 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
732 | 0 | return 0; |
733 | 0 | } |
734 | | |
735 | 0 | ctx->construct_data = construct_data; |
736 | 0 | return 1; |
737 | 0 | } |
738 | | |
739 | | int OSSL_DECODER_CTX_set_cleanup(OSSL_DECODER_CTX *ctx, |
740 | | OSSL_DECODER_CLEANUP *cleanup) |
741 | 0 | { |
742 | 0 | if (ctx == NULL) { |
743 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
744 | 0 | return 0; |
745 | 0 | } |
746 | | |
747 | 0 | if (ctx->finalized != 0) { |
748 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
749 | 0 | return 0; |
750 | 0 | } |
751 | | |
752 | 0 | ctx->cleanup = cleanup; |
753 | 0 | return 1; |
754 | 0 | } |
755 | | |
756 | | int OSSL_DECODER_CTX_set_finalized(OSSL_DECODER_CTX *ctx) |
757 | 0 | { |
758 | 0 | if (ctx == NULL) { |
759 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
760 | 0 | return 0; |
761 | 0 | } |
762 | | |
763 | 0 | ctx->finalized = 1; |
764 | 0 | return 1; |
765 | 0 | } |
766 | | |
767 | | int OSSL_DECODER_CTX_get_finalized(OSSL_DECODER_CTX *ctx) |
768 | 0 | { |
769 | 0 | if (ctx == NULL) |
770 | 0 | return 0; |
771 | | |
772 | 0 | return ctx->finalized; |
773 | 0 | } |
774 | | |
775 | | OSSL_DECODER_CONSTRUCT * |
776 | | OSSL_DECODER_CTX_get_construct(OSSL_DECODER_CTX *ctx) |
777 | 0 | { |
778 | 0 | if (ctx == NULL) |
779 | 0 | return NULL; |
780 | 0 | return ctx->construct; |
781 | 0 | } |
782 | | |
783 | | void *OSSL_DECODER_CTX_get_construct_data(OSSL_DECODER_CTX *ctx) |
784 | 0 | { |
785 | 0 | if (ctx == NULL) |
786 | 0 | return NULL; |
787 | 0 | return ctx->construct_data; |
788 | 0 | } |
789 | | |
790 | | OSSL_DECODER_CLEANUP * |
791 | | OSSL_DECODER_CTX_get_cleanup(OSSL_DECODER_CTX *ctx) |
792 | 0 | { |
793 | 0 | if (ctx == NULL) |
794 | 0 | return NULL; |
795 | 0 | return ctx->cleanup; |
796 | 0 | } |
797 | | |
798 | | int OSSL_DECODER_export(OSSL_DECODER_INSTANCE *decoder_inst, |
799 | | void *reference, size_t reference_sz, |
800 | | OSSL_CALLBACK *export_cb, void *export_cbarg) |
801 | 0 | { |
802 | 0 | OSSL_DECODER *decoder = NULL; |
803 | 0 | void *decoderctx = NULL; |
804 | |
|
805 | 0 | if (decoder_inst == NULL || reference == NULL |
806 | 0 | || export_cb == NULL || export_cbarg == NULL) { |
807 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
808 | 0 | return 0; |
809 | 0 | } |
810 | | |
811 | 0 | decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst); |
812 | 0 | decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst); |
813 | 0 | return decoder->export_object(decoderctx, reference, reference_sz, |
814 | 0 | export_cb, export_cbarg); |
815 | 0 | } |
816 | | |
817 | | OSSL_DECODER * |
818 | | OSSL_DECODER_INSTANCE_get_decoder(OSSL_DECODER_INSTANCE *decoder_inst) |
819 | 0 | { |
820 | 0 | if (decoder_inst == NULL) |
821 | 0 | return NULL; |
822 | 0 | return decoder_inst->decoder; |
823 | 0 | } |
824 | | |
825 | | void * |
826 | | OSSL_DECODER_INSTANCE_get_decoder_ctx(OSSL_DECODER_INSTANCE *decoder_inst) |
827 | 0 | { |
828 | 0 | if (decoder_inst == NULL) |
829 | 0 | return NULL; |
830 | 0 | return decoder_inst->decoderctx; |
831 | 0 | } |
832 | | |
833 | | const char * |
834 | | OSSL_DECODER_INSTANCE_get_input_type(OSSL_DECODER_INSTANCE *decoder_inst) |
835 | 0 | { |
836 | 0 | if (decoder_inst == NULL) |
837 | 0 | return NULL; |
838 | 0 | return decoder_inst->input_type; |
839 | 0 | } |
840 | | |
841 | | const char * |
842 | | OSSL_DECODER_INSTANCE_get_input_structure(OSSL_DECODER_INSTANCE *decoder_inst, |
843 | | int *was_set) |
844 | 0 | { |
845 | 0 | if (decoder_inst == NULL) |
846 | 0 | return NULL; |
847 | 0 | *was_set = decoder_inst->flag_input_structure_was_set; |
848 | 0 | return decoder_inst->input_structure; |
849 | 0 | } |
850 | | |
851 | | static int decoder_process(const OSSL_PARAM params[], void *arg) |
852 | 0 | { |
853 | 0 | struct decoder_process_data_st *data = arg; |
854 | 0 | OSSL_DECODER_CTX *ctx = data->ctx; |
855 | 0 | OSSL_DECODER_INSTANCE *decoder_inst = NULL; |
856 | 0 | OSSL_DECODER *decoder = NULL; |
857 | 0 | OSSL_CORE_BIO *cbio = NULL; |
858 | 0 | BIO *bio = data->bio; |
859 | 0 | long loc; |
860 | 0 | int i; |
861 | 0 | int ok = 0; |
862 | | /* For recursions */ |
863 | 0 | struct decoder_process_data_st new_data; |
864 | 0 | const char *data_type = NULL; |
865 | 0 | const char *data_structure = NULL; |
866 | | /* Saved to restore on return, mutated in PEM->DER transition. */ |
867 | 0 | const char *start_input_type = ctx->start_input_type; |
868 | | |
869 | | /* |
870 | | * This is an indicator up the call stack that something was indeed |
871 | | * decoded, leading to a recursive call of this function. |
872 | | */ |
873 | 0 | data->flag_next_level_called = 1; |
874 | |
|
875 | 0 | memset(&new_data, 0, sizeof(new_data)); |
876 | 0 | new_data.ctx = data->ctx; |
877 | 0 | new_data.recursion = data->recursion + 1; |
878 | |
|
879 | 0 | #define LEVEL_STR ">>>>>>>>>>>>>>>>" |
880 | 0 | #define LEVEL ((size_t)new_data.recursion < sizeof(LEVEL_STR) \ |
881 | 0 | ? &LEVEL_STR[sizeof(LEVEL_STR) - new_data.recursion - 1] \ |
882 | 0 | : LEVEL_STR "...") |
883 | |
|
884 | 0 | if (params == NULL) { |
885 | | /* First iteration, where we prepare for what is to come */ |
886 | |
|
887 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
888 | 0 | BIO_printf(trc_out, |
889 | 0 | "(ctx %p) starting to walk the decoder chain\n", |
890 | 0 | (void *)new_data.ctx); |
891 | 0 | } OSSL_TRACE_END(DECODER); |
892 | |
|
893 | 0 | data->current_decoder_inst_index = |
894 | 0 | OSSL_DECODER_CTX_get_num_decoders(ctx); |
895 | |
|
896 | 0 | bio = data->bio; |
897 | 0 | } else { |
898 | 0 | const OSSL_PARAM *p; |
899 | 0 | const char *trace_data_structure; |
900 | |
|
901 | 0 | decoder_inst = |
902 | 0 | sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, |
903 | 0 | data->current_decoder_inst_index); |
904 | 0 | decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst); |
905 | |
|
906 | 0 | data->flag_construct_called = 0; |
907 | 0 | if (ctx->construct != NULL) { |
908 | 0 | int rv; |
909 | |
|
910 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
911 | 0 | BIO_printf(trc_out, |
912 | 0 | "(ctx %p) %s Running constructor\n", |
913 | 0 | (void *)new_data.ctx, LEVEL); |
914 | 0 | } OSSL_TRACE_END(DECODER); |
915 | |
|
916 | 0 | rv = ctx->construct(decoder_inst, params, ctx->construct_data); |
917 | |
|
918 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
919 | 0 | BIO_printf(trc_out, |
920 | 0 | "(ctx %p) %s Running constructor => %d\n", |
921 | 0 | (void *)new_data.ctx, LEVEL, rv); |
922 | 0 | } OSSL_TRACE_END(DECODER); |
923 | |
|
924 | 0 | ok = (rv > 0); |
925 | 0 | if (ok) { |
926 | 0 | data->flag_construct_called = 1; |
927 | 0 | goto end; |
928 | 0 | } |
929 | 0 | } |
930 | | |
931 | | /* The constructor didn't return success */ |
932 | | |
933 | | /* |
934 | | * so we try to use the object we got and feed it to any next |
935 | | * decoder that will take it. Object references are not |
936 | | * allowed for this. |
937 | | * If this data isn't present, decoding has failed. |
938 | | */ |
939 | | |
940 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA); |
941 | 0 | if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING) |
942 | 0 | goto end; |
943 | 0 | new_data.bio = BIO_new_mem_buf(p->data, (int)p->data_size); |
944 | 0 | if (new_data.bio == NULL) |
945 | 0 | goto end; |
946 | 0 | bio = new_data.bio; |
947 | | |
948 | | /* Get the data type if there is one */ |
949 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE); |
950 | 0 | if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &data_type)) |
951 | 0 | goto end; |
952 | | |
953 | | /* Get the data structure if there is one */ |
954 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_STRUCTURE); |
955 | 0 | if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &data_structure)) |
956 | 0 | goto end; |
957 | | |
958 | | /* Get the new input type if there is one */ |
959 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_INPUT_TYPE); |
960 | 0 | if (p != NULL) { |
961 | 0 | if (!OSSL_PARAM_get_utf8_string_ptr(p, &ctx->start_input_type)) |
962 | 0 | goto end; |
963 | | /* |
964 | | * When switching PKCS8 from PEM to DER we decrypt the data if needed |
965 | | * and then determine the algorithm OID. Likewise, with SPKI, only |
966 | | * this time sans decryption. |
967 | | */ |
968 | 0 | if (ctx->input_structure != NULL |
969 | 0 | && (OPENSSL_strcasecmp(ctx->input_structure, "SubjectPublicKeyInfo") == 0 |
970 | 0 | || OPENSSL_strcasecmp(data_structure, "PrivateKeyInfo") == 0 |
971 | 0 | || OPENSSL_strcasecmp(ctx->input_structure, "PrivateKeyInfo") == 0)) |
972 | 0 | data->flag_input_structure_checked = 1; |
973 | 0 | } |
974 | | |
975 | | /* |
976 | | * If the data structure is "type-specific" and the data type is |
977 | | * given, we drop the data structure. The reasoning is that the |
978 | | * data type is already enough to find the applicable next decoder, |
979 | | * so an additional "type-specific" data structure is extraneous. |
980 | | * |
981 | | * Furthermore, if the OSSL_DECODER caller asked for a type specific |
982 | | * structure under another name, such as "DH", we get a mismatch |
983 | | * if the data structure we just received is "type-specific". |
984 | | * There's only so much you can do without infusing this code with |
985 | | * too special knowledge. |
986 | | */ |
987 | 0 | trace_data_structure = data_structure; |
988 | 0 | if (data_type != NULL && data_structure != NULL |
989 | 0 | && OPENSSL_strcasecmp(data_structure, "type-specific") == 0) |
990 | 0 | data_structure = NULL; |
991 | |
|
992 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
993 | 0 | BIO_printf(trc_out, |
994 | 0 | "(ctx %p) %s incoming from previous decoder (%p):\n" |
995 | 0 | " data type: %s, data structure: %s%s\n", |
996 | 0 | (void *)new_data.ctx, LEVEL, (void *)decoder, |
997 | 0 | data_type, trace_data_structure, |
998 | 0 | (trace_data_structure == data_structure |
999 | 0 | ? "" : " (dropped)")); |
1000 | 0 | } OSSL_TRACE_END(DECODER); |
1001 | 0 | } |
1002 | | |
1003 | | /* |
1004 | | * If we have no more decoders to look through at this point, |
1005 | | * we failed |
1006 | | */ |
1007 | 0 | if (data->current_decoder_inst_index == 0) |
1008 | 0 | goto end; |
1009 | | |
1010 | 0 | if ((loc = BIO_tell(bio)) < 0) { |
1011 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB); |
1012 | 0 | goto end; |
1013 | 0 | } |
1014 | | |
1015 | 0 | if ((cbio = ossl_core_bio_new_from_bio(bio)) == NULL) { |
1016 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB); |
1017 | 0 | goto end; |
1018 | 0 | } |
1019 | | |
1020 | 0 | for (i = data->current_decoder_inst_index; i-- > 0;) { |
1021 | 0 | OSSL_DECODER_INSTANCE *new_decoder_inst = |
1022 | 0 | sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i); |
1023 | 0 | OSSL_DECODER *new_decoder = |
1024 | 0 | OSSL_DECODER_INSTANCE_get_decoder(new_decoder_inst); |
1025 | 0 | const char *new_decoder_name = NULL; |
1026 | 0 | void *new_decoderctx = |
1027 | 0 | OSSL_DECODER_INSTANCE_get_decoder_ctx(new_decoder_inst); |
1028 | 0 | const char *new_input_type = |
1029 | 0 | OSSL_DECODER_INSTANCE_get_input_type(new_decoder_inst); |
1030 | 0 | int n_i_s_was_set = 0; /* We don't care here */ |
1031 | 0 | const char *new_input_structure = |
1032 | 0 | OSSL_DECODER_INSTANCE_get_input_structure(new_decoder_inst, |
1033 | 0 | &n_i_s_was_set); |
1034 | |
|
1035 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
1036 | 0 | new_decoder_name = OSSL_DECODER_get0_name(new_decoder); |
1037 | 0 | BIO_printf(trc_out, |
1038 | 0 | "(ctx %p) %s [%u] Considering decoder instance %p (decoder %p):\n" |
1039 | 0 | " %s with %s\n", |
1040 | 0 | (void *)new_data.ctx, LEVEL, (unsigned int)i, |
1041 | 0 | (void *)new_decoder_inst, (void *)new_decoder, |
1042 | 0 | new_decoder_name, |
1043 | 0 | OSSL_DECODER_get0_properties(new_decoder)); |
1044 | 0 | } OSSL_TRACE_END(DECODER); |
1045 | | |
1046 | | /* |
1047 | | * If |decoder| is NULL, it means we've just started, and the caller |
1048 | | * may have specified what it expects the initial input to be. If |
1049 | | * that's the case, we do this extra check. |
1050 | | */ |
1051 | 0 | if (decoder == NULL && ctx->start_input_type != NULL |
1052 | 0 | && OPENSSL_strcasecmp(ctx->start_input_type, new_input_type) != 0) { |
1053 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
1054 | 0 | BIO_printf(trc_out, |
1055 | 0 | "(ctx %p) %s [%u] the start input type '%s' doesn't match the input type of the considered decoder, skipping...\n", |
1056 | 0 | (void *)new_data.ctx, LEVEL, (unsigned int)i, |
1057 | 0 | ctx->start_input_type); |
1058 | 0 | } OSSL_TRACE_END(DECODER); |
1059 | 0 | continue; |
1060 | 0 | } |
1061 | | |
1062 | | /* |
1063 | | * If we have a previous decoder, we check that the input type |
1064 | | * of the next to be used matches the type of this previous one. |
1065 | | * |new_input_type| holds the value of the "input-type" parameter |
1066 | | * for the decoder we're currently considering. |
1067 | | */ |
1068 | 0 | if (decoder != NULL && !ossl_decoder_fast_is_a(decoder, new_input_type, |
1069 | 0 | &new_decoder_inst->input_type_id)) { |
1070 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
1071 | 0 | BIO_printf(trc_out, |
1072 | 0 | "(ctx %p) %s [%u] the input type doesn't match the name of the previous decoder (%p), skipping...\n", |
1073 | 0 | (void *)new_data.ctx, LEVEL, (unsigned int)i, |
1074 | 0 | (void *)decoder); |
1075 | 0 | } OSSL_TRACE_END(DECODER); |
1076 | 0 | continue; |
1077 | 0 | } |
1078 | | |
1079 | | /* |
1080 | | * If the previous decoder gave us a data type, we check to see |
1081 | | * if that matches the decoder we're currently considering. |
1082 | | */ |
1083 | 0 | if (data_type != NULL && !OSSL_DECODER_is_a(new_decoder, data_type)) { |
1084 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
1085 | 0 | BIO_printf(trc_out, |
1086 | 0 | "(ctx %p) %s [%u] the previous decoder's data type doesn't match the name of the considered decoder, skipping...\n", |
1087 | 0 | (void *)new_data.ctx, LEVEL, (unsigned int)i); |
1088 | 0 | } OSSL_TRACE_END(DECODER); |
1089 | 0 | continue; |
1090 | 0 | } |
1091 | | |
1092 | | /* |
1093 | | * If the previous decoder gave us a data structure name, we check |
1094 | | * to see that it matches the input data structure of the decoder |
1095 | | * we're currently considering. |
1096 | | */ |
1097 | 0 | if (data_structure != NULL |
1098 | 0 | && (new_input_structure == NULL |
1099 | 0 | || OPENSSL_strcasecmp(data_structure, |
1100 | 0 | new_input_structure) != 0)) { |
1101 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
1102 | 0 | BIO_printf(trc_out, |
1103 | 0 | "(ctx %p) %s [%u] the previous decoder's data structure doesn't match the input structure of the considered decoder, skipping...\n", |
1104 | 0 | (void *)new_data.ctx, LEVEL, (unsigned int)i); |
1105 | 0 | } OSSL_TRACE_END(DECODER); |
1106 | 0 | continue; |
1107 | 0 | } |
1108 | | |
1109 | | /* |
1110 | | * If the decoder we're currently considering specifies a structure, |
1111 | | * and this check hasn't already been done earlier in this chain of |
1112 | | * decoder_process() calls, check that it matches the user provided |
1113 | | * input structure, if one is given. |
1114 | | */ |
1115 | 0 | if (!data->flag_input_structure_checked |
1116 | 0 | && ctx->input_structure != NULL |
1117 | 0 | && new_input_structure != NULL) { |
1118 | 0 | data->flag_input_structure_checked = 1; |
1119 | 0 | if (OPENSSL_strcasecmp(new_input_structure, |
1120 | 0 | ctx->input_structure) != 0) { |
1121 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
1122 | 0 | BIO_printf(trc_out, |
1123 | 0 | "(ctx %p) %s [%u] the previous decoder's data structure doesn't match the input structure given by the user, skipping...\n", |
1124 | 0 | (void *)new_data.ctx, LEVEL, (unsigned int)i); |
1125 | 0 | } OSSL_TRACE_END(DECODER); |
1126 | 0 | continue; |
1127 | 0 | } |
1128 | 0 | } |
1129 | | |
1130 | | /* |
1131 | | * Checking the return value of BIO_reset() or BIO_seek() is unsafe. |
1132 | | * Furthermore, BIO_reset() is unsafe to use if the source BIO happens |
1133 | | * to be a BIO_s_mem(), because the earlier BIO_tell() gives us zero |
1134 | | * no matter where we are in the underlying buffer we're reading from. |
1135 | | * |
1136 | | * So, we simply do a BIO_seek(), and use BIO_tell() that we're back |
1137 | | * at the same position. This is a best effort attempt, but BIO_seek() |
1138 | | * and BIO_tell() should come as a pair... |
1139 | | */ |
1140 | 0 | (void)BIO_seek(bio, loc); |
1141 | 0 | if (BIO_tell(bio) != loc) |
1142 | 0 | goto end; |
1143 | | |
1144 | | /* Recurse */ |
1145 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
1146 | 0 | BIO_printf(trc_out, |
1147 | 0 | "(ctx %p) %s [%u] Running decoder instance %s (%p)\n", |
1148 | 0 | (void *)new_data.ctx, LEVEL, (unsigned int)i, |
1149 | 0 | new_decoder_name, (void *)new_decoder_inst); |
1150 | 0 | } OSSL_TRACE_END(DECODER); |
1151 | | |
1152 | | /* |
1153 | | * We only care about errors reported from decoder implementations |
1154 | | * if it returns false (i.e. there was a fatal error). |
1155 | | */ |
1156 | 0 | ERR_set_mark(); |
1157 | |
|
1158 | 0 | new_data.current_decoder_inst_index = i; |
1159 | 0 | new_data.flag_input_structure_checked |
1160 | 0 | = data->flag_input_structure_checked; |
1161 | 0 | ok = new_decoder->decode(new_decoderctx, cbio, |
1162 | 0 | new_data.ctx->selection, |
1163 | 0 | decoder_process, &new_data, |
1164 | 0 | ossl_pw_passphrase_callback_dec, |
1165 | 0 | &new_data.ctx->pwdata); |
1166 | |
|
1167 | 0 | OSSL_TRACE_BEGIN(DECODER) { |
1168 | 0 | BIO_printf(trc_out, |
1169 | 0 | "(ctx %p) %s [%u] Running decoder instance %s (%p) => %d" |
1170 | 0 | " (recursed further: %s, construct called: %s)\n", |
1171 | 0 | (void *)new_data.ctx, LEVEL, (unsigned int)i, |
1172 | 0 | new_decoder_name, (void *)new_decoder_inst, ok, |
1173 | 0 | new_data.flag_next_level_called ? "yes" : "no", |
1174 | 0 | new_data.flag_construct_called ? "yes" : "no"); |
1175 | 0 | } OSSL_TRACE_END(DECODER); |
1176 | |
|
1177 | 0 | data->flag_construct_called = new_data.flag_construct_called; |
1178 | | |
1179 | | /* Break on error or if we tried to construct an object already */ |
1180 | 0 | if (!ok || data->flag_construct_called) { |
1181 | 0 | ERR_clear_last_mark(); |
1182 | 0 | break; |
1183 | 0 | } |
1184 | 0 | ERR_pop_to_mark(); |
1185 | | |
1186 | | /* |
1187 | | * Break if the decoder implementation that we called recursed, since |
1188 | | * that indicates that it successfully decoded something. |
1189 | | */ |
1190 | 0 | if (new_data.flag_next_level_called) |
1191 | 0 | break; |
1192 | 0 | } |
1193 | | |
1194 | 0 | end: |
1195 | 0 | ossl_core_bio_free(cbio); |
1196 | 0 | BIO_free(new_data.bio); |
1197 | 0 | ctx->start_input_type = start_input_type; |
1198 | 0 | return ok; |
1199 | 0 | } |