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