2
0

fipsprov.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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 <openssl/core_dispatch.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/params.h>
  12. #include <openssl/fips_names.h>
  13. #include <openssl/rand.h> /* RAND_get0_public() */
  14. #include "internal/cryptlib.h"
  15. #include "prov/implementations.h"
  16. #include "prov/provider_ctx.h"
  17. #include "prov/providercommon.h"
  18. #include "prov/providercommonerr.h"
  19. #include "prov/provider_util.h"
  20. #include "prov/seeding.h"
  21. #include "self_test.h"
  22. static const char FIPS_DEFAULT_PROPERTIES[] = "provider=fips,fips=yes";
  23. static const char FIPS_UNAPPROVED_PROPERTIES[] = "provider=fips,fips=no";
  24. /*
  25. * Forward declarations to ensure that interface functions are correctly
  26. * defined.
  27. */
  28. static OSSL_FUNC_provider_teardown_fn fips_teardown;
  29. static OSSL_FUNC_provider_gettable_params_fn fips_gettable_params;
  30. static OSSL_FUNC_provider_get_params_fn fips_get_params;
  31. static OSSL_FUNC_provider_query_operation_fn fips_query;
  32. #define ALGC(NAMES, FUNC, CHECK) { { NAMES, FIPS_DEFAULT_PROPERTIES, FUNC }, CHECK }
  33. #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
  34. extern OSSL_FUNC_core_thread_start_fn *c_thread_start;
  35. int FIPS_security_check_enabled(void);
  36. /*
  37. * TODO(3.0): Should these be stored in the provider side provctx? Could they
  38. * ever be different from one init to the next? Unfortunately we can't do this
  39. * at the moment because c_put_error/c_add_error_vdata do not provide
  40. * us with the OSSL_LIB_CTX as a parameter.
  41. */
  42. static SELF_TEST_POST_PARAMS selftest_params;
  43. static int fips_security_checks = 1;
  44. static const char *fips_security_check_option = "1";
  45. /* Functions provided by the core */
  46. static OSSL_FUNC_core_gettable_params_fn *c_gettable_params;
  47. static OSSL_FUNC_core_get_params_fn *c_get_params;
  48. OSSL_FUNC_core_thread_start_fn *c_thread_start;
  49. static OSSL_FUNC_core_new_error_fn *c_new_error;
  50. static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
  51. static OSSL_FUNC_core_vset_error_fn *c_vset_error;
  52. static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;
  53. static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;
  54. static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;
  55. static OSSL_FUNC_CRYPTO_malloc_fn *c_CRYPTO_malloc;
  56. static OSSL_FUNC_CRYPTO_zalloc_fn *c_CRYPTO_zalloc;
  57. static OSSL_FUNC_CRYPTO_free_fn *c_CRYPTO_free;
  58. static OSSL_FUNC_CRYPTO_clear_free_fn *c_CRYPTO_clear_free;
  59. static OSSL_FUNC_CRYPTO_realloc_fn *c_CRYPTO_realloc;
  60. static OSSL_FUNC_CRYPTO_clear_realloc_fn *c_CRYPTO_clear_realloc;
  61. static OSSL_FUNC_CRYPTO_secure_malloc_fn *c_CRYPTO_secure_malloc;
  62. static OSSL_FUNC_CRYPTO_secure_zalloc_fn *c_CRYPTO_secure_zalloc;
  63. static OSSL_FUNC_CRYPTO_secure_free_fn *c_CRYPTO_secure_free;
  64. static OSSL_FUNC_CRYPTO_secure_clear_free_fn *c_CRYPTO_secure_clear_free;
  65. static OSSL_FUNC_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated;
  66. static OSSL_FUNC_BIO_vsnprintf_fn *c_BIO_vsnprintf;
  67. static OSSL_FUNC_self_test_cb_fn *c_stcbfn = NULL;
  68. static OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
  69. typedef struct fips_global_st {
  70. const OSSL_CORE_HANDLE *handle;
  71. } FIPS_GLOBAL;
  72. static void *fips_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
  73. {
  74. FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
  75. return fgbl;
  76. }
  77. static void fips_prov_ossl_ctx_free(void *fgbl)
  78. {
  79. OPENSSL_free(fgbl);
  80. }
  81. static const OSSL_LIB_CTX_METHOD fips_prov_ossl_ctx_method = {
  82. fips_prov_ossl_ctx_new,
  83. fips_prov_ossl_ctx_free,
  84. };
  85. /* Parameters we provide to the core */
  86. static const OSSL_PARAM fips_param_types[] = {
  87. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
  88. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  89. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
  90. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
  91. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_SECURITY_CHECKS, OSSL_PARAM_INTEGER, NULL, 0),
  92. OSSL_PARAM_END
  93. };
  94. /*
  95. * Parameters to retrieve from the core provider - required for self testing.
  96. * NOTE: inside core_get_params() these will be loaded from config items
  97. * stored inside prov->parameters (except for
  98. * OSSL_PROV_PARAM_CORE_MODULE_FILENAME).
  99. * OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS is not a self test parameter.
  100. */
  101. static OSSL_PARAM core_params[] =
  102. {
  103. OSSL_PARAM_utf8_ptr(OSSL_PROV_PARAM_CORE_MODULE_FILENAME,
  104. selftest_params.module_filename,
  105. sizeof(selftest_params.module_filename)),
  106. OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_MODULE_MAC,
  107. selftest_params.module_checksum_data,
  108. sizeof(selftest_params.module_checksum_data)),
  109. OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_MAC,
  110. selftest_params.indicator_checksum_data,
  111. sizeof(selftest_params.indicator_checksum_data)),
  112. OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
  113. selftest_params.indicator_data,
  114. sizeof(selftest_params.indicator_data)),
  115. OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
  116. selftest_params.indicator_version,
  117. sizeof(selftest_params.indicator_version)),
  118. OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
  119. selftest_params.conditional_error_check,
  120. sizeof(selftest_params.conditional_error_check)),
  121. OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
  122. fips_security_check_option,
  123. sizeof(fips_security_check_option)),
  124. OSSL_PARAM_END
  125. };
  126. static const OSSL_PARAM *fips_gettable_params(void *provctx)
  127. {
  128. return fips_param_types;
  129. }
  130. static int fips_get_params(void *provctx, OSSL_PARAM params[])
  131. {
  132. OSSL_PARAM *p;
  133. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
  134. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
  135. return 0;
  136. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
  137. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
  138. return 0;
  139. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
  140. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
  141. return 0;
  142. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
  143. if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
  144. return 0;
  145. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_SECURITY_CHECKS);
  146. if (p != NULL && !OSSL_PARAM_set_int(p, fips_security_checks))
  147. return 0;
  148. return 1;
  149. }
  150. static void set_self_test_cb(const OSSL_CORE_HANDLE *handle)
  151. {
  152. if (c_stcbfn != NULL && c_get_libctx != NULL) {
  153. c_stcbfn(c_get_libctx(handle), &selftest_params.cb,
  154. &selftest_params.cb_arg);
  155. } else {
  156. selftest_params.cb = NULL;
  157. selftest_params.cb_arg = NULL;
  158. }
  159. }
  160. static int fips_self_test(void *provctx)
  161. {
  162. set_self_test_cb(FIPS_get_core_handle(selftest_params.libctx));
  163. return SELF_TEST_post(&selftest_params, 1) ? 1 : 0;
  164. }
  165. /*
  166. * For the algorithm names, we use the following formula for our primary
  167. * names:
  168. *
  169. * ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
  170. *
  171. * VERSION is only present if there are multiple versions of
  172. * an alg (MD2, MD4, MD5). It may be omitted if there is only
  173. * one version (if a subsequent version is released in the future,
  174. * we can always change the canonical name, and add the old name
  175. * as an alias).
  176. *
  177. * SUBNAME may be present where we are combining multiple
  178. * algorithms together, e.g. MD5-SHA1.
  179. *
  180. * SIZE is only present if multiple versions of an algorithm exist
  181. * with different sizes (e.g. AES-128-CBC, AES-256-CBC)
  182. *
  183. * MODE is only present where applicable.
  184. *
  185. * We add diverse other names where applicable, such as the names that
  186. * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
  187. * we have used historically.
  188. */
  189. static const OSSL_ALGORITHM fips_digests[] = {
  190. /* Our primary name:NiST name[:our older names] */
  191. { "SHA1:SHA-1:SSL3-SHA1", FIPS_DEFAULT_PROPERTIES, ossl_sha1_functions },
  192. { "SHA2-224:SHA-224:SHA224", FIPS_DEFAULT_PROPERTIES,
  193. ossl_sha224_functions },
  194. { "SHA2-256:SHA-256:SHA256", FIPS_DEFAULT_PROPERTIES,
  195. ossl_sha256_functions },
  196. { "SHA2-384:SHA-384:SHA384", FIPS_DEFAULT_PROPERTIES,
  197. ossl_sha384_functions },
  198. { "SHA2-512:SHA-512:SHA512", FIPS_DEFAULT_PROPERTIES,
  199. ossl_sha512_functions },
  200. { "SHA2-512/224:SHA-512/224:SHA512-224", FIPS_DEFAULT_PROPERTIES,
  201. ossl_sha512_224_functions },
  202. { "SHA2-512/256:SHA-512/256:SHA512-256", FIPS_DEFAULT_PROPERTIES,
  203. ossl_sha512_256_functions },
  204. /* We agree with NIST here, so one name only */
  205. { "SHA3-224", FIPS_DEFAULT_PROPERTIES, ossl_sha3_224_functions },
  206. { "SHA3-256", FIPS_DEFAULT_PROPERTIES, ossl_sha3_256_functions },
  207. { "SHA3-384", FIPS_DEFAULT_PROPERTIES, ossl_sha3_384_functions },
  208. { "SHA3-512", FIPS_DEFAULT_PROPERTIES, ossl_sha3_512_functions },
  209. { "SHAKE-128:SHAKE128", FIPS_DEFAULT_PROPERTIES, ossl_shake_128_functions },
  210. { "SHAKE-256:SHAKE256", FIPS_DEFAULT_PROPERTIES, ossl_shake_256_functions },
  211. /*
  212. * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
  213. * KMAC128 and KMAC256.
  214. */
  215. { "KECCAK-KMAC-128:KECCAK-KMAC128", FIPS_DEFAULT_PROPERTIES,
  216. ossl_keccak_kmac_128_functions },
  217. { "KECCAK-KMAC-256:KECCAK-KMAC256", FIPS_DEFAULT_PROPERTIES,
  218. ossl_keccak_kmac_256_functions },
  219. { NULL, NULL, NULL }
  220. };
  221. static const OSSL_ALGORITHM_CAPABLE fips_ciphers[] = {
  222. /* Our primary name[:ASN.1 OID name][:our older names] */
  223. ALG("AES-256-ECB", ossl_aes256ecb_functions),
  224. ALG("AES-192-ECB", ossl_aes192ecb_functions),
  225. ALG("AES-128-ECB", ossl_aes128ecb_functions),
  226. ALG("AES-256-CBC:AES256", ossl_aes256cbc_functions),
  227. ALG("AES-192-CBC:AES192", ossl_aes192cbc_functions),
  228. ALG("AES-128-CBC:AES128", ossl_aes128cbc_functions),
  229. ALG("AES-256-CBC-CTS", ossl_aes256cbc_cts_functions),
  230. ALG("AES-192-CBC-CTS", ossl_aes192cbc_cts_functions),
  231. ALG("AES-128-CBC-CTS", ossl_aes128cbc_cts_functions),
  232. ALG("AES-256-OFB", ossl_aes256ofb_functions),
  233. ALG("AES-192-OFB", ossl_aes192ofb_functions),
  234. ALG("AES-128-OFB", ossl_aes128ofb_functions),
  235. ALG("AES-256-CFB", ossl_aes256cfb_functions),
  236. ALG("AES-192-CFB", ossl_aes192cfb_functions),
  237. ALG("AES-128-CFB", ossl_aes128cfb_functions),
  238. ALG("AES-256-CFB1", ossl_aes256cfb1_functions),
  239. ALG("AES-192-CFB1", ossl_aes192cfb1_functions),
  240. ALG("AES-128-CFB1", ossl_aes128cfb1_functions),
  241. ALG("AES-256-CFB8", ossl_aes256cfb8_functions),
  242. ALG("AES-192-CFB8", ossl_aes192cfb8_functions),
  243. ALG("AES-128-CFB8", ossl_aes128cfb8_functions),
  244. ALG("AES-256-CTR", ossl_aes256ctr_functions),
  245. ALG("AES-192-CTR", ossl_aes192ctr_functions),
  246. ALG("AES-128-CTR", ossl_aes128ctr_functions),
  247. ALG("AES-256-XTS", ossl_aes256xts_functions),
  248. ALG("AES-128-XTS", ossl_aes128xts_functions),
  249. ALG("AES-256-GCM:id-aes256-GCM", ossl_aes256gcm_functions),
  250. ALG("AES-192-GCM:id-aes192-GCM", ossl_aes192gcm_functions),
  251. ALG("AES-128-GCM:id-aes128-GCM", ossl_aes128gcm_functions),
  252. ALG("AES-256-CCM:id-aes256-CCM", ossl_aes256ccm_functions),
  253. ALG("AES-192-CCM:id-aes192-CCM", ossl_aes192ccm_functions),
  254. ALG("AES-128-CCM:id-aes128-CCM", ossl_aes128ccm_functions),
  255. ALG("AES-256-WRAP:id-aes256-wrap:AES256-WRAP", ossl_aes256wrap_functions),
  256. ALG("AES-192-WRAP:id-aes192-wrap:AES192-WRAP", ossl_aes192wrap_functions),
  257. ALG("AES-128-WRAP:id-aes128-wrap:AES128-WRAP", ossl_aes128wrap_functions),
  258. ALG("AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD",
  259. ossl_aes256wrappad_functions),
  260. ALG("AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD",
  261. ossl_aes192wrappad_functions),
  262. ALG("AES-128-WRAP-PAD:id-aes128-wrap-pad:AES128-WRAP-PAD",
  263. ossl_aes128wrappad_functions),
  264. ALG("AES-256-WRAP-INV:AES256-WRAP-INV", ossl_aes256wrapinv_functions),
  265. ALG("AES-192-WRAP-INV:AES192-WRAP-INV", ossl_aes192wrapinv_functions),
  266. ALG("AES-128-WRAP-INV:AES128-WRAP-INV", ossl_aes128wrapinv_functions),
  267. ALG("AES-256-WRAP-PAD-INV:AES256-WRAP-PAD-INV",
  268. ossl_aes256wrappadinv_functions),
  269. ALG("AES-192-WRAP-PAD-INV:AES192-WRAP-PAD-INV",
  270. ossl_aes192wrappadinv_functions),
  271. ALG("AES-128-WRAP-PAD-INV:AES128-WRAP-PAD-INV",
  272. ossl_aes128wrappadinv_functions),
  273. ALGC("AES-128-CBC-HMAC-SHA1", ossl_aes128cbc_hmac_sha1_functions,
  274. ossl_cipher_capable_aes_cbc_hmac_sha1),
  275. ALGC("AES-256-CBC-HMAC-SHA1", ossl_aes256cbc_hmac_sha1_functions,
  276. ossl_cipher_capable_aes_cbc_hmac_sha1),
  277. ALGC("AES-128-CBC-HMAC-SHA256", ossl_aes128cbc_hmac_sha256_functions,
  278. ossl_cipher_capable_aes_cbc_hmac_sha256),
  279. ALGC("AES-256-CBC-HMAC-SHA256", ossl_aes256cbc_hmac_sha256_functions,
  280. ossl_cipher_capable_aes_cbc_hmac_sha256),
  281. #ifndef OPENSSL_NO_DES
  282. ALG("DES-EDE3-ECB:DES-EDE3", ossl_tdes_ede3_ecb_functions),
  283. ALG("DES-EDE3-CBC:DES3", ossl_tdes_ede3_cbc_functions),
  284. #endif /* OPENSSL_NO_DES */
  285. { { NULL, NULL, NULL }, NULL }
  286. };
  287. static OSSL_ALGORITHM exported_fips_ciphers[OSSL_NELEM(fips_ciphers)];
  288. static const OSSL_ALGORITHM fips_macs[] = {
  289. #ifndef OPENSSL_NO_CMAC
  290. { "CMAC", FIPS_DEFAULT_PROPERTIES, ossl_cmac_functions },
  291. #endif
  292. { "GMAC", FIPS_DEFAULT_PROPERTIES, ossl_gmac_functions },
  293. { "HMAC", FIPS_DEFAULT_PROPERTIES, ossl_hmac_functions },
  294. { "KMAC-128:KMAC128", FIPS_DEFAULT_PROPERTIES, ossl_kmac128_functions },
  295. { "KMAC-256:KMAC256", FIPS_DEFAULT_PROPERTIES, ossl_kmac256_functions },
  296. { NULL, NULL, NULL }
  297. };
  298. static const OSSL_ALGORITHM fips_kdfs[] = {
  299. { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_functions },
  300. { "SSKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_sskdf_functions },
  301. { "PBKDF2", FIPS_DEFAULT_PROPERTIES, ossl_kdf_pbkdf2_functions },
  302. { "SSHKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_sshkdf_functions },
  303. { "X963KDF:X942KDF-CONCAT", FIPS_DEFAULT_PROPERTIES,
  304. ossl_kdf_x963_kdf_functions },
  305. { "X942KDF-ASN1:X942KDF", FIPS_DEFAULT_PROPERTIES,
  306. ossl_kdf_x942_kdf_functions },
  307. { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_tls1_prf_functions },
  308. { "KBKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_kbkdf_functions },
  309. { NULL, NULL, NULL }
  310. };
  311. static const OSSL_ALGORITHM fips_rands[] = {
  312. { "CTR-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_ctr_functions },
  313. { "HASH-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_hash_functions },
  314. { "HMAC-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_ossl_hmac_functions },
  315. { "TEST-RAND", FIPS_UNAPPROVED_PROPERTIES, ossl_test_rng_functions },
  316. { NULL, NULL, NULL }
  317. };
  318. static const OSSL_ALGORITHM fips_keyexch[] = {
  319. #ifndef OPENSSL_NO_DH
  320. { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES, ossl_dh_keyexch_functions },
  321. #endif
  322. #ifndef OPENSSL_NO_EC
  323. { "ECDH", FIPS_DEFAULT_PROPERTIES, ecossl_dh_keyexch_functions },
  324. { "X25519", FIPS_DEFAULT_PROPERTIES, ossl_x25519_keyexch_functions },
  325. { "X448", FIPS_DEFAULT_PROPERTIES, ossl_x448_keyexch_functions },
  326. #endif
  327. { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES,
  328. ossl_kdf_tls1_prf_keyexch_functions },
  329. { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_keyexch_functions },
  330. { NULL, NULL, NULL }
  331. };
  332. static const OSSL_ALGORITHM fips_signature[] = {
  333. #ifndef OPENSSL_NO_DSA
  334. { "DSA:dsaEncryption", FIPS_DEFAULT_PROPERTIES,
  335. ossl_dsa_signature_functions },
  336. #endif
  337. { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
  338. ossl_rsa_signature_functions },
  339. #ifndef OPENSSL_NO_EC
  340. { "ED25519", FIPS_DEFAULT_PROPERTIES, ossl_ed25519_signature_functions },
  341. { "ED448", FIPS_DEFAULT_PROPERTIES, ossl_ed448_signature_functions },
  342. { "ECDSA", FIPS_DEFAULT_PROPERTIES, ecossl_dsa_signature_functions },
  343. #endif
  344. { "HMAC", FIPS_DEFAULT_PROPERTIES,
  345. ossl_mac_legacy_hmac_signature_functions },
  346. #ifndef OPENSSL_NO_CMAC
  347. { "CMAC", FIPS_DEFAULT_PROPERTIES,
  348. ossl_mac_legacy_cmac_signature_functions },
  349. #endif
  350. { NULL, NULL, NULL }
  351. };
  352. static const OSSL_ALGORITHM fips_asym_cipher[] = {
  353. { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
  354. ossl_rsa_asym_cipher_functions },
  355. { NULL, NULL, NULL }
  356. };
  357. static const OSSL_ALGORITHM fips_asym_kem[] = {
  358. { "RSA", FIPS_DEFAULT_PROPERTIES, ossl_rsa_asym_kem_functions },
  359. { NULL, NULL, NULL }
  360. };
  361. static const OSSL_ALGORITHM fips_keymgmt[] = {
  362. #ifndef OPENSSL_NO_DH
  363. { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES, ossl_dh_keymgmt_functions },
  364. { "DHX:X9.42 DH:dhpublicnumber", FIPS_DEFAULT_PROPERTIES,
  365. ossl_dhx_keymgmt_functions },
  366. #endif
  367. #ifndef OPENSSL_NO_DSA
  368. { "DSA", FIPS_DEFAULT_PROPERTIES, ossl_dsa_keymgmt_functions },
  369. #endif
  370. { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
  371. ossl_rsa_keymgmt_functions },
  372. { "RSA-PSS:RSASSA-PSS", FIPS_DEFAULT_PROPERTIES,
  373. ossl_rsapss_keymgmt_functions },
  374. #ifndef OPENSSL_NO_EC
  375. { "EC:id-ecPublicKey", FIPS_DEFAULT_PROPERTIES, ossl_ec_keymgmt_functions },
  376. { "X25519", FIPS_DEFAULT_PROPERTIES, ossl_x25519_keymgmt_functions },
  377. { "X448", FIPS_DEFAULT_PROPERTIES, ossl_x448_keymgmt_functions },
  378. { "ED25519", FIPS_DEFAULT_PROPERTIES, ossl_ed25519_keymgmt_functions },
  379. { "ED448", FIPS_DEFAULT_PROPERTIES, ossl_ed448_keymgmt_functions },
  380. #endif
  381. { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions },
  382. { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions },
  383. { "HMAC", FIPS_DEFAULT_PROPERTIES, ossl_mac_legacy_keymgmt_functions },
  384. #ifndef OPENSSL_NO_CMAC
  385. { "CMAC", FIPS_DEFAULT_PROPERTIES,
  386. ossl_cossl_mac_legacy_keymgmt_functions },
  387. #endif
  388. { NULL, NULL, NULL }
  389. };
  390. static const OSSL_ALGORITHM *fips_query(void *provctx, int operation_id,
  391. int *no_cache)
  392. {
  393. *no_cache = 0;
  394. if (!ossl_prov_is_running())
  395. return NULL;
  396. switch (operation_id) {
  397. case OSSL_OP_DIGEST:
  398. return fips_digests;
  399. case OSSL_OP_CIPHER:
  400. ossl_prov_cache_exported_algorithms(fips_ciphers,
  401. exported_fips_ciphers);
  402. return exported_fips_ciphers;
  403. case OSSL_OP_MAC:
  404. return fips_macs;
  405. case OSSL_OP_KDF:
  406. return fips_kdfs;
  407. case OSSL_OP_RAND:
  408. return fips_rands;
  409. case OSSL_OP_KEYMGMT:
  410. return fips_keymgmt;
  411. case OSSL_OP_KEYEXCH:
  412. return fips_keyexch;
  413. case OSSL_OP_SIGNATURE:
  414. return fips_signature;
  415. case OSSL_OP_ASYM_CIPHER:
  416. return fips_asym_cipher;
  417. case OSSL_OP_KEM:
  418. return fips_asym_kem;
  419. }
  420. return NULL;
  421. }
  422. static void fips_teardown(void *provctx)
  423. {
  424. OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
  425. ossl_prov_ctx_free(provctx);
  426. }
  427. static void fips_intern_teardown(void *provctx)
  428. {
  429. /*
  430. * We know that the library context is the same as for the outer provider,
  431. * so no need to destroy it here.
  432. */
  433. ossl_prov_ctx_free(provctx);
  434. }
  435. /* Functions we provide to the core */
  436. static const OSSL_DISPATCH fips_dispatch_table[] = {
  437. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_teardown },
  438. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params },
  439. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
  440. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
  441. { OSSL_FUNC_PROVIDER_GET_CAPABILITIES,
  442. (void (*)(void))provider_get_capabilities },
  443. { OSSL_FUNC_PROVIDER_SELF_TEST, (void (*)(void))fips_self_test },
  444. { 0, NULL }
  445. };
  446. /* Functions we provide to ourself */
  447. static const OSSL_DISPATCH intern_dispatch_table[] = {
  448. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_intern_teardown },
  449. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
  450. { 0, NULL }
  451. };
  452. int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
  453. const OSSL_DISPATCH *in,
  454. const OSSL_DISPATCH **out,
  455. void **provctx)
  456. {
  457. FIPS_GLOBAL *fgbl;
  458. OSSL_LIB_CTX *libctx = NULL;
  459. if (!ossl_prov_seeding_from_dispatch(in))
  460. return 0;
  461. for (; in->function_id != 0; in++) {
  462. switch (in->function_id) {
  463. case OSSL_FUNC_CORE_GET_LIBCTX:
  464. c_get_libctx = OSSL_FUNC_core_get_libctx(in);
  465. break;
  466. case OSSL_FUNC_CORE_GETTABLE_PARAMS:
  467. c_gettable_params = OSSL_FUNC_core_gettable_params(in);
  468. break;
  469. case OSSL_FUNC_CORE_GET_PARAMS:
  470. c_get_params = OSSL_FUNC_core_get_params(in);
  471. break;
  472. case OSSL_FUNC_CORE_THREAD_START:
  473. c_thread_start = OSSL_FUNC_core_thread_start(in);
  474. break;
  475. case OSSL_FUNC_CORE_NEW_ERROR:
  476. c_new_error = OSSL_FUNC_core_new_error(in);
  477. break;
  478. case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
  479. c_set_error_debug = OSSL_FUNC_core_set_error_debug(in);
  480. break;
  481. case OSSL_FUNC_CORE_VSET_ERROR:
  482. c_vset_error = OSSL_FUNC_core_vset_error(in);
  483. break;
  484. case OSSL_FUNC_CORE_SET_ERROR_MARK:
  485. c_set_error_mark = OSSL_FUNC_core_set_error_mark(in);
  486. break;
  487. case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
  488. c_clear_last_error_mark = OSSL_FUNC_core_clear_last_error_mark(in);
  489. break;
  490. case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
  491. c_pop_error_to_mark = OSSL_FUNC_core_pop_error_to_mark(in);
  492. break;
  493. case OSSL_FUNC_CRYPTO_MALLOC:
  494. c_CRYPTO_malloc = OSSL_FUNC_CRYPTO_malloc(in);
  495. break;
  496. case OSSL_FUNC_CRYPTO_ZALLOC:
  497. c_CRYPTO_zalloc = OSSL_FUNC_CRYPTO_zalloc(in);
  498. break;
  499. case OSSL_FUNC_CRYPTO_FREE:
  500. c_CRYPTO_free = OSSL_FUNC_CRYPTO_free(in);
  501. break;
  502. case OSSL_FUNC_CRYPTO_CLEAR_FREE:
  503. c_CRYPTO_clear_free = OSSL_FUNC_CRYPTO_clear_free(in);
  504. break;
  505. case OSSL_FUNC_CRYPTO_REALLOC:
  506. c_CRYPTO_realloc = OSSL_FUNC_CRYPTO_realloc(in);
  507. break;
  508. case OSSL_FUNC_CRYPTO_CLEAR_REALLOC:
  509. c_CRYPTO_clear_realloc = OSSL_FUNC_CRYPTO_clear_realloc(in);
  510. break;
  511. case OSSL_FUNC_CRYPTO_SECURE_MALLOC:
  512. c_CRYPTO_secure_malloc = OSSL_FUNC_CRYPTO_secure_malloc(in);
  513. break;
  514. case OSSL_FUNC_CRYPTO_SECURE_ZALLOC:
  515. c_CRYPTO_secure_zalloc = OSSL_FUNC_CRYPTO_secure_zalloc(in);
  516. break;
  517. case OSSL_FUNC_CRYPTO_SECURE_FREE:
  518. c_CRYPTO_secure_free = OSSL_FUNC_CRYPTO_secure_free(in);
  519. break;
  520. case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE:
  521. c_CRYPTO_secure_clear_free = OSSL_FUNC_CRYPTO_secure_clear_free(in);
  522. break;
  523. case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
  524. c_CRYPTO_secure_allocated = OSSL_FUNC_CRYPTO_secure_allocated(in);
  525. break;
  526. case OSSL_FUNC_BIO_NEW_FILE:
  527. selftest_params.bio_new_file_cb = OSSL_FUNC_BIO_new_file(in);
  528. break;
  529. case OSSL_FUNC_BIO_NEW_MEMBUF:
  530. selftest_params.bio_new_buffer_cb = OSSL_FUNC_BIO_new_membuf(in);
  531. break;
  532. case OSSL_FUNC_BIO_READ_EX:
  533. selftest_params.bio_read_ex_cb = OSSL_FUNC_BIO_read_ex(in);
  534. break;
  535. case OSSL_FUNC_BIO_FREE:
  536. selftest_params.bio_free_cb = OSSL_FUNC_BIO_free(in);
  537. break;
  538. case OSSL_FUNC_BIO_VSNPRINTF:
  539. c_BIO_vsnprintf = OSSL_FUNC_BIO_vsnprintf(in);
  540. break;
  541. case OSSL_FUNC_SELF_TEST_CB:
  542. c_stcbfn = OSSL_FUNC_self_test_cb(in);
  543. break;
  544. default:
  545. /* Just ignore anything we don't understand */
  546. break;
  547. }
  548. }
  549. set_self_test_cb(handle);
  550. if (!c_get_params(handle, core_params)) {
  551. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  552. return 0;
  553. }
  554. /* Disable the conditional error check if is disabled in the fips config file*/
  555. if (selftest_params.conditional_error_check != NULL
  556. && strcmp(selftest_params.conditional_error_check, "0") == 0)
  557. SELF_TEST_disable_conditional_error_state();
  558. /* Disable the security check if is disabled in the fips config file*/
  559. if (fips_security_check_option != NULL
  560. && strcmp(fips_security_check_option, "0") == 0)
  561. fips_security_checks = 0;
  562. /* Create a context. */
  563. if ((*provctx = ossl_prov_ctx_new()) == NULL
  564. || (libctx = OSSL_LIB_CTX_new()) == NULL) {
  565. /*
  566. * We free libctx separately here and only here because it hasn't
  567. * been attached to *provctx. All other error paths below rely
  568. * solely on fips_teardown.
  569. */
  570. OSSL_LIB_CTX_free(libctx);
  571. goto err;
  572. }
  573. ossl_prov_ctx_set0_libctx(*provctx, libctx);
  574. ossl_prov_ctx_set0_handle(*provctx, handle);
  575. if ((fgbl = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_FIPS_PROV_INDEX,
  576. &fips_prov_ossl_ctx_method)) == NULL)
  577. goto err;
  578. fgbl->handle = handle;
  579. selftest_params.libctx = libctx;
  580. if (!SELF_TEST_post(&selftest_params, 0)) {
  581. ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_POST_FAILURE);
  582. goto err;
  583. }
  584. /* TODO(3.0): Tests will hang if this is removed */
  585. (void)RAND_get0_public(libctx);
  586. *out = fips_dispatch_table;
  587. return 1;
  588. err:
  589. fips_teardown(*provctx);
  590. *provctx = NULL;
  591. return 0;
  592. }
  593. /*
  594. * The internal init function used when the FIPS module uses EVP to call
  595. * another algorithm also in the FIPS module. This is a recursive call that has
  596. * been made from within the FIPS module itself. To make this work, we populate
  597. * the provider context of this inner instance with the same library context
  598. * that was used in the EVP call that initiated this recursive call.
  599. */
  600. OSSL_provider_init_fn fips_intern_provider_init;
  601. int fips_intern_provider_init(const OSSL_CORE_HANDLE *handle,
  602. const OSSL_DISPATCH *in,
  603. const OSSL_DISPATCH **out,
  604. void **provctx)
  605. {
  606. OSSL_FUNC_core_get_libctx_fn *c_internal_get_libctx = NULL;
  607. for (; in->function_id != 0; in++) {
  608. switch (in->function_id) {
  609. case OSSL_FUNC_CORE_GET_LIBCTX:
  610. c_internal_get_libctx = OSSL_FUNC_core_get_libctx(in);
  611. break;
  612. default:
  613. break;
  614. }
  615. }
  616. if (c_internal_get_libctx == NULL)
  617. return 0;
  618. if ((*provctx = ossl_prov_ctx_new()) == NULL)
  619. return 0;
  620. /*
  621. * Using the parent library context only works because we are a built-in
  622. * internal provider. This is not something that most providers would be
  623. * able to do.
  624. */
  625. ossl_prov_ctx_set0_libctx(*provctx,
  626. (OSSL_LIB_CTX *)c_internal_get_libctx(handle));
  627. ossl_prov_ctx_set0_handle(*provctx, handle);
  628. *out = intern_dispatch_table;
  629. return 1;
  630. }
  631. void ERR_new(void)
  632. {
  633. c_new_error(NULL);
  634. }
  635. void ERR_set_debug(const char *file, int line, const char *func)
  636. {
  637. c_set_error_debug(NULL, file, line, func);
  638. }
  639. void ERR_set_error(int lib, int reason, const char *fmt, ...)
  640. {
  641. va_list args;
  642. va_start(args, fmt);
  643. c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
  644. va_end(args);
  645. }
  646. void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
  647. {
  648. c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
  649. }
  650. int ERR_set_mark(void)
  651. {
  652. return c_set_error_mark(NULL);
  653. }
  654. int ERR_clear_last_mark(void)
  655. {
  656. return c_clear_last_error_mark(NULL);
  657. }
  658. int ERR_pop_to_mark(void)
  659. {
  660. return c_pop_error_to_mark(NULL);
  661. }
  662. /*
  663. * This must take a library context, since it's called from the depths
  664. * of crypto/initthread.c code, where it's (correctly) assumed that the
  665. * passed caller argument is an OSSL_LIB_CTX pointer (since the same routine
  666. * is also called from other parts of libcrypto, which all pass around a
  667. * OSSL_LIB_CTX pointer)
  668. */
  669. const OSSL_CORE_HANDLE *FIPS_get_core_handle(OSSL_LIB_CTX *libctx)
  670. {
  671. FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(libctx,
  672. OSSL_LIB_CTX_FIPS_PROV_INDEX,
  673. &fips_prov_ossl_ctx_method);
  674. if (fgbl == NULL)
  675. return NULL;
  676. return fgbl->handle;
  677. }
  678. void *CRYPTO_malloc(size_t num, const char *file, int line)
  679. {
  680. return c_CRYPTO_malloc(num, file, line);
  681. }
  682. void *CRYPTO_zalloc(size_t num, const char *file, int line)
  683. {
  684. return c_CRYPTO_zalloc(num, file, line);
  685. }
  686. void CRYPTO_free(void *ptr, const char *file, int line)
  687. {
  688. c_CRYPTO_free(ptr, file, line);
  689. }
  690. void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line)
  691. {
  692. c_CRYPTO_clear_free(ptr, num, file, line);
  693. }
  694. void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
  695. {
  696. return c_CRYPTO_realloc(addr, num, file, line);
  697. }
  698. void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
  699. const char *file, int line)
  700. {
  701. return c_CRYPTO_clear_realloc(addr, old_num, num, file, line);
  702. }
  703. void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
  704. {
  705. return c_CRYPTO_secure_malloc(num, file, line);
  706. }
  707. void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
  708. {
  709. return c_CRYPTO_secure_zalloc(num, file, line);
  710. }
  711. void CRYPTO_secure_free(void *ptr, const char *file, int line)
  712. {
  713. c_CRYPTO_secure_free(ptr, file, line);
  714. }
  715. void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *file, int line)
  716. {
  717. c_CRYPTO_secure_clear_free(ptr, num, file, line);
  718. }
  719. int CRYPTO_secure_allocated(const void *ptr)
  720. {
  721. return c_CRYPTO_secure_allocated(ptr);
  722. }
  723. int BIO_snprintf(char *buf, size_t n, const char *format, ...)
  724. {
  725. va_list args;
  726. int ret;
  727. va_start(args, format);
  728. ret = c_BIO_vsnprintf(buf, n, format, args);
  729. va_end(args);
  730. return ret;
  731. }
  732. int FIPS_security_check_enabled(void)
  733. {
  734. return fips_security_checks;
  735. }
  736. void OSSL_SELF_TEST_get_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK **cb,
  737. void **cbarg)
  738. {
  739. if (libctx == NULL)
  740. libctx = selftest_params.libctx;
  741. if (c_stcbfn != NULL && c_get_libctx != NULL) {
  742. /* Get the parent libctx */
  743. c_stcbfn(c_get_libctx(FIPS_get_core_handle(libctx)), cb, cbarg);
  744. } else {
  745. if (cb != NULL)
  746. *cb = NULL;
  747. if (cbarg != NULL)
  748. *cbarg = NULL;
  749. }
  750. }