self_test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright 2019-2022 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/params.h>
  12. #include <openssl/crypto.h>
  13. #include "internal/cryptlib.h"
  14. #include <openssl/fipskey.h>
  15. #include <openssl/err.h>
  16. #include <openssl/proverr.h>
  17. #include "internal/e_os.h"
  18. #include "prov/providercommon.h"
  19. /*
  20. * We're cheating here. Normally we don't allow RUN_ONCE usage inside the FIPS
  21. * module because all such initialisation should be associated with an
  22. * individual OSSL_LIB_CTX. That doesn't work with the self test though because
  23. * it should be run once regardless of the number of OSSL_LIB_CTXs we have.
  24. */
  25. #define ALLOW_RUN_ONCE_IN_FIPS
  26. #include "internal/thread_once.h"
  27. #include "self_test.h"
  28. #define FIPS_STATE_INIT 0
  29. #define FIPS_STATE_SELFTEST 1
  30. #define FIPS_STATE_RUNNING 2
  31. #define FIPS_STATE_ERROR 3
  32. /*
  33. * The number of times the module will report it is in the error state
  34. * before going quiet.
  35. */
  36. #define FIPS_ERROR_REPORTING_RATE_LIMIT 10
  37. /* The size of a temp buffer used to read in data */
  38. #define INTEGRITY_BUF_SIZE (4096)
  39. #define MAX_MD_SIZE 64
  40. #define MAC_NAME "HMAC"
  41. #define DIGEST_NAME "SHA256"
  42. static int FIPS_conditional_error_check = 1;
  43. static CRYPTO_RWLOCK *self_test_lock = NULL;
  44. static CRYPTO_RWLOCK *fips_state_lock = NULL;
  45. static unsigned char fixed_key[32] = { FIPS_KEY_ELEMENTS };
  46. static CRYPTO_ONCE fips_self_test_init = CRYPTO_ONCE_STATIC_INIT;
  47. DEFINE_RUN_ONCE_STATIC(do_fips_self_test_init)
  48. {
  49. /*
  50. * These locks get freed in platform specific ways that may occur after we
  51. * do mem leak checking. If we don't know how to free it for a particular
  52. * platform then we just leak it deliberately.
  53. */
  54. self_test_lock = CRYPTO_THREAD_lock_new();
  55. fips_state_lock = CRYPTO_THREAD_lock_new();
  56. return self_test_lock != NULL;
  57. }
  58. /*
  59. * Declarations for the DEP entry/exit points.
  60. * Ones not required or incorrect need to be undefined or redefined respectively.
  61. */
  62. #define DEP_INITIAL_STATE FIPS_STATE_INIT
  63. #define DEP_INIT_ATTRIBUTE static
  64. #define DEP_FINI_ATTRIBUTE static
  65. static void init(void);
  66. static void cleanup(void);
  67. /*
  68. * This is the Default Entry Point (DEP) code.
  69. * See FIPS 140-2 IG 9.10
  70. */
  71. #if defined(_WIN32) || defined(__CYGWIN__)
  72. # ifdef __CYGWIN__
  73. /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
  74. # include <windows.h>
  75. /*
  76. * this has side-effect of _WIN32 getting defined, which otherwise is
  77. * mutually exclusive with __CYGWIN__...
  78. */
  79. # endif
  80. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
  81. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  82. {
  83. switch (fdwReason) {
  84. case DLL_PROCESS_ATTACH:
  85. init();
  86. break;
  87. case DLL_PROCESS_DETACH:
  88. cleanup();
  89. break;
  90. default:
  91. break;
  92. }
  93. return TRUE;
  94. }
  95. #elif defined(__GNUC__) && !defined(_AIX)
  96. # undef DEP_INIT_ATTRIBUTE
  97. # undef DEP_FINI_ATTRIBUTE
  98. # define DEP_INIT_ATTRIBUTE static __attribute__((constructor))
  99. # define DEP_FINI_ATTRIBUTE static __attribute__((destructor))
  100. #elif defined(__sun)
  101. # pragma init(init)
  102. # pragma fini(cleanup)
  103. #elif defined(_AIX) && !defined(__GNUC__)
  104. void _init(void);
  105. void _cleanup(void);
  106. # pragma init(_init)
  107. # pragma fini(_cleanup)
  108. void _init(void)
  109. {
  110. init();
  111. }
  112. void _cleanup(void)
  113. {
  114. cleanup();
  115. }
  116. #elif defined(__hpux)
  117. # pragma init "init"
  118. # pragma fini "cleanup"
  119. #elif defined(__TANDEM)
  120. /* Method automatically called by the NonStop OS when the DLL loads */
  121. void __INIT__init(void) {
  122. init();
  123. }
  124. /* Method automatically called by the NonStop OS prior to unloading the DLL */
  125. void __TERM__cleanup(void) {
  126. cleanup();
  127. }
  128. #else
  129. /*
  130. * This build does not support any kind of DEP.
  131. * We force the self-tests to run as part of the FIPS provider initialisation
  132. * rather than being triggered by the DEP.
  133. */
  134. # undef DEP_INIT_ATTRIBUTE
  135. # undef DEP_FINI_ATTRIBUTE
  136. # undef DEP_INITIAL_STATE
  137. # define DEP_INITIAL_STATE FIPS_STATE_SELFTEST
  138. #endif
  139. static int FIPS_state = DEP_INITIAL_STATE;
  140. #if defined(DEP_INIT_ATTRIBUTE)
  141. DEP_INIT_ATTRIBUTE void init(void)
  142. {
  143. FIPS_state = FIPS_STATE_SELFTEST;
  144. }
  145. #endif
  146. #if defined(DEP_FINI_ATTRIBUTE)
  147. DEP_FINI_ATTRIBUTE void cleanup(void)
  148. {
  149. CRYPTO_THREAD_lock_free(self_test_lock);
  150. CRYPTO_THREAD_lock_free(fips_state_lock);
  151. }
  152. #endif
  153. /*
  154. * Calculate the HMAC SHA256 of data read using a BIO and read_cb, and verify
  155. * the result matches the expected value.
  156. * Return 1 if verified, or 0 if it fails.
  157. */
  158. static int verify_integrity(OSSL_CORE_BIO *bio, OSSL_FUNC_BIO_read_ex_fn read_ex_cb,
  159. unsigned char *expected, size_t expected_len,
  160. OSSL_LIB_CTX *libctx, OSSL_SELF_TEST *ev,
  161. const char *event_type)
  162. {
  163. int ret = 0, status;
  164. unsigned char out[MAX_MD_SIZE];
  165. unsigned char buf[INTEGRITY_BUF_SIZE];
  166. size_t bytes_read = 0, out_len = 0;
  167. EVP_MAC *mac = NULL;
  168. EVP_MAC_CTX *ctx = NULL;
  169. OSSL_PARAM params[2], *p = params;
  170. OSSL_SELF_TEST_onbegin(ev, event_type, OSSL_SELF_TEST_DESC_INTEGRITY_HMAC);
  171. mac = EVP_MAC_fetch(libctx, MAC_NAME, NULL);
  172. if (mac == NULL)
  173. goto err;
  174. ctx = EVP_MAC_CTX_new(mac);
  175. if (ctx == NULL)
  176. goto err;
  177. *p++ = OSSL_PARAM_construct_utf8_string("digest", DIGEST_NAME, 0);
  178. *p = OSSL_PARAM_construct_end();
  179. if (!EVP_MAC_init(ctx, fixed_key, sizeof(fixed_key), params))
  180. goto err;
  181. while (1) {
  182. status = read_ex_cb(bio, buf, sizeof(buf), &bytes_read);
  183. if (status != 1)
  184. break;
  185. if (!EVP_MAC_update(ctx, buf, bytes_read))
  186. goto err;
  187. }
  188. if (!EVP_MAC_final(ctx, out, &out_len, sizeof(out)))
  189. goto err;
  190. OSSL_SELF_TEST_oncorrupt_byte(ev, out);
  191. if (expected_len != out_len
  192. || memcmp(expected, out, out_len) != 0)
  193. goto err;
  194. ret = 1;
  195. err:
  196. OSSL_SELF_TEST_onend(ev, ret);
  197. EVP_MAC_CTX_free(ctx);
  198. EVP_MAC_free(mac);
  199. return ret;
  200. }
  201. static void set_fips_state(int state)
  202. {
  203. if (ossl_assert(CRYPTO_THREAD_write_lock(fips_state_lock) != 0)) {
  204. FIPS_state = state;
  205. CRYPTO_THREAD_unlock(fips_state_lock);
  206. }
  207. }
  208. /* This API is triggered either on loading of the FIPS module or on demand */
  209. int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
  210. {
  211. int ok = 0;
  212. int kats_already_passed = 0;
  213. long checksum_len;
  214. OSSL_CORE_BIO *bio_module = NULL, *bio_indicator = NULL;
  215. unsigned char *module_checksum = NULL;
  216. unsigned char *indicator_checksum = NULL;
  217. int loclstate;
  218. OSSL_SELF_TEST *ev = NULL;
  219. if (!RUN_ONCE(&fips_self_test_init, do_fips_self_test_init))
  220. return 0;
  221. if (!CRYPTO_THREAD_read_lock(fips_state_lock))
  222. return 0;
  223. loclstate = FIPS_state;
  224. CRYPTO_THREAD_unlock(fips_state_lock);
  225. if (loclstate == FIPS_STATE_RUNNING) {
  226. if (!on_demand_test)
  227. return 1;
  228. } else if (loclstate != FIPS_STATE_SELFTEST) {
  229. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
  230. return 0;
  231. }
  232. if (!CRYPTO_THREAD_write_lock(self_test_lock))
  233. return 0;
  234. if (!CRYPTO_THREAD_read_lock(fips_state_lock)) {
  235. CRYPTO_THREAD_unlock(self_test_lock);
  236. return 0;
  237. }
  238. if (FIPS_state == FIPS_STATE_RUNNING) {
  239. CRYPTO_THREAD_unlock(fips_state_lock);
  240. if (!on_demand_test) {
  241. CRYPTO_THREAD_unlock(self_test_lock);
  242. return 1;
  243. }
  244. set_fips_state(FIPS_STATE_SELFTEST);
  245. } else if (FIPS_state != FIPS_STATE_SELFTEST) {
  246. CRYPTO_THREAD_unlock(fips_state_lock);
  247. CRYPTO_THREAD_unlock(self_test_lock);
  248. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
  249. return 0;
  250. } else {
  251. CRYPTO_THREAD_unlock(fips_state_lock);
  252. }
  253. if (st == NULL
  254. || st->module_checksum_data == NULL) {
  255. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
  256. goto end;
  257. }
  258. ev = OSSL_SELF_TEST_new(st->cb, st->cb_arg);
  259. if (ev == NULL)
  260. goto end;
  261. module_checksum = OPENSSL_hexstr2buf(st->module_checksum_data,
  262. &checksum_len);
  263. if (module_checksum == NULL) {
  264. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
  265. goto end;
  266. }
  267. bio_module = (*st->bio_new_file_cb)(st->module_filename, "rb");
  268. /* Always check the integrity of the fips module */
  269. if (bio_module == NULL
  270. || !verify_integrity(bio_module, st->bio_read_ex_cb,
  271. module_checksum, checksum_len, st->libctx,
  272. ev, OSSL_SELF_TEST_TYPE_MODULE_INTEGRITY)) {
  273. ERR_raise(ERR_LIB_PROV, PROV_R_MODULE_INTEGRITY_FAILURE);
  274. goto end;
  275. }
  276. /* This will be NULL during installation - so the self test KATS will run */
  277. if (st->indicator_data != NULL) {
  278. /*
  279. * If the kats have already passed indicator is set - then check the
  280. * integrity of the indicator.
  281. */
  282. if (st->indicator_checksum_data == NULL) {
  283. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
  284. goto end;
  285. }
  286. indicator_checksum = OPENSSL_hexstr2buf(st->indicator_checksum_data,
  287. &checksum_len);
  288. if (indicator_checksum == NULL) {
  289. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
  290. goto end;
  291. }
  292. bio_indicator =
  293. (*st->bio_new_buffer_cb)(st->indicator_data,
  294. strlen(st->indicator_data));
  295. if (bio_indicator == NULL
  296. || !verify_integrity(bio_indicator, st->bio_read_ex_cb,
  297. indicator_checksum, checksum_len,
  298. st->libctx, ev,
  299. OSSL_SELF_TEST_TYPE_INSTALL_INTEGRITY)) {
  300. ERR_raise(ERR_LIB_PROV, PROV_R_INDICATOR_INTEGRITY_FAILURE);
  301. goto end;
  302. } else {
  303. kats_already_passed = 1;
  304. }
  305. }
  306. /*
  307. * Only runs the KAT's during installation OR on_demand().
  308. * NOTE: If the installation option 'self_test_onload' is chosen then this
  309. * path will always be run, since kats_already_passed will always be 0.
  310. */
  311. if (on_demand_test || kats_already_passed == 0) {
  312. if (!SELF_TEST_kats(ev, st->libctx)) {
  313. ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_KAT_FAILURE);
  314. goto end;
  315. }
  316. }
  317. ok = 1;
  318. end:
  319. OSSL_SELF_TEST_free(ev);
  320. OPENSSL_free(module_checksum);
  321. OPENSSL_free(indicator_checksum);
  322. if (st != NULL) {
  323. (*st->bio_free_cb)(bio_indicator);
  324. (*st->bio_free_cb)(bio_module);
  325. }
  326. if (ok)
  327. set_fips_state(FIPS_STATE_RUNNING);
  328. else
  329. ossl_set_error_state(OSSL_SELF_TEST_TYPE_NONE);
  330. CRYPTO_THREAD_unlock(self_test_lock);
  331. return ok;
  332. }
  333. void SELF_TEST_disable_conditional_error_state(void)
  334. {
  335. FIPS_conditional_error_check = 0;
  336. }
  337. void ossl_set_error_state(const char *type)
  338. {
  339. int cond_test = (type != NULL && strcmp(type, OSSL_SELF_TEST_TYPE_PCT) == 0);
  340. if (!cond_test || (FIPS_conditional_error_check == 1)) {
  341. set_fips_state(FIPS_STATE_ERROR);
  342. ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_ENTERING_ERROR_STATE);
  343. } else {
  344. ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_CONDITIONAL_ERROR);
  345. }
  346. }
  347. int ossl_prov_is_running(void)
  348. {
  349. int res;
  350. static unsigned int rate_limit = 0;
  351. if (!CRYPTO_THREAD_read_lock(fips_state_lock))
  352. return 0;
  353. res = FIPS_state == FIPS_STATE_RUNNING
  354. || FIPS_state == FIPS_STATE_SELFTEST;
  355. if (FIPS_state == FIPS_STATE_ERROR) {
  356. CRYPTO_THREAD_unlock(fips_state_lock);
  357. if (!CRYPTO_THREAD_write_lock(fips_state_lock))
  358. return 0;
  359. if (rate_limit++ < FIPS_ERROR_REPORTING_RATE_LIMIT)
  360. ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_IN_ERROR_STATE);
  361. }
  362. CRYPTO_THREAD_unlock(fips_state_lock);
  363. return res;
  364. }