provider-base.pod 30 KB

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