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