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_SELF_TEST_ONLOAD, OPT_SELF_TEST_ONINSTALL
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 {"self_test_oninstall", OPT_SELF_TEST_ONINSTALL, '-',
61 "Forces self tests to run once on module installation"},
63 {"in", OPT_IN, '<', "Input config file, used when verifying"},
65 OPT_SECTION("Output"),
66 {"out", OPT_OUT, '>', "Output config file, used when generating"},
67 {"mac_name", OPT_MAC_NAME, 's', "MAC name"},
68 {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form. "
69 "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
70 {"noout", OPT_NO_LOG, '-', "Disable logging of self test events"},
71 {"corrupt_desc", OPT_CORRUPT_DESC, 's', "Corrupt a self test by description"},
72 {"corrupt_type", OPT_CORRUPT_TYPE, 's', "Corrupt a self test by type"},
73 {"config", OPT_CONFIG, '<', "The parent config to verify"},
74 {"quiet", OPT_QUIET, '-', "No messages, just exit status"},
78 static int do_mac(EVP_MAC_CTX *ctx, unsigned char *tmp, BIO *in,
79 unsigned char *out, size_t *out_len)
83 size_t outsz = *out_len;
85 if (!EVP_MAC_init(ctx, NULL, 0, NULL))
87 if (EVP_MAC_CTX_get_mac_size(ctx) > outsz)
89 while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) {
90 if (i < 0 || !EVP_MAC_update(ctx, tmp, i))
94 if (!EVP_MAC_final(ctx, out, out_len, outsz))
101 static int load_fips_prov_and_run_self_test(const char *prov_name)
104 OSSL_PROVIDER *prov = NULL;
105 OSSL_PARAM params[4], *p = params;
106 char *name = "", *vers = "", *build = "";
108 prov = OSSL_PROVIDER_load(NULL, prov_name);
110 BIO_printf(bio_err, "Failed to load FIPS module\n");
114 *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_NAME,
115 &name, sizeof(name));
116 *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION,
117 &vers, sizeof(vers));
118 *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_BUILDINFO,
119 &build, sizeof(build));
120 *p = OSSL_PARAM_construct_end();
121 if (!OSSL_PROVIDER_get_params(prov, params)) {
122 BIO_printf(bio_err, "Failed to query FIPS module parameters\n");
125 if (OSSL_PARAM_modified(params))
126 BIO_printf(bio_err, "\t%-10s\t%s\n", "name:", name);
127 if (OSSL_PARAM_modified(params + 1))
128 BIO_printf(bio_err, "\t%-10s\t%s\n", "version:", vers);
129 if (OSSL_PARAM_modified(params + 2))
130 BIO_printf(bio_err, "\t%-10s\t%s\n", "build:", build);
134 OSSL_PROVIDER_unload(prov);
138 static int print_mac(BIO *bio, const char *label, const unsigned char *mac,
144 hexstr = OPENSSL_buf2hexstr(mac, (long)len);
147 ret = BIO_printf(bio, "%s = %s\n", label, hexstr);
148 OPENSSL_free(hexstr);
152 static int write_config_header(BIO *out, const char *prov_name,
155 return BIO_printf(out, "openssl_conf = openssl_init\n\n")
156 && BIO_printf(out, "[openssl_init]\n")
157 && BIO_printf(out, "providers = provider_section\n\n")
158 && BIO_printf(out, "[provider_section]\n")
159 && BIO_printf(out, "%s = %s\n\n", prov_name, section);
163 * Outputs a fips related config file that contains entries for the fips
164 * module checksum, installation indicator checksum and the options
165 * conditional_errors and security_checks.
167 * Returns 1 if the config file is written otherwise it returns 0 on error.
169 static int write_config_fips_section(BIO *out, const char *section,
170 unsigned char *module_mac,
171 size_t module_mac_len,
172 int conditional_errors,
174 unsigned char *install_mac,
175 size_t install_mac_len)
179 if (BIO_printf(out, "[%s]\n", section) <= 0
180 || BIO_printf(out, "activate = 1\n") <= 0
181 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
183 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
184 conditional_errors ? "1" : "0") <= 0
185 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
186 security_checks ? "1" : "0") <= 0
187 || !print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
191 if (install_mac != NULL && install_mac_len > 0) {
192 if (!print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
194 || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
195 INSTALL_STATUS_VAL) <= 0)
203 static CONF *generate_config_and_load(const char *prov_name,
205 unsigned char *module_mac,
206 size_t module_mac_len,
207 int conditional_errors,
213 mem_bio = BIO_new(BIO_s_mem());
216 if (!write_config_header(mem_bio, prov_name, section)
217 || !write_config_fips_section(mem_bio, section,
218 module_mac, module_mac_len,
224 conf = app_load_config_bio(mem_bio, NULL);
228 if (CONF_modules_load(conf, NULL, 0) <= 0)
238 static void free_config_and_unload(CONF *conf)
242 CONF_modules_unload(1);
246 static int verify_module_load(const char *parent_config_file)
248 return OSSL_LIB_CTX_load_config(NULL, parent_config_file);
252 * Returns 1 if the config file entries match the passed in module_mac and
253 * install_mac values, otherwise it returns 0.
255 static int verify_config(const char *infile, const char *section,
256 unsigned char *module_mac, size_t module_mac_len,
257 unsigned char *install_mac, size_t install_mac_len)
261 unsigned char *buf1 = NULL, *buf2 = NULL;
265 /* read in the existing values and check they match the saved values */
266 conf = app_load_config(infile);
270 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION);
271 if (s == NULL || strcmp(s, VERSION_VAL) != 0) {
272 BIO_printf(bio_err, "version not found\n");
275 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
277 BIO_printf(bio_err, "Module integrity MAC not found\n");
280 buf1 = OPENSSL_hexstr2buf(s, &len);
282 || (size_t)len != module_mac_len
283 || memcmp(module_mac, buf1, module_mac_len) != 0) {
284 BIO_printf(bio_err, "Module integrity mismatch\n");
287 if (install_mac != NULL && install_mac_len > 0) {
288 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
289 if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
290 BIO_printf(bio_err, "install status not found\n");
293 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
295 BIO_printf(bio_err, "Install indicator MAC not found\n");
298 buf2 = OPENSSL_hexstr2buf(s, &len);
300 || (size_t)len != install_mac_len
301 || memcmp(install_mac, buf2, install_mac_len) != 0) {
302 BIO_printf(bio_err, "Install indicator status mismatch\n");
314 int fipsinstall_main(int argc, char **argv)
316 int ret = 1, verify = 0, gotkey = 0, gotdigest = 0, self_test_onload = 1;
317 int enable_conditional_errors = 1, enable_security_checks = 1;
318 const char *section_name = "fips_sect";
319 const char *mac_name = "HMAC";
320 const char *prov_name = "fips";
321 BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
322 char *in_fname = NULL, *out_fname = NULL, *prog;
323 char *module_fname = NULL, *parent_config = NULL, *module_path = NULL;
325 EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
326 STACK_OF(OPENSSL_STRING) *opts = NULL;
328 unsigned char *read_buffer = NULL;
329 unsigned char module_mac[EVP_MAX_MD_SIZE];
330 size_t module_mac_len = EVP_MAX_MD_SIZE;
331 unsigned char install_mac[EVP_MAX_MD_SIZE];
332 size_t install_mac_len = EVP_MAX_MD_SIZE;
336 if ((opts = sk_OPENSSL_STRING_new_null()) == NULL)
339 prog = opt_init(argc, argv, fipsinstall_options);
340 while ((o = opt_next()) != OPT_EOF) {
345 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
348 opt_help(fipsinstall_options);
352 in_fname = opt_arg();
355 out_fname = opt_arg();
357 case OPT_NO_CONDITIONAL_ERRORS:
358 enable_conditional_errors = 0;
360 case OPT_NO_SECURITY_CHECKS:
361 enable_security_checks = 0;
369 case OPT_CORRUPT_DESC:
370 self_test_corrupt_desc = opt_arg();
372 case OPT_CORRUPT_TYPE:
373 self_test_corrupt_type = opt_arg();
376 prov_name = opt_arg();
379 module_fname = opt_arg();
381 case OPT_SECTION_NAME:
382 section_name = opt_arg();
385 mac_name = opt_arg();
388 parent_config = opt_arg();
391 if (!sk_OPENSSL_STRING_push(opts, opt_arg()))
393 if (HAS_PREFIX(opt_arg(), "hexkey:"))
395 else if (HAS_PREFIX(opt_arg(), "digest:"))
401 case OPT_SELF_TEST_ONLOAD:
402 self_test_onload = 1;
404 case OPT_SELF_TEST_ONINSTALL:
405 self_test_onload = 0;
410 /* No extra arguments. */
411 if (!opt_check_rest_arg(NULL))
413 if (verify && in_fname == NULL) {
414 BIO_printf(bio_err, "Missing -in option for -verify\n");
418 if (parent_config != NULL) {
419 /* Test that a parent config can load the module */
420 if (verify_module_load(parent_config)) {
421 ret = OSSL_PROVIDER_available(NULL, prov_name) ? 0 : 1;
423 BIO_printf(bio_err, "FIPS provider is %s\n",
424 ret == 0 ? "available" : " not available");
429 if (module_fname == NULL)
432 tail = opt_path_end(module_fname);
434 module_path = OPENSSL_strdup(module_fname);
435 if (module_path == NULL)
437 module_path[tail - module_fname] = '\0';
438 if (!OSSL_PROVIDER_set_default_search_path(NULL, module_path))
443 || self_test_corrupt_desc != NULL
444 || self_test_corrupt_type != NULL)
445 OSSL_SELF_TEST_set_callback(NULL, self_test_events, NULL);
447 /* Use the default FIPS HMAC digest and key if not specified. */
448 if (!gotdigest && !sk_OPENSSL_STRING_push(opts, "digest:SHA256"))
450 if (!gotkey && !sk_OPENSSL_STRING_push(opts, "hexkey:" FIPS_KEY_STRING))
453 module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY);
454 if (module_bio == NULL) {
455 BIO_printf(bio_err, "Failed to open module file\n");
459 read_buffer = app_malloc(BUFSIZE, "I/O buffer");
460 if (read_buffer == NULL)
463 mac = EVP_MAC_fetch(app_get0_libctx(), mac_name, app_get0_propq());
465 BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
469 ctx = EVP_MAC_CTX_new(mac);
471 BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
478 app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
483 if (!EVP_MAC_CTX_set_params(ctx, params)) {
484 BIO_printf(bio_err, "MAC parameter error\n");
485 ERR_print_errors(bio_err);
488 app_params_free(params);
493 ctx2 = EVP_MAC_CTX_dup(ctx);
495 BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
499 if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
502 if (self_test_onload == 0) {
503 mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
504 strlen(INSTALL_STATUS_VAL));
505 if (mem_bio == NULL) {
506 BIO_printf(bio_err, "Unable to create memory BIO\n");
509 if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
516 if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
517 install_mac, install_mac_len))
520 BIO_printf(bio_err, "VERIFY PASSED\n");
523 conf = generate_config_and_load(prov_name, section_name, module_mac,
525 enable_conditional_errors,
526 enable_security_checks);
529 if (!load_fips_prov_and_run_self_test(prov_name))
533 out_fname == NULL ? dup_bio_out(FORMAT_TEXT)
534 : bio_open_default(out_fname, 'w', FORMAT_TEXT);
536 BIO_printf(bio_err, "Failed to open file\n");
539 if (!write_config_fips_section(fout, section_name,
540 module_mac, module_mac_len,
541 enable_conditional_errors,
542 enable_security_checks,
543 install_mac, install_mac_len))
546 BIO_printf(bio_err, "INSTALL PASSED\n");
553 BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
554 ERR_print_errors(bio_err);
558 OPENSSL_free(module_path);
561 BIO_free(module_bio);
562 sk_OPENSSL_STRING_free(opts);
564 EVP_MAC_CTX_free(ctx2);
565 EVP_MAC_CTX_free(ctx);
566 OPENSSL_free(read_buffer);
567 free_config_and_unload(conf);
571 static int self_test_events(const OSSL_PARAM params[], void *arg)
573 const OSSL_PARAM *p = NULL;
574 const char *phase = NULL, *type = NULL, *desc = NULL;
577 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
578 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
580 phase = (const char *)p->data;
582 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
583 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
585 desc = (const char *)p->data;
587 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
588 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
590 type = (const char *)p->data;
593 if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
594 BIO_printf(bio_err, "%s : (%s) : ", desc, type);
595 else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
596 || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
597 BIO_printf(bio_err, "%s\n", phase);
600 * The self test code will internally corrupt the KAT test result if an
601 * error is returned during the corrupt phase.
603 if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0
604 && (self_test_corrupt_desc != NULL
605 || self_test_corrupt_type != NULL)) {
606 if (self_test_corrupt_desc != NULL
607 && strcmp(self_test_corrupt_desc, desc) != 0)
609 if (self_test_corrupt_type != NULL
610 && strcmp(self_test_corrupt_type, type) != 0)
612 BIO_printf(bio_err, "%s ", phase);