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,
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_SELF_TEST_ONLOAD, OPT_SELF_TEST_ONINSTALL
45 const OPTIONS fipsinstall_options[] = {
46 OPT_SECTION("General"),
47 {"help", OPT_HELP, '-', "Display this summary"},
48 {"verify", OPT_VERIFY, '-',
49 "Verify a config file instead of generating one"},
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)"},
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"},
57 {"no_security_checks", OPT_NO_SECURITY_CHECKS, '-',
58 "Disable the run-time FIPS security checks in the module"},
59 {"self_test_onload", OPT_SELF_TEST_ONLOAD, '-',
60 "Forces self tests to always run on module load"},
61 {"self_test_oninstall", OPT_SELF_TEST_ONINSTALL, '-',
62 "Forces self tests to run once on module installation"},
63 {"ems_check", OPT_TLS_PRF_EMS_CHECK, '-',
64 "Enable the run-time FIPS check for EMS during TLS1_PRF"},
66 {"in", OPT_IN, '<', "Input config file, used when verifying"},
68 OPT_SECTION("Output"),
69 {"out", OPT_OUT, '>', "Output config file, used when generating"},
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"},
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"},
76 {"config", OPT_CONFIG, '<', "The parent config to verify"},
77 {"quiet", OPT_QUIET, '-', "No messages, just exit status"},
81 static int do_mac(EVP_MAC_CTX *ctx, unsigned char *tmp, BIO *in,
82 unsigned char *out, size_t *out_len)
86 size_t outsz = *out_len;
88 if (!EVP_MAC_init(ctx, NULL, 0, NULL))
90 if (EVP_MAC_CTX_get_mac_size(ctx) > outsz)
92 while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) {
93 if (i < 0 || !EVP_MAC_update(ctx, tmp, i))
97 if (!EVP_MAC_final(ctx, out, out_len, outsz))
104 static int load_fips_prov_and_run_self_test(const char *prov_name)
107 OSSL_PROVIDER *prov = NULL;
108 OSSL_PARAM params[4], *p = params;
109 char *name = "", *vers = "", *build = "";
111 prov = OSSL_PROVIDER_load(NULL, prov_name);
113 BIO_printf(bio_err, "Failed to load FIPS module\n");
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");
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);
137 OSSL_PROVIDER_unload(prov);
141 static int print_mac(BIO *bio, const char *label, const unsigned char *mac,
147 hexstr = OPENSSL_buf2hexstr(mac, (long)len);
150 ret = BIO_printf(bio, "%s = %s\n", label, hexstr);
151 OPENSSL_free(hexstr);
155 static int write_config_header(BIO *out, const char *prov_name,
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);
166 * Outputs a fips related config file that contains entries for the fips
167 * module checksum, installation indicator checksum and the options
168 * conditional_errors and security_checks.
170 * Returns 1 if the config file is written otherwise it returns 0 on error.
172 static int write_config_fips_section(BIO *out, const char *section,
173 unsigned char *module_mac,
174 size_t module_mac_len,
175 int conditional_errors,
178 unsigned char *install_mac,
179 size_t install_mac_len)
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,
187 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
188 conditional_errors ? "1" : "0") <= 0
189 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
190 security_checks ? "1" : "0") <= 0
191 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_TLS1_PRF_EMS_CHECK,
192 ems_check ? "1" : "0") <= 0
193 || !print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
197 if (install_mac != NULL && install_mac_len > 0) {
198 if (!print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
200 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
201 INSTALL_STATUS_VAL) <= 0)
209 static CONF *generate_config_and_load(const char *prov_name,
211 unsigned char *module_mac,
212 size_t module_mac_len,
213 int conditional_errors,
220 mem_bio = BIO_new(BIO_s_mem());
223 if (!write_config_header(mem_bio, prov_name, section)
224 || !write_config_fips_section(mem_bio, section,
225 module_mac, module_mac_len,
232 conf = app_load_config_bio(mem_bio, NULL);
236 if (CONF_modules_load(conf, NULL, 0) <= 0)
246 static void free_config_and_unload(CONF *conf)
250 CONF_modules_unload(1);
254 static int verify_module_load(const char *parent_config_file)
256 return OSSL_LIB_CTX_load_config(NULL, parent_config_file);
260 * Returns 1 if the config file entries match the passed in module_mac and
261 * install_mac values, otherwise it returns 0.
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)
269 unsigned char *buf1 = NULL, *buf2 = NULL;
273 /* read in the existing values and check they match the saved values */
274 conf = app_load_config(infile);
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");
283 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
285 BIO_printf(bio_err, "Module integrity MAC not found\n");
288 buf1 = OPENSSL_hexstr2buf(s, &len);
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");
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");
301 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
303 BIO_printf(bio_err, "Install indicator MAC not found\n");
306 buf2 = OPENSSL_hexstr2buf(s, &len);
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");
322 int fipsinstall_main(int argc, char **argv)
324 int ret = 1, verify = 0, gotkey = 0, gotdigest = 0, self_test_onload = 1;
325 int enable_conditional_errors = 1, enable_security_checks = 1;
326 int enable_tls_prf_ems_check = 0; /* This is off by default */
327 const char *section_name = "fips_sect";
328 const char *mac_name = "HMAC";
329 const char *prov_name = "fips";
330 BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
331 char *in_fname = NULL, *out_fname = NULL, *prog;
332 char *module_fname = NULL, *parent_config = NULL, *module_path = NULL;
334 EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
335 STACK_OF(OPENSSL_STRING) *opts = NULL;
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;
345 if ((opts = sk_OPENSSL_STRING_new_null()) == NULL)
348 prog = opt_init(argc, argv, fipsinstall_options);
349 while ((o = opt_next()) != OPT_EOF) {
354 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
357 opt_help(fipsinstall_options);
361 in_fname = opt_arg();
364 out_fname = opt_arg();
366 case OPT_NO_CONDITIONAL_ERRORS:
367 enable_conditional_errors = 0;
369 case OPT_NO_SECURITY_CHECKS:
370 enable_security_checks = 0;
372 case OPT_TLS_PRF_EMS_CHECK:
373 enable_tls_prf_ems_check = 1;
381 case OPT_CORRUPT_DESC:
382 self_test_corrupt_desc = opt_arg();
384 case OPT_CORRUPT_TYPE:
385 self_test_corrupt_type = opt_arg();
388 prov_name = opt_arg();
391 module_fname = opt_arg();
393 case OPT_SECTION_NAME:
394 section_name = opt_arg();
397 mac_name = opt_arg();
400 parent_config = opt_arg();
403 if (!sk_OPENSSL_STRING_push(opts, opt_arg()))
405 if (HAS_PREFIX(opt_arg(), "hexkey:"))
407 else if (HAS_PREFIX(opt_arg(), "digest:"))
413 case OPT_SELF_TEST_ONLOAD:
414 self_test_onload = 1;
416 case OPT_SELF_TEST_ONINSTALL:
417 self_test_onload = 0;
422 /* No extra arguments. */
423 if (!opt_check_rest_arg(NULL))
425 if (verify && in_fname == NULL) {
426 BIO_printf(bio_err, "Missing -in option for -verify\n");
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;
435 BIO_printf(bio_err, "FIPS provider is %s\n",
436 ret == 0 ? "available" : " not available");
441 if (module_fname == NULL)
444 tail = opt_path_end(module_fname);
446 module_path = OPENSSL_strdup(module_fname);
447 if (module_path == NULL)
449 module_path[tail - module_fname] = '\0';
450 if (!OSSL_PROVIDER_set_default_search_path(NULL, module_path))
455 || self_test_corrupt_desc != NULL
456 || self_test_corrupt_type != NULL)
457 OSSL_SELF_TEST_set_callback(NULL, self_test_events, NULL);
459 /* Use the default FIPS HMAC digest and key if not specified. */
460 if (!gotdigest && !sk_OPENSSL_STRING_push(opts, "digest:SHA256"))
462 if (!gotkey && !sk_OPENSSL_STRING_push(opts, "hexkey:" FIPS_KEY_STRING))
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");
471 read_buffer = app_malloc(BUFSIZE, "I/O buffer");
472 if (read_buffer == NULL)
475 mac = EVP_MAC_fetch(app_get0_libctx(), mac_name, app_get0_propq());
477 BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
481 ctx = EVP_MAC_CTX_new(mac);
483 BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
490 app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
495 if (!EVP_MAC_CTX_set_params(ctx, params)) {
496 BIO_printf(bio_err, "MAC parameter error\n");
497 ERR_print_errors(bio_err);
500 app_params_free(params);
505 ctx2 = EVP_MAC_CTX_dup(ctx);
507 BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
511 if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
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");
521 if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
528 if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
529 install_mac, install_mac_len))
532 BIO_printf(bio_err, "VERIFY PASSED\n");
535 conf = generate_config_and_load(prov_name, section_name, module_mac,
537 enable_conditional_errors,
538 enable_security_checks,
539 enable_tls_prf_ems_check);
542 if (!load_fips_prov_and_run_self_test(prov_name))
546 out_fname == NULL ? dup_bio_out(FORMAT_TEXT)
547 : bio_open_default(out_fname, 'w', FORMAT_TEXT);
549 BIO_printf(bio_err, "Failed to open file\n");
552 if (!write_config_fips_section(fout, section_name,
553 module_mac, module_mac_len,
554 enable_conditional_errors,
555 enable_security_checks,
556 enable_tls_prf_ems_check,
557 install_mac, install_mac_len))
560 BIO_printf(bio_err, "INSTALL PASSED\n");
567 BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
568 ERR_print_errors(bio_err);
572 OPENSSL_free(module_path);
575 BIO_free(module_bio);
576 sk_OPENSSL_STRING_free(opts);
578 EVP_MAC_CTX_free(ctx2);
579 EVP_MAC_CTX_free(ctx);
580 OPENSSL_free(read_buffer);
581 free_config_and_unload(conf);
585 static int self_test_events(const OSSL_PARAM params[], void *arg)
587 const OSSL_PARAM *p = NULL;
588 const char *phase = NULL, *type = NULL, *desc = NULL;
591 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
592 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
594 phase = (const char *)p->data;
596 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
597 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
599 desc = (const char *)p->data;
601 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
602 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
604 type = (const char *)p->data;
607 if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
608 BIO_printf(bio_err, "%s : (%s) : ", desc, type);
609 else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
610 || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
611 BIO_printf(bio_err, "%s\n", phase);
614 * The self test code will internally corrupt the KAT test result if an
615 * error is returned during the corrupt phase.
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)
623 if (self_test_corrupt_type != NULL
624 && strcmp(self_test_corrupt_type, type) != 0)
626 BIO_printf(bio_err, "%s ", phase);