init.c 21 KB

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