mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
6.3 KiB
166 lines
6.3 KiB
10 years ago
|
#ifndef SSU_SESSION_H__
|
||
|
#define SSU_SESSION_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
|
#include <set>
|
||
10 years ago
|
#include <memory>
|
||
9 years ago
|
#include "Crypto.h"
|
||
10 years ago
|
#include "I2NPProtocol.h"
|
||
|
#include "TransportSession.h"
|
||
|
#include "SSUData.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace transport
|
||
|
{
|
||
9 years ago
|
const uint8_t SSU_HEADER_EXTENDED_OPTIONS_INCLUDED = 0x04;
|
||
10 years ago
|
struct SSUHeader
|
||
|
{
|
||
|
uint8_t mac[16];
|
||
|
uint8_t iv[16];
|
||
|
uint8_t flag;
|
||
9 years ago
|
uint8_t time[4];
|
||
10 years ago
|
|
||
|
uint8_t GetPayloadType () const { return flag >> 4; };
|
||
9 years ago
|
bool IsExtendedOptions () const { return flag & SSU_HEADER_EXTENDED_OPTIONS_INCLUDED; };
|
||
10 years ago
|
};
|
||
|
|
||
|
const int SSU_CONNECT_TIMEOUT = 5; // 5 seconds
|
||
|
const int SSU_TERMINATION_TIMEOUT = 330; // 5.5 minutes
|
||
8 years ago
|
const int SSU_CLOCK_SKEW = 60; // in seconds
|
||
10 years ago
|
|
||
|
// payload types (4 bits)
|
||
|
const uint8_t PAYLOAD_TYPE_SESSION_REQUEST = 0;
|
||
|
const uint8_t PAYLOAD_TYPE_SESSION_CREATED = 1;
|
||
|
const uint8_t PAYLOAD_TYPE_SESSION_CONFIRMED = 2;
|
||
|
const uint8_t PAYLOAD_TYPE_RELAY_REQUEST = 3;
|
||
|
const uint8_t PAYLOAD_TYPE_RELAY_RESPONSE = 4;
|
||
|
const uint8_t PAYLOAD_TYPE_RELAY_INTRO = 5;
|
||
|
const uint8_t PAYLOAD_TYPE_DATA = 6;
|
||
|
const uint8_t PAYLOAD_TYPE_PEER_TEST = 7;
|
||
|
const uint8_t PAYLOAD_TYPE_SESSION_DESTROYED = 8;
|
||
|
|
||
9 years ago
|
// extended options
|
||
|
const uint16_t EXTENDED_OPTIONS_FLAG_REQUEST_RELAY_TAG = 0x0001;
|
||
|
|
||
10 years ago
|
enum SessionState
|
||
|
{
|
||
|
eSessionStateUnknown,
|
||
|
eSessionStateIntroduced,
|
||
|
eSessionStateEstablished,
|
||
10 years ago
|
eSessionStateClosed,
|
||
10 years ago
|
eSessionStateFailed
|
||
|
};
|
||
|
|
||
10 years ago
|
enum PeerTestParticipant
|
||
|
{
|
||
|
ePeerTestParticipantUnknown = 0,
|
||
|
ePeerTestParticipantAlice1,
|
||
|
ePeerTestParticipantAlice2,
|
||
|
ePeerTestParticipantBob,
|
||
|
ePeerTestParticipantCharlie
|
||
|
};
|
||
|
|
||
10 years ago
|
class SSUServer;
|
||
10 years ago
|
class SSUSession: public TransportSession, public std::enable_shared_from_this<SSUSession>
|
||
10 years ago
|
{
|
||
|
public:
|
||
|
|
||
|
SSUSession (SSUServer& server, boost::asio::ip::udp::endpoint& remoteEndpoint,
|
||
10 years ago
|
std::shared_ptr<const i2p::data::RouterInfo> router = nullptr, bool peerTest = false);
|
||
10 years ago
|
void ProcessNextMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
|
||
|
~SSUSession ();
|
||
|
|
||
|
void Connect ();
|
||
10 years ago
|
void WaitForConnect ();
|
||
9 years ago
|
void Introduce (const i2p::data::RouterInfo::Introducer& introducer,
|
||
|
std::shared_ptr<const i2p::data::RouterInfo> to); // Alice to Charlie
|
||
10 years ago
|
void WaitForIntroduction ();
|
||
|
void Close ();
|
||
10 years ago
|
void Done ();
|
||
8 years ago
|
void Failed ();
|
||
10 years ago
|
boost::asio::ip::udp::endpoint& GetRemoteEndpoint () { return m_RemoteEndpoint; };
|
||
|
bool IsV6 () const { return m_RemoteEndpoint.address ().is_v6 (); };
|
||
10 years ago
|
void SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs);
|
||
10 years ago
|
void SendPeerTest (); // Alice
|
||
|
|
||
|
SessionState GetState () const { return m_State; };
|
||
|
size_t GetNumSentBytes () const { return m_NumSentBytes; };
|
||
|
size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; };
|
||
|
|
||
|
void SendKeepAlive ();
|
||
|
uint32_t GetRelayTag () const { return m_RelayTag; };
|
||
9 years ago
|
const i2p::data::RouterInfo::IntroKey& GetIntroKey () const { return m_IntroKey; };
|
||
10 years ago
|
uint32_t GetCreationTime () const { return m_CreationTime; };
|
||
|
|
||
10 years ago
|
void FlushData ();
|
||
|
|
||
10 years ago
|
private:
|
||
|
|
||
10 years ago
|
boost::asio::io_service& GetService ();
|
||
10 years ago
|
void CreateAESandMacKey (const uint8_t * pubKey);
|
||
9 years ago
|
size_t GetSSUHeaderSize (const uint8_t * buf) const;
|
||
10 years ago
|
void PostI2NPMessages (std::vector<std::shared_ptr<I2NPMessage> > msgs);
|
||
10 years ago
|
void ProcessMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint); // call for established session
|
||
9 years ago
|
void ProcessSessionRequest (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
|
||
10 years ago
|
void SendSessionRequest ();
|
||
9 years ago
|
void SendRelayRequest (const i2p::data::RouterInfo::Introducer& introducer, uint32_t nonce);
|
||
10 years ago
|
void ProcessSessionCreated (uint8_t * buf, size_t len);
|
||
9 years ago
|
void SendSessionCreated (const uint8_t * x, bool sendRelayTag = true);
|
||
9 years ago
|
void ProcessSessionConfirmed (const uint8_t * buf, size_t len);
|
||
10 years ago
|
void SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress, size_t ourAddressLen);
|
||
9 years ago
|
void ProcessRelayRequest (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& from);
|
||
10 years ago
|
void SendRelayResponse (uint32_t nonce, const boost::asio::ip::udp::endpoint& from,
|
||
|
const uint8_t * introKey, const boost::asio::ip::udp::endpoint& to);
|
||
9 years ago
|
void SendRelayIntro (std::shared_ptr<SSUSession> session, const boost::asio::ip::udp::endpoint& from);
|
||
9 years ago
|
void ProcessRelayResponse (const uint8_t * buf, size_t len);
|
||
|
void ProcessRelayIntro (const uint8_t * buf, size_t len);
|
||
10 years ago
|
void Established ();
|
||
|
void ScheduleConnectTimer ();
|
||
|
void HandleConnectTimer (const boost::system::error_code& ecode);
|
||
10 years ago
|
void ProcessPeerTest (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
|
||
9 years ago
|
void SendPeerTest (uint32_t nonce, const boost::asio::ip::address& address, uint16_t port, const uint8_t * introKey, bool toAddress = true, bool sendAddress = true);
|
||
10 years ago
|
void ProcessData (uint8_t * buf, size_t len);
|
||
8 years ago
|
void SendSessionDestroyed ();
|
||
10 years ago
|
void Send (uint8_t type, const uint8_t * payload, size_t len); // with session key
|
||
|
void Send (const uint8_t * buf, size_t size);
|
||
|
|
||
9 years ago
|
void FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len, const i2p::crypto::AESKey& aesKey,
|
||
|
const uint8_t * iv, const i2p::crypto::MACKey& macKey, uint8_t flag = 0);
|
||
10 years ago
|
void FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len); // with session key
|
||
9 years ago
|
void Decrypt (uint8_t * buf, size_t len, const i2p::crypto::AESKey& aesKey);
|
||
10 years ago
|
void DecryptSessionKey (uint8_t * buf, size_t len);
|
||
9 years ago
|
bool Validate (uint8_t * buf, size_t len, const i2p::crypto::MACKey& macKey);
|
||
10 years ago
|
|
||
8 years ago
|
void Reset ();
|
||
|
|
||
10 years ago
|
private:
|
||
|
|
||
|
friend class SSUData; // TODO: change in later
|
||
|
SSUServer& m_Server;
|
||
|
boost::asio::ip::udp::endpoint m_RemoteEndpoint;
|
||
8 years ago
|
boost::asio::deadline_timer m_ConnectTimer;
|
||
9 years ago
|
bool m_IsPeerTest;
|
||
10 years ago
|
SessionState m_State;
|
||
|
bool m_IsSessionKey;
|
||
8 years ago
|
uint32_t m_RelayTag; // received from peer
|
||
|
uint32_t m_SentRelayTag; // sent by us
|
||
10 years ago
|
i2p::crypto::CBCEncryption m_SessionKeyEncryption;
|
||
|
i2p::crypto::CBCDecryption m_SessionKeyDecryption;
|
||
10 years ago
|
i2p::crypto::AESKey m_SessionKey;
|
||
|
i2p::crypto::MACKey m_MacKey;
|
||
9 years ago
|
i2p::data::RouterInfo::IntroKey m_IntroKey;
|
||
10 years ago
|
uint32_t m_CreationTime; // seconds since epoch
|
||
10 years ago
|
SSUData m_Data;
|
||
10 years ago
|
bool m_IsDataReceived;
|
||
9 years ago
|
std::unique_ptr<SignedData> m_SignedData; // we need it for SessionConfirmed only
|
||
9 years ago
|
std::map<uint32_t, std::shared_ptr<const i2p::data::RouterInfo> > m_RelayRequests; // nonce->Charlie
|
||
10 years ago
|
};
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|