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,
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"},
61 {"in", OPT_IN, '<', "Input config file, used when verifying"},
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"},
76 static int do_mac(EVP_MAC_CTX *ctx, unsigned char *tmp, BIO *in,
77 unsigned char *out, size_t *out_len)
81 size_t outsz = *out_len;
83 if (!EVP_MAC_init(ctx, NULL, 0, NULL))
85 if (EVP_MAC_CTX_get_mac_size(ctx) > outsz)
87 while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) {
88 if (i < 0 || !EVP_MAC_update(ctx, tmp, i))
92 if (!EVP_MAC_final(ctx, out, out_len, outsz))
99 static int load_fips_prov_and_run_self_test(const char *prov_name)
102 OSSL_PROVIDER *prov = NULL;
104 prov = OSSL_PROVIDER_load(NULL, prov_name);
106 BIO_printf(bio_err, "Failed to load FIPS module\n");
111 OSSL_PROVIDER_unload(prov);
115 static int print_mac(BIO *bio, const char *label, const unsigned char *mac,
121 hexstr = OPENSSL_buf2hexstr(mac, (long)len);
124 ret = BIO_printf(bio, "%s = %s\n", label, hexstr);
125 OPENSSL_free(hexstr);
129 static int write_config_header(BIO *out, const char *prov_name,
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);
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.
144 * Returns 1 if the config file is written otherwise it returns 0 on error.
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,
151 unsigned char *install_mac,
152 size_t install_mac_len)
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,
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,
168 if (install_mac != NULL && install_mac_len > 0) {
169 if (!print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
171 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
172 INSTALL_STATUS_VAL) <= 0)
180 static CONF *generate_config_and_load(const char *prov_name,
182 unsigned char *module_mac,
183 size_t module_mac_len,
184 int conditional_errors,
190 mem_bio = BIO_new(BIO_s_mem());
193 if (!write_config_header(mem_bio, prov_name, section)
194 || !write_config_fips_section(mem_bio, section,
195 module_mac, module_mac_len,
201 conf = app_load_config_bio(mem_bio, NULL);
205 if (CONF_modules_load(conf, NULL, 0) <= 0)
215 static void free_config_and_unload(CONF *conf)
219 CONF_modules_unload(1);
223 static int verify_module_load(const char *parent_config_file)
225 return OSSL_LIB_CTX_load_config(NULL, parent_config_file);
229 * Returns 1 if the config file entries match the passed in module_mac and
230 * install_mac values, otherwise it returns 0.
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)
238 unsigned char *buf1 = NULL, *buf2 = NULL;
242 /* read in the existing values and check they match the saved values */
243 conf = app_load_config(infile);
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");
252 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
254 BIO_printf(bio_err, "Module integrity MAC not found\n");
257 buf1 = OPENSSL_hexstr2buf(s, &len);
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");
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");
270 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
272 BIO_printf(bio_err, "Install indicator MAC not found\n");
275 buf2 = OPENSSL_hexstr2buf(s, &len);
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");
291 int fipsinstall_main(int argc, char **argv)
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;
302 EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
303 STACK_OF(OPENSSL_STRING) *opts = NULL;
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;
313 if ((opts = sk_OPENSSL_STRING_new_null()) == NULL)
316 prog = opt_init(argc, argv, fipsinstall_options);
317 while ((o = opt_next()) != OPT_EOF) {
322 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
325 opt_help(fipsinstall_options);
329 in_fname = opt_arg();
332 out_fname = opt_arg();
334 case OPT_NO_CONDITIONAL_ERRORS:
335 enable_conditional_errors = 0;
337 case OPT_NO_SECURITY_CHECKS:
338 enable_security_checks = 0;
346 case OPT_CORRUPT_DESC:
347 self_test_corrupt_desc = opt_arg();
349 case OPT_CORRUPT_TYPE:
350 self_test_corrupt_type = opt_arg();
353 prov_name = opt_arg();
356 module_fname = opt_arg();
358 case OPT_SECTION_NAME:
359 section_name = opt_arg();
362 mac_name = opt_arg();
365 parent_config = opt_arg();
368 if (!sk_OPENSSL_STRING_push(opts, opt_arg()))
370 if (HAS_PREFIX(opt_arg(), "hexkey:"))
372 else if (HAS_PREFIX(opt_arg(), "digest:"))
378 case OPT_SELF_TEST_ONLOAD:
379 self_test_onload = 1;
384 /* No extra arguments. */
385 argc = opt_num_rest();
386 if (argc != 0 || (verify && in_fname == NULL))
389 if (parent_config != NULL) {
390 /* Test that a parent config can load the module */
391 if (verify_module_load(parent_config)) {
392 ret = OSSL_PROVIDER_available(NULL, prov_name) ? 0 : 1;
394 BIO_printf(bio_err, "FIPS provider is %s\n",
395 ret == 0 ? "available" : " not available");
399 if (module_fname == NULL)
402 tail = opt_path_end(module_fname);
404 module_path = OPENSSL_strdup(module_fname);
405 if (module_path == NULL)
407 module_path[tail - module_fname] = '\0';
408 if (!OSSL_PROVIDER_set_default_search_path(NULL, module_path))
413 || self_test_corrupt_desc != NULL
414 || self_test_corrupt_type != NULL)
415 OSSL_SELF_TEST_set_callback(NULL, self_test_events, NULL);
417 /* Use the default FIPS HMAC digest and key if not specified. */
418 if (!gotdigest && !sk_OPENSSL_STRING_push(opts, "digest:SHA256"))
420 if (!gotkey && !sk_OPENSSL_STRING_push(opts, "hexkey:" FIPS_KEY_STRING))
423 module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY);
424 if (module_bio == NULL) {
425 BIO_printf(bio_err, "Failed to open module file\n");
429 read_buffer = app_malloc(BUFSIZE, "I/O buffer");
430 if (read_buffer == NULL)
433 mac = EVP_MAC_fetch(app_get0_libctx(), mac_name, app_get0_propq());
435 BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
439 ctx = EVP_MAC_CTX_new(mac);
441 BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
448 app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
453 if (!EVP_MAC_CTX_set_params(ctx, params)) {
454 BIO_printf(bio_err, "MAC parameter error\n");
455 ERR_print_errors(bio_err);
458 app_params_free(params);
463 ctx2 = EVP_MAC_CTX_dup(ctx);
465 BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
469 if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
472 if (self_test_onload == 0) {
473 mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
474 strlen(INSTALL_STATUS_VAL));
475 if (mem_bio == NULL) {
476 BIO_printf(bio_err, "Unable to create memory BIO\n");
479 if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
486 if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
487 install_mac, install_mac_len))
490 BIO_printf(bio_err, "VERIFY PASSED\n");
493 conf = generate_config_and_load(prov_name, section_name, module_mac,
495 enable_conditional_errors,
496 enable_security_checks);
499 if (!load_fips_prov_and_run_self_test(prov_name))
503 out_fname == NULL ? dup_bio_out(FORMAT_TEXT)
504 : bio_open_default(out_fname, 'w', FORMAT_TEXT);
506 BIO_printf(bio_err, "Failed to open file\n");
509 if (!write_config_fips_section(fout, section_name,
510 module_mac, module_mac_len,
511 enable_conditional_errors,
512 enable_security_checks,
513 install_mac, install_mac_len))
516 BIO_printf(bio_err, "INSTALL PASSED\n");
523 BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
524 ERR_print_errors(bio_err);
528 OPENSSL_free(module_path);
531 BIO_free(module_bio);
532 sk_OPENSSL_STRING_free(opts);
534 EVP_MAC_CTX_free(ctx2);
535 EVP_MAC_CTX_free(ctx);
536 OPENSSL_free(read_buffer);
537 free_config_and_unload(conf);
541 static int self_test_events(const OSSL_PARAM params[], void *arg)
543 const OSSL_PARAM *p = NULL;
544 const char *phase = NULL, *type = NULL, *desc = NULL;
547 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
548 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
550 phase = (const char *)p->data;
552 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
553 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
555 desc = (const char *)p->data;
557 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
558 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
560 type = (const char *)p->data;
563 if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
564 BIO_printf(bio_err, "%s : (%s) : ", desc, type);
565 else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
566 || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
567 BIO_printf(bio_err, "%s\n", phase);
570 * The self test code will internally corrupt the KAT test result if an
571 * error is returned during the corrupt phase.
573 if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0
574 && (self_test_corrupt_desc != NULL
575 || self_test_corrupt_type != NULL)) {
576 if (self_test_corrupt_desc != NULL
577 && strcmp(self_test_corrupt_desc, desc) != 0)
579 if (self_test_corrupt_type != NULL
580 && strcmp(self_test_corrupt_type, type) != 0)
582 BIO_printf(bio_err, "%s ", phase);