/*
- * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* https://www.openssl.org/source/license.html
*/
-#include <string.h>
+#include "internal/cryptlib.h"
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/provider.h>
#include "apps.h"
#include "progs.h"
-DEFINE_STACK_OF_STRING()
-
#define BUFSIZE 4096
/* Configuration file values */
static int quiet = 0;
typedef enum OPTION_choice {
- OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
+ OPT_COMMON,
OPT_IN, OPT_OUT, OPT_MODULE,
OPT_PROV_NAME, OPT_SECTION_NAME, OPT_MAC_NAME, OPT_MACOPT, OPT_VERIFY,
- OPT_NO_LOG, OPT_CORRUPT_DESC, OPT_CORRUPT_TYPE, OPT_QUIET, OPT_CONFIG
+ OPT_NO_LOG, OPT_CORRUPT_DESC, OPT_CORRUPT_TYPE, OPT_QUIET, OPT_CONFIG,
+ OPT_NO_CONDITIONAL_ERRORS,
+ OPT_NO_SECURITY_CHECKS,
+ OPT_SELF_TEST_ONLOAD
} OPTION_CHOICE;
const OPTIONS fipsinstall_options[] = {
{"provider_name", OPT_PROV_NAME, 's', "FIPS provider name"},
{"section_name", OPT_SECTION_NAME, 's',
"FIPS Provider config section name (optional)"},
-
+ {"no_conditional_errors", OPT_NO_CONDITIONAL_ERRORS, '-',
+ "Disable the ability of the fips module to enter an error state if"
+ " any conditional self tests fail"},
+ {"no_security_checks", OPT_NO_SECURITY_CHECKS, '-',
+ "Disable the run-time FIPS security checks in the module"},
+ {"self_test_onload", OPT_SELF_TEST_ONLOAD, '-',
+ "Forces self tests to always run on module load"},
OPT_SECTION("Input"),
{"in", OPT_IN, '<', "Input config file, used when verifying"},
int i;
size_t outsz = *out_len;
- if (!EVP_MAC_init(ctx))
+ if (!EVP_MAC_init(ctx, NULL, 0, NULL))
goto err;
- if (EVP_MAC_size(ctx) > outsz)
+ if (EVP_MAC_CTX_get_mac_size(ctx) > outsz)
goto end;
while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) {
if (i < 0 || !EVP_MAC_update(ctx, tmp, i))
/*
* Outputs a fips related config file that contains entries for the fips
- * module checksum and the installation indicator checksum.
+ * module checksum, installation indicator checksum and the options
+ * conditional_errors and security_checks.
*
* Returns 1 if the config file is written otherwise it returns 0 on error.
*/
static int write_config_fips_section(BIO *out, const char *section,
unsigned char *module_mac,
size_t module_mac_len,
+ int conditional_errors,
+ int security_checks,
unsigned char *install_mac,
size_t install_mac_len)
{
int ret = 0;
- if (!(BIO_printf(out, "[%s]\n", section) > 0
- && BIO_printf(out, "activate = 1\n") > 0
- && BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
- VERSION_VAL) > 0
- && print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
- module_mac_len)))
+ if (BIO_printf(out, "[%s]\n", section) <= 0
+ || BIO_printf(out, "activate = 1\n") <= 0
+ || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
+ VERSION_VAL) <= 0
+ || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
+ conditional_errors ? "1" : "0") <= 0
+ || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
+ security_checks ? "1" : "0") <= 0
+ || !print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
+ module_mac_len))
goto end;
- if (install_mac != NULL) {
- if (!(print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
- install_mac_len)
- && BIO_printf(out, "%s = %s\n",
- OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
- INSTALL_STATUS_VAL) > 0))
+ if (install_mac != NULL && install_mac_len > 0) {
+ if (!print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
+ install_mac_len)
+ || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
+ INSTALL_STATUS_VAL) <= 0)
goto end;
}
ret = 1;
static CONF *generate_config_and_load(const char *prov_name,
const char *section,
unsigned char *module_mac,
- size_t module_mac_len)
+ size_t module_mac_len,
+ int conditional_errors,
+ int security_checks)
{
BIO *mem_bio = NULL;
CONF *conf = NULL;
if (mem_bio == NULL)
return 0;
if (!write_config_header(mem_bio, prov_name, section)
- || !write_config_fips_section(mem_bio, section, module_mac,
- module_mac_len, NULL, 0))
+ || !write_config_fips_section(mem_bio, section,
+ module_mac, module_mac_len,
+ conditional_errors,
+ security_checks,
+ NULL, 0))
goto end;
conf = app_load_config_bio(mem_bio, NULL);
static int verify_module_load(const char *parent_config_file)
{
- return OPENSSL_CTX_load_config(NULL, parent_config_file);
+ return OSSL_LIB_CTX_load_config(NULL, parent_config_file);
}
/*
BIO_printf(bio_err, "version not found\n");
goto end;
}
- s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
- if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
- BIO_printf(bio_err, "install status not found\n");
- goto end;
- }
s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
if (s == NULL) {
BIO_printf(bio_err, "Module integrity MAC not found\n");
BIO_printf(bio_err, "Module integrity mismatch\n");
goto end;
}
- s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
- if (s == NULL) {
- BIO_printf(bio_err, "Install indicator MAC not found\n");
- goto end;
- }
- buf2 = OPENSSL_hexstr2buf(s, &len);
- if (buf2 == NULL
- || (size_t)len != install_mac_len
- || memcmp(install_mac, buf2, install_mac_len) != 0) {
- BIO_printf(bio_err, "Install indicator status mismatch\n");
- goto end;
+ if (install_mac != NULL && install_mac_len > 0) {
+ s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
+ if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
+ BIO_printf(bio_err, "install status not found\n");
+ goto end;
+ }
+ s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
+ if (s == NULL) {
+ BIO_printf(bio_err, "Install indicator MAC not found\n");
+ goto end;
+ }
+ buf2 = OPENSSL_hexstr2buf(s, &len);
+ if (buf2 == NULL
+ || (size_t)len != install_mac_len
+ || memcmp(install_mac, buf2, install_mac_len) != 0) {
+ BIO_printf(bio_err, "Install indicator status mismatch\n");
+ goto end;
+ }
}
ret = 1;
end:
int fipsinstall_main(int argc, char **argv)
{
- int ret = 1, verify = 0, gotkey = 0, gotdigest = 0;
+ int ret = 1, verify = 0, gotkey = 0, gotdigest = 0, self_test_onload = 0;
+ int enable_conditional_errors = 1, enable_security_checks = 1;
const char *section_name = "fips_sect";
const char *mac_name = "HMAC";
const char *prov_name = "fips";
BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
char *in_fname = NULL, *out_fname = NULL, *prog;
- char *module_fname = NULL, *parent_config = NULL;
+ char *module_fname = NULL, *parent_config = NULL, *module_path = NULL;
+ const char *tail;
EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
STACK_OF(OPENSSL_STRING) *opts = NULL;
OPTION_CHOICE o;
case OPT_OUT:
out_fname = opt_arg();
break;
+ case OPT_NO_CONDITIONAL_ERRORS:
+ enable_conditional_errors = 0;
+ break;
+ case OPT_NO_SECURITY_CHECKS:
+ enable_security_checks = 0;
+ break;
case OPT_QUIET:
quiet = 1;
/* FALLTHROUGH */
case OPT_MACOPT:
if (!sk_OPENSSL_STRING_push(opts, opt_arg()))
goto opthelp;
- if (strncmp(opt_arg(), "hexkey:", 7) == 0)
+ if (HAS_PREFIX(opt_arg(), "hexkey:"))
gotkey = 1;
- else if (strncmp(opt_arg(), "digest:", 7) == 0)
+ else if (HAS_PREFIX(opt_arg(), "digest:"))
gotdigest = 1;
break;
case OPT_VERIFY:
verify = 1;
break;
+ case OPT_SELF_TEST_ONLOAD:
+ self_test_onload = 1;
+ break;
}
}
+
+ /* No extra arguments. */
argc = opt_num_rest();
+ if (argc != 0 || (verify && in_fname == NULL))
+ goto opthelp;
if (parent_config != NULL) {
/* Test that a parent config can load the module */
if (verify_module_load(parent_config)) {
ret = OSSL_PROVIDER_available(NULL, prov_name) ? 0 : 1;
if (!quiet)
- BIO_printf(bio_out, "FIPS provider is %s\n",
+ BIO_printf(bio_err, "FIPS provider is %s\n",
ret == 0 ? "available" : " not available");
}
goto end;
}
- if (module_fname == NULL
- || (verify && in_fname == NULL)
- || (!verify && out_fname == NULL)
- || argc != 0)
+ if (module_fname == NULL)
goto opthelp;
+ tail = opt_path_end(module_fname);
+ if (tail != NULL) {
+ module_path = OPENSSL_strdup(module_fname);
+ if (module_path == NULL)
+ goto end;
+ module_path[tail - module_fname] = '\0';
+ if (!OSSL_PROVIDER_set_default_search_path(NULL, module_path))
+ goto end;
+ }
+
if (self_test_log
|| self_test_corrupt_desc != NULL
|| self_test_corrupt_type != NULL)
if (read_buffer == NULL)
goto end;
- mac = EVP_MAC_fetch(NULL, mac_name, NULL);
+ mac = EVP_MAC_fetch(app_get0_libctx(), mac_name, app_get0_propq());
if (mac == NULL) {
BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
goto end;
if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
goto end;
- mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
- strlen(INSTALL_STATUS_VAL));
- if (mem_bio == NULL) {
- BIO_printf(bio_err, "Unable to create memory BIO\n");
- goto end;
+ if (self_test_onload == 0) {
+ mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
+ strlen(INSTALL_STATUS_VAL));
+ if (mem_bio == NULL) {
+ BIO_printf(bio_err, "Unable to create memory BIO\n");
+ goto end;
+ }
+ if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
+ goto end;
+ } else {
+ install_mac_len = 0;
}
- if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
- goto end;
if (verify) {
if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
install_mac, install_mac_len))
goto end;
if (!quiet)
- BIO_printf(bio_out, "VERIFY PASSED\n");
+ BIO_printf(bio_err, "VERIFY PASSED\n");
} else {
conf = generate_config_and_load(prov_name, section_name, module_mac,
- module_mac_len);
+ module_mac_len,
+ enable_conditional_errors,
+ enable_security_checks);
if (conf == NULL)
goto end;
if (!load_fips_prov_and_run_self_test(prov_name))
goto end;
- fout = bio_open_default(out_fname, 'w', FORMAT_TEXT);
+ fout =
+ out_fname == NULL ? dup_bio_out(FORMAT_TEXT)
+ : bio_open_default(out_fname, 'w', FORMAT_TEXT);
if (fout == NULL) {
BIO_printf(bio_err, "Failed to open file\n");
goto end;
}
- if (!write_config_fips_section(fout, section_name, module_mac,
- module_mac_len, install_mac,
- install_mac_len))
+ if (!write_config_fips_section(fout, section_name,
+ module_mac, module_mac_len,
+ enable_conditional_errors,
+ enable_security_checks,
+ install_mac, install_mac_len))
goto end;
if (!quiet)
- BIO_printf(bio_out, "INSTALL PASSED\n");
+ BIO_printf(bio_err, "INSTALL PASSED\n");
}
ret = 0;
}
cleanup:
+ OPENSSL_free(module_path);
BIO_free(fout);
BIO_free(mem_bio);
BIO_free(module_bio);
if (self_test_log) {
if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
- BIO_printf(bio_out, "%s : (%s) : ", desc, type);
+ BIO_printf(bio_err, "%s : (%s) : ", desc, type);
else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
|| strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
- BIO_printf(bio_out, "%s\n", phase);
+ BIO_printf(bio_err, "%s\n", phase);
}
/*
* The self test code will internally corrupt the KAT test result if an
if (self_test_corrupt_type != NULL
&& strcmp(self_test_corrupt_type, type) != 0)
goto end;
- BIO_printf(bio_out, "%s ", phase);
+ BIO_printf(bio_err, "%s ", phase);
goto err;
}
end: