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()
26 /* Configuration file values */
27 #define VERSION_KEY "version"
28 #define VERSION_VAL "1"
29 #define INSTALL_STATUS_VAL "INSTALL_SELF_TEST_KATS_RUN"
31 static OSSL_CALLBACK self_test_events
;
32 static char *self_test_corrupt_desc
= NULL
;
33 static char *self_test_corrupt_type
= NULL
;
34 static int self_test_log
= 1;
37 typedef enum OPTION_choice
{
38 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
39 OPT_IN
, OPT_OUT
, OPT_MODULE
,
40 OPT_PROV_NAME
, OPT_SECTION_NAME
, OPT_MAC_NAME
, OPT_MACOPT
, OPT_VERIFY
,
41 OPT_NO_LOG
, OPT_CORRUPT_DESC
, OPT_CORRUPT_TYPE
, OPT_QUIET
, OPT_CONFIG
44 const OPTIONS fipsinstall_options
[] = {
45 OPT_SECTION("General"),
46 {"help", OPT_HELP
, '-', "Display this summary"},
47 {"verify", OPT_VERIFY
, '-',
48 "Verify a config file instead of generating one"},
49 {"module", OPT_MODULE
, '<', "File name of the provider module"},
50 {"provider_name", OPT_PROV_NAME
, 's', "FIPS provider name"},
51 {"section_name", OPT_SECTION_NAME
, 's',
52 "FIPS Provider config section name (optional)"},
55 {"in", OPT_IN
, '<', "Input config file, used when verifying"},
57 OPT_SECTION("Output"),
58 {"out", OPT_OUT
, '>', "Output config file, used when generating"},
59 {"mac_name", OPT_MAC_NAME
, 's', "MAC name"},
60 {"macopt", OPT_MACOPT
, 's', "MAC algorithm parameters in n:v form. "
61 "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
62 {"noout", OPT_NO_LOG
, '-', "Disable logging of self test events"},
63 {"corrupt_desc", OPT_CORRUPT_DESC
, 's', "Corrupt a self test by description"},
64 {"corrupt_type", OPT_CORRUPT_TYPE
, 's', "Corrupt a self test by type"},
65 {"config", OPT_CONFIG
, '<', "The parent config to verify"},
66 {"quiet", OPT_QUIET
, '-', "No messages, just exit status"},
70 static int do_mac(EVP_MAC_CTX
*ctx
, unsigned char *tmp
, BIO
*in
,
71 unsigned char *out
, size_t *out_len
)
75 size_t outsz
= *out_len
;
77 if (!EVP_MAC_init(ctx
))
79 if (EVP_MAC_size(ctx
) > outsz
)
81 while ((i
= BIO_read(in
, (char *)tmp
, BUFSIZE
)) != 0) {
82 if (i
< 0 || !EVP_MAC_update(ctx
, tmp
, i
))
86 if (!EVP_MAC_final(ctx
, out
, out_len
, outsz
))
93 static int load_fips_prov_and_run_self_test(const char *prov_name
)
96 OSSL_PROVIDER
*prov
= NULL
;
98 prov
= OSSL_PROVIDER_load(NULL
, prov_name
);
100 BIO_printf(bio_err
, "Failed to load FIPS module\n");
105 OSSL_PROVIDER_unload(prov
);
109 static int print_mac(BIO
*bio
, const char *label
, const unsigned char *mac
,
115 hexstr
= OPENSSL_buf2hexstr(mac
, (long)len
);
118 ret
= BIO_printf(bio
, "%s = %s\n", label
, hexstr
);
119 OPENSSL_free(hexstr
);
123 static int write_config_header(BIO
*out
, const char *prov_name
,
126 return BIO_printf(out
, "openssl_conf = openssl_init\n\n")
127 && BIO_printf(out
, "[openssl_init]\n")
128 && BIO_printf(out
, "providers = provider_section\n\n")
129 && BIO_printf(out
, "[provider_section]\n")
130 && BIO_printf(out
, "%s = %s\n\n", prov_name
, section
);
134 * Outputs a fips related config file that contains entries for the fips
135 * module checksum and the installation indicator checksum.
137 * Returns 1 if the config file is written otherwise it returns 0 on error.
139 static int write_config_fips_section(BIO
*out
, const char *section
,
140 unsigned char *module_mac
,
141 size_t module_mac_len
,
142 unsigned char *install_mac
,
143 size_t install_mac_len
)
147 if (!(BIO_printf(out
, "[%s]\n", section
) > 0
148 && BIO_printf(out
, "activate = 1\n") > 0
149 && BIO_printf(out
, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION
,
151 && print_mac(out
, OSSL_PROV_FIPS_PARAM_MODULE_MAC
, module_mac
,
155 if (install_mac
!= NULL
) {
156 if (!(print_mac(out
, OSSL_PROV_FIPS_PARAM_INSTALL_MAC
, install_mac
,
158 && BIO_printf(out
, "%s = %s\n",
159 OSSL_PROV_FIPS_PARAM_INSTALL_STATUS
,
160 INSTALL_STATUS_VAL
) > 0))
168 static CONF
*generate_config_and_load(const char *prov_name
,
170 unsigned char *module_mac
,
171 size_t module_mac_len
)
176 mem_bio
= BIO_new(BIO_s_mem());
179 if (!write_config_header(mem_bio
, prov_name
, section
)
180 || !write_config_fips_section(mem_bio
, section
, module_mac
,
181 module_mac_len
, NULL
, 0))
184 conf
= app_load_config_bio(mem_bio
, NULL
);
188 if (CONF_modules_load(conf
, NULL
, 0) <= 0)
198 static void free_config_and_unload(CONF
*conf
)
202 CONF_modules_unload(1);
206 static int verify_module_load(const char *parent_config_file
)
208 return OPENSSL_CTX_load_config(NULL
, parent_config_file
);
212 * Returns 1 if the config file entries match the passed in module_mac and
213 * install_mac values, otherwise it returns 0.
215 static int verify_config(const char *infile
, const char *section
,
216 unsigned char *module_mac
, size_t module_mac_len
,
217 unsigned char *install_mac
, size_t install_mac_len
)
221 unsigned char *buf1
= NULL
, *buf2
= NULL
;
225 /* read in the existing values and check they match the saved values */
226 conf
= app_load_config(infile
);
230 s
= NCONF_get_string(conf
, section
, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION
);
231 if (s
== NULL
|| strcmp(s
, VERSION_VAL
) != 0) {
232 BIO_printf(bio_err
, "version not found\n");
235 s
= NCONF_get_string(conf
, section
, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS
);
236 if (s
== NULL
|| strcmp(s
, INSTALL_STATUS_VAL
) != 0) {
237 BIO_printf(bio_err
, "install status not found\n");
240 s
= NCONF_get_string(conf
, section
, OSSL_PROV_FIPS_PARAM_MODULE_MAC
);
242 BIO_printf(bio_err
, "Module integrity MAC not found\n");
245 buf1
= OPENSSL_hexstr2buf(s
, &len
);
247 || (size_t)len
!= module_mac_len
248 || memcmp(module_mac
, buf1
, module_mac_len
) != 0) {
249 BIO_printf(bio_err
, "Module integrity mismatch\n");
252 s
= NCONF_get_string(conf
, section
, OSSL_PROV_FIPS_PARAM_INSTALL_MAC
);
254 BIO_printf(bio_err
, "Install indicator MAC not found\n");
257 buf2
= OPENSSL_hexstr2buf(s
, &len
);
259 || (size_t)len
!= install_mac_len
260 || memcmp(install_mac
, buf2
, install_mac_len
) != 0) {
261 BIO_printf(bio_err
, "Install indicator status mismatch\n");
272 int fipsinstall_main(int argc
, char **argv
)
274 int ret
= 1, verify
= 0, gotkey
= 0, gotdigest
= 0;
275 const char *section_name
= "fips_sect";
276 const char *mac_name
= "HMAC";
277 const char *prov_name
= "fips";
278 BIO
*module_bio
= NULL
, *mem_bio
= NULL
, *fout
= NULL
;
279 char *in_fname
= NULL
, *out_fname
= NULL
, *prog
;
280 char *module_fname
= NULL
, *parent_config
= NULL
;
281 EVP_MAC_CTX
*ctx
= NULL
, *ctx2
= NULL
;
282 STACK_OF(OPENSSL_STRING
) *opts
= NULL
;
284 unsigned char *read_buffer
= NULL
;
285 unsigned char module_mac
[EVP_MAX_MD_SIZE
];
286 size_t module_mac_len
= EVP_MAX_MD_SIZE
;
287 unsigned char install_mac
[EVP_MAX_MD_SIZE
];
288 size_t install_mac_len
= EVP_MAX_MD_SIZE
;
292 if ((opts
= sk_OPENSSL_STRING_new_null()) == NULL
)
295 prog
= opt_init(argc
, argv
, fipsinstall_options
);
296 while ((o
= opt_next()) != OPT_EOF
) {
301 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
304 opt_help(fipsinstall_options
);
308 in_fname
= opt_arg();
311 out_fname
= opt_arg();
319 case OPT_CORRUPT_DESC
:
320 self_test_corrupt_desc
= opt_arg();
322 case OPT_CORRUPT_TYPE
:
323 self_test_corrupt_type
= opt_arg();
326 prov_name
= opt_arg();
329 module_fname
= opt_arg();
331 case OPT_SECTION_NAME
:
332 section_name
= opt_arg();
335 mac_name
= opt_arg();
338 parent_config
= opt_arg();
341 if (!sk_OPENSSL_STRING_push(opts
, opt_arg()))
343 if (strncmp(opt_arg(), "hexkey:", 7) == 0)
345 else if (strncmp(opt_arg(), "digest:", 7) == 0)
353 argc
= opt_num_rest();
355 if (parent_config
!= NULL
) {
356 /* Test that a parent config can load the module */
357 if (verify_module_load(parent_config
)) {
358 ret
= OSSL_PROVIDER_available(NULL
, prov_name
) ? 0 : 1;
360 BIO_printf(bio_out
, "FIPS provider is %s\n",
361 ret
== 0 ? "available" : " not available");
365 if (module_fname
== NULL
366 || (verify
&& in_fname
== NULL
)
367 || (!verify
&& out_fname
== NULL
)
372 || self_test_corrupt_desc
!= NULL
373 || self_test_corrupt_type
!= NULL
)
374 OSSL_SELF_TEST_set_callback(NULL
, self_test_events
, NULL
);
376 /* Use the default FIPS HMAC digest and key if not specified. */
377 if (!gotdigest
&& !sk_OPENSSL_STRING_push(opts
, "digest:SHA256"))
379 if (!gotkey
&& !sk_OPENSSL_STRING_push(opts
, "hexkey:" FIPS_KEY_STRING
))
382 module_bio
= bio_open_default(module_fname
, 'r', FORMAT_BINARY
);
383 if (module_bio
== NULL
) {
384 BIO_printf(bio_err
, "Failed to open module file\n");
388 read_buffer
= app_malloc(BUFSIZE
, "I/O buffer");
389 if (read_buffer
== NULL
)
392 mac
= EVP_MAC_fetch(NULL
, mac_name
, NULL
);
394 BIO_printf(bio_err
, "Unable to get MAC of type %s\n", mac_name
);
398 ctx
= EVP_MAC_CTX_new(mac
);
400 BIO_printf(bio_err
, "Unable to create MAC CTX for module check\n");
407 app_params_new_from_opts(opts
, EVP_MAC_settable_ctx_params(mac
));
412 if (!EVP_MAC_CTX_set_params(ctx
, params
)) {
413 BIO_printf(bio_err
, "MAC parameter error\n");
414 ERR_print_errors(bio_err
);
417 app_params_free(params
);
422 ctx2
= EVP_MAC_CTX_dup(ctx
);
424 BIO_printf(bio_err
, "Unable to create MAC CTX for install indicator\n");
428 if (!do_mac(ctx
, read_buffer
, module_bio
, module_mac
, &module_mac_len
))
431 mem_bio
= BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL
,
432 strlen(INSTALL_STATUS_VAL
));
433 if (mem_bio
== NULL
) {
434 BIO_printf(bio_err
, "Unable to create memory BIO\n");
437 if (!do_mac(ctx2
, read_buffer
, mem_bio
, install_mac
, &install_mac_len
))
441 if (!verify_config(in_fname
, section_name
, module_mac
, module_mac_len
,
442 install_mac
, install_mac_len
))
445 BIO_printf(bio_out
, "VERIFY PASSED\n");
448 conf
= generate_config_and_load(prov_name
, section_name
, module_mac
,
452 if (!load_fips_prov_and_run_self_test(prov_name
))
455 fout
= bio_open_default(out_fname
, 'w', FORMAT_TEXT
);
457 BIO_printf(bio_err
, "Failed to open file\n");
460 if (!write_config_fips_section(fout
, section_name
, module_mac
,
461 module_mac_len
, install_mac
,
465 BIO_printf(bio_out
, "INSTALL PASSED\n");
472 BIO_printf(bio_err
, "%s FAILED\n", verify
? "VERIFY" : "INSTALL");
473 ERR_print_errors(bio_err
);
479 BIO_free(module_bio
);
480 sk_OPENSSL_STRING_free(opts
);
482 EVP_MAC_CTX_free(ctx2
);
483 EVP_MAC_CTX_free(ctx
);
484 OPENSSL_free(read_buffer
);
485 free_config_and_unload(conf
);
489 static int self_test_events(const OSSL_PARAM params
[], void *arg
)
491 const OSSL_PARAM
*p
= NULL
;
492 const char *phase
= NULL
, *type
= NULL
, *desc
= NULL
;
495 p
= OSSL_PARAM_locate_const(params
, OSSL_PROV_PARAM_SELF_TEST_PHASE
);
496 if (p
== NULL
|| p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
498 phase
= (const char *)p
->data
;
500 p
= OSSL_PARAM_locate_const(params
, OSSL_PROV_PARAM_SELF_TEST_DESC
);
501 if (p
== NULL
|| p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
503 desc
= (const char *)p
->data
;
505 p
= OSSL_PARAM_locate_const(params
, OSSL_PROV_PARAM_SELF_TEST_TYPE
);
506 if (p
== NULL
|| p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
508 type
= (const char *)p
->data
;
511 if (strcmp(phase
, OSSL_SELF_TEST_PHASE_START
) == 0)
512 BIO_printf(bio_out
, "%s : (%s) : ", desc
, type
);
513 else if (strcmp(phase
, OSSL_SELF_TEST_PHASE_PASS
) == 0
514 || strcmp(phase
, OSSL_SELF_TEST_PHASE_FAIL
) == 0)
515 BIO_printf(bio_out
, "%s\n", phase
);
518 * The self test code will internally corrupt the KAT test result if an
519 * error is returned during the corrupt phase.
521 if (strcmp(phase
, OSSL_SELF_TEST_PHASE_CORRUPT
) == 0
522 && (self_test_corrupt_desc
!= NULL
523 || self_test_corrupt_type
!= NULL
)) {
524 if (self_test_corrupt_desc
!= NULL
525 && strcmp(self_test_corrupt_desc
, desc
) != 0)
527 if (self_test_corrupt_type
!= NULL
528 && strcmp(self_test_corrupt_type
, type
) != 0)
530 BIO_printf(bio_out
, "%s ", phase
);