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>
20 #define DEFAULT_MAC_NAME "HMAC"
21 #define DEFAULT_FIPS_SECTION "fips_check_section"
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 typedef enum OPTION_choice {
29 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
30 OPT_IN, OPT_OUT, OPT_MODULE,
31 OPT_PROV_NAME, OPT_SECTION_NAME, OPT_MAC_NAME, OPT_MACOPT, OPT_VERIFY
34 const OPTIONS fipsinstall_options[] = {
35 {"help", OPT_HELP, '-', "Display this summary"},
36 {OPT_MORE_STR, 0, 0, "e.g: openssl fipsinstall -provider_name fips"
37 "-section_name fipsinstall -out fips.conf -module ./fips.so"
38 "-mac_name HMAC -macopt digest:SHA256 -macopt hexkey:00"},
39 {"verify", OPT_VERIFY, '-', "Verification mode, i.e verify a config file "
40 "instead of generating one"},
41 {"in", OPT_IN, '<', "Input config file, used when verifying"},
42 {"out", OPT_OUT, '>', "Output config file, used when generating"},
43 {"module", OPT_MODULE, '<', "File name of the provider module"},
44 {"provider_name", OPT_PROV_NAME, 's', "FIPS provider name"},
45 {"section_name", OPT_SECTION_NAME, 's',
46 "FIPS Provider config section name (optional)"},
47 {"mac_name", OPT_MAC_NAME, 's', "MAC name"},
48 {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form. "
49 "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
53 static int do_mac(EVP_MAC_CTX *ctx, unsigned char *tmp, BIO *in,
54 unsigned char *out, size_t *out_len)
58 size_t outsz = *out_len;
60 if (!EVP_MAC_init(ctx))
62 if (EVP_MAC_size(ctx) > outsz)
64 while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) {
65 if (i < 0 || !EVP_MAC_update(ctx, tmp, i))
69 if (!EVP_MAC_final(ctx, out, out_len, outsz))
76 static int load_fips_prov_and_run_self_test(const char *prov_name)
79 OSSL_PROVIDER *prov = NULL;
81 prov = OSSL_PROVIDER_load(NULL, prov_name);
83 BIO_printf(bio_err, "Failed to load FIPS module\n");
88 OSSL_PROVIDER_unload(prov);
92 static int print_mac(BIO *bio, const char *label, const unsigned char *mac,
98 hexstr = OPENSSL_buf2hexstr(mac, (long)len);
101 ret = BIO_printf(bio, "%s = %s\n", label, hexstr);
102 OPENSSL_free(hexstr);
106 static int write_config_header(BIO *out, const char *prov_name,
109 return BIO_printf(out, "openssl_conf = openssl_init\n\n")
110 && BIO_printf(out, "[openssl_init]\n")
111 && BIO_printf(out, "providers = provider_section\n\n")
112 && BIO_printf(out, "[provider_section]\n")
113 && BIO_printf(out, "%s = %s\n\n", prov_name, section);
117 * Outputs a fips related config file that contains entries for the fips
118 * module checksum and the installation indicator checksum.
120 * Returns 1 if the config file is written otherwise it returns 0 on error.
122 static int write_config_fips_section(BIO *out, const char *section,
123 unsigned char *module_mac,
124 size_t module_mac_len,
125 unsigned char *install_mac,
126 size_t install_mac_len)
130 if (!(BIO_printf(out, "[%s]\n", section) > 0
131 && BIO_printf(out, "activate = 1\n") > 0
132 && BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
134 && print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
138 if (install_mac != NULL) {
139 if (!(print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
141 && BIO_printf(out, "%s = %s\n",
142 OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
143 INSTALL_STATUS_VAL) > 0))
151 static CONF *generate_config_and_load(const char *prov_name,
153 unsigned char *module_mac,
154 size_t module_mac_len)
159 mem_bio = BIO_new(BIO_s_mem());
162 if (!write_config_header(mem_bio, prov_name, section)
163 || !write_config_fips_section(mem_bio, section, module_mac,
164 module_mac_len, NULL, 0))
167 conf = app_load_config_bio(mem_bio, NULL);
171 if (!CONF_modules_load(conf, NULL, 0))
181 static void free_config_and_unload(CONF *conf)
185 CONF_modules_unload(1);
190 * Returns 1 if the config file entries match the passed in module_mac and
191 * install_mac values, otherwise it returns 0.
193 static int verify_config(const char *infile, const char *section,
194 unsigned char *module_mac, size_t module_mac_len,
195 unsigned char *install_mac, size_t install_mac_len)
199 unsigned char *buf1 = NULL, *buf2 = NULL;
203 /* read in the existing values and check they match the saved values */
204 conf = app_load_config(infile);
208 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION);
209 if (s == NULL || strcmp(s, VERSION_VAL) != 0) {
210 BIO_printf(bio_err, "version not found\n");
213 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
214 if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
215 BIO_printf(bio_err, "install status not found\n");
218 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
220 BIO_printf(bio_err, "Module integrity MAC not found\n");
223 buf1 = OPENSSL_hexstr2buf(s, &len);
225 || (size_t)len != module_mac_len
226 || memcmp(module_mac, buf1, module_mac_len) != 0) {
227 BIO_printf(bio_err, "Module integrity mismatch\n");
230 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
232 BIO_printf(bio_err, "Install indicator MAC not found\n");
235 buf2 = OPENSSL_hexstr2buf(s, &len);
237 || (size_t)len != install_mac_len
238 || memcmp(install_mac, buf2, install_mac_len) != 0) {
239 BIO_printf(bio_err, "Install indicator status mismatch\n");
250 int fipsinstall_main(int argc, char **argv)
252 int ret = 1, verify = 0;
253 BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
254 char *in_fname = NULL, *out_fname = NULL, *prog, *section_name = NULL;
255 char *prov_name = NULL, *module_fname = NULL;
256 static const char *mac_name = DEFAULT_MAC_NAME;
257 EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
258 STACK_OF(OPENSSL_STRING) *opts = NULL;
260 unsigned char *read_buffer = NULL;
261 unsigned char module_mac[EVP_MAX_MD_SIZE];
262 size_t module_mac_len = EVP_MAX_MD_SIZE;
263 unsigned char install_mac[EVP_MAX_MD_SIZE];
264 size_t install_mac_len = EVP_MAX_MD_SIZE;
268 section_name = DEFAULT_FIPS_SECTION;
270 prog = opt_init(argc, argv, fipsinstall_options);
271 while ((o = opt_next()) != OPT_EOF) {
276 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
279 opt_help(fipsinstall_options);
283 in_fname = opt_arg();
286 out_fname = opt_arg();
289 prov_name = opt_arg();
292 module_fname = opt_arg();
294 case OPT_SECTION_NAME:
295 section_name = opt_arg();
298 mac_name = opt_arg();
302 opts = sk_OPENSSL_STRING_new_null();
303 if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
311 argc = opt_num_rest();
312 if (module_fname == NULL
313 || (verify && in_fname == NULL)
314 || (!verify && (out_fname == NULL || prov_name == NULL))
319 module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY);
320 if (module_bio == NULL) {
321 BIO_printf(bio_err, "Failed to open module file\n");
325 read_buffer = app_malloc(BUFSIZE, "I/O buffer");
326 if (read_buffer == NULL)
329 mac = EVP_MAC_fetch(NULL, mac_name, NULL);
331 BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
335 ctx = EVP_MAC_CTX_new(mac);
337 BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
344 app_params_new_from_opts(opts, EVP_MAC_CTX_settable_params(mac));
349 if (!EVP_MAC_CTX_set_params(ctx, params)) {
350 BIO_printf(bio_err, "MAC parameter error\n");
351 ERR_print_errors(bio_err);
354 app_params_free(params);
359 ctx2 = EVP_MAC_CTX_dup(ctx);
361 BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
365 if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
368 mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
369 strlen(INSTALL_STATUS_VAL));
370 if (mem_bio == NULL) {
371 BIO_printf(bio_err, "Unable to create memory BIO\n");
374 if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
378 if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
379 install_mac, install_mac_len))
381 BIO_printf(bio_out, "VERIFY PASSED\n");
384 conf = generate_config_and_load(prov_name, section_name, module_mac,
388 if (!load_fips_prov_and_run_self_test(prov_name))
391 fout = bio_open_default(out_fname, 'w', FORMAT_TEXT);
393 BIO_printf(bio_err, "Failed to open file\n");
396 if (!write_config_fips_section(fout, section_name, module_mac,
397 module_mac_len, install_mac,
400 BIO_printf(bio_out, "INSTALL PASSED\n");
406 BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
407 ERR_print_errors(bio_err);
412 BIO_free(module_bio);
413 sk_OPENSSL_STRING_free(opts);
415 EVP_MAC_CTX_free(ctx2);
416 EVP_MAC_CTX_free(ctx);
417 OPENSSL_free(read_buffer);
418 free_config_and_unload(conf);