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>
18 #include <openssl/fipskey.h>
22 DEFINE_STACK_OF_STRING()
25 #define DEFAULT_MAC_NAME "HMAC"
26 #define DEFAULT_FIPS_SECTION "fips_check_section"
28 /* Configuration file values */
29 #define VERSION_KEY "version"
30 #define VERSION_VAL "1"
31 #define INSTALL_STATUS_VAL "INSTALL_SELF_TEST_KATS_RUN"
33 static OSSL_CALLBACK self_test_events
;
34 static char *self_test_corrupt_desc
= NULL
;
35 static char *self_test_corrupt_type
= NULL
;
36 static int self_test_log
= 1;
39 typedef enum OPTION_choice
{
40 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
41 OPT_IN
, OPT_OUT
, OPT_MODULE
,
42 OPT_PROV_NAME
, OPT_SECTION_NAME
, OPT_MAC_NAME
, OPT_MACOPT
, OPT_VERIFY
,
43 OPT_NO_LOG
, OPT_CORRUPT_DESC
, OPT_CORRUPT_TYPE
, OPT_QUIET
46 const OPTIONS fipsinstall_options
[] = {
47 OPT_SECTION("General"),
48 {"help", OPT_HELP
, '-', "Display this summary"},
49 {"verify", OPT_VERIFY
, '-',
50 "Verify a config file instead of generating one"},
51 {"module", OPT_MODULE
, '<', "File name of the provider module"},
52 {"provider_name", OPT_PROV_NAME
, 's', "FIPS provider name"},
53 {"section_name", OPT_SECTION_NAME
, 's',
54 "FIPS Provider config section name (optional)"},
57 {"in", OPT_IN
, '<', "Input config file, used when verifying"},
59 OPT_SECTION("Output"),
60 {"out", OPT_OUT
, '>', "Output config file, used when generating"},
61 {"mac_name", OPT_MAC_NAME
, 's', "MAC name"},
62 {"macopt", OPT_MACOPT
, 's', "MAC algorithm parameters in n:v form. "
63 "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
64 {"noout", OPT_NO_LOG
, '-', "Disable logging of self test events"},
65 {"corrupt_desc", OPT_CORRUPT_DESC
, 's', "Corrupt a self test by description"},
66 {"corrupt_type", OPT_CORRUPT_TYPE
, 's', "Corrupt a self test by type"},
67 {"quiet", OPT_QUIET
, '-', "No messages, just exit status"},
71 static int do_mac(EVP_MAC_CTX
*ctx
, unsigned char *tmp
, BIO
*in
,
72 unsigned char *out
, size_t *out_len
)
76 size_t outsz
= *out_len
;
78 if (!EVP_MAC_init(ctx
))
80 if (EVP_MAC_size(ctx
) > outsz
)
82 while ((i
= BIO_read(in
, (char *)tmp
, BUFSIZE
)) != 0) {
83 if (i
< 0 || !EVP_MAC_update(ctx
, tmp
, i
))
87 if (!EVP_MAC_final(ctx
, out
, out_len
, outsz
))
94 static int load_fips_prov_and_run_self_test(const char *prov_name
)
97 OSSL_PROVIDER
*prov
= NULL
;
99 prov
= OSSL_PROVIDER_load(NULL
, prov_name
);
101 BIO_printf(bio_err
, "Failed to load FIPS module\n");
106 OSSL_PROVIDER_unload(prov
);
110 static int print_mac(BIO
*bio
, const char *label
, const unsigned char *mac
,
116 hexstr
= OPENSSL_buf2hexstr(mac
, (long)len
);
119 ret
= BIO_printf(bio
, "%s = %s\n", label
, hexstr
);
120 OPENSSL_free(hexstr
);
124 static int write_config_header(BIO
*out
, const char *prov_name
,
127 return BIO_printf(out
, "openssl_conf = openssl_init\n\n")
128 && BIO_printf(out
, "[openssl_init]\n")
129 && BIO_printf(out
, "providers = provider_section\n\n")
130 && BIO_printf(out
, "[provider_section]\n")
131 && BIO_printf(out
, "%s = %s\n\n", prov_name
, section
);
135 * Outputs a fips related config file that contains entries for the fips
136 * module checksum and the installation indicator checksum.
138 * Returns 1 if the config file is written otherwise it returns 0 on error.
140 static int write_config_fips_section(BIO
*out
, const char *section
,
141 unsigned char *module_mac
,
142 size_t module_mac_len
,
143 unsigned char *install_mac
,
144 size_t install_mac_len
)
148 if (!(BIO_printf(out
, "[%s]\n", section
) > 0
149 && BIO_printf(out
, "activate = 1\n") > 0
150 && BIO_printf(out
, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION
,
152 && print_mac(out
, OSSL_PROV_FIPS_PARAM_MODULE_MAC
, module_mac
,
156 if (install_mac
!= NULL
) {
157 if (!(print_mac(out
, OSSL_PROV_FIPS_PARAM_INSTALL_MAC
, install_mac
,
159 && BIO_printf(out
, "%s = %s\n",
160 OSSL_PROV_FIPS_PARAM_INSTALL_STATUS
,
161 INSTALL_STATUS_VAL
) > 0))
169 static CONF
*generate_config_and_load(const char *prov_name
,
171 unsigned char *module_mac
,
172 size_t module_mac_len
)
177 mem_bio
= BIO_new(BIO_s_mem());
180 if (!write_config_header(mem_bio
, prov_name
, section
)
181 || !write_config_fips_section(mem_bio
, section
, module_mac
,
182 module_mac_len
, NULL
, 0))
185 conf
= app_load_config_bio(mem_bio
, NULL
);
189 if (CONF_modules_load(conf
, NULL
, 0) <= 0)
199 static void free_config_and_unload(CONF
*conf
)
203 CONF_modules_unload(1);
208 * Returns 1 if the config file entries match the passed in module_mac and
209 * install_mac values, otherwise it returns 0.
211 static int verify_config(const char *infile
, const char *section
,
212 unsigned char *module_mac
, size_t module_mac_len
,
213 unsigned char *install_mac
, size_t install_mac_len
)
217 unsigned char *buf1
= NULL
, *buf2
= NULL
;
221 /* read in the existing values and check they match the saved values */
222 conf
= app_load_config(infile
);
226 s
= NCONF_get_string(conf
, section
, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION
);
227 if (s
== NULL
|| strcmp(s
, VERSION_VAL
) != 0) {
228 BIO_printf(bio_err
, "version not found\n");
231 s
= NCONF_get_string(conf
, section
, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS
);
232 if (s
== NULL
|| strcmp(s
, INSTALL_STATUS_VAL
) != 0) {
233 BIO_printf(bio_err
, "install status not found\n");
236 s
= NCONF_get_string(conf
, section
, OSSL_PROV_FIPS_PARAM_MODULE_MAC
);
238 BIO_printf(bio_err
, "Module integrity MAC not found\n");
241 buf1
= OPENSSL_hexstr2buf(s
, &len
);
243 || (size_t)len
!= module_mac_len
244 || memcmp(module_mac
, buf1
, module_mac_len
) != 0) {
245 BIO_printf(bio_err
, "Module integrity mismatch\n");
248 s
= NCONF_get_string(conf
, section
, OSSL_PROV_FIPS_PARAM_INSTALL_MAC
);
250 BIO_printf(bio_err
, "Install indicator MAC not found\n");
253 buf2
= OPENSSL_hexstr2buf(s
, &len
);
255 || (size_t)len
!= install_mac_len
256 || memcmp(install_mac
, buf2
, install_mac_len
) != 0) {
257 BIO_printf(bio_err
, "Install indicator status mismatch\n");
268 int fipsinstall_main(int argc
, char **argv
)
270 int ret
= 1, verify
= 0, gotkey
= 0, gotdigest
= 0;
271 BIO
*module_bio
= NULL
, *mem_bio
= NULL
, *fout
= NULL
;
272 char *in_fname
= NULL
, *out_fname
= NULL
, *prog
, *section_name
= NULL
;
273 char *prov_name
= NULL
, *module_fname
= NULL
;
274 static const char *mac_name
= DEFAULT_MAC_NAME
;
275 EVP_MAC_CTX
*ctx
= NULL
, *ctx2
= NULL
;
276 STACK_OF(OPENSSL_STRING
) *opts
= NULL
;
278 unsigned char *read_buffer
= NULL
;
279 unsigned char module_mac
[EVP_MAX_MD_SIZE
];
280 size_t module_mac_len
= EVP_MAX_MD_SIZE
;
281 unsigned char install_mac
[EVP_MAX_MD_SIZE
];
282 size_t install_mac_len
= EVP_MAX_MD_SIZE
;
286 section_name
= DEFAULT_FIPS_SECTION
;
287 if ((opts
= sk_OPENSSL_STRING_new_null()) == NULL
)
290 prog
= opt_init(argc
, argv
, fipsinstall_options
);
291 while ((o
= opt_next()) != OPT_EOF
) {
296 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
299 opt_help(fipsinstall_options
);
303 in_fname
= opt_arg();
306 out_fname
= opt_arg();
314 case OPT_CORRUPT_DESC
:
315 self_test_corrupt_desc
= opt_arg();
317 case OPT_CORRUPT_TYPE
:
318 self_test_corrupt_type
= opt_arg();
321 prov_name
= opt_arg();
324 module_fname
= opt_arg();
326 case OPT_SECTION_NAME
:
327 section_name
= opt_arg();
330 mac_name
= opt_arg();
333 if (!sk_OPENSSL_STRING_push(opts
, opt_arg()))
335 if (strncmp(opt_arg(), "hexkey:", 7) == 0)
337 else if (strncmp(opt_arg(), "digest:", 7) == 0)
345 argc
= opt_num_rest();
346 if (module_fname
== NULL
347 || (verify
&& in_fname
== NULL
)
348 || (!verify
&& (out_fname
== NULL
|| prov_name
== NULL
))
353 || self_test_corrupt_desc
!= NULL
354 || self_test_corrupt_type
!= NULL
)
355 OSSL_SELF_TEST_set_callback(NULL
, self_test_events
, NULL
);
357 /* Use the default FIPS HMAC digest and key if not specified. */
358 if (!gotdigest
&& !sk_OPENSSL_STRING_push(opts
, "digest:SHA256"))
360 /* Use the default FIPS HMAC key if not specified. */
361 if (!gotkey
&& !sk_OPENSSL_STRING_push(opts
, "hexkey:" FIPS_KEY_STRING
))
364 module_bio
= bio_open_default(module_fname
, 'r', FORMAT_BINARY
);
365 if (module_bio
== NULL
) {
366 BIO_printf(bio_err
, "Failed to open module file\n");
370 read_buffer
= app_malloc(BUFSIZE
, "I/O buffer");
371 if (read_buffer
== NULL
)
374 mac
= EVP_MAC_fetch(NULL
, mac_name
, NULL
);
376 BIO_printf(bio_err
, "Unable to get MAC of type %s\n", mac_name
);
380 ctx
= EVP_MAC_new_ctx(mac
);
382 BIO_printf(bio_err
, "Unable to create MAC CTX for module check\n");
389 app_params_new_from_opts(opts
, EVP_MAC_settable_ctx_params(mac
));
394 if (!EVP_MAC_set_ctx_params(ctx
, params
)) {
395 BIO_printf(bio_err
, "MAC parameter error\n");
396 ERR_print_errors(bio_err
);
399 app_params_free(params
);
404 ctx2
= EVP_MAC_dup_ctx(ctx
);
406 BIO_printf(bio_err
, "Unable to create MAC CTX for install indicator\n");
410 if (!do_mac(ctx
, read_buffer
, module_bio
, module_mac
, &module_mac_len
))
413 mem_bio
= BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL
,
414 strlen(INSTALL_STATUS_VAL
));
415 if (mem_bio
== NULL
) {
416 BIO_printf(bio_err
, "Unable to create memory BIO\n");
419 if (!do_mac(ctx2
, read_buffer
, mem_bio
, install_mac
, &install_mac_len
))
423 if (!verify_config(in_fname
, section_name
, module_mac
, module_mac_len
,
424 install_mac
, install_mac_len
))
427 BIO_printf(bio_out
, "VERIFY PASSED\n");
430 conf
= generate_config_and_load(prov_name
, section_name
, module_mac
,
434 if (!load_fips_prov_and_run_self_test(prov_name
))
437 fout
= bio_open_default(out_fname
, 'w', FORMAT_TEXT
);
439 BIO_printf(bio_err
, "Failed to open file\n");
442 if (!write_config_fips_section(fout
, section_name
, module_mac
,
443 module_mac_len
, install_mac
,
447 BIO_printf(bio_out
, "INSTALL PASSED\n");
454 BIO_printf(bio_err
, "%s FAILED\n", verify
? "VERIFY" : "INSTALL");
455 ERR_print_errors(bio_err
);
461 BIO_free(module_bio
);
462 sk_OPENSSL_STRING_free(opts
);
464 EVP_MAC_free_ctx(ctx2
);
465 EVP_MAC_free_ctx(ctx
);
466 OPENSSL_free(read_buffer
);
467 free_config_and_unload(conf
);
471 static int self_test_events(const OSSL_PARAM params
[], void *arg
)
473 const OSSL_PARAM
*p
= NULL
;
474 const char *phase
= NULL
, *type
= NULL
, *desc
= NULL
;
477 p
= OSSL_PARAM_locate_const(params
, OSSL_PROV_PARAM_SELF_TEST_PHASE
);
478 if (p
== NULL
|| p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
480 phase
= (const char *)p
->data
;
482 p
= OSSL_PARAM_locate_const(params
, OSSL_PROV_PARAM_SELF_TEST_DESC
);
483 if (p
== NULL
|| p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
485 desc
= (const char *)p
->data
;
487 p
= OSSL_PARAM_locate_const(params
, OSSL_PROV_PARAM_SELF_TEST_TYPE
);
488 if (p
== NULL
|| p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
490 type
= (const char *)p
->data
;
493 if (strcmp(phase
, OSSL_SELF_TEST_PHASE_START
) == 0)
494 BIO_printf(bio_out
, "%s : (%s) : ", desc
, type
);
495 else if (strcmp(phase
, OSSL_SELF_TEST_PHASE_PASS
) == 0
496 || strcmp(phase
, OSSL_SELF_TEST_PHASE_FAIL
) == 0)
497 BIO_printf(bio_out
, "%s\n", phase
);
500 * The self test code will internally corrupt the KAT test result if an
501 * error is returned during the corrupt phase.
503 if (strcmp(phase
, OSSL_SELF_TEST_PHASE_CORRUPT
) == 0
504 && (self_test_corrupt_desc
!= NULL
505 || self_test_corrupt_type
!= NULL
)) {
506 if (self_test_corrupt_desc
!= NULL
507 && strcmp(self_test_corrupt_desc
, desc
) != 0)
509 if (self_test_corrupt_type
!= NULL
510 && strcmp(self_test_corrupt_type
, type
) != 0)
512 BIO_printf(bio_out
, "%s ", phase
);