self_test.c 11 KB

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