context.c 18 KB

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