1 /*
2 * Copyright 2019-2024 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 <assert.h>
11 #include <openssl/core_dispatch.h>
12 #include <openssl/core_names.h>
13 #include <openssl/params.h>
14 #include <openssl/fips_names.h>
15 #include <openssl/fipskey.h>
16 #include <openssl/rand.h> /* RAND_get0_public() */
17 #include <openssl/proverr.h>
18 #include <openssl/indicator.h>
19 #include "internal/cryptlib.h"
20 #include "prov/implementations.h"
21 #include "prov/names.h"
22 #include "prov/provider_ctx.h"
23 #include "prov/providercommon.h"
24 #include "prov/provider_util.h"
25 #include "prov/seeding.h"
26 #include "internal/nelem.h"
27 #include "self_test.h"
28 #include "crypto/context.h"
29 #include "fipscommon.h"
30 #include "internal/core.h"
31
32 static const char FIPS_DEFAULT_PROPERTIES[] = "provider=fips,fips=yes";
33 static const char FIPS_UNAPPROVED_PROPERTIES[] = "provider=fips,fips=no";
34
35 /*
36 * Forward declarations to ensure that interface functions are correctly
37 * defined.
38 */
39 static OSSL_FUNC_provider_teardown_fn fips_teardown;
40 static OSSL_FUNC_provider_gettable_params_fn fips_gettable_params;
41 static OSSL_FUNC_provider_get_params_fn fips_get_params;
42 static OSSL_FUNC_provider_query_operation_fn fips_query;
43 static OSSL_FUNC_provider_query_operation_fn fips_query_internal;
44
45 #define ALGC(NAMES, FUNC, CHECK) \
46 { { NAMES, FIPS_DEFAULT_PROPERTIES, FUNC }, CHECK }
47 #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
48
49 extern OSSL_FUNC_core_thread_start_fn *c_thread_start;
50
51 /*
52 * Should these function pointers be stored in the provider side provctx? Could
53 * they ever be different from one init to the next? We assume not for now.
54 */
55
56 /* Functions provided by the core */
57 static OSSL_FUNC_core_gettable_params_fn *c_gettable_params;
58 static OSSL_FUNC_core_get_params_fn *c_get_params;
59 OSSL_FUNC_core_thread_start_fn *c_thread_start;
60 static OSSL_FUNC_core_new_error_fn *c_new_error;
61 static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
62 static OSSL_FUNC_core_vset_error_fn *c_vset_error;
63 static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;
64 static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;
65 static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;
66 static OSSL_FUNC_CRYPTO_malloc_fn *c_CRYPTO_malloc;
67 static OSSL_FUNC_CRYPTO_zalloc_fn *c_CRYPTO_zalloc;
68 static OSSL_FUNC_CRYPTO_free_fn *c_CRYPTO_free;
69 static OSSL_FUNC_CRYPTO_clear_free_fn *c_CRYPTO_clear_free;
70 static OSSL_FUNC_CRYPTO_realloc_fn *c_CRYPTO_realloc;
71 static OSSL_FUNC_CRYPTO_clear_realloc_fn *c_CRYPTO_clear_realloc;
72 static OSSL_FUNC_CRYPTO_secure_malloc_fn *c_CRYPTO_secure_malloc;
73 static OSSL_FUNC_CRYPTO_secure_zalloc_fn *c_CRYPTO_secure_zalloc;
74 static OSSL_FUNC_CRYPTO_secure_free_fn *c_CRYPTO_secure_free;
75 static OSSL_FUNC_CRYPTO_secure_clear_free_fn *c_CRYPTO_secure_clear_free;
76 static OSSL_FUNC_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated;
77 static OSSL_FUNC_BIO_vsnprintf_fn *c_BIO_vsnprintf;
78 static OSSL_FUNC_self_test_cb_fn *c_stcbfn = NULL;
79 static OSSL_FUNC_indicator_cb_fn *c_indcbfn = NULL;
80 static OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
81
82 typedef struct {
83 const char *option;
84 unsigned char enabled;
85 } FIPS_OPTION;
86
87 typedef struct fips_global_st {
88 const OSSL_CORE_HANDLE *handle;
89 SELF_TEST_POST_PARAMS selftest_params;
90
91 #define OSSL_FIPS_PARAM(structname, paramname, initvalue) \
92 FIPS_OPTION fips_##structname;
93 #include "fips_indicator_params.inc"
94 #undef OSSL_FIPS_PARAM
95
96 } FIPS_GLOBAL;
97
init_fips_option(FIPS_OPTION * opt,int enabled)98 static void init_fips_option(FIPS_OPTION *opt, int enabled)
99 {
100 opt->enabled = enabled;
101 opt->option = enabled ? "1" : "0";
102 }
103
ossl_fips_prov_ossl_ctx_new(OSSL_LIB_CTX * libctx)104 void *ossl_fips_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
105 {
106 FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
107
108 if (fgbl == NULL)
109 return NULL;
110
111 #define OSSL_FIPS_PARAM(structname, paramname, initvalue) \
112 init_fips_option(&fgbl->fips_##structname, initvalue);
113 #include "fips_indicator_params.inc"
114 #undef OSSL_FIPS_PARAM
115
116 return fgbl;
117 }
118
ossl_fips_prov_ossl_ctx_free(void * fgbl)119 void ossl_fips_prov_ossl_ctx_free(void *fgbl)
120 {
121 OPENSSL_free(fgbl);
122 }
123
124 /*
125 * Parameters to retrieve from the core provider
126 * NOTE: inside core_get_params() these will be loaded from config items
127 * stored inside prov->parameters
128 */
fips_get_params_from_core(FIPS_GLOBAL * fgbl)129 static int fips_get_params_from_core(FIPS_GLOBAL *fgbl)
130 {
131 OSSL_PARAM core_params[32], *p = core_params;
132
133 #define OSSL_FIPS_PARAM(structname, paramname) \
134 *p++ = OSSL_PARAM_construct_utf8_ptr( \
135 paramname, (char **)&fgbl->selftest_params.structname, \
136 sizeof(fgbl->selftest_params.structname));
137
138 /* Parameters required for self testing */
139 #include "fips_selftest_params.inc"
140 #undef OSSL_FIPS_PARAM
141
142 /* FIPS indicator options can be enabled or disabled independently */
143 #define OSSL_FIPS_PARAM(structname, paramname, initvalue) \
144 *p++ = OSSL_PARAM_construct_utf8_ptr( \
145 OSSL_PROV_PARAM_##paramname, \
146 (char **)&fgbl->fips_##structname.option, \
147 sizeof(fgbl->fips_##structname.option));
148 #include "fips_indicator_params.inc"
149 #undef OSSL_FIPS_PARAM
150
151 *p = OSSL_PARAM_construct_end();
152
153 if (!c_get_params(fgbl->handle, core_params)) {
154 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
155 return 0;
156 }
157
158 return 1;
159 }
160
fips_gettable_params(void * provctx)161 static const OSSL_PARAM *fips_gettable_params(void *provctx)
162 {
163 /* Parameters we provide to the core */
164 static const OSSL_PARAM fips_param_types[] = {
165 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
166 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
167 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
168 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
169
170 #define OSSL_FIPS_PARAM(structname, paramname, initvalue) \
171 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_##paramname, OSSL_PARAM_INTEGER, NULL, 0),
172 #include "fips_indicator_params.inc"
173 #undef OSSL_FIPS_PARAM
174
175 OSSL_PARAM_END
176 };
177 return fips_param_types;
178 }
179
fips_get_params(void * provctx,OSSL_PARAM params[])180 static int fips_get_params(void *provctx, OSSL_PARAM params[])
181 {
182 OSSL_PARAM *p;
183 FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(ossl_prov_ctx_get0_libctx(provctx),
184 OSSL_LIB_CTX_FIPS_PROV_INDEX);
185
186 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
187 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, FIPS_VENDOR))
188 return 0;
189 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
190 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
191 return 0;
192 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
193 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
194 return 0;
195 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
196 if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
197 return 0;
198
199 #define OSSL_FIPS_PARAM(structname, paramname, initvalue) \
200 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_##paramname); \
201 if (p != NULL && !OSSL_PARAM_set_int(p, fgbl->fips_##structname.enabled)) \
202 return 0;
203 #include "fips_indicator_params.inc"
204 #undef OSSL_FIPS_PARAM
205
206 return 1;
207 }
208
set_self_test_cb(FIPS_GLOBAL * fgbl)209 static void set_self_test_cb(FIPS_GLOBAL *fgbl)
210 {
211 const OSSL_CORE_HANDLE *handle =
212 FIPS_get_core_handle(fgbl->selftest_params.libctx);
213
214 if (c_stcbfn != NULL && c_get_libctx != NULL) {
215 c_stcbfn(c_get_libctx(handle), &fgbl->selftest_params.cb,
216 &fgbl->selftest_params.cb_arg);
217 } else {
218 fgbl->selftest_params.cb = NULL;
219 fgbl->selftest_params.cb_arg = NULL;
220 }
221 }
222
fips_self_test(void * provctx)223 static int fips_self_test(void *provctx)
224 {
225 FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(ossl_prov_ctx_get0_libctx(provctx),
226 OSSL_LIB_CTX_FIPS_PROV_INDEX);
227
228 set_self_test_cb(fgbl);
229 return SELF_TEST_post(&fgbl->selftest_params, 1) ? 1 : 0;
230 }
231
232 /*
233 * For the algorithm names, we use the following formula for our primary
234 * names:
235 *
236 * ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
237 *
238 * VERSION is only present if there are multiple versions of
239 * an alg (MD2, MD4, MD5). It may be omitted if there is only
240 * one version (if a subsequent version is released in the future,
241 * we can always change the canonical name, and add the old name
242 * as an alias).
243 *
244 * SUBNAME may be present where we are combining multiple
245 * algorithms together, e.g. MD5-SHA1.
246 *
247 * SIZE is only present if multiple versions of an algorithm exist
248 * with different sizes (e.g. AES-128-CBC, AES-256-CBC)
249 *
250 * MODE is only present where applicable.
251 *
252 * We add diverse other names where applicable, such as the names that
253 * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
254 * we have used historically.
255 */
256 static const OSSL_ALGORITHM fips_digests[] = {
257 /* Our primary name:NiST name[:our older names] */
258 { PROV_NAMES_SHA1, FIPS_DEFAULT_PROPERTIES, ossl_sha1_functions },
259 { PROV_NAMES_SHA2_224, FIPS_DEFAULT_PROPERTIES, ossl_sha224_functions },
260 { PROV_NAMES_SHA2_256, FIPS_DEFAULT_PROPERTIES, ossl_sha256_functions },
261 { PROV_NAMES_SHA2_384, FIPS_DEFAULT_PROPERTIES, ossl_sha384_functions },
262 { PROV_NAMES_SHA2_512, FIPS_DEFAULT_PROPERTIES, ossl_sha512_functions },
263 { PROV_NAMES_SHA2_512_224, FIPS_DEFAULT_PROPERTIES,
264 ossl_sha512_224_functions },
265 { PROV_NAMES_SHA2_512_256, FIPS_DEFAULT_PROPERTIES,
266 ossl_sha512_256_functions },
267
268 /* We agree with NIST here, so one name only */
269 { PROV_NAMES_SHA3_224, FIPS_DEFAULT_PROPERTIES, ossl_sha3_224_functions },
270 { PROV_NAMES_SHA3_256, FIPS_DEFAULT_PROPERTIES, ossl_sha3_256_functions },
271 { PROV_NAMES_SHA3_384, FIPS_DEFAULT_PROPERTIES, ossl_sha3_384_functions },
272 { PROV_NAMES_SHA3_512, FIPS_DEFAULT_PROPERTIES, ossl_sha3_512_functions },
273
274 { PROV_NAMES_SHAKE_128, FIPS_DEFAULT_PROPERTIES, ossl_shake_128_functions },
275 { PROV_NAMES_SHAKE_256, FIPS_DEFAULT_PROPERTIES, ossl_shake_256_functions },
276
277 /*
278 * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
279 * KMAC128 and KMAC256.
280 */
281 { PROV_NAMES_KECCAK_KMAC_128, FIPS_DEFAULT_PROPERTIES,
282 ossl_keccak_kmac_128_functions },
283 { PROV_NAMES_KECCAK_KMAC_256, FIPS_DEFAULT_PROPERTIES,
284 ossl_keccak_kmac_256_functions },
285 { NULL, NULL, NULL }
286 };
287
288 static const OSSL_ALGORITHM_CAPABLE fips_ciphers[] = {
289 /* Our primary name[:ASN.1 OID name][:our older names] */
290 ALG(PROV_NAMES_AES_256_ECB, ossl_aes256ecb_functions),
291 ALG(PROV_NAMES_AES_192_ECB, ossl_aes192ecb_functions),
292 ALG(PROV_NAMES_AES_128_ECB, ossl_aes128ecb_functions),
293 ALG(PROV_NAMES_AES_256_CBC, ossl_aes256cbc_functions),
294 ALG(PROV_NAMES_AES_192_CBC, ossl_aes192cbc_functions),
295 ALG(PROV_NAMES_AES_128_CBC, ossl_aes128cbc_functions),
296 ALG(PROV_NAMES_AES_256_CBC_CTS, ossl_aes256cbc_cts_functions),
297 ALG(PROV_NAMES_AES_192_CBC_CTS, ossl_aes192cbc_cts_functions),
298 ALG(PROV_NAMES_AES_128_CBC_CTS, ossl_aes128cbc_cts_functions),
299 ALG(PROV_NAMES_AES_256_OFB, ossl_aes256ofb_functions),
300 ALG(PROV_NAMES_AES_192_OFB, ossl_aes192ofb_functions),
301 ALG(PROV_NAMES_AES_128_OFB, ossl_aes128ofb_functions),
302 ALG(PROV_NAMES_AES_256_CFB, ossl_aes256cfb_functions),
303 ALG(PROV_NAMES_AES_192_CFB, ossl_aes192cfb_functions),
304 ALG(PROV_NAMES_AES_128_CFB, ossl_aes128cfb_functions),
305 ALG(PROV_NAMES_AES_256_CFB1, ossl_aes256cfb1_functions),
306 ALG(PROV_NAMES_AES_192_CFB1, ossl_aes192cfb1_functions),
307 ALG(PROV_NAMES_AES_128_CFB1, ossl_aes128cfb1_functions),
308 ALG(PROV_NAMES_AES_256_CFB8, ossl_aes256cfb8_functions),
309 ALG(PROV_NAMES_AES_192_CFB8, ossl_aes192cfb8_functions),
310 ALG(PROV_NAMES_AES_128_CFB8, ossl_aes128cfb8_functions),
311 ALG(PROV_NAMES_AES_256_CTR, ossl_aes256ctr_functions),
312 ALG(PROV_NAMES_AES_192_CTR, ossl_aes192ctr_functions),
313 ALG(PROV_NAMES_AES_128_CTR, ossl_aes128ctr_functions),
314 ALG(PROV_NAMES_AES_256_XTS, ossl_aes256xts_functions),
315 ALG(PROV_NAMES_AES_128_XTS, ossl_aes128xts_functions),
316 ALG(PROV_NAMES_AES_256_GCM, ossl_aes256gcm_functions),
317 ALG(PROV_NAMES_AES_192_GCM, ossl_aes192gcm_functions),
318 ALG(PROV_NAMES_AES_128_GCM, ossl_aes128gcm_functions),
319 ALG(PROV_NAMES_AES_256_CCM, ossl_aes256ccm_functions),
320 ALG(PROV_NAMES_AES_192_CCM, ossl_aes192ccm_functions),
321 ALG(PROV_NAMES_AES_128_CCM, ossl_aes128ccm_functions),
322 ALG(PROV_NAMES_AES_256_WRAP, ossl_aes256wrap_functions),
323 ALG(PROV_NAMES_AES_192_WRAP, ossl_aes192wrap_functions),
324 ALG(PROV_NAMES_AES_128_WRAP, ossl_aes128wrap_functions),
325 ALG(PROV_NAMES_AES_256_WRAP_PAD, ossl_aes256wrappad_functions),
326 ALG(PROV_NAMES_AES_192_WRAP_PAD, ossl_aes192wrappad_functions),
327 ALG(PROV_NAMES_AES_128_WRAP_PAD, ossl_aes128wrappad_functions),
328 ALG(PROV_NAMES_AES_256_WRAP_INV, ossl_aes256wrapinv_functions),
329 ALG(PROV_NAMES_AES_192_WRAP_INV, ossl_aes192wrapinv_functions),
330 ALG(PROV_NAMES_AES_128_WRAP_INV, ossl_aes128wrapinv_functions),
331 ALG(PROV_NAMES_AES_256_WRAP_PAD_INV, ossl_aes256wrappadinv_functions),
332 ALG(PROV_NAMES_AES_192_WRAP_PAD_INV, ossl_aes192wrappadinv_functions),
333 ALG(PROV_NAMES_AES_128_WRAP_PAD_INV, ossl_aes128wrappadinv_functions),
334 ALGC(PROV_NAMES_AES_128_CBC_HMAC_SHA1, ossl_aes128cbc_hmac_sha1_functions,
335 ossl_cipher_capable_aes_cbc_hmac_sha1),
336 ALGC(PROV_NAMES_AES_256_CBC_HMAC_SHA1, ossl_aes256cbc_hmac_sha1_functions,
337 ossl_cipher_capable_aes_cbc_hmac_sha1),
338 ALGC(PROV_NAMES_AES_128_CBC_HMAC_SHA256, ossl_aes128cbc_hmac_sha256_functions,
339 ossl_cipher_capable_aes_cbc_hmac_sha256),
340 ALGC(PROV_NAMES_AES_256_CBC_HMAC_SHA256, ossl_aes256cbc_hmac_sha256_functions,
341 ossl_cipher_capable_aes_cbc_hmac_sha256),
342 #ifndef OPENSSL_NO_DES
343 ALG(PROV_NAMES_DES_EDE3_ECB, ossl_tdes_ede3_ecb_functions),
344 ALG(PROV_NAMES_DES_EDE3_CBC, ossl_tdes_ede3_cbc_functions),
345 #endif /* OPENSSL_NO_DES */
346 { { NULL, NULL, NULL }, NULL }
347 };
348 static OSSL_ALGORITHM exported_fips_ciphers[OSSL_NELEM(fips_ciphers)];
349
350 static const OSSL_ALGORITHM fips_macs[] = {
351 #ifndef OPENSSL_NO_CMAC
352 { PROV_NAMES_CMAC, FIPS_DEFAULT_PROPERTIES, ossl_cmac_functions },
353 #endif
354 { PROV_NAMES_GMAC, FIPS_DEFAULT_PROPERTIES, ossl_gmac_functions },
355 { PROV_NAMES_HMAC, FIPS_DEFAULT_PROPERTIES, ossl_hmac_functions },
356 { PROV_NAMES_KMAC_128, FIPS_DEFAULT_PROPERTIES, ossl_kmac128_functions },
357 { PROV_NAMES_KMAC_256, FIPS_DEFAULT_PROPERTIES, ossl_kmac256_functions },
358 { NULL, NULL, NULL }
359 };
360
361 static const OSSL_ALGORITHM fips_macs_internal[] = {
362 #ifndef OPENSSL_NO_CMAC
363 { PROV_NAMES_CMAC, FIPS_DEFAULT_PROPERTIES, ossl_cmac_functions },
364 #endif
365 { PROV_NAMES_HMAC, FIPS_DEFAULT_PROPERTIES, ossl_hmac_internal_functions },
366 { PROV_NAMES_KMAC_128, FIPS_DEFAULT_PROPERTIES, ossl_kmac128_internal_functions },
367 { PROV_NAMES_KMAC_256, FIPS_DEFAULT_PROPERTIES, ossl_kmac256_internal_functions },
368 { NULL, NULL, NULL }
369 };
370
371 static const OSSL_ALGORITHM fips_kdfs[] = {
372 { PROV_NAMES_HKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_functions },
373 { PROV_NAMES_TLS1_3_KDF, FIPS_DEFAULT_PROPERTIES,
374 ossl_kdf_tls1_3_kdf_functions },
375 { PROV_NAMES_SSKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_sskdf_functions },
376 { PROV_NAMES_PBKDF2, FIPS_DEFAULT_PROPERTIES, ossl_kdf_pbkdf2_functions },
377 { PROV_NAMES_SSHKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_sshkdf_functions },
378 { PROV_NAMES_X963KDF, FIPS_DEFAULT_PROPERTIES,
379 ossl_kdf_x963_kdf_functions },
380 { PROV_NAMES_X942KDF_ASN1, FIPS_DEFAULT_PROPERTIES,
381 ossl_kdf_x942_kdf_functions },
382 { PROV_NAMES_TLS1_PRF, FIPS_DEFAULT_PROPERTIES,
383 ossl_kdf_tls1_prf_functions },
384 { PROV_NAMES_KBKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_kbkdf_functions },
385 { NULL, NULL, NULL }
386 };
387
388 static const OSSL_ALGORITHM fips_rands[] = {
389 { PROV_NAMES_CRNG_TEST, FIPS_UNAPPROVED_PROPERTIES, ossl_crng_test_functions },
390 { PROV_NAMES_CTR_DRBG, FIPS_DEFAULT_PROPERTIES, ossl_drbg_ctr_functions },
391 { PROV_NAMES_HASH_DRBG, FIPS_DEFAULT_PROPERTIES, ossl_drbg_hash_functions },
392 { PROV_NAMES_HMAC_DRBG, FIPS_DEFAULT_PROPERTIES, ossl_drbg_ossl_hmac_functions },
393 #ifndef OPENSSL_NO_FIPS_JITTER
394 { PROV_NAMES_JITTER, FIPS_DEFAULT_PROPERTIES, ossl_jitter_functions },
395 #endif
396 { PROV_NAMES_TEST_RAND, FIPS_UNAPPROVED_PROPERTIES, ossl_test_rng_functions },
397 { NULL, NULL, NULL }
398 };
399
400 static const OSSL_ALGORITHM fips_keyexch[] = {
401 #ifndef OPENSSL_NO_DH
402 { PROV_NAMES_DH, FIPS_DEFAULT_PROPERTIES, ossl_dh_keyexch_functions },
403 #endif
404 #ifndef OPENSSL_NO_EC
405 { PROV_NAMES_ECDH, FIPS_DEFAULT_PROPERTIES, ossl_ecdh_keyexch_functions },
406 # ifndef OPENSSL_NO_ECX
407 { PROV_NAMES_X25519, FIPS_UNAPPROVED_PROPERTIES, ossl_x25519_keyexch_functions },
408 { PROV_NAMES_X448, FIPS_UNAPPROVED_PROPERTIES, ossl_x448_keyexch_functions },
409 # endif
410 #endif
411 { PROV_NAMES_TLS1_PRF, FIPS_DEFAULT_PROPERTIES,
412 ossl_kdf_tls1_prf_keyexch_functions },
413 { PROV_NAMES_HKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_keyexch_functions },
414 { NULL, NULL, NULL }
415 };
416
417 static const OSSL_ALGORITHM fips_signature[] = {
418 #ifndef OPENSSL_NO_DSA
419 { PROV_NAMES_DSA, FIPS_DEFAULT_PROPERTIES, ossl_dsa_signature_functions },
420 { PROV_NAMES_DSA_SHA1, FIPS_DEFAULT_PROPERTIES, ossl_dsa_sha1_signature_functions },
421 { PROV_NAMES_DSA_SHA224, FIPS_DEFAULT_PROPERTIES, ossl_dsa_sha224_signature_functions },
422 { PROV_NAMES_DSA_SHA256, FIPS_DEFAULT_PROPERTIES, ossl_dsa_sha256_signature_functions },
423 { PROV_NAMES_DSA_SHA384, FIPS_DEFAULT_PROPERTIES, ossl_dsa_sha384_signature_functions },
424 { PROV_NAMES_DSA_SHA512, FIPS_DEFAULT_PROPERTIES, ossl_dsa_sha512_signature_functions },
425 { PROV_NAMES_DSA_SHA3_224, FIPS_DEFAULT_PROPERTIES, ossl_dsa_sha3_224_signature_functions },
426 { PROV_NAMES_DSA_SHA3_256, FIPS_DEFAULT_PROPERTIES, ossl_dsa_sha3_256_signature_functions },
427 { PROV_NAMES_DSA_SHA3_384, FIPS_DEFAULT_PROPERTIES, ossl_dsa_sha3_384_signature_functions },
428 { PROV_NAMES_DSA_SHA3_512, FIPS_DEFAULT_PROPERTIES, ossl_dsa_sha3_512_signature_functions },
429 #endif
430 { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_signature_functions },
431 { PROV_NAMES_RSA_SHA1, FIPS_DEFAULT_PROPERTIES,
432 ossl_rsa_sha1_signature_functions },
433 { PROV_NAMES_RSA_SHA224, FIPS_DEFAULT_PROPERTIES,
434 ossl_rsa_sha224_signature_functions },
435 { PROV_NAMES_RSA_SHA256, FIPS_DEFAULT_PROPERTIES,
436 ossl_rsa_sha256_signature_functions },
437 { PROV_NAMES_RSA_SHA384, FIPS_DEFAULT_PROPERTIES,
438 ossl_rsa_sha384_signature_functions },
439 { PROV_NAMES_RSA_SHA512, FIPS_DEFAULT_PROPERTIES,
440 ossl_rsa_sha512_signature_functions },
441 { PROV_NAMES_RSA_SHA512_224, FIPS_DEFAULT_PROPERTIES,
442 ossl_rsa_sha512_224_signature_functions },
443 { PROV_NAMES_RSA_SHA512_256, FIPS_DEFAULT_PROPERTIES,
444 ossl_rsa_sha512_256_signature_functions },
445 { PROV_NAMES_RSA_SHA3_224, FIPS_DEFAULT_PROPERTIES,
446 ossl_rsa_sha3_224_signature_functions },
447 { PROV_NAMES_RSA_SHA3_256, FIPS_DEFAULT_PROPERTIES,
448 ossl_rsa_sha3_256_signature_functions },
449 { PROV_NAMES_RSA_SHA3_384, FIPS_DEFAULT_PROPERTIES,
450 ossl_rsa_sha3_384_signature_functions },
451 { PROV_NAMES_RSA_SHA3_512, FIPS_DEFAULT_PROPERTIES,
452 ossl_rsa_sha3_512_signature_functions },
453 #ifndef OPENSSL_NO_EC
454 # ifndef OPENSSL_NO_ECX
455 { PROV_NAMES_ED25519, FIPS_DEFAULT_PROPERTIES,
456 ossl_ed25519_signature_functions },
457 { PROV_NAMES_ED25519ph, FIPS_DEFAULT_PROPERTIES,
458 ossl_ed25519ph_signature_functions },
459 { PROV_NAMES_ED25519ctx, FIPS_DEFAULT_PROPERTIES,
460 ossl_ed25519ctx_signature_functions },
461 { PROV_NAMES_ED448, FIPS_DEFAULT_PROPERTIES,
462 ossl_ed448_signature_functions },
463 { PROV_NAMES_ED448ph, FIPS_DEFAULT_PROPERTIES,
464 ossl_ed448ph_signature_functions },
465 # endif
466 { PROV_NAMES_ECDSA, FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_signature_functions },
467 { PROV_NAMES_ECDSA_SHA1, FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_sha1_signature_functions },
468 { PROV_NAMES_ECDSA_SHA224, FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_sha224_signature_functions },
469 { PROV_NAMES_ECDSA_SHA256, FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_sha256_signature_functions },
470 { PROV_NAMES_ECDSA_SHA384, FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_sha384_signature_functions },
471 { PROV_NAMES_ECDSA_SHA512, FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_sha512_signature_functions },
472 { PROV_NAMES_ECDSA_SHA3_224, FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_sha3_224_signature_functions },
473 { PROV_NAMES_ECDSA_SHA3_256, FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_sha3_256_signature_functions },
474 { PROV_NAMES_ECDSA_SHA3_384, FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_sha3_384_signature_functions },
475 { PROV_NAMES_ECDSA_SHA3_512, FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_sha3_512_signature_functions },
476 #endif
477 { PROV_NAMES_HMAC, FIPS_DEFAULT_PROPERTIES,
478 ossl_mac_legacy_hmac_signature_functions },
479 #ifndef OPENSSL_NO_CMAC
480 { PROV_NAMES_CMAC, FIPS_DEFAULT_PROPERTIES,
481 ossl_mac_legacy_cmac_signature_functions },
482 #endif
483 { NULL, NULL, NULL }
484 };
485
486 static const OSSL_ALGORITHM fips_asym_cipher[] = {
487 { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_asym_cipher_functions },
488 { NULL, NULL, NULL }
489 };
490
491 static const OSSL_ALGORITHM fips_asym_kem[] = {
492 { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_asym_kem_functions },
493 { NULL, NULL, NULL }
494 };
495
496 static const OSSL_ALGORITHM fips_keymgmt[] = {
497 #ifndef OPENSSL_NO_DH
498 { PROV_NAMES_DH, FIPS_DEFAULT_PROPERTIES, ossl_dh_keymgmt_functions,
499 PROV_DESCS_DH },
500 { PROV_NAMES_DHX, FIPS_DEFAULT_PROPERTIES, ossl_dhx_keymgmt_functions,
501 PROV_DESCS_DHX },
502 #endif
503 #ifndef OPENSSL_NO_DSA
504 { PROV_NAMES_DSA, FIPS_DEFAULT_PROPERTIES, ossl_dsa_keymgmt_functions,
505 PROV_DESCS_DSA },
506 #endif
507 { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_keymgmt_functions,
508 PROV_DESCS_RSA },
509 { PROV_NAMES_RSA_PSS, FIPS_DEFAULT_PROPERTIES,
510 ossl_rsapss_keymgmt_functions, PROV_DESCS_RSA_PSS },
511 #ifndef OPENSSL_NO_EC
512 { PROV_NAMES_EC, FIPS_DEFAULT_PROPERTIES, ossl_ec_keymgmt_functions,
513 PROV_DESCS_EC },
514 # ifndef OPENSSL_NO_ECX
515 { PROV_NAMES_X25519, FIPS_UNAPPROVED_PROPERTIES, ossl_x25519_keymgmt_functions,
516 PROV_DESCS_X25519 },
517 { PROV_NAMES_X448, FIPS_UNAPPROVED_PROPERTIES, ossl_x448_keymgmt_functions,
518 PROV_DESCS_X448 },
519 { PROV_NAMES_ED25519, FIPS_DEFAULT_PROPERTIES, ossl_ed25519_keymgmt_functions,
520 PROV_DESCS_ED25519 },
521 { PROV_NAMES_ED448, FIPS_DEFAULT_PROPERTIES, ossl_ed448_keymgmt_functions,
522 PROV_DESCS_ED448 },
523 # endif
524 #endif
525 { PROV_NAMES_TLS1_PRF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
526 PROV_DESCS_TLS1_PRF_SIGN },
527 { PROV_NAMES_HKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
528 PROV_DESCS_HKDF_SIGN },
529 { PROV_NAMES_HMAC, FIPS_DEFAULT_PROPERTIES, ossl_mac_legacy_keymgmt_functions,
530 PROV_DESCS_HMAC_SIGN },
531 #ifndef OPENSSL_NO_CMAC
532 { PROV_NAMES_CMAC, FIPS_DEFAULT_PROPERTIES,
533 ossl_cmac_legacy_keymgmt_functions, PROV_DESCS_CMAC_SIGN },
534 #endif
535 { NULL, NULL, NULL }
536 };
537
fips_query(void * provctx,int operation_id,int * no_cache)538 static const OSSL_ALGORITHM *fips_query(void *provctx, int operation_id,
539 int *no_cache)
540 {
541 *no_cache = 0;
542
543 if (!ossl_prov_is_running())
544 return NULL;
545
546 switch (operation_id) {
547 case OSSL_OP_DIGEST:
548 return fips_digests;
549 case OSSL_OP_CIPHER:
550 return exported_fips_ciphers;
551 case OSSL_OP_MAC:
552 return fips_macs;
553 case OSSL_OP_KDF:
554 return fips_kdfs;
555 case OSSL_OP_RAND:
556 return fips_rands;
557 case OSSL_OP_KEYMGMT:
558 return fips_keymgmt;
559 case OSSL_OP_KEYEXCH:
560 return fips_keyexch;
561 case OSSL_OP_SIGNATURE:
562 return fips_signature;
563 case OSSL_OP_ASYM_CIPHER:
564 return fips_asym_cipher;
565 case OSSL_OP_KEM:
566 return fips_asym_kem;
567 }
568 return NULL;
569 }
570
fips_query_internal(void * provctx,int operation_id,int * no_cache)571 static const OSSL_ALGORITHM *fips_query_internal(void *provctx, int operation_id,
572 int *no_cache)
573 {
574 if (operation_id == OSSL_OP_MAC) {
575 *no_cache = 0;
576 if (!ossl_prov_is_running())
577 return NULL;
578 return fips_macs_internal;
579 }
580 return fips_query(provctx, operation_id, no_cache);
581 }
582
fips_teardown(void * provctx)583 static void fips_teardown(void *provctx)
584 {
585 OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
586 ossl_prov_ctx_free(provctx);
587 }
588
fips_intern_teardown(void * provctx)589 static void fips_intern_teardown(void *provctx)
590 {
591 /*
592 * We know that the library context is the same as for the outer provider,
593 * so no need to destroy it here.
594 */
595 ossl_prov_ctx_free(provctx);
596 }
597
598 /* Functions we provide to the core */
599 static const OSSL_DISPATCH fips_dispatch_table[] = {
600 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_teardown },
601 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params },
602 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
603 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
604 { OSSL_FUNC_PROVIDER_GET_CAPABILITIES,
605 (void (*)(void))ossl_prov_get_capabilities },
606 { OSSL_FUNC_PROVIDER_SELF_TEST, (void (*)(void))fips_self_test },
607 OSSL_DISPATCH_END
608 };
609
610 /* Functions we provide to ourself */
611 static const OSSL_DISPATCH intern_dispatch_table[] = {
612 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_intern_teardown },
613 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query_internal },
614 OSSL_DISPATCH_END
615 };
616
617 /*
618 * On VMS, the provider init function name is expected to be uppercase,
619 * see the pragmas in <openssl/core.h>. Let's do the same with this
620 * internal name. This is how symbol names are treated by default
621 * by the compiler if nothing else is said, but since this is part
622 * of libfips, and we build our libraries with mixed case symbol names,
623 * we must switch back to this default explicitly here.
624 */
625 #ifdef __VMS
626 # pragma names save
627 # pragma names uppercase,truncated
628 #endif
629 OSSL_provider_init_fn OSSL_provider_init_int;
630 #ifdef __VMS
631 # pragma names restore
632 #endif
OSSL_provider_init_int(const OSSL_CORE_HANDLE * handle,const OSSL_DISPATCH * in,const OSSL_DISPATCH ** out,void ** provctx)633 int OSSL_provider_init_int(const OSSL_CORE_HANDLE *handle,
634 const OSSL_DISPATCH *in,
635 const OSSL_DISPATCH **out,
636 void **provctx)
637 {
638 FIPS_GLOBAL *fgbl;
639 OSSL_LIB_CTX *libctx = NULL;
640 SELF_TEST_POST_PARAMS selftest_params;
641
642 memset(&selftest_params, 0, sizeof(selftest_params));
643
644 if (!ossl_prov_seeding_from_dispatch(in))
645 goto err;
646 for (; in->function_id != 0; in++) {
647 /*
648 * We do not support the scenario of an application linked against
649 * multiple versions of libcrypto (e.g. one static and one dynamic), but
650 * sharing a single fips.so. We do a simple sanity check here.
651 */
652 #define set_func(c, f) if (c == NULL) c = f; else if (c != f) return 0;
653 switch (in->function_id) {
654 case OSSL_FUNC_CORE_GET_LIBCTX:
655 set_func(c_get_libctx, OSSL_FUNC_core_get_libctx(in));
656 break;
657 case OSSL_FUNC_CORE_GETTABLE_PARAMS:
658 set_func(c_gettable_params, OSSL_FUNC_core_gettable_params(in));
659 break;
660 case OSSL_FUNC_CORE_GET_PARAMS:
661 set_func(c_get_params, OSSL_FUNC_core_get_params(in));
662 break;
663 case OSSL_FUNC_CORE_THREAD_START:
664 set_func(c_thread_start, OSSL_FUNC_core_thread_start(in));
665 break;
666 case OSSL_FUNC_CORE_NEW_ERROR:
667 set_func(c_new_error, OSSL_FUNC_core_new_error(in));
668 break;
669 case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
670 set_func(c_set_error_debug, OSSL_FUNC_core_set_error_debug(in));
671 break;
672 case OSSL_FUNC_CORE_VSET_ERROR:
673 set_func(c_vset_error, OSSL_FUNC_core_vset_error(in));
674 break;
675 case OSSL_FUNC_CORE_SET_ERROR_MARK:
676 set_func(c_set_error_mark, OSSL_FUNC_core_set_error_mark(in));
677 break;
678 case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
679 set_func(c_clear_last_error_mark,
680 OSSL_FUNC_core_clear_last_error_mark(in));
681 break;
682 case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
683 set_func(c_pop_error_to_mark, OSSL_FUNC_core_pop_error_to_mark(in));
684 break;
685 case OSSL_FUNC_CRYPTO_MALLOC:
686 set_func(c_CRYPTO_malloc, OSSL_FUNC_CRYPTO_malloc(in));
687 break;
688 case OSSL_FUNC_CRYPTO_ZALLOC:
689 set_func(c_CRYPTO_zalloc, OSSL_FUNC_CRYPTO_zalloc(in));
690 break;
691 case OSSL_FUNC_CRYPTO_FREE:
692 set_func(c_CRYPTO_free, OSSL_FUNC_CRYPTO_free(in));
693 break;
694 case OSSL_FUNC_CRYPTO_CLEAR_FREE:
695 set_func(c_CRYPTO_clear_free, OSSL_FUNC_CRYPTO_clear_free(in));
696 break;
697 case OSSL_FUNC_CRYPTO_REALLOC:
698 set_func(c_CRYPTO_realloc, OSSL_FUNC_CRYPTO_realloc(in));
699 break;
700 case OSSL_FUNC_CRYPTO_CLEAR_REALLOC:
701 set_func(c_CRYPTO_clear_realloc,
702 OSSL_FUNC_CRYPTO_clear_realloc(in));
703 break;
704 case OSSL_FUNC_CRYPTO_SECURE_MALLOC:
705 set_func(c_CRYPTO_secure_malloc,
706 OSSL_FUNC_CRYPTO_secure_malloc(in));
707 break;
708 case OSSL_FUNC_CRYPTO_SECURE_ZALLOC:
709 set_func(c_CRYPTO_secure_zalloc,
710 OSSL_FUNC_CRYPTO_secure_zalloc(in));
711 break;
712 case OSSL_FUNC_CRYPTO_SECURE_FREE:
713 set_func(c_CRYPTO_secure_free,
714 OSSL_FUNC_CRYPTO_secure_free(in));
715 break;
716 case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE:
717 set_func(c_CRYPTO_secure_clear_free,
718 OSSL_FUNC_CRYPTO_secure_clear_free(in));
719 break;
720 case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
721 set_func(c_CRYPTO_secure_allocated,
722 OSSL_FUNC_CRYPTO_secure_allocated(in));
723 break;
724 case OSSL_FUNC_BIO_NEW_FILE:
725 set_func(selftest_params.bio_new_file_cb,
726 OSSL_FUNC_BIO_new_file(in));
727 break;
728 case OSSL_FUNC_BIO_NEW_MEMBUF:
729 set_func(selftest_params.bio_new_buffer_cb,
730 OSSL_FUNC_BIO_new_membuf(in));
731 break;
732 case OSSL_FUNC_BIO_READ_EX:
733 set_func(selftest_params.bio_read_ex_cb,
734 OSSL_FUNC_BIO_read_ex(in));
735 break;
736 case OSSL_FUNC_BIO_FREE:
737 set_func(selftest_params.bio_free_cb, OSSL_FUNC_BIO_free(in));
738 break;
739 case OSSL_FUNC_BIO_VSNPRINTF:
740 set_func(c_BIO_vsnprintf, OSSL_FUNC_BIO_vsnprintf(in));
741 break;
742 case OSSL_FUNC_SELF_TEST_CB:
743 set_func(c_stcbfn, OSSL_FUNC_self_test_cb(in));
744 break;
745 case OSSL_FUNC_INDICATOR_CB:
746 set_func(c_indcbfn, OSSL_FUNC_indicator_cb(in));
747 break;
748 default:
749 /* Just ignore anything we don't understand */
750 break;
751 }
752 }
753
754 OPENSSL_cpuid_setup();
755
756 /* Create a context. */
757 if ((*provctx = ossl_prov_ctx_new()) == NULL
758 || (libctx = OSSL_LIB_CTX_new()) == NULL)
759 goto err;
760
761 if ((fgbl = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_FIPS_PROV_INDEX)) == NULL)
762 goto err;
763
764 fgbl->handle = handle;
765
766 /*
767 * We need to register this thread to receive thread lifecycle callbacks.
768 * This wouldn't matter if the current thread is also the same thread that
769 * closes the FIPS provider down. But if that happens on a different thread
770 * then memory leaks could otherwise occur.
771 */
772 if (!ossl_thread_register_fips(libctx))
773 goto err;
774
775 /*
776 * We did initial set up of selftest_params in a local copy, because we
777 * could not create fgbl until c_CRYPTO_zalloc was defined in the loop
778 * above.
779 */
780 fgbl->selftest_params = selftest_params;
781
782 fgbl->selftest_params.libctx = libctx;
783
784 set_self_test_cb(fgbl);
785
786 if (!fips_get_params_from_core(fgbl)) {
787 /* Error already raised */
788 goto err;
789 }
790 /*
791 * Disable the conditional error check if it's disabled in the fips config
792 * file.
793 */
794 if (fgbl->selftest_params.conditional_error_check != NULL
795 && strcmp(fgbl->selftest_params.conditional_error_check, "0") == 0)
796 SELF_TEST_disable_conditional_error_state();
797
798 /* Enable or disable FIPS provider options */
799 #define OSSL_FIPS_PARAM(structname, paramname, unused) \
800 if (fgbl->fips_##structname.option != NULL) { \
801 if (strcmp(fgbl->fips_##structname.option, "1") == 0) \
802 fgbl->fips_##structname.enabled = 1; \
803 else if (strcmp(fgbl->fips_##structname.option, "0") == 0) \
804 fgbl->fips_##structname.enabled = 0; \
805 else \
806 goto err; \
807 }
808 #include "fips_indicator_params.inc"
809 #undef OSSL_FIPS_PARAM
810
811 ossl_prov_cache_exported_algorithms(fips_ciphers, exported_fips_ciphers);
812
813 if (!SELF_TEST_post(&fgbl->selftest_params, 0)) {
814 ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_POST_FAILURE);
815 goto err;
816 }
817
818 ossl_prov_ctx_set0_libctx(*provctx, libctx);
819 ossl_prov_ctx_set0_handle(*provctx, handle);
820
821 *out = fips_dispatch_table;
822 return 1;
823 err:
824 fips_teardown(*provctx);
825 OSSL_LIB_CTX_free(libctx);
826 *provctx = NULL;
827 return 0;
828 }
829
830 /*
831 * The internal init function used when the FIPS module uses EVP to call
832 * another algorithm also in the FIPS module. This is a recursive call that has
833 * been made from within the FIPS module itself. To make this work, we populate
834 * the provider context of this inner instance with the same library context
835 * that was used in the EVP call that initiated this recursive call.
836 */
837 OSSL_provider_init_fn ossl_fips_intern_provider_init;
ossl_fips_intern_provider_init(const OSSL_CORE_HANDLE * handle,const OSSL_DISPATCH * in,const OSSL_DISPATCH ** out,void ** provctx)838 int ossl_fips_intern_provider_init(const OSSL_CORE_HANDLE *handle,
839 const OSSL_DISPATCH *in,
840 const OSSL_DISPATCH **out,
841 void **provctx)
842 {
843 OSSL_FUNC_core_get_libctx_fn *c_internal_get_libctx = NULL;
844
845 for (; in->function_id != 0; in++) {
846 switch (in->function_id) {
847 case OSSL_FUNC_CORE_GET_LIBCTX:
848 c_internal_get_libctx = OSSL_FUNC_core_get_libctx(in);
849 break;
850 default:
851 break;
852 }
853 }
854
855 if (c_internal_get_libctx == NULL)
856 return 0;
857
858 if ((*provctx = ossl_prov_ctx_new()) == NULL)
859 return 0;
860
861 /*
862 * Using the parent library context only works because we are a built-in
863 * internal provider. This is not something that most providers would be
864 * able to do.
865 */
866 ossl_prov_ctx_set0_libctx(*provctx,
867 (OSSL_LIB_CTX *)c_internal_get_libctx(handle));
868 ossl_prov_ctx_set0_handle(*provctx, handle);
869
870 *out = intern_dispatch_table;
871 return 1;
872 }
873
ERR_new(void)874 void ERR_new(void)
875 {
876 c_new_error(NULL);
877 }
878
ERR_set_debug(const char * file,int line,const char * func)879 void ERR_set_debug(const char *file, int line, const char *func)
880 {
881 c_set_error_debug(NULL, file, line, func);
882 }
883
ERR_set_error(int lib,int reason,const char * fmt,...)884 void ERR_set_error(int lib, int reason, const char *fmt, ...)
885 {
886 va_list args;
887
888 va_start(args, fmt);
889 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
890 va_end(args);
891 }
892
ERR_vset_error(int lib,int reason,const char * fmt,va_list args)893 void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
894 {
895 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
896 }
897
ERR_set_mark(void)898 int ERR_set_mark(void)
899 {
900 return c_set_error_mark(NULL);
901 }
902
ERR_clear_last_mark(void)903 int ERR_clear_last_mark(void)
904 {
905 return c_clear_last_error_mark(NULL);
906 }
907
ERR_pop_to_mark(void)908 int ERR_pop_to_mark(void)
909 {
910 return c_pop_error_to_mark(NULL);
911 }
912
913 /*
914 * This must take a library context, since it's called from the depths
915 * of crypto/initthread.c code, where it's (correctly) assumed that the
916 * passed caller argument is an OSSL_LIB_CTX pointer (since the same routine
917 * is also called from other parts of libcrypto, which all pass around a
918 * OSSL_LIB_CTX pointer)
919 */
FIPS_get_core_handle(OSSL_LIB_CTX * libctx)920 const OSSL_CORE_HANDLE *FIPS_get_core_handle(OSSL_LIB_CTX *libctx)
921 {
922 FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(libctx,
923 OSSL_LIB_CTX_FIPS_PROV_INDEX);
924
925 if (fgbl == NULL)
926 return NULL;
927
928 return fgbl->handle;
929 }
930
CRYPTO_malloc(size_t num,const char * file,int line)931 void *CRYPTO_malloc(size_t num, const char *file, int line)
932 {
933 return c_CRYPTO_malloc(num, file, line);
934 }
935
CRYPTO_zalloc(size_t num,const char * file,int line)936 void *CRYPTO_zalloc(size_t num, const char *file, int line)
937 {
938 return c_CRYPTO_zalloc(num, file, line);
939 }
940
CRYPTO_free(void * ptr,const char * file,int line)941 void CRYPTO_free(void *ptr, const char *file, int line)
942 {
943 c_CRYPTO_free(ptr, file, line);
944 }
945
CRYPTO_clear_free(void * ptr,size_t num,const char * file,int line)946 void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line)
947 {
948 c_CRYPTO_clear_free(ptr, num, file, line);
949 }
950
CRYPTO_realloc(void * addr,size_t num,const char * file,int line)951 void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
952 {
953 return c_CRYPTO_realloc(addr, num, file, line);
954 }
955
CRYPTO_clear_realloc(void * addr,size_t old_num,size_t num,const char * file,int line)956 void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
957 const char *file, int line)
958 {
959 return c_CRYPTO_clear_realloc(addr, old_num, num, file, line);
960 }
961
CRYPTO_secure_malloc(size_t num,const char * file,int line)962 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
963 {
964 return c_CRYPTO_secure_malloc(num, file, line);
965 }
966
CRYPTO_secure_zalloc(size_t num,const char * file,int line)967 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
968 {
969 return c_CRYPTO_secure_zalloc(num, file, line);
970 }
971
CRYPTO_secure_free(void * ptr,const char * file,int line)972 void CRYPTO_secure_free(void *ptr, const char *file, int line)
973 {
974 c_CRYPTO_secure_free(ptr, file, line);
975 }
976
CRYPTO_secure_clear_free(void * ptr,size_t num,const char * file,int line)977 void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *file, int line)
978 {
979 c_CRYPTO_secure_clear_free(ptr, num, file, line);
980 }
981
CRYPTO_secure_allocated(const void * ptr)982 int CRYPTO_secure_allocated(const void *ptr)
983 {
984 return c_CRYPTO_secure_allocated(ptr);
985 }
986
CRYPTO_aligned_alloc(size_t num,size_t align,void ** freeptr,const char * file,int line)987 void *CRYPTO_aligned_alloc(size_t num, size_t align, void **freeptr,
988 const char *file, int line)
989 {
990 return NULL;
991 }
992
BIO_snprintf(char * buf,size_t n,const char * format,...)993 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
994 {
995 va_list args;
996 int ret;
997
998 va_start(args, format);
999 ret = c_BIO_vsnprintf(buf, n, format, args);
1000 va_end(args);
1001 return ret;
1002 }
1003
1004 #define OSSL_FIPS_PARAM(structname, paramname, unused) \
1005 int ossl_fips_config_##structname(OSSL_LIB_CTX *libctx) \
1006 { \
1007 FIPS_GLOBAL *fgbl = \
1008 ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_FIPS_PROV_INDEX); \
1009 \
1010 return fgbl->fips_##structname.enabled; \
1011 }
1012 #include "fips_indicator_params.inc"
1013 #undef OSSL_FIPS_PARAM
1014
OSSL_SELF_TEST_get_callback(OSSL_LIB_CTX * libctx,OSSL_CALLBACK ** cb,void ** cbarg)1015 void OSSL_SELF_TEST_get_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK **cb,
1016 void **cbarg)
1017 {
1018 assert(libctx != NULL);
1019
1020 if (c_stcbfn != NULL && c_get_libctx != NULL) {
1021 /* Get the parent libctx */
1022 c_stcbfn(c_get_libctx(FIPS_get_core_handle(libctx)), cb, cbarg);
1023 } else {
1024 if (cb != NULL)
1025 *cb = NULL;
1026 if (cbarg != NULL)
1027 *cbarg = NULL;
1028 }
1029 }
1030
OSSL_INDICATOR_get_callback(OSSL_LIB_CTX * libctx,OSSL_INDICATOR_CALLBACK ** cb)1031 void OSSL_INDICATOR_get_callback(OSSL_LIB_CTX *libctx,
1032 OSSL_INDICATOR_CALLBACK **cb)
1033 {
1034 assert(libctx != NULL);
1035
1036 if (c_indcbfn != NULL && c_get_libctx != NULL) {
1037 /* Get the parent libctx */
1038 c_indcbfn(c_get_libctx(FIPS_get_core_handle(libctx)), cb);
1039 } else {
1040 if (cb != NULL)
1041 *cb = NULL;
1042 }
1043 }
1044