VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.0/providers/fips/fipsprov.c@ 100111

Last change on this file since 100111 was 99366, checked in by vboxsync, 21 months ago

openssl-3.1.0: Applied and adjusted our OpenSSL changes to 3.0.7. bugref:10418

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette