2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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 "internal/cryptlib.h"
16 #include "prov/implementations.h"
17 #include "prov/provider_ctx.h"
18 #include "prov/providercommon.h"
19 #include "prov/providercommonerr.h"
20 #include "prov/provider_util.h"
21 #include "self_test.h"
23 static const char FIPS_DEFAULT_PROPERTIES[] = "provider=fips,fips=yes";
24 static const char FIPS_UNAPPROVED_PROPERTIES[] = "provider=fips,fips=no";
27 * Forward declarations to ensure that interface functions are correctly
30 static OSSL_FUNC_provider_teardown_fn fips_teardown;
31 static OSSL_FUNC_provider_gettable_params_fn fips_gettable_params;
32 static OSSL_FUNC_provider_get_params_fn fips_get_params;
33 static OSSL_FUNC_provider_query_operation_fn fips_query;
35 #define ALGC(NAMES, FUNC, CHECK) { { NAMES, FIPS_DEFAULT_PROPERTIES, FUNC }, CHECK }
36 #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
38 extern OSSL_FUNC_core_thread_start_fn *c_thread_start;
39 int FIPS_security_check_enabled(void);
42 * TODO(3.0): Should these be stored in the provider side provctx? Could they
43 * ever be different from one init to the next? Unfortunately we can't do this
44 * at the moment because c_put_error/c_add_error_vdata do not provide
45 * us with the OSSL_LIB_CTX as a parameter.
48 static SELF_TEST_POST_PARAMS selftest_params;
49 static int fips_security_checks = 1;
50 static const char *fips_security_check_option = "1";
52 /* Functions provided by the core */
53 static OSSL_FUNC_core_gettable_params_fn *c_gettable_params;
54 static OSSL_FUNC_core_get_params_fn *c_get_params;
55 OSSL_FUNC_core_thread_start_fn *c_thread_start;
56 static OSSL_FUNC_core_new_error_fn *c_new_error;
57 static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
58 static OSSL_FUNC_core_vset_error_fn *c_vset_error;
59 static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;
60 static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;
61 static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;
62 static OSSL_FUNC_CRYPTO_malloc_fn *c_CRYPTO_malloc;
63 static OSSL_FUNC_CRYPTO_zalloc_fn *c_CRYPTO_zalloc;
64 static OSSL_FUNC_CRYPTO_free_fn *c_CRYPTO_free;
65 static OSSL_FUNC_CRYPTO_clear_free_fn *c_CRYPTO_clear_free;
66 static OSSL_FUNC_CRYPTO_realloc_fn *c_CRYPTO_realloc;
67 static OSSL_FUNC_CRYPTO_clear_realloc_fn *c_CRYPTO_clear_realloc;
68 static OSSL_FUNC_CRYPTO_secure_malloc_fn *c_CRYPTO_secure_malloc;
69 static OSSL_FUNC_CRYPTO_secure_zalloc_fn *c_CRYPTO_secure_zalloc;
70 static OSSL_FUNC_CRYPTO_secure_free_fn *c_CRYPTO_secure_free;
71 static OSSL_FUNC_CRYPTO_secure_clear_free_fn *c_CRYPTO_secure_clear_free;
72 static OSSL_FUNC_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated;
73 static OSSL_FUNC_BIO_vsnprintf_fn *c_BIO_vsnprintf;
74 static OSSL_FUNC_self_test_cb_fn *c_stcbfn = NULL;
75 static OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
77 typedef struct fips_global_st {
78 const OSSL_CORE_HANDLE *handle;
81 static void *fips_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
83 FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
88 static void fips_prov_ossl_ctx_free(void *fgbl)
93 static const OSSL_LIB_CTX_METHOD fips_prov_ossl_ctx_method = {
94 fips_prov_ossl_ctx_new,
95 fips_prov_ossl_ctx_free,
99 /* Parameters we provide to the core */
100 static const OSSL_PARAM fips_param_types[] = {
101 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
102 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
103 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
104 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
105 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_SECURITY_CHECKS, OSSL_PARAM_INTEGER, NULL, 0),
110 * Parameters to retrieve from the core provider - required for self testing.
111 * NOTE: inside core_get_params() these will be loaded from config items
112 * stored inside prov->parameters (except for
113 * OSSL_PROV_PARAM_CORE_MODULE_FILENAME).
114 * OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS is not a self test parameter.
116 static OSSL_PARAM core_params[] =
118 OSSL_PARAM_utf8_ptr(OSSL_PROV_PARAM_CORE_MODULE_FILENAME,
119 selftest_params.module_filename,
120 sizeof(selftest_params.module_filename)),
121 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_MODULE_MAC,
122 selftest_params.module_checksum_data,
123 sizeof(selftest_params.module_checksum_data)),
124 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_MAC,
125 selftest_params.indicator_checksum_data,
126 sizeof(selftest_params.indicator_checksum_data)),
127 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
128 selftest_params.indicator_data,
129 sizeof(selftest_params.indicator_data)),
130 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
131 selftest_params.indicator_version,
132 sizeof(selftest_params.indicator_version)),
133 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
134 selftest_params.conditional_error_check,
135 sizeof(selftest_params.conditional_error_check)),
136 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
137 fips_security_check_option,
138 sizeof(fips_security_check_option)),
142 static const OSSL_PARAM *fips_gettable_params(void *provctx)
144 return fips_param_types;
147 static int fips_get_params(void *provctx, OSSL_PARAM params[])
151 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
152 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
154 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
155 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
157 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
158 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
160 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
161 if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
163 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_SECURITY_CHECKS);
164 if (p != NULL && !OSSL_PARAM_set_int(p, fips_security_checks))
169 static void set_self_test_cb(const OSSL_CORE_HANDLE *handle)
171 if (c_stcbfn != NULL && c_get_libctx != NULL) {
172 c_stcbfn(c_get_libctx(handle), &selftest_params.cb,
173 &selftest_params.cb_arg);
175 selftest_params.cb = NULL;
176 selftest_params.cb_arg = NULL;
180 static int fips_self_test(void *provctx)
182 set_self_test_cb(FIPS_get_core_handle(selftest_params.libctx));
183 return SELF_TEST_post(&selftest_params, 1) ? 1 : 0;
187 * For the algorithm names, we use the following formula for our primary
190 * ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
192 * VERSION is only present if there are multiple versions of
193 * an alg (MD2, MD4, MD5). It may be omitted if there is only
194 * one version (if a subsequent version is released in the future,
195 * we can always change the canonical name, and add the old name
198 * SUBNAME may be present where we are combining multiple
199 * algorithms together, e.g. MD5-SHA1.
201 * SIZE is only present if multiple versions of an algorithm exist
202 * with different sizes (e.g. AES-128-CBC, AES-256-CBC)
204 * MODE is only present where applicable.
206 * We add diverse other names where applicable, such as the names that
207 * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
208 * we have used historically.
210 static const OSSL_ALGORITHM fips_digests[] = {
211 /* Our primary name:NiST name[:our older names] */
212 { "SHA1:SHA-1:SSL3-SHA1", FIPS_DEFAULT_PROPERTIES, ossl_sha1_functions },
213 { "SHA2-224:SHA-224:SHA224", FIPS_DEFAULT_PROPERTIES,
214 ossl_sha224_functions },
215 { "SHA2-256:SHA-256:SHA256", FIPS_DEFAULT_PROPERTIES,
216 ossl_sha256_functions },
217 { "SHA2-384:SHA-384:SHA384", FIPS_DEFAULT_PROPERTIES,
218 ossl_sha384_functions },
219 { "SHA2-512:SHA-512:SHA512", FIPS_DEFAULT_PROPERTIES,
220 ossl_sha512_functions },
221 { "SHA2-512/224:SHA-512/224:SHA512-224", FIPS_DEFAULT_PROPERTIES,
222 ossl_sha512_224_functions },
223 { "SHA2-512/256:SHA-512/256:SHA512-256", FIPS_DEFAULT_PROPERTIES,
224 ossl_sha512_256_functions },
226 /* We agree with NIST here, so one name only */
227 { "SHA3-224", FIPS_DEFAULT_PROPERTIES, ossl_sha3_224_functions },
228 { "SHA3-256", FIPS_DEFAULT_PROPERTIES, ossl_sha3_256_functions },
229 { "SHA3-384", FIPS_DEFAULT_PROPERTIES, ossl_sha3_384_functions },
230 { "SHA3-512", FIPS_DEFAULT_PROPERTIES, ossl_sha3_512_functions },
232 { "SHAKE-128:SHAKE128", FIPS_DEFAULT_PROPERTIES, ossl_shake_128_functions },
233 { "SHAKE-256:SHAKE256", FIPS_DEFAULT_PROPERTIES, ossl_shake_256_functions },
236 * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
237 * KMAC128 and KMAC256.
239 { "KECCAK-KMAC-128:KECCAK-KMAC128", FIPS_DEFAULT_PROPERTIES,
240 ossl_keccak_kmac_128_functions },
241 { "KECCAK-KMAC-256:KECCAK-KMAC256", FIPS_DEFAULT_PROPERTIES,
242 ossl_keccak_kmac_256_functions },
246 static const OSSL_ALGORITHM_CAPABLE fips_ciphers[] = {
247 /* Our primary name[:ASN.1 OID name][:our older names] */
248 ALG("AES-256-ECB", ossl_aes256ecb_functions),
249 ALG("AES-192-ECB", ossl_aes192ecb_functions),
250 ALG("AES-128-ECB", ossl_aes128ecb_functions),
251 ALG("AES-256-CBC:AES256", ossl_aes256cbc_functions),
252 ALG("AES-192-CBC:AES192", ossl_aes192cbc_functions),
253 ALG("AES-128-CBC:AES128", ossl_aes128cbc_functions),
254 ALG("AES-256-CBC-CTS", ossl_aes256cbc_cts_functions),
255 ALG("AES-192-CBC-CTS", ossl_aes192cbc_cts_functions),
256 ALG("AES-128-CBC-CTS", ossl_aes128cbc_cts_functions),
257 ALG("AES-256-OFB", ossl_aes256ofb_functions),
258 ALG("AES-192-OFB", ossl_aes192ofb_functions),
259 ALG("AES-128-OFB", ossl_aes128ofb_functions),
260 ALG("AES-256-CFB", ossl_aes256cfb_functions),
261 ALG("AES-192-CFB", ossl_aes192cfb_functions),
262 ALG("AES-128-CFB", ossl_aes128cfb_functions),
263 ALG("AES-256-CFB1", ossl_aes256cfb1_functions),
264 ALG("AES-192-CFB1", ossl_aes192cfb1_functions),
265 ALG("AES-128-CFB1", ossl_aes128cfb1_functions),
266 ALG("AES-256-CFB8", ossl_aes256cfb8_functions),
267 ALG("AES-192-CFB8", ossl_aes192cfb8_functions),
268 ALG("AES-128-CFB8", ossl_aes128cfb8_functions),
269 ALG("AES-256-CTR", ossl_aes256ctr_functions),
270 ALG("AES-192-CTR", ossl_aes192ctr_functions),
271 ALG("AES-128-CTR", ossl_aes128ctr_functions),
272 ALG("AES-256-XTS", ossl_aes256xts_functions),
273 ALG("AES-128-XTS", ossl_aes128xts_functions),
274 ALG("AES-256-GCM:id-aes256-GCM", ossl_aes256gcm_functions),
275 ALG("AES-192-GCM:id-aes192-GCM", ossl_aes192gcm_functions),
276 ALG("AES-128-GCM:id-aes128-GCM", ossl_aes128gcm_functions),
277 ALG("AES-256-CCM:id-aes256-CCM", ossl_aes256ccm_functions),
278 ALG("AES-192-CCM:id-aes192-CCM", ossl_aes192ccm_functions),
279 ALG("AES-128-CCM:id-aes128-CCM", ossl_aes128ccm_functions),
280 ALG("AES-256-WRAP:id-aes256-wrap:AES256-WRAP", ossl_aes256wrap_functions),
281 ALG("AES-192-WRAP:id-aes192-wrap:AES192-WRAP", ossl_aes192wrap_functions),
282 ALG("AES-128-WRAP:id-aes128-wrap:AES128-WRAP", ossl_aes128wrap_functions),
283 ALG("AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD",
284 ossl_aes256wrappad_functions),
285 ALG("AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD",
286 ossl_aes192wrappad_functions),
287 ALG("AES-128-WRAP-PAD:id-aes128-wrap-pad:AES128-WRAP-PAD",
288 ossl_aes128wrappad_functions),
289 ALGC("AES-128-CBC-HMAC-SHA1", ossl_aes128cbc_hmac_sha1_functions,
290 ossl_cipher_capable_aes_cbc_hmac_sha1),
291 ALGC("AES-256-CBC-HMAC-SHA1", ossl_aes256cbc_hmac_sha1_functions,
292 ossl_cipher_capable_aes_cbc_hmac_sha1),
293 ALGC("AES-128-CBC-HMAC-SHA256", ossl_aes128cbc_hmac_sha256_functions,
294 ossl_cipher_capable_aes_cbc_hmac_sha256),
295 ALGC("AES-256-CBC-HMAC-SHA256", ossl_aes256cbc_hmac_sha256_functions,
296 ossl_cipher_capable_aes_cbc_hmac_sha256),
297 #ifndef OPENSSL_NO_DES
298 ALG("DES-EDE3-ECB:DES-EDE3", ossl_tdes_ede3_ecb_functions),
299 ALG("DES-EDE3-CBC:DES3", ossl_tdes_ede3_cbc_functions),
300 #endif /* OPENSSL_NO_DES */
301 { { NULL, NULL, NULL }, NULL }
303 static OSSL_ALGORITHM exported_fips_ciphers[OSSL_NELEM(fips_ciphers)];
305 static const OSSL_ALGORITHM fips_macs[] = {
306 #ifndef OPENSSL_NO_CMAC
307 { "CMAC", FIPS_DEFAULT_PROPERTIES, ossl_cmac_functions },
309 { "GMAC", FIPS_DEFAULT_PROPERTIES, ossl_gmac_functions },
310 { "HMAC", FIPS_DEFAULT_PROPERTIES, ossl_hmac_functions },
311 { "KMAC-128:KMAC128", FIPS_DEFAULT_PROPERTIES, ossl_kmac128_functions },
312 { "KMAC-256:KMAC256", FIPS_DEFAULT_PROPERTIES, ossl_kmac256_functions },
316 static const OSSL_ALGORITHM fips_kdfs[] = {
317 { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_functions },
318 { "SSKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_sskdf_functions },
319 { "PBKDF2", FIPS_DEFAULT_PROPERTIES, ossl_kdf_pbkdf2_functions },
320 { "SSHKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_sshkdf_functions },
321 { "X963KDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_x963_kdf_functions },
322 { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_tls1_prf_functions },
323 { "KBKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_kbkdf_functions },
327 static const OSSL_ALGORITHM fips_rands[] = {
328 { "CTR-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_ctr_functions },
329 { "HASH-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_hash_functions },
330 { "HMAC-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_ossl_hmac_functions },
331 { "TEST-RAND", FIPS_UNAPPROVED_PROPERTIES, ossl_test_rng_functions },
335 static const OSSL_ALGORITHM fips_keyexch[] = {
336 #ifndef OPENSSL_NO_DH
337 { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES, ossl_dh_keyexch_functions },
339 #ifndef OPENSSL_NO_EC
340 { "ECDH", FIPS_DEFAULT_PROPERTIES, ecossl_dh_keyexch_functions },
341 { "X25519", FIPS_DEFAULT_PROPERTIES, ossl_x25519_keyexch_functions },
342 { "X448", FIPS_DEFAULT_PROPERTIES, ossl_x448_keyexch_functions },
344 { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES,
345 ossl_kdf_tls1_prf_keyexch_functions },
346 { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_keyexch_functions },
350 static const OSSL_ALGORITHM fips_signature[] = {
351 #ifndef OPENSSL_NO_DSA
352 { "DSA:dsaEncryption", FIPS_DEFAULT_PROPERTIES,
353 ossl_dsa_signature_functions },
355 { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
356 ossl_rsa_signature_functions },
357 #ifndef OPENSSL_NO_EC
358 { "ED25519", FIPS_DEFAULT_PROPERTIES, ossl_ed25519_signature_functions },
359 { "ED448", FIPS_DEFAULT_PROPERTIES, ossl_ed448_signature_functions },
360 { "ECDSA", FIPS_DEFAULT_PROPERTIES, ecossl_dsa_signature_functions },
362 { "HMAC", FIPS_DEFAULT_PROPERTIES,
363 ossl_mac_legacy_hmac_signature_functions },
364 #ifndef OPENSSL_NO_CMAC
365 { "CMAC", FIPS_DEFAULT_PROPERTIES,
366 ossl_mac_legacy_cmac_signature_functions },
371 static const OSSL_ALGORITHM fips_asym_cipher[] = {
372 { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
373 ossl_rsa_asym_cipher_functions },
377 static const OSSL_ALGORITHM fips_asym_kem[] = {
378 { "RSA", FIPS_DEFAULT_PROPERTIES, ossl_rsa_asym_kem_functions },
382 static const OSSL_ALGORITHM fips_keymgmt[] = {
383 #ifndef OPENSSL_NO_DH
384 { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES, ossl_dh_keymgmt_functions },
385 { "DHX:X9.42 DH:dhpublicnumber", FIPS_DEFAULT_PROPERTIES,
386 ossl_dhx_keymgmt_functions },
388 #ifndef OPENSSL_NO_DSA
389 { "DSA", FIPS_DEFAULT_PROPERTIES, ossl_dsa_keymgmt_functions },
391 { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
392 ossl_rsa_keymgmt_functions },
393 { "RSA-PSS:RSASSA-PSS", FIPS_DEFAULT_PROPERTIES,
394 ossl_rsapss_keymgmt_functions },
395 #ifndef OPENSSL_NO_EC
396 { "EC:id-ecPublicKey", FIPS_DEFAULT_PROPERTIES, ossl_ec_keymgmt_functions },
397 { "X25519", FIPS_DEFAULT_PROPERTIES, ossl_x25519_keymgmt_functions },
398 { "X448", FIPS_DEFAULT_PROPERTIES, ossl_x448_keymgmt_functions },
399 { "ED25519", FIPS_DEFAULT_PROPERTIES, ossl_ed25519_keymgmt_functions },
400 { "ED448", FIPS_DEFAULT_PROPERTIES, ossl_ed448_keymgmt_functions },
402 { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions },
403 { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions },
404 { "HMAC", FIPS_DEFAULT_PROPERTIES, ossl_mac_legacy_keymgmt_functions },
405 #ifndef OPENSSL_NO_CMAC
406 { "CMAC", FIPS_DEFAULT_PROPERTIES,
407 ossl_cossl_mac_legacy_keymgmt_functions },
412 static const OSSL_ALGORITHM *fips_query(void *provctx, int operation_id,
417 if (!ossl_prov_is_running())
420 switch (operation_id) {
424 ossl_prov_cache_exported_algorithms(fips_ciphers,
425 exported_fips_ciphers);
426 return exported_fips_ciphers;
433 case OSSL_OP_KEYMGMT:
435 case OSSL_OP_KEYEXCH:
437 case OSSL_OP_SIGNATURE:
438 return fips_signature;
439 case OSSL_OP_ASYM_CIPHER:
440 return fips_asym_cipher;
442 return fips_asym_kem;
447 static void fips_teardown(void *provctx)
449 OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
450 ossl_prov_ctx_free(provctx);
453 static void fips_intern_teardown(void *provctx)
456 * We know that the library context is the same as for the outer provider,
457 * so no need to destroy it here.
459 ossl_prov_ctx_free(provctx);
462 /* Functions we provide to the core */
463 static const OSSL_DISPATCH fips_dispatch_table[] = {
464 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_teardown },
465 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params },
466 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
467 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
468 { OSSL_FUNC_PROVIDER_GET_CAPABILITIES,
469 (void (*)(void))provider_get_capabilities },
470 { OSSL_FUNC_PROVIDER_SELF_TEST, (void (*)(void))fips_self_test },
474 /* Functions we provide to ourself */
475 static const OSSL_DISPATCH intern_dispatch_table[] = {
476 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_intern_teardown },
477 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
481 int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
482 const OSSL_DISPATCH *in,
483 const OSSL_DISPATCH **out,
487 OSSL_LIB_CTX *libctx = NULL;
489 for (; in->function_id != 0; in++) {
490 switch (in->function_id) {
491 case OSSL_FUNC_CORE_GET_LIBCTX:
492 c_get_libctx = OSSL_FUNC_core_get_libctx(in);
494 case OSSL_FUNC_CORE_GETTABLE_PARAMS:
495 c_gettable_params = OSSL_FUNC_core_gettable_params(in);
497 case OSSL_FUNC_CORE_GET_PARAMS:
498 c_get_params = OSSL_FUNC_core_get_params(in);
500 case OSSL_FUNC_CORE_THREAD_START:
501 c_thread_start = OSSL_FUNC_core_thread_start(in);
503 case OSSL_FUNC_CORE_NEW_ERROR:
504 c_new_error = OSSL_FUNC_core_new_error(in);
506 case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
507 c_set_error_debug = OSSL_FUNC_core_set_error_debug(in);
509 case OSSL_FUNC_CORE_VSET_ERROR:
510 c_vset_error = OSSL_FUNC_core_vset_error(in);
512 case OSSL_FUNC_CORE_SET_ERROR_MARK:
513 c_set_error_mark = OSSL_FUNC_core_set_error_mark(in);
515 case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
516 c_clear_last_error_mark = OSSL_FUNC_core_clear_last_error_mark(in);
518 case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
519 c_pop_error_to_mark = OSSL_FUNC_core_pop_error_to_mark(in);
521 case OSSL_FUNC_CRYPTO_MALLOC:
522 c_CRYPTO_malloc = OSSL_FUNC_CRYPTO_malloc(in);
524 case OSSL_FUNC_CRYPTO_ZALLOC:
525 c_CRYPTO_zalloc = OSSL_FUNC_CRYPTO_zalloc(in);
527 case OSSL_FUNC_CRYPTO_FREE:
528 c_CRYPTO_free = OSSL_FUNC_CRYPTO_free(in);
530 case OSSL_FUNC_CRYPTO_CLEAR_FREE:
531 c_CRYPTO_clear_free = OSSL_FUNC_CRYPTO_clear_free(in);
533 case OSSL_FUNC_CRYPTO_REALLOC:
534 c_CRYPTO_realloc = OSSL_FUNC_CRYPTO_realloc(in);
536 case OSSL_FUNC_CRYPTO_CLEAR_REALLOC:
537 c_CRYPTO_clear_realloc = OSSL_FUNC_CRYPTO_clear_realloc(in);
539 case OSSL_FUNC_CRYPTO_SECURE_MALLOC:
540 c_CRYPTO_secure_malloc = OSSL_FUNC_CRYPTO_secure_malloc(in);
542 case OSSL_FUNC_CRYPTO_SECURE_ZALLOC:
543 c_CRYPTO_secure_zalloc = OSSL_FUNC_CRYPTO_secure_zalloc(in);
545 case OSSL_FUNC_CRYPTO_SECURE_FREE:
546 c_CRYPTO_secure_free = OSSL_FUNC_CRYPTO_secure_free(in);
548 case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE:
549 c_CRYPTO_secure_clear_free = OSSL_FUNC_CRYPTO_secure_clear_free(in);
551 case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
552 c_CRYPTO_secure_allocated = OSSL_FUNC_CRYPTO_secure_allocated(in);
554 case OSSL_FUNC_BIO_NEW_FILE:
555 selftest_params.bio_new_file_cb = OSSL_FUNC_BIO_new_file(in);
557 case OSSL_FUNC_BIO_NEW_MEMBUF:
558 selftest_params.bio_new_buffer_cb = OSSL_FUNC_BIO_new_membuf(in);
560 case OSSL_FUNC_BIO_READ_EX:
561 selftest_params.bio_read_ex_cb = OSSL_FUNC_BIO_read_ex(in);
563 case OSSL_FUNC_BIO_FREE:
564 selftest_params.bio_free_cb = OSSL_FUNC_BIO_free(in);
566 case OSSL_FUNC_BIO_VSNPRINTF:
567 c_BIO_vsnprintf = OSSL_FUNC_BIO_vsnprintf(in);
569 case OSSL_FUNC_SELF_TEST_CB: {
570 c_stcbfn = OSSL_FUNC_self_test_cb(in);
574 /* Just ignore anything we don't understand */
579 set_self_test_cb(handle);
581 if (!c_get_params(handle, core_params)) {
582 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
585 /* Disable the conditional error check if is disabled in the fips config file*/
586 if (selftest_params.conditional_error_check != NULL
587 && strcmp(selftest_params.conditional_error_check, "0") == 0)
588 SELF_TEST_disable_conditional_error_state();
590 /* Disable the security check if is disabled in the fips config file*/
591 if (fips_security_check_option != NULL
592 && strcmp(fips_security_check_option, "0") == 0)
593 fips_security_checks = 0;
595 /* Create a context. */
596 if ((*provctx = ossl_prov_ctx_new()) == NULL
597 || (libctx = OSSL_LIB_CTX_new()) == NULL) {
599 * We free libctx separately here and only here because it hasn't
600 * been attached to *provctx. All other error paths below rely
601 * solely on fips_teardown.
603 OSSL_LIB_CTX_free(libctx);
606 ossl_prov_ctx_set0_libctx(*provctx, libctx);
607 ossl_prov_ctx_set0_handle(*provctx, handle);
609 if ((fgbl = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_FIPS_PROV_INDEX,
610 &fips_prov_ossl_ctx_method)) == NULL)
613 fgbl->handle = handle;
615 selftest_params.libctx = libctx;
616 if (!SELF_TEST_post(&selftest_params, 0)) {
617 ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_POST_FAILURE);
621 /* TODO(3.0): Tests will hang if this is removed */
622 (void)RAND_get0_public(libctx);
624 *out = fips_dispatch_table;
627 fips_teardown(*provctx);
633 * The internal init function used when the FIPS module uses EVP to call
634 * another algorithm also in the FIPS module. This is a recursive call that has
635 * been made from within the FIPS module itself. To make this work, we populate
636 * the provider context of this inner instance with the same library context
637 * that was used in the EVP call that initiated this recursive call.
639 OSSL_provider_init_fn fips_intern_provider_init;
640 int fips_intern_provider_init(const OSSL_CORE_HANDLE *handle,
641 const OSSL_DISPATCH *in,
642 const OSSL_DISPATCH **out,
645 OSSL_FUNC_core_get_libctx_fn *c_internal_get_libctx = NULL;
647 for (; in->function_id != 0; in++) {
648 switch (in->function_id) {
649 case OSSL_FUNC_CORE_GET_LIBCTX:
650 c_internal_get_libctx = OSSL_FUNC_core_get_libctx(in);
657 if (c_internal_get_libctx == NULL)
660 if ((*provctx = ossl_prov_ctx_new()) == NULL)
664 * Using the parent library context only works because we are a built-in
665 * internal provider. This is not something that most providers would be
668 ossl_prov_ctx_set0_libctx(*provctx,
669 (OSSL_LIB_CTX *)c_internal_get_libctx(handle));
670 ossl_prov_ctx_set0_handle(*provctx, handle);
672 *out = intern_dispatch_table;
681 void ERR_set_debug(const char *file, int line, const char *func)
683 c_set_error_debug(NULL, file, line, func);
686 void ERR_set_error(int lib, int reason, const char *fmt, ...)
691 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
695 void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
697 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
700 int ERR_set_mark(void)
702 return c_set_error_mark(NULL);
705 int ERR_clear_last_mark(void)
707 return c_clear_last_error_mark(NULL);
710 int ERR_pop_to_mark(void)
712 return c_pop_error_to_mark(NULL);
716 * This must take a library context, since it's called from the depths
717 * of crypto/initthread.c code, where it's (correctly) assumed that the
718 * passed caller argument is an OSSL_LIB_CTX pointer (since the same routine
719 * is also called from other parts of libcrypto, which all pass around a
720 * OSSL_LIB_CTX pointer)
722 const OSSL_CORE_HANDLE *FIPS_get_core_handle(OSSL_LIB_CTX *libctx)
724 FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(libctx,
725 OSSL_LIB_CTX_FIPS_PROV_INDEX,
726 &fips_prov_ossl_ctx_method);
734 void *CRYPTO_malloc(size_t num, const char *file, int line)
736 return c_CRYPTO_malloc(num, file, line);
739 void *CRYPTO_zalloc(size_t num, const char *file, int line)
741 return c_CRYPTO_zalloc(num, file, line);
744 void CRYPTO_free(void *ptr, const char *file, int line)
746 c_CRYPTO_free(ptr, file, line);
749 void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line)
751 c_CRYPTO_clear_free(ptr, num, file, line);
754 void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
756 return c_CRYPTO_realloc(addr, num, file, line);
759 void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
760 const char *file, int line)
762 return c_CRYPTO_clear_realloc(addr, old_num, num, file, line);
765 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
767 return c_CRYPTO_secure_malloc(num, file, line);
770 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
772 return c_CRYPTO_secure_zalloc(num, file, line);
775 void CRYPTO_secure_free(void *ptr, const char *file, int line)
777 c_CRYPTO_secure_free(ptr, file, line);
780 void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *file, int line)
782 c_CRYPTO_secure_clear_free(ptr, num, file, line);
785 int CRYPTO_secure_allocated(const void *ptr)
787 return c_CRYPTO_secure_allocated(ptr);
790 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
795 va_start(args, format);
796 ret = c_BIO_vsnprintf(buf, n, format, args);
801 int FIPS_security_check_enabled(void)
803 return fips_security_checks;
806 void OSSL_SELF_TEST_get_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK **cb,
810 libctx = selftest_params.libctx;
812 if (c_stcbfn != NULL && c_get_libctx != NULL) {
813 /* Get the parent libctx */
814 c_stcbfn(c_get_libctx(FIPS_get_core_handle(libctx)), cb, cbarg);