self_test.c 12 KB

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