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
;
39 /* Data type = the name of the first succeeding encoder implementation */
40 const char *data_type
;
43 static int encoder_process(struct encoder_process_data_st
*data
);
45 int OSSL_ENCODER_to_bio(OSSL_ENCODER_CTX
*ctx
, BIO
*out
)
47 struct encoder_process_data_st data
;
49 memset(&data
, 0, sizeof(data
));
52 data
.current_encoder_inst_index
= OSSL_ENCODER_CTX_get_num_encoders(ctx
);
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?");
62 return encoder_process(&data
) > 0;
65 #ifndef OPENSSL_NO_STDIO
66 static BIO
*bio_from_file(FILE *fp
)
70 if ((b
= BIO_new(BIO_s_file())) == NULL
) {
71 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_BUF_LIB
);
74 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
78 int OSSL_ENCODER_to_fp(OSSL_ENCODER_CTX
*ctx
, FILE *fp
)
80 BIO
*b
= bio_from_file(fp
);
84 ret
= OSSL_ENCODER_to_bio(ctx
, b
);
91 int OSSL_ENCODER_to_data(OSSL_ENCODER_CTX
*ctx
, unsigned char **pdata
,
94 BIO
*out
= BIO_new(BIO_s_mem());
98 if (pdata_len
== NULL
) {
99 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
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 */
107 if (pdata
!= NULL
&& *pdata
!= NULL
) {
108 if (*pdata_len
< buf
->length
)
110 * It's tempting to do |*pdata_len = (size_t)buf->length|
111 * However, it's believed to be confusing more than helpful,
116 *pdata_len
-= buf
->length
;
118 /* The buffer with the right size is already allocated for us */
119 *pdata_len
= (size_t)buf
->length
;
124 if (*pdata
!= NULL
) {
125 memcpy(*pdata
, buf
->data
, buf
->length
);
126 *pdata
+= buf
->length
;
128 /* In this case, we steal the data from BIO_s_mem() */
129 *pdata
= (unsigned char *)buf
->data
;
139 int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX
*ctx
, int selection
)
141 if (!ossl_assert(ctx
!= NULL
)) {
142 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
146 if (!ossl_assert(selection
!= 0)) {
147 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_INVALID_ARGUMENT
);
151 ctx
->selection
= selection
;
155 int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX
*ctx
,
156 const char *output_type
)
158 if (!ossl_assert(ctx
!= NULL
) || !ossl_assert(output_type
!= NULL
)) {
159 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
163 ctx
->output_type
= output_type
;
167 int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX
*ctx
,
168 const char *output_structure
)
170 if (!ossl_assert(ctx
!= NULL
) || !ossl_assert(output_structure
!= NULL
)) {
171 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
175 ctx
->output_structure
= output_structure
;
179 static OSSL_ENCODER_INSTANCE
*ossl_encoder_instance_new(OSSL_ENCODER
*encoder
,
182 OSSL_ENCODER_INSTANCE
*encoder_inst
= NULL
;
183 OSSL_PARAM params
[4];
185 if (!ossl_assert(encoder
!= NULL
)) {
186 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
190 if (encoder
->get_params
== NULL
) {
191 ERR_raise(ERR_LIB_OSSL_ENCODER
,
192 OSSL_ENCODER_R_MISSING_GET_PARAMS
);
196 if ((encoder_inst
= OPENSSL_zalloc(sizeof(*encoder_inst
))) == NULL
) {
197 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_MALLOC_FAILURE
);
202 * Cache the input and output types for this encoder. The output type
206 OSSL_PARAM_construct_utf8_ptr(OSSL_ENCODER_PARAM_OUTPUT_TYPE
,
207 (char **)&encoder_inst
->output_type
, 0);
209 OSSL_PARAM_construct_utf8_ptr(OSSL_ENCODER_PARAM_OUTPUT_STRUCTURE
,
210 (char **)&encoder_inst
->output_structure
,
212 params
[2] = OSSL_PARAM_construct_end();
214 if (!encoder
->get_params(params
)
215 || !OSSL_PARAM_modified(¶ms
[0]))
218 if (!OSSL_ENCODER_up_ref(encoder
)) {
219 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_INTERNAL_ERROR
);
223 encoder_inst
->encoder
= encoder
;
224 encoder_inst
->encoderctx
= encoderctx
;
227 ossl_encoder_instance_free(encoder_inst
);
231 void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE
*encoder_inst
)
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
);
243 static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX
*ctx
,
244 OSSL_ENCODER_INSTANCE
*ei
)
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
);
255 ok
= (sk_OSSL_ENCODER_INSTANCE_push(ctx
->encoder_insts
, ei
) > 0);
257 OSSL_TRACE_BEGIN(ENCODER
) {
259 "(ctx %p) Added encoder instance %p (encoder %p) with:\n",
260 (void *)ctx
, (void *)ei
, (void *)ei
->encoder
);
262 " output type: %s, output structure: %s\n",
263 ei
->output_type
, ei
->output_structure
);
264 } OSSL_TRACE_END(ENCODER
);
269 int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX
*ctx
, OSSL_ENCODER
*encoder
)
271 OSSL_ENCODER_INSTANCE
*encoder_inst
= NULL
;
272 const OSSL_PROVIDER
*prov
= NULL
;
273 void *encoderctx
= NULL
;
274 void *provctx
= NULL
;
276 if (!ossl_assert(ctx
!= NULL
) || !ossl_assert(encoder
!= NULL
)) {
277 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
281 prov
= OSSL_ENCODER_get0_provider(encoder
);
282 provctx
= OSSL_PROVIDER_get0_provider_ctx(prov
);
284 if ((encoderctx
= encoder
->newctx(provctx
)) == NULL
286 ossl_encoder_instance_new(encoder
, encoderctx
)) == NULL
)
288 /* Avoid double free of encoderctx on further errors */
291 if (!ossl_encoder_ctx_add_encoder_inst(ctx
, encoder_inst
))
296 ossl_encoder_instance_free(encoder_inst
);
297 if (encoderctx
!= NULL
)
298 encoder
->freectx(encoderctx
);
302 int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX
*ctx
,
303 OSSL_LIB_CTX
*libctx
, const char *propq
)
308 int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX
*ctx
)
310 if (ctx
== NULL
|| ctx
->encoder_insts
== NULL
)
312 return sk_OSSL_ENCODER_INSTANCE_num(ctx
->encoder_insts
);
315 int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX
*ctx
,
316 OSSL_ENCODER_CONSTRUCT
*construct
)
318 if (!ossl_assert(ctx
!= NULL
)) {
319 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
322 ctx
->construct
= construct
;
326 int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX
*ctx
,
327 void *construct_data
)
329 if (!ossl_assert(ctx
!= NULL
)) {
330 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
333 ctx
->construct_data
= construct_data
;
337 int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX
*ctx
,
338 OSSL_ENCODER_CLEANUP
*cleanup
)
340 if (!ossl_assert(ctx
!= NULL
)) {
341 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
344 ctx
->cleanup
= cleanup
;
349 OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE
*encoder_inst
)
351 if (encoder_inst
== NULL
)
353 return encoder_inst
->encoder
;
357 OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE
*encoder_inst
)
359 if (encoder_inst
== NULL
)
361 return encoder_inst
->encoderctx
;
365 OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE
*encoder_inst
)
367 if (encoder_inst
== NULL
)
369 return encoder_inst
->output_type
;
373 OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE
*encoder_inst
)
375 if (encoder_inst
== NULL
)
377 return encoder_inst
->output_structure
;
380 static int encoder_process(struct encoder_process_data_st
*data
)
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
;
390 int ok
= -1; /* -1 signifies that the lookup loop gave nothing */
393 if (data
->next_encoder_inst
== NULL
) {
394 /* First iteration, where we prepare for what is to come */
396 data
->count_output_structure
=
397 data
->ctx
->output_structure
== NULL
? -1 : 0;
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
;
409 OSSL_ENCODER_INSTANCE_get_encoder(data
->next_encoder_inst
);
411 current_encoder_inst
=
412 sk_OSSL_ENCODER_INSTANCE_value(data
->ctx
->encoder_insts
, i
);
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;
428 OSSL_TRACE_BEGIN(ENCODER
) {
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
);
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).
443 if (data
->ctx
->output_type
!= NULL
444 && strcasecmp(current_output_type
,
445 data
->ctx
->output_type
) != 0) {
446 OSSL_TRACE_BEGIN(ENCODER
) {
448 "[%d] Skipping because current encoder output type (%s) != desired output type (%s)\n",
450 current_output_type
, data
->ctx
->output_type
);
451 } OSSL_TRACE_END(ENCODER
);
455 if (!OSSL_ENCODER_is_a(next_encoder
, current_output_type
)) {
456 OSSL_TRACE_BEGIN(ENCODER
) {
458 "[%d] Skipping because current encoder output type (%s) != name of encoder %p\n",
460 current_output_type
, (void *)next_encoder
);
461 } OSSL_TRACE_END(ENCODER
);
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.
471 if (data
->ctx
->output_structure
!= NULL
472 && current_output_structure
!= NULL
) {
473 if (strcasecmp(data
->ctx
->output_structure
,
474 current_output_structure
) != 0) {
475 OSSL_TRACE_BEGIN(ENCODER
) {
477 "[%d] Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
479 current_output_structure
,
480 data
->ctx
->output_structure
);
481 } OSSL_TRACE_END(ENCODER
);
485 data
->count_output_structure
++;
489 * Recurse to process the encoder implementations before the current
492 ok
= encoder_process(&new_data
);
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
;
499 * ok == -1 means that the recursion call above gave no further
500 * encoders, and that the one we're currently at should
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.
512 OSSL_TRACE_BEGIN(ENCODER
) {
514 "[%d] Skipping because recusion level %d failed\n",
515 data
->level
, new_data
.level
);
516 } OSSL_TRACE_END(ENCODER
);
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|.
526 OSSL_TRACE_BEGIN(ENCODER
) {
528 "[%d] (ctx %p) No suitable encoder found\n",
529 data
->level
, (void *)data
->ctx
);
530 } OSSL_TRACE_END(ENCODER
);
539 * We have reached the beginning of the encoder instance sequence,
540 * so we prepare the object to be encoded.
544 * |data->count_output_structure| is one of these values:
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
550 * >0 There is a desired output structure, and at least one
551 * of the encoder instances matched it
553 if (data
->count_output_structure
== 0)
557 data
->ctx
->construct(current_encoder_inst
,
558 data
->ctx
->construct_data
);
560 /* Also set the data type, using the encoder implementation name */
561 data
->data_type
= OSSL_ENCODER_get0_name(current_encoder
);
563 /* Assume that the constructor recorded an error */
564 if (original_data
!= NULL
)
570 if (!ossl_assert(data
->running_output
!= NULL
)) {
571 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_INTERNAL_ERROR
);
578 * Create an object abstraction from the latest output, which
579 * was stolen from the previous round.
582 OSSL_PARAM
*abstract_p
= abstract
;
583 const char *prev_output_structure
=
584 OSSL_ENCODER_INSTANCE_get_output_structure(data
->prev_encoder_inst
);
587 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE
,
588 (char *)data
->data_type
, 0);
589 if (prev_output_structure
!= NULL
)
591 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE
,
592 (char *)prev_output_structure
,
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
;
604 /* Calling the encoder implementation */
607 OSSL_CORE_BIO
*cbio
= NULL
;
608 BIO
*current_out
= NULL
;
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.
615 current_out
= data
->bio
;
616 else if ((current_out
= allocated_out
= BIO_new(BIO_s_mem()))
618 ok
= 0; /* Assume BIO_new() recorded an error */
621 ok
= (cbio
= ossl_core_bio_new_from_bio(current_out
)) != NULL
;
623 ok
= current_encoder
->encode(current_encoder_ctx
, cbio
,
624 original_data
, current_abstract
,
625 data
->ctx
->selection
,
626 ossl_pw_passphrase_callback_enc
,
628 OSSL_TRACE_BEGIN(ENCODER
) {
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
);
636 ossl_core_bio_free(cbio
);
637 data
->prev_encoder_inst
= current_encoder_inst
;
641 /* Cleanup and collecting the result */
643 OPENSSL_free(data
->running_output
);
644 data
->running_output
= NULL
;
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.
650 if (allocated_out
!= NULL
) {
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
));
659 BIO_free(allocated_out
);
660 if (original_data
!= NULL
)
661 data
->ctx
->cleanup(data
->ctx
->construct_data
);