init.c 21 KB

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