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