| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "utils/s2n_init.h" |
| 17 |
|
| 18 |
#include <pthread.h> |
| 19 |
|
| 20 |
#include "api/unstable/cleanup.h" |
| 21 |
#include "crypto/s2n_fips.h" |
| 22 |
#include "crypto/s2n_libcrypto.h" |
| 23 |
#include "crypto/s2n_locking.h" |
| 24 |
#include "error/s2n_errno.h" |
| 25 |
#include "openssl/opensslv.h" |
| 26 |
#include "tls/extensions/s2n_client_key_share.h" |
| 27 |
#include "tls/extensions/s2n_extension_type.h" |
| 28 |
#include "tls/s2n_cipher_suites.h" |
| 29 |
#include "tls/s2n_security_policies.h" |
| 30 |
#include "tls/s2n_tls13_secrets.h" |
| 31 |
#include "utils/s2n_mem.h" |
| 32 |
#include "utils/s2n_random.h" |
| 33 |
#include "utils/s2n_safety.h" |
| 34 |
#include "utils/s2n_safety_macros.h" |
| 35 |
|
| 36 |
static void s2n_cleanup_atexit(void); |
| 37 |
|
| 38 |
static pthread_t main_thread = 0; |
| 39 |
static bool initialized = false; |
| 40 |
static bool atexit_cleanup = false; |
| 41 |
int s2n_disable_atexit(void) |
| 42 |
{ |
| 43 |
POSIX_ENSURE(!initialized, S2N_ERR_INITIALIZED); |
| 44 |
atexit_cleanup = false; |
| 45 |
return S2N_SUCCESS; |
| 46 |
} |
| 47 |
|
| 48 |
int s2n_enable_atexit(void) |
| 49 |
{ |
| 50 |
atexit_cleanup = true; |
| 51 |
return S2N_SUCCESS; |
| 52 |
} |
| 53 |
|
| 54 |
int s2n_init(void) |
| 55 |
{ |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
POSIX_ENSURE(!initialized, S2N_ERR_INITIALIZED); |
| 61 |
|
| 62 |
main_thread = pthread_self(); |
| 63 |
|
| 64 |
if (getenv("S2N_INTEG_TEST")) { |
| 65 |
POSIX_GUARD(s2n_in_integ_test_set(true)); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
POSIX_GUARD_RESULT(s2n_libcrypto_validate_runtime()); |
| 73 |
|
| 74 |
POSIX_GUARD(s2n_mem_init()); |
| 75 |
|
| 76 |
POSIX_GUARD_RESULT(s2n_locking_init()); |
| 77 |
POSIX_GUARD(s2n_fips_init()); |
| 78 |
POSIX_GUARD_RESULT(s2n_rand_init()); |
| 79 |
POSIX_GUARD_RESULT(s2n_hash_algorithms_init()); |
| 80 |
POSIX_GUARD(s2n_cipher_suites_init()); |
| 81 |
POSIX_GUARD(s2n_security_policies_init()); |
| 82 |
POSIX_GUARD(s2n_config_defaults_init()); |
| 83 |
POSIX_GUARD(s2n_extension_type_init()); |
| 84 |
POSIX_GUARD_RESULT(s2n_tls13_empty_transcripts_init()); |
| 85 |
POSIX_GUARD_RESULT(s2n_atomic_init()); |
| 86 |
|
| 87 |
if (atexit_cleanup) { |
| 88 |
POSIX_ENSURE_OK(atexit(s2n_cleanup_atexit), S2N_ERR_ATEXIT); |
| 89 |
} |
| 90 |
|
| 91 |
if (getenv("S2N_PRINT_STACKTRACE")) { |
| 92 |
s2n_stack_traces_enabled_set(true); |
| 93 |
} |
| 94 |
|
| 95 |
#if defined(OPENSSL_IS_AWSLC) |
| 96 |
CRYPTO_pre_sandbox_init(); |
| 97 |
#endif |
| 98 |
|
| 99 |
initialized = true; |
| 100 |
|
| 101 |
return S2N_SUCCESS; |
| 102 |
} |
| 103 |
|
| 104 |
static bool s2n_cleanup_atexit_impl(void) |
| 105 |
{ |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
s2n_wipe_static_configs(); |
| 111 |
|
| 112 |
bool cleaned_up = s2n_result_is_ok(s2n_cipher_suites_cleanup()) |
| 113 |
&& s2n_result_is_ok(s2n_hash_algorithms_cleanup()) |
| 114 |
&& s2n_result_is_ok(s2n_rand_cleanup()) |
| 115 |
&& s2n_result_is_ok(s2n_locking_cleanup()) |
| 116 |
&& (s2n_mem_cleanup() == S2N_SUCCESS); |
| 117 |
|
| 118 |
initialized = !cleaned_up; |
| 119 |
return cleaned_up; |
| 120 |
} |
| 121 |
|
| 122 |
int s2n_cleanup_final(void) |
| 123 |
{ |
| 124 |
|
| 125 |
POSIX_ENSURE(initialized, S2N_ERR_NOT_INITIALIZED); |
| 126 |
POSIX_ENSURE(s2n_cleanup_atexit_impl(), S2N_ERR_ATEXIT); |
| 127 |
|
| 128 |
return S2N_SUCCESS; |
| 129 |
} |
| 130 |
|
| 131 |
int s2n_cleanup(void) |
| 132 |
{ |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
return S2N_SUCCESS; |
| 137 |
} |
| 138 |
|
| 139 |
int s2n_cleanup_thread(void) |
| 140 |
{ |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
return S2N_SUCCESS; |
| 145 |
} |
| 146 |
|
| 147 |
static void s2n_cleanup_atexit(void) |
| 148 |
{ |
| 149 |
(void) s2n_cleanup_atexit_impl(); |
| 150 |
} |
| 151 |
|
| 152 |
bool s2n_is_initialized(void) |
| 153 |
{ |
| 154 |
return initialized; |
| 155 |
} |