1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 #include "crypto/s2n_fips.h"
16 
17 #include "error/s2n_errno.h"
18 
19 #include "tls/s2n_cipher_suites.h"
20 #include "tls/extensions/s2n_extension_type.h"
21 #include "tls/s2n_security_policies.h"
22 #include "tls/extensions/s2n_client_key_share.h"
23 
24 #include "utils/s2n_mem.h"
25 #include "utils/s2n_random.h"
26 #include "utils/s2n_safety.h"
27 #include "utils/s2n_safety_macros.h"
28 
29 #include "openssl/opensslv.h"
30 
31 #include "pq-crypto/s2n_pq.h"
32 
33 #include <pthread.h>
34 
35 static void s2n_cleanup_atexit(void);
36 
s2n_get_openssl_version(void)37 unsigned long s2n_get_openssl_version(void)
38 {
39     return OPENSSL_VERSION_NUMBER;
40 }
41 
42 static pthread_t main_thread = 0;
43 static bool initialized = false;
44 static bool atexit_cleanup = true;
s2n_disable_atexit(void)45 int s2n_disable_atexit(void) {
46     POSIX_ENSURE(!initialized, S2N_ERR_INITIALIZED);
47     atexit_cleanup = false;
48     return S2N_SUCCESS;
49 }
50 
s2n_init(void)51 int s2n_init(void)
52 {
53     main_thread = pthread_self();
54     POSIX_GUARD(s2n_fips_init());
55     POSIX_GUARD(s2n_mem_init());
56     POSIX_GUARD_RESULT(s2n_rand_init());
57     POSIX_GUARD(s2n_cipher_suites_init());
58     POSIX_GUARD(s2n_security_policies_init());
59     POSIX_GUARD(s2n_config_defaults_init());
60     POSIX_GUARD(s2n_extension_type_init());
61     POSIX_GUARD_RESULT(s2n_pq_init());
62 
63     if (atexit_cleanup) {
64         POSIX_ENSURE_OK(atexit(s2n_cleanup_atexit), S2N_ERR_ATEXIT);
65     }
66 
67     if (getenv("S2N_PRINT_STACKTRACE")) {
68         s2n_stack_traces_enabled_set(true);
69     }
70 
71     initialized = true;
72 
73     return S2N_SUCCESS;
74 }
75 
s2n_cleanup_atexit_impl(void)76 static bool s2n_cleanup_atexit_impl(void)
77 {
78     /* all of these should run, regardless of result, but the
79      * values to need to be consumed to prevent warnings */
80     bool a = s2n_result_is_ok(s2n_rand_cleanup_thread());
81     bool b = s2n_result_is_ok(s2n_rand_cleanup());
82     bool c = s2n_mem_cleanup() == 0;
83     s2n_wipe_static_configs();
84 
85     return a && b && c;
86 }
87 
s2n_cleanup(void)88 int s2n_cleanup(void)
89 {
90     /* s2n_cleanup is supposed to be called from each thread before exiting,
91      * so ensure that whatever clean ups we have here are thread safe */
92     POSIX_GUARD_RESULT(s2n_rand_cleanup_thread());
93 
94     /* If this is the main thread and atexit cleanup is disabled,
95      * perform final cleanup now */
96     if (pthread_equal(pthread_self(), main_thread) && !atexit_cleanup) {
97         POSIX_ENSURE(s2n_cleanup_atexit_impl(), S2N_ERR_ATEXIT);
98     }
99     return 0;
100 }
101 
s2n_cleanup_atexit(void)102 static void s2n_cleanup_atexit(void)
103 {
104     s2n_cleanup_atexit_impl();
105 }
106