MSEHandshake.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #ifndef D_MSE_HANDSHAKE_H
  36. #define D_MSE_HANDSHAKE_H
  37. #include "common.h"
  38. #include <vector>
  39. #include "SharedHandle.h"
  40. #include "BtConstants.h"
  41. #include "SocketBuffer.h"
  42. #include "Command.h"
  43. namespace aria2 {
  44. class Option;
  45. class SocketCore;
  46. class DHKeyExchange;
  47. class ARC4Encryptor;
  48. class ARC4Decryptor;
  49. class DownloadContext;
  50. class MessageDigest;
  51. class MSEHandshake {
  52. public:
  53. enum HANDSHAKE_TYPE {
  54. HANDSHAKE_NOT_YET = 0,
  55. HANDSHAKE_LEGACY,
  56. HANDSHAKE_ENCRYPTED
  57. };
  58. enum CRYPTO_TYPE {
  59. CRYPTO_NONE = 0,
  60. CRYPTO_PLAIN_TEXT = 0x01u,
  61. CRYPTO_ARC4 = 0x02u
  62. };
  63. private:
  64. static const size_t PRIME_BITS = 768;
  65. static const size_t KEY_LENGTH = (PRIME_BITS+7)/8;
  66. static const size_t VC_LENGTH = 8;
  67. // The largest buffering occurs when receiver receives step2
  68. // handshake. We believe that IA is less than or equal to
  69. // BtHandshakeMessage::MESSAGE_LENGTH
  70. static const size_t MAX_BUFFER_LENGTH = 636;
  71. cuid_t cuid_;
  72. SharedHandle<SocketCore> socket_;
  73. bool wantRead_;
  74. const Option* option_;
  75. unsigned char rbuf_[MAX_BUFFER_LENGTH];
  76. size_t rbufLength_;
  77. SocketBuffer socketBuffer_;
  78. CRYPTO_TYPE negotiatedCryptoType_;
  79. DHKeyExchange* dh_;
  80. SharedHandle<ARC4Encryptor> encryptor_;
  81. SharedHandle<ARC4Decryptor> decryptor_;
  82. unsigned char infoHash_[INFO_HASH_LENGTH];
  83. unsigned char secret_[KEY_LENGTH];
  84. bool initiator_;
  85. unsigned char initiatorVCMarker_[VC_LENGTH];
  86. size_t markerIndex_;
  87. uint16_t padLength_;
  88. uint16_t iaLength_;
  89. unsigned char* ia_;
  90. SharedHandle<MessageDigest> sha1_;
  91. void encryptAndSendData(const unsigned char* data, size_t length);
  92. void createReq1Hash(unsigned char* md) const;
  93. void createReq23Hash(unsigned char* md, const unsigned char* infoHash) const;
  94. uint16_t decodeLength16(const unsigned char* buffer);
  95. uint16_t decodeLength16(const char* buffer)
  96. {
  97. return decodeLength16(reinterpret_cast<const unsigned char*>(buffer));
  98. }
  99. uint16_t verifyPadLength(const unsigned char* padlenbuf,
  100. const char* padName);
  101. void verifyVC(const unsigned char* vcbuf);
  102. void verifyReq1Hash(const unsigned char* req1buf);
  103. void shiftBuffer(size_t offset);
  104. public:
  105. MSEHandshake(cuid_t cuid, const SharedHandle<SocketCore>& socket,
  106. const Option* op);
  107. ~MSEHandshake();
  108. HANDSHAKE_TYPE identifyHandshakeType();
  109. void initEncryptionFacility(bool initiator);
  110. // Reads data from Socket. If EOF is reached, throws
  111. // RecoverableException.
  112. void read();
  113. // Sends pending data in the send buffer. Returns true if all data
  114. // is sent. Otherwise returns false.
  115. bool send();
  116. bool getWantRead() const
  117. {
  118. return wantRead_;
  119. }
  120. void setWantRead(bool wantRead)
  121. {
  122. wantRead_ = wantRead;
  123. }
  124. bool getWantWrite() const;
  125. void sendPublicKey();
  126. bool receivePublicKey();
  127. void initCipher(const unsigned char* infoHash);
  128. void sendInitiatorStep2();
  129. bool findInitiatorVCMarker();
  130. bool receiveInitiatorCryptoSelectAndPadDLength();
  131. bool receivePad();
  132. bool findReceiverHashMarker();
  133. bool receiveReceiverHashAndPadCLength
  134. (const std::vector<SharedHandle<DownloadContext> >& downloadContexts);
  135. bool receiveReceiverIALength();
  136. bool receiveReceiverIA();
  137. void sendReceiverStep2();
  138. // returns plain text IA
  139. const unsigned char* getIA() const
  140. {
  141. return ia_;
  142. }
  143. size_t getIALength() const
  144. {
  145. return iaLength_;
  146. }
  147. const unsigned char* getInfoHash() const
  148. {
  149. return infoHash_;
  150. }
  151. CRYPTO_TYPE getNegotiatedCryptoType() const
  152. {
  153. return negotiatedCryptoType_;
  154. }
  155. const SharedHandle<ARC4Encryptor>& getEncryptor() const
  156. {
  157. return encryptor_;
  158. }
  159. const SharedHandle<ARC4Decryptor>& getDecryptor() const
  160. {
  161. return decryptor_;
  162. }
  163. const unsigned char* getBuffer() const
  164. {
  165. return rbuf_;
  166. }
  167. size_t getBufferLength() const
  168. {
  169. return rbufLength_;
  170. }
  171. };
  172. } // namespace aria2
  173. #endif // D_MSE_HANDSHAKE_H