init.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /*
  2. * Copyright 2016-2019 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. static int load_crypto_strings_inited = 0;
  189. DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
  190. {
  191. int ret = 1;
  192. /*
  193. * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
  194. * pulling in all the error strings during static linking
  195. */
  196. #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
  197. # ifdef OPENSSL_INIT_DEBUG
  198. fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
  199. "err_load_crypto_strings_int()\n");
  200. # endif
  201. ret = err_load_crypto_strings_int();
  202. load_crypto_strings_inited = 1;
  203. #endif
  204. return ret;
  205. }
  206. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_crypto_strings,
  207. ossl_init_load_crypto_strings)
  208. {
  209. /* Do nothing in this case */
  210. return 1;
  211. }
  212. static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
  213. DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
  214. {
  215. /*
  216. * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
  217. * pulling in all the ciphers during static linking
  218. */
  219. #ifndef OPENSSL_NO_AUTOALGINIT
  220. # ifdef OPENSSL_INIT_DEBUG
  221. fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
  222. "openssl_add_all_ciphers_int()\n");
  223. # endif
  224. openssl_add_all_ciphers_int();
  225. #endif
  226. return 1;
  227. }
  228. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_ciphers,
  229. ossl_init_add_all_ciphers)
  230. {
  231. /* Do nothing */
  232. return 1;
  233. }
  234. static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
  235. DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
  236. {
  237. /*
  238. * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
  239. * pulling in all the ciphers during static linking
  240. */
  241. #ifndef OPENSSL_NO_AUTOALGINIT
  242. # ifdef OPENSSL_INIT_DEBUG
  243. fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
  244. "openssl_add_all_digests()\n");
  245. # endif
  246. openssl_add_all_digests_int();
  247. #endif
  248. return 1;
  249. }
  250. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_digests,
  251. ossl_init_add_all_digests)
  252. {
  253. /* Do nothing */
  254. return 1;
  255. }
  256. static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
  257. static int config_inited = 0;
  258. static const OPENSSL_INIT_SETTINGS *conf_settings = NULL;
  259. DEFINE_RUN_ONCE_STATIC(ossl_init_config)
  260. {
  261. int ret = openssl_config_int(conf_settings);
  262. config_inited = 1;
  263. return ret;
  264. }
  265. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_config, ossl_init_config)
  266. {
  267. #ifdef OPENSSL_INIT_DEBUG
  268. fprintf(stderr,
  269. "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
  270. #endif
  271. openssl_no_config_int();
  272. config_inited = 1;
  273. return 1;
  274. }
  275. static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
  276. static int async_inited = 0;
  277. DEFINE_RUN_ONCE_STATIC(ossl_init_async)
  278. {
  279. #ifdef OPENSSL_INIT_DEBUG
  280. fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
  281. #endif
  282. if (!async_init())
  283. return 0;
  284. async_inited = 1;
  285. return 1;
  286. }
  287. #ifndef OPENSSL_NO_ENGINE
  288. static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
  289. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
  290. {
  291. # ifdef OPENSSL_INIT_DEBUG
  292. fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
  293. "engine_load_openssl_int()\n");
  294. # endif
  295. engine_load_openssl_int();
  296. return 1;
  297. }
  298. # ifndef OPENSSL_NO_DEVCRYPTOENG
  299. static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
  300. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
  301. {
  302. # ifdef OPENSSL_INIT_DEBUG
  303. fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
  304. "engine_load_devcrypto_int()\n");
  305. # endif
  306. engine_load_devcrypto_int();
  307. return 1;
  308. }
  309. # endif
  310. # ifndef OPENSSL_NO_RDRAND
  311. static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
  312. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
  313. {
  314. # ifdef OPENSSL_INIT_DEBUG
  315. fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
  316. "engine_load_rdrand_int()\n");
  317. # endif
  318. engine_load_rdrand_int();
  319. return 1;
  320. }
  321. # endif
  322. static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
  323. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
  324. {
  325. # ifdef OPENSSL_INIT_DEBUG
  326. fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
  327. "engine_load_dynamic_int()\n");
  328. # endif
  329. engine_load_dynamic_int();
  330. return 1;
  331. }
  332. # ifndef OPENSSL_NO_STATIC_ENGINE
  333. # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
  334. static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
  335. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
  336. {
  337. # ifdef OPENSSL_INIT_DEBUG
  338. fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
  339. "engine_load_padlock_int()\n");
  340. # endif
  341. engine_load_padlock_int();
  342. return 1;
  343. }
  344. # endif
  345. # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
  346. static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
  347. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
  348. {
  349. # ifdef OPENSSL_INIT_DEBUG
  350. fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
  351. "engine_load_capi_int()\n");
  352. # endif
  353. engine_load_capi_int();
  354. return 1;
  355. }
  356. # endif
  357. # if !defined(OPENSSL_NO_AFALGENG)
  358. static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
  359. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
  360. {
  361. # ifdef OPENSSL_INIT_DEBUG
  362. fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
  363. "engine_load_afalg_int()\n");
  364. # endif
  365. engine_load_afalg_int();
  366. return 1;
  367. }
  368. # endif
  369. # endif
  370. #endif
  371. #ifndef OPENSSL_NO_COMP
  372. static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
  373. static int zlib_inited = 0;
  374. DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
  375. {
  376. /* Do nothing - we need to know about this for the later cleanup */
  377. zlib_inited = 1;
  378. return 1;
  379. }
  380. #endif
  381. static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
  382. {
  383. /* Can't do much about this */
  384. if (locals == NULL)
  385. return;
  386. if (locals->async) {
  387. #ifdef OPENSSL_INIT_DEBUG
  388. fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
  389. "async_delete_thread_state()\n");
  390. #endif
  391. async_delete_thread_state();
  392. }
  393. if (locals->err_state) {
  394. #ifdef OPENSSL_INIT_DEBUG
  395. fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
  396. "err_delete_thread_state()\n");
  397. #endif
  398. err_delete_thread_state();
  399. }
  400. if (locals->rand) {
  401. #ifdef OPENSSL_INIT_DEBUG
  402. fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
  403. "drbg_delete_thread_state()\n");
  404. #endif
  405. drbg_delete_thread_state();
  406. }
  407. OPENSSL_free(locals);
  408. }
  409. void OPENSSL_thread_stop(void)
  410. {
  411. if (destructor_key.sane != -1)
  412. ossl_init_thread_stop(ossl_init_get_thread_local(0));
  413. }
  414. int ossl_init_thread_start(uint64_t opts)
  415. {
  416. struct thread_local_inits_st *locals;
  417. if (!OPENSSL_init_crypto(0, NULL))
  418. return 0;
  419. locals = ossl_init_get_thread_local(1);
  420. if (locals == NULL)
  421. return 0;
  422. if (opts & OPENSSL_INIT_THREAD_ASYNC) {
  423. #ifdef OPENSSL_INIT_DEBUG
  424. fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
  425. "marking thread for async\n");
  426. #endif
  427. locals->async = 1;
  428. }
  429. if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
  430. #ifdef OPENSSL_INIT_DEBUG
  431. fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
  432. "marking thread for err_state\n");
  433. #endif
  434. locals->err_state = 1;
  435. }
  436. if (opts & OPENSSL_INIT_THREAD_RAND) {
  437. #ifdef OPENSSL_INIT_DEBUG
  438. fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
  439. "marking thread for rand\n");
  440. #endif
  441. locals->rand = 1;
  442. }
  443. return 1;
  444. }
  445. void OPENSSL_cleanup(void)
  446. {
  447. OPENSSL_INIT_STOP *currhandler, *lasthandler;
  448. CRYPTO_THREAD_LOCAL key;
  449. /* If we've not been inited then no need to deinit */
  450. if (!base_inited)
  451. return;
  452. /* Might be explicitly called and also by atexit */
  453. if (stopped)
  454. return;
  455. stopped = 1;
  456. /*
  457. * Thread stop may not get automatically called by the thread library for
  458. * the very last thread in some situations, so call it directly.
  459. */
  460. ossl_init_thread_stop(ossl_init_get_thread_local(0));
  461. currhandler = stop_handlers;
  462. while (currhandler != NULL) {
  463. currhandler->handler();
  464. lasthandler = currhandler;
  465. currhandler = currhandler->next;
  466. OPENSSL_free(lasthandler);
  467. }
  468. stop_handlers = NULL;
  469. CRYPTO_THREAD_lock_free(init_lock);
  470. init_lock = NULL;
  471. /*
  472. * We assume we are single-threaded for this function, i.e. no race
  473. * conditions for the various "*_inited" vars below.
  474. */
  475. #ifndef OPENSSL_NO_COMP
  476. if (zlib_inited) {
  477. #ifdef OPENSSL_INIT_DEBUG
  478. fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
  479. "comp_zlib_cleanup_int()\n");
  480. #endif
  481. comp_zlib_cleanup_int();
  482. }
  483. #endif
  484. if (async_inited) {
  485. # ifdef OPENSSL_INIT_DEBUG
  486. fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
  487. "async_deinit()\n");
  488. # endif
  489. async_deinit();
  490. }
  491. if (load_crypto_strings_inited) {
  492. #ifdef OPENSSL_INIT_DEBUG
  493. fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
  494. "err_free_strings_int()\n");
  495. #endif
  496. err_free_strings_int();
  497. }
  498. key = destructor_key.value;
  499. destructor_key.sane = -1;
  500. CRYPTO_THREAD_cleanup_local(&key);
  501. #ifdef OPENSSL_INIT_DEBUG
  502. fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
  503. "rand_cleanup_int()\n");
  504. fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
  505. "conf_modules_free_int()\n");
  506. #ifndef OPENSSL_NO_ENGINE
  507. fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
  508. "engine_cleanup_int()\n");
  509. #endif
  510. fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
  511. "crypto_cleanup_all_ex_data_int()\n");
  512. fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
  513. "bio_sock_cleanup_int()\n");
  514. fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
  515. "bio_cleanup()\n");
  516. fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
  517. "evp_cleanup_int()\n");
  518. fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
  519. "obj_cleanup_int()\n");
  520. fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
  521. "err_cleanup()\n");
  522. #endif
  523. /*
  524. * Note that cleanup order is important:
  525. * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
  526. * must be called before engine_cleanup_int()
  527. * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
  528. * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
  529. * - conf_modules_free_int() can end up in ENGINE code so must be called
  530. * before engine_cleanup_int()
  531. * - ENGINEs and additional EVP algorithms might use added OIDs names so
  532. * obj_cleanup_int() must be called last
  533. */
  534. rand_cleanup_int();
  535. rand_drbg_cleanup_int();
  536. conf_modules_free_int();
  537. #ifndef OPENSSL_NO_ENGINE
  538. engine_cleanup_int();
  539. #endif
  540. ossl_store_cleanup_int();
  541. crypto_cleanup_all_ex_data_int();
  542. bio_cleanup();
  543. evp_cleanup_int();
  544. obj_cleanup_int();
  545. err_cleanup();
  546. CRYPTO_secure_malloc_done();
  547. base_inited = 0;
  548. }
  549. /*
  550. * If this function is called with a non NULL settings value then it must be
  551. * called prior to any threads making calls to any OpenSSL functions,
  552. * i.e. passing a non-null settings value is assumed to be single-threaded.
  553. */
  554. int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
  555. {
  556. if (stopped) {
  557. if (!(opts & OPENSSL_INIT_BASE_ONLY))
  558. CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
  559. return 0;
  560. }
  561. /*
  562. * When the caller specifies OPENSSL_INIT_BASE_ONLY, that should be the
  563. * *only* option specified. With that option we return immediately after
  564. * doing the requested limited initialization. Note that
  565. * err_shelve_state() called by us via ossl_init_load_crypto_nodelete()
  566. * re-enters OPENSSL_init_crypto() with OPENSSL_INIT_BASE_ONLY, but with
  567. * base already initialized this is a harmless NOOP.
  568. *
  569. * If we remain the only caller of err_shelve_state() the recursion should
  570. * perhaps be removed, but if in doubt, it can be left in place.
  571. */
  572. if (!RUN_ONCE(&base, ossl_init_base))
  573. return 0;
  574. if (opts & OPENSSL_INIT_BASE_ONLY)
  575. return 1;
  576. /*
  577. * Now we don't always set up exit handlers, the INIT_BASE_ONLY calls
  578. * should not have the side-effect of setting up exit handlers, and
  579. * therefore, this code block is below the INIT_BASE_ONLY-conditioned early
  580. * return above.
  581. */
  582. if ((opts & OPENSSL_INIT_NO_ATEXIT) != 0) {
  583. if (!RUN_ONCE_ALT(&register_atexit, ossl_init_no_register_atexit,
  584. ossl_init_register_atexit))
  585. return 0;
  586. } else if (!RUN_ONCE(&register_atexit, ossl_init_register_atexit)) {
  587. return 0;
  588. }
  589. if (!RUN_ONCE(&load_crypto_nodelete, ossl_init_load_crypto_nodelete))
  590. return 0;
  591. if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
  592. && !RUN_ONCE_ALT(&load_crypto_strings,
  593. ossl_init_no_load_crypto_strings,
  594. ossl_init_load_crypto_strings))
  595. return 0;
  596. if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
  597. && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
  598. return 0;
  599. if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
  600. && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers,
  601. ossl_init_add_all_ciphers))
  602. return 0;
  603. if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
  604. && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
  605. return 0;
  606. if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
  607. && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests,
  608. ossl_init_add_all_digests))
  609. return 0;
  610. if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
  611. && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
  612. return 0;
  613. if ((opts & OPENSSL_INIT_ATFORK)
  614. && !openssl_init_fork_handlers())
  615. return 0;
  616. if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
  617. && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config))
  618. return 0;
  619. if (opts & OPENSSL_INIT_LOAD_CONFIG) {
  620. int ret;
  621. CRYPTO_THREAD_write_lock(init_lock);
  622. conf_settings = settings;
  623. ret = RUN_ONCE(&config, ossl_init_config);
  624. conf_settings = NULL;
  625. CRYPTO_THREAD_unlock(init_lock);
  626. if (ret <= 0)
  627. return 0;
  628. }
  629. if ((opts & OPENSSL_INIT_ASYNC)
  630. && !RUN_ONCE(&async, ossl_init_async))
  631. return 0;
  632. #ifndef OPENSSL_NO_ENGINE
  633. if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
  634. && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
  635. return 0;
  636. # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
  637. if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
  638. && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
  639. return 0;
  640. # endif
  641. # ifndef OPENSSL_NO_RDRAND
  642. if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
  643. && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
  644. return 0;
  645. # endif
  646. if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
  647. && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
  648. return 0;
  649. # ifndef OPENSSL_NO_STATIC_ENGINE
  650. # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
  651. if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
  652. && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
  653. return 0;
  654. # endif
  655. # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
  656. if ((opts & OPENSSL_INIT_ENGINE_CAPI)
  657. && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
  658. return 0;
  659. # endif
  660. # if !defined(OPENSSL_NO_AFALGENG)
  661. if ((opts & OPENSSL_INIT_ENGINE_AFALG)
  662. && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
  663. return 0;
  664. # endif
  665. # endif
  666. if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
  667. | OPENSSL_INIT_ENGINE_OPENSSL
  668. | OPENSSL_INIT_ENGINE_AFALG)) {
  669. ENGINE_register_all_complete();
  670. }
  671. #endif
  672. #ifndef OPENSSL_NO_COMP
  673. if ((opts & OPENSSL_INIT_ZLIB)
  674. && !RUN_ONCE(&zlib, ossl_init_zlib))
  675. return 0;
  676. #endif
  677. return 1;
  678. }
  679. int OPENSSL_atexit(void (*handler)(void))
  680. {
  681. OPENSSL_INIT_STOP *newhand;
  682. #if !defined(OPENSSL_USE_NODELETE)\
  683. && !defined(OPENSSL_NO_PINSHARED)
  684. {
  685. union {
  686. void *sym;
  687. void (*func)(void);
  688. } handlersym;
  689. handlersym.func = handler;
  690. # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
  691. {
  692. HMODULE handle = NULL;
  693. BOOL ret;
  694. /*
  695. * We don't use the DSO route for WIN32 because there is a better
  696. * way
  697. */
  698. ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
  699. | GET_MODULE_HANDLE_EX_FLAG_PIN,
  700. handlersym.sym, &handle);
  701. if (!ret)
  702. return 0;
  703. }
  704. # elif !defined(DSO_NONE)
  705. /*
  706. * Deliberately leak a reference to the handler. This will force the
  707. * library/code containing the handler to remain loaded until we run the
  708. * atexit handler. If -znodelete has been used then this is
  709. * unnecessary.
  710. */
  711. {
  712. DSO *dso = NULL;
  713. ERR_set_mark();
  714. dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
  715. # ifdef OPENSSL_INIT_DEBUG
  716. fprintf(stderr,
  717. "OPENSSL_INIT: OPENSSL_atexit: obtained DSO reference? %s\n",
  718. (dso == NULL ? "No!" : "Yes."));
  719. /* See same code above in ossl_init_base() for an explanation. */
  720. # endif
  721. DSO_free(dso);
  722. ERR_pop_to_mark();
  723. }
  724. # endif
  725. }
  726. #endif
  727. if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) {
  728. CRYPTOerr(CRYPTO_F_OPENSSL_ATEXIT, ERR_R_MALLOC_FAILURE);
  729. return 0;
  730. }
  731. newhand->handler = handler;
  732. newhand->next = stop_handlers;
  733. stop_handlers = newhand;
  734. return 1;
  735. }
  736. #ifdef OPENSSL_SYS_UNIX
  737. /*
  738. * The following three functions are for OpenSSL developers. This is
  739. * where we set/reset state across fork (called via pthread_atfork when
  740. * it exists, or manually by the application when it doesn't).
  741. *
  742. * WARNING! If you put code in either OPENSSL_fork_parent or
  743. * OPENSSL_fork_child, you MUST MAKE SURE that they are async-signal-
  744. * safe. See this link, for example:
  745. * http://man7.org/linux/man-pages/man7/signal-safety.7.html
  746. */
  747. void OPENSSL_fork_prepare(void)
  748. {
  749. }
  750. void OPENSSL_fork_parent(void)
  751. {
  752. }
  753. void OPENSSL_fork_child(void)
  754. {
  755. }
  756. #endif