]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/encode_decode/encoder_lib.c
Rename all getters to use get/get0 in name
[thirdparty/openssl.git] / crypto / encode_decode / encoder_lib.c
CommitLineData
742496f1 1/*
8020d79b 2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
742496f1
RL
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
b8975c68
RL
10#include "e_os.h" /* strcasecmp on Windows */
11#include <openssl/core_names.h>
742496f1 12#include <openssl/bio.h>
ece9304c 13#include <openssl/encoder.h>
b8975c68
RL
14#include <openssl/buffer.h>
15#include <openssl/params.h>
16#include <openssl/provider.h>
0b9f90f5 17#include <openssl/trace.h>
141cc94e 18#include "internal/bio.h"
ece9304c 19#include "encoder_local.h"
742496f1 20
b9a2afdf
RL
21struct 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};
40
41static int encoder_process(struct encoder_process_data_st *data);
b8975c68 42
ece9304c 43int OSSL_ENCODER_to_bio(OSSL_ENCODER_CTX *ctx, BIO *out)
742496f1 44{
b9a2afdf
RL
45 struct encoder_process_data_st data;
46
47 memset(&data, 0, sizeof(data));
48 data.ctx = ctx;
49 data.bio = out;
50 data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx);
51
40692ed7
MC
52 if (data.current_encoder_inst_index == 0) {
53 ERR_raise_data(ERR_LIB_OSSL_ENCODER, OSSL_ENCODER_R_ENCODER_NOT_FOUND,
54 "No encoders were found. For standard encoders you need "
55 "at least one of the default or base providers "
56 "available. Did you forget to load them?");
57 return 0;
58 }
59
b9a2afdf 60 return encoder_process(&data) > 0;
742496f1
RL
61}
62
63#ifndef OPENSSL_NO_STDIO
64static BIO *bio_from_file(FILE *fp)
65{
66 BIO *b;
67
68 if ((b = BIO_new(BIO_s_file())) == NULL) {
ece9304c 69 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_BUF_LIB);
742496f1
RL
70 return NULL;
71 }
72 BIO_set_fp(b, fp, BIO_NOCLOSE);
73 return b;
74}
75
ece9304c 76int OSSL_ENCODER_to_fp(OSSL_ENCODER_CTX *ctx, FILE *fp)
742496f1
RL
77{
78 BIO *b = bio_from_file(fp);
79 int ret = 0;
80
81 if (b != NULL)
ece9304c 82 ret = OSSL_ENCODER_to_bio(ctx, b);
742496f1
RL
83
84 BIO_free(b);
85 return ret;
86}
87#endif
b8975c68 88
25cf949f
RL
89int OSSL_ENCODER_to_data(OSSL_ENCODER_CTX *ctx, unsigned char **pdata,
90 size_t *pdata_len)
91{
92 BIO *out = BIO_new(BIO_s_mem());
93 BUF_MEM *buf = NULL;
94 int ret = 0;
95
96 if (pdata_len == NULL) {
b9a2afdf 97 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
25cf949f
RL
98 return 0;
99 }
100
101 if (OSSL_ENCODER_to_bio(ctx, out)
102 && BIO_get_mem_ptr(out, &buf) > 0) {
103 ret = 1; /* Hope for the best. A too small buffer will clear this */
104
105 if (pdata != NULL && *pdata != NULL) {
106 if (*pdata_len < buf->length)
107 /*
108 * It's tempting to do |*pdata_len = (size_t)buf->length|
109 * However, it's believed to be confusing more than helpful,
110 * so we don't.
111 */
112 ret = 0;
113 else
114 *pdata_len -= buf->length;
115 } else {
116 /* The buffer with the right size is already allocated for us */
117 *pdata_len = (size_t)buf->length;
118 }
119
120 if (ret) {
121 if (pdata != NULL) {
122 if (*pdata != NULL) {
123 memcpy(*pdata, buf->data, buf->length);
124 *pdata += buf->length;
125 } else {
126 /* In this case, we steal the data from BIO_s_mem() */
127 *pdata = (unsigned char *)buf->data;
128 buf->data = NULL;
129 }
130 }
131 }
132 }
133 BIO_free(out);
134 return ret;
135}
136
8a98a507
RL
137int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)
138{
139 if (!ossl_assert(ctx != NULL)) {
140 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
141 return 0;
142 }
143
144 if (!ossl_assert(selection != 0)) {
145 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
146 return 0;
147 }
148
149 ctx->selection = selection;
150 return 1;
151}
152
b8975c68
RL
153int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
154 const char *output_type)
155{
156 if (!ossl_assert(ctx != NULL) || !ossl_assert(output_type != NULL)) {
157 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
158 return 0;
159 }
160
161 ctx->output_type = output_type;
162 return 1;
163}
164
8a98a507
RL
165int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,
166 const char *output_structure)
b8975c68 167{
8a98a507 168 if (!ossl_assert(ctx != NULL) || !ossl_assert(output_structure != NULL)) {
b8975c68
RL
169 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
170 return 0;
171 }
172
8a98a507 173 ctx->output_structure = output_structure;
b8975c68
RL
174 return 1;
175}
176
177static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder,
178 void *encoderctx)
179{
180 OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
8a98a507 181 OSSL_PARAM params[4];
b8975c68
RL
182
183 if (!ossl_assert(encoder != NULL)) {
184 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
185 return 0;
186 }
187
188 if (encoder->get_params == NULL) {
189 ERR_raise(ERR_LIB_OSSL_ENCODER,
190 OSSL_ENCODER_R_MISSING_GET_PARAMS);
191 return 0;
192 }
193
194 if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL) {
195 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
196 return 0;
197 }
198
199 /*
200 * Cache the input and output types for this encoder. The output type
201 * is mandatory.
202 */
203 params[0] =
204 OSSL_PARAM_construct_utf8_ptr(OSSL_ENCODER_PARAM_OUTPUT_TYPE,
205 (char **)&encoder_inst->output_type, 0);
206 params[1] =
8a98a507
RL
207 OSSL_PARAM_construct_utf8_ptr(OSSL_ENCODER_PARAM_OUTPUT_STRUCTURE,
208 (char **)&encoder_inst->output_structure,
209 0);
210 params[2] =
b8975c68
RL
211 OSSL_PARAM_construct_utf8_ptr(OSSL_ENCODER_PARAM_INPUT_TYPE,
212 (char **)&encoder_inst->input_type, 0);
8a98a507 213 params[3] = OSSL_PARAM_construct_end();
b8975c68
RL
214
215 if (!encoder->get_params(params)
8a98a507 216 || !OSSL_PARAM_modified(&params[0]))
b8975c68
RL
217 goto err;
218
219 if (!OSSL_ENCODER_up_ref(encoder)) {
220 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
221 goto err;
222 }
223
224 encoder_inst->encoder = encoder;
225 encoder_inst->encoderctx = encoderctx;
226 return encoder_inst;
227 err:
228 ossl_encoder_instance_free(encoder_inst);
229 return NULL;
230}
231
232void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
233{
234 if (encoder_inst != NULL) {
235 if (encoder_inst->encoder != NULL)
236 encoder_inst->encoder->freectx(encoder_inst->encoderctx);
237 encoder_inst->encoderctx = NULL;
238 OSSL_ENCODER_free(encoder_inst->encoder);
239 encoder_inst->encoder = NULL;
240 OPENSSL_free(encoder_inst);
241 }
242}
243
244static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
245 OSSL_ENCODER_INSTANCE *ei)
246{
0b9f90f5
RL
247 int ok;
248
b8975c68
RL
249 if (ctx->encoder_insts == NULL
250 && (ctx->encoder_insts =
251 sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) {
252 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
253 return 0;
254 }
255
0b9f90f5
RL
256 ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
257 if (ok) {
258 OSSL_TRACE_BEGIN(ENCODER) {
259 BIO_printf(trc_out,
260 "(ctx %p) Added encoder instance %p (encoder %p) with:\n",
261 (void *)ctx, (void *)ei, (void *)ei->encoder);
262 BIO_printf(trc_out,
263 " output type: %s, output structure: %s, input type :%s\n",
264 ei->output_type, ei->output_structure, ei->input_type);
265 } OSSL_TRACE_END(ENCODER);
266 }
267 return ok;
b8975c68
RL
268}
269
270int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
271{
272 OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
273 const OSSL_PROVIDER *prov = NULL;
274 void *encoderctx = NULL;
275 void *provctx = NULL;
276
277 if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) {
278 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
279 return 0;
280 }
281
ed576acd 282 prov = OSSL_ENCODER_get0_provider(encoder);
b8975c68
RL
283 provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
284
285 if ((encoderctx = encoder->newctx(provctx)) == NULL
286 || (encoder_inst =
287 ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
288 goto err;
289 /* Avoid double free of encoderctx on further errors */
290 encoderctx = NULL;
291
292 if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
293 goto err;
294
295 return 1;
296 err:
297 ossl_encoder_instance_free(encoder_inst);
298 if (encoderctx != NULL)
299 encoder->freectx(encoderctx);
300 return 0;
301}
302
303int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
b4250010 304 OSSL_LIB_CTX *libctx, const char *propq)
b8975c68
RL
305{
306 return 1;
307}
308
309int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
310{
311 if (ctx == NULL || ctx->encoder_insts == NULL)
312 return 0;
313 return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
314}
315
316int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
317 OSSL_ENCODER_CONSTRUCT *construct)
318{
319 if (!ossl_assert(ctx != NULL)) {
320 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
321 return 0;
322 }
323 ctx->construct = construct;
324 return 1;
325}
326
327int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
328 void *construct_data)
329{
330 if (!ossl_assert(ctx != NULL)) {
331 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
332 return 0;
333 }
334 ctx->construct_data = construct_data;
335 return 1;
336}
337
338int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
339 OSSL_ENCODER_CLEANUP *cleanup)
340{
341 if (!ossl_assert(ctx != NULL)) {
342 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
343 return 0;
344 }
345 ctx->cleanup = cleanup;
346 return 1;
347}
348
349OSSL_ENCODER *
350OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
351{
352 if (encoder_inst == NULL)
353 return NULL;
354 return encoder_inst->encoder;
355}
356
357void *
358OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
359{
360 if (encoder_inst == NULL)
361 return NULL;
362 return encoder_inst->encoderctx;
363}
364
365const char *
366OSSL_ENCODER_INSTANCE_get_input_type(OSSL_ENCODER_INSTANCE *encoder_inst)
367{
368 if (encoder_inst == NULL)
369 return NULL;
370 return encoder_inst->input_type;
371}
372
373const char *
374OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
375{
376 if (encoder_inst == NULL)
377 return NULL;
378 return encoder_inst->output_type;
379}
380
8a98a507
RL
381const char *
382OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
383{
384 if (encoder_inst == NULL)
385 return NULL;
386 return encoder_inst->output_structure;
387}
388
b9a2afdf 389static int encoder_process(struct encoder_process_data_st *data)
b8975c68 390{
b9a2afdf
RL
391 OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;
392 OSSL_ENCODER *current_encoder = NULL;
393 OSSL_ENCODER_CTX *current_encoder_ctx = NULL;
394 BIO *allocated_out = NULL;
395 const void *original_data = NULL;
396 OSSL_PARAM abstract[10];
397 const OSSL_PARAM *current_abstract = NULL;
398 int i;
399 int ok = -1; /* -1 signifies that the lookup loop gave nothing */
400 int top = 0;
401
402 if (data->next_encoder_inst == NULL) {
403 /* First iteration, where we prepare for what is to come */
404
405 data->count_output_structure =
406 data->ctx->output_structure == NULL ? -1 : 0;
407 top = 1;
408 }
b8975c68 409
b9a2afdf
RL
410 for (i = data->current_encoder_inst_index; i-- > 0;) {
411 OSSL_ENCODER *next_encoder = NULL;
412 const char *current_output_type;
413 const char *current_output_structure;
414 struct encoder_process_data_st new_data;
415
416 if (!top)
417 next_encoder =
418 OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
419
420 current_encoder_inst =
421 sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
422 current_encoder =
423 OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);
424 current_encoder_ctx =
425 OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);
426 current_output_type =
427 OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);
428 current_output_structure =
429 OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);
430 memset(&new_data, 0, sizeof(new_data));
431 new_data.ctx = data->ctx;
432 new_data.current_encoder_inst_index = i;
433 new_data.next_encoder_inst = current_encoder_inst;
434 new_data.count_output_structure = data->count_output_structure;
435 new_data.level = data->level + 1;
b8975c68 436
0b9f90f5
RL
437 OSSL_TRACE_BEGIN(ENCODER) {
438 BIO_printf(trc_out,
439 "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n",
440 data->level, (void *)data->ctx,
441 (void *)current_encoder_inst, (void *)current_encoder);
442 } OSSL_TRACE_END(ENCODER);
443
b9a2afdf
RL
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 if (top) {
452 if (data->ctx->output_type != NULL
453 && strcasecmp(current_output_type,
0b9f90f5
RL
454 data->ctx->output_type) != 0) {
455 OSSL_TRACE_BEGIN(ENCODER) {
456 BIO_printf(trc_out,
457 "[%d] Skipping because current encoder output type (%s) != desired output type (%s)\n",
458 data->level,
459 current_output_type, data->ctx->output_type);
460 } OSSL_TRACE_END(ENCODER);
b9a2afdf 461 continue;
0b9f90f5 462 }
b8975c68 463 } else {
0b9f90f5
RL
464 if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
465 OSSL_TRACE_BEGIN(ENCODER) {
466 BIO_printf(trc_out,
467 "[%d] Skipping because current encoder output type (%s) != name of encoder %p\n",
468 data->level,
469 current_output_type, (void *)next_encoder);
470 } OSSL_TRACE_END(ENCODER);
b8975c68 471 continue;
0b9f90f5 472 }
b9a2afdf
RL
473 }
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 if (data->ctx->output_structure != NULL
481 && current_output_structure != NULL) {
482 if (strcasecmp(data->ctx->output_structure,
0b9f90f5
RL
483 current_output_structure) != 0) {
484 OSSL_TRACE_BEGIN(ENCODER) {
485 BIO_printf(trc_out,
486 "[%d] Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
487 data->level,
488 current_output_structure,
489 data->ctx->output_structure);
490 } OSSL_TRACE_END(ENCODER);
b9a2afdf 491 continue;
0b9f90f5 492 }
b9a2afdf
RL
493
494 data->count_output_structure++;
495 }
496
497 /*
498 * Recurse to process the encoder implementations before the current
499 * one.
500 */
501 ok = encoder_process(&new_data);
502
503 data->prev_encoder_inst = new_data.prev_encoder_inst;
504 data->running_output = new_data.running_output;
505 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 if (ok != 0)
519 break;
0b9f90f5
RL
520
521 OSSL_TRACE_BEGIN(ENCODER) {
522 BIO_printf(trc_out,
523 "[%d] Skipping because recusion level %d failed\n",
524 data->level, new_data.level);
525 } OSSL_TRACE_END(ENCODER);
b9a2afdf 526 }
b8975c68 527
b9a2afdf
RL
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 if (i < 0) {
533 ok = -1;
0b9f90f5
RL
534
535 OSSL_TRACE_BEGIN(ENCODER) {
536 BIO_printf(trc_out,
537 "[%d] (ctx %p) No suitable encoder found\n",
538 data->level, (void *)data->ctx);
539 } OSSL_TRACE_END(ENCODER);
b9a2afdf
RL
540 } else {
541 /* Preparations */
542
543 switch (ok) {
544 case 0:
545 break;
546 case -1:
b8975c68 547 /*
b9a2afdf
RL
548 * We have reached the beginning of the encoder instance sequence,
549 * so we prepare the object to be encoded.
b8975c68 550 */
b8975c68
RL
551
552 /*
b9a2afdf
RL
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
b8975c68 561 */
b9a2afdf
RL
562 if (data->count_output_structure == 0)
563 return 0;
b8975c68 564
b9a2afdf
RL
565 original_data =
566 data->ctx->construct(current_encoder_inst,
567 data->ctx->construct_data);
568
569 /* Assume that the constructor recorded an error */
570 if (original_data != NULL)
571 ok = 1;
572 else
573 ok = 0;
574 break;
575 case 1:
576 if (!ossl_assert(data->running_output != NULL)) {
577 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
578 ok = 0;
579 break;
580 }
b8975c68 581
b9a2afdf
RL
582 {
583 /*
584 * Create an object abstraction from the latest output, which
585 * was stolen from the previous round.
586 */
b8975c68 587
b9a2afdf
RL
588 OSSL_PARAM *abstract_p = abstract;
589 const char *prev_input_type =
590 OSSL_ENCODER_INSTANCE_get_input_type(data->prev_encoder_inst);
591 const char *prev_output_structure =
592 OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);
593
594 if (prev_input_type != NULL)
595 *abstract_p++ =
596 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
597 (char *)prev_input_type, 0);
598 if (prev_output_structure != NULL)
599 *abstract_p++ =
600 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
601 (char *)prev_output_structure,
602 0);
603 *abstract_p++ =
604 OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
605 data->running_output,
606 data->running_output_length);
607 *abstract_p = OSSL_PARAM_construct_end();
608 current_abstract = abstract;
609 }
610 break;
611 }
b8975c68 612
b9a2afdf 613 /* Calling the encoder implementation */
b8975c68 614
b9a2afdf 615 if (ok) {
141cc94e 616 OSSL_CORE_BIO *cbio = NULL;
b9a2afdf 617 BIO *current_out = NULL;
b8975c68 618
b9a2afdf
RL
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 if (top)
624 current_out = data->bio;
625 else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))
626 == NULL)
627 ok = 0; /* Assume BIO_new() recorded an error */
628
141cc94e
P
629 if (ok)
630 ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
0b9f90f5 631 if (ok) {
141cc94e 632 ok = current_encoder->encode(current_encoder_ctx, cbio,
b9a2afdf
RL
633 original_data, current_abstract,
634 data->ctx->selection,
635 ossl_pw_passphrase_callback_enc,
636 &data->ctx->pwdata);
0b9f90f5
RL
637 OSSL_TRACE_BEGIN(ENCODER) {
638 BIO_printf(trc_out,
639 "[%d] (ctx %p) Running encoder instance %p => %d\n",
640 data->level, (void *)data->ctx,
641 (void *)current_encoder_inst, ok);
642 } OSSL_TRACE_END(ENCODER);
643 }
b9a2afdf 644
141cc94e 645 ossl_core_bio_free(cbio);
b9a2afdf 646 data->prev_encoder_inst = current_encoder_inst;
b8975c68 647 }
b9a2afdf 648 }
b8975c68 649
b9a2afdf 650 /* Cleanup and collecting the result */
8a98a507 651
b9a2afdf
RL
652 OPENSSL_free(data->running_output);
653 data->running_output = NULL;
b8975c68 654
b9a2afdf
RL
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 if (allocated_out != NULL) {
660 BUF_MEM *buf;
661
662 BIO_get_mem_ptr(allocated_out, &buf);
663 data->running_output = (unsigned char *)buf->data;
664 data->running_output_length = buf->length;
665 memset(buf, 0, sizeof(*buf));
b8975c68
RL
666 }
667
b9a2afdf
RL
668 BIO_free(allocated_out);
669 if (original_data != NULL)
670 data->ctx->cleanup(data->ctx->construct_data);
b8975c68
RL
671 return ok;
672}