Add ossl_provider symbols
[openssl.git] / providers / fips / fipsprov.c
1 /*
2  * Copyright 2019-2021 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 <openssl/core_dispatch.h>
11 #include <openssl/core_names.h>
12 #include <openssl/params.h>
13 #include <openssl/fips_names.h>
14 #include <openssl/rand.h> /* RAND_get0_public() */
15 #include <openssl/proverr.h>
16 #include "internal/cryptlib.h"
17 #include "prov/implementations.h"
18 #include "prov/provider_ctx.h"
19 #include "prov/providercommon.h"
20 #include "prov/provider_util.h"
21 #include "prov/seeding.h"
22 #include "self_test.h"
23
24 static const char FIPS_DEFAULT_PROPERTIES[] = "provider=fips,fips=yes";
25 static const char FIPS_UNAPPROVED_PROPERTIES[] = "provider=fips,fips=no";
26
27 /*
28  * Forward declarations to ensure that interface functions are correctly
29  * defined.
30  */
31 static OSSL_FUNC_provider_teardown_fn fips_teardown;
32 static OSSL_FUNC_provider_gettable_params_fn fips_gettable_params;
33 static OSSL_FUNC_provider_get_params_fn fips_get_params;
34 static OSSL_FUNC_provider_query_operation_fn fips_query;
35
36 #define ALGC(NAMES, FUNC, CHECK) { { NAMES, FIPS_DEFAULT_PROPERTIES, FUNC }, CHECK }
37 #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
38
39 extern OSSL_FUNC_core_thread_start_fn *c_thread_start;
40 int FIPS_security_check_enabled(void);
41
42 /*
43  * TODO(3.0): Should these be stored in the provider side provctx? Could they
44  * ever be different from one init to the next? Unfortunately we can't do this
45  * at the moment because c_put_error/c_add_error_vdata do not provide
46  * us with the OSSL_LIB_CTX as a parameter.
47  */
48
49 static SELF_TEST_POST_PARAMS selftest_params;
50 static int fips_security_checks = 1;
51 static const char *fips_security_check_option = "1";
52
53 /* Functions provided by the core */
54 static OSSL_FUNC_core_gettable_params_fn *c_gettable_params;
55 static OSSL_FUNC_core_get_params_fn *c_get_params;
56 OSSL_FUNC_core_thread_start_fn *c_thread_start;
57 static OSSL_FUNC_core_new_error_fn *c_new_error;
58 static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
59 static OSSL_FUNC_core_vset_error_fn *c_vset_error;
60 static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;
61 static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;
62 static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;
63 static OSSL_FUNC_CRYPTO_malloc_fn *c_CRYPTO_malloc;
64 static OSSL_FUNC_CRYPTO_zalloc_fn *c_CRYPTO_zalloc;
65 static OSSL_FUNC_CRYPTO_free_fn *c_CRYPTO_free;
66 static OSSL_FUNC_CRYPTO_clear_free_fn *c_CRYPTO_clear_free;
67 static OSSL_FUNC_CRYPTO_realloc_fn *c_CRYPTO_realloc;
68 static OSSL_FUNC_CRYPTO_clear_realloc_fn *c_CRYPTO_clear_realloc;
69 static OSSL_FUNC_CRYPTO_secure_malloc_fn *c_CRYPTO_secure_malloc;
70 static OSSL_FUNC_CRYPTO_secure_zalloc_fn *c_CRYPTO_secure_zalloc;
71 static OSSL_FUNC_CRYPTO_secure_free_fn *c_CRYPTO_secure_free;
72 static OSSL_FUNC_CRYPTO_secure_clear_free_fn *c_CRYPTO_secure_clear_free;
73 static OSSL_FUNC_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated;
74 static OSSL_FUNC_BIO_vsnprintf_fn *c_BIO_vsnprintf;
75 static OSSL_FUNC_self_test_cb_fn *c_stcbfn = NULL;
76 static OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
77
78 typedef struct fips_global_st {
79     const OSSL_CORE_HANDLE *handle;
80 } FIPS_GLOBAL;
81
82 static void *fips_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
83 {
84     FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
85
86     return fgbl;
87 }
88
89 static void fips_prov_ossl_ctx_free(void *fgbl)
90 {
91     OPENSSL_free(fgbl);
92 }
93
94 static const OSSL_LIB_CTX_METHOD fips_prov_ossl_ctx_method = {
95     fips_prov_ossl_ctx_new,
96     fips_prov_ossl_ctx_free,
97 };
98
99
100 /* Parameters we provide to the core */
101 static const OSSL_PARAM fips_param_types[] = {
102     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
103     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
104     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
105     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
106     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_SECURITY_CHECKS, OSSL_PARAM_INTEGER, NULL, 0),
107     OSSL_PARAM_END
108 };
109
110 /*
111  * Parameters to retrieve from the core provider - required for self testing.
112  * NOTE: inside core_get_params() these will be loaded from config items
113  * stored inside prov->parameters (except for
114  * OSSL_PROV_PARAM_CORE_MODULE_FILENAME).
115  * OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS is not a self test parameter.
116  */
117 static OSSL_PARAM core_params[] =
118 {
119     OSSL_PARAM_utf8_ptr(OSSL_PROV_PARAM_CORE_MODULE_FILENAME,
120                         &selftest_params.module_filename,
121                         sizeof(selftest_params.module_filename)),
122     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_MODULE_MAC,
123                         &selftest_params.module_checksum_data,
124                         sizeof(selftest_params.module_checksum_data)),
125     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_MAC,
126                         &selftest_params.indicator_checksum_data,
127                         sizeof(selftest_params.indicator_checksum_data)),
128     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
129                         &selftest_params.indicator_data,
130                         sizeof(selftest_params.indicator_data)),
131     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
132                         &selftest_params.indicator_version,
133                         sizeof(selftest_params.indicator_version)),
134     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
135                         &selftest_params.conditional_error_check,
136                         sizeof(selftest_params.conditional_error_check)),
137     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
138                         &fips_security_check_option,
139                         sizeof(fips_security_check_option)),
140     OSSL_PARAM_END
141 };
142
143 static const OSSL_PARAM *fips_gettable_params(void *provctx)
144 {
145     return fips_param_types;
146 }
147
148 static int fips_get_params(void *provctx, OSSL_PARAM params[])
149 {
150     OSSL_PARAM *p;
151
152     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
153     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
154         return 0;
155     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
156     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
157         return 0;
158     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
159     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
160         return 0;
161     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
162     if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
163         return 0;
164     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_SECURITY_CHECKS);
165     if (p != NULL && !OSSL_PARAM_set_int(p, fips_security_checks))
166         return 0;
167     return 1;
168 }
169
170 static void set_self_test_cb(const OSSL_CORE_HANDLE *handle)
171 {
172     if (c_stcbfn != NULL && c_get_libctx != NULL) {
173         c_stcbfn(c_get_libctx(handle), &selftest_params.cb,
174                               &selftest_params.cb_arg);
175     } else {
176         selftest_params.cb = NULL;
177         selftest_params.cb_arg = NULL;
178     }
179 }
180
181 static int fips_self_test(void *provctx)
182 {
183     set_self_test_cb(FIPS_get_core_handle(selftest_params.libctx));
184     return SELF_TEST_post(&selftest_params, 1) ? 1 : 0;
185 }
186
187 /*
188  * For the algorithm names, we use the following formula for our primary
189  * names:
190  *
191  *     ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
192  *
193  *     VERSION is only present if there are multiple versions of
194  *     an alg (MD2, MD4, MD5).  It may be omitted if there is only
195  *     one version (if a subsequent version is released in the future,
196  *     we can always change the canonical name, and add the old name
197  *     as an alias).
198  *
199  *     SUBNAME may be present where we are combining multiple
200  *     algorithms together, e.g. MD5-SHA1.
201  *
202  *     SIZE is only present if multiple versions of an algorithm exist
203  *     with different sizes (e.g. AES-128-CBC, AES-256-CBC)
204  *
205  *     MODE is only present where applicable.
206  *
207  * We add diverse other names where applicable, such as the names that
208  * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
209  * we have used historically.
210  */
211 static const OSSL_ALGORITHM fips_digests[] = {
212     /* Our primary name:NiST name[:our older names] */
213     { "SHA1:SHA-1:SSL3-SHA1", FIPS_DEFAULT_PROPERTIES, ossl_sha1_functions },
214     { "SHA2-224:SHA-224:SHA224", FIPS_DEFAULT_PROPERTIES,
215       ossl_sha224_functions },
216     { "SHA2-256:SHA-256:SHA256", FIPS_DEFAULT_PROPERTIES,
217       ossl_sha256_functions },
218     { "SHA2-384:SHA-384:SHA384", FIPS_DEFAULT_PROPERTIES,
219       ossl_sha384_functions },
220     { "SHA2-512:SHA-512:SHA512", FIPS_DEFAULT_PROPERTIES,
221       ossl_sha512_functions },
222     { "SHA2-512/224:SHA-512/224:SHA512-224", FIPS_DEFAULT_PROPERTIES,
223       ossl_sha512_224_functions },
224     { "SHA2-512/256:SHA-512/256:SHA512-256", FIPS_DEFAULT_PROPERTIES,
225       ossl_sha512_256_functions },
226
227     /* We agree with NIST here, so one name only */
228     { "SHA3-224", FIPS_DEFAULT_PROPERTIES, ossl_sha3_224_functions },
229     { "SHA3-256", FIPS_DEFAULT_PROPERTIES, ossl_sha3_256_functions },
230     { "SHA3-384", FIPS_DEFAULT_PROPERTIES, ossl_sha3_384_functions },
231     { "SHA3-512", FIPS_DEFAULT_PROPERTIES, ossl_sha3_512_functions },
232
233     { "SHAKE-128:SHAKE128", FIPS_DEFAULT_PROPERTIES, ossl_shake_128_functions },
234     { "SHAKE-256:SHAKE256", FIPS_DEFAULT_PROPERTIES, ossl_shake_256_functions },
235
236     /*
237      * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
238      * KMAC128 and KMAC256.
239      */
240     { "KECCAK-KMAC-128:KECCAK-KMAC128", FIPS_DEFAULT_PROPERTIES,
241       ossl_keccak_kmac_128_functions },
242     { "KECCAK-KMAC-256:KECCAK-KMAC256", FIPS_DEFAULT_PROPERTIES,
243       ossl_keccak_kmac_256_functions },
244     { NULL, NULL, NULL }
245 };
246
247 static const OSSL_ALGORITHM_CAPABLE fips_ciphers[] = {
248     /* Our primary name[:ASN.1 OID name][:our older names] */
249     ALG("AES-256-ECB", ossl_aes256ecb_functions),
250     ALG("AES-192-ECB", ossl_aes192ecb_functions),
251     ALG("AES-128-ECB", ossl_aes128ecb_functions),
252     ALG("AES-256-CBC:AES256", ossl_aes256cbc_functions),
253     ALG("AES-192-CBC:AES192", ossl_aes192cbc_functions),
254     ALG("AES-128-CBC:AES128", ossl_aes128cbc_functions),
255     ALG("AES-256-CBC-CTS", ossl_aes256cbc_cts_functions),
256     ALG("AES-192-CBC-CTS", ossl_aes192cbc_cts_functions),
257     ALG("AES-128-CBC-CTS", ossl_aes128cbc_cts_functions),
258     ALG("AES-256-OFB", ossl_aes256ofb_functions),
259     ALG("AES-192-OFB", ossl_aes192ofb_functions),
260     ALG("AES-128-OFB", ossl_aes128ofb_functions),
261     ALG("AES-256-CFB", ossl_aes256cfb_functions),
262     ALG("AES-192-CFB", ossl_aes192cfb_functions),
263     ALG("AES-128-CFB", ossl_aes128cfb_functions),
264     ALG("AES-256-CFB1", ossl_aes256cfb1_functions),
265     ALG("AES-192-CFB1", ossl_aes192cfb1_functions),
266     ALG("AES-128-CFB1", ossl_aes128cfb1_functions),
267     ALG("AES-256-CFB8", ossl_aes256cfb8_functions),
268     ALG("AES-192-CFB8", ossl_aes192cfb8_functions),
269     ALG("AES-128-CFB8", ossl_aes128cfb8_functions),
270     ALG("AES-256-CTR", ossl_aes256ctr_functions),
271     ALG("AES-192-CTR", ossl_aes192ctr_functions),
272     ALG("AES-128-CTR", ossl_aes128ctr_functions),
273     ALG("AES-256-XTS", ossl_aes256xts_functions),
274     ALG("AES-128-XTS", ossl_aes128xts_functions),
275     ALG("AES-256-GCM:id-aes256-GCM", ossl_aes256gcm_functions),
276     ALG("AES-192-GCM:id-aes192-GCM", ossl_aes192gcm_functions),
277     ALG("AES-128-GCM:id-aes128-GCM", ossl_aes128gcm_functions),
278     ALG("AES-256-CCM:id-aes256-CCM", ossl_aes256ccm_functions),
279     ALG("AES-192-CCM:id-aes192-CCM", ossl_aes192ccm_functions),
280     ALG("AES-128-CCM:id-aes128-CCM", ossl_aes128ccm_functions),
281     ALG("AES-256-WRAP:id-aes256-wrap:AES256-WRAP", ossl_aes256wrap_functions),
282     ALG("AES-192-WRAP:id-aes192-wrap:AES192-WRAP", ossl_aes192wrap_functions),
283     ALG("AES-128-WRAP:id-aes128-wrap:AES128-WRAP", ossl_aes128wrap_functions),
284     ALG("AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD",
285         ossl_aes256wrappad_functions),
286     ALG("AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD",
287         ossl_aes192wrappad_functions),
288     ALG("AES-128-WRAP-PAD:id-aes128-wrap-pad:AES128-WRAP-PAD",
289         ossl_aes128wrappad_functions),
290     ALG("AES-256-WRAP-INV:AES256-WRAP-INV", ossl_aes256wrapinv_functions),
291     ALG("AES-192-WRAP-INV:AES192-WRAP-INV", ossl_aes192wrapinv_functions),
292     ALG("AES-128-WRAP-INV:AES128-WRAP-INV", ossl_aes128wrapinv_functions),
293     ALG("AES-256-WRAP-PAD-INV:AES256-WRAP-PAD-INV",
294         ossl_aes256wrappadinv_functions),
295     ALG("AES-192-WRAP-PAD-INV:AES192-WRAP-PAD-INV",
296         ossl_aes192wrappadinv_functions),
297     ALG("AES-128-WRAP-PAD-INV:AES128-WRAP-PAD-INV",
298         ossl_aes128wrappadinv_functions),
299     ALGC("AES-128-CBC-HMAC-SHA1", ossl_aes128cbc_hmac_sha1_functions,
300          ossl_cipher_capable_aes_cbc_hmac_sha1),
301     ALGC("AES-256-CBC-HMAC-SHA1", ossl_aes256cbc_hmac_sha1_functions,
302          ossl_cipher_capable_aes_cbc_hmac_sha1),
303     ALGC("AES-128-CBC-HMAC-SHA256", ossl_aes128cbc_hmac_sha256_functions,
304          ossl_cipher_capable_aes_cbc_hmac_sha256),
305     ALGC("AES-256-CBC-HMAC-SHA256", ossl_aes256cbc_hmac_sha256_functions,
306          ossl_cipher_capable_aes_cbc_hmac_sha256),
307 #ifndef OPENSSL_NO_DES
308     ALG("DES-EDE3-ECB:DES-EDE3", ossl_tdes_ede3_ecb_functions),
309     ALG("DES-EDE3-CBC:DES3", ossl_tdes_ede3_cbc_functions),
310 #endif  /* OPENSSL_NO_DES */
311     { { NULL, NULL, NULL }, NULL }
312 };
313 static OSSL_ALGORITHM exported_fips_ciphers[OSSL_NELEM(fips_ciphers)];
314
315 static const OSSL_ALGORITHM fips_macs[] = {
316 #ifndef OPENSSL_NO_CMAC
317     { "CMAC", FIPS_DEFAULT_PROPERTIES, ossl_cmac_functions },
318 #endif
319     { "GMAC", FIPS_DEFAULT_PROPERTIES, ossl_gmac_functions },
320     { "HMAC", FIPS_DEFAULT_PROPERTIES, ossl_hmac_functions },
321     { "KMAC-128:KMAC128", FIPS_DEFAULT_PROPERTIES, ossl_kmac128_functions },
322     { "KMAC-256:KMAC256", FIPS_DEFAULT_PROPERTIES, ossl_kmac256_functions },
323     { NULL, NULL, NULL }
324 };
325
326 static const OSSL_ALGORITHM fips_kdfs[] = {
327     { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_functions },
328     { "SSKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_sskdf_functions },
329     { "PBKDF2", FIPS_DEFAULT_PROPERTIES, ossl_kdf_pbkdf2_functions },
330     { "SSHKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_sshkdf_functions },
331     { "X963KDF:X942KDF-CONCAT", FIPS_DEFAULT_PROPERTIES,
332       ossl_kdf_x963_kdf_functions },
333     { "X942KDF-ASN1:X942KDF", FIPS_DEFAULT_PROPERTIES,
334       ossl_kdf_x942_kdf_functions },
335     { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_tls1_prf_functions },
336     { "KBKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_kbkdf_functions },
337     { NULL, NULL, NULL }
338 };
339
340 static const OSSL_ALGORITHM fips_rands[] = {
341     { "CTR-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_ctr_functions },
342     { "HASH-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_hash_functions },
343     { "HMAC-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_ossl_hmac_functions },
344     { "TEST-RAND", FIPS_UNAPPROVED_PROPERTIES, ossl_test_rng_functions },
345     { NULL, NULL, NULL }
346 };
347
348 static const OSSL_ALGORITHM fips_keyexch[] = {
349 #ifndef OPENSSL_NO_DH
350     { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES, ossl_dh_keyexch_functions },
351 #endif
352 #ifndef OPENSSL_NO_EC
353     { "ECDH", FIPS_DEFAULT_PROPERTIES, ossl_ecdh_keyexch_functions },
354     { "X25519", FIPS_DEFAULT_PROPERTIES, ossl_x25519_keyexch_functions },
355     { "X448", FIPS_DEFAULT_PROPERTIES, ossl_x448_keyexch_functions },
356 #endif
357     { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES,
358       ossl_kdf_tls1_prf_keyexch_functions },
359     { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_keyexch_functions },
360     { NULL, NULL, NULL }
361 };
362
363 static const OSSL_ALGORITHM fips_signature[] = {
364 #ifndef OPENSSL_NO_DSA
365     { "DSA:dsaEncryption", FIPS_DEFAULT_PROPERTIES,
366       ossl_dsa_signature_functions },
367 #endif
368     { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
369       ossl_rsa_signature_functions },
370 #ifndef OPENSSL_NO_EC
371     { "ED25519", FIPS_DEFAULT_PROPERTIES, ossl_ed25519_signature_functions },
372     { "ED448", FIPS_DEFAULT_PROPERTIES, ossl_ed448_signature_functions },
373     { "ECDSA", FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_signature_functions },
374 #endif
375     { "HMAC", FIPS_DEFAULT_PROPERTIES,
376       ossl_mac_legacy_hmac_signature_functions },
377 #ifndef OPENSSL_NO_CMAC
378     { "CMAC", FIPS_DEFAULT_PROPERTIES,
379       ossl_mac_legacy_cmac_signature_functions },
380 #endif
381     { NULL, NULL, NULL }
382 };
383
384 static const OSSL_ALGORITHM fips_asym_cipher[] = {
385     { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
386       ossl_rsa_asym_cipher_functions },
387     { NULL, NULL, NULL }
388 };
389
390 static const OSSL_ALGORITHM fips_asym_kem[] = {
391     { "RSA", FIPS_DEFAULT_PROPERTIES, ossl_rsa_asym_kem_functions },
392     { NULL, NULL, NULL }
393 };
394
395 static const OSSL_ALGORITHM fips_keymgmt[] = {
396 #ifndef OPENSSL_NO_DH
397     { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES, ossl_dh_keymgmt_functions },
398     { "DHX:X9.42 DH:dhpublicnumber", FIPS_DEFAULT_PROPERTIES,
399       ossl_dhx_keymgmt_functions },
400 #endif
401 #ifndef OPENSSL_NO_DSA
402     { "DSA", FIPS_DEFAULT_PROPERTIES, ossl_dsa_keymgmt_functions },
403 #endif
404     { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
405       ossl_rsa_keymgmt_functions },
406     { "RSA-PSS:RSASSA-PSS", FIPS_DEFAULT_PROPERTIES,
407       ossl_rsapss_keymgmt_functions },
408 #ifndef OPENSSL_NO_EC
409     { "EC:id-ecPublicKey", FIPS_DEFAULT_PROPERTIES, ossl_ec_keymgmt_functions },
410     { "X25519", FIPS_DEFAULT_PROPERTIES, ossl_x25519_keymgmt_functions },
411     { "X448", FIPS_DEFAULT_PROPERTIES, ossl_x448_keymgmt_functions },
412     { "ED25519", FIPS_DEFAULT_PROPERTIES, ossl_ed25519_keymgmt_functions },
413     { "ED448", FIPS_DEFAULT_PROPERTIES, ossl_ed448_keymgmt_functions },
414 #endif
415     { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions },
416     { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions },
417     { "HMAC", FIPS_DEFAULT_PROPERTIES, ossl_mac_legacy_keymgmt_functions },
418 #ifndef OPENSSL_NO_CMAC
419     { "CMAC", FIPS_DEFAULT_PROPERTIES,
420       ossl_cossl_mac_legacy_keymgmt_functions },
421 #endif
422     { NULL, NULL, NULL }
423 };
424
425 static const OSSL_ALGORITHM *fips_query(void *provctx, int operation_id,
426                                         int *no_cache)
427 {
428     *no_cache = 0;
429
430     if (!ossl_prov_is_running())
431         return NULL;
432
433     switch (operation_id) {
434     case OSSL_OP_DIGEST:
435         return fips_digests;
436     case OSSL_OP_CIPHER:
437         return exported_fips_ciphers;
438     case OSSL_OP_MAC:
439         return fips_macs;
440     case OSSL_OP_KDF:
441         return fips_kdfs;
442     case OSSL_OP_RAND:
443         return fips_rands;
444     case OSSL_OP_KEYMGMT:
445         return fips_keymgmt;
446     case OSSL_OP_KEYEXCH:
447         return fips_keyexch;
448     case OSSL_OP_SIGNATURE:
449         return fips_signature;
450     case OSSL_OP_ASYM_CIPHER:
451         return fips_asym_cipher;
452     case OSSL_OP_KEM:
453         return fips_asym_kem;
454     }
455     return NULL;
456 }
457
458 static void fips_teardown(void *provctx)
459 {
460     OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
461     ossl_prov_ctx_free(provctx);
462 }
463
464 static void fips_intern_teardown(void *provctx)
465 {
466     /*
467      * We know that the library context is the same as for the outer provider,
468      * so no need to destroy it here.
469      */
470     ossl_prov_ctx_free(provctx);
471 }
472
473 /* Functions we provide to the core */
474 static const OSSL_DISPATCH fips_dispatch_table[] = {
475     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_teardown },
476     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params },
477     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
478     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
479     { OSSL_FUNC_PROVIDER_GET_CAPABILITIES,
480       (void (*)(void))ossl_prov_get_capabilities },
481     { OSSL_FUNC_PROVIDER_SELF_TEST, (void (*)(void))fips_self_test },
482     { 0, NULL }
483 };
484
485 /* Functions we provide to ourself */
486 static const OSSL_DISPATCH intern_dispatch_table[] = {
487     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_intern_teardown },
488     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
489     { 0, NULL }
490 };
491
492 int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
493                        const OSSL_DISPATCH *in,
494                        const OSSL_DISPATCH **out,
495                        void **provctx)
496 {
497     FIPS_GLOBAL *fgbl;
498     OSSL_LIB_CTX *libctx = NULL;
499
500     if (!ossl_prov_seeding_from_dispatch(in))
501         return 0;
502     for (; in->function_id != 0; in++) {
503         switch (in->function_id) {
504         case OSSL_FUNC_CORE_GET_LIBCTX:
505             c_get_libctx = OSSL_FUNC_core_get_libctx(in);
506             break;
507         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
508             c_gettable_params = OSSL_FUNC_core_gettable_params(in);
509             break;
510         case OSSL_FUNC_CORE_GET_PARAMS:
511             c_get_params = OSSL_FUNC_core_get_params(in);
512             break;
513         case OSSL_FUNC_CORE_THREAD_START:
514             c_thread_start = OSSL_FUNC_core_thread_start(in);
515             break;
516         case OSSL_FUNC_CORE_NEW_ERROR:
517             c_new_error = OSSL_FUNC_core_new_error(in);
518             break;
519         case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
520             c_set_error_debug = OSSL_FUNC_core_set_error_debug(in);
521             break;
522         case OSSL_FUNC_CORE_VSET_ERROR:
523             c_vset_error = OSSL_FUNC_core_vset_error(in);
524             break;
525         case OSSL_FUNC_CORE_SET_ERROR_MARK:
526             c_set_error_mark = OSSL_FUNC_core_set_error_mark(in);
527             break;
528         case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
529             c_clear_last_error_mark = OSSL_FUNC_core_clear_last_error_mark(in);
530             break;
531         case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
532             c_pop_error_to_mark = OSSL_FUNC_core_pop_error_to_mark(in);
533             break;
534         case OSSL_FUNC_CRYPTO_MALLOC:
535             c_CRYPTO_malloc = OSSL_FUNC_CRYPTO_malloc(in);
536             break;
537         case OSSL_FUNC_CRYPTO_ZALLOC:
538             c_CRYPTO_zalloc = OSSL_FUNC_CRYPTO_zalloc(in);
539             break;
540         case OSSL_FUNC_CRYPTO_FREE:
541             c_CRYPTO_free = OSSL_FUNC_CRYPTO_free(in);
542             break;
543         case OSSL_FUNC_CRYPTO_CLEAR_FREE:
544             c_CRYPTO_clear_free = OSSL_FUNC_CRYPTO_clear_free(in);
545             break;
546         case OSSL_FUNC_CRYPTO_REALLOC:
547             c_CRYPTO_realloc = OSSL_FUNC_CRYPTO_realloc(in);
548             break;
549         case OSSL_FUNC_CRYPTO_CLEAR_REALLOC:
550             c_CRYPTO_clear_realloc = OSSL_FUNC_CRYPTO_clear_realloc(in);
551             break;
552         case OSSL_FUNC_CRYPTO_SECURE_MALLOC:
553             c_CRYPTO_secure_malloc = OSSL_FUNC_CRYPTO_secure_malloc(in);
554             break;
555         case OSSL_FUNC_CRYPTO_SECURE_ZALLOC:
556             c_CRYPTO_secure_zalloc = OSSL_FUNC_CRYPTO_secure_zalloc(in);
557             break;
558         case OSSL_FUNC_CRYPTO_SECURE_FREE:
559             c_CRYPTO_secure_free = OSSL_FUNC_CRYPTO_secure_free(in);
560             break;
561         case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE:
562             c_CRYPTO_secure_clear_free = OSSL_FUNC_CRYPTO_secure_clear_free(in);
563             break;
564         case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
565             c_CRYPTO_secure_allocated = OSSL_FUNC_CRYPTO_secure_allocated(in);
566             break;
567         case OSSL_FUNC_BIO_NEW_FILE:
568             selftest_params.bio_new_file_cb = OSSL_FUNC_BIO_new_file(in);
569             break;
570         case OSSL_FUNC_BIO_NEW_MEMBUF:
571             selftest_params.bio_new_buffer_cb = OSSL_FUNC_BIO_new_membuf(in);
572             break;
573         case OSSL_FUNC_BIO_READ_EX:
574             selftest_params.bio_read_ex_cb = OSSL_FUNC_BIO_read_ex(in);
575             break;
576         case OSSL_FUNC_BIO_FREE:
577             selftest_params.bio_free_cb = OSSL_FUNC_BIO_free(in);
578             break;
579         case OSSL_FUNC_BIO_VSNPRINTF:
580             c_BIO_vsnprintf = OSSL_FUNC_BIO_vsnprintf(in);
581             break;
582         case OSSL_FUNC_SELF_TEST_CB:
583             c_stcbfn = OSSL_FUNC_self_test_cb(in);
584             break;
585         default:
586             /* Just ignore anything we don't understand */
587             break;
588         }
589     }
590
591     set_self_test_cb(handle);
592
593     if (!c_get_params(handle, core_params)) {
594         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
595         return 0;
596     }
597     /* Disable the conditional error check if is disabled in the fips config file*/
598     if (selftest_params.conditional_error_check != NULL
599         && strcmp(selftest_params.conditional_error_check, "0") == 0)
600         SELF_TEST_disable_conditional_error_state();
601
602     /* Disable the security check if is disabled in the fips config file*/
603     if (fips_security_check_option != NULL
604         && strcmp(fips_security_check_option, "0") == 0)
605         fips_security_checks = 0;
606
607     /*  Create a context. */
608     if ((*provctx = ossl_prov_ctx_new()) == NULL
609         || (libctx = OSSL_LIB_CTX_new()) == NULL) {
610         /*
611          * We free libctx separately here and only here because it hasn't
612          * been attached to *provctx.  All other error paths below rely
613          * solely on fips_teardown.
614          */
615         OSSL_LIB_CTX_free(libctx);
616         goto err;
617     }
618     ossl_prov_ctx_set0_libctx(*provctx, libctx);
619     ossl_prov_ctx_set0_handle(*provctx, handle);
620
621     if ((fgbl = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_FIPS_PROV_INDEX,
622                                       &fips_prov_ossl_ctx_method)) == NULL)
623         goto err;
624
625     fgbl->handle = handle;
626
627     ossl_prov_cache_exported_algorithms(fips_ciphers, exported_fips_ciphers);
628
629     selftest_params.libctx = libctx;
630     if (!SELF_TEST_post(&selftest_params, 0)) {
631         ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_POST_FAILURE);
632         goto err;
633     }
634
635     *out = fips_dispatch_table;
636     return 1;
637  err:
638     fips_teardown(*provctx);
639     *provctx = NULL;
640     return 0;
641 }
642
643 /*
644  * The internal init function used when the FIPS module uses EVP to call
645  * another algorithm also in the FIPS module. This is a recursive call that has
646  * been made from within the FIPS module itself. To make this work, we populate
647  * the provider context of this inner instance with the same library context
648  * that was used in the EVP call that initiated this recursive call.
649  */
650 OSSL_provider_init_fn ossl_fips_intern_provider_init;
651 int ossl_fips_intern_provider_init(const OSSL_CORE_HANDLE *handle,
652                                    const OSSL_DISPATCH *in,
653                                    const OSSL_DISPATCH **out,
654                                    void **provctx)
655 {
656     OSSL_FUNC_core_get_libctx_fn *c_internal_get_libctx = NULL;
657
658     for (; in->function_id != 0; in++) {
659         switch (in->function_id) {
660         case OSSL_FUNC_CORE_GET_LIBCTX:
661             c_internal_get_libctx = OSSL_FUNC_core_get_libctx(in);
662             break;
663         default:
664             break;
665         }
666     }
667
668     if (c_internal_get_libctx == NULL)
669         return 0;
670
671     if ((*provctx = ossl_prov_ctx_new()) == NULL)
672         return 0;
673
674     /*
675      * Using the parent library context only works because we are a built-in
676      * internal provider. This is not something that most providers would be
677      * able to do.
678      */
679     ossl_prov_ctx_set0_libctx(*provctx,
680                               (OSSL_LIB_CTX *)c_internal_get_libctx(handle));
681     ossl_prov_ctx_set0_handle(*provctx, handle);
682
683     *out = intern_dispatch_table;
684     return 1;
685 }
686
687 void ERR_new(void)
688 {
689     c_new_error(NULL);
690 }
691
692 void ERR_set_debug(const char *file, int line, const char *func)
693 {
694     c_set_error_debug(NULL, file, line, func);
695 }
696
697 void ERR_set_error(int lib, int reason, const char *fmt, ...)
698 {
699     va_list args;
700
701     va_start(args, fmt);
702     c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
703     va_end(args);
704 }
705
706 void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
707 {
708     c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
709 }
710
711 int ERR_set_mark(void)
712 {
713     return c_set_error_mark(NULL);
714 }
715
716 int ERR_clear_last_mark(void)
717 {
718     return c_clear_last_error_mark(NULL);
719 }
720
721 int ERR_pop_to_mark(void)
722 {
723     return c_pop_error_to_mark(NULL);
724 }
725
726 /*
727  * This must take a library context, since it's called from the depths
728  * of crypto/initthread.c code, where it's (correctly) assumed that the
729  * passed caller argument is an OSSL_LIB_CTX pointer (since the same routine
730  * is also called from other parts of libcrypto, which all pass around a
731  * OSSL_LIB_CTX pointer)
732  */
733 const OSSL_CORE_HANDLE *FIPS_get_core_handle(OSSL_LIB_CTX *libctx)
734 {
735     FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(libctx,
736                                               OSSL_LIB_CTX_FIPS_PROV_INDEX,
737                                               &fips_prov_ossl_ctx_method);
738
739     if (fgbl == NULL)
740         return NULL;
741
742     return fgbl->handle;
743 }
744
745 void *CRYPTO_malloc(size_t num, const char *file, int line)
746 {
747     return c_CRYPTO_malloc(num, file, line);
748 }
749
750 void *CRYPTO_zalloc(size_t num, const char *file, int line)
751 {
752     return c_CRYPTO_zalloc(num, file, line);
753 }
754
755 void CRYPTO_free(void *ptr, const char *file, int line)
756 {
757     c_CRYPTO_free(ptr, file, line);
758 }
759
760 void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line)
761 {
762     c_CRYPTO_clear_free(ptr, num, file, line);
763 }
764
765 void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
766 {
767     return c_CRYPTO_realloc(addr, num, file, line);
768 }
769
770 void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
771                            const char *file, int line)
772 {
773     return c_CRYPTO_clear_realloc(addr, old_num, num, file, line);
774 }
775
776 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
777 {
778     return c_CRYPTO_secure_malloc(num, file, line);
779 }
780
781 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
782 {
783     return c_CRYPTO_secure_zalloc(num, file, line);
784 }
785
786 void CRYPTO_secure_free(void *ptr, const char *file, int line)
787 {
788     c_CRYPTO_secure_free(ptr, file, line);
789 }
790
791 void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *file, int line)
792 {
793     c_CRYPTO_secure_clear_free(ptr, num, file, line);
794 }
795
796 int CRYPTO_secure_allocated(const void *ptr)
797 {
798     return c_CRYPTO_secure_allocated(ptr);
799 }
800
801 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
802 {
803     va_list args;
804     int ret;
805
806     va_start(args, format);
807     ret = c_BIO_vsnprintf(buf, n, format, args);
808     va_end(args);
809     return ret;
810 }
811
812 int FIPS_security_check_enabled(void)
813 {
814     return fips_security_checks;
815 }
816
817 void OSSL_SELF_TEST_get_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK **cb,
818                                  void **cbarg)
819 {
820     if (libctx == NULL)
821         libctx = selftest_params.libctx;
822
823     if (c_stcbfn != NULL && c_get_libctx != NULL) {
824         /* Get the parent libctx */
825         c_stcbfn(c_get_libctx(FIPS_get_core_handle(libctx)), cb, cbarg);
826     } else {
827         if (cb != NULL)
828             *cb = NULL;
829         if (cbarg != NULL)
830             *cbarg = NULL;
831     }
832 }