/src/openssl30/ssl/ssl_init.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include "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 "sslerr.h" |
18 | | #include "internal/thread_once.h" |
19 | | |
20 | | static int stopped; |
21 | | |
22 | | static void ssl_library_stop(void); |
23 | | |
24 | | static CRYPTO_ONCE ssl_base = CRYPTO_ONCE_STATIC_INIT; |
25 | | static int ssl_base_inited = 0; |
26 | | DEFINE_RUN_ONCE_STATIC(ossl_init_ssl_base) |
27 | 40 | { |
28 | 40 | #ifndef OPENSSL_NO_COMP |
29 | 40 | OSSL_TRACE(INIT, "ossl_init_ssl_base: " |
30 | 40 | "SSL_COMP_get_compression_methods()\n"); |
31 | | /* |
32 | | * This will initialise the built-in compression algorithms. The value |
33 | | * returned is a STACK_OF(SSL_COMP), but that can be discarded safely |
34 | | */ |
35 | 40 | SSL_COMP_get_compression_methods(); |
36 | 40 | #endif |
37 | 40 | ssl_sort_cipher_list(); |
38 | 40 | OSSL_TRACE(INIT,"ossl_init_ssl_base: SSL_add_ssl_module()\n"); |
39 | | /* |
40 | | * We ignore an error return here. Not much we can do - but not that bad |
41 | | * either. We can still safely continue. |
42 | | */ |
43 | 40 | OPENSSL_atexit(ssl_library_stop); |
44 | 40 | ssl_base_inited = 1; |
45 | 40 | return 1; |
46 | 40 | } |
47 | | |
48 | | static CRYPTO_ONCE ssl_strings = CRYPTO_ONCE_STATIC_INIT; |
49 | | |
50 | | DEFINE_RUN_ONCE_STATIC(ossl_init_load_ssl_strings) |
51 | 20 | { |
52 | | /* |
53 | | * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time |
54 | | * pulling in all the error strings during static linking |
55 | | */ |
56 | 20 | #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT) |
57 | 20 | OSSL_TRACE(INIT, "ossl_init_load_ssl_strings: ossl_err_load_SSL_strings()\n"); |
58 | 20 | ossl_err_load_SSL_strings(); |
59 | 20 | #endif |
60 | 20 | return 1; |
61 | 20 | } |
62 | | |
63 | | DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_ssl_strings, |
64 | | ossl_init_load_ssl_strings) |
65 | 0 | { |
66 | | /* Do nothing in this case */ |
67 | 0 | return 1; |
68 | 0 | } |
69 | | |
70 | | static void ssl_library_stop(void) |
71 | 20 | { |
72 | | /* Might be explicitly called and also by atexit */ |
73 | 20 | if (stopped) |
74 | 0 | return; |
75 | 20 | stopped = 1; |
76 | | |
77 | 20 | if (ssl_base_inited) { |
78 | 20 | #ifndef OPENSSL_NO_COMP |
79 | 20 | OSSL_TRACE(INIT, "ssl_library_stop: " |
80 | 20 | "ssl_comp_free_compression_methods_int()\n"); |
81 | 20 | ssl_comp_free_compression_methods_int(); |
82 | 20 | #endif |
83 | 20 | } |
84 | 20 | } |
85 | | |
86 | | /* |
87 | | * If this function is called with a non NULL settings value then it must be |
88 | | * called prior to any threads making calls to any OpenSSL functions, |
89 | | * i.e. passing a non-null settings value is assumed to be single-threaded. |
90 | | */ |
91 | | int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS * settings) |
92 | 96.0k | { |
93 | 96.0k | static int stoperrset = 0; |
94 | | |
95 | 96.0k | if (stopped) { |
96 | 0 | if (!stoperrset) { |
97 | | /* |
98 | | * We only ever set this once to avoid getting into an infinite |
99 | | * loop where the error system keeps trying to init and fails so |
100 | | * sets an error etc |
101 | | */ |
102 | 0 | stoperrset = 1; |
103 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INIT_FAIL); |
104 | 0 | } |
105 | 0 | return 0; |
106 | 0 | } |
107 | | |
108 | 96.0k | opts |= OPENSSL_INIT_ADD_ALL_CIPHERS |
109 | 96.0k | | OPENSSL_INIT_ADD_ALL_DIGESTS; |
110 | 96.0k | #ifndef OPENSSL_NO_AUTOLOAD_CONFIG |
111 | 96.0k | if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) == 0) |
112 | 96.0k | opts |= OPENSSL_INIT_LOAD_CONFIG; |
113 | 96.0k | #endif |
114 | | |
115 | 96.0k | if (!OPENSSL_init_crypto(opts, settings)) |
116 | 0 | return 0; |
117 | | |
118 | 96.0k | if (!RUN_ONCE(&ssl_base, ossl_init_ssl_base)) |
119 | 0 | return 0; |
120 | | |
121 | 96.0k | if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS) |
122 | 96.0k | && !RUN_ONCE_ALT(&ssl_strings, ossl_init_no_load_ssl_strings, |
123 | 96.0k | ossl_init_load_ssl_strings)) |
124 | 0 | return 0; |
125 | | |
126 | 96.0k | if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS) |
127 | 96.0k | && !RUN_ONCE(&ssl_strings, ossl_init_load_ssl_strings)) |
128 | 0 | return 0; |
129 | | |
130 | 96.0k | return 1; |
131 | 96.0k | } |