PROV: Re-implement all the keypair decoders
[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     ALG("AES-256-WRAP-INV:AES256-WRAP-INV", ossl_aes256wrapinv_functions),
208     ALG("AES-192-WRAP-INV:AES192-WRAP-INV", ossl_aes192wrapinv_functions),
209     ALG("AES-128-WRAP-INV:AES128-WRAP-INV", ossl_aes128wrapinv_functions),
210     ALG("AES-256-WRAP-PAD-INV:AES256-WRAP-PAD-INV",
211         ossl_aes256wrappadinv_functions),
212     ALG("AES-192-WRAP-PAD-INV:AES192-WRAP-PAD-INV",
213         ossl_aes192wrappadinv_functions),
214     ALG("AES-128-WRAP-PAD-INV:AES128-WRAP-PAD-INV",
215         ossl_aes128wrappadinv_functions),
216     ALGC("AES-128-CBC-HMAC-SHA1", ossl_aes128cbc_hmac_sha1_functions,
217          ossl_cipher_capable_aes_cbc_hmac_sha1),
218     ALGC("AES-256-CBC-HMAC-SHA1", ossl_aes256cbc_hmac_sha1_functions,
219          ossl_cipher_capable_aes_cbc_hmac_sha1),
220     ALGC("AES-128-CBC-HMAC-SHA256", ossl_aes128cbc_hmac_sha256_functions,
221         ossl_cipher_capable_aes_cbc_hmac_sha256),
222     ALGC("AES-256-CBC-HMAC-SHA256", ossl_aes256cbc_hmac_sha256_functions,
223          ossl_cipher_capable_aes_cbc_hmac_sha256),
224 #ifndef OPENSSL_NO_ARIA
225     ALG("ARIA-256-GCM", ossl_aria256gcm_functions),
226     ALG("ARIA-192-GCM", ossl_aria192gcm_functions),
227     ALG("ARIA-128-GCM", ossl_aria128gcm_functions),
228     ALG("ARIA-256-CCM", ossl_aria256ccm_functions),
229     ALG("ARIA-192-CCM", ossl_aria192ccm_functions),
230     ALG("ARIA-128-CCM", ossl_aria128ccm_functions),
231     ALG("ARIA-256-ECB", ossl_aria256ecb_functions),
232     ALG("ARIA-192-ECB", ossl_aria192ecb_functions),
233     ALG("ARIA-128-ECB", ossl_aria128ecb_functions),
234     ALG("ARIA-256-CBC:ARIA256", ossl_aria256cbc_functions),
235     ALG("ARIA-192-CBC:ARIA192", ossl_aria192cbc_functions),
236     ALG("ARIA-128-CBC:ARIA128", ossl_aria128cbc_functions),
237     ALG("ARIA-256-OFB", ossl_aria256ofb_functions),
238     ALG("ARIA-192-OFB", ossl_aria192ofb_functions),
239     ALG("ARIA-128-OFB", ossl_aria128ofb_functions),
240     ALG("ARIA-256-CFB", ossl_aria256cfb_functions),
241     ALG("ARIA-192-CFB", ossl_aria192cfb_functions),
242     ALG("ARIA-128-CFB", ossl_aria128cfb_functions),
243     ALG("ARIA-256-CFB1", ossl_aria256cfb1_functions),
244     ALG("ARIA-192-CFB1", ossl_aria192cfb1_functions),
245     ALG("ARIA-128-CFB1", ossl_aria128cfb1_functions),
246     ALG("ARIA-256-CFB8", ossl_aria256cfb8_functions),
247     ALG("ARIA-192-CFB8", ossl_aria192cfb8_functions),
248     ALG("ARIA-128-CFB8", ossl_aria128cfb8_functions),
249     ALG("ARIA-256-CTR", ossl_aria256ctr_functions),
250     ALG("ARIA-192-CTR", ossl_aria192ctr_functions),
251     ALG("ARIA-128-CTR", ossl_aria128ctr_functions),
252 #endif /* OPENSSL_NO_ARIA */
253 #ifndef OPENSSL_NO_CAMELLIA
254     ALG("CAMELLIA-256-ECB", ossl_camellia256ecb_functions),
255     ALG("CAMELLIA-192-ECB", ossl_camellia192ecb_functions),
256     ALG("CAMELLIA-128-ECB", ossl_camellia128ecb_functions),
257     ALG("CAMELLIA-256-CBC:CAMELLIA256", ossl_camellia256cbc_functions),
258     ALG("CAMELLIA-192-CBC:CAMELLIA192", ossl_camellia192cbc_functions),
259     ALG("CAMELLIA-128-CBC:CAMELLIA128", ossl_camellia128cbc_functions),
260     ALG("CAMELLIA-256-OFB", ossl_camellia256ofb_functions),
261     ALG("CAMELLIA-192-OFB", ossl_camellia192ofb_functions),
262     ALG("CAMELLIA-128-OFB", ossl_camellia128ofb_functions),
263     ALG("CAMELLIA-256-CFB", ossl_camellia256cfb_functions),
264     ALG("CAMELLIA-192-CFB", ossl_camellia192cfb_functions),
265     ALG("CAMELLIA-128-CFB", ossl_camellia128cfb_functions),
266     ALG("CAMELLIA-256-CFB1", ossl_camellia256cfb1_functions),
267     ALG("CAMELLIA-192-CFB1", ossl_camellia192cfb1_functions),
268     ALG("CAMELLIA-128-CFB1", ossl_camellia128cfb1_functions),
269     ALG("CAMELLIA-256-CFB8", ossl_camellia256cfb8_functions),
270     ALG("CAMELLIA-192-CFB8", ossl_camellia192cfb8_functions),
271     ALG("CAMELLIA-128-CFB8", ossl_camellia128cfb8_functions),
272     ALG("CAMELLIA-256-CTR", ossl_camellia256ctr_functions),
273     ALG("CAMELLIA-192-CTR", ossl_camellia192ctr_functions),
274     ALG("CAMELLIA-128-CTR", ossl_camellia128ctr_functions),
275 #endif /* OPENSSL_NO_CAMELLIA */
276 #ifndef OPENSSL_NO_DES
277     ALG("DES-EDE3-ECB:DES-EDE3", ossl_tdes_ede3_ecb_functions),
278     ALG("DES-EDE3-CBC:DES3", ossl_tdes_ede3_cbc_functions),
279     ALG("DES-EDE3-OFB", ossl_tdes_ede3_ofb_functions),
280     ALG("DES-EDE3-CFB", ossl_tdes_ede3_cfb_functions),
281     ALG("DES-EDE3-CFB8", ossl_tdes_ede3_cfb8_functions),
282     ALG("DES-EDE3-CFB1", ossl_tdes_ede3_cfb1_functions),
283     ALG("DES3-WRAP:id-smime-alg-CMS3DESwrap", ossl_tdes_wrap_cbc_functions),
284     ALG("DES-EDE-ECB:DES-EDE", ossl_tdes_ede2_ecb_functions),
285     ALG("DES-EDE-CBC", ossl_tdes_ede2_cbc_functions),
286     ALG("DES-EDE-OFB", ossl_tdes_ede2_ofb_functions),
287     ALG("DES-EDE-CFB", ossl_tdes_ede2_cfb_functions),
288 #endif /* OPENSSL_NO_DES */
289 #ifndef OPENSSL_NO_SM4
290     ALG("SM4-ECB", ossl_sm4128ecb_functions),
291     ALG("SM4-CBC:SM4", ossl_sm4128cbc_functions),
292     ALG("SM4-CTR", ossl_sm4128ctr_functions),
293     ALG("SM4-OFB:SM4-OFB128", ossl_sm4128ofb128_functions),
294     ALG("SM4-CFB:SM4-CFB128", ossl_sm4128cfb128_functions),
295 #endif /* OPENSSL_NO_SM4 */
296 #ifndef OPENSSL_NO_CHACHA
297     ALG("ChaCha20", ossl_chacha20_functions),
298 # ifndef OPENSSL_NO_POLY1305
299     ALG("ChaCha20-Poly1305", ossl_chacha20_ossl_poly1305_functions),
300 # endif /* OPENSSL_NO_POLY1305 */
301 #endif /* OPENSSL_NO_CHACHA */
302     { { NULL, NULL, NULL }, NULL }
303 };
304 static OSSL_ALGORITHM exported_ciphers[OSSL_NELEM(deflt_ciphers)];
305
306 static const OSSL_ALGORITHM deflt_macs[] = {
307 #ifndef OPENSSL_NO_BLAKE2
308     { "BLAKE2BMAC", "provider=default", ossl_blake2bmac_functions },
309     { "BLAKE2SMAC", "provider=default", ossl_blake2smac_functions },
310 #endif
311 #ifndef OPENSSL_NO_CMAC
312     { "CMAC", "provider=default", ossl_cmac_functions },
313 #endif
314     { "GMAC", "provider=default", ossl_gmac_functions },
315     { "HMAC", "provider=default", ossl_hmac_functions },
316     { "KMAC-128:KMAC128", "provider=default", ossl_kmac128_functions },
317     { "KMAC-256:KMAC256", "provider=default", ossl_kmac256_functions },
318 #ifndef OPENSSL_NO_SIPHASH
319     { "SIPHASH", "provider=default", ossl_siphash_functions },
320 #endif
321 #ifndef OPENSSL_NO_POLY1305
322     { "POLY1305", "provider=default", ossl_poly1305_functions },
323 #endif
324     { NULL, NULL, NULL }
325 };
326
327 static const OSSL_ALGORITHM deflt_kdfs[] = {
328     { "HKDF", "provider=default", ossl_kdf_hkdf_functions },
329     { "SSKDF", "provider=default", ossl_kdf_sskdf_functions },
330     { "PBKDF2", "provider=default", ossl_kdf_pbkdf2_functions },
331     { "PKCS12KDF", "provider=default", ossl_kdf_pkcs12_functions },
332     { "SSHKDF", "provider=default", ossl_kdf_sshkdf_functions },
333     { "X963KDF", "provider=default", ossl_kdf_x963_kdf_functions },
334     { "TLS1-PRF", "provider=default", ossl_kdf_tls1_prf_functions },
335     { "KBKDF", "provider=default", ossl_kdf_kbkdf_functions },
336 #ifndef OPENSSL_NO_CMS
337     { "X942KDF", "provider=default", ossl_kdf_x942_kdf_functions },
338 #endif
339 #ifndef OPENSSL_NO_SCRYPT
340     { "SCRYPT:id-scrypt", "provider=default", ossl_kdf_scrypt_functions },
341 #endif
342     { "KRB5KDF", "provider=default", ossl_kdf_krb5kdf_functions },
343     { NULL, NULL, NULL }
344 };
345
346 static const OSSL_ALGORITHM deflt_keyexch[] = {
347 #ifndef OPENSSL_NO_DH
348     { "DH:dhKeyAgreement", "provider=default", ossl_dh_keyexch_functions },
349 #endif
350 #ifndef OPENSSL_NO_EC
351     { "ECDH", "provider=default", ecossl_dh_keyexch_functions },
352     { "X25519", "provider=default", ossl_x25519_keyexch_functions },
353     { "X448", "provider=default", ossl_x448_keyexch_functions },
354 #endif
355     { "TLS1-PRF", "provider=default", ossl_kdf_tls1_prf_keyexch_functions },
356     { "HKDF", "provider=default", ossl_kdf_hkdf_keyexch_functions },
357     { "SCRYPT:id-scrypt", "provider=default",
358       ossl_kdf_scrypt_keyexch_functions },
359     { NULL, NULL, NULL }
360 };
361
362 static const OSSL_ALGORITHM deflt_rands[] = {
363     { "CTR-DRBG", "provider=default", ossl_drbg_ctr_functions },
364     { "HASH-DRBG", "provider=default", ossl_drbg_hash_functions },
365     { "HMAC-DRBG", "provider=default", ossl_drbg_ossl_hmac_functions },
366     { "TEST-RAND", "provider=default", ossl_test_rng_functions },
367     { NULL, NULL, NULL }
368 };
369
370 static const OSSL_ALGORITHM deflt_signature[] = {
371 #ifndef OPENSSL_NO_DSA
372     { "DSA:dsaEncryption", "provider=default", ossl_dsa_signature_functions },
373 #endif
374     { "RSA:rsaEncryption", "provider=default", ossl_rsa_signature_functions },
375 #ifndef OPENSSL_NO_EC
376     { "ED25519", "provider=default", ossl_ed25519_signature_functions },
377     { "ED448", "provider=default", ossl_ed448_signature_functions },
378     { "ECDSA", "provider=default", ecossl_dsa_signature_functions },
379 # ifndef OPENSSL_NO_SM2
380     { "SM2", "provider=default", sm2_signature_functions },
381 # endif
382 #endif
383     { "HMAC", "provider=default", ossl_mac_legacy_hmac_signature_functions },
384     { "SIPHASH", "provider=default",
385       ossl_mac_legacy_siphash_signature_functions },
386 #ifndef OPENSSL_NO_POLY1305
387     { "POLY1305", "provider=default",
388       ossl_mac_legacy_poly1305_signature_functions },
389 #endif
390 #ifndef OPENSSL_NO_CMAC
391     { "CMAC", "provider=default", ossl_mac_legacy_cmac_signature_functions },
392 #endif
393     { NULL, NULL, NULL }
394 };
395
396 static const OSSL_ALGORITHM deflt_asym_cipher[] = {
397     { "RSA:rsaEncryption", "provider=default", ossl_rsa_asym_cipher_functions },
398 #ifndef OPENSSL_NO_SM2
399     { "SM2", "provider=default", sm2_asym_cipher_functions },
400 #endif
401     { NULL, NULL, NULL }
402 };
403
404 static const OSSL_ALGORITHM deflt_asym_kem[] = {
405     { "RSA", "provider=default", ossl_rsa_asym_kem_functions },
406     { NULL, NULL, NULL }
407 };
408
409 static const OSSL_ALGORITHM deflt_keymgmt[] = {
410 #ifndef OPENSSL_NO_DH
411     { "DH:dhKeyAgreement", "provider=default", ossl_dh_keymgmt_functions },
412     { "DHX:X9.42 DH:dhpublicnumber", "provider=default",
413       ossl_dhx_keymgmt_functions },
414 #endif
415 #ifndef OPENSSL_NO_DSA
416     { "DSA:dsaEncryption", "provider=default", ossl_dsa_keymgmt_functions },
417 #endif
418     { "RSA:rsaEncryption", "provider=default", ossl_rsa_keymgmt_functions },
419     { "RSA-PSS:RSASSA-PSS", "provider=default", ossl_rsapss_keymgmt_functions },
420 #ifndef OPENSSL_NO_EC
421     { "EC:id-ecPublicKey", "provider=default", ossl_ec_keymgmt_functions },
422     { "X25519", "provider=default", ossl_x25519_keymgmt_functions },
423     { "X448", "provider=default", ossl_x448_keymgmt_functions },
424     { "ED25519", "provider=default", ossl_ed25519_keymgmt_functions },
425     { "ED448", "provider=default", ossl_ed448_keymgmt_functions },
426 #endif
427     { "TLS1-PRF", "provider=default", ossl_kdf_keymgmt_functions },
428     { "HKDF", "provider=default", ossl_kdf_keymgmt_functions },
429     { "SCRYPT:id-scrypt", "provider=default", ossl_kdf_keymgmt_functions },
430     { "HMAC", "provider=default", ossl_mac_legacy_keymgmt_functions },
431     { "SIPHASH", "provider=default", ossl_mac_legacy_keymgmt_functions },
432 #ifndef OPENSSL_NO_POLY1305
433     { "POLY1305", "provider=default", ossl_mac_legacy_keymgmt_functions },
434 #endif
435 #ifndef OPENSSL_NO_CMAC
436     { "CMAC", "provider=default", ossl_cossl_mac_legacy_keymgmt_functions },
437 #endif
438 #ifndef OPENSSL_NO_SM2
439     { "SM2", "provider=default", sm2_keymgmt_functions },
440 #endif
441     { NULL, NULL, NULL }
442 };
443
444 static const OSSL_ALGORITHM deflt_encoder[] = {
445 #define ENCODER(name, _fips, _output, func_table)                           \
446     { name,                                                                 \
447       "provider=default,fips=" _fips ",output=" _output,                    \
448       (func_table) }
449
450 #include "encoders.inc"
451     { NULL, NULL, NULL }
452 };
453 #undef ENCODER
454
455 static const OSSL_ALGORITHM deflt_decoder[] = {
456 #define DECODER_PROVIDER "default"
457 #include "decoders.inc"
458     { NULL, NULL, NULL }
459 #undef DECODER_PROVIDER
460 };
461
462 static const OSSL_ALGORITHM deflt_store[] = {
463 #define STORE(name, _fips, func_table)                           \
464     { name, "provider=default,fips=" _fips, (func_table) },
465
466 #include "stores.inc"
467     { NULL, NULL, NULL }
468 #undef STORE
469 };
470
471 static const OSSL_ALGORITHM *deflt_query(void *provctx, int operation_id,
472                                          int *no_cache)
473 {
474     *no_cache = 0;
475     switch (operation_id) {
476     case OSSL_OP_DIGEST:
477         return deflt_digests;
478     case OSSL_OP_CIPHER:
479         ossl_prov_cache_exported_algorithms(deflt_ciphers, exported_ciphers);
480         return exported_ciphers;
481     case OSSL_OP_MAC:
482         return deflt_macs;
483     case OSSL_OP_KDF:
484         return deflt_kdfs;
485     case OSSL_OP_RAND:
486         return deflt_rands;
487     case OSSL_OP_KEYMGMT:
488         return deflt_keymgmt;
489     case OSSL_OP_KEYEXCH:
490         return deflt_keyexch;
491     case OSSL_OP_SIGNATURE:
492         return deflt_signature;
493     case OSSL_OP_ASYM_CIPHER:
494         return deflt_asym_cipher;
495     case OSSL_OP_KEM:
496         return deflt_asym_kem;
497     case OSSL_OP_ENCODER:
498         return deflt_encoder;
499     case OSSL_OP_DECODER:
500         return deflt_decoder;
501     case OSSL_OP_STORE:
502         return deflt_store;
503     }
504     return NULL;
505 }
506
507
508 static void deflt_teardown(void *provctx)
509 {
510     BIO_meth_free(ossl_prov_ctx_get0_core_bio_method(provctx));
511     ossl_prov_ctx_free(provctx);
512 }
513
514 /* Functions we provide to the core */
515 static const OSSL_DISPATCH deflt_dispatch_table[] = {
516     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))deflt_teardown },
517     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))deflt_gettable_params },
518     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))deflt_get_params },
519     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))deflt_query },
520     { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))provider_get_capabilities },
521     { 0, NULL }
522 };
523
524 OSSL_provider_init_fn ossl_default_provider_init;
525
526 int ossl_default_provider_init(const OSSL_CORE_HANDLE *handle,
527                                const OSSL_DISPATCH *in,
528                                const OSSL_DISPATCH **out,
529                                void **provctx)
530 {
531     OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
532     BIO_METHOD *corebiometh;
533
534     if (!ossl_prov_bio_from_dispatch(in))
535         return 0;
536     for (; in->function_id != 0; in++) {
537         switch (in->function_id) {
538         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
539             c_gettable_params = OSSL_FUNC_core_gettable_params(in);
540             break;
541         case OSSL_FUNC_CORE_GET_PARAMS:
542             c_get_params = OSSL_FUNC_core_get_params(in);
543             break;
544         case OSSL_FUNC_CORE_GET_LIBCTX:
545             c_get_libctx = OSSL_FUNC_core_get_libctx(in);
546             break;
547         default:
548             /* Just ignore anything we don't understand */
549             break;
550         }
551     }
552
553     if (c_get_libctx == NULL)
554         return 0;
555
556     /*
557      * We want to make sure that all calls from this provider that requires
558      * a library context use the same context as the one used to call our
559      * functions.  We do that by passing it along in the provider context.
560      *
561      * This only works for built-in providers.  Most providers should
562      * create their own library context.
563      */
564     if ((*provctx = ossl_prov_ctx_new()) == NULL
565             || (corebiometh = bio_prov_init_bio_method()) == NULL) {
566         ossl_prov_ctx_free(*provctx);
567         *provctx = NULL;
568         return 0;
569     }
570     ossl_prov_ctx_set0_libctx(*provctx,
571                                        (OSSL_LIB_CTX *)c_get_libctx(handle));
572     ossl_prov_ctx_set0_handle(*provctx, handle);
573     ossl_prov_ctx_set0_core_bio_method(*provctx, corebiometh);
574
575     *out = deflt_dispatch_table;
576
577     return 1;
578 }