AppleTLSSession.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 "fmt.h"
  39. #include "LogFactory.h"
  40. #define ioErr -36
  41. #define paramErr -50
  42. #define errSSLServerAuthCompleted -9841
  43. namespace {
  44. #if !defined(__MAC_10_8)
  45. static const SSLProtocol kTLSProtocol11 = (SSLProtocol)(kSSLProtocolAll + 1);
  46. static const SSLProtocol kTLSProtocol12 = (SSLProtocol)(kSSLProtocolAll + 2);
  47. #endif
  48. static inline const char *protoToString(SSLProtocol proto) {
  49. switch (proto) {
  50. case kSSLProtocol2:
  51. return "SSLv2 (!)";
  52. case kSSLProtocol3:
  53. return "SSLv3";
  54. case kTLSProtocol1:
  55. return "TLSv1";
  56. case kTLSProtocol11:
  57. return "TLSv1.1";
  58. case kTLSProtocol12:
  59. return "TLSv1.2";
  60. default:
  61. return "Unknown";
  62. }
  63. }
  64. #define SUITE(s) { s, #s }
  65. static struct {
  66. SSLCipherSuite suite;
  67. const char *name;
  68. } kSuites[] = {
  69. SUITE(SSL_NULL_WITH_NULL_NULL),
  70. SUITE(SSL_RSA_WITH_NULL_MD5),
  71. SUITE(SSL_RSA_WITH_NULL_SHA),
  72. SUITE(SSL_RSA_EXPORT_WITH_RC4_40_MD5),
  73. SUITE(SSL_RSA_WITH_RC4_128_MD5),
  74. SUITE(SSL_RSA_WITH_RC4_128_SHA),
  75. SUITE(SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5),
  76. SUITE(SSL_RSA_WITH_IDEA_CBC_SHA),
  77. SUITE(SSL_RSA_EXPORT_WITH_DES40_CBC_SHA),
  78. SUITE(SSL_RSA_WITH_DES_CBC_SHA),
  79. SUITE(SSL_RSA_WITH_3DES_EDE_CBC_SHA),
  80. SUITE(SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA),
  81. SUITE(SSL_DH_DSS_WITH_DES_CBC_SHA),
  82. SUITE(SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA),
  83. SUITE(SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA),
  84. SUITE(SSL_DH_RSA_WITH_DES_CBC_SHA),
  85. SUITE(SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA),
  86. SUITE(SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA),
  87. SUITE(SSL_DHE_DSS_WITH_DES_CBC_SHA),
  88. SUITE(SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA),
  89. SUITE(SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA),
  90. SUITE(SSL_DHE_RSA_WITH_DES_CBC_SHA),
  91. SUITE(SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA),
  92. SUITE(SSL_DH_anon_EXPORT_WITH_RC4_40_MD5),
  93. SUITE(SSL_DH_anon_WITH_RC4_128_MD5),
  94. SUITE(SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA),
  95. SUITE(SSL_DH_anon_WITH_DES_CBC_SHA),
  96. SUITE(SSL_DH_anon_WITH_3DES_EDE_CBC_SHA),
  97. SUITE(SSL_FORTEZZA_DMS_WITH_NULL_SHA),
  98. SUITE(SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA),
  99. SUITE(TLS_RSA_WITH_AES_128_CBC_SHA),
  100. SUITE(TLS_DH_DSS_WITH_AES_128_CBC_SHA),
  101. SUITE(TLS_DH_RSA_WITH_AES_128_CBC_SHA),
  102. SUITE(TLS_DHE_DSS_WITH_AES_128_CBC_SHA),
  103. SUITE(TLS_DHE_RSA_WITH_AES_128_CBC_SHA),
  104. SUITE(TLS_DH_anon_WITH_AES_128_CBC_SHA),
  105. SUITE(TLS_RSA_WITH_AES_256_CBC_SHA),
  106. SUITE(TLS_DH_DSS_WITH_AES_256_CBC_SHA),
  107. SUITE(TLS_DH_RSA_WITH_AES_256_CBC_SHA),
  108. SUITE(TLS_DHE_DSS_WITH_AES_256_CBC_SHA),
  109. SUITE(TLS_DHE_RSA_WITH_AES_256_CBC_SHA),
  110. SUITE(TLS_DH_anon_WITH_AES_256_CBC_SHA),
  111. SUITE(TLS_ECDH_ECDSA_WITH_NULL_SHA),
  112. SUITE(TLS_ECDH_ECDSA_WITH_RC4_128_SHA),
  113. SUITE(TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA),
  114. SUITE(TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA),
  115. SUITE(TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA),
  116. SUITE(TLS_ECDHE_ECDSA_WITH_NULL_SHA),
  117. SUITE(TLS_ECDHE_ECDSA_WITH_RC4_128_SHA),
  118. SUITE(TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA),
  119. SUITE(TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA),
  120. SUITE(TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA),
  121. SUITE(TLS_ECDH_RSA_WITH_NULL_SHA),
  122. SUITE(TLS_ECDH_RSA_WITH_RC4_128_SHA),
  123. SUITE(TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA),
  124. SUITE(TLS_ECDH_RSA_WITH_AES_128_CBC_SHA),
  125. SUITE(TLS_ECDH_RSA_WITH_AES_256_CBC_SHA),
  126. SUITE(TLS_ECDHE_RSA_WITH_NULL_SHA),
  127. SUITE(TLS_ECDHE_RSA_WITH_RC4_128_SHA),
  128. SUITE(TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA),
  129. SUITE(TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA),
  130. SUITE(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA),
  131. SUITE(TLS_ECDH_anon_WITH_NULL_SHA),
  132. SUITE(TLS_ECDH_anon_WITH_RC4_128_SHA),
  133. SUITE(SSL_RSA_WITH_RC2_CBC_MD5),
  134. SUITE(SSL_RSA_WITH_IDEA_CBC_MD5),
  135. SUITE(SSL_RSA_WITH_DES_CBC_MD5),
  136. SUITE(SSL_RSA_WITH_3DES_EDE_CBC_MD5),
  137. #if defined(__MAC_10_8)
  138. SUITE(TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA),
  139. SUITE(TLS_DHE_DSS_WITH_AES_128_CBC_SHA256),
  140. SUITE(TLS_DHE_DSS_WITH_AES_128_GCM_SHA256),
  141. SUITE(TLS_DHE_DSS_WITH_AES_256_CBC_SHA256),
  142. SUITE(TLS_DHE_DSS_WITH_AES_256_GCM_SHA384),
  143. SUITE(TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA),
  144. SUITE(TLS_DHE_RSA_WITH_AES_128_CBC_SHA256),
  145. SUITE(TLS_DHE_RSA_WITH_AES_128_GCM_SHA256),
  146. SUITE(TLS_DHE_RSA_WITH_AES_256_CBC_SHA256),
  147. SUITE(TLS_DHE_RSA_WITH_AES_256_GCM_SHA384),
  148. SUITE(TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA),
  149. SUITE(TLS_DH_DSS_WITH_AES_128_CBC_SHA256),
  150. SUITE(TLS_DH_DSS_WITH_AES_128_GCM_SHA256),
  151. SUITE(TLS_DH_DSS_WITH_AES_256_CBC_SHA256),
  152. SUITE(TLS_DH_DSS_WITH_AES_256_GCM_SHA384),
  153. SUITE(TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA),
  154. SUITE(TLS_DH_RSA_WITH_AES_128_CBC_SHA256),
  155. SUITE(TLS_DH_RSA_WITH_AES_128_GCM_SHA256),
  156. SUITE(TLS_DH_RSA_WITH_AES_256_CBC_SHA256),
  157. SUITE(TLS_DH_RSA_WITH_AES_256_GCM_SHA384),
  158. SUITE(TLS_DH_anon_WITH_3DES_EDE_CBC_SHA),
  159. SUITE(TLS_DH_anon_WITH_AES_128_CBC_SHA256),
  160. SUITE(TLS_DH_anon_WITH_AES_128_GCM_SHA256),
  161. SUITE(TLS_DH_anon_WITH_AES_256_CBC_SHA256),
  162. SUITE(TLS_DH_anon_WITH_AES_256_GCM_SHA384),
  163. SUITE(TLS_DH_anon_WITH_RC4_128_MD5),
  164. SUITE(TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256),
  165. SUITE(TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
  166. SUITE(TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384),
  167. SUITE(TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384),
  168. SUITE(TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256),
  169. SUITE(TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256),
  170. SUITE(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384),
  171. SUITE(TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384),
  172. SUITE(TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256),
  173. SUITE(TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256),
  174. SUITE(TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384),
  175. SUITE(TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384),
  176. SUITE(TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256),
  177. SUITE(TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256),
  178. SUITE(TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384),
  179. SUITE(TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384),
  180. SUITE(TLS_EMPTY_RENEGOTIATION_INFO_SCSV),
  181. SUITE(TLS_NULL_WITH_NULL_NULL),
  182. SUITE(TLS_RSA_WITH_3DES_EDE_CBC_SHA),
  183. SUITE(TLS_RSA_WITH_AES_128_CBC_SHA256),
  184. SUITE(TLS_RSA_WITH_AES_128_GCM_SHA256),
  185. SUITE(TLS_RSA_WITH_AES_256_CBC_SHA256),
  186. SUITE(TLS_RSA_WITH_AES_256_GCM_SHA384),
  187. SUITE(TLS_RSA_WITH_NULL_MD5),
  188. SUITE(TLS_RSA_WITH_NULL_SHA),
  189. SUITE(TLS_RSA_WITH_NULL_SHA256),
  190. SUITE(TLS_RSA_WITH_RC4_128_MD5),
  191. SUITE(TLS_RSA_WITH_RC4_128_SHA),
  192. #endif
  193. SUITE(SSL_NO_SUCH_CIPHERSUITE)
  194. };
  195. static const size_t nSuites = sizeof(kSuites) / sizeof(*kSuites);
  196. #undef SUITE
  197. static inline const char* suiteToString(const SSLCipherSuite suite)
  198. {
  199. for (size_t i = 0; i < nSuites; ++i) {
  200. if (kSuites[i].suite == suite) {
  201. return kSuites[i].name;
  202. }
  203. }
  204. return "Unknown suite";
  205. }
  206. static const char* kBlocked[] = {
  207. "NULL", "anon", "MD5", "EXPORT", "DES", "IDEA", "NO_SUCH", "EMPTY"
  208. };
  209. static const size_t nBlocked = sizeof(kBlocked) / sizeof(*kBlocked);
  210. static inline bool isBlockedSuite(SSLCipherSuite suite)
  211. {
  212. const char* name = suiteToString(suite);
  213. for (size_t i = 0; i < nBlocked; ++i) {
  214. if (strstr(name, kBlocked[i])) {
  215. return true;
  216. }
  217. }
  218. return false;
  219. }
  220. typedef std::vector<SSLCipherSuite> SSLCipherSuiteList;
  221. static SSLCipherSuiteList constructEnabledSuites(SSLContextRef ctx)
  222. {
  223. #ifndef CIPHER_CONSTRUCT_ALWAYS
  224. static
  225. #endif
  226. SSLCipherSuiteList rv(0);
  227. #ifndef CIPHER_CONSTRUCT_ALWAYS
  228. if (!rv.empty()) {
  229. return rv;
  230. }
  231. #endif
  232. size_t supported = 0;
  233. OSStatus err = SSLGetNumberSupportedCiphers(ctx, &supported);
  234. if (err != noErr || !supported) {
  235. return rv;
  236. }
  237. rv.resize(supported, SSL_NO_SUCH_CIPHERSUITE);
  238. err = SSLGetSupportedCiphers(ctx, &rv[0], &supported);
  239. if (err != noErr || !supported) {
  240. rv.clear();
  241. return rv;
  242. }
  243. rv.erase(std::remove_if(rv.begin(), rv.end(), isBlockedSuite), rv.end());
  244. return rv;
  245. }
  246. }
  247. namespace aria2 {
  248. TLSSession* TLSSession::make(TLSContext* ctx)
  249. {
  250. return new AppleTLSSession(static_cast<AppleTLSContext*>(ctx));
  251. }
  252. AppleTLSSession::AppleTLSSession(AppleTLSContext* ctx)
  253. : ctx_(ctx),
  254. sslCtx_(0),
  255. sockfd_(0),
  256. state_(st_constructed),
  257. lastError_(noErr),
  258. writeBuffered_(0)
  259. {
  260. lastError_ = SSLNewContext(ctx->getSide() == TLS_SERVER, &sslCtx_) == noErr;
  261. if (lastError_ == noErr) {
  262. state_ = st_error;
  263. return;
  264. }
  265. #if defined(__MAC_10_8)
  266. (void)SSLSetProtocolVersionMin(sslCtx_, kSSLProtocol3);
  267. (void)SSLSetProtocolVersionMax(sslCtx_, kTLSProtocol12);
  268. #else
  269. (void)SSLSetProtocolVersionEnabled(sslCtx_, kSSLProtocolAll, false);
  270. (void)SSLSetProtocolVersionEnabled(sslCtx_, kSSLProtocol3, true);
  271. (void)SSLSetProtocolVersionEnabled(sslCtx_, kTLSProtocol1, true);
  272. (void)SSLSetProtocolVersionEnabled(sslCtx_, kTLSProtocol11, true);
  273. (void)SSLSetProtocolVersionEnabled(sslCtx_, kTLSProtocol12, true);
  274. #endif
  275. (void)SSLSetEnableCertVerify(sslCtx_, ctx->getVerifyPeer());
  276. #ifndef CIPHER_ENABLE_ALL
  277. SSLCipherSuiteList enabled = constructEnabledSuites(sslCtx_);
  278. if (enabled.empty()) {
  279. A2_LOG_ERROR("AppleTLS: Failed to construct enabled ciphers list");
  280. state_ = st_error;
  281. return;
  282. }
  283. for (SSLCipherSuiteList::iterator i = enabled.begin(), e = enabled.end(); i != e; ++i) {
  284. A2_LOG_INFO(fmt("AppleTLS: Enabled suite %s", suiteToString(*i)));
  285. }
  286. if (SSLSetEnabledCiphers(sslCtx_, &enabled[0], enabled.size()) != noErr) {
  287. A2_LOG_ERROR("AppleTLS: Failed to set enabled ciphers list");
  288. state_ = st_error;
  289. }
  290. #endif
  291. }
  292. AppleTLSSession::~AppleTLSSession()
  293. {
  294. closeConnection();
  295. if (sslCtx_) {
  296. SSLDisposeContext(sslCtx_);
  297. sslCtx_ = 0;
  298. }
  299. state_ = st_error;
  300. }
  301. int AppleTLSSession::init(sock_t sockfd)
  302. {
  303. if (state_ != st_constructed) {
  304. lastError_ = noErr;
  305. return TLS_ERR_ERROR;
  306. }
  307. lastError_ = SSLSetIOFuncs(sslCtx_, SocketRead, SocketWrite);
  308. if (lastError_ != noErr) {
  309. state_ = st_error;
  310. return TLS_ERR_ERROR;
  311. }
  312. lastError_ = SSLSetConnection(sslCtx_, this);
  313. if (lastError_ != noErr) {
  314. state_ = st_error;
  315. return TLS_ERR_ERROR;
  316. }
  317. sockfd_ = sockfd;
  318. state_ = st_initialized;
  319. return TLS_ERR_OK;
  320. }
  321. int AppleTLSSession::setSNIHostname(const std::string& hostname)
  322. {
  323. if (state_ != st_initialized) {
  324. lastError_ = noErr;
  325. return TLS_ERR_ERROR;
  326. }
  327. lastError_ = SSLSetPeerDomainName(sslCtx_, hostname.c_str(), hostname.length());
  328. return (lastError_ != noErr) ? TLS_ERR_ERROR : TLS_ERR_OK;
  329. }
  330. int AppleTLSSession::closeConnection()
  331. {
  332. if (state_ != st_connected) {
  333. lastError_ = noErr;
  334. return TLS_ERR_ERROR;
  335. }
  336. lastError_ = SSLClose(sslCtx_);
  337. state_ = st_closed;
  338. return lastError_ == noErr ? TLS_ERR_OK : TLS_ERR_ERROR;
  339. }
  340. int AppleTLSSession::checkDirection() {
  341. // See: https://github.com/tatsuhiro-t/aria2/pull/61#issuecomment-16051793
  342. if (state_ == st_connected) {
  343. // Need to check read first, as SocketCore kinda expects this
  344. size_t buffered;
  345. lastError_ = SSLGetBufferedReadSize(sslCtx_, &buffered);
  346. if (lastError_ == noErr && buffered) {
  347. return TLS_WANT_READ;
  348. }
  349. }
  350. if (writeBuffered_) {
  351. return TLS_WANT_WRITE;
  352. }
  353. // Default to WANT_READ, as SocketCore kinda expects this
  354. return TLS_WANT_READ;
  355. }
  356. ssize_t AppleTLSSession::writeData(const void* data, size_t len)
  357. {
  358. if (state_ != st_connected) {
  359. lastError_ = noErr;
  360. return TLS_ERR_ERROR;
  361. }
  362. size_t processed = 0;
  363. if (writeBuffered_) {
  364. lastError_ = SSLWrite(sslCtx_, 0, 0, &processed);
  365. switch (lastError_) {
  366. case noErr:
  367. processed = writeBuffered_;
  368. writeBuffered_ = 0;
  369. return processed;
  370. case errSSLWouldBlock:
  371. return TLS_ERR_WOULDBLOCK;
  372. case errSSLClosedGraceful:
  373. case errSSLClosedNoNotify:
  374. closeConnection();
  375. return TLS_ERR_ERROR;
  376. default:
  377. closeConnection();
  378. state_ = st_error;
  379. return TLS_ERR_ERROR;
  380. }
  381. }
  382. lastError_ = SSLWrite(sslCtx_, data, len, &processed);
  383. switch (lastError_) {
  384. case noErr:
  385. return processed;
  386. case errSSLWouldBlock:
  387. writeBuffered_ = len;
  388. return TLS_ERR_WOULDBLOCK;
  389. case errSSLClosedGraceful:
  390. case errSSLClosedNoNotify:
  391. closeConnection();
  392. return TLS_ERR_ERROR;
  393. default:
  394. closeConnection();
  395. state_ = st_error;
  396. return TLS_ERR_ERROR;
  397. }
  398. }
  399. OSStatus AppleTLSSession::sockWrite(const void* data, size_t* len)
  400. {
  401. size_t remain = *len;
  402. const uint8_t *buffer = static_cast<const uint8_t*>(data);
  403. *len = 0;
  404. while (remain) {
  405. ssize_t w = write(sockfd_, buffer, remain);
  406. if (w <= 0) {
  407. switch (errno) {
  408. case EAGAIN:
  409. return errSSLWouldBlock;
  410. default:
  411. return errSSLClosedAbort;
  412. }
  413. }
  414. remain -= w;
  415. buffer += w;
  416. *len += w;
  417. }
  418. return noErr;
  419. }
  420. ssize_t AppleTLSSession::readData(void* data, size_t len)
  421. {
  422. if (state_ != st_connected) {
  423. lastError_ = noErr;
  424. return TLS_ERR_ERROR;
  425. }
  426. size_t processed = 0;
  427. lastError_ = SSLRead(sslCtx_, data, len, &processed);
  428. switch (lastError_) {
  429. case noErr:
  430. return processed;
  431. case errSSLWouldBlock:
  432. if (processed) {
  433. return processed;
  434. }
  435. return TLS_ERR_WOULDBLOCK;
  436. case errSSLClosedGraceful:
  437. case errSSLClosedNoNotify:
  438. closeConnection();
  439. return TLS_ERR_ERROR;
  440. default:
  441. closeConnection();
  442. state_ = st_error;
  443. return TLS_ERR_ERROR;
  444. }
  445. }
  446. OSStatus AppleTLSSession::sockRead(void* data, size_t* len)
  447. {
  448. size_t remain = *len;
  449. uint8_t *buffer = static_cast<uint8_t*>(data);
  450. *len = 0;
  451. while (remain) {
  452. ssize_t r = read(sockfd_, buffer, remain);
  453. if (r == 0) {
  454. return errSSLClosedGraceful;
  455. }
  456. if (r < 0) {
  457. switch (errno) {
  458. case ENOENT:
  459. return errSSLClosedGraceful;
  460. case ECONNRESET:
  461. return errSSLClosedAbort;
  462. case EAGAIN:
  463. return errSSLWouldBlock;
  464. default:
  465. return errSSLClosedAbort;
  466. }
  467. }
  468. remain -= r;
  469. buffer += r;
  470. *len += r;
  471. }
  472. return noErr;
  473. }
  474. int AppleTLSSession::tlsConnect(const std::string& hostname, std::string& handshakeErr)
  475. {
  476. if (state_ != st_initialized) {
  477. return TLS_ERR_ERROR;
  478. }
  479. if (!hostname.empty()) {
  480. setSNIHostname(hostname);
  481. }
  482. lastError_ = SSLHandshake(sslCtx_);
  483. switch (lastError_) {
  484. case noErr:
  485. break;
  486. case errSSLWouldBlock:
  487. return TLS_ERR_WOULDBLOCK;
  488. case errSSLServerAuthCompleted:
  489. return tlsConnect(hostname, handshakeErr);
  490. default:
  491. handshakeErr = getLastErrorString();
  492. return TLS_ERR_ERROR;
  493. }
  494. state_ = st_connected;
  495. SSLProtocol proto = kSSLProtocolUnknown;
  496. (void)SSLGetNegotiatedProtocolVersion(sslCtx_, &proto);
  497. SSLCipherSuite suite = SSL_NO_SUCH_CIPHERSUITE;
  498. (void)SSLGetNegotiatedCipher(sslCtx_, &suite);
  499. A2_LOG_INFO(fmt("AppleTLS: Connected to %s with %s (%s)",
  500. hostname.c_str(),
  501. protoToString(proto),
  502. suiteToString(suite)));
  503. return TLS_ERR_OK;
  504. }
  505. int AppleTLSSession::tlsAccept()
  506. {
  507. std::string hostname, err;
  508. return tlsConnect(hostname, err);
  509. }
  510. std::string AppleTLSSession::getLastErrorString()
  511. {
  512. switch (lastError_) {
  513. case errSSLProtocol:
  514. return "Protocol error";
  515. case errSSLNegotiation:
  516. return "No common cipher suites";
  517. case errSSLFatalAlert:
  518. return "Received fatal alert";
  519. case errSSLSessionNotFound:
  520. return "Unknown session";
  521. case errSSLClosedGraceful:
  522. return "Closed gracefully";
  523. case errSSLClosedAbort:
  524. return "Connection aborted";
  525. case errSSLXCertChainInvalid:
  526. return "Invalid certificate chain";
  527. case errSSLBadCert:
  528. return "Invalid certificate format";
  529. case errSSLCrypto:
  530. return "Cryptographic error";
  531. case paramErr:
  532. case errSSLInternal:
  533. return "Internal SSL error";
  534. case errSSLUnknownRootCert:
  535. return "Self-signed certificate";
  536. case errSSLNoRootCert:
  537. return "No root certificate";
  538. case errSSLCertExpired:
  539. return "Certificate expired";
  540. case errSSLCertNotYetValid:
  541. return "Certificate not yet valid";
  542. case errSSLClosedNoNotify:
  543. return "Closed without notification";
  544. case errSSLBufferOverflow:
  545. return "Buffer not large enough";
  546. case errSSLBadCipherSuite:
  547. return "Bad cipher suite";
  548. case errSSLPeerUnexpectedMsg:
  549. return "Unexpected peer message";
  550. case errSSLPeerBadRecordMac:
  551. return "Bad MAC";
  552. case errSSLPeerDecryptionFail:
  553. return "Decryption failure";
  554. case errSSLHostNameMismatch:
  555. return "Invalid hostname";
  556. case errSSLConnectionRefused:
  557. return "Connection refused";
  558. default:
  559. return fmt("Unspecified error %d", lastError_);
  560. }
  561. }
  562. }