xref: /freebsd/crypto/openssl/crypto/encode_decode/encoder_local.h (revision 88b8b7f0c4e9948667a2279e78e975a784049cba)
1 /*
2  * Copyright 2019-2025 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 "crypto/decoder.h"
16 #include "internal/cryptlib.h"
17 #include "internal/passphrase.h"
18 #include "internal/property.h"
19 #include "internal/refcount.h"
20 
21 struct ossl_endecode_base_st {
22     OSSL_PROVIDER *prov;
23     int id;
24     char *name;
25     const OSSL_ALGORITHM *algodef;
26     OSSL_PROPERTY_LIST *parsed_propdef;
27 
28     CRYPTO_REF_COUNT refcnt;
29 };
30 
31 struct ossl_encoder_st {
32     struct ossl_endecode_base_st base;
33     OSSL_FUNC_encoder_newctx_fn *newctx;
34     OSSL_FUNC_encoder_freectx_fn *freectx;
35     OSSL_FUNC_encoder_get_params_fn *get_params;
36     OSSL_FUNC_encoder_gettable_params_fn *gettable_params;
37     OSSL_FUNC_encoder_set_ctx_params_fn *set_ctx_params;
38     OSSL_FUNC_encoder_settable_ctx_params_fn *settable_ctx_params;
39     OSSL_FUNC_encoder_does_selection_fn *does_selection;
40     OSSL_FUNC_encoder_encode_fn *encode;
41     OSSL_FUNC_encoder_import_object_fn *import_object;
42     OSSL_FUNC_encoder_free_object_fn *free_object;
43 };
44 
45 struct ossl_decoder_st {
46     struct ossl_endecode_base_st base;
47     OSSL_FUNC_decoder_newctx_fn *newctx;
48     OSSL_FUNC_decoder_freectx_fn *freectx;
49     OSSL_FUNC_decoder_get_params_fn *get_params;
50     OSSL_FUNC_decoder_gettable_params_fn *gettable_params;
51     OSSL_FUNC_decoder_set_ctx_params_fn *set_ctx_params;
52     OSSL_FUNC_decoder_settable_ctx_params_fn *settable_ctx_params;
53     OSSL_FUNC_decoder_does_selection_fn *does_selection;
54     OSSL_FUNC_decoder_decode_fn *decode;
55     OSSL_FUNC_decoder_export_object_fn *export_object;
56 };
57 
58 struct ossl_encoder_instance_st {
59     OSSL_ENCODER *encoder;        /* Never NULL */
60     void *encoderctx;             /* Never NULL */
61     const char *output_type;      /* Never NULL */
62     const char *output_structure; /* May be NULL */
63 };
64 
65 DEFINE_STACK_OF(OSSL_ENCODER_INSTANCE)
66 
67 void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst);
68 
69 struct ossl_encoder_ctx_st {
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      * The desired output type.  The encoder implementation must have a
79      * gettable "output-type" parameter that this will match against.
80      */
81     const char *output_type;
82     /*
83      * The desired output structure, if that's relevant for the type of
84      * object being encoded.  It may be used for selection of the starting
85      * encoder implementations in a chain.
86      */
87     const char *output_structure;
88 
89     /*
90      * Decoders that are components of any current decoding path.
91      */
92     STACK_OF(OSSL_ENCODER_INSTANCE) *encoder_insts;
93 
94     /*
95      * The constructor and destructor of an object to pass to the first
96      * encoder in a chain.
97      */
98     OSSL_ENCODER_CONSTRUCT *construct;
99     OSSL_ENCODER_CLEANUP *cleanup;
100     void *construct_data;
101 
102     /* For any function that needs a passphrase reader */
103     struct ossl_passphrase_data_st pwdata;
104 };
105 
106 struct ossl_decoder_instance_st {
107     OSSL_DECODER *decoder;       /* Never NULL */
108     void *decoderctx;            /* Never NULL */
109     const char *input_type;      /* Never NULL */
110     const char *input_structure; /* May be NULL */
111     int input_type_id;
112     int order;                   /* For stable ordering of decoders wrt proqs */
113     int score;                   /* For ordering decoders wrt proqs */
114 
115     unsigned int flag_input_structure_was_set : 1;
116 };
117 
118 DEFINE_STACK_OF(OSSL_DECODER_INSTANCE)
119 
120 struct ossl_decoder_ctx_st {
121     /*
122      * The caller may know the input type of the data they pass.  If not,
123      * this will remain NULL and the decoding functionality will start
124      * with trying to decode with any desencoder in |decoder_insts|,
125      * regardless of their respective input type.
126      */
127     const char *start_input_type;
128     /*
129      * The desired input structure, if that's relevant for the type of
130      * object being encoded.  It may be used for selection of the ending
131      * decoder implementations in a chain, i.e. those chosen using the
132      * expected output data type.
133      */
134     const char *input_structure;
135     /*
136      * Select what parts of an object are expected.  This may affect what
137      * decoder implementations are selected, because there are structures
138      * that look different depending on this selection; for example, EVP_PKEY
139      * objects often have different encoding structures for private keys,
140      * public keys and key parameters.
141      * This selection is bit encoded, and the bits correspond to selection
142      * bits available with the provider side operation.  For example, when
143      * encoding an EVP_PKEY, the OSSL_KEYMGMT_SELECT_ macros are used for
144      * this.
145      */
146     int selection;
147 
148     /*
149      * Decoders that are components of any current decoding path.
150      */
151     STACK_OF(OSSL_DECODER_INSTANCE) *decoder_insts;
152 
153     /*
154      * The constructors of a decoding, and its caller argument.
155      */
156     OSSL_DECODER_CONSTRUCT *construct;
157     OSSL_DECODER_CLEANUP *cleanup;
158     void *construct_data;
159 
160     /* For any function that needs a passphrase reader */
161     struct ossl_passphrase_data_st pwdata;
162 
163     /* Signal that further processing should not continue. */
164     int harderr;
165 };
166 
167 const OSSL_PROPERTY_LIST *
168 ossl_decoder_parsed_properties(const OSSL_DECODER *decoder);
169 const OSSL_PROPERTY_LIST *
170 ossl_encoder_parsed_properties(const OSSL_ENCODER *encoder);
171 
172 int ossl_decoder_fast_is_a(OSSL_DECODER *decoder,
173                            const char *name, int *id_cache);
174