2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "e_os.h" /* strcasecmp on Windows */
11 #include <openssl/core_names.h>
12 #include <openssl/bio.h>
13 #include <openssl/encoder.h>
14 #include <openssl/buffer.h>
15 #include <openssl/params.h>
16 #include <openssl/provider.h>
17 #include <openssl/trace.h>
18 #include "internal/bio.h"
19 #include "encoder_local.h"
21 struct encoder_process_data_st {
22 OSSL_ENCODER_CTX *ctx;
27 /* Index of the current encoder instance to be processed */
28 int current_encoder_inst_index;
30 /* Processing data passed down through recursion */
31 int level; /* Recursion level */
32 OSSL_ENCODER_INSTANCE *next_encoder_inst;
33 int count_output_structure;
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;
41 static int encoder_process(struct encoder_process_data_st *data);
43 int OSSL_ENCODER_to_bio(OSSL_ENCODER_CTX *ctx, BIO *out)
45 struct encoder_process_data_st data;
47 memset(&data, 0, sizeof(data));
50 data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx);
52 return encoder_process(&data) > 0;
55 #ifndef OPENSSL_NO_STDIO
56 static BIO *bio_from_file(FILE *fp)
60 if ((b = BIO_new(BIO_s_file())) == NULL) {
61 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_BUF_LIB);
64 BIO_set_fp(b, fp, BIO_NOCLOSE);
68 int OSSL_ENCODER_to_fp(OSSL_ENCODER_CTX *ctx, FILE *fp)
70 BIO *b = bio_from_file(fp);
74 ret = OSSL_ENCODER_to_bio(ctx, b);
81 int OSSL_ENCODER_to_data(OSSL_ENCODER_CTX *ctx, unsigned char **pdata,
84 BIO *out = BIO_new(BIO_s_mem());
88 if (pdata_len == NULL) {
89 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
93 if (OSSL_ENCODER_to_bio(ctx, out)
94 && BIO_get_mem_ptr(out, &buf) > 0) {
95 ret = 1; /* Hope for the best. A too small buffer will clear this */
97 if (pdata != NULL && *pdata != NULL) {
98 if (*pdata_len < buf->length)
100 * It's tempting to do |*pdata_len = (size_t)buf->length|
101 * However, it's believed to be confusing more than helpful,
106 *pdata_len -= buf->length;
108 /* The buffer with the right size is already allocated for us */
109 *pdata_len = (size_t)buf->length;
114 if (*pdata != NULL) {
115 memcpy(*pdata, buf->data, buf->length);
116 *pdata += buf->length;
118 /* In this case, we steal the data from BIO_s_mem() */
119 *pdata = (unsigned char *)buf->data;
129 int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)
131 if (!ossl_assert(ctx != NULL)) {
132 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
136 if (!ossl_assert(selection != 0)) {
137 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
141 ctx->selection = selection;
145 int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
146 const char *output_type)
148 if (!ossl_assert(ctx != NULL) || !ossl_assert(output_type != NULL)) {
149 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
153 ctx->output_type = output_type;
157 int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,
158 const char *output_structure)
160 if (!ossl_assert(ctx != NULL) || !ossl_assert(output_structure != NULL)) {
161 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
165 ctx->output_structure = output_structure;
169 static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder,
172 OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
173 OSSL_PARAM params[4];
175 if (!ossl_assert(encoder != NULL)) {
176 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
180 if (encoder->get_params == NULL) {
181 ERR_raise(ERR_LIB_OSSL_ENCODER,
182 OSSL_ENCODER_R_MISSING_GET_PARAMS);
186 if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL) {
187 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
192 * Cache the input and output types for this encoder. The output type
196 OSSL_PARAM_construct_utf8_ptr(OSSL_ENCODER_PARAM_OUTPUT_TYPE,
197 (char **)&encoder_inst->output_type, 0);
199 OSSL_PARAM_construct_utf8_ptr(OSSL_ENCODER_PARAM_OUTPUT_STRUCTURE,
200 (char **)&encoder_inst->output_structure,
203 OSSL_PARAM_construct_utf8_ptr(OSSL_ENCODER_PARAM_INPUT_TYPE,
204 (char **)&encoder_inst->input_type, 0);
205 params[3] = OSSL_PARAM_construct_end();
207 if (!encoder->get_params(params)
208 || !OSSL_PARAM_modified(¶ms[0]))
211 if (!OSSL_ENCODER_up_ref(encoder)) {
212 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
216 encoder_inst->encoder = encoder;
217 encoder_inst->encoderctx = encoderctx;
220 ossl_encoder_instance_free(encoder_inst);
224 void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
226 if (encoder_inst != NULL) {
227 if (encoder_inst->encoder != NULL)
228 encoder_inst->encoder->freectx(encoder_inst->encoderctx);
229 encoder_inst->encoderctx = NULL;
230 OSSL_ENCODER_free(encoder_inst->encoder);
231 encoder_inst->encoder = NULL;
232 OPENSSL_free(encoder_inst);
236 static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
237 OSSL_ENCODER_INSTANCE *ei)
241 if (ctx->encoder_insts == NULL
242 && (ctx->encoder_insts =
243 sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) {
244 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
248 ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
250 OSSL_TRACE_BEGIN(ENCODER) {
252 "(ctx %p) Added encoder instance %p (encoder %p) with:\n",
253 (void *)ctx, (void *)ei, (void *)ei->encoder);
255 " output type: %s, output structure: %s, input type :%s\n",
256 ei->output_type, ei->output_structure, ei->input_type);
257 } OSSL_TRACE_END(ENCODER);
262 int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
264 OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
265 const OSSL_PROVIDER *prov = NULL;
266 void *encoderctx = NULL;
267 void *provctx = NULL;
269 if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) {
270 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
274 prov = OSSL_ENCODER_provider(encoder);
275 provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
277 if ((encoderctx = encoder->newctx(provctx)) == NULL
279 ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
281 /* Avoid double free of encoderctx on further errors */
284 if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
289 ossl_encoder_instance_free(encoder_inst);
290 if (encoderctx != NULL)
291 encoder->freectx(encoderctx);
295 int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
296 OSSL_LIB_CTX *libctx, const char *propq)
301 int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
303 if (ctx == NULL || ctx->encoder_insts == NULL)
305 return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
308 int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
309 OSSL_ENCODER_CONSTRUCT *construct)
311 if (!ossl_assert(ctx != NULL)) {
312 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
315 ctx->construct = construct;
319 int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
320 void *construct_data)
322 if (!ossl_assert(ctx != NULL)) {
323 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
326 ctx->construct_data = construct_data;
330 int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
331 OSSL_ENCODER_CLEANUP *cleanup)
333 if (!ossl_assert(ctx != NULL)) {
334 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
337 ctx->cleanup = cleanup;
342 OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
344 if (encoder_inst == NULL)
346 return encoder_inst->encoder;
350 OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
352 if (encoder_inst == NULL)
354 return encoder_inst->encoderctx;
358 OSSL_ENCODER_INSTANCE_get_input_type(OSSL_ENCODER_INSTANCE *encoder_inst)
360 if (encoder_inst == NULL)
362 return encoder_inst->input_type;
366 OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
368 if (encoder_inst == NULL)
370 return encoder_inst->output_type;
374 OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
376 if (encoder_inst == NULL)
378 return encoder_inst->output_structure;
381 static int encoder_process(struct encoder_process_data_st *data)
383 OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;
384 OSSL_ENCODER *current_encoder = NULL;
385 OSSL_ENCODER_CTX *current_encoder_ctx = NULL;
386 BIO *allocated_out = NULL;
387 const void *original_data = NULL;
388 OSSL_PARAM abstract[10];
389 const OSSL_PARAM *current_abstract = NULL;
391 int ok = -1; /* -1 signifies that the lookup loop gave nothing */
394 if (data->next_encoder_inst == NULL) {
395 /* First iteration, where we prepare for what is to come */
397 data->count_output_structure =
398 data->ctx->output_structure == NULL ? -1 : 0;
402 for (i = data->current_encoder_inst_index; i-- > 0;) {
403 OSSL_ENCODER *next_encoder = NULL;
404 const char *current_output_type;
405 const char *current_output_structure;
406 struct encoder_process_data_st new_data;
410 OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
412 current_encoder_inst =
413 sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
415 OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);
416 current_encoder_ctx =
417 OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);
418 current_output_type =
419 OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);
420 current_output_structure =
421 OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);
422 memset(&new_data, 0, sizeof(new_data));
423 new_data.ctx = data->ctx;
424 new_data.current_encoder_inst_index = i;
425 new_data.next_encoder_inst = current_encoder_inst;
426 new_data.count_output_structure = data->count_output_structure;
427 new_data.level = data->level + 1;
429 OSSL_TRACE_BEGIN(ENCODER) {
431 "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n",
432 data->level, (void *)data->ctx,
433 (void *)current_encoder_inst, (void *)current_encoder);
434 } OSSL_TRACE_END(ENCODER);
437 * If this is the top call, we check if the output type of the current
438 * encoder matches the desired output type.
439 * If this isn't the top call, i.e. this is deeper in the recursion,
440 * we instead check if the output type of the current encoder matches
441 * the name of the next encoder (the one found by the parent call).
444 if (data->ctx->output_type != NULL
445 && strcasecmp(current_output_type,
446 data->ctx->output_type) != 0) {
447 OSSL_TRACE_BEGIN(ENCODER) {
449 "[%d] Skipping because current encoder output type (%s) != desired output type (%s)\n",
451 current_output_type, data->ctx->output_type);
452 } OSSL_TRACE_END(ENCODER);
456 if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
457 OSSL_TRACE_BEGIN(ENCODER) {
459 "[%d] Skipping because current encoder output type (%s) != name of encoder %p\n",
461 current_output_type, (void *)next_encoder);
462 } OSSL_TRACE_END(ENCODER);
468 * If the caller and the current encoder specify an output structure,
469 * Check if they match. If they do, count the match, otherwise skip
470 * the current encoder.
472 if (data->ctx->output_structure != NULL
473 && current_output_structure != NULL) {
474 if (strcasecmp(data->ctx->output_structure,
475 current_output_structure) != 0) {
476 OSSL_TRACE_BEGIN(ENCODER) {
478 "[%d] Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
480 current_output_structure,
481 data->ctx->output_structure);
482 } OSSL_TRACE_END(ENCODER);
486 data->count_output_structure++;
490 * Recurse to process the encoder implementations before the current
493 ok = encoder_process(&new_data);
495 data->prev_encoder_inst = new_data.prev_encoder_inst;
496 data->running_output = new_data.running_output;
497 data->running_output_length = new_data.running_output_length;
500 * ok == -1 means that the recursion call above gave no further
501 * encoders, and that the one we're currently at should
503 * ok == 0 means that something failed in the recursion call
504 * above, making the result unsuitable for a chain.
505 * In this case, we simply continue to try finding a
506 * suitable encoder at this recursion level.
507 * ok == 1 means that the recursion call was successful, and we
508 * try to use the result at this recursion level.
513 OSSL_TRACE_BEGIN(ENCODER) {
515 "[%d] Skipping because recusion level %d failed\n",
516 data->level, new_data.level);
517 } OSSL_TRACE_END(ENCODER);
521 * If |i < 0|, we didn't find any useful encoder in this recursion, so
522 * we do the rest of the process only if |i >= 0|.
527 OSSL_TRACE_BEGIN(ENCODER) {
529 "[%d] (ctx %p) No suitable encoder found\n",
530 data->level, (void *)data->ctx);
531 } OSSL_TRACE_END(ENCODER);
540 * We have reached the beginning of the encoder instance sequence,
541 * so we prepare the object to be encoded.
545 * |data->count_output_structure| is one of these values:
547 * -1 There is no desired output structure
548 * 0 There is a desired output structure, and it wasn't
549 * matched by any of the encoder instances that were
551 * >0 There is a desired output structure, and at least one
552 * of the encoder instances matched it
554 if (data->count_output_structure == 0)
558 data->ctx->construct(current_encoder_inst,
559 data->ctx->construct_data);
561 /* Assume that the constructor recorded an error */
562 if (original_data != NULL)
568 if (!ossl_assert(data->running_output != NULL)) {
569 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
576 * Create an object abstraction from the latest output, which
577 * was stolen from the previous round.
580 OSSL_PARAM *abstract_p = abstract;
581 const char *prev_input_type =
582 OSSL_ENCODER_INSTANCE_get_input_type(data->prev_encoder_inst);
583 const char *prev_output_structure =
584 OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);
586 if (prev_input_type != NULL)
588 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
589 (char *)prev_input_type, 0);
590 if (prev_output_structure != NULL)
592 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
593 (char *)prev_output_structure,
596 OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
597 data->running_output,
598 data->running_output_length);
599 *abstract_p = OSSL_PARAM_construct_end();
600 current_abstract = abstract;
605 /* Calling the encoder implementation */
608 OSSL_CORE_BIO *cbio = NULL;
609 BIO *current_out = NULL;
612 * If we're at the last encoder instance to use, we're setting up
613 * final output. Otherwise, set up an intermediary memory output.
616 current_out = data->bio;
617 else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))
619 ok = 0; /* Assume BIO_new() recorded an error */
622 ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
624 ok = current_encoder->encode(current_encoder_ctx, cbio,
625 original_data, current_abstract,
626 data->ctx->selection,
627 ossl_pw_passphrase_callback_enc,
629 OSSL_TRACE_BEGIN(ENCODER) {
631 "[%d] (ctx %p) Running encoder instance %p => %d\n",
632 data->level, (void *)data->ctx,
633 (void *)current_encoder_inst, ok);
634 } OSSL_TRACE_END(ENCODER);
637 ossl_core_bio_free(cbio);
638 data->prev_encoder_inst = current_encoder_inst;
642 /* Cleanup and collecting the result */
644 OPENSSL_free(data->running_output);
645 data->running_output = NULL;
648 * Steal the output from the BIO_s_mem, if we did allocate one.
649 * That'll be the data for an object abstraction in the next round.
651 if (allocated_out != NULL) {
654 BIO_get_mem_ptr(allocated_out, &buf);
655 data->running_output = (unsigned char *)buf->data;
656 data->running_output_length = buf->length;
657 memset(buf, 0, sizeof(*buf));
660 BIO_free(allocated_out);
661 if (original_data != NULL)
662 data->ctx->cleanup(data->ctx->construct_data);