LibgnutlsTLSSession.cc 10 KB

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