WinTLSContext.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <cassert>
  37. #include <sstream>
  38. #include "BufferedFile.h"
  39. #include "LogFactory.h"
  40. #include "Logger.h"
  41. #include "fmt.h"
  42. #include "message.h"
  43. #include "util.h"
  44. #ifndef SP_PROT_TLS1_1_CLIENT
  45. # define SP_PROT_TLS1_1_CLIENT 0x00000200
  46. #endif
  47. #ifndef SP_PROT_TLS1_1_SERVER
  48. # define SP_PROT_TLS1_1_SERVER 0x00000100
  49. #endif
  50. #ifndef SP_PROT_TLS1_2_CLIENT
  51. # define SP_PROT_TLS1_2_CLIENT 0x00000800
  52. #endif
  53. #ifndef SP_PROT_TLS1_2_SERVER
  54. # define SP_PROT_TLS1_2_SERVER 0x00000400
  55. #endif
  56. #ifndef SCH_USE_STRONG_CRYPTO
  57. # define SCH_USE_STRONG_CRYPTO 0x00400000
  58. #endif
  59. #define WEAK_CIPHER_BITS 56
  60. #define STRONG_CIPHER_BITS 128
  61. namespace aria2 {
  62. WinTLSContext::WinTLSContext(TLSSessionSide side, TLSVersion ver)
  63. : side_(side), store_(0)
  64. {
  65. memset(&credentials_, 0, sizeof(credentials_));
  66. credentials_.dwVersion = SCHANNEL_CRED_VERSION;
  67. credentials_.grbitEnabledProtocols = 0;
  68. if (side_ == TLS_CLIENT) {
  69. switch (ver) {
  70. case TLS_PROTO_TLS11:
  71. credentials_.grbitEnabledProtocols |= SP_PROT_TLS1_1_CLIENT;
  72. // fall through
  73. case TLS_PROTO_TLS12:
  74. credentials_.grbitEnabledProtocols |= SP_PROT_TLS1_2_CLIENT;
  75. break;
  76. default:
  77. assert(0);
  78. abort();
  79. }
  80. }
  81. else {
  82. switch (ver) {
  83. case TLS_PROTO_TLS11:
  84. credentials_.grbitEnabledProtocols |= SP_PROT_TLS1_1_SERVER;
  85. // fall through
  86. case TLS_PROTO_TLS12:
  87. credentials_.grbitEnabledProtocols |= SP_PROT_TLS1_2_SERVER;
  88. break;
  89. default:
  90. assert(0);
  91. abort();
  92. }
  93. }
  94. // Strong protocol versions: Use a minimum strength, which might be later
  95. // refined using SCH_USE_STRONG_CRYPTO in the flags.
  96. credentials_.dwMinimumCipherStrength = STRONG_CIPHER_BITS;
  97. setVerifyPeer(side_ == TLS_CLIENT);
  98. }
  99. TLSContext* TLSContext::make(TLSSessionSide side, TLSVersion ver)
  100. {
  101. return new WinTLSContext(side, ver);
  102. }
  103. WinTLSContext::~WinTLSContext()
  104. {
  105. if (store_) {
  106. ::CertCloseStore(store_, 0);
  107. store_ = 0;
  108. }
  109. }
  110. bool WinTLSContext::getVerifyPeer() const
  111. {
  112. return credentials_.dwFlags & SCH_CRED_AUTO_CRED_VALIDATION;
  113. }
  114. void WinTLSContext::setVerifyPeer(bool verify)
  115. {
  116. cred_.reset();
  117. // Never automatically push any client or server certs. We'll do cert setup
  118. // ourselves.
  119. credentials_.dwFlags = SCH_CRED_NO_DEFAULT_CREDS;
  120. if (credentials_.dwMinimumCipherStrength > WEAK_CIPHER_BITS) {
  121. // Enable strong crypto if we already set a minimum cipher streams.
  122. // This might actually require even stronger algorithms, which is a good
  123. // thing.
  124. credentials_.dwFlags |= SCH_USE_STRONG_CRYPTO;
  125. }
  126. if (side_ != TLS_CLIENT || !verify) {
  127. // No verification for servers and if user explicitly requested it
  128. credentials_.dwFlags |=
  129. SCH_CRED_MANUAL_CRED_VALIDATION | SCH_CRED_IGNORE_NO_REVOCATION_CHECK |
  130. SCH_CRED_IGNORE_REVOCATION_OFFLINE | SCH_CRED_NO_SERVERNAME_CHECK;
  131. return;
  132. }
  133. // Verify other side's cert chain.
  134. credentials_.dwFlags |= SCH_CRED_AUTO_CRED_VALIDATION |
  135. SCH_CRED_REVOCATION_CHECK_CHAIN |
  136. SCH_CRED_IGNORE_NO_REVOCATION_CHECK;
  137. }
  138. CredHandle* WinTLSContext::getCredHandle()
  139. {
  140. if (cred_) {
  141. return cred_.get();
  142. }
  143. TimeStamp ts;
  144. cred_.reset(new CredHandle());
  145. const CERT_CONTEXT* ctx = nullptr;
  146. if (store_) {
  147. ctx = ::CertEnumCertificatesInStore(store_, nullptr);
  148. if (!ctx) {
  149. throw DL_ABORT_EX("Failed to load certificate");
  150. }
  151. credentials_.cCreds = 1;
  152. credentials_.paCred = &ctx;
  153. }
  154. else {
  155. credentials_.cCreds = 0;
  156. credentials_.paCred = nullptr;
  157. }
  158. SECURITY_STATUS status = ::AcquireCredentialsHandleW(
  159. nullptr, (SEC_WCHAR*)UNISP_NAME_W,
  160. side_ == TLS_CLIENT ? SECPKG_CRED_OUTBOUND : SECPKG_CRED_INBOUND, nullptr,
  161. &credentials_, nullptr, nullptr, cred_.get(), &ts);
  162. if (ctx) {
  163. ::CertFreeCertificateContext(ctx);
  164. }
  165. if (status != SEC_E_OK) {
  166. cred_.reset();
  167. throw DL_ABORT_EX("Failed to initialize WinTLS context handle");
  168. }
  169. return cred_.get();
  170. }
  171. bool WinTLSContext::addCredentialFile(const std::string& certfile,
  172. const std::string& keyfile)
  173. {
  174. std::stringstream ss;
  175. BufferedFile(certfile.c_str(), "rb").transfer(ss);
  176. auto data = ss.str();
  177. CRYPT_DATA_BLOB blob = {(DWORD)data.length(), (BYTE*)data.c_str()};
  178. if (!::PFXIsPFXBlob(&blob)) {
  179. A2_LOG_ERROR("Not a valid PKCS12 file");
  180. return false;
  181. }
  182. HCERTSTORE store =
  183. ::PFXImportCertStore(&blob, L"", CRYPT_EXPORTABLE | CRYPT_USER_KEYSET);
  184. if (!store_) {
  185. store = ::PFXImportCertStore(&blob, nullptr,
  186. CRYPT_EXPORTABLE | CRYPT_USER_KEYSET);
  187. }
  188. if (!store) {
  189. A2_LOG_ERROR("Failed to import PKCS12 store");
  190. return false;
  191. }
  192. auto ctx = ::CertEnumCertificatesInStore(store, nullptr);
  193. if (!ctx) {
  194. A2_LOG_ERROR("PKCS12 file does not contain certificates");
  195. ::CertCloseStore(store, 0);
  196. return false;
  197. }
  198. ::CertFreeCertificateContext(ctx);
  199. if (store_) {
  200. ::CertCloseStore(store_, 0);
  201. }
  202. store_ = store;
  203. cred_.reset();
  204. return true;
  205. }
  206. bool WinTLSContext::addTrustedCACertFile(const std::string& certfile)
  207. {
  208. A2_LOG_WARN("TLS CA bundle files are not supported. "
  209. "The system trust store will be used.");
  210. return false;
  211. }
  212. } // namespace aria2