init.c 19 KB

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