fipsprov.c 30 KB

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