ssl_init.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2016-2018 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 "internal/thread_once.h"
  16. static int stopped;
  17. static void ssl_library_stop(void);
  18. static CRYPTO_ONCE ssl_base = CRYPTO_ONCE_STATIC_INIT;
  19. static int ssl_base_inited = 0;
  20. DEFINE_RUN_ONCE_STATIC(ossl_init_ssl_base)
  21. {
  22. OSSL_TRACE(INIT, "ossl_init_ssl_base: adding SSL ciphers and digests\n");
  23. #ifndef OPENSSL_NO_DES
  24. EVP_add_cipher(EVP_des_cbc());
  25. EVP_add_cipher(EVP_des_ede3_cbc());
  26. #endif
  27. #ifndef OPENSSL_NO_IDEA
  28. EVP_add_cipher(EVP_idea_cbc());
  29. #endif
  30. #ifndef OPENSSL_NO_RC4
  31. EVP_add_cipher(EVP_rc4());
  32. # ifndef OPENSSL_NO_MD5
  33. EVP_add_cipher(EVP_rc4_hmac_md5());
  34. # endif
  35. #endif
  36. #ifndef OPENSSL_NO_RC2
  37. EVP_add_cipher(EVP_rc2_cbc());
  38. /*
  39. * Not actually used for SSL/TLS but this makes PKCS#12 work if an
  40. * application only calls SSL_library_init().
  41. */
  42. EVP_add_cipher(EVP_rc2_40_cbc());
  43. #endif
  44. EVP_add_cipher(EVP_aes_128_cbc());
  45. EVP_add_cipher(EVP_aes_192_cbc());
  46. EVP_add_cipher(EVP_aes_256_cbc());
  47. EVP_add_cipher(EVP_aes_128_gcm());
  48. EVP_add_cipher(EVP_aes_256_gcm());
  49. EVP_add_cipher(EVP_aes_128_ccm());
  50. EVP_add_cipher(EVP_aes_256_ccm());
  51. EVP_add_cipher(EVP_aes_128_cbc_hmac_sha1());
  52. EVP_add_cipher(EVP_aes_256_cbc_hmac_sha1());
  53. EVP_add_cipher(EVP_aes_128_cbc_hmac_sha256());
  54. EVP_add_cipher(EVP_aes_256_cbc_hmac_sha256());
  55. #ifndef OPENSSL_NO_ARIA
  56. EVP_add_cipher(EVP_aria_128_gcm());
  57. EVP_add_cipher(EVP_aria_256_gcm());
  58. #endif
  59. #ifndef OPENSSL_NO_CAMELLIA
  60. EVP_add_cipher(EVP_camellia_128_cbc());
  61. EVP_add_cipher(EVP_camellia_256_cbc());
  62. #endif
  63. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  64. EVP_add_cipher(EVP_chacha20_poly1305());
  65. #endif
  66. #ifndef OPENSSL_NO_SEED
  67. EVP_add_cipher(EVP_seed_cbc());
  68. #endif
  69. #ifndef OPENSSL_NO_MD5
  70. EVP_add_digest(EVP_md5());
  71. EVP_add_digest_alias(SN_md5, "ssl3-md5");
  72. EVP_add_digest(EVP_md5_sha1());
  73. #endif
  74. EVP_add_digest(EVP_sha1()); /* RSA with sha1 */
  75. EVP_add_digest_alias(SN_sha1, "ssl3-sha1");
  76. EVP_add_digest_alias(SN_sha1WithRSAEncryption, SN_sha1WithRSA);
  77. EVP_add_digest(EVP_sha224());
  78. EVP_add_digest(EVP_sha256());
  79. EVP_add_digest(EVP_sha384());
  80. EVP_add_digest(EVP_sha512());
  81. #ifndef OPENSSL_NO_COMP
  82. OSSL_TRACE(INIT, "ossl_init_ssl_base: "
  83. "SSL_COMP_get_compression_methods()\n");
  84. /*
  85. * This will initialise the built-in compression algorithms. The value
  86. * returned is a STACK_OF(SSL_COMP), but that can be discarded safely
  87. */
  88. SSL_COMP_get_compression_methods();
  89. #endif
  90. /* initialize cipher/digest methods table */
  91. if (!ssl_load_ciphers())
  92. return 0;
  93. OSSL_TRACE(INIT,"ossl_init_ssl_base: SSL_add_ssl_module()\n");
  94. /*
  95. * We ignore an error return here. Not much we can do - but not that bad
  96. * either. We can still safely continue.
  97. */
  98. OPENSSL_atexit(ssl_library_stop);
  99. ssl_base_inited = 1;
  100. return 1;
  101. }
  102. static CRYPTO_ONCE ssl_strings = CRYPTO_ONCE_STATIC_INIT;
  103. static int ssl_strings_inited = 0;
  104. DEFINE_RUN_ONCE_STATIC(ossl_init_load_ssl_strings)
  105. {
  106. /*
  107. * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
  108. * pulling in all the error strings during static linking
  109. */
  110. #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
  111. OSSL_TRACE(INIT, "ossl_init_load_ssl_strings: ERR_load_SSL_strings()\n");
  112. ERR_load_SSL_strings();
  113. ssl_strings_inited = 1;
  114. #endif
  115. return 1;
  116. }
  117. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_ssl_strings,
  118. ossl_init_load_ssl_strings)
  119. {
  120. /* Do nothing in this case */
  121. return 1;
  122. }
  123. static void ssl_library_stop(void)
  124. {
  125. /* Might be explicitly called and also by atexit */
  126. if (stopped)
  127. return;
  128. stopped = 1;
  129. if (ssl_base_inited) {
  130. #ifndef OPENSSL_NO_COMP
  131. OSSL_TRACE(INIT, "ssl_library_stop: "
  132. "ssl_comp_free_compression_methods_int()\n");
  133. ssl_comp_free_compression_methods_int();
  134. #endif
  135. }
  136. if (ssl_strings_inited) {
  137. OSSL_TRACE(INIT, "ssl_library_stop: err_free_strings_int()\n");
  138. /*
  139. * If both crypto and ssl error strings are inited we will end up
  140. * calling err_free_strings_int() twice - but that's ok. The second
  141. * time will be a no-op. It's easier to do that than to try and track
  142. * between the two libraries whether they have both been inited.
  143. */
  144. err_free_strings_int();
  145. }
  146. }
  147. /*
  148. * If this function is called with a non NULL settings value then it must be
  149. * called prior to any threads making calls to any OpenSSL functions,
  150. * i.e. passing a non-null settings value is assumed to be single-threaded.
  151. */
  152. int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS * settings)
  153. {
  154. static int stoperrset = 0;
  155. if (stopped) {
  156. if (!stoperrset) {
  157. /*
  158. * We only ever set this once to avoid getting into an infinite
  159. * loop where the error system keeps trying to init and fails so
  160. * sets an error etc
  161. */
  162. stoperrset = 1;
  163. SSLerr(SSL_F_OPENSSL_INIT_SSL, ERR_R_INIT_FAIL);
  164. }
  165. return 0;
  166. }
  167. opts |= OPENSSL_INIT_ADD_ALL_CIPHERS
  168. | OPENSSL_INIT_ADD_ALL_DIGESTS;
  169. #ifndef OPENSSL_NO_AUTOLOAD_CONFIG
  170. if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) == 0)
  171. opts |= OPENSSL_INIT_LOAD_CONFIG;
  172. #endif
  173. if (!OPENSSL_init_crypto(opts, settings))
  174. return 0;
  175. if (!RUN_ONCE(&ssl_base, ossl_init_ssl_base))
  176. return 0;
  177. if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS)
  178. && !RUN_ONCE_ALT(&ssl_strings, ossl_init_no_load_ssl_strings,
  179. ossl_init_load_ssl_strings))
  180. return 0;
  181. if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS)
  182. && !RUN_ONCE(&ssl_strings, ossl_init_load_ssl_strings))
  183. return 0;
  184. return 1;
  185. }