init.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 "internal/cryptlib_int.h"
  11. #include <openssl/err.h>
  12. #include "internal/rand_int.h"
  13. #include "internal/bio.h"
  14. #include <openssl/evp.h>
  15. #include "internal/evp_int.h"
  16. #include "internal/conf.h"
  17. #include "internal/async.h"
  18. #include "internal/engine.h"
  19. #include "internal/comp.h"
  20. #include "internal/err.h"
  21. #include "internal/err_int.h"
  22. #include "internal/objects.h"
  23. #include <stdlib.h>
  24. #include <assert.h>
  25. #include "internal/thread_once.h"
  26. #include "internal/dso_conf.h"
  27. #include "internal/dso.h"
  28. #include "internal/store.h"
  29. #include <openssl/trace.h>
  30. static int stopped = 0;
  31. typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
  32. struct ossl_init_stop_st {
  33. void (*handler)(void);
  34. OPENSSL_INIT_STOP *next;
  35. };
  36. static OPENSSL_INIT_STOP *stop_handlers = NULL;
  37. static CRYPTO_RWLOCK *init_lock = NULL;
  38. static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
  39. static int base_inited = 0;
  40. DEFINE_RUN_ONCE_STATIC(ossl_init_base)
  41. {
  42. if (ossl_trace_init() == 0)
  43. return 0;
  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 add_all_macs = CRYPTO_ONCE_STATIC_INIT;
  201. DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_macs)
  202. {
  203. /*
  204. * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
  205. * pulling in all the macs during static linking
  206. */
  207. #ifndef OPENSSL_NO_AUTOALGINIT
  208. OSSL_TRACE(INIT, "openssl_add_all_macs_int()\n");
  209. openssl_add_all_macs_int();
  210. #endif
  211. return 1;
  212. }
  213. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_macs, ossl_init_add_all_macs)
  214. {
  215. /* Do nothing */
  216. return 1;
  217. }
  218. static CRYPTO_ONCE add_all_kdfs = CRYPTO_ONCE_STATIC_INIT;
  219. DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_kdfs)
  220. {
  221. /*
  222. * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
  223. * pulling in all the macs during static linking
  224. */
  225. #ifndef OPENSSL_NO_AUTOALGINIT
  226. OSSL_TRACE(INIT, "openssl_add_all_kdfs_int()\n");
  227. openssl_add_all_kdfs_int();
  228. #endif
  229. return 1;
  230. }
  231. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_kdfs, ossl_init_add_all_kdfs)
  232. {
  233. /* Do nothing */
  234. return 1;
  235. }
  236. static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
  237. static int config_inited = 0;
  238. static const OPENSSL_INIT_SETTINGS *conf_settings = NULL;
  239. DEFINE_RUN_ONCE_STATIC(ossl_init_config)
  240. {
  241. int ret = openssl_config_int(conf_settings);
  242. config_inited = 1;
  243. return ret;
  244. }
  245. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_config, ossl_init_config)
  246. {
  247. OSSL_TRACE(INIT, "openssl_no_config_int()\n");
  248. openssl_no_config_int();
  249. config_inited = 1;
  250. return 1;
  251. }
  252. static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
  253. static int async_inited = 0;
  254. DEFINE_RUN_ONCE_STATIC(ossl_init_async)
  255. {
  256. OSSL_TRACE(INIT, "async_init()\n");
  257. if (!async_init())
  258. return 0;
  259. async_inited = 1;
  260. return 1;
  261. }
  262. #ifndef OPENSSL_NO_ENGINE
  263. static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
  264. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
  265. {
  266. OSSL_TRACE(INIT, "engine_load_openssl_int()\n");
  267. engine_load_openssl_int();
  268. return 1;
  269. }
  270. # ifndef OPENSSL_NO_RDRAND
  271. static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
  272. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
  273. {
  274. OSSL_TRACE(INIT, "engine_load_rdrand_int()\n");
  275. engine_load_rdrand_int();
  276. return 1;
  277. }
  278. # endif
  279. static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
  280. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
  281. {
  282. OSSL_TRACE(INIT, "engine_load_dynamic_int()\n");
  283. engine_load_dynamic_int();
  284. return 1;
  285. }
  286. # ifndef OPENSSL_NO_STATIC_ENGINE
  287. # ifndef OPENSSL_NO_DEVCRYPTOENG
  288. static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
  289. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
  290. {
  291. OSSL_TRACE(INIT, "engine_load_devcrypto_int()\n");
  292. engine_load_devcrypto_int();
  293. return 1;
  294. }
  295. # endif
  296. # if !defined(OPENSSL_NO_PADLOCKENG)
  297. static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
  298. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
  299. {
  300. OSSL_TRACE(INIT, "engine_load_padlock_int()\n");
  301. engine_load_padlock_int();
  302. return 1;
  303. }
  304. # endif
  305. # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
  306. static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
  307. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
  308. {
  309. OSSL_TRACE(INIT, "engine_load_capi_int()\n");
  310. engine_load_capi_int();
  311. return 1;
  312. }
  313. # endif
  314. # if !defined(OPENSSL_NO_AFALGENG)
  315. static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
  316. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
  317. {
  318. OSSL_TRACE(INIT, "engine_load_afalg_int()\n");
  319. engine_load_afalg_int();
  320. return 1;
  321. }
  322. # endif
  323. # endif
  324. #endif
  325. #ifndef OPENSSL_NO_COMP
  326. static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
  327. static int zlib_inited = 0;
  328. DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
  329. {
  330. /* Do nothing - we need to know about this for the later cleanup */
  331. zlib_inited = 1;
  332. return 1;
  333. }
  334. #endif
  335. void OPENSSL_cleanup(void)
  336. {
  337. OPENSSL_INIT_STOP *currhandler, *lasthandler;
  338. /*
  339. * TODO(3.0): This function needs looking at with a view to moving most/all
  340. * of this into onfree handlers in OPENSSL_CTX.
  341. */
  342. /* If we've not been inited then no need to deinit */
  343. if (!base_inited)
  344. return;
  345. /* Might be explicitly called and also by atexit */
  346. if (stopped)
  347. return;
  348. stopped = 1;
  349. /*
  350. * Thread stop may not get automatically called by the thread library for
  351. * the very last thread in some situations, so call it directly.
  352. */
  353. OPENSSL_thread_stop();
  354. currhandler = stop_handlers;
  355. while (currhandler != NULL) {
  356. currhandler->handler();
  357. lasthandler = currhandler;
  358. currhandler = currhandler->next;
  359. OPENSSL_free(lasthandler);
  360. }
  361. stop_handlers = NULL;
  362. CRYPTO_THREAD_lock_free(init_lock);
  363. init_lock = NULL;
  364. /*
  365. * We assume we are single-threaded for this function, i.e. no race
  366. * conditions for the various "*_inited" vars below.
  367. */
  368. #ifndef OPENSSL_NO_COMP
  369. if (zlib_inited) {
  370. OSSL_TRACE(INIT, "OPENSSL_cleanup: comp_zlib_cleanup_int()\n");
  371. comp_zlib_cleanup_int();
  372. }
  373. #endif
  374. if (async_inited) {
  375. OSSL_TRACE(INIT, "OPENSSL_cleanup: async_deinit()\n");
  376. async_deinit();
  377. }
  378. if (load_crypto_strings_inited) {
  379. OSSL_TRACE(INIT, "OPENSSL_cleanup: err_free_strings_int()\n");
  380. err_free_strings_int();
  381. }
  382. /*
  383. * Note that cleanup order is important:
  384. * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
  385. * must be called before engine_cleanup_int()
  386. * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
  387. * before the ex data handlers are wiped during default openssl_ctx deinit.
  388. * - conf_modules_free_int() can end up in ENGINE code so must be called
  389. * before engine_cleanup_int()
  390. * - ENGINEs and additional EVP algorithms might use added OIDs names so
  391. * obj_cleanup_int() must be called last
  392. */
  393. OSSL_TRACE(INIT, "OPENSSL_cleanup: rand_cleanup_int()\n");
  394. rand_cleanup_int();
  395. OSSL_TRACE(INIT, "OPENSSL_cleanup: conf_modules_free_int()\n");
  396. conf_modules_free_int();
  397. #ifndef OPENSSL_NO_ENGINE
  398. OSSL_TRACE(INIT, "OPENSSL_cleanup: engine_cleanup_int()\n");
  399. engine_cleanup_int();
  400. #endif
  401. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_store_cleanup_int()\n");
  402. ossl_store_cleanup_int();
  403. OSSL_TRACE(INIT, "OPENSSL_cleanup: openssl_ctx_default_deinit()\n");
  404. openssl_ctx_default_deinit();
  405. ossl_cleanup_thread();
  406. OSSL_TRACE(INIT, "OPENSSL_cleanup: bio_cleanup()\n");
  407. bio_cleanup();
  408. OSSL_TRACE(INIT, "OPENSSL_cleanup: evp_cleanup_int()\n");
  409. evp_cleanup_int();
  410. OSSL_TRACE(INIT, "OPENSSL_cleanup: obj_cleanup_int()\n");
  411. obj_cleanup_int();
  412. OSSL_TRACE(INIT, "OPENSSL_cleanup: err_int()\n");
  413. err_cleanup();
  414. OSSL_TRACE(INIT, "OPENSSL_cleanup: CRYPTO_secure_malloc_done()\n");
  415. CRYPTO_secure_malloc_done();
  416. OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_trace_cleanup()\n");
  417. ossl_trace_cleanup();
  418. base_inited = 0;
  419. }
  420. /*
  421. * If this function is called with a non NULL settings value then it must be
  422. * called prior to any threads making calls to any OpenSSL functions,
  423. * i.e. passing a non-null settings value is assumed to be single-threaded.
  424. */
  425. int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
  426. {
  427. /*
  428. * TODO(3.0): This function needs looking at with a view to moving most/all
  429. * of this into OPENSSL_CTX.
  430. */
  431. if (stopped) {
  432. if (!(opts & OPENSSL_INIT_BASE_ONLY))
  433. CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
  434. return 0;
  435. }
  436. /*
  437. * When the caller specifies OPENSSL_INIT_BASE_ONLY, that should be the
  438. * *only* option specified. With that option we return immediately after
  439. * doing the requested limited initialization. Note that
  440. * err_shelve_state() called by us via ossl_init_load_crypto_nodelete()
  441. * re-enters OPENSSL_init_crypto() with OPENSSL_INIT_BASE_ONLY, but with
  442. * base already initialized this is a harmless NOOP.
  443. *
  444. * If we remain the only caller of err_shelve_state() the recursion should
  445. * perhaps be removed, but if in doubt, it can be left in place.
  446. */
  447. if (!RUN_ONCE(&base, ossl_init_base))
  448. return 0;
  449. if (opts & OPENSSL_INIT_BASE_ONLY)
  450. return 1;
  451. /*
  452. * Now we don't always set up exit handlers, the INIT_BASE_ONLY calls
  453. * should not have the side-effect of setting up exit handlers, and
  454. * therefore, this code block is below the INIT_BASE_ONLY-conditioned early
  455. * return above.
  456. */
  457. if ((opts & OPENSSL_INIT_NO_ATEXIT) != 0) {
  458. if (!RUN_ONCE_ALT(&register_atexit, ossl_init_no_register_atexit,
  459. ossl_init_register_atexit))
  460. return 0;
  461. } else if (!RUN_ONCE(&register_atexit, ossl_init_register_atexit)) {
  462. return 0;
  463. }
  464. if (!RUN_ONCE(&load_crypto_nodelete, ossl_init_load_crypto_nodelete))
  465. return 0;
  466. if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
  467. && !RUN_ONCE_ALT(&load_crypto_strings,
  468. ossl_init_no_load_crypto_strings,
  469. ossl_init_load_crypto_strings))
  470. return 0;
  471. if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
  472. && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
  473. return 0;
  474. if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
  475. && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers,
  476. ossl_init_add_all_ciphers))
  477. return 0;
  478. if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
  479. && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
  480. return 0;
  481. if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
  482. && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests,
  483. ossl_init_add_all_digests))
  484. return 0;
  485. if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
  486. && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
  487. return 0;
  488. if ((opts & OPENSSL_INIT_NO_ADD_ALL_MACS)
  489. && !RUN_ONCE_ALT(&add_all_macs, ossl_init_no_add_all_macs,
  490. ossl_init_add_all_macs))
  491. return 0;
  492. if ((opts & OPENSSL_INIT_ADD_ALL_MACS)
  493. && !RUN_ONCE(&add_all_macs, ossl_init_add_all_macs))
  494. return 0;
  495. if ((opts & OPENSSL_INIT_NO_ADD_ALL_KDFS)
  496. && !RUN_ONCE_ALT(&add_all_kdfs, ossl_init_no_add_all_kdfs,
  497. ossl_init_add_all_kdfs))
  498. return 0;
  499. if ((opts & OPENSSL_INIT_ADD_ALL_KDFS)
  500. && !RUN_ONCE(&add_all_kdfs, ossl_init_add_all_kdfs))
  501. return 0;
  502. if ((opts & OPENSSL_INIT_ATFORK)
  503. && !openssl_init_fork_handlers())
  504. return 0;
  505. if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
  506. && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config))
  507. return 0;
  508. if (opts & OPENSSL_INIT_LOAD_CONFIG) {
  509. int ret;
  510. CRYPTO_THREAD_write_lock(init_lock);
  511. conf_settings = settings;
  512. ret = RUN_ONCE(&config, ossl_init_config);
  513. conf_settings = NULL;
  514. CRYPTO_THREAD_unlock(init_lock);
  515. if (ret <= 0)
  516. return 0;
  517. }
  518. if ((opts & OPENSSL_INIT_ASYNC)
  519. && !RUN_ONCE(&async, ossl_init_async))
  520. return 0;
  521. #ifndef OPENSSL_NO_ENGINE
  522. if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
  523. && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
  524. return 0;
  525. # ifndef OPENSSL_NO_RDRAND
  526. if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
  527. && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
  528. return 0;
  529. # endif
  530. if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
  531. && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
  532. return 0;
  533. # ifndef OPENSSL_NO_STATIC_ENGINE
  534. # ifndef OPENSSL_NO_DEVCRYPTOENG
  535. if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
  536. && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
  537. return 0;
  538. # endif
  539. # if !defined(OPENSSL_NO_PADLOCKENG)
  540. if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
  541. && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
  542. return 0;
  543. # endif
  544. # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
  545. if ((opts & OPENSSL_INIT_ENGINE_CAPI)
  546. && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
  547. return 0;
  548. # endif
  549. # if !defined(OPENSSL_NO_AFALGENG)
  550. if ((opts & OPENSSL_INIT_ENGINE_AFALG)
  551. && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
  552. return 0;
  553. # endif
  554. # endif
  555. if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
  556. | OPENSSL_INIT_ENGINE_OPENSSL
  557. | OPENSSL_INIT_ENGINE_AFALG)) {
  558. ENGINE_register_all_complete();
  559. }
  560. #endif
  561. #ifndef OPENSSL_NO_COMP
  562. if ((opts & OPENSSL_INIT_ZLIB)
  563. && !RUN_ONCE(&zlib, ossl_init_zlib))
  564. return 0;
  565. #endif
  566. return 1;
  567. }
  568. int OPENSSL_atexit(void (*handler)(void))
  569. {
  570. OPENSSL_INIT_STOP *newhand;
  571. #if !defined(OPENSSL_USE_NODELETE)\
  572. && !defined(OPENSSL_NO_PINSHARED)
  573. {
  574. union {
  575. void *sym;
  576. void (*func)(void);
  577. } handlersym;
  578. handlersym.func = handler;
  579. # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
  580. {
  581. HMODULE handle = NULL;
  582. BOOL ret;
  583. /*
  584. * We don't use the DSO route for WIN32 because there is a better
  585. * way
  586. */
  587. ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
  588. | GET_MODULE_HANDLE_EX_FLAG_PIN,
  589. handlersym.sym, &handle);
  590. if (!ret)
  591. return 0;
  592. }
  593. # elif !defined(DSO_NONE)
  594. /*
  595. * Deliberately leak a reference to the handler. This will force the
  596. * library/code containing the handler to remain loaded until we run the
  597. * atexit handler. If -znodelete has been used then this is
  598. * unnecessary.
  599. */
  600. {
  601. DSO *dso = NULL;
  602. ERR_set_mark();
  603. dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
  604. /* See same code above in ossl_init_base() for an explanation. */
  605. OSSL_TRACE1(INIT,
  606. "atexit: obtained DSO reference? %s\n",
  607. (dso == NULL ? "No!" : "Yes."));
  608. DSO_free(dso);
  609. ERR_pop_to_mark();
  610. }
  611. # endif
  612. }
  613. #endif
  614. if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) {
  615. CRYPTOerr(CRYPTO_F_OPENSSL_ATEXIT, ERR_R_MALLOC_FAILURE);
  616. return 0;
  617. }
  618. newhand->handler = handler;
  619. newhand->next = stop_handlers;
  620. stop_handlers = newhand;
  621. return 1;
  622. }
  623. #ifdef OPENSSL_SYS_UNIX
  624. /*
  625. * The following three functions are for OpenSSL developers. This is
  626. * where we set/reset state across fork (called via pthread_atfork when
  627. * it exists, or manually by the application when it doesn't).
  628. *
  629. * WARNING! If you put code in either OPENSSL_fork_parent or
  630. * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
  631. * safe. See this link, for example:
  632. * http://man7.org/linux/man-pages/man7/signal-safety.7.html
  633. */
  634. void OPENSSL_fork_prepare(void)
  635. {
  636. }
  637. void OPENSSL_fork_parent(void)
  638. {
  639. }
  640. void OPENSSL_fork_child(void)
  641. {
  642. rand_fork();
  643. /* TODO(3.0): Inform all providers about a fork event */
  644. }
  645. #endif