provider-encoder.pod 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 OSSL_FUNC_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 *OSSL_FUNC_encoder_import_object(void *ctx, int selection,
  30. const OSSL_PARAM params[]);
  31. void OSSL_FUNC_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_mem(3)> B<BIO>.
  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 L<OSSL_DISPATCH(3)> arrays via
  65. L<OSSL_ALGORITHM(3)> 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_FUNC_{name}_fn>, and a helper function to retrieve the
  70. function pointer from an L<OSSL_DISPATCH(3)> element named
  71. B<OSSL_FUNC_{name}>.
  72. For example, the "function" OSSL_FUNC_encoder_encode() 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(const OSSL_DISPATCH *opf);
  81. L<OSSL_DISPATCH(3)> 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
  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 used to further specify details about an implementation:
  98. =over 4
  99. =item output
  100. This property is used to specify what type of output the implementation
  101. produces.
  102. This property is I<mandatory>.
  103. OpenSSL providers recognize the following output types:
  104. =over 4
  105. =item text
  106. An implementation with that output type outputs human readable text, making
  107. that implementation suitable for C<-text> output in diverse L<openssl(1)>
  108. commands.
  109. =item pem
  110. An implementation with that output type outputs PEM formatted data.
  111. =item der
  112. An implementation with that output type outputs DER formatted data.
  113. =item msblob
  114. An implementation with that output type outputs MSBLOB formatted data.
  115. =item pvk
  116. An implementation with that output type outputs PVK formatted data.
  117. =back
  118. =item structure
  119. This property is used to specify the structure that is used for the encoded
  120. object. An example could be C<pkcs8>, to specify explicitly that an object
  121. (presumably an asymmetric key pair, in this case) will be wrapped in a
  122. PKCS#8 structure as part of the encoding.
  123. This property is I<optional>.
  124. =back
  125. The possible values of both these properties is open ended. A provider may
  126. very well specify output types and structures that libcrypto doesn't know
  127. anything about.
  128. =head2 Subset selections
  129. Sometimes, an object has more than one subset of data that is interesting to
  130. treat separately or together. It's possible to specify what subsets are to
  131. be encoded, with a set of bits I<selection> that are passed in an B<int>.
  132. This set of bits depend entirely on what kind of provider-side object is
  133. passed. For example, those bits are assumed to be the same as those used
  134. with L<provider-keymgmt(7)> (see L<provider-keymgmt(7)/Key Objects>) when
  135. the object is an asymmetric keypair.
  136. ENCODER implementations are free to regard the I<selection> as a set of
  137. hints, but must do so with care. In the end, the output must make sense,
  138. and if there's a corresponding decoder, the resulting decoded object must
  139. match the original object that was encoded.
  140. OSSL_FUNC_encoder_does_selection() should tell if a particular implementation
  141. supports any of the combinations given by I<selection>.
  142. =head2 Context functions
  143. OSSL_FUNC_encoder_newctx() returns a context to be used with the rest of
  144. the functions.
  145. OSSL_FUNC_encoder_freectx() frees the given I<ctx>, if it was created by
  146. OSSL_FUNC_encoder_newctx().
  147. OSSL_FUNC_encoder_set_ctx_params() sets context data according to parameters
  148. from I<params> that it recognises. Unrecognised parameters should be
  149. ignored.
  150. Passing NULL for I<params> should return true.
  151. OSSL_FUNC_encoder_settable_ctx_params() returns a constant L<OSSL_PARAM(3)>
  152. array describing the parameters that OSSL_FUNC_encoder_set_ctx_params()
  153. can handle.
  154. See L<OSSL_PARAM(3)> for further details on the parameters structure used by
  155. OSSL_FUNC_encoder_set_ctx_params() and OSSL_FUNC_encoder_settable_ctx_params().
  156. =head2 Import functions
  157. A provider-native object may be associated with a foreign provider, and may
  158. therefore be unsuitable for direct use with a given ENCODER implementation.
  159. Provided that the foreign provider's implementation to handle the object has
  160. a function to export that object in L<OSSL_PARAM(3)> array form, the ENCODER
  161. implementation should be able to import that array and create a suitable
  162. object to be passed to OSSL_FUNC_encoder_encode()'s I<obj_raw>.
  163. OSSL_FUNC_encoder_import_object() should import the subset of I<params>
  164. given with I<selection> to create a provider-native object that can be
  165. passed as I<obj_raw> to OSSL_FUNC_encoder_encode().
  166. OSSL_FUNC_encoder_free_object() should free the object that was created with
  167. OSSL_FUNC_encoder_import_object().
  168. =head2 Encoding functions
  169. OSSL_FUNC_encoder_encode() should take a provider-native object (in
  170. I<obj_raw>) or an object abstraction (in I<obj_abstract>), and should output
  171. the object in encoded form to the B<OSSL_CORE_BIO>. The I<selection> bits,
  172. if relevant, should determine in greater detail what will be output.
  173. The encoding functions also take an L<OSSL_PASSPHRASE_CALLBACK(3)> function
  174. pointer along with a pointer to application data I<cbarg>, which should be
  175. used when a pass phrase prompt is needed.
  176. =head2 Encoder operation parameters
  177. Operation parameters currently recognised by built-in encoders are as
  178. follows:
  179. =over 4
  180. =item "cipher" (B<OSSL_ENCODER_PARAM_CIPHER>) <UTF8 string>
  181. The name of the encryption cipher to be used when generating encrypted
  182. encoding. This is used when encoding private keys, as well as
  183. other objects that need protection.
  184. If this name is invalid for the encoding implementation, the
  185. implementation should refuse to perform the encoding, i.e.
  186. OSSL_FUNC_encoder_encode_data() and OSSL_FUNC_encoder_encode_object()
  187. should return an error.
  188. =item "properties" (B<OSSL_ENCODER_PARAM_PROPERTIES>) <UTF8 string>
  189. The properties to be queried when trying to fetch the algorithm given
  190. with the "cipher" parameter.
  191. This must be given together with the "cipher" parameter to be
  192. considered valid.
  193. The encoding implementation isn't obligated to use this value.
  194. However, it is recommended that implementations that do not handle
  195. property strings return an error on receiving this parameter unless
  196. its value NULL or the empty string.
  197. =item "save-parameters" (B<OSSL_ENCODER_PARAM_SAVE_PARAMETERS>) <integer>
  198. If set to 0 disables saving of key domain parameters. Default is 1.
  199. It currently has an effect only on DSA keys.
  200. =back
  201. Parameters currently recognised by the built-in pass phrase callback:
  202. =over 4
  203. =item "info" (B<OSSL_PASSPHRASE_PARAM_INFO>) <UTF8 string>
  204. A string of information that will become part of the pass phrase
  205. prompt. This could be used to give the user information on what kind
  206. of object it's being prompted for.
  207. =back
  208. =head1 RETURN VALUES
  209. OSSL_FUNC_encoder_newctx() returns a pointer to a context, or NULL on
  210. failure.
  211. OSSL_FUNC_encoder_set_ctx_params() returns 1, unless a recognised
  212. parameter was invalid or caused an error, for which 0 is returned.
  213. OSSL_FUNC_encoder_settable_ctx_params() returns a pointer to an array of
  214. constant L<OSSL_PARAM(3)> elements.
  215. OSSL_FUNC_encoder_does_selection() returns 1 if the encoder implementation
  216. supports any of the I<selection> bits, otherwise 0.
  217. OSSL_FUNC_encoder_encode() returns 1 on success, or 0 on failure.
  218. =head1 SEE ALSO
  219. L<provider(7)>
  220. =head1 HISTORY
  221. The ENCODER interface was introduced in OpenSSL 3.0.
  222. =head1 COPYRIGHT
  223. Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  224. Licensed under the Apache License 2.0 (the "License"). You may not use
  225. this file except in compliance with the License. You can obtain a copy
  226. in the file LICENSE in the source distribution or at
  227. L<https://www.openssl.org/source/license.html>.
  228. =cut