s2n_init.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <pthread.h>
  16. #include "crypto/s2n_fips.h"
  17. #include "crypto/s2n_libcrypto.h"
  18. #include "crypto/s2n_locking.h"
  19. #include "error/s2n_errno.h"
  20. #include "openssl/opensslv.h"
  21. #include "pq-crypto/s2n_pq.h"
  22. #include "tls/extensions/s2n_client_key_share.h"
  23. #include "tls/extensions/s2n_extension_type.h"
  24. #include "tls/s2n_cipher_suites.h"
  25. #include "tls/s2n_security_policies.h"
  26. #include "tls/s2n_tls13_secrets.h"
  27. #include "utils/s2n_mem.h"
  28. #include "utils/s2n_random.h"
  29. #include "utils/s2n_safety.h"
  30. #include "utils/s2n_safety_macros.h"
  31. static void s2n_cleanup_atexit(void);
  32. static pthread_t main_thread = 0;
  33. static bool initialized = false;
  34. static bool atexit_cleanup = true;
  35. int s2n_disable_atexit(void)
  36. {
  37. POSIX_ENSURE(!initialized, S2N_ERR_INITIALIZED);
  38. atexit_cleanup = false;
  39. return S2N_SUCCESS;
  40. }
  41. int s2n_enable_atexit(void)
  42. {
  43. atexit_cleanup = true;
  44. return S2N_SUCCESS;
  45. }
  46. int s2n_init(void)
  47. {
  48. /* USAGE-GUIDE says s2n_init MUST NOT be called more than once
  49. * Public documentation for API states s2n_init should only be called once
  50. * https://github.com/aws/s2n-tls/issues/3446 is a result of not enforcing this
  51. */
  52. POSIX_ENSURE(!initialized, S2N_ERR_INITIALIZED);
  53. main_thread = pthread_self();
  54. if (getenv("S2N_INTEG_TEST")) {
  55. POSIX_GUARD(s2n_in_integ_test_set(true));
  56. }
  57. /* Should run before any init method that calls libcrypto methods
  58. * to ensure we don't try to call methods that don't exist.
  59. * It doesn't require any locks since it only deals with values that
  60. * should be constant, so can run before s2n_locking_init. */
  61. POSIX_GUARD_RESULT(s2n_libcrypto_validate_runtime());
  62. /* Must run before any init method that allocates memory. */
  63. POSIX_GUARD(s2n_mem_init());
  64. /* Must run before any init method that calls libcrypto methods. */
  65. POSIX_GUARD_RESULT(s2n_locking_init());
  66. POSIX_GUARD(s2n_fips_init());
  67. POSIX_GUARD_RESULT(s2n_rand_init());
  68. POSIX_GUARD(s2n_cipher_suites_init());
  69. POSIX_GUARD(s2n_security_policies_init());
  70. POSIX_GUARD(s2n_config_defaults_init());
  71. POSIX_GUARD(s2n_extension_type_init());
  72. POSIX_GUARD_RESULT(s2n_pq_init());
  73. POSIX_GUARD_RESULT(s2n_tls13_empty_transcripts_init());
  74. POSIX_GUARD_RESULT(s2n_atomic_init());
  75. if (atexit_cleanup) {
  76. POSIX_ENSURE_OK(atexit(s2n_cleanup_atexit), S2N_ERR_ATEXIT);
  77. }
  78. if (getenv("S2N_PRINT_STACKTRACE")) {
  79. s2n_stack_traces_enabled_set(true);
  80. }
  81. initialized = true;
  82. return S2N_SUCCESS;
  83. }
  84. static bool s2n_cleanup_atexit_impl(void)
  85. {
  86. /* all of these should run, regardless of result, but the
  87. * values to need to be consumed to prevent warnings */
  88. /* the configs need to be wiped before resetting the memory callbacks */
  89. s2n_wipe_static_configs();
  90. bool cleaned_up = s2n_result_is_ok(s2n_cipher_suites_cleanup())
  91. && s2n_result_is_ok(s2n_rand_cleanup_thread())
  92. && s2n_result_is_ok(s2n_rand_cleanup())
  93. && s2n_result_is_ok(s2n_locking_cleanup())
  94. && (s2n_mem_cleanup() == S2N_SUCCESS);
  95. initialized = !cleaned_up;
  96. return cleaned_up;
  97. }
  98. int s2n_cleanup(void)
  99. {
  100. /* s2n_cleanup is supposed to be called from each thread before exiting,
  101. * so ensure that whatever clean ups we have here are thread safe */
  102. POSIX_GUARD_RESULT(s2n_rand_cleanup_thread());
  103. /* If this is the main thread and atexit cleanup is disabled,
  104. * perform final cleanup now */
  105. if (pthread_equal(pthread_self(), main_thread) && !atexit_cleanup) {
  106. /* some cleanups are not idempotent (rand_cleanup, mem_cleanup) so protect */
  107. POSIX_ENSURE(initialized, S2N_ERR_NOT_INITIALIZED);
  108. POSIX_ENSURE(s2n_cleanup_atexit_impl(), S2N_ERR_ATEXIT);
  109. }
  110. return 0;
  111. }
  112. static void s2n_cleanup_atexit(void)
  113. {
  114. (void) s2n_cleanup_atexit_impl();
  115. }
  116. bool s2n_is_initialized(void)
  117. {
  118. return initialized;
  119. }