/src/openssl/ssl/ssl_init.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2016-2025 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  |  | #include "internal/e_os.h"  | 
11  |  |  | 
12  |  | #include "internal/err.h"  | 
13  |  | #include <openssl/crypto.h>  | 
14  |  | #include <openssl/evp.h>  | 
15  |  | #include <openssl/trace.h>  | 
16  |  | #include "ssl_local.h"  | 
17  |  | #include "internal/thread_once.h"  | 
18  |  | #include "internal/rio_notifier.h"    /* for ossl_wsa_cleanup() */  | 
19  |  |  | 
20  |  | static int stopped;  | 
21  |  |  | 
22  |  | static CRYPTO_ONCE ssl_base = CRYPTO_ONCE_STATIC_INIT;  | 
23  |  | static int ssl_base_inited = 0;  | 
24  |  | DEFINE_RUN_ONCE_STATIC(ossl_init_ssl_base)  | 
25  | 16  | { | 
26  | 16  | #ifndef OPENSSL_NO_COMP  | 
27  | 16  |     OSSL_TRACE(INIT, "ossl_init_ssl_base: "  | 
28  | 16  |                "SSL_COMP_get_compression_methods()\n");  | 
29  |  |     /*  | 
30  |  |      * This will initialise the built-in compression algorithms. The value  | 
31  |  |      * returned is a STACK_OF(SSL_COMP), but that can be discarded safely  | 
32  |  |      */  | 
33  | 16  |     SSL_COMP_get_compression_methods();  | 
34  | 16  | #endif  | 
35  | 16  |     ssl_sort_cipher_list();  | 
36  | 16  |     OSSL_TRACE(INIT, "ossl_init_ssl_base: SSL_add_ssl_module()\n");  | 
37  | 16  |     ssl_base_inited = 1;  | 
38  | 16  |     return 1;  | 
39  | 16  | }  | 
40  |  |  | 
41  |  | /*  | 
42  |  |  * If this function is called with a non NULL settings value then it must be  | 
43  |  |  * called prior to any threads making calls to any OpenSSL functions,  | 
44  |  |  * i.e. passing a non-null settings value is assumed to be single-threaded.  | 
45  |  |  */  | 
46  |  | int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)  | 
47  | 16  | { | 
48  | 16  |     static int stoperrset = 0;  | 
49  |  |  | 
50  | 16  |     if (stopped) { | 
51  | 0  |         if (!stoperrset) { | 
52  |  |             /*  | 
53  |  |              * We only ever set this once to avoid getting into an infinite  | 
54  |  |              * loop where the error system keeps trying to init and fails so  | 
55  |  |              * sets an error etc  | 
56  |  |              */  | 
57  | 0  |             stoperrset = 1;  | 
58  | 0  |             ERR_raise(ERR_LIB_SSL, ERR_R_INIT_FAIL);  | 
59  | 0  |         }  | 
60  | 0  |         return 0;  | 
61  | 0  |     }  | 
62  |  |  | 
63  | 16  |     opts |= OPENSSL_INIT_ADD_ALL_CIPHERS  | 
64  | 16  |          |  OPENSSL_INIT_ADD_ALL_DIGESTS;  | 
65  | 16  | #ifndef OPENSSL_NO_AUTOLOAD_CONFIG  | 
66  | 16  |     if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) == 0)  | 
67  | 16  |         opts |= OPENSSL_INIT_LOAD_CONFIG;  | 
68  | 16  | #endif  | 
69  |  |  | 
70  | 16  |     if (!OPENSSL_init_crypto(opts, settings))  | 
71  | 0  |         return 0;  | 
72  |  |  | 
73  | 16  |     if (!RUN_ONCE(&ssl_base, ossl_init_ssl_base))  | 
74  | 0  |         return 0;  | 
75  |  |  | 
76  | 16  |     return 1;  | 
77  | 16  | }  |