fipsinstall.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*
  2. * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include <openssl/evp.h>
  10. #include <openssl/err.h>
  11. #include <openssl/provider.h>
  12. #include <openssl/params.h>
  13. #include <openssl/fips_names.h>
  14. #include <openssl/core_names.h>
  15. #include <openssl/self_test.h>
  16. #include <openssl/fipskey.h>
  17. #include "apps.h"
  18. #include "progs.h"
  19. #define BUFSIZE 4096
  20. /* Configuration file values */
  21. #define VERSION_KEY "version"
  22. #define VERSION_VAL "1"
  23. #define INSTALL_STATUS_VAL "INSTALL_SELF_TEST_KATS_RUN"
  24. static OSSL_CALLBACK self_test_events;
  25. static char *self_test_corrupt_desc = NULL;
  26. static char *self_test_corrupt_type = NULL;
  27. static int self_test_log = 1;
  28. static int quiet = 0;
  29. typedef enum OPTION_choice {
  30. OPT_COMMON,
  31. OPT_IN, OPT_OUT, OPT_MODULE, OPT_PEDANTIC,
  32. OPT_PROV_NAME, OPT_SECTION_NAME, OPT_MAC_NAME, OPT_MACOPT, OPT_VERIFY,
  33. OPT_NO_LOG, OPT_CORRUPT_DESC, OPT_CORRUPT_TYPE, OPT_QUIET, OPT_CONFIG,
  34. OPT_NO_CONDITIONAL_ERRORS,
  35. OPT_NO_SECURITY_CHECKS,
  36. OPT_TLS_PRF_EMS_CHECK,
  37. OPT_DISALLOW_DRGB_TRUNC_DIGEST,
  38. OPT_SELF_TEST_ONLOAD, OPT_SELF_TEST_ONINSTALL
  39. } OPTION_CHOICE;
  40. const OPTIONS fipsinstall_options[] = {
  41. OPT_SECTION("General"),
  42. {"help", OPT_HELP, '-', "Display this summary"},
  43. {"pedantic", OPT_PEDANTIC, '-', "Set options for strict FIPS compliance"},
  44. {"verify", OPT_VERIFY, '-',
  45. "Verify a config file instead of generating one"},
  46. {"module", OPT_MODULE, '<', "File name of the provider module"},
  47. {"provider_name", OPT_PROV_NAME, 's', "FIPS provider name"},
  48. {"section_name", OPT_SECTION_NAME, 's',
  49. "FIPS Provider config section name (optional)"},
  50. {"no_conditional_errors", OPT_NO_CONDITIONAL_ERRORS, '-',
  51. "Disable the ability of the fips module to enter an error state if"
  52. " any conditional self tests fail"},
  53. {"no_security_checks", OPT_NO_SECURITY_CHECKS, '-',
  54. "Disable the run-time FIPS security checks in the module"},
  55. {"self_test_onload", OPT_SELF_TEST_ONLOAD, '-',
  56. "Forces self tests to always run on module load"},
  57. {"self_test_oninstall", OPT_SELF_TEST_ONINSTALL, '-',
  58. "Forces self tests to run once on module installation"},
  59. {"ems_check", OPT_TLS_PRF_EMS_CHECK, '-',
  60. "Enable the run-time FIPS check for EMS during TLS1_PRF"},
  61. {"no_drbg_truncated_digests", OPT_DISALLOW_DRGB_TRUNC_DIGEST, '-',
  62. "Disallow truncated digests with Hash and HMAC DRBGs"},
  63. OPT_SECTION("Input"),
  64. {"in", OPT_IN, '<', "Input config file, used when verifying"},
  65. OPT_SECTION("Output"),
  66. {"out", OPT_OUT, '>', "Output config file, used when generating"},
  67. {"mac_name", OPT_MAC_NAME, 's', "MAC name"},
  68. {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form."},
  69. {OPT_MORE_STR, 0, 0, "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
  70. {"noout", OPT_NO_LOG, '-', "Disable logging of self test events"},
  71. {"corrupt_desc", OPT_CORRUPT_DESC, 's', "Corrupt a self test by description"},
  72. {"corrupt_type", OPT_CORRUPT_TYPE, 's', "Corrupt a self test by type"},
  73. {"config", OPT_CONFIG, '<', "The parent config to verify"},
  74. {"quiet", OPT_QUIET, '-', "No messages, just exit status"},
  75. {NULL}
  76. };
  77. typedef struct {
  78. unsigned int self_test_onload : 1;
  79. unsigned int conditional_errors : 1;
  80. unsigned int security_checks : 1;
  81. unsigned int tls_prf_ems_check : 1;
  82. unsigned int drgb_no_trunc_dgst : 1;
  83. } FIPS_OPTS;
  84. /* Pedantic FIPS compliance */
  85. static const FIPS_OPTS pedantic_opts = {
  86. 1, /* self_test_onload */
  87. 1, /* conditional_errors */
  88. 1, /* security_checks */
  89. 1, /* tls_prf_ems_check */
  90. 1, /* drgb_no_trunc_dgst */
  91. };
  92. /* Default FIPS settings for backward compatibility */
  93. static FIPS_OPTS fips_opts = {
  94. 1, /* self_test_onload */
  95. 1, /* conditional_errors */
  96. 1, /* security_checks */
  97. 0, /* tls_prf_ems_check */
  98. 0, /* drgb_no_trunc_dgst */
  99. };
  100. static int check_non_pedantic_fips(int pedantic, const char *name)
  101. {
  102. if (pedantic) {
  103. BIO_printf(bio_err, "Cannot specify -%s after -pedantic\n", name);
  104. return 0;
  105. }
  106. return 1;
  107. }
  108. static int do_mac(EVP_MAC_CTX *ctx, unsigned char *tmp, BIO *in,
  109. unsigned char *out, size_t *out_len)
  110. {
  111. int ret = 0;
  112. int i;
  113. size_t outsz = *out_len;
  114. if (!EVP_MAC_init(ctx, NULL, 0, NULL))
  115. goto err;
  116. if (EVP_MAC_CTX_get_mac_size(ctx) > outsz)
  117. goto end;
  118. while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) {
  119. if (i < 0 || !EVP_MAC_update(ctx, tmp, i))
  120. goto err;
  121. }
  122. end:
  123. if (!EVP_MAC_final(ctx, out, out_len, outsz))
  124. goto err;
  125. ret = 1;
  126. err:
  127. return ret;
  128. }
  129. static int load_fips_prov_and_run_self_test(const char *prov_name)
  130. {
  131. int ret = 0;
  132. OSSL_PROVIDER *prov = NULL;
  133. OSSL_PARAM params[4], *p = params;
  134. char *name = "", *vers = "", *build = "";
  135. prov = OSSL_PROVIDER_load(NULL, prov_name);
  136. if (prov == NULL) {
  137. BIO_printf(bio_err, "Failed to load FIPS module\n");
  138. goto end;
  139. }
  140. if (!quiet) {
  141. *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_NAME,
  142. &name, sizeof(name));
  143. *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION,
  144. &vers, sizeof(vers));
  145. *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_BUILDINFO,
  146. &build, sizeof(build));
  147. *p = OSSL_PARAM_construct_end();
  148. if (!OSSL_PROVIDER_get_params(prov, params)) {
  149. BIO_printf(bio_err, "Failed to query FIPS module parameters\n");
  150. goto end;
  151. }
  152. if (OSSL_PARAM_modified(params))
  153. BIO_printf(bio_err, "\t%-10s\t%s\n", "name:", name);
  154. if (OSSL_PARAM_modified(params + 1))
  155. BIO_printf(bio_err, "\t%-10s\t%s\n", "version:", vers);
  156. if (OSSL_PARAM_modified(params + 2))
  157. BIO_printf(bio_err, "\t%-10s\t%s\n", "build:", build);
  158. }
  159. ret = 1;
  160. end:
  161. OSSL_PROVIDER_unload(prov);
  162. return ret;
  163. }
  164. static int print_mac(BIO *bio, const char *label, const unsigned char *mac,
  165. size_t len)
  166. {
  167. int ret;
  168. char *hexstr = NULL;
  169. hexstr = OPENSSL_buf2hexstr(mac, (long)len);
  170. if (hexstr == NULL)
  171. return 0;
  172. ret = BIO_printf(bio, "%s = %s\n", label, hexstr);
  173. OPENSSL_free(hexstr);
  174. return ret;
  175. }
  176. static int write_config_header(BIO *out, const char *prov_name,
  177. const char *section)
  178. {
  179. return BIO_printf(out, "openssl_conf = openssl_init\n\n")
  180. && BIO_printf(out, "[openssl_init]\n")
  181. && BIO_printf(out, "providers = provider_section\n\n")
  182. && BIO_printf(out, "[provider_section]\n")
  183. && BIO_printf(out, "%s = %s\n\n", prov_name, section);
  184. }
  185. /*
  186. * Outputs a fips related config file that contains entries for the fips
  187. * module checksum, installation indicator checksum and the options
  188. * conditional_errors and security_checks.
  189. *
  190. * Returns 1 if the config file is written otherwise it returns 0 on error.
  191. */
  192. static int write_config_fips_section(BIO *out, const char *section,
  193. unsigned char *module_mac,
  194. size_t module_mac_len,
  195. const FIPS_OPTS *opts,
  196. unsigned char *install_mac,
  197. size_t install_mac_len)
  198. {
  199. int ret = 0;
  200. if (BIO_printf(out, "[%s]\n", section) <= 0
  201. || BIO_printf(out, "activate = 1\n") <= 0
  202. || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
  203. VERSION_VAL) <= 0
  204. || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
  205. opts->conditional_errors ? "1" : "0") <= 0
  206. || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
  207. opts->security_checks ? "1" : "0") <= 0
  208. || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_TLS1_PRF_EMS_CHECK,
  209. opts->tls_prf_ems_check ? "1" : "0") <= 0
  210. || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_DRBG_TRUNC_DIGEST,
  211. opts->drgb_no_trunc_dgst ? "1" : "0") <= 0
  212. || !print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
  213. module_mac_len))
  214. goto end;
  215. if (install_mac != NULL && install_mac_len > 0) {
  216. if (!print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
  217. install_mac_len)
  218. || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
  219. INSTALL_STATUS_VAL) <= 0)
  220. goto end;
  221. }
  222. ret = 1;
  223. end:
  224. return ret;
  225. }
  226. static CONF *generate_config_and_load(const char *prov_name,
  227. const char *section,
  228. unsigned char *module_mac,
  229. size_t module_mac_len,
  230. const FIPS_OPTS *opts)
  231. {
  232. BIO *mem_bio = NULL;
  233. CONF *conf = NULL;
  234. mem_bio = BIO_new(BIO_s_mem());
  235. if (mem_bio == NULL)
  236. return 0;
  237. if (!write_config_header(mem_bio, prov_name, section)
  238. || !write_config_fips_section(mem_bio, section,
  239. module_mac, module_mac_len,
  240. opts, NULL, 0))
  241. goto end;
  242. conf = app_load_config_bio(mem_bio, NULL);
  243. if (conf == NULL)
  244. goto end;
  245. if (CONF_modules_load(conf, NULL, 0) <= 0)
  246. goto end;
  247. BIO_free(mem_bio);
  248. return conf;
  249. end:
  250. NCONF_free(conf);
  251. BIO_free(mem_bio);
  252. return NULL;
  253. }
  254. static void free_config_and_unload(CONF *conf)
  255. {
  256. if (conf != NULL) {
  257. NCONF_free(conf);
  258. CONF_modules_unload(1);
  259. }
  260. }
  261. static int verify_module_load(const char *parent_config_file)
  262. {
  263. return OSSL_LIB_CTX_load_config(NULL, parent_config_file);
  264. }
  265. /*
  266. * Returns 1 if the config file entries match the passed in module_mac and
  267. * install_mac values, otherwise it returns 0.
  268. */
  269. static int verify_config(const char *infile, const char *section,
  270. unsigned char *module_mac, size_t module_mac_len,
  271. unsigned char *install_mac, size_t install_mac_len)
  272. {
  273. int ret = 0;
  274. char *s = NULL;
  275. unsigned char *buf1 = NULL, *buf2 = NULL;
  276. long len;
  277. CONF *conf = NULL;
  278. /* read in the existing values and check they match the saved values */
  279. conf = app_load_config(infile);
  280. if (conf == NULL)
  281. goto end;
  282. s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION);
  283. if (s == NULL || strcmp(s, VERSION_VAL) != 0) {
  284. BIO_printf(bio_err, "version not found\n");
  285. goto end;
  286. }
  287. s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
  288. if (s == NULL) {
  289. BIO_printf(bio_err, "Module integrity MAC not found\n");
  290. goto end;
  291. }
  292. buf1 = OPENSSL_hexstr2buf(s, &len);
  293. if (buf1 == NULL
  294. || (size_t)len != module_mac_len
  295. || memcmp(module_mac, buf1, module_mac_len) != 0) {
  296. BIO_printf(bio_err, "Module integrity mismatch\n");
  297. goto end;
  298. }
  299. if (install_mac != NULL && install_mac_len > 0) {
  300. s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
  301. if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
  302. BIO_printf(bio_err, "install status not found\n");
  303. goto end;
  304. }
  305. s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
  306. if (s == NULL) {
  307. BIO_printf(bio_err, "Install indicator MAC not found\n");
  308. goto end;
  309. }
  310. buf2 = OPENSSL_hexstr2buf(s, &len);
  311. if (buf2 == NULL
  312. || (size_t)len != install_mac_len
  313. || memcmp(install_mac, buf2, install_mac_len) != 0) {
  314. BIO_printf(bio_err, "Install indicator status mismatch\n");
  315. goto end;
  316. }
  317. }
  318. ret = 1;
  319. end:
  320. OPENSSL_free(buf1);
  321. OPENSSL_free(buf2);
  322. NCONF_free(conf);
  323. return ret;
  324. }
  325. int fipsinstall_main(int argc, char **argv)
  326. {
  327. int ret = 1, verify = 0, gotkey = 0, gotdigest = 0, pedantic = 0;
  328. const char *section_name = "fips_sect";
  329. const char *mac_name = "HMAC";
  330. const char *prov_name = "fips";
  331. BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
  332. char *in_fname = NULL, *out_fname = NULL, *prog;
  333. char *module_fname = NULL, *parent_config = NULL, *module_path = NULL;
  334. const char *tail;
  335. EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
  336. STACK_OF(OPENSSL_STRING) *opts = NULL;
  337. OPTION_CHOICE o;
  338. unsigned char *read_buffer = NULL;
  339. unsigned char module_mac[EVP_MAX_MD_SIZE];
  340. size_t module_mac_len = EVP_MAX_MD_SIZE;
  341. unsigned char install_mac[EVP_MAX_MD_SIZE];
  342. size_t install_mac_len = EVP_MAX_MD_SIZE;
  343. EVP_MAC *mac = NULL;
  344. CONF *conf = NULL;
  345. if ((opts = sk_OPENSSL_STRING_new_null()) == NULL)
  346. goto end;
  347. prog = opt_init(argc, argv, fipsinstall_options);
  348. while ((o = opt_next()) != OPT_EOF) {
  349. switch (o) {
  350. case OPT_EOF:
  351. case OPT_ERR:
  352. opthelp:
  353. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  354. goto cleanup;
  355. case OPT_HELP:
  356. opt_help(fipsinstall_options);
  357. ret = 0;
  358. goto end;
  359. case OPT_IN:
  360. in_fname = opt_arg();
  361. break;
  362. case OPT_OUT:
  363. out_fname = opt_arg();
  364. break;
  365. case OPT_PEDANTIC:
  366. fips_opts = pedantic_opts;
  367. pedantic = 1;
  368. break;
  369. case OPT_NO_CONDITIONAL_ERRORS:
  370. if (!check_non_pedantic_fips(pedantic, "no_conditional_errors"))
  371. goto end;
  372. fips_opts.conditional_errors = 0;
  373. break;
  374. case OPT_NO_SECURITY_CHECKS:
  375. if (!check_non_pedantic_fips(pedantic, "no_security_checks"))
  376. goto end;
  377. fips_opts.security_checks = 0;
  378. break;
  379. case OPT_TLS_PRF_EMS_CHECK:
  380. fips_opts.tls_prf_ems_check = 1;
  381. break;
  382. case OPT_DISALLOW_DRGB_TRUNC_DIGEST:
  383. fips_opts.drgb_no_trunc_dgst = 1;
  384. break;
  385. case OPT_QUIET:
  386. quiet = 1;
  387. /* FALLTHROUGH */
  388. case OPT_NO_LOG:
  389. self_test_log = 0;
  390. break;
  391. case OPT_CORRUPT_DESC:
  392. self_test_corrupt_desc = opt_arg();
  393. break;
  394. case OPT_CORRUPT_TYPE:
  395. self_test_corrupt_type = opt_arg();
  396. break;
  397. case OPT_PROV_NAME:
  398. prov_name = opt_arg();
  399. break;
  400. case OPT_MODULE:
  401. module_fname = opt_arg();
  402. break;
  403. case OPT_SECTION_NAME:
  404. section_name = opt_arg();
  405. break;
  406. case OPT_MAC_NAME:
  407. mac_name = opt_arg();
  408. break;
  409. case OPT_CONFIG:
  410. parent_config = opt_arg();
  411. break;
  412. case OPT_MACOPT:
  413. if (!sk_OPENSSL_STRING_push(opts, opt_arg()))
  414. goto opthelp;
  415. if (HAS_PREFIX(opt_arg(), "hexkey:"))
  416. gotkey = 1;
  417. else if (HAS_PREFIX(opt_arg(), "digest:"))
  418. gotdigest = 1;
  419. break;
  420. case OPT_VERIFY:
  421. verify = 1;
  422. break;
  423. case OPT_SELF_TEST_ONLOAD:
  424. fips_opts.self_test_onload = 1;
  425. break;
  426. case OPT_SELF_TEST_ONINSTALL:
  427. if (!check_non_pedantic_fips(pedantic, "self_test_oninstall"))
  428. goto end;
  429. fips_opts.self_test_onload = 0;
  430. break;
  431. }
  432. }
  433. /* No extra arguments. */
  434. if (!opt_check_rest_arg(NULL))
  435. goto opthelp;
  436. if (verify && in_fname == NULL) {
  437. BIO_printf(bio_err, "Missing -in option for -verify\n");
  438. goto opthelp;
  439. }
  440. if (parent_config != NULL) {
  441. /* Test that a parent config can load the module */
  442. if (verify_module_load(parent_config)) {
  443. ret = OSSL_PROVIDER_available(NULL, prov_name) ? 0 : 1;
  444. if (!quiet) {
  445. BIO_printf(bio_err, "FIPS provider is %s\n",
  446. ret == 0 ? "available" : " not available");
  447. }
  448. }
  449. goto end;
  450. }
  451. if (module_fname == NULL)
  452. goto opthelp;
  453. tail = opt_path_end(module_fname);
  454. if (tail != NULL) {
  455. module_path = OPENSSL_strdup(module_fname);
  456. if (module_path == NULL)
  457. goto end;
  458. module_path[tail - module_fname] = '\0';
  459. if (!OSSL_PROVIDER_set_default_search_path(NULL, module_path))
  460. goto end;
  461. }
  462. if (self_test_log
  463. || self_test_corrupt_desc != NULL
  464. || self_test_corrupt_type != NULL)
  465. OSSL_SELF_TEST_set_callback(NULL, self_test_events, NULL);
  466. /* Use the default FIPS HMAC digest and key if not specified. */
  467. if (!gotdigest && !sk_OPENSSL_STRING_push(opts, "digest:SHA256"))
  468. goto end;
  469. if (!gotkey && !sk_OPENSSL_STRING_push(opts, "hexkey:" FIPS_KEY_STRING))
  470. goto end;
  471. module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY);
  472. if (module_bio == NULL) {
  473. BIO_printf(bio_err, "Failed to open module file\n");
  474. goto end;
  475. }
  476. read_buffer = app_malloc(BUFSIZE, "I/O buffer");
  477. if (read_buffer == NULL)
  478. goto end;
  479. mac = EVP_MAC_fetch(app_get0_libctx(), mac_name, app_get0_propq());
  480. if (mac == NULL) {
  481. BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
  482. goto end;
  483. }
  484. ctx = EVP_MAC_CTX_new(mac);
  485. if (ctx == NULL) {
  486. BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
  487. goto end;
  488. }
  489. if (opts != NULL) {
  490. int ok = 1;
  491. OSSL_PARAM *params =
  492. app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
  493. if (params == NULL)
  494. goto end;
  495. if (!EVP_MAC_CTX_set_params(ctx, params)) {
  496. BIO_printf(bio_err, "MAC parameter error\n");
  497. ERR_print_errors(bio_err);
  498. ok = 0;
  499. }
  500. app_params_free(params);
  501. if (!ok)
  502. goto end;
  503. }
  504. ctx2 = EVP_MAC_CTX_dup(ctx);
  505. if (ctx2 == NULL) {
  506. BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
  507. goto end;
  508. }
  509. if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
  510. goto end;
  511. if (fips_opts.self_test_onload == 0) {
  512. mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
  513. strlen(INSTALL_STATUS_VAL));
  514. if (mem_bio == NULL) {
  515. BIO_printf(bio_err, "Unable to create memory BIO\n");
  516. goto end;
  517. }
  518. if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
  519. goto end;
  520. } else {
  521. install_mac_len = 0;
  522. }
  523. if (verify) {
  524. if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
  525. install_mac, install_mac_len))
  526. goto end;
  527. if (!quiet)
  528. BIO_printf(bio_err, "VERIFY PASSED\n");
  529. } else {
  530. conf = generate_config_and_load(prov_name, section_name, module_mac,
  531. module_mac_len, &fips_opts);
  532. if (conf == NULL)
  533. goto end;
  534. if (!load_fips_prov_and_run_self_test(prov_name))
  535. goto end;
  536. fout =
  537. out_fname == NULL ? dup_bio_out(FORMAT_TEXT)
  538. : bio_open_default(out_fname, 'w', FORMAT_TEXT);
  539. if (fout == NULL) {
  540. BIO_printf(bio_err, "Failed to open file\n");
  541. goto end;
  542. }
  543. if (!write_config_fips_section(fout, section_name,
  544. module_mac, module_mac_len, &fips_opts,
  545. install_mac, install_mac_len))
  546. goto end;
  547. if (!quiet)
  548. BIO_printf(bio_err, "INSTALL PASSED\n");
  549. }
  550. ret = 0;
  551. end:
  552. if (ret == 1) {
  553. if (!quiet)
  554. BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
  555. ERR_print_errors(bio_err);
  556. }
  557. cleanup:
  558. OPENSSL_free(module_path);
  559. BIO_free(fout);
  560. BIO_free(mem_bio);
  561. BIO_free(module_bio);
  562. sk_OPENSSL_STRING_free(opts);
  563. EVP_MAC_free(mac);
  564. EVP_MAC_CTX_free(ctx2);
  565. EVP_MAC_CTX_free(ctx);
  566. OPENSSL_free(read_buffer);
  567. free_config_and_unload(conf);
  568. return ret;
  569. }
  570. static int self_test_events(const OSSL_PARAM params[], void *arg)
  571. {
  572. const OSSL_PARAM *p = NULL;
  573. const char *phase = NULL, *type = NULL, *desc = NULL;
  574. int ret = 0;
  575. p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
  576. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
  577. goto err;
  578. phase = (const char *)p->data;
  579. p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
  580. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
  581. goto err;
  582. desc = (const char *)p->data;
  583. p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
  584. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
  585. goto err;
  586. type = (const char *)p->data;
  587. if (self_test_log) {
  588. if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
  589. BIO_printf(bio_err, "%s : (%s) : ", desc, type);
  590. else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
  591. || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
  592. BIO_printf(bio_err, "%s\n", phase);
  593. }
  594. /*
  595. * The self test code will internally corrupt the KAT test result if an
  596. * error is returned during the corrupt phase.
  597. */
  598. if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0
  599. && (self_test_corrupt_desc != NULL
  600. || self_test_corrupt_type != NULL)) {
  601. if (self_test_corrupt_desc != NULL
  602. && strcmp(self_test_corrupt_desc, desc) != 0)
  603. goto end;
  604. if (self_test_corrupt_type != NULL
  605. && strcmp(self_test_corrupt_type, type) != 0)
  606. goto end;
  607. BIO_printf(bio_err, "%s ", phase);
  608. goto err;
  609. }
  610. end:
  611. ret = 1;
  612. err:
  613. return ret;
  614. }