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