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