fipsinstall.c 18 KB

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