init.c 21 KB

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