LibgnutlsTLSSession.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 aria2 {
  41. TLSSession* TLSSession::make(TLSContext* ctx)
  42. {
  43. return new GnuTLSSession(static_cast<GnuTLSContext*>(ctx));
  44. }
  45. GnuTLSSession::GnuTLSSession(GnuTLSContext* tlsContext)
  46. : sslSession_(0),
  47. tlsContext_(tlsContext),
  48. rv_(0)
  49. {}
  50. GnuTLSSession::~GnuTLSSession()
  51. {
  52. if(sslSession_) {
  53. gnutls_deinit(sslSession_);
  54. }
  55. }
  56. int GnuTLSSession::init(sock_t sockfd)
  57. {
  58. rv_ = gnutls_init(&sslSession_,
  59. tlsContext_->getSide() == TLS_CLIENT ?
  60. GNUTLS_CLIENT : GNUTLS_SERVER);
  61. if(rv_ != GNUTLS_E_SUCCESS) {
  62. return TLS_ERR_ERROR;
  63. }
  64. // It seems err is not error message, but the argument string
  65. // which causes syntax error.
  66. const char* err;
  67. rv_ = gnutls_priority_set_direct(sslSession_, "NORMAL", &err);
  68. if(rv_ != GNUTLS_E_SUCCESS) {
  69. return TLS_ERR_ERROR;
  70. }
  71. // put the x509 credentials to the current session
  72. rv_ = gnutls_credentials_set(sslSession_, GNUTLS_CRD_CERTIFICATE,
  73. tlsContext_->getCertCred());
  74. if(rv_ != GNUTLS_E_SUCCESS) {
  75. return TLS_ERR_ERROR;
  76. }
  77. // TODO Consider to use gnutls_transport_set_int() for GNUTLS 3.1.9
  78. // or later
  79. gnutls_transport_set_ptr(sslSession_,
  80. (gnutls_transport_ptr_t)(ptrdiff_t)sockfd);
  81. return TLS_ERR_OK;
  82. }
  83. int GnuTLSSession::setSNIHostname(const std::string& hostname)
  84. {
  85. // TLS extensions: SNI
  86. rv_ = gnutls_server_name_set(sslSession_, GNUTLS_NAME_DNS,
  87. hostname.c_str(), hostname.size());
  88. if(rv_ != GNUTLS_E_SUCCESS) {
  89. return TLS_ERR_ERROR;
  90. }
  91. return TLS_ERR_OK;
  92. }
  93. int GnuTLSSession::closeConnection()
  94. {
  95. rv_ = gnutls_bye(sslSession_, GNUTLS_SHUT_WR);
  96. if(rv_ == GNUTLS_E_SUCCESS) {
  97. return TLS_ERR_OK;
  98. } else if(rv_ == GNUTLS_E_AGAIN) {
  99. return TLS_ERR_WOULDBLOCK;
  100. } else {
  101. return TLS_ERR_ERROR;
  102. }
  103. }
  104. int GnuTLSSession::checkDirection()
  105. {
  106. int direction = gnutls_record_get_direction(sslSession_);
  107. return direction == 0 ? TLS_WANT_READ : TLS_WANT_WRITE;
  108. }
  109. ssize_t GnuTLSSession::writeData(const void* data, size_t len)
  110. {
  111. while((rv_ = gnutls_record_send(sslSession_, data, len)) ==
  112. GNUTLS_E_INTERRUPTED);
  113. if(rv_ >= 0) {
  114. ssize_t ret = rv_;
  115. rv_ = 0;
  116. return ret;
  117. } else if(rv_ == GNUTLS_E_AGAIN) {
  118. return TLS_ERR_WOULDBLOCK;
  119. } else {
  120. return TLS_ERR_ERROR;
  121. }
  122. }
  123. ssize_t GnuTLSSession::readData(void* data, size_t len)
  124. {
  125. while((rv_ = gnutls_record_recv(sslSession_, data, len)) ==
  126. GNUTLS_E_INTERRUPTED);
  127. if(rv_ >= 0) {
  128. ssize_t ret = rv_;
  129. rv_ = 0;
  130. return ret;
  131. } else if(rv_ == GNUTLS_E_AGAIN) {
  132. return TLS_ERR_WOULDBLOCK;
  133. } else {
  134. return TLS_ERR_ERROR;
  135. }
  136. }
  137. int GnuTLSSession::tlsConnect(const std::string& hostname,
  138. std::string& handshakeErr)
  139. {
  140. handshakeErr = "";
  141. rv_ = gnutls_handshake(sslSession_);
  142. if(rv_ < 0) {
  143. if(rv_ == GNUTLS_E_AGAIN) {
  144. return TLS_ERR_WOULDBLOCK;
  145. } else {
  146. return TLS_ERR_ERROR;
  147. }
  148. }
  149. if(tlsContext_->getVerifyPeer()) {
  150. // verify peer
  151. unsigned int status;
  152. rv_ = gnutls_certificate_verify_peers2(sslSession_, &status);
  153. if(rv_ != GNUTLS_E_SUCCESS) {
  154. return TLS_ERR_ERROR;
  155. }
  156. if(status) {
  157. handshakeErr = "";
  158. if(status & GNUTLS_CERT_INVALID) {
  159. handshakeErr += " `not signed by known authorities or invalid'";
  160. }
  161. if(status & GNUTLS_CERT_REVOKED) {
  162. handshakeErr += " `revoked by its CA'";
  163. }
  164. if(status & GNUTLS_CERT_SIGNER_NOT_FOUND) {
  165. handshakeErr += " `issuer is not known'";
  166. }
  167. // TODO should check GNUTLS_CERT_SIGNER_NOT_CA ?
  168. if(status & GNUTLS_CERT_INSECURE_ALGORITHM) {
  169. handshakeErr += " `insecure algorithm'";
  170. }
  171. if(status & GNUTLS_CERT_NOT_ACTIVATED) {
  172. handshakeErr += " `not activated yet'";
  173. }
  174. if(status & GNUTLS_CERT_EXPIRED) {
  175. handshakeErr += " `expired'";
  176. }
  177. // TODO Add GNUTLS_CERT_SIGNATURE_FAILURE here
  178. if(!handshakeErr.empty()) {
  179. return TLS_ERR_ERROR;
  180. }
  181. }
  182. // certificate type: only X509 is allowed.
  183. if(gnutls_certificate_type_get(sslSession_) != GNUTLS_CRT_X509) {
  184. handshakeErr = "certificate type must be X509";
  185. return TLS_ERR_ERROR;
  186. }
  187. unsigned int peerCertsLength;
  188. const gnutls_datum_t* peerCerts;
  189. peerCerts = gnutls_certificate_get_peers(sslSession_, &peerCertsLength);
  190. if(!peerCerts || peerCertsLength == 0 ) {
  191. handshakeErr = "certificate not found";
  192. return TLS_ERR_ERROR;
  193. }
  194. gnutls_x509_crt_t cert;
  195. rv_ = gnutls_x509_crt_init(&cert);
  196. if(rv_ != GNUTLS_E_SUCCESS) {
  197. return TLS_ERR_ERROR;
  198. }
  199. std::unique_ptr<std::remove_pointer<gnutls_x509_crt_t>::type,
  200. decltype(&gnutls_x509_crt_deinit)> certDeleter
  201. (cert, gnutls_x509_crt_deinit);
  202. rv_ = gnutls_x509_crt_import(cert, &peerCerts[0], GNUTLS_X509_FMT_DER);
  203. if(rv_ != GNUTLS_E_SUCCESS) {
  204. return TLS_ERR_ERROR;
  205. }
  206. std::string commonName;
  207. std::vector<std::string> dnsNames;
  208. std::vector<std::string> ipAddrs;
  209. int ret = 0;
  210. char altName[256];
  211. size_t altNameLen;
  212. for(int i = 0; !(ret < 0); ++i) {
  213. altNameLen = sizeof(altName);
  214. ret = gnutls_x509_crt_get_subject_alt_name(cert, i, altName,
  215. &altNameLen, 0);
  216. if(ret == GNUTLS_SAN_DNSNAME) {
  217. dnsNames.push_back(std::string(altName, altNameLen));
  218. } else if(ret == GNUTLS_SAN_IPADDRESS) {
  219. ipAddrs.push_back(std::string(altName, altNameLen));
  220. }
  221. }
  222. altNameLen = sizeof(altName);
  223. ret = gnutls_x509_crt_get_dn_by_oid(cert,
  224. GNUTLS_OID_X520_COMMON_NAME, 0, 0,
  225. altName, &altNameLen);
  226. if(ret == 0) {
  227. commonName.assign(altName, altNameLen);
  228. }
  229. if(!net::verifyHostname(hostname, dnsNames, ipAddrs, commonName)) {
  230. handshakeErr = "hostname does not match";
  231. return TLS_ERR_ERROR;
  232. }
  233. }
  234. return TLS_ERR_OK;
  235. }
  236. int GnuTLSSession::tlsAccept()
  237. {
  238. rv_ = gnutls_handshake(sslSession_);
  239. if(rv_ == GNUTLS_E_SUCCESS) {
  240. return TLS_ERR_OK;
  241. } else if(rv_ == GNUTLS_E_AGAIN) {
  242. return TLS_ERR_WOULDBLOCK;
  243. } else {
  244. return TLS_ERR_ERROR;
  245. }
  246. }
  247. std::string GnuTLSSession::getLastErrorString()
  248. {
  249. return gnutls_strerror(rv_);
  250. }
  251. } // namespace aria2