fipsinstall.c 20 KB

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