LibsslTLSSession.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 "LibsslTLSSession.h"
  36. #include <openssl/err.h>
  37. #include <openssl/x509.h>
  38. #include <openssl/x509v3.h>
  39. #include "LogFactory.h"
  40. #include "util.h"
  41. #include "SocketCore.h"
  42. namespace aria2 {
  43. #if !OPENSSL_101_API
  44. namespace {
  45. const unsigned char* ASN1_STRING_get0_data(ASN1_STRING* x)
  46. {
  47. return ASN1_STRING_data(x);
  48. }
  49. } // namespace
  50. #endif // !OPENSSL_101_API
  51. TLSSession* TLSSession::make(TLSContext* ctx)
  52. {
  53. return new OpenSSLTLSSession(static_cast<OpenSSLTLSContext*>(ctx));
  54. }
  55. OpenSSLTLSSession::OpenSSLTLSSession(OpenSSLTLSContext* tlsContext)
  56. : ssl_(nullptr), tlsContext_(tlsContext), rv_(1)
  57. {
  58. }
  59. OpenSSLTLSSession::~OpenSSLTLSSession()
  60. {
  61. if (ssl_) {
  62. SSL_free(ssl_);
  63. }
  64. }
  65. int OpenSSLTLSSession::init(sock_t sockfd)
  66. {
  67. ERR_clear_error();
  68. ssl_ = SSL_new(tlsContext_->getSSLCtx());
  69. if (!ssl_) {
  70. return TLS_ERR_ERROR;
  71. }
  72. rv_ = SSL_set_fd(ssl_, sockfd);
  73. if (rv_ == 0) {
  74. return TLS_ERR_ERROR;
  75. }
  76. return TLS_ERR_OK;
  77. }
  78. int OpenSSLTLSSession::setSNIHostname(const std::string& hostname)
  79. {
  80. #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
  81. ERR_clear_error();
  82. // TLS extensions: SNI. There is not documentation about the
  83. // return code for this function (actually this is macro
  84. // wrapping SSL_ctrl at the time of this writing).
  85. SSL_set_tlsext_host_name(ssl_, hostname.c_str());
  86. #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
  87. return TLS_ERR_OK;
  88. }
  89. int OpenSSLTLSSession::closeConnection()
  90. {
  91. ERR_clear_error();
  92. SSL_shutdown(ssl_);
  93. // TODO handle return value
  94. return TLS_ERR_OK;
  95. }
  96. int OpenSSLTLSSession::checkDirection()
  97. {
  98. int error = SSL_get_error(ssl_, rv_);
  99. if (error == SSL_ERROR_WANT_WRITE) {
  100. return TLS_WANT_WRITE;
  101. }
  102. else {
  103. // TODO We ignore error other than SSL_ERR_WANT_READ here for now
  104. return TLS_WANT_READ;
  105. }
  106. }
  107. namespace {
  108. bool wouldblock(SSL* ssl, int rv)
  109. {
  110. int error = SSL_get_error(ssl, rv);
  111. return error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE;
  112. }
  113. } // namespace
  114. ssize_t OpenSSLTLSSession::writeData(const void* data, size_t len)
  115. {
  116. ERR_clear_error();
  117. rv_ = SSL_write(ssl_, data, len);
  118. if (rv_ <= 0) {
  119. if (wouldblock(ssl_, rv_)) {
  120. return TLS_ERR_WOULDBLOCK;
  121. }
  122. else {
  123. return TLS_ERR_ERROR;
  124. }
  125. }
  126. else {
  127. ssize_t ret = rv_;
  128. rv_ = 1;
  129. return ret;
  130. }
  131. }
  132. ssize_t OpenSSLTLSSession::readData(void* data, size_t len)
  133. {
  134. ERR_clear_error();
  135. rv_ = SSL_read(ssl_, data, len);
  136. if (rv_ <= 0) {
  137. if (wouldblock(ssl_, rv_)) {
  138. return TLS_ERR_WOULDBLOCK;
  139. }
  140. if (rv_ == 0) {
  141. auto err = SSL_get_error(ssl_, rv_);
  142. if (err == SSL_ERROR_ZERO_RETURN) {
  143. return 0;
  144. }
  145. }
  146. return TLS_ERR_ERROR;
  147. }
  148. ssize_t ret = rv_;
  149. rv_ = 1;
  150. return ret;
  151. }
  152. int OpenSSLTLSSession::handshake(TLSVersion& version)
  153. {
  154. ERR_clear_error();
  155. if (tlsContext_->getSide() == TLS_CLIENT) {
  156. rv_ = SSL_connect(ssl_);
  157. }
  158. else {
  159. rv_ = SSL_accept(ssl_);
  160. }
  161. if (rv_ <= 0) {
  162. int sslError = SSL_get_error(ssl_, rv_);
  163. switch (sslError) {
  164. case SSL_ERROR_NONE:
  165. case SSL_ERROR_WANT_X509_LOOKUP:
  166. case SSL_ERROR_ZERO_RETURN:
  167. // TODO Now assume we are doing non-blocking. Then above 2
  168. // errors are OK.
  169. break;
  170. case SSL_ERROR_WANT_READ:
  171. case SSL_ERROR_WANT_WRITE:
  172. return TLS_ERR_WOULDBLOCK;
  173. default:
  174. return TLS_ERR_ERROR;
  175. }
  176. }
  177. switch (SSL_version(ssl_)) {
  178. #ifdef TLS1_1_VERSION
  179. case TLS1_1_VERSION:
  180. version = TLS_PROTO_TLS11;
  181. break;
  182. #endif // TLS1_1_VERSION
  183. #ifdef TLS1_2_VERSION
  184. case TLS1_2_VERSION:
  185. version = TLS_PROTO_TLS12;
  186. break;
  187. #endif // TLS1_2_VERSION
  188. #ifdef TLS1_3_VERSION
  189. case TLS1_3_VERSION:
  190. version = TLS_PROTO_TLS13;
  191. break;
  192. #endif // TLS1_3_VERSION
  193. default:
  194. version = TLS_PROTO_NONE;
  195. break;
  196. }
  197. return TLS_ERR_OK;
  198. }
  199. int OpenSSLTLSSession::tlsConnect(const std::string& hostname,
  200. TLSVersion& version,
  201. std::string& handshakeErr)
  202. {
  203. handshakeErr = "";
  204. int ret;
  205. ret = handshake(version);
  206. if (ret != TLS_ERR_OK) {
  207. return ret;
  208. }
  209. if (tlsContext_->getSide() == TLS_CLIENT && tlsContext_->getVerifyPeer()) {
  210. // verify peer
  211. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  212. auto peerCert = SSL_get1_peer_certificate(ssl_);
  213. #else // !(OPENSSL_VERSION_NUMBER >= 0x30000000L)
  214. auto peerCert = SSL_get_peer_certificate(ssl_);
  215. #endif // !(OPENSSL_VERSION_NUMBER >= 0x30000000L)
  216. if (!peerCert) {
  217. handshakeErr = "certificate not found";
  218. return TLS_ERR_ERROR;
  219. }
  220. std::unique_ptr<X509, decltype(&X509_free)> certDeleter(peerCert,
  221. X509_free);
  222. long verifyResult = SSL_get_verify_result(ssl_);
  223. if (verifyResult != X509_V_OK) {
  224. handshakeErr = X509_verify_cert_error_string(verifyResult);
  225. return TLS_ERR_ERROR;
  226. }
  227. std::string commonName;
  228. std::vector<std::string> dnsNames;
  229. std::vector<std::string> ipAddrs;
  230. GENERAL_NAMES* altNames;
  231. altNames = reinterpret_cast<GENERAL_NAMES*>(
  232. X509_get_ext_d2i(peerCert, NID_subject_alt_name, nullptr, NULL));
  233. if (altNames) {
  234. std::unique_ptr<GENERAL_NAMES, decltype(&GENERAL_NAMES_free)>
  235. altNamesDeleter(altNames, GENERAL_NAMES_free);
  236. size_t n = sk_GENERAL_NAME_num(altNames);
  237. for (size_t i = 0; i < n; ++i) {
  238. const GENERAL_NAME* altName = sk_GENERAL_NAME_value(altNames, i);
  239. if (altName->type == GEN_DNS) {
  240. auto name = ASN1_STRING_get0_data(altName->d.ia5);
  241. if (!name) {
  242. continue;
  243. }
  244. size_t len = ASN1_STRING_length(altName->d.ia5);
  245. if (len == 0) {
  246. continue;
  247. }
  248. if (name[len - 1] == '.') {
  249. --len;
  250. if (len == 0) {
  251. continue;
  252. }
  253. }
  254. dnsNames.push_back(std::string(name, name + len));
  255. }
  256. else if (altName->type == GEN_IPADD) {
  257. const unsigned char* ipAddr = altName->d.iPAddress->data;
  258. if (!ipAddr) {
  259. continue;
  260. }
  261. size_t len = altName->d.iPAddress->length;
  262. ipAddrs.push_back(
  263. std::string(reinterpret_cast<const char*>(ipAddr), len));
  264. }
  265. }
  266. }
  267. X509_NAME* subjectName = X509_get_subject_name(peerCert);
  268. if (!subjectName) {
  269. handshakeErr = "could not get X509 name object from the certificate.";
  270. return TLS_ERR_ERROR;
  271. }
  272. int lastpos = -1;
  273. while (1) {
  274. lastpos =
  275. X509_NAME_get_index_by_NID(subjectName, NID_commonName, lastpos);
  276. if (lastpos == -1) {
  277. break;
  278. }
  279. X509_NAME_ENTRY* entry = X509_NAME_get_entry(subjectName, lastpos);
  280. unsigned char* out;
  281. int outlen = ASN1_STRING_to_UTF8(&out, X509_NAME_ENTRY_get_data(entry));
  282. if (outlen < 0) {
  283. continue;
  284. }
  285. if (outlen == 0) {
  286. OPENSSL_free(out);
  287. continue;
  288. }
  289. if (out[outlen - 1] == '.') {
  290. --outlen;
  291. if (outlen == 0) {
  292. OPENSSL_free(out);
  293. continue;
  294. }
  295. }
  296. commonName.assign(&out[0], &out[outlen]);
  297. OPENSSL_free(out);
  298. break;
  299. }
  300. if (!net::verifyHostname(hostname, dnsNames, ipAddrs, commonName)) {
  301. handshakeErr = "hostname does not match";
  302. return TLS_ERR_ERROR;
  303. }
  304. }
  305. return TLS_ERR_OK;
  306. }
  307. int OpenSSLTLSSession::tlsAccept(TLSVersion& version)
  308. {
  309. return handshake(version);
  310. }
  311. std::string OpenSSLTLSSession::getLastErrorString()
  312. {
  313. if (rv_ <= 0) {
  314. int sslError = SSL_get_error(ssl_, rv_);
  315. switch (sslError) {
  316. case SSL_ERROR_NONE:
  317. case SSL_ERROR_WANT_READ:
  318. case SSL_ERROR_WANT_WRITE:
  319. case SSL_ERROR_WANT_X509_LOOKUP:
  320. case SSL_ERROR_ZERO_RETURN:
  321. return "";
  322. case SSL_ERROR_SYSCALL: {
  323. int err = ERR_get_error();
  324. if (err == 0) {
  325. if (rv_ == 0) {
  326. return "EOF was received";
  327. }
  328. else if (rv_ == -1) {
  329. return "SSL I/O error";
  330. }
  331. else {
  332. return "unknown syscall error";
  333. }
  334. }
  335. else {
  336. return ERR_error_string(err, nullptr);
  337. }
  338. }
  339. case SSL_ERROR_SSL:
  340. return "protocol error";
  341. default:
  342. return "unknown error";
  343. }
  344. }
  345. else {
  346. return "";
  347. }
  348. }
  349. } // namespace aria2