Add the concept of "Capabilities" to the default and fips providers
[oweals/openssl.git] / providers / fips / fipsprov.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/core.h>
13 #include <openssl/core_numbers.h>
14 #include <openssl/core_names.h>
15 #include <openssl/params.h>
16 #include <openssl/err.h>
17 #include <openssl/evp.h>
18 #include <openssl/kdf.h>
19
20 /* TODO(3.0): Needed for dummy_evp_call(). To be removed */
21 #include <openssl/sha.h>
22 #include <openssl/rand_drbg.h>
23 #include <openssl/ec.h>
24 #include <openssl/fips_names.h>
25
26 #include "internal/cryptlib.h"
27 #include "internal/property.h"
28 #include "internal/nelem.h"
29 #include "openssl/param_build.h"
30 #include "crypto/evp.h"
31 #include "prov/implementations.h"
32 #include "prov/provider_ctx.h"
33 #include "prov/providercommon.h"
34 #include "prov/provider_util.h"
35 #include "self_test.h"
36
37 /*
38  * Forward declarations to ensure that interface functions are correctly
39  * defined.
40  */
41 static OSSL_provider_teardown_fn fips_teardown;
42 static OSSL_provider_gettable_params_fn fips_gettable_params;
43 static OSSL_provider_get_params_fn fips_get_params;
44 static OSSL_provider_query_operation_fn fips_query;
45
46 #define ALGC(NAMES, FUNC, CHECK) { { NAMES, "provider=fips,fips=yes", FUNC }, CHECK }
47 #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
48
49 extern OSSL_core_thread_start_fn *c_thread_start;
50
51 /*
52  * TODO(3.0): Should these be stored in the provider side provctx? Could they
53  * ever be different from one init to the next? Unfortunately we can't do this
54  * at the moment because c_put_error/c_add_error_vdata do not provide
55  * us with the OPENSSL_CTX as a parameter.
56  */
57
58 static SELF_TEST_POST_PARAMS selftest_params;
59
60 /* Functions provided by the core */
61 static OSSL_core_gettable_params_fn *c_gettable_params;
62 static OSSL_core_get_params_fn *c_get_params;
63 OSSL_core_thread_start_fn *c_thread_start;
64 static OSSL_core_new_error_fn *c_new_error;
65 static OSSL_core_set_error_debug_fn *c_set_error_debug;
66 static OSSL_core_vset_error_fn *c_vset_error;
67 static OSSL_core_set_error_mark_fn *c_set_error_mark;
68 static OSSL_core_clear_last_error_mark_fn *c_clear_last_error_mark;
69 static OSSL_core_pop_error_to_mark_fn *c_pop_error_to_mark;
70 static OSSL_CRYPTO_malloc_fn *c_CRYPTO_malloc;
71 static OSSL_CRYPTO_zalloc_fn *c_CRYPTO_zalloc;
72 static OSSL_CRYPTO_free_fn *c_CRYPTO_free;
73 static OSSL_CRYPTO_clear_free_fn *c_CRYPTO_clear_free;
74 static OSSL_CRYPTO_realloc_fn *c_CRYPTO_realloc;
75 static OSSL_CRYPTO_clear_realloc_fn *c_CRYPTO_clear_realloc;
76 static OSSL_CRYPTO_secure_malloc_fn *c_CRYPTO_secure_malloc;
77 static OSSL_CRYPTO_secure_zalloc_fn *c_CRYPTO_secure_zalloc;
78 static OSSL_CRYPTO_secure_free_fn *c_CRYPTO_secure_free;
79 static OSSL_CRYPTO_secure_clear_free_fn *c_CRYPTO_secure_clear_free;
80 static OSSL_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated;
81 static OSSL_BIO_vsnprintf_fn *c_BIO_vsnprintf;
82
83 typedef struct fips_global_st {
84     const OSSL_CORE_HANDLE *handle;
85 } FIPS_GLOBAL;
86
87 static void *fips_prov_ossl_ctx_new(OPENSSL_CTX *libctx)
88 {
89     FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
90
91     return fgbl;
92 }
93
94 static void fips_prov_ossl_ctx_free(void *fgbl)
95 {
96     OPENSSL_free(fgbl);
97 }
98
99 static const OPENSSL_CTX_METHOD fips_prov_ossl_ctx_method = {
100     fips_prov_ossl_ctx_new,
101     fips_prov_ossl_ctx_free,
102 };
103
104
105 /* Parameters we provide to the core */
106 static const OSSL_PARAM fips_param_types[] = {
107     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
108     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
109     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
110     OSSL_PARAM_END
111 };
112
113 /*
114  * Parameters to retrieve from the core provider - required for self testing.
115  * NOTE: inside core_get_params() these will be loaded from config items
116  * stored inside prov->parameters (except for
117  * OSSL_PROV_PARAM_CORE_MODULE_FILENAME).
118  */
119 static OSSL_PARAM core_params[] =
120 {
121     OSSL_PARAM_utf8_ptr(OSSL_PROV_PARAM_CORE_MODULE_FILENAME,
122                         selftest_params.module_filename,
123                         sizeof(selftest_params.module_filename)),
124     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_MODULE_MAC,
125                         selftest_params.module_checksum_data,
126                         sizeof(selftest_params.module_checksum_data)),
127     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_MAC,
128                         selftest_params.indicator_checksum_data,
129                         sizeof(selftest_params.indicator_checksum_data)),
130     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
131                         selftest_params.indicator_data,
132                         sizeof(selftest_params.indicator_data)),
133     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
134                         selftest_params.indicator_version,
135                         sizeof(selftest_params.indicator_version)),
136     OSSL_PARAM_END
137 };
138
139 /* TODO(3.0): To be removed */
140 static int dummy_evp_call(OPENSSL_CTX *libctx)
141 {
142     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
143     EVP_MD *sha256 = EVP_MD_fetch(libctx, "SHA256", NULL);
144     EVP_KDF *kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF2, NULL);
145     unsigned char dgst[SHA256_DIGEST_LENGTH];
146     unsigned int dgstlen;
147     int ret = 0;
148     BN_CTX *bnctx = NULL;
149     BIGNUM *a = NULL, *b = NULL;
150     unsigned char randbuf[128];
151     RAND_DRBG *drbg = OPENSSL_CTX_get0_public_drbg(libctx);
152 #ifndef OPENSSL_NO_EC
153     EC_KEY *key = NULL;
154 #endif
155
156     static const char msg[] = "Hello World!";
157     static const unsigned char exptd[] = {
158         0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
159         0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
160         0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
161     };
162
163     if (ctx == NULL || sha256 == NULL || drbg == NULL || kdf == NULL)
164         goto err;
165
166     if (!EVP_DigestInit_ex(ctx, sha256, NULL))
167         goto err;
168     if (!EVP_DigestUpdate(ctx, msg, sizeof(msg) - 1))
169         goto err;
170     if (!EVP_DigestFinal(ctx, dgst, &dgstlen))
171         goto err;
172     if (dgstlen != sizeof(exptd) || memcmp(dgst, exptd, sizeof(exptd)) != 0)
173         goto err;
174
175     bnctx = BN_CTX_new_ex(libctx);
176     if (bnctx == NULL)
177         goto err;
178     BN_CTX_start(bnctx);
179     a = BN_CTX_get(bnctx);
180     b = BN_CTX_get(bnctx);
181     if (b == NULL)
182         goto err;
183     BN_zero(a);
184     if (!BN_one(b)
185         || !BN_add(a, a, b)
186         || BN_cmp(a, b) != 0)
187         goto err;
188
189     if (RAND_DRBG_bytes(drbg, randbuf, sizeof(randbuf)) <= 0)
190         goto err;
191
192     if (!BN_rand_ex(a, 256, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, bnctx))
193         goto err;
194
195 #ifndef OPENSSL_NO_EC
196     /* Do some dummy EC calls */
197     key = EC_KEY_new_by_curve_name_ex(libctx, NID_X9_62_prime256v1);
198     if (key == NULL)
199         goto err;
200
201     if (!EC_KEY_generate_key(key))
202         goto err;
203 #endif
204
205     ret = 1;
206  err:
207     BN_CTX_end(bnctx);
208     BN_CTX_free(bnctx);
209
210     EVP_KDF_free(kdf);
211     EVP_MD_CTX_free(ctx);
212     EVP_MD_free(sha256);
213
214 #ifndef OPENSSL_NO_EC
215     EC_KEY_free(key);
216 #endif
217     return ret;
218 }
219
220 static const OSSL_PARAM *fips_gettable_params(void *provctx)
221 {
222     return fips_param_types;
223 }
224
225 static int fips_get_params(void *provctx, OSSL_PARAM params[])
226 {
227     OSSL_PARAM *p;
228
229     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
230     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
231         return 0;
232     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
233     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
234         return 0;
235     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
236     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
237         return 0;
238
239     return 1;
240 }
241
242 /* FIPS specific version of the function of the same name in provlib.c */
243 const char *ossl_prov_util_nid_to_name(int nid)
244 {
245     /* We don't have OBJ_nid2n() in FIPS_MODULE so we have an explicit list */
246
247     switch (nid) {
248     /* Digests */
249     case NID_sha1:
250         return "SHA1";
251     case NID_sha224:
252         return "SHA-224";
253     case NID_sha256:
254         return "SHA-256";
255     case NID_sha384:
256         return "SHA-384";
257     case NID_sha512:
258         return "SHA-512";
259     case NID_sha512_224:
260         return "SHA-512/224";
261     case NID_sha512_256:
262         return "SHA-512/256";
263     case NID_sha3_224:
264         return "SHA3-224";
265     case NID_sha3_256:
266         return "SHA3-256";
267     case NID_sha3_384:
268         return "SHA3-384";
269     case NID_sha3_512:
270         return "SHA3-512";
271
272     /* Ciphers */
273     case NID_aes_256_ecb:
274         return "AES-256-ECB";
275     case NID_aes_192_ecb:
276         return "AES-192-ECB";
277     case NID_aes_128_ecb:
278         return "AES-128-ECB";
279     case NID_aes_256_cbc:
280         return "AES-256-CBC";
281     case NID_aes_192_cbc:
282         return "AES-192-CBC";
283     case NID_aes_128_cbc:
284         return "AES-128-CBC";
285     case NID_aes_256_ctr:
286         return "AES-256-CTR";
287     case NID_aes_192_ctr:
288         return "AES-192-CTR";
289     case NID_aes_128_ctr:
290         return "AES-128-CTR";
291     case NID_aes_256_xts:
292         return "AES-256-XTS";
293     case NID_aes_128_xts:
294         return "AES-128-XTS";
295     case NID_aes_256_gcm:
296         return "AES-256-GCM";
297     case NID_aes_192_gcm:
298         return "AES-192-GCM";
299     case NID_aes_128_gcm:
300         return "AES-128-GCM";
301     case NID_aes_256_ccm:
302         return "AES-256-CCM";
303     case NID_aes_192_ccm:
304         return "AES-192-CCM";
305     case NID_aes_128_ccm:
306         return "AES-128-CCM";
307     case NID_id_aes256_wrap:
308         return "AES-256-WRAP";
309     case NID_id_aes192_wrap:
310         return "AES-192-WRAP";
311     case NID_id_aes128_wrap:
312         return "AES-128-WRAP";
313     case NID_id_aes256_wrap_pad:
314         return "AES-256-WRAP-PAD";
315     case NID_id_aes192_wrap_pad:
316         return "AES-192-WRAP-PAD";
317     case NID_id_aes128_wrap_pad:
318         return "AES-128-WRAP-PAD";
319     case NID_des_ede3_ecb:
320         return "DES-EDE3";
321     case NID_des_ede3_cbc:
322         return "DES-EDE3-CBC";
323     case NID_aes_256_cbc_hmac_sha256:
324         return "AES-256-CBC-HMAC-SHA256";
325     case NID_aes_128_cbc_hmac_sha256:
326         return "AES-128-CBC-HMAC-SHA256";
327     case NID_aes_256_cbc_hmac_sha1:
328         return "AES-256-CBC-HMAC-SHA1";
329     case NID_aes_128_cbc_hmac_sha1:
330         return "AES-128-CBC-HMAC-SHA1";
331     default:
332         break;
333     }
334
335     return NULL;
336 }
337
338 /*
339  * For the algorithm names, we use the following formula for our primary
340  * names:
341  *
342  *     ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
343  *
344  *     VERSION is only present if there are multiple versions of
345  *     an alg (MD2, MD4, MD5).  It may be omitted if there is only
346  *     one version (if a subsequent version is released in the future,
347  *     we can always change the canonical name, and add the old name
348  *     as an alias).
349  *
350  *     SUBNAME may be present where we are combining multiple
351  *     algorithms together, e.g. MD5-SHA1.
352  *
353  *     SIZE is only present if multiple versions of an algorithm exist
354  *     with different sizes (e.g. AES-128-CBC, AES-256-CBC)
355  *
356  *     MODE is only present where applicable.
357  *
358  * We add diverse other names where applicable, such as the names that
359  * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
360  * we have used historically.
361  */
362 static const OSSL_ALGORITHM fips_digests[] = {
363     /* Our primary name:NiST name[:our older names] */
364     { "SHA1:SHA-1", "provider=fips,fips=yes", sha1_functions },
365     { "SHA2-224:SHA-224:SHA224", "provider=fips,fips=yes", sha224_functions },
366     { "SHA2-256:SHA-256:SHA256", "provider=fips,fips=yes", sha256_functions },
367     { "SHA2-384:SHA-384:SHA384", "provider=fips,fips=yes", sha384_functions },
368     { "SHA2-512:SHA-512:SHA512", "provider=fips,fips=yes", sha512_functions },
369     { "SHA2-512/224:SHA-512/224:SHA512-224", "provider=fips,fips=yes",
370       sha512_224_functions },
371     { "SHA2-512/256:SHA-512/256:SHA512-256", "provider=fips,fips=yes",
372       sha512_256_functions },
373
374     /* We agree with NIST here, so one name only */
375     { "SHA3-224", "provider=fips,fips=yes", sha3_224_functions },
376     { "SHA3-256", "provider=fips,fips=yes", sha3_256_functions },
377     { "SHA3-384", "provider=fips,fips=yes", sha3_384_functions },
378     { "SHA3-512", "provider=fips,fips=yes", sha3_512_functions },
379
380     { "SHAKE-128:SHAKE128", "provider=fips,fips=yes", shake_128_functions },
381     { "SHAKE-256:SHAKE256", "provider=fips,fips=yes", shake_256_functions },
382
383     /*
384      * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
385      * KMAC128 and KMAC256.
386      */
387     { "KECCAK-KMAC-128:KECCAK-KMAC128", "provider=fips,fips=yes",
388       keccak_kmac_128_functions },
389     { "KECCAK-KMAC-256:KECCAK-KMAC256", "provider=fips,fips=yes",
390       keccak_kmac_256_functions },
391     { NULL, NULL, NULL }
392 };
393
394 static const OSSL_ALGORITHM_CAPABLE fips_ciphers[] = {
395     /* Our primary name[:ASN.1 OID name][:our older names] */
396     ALG("AES-256-ECB", aes256ecb_functions),
397     ALG("AES-192-ECB", aes192ecb_functions),
398     ALG("AES-128-ECB", aes128ecb_functions),
399     ALG("AES-256-CBC", aes256cbc_functions),
400     ALG("AES-192-CBC", aes192cbc_functions),
401     ALG("AES-128-CBC", aes128cbc_functions),
402     ALG("AES-256-OFB", aes256ofb_functions),
403     ALG("AES-192-OFB", aes192ofb_functions),
404     ALG("AES-128-OFB", aes128ofb_functions),
405     ALG("AES-256-CFB", aes256cfb_functions),
406     ALG("AES-192-CFB", aes192cfb_functions),
407     ALG("AES-128-CFB", aes128cfb_functions),
408     ALG("AES-256-CFB1", aes256cfb1_functions),
409     ALG("AES-192-CFB1", aes192cfb1_functions),
410     ALG("AES-128-CFB1", aes128cfb1_functions),
411     ALG("AES-256-CFB8", aes256cfb8_functions),
412     ALG("AES-192-CFB8", aes192cfb8_functions),
413     ALG("AES-128-CFB8", aes128cfb8_functions),
414     ALG("AES-256-CTR", aes256ctr_functions),
415     ALG("AES-192-CTR", aes192ctr_functions),
416     ALG("AES-128-CTR", aes128ctr_functions),
417     ALG("AES-256-XTS", aes256xts_functions),
418     ALG("AES-128-XTS", aes128xts_functions),
419     ALG("AES-256-GCM:id-aes256-GCM", aes256gcm_functions),
420     ALG("AES-192-GCM:id-aes192-GCM", aes192gcm_functions),
421     ALG("AES-128-GCM:id-aes128-GCM", aes128gcm_functions),
422     ALG("AES-256-CCM:id-aes256-CCM", aes256ccm_functions),
423     ALG("AES-192-CCM:id-aes192-CCM", aes192ccm_functions),
424     ALG("AES-128-CCM:id-aes128-CCM", aes128ccm_functions),
425     ALG("AES-256-WRAP:id-aes256-wrap:AES256-WRAP", aes256wrap_functions),
426     ALG("AES-192-WRAP:id-aes192-wrap:AES192-WRAP", aes192wrap_functions),
427     ALG("AES-128-WRAP:id-aes128-wrap:AES128-WRAP", aes128wrap_functions),
428     ALG("AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD",
429         aes256wrappad_functions),
430     ALG("AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD",
431         aes192wrappad_functions),
432     ALG("AES-128-WRAP-PAD:id-aes128-wrap-pad:AES128-WRAP-PAD",
433         aes128wrappad_functions),
434     ALGC("AES-128-CBC-HMAC-SHA1", aes128cbc_hmac_sha1_functions,
435          cipher_capable_aes_cbc_hmac_sha1),
436     ALGC("AES-256-CBC-HMAC-SHA1", aes256cbc_hmac_sha1_functions,
437          cipher_capable_aes_cbc_hmac_sha1),
438     ALGC("AES-128-CBC-HMAC-SHA256", aes128cbc_hmac_sha256_functions,
439          cipher_capable_aes_cbc_hmac_sha256),
440     ALGC("AES-256-CBC-HMAC-SHA256", aes256cbc_hmac_sha256_functions,
441          cipher_capable_aes_cbc_hmac_sha256),
442 #ifndef OPENSSL_NO_DES
443     ALG("DES-EDE3-ECB:DES-EDE3", tdes_ede3_ecb_functions),
444     ALG("DES-EDE3-CBC:DES3", tdes_ede3_cbc_functions),
445 #endif  /* OPENSSL_NO_DES */
446     { { NULL, NULL, NULL }, NULL }
447 };
448 static OSSL_ALGORITHM exported_fips_ciphers[OSSL_NELEM(fips_ciphers)];
449
450 static const OSSL_ALGORITHM fips_macs[] = {
451 #ifndef OPENSSL_NO_CMAC
452     { "CMAC", "provider=fips,fips=yes", cmac_functions },
453 #endif
454     { "GMAC", "provider=fips,fips=yes", gmac_functions },
455     { "HMAC", "provider=fips,fips=yes", hmac_functions },
456     { "KMAC-128:KMAC128", "provider=fips,fips=yes", kmac128_functions },
457     { "KMAC-256:KMAC256", "provider=fips,fips=yes", kmac256_functions },
458     { NULL, NULL, NULL }
459 };
460
461 static const OSSL_ALGORITHM fips_kdfs[] = {
462     { "HKDF", "provider=fips,fips=yes", kdf_hkdf_functions },
463     { "SSKDF", "provider=fips,fips=yes", kdf_sskdf_functions },
464     { "PBKDF2", "provider=fips,fips=yes", kdf_pbkdf2_functions },
465     { "SSHKDF", "provider=fips,fips=yes", kdf_sshkdf_functions },
466     { "X963KDF", "provider=fips,fips=yes", kdf_x963_kdf_functions },
467     { "TLS1-PRF", "provider=fips,fips=yes", kdf_tls1_prf_functions },
468     { "KBKDF", "provider=fips,fips=yes", kdf_kbkdf_functions },
469     { NULL, NULL, NULL }
470 };
471
472 static const OSSL_ALGORITHM fips_keyexch[] = {
473 #ifndef OPENSSL_NO_DH
474     { "DH:dhKeyAgreement", "provider=fips,fips=yes", dh_keyexch_functions },
475 #endif
476 #ifndef OPENSSL_NO_EC
477     { "ECDH", "provider=fips,fips=yes", ecdh_keyexch_functions },
478     { "X25519", "provider=fips,fips=no", x25519_keyexch_functions },
479     { "X448", "provider=fips,fips=no", x448_keyexch_functions },
480 #endif
481     { NULL, NULL, NULL }
482 };
483
484 static const OSSL_ALGORITHM fips_signature[] = {
485 #ifndef OPENSSL_NO_DSA
486     { "DSA:dsaEncryption", "provider=fips,fips=yes", dsa_signature_functions },
487 #endif
488     { "RSA:rsaEncryption", "provider=fips,fips=yes", rsa_signature_functions },
489 #ifndef OPENSSL_NO_EC
490     { "ED25519", "provider=fips,fips=no", ed25519_signature_functions },
491     { "ED448", "provider=fips,fips=no", ed448_signature_functions },
492     { "ECDSA", "provider=fips,fips=yes", ecdsa_signature_functions },
493 #endif
494     { NULL, NULL, NULL }
495 };
496
497 static const OSSL_ALGORITHM fips_asym_cipher[] = {
498     { "RSA:rsaEncryption", "provider=fips,fips=yes", rsa_asym_cipher_functions },
499     { NULL, NULL, NULL }
500 };
501
502 static const OSSL_ALGORITHM fips_keymgmt[] = {
503 #ifndef OPENSSL_NO_DH
504     { "DH:dhKeyAgreement", "provider=fips,fips=yes", dh_keymgmt_functions },
505 #endif
506 #ifndef OPENSSL_NO_DSA
507     { "DSA", "provider=fips,fips=yes", dsa_keymgmt_functions },
508 #endif
509     { "RSA:rsaEncryption", "provider=fips,fips=yes", rsa_keymgmt_functions },
510     { "RSA-PSS:RSASSA-PSS", "provider=fips,fips=yes",
511       rsapss_keymgmt_functions },
512 #ifndef OPENSSL_NO_EC
513     { "EC:id-ecPublicKey", "provider=fips,fips=yes", ec_keymgmt_functions },
514     { "X25519", "provider=fips,fips=no", x25519_keymgmt_functions },
515     { "X448", "provider=fips,fips=no", x448_keymgmt_functions },
516     { "ED25519", "provider=fips,fips=no", ed25519_keymgmt_functions },
517     { "ED448", "provider=fips,fips=no", ed448_keymgmt_functions },
518 #endif
519     { NULL, NULL, NULL }
520 };
521
522 static const OSSL_ALGORITHM *fips_query(void *provctx, int operation_id,
523                                         int *no_cache)
524 {
525     *no_cache = 0;
526     switch (operation_id) {
527     case OSSL_OP_DIGEST:
528         return fips_digests;
529     case OSSL_OP_CIPHER:
530         ossl_prov_cache_exported_algorithms(fips_ciphers, exported_fips_ciphers);
531         return exported_fips_ciphers;
532     case OSSL_OP_MAC:
533         return fips_macs;
534     case OSSL_OP_KDF:
535         return fips_kdfs;
536     case OSSL_OP_KEYMGMT:
537         return fips_keymgmt;
538     case OSSL_OP_KEYEXCH:
539         return fips_keyexch;
540     case OSSL_OP_SIGNATURE:
541         return fips_signature;
542     case OSSL_OP_ASYM_CIPHER:
543         return fips_asym_cipher;
544     }
545     return NULL;
546 }
547
548 static void fips_teardown(void *provctx)
549 {
550     OPENSSL_CTX_free(PROV_LIBRARY_CONTEXT_OF(provctx));
551     PROV_CTX_free(provctx);
552 }
553
554 static void fips_intern_teardown(void *provctx)
555 {
556     /*
557      * We know that the library context is the same as for the outer provider,
558      * so no need to destroy it here.
559      */
560     PROV_CTX_free(provctx);
561 }
562
563 /* Functions we provide to the core */
564 static const OSSL_DISPATCH fips_dispatch_table[] = {
565     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_teardown },
566     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params },
567     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
568     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
569     { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))provider_get_capabilities },
570     { 0, NULL }
571 };
572
573 /* Functions we provide to ourself */
574 static const OSSL_DISPATCH intern_dispatch_table[] = {
575     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_intern_teardown },
576     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
577     { 0, NULL }
578 };
579
580
581 int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
582                        const OSSL_DISPATCH *in,
583                        const OSSL_DISPATCH **out,
584                        void **provctx)
585 {
586     FIPS_GLOBAL *fgbl;
587     OPENSSL_CTX *libctx = NULL;
588     OSSL_self_test_cb_fn *stcbfn = NULL;
589     OSSL_core_get_library_context_fn *c_get_libctx = NULL;
590
591     for (; in->function_id != 0; in++) {
592         switch (in->function_id) {
593         case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
594             c_get_libctx = OSSL_get_core_get_library_context(in);
595             break;
596         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
597             c_gettable_params = OSSL_get_core_gettable_params(in);
598             break;
599         case OSSL_FUNC_CORE_GET_PARAMS:
600             c_get_params = OSSL_get_core_get_params(in);
601             break;
602         case OSSL_FUNC_CORE_THREAD_START:
603             c_thread_start = OSSL_get_core_thread_start(in);
604             break;
605         case OSSL_FUNC_CORE_NEW_ERROR:
606             c_new_error = OSSL_get_core_new_error(in);
607             break;
608         case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
609             c_set_error_debug = OSSL_get_core_set_error_debug(in);
610             break;
611         case OSSL_FUNC_CORE_VSET_ERROR:
612             c_vset_error = OSSL_get_core_vset_error(in);
613             break;
614         case OSSL_FUNC_CORE_SET_ERROR_MARK:
615             c_set_error_mark = OSSL_get_core_set_error_mark(in);
616             break;
617         case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
618             c_clear_last_error_mark = OSSL_get_core_clear_last_error_mark(in);
619             break;
620         case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
621             c_pop_error_to_mark = OSSL_get_core_pop_error_to_mark(in);
622             break;
623         case OSSL_FUNC_CRYPTO_MALLOC:
624             c_CRYPTO_malloc = OSSL_get_CRYPTO_malloc(in);
625             break;
626         case OSSL_FUNC_CRYPTO_ZALLOC:
627             c_CRYPTO_zalloc = OSSL_get_CRYPTO_zalloc(in);
628             break;
629         case OSSL_FUNC_CRYPTO_FREE:
630             c_CRYPTO_free = OSSL_get_CRYPTO_free(in);
631             break;
632         case OSSL_FUNC_CRYPTO_CLEAR_FREE:
633             c_CRYPTO_clear_free = OSSL_get_CRYPTO_clear_free(in);
634             break;
635         case OSSL_FUNC_CRYPTO_REALLOC:
636             c_CRYPTO_realloc = OSSL_get_CRYPTO_realloc(in);
637             break;
638         case OSSL_FUNC_CRYPTO_CLEAR_REALLOC:
639             c_CRYPTO_clear_realloc = OSSL_get_CRYPTO_clear_realloc(in);
640             break;
641         case OSSL_FUNC_CRYPTO_SECURE_MALLOC:
642             c_CRYPTO_secure_malloc = OSSL_get_CRYPTO_secure_malloc(in);
643             break;
644         case OSSL_FUNC_CRYPTO_SECURE_ZALLOC:
645             c_CRYPTO_secure_zalloc = OSSL_get_CRYPTO_secure_zalloc(in);
646             break;
647         case OSSL_FUNC_CRYPTO_SECURE_FREE:
648             c_CRYPTO_secure_free = OSSL_get_CRYPTO_secure_free(in);
649             break;
650         case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE:
651             c_CRYPTO_secure_clear_free = OSSL_get_CRYPTO_secure_clear_free(in);
652             break;
653         case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
654             c_CRYPTO_secure_allocated = OSSL_get_CRYPTO_secure_allocated(in);
655             break;
656         case OSSL_FUNC_BIO_NEW_FILE:
657             selftest_params.bio_new_file_cb = OSSL_get_BIO_new_file(in);
658             break;
659         case OSSL_FUNC_BIO_NEW_MEMBUF:
660             selftest_params.bio_new_buffer_cb = OSSL_get_BIO_new_membuf(in);
661             break;
662         case OSSL_FUNC_BIO_READ_EX:
663             selftest_params.bio_read_ex_cb = OSSL_get_BIO_read_ex(in);
664             break;
665         case OSSL_FUNC_BIO_FREE:
666             selftest_params.bio_free_cb = OSSL_get_BIO_free(in);
667             break;
668         case OSSL_FUNC_BIO_VSNPRINTF:
669             c_BIO_vsnprintf = OSSL_get_BIO_vsnprintf(in);
670             break;
671         case OSSL_FUNC_SELF_TEST_CB: {
672             stcbfn = OSSL_get_self_test_cb(in);
673             break;
674         }
675         default:
676             /* Just ignore anything we don't understand */
677             break;
678         }
679     }
680
681     if (stcbfn != NULL && c_get_libctx != NULL) {
682         stcbfn(c_get_libctx(handle), &selftest_params.cb,
683                &selftest_params.cb_arg);
684     }
685     else {
686         selftest_params.cb = NULL;
687         selftest_params.cb_arg = NULL;
688     }
689
690     if (!c_get_params(handle, core_params))
691         return 0;
692
693     /*  Create a context. */
694     if ((*provctx = PROV_CTX_new()) == NULL
695         || (libctx = OPENSSL_CTX_new()) == NULL) {
696         /*
697          * We free libctx separately here and only here because it hasn't
698          * been attached to *provctx.  All other error paths below rely
699          * solely on fips_teardown.
700          */
701         OPENSSL_CTX_free(libctx);
702         goto err;
703     }
704     PROV_CTX_set0_library_context(*provctx, libctx);
705     PROV_CTX_set0_handle(*provctx, handle);
706
707     if ((fgbl = openssl_ctx_get_data(libctx, OPENSSL_CTX_FIPS_PROV_INDEX,
708                                      &fips_prov_ossl_ctx_method)) == NULL)
709         goto err;
710
711     fgbl->handle = handle;
712
713     selftest_params.libctx = libctx;
714     if (!SELF_TEST_post(&selftest_params, 0))
715         goto err;
716
717     /*
718      * TODO(3.0): Remove me. This is just a dummy call to demonstrate making
719      * EVP calls from within the FIPS module.
720      */
721     if (!dummy_evp_call(libctx))
722         goto err;
723
724     *out = fips_dispatch_table;
725
726     return 1;
727  err:
728     fips_teardown(*provctx);
729     *provctx = NULL;
730     return 0;
731 }
732
733 /*
734  * The internal init function used when the FIPS module uses EVP to call
735  * another algorithm also in the FIPS module. This is a recursive call that has
736  * been made from within the FIPS module itself. To make this work, we populate
737  * the provider context of this inner instance with the same library context
738  * that was used in the EVP call that initiated this recursive call.
739  */
740 OSSL_provider_init_fn fips_intern_provider_init;
741 int fips_intern_provider_init(const OSSL_CORE_HANDLE *handle,
742                               const OSSL_DISPATCH *in,
743                               const OSSL_DISPATCH **out,
744                               void **provctx)
745 {
746     OSSL_core_get_library_context_fn *c_get_libctx = NULL;
747
748     for (; in->function_id != 0; in++) {
749         switch (in->function_id) {
750         case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
751             c_get_libctx = OSSL_get_core_get_library_context(in);
752             break;
753         default:
754             break;
755         }
756     }
757
758     if (c_get_libctx == NULL)
759         return 0;
760
761     if ((*provctx = PROV_CTX_new()) == NULL)
762         return 0;
763     /*
764      * Using the parent library context only works because we are a built-in
765      * internal provider. This is not something that most providers would be
766      * able to do.
767      */
768     PROV_CTX_set0_library_context(*provctx, (OPENSSL_CTX *)c_get_libctx(handle));
769     PROV_CTX_set0_handle(*provctx, handle);
770
771     *out = intern_dispatch_table;
772     return 1;
773 }
774
775 void ERR_new(void)
776 {
777     c_new_error(NULL);
778 }
779
780 void ERR_set_debug(const char *file, int line, const char *func)
781 {
782     c_set_error_debug(NULL, file, line, func);
783 }
784
785 void ERR_set_error(int lib, int reason, const char *fmt, ...)
786 {
787     va_list args;
788
789     va_start(args, fmt);
790     c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
791     va_end(args);
792 }
793
794 void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
795 {
796     c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
797 }
798
799 int ERR_set_mark(void)
800 {
801     return c_set_error_mark(NULL);
802 }
803
804 int ERR_clear_last_mark(void)
805 {
806     return c_clear_last_error_mark(NULL);
807 }
808
809 int ERR_pop_to_mark(void)
810 {
811     return c_pop_error_to_mark(NULL);
812 }
813
814 /*
815  * This must take a library context, since it's called from the depths
816  * of crypto/initthread.c code, where it's (correctly) assumed that the
817  * passed caller argument is an OPENSSL_CTX pointer (since the same routine
818  * is also called from other parts of libcrypto, which all pass around a
819  * OPENSSL_CTX pointer)
820  */
821 const OSSL_CORE_HANDLE *FIPS_get_core_handle(OPENSSL_CTX *libctx)
822 {
823     FIPS_GLOBAL *fgbl = openssl_ctx_get_data(libctx,
824                                              OPENSSL_CTX_FIPS_PROV_INDEX,
825                                              &fips_prov_ossl_ctx_method);
826
827     if (fgbl == NULL)
828         return NULL;
829
830     return fgbl->handle;
831 }
832
833 void *CRYPTO_malloc(size_t num, const char *file, int line)
834 {
835     return c_CRYPTO_malloc(num, file, line);
836 }
837
838 void *CRYPTO_zalloc(size_t num, const char *file, int line)
839 {
840     return c_CRYPTO_zalloc(num, file, line);
841 }
842
843 void CRYPTO_free(void *ptr, const char *file, int line)
844 {
845     c_CRYPTO_free(ptr, file, line);
846 }
847
848 void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line)
849 {
850     c_CRYPTO_clear_free(ptr, num, file, line);
851 }
852
853 void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
854 {
855     return c_CRYPTO_realloc(addr, num, file, line);
856 }
857
858 void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
859                            const char *file, int line)
860 {
861     return c_CRYPTO_clear_realloc(addr, old_num, num, file, line);
862 }
863
864 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
865 {
866     return c_CRYPTO_secure_malloc(num, file, line);
867 }
868
869 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
870 {
871     return c_CRYPTO_secure_zalloc(num, file, line);
872 }
873
874 void CRYPTO_secure_free(void *ptr, const char *file, int line)
875 {
876     c_CRYPTO_secure_free(ptr, file, line);
877 }
878
879 void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *file, int line)
880 {
881     c_CRYPTO_secure_clear_free(ptr, num, file, line);
882 }
883
884 int CRYPTO_secure_allocated(const void *ptr)
885 {
886     return c_CRYPTO_secure_allocated(ptr);
887 }
888
889 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
890 {
891     va_list args;
892     int ret;
893
894     va_start(args, format);
895     ret = c_BIO_vsnprintf(buf, n, format, args);
896     va_end(args);
897     return ret;
898 }