AppleTLSSession.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 Nils Maier
  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 "AppleTLSSession.h"
  36. #include <vector>
  37. #include <CoreFoundation/CoreFoundation.h>
  38. #include "LogFactory.h"
  39. #include "a2functional.h"
  40. #include "fmt.h"
  41. #define ioErr -36
  42. #define paramErr -50
  43. #define errSSLServerAuthCompleted -9841
  44. namespace {
  45. #if !defined(__MAC_10_8)
  46. static const SSLProtocol kTLSProtocol11 = (SSLProtocol)(kSSLProtocolAll + 1);
  47. static const SSLProtocol kTLSProtocol12 = (SSLProtocol)(kSSLProtocolAll + 2);
  48. #endif
  49. #ifndef CIPHER_NO_DHPARAM
  50. // Diffie-Hellman params, to seed the engine instead of having it spend up
  51. // to 30 seconds on generating them. It should be save to share these. :p
  52. // This was generated using: openssl dhparam -outform DER 2048
  53. static const uint8_t dhparam[] =
  54. "\x30\x82\x01\x08\x02\x82\x01\x01\x00\x97\xea\xd0\x46\xf7\xae\xa7\x76\x80"
  55. "\x9c\x74\x56\x98\xd8\x56\x97\x2b\x20\x6c\x77\xe2\x82\xbb\xc8\x84\xbe\xe7"
  56. "\x63\xaf\xcc\x30\xd0\x67\x97\x7d\x1b\xab\x59\x30\xa9\x13\x67\x21\xd7\xd4"
  57. "\x0e\x46\xcf\xe5\x80\xdf\xc9\xb9\xba\x54\x9b\x46\x2f\x3b\x45\xfc\x2f\xaf"
  58. "\xad\xc0\x17\x56\xdd\x52\x42\x57\x45\x70\x14\xe5\xbe\x67\xaa\xde\x69\x75"
  59. "\x30\x0d\xf9\xa2\xc4\x63\x4d\x7a\x39\xef\x14\x62\x18\x33\x44\xa1\xf9\xc1"
  60. "\x52\xd1\xb6\x72\x21\x98\xf8\xab\x16\x1b\x7b\x37\x65\xe3\xc5\x11\x00\xf6"
  61. "\x36\x1f\xd8\x5f\xd8\x9f\x43\xa8\xce\x9d\xbf\x5e\xd6\x2d\xfa\x0a\xc2\x01"
  62. "\x54\xc2\xd9\x81\x54\x55\xb5\x26\xf8\x88\x37\xf5\xfe\xe0\xef\x4a\x34\x81"
  63. "\xdc\x5a\xb3\x71\x46\x27\xe3\xcd\x24\xf6\x1b\xf1\xe2\x0f\xc2\xa1\x39\x53"
  64. "\x5b\xc5\x38\x46\x8e\x67\x4c\xd9\xdd\xe4\x37\x06\x03\x16\xf1\x1d\x7a\xba"
  65. "\x2d\xc1\xe4\x03\x1a\x58\xe5\x29\x5a\x29\x06\x69\x61\x7a\xd8\xa9\x05\x9f"
  66. "\xc1\xa2\x45\x9c\x17\xad\x52\x69\x33\xdc\x18\x8d\x15\xa6\x5e\xcd\x94\xf4"
  67. "\x45\xbb\x9f\xc2\x7b\x85\x00\x61\xb0\x1a\xdc\x3c\x86\xaa\x9f\x5c\x04\xb3"
  68. "\x90\x0b\x35\x64\xff\xd9\xe3\xac\xf2\xf2\xeb\x3a\x63\x02\x01\x02";
  69. #endif // CIPHER_NO_DHPARAM
  70. static inline const char *protoToString(SSLProtocol proto) {
  71. switch (proto) {
  72. case kSSLProtocol2:
  73. return "SSLv2 (!)";
  74. case kSSLProtocol3:
  75. return "SSLv3";
  76. case kTLSProtocol1:
  77. return "TLSv1";
  78. case kTLSProtocol11:
  79. return "TLSv1.1";
  80. case kTLSProtocol12:
  81. return "TLSv1.2";
  82. default:
  83. return "Unknown";
  84. }
  85. }
  86. #define SUITE(s) { s, #s }
  87. static struct {
  88. SSLCipherSuite suite;
  89. const char *name;
  90. } kSuites[] = {
  91. SUITE(SSL_NULL_WITH_NULL_NULL),
  92. SUITE(SSL_RSA_WITH_NULL_MD5),
  93. SUITE(SSL_RSA_WITH_NULL_SHA),
  94. SUITE(SSL_RSA_EXPORT_WITH_RC4_40_MD5),
  95. SUITE(SSL_RSA_WITH_RC4_128_MD5),
  96. SUITE(SSL_RSA_WITH_RC4_128_SHA),
  97. SUITE(SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5),
  98. SUITE(SSL_RSA_WITH_IDEA_CBC_SHA),
  99. SUITE(SSL_RSA_EXPORT_WITH_DES40_CBC_SHA),
  100. SUITE(SSL_RSA_WITH_DES_CBC_SHA),
  101. SUITE(SSL_RSA_WITH_3DES_EDE_CBC_SHA),
  102. SUITE(SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA),
  103. SUITE(SSL_DH_DSS_WITH_DES_CBC_SHA),
  104. SUITE(SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA),
  105. SUITE(SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA),
  106. SUITE(SSL_DH_RSA_WITH_DES_CBC_SHA),
  107. SUITE(SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA),
  108. SUITE(SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA),
  109. SUITE(SSL_DHE_DSS_WITH_DES_CBC_SHA),
  110. SUITE(SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA),
  111. SUITE(SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA),
  112. SUITE(SSL_DHE_RSA_WITH_DES_CBC_SHA),
  113. SUITE(SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA),
  114. SUITE(SSL_DH_anon_EXPORT_WITH_RC4_40_MD5),
  115. SUITE(SSL_DH_anon_WITH_RC4_128_MD5),
  116. SUITE(SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA),
  117. SUITE(SSL_DH_anon_WITH_DES_CBC_SHA),
  118. SUITE(SSL_DH_anon_WITH_3DES_EDE_CBC_SHA),
  119. SUITE(SSL_FORTEZZA_DMS_WITH_NULL_SHA),
  120. SUITE(SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA),
  121. SUITE(TLS_RSA_WITH_AES_128_CBC_SHA),
  122. SUITE(TLS_DH_DSS_WITH_AES_128_CBC_SHA),
  123. SUITE(TLS_DH_RSA_WITH_AES_128_CBC_SHA),
  124. SUITE(TLS_DHE_DSS_WITH_AES_128_CBC_SHA),
  125. SUITE(TLS_DHE_RSA_WITH_AES_128_CBC_SHA),
  126. SUITE(TLS_DH_anon_WITH_AES_128_CBC_SHA),
  127. SUITE(TLS_RSA_WITH_AES_256_CBC_SHA),
  128. SUITE(TLS_DH_DSS_WITH_AES_256_CBC_SHA),
  129. SUITE(TLS_DH_RSA_WITH_AES_256_CBC_SHA),
  130. SUITE(TLS_DHE_DSS_WITH_AES_256_CBC_SHA),
  131. SUITE(TLS_DHE_RSA_WITH_AES_256_CBC_SHA),
  132. SUITE(TLS_DH_anon_WITH_AES_256_CBC_SHA),
  133. SUITE(TLS_ECDH_ECDSA_WITH_NULL_SHA),
  134. SUITE(TLS_ECDH_ECDSA_WITH_RC4_128_SHA),
  135. SUITE(TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA),
  136. SUITE(TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA),
  137. SUITE(TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA),
  138. SUITE(TLS_ECDHE_ECDSA_WITH_NULL_SHA),
  139. SUITE(TLS_ECDHE_ECDSA_WITH_RC4_128_SHA),
  140. SUITE(TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA),
  141. SUITE(TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA),
  142. SUITE(TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA),
  143. SUITE(TLS_ECDH_RSA_WITH_NULL_SHA),
  144. SUITE(TLS_ECDH_RSA_WITH_RC4_128_SHA),
  145. SUITE(TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA),
  146. SUITE(TLS_ECDH_RSA_WITH_AES_128_CBC_SHA),
  147. SUITE(TLS_ECDH_RSA_WITH_AES_256_CBC_SHA),
  148. SUITE(TLS_ECDHE_RSA_WITH_NULL_SHA),
  149. SUITE(TLS_ECDHE_RSA_WITH_RC4_128_SHA),
  150. SUITE(TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA),
  151. SUITE(TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA),
  152. SUITE(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA),
  153. SUITE(TLS_ECDH_anon_WITH_NULL_SHA),
  154. SUITE(TLS_ECDH_anon_WITH_RC4_128_SHA),
  155. SUITE(SSL_RSA_WITH_RC2_CBC_MD5),
  156. SUITE(SSL_RSA_WITH_IDEA_CBC_MD5),
  157. SUITE(SSL_RSA_WITH_DES_CBC_MD5),
  158. SUITE(SSL_RSA_WITH_3DES_EDE_CBC_MD5),
  159. #if defined(__MAC_10_8)
  160. SUITE(TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA),
  161. SUITE(TLS_DHE_DSS_WITH_AES_128_CBC_SHA256),
  162. SUITE(TLS_DHE_DSS_WITH_AES_128_GCM_SHA256),
  163. SUITE(TLS_DHE_DSS_WITH_AES_256_CBC_SHA256),
  164. SUITE(TLS_DHE_DSS_WITH_AES_256_GCM_SHA384),
  165. SUITE(TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA),
  166. SUITE(TLS_DHE_RSA_WITH_AES_128_CBC_SHA256),
  167. SUITE(TLS_DHE_RSA_WITH_AES_128_GCM_SHA256),
  168. SUITE(TLS_DHE_RSA_WITH_AES_256_CBC_SHA256),
  169. SUITE(TLS_DHE_RSA_WITH_AES_256_GCM_SHA384),
  170. SUITE(TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA),
  171. SUITE(TLS_DH_DSS_WITH_AES_128_CBC_SHA256),
  172. SUITE(TLS_DH_DSS_WITH_AES_128_GCM_SHA256),
  173. SUITE(TLS_DH_DSS_WITH_AES_256_CBC_SHA256),
  174. SUITE(TLS_DH_DSS_WITH_AES_256_GCM_SHA384),
  175. SUITE(TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA),
  176. SUITE(TLS_DH_RSA_WITH_AES_128_CBC_SHA256),
  177. SUITE(TLS_DH_RSA_WITH_AES_128_GCM_SHA256),
  178. SUITE(TLS_DH_RSA_WITH_AES_256_CBC_SHA256),
  179. SUITE(TLS_DH_RSA_WITH_AES_256_GCM_SHA384),
  180. SUITE(TLS_DH_anon_WITH_3DES_EDE_CBC_SHA),
  181. SUITE(TLS_DH_anon_WITH_AES_128_CBC_SHA256),
  182. SUITE(TLS_DH_anon_WITH_AES_128_GCM_SHA256),
  183. SUITE(TLS_DH_anon_WITH_AES_256_CBC_SHA256),
  184. SUITE(TLS_DH_anon_WITH_AES_256_GCM_SHA384),
  185. SUITE(TLS_DH_anon_WITH_RC4_128_MD5),
  186. SUITE(TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256),
  187. SUITE(TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
  188. SUITE(TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384),
  189. SUITE(TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384),
  190. SUITE(TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256),
  191. SUITE(TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256),
  192. SUITE(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384),
  193. SUITE(TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384),
  194. SUITE(TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256),
  195. SUITE(TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256),
  196. SUITE(TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384),
  197. SUITE(TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384),
  198. SUITE(TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256),
  199. SUITE(TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256),
  200. SUITE(TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384),
  201. SUITE(TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384),
  202. SUITE(TLS_EMPTY_RENEGOTIATION_INFO_SCSV),
  203. SUITE(TLS_NULL_WITH_NULL_NULL),
  204. SUITE(TLS_RSA_WITH_3DES_EDE_CBC_SHA),
  205. SUITE(TLS_RSA_WITH_AES_128_CBC_SHA256),
  206. SUITE(TLS_RSA_WITH_AES_128_GCM_SHA256),
  207. SUITE(TLS_RSA_WITH_AES_256_CBC_SHA256),
  208. SUITE(TLS_RSA_WITH_AES_256_GCM_SHA384),
  209. SUITE(TLS_RSA_WITH_NULL_MD5),
  210. SUITE(TLS_RSA_WITH_NULL_SHA),
  211. SUITE(TLS_RSA_WITH_NULL_SHA256),
  212. SUITE(TLS_RSA_WITH_RC4_128_MD5),
  213. SUITE(TLS_RSA_WITH_RC4_128_SHA),
  214. #endif
  215. SUITE(SSL_NO_SUCH_CIPHERSUITE)
  216. };
  217. static const size_t nSuites = sizeof(kSuites) / sizeof(*kSuites);
  218. #undef SUITE
  219. static inline const char* suiteToString(const SSLCipherSuite suite)
  220. {
  221. for (size_t i = 0; i < nSuites; ++i) {
  222. if (kSuites[i].suite == suite) {
  223. return kSuites[i].name;
  224. }
  225. }
  226. return "Unknown suite";
  227. }
  228. static const char* kBlocked[] = {
  229. "NULL", "anon", "MD5", "EXPORT", "DES", "IDEA", "NO_SUCH", "EMPTY"
  230. };
  231. static const size_t nBlocked = sizeof(kBlocked) / sizeof(*kBlocked);
  232. static inline bool isBlockedSuite(SSLCipherSuite suite)
  233. {
  234. const char* name = suiteToString(suite);
  235. for (size_t i = 0; i < nBlocked; ++i) {
  236. if (strstr(name, kBlocked[i])) {
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. typedef std::vector<SSLCipherSuite> SSLCipherSuiteList;
  243. static SSLCipherSuiteList constructEnabledSuites(SSLContextRef ctx)
  244. {
  245. #ifndef CIPHER_CONSTRUCT_ALWAYS
  246. static
  247. #endif
  248. SSLCipherSuiteList rv(0);
  249. #ifndef CIPHER_CONSTRUCT_ALWAYS
  250. if (!rv.empty()) {
  251. return rv;
  252. }
  253. #endif
  254. size_t supported = 0;
  255. OSStatus err = SSLGetNumberSupportedCiphers(ctx, &supported);
  256. if (err != noErr || !supported) {
  257. return rv;
  258. }
  259. rv.resize(supported, SSL_NO_SUCH_CIPHERSUITE);
  260. err = SSLGetSupportedCiphers(ctx, &rv[0], &supported);
  261. if (err != noErr || !supported) {
  262. rv.clear();
  263. return rv;
  264. }
  265. rv.erase(std::remove_if(rv.begin(), rv.end(), isBlockedSuite), rv.end());
  266. return rv;
  267. }
  268. }
  269. namespace aria2 {
  270. TLSSession* TLSSession::make(TLSContext* ctx)
  271. {
  272. return new AppleTLSSession(static_cast<AppleTLSContext*>(ctx));
  273. }
  274. AppleTLSSession::AppleTLSSession(AppleTLSContext* ctx)
  275. : ctx_(ctx),
  276. sslCtx_(0),
  277. sockfd_(0),
  278. state_(st_constructed),
  279. lastError_(noErr),
  280. writeBuffered_(0)
  281. {
  282. lastError_ = SSLNewContext(ctx->getSide() == TLS_SERVER, &sslCtx_) == noErr;
  283. if (lastError_ == noErr) {
  284. state_ = st_error;
  285. return;
  286. }
  287. #if defined(__MAC_10_8)
  288. (void)SSLSetProtocolVersionMin(sslCtx_, kSSLProtocol3);
  289. (void)SSLSetProtocolVersionMax(sslCtx_, kTLSProtocol12);
  290. #else
  291. (void)SSLSetProtocolVersionEnabled(sslCtx_, kSSLProtocolAll, false);
  292. (void)SSLSetProtocolVersionEnabled(sslCtx_, kSSLProtocol3, true);
  293. (void)SSLSetProtocolVersionEnabled(sslCtx_, kTLSProtocol1, true);
  294. (void)SSLSetProtocolVersionEnabled(sslCtx_, kTLSProtocol11, true);
  295. (void)SSLSetProtocolVersionEnabled(sslCtx_, kTLSProtocol12, true);
  296. #endif
  297. (void)SSLSetEnableCertVerify(sslCtx_, ctx->getVerifyPeer());
  298. #ifndef CIPHER_ENABLE_ALL
  299. SSLCipherSuiteList enabled = constructEnabledSuites(sslCtx_);
  300. if (enabled.empty()) {
  301. A2_LOG_ERROR("AppleTLS: Failed to construct enabled ciphers list");
  302. state_ = st_error;
  303. return;
  304. }
  305. for (SSLCipherSuiteList::iterator i = enabled.begin(), e = enabled.end(); i != e; ++i) {
  306. A2_LOG_INFO(fmt("AppleTLS: Enabled suite %s", suiteToString(*i)));
  307. }
  308. if (SSLSetEnabledCiphers(sslCtx_, &enabled[0], enabled.size()) != noErr) {
  309. A2_LOG_ERROR("AppleTLS: Failed to set enabled ciphers list");
  310. state_ = st_error;
  311. return;
  312. }
  313. #endif
  314. if (ctx->getSide() == TLS_SERVER) {
  315. SecIdentityRef creds = ctx->getCredentials();
  316. if (!creds) {
  317. A2_LOG_ERROR("AppleTLS: No credentials");
  318. state_ = st_error;
  319. return;
  320. }
  321. CFArrayRef certs = CFArrayCreate(0, (const void**)&creds, 1, 0);
  322. if (!certs) {
  323. A2_LOG_ERROR("AppleTLS: Failed to setup credentials");
  324. state_ = st_error;
  325. return;
  326. }
  327. std::unique_ptr<void, decltype(&CFRelease)> del_certs(certs, CFRelease);
  328. lastError_ = SSLSetCertificate(sslCtx_, certs);
  329. if (lastError_ != noErr) {
  330. A2_LOG_ERROR(fmt("AppleTLS: Failed to set credentials: %s", getLastErrorString().c_str()));
  331. state_ = st_error;
  332. return;
  333. }
  334. #ifndef CIPHER_NO_DHPARAM
  335. lastError_ = SSLSetDiffieHellmanParams(sslCtx_, dhparam, sizeof(dhparam));
  336. if (lastError_ != noErr) {
  337. A2_LOG_WARN(fmt("AppleTLS: Failed to set DHParams: %s", getLastErrorString().c_str()));
  338. // Engine will still generate some for us, so this is no problem, except
  339. // it will take longer.
  340. }
  341. #endif // CIPHER_NO_DHPARAM
  342. }
  343. }
  344. AppleTLSSession::~AppleTLSSession()
  345. {
  346. closeConnection();
  347. if (sslCtx_) {
  348. SSLDisposeContext(sslCtx_);
  349. sslCtx_ = 0;
  350. }
  351. state_ = st_error;
  352. }
  353. int AppleTLSSession::init(sock_t sockfd)
  354. {
  355. if (state_ != st_constructed) {
  356. lastError_ = noErr;
  357. return TLS_ERR_ERROR;
  358. }
  359. lastError_ = SSLSetIOFuncs(sslCtx_, SocketRead, SocketWrite);
  360. if (lastError_ != noErr) {
  361. state_ = st_error;
  362. return TLS_ERR_ERROR;
  363. }
  364. lastError_ = SSLSetConnection(sslCtx_, this);
  365. if (lastError_ != noErr) {
  366. state_ = st_error;
  367. return TLS_ERR_ERROR;
  368. }
  369. sockfd_ = sockfd;
  370. state_ = st_initialized;
  371. return TLS_ERR_OK;
  372. }
  373. int AppleTLSSession::setSNIHostname(const std::string& hostname)
  374. {
  375. if (state_ != st_initialized) {
  376. lastError_ = noErr;
  377. return TLS_ERR_ERROR;
  378. }
  379. lastError_ = SSLSetPeerDomainName(sslCtx_, hostname.c_str(), hostname.length());
  380. return (lastError_ != noErr) ? TLS_ERR_ERROR : TLS_ERR_OK;
  381. }
  382. int AppleTLSSession::closeConnection()
  383. {
  384. if (state_ != st_connected) {
  385. lastError_ = noErr;
  386. return TLS_ERR_ERROR;
  387. }
  388. lastError_ = SSLClose(sslCtx_);
  389. state_ = st_closed;
  390. return lastError_ == noErr ? TLS_ERR_OK : TLS_ERR_ERROR;
  391. }
  392. int AppleTLSSession::checkDirection() {
  393. // See: https://github.com/tatsuhiro-t/aria2/pull/61#issuecomment-16051793
  394. if (state_ == st_connected) {
  395. // Need to check read first, as SocketCore kinda expects this
  396. size_t buffered;
  397. lastError_ = SSLGetBufferedReadSize(sslCtx_, &buffered);
  398. if (lastError_ == noErr && buffered) {
  399. return TLS_WANT_READ;
  400. }
  401. }
  402. if (writeBuffered_) {
  403. return TLS_WANT_WRITE;
  404. }
  405. // Default to WANT_READ, as SocketCore kinda expects this
  406. return TLS_WANT_READ;
  407. }
  408. ssize_t AppleTLSSession::writeData(const void* data, size_t len)
  409. {
  410. if (state_ != st_connected) {
  411. lastError_ = noErr;
  412. return TLS_ERR_ERROR;
  413. }
  414. size_t processed = 0;
  415. if (writeBuffered_) {
  416. lastError_ = SSLWrite(sslCtx_, 0, 0, &processed);
  417. switch (lastError_) {
  418. case noErr:
  419. processed = writeBuffered_;
  420. writeBuffered_ = 0;
  421. return processed;
  422. case errSSLWouldBlock:
  423. return TLS_ERR_WOULDBLOCK;
  424. case errSSLClosedGraceful:
  425. case errSSLClosedNoNotify:
  426. closeConnection();
  427. return TLS_ERR_ERROR;
  428. default:
  429. closeConnection();
  430. state_ = st_error;
  431. return TLS_ERR_ERROR;
  432. }
  433. }
  434. lastError_ = SSLWrite(sslCtx_, data, len, &processed);
  435. switch (lastError_) {
  436. case noErr:
  437. return processed;
  438. case errSSLWouldBlock:
  439. writeBuffered_ = len;
  440. return TLS_ERR_WOULDBLOCK;
  441. case errSSLClosedGraceful:
  442. case errSSLClosedNoNotify:
  443. closeConnection();
  444. return TLS_ERR_ERROR;
  445. default:
  446. closeConnection();
  447. state_ = st_error;
  448. return TLS_ERR_ERROR;
  449. }
  450. }
  451. OSStatus AppleTLSSession::sockWrite(const void* data, size_t* len)
  452. {
  453. size_t remain = *len;
  454. const uint8_t *buffer = static_cast<const uint8_t*>(data);
  455. *len = 0;
  456. while (remain) {
  457. ssize_t w = write(sockfd_, buffer, remain);
  458. if (w <= 0) {
  459. switch (errno) {
  460. case EAGAIN:
  461. return errSSLWouldBlock;
  462. default:
  463. return errSSLClosedAbort;
  464. }
  465. }
  466. remain -= w;
  467. buffer += w;
  468. *len += w;
  469. }
  470. return noErr;
  471. }
  472. ssize_t AppleTLSSession::readData(void* data, size_t len)
  473. {
  474. if (state_ != st_connected) {
  475. lastError_ = noErr;
  476. return TLS_ERR_ERROR;
  477. }
  478. size_t processed = 0;
  479. lastError_ = SSLRead(sslCtx_, data, len, &processed);
  480. switch (lastError_) {
  481. case noErr:
  482. return processed;
  483. case errSSLWouldBlock:
  484. if (processed) {
  485. return processed;
  486. }
  487. return TLS_ERR_WOULDBLOCK;
  488. case errSSLClosedGraceful:
  489. case errSSLClosedNoNotify:
  490. closeConnection();
  491. return TLS_ERR_ERROR;
  492. default:
  493. closeConnection();
  494. state_ = st_error;
  495. return TLS_ERR_ERROR;
  496. }
  497. }
  498. OSStatus AppleTLSSession::sockRead(void* data, size_t* len)
  499. {
  500. size_t remain = *len;
  501. uint8_t *buffer = static_cast<uint8_t*>(data);
  502. *len = 0;
  503. while (remain) {
  504. ssize_t r = read(sockfd_, buffer, remain);
  505. if (r == 0) {
  506. return errSSLClosedGraceful;
  507. }
  508. if (r < 0) {
  509. switch (errno) {
  510. case ENOENT:
  511. return errSSLClosedGraceful;
  512. case ECONNRESET:
  513. return errSSLClosedAbort;
  514. case EAGAIN:
  515. return errSSLWouldBlock;
  516. default:
  517. return errSSLClosedAbort;
  518. }
  519. }
  520. remain -= r;
  521. buffer += r;
  522. *len += r;
  523. }
  524. return noErr;
  525. }
  526. int AppleTLSSession::tlsConnect(const std::string& hostname, std::string& handshakeErr)
  527. {
  528. if (state_ != st_initialized) {
  529. return TLS_ERR_ERROR;
  530. }
  531. if (!hostname.empty()) {
  532. setSNIHostname(hostname);
  533. }
  534. lastError_ = SSLHandshake(sslCtx_);
  535. switch (lastError_) {
  536. case noErr:
  537. break;
  538. case errSSLWouldBlock:
  539. return TLS_ERR_WOULDBLOCK;
  540. case errSSLServerAuthCompleted:
  541. return tlsConnect(hostname, handshakeErr);
  542. default:
  543. handshakeErr = getLastErrorString();
  544. return TLS_ERR_ERROR;
  545. }
  546. state_ = st_connected;
  547. SSLProtocol proto = kSSLProtocolUnknown;
  548. (void)SSLGetNegotiatedProtocolVersion(sslCtx_, &proto);
  549. SSLCipherSuite suite = SSL_NO_SUCH_CIPHERSUITE;
  550. (void)SSLGetNegotiatedCipher(sslCtx_, &suite);
  551. A2_LOG_INFO(fmt("AppleTLS: Connected to %s with %s (%s)",
  552. hostname.c_str(),
  553. protoToString(proto),
  554. suiteToString(suite)));
  555. return TLS_ERR_OK;
  556. }
  557. int AppleTLSSession::tlsAccept()
  558. {
  559. std::string hostname, err;
  560. return tlsConnect(hostname, err);
  561. }
  562. std::string AppleTLSSession::getLastErrorString()
  563. {
  564. switch (lastError_) {
  565. case errSSLProtocol:
  566. return "Protocol error";
  567. case errSSLNegotiation:
  568. return "No common cipher suites";
  569. case errSSLFatalAlert:
  570. return "Received fatal alert";
  571. case errSSLSessionNotFound:
  572. return "Unknown session";
  573. case errSSLClosedGraceful:
  574. return "Closed gracefully";
  575. case errSSLClosedAbort:
  576. return "Connection aborted";
  577. case errSSLXCertChainInvalid:
  578. return "Invalid certificate chain";
  579. case errSSLBadCert:
  580. return "Invalid certificate format";
  581. case errSSLCrypto:
  582. return "Cryptographic error";
  583. case paramErr:
  584. case errSSLInternal:
  585. return "Internal SSL error";
  586. case errSSLUnknownRootCert:
  587. return "Self-signed certificate";
  588. case errSSLNoRootCert:
  589. return "No root certificate";
  590. case errSSLCertExpired:
  591. return "Certificate expired";
  592. case errSSLCertNotYetValid:
  593. return "Certificate not yet valid";
  594. case errSSLClosedNoNotify:
  595. return "Closed without notification";
  596. case errSSLBufferOverflow:
  597. return "Buffer not large enough";
  598. case errSSLBadCipherSuite:
  599. return "Bad cipher suite";
  600. case errSSLPeerUnexpectedMsg:
  601. return "Unexpected peer message";
  602. case errSSLPeerBadRecordMac:
  603. return "Bad MAC";
  604. case errSSLPeerDecryptionFail:
  605. return "Decryption failure";
  606. case errSSLHostNameMismatch:
  607. return "Invalid hostname";
  608. case errSSLConnectionRefused:
  609. return "Connection refused";
  610. default:
  611. return fmt("Unspecified error %d", lastError_);
  612. }
  613. }
  614. }