aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/s2n/utils/s2n_init.c
blob: 0f79f959fbd3a7822a57537a423fb958589257a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
#include "crypto/s2n_fips.h"

#include "error/s2n_errno.h"

#include "tls/s2n_cipher_suites.h"
#include "tls/extensions/s2n_extension_type.h"
#include "tls/s2n_security_policies.h"
#include "tls/extensions/s2n_client_key_share.h"

#include "utils/s2n_mem.h"
#include "utils/s2n_random.h"
#include "utils/s2n_safety.h"

#include "openssl/opensslv.h"

#include "pq-crypto/s2n_pq.h"

static void s2n_cleanup_atexit(void);

unsigned long s2n_get_openssl_version(void)
{
    return OPENSSL_VERSION_NUMBER;
}

int s2n_init(void)
{
    GUARD_POSIX(s2n_fips_init());
    GUARD_POSIX(s2n_mem_init());
    GUARD_AS_POSIX(s2n_rand_init());
    GUARD_POSIX(s2n_cipher_suites_init());
    GUARD_POSIX(s2n_security_policies_init());
    GUARD_POSIX(s2n_config_defaults_init());
    GUARD_POSIX(s2n_extension_type_init());
    GUARD_AS_POSIX(s2n_pq_init());

    S2N_ERROR_IF(atexit(s2n_cleanup_atexit) != 0, S2N_ERR_ATEXIT);

    if (getenv("S2N_PRINT_STACKTRACE")) {
      s2n_stack_traces_enabled_set(true);
    }

    return 0;
}

int s2n_cleanup(void)
{
    /* s2n_cleanup is supposed to be called from each thread before exiting,
     * so ensure that whatever clean ups we have here are thread safe */
    GUARD_AS_POSIX(s2n_rand_cleanup_thread());
    return 0;
}

static bool s2n_cleanup_atexit_impl(void)
{
    /* all of these should run, regardless of result, but the
     * values to need to be consumed to prevent warnings */
    bool a = s2n_result_is_ok(s2n_rand_cleanup_thread());
    bool b = s2n_result_is_ok(s2n_rand_cleanup());
    bool c = s2n_mem_cleanup() == 0;
    s2n_wipe_static_configs();

    return a && b && c;
}

static void s2n_cleanup_atexit(void)
{
    s2n_cleanup_atexit_impl();
}