]>
Commit | Line | Data |
---|---|---|
95214b43 | 1 | /* |
8020d79b | 2 | * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. |
95214b43 SL |
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 | ||
95214b43 SL |
10 | #include <openssl/evp.h> |
11 | #include <openssl/err.h> | |
12 | #include <openssl/provider.h> | |
13 | #include <openssl/params.h> | |
14 | #include <openssl/fips_names.h> | |
36fc5fc6 SL |
15 | #include <openssl/core_names.h> |
16 | #include <openssl/self_test.h> | |
31214258 | 17 | #include <openssl/fipskey.h> |
95214b43 SL |
18 | #include "apps.h" |
19 | #include "progs.h" | |
20 | ||
21 | #define BUFSIZE 4096 | |
95214b43 SL |
22 | |
23 | /* Configuration file values */ | |
24 | #define VERSION_KEY "version" | |
25 | #define VERSION_VAL "1" | |
26 | #define INSTALL_STATUS_VAL "INSTALL_SELF_TEST_KATS_RUN" | |
27 | ||
36fc5fc6 SL |
28 | static OSSL_CALLBACK self_test_events; |
29 | static char *self_test_corrupt_desc = NULL; | |
30 | static char *self_test_corrupt_type = NULL; | |
31 | static int self_test_log = 1; | |
1cd2c1f8 | 32 | static int quiet = 0; |
36fc5fc6 | 33 | |
95214b43 | 34 | typedef enum OPTION_choice { |
b0f96018 | 35 | OPT_COMMON, |
95214b43 | 36 | OPT_IN, OPT_OUT, OPT_MODULE, |
36fc5fc6 | 37 | OPT_PROV_NAME, OPT_SECTION_NAME, OPT_MAC_NAME, OPT_MACOPT, OPT_VERIFY, |
35e6ea3b | 38 | OPT_NO_LOG, OPT_CORRUPT_DESC, OPT_CORRUPT_TYPE, OPT_QUIET, OPT_CONFIG, |
991a6bb5 | 39 | OPT_NO_CONDITIONAL_ERRORS, |
2abffec0 | 40 | OPT_NO_SECURITY_CHECKS, |
50ea5cdc | 41 | OPT_TLS_PRF_EMS_CHECK, |
7057dddb | 42 | OPT_SELF_TEST_ONLOAD, OPT_SELF_TEST_ONINSTALL |
95214b43 SL |
43 | } OPTION_CHOICE; |
44 | ||
45 | const OPTIONS fipsinstall_options[] = { | |
5388f986 | 46 | OPT_SECTION("General"), |
95214b43 | 47 | {"help", OPT_HELP, '-', "Display this summary"}, |
92de469f RS |
48 | {"verify", OPT_VERIFY, '-', |
49 | "Verify a config file instead of generating one"}, | |
95214b43 SL |
50 | {"module", OPT_MODULE, '<', "File name of the provider module"}, |
51 | {"provider_name", OPT_PROV_NAME, 's', "FIPS provider name"}, | |
52 | {"section_name", OPT_SECTION_NAME, 's', | |
53 | "FIPS Provider config section name (optional)"}, | |
50ea5cdc | 54 | {"no_conditional_errors", OPT_NO_CONDITIONAL_ERRORS, '-', |
55 | "Disable the ability of the fips module to enter an error state if" | |
56 | " any conditional self tests fail"}, | |
991a6bb5 SL |
57 | {"no_security_checks", OPT_NO_SECURITY_CHECKS, '-', |
58 | "Disable the run-time FIPS security checks in the module"}, | |
2abffec0 SL |
59 | {"self_test_onload", OPT_SELF_TEST_ONLOAD, '-', |
60 | "Forces self tests to always run on module load"}, | |
7057dddb P |
61 | {"self_test_oninstall", OPT_SELF_TEST_ONINSTALL, '-', |
62 | "Forces self tests to run once on module installation"}, | |
50ea5cdc | 63 | {"ems_check", OPT_TLS_PRF_EMS_CHECK, '-', |
64 | "Enable the run-time FIPS check for EMS during TLS1_PRF"}, | |
5388f986 RS |
65 | OPT_SECTION("Input"), |
66 | {"in", OPT_IN, '<', "Input config file, used when verifying"}, | |
67 | ||
68 | OPT_SECTION("Output"), | |
69 | {"out", OPT_OUT, '>', "Output config file, used when generating"}, | |
95214b43 SL |
70 | {"mac_name", OPT_MAC_NAME, 's', "MAC name"}, |
71 | {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form. " | |
72 | "See 'PARAMETER NAMES' in the EVP_MAC_ docs"}, | |
36fc5fc6 SL |
73 | {"noout", OPT_NO_LOG, '-', "Disable logging of self test events"}, |
74 | {"corrupt_desc", OPT_CORRUPT_DESC, 's', "Corrupt a self test by description"}, | |
75 | {"corrupt_type", OPT_CORRUPT_TYPE, 's', "Corrupt a self test by type"}, | |
9f7bdcf3 | 76 | {"config", OPT_CONFIG, '<', "The parent config to verify"}, |
1cd2c1f8 | 77 | {"quiet", OPT_QUIET, '-', "No messages, just exit status"}, |
95214b43 SL |
78 | {NULL} |
79 | }; | |
80 | ||
81 | static int do_mac(EVP_MAC_CTX *ctx, unsigned char *tmp, BIO *in, | |
82 | unsigned char *out, size_t *out_len) | |
83 | { | |
84 | int ret = 0; | |
85 | int i; | |
86 | size_t outsz = *out_len; | |
87 | ||
ebf8274c | 88 | if (!EVP_MAC_init(ctx, NULL, 0, NULL)) |
95214b43 | 89 | goto err; |
90a2576b | 90 | if (EVP_MAC_CTX_get_mac_size(ctx) > outsz) |
95214b43 SL |
91 | goto end; |
92 | while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) { | |
93 | if (i < 0 || !EVP_MAC_update(ctx, tmp, i)) | |
94 | goto err; | |
95 | } | |
96 | end: | |
97 | if (!EVP_MAC_final(ctx, out, out_len, outsz)) | |
98 | goto err; | |
99 | ret = 1; | |
100 | err: | |
101 | return ret; | |
102 | } | |
103 | ||
104 | static int load_fips_prov_and_run_self_test(const char *prov_name) | |
105 | { | |
106 | int ret = 0; | |
107 | OSSL_PROVIDER *prov = NULL; | |
7057dddb P |
108 | OSSL_PARAM params[4], *p = params; |
109 | char *name = "", *vers = "", *build = ""; | |
95214b43 SL |
110 | |
111 | prov = OSSL_PROVIDER_load(NULL, prov_name); | |
112 | if (prov == NULL) { | |
113 | BIO_printf(bio_err, "Failed to load FIPS module\n"); | |
114 | goto end; | |
115 | } | |
7057dddb P |
116 | if (!quiet) { |
117 | *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_NAME, | |
118 | &name, sizeof(name)); | |
119 | *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION, | |
120 | &vers, sizeof(vers)); | |
121 | *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_BUILDINFO, | |
122 | &build, sizeof(build)); | |
123 | *p = OSSL_PARAM_construct_end(); | |
124 | if (!OSSL_PROVIDER_get_params(prov, params)) { | |
125 | BIO_printf(bio_err, "Failed to query FIPS module parameters\n"); | |
126 | goto end; | |
127 | } | |
128 | if (OSSL_PARAM_modified(params)) | |
129 | BIO_printf(bio_err, "\t%-10s\t%s\n", "name:", name); | |
130 | if (OSSL_PARAM_modified(params + 1)) | |
131 | BIO_printf(bio_err, "\t%-10s\t%s\n", "version:", vers); | |
132 | if (OSSL_PARAM_modified(params + 2)) | |
133 | BIO_printf(bio_err, "\t%-10s\t%s\n", "build:", build); | |
134 | } | |
95214b43 SL |
135 | ret = 1; |
136 | end: | |
137 | OSSL_PROVIDER_unload(prov); | |
138 | return ret; | |
139 | } | |
140 | ||
141 | static int print_mac(BIO *bio, const char *label, const unsigned char *mac, | |
142 | size_t len) | |
143 | { | |
144 | int ret; | |
145 | char *hexstr = NULL; | |
146 | ||
147 | hexstr = OPENSSL_buf2hexstr(mac, (long)len); | |
148 | if (hexstr == NULL) | |
149 | return 0; | |
150 | ret = BIO_printf(bio, "%s = %s\n", label, hexstr); | |
151 | OPENSSL_free(hexstr); | |
152 | return ret; | |
153 | } | |
154 | ||
155 | static int write_config_header(BIO *out, const char *prov_name, | |
156 | const char *section) | |
157 | { | |
158 | return BIO_printf(out, "openssl_conf = openssl_init\n\n") | |
159 | && BIO_printf(out, "[openssl_init]\n") | |
160 | && BIO_printf(out, "providers = provider_section\n\n") | |
161 | && BIO_printf(out, "[provider_section]\n") | |
162 | && BIO_printf(out, "%s = %s\n\n", prov_name, section); | |
163 | } | |
164 | ||
165 | /* | |
166 | * Outputs a fips related config file that contains entries for the fips | |
991a6bb5 SL |
167 | * module checksum, installation indicator checksum and the options |
168 | * conditional_errors and security_checks. | |
95214b43 SL |
169 | * |
170 | * Returns 1 if the config file is written otherwise it returns 0 on error. | |
171 | */ | |
172 | static int write_config_fips_section(BIO *out, const char *section, | |
173 | unsigned char *module_mac, | |
174 | size_t module_mac_len, | |
35e6ea3b | 175 | int conditional_errors, |
991a6bb5 | 176 | int security_checks, |
50ea5cdc | 177 | int ems_check, |
95214b43 SL |
178 | unsigned char *install_mac, |
179 | size_t install_mac_len) | |
180 | { | |
181 | int ret = 0; | |
182 | ||
35e6ea3b SL |
183 | if (BIO_printf(out, "[%s]\n", section) <= 0 |
184 | || BIO_printf(out, "activate = 1\n") <= 0 | |
185 | || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION, | |
186 | VERSION_VAL) <= 0 | |
187 | || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS, | |
188 | conditional_errors ? "1" : "0") <= 0 | |
991a6bb5 SL |
189 | || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS, |
190 | security_checks ? "1" : "0") <= 0 | |
50ea5cdc | 191 | || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_TLS1_PRF_EMS_CHECK, |
192 | ems_check ? "1" : "0") <= 0 | |
35e6ea3b SL |
193 | || !print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac, |
194 | module_mac_len)) | |
95214b43 SL |
195 | goto end; |
196 | ||
2abffec0 | 197 | if (install_mac != NULL && install_mac_len > 0) { |
991a6bb5 SL |
198 | if (!print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac, |
199 | install_mac_len) | |
200 | || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_STATUS, | |
201 | INSTALL_STATUS_VAL) <= 0) | |
95214b43 SL |
202 | goto end; |
203 | } | |
204 | ret = 1; | |
205 | end: | |
206 | return ret; | |
207 | } | |
208 | ||
209 | static CONF *generate_config_and_load(const char *prov_name, | |
210 | const char *section, | |
211 | unsigned char *module_mac, | |
35e6ea3b | 212 | size_t module_mac_len, |
991a6bb5 | 213 | int conditional_errors, |
50ea5cdc | 214 | int security_checks, |
215 | int ems_check) | |
95214b43 SL |
216 | { |
217 | BIO *mem_bio = NULL; | |
218 | CONF *conf = NULL; | |
219 | ||
220 | mem_bio = BIO_new(BIO_s_mem()); | |
221 | if (mem_bio == NULL) | |
222 | return 0; | |
223 | if (!write_config_header(mem_bio, prov_name, section) | |
35e6ea3b SL |
224 | || !write_config_fips_section(mem_bio, section, |
225 | module_mac, module_mac_len, | |
226 | conditional_errors, | |
991a6bb5 | 227 | security_checks, |
50ea5cdc | 228 | ems_check, |
35e6ea3b | 229 | NULL, 0)) |
95214b43 SL |
230 | goto end; |
231 | ||
232 | conf = app_load_config_bio(mem_bio, NULL); | |
233 | if (conf == NULL) | |
234 | goto end; | |
235 | ||
e683582b | 236 | if (CONF_modules_load(conf, NULL, 0) <= 0) |
95214b43 SL |
237 | goto end; |
238 | BIO_free(mem_bio); | |
239 | return conf; | |
240 | end: | |
241 | NCONF_free(conf); | |
242 | BIO_free(mem_bio); | |
243 | return NULL; | |
244 | } | |
245 | ||
246 | static void free_config_and_unload(CONF *conf) | |
247 | { | |
248 | if (conf != NULL) { | |
249 | NCONF_free(conf); | |
250 | CONF_modules_unload(1); | |
251 | } | |
252 | } | |
253 | ||
9f7bdcf3 SL |
254 | static int verify_module_load(const char *parent_config_file) |
255 | { | |
b4250010 | 256 | return OSSL_LIB_CTX_load_config(NULL, parent_config_file); |
9f7bdcf3 SL |
257 | } |
258 | ||
95214b43 SL |
259 | /* |
260 | * Returns 1 if the config file entries match the passed in module_mac and | |
261 | * install_mac values, otherwise it returns 0. | |
262 | */ | |
263 | static int verify_config(const char *infile, const char *section, | |
264 | unsigned char *module_mac, size_t module_mac_len, | |
265 | unsigned char *install_mac, size_t install_mac_len) | |
266 | { | |
267 | int ret = 0; | |
268 | char *s = NULL; | |
269 | unsigned char *buf1 = NULL, *buf2 = NULL; | |
270 | long len; | |
271 | CONF *conf = NULL; | |
272 | ||
273 | /* read in the existing values and check they match the saved values */ | |
274 | conf = app_load_config(infile); | |
275 | if (conf == NULL) | |
276 | goto end; | |
277 | ||
278 | s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION); | |
279 | if (s == NULL || strcmp(s, VERSION_VAL) != 0) { | |
280 | BIO_printf(bio_err, "version not found\n"); | |
281 | goto end; | |
282 | } | |
95214b43 SL |
283 | s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC); |
284 | if (s == NULL) { | |
285 | BIO_printf(bio_err, "Module integrity MAC not found\n"); | |
286 | goto end; | |
287 | } | |
288 | buf1 = OPENSSL_hexstr2buf(s, &len); | |
289 | if (buf1 == NULL | |
290 | || (size_t)len != module_mac_len | |
291 | || memcmp(module_mac, buf1, module_mac_len) != 0) { | |
292 | BIO_printf(bio_err, "Module integrity mismatch\n"); | |
293 | goto end; | |
294 | } | |
2abffec0 SL |
295 | if (install_mac != NULL && install_mac_len > 0) { |
296 | s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS); | |
297 | if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) { | |
298 | BIO_printf(bio_err, "install status not found\n"); | |
299 | goto end; | |
300 | } | |
301 | s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC); | |
302 | if (s == NULL) { | |
303 | BIO_printf(bio_err, "Install indicator MAC not found\n"); | |
304 | goto end; | |
305 | } | |
306 | buf2 = OPENSSL_hexstr2buf(s, &len); | |
307 | if (buf2 == NULL | |
308 | || (size_t)len != install_mac_len | |
309 | || memcmp(install_mac, buf2, install_mac_len) != 0) { | |
310 | BIO_printf(bio_err, "Install indicator status mismatch\n"); | |
311 | goto end; | |
312 | } | |
95214b43 SL |
313 | } |
314 | ret = 1; | |
315 | end: | |
316 | OPENSSL_free(buf1); | |
317 | OPENSSL_free(buf2); | |
318 | NCONF_free(conf); | |
319 | return ret; | |
320 | } | |
321 | ||
322 | int fipsinstall_main(int argc, char **argv) | |
323 | { | |
7057dddb | 324 | int ret = 1, verify = 0, gotkey = 0, gotdigest = 0, self_test_onload = 1; |
991a6bb5 | 325 | int enable_conditional_errors = 1, enable_security_checks = 1; |
50ea5cdc | 326 | int enable_tls_prf_ems_check = 0; /* This is off by default */ |
5744dacb RS |
327 | const char *section_name = "fips_sect"; |
328 | const char *mac_name = "HMAC"; | |
329 | const char *prov_name = "fips"; | |
95214b43 | 330 | BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL; |
5744dacb | 331 | char *in_fname = NULL, *out_fname = NULL, *prog; |
9a62ccbe SL |
332 | char *module_fname = NULL, *parent_config = NULL, *module_path = NULL; |
333 | const char *tail; | |
95214b43 SL |
334 | EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL; |
335 | STACK_OF(OPENSSL_STRING) *opts = NULL; | |
336 | OPTION_CHOICE o; | |
337 | unsigned char *read_buffer = NULL; | |
338 | unsigned char module_mac[EVP_MAX_MD_SIZE]; | |
339 | size_t module_mac_len = EVP_MAX_MD_SIZE; | |
340 | unsigned char install_mac[EVP_MAX_MD_SIZE]; | |
341 | size_t install_mac_len = EVP_MAX_MD_SIZE; | |
342 | EVP_MAC *mac = NULL; | |
343 | CONF *conf = NULL; | |
344 | ||
31214258 RS |
345 | if ((opts = sk_OPENSSL_STRING_new_null()) == NULL) |
346 | goto end; | |
95214b43 SL |
347 | |
348 | prog = opt_init(argc, argv, fipsinstall_options); | |
349 | while ((o = opt_next()) != OPT_EOF) { | |
350 | switch (o) { | |
351 | case OPT_EOF: | |
352 | case OPT_ERR: | |
353 | opthelp: | |
354 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
1cd2c1f8 | 355 | goto cleanup; |
95214b43 SL |
356 | case OPT_HELP: |
357 | opt_help(fipsinstall_options); | |
358 | ret = 0; | |
359 | goto end; | |
360 | case OPT_IN: | |
361 | in_fname = opt_arg(); | |
362 | break; | |
363 | case OPT_OUT: | |
364 | out_fname = opt_arg(); | |
365 | break; | |
35e6ea3b SL |
366 | case OPT_NO_CONDITIONAL_ERRORS: |
367 | enable_conditional_errors = 0; | |
368 | break; | |
991a6bb5 SL |
369 | case OPT_NO_SECURITY_CHECKS: |
370 | enable_security_checks = 0; | |
371 | break; | |
50ea5cdc | 372 | case OPT_TLS_PRF_EMS_CHECK: |
373 | enable_tls_prf_ems_check = 1; | |
374 | break; | |
1cd2c1f8 RS |
375 | case OPT_QUIET: |
376 | quiet = 1; | |
377 | /* FALLTHROUGH */ | |
36fc5fc6 SL |
378 | case OPT_NO_LOG: |
379 | self_test_log = 0; | |
380 | break; | |
381 | case OPT_CORRUPT_DESC: | |
382 | self_test_corrupt_desc = opt_arg(); | |
383 | break; | |
384 | case OPT_CORRUPT_TYPE: | |
385 | self_test_corrupt_type = opt_arg(); | |
386 | break; | |
95214b43 SL |
387 | case OPT_PROV_NAME: |
388 | prov_name = opt_arg(); | |
389 | break; | |
390 | case OPT_MODULE: | |
391 | module_fname = opt_arg(); | |
392 | break; | |
393 | case OPT_SECTION_NAME: | |
394 | section_name = opt_arg(); | |
395 | break; | |
396 | case OPT_MAC_NAME: | |
397 | mac_name = opt_arg(); | |
398 | break; | |
9f7bdcf3 SL |
399 | case OPT_CONFIG: |
400 | parent_config = opt_arg(); | |
401 | break; | |
95214b43 | 402 | case OPT_MACOPT: |
31214258 | 403 | if (!sk_OPENSSL_STRING_push(opts, opt_arg())) |
95214b43 | 404 | goto opthelp; |
2ff286c2 | 405 | if (HAS_PREFIX(opt_arg(), "hexkey:")) |
31214258 | 406 | gotkey = 1; |
2ff286c2 | 407 | else if (HAS_PREFIX(opt_arg(), "digest:")) |
31214258 | 408 | gotdigest = 1; |
95214b43 SL |
409 | break; |
410 | case OPT_VERIFY: | |
411 | verify = 1; | |
412 | break; | |
2abffec0 SL |
413 | case OPT_SELF_TEST_ONLOAD: |
414 | self_test_onload = 1; | |
415 | break; | |
7057dddb P |
416 | case OPT_SELF_TEST_ONINSTALL: |
417 | self_test_onload = 0; | |
418 | break; | |
95214b43 SL |
419 | } |
420 | } | |
021410ea RS |
421 | |
422 | /* No extra arguments. */ | |
d9f07357 | 423 | if (!opt_check_rest_arg(NULL)) |
021410ea | 424 | goto opthelp; |
d9f07357 DDO |
425 | if (verify && in_fname == NULL) { |
426 | BIO_printf(bio_err, "Missing -in option for -verify\n"); | |
427 | goto opthelp; | |
428 | } | |
9f7bdcf3 SL |
429 | |
430 | if (parent_config != NULL) { | |
431 | /* Test that a parent config can load the module */ | |
432 | if (verify_module_load(parent_config)) { | |
433 | ret = OSSL_PROVIDER_available(NULL, prov_name) ? 0 : 1; | |
7057dddb | 434 | if (!quiet) { |
e9d74dbd | 435 | BIO_printf(bio_err, "FIPS provider is %s\n", |
9f7bdcf3 | 436 | ret == 0 ? "available" : " not available"); |
7057dddb | 437 | } |
9f7bdcf3 SL |
438 | } |
439 | goto end; | |
440 | } | |
eb78f955 | 441 | if (module_fname == NULL) |
95214b43 SL |
442 | goto opthelp; |
443 | ||
9a62ccbe SL |
444 | tail = opt_path_end(module_fname); |
445 | if (tail != NULL) { | |
446 | module_path = OPENSSL_strdup(module_fname); | |
447 | if (module_path == NULL) | |
448 | goto end; | |
449 | module_path[tail - module_fname] = '\0'; | |
450 | if (!OSSL_PROVIDER_set_default_search_path(NULL, module_path)) | |
451 | goto end; | |
452 | } | |
453 | ||
36fc5fc6 SL |
454 | if (self_test_log |
455 | || self_test_corrupt_desc != NULL | |
456 | || self_test_corrupt_type != NULL) | |
457 | OSSL_SELF_TEST_set_callback(NULL, self_test_events, NULL); | |
458 | ||
31214258 RS |
459 | /* Use the default FIPS HMAC digest and key if not specified. */ |
460 | if (!gotdigest && !sk_OPENSSL_STRING_push(opts, "digest:SHA256")) | |
461 | goto end; | |
462 | if (!gotkey && !sk_OPENSSL_STRING_push(opts, "hexkey:" FIPS_KEY_STRING)) | |
463 | goto end; | |
464 | ||
95214b43 SL |
465 | module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY); |
466 | if (module_bio == NULL) { | |
467 | BIO_printf(bio_err, "Failed to open module file\n"); | |
468 | goto end; | |
469 | } | |
470 | ||
471 | read_buffer = app_malloc(BUFSIZE, "I/O buffer"); | |
472 | if (read_buffer == NULL) | |
473 | goto end; | |
474 | ||
c8dd887d | 475 | mac = EVP_MAC_fetch(app_get0_libctx(), mac_name, app_get0_propq()); |
95214b43 SL |
476 | if (mac == NULL) { |
477 | BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name); | |
478 | goto end; | |
479 | } | |
480 | ||
865adf97 | 481 | ctx = EVP_MAC_CTX_new(mac); |
95214b43 SL |
482 | if (ctx == NULL) { |
483 | BIO_printf(bio_err, "Unable to create MAC CTX for module check\n"); | |
484 | goto end; | |
485 | } | |
486 | ||
487 | if (opts != NULL) { | |
488 | int ok = 1; | |
489 | OSSL_PARAM *params = | |
41f7ecf3 | 490 | app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac)); |
95214b43 SL |
491 | |
492 | if (params == NULL) | |
493 | goto end; | |
494 | ||
865adf97 | 495 | if (!EVP_MAC_CTX_set_params(ctx, params)) { |
95214b43 SL |
496 | BIO_printf(bio_err, "MAC parameter error\n"); |
497 | ERR_print_errors(bio_err); | |
498 | ok = 0; | |
499 | } | |
500 | app_params_free(params); | |
501 | if (!ok) | |
502 | goto end; | |
503 | } | |
504 | ||
865adf97 | 505 | ctx2 = EVP_MAC_CTX_dup(ctx); |
95214b43 SL |
506 | if (ctx2 == NULL) { |
507 | BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n"); | |
508 | goto end; | |
509 | } | |
510 | ||
511 | if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len)) | |
512 | goto end; | |
513 | ||
2abffec0 SL |
514 | if (self_test_onload == 0) { |
515 | mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL, | |
516 | strlen(INSTALL_STATUS_VAL)); | |
517 | if (mem_bio == NULL) { | |
518 | BIO_printf(bio_err, "Unable to create memory BIO\n"); | |
519 | goto end; | |
520 | } | |
521 | if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len)) | |
522 | goto end; | |
523 | } else { | |
524 | install_mac_len = 0; | |
95214b43 | 525 | } |
95214b43 SL |
526 | |
527 | if (verify) { | |
528 | if (!verify_config(in_fname, section_name, module_mac, module_mac_len, | |
529 | install_mac, install_mac_len)) | |
530 | goto end; | |
1cd2c1f8 | 531 | if (!quiet) |
e9d74dbd | 532 | BIO_printf(bio_err, "VERIFY PASSED\n"); |
95214b43 SL |
533 | } else { |
534 | ||
535 | conf = generate_config_and_load(prov_name, section_name, module_mac, | |
35e6ea3b | 536 | module_mac_len, |
991a6bb5 | 537 | enable_conditional_errors, |
50ea5cdc | 538 | enable_security_checks, |
539 | enable_tls_prf_ems_check); | |
95214b43 SL |
540 | if (conf == NULL) |
541 | goto end; | |
542 | if (!load_fips_prov_and_run_self_test(prov_name)) | |
543 | goto end; | |
544 | ||
eb78f955 RS |
545 | fout = |
546 | out_fname == NULL ? dup_bio_out(FORMAT_TEXT) | |
547 | : bio_open_default(out_fname, 'w', FORMAT_TEXT); | |
95214b43 SL |
548 | if (fout == NULL) { |
549 | BIO_printf(bio_err, "Failed to open file\n"); | |
550 | goto end; | |
551 | } | |
35e6ea3b SL |
552 | if (!write_config_fips_section(fout, section_name, |
553 | module_mac, module_mac_len, | |
554 | enable_conditional_errors, | |
991a6bb5 | 555 | enable_security_checks, |
50ea5cdc | 556 | enable_tls_prf_ems_check, |
35e6ea3b | 557 | install_mac, install_mac_len)) |
95214b43 | 558 | goto end; |
1cd2c1f8 | 559 | if (!quiet) |
e9d74dbd | 560 | BIO_printf(bio_err, "INSTALL PASSED\n"); |
95214b43 SL |
561 | } |
562 | ||
563 | ret = 0; | |
564 | end: | |
565 | if (ret == 1) { | |
1cd2c1f8 RS |
566 | if (!quiet) |
567 | BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL"); | |
95214b43 SL |
568 | ERR_print_errors(bio_err); |
569 | } | |
570 | ||
1cd2c1f8 | 571 | cleanup: |
9a62ccbe | 572 | OPENSSL_free(module_path); |
95214b43 SL |
573 | BIO_free(fout); |
574 | BIO_free(mem_bio); | |
575 | BIO_free(module_bio); | |
576 | sk_OPENSSL_STRING_free(opts); | |
577 | EVP_MAC_free(mac); | |
865adf97 MC |
578 | EVP_MAC_CTX_free(ctx2); |
579 | EVP_MAC_CTX_free(ctx); | |
95214b43 SL |
580 | OPENSSL_free(read_buffer); |
581 | free_config_and_unload(conf); | |
582 | return ret; | |
583 | } | |
36fc5fc6 SL |
584 | |
585 | static int self_test_events(const OSSL_PARAM params[], void *arg) | |
586 | { | |
587 | const OSSL_PARAM *p = NULL; | |
588 | const char *phase = NULL, *type = NULL, *desc = NULL; | |
589 | int ret = 0; | |
590 | ||
591 | p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE); | |
592 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) | |
593 | goto err; | |
594 | phase = (const char *)p->data; | |
595 | ||
596 | p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC); | |
597 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) | |
598 | goto err; | |
599 | desc = (const char *)p->data; | |
600 | ||
601 | p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE); | |
602 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) | |
603 | goto err; | |
604 | type = (const char *)p->data; | |
605 | ||
606 | if (self_test_log) { | |
607 | if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0) | |
e9d74dbd | 608 | BIO_printf(bio_err, "%s : (%s) : ", desc, type); |
36fc5fc6 SL |
609 | else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0 |
610 | || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0) | |
e9d74dbd | 611 | BIO_printf(bio_err, "%s\n", phase); |
36fc5fc6 SL |
612 | } |
613 | /* | |
614 | * The self test code will internally corrupt the KAT test result if an | |
615 | * error is returned during the corrupt phase. | |
616 | */ | |
617 | if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0 | |
618 | && (self_test_corrupt_desc != NULL | |
619 | || self_test_corrupt_type != NULL)) { | |
620 | if (self_test_corrupt_desc != NULL | |
621 | && strcmp(self_test_corrupt_desc, desc) != 0) | |
622 | goto end; | |
623 | if (self_test_corrupt_type != NULL | |
624 | && strcmp(self_test_corrupt_type, type) != 0) | |
625 | goto end; | |
e9d74dbd | 626 | BIO_printf(bio_err, "%s ", phase); |
36fc5fc6 SL |
627 | goto err; |
628 | } | |
629 | end: | |
630 | ret = 1; | |
631 | err: | |
632 | return ret; | |
633 | } |