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 "internal/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 "internal/provider.h"
20 #include "encoder_local.h"
22 struct encoder_process_data_st {
23 OSSL_ENCODER_CTX *ctx;
28 /* Index of the current encoder instance to be processed */
29 int current_encoder_inst_index;
31 /* Processing data passed down through recursion */
32 int level; /* Recursion level */
33 OSSL_ENCODER_INSTANCE *next_encoder_inst;
34 int count_output_structure;
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;
40 /* Data type = the name of the first succeeding encoder implementation */
41 const char *data_type;
44 static int encoder_process(struct encoder_process_data_st *data);
46 int OSSL_ENCODER_to_bio(OSSL_ENCODER_CTX *ctx, BIO *out)
48 struct encoder_process_data_st data;
50 memset(&data, 0, sizeof(data));
53 data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx);
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?");
63 return encoder_process(&data) > 0;
66 #ifndef OPENSSL_NO_STDIO
67 static BIO *bio_from_file(FILE *fp)
71 if ((b = BIO_new(BIO_s_file())) == NULL) {
72 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_BUF_LIB);
75 BIO_set_fp(b, fp, BIO_NOCLOSE);
79 int OSSL_ENCODER_to_fp(OSSL_ENCODER_CTX *ctx, FILE *fp)
81 BIO *b = bio_from_file(fp);
85 ret = OSSL_ENCODER_to_bio(ctx, b);
92 int OSSL_ENCODER_to_data(OSSL_ENCODER_CTX *ctx, unsigned char **pdata,
99 if (pdata_len == NULL) {
100 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
104 out = BIO_new(BIO_s_mem());
107 && OSSL_ENCODER_to_bio(ctx, out)
108 && BIO_get_mem_ptr(out, &buf) > 0) {
109 ret = 1; /* Hope for the best. A too small buffer will clear this */
111 if (pdata != NULL && *pdata != NULL) {
112 if (*pdata_len < buf->length)
114 * It's tempting to do |*pdata_len = (size_t)buf->length|
115 * However, it's believed to be confusing more than helpful,
120 *pdata_len -= buf->length;
122 /* The buffer with the right size is already allocated for us */
123 *pdata_len = (size_t)buf->length;
128 if (*pdata != NULL) {
129 memcpy(*pdata, buf->data, buf->length);
130 *pdata += buf->length;
132 /* In this case, we steal the data from BIO_s_mem() */
133 *pdata = (unsigned char *)buf->data;
143 int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)
145 if (!ossl_assert(ctx != NULL)) {
146 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
150 if (!ossl_assert(selection != 0)) {
151 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
155 ctx->selection = selection;
159 int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
160 const char *output_type)
162 if (!ossl_assert(ctx != NULL) || !ossl_assert(output_type != NULL)) {
163 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
167 ctx->output_type = output_type;
171 int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,
172 const char *output_structure)
174 if (!ossl_assert(ctx != NULL) || !ossl_assert(output_structure != NULL)) {
175 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
179 ctx->output_structure = output_structure;
183 static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder,
186 OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
187 const OSSL_PROVIDER *prov;
188 OSSL_LIB_CTX *libctx;
189 const OSSL_PROPERTY_LIST *props;
190 const OSSL_PROPERTY_DEFINITION *prop;
192 if (!ossl_assert(encoder != NULL)) {
193 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
197 if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL) {
198 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
202 if (!OSSL_ENCODER_up_ref(encoder)) {
203 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
207 prov = OSSL_ENCODER_get0_provider(encoder);
208 libctx = ossl_provider_libctx(prov);
209 props = ossl_encoder_parsed_properties(encoder);
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));
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));
229 /* The "structure" property is optional */
230 prop = ossl_property_find_property(props, libctx, "structure");
232 encoder_inst->output_structure
233 = ossl_property_get_string_value(libctx, prop);
235 encoder_inst->encoder = encoder;
236 encoder_inst->encoderctx = encoderctx;
239 ossl_encoder_instance_free(encoder_inst);
243 void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
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);
255 static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
256 OSSL_ENCODER_INSTANCE *ei)
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);
267 ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
269 OSSL_TRACE_BEGIN(ENCODER) {
271 "(ctx %p) Added encoder instance %p (encoder %p):\n"
273 (void *)ctx, (void *)ei, (void *)ei->encoder,
274 OSSL_ENCODER_get0_name(ei->encoder),
275 OSSL_ENCODER_get0_properties(ei->encoder));
276 } OSSL_TRACE_END(ENCODER);
281 int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
283 OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
284 const OSSL_PROVIDER *prov = NULL;
285 void *encoderctx = NULL;
286 void *provctx = NULL;
288 if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) {
289 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
293 prov = OSSL_ENCODER_get0_provider(encoder);
294 provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
296 if ((encoderctx = encoder->newctx(provctx)) == NULL
298 ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
300 /* Avoid double free of encoderctx on further errors */
303 if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
308 ossl_encoder_instance_free(encoder_inst);
309 if (encoderctx != NULL)
310 encoder->freectx(encoderctx);
314 int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
315 OSSL_LIB_CTX *libctx, const char *propq)
320 int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
322 if (ctx == NULL || ctx->encoder_insts == NULL)
324 return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
327 int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
328 OSSL_ENCODER_CONSTRUCT *construct)
330 if (!ossl_assert(ctx != NULL)) {
331 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
334 ctx->construct = construct;
338 int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
339 void *construct_data)
341 if (!ossl_assert(ctx != NULL)) {
342 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
345 ctx->construct_data = construct_data;
349 int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
350 OSSL_ENCODER_CLEANUP *cleanup)
352 if (!ossl_assert(ctx != NULL)) {
353 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
356 ctx->cleanup = cleanup;
361 OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
363 if (encoder_inst == NULL)
365 return encoder_inst->encoder;
369 OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
371 if (encoder_inst == NULL)
373 return encoder_inst->encoderctx;
377 OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
379 if (encoder_inst == NULL)
381 return encoder_inst->output_type;
385 OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
387 if (encoder_inst == NULL)
389 return encoder_inst->output_structure;
392 static int encoder_process(struct encoder_process_data_st *data)
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;
402 int ok = -1; /* -1 signifies that the lookup loop gave nothing */
405 if (data->next_encoder_inst == NULL) {
406 /* First iteration, where we prepare for what is to come */
408 data->count_output_structure =
409 data->ctx->output_structure == NULL ? -1 : 0;
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;
421 OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
423 current_encoder_inst =
424 sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
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;
440 OSSL_TRACE_BEGIN(ENCODER) {
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);
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).
455 if (data->ctx->output_type != NULL
456 && strcasecmp(current_output_type,
457 data->ctx->output_type) != 0) {
458 OSSL_TRACE_BEGIN(ENCODER) {
460 "[%d] Skipping because current encoder output type (%s) != desired output type (%s)\n",
462 current_output_type, data->ctx->output_type);
463 } OSSL_TRACE_END(ENCODER);
467 if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
468 OSSL_TRACE_BEGIN(ENCODER) {
470 "[%d] Skipping because current encoder output type (%s) != name of encoder %p\n",
472 current_output_type, (void *)next_encoder);
473 } OSSL_TRACE_END(ENCODER);
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.
483 if (data->ctx->output_structure != NULL
484 && current_output_structure != NULL) {
485 if (strcasecmp(data->ctx->output_structure,
486 current_output_structure) != 0) {
487 OSSL_TRACE_BEGIN(ENCODER) {
489 "[%d] Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
491 current_output_structure,
492 data->ctx->output_structure);
493 } OSSL_TRACE_END(ENCODER);
497 data->count_output_structure++;
501 * Recurse to process the encoder implementations before the current
504 ok = encoder_process(&new_data);
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;
511 * ok == -1 means that the recursion call above gave no further
512 * encoders, and that the one we're currently at should
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.
524 OSSL_TRACE_BEGIN(ENCODER) {
526 "[%d] Skipping because recursion level %d failed\n",
527 data->level, new_data.level);
528 } OSSL_TRACE_END(ENCODER);
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|.
538 OSSL_TRACE_BEGIN(ENCODER) {
540 "[%d] (ctx %p) No suitable encoder found\n",
541 data->level, (void *)data->ctx);
542 } OSSL_TRACE_END(ENCODER);
551 * We have reached the beginning of the encoder instance sequence,
552 * so we prepare the object to be encoded.
556 * |data->count_output_structure| is one of these values:
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
562 * >0 There is a desired output structure, and at least one
563 * of the encoder instances matched it
565 if (data->count_output_structure == 0)
569 data->ctx->construct(current_encoder_inst,
570 data->ctx->construct_data);
572 /* Also set the data type, using the encoder implementation name */
573 data->data_type = OSSL_ENCODER_get0_name(current_encoder);
575 /* Assume that the constructor recorded an error */
576 if (original_data != NULL)
582 if (!ossl_assert(data->running_output != NULL)) {
583 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
590 * Create an object abstraction from the latest output, which
591 * was stolen from the previous round.
594 OSSL_PARAM *abstract_p = abstract;
595 const char *prev_output_structure =
596 OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);
599 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
600 (char *)data->data_type, 0);
601 if (prev_output_structure != NULL)
603 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
604 (char *)prev_output_structure,
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;
616 /* Calling the encoder implementation */
619 OSSL_CORE_BIO *cbio = NULL;
620 BIO *current_out = NULL;
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.
627 current_out = data->bio;
628 else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))
630 ok = 0; /* Assume BIO_new() recorded an error */
633 ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
635 ok = current_encoder->encode(current_encoder_ctx, cbio,
636 original_data, current_abstract,
637 data->ctx->selection,
638 ossl_pw_passphrase_callback_enc,
640 OSSL_TRACE_BEGIN(ENCODER) {
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);
648 ossl_core_bio_free(cbio);
649 data->prev_encoder_inst = current_encoder_inst;
653 /* Cleanup and collecting the result */
655 OPENSSL_free(data->running_output);
656 data->running_output = NULL;
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.
662 if (allocated_out != NULL) {
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));
671 BIO_free(allocated_out);
672 if (original_data != NULL)
673 data->ctx->cleanup(data->ctx->construct_data);