2 * Copyright 2019 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;
35 typedef enum OPTION_choice {
36 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
37 OPT_IN, OPT_OUT, OPT_MODULE,
38 OPT_PROV_NAME, OPT_SECTION_NAME, OPT_MAC_NAME, OPT_MACOPT, OPT_VERIFY,
39 OPT_NO_LOG, OPT_CORRUPT_DESC, OPT_CORRUPT_TYPE
42 const OPTIONS fipsinstall_options[] = {
43 OPT_SECTION("General"),
44 {"help", OPT_HELP, '-', "Display this summary"},
45 {"verify", OPT_VERIFY, '-',
46 "Verify a config file instead of generating one"},
47 {"module", OPT_MODULE, '<', "File name of the provider module"},
48 {"provider_name", OPT_PROV_NAME, 's', "FIPS provider name"},
49 {"section_name", OPT_SECTION_NAME, 's',
50 "FIPS Provider config section name (optional)"},
53 {"in", OPT_IN, '<', "Input config file, used when verifying"},
55 OPT_SECTION("Output"),
56 {"out", OPT_OUT, '>', "Output config file, used when generating"},
57 {"mac_name", OPT_MAC_NAME, 's', "MAC name"},
58 {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form. "
59 "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
60 {"noout", OPT_NO_LOG, '-', "Disable logging of self test events"},
61 {"corrupt_desc", OPT_CORRUPT_DESC, 's', "Corrupt a self test by description"},
62 {"corrupt_type", OPT_CORRUPT_TYPE, 's', "Corrupt a self test by type"},
66 static int do_mac(EVP_MAC_CTX *ctx, unsigned char *tmp, BIO *in,
67 unsigned char *out, size_t *out_len)
71 size_t outsz = *out_len;
73 if (!EVP_MAC_init(ctx))
75 if (EVP_MAC_size(ctx) > outsz)
77 while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) {
78 if (i < 0 || !EVP_MAC_update(ctx, tmp, i))
82 if (!EVP_MAC_final(ctx, out, out_len, outsz))
89 static int load_fips_prov_and_run_self_test(const char *prov_name)
92 OSSL_PROVIDER *prov = NULL;
94 prov = OSSL_PROVIDER_load(NULL, prov_name);
96 BIO_printf(bio_err, "Failed to load FIPS module\n");
101 OSSL_PROVIDER_unload(prov);
105 static int print_mac(BIO *bio, const char *label, const unsigned char *mac,
111 hexstr = OPENSSL_buf2hexstr(mac, (long)len);
114 ret = BIO_printf(bio, "%s = %s\n", label, hexstr);
115 OPENSSL_free(hexstr);
119 static int write_config_header(BIO *out, const char *prov_name,
122 return BIO_printf(out, "openssl_conf = openssl_init\n\n")
123 && BIO_printf(out, "[openssl_init]\n")
124 && BIO_printf(out, "providers = provider_section\n\n")
125 && BIO_printf(out, "[provider_section]\n")
126 && BIO_printf(out, "%s = %s\n\n", prov_name, section);
130 * Outputs a fips related config file that contains entries for the fips
131 * module checksum and the installation indicator checksum.
133 * Returns 1 if the config file is written otherwise it returns 0 on error.
135 static int write_config_fips_section(BIO *out, const char *section,
136 unsigned char *module_mac,
137 size_t module_mac_len,
138 unsigned char *install_mac,
139 size_t install_mac_len)
143 if (!(BIO_printf(out, "[%s]\n", section) > 0
144 && BIO_printf(out, "activate = 1\n") > 0
145 && BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
147 && print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
151 if (install_mac != NULL) {
152 if (!(print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
154 && BIO_printf(out, "%s = %s\n",
155 OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
156 INSTALL_STATUS_VAL) > 0))
164 static CONF *generate_config_and_load(const char *prov_name,
166 unsigned char *module_mac,
167 size_t module_mac_len)
172 mem_bio = BIO_new(BIO_s_mem());
175 if (!write_config_header(mem_bio, prov_name, section)
176 || !write_config_fips_section(mem_bio, section, module_mac,
177 module_mac_len, NULL, 0))
180 conf = app_load_config_bio(mem_bio, NULL);
184 if (CONF_modules_load(conf, NULL, 0) <= 0)
194 static void free_config_and_unload(CONF *conf)
198 CONF_modules_unload(1);
203 * Returns 1 if the config file entries match the passed in module_mac and
204 * install_mac values, otherwise it returns 0.
206 static int verify_config(const char *infile, const char *section,
207 unsigned char *module_mac, size_t module_mac_len,
208 unsigned char *install_mac, size_t install_mac_len)
212 unsigned char *buf1 = NULL, *buf2 = NULL;
216 /* read in the existing values and check they match the saved values */
217 conf = app_load_config(infile);
221 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION);
222 if (s == NULL || strcmp(s, VERSION_VAL) != 0) {
223 BIO_printf(bio_err, "version not found\n");
226 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
227 if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
228 BIO_printf(bio_err, "install status not found\n");
231 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
233 BIO_printf(bio_err, "Module integrity MAC not found\n");
236 buf1 = OPENSSL_hexstr2buf(s, &len);
238 || (size_t)len != module_mac_len
239 || memcmp(module_mac, buf1, module_mac_len) != 0) {
240 BIO_printf(bio_err, "Module integrity mismatch\n");
243 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
245 BIO_printf(bio_err, "Install indicator MAC not found\n");
248 buf2 = OPENSSL_hexstr2buf(s, &len);
250 || (size_t)len != install_mac_len
251 || memcmp(install_mac, buf2, install_mac_len) != 0) {
252 BIO_printf(bio_err, "Install indicator status mismatch\n");
263 int fipsinstall_main(int argc, char **argv)
265 int ret = 1, verify = 0;
266 BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
267 char *in_fname = NULL, *out_fname = NULL, *prog, *section_name = NULL;
268 char *prov_name = NULL, *module_fname = NULL;
269 static const char *mac_name = DEFAULT_MAC_NAME;
270 EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
271 STACK_OF(OPENSSL_STRING) *opts = NULL;
273 unsigned char *read_buffer = NULL;
274 unsigned char module_mac[EVP_MAX_MD_SIZE];
275 size_t module_mac_len = EVP_MAX_MD_SIZE;
276 unsigned char install_mac[EVP_MAX_MD_SIZE];
277 size_t install_mac_len = EVP_MAX_MD_SIZE;
281 section_name = DEFAULT_FIPS_SECTION;
283 prog = opt_init(argc, argv, fipsinstall_options);
284 while ((o = opt_next()) != OPT_EOF) {
289 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
292 opt_help(fipsinstall_options);
296 in_fname = opt_arg();
299 out_fname = opt_arg();
304 case OPT_CORRUPT_DESC:
305 self_test_corrupt_desc = opt_arg();
307 case OPT_CORRUPT_TYPE:
308 self_test_corrupt_type = opt_arg();
311 prov_name = opt_arg();
314 module_fname = opt_arg();
316 case OPT_SECTION_NAME:
317 section_name = opt_arg();
320 mac_name = opt_arg();
324 opts = sk_OPENSSL_STRING_new_null();
325 if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
333 argc = opt_num_rest();
334 if (module_fname == NULL
335 || (verify && in_fname == NULL)
336 || (!verify && (out_fname == NULL || prov_name == NULL))
342 || self_test_corrupt_desc != NULL
343 || self_test_corrupt_type != NULL)
344 OSSL_SELF_TEST_set_callback(NULL, self_test_events, NULL);
346 module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY);
347 if (module_bio == NULL) {
348 BIO_printf(bio_err, "Failed to open module file\n");
352 read_buffer = app_malloc(BUFSIZE, "I/O buffer");
353 if (read_buffer == NULL)
356 mac = EVP_MAC_fetch(NULL, mac_name, NULL);
358 BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
362 ctx = EVP_MAC_CTX_new(mac);
364 BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
371 app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
376 if (!EVP_MAC_CTX_set_params(ctx, params)) {
377 BIO_printf(bio_err, "MAC parameter error\n");
378 ERR_print_errors(bio_err);
381 app_params_free(params);
386 ctx2 = EVP_MAC_CTX_dup(ctx);
388 BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
392 if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
395 mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
396 strlen(INSTALL_STATUS_VAL));
397 if (mem_bio == NULL) {
398 BIO_printf(bio_err, "Unable to create memory BIO\n");
401 if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
405 if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
406 install_mac, install_mac_len))
408 BIO_printf(bio_out, "VERIFY PASSED\n");
411 conf = generate_config_and_load(prov_name, section_name, module_mac,
415 if (!load_fips_prov_and_run_self_test(prov_name))
418 fout = bio_open_default(out_fname, 'w', FORMAT_TEXT);
420 BIO_printf(bio_err, "Failed to open file\n");
423 if (!write_config_fips_section(fout, section_name, module_mac,
424 module_mac_len, install_mac,
427 BIO_printf(bio_out, "INSTALL PASSED\n");
433 BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
434 ERR_print_errors(bio_err);
439 BIO_free(module_bio);
440 sk_OPENSSL_STRING_free(opts);
442 EVP_MAC_CTX_free(ctx2);
443 EVP_MAC_CTX_free(ctx);
444 OPENSSL_free(read_buffer);
445 free_config_and_unload(conf);
449 static int self_test_events(const OSSL_PARAM params[], void *arg)
451 const OSSL_PARAM *p = NULL;
452 const char *phase = NULL, *type = NULL, *desc = NULL;
455 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
456 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
458 phase = (const char *)p->data;
460 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
461 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
463 desc = (const char *)p->data;
465 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
466 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
468 type = (const char *)p->data;
471 if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
472 BIO_printf(bio_out, "%s : (%s) : ", desc, type);
473 else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
474 || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
475 BIO_printf(bio_out, "%s\n", phase);
478 * The self test code will internally corrupt the KAT test result if an
479 * error is returned during the corrupt phase.
481 if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0
482 && (self_test_corrupt_desc != NULL
483 || self_test_corrupt_type != NULL)) {
484 if (self_test_corrupt_desc != NULL
485 && strcmp(self_test_corrupt_desc, desc) != 0)
487 if (self_test_corrupt_type != NULL
488 && strcmp(self_test_corrupt_type, type) != 0)
490 BIO_printf(bio_out, "%s ", phase);