]>
Commit | Line | Data |
---|---|---|
b184e3ef | 1 | /* |
2039c421 | 2 | * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. |
b184e3ef | 3 | * |
2039c421 RS |
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 | |
b184e3ef MC |
8 | */ |
9 | ||
b184e3ef MC |
10 | #include <internal/cryptlib_int.h> |
11 | #include <openssl/err.h> | |
f3cd81d6 | 12 | #include <internal/rand.h> |
62d876ad | 13 | #include <internal/bio.h> |
b184e3ef | 14 | #include <openssl/evp.h> |
b184e3ef MC |
15 | #include <internal/evp_int.h> |
16 | #include <internal/conf.h> | |
17 | #include <internal/async.h> | |
18 | #include <internal/engine.h> | |
02a247e0 | 19 | #include <internal/comp.h> |
b184e3ef | 20 | #include <internal/err.h> |
13524b11 | 21 | #include <internal/err_int.h> |
7b8cc9b3 | 22 | #include <internal/objects.h> |
b184e3ef | 23 | #include <stdlib.h> |
dd27f16e | 24 | #include <assert.h> |
c2e4e5d2 | 25 | #include <internal/thread_once.h> |
dd27f16e RS |
26 | |
27 | static int stopped = 0; | |
b184e3ef | 28 | |
71567a6f MC |
29 | static void ossl_init_thread_stop(struct thread_local_inits_st *locals); |
30 | ||
a072ed0c | 31 | static CRYPTO_THREAD_LOCAL threadstopkey; |
b184e3ef | 32 | |
b184e3ef MC |
33 | static void ossl_init_thread_stop_wrap(void *local) |
34 | { | |
35 | ossl_init_thread_stop((struct thread_local_inits_st *)local); | |
36 | } | |
37 | ||
b7326ea7 | 38 | static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc) |
b184e3ef | 39 | { |
a072ed0c MC |
40 | struct thread_local_inits_st *local = |
41 | CRYPTO_THREAD_get_local(&threadstopkey); | |
b184e3ef MC |
42 | |
43 | if (local == NULL && alloc) { | |
44 | local = OPENSSL_zalloc(sizeof *local); | |
a072ed0c | 45 | CRYPTO_THREAD_set_local(&threadstopkey, local); |
b184e3ef | 46 | } |
b7326ea7 | 47 | if (!alloc) { |
a072ed0c | 48 | CRYPTO_THREAD_set_local(&threadstopkey, NULL); |
b7326ea7 | 49 | } |
b184e3ef MC |
50 | |
51 | return local; | |
52 | } | |
53 | ||
7253fd55 | 54 | typedef struct ossl_init_stop_st OPENSSL_INIT_STOP; |
b184e3ef MC |
55 | struct ossl_init_stop_st { |
56 | void (*handler)(void); | |
57 | OPENSSL_INIT_STOP *next; | |
58 | }; | |
59 | ||
60 | static OPENSSL_INIT_STOP *stop_handlers = NULL; | |
c292b105 | 61 | static CRYPTO_RWLOCK *init_lock = NULL; |
b184e3ef | 62 | |
b1f1e7ae | 63 | static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 64 | static int base_inited = 0; |
c2e4e5d2 | 65 | DEFINE_RUN_ONCE_STATIC(ossl_init_base) |
b184e3ef MC |
66 | { |
67 | #ifdef OPENSSL_INIT_DEBUG | |
68 | fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n"); | |
69 | #endif | |
a072ed0c MC |
70 | /* |
71 | * We use a dummy thread local key here. We use the destructor to detect | |
72 | * when the thread is going to stop (where that feature is available) | |
73 | */ | |
74 | CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap); | |
c7b7938e | 75 | #ifndef OPENSSL_SYS_UEFI |
f672aee4 | 76 | atexit(OPENSSL_cleanup); |
c7b7938e | 77 | #endif |
c2e4e5d2 RL |
78 | if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL) |
79 | return 0; | |
b184e3ef MC |
80 | OPENSSL_cpuid_setup(); |
81 | base_inited = 1; | |
c2e4e5d2 | 82 | return 1; |
b184e3ef MC |
83 | } |
84 | ||
b1f1e7ae | 85 | static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 86 | static int load_crypto_strings_inited = 0; |
c2e4e5d2 | 87 | DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings) |
b184e3ef MC |
88 | { |
89 | /* Do nothing in this case */ | |
c2e4e5d2 | 90 | return 1; |
b184e3ef MC |
91 | } |
92 | ||
c2e4e5d2 | 93 | DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings) |
b184e3ef | 94 | { |
498abff0 MC |
95 | /* |
96 | * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time | |
97 | * pulling in all the error strings during static linking | |
98 | */ | |
99 | #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT) | |
b184e3ef MC |
100 | # ifdef OPENSSL_INIT_DEBUG |
101 | fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: " | |
b3599dbb | 102 | "err_load_crypto_strings_int()\n"); |
b184e3ef | 103 | # endif |
b3599dbb | 104 | err_load_crypto_strings_int(); |
b184e3ef MC |
105 | #endif |
106 | load_crypto_strings_inited = 1; | |
c2e4e5d2 | 107 | return 1; |
b184e3ef MC |
108 | } |
109 | ||
b1f1e7ae | 110 | static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 111 | DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers) |
b184e3ef MC |
112 | { |
113 | /* | |
114 | * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time | |
115 | * pulling in all the ciphers during static linking | |
116 | */ | |
117 | #ifndef OPENSSL_NO_AUTOALGINIT | |
118 | # ifdef OPENSSL_INIT_DEBUG | |
119 | fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: " | |
b3599dbb | 120 | "openssl_add_all_ciphers_int()\n"); |
b184e3ef | 121 | # endif |
b3599dbb | 122 | openssl_add_all_ciphers_int(); |
b184e3ef | 123 | #endif |
c2e4e5d2 | 124 | return 1; |
b184e3ef MC |
125 | } |
126 | ||
b1f1e7ae | 127 | static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 128 | DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests) |
b184e3ef MC |
129 | { |
130 | /* | |
131 | * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time | |
132 | * pulling in all the ciphers during static linking | |
133 | */ | |
134 | #ifndef OPENSSL_NO_AUTOALGINIT | |
135 | # ifdef OPENSSL_INIT_DEBUG | |
136 | fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: " | |
b3599dbb | 137 | "openssl_add_all_digests()\n"); |
b184e3ef | 138 | # endif |
b3599dbb | 139 | openssl_add_all_digests_int(); |
b184e3ef | 140 | #endif |
c2e4e5d2 | 141 | return 1; |
b184e3ef MC |
142 | } |
143 | ||
c2e4e5d2 | 144 | DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs) |
b184e3ef MC |
145 | { |
146 | /* Do nothing */ | |
c2e4e5d2 | 147 | return 1; |
b184e3ef MC |
148 | } |
149 | ||
b1f1e7ae | 150 | static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 151 | static int config_inited = 0; |
cda3ae5b | 152 | static const char *appname; |
c2e4e5d2 | 153 | DEFINE_RUN_ONCE_STATIC(ossl_init_config) |
b184e3ef MC |
154 | { |
155 | #ifdef OPENSSL_INIT_DEBUG | |
156 | fprintf(stderr, | |
b3599dbb | 157 | "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n", |
cda3ae5b | 158 | appname == NULL ? "NULL" : appname); |
b184e3ef | 159 | #endif |
cda3ae5b | 160 | openssl_config_int(appname); |
b184e3ef | 161 | config_inited = 1; |
c2e4e5d2 | 162 | return 1; |
b184e3ef | 163 | } |
c2e4e5d2 | 164 | DEFINE_RUN_ONCE_STATIC(ossl_init_no_config) |
b184e3ef MC |
165 | { |
166 | #ifdef OPENSSL_INIT_DEBUG | |
167 | fprintf(stderr, | |
b3599dbb | 168 | "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n"); |
b184e3ef | 169 | #endif |
b3599dbb | 170 | openssl_no_config_int(); |
b184e3ef | 171 | config_inited = 1; |
c2e4e5d2 | 172 | return 1; |
b184e3ef MC |
173 | } |
174 | ||
b1f1e7ae | 175 | static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef | 176 | static int async_inited = 0; |
c2e4e5d2 | 177 | DEFINE_RUN_ONCE_STATIC(ossl_init_async) |
b184e3ef MC |
178 | { |
179 | #ifdef OPENSSL_INIT_DEBUG | |
180 | fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n"); | |
181 | #endif | |
c2e4e5d2 RL |
182 | if (!async_init()) |
183 | return 0; | |
b184e3ef | 184 | async_inited = 1; |
c2e4e5d2 | 185 | return 1; |
b184e3ef MC |
186 | } |
187 | ||
188 | #ifndef OPENSSL_NO_ENGINE | |
b1f1e7ae | 189 | static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 190 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl) |
b184e3ef MC |
191 | { |
192 | # ifdef OPENSSL_INIT_DEBUG | |
193 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: " | |
b3599dbb | 194 | "engine_load_openssl_int()\n"); |
b184e3ef | 195 | # endif |
b3599dbb | 196 | engine_load_openssl_int(); |
c2e4e5d2 | 197 | return 1; |
b184e3ef MC |
198 | } |
199 | # if !defined(OPENSSL_NO_HW) && \ | |
200 | (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)) | |
b1f1e7ae | 201 | static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 202 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_cryptodev) |
b184e3ef MC |
203 | { |
204 | # ifdef OPENSSL_INIT_DEBUG | |
205 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: " | |
b3599dbb | 206 | "engine_load_cryptodev_int()\n"); |
b184e3ef | 207 | # endif |
b3599dbb | 208 | engine_load_cryptodev_int(); |
c2e4e5d2 | 209 | return 1; |
b184e3ef MC |
210 | } |
211 | # endif | |
212 | ||
213 | # ifndef OPENSSL_NO_RDRAND | |
b1f1e7ae | 214 | static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 215 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand) |
b184e3ef MC |
216 | { |
217 | # ifdef OPENSSL_INIT_DEBUG | |
218 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: " | |
b3599dbb | 219 | "engine_load_rdrand_int()\n"); |
b184e3ef | 220 | # endif |
b3599dbb | 221 | engine_load_rdrand_int(); |
c2e4e5d2 | 222 | return 1; |
b184e3ef MC |
223 | } |
224 | # endif | |
b1f1e7ae | 225 | static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 226 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic) |
b184e3ef MC |
227 | { |
228 | # ifdef OPENSSL_INIT_DEBUG | |
229 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: " | |
b3599dbb | 230 | "engine_load_dynamic_int()\n"); |
b184e3ef | 231 | # endif |
b3599dbb | 232 | engine_load_dynamic_int(); |
c2e4e5d2 | 233 | return 1; |
b184e3ef MC |
234 | } |
235 | # ifndef OPENSSL_NO_STATIC_ENGINE | |
236 | # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK) | |
b1f1e7ae | 237 | static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 238 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock) |
b184e3ef MC |
239 | { |
240 | # ifdef OPENSSL_INIT_DEBUG | |
241 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: " | |
b3599dbb | 242 | "engine_load_padlock_int()\n"); |
b184e3ef | 243 | # endif |
b3599dbb | 244 | engine_load_padlock_int(); |
c2e4e5d2 | 245 | return 1; |
b184e3ef MC |
246 | } |
247 | # endif | |
248 | # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG) | |
b1f1e7ae | 249 | static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 250 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi) |
b184e3ef MC |
251 | { |
252 | # ifdef OPENSSL_INIT_DEBUG | |
253 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: " | |
b3599dbb | 254 | "engine_load_capi_int()\n"); |
b184e3ef | 255 | # endif |
b3599dbb | 256 | engine_load_capi_int(); |
c2e4e5d2 | 257 | return 1; |
b184e3ef MC |
258 | } |
259 | # endif | |
b1f1e7ae | 260 | static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 261 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dasync) |
b184e3ef MC |
262 | { |
263 | # ifdef OPENSSL_INIT_DEBUG | |
264 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: " | |
b3599dbb | 265 | "engine_load_dasync_int()\n"); |
b184e3ef | 266 | # endif |
b3599dbb | 267 | engine_load_dasync_int(); |
c2e4e5d2 | 268 | return 1; |
b184e3ef | 269 | } |
6cba4a66 | 270 | # if !defined(OPENSSL_NO_AFALGENG) |
a4d8bcf1 | 271 | static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT; |
c2e4e5d2 | 272 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg) |
6cba4a66 | 273 | { |
274 | # ifdef OPENSSL_INIT_DEBUG | |
275 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: " | |
b3599dbb | 276 | "engine_load_afalg_int()\n"); |
6cba4a66 | 277 | # endif |
b3599dbb | 278 | engine_load_afalg_int(); |
c2e4e5d2 | 279 | return 1; |
6cba4a66 | 280 | } |
281 | # endif | |
b184e3ef MC |
282 | # endif |
283 | #endif | |
284 | ||
e4ad0763 | 285 | #ifndef OPENSSL_NO_COMP |
b1f1e7ae | 286 | static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT; |
e4ad0763 | 287 | |
b184e3ef | 288 | static int zlib_inited = 0; |
c2e4e5d2 | 289 | DEFINE_RUN_ONCE_STATIC(ossl_init_zlib) |
b184e3ef MC |
290 | { |
291 | /* Do nothing - we need to know about this for the later cleanup */ | |
292 | zlib_inited = 1; | |
c2e4e5d2 | 293 | return 1; |
b184e3ef | 294 | } |
e4ad0763 | 295 | #endif |
b184e3ef | 296 | |
71567a6f | 297 | static void ossl_init_thread_stop(struct thread_local_inits_st *locals) |
b184e3ef MC |
298 | { |
299 | /* Can't do much about this */ | |
300 | if (locals == NULL) | |
301 | return; | |
302 | ||
303 | if (locals->async) { | |
304 | #ifdef OPENSSL_INIT_DEBUG | |
305 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: " | |
306 | "ASYNC_cleanup_thread()\n"); | |
307 | #endif | |
308 | ASYNC_cleanup_thread(); | |
309 | } | |
310 | ||
311 | if (locals->err_state) { | |
312 | #ifdef OPENSSL_INIT_DEBUG | |
313 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: " | |
21e00174 | 314 | "err_delete_thread_state()\n"); |
b184e3ef | 315 | #endif |
21e00174 | 316 | err_delete_thread_state(); |
b184e3ef MC |
317 | } |
318 | ||
319 | OPENSSL_free(locals); | |
b184e3ef MC |
320 | } |
321 | ||
f672aee4 | 322 | void OPENSSL_thread_stop(void) |
71567a6f MC |
323 | { |
324 | ossl_init_thread_stop( | |
325 | (struct thread_local_inits_st *)ossl_init_get_thread_local(0)); | |
326 | } | |
327 | ||
b184e3ef MC |
328 | int ossl_init_thread_start(uint64_t opts) |
329 | { | |
330 | struct thread_local_inits_st *locals = ossl_init_get_thread_local(1); | |
331 | ||
332 | if (locals == NULL) | |
333 | return 0; | |
334 | ||
335 | if (opts & OPENSSL_INIT_THREAD_ASYNC) { | |
336 | #ifdef OPENSSL_INIT_DEBUG | |
337 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " | |
338 | "marking thread for async\n"); | |
339 | #endif | |
340 | locals->async = 1; | |
341 | } | |
342 | ||
343 | if (opts & OPENSSL_INIT_THREAD_ERR_STATE) { | |
344 | #ifdef OPENSSL_INIT_DEBUG | |
345 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " | |
346 | "marking thread for err_state\n"); | |
347 | #endif | |
348 | locals->err_state = 1; | |
349 | } | |
350 | ||
351 | return 1; | |
352 | } | |
353 | ||
f672aee4 | 354 | void OPENSSL_cleanup(void) |
b184e3ef MC |
355 | { |
356 | OPENSSL_INIT_STOP *currhandler, *lasthandler; | |
357 | ||
deca5df2 MC |
358 | /* If we've not been inited then no need to deinit */ |
359 | if (!base_inited) | |
360 | return; | |
361 | ||
dd27f16e RS |
362 | /* Might be explicitly called and also by atexit */ |
363 | if (stopped) | |
364 | return; | |
365 | stopped = 1; | |
366 | ||
b184e3ef MC |
367 | /* |
368 | * Thread stop may not get automatically called by the thread library for | |
369 | * the very last thread in some situations, so call it directly. | |
370 | */ | |
371 | ossl_init_thread_stop(ossl_init_get_thread_local(0)); | |
372 | ||
373 | currhandler = stop_handlers; | |
374 | while (currhandler != NULL) { | |
375 | currhandler->handler(); | |
376 | lasthandler = currhandler; | |
377 | currhandler = currhandler->next; | |
378 | OPENSSL_free(lasthandler); | |
379 | } | |
380 | stop_handlers = NULL; | |
c292b105 MC |
381 | |
382 | CRYPTO_THREAD_lock_free(init_lock); | |
383 | ||
b184e3ef MC |
384 | /* |
385 | * We assume we are single-threaded for this function, i.e. no race | |
386 | * conditions for the various "*_inited" vars below. | |
387 | */ | |
388 | ||
e4ad0763 | 389 | #ifndef OPENSSL_NO_COMP |
b184e3ef MC |
390 | if (zlib_inited) { |
391 | #ifdef OPENSSL_INIT_DEBUG | |
f672aee4 | 392 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 393 | "comp_zlib_cleanup_int()\n"); |
b184e3ef | 394 | #endif |
b3599dbb | 395 | comp_zlib_cleanup_int(); |
b184e3ef | 396 | } |
e4ad0763 | 397 | #endif |
b184e3ef | 398 | |
ed49f43a MC |
399 | if (async_inited) { |
400 | # ifdef OPENSSL_INIT_DEBUG | |
401 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " | |
402 | "async_deinit()\n"); | |
403 | # endif | |
404 | async_deinit(); | |
405 | } | |
ed49f43a | 406 | |
b184e3ef MC |
407 | if (load_crypto_strings_inited) { |
408 | #ifdef OPENSSL_INIT_DEBUG | |
f672aee4 | 409 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 410 | "err_free_strings_int()\n"); |
b184e3ef | 411 | #endif |
b3599dbb | 412 | err_free_strings_int(); |
b184e3ef MC |
413 | } |
414 | ||
a072ed0c | 415 | CRYPTO_THREAD_cleanup_local(&threadstopkey); |
6bc7bad0 | 416 | |
b184e3ef | 417 | #ifdef OPENSSL_INIT_DEBUG |
58a8fc25 | 418 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 419 | "rand_cleanup_int()\n"); |
58a8fc25 | 420 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 421 | "conf_modules_free_int()\n"); |
9749a07a | 422 | #ifndef OPENSSL_NO_ENGINE |
ae6412f3 | 423 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 424 | "engine_cleanup_int()\n"); |
9749a07a | 425 | #endif |
58a8fc25 | 426 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 427 | "crypto_cleanup_all_ex_data_int()\n"); |
58a8fc25 | 428 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 429 | "bio_sock_cleanup_int()\n"); |
ff234405 MC |
430 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
431 | "bio_cleanup()\n"); | |
58a8fc25 | 432 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 433 | "evp_cleanup_int()\n"); |
58a8fc25 | 434 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
b3599dbb | 435 | "obj_cleanup_int()\n"); |
ff234405 MC |
436 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
437 | "err_cleanup()\n"); | |
9749a07a | 438 | #endif |
58a8fc25 MC |
439 | /* |
440 | * Note that cleanup order is important: | |
a535fe12 | 441 | * - rand_cleanup_int could call an ENGINE's RAND cleanup function so |
b3599dbb | 442 | * must be called before engine_cleanup_int() |
58a8fc25 MC |
443 | * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up |
444 | * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data(). | |
b3599dbb MC |
445 | * - conf_modules_free_int() can end up in ENGINE code so must be called |
446 | * before engine_cleanup_int() | |
a535fe12 DSH |
447 | * - ENGINEs and additional EVP algorithms might use added OIDs names so |
448 | * obj_cleanup_int() must be called last | |
58a8fc25 | 449 | */ |
b3599dbb MC |
450 | rand_cleanup_int(); |
451 | conf_modules_free_int(); | |
773fd0ba | 452 | #ifndef OPENSSL_NO_ENGINE |
b3599dbb | 453 | engine_cleanup_int(); |
773fd0ba | 454 | #endif |
b3599dbb | 455 | crypto_cleanup_all_ex_data_int(); |
ff234405 | 456 | bio_cleanup(); |
b3599dbb MC |
457 | evp_cleanup_int(); |
458 | obj_cleanup_int(); | |
ff234405 MC |
459 | err_cleanup(); |
460 | ||
deca5df2 | 461 | base_inited = 0; |
b184e3ef MC |
462 | } |
463 | ||
b184e3ef MC |
464 | /* |
465 | * If this function is called with a non NULL settings value then it must be | |
466 | * called prior to any threads making calls to any OpenSSL functions, | |
467 | * i.e. passing a non-null settings value is assumed to be single-threaded. | |
468 | */ | |
0fc32b07 | 469 | int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) |
b184e3ef | 470 | { |
302f7588 MC |
471 | static int stoperrset = 0; |
472 | ||
473 | if (stopped) { | |
474 | if (!stoperrset) { | |
475 | /* | |
476 | * We only ever set this once to avoid getting into an infinite | |
477 | * loop where the error system keeps trying to init and fails so | |
478 | * sets an error etc | |
479 | */ | |
480 | stoperrset = 1; | |
a4625290 | 481 | CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL); |
302f7588 | 482 | } |
0fc32b07 | 483 | return 0; |
302f7588 | 484 | } |
dd27f16e | 485 | |
c2e4e5d2 | 486 | if (!RUN_ONCE(&base, ossl_init_base)) |
b1f1e7ae | 487 | return 0; |
b184e3ef | 488 | |
b1f1e7ae | 489 | if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS) |
c2e4e5d2 RL |
490 | && !RUN_ONCE(&load_crypto_strings, |
491 | ossl_init_no_load_crypto_strings)) | |
b1f1e7ae | 492 | return 0; |
b184e3ef | 493 | |
b1f1e7ae | 494 | if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS) |
c2e4e5d2 | 495 | && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings)) |
b1f1e7ae | 496 | return 0; |
b184e3ef | 497 | |
b1f1e7ae | 498 | if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS) |
c2e4e5d2 | 499 | && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs)) |
b1f1e7ae | 500 | return 0; |
b184e3ef | 501 | |
b1f1e7ae | 502 | if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS) |
c2e4e5d2 | 503 | && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers)) |
b1f1e7ae | 504 | return 0; |
b184e3ef | 505 | |
b1f1e7ae | 506 | if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS) |
c2e4e5d2 | 507 | && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs)) |
b1f1e7ae | 508 | return 0; |
b184e3ef | 509 | |
b1f1e7ae | 510 | if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS) |
c2e4e5d2 | 511 | && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests)) |
b1f1e7ae | 512 | return 0; |
b184e3ef | 513 | |
b1f1e7ae | 514 | if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) |
c2e4e5d2 | 515 | && !RUN_ONCE(&config, ossl_init_no_config)) |
b1f1e7ae | 516 | return 0; |
b184e3ef MC |
517 | |
518 | if (opts & OPENSSL_INIT_LOAD_CONFIG) { | |
b1f1e7ae | 519 | int ret; |
c292b105 | 520 | CRYPTO_THREAD_write_lock(init_lock); |
cda3ae5b | 521 | appname = (settings == NULL) ? NULL : settings->appname; |
c2e4e5d2 | 522 | ret = RUN_ONCE(&config, ossl_init_config); |
c292b105 | 523 | CRYPTO_THREAD_unlock(init_lock); |
b1f1e7ae MC |
524 | if (!ret) |
525 | return 0; | |
b184e3ef MC |
526 | } |
527 | ||
b1f1e7ae | 528 | if ((opts & OPENSSL_INIT_ASYNC) |
c2e4e5d2 | 529 | && !RUN_ONCE(&async, ossl_init_async)) |
b1f1e7ae | 530 | return 0; |
7626fbf2 | 531 | |
b184e3ef | 532 | #ifndef OPENSSL_NO_ENGINE |
b1f1e7ae | 533 | if ((opts & OPENSSL_INIT_ENGINE_OPENSSL) |
c2e4e5d2 | 534 | && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl)) |
b1f1e7ae | 535 | return 0; |
b184e3ef MC |
536 | # if !defined(OPENSSL_NO_HW) && \ |
537 | (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)) | |
b1f1e7ae | 538 | if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV) |
c2e4e5d2 | 539 | && !RUN_ONCE(&engine_cryptodev, ossl_init_engine_cryptodev)) |
b1f1e7ae | 540 | return 0; |
b184e3ef MC |
541 | # endif |
542 | # ifndef OPENSSL_NO_RDRAND | |
b1f1e7ae | 543 | if ((opts & OPENSSL_INIT_ENGINE_RDRAND) |
c2e4e5d2 | 544 | && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand)) |
b1f1e7ae | 545 | return 0; |
b184e3ef | 546 | # endif |
b1f1e7ae | 547 | if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC) |
c2e4e5d2 | 548 | && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic)) |
b1f1e7ae | 549 | return 0; |
b184e3ef MC |
550 | # ifndef OPENSSL_NO_STATIC_ENGINE |
551 | # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK) | |
b1f1e7ae | 552 | if ((opts & OPENSSL_INIT_ENGINE_PADLOCK) |
c2e4e5d2 | 553 | && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock)) |
b1f1e7ae | 554 | return 0; |
b184e3ef MC |
555 | # endif |
556 | # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG) | |
b1f1e7ae | 557 | if ((opts & OPENSSL_INIT_ENGINE_CAPI) |
c2e4e5d2 | 558 | && !RUN_ONCE(&engine_capi, ossl_init_engine_capi)) |
b1f1e7ae | 559 | return 0; |
b184e3ef | 560 | # endif |
b1f1e7ae | 561 | if ((opts & OPENSSL_INIT_ENGINE_DASYNC) |
c2e4e5d2 | 562 | && !RUN_ONCE(&engine_dasync, ossl_init_engine_dasync)) |
b1f1e7ae | 563 | return 0; |
6cba4a66 | 564 | # if !defined(OPENSSL_NO_AFALGENG) |
b1f1e7ae | 565 | if ((opts & OPENSSL_INIT_ENGINE_AFALG) |
c2e4e5d2 | 566 | && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg)) |
b1f1e7ae | 567 | return 0; |
6cba4a66 | 568 | # endif |
b184e3ef MC |
569 | # endif |
570 | if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN | |
6cba4a66 | 571 | | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL |
572 | | OPENSSL_INIT_ENGINE_AFALG)) { | |
b184e3ef MC |
573 | ENGINE_register_all_complete(); |
574 | } | |
575 | #endif | |
576 | ||
e4ad0763 | 577 | #ifndef OPENSSL_NO_COMP |
b1f1e7ae | 578 | if ((opts & OPENSSL_INIT_ZLIB) |
c2e4e5d2 | 579 | && !RUN_ONCE(&zlib, ossl_init_zlib)) |
b1f1e7ae | 580 | return 0; |
e4ad0763 | 581 | #endif |
0fc32b07 MC |
582 | |
583 | return 1; | |
b184e3ef MC |
584 | } |
585 | ||
f672aee4 | 586 | int OPENSSL_atexit(void (*handler)(void)) |
b184e3ef MC |
587 | { |
588 | OPENSSL_INIT_STOP *newhand; | |
589 | ||
590 | newhand = OPENSSL_malloc(sizeof(*newhand)); | |
591 | if (newhand == NULL) | |
592 | return 0; | |
593 | ||
594 | newhand->handler = handler; | |
595 | newhand->next = stop_handlers; | |
596 | stop_handlers = newhand; | |
597 | ||
598 | return 1; | |
599 | } |