]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/init.c
Updates for auto init/deinit review comments
[thirdparty/openssl.git] / crypto / init.c
CommitLineData
b184e3ef
MC
1/*
2 * Written by Matt Caswell for the OpenSSL project.
3 */
4/* ====================================================================
5 * Copyright (c) 2016 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23 *
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * openssl-core@openssl.org.
28 *
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
51 *
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
55 *
56 */
57
b184e3ef
MC
58#include <internal/cryptlib_int.h>
59#include <openssl/err.h>
60#include <openssl/evp.h>
b184e3ef
MC
61#include <internal/evp_int.h>
62#include <internal/conf.h>
63#include <internal/async.h>
64#include <internal/engine.h>
b184e3ef 65#include <openssl/comp.h>
b184e3ef 66#include <internal/err.h>
b184e3ef
MC
67#include <stdlib.h>
68
71567a6f
MC
69static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
70
b184e3ef
MC
71/* Implement "once" functionality */
72#if !defined(OPENSSL_THREADS)
73typedef int OPENSSL_INIT_ONCE;
74# define OPENSSL_INIT_ONCE_STATIC_INIT 0
75# define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once) (*(once) = 0)
76
77static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
78{
79 if (*once == OPENSSL_INIT_ONCE_STATIC_INIT) {
80 *once = 1;
81 init();
82 }
83}
84
85static int ossl_init_setup_thread_stop(void)
86{
87 /*
88 * There are no threads to stop. Do nothing.
89 */
90 return 1;
91}
92
93static void ossl_init_thread_stop_cleanup(void)
94{
95}
96
97static struct thread_local_inits_st *local = NULL;
98void *ossl_init_get_thread_local(int alloc)
99{
100 if (local == NULL && alloc)
101 local = OPENSSL_zalloc(sizeof(*local));
102 return local;
103}
104
105#elif defined(OPENSSL_SYS_WINDOWS)
106
107# include <windows.h>
108
109# if _WIN32_WINNT < 0x0600
110
111/*
112 * Versions before 0x0600 (Windows Vista, Windows Server 2008 or later) do not
113 * have InitOnceExecuteOnce, so we fall back to using a spinlock instead.
114 */
115typedef LONG OPENSSL_INIT_ONCE;
116# define OPENSSL_INIT_ONCE_STATIC_INIT 0
117# define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once) (*(once) = 0)
118
119# define ONCE_UNINITED 0
120# define ONCE_ININIT 1
121# define ONCE_DONE 2
122
123static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
124{
125 LONG volatile *lock = (LONG *)once;
126 LONG result;
127
128 if (*lock == ONCE_DONE)
129 return;
130
131 do {
132 result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
133 if (result == ONCE_UNINITED) {
134 init();
135 *lock = ONCE_DONE;
136 return;
137 }
138 } while (result == ONCE_ININIT);
139}
140
141# else
142
143typedef INIT_ONCE OPENSSL_INIT_ONCE;
144# define OPENSSL_INIT_ONCE_STATIC_INIT INIT_ONCE_STATIC_INIT
145# define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once) \
146 InitOnceInitialize((PINIT_ONCE)(once))
147
148static BOOL CALLBACK once_cb(PINIT_ONCE once, PVOID initfp, PVOID *unused)
149{
150 void (*init)(void) = initfp;
151
152 init();
153
154 return TRUE;
155}
156
157static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
158{
159 InitOnceExecuteOnce((INIT_ONCE *)once, once_cb, init, NULL);
160}
161# endif
162
163DWORD threadstopkey = TLS_OUT_OF_INDEXES;
164
165static int ossl_init_setup_thread_stop(void)
166{
167 /*
168 * We use a dummy thread local key here. We use the destructor to detect
169 * when the thread is going to stop
170 */
171 threadstopkey = TlsAlloc();
172 if (threadstopkey == TLS_OUT_OF_INDEXES)
173 return 0;
174
175 return 1;
176}
177
178static void ossl_init_thread_stop_cleanup(void)
179{
180 if (threadstopkey != TLS_OUT_OF_INDEXES) {
181 TlsFree(threadstopkey);
182 }
183}
184
185void *ossl_init_get_thread_local(int alloc)
186{
187 struct thread_local_inits_st *local = TlsGetValue(threadstopkey);
188
189 if (local == NULL && alloc) {
190 local = OPENSSL_zalloc(sizeof *local);
191 TlsSetValue(threadstopkey, local);
192 }
193
194 return local;
195}
196
197#else /* pthreads */
198# include <pthread.h>
199
200pthread_key_t threadstopkey;
201
202typedef pthread_once_t OPENSSL_INIT_ONCE;
203# define OPENSSL_INIT_ONCE_STATIC_INIT PTHREAD_ONCE_INIT
204# define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once) (*(once) = PTHREAD_ONCE_INIT)
205
206static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
207{
208 pthread_once(once, init);
209}
210
211static void ossl_init_thread_stop_wrap(void *local)
212{
213 ossl_init_thread_stop((struct thread_local_inits_st *)local);
214}
215
216static int ossl_init_setup_thread_stop(void)
217{
218 /*
219 * We use a dummy thread local key here. We use the destructor to detect
220 * when the thread is going to stop
221 */
222 return (pthread_key_create(&threadstopkey,
223 ossl_init_thread_stop_wrap) == 0);
224}
225
226static void ossl_init_thread_stop_cleanup(void)
227{
228}
229
230void *ossl_init_get_thread_local(int alloc)
231{
232 struct thread_local_inits_st *local = pthread_getspecific(threadstopkey);
233
234 if (local == NULL && alloc) {
235 local = OPENSSL_zalloc(sizeof *local);
236 pthread_setspecific(threadstopkey, local);
237 }
238
239 return local;
240}
241
242#endif
243
244struct ossl_init_stop_st {
245 void (*handler)(void);
246 OPENSSL_INIT_STOP *next;
247};
248
249static OPENSSL_INIT_STOP *stop_handlers = NULL;
250
251static OPENSSL_INIT_ONCE base = OPENSSL_INIT_ONCE_STATIC_INIT;
252static int base_inited = 0;
253static void ossl_init_base(void)
254{
255#ifdef OPENSSL_INIT_DEBUG
256 fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
257#endif
258 ossl_init_setup_thread_stop();
259 atexit(OPENSSL_INIT_library_stop);
260 OPENSSL_cpuid_setup();
261 base_inited = 1;
262}
263
264static OPENSSL_INIT_ONCE load_crypto_strings = OPENSSL_INIT_ONCE_STATIC_INIT;
265static int load_crypto_strings_inited = 0;
266static void ossl_init_no_load_crypto_strings(void)
267{
268 /* Do nothing in this case */
269 return;
270}
271
272static void ossl_init_load_crypto_strings(void)
273{
498abff0
MC
274 /*
275 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
276 * pulling in all the error strings during static linking
277 */
278#if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
b184e3ef
MC
279# ifdef OPENSSL_INIT_DEBUG
280 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
281 "err_load_crypto_strings_intern()\n");
282# endif
b184e3ef 283 err_load_crypto_strings_intern();
b184e3ef
MC
284#endif
285 load_crypto_strings_inited = 1;
286}
287
288static OPENSSL_INIT_ONCE add_all_ciphers = OPENSSL_INIT_ONCE_STATIC_INIT;
289static void ossl_init_add_all_ciphers(void)
290{
291 /*
292 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
293 * pulling in all the ciphers during static linking
294 */
295#ifndef OPENSSL_NO_AUTOALGINIT
296# ifdef OPENSSL_INIT_DEBUG
297 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
298 "openssl_add_all_ciphers_internal()\n");
299# endif
b184e3ef 300 openssl_add_all_ciphers_internal();
b184e3ef
MC
301# ifndef OPENSSL_NO_ENGINE
302# if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
303 ENGINE_setup_bsd_cryptodev();
304# endif
305# endif
306#endif
307}
308
309static OPENSSL_INIT_ONCE add_all_digests = OPENSSL_INIT_ONCE_STATIC_INIT;
310static void ossl_init_add_all_digests(void)
311{
312 /*
313 * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
314 * pulling in all the ciphers during static linking
315 */
316#ifndef OPENSSL_NO_AUTOALGINIT
317# ifdef OPENSSL_INIT_DEBUG
318 fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
319 "openssl_add_all_digests_internal()\n");
320# endif
b184e3ef 321 openssl_add_all_digests_internal();
b184e3ef
MC
322# ifndef OPENSSL_NO_ENGINE
323# if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
324 ENGINE_setup_bsd_cryptodev();
325# endif
326# endif
327#endif
328}
329
330static void ossl_init_no_add_algs(void)
331{
332 /* Do nothing */
333 return;
334}
335
336static OPENSSL_INIT_ONCE config = OPENSSL_INIT_ONCE_STATIC_INIT;
337static int config_inited = 0;
338static const char *config_filename;
339static void ossl_init_config(void)
340{
341#ifdef OPENSSL_INIT_DEBUG
342 fprintf(stderr,
343 "OPENSSL_INIT: ossl_init_config: openssl_config_internal(%s)\n",
344 config_filename==NULL?"NULL":config_filename);
345#endif
b184e3ef 346 openssl_config_internal(config_filename);
b184e3ef
MC
347 config_inited = 1;
348}
349static void ossl_init_no_config(void)
350{
351#ifdef OPENSSL_INIT_DEBUG
352 fprintf(stderr,
353 "OPENSSL_INIT: ossl_init_config: openssl_no_config_internal()\n");
354#endif
b184e3ef 355 openssl_no_config_internal();
b184e3ef
MC
356 config_inited = 1;
357}
358
359static OPENSSL_INIT_ONCE async = OPENSSL_INIT_ONCE_STATIC_INIT;
360static int async_inited = 0;
361static void ossl_init_async(void)
362{
363#ifdef OPENSSL_INIT_DEBUG
364 fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
365#endif
b184e3ef 366 async_init();
b184e3ef
MC
367 async_inited = 1;
368}
369
370#ifndef OPENSSL_NO_ENGINE
371static int engine_inited = 0;
372static OPENSSL_INIT_ONCE engine_openssl = OPENSSL_INIT_ONCE_STATIC_INIT;
373static void ossl_init_engine_openssl(void)
374{
375# ifdef OPENSSL_INIT_DEBUG
376 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
377 "engine_load_openssl_internal()\n");
378# endif
b184e3ef 379 engine_load_openssl_internal();
b184e3ef
MC
380 engine_inited = 1;
381}
382# if !defined(OPENSSL_NO_HW) && \
383 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
384static OPENSSL_INIT_ONCE engine_cryptodev = OPENSSL_INIT_ONCE_STATIC_INIT;
385static void ossl_init_engine_cryptodev(void)
386{
387# ifdef OPENSSL_INIT_DEBUG
388 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
389 "engine_load_cryptodev_internal()\n");
390# endif
b184e3ef 391 engine_load_cryptodev_internal();
b184e3ef
MC
392 engine_inited = 1;
393}
394# endif
395
396# ifndef OPENSSL_NO_RDRAND
397static OPENSSL_INIT_ONCE engine_rdrand = OPENSSL_INIT_ONCE_STATIC_INIT;
398static void ossl_init_engine_rdrand(void)
399{
400# ifdef OPENSSL_INIT_DEBUG
401 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
402 "engine_load_rdrand_internal()\n");
403# endif
b184e3ef 404 engine_load_rdrand_internal();
b184e3ef
MC
405 engine_inited = 1;
406}
407# endif
408static OPENSSL_INIT_ONCE engine_dynamic = OPENSSL_INIT_ONCE_STATIC_INIT;
409static void ossl_init_engine_dynamic(void)
410{
411# ifdef OPENSSL_INIT_DEBUG
412 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
413 "engine_load_dynamic_internal()\n");
414# endif
b184e3ef 415 engine_load_dynamic_internal();
b184e3ef
MC
416 engine_inited = 1;
417}
418# ifndef OPENSSL_NO_STATIC_ENGINE
419# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
420static OPENSSL_INIT_ONCE engine_padlock = OPENSSL_INIT_ONCE_STATIC_INIT;
421static void ossl_init_engine_padlock(void)
422{
423# ifdef OPENSSL_INIT_DEBUG
424 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
425 "engine_load_padlock_internal()\n");
426# endif
b184e3ef 427 engine_load_padlock_internal();
b184e3ef
MC
428 engine_inited = 1;
429}
430# endif
431# if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
432static OPENSSL_INIT_ONCE engine_capi = OPENSSL_INIT_ONCE_STATIC_INIT;
433static void ossl_init_engine_capi(void)
434{
435# ifdef OPENSSL_INIT_DEBUG
436 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
437 "engine_load_capi_internal()\n");
438# endif
b184e3ef 439 engine_load_capi_internal();
b184e3ef
MC
440 engine_inited = 1;
441}
442# endif
443static OPENSSL_INIT_ONCE engine_dasync = OPENSSL_INIT_ONCE_STATIC_INIT;
444static void ossl_init_engine_dasync(void)
445{
446# ifdef OPENSSL_INIT_DEBUG
447 fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
448 "engine_load_dasync_internal()\n");
449# endif
b184e3ef 450 engine_load_dasync_internal();
b184e3ef
MC
451 engine_inited = 1;
452}
453# endif
454#endif
455
456static OPENSSL_INIT_ONCE zlib = OPENSSL_INIT_ONCE_STATIC_INIT;
457static int zlib_inited = 0;
458static void ossl_init_zlib(void)
459{
460 /* Do nothing - we need to know about this for the later cleanup */
461 zlib_inited = 1;
462}
463
71567a6f 464static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
b184e3ef
MC
465{
466 /* Can't do much about this */
467 if (locals == NULL)
468 return;
469
470 if (locals->async) {
471#ifdef OPENSSL_INIT_DEBUG
472 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
473 "ASYNC_cleanup_thread()\n");
474#endif
475 ASYNC_cleanup_thread();
476 }
477
478 if (locals->err_state) {
479#ifdef OPENSSL_INIT_DEBUG
480 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
481 "ERR_remove_thread_state(NULL)\n");
482#endif
483 ERR_remove_thread_state(NULL);
484 }
485
486 OPENSSL_free(locals);
487 ossl_init_thread_stop_cleanup();
488}
489
71567a6f
MC
490void OPENSSL_INIT_thread_stop(void)
491{
492 ossl_init_thread_stop(
493 (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
494}
495
b184e3ef
MC
496int ossl_init_thread_start(uint64_t opts)
497{
498 struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
499
500 if (locals == NULL)
501 return 0;
502
503 if (opts & OPENSSL_INIT_THREAD_ASYNC) {
504#ifdef OPENSSL_INIT_DEBUG
505 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
506 "marking thread for async\n");
507#endif
508 locals->async = 1;
509 }
510
511 if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
512#ifdef OPENSSL_INIT_DEBUG
513 fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
514 "marking thread for err_state\n");
515#endif
516 locals->err_state = 1;
517 }
518
519 return 1;
520}
521
522void OPENSSL_INIT_library_stop(void)
523{
524 OPENSSL_INIT_STOP *currhandler, *lasthandler;
525
526 /*
527 * Thread stop may not get automatically called by the thread library for
528 * the very last thread in some situations, so call it directly.
529 */
530 ossl_init_thread_stop(ossl_init_get_thread_local(0));
531
532 currhandler = stop_handlers;
533 while (currhandler != NULL) {
534 currhandler->handler();
535 lasthandler = currhandler;
536 currhandler = currhandler->next;
537 OPENSSL_free(lasthandler);
538 }
539 stop_handlers = NULL;
540 /*
541 * We assume we are single-threaded for this function, i.e. no race
542 * conditions for the various "*_inited" vars below.
543 */
544
545 if (zlib_inited) {
546#ifdef OPENSSL_INIT_DEBUG
547 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
548 "COMP_zlib_cleanup()\n");
549#endif
550 COMP_zlib_cleanup();
551 zlib_inited = 0;
552 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&zlib);
553 }
554
555#ifndef OPENSSL_NO_ENGINE
556 if (engine_inited) {
557# ifdef OPENSSL_INIT_DEBUG
558 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
559 "ENGINE_cleanup()\n");
560# endif
561 ENGINE_cleanup();
562 engine_inited = 0;
563 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_openssl);
564# if !defined(OPENSSL_NO_HW) && \
565 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
566 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_cryptodev);
567# endif
568# ifndef OPENSSL_NO_RDRAND
569 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_rdrand);
570# endif
571 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_dynamic);
572# ifndef OPENSSL_NO_STATIC_ENGINE
573# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
574 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_padlock);
575# endif
576# if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
577 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_capi);
578# endif
579 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_dasync);
580# endif
581 }
582#endif
583
584 async_inited = 0;
585 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&async);
586
587 config_inited = 0;
588 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&config);
589 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&add_all_ciphers);
590 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&add_all_digests);
591
592 if (load_crypto_strings_inited) {
593#ifdef OPENSSL_INIT_DEBUG
594 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
595 "ERR_free_strings()\n");
596#endif
597 ERR_free_strings();
598 load_crypto_strings_inited = 0;
599 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&load_crypto_strings);
600 }
601
602 if (base_inited) {
603#ifdef OPENSSL_INIT_DEBUG
604 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
605 "CRYPTO_cleanup_all_ex_data()\n");
606 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
607 "EVP_cleanup()\n");
608 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
609 "CONF_modules_free()\n");
610 fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
611 "RAND_cleanup()\n");
612#endif
613 CRYPTO_cleanup_all_ex_data();
614 EVP_cleanup();
615 CONF_modules_free();
616 RAND_cleanup();
617 base_inited = 0;
618 OPENSSL_INIT_ONCE_DYNAMIC_INIT(&base);
619 }
620}
621
622static const OPENSSL_INIT_SETTINGS *ossl_init_get_setting(
623 const OPENSSL_INIT_SETTINGS *settings, int name)
624{
625 if (settings == NULL)
626 return NULL;
627
628 while (settings->name != OPENSSL_INIT_SET_END) {
629 if (settings->name == name)
630 return settings;
631 settings++;
632 }
633
634 return NULL;
635}
636
637/*
638 * If this function is called with a non NULL settings value then it must be
639 * called prior to any threads making calls to any OpenSSL functions,
640 * i.e. passing a non-null settings value is assumed to be single-threaded.
641 */
642void OPENSSL_INIT_crypto_library_start(uint64_t opts,
643 const OPENSSL_INIT_SETTINGS *settings)
644{
645 ossl_init_once_run(&base, ossl_init_base);
646
647 if (opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
648 ossl_init_once_run(&load_crypto_strings,
649 ossl_init_no_load_crypto_strings);
650
651 if (opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
652 ossl_init_once_run(&load_crypto_strings, ossl_init_load_crypto_strings);
653
654 if (opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
655 ossl_init_once_run(&add_all_ciphers, ossl_init_no_add_algs);
656
657 if (opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
658 ossl_init_once_run(&add_all_ciphers, ossl_init_add_all_ciphers);
659
660 if (opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
661 ossl_init_once_run(&add_all_digests, ossl_init_no_add_algs);
662
663 if (opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
664 ossl_init_once_run(&add_all_digests, ossl_init_add_all_digests);
665
666 if (opts & OPENSSL_INIT_NO_LOAD_CONFIG) {
667 ossl_init_once_run(&config, ossl_init_no_config);
668 }
669
670 if (opts & OPENSSL_INIT_LOAD_CONFIG) {
bf24111b 671 CRYPTO_w_lock(CRYPTO_LOCK_INIT);
b184e3ef
MC
672 if (settings != NULL) {
673 const OPENSSL_INIT_SETTINGS *curr;
674 curr = ossl_init_get_setting(settings,
675 OPENSSL_INIT_SET_CONF_FILENAME);
bf24111b
MC
676 config_filename = (curr == NULL) ? NULL : curr->value.type_string;
677 } else {
678 config_filename = NULL;
b184e3ef
MC
679 }
680 ossl_init_once_run(&config, ossl_init_config);
bf24111b 681 CRYPTO_w_unlock(CRYPTO_LOCK_INIT);
b184e3ef
MC
682 }
683
684 if (opts & OPENSSL_INIT_ASYNC) {
685 ossl_init_once_run(&async, ossl_init_async);
686 }
687
688#ifndef OPENSSL_NO_ENGINE
689 if (opts & OPENSSL_INIT_ENGINE_OPENSSL) {
690 ossl_init_once_run(&engine_openssl, ossl_init_engine_openssl);
691 }
692# if !defined(OPENSSL_NO_HW) && \
693 (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
694 if (opts & OPENSSL_INIT_ENGINE_CRYPTODEV) {
695 ossl_init_once_run(&engine_cryptodev, ossl_init_engine_cryptodev);
696 }
697# endif
698# ifndef OPENSSL_NO_RDRAND
699 if (opts & OPENSSL_INIT_ENGINE_RDRAND) {
700 ossl_init_once_run(&engine_rdrand, ossl_init_engine_rdrand);
701 }
702# endif
703 if (opts & OPENSSL_INIT_ENGINE_DYNAMIC) {
704 ossl_init_once_run(&engine_dynamic, ossl_init_engine_dynamic);
705 }
706# ifndef OPENSSL_NO_STATIC_ENGINE
707# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
708 if (opts & OPENSSL_INIT_ENGINE_PADLOCK) {
709 ossl_init_once_run(&engine_padlock, ossl_init_engine_padlock);
710 }
711# endif
712# if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
713 if (opts & OPENSSL_INIT_ENGINE_CAPI) {
714 ossl_init_once_run(&engine_capi, ossl_init_engine_capi);
715 }
716# endif
717 if (opts & OPENSSL_INIT_ENGINE_DASYNC) {
718 ossl_init_once_run(&engine_dasync, ossl_init_engine_dasync);
719 }
720# endif
721 if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
722 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL)) {
723 ENGINE_register_all_complete();
724 }
725#endif
726
727 if (opts & OPENSSL_INIT_ZLIB) {
728 ossl_init_once_run(&zlib, ossl_init_zlib);
729 }
730}
731
732int OPENSSL_INIT_register_stop_handler(void (*handler)(void))
733{
734 OPENSSL_INIT_STOP *newhand;
735
736 newhand = OPENSSL_malloc(sizeof(*newhand));
737 if (newhand == NULL)
738 return 0;
739
740 newhand->handler = handler;
741 newhand->next = stop_handlers;
742 stop_handlers = newhand;
743
744 return 1;
745}
746
747