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, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
133 && print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
137 if (install_mac != NULL) {
138 if (!(print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
140 && BIO_printf(out, "%s = %s\n",
141 OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
142 INSTALL_STATUS_VAL) > 0))
150 static CONF *generate_config_and_load(const char *prov_name,
152 unsigned char *module_mac,
153 size_t module_mac_len)
158 mem_bio = BIO_new(BIO_s_mem());
161 if (!write_config_header(mem_bio, prov_name, section)
162 || !write_config_fips_section(mem_bio, section, module_mac,
163 module_mac_len, NULL, 0))
166 conf = app_load_config_bio(mem_bio, NULL);
170 if (!CONF_modules_load(conf, NULL, 0))
180 static void free_config_and_unload(CONF *conf)
184 CONF_modules_unload(1);
189 * Returns 1 if the config file entries match the passed in module_mac and
190 * install_mac values, otherwise it returns 0.
192 static int verify_config(const char *infile, const char *section,
193 unsigned char *module_mac, size_t module_mac_len,
194 unsigned char *install_mac, size_t install_mac_len)
198 unsigned char *buf1 = NULL, *buf2 = NULL;
202 /* read in the existing values and check they match the saved values */
203 conf = app_load_config(infile);
207 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION);
208 if (s == NULL || strcmp(s, VERSION_VAL) != 0) {
209 BIO_printf(bio_err, "version not found\n");
212 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
213 if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
214 BIO_printf(bio_err, "install status not found\n");
217 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
219 BIO_printf(bio_err, "Module integrity MAC not found\n");
222 buf1 = OPENSSL_hexstr2buf(s, &len);
224 || (size_t)len != module_mac_len
225 || memcmp(module_mac, buf1, module_mac_len) != 0) {
226 BIO_printf(bio_err, "Module integrity mismatch\n");
229 s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
231 BIO_printf(bio_err, "Install indicator MAC not found\n");
234 buf2 = OPENSSL_hexstr2buf(s, &len);
236 || (size_t)len != install_mac_len
237 || memcmp(install_mac, buf2, install_mac_len) != 0) {
238 BIO_printf(bio_err, "Install indicator status mismatch\n");
249 int fipsinstall_main(int argc, char **argv)
251 int ret = 1, verify = 0;
252 BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
253 char *in_fname = NULL, *out_fname = NULL, *prog, *section_name = NULL;
254 char *prov_name = NULL, *module_fname = NULL;
255 static const char *mac_name = DEFAULT_MAC_NAME;
256 EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
257 STACK_OF(OPENSSL_STRING) *opts = NULL;
259 unsigned char *read_buffer = NULL;
260 unsigned char module_mac[EVP_MAX_MD_SIZE];
261 size_t module_mac_len = EVP_MAX_MD_SIZE;
262 unsigned char install_mac[EVP_MAX_MD_SIZE];
263 size_t install_mac_len = EVP_MAX_MD_SIZE;
267 section_name = DEFAULT_FIPS_SECTION;
269 prog = opt_init(argc, argv, fipsinstall_options);
270 while ((o = opt_next()) != OPT_EOF) {
275 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
278 opt_help(fipsinstall_options);
282 in_fname = opt_arg();
285 out_fname = opt_arg();
288 prov_name = opt_arg();
291 module_fname = opt_arg();
293 case OPT_SECTION_NAME:
294 section_name = opt_arg();
297 mac_name = opt_arg();
301 opts = sk_OPENSSL_STRING_new_null();
302 if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
310 argc = opt_num_rest();
311 if (module_fname == NULL
312 || (verify && in_fname == NULL)
313 || (!verify && (out_fname == NULL || prov_name == NULL))
318 module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY);
319 if (module_bio == NULL) {
320 BIO_printf(bio_err, "Failed to open module file\n");
324 read_buffer = app_malloc(BUFSIZE, "I/O buffer");
325 if (read_buffer == NULL)
328 mac = EVP_MAC_fetch(NULL, mac_name, NULL);
330 BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
334 ctx = EVP_MAC_CTX_new(mac);
336 BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
343 app_params_new_from_opts(opts, EVP_MAC_CTX_settable_params(mac));
348 if (!EVP_MAC_CTX_set_params(ctx, params)) {
349 BIO_printf(bio_err, "MAC parameter error\n");
350 ERR_print_errors(bio_err);
353 app_params_free(params);
358 ctx2 = EVP_MAC_CTX_dup(ctx);
360 BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
364 if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
367 mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
368 strlen(INSTALL_STATUS_VAL));
369 if (mem_bio == NULL) {
370 BIO_printf(bio_err, "Unable to create memory BIO\n");
373 if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
377 if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
378 install_mac, install_mac_len))
380 BIO_printf(bio_out, "VERIFY PASSED\n");
383 conf = generate_config_and_load(prov_name, section_name, module_mac,
387 if (!load_fips_prov_and_run_self_test(prov_name))
390 fout = bio_open_default(out_fname, 'w', FORMAT_TEXT);
392 BIO_printf(bio_err, "Failed to open file\n");
395 if (!write_config_fips_section(fout, section_name, module_mac,
396 module_mac_len, install_mac,
399 BIO_printf(bio_out, "INSTALL PASSED\n");
405 BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
406 ERR_print_errors(bio_err);
411 BIO_free(module_bio);
412 sk_OPENSSL_STRING_free(opts);
414 EVP_MAC_CTX_free(ctx2);
415 EVP_MAC_CTX_free(ctx);
416 OPENSSL_free(read_buffer);
417 free_config_and_unload(conf);