init.c 21 KB

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