/src/openssl32/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 | return 0; |
203 | | |
204 | 352k | if (!OSSL_ENCODER_up_ref(encoder)) { |
205 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR); |
206 | 0 | goto err; |
207 | 0 | } |
208 | | |
209 | 352k | prov = OSSL_ENCODER_get0_provider(encoder); |
210 | 352k | libctx = ossl_provider_libctx(prov); |
211 | 352k | props = ossl_encoder_parsed_properties(encoder); |
212 | 352k | if (props == NULL) { |
213 | 0 | ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION, |
214 | 0 | "there are no property definitions with encoder %s", |
215 | 0 | OSSL_ENCODER_get0_name(encoder)); |
216 | 0 | goto err; |
217 | 0 | } |
218 | | |
219 | | /* The "output" property is mandatory */ |
220 | 352k | prop = ossl_property_find_property(props, libctx, "output"); |
221 | 352k | encoder_inst->output_type = ossl_property_get_string_value(libctx, prop); |
222 | 352k | if (encoder_inst->output_type == NULL) { |
223 | 0 | ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION, |
224 | 0 | "the mandatory 'output' property is missing " |
225 | 0 | "for encoder %s (properties: %s)", |
226 | 0 | OSSL_ENCODER_get0_name(encoder), |
227 | 0 | OSSL_ENCODER_get0_properties(encoder)); |
228 | 0 | goto err; |
229 | 0 | } |
230 | | |
231 | | /* The "structure" property is optional */ |
232 | 352k | prop = ossl_property_find_property(props, libctx, "structure"); |
233 | 352k | if (prop != NULL) |
234 | 275k | encoder_inst->output_structure |
235 | 275k | = ossl_property_get_string_value(libctx, prop); |
236 | | |
237 | 352k | encoder_inst->encoder = encoder; |
238 | 352k | encoder_inst->encoderctx = encoderctx; |
239 | 352k | return encoder_inst; |
240 | 0 | err: |
241 | 0 | ossl_encoder_instance_free(encoder_inst); |
242 | 0 | return NULL; |
243 | 352k | } |
244 | | |
245 | | void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst) |
246 | 352k | { |
247 | 352k | if (encoder_inst != NULL) { |
248 | 352k | if (encoder_inst->encoder != NULL) |
249 | 352k | encoder_inst->encoder->freectx(encoder_inst->encoderctx); |
250 | 352k | encoder_inst->encoderctx = NULL; |
251 | 352k | OSSL_ENCODER_free(encoder_inst->encoder); |
252 | 352k | encoder_inst->encoder = NULL; |
253 | 352k | OPENSSL_free(encoder_inst); |
254 | 352k | } |
255 | 352k | } |
256 | | |
257 | | static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx, |
258 | | OSSL_ENCODER_INSTANCE *ei) |
259 | 352k | { |
260 | 352k | int ok; |
261 | | |
262 | 352k | if (ctx->encoder_insts == NULL |
263 | 352k | && (ctx->encoder_insts = |
264 | 43.7k | sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) { |
265 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_CRYPTO_LIB); |
266 | 0 | return 0; |
267 | 0 | } |
268 | | |
269 | 352k | ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0); |
270 | 352k | if (ok) { |
271 | 352k | OSSL_TRACE_BEGIN(ENCODER) { |
272 | 0 | BIO_printf(trc_out, |
273 | 0 | "(ctx %p) Added encoder instance %p (encoder %p):\n" |
274 | 0 | " %s with %s\n", |
275 | 0 | (void *)ctx, (void *)ei, (void *)ei->encoder, |
276 | 0 | OSSL_ENCODER_get0_name(ei->encoder), |
277 | 0 | OSSL_ENCODER_get0_properties(ei->encoder)); |
278 | 352k | } OSSL_TRACE_END(ENCODER); |
279 | 352k | } |
280 | 352k | return ok; |
281 | 352k | } |
282 | | |
283 | | int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder) |
284 | 352k | { |
285 | 352k | OSSL_ENCODER_INSTANCE *encoder_inst = NULL; |
286 | 352k | const OSSL_PROVIDER *prov = NULL; |
287 | 352k | void *encoderctx = NULL; |
288 | 352k | void *provctx = NULL; |
289 | | |
290 | 352k | if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) { |
291 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
292 | 0 | return 0; |
293 | 0 | } |
294 | | |
295 | 352k | prov = OSSL_ENCODER_get0_provider(encoder); |
296 | 352k | provctx = OSSL_PROVIDER_get0_provider_ctx(prov); |
297 | | |
298 | 352k | if ((encoderctx = encoder->newctx(provctx)) == NULL |
299 | 352k | || (encoder_inst = |
300 | 352k | ossl_encoder_instance_new(encoder, encoderctx)) == NULL) |
301 | 0 | goto err; |
302 | | /* Avoid double free of encoderctx on further errors */ |
303 | 352k | encoderctx = NULL; |
304 | | |
305 | 352k | if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst)) |
306 | 0 | goto err; |
307 | | |
308 | 352k | return 1; |
309 | 0 | err: |
310 | 0 | ossl_encoder_instance_free(encoder_inst); |
311 | 0 | if (encoderctx != NULL) |
312 | 0 | encoder->freectx(encoderctx); |
313 | 0 | return 0; |
314 | 352k | } |
315 | | |
316 | | int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx, |
317 | | OSSL_LIB_CTX *libctx, const char *propq) |
318 | 44.6k | { |
319 | 44.6k | return 1; |
320 | 44.6k | } |
321 | | |
322 | | int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx) |
323 | 169k | { |
324 | 169k | if (ctx == NULL || ctx->encoder_insts == NULL) |
325 | 874 | return 0; |
326 | 168k | return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts); |
327 | 169k | } |
328 | | |
329 | | int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx, |
330 | | OSSL_ENCODER_CONSTRUCT *construct) |
331 | 43.7k | { |
332 | 43.7k | if (!ossl_assert(ctx != NULL)) { |
333 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
334 | 0 | return 0; |
335 | 0 | } |
336 | 43.7k | ctx->construct = construct; |
337 | 43.7k | return 1; |
338 | 43.7k | } |
339 | | |
340 | | int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx, |
341 | | void *construct_data) |
342 | 43.7k | { |
343 | 43.7k | if (!ossl_assert(ctx != NULL)) { |
344 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
345 | 0 | return 0; |
346 | 0 | } |
347 | 43.7k | ctx->construct_data = construct_data; |
348 | 43.7k | return 1; |
349 | 43.7k | } |
350 | | |
351 | | int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx, |
352 | | OSSL_ENCODER_CLEANUP *cleanup) |
353 | 43.7k | { |
354 | 43.7k | if (!ossl_assert(ctx != NULL)) { |
355 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
356 | 0 | return 0; |
357 | 0 | } |
358 | 43.7k | ctx->cleanup = cleanup; |
359 | 43.7k | return 1; |
360 | 43.7k | } |
361 | | |
362 | | OSSL_ENCODER * |
363 | | OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst) |
364 | 759k | { |
365 | 759k | if (encoder_inst == NULL) |
366 | 0 | return NULL; |
367 | 759k | return encoder_inst->encoder; |
368 | 759k | } |
369 | | |
370 | | void * |
371 | | OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst) |
372 | 705k | { |
373 | 705k | if (encoder_inst == NULL) |
374 | 0 | return NULL; |
375 | 705k | return encoder_inst->encoderctx; |
376 | 705k | } |
377 | | |
378 | | const char * |
379 | | OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst) |
380 | 352k | { |
381 | 352k | if (encoder_inst == NULL) |
382 | 0 | return NULL; |
383 | 352k | return encoder_inst->output_type; |
384 | 352k | } |
385 | | |
386 | | const char * |
387 | | OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst) |
388 | 352k | { |
389 | 352k | if (encoder_inst == NULL) |
390 | 0 | return NULL; |
391 | 352k | return encoder_inst->output_structure; |
392 | 352k | } |
393 | | |
394 | | static int encoder_process(struct encoder_process_data_st *data) |
395 | 87.4k | { |
396 | 87.4k | OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL; |
397 | 87.4k | OSSL_ENCODER *current_encoder = NULL; |
398 | 87.4k | OSSL_ENCODER_CTX *current_encoder_ctx = NULL; |
399 | 87.4k | BIO *allocated_out = NULL; |
400 | 87.4k | const void *original_data = NULL; |
401 | 87.4k | OSSL_PARAM abstract[10]; |
402 | 87.4k | const OSSL_PARAM *current_abstract = NULL; |
403 | 87.4k | int i; |
404 | 87.4k | int ok = -1; /* -1 signifies that the lookup loop gave nothing */ |
405 | 87.4k | int top = 0; |
406 | | |
407 | 87.4k | if (data->next_encoder_inst == NULL) { |
408 | | /* First iteration, where we prepare for what is to come */ |
409 | | |
410 | 43.7k | data->count_output_structure = |
411 | 43.7k | data->ctx->output_structure == NULL ? -1 : 0; |
412 | 43.7k | top = 1; |
413 | 43.7k | } |
414 | | |
415 | 396k | for (i = data->current_encoder_inst_index; i-- > 0;) { |
416 | 352k | OSSL_ENCODER *next_encoder = NULL; |
417 | 352k | const char *current_output_type; |
418 | 352k | const char *current_output_structure; |
419 | 352k | struct encoder_process_data_st new_data; |
420 | | |
421 | 352k | if (!top) |
422 | 10.3k | next_encoder = |
423 | 10.3k | OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst); |
424 | | |
425 | 352k | current_encoder_inst = |
426 | 352k | sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i); |
427 | 352k | current_encoder = |
428 | 352k | OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst); |
429 | 352k | current_encoder_ctx = |
430 | 352k | OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst); |
431 | 352k | current_output_type = |
432 | 352k | OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst); |
433 | 352k | current_output_structure = |
434 | 352k | OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst); |
435 | 352k | memset(&new_data, 0, sizeof(new_data)); |
436 | 352k | new_data.ctx = data->ctx; |
437 | 352k | new_data.current_encoder_inst_index = i; |
438 | 352k | new_data.next_encoder_inst = current_encoder_inst; |
439 | 352k | new_data.count_output_structure = data->count_output_structure; |
440 | 352k | new_data.level = data->level + 1; |
441 | | |
442 | 352k | OSSL_TRACE_BEGIN(ENCODER) { |
443 | 0 | BIO_printf(trc_out, |
444 | 0 | "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n", |
445 | 0 | data->level, (void *)data->ctx, |
446 | 0 | (void *)current_encoder_inst, (void *)current_encoder); |
447 | 352k | } OSSL_TRACE_END(ENCODER); |
448 | | |
449 | | /* |
450 | | * If this is the top call, we check if the output type of the current |
451 | | * encoder matches the desired output type. |
452 | | * If this isn't the top call, i.e. this is deeper in the recursion, |
453 | | * we instead check if the output type of the current encoder matches |
454 | | * the name of the next encoder (the one found by the parent call). |
455 | | */ |
456 | 352k | if (top) { |
457 | 342k | if (data->ctx->output_type != NULL |
458 | 342k | && OPENSSL_strcasecmp(current_output_type, |
459 | 342k | data->ctx->output_type) != 0) { |
460 | 275k | OSSL_TRACE_BEGIN(ENCODER) { |
461 | 0 | BIO_printf(trc_out, |
462 | 0 | "[%d] Skipping because current encoder output type (%s) != desired output type (%s)\n", |
463 | 0 | data->level, |
464 | 0 | current_output_type, data->ctx->output_type); |
465 | 275k | } OSSL_TRACE_END(ENCODER); |
466 | 275k | continue; |
467 | 275k | } |
468 | 342k | } else { |
469 | 10.3k | if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) { |
470 | 10.3k | OSSL_TRACE_BEGIN(ENCODER) { |
471 | 0 | BIO_printf(trc_out, |
472 | 0 | "[%d] Skipping because current encoder output type (%s) != name of encoder %p\n", |
473 | 0 | data->level, |
474 | 0 | current_output_type, (void *)next_encoder); |
475 | 10.3k | } OSSL_TRACE_END(ENCODER); |
476 | 10.3k | continue; |
477 | 10.3k | } |
478 | 10.3k | } |
479 | | |
480 | | /* |
481 | | * If the caller and the current encoder specify an output structure, |
482 | | * Check if they match. If they do, count the match, otherwise skip |
483 | | * the current encoder. |
484 | | */ |
485 | 66.2k | if (data->ctx->output_structure != NULL |
486 | 66.2k | && current_output_structure != NULL) { |
487 | 29.2k | if (OPENSSL_strcasecmp(data->ctx->output_structure, |
488 | 29.2k | current_output_structure) != 0) { |
489 | 22.6k | OSSL_TRACE_BEGIN(ENCODER) { |
490 | 0 | BIO_printf(trc_out, |
491 | 0 | "[%d] Skipping because current encoder output structure (%s) != ctx output structure (%s)\n", |
492 | 0 | data->level, |
493 | 0 | current_output_structure, |
494 | 0 | data->ctx->output_structure); |
495 | 22.6k | } OSSL_TRACE_END(ENCODER); |
496 | 22.6k | continue; |
497 | 22.6k | } |
498 | | |
499 | 6.59k | data->count_output_structure++; |
500 | 6.59k | } |
501 | | |
502 | | /* |
503 | | * Recurse to process the encoder implementations before the current |
504 | | * one. |
505 | | */ |
506 | 43.6k | ok = encoder_process(&new_data); |
507 | | |
508 | 43.6k | data->prev_encoder_inst = new_data.prev_encoder_inst; |
509 | 43.6k | data->running_output = new_data.running_output; |
510 | 43.6k | data->running_output_length = new_data.running_output_length; |
511 | | |
512 | | /* |
513 | | * ok == -1 means that the recursion call above gave no further |
514 | | * encoders, and that the one we're currently at should |
515 | | * be tried. |
516 | | * ok == 0 means that something failed in the recursion call |
517 | | * above, making the result unsuitable for a chain. |
518 | | * In this case, we simply continue to try finding a |
519 | | * suitable encoder at this recursion level. |
520 | | * ok == 1 means that the recursion call was successful, and we |
521 | | * try to use the result at this recursion level. |
522 | | */ |
523 | 43.6k | if (ok != 0) |
524 | 43.6k | break; |
525 | | |
526 | 0 | OSSL_TRACE_BEGIN(ENCODER) { |
527 | 0 | BIO_printf(trc_out, |
528 | 0 | "[%d] Skipping because recursion level %d failed\n", |
529 | 0 | data->level, new_data.level); |
530 | 0 | } OSSL_TRACE_END(ENCODER); |
531 | 0 | } |
532 | | |
533 | | /* |
534 | | * If |i < 0|, we didn't find any useful encoder in this recursion, so |
535 | | * we do the rest of the process only if |i >= 0|. |
536 | | */ |
537 | 87.4k | if (i < 0) { |
538 | 43.7k | ok = -1; |
539 | | |
540 | 43.7k | OSSL_TRACE_BEGIN(ENCODER) { |
541 | 0 | BIO_printf(trc_out, |
542 | 0 | "[%d] (ctx %p) No suitable encoder found\n", |
543 | 0 | data->level, (void *)data->ctx); |
544 | 43.7k | } OSSL_TRACE_END(ENCODER); |
545 | 43.7k | } else { |
546 | | /* Preparations */ |
547 | | |
548 | 43.6k | switch (ok) { |
549 | 0 | case 0: |
550 | 0 | break; |
551 | 43.6k | case -1: |
552 | | /* |
553 | | * We have reached the beginning of the encoder instance sequence, |
554 | | * so we prepare the object to be encoded. |
555 | | */ |
556 | | |
557 | | /* |
558 | | * |data->count_output_structure| is one of these values: |
559 | | * |
560 | | * -1 There is no desired output structure |
561 | | * 0 There is a desired output structure, and it wasn't |
562 | | * matched by any of the encoder instances that were |
563 | | * considered |
564 | | * >0 There is a desired output structure, and at least one |
565 | | * of the encoder instances matched it |
566 | | */ |
567 | 43.6k | if (data->count_output_structure == 0) |
568 | 0 | return 0; |
569 | | |
570 | 43.6k | original_data = |
571 | 43.6k | data->ctx->construct(current_encoder_inst, |
572 | 43.6k | data->ctx->construct_data); |
573 | | |
574 | | /* Also set the data type, using the encoder implementation name */ |
575 | 43.6k | data->data_type = OSSL_ENCODER_get0_name(current_encoder); |
576 | | |
577 | | /* Assume that the constructor recorded an error */ |
578 | 43.6k | if (original_data != NULL) |
579 | 43.6k | ok = 1; |
580 | 0 | else |
581 | 0 | ok = 0; |
582 | 43.6k | break; |
583 | 0 | case 1: |
584 | 0 | if (!ossl_assert(data->running_output != NULL)) { |
585 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR); |
586 | 0 | ok = 0; |
587 | 0 | break; |
588 | 0 | } |
589 | | |
590 | 0 | { |
591 | | /* |
592 | | * Create an object abstraction from the latest output, which |
593 | | * was stolen from the previous round. |
594 | | */ |
595 | |
|
596 | 0 | OSSL_PARAM *abstract_p = abstract; |
597 | 0 | const char *prev_output_structure = |
598 | 0 | OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst); |
599 | |
|
600 | 0 | *abstract_p++ = |
601 | 0 | OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, |
602 | 0 | (char *)data->data_type, 0); |
603 | 0 | if (prev_output_structure != NULL) |
604 | 0 | *abstract_p++ = |
605 | 0 | OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, |
606 | 0 | (char *)prev_output_structure, |
607 | 0 | 0); |
608 | 0 | *abstract_p++ = |
609 | 0 | OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA, |
610 | 0 | data->running_output, |
611 | 0 | data->running_output_length); |
612 | 0 | *abstract_p = OSSL_PARAM_construct_end(); |
613 | 0 | current_abstract = abstract; |
614 | 0 | } |
615 | 0 | break; |
616 | 43.6k | } |
617 | | |
618 | | /* Calling the encoder implementation */ |
619 | | |
620 | 43.6k | if (ok) { |
621 | 43.6k | OSSL_CORE_BIO *cbio = NULL; |
622 | 43.6k | BIO *current_out = NULL; |
623 | | |
624 | | /* |
625 | | * If we're at the last encoder instance to use, we're setting up |
626 | | * final output. Otherwise, set up an intermediary memory output. |
627 | | */ |
628 | 43.6k | if (top) |
629 | 43.6k | current_out = data->bio; |
630 | 0 | else if ((current_out = allocated_out = BIO_new(BIO_s_mem())) |
631 | 0 | == NULL) |
632 | 0 | ok = 0; /* Assume BIO_new() recorded an error */ |
633 | | |
634 | 43.6k | if (ok) |
635 | 43.6k | ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL; |
636 | 43.6k | if (ok) { |
637 | 43.6k | ok = current_encoder->encode(current_encoder_ctx, cbio, |
638 | 43.6k | original_data, current_abstract, |
639 | 43.6k | data->ctx->selection, |
640 | 43.6k | ossl_pw_passphrase_callback_enc, |
641 | 43.6k | &data->ctx->pwdata); |
642 | 43.6k | OSSL_TRACE_BEGIN(ENCODER) { |
643 | 0 | BIO_printf(trc_out, |
644 | 0 | "[%d] (ctx %p) Running encoder instance %p => %d\n", |
645 | 0 | data->level, (void *)data->ctx, |
646 | 0 | (void *)current_encoder_inst, ok); |
647 | 43.6k | } OSSL_TRACE_END(ENCODER); |
648 | 43.6k | } |
649 | | |
650 | 43.6k | ossl_core_bio_free(cbio); |
651 | 43.6k | data->prev_encoder_inst = current_encoder_inst; |
652 | 43.6k | } |
653 | 43.6k | } |
654 | | |
655 | | /* Cleanup and collecting the result */ |
656 | | |
657 | 87.4k | OPENSSL_free(data->running_output); |
658 | 87.4k | data->running_output = NULL; |
659 | | |
660 | | /* |
661 | | * Steal the output from the BIO_s_mem, if we did allocate one. |
662 | | * That'll be the data for an object abstraction in the next round. |
663 | | */ |
664 | 87.4k | if (allocated_out != NULL) { |
665 | 0 | BUF_MEM *buf; |
666 | |
|
667 | 0 | BIO_get_mem_ptr(allocated_out, &buf); |
668 | 0 | data->running_output = (unsigned char *)buf->data; |
669 | 0 | data->running_output_length = buf->length; |
670 | 0 | memset(buf, 0, sizeof(*buf)); |
671 | 0 | } |
672 | | |
673 | 87.4k | BIO_free(allocated_out); |
674 | 87.4k | if (original_data != NULL) |
675 | 43.6k | data->ctx->cleanup(data->ctx->construct_data); |
676 | 87.4k | return ok; |
677 | 87.4k | } |