provider-base.pod 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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_numbers.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_PROVIDER *prov);
  14. int core_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[]);
  15. int core_thread_start(const OSSL_PROVIDER *prov,
  16. OSSL_thread_stop_handler_fn handfn);
  17. OPENSSL_CTX *core_get_library_context(const OSSL_PROVIDER *prov);
  18. void core_new_error(const OSSL_PROVIDER *prov);
  19. void core_set_error_debug(const OSSL_PROVIDER *prov,
  20. const char *file, int line, const char *func);
  21. void core_vset_error(const OSSL_PROVIDER *prov,
  22. uint32_t reason, const char *fmt, va_list args);
  23. /*
  24. * Some OpenSSL functionality is directly offered to providers via
  25. * dispatch
  26. */
  27. void *CRYPTO_malloc(size_t num, const char *file, int line);
  28. void *CRYPTO_zalloc(size_t num, const char *file, int line);
  29. void *CRYPTO_memdup(const void *str, size_t siz,
  30. const char *file, int line);
  31. char *CRYPTO_strdup(const char *str, const char *file, int line);
  32. char *CRYPTO_strndup(const char *str, size_t s,
  33. const char *file, int line);
  34. void CRYPTO_free(void *ptr, const char *file, int line);
  35. void CRYPTO_clear_free(void *ptr, size_t num,
  36. const char *file, int line);
  37. void *CRYPTO_realloc(void *addr, size_t num,
  38. const char *file, int line);
  39. void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
  40. const char *file, int line);
  41. void *CRYPTO_secure_malloc(size_t num, const char *file, int line);
  42. void *CRYPTO_secure_zalloc(size_t num, const char *file, int line);
  43. void CRYPTO_secure_free(void *ptr, const char *file, int line);
  44. void CRYPTO_secure_clear_free(void *ptr, size_t num,
  45. const char *file, int line);
  46. int CRYPTO_secure_allocated(const void *ptr);
  47. void OPENSSL_cleanse(void *ptr, size_t len);
  48. unsigned char *OPENSSL_hexstr2buf(const char *str, long *len);
  49. /* Functions offered by the provider to libcrypto */
  50. void provider_teardown(void *provctx);
  51. const OSSL_ITEM *provider_gettable_params(void *provctx);
  52. int provider_get_params(void *provctx, OSSL_PARAM params[]);
  53. const OSSL_ALGORITHM *provider_query_operation(void *provctx,
  54. int operation_id,
  55. const int *no_store);
  56. const OSSL_ITEM *provider_get_reason_strings(void *provctx);
  57. =head1 DESCRIPTION
  58. All "functions" mentioned here are passed as function pointers between
  59. F<libcrypto> and the provider in B<OSSL_DISPATCH> arrays, in the call
  60. of the provider initialization function. See L<provider(7)/Provider>
  61. for a description of the initialization function.
  62. All these "functions" have a corresponding function type definition
  63. named B<OSSL_{name}_fn>, and a helper function to retrieve the
  64. function pointer from a B<OSSL_DISPATCH> element named
  65. B<OSSL_get_{name}>.
  66. For example, the "function" core_gettable_params() has these:
  67. typedef OSSL_ITEM *
  68. (OSSL_core_gettable_params_fn)(const OSSL_PROVIDER *prov);
  69. static ossl_inline OSSL_NAME_core_gettable_params_fn
  70. OSSL_get_core_gettable_params(const OSSL_DISPATCH *opf);
  71. B<OSSL_DISPATCH> arrays are indexed by numbers that are provided as
  72. macros in L<openssl-core_numbers.h(7)>, as follows:
  73. For I<in> (the B<OSSL_DISPATCH> array passed from F<libcrypto> to the
  74. provider):
  75. core_gettable_params OSSL_FUNC_CORE_GETTABLE_PARAMS
  76. core_get_params OSSL_FUNC_CORE_GET_PARAMS
  77. core_thread_start OSSL_FUNC_CORE_THREAD_START
  78. core_get_library_context OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT
  79. core_new_error OSSL_FUNC_CORE_NEW_ERROR
  80. core_set_error_debug OSSL_FUNC_CORE_SET_ERROR_DEBUG
  81. core_set_error OSSL_FUNC_CORE_SET_ERROR
  82. CRYPTO_malloc OSSL_FUNC_CRYPTO_MALLOC
  83. CRYPTO_zalloc OSSL_FUNC_CRYPTO_ZALLOC
  84. CRYPTO_memdup OSSL_FUNC_CRYPTO_MEMDUP
  85. CRYPTO_strdup OSSL_FUNC_CRYPTO_STRDUP
  86. CRYPTO_strndup OSSL_FUNC_CRYPTO_STRNDUP
  87. CRYPTO_free OSSL_FUNC_CRYPTO_FREE
  88. CRYPTO_clear_free OSSL_FUNC_CRYPTO_CLEAR_FREE
  89. CRYPTO_realloc OSSL_FUNC_CRYPTO_REALLOC
  90. CRYPTO_clear_realloc OSSL_FUNC_CRYPTO_CLEAR_REALLOC
  91. CRYPTO_secure_malloc OSSL_FUNC_CRYPTO_SECURE_MALLOC
  92. CRYPTO_secure_zalloc OSSL_FUNC_CRYPTO_SECURE_ZALLOC
  93. CRYPTO_secure_free OSSL_FUNC_CRYPTO_SECURE_FREE
  94. CRYPTO_secure_clear_free OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE
  95. CRYPTO_secure_allocated OSSL_FUNC_CRYPTO_SECURE_ALLOCATED
  96. BIO_new_file OSSL_FUNC_BIO_NEW_FILE
  97. BIO_new_mem_buf OSSL_FUNC_BIO_NEW_MEMBUF
  98. BIO_read_ex OSSL_FUNC_BIO_READ_EX
  99. BIO_free OSSL_FUNC_BIO_FREE
  100. BIO_vprintf OSSL_FUNC_BIO_VPRINTF
  101. OPENSSL_cleanse OSSL_FUNC_OPENSSL_CLEANSE
  102. OPENSSL_hexstr2buf OSSL_FUNC_OPENSSL_HEXSTR2BUF
  103. OSSL_SELF_TEST_set_callback OSSL_FUNC_SELF_TEST_CB
  104. For I<*out> (the B<OSSL_DISPATCH> array passed from the provider to
  105. F<libcrypto>):
  106. provider_teardown OSSL_FUNC_PROVIDER_TEARDOWN
  107. provider_gettable_params OSSL_FUNC_PROVIDER_GETTABLE_PARAMS
  108. provider_get_params OSSL_FUNC_PROVIDER_GET_PARAMS
  109. provider_query_operation OSSL_FUNC_PROVIDER_QUERY_OPERATION
  110. provider_get_reason_strings OSSL_FUNC_PROVIDER_GET_REASON_STRINGS
  111. =head2 Core functions
  112. core_gettable_params() returns a constant array of descriptor
  113. B<OSSL_PARAM>, for parameters that core_get_params() can handle.
  114. core_get_params() retrieves I<prov> parameters from the core.
  115. See L</Core parameters> below for a description of currently known
  116. parameters.
  117. =for comment core_thread_start() TBA
  118. core_get_library_context() retrieves the library context in which the
  119. B<OSSL_PROVIDER> object I<prov> is stored.
  120. This may sometimes be useful if the provider wishes to store a
  121. reference to its context in the same library context.
  122. core_new_error(), core_set_error_debug() and core_set_error() are
  123. building blocks for reporting an error back to the core, with
  124. reference to the provider object I<prov>.
  125. =over 4
  126. =item core_new_error()
  127. allocates a new thread specific error record.
  128. This corresponds to the OpenSSL function L<ERR_new(3)>.
  129. =item core_set_error_debug()
  130. sets debugging information in the current thread specific error
  131. record.
  132. The debugging information includes the name of the file I<file>, the
  133. line I<line> and the function name I<func> where the error occurred.
  134. This corresponds to the OpenSSL function L<ERR_set_debug(3)>.
  135. =item core_set_error()
  136. sets the I<reason> for the error, along with any addition data.
  137. The I<reason> is a number defined by the provider and used to index
  138. the reason strings table that's returned by
  139. provider_get_reason_strings().
  140. The additional data is given as a format string I<fmt> and a set of
  141. arguments I<args>, which are treated in the same manner as with
  142. BIO_vsnprintf().
  143. I<file> and I<line> may also be passed to indicate exactly where the
  144. error occurred or was reported.
  145. This corresponds to the OpenSSL function L<ERR_vset_error(3)>.
  146. =back
  147. CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_memdup(), CRYPTO_strdup(),
  148. CRYPTO_strndup(), CRYPTO_free(), CRYPTO_clear_free(),
  149. CRYPTO_realloc(), CRYPTO_clear_realloc(), CRYPTO_secure_malloc(),
  150. CRYPTO_secure_zalloc(), CRYPTO_secure_free(),
  151. CRYPTO_secure_clear_free(), CRYPTO_secure_allocated(),
  152. BIO_new_file(), BIO_new_mem_buf(), BIO_read_ex(), BIO_free(),
  153. BIO_vprintf(), OPENSSL_cleanse(), and OPENSSL_hexstr2buf()
  154. correspond exactly to the public functions with the same name.
  155. As a matter of fact, the pointers in the B<OSSL_DISPATCH> array are
  156. direct pointers to those public functions.
  157. OSSL_SELF_TEST_set_callback() is used to set an optional callback that can be
  158. passed into a provider. This may be ignored by a provider.
  159. =head2 Provider functions
  160. provider_teardown() is called when a provider is shut down and removed
  161. from the core's provider store.
  162. It must free the passed I<provctx>.
  163. provider_gettable_params() should return a constant array of
  164. descriptor B<OSSL_PARAM>, for parameters that provider_get_params()
  165. can handle.
  166. provider_get_params() should process the B<OSSL_PARAM> array
  167. I<params>, setting the values of the parameters it understands.
  168. provider_query_operation() should return a constant B<OSSL_ALGORITHM>
  169. that corresponds to the given I<operation_id>.
  170. It should indicate if the core may store a reference to this array by
  171. setting I<*no_store> to 0 (core may store a reference) or 1 (core may
  172. not store a reference).
  173. provider_get_reason_strings() should return a constant B<OSSL_ITEM>
  174. array that provides reason strings for reason codes the provider may
  175. use when reporting errors using core_put_error().
  176. None of these functions are mandatory, but a provider is fairly
  177. useless without at least provider_query_operation(), and
  178. provider_gettable_params() is fairly useless if not accompanied by
  179. provider_get_params().
  180. =head2 Core parameters
  181. core_get_params() understands the following known parameters:
  182. =over 4
  183. =item "openssl-version"
  184. This is a B<OSSL_PARAM_UTF8_PTR> type of parameter, pointing at the
  185. OpenSSL libraries' full version string, i.e. the string expanded from
  186. the macro B<OPENSSL_VERSION_STR>.
  187. =item "provider-name"
  188. This is a B<OSSL_PARAM_UTF8_PTR> type of parameter, pointing at the
  189. OpenSSL libraries' idea of what the calling provider is called.
  190. =back
  191. Additionally, provider specific configuration parameters from the
  192. config file are available, in dotted name form.
  193. The dotted name form is a concatenation of section names and final
  194. config command name separated by periods.
  195. For example, let's say we have the following config example:
  196. openssl_conf = openssl_init
  197. [openssl_init]
  198. providers = providers_sect
  199. [providers_sect]
  200. foo = foo_sect
  201. [foo_sect]
  202. activate = 1
  203. data1 = 2
  204. data2 = str
  205. more = foo_more
  206. [foo_more]
  207. data3 = foo,bar
  208. The provider will have these additional parameters available:
  209. =over 4
  210. =item "activate"
  211. pointing at the string "1"
  212. =item "data1"
  213. pointing at the string "2"
  214. =item "data2"
  215. pointing at the string "str"
  216. =item "more.data3"
  217. pointing at the string "foo,bar"
  218. =back
  219. For more information on handling parameters, see L<OSSL_PARAM(3)> as
  220. L<OSSL_PARAM_int(3)>.
  221. =head1 EXAMPLES
  222. This is an example of a simple provider made available as a
  223. dynamically loadable module.
  224. It implements the fictitious algorithm C<FOO> for the fictitious
  225. operation C<BAR>.
  226. #include <malloc.h>
  227. #include <openssl/core.h>
  228. #include <openssl/core_numbers.h>
  229. /* Errors used in this provider */
  230. #define E_MALLOC 1
  231. static const OSSL_ITEM reasons[] = {
  232. { E_MALLOC, "memory allocation failure" }.
  233. { 0, NULL } /* Termination */
  234. };
  235. /*
  236. * To ensure we get the function signature right, forward declare
  237. * them using function types provided by openssl/core_numbers.h
  238. */
  239. OSSL_OP_bar_newctx_fn foo_newctx;
  240. OSSL_OP_bar_freectx_fn foo_freectx;
  241. OSSL_OP_bar_init_fn foo_init;
  242. OSSL_OP_bar_update_fn foo_update;
  243. OSSL_OP_bar_final_fn foo_final;
  244. OSSL_provider_query_operation_fn p_query;
  245. OSSL_provider_get_reason_strings_fn p_reasons;
  246. OSSL_provider_teardown_fn p_teardown;
  247. OSSL_provider_init_fn OSSL_provider_init;
  248. OSSL_core_put_error *c_put_error = NULL;
  249. /* Provider context */
  250. struct prov_ctx_st {
  251. OSSL_PROVIDER *prov;
  252. }
  253. /* operation context for the algorithm FOO */
  254. struct foo_ctx_st {
  255. struct prov_ctx_st *provctx;
  256. int b;
  257. };
  258. static void *foo_newctx(void *provctx)
  259. {
  260. struct foo_ctx_st *fooctx = malloc(sizeof(*fooctx));
  261. if (fooctx != NULL)
  262. fooctx->provctx = provctx;
  263. else
  264. c_put_error(provctx->prov, E_MALLOC, __FILE__, __LINE__);
  265. return fooctx;
  266. }
  267. static void foo_freectx(void *fooctx)
  268. {
  269. free(fooctx);
  270. }
  271. static int foo_init(void *vfooctx)
  272. {
  273. struct foo_ctx_st *fooctx = vfooctx;
  274. fooctx->b = 0x33;
  275. }
  276. static int foo_update(void *vfooctx, unsigned char *in, size_t inl)
  277. {
  278. struct foo_ctx_st *fooctx = vfooctx;
  279. /* did you expect something serious? */
  280. if (inl == 0)
  281. return 1;
  282. for (; inl-- > 0; in++)
  283. *in ^= fooctx->b;
  284. return 1;
  285. }
  286. static int foo_final(void *vfooctx)
  287. {
  288. struct foo_ctx_st *fooctx = vfooctx;
  289. fooctx->b = 0x66;
  290. }
  291. static const OSSL_DISPATCH foo_fns[] = {
  292. { OSSL_FUNC_BAR_NEWCTX, (void (*)(void))foo_newctx },
  293. { OSSL_FUNC_BAR_FREECTX, (void (*)(void))foo_freectx },
  294. { OSSL_FUNC_BAR_INIT, (void (*)(void))foo_init },
  295. { OSSL_FUNC_BAR_UPDATE, (void (*)(void))foo_update },
  296. { OSSL_FUNC_BAR_FINAL, (void (*)(void))foo_final },
  297. { 0, NULL }
  298. };
  299. static const OSSL_ALGORITHM bars[] = {
  300. { "FOO", "provider=chumbawamba", foo_fns },
  301. { NULL, NULL, NULL }
  302. };
  303. static const OSSL_ALGORITHM *p_query(void *provctx, int operation_id,
  304. int *no_store)
  305. {
  306. switch (operation_id) {
  307. case OSSL_OP_BAR:
  308. return bars;
  309. }
  310. return NULL;
  311. }
  312. static const OSSL_ITEM *p_reasons(void *provctx)
  313. {
  314. return reasons;
  315. }
  316. static void p_teardown(void *provctx)
  317. {
  318. free(provctx);
  319. }
  320. static const OSSL_DISPATCH prov_fns[] = {
  321. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))p_teardown },
  322. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))p_query },
  323. { OSSL_FUNC_PROVIDER_GET_REASON_STRINGS, (void (*)(void))p_reasons },
  324. { 0, NULL }
  325. };
  326. int OSSL_provider_init(const OSSL_PROVIDER *provider,
  327. const OSSL_DISPATCH *in,
  328. const OSSL_DISPATCH **out,
  329. void **provctx)
  330. {
  331. struct prov_ctx_st *pctx = NULL;
  332. for (; in->function_id != 0; in++)
  333. switch (in->function_id) {
  334. case OSSL_FUNC_CORE_PUT_ERROR:
  335. c_put_error = OSSL_get_core_put_error(in);
  336. break;
  337. }
  338. *out = prov_fns;
  339. if ((pctx = malloc(sizeof(*pctx))) == NULL) {
  340. /*
  341. * ALEA IACTA EST, if the core retrieves the reason table
  342. * regardless, that string will be displayed, otherwise not.
  343. */
  344. c_put_error(provider, E_MALLOC, __FILE__, __LINE__);
  345. return 0;
  346. }
  347. return 1;
  348. }
  349. This relies on a few things existing in F<openssl/core_numbers.h>:
  350. #define OSSL_OP_BAR 4711
  351. #define OSSL_FUNC_BAR_NEWCTX 1
  352. typedef void *(OSSL_OP_bar_newctx_fn)(void *provctx);
  353. static ossl_inline OSSL_get_bar_newctx(const OSSL_DISPATCH *opf)
  354. { return (OSSL_OP_bar_newctx_fn *)opf->function; }
  355. #define OSSL_FUNC_BAR_FREECTX 2
  356. typedef void (OSSL_OP_bar_freectx_fn)(void *ctx);
  357. static ossl_inline OSSL_get_bar_newctx(const OSSL_DISPATCH *opf)
  358. { return (OSSL_OP_bar_freectx_fn *)opf->function; }
  359. #define OSSL_FUNC_BAR_INIT 3
  360. typedef void *(OSSL_OP_bar_init_fn)(void *ctx);
  361. static ossl_inline OSSL_get_bar_init(const OSSL_DISPATCH *opf)
  362. { return (OSSL_OP_bar_init_fn *)opf->function; }
  363. #define OSSL_FUNC_BAR_UPDATE 4
  364. typedef void *(OSSL_OP_bar_update_fn)(void *ctx,
  365. unsigned char *in, size_t inl);
  366. static ossl_inline OSSL_get_bar_update(const OSSL_DISPATCH *opf)
  367. { return (OSSL_OP_bar_update_fn *)opf->function; }
  368. #define OSSL_FUNC_BAR_FINAL 5
  369. typedef void *(OSSL_OP_bar_final_fn)(void *ctx);
  370. static ossl_inline OSSL_get_bar_final(const OSSL_DISPATCH *opf)
  371. { return (OSSL_OP_bar_final_fn *)opf->function; }
  372. =head1 SEE ALSO
  373. L<provider(7)>
  374. =head1 HISTORY
  375. The concept of providers and everything surrounding them was
  376. introduced in OpenSSL 3.0.
  377. =head1 COPYRIGHT
  378. Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
  379. Licensed under the Apache License 2.0 (the "License"). You may not use
  380. this file except in compliance with the License. You can obtain a copy
  381. in the file LICENSE in the source distribution or at
  382. L<https://www.openssl.org/source/license.html>.
  383. =cut