LibgnutlsTLSSession.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #ifdef USE_GNUTLS_SYSTEM_CRYPTO_POLICY
  120. rv_ = gnutls_priority_set_direct(sslSession_, "@SYSTEM", &err);
  121. #else
  122. std::string pri = "SECURE128:+SIGN-RSA-SHA1";
  123. switch (tlsContext_->getMinTLSVersion()) {
  124. case TLS_PROTO_TLS12:
  125. pri += ":-VERS-TLS1.1";
  126. // fall through
  127. case TLS_PROTO_TLS11:
  128. pri += ":-VERS-TLS1.0";
  129. // fall through
  130. case TLS_PROTO_TLS10:
  131. pri += ":-VERS-SSL3.0";
  132. default:
  133. break;
  134. };
  135. rv_ = gnutls_priority_set_direct(sslSession_, pri.c_str(), &err);
  136. #endif
  137. if (rv_ != GNUTLS_E_SUCCESS) {
  138. return TLS_ERR_ERROR;
  139. }
  140. // put the x509 credentials to the current session
  141. rv_ = gnutls_credentials_set(sslSession_, GNUTLS_CRD_CERTIFICATE,
  142. tlsContext_->getCertCred());
  143. if (rv_ != GNUTLS_E_SUCCESS) {
  144. return TLS_ERR_ERROR;
  145. }
  146. // TODO Consider to use gnutls_transport_set_int() for GNUTLS 3.1.9
  147. // or later
  148. gnutls_transport_set_ptr(sslSession_,
  149. (gnutls_transport_ptr_t)(ptrdiff_t)sockfd);
  150. return TLS_ERR_OK;
  151. }
  152. int GnuTLSSession::setSNIHostname(const std::string& hostname)
  153. {
  154. // TLS extensions: SNI
  155. rv_ = gnutls_server_name_set(sslSession_, GNUTLS_NAME_DNS, hostname.c_str(),
  156. hostname.size());
  157. if (rv_ != GNUTLS_E_SUCCESS) {
  158. return TLS_ERR_ERROR;
  159. }
  160. return TLS_ERR_OK;
  161. }
  162. int GnuTLSSession::closeConnection()
  163. {
  164. rv_ = gnutls_bye(sslSession_, GNUTLS_SHUT_WR);
  165. if (rv_ == GNUTLS_E_SUCCESS) {
  166. return TLS_ERR_OK;
  167. }
  168. else if (rv_ == GNUTLS_E_AGAIN || rv_ == GNUTLS_E_INTERRUPTED) {
  169. return TLS_ERR_WOULDBLOCK;
  170. }
  171. else {
  172. return TLS_ERR_ERROR;
  173. }
  174. }
  175. int GnuTLSSession::checkDirection()
  176. {
  177. int direction = gnutls_record_get_direction(sslSession_);
  178. return direction == 0 ? TLS_WANT_READ : TLS_WANT_WRITE;
  179. }
  180. ssize_t GnuTLSSession::writeData(const void* data, size_t len)
  181. {
  182. while ((rv_ = gnutls_record_send(sslSession_, data, len)) ==
  183. GNUTLS_E_INTERRUPTED)
  184. ;
  185. if (rv_ >= 0) {
  186. ssize_t ret = rv_;
  187. rv_ = 0;
  188. return ret;
  189. }
  190. else if (rv_ == GNUTLS_E_AGAIN || rv_ == GNUTLS_E_INTERRUPTED) {
  191. return TLS_ERR_WOULDBLOCK;
  192. }
  193. else {
  194. return TLS_ERR_ERROR;
  195. }
  196. }
  197. ssize_t GnuTLSSession::readData(void* data, size_t len)
  198. {
  199. while ((rv_ = gnutls_record_recv(sslSession_, data, len)) ==
  200. GNUTLS_E_INTERRUPTED)
  201. ;
  202. if (rv_ >= 0) {
  203. ssize_t ret = rv_;
  204. rv_ = 0;
  205. return ret;
  206. }
  207. else if (rv_ == GNUTLS_E_AGAIN || rv_ == GNUTLS_E_INTERRUPTED) {
  208. return TLS_ERR_WOULDBLOCK;
  209. }
  210. else {
  211. return TLS_ERR_ERROR;
  212. }
  213. }
  214. int GnuTLSSession::tlsConnect(const std::string& hostname, TLSVersion& version,
  215. std::string& handshakeErr)
  216. {
  217. handshakeErr = "";
  218. for (;;) {
  219. rv_ = gnutls_handshake(sslSession_);
  220. if (rv_ == GNUTLS_E_SUCCESS) {
  221. break;
  222. }
  223. if (rv_ == GNUTLS_E_AGAIN || rv_ == GNUTLS_E_INTERRUPTED) {
  224. return TLS_ERR_WOULDBLOCK;
  225. }
  226. if (gnutls_error_is_fatal(rv_)) {
  227. return TLS_ERR_ERROR;
  228. }
  229. }
  230. if (tlsContext_->getVerifyPeer()) {
  231. // verify peer
  232. unsigned int status;
  233. rv_ = gnutls_certificate_verify_peers2(sslSession_, &status);
  234. if (rv_ != GNUTLS_E_SUCCESS) {
  235. return TLS_ERR_ERROR;
  236. }
  237. if (status) {
  238. handshakeErr = "";
  239. if (status & GNUTLS_CERT_INVALID) {
  240. handshakeErr += " `not signed by known authorities or invalid'";
  241. }
  242. if (status & GNUTLS_CERT_REVOKED) {
  243. handshakeErr += " `revoked by its CA'";
  244. }
  245. if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) {
  246. handshakeErr += " `issuer is not known'";
  247. }
  248. // TODO should check GNUTLS_CERT_SIGNER_NOT_CA ?
  249. if (status & GNUTLS_CERT_INSECURE_ALGORITHM) {
  250. handshakeErr += " `insecure algorithm'";
  251. }
  252. if (status & GNUTLS_CERT_NOT_ACTIVATED) {
  253. handshakeErr += " `not activated yet'";
  254. }
  255. if (status & GNUTLS_CERT_EXPIRED) {
  256. handshakeErr += " `expired'";
  257. }
  258. // TODO Add GNUTLS_CERT_SIGNATURE_FAILURE here
  259. if (!handshakeErr.empty()) {
  260. return TLS_ERR_ERROR;
  261. }
  262. }
  263. // certificate type: only X509 is allowed.
  264. if (gnutls_certificate_type_get(sslSession_) != GNUTLS_CRT_X509) {
  265. handshakeErr = "certificate type must be X509";
  266. return TLS_ERR_ERROR;
  267. }
  268. unsigned int peerCertsLength;
  269. const gnutls_datum_t* peerCerts;
  270. peerCerts = gnutls_certificate_get_peers(sslSession_, &peerCertsLength);
  271. if (!peerCerts || peerCertsLength == 0) {
  272. handshakeErr = "certificate not found";
  273. return TLS_ERR_ERROR;
  274. }
  275. gnutls_x509_crt_t cert;
  276. rv_ = gnutls_x509_crt_init(&cert);
  277. if (rv_ != GNUTLS_E_SUCCESS) {
  278. return TLS_ERR_ERROR;
  279. }
  280. std::unique_ptr<std::remove_pointer<gnutls_x509_crt_t>::type,
  281. decltype(&gnutls_x509_crt_deinit)>
  282. certDeleter(cert, gnutls_x509_crt_deinit);
  283. rv_ = gnutls_x509_crt_import(cert, &peerCerts[0], GNUTLS_X509_FMT_DER);
  284. if (rv_ != GNUTLS_E_SUCCESS) {
  285. return TLS_ERR_ERROR;
  286. }
  287. std::string commonName;
  288. std::vector<std::string> dnsNames;
  289. std::vector<std::string> ipAddrs;
  290. int ret = 0;
  291. char altName[256];
  292. size_t altNameLen;
  293. for (int i = 0; !(ret < 0); ++i) {
  294. altNameLen = sizeof(altName);
  295. ret = gnutls_x509_crt_get_subject_alt_name(cert, i, altName, &altNameLen,
  296. nullptr);
  297. if (ret == GNUTLS_SAN_DNSNAME) {
  298. if (altNameLen == 0) {
  299. continue;
  300. }
  301. if (altName[altNameLen - 1] == '.') {
  302. --altNameLen;
  303. if (altNameLen == 0) {
  304. continue;
  305. }
  306. }
  307. dnsNames.push_back(std::string(altName, altNameLen));
  308. }
  309. else if (ret == GNUTLS_SAN_IPADDRESS) {
  310. ipAddrs.push_back(std::string(altName, altNameLen));
  311. }
  312. }
  313. altNameLen = sizeof(altName);
  314. ret = gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME, 0, 0,
  315. altName, &altNameLen);
  316. if (ret == 0) {
  317. if (altNameLen > 0) {
  318. if (altName[altNameLen - 1] == '.') {
  319. --altNameLen;
  320. if (altNameLen > 0) {
  321. commonName.assign(altName, altNameLen);
  322. }
  323. }
  324. }
  325. }
  326. if (!net::verifyHostname(hostname, dnsNames, ipAddrs, commonName)) {
  327. handshakeErr = "hostname does not match";
  328. return TLS_ERR_ERROR;
  329. }
  330. }
  331. version = getProtocolFromSession(sslSession_);
  332. return TLS_ERR_OK;
  333. }
  334. int GnuTLSSession::tlsAccept(TLSVersion& version)
  335. {
  336. for (;;) {
  337. rv_ = gnutls_handshake(sslSession_);
  338. if (rv_ == GNUTLS_E_SUCCESS) {
  339. version = getProtocolFromSession(sslSession_);
  340. return TLS_ERR_OK;
  341. }
  342. if (rv_ == GNUTLS_E_AGAIN || rv_ == GNUTLS_E_INTERRUPTED) {
  343. return TLS_ERR_WOULDBLOCK;
  344. }
  345. if (gnutls_error_is_fatal(rv_)) {
  346. return TLS_ERR_ERROR;
  347. }
  348. }
  349. }
  350. std::string GnuTLSSession::getLastErrorString() { return gnutls_strerror(rv_); }
  351. } // namespace aria2