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