2
0

init.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. * Copyright 2016-2024 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. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include "internal/e_os.h"
  12. #include "crypto/cryptlib.h"
  13. #include <openssl/err.h>
  14. #include "crypto/rand.h"
  15. #include "internal/bio.h"
  16. #include <openssl/evp.h>
  17. #include "crypto/evp.h"
  18. #include "internal/conf.h"
  19. #include "crypto/async.h"
  20. #include "crypto/engine.h"
  21. #include "internal/comp.h"
  22. #include "internal/err.h"
  23. #include "crypto/err.h"
  24. #include "crypto/objects.h"
  25. #include <stdlib.h>
  26. #include <assert.h>
  27. #include "internal/thread_once.h"
  28. #include "crypto/dso_conf.h"
  29. #include "internal/dso.h"
  30. #include "crypto/store.h"
  31. #include <openssl/cmp_util.h> /* for OSSL_CMP_log_close() */
  32. #include <openssl/trace.h>
  33. #include "crypto/ctype.h"
  34. static int stopped = 0;
  35. static uint64_t optsdone = 0;
  36. typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
  37. struct ossl_init_stop_st {
  38. void (*handler)(void);
  39. OPENSSL_INIT_STOP *next;
  40. };
  41. static OPENSSL_INIT_STOP *stop_handlers = NULL;
  42. /* Guards access to the optsdone variable on platforms without atomics */
  43. static CRYPTO_RWLOCK *optsdone_lock = NULL;
  44. /* Guards simultaneous INIT_LOAD_CONFIG calls with non-NULL settings */
  45. static CRYPTO_RWLOCK *init_lock = NULL;
  46. static CRYPTO_THREAD_LOCAL in_init_config_local;
  47. static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
  48. static int base_inited = 0;
  49. DEFINE_RUN_ONCE_STATIC(ossl_init_base)
  50. {
  51. /* no need to init trace */
  52. OSSL_TRACE(INIT, "ossl_init_base: setting up stop handlers\n");
  53. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  54. ossl_malloc_setup_failures();
  55. #endif
  56. if ((optsdone_lock = CRYPTO_THREAD_lock_new()) == NULL
  57. || (init_lock = CRYPTO_THREAD_lock_new()) == NULL)
  58. goto err;
  59. OPENSSL_cpuid_setup();
  60. if (!ossl_init_thread())
  61. goto err;
  62. if (!CRYPTO_THREAD_init_local(&in_init_config_local, NULL))
  63. goto err;
  64. base_inited = 1;
  65. return 1;
  66. err:
  67. OSSL_TRACE(INIT, "ossl_init_base failed!\n");
  68. CRYPTO_THREAD_lock_free(optsdone_lock);
  69. optsdone_lock = NULL;
  70. CRYPTO_THREAD_lock_free(init_lock);
  71. init_lock = NULL;
  72. return 0;
  73. }
  74. static CRYPTO_ONCE register_atexit = CRYPTO_ONCE_STATIC_INIT;
  75. #if !defined(OPENSSL_SYS_UEFI) && defined(_WIN32)
  76. static int win32atexit(void)
  77. {
  78. OPENSSL_cleanup();
  79. return 0;
  80. }
  81. #endif
  82. DEFINE_RUN_ONCE_STATIC(ossl_init_register_atexit)
  83. {
  84. #ifndef OPENSSL_NO_ATEXIT
  85. # ifdef OPENSSL_INIT_DEBUG
  86. fprintf(stderr, "OPENSSL_INIT: ossl_init_register_atexit()\n");
  87. # endif
  88. # ifndef OPENSSL_SYS_UEFI
  89. # if defined(_WIN32) && !defined(__BORLANDC__)
  90. /* We use _onexit() in preference because it gets called on DLL unload */
  91. if (_onexit(win32atexit) == NULL)
  92. return 0;
  93. # else
  94. if (atexit(OPENSSL_cleanup) != 0)
  95. return 0;
  96. # endif
  97. # endif
  98. #endif
  99. return 1;
  100. }
  101. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_register_atexit,
  102. ossl_init_register_atexit)
  103. {
  104. #ifdef OPENSSL_INIT_DEBUG
  105. fprintf(stderr, "OPENSSL_INIT: ossl_init_no_register_atexit ok!\n");
  106. #endif
  107. /* Do nothing in this case */
  108. return 1;
  109. }
  110. static CRYPTO_ONCE load_crypto_nodelete = CRYPTO_ONCE_STATIC_INIT;
  111. DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_nodelete)
  112. {
  113. OSSL_TRACE(INIT, "ossl_init_load_crypto_nodelete()\n");
  114. #if !defined(OPENSSL_USE_NODELETE) \
  115. && !defined(OPENSSL_NO_PINSHARED)
  116. # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
  117. {
  118. HMODULE handle = NULL;
  119. BOOL ret;
  120. /* We don't use the DSO route for WIN32 because there is a better way */
  121. ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
  122. | GET_MODULE_HANDLE_EX_FLAG_PIN,
  123. (void *)&base_inited, &handle);
  124. OSSL_TRACE1(INIT,
  125. "ossl_init_load_crypto_nodelete: "
  126. "obtained DSO reference? %s\n",
  127. (ret == TRUE ? "No!" : "Yes."));
  128. return (ret == TRUE) ? 1 : 0;
  129. }
  130. # elif !defined(DSO_NONE)
  131. /*
  132. * Deliberately leak a reference to ourselves. This will force the library
  133. * to remain loaded until the atexit() handler is run at process exit.
  134. */
  135. {
  136. DSO *dso;
  137. void *err;
  138. if (!err_shelve_state(&err))
  139. return 0;
  140. dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
  141. /*
  142. * In case of No!, it is uncertain our exit()-handlers can still be
  143. * called. After dlclose() the whole library might have been unloaded
  144. * already.
  145. */
  146. OSSL_TRACE1(INIT, "obtained DSO reference? %s\n",
  147. (dso == NULL ? "No!" : "Yes."));
  148. DSO_free(dso);
  149. err_unshelve_state(err);
  150. }
  151. # endif
  152. #endif
  153. return 1;
  154. }
  155. static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
  156. DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
  157. {
  158. int ret = 1;
  159. /*
  160. * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
  161. * pulling in all the error strings during static linking
  162. */
  163. #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
  164. void *err;
  165. if (!err_shelve_state(&err))
  166. return 0;
  167. OSSL_TRACE(INIT, "ossl_err_load_crypto_strings()\n");
  168. ret = ossl_err_load_crypto_strings();
  169. err_unshelve_state(err);
  170. #endif
  171. return ret;
  172. }
  173. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_crypto_strings,
  174. ossl_init_load_crypto_strings)
  175. {
  176. /* Do nothing in this case */
  177. return 1;
  178. }
  179. static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
  180. DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
  181. {
  182. /*
  183. * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
  184. * pulling in all the ciphers during static linking
  185. */
  186. #ifndef OPENSSL_NO_AUTOALGINIT
  187. OSSL_TRACE(INIT, "openssl_add_all_ciphers_int()\n");
  188. openssl_add_all_ciphers_int();
  189. #endif
  190. return 1;
  191. }
  192. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_ciphers,
  193. ossl_init_add_all_ciphers)
  194. {
  195. /* Do nothing */
  196. return 1;
  197. }
  198. static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
  199. DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
  200. {
  201. /*
  202. * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
  203. * pulling in all the ciphers during static linking
  204. */
  205. #ifndef OPENSSL_NO_AUTOALGINIT
  206. OSSL_TRACE(INIT, "openssl_add_all_digests()\n");
  207. openssl_add_all_digests_int();
  208. #endif
  209. return 1;
  210. }
  211. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_digests,
  212. ossl_init_add_all_digests)
  213. {
  214. /* Do nothing */
  215. return 1;
  216. }
  217. static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
  218. static int config_inited = 0;
  219. static const OPENSSL_INIT_SETTINGS *conf_settings = NULL;
  220. DEFINE_RUN_ONCE_STATIC(ossl_init_config)
  221. {
  222. int ret = ossl_config_int(NULL);
  223. config_inited = 1;
  224. return ret;
  225. }
  226. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_config_settings, ossl_init_config)
  227. {
  228. int ret = ossl_config_int(conf_settings);
  229. config_inited = 1;
  230. return ret;
  231. }
  232. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_config, ossl_init_config)
  233. {
  234. OSSL_TRACE(INIT, "ossl_no_config_int()\n");
  235. ossl_no_config_int();
  236. config_inited = 1;
  237. return 1;
  238. }
  239. static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
  240. static int async_inited = 0;
  241. DEFINE_RUN_ONCE_STATIC(ossl_init_async)
  242. {
  243. OSSL_TRACE(INIT, "async_init()\n");
  244. if (!async_init())
  245. return 0;
  246. async_inited = 1;
  247. return 1;
  248. }
  249. #ifndef OPENSSL_NO_ENGINE
  250. static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
  251. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
  252. {
  253. OSSL_TRACE(INIT, "engine_load_openssl_int()\n");
  254. engine_load_openssl_int();
  255. return 1;
  256. }
  257. # ifndef OPENSSL_NO_RDRAND
  258. static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
  259. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
  260. {
  261. OSSL_TRACE(INIT, "engine_load_rdrand_int()\n");
  262. engine_load_rdrand_int();
  263. return 1;
  264. }
  265. # endif
  266. static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
  267. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
  268. {
  269. OSSL_TRACE(INIT, "engine_load_dynamic_int()\n");
  270. engine_load_dynamic_int();
  271. return 1;
  272. }
  273. # ifndef OPENSSL_NO_STATIC_ENGINE
  274. # ifndef OPENSSL_NO_DEVCRYPTOENG
  275. static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
  276. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
  277. {
  278. OSSL_TRACE(INIT, "engine_load_devcrypto_int()\n");
  279. engine_load_devcrypto_int();
  280. return 1;
  281. }
  282. # endif
  283. # if !defined(OPENSSL_NO_PADLOCKENG)
  284. static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
  285. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
  286. {
  287. OSSL_TRACE(INIT, "engine_load_padlock_int()\n");
  288. engine_load_padlock_int();
  289. return 1;
  290. }
  291. # endif
  292. # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
  293. static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
  294. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
  295. {
  296. OSSL_TRACE(INIT, "engine_load_capi_int()\n");
  297. engine_load_capi_int();
  298. return 1;
  299. }
  300. # endif
  301. # if !defined(OPENSSL_NO_AFALGENG)
  302. static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
  303. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
  304. {
  305. OSSL_TRACE(INIT, "engine_load_afalg_int()\n");
  306. engine_load_afalg_int();
  307. return 1;
  308. }
  309. # endif
  310. # endif
  311. #endif
  312. void OPENSSL_cleanup(void)
  313. {
  314. OPENSSL_INIT_STOP *currhandler, *lasthandler;
  315. /*
  316. * At some point we should consider looking at this function with a view to
  317. * moving most/all of this into onfree handlers in OSSL_LIB_CTX.
  318. */
  319. /* If we've not been inited then no need to deinit */
  320. if (!base_inited)
  321. return;
  322. /* Might be explicitly called and also by atexit */
  323. if (stopped)
  324. return;
  325. stopped = 1;
  326. /*
  327. * Thread stop may not get automatically called by the thread library for
  328. * the very last thread in some situations, so call it directly.
  329. */
  330. OPENSSL_thread_stop();
  331. currhandler = stop_handlers;
  332. while (currhandler != NULL) {
  333. currhandler->handler();
  334. lasthandler = currhandler;
  335. currhandler = currhandler->next;
  336. OPENSSL_free(lasthandler);
  337. }
  338. stop_handlers = NULL;
  339. CRYPTO_THREAD_lock_free(optsdone_lock);
  340. optsdone_lock = NULL;
  341. CRYPTO_THREAD_lock_free(init_lock);
  342. init_lock = NULL;
  343. CRYPTO_THREAD_cleanup_local(&in_init_config_local);
  344. /*
  345. * We assume we are single-threaded for this function, i.e. no race
  346. * conditions for the various "*_inited" vars below.
  347. */
  348. #ifndef OPENSSL_NO_COMP
  349. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_comp_zlib_cleanup()\n");
  350. ossl_comp_zlib_cleanup();
  351. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_comp_brotli_cleanup()\n");
  352. ossl_comp_brotli_cleanup();
  353. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_comp_zstd_cleanup()\n");
  354. ossl_comp_zstd_cleanup();
  355. #endif
  356. if (async_inited) {
  357. OSSL_TRACE(INIT, "OPENSSL_cleanup: async_deinit()\n");
  358. async_deinit();
  359. }
  360. /*
  361. * Note that cleanup order is important:
  362. * - ossl_rand_cleanup_int could call an ENGINE's RAND cleanup function so
  363. * must be called before engine_cleanup_int()
  364. * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
  365. * before the ex data handlers are wiped during default ossl_lib_ctx deinit.
  366. * - ossl_config_modules_free() can end up in ENGINE code so must be called
  367. * before engine_cleanup_int()
  368. * - ENGINEs and additional EVP algorithms might use added OIDs names so
  369. * ossl_obj_cleanup_int() must be called last
  370. */
  371. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_rand_cleanup_int()\n");
  372. ossl_rand_cleanup_int();
  373. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_config_modules_free()\n");
  374. ossl_config_modules_free();
  375. #ifndef OPENSSL_NO_ENGINE
  376. OSSL_TRACE(INIT, "OPENSSL_cleanup: engine_cleanup_int()\n");
  377. engine_cleanup_int();
  378. #endif
  379. #ifndef OPENSSL_NO_DEPRECATED_3_0
  380. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_store_cleanup_int()\n");
  381. ossl_store_cleanup_int();
  382. #endif
  383. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_lib_ctx_default_deinit()\n");
  384. ossl_lib_ctx_default_deinit();
  385. ossl_cleanup_thread();
  386. OSSL_TRACE(INIT, "OPENSSL_cleanup: bio_cleanup()\n");
  387. bio_cleanup();
  388. OSSL_TRACE(INIT, "OPENSSL_cleanup: evp_cleanup_int()\n");
  389. evp_cleanup_int();
  390. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_obj_cleanup_int()\n");
  391. ossl_obj_cleanup_int();
  392. OSSL_TRACE(INIT, "OPENSSL_cleanup: err_int()\n");
  393. err_cleanup();
  394. OSSL_TRACE(INIT, "OPENSSL_cleanup: CRYPTO_secure_malloc_done()\n");
  395. CRYPTO_secure_malloc_done();
  396. #ifndef OPENSSL_NO_CMP
  397. OSSL_TRACE(INIT, "OPENSSL_cleanup: OSSL_CMP_log_close()\n");
  398. OSSL_CMP_log_close();
  399. #endif
  400. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_trace_cleanup()\n");
  401. ossl_trace_cleanup();
  402. base_inited = 0;
  403. }
  404. /*
  405. * If this function is called with a non NULL settings value then it must be
  406. * called prior to any threads making calls to any OpenSSL functions,
  407. * i.e. passing a non-null settings value is assumed to be single-threaded.
  408. */
  409. int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
  410. {
  411. uint64_t tmp;
  412. int aloaddone = 0;
  413. /* Applications depend on 0 being returned when cleanup was already done */
  414. if (stopped) {
  415. if (!(opts & OPENSSL_INIT_BASE_ONLY))
  416. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL);
  417. return 0;
  418. }
  419. /*
  420. * We ignore failures from this function. It is probably because we are
  421. * on a platform that doesn't support lockless atomic loads (we may not
  422. * have created optsdone_lock yet so we can't use it). This is just an
  423. * optimisation to skip the full checks in this function if we don't need
  424. * to, so we carry on regardless in the event of failure.
  425. *
  426. * There could be a race here with other threads, so that optsdone has not
  427. * been updated yet, even though the options have in fact been initialised.
  428. * This doesn't matter - it just means we will run the full function
  429. * unnecessarily - but all the critical code is contained in RUN_ONCE
  430. * functions anyway so we are safe.
  431. */
  432. if (CRYPTO_atomic_load(&optsdone, &tmp, NULL)) {
  433. if ((tmp & opts) == opts)
  434. return 1;
  435. aloaddone = 1;
  436. }
  437. /*
  438. * At some point we should look at this function with a view to moving
  439. * most/all of this into OSSL_LIB_CTX.
  440. *
  441. * When the caller specifies OPENSSL_INIT_BASE_ONLY, that should be the
  442. * *only* option specified. With that option we return immediately after
  443. * doing the requested limited initialization. Note that
  444. * err_shelve_state() called by us via ossl_init_load_crypto_nodelete()
  445. * re-enters OPENSSL_init_crypto() with OPENSSL_INIT_BASE_ONLY, but with
  446. * base already initialized this is a harmless NOOP.
  447. *
  448. * If we remain the only caller of err_shelve_state() the recursion should
  449. * perhaps be removed, but if in doubt, it can be left in place.
  450. */
  451. if (!RUN_ONCE(&base, ossl_init_base))
  452. return 0;
  453. if (opts & OPENSSL_INIT_BASE_ONLY)
  454. return 1;
  455. /*
  456. * optsdone_lock should definitely be set up now, so we can now repeat the
  457. * same check from above but be sure that it will work even on platforms
  458. * without lockless CRYPTO_atomic_load
  459. */
  460. if (!aloaddone) {
  461. if (!CRYPTO_atomic_load(&optsdone, &tmp, optsdone_lock))
  462. return 0;
  463. if ((tmp & opts) == opts)
  464. return 1;
  465. }
  466. /*
  467. * Now we don't always set up exit handlers, the INIT_BASE_ONLY calls
  468. * should not have the side-effect of setting up exit handlers, and
  469. * therefore, this code block is below the INIT_BASE_ONLY-conditioned early
  470. * return above.
  471. */
  472. if ((opts & OPENSSL_INIT_NO_ATEXIT) != 0) {
  473. if (!RUN_ONCE_ALT(&register_atexit, ossl_init_no_register_atexit,
  474. ossl_init_register_atexit))
  475. return 0;
  476. } else if (!RUN_ONCE(&register_atexit, ossl_init_register_atexit)) {
  477. return 0;
  478. }
  479. if (!RUN_ONCE(&load_crypto_nodelete, ossl_init_load_crypto_nodelete))
  480. return 0;
  481. if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
  482. && !RUN_ONCE_ALT(&load_crypto_strings,
  483. ossl_init_no_load_crypto_strings,
  484. ossl_init_load_crypto_strings))
  485. return 0;
  486. if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
  487. && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
  488. return 0;
  489. if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
  490. && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers,
  491. ossl_init_add_all_ciphers))
  492. return 0;
  493. if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
  494. && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
  495. return 0;
  496. if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
  497. && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests,
  498. ossl_init_add_all_digests))
  499. return 0;
  500. if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
  501. && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
  502. return 0;
  503. if ((opts & OPENSSL_INIT_ATFORK)
  504. && !openssl_init_fork_handlers())
  505. return 0;
  506. if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
  507. && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config))
  508. return 0;
  509. if (opts & OPENSSL_INIT_LOAD_CONFIG) {
  510. int loading = CRYPTO_THREAD_get_local(&in_init_config_local) != NULL;
  511. /* If called recursively from OBJ_ calls, just skip it. */
  512. if (!loading) {
  513. int ret;
  514. if (!CRYPTO_THREAD_set_local(&in_init_config_local, (void *)-1))
  515. return 0;
  516. if (settings == NULL) {
  517. ret = RUN_ONCE(&config, ossl_init_config);
  518. } else {
  519. if (!CRYPTO_THREAD_write_lock(init_lock))
  520. return 0;
  521. conf_settings = settings;
  522. ret = RUN_ONCE_ALT(&config, ossl_init_config_settings,
  523. ossl_init_config);
  524. conf_settings = NULL;
  525. CRYPTO_THREAD_unlock(init_lock);
  526. }
  527. if (ret <= 0)
  528. return 0;
  529. }
  530. }
  531. if ((opts & OPENSSL_INIT_ASYNC)
  532. && !RUN_ONCE(&async, ossl_init_async))
  533. return 0;
  534. #ifndef OPENSSL_NO_ENGINE
  535. if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
  536. && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
  537. return 0;
  538. # ifndef OPENSSL_NO_RDRAND
  539. if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
  540. && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
  541. return 0;
  542. # endif
  543. if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
  544. && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
  545. return 0;
  546. # ifndef OPENSSL_NO_STATIC_ENGINE
  547. # ifndef OPENSSL_NO_DEVCRYPTOENG
  548. if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
  549. && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
  550. return 0;
  551. # endif
  552. # if !defined(OPENSSL_NO_PADLOCKENG)
  553. if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
  554. && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
  555. return 0;
  556. # endif
  557. # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
  558. if ((opts & OPENSSL_INIT_ENGINE_CAPI)
  559. && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
  560. return 0;
  561. # endif
  562. # if !defined(OPENSSL_NO_AFALGENG)
  563. if ((opts & OPENSSL_INIT_ENGINE_AFALG)
  564. && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
  565. return 0;
  566. # endif
  567. # endif
  568. if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
  569. | OPENSSL_INIT_ENGINE_OPENSSL
  570. | OPENSSL_INIT_ENGINE_AFALG)) {
  571. ENGINE_register_all_complete();
  572. }
  573. #endif
  574. if (!CRYPTO_atomic_or(&optsdone, opts, &tmp, optsdone_lock))
  575. return 0;
  576. return 1;
  577. }
  578. int OPENSSL_atexit(void (*handler)(void))
  579. {
  580. OPENSSL_INIT_STOP *newhand;
  581. #if !defined(OPENSSL_USE_NODELETE)\
  582. && !defined(OPENSSL_NO_PINSHARED)
  583. {
  584. # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
  585. HMODULE handle = NULL;
  586. BOOL ret;
  587. union {
  588. void *sym;
  589. void (*func)(void);
  590. } handlersym;
  591. handlersym.func = handler;
  592. /*
  593. * We don't use the DSO route for WIN32 because there is a better
  594. * way
  595. */
  596. ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
  597. | GET_MODULE_HANDLE_EX_FLAG_PIN,
  598. handlersym.sym, &handle);
  599. if (!ret)
  600. return 0;
  601. # elif !defined(DSO_NONE)
  602. /*
  603. * Deliberately leak a reference to the handler. This will force the
  604. * library/code containing the handler to remain loaded until we run the
  605. * atexit handler. If -znodelete has been used then this is
  606. * unnecessary.
  607. */
  608. DSO *dso = NULL;
  609. union {
  610. void *sym;
  611. void (*func)(void);
  612. } handlersym;
  613. handlersym.func = handler;
  614. ERR_set_mark();
  615. dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
  616. /* See same code above in ossl_init_base() for an explanation. */
  617. OSSL_TRACE1(INIT,
  618. "atexit: obtained DSO reference? %s\n",
  619. (dso == NULL ? "No!" : "Yes."));
  620. DSO_free(dso);
  621. ERR_pop_to_mark();
  622. # endif
  623. }
  624. #endif
  625. if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL)
  626. return 0;
  627. newhand->handler = handler;
  628. newhand->next = stop_handlers;
  629. stop_handlers = newhand;
  630. return 1;
  631. }