init.c 20 KB

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