context.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 "crypto/cryptlib.h"
  10. #include <openssl/conf.h>
  11. #include "internal/thread_once.h"
  12. #include "internal/property.h"
  13. #include "internal/core.h"
  14. #include "internal/bio.h"
  15. #include "internal/provider.h"
  16. #include "crypto/decoder.h"
  17. #include "crypto/context.h"
  18. struct ossl_lib_ctx_st {
  19. CRYPTO_RWLOCK *lock, *rand_crngt_lock;
  20. OSSL_EX_DATA_GLOBAL global;
  21. void *property_string_data;
  22. void *evp_method_store;
  23. void *provider_store;
  24. void *namemap;
  25. void *property_defns;
  26. void *global_properties;
  27. void *drbg;
  28. void *drbg_nonce;
  29. #ifndef FIPS_MODULE
  30. void *provider_conf;
  31. void *bio_core;
  32. void *child_provider;
  33. OSSL_METHOD_STORE *decoder_store;
  34. void *decoder_cache;
  35. OSSL_METHOD_STORE *encoder_store;
  36. OSSL_METHOD_STORE *store_loader_store;
  37. void *self_test_cb;
  38. #endif
  39. #if defined(OPENSSL_THREADS)
  40. void *threads;
  41. #endif
  42. void *rand_crngt;
  43. #ifdef FIPS_MODULE
  44. void *thread_event_handler;
  45. void *fips_prov;
  46. #endif
  47. unsigned int ischild:1;
  48. };
  49. int ossl_lib_ctx_write_lock(OSSL_LIB_CTX *ctx)
  50. {
  51. return CRYPTO_THREAD_write_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
  52. }
  53. int ossl_lib_ctx_read_lock(OSSL_LIB_CTX *ctx)
  54. {
  55. return CRYPTO_THREAD_read_lock(ossl_lib_ctx_get_concrete(ctx)->lock);
  56. }
  57. int ossl_lib_ctx_unlock(OSSL_LIB_CTX *ctx)
  58. {
  59. return CRYPTO_THREAD_unlock(ossl_lib_ctx_get_concrete(ctx)->lock);
  60. }
  61. int ossl_lib_ctx_is_child(OSSL_LIB_CTX *ctx)
  62. {
  63. ctx = ossl_lib_ctx_get_concrete(ctx);
  64. if (ctx == NULL)
  65. return 0;
  66. return ctx->ischild;
  67. }
  68. static void context_deinit_objs(OSSL_LIB_CTX *ctx);
  69. static int context_init(OSSL_LIB_CTX *ctx)
  70. {
  71. int exdata_done = 0;
  72. ctx->lock = CRYPTO_THREAD_lock_new();
  73. if (ctx->lock == NULL)
  74. return 0;
  75. ctx->rand_crngt_lock = CRYPTO_THREAD_lock_new();
  76. if (ctx->rand_crngt_lock == NULL)
  77. goto err;
  78. /* Initialize ex_data. */
  79. if (!ossl_do_ex_data_init(ctx))
  80. goto err;
  81. exdata_done = 1;
  82. /* P2. We want evp_method_store to be cleaned up before the provider store */
  83. ctx->evp_method_store = ossl_method_store_new(ctx);
  84. if (ctx->evp_method_store == NULL)
  85. goto err;
  86. #ifndef FIPS_MODULE
  87. /* P2. Must be freed before the provider store is freed */
  88. ctx->provider_conf = ossl_prov_conf_ctx_new(ctx);
  89. if (ctx->provider_conf == NULL)
  90. goto err;
  91. #endif
  92. /* P2. */
  93. ctx->drbg = ossl_rand_ctx_new(ctx);
  94. if (ctx->drbg == NULL)
  95. goto err;
  96. #ifndef FIPS_MODULE
  97. /*
  98. * P2. We want decoder_store/decoder_cache to be cleaned up before the
  99. * provider store
  100. */
  101. ctx->decoder_store = ossl_method_store_new(ctx);
  102. if (ctx->decoder_store == NULL)
  103. goto err;
  104. ctx->decoder_cache = ossl_decoder_cache_new(ctx);
  105. if (ctx->decoder_cache == NULL)
  106. goto err;
  107. /* P2. We want encoder_store to be cleaned up before the provider store */
  108. ctx->encoder_store = ossl_method_store_new(ctx);
  109. if (ctx->encoder_store == NULL)
  110. goto err;
  111. /* P2. We want loader_store to be cleaned up before the provider store */
  112. ctx->store_loader_store = ossl_method_store_new(ctx);
  113. if (ctx->store_loader_store == NULL)
  114. goto err;
  115. #endif
  116. /* P1. Needs to be freed before the child provider data is freed */
  117. ctx->provider_store = ossl_provider_store_new(ctx);
  118. if (ctx->provider_store == NULL)
  119. goto err;
  120. /* Default priority. */
  121. ctx->property_string_data = ossl_property_string_data_new(ctx);
  122. if (ctx->property_string_data == NULL)
  123. goto err;
  124. ctx->namemap = ossl_stored_namemap_new(ctx);
  125. if (ctx->namemap == NULL)
  126. goto err;
  127. ctx->property_defns = ossl_property_defns_new(ctx);
  128. if (ctx->property_defns == NULL)
  129. goto err;
  130. ctx->global_properties = ossl_ctx_global_properties_new(ctx);
  131. if (ctx->global_properties == NULL)
  132. goto err;
  133. #ifndef FIPS_MODULE
  134. ctx->bio_core = ossl_bio_core_globals_new(ctx);
  135. if (ctx->bio_core == NULL)
  136. goto err;
  137. #endif
  138. ctx->drbg_nonce = ossl_prov_drbg_nonce_ctx_new(ctx);
  139. if (ctx->drbg_nonce == NULL)
  140. goto err;
  141. #ifndef FIPS_MODULE
  142. ctx->self_test_cb = ossl_self_test_set_callback_new(ctx);
  143. if (ctx->self_test_cb == NULL)
  144. goto err;
  145. #endif
  146. #ifdef FIPS_MODULE
  147. ctx->thread_event_handler = ossl_thread_event_ctx_new(ctx);
  148. if (ctx->thread_event_handler == NULL)
  149. goto err;
  150. ctx->fips_prov = ossl_fips_prov_ossl_ctx_new(ctx);
  151. if (ctx->fips_prov == NULL)
  152. goto err;
  153. #endif
  154. #ifndef OPENSSL_NO_THREAD_POOL
  155. ctx->threads = ossl_threads_ctx_new(ctx);
  156. if (ctx->threads == NULL)
  157. goto err;
  158. #endif
  159. /* Low priority. */
  160. #ifndef FIPS_MODULE
  161. ctx->child_provider = ossl_child_prov_ctx_new(ctx);
  162. if (ctx->child_provider == NULL)
  163. goto err;
  164. #endif
  165. /* Everything depends on properties, so we also pre-initialise that */
  166. if (!ossl_property_parse_init(ctx))
  167. goto err;
  168. return 1;
  169. err:
  170. context_deinit_objs(ctx);
  171. if (exdata_done)
  172. ossl_crypto_cleanup_all_ex_data_int(ctx);
  173. CRYPTO_THREAD_lock_free(ctx->rand_crngt_lock);
  174. CRYPTO_THREAD_lock_free(ctx->lock);
  175. memset(ctx, '\0', sizeof(*ctx));
  176. return 0;
  177. }
  178. static void context_deinit_objs(OSSL_LIB_CTX *ctx)
  179. {
  180. /* P2. We want evp_method_store to be cleaned up before the provider store */
  181. if (ctx->evp_method_store != NULL) {
  182. ossl_method_store_free(ctx->evp_method_store);
  183. ctx->evp_method_store = NULL;
  184. }
  185. /* P2. */
  186. if (ctx->drbg != NULL) {
  187. ossl_rand_ctx_free(ctx->drbg);
  188. ctx->drbg = NULL;
  189. }
  190. #ifndef FIPS_MODULE
  191. /* P2. */
  192. if (ctx->provider_conf != NULL) {
  193. ossl_prov_conf_ctx_free(ctx->provider_conf);
  194. ctx->provider_conf = NULL;
  195. }
  196. /*
  197. * P2. We want decoder_store/decoder_cache to be cleaned up before the
  198. * provider store
  199. */
  200. if (ctx->decoder_store != NULL) {
  201. ossl_method_store_free(ctx->decoder_store);
  202. ctx->decoder_store = NULL;
  203. }
  204. if (ctx->decoder_cache != NULL) {
  205. ossl_decoder_cache_free(ctx->decoder_cache);
  206. ctx->decoder_cache = NULL;
  207. }
  208. /* P2. We want encoder_store to be cleaned up before the provider store */
  209. if (ctx->encoder_store != NULL) {
  210. ossl_method_store_free(ctx->encoder_store);
  211. ctx->encoder_store = NULL;
  212. }
  213. /* P2. We want loader_store to be cleaned up before the provider store */
  214. if (ctx->store_loader_store != NULL) {
  215. ossl_method_store_free(ctx->store_loader_store);
  216. ctx->store_loader_store = NULL;
  217. }
  218. #endif
  219. /* P1. Needs to be freed before the child provider data is freed */
  220. if (ctx->provider_store != NULL) {
  221. ossl_provider_store_free(ctx->provider_store);
  222. ctx->provider_store = NULL;
  223. }
  224. /* Default priority. */
  225. if (ctx->property_string_data != NULL) {
  226. ossl_property_string_data_free(ctx->property_string_data);
  227. ctx->property_string_data = NULL;
  228. }
  229. if (ctx->namemap != NULL) {
  230. ossl_stored_namemap_free(ctx->namemap);
  231. ctx->namemap = NULL;
  232. }
  233. if (ctx->property_defns != NULL) {
  234. ossl_property_defns_free(ctx->property_defns);
  235. ctx->property_defns = NULL;
  236. }
  237. if (ctx->global_properties != NULL) {
  238. ossl_ctx_global_properties_free(ctx->global_properties);
  239. ctx->global_properties = NULL;
  240. }
  241. #ifndef FIPS_MODULE
  242. if (ctx->bio_core != NULL) {
  243. ossl_bio_core_globals_free(ctx->bio_core);
  244. ctx->bio_core = NULL;
  245. }
  246. #endif
  247. if (ctx->drbg_nonce != NULL) {
  248. ossl_prov_drbg_nonce_ctx_free(ctx->drbg_nonce);
  249. ctx->drbg_nonce = NULL;
  250. }
  251. #ifndef FIPS_MODULE
  252. if (ctx->self_test_cb != NULL) {
  253. ossl_self_test_set_callback_free(ctx->self_test_cb);
  254. ctx->self_test_cb = NULL;
  255. }
  256. #endif
  257. if (ctx->rand_crngt != NULL) {
  258. ossl_rand_crng_ctx_free(ctx->rand_crngt);
  259. ctx->rand_crngt = NULL;
  260. }
  261. #ifdef FIPS_MODULE
  262. if (ctx->thread_event_handler != NULL) {
  263. ossl_thread_event_ctx_free(ctx->thread_event_handler);
  264. ctx->thread_event_handler = NULL;
  265. }
  266. if (ctx->fips_prov != NULL) {
  267. ossl_fips_prov_ossl_ctx_free(ctx->fips_prov);
  268. ctx->fips_prov = NULL;
  269. }
  270. #endif
  271. #ifndef OPENSSL_NO_THREAD_POOL
  272. if (ctx->threads != NULL) {
  273. ossl_threads_ctx_free(ctx->threads);
  274. ctx->threads = NULL;
  275. }
  276. #endif
  277. /* Low priority. */
  278. #ifndef FIPS_MODULE
  279. if (ctx->child_provider != NULL) {
  280. ossl_child_prov_ctx_free(ctx->child_provider);
  281. ctx->child_provider = NULL;
  282. }
  283. #endif
  284. }
  285. static int context_deinit(OSSL_LIB_CTX *ctx)
  286. {
  287. if (ctx == NULL)
  288. return 1;
  289. ossl_ctx_thread_stop(ctx);
  290. context_deinit_objs(ctx);
  291. ossl_crypto_cleanup_all_ex_data_int(ctx);
  292. CRYPTO_THREAD_lock_free(ctx->rand_crngt_lock);
  293. CRYPTO_THREAD_lock_free(ctx->lock);
  294. ctx->rand_crngt_lock = NULL;
  295. ctx->lock = NULL;
  296. return 1;
  297. }
  298. #ifndef FIPS_MODULE
  299. /* The default default context */
  300. static OSSL_LIB_CTX default_context_int;
  301. static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
  302. static CRYPTO_THREAD_LOCAL default_context_thread_local;
  303. static int default_context_inited = 0;
  304. DEFINE_RUN_ONCE_STATIC(default_context_do_init)
  305. {
  306. if (!CRYPTO_THREAD_init_local(&default_context_thread_local, NULL))
  307. goto err;
  308. if (!context_init(&default_context_int))
  309. goto deinit_thread;
  310. default_context_inited = 1;
  311. return 1;
  312. deinit_thread:
  313. CRYPTO_THREAD_cleanup_local(&default_context_thread_local);
  314. err:
  315. return 0;
  316. }
  317. void ossl_lib_ctx_default_deinit(void)
  318. {
  319. if (!default_context_inited)
  320. return;
  321. context_deinit(&default_context_int);
  322. CRYPTO_THREAD_cleanup_local(&default_context_thread_local);
  323. default_context_inited = 0;
  324. }
  325. static OSSL_LIB_CTX *get_thread_default_context(void)
  326. {
  327. if (!RUN_ONCE(&default_context_init, default_context_do_init))
  328. return NULL;
  329. return CRYPTO_THREAD_get_local(&default_context_thread_local);
  330. }
  331. static OSSL_LIB_CTX *get_default_context(void)
  332. {
  333. OSSL_LIB_CTX *current_defctx = get_thread_default_context();
  334. if (current_defctx == NULL)
  335. current_defctx = &default_context_int;
  336. return current_defctx;
  337. }
  338. static int set_default_context(OSSL_LIB_CTX *defctx)
  339. {
  340. if (defctx == &default_context_int)
  341. defctx = NULL;
  342. return CRYPTO_THREAD_set_local(&default_context_thread_local, defctx);
  343. }
  344. #endif
  345. OSSL_LIB_CTX *OSSL_LIB_CTX_new(void)
  346. {
  347. OSSL_LIB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  348. if (ctx != NULL && !context_init(ctx)) {
  349. OPENSSL_free(ctx);
  350. ctx = NULL;
  351. }
  352. return ctx;
  353. }
  354. #ifndef FIPS_MODULE
  355. OSSL_LIB_CTX *OSSL_LIB_CTX_new_from_dispatch(const OSSL_CORE_HANDLE *handle,
  356. const OSSL_DISPATCH *in)
  357. {
  358. OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
  359. if (ctx == NULL)
  360. return NULL;
  361. if (!ossl_bio_init_core(ctx, in)) {
  362. OSSL_LIB_CTX_free(ctx);
  363. return NULL;
  364. }
  365. return ctx;
  366. }
  367. OSSL_LIB_CTX *OSSL_LIB_CTX_new_child(const OSSL_CORE_HANDLE *handle,
  368. const OSSL_DISPATCH *in)
  369. {
  370. OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new_from_dispatch(handle, in);
  371. if (ctx == NULL)
  372. return NULL;
  373. if (!ossl_provider_init_as_child(ctx, handle, in)) {
  374. OSSL_LIB_CTX_free(ctx);
  375. return NULL;
  376. }
  377. ctx->ischild = 1;
  378. return ctx;
  379. }
  380. int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file)
  381. {
  382. return CONF_modules_load_file_ex(ctx, config_file, NULL, 0) > 0;
  383. }
  384. #endif
  385. void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx)
  386. {
  387. if (ossl_lib_ctx_is_default(ctx))
  388. return;
  389. #ifndef FIPS_MODULE
  390. if (ctx->ischild)
  391. ossl_provider_deinit_child(ctx);
  392. #endif
  393. context_deinit(ctx);
  394. OPENSSL_free(ctx);
  395. }
  396. #ifndef FIPS_MODULE
  397. OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void)
  398. {
  399. if (!RUN_ONCE(&default_context_init, default_context_do_init))
  400. return NULL;
  401. return &default_context_int;
  402. }
  403. OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx)
  404. {
  405. OSSL_LIB_CTX *current_defctx;
  406. if ((current_defctx = get_default_context()) != NULL) {
  407. if (libctx != NULL)
  408. set_default_context(libctx);
  409. return current_defctx;
  410. }
  411. return NULL;
  412. }
  413. void ossl_release_default_drbg_ctx(void)
  414. {
  415. /* early release of the DRBG in global default libctx */
  416. if (default_context_int.drbg != NULL) {
  417. ossl_rand_ctx_free(default_context_int.drbg);
  418. default_context_int.drbg = NULL;
  419. }
  420. }
  421. #endif
  422. OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx)
  423. {
  424. #ifndef FIPS_MODULE
  425. if (ctx == NULL)
  426. return get_default_context();
  427. #endif
  428. return ctx;
  429. }
  430. int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx)
  431. {
  432. #ifndef FIPS_MODULE
  433. if (ctx == NULL || ctx == get_default_context())
  434. return 1;
  435. #endif
  436. return 0;
  437. }
  438. int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx)
  439. {
  440. #ifndef FIPS_MODULE
  441. if (ossl_lib_ctx_get_concrete(ctx) == &default_context_int)
  442. return 1;
  443. #endif
  444. return 0;
  445. }
  446. void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index)
  447. {
  448. void *p;
  449. ctx = ossl_lib_ctx_get_concrete(ctx);
  450. if (ctx == NULL)
  451. return NULL;
  452. switch (index) {
  453. case OSSL_LIB_CTX_PROPERTY_STRING_INDEX:
  454. return ctx->property_string_data;
  455. case OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX:
  456. return ctx->evp_method_store;
  457. case OSSL_LIB_CTX_PROVIDER_STORE_INDEX:
  458. return ctx->provider_store;
  459. case OSSL_LIB_CTX_NAMEMAP_INDEX:
  460. return ctx->namemap;
  461. case OSSL_LIB_CTX_PROPERTY_DEFN_INDEX:
  462. return ctx->property_defns;
  463. case OSSL_LIB_CTX_GLOBAL_PROPERTIES:
  464. return ctx->global_properties;
  465. case OSSL_LIB_CTX_DRBG_INDEX:
  466. return ctx->drbg;
  467. case OSSL_LIB_CTX_DRBG_NONCE_INDEX:
  468. return ctx->drbg_nonce;
  469. #ifndef FIPS_MODULE
  470. case OSSL_LIB_CTX_PROVIDER_CONF_INDEX:
  471. return ctx->provider_conf;
  472. case OSSL_LIB_CTX_BIO_CORE_INDEX:
  473. return ctx->bio_core;
  474. case OSSL_LIB_CTX_CHILD_PROVIDER_INDEX:
  475. return ctx->child_provider;
  476. case OSSL_LIB_CTX_DECODER_STORE_INDEX:
  477. return ctx->decoder_store;
  478. case OSSL_LIB_CTX_DECODER_CACHE_INDEX:
  479. return ctx->decoder_cache;
  480. case OSSL_LIB_CTX_ENCODER_STORE_INDEX:
  481. return ctx->encoder_store;
  482. case OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX:
  483. return ctx->store_loader_store;
  484. case OSSL_LIB_CTX_SELF_TEST_CB_INDEX:
  485. return ctx->self_test_cb;
  486. #endif
  487. #ifndef OPENSSL_NO_THREAD_POOL
  488. case OSSL_LIB_CTX_THREAD_INDEX:
  489. return ctx->threads;
  490. #endif
  491. case OSSL_LIB_CTX_RAND_CRNGT_INDEX: {
  492. /*
  493. * rand_crngt must be lazily initialized because it calls into
  494. * libctx, so must not be called from context_init, else a deadlock
  495. * will occur.
  496. *
  497. * We use a separate lock because code called by the instantiation
  498. * of rand_crngt is liable to try and take the libctx lock.
  499. */
  500. if (CRYPTO_THREAD_read_lock(ctx->rand_crngt_lock) != 1)
  501. return NULL;
  502. if (ctx->rand_crngt == NULL) {
  503. CRYPTO_THREAD_unlock(ctx->rand_crngt_lock);
  504. if (CRYPTO_THREAD_write_lock(ctx->rand_crngt_lock) != 1)
  505. return NULL;
  506. if (ctx->rand_crngt == NULL)
  507. ctx->rand_crngt = ossl_rand_crng_ctx_new(ctx);
  508. }
  509. p = ctx->rand_crngt;
  510. CRYPTO_THREAD_unlock(ctx->rand_crngt_lock);
  511. return p;
  512. }
  513. #ifdef FIPS_MODULE
  514. case OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX:
  515. return ctx->thread_event_handler;
  516. case OSSL_LIB_CTX_FIPS_PROV_INDEX:
  517. return ctx->fips_prov;
  518. #endif
  519. default:
  520. return NULL;
  521. }
  522. }
  523. OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx)
  524. {
  525. ctx = ossl_lib_ctx_get_concrete(ctx);
  526. if (ctx == NULL)
  527. return NULL;
  528. return &ctx->global;
  529. }
  530. const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx)
  531. {
  532. #ifdef FIPS_MODULE
  533. return "FIPS internal library context";
  534. #else
  535. if (ossl_lib_ctx_is_global_default(libctx))
  536. return "Global default library context";
  537. if (ossl_lib_ctx_is_default(libctx))
  538. return "Thread-local default library context";
  539. return "Non-default library context";
  540. #endif
  541. }