1 | /*
|
---|
2 | * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
5 | * this file except in compliance with the License. You can obtain a copy
|
---|
6 | * in the file LICENSE in the source distribution or at
|
---|
7 | * https://www.openssl.org/source/license.html
|
---|
8 | */
|
---|
9 |
|
---|
10 | /* We need to use some engine deprecated APIs */
|
---|
11 | #define OPENSSL_SUPPRESS_DEPRECATED
|
---|
12 |
|
---|
13 | #include "e_os.h"
|
---|
14 | #include "crypto/cryptlib.h"
|
---|
15 | #include <openssl/err.h>
|
---|
16 | #include "crypto/rand.h"
|
---|
17 | #include "internal/bio.h"
|
---|
18 | #include <openssl/evp.h>
|
---|
19 | #include "crypto/evp.h"
|
---|
20 | #include "internal/conf.h"
|
---|
21 | #include "crypto/async.h"
|
---|
22 | #include "crypto/engine.h"
|
---|
23 | #include "internal/comp.h"
|
---|
24 | #include "internal/err.h"
|
---|
25 | #include "crypto/err.h"
|
---|
26 | #include "crypto/objects.h"
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <assert.h>
|
---|
29 | #include "internal/thread_once.h"
|
---|
30 | #include "crypto/dso_conf.h"
|
---|
31 | #include "internal/dso.h"
|
---|
32 | #include "crypto/store.h"
|
---|
33 | #include <openssl/cmp_util.h> /* for OSSL_CMP_log_close() */
|
---|
34 | #include <openssl/trace.h>
|
---|
35 | #include "crypto/ctype.h"
|
---|
36 |
|
---|
37 | static int stopped = 0;
|
---|
38 | static uint64_t optsdone = 0;
|
---|
39 |
|
---|
40 | typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
|
---|
41 | struct ossl_init_stop_st {
|
---|
42 | void (*handler)(void);
|
---|
43 | OPENSSL_INIT_STOP *next;
|
---|
44 | };
|
---|
45 |
|
---|
46 | static OPENSSL_INIT_STOP *stop_handlers = NULL;
|
---|
47 | static CRYPTO_RWLOCK *init_lock = NULL;
|
---|
48 | static CRYPTO_THREAD_LOCAL in_init_config_local;
|
---|
49 |
|
---|
50 | static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
|
---|
51 | static int base_inited = 0;
|
---|
52 | DEFINE_RUN_ONCE_STATIC(ossl_init_base)
|
---|
53 | {
|
---|
54 | /* no need to init trace */
|
---|
55 |
|
---|
56 | OSSL_TRACE(INIT, "ossl_init_base: setting up stop handlers\n");
|
---|
57 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG
|
---|
58 | ossl_malloc_setup_failures();
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
|
---|
62 | goto err;
|
---|
63 | OPENSSL_cpuid_setup();
|
---|
64 |
|
---|
65 | if (!ossl_init_thread())
|
---|
66 | goto err;
|
---|
67 |
|
---|
68 | if (!CRYPTO_THREAD_init_local(&in_init_config_local, NULL))
|
---|
69 | goto err;
|
---|
70 |
|
---|
71 | base_inited = 1;
|
---|
72 | return 1;
|
---|
73 |
|
---|
74 | err:
|
---|
75 | OSSL_TRACE(INIT, "ossl_init_base failed!\n");
|
---|
76 | CRYPTO_THREAD_lock_free(init_lock);
|
---|
77 | init_lock = NULL;
|
---|
78 |
|
---|
79 | return 0;
|
---|
80 | }
|
---|
81 |
|
---|
82 | static CRYPTO_ONCE register_atexit = CRYPTO_ONCE_STATIC_INIT;
|
---|
83 | #if !defined(OPENSSL_SYS_UEFI) && defined(_WIN32)
|
---|
84 | static int win32atexit(void)
|
---|
85 | {
|
---|
86 | OPENSSL_cleanup();
|
---|
87 | return 0;
|
---|
88 | }
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | DEFINE_RUN_ONCE_STATIC(ossl_init_register_atexit)
|
---|
92 | {
|
---|
93 | #ifdef OPENSSL_INIT_DEBUG
|
---|
94 | fprintf(stderr, "OPENSSL_INIT: ossl_init_register_atexit()\n");
|
---|
95 | #endif
|
---|
96 | #ifndef OPENSSL_SYS_UEFI
|
---|
97 | # if defined(_WIN32) && !defined(__BORLANDC__)
|
---|
98 | /* We use _onexit() in preference because it gets called on DLL unload */
|
---|
99 | if (_onexit(win32atexit) == NULL)
|
---|
100 | return 0;
|
---|
101 | # else
|
---|
102 | if (atexit(OPENSSL_cleanup) != 0)
|
---|
103 | return 0;
|
---|
104 | # endif
|
---|
105 | #endif
|
---|
106 |
|
---|
107 | return 1;
|
---|
108 | }
|
---|
109 |
|
---|
110 | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_register_atexit,
|
---|
111 | ossl_init_register_atexit)
|
---|
112 | {
|
---|
113 | #ifdef OPENSSL_INIT_DEBUG
|
---|
114 | fprintf(stderr, "OPENSSL_INIT: ossl_init_no_register_atexit ok!\n");
|
---|
115 | #endif
|
---|
116 | /* Do nothing in this case */
|
---|
117 | return 1;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static CRYPTO_ONCE load_crypto_nodelete = CRYPTO_ONCE_STATIC_INIT;
|
---|
121 | DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_nodelete)
|
---|
122 | {
|
---|
123 | OSSL_TRACE(INIT, "ossl_init_load_crypto_nodelete()\n");
|
---|
124 |
|
---|
125 | #if !defined(OPENSSL_USE_NODELETE) \
|
---|
126 | && !defined(OPENSSL_NO_PINSHARED)
|
---|
127 | # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
|
---|
128 | {
|
---|
129 | HMODULE handle = NULL;
|
---|
130 | BOOL ret;
|
---|
131 |
|
---|
132 | /* We don't use the DSO route for WIN32 because there is a better way */
|
---|
133 | ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
|
---|
134 | | GET_MODULE_HANDLE_EX_FLAG_PIN,
|
---|
135 | (void *)&base_inited, &handle);
|
---|
136 |
|
---|
137 | OSSL_TRACE1(INIT,
|
---|
138 | "ossl_init_load_crypto_nodelete: "
|
---|
139 | "obtained DSO reference? %s\n",
|
---|
140 | (ret == TRUE ? "No!" : "Yes."));
|
---|
141 | return (ret == TRUE) ? 1 : 0;
|
---|
142 | }
|
---|
143 | # elif !defined(DSO_NONE)
|
---|
144 | /*
|
---|
145 | * Deliberately leak a reference to ourselves. This will force the library
|
---|
146 | * to remain loaded until the atexit() handler is run at process exit.
|
---|
147 | */
|
---|
148 | {
|
---|
149 | DSO *dso;
|
---|
150 | void *err;
|
---|
151 |
|
---|
152 | if (!err_shelve_state(&err))
|
---|
153 | return 0;
|
---|
154 |
|
---|
155 | dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
|
---|
156 | /*
|
---|
157 | * In case of No!, it is uncertain our exit()-handlers can still be
|
---|
158 | * called. After dlclose() the whole library might have been unloaded
|
---|
159 | * already.
|
---|
160 | */
|
---|
161 | OSSL_TRACE1(INIT, "obtained DSO reference? %s\n",
|
---|
162 | (dso == NULL ? "No!" : "Yes."));
|
---|
163 | DSO_free(dso);
|
---|
164 | err_unshelve_state(err);
|
---|
165 | }
|
---|
166 | # endif
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | return 1;
|
---|
170 | }
|
---|
171 |
|
---|
172 | static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
|
---|
173 | static int load_crypto_strings_inited = 0;
|
---|
174 | DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
|
---|
175 | {
|
---|
176 | int ret = 1;
|
---|
177 | /*
|
---|
178 | * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
|
---|
179 | * pulling in all the error strings during static linking
|
---|
180 | */
|
---|
181 | #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
|
---|
182 | OSSL_TRACE(INIT, "ossl_err_load_crypto_strings()\n");
|
---|
183 | ret = ossl_err_load_crypto_strings();
|
---|
184 | load_crypto_strings_inited = 1;
|
---|
185 | #endif
|
---|
186 | return ret;
|
---|
187 | }
|
---|
188 |
|
---|
189 | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_crypto_strings,
|
---|
190 | ossl_init_load_crypto_strings)
|
---|
191 | {
|
---|
192 | /* Do nothing in this case */
|
---|
193 | return 1;
|
---|
194 | }
|
---|
195 |
|
---|
196 | static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
|
---|
197 | DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
|
---|
198 | {
|
---|
199 | /*
|
---|
200 | * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
|
---|
201 | * pulling in all the ciphers during static linking
|
---|
202 | */
|
---|
203 | #ifndef OPENSSL_NO_AUTOALGINIT
|
---|
204 | OSSL_TRACE(INIT, "openssl_add_all_ciphers_int()\n");
|
---|
205 | openssl_add_all_ciphers_int();
|
---|
206 | #endif
|
---|
207 | return 1;
|
---|
208 | }
|
---|
209 |
|
---|
210 | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_ciphers,
|
---|
211 | ossl_init_add_all_ciphers)
|
---|
212 | {
|
---|
213 | /* Do nothing */
|
---|
214 | return 1;
|
---|
215 | }
|
---|
216 |
|
---|
217 | static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
|
---|
218 | DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
|
---|
219 | {
|
---|
220 | /*
|
---|
221 | * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
|
---|
222 | * pulling in all the ciphers during static linking
|
---|
223 | */
|
---|
224 | #ifndef OPENSSL_NO_AUTOALGINIT
|
---|
225 | OSSL_TRACE(INIT, "openssl_add_all_digests()\n");
|
---|
226 | openssl_add_all_digests_int();
|
---|
227 | #endif
|
---|
228 | return 1;
|
---|
229 | }
|
---|
230 |
|
---|
231 | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_add_all_digests,
|
---|
232 | ossl_init_add_all_digests)
|
---|
233 | {
|
---|
234 | /* Do nothing */
|
---|
235 | return 1;
|
---|
236 | }
|
---|
237 |
|
---|
238 | static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
|
---|
239 | static int config_inited = 0;
|
---|
240 | static const OPENSSL_INIT_SETTINGS *conf_settings = NULL;
|
---|
241 | DEFINE_RUN_ONCE_STATIC(ossl_init_config)
|
---|
242 | {
|
---|
243 | int ret = ossl_config_int(NULL);
|
---|
244 |
|
---|
245 | config_inited = 1;
|
---|
246 | return ret;
|
---|
247 | }
|
---|
248 | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_config_settings, ossl_init_config)
|
---|
249 | {
|
---|
250 | int ret = ossl_config_int(conf_settings);
|
---|
251 |
|
---|
252 | config_inited = 1;
|
---|
253 | return ret;
|
---|
254 | }
|
---|
255 | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_config, ossl_init_config)
|
---|
256 | {
|
---|
257 | OSSL_TRACE(INIT, "ossl_no_config_int()\n");
|
---|
258 | ossl_no_config_int();
|
---|
259 | config_inited = 1;
|
---|
260 | return 1;
|
---|
261 | }
|
---|
262 |
|
---|
263 | static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
|
---|
264 | static int async_inited = 0;
|
---|
265 | DEFINE_RUN_ONCE_STATIC(ossl_init_async)
|
---|
266 | {
|
---|
267 | OSSL_TRACE(INIT, "async_init()\n");
|
---|
268 | if (!async_init())
|
---|
269 | return 0;
|
---|
270 | async_inited = 1;
|
---|
271 | return 1;
|
---|
272 | }
|
---|
273 |
|
---|
274 | #ifndef OPENSSL_NO_ENGINE
|
---|
275 | static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
|
---|
276 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
|
---|
277 | {
|
---|
278 | OSSL_TRACE(INIT, "engine_load_openssl_int()\n");
|
---|
279 | engine_load_openssl_int();
|
---|
280 | return 1;
|
---|
281 | }
|
---|
282 | # ifndef OPENSSL_NO_RDRAND
|
---|
283 | static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
|
---|
284 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
|
---|
285 | {
|
---|
286 | OSSL_TRACE(INIT, "engine_load_rdrand_int()\n");
|
---|
287 | engine_load_rdrand_int();
|
---|
288 | return 1;
|
---|
289 | }
|
---|
290 | # endif
|
---|
291 | static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
|
---|
292 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
|
---|
293 | {
|
---|
294 | OSSL_TRACE(INIT, "engine_load_dynamic_int()\n");
|
---|
295 | engine_load_dynamic_int();
|
---|
296 | return 1;
|
---|
297 | }
|
---|
298 | # ifndef OPENSSL_NO_STATIC_ENGINE
|
---|
299 | # ifndef OPENSSL_NO_DEVCRYPTOENG
|
---|
300 | static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
|
---|
301 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
|
---|
302 | {
|
---|
303 | OSSL_TRACE(INIT, "engine_load_devcrypto_int()\n");
|
---|
304 | engine_load_devcrypto_int();
|
---|
305 | return 1;
|
---|
306 | }
|
---|
307 | # endif
|
---|
308 | # if !defined(OPENSSL_NO_PADLOCKENG)
|
---|
309 | static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
|
---|
310 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
|
---|
311 | {
|
---|
312 | OSSL_TRACE(INIT, "engine_load_padlock_int()\n");
|
---|
313 | engine_load_padlock_int();
|
---|
314 | return 1;
|
---|
315 | }
|
---|
316 | # endif
|
---|
317 | # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
|
---|
318 | static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
|
---|
319 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
|
---|
320 | {
|
---|
321 | OSSL_TRACE(INIT, "engine_load_capi_int()\n");
|
---|
322 | engine_load_capi_int();
|
---|
323 | return 1;
|
---|
324 | }
|
---|
325 | # endif
|
---|
326 | # if !defined(OPENSSL_NO_AFALGENG)
|
---|
327 | static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
|
---|
328 | DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
|
---|
329 | {
|
---|
330 | OSSL_TRACE(INIT, "engine_load_afalg_int()\n");
|
---|
331 | engine_load_afalg_int();
|
---|
332 | return 1;
|
---|
333 | }
|
---|
334 | # endif
|
---|
335 | # endif
|
---|
336 | #endif
|
---|
337 |
|
---|
338 | void OPENSSL_cleanup(void)
|
---|
339 | {
|
---|
340 | OPENSSL_INIT_STOP *currhandler, *lasthandler;
|
---|
341 |
|
---|
342 | /*
|
---|
343 | * At some point we should consider looking at this function with a view to
|
---|
344 | * moving most/all of this into onfree handlers in OSSL_LIB_CTX.
|
---|
345 | */
|
---|
346 |
|
---|
347 | /* If we've not been inited then no need to deinit */
|
---|
348 | if (!base_inited)
|
---|
349 | return;
|
---|
350 |
|
---|
351 | /* Might be explicitly called and also by atexit */
|
---|
352 | if (stopped)
|
---|
353 | return;
|
---|
354 | stopped = 1;
|
---|
355 |
|
---|
356 | /*
|
---|
357 | * Thread stop may not get automatically called by the thread library for
|
---|
358 | * the very last thread in some situations, so call it directly.
|
---|
359 | */
|
---|
360 | OPENSSL_thread_stop();
|
---|
361 |
|
---|
362 | currhandler = stop_handlers;
|
---|
363 | while (currhandler != NULL) {
|
---|
364 | currhandler->handler();
|
---|
365 | lasthandler = currhandler;
|
---|
366 | currhandler = currhandler->next;
|
---|
367 | OPENSSL_free(lasthandler);
|
---|
368 | }
|
---|
369 | stop_handlers = NULL;
|
---|
370 |
|
---|
371 | CRYPTO_THREAD_lock_free(init_lock);
|
---|
372 | init_lock = NULL;
|
---|
373 |
|
---|
374 | CRYPTO_THREAD_cleanup_local(&in_init_config_local);
|
---|
375 |
|
---|
376 | /*
|
---|
377 | * We assume we are single-threaded for this function, i.e. no race
|
---|
378 | * conditions for the various "*_inited" vars below.
|
---|
379 | */
|
---|
380 |
|
---|
381 | #ifndef OPENSSL_NO_COMP
|
---|
382 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_comp_zlib_cleanup()\n");
|
---|
383 | ossl_comp_zlib_cleanup();
|
---|
384 | #endif
|
---|
385 |
|
---|
386 | if (async_inited) {
|
---|
387 | OSSL_TRACE(INIT, "OPENSSL_cleanup: async_deinit()\n");
|
---|
388 | async_deinit();
|
---|
389 | }
|
---|
390 |
|
---|
391 | if (load_crypto_strings_inited) {
|
---|
392 | OSSL_TRACE(INIT, "OPENSSL_cleanup: err_free_strings_int()\n");
|
---|
393 | err_free_strings_int();
|
---|
394 | }
|
---|
395 |
|
---|
396 | /*
|
---|
397 | * Note that cleanup order is important:
|
---|
398 | * - ossl_rand_cleanup_int could call an ENGINE's RAND cleanup function so
|
---|
399 | * must be called before engine_cleanup_int()
|
---|
400 | * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
|
---|
401 | * before the ex data handlers are wiped during default ossl_lib_ctx deinit.
|
---|
402 | * - ossl_config_modules_free() can end up in ENGINE code so must be called
|
---|
403 | * before engine_cleanup_int()
|
---|
404 | * - ENGINEs and additional EVP algorithms might use added OIDs names so
|
---|
405 | * ossl_obj_cleanup_int() must be called last
|
---|
406 | */
|
---|
407 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_rand_cleanup_int()\n");
|
---|
408 | ossl_rand_cleanup_int();
|
---|
409 |
|
---|
410 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_config_modules_free()\n");
|
---|
411 | ossl_config_modules_free();
|
---|
412 |
|
---|
413 | #ifndef OPENSSL_NO_ENGINE
|
---|
414 | OSSL_TRACE(INIT, "OPENSSL_cleanup: engine_cleanup_int()\n");
|
---|
415 | engine_cleanup_int();
|
---|
416 | #endif
|
---|
417 |
|
---|
418 | #ifndef OPENSSL_NO_DEPRECATED_3_0
|
---|
419 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_store_cleanup_int()\n");
|
---|
420 | ossl_store_cleanup_int();
|
---|
421 | #endif
|
---|
422 |
|
---|
423 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_lib_ctx_default_deinit()\n");
|
---|
424 | ossl_lib_ctx_default_deinit();
|
---|
425 |
|
---|
426 | ossl_cleanup_thread();
|
---|
427 |
|
---|
428 | OSSL_TRACE(INIT, "OPENSSL_cleanup: bio_cleanup()\n");
|
---|
429 | bio_cleanup();
|
---|
430 |
|
---|
431 | OSSL_TRACE(INIT, "OPENSSL_cleanup: evp_cleanup_int()\n");
|
---|
432 | evp_cleanup_int();
|
---|
433 |
|
---|
434 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_obj_cleanup_int()\n");
|
---|
435 | ossl_obj_cleanup_int();
|
---|
436 |
|
---|
437 | OSSL_TRACE(INIT, "OPENSSL_cleanup: err_int()\n");
|
---|
438 | err_cleanup();
|
---|
439 |
|
---|
440 | OSSL_TRACE(INIT, "OPENSSL_cleanup: CRYPTO_secure_malloc_done()\n");
|
---|
441 | CRYPTO_secure_malloc_done();
|
---|
442 |
|
---|
443 | #ifndef OPENSSL_NO_CMP
|
---|
444 | OSSL_TRACE(INIT, "OPENSSL_cleanup: OSSL_CMP_log_close()\n");
|
---|
445 | OSSL_CMP_log_close();
|
---|
446 | #endif
|
---|
447 |
|
---|
448 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_trace_cleanup()\n");
|
---|
449 | ossl_trace_cleanup();
|
---|
450 |
|
---|
451 | OSSL_TRACE(INIT, "OPENSSL_cleanup: ossl_deinit_casecmp()\n");
|
---|
452 | ossl_deinit_casecmp();
|
---|
453 |
|
---|
454 | base_inited = 0;
|
---|
455 | }
|
---|
456 |
|
---|
457 | /*
|
---|
458 | * If this function is called with a non NULL settings value then it must be
|
---|
459 | * called prior to any threads making calls to any OpenSSL functions,
|
---|
460 | * i.e. passing a non-null settings value is assumed to be single-threaded.
|
---|
461 | */
|
---|
462 | int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
|
---|
463 | {
|
---|
464 | uint64_t tmp;
|
---|
465 | int aloaddone = 0;
|
---|
466 |
|
---|
467 | if (!ossl_init_casecmp())
|
---|
468 | return 0;
|
---|
469 |
|
---|
470 | /* Applications depend on 0 being returned when cleanup was already done */
|
---|
471 | if (stopped) {
|
---|
472 | if (!(opts & OPENSSL_INIT_BASE_ONLY))
|
---|
473 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL);
|
---|
474 | return 0;
|
---|
475 | }
|
---|
476 |
|
---|
477 | /*
|
---|
478 | * We ignore failures from this function. It is probably because we are
|
---|
479 | * on a platform that doesn't support lockless atomic loads (we may not
|
---|
480 | * have created init_lock yet so we can't use it). This is just an
|
---|
481 | * optimisation to skip the full checks in this function if we don't need
|
---|
482 | * to, so we carry on regardless in the event of failure.
|
---|
483 | *
|
---|
484 | * There could be a race here with other threads, so that optsdone has not
|
---|
485 | * been updated yet, even though the options have in fact been initialised.
|
---|
486 | * This doesn't matter - it just means we will run the full function
|
---|
487 | * unnecessarily - but all the critical code is contained in RUN_ONCE
|
---|
488 | * functions anyway so we are safe.
|
---|
489 | */
|
---|
490 | if (CRYPTO_atomic_load(&optsdone, &tmp, NULL)) {
|
---|
491 | if ((tmp & opts) == opts)
|
---|
492 | return 1;
|
---|
493 | aloaddone = 1;
|
---|
494 | }
|
---|
495 |
|
---|
496 | /*
|
---|
497 | * At some point we should look at this function with a view to moving
|
---|
498 | * most/all of this into OSSL_LIB_CTX.
|
---|
499 | *
|
---|
500 | * When the caller specifies OPENSSL_INIT_BASE_ONLY, that should be the
|
---|
501 | * *only* option specified. With that option we return immediately after
|
---|
502 | * doing the requested limited initialization. Note that
|
---|
503 | * err_shelve_state() called by us via ossl_init_load_crypto_nodelete()
|
---|
504 | * re-enters OPENSSL_init_crypto() with OPENSSL_INIT_BASE_ONLY, but with
|
---|
505 | * base already initialized this is a harmless NOOP.
|
---|
506 | *
|
---|
507 | * If we remain the only caller of err_shelve_state() the recursion should
|
---|
508 | * perhaps be removed, but if in doubt, it can be left in place.
|
---|
509 | */
|
---|
510 | if (!RUN_ONCE(&base, ossl_init_base))
|
---|
511 | return 0;
|
---|
512 |
|
---|
513 | if (opts & OPENSSL_INIT_BASE_ONLY)
|
---|
514 | return 1;
|
---|
515 |
|
---|
516 | /*
|
---|
517 | * init_lock should definitely be set up now, so we can now repeat the
|
---|
518 | * same check from above but be sure that it will work even on platforms
|
---|
519 | * without lockless CRYPTO_atomic_load
|
---|
520 | */
|
---|
521 | if (!aloaddone) {
|
---|
522 | if (!CRYPTO_atomic_load(&optsdone, &tmp, init_lock))
|
---|
523 | return 0;
|
---|
524 | if ((tmp & opts) == opts)
|
---|
525 | return 1;
|
---|
526 | }
|
---|
527 |
|
---|
528 | /*
|
---|
529 | * Now we don't always set up exit handlers, the INIT_BASE_ONLY calls
|
---|
530 | * should not have the side-effect of setting up exit handlers, and
|
---|
531 | * therefore, this code block is below the INIT_BASE_ONLY-conditioned early
|
---|
532 | * return above.
|
---|
533 | */
|
---|
534 | if ((opts & OPENSSL_INIT_NO_ATEXIT) != 0) {
|
---|
535 | if (!RUN_ONCE_ALT(®ister_atexit, ossl_init_no_register_atexit,
|
---|
536 | ossl_init_register_atexit))
|
---|
537 | return 0;
|
---|
538 | } else if (!RUN_ONCE(®ister_atexit, ossl_init_register_atexit)) {
|
---|
539 | return 0;
|
---|
540 | }
|
---|
541 |
|
---|
542 | if (!RUN_ONCE(&load_crypto_nodelete, ossl_init_load_crypto_nodelete))
|
---|
543 | return 0;
|
---|
544 |
|
---|
545 | if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
|
---|
546 | && !RUN_ONCE_ALT(&load_crypto_strings,
|
---|
547 | ossl_init_no_load_crypto_strings,
|
---|
548 | ossl_init_load_crypto_strings))
|
---|
549 | return 0;
|
---|
550 |
|
---|
551 | if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
|
---|
552 | && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
|
---|
553 | return 0;
|
---|
554 |
|
---|
555 | if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
|
---|
556 | && !RUN_ONCE_ALT(&add_all_ciphers, ossl_init_no_add_all_ciphers,
|
---|
557 | ossl_init_add_all_ciphers))
|
---|
558 | return 0;
|
---|
559 |
|
---|
560 | if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
|
---|
561 | && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
|
---|
562 | return 0;
|
---|
563 |
|
---|
564 | if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
|
---|
565 | && !RUN_ONCE_ALT(&add_all_digests, ossl_init_no_add_all_digests,
|
---|
566 | ossl_init_add_all_digests))
|
---|
567 | return 0;
|
---|
568 |
|
---|
569 | if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
|
---|
570 | && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
|
---|
571 | return 0;
|
---|
572 |
|
---|
573 | if ((opts & OPENSSL_INIT_ATFORK)
|
---|
574 | && !openssl_init_fork_handlers())
|
---|
575 | return 0;
|
---|
576 |
|
---|
577 | if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
|
---|
578 | && !RUN_ONCE_ALT(&config, ossl_init_no_config, ossl_init_config))
|
---|
579 | return 0;
|
---|
580 |
|
---|
581 | if (opts & OPENSSL_INIT_LOAD_CONFIG) {
|
---|
582 | int loading = CRYPTO_THREAD_get_local(&in_init_config_local) != NULL;
|
---|
583 |
|
---|
584 | /* If called recursively from OBJ_ calls, just skip it. */
|
---|
585 | if (!loading) {
|
---|
586 | int ret;
|
---|
587 |
|
---|
588 | if (!CRYPTO_THREAD_set_local(&in_init_config_local, (void *)-1))
|
---|
589 | return 0;
|
---|
590 | if (settings == NULL) {
|
---|
591 | ret = RUN_ONCE(&config, ossl_init_config);
|
---|
592 | } else {
|
---|
593 | if (!CRYPTO_THREAD_write_lock(init_lock))
|
---|
594 | return 0;
|
---|
595 | conf_settings = settings;
|
---|
596 | ret = RUN_ONCE_ALT(&config, ossl_init_config_settings,
|
---|
597 | ossl_init_config);
|
---|
598 | conf_settings = NULL;
|
---|
599 | CRYPTO_THREAD_unlock(init_lock);
|
---|
600 | }
|
---|
601 |
|
---|
602 | if (ret <= 0)
|
---|
603 | return 0;
|
---|
604 | }
|
---|
605 | }
|
---|
606 |
|
---|
607 | if ((opts & OPENSSL_INIT_ASYNC)
|
---|
608 | && !RUN_ONCE(&async, ossl_init_async))
|
---|
609 | return 0;
|
---|
610 |
|
---|
611 | #ifndef OPENSSL_NO_ENGINE
|
---|
612 | if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
|
---|
613 | && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
|
---|
614 | return 0;
|
---|
615 | # ifndef OPENSSL_NO_RDRAND
|
---|
616 | if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
|
---|
617 | && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
|
---|
618 | return 0;
|
---|
619 | # endif
|
---|
620 | if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
|
---|
621 | && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
|
---|
622 | return 0;
|
---|
623 | # ifndef OPENSSL_NO_STATIC_ENGINE
|
---|
624 | # ifndef OPENSSL_NO_DEVCRYPTOENG
|
---|
625 | if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
|
---|
626 | && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
|
---|
627 | return 0;
|
---|
628 | # endif
|
---|
629 | # if !defined(OPENSSL_NO_PADLOCKENG)
|
---|
630 | if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
|
---|
631 | && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
|
---|
632 | return 0;
|
---|
633 | # endif
|
---|
634 | # if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
|
---|
635 | if ((opts & OPENSSL_INIT_ENGINE_CAPI)
|
---|
636 | && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
|
---|
637 | return 0;
|
---|
638 | # endif
|
---|
639 | # if !defined(OPENSSL_NO_AFALGENG)
|
---|
640 | if ((opts & OPENSSL_INIT_ENGINE_AFALG)
|
---|
641 | && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
|
---|
642 | return 0;
|
---|
643 | # endif
|
---|
644 | # endif
|
---|
645 | if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
|
---|
646 | | OPENSSL_INIT_ENGINE_OPENSSL
|
---|
647 | | OPENSSL_INIT_ENGINE_AFALG)) {
|
---|
648 | ENGINE_register_all_complete();
|
---|
649 | }
|
---|
650 | #endif
|
---|
651 |
|
---|
652 | if (!CRYPTO_atomic_or(&optsdone, opts, &tmp, init_lock))
|
---|
653 | return 0;
|
---|
654 |
|
---|
655 | return 1;
|
---|
656 | }
|
---|
657 |
|
---|
658 | int OPENSSL_atexit(void (*handler)(void))
|
---|
659 | {
|
---|
660 | OPENSSL_INIT_STOP *newhand;
|
---|
661 |
|
---|
662 | #if !defined(OPENSSL_USE_NODELETE)\
|
---|
663 | && !defined(OPENSSL_NO_PINSHARED)
|
---|
664 | {
|
---|
665 | union {
|
---|
666 | void *sym;
|
---|
667 | void (*func)(void);
|
---|
668 | } handlersym;
|
---|
669 |
|
---|
670 | handlersym.func = handler;
|
---|
671 | # if defined(DSO_WIN32) && !defined(_WIN32_WCE)
|
---|
672 | {
|
---|
673 | HMODULE handle = NULL;
|
---|
674 | BOOL ret;
|
---|
675 |
|
---|
676 | /*
|
---|
677 | * We don't use the DSO route for WIN32 because there is a better
|
---|
678 | * way
|
---|
679 | */
|
---|
680 | ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
|
---|
681 | | GET_MODULE_HANDLE_EX_FLAG_PIN,
|
---|
682 | handlersym.sym, &handle);
|
---|
683 |
|
---|
684 | if (!ret)
|
---|
685 | return 0;
|
---|
686 | }
|
---|
687 | # elif !defined(DSO_NONE)
|
---|
688 | /*
|
---|
689 | * Deliberately leak a reference to the handler. This will force the
|
---|
690 | * library/code containing the handler to remain loaded until we run the
|
---|
691 | * atexit handler. If -znodelete has been used then this is
|
---|
692 | * unnecessary.
|
---|
693 | */
|
---|
694 | {
|
---|
695 | DSO *dso = NULL;
|
---|
696 |
|
---|
697 | ERR_set_mark();
|
---|
698 | dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
|
---|
699 | /* See same code above in ossl_init_base() for an explanation. */
|
---|
700 | OSSL_TRACE1(INIT,
|
---|
701 | "atexit: obtained DSO reference? %s\n",
|
---|
702 | (dso == NULL ? "No!" : "Yes."));
|
---|
703 | DSO_free(dso);
|
---|
704 | ERR_pop_to_mark();
|
---|
705 | }
|
---|
706 | # endif
|
---|
707 | }
|
---|
708 | #endif
|
---|
709 |
|
---|
710 | if ((newhand = OPENSSL_malloc(sizeof(*newhand))) == NULL) {
|
---|
711 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
|
---|
712 | return 0;
|
---|
713 | }
|
---|
714 |
|
---|
715 | newhand->handler = handler;
|
---|
716 | newhand->next = stop_handlers;
|
---|
717 | stop_handlers = newhand;
|
---|
718 |
|
---|
719 | return 1;
|
---|
720 | }
|
---|
721 |
|
---|