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