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