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