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