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