MSEHandshake.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 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 "MSEHandshake.h"
  36. #include "message.h"
  37. #include "DlAbortEx.h"
  38. #include "LogFactory.h"
  39. #include "Logger.h"
  40. #include "BtHandshakeMessage.h"
  41. #include "Socket.h"
  42. #include "a2netcompat.h"
  43. #include "DHKeyExchange.h"
  44. #include "ARC4Encryptor.h"
  45. #include "ARC4Decryptor.h"
  46. #include "MessageDigestHelper.h"
  47. #include "SimpleRandomizer.h"
  48. #include "Util.h"
  49. #include "BtRegistry.h"
  50. #include "BtContext.h"
  51. #include "prefs.h"
  52. #include "Option.h"
  53. #include "StringFormat.h"
  54. #include <cstring>
  55. #include <cassert>
  56. namespace aria2 {
  57. const unsigned char* MSEHandshake::PRIME = reinterpret_cast<const unsigned char*>("FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A36210000000000090563");
  58. const unsigned char* MSEHandshake::GENERATOR = reinterpret_cast<const unsigned char*>("2");
  59. const unsigned char MSEHandshake::VC[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  60. MSEHandshake::MSEHandshake(int32_t cuid,
  61. const SocketHandle& socket,
  62. const Option* op):
  63. _cuid(cuid),
  64. _socket(socket),
  65. _option(op),
  66. _logger(LogFactory::getInstance()),
  67. _rbufLength(0),
  68. _negotiatedCryptoType(CRYPTO_NONE),
  69. _dh(0),
  70. _initiator(true),
  71. _markerIndex(0),
  72. _padLength(0),
  73. _iaLength(0),
  74. _ia(0)
  75. {}
  76. MSEHandshake::~MSEHandshake()
  77. {
  78. delete _dh;
  79. delete [] _ia;
  80. }
  81. MSEHandshake::HANDSHAKE_TYPE MSEHandshake::identifyHandshakeType()
  82. {
  83. if(!_socket->isReadable(0)) {
  84. return HANDSHAKE_NOT_YET;
  85. }
  86. size_t r = 20-_rbufLength;
  87. _socket->readData(_rbuf+_rbufLength, r);
  88. if(r == 0) {
  89. throw DlAbortEx(EX_EOF_FROM_PEER);
  90. }
  91. _rbufLength += r;
  92. if(_rbufLength < 20) {
  93. return HANDSHAKE_NOT_YET;
  94. }
  95. if(_rbuf[0] == BtHandshakeMessage::PSTR_LENGTH &&
  96. memcmp(BtHandshakeMessage::BT_PSTR, _rbuf+1, 19) == 0) {
  97. _logger->debug("CUID#%d - This is legacy BitTorrent handshake.", _cuid);
  98. return HANDSHAKE_LEGACY;
  99. } else {
  100. _logger->debug("CUID#%d - This may be encrypted BitTorrent handshake.", _cuid);
  101. return HANDSHAKE_ENCRYPTED;
  102. }
  103. }
  104. void MSEHandshake::initEncryptionFacility(bool initiator)
  105. {
  106. delete _dh;
  107. _dh = new DHKeyExchange();
  108. _dh->init(PRIME, PRIME_BITS, GENERATOR, 160);
  109. _dh->generatePublicKey();
  110. _logger->debug("CUID#%d - DH initialized.", _cuid);
  111. _initiator = initiator;
  112. }
  113. void MSEHandshake::sendPublicKey()
  114. {
  115. _logger->debug("CUID#%d - Sending public key.", _cuid);
  116. unsigned char buffer[KEY_LENGTH+MAX_PAD_LENGTH];
  117. _dh->getPublicKey(buffer, KEY_LENGTH);
  118. size_t padLength = SimpleRandomizer::getInstance()->getRandomNumber(MAX_PAD_LENGTH+1);
  119. _dh->generateNonce(buffer+KEY_LENGTH, padLength);
  120. _socket->writeData(buffer, KEY_LENGTH+padLength);
  121. }
  122. bool MSEHandshake::receivePublicKey()
  123. {
  124. size_t r = KEY_LENGTH-_rbufLength;
  125. if(r > receiveNBytes(r)) {
  126. return false;
  127. }
  128. _logger->debug("CUID#%d - public key received.", _cuid);
  129. // TODO handle exception. in catch, resbufLength = 0;
  130. _dh->computeSecret(_secret, sizeof(_secret), _rbuf, _rbufLength);
  131. // reset _rbufLength
  132. _rbufLength = 0;
  133. return true;
  134. }
  135. void MSEHandshake::initCipher(const unsigned char* infoHash)
  136. {
  137. memcpy(_infoHash, infoHash, INFO_HASH_LENGTH);
  138. //Initialize cipher
  139. unsigned char s[4+KEY_LENGTH+INFO_HASH_LENGTH];
  140. memcpy(s, _initiator?"keyA":"keyB", 4);
  141. memcpy(s+4, _secret, KEY_LENGTH);
  142. memcpy(s+4+KEY_LENGTH, infoHash, INFO_HASH_LENGTH);
  143. unsigned char localCipherKey[20];
  144. MessageDigestHelper::digest(localCipherKey, sizeof(localCipherKey),
  145. MessageDigestContext::SHA1,
  146. s, sizeof(s));
  147. _encryptor.reset(new ARC4Encryptor());
  148. _encryptor->init(localCipherKey, sizeof(localCipherKey));
  149. unsigned char peerCipherKey[20];
  150. memcpy(s, _initiator?"keyB":"keyA", 4);
  151. MessageDigestHelper::digest(peerCipherKey, sizeof(peerCipherKey),
  152. MessageDigestContext::SHA1,
  153. s, sizeof(s));
  154. _decryptor.reset(new ARC4Decryptor());
  155. _decryptor->init(peerCipherKey, sizeof(peerCipherKey));
  156. // discard first 1024 bytes ARC4 output.
  157. unsigned char from[1024];
  158. unsigned char to[1024];
  159. _encryptor->encrypt(to, 1024, from, 1024);
  160. _decryptor->decrypt(to, 1024, from, 1024);
  161. if(_initiator) {
  162. ARC4Encryptor enc;
  163. enc.init(peerCipherKey, sizeof(peerCipherKey));
  164. // discard first 1024 bytes ARC4 output.
  165. enc.encrypt(to, 1024, from, 1024);
  166. enc.encrypt(_initiatorVCMarker, sizeof(_initiatorVCMarker), VC, sizeof(VC));
  167. }
  168. }
  169. void MSEHandshake::encryptAndSendData(const unsigned char* data, size_t length)
  170. {
  171. unsigned char temp[4096];
  172. const unsigned char* dptr = data;
  173. size_t s;
  174. size_t r = length;
  175. while(r > 0) {
  176. s = std::min(r, sizeof(temp));
  177. _encryptor->encrypt(temp, s, dptr, s);
  178. _socket->writeData(temp, s);
  179. dptr += s;
  180. r -= s;
  181. }
  182. }
  183. void MSEHandshake::createReq1Hash(unsigned char* md) const
  184. {
  185. unsigned char buffer[100];
  186. memcpy(buffer, "req1", 4);
  187. memcpy(buffer+4, _secret, KEY_LENGTH);
  188. MessageDigestHelper::digest(md, 20, MessageDigestContext::SHA1,
  189. buffer, 4+KEY_LENGTH);
  190. }
  191. void MSEHandshake::createReq23Hash(unsigned char* md, const unsigned char* infoHash) const
  192. {
  193. unsigned char x[24];
  194. memcpy(x, "req2", 4);
  195. memcpy(x+4, infoHash, INFO_HASH_LENGTH);
  196. unsigned char xh[20];
  197. MessageDigestHelper::digest(xh, sizeof(xh), MessageDigestContext::SHA1,
  198. x, sizeof(x));
  199. unsigned char y[4+96];
  200. memcpy(y, "req3", 4);
  201. memcpy(y+4, _secret, KEY_LENGTH);
  202. unsigned char yh[20];
  203. MessageDigestHelper::digest(yh, sizeof(yh), MessageDigestContext::SHA1,
  204. y, sizeof(y));
  205. for(size_t i = 0; i < 20; ++i) {
  206. md[i] = xh[i]^yh[i];
  207. }
  208. }
  209. uint16_t MSEHandshake::decodeLength16(const unsigned char* buffer)
  210. {
  211. uint16_t be;
  212. _decryptor->decrypt(reinterpret_cast<unsigned char*>(&be),
  213. sizeof(be),
  214. buffer, sizeof(be));
  215. return ntohs(be);
  216. }
  217. void MSEHandshake::sendInitiatorStep2()
  218. {
  219. _logger->debug("CUID#%d - Sending negotiation step2.", _cuid);
  220. unsigned char md[20];
  221. createReq1Hash(md);
  222. _socket->writeData(md, sizeof(md));
  223. createReq23Hash(md, _infoHash);
  224. _socket->writeData(md, sizeof(md));
  225. {
  226. unsigned char buffer[8+4+2+MAX_PAD_LENGTH+2];
  227. // VC
  228. memcpy(buffer, VC, sizeof(VC));
  229. // crypto_provide
  230. unsigned char cryptoProvide[4];
  231. memset(cryptoProvide, 0, sizeof(cryptoProvide));
  232. if(_option->get(PREF_BT_MIN_CRYPTO_LEVEL) == V_PLAIN) {
  233. cryptoProvide[3] = CRYPTO_PLAIN_TEXT;
  234. }
  235. cryptoProvide[3] |= CRYPTO_ARC4;
  236. memcpy(buffer+8, cryptoProvide, sizeof(cryptoProvide));
  237. // len(padC)
  238. uint16_t padCLength = SimpleRandomizer::getInstance()->getRandomNumber(MAX_PAD_LENGTH+1);
  239. {
  240. uint16_t padCLengthBE = htons(padCLength);
  241. memcpy(buffer+8+4, &padCLengthBE, sizeof(padCLengthBE));
  242. }
  243. // padC
  244. memset(buffer+8+4+2, 0, padCLength);
  245. // len(IA)
  246. // currently, IA is zero-length.
  247. uint16_t iaLength = 0;
  248. {
  249. uint16_t iaLengthBE = htons(iaLength);
  250. memcpy(buffer+8+4+2+padCLength, &iaLengthBE, sizeof(iaLengthBE));
  251. }
  252. encryptAndSendData(buffer, 8+4+2+padCLength+2);
  253. }
  254. }
  255. // This function reads exactly until the end of VC marker is reached.
  256. bool MSEHandshake::findInitiatorVCMarker()
  257. {
  258. // 616 is synchronization point of initiator
  259. size_t r = 616-KEY_LENGTH-_rbufLength;
  260. if(!_socket->isReadable(0)) {
  261. return false;
  262. }
  263. _socket->peekData(_rbuf+_rbufLength, r);
  264. if(r == 0) {
  265. throw DlAbortEx(EX_EOF_FROM_PEER);
  266. }
  267. // find vc
  268. {
  269. std::string buf(&_rbuf[0], &_rbuf[_rbufLength+r]);
  270. std::string vc(&_initiatorVCMarker[0], &_initiatorVCMarker[VC_LENGTH]);
  271. if((_markerIndex = buf.find(vc)) == std::string::npos) {
  272. if(616-KEY_LENGTH <= _rbufLength+r) {
  273. throw DlAbortEx("Failed to find VC marker.");
  274. } else {
  275. _socket->readData(_rbuf+_rbufLength, r);
  276. _rbufLength += r;
  277. return false;
  278. }
  279. }
  280. }
  281. assert(_markerIndex+VC_LENGTH-_rbufLength <= r);
  282. size_t toRead = _markerIndex+VC_LENGTH-_rbufLength;
  283. _socket->readData(_rbuf+_rbufLength, toRead);
  284. _rbufLength += toRead;
  285. _logger->debug("CUID#%d - VC marker found at %u", _cuid, _markerIndex);
  286. verifyVC(_rbuf+_markerIndex);
  287. // reset _rbufLength
  288. _rbufLength = 0;
  289. return true;
  290. }
  291. bool MSEHandshake::receiveInitiatorCryptoSelectAndPadDLength()
  292. {
  293. size_t r = CRYPTO_BITFIELD_LENGTH+2/* PadD length*/-_rbufLength;
  294. if(r > receiveNBytes(r)) {
  295. return false;
  296. }
  297. //verifyCryptoSelect
  298. unsigned char* rbufptr = _rbuf;
  299. {
  300. unsigned char cryptoSelect[CRYPTO_BITFIELD_LENGTH];
  301. _decryptor->decrypt(cryptoSelect, sizeof(cryptoSelect),
  302. rbufptr, sizeof(cryptoSelect));
  303. if(cryptoSelect[3]&CRYPTO_PLAIN_TEXT &&
  304. _option->get(PREF_BT_MIN_CRYPTO_LEVEL) == V_PLAIN) {
  305. _logger->debug("CUID#%d - peer prefers plaintext.", _cuid);
  306. _negotiatedCryptoType = CRYPTO_PLAIN_TEXT;
  307. }
  308. if(cryptoSelect[3]&CRYPTO_ARC4) {
  309. _logger->debug("CUID#%d - peer prefers ARC4", _cuid);
  310. _negotiatedCryptoType = CRYPTO_ARC4;
  311. }
  312. if(_negotiatedCryptoType == CRYPTO_NONE) {
  313. throw DlAbortEx
  314. (StringFormat("CUID#%d - No supported crypto type selected.", _cuid).str());
  315. }
  316. }
  317. // padD length
  318. rbufptr += CRYPTO_BITFIELD_LENGTH;
  319. _padLength = verifyPadLength(rbufptr, "PadD");
  320. // reset _rbufLength
  321. _rbufLength = 0;
  322. return true;
  323. }
  324. bool MSEHandshake::receivePad()
  325. {
  326. if(_padLength == 0) {
  327. return true;
  328. }
  329. size_t r = _padLength-_rbufLength;
  330. if(r > receiveNBytes(r)) {
  331. return false;
  332. }
  333. unsigned char temp[MAX_PAD_LENGTH];
  334. _decryptor->decrypt(temp, _padLength, _rbuf, _padLength);
  335. // reset _rbufLength
  336. _rbufLength = 0;
  337. return true;
  338. }
  339. bool MSEHandshake::findReceiverHashMarker()
  340. {
  341. // 628 is synchronization limit of receiver.
  342. size_t r = 628-KEY_LENGTH-_rbufLength;
  343. if(!_socket->isReadable(0)) {
  344. return false;
  345. }
  346. _socket->peekData(_rbuf+_rbufLength, r);
  347. if(r == 0) {
  348. throw DlAbortEx(EX_EOF_FROM_PEER);
  349. }
  350. // find hash('req1', S), S is _secret.
  351. {
  352. std::string buf(&_rbuf[0], &_rbuf[_rbufLength+r]);
  353. unsigned char md[20];
  354. createReq1Hash(md);
  355. std::string req1(&md[0], &md[sizeof(md)]);
  356. if((_markerIndex = buf.find(req1)) == std::string::npos) {
  357. if(628-KEY_LENGTH <= _rbufLength+r) {
  358. throw DlAbortEx("Failed to find hash marker.");
  359. } else {
  360. _socket->readData(_rbuf+_rbufLength, r);
  361. _rbufLength += r;
  362. return false;
  363. }
  364. }
  365. }
  366. assert(_markerIndex+20-_rbufLength <= r);
  367. size_t toRead = _markerIndex+20-_rbufLength;
  368. _socket->readData(_rbuf+_rbufLength, toRead);
  369. _rbufLength += toRead;
  370. _logger->debug("CUID#%d - Hash marker found at %u.", _cuid, _markerIndex);
  371. verifyReq1Hash(_rbuf+_markerIndex);
  372. // reset _rbufLength
  373. _rbufLength = 0;
  374. return true;
  375. }
  376. bool MSEHandshake::receiveReceiverHashAndPadCLength()
  377. {
  378. size_t r = 20+VC_LENGTH+CRYPTO_BITFIELD_LENGTH+2/*PadC length*/-_rbufLength;
  379. if(r > receiveNBytes(r)) {
  380. return false;
  381. }
  382. // resolve info hash
  383. std::deque<SharedHandle<BtContext> > btContexts = BtRegistry::getAllBtContext();
  384. // pointing to the position of HASH('req2', SKEY) xor HASH('req3', S)
  385. unsigned char* rbufptr = _rbuf;
  386. SharedHandle<BtContext> btContext;
  387. for(std::deque<SharedHandle<BtContext> >::const_iterator i = btContexts.begin();
  388. i != btContexts.end(); ++i) {
  389. unsigned char md[20];
  390. createReq23Hash(md, (*i)->getInfoHash());
  391. if(memcmp(md, rbufptr, sizeof(md)) == 0) {
  392. _logger->debug("CUID#%d - info hash found: %s", _cuid, (*i)->getInfoHashAsString().c_str());
  393. btContext = *i;
  394. break;
  395. }
  396. }
  397. if(btContext.isNull()) {
  398. throw DlAbortEx("Unknown info hash.");
  399. }
  400. initCipher(btContext->getInfoHash());
  401. // decrypt VC
  402. rbufptr += 20;
  403. verifyVC(rbufptr);
  404. // decrypt crypto_provide
  405. rbufptr += VC_LENGTH;
  406. {
  407. unsigned char cryptoProvide[4];
  408. _decryptor->decrypt(cryptoProvide, sizeof(cryptoProvide),
  409. rbufptr, sizeof(cryptoProvide));
  410. // TODO choose the crypto type based on the preference.
  411. // For now, choose ARC4.
  412. if(cryptoProvide[3]&CRYPTO_PLAIN_TEXT &&
  413. _option->get(PREF_BT_MIN_CRYPTO_LEVEL) == V_PLAIN) {
  414. _logger->debug("CUID#%d - peer provides plaintext.", _cuid);
  415. _negotiatedCryptoType = CRYPTO_PLAIN_TEXT;
  416. } else if(cryptoProvide[3]&CRYPTO_ARC4) {
  417. _logger->debug("CUID#%d - peer provides ARC4.", _cuid);
  418. _negotiatedCryptoType = CRYPTO_ARC4;
  419. }
  420. if(_negotiatedCryptoType == CRYPTO_NONE) {
  421. throw DlAbortEx
  422. (StringFormat("CUID#%d - No supported crypto type provided.", _cuid).str());
  423. }
  424. }
  425. // decrypt PadC length
  426. rbufptr += CRYPTO_BITFIELD_LENGTH;
  427. _padLength = verifyPadLength(rbufptr, "PadC");
  428. // reset _rbufLength
  429. _rbufLength = 0;
  430. return true;
  431. }
  432. bool MSEHandshake::receiveReceiverIALength()
  433. {
  434. size_t r = 2-_rbufLength;
  435. assert(r > 0);
  436. if(r > receiveNBytes(r)) {
  437. return false;
  438. }
  439. _iaLength = decodeLength16(_rbuf);
  440. _logger->debug("CUID#%d - len(IA)=%u.", _cuid, _iaLength);
  441. // reset _rbufLength
  442. _rbufLength = 0;
  443. return true;
  444. }
  445. bool MSEHandshake::receiveReceiverIA()
  446. {
  447. if(_iaLength == 0) {
  448. return true;
  449. }
  450. size_t r = _iaLength-_rbufLength;
  451. if(r > receiveNBytes(r)) {
  452. return false;
  453. }
  454. delete [] _ia;
  455. _ia = new unsigned char[_iaLength];
  456. _decryptor->decrypt(_ia, _iaLength, _rbuf, _iaLength);
  457. _logger->debug("CUID#%d - IA received.", _cuid);
  458. // reset _rbufLength
  459. _rbufLength = 0;
  460. return true;
  461. }
  462. void MSEHandshake::sendReceiverStep2()
  463. {
  464. unsigned char buffer[8+4+2+MAX_PAD_LENGTH];
  465. // VC
  466. memcpy(buffer, VC, sizeof(VC));
  467. // crypto_select
  468. unsigned char cryptoSelect[4];
  469. memset(cryptoSelect, 0, sizeof(cryptoSelect));
  470. cryptoSelect[3] = _negotiatedCryptoType;
  471. memcpy(buffer+8, cryptoSelect, sizeof(cryptoSelect));
  472. // len(padD)
  473. uint16_t padDLength = SimpleRandomizer::getInstance()->getRandomNumber(MAX_PAD_LENGTH+1);
  474. {
  475. uint16_t padDLengthBE = htons(padDLength);
  476. memcpy(buffer+8+4, &padDLengthBE, sizeof(padDLengthBE));
  477. }
  478. // padD, all zeroed
  479. memset(buffer+8+4+2, 0, padDLength);
  480. encryptAndSendData(buffer, 8+4+2+padDLength);
  481. }
  482. uint16_t MSEHandshake::verifyPadLength(const unsigned char* padlenbuf, const std::string& padName)
  483. {
  484. _logger->debug("CUID#%d - Veryfying Pad length for %s", _cuid, padName.c_str());
  485. uint16_t padLength = decodeLength16(padlenbuf);
  486. _logger->debug("CUID#%d - len(%s)=%u", _cuid, padName.c_str(), padLength);
  487. if(padLength > 512) {
  488. throw DlAbortEx
  489. (StringFormat("Too large %s length: %u", padName.c_str(), padLength).str());
  490. }
  491. return padLength;
  492. }
  493. void MSEHandshake::verifyVC(const unsigned char* vcbuf)
  494. {
  495. _logger->debug("CUID#%d - Veryfying VC.", _cuid);
  496. unsigned char vc[VC_LENGTH];
  497. _decryptor->decrypt(vc, sizeof(vc), vcbuf, sizeof(vc));
  498. if(memcmp(VC, vc, sizeof(VC)) != 0) {
  499. throw DlAbortEx
  500. (StringFormat("Invalid VC: %s", Util::toHex(vc, VC_LENGTH).c_str()).str());
  501. }
  502. }
  503. void MSEHandshake::verifyReq1Hash(const unsigned char* req1buf)
  504. {
  505. _logger->debug("CUID#%d - Verifying req hash.", _cuid);
  506. unsigned char md[20];
  507. createReq1Hash(md);
  508. if(memcmp(md, req1buf, sizeof(md)) != 0) {
  509. throw DlAbortEx("Invalid req1 hash found.");
  510. }
  511. }
  512. size_t MSEHandshake::receiveNBytes(size_t bytes)
  513. {
  514. size_t r = bytes;
  515. if(r > 0) {
  516. if(!_socket->isReadable(0)) {
  517. return 0;
  518. }
  519. _socket->readData(_rbuf+_rbufLength, r);
  520. if(r == 0) {
  521. throw DlAbortEx(EX_EOF_FROM_PEER);
  522. }
  523. _rbufLength += r;
  524. }
  525. return r;
  526. }
  527. const unsigned char* MSEHandshake::getIA() const
  528. {
  529. return _ia;
  530. }
  531. size_t MSEHandshake::getIALength() const
  532. {
  533. return _iaLength;
  534. }
  535. const unsigned char* MSEHandshake::getInfoHash() const
  536. {
  537. return _infoHash;
  538. }
  539. MSEHandshake::CRYPTO_TYPE MSEHandshake::getNegotiatedCryptoType() const
  540. {
  541. return _negotiatedCryptoType;
  542. }
  543. SharedHandle<ARC4Encryptor> MSEHandshake::getEncryptor() const
  544. {
  545. return _encryptor;
  546. }
  547. SharedHandle<ARC4Decryptor> MSEHandshake::getDecryptor() const
  548. {
  549. return _decryptor;
  550. }
  551. const unsigned char* MSEHandshake::getBuffer() const
  552. {
  553. return _rbuf;
  554. }
  555. size_t MSEHandshake::getBufferLength() const
  556. {
  557. return _rbufLength;
  558. }
  559. } // namespace aria2