WinTLSContext.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 Nils Maier
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "WinTLSContext.h"
  36. #include <sstream>
  37. #include "BufferedFile.h"
  38. #include "LogFactory.h"
  39. #include "Logger.h"
  40. #include "fmt.h"
  41. #include "message.h"
  42. #include "util.h"
  43. #ifndef SP_PROT_TLS1_1_CLIENT
  44. #define SP_PROT_TLS1_1_CLIENT 0x00000200
  45. #endif
  46. #ifndef SP_PROT_TLS1_1_SERVER
  47. #define SP_PROT_TLS1_1_SERVER 0x00000100
  48. #endif
  49. #ifndef SP_PROT_TLS1_2_CLIENT
  50. #define SP_PROT_TLS1_2_CLIENT 0x00000800
  51. #endif
  52. #ifndef SP_PROT_TLS1_2_SERVER
  53. #define SP_PROT_TLS1_2_SERVER 0x00000400
  54. #endif
  55. #ifndef SCH_USE_STRONG_CRYPTO
  56. #define SCH_USE_STRONG_CRYPTO 0x00400000
  57. #endif
  58. #define WEAK_CIPHER_BITS 56
  59. #define STRONG_CIPHER_BITS 128
  60. namespace aria2 {
  61. WinTLSContext::WinTLSContext(TLSSessionSide side, TLSVersion ver)
  62. : side_(side), store_(0)
  63. {
  64. memset(&credentials_, 0, sizeof(credentials_));
  65. credentials_.dwVersion = SCHANNEL_CRED_VERSION;
  66. credentials_.grbitEnabledProtocols = 0;
  67. if (side_ == TLS_CLIENT) {
  68. switch (ver) {
  69. case TLS_PROTO_SSL3:
  70. credentials_.grbitEnabledProtocols |= SP_PROT_SSL3_CLIENT;
  71. // fall through
  72. case TLS_PROTO_TLS10:
  73. credentials_.grbitEnabledProtocols |= SP_PROT_TLS1_CLIENT;
  74. // fall through
  75. case TLS_PROTO_TLS11:
  76. credentials_.grbitEnabledProtocols |= SP_PROT_TLS1_1_CLIENT;
  77. // fall through
  78. case TLS_PROTO_TLS12:
  79. credentials_.grbitEnabledProtocols |= SP_PROT_TLS1_2_CLIENT;
  80. // fall through
  81. default:
  82. break;
  83. }
  84. }
  85. else {
  86. switch (ver) {
  87. case TLS_PROTO_SSL3:
  88. credentials_.grbitEnabledProtocols |= SP_PROT_SSL3_SERVER;
  89. // fall through
  90. case TLS_PROTO_TLS10:
  91. credentials_.grbitEnabledProtocols |= SP_PROT_TLS1_SERVER;
  92. // fall through
  93. case TLS_PROTO_TLS11:
  94. credentials_.grbitEnabledProtocols |= SP_PROT_TLS1_1_SERVER;
  95. // fall through
  96. case TLS_PROTO_TLS12:
  97. credentials_.grbitEnabledProtocols |= SP_PROT_TLS1_2_SERVER;
  98. // fall through
  99. default:
  100. break;
  101. }
  102. }
  103. switch (ver) {
  104. case TLS_PROTO_SSL3:
  105. // User explicitly wanted SSLv3 and therefore weak ciphers.
  106. credentials_.dwMinimumCipherStrength = WEAK_CIPHER_BITS;
  107. break;
  108. default:
  109. // Strong protocol versions: Use a minimum strength, which might be later
  110. // refined using SCH_USE_STRONG_CRYPTO in the flags.
  111. credentials_.dwMinimumCipherStrength = STRONG_CIPHER_BITS;
  112. }
  113. setVerifyPeer(side_ == TLS_CLIENT);
  114. }
  115. TLSContext* TLSContext::make(TLSSessionSide side, TLSVersion ver)
  116. {
  117. return new WinTLSContext(side, ver);
  118. }
  119. WinTLSContext::~WinTLSContext()
  120. {
  121. if (store_) {
  122. ::CertCloseStore(store_, 0);
  123. store_ = 0;
  124. }
  125. }
  126. bool WinTLSContext::getVerifyPeer() const
  127. {
  128. return credentials_.dwFlags & SCH_CRED_AUTO_CRED_VALIDATION;
  129. }
  130. void WinTLSContext::setVerifyPeer(bool verify)
  131. {
  132. cred_.reset();
  133. // Never automatically push any client or server certs. We'll do cert setup
  134. // ourselves.
  135. credentials_.dwFlags = SCH_CRED_NO_DEFAULT_CREDS;
  136. if (credentials_.dwMinimumCipherStrength > WEAK_CIPHER_BITS) {
  137. // Enable strong crypto if we already set a minimum cipher streams.
  138. // This might actually require even stronger algorithms, which is a good
  139. // thing.
  140. credentials_.dwFlags |= SCH_USE_STRONG_CRYPTO;
  141. }
  142. if (side_ != TLS_CLIENT || !verify) {
  143. // No verification for servers and if user explicitly requested it
  144. credentials_.dwFlags |=
  145. SCH_CRED_MANUAL_CRED_VALIDATION | SCH_CRED_IGNORE_NO_REVOCATION_CHECK |
  146. SCH_CRED_IGNORE_REVOCATION_OFFLINE | SCH_CRED_NO_SERVERNAME_CHECK;
  147. return;
  148. }
  149. // Verify other side's cert chain.
  150. credentials_.dwFlags |= SCH_CRED_AUTO_CRED_VALIDATION |
  151. SCH_CRED_REVOCATION_CHECK_CHAIN |
  152. SCH_CRED_IGNORE_NO_REVOCATION_CHECK;
  153. }
  154. CredHandle* WinTLSContext::getCredHandle()
  155. {
  156. if (cred_) {
  157. return cred_.get();
  158. }
  159. TimeStamp ts;
  160. cred_.reset(new CredHandle());
  161. const CERT_CONTEXT* ctx = nullptr;
  162. if (store_) {
  163. ctx = ::CertEnumCertificatesInStore(store_, nullptr);
  164. if (!ctx) {
  165. throw DL_ABORT_EX("Failed to load certificate");
  166. }
  167. credentials_.cCreds = 1;
  168. credentials_.paCred = &ctx;
  169. }
  170. else {
  171. credentials_.cCreds = 0;
  172. credentials_.paCred = nullptr;
  173. }
  174. SECURITY_STATUS status = ::AcquireCredentialsHandleW(
  175. nullptr, (SEC_WCHAR*)UNISP_NAME_W,
  176. side_ == TLS_CLIENT ? SECPKG_CRED_OUTBOUND : SECPKG_CRED_INBOUND, nullptr,
  177. &credentials_, nullptr, nullptr, cred_.get(), &ts);
  178. if (ctx) {
  179. ::CertFreeCertificateContext(ctx);
  180. }
  181. if (status != SEC_E_OK) {
  182. cred_.reset();
  183. throw DL_ABORT_EX("Failed to initialize WinTLS context handle");
  184. }
  185. return cred_.get();
  186. }
  187. bool WinTLSContext::addCredentialFile(const std::string& certfile,
  188. const std::string& keyfile)
  189. {
  190. std::stringstream ss;
  191. BufferedFile(certfile.c_str(), "rb").transfer(ss);
  192. auto data = ss.str();
  193. CRYPT_DATA_BLOB blob = {(DWORD)data.length(), (BYTE*)data.c_str()};
  194. if (!::PFXIsPFXBlob(&blob)) {
  195. A2_LOG_ERROR("Not a valid PKCS12 file");
  196. return false;
  197. }
  198. HCERTSTORE store =
  199. ::PFXImportCertStore(&blob, L"", CRYPT_EXPORTABLE | CRYPT_USER_KEYSET);
  200. if (!store_) {
  201. store = ::PFXImportCertStore(&blob, nullptr,
  202. CRYPT_EXPORTABLE | CRYPT_USER_KEYSET);
  203. }
  204. if (!store) {
  205. A2_LOG_ERROR("Failed to import PKCS12 store");
  206. return false;
  207. }
  208. auto ctx = ::CertEnumCertificatesInStore(store, nullptr);
  209. if (!ctx) {
  210. A2_LOG_ERROR("PKCS12 file does not contain certificates");
  211. ::CertCloseStore(store, 0);
  212. return false;
  213. }
  214. ::CertFreeCertificateContext(ctx);
  215. if (store_) {
  216. ::CertCloseStore(store_, 0);
  217. }
  218. store_ = store;
  219. cred_.reset();
  220. return true;
  221. }
  222. bool WinTLSContext::addTrustedCACertFile(const std::string& certfile)
  223. {
  224. A2_LOG_WARN("TLS CA bundle files are not supported. "
  225. "The system trust store will be used.");
  226. return false;
  227. }
  228. } // namespace aria2