RLPxHandshake.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file RLPXHandshake.h
  15. * @author Alex Leverington <nessence@gmail.com>
  16. * @date 2015
  17. */
  18. #pragma once
  19. #include <memory>
  20. #include <libdevcrypto/Common.h>
  21. #include <libdevcrypto/ECDHE.h>
  22. #include "RLPXSocket.h"
  23. #include "RLPXFrameCoder.h"
  24. #include "Common.h"
  25. namespace ba = boost::asio;
  26. namespace bi = boost::asio::ip;
  27. namespace dev
  28. {
  29. namespace p2p
  30. {
  31. static const unsigned c_rlpxVersion = 4;
  32. /**
  33. * @brief Setup inbound or outbound connection for communication over RLPXFrameCoder.
  34. * RLPx Spec: https://github.com/ethereum/devp2p/blob/master/rlpx.md#encrypted-handshake
  35. *
  36. * @todo Implement StartSession transition via lambda which is passed to constructor.
  37. *
  38. * Thread Safety
  39. * Distinct Objects: Safe.
  40. * Shared objects: Unsafe.
  41. */
  42. class RLPXHandshake: public std::enable_shared_from_this<RLPXHandshake>
  43. {
  44. friend class RLPXFrameCoder;
  45. public:
  46. /// Setup incoming connection.
  47. RLPXHandshake(Host* _host, std::shared_ptr<RLPXSocket> const& _socket): m_host(_host), m_originated(false), m_socket(_socket), m_idleTimer(m_socket->ref().get_io_service()) { crypto::Nonce::get().ref().copyTo(m_nonce.ref()); }
  48. /// Setup outbound connection.
  49. RLPXHandshake(Host* _host, std::shared_ptr<RLPXSocket> const& _socket, NodeID _remote): m_host(_host), m_remote(_remote), m_originated(true), m_socket(_socket), m_idleTimer(m_socket->ref().get_io_service()) { crypto::Nonce::get().ref().copyTo(m_nonce.ref()); }
  50. ~RLPXHandshake() {}
  51. /// Start handshake.
  52. void start() { transition(); }
  53. /// Aborts the handshake.
  54. void cancel();
  55. protected:
  56. /// Sequential states of handshake
  57. enum State
  58. {
  59. Error = -1,
  60. New,
  61. AckAuth,
  62. AckAuthEIP8,
  63. WriteHello,
  64. ReadHello,
  65. StartSession
  66. };
  67. /// Write Auth message to socket and transitions to AckAuth.
  68. void writeAuth();
  69. /// Reads Auth message from socket and transitions to AckAuth.
  70. void readAuth();
  71. /// Continues reading Auth message in EIP-8 format and transitions to AckAuthEIP8.
  72. void readAuthEIP8();
  73. /// Derives ephemeral secret from signature and sets members after Auth has been decrypted.
  74. void setAuthValues(Signature const& sig, Public const& remotePubk, h256 const& remoteNonce, uint64_t remoteVersion);
  75. /// Write Ack message to socket and transitions to WriteHello.
  76. void writeAck();
  77. /// Write Ack message in EIP-8 format to socket and transitions to WriteHello.
  78. void writeAckEIP8();
  79. /// Reads Auth message from socket and transitions to WriteHello.
  80. void readAck();
  81. /// Continues reading Ack message in EIP-8 format and transitions to WriteHello.
  82. void readAckEIP8();
  83. /// Closes connection and ends transitions.
  84. void error();
  85. /// Performs transition for m_nextState.
  86. virtual void transition(boost::system::error_code _ech = boost::system::error_code());
  87. /// Timeout for remote to respond to transition events. Enforced by m_idleTimer and refreshed by transition().
  88. boost::posix_time::milliseconds const c_timeout = boost::posix_time::milliseconds(1800);
  89. State m_nextState = New; ///< Current or expected state of transition.
  90. bool m_cancel = false; ///< Will be set to true if connection was canceled.
  91. Host* m_host; ///< Host which provides m_alias, protocolVersion(), m_clientVersion, caps(), and TCP listenPort().
  92. /// Node id of remote host for socket.
  93. NodeID m_remote; ///< Public address of remote host.
  94. bool m_originated = false; ///< True if connection is outbound.
  95. /// Buffers for encoded and decoded handshake phases
  96. bytes m_auth; ///< Plaintext of egress or ingress Auth message.
  97. bytes m_authCipher; ///< Ciphertext of egress or ingress Auth message.
  98. bytes m_ack; ///< Plaintext of egress or ingress Ack message.
  99. bytes m_ackCipher; ///< Ciphertext of egress or ingress Ack message.
  100. bytes m_handshakeOutBuffer; ///< Frame buffer for egress Hello packet.
  101. bytes m_handshakeInBuffer; ///< Frame buffer for ingress Hello packet.
  102. crypto::ECDHE m_ecdhe; ///< Ephemeral ECDH secret and agreement.
  103. h256 m_nonce; ///< Nonce generated by this host for handshake.
  104. Public m_remoteEphemeral; ///< Remote ephemeral public key.
  105. h256 m_remoteNonce; ///< Nonce generated by remote host for handshake.
  106. uint64_t m_remoteVersion;
  107. /// Used to read and write RLPx encrypted frames for last step of handshake authentication.
  108. /// Passed onto Host which will take ownership.
  109. std::unique_ptr<RLPXFrameCoder> m_io;
  110. std::shared_ptr<RLPXSocket> m_socket; ///< Socket.
  111. boost::asio::deadline_timer m_idleTimer; ///< Timer which enforces c_timeout.
  112. };
  113. }
  114. }