/src/openssl/crypto/encode_decode/encoder_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2022 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/encoder.h> |
13 | | #include <openssl/buffer.h> |
14 | | #include <openssl/params.h> |
15 | | #include <openssl/provider.h> |
16 | | #include <openssl/trace.h> |
17 | | #include "internal/bio.h" |
18 | | #include "internal/provider.h" |
19 | | #include "encoder_local.h" |
20 | | |
21 | | struct encoder_process_data_st { |
22 | | OSSL_ENCODER_CTX *ctx; |
23 | | |
24 | | /* Current BIO */ |
25 | | BIO *bio; |
26 | | |
27 | | /* Index of the current encoder instance to be processed */ |
28 | | int current_encoder_inst_index; |
29 | | |
30 | | /* Processing data passed down through recursion */ |
31 | | int level; /* Recursion level */ |
32 | | OSSL_ENCODER_INSTANCE *next_encoder_inst; |
33 | | int count_output_structure; |
34 | | |
35 | | /* Processing data passed up through recursion */ |
36 | | OSSL_ENCODER_INSTANCE *prev_encoder_inst; |
37 | | unsigned char *running_output; |
38 | | size_t running_output_length; |
39 | | /* Data type = the name of the first succeeding encoder implementation */ |
40 | | const char *data_type; |
41 | | }; |
42 | | |
43 | | static int encoder_process(struct encoder_process_data_st *data); |
44 | | |
45 | | int OSSL_ENCODER_to_bio(OSSL_ENCODER_CTX *ctx, BIO *out) |
46 | 329 | { |
47 | 329 | struct encoder_process_data_st data; |
48 | | |
49 | 329 | memset(&data, 0, sizeof(data)); |
50 | 329 | data.ctx = ctx; |
51 | 329 | data.bio = out; |
52 | 329 | data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx); |
53 | | |
54 | 329 | if (data.current_encoder_inst_index == 0) { |
55 | 0 | ERR_raise_data(ERR_LIB_OSSL_ENCODER, OSSL_ENCODER_R_ENCODER_NOT_FOUND, |
56 | 0 | "No encoders were found. For standard encoders you need " |
57 | 0 | "at least one of the default or base providers " |
58 | 0 | "available. Did you forget to load them?"); |
59 | 0 | return 0; |
60 | 0 | } |
61 | | |
62 | 329 | return encoder_process(&data) > 0; |
63 | 329 | } |
64 | | |
65 | | #ifndef OPENSSL_NO_STDIO |
66 | | static BIO *bio_from_file(FILE *fp) |
67 | 0 | { |
68 | 0 | BIO *b; |
69 | |
|
70 | 0 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
71 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_BUF_LIB); |
72 | 0 | return NULL; |
73 | 0 | } |
74 | 0 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
75 | 0 | return b; |
76 | 0 | } |
77 | | |
78 | | int OSSL_ENCODER_to_fp(OSSL_ENCODER_CTX *ctx, FILE *fp) |
79 | 0 | { |
80 | 0 | BIO *b = bio_from_file(fp); |
81 | 0 | int ret = 0; |
82 | |
|
83 | 0 | if (b != NULL) |
84 | 0 | ret = OSSL_ENCODER_to_bio(ctx, b); |
85 | |
|
86 | 0 | BIO_free(b); |
87 | 0 | return ret; |
88 | 0 | } |
89 | | #endif |
90 | | |
91 | | int OSSL_ENCODER_to_data(OSSL_ENCODER_CTX *ctx, unsigned char **pdata, |
92 | | size_t *pdata_len) |
93 | 0 | { |
94 | 0 | BIO *out; |
95 | 0 | BUF_MEM *buf = NULL; |
96 | 0 | int ret = 0; |
97 | |
|
98 | 0 | if (pdata_len == NULL) { |
99 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
100 | 0 | return 0; |
101 | 0 | } |
102 | | |
103 | 0 | out = BIO_new(BIO_s_mem()); |
104 | |
|
105 | 0 | if (out != NULL |
106 | 0 | && OSSL_ENCODER_to_bio(ctx, out) |
107 | 0 | && BIO_get_mem_ptr(out, &buf) > 0) { |
108 | 0 | ret = 1; /* Hope for the best. A too small buffer will clear this */ |
109 | |
|
110 | 0 | if (pdata != NULL && *pdata != NULL) { |
111 | 0 | if (*pdata_len < buf->length) |
112 | | /* |
113 | | * It's tempting to do |*pdata_len = (size_t)buf->length| |
114 | | * However, it's believed to be confusing more than helpful, |
115 | | * so we don't. |
116 | | */ |
117 | 0 | ret = 0; |
118 | 0 | else |
119 | 0 | *pdata_len -= buf->length; |
120 | 0 | } else { |
121 | | /* The buffer with the right size is already allocated for us */ |
122 | 0 | *pdata_len = (size_t)buf->length; |
123 | 0 | } |
124 | |
|
125 | 0 | if (ret) { |
126 | 0 | if (pdata != NULL) { |
127 | 0 | if (*pdata != NULL) { |
128 | 0 | memcpy(*pdata, buf->data, buf->length); |
129 | 0 | *pdata += buf->length; |
130 | 0 | } else { |
131 | | /* In this case, we steal the data from BIO_s_mem() */ |
132 | 0 | *pdata = (unsigned char *)buf->data; |
133 | 0 | buf->data = NULL; |
134 | 0 | } |
135 | 0 | } |
136 | 0 | } |
137 | 0 | } |
138 | 0 | BIO_free(out); |
139 | 0 | return ret; |
140 | 0 | } |
141 | | |
142 | | int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection) |
143 | 329 | { |
144 | 329 | if (!ossl_assert(ctx != NULL)) { |
145 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
146 | 0 | return 0; |
147 | 0 | } |
148 | | |
149 | 329 | if (!ossl_assert(selection != 0)) { |
150 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT); |
151 | 0 | return 0; |
152 | 0 | } |
153 | | |
154 | 329 | ctx->selection = selection; |
155 | 329 | return 1; |
156 | 329 | } |
157 | | |
158 | | int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx, |
159 | | const char *output_type) |
160 | 329 | { |
161 | 329 | if (!ossl_assert(ctx != NULL) || !ossl_assert(output_type != NULL)) { |
162 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
163 | 0 | return 0; |
164 | 0 | } |
165 | | |
166 | 329 | ctx->output_type = output_type; |
167 | 329 | return 1; |
168 | 329 | } |
169 | | |
170 | | int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx, |
171 | | const char *output_structure) |
172 | 0 | { |
173 | 0 | if (!ossl_assert(ctx != NULL) || !ossl_assert(output_structure != NULL)) { |
174 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
175 | 0 | return 0; |
176 | 0 | } |
177 | | |
178 | 0 | ctx->output_structure = output_structure; |
179 | 0 | return 1; |
180 | 0 | } |
181 | | |
182 | | static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder, |
183 | | void *encoderctx) |
184 | 1.43k | { |
185 | 1.43k | OSSL_ENCODER_INSTANCE *encoder_inst = NULL; |
186 | 1.43k | const OSSL_PROVIDER *prov; |
187 | 1.43k | OSSL_LIB_CTX *libctx; |
188 | 1.43k | const OSSL_PROPERTY_LIST *props; |
189 | 1.43k | const OSSL_PROPERTY_DEFINITION *prop; |
190 | | |
191 | 1.43k | if (!ossl_assert(encoder != NULL)) { |
192 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
193 | 0 | return 0; |
194 | 0 | } |
195 | | |
196 | 1.43k | if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL) |
197 | 0 | return 0; |
198 | | |
199 | 1.43k | if (!OSSL_ENCODER_up_ref(encoder)) { |
200 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR); |
201 | 0 | goto err; |
202 | 0 | } |
203 | | |
204 | 1.43k | prov = OSSL_ENCODER_get0_provider(encoder); |
205 | 1.43k | libctx = ossl_provider_libctx(prov); |
206 | 1.43k | props = ossl_encoder_parsed_properties(encoder); |
207 | 1.43k | if (props == NULL) { |
208 | 0 | ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION, |
209 | 0 | "there are no property definitions with encoder %s", |
210 | 0 | OSSL_ENCODER_get0_name(encoder)); |
211 | 0 | goto err; |
212 | 0 | } |
213 | | |
214 | | /* The "output" property is mandatory */ |
215 | 1.43k | prop = ossl_property_find_property(props, libctx, "output"); |
216 | 1.43k | encoder_inst->output_type = ossl_property_get_string_value(libctx, prop); |
217 | 1.43k | if (encoder_inst->output_type == NULL) { |
218 | 0 | ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION, |
219 | 0 | "the mandatory 'output' property is missing " |
220 | 0 | "for encoder %s (properties: %s)", |
221 | 0 | OSSL_ENCODER_get0_name(encoder), |
222 | 0 | OSSL_ENCODER_get0_properties(encoder)); |
223 | 0 | goto err; |
224 | 0 | } |
225 | | |
226 | | /* The "structure" property is optional */ |
227 | 1.43k | prop = ossl_property_find_property(props, libctx, "structure"); |
228 | 1.43k | if (prop != NULL) |
229 | 922 | encoder_inst->output_structure |
230 | 922 | = ossl_property_get_string_value(libctx, prop); |
231 | | |
232 | 1.43k | encoder_inst->encoder = encoder; |
233 | 1.43k | encoder_inst->encoderctx = encoderctx; |
234 | 1.43k | return encoder_inst; |
235 | 0 | err: |
236 | 0 | ossl_encoder_instance_free(encoder_inst); |
237 | 0 | return NULL; |
238 | 1.43k | } |
239 | | |
240 | | void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst) |
241 | 1.43k | { |
242 | 1.43k | if (encoder_inst != NULL) { |
243 | 1.43k | if (encoder_inst->encoder != NULL) |
244 | 1.43k | encoder_inst->encoder->freectx(encoder_inst->encoderctx); |
245 | 1.43k | encoder_inst->encoderctx = NULL; |
246 | 1.43k | OSSL_ENCODER_free(encoder_inst->encoder); |
247 | 1.43k | encoder_inst->encoder = NULL; |
248 | 1.43k | OPENSSL_free(encoder_inst); |
249 | 1.43k | } |
250 | 1.43k | } |
251 | | |
252 | | static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx, |
253 | | OSSL_ENCODER_INSTANCE *ei) |
254 | 1.43k | { |
255 | 1.43k | int ok; |
256 | | |
257 | 1.43k | if (ctx->encoder_insts == NULL |
258 | 1.43k | && (ctx->encoder_insts = |
259 | 329 | sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) { |
260 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_CRYPTO_LIB); |
261 | 0 | return 0; |
262 | 0 | } |
263 | | |
264 | 1.43k | ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0); |
265 | 1.43k | if (ok) { |
266 | 1.43k | OSSL_TRACE_BEGIN(ENCODER) { |
267 | 0 | BIO_printf(trc_out, |
268 | 0 | "(ctx %p) Added encoder instance %p (encoder %p):\n" |
269 | 0 | " %s with %s\n", |
270 | 0 | (void *)ctx, (void *)ei, (void *)ei->encoder, |
271 | 0 | OSSL_ENCODER_get0_name(ei->encoder), |
272 | 0 | OSSL_ENCODER_get0_properties(ei->encoder)); |
273 | 1.43k | } OSSL_TRACE_END(ENCODER); |
274 | 1.43k | } |
275 | 1.43k | return ok; |
276 | 1.43k | } |
277 | | |
278 | | int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder) |
279 | 1.43k | { |
280 | 1.43k | OSSL_ENCODER_INSTANCE *encoder_inst = NULL; |
281 | 1.43k | const OSSL_PROVIDER *prov = NULL; |
282 | 1.43k | void *encoderctx = NULL; |
283 | 1.43k | void *provctx = NULL; |
284 | | |
285 | 1.43k | if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) { |
286 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
287 | 0 | return 0; |
288 | 0 | } |
289 | | |
290 | 1.43k | prov = OSSL_ENCODER_get0_provider(encoder); |
291 | 1.43k | provctx = OSSL_PROVIDER_get0_provider_ctx(prov); |
292 | | |
293 | 1.43k | if ((encoderctx = encoder->newctx(provctx)) == NULL |
294 | 1.43k | || (encoder_inst = |
295 | 1.43k | ossl_encoder_instance_new(encoder, encoderctx)) == NULL) |
296 | 0 | goto err; |
297 | | /* Avoid double free of encoderctx on further errors */ |
298 | 1.43k | encoderctx = NULL; |
299 | | |
300 | 1.43k | if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst)) |
301 | 0 | goto err; |
302 | | |
303 | 1.43k | return 1; |
304 | 0 | err: |
305 | 0 | ossl_encoder_instance_free(encoder_inst); |
306 | 0 | if (encoderctx != NULL) |
307 | 0 | encoder->freectx(encoderctx); |
308 | 0 | return 0; |
309 | 1.43k | } |
310 | | |
311 | | int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx, |
312 | | OSSL_LIB_CTX *libctx, const char *propq) |
313 | 329 | { |
314 | 329 | return 1; |
315 | 329 | } |
316 | | |
317 | | int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx) |
318 | 1.31k | { |
319 | 1.31k | if (ctx == NULL || ctx->encoder_insts == NULL) |
320 | 0 | return 0; |
321 | 1.31k | return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts); |
322 | 1.31k | } |
323 | | |
324 | | int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx, |
325 | | OSSL_ENCODER_CONSTRUCT *construct) |
326 | 329 | { |
327 | 329 | if (!ossl_assert(ctx != NULL)) { |
328 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
329 | 0 | return 0; |
330 | 0 | } |
331 | 329 | ctx->construct = construct; |
332 | 329 | return 1; |
333 | 329 | } |
334 | | |
335 | | int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx, |
336 | | void *construct_data) |
337 | 329 | { |
338 | 329 | if (!ossl_assert(ctx != NULL)) { |
339 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
340 | 0 | return 0; |
341 | 0 | } |
342 | 329 | ctx->construct_data = construct_data; |
343 | 329 | return 1; |
344 | 329 | } |
345 | | |
346 | | int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx, |
347 | | OSSL_ENCODER_CLEANUP *cleanup) |
348 | 329 | { |
349 | 329 | if (!ossl_assert(ctx != NULL)) { |
350 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
351 | 0 | return 0; |
352 | 0 | } |
353 | 329 | ctx->cleanup = cleanup; |
354 | 329 | return 1; |
355 | 329 | } |
356 | | |
357 | | OSSL_ENCODER * |
358 | | OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst) |
359 | 3.20k | { |
360 | 3.20k | if (encoder_inst == NULL) |
361 | 0 | return NULL; |
362 | 3.20k | return encoder_inst->encoder; |
363 | 3.20k | } |
364 | | |
365 | | void * |
366 | | OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst) |
367 | 2.87k | { |
368 | 2.87k | if (encoder_inst == NULL) |
369 | 0 | return NULL; |
370 | 2.87k | return encoder_inst->encoderctx; |
371 | 2.87k | } |
372 | | |
373 | | const char * |
374 | | OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst) |
375 | 1.43k | { |
376 | 1.43k | if (encoder_inst == NULL) |
377 | 0 | return NULL; |
378 | 1.43k | return encoder_inst->output_type; |
379 | 1.43k | } |
380 | | |
381 | | const char * |
382 | | OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst) |
383 | 1.43k | { |
384 | 1.43k | if (encoder_inst == NULL) |
385 | 0 | return NULL; |
386 | 1.43k | return encoder_inst->output_structure; |
387 | 1.43k | } |
388 | | |
389 | | static int encoder_process(struct encoder_process_data_st *data) |
390 | 658 | { |
391 | 658 | OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL; |
392 | 658 | OSSL_ENCODER *current_encoder = NULL; |
393 | 658 | OSSL_ENCODER_CTX *current_encoder_ctx = NULL; |
394 | 658 | BIO *allocated_out = NULL; |
395 | 658 | const void *original_data = NULL; |
396 | 658 | OSSL_PARAM abstract[10]; |
397 | 658 | const OSSL_PARAM *current_abstract = NULL; |
398 | 658 | int i; |
399 | 658 | int ok = -1; /* -1 signifies that the lookup loop gave nothing */ |
400 | 658 | int top = 0; |
401 | | |
402 | 658 | if (data->next_encoder_inst == NULL) { |
403 | | /* First iteration, where we prepare for what is to come */ |
404 | | |
405 | 329 | data->count_output_structure = |
406 | 329 | data->ctx->output_structure == NULL ? -1 : 0; |
407 | 329 | top = 1; |
408 | 329 | } |
409 | | |
410 | 1.76k | for (i = data->current_encoder_inst_index; i-- > 0;) { |
411 | 1.43k | OSSL_ENCODER *next_encoder = NULL; |
412 | 1.43k | const char *current_output_type; |
413 | 1.43k | const char *current_output_structure; |
414 | 1.43k | struct encoder_process_data_st new_data; |
415 | | |
416 | 1.43k | if (!top) |
417 | 0 | next_encoder = |
418 | 0 | OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst); |
419 | | |
420 | 1.43k | current_encoder_inst = |
421 | 1.43k | sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i); |
422 | 1.43k | current_encoder = |
423 | 1.43k | OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst); |
424 | 1.43k | current_encoder_ctx = |
425 | 1.43k | OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst); |
426 | 1.43k | current_output_type = |
427 | 1.43k | OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst); |
428 | 1.43k | current_output_structure = |
429 | 1.43k | OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst); |
430 | 1.43k | memset(&new_data, 0, sizeof(new_data)); |
431 | 1.43k | new_data.ctx = data->ctx; |
432 | 1.43k | new_data.current_encoder_inst_index = i; |
433 | 1.43k | new_data.next_encoder_inst = current_encoder_inst; |
434 | 1.43k | new_data.count_output_structure = data->count_output_structure; |
435 | 1.43k | new_data.level = data->level + 1; |
436 | | |
437 | 1.43k | OSSL_TRACE_BEGIN(ENCODER) { |
438 | 0 | BIO_printf(trc_out, |
439 | 0 | "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n", |
440 | 0 | data->level, (void *)data->ctx, |
441 | 0 | (void *)current_encoder_inst, (void *)current_encoder); |
442 | 1.43k | } OSSL_TRACE_END(ENCODER); |
443 | | |
444 | | /* |
445 | | * If this is the top call, we check if the output type of the current |
446 | | * encoder matches the desired output type. |
447 | | * If this isn't the top call, i.e. this is deeper in the recursion, |
448 | | * we instead check if the output type of the current encoder matches |
449 | | * the name of the next encoder (the one found by the parent call). |
450 | | */ |
451 | 1.43k | if (top) { |
452 | 1.43k | if (data->ctx->output_type != NULL |
453 | 1.43k | && OPENSSL_strcasecmp(current_output_type, |
454 | 1.43k | data->ctx->output_type) != 0) { |
455 | 1.11k | OSSL_TRACE_BEGIN(ENCODER) { |
456 | 0 | BIO_printf(trc_out, |
457 | 0 | "[%d] Skipping because current encoder output type (%s) != desired output type (%s)\n", |
458 | 0 | data->level, |
459 | 0 | current_output_type, data->ctx->output_type); |
460 | 1.11k | } OSSL_TRACE_END(ENCODER); |
461 | 1.11k | continue; |
462 | 1.11k | } |
463 | 1.43k | } else { |
464 | 0 | if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) { |
465 | 0 | OSSL_TRACE_BEGIN(ENCODER) { |
466 | 0 | BIO_printf(trc_out, |
467 | 0 | "[%d] Skipping because current encoder output type (%s) != name of encoder %p\n", |
468 | 0 | data->level, |
469 | 0 | current_output_type, (void *)next_encoder); |
470 | 0 | } OSSL_TRACE_END(ENCODER); |
471 | 0 | continue; |
472 | 0 | } |
473 | 0 | } |
474 | | |
475 | | /* |
476 | | * If the caller and the current encoder specify an output structure, |
477 | | * Check if they match. If they do, count the match, otherwise skip |
478 | | * the current encoder. |
479 | | */ |
480 | 329 | if (data->ctx->output_structure != NULL |
481 | 329 | && current_output_structure != NULL) { |
482 | 0 | if (OPENSSL_strcasecmp(data->ctx->output_structure, |
483 | 0 | current_output_structure) != 0) { |
484 | 0 | OSSL_TRACE_BEGIN(ENCODER) { |
485 | 0 | BIO_printf(trc_out, |
486 | 0 | "[%d] Skipping because current encoder output structure (%s) != ctx output structure (%s)\n", |
487 | 0 | data->level, |
488 | 0 | current_output_structure, |
489 | 0 | data->ctx->output_structure); |
490 | 0 | } OSSL_TRACE_END(ENCODER); |
491 | 0 | continue; |
492 | 0 | } |
493 | | |
494 | 0 | data->count_output_structure++; |
495 | 0 | } |
496 | | |
497 | | /* |
498 | | * Recurse to process the encoder implementations before the current |
499 | | * one. |
500 | | */ |
501 | 329 | ok = encoder_process(&new_data); |
502 | | |
503 | 329 | data->prev_encoder_inst = new_data.prev_encoder_inst; |
504 | 329 | data->running_output = new_data.running_output; |
505 | 329 | data->running_output_length = new_data.running_output_length; |
506 | | |
507 | | /* |
508 | | * ok == -1 means that the recursion call above gave no further |
509 | | * encoders, and that the one we're currently at should |
510 | | * be tried. |
511 | | * ok == 0 means that something failed in the recursion call |
512 | | * above, making the result unsuitable for a chain. |
513 | | * In this case, we simply continue to try finding a |
514 | | * suitable encoder at this recursion level. |
515 | | * ok == 1 means that the recursion call was successful, and we |
516 | | * try to use the result at this recursion level. |
517 | | */ |
518 | 329 | if (ok != 0) |
519 | 329 | break; |
520 | | |
521 | 0 | OSSL_TRACE_BEGIN(ENCODER) { |
522 | 0 | BIO_printf(trc_out, |
523 | 0 | "[%d] Skipping because recursion level %d failed\n", |
524 | 0 | data->level, new_data.level); |
525 | 0 | } OSSL_TRACE_END(ENCODER); |
526 | 0 | } |
527 | | |
528 | | /* |
529 | | * If |i < 0|, we didn't find any useful encoder in this recursion, so |
530 | | * we do the rest of the process only if |i >= 0|. |
531 | | */ |
532 | 658 | if (i < 0) { |
533 | 329 | ok = -1; |
534 | | |
535 | 329 | OSSL_TRACE_BEGIN(ENCODER) { |
536 | 0 | BIO_printf(trc_out, |
537 | 0 | "[%d] (ctx %p) No suitable encoder found\n", |
538 | 0 | data->level, (void *)data->ctx); |
539 | 329 | } OSSL_TRACE_END(ENCODER); |
540 | 329 | } else { |
541 | | /* Preparations */ |
542 | | |
543 | 329 | switch (ok) { |
544 | 0 | case 0: |
545 | 0 | break; |
546 | 329 | case -1: |
547 | | /* |
548 | | * We have reached the beginning of the encoder instance sequence, |
549 | | * so we prepare the object to be encoded. |
550 | | */ |
551 | | |
552 | | /* |
553 | | * |data->count_output_structure| is one of these values: |
554 | | * |
555 | | * -1 There is no desired output structure |
556 | | * 0 There is a desired output structure, and it wasn't |
557 | | * matched by any of the encoder instances that were |
558 | | * considered |
559 | | * >0 There is a desired output structure, and at least one |
560 | | * of the encoder instances matched it |
561 | | */ |
562 | 329 | if (data->count_output_structure == 0) |
563 | 0 | return 0; |
564 | | |
565 | 329 | original_data = |
566 | 329 | data->ctx->construct(current_encoder_inst, |
567 | 329 | data->ctx->construct_data); |
568 | | |
569 | | /* Also set the data type, using the encoder implementation name */ |
570 | 329 | data->data_type = OSSL_ENCODER_get0_name(current_encoder); |
571 | | |
572 | | /* Assume that the constructor recorded an error */ |
573 | 329 | if (original_data != NULL) |
574 | 329 | ok = 1; |
575 | 0 | else |
576 | 0 | ok = 0; |
577 | 329 | break; |
578 | 0 | case 1: |
579 | 0 | if (!ossl_assert(data->running_output != NULL)) { |
580 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR); |
581 | 0 | ok = 0; |
582 | 0 | break; |
583 | 0 | } |
584 | | |
585 | 0 | { |
586 | | /* |
587 | | * Create an object abstraction from the latest output, which |
588 | | * was stolen from the previous round. |
589 | | */ |
590 | |
|
591 | 0 | OSSL_PARAM *abstract_p = abstract; |
592 | 0 | const char *prev_output_structure = |
593 | 0 | OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst); |
594 | |
|
595 | 0 | *abstract_p++ = |
596 | 0 | OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, |
597 | 0 | (char *)data->data_type, 0); |
598 | 0 | if (prev_output_structure != NULL) |
599 | 0 | *abstract_p++ = |
600 | 0 | OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, |
601 | 0 | (char *)prev_output_structure, |
602 | 0 | 0); |
603 | 0 | *abstract_p++ = |
604 | 0 | OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA, |
605 | 0 | data->running_output, |
606 | 0 | data->running_output_length); |
607 | 0 | *abstract_p = OSSL_PARAM_construct_end(); |
608 | 0 | current_abstract = abstract; |
609 | 0 | } |
610 | 0 | break; |
611 | 329 | } |
612 | | |
613 | | /* Calling the encoder implementation */ |
614 | | |
615 | 329 | if (ok) { |
616 | 329 | OSSL_CORE_BIO *cbio = NULL; |
617 | 329 | BIO *current_out = NULL; |
618 | | |
619 | | /* |
620 | | * If we're at the last encoder instance to use, we're setting up |
621 | | * final output. Otherwise, set up an intermediary memory output. |
622 | | */ |
623 | 329 | if (top) |
624 | 329 | current_out = data->bio; |
625 | 0 | else if ((current_out = allocated_out = BIO_new(BIO_s_mem())) |
626 | 0 | == NULL) |
627 | 0 | ok = 0; /* Assume BIO_new() recorded an error */ |
628 | | |
629 | 329 | if (ok) |
630 | 329 | ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL; |
631 | 329 | if (ok) { |
632 | 329 | ok = current_encoder->encode(current_encoder_ctx, cbio, |
633 | 329 | original_data, current_abstract, |
634 | 329 | data->ctx->selection, |
635 | 329 | ossl_pw_passphrase_callback_enc, |
636 | 329 | &data->ctx->pwdata); |
637 | 329 | OSSL_TRACE_BEGIN(ENCODER) { |
638 | 0 | BIO_printf(trc_out, |
639 | 0 | "[%d] (ctx %p) Running encoder instance %p => %d\n", |
640 | 0 | data->level, (void *)data->ctx, |
641 | 0 | (void *)current_encoder_inst, ok); |
642 | 329 | } OSSL_TRACE_END(ENCODER); |
643 | 329 | } |
644 | | |
645 | 329 | ossl_core_bio_free(cbio); |
646 | 329 | data->prev_encoder_inst = current_encoder_inst; |
647 | 329 | } |
648 | 329 | } |
649 | | |
650 | | /* Cleanup and collecting the result */ |
651 | | |
652 | 658 | OPENSSL_free(data->running_output); |
653 | 658 | data->running_output = NULL; |
654 | | |
655 | | /* |
656 | | * Steal the output from the BIO_s_mem, if we did allocate one. |
657 | | * That'll be the data for an object abstraction in the next round. |
658 | | */ |
659 | 658 | if (allocated_out != NULL) { |
660 | 0 | BUF_MEM *buf; |
661 | |
|
662 | 0 | BIO_get_mem_ptr(allocated_out, &buf); |
663 | 0 | data->running_output = (unsigned char *)buf->data; |
664 | 0 | data->running_output_length = buf->length; |
665 | 0 | memset(buf, 0, sizeof(*buf)); |
666 | 0 | } |
667 | | |
668 | 658 | BIO_free(allocated_out); |
669 | 658 | if (original_data != NULL) |
670 | 329 | data->ctx->cleanup(data->ctx->construct_data); |
671 | 658 | return ok; |
672 | 658 | } |