2
0

ssl_init.c 6.5 KB

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