ReceiverMSEHandshakeCommand.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "ReceiverMSEHandshakeCommand.h"
  36. #include "PeerReceiveHandshakeCommand.h"
  37. #include "PeerConnection.h"
  38. #include "DownloadEngine.h"
  39. #include "BtHandshakeMessage.h"
  40. #include "DlAbortEx.h"
  41. #include "Peer.h"
  42. #include "message.h"
  43. #include "Socket.h"
  44. #include "Logger.h"
  45. #include "prefs.h"
  46. #include "Option.h"
  47. #include "MSEHandshake.h"
  48. #include "ARC4Encryptor.h"
  49. #include "ARC4Decryptor.h"
  50. #include "RequestGroupMan.h"
  51. namespace aria2 {
  52. ReceiverMSEHandshakeCommand::ReceiverMSEHandshakeCommand
  53. (int32_t cuid,
  54. const SharedHandle<Peer>& peer,
  55. DownloadEngine* e,
  56. const SharedHandle<SocketCore>& s):
  57. PeerAbstractCommand(cuid, peer, e, s),
  58. _sequence(RECEIVER_IDENTIFY_HANDSHAKE),
  59. _mseHandshake(new MSEHandshake(cuid, s, e->option))
  60. {
  61. setTimeout(e->option->getAsInt(PREF_PEER_CONNECTION_TIMEOUT));
  62. }
  63. ReceiverMSEHandshakeCommand::~ReceiverMSEHandshakeCommand()
  64. {
  65. delete _mseHandshake;
  66. }
  67. bool ReceiverMSEHandshakeCommand::exitBeforeExecute()
  68. {
  69. return e->isHaltRequested() || e->_requestGroupMan->downloadFinished();
  70. }
  71. bool ReceiverMSEHandshakeCommand::executeInternal()
  72. {
  73. switch(_sequence) {
  74. case RECEIVER_IDENTIFY_HANDSHAKE: {
  75. MSEHandshake::HANDSHAKE_TYPE type = _mseHandshake->identifyHandshakeType();
  76. switch(type) {
  77. case MSEHandshake::HANDSHAKE_NOT_YET:
  78. break;
  79. case MSEHandshake::HANDSHAKE_ENCRYPTED:
  80. _mseHandshake->initEncryptionFacility(false);
  81. _sequence = RECEIVER_WAIT_KEY;
  82. break;
  83. case MSEHandshake::HANDSHAKE_LEGACY: {
  84. if(e->option->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
  85. throw DlAbortEx("The legacy BitTorrent handshake is not acceptable by the preference.");
  86. }
  87. SharedHandle<PeerConnection> peerConnection
  88. (new PeerConnection(cuid, socket, e->option));
  89. peerConnection->presetBuffer(_mseHandshake->getBuffer(),
  90. _mseHandshake->getBufferLength());
  91. Command* c = new PeerReceiveHandshakeCommand(cuid, peer, e, socket,
  92. peerConnection);
  93. e->commands.push_back(c);
  94. return true;
  95. }
  96. default:
  97. throw DlAbortEx("Not supported handshake type.");
  98. }
  99. break;
  100. }
  101. case RECEIVER_WAIT_KEY: {
  102. if(_mseHandshake->receivePublicKey()) {
  103. _mseHandshake->sendPublicKey();
  104. _sequence = RECEIVER_FIND_HASH_MARKER;
  105. }
  106. break;
  107. }
  108. case RECEIVER_FIND_HASH_MARKER: {
  109. if(_mseHandshake->findReceiverHashMarker()) {
  110. _sequence = RECEIVER_RECEIVE_PAD_C_LENGTH;
  111. }
  112. break;
  113. }
  114. case RECEIVER_RECEIVE_PAD_C_LENGTH: {
  115. if(_mseHandshake->receiveReceiverHashAndPadCLength()) {
  116. _sequence = RECEIVER_RECEIVE_PAD_C;
  117. }
  118. break;
  119. }
  120. case RECEIVER_RECEIVE_PAD_C: {
  121. if(_mseHandshake->receivePad()) {
  122. _sequence = RECEIVER_RECEIVE_IA_LENGTH;
  123. }
  124. break;
  125. }
  126. case RECEIVER_RECEIVE_IA_LENGTH: {
  127. if(_mseHandshake->receiveReceiverIALength()) {
  128. _sequence = RECEIVER_RECEIVE_IA;
  129. }
  130. break;
  131. }
  132. case RECEIVER_RECEIVE_IA: {
  133. if(_mseHandshake->receiveReceiverIA()) {
  134. _mseHandshake->sendReceiverStep2();
  135. SharedHandle<PeerConnection> peerConnection
  136. (new PeerConnection(cuid, socket, e->option));
  137. if(_mseHandshake->getNegotiatedCryptoType() == MSEHandshake::CRYPTO_ARC4) {
  138. peerConnection->enableEncryption(_mseHandshake->getEncryptor(),
  139. _mseHandshake->getDecryptor());
  140. }
  141. if(_mseHandshake->getIALength() > 0) {
  142. peerConnection->presetBuffer(_mseHandshake->getIA(),
  143. _mseHandshake->getIALength());
  144. }
  145. // TODO add _mseHandshake->getInfoHash() to PeerReceiveHandshakeCommand
  146. // as a hint. If this info hash and one in BitTorrent Handshake does not
  147. // match, then drop connection.
  148. Command* c =
  149. new PeerReceiveHandshakeCommand(cuid, peer, e, socket, peerConnection);
  150. e->commands.push_back(c);
  151. return true;
  152. }
  153. break;
  154. }
  155. }
  156. e->commands.push_back(this);
  157. return false;
  158. }
  159. } // namespace aria2