init.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. /* 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 OPENSSL_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 openssl_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. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_store_cleanup_int()\n");
  368. ossl_store_cleanup_int();
  369. OSSL_TRACE(INIT, "OPENSSL_cleanup: openssl_ctx_default_deinit()\n");
  370. openssl_ctx_default_deinit();
  371. ossl_cleanup_thread();
  372. OSSL_TRACE(INIT, "OPENSSL_cleanup: bio_cleanup()\n");
  373. bio_cleanup();
  374. OSSL_TRACE(INIT, "OPENSSL_cleanup: evp_cleanup_int()\n");
  375. evp_cleanup_int();
  376. OSSL_TRACE(INIT, "OPENSSL_cleanup: obj_cleanup_int()\n");
  377. obj_cleanup_int();
  378. OSSL_TRACE(INIT, "OPENSSL_cleanup: err_int()\n");
  379. err_cleanup();
  380. OSSL_TRACE(INIT, "OPENSSL_cleanup: CRYPTO_secure_malloc_done()\n");
  381. CRYPTO_secure_malloc_done();
  382. #ifndef OPENSSL_NO_CMP
  383. OSSL_TRACE(INIT, "OPENSSL_cleanup: OSSL_CMP_log_close()\n");
  384. OSSL_CMP_log_close();
  385. #endif
  386. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_trace_cleanup()\n");
  387. ossl_trace_cleanup();
  388. base_inited = 0;
  389. }
  390. /*
  391. * If this function is called with a non NULL settings value then it must be
  392. * called prior to any threads making calls to any OpenSSL functions,
  393. * i.e. passing a non-null settings value is assumed to be single-threaded.
  394. */
  395. int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
  396. {
  397. /*
  398. * TODO(3.0): This function needs looking at with a view to moving most/all
  399. * of this into OPENSSL_CTX.
  400. */
  401. if (stopped) {
  402. if (!(opts & OPENSSL_INIT_BASE_ONLY))
  403. CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
  404. return 0;
  405. }
  406. /*
  407. * When the caller specifies OPENSSL_INIT_BASE_ONLY, that should be the
  408. * *only* option specified. With that option we return immediately after
  409. * doing the requested limited initialization. Note that
  410. * err_shelve_state() called by us via ossl_init_load_crypto_nodelete()
  411. * re-enters OPENSSL_init_crypto() with OPENSSL_INIT_BASE_ONLY, but with
  412. * base already initialized this is a harmless NOOP.
  413. *
  414. * If we remain the only caller of err_shelve_state() the recursion should
  415. * perhaps be removed, but if in doubt, it can be left in place.
  416. */
  417. if (!RUN_ONCE(&base, ossl_init_base))
  418. return 0;
  419. if (opts & OPENSSL_INIT_BASE_ONLY)
  420. return 1;
  421. /*
  422. * Now we don't always set up exit handlers, the INIT_BASE_ONLY calls
  423. * should not have the side-effect of setting up exit handlers, and
  424. * therefore, this code block is below the INIT_BASE_ONLY-conditioned early
  425. * return above.
  426. */
  427. if ((opts & OPENSSL_INIT_NO_ATEXIT) != 0) {
  428. if (!RUN_ONCE_ALT(&register_atexit, ossl_init_no_register_atexit,
  429. ossl_init_register_atexit))
  430. return 0;
  431. } else if (!RUN_ONCE(&register_atexit, ossl_init_register_atexit)) {
  432. return 0;
  433. }
  434. if (!RUN_ONCE(&load_crypto_nodelete, ossl_init_load_crypto_nodelete))
  435. return 0;
  436. if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
  437. && !RUN_ONCE_ALT(&load_crypto_strings,
  438. ossl_init_no_load_crypto_strings,
  439. ossl_init_load_crypto_strings))
  440. return 0;
  441. if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
  442. && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
  443. return 0;
  444. if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
  445. && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers,
  446. ossl_init_add_all_ciphers))
  447. return 0;
  448. if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
  449. && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
  450. return 0;
  451. if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
  452. && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests,
  453. ossl_init_add_all_digests))
  454. return 0;
  455. if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
  456. && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
  457. return 0;
  458. if ((opts & OPENSSL_INIT_ATFORK)
  459. && !openssl_init_fork_handlers())
  460. return 0;
  461. if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
  462. && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config))
  463. return 0;
  464. if (opts & OPENSSL_INIT_LOAD_CONFIG) {
  465. int ret;
  466. CRYPTO_THREAD_write_lock(init_lock);
  467. conf_settings = settings;
  468. ret = RUN_ONCE(&config, ossl_init_config);
  469. conf_settings = NULL;
  470. CRYPTO_THREAD_unlock(init_lock);
  471. if (ret <= 0)
  472. return 0;
  473. }
  474. if ((opts & OPENSSL_INIT_ASYNC)
  475. && !RUN_ONCE(&async, ossl_init_async))
  476. return 0;
  477. #ifndef OPENSSL_NO_ENGINE
  478. if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
  479. && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
  480. return 0;
  481. # ifndef OPENSSL_NO_RDRAND
  482. if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
  483. && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
  484. return 0;
  485. # endif
  486. if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
  487. && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
  488. return 0;
  489. # ifndef OPENSSL_NO_STATIC_ENGINE
  490. # ifndef OPENSSL_NO_DEVCRYPTOENG
  491. if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
  492. && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
  493. return 0;
  494. # endif
  495. # if !defined(OPENSSL_NO_PADLOCKENG)
  496. if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
  497. && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
  498. return 0;
  499. # endif
  500. # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
  501. if ((opts & OPENSSL_INIT_ENGINE_CAPI)
  502. && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
  503. return 0;
  504. # endif
  505. # if !defined(OPENSSL_NO_AFALGENG)
  506. if ((opts & OPENSSL_INIT_ENGINE_AFALG)
  507. && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
  508. return 0;
  509. # endif
  510. # endif
  511. if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
  512. | OPENSSL_INIT_ENGINE_OPENSSL
  513. | OPENSSL_INIT_ENGINE_AFALG)) {
  514. ENGINE_register_all_complete();
  515. }
  516. #endif
  517. #ifndef OPENSSL_NO_COMP
  518. if ((opts & OPENSSL_INIT_ZLIB)
  519. && !RUN_ONCE(&zlib, ossl_init_zlib))
  520. return 0;
  521. #endif
  522. return 1;
  523. }
  524. int OPENSSL_atexit(void (*handler)(void))
  525. {
  526. OPENSSL_INIT_STOP *newhand;
  527. #if !defined(OPENSSL_USE_NODELETE)\
  528. && !defined(OPENSSL_NO_PINSHARED)
  529. {
  530. union {
  531. void *sym;
  532. void (*func)(void);
  533. } handlersym;
  534. handlersym.func = handler;
  535. # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
  536. {
  537. HMODULE handle = NULL;
  538. BOOL ret;
  539. /*
  540. * We don't use the DSO route for WIN32 because there is a better
  541. * way
  542. */
  543. ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
  544. | GET_MODULE_HANDLE_EX_FLAG_PIN,
  545. handlersym.sym, &handle);
  546. if (!ret)
  547. return 0;
  548. }
  549. # elif !defined(DSO_NONE)
  550. /*
  551. * Deliberately leak a reference to the handler. This will force the
  552. * library/code containing the handler to remain loaded until we run the
  553. * atexit handler. If -znodelete has been used then this is
  554. * unnecessary.
  555. */
  556. {
  557. DSO *dso = NULL;
  558. ERR_set_mark();
  559. dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
  560. /* See same code above in ossl_init_base() for an explanation. */
  561. OSSL_TRACE1(INIT,
  562. "atexit: obtained DSO reference? %s\n",
  563. (dso == NULL ? "No!" : "Yes."));
  564. DSO_free(dso);
  565. ERR_pop_to_mark();
  566. }
  567. # endif
  568. }
  569. #endif
  570. if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) {
  571. CRYPTOerr(CRYPTO_F_OPENSSL_ATEXIT, ERR_R_MALLOC_FAILURE);
  572. return 0;
  573. }
  574. newhand->handler = handler;
  575. newhand->next = stop_handlers;
  576. stop_handlers = newhand;
  577. return 1;
  578. }
  579. #ifdef OPENSSL_SYS_UNIX
  580. /*
  581. * The following three functions are for OpenSSL developers. This is
  582. * where we set/reset state across fork (called via pthread_atfork when
  583. * it exists, or manually by the application when it doesn't).
  584. *
  585. * WARNING! If you put code in either OPENSSL_fork_parent or
  586. * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
  587. * safe. See this link, for example:
  588. * http://man7.org/linux/man-pages/man7/signal-safety.7.html
  589. */
  590. void OPENSSL_fork_prepare(void)
  591. {
  592. }
  593. void OPENSSL_fork_parent(void)
  594. {
  595. }
  596. void OPENSSL_fork_child(void)
  597. {
  598. /* TODO(3.0): Inform all providers about a fork event */
  599. }
  600. #endif