Rename some occurrences of 'library_context' and 'lib_ctx' to 'libctx'
[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_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
45     OSSL_PARAM_END
46 };
47
48 static const OSSL_PARAM *deflt_gettable_params(void *provctx)
49 {
50     return deflt_param_types;
51 }
52
53 static int deflt_get_params(void *provctx, OSSL_PARAM params[])
54 {
55     OSSL_PARAM *p;
56
57     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
58     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Default Provider"))
59         return 0;
60     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
61     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
62         return 0;
63     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
64     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
65         return 0;
66     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
67     if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
68         return 0;
69     return 1;
70 }
71
72 /*
73  * For the algorithm names, we use the following formula for our primary
74  * names:
75  *
76  *     ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
77  *
78  *     VERSION is only present if there are multiple versions of
79  *     an alg (MD2, MD4, MD5).  It may be omitted if there is only
80  *     one version (if a subsequent version is released in the future,
81  *     we can always change the canonical name, and add the old name
82  *     as an alias).
83  *
84  *     SUBNAME may be present where we are combining multiple
85  *     algorithms together, e.g. MD5-SHA1.
86  *
87  *     SIZE is only present if multiple versions of an algorithm exist
88  *     with different sizes (e.g. AES-128-CBC, AES-256-CBC)
89  *
90  *     MODE is only present where applicable.
91  *
92  * We add diverse other names where applicable, such as the names that
93  * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
94  * we have used historically.
95  *
96  * Algorithm names are case insensitive, but we use all caps in our "canonical"
97  * names for consistency.
98  */
99 static const OSSL_ALGORITHM deflt_digests[] = {
100     /* Our primary name:NIST name[:our older names] */
101     { "SHA1:SHA-1:SSL3-SHA1", "provider=default", ossl_sha1_functions },
102     { "SHA2-224:SHA-224:SHA224", "provider=default", ossl_sha224_functions },
103     { "SHA2-256:SHA-256:SHA256", "provider=default", ossl_sha256_functions },
104     { "SHA2-384:SHA-384:SHA384", "provider=default", ossl_sha384_functions },
105     { "SHA2-512:SHA-512:SHA512", "provider=default", ossl_sha512_functions },
106     { "SHA2-512/224:SHA-512/224:SHA512-224", "provider=default",
107       ossl_sha512_224_functions },
108     { "SHA2-512/256:SHA-512/256:SHA512-256", "provider=default",
109       ossl_sha512_256_functions },
110
111     /* We agree with NIST here, so one name only */
112     { "SHA3-224", "provider=default", ossl_sha3_224_functions },
113     { "SHA3-256", "provider=default", ossl_sha3_256_functions },
114     { "SHA3-384", "provider=default", ossl_sha3_384_functions },
115     { "SHA3-512", "provider=default", ossl_sha3_512_functions },
116
117     /*
118      * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
119      * the KMAC-128 and KMAC-256.
120      */
121     { "KECCAK-KMAC-128:KECCAK-KMAC128", "provider=default",
122       ossl_keccak_kmac_128_functions },
123     { "KECCAK-KMAC-256:KECCAK-KMAC256", "provider=default",
124       ossl_keccak_kmac_256_functions },
125
126     /* Our primary name:NIST name */
127     { "SHAKE-128:SHAKE128", "provider=default", ossl_shake_128_functions },
128     { "SHAKE-256:SHAKE256", "provider=default", ossl_shake_256_functions },
129
130 #ifndef OPENSSL_NO_BLAKE2
131     /*
132      * https://blake2.net/ doesn't specify size variants,
133      * but mentions that Bouncy Castle uses the names
134      * BLAKE2b-160, BLAKE2b-256, BLAKE2b-384, and BLAKE2b-512
135      * If we assume that "2b" and "2s" are versions, that pattern
136      * fits with ours.  We also add our historical names.
137      */
138     { "BLAKE2S-256:BLAKE2s256", "provider=default", ossl_blake2s256_functions },
139     { "BLAKE2B-512:BLAKE2b512", "provider=default", ossl_blake2b512_functions },
140 #endif /* OPENSSL_NO_BLAKE2 */
141
142 #ifndef OPENSSL_NO_SM3
143     { "SM3", "provider=default", ossl_sm3_functions },
144 #endif /* OPENSSL_NO_SM3 */
145
146 #ifndef OPENSSL_NO_MD5
147     { "MD5:SSL3-MD5", "provider=default", ossl_md5_functions },
148     { "MD5-SHA1", "provider=default", ossl_md5_sha1_functions },
149 #endif /* OPENSSL_NO_MD5 */
150
151     { NULL, NULL, NULL }
152 };
153
154 static const OSSL_ALGORITHM_CAPABLE deflt_ciphers[] = {
155     ALG("NULL", ossl_null_functions),
156     ALG("AES-256-ECB", ossl_aes256ecb_functions),
157     ALG("AES-192-ECB", ossl_aes192ecb_functions),
158     ALG("AES-128-ECB", ossl_aes128ecb_functions),
159     ALG("AES-256-CBC:AES256", ossl_aes256cbc_functions),
160     ALG("AES-192-CBC:AES192", ossl_aes192cbc_functions),
161     ALG("AES-128-CBC:AES128", ossl_aes128cbc_functions),
162     ALG("AES-128-CBC-CTS", ossl_aes128cbc_cts_functions),
163     ALG("AES-192-CBC-CTS", ossl_aes192cbc_cts_functions),
164     ALG("AES-256-CBC-CTS", ossl_aes256cbc_cts_functions),
165     ALG("AES-256-OFB", ossl_aes256ofb_functions),
166     ALG("AES-192-OFB", ossl_aes192ofb_functions),
167     ALG("AES-128-OFB", ossl_aes128ofb_functions),
168     ALG("AES-256-CFB", ossl_aes256cfb_functions),
169     ALG("AES-192-CFB", ossl_aes192cfb_functions),
170     ALG("AES-128-CFB", ossl_aes128cfb_functions),
171     ALG("AES-256-CFB1", ossl_aes256cfb1_functions),
172     ALG("AES-192-CFB1", ossl_aes192cfb1_functions),
173     ALG("AES-128-CFB1", ossl_aes128cfb1_functions),
174     ALG("AES-256-CFB8", ossl_aes256cfb8_functions),
175     ALG("AES-192-CFB8", ossl_aes192cfb8_functions),
176     ALG("AES-128-CFB8", ossl_aes128cfb8_functions),
177     ALG("AES-256-CTR", ossl_aes256ctr_functions),
178     ALG("AES-192-CTR", ossl_aes192ctr_functions),
179     ALG("AES-128-CTR", ossl_aes128ctr_functions),
180     ALG("AES-256-XTS", ossl_aes256xts_functions),
181     ALG("AES-128-XTS", ossl_aes128xts_functions),
182 #ifndef OPENSSL_NO_OCB
183     ALG("AES-256-OCB", ossl_aes256ocb_functions),
184     ALG("AES-192-OCB", ossl_aes192ocb_functions),
185     ALG("AES-128-OCB", ossl_aes128ocb_functions),
186 #endif /* OPENSSL_NO_OCB */
187 #ifndef OPENSSL_NO_SIV
188     ALG("AES-128-SIV", ossl_aes128siv_functions),
189     ALG("AES-192-SIV", ossl_aes192siv_functions),
190     ALG("AES-256-SIV", ossl_aes256siv_functions),
191 #endif /* OPENSSL_NO_SIV */
192     ALG("AES-256-GCM:id-aes256-GCM", ossl_aes256gcm_functions),
193     ALG("AES-192-GCM:id-aes192-GCM", ossl_aes192gcm_functions),
194     ALG("AES-128-GCM:id-aes128-GCM", ossl_aes128gcm_functions),
195     ALG("AES-256-CCM:id-aes256-CCM", ossl_aes256ccm_functions),
196     ALG("AES-192-CCM:id-aes192-CCM", ossl_aes192ccm_functions),
197     ALG("AES-128-CCM:id-aes128-CCM", ossl_aes128ccm_functions),
198     ALG("AES-256-WRAP:id-aes256-wrap:AES256-WRAP", ossl_aes256wrap_functions),
199     ALG("AES-192-WRAP:id-aes192-wrap:AES192-WRAP", ossl_aes192wrap_functions),
200     ALG("AES-128-WRAP:id-aes128-wrap:AES128-WRAP", ossl_aes128wrap_functions),
201     ALG("AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD",
202         ossl_aes256wrappad_functions),
203     ALG("AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD",
204         ossl_aes192wrappad_functions),
205     ALG("AES-128-WRAP-PAD:id-aes128-wrap-pad:AES128-WRAP-PAD",
206         ossl_aes128wrappad_functions),
207     ALGC("AES-128-CBC-HMAC-SHA1", ossl_aes128cbc_hmac_sha1_functions,
208          ossl_cipher_capable_aes_cbc_hmac_sha1),
209     ALGC("AES-256-CBC-HMAC-SHA1", ossl_aes256cbc_hmac_sha1_functions,
210          ossl_cipher_capable_aes_cbc_hmac_sha1),
211     ALGC("AES-128-CBC-HMAC-SHA256", ossl_aes128cbc_hmac_sha256_functions,
212         ossl_cipher_capable_aes_cbc_hmac_sha256),
213     ALGC("AES-256-CBC-HMAC-SHA256", ossl_aes256cbc_hmac_sha256_functions,
214          ossl_cipher_capable_aes_cbc_hmac_sha256),
215 #ifndef OPENSSL_NO_ARIA
216     ALG("ARIA-256-GCM", ossl_aria256gcm_functions),
217     ALG("ARIA-192-GCM", ossl_aria192gcm_functions),
218     ALG("ARIA-128-GCM", ossl_aria128gcm_functions),
219     ALG("ARIA-256-CCM", ossl_aria256ccm_functions),
220     ALG("ARIA-192-CCM", ossl_aria192ccm_functions),
221     ALG("ARIA-128-CCM", ossl_aria128ccm_functions),
222     ALG("ARIA-256-ECB", ossl_aria256ecb_functions),
223     ALG("ARIA-192-ECB", ossl_aria192ecb_functions),
224     ALG("ARIA-128-ECB", ossl_aria128ecb_functions),
225     ALG("ARIA-256-CBC:ARIA256", ossl_aria256cbc_functions),
226     ALG("ARIA-192-CBC:ARIA192", ossl_aria192cbc_functions),
227     ALG("ARIA-128-CBC:ARIA128", ossl_aria128cbc_functions),
228     ALG("ARIA-256-OFB", ossl_aria256ofb_functions),
229     ALG("ARIA-192-OFB", ossl_aria192ofb_functions),
230     ALG("ARIA-128-OFB", ossl_aria128ofb_functions),
231     ALG("ARIA-256-CFB", ossl_aria256cfb_functions),
232     ALG("ARIA-192-CFB", ossl_aria192cfb_functions),
233     ALG("ARIA-128-CFB", ossl_aria128cfb_functions),
234     ALG("ARIA-256-CFB1", ossl_aria256cfb1_functions),
235     ALG("ARIA-192-CFB1", ossl_aria192cfb1_functions),
236     ALG("ARIA-128-CFB1", ossl_aria128cfb1_functions),
237     ALG("ARIA-256-CFB8", ossl_aria256cfb8_functions),
238     ALG("ARIA-192-CFB8", ossl_aria192cfb8_functions),
239     ALG("ARIA-128-CFB8", ossl_aria128cfb8_functions),
240     ALG("ARIA-256-CTR", ossl_aria256ctr_functions),
241     ALG("ARIA-192-CTR", ossl_aria192ctr_functions),
242     ALG("ARIA-128-CTR", ossl_aria128ctr_functions),
243 #endif /* OPENSSL_NO_ARIA */
244 #ifndef OPENSSL_NO_CAMELLIA
245     ALG("CAMELLIA-256-ECB", ossl_camellia256ecb_functions),
246     ALG("CAMELLIA-192-ECB", ossl_camellia192ecb_functions),
247     ALG("CAMELLIA-128-ECB", ossl_camellia128ecb_functions),
248     ALG("CAMELLIA-256-CBC:CAMELLIA256", ossl_camellia256cbc_functions),
249     ALG("CAMELLIA-192-CBC:CAMELLIA192", ossl_camellia192cbc_functions),
250     ALG("CAMELLIA-128-CBC:CAMELLIA128", ossl_camellia128cbc_functions),
251     ALG("CAMELLIA-256-OFB", ossl_camellia256ofb_functions),
252     ALG("CAMELLIA-192-OFB", ossl_camellia192ofb_functions),
253     ALG("CAMELLIA-128-OFB", ossl_camellia128ofb_functions),
254     ALG("CAMELLIA-256-CFB", ossl_camellia256cfb_functions),
255     ALG("CAMELLIA-192-CFB", ossl_camellia192cfb_functions),
256     ALG("CAMELLIA-128-CFB", ossl_camellia128cfb_functions),
257     ALG("CAMELLIA-256-CFB1", ossl_camellia256cfb1_functions),
258     ALG("CAMELLIA-192-CFB1", ossl_camellia192cfb1_functions),
259     ALG("CAMELLIA-128-CFB1", ossl_camellia128cfb1_functions),
260     ALG("CAMELLIA-256-CFB8", ossl_camellia256cfb8_functions),
261     ALG("CAMELLIA-192-CFB8", ossl_camellia192cfb8_functions),
262     ALG("CAMELLIA-128-CFB8", ossl_camellia128cfb8_functions),
263     ALG("CAMELLIA-256-CTR", ossl_camellia256ctr_functions),
264     ALG("CAMELLIA-192-CTR", ossl_camellia192ctr_functions),
265     ALG("CAMELLIA-128-CTR", ossl_camellia128ctr_functions),
266 #endif /* OPENSSL_NO_CAMELLIA */
267 #ifndef OPENSSL_NO_DES
268     ALG("DES-EDE3-ECB:DES-EDE3", ossl_tdes_ede3_ecb_functions),
269     ALG("DES-EDE3-CBC:DES3", ossl_tdes_ede3_cbc_functions),
270     ALG("DES-EDE3-OFB", ossl_tdes_ede3_ofb_functions),
271     ALG("DES-EDE3-CFB", ossl_tdes_ede3_cfb_functions),
272     ALG("DES-EDE3-CFB8", ossl_tdes_ede3_cfb8_functions),
273     ALG("DES-EDE3-CFB1", ossl_tdes_ede3_cfb1_functions),
274     ALG("DES3-WRAP:id-smime-alg-CMS3DESwrap", ossl_tdes_wrap_cbc_functions),
275     ALG("DES-EDE-ECB:DES-EDE", ossl_tdes_ede2_ecb_functions),
276     ALG("DES-EDE-CBC", ossl_tdes_ede2_cbc_functions),
277     ALG("DES-EDE-OFB", ossl_tdes_ede2_ofb_functions),
278     ALG("DES-EDE-CFB", ossl_tdes_ede2_cfb_functions),
279 #endif /* OPENSSL_NO_DES */
280 #ifndef OPENSSL_NO_SM4
281     ALG("SM4-ECB", ossl_sm4128ecb_functions),
282     ALG("SM4-CBC:SM4", ossl_sm4128cbc_functions),
283     ALG("SM4-CTR", ossl_sm4128ctr_functions),
284     ALG("SM4-OFB:SM4-OFB128", ossl_sm4128ofb128_functions),
285     ALG("SM4-CFB:SM4-CFB128", ossl_sm4128cfb128_functions),
286 #endif /* OPENSSL_NO_SM4 */
287 #ifndef OPENSSL_NO_CHACHA
288     ALG("ChaCha20", ossl_chacha20_functions),
289 # ifndef OPENSSL_NO_POLY1305
290     ALG("ChaCha20-Poly1305", ossl_chacha20_ossl_poly1305_functions),
291 # endif /* OPENSSL_NO_POLY1305 */
292 #endif /* OPENSSL_NO_CHACHA */
293     { { NULL, NULL, NULL }, NULL }
294 };
295 static OSSL_ALGORITHM exported_ciphers[OSSL_NELEM(deflt_ciphers)];
296
297 static const OSSL_ALGORITHM deflt_macs[] = {
298 #ifndef OPENSSL_NO_BLAKE2
299     { "BLAKE2BMAC", "provider=default", ossl_blake2bmac_functions },
300     { "BLAKE2SMAC", "provider=default", ossl_blake2smac_functions },
301 #endif
302 #ifndef OPENSSL_NO_CMAC
303     { "CMAC", "provider=default", ossl_cmac_functions },
304 #endif
305     { "GMAC", "provider=default", ossl_gmac_functions },
306     { "HMAC", "provider=default", ossl_hmac_functions },
307     { "KMAC-128:KMAC128", "provider=default", ossl_kmac128_functions },
308     { "KMAC-256:KMAC256", "provider=default", ossl_kmac256_functions },
309 #ifndef OPENSSL_NO_SIPHASH
310     { "SIPHASH", "provider=default", ossl_siphash_functions },
311 #endif
312 #ifndef OPENSSL_NO_POLY1305
313     { "POLY1305", "provider=default", ossl_poly1305_functions },
314 #endif
315     { NULL, NULL, NULL }
316 };
317
318 static const OSSL_ALGORITHM deflt_kdfs[] = {
319     { "HKDF", "provider=default", ossl_kdf_hkdf_functions },
320     { "SSKDF", "provider=default", ossl_kdf_sskdf_functions },
321     { "PBKDF2", "provider=default", ossl_kdf_pbkdf2_functions },
322     { "PKCS12KDF", "provider=default", ossl_kdf_pkcs12_functions },
323     { "SSHKDF", "provider=default", ossl_kdf_sshkdf_functions },
324     { "X963KDF", "provider=default", ossl_kdf_x963_kdf_functions },
325     { "TLS1-PRF", "provider=default", ossl_kdf_tls1_prf_functions },
326     { "KBKDF", "provider=default", ossl_kdf_kbkdf_functions },
327 #ifndef OPENSSL_NO_CMS
328     { "X942KDF", "provider=default", ossl_kdf_x942_kdf_functions },
329 #endif
330 #ifndef OPENSSL_NO_SCRYPT
331     { "SCRYPT:id-scrypt", "provider=default", ossl_kdf_scrypt_functions },
332 #endif
333     { "KRB5KDF", "provider=default", ossl_kdf_krb5kdf_functions },
334     { NULL, NULL, NULL }
335 };
336
337 static const OSSL_ALGORITHM deflt_keyexch[] = {
338 #ifndef OPENSSL_NO_DH
339     { "DH:dhKeyAgreement", "provider=default", ossl_dh_keyexch_functions },
340 #endif
341 #ifndef OPENSSL_NO_EC
342     { "ECDH", "provider=default", ecossl_dh_keyexch_functions },
343     { "X25519", "provider=default", ossl_x25519_keyexch_functions },
344     { "X448", "provider=default", ossl_x448_keyexch_functions },
345 #endif
346     { "TLS1-PRF", "provider=default", ossl_kdf_tls1_prf_keyexch_functions },
347     { "HKDF", "provider=default", ossl_kdf_hkdf_keyexch_functions },
348     { "SCRYPT:id-scrypt", "provider=default",
349       ossl_kdf_scrypt_keyexch_functions },
350     { NULL, NULL, NULL }
351 };
352
353 static const OSSL_ALGORITHM deflt_rands[] = {
354     { "CTR-DRBG", "provider=default", ossl_drbg_ctr_functions },
355     { "HASH-DRBG", "provider=default", ossl_drbg_hash_functions },
356     { "HMAC-DRBG", "provider=default", ossl_drbg_ossl_hmac_functions },
357     { "TEST-RAND", "provider=default", ossl_test_rng_functions },
358     { NULL, NULL, NULL }
359 };
360
361 static const OSSL_ALGORITHM deflt_signature[] = {
362 #ifndef OPENSSL_NO_DSA
363     { "DSA:dsaEncryption", "provider=default", ossl_dsa_signature_functions },
364 #endif
365     { "RSA:rsaEncryption", "provider=default", ossl_rsa_signature_functions },
366 #ifndef OPENSSL_NO_EC
367     { "ED25519:Ed25519", "provider=default", ossl_ed25519_signature_functions },
368     { "ED448:Ed448", "provider=default", ossl_ed448_signature_functions },
369     { "ECDSA", "provider=default", ecossl_dsa_signature_functions },
370 # ifndef OPENSSL_NO_SM2
371     { "SM2", "provider=default", sm2_signature_functions },
372 # endif
373 #endif
374     { "HMAC", "provider=default", ossl_mac_legacy_hmac_signature_functions },
375     { "SIPHASH", "provider=default",
376       ossl_mac_legacy_siphash_signature_functions },
377 #ifndef OPENSSL_NO_POLY1305
378     { "POLY1305", "provider=default",
379       ossl_mac_legacy_poly1305_signature_functions },
380 #endif
381 #ifndef OPENSSL_NO_CMAC
382     { "CMAC", "provider=default", ossl_mac_legacy_cmac_signature_functions },
383 #endif
384     { NULL, NULL, NULL }
385 };
386
387 static const OSSL_ALGORITHM deflt_asym_cipher[] = {
388     { "RSA:rsaEncryption", "provider=default", ossl_rsa_asym_cipher_functions },
389 #ifndef OPENSSL_NO_SM2
390     { "SM2", "provider=default", sm2_asym_cipher_functions },
391 #endif
392     { NULL, NULL, NULL }
393 };
394
395 static const OSSL_ALGORITHM deflt_asym_kem[] = {
396     { "RSA", "provider=default", ossl_rsa_asym_kem_functions },
397     { NULL, NULL, NULL }
398 };
399
400 static const OSSL_ALGORITHM deflt_keymgmt[] = {
401 #ifndef OPENSSL_NO_DH
402     { "DH:dhKeyAgreement", "provider=default", ossl_dh_keymgmt_functions },
403     { "DHX:X9.42 DH:dhpublicnumber", "provider=default",
404       ossl_dhx_keymgmt_functions },
405 #endif
406 #ifndef OPENSSL_NO_DSA
407     { "DSA:dsaEncryption", "provider=default", ossl_dsa_keymgmt_functions },
408 #endif
409     { "RSA:rsaEncryption", "provider=default", ossl_rsa_keymgmt_functions },
410     { "RSA-PSS:RSASSA-PSS", "provider=default", ossl_rsapss_keymgmt_functions },
411 #ifndef OPENSSL_NO_EC
412     { "EC:id-ecPublicKey", "provider=default", ossl_ec_keymgmt_functions },
413     { "X25519", "provider=default", ossl_x25519_keymgmt_functions },
414     { "X448", "provider=default", ossl_x448_keymgmt_functions },
415     { "ED25519", "provider=default", ossl_ed25519_keymgmt_functions },
416     { "ED448", "provider=default", ossl_ed448_keymgmt_functions },
417 #endif
418     { "TLS1-PRF", "provider=default", ossl_kdf_keymgmt_functions },
419     { "HKDF", "provider=default", ossl_kdf_keymgmt_functions },
420     { "SCRYPT:id-scrypt", "provider=default", ossl_kdf_keymgmt_functions },
421     { "HMAC", "provider=default", ossl_mac_legacy_keymgmt_functions },
422     { "SIPHASH", "provider=default", ossl_mac_legacy_keymgmt_functions },
423 #ifndef OPENSSL_NO_POLY1305
424     { "POLY1305", "provider=default", ossl_mac_legacy_keymgmt_functions },
425 #endif
426 #ifndef OPENSSL_NO_CMAC
427     { "CMAC", "provider=default", ossl_cossl_mac_legacy_keymgmt_functions },
428 #endif
429 #ifndef OPENSSL_NO_SM2
430     { "SM2", "provider=default", sm2_keymgmt_functions },
431 #endif
432     { NULL, NULL, NULL }
433 };
434
435 static const OSSL_ALGORITHM deflt_encoder[] = {
436 #define ENCODER(name, _fips, _output, func_table)                           \
437     { name,                                                                 \
438       "provider=default,fips=" _fips ",output=" _output,                    \
439       (func_table) }
440
441 #include "encoders.inc"
442     { NULL, NULL, NULL }
443 };
444 #undef ENCODER
445
446 static const OSSL_ALGORITHM deflt_decoder[] = {
447 #define DECODER(name, _fips, _input, func_table)                            \
448     { name,                                                                 \
449       "provider=default,fips=" _fips ",input=" _input,                      \
450       (func_table) }
451
452 #include "decoders.inc"
453     { NULL, NULL, NULL }
454 };
455 #undef DECODER
456
457 static const OSSL_ALGORITHM deflt_store[] = {
458 #define STORE(name, _fips, func_table)                           \
459     { name, "provider=default,fips=" _fips, (func_table) },
460
461 #include "stores.inc"
462     { NULL, NULL, NULL }
463 #undef STORE
464 };
465
466 static const OSSL_ALGORITHM *deflt_query(void *provctx, int operation_id,
467                                          int *no_cache)
468 {
469     *no_cache = 0;
470     switch (operation_id) {
471     case OSSL_OP_DIGEST:
472         return deflt_digests;
473     case OSSL_OP_CIPHER:
474         ossl_prov_cache_exported_algorithms(deflt_ciphers, exported_ciphers);
475         return exported_ciphers;
476     case OSSL_OP_MAC:
477         return deflt_macs;
478     case OSSL_OP_KDF:
479         return deflt_kdfs;
480     case OSSL_OP_RAND:
481         return deflt_rands;
482     case OSSL_OP_KEYMGMT:
483         return deflt_keymgmt;
484     case OSSL_OP_KEYEXCH:
485         return deflt_keyexch;
486     case OSSL_OP_SIGNATURE:
487         return deflt_signature;
488     case OSSL_OP_ASYM_CIPHER:
489         return deflt_asym_cipher;
490     case OSSL_OP_KEM:
491         return deflt_asym_kem;
492     case OSSL_OP_ENCODER:
493         return deflt_encoder;
494     case OSSL_OP_DECODER:
495         return deflt_decoder;
496     case OSSL_OP_STORE:
497         return deflt_store;
498     }
499     return NULL;
500 }
501
502
503 static void deflt_teardown(void *provctx)
504 {
505     BIO_meth_free(ossl_prov_ctx_get0_core_bio_method(provctx));
506     ossl_prov_ctx_free(provctx);
507 }
508
509 /* Functions we provide to the core */
510 static const OSSL_DISPATCH deflt_dispatch_table[] = {
511     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))deflt_teardown },
512     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))deflt_gettable_params },
513     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))deflt_get_params },
514     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))deflt_query },
515     { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))provider_get_capabilities },
516     { 0, NULL }
517 };
518
519 OSSL_provider_init_fn ossl_default_provider_init;
520
521 int ossl_default_provider_init(const OSSL_CORE_HANDLE *handle,
522                                const OSSL_DISPATCH *in,
523                                const OSSL_DISPATCH **out,
524                                void **provctx)
525 {
526     OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
527     BIO_METHOD *corebiometh;
528
529     if (!ossl_prov_bio_from_dispatch(in))
530         return 0;
531     for (; in->function_id != 0; in++) {
532         switch (in->function_id) {
533         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
534             c_gettable_params = OSSL_FUNC_core_gettable_params(in);
535             break;
536         case OSSL_FUNC_CORE_GET_PARAMS:
537             c_get_params = OSSL_FUNC_core_get_params(in);
538             break;
539         case OSSL_FUNC_CORE_GET_LIBCTX:
540             c_get_libctx = OSSL_FUNC_core_get_libctx(in);
541             break;
542         default:
543             /* Just ignore anything we don't understand */
544             break;
545         }
546     }
547
548     if (c_get_libctx == NULL)
549         return 0;
550
551     /*
552      * We want to make sure that all calls from this provider that requires
553      * a library context use the same context as the one used to call our
554      * functions.  We do that by passing it along in the provider context.
555      *
556      * This only works for built-in providers.  Most providers should
557      * create their own library context.
558      */
559     if ((*provctx = ossl_prov_ctx_new()) == NULL
560             || (corebiometh = bio_prov_init_bio_method()) == NULL) {
561         ossl_prov_ctx_free(*provctx);
562         *provctx = NULL;
563         return 0;
564     }
565     ossl_prov_ctx_set0_libctx(*provctx,
566                                        (OSSL_LIB_CTX *)c_get_libctx(handle));
567     ossl_prov_ctx_set0_handle(*provctx, handle);
568     ossl_prov_ctx_set0_core_bio_method(*provctx, corebiometh);
569
570     *out = deflt_dispatch_table;
571
572     return 1;
573 }