MSEHandshake.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 Logger;
  46. class SocketCore;
  47. class DHKeyExchange;
  48. class ARC4Encryptor;
  49. class ARC4Decryptor;
  50. class DownloadContext;
  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 = 0x01,
  61. CRYPTO_ARC4 = 0x02
  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 MAX_PAD_LENGTH = 512;
  67. static const size_t VC_LENGTH = 8;
  68. static const size_t CRYPTO_BITFIELD_LENGTH = 4;
  69. static const size_t MAX_BUFFER_LENGTH = 6*1024;
  70. cuid_t _cuid;
  71. SharedHandle<SocketCore> _socket;
  72. const Option* _option;
  73. Logger* _logger;
  74. unsigned char _rbuf[MAX_BUFFER_LENGTH];
  75. size_t _rbufLength;
  76. SocketBuffer _socketBuffer;
  77. CRYPTO_TYPE _negotiatedCryptoType;
  78. DHKeyExchange* _dh;
  79. SharedHandle<ARC4Encryptor> _encryptor;
  80. SharedHandle<ARC4Decryptor> _decryptor;
  81. unsigned char _infoHash[INFO_HASH_LENGTH];
  82. unsigned char _secret[KEY_LENGTH];
  83. bool _initiator;
  84. unsigned char _initiatorVCMarker[VC_LENGTH];
  85. size_t _markerIndex;
  86. uint16_t _padLength;
  87. uint16_t _iaLength;
  88. unsigned char* _ia;
  89. static const unsigned char* PRIME;
  90. static const unsigned char* GENERATOR;
  91. static const unsigned char VC[VC_LENGTH];
  92. void encryptAndSendData(const unsigned char* data, size_t length);
  93. void createReq1Hash(unsigned char* md) const;
  94. void createReq23Hash(unsigned char* md, const unsigned char* infoHash) const;
  95. uint16_t decodeLength16(const unsigned char* buffer);
  96. uint16_t decodeLength16(const char* buffer)
  97. {
  98. return decodeLength16(reinterpret_cast<const unsigned char*>(buffer));
  99. }
  100. uint16_t verifyPadLength(const unsigned char* padlenbuf,
  101. const char* padName);
  102. void verifyVC(const unsigned char* vcbuf);
  103. void verifyReq1Hash(const unsigned char* req1buf);
  104. size_t receiveNBytes(size_t bytes);
  105. public:
  106. MSEHandshake(cuid_t cuid, const SharedHandle<SocketCore>& socket,
  107. const Option* op);
  108. ~MSEHandshake();
  109. HANDSHAKE_TYPE identifyHandshakeType();
  110. void initEncryptionFacility(bool initiator);
  111. bool sendPublicKey();
  112. bool receivePublicKey();
  113. void initCipher(const unsigned char* infoHash);
  114. bool sendInitiatorStep2();
  115. bool findInitiatorVCMarker();
  116. bool receiveInitiatorCryptoSelectAndPadDLength();
  117. bool receivePad();
  118. bool findReceiverHashMarker();
  119. bool receiveReceiverHashAndPadCLength
  120. (const std::vector<SharedHandle<DownloadContext> >& downloadContexts);
  121. bool receiveReceiverIALength();
  122. bool receiveReceiverIA();
  123. bool sendReceiverStep2();
  124. // returns plain text IA
  125. const unsigned char* getIA() const
  126. {
  127. return _ia;
  128. }
  129. size_t getIALength() const
  130. {
  131. return _iaLength;
  132. }
  133. const unsigned char* getInfoHash() const
  134. {
  135. return _infoHash;
  136. }
  137. CRYPTO_TYPE getNegotiatedCryptoType() const
  138. {
  139. return _negotiatedCryptoType;
  140. }
  141. const SharedHandle<ARC4Encryptor>& getEncryptor() const
  142. {
  143. return _encryptor;
  144. }
  145. const SharedHandle<ARC4Decryptor>& getDecryptor() const
  146. {
  147. return _decryptor;
  148. }
  149. const unsigned char* getBuffer() const
  150. {
  151. return _rbuf;
  152. }
  153. size_t getBufferLength() const
  154. {
  155. return _rbufLength;
  156. }
  157. };
  158. } // namespace aria2
  159. #endif // _D_MSE_HANDSHAKE_H_