provider-base.pod 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. =pod
  2. =head1 NAME
  3. provider-base
  4. - The basic OpenSSL library E<lt>-E<gt> provider functions
  5. =head1 SYNOPSIS
  6. #include <openssl/core_dispatch.h>
  7. /*
  8. * None of these are actual functions, but are displayed like this for
  9. * the function signatures for functions that are offered as function
  10. * pointers in OSSL_DISPATCH arrays.
  11. */
  12. /* Functions offered by libcrypto to the providers */
  13. const OSSL_ITEM *core_gettable_params(const OSSL_CORE_HANDLE *handle);
  14. int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[]);
  15. int core_thread_start(const OSSL_CORE_HANDLE *handle,
  16. OSSL_thread_stop_handler_fn handfn);
  17. OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle);
  18. void core_new_error(const OSSL_CORE_HANDLE *handle);
  19. void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
  20. const char *file, int line, const char *func);
  21. void core_vset_error(const OSSL_CORE_HANDLE *handle,
  22. uint32_t reason, const char *fmt, va_list args);
  23. /*
  24. * Some OpenSSL functionality is directly offered to providers via
  25. * dispatch
  26. */
  27. void *CRYPTO_malloc(size_t num, const char *file, int line);
  28. void *CRYPTO_zalloc(size_t num, const char *file, int line);
  29. void *CRYPTO_memdup(const void *str, size_t siz,
  30. const char *file, int line);
  31. char *CRYPTO_strdup(const char *str, const char *file, int line);
  32. char *CRYPTO_strndup(const char *str, size_t s,
  33. const char *file, int line);
  34. void CRYPTO_free(void *ptr, const char *file, int line);
  35. void CRYPTO_clear_free(void *ptr, size_t num,
  36. const char *file, int line);
  37. void *CRYPTO_realloc(void *addr, size_t num,
  38. const char *file, int line);
  39. void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
  40. const char *file, int line);
  41. void *CRYPTO_secure_malloc(size_t num, const char *file, int line);
  42. void *CRYPTO_secure_zalloc(size_t num, const char *file, int line);
  43. void CRYPTO_secure_free(void *ptr, const char *file, int line);
  44. void CRYPTO_secure_clear_free(void *ptr, size_t num,
  45. const char *file, int line);
  46. int CRYPTO_secure_allocated(const void *ptr);
  47. void OPENSSL_cleanse(void *ptr, size_t len);
  48. OSSL_CORE_BIO * BIO_new_file(const char *filename, const char *mode)
  49. OSSL_CORE_BIO * BIO_new_membuf(const void *buf, int len)
  50. int BIO_read_ex(OSSL_CORE_BIO *bio, void *data, size_t data_len,
  51. size_t *bytes_read))
  52. int BIO_write_ex(OSSL_CORE_BIO *bio, const void *data, size_t data_len,
  53. size_t *written)
  54. int BIO_free(OSSL_CORE_BIO *bio))
  55. int BIO_vprintf(OSSL_CORE_BIO *bio, const char *format, va_list args)
  56. int BIO_vsnprintf(char *buf, size_t n, const char *fmt, va_list args)
  57. void self_test_cb(OPENSSL_CORE_CTX *ctx, OSSL_CALLBACK **cb, void **cbarg)
  58. size_t get_entropy(const OSSL_CORE_HANDLE *handle,
  59. unsigned char **pout, int entropy,
  60. size_t min_len, size_t max_len)
  61. void cleanup_entropy(const OSSL_CORE_HANDLE *handle,
  62. unsigned char *buf, size_t len)
  63. size_t get_nonce(const OSSL_CORE_HANDLE *handle,
  64. unsigned char **pout, size_t min_len, size_t max_len,
  65. const void *salt, size_t salt_len)
  66. void cleanup_nonce(const OSSL_CORE_HANDLE *handle,
  67. unsigned char *buf, size_t len)
  68. /* Functions offered by the provider to libcrypto */
  69. void provider_teardown(void *provctx);
  70. const OSSL_ITEM *provider_gettable_params(void *provctx);
  71. int provider_get_params(void *provctx, OSSL_PARAM params[]);
  72. const OSSL_ALGORITHM *provider_query_operation(void *provctx,
  73. int operation_id,
  74. const int *no_store);
  75. const OSSL_ITEM *provider_get_reason_strings(void *provctx);
  76. int provider_get_capabilities(void *provctx, const char *capability,
  77. OSSL_CALLBACK *cb, void *arg);
  78. =head1 DESCRIPTION
  79. All "functions" mentioned here are passed as function pointers between
  80. F<libcrypto> and the provider in B<OSSL_DISPATCH> arrays, in the call
  81. of the provider initialization function. See L<provider(7)/Provider>
  82. for a description of the initialization function.
  83. All these "functions" have a corresponding function type definition
  84. named B<OSSL_{name}_fn>, and a helper function to retrieve the
  85. function pointer from a B<OSSL_DISPATCH> element named
  86. B<OSSL_FUNC_{name}>.
  87. For example, the "function" core_gettable_params() has these:
  88. typedef OSSL_PARAM *
  89. (OSSL_FUNC_core_gettable_params_fn)(const OSSL_CORE_HANDLE *handle);
  90. static ossl_inline OSSL_NAME_core_gettable_params_fn
  91. OSSL_FUNC_core_gettable_params(const OSSL_DISPATCH *opf);
  92. B<OSSL_DISPATCH> arrays are indexed by numbers that are provided as
  93. macros in L<openssl-core_dispatch.h(7)>, as follows:
  94. For I<in> (the B<OSSL_DISPATCH> array passed from F<libcrypto> to the
  95. provider):
  96. core_gettable_params OSSL_FUNC_CORE_GETTABLE_PARAMS
  97. core_get_params OSSL_FUNC_CORE_GET_PARAMS
  98. core_thread_start OSSL_FUNC_CORE_THREAD_START
  99. core_get_libctx OSSL_FUNC_CORE_GET_LIBCTX
  100. core_new_error OSSL_FUNC_CORE_NEW_ERROR
  101. core_set_error_debug OSSL_FUNC_CORE_SET_ERROR_DEBUG
  102. core_set_error OSSL_FUNC_CORE_SET_ERROR
  103. CRYPTO_malloc OSSL_FUNC_CRYPTO_MALLOC
  104. CRYPTO_zalloc OSSL_FUNC_CRYPTO_ZALLOC
  105. CRYPTO_memdup OSSL_FUNC_CRYPTO_MEMDUP
  106. CRYPTO_strdup OSSL_FUNC_CRYPTO_STRDUP
  107. CRYPTO_strndup OSSL_FUNC_CRYPTO_STRNDUP
  108. CRYPTO_free OSSL_FUNC_CRYPTO_FREE
  109. CRYPTO_clear_free OSSL_FUNC_CRYPTO_CLEAR_FREE
  110. CRYPTO_realloc OSSL_FUNC_CRYPTO_REALLOC
  111. CRYPTO_clear_realloc OSSL_FUNC_CRYPTO_CLEAR_REALLOC
  112. CRYPTO_secure_malloc OSSL_FUNC_CRYPTO_SECURE_MALLOC
  113. CRYPTO_secure_zalloc OSSL_FUNC_CRYPTO_SECURE_ZALLOC
  114. CRYPTO_secure_free OSSL_FUNC_CRYPTO_SECURE_FREE
  115. CRYPTO_secure_clear_free OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE
  116. CRYPTO_secure_allocated OSSL_FUNC_CRYPTO_SECURE_ALLOCATED
  117. BIO_new_file OSSL_FUNC_BIO_NEW_FILE
  118. BIO_new_mem_buf OSSL_FUNC_BIO_NEW_MEMBUF
  119. BIO_read_ex OSSL_FUNC_BIO_READ_EX
  120. BIO_free OSSL_FUNC_BIO_FREE
  121. BIO_vprintf OSSL_FUNC_BIO_VPRINTF
  122. OPENSSL_cleanse OSSL_FUNC_OPENSSL_CLEANSE
  123. OSSL_SELF_TEST_set_callback OSSL_FUNC_SELF_TEST_CB
  124. ossl_rand_get_entropy OSSL_FUNC_GET_ENTROPY
  125. ossl_rand_cleanup_entropy OSSL_FUNC_CLEANUP_ENTROPY
  126. ossl_rand_get_nonce OSSL_FUNC_GET_NONCE
  127. ossl_rand_cleanup_nonce OSSL_FUNC_CLEANUP_NONCE
  128. For I<*out> (the B<OSSL_DISPATCH> array passed from the provider to
  129. F<libcrypto>):
  130. provider_teardown OSSL_FUNC_PROVIDER_TEARDOWN
  131. provider_gettable_params OSSL_FUNC_PROVIDER_GETTABLE_PARAMS
  132. provider_get_params OSSL_FUNC_PROVIDER_GET_PARAMS
  133. provider_query_operation OSSL_FUNC_PROVIDER_QUERY_OPERATION
  134. provider_get_reason_strings OSSL_FUNC_PROVIDER_GET_REASON_STRINGS
  135. provider_get_capabilities OSSL_FUNC_PROVIDER_GET_CAPABILITIES
  136. provider_self_test OSSL_FUNC_PROVIDER_SELF_TEST
  137. =head2 Core functions
  138. core_gettable_params() returns a constant array of descriptor
  139. B<OSSL_PARAM>, for parameters that core_get_params() can handle.
  140. core_get_params() retrieves parameters from the core for the given I<handle>.
  141. See L</Core parameters> below for a description of currently known
  142. parameters.
  143. =for comment core_thread_start() TBA
  144. core_get_libctx() retrieves the library context in which the library
  145. object for the current provider is stored, accessible through the I<handle>.
  146. This may sometimes be useful if the provider wishes to store a
  147. reference to its context in the same library context.
  148. core_new_error(), core_set_error_debug() and core_set_error() are
  149. building blocks for reporting an error back to the core, with
  150. reference to the I<handle>.
  151. =over 4
  152. =item core_new_error()
  153. allocates a new thread specific error record.
  154. This corresponds to the OpenSSL function L<ERR_new(3)>.
  155. =item core_set_error_debug()
  156. sets debugging information in the current thread specific error
  157. record.
  158. The debugging information includes the name of the file I<file>, the
  159. line I<line> and the function name I<func> where the error occurred.
  160. This corresponds to the OpenSSL function L<ERR_set_debug(3)>.
  161. =item core_set_error()
  162. sets the I<reason> for the error, along with any addition data.
  163. The I<reason> is a number defined by the provider and used to index
  164. the reason strings table that's returned by
  165. provider_get_reason_strings().
  166. The additional data is given as a format string I<fmt> and a set of
  167. arguments I<args>, which are treated in the same manner as with
  168. BIO_vsnprintf().
  169. I<file> and I<line> may also be passed to indicate exactly where the
  170. error occurred or was reported.
  171. This corresponds to the OpenSSL function L<ERR_vset_error(3)>.
  172. =back
  173. CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_memdup(), CRYPTO_strdup(),
  174. CRYPTO_strndup(), CRYPTO_free(), CRYPTO_clear_free(),
  175. CRYPTO_realloc(), CRYPTO_clear_realloc(), CRYPTO_secure_malloc(),
  176. CRYPTO_secure_zalloc(), CRYPTO_secure_free(),
  177. CRYPTO_secure_clear_free(), CRYPTO_secure_allocated(),
  178. BIO_new_file(), BIO_new_mem_buf(), BIO_read_ex(), BIO_free(),
  179. BIO_vprintf(), OPENSSL_cleanse(), and OPENSSL_hexstr2buf()
  180. correspond exactly to the public functions with the same name.
  181. As a matter of fact, the pointers in the B<OSSL_DISPATCH> array are
  182. direct pointers to those public functions. Note that the BIO functions take an
  183. B<OSSL_CORE_BIO> type rather than the standard B<BIO> type. This is to ensure
  184. that a provider does not mix BIOs from the core with BIOs used on the provider
  185. side (the two are not compatible).
  186. OSSL_SELF_TEST_set_callback() is used to set an optional callback that can be
  187. passed into a provider. This may be ignored by a provider.
  188. get_entropy() retrieves seeding material from the operating system.
  189. The seeding material will have at least I<entropy> bytes of randomness and the
  190. output will have at least I<min_len> and at most I<max_len> bytes.
  191. The buffer address is stored in I<*pout> and the buffer length is
  192. returned to the caller. On error, zero is returned.
  193. cleanup_entropy() is used to clean up and free the buffer returned by
  194. get_entropy(). The entropy pointer returned by get_entropy() is passed in
  195. B<buf> and its length in B<len>.
  196. get_nonce() retrieves a nonce using the passed I<salt> parameter
  197. of length I<salt_len> and operating system specific information.
  198. The I<salt> should contain uniquely identifying information and this is
  199. included, in an unspecified manner, as part of the output.
  200. The output is stored in a buffer which contrains at least I<min_len> and at
  201. most I<max_len> bytes. The buffer address is stored in I<*pout> and the
  202. buffer length returned to the caller. On error, zero is returned.
  203. cleanup_nonce() is used to clean up and free the buffer returned by
  204. get_nonce(). The nonce pointer returned by get_nonce() is passed in
  205. B<buf> and its length in B<len>.
  206. =head2 Provider functions
  207. provider_teardown() is called when a provider is shut down and removed
  208. from the core's provider store.
  209. It must free the passed I<provctx>.
  210. provider_gettable_params() should return a constant array of
  211. descriptor B<OSSL_PARAM>, for parameters that provider_get_params()
  212. can handle.
  213. provider_get_params() should process the B<OSSL_PARAM> array
  214. I<params>, setting the values of the parameters it understands.
  215. provider_query_operation() should return a constant B<OSSL_ALGORITHM>
  216. that corresponds to the given I<operation_id>.
  217. It should indicate if the core may store a reference to this array by
  218. setting I<*no_store> to 0 (core may store a reference) or 1 (core may
  219. not store a reference).
  220. provider_get_reason_strings() should return a constant B<OSSL_ITEM>
  221. array that provides reason strings for reason codes the provider may
  222. use when reporting errors using core_put_error().
  223. The provider_get_capabilities() function should call the callback I<cb> passing
  224. it a set of B<OSSL_PARAM>s and the caller supplied argument I<arg>. The
  225. B<OSSL_PARAM>s should provide details about the capability with the name given
  226. in the I<capability> argument relevant for the provider context I<provctx>. If a
  227. provider supports multiple capabilities with the given name then it may call the
  228. callback multiple times (one for each capability). Capabilities can be useful for
  229. describing the services that a provider can offer. For further details see the
  230. L</CAPABILITIES> section below. It should return 1 on success or 0 on error.
  231. The provider_self_test() function should perform known answer tests on a subset
  232. of the algorithms that it uses, and may also verify the integrity of the
  233. provider module. It should return 1 on success or 0 on error. It will return 1
  234. if this function is not used.
  235. None of these functions are mandatory, but a provider is fairly
  236. useless without at least provider_query_operation(), and
  237. provider_gettable_params() is fairly useless if not accompanied by
  238. provider_get_params().
  239. =head2 Provider parameters
  240. provider_get_params() can return the following provider parameters to the core:
  241. =over 4
  242. =item "name" (B<OSSL_PROV_PARAM_NAME>) <UTF8_ptr>
  243. This points to a string that should give a unique name for the provider.
  244. =item "version" (B<OSSL_PROV_PARAM_VERSION>) <UTF8_ptr>
  245. This points to a string that is a version number associated with this provider.
  246. OpenSSL in-built providers use OPENSSL_VERSION_STR, but this may be different
  247. for any third party provider. This string is for informational purposes only.
  248. =item "buildinfo" (B<OSSL_PROV_PARAM_BUILDINFO>) <UTF8_ptr>
  249. This points to a string that is a build information associated with this provider.
  250. OpenSSL in-built providers use OPENSSL_FULL_VERSION_STR, but this may be
  251. different for any third party provider.
  252. =item "status" (B<OSSL_PROV_PARAM_STATUS>) <unsigned integer>
  253. This returns 0 if the provider has entered an error state, otherwise it returns
  254. 1.
  255. =back
  256. provider_gettable_params() should return the above parameters.
  257. =head2 Core parameters
  258. core_get_params() can retrieve the following core parameters for each provider:
  259. =over 4
  260. =item "openssl-version" (B<OSSL_PROV_PARAM_CORE_VERSION>) <UTF8_ptr>
  261. This points to the OpenSSL libraries' full version string, i.e. the string
  262. expanded from the macro B<OPENSSL_VERSION_STR>.
  263. =item "provider-name" (B<OSSL_PROV_PARAM_CORE_PROV_NAME>) <UTF8_ptr>
  264. This points to the OpenSSL libraries' idea of what the calling provider is named.
  265. =item "module-filename" (B<OSSL_PROV_PARAM_CORE_MODULE_FILENAME>) <UTF8_ptr>
  266. This points to a string containing the full filename of the providers
  267. module file.
  268. =back
  269. Additionally, provider specific configuration parameters from the
  270. config file are available, in dotted name form.
  271. The dotted name form is a concatenation of section names and final
  272. config command name separated by periods.
  273. For example, let's say we have the following config example:
  274. openssl_conf = openssl_init
  275. [openssl_init]
  276. providers = providers_sect
  277. [providers_sect]
  278. foo = foo_sect
  279. [foo_sect]
  280. activate = 1
  281. data1 = 2
  282. data2 = str
  283. more = foo_more
  284. [foo_more]
  285. data3 = foo,bar
  286. The provider will have these additional parameters available:
  287. =over 4
  288. =item "activate"
  289. pointing at the string "1"
  290. =item "data1"
  291. pointing at the string "2"
  292. =item "data2"
  293. pointing at the string "str"
  294. =item "more.data3"
  295. pointing at the string "foo,bar"
  296. =back
  297. For more information on handling parameters, see L<OSSL_PARAM(3)> as
  298. L<OSSL_PARAM_int(3)>.
  299. =head1 CAPABILITIES
  300. Capabilities describe some of the services that a provider can offer.
  301. Applications can query the capabilities to discover those services.
  302. =head3 "TLS-GROUP" Capability
  303. The "TLS-GROUP" capability can be queried by libssl to discover the list of
  304. TLS groups that a provider can support. Each group supported can be used for
  305. I<key exchange> (KEX) or I<key encapsulation method> (KEM) during a TLS
  306. handshake.
  307. TLS clients can advertise the list of TLS groups they support in the
  308. supported_groups extension, and TLS servers can select a group from the offered
  309. list that they also support. In this way a provider can add to the list of
  310. groups that libssl already supports with additional ones.
  311. Each TLS group that a provider supports should be described via the callback
  312. passed in through the provider_get_capabilities function. Each group should have
  313. the following details supplied (all are mandatory, except
  314. B<OSSL_CAPABILITY_TLS_GROUP_IS_KEM>):
  315. =over 4
  316. =item "tls-group-name" (B<OSSL_CAPABILITY_TLS_GROUP_NAME>) <utf8 string>
  317. The name of the group as given in the IANA TLS Supported Groups registry
  318. L<https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8>.
  319. =item "tls-group-name-internal" (B<OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL>) <utf8 string>
  320. The name of the group as known by the provider. This could be the same as the
  321. "tls-group-name", but does not have to be.
  322. =item "tls-group-id" (B<OSSL_CAPABILITY_TLS_GROUP_ID>) <unsigned integer>
  323. The TLS group id value as given in the IANA TLS Supported Groups registry.
  324. =item "tls-group-alg" (B<OSSL_CAPABILITY_TLS_GROUP_ALG>) <utf8 string>
  325. The name of a Key Management algorithm that the provider offers and that should
  326. be used with this group. Keys created should be able to support I<key exchange>
  327. or I<key encapsulation method> (KEM), as implied by the optional
  328. B<OSSL_CAPABILITY_TLS_GROUP_IS_KEM> flag.
  329. The algorithm must support key and parameter generation as well as the
  330. key/parameter generation parameter, B<OSSL_PKEY_PARAM_GROUP_NAME>. The group
  331. name given via "tls-group-name-internal" above will be passed via
  332. B<OSSL_PKEY_PARAM_GROUP_NAME> when libssl wishes to generate keys/parameters.
  333. =item "tls-group-sec-bits" (B<OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS>) <unsigned integer>
  334. The number of bits of security offered by keys in this group. The number of bits
  335. should be comparable with the ones given in table 2 and 3 of the NIST SP800-57
  336. document.
  337. =item "tls-group-is-kem" (B<OSSL_CAPABILITY_TLS_GROUP_IS_KEM>) <unsigned integer>
  338. Boolean flag to describe if the group should be used in I<key exchange> (KEX)
  339. mode (0, default) or in I<key encapsulation method> (KEM) mode (1).
  340. This parameter is optional: if not specified, KEX mode is assumed as the default
  341. mode for the group.
  342. In KEX mode, in a typical Diffie-Hellman fashion, both sides execute I<keygen>
  343. then I<derive> against the peer public key. To operate in KEX mode, the group
  344. implementation must support the provider functions as described in
  345. L<provider-keyexch(7)>.
  346. In KEM mode, the client executes I<keygen> and sends its public key, the server
  347. executes I<encapsulate> using the client's public key and sends back the
  348. resulting I<ciphertext>, finally the client executes I<decapsulate> to retrieve
  349. the same I<shared secret> generated by the server's I<encapsulate>. To operate
  350. in KEM mode, the group implementation must support the provider functions as
  351. described in L<provider-kem(7)>.
  352. Both in KEX and KEM mode, the resulting I<shared secret> is then used according
  353. to the protocol specification.
  354. =item "tls-min-tls" (B<OSSL_CAPABILITY_TLS_GROUP_MIN_TLS>) <integer>
  355. =item "tls-max-tls" (B<OSSL_CAPABILITY_TLS_GROUP_MAX_TLS>) <integer>
  356. =item "tls-min-dtls" (B<OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS>) <integer>
  357. =item "tls-max-dtls" (B<OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS>) <integer>
  358. These parameters can be used to describe the minimum and maximum TLS and DTLS
  359. versions supported by the group. The values equate to the on-the-wire encoding
  360. of the various TLS versions. For example TLSv1.3 is 0x0304 (772 decimal), and
  361. TLSv1.2 is 0x0303 (771 decimal). A 0 indicates that there is no defined minimum
  362. or maximum. A -1 indicates that the group should not be used in that protocol.
  363. =back
  364. =head1 EXAMPLES
  365. This is an example of a simple provider made available as a
  366. dynamically loadable module.
  367. It implements the fictitious algorithm C<FOO> for the fictitious
  368. operation C<BAR>.
  369. #include <malloc.h>
  370. #include <openssl/core.h>
  371. #include <openssl/core_dispatch.h>
  372. /* Errors used in this provider */
  373. #define E_MALLOC 1
  374. static const OSSL_ITEM reasons[] = {
  375. { E_MALLOC, "memory allocation failure" }.
  376. { 0, NULL } /* Termination */
  377. };
  378. /*
  379. * To ensure we get the function signature right, forward declare
  380. * them using function types provided by openssl/core_dispatch.h
  381. */
  382. OSSL_FUNC_bar_newctx_fn foo_newctx;
  383. OSSL_FUNC_bar_freectx_fn foo_freectx;
  384. OSSL_FUNC_bar_init_fn foo_init;
  385. OSSL_FUNC_bar_update_fn foo_update;
  386. OSSL_FUNC_bar_final_fn foo_final;
  387. OSSL_FUNC_provider_query_operation_fn p_query;
  388. OSSL_FUNC_provider_get_reason_strings_fn p_reasons;
  389. OSSL_FUNC_provider_teardown_fn p_teardown;
  390. OSSL_provider_init_fn OSSL_provider_init;
  391. OSSL_FUNC_core_put_error *c_put_error = NULL;
  392. /* Provider context */
  393. struct prov_ctx_st {
  394. OSSL_CORE_HANDLE *handle;
  395. }
  396. /* operation context for the algorithm FOO */
  397. struct foo_ctx_st {
  398. struct prov_ctx_st *provctx;
  399. int b;
  400. };
  401. static void *foo_newctx(void *provctx)
  402. {
  403. struct foo_ctx_st *fooctx = malloc(sizeof(*fooctx));
  404. if (fooctx != NULL)
  405. fooctx->provctx = provctx;
  406. else
  407. c_put_error(provctx->handle, E_MALLOC, __FILE__, __LINE__);
  408. return fooctx;
  409. }
  410. static void foo_freectx(void *fooctx)
  411. {
  412. free(fooctx);
  413. }
  414. static int foo_init(void *vfooctx)
  415. {
  416. struct foo_ctx_st *fooctx = vfooctx;
  417. fooctx->b = 0x33;
  418. }
  419. static int foo_update(void *vfooctx, unsigned char *in, size_t inl)
  420. {
  421. struct foo_ctx_st *fooctx = vfooctx;
  422. /* did you expect something serious? */
  423. if (inl == 0)
  424. return 1;
  425. for (; inl-- > 0; in++)
  426. *in ^= fooctx->b;
  427. return 1;
  428. }
  429. static int foo_final(void *vfooctx)
  430. {
  431. struct foo_ctx_st *fooctx = vfooctx;
  432. fooctx->b = 0x66;
  433. }
  434. static const OSSL_DISPATCH foo_fns[] = {
  435. { OSSL_FUNC_BAR_NEWCTX, (void (*)(void))foo_newctx },
  436. { OSSL_FUNC_BAR_FREECTX, (void (*)(void))foo_freectx },
  437. { OSSL_FUNC_BAR_INIT, (void (*)(void))foo_init },
  438. { OSSL_FUNC_BAR_UPDATE, (void (*)(void))foo_update },
  439. { OSSL_FUNC_BAR_FINAL, (void (*)(void))foo_final },
  440. { 0, NULL }
  441. };
  442. static const OSSL_ALGORITHM bars[] = {
  443. { "FOO", "provider=chumbawamba", foo_fns },
  444. { NULL, NULL, NULL }
  445. };
  446. static const OSSL_ALGORITHM *p_query(void *provctx, int operation_id,
  447. int *no_store)
  448. {
  449. switch (operation_id) {
  450. case OSSL_OP_BAR:
  451. return bars;
  452. }
  453. return NULL;
  454. }
  455. static const OSSL_ITEM *p_reasons(void *provctx)
  456. {
  457. return reasons;
  458. }
  459. static void p_teardown(void *provctx)
  460. {
  461. free(provctx);
  462. }
  463. static const OSSL_DISPATCH prov_fns[] = {
  464. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))p_teardown },
  465. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))p_query },
  466. { OSSL_FUNC_PROVIDER_GET_REASON_STRINGS, (void (*)(void))p_reasons },
  467. { 0, NULL }
  468. };
  469. int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
  470. const OSSL_DISPATCH *in,
  471. const OSSL_DISPATCH **out,
  472. void **provctx)
  473. {
  474. struct prov_ctx_st *pctx = NULL;
  475. for (; in->function_id != 0; in++)
  476. switch (in->function_id) {
  477. case OSSL_FUNC_CORE_PUT_ERROR:
  478. c_put_error = OSSL_FUNC_core_put_error(in);
  479. break;
  480. }
  481. *out = prov_fns;
  482. if ((pctx = malloc(sizeof(*pctx))) == NULL) {
  483. /*
  484. * ALEA IACTA EST, if the core retrieves the reason table
  485. * regardless, that string will be displayed, otherwise not.
  486. */
  487. c_put_error(handle, E_MALLOC, __FILE__, __LINE__);
  488. return 0;
  489. }
  490. pctx->handle = handle;
  491. return 1;
  492. }
  493. This relies on a few things existing in F<openssl/core_dispatch.h>:
  494. #define OSSL_OP_BAR 4711
  495. #define OSSL_FUNC_BAR_NEWCTX 1
  496. typedef void *(OSSL_FUNC_bar_newctx_fn)(void *provctx);
  497. static ossl_inline OSSL_FUNC_bar_newctx(const OSSL_DISPATCH *opf)
  498. { return (OSSL_FUNC_bar_newctx_fn *)opf->function; }
  499. #define OSSL_FUNC_BAR_FREECTX 2
  500. typedef void (OSSL_FUNC_bar_freectx_fn)(void *ctx);
  501. static ossl_inline OSSL_FUNC_bar_newctx(const OSSL_DISPATCH *opf)
  502. { return (OSSL_FUNC_bar_freectx_fn *)opf->function; }
  503. #define OSSL_FUNC_BAR_INIT 3
  504. typedef void *(OSSL_FUNC_bar_init_fn)(void *ctx);
  505. static ossl_inline OSSL_FUNC_bar_init(const OSSL_DISPATCH *opf)
  506. { return (OSSL_FUNC_bar_init_fn *)opf->function; }
  507. #define OSSL_FUNC_BAR_UPDATE 4
  508. typedef void *(OSSL_FUNC_bar_update_fn)(void *ctx,
  509. unsigned char *in, size_t inl);
  510. static ossl_inline OSSL_FUNC_bar_update(const OSSL_DISPATCH *opf)
  511. { return (OSSL_FUNC_bar_update_fn *)opf->function; }
  512. #define OSSL_FUNC_BAR_FINAL 5
  513. typedef void *(OSSL_FUNC_bar_final_fn)(void *ctx);
  514. static ossl_inline OSSL_FUNC_bar_final(const OSSL_DISPATCH *opf)
  515. { return (OSSL_FUNC_bar_final_fn *)opf->function; }
  516. =head1 SEE ALSO
  517. L<provider(7)>
  518. =head1 HISTORY
  519. The concept of providers and everything surrounding them was
  520. introduced in OpenSSL 3.0.
  521. =head1 COPYRIGHT
  522. Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
  523. Licensed under the Apache License 2.0 (the "License"). You may not use
  524. this file except in compliance with the License. You can obtain a copy
  525. in the file LICENSE in the source distribution or at
  526. L<https://www.openssl.org/source/license.html>.
  527. =cut