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 OPT_SECTION("General"),
36 {"help", OPT_HELP
, '-', "Display this summary"},
37 {"verify", OPT_VERIFY
, '-',
38 "Verify a config file instead of generating one"},
39 {"module", OPT_MODULE
, '<', "File name of the provider module"},
40 {"provider_name", OPT_PROV_NAME
, 's', "FIPS provider name"},
41 {"section_name", OPT_SECTION_NAME
, 's',
42 "FIPS Provider config section name (optional)"},
45 {"in", OPT_IN
, '<', "Input config file, used when verifying"},
47 OPT_SECTION("Output"),
48 {"out", OPT_OUT
, '>', "Output config file, used when generating"},
49 {"mac_name", OPT_MAC_NAME
, 's', "MAC name"},
50 {"macopt", OPT_MACOPT
, 's', "MAC algorithm parameters in n:v form. "
51 "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
55 static int do_mac(EVP_MAC_CTX
*ctx
, unsigned char *tmp
, BIO
*in
,
56 unsigned char *out
, size_t *out_len
)
60 size_t outsz
= *out_len
;
62 if (!EVP_MAC_init(ctx
))
64 if (EVP_MAC_size(ctx
) > outsz
)
66 while ((i
= BIO_read(in
, (char *)tmp
, BUFSIZE
)) != 0) {
67 if (i
< 0 || !EVP_MAC_update(ctx
, tmp
, i
))
71 if (!EVP_MAC_final(ctx
, out
, out_len
, outsz
))
78 static int load_fips_prov_and_run_self_test(const char *prov_name
)
81 OSSL_PROVIDER
*prov
= NULL
;
83 prov
= OSSL_PROVIDER_load(NULL
, prov_name
);
85 BIO_printf(bio_err
, "Failed to load FIPS module\n");
90 OSSL_PROVIDER_unload(prov
);
94 static int print_mac(BIO
*bio
, const char *label
, const unsigned char *mac
,
100 hexstr
= OPENSSL_buf2hexstr(mac
, (long)len
);
103 ret
= BIO_printf(bio
, "%s = %s\n", label
, hexstr
);
104 OPENSSL_free(hexstr
);
108 static int write_config_header(BIO
*out
, const char *prov_name
,
111 return BIO_printf(out
, "openssl_conf = openssl_init\n\n")
112 && BIO_printf(out
, "[openssl_init]\n")
113 && BIO_printf(out
, "providers = provider_section\n\n")
114 && BIO_printf(out
, "[provider_section]\n")
115 && BIO_printf(out
, "%s = %s\n\n", prov_name
, section
);
119 * Outputs a fips related config file that contains entries for the fips
120 * module checksum and the installation indicator checksum.
122 * Returns 1 if the config file is written otherwise it returns 0 on error.
124 static int write_config_fips_section(BIO
*out
, const char *section
,
125 unsigned char *module_mac
,
126 size_t module_mac_len
,
127 unsigned char *install_mac
,
128 size_t install_mac_len
)
132 if (!(BIO_printf(out
, "[%s]\n", section
) > 0
133 && BIO_printf(out
, "activate = 1\n") > 0
134 && BIO_printf(out
, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION
,
136 && print_mac(out
, OSSL_PROV_FIPS_PARAM_MODULE_MAC
, module_mac
,
140 if (install_mac
!= NULL
) {
141 if (!(print_mac(out
, OSSL_PROV_FIPS_PARAM_INSTALL_MAC
, install_mac
,
143 && BIO_printf(out
, "%s = %s\n",
144 OSSL_PROV_FIPS_PARAM_INSTALL_STATUS
,
145 INSTALL_STATUS_VAL
) > 0))
153 static CONF
*generate_config_and_load(const char *prov_name
,
155 unsigned char *module_mac
,
156 size_t module_mac_len
)
161 mem_bio
= BIO_new(BIO_s_mem());
164 if (!write_config_header(mem_bio
, prov_name
, section
)
165 || !write_config_fips_section(mem_bio
, section
, module_mac
,
166 module_mac_len
, NULL
, 0))
169 conf
= app_load_config_bio(mem_bio
, NULL
);
173 if (CONF_modules_load(conf
, NULL
, 0) <= 0)
183 static void free_config_and_unload(CONF
*conf
)
187 CONF_modules_unload(1);
192 * Returns 1 if the config file entries match the passed in module_mac and
193 * install_mac values, otherwise it returns 0.
195 static int verify_config(const char *infile
, const char *section
,
196 unsigned char *module_mac
, size_t module_mac_len
,
197 unsigned char *install_mac
, size_t install_mac_len
)
201 unsigned char *buf1
= NULL
, *buf2
= NULL
;
205 /* read in the existing values and check they match the saved values */
206 conf
= app_load_config(infile
);
210 s
= NCONF_get_string(conf
, section
, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION
);
211 if (s
== NULL
|| strcmp(s
, VERSION_VAL
) != 0) {
212 BIO_printf(bio_err
, "version not found\n");
215 s
= NCONF_get_string(conf
, section
, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS
);
216 if (s
== NULL
|| strcmp(s
, INSTALL_STATUS_VAL
) != 0) {
217 BIO_printf(bio_err
, "install status not found\n");
220 s
= NCONF_get_string(conf
, section
, OSSL_PROV_FIPS_PARAM_MODULE_MAC
);
222 BIO_printf(bio_err
, "Module integrity MAC not found\n");
225 buf1
= OPENSSL_hexstr2buf(s
, &len
);
227 || (size_t)len
!= module_mac_len
228 || memcmp(module_mac
, buf1
, module_mac_len
) != 0) {
229 BIO_printf(bio_err
, "Module integrity mismatch\n");
232 s
= NCONF_get_string(conf
, section
, OSSL_PROV_FIPS_PARAM_INSTALL_MAC
);
234 BIO_printf(bio_err
, "Install indicator MAC not found\n");
237 buf2
= OPENSSL_hexstr2buf(s
, &len
);
239 || (size_t)len
!= install_mac_len
240 || memcmp(install_mac
, buf2
, install_mac_len
) != 0) {
241 BIO_printf(bio_err
, "Install indicator status mismatch\n");
252 int fipsinstall_main(int argc
, char **argv
)
254 int ret
= 1, verify
= 0;
255 BIO
*module_bio
= NULL
, *mem_bio
= NULL
, *fout
= NULL
;
256 char *in_fname
= NULL
, *out_fname
= NULL
, *prog
, *section_name
= NULL
;
257 char *prov_name
= NULL
, *module_fname
= NULL
;
258 static const char *mac_name
= DEFAULT_MAC_NAME
;
259 EVP_MAC_CTX
*ctx
= NULL
, *ctx2
= NULL
;
260 STACK_OF(OPENSSL_STRING
) *opts
= NULL
;
262 unsigned char *read_buffer
= NULL
;
263 unsigned char module_mac
[EVP_MAX_MD_SIZE
];
264 size_t module_mac_len
= EVP_MAX_MD_SIZE
;
265 unsigned char install_mac
[EVP_MAX_MD_SIZE
];
266 size_t install_mac_len
= EVP_MAX_MD_SIZE
;
270 section_name
= DEFAULT_FIPS_SECTION
;
272 prog
= opt_init(argc
, argv
, fipsinstall_options
);
273 while ((o
= opt_next()) != OPT_EOF
) {
278 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
281 opt_help(fipsinstall_options
);
285 in_fname
= opt_arg();
288 out_fname
= opt_arg();
291 prov_name
= opt_arg();
294 module_fname
= opt_arg();
296 case OPT_SECTION_NAME
:
297 section_name
= opt_arg();
300 mac_name
= opt_arg();
304 opts
= sk_OPENSSL_STRING_new_null();
305 if (opts
== NULL
|| !sk_OPENSSL_STRING_push(opts
, opt_arg()))
313 argc
= opt_num_rest();
314 if (module_fname
== NULL
315 || (verify
&& in_fname
== NULL
)
316 || (!verify
&& (out_fname
== NULL
|| prov_name
== NULL
))
321 module_bio
= bio_open_default(module_fname
, 'r', FORMAT_BINARY
);
322 if (module_bio
== NULL
) {
323 BIO_printf(bio_err
, "Failed to open module file\n");
327 read_buffer
= app_malloc(BUFSIZE
, "I/O buffer");
328 if (read_buffer
== NULL
)
331 mac
= EVP_MAC_fetch(NULL
, mac_name
, NULL
);
333 BIO_printf(bio_err
, "Unable to get MAC of type %s\n", mac_name
);
337 ctx
= EVP_MAC_CTX_new(mac
);
339 BIO_printf(bio_err
, "Unable to create MAC CTX for module check\n");
346 app_params_new_from_opts(opts
, EVP_MAC_settable_ctx_params(mac
));
351 if (!EVP_MAC_CTX_set_params(ctx
, params
)) {
352 BIO_printf(bio_err
, "MAC parameter error\n");
353 ERR_print_errors(bio_err
);
356 app_params_free(params
);
361 ctx2
= EVP_MAC_CTX_dup(ctx
);
363 BIO_printf(bio_err
, "Unable to create MAC CTX for install indicator\n");
367 if (!do_mac(ctx
, read_buffer
, module_bio
, module_mac
, &module_mac_len
))
370 mem_bio
= BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL
,
371 strlen(INSTALL_STATUS_VAL
));
372 if (mem_bio
== NULL
) {
373 BIO_printf(bio_err
, "Unable to create memory BIO\n");
376 if (!do_mac(ctx2
, read_buffer
, mem_bio
, install_mac
, &install_mac_len
))
380 if (!verify_config(in_fname
, section_name
, module_mac
, module_mac_len
,
381 install_mac
, install_mac_len
))
383 BIO_printf(bio_out
, "VERIFY PASSED\n");
386 conf
= generate_config_and_load(prov_name
, section_name
, module_mac
,
390 if (!load_fips_prov_and_run_self_test(prov_name
))
393 fout
= bio_open_default(out_fname
, 'w', FORMAT_TEXT
);
395 BIO_printf(bio_err
, "Failed to open file\n");
398 if (!write_config_fips_section(fout
, section_name
, module_mac
,
399 module_mac_len
, install_mac
,
402 BIO_printf(bio_out
, "INSTALL PASSED\n");
408 BIO_printf(bio_err
, "%s FAILED\n", verify
? "VERIFY" : "INSTALL");
409 ERR_print_errors(bio_err
);
414 BIO_free(module_bio
);
415 sk_OPENSSL_STRING_free(opts
);
417 EVP_MAC_CTX_free(ctx2
);
418 EVP_MAC_CTX_free(ctx
);
419 OPENSSL_free(read_buffer
);
420 free_config_and_unload(conf
);