2018-06-05 16:53:13 +00:00
|
|
|
#ifndef NTCP2_H__
|
|
|
|
#define NTCP2_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <memory>
|
2018-06-11 18:05:30 +00:00
|
|
|
#include <thread>
|
2018-07-18 15:16:40 +00:00
|
|
|
#include <list>
|
2018-07-18 16:58:29 +00:00
|
|
|
#include <map>
|
2018-08-08 20:23:44 +00:00
|
|
|
#include <array>
|
2018-07-03 20:26:02 +00:00
|
|
|
#include <openssl/bn.h>
|
2018-06-06 19:38:18 +00:00
|
|
|
#include <boost/asio.hpp>
|
2018-08-08 20:23:44 +00:00
|
|
|
#include "util.h"
|
2018-06-05 16:53:13 +00:00
|
|
|
#include "RouterInfo.h"
|
|
|
|
#include "TransportSession.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace transport
|
|
|
|
{
|
2018-07-18 15:16:40 +00:00
|
|
|
|
|
|
|
const size_t NTCP2_UNENCRYPTED_FRAME_MAX_SIZE = 65519;
|
|
|
|
const int NTCP2_MAX_PADDING_RATIO = 6; // in %
|
2018-08-03 17:10:32 +00:00
|
|
|
|
|
|
|
const int NTCP2_CONNECT_TIMEOUT = 5; // 5 seconds
|
|
|
|
const int NTCP2_ESTABLISH_TIMEOUT = 10; // 10 seconds
|
|
|
|
const int NTCP2_TERMINATION_TIMEOUT = 120; // 2 minutes
|
|
|
|
const int NTCP2_TERMINATION_CHECK_TIMEOUT = 30; // 30 seconds
|
|
|
|
|
2018-07-17 19:17:05 +00:00
|
|
|
enum NTCP2BlockType
|
|
|
|
{
|
|
|
|
eNTCP2BlkDateTime = 0,
|
|
|
|
eNTCP2BlkOptions, // 1
|
|
|
|
eNTCP2BlkRouterInfo, // 2
|
|
|
|
eNTCP2BlkI2NPMessage, // 3
|
|
|
|
eNTCP2BlkTermination, // 4
|
|
|
|
eNTCP2BlkPadding = 254
|
|
|
|
};
|
|
|
|
|
2018-08-01 13:43:48 +00:00
|
|
|
enum NTCP2TerminationReason
|
|
|
|
{
|
|
|
|
eNTCP2NormalClose = 0,
|
|
|
|
eNTCP2TerminationReceived, // 1
|
|
|
|
eNTCP2IdleTimeout, // 2
|
|
|
|
eNTCP2RouterShutdown, // 3
|
|
|
|
eNTCP2DataPhaseAEADFailure, // 4
|
|
|
|
eNTCP2IncompatibleOptions, // 5
|
|
|
|
eNTCP2IncompatibleSignatureType, // 6
|
|
|
|
eNTCP2ClockSkew, // 7
|
|
|
|
eNTCP2PaddingViolation, // 8
|
2018-08-03 17:10:32 +00:00
|
|
|
eNTCP2AEADFramingError, // 9
|
2018-08-02 19:31:15 +00:00
|
|
|
eNTCP2PayloadFormatError, // 10
|
2018-08-01 13:43:48 +00:00
|
|
|
eNTCP2Message1Error, // 11
|
|
|
|
eNTCP2Message2Error, // 12
|
|
|
|
eNTCP2Message3Error, // 13
|
|
|
|
eNTCP2IntraFrameReadTimeout, // 14
|
|
|
|
eNTCP2RouterInfoSignatureVerificationFail, // 15
|
|
|
|
eNTCP2IncorrectSParameter, // 16
|
|
|
|
eNTCP2Banned, // 17
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-08-08 20:23:44 +00:00
|
|
|
typedef std::array<uint8_t, NTCP2_UNENCRYPTED_FRAME_MAX_SIZE> NTCP2FrameBuffer;
|
2018-07-03 20:26:02 +00:00
|
|
|
struct NTCP2Establisher
|
|
|
|
{
|
2018-07-04 18:15:40 +00:00
|
|
|
NTCP2Establisher ();
|
|
|
|
~NTCP2Establisher ();
|
2018-07-03 20:26:02 +00:00
|
|
|
|
2018-07-04 18:15:40 +00:00
|
|
|
const uint8_t * GetPub () const { return m_EphemeralPublicKey; };
|
|
|
|
const uint8_t * GetPriv () const { return m_EphemeralPrivateKey; };
|
|
|
|
const uint8_t * GetRemotePub () const { return m_RemoteEphemeralPublicKey; }; // Y for Alice and X for Bob
|
|
|
|
uint8_t * GetRemotePub () { return m_RemoteEphemeralPublicKey; }; // to set
|
|
|
|
|
|
|
|
const uint8_t * GetK () const { return m_K; };
|
2018-07-03 20:26:02 +00:00
|
|
|
const uint8_t * GetCK () const { return m_CK; };
|
|
|
|
const uint8_t * GetH () const { return m_H; };
|
|
|
|
|
2018-07-04 18:15:40 +00:00
|
|
|
void KDF1Alice ();
|
|
|
|
void KDF1Bob ();
|
2018-08-09 16:53:36 +00:00
|
|
|
void KDF2Alice ();
|
|
|
|
void KDF2Bob ();
|
2018-07-09 19:56:23 +00:00
|
|
|
void KDF3Alice (); // for SessionConfirmed part 2
|
|
|
|
void KDF3Bob ();
|
2018-07-31 19:41:13 +00:00
|
|
|
|
|
|
|
void MixKey (const uint8_t * inputKeyMaterial, uint8_t * derived);
|
|
|
|
void KeyDerivationFunction1 (const uint8_t * pub, const uint8_t * priv, const uint8_t * rs, const uint8_t * epub); // for SessionRequest, (pub, priv) for DH
|
|
|
|
void KeyDerivationFunction2 (const uint8_t * sessionRequest, size_t sessionRequestLen, const uint8_t * epub); // for SessionCreate
|
2018-07-04 18:15:40 +00:00
|
|
|
void CreateEphemeralKey ();
|
|
|
|
|
2018-08-10 14:53:34 +00:00
|
|
|
void CreateSessionRequestMessage ();
|
|
|
|
void CreateSessionCreatedMessage ();
|
|
|
|
void CreateSessionConfirmedMessagePart1 (const uint8_t * nonce);
|
|
|
|
void CreateSessionConfirmedMessagePart2 (const uint8_t * nonce);
|
2018-07-03 20:26:02 +00:00
|
|
|
|
|
|
|
BN_CTX * m_Ctx;
|
2018-07-04 18:15:40 +00:00
|
|
|
uint8_t m_EphemeralPrivateKey[32], m_EphemeralPublicKey[32], m_RemoteEphemeralPublicKey[32]; // x25519
|
|
|
|
uint8_t m_RemoteStaticKey[32], m_IV[16], m_H[32] /*h*/, m_CK[33] /*ck*/, m_K[32] /*k*/;
|
2018-08-10 14:53:34 +00:00
|
|
|
i2p::data::IdentHash m_RemoteIdentHash;
|
2018-07-09 19:56:23 +00:00
|
|
|
uint16_t m3p2Len;
|
2018-08-09 16:53:36 +00:00
|
|
|
|
|
|
|
uint8_t * m_SessionRequestBuffer, * m_SessionCreatedBuffer, * m_SessionConfirmedBuffer;
|
|
|
|
size_t m_SessionRequestBufferLen, m_SessionCreatedBufferLen;
|
|
|
|
|
2018-07-03 20:26:02 +00:00
|
|
|
};
|
|
|
|
|
2018-06-06 19:38:18 +00:00
|
|
|
class NTCP2Server;
|
2018-06-05 16:53:13 +00:00
|
|
|
class NTCP2Session: public TransportSession, public std::enable_shared_from_this<NTCP2Session>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2018-06-11 18:05:30 +00:00
|
|
|
NTCP2Session (NTCP2Server& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter = nullptr);
|
2018-06-05 16:53:13 +00:00
|
|
|
~NTCP2Session ();
|
2018-06-11 16:29:30 +00:00
|
|
|
void Terminate ();
|
2018-08-03 17:10:32 +00:00
|
|
|
void TerminateByTimeout ();
|
2018-06-11 18:05:30 +00:00
|
|
|
void Done ();
|
2018-06-05 16:53:13 +00:00
|
|
|
|
2018-06-06 19:38:18 +00:00
|
|
|
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
|
|
|
|
|
2018-07-18 16:58:29 +00:00
|
|
|
bool IsEstablished () const { return m_IsEstablished; };
|
|
|
|
bool IsTerminated () const { return m_IsTerminated; };
|
|
|
|
|
2018-06-06 19:38:18 +00:00
|
|
|
void ClientLogin (); // Alice
|
2018-06-19 19:43:47 +00:00
|
|
|
void ServerLogin (); // Bob
|
2018-07-18 15:16:40 +00:00
|
|
|
void SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs);
|
2018-06-06 19:38:18 +00:00
|
|
|
|
2018-06-05 16:53:13 +00:00
|
|
|
private:
|
|
|
|
|
2018-07-13 19:59:28 +00:00
|
|
|
void Established ();
|
|
|
|
|
2018-06-25 16:28:07 +00:00
|
|
|
void CreateNonce (uint64_t seqn, uint8_t * nonce);
|
2018-06-21 16:39:24 +00:00
|
|
|
void KeyDerivationFunctionDataPhase ();
|
2018-06-13 20:16:23 +00:00
|
|
|
|
2018-06-21 20:24:19 +00:00
|
|
|
// establish
|
2018-06-06 19:38:18 +00:00
|
|
|
void SendSessionRequest ();
|
2018-06-19 19:43:47 +00:00
|
|
|
void SendSessionCreated ();
|
2018-06-14 19:29:36 +00:00
|
|
|
void SendSessionConfirmed ();
|
2018-06-06 19:38:18 +00:00
|
|
|
|
|
|
|
void HandleSessionRequestSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
2018-06-19 19:43:47 +00:00
|
|
|
void HandleSessionRequestReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
|
|
|
void HandleSessionRequestPaddingReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
|
|
|
void HandleSessionCreatedSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
2018-06-11 16:29:30 +00:00
|
|
|
void HandleSessionCreatedReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
2018-06-14 19:29:36 +00:00
|
|
|
void HandleSessionCreatedPaddingReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
|
|
|
void HandleSessionConfirmedSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
2018-07-09 19:56:23 +00:00
|
|
|
void HandleSessionConfirmedReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
2018-06-05 16:53:13 +00:00
|
|
|
|
2018-06-21 20:24:19 +00:00
|
|
|
// data
|
|
|
|
void ReceiveLength ();
|
|
|
|
void HandleReceivedLength (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
|
|
|
void Receive ();
|
|
|
|
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
2018-06-22 19:02:49 +00:00
|
|
|
void ProcessNextFrame (const uint8_t * frame, size_t len);
|
2018-06-21 20:24:19 +00:00
|
|
|
|
2018-06-25 16:28:07 +00:00
|
|
|
void SendNextFrame (const uint8_t * payload, size_t len);
|
|
|
|
void HandleNextFrameSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
2018-07-18 15:16:40 +00:00
|
|
|
void SendQueue ();
|
2018-08-02 16:42:39 +00:00
|
|
|
void SendRouterInfo ();
|
2018-08-02 19:31:15 +00:00
|
|
|
void SendTermination (NTCP2TerminationReason reason);
|
|
|
|
void SendTerminationAndTerminate (NTCP2TerminationReason reason);
|
2018-07-19 16:46:19 +00:00
|
|
|
void PostI2NPMessages (std::vector<std::shared_ptr<I2NPMessage> > msgs);
|
2018-06-25 16:28:07 +00:00
|
|
|
|
2018-06-05 16:53:13 +00:00
|
|
|
private:
|
|
|
|
|
2018-06-06 19:38:18 +00:00
|
|
|
NTCP2Server& m_Server;
|
|
|
|
boost::asio::ip::tcp::socket m_Socket;
|
2018-06-11 16:29:30 +00:00
|
|
|
bool m_IsEstablished, m_IsTerminated;
|
|
|
|
|
2018-07-03 20:26:02 +00:00
|
|
|
std::unique_ptr<NTCP2Establisher> m_Establisher;
|
2018-06-21 16:39:24 +00:00
|
|
|
// data phase
|
2018-06-21 20:24:19 +00:00
|
|
|
uint8_t m_Kab[33], m_Kba[32], m_Sipkeysab[33], m_Sipkeysba[32];
|
2018-07-09 19:56:23 +00:00
|
|
|
const uint8_t * m_SendKey, * m_ReceiveKey, * m_SendSipKey, * m_ReceiveSipKey;
|
2018-06-21 20:24:19 +00:00
|
|
|
uint16_t m_NextReceivedLen;
|
2018-06-25 16:28:07 +00:00
|
|
|
uint8_t * m_NextReceivedBuffer, * m_NextSendBuffer;
|
|
|
|
uint8_t m_ReceiveIV[8], m_SendIV[8];
|
|
|
|
uint64_t m_ReceiveSequenceNumber, m_SendSequenceNumber;
|
2018-07-17 19:17:05 +00:00
|
|
|
|
|
|
|
i2p::I2NPMessagesHandler m_Handler;
|
2018-07-18 15:16:40 +00:00
|
|
|
|
|
|
|
bool m_IsSending;
|
|
|
|
std::list<std::shared_ptr<I2NPMessage> > m_SendQueue;
|
2018-06-06 19:38:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class NTCP2Server
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2018-06-11 16:29:30 +00:00
|
|
|
NTCP2Server ();
|
|
|
|
~NTCP2Server ();
|
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
|
2018-07-18 16:58:29 +00:00
|
|
|
bool AddNTCP2Session (std::shared_ptr<NTCP2Session> session);
|
|
|
|
void RemoveNTCP2Session (std::shared_ptr<NTCP2Session> session);
|
|
|
|
std::shared_ptr<NTCP2Session> FindNTCP2Session (const i2p::data::IdentHash& ident);
|
|
|
|
|
2018-06-06 19:38:18 +00:00
|
|
|
boost::asio::io_service& GetService () { return m_Service; };
|
2018-06-11 16:29:30 +00:00
|
|
|
|
|
|
|
void Connect(const boost::asio::ip::address & address, uint16_t port, std::shared_ptr<NTCP2Session> conn);
|
|
|
|
|
2018-08-08 20:23:44 +00:00
|
|
|
NTCP2FrameBuffer * NewNTCP2FrameBuffer () { return m_NTCP2FrameBuffersPool.Acquire(); }
|
|
|
|
void DeleteNTCP2FrameBuffer (NTCP2FrameBuffer * buf) { return m_NTCP2FrameBuffersPool.Release(buf); }
|
|
|
|
|
2018-06-11 16:29:30 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
void Run ();
|
2018-07-23 19:30:51 +00:00
|
|
|
void HandleAccept (std::shared_ptr<NTCP2Session> conn, const boost::system::error_code& error);
|
|
|
|
void HandleAcceptV6 (std::shared_ptr<NTCP2Session> conn, const boost::system::error_code& error);
|
|
|
|
|
2018-08-03 17:10:32 +00:00
|
|
|
void HandleConnect (const boost::system::error_code& ecode, std::shared_ptr<NTCP2Session> conn, std::shared_ptr<boost::asio::deadline_timer> timer);
|
|
|
|
|
|
|
|
// timer
|
|
|
|
void ScheduleTermination ();
|
|
|
|
void HandleTerminationTimer (const boost::system::error_code& ecode);
|
2018-06-11 16:29:30 +00:00
|
|
|
|
2018-06-06 19:38:18 +00:00
|
|
|
private:
|
|
|
|
|
2018-06-11 16:29:30 +00:00
|
|
|
bool m_IsRunning;
|
|
|
|
std::thread * m_Thread;
|
2018-06-06 19:38:18 +00:00
|
|
|
boost::asio::io_service m_Service;
|
2018-06-11 16:29:30 +00:00
|
|
|
boost::asio::io_service::work m_Work;
|
2018-08-03 17:10:32 +00:00
|
|
|
boost::asio::deadline_timer m_TerminationTimer;
|
2018-07-23 19:30:51 +00:00
|
|
|
std::unique_ptr<boost::asio::ip::tcp::acceptor> m_NTCP2Acceptor, m_NTCP2V6Acceptor;
|
2018-07-18 16:58:29 +00:00
|
|
|
std::map<i2p::data::IdentHash, std::shared_ptr<NTCP2Session> > m_NTCP2Sessions;
|
2018-07-23 19:30:51 +00:00
|
|
|
std::list<std::shared_ptr<NTCP2Session> > m_PendingIncomingSessions;
|
2018-07-18 16:58:29 +00:00
|
|
|
|
2018-08-08 20:23:44 +00:00
|
|
|
i2p::util::MemoryPool<NTCP2FrameBuffer> m_NTCP2FrameBuffersPool;
|
|
|
|
|
2018-07-18 16:58:29 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
// for HTTP/I2PControl
|
|
|
|
const decltype(m_NTCP2Sessions)& GetNTCP2Sessions () const { return m_NTCP2Sessions; };
|
2018-06-05 16:53:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|