2 * Copyright 2019-2020 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 <openssl/core_dispatch.h>
11 #include <openssl/types.h>
12 #include <openssl/safestack.h>
13 #include <openssl/encoder.h>
14 #include <openssl/decoder.h>
15 #include "internal/cryptlib.h"
16 #include "internal/refcount.h"
18 struct ossl_serdes_base_st
{
23 CRYPTO_REF_COUNT refcnt
;
27 struct ossl_encoder_st
{
28 struct ossl_serdes_base_st base
;
29 OSSL_FUNC_encoder_newctx_fn
*newctx
;
30 OSSL_FUNC_encoder_freectx_fn
*freectx
;
31 OSSL_FUNC_encoder_set_ctx_params_fn
*set_ctx_params
;
32 OSSL_FUNC_encoder_settable_ctx_params_fn
*settable_ctx_params
;
33 OSSL_FUNC_encoder_encode_data_fn
*encode_data
;
34 OSSL_FUNC_encoder_encode_object_fn
*encode_object
;
37 struct ossl_decoder_st
{
38 struct ossl_serdes_base_st base
;
39 OSSL_FUNC_decoder_newctx_fn
*newctx
;
40 OSSL_FUNC_decoder_freectx_fn
*freectx
;
41 OSSL_FUNC_decoder_get_params_fn
*get_params
;
42 OSSL_FUNC_decoder_gettable_params_fn
*gettable_params
;
43 OSSL_FUNC_decoder_set_ctx_params_fn
*set_ctx_params
;
44 OSSL_FUNC_decoder_settable_ctx_params_fn
*settable_ctx_params
;
45 OSSL_FUNC_decoder_decode_fn
*decode
;
46 OSSL_FUNC_decoder_export_object_fn
*export_object
;
49 struct ossl_encoder_ctx_st
{
50 OSSL_ENCODER
*encoder
;
56 * Output / encoding data, used by OSSL_ENCODER_to_{bio,fp}
58 * |object| is the libcrypto object to handle.
59 * |do_output| performs the actual encoding.
61 * |do_output| must have intimate knowledge of |object|.
64 int (*do_output
)(OSSL_ENCODER_CTX
*ctx
, BIO
*out
);
66 /* For any function that needs a passphrase reader */
67 const UI_METHOD
*ui_method
;
70 * if caller used OSSL_ENCODER_CTX_set_passphrase_cb(), we need
71 * intermediary storage.
73 UI_METHOD
*allocated_ui_method
;
76 struct ossl_decoder_instance_st
{
77 OSSL_DECODER
*decoder
; /* Never NULL */
78 void *deserctx
; /* Never NULL */
79 const char *input_type
; /* Never NULL */
82 DEFINE_STACK_OF(OSSL_DECODER_INSTANCE
)
84 struct ossl_decoder_ctx_st
{
86 * The caller may know the input type of the data they pass. If not,
87 * this will remain NULL and the decoding functionality will start
88 * with trying to decode with any desencoder in |decoder_insts|,
89 * regardless of their respective input type.
91 const char *start_input_type
;
94 * Decoders that are components of any current decoding path.
96 STACK_OF(OSSL_DECODER_INSTANCE
) *decoder_insts
;
99 * The constructors of a decoding, and its caller argument.
101 OSSL_DECODER_CONSTRUCT
*construct
;
102 OSSL_DECODER_CLEANUP
*cleanup
;
103 void *construct_data
;
105 /* For any function that needs a passphrase reader */
106 OSSL_PASSPHRASE_CALLBACK
*passphrase_cb
;
107 const UI_METHOD
*ui_method
;
110 * if caller used OSSL_ENCODER_CTX_set_pem_password_cb(), we need
111 * intermediary storage.
113 UI_METHOD
*allocated_ui_method
;
115 * Because the same input may pass through more than one decoder,
116 * we cache any passphrase passed to us. The desrializing processor
117 * must clear this at the end of a run.
119 unsigned char *cached_passphrase
;
120 size_t cached_passphrase_len
;
123 * Flag section. Keep these together
127 * The passphrase was passed to us by the user. In that case, it
128 * should only be freed when freeing this context.
130 unsigned int flag_user_passphrase
:1;
133 /* Passphrase callbacks, found in serdes_pass.c */
136 * Encoders typically want to get an outgoing passphrase, while
137 * decoders typically want to get en incoming passphrase.
139 OSSL_PASSPHRASE_CALLBACK ossl_encoder_passphrase_out_cb
;
140 OSSL_PASSPHRASE_CALLBACK ossl_decoder_passphrase_in_cb
;