2
0

provider-base.pod 32 KB

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