ssl_init.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright 2016-2019 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_local.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_ALT(ossl_init_no_load_ssl_strings,
  128. ossl_init_load_ssl_strings)
  129. {
  130. /* Do nothing in this case */
  131. return 1;
  132. }
  133. static void ssl_library_stop(void)
  134. {
  135. /* Might be explicitly called and also by atexit */
  136. if (stopped)
  137. return;
  138. stopped = 1;
  139. if (ssl_base_inited) {
  140. #ifndef OPENSSL_NO_COMP
  141. # ifdef OPENSSL_INIT_DEBUG
  142. fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: "
  143. "ssl_comp_free_compression_methods_int()\n");
  144. # endif
  145. ssl_comp_free_compression_methods_int();
  146. #endif
  147. }
  148. if (ssl_strings_inited) {
  149. #ifdef OPENSSL_INIT_DEBUG
  150. fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: "
  151. "err_free_strings_int()\n");
  152. #endif
  153. /*
  154. * If both crypto and ssl error strings are inited we will end up
  155. * calling err_free_strings_int() twice - but that's ok. The second
  156. * time will be a no-op. It's easier to do that than to try and track
  157. * between the two libraries whether they have both been inited.
  158. */
  159. err_free_strings_int();
  160. }
  161. }
  162. /*
  163. * If this function is called with a non NULL settings value then it must be
  164. * called prior to any threads making calls to any OpenSSL functions,
  165. * i.e. passing a non-null settings value is assumed to be single-threaded.
  166. */
  167. int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS * settings)
  168. {
  169. static int stoperrset = 0;
  170. if (stopped) {
  171. if (!stoperrset) {
  172. /*
  173. * We only ever set this once to avoid getting into an infinite
  174. * loop where the error system keeps trying to init and fails so
  175. * sets an error etc
  176. */
  177. stoperrset = 1;
  178. SSLerr(SSL_F_OPENSSL_INIT_SSL, ERR_R_INIT_FAIL);
  179. }
  180. return 0;
  181. }
  182. opts |= OPENSSL_INIT_ADD_ALL_CIPHERS
  183. | OPENSSL_INIT_ADD_ALL_DIGESTS;
  184. #ifndef OPENSSL_NO_AUTOLOAD_CONFIG
  185. if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) == 0)
  186. opts |= OPENSSL_INIT_LOAD_CONFIG;
  187. #endif
  188. if (!OPENSSL_init_crypto(opts, 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_ALT(&ssl_strings, ossl_init_no_load_ssl_strings,
  194. ossl_init_load_ssl_strings))
  195. return 0;
  196. if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS)
  197. && !RUN_ONCE(&ssl_strings, ossl_init_load_ssl_strings))
  198. return 0;
  199. return 1;
  200. }