provider-encoder.pod 13 KB

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