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