MSEHandshake.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "SharedHandle.h"
  39. #include "BtConstants.h"
  40. namespace aria2 {
  41. class Option;
  42. class Logger;
  43. class SocketCore;
  44. class DHKeyExchange;
  45. class ARC4Encryptor;
  46. class ARC4Decryptor;
  47. class MSEHandshake {
  48. public:
  49. enum HANDSHAKE_TYPE {
  50. HANDSHAKE_NOT_YET = 0,
  51. HANDSHAKE_LEGACY,
  52. HANDSHAKE_ENCRYPTED
  53. };
  54. enum CRYPTO_TYPE {
  55. CRYPTO_NONE = 0,
  56. CRYPTO_PLAIN_TEXT = 0x01,
  57. CRYPTO_ARC4 = 0x02
  58. };
  59. private:
  60. static const size_t PRIME_BITS = 768;
  61. static const size_t KEY_LENGTH = (PRIME_BITS+7)/8;
  62. static const size_t MAX_PAD_LENGTH = 512;
  63. static const size_t VC_LENGTH = 8;
  64. static const size_t CRYPTO_BITFIELD_LENGTH = 4;
  65. static const size_t MAX_BUFFER_LENGTH = 6*1024;
  66. int32_t _cuid;
  67. SharedHandle<SocketCore> _socket;
  68. const Option* _option;
  69. Logger* _logger;
  70. unsigned char _rbuf[MAX_BUFFER_LENGTH];
  71. size_t _rbufLength;
  72. CRYPTO_TYPE _negotiatedCryptoType;
  73. DHKeyExchange* _dh;
  74. SharedHandle<ARC4Encryptor> _encryptor;
  75. SharedHandle<ARC4Decryptor> _decryptor;
  76. unsigned char _infoHash[INFO_HASH_LENGTH];
  77. unsigned char _secret[KEY_LENGTH];
  78. bool _initiator;
  79. unsigned char _initiatorVCMarker[VC_LENGTH];
  80. size_t _markerIndex;
  81. uint16_t _padLength;
  82. uint16_t _iaLength;
  83. unsigned char* _ia;
  84. static const unsigned char* PRIME;
  85. static const unsigned char* GENERATOR;
  86. static const unsigned char VC[VC_LENGTH];
  87. void encryptAndSendData(const unsigned char* data, size_t length);
  88. void createReq1Hash(unsigned char* md) const;
  89. void createReq23Hash(unsigned char* md, const unsigned char* infoHash) const;
  90. uint16_t decodeLength16(const unsigned char* buffer);
  91. uint16_t decodeLength16(const char* buffer)
  92. {
  93. return decodeLength16(reinterpret_cast<const unsigned char*>(buffer));
  94. }
  95. uint16_t verifyPadLength(const unsigned char* padlenbuf,
  96. const char* padName);
  97. void verifyVC(const unsigned char* vcbuf);
  98. void verifyReq1Hash(const unsigned char* req1buf);
  99. size_t receiveNBytes(size_t bytes);
  100. public:
  101. MSEHandshake(int32_t cuid, const SharedHandle<SocketCore>& socket,
  102. const Option* op);
  103. ~MSEHandshake();
  104. HANDSHAKE_TYPE identifyHandshakeType();
  105. void initEncryptionFacility(bool initiator);
  106. void sendPublicKey();
  107. bool receivePublicKey();
  108. void initCipher(const unsigned char* infoHash);
  109. void sendInitiatorStep2();
  110. bool findInitiatorVCMarker();
  111. bool receiveInitiatorCryptoSelectAndPadDLength();
  112. bool receivePad();
  113. bool findReceiverHashMarker();
  114. bool receiveReceiverHashAndPadCLength();
  115. bool receiveReceiverIALength();
  116. bool receiveReceiverIA();
  117. void sendReceiverStep2();
  118. // returns plain text IA
  119. const unsigned char* getIA() const;
  120. size_t getIALength() const;
  121. const unsigned char* getInfoHash() const;
  122. CRYPTO_TYPE getNegotiatedCryptoType() const;
  123. SharedHandle<ARC4Encryptor> getEncryptor() const;
  124. SharedHandle<ARC4Decryptor> getDecryptor() const;
  125. const unsigned char* getBuffer() const;
  126. size_t getBufferLength() const;
  127. };
  128. } // namespace aria2
  129. #endif // _D_MSE_HANDSHAKE_H_