LibgnutlsTLSSession.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 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 "LibgnutlsTLSSession.h"
  36. #include <gnutls/x509.h>
  37. #include "TLSContext.h"
  38. #include "util.h"
  39. #include "SocketCore.h"
  40. namespace {
  41. using namespace aria2;
  42. TLSVersion getProtocolFromSession(gnutls_session_t& session)
  43. {
  44. auto proto = gnutls_protocol_get_version(session);
  45. switch (proto) {
  46. case GNUTLS_SSL3:
  47. return TLS_PROTO_SSL3;
  48. case GNUTLS_TLS1_0:
  49. return TLS_PROTO_TLS10;
  50. case GNUTLS_TLS1_1:
  51. return TLS_PROTO_TLS11;
  52. case GNUTLS_TLS1_2:
  53. return TLS_PROTO_TLS12;
  54. default:
  55. return TLS_PROTO_NONE;
  56. }
  57. }
  58. } // namespace
  59. namespace aria2 {
  60. TLSSession* TLSSession::make(TLSContext* ctx)
  61. {
  62. return new GnuTLSSession(static_cast<GnuTLSContext*>(ctx));
  63. }
  64. GnuTLSSession::GnuTLSSession(GnuTLSContext* tlsContext)
  65. : sslSession_(nullptr), tlsContext_(tlsContext), rv_(0)
  66. {
  67. }
  68. GnuTLSSession::~GnuTLSSession()
  69. {
  70. if (sslSession_) {
  71. gnutls_deinit(sslSession_);
  72. }
  73. }
  74. // GnuTLS version 3.1.3 - 3.1.18 and 3.2.0 - 3.2.8, inclusive, have a
  75. // bug which makes SSL/TLS handshake fail if OCSP status extension is
  76. // enabled and non-blocking socket is used. To workaround this bug,
  77. // for these versions of GnuTLS, we disable OCSP status extension. We
  78. // expect that upcoming (at the time of this writing) 3.1.19 and 3.2.9
  79. // will fix this bug. See
  80. // http://lists.gnutls.org/pipermail/gnutls-devel/2014-January/006679.html
  81. // for details.
  82. #if (GNUTLS_VERSION_NUMBER >= 0x030103 && \
  83. GNUTLS_VERSION_NUMBER <= 0x030112) || \
  84. (GNUTLS_VERSION_NUMBER >= 0x030200 && GNUTLS_VERSION_NUMBER <= 0x030208)
  85. #define A2_DISABLE_OCSP 1
  86. #endif
  87. int GnuTLSSession::init(sock_t sockfd)
  88. {
  89. #if GNUTLS_VERSION_NUMBER >= 0x030000
  90. unsigned int flags =
  91. tlsContext_->getSide() == TLS_CLIENT ? GNUTLS_CLIENT : GNUTLS_SERVER;
  92. #ifdef A2_DISABLE_OCSP
  93. if (tlsContext_->getSide() == TLS_CLIENT) {
  94. flags |= GNUTLS_NO_EXTENSIONS;
  95. }
  96. #endif // A2_DISABLE_OCSP
  97. rv_ = gnutls_init(&sslSession_, flags);
  98. #else // GNUTLS_VERSION_NUMBER >= 0x030000
  99. rv_ = gnutls_init(&sslSession_, tlsContext_->getSide() == TLS_CLIENT
  100. ? GNUTLS_CLIENT
  101. : GNUTLS_SERVER);
  102. #endif // GNUTLS_VERSION_NUMBER >= 0x030000
  103. if (rv_ != GNUTLS_E_SUCCESS) {
  104. return TLS_ERR_ERROR;
  105. }
  106. #ifdef A2_DISABLE_OCSP
  107. if (tlsContext_->getSide() == TLS_CLIENT) {
  108. // Enable session ticket extension manually because of
  109. // GNUTLS_NO_EXTENSIONS.
  110. rv_ = gnutls_session_ticket_enable_client(sslSession_);
  111. if (rv_ != GNUTLS_E_SUCCESS) {
  112. return TLS_ERR_ERROR;
  113. }
  114. }
  115. #endif // A2_DISABLE_OCSP
  116. // It seems err is not error message, but the argument string
  117. // which causes syntax error.
  118. const char* err;
  119. std::string pri = "SECURE128:+SIGN-RSA-SHA1";
  120. switch (tlsContext_->getMinTLSVersion()) {
  121. case TLS_PROTO_TLS12:
  122. pri += ":-VERS-TLS1.1";
  123. // fall through
  124. case TLS_PROTO_TLS11:
  125. pri += ":-VERS-TLS1.0";
  126. // fall through
  127. case TLS_PROTO_TLS10:
  128. pri += ":-VERS-SSL3.0";
  129. default:
  130. break;
  131. };
  132. rv_ = gnutls_priority_set_direct(sslSession_, pri.c_str(), &err);
  133. if (rv_ != GNUTLS_E_SUCCESS) {
  134. return TLS_ERR_ERROR;
  135. }
  136. // put the x509 credentials to the current session
  137. rv_ = gnutls_credentials_set(sslSession_, GNUTLS_CRD_CERTIFICATE,
  138. tlsContext_->getCertCred());
  139. if (rv_ != GNUTLS_E_SUCCESS) {
  140. return TLS_ERR_ERROR;
  141. }
  142. // TODO Consider to use gnutls_transport_set_int() for GNUTLS 3.1.9
  143. // or later
  144. gnutls_transport_set_ptr(sslSession_,
  145. (gnutls_transport_ptr_t)(ptrdiff_t)sockfd);
  146. return TLS_ERR_OK;
  147. }
  148. int GnuTLSSession::setSNIHostname(const std::string& hostname)
  149. {
  150. // TLS extensions: SNI
  151. rv_ = gnutls_server_name_set(sslSession_, GNUTLS_NAME_DNS, hostname.c_str(),
  152. hostname.size());
  153. if (rv_ != GNUTLS_E_SUCCESS) {
  154. return TLS_ERR_ERROR;
  155. }
  156. return TLS_ERR_OK;
  157. }
  158. int GnuTLSSession::closeConnection()
  159. {
  160. rv_ = gnutls_bye(sslSession_, GNUTLS_SHUT_WR);
  161. if (rv_ == GNUTLS_E_SUCCESS) {
  162. return TLS_ERR_OK;
  163. }
  164. else if (rv_ == GNUTLS_E_AGAIN || rv_ == GNUTLS_E_INTERRUPTED) {
  165. return TLS_ERR_WOULDBLOCK;
  166. }
  167. else {
  168. return TLS_ERR_ERROR;
  169. }
  170. }
  171. int GnuTLSSession::checkDirection()
  172. {
  173. int direction = gnutls_record_get_direction(sslSession_);
  174. return direction == 0 ? TLS_WANT_READ : TLS_WANT_WRITE;
  175. }
  176. ssize_t GnuTLSSession::writeData(const void* data, size_t len)
  177. {
  178. while ((rv_ = gnutls_record_send(sslSession_, data, len)) ==
  179. GNUTLS_E_INTERRUPTED)
  180. ;
  181. if (rv_ >= 0) {
  182. ssize_t ret = rv_;
  183. rv_ = 0;
  184. return ret;
  185. }
  186. else if (rv_ == GNUTLS_E_AGAIN || rv_ == GNUTLS_E_INTERRUPTED) {
  187. return TLS_ERR_WOULDBLOCK;
  188. }
  189. else {
  190. return TLS_ERR_ERROR;
  191. }
  192. }
  193. ssize_t GnuTLSSession::readData(void* data, size_t len)
  194. {
  195. while ((rv_ = gnutls_record_recv(sslSession_, data, len)) ==
  196. GNUTLS_E_INTERRUPTED)
  197. ;
  198. if (rv_ >= 0) {
  199. ssize_t ret = rv_;
  200. rv_ = 0;
  201. return ret;
  202. }
  203. else if (rv_ == GNUTLS_E_AGAIN || rv_ == GNUTLS_E_INTERRUPTED) {
  204. return TLS_ERR_WOULDBLOCK;
  205. }
  206. else {
  207. return TLS_ERR_ERROR;
  208. }
  209. }
  210. int GnuTLSSession::tlsConnect(const std::string& hostname, TLSVersion& version,
  211. std::string& handshakeErr)
  212. {
  213. handshakeErr = "";
  214. for (;;) {
  215. rv_ = gnutls_handshake(sslSession_);
  216. if (rv_ == GNUTLS_E_SUCCESS) {
  217. break;
  218. }
  219. if (rv_ == GNUTLS_E_AGAIN || rv_ == GNUTLS_E_INTERRUPTED) {
  220. return TLS_ERR_WOULDBLOCK;
  221. }
  222. if (gnutls_error_is_fatal(rv_)) {
  223. return TLS_ERR_ERROR;
  224. }
  225. }
  226. if (tlsContext_->getVerifyPeer()) {
  227. // verify peer
  228. unsigned int status;
  229. rv_ = gnutls_certificate_verify_peers2(sslSession_, &status);
  230. if (rv_ != GNUTLS_E_SUCCESS) {
  231. return TLS_ERR_ERROR;
  232. }
  233. if (status) {
  234. handshakeErr = "";
  235. if (status & GNUTLS_CERT_INVALID) {
  236. handshakeErr += " `not signed by known authorities or invalid'";
  237. }
  238. if (status & GNUTLS_CERT_REVOKED) {
  239. handshakeErr += " `revoked by its CA'";
  240. }
  241. if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) {
  242. handshakeErr += " `issuer is not known'";
  243. }
  244. // TODO should check GNUTLS_CERT_SIGNER_NOT_CA ?
  245. if (status & GNUTLS_CERT_INSECURE_ALGORITHM) {
  246. handshakeErr += " `insecure algorithm'";
  247. }
  248. if (status & GNUTLS_CERT_NOT_ACTIVATED) {
  249. handshakeErr += " `not activated yet'";
  250. }
  251. if (status & GNUTLS_CERT_EXPIRED) {
  252. handshakeErr += " `expired'";
  253. }
  254. // TODO Add GNUTLS_CERT_SIGNATURE_FAILURE here
  255. if (!handshakeErr.empty()) {
  256. return TLS_ERR_ERROR;
  257. }
  258. }
  259. // certificate type: only X509 is allowed.
  260. if (gnutls_certificate_type_get(sslSession_) != GNUTLS_CRT_X509) {
  261. handshakeErr = "certificate type must be X509";
  262. return TLS_ERR_ERROR;
  263. }
  264. unsigned int peerCertsLength;
  265. const gnutls_datum_t* peerCerts;
  266. peerCerts = gnutls_certificate_get_peers(sslSession_, &peerCertsLength);
  267. if (!peerCerts || peerCertsLength == 0) {
  268. handshakeErr = "certificate not found";
  269. return TLS_ERR_ERROR;
  270. }
  271. gnutls_x509_crt_t cert;
  272. rv_ = gnutls_x509_crt_init(&cert);
  273. if (rv_ != GNUTLS_E_SUCCESS) {
  274. return TLS_ERR_ERROR;
  275. }
  276. std::unique_ptr<std::remove_pointer<gnutls_x509_crt_t>::type,
  277. decltype(&gnutls_x509_crt_deinit)>
  278. certDeleter(cert, gnutls_x509_crt_deinit);
  279. rv_ = gnutls_x509_crt_import(cert, &peerCerts[0], GNUTLS_X509_FMT_DER);
  280. if (rv_ != GNUTLS_E_SUCCESS) {
  281. return TLS_ERR_ERROR;
  282. }
  283. std::string commonName;
  284. std::vector<std::string> dnsNames;
  285. std::vector<std::string> ipAddrs;
  286. int ret = 0;
  287. char altName[256];
  288. size_t altNameLen;
  289. for (int i = 0; !(ret < 0); ++i) {
  290. altNameLen = sizeof(altName);
  291. ret = gnutls_x509_crt_get_subject_alt_name(cert, i, altName, &altNameLen,
  292. nullptr);
  293. if (ret == GNUTLS_SAN_DNSNAME) {
  294. dnsNames.push_back(std::string(altName, altNameLen));
  295. }
  296. else if (ret == GNUTLS_SAN_IPADDRESS) {
  297. ipAddrs.push_back(std::string(altName, altNameLen));
  298. }
  299. }
  300. altNameLen = sizeof(altName);
  301. ret = gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME, 0, 0,
  302. altName, &altNameLen);
  303. if (ret == 0) {
  304. commonName.assign(altName, altNameLen);
  305. }
  306. if (!net::verifyHostname(hostname, dnsNames, ipAddrs, commonName)) {
  307. handshakeErr = "hostname does not match";
  308. return TLS_ERR_ERROR;
  309. }
  310. }
  311. version = getProtocolFromSession(sslSession_);
  312. return TLS_ERR_OK;
  313. }
  314. int GnuTLSSession::tlsAccept(TLSVersion& version)
  315. {
  316. for (;;) {
  317. rv_ = gnutls_handshake(sslSession_);
  318. if (rv_ == GNUTLS_E_SUCCESS) {
  319. version = getProtocolFromSession(sslSession_);
  320. return TLS_ERR_OK;
  321. }
  322. if (rv_ == GNUTLS_E_AGAIN || rv_ == GNUTLS_E_INTERRUPTED) {
  323. return TLS_ERR_WOULDBLOCK;
  324. }
  325. if (gnutls_error_is_fatal(rv_)) {
  326. return TLS_ERR_ERROR;
  327. }
  328. }
  329. }
  330. std::string GnuTLSSession::getLastErrorString() { return gnutls_strerror(rv_); }
  331. } // namespace aria2