provider-base.pod 40 KB

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