init.c 20 KB

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