4ab39e6531e7a255f12dc5ea3af5f433dc4fd723
[openssl.git] / providers / defltprov.c
1 /*
2  * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <string.h>
11 #include <stdio.h>
12 #include <openssl/opensslconf.h>
13 #include <openssl/core.h>
14 #include <openssl/core_dispatch.h>
15 #include <openssl/core_names.h>
16 #include <openssl/params.h>
17 #include "prov/bio.h"
18 #include "prov/provider_ctx.h"
19 #include "prov/providercommon.h"
20 #include "prov/implementations.h"
21 #include "prov/provider_util.h"
22 #include "internal/nelem.h"
23
24 /*
25  * Forward declarations to ensure that interface functions are correctly
26  * defined.
27  */
28 static OSSL_FUNC_provider_gettable_params_fn deflt_gettable_params;
29 static OSSL_FUNC_provider_get_params_fn deflt_get_params;
30 static OSSL_FUNC_provider_query_operation_fn deflt_query;
31
32 #define ALGC(NAMES, FUNC, CHECK) { { NAMES, "provider=default", FUNC }, CHECK }
33 #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
34
35 /* Functions provided by the core */
36 static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
37 static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;
38
39 /* Parameters we provide to the core */
40 static const OSSL_PARAM deflt_param_types[] = {
41     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
42     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
43     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
44     OSSL_PARAM_END
45 };
46
47 static const OSSL_PARAM *deflt_gettable_params(void *provctx)
48 {
49     return deflt_param_types;
50 }
51
52 static int deflt_get_params(void *provctx, OSSL_PARAM params[])
53 {
54     OSSL_PARAM *p;
55
56     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
57     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Default Provider"))
58         return 0;
59     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
60     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
61         return 0;
62     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
63     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
64         return 0;
65     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
66     if (p != NULL && !OSSL_PARAM_set_uint(p, 1))
67         return 0;
68     return 1;
69 }
70
71 /*
72  * For the algorithm names, we use the following formula for our primary
73  * names:
74  *
75  *     ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
76  *
77  *     VERSION is only present if there are multiple versions of
78  *     an alg (MD2, MD4, MD5).  It may be omitted if there is only
79  *     one version (if a subsequent version is released in the future,
80  *     we can always change the canonical name, and add the old name
81  *     as an alias).
82  *
83  *     SUBNAME may be present where we are combining multiple
84  *     algorithms together, e.g. MD5-SHA1.
85  *
86  *     SIZE is only present if multiple versions of an algorithm exist
87  *     with different sizes (e.g. AES-128-CBC, AES-256-CBC)
88  *
89  *     MODE is only present where applicable.
90  *
91  * We add diverse other names where applicable, such as the names that
92  * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
93  * we have used historically.
94  *
95  * Algorithm names are case insensitive, but we use all caps in our "canonical"
96  * names for consistency.
97  */
98 static const OSSL_ALGORITHM deflt_digests[] = {
99     /* Our primary name:NIST name[:our older names] */
100     { "SHA1:SHA-1:SSL3-SHA1", "provider=default", sha1_functions },
101     { "SHA2-224:SHA-224:SHA224", "provider=default", sha224_functions },
102     { "SHA2-256:SHA-256:SHA256", "provider=default", sha256_functions },
103     { "SHA2-384:SHA-384:SHA384", "provider=default", sha384_functions },
104     { "SHA2-512:SHA-512:SHA512", "provider=default", sha512_functions },
105     { "SHA2-512/224:SHA-512/224:SHA512-224", "provider=default",
106       sha512_224_functions },
107     { "SHA2-512/256:SHA-512/256:SHA512-256", "provider=default",
108       sha512_256_functions },
109
110     /* We agree with NIST here, so one name only */
111     { "SHA3-224", "provider=default", sha3_224_functions },
112     { "SHA3-256", "provider=default", sha3_256_functions },
113     { "SHA3-384", "provider=default", sha3_384_functions },
114     { "SHA3-512", "provider=default", sha3_512_functions },
115
116     /*
117      * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
118      * the KMAC-128 and KMAC-256.
119      */
120     { "KECCAK-KMAC-128:KECCAK-KMAC128", "provider=default", keccak_kmac_128_functions },
121     { "KECCAK-KMAC-256:KECCAK-KMAC256", "provider=default", keccak_kmac_256_functions },
122
123     /* Our primary name:NIST name */
124     { "SHAKE-128:SHAKE128", "provider=default", shake_128_functions },
125     { "SHAKE-256:SHAKE256", "provider=default", shake_256_functions },
126
127 #ifndef OPENSSL_NO_BLAKE2
128     /*
129      * https://blake2.net/ doesn't specify size variants,
130      * but mentions that Bouncy Castle uses the names
131      * BLAKE2b-160, BLAKE2b-256, BLAKE2b-384, and BLAKE2b-512
132      * If we assume that "2b" and "2s" are versions, that pattern
133      * fits with ours.  We also add our historical names.
134      */
135     { "BLAKE2S-256:BLAKE2s256", "provider=default", blake2s256_functions },
136     { "BLAKE2B-512:BLAKE2b512", "provider=default", blake2b512_functions },
137 #endif /* OPENSSL_NO_BLAKE2 */
138
139 #ifndef OPENSSL_NO_SM3
140     { "SM3", "provider=default", sm3_functions },
141 #endif /* OPENSSL_NO_SM3 */
142
143 #ifndef OPENSSL_NO_MD5
144     { "MD5:SSL3-MD5", "provider=default", md5_functions },
145     { "MD5-SHA1", "provider=default", md5_sha1_functions },
146 #endif /* OPENSSL_NO_MD5 */
147
148     { NULL, NULL, NULL }
149 };
150
151 static const OSSL_ALGORITHM_CAPABLE deflt_ciphers[] = {
152     ALG("NULL", null_functions),
153     ALG("AES-256-ECB", aes256ecb_functions),
154     ALG("AES-192-ECB", aes192ecb_functions),
155     ALG("AES-128-ECB", aes128ecb_functions),
156     ALG("AES-256-CBC:AES256", aes256cbc_functions),
157     ALG("AES-192-CBC:AES192", aes192cbc_functions),
158     ALG("AES-128-CBC:AES128", aes128cbc_functions),
159     ALG("AES-128-CBC-CTS", aes128cbc_cts_functions),
160     ALG("AES-192-CBC-CTS", aes192cbc_cts_functions),
161     ALG("AES-256-CBC-CTS", aes256cbc_cts_functions),
162     ALG("AES-256-OFB", aes256ofb_functions),
163     ALG("AES-192-OFB", aes192ofb_functions),
164     ALG("AES-128-OFB", aes128ofb_functions),
165     ALG("AES-256-CFB", aes256cfb_functions),
166     ALG("AES-192-CFB", aes192cfb_functions),
167     ALG("AES-128-CFB", aes128cfb_functions),
168     ALG("AES-256-CFB1", aes256cfb1_functions),
169     ALG("AES-192-CFB1", aes192cfb1_functions),
170     ALG("AES-128-CFB1", aes128cfb1_functions),
171     ALG("AES-256-CFB8", aes256cfb8_functions),
172     ALG("AES-192-CFB8", aes192cfb8_functions),
173     ALG("AES-128-CFB8", aes128cfb8_functions),
174     ALG("AES-256-CTR", aes256ctr_functions),
175     ALG("AES-192-CTR", aes192ctr_functions),
176     ALG("AES-128-CTR", aes128ctr_functions),
177     ALG("AES-256-XTS", aes256xts_functions),
178     ALG("AES-128-XTS", aes128xts_functions),
179 #ifndef OPENSSL_NO_OCB
180     ALG("AES-256-OCB", aes256ocb_functions),
181     ALG("AES-192-OCB", aes192ocb_functions),
182     ALG("AES-128-OCB", aes128ocb_functions),
183 #endif /* OPENSSL_NO_OCB */
184 #ifndef OPENSSL_NO_SIV
185     ALG("AES-128-SIV", aes128siv_functions),
186     ALG("AES-192-SIV", aes192siv_functions),
187     ALG("AES-256-SIV", aes256siv_functions),
188 #endif /* OPENSSL_NO_SIV */
189     ALG("AES-256-GCM:id-aes256-GCM", aes256gcm_functions),
190     ALG("AES-192-GCM:id-aes192-GCM", aes192gcm_functions),
191     ALG("AES-128-GCM:id-aes128-GCM", aes128gcm_functions),
192     ALG("AES-256-CCM:id-aes256-CCM", aes256ccm_functions),
193     ALG("AES-192-CCM:id-aes192-CCM", aes192ccm_functions),
194     ALG("AES-128-CCM:id-aes128-CCM", aes128ccm_functions),
195     ALG("AES-256-WRAP:id-aes256-wrap:AES256-WRAP", aes256wrap_functions),
196     ALG("AES-192-WRAP:id-aes192-wrap:AES192-WRAP", aes192wrap_functions),
197     ALG("AES-128-WRAP:id-aes128-wrap:AES128-WRAP", aes128wrap_functions),
198     ALG("AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD",
199         aes256wrappad_functions),
200     ALG("AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD",
201         aes192wrappad_functions),
202     ALG("AES-128-WRAP-PAD:id-aes128-wrap-pad:AES128-WRAP-PAD",
203         aes128wrappad_functions),
204     ALGC("AES-128-CBC-HMAC-SHA1", aes128cbc_hmac_sha1_functions,
205          cipher_capable_aes_cbc_hmac_sha1),
206     ALGC("AES-256-CBC-HMAC-SHA1", aes256cbc_hmac_sha1_functions,
207          cipher_capable_aes_cbc_hmac_sha1),
208     ALGC("AES-128-CBC-HMAC-SHA256", aes128cbc_hmac_sha256_functions,
209         cipher_capable_aes_cbc_hmac_sha256),
210     ALGC("AES-256-CBC-HMAC-SHA256", aes256cbc_hmac_sha256_functions,
211          cipher_capable_aes_cbc_hmac_sha256),
212 #ifndef OPENSSL_NO_ARIA
213     ALG("ARIA-256-GCM", aria256gcm_functions),
214     ALG("ARIA-192-GCM", aria192gcm_functions),
215     ALG("ARIA-128-GCM", aria128gcm_functions),
216     ALG("ARIA-256-CCM", aria256ccm_functions),
217     ALG("ARIA-192-CCM", aria192ccm_functions),
218     ALG("ARIA-128-CCM", aria128ccm_functions),
219     ALG("ARIA-256-ECB", aria256ecb_functions),
220     ALG("ARIA-192-ECB", aria192ecb_functions),
221     ALG("ARIA-128-ECB", aria128ecb_functions),
222     ALG("ARIA-256-CBC:ARIA256", aria256cbc_functions),
223     ALG("ARIA-192-CBC:ARIA192", aria192cbc_functions),
224     ALG("ARIA-128-CBC:ARIA128", aria128cbc_functions),
225     ALG("ARIA-256-OFB", aria256ofb_functions),
226     ALG("ARIA-192-OFB", aria192ofb_functions),
227     ALG("ARIA-128-OFB", aria128ofb_functions),
228     ALG("ARIA-256-CFB", aria256cfb_functions),
229     ALG("ARIA-192-CFB", aria192cfb_functions),
230     ALG("ARIA-128-CFB", aria128cfb_functions),
231     ALG("ARIA-256-CFB1", aria256cfb1_functions),
232     ALG("ARIA-192-CFB1", aria192cfb1_functions),
233     ALG("ARIA-128-CFB1", aria128cfb1_functions),
234     ALG("ARIA-256-CFB8", aria256cfb8_functions),
235     ALG("ARIA-192-CFB8", aria192cfb8_functions),
236     ALG("ARIA-128-CFB8", aria128cfb8_functions),
237     ALG("ARIA-256-CTR", aria256ctr_functions),
238     ALG("ARIA-192-CTR", aria192ctr_functions),
239     ALG("ARIA-128-CTR", aria128ctr_functions),
240 #endif /* OPENSSL_NO_ARIA */
241 #ifndef OPENSSL_NO_CAMELLIA
242     ALG("CAMELLIA-256-ECB", camellia256ecb_functions),
243     ALG("CAMELLIA-192-ECB", camellia192ecb_functions),
244     ALG("CAMELLIA-128-ECB", camellia128ecb_functions),
245     ALG("CAMELLIA-256-CBC:CAMELLIA256", camellia256cbc_functions),
246     ALG("CAMELLIA-192-CBC:CAMELLIA192", camellia192cbc_functions),
247     ALG("CAMELLIA-128-CBC:CAMELLIA128", camellia128cbc_functions),
248     ALG("CAMELLIA-256-OFB", camellia256ofb_functions),
249     ALG("CAMELLIA-192-OFB", camellia192ofb_functions),
250     ALG("CAMELLIA-128-OFB", camellia128ofb_functions),
251     ALG("CAMELLIA-256-CFB", camellia256cfb_functions),
252     ALG("CAMELLIA-192-CFB", camellia192cfb_functions),
253     ALG("CAMELLIA-128-CFB", camellia128cfb_functions),
254     ALG("CAMELLIA-256-CFB1", camellia256cfb1_functions),
255     ALG("CAMELLIA-192-CFB1", camellia192cfb1_functions),
256     ALG("CAMELLIA-128-CFB1", camellia128cfb1_functions),
257     ALG("CAMELLIA-256-CFB8", camellia256cfb8_functions),
258     ALG("CAMELLIA-192-CFB8", camellia192cfb8_functions),
259     ALG("CAMELLIA-128-CFB8", camellia128cfb8_functions),
260     ALG("CAMELLIA-256-CTR", camellia256ctr_functions),
261     ALG("CAMELLIA-192-CTR", camellia192ctr_functions),
262     ALG("CAMELLIA-128-CTR", camellia128ctr_functions),
263 #endif /* OPENSSL_NO_CAMELLIA */
264 #ifndef OPENSSL_NO_DES
265     ALG("DES-EDE3-ECB:DES-EDE3", tdes_ede3_ecb_functions),
266     ALG("DES-EDE3-CBC:DES3", tdes_ede3_cbc_functions),
267     ALG("DES-EDE3-OFB", tdes_ede3_ofb_functions),
268     ALG("DES-EDE3-CFB", tdes_ede3_cfb_functions),
269     ALG("DES-EDE3-CFB8", tdes_ede3_cfb8_functions),
270     ALG("DES-EDE3-CFB1", tdes_ede3_cfb1_functions),
271     ALG("DES3-WRAP:id-smime-alg-CMS3DESwrap", tdes_wrap_cbc_functions),
272     ALG("DES-EDE-ECB:DES-EDE", tdes_ede2_ecb_functions),
273     ALG("DES-EDE-CBC", tdes_ede2_cbc_functions),
274     ALG("DES-EDE-OFB", tdes_ede2_ofb_functions),
275     ALG("DES-EDE-CFB", tdes_ede2_cfb_functions),
276 #endif /* OPENSSL_NO_DES */
277 #ifndef OPENSSL_NO_SM4
278     ALG("SM4-ECB", sm4128ecb_functions),
279     ALG("SM4-CBC:SM4", sm4128cbc_functions),
280     ALG("SM4-CTR", sm4128ctr_functions),
281     ALG("SM4-OFB:SM4-OFB128", sm4128ofb128_functions),
282     ALG("SM4-CFB:SM4-CFB128", sm4128cfb128_functions),
283 #endif /* OPENSSL_NO_SM4 */
284 #ifndef OPENSSL_NO_CHACHA
285     ALG("ChaCha20", chacha20_functions),
286 # ifndef OPENSSL_NO_POLY1305
287     ALG("ChaCha20-Poly1305", chacha20_poly1305_functions),
288 # endif /* OPENSSL_NO_POLY1305 */
289 #endif /* OPENSSL_NO_CHACHA */
290     { { NULL, NULL, NULL }, NULL }
291 };
292 static OSSL_ALGORITHM exported_ciphers[OSSL_NELEM(deflt_ciphers)];
293
294 static const OSSL_ALGORITHM deflt_macs[] = {
295 #ifndef OPENSSL_NO_BLAKE2
296     { "BLAKE2BMAC", "provider=default", blake2bmac_functions },
297     { "BLAKE2SMAC", "provider=default", blake2smac_functions },
298 #endif
299 #ifndef OPENSSL_NO_CMAC
300     { "CMAC", "provider=default", cmac_functions },
301 #endif
302     { "GMAC", "provider=default", gmac_functions },
303     { "HMAC", "provider=default", hmac_functions },
304     { "KMAC-128:KMAC128", "provider=default", kmac128_functions },
305     { "KMAC-256:KMAC256", "provider=default", kmac256_functions },
306 #ifndef OPENSSL_NO_SIPHASH
307     { "SIPHASH", "provider=default", siphash_functions },
308 #endif
309 #ifndef OPENSSL_NO_POLY1305
310     { "POLY1305", "provider=default", poly1305_functions },
311 #endif
312     { NULL, NULL, NULL }
313 };
314
315 static const OSSL_ALGORITHM deflt_kdfs[] = {
316     { "HKDF", "provider=default", kdf_hkdf_functions },
317     { "SSKDF", "provider=default", kdf_sskdf_functions },
318     { "PBKDF2", "provider=default", kdf_pbkdf2_functions },
319     { "SSHKDF", "provider=default", kdf_sshkdf_functions },
320     { "X963KDF", "provider=default", kdf_x963_kdf_functions },
321     { "TLS1-PRF", "provider=default", kdf_tls1_prf_functions },
322     { "KBKDF", "provider=default", kdf_kbkdf_functions },
323 #ifndef OPENSSL_NO_CMS
324     { "X942KDF", "provider=default", kdf_x942_kdf_functions },
325 #endif
326 #ifndef OPENSSL_NO_SCRYPT
327     { "SCRYPT:id-scrypt", "provider=default", kdf_scrypt_functions },
328 #endif
329     { "KRB5KDF", "provider=default", kdf_krb5kdf_functions },
330     { NULL, NULL, NULL }
331 };
332
333 static const OSSL_ALGORITHM deflt_keyexch[] = {
334 #ifndef OPENSSL_NO_DH
335     { "DH:dhKeyAgreement", "provider=default", dh_keyexch_functions },
336 #endif
337 #ifndef OPENSSL_NO_EC
338     { "ECDH", "provider=default", ecdh_keyexch_functions },
339     { "X25519", "provider=default", x25519_keyexch_functions },
340     { "X448", "provider=default", x448_keyexch_functions },
341 #endif
342     { NULL, NULL, NULL }
343 };
344
345 static const OSSL_ALGORITHM deflt_rands[] = {
346     { "CTR-DRBG", "provider=default", drbg_ctr_functions },
347     { "HASH-DRBG", "provider=default", drbg_hash_functions },
348     { "HMAC-DRBG", "provider=default", drbg_hmac_functions },
349     { "TEST-RAND", "provider=default", test_rng_functions },
350     { NULL, NULL, NULL }
351 };
352
353 static const OSSL_ALGORITHM deflt_signature[] = {
354 #ifndef OPENSSL_NO_DSA
355     { "DSA:dsaEncryption", "provider=default", dsa_signature_functions },
356 #endif
357     { "RSA:rsaEncryption", "provider=default", rsa_signature_functions },
358 #ifndef OPENSSL_NO_EC
359     { "ED25519:Ed25519", "provider=default", ed25519_signature_functions },
360     { "ED448:Ed448", "provider=default", ed448_signature_functions },
361     { "ECDSA", "provider=default", ecdsa_signature_functions },
362 #endif
363     { NULL, NULL, NULL }
364 };
365
366 static const OSSL_ALGORITHM deflt_asym_cipher[] = {
367     { "RSA:rsaEncryption", "provider=default", rsa_asym_cipher_functions },
368     { NULL, NULL, NULL }
369 };
370
371 static const OSSL_ALGORITHM deflt_keymgmt[] = {
372 #ifndef OPENSSL_NO_DH
373     { "DH:dhKeyAgreement", "provider=default", dh_keymgmt_functions },
374 #endif
375 #ifndef OPENSSL_NO_DSA
376     { "DSA:dsaEncryption", "provider=default", dsa_keymgmt_functions },
377 #endif
378     { "RSA:rsaEncryption", "provider=default", rsa_keymgmt_functions },
379     { "RSA-PSS:RSASSA-PSS", "provider=default", rsapss_keymgmt_functions },
380 #ifndef OPENSSL_NO_EC
381     { "EC:id-ecPublicKey", "provider=default", ec_keymgmt_functions },
382     { "X25519", "provider=default", x25519_keymgmt_functions },
383     { "X448", "provider=default", x448_keymgmt_functions },
384     { "ED25519", "provider=default", ed25519_keymgmt_functions },
385     { "ED448", "provider=default", ed448_keymgmt_functions },
386 #endif
387     { NULL, NULL, NULL }
388 };
389
390 static const OSSL_ALGORITHM deflt_serializer[] = {
391 #define SER(name, fips, format, type, func_table)                           \
392     { name,                                                                 \
393       "provider=default,fips=" fips ",format=" format ",type=" type,        \
394       (func_table) }
395
396 #include "serializers.inc"
397     { NULL, NULL, NULL }
398 };
399 #undef SER
400
401 static const OSSL_ALGORITHM deflt_deserializer[] = {
402 #define DESER(name, fips, input, func_table)                                \
403     { name,                                                                 \
404       "provider=default,fips=" fips ",input=" input,                        \
405       (func_table) }
406
407 #include "deserializers.inc"
408     { NULL, NULL, NULL }
409 };
410 #undef DESER
411
412 static const OSSL_ALGORITHM *deflt_query(void *provctx, int operation_id,
413                                          int *no_cache)
414 {
415     *no_cache = 0;
416     switch (operation_id) {
417     case OSSL_OP_DIGEST:
418         return deflt_digests;
419     case OSSL_OP_CIPHER:
420         ossl_prov_cache_exported_algorithms(deflt_ciphers, exported_ciphers);
421         return exported_ciphers;
422     case OSSL_OP_MAC:
423         return deflt_macs;
424     case OSSL_OP_KDF:
425         return deflt_kdfs;
426     case OSSL_OP_RAND:
427         return deflt_rands;
428     case OSSL_OP_KEYMGMT:
429         return deflt_keymgmt;
430     case OSSL_OP_KEYEXCH:
431         return deflt_keyexch;
432     case OSSL_OP_SIGNATURE:
433         return deflt_signature;
434     case OSSL_OP_ASYM_CIPHER:
435         return deflt_asym_cipher;
436     case OSSL_OP_SERIALIZER:
437         return deflt_serializer;
438     case OSSL_OP_DESERIALIZER:
439         return deflt_deserializer;
440     }
441     return NULL;
442 }
443
444
445 static void deflt_teardown(void *provctx)
446 {
447     BIO_meth_free(PROV_CTX_get0_core_bio_method(provctx));
448     PROV_CTX_free(provctx);
449 }
450
451 /* Functions we provide to the core */
452 static const OSSL_DISPATCH deflt_dispatch_table[] = {
453     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))deflt_teardown },
454     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))deflt_gettable_params },
455     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))deflt_get_params },
456     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))deflt_query },
457     { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))provider_get_capabilities },
458     { 0, NULL }
459 };
460
461 OSSL_provider_init_fn ossl_default_provider_init;
462
463 int ossl_default_provider_init(const OSSL_CORE_HANDLE *handle,
464                                const OSSL_DISPATCH *in,
465                                const OSSL_DISPATCH **out,
466                                void **provctx)
467 {
468     OSSL_FUNC_core_get_library_context_fn *c_get_libctx = NULL;
469     BIO_METHOD *corebiometh;
470
471     if (!ossl_prov_bio_from_dispatch(in))
472         return 0;
473     for (; in->function_id != 0; in++) {
474         switch (in->function_id) {
475         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
476             c_gettable_params = OSSL_FUNC_core_gettable_params(in);
477             break;
478         case OSSL_FUNC_CORE_GET_PARAMS:
479             c_get_params = OSSL_FUNC_core_get_params(in);
480             break;
481         case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
482             c_get_libctx = OSSL_FUNC_core_get_library_context(in);
483             break;
484         default:
485             /* Just ignore anything we don't understand */
486             break;
487         }
488     }
489
490     if (c_get_libctx == NULL)
491         return 0;
492
493     /*
494      * We want to make sure that all calls from this provider that requires
495      * a library context use the same context as the one used to call our
496      * functions.  We do that by passing it along in the provider context.
497      *
498      * This only works for built-in providers.  Most providers should
499      * create their own library context.
500      */
501     if ((*provctx = PROV_CTX_new()) == NULL
502             || (corebiometh = bio_prov_init_bio_method()) == NULL) {
503         PROV_CTX_free(*provctx);
504         *provctx = NULL;
505         return 0;
506     }
507     PROV_CTX_set0_library_context(*provctx, (OPENSSL_CTX *)c_get_libctx(handle));
508     PROV_CTX_set0_handle(*provctx, handle);
509     PROV_CTX_set0_core_bio_method(*provctx, corebiometh);
510
511     *out = deflt_dispatch_table;
512
513     return 1;
514 }