ENCODER: Refactor the OSSL_ENCODER API to be more like OSSL_DECODER
[openssl.git] / crypto / encode_decode / encoder_local.h
1 /*
2  * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
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/passphrase.h"
17 #include "internal/refcount.h"
18
19 struct ossl_endecode_base_st {
20     OSSL_PROVIDER *prov;
21     int id;
22     const char *propdef;
23
24     CRYPTO_REF_COUNT refcnt;
25     CRYPTO_RWLOCK *lock;
26 };
27
28 struct ossl_encoder_st {
29     struct ossl_endecode_base_st base;
30     OSSL_FUNC_encoder_newctx_fn *newctx;
31     OSSL_FUNC_encoder_freectx_fn *freectx;
32     OSSL_FUNC_encoder_get_params_fn *get_params;
33     OSSL_FUNC_encoder_gettable_params_fn *gettable_params;
34     OSSL_FUNC_encoder_set_ctx_params_fn *set_ctx_params;
35     OSSL_FUNC_encoder_settable_ctx_params_fn *settable_ctx_params;
36     OSSL_FUNC_encoder_encode_fn *encode;
37     OSSL_FUNC_encoder_import_object_fn *import_object;
38     OSSL_FUNC_encoder_free_object_fn *free_object;
39 };
40
41 struct ossl_decoder_st {
42     struct ossl_endecode_base_st base;
43     OSSL_FUNC_decoder_newctx_fn *newctx;
44     OSSL_FUNC_decoder_freectx_fn *freectx;
45     OSSL_FUNC_decoder_get_params_fn *get_params;
46     OSSL_FUNC_decoder_gettable_params_fn *gettable_params;
47     OSSL_FUNC_decoder_set_ctx_params_fn *set_ctx_params;
48     OSSL_FUNC_decoder_settable_ctx_params_fn *settable_ctx_params;
49     OSSL_FUNC_decoder_decode_fn *decode;
50     OSSL_FUNC_decoder_export_object_fn *export_object;
51 };
52
53 struct ossl_encoder_instance_st {
54     OSSL_ENCODER *encoder;       /* Never NULL */
55     void *encoderctx;            /* Never NULL */
56     const char *input_type;      /* May be NULL */
57     const char *output_type;     /* Never NULL */
58 };
59
60 DEFINE_STACK_OF(OSSL_ENCODER_INSTANCE)
61
62 void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst);
63
64 struct ossl_encoder_ctx_st {
65     /*
66      * The desired output type.  The encoder implementation have a gettable
67      * "output-type" parameter that this will match against.
68      */
69     const char *output_type;
70     /*
71      * Select what parts of an object will be encoded.  This selection is
72      * bit encoded, and the bits correspond to selection bits available with
73      * the provider side operation.  For example, when encoding an EVP_PKEY,
74      * the OSSL_KEYMGMT_SELECT_ macros are used for this.
75      */
76     int selection;
77
78     /*
79      * Decoders that are components of any current decoding path.
80      */
81     STACK_OF(OSSL_ENCODER_INSTANCE) *encoder_insts;
82
83     /*
84      * The constructor and destructor of an object to pass to the first
85      * encoder in a chain.
86      */
87     OSSL_ENCODER_CONSTRUCT *construct;
88     OSSL_ENCODER_CLEANUP *cleanup;
89     void *construct_data;
90
91     /* For any function that needs a passphrase reader */
92     struct ossl_passphrase_data_st pwdata;
93 };
94
95 struct ossl_decoder_instance_st {
96     OSSL_DECODER *decoder;       /* Never NULL */
97     void *decoderctx;            /* Never NULL */
98     const char *input_type;      /* Never NULL */
99 };
100
101 DEFINE_STACK_OF(OSSL_DECODER_INSTANCE)
102
103 struct ossl_decoder_ctx_st {
104     /*
105      * The caller may know the input type of the data they pass.  If not,
106      * this will remain NULL and the decoding functionality will start
107      * with trying to decode with any desencoder in |decoder_insts|,
108      * regardless of their respective input type.
109      */
110     const char *start_input_type;
111
112     /*
113      * Decoders that are components of any current decoding path.
114      */
115     STACK_OF(OSSL_DECODER_INSTANCE) *decoder_insts;
116
117     /*
118      * The constructors of a decoding, and its caller argument.
119      */
120     OSSL_DECODER_CONSTRUCT *construct;
121     OSSL_DECODER_CLEANUP *cleanup;
122     void *construct_data;
123
124     /* For any function that needs a passphrase reader */
125     struct ossl_passphrase_data_st pwdata;
126 };