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