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