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 "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,
95 BIO *out = BIO_new(BIO_s_mem());
99 if (pdata_len == NULL) {
100 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
104 if (OSSL_ENCODER_to_bio(ctx, out)
105 && BIO_get_mem_ptr(out, &buf) > 0) {
106 ret = 1; /* Hope for the best. A too small buffer will clear this */
108 if (pdata != NULL && *pdata != NULL) {
109 if (*pdata_len < buf->length)
111 * It's tempting to do |*pdata_len = (size_t)buf->length|
112 * However, it's believed to be confusing more than helpful,
117 *pdata_len -= buf->length;
119 /* The buffer with the right size is already allocated for us */
120 *pdata_len = (size_t)buf->length;
125 if (*pdata != NULL) {
126 memcpy(*pdata, buf->data, buf->length);
127 *pdata += buf->length;
129 /* In this case, we steal the data from BIO_s_mem() */
130 *pdata = (unsigned char *)buf->data;
140 int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)
142 if (!ossl_assert(ctx != NULL)) {
143 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
147 if (!ossl_assert(selection != 0)) {
148 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
152 ctx->selection = selection;
156 int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
157 const char *output_type)
159 if (!ossl_assert(ctx != NULL) || !ossl_assert(output_type != NULL)) {
160 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
164 ctx->output_type = output_type;
168 int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,
169 const char *output_structure)
171 if (!ossl_assert(ctx != NULL) || !ossl_assert(output_structure != NULL)) {
172 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
176 ctx->output_structure = output_structure;
180 static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder,
183 OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
184 const OSSL_PROVIDER *prov;
185 OSSL_LIB_CTX *libctx;
186 const OSSL_PROPERTY_LIST *props;
187 const OSSL_PROPERTY_DEFINITION *prop;
189 if (!ossl_assert(encoder != NULL)) {
190 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
194 if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL) {
195 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
199 if (!OSSL_ENCODER_up_ref(encoder)) {
200 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
204 prov = OSSL_ENCODER_get0_provider(encoder);
205 libctx = ossl_provider_libctx(prov);
206 props = ossl_encoder_parsed_properties(encoder);
208 ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
209 "there are no property definitions with encoder %s",
210 OSSL_ENCODER_get0_name(encoder));
214 /* The "output" property is mandatory */
215 prop = ossl_property_find_property(props, libctx, "output");
216 encoder_inst->output_type = ossl_property_get_string_value(libctx, prop);
217 if (encoder_inst->output_type == NULL) {
218 ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
219 "the mandatory 'output' property is missing "
220 "for encoder %s (properties: %s)",
221 OSSL_ENCODER_get0_name(encoder),
222 OSSL_ENCODER_get0_properties(encoder));
226 /* The "structure" property is optional */
227 prop = ossl_property_find_property(props, libctx, "structure");
229 encoder_inst->output_structure
230 = ossl_property_get_string_value(libctx, prop);
232 encoder_inst->encoder = encoder;
233 encoder_inst->encoderctx = encoderctx;
236 ossl_encoder_instance_free(encoder_inst);
240 void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
242 if (encoder_inst != NULL) {
243 if (encoder_inst->encoder != NULL)
244 encoder_inst->encoder->freectx(encoder_inst->encoderctx);
245 encoder_inst->encoderctx = NULL;
246 OSSL_ENCODER_free(encoder_inst->encoder);
247 encoder_inst->encoder = NULL;
248 OPENSSL_free(encoder_inst);
252 static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
253 OSSL_ENCODER_INSTANCE *ei)
257 if (ctx->encoder_insts == NULL
258 && (ctx->encoder_insts =
259 sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) {
260 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
264 ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
266 OSSL_TRACE_BEGIN(ENCODER) {
268 "(ctx %p) Added encoder instance %p (encoder %p):\n"
270 (void *)ctx, (void *)ei, (void *)ei->encoder,
271 OSSL_ENCODER_get0_name(ei->encoder),
272 OSSL_ENCODER_get0_properties(ei->encoder));
273 } OSSL_TRACE_END(ENCODER);
278 int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
280 OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
281 const OSSL_PROVIDER *prov = NULL;
282 void *encoderctx = NULL;
283 void *provctx = NULL;
285 if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) {
286 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
290 prov = OSSL_ENCODER_get0_provider(encoder);
291 provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
293 if ((encoderctx = encoder->newctx(provctx)) == NULL
295 ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
297 /* Avoid double free of encoderctx on further errors */
300 if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
305 ossl_encoder_instance_free(encoder_inst);
306 if (encoderctx != NULL)
307 encoder->freectx(encoderctx);
311 int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
312 OSSL_LIB_CTX *libctx, const char *propq)
317 int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
319 if (ctx == NULL || ctx->encoder_insts == NULL)
321 return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
324 int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
325 OSSL_ENCODER_CONSTRUCT *construct)
327 if (!ossl_assert(ctx != NULL)) {
328 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
331 ctx->construct = construct;
335 int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
336 void *construct_data)
338 if (!ossl_assert(ctx != NULL)) {
339 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
342 ctx->construct_data = construct_data;
346 int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
347 OSSL_ENCODER_CLEANUP *cleanup)
349 if (!ossl_assert(ctx != NULL)) {
350 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
353 ctx->cleanup = cleanup;
358 OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
360 if (encoder_inst == NULL)
362 return encoder_inst->encoder;
366 OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
368 if (encoder_inst == NULL)
370 return encoder_inst->encoderctx;
374 OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
376 if (encoder_inst == NULL)
378 return encoder_inst->output_type;
382 OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
384 if (encoder_inst == NULL)
386 return encoder_inst->output_structure;
389 static int encoder_process(struct encoder_process_data_st *data)
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;
399 int ok = -1; /* -1 signifies that the lookup loop gave nothing */
402 if (data->next_encoder_inst == NULL) {
403 /* First iteration, where we prepare for what is to come */
405 data->count_output_structure =
406 data->ctx->output_structure == NULL ? -1 : 0;
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;
418 OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
420 current_encoder_inst =
421 sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
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;
437 OSSL_TRACE_BEGIN(ENCODER) {
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);
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).
452 if (data->ctx->output_type != NULL
453 && strcasecmp(current_output_type,
454 data->ctx->output_type) != 0) {
455 OSSL_TRACE_BEGIN(ENCODER) {
457 "[%d] Skipping because current encoder output type (%s) != desired output type (%s)\n",
459 current_output_type, data->ctx->output_type);
460 } OSSL_TRACE_END(ENCODER);
464 if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
465 OSSL_TRACE_BEGIN(ENCODER) {
467 "[%d] Skipping because current encoder output type (%s) != name of encoder %p\n",
469 current_output_type, (void *)next_encoder);
470 } OSSL_TRACE_END(ENCODER);
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.
480 if (data->ctx->output_structure != NULL
481 && current_output_structure != NULL) {
482 if (strcasecmp(data->ctx->output_structure,
483 current_output_structure) != 0) {
484 OSSL_TRACE_BEGIN(ENCODER) {
486 "[%d] Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
488 current_output_structure,
489 data->ctx->output_structure);
490 } OSSL_TRACE_END(ENCODER);
494 data->count_output_structure++;
498 * Recurse to process the encoder implementations before the current
501 ok = encoder_process(&new_data);
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;
508 * ok == -1 means that the recursion call above gave no further
509 * encoders, and that the one we're currently at should
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.
521 OSSL_TRACE_BEGIN(ENCODER) {
523 "[%d] Skipping because recusion level %d failed\n",
524 data->level, new_data.level);
525 } OSSL_TRACE_END(ENCODER);
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|.
535 OSSL_TRACE_BEGIN(ENCODER) {
537 "[%d] (ctx %p) No suitable encoder found\n",
538 data->level, (void *)data->ctx);
539 } OSSL_TRACE_END(ENCODER);
548 * We have reached the beginning of the encoder instance sequence,
549 * so we prepare the object to be encoded.
553 * |data->count_output_structure| is one of these values:
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
559 * >0 There is a desired output structure, and at least one
560 * of the encoder instances matched it
562 if (data->count_output_structure == 0)
566 data->ctx->construct(current_encoder_inst,
567 data->ctx->construct_data);
569 /* Also set the data type, using the encoder implementation name */
570 data->data_type = OSSL_ENCODER_get0_name(current_encoder);
572 /* Assume that the constructor recorded an error */
573 if (original_data != NULL)
579 if (!ossl_assert(data->running_output != NULL)) {
580 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
587 * Create an object abstraction from the latest output, which
588 * was stolen from the previous round.
591 OSSL_PARAM *abstract_p = abstract;
592 const char *prev_output_structure =
593 OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);
596 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
597 (char *)data->data_type, 0);
598 if (prev_output_structure != NULL)
600 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
601 (char *)prev_output_structure,
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;
613 /* Calling the encoder implementation */
616 OSSL_CORE_BIO *cbio = NULL;
617 BIO *current_out = NULL;
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.
624 current_out = data->bio;
625 else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))
627 ok = 0; /* Assume BIO_new() recorded an error */
630 ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
632 ok = current_encoder->encode(current_encoder_ctx, cbio,
633 original_data, current_abstract,
634 data->ctx->selection,
635 ossl_pw_passphrase_callback_enc,
637 OSSL_TRACE_BEGIN(ENCODER) {
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);
645 ossl_core_bio_free(cbio);
646 data->prev_encoder_inst = current_encoder_inst;
650 /* Cleanup and collecting the result */
652 OPENSSL_free(data->running_output);
653 data->running_output = NULL;
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.
659 if (allocated_out != NULL) {
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));
668 BIO_free(allocated_out);
669 if (original_data != NULL)
670 data->ctx->cleanup(data->ctx->construct_data);