2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
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
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>
23 /* Configuration file values */
24 #define VERSION_KEY "version"
25 #define VERSION_VAL "1"
26 #define INSTALL_STATUS_VAL "INSTALL_SELF_TEST_KATS_RUN"
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;
34 typedef enum OPTION_choice {
36 OPT_IN, OPT_OUT, OPT_MODULE, OPT_PEDANTIC,
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_TLS_PRF_EMS_CHECK,
42 OPT_DISALLOW_DRGB_TRUNC_DIGEST,
43 OPT_SELF_TEST_ONLOAD, OPT_SELF_TEST_ONINSTALL
46 const OPTIONS fipsinstall_options[] = {
47 OPT_SECTION("General"),
48 {"help", OPT_HELP, '-', "Display this summary"},
49 {"pedantic", OPT_PEDANTIC, '-', "Set options for strict FIPS compliance"},
50 {"verify", OPT_VERIFY, '-',
51 "Verify a config file instead of generating one"},
52 {"module", OPT_MODULE, '<', "File name of the provider module"},
53 {"provider_name", OPT_PROV_NAME, 's', "FIPS provider name"},
54 {"section_name", OPT_SECTION_NAME, 's',
55 "FIPS Provider config section name (optional)"},
56 {"no_conditional_errors", OPT_NO_CONDITIONAL_ERRORS, '-',
57 "Disable the ability of the fips module to enter an error state if"
58 " any conditional self tests fail"},
59 {"no_security_checks", OPT_NO_SECURITY_CHECKS, '-',
60 "Disable the run-time FIPS security checks in the module"},
61 {"self_test_onload", OPT_SELF_TEST_ONLOAD, '-',
62 "Forces self tests to always run on module load"},
63 {"self_test_oninstall", OPT_SELF_TEST_ONINSTALL, '-',
64 "Forces self tests to run once on module installation"},
65 {"ems_check", OPT_TLS_PRF_EMS_CHECK, '-',
66 "Enable the run-time FIPS check for EMS during TLS1_PRF"},
67 {"no_drbg_truncated_digests", OPT_DISALLOW_DRGB_TRUNC_DIGEST, '-',
68 "Disallow truncated digests with Hash and HMAC DRBGs"},
70 {"in", OPT_IN, '<', "Input config file, used when verifying"},
72 OPT_SECTION("Output"),
73 {"out", OPT_OUT, '>', "Output config file, used when generating"},
74 {"mac_name", OPT_MAC_NAME, 's', "MAC name"},
75 {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form."},
76 {OPT_MORE_STR, 0, 0, "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
77 {"noout", OPT_NO_LOG, '-', "Disable logging of self test events"},
78 {"corrupt_desc", OPT_CORRUPT_DESC, 's', "Corrupt a self test by description"},
79 {"corrupt_type", OPT_CORRUPT_TYPE, 's', "Corrupt a self test by type"},
80 {"config", OPT_CONFIG, '<', "The parent config to verify"},
81 {"quiet", OPT_QUIET, '-', "No messages, just exit status"},
86 unsigned int self_test_onload : 1;
87 unsigned int conditional_errors : 1;
88 unsigned int security_checks : 1;
89 unsigned int tls_prf_ems_check : 1;
90 unsigned int drgb_no_trunc_dgst : 1;
93 /* Pedantic FIPS compliance */
94 static const FIPS_OPTS pedantic_opts = {
95 1, /* self_test_onload */
96 1, /* conditional_errors */
97 1, /* security_checks */
98 1, /* tls_prf_ems_check */
99 1, /* drgb_no_trunc_dgst */
102 /* Default FIPS settings for backward compatibility */
103 static FIPS_OPTS fips_opts = {
104 1, /* self_test_onload */
105 1, /* conditional_errors */
106 1, /* security_checks */
107 0, /* tls_prf_ems_check */
108 0, /* drgb_no_trunc_dgst */
111 static int check_non_pedantic_fips(int pedantic, const char *name)
114 BIO_printf(bio_err, "Cannot specify -%s after -pedantic\n", name);
120 static int do_mac(EVP_MAC_CTX *ctx, unsigned char *tmp, BIO *in,
121 unsigned char *out, size_t *out_len)
125 size_t outsz = *out_len;
127 if (!EVP_MAC_init(ctx, NULL, 0, NULL))
129 if (EVP_MAC_CTX_get_mac_size(ctx) > outsz)
131 while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) {
132 if (i < 0 || !EVP_MAC_update(ctx, tmp, i))
136 if (!EVP_MAC_final(ctx, out, out_len, outsz))
143 static int load_fips_prov_and_run_self_test(const char *prov_name)
146 OSSL_PROVIDER *prov = NULL;
147 OSSL_PARAM params[4], *p = params;
148 char *name = "", *vers = "", *build = "";
150 prov = OSSL_PROVIDER_load(NULL, prov_name);
152 BIO_printf(bio_err, "Failed to load FIPS module\n");
156 *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_NAME,
157 &name, sizeof(name));
158 *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION,
159 &vers, sizeof(vers));
160 *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_BUILDINFO,
161 &build, sizeof(build));
162 *p = OSSL_PARAM_construct_end();
163 if (!OSSL_PROVIDER_get_params(prov, params)) {
164 BIO_printf(bio_err, "Failed to query FIPS module parameters\n");
167 if (OSSL_PARAM_modified(params))
168 BIO_printf(bio_err, "\t%-10s\t%s\n", "name:", name);
169 if (OSSL_PARAM_modified(params + 1))
170 BIO_printf(bio_err, "\t%-10s\t%s\n", "version:", vers);
171 if (OSSL_PARAM_modified(params + 2))
172 BIO_printf(bio_err, "\t%-10s\t%s\n", "build:", build);
176 OSSL_PROVIDER_unload(prov);
180 static int print_mac(BIO *bio, const char *label, const unsigned char *mac,
186 hexstr = OPENSSL_buf2hexstr(mac, (long)len);
189 ret = BIO_printf(bio, "%s = %s\n", label, hexstr);
190 OPENSSL_free(hexstr);
194 static int write_config_header(BIO *out, const char *prov_name,
197 return BIO_printf(out, "openssl_conf = openssl_init\n\n")
198 && BIO_printf(out, "[openssl_init]\n")
199 && BIO_printf(out, "providers = provider_section\n\n")
200 && BIO_printf(out, "[provider_section]\n")
201 && BIO_printf(out, "%s = %s\n\n", prov_name, section);
205 * Outputs a fips related config file that contains entries for the fips
206 * module checksum, installation indicator checksum and the options
207 * conditional_errors and security_checks.
209 * Returns 1 if the config file is written otherwise it returns 0 on error.
211 static int write_config_fips_section(BIO *out, const char *section,
212 unsigned char *module_mac,
213 size_t module_mac_len,
214 const FIPS_OPTS *opts,
215 unsigned char *install_mac,
216 size_t install_mac_len)
220 if (BIO_printf(out, "[%s]\n", section) <= 0
221 || BIO_printf(out, "activate = 1\n") <= 0
222 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
224 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
225 opts->conditional_errors ? "1" : "0") <= 0
226 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
227 opts->security_checks ? "1" : "0") <= 0
228 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_TLS1_PRF_EMS_CHECK,
229 opts->tls_prf_ems_check ? "1" : "0") <= 0
230 || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_DRBG_TRUNC_DIGEST,
231 opts->drgb_no_trunc_dgst ? "1" : "0") <= 0
232 || !print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
236 if (install_mac != NULL && install_mac_len > 0) {
237 if (!print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
239 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
240 INSTALL_STATUS_VAL) <= 0)
248 static CONF *generate_config_and_load(const char *prov_name,
250 unsigned char *module_mac,
251 size_t module_mac_len,
252 const FIPS_OPTS *opts)
257 mem_bio = BIO_new(BIO_s_mem());
260 if (!write_config_header(mem_bio, prov_name, section)
261 || !write_config_fips_section(mem_bio, section,
262 module_mac, module_mac_len,
266 conf = app_load_config_bio(mem_bio, NULL);
270 if (CONF_modules_load(conf, NULL, 0) <= 0)
280 static void free_config_and_unload(CONF *conf)
284 CONF_modules_unload(1);
288 static int verify_module_load(const char *parent_config_file)
290 return OSSL_LIB_CTX_load_config(NULL, parent_config_file);
294 * Returns 1 if the config file entries match the passed in module_mac and
295 * install_mac values, otherwise it returns 0.
297 static int verify_config(const char *infile, const char *section,
298 unsigned char *module_mac, size_t module_mac_len,
299 unsigned char *install_mac, size_t install_mac_len)
303 unsigned char *buf1 = NULL, *buf2 = NULL;
307 /* read in the existing values and check they match the saved values */
308 conf = app_load_config(infile);
312 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION);
313 if (s == NULL || strcmp(s, VERSION_VAL) != 0) {
314 BIO_printf(bio_err, "version not found\n");
317 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
319 BIO_printf(bio_err, "Module integrity MAC not found\n");
322 buf1 = OPENSSL_hexstr2buf(s, &len);
324 || (size_t)len != module_mac_len
325 || memcmp(module_mac, buf1, module_mac_len) != 0) {
326 BIO_printf(bio_err, "Module integrity mismatch\n");
329 if (install_mac != NULL && install_mac_len > 0) {
330 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
331 if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
332 BIO_printf(bio_err, "install status not found\n");
335 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
337 BIO_printf(bio_err, "Install indicator MAC not found\n");
340 buf2 = OPENSSL_hexstr2buf(s, &len);
342 || (size_t)len != install_mac_len
343 || memcmp(install_mac, buf2, install_mac_len) != 0) {
344 BIO_printf(bio_err, "Install indicator status mismatch\n");
356 int fipsinstall_main(int argc, char **argv)
358 int ret = 1, verify = 0, gotkey = 0, gotdigest = 0, pedantic = 0;
359 const char *section_name = "fips_sect";
360 const char *mac_name = "HMAC";
361 const char *prov_name = "fips";
362 BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
363 char *in_fname = NULL, *out_fname = NULL, *prog;
364 char *module_fname = NULL, *parent_config = NULL, *module_path = NULL;
366 EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
367 STACK_OF(OPENSSL_STRING) *opts = NULL;
369 unsigned char *read_buffer = NULL;
370 unsigned char module_mac[EVP_MAX_MD_SIZE];
371 size_t module_mac_len = EVP_MAX_MD_SIZE;
372 unsigned char install_mac[EVP_MAX_MD_SIZE];
373 size_t install_mac_len = EVP_MAX_MD_SIZE;
377 if ((opts = sk_OPENSSL_STRING_new_null()) == NULL)
380 prog = opt_init(argc, argv, fipsinstall_options);
381 while ((o = opt_next()) != OPT_EOF) {
386 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
389 opt_help(fipsinstall_options);
393 in_fname = opt_arg();
396 out_fname = opt_arg();
399 fips_opts = pedantic_opts;
402 case OPT_NO_CONDITIONAL_ERRORS:
403 if (!check_non_pedantic_fips(pedantic, "no_conditional_errors"))
405 fips_opts.conditional_errors = 0;
407 case OPT_NO_SECURITY_CHECKS:
408 if (!check_non_pedantic_fips(pedantic, "no_security_checks"))
410 fips_opts.security_checks = 0;
412 case OPT_TLS_PRF_EMS_CHECK:
413 fips_opts.tls_prf_ems_check = 1;
415 case OPT_DISALLOW_DRGB_TRUNC_DIGEST:
416 fips_opts.drgb_no_trunc_dgst = 1;
424 case OPT_CORRUPT_DESC:
425 self_test_corrupt_desc = opt_arg();
427 case OPT_CORRUPT_TYPE:
428 self_test_corrupt_type = opt_arg();
431 prov_name = opt_arg();
434 module_fname = opt_arg();
436 case OPT_SECTION_NAME:
437 section_name = opt_arg();
440 mac_name = opt_arg();
443 parent_config = opt_arg();
446 if (!sk_OPENSSL_STRING_push(opts, opt_arg()))
448 if (HAS_PREFIX(opt_arg(), "hexkey:"))
450 else if (HAS_PREFIX(opt_arg(), "digest:"))
456 case OPT_SELF_TEST_ONLOAD:
457 fips_opts.self_test_onload = 1;
459 case OPT_SELF_TEST_ONINSTALL:
460 if (!check_non_pedantic_fips(pedantic, "self_test_oninstall"))
462 fips_opts.self_test_onload = 0;
467 /* No extra arguments. */
468 if (!opt_check_rest_arg(NULL))
470 if (verify && in_fname == NULL) {
471 BIO_printf(bio_err, "Missing -in option for -verify\n");
475 if (parent_config != NULL) {
476 /* Test that a parent config can load the module */
477 if (verify_module_load(parent_config)) {
478 ret = OSSL_PROVIDER_available(NULL, prov_name) ? 0 : 1;
480 BIO_printf(bio_err, "FIPS provider is %s\n",
481 ret == 0 ? "available" : " not available");
486 if (module_fname == NULL)
489 tail = opt_path_end(module_fname);
491 module_path = OPENSSL_strdup(module_fname);
492 if (module_path == NULL)
494 module_path[tail - module_fname] = '\0';
495 if (!OSSL_PROVIDER_set_default_search_path(NULL, module_path))
500 || self_test_corrupt_desc != NULL
501 || self_test_corrupt_type != NULL)
502 OSSL_SELF_TEST_set_callback(NULL, self_test_events, NULL);
504 /* Use the default FIPS HMAC digest and key if not specified. */
505 if (!gotdigest && !sk_OPENSSL_STRING_push(opts, "digest:SHA256"))
507 if (!gotkey && !sk_OPENSSL_STRING_push(opts, "hexkey:" FIPS_KEY_STRING))
510 module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY);
511 if (module_bio == NULL) {
512 BIO_printf(bio_err, "Failed to open module file\n");
516 read_buffer = app_malloc(BUFSIZE, "I/O buffer");
517 if (read_buffer == NULL)
520 mac = EVP_MAC_fetch(app_get0_libctx(), mac_name, app_get0_propq());
522 BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
526 ctx = EVP_MAC_CTX_new(mac);
528 BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
535 app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
540 if (!EVP_MAC_CTX_set_params(ctx, params)) {
541 BIO_printf(bio_err, "MAC parameter error\n");
542 ERR_print_errors(bio_err);
545 app_params_free(params);
550 ctx2 = EVP_MAC_CTX_dup(ctx);
552 BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
556 if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
559 if (fips_opts.self_test_onload == 0) {
560 mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
561 strlen(INSTALL_STATUS_VAL));
562 if (mem_bio == NULL) {
563 BIO_printf(bio_err, "Unable to create memory BIO\n");
566 if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
573 if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
574 install_mac, install_mac_len))
577 BIO_printf(bio_err, "VERIFY PASSED\n");
580 conf = generate_config_and_load(prov_name, section_name, module_mac,
581 module_mac_len, &fips_opts);
584 if (!load_fips_prov_and_run_self_test(prov_name))
588 out_fname == NULL ? dup_bio_out(FORMAT_TEXT)
589 : bio_open_default(out_fname, 'w', FORMAT_TEXT);
591 BIO_printf(bio_err, "Failed to open file\n");
594 if (!write_config_fips_section(fout, section_name,
595 module_mac, module_mac_len, &fips_opts,
596 install_mac, install_mac_len))
599 BIO_printf(bio_err, "INSTALL PASSED\n");
606 BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
607 ERR_print_errors(bio_err);
611 OPENSSL_free(module_path);
614 BIO_free(module_bio);
615 sk_OPENSSL_STRING_free(opts);
617 EVP_MAC_CTX_free(ctx2);
618 EVP_MAC_CTX_free(ctx);
619 OPENSSL_free(read_buffer);
620 free_config_and_unload(conf);
624 static int self_test_events(const OSSL_PARAM params[], void *arg)
626 const OSSL_PARAM *p = NULL;
627 const char *phase = NULL, *type = NULL, *desc = NULL;
630 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
631 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
633 phase = (const char *)p->data;
635 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
636 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
638 desc = (const char *)p->data;
640 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
641 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
643 type = (const char *)p->data;
646 if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
647 BIO_printf(bio_err, "%s : (%s) : ", desc, type);
648 else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
649 || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
650 BIO_printf(bio_err, "%s\n", phase);
653 * The self test code will internally corrupt the KAT test result if an
654 * error is returned during the corrupt phase.
656 if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0
657 && (self_test_corrupt_desc != NULL
658 || self_test_corrupt_type != NULL)) {
659 if (self_test_corrupt_desc != NULL
660 && strcmp(self_test_corrupt_desc, desc) != 0)
662 if (self_test_corrupt_type != NULL
663 && strcmp(self_test_corrupt_type, type) != 0)
665 BIO_printf(bio_err, "%s ", phase);