init.c 24 KB

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