ENCODER: Add support for specifying the outermost output structure
[openssl.git] / doc / man7 / provider-encoder.pod
1 =pod
2
3 =head1 NAME
4
5 provider-encoder - The OSSL_ENCODER library E<lt>-E<gt> provider functions
6
7 =head1 SYNOPSIS
8
9  #include <openssl/core_dispatch.h>
10
11  /*
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.
15   */
16
17  /* Encoder parameter accessor and descriptor */
18  const OSSL_PARAM *OSSL_FUNC_encoder_gettable_params(void *provctx);
19  int encoder_get_params(OSSL_PARAM params[]);
20
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)
26
27  /* Functions to encode object data */
28  int OSSL_FUNC_encoder_encode(void *ctx, OSSL_CORE_BIO *out,
29                               const void *obj_raw,
30                               const OSSL_PARAM obj_abstract[],
31                               int selection,
32                               OSSL_PASSPHRASE_CALLBACK *cb,
33                               void *cbarg);
34
35  /* Functions to import and free a temporary object to be encoded */
36  void *encoder_import_object(void *ctx, int selection,
37                              const OSSL_PARAM params[]);
38  void encoder_free_object(void *obj);
39
40
41 =head1 DESCRIPTION
42
43 I<We use the wide term "encode" in this manual.  This includes but is
44 not limited to serialization.>
45
46 The ENCODER operation is a generic method to encode a provider-native
47 object (I<obj_raw>) or an object abstraction (I<object_abstract>, see
48 L<provider-object(7)>) into an encoded form, and write the result to
49 the given OSSL_CORE_BIO.  If the caller wants to get the encoded
50 stream to memory, it should provide a L<BIO_s_membuf(3)>.
51
52 The encoder doesn't need to know more about the B<OSSL_CORE_BIO>
53 pointer than being able to pass it to the appropriate BIO upcalls (see
54 L<provider-base(7)/Core functions>).
55
56 The ENCODER implementation may be part of a chain, where data is
57 passed from one to the next.  For example, there may be an
58 implementation to encode an object to DER (that object is assumed to
59 be provider-native and thereby passed via I<obj_raw>), and another one
60 that encodes DER to PEM (that one would receive the DER encoding via
61 I<obj_abstract>).
62
63 =begin comment
64
65 Having the DER encoding passed via I<obj_abstract> may seem
66 complicated.  However, there may be associated meta-data, such as the
67 original data type, that need to be passed alongside it, and since
68 L<provider-object(7)> already defines a way to pass such data,
69 inventing another way to do it makes things even more complicated.
70
71 =end comment
72
73 The encoding using the L<OSSL_PARAM(3)> array form allows a
74 encoder to be used for data that's been exported from another
75 provider, and thereby allow them to exist independently of each
76 other.
77
78 The encoding using a provider side object can only be safely used
79 with provider data coming from the same provider, for example keys
80 with the L<KEYMGMT|provider-keymgmt(7)> provider.
81
82 All "functions" mentioned here are passed as function pointers between
83 F<libcrypto> and the provider in B<OSSL_DISPATCH> arrays via
84 B<OSSL_ALGORITHM> arrays that are returned by the provider's
85 provider_query_operation() function
86 (see L<provider-base(7)/Provider Functions>).
87
88 All these "functions" have a corresponding function type definition
89 named B<OSSL_{name}_fn>, and a helper function to retrieve the
90 function pointer from a B<OSSL_DISPATCH> element named
91 B<OSSL_FUNC_{name}>.
92 For example, the "function" OSSL_FUNC_encoder_encode_data() has these:
93
94  typedef int
95      (OSSL_FUNC_encoder_encode_fn)(void *ctx, OSSL_CORE_BIO *out,
96                                    const void *obj_raw,
97                                    const OSSL_PARAM obj_abstract[],
98                                    int selection,
99                                    OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg);
100  static ossl_inline OSSL_FUNC_encoder_encode_fn
101      OSSL_FUNC_encoder_encode_data(const OSSL_DISPATCH *opf);
102
103 B<OSSL_DISPATCH> arrays are indexed by numbers that are provided as
104 macros in L<openssl-core_dispatch.h(7)>, as follows:
105
106  OSSL_FUNC_encoder_get_params          OSSL_FUNC_ENCODER_GET_PARAMS
107  OSSL_FUNC_encoder_gettable_params     OSSL_FUNC_ENCODER_GETTABLE_PARAMS
108
109  OSSL_FUNC_encoder_newctx              OSSL_FUNC_ENCODER_NEWCTX
110  OSSL_FUNC_encoder_freectx             OSSL_FUNC_ENCODER_FREECTX
111  OSSL_FUNC_encoder_set_ctx_params      OSSL_FUNC_ENCODER_SET_CTX_PARAMS
112  OSSL_FUNC_encoder_settable_ctx_params OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS
113
114  OSSL_FUNC_encoder_encode              OSSL_FUNC_ENCODER_ENCODE_DATA
115
116  OSSL_FUNC_encoder_import_object       OSSL_FUNC_ENCODER_IMPORT_OBJECT
117  OSSL_FUNC_encoder_free_object         OSSL_FUNC_ENCODER_FREE_OBJECT
118
119 =head2 Names and properties
120
121 The name of an implementation should match the type of object it handles.
122 For example, an implementation that encodes an RSA key should be named "RSA".
123 Likewise, an implementation that further encodes DER should be named "DER".
124
125 Properties can be use to further specify details about an implementation:
126
127 =over 4
128
129 =item output
130
131 This property is used to specify what type of output implementation
132 produces.  Currently known output types are:
133
134 =over 4
135
136 =item text
137
138 An implementation with that output type outputs human readable text, making
139 that implementation suitable for C<-text> output in diverse L<openssl(1)>
140 commands.
141
142 =item pem
143
144 An implementation with that output type outputs PEM formatted data.
145
146 =item der
147
148 An implementation with that output type outputs DER formatted data.
149
150 =back
151
152 =item structure
153
154 This property is used to specify the structure that is used for the encoded
155 object.  An example could be C<pkcs8>, to specify explicitly that an object
156 (presumably an asymmetric key pair, in this case) will be wrapped in a
157 PKCS#8 structure as part of the encoding.
158
159 =back
160
161 The possible values of both these properties is open ended.  A provider may
162 very well specify output types and structures that libcrypto doesn't know
163 anything about.
164
165 =head2 Subset selections
166
167 Sometimes, an object has more than one subset of data that is interesting to
168 treat separately or together.  It's possible to specify what subsets are to
169 be encoded, with a set of bits I<selection> that are passed in an B<int>.
170
171 This set of bits depend entirely on what kind of provider-side object is
172 passed.  For example, those bits are assumed to be the same as those used
173 with L<provider-keymgmt(7)> (see L<provider-keymgmt(7)/Key Objects>) when
174 the object is an asymmetric key.
175
176 ENCODER implementations are free to regard the I<selection> as a set of
177 hints, but must do so with care.  In the end, the output must make sense,
178 and if there's a corresponding decoder, the resulting decoded object must
179 match the original object that was encoded.
180
181 =head2 Context functions
182
183 OSSL_FUNC_encoder_newctx() returns a context to be used with the rest of
184 the functions.
185
186 OSSL_FUNC_encoder_freectx() frees the given I<ctx>, if it was created by
187 OSSL_FUNC_encoder_newctx().
188
189 OSSL_FUNC_encoder_set_ctx_params() sets context data according to parameters
190 from I<params> that it recognises.  Unrecognised parameters should be
191 ignored.
192
193 OSSL_FUNC_encoder_settable_ctx_params() returns a constant B<OSSL_PARAM>
194 array describing the parameters that OSSL_FUNC_encoder_set_ctx_params()
195 can handle.
196
197 See L<OSSL_PARAM(3)> for further details on the parameters structure used by
198 OSSL_FUNC_encoder_set_ctx_params() and OSSL_FUNC_encoder_settable_ctx_params().
199
200 =head2 Import functions
201
202 A provider-native object may be associated with a foreign provider, and may
203 therefore be unsuitable for direct use with a given ENCODER implementation.
204 Provided that the foreign provider's implementation to handle the object has
205 a function to export that object in L<OSSL_PARAM(3)> array form, the ENCODER
206 implementation should be able to import that array and create a suitable
207 object to be passed to OSSL_FUNC_encoder_encode()'s I<obj_raw>.
208
209 OSSL_FUNC_encoder_import_object() should import the subset of I<params>
210 given with I<selection> to create a provider-native object that can be
211 passed as I<obj_raw> to OSSL_FUNC_encoder_encode().
212
213 OSSL_FUNC_encoder_free_object() should free the object that was created with
214 OSSL_FUNC_encoder_import_object().
215
216 =head2 Encoding functions
217
218 =for comment There will be a "Decoding functions" title as well
219
220 OSSL_FUNC_encoder_encode() should take an provider-native object (in
221 I<obj_raw>) or an object abstraction (in I<obj_abstract>), and should output
222 the object in encoded form to the B<OSSL_CORE_BIO>.  The I<selection> bits,
223 if relevant, should determine in greater detail what will be output.
224 The encoding functions also take an B<OSSL_PASSPHRASE_CALLBACK> function
225 pointer along with a pointer to application data I<cbarg>, which should be
226 used when a pass phrase prompt is needed.
227
228 =head2 Encoder parameters
229
230 The ENCODER implementation itself has parameters that can be used to
231 determine how it fits in a chain of encoders:
232
233 =over 4
234
235 =item "input-type" (B<OSSL_ENCODER_PARAM_INPUT_TYPE>) <UTF8 string>
236
237 This is used to specify a distinct type name for the object passed as
238 I<obj_raw> to OSSL_FUNC_encoder_encode.
239
240 This parameter is an optional parameter, to be used if the name of the
241 implementation can be ambiguous because of aliases, and something more
242 deterministic is needed.
243
244 =item "output-type" (B<OSSL_ENCODER_PARAM_OUTPUT_TYPE>) <UTF8 string>
245
246 This is used to specify the output type for an ENCODER implementation.
247
248 This parameter is I<mandatory>.
249
250 =for comment If we had functionality to get the value of a specific property
251 in a set of properties, it would be possible to determine the output type
252 from the C<output> property.
253
254 =item "output-structure" (B<OSSL_ENCODER_PARAM_OUTPUT_STRUCTURE>) <UTF8 string>
255
256 This is used to specify the outermost output structure for an ENCODER
257 implementation.
258
259 For example, an output of type "DER" for a key pair could be structured
260 using PKCS#8, or a key type specific structure, such as PKCS#1 for RSA
261 keys.
262
263 =for comment If we had functionality to get the value of a specific property
264 in a set of properties, it would be possible to determine the output
265 structure from the C<structure> property.
266
267 =back
268
269 =head2 Encoder operation parameters
270
271 Operation parameters currently recognised by built-in encoders are as
272 follows:
273
274 =over 4
275
276 =item "cipher" (B<OSSL_ENCODER_PARAM_CIPHER>) <UTF8 string>
277
278 The name of the encryption cipher to be used when generating encrypted
279 encoding.  This is used when encoding private keys, as well as
280 other objects that need protection.
281
282 If this name is invalid for the encoding implementation, the
283 implementation should refuse to perform the encoding, i.e.
284 OSSL_FUNC_encoder_encode_data() and OSSL_FUNC_encoder_encode_object()
285 should return an error.
286
287 =item "properties" (B<OSSL_ENCODER_PARAM_PROPERTIES>) <UTF8 string>
288
289 The properties to be queried when trying to fetch the algorithm given
290 with the "cipher" parameter.
291 This must be given together with the "cipher" parameter to be
292 considered valid.
293
294 The encoding implementation isn't obligated to use this value.
295 However, it is recommended that implementations that do not handle
296 property strings return an error on receiving this parameter unless
297 its value NULL or the empty string.
298
299 =back
300
301 Parameters currently recognised by the built-in pass phrase callback:
302
303 =over 4
304
305 =item "info" (B<OSSL_PASSPHRASE_PARAM_INFO>) <UTF8 string>
306
307 A string of information that will become part of the pass phrase
308 prompt.  This could be used to give the user information on what kind
309 of object it's being prompted for.
310
311 =back
312
313 =head1 RETURN VALUES
314
315 OSSL_FUNC_encoder_newctx() returns a pointer to a context, or NULL on
316 failure.
317
318 OSSL_FUNC_encoder_set_ctx_params() returns 1, unless a recognised
319 parameters was invalid or caused an error, for which 0 is returned.
320
321 OSSL_FUNC_encoder_settable_ctx_params() returns a pointer to an array of
322 constant B<OSSL_PARAM> elements.
323
324 OSSL_FUNC_encoder_encode() return 1 on success, or 0 on failure.
325
326 =head1 SEE ALSO
327
328 L<provider(7)>
329
330 =head1 HISTORY
331
332 The ENCODER interface was introduced in OpenSSL 3.0.
333
334 =head1 COPYRIGHT
335
336 Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
337
338 Licensed under the Apache License 2.0 (the "License").  You may not use
339 this file except in compliance with the License.  You can obtain a copy
340 in the file LICENSE in the source distribution or at
341 L<https://www.openssl.org/source/license.html>.
342
343 =cut