init.c 21 KB

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