2 * Copyright 2019-2020 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
11 #include <openssl/evp.h>
12 #include <openssl/err.h>
13 #include <openssl/provider.h>
14 #include <openssl/params.h>
15 #include <openssl/fips_names.h>
16 #include <openssl/core_names.h>
17 #include <openssl/self_test.h>
22 #define DEFAULT_MAC_NAME "HMAC"
23 #define DEFAULT_FIPS_SECTION "fips_check_section"
25 /* Configuration file values */
26 #define VERSION_KEY "version"
27 #define VERSION_VAL "1"
28 #define INSTALL_STATUS_VAL "INSTALL_SELF_TEST_KATS_RUN"
30 static OSSL_CALLBACK self_test_events;
31 static char *self_test_corrupt_desc = NULL;
32 static char *self_test_corrupt_type = NULL;
33 static int self_test_log = 1;
36 typedef enum OPTION_choice {
37 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
38 OPT_IN, OPT_OUT, OPT_MODULE,
39 OPT_PROV_NAME, OPT_SECTION_NAME, OPT_MAC_NAME, OPT_MACOPT, OPT_VERIFY,
40 OPT_NO_LOG, OPT_CORRUPT_DESC, OPT_CORRUPT_TYPE, OPT_QUIET
43 const OPTIONS fipsinstall_options[] = {
44 OPT_SECTION("General"),
45 {"help", OPT_HELP, '-', "Display this summary"},
46 {"verify", OPT_VERIFY, '-',
47 "Verify a config file instead of generating one"},
48 {"module", OPT_MODULE, '<', "File name of the provider module"},
49 {"provider_name", OPT_PROV_NAME, 's', "FIPS provider name"},
50 {"section_name", OPT_SECTION_NAME, 's',
51 "FIPS Provider config section name (optional)"},
54 {"in", OPT_IN, '<', "Input config file, used when verifying"},
56 OPT_SECTION("Output"),
57 {"out", OPT_OUT, '>', "Output config file, used when generating"},
58 {"mac_name", OPT_MAC_NAME, 's', "MAC name"},
59 {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form. "
60 "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
61 {"noout", OPT_NO_LOG, '-', "Disable logging of self test events"},
62 {"corrupt_desc", OPT_CORRUPT_DESC, 's', "Corrupt a self test by description"},
63 {"corrupt_type", OPT_CORRUPT_TYPE, 's', "Corrupt a self test by type"},
64 {"quiet", OPT_QUIET, '-', "No messages, just exit status"},
68 static int do_mac(EVP_MAC_CTX *ctx, unsigned char *tmp, BIO *in,
69 unsigned char *out, size_t *out_len)
73 size_t outsz = *out_len;
75 if (!EVP_MAC_init(ctx))
77 if (EVP_MAC_size(ctx) > outsz)
79 while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) {
80 if (i < 0 || !EVP_MAC_update(ctx, tmp, i))
84 if (!EVP_MAC_final(ctx, out, out_len, outsz))
91 static int load_fips_prov_and_run_self_test(const char *prov_name)
94 OSSL_PROVIDER *prov = NULL;
96 prov = OSSL_PROVIDER_load(NULL, prov_name);
98 BIO_printf(bio_err, "Failed to load FIPS module\n");
103 OSSL_PROVIDER_unload(prov);
107 static int print_mac(BIO *bio, const char *label, const unsigned char *mac,
113 hexstr = OPENSSL_buf2hexstr(mac, (long)len);
116 ret = BIO_printf(bio, "%s = %s\n", label, hexstr);
117 OPENSSL_free(hexstr);
121 static int write_config_header(BIO *out, const char *prov_name,
124 return BIO_printf(out, "openssl_conf = openssl_init\n\n")
125 && BIO_printf(out, "[openssl_init]\n")
126 && BIO_printf(out, "providers = provider_section\n\n")
127 && BIO_printf(out, "[provider_section]\n")
128 && BIO_printf(out, "%s = %s\n\n", prov_name, section);
132 * Outputs a fips related config file that contains entries for the fips
133 * module checksum and the installation indicator checksum.
135 * Returns 1 if the config file is written otherwise it returns 0 on error.
137 static int write_config_fips_section(BIO *out, const char *section,
138 unsigned char *module_mac,
139 size_t module_mac_len,
140 unsigned char *install_mac,
141 size_t install_mac_len)
145 if (!(BIO_printf(out, "[%s]\n", section) > 0
146 && BIO_printf(out, "activate = 1\n") > 0
147 && BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
149 && print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
153 if (install_mac != NULL) {
154 if (!(print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
156 && BIO_printf(out, "%s = %s\n",
157 OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
158 INSTALL_STATUS_VAL) > 0))
166 static CONF *generate_config_and_load(const char *prov_name,
168 unsigned char *module_mac,
169 size_t module_mac_len)
174 mem_bio = BIO_new(BIO_s_mem());
177 if (!write_config_header(mem_bio, prov_name, section)
178 || !write_config_fips_section(mem_bio, section, module_mac,
179 module_mac_len, NULL, 0))
182 conf = app_load_config_bio(mem_bio, NULL);
186 if (CONF_modules_load(conf, NULL, 0) <= 0)
196 static void free_config_and_unload(CONF *conf)
200 CONF_modules_unload(1);
205 * Returns 1 if the config file entries match the passed in module_mac and
206 * install_mac values, otherwise it returns 0.
208 static int verify_config(const char *infile, const char *section,
209 unsigned char *module_mac, size_t module_mac_len,
210 unsigned char *install_mac, size_t install_mac_len)
214 unsigned char *buf1 = NULL, *buf2 = NULL;
218 /* read in the existing values and check they match the saved values */
219 conf = app_load_config(infile);
223 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION);
224 if (s == NULL || strcmp(s, VERSION_VAL) != 0) {
225 BIO_printf(bio_err, "version not found\n");
228 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
229 if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
230 BIO_printf(bio_err, "install status not found\n");
233 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
235 BIO_printf(bio_err, "Module integrity MAC not found\n");
238 buf1 = OPENSSL_hexstr2buf(s, &len);
240 || (size_t)len != module_mac_len
241 || memcmp(module_mac, buf1, module_mac_len) != 0) {
242 BIO_printf(bio_err, "Module integrity mismatch\n");
245 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
247 BIO_printf(bio_err, "Install indicator MAC not found\n");
250 buf2 = OPENSSL_hexstr2buf(s, &len);
252 || (size_t)len != install_mac_len
253 || memcmp(install_mac, buf2, install_mac_len) != 0) {
254 BIO_printf(bio_err, "Install indicator status mismatch\n");
265 int fipsinstall_main(int argc, char **argv)
267 int ret = 1, verify = 0;
268 BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
269 char *in_fname = NULL, *out_fname = NULL, *prog, *section_name = NULL;
270 char *prov_name = NULL, *module_fname = NULL;
271 static const char *mac_name = DEFAULT_MAC_NAME;
272 EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
273 STACK_OF(OPENSSL_STRING) *opts = NULL;
275 unsigned char *read_buffer = NULL;
276 unsigned char module_mac[EVP_MAX_MD_SIZE];
277 size_t module_mac_len = EVP_MAX_MD_SIZE;
278 unsigned char install_mac[EVP_MAX_MD_SIZE];
279 size_t install_mac_len = EVP_MAX_MD_SIZE;
283 section_name = DEFAULT_FIPS_SECTION;
285 prog = opt_init(argc, argv, fipsinstall_options);
286 while ((o = opt_next()) != OPT_EOF) {
291 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
294 opt_help(fipsinstall_options);
298 in_fname = opt_arg();
301 out_fname = opt_arg();
309 case OPT_CORRUPT_DESC:
310 self_test_corrupt_desc = opt_arg();
312 case OPT_CORRUPT_TYPE:
313 self_test_corrupt_type = opt_arg();
316 prov_name = opt_arg();
319 module_fname = opt_arg();
321 case OPT_SECTION_NAME:
322 section_name = opt_arg();
325 mac_name = opt_arg();
329 opts = sk_OPENSSL_STRING_new_null();
330 if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
338 argc = opt_num_rest();
339 if (module_fname == NULL
340 || (verify && in_fname == NULL)
341 || (!verify && (out_fname == NULL || prov_name == NULL))
347 || self_test_corrupt_desc != NULL
348 || self_test_corrupt_type != NULL)
349 OSSL_SELF_TEST_set_callback(NULL, self_test_events, NULL);
351 module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY);
352 if (module_bio == NULL) {
353 BIO_printf(bio_err, "Failed to open module file\n");
357 read_buffer = app_malloc(BUFSIZE, "I/O buffer");
358 if (read_buffer == NULL)
361 mac = EVP_MAC_fetch(NULL, mac_name, NULL);
363 BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
367 ctx = EVP_MAC_CTX_new(mac);
369 BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
376 app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
381 if (!EVP_MAC_CTX_set_params(ctx, params)) {
382 BIO_printf(bio_err, "MAC parameter error\n");
383 ERR_print_errors(bio_err);
386 app_params_free(params);
391 ctx2 = EVP_MAC_CTX_dup(ctx);
393 BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
397 if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
400 mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
401 strlen(INSTALL_STATUS_VAL));
402 if (mem_bio == NULL) {
403 BIO_printf(bio_err, "Unable to create memory BIO\n");
406 if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
410 if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
411 install_mac, install_mac_len))
414 BIO_printf(bio_out, "VERIFY PASSED\n");
417 conf = generate_config_and_load(prov_name, section_name, module_mac,
421 if (!load_fips_prov_and_run_self_test(prov_name))
424 fout = bio_open_default(out_fname, 'w', FORMAT_TEXT);
426 BIO_printf(bio_err, "Failed to open file\n");
429 if (!write_config_fips_section(fout, section_name, module_mac,
430 module_mac_len, install_mac,
434 BIO_printf(bio_out, "INSTALL PASSED\n");
441 BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
442 ERR_print_errors(bio_err);
448 BIO_free(module_bio);
449 sk_OPENSSL_STRING_free(opts);
451 EVP_MAC_CTX_free(ctx2);
452 EVP_MAC_CTX_free(ctx);
453 OPENSSL_free(read_buffer);
454 free_config_and_unload(conf);
458 static int self_test_events(const OSSL_PARAM params[], void *arg)
460 const OSSL_PARAM *p = NULL;
461 const char *phase = NULL, *type = NULL, *desc = NULL;
464 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
465 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
467 phase = (const char *)p->data;
469 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
470 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
472 desc = (const char *)p->data;
474 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
475 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
477 type = (const char *)p->data;
480 if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
481 BIO_printf(bio_out, "%s : (%s) : ", desc, type);
482 else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
483 || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
484 BIO_printf(bio_out, "%s\n", phase);
487 * The self test code will internally corrupt the KAT test result if an
488 * error is returned during the corrupt phase.
490 if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0
491 && (self_test_corrupt_desc != NULL
492 || self_test_corrupt_type != NULL)) {
493 if (self_test_corrupt_desc != NULL
494 && strcmp(self_test_corrupt_desc, desc) != 0)
496 if (self_test_corrupt_type != NULL
497 && strcmp(self_test_corrupt_type, type) != 0)
499 BIO_printf(bio_out, "%s ", phase);