5 provider-encoder - The OSSL_ENCODER library E<lt>-E<gt> provider functions
9 #include <openssl/core_dispatch.h>
12 * None of these are actual functions, but are displayed like this for
13 * the function signatures for functions that are offered as function
14 * pointers in OSSL_DISPATCH arrays.
17 /* Encoder parameter accessor and descriptor */
18 const OSSL_PARAM *OSSL_FUNC_encoder_gettable_params(void *provctx);
19 int OSSL_FUNC_encoder_get_params(OSSL_PARAM params[]);
21 /* Functions to construct / destruct / manipulate the encoder context */
22 void *OSSL_FUNC_encoder_newctx(void *provctx);
23 void OSSL_FUNC_encoder_freectx(void *ctx);
24 int OSSL_FUNC_encoder_set_ctx_params(void *ctx, const OSSL_PARAM params[]);
25 const OSSL_PARAM *OSSL_FUNC_encoder_settable_ctx_params(void *provctx);
27 /* Functions to check selection support */
28 int OSSL_FUNC_encoder_does_selection(void *provctx, int selection);
30 /* Functions to encode object data */
31 int OSSL_FUNC_encoder_encode(void *ctx, OSSL_CORE_BIO *out,
33 const OSSL_PARAM obj_abstract[],
35 OSSL_PASSPHRASE_CALLBACK *cb,
38 /* Functions to import and free a temporary object to be encoded */
39 void *OSSL_FUNC_encoder_import_object(void *ctx, int selection,
40 const OSSL_PARAM params[]);
41 void OSSL_FUNC_encoder_free_object(void *obj);
46 I<We use the wide term "encode" in this manual. This includes but is
47 not limited to serialization.>
49 The ENCODER operation is a generic method to encode a provider-native
50 object (I<obj_raw>) or an object abstraction (I<object_abstract>, see
51 L<provider-object(7)>) into an encoded form, and write the result to
52 the given OSSL_CORE_BIO. If the caller wants to get the encoded
53 stream to memory, it should provide a L<BIO_s_mem(3)> B<BIO>.
55 The encoder doesn't need to know more about the B<OSSL_CORE_BIO>
56 pointer than being able to pass it to the appropriate BIO upcalls (see
57 L<provider-base(7)/Core functions>).
59 The ENCODER implementation may be part of a chain, where data is
60 passed from one to the next. For example, there may be an
61 implementation to encode an object to DER (that object is assumed to
62 be provider-native and thereby passed via I<obj_raw>), and another one
63 that encodes DER to PEM (that one would receive the DER encoding via
68 Having the DER encoding passed via I<obj_abstract> may seem
69 complicated. However, there may be associated meta-data, such as the
70 original data type, that need to be passed alongside it, and since
71 L<provider-object(7)> already defines a way to pass such data,
72 inventing another way to do it makes things even more complicated.
76 The encoding using the L<OSSL_PARAM(3)> array form allows a
77 encoder to be used for data that's been exported from another
78 provider, and thereby allow them to exist independently of each
81 The encoding using a provider side object can only be safely used
82 with provider data coming from the same provider, for example keys
83 with the L<KEYMGMT|provider-keymgmt(7)> provider.
85 All "functions" mentioned here are passed as function pointers between
86 F<libcrypto> and the provider in B<OSSL_DISPATCH> arrays via
87 B<OSSL_ALGORITHM> arrays that are returned by the provider's
88 provider_query_operation() function
89 (see L<provider-base(7)/Provider Functions>).
91 All these "functions" have a corresponding function type definition
92 named B<OSSL_FUNC_{name}_fn>, and a helper function to retrieve the
93 function pointer from an B<OSSL_DISPATCH> element named
95 For example, the "function" OSSL_FUNC_encoder_encode() has these:
98 (OSSL_FUNC_encoder_encode_fn)(void *ctx, OSSL_CORE_BIO *out,
100 const OSSL_PARAM obj_abstract[],
102 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg);
103 static ossl_inline OSSL_FUNC_encoder_encode_fn
104 OSSL_FUNC_encoder_encode(const OSSL_DISPATCH *opf);
106 B<OSSL_DISPATCH> arrays are indexed by numbers that are provided as
107 macros in L<openssl-core_dispatch.h(7)>, as follows:
109 OSSL_FUNC_encoder_get_params OSSL_FUNC_ENCODER_GET_PARAMS
110 OSSL_FUNC_encoder_gettable_params OSSL_FUNC_ENCODER_GETTABLE_PARAMS
112 OSSL_FUNC_encoder_newctx OSSL_FUNC_ENCODER_NEWCTX
113 OSSL_FUNC_encoder_freectx OSSL_FUNC_ENCODER_FREECTX
114 OSSL_FUNC_encoder_set_ctx_params OSSL_FUNC_ENCODER_SET_CTX_PARAMS
115 OSSL_FUNC_encoder_settable_ctx_params OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS
117 OSSL_FUNC_encoder_does_selection OSSL_FUNC_ENCODER_DOES_SELECTION
119 OSSL_FUNC_encoder_encode OSSL_FUNC_ENCODER_ENCODE
121 OSSL_FUNC_encoder_import_object OSSL_FUNC_ENCODER_IMPORT_OBJECT
122 OSSL_FUNC_encoder_free_object OSSL_FUNC_ENCODER_FREE_OBJECT
124 =head2 Names and properties
126 The name of an implementation should match the type of object it handles.
127 For example, an implementation that encodes an RSA key should be named "RSA".
128 Likewise, an implementation that further encodes DER should be named "DER".
130 Properties can be used to further specify details about an implementation:
136 This property is used to specify what type of output implementation
137 produces. OpenSSL providers recognize the following output types:
143 An implementation with that output type outputs human readable text, making
144 that implementation suitable for C<-text> output in diverse L<openssl(1)>
149 An implementation with that output type outputs PEM formatted data.
153 An implementation with that output type outputs DER formatted data.
157 An implementation with that output type outputs MSBLOB formatted data.
161 An implementation with that output type outputs PVK formatted data.
167 This property is used to specify the structure that is used for the encoded
168 object. An example could be C<pkcs8>, to specify explicitly that an object
169 (presumably an asymmetric key pair, in this case) will be wrapped in a
170 PKCS#8 structure as part of the encoding.
174 The possible values of both these properties is open ended. A provider may
175 very well specify output types and structures that libcrypto doesn't know
178 =head2 Subset selections
180 Sometimes, an object has more than one subset of data that is interesting to
181 treat separately or together. It's possible to specify what subsets are to
182 be encoded, with a set of bits I<selection> that are passed in an B<int>.
184 This set of bits depend entirely on what kind of provider-side object is
185 passed. For example, those bits are assumed to be the same as those used
186 with L<provider-keymgmt(7)> (see L<provider-keymgmt(7)/Key Objects>) when
187 the object is an asymmetric keypair.
189 ENCODER implementations are free to regard the I<selection> as a set of
190 hints, but must do so with care. In the end, the output must make sense,
191 and if there's a corresponding decoder, the resulting decoded object must
192 match the original object that was encoded.
194 OSSL_FUNC_encoder_does_selection() should tell if a particular implementation
195 supports any of the combinations given by I<selection>.
197 =head2 Context functions
199 OSSL_FUNC_encoder_newctx() returns a context to be used with the rest of
202 OSSL_FUNC_encoder_freectx() frees the given I<ctx>, if it was created by
203 OSSL_FUNC_encoder_newctx().
205 OSSL_FUNC_encoder_set_ctx_params() sets context data according to parameters
206 from I<params> that it recognises. Unrecognised parameters should be
208 Passing NULL for I<params> should return true.
210 OSSL_FUNC_encoder_settable_ctx_params() returns a constant B<OSSL_PARAM>
211 array describing the parameters that OSSL_FUNC_encoder_set_ctx_params()
214 See L<OSSL_PARAM(3)> for further details on the parameters structure used by
215 OSSL_FUNC_encoder_set_ctx_params() and OSSL_FUNC_encoder_settable_ctx_params().
217 =head2 Import functions
219 A provider-native object may be associated with a foreign provider, and may
220 therefore be unsuitable for direct use with a given ENCODER implementation.
221 Provided that the foreign provider's implementation to handle the object has
222 a function to export that object in L<OSSL_PARAM(3)> array form, the ENCODER
223 implementation should be able to import that array and create a suitable
224 object to be passed to OSSL_FUNC_encoder_encode()'s I<obj_raw>.
226 OSSL_FUNC_encoder_import_object() should import the subset of I<params>
227 given with I<selection> to create a provider-native object that can be
228 passed as I<obj_raw> to OSSL_FUNC_encoder_encode().
230 OSSL_FUNC_encoder_free_object() should free the object that was created with
231 OSSL_FUNC_encoder_import_object().
233 =head2 Encoding functions
235 OSSL_FUNC_encoder_encode() should take a provider-native object (in
236 I<obj_raw>) or an object abstraction (in I<obj_abstract>), and should output
237 the object in encoded form to the B<OSSL_CORE_BIO>. The I<selection> bits,
238 if relevant, should determine in greater detail what will be output.
239 The encoding functions also take an B<OSSL_PASSPHRASE_CALLBACK> function
240 pointer along with a pointer to application data I<cbarg>, which should be
241 used when a pass phrase prompt is needed.
243 =head2 Encoder parameters
245 The ENCODER implementation itself has parameters that can be used to
246 determine how it fits in a chain of encoders:
250 =item "output-type" (B<OSSL_ENCODER_PARAM_OUTPUT_TYPE>) <UTF8 string>
252 This is used to specify the output type for an ENCODER implementation.
254 This parameter is I<mandatory>.
256 =for comment If we had functionality to get the value of a specific property
257 in a set of properties, it would be possible to determine the output type
258 from the C<output> property.
260 =item "output-structure" (B<OSSL_ENCODER_PARAM_OUTPUT_STRUCTURE>) <UTF8 string>
262 This is used to specify the outermost output structure for an ENCODER
265 For example, an output of type "DER" for a key pair could be structured
266 using PKCS#8, or a key type specific structure, such as PKCS#1 for RSA
269 =for comment If we had functionality to get the value of a specific property
270 in a set of properties, it would be possible to determine the output
271 structure from the C<structure> property.
275 =head2 Encoder operation parameters
277 Operation parameters currently recognised by built-in encoders are as
282 =item "cipher" (B<OSSL_ENCODER_PARAM_CIPHER>) <UTF8 string>
284 The name of the encryption cipher to be used when generating encrypted
285 encoding. This is used when encoding private keys, as well as
286 other objects that need protection.
288 If this name is invalid for the encoding implementation, the
289 implementation should refuse to perform the encoding, i.e.
290 OSSL_FUNC_encoder_encode_data() and OSSL_FUNC_encoder_encode_object()
291 should return an error.
293 =item "properties" (B<OSSL_ENCODER_PARAM_PROPERTIES>) <UTF8 string>
295 The properties to be queried when trying to fetch the algorithm given
296 with the "cipher" parameter.
297 This must be given together with the "cipher" parameter to be
300 The encoding implementation isn't obligated to use this value.
301 However, it is recommended that implementations that do not handle
302 property strings return an error on receiving this parameter unless
303 its value NULL or the empty string.
305 =item "save-parameters" (B<OSSL_ENCODER_PARAM_SAVE_PARAMETERS>) <integer>
307 If set to 0 disables saving of key domain parameters. Default is 1.
308 It currently has an effect only on DSA keys.
312 Parameters currently recognised by the built-in pass phrase callback:
316 =item "info" (B<OSSL_PASSPHRASE_PARAM_INFO>) <UTF8 string>
318 A string of information that will become part of the pass phrase
319 prompt. This could be used to give the user information on what kind
320 of object it's being prompted for.
326 OSSL_FUNC_encoder_newctx() returns a pointer to a context, or NULL on
329 OSSL_FUNC_encoder_set_ctx_params() returns 1, unless a recognised
330 parameter was invalid or caused an error, for which 0 is returned.
332 OSSL_FUNC_encoder_settable_ctx_params() returns a pointer to an array of
333 constant B<OSSL_PARAM> elements.
335 OSSL_FUNC_encoder_does_selection() returns 1 if the encoder implementation
336 supports any of the I<selection> bits, otherwise 0.
338 OSSL_FUNC_encoder_encode() returns 1 on success, or 0 on failure.
346 The ENCODER interface was introduced in OpenSSL 3.0.
350 Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
352 Licensed under the Apache License 2.0 (the "License"). You may not use
353 this file except in compliance with the License. You can obtain a copy
354 in the file LICENSE in the source distribution or at
355 L<https://www.openssl.org/source/license.html>.