fipsinstall.c 18 KB

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