ssl_init.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2016-2020 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. #include "e_os.h"
  10. #include "internal/err.h"
  11. #include <openssl/crypto.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/trace.h>
  14. #include "ssl_local.h"
  15. #include "sslerr.h"
  16. #include "internal/thread_once.h"
  17. static int stopped;
  18. static void ssl_library_stop(void);
  19. static CRYPTO_ONCE ssl_base = CRYPTO_ONCE_STATIC_INIT;
  20. static int ssl_base_inited = 0;
  21. DEFINE_RUN_ONCE_STATIC(ossl_init_ssl_base)
  22. {
  23. #ifndef OPENSSL_NO_COMP
  24. OSSL_TRACE(INIT, "ossl_init_ssl_base: "
  25. "SSL_COMP_get_compression_methods()\n");
  26. /*
  27. * This will initialise the built-in compression algorithms. The value
  28. * returned is a STACK_OF(SSL_COMP), but that can be discarded safely
  29. */
  30. SSL_COMP_get_compression_methods();
  31. #endif
  32. ssl_sort_cipher_list();
  33. OSSL_TRACE(INIT,"ossl_init_ssl_base: SSL_add_ssl_module()\n");
  34. /*
  35. * We ignore an error return here. Not much we can do - but not that bad
  36. * either. We can still safely continue.
  37. */
  38. OPENSSL_atexit(ssl_library_stop);
  39. ssl_base_inited = 1;
  40. return 1;
  41. }
  42. static CRYPTO_ONCE ssl_strings = CRYPTO_ONCE_STATIC_INIT;
  43. static int ssl_strings_inited = 0;
  44. DEFINE_RUN_ONCE_STATIC(ossl_init_load_ssl_strings)
  45. {
  46. /*
  47. * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
  48. * pulling in all the error strings during static linking
  49. */
  50. #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
  51. OSSL_TRACE(INIT, "ossl_init_load_ssl_strings: ossl_err_load_SSL_strings()\n");
  52. ossl_err_load_SSL_strings();
  53. ssl_strings_inited = 1;
  54. #endif
  55. return 1;
  56. }
  57. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_ssl_strings,
  58. ossl_init_load_ssl_strings)
  59. {
  60. /* Do nothing in this case */
  61. return 1;
  62. }
  63. static void ssl_library_stop(void)
  64. {
  65. /* Might be explicitly called and also by atexit */
  66. if (stopped)
  67. return;
  68. stopped = 1;
  69. if (ssl_base_inited) {
  70. #ifndef OPENSSL_NO_COMP
  71. OSSL_TRACE(INIT, "ssl_library_stop: "
  72. "ssl_comp_free_compression_methods_int()\n");
  73. ssl_comp_free_compression_methods_int();
  74. #endif
  75. }
  76. if (ssl_strings_inited) {
  77. OSSL_TRACE(INIT, "ssl_library_stop: err_free_strings_int()\n");
  78. /*
  79. * If both crypto and ssl error strings are inited we will end up
  80. * calling err_free_strings_int() twice - but that's ok. The second
  81. * time will be a no-op. It's easier to do that than to try and track
  82. * between the two libraries whether they have both been inited.
  83. */
  84. err_free_strings_int();
  85. }
  86. }
  87. /*
  88. * If this function is called with a non NULL settings value then it must be
  89. * called prior to any threads making calls to any OpenSSL functions,
  90. * i.e. passing a non-null settings value is assumed to be single-threaded.
  91. */
  92. int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS * settings)
  93. {
  94. static int stoperrset = 0;
  95. if (stopped) {
  96. 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. stoperrset = 1;
  103. ERR_raise(ERR_LIB_SSL, ERR_R_INIT_FAIL);
  104. }
  105. return 0;
  106. }
  107. opts |= OPENSSL_INIT_ADD_ALL_CIPHERS
  108. | OPENSSL_INIT_ADD_ALL_DIGESTS;
  109. #ifndef OPENSSL_NO_AUTOLOAD_CONFIG
  110. if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) == 0)
  111. opts |= OPENSSL_INIT_LOAD_CONFIG;
  112. #endif
  113. if (!OPENSSL_init_crypto(opts, settings))
  114. return 0;
  115. if (!RUN_ONCE(&ssl_base, ossl_init_ssl_base))
  116. return 0;
  117. if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS)
  118. && !RUN_ONCE_ALT(&ssl_strings, ossl_init_no_load_ssl_strings,
  119. ossl_init_load_ssl_strings))
  120. return 0;
  121. if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS)
  122. && !RUN_ONCE(&ssl_strings, ossl_init_load_ssl_strings))
  123. return 0;
  124. return 1;
  125. }