init.c 24 KB

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