provider-base.pod 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. CRYPTO_mem_ctrl OSSL_FUNC_CRYPTO_MEM_CTRL
  97. BIO_new_file OSSL_FUNC_BIO_NEW_FILE
  98. BIO_new_mem_buf OSSL_FUNC_BIO_NEW_MEMBUF
  99. BIO_read_ex OSSL_FUNC_BIO_READ_EX
  100. BIO_free OSSL_FUNC_BIO_FREE
  101. BIO_vprintf OSSL_FUNC_BIO_VPRINTF
  102. OPENSSL_cleanse OSSL_FUNC_OPENSSL_CLEANSE
  103. OPENSSL_hexstr2buf OSSL_FUNC_OPENSSL_HEXSTR2BUF
  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(), CRYPTO_mem_ctrl(),
  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. =head2 Provider functions
  158. provider_teardown() is called when a provider is shut down and removed
  159. from the core's provider store.
  160. It must free the passed I<provctx>.
  161. provider_gettable_params() should return a constant array of
  162. descriptor B<OSSL_PARAM>, for parameters that provider_get_params()
  163. can handle.
  164. provider_get_params() should process the B<OSSL_PARAM> array
  165. I<params>, setting the values of the parameters it understands.
  166. provider_query_operation() should return a constant B<OSSL_ALGORITHM>
  167. that corresponds to the given I<operation_id>.
  168. It should indicate if the core may store a reference to this array by
  169. setting I<*no_store> to 0 (core may store a reference) or 1 (core may
  170. not store a reference).
  171. provider_get_reason_strings() should return a constant B<OSSL_ITEM>
  172. array that provides reason strings for reason codes the provider may
  173. use when reporting errors using core_put_error().
  174. None of these functions are mandatory, but a provider is fairly
  175. useless without at least provider_query_operation(), and
  176. provider_gettable_params() is fairly useless if not accompanied by
  177. provider_get_params().
  178. =head2 Core parameters
  179. core_get_params() understands the following known parameters:
  180. =over 4
  181. =item "openssl-version"
  182. This is a B<OSSL_PARAM_UTF8_PTR> type of parameter, pointing at the
  183. OpenSSL libraries' full version string, i.e. the string expanded from
  184. the macro B<OPENSSL_VERSION_STR>.
  185. =item "provider-name"
  186. This is a B<OSSL_PARAM_UTF8_PTR> type of parameter, pointing at the
  187. OpenSSL libraries' idea of what the calling provider is called.
  188. =back
  189. Additionally, provider specific configuration parameters from the
  190. config file are available, in dotted name form.
  191. The dotted name form is a concatenation of section names and final
  192. config command name separated by periods.
  193. For example, let's say we have the following config example:
  194. openssl_conf = openssl_init
  195. [openssl_init]
  196. providers = providers_sect
  197. [providers_sect]
  198. foo = foo_sect
  199. [foo_sect]
  200. activate = 1
  201. data1 = 2
  202. data2 = str
  203. more = foo_more
  204. [foo_more]
  205. data3 = foo,bar
  206. The provider will have these additional parameters available:
  207. =over 4
  208. =item "activate"
  209. pointing at the string "1"
  210. =item "data1"
  211. pointing at the string "2"
  212. =item "data2"
  213. pointing at the string "str"
  214. =item "more.data3"
  215. pointing at the string "foo,bar"
  216. =back
  217. For more information on handling parameters, see L<OSSL_PARAM(3)> as
  218. L<OSSL_PARAM_int(3)>.
  219. =head1 EXAMPLES
  220. This is an example of a simple provider made available as a
  221. dynamically loadable module.
  222. It implements the fictitious algorithm C<FOO> for the fictitious
  223. operation C<BAR>.
  224. #include <malloc.h>
  225. #include <openssl/core.h>
  226. #include <openssl/core_numbers.h>
  227. /* Errors used in this provider */
  228. #define E_MALLOC 1
  229. static const OSSL_ITEM reasons[] = {
  230. { E_MALLOC, "memory allocation failure" }.
  231. { 0, NULL } /* Termination */
  232. };
  233. /*
  234. * To ensure we get the function signature right, forward declare
  235. * them using function types provided by openssl/core_numbers.h
  236. */
  237. OSSL_OP_bar_newctx_fn foo_newctx;
  238. OSSL_OP_bar_freectx_fn foo_freectx;
  239. OSSL_OP_bar_init_fn foo_init;
  240. OSSL_OP_bar_update_fn foo_update;
  241. OSSL_OP_bar_final_fn foo_final;
  242. OSSL_provider_query_operation_fn p_query;
  243. OSSL_provider_get_reason_strings_fn p_reasons;
  244. OSSL_provider_teardown_fn p_teardown;
  245. OSSL_provider_init_fn OSSL_provider_init;
  246. OSSL_core_put_error *c_put_error = NULL;
  247. /* Provider context */
  248. struct prov_ctx_st {
  249. OSSL_PROVIDER *prov;
  250. }
  251. /* operation context for the algorithm FOO */
  252. struct foo_ctx_st {
  253. struct prov_ctx_st *provctx;
  254. int b;
  255. };
  256. static void *foo_newctx(void *provctx)
  257. {
  258. struct foo_ctx_st *fooctx = malloc(sizeof(*fooctx));
  259. if (fooctx != NULL)
  260. fooctx->provctx = provctx;
  261. else
  262. c_put_error(provctx->prov, E_MALLOC, __FILE__, __LINE__);
  263. return fooctx;
  264. }
  265. static void foo_freectx(void *fooctx)
  266. {
  267. free(fooctx);
  268. }
  269. static int foo_init(void *vfooctx)
  270. {
  271. struct foo_ctx_st *fooctx = vfooctx;
  272. fooctx->b = 0x33;
  273. }
  274. static int foo_update(void *vfooctx, unsigned char *in, size_t inl)
  275. {
  276. struct foo_ctx_st *fooctx = vfooctx;
  277. /* did you expect something serious? */
  278. if (inl == 0)
  279. return 1;
  280. for (; inl-- > 0; in++)
  281. *in ^= fooctx->b;
  282. return 1;
  283. }
  284. static int foo_final(void *vfooctx)
  285. {
  286. struct foo_ctx_st *fooctx = vfooctx;
  287. fooctx->b = 0x66;
  288. }
  289. static const OSSL_DISPATCH foo_fns[] = {
  290. { OSSL_FUNC_BAR_NEWCTX, (void (*)(void))foo_newctx },
  291. { OSSL_FUNC_BAR_FREECTX, (void (*)(void))foo_freectx },
  292. { OSSL_FUNC_BAR_INIT, (void (*)(void))foo_init },
  293. { OSSL_FUNC_BAR_UPDATE, (void (*)(void))foo_update },
  294. { OSSL_FUNC_BAR_FINAL, (void (*)(void))foo_final },
  295. { 0, NULL }
  296. };
  297. static const OSSL_ALGORITHM bars[] = {
  298. { "FOO", "provider=chumbawamba", foo_fns },
  299. { NULL, NULL, NULL }
  300. };
  301. static const OSSL_ALGORITHM *p_query(void *provctx, int operation_id,
  302. int *no_store)
  303. {
  304. switch (operation_id) {
  305. case OSSL_OP_BAR:
  306. return bars;
  307. }
  308. return NULL;
  309. }
  310. static const OSSL_ITEM *p_reasons(void *provctx)
  311. {
  312. return reasons;
  313. }
  314. static void p_teardown(void *provctx)
  315. {
  316. free(provctx);
  317. }
  318. static const OSSL_DISPATCH prov_fns[] = {
  319. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))p_teardown },
  320. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))p_query },
  321. { OSSL_FUNC_PROVIDER_GET_REASON_STRINGS, (void (*)(void))p_reasons },
  322. { 0, NULL }
  323. };
  324. int OSSL_provider_init(const OSSL_PROVIDER *provider,
  325. const OSSL_DISPATCH *in,
  326. const OSSL_DISPATCH **out,
  327. void **provctx)
  328. {
  329. struct prov_ctx_st *pctx = NULL;
  330. for (; in->function_id != 0; in++)
  331. switch (in->function_id) {
  332. case OSSL_FUNC_CORE_PUT_ERROR:
  333. c_put_error = OSSL_get_core_put_error(in);
  334. break;
  335. }
  336. *out = prov_fns;
  337. if ((pctx = malloc(sizeof(*pctx))) == NULL) {
  338. /*
  339. * ALEA IACTA EST, if the core retrieves the reason table
  340. * regardless, that string will be displayed, otherwise not.
  341. */
  342. c_put_error(provider, E_MALLOC, __FILE__, __LINE__);
  343. return 0;
  344. }
  345. return 1;
  346. }
  347. This relies on a few things existing in F<openssl/core_numbers.h>:
  348. #define OSSL_OP_BAR 4711
  349. #define OSSL_FUNC_BAR_NEWCTX 1
  350. typedef void *(OSSL_OP_bar_newctx_fn)(void *provctx);
  351. static ossl_inline OSSL_get_bar_newctx(const OSSL_DISPATCH *opf)
  352. { return (OSSL_OP_bar_newctx_fn *)opf->function; }
  353. #define OSSL_FUNC_BAR_FREECTX 2
  354. typedef void (OSSL_OP_bar_freectx_fn)(void *ctx);
  355. static ossl_inline OSSL_get_bar_newctx(const OSSL_DISPATCH *opf)
  356. { return (OSSL_OP_bar_freectx_fn *)opf->function; }
  357. #define OSSL_FUNC_BAR_INIT 3
  358. typedef void *(OSSL_OP_bar_init_fn)(void *ctx);
  359. static ossl_inline OSSL_get_bar_init(const OSSL_DISPATCH *opf)
  360. { return (OSSL_OP_bar_init_fn *)opf->function; }
  361. #define OSSL_FUNC_BAR_UPDATE 4
  362. typedef void *(OSSL_OP_bar_update_fn)(void *ctx,
  363. unsigned char *in, size_t inl);
  364. static ossl_inline OSSL_get_bar_update(const OSSL_DISPATCH *opf)
  365. { return (OSSL_OP_bar_update_fn *)opf->function; }
  366. #define OSSL_FUNC_BAR_FINAL 5
  367. typedef void *(OSSL_OP_bar_final_fn)(void *ctx);
  368. static ossl_inline OSSL_get_bar_final(const OSSL_DISPATCH *opf)
  369. { return (OSSL_OP_bar_final_fn *)opf->function; }
  370. =head1 SEE ALSO
  371. L<provider(7)>
  372. =head1 HISTORY
  373. The concept of providers and everything surrounding them was
  374. introduced in OpenSSL 3.0.
  375. =head1 COPYRIGHT
  376. Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
  377. Licensed under the Apache License 2.0 (the "License"). You may not use
  378. this file except in compliance with the License. You can obtain a copy
  379. in the file LICENSE in the source distribution or at
  380. L<https://www.openssl.org/source/license.html>.
  381. =cut