]>
Commit | Line | Data |
---|---|---|
b184e3ef MC |
1 | /* |
2 | * Written by Matt Caswell for the OpenSSL project. | |
3 | */ | |
4 | /* ==================================================================== | |
5 | * Copyright (c) 2016 The OpenSSL Project. All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in | |
16 | * the documentation and/or other materials provided with the | |
17 | * distribution. | |
18 | * | |
19 | * 3. All advertising materials mentioning features or use of this | |
20 | * software must display the following acknowledgment: | |
21 | * "This product includes software developed by the OpenSSL Project | |
22 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
23 | * | |
24 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
25 | * endorse or promote products derived from this software without | |
26 | * prior written permission. For written permission, please contact | |
27 | * openssl-core@openssl.org. | |
28 | * | |
29 | * 5. Products derived from this software may not be called "OpenSSL" | |
30 | * nor may "OpenSSL" appear in their names without prior written | |
31 | * permission of the OpenSSL Project. | |
32 | * | |
33 | * 6. Redistributions of any form whatsoever must retain the following | |
34 | * acknowledgment: | |
35 | * "This product includes software developed by the OpenSSL Project | |
36 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
37 | * | |
38 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
39 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
41 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
42 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
47 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
48 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
49 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
50 | * ==================================================================== | |
51 | * | |
52 | * This product includes cryptographic software written by Eric Young | |
53 | * (eay@cryptsoft.com). This product includes software written by Tim | |
54 | * Hudson (tjh@cryptsoft.com). | |
55 | * | |
56 | */ | |
57 | ||
b1f1e7ae | 58 | #include <internal/threads.h> |
b184e3ef MC |
59 | #include <internal/cryptlib_int.h> |
60 | #include <openssl/err.h> | |
f3cd81d6 | 61 | #include <internal/rand.h> |
62d876ad | 62 | #include <internal/bio.h> |
b184e3ef | 63 | #include <openssl/evp.h> |
b184e3ef MC |
64 | #include <internal/evp_int.h> |
65 | #include <internal/conf.h> | |
66 | #include <internal/async.h> | |
67 | #include <internal/engine.h> | |
02a247e0 | 68 | #include <internal/comp.h> |
b184e3ef | 69 | #include <internal/err.h> |
7b8cc9b3 | 70 | #include <internal/objects.h> |
b184e3ef | 71 | #include <stdlib.h> |
dd27f16e RS |
72 | #include <assert.h> |
73 | ||
74 | static int stopped = 0; | |
b184e3ef | 75 | |
71567a6f MC |
76 | static void ossl_init_thread_stop(struct thread_local_inits_st *locals); |
77 | ||
a072ed0c | 78 | static CRYPTO_THREAD_LOCAL threadstopkey; |
b184e3ef | 79 | |
b184e3ef MC |
80 | static void ossl_init_thread_stop_wrap(void *local) |
81 | { | |
82 | ossl_init_thread_stop((struct thread_local_inits_st *)local); | |
83 | } | |
84 | ||
b7326ea7 | 85 | static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc) |
b184e3ef | 86 | { |
a072ed0c MC |
87 | struct thread_local_inits_st *local = |
88 | CRYPTO_THREAD_get_local(&threadstopkey); | |
b184e3ef MC |
89 | |
90 | if (local == NULL && alloc) { | |
91 | local = OPENSSL_zalloc(sizeof *local); | |
a072ed0c | 92 | CRYPTO_THREAD_set_local(&threadstopkey, local); |
b184e3ef | 93 | } |
b7326ea7 | 94 | if (!alloc) { |
a072ed0c | 95 | CRYPTO_THREAD_set_local(&threadstopkey, NULL); |
b7326ea7 | 96 | } |
b184e3ef MC |
97 | |
98 | return local; | |
99 | } | |
100 | ||
7253fd55 | 101 | typedef struct ossl_init_stop_st OPENSSL_INIT_STOP; |
b184e3ef MC |
102 | struct ossl_init_stop_st { |
103 | void (*handler)(void); | |
104 | OPENSSL_INIT_STOP *next; | |
105 | }; | |
106 | ||
107 | static OPENSSL_INIT_STOP *stop_handlers = NULL; | |
c292b105 | 108 | static CRYPTO_RWLOCK *init_lock = NULL; |
b184e3ef | 109 | |
b1f1e7ae | 110 | static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef MC |
111 | static int base_inited = 0; |
112 | static void ossl_init_base(void) | |
113 | { | |
114 | #ifdef OPENSSL_INIT_DEBUG | |
115 | fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n"); | |
116 | #endif | |
a072ed0c MC |
117 | /* |
118 | * We use a dummy thread local key here. We use the destructor to detect | |
119 | * when the thread is going to stop (where that feature is available) | |
120 | */ | |
121 | CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap); | |
c7b7938e | 122 | #ifndef OPENSSL_SYS_UEFI |
f672aee4 | 123 | atexit(OPENSSL_cleanup); |
c7b7938e | 124 | #endif |
c292b105 | 125 | init_lock = CRYPTO_THREAD_lock_new(); |
b184e3ef MC |
126 | OPENSSL_cpuid_setup(); |
127 | base_inited = 1; | |
128 | } | |
129 | ||
b1f1e7ae | 130 | static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef MC |
131 | static int load_crypto_strings_inited = 0; |
132 | static void ossl_init_no_load_crypto_strings(void) | |
133 | { | |
134 | /* Do nothing in this case */ | |
135 | return; | |
136 | } | |
137 | ||
138 | static void ossl_init_load_crypto_strings(void) | |
139 | { | |
498abff0 MC |
140 | /* |
141 | * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time | |
142 | * pulling in all the error strings during static linking | |
143 | */ | |
144 | #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT) | |
b184e3ef MC |
145 | # ifdef OPENSSL_INIT_DEBUG |
146 | fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: " | |
342c21cd | 147 | "int_err_load_crypto_strings()\n"); |
b184e3ef | 148 | # endif |
342c21cd | 149 | int_err_load_crypto_strings(); |
b184e3ef MC |
150 | #endif |
151 | load_crypto_strings_inited = 1; | |
152 | } | |
153 | ||
b1f1e7ae | 154 | static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef MC |
155 | static void ossl_init_add_all_ciphers(void) |
156 | { | |
157 | /* | |
158 | * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time | |
159 | * pulling in all the ciphers during static linking | |
160 | */ | |
161 | #ifndef OPENSSL_NO_AUTOALGINIT | |
162 | # ifdef OPENSSL_INIT_DEBUG | |
163 | fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: " | |
342c21cd | 164 | "int_openssl_add_all_ciphers()\n"); |
b184e3ef | 165 | # endif |
342c21cd | 166 | int_openssl_add_all_ciphers(); |
b184e3ef MC |
167 | # ifndef OPENSSL_NO_ENGINE |
168 | # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV) | |
169 | ENGINE_setup_bsd_cryptodev(); | |
170 | # endif | |
171 | # endif | |
172 | #endif | |
173 | } | |
174 | ||
b1f1e7ae | 175 | static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef MC |
176 | static void ossl_init_add_all_digests(void) |
177 | { | |
178 | /* | |
179 | * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time | |
180 | * pulling in all the ciphers during static linking | |
181 | */ | |
182 | #ifndef OPENSSL_NO_AUTOALGINIT | |
183 | # ifdef OPENSSL_INIT_DEBUG | |
184 | fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: " | |
342c21cd | 185 | "int_openssl_add_all_digests()\n"); |
b184e3ef | 186 | # endif |
342c21cd | 187 | int_openssl_add_all_digests(); |
b184e3ef MC |
188 | # ifndef OPENSSL_NO_ENGINE |
189 | # if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV) | |
190 | ENGINE_setup_bsd_cryptodev(); | |
191 | # endif | |
192 | # endif | |
193 | #endif | |
194 | } | |
195 | ||
196 | static void ossl_init_no_add_algs(void) | |
197 | { | |
198 | /* Do nothing */ | |
199 | return; | |
200 | } | |
201 | ||
b1f1e7ae | 202 | static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef MC |
203 | static int config_inited = 0; |
204 | static const char *config_filename; | |
205 | static void ossl_init_config(void) | |
206 | { | |
207 | #ifdef OPENSSL_INIT_DEBUG | |
208 | fprintf(stderr, | |
342c21cd | 209 | "OPENSSL_INIT: ossl_init_config: int_openssl_config(%s)\n", |
b184e3ef MC |
210 | config_filename==NULL?"NULL":config_filename); |
211 | #endif | |
342c21cd | 212 | int_openssl_config(config_filename); |
b184e3ef MC |
213 | config_inited = 1; |
214 | } | |
215 | static void ossl_init_no_config(void) | |
216 | { | |
217 | #ifdef OPENSSL_INIT_DEBUG | |
218 | fprintf(stderr, | |
342c21cd | 219 | "OPENSSL_INIT: ossl_init_config: int_openssl_no_config()\n"); |
b184e3ef | 220 | #endif |
342c21cd | 221 | int_openssl_no_config(); |
b184e3ef MC |
222 | config_inited = 1; |
223 | } | |
224 | ||
b1f1e7ae | 225 | static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef MC |
226 | static int async_inited = 0; |
227 | static void ossl_init_async(void) | |
228 | { | |
229 | #ifdef OPENSSL_INIT_DEBUG | |
230 | fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n"); | |
231 | #endif | |
b184e3ef | 232 | async_init(); |
b184e3ef MC |
233 | async_inited = 1; |
234 | } | |
235 | ||
236 | #ifndef OPENSSL_NO_ENGINE | |
b1f1e7ae | 237 | static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef MC |
238 | static void ossl_init_engine_openssl(void) |
239 | { | |
240 | # ifdef OPENSSL_INIT_DEBUG | |
241 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: " | |
342c21cd | 242 | "int_engine_load_openssl()\n"); |
b184e3ef | 243 | # endif |
342c21cd | 244 | int_engine_load_openssl(); |
b184e3ef MC |
245 | } |
246 | # if !defined(OPENSSL_NO_HW) && \ | |
247 | (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)) | |
b1f1e7ae | 248 | static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef MC |
249 | static void ossl_init_engine_cryptodev(void) |
250 | { | |
251 | # ifdef OPENSSL_INIT_DEBUG | |
252 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: " | |
342c21cd | 253 | "int_engine_load_cryptodev()\n"); |
b184e3ef | 254 | # endif |
342c21cd | 255 | int_engine_load_cryptodev(); |
b184e3ef MC |
256 | } |
257 | # endif | |
258 | ||
259 | # ifndef OPENSSL_NO_RDRAND | |
b1f1e7ae | 260 | static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef MC |
261 | static void ossl_init_engine_rdrand(void) |
262 | { | |
263 | # ifdef OPENSSL_INIT_DEBUG | |
264 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: " | |
342c21cd | 265 | "int_engine_load_rdrand()\n"); |
b184e3ef | 266 | # endif |
342c21cd | 267 | int_engine_load_rdrand(); |
b184e3ef MC |
268 | } |
269 | # endif | |
b1f1e7ae | 270 | static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef MC |
271 | static void ossl_init_engine_dynamic(void) |
272 | { | |
273 | # ifdef OPENSSL_INIT_DEBUG | |
274 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: " | |
342c21cd | 275 | "int_engine_load_dynamic()\n"); |
b184e3ef | 276 | # endif |
342c21cd | 277 | int_engine_load_dynamic(); |
b184e3ef MC |
278 | } |
279 | # ifndef OPENSSL_NO_STATIC_ENGINE | |
280 | # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK) | |
b1f1e7ae | 281 | static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef MC |
282 | static void ossl_init_engine_padlock(void) |
283 | { | |
284 | # ifdef OPENSSL_INIT_DEBUG | |
285 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: " | |
342c21cd | 286 | "int_engine_load_padlock()\n"); |
b184e3ef | 287 | # endif |
342c21cd | 288 | int_engine_load_padlock(); |
b184e3ef MC |
289 | } |
290 | # endif | |
291 | # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG) | |
b1f1e7ae | 292 | static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef MC |
293 | static void ossl_init_engine_capi(void) |
294 | { | |
295 | # ifdef OPENSSL_INIT_DEBUG | |
296 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: " | |
342c21cd | 297 | "int_engine_load_capi()\n"); |
b184e3ef | 298 | # endif |
342c21cd | 299 | int_engine_load_capi(); |
b184e3ef MC |
300 | } |
301 | # endif | |
b1f1e7ae | 302 | static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT; |
b184e3ef MC |
303 | static void ossl_init_engine_dasync(void) |
304 | { | |
305 | # ifdef OPENSSL_INIT_DEBUG | |
306 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: " | |
342c21cd | 307 | "int_engine_load_dasync()\n"); |
b184e3ef | 308 | # endif |
342c21cd | 309 | int_engine_load_dasync(); |
b184e3ef | 310 | } |
6cba4a66 | 311 | # if !defined(OPENSSL_NO_AFALGENG) |
a4d8bcf1 | 312 | static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT; |
6cba4a66 | 313 | static void ossl_init_engine_afalg(void) |
314 | { | |
315 | # ifdef OPENSSL_INIT_DEBUG | |
316 | fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: " | |
342c21cd | 317 | "int_engine_load_afalg()\n"); |
6cba4a66 | 318 | # endif |
342c21cd | 319 | int_engine_load_afalg(); |
6cba4a66 | 320 | } |
321 | # endif | |
b184e3ef MC |
322 | # endif |
323 | #endif | |
324 | ||
e4ad0763 | 325 | #ifndef OPENSSL_NO_COMP |
b1f1e7ae | 326 | static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT; |
e4ad0763 | 327 | |
b184e3ef MC |
328 | static int zlib_inited = 0; |
329 | static void ossl_init_zlib(void) | |
330 | { | |
331 | /* Do nothing - we need to know about this for the later cleanup */ | |
332 | zlib_inited = 1; | |
333 | } | |
e4ad0763 | 334 | #endif |
b184e3ef | 335 | |
71567a6f | 336 | static void ossl_init_thread_stop(struct thread_local_inits_st *locals) |
b184e3ef MC |
337 | { |
338 | /* Can't do much about this */ | |
339 | if (locals == NULL) | |
340 | return; | |
341 | ||
342 | if (locals->async) { | |
343 | #ifdef OPENSSL_INIT_DEBUG | |
344 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: " | |
345 | "ASYNC_cleanup_thread()\n"); | |
346 | #endif | |
347 | ASYNC_cleanup_thread(); | |
348 | } | |
349 | ||
350 | if (locals->err_state) { | |
351 | #ifdef OPENSSL_INIT_DEBUG | |
352 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: " | |
8509dcc9 | 353 | "ERR_remove_thread_state()\n"); |
b184e3ef | 354 | #endif |
8509dcc9 | 355 | ERR_remove_thread_state(); |
b184e3ef MC |
356 | } |
357 | ||
358 | OPENSSL_free(locals); | |
b184e3ef MC |
359 | } |
360 | ||
f672aee4 | 361 | void OPENSSL_thread_stop(void) |
71567a6f MC |
362 | { |
363 | ossl_init_thread_stop( | |
364 | (struct thread_local_inits_st *)ossl_init_get_thread_local(0)); | |
365 | } | |
366 | ||
b184e3ef MC |
367 | int ossl_init_thread_start(uint64_t opts) |
368 | { | |
369 | struct thread_local_inits_st *locals = ossl_init_get_thread_local(1); | |
370 | ||
371 | if (locals == NULL) | |
372 | return 0; | |
373 | ||
374 | if (opts & OPENSSL_INIT_THREAD_ASYNC) { | |
375 | #ifdef OPENSSL_INIT_DEBUG | |
376 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " | |
377 | "marking thread for async\n"); | |
378 | #endif | |
379 | locals->async = 1; | |
380 | } | |
381 | ||
382 | if (opts & OPENSSL_INIT_THREAD_ERR_STATE) { | |
383 | #ifdef OPENSSL_INIT_DEBUG | |
384 | fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " | |
385 | "marking thread for err_state\n"); | |
386 | #endif | |
387 | locals->err_state = 1; | |
388 | } | |
389 | ||
390 | return 1; | |
391 | } | |
392 | ||
f672aee4 | 393 | void OPENSSL_cleanup(void) |
b184e3ef MC |
394 | { |
395 | OPENSSL_INIT_STOP *currhandler, *lasthandler; | |
396 | ||
deca5df2 MC |
397 | /* If we've not been inited then no need to deinit */ |
398 | if (!base_inited) | |
399 | return; | |
400 | ||
dd27f16e RS |
401 | /* Might be explicitly called and also by atexit */ |
402 | if (stopped) | |
403 | return; | |
404 | stopped = 1; | |
405 | ||
b184e3ef MC |
406 | /* |
407 | * Thread stop may not get automatically called by the thread library for | |
408 | * the very last thread in some situations, so call it directly. | |
409 | */ | |
410 | ossl_init_thread_stop(ossl_init_get_thread_local(0)); | |
411 | ||
412 | currhandler = stop_handlers; | |
413 | while (currhandler != NULL) { | |
414 | currhandler->handler(); | |
415 | lasthandler = currhandler; | |
416 | currhandler = currhandler->next; | |
417 | OPENSSL_free(lasthandler); | |
418 | } | |
419 | stop_handlers = NULL; | |
c292b105 MC |
420 | |
421 | CRYPTO_THREAD_lock_free(init_lock); | |
422 | ||
b184e3ef MC |
423 | /* |
424 | * We assume we are single-threaded for this function, i.e. no race | |
425 | * conditions for the various "*_inited" vars below. | |
426 | */ | |
427 | ||
e4ad0763 | 428 | #ifndef OPENSSL_NO_COMP |
b184e3ef MC |
429 | if (zlib_inited) { |
430 | #ifdef OPENSSL_INIT_DEBUG | |
f672aee4 | 431 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
342c21cd | 432 | "int_comp_zlib_cleanup()\n"); |
b184e3ef | 433 | #endif |
342c21cd | 434 | int_comp_zlib_cleanup(); |
b184e3ef | 435 | } |
e4ad0763 | 436 | #endif |
b184e3ef | 437 | |
ed49f43a MC |
438 | if (async_inited) { |
439 | # ifdef OPENSSL_INIT_DEBUG | |
440 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " | |
441 | "async_deinit()\n"); | |
442 | # endif | |
443 | async_deinit(); | |
444 | } | |
ed49f43a | 445 | |
b184e3ef MC |
446 | if (load_crypto_strings_inited) { |
447 | #ifdef OPENSSL_INIT_DEBUG | |
f672aee4 | 448 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
342c21cd | 449 | "int_err_free_strings()\n"); |
b184e3ef | 450 | #endif |
342c21cd | 451 | int_err_free_strings(); |
b184e3ef MC |
452 | } |
453 | ||
a072ed0c | 454 | CRYPTO_THREAD_cleanup_local(&threadstopkey); |
6bc7bad0 | 455 | |
b184e3ef | 456 | #ifdef OPENSSL_INIT_DEBUG |
58a8fc25 | 457 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
342c21cd | 458 | "int_rand_cleanup()\n"); |
58a8fc25 | 459 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
342c21cd | 460 | "int_conf_modules_free()\n"); |
9749a07a | 461 | #ifndef OPENSSL_NO_ENGINE |
ae6412f3 | 462 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
342c21cd | 463 | "int_engine_cleanup()\n"); |
9749a07a | 464 | #endif |
58a8fc25 | 465 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
342c21cd | 466 | "int_crypto_cleanup_all_ex_data()\n"); |
58a8fc25 | 467 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
342c21cd | 468 | "int_bio_sock_cleanup()\n"); |
58a8fc25 | 469 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
342c21cd | 470 | "int_evp_cleanup()\n"); |
58a8fc25 | 471 | fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: " |
342c21cd | 472 | "int_obj_cleanup()\n"); |
9749a07a | 473 | #endif |
58a8fc25 MC |
474 | /* |
475 | * Note that cleanup order is important: | |
f3cd81d6 | 476 | * - rand_cleanup_intern could call an ENINGE's RAND cleanup function so |
342c21cd | 477 | * must be called before int_engine_cleanup() |
58a8fc25 MC |
478 | * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up |
479 | * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data(). | |
342c21cd MC |
480 | * - int_conf_modules_free() can end up in ENGINE code so must be called |
481 | * before int_engine_cleanup() | |
58a8fc25 | 482 | */ |
342c21cd MC |
483 | int_rand_cleanup(); |
484 | int_conf_modules_free(); | |
773fd0ba | 485 | #ifndef OPENSSL_NO_ENGINE |
342c21cd | 486 | int_engine_cleanup(); |
773fd0ba | 487 | #endif |
342c21cd | 488 | int_crypto_cleanup_all_ex_data(); |
f9e55034 | 489 | #ifndef OPENSSL_NO_SOCK |
342c21cd | 490 | int_bio_sock_cleanup(); |
f9e55034 | 491 | #endif |
342c21cd MC |
492 | int_evp_cleanup(); |
493 | int_obj_cleanup(); | |
deca5df2 | 494 | base_inited = 0; |
b184e3ef MC |
495 | } |
496 | ||
b184e3ef MC |
497 | /* |
498 | * If this function is called with a non NULL settings value then it must be | |
499 | * called prior to any threads making calls to any OpenSSL functions, | |
500 | * i.e. passing a non-null settings value is assumed to be single-threaded. | |
501 | */ | |
0fc32b07 | 502 | int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) |
b184e3ef | 503 | { |
302f7588 MC |
504 | static int stoperrset = 0; |
505 | ||
506 | if (stopped) { | |
507 | if (!stoperrset) { | |
508 | /* | |
509 | * We only ever set this once to avoid getting into an infinite | |
510 | * loop where the error system keeps trying to init and fails so | |
511 | * sets an error etc | |
512 | */ | |
513 | stoperrset = 1; | |
a4625290 | 514 | CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL); |
302f7588 | 515 | } |
0fc32b07 | 516 | return 0; |
302f7588 | 517 | } |
dd27f16e | 518 | |
b1f1e7ae MC |
519 | if (!CRYPTO_THREAD_run_once(&base, ossl_init_base)) |
520 | return 0; | |
b184e3ef | 521 | |
b1f1e7ae MC |
522 | if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS) |
523 | && !CRYPTO_THREAD_run_once(&load_crypto_strings, | |
524 | ossl_init_no_load_crypto_strings)) | |
525 | return 0; | |
b184e3ef | 526 | |
b1f1e7ae MC |
527 | if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS) |
528 | && !CRYPTO_THREAD_run_once(&load_crypto_strings, | |
529 | ossl_init_load_crypto_strings)) | |
530 | return 0; | |
b184e3ef | 531 | |
b1f1e7ae MC |
532 | if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS) |
533 | && !CRYPTO_THREAD_run_once(&add_all_ciphers, ossl_init_no_add_algs)) | |
534 | return 0; | |
b184e3ef | 535 | |
b1f1e7ae MC |
536 | if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS) |
537 | && !CRYPTO_THREAD_run_once(&add_all_ciphers, | |
538 | ossl_init_add_all_ciphers)) | |
539 | return 0; | |
b184e3ef | 540 | |
b1f1e7ae MC |
541 | if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS) |
542 | && !CRYPTO_THREAD_run_once(&add_all_digests, ossl_init_no_add_algs)) | |
543 | return 0; | |
b184e3ef | 544 | |
b1f1e7ae MC |
545 | if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS) |
546 | && !CRYPTO_THREAD_run_once(&add_all_digests, | |
547 | ossl_init_add_all_digests)) | |
548 | return 0; | |
b184e3ef | 549 | |
b1f1e7ae MC |
550 | if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) |
551 | && !CRYPTO_THREAD_run_once(&config, ossl_init_no_config)) | |
552 | return 0; | |
b184e3ef MC |
553 | |
554 | if (opts & OPENSSL_INIT_LOAD_CONFIG) { | |
b1f1e7ae | 555 | int ret; |
c292b105 | 556 | CRYPTO_THREAD_write_lock(init_lock); |
7253fd55 | 557 | config_filename = (settings == NULL) ? NULL : settings->config_name; |
b1f1e7ae | 558 | ret = CRYPTO_THREAD_run_once(&config, ossl_init_config); |
c292b105 | 559 | CRYPTO_THREAD_unlock(init_lock); |
b1f1e7ae MC |
560 | if (!ret) |
561 | return 0; | |
b184e3ef MC |
562 | } |
563 | ||
b1f1e7ae MC |
564 | if ((opts & OPENSSL_INIT_ASYNC) |
565 | && !CRYPTO_THREAD_run_once(&async, ossl_init_async)) | |
566 | return 0; | |
7626fbf2 | 567 | |
b184e3ef | 568 | #ifndef OPENSSL_NO_ENGINE |
b1f1e7ae MC |
569 | if ((opts & OPENSSL_INIT_ENGINE_OPENSSL) |
570 | && !CRYPTO_THREAD_run_once(&engine_openssl, | |
571 | ossl_init_engine_openssl)) | |
572 | return 0; | |
b184e3ef MC |
573 | # if !defined(OPENSSL_NO_HW) && \ |
574 | (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)) | |
b1f1e7ae MC |
575 | if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV) |
576 | && !CRYPTO_THREAD_run_once(&engine_cryptodev, | |
577 | ossl_init_engine_cryptodev)) | |
578 | return 0; | |
b184e3ef MC |
579 | # endif |
580 | # ifndef OPENSSL_NO_RDRAND | |
b1f1e7ae MC |
581 | if ((opts & OPENSSL_INIT_ENGINE_RDRAND) |
582 | && !CRYPTO_THREAD_run_once(&engine_rdrand, ossl_init_engine_rdrand)) | |
583 | return 0; | |
b184e3ef | 584 | # endif |
b1f1e7ae MC |
585 | if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC) |
586 | && !CRYPTO_THREAD_run_once(&engine_dynamic, | |
587 | ossl_init_engine_dynamic)) | |
588 | return 0; | |
b184e3ef MC |
589 | # ifndef OPENSSL_NO_STATIC_ENGINE |
590 | # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK) | |
b1f1e7ae | 591 | if ((opts & OPENSSL_INIT_ENGINE_PADLOCK) |
3ddd1d04 EK |
592 | && !CRYPTO_THREAD_run_once(&engine_padlock, |
593 | ossl_init_engine_padlock)) | |
b1f1e7ae | 594 | return 0; |
b184e3ef MC |
595 | # endif |
596 | # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG) | |
b1f1e7ae | 597 | if ((opts & OPENSSL_INIT_ENGINE_CAPI) |
3ddd1d04 | 598 | && !CRYPTO_THREAD_run_once(&engine_capi, ossl_init_engine_capi)) |
b1f1e7ae | 599 | return 0; |
b184e3ef | 600 | # endif |
b1f1e7ae MC |
601 | if ((opts & OPENSSL_INIT_ENGINE_DASYNC) |
602 | && !CRYPTO_THREAD_run_once(&engine_dasync, ossl_init_engine_dasync)) | |
603 | return 0; | |
6cba4a66 | 604 | # if !defined(OPENSSL_NO_AFALGENG) |
b1f1e7ae MC |
605 | if ((opts & OPENSSL_INIT_ENGINE_AFALG) |
606 | && !CRYPTO_THREAD_run_once(&engine_afalg, ossl_init_engine_afalg)) | |
607 | return 0; | |
6cba4a66 | 608 | # endif |
b184e3ef MC |
609 | # endif |
610 | if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN | |
6cba4a66 | 611 | | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL |
612 | | OPENSSL_INIT_ENGINE_AFALG)) { | |
b184e3ef MC |
613 | ENGINE_register_all_complete(); |
614 | } | |
615 | #endif | |
616 | ||
e4ad0763 | 617 | #ifndef OPENSSL_NO_COMP |
b1f1e7ae | 618 | if ((opts & OPENSSL_INIT_ZLIB) |
3ddd1d04 | 619 | && !CRYPTO_THREAD_run_once(&zlib, ossl_init_zlib)) |
b1f1e7ae | 620 | return 0; |
e4ad0763 | 621 | #endif |
0fc32b07 MC |
622 | |
623 | return 1; | |
b184e3ef MC |
624 | } |
625 | ||
f672aee4 | 626 | int OPENSSL_atexit(void (*handler)(void)) |
b184e3ef MC |
627 | { |
628 | OPENSSL_INIT_STOP *newhand; | |
629 | ||
630 | newhand = OPENSSL_malloc(sizeof(*newhand)); | |
631 | if (newhand == NULL) | |
632 | return 0; | |
633 | ||
634 | newhand->handler = handler; | |
635 | newhand->next = stop_handlers; | |
636 | stop_handlers = newhand; | |
637 | ||
638 | return 1; | |
639 | } |