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