self_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 OPENSSL_CTX. That doesn't work with the self test though because
  22. * it should be run once regardless of the number of OPENSSL_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. #endif
  113. #if defined(DEP_INIT_ATTRIBUTE) && defined(DEP_FINI_ATTRIBUTE)
  114. DEP_INIT_ATTRIBUTE void init(void)
  115. {
  116. FIPS_state = FIPS_STATE_SELFTEST;
  117. }
  118. DEP_FINI_ATTRIBUTE void cleanup(void)
  119. {
  120. CRYPTO_THREAD_lock_free(self_test_lock);
  121. }
  122. #endif
  123. /*
  124. * Calculate the HMAC SHA256 of data read using a BIO and read_cb, and verify
  125. * the result matches the expected value.
  126. * Return 1 if verified, or 0 if it fails.
  127. */
  128. static int verify_integrity(OSSL_CORE_BIO *bio, OSSL_FUNC_BIO_read_ex_fn read_ex_cb,
  129. unsigned char *expected, size_t expected_len,
  130. OPENSSL_CTX *libctx, OSSL_SELF_TEST *ev,
  131. const char *event_type)
  132. {
  133. int ret = 0, status;
  134. unsigned char out[MAX_MD_SIZE];
  135. unsigned char buf[INTEGRITY_BUF_SIZE];
  136. size_t bytes_read = 0, out_len = 0;
  137. EVP_MAC *mac = NULL;
  138. EVP_MAC_CTX *ctx = NULL;
  139. OSSL_PARAM params[3], *p = params;
  140. OSSL_SELF_TEST_onbegin(ev, event_type, OSSL_SELF_TEST_DESC_INTEGRITY_HMAC);
  141. mac = EVP_MAC_fetch(libctx, MAC_NAME, NULL);
  142. ctx = EVP_MAC_CTX_new(mac);
  143. if (mac == NULL || ctx == NULL)
  144. goto err;
  145. *p++ = OSSL_PARAM_construct_utf8_string("digest", DIGEST_NAME,
  146. strlen(DIGEST_NAME) + 1);
  147. *p++ = OSSL_PARAM_construct_octet_string("key", fixed_key,
  148. sizeof(fixed_key));
  149. *p = OSSL_PARAM_construct_end();
  150. if (EVP_MAC_CTX_set_params(ctx, params) <= 0
  151. || !EVP_MAC_init(ctx))
  152. goto err;
  153. while (1) {
  154. status = read_ex_cb(bio, buf, sizeof(buf), &bytes_read);
  155. if (status != 1)
  156. break;
  157. if (!EVP_MAC_update(ctx, buf, bytes_read))
  158. goto err;
  159. }
  160. if (!EVP_MAC_final(ctx, out, &out_len, sizeof(out)))
  161. goto err;
  162. OSSL_SELF_TEST_oncorrupt_byte(ev, out);
  163. if (expected_len != out_len
  164. || memcmp(expected, out, out_len) != 0)
  165. goto err;
  166. ret = 1;
  167. err:
  168. OSSL_SELF_TEST_onend(ev, ret);
  169. EVP_MAC_CTX_free(ctx);
  170. EVP_MAC_free(mac);
  171. return ret;
  172. }
  173. /* This API is triggered either on loading of the FIPS module or on demand */
  174. int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
  175. {
  176. int ok = 0;
  177. int kats_already_passed = 0;
  178. long checksum_len;
  179. OSSL_CORE_BIO *bio_module = NULL, *bio_indicator = NULL;
  180. unsigned char *module_checksum = NULL;
  181. unsigned char *indicator_checksum = NULL;
  182. int loclstate;
  183. OSSL_SELF_TEST *ev = NULL;
  184. if (!RUN_ONCE(&fips_self_test_init, do_fips_self_test_init))
  185. return 0;
  186. CRYPTO_THREAD_read_lock(self_test_lock);
  187. loclstate = FIPS_state;
  188. CRYPTO_THREAD_unlock(self_test_lock);
  189. if (loclstate == FIPS_STATE_RUNNING) {
  190. if (!on_demand_test)
  191. return 1;
  192. } else if (loclstate != FIPS_STATE_SELFTEST) {
  193. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
  194. return 0;
  195. }
  196. CRYPTO_THREAD_write_lock(self_test_lock);
  197. if (FIPS_state == FIPS_STATE_RUNNING) {
  198. if (!on_demand_test) {
  199. CRYPTO_THREAD_unlock(self_test_lock);
  200. return 1;
  201. }
  202. FIPS_state = FIPS_STATE_SELFTEST;
  203. } else if (FIPS_state != FIPS_STATE_SELFTEST) {
  204. CRYPTO_THREAD_unlock(self_test_lock);
  205. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
  206. return 0;
  207. }
  208. if (st == NULL
  209. || st->module_checksum_data == NULL) {
  210. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
  211. goto end;
  212. }
  213. ev = OSSL_SELF_TEST_new(st->cb, st->cb_arg);
  214. if (ev == NULL)
  215. goto end;
  216. module_checksum = OPENSSL_hexstr2buf(st->module_checksum_data,
  217. &checksum_len);
  218. if (module_checksum == NULL) {
  219. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
  220. goto end;
  221. }
  222. bio_module = (*st->bio_new_file_cb)(st->module_filename, "rb");
  223. /* Always check the integrity of the fips module */
  224. if (bio_module == NULL
  225. || !verify_integrity(bio_module, st->bio_read_ex_cb,
  226. module_checksum, checksum_len, st->libctx,
  227. ev, OSSL_SELF_TEST_TYPE_MODULE_INTEGRITY)) {
  228. ERR_raise(ERR_LIB_PROV, PROV_R_MODULE_INTEGRITY_FAILURE);
  229. goto end;
  230. }
  231. /* This will be NULL during installation - so the self test KATS will run */
  232. if (st->indicator_data != NULL) {
  233. /*
  234. * If the kats have already passed indicator is set - then check the
  235. * integrity of the indicator.
  236. */
  237. if (st->indicator_checksum_data == NULL) {
  238. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
  239. goto end;
  240. }
  241. indicator_checksum = OPENSSL_hexstr2buf(st->indicator_checksum_data,
  242. &checksum_len);
  243. if (indicator_checksum == NULL) {
  244. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
  245. goto end;
  246. }
  247. bio_indicator =
  248. (*st->bio_new_buffer_cb)(st->indicator_data,
  249. strlen(st->indicator_data));
  250. if (bio_indicator == NULL
  251. || !verify_integrity(bio_indicator, st->bio_read_ex_cb,
  252. indicator_checksum, checksum_len,
  253. st->libctx, ev,
  254. OSSL_SELF_TEST_TYPE_INSTALL_INTEGRITY)) {
  255. ERR_raise(ERR_LIB_PROV, PROV_R_INDICATOR_INTEGRITY_FAILURE);
  256. goto end;
  257. } else {
  258. kats_already_passed = 1;
  259. }
  260. }
  261. /* Only runs the KAT's during installation OR on_demand() */
  262. if (on_demand_test || kats_already_passed == 0) {
  263. if (!SELF_TEST_kats(ev, st->libctx)) {
  264. ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_KAT_FAILURE);
  265. goto end;
  266. }
  267. }
  268. ok = 1;
  269. end:
  270. OSSL_SELF_TEST_free(ev);
  271. OPENSSL_free(module_checksum);
  272. OPENSSL_free(indicator_checksum);
  273. if (st != NULL) {
  274. (*st->bio_free_cb)(bio_indicator);
  275. (*st->bio_free_cb)(bio_module);
  276. }
  277. if (ok)
  278. FIPS_state = FIPS_STATE_RUNNING;
  279. else
  280. ossl_set_error_state(OSSL_SELF_TEST_TYPE_NONE);
  281. CRYPTO_THREAD_unlock(self_test_lock);
  282. return ok;
  283. }
  284. void SELF_TEST_disable_conditional_error_state(void)
  285. {
  286. FIPS_conditional_error_check = 0;
  287. }
  288. void ossl_set_error_state(const char *type)
  289. {
  290. int cond_test = (type != NULL && strcmp(type, OSSL_SELF_TEST_TYPE_PCT) == 0);
  291. if (!cond_test || (FIPS_conditional_error_check == 1)) {
  292. FIPS_state = FIPS_STATE_ERROR;
  293. ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_ENTERING_ERROR_STATE);
  294. } else {
  295. ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_CONDITIONAL_ERROR);
  296. }
  297. }
  298. int ossl_prov_is_running(void)
  299. {
  300. const int res = FIPS_state == FIPS_STATE_RUNNING
  301. || FIPS_state == FIPS_STATE_SELFTEST;
  302. static unsigned int rate_limit = 0;
  303. if (res) {
  304. rate_limit = 0;
  305. } else if (FIPS_state == FIPS_STATE_ERROR) {
  306. if (rate_limit++ < FIPS_ERROR_REPORTING_RATE_LIMIT)
  307. ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_IN_ERROR_STATE);
  308. }
  309. return res;
  310. }