LibsslTLSContext.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  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 "LibsslTLSContext.h"
  36. #include <sstream>
  37. #include <openssl/err.h>
  38. #include <openssl/pkcs12.h>
  39. #include <openssl/bio.h>
  40. #include "LogFactory.h"
  41. #include "Logger.h"
  42. #include "fmt.h"
  43. #include "message.h"
  44. #include "BufferedFile.h"
  45. namespace {
  46. struct bio_deleter {
  47. void operator()(BIO* b)
  48. {
  49. if (b)
  50. BIO_free(b);
  51. }
  52. };
  53. typedef std::unique_ptr<BIO, bio_deleter> bio_t;
  54. struct p12_deleter {
  55. void operator()(PKCS12* p)
  56. {
  57. if (p)
  58. PKCS12_free(p);
  59. }
  60. };
  61. typedef std::unique_ptr<PKCS12, p12_deleter> p12_t;
  62. struct pkey_deleter {
  63. void operator()(EVP_PKEY* x)
  64. {
  65. if (x)
  66. EVP_PKEY_free(x);
  67. }
  68. };
  69. typedef std::unique_ptr<EVP_PKEY, pkey_deleter> pkey_t;
  70. struct x509_deleter {
  71. void operator()(X509* x)
  72. {
  73. if (x)
  74. X509_free(x);
  75. }
  76. };
  77. typedef std::unique_ptr<X509, x509_deleter> x509_t;
  78. struct x509_sk_deleter {
  79. void operator()(STACK_OF(X509) * x)
  80. {
  81. if (x)
  82. sk_X509_pop_free(x, X509_free);
  83. }
  84. };
  85. typedef std::unique_ptr<STACK_OF(X509), x509_sk_deleter> x509_sk_t;
  86. } // namespace
  87. namespace aria2 {
  88. TLSContext* TLSContext::make(TLSSessionSide side, TLSVersion minVer)
  89. {
  90. return new OpenSSLTLSContext(side, minVer);
  91. }
  92. OpenSSLTLSContext::OpenSSLTLSContext(TLSSessionSide side, TLSVersion minVer)
  93. : sslCtx_(nullptr), side_(side), verifyPeer_(true)
  94. {
  95. sslCtx_ = SSL_CTX_new(SSLv23_method());
  96. if (sslCtx_) {
  97. good_ = true;
  98. }
  99. else {
  100. good_ = false;
  101. A2_LOG_ERROR(fmt("SSL_CTX_new() failed. Cause: %s",
  102. ERR_error_string(ERR_get_error(), nullptr)));
  103. return;
  104. }
  105. long ver_opts = 0;
  106. switch (minVer) {
  107. case TLS_PROTO_TLS12:
  108. ver_opts |= SSL_OP_NO_TLSv1_1;
  109. // fall through
  110. case TLS_PROTO_TLS11:
  111. ver_opts |= SSL_OP_NO_TLSv1;
  112. // fall through
  113. case TLS_PROTO_TLS10:
  114. ver_opts |= SSL_OP_NO_SSLv3;
  115. default:
  116. break;
  117. };
  118. // Disable SSLv2 and enable all workarounds for buggy servers
  119. SSL_CTX_set_options(sslCtx_, SSL_OP_ALL | SSL_OP_NO_SSLv2 | ver_opts
  120. #ifdef SSL_OP_SINGLE_ECDH_USE
  121. | SSL_OP_SINGLE_ECDH_USE
  122. #endif // SSL_OP_SINGLE_ECDH_USE
  123. #ifdef SSL_OP_NO_COMPRESSION
  124. | SSL_OP_NO_COMPRESSION
  125. #endif // SSL_OP_NO_COMPRESSION
  126. );
  127. SSL_CTX_set_mode(sslCtx_, SSL_MODE_AUTO_RETRY);
  128. SSL_CTX_set_mode(sslCtx_, SSL_MODE_ENABLE_PARTIAL_WRITE);
  129. #ifdef SSL_MODE_RELEASE_BUFFERS
  130. /* keep memory usage low */
  131. SSL_CTX_set_mode(sslCtx_, SSL_MODE_RELEASE_BUFFERS);
  132. #endif
  133. if (SSL_CTX_set_cipher_list(sslCtx_, "HIGH:!aNULL:!eNULL") == 0) {
  134. good_ = false;
  135. A2_LOG_ERROR(fmt("SSL_CTX_set_cipher_list() failed. Cause: %s",
  136. ERR_error_string(ERR_get_error(), nullptr)));
  137. }
  138. #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
  139. #ifndef OPENSSL_NO_ECDH
  140. auto ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  141. if (ecdh == nullptr) {
  142. A2_LOG_WARN(fmt("Failed to enable ECDHE cipher suites. Cause: %s",
  143. ERR_error_string(ERR_get_error(), nullptr)));
  144. }
  145. else {
  146. SSL_CTX_set_tmp_ecdh(sslCtx_, ecdh);
  147. EC_KEY_free(ecdh);
  148. }
  149. #endif // OPENSSL_NO_ECDH
  150. #endif // OPENSSL_VERSION_NUMBER >= 0x0090800fL
  151. }
  152. OpenSSLTLSContext::~OpenSSLTLSContext() { SSL_CTX_free(sslCtx_); }
  153. bool OpenSSLTLSContext::good() const { return good_; }
  154. bool OpenSSLTLSContext::addCredentialFile(const std::string& certfile,
  155. const std::string& keyfile)
  156. {
  157. if (keyfile.empty()) {
  158. return addP12CredentialFile(certfile);
  159. }
  160. if (SSL_CTX_use_PrivateKey_file(sslCtx_, keyfile.c_str(), SSL_FILETYPE_PEM) !=
  161. 1) {
  162. A2_LOG_ERROR(fmt("Failed to load private key from %s. Cause: %s",
  163. keyfile.c_str(),
  164. ERR_error_string(ERR_get_error(), nullptr)));
  165. return false;
  166. }
  167. if (SSL_CTX_use_certificate_chain_file(sslCtx_, certfile.c_str()) != 1) {
  168. A2_LOG_ERROR(fmt("Failed to load certificate from %s. Cause: %s",
  169. certfile.c_str(),
  170. ERR_error_string(ERR_get_error(), nullptr)));
  171. return false;
  172. }
  173. A2_LOG_INFO(fmt("Credential files(cert=%s, key=%s) were successfully added.",
  174. certfile.c_str(), keyfile.c_str()));
  175. return true;
  176. }
  177. bool OpenSSLTLSContext::addP12CredentialFile(const std::string& p12file)
  178. {
  179. std::stringstream ss;
  180. BufferedFile(p12file.c_str(), BufferedFile::READ).transfer(ss);
  181. auto data = ss.str();
  182. void* ptr = const_cast<char*>(data.c_str());
  183. size_t len = data.length();
  184. bio_t bio(BIO_new_mem_buf(ptr, len));
  185. if (!bio) {
  186. A2_LOG_ERROR("Failed to open PKCS12 file: no memory.");
  187. return false;
  188. }
  189. p12_t p12(d2i_PKCS12_bio(bio.get(), nullptr));
  190. if (!p12) {
  191. if (side_ == TLS_SERVER) {
  192. A2_LOG_ERROR(fmt("Failed to open PKCS12 file: %s. "
  193. "If you meant to use PEM, you'll also have to specify "
  194. "--rpc-private-key. See the manual.",
  195. ERR_error_string(ERR_get_error(), nullptr)));
  196. }
  197. else {
  198. A2_LOG_ERROR(fmt("Failed to open PKCS12 file: %s. "
  199. "If you meant to use PEM, you'll also have to specify "
  200. "--private-key. See the manual.",
  201. ERR_error_string(ERR_get_error(), nullptr)));
  202. }
  203. return false;
  204. }
  205. EVP_PKEY* pkey;
  206. X509* cert;
  207. STACK_OF(X509)* ca = nullptr;
  208. if (!PKCS12_parse(p12.get(), "", &pkey, &cert, &ca)) {
  209. if (side_ == TLS_SERVER) {
  210. A2_LOG_ERROR(fmt("Failed to parse PKCS12 file: %s. "
  211. "If you meant to use PEM, you'll also have to specify "
  212. "--rpc-private-key. See the manual.",
  213. ERR_error_string(ERR_get_error(), nullptr)));
  214. }
  215. else {
  216. A2_LOG_ERROR(fmt("Failed to parse PKCS12 file: %s. "
  217. "If you meant to use PEM, you'll also have to specify "
  218. "--private-key. See the manual.",
  219. ERR_error_string(ERR_get_error(), nullptr)));
  220. }
  221. return false;
  222. }
  223. pkey_t pkey_holder(pkey);
  224. x509_t cert_holder(cert);
  225. x509_sk_t ca_holder(ca);
  226. if (!pkey || !cert) {
  227. A2_LOG_ERROR(fmt("Failed to use PKCS12 file: no pkey or cert %s",
  228. ERR_error_string(ERR_get_error(), nullptr)));
  229. return false;
  230. }
  231. if (!SSL_CTX_use_PrivateKey(sslCtx_, pkey)) {
  232. A2_LOG_ERROR(fmt("Failed to use PKCS12 file pkey: %s",
  233. ERR_error_string(ERR_get_error(), nullptr)));
  234. return false;
  235. }
  236. if (!SSL_CTX_use_certificate(sslCtx_, cert)) {
  237. A2_LOG_ERROR(fmt("Failed to use PKCS12 file cert: %s",
  238. ERR_error_string(ERR_get_error(), nullptr)));
  239. return false;
  240. }
  241. if (ca && sk_X509_num(ca) && !SSL_CTX_add_extra_chain_cert(sslCtx_, ca)) {
  242. A2_LOG_ERROR(fmt("Failed to use PKCS12 file chain: %s",
  243. ERR_error_string(ERR_get_error(), nullptr)));
  244. return false;
  245. }
  246. A2_LOG_INFO("Using certificate and key from PKCS12 file");
  247. return true;
  248. }
  249. bool OpenSSLTLSContext::addSystemTrustedCACerts()
  250. {
  251. if (SSL_CTX_set_default_verify_paths(sslCtx_) != 1) {
  252. A2_LOG_INFO(fmt(MSG_LOADING_SYSTEM_TRUSTED_CA_CERTS_FAILED,
  253. ERR_error_string(ERR_get_error(), nullptr)));
  254. return false;
  255. }
  256. else {
  257. A2_LOG_INFO("System trusted CA certificates were successfully added.");
  258. return true;
  259. }
  260. }
  261. bool OpenSSLTLSContext::addTrustedCACertFile(const std::string& certfile)
  262. {
  263. if (SSL_CTX_load_verify_locations(sslCtx_, certfile.c_str(), nullptr) != 1) {
  264. A2_LOG_ERROR(fmt(MSG_LOADING_TRUSTED_CA_CERT_FAILED, certfile.c_str(),
  265. ERR_error_string(ERR_get_error(), nullptr)));
  266. return false;
  267. }
  268. else {
  269. A2_LOG_INFO("Trusted CA certificates were successfully added.");
  270. return true;
  271. }
  272. }
  273. } // namespace aria2