2
0

init.c 23 KB

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