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_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 {"verify", OPT_VERIFY, '-',
50 "Verify a config file instead of generating one"},
51 {"module", OPT_MODULE, '<', "File name of the provider module"},
52 {"provider_name", OPT_PROV_NAME, 's', "FIPS provider name"},
53 {"section_name", OPT_SECTION_NAME, 's',
54 "FIPS Provider config section name (optional)"},
55 {"no_conditional_errors", OPT_NO_CONDITIONAL_ERRORS, '-',
56 "Disable the ability of the fips module to enter an error state if"
57 " any conditional self tests fail"},
58 {"no_security_checks", OPT_NO_SECURITY_CHECKS, '-',
59 "Disable the run-time FIPS security checks in the module"},
60 {"self_test_onload", OPT_SELF_TEST_ONLOAD, '-',
61 "Forces self tests to always run on module load"},
62 {"self_test_oninstall", OPT_SELF_TEST_ONINSTALL, '-',
63 "Forces self tests to run once on module installation"},
64 {"ems_check", OPT_TLS_PRF_EMS_CHECK, '-',
65 "Enable the run-time FIPS check for EMS during TLS1_PRF"},
66 {"no_drbg_truncated_digests", OPT_DISALLOW_DRGB_TRUNC_DIGEST, '-',
67 "Disallow truncated digests with Hash and HMAC DRBGs"},
69 {"in", OPT_IN, '<', "Input config file, used when verifying"},
71 OPT_SECTION("Output"),
72 {"out", OPT_OUT, '>', "Output config file, used when generating"},
73 {"mac_name", OPT_MAC_NAME, 's', "MAC name"},
74 {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form."},
75 {OPT_MORE_STR, 0, 0, "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
76 {"noout", OPT_NO_LOG, '-', "Disable logging of self test events"},
77 {"corrupt_desc", OPT_CORRUPT_DESC, 's', "Corrupt a self test by description"},
78 {"corrupt_type", OPT_CORRUPT_TYPE, 's', "Corrupt a self test by type"},
79 {"config", OPT_CONFIG, '<', "The parent config to verify"},
80 {"quiet", OPT_QUIET, '-', "No messages, just exit status"},
84 static int do_mac(EVP_MAC_CTX *ctx, unsigned char *tmp, BIO *in,
85 unsigned char *out, size_t *out_len)
89 size_t outsz = *out_len;
91 if (!EVP_MAC_init(ctx, NULL, 0, NULL))
93 if (EVP_MAC_CTX_get_mac_size(ctx) > outsz)
95 while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) {
96 if (i < 0 || !EVP_MAC_update(ctx, tmp, i))
100 if (!EVP_MAC_final(ctx, out, out_len, outsz))
107 static int load_fips_prov_and_run_self_test(const char *prov_name)
110 OSSL_PROVIDER *prov = NULL;
111 OSSL_PARAM params[4], *p = params;
112 char *name = "", *vers = "", *build = "";
114 prov = OSSL_PROVIDER_load(NULL, prov_name);
116 BIO_printf(bio_err, "Failed to load FIPS module\n");
120 *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_NAME,
121 &name, sizeof(name));
122 *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION,
123 &vers, sizeof(vers));
124 *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_BUILDINFO,
125 &build, sizeof(build));
126 *p = OSSL_PARAM_construct_end();
127 if (!OSSL_PROVIDER_get_params(prov, params)) {
128 BIO_printf(bio_err, "Failed to query FIPS module parameters\n");
131 if (OSSL_PARAM_modified(params))
132 BIO_printf(bio_err, "\t%-10s\t%s\n", "name:", name);
133 if (OSSL_PARAM_modified(params + 1))
134 BIO_printf(bio_err, "\t%-10s\t%s\n", "version:", vers);
135 if (OSSL_PARAM_modified(params + 2))
136 BIO_printf(bio_err, "\t%-10s\t%s\n", "build:", build);
140 OSSL_PROVIDER_unload(prov);
144 static int print_mac(BIO *bio, const char *label, const unsigned char *mac,
150 hexstr = OPENSSL_buf2hexstr(mac, (long)len);
153 ret = BIO_printf(bio, "%s = %s\n", label, hexstr);
154 OPENSSL_free(hexstr);
158 static int write_config_header(BIO *out, const char *prov_name,
161 return BIO_printf(out, "openssl_conf = openssl_init\n\n")
162 && BIO_printf(out, "[openssl_init]\n")
163 && BIO_printf(out, "providers = provider_section\n\n")
164 && BIO_printf(out, "[provider_section]\n")
165 && BIO_printf(out, "%s = %s\n\n", prov_name, section);
169 * Outputs a fips related config file that contains entries for the fips
170 * module checksum, installation indicator checksum and the options
171 * conditional_errors and security_checks.
173 * Returns 1 if the config file is written otherwise it returns 0 on error.
175 static int write_config_fips_section(BIO *out, const char *section,
176 unsigned char *module_mac,
177 size_t module_mac_len,
178 int conditional_errors,
181 int drgb_no_trunc_dgst,
182 unsigned char *install_mac,
183 size_t install_mac_len)
187 if (BIO_printf(out, "[%s]\n", section) <= 0
188 || BIO_printf(out, "activate = 1\n") <= 0
189 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
191 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
192 conditional_errors ? "1" : "0") <= 0
193 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
194 security_checks ? "1" : "0") <= 0
195 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_TLS1_PRF_EMS_CHECK,
196 ems_check ? "1" : "0") <= 0
197 || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_DRBG_TRUNC_DIGEST,
198 drgb_no_trunc_dgst ? "1" : "0") <= 0
199 || !print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
203 if (install_mac != NULL && install_mac_len > 0) {
204 if (!print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
206 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
207 INSTALL_STATUS_VAL) <= 0)
215 static CONF *generate_config_and_load(const char *prov_name,
217 unsigned char *module_mac,
218 size_t module_mac_len,
219 int conditional_errors,
222 int drgb_no_trunc_dgst)
227 mem_bio = BIO_new(BIO_s_mem());
230 if (!write_config_header(mem_bio, prov_name, section)
231 || !write_config_fips_section(mem_bio, section,
232 module_mac, module_mac_len,
240 conf = app_load_config_bio(mem_bio, NULL);
244 if (CONF_modules_load(conf, NULL, 0) <= 0)
254 static void free_config_and_unload(CONF *conf)
258 CONF_modules_unload(1);
262 static int verify_module_load(const char *parent_config_file)
264 return OSSL_LIB_CTX_load_config(NULL, parent_config_file);
268 * Returns 1 if the config file entries match the passed in module_mac and
269 * install_mac values, otherwise it returns 0.
271 static int verify_config(const char *infile, const char *section,
272 unsigned char *module_mac, size_t module_mac_len,
273 unsigned char *install_mac, size_t install_mac_len)
277 unsigned char *buf1 = NULL, *buf2 = NULL;
281 /* read in the existing values and check they match the saved values */
282 conf = app_load_config(infile);
286 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION);
287 if (s == NULL || strcmp(s, VERSION_VAL) != 0) {
288 BIO_printf(bio_err, "version not found\n");
291 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
293 BIO_printf(bio_err, "Module integrity MAC not found\n");
296 buf1 = OPENSSL_hexstr2buf(s, &len);
298 || (size_t)len != module_mac_len
299 || memcmp(module_mac, buf1, module_mac_len) != 0) {
300 BIO_printf(bio_err, "Module integrity mismatch\n");
303 if (install_mac != NULL && install_mac_len > 0) {
304 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
305 if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
306 BIO_printf(bio_err, "install status not found\n");
309 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
311 BIO_printf(bio_err, "Install indicator MAC not found\n");
314 buf2 = OPENSSL_hexstr2buf(s, &len);
316 || (size_t)len != install_mac_len
317 || memcmp(install_mac, buf2, install_mac_len) != 0) {
318 BIO_printf(bio_err, "Install indicator status mismatch\n");
330 int fipsinstall_main(int argc, char **argv)
332 int ret = 1, verify = 0, gotkey = 0, gotdigest = 0, self_test_onload = 1;
333 int enable_conditional_errors = 1, enable_security_checks = 1;
334 int enable_tls_prf_ems_check = 0; /* This is off by default */
335 int enable_drgb_no_trunc_dgst = 0; /* This is off by default */
336 const char *section_name = "fips_sect";
337 const char *mac_name = "HMAC";
338 const char *prov_name = "fips";
339 BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
340 char *in_fname = NULL, *out_fname = NULL, *prog;
341 char *module_fname = NULL, *parent_config = NULL, *module_path = NULL;
343 EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
344 STACK_OF(OPENSSL_STRING) *opts = NULL;
346 unsigned char *read_buffer = NULL;
347 unsigned char module_mac[EVP_MAX_MD_SIZE];
348 size_t module_mac_len = EVP_MAX_MD_SIZE;
349 unsigned char install_mac[EVP_MAX_MD_SIZE];
350 size_t install_mac_len = EVP_MAX_MD_SIZE;
354 if ((opts = sk_OPENSSL_STRING_new_null()) == NULL)
357 prog = opt_init(argc, argv, fipsinstall_options);
358 while ((o = opt_next()) != OPT_EOF) {
363 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
366 opt_help(fipsinstall_options);
370 in_fname = opt_arg();
373 out_fname = opt_arg();
375 case OPT_NO_CONDITIONAL_ERRORS:
376 enable_conditional_errors = 0;
378 case OPT_NO_SECURITY_CHECKS:
379 enable_security_checks = 0;
381 case OPT_TLS_PRF_EMS_CHECK:
382 enable_tls_prf_ems_check = 1;
384 case OPT_DISALLOW_DRGB_TRUNC_DIGEST:
385 enable_drgb_no_trunc_dgst = 1;
393 case OPT_CORRUPT_DESC:
394 self_test_corrupt_desc = opt_arg();
396 case OPT_CORRUPT_TYPE:
397 self_test_corrupt_type = opt_arg();
400 prov_name = opt_arg();
403 module_fname = opt_arg();
405 case OPT_SECTION_NAME:
406 section_name = opt_arg();
409 mac_name = opt_arg();
412 parent_config = opt_arg();
415 if (!sk_OPENSSL_STRING_push(opts, opt_arg()))
417 if (HAS_PREFIX(opt_arg(), "hexkey:"))
419 else if (HAS_PREFIX(opt_arg(), "digest:"))
425 case OPT_SELF_TEST_ONLOAD:
426 self_test_onload = 1;
428 case OPT_SELF_TEST_ONINSTALL:
429 self_test_onload = 0;
434 /* No extra arguments. */
435 if (!opt_check_rest_arg(NULL))
437 if (verify && in_fname == NULL) {
438 BIO_printf(bio_err, "Missing -in option for -verify\n");
442 if (parent_config != NULL) {
443 /* Test that a parent config can load the module */
444 if (verify_module_load(parent_config)) {
445 ret = OSSL_PROVIDER_available(NULL, prov_name) ? 0 : 1;
447 BIO_printf(bio_err, "FIPS provider is %s\n",
448 ret == 0 ? "available" : " not available");
453 if (module_fname == NULL)
456 tail = opt_path_end(module_fname);
458 module_path = OPENSSL_strdup(module_fname);
459 if (module_path == NULL)
461 module_path[tail - module_fname] = '\0';
462 if (!OSSL_PROVIDER_set_default_search_path(NULL, module_path))
467 || self_test_corrupt_desc != NULL
468 || self_test_corrupt_type != NULL)
469 OSSL_SELF_TEST_set_callback(NULL, self_test_events, NULL);
471 /* Use the default FIPS HMAC digest and key if not specified. */
472 if (!gotdigest && !sk_OPENSSL_STRING_push(opts, "digest:SHA256"))
474 if (!gotkey && !sk_OPENSSL_STRING_push(opts, "hexkey:" FIPS_KEY_STRING))
477 module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY);
478 if (module_bio == NULL) {
479 BIO_printf(bio_err, "Failed to open module file\n");
483 read_buffer = app_malloc(BUFSIZE, "I/O buffer");
484 if (read_buffer == NULL)
487 mac = EVP_MAC_fetch(app_get0_libctx(), mac_name, app_get0_propq());
489 BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
493 ctx = EVP_MAC_CTX_new(mac);
495 BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
502 app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
507 if (!EVP_MAC_CTX_set_params(ctx, params)) {
508 BIO_printf(bio_err, "MAC parameter error\n");
509 ERR_print_errors(bio_err);
512 app_params_free(params);
517 ctx2 = EVP_MAC_CTX_dup(ctx);
519 BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
523 if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
526 if (self_test_onload == 0) {
527 mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
528 strlen(INSTALL_STATUS_VAL));
529 if (mem_bio == NULL) {
530 BIO_printf(bio_err, "Unable to create memory BIO\n");
533 if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
540 if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
541 install_mac, install_mac_len))
544 BIO_printf(bio_err, "VERIFY PASSED\n");
547 conf = generate_config_and_load(prov_name, section_name, module_mac,
549 enable_conditional_errors,
550 enable_security_checks,
551 enable_tls_prf_ems_check,
552 enable_drgb_no_trunc_dgst);
555 if (!load_fips_prov_and_run_self_test(prov_name))
559 out_fname == NULL ? dup_bio_out(FORMAT_TEXT)
560 : bio_open_default(out_fname, 'w', FORMAT_TEXT);
562 BIO_printf(bio_err, "Failed to open file\n");
565 if (!write_config_fips_section(fout, section_name,
566 module_mac, module_mac_len,
567 enable_conditional_errors,
568 enable_security_checks,
569 enable_tls_prf_ems_check,
570 enable_drgb_no_trunc_dgst,
571 install_mac, install_mac_len))
574 BIO_printf(bio_err, "INSTALL PASSED\n");
581 BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
582 ERR_print_errors(bio_err);
586 OPENSSL_free(module_path);
589 BIO_free(module_bio);
590 sk_OPENSSL_STRING_free(opts);
592 EVP_MAC_CTX_free(ctx2);
593 EVP_MAC_CTX_free(ctx);
594 OPENSSL_free(read_buffer);
595 free_config_and_unload(conf);
599 static int self_test_events(const OSSL_PARAM params[], void *arg)
601 const OSSL_PARAM *p = NULL;
602 const char *phase = NULL, *type = NULL, *desc = NULL;
605 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
606 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
608 phase = (const char *)p->data;
610 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
611 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
613 desc = (const char *)p->data;
615 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
616 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
618 type = (const char *)p->data;
621 if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
622 BIO_printf(bio_err, "%s : (%s) : ", desc, type);
623 else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
624 || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
625 BIO_printf(bio_err, "%s\n", phase);
628 * The self test code will internally corrupt the KAT test result if an
629 * error is returned during the corrupt phase.
631 if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0
632 && (self_test_corrupt_desc != NULL
633 || self_test_corrupt_type != NULL)) {
634 if (self_test_corrupt_desc != NULL
635 && strcmp(self_test_corrupt_desc, desc) != 0)
637 if (self_test_corrupt_type != NULL
638 && strcmp(self_test_corrupt_type, type) != 0)
640 BIO_printf(bio_err, "%s ", phase);