MSEHandshake.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <memory>
  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 DownloadContext;
  49. class MessageDigest;
  50. class MSEHandshake {
  51. public:
  52. enum HANDSHAKE_TYPE {
  53. HANDSHAKE_NOT_YET = 0,
  54. HANDSHAKE_LEGACY,
  55. HANDSHAKE_ENCRYPTED
  56. };
  57. enum CRYPTO_TYPE {
  58. CRYPTO_NONE = 0,
  59. CRYPTO_PLAIN_TEXT = 0x01u,
  60. CRYPTO_ARC4 = 0x02u
  61. };
  62. static constexpr size_t VC_LENGTH = 8U;
  63. private:
  64. static constexpr size_t PRIME_BITS = 768U;
  65. static constexpr size_t KEY_LENGTH = (PRIME_BITS + 7U) / 8U;
  66. // The largest buffering occurs when receiver receives step2
  67. // handshake. We believe that IA is less than or equal to
  68. // BtHandshakeMessage::MESSAGE_LENGTH
  69. static constexpr size_t MAX_BUFFER_LENGTH = 636U;
  70. cuid_t cuid_;
  71. std::shared_ptr<SocketCore> socket_;
  72. bool wantRead_;
  73. const Option* option_;
  74. unsigned char rbuf_[MAX_BUFFER_LENGTH];
  75. size_t rbufLength_;
  76. SocketBuffer socketBuffer_;
  77. CRYPTO_TYPE negotiatedCryptoType_;
  78. std::unique_ptr<DHKeyExchange> dh_;
  79. std::unique_ptr<ARC4Encryptor> encryptor_;
  80. std::unique_ptr<ARC4Encryptor> 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. std::vector<unsigned char> ia_;
  89. std::unique_ptr<MessageDigest> sha1_;
  90. void encryptAndSendData(std::vector<unsigned char> data);
  91. void createReq1Hash(unsigned char* md) const;
  92. void createReq23Hash(unsigned char* md, const unsigned char* infoHash) const;
  93. uint16_t decodeLength16(const unsigned char* buffer);
  94. uint16_t decodeLength16(const char* buffer)
  95. {
  96. return decodeLength16(reinterpret_cast<const unsigned char*>(buffer));
  97. }
  98. uint16_t verifyPadLength(const unsigned char* padlenbuf, const char* padName);
  99. void verifyVC(unsigned char* vcbuf);
  100. void verifyReq1Hash(const unsigned char* req1buf);
  101. void shiftBuffer(size_t offset);
  102. public:
  103. MSEHandshake(cuid_t cuid, const std::shared_ptr<SocketCore>& socket,
  104. const Option* op);
  105. ~MSEHandshake();
  106. HANDSHAKE_TYPE identifyHandshakeType();
  107. void initEncryptionFacility(bool initiator);
  108. // Reads data from Socket. If EOF is reached, throws
  109. // RecoverableException.
  110. void read();
  111. // Sends pending data in the send buffer. Returns true if all data
  112. // is sent. Otherwise returns false.
  113. bool send();
  114. bool getWantRead() const { return wantRead_; }
  115. void setWantRead(bool wantRead) { wantRead_ = wantRead; }
  116. bool getWantWrite() const;
  117. void sendPublicKey();
  118. bool receivePublicKey();
  119. void initCipher(const unsigned char* infoHash);
  120. void sendInitiatorStep2();
  121. bool findInitiatorVCMarker();
  122. bool receiveInitiatorCryptoSelectAndPadDLength();
  123. bool receivePad();
  124. bool findReceiverHashMarker();
  125. bool receiveReceiverHashAndPadCLength(
  126. const std::vector<std::shared_ptr<DownloadContext>>& downloadContexts);
  127. bool receiveReceiverIALength();
  128. bool receiveReceiverIA();
  129. void sendReceiverStep2();
  130. // returns plain text IA
  131. const unsigned char* getIA() const { return ia_.data(); }
  132. size_t getIALength() const { return iaLength_; }
  133. const unsigned char* getInfoHash() const { return infoHash_; }
  134. CRYPTO_TYPE getNegotiatedCryptoType() const { return negotiatedCryptoType_; }
  135. const std::unique_ptr<ARC4Encryptor>& getEncryptor() const
  136. {
  137. return encryptor_;
  138. }
  139. const std::unique_ptr<ARC4Encryptor>& getDecryptor() const
  140. {
  141. return decryptor_;
  142. }
  143. std::unique_ptr<ARC4Encryptor> popEncryptor();
  144. std::unique_ptr<ARC4Encryptor> popDecryptor();
  145. const unsigned char* getBuffer() const { return rbuf_; }
  146. unsigned char* getBuffer() { return rbuf_; }
  147. size_t getBufferLength() const { return rbufLength_; }
  148. };
  149. } // namespace aria2
  150. #endif // D_MSE_HANDSHAKE_H