init.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. # ifdef _WIN32
  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, "err_load_crypto_strings_int()\n");
  154. ret = err_load_crypto_strings_int();
  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. #ifndef OPENSSL_NO_COMP
  299. static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
  300. static int zlib_inited = 0;
  301. DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
  302. {
  303. /* Do nothing - we need to know about this for the later cleanup */
  304. zlib_inited = 1;
  305. return 1;
  306. }
  307. #endif
  308. void OPENSSL_cleanup(void)
  309. {
  310. OPENSSL_INIT_STOP *currhandler, *lasthandler;
  311. /*
  312. * At some point we should consider looking at this function with a view to
  313. * moving most/all of this into onfree handlers in OSSL_LIB_CTX.
  314. */
  315. /* If we've not been inited then no need to deinit */
  316. if (!base_inited)
  317. return;
  318. /* Might be explicitly called and also by atexit */
  319. if (stopped)
  320. return;
  321. stopped = 1;
  322. /*
  323. * Thread stop may not get automatically called by the thread library for
  324. * the very last thread in some situations, so call it directly.
  325. */
  326. OPENSSL_thread_stop();
  327. currhandler = stop_handlers;
  328. while (currhandler != NULL) {
  329. currhandler->handler();
  330. lasthandler = currhandler;
  331. currhandler = currhandler->next;
  332. OPENSSL_free(lasthandler);
  333. }
  334. stop_handlers = NULL;
  335. CRYPTO_THREAD_lock_free(init_lock);
  336. init_lock = NULL;
  337. /*
  338. * We assume we are single-threaded for this function, i.e. no race
  339. * conditions for the various "*_inited" vars below.
  340. */
  341. #ifndef OPENSSL_NO_COMP
  342. if (zlib_inited) {
  343. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_comp_zlib_cleanup()\n");
  344. ossl_comp_zlib_cleanup();
  345. }
  346. #endif
  347. if (async_inited) {
  348. OSSL_TRACE(INIT, "OPENSSL_cleanup: async_deinit()\n");
  349. async_deinit();
  350. }
  351. if (load_crypto_strings_inited) {
  352. OSSL_TRACE(INIT, "OPENSSL_cleanup: err_free_strings_int()\n");
  353. err_free_strings_int();
  354. }
  355. /*
  356. * Note that cleanup order is important:
  357. * - ossl_rand_cleanup_int could call an ENGINE's RAND cleanup function so
  358. * must be called before engine_cleanup_int()
  359. * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
  360. * before the ex data handlers are wiped during default ossl_lib_ctx deinit.
  361. * - ossl_config_modules_free() can end up in ENGINE code so must be called
  362. * before engine_cleanup_int()
  363. * - ENGINEs and additional EVP algorithms might use added OIDs names so
  364. * ossl_obj_cleanup_int() must be called last
  365. */
  366. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_rand_cleanup_int()\n");
  367. ossl_rand_cleanup_int();
  368. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_config_modules_free()\n");
  369. ossl_config_modules_free();
  370. #ifndef OPENSSL_NO_ENGINE
  371. OSSL_TRACE(INIT, "OPENSSL_cleanup: engine_cleanup_int()\n");
  372. engine_cleanup_int();
  373. #endif
  374. #ifndef OPENSSL_NO_DEPRECATED_3_0
  375. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_store_cleanup_int()\n");
  376. ossl_store_cleanup_int();
  377. #endif
  378. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_lib_ctx_default_deinit()\n");
  379. ossl_lib_ctx_default_deinit();
  380. ossl_cleanup_thread();
  381. OSSL_TRACE(INIT, "OPENSSL_cleanup: bio_cleanup()\n");
  382. bio_cleanup();
  383. OSSL_TRACE(INIT, "OPENSSL_cleanup: evp_cleanup_int()\n");
  384. evp_cleanup_int();
  385. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_obj_cleanup_int()\n");
  386. ossl_obj_cleanup_int();
  387. OSSL_TRACE(INIT, "OPENSSL_cleanup: err_int()\n");
  388. err_cleanup();
  389. OSSL_TRACE(INIT, "OPENSSL_cleanup: CRYPTO_secure_malloc_done()\n");
  390. CRYPTO_secure_malloc_done();
  391. #ifndef OPENSSL_NO_CMP
  392. OSSL_TRACE(INIT, "OPENSSL_cleanup: OSSL_CMP_log_close()\n");
  393. OSSL_CMP_log_close();
  394. #endif
  395. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_trace_cleanup()\n");
  396. ossl_trace_cleanup();
  397. base_inited = 0;
  398. }
  399. /*
  400. * If this function is called with a non NULL settings value then it must be
  401. * called prior to any threads making calls to any OpenSSL functions,
  402. * i.e. passing a non-null settings value is assumed to be single-threaded.
  403. */
  404. int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
  405. {
  406. uint64_t tmp;
  407. int aloaddone = 0;
  408. /*
  409. * We ignore failures from this function. It is probably because we are
  410. * on a platform that doesn't support lockless atomic loads (we may not
  411. * have created init_lock yet so we can't use it). This is just an
  412. * optimisation to skip the full checks in this function if we don't need
  413. * to, so we carry on regardless in the event of failure.
  414. *
  415. * There could be a race here with other threads, so that optsdone has not
  416. * been updated yet, even though the options have in fact been initialised.
  417. * This doesn't matter - it just means we will run the full function
  418. * unnecessarily - but all the critical code is contained in RUN_ONCE
  419. * functions anyway so we are safe.
  420. */
  421. if (CRYPTO_atomic_load(&optsdone, &tmp, NULL)) {
  422. if ((tmp & opts) == opts)
  423. return 1;
  424. aloaddone = 1;
  425. }
  426. /*
  427. * At some point we should look at this function with a view to moving
  428. * most/all of this into OSSL_LIB_CTX.
  429. */
  430. if (stopped) {
  431. if (!(opts & OPENSSL_INIT_BASE_ONLY))
  432. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL);
  433. return 0;
  434. }
  435. /*
  436. * When the caller specifies OPENSSL_INIT_BASE_ONLY, that should be the
  437. * *only* option specified. With that option we return immediately after
  438. * doing the requested limited initialization. Note that
  439. * err_shelve_state() called by us via ossl_init_load_crypto_nodelete()
  440. * re-enters OPENSSL_init_crypto() with OPENSSL_INIT_BASE_ONLY, but with
  441. * base already initialized this is a harmless NOOP.
  442. *
  443. * If we remain the only caller of err_shelve_state() the recursion should
  444. * perhaps be removed, but if in doubt, it can be left in place.
  445. */
  446. if (!RUN_ONCE(&base, ossl_init_base))
  447. return 0;
  448. if (opts & OPENSSL_INIT_BASE_ONLY)
  449. return 1;
  450. /*
  451. * init_lock should definitely be set up now, so we can now repeat the
  452. * same check from above but be sure that it will work even on platforms
  453. * without lockless CRYPTO_atomic_load
  454. */
  455. if (!aloaddone) {
  456. if (!CRYPTO_atomic_load(&optsdone, &tmp, init_lock))
  457. return 0;
  458. if ((tmp & opts) == opts)
  459. return 1;
  460. }
  461. /*
  462. * Now we don't always set up exit handlers, the INIT_BASE_ONLY calls
  463. * should not have the side-effect of setting up exit handlers, and
  464. * therefore, this code block is below the INIT_BASE_ONLY-conditioned early
  465. * return above.
  466. */
  467. if ((opts & OPENSSL_INIT_NO_ATEXIT) != 0) {
  468. if (!RUN_ONCE_ALT(&register_atexit, ossl_init_no_register_atexit,
  469. ossl_init_register_atexit))
  470. return 0;
  471. } else if (!RUN_ONCE(&register_atexit, ossl_init_register_atexit)) {
  472. return 0;
  473. }
  474. if (!RUN_ONCE(&load_crypto_nodelete, ossl_init_load_crypto_nodelete))
  475. return 0;
  476. if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
  477. && !RUN_ONCE_ALT(&load_crypto_strings,
  478. ossl_init_no_load_crypto_strings,
  479. ossl_init_load_crypto_strings))
  480. return 0;
  481. if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
  482. && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
  483. return 0;
  484. if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
  485. && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers,
  486. ossl_init_add_all_ciphers))
  487. return 0;
  488. if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
  489. && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
  490. return 0;
  491. if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
  492. && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests,
  493. ossl_init_add_all_digests))
  494. return 0;
  495. if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
  496. && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
  497. return 0;
  498. if ((opts & OPENSSL_INIT_ATFORK)
  499. && !openssl_init_fork_handlers())
  500. return 0;
  501. if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
  502. && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config))
  503. return 0;
  504. if (opts & OPENSSL_INIT_LOAD_CONFIG) {
  505. int ret;
  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. if ((opts & OPENSSL_INIT_ASYNC)
  521. && !RUN_ONCE(&async, ossl_init_async))
  522. return 0;
  523. #ifndef OPENSSL_NO_ENGINE
  524. if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
  525. && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
  526. return 0;
  527. # ifndef OPENSSL_NO_RDRAND
  528. if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
  529. && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
  530. return 0;
  531. # endif
  532. if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
  533. && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
  534. return 0;
  535. # ifndef OPENSSL_NO_STATIC_ENGINE
  536. # ifndef OPENSSL_NO_DEVCRYPTOENG
  537. if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
  538. && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
  539. return 0;
  540. # endif
  541. # if !defined(OPENSSL_NO_PADLOCKENG)
  542. if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
  543. && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
  544. return 0;
  545. # endif
  546. # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
  547. if ((opts & OPENSSL_INIT_ENGINE_CAPI)
  548. && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
  549. return 0;
  550. # endif
  551. # if !defined(OPENSSL_NO_AFALGENG)
  552. if ((opts & OPENSSL_INIT_ENGINE_AFALG)
  553. && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
  554. return 0;
  555. # endif
  556. # endif
  557. if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
  558. | OPENSSL_INIT_ENGINE_OPENSSL
  559. | OPENSSL_INIT_ENGINE_AFALG)) {
  560. ENGINE_register_all_complete();
  561. }
  562. #endif
  563. #ifndef OPENSSL_NO_COMP
  564. if ((opts & OPENSSL_INIT_ZLIB)
  565. && !RUN_ONCE(&zlib, ossl_init_zlib))
  566. return 0;
  567. #endif
  568. if (!CRYPTO_atomic_or(&optsdone, opts, &tmp, init_lock))
  569. return 0;
  570. return 1;
  571. }
  572. int OPENSSL_atexit(void (*handler)(void))
  573. {
  574. OPENSSL_INIT_STOP *newhand;
  575. #if !defined(OPENSSL_USE_NODELETE)\
  576. && !defined(OPENSSL_NO_PINSHARED)
  577. {
  578. union {
  579. void *sym;
  580. void (*func)(void);
  581. } handlersym;
  582. handlersym.func = handler;
  583. # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
  584. {
  585. HMODULE handle = NULL;
  586. BOOL ret;
  587. /*
  588. * We don't use the DSO route for WIN32 because there is a better
  589. * way
  590. */
  591. ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
  592. | GET_MODULE_HANDLE_EX_FLAG_PIN,
  593. handlersym.sym, &handle);
  594. if (!ret)
  595. return 0;
  596. }
  597. # elif !defined(DSO_NONE)
  598. /*
  599. * Deliberately leak a reference to the handler. This will force the
  600. * library/code containing the handler to remain loaded until we run the
  601. * atexit handler. If -znodelete has been used then this is
  602. * unnecessary.
  603. */
  604. {
  605. DSO *dso = NULL;
  606. ERR_set_mark();
  607. dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
  608. /* See same code above in ossl_init_base() for an explanation. */
  609. OSSL_TRACE1(INIT,
  610. "atexit: obtained DSO reference? %s\n",
  611. (dso == NULL ? "No!" : "Yes."));
  612. DSO_free(dso);
  613. ERR_pop_to_mark();
  614. }
  615. # endif
  616. }
  617. #endif
  618. if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) {
  619. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  620. return 0;
  621. }
  622. newhand->handler = handler;
  623. newhand->next = stop_handlers;
  624. stop_handlers = newhand;
  625. return 1;
  626. }