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