2
0

ssl_init.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2016-2022 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. DEFINE_RUN_ONCE_STATIC(ossl_init_load_ssl_strings)
  111. {
  112. /*
  113. * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
  114. * pulling in all the error strings during static linking
  115. */
  116. #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
  117. # ifdef OPENSSL_INIT_DEBUG
  118. fprintf(stderr, "OPENSSL_INIT: ossl_init_load_ssl_strings: "
  119. "ERR_load_SSL_strings()\n");
  120. # endif
  121. ERR_load_SSL_strings();
  122. #endif
  123. return 1;
  124. }
  125. DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_ssl_strings,
  126. ossl_init_load_ssl_strings)
  127. {
  128. /* Do nothing in this case */
  129. return 1;
  130. }
  131. static void ssl_library_stop(void)
  132. {
  133. /* Might be explicitly called and also by atexit */
  134. if (stopped)
  135. return;
  136. stopped = 1;
  137. if (ssl_base_inited) {
  138. #ifndef OPENSSL_NO_COMP
  139. # ifdef OPENSSL_INIT_DEBUG
  140. fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: "
  141. "ssl_comp_free_compression_methods_int()\n");
  142. # endif
  143. ssl_comp_free_compression_methods_int();
  144. #endif
  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. }