2
0

fipsprov.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /*
  2. * Copyright 2019-2023 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 <assert.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/params.h>
  13. #include <openssl/fips_names.h>
  14. #include <openssl/rand.h> /* RAND_get0_public() */
  15. #include <openssl/proverr.h>
  16. #include "internal/cryptlib.h"
  17. #include "prov/implementations.h"
  18. #include "prov/names.h"
  19. #include "prov/provider_ctx.h"
  20. #include "prov/providercommon.h"
  21. #include "prov/provider_util.h"
  22. #include "prov/seeding.h"
  23. #include "prov/fipscommon.h"
  24. #include "internal/nelem.h"
  25. #include "self_test.h"
  26. #include "crypto/context.h"
  27. #include "internal/core.h"
  28. static const char FIPS_DEFAULT_PROPERTIES[] = "provider=fips,fips=yes";
  29. static const char FIPS_UNAPPROVED_PROPERTIES[] = "provider=fips,fips=no";
  30. /*
  31. * Forward declarations to ensure that interface functions are correctly
  32. * defined.
  33. */
  34. static OSSL_FUNC_provider_teardown_fn fips_teardown;
  35. static OSSL_FUNC_provider_gettable_params_fn fips_gettable_params;
  36. static OSSL_FUNC_provider_get_params_fn fips_get_params;
  37. static OSSL_FUNC_provider_query_operation_fn fips_query;
  38. #define ALGC(NAMES, FUNC, CHECK) \
  39. { { NAMES, FIPS_DEFAULT_PROPERTIES, FUNC }, CHECK }
  40. #define UNAPPROVED_ALGC(NAMES, FUNC, CHECK) \
  41. { { NAMES, FIPS_UNAPPROVED_PROPERTIES, FUNC }, CHECK }
  42. #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
  43. #define UNAPPROVED_ALG(NAMES, FUNC) UNAPPROVED_ALGC(NAMES, FUNC, NULL)
  44. extern OSSL_FUNC_core_thread_start_fn *c_thread_start;
  45. /*
  46. * Should these function pointers be stored in the provider side provctx? Could
  47. * they ever be different from one init to the next? We assume not for now.
  48. */
  49. /* Functions provided by the core */
  50. static OSSL_FUNC_core_gettable_params_fn *c_gettable_params;
  51. static OSSL_FUNC_core_get_params_fn *c_get_params;
  52. OSSL_FUNC_core_thread_start_fn *c_thread_start;
  53. static OSSL_FUNC_core_new_error_fn *c_new_error;
  54. static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
  55. static OSSL_FUNC_core_vset_error_fn *c_vset_error;
  56. static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;
  57. static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;
  58. static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;
  59. static OSSL_FUNC_CRYPTO_malloc_fn *c_CRYPTO_malloc;
  60. static OSSL_FUNC_CRYPTO_zalloc_fn *c_CRYPTO_zalloc;
  61. static OSSL_FUNC_CRYPTO_free_fn *c_CRYPTO_free;
  62. static OSSL_FUNC_CRYPTO_clear_free_fn *c_CRYPTO_clear_free;
  63. static OSSL_FUNC_CRYPTO_realloc_fn *c_CRYPTO_realloc;
  64. static OSSL_FUNC_CRYPTO_clear_realloc_fn *c_CRYPTO_clear_realloc;
  65. static OSSL_FUNC_CRYPTO_secure_malloc_fn *c_CRYPTO_secure_malloc;
  66. static OSSL_FUNC_CRYPTO_secure_zalloc_fn *c_CRYPTO_secure_zalloc;
  67. static OSSL_FUNC_CRYPTO_secure_free_fn *c_CRYPTO_secure_free;
  68. static OSSL_FUNC_CRYPTO_secure_clear_free_fn *c_CRYPTO_secure_clear_free;
  69. static OSSL_FUNC_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated;
  70. static OSSL_FUNC_BIO_vsnprintf_fn *c_BIO_vsnprintf;
  71. static OSSL_FUNC_self_test_cb_fn *c_stcbfn = NULL;
  72. static OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
  73. typedef struct {
  74. const char *option;
  75. unsigned char enabled;
  76. } FIPS_OPTION;
  77. typedef struct fips_global_st {
  78. const OSSL_CORE_HANDLE *handle;
  79. SELF_TEST_POST_PARAMS selftest_params;
  80. FIPS_OPTION fips_security_checks;
  81. FIPS_OPTION fips_tls1_prf_ems_check;
  82. FIPS_OPTION fips_restricted_drgb_digests;
  83. } FIPS_GLOBAL;
  84. static void init_fips_option(FIPS_OPTION *opt, int enabled)
  85. {
  86. opt->enabled = enabled;
  87. opt->option = enabled ? "1" : "0";
  88. }
  89. void *ossl_fips_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
  90. {
  91. FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
  92. if (fgbl == NULL)
  93. return NULL;
  94. init_fips_option(&fgbl->fips_security_checks, 1);
  95. init_fips_option(&fgbl->fips_tls1_prf_ems_check, 0); /* Disabled by default */
  96. init_fips_option(&fgbl->fips_restricted_drgb_digests, 0);
  97. return fgbl;
  98. }
  99. void ossl_fips_prov_ossl_ctx_free(void *fgbl)
  100. {
  101. OPENSSL_free(fgbl);
  102. }
  103. /* Parameters we provide to the core */
  104. static const OSSL_PARAM fips_param_types[] = {
  105. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
  106. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  107. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
  108. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
  109. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_SECURITY_CHECKS, OSSL_PARAM_INTEGER, NULL, 0),
  110. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_TLS1_PRF_EMS_CHECK, OSSL_PARAM_INTEGER, NULL, 0),
  111. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_DRBG_TRUNC_DIGEST, OSSL_PARAM_INTEGER, NULL, 0),
  112. OSSL_PARAM_END
  113. };
  114. static int fips_get_params_from_core(FIPS_GLOBAL *fgbl)
  115. {
  116. /*
  117. * Parameters to retrieve from the core provider - required for self testing.
  118. * NOTE: inside core_get_params() these will be loaded from config items
  119. * stored inside prov->parameters (except for
  120. * OSSL_PROV_PARAM_CORE_MODULE_FILENAME).
  121. * OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS and
  122. * OSSL_PROV_FIPS_PARAM_TLS1_PRF_EMS_CHECK are not self test parameters.
  123. */
  124. OSSL_PARAM core_params[10], *p = core_params;
  125. *p++ = OSSL_PARAM_construct_utf8_ptr(
  126. OSSL_PROV_PARAM_CORE_MODULE_FILENAME,
  127. (char **)&fgbl->selftest_params.module_filename,
  128. sizeof(fgbl->selftest_params.module_filename));
  129. *p++ = OSSL_PARAM_construct_utf8_ptr(
  130. OSSL_PROV_FIPS_PARAM_MODULE_MAC,
  131. (char **)&fgbl->selftest_params.module_checksum_data,
  132. sizeof(fgbl->selftest_params.module_checksum_data));
  133. *p++ = OSSL_PARAM_construct_utf8_ptr(
  134. OSSL_PROV_FIPS_PARAM_INSTALL_MAC,
  135. (char **)&fgbl->selftest_params.indicator_checksum_data,
  136. sizeof(fgbl->selftest_params.indicator_checksum_data));
  137. *p++ = OSSL_PARAM_construct_utf8_ptr(
  138. OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
  139. (char **)&fgbl->selftest_params.indicator_data,
  140. sizeof(fgbl->selftest_params.indicator_data));
  141. *p++ = OSSL_PARAM_construct_utf8_ptr(
  142. OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
  143. (char **)&fgbl->selftest_params.indicator_version,
  144. sizeof(fgbl->selftest_params.indicator_version));
  145. *p++ = OSSL_PARAM_construct_utf8_ptr(
  146. OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
  147. (char **)&fgbl->selftest_params.conditional_error_check,
  148. sizeof(fgbl->selftest_params.conditional_error_check));
  149. /* FIPS features can be enabled or disabled independently */
  150. #define FIPS_FEATURE_OPTION(fgbl, pname, field) \
  151. *p++ = OSSL_PARAM_construct_utf8_ptr( \
  152. pname, (char **)&fgbl->field.option, \
  153. sizeof(fgbl->field.option))
  154. FIPS_FEATURE_OPTION(fgbl, OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
  155. fips_security_checks);
  156. FIPS_FEATURE_OPTION(fgbl, OSSL_PROV_FIPS_PARAM_TLS1_PRF_EMS_CHECK,
  157. fips_tls1_prf_ems_check);
  158. FIPS_FEATURE_OPTION(fgbl, OSSL_PROV_FIPS_PARAM_DRBG_TRUNC_DIGEST,
  159. fips_restricted_drgb_digests);
  160. #undef FIPS_FEATURE_OPTION
  161. *p = OSSL_PARAM_construct_end();
  162. if (!c_get_params(fgbl->handle, core_params)) {
  163. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  164. return 0;
  165. }
  166. return 1;
  167. }
  168. static const OSSL_PARAM *fips_gettable_params(void *provctx)
  169. {
  170. return fips_param_types;
  171. }
  172. static int fips_get_params(void *provctx, OSSL_PARAM params[])
  173. {
  174. OSSL_PARAM *p;
  175. FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(ossl_prov_ctx_get0_libctx(provctx),
  176. OSSL_LIB_CTX_FIPS_PROV_INDEX);
  177. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
  178. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
  179. return 0;
  180. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
  181. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
  182. return 0;
  183. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
  184. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
  185. return 0;
  186. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
  187. if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
  188. return 0;
  189. #define FIPS_FEATURE_GET(fgbl, pname, field) \
  190. p = OSSL_PARAM_locate(params, pname); \
  191. if (p != NULL && !OSSL_PARAM_set_int(p, fgbl->field.enabled)) \
  192. return 0
  193. FIPS_FEATURE_GET(fgbl, OSSL_PROV_PARAM_SECURITY_CHECKS,
  194. fips_security_checks);
  195. FIPS_FEATURE_GET(fgbl, OSSL_PROV_PARAM_TLS1_PRF_EMS_CHECK,
  196. fips_tls1_prf_ems_check);
  197. FIPS_FEATURE_GET(fgbl, OSSL_PROV_PARAM_DRBG_TRUNC_DIGEST,
  198. fips_restricted_drgb_digests);
  199. #undef FIPS_FEATURE_GET
  200. return 1;
  201. }
  202. static void set_self_test_cb(FIPS_GLOBAL *fgbl)
  203. {
  204. const OSSL_CORE_HANDLE *handle =
  205. FIPS_get_core_handle(fgbl->selftest_params.libctx);
  206. if (c_stcbfn != NULL && c_get_libctx != NULL) {
  207. c_stcbfn(c_get_libctx(handle), &fgbl->selftest_params.cb,
  208. &fgbl->selftest_params.cb_arg);
  209. } else {
  210. fgbl->selftest_params.cb = NULL;
  211. fgbl->selftest_params.cb_arg = NULL;
  212. }
  213. }
  214. static int fips_self_test(void *provctx)
  215. {
  216. FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(ossl_prov_ctx_get0_libctx(provctx),
  217. OSSL_LIB_CTX_FIPS_PROV_INDEX);
  218. set_self_test_cb(fgbl);
  219. return SELF_TEST_post(&fgbl->selftest_params, 1) ? 1 : 0;
  220. }
  221. /*
  222. * For the algorithm names, we use the following formula for our primary
  223. * names:
  224. *
  225. * ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
  226. *
  227. * VERSION is only present if there are multiple versions of
  228. * an alg (MD2, MD4, MD5). It may be omitted if there is only
  229. * one version (if a subsequent version is released in the future,
  230. * we can always change the canonical name, and add the old name
  231. * as an alias).
  232. *
  233. * SUBNAME may be present where we are combining multiple
  234. * algorithms together, e.g. MD5-SHA1.
  235. *
  236. * SIZE is only present if multiple versions of an algorithm exist
  237. * with different sizes (e.g. AES-128-CBC, AES-256-CBC)
  238. *
  239. * MODE is only present where applicable.
  240. *
  241. * We add diverse other names where applicable, such as the names that
  242. * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
  243. * we have used historically.
  244. */
  245. static const OSSL_ALGORITHM fips_digests[] = {
  246. /* Our primary name:NiST name[:our older names] */
  247. { PROV_NAMES_SHA1, FIPS_DEFAULT_PROPERTIES, ossl_sha1_functions },
  248. { PROV_NAMES_SHA2_224, FIPS_DEFAULT_PROPERTIES, ossl_sha224_functions },
  249. { PROV_NAMES_SHA2_256, FIPS_DEFAULT_PROPERTIES, ossl_sha256_functions },
  250. { PROV_NAMES_SHA2_384, FIPS_DEFAULT_PROPERTIES, ossl_sha384_functions },
  251. { PROV_NAMES_SHA2_512, FIPS_DEFAULT_PROPERTIES, ossl_sha512_functions },
  252. { PROV_NAMES_SHA2_512_224, FIPS_DEFAULT_PROPERTIES,
  253. ossl_sha512_224_functions },
  254. { PROV_NAMES_SHA2_512_256, FIPS_DEFAULT_PROPERTIES,
  255. ossl_sha512_256_functions },
  256. /* We agree with NIST here, so one name only */
  257. { PROV_NAMES_SHA3_224, FIPS_DEFAULT_PROPERTIES, ossl_sha3_224_functions },
  258. { PROV_NAMES_SHA3_256, FIPS_DEFAULT_PROPERTIES, ossl_sha3_256_functions },
  259. { PROV_NAMES_SHA3_384, FIPS_DEFAULT_PROPERTIES, ossl_sha3_384_functions },
  260. { PROV_NAMES_SHA3_512, FIPS_DEFAULT_PROPERTIES, ossl_sha3_512_functions },
  261. { PROV_NAMES_SHAKE_128, FIPS_DEFAULT_PROPERTIES, ossl_shake_128_functions },
  262. { PROV_NAMES_SHAKE_256, FIPS_DEFAULT_PROPERTIES, ossl_shake_256_functions },
  263. /*
  264. * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
  265. * KMAC128 and KMAC256.
  266. */
  267. { PROV_NAMES_KECCAK_KMAC_128, FIPS_DEFAULT_PROPERTIES,
  268. ossl_keccak_kmac_128_functions },
  269. { PROV_NAMES_KECCAK_KMAC_256, FIPS_DEFAULT_PROPERTIES,
  270. ossl_keccak_kmac_256_functions },
  271. { NULL, NULL, NULL }
  272. };
  273. static const OSSL_ALGORITHM_CAPABLE fips_ciphers[] = {
  274. /* Our primary name[:ASN.1 OID name][:our older names] */
  275. ALG(PROV_NAMES_AES_256_ECB, ossl_aes256ecb_functions),
  276. ALG(PROV_NAMES_AES_192_ECB, ossl_aes192ecb_functions),
  277. ALG(PROV_NAMES_AES_128_ECB, ossl_aes128ecb_functions),
  278. ALG(PROV_NAMES_AES_256_CBC, ossl_aes256cbc_functions),
  279. ALG(PROV_NAMES_AES_192_CBC, ossl_aes192cbc_functions),
  280. ALG(PROV_NAMES_AES_128_CBC, ossl_aes128cbc_functions),
  281. ALG(PROV_NAMES_AES_256_CBC_CTS, ossl_aes256cbc_cts_functions),
  282. ALG(PROV_NAMES_AES_192_CBC_CTS, ossl_aes192cbc_cts_functions),
  283. ALG(PROV_NAMES_AES_128_CBC_CTS, ossl_aes128cbc_cts_functions),
  284. ALG(PROV_NAMES_AES_256_OFB, ossl_aes256ofb_functions),
  285. ALG(PROV_NAMES_AES_192_OFB, ossl_aes192ofb_functions),
  286. ALG(PROV_NAMES_AES_128_OFB, ossl_aes128ofb_functions),
  287. ALG(PROV_NAMES_AES_256_CFB, ossl_aes256cfb_functions),
  288. ALG(PROV_NAMES_AES_192_CFB, ossl_aes192cfb_functions),
  289. ALG(PROV_NAMES_AES_128_CFB, ossl_aes128cfb_functions),
  290. ALG(PROV_NAMES_AES_256_CFB1, ossl_aes256cfb1_functions),
  291. ALG(PROV_NAMES_AES_192_CFB1, ossl_aes192cfb1_functions),
  292. ALG(PROV_NAMES_AES_128_CFB1, ossl_aes128cfb1_functions),
  293. ALG(PROV_NAMES_AES_256_CFB8, ossl_aes256cfb8_functions),
  294. ALG(PROV_NAMES_AES_192_CFB8, ossl_aes192cfb8_functions),
  295. ALG(PROV_NAMES_AES_128_CFB8, ossl_aes128cfb8_functions),
  296. ALG(PROV_NAMES_AES_256_CTR, ossl_aes256ctr_functions),
  297. ALG(PROV_NAMES_AES_192_CTR, ossl_aes192ctr_functions),
  298. ALG(PROV_NAMES_AES_128_CTR, ossl_aes128ctr_functions),
  299. ALG(PROV_NAMES_AES_256_XTS, ossl_aes256xts_functions),
  300. ALG(PROV_NAMES_AES_128_XTS, ossl_aes128xts_functions),
  301. ALG(PROV_NAMES_AES_256_GCM, ossl_aes256gcm_functions),
  302. ALG(PROV_NAMES_AES_192_GCM, ossl_aes192gcm_functions),
  303. ALG(PROV_NAMES_AES_128_GCM, ossl_aes128gcm_functions),
  304. ALG(PROV_NAMES_AES_256_CCM, ossl_aes256ccm_functions),
  305. ALG(PROV_NAMES_AES_192_CCM, ossl_aes192ccm_functions),
  306. ALG(PROV_NAMES_AES_128_CCM, ossl_aes128ccm_functions),
  307. ALG(PROV_NAMES_AES_256_WRAP, ossl_aes256wrap_functions),
  308. ALG(PROV_NAMES_AES_192_WRAP, ossl_aes192wrap_functions),
  309. ALG(PROV_NAMES_AES_128_WRAP, ossl_aes128wrap_functions),
  310. ALG(PROV_NAMES_AES_256_WRAP_PAD, ossl_aes256wrappad_functions),
  311. ALG(PROV_NAMES_AES_192_WRAP_PAD, ossl_aes192wrappad_functions),
  312. ALG(PROV_NAMES_AES_128_WRAP_PAD, ossl_aes128wrappad_functions),
  313. ALG(PROV_NAMES_AES_256_WRAP_INV, ossl_aes256wrapinv_functions),
  314. ALG(PROV_NAMES_AES_192_WRAP_INV, ossl_aes192wrapinv_functions),
  315. ALG(PROV_NAMES_AES_128_WRAP_INV, ossl_aes128wrapinv_functions),
  316. ALG(PROV_NAMES_AES_256_WRAP_PAD_INV, ossl_aes256wrappadinv_functions),
  317. ALG(PROV_NAMES_AES_192_WRAP_PAD_INV, ossl_aes192wrappadinv_functions),
  318. ALG(PROV_NAMES_AES_128_WRAP_PAD_INV, ossl_aes128wrappadinv_functions),
  319. ALGC(PROV_NAMES_AES_128_CBC_HMAC_SHA1, ossl_aes128cbc_hmac_sha1_functions,
  320. ossl_cipher_capable_aes_cbc_hmac_sha1),
  321. ALGC(PROV_NAMES_AES_256_CBC_HMAC_SHA1, ossl_aes256cbc_hmac_sha1_functions,
  322. ossl_cipher_capable_aes_cbc_hmac_sha1),
  323. ALGC(PROV_NAMES_AES_128_CBC_HMAC_SHA256, ossl_aes128cbc_hmac_sha256_functions,
  324. ossl_cipher_capable_aes_cbc_hmac_sha256),
  325. ALGC(PROV_NAMES_AES_256_CBC_HMAC_SHA256, ossl_aes256cbc_hmac_sha256_functions,
  326. ossl_cipher_capable_aes_cbc_hmac_sha256),
  327. #ifndef OPENSSL_NO_DES
  328. UNAPPROVED_ALG(PROV_NAMES_DES_EDE3_ECB, ossl_tdes_ede3_ecb_functions),
  329. UNAPPROVED_ALG(PROV_NAMES_DES_EDE3_CBC, ossl_tdes_ede3_cbc_functions),
  330. #endif /* OPENSSL_NO_DES */
  331. { { NULL, NULL, NULL }, NULL }
  332. };
  333. static OSSL_ALGORITHM exported_fips_ciphers[OSSL_NELEM(fips_ciphers)];
  334. static const OSSL_ALGORITHM fips_macs[] = {
  335. #ifndef OPENSSL_NO_CMAC
  336. { PROV_NAMES_CMAC, FIPS_DEFAULT_PROPERTIES, ossl_cmac_functions },
  337. #endif
  338. { PROV_NAMES_GMAC, FIPS_DEFAULT_PROPERTIES, ossl_gmac_functions },
  339. { PROV_NAMES_HMAC, FIPS_DEFAULT_PROPERTIES, ossl_hmac_functions },
  340. { PROV_NAMES_KMAC_128, FIPS_DEFAULT_PROPERTIES, ossl_kmac128_functions },
  341. { PROV_NAMES_KMAC_256, FIPS_DEFAULT_PROPERTIES, ossl_kmac256_functions },
  342. { NULL, NULL, NULL }
  343. };
  344. static const OSSL_ALGORITHM fips_kdfs[] = {
  345. { PROV_NAMES_HKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_functions },
  346. { PROV_NAMES_TLS1_3_KDF, FIPS_DEFAULT_PROPERTIES,
  347. ossl_kdf_tls1_3_kdf_functions },
  348. { PROV_NAMES_SSKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_sskdf_functions },
  349. { PROV_NAMES_PBKDF2, FIPS_DEFAULT_PROPERTIES, ossl_kdf_pbkdf2_functions },
  350. { PROV_NAMES_SSHKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_sshkdf_functions },
  351. { PROV_NAMES_X963KDF, FIPS_DEFAULT_PROPERTIES,
  352. ossl_kdf_x963_kdf_functions },
  353. { PROV_NAMES_X942KDF_ASN1, FIPS_DEFAULT_PROPERTIES,
  354. ossl_kdf_x942_kdf_functions },
  355. { PROV_NAMES_TLS1_PRF, FIPS_DEFAULT_PROPERTIES,
  356. ossl_kdf_tls1_prf_functions },
  357. { PROV_NAMES_KBKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_kbkdf_functions },
  358. { NULL, NULL, NULL }
  359. };
  360. static const OSSL_ALGORITHM fips_rands[] = {
  361. { PROV_NAMES_CTR_DRBG, FIPS_DEFAULT_PROPERTIES, ossl_drbg_ctr_functions },
  362. { PROV_NAMES_HASH_DRBG, FIPS_DEFAULT_PROPERTIES, ossl_drbg_hash_functions },
  363. { PROV_NAMES_HMAC_DRBG, FIPS_DEFAULT_PROPERTIES, ossl_drbg_ossl_hmac_functions },
  364. { PROV_NAMES_TEST_RAND, FIPS_UNAPPROVED_PROPERTIES, ossl_test_rng_functions },
  365. { NULL, NULL, NULL }
  366. };
  367. static const OSSL_ALGORITHM fips_keyexch[] = {
  368. #ifndef OPENSSL_NO_DH
  369. { PROV_NAMES_DH, FIPS_DEFAULT_PROPERTIES, ossl_dh_keyexch_functions },
  370. #endif
  371. #ifndef OPENSSL_NO_EC
  372. { PROV_NAMES_ECDH, FIPS_DEFAULT_PROPERTIES, ossl_ecdh_keyexch_functions },
  373. # ifndef OPENSSL_NO_ECX
  374. { PROV_NAMES_X25519, FIPS_UNAPPROVED_PROPERTIES, ossl_x25519_keyexch_functions },
  375. { PROV_NAMES_X448, FIPS_UNAPPROVED_PROPERTIES, ossl_x448_keyexch_functions },
  376. # endif
  377. #endif
  378. { PROV_NAMES_TLS1_PRF, FIPS_DEFAULT_PROPERTIES,
  379. ossl_kdf_tls1_prf_keyexch_functions },
  380. { PROV_NAMES_HKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_keyexch_functions },
  381. { NULL, NULL, NULL }
  382. };
  383. static const OSSL_ALGORITHM fips_signature[] = {
  384. #ifndef OPENSSL_NO_DSA
  385. { PROV_NAMES_DSA, FIPS_DEFAULT_PROPERTIES, ossl_dsa_signature_functions },
  386. #endif
  387. { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_signature_functions },
  388. #ifndef OPENSSL_NO_EC
  389. # ifndef OPENSSL_NO_ECX
  390. { PROV_NAMES_ED25519, FIPS_UNAPPROVED_PROPERTIES,
  391. ossl_ed25519_signature_functions },
  392. { PROV_NAMES_ED448, FIPS_UNAPPROVED_PROPERTIES, ossl_ed448_signature_functions },
  393. # endif
  394. { PROV_NAMES_ECDSA, FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_signature_functions },
  395. #endif
  396. { PROV_NAMES_HMAC, FIPS_DEFAULT_PROPERTIES,
  397. ossl_mac_legacy_hmac_signature_functions },
  398. #ifndef OPENSSL_NO_CMAC
  399. { PROV_NAMES_CMAC, FIPS_DEFAULT_PROPERTIES,
  400. ossl_mac_legacy_cmac_signature_functions },
  401. #endif
  402. { NULL, NULL, NULL }
  403. };
  404. static const OSSL_ALGORITHM fips_asym_cipher[] = {
  405. { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_asym_cipher_functions },
  406. { NULL, NULL, NULL }
  407. };
  408. static const OSSL_ALGORITHM fips_asym_kem[] = {
  409. { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_asym_kem_functions },
  410. { NULL, NULL, NULL }
  411. };
  412. static const OSSL_ALGORITHM fips_keymgmt[] = {
  413. #ifndef OPENSSL_NO_DH
  414. { PROV_NAMES_DH, FIPS_DEFAULT_PROPERTIES, ossl_dh_keymgmt_functions,
  415. PROV_DESCS_DH },
  416. { PROV_NAMES_DHX, FIPS_DEFAULT_PROPERTIES, ossl_dhx_keymgmt_functions,
  417. PROV_DESCS_DHX },
  418. #endif
  419. #ifndef OPENSSL_NO_DSA
  420. { PROV_NAMES_DSA, FIPS_DEFAULT_PROPERTIES, ossl_dsa_keymgmt_functions,
  421. PROV_DESCS_DSA },
  422. #endif
  423. { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_keymgmt_functions,
  424. PROV_DESCS_RSA },
  425. { PROV_NAMES_RSA_PSS, FIPS_DEFAULT_PROPERTIES,
  426. ossl_rsapss_keymgmt_functions, PROV_DESCS_RSA_PSS },
  427. #ifndef OPENSSL_NO_EC
  428. { PROV_NAMES_EC, FIPS_DEFAULT_PROPERTIES, ossl_ec_keymgmt_functions,
  429. PROV_DESCS_EC },
  430. # ifndef OPENSSL_NO_ECX
  431. { PROV_NAMES_X25519, FIPS_UNAPPROVED_PROPERTIES, ossl_x25519_keymgmt_functions,
  432. PROV_DESCS_X25519 },
  433. { PROV_NAMES_X448, FIPS_UNAPPROVED_PROPERTIES, ossl_x448_keymgmt_functions,
  434. PROV_DESCS_X448 },
  435. { PROV_NAMES_ED25519, FIPS_UNAPPROVED_PROPERTIES, ossl_ed25519_keymgmt_functions,
  436. PROV_DESCS_ED25519 },
  437. { PROV_NAMES_ED448, FIPS_UNAPPROVED_PROPERTIES, ossl_ed448_keymgmt_functions,
  438. PROV_DESCS_ED448 },
  439. # endif
  440. #endif
  441. { PROV_NAMES_TLS1_PRF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
  442. PROV_DESCS_TLS1_PRF_SIGN },
  443. { PROV_NAMES_HKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
  444. PROV_DESCS_HKDF_SIGN },
  445. { PROV_NAMES_HMAC, FIPS_DEFAULT_PROPERTIES, ossl_mac_legacy_keymgmt_functions,
  446. PROV_DESCS_HMAC_SIGN },
  447. #ifndef OPENSSL_NO_CMAC
  448. { PROV_NAMES_CMAC, FIPS_DEFAULT_PROPERTIES,
  449. ossl_cmac_legacy_keymgmt_functions, PROV_DESCS_CMAC_SIGN },
  450. #endif
  451. { NULL, NULL, NULL }
  452. };
  453. static const OSSL_ALGORITHM *fips_query(void *provctx, int operation_id,
  454. int *no_cache)
  455. {
  456. *no_cache = 0;
  457. if (!ossl_prov_is_running())
  458. return NULL;
  459. switch (operation_id) {
  460. case OSSL_OP_DIGEST:
  461. return fips_digests;
  462. case OSSL_OP_CIPHER:
  463. return exported_fips_ciphers;
  464. case OSSL_OP_MAC:
  465. return fips_macs;
  466. case OSSL_OP_KDF:
  467. return fips_kdfs;
  468. case OSSL_OP_RAND:
  469. return fips_rands;
  470. case OSSL_OP_KEYMGMT:
  471. return fips_keymgmt;
  472. case OSSL_OP_KEYEXCH:
  473. return fips_keyexch;
  474. case OSSL_OP_SIGNATURE:
  475. return fips_signature;
  476. case OSSL_OP_ASYM_CIPHER:
  477. return fips_asym_cipher;
  478. case OSSL_OP_KEM:
  479. return fips_asym_kem;
  480. }
  481. return NULL;
  482. }
  483. static void fips_teardown(void *provctx)
  484. {
  485. OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
  486. ossl_prov_ctx_free(provctx);
  487. }
  488. static void fips_intern_teardown(void *provctx)
  489. {
  490. /*
  491. * We know that the library context is the same as for the outer provider,
  492. * so no need to destroy it here.
  493. */
  494. ossl_prov_ctx_free(provctx);
  495. }
  496. /* Functions we provide to the core */
  497. static const OSSL_DISPATCH fips_dispatch_table[] = {
  498. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_teardown },
  499. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params },
  500. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
  501. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
  502. { OSSL_FUNC_PROVIDER_GET_CAPABILITIES,
  503. (void (*)(void))ossl_prov_get_capabilities },
  504. { OSSL_FUNC_PROVIDER_SELF_TEST, (void (*)(void))fips_self_test },
  505. OSSL_DISPATCH_END
  506. };
  507. /* Functions we provide to ourself */
  508. static const OSSL_DISPATCH intern_dispatch_table[] = {
  509. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_intern_teardown },
  510. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
  511. OSSL_DISPATCH_END
  512. };
  513. /*
  514. * On VMS, the provider init function name is expected to be uppercase,
  515. * see the pragmas in <openssl/core.h>. Let's do the same with this
  516. * internal name. This is how symbol names are treated by default
  517. * by the compiler if nothing else is said, but since this is part
  518. * of libfips, and we build our libraries with mixed case symbol names,
  519. * we must switch back to this default explicitly here.
  520. */
  521. #ifdef __VMS
  522. # pragma names save
  523. # pragma names uppercase,truncated
  524. #endif
  525. OSSL_provider_init_fn OSSL_provider_init_int;
  526. #ifdef __VMS
  527. # pragma names restore
  528. #endif
  529. int OSSL_provider_init_int(const OSSL_CORE_HANDLE *handle,
  530. const OSSL_DISPATCH *in,
  531. const OSSL_DISPATCH **out,
  532. void **provctx)
  533. {
  534. FIPS_GLOBAL *fgbl;
  535. OSSL_LIB_CTX *libctx = NULL;
  536. SELF_TEST_POST_PARAMS selftest_params;
  537. memset(&selftest_params, 0, sizeof(selftest_params));
  538. if (!ossl_prov_seeding_from_dispatch(in))
  539. goto err;
  540. for (; in->function_id != 0; in++) {
  541. /*
  542. * We do not support the scenario of an application linked against
  543. * multiple versions of libcrypto (e.g. one static and one dynamic), but
  544. * sharing a single fips.so. We do a simple sanity check here.
  545. */
  546. #define set_func(c, f) if (c == NULL) c = f; else if (c != f) return 0;
  547. switch (in->function_id) {
  548. case OSSL_FUNC_CORE_GET_LIBCTX:
  549. set_func(c_get_libctx, OSSL_FUNC_core_get_libctx(in));
  550. break;
  551. case OSSL_FUNC_CORE_GETTABLE_PARAMS:
  552. set_func(c_gettable_params, OSSL_FUNC_core_gettable_params(in));
  553. break;
  554. case OSSL_FUNC_CORE_GET_PARAMS:
  555. set_func(c_get_params, OSSL_FUNC_core_get_params(in));
  556. break;
  557. case OSSL_FUNC_CORE_THREAD_START:
  558. set_func(c_thread_start, OSSL_FUNC_core_thread_start(in));
  559. break;
  560. case OSSL_FUNC_CORE_NEW_ERROR:
  561. set_func(c_new_error, OSSL_FUNC_core_new_error(in));
  562. break;
  563. case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
  564. set_func(c_set_error_debug, OSSL_FUNC_core_set_error_debug(in));
  565. break;
  566. case OSSL_FUNC_CORE_VSET_ERROR:
  567. set_func(c_vset_error, OSSL_FUNC_core_vset_error(in));
  568. break;
  569. case OSSL_FUNC_CORE_SET_ERROR_MARK:
  570. set_func(c_set_error_mark, OSSL_FUNC_core_set_error_mark(in));
  571. break;
  572. case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
  573. set_func(c_clear_last_error_mark,
  574. OSSL_FUNC_core_clear_last_error_mark(in));
  575. break;
  576. case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
  577. set_func(c_pop_error_to_mark, OSSL_FUNC_core_pop_error_to_mark(in));
  578. break;
  579. case OSSL_FUNC_CRYPTO_MALLOC:
  580. set_func(c_CRYPTO_malloc, OSSL_FUNC_CRYPTO_malloc(in));
  581. break;
  582. case OSSL_FUNC_CRYPTO_ZALLOC:
  583. set_func(c_CRYPTO_zalloc, OSSL_FUNC_CRYPTO_zalloc(in));
  584. break;
  585. case OSSL_FUNC_CRYPTO_FREE:
  586. set_func(c_CRYPTO_free, OSSL_FUNC_CRYPTO_free(in));
  587. break;
  588. case OSSL_FUNC_CRYPTO_CLEAR_FREE:
  589. set_func(c_CRYPTO_clear_free, OSSL_FUNC_CRYPTO_clear_free(in));
  590. break;
  591. case OSSL_FUNC_CRYPTO_REALLOC:
  592. set_func(c_CRYPTO_realloc, OSSL_FUNC_CRYPTO_realloc(in));
  593. break;
  594. case OSSL_FUNC_CRYPTO_CLEAR_REALLOC:
  595. set_func(c_CRYPTO_clear_realloc,
  596. OSSL_FUNC_CRYPTO_clear_realloc(in));
  597. break;
  598. case OSSL_FUNC_CRYPTO_SECURE_MALLOC:
  599. set_func(c_CRYPTO_secure_malloc,
  600. OSSL_FUNC_CRYPTO_secure_malloc(in));
  601. break;
  602. case OSSL_FUNC_CRYPTO_SECURE_ZALLOC:
  603. set_func(c_CRYPTO_secure_zalloc,
  604. OSSL_FUNC_CRYPTO_secure_zalloc(in));
  605. break;
  606. case OSSL_FUNC_CRYPTO_SECURE_FREE:
  607. set_func(c_CRYPTO_secure_free,
  608. OSSL_FUNC_CRYPTO_secure_free(in));
  609. break;
  610. case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE:
  611. set_func(c_CRYPTO_secure_clear_free,
  612. OSSL_FUNC_CRYPTO_secure_clear_free(in));
  613. break;
  614. case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
  615. set_func(c_CRYPTO_secure_allocated,
  616. OSSL_FUNC_CRYPTO_secure_allocated(in));
  617. break;
  618. case OSSL_FUNC_BIO_NEW_FILE:
  619. set_func(selftest_params.bio_new_file_cb,
  620. OSSL_FUNC_BIO_new_file(in));
  621. break;
  622. case OSSL_FUNC_BIO_NEW_MEMBUF:
  623. set_func(selftest_params.bio_new_buffer_cb,
  624. OSSL_FUNC_BIO_new_membuf(in));
  625. break;
  626. case OSSL_FUNC_BIO_READ_EX:
  627. set_func(selftest_params.bio_read_ex_cb,
  628. OSSL_FUNC_BIO_read_ex(in));
  629. break;
  630. case OSSL_FUNC_BIO_FREE:
  631. set_func(selftest_params.bio_free_cb, OSSL_FUNC_BIO_free(in));
  632. break;
  633. case OSSL_FUNC_BIO_VSNPRINTF:
  634. set_func(c_BIO_vsnprintf, OSSL_FUNC_BIO_vsnprintf(in));
  635. break;
  636. case OSSL_FUNC_SELF_TEST_CB:
  637. set_func(c_stcbfn, OSSL_FUNC_self_test_cb(in));
  638. break;
  639. default:
  640. /* Just ignore anything we don't understand */
  641. break;
  642. }
  643. }
  644. OPENSSL_cpuid_setup();
  645. /* Create a context. */
  646. if ((*provctx = ossl_prov_ctx_new()) == NULL
  647. || (libctx = OSSL_LIB_CTX_new()) == NULL)
  648. goto err;
  649. if ((fgbl = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_FIPS_PROV_INDEX)) == NULL)
  650. goto err;
  651. fgbl->handle = handle;
  652. /*
  653. * We need to register this thread to receive thread lifecycle callbacks.
  654. * This wouldn't matter if the current thread is also the same thread that
  655. * closes the FIPS provider down. But if that happens on a different thread
  656. * then memory leaks could otherwise occur.
  657. */
  658. if (!ossl_thread_register_fips(libctx))
  659. goto err;
  660. /*
  661. * We did initial set up of selftest_params in a local copy, because we
  662. * could not create fgbl until c_CRYPTO_zalloc was defined in the loop
  663. * above.
  664. */
  665. fgbl->selftest_params = selftest_params;
  666. fgbl->selftest_params.libctx = libctx;
  667. set_self_test_cb(fgbl);
  668. if (!fips_get_params_from_core(fgbl)) {
  669. /* Error already raised */
  670. goto err;
  671. }
  672. /*
  673. * Disable the conditional error check if it's disabled in the fips config
  674. * file.
  675. */
  676. if (fgbl->selftest_params.conditional_error_check != NULL
  677. && strcmp(fgbl->selftest_params.conditional_error_check, "0") == 0)
  678. SELF_TEST_disable_conditional_error_state();
  679. /* Enable or disable FIPS provider options */
  680. #define FIPS_SET_OPTION(fgbl, field) \
  681. if (fgbl->field.option != NULL) { \
  682. if (strcmp(fgbl->field.option, "1") == 0) \
  683. fgbl->field.enabled = 1; \
  684. else if (strcmp(fgbl->field.option, "0") == 0) \
  685. fgbl->field.enabled = 0; \
  686. else \
  687. goto err; \
  688. }
  689. FIPS_SET_OPTION(fgbl, fips_security_checks);
  690. FIPS_SET_OPTION(fgbl, fips_tls1_prf_ems_check);
  691. FIPS_SET_OPTION(fgbl, fips_restricted_drgb_digests);
  692. #undef FIPS_SET_OPTION
  693. ossl_prov_cache_exported_algorithms(fips_ciphers, exported_fips_ciphers);
  694. if (!SELF_TEST_post(&fgbl->selftest_params, 0)) {
  695. ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_POST_FAILURE);
  696. goto err;
  697. }
  698. ossl_prov_ctx_set0_libctx(*provctx, libctx);
  699. ossl_prov_ctx_set0_handle(*provctx, handle);
  700. *out = fips_dispatch_table;
  701. return 1;
  702. err:
  703. fips_teardown(*provctx);
  704. OSSL_LIB_CTX_free(libctx);
  705. *provctx = NULL;
  706. return 0;
  707. }
  708. /*
  709. * The internal init function used when the FIPS module uses EVP to call
  710. * another algorithm also in the FIPS module. This is a recursive call that has
  711. * been made from within the FIPS module itself. To make this work, we populate
  712. * the provider context of this inner instance with the same library context
  713. * that was used in the EVP call that initiated this recursive call.
  714. */
  715. OSSL_provider_init_fn ossl_fips_intern_provider_init;
  716. int ossl_fips_intern_provider_init(const OSSL_CORE_HANDLE *handle,
  717. const OSSL_DISPATCH *in,
  718. const OSSL_DISPATCH **out,
  719. void **provctx)
  720. {
  721. OSSL_FUNC_core_get_libctx_fn *c_internal_get_libctx = NULL;
  722. for (; in->function_id != 0; in++) {
  723. switch (in->function_id) {
  724. case OSSL_FUNC_CORE_GET_LIBCTX:
  725. c_internal_get_libctx = OSSL_FUNC_core_get_libctx(in);
  726. break;
  727. default:
  728. break;
  729. }
  730. }
  731. if (c_internal_get_libctx == NULL)
  732. return 0;
  733. if ((*provctx = ossl_prov_ctx_new()) == NULL)
  734. return 0;
  735. /*
  736. * Using the parent library context only works because we are a built-in
  737. * internal provider. This is not something that most providers would be
  738. * able to do.
  739. */
  740. ossl_prov_ctx_set0_libctx(*provctx,
  741. (OSSL_LIB_CTX *)c_internal_get_libctx(handle));
  742. ossl_prov_ctx_set0_handle(*provctx, handle);
  743. *out = intern_dispatch_table;
  744. return 1;
  745. }
  746. void ERR_new(void)
  747. {
  748. c_new_error(NULL);
  749. }
  750. void ERR_set_debug(const char *file, int line, const char *func)
  751. {
  752. c_set_error_debug(NULL, file, line, func);
  753. }
  754. void ERR_set_error(int lib, int reason, const char *fmt, ...)
  755. {
  756. va_list args;
  757. va_start(args, fmt);
  758. c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
  759. va_end(args);
  760. }
  761. void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
  762. {
  763. c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
  764. }
  765. int ERR_set_mark(void)
  766. {
  767. return c_set_error_mark(NULL);
  768. }
  769. int ERR_clear_last_mark(void)
  770. {
  771. return c_clear_last_error_mark(NULL);
  772. }
  773. int ERR_pop_to_mark(void)
  774. {
  775. return c_pop_error_to_mark(NULL);
  776. }
  777. /*
  778. * This must take a library context, since it's called from the depths
  779. * of crypto/initthread.c code, where it's (correctly) assumed that the
  780. * passed caller argument is an OSSL_LIB_CTX pointer (since the same routine
  781. * is also called from other parts of libcrypto, which all pass around a
  782. * OSSL_LIB_CTX pointer)
  783. */
  784. const OSSL_CORE_HANDLE *FIPS_get_core_handle(OSSL_LIB_CTX *libctx)
  785. {
  786. FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(libctx,
  787. OSSL_LIB_CTX_FIPS_PROV_INDEX);
  788. if (fgbl == NULL)
  789. return NULL;
  790. return fgbl->handle;
  791. }
  792. void *CRYPTO_malloc(size_t num, const char *file, int line)
  793. {
  794. return c_CRYPTO_malloc(num, file, line);
  795. }
  796. void *CRYPTO_zalloc(size_t num, const char *file, int line)
  797. {
  798. return c_CRYPTO_zalloc(num, file, line);
  799. }
  800. void CRYPTO_free(void *ptr, const char *file, int line)
  801. {
  802. c_CRYPTO_free(ptr, file, line);
  803. }
  804. void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line)
  805. {
  806. c_CRYPTO_clear_free(ptr, num, file, line);
  807. }
  808. void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
  809. {
  810. return c_CRYPTO_realloc(addr, num, file, line);
  811. }
  812. void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
  813. const char *file, int line)
  814. {
  815. return c_CRYPTO_clear_realloc(addr, old_num, num, file, line);
  816. }
  817. void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
  818. {
  819. return c_CRYPTO_secure_malloc(num, file, line);
  820. }
  821. void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
  822. {
  823. return c_CRYPTO_secure_zalloc(num, file, line);
  824. }
  825. void CRYPTO_secure_free(void *ptr, const char *file, int line)
  826. {
  827. c_CRYPTO_secure_free(ptr, file, line);
  828. }
  829. void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *file, int line)
  830. {
  831. c_CRYPTO_secure_clear_free(ptr, num, file, line);
  832. }
  833. int CRYPTO_secure_allocated(const void *ptr)
  834. {
  835. return c_CRYPTO_secure_allocated(ptr);
  836. }
  837. int BIO_snprintf(char *buf, size_t n, const char *format, ...)
  838. {
  839. va_list args;
  840. int ret;
  841. va_start(args, format);
  842. ret = c_BIO_vsnprintf(buf, n, format, args);
  843. va_end(args);
  844. return ret;
  845. }
  846. #define FIPS_FEATURE_CHECK(fname, field) \
  847. int fname(OSSL_LIB_CTX *libctx) \
  848. { \
  849. FIPS_GLOBAL *fgbl = \
  850. ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_FIPS_PROV_INDEX); \
  851. return fgbl->field.enabled; \
  852. }
  853. FIPS_FEATURE_CHECK(FIPS_security_check_enabled, fips_security_checks)
  854. FIPS_FEATURE_CHECK(FIPS_tls_prf_ems_check, fips_tls1_prf_ems_check)
  855. FIPS_FEATURE_CHECK(FIPS_restricted_drbg_digests_enabled,
  856. fips_restricted_drgb_digests)
  857. #undef FIPS_FEATURE_CHECK
  858. void OSSL_SELF_TEST_get_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK **cb,
  859. void **cbarg)
  860. {
  861. assert(libctx != NULL);
  862. if (c_stcbfn != NULL && c_get_libctx != NULL) {
  863. /* Get the parent libctx */
  864. c_stcbfn(c_get_libctx(FIPS_get_core_handle(libctx)), cb, cbarg);
  865. } else {
  866. if (cb != NULL)
  867. *cb = NULL;
  868. if (cbarg != NULL)
  869. *cbarg = NULL;
  870. }
  871. }