2022-02-04 23:01:18 +03:00
|
|
|
/*
|
2022-02-18 09:20:06 +03:00
|
|
|
* Copyright (c) 2022, The PurpleI2P Project
|
2022-02-04 23:01:18 +03:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2022-02-12 03:21:04 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <openssl/rand.h>
|
2022-03-01 05:46:00 +03:00
|
|
|
#include "Log.h"
|
2022-02-28 04:15:14 +03:00
|
|
|
#include "RouterContext.h"
|
2022-02-05 23:58:39 +03:00
|
|
|
#include "Transports.h"
|
2022-03-14 04:34:11 +03:00
|
|
|
#include "Config.h"
|
2022-03-22 04:06:14 +03:00
|
|
|
#include "Gzip.h"
|
2022-03-23 21:06:55 +03:00
|
|
|
#include "NetDb.hpp"
|
2022-02-04 23:01:18 +03:00
|
|
|
#include "SSU2.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace transport
|
|
|
|
{
|
2022-03-05 05:51:40 +03:00
|
|
|
static uint64_t CreateHeaderMask (const uint8_t * kh, const uint8_t * nonce)
|
|
|
|
{
|
|
|
|
uint64_t data = 0;
|
|
|
|
i2p::crypto::ChaCha20 ((uint8_t *)&data, 8, kh, nonce, (uint8_t *)&data);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2022-02-28 04:15:14 +03:00
|
|
|
SSU2Session::SSU2Session (SSU2Server& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter,
|
2022-02-04 23:01:18 +03:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo::Address> addr, bool peerTest):
|
2022-03-19 03:21:31 +03:00
|
|
|
TransportSession (in_RemoteRouter, SSU2_CONNECT_TIMEOUT),
|
2022-03-25 22:34:43 +03:00
|
|
|
m_Server (server), m_Address (addr), m_DestConnID (0), m_SourceConnID (0),
|
2022-04-12 18:42:51 +03:00
|
|
|
m_State (eSSU2SessionStateUnknown), m_SendPacketNum (0), m_ReceivePacketNum (0),
|
2022-04-21 22:47:36 +03:00
|
|
|
m_IsDataReceived (false), m_WindowSize (SSU2_MAX_WINDOW_SIZE), m_RelayTag (0)
|
2022-02-04 23:01:18 +03:00
|
|
|
{
|
2022-02-05 23:58:39 +03:00
|
|
|
m_NoiseState.reset (new i2p::crypto::NoiseSymmetricState);
|
2022-03-09 05:33:21 +03:00
|
|
|
if (in_RemoteRouter && m_Address)
|
2022-02-05 23:58:39 +03:00
|
|
|
{
|
|
|
|
// outgoing
|
2022-03-09 05:33:21 +03:00
|
|
|
InitNoiseXKState1 (*m_NoiseState, m_Address->s);
|
|
|
|
m_RemoteEndpoint = boost::asio::ip::udp::endpoint (m_Address->host, m_Address->port);
|
2022-03-20 23:53:32 +03:00
|
|
|
RAND_bytes ((uint8_t *)&m_DestConnID, 8);
|
|
|
|
RAND_bytes ((uint8_t *)&m_SourceConnID, 8);
|
2022-02-05 23:58:39 +03:00
|
|
|
}
|
2022-03-06 02:39:27 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// incoming
|
|
|
|
InitNoiseXKState1 (*m_NoiseState, i2p::context.GetSSU2StaticPublicKey ());
|
|
|
|
}
|
2022-02-04 23:01:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SSU2Session::~SSU2Session ()
|
|
|
|
{
|
|
|
|
}
|
2022-02-10 22:03:09 +03:00
|
|
|
|
2022-03-17 04:11:48 +03:00
|
|
|
void SSU2Session::Connect ()
|
|
|
|
{
|
2022-03-24 02:13:44 +03:00
|
|
|
auto token = m_Server.FindOutgoingToken (m_RemoteEndpoint);
|
|
|
|
if (token)
|
|
|
|
SendSessionRequest (token);
|
|
|
|
else
|
|
|
|
SendTokenRequest ();
|
2022-03-17 04:11:48 +03:00
|
|
|
}
|
2022-03-24 04:48:41 +03:00
|
|
|
|
2022-04-28 01:52:44 +03:00
|
|
|
bool SSU2Session::Introduce (std::shared_ptr<SSU2Session> session, uint32_t relayTag)
|
|
|
|
{
|
|
|
|
// we are Alice
|
|
|
|
if (!session || !relayTag) return false;
|
|
|
|
// find local adddress to introduce
|
|
|
|
std::shared_ptr<const i2p::data::RouterInfo::Address> localAddress;
|
|
|
|
if (session->m_Address->IsV4 ())
|
|
|
|
localAddress = i2p::context.GetRouterInfo ().GetSSU2V4Address ();
|
|
|
|
else if (session->m_Address->IsV6 ())
|
|
|
|
localAddress = i2p::context.GetRouterInfo ().GetSSU2V6Address ();
|
|
|
|
if (localAddress) return false;
|
|
|
|
// create nonce
|
|
|
|
uint32_t nonce;
|
|
|
|
RAND_bytes ((uint8_t *)&nonce, 4);
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
// payload
|
|
|
|
uint8_t payload[SSU2_MAX_PAYLOAD_SIZE];
|
|
|
|
size_t payloadSize = 0;
|
|
|
|
payload[0] = eSSU2BlkRelayRequest;
|
|
|
|
payload[3] = 0; // flag
|
|
|
|
htobe32buf (payload + 4, nonce);
|
|
|
|
htobe32buf (payload + 8, relayTag);
|
|
|
|
htobe32buf (payload + 12, ts);
|
|
|
|
payload[16] = 2; // ver
|
|
|
|
size_t asz = CreateEndpoint (payload + 18, SSU2_MAX_PAYLOAD_SIZE - 18, boost::asio::ip::udp::endpoint (localAddress->host, localAddress->port));
|
|
|
|
if (!asz) return false;
|
|
|
|
payload[17] = asz;
|
|
|
|
payloadSize += asz + 17;
|
|
|
|
SignedData s;
|
|
|
|
s.Insert ((const uint8_t *)"RelayRequestData", 16); // prologue
|
|
|
|
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
|
|
|
s.Insert (session->GetRemoteIdentity ()->GetIdentHash (), 32); // chash
|
|
|
|
s.Insert (payload + 4, 14 + asz); // nonce, relay tag, timestamp, ver, asz and Alice's endpoint
|
|
|
|
s.Sign (i2p::context.GetPrivateKeys (), payload + 17 + asz);
|
|
|
|
payloadSize += i2p::context.GetIdentity ()->GetSignatureLen ();
|
|
|
|
htobe16buf (payload + 1, payloadSize - 3); // size
|
|
|
|
payloadSize += CreatePaddingBlock (payload + payloadSize, SSU2_MAX_PAYLOAD_SIZE - payloadSize);
|
|
|
|
// send
|
|
|
|
m_RelaySessions.emplace (nonce, std::make_pair (session, ts));
|
|
|
|
SendData (payload, payloadSize);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-27 23:39:58 +03:00
|
|
|
void SSU2Session::Terminate ()
|
|
|
|
{
|
|
|
|
if (m_State != eSSU2SessionStateTerminated)
|
|
|
|
{
|
|
|
|
m_State = eSSU2SessionStateTerminated;
|
|
|
|
transports.PeerDisconnected (shared_from_this ());
|
|
|
|
m_Server.RemoveSession (m_SourceConnID);
|
2022-04-21 22:47:36 +03:00
|
|
|
if (m_RelayTag)
|
|
|
|
m_Server.RemoveRelay (m_RelayTag);
|
2022-03-31 01:04:12 +03:00
|
|
|
m_SendQueue.clear ();
|
2022-03-27 23:39:58 +03:00
|
|
|
LogPrint (eLogDebug, "SSU2: Session terminated");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Session::TerminateByTimeout ()
|
|
|
|
{
|
|
|
|
SendTermination ();
|
|
|
|
m_Server.GetService ().post (std::bind (&SSU2Session::Terminate, shared_from_this ()));
|
|
|
|
}
|
|
|
|
|
2022-03-26 23:35:07 +03:00
|
|
|
void SSU2Session::Established ()
|
|
|
|
{
|
|
|
|
m_State = eSSU2SessionStateEstablished;
|
|
|
|
m_EphemeralKeys = nullptr;
|
|
|
|
m_NoiseState.reset (nullptr);
|
2022-04-10 02:56:57 +03:00
|
|
|
m_SessionConfirmedFragment1.reset (nullptr);
|
2022-03-26 23:35:07 +03:00
|
|
|
SetTerminationTimeout (SSU2_TERMINATION_TIMEOUT);
|
2022-03-31 01:04:12 +03:00
|
|
|
transports.PeerConnected (shared_from_this ());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Session::Done ()
|
|
|
|
{
|
|
|
|
m_Server.GetService ().post (std::bind (&SSU2Session::Terminate, shared_from_this ()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Session::SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs)
|
|
|
|
{
|
|
|
|
m_Server.GetService ().post (std::bind (&SSU2Session::PostI2NPMessages, shared_from_this (), msgs));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Session::PostI2NPMessages (std::vector<std::shared_ptr<I2NPMessage> > msgs)
|
|
|
|
{
|
|
|
|
for (auto it: msgs)
|
|
|
|
m_SendQueue.push_back (it);
|
|
|
|
SendQueue ();
|
2022-03-31 22:35:55 +03:00
|
|
|
}
|
|
|
|
|
2022-04-18 22:47:35 +03:00
|
|
|
bool SSU2Session::SendQueue ()
|
2022-03-31 01:04:12 +03:00
|
|
|
{
|
2022-04-18 20:14:09 +03:00
|
|
|
if (!m_SendQueue.empty () && m_SentPackets.size () <= m_WindowSize)
|
2022-03-31 01:04:12 +03:00
|
|
|
{
|
2022-04-16 22:42:11 +03:00
|
|
|
auto nextResend = i2p::util::GetSecondsSinceEpoch () + SSU2_RESEND_INTERVAL;
|
|
|
|
auto packet = std::make_shared<SentPacket>();
|
|
|
|
packet->payloadSize += CreateAckBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize);
|
2022-04-18 20:14:09 +03:00
|
|
|
while (!m_SendQueue.empty () && m_SentPackets.size () <= m_WindowSize)
|
2022-03-31 01:04:12 +03:00
|
|
|
{
|
|
|
|
auto msg = m_SendQueue.front ();
|
|
|
|
size_t len = msg->GetNTCP2Length ();
|
2022-04-16 22:42:11 +03:00
|
|
|
if (len + 3 < SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize)
|
2022-03-31 01:04:12 +03:00
|
|
|
{
|
|
|
|
m_SendQueue.pop_front ();
|
2022-04-16 22:42:11 +03:00
|
|
|
packet->payloadSize += CreateI2NPBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize, std::move (msg));
|
2022-03-31 01:04:12 +03:00
|
|
|
}
|
|
|
|
else if (len > SSU2_MAX_PAYLOAD_SIZE - 32) // message too long
|
2022-04-09 18:42:34 +03:00
|
|
|
{
|
|
|
|
m_SendQueue.pop_front ();
|
|
|
|
SendFragmentedMessage (msg);
|
|
|
|
}
|
2022-03-31 01:04:12 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// send right a way
|
2022-04-16 22:42:11 +03:00
|
|
|
if (packet->payloadSize + 16 < SSU2_MAX_PAYLOAD_SIZE)
|
|
|
|
packet->payloadSize += CreatePaddingBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize);
|
|
|
|
uint32_t packetNum = SendData (packet->payload, packet->payloadSize);
|
|
|
|
packet->nextResendTime = nextResend;
|
|
|
|
m_SentPackets.emplace (packetNum, packet);
|
|
|
|
packet = std::make_shared<SentPacket>();
|
|
|
|
packet->payloadSize += CreateAckBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize);
|
2022-03-31 01:04:12 +03:00
|
|
|
}
|
|
|
|
};
|
2022-04-16 22:42:11 +03:00
|
|
|
if (packet->payloadSize)
|
2022-03-31 01:04:12 +03:00
|
|
|
{
|
2022-04-16 22:42:11 +03:00
|
|
|
if (packet->payloadSize + 16 < SSU2_MAX_PAYLOAD_SIZE)
|
|
|
|
packet->payloadSize += CreatePaddingBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize);
|
|
|
|
uint32_t packetNum = SendData (packet->payload, packet->payloadSize);
|
|
|
|
packet->nextResendTime = nextResend;
|
|
|
|
m_SentPackets.emplace (packetNum, packet);
|
2022-03-31 01:04:12 +03:00
|
|
|
}
|
2022-04-18 22:47:35 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2022-03-26 23:35:07 +03:00
|
|
|
}
|
2022-03-31 22:35:55 +03:00
|
|
|
|
2022-04-09 18:42:34 +03:00
|
|
|
void SSU2Session::SendFragmentedMessage (std::shared_ptr<I2NPMessage> msg)
|
|
|
|
{
|
|
|
|
uint32_t msgID;
|
|
|
|
memcpy (&msgID, msg->GetHeader () + I2NP_HEADER_MSGID_OFFSET, 4);
|
2022-04-16 22:42:11 +03:00
|
|
|
auto nextResend = i2p::util::GetSecondsSinceEpoch () + SSU2_RESEND_INTERVAL;
|
|
|
|
auto packet = std::make_shared<SentPacket>();
|
|
|
|
packet->payloadSize += CreateAckBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize);
|
|
|
|
auto size = CreateFirstFragmentBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - 32 - packet->payloadSize, msg);
|
2022-04-09 18:42:34 +03:00
|
|
|
if (!size) return;
|
2022-04-16 22:42:11 +03:00
|
|
|
packet->payloadSize += size;
|
|
|
|
packet->payloadSize += CreatePaddingBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize);
|
|
|
|
uint32_t firstPacketNum = SendData (packet->payload, packet->payloadSize);
|
|
|
|
packet->nextResendTime = nextResend;
|
|
|
|
m_SentPackets.emplace (firstPacketNum, packet);
|
2022-04-09 18:42:34 +03:00
|
|
|
uint8_t fragmentNum = 0;
|
|
|
|
while (msg->offset < msg->len)
|
|
|
|
{
|
2022-04-16 22:42:11 +03:00
|
|
|
packet = std::make_shared<SentPacket>();
|
|
|
|
packet->payloadSize += CreateFollowOnFragmentBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize - 16, msg, fragmentNum, msgID);
|
|
|
|
packet->payloadSize += CreatePaddingBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize);
|
|
|
|
uint32_t followonPacketNum = SendData (packet->payload, packet->payloadSize);
|
|
|
|
packet->nextResendTime = nextResend;
|
|
|
|
m_SentPackets.emplace (followonPacketNum, packet);
|
2022-04-09 18:42:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-31 22:35:55 +03:00
|
|
|
void SSU2Session::Resend (uint64_t ts)
|
|
|
|
{
|
2022-04-18 20:14:09 +03:00
|
|
|
if (m_SentPackets.empty ()) return;
|
2022-04-16 22:42:11 +03:00
|
|
|
std::map<uint32_t, std::shared_ptr<SentPacket> > resentPackets;
|
2022-03-31 22:35:55 +03:00
|
|
|
for (auto it = m_SentPackets.begin (); it != m_SentPackets.end (); )
|
|
|
|
if (ts > it->second->nextResendTime)
|
|
|
|
{
|
|
|
|
if (it->second->numResends > SSU2_MAX_NUM_RESENDS)
|
|
|
|
it = m_SentPackets.erase (it);
|
|
|
|
else
|
|
|
|
{
|
2022-04-16 22:42:11 +03:00
|
|
|
uint32_t packetNum = SendData (it->second->payload, it->second->payloadSize);
|
2022-03-31 22:35:55 +03:00
|
|
|
it->second->numResends++;
|
|
|
|
it->second->nextResendTime = ts + it->second->numResends*SSU2_RESEND_INTERVAL;
|
|
|
|
m_LastActivityTimestamp = ts;
|
2022-04-16 22:42:11 +03:00
|
|
|
resentPackets.emplace (packetNum, it->second);
|
|
|
|
it = m_SentPackets.erase (it);
|
2022-03-31 22:35:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
it++;
|
2022-04-16 22:42:11 +03:00
|
|
|
if (!resentPackets.empty ())
|
|
|
|
{
|
|
|
|
#if (__cplusplus >= 201703L) // C++ 17 or higher
|
|
|
|
m_SentPackets.merge (resentPackets);
|
|
|
|
#else
|
|
|
|
m_SentPackets.insert (resentPackets.begin (), resentPackets.end ());
|
|
|
|
#endif
|
|
|
|
}
|
2022-04-18 20:14:09 +03:00
|
|
|
SendQueue ();
|
2022-03-31 22:35:55 +03:00
|
|
|
}
|
2022-03-26 23:35:07 +03:00
|
|
|
|
2022-03-24 04:48:41 +03:00
|
|
|
void SSU2Session::ProcessFirstIncomingMessage (uint64_t connID, uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
// we are Bob
|
|
|
|
m_SourceConnID = connID;
|
|
|
|
Header header;
|
|
|
|
header.h.connID = connID;
|
|
|
|
memcpy (header.buf + 8, buf + 8, 8);
|
|
|
|
header.ll[1] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), buf + (len - 12));
|
|
|
|
switch (header.h.type)
|
|
|
|
{
|
|
|
|
case eSSU2SessionRequest:
|
|
|
|
ProcessSessionRequest (header, buf, len);
|
|
|
|
break;
|
|
|
|
case eSSU2TokenRequest:
|
|
|
|
ProcessTokenRequest (header, buf, len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type);
|
|
|
|
}
|
|
|
|
}
|
2022-03-17 04:11:48 +03:00
|
|
|
|
2022-03-20 22:10:18 +03:00
|
|
|
void SSU2Session::SendSessionRequest (uint64_t token)
|
2022-02-10 22:03:09 +03:00
|
|
|
{
|
2022-03-09 05:33:21 +03:00
|
|
|
// we are Alice
|
2022-02-10 22:03:09 +03:00
|
|
|
m_EphemeralKeys = i2p::transport::transports.GetNextX25519KeysPair ();
|
2022-03-05 05:51:40 +03:00
|
|
|
|
2022-02-12 03:21:04 +03:00
|
|
|
Header header;
|
2022-03-16 20:13:31 +03:00
|
|
|
uint8_t headerX[48], payload[40];
|
2022-02-10 22:03:09 +03:00
|
|
|
// fill packet
|
2022-03-01 05:46:00 +03:00
|
|
|
header.h.connID = m_DestConnID; // dest id
|
2022-03-22 04:06:14 +03:00
|
|
|
header.h.packetNum = 0;
|
2022-03-09 05:33:21 +03:00
|
|
|
header.h.type = eSSU2SessionRequest;
|
|
|
|
header.h.flags[0] = 2; // ver
|
|
|
|
header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID
|
|
|
|
header.h.flags[2] = 0; // flag
|
2022-03-01 05:46:00 +03:00
|
|
|
memcpy (headerX, &m_SourceConnID, 8); // source id
|
2022-03-20 22:10:18 +03:00
|
|
|
memcpy (headerX + 8, &token, 8); // token
|
2022-03-01 05:46:00 +03:00
|
|
|
memcpy (headerX + 16, m_EphemeralKeys->GetPublicKey (), 32); // X
|
2022-03-16 03:49:41 +03:00
|
|
|
// payload
|
|
|
|
payload[0] = eSSU2BlkDateTime;
|
|
|
|
htobe16buf (payload + 1, 4);
|
|
|
|
htobe32buf (payload + 3, i2p::util::GetSecondsSinceEpoch ());
|
|
|
|
size_t payloadSize = 7;
|
2022-03-27 16:26:45 +03:00
|
|
|
payloadSize += CreatePaddingBlock (payload + payloadSize, 40 - payloadSize, 1);
|
2022-03-05 05:51:40 +03:00
|
|
|
// KDF for session request
|
2022-03-08 02:20:06 +03:00
|
|
|
m_NoiseState->MixHash ({ {header.buf, 16}, {headerX, 16} }); // h = SHA256(h || header)
|
2022-03-05 05:51:40 +03:00
|
|
|
m_NoiseState->MixHash (m_EphemeralKeys->GetPublicKey (), 32); // h = SHA256(h || aepk);
|
|
|
|
uint8_t sharedSecret[32];
|
|
|
|
m_EphemeralKeys->Agree (m_Address->s, sharedSecret);
|
|
|
|
m_NoiseState->MixKey (sharedSecret);
|
2022-02-10 22:03:09 +03:00
|
|
|
// encrypt
|
2022-03-06 02:39:27 +03:00
|
|
|
const uint8_t nonce[12] = {0};
|
|
|
|
i2p::crypto::AEADChaCha20Poly1305 (payload, payloadSize, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, payload, payloadSize + 16, true);
|
2022-03-09 05:33:21 +03:00
|
|
|
payloadSize += 16;
|
2022-03-05 05:51:40 +03:00
|
|
|
header.ll[0] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 24));
|
|
|
|
header.ll[1] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 12));
|
|
|
|
i2p::crypto::ChaCha20 (headerX, 48, m_Address->i, nonce, headerX);
|
2022-03-21 20:21:08 +03:00
|
|
|
m_NoiseState->MixHash (payload, payloadSize); // h = SHA256(h || encrypted payload from Session Request) for SessionCreated
|
2022-03-05 05:51:40 +03:00
|
|
|
// send
|
2022-03-27 23:39:58 +03:00
|
|
|
m_Server.AddPendingOutgoingSession (shared_from_this ());
|
2022-03-09 05:33:21 +03:00
|
|
|
m_Server.Send (header.buf, 16, headerX, 48, payload, payloadSize, m_RemoteEndpoint);
|
2022-02-10 22:03:09 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 04:48:41 +03:00
|
|
|
void SSU2Session::ProcessSessionRequest (Header& header, uint8_t * buf, size_t len)
|
2022-03-06 02:39:27 +03:00
|
|
|
{
|
|
|
|
// we are Bob
|
|
|
|
const uint8_t nonce[12] = {0};
|
|
|
|
uint8_t headerX[48];
|
|
|
|
i2p::crypto::ChaCha20 (buf + 16, 48, i2p::context.GetSSU2IntroKey (), nonce, headerX);
|
|
|
|
memcpy (&m_DestConnID, headerX, 8);
|
2022-04-01 04:07:51 +03:00
|
|
|
uint64_t token;
|
|
|
|
memcpy (&token, headerX + 8, 8);
|
|
|
|
if (!token || token != m_Server.GetIncomingToken (m_RemoteEndpoint))
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "SSU2: SessionRequest token mismatch. Retry");
|
|
|
|
SendRetry ();
|
|
|
|
return;
|
|
|
|
}
|
2022-03-06 02:39:27 +03:00
|
|
|
// KDF for session request
|
2022-03-08 02:20:06 +03:00
|
|
|
m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header)
|
2022-03-06 02:39:27 +03:00
|
|
|
m_NoiseState->MixHash (headerX + 16, 32); // h = SHA256(h || aepk);
|
|
|
|
uint8_t sharedSecret[32];
|
|
|
|
i2p::context.GetSSU2StaticKeys ().Agree (headerX + 16, sharedSecret);
|
|
|
|
m_NoiseState->MixKey (sharedSecret);
|
|
|
|
// decrypt
|
2022-03-18 22:32:32 +03:00
|
|
|
uint8_t * payload = buf + 64;
|
|
|
|
std::vector<uint8_t> decryptedPayload(len - 80);
|
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 80, m_NoiseState->m_H, 32,
|
|
|
|
m_NoiseState->m_CK + 32, nonce, decryptedPayload.data (), decryptedPayload.size (), false))
|
2022-03-06 02:39:27 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: SessionRequest AEAD verification failed ");
|
|
|
|
return;
|
|
|
|
}
|
2022-03-21 20:21:08 +03:00
|
|
|
m_NoiseState->MixHash (payload, len - 64); // h = SHA256(h || encrypted payload from Session Request) for SessionCreated
|
2022-03-16 03:49:41 +03:00
|
|
|
// payload
|
2022-03-18 22:32:32 +03:00
|
|
|
HandlePayload (decryptedPayload.data (), decryptedPayload.size ());
|
2022-03-06 02:39:27 +03:00
|
|
|
|
2022-03-27 23:39:58 +03:00
|
|
|
m_Server.AddSession (shared_from_this ());
|
2022-03-09 05:33:21 +03:00
|
|
|
SendSessionCreated (headerX + 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Session::SendSessionCreated (const uint8_t * X)
|
|
|
|
{
|
|
|
|
// we are Bob
|
|
|
|
m_EphemeralKeys = i2p::transport::transports.GetNextX25519KeysPair ();
|
2022-03-22 04:06:14 +03:00
|
|
|
uint8_t kh2[32];
|
|
|
|
i2p::crypto::HKDF (m_NoiseState->m_CK, nullptr, 0, "SessCreateHeader", kh2, 32); // k_header_2 = HKDF(chainKey, ZEROLEN, "SessCreateHeader", 32)
|
|
|
|
|
2022-03-09 05:33:21 +03:00
|
|
|
// fill packet
|
|
|
|
Header header;
|
2022-03-16 20:13:31 +03:00
|
|
|
uint8_t headerX[48], payload[64];
|
2022-03-09 05:33:21 +03:00
|
|
|
header.h.connID = m_DestConnID; // dest id
|
2022-03-22 04:06:14 +03:00
|
|
|
header.h.packetNum = 0;
|
2022-03-09 05:33:21 +03:00
|
|
|
header.h.type = eSSU2SessionCreated;
|
|
|
|
header.h.flags[0] = 2; // ver
|
|
|
|
header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID
|
|
|
|
header.h.flags[2] = 0; // flag
|
|
|
|
memcpy (headerX, &m_SourceConnID, 8); // source id
|
|
|
|
RAND_bytes (headerX + 8, 8); // token
|
|
|
|
memcpy (headerX + 16, m_EphemeralKeys->GetPublicKey (), 32); // Y
|
2022-03-16 20:13:31 +03:00
|
|
|
// payload
|
|
|
|
payload[0] = eSSU2BlkDateTime;
|
|
|
|
htobe16buf (payload + 1, 4);
|
|
|
|
htobe32buf (payload + 3, i2p::util::GetSecondsSinceEpoch ());
|
|
|
|
size_t payloadSize = 7;
|
2022-04-03 20:43:33 +03:00
|
|
|
payloadSize += CreateAddressBlock (m_RemoteEndpoint, payload + payloadSize, 64 - payloadSize);
|
2022-04-21 22:47:36 +03:00
|
|
|
if (m_RelayTag)
|
|
|
|
{
|
|
|
|
payload[payloadSize] = eSSU2BlkRelayTag;
|
|
|
|
htobe16buf (payload + payloadSize + 1, 4);
|
|
|
|
htobe32buf (payload + payloadSize + 3, m_RelayTag);
|
|
|
|
payloadSize += 7;
|
|
|
|
}
|
2022-03-26 23:35:07 +03:00
|
|
|
payloadSize += CreatePaddingBlock (payload + payloadSize, 64 - payloadSize);
|
2022-03-09 05:33:21 +03:00
|
|
|
// KDF for SessionCreated
|
|
|
|
m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header)
|
|
|
|
m_NoiseState->MixHash (headerX + 16, 32); // h = SHA256(h || bepk);
|
|
|
|
uint8_t sharedSecret[32];
|
|
|
|
m_EphemeralKeys->Agree (X, sharedSecret);
|
|
|
|
m_NoiseState->MixKey (sharedSecret);
|
|
|
|
// encrypt
|
|
|
|
const uint8_t nonce[12] = {0};
|
|
|
|
i2p::crypto::AEADChaCha20Poly1305 (payload, payloadSize, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, payload, payloadSize + 16, true);
|
|
|
|
payloadSize += 16;
|
2022-03-22 04:06:14 +03:00
|
|
|
m_NoiseState->MixHash (payload, payloadSize); // h = SHA256(h || encrypted Noise payload from Session Created)
|
2022-03-09 05:33:21 +03:00
|
|
|
header.ll[0] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), payload + (payloadSize - 24));
|
|
|
|
header.ll[1] ^= CreateHeaderMask (kh2, payload + (payloadSize - 12));
|
|
|
|
i2p::crypto::ChaCha20 (headerX, 48, kh2, nonce, headerX);
|
|
|
|
// send
|
|
|
|
m_Server.Send (header.buf, 16, headerX, 48, payload, payloadSize, m_RemoteEndpoint);
|
2022-03-06 02:39:27 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 05:51:40 +03:00
|
|
|
bool SSU2Session::ProcessSessionCreated (uint8_t * buf, size_t len)
|
2022-02-10 22:03:09 +03:00
|
|
|
{
|
2022-03-06 02:39:27 +03:00
|
|
|
// we are Alice
|
2022-03-08 02:20:06 +03:00
|
|
|
Header header;
|
2022-03-20 22:10:18 +03:00
|
|
|
memcpy (header.buf, buf, 16);
|
|
|
|
header.ll[0] ^= CreateHeaderMask (m_Address->i, buf + (len - 24));
|
2022-03-05 05:51:40 +03:00
|
|
|
uint8_t kh2[32];
|
|
|
|
i2p::crypto::HKDF (m_NoiseState->m_CK, nullptr, 0, "SessCreateHeader", kh2, 32); // k_header_2 = HKDF(chainKey, ZEROLEN, "SessCreateHeader", 32)
|
2022-03-09 05:33:21 +03:00
|
|
|
header.ll[1] ^= CreateHeaderMask (kh2, buf + (len - 12));
|
|
|
|
if (header.h.type != eSSU2SessionCreated)
|
2022-03-21 19:56:02 +03:00
|
|
|
// this situation is valid, because it might be Retry with different encryption
|
2022-03-08 02:20:06 +03:00
|
|
|
return false;
|
|
|
|
const uint8_t nonce[12] = {0};
|
|
|
|
uint8_t headerX[48];
|
|
|
|
i2p::crypto::ChaCha20 (buf + 16, 48, kh2, nonce, headerX);
|
|
|
|
// KDF for SessionCreated
|
|
|
|
m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header)
|
|
|
|
m_NoiseState->MixHash (headerX + 16, 32); // h = SHA256(h || bepk);
|
|
|
|
uint8_t sharedSecret[32];
|
|
|
|
m_EphemeralKeys->Agree (headerX + 16, sharedSecret);
|
|
|
|
m_NoiseState->MixKey (sharedSecret);
|
|
|
|
// decrypt
|
|
|
|
uint8_t * payload = buf + 64;
|
2022-03-22 04:06:14 +03:00
|
|
|
std::vector<uint8_t> decryptedPayload(len - 80);
|
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 80, m_NoiseState->m_H, 32,
|
|
|
|
m_NoiseState->m_CK + 32, nonce, decryptedPayload.data (), decryptedPayload.size (), false))
|
2022-03-05 05:51:40 +03:00
|
|
|
{
|
2022-03-08 02:20:06 +03:00
|
|
|
LogPrint (eLogWarning, "SSU2: SessionCreated AEAD verification failed ");
|
2022-03-05 05:51:40 +03:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-22 04:06:14 +03:00
|
|
|
m_NoiseState->MixHash (payload, len - 64); // h = SHA256(h || encrypted payload from SessionCreated) for SessionConfirmed
|
2022-03-16 03:49:41 +03:00
|
|
|
// payload
|
2022-03-22 04:06:14 +03:00
|
|
|
HandlePayload (decryptedPayload.data (), decryptedPayload.size ());
|
2022-03-08 02:20:06 +03:00
|
|
|
|
2022-03-27 23:39:58 +03:00
|
|
|
m_Server.AddSession (shared_from_this ());
|
2022-03-22 04:06:14 +03:00
|
|
|
SendSessionConfirmed (headerX + 16);
|
2022-03-25 22:34:43 +03:00
|
|
|
KDFDataPhase (m_KeyDataSend, m_KeyDataReceive);
|
2022-03-26 23:35:07 +03:00
|
|
|
Established ();
|
2022-03-08 02:20:06 +03:00
|
|
|
|
2022-03-05 05:51:40 +03:00
|
|
|
return true;
|
2022-02-10 22:03:09 +03:00
|
|
|
}
|
2022-02-28 04:15:14 +03:00
|
|
|
|
2022-03-22 04:06:14 +03:00
|
|
|
void SSU2Session::SendSessionConfirmed (const uint8_t * Y)
|
|
|
|
{
|
|
|
|
// we are Alice
|
|
|
|
uint8_t kh2[32];
|
|
|
|
i2p::crypto::HKDF (m_NoiseState->m_CK, nullptr, 0, "SessionConfirmed", kh2, 32); // k_header_2 = HKDF(chainKey, ZEROLEN, "SessionConfirmed", 32)
|
|
|
|
// fill packet
|
|
|
|
Header header;
|
|
|
|
header.h.connID = m_DestConnID; // dest id
|
2022-03-23 21:06:55 +03:00
|
|
|
header.h.packetNum = 0;
|
2022-03-22 04:06:14 +03:00
|
|
|
header.h.type = eSSU2SessionConfirmed;
|
2022-03-24 02:13:44 +03:00
|
|
|
memset (header.h.flags, 0, 3);
|
2022-04-10 02:56:57 +03:00
|
|
|
header.h.flags[0] = 1; // frag, total fragments always 1
|
2022-03-22 04:06:14 +03:00
|
|
|
// payload
|
|
|
|
uint8_t payload[SSU2_MTU];
|
|
|
|
size_t payloadSize = i2p::context.GetRouterInfo ().GetBufferLen ();
|
|
|
|
payload[0] = eSSU2BlkRouterInfo;
|
|
|
|
if (payloadSize < 1024)
|
|
|
|
{
|
|
|
|
memcpy (payload + 5, i2p::context.GetRouterInfo ().GetBuffer (), payloadSize);
|
|
|
|
payload[3] = 0; // flag
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i2p::data::GzipDeflator deflator;
|
|
|
|
payloadSize = deflator.Deflate (i2p::context.GetRouterInfo ().GetBuffer (),
|
|
|
|
i2p::context.GetRouterInfo ().GetBufferLen (), payload + 5, SSU2_MTU -5);
|
|
|
|
payload[3] = SSU2_ROUTER_INFO_FLAG_GZIP; // flag
|
|
|
|
}
|
|
|
|
htobe16buf (payload + 1, payloadSize + 2);
|
|
|
|
payload[4] = 1; // frag
|
|
|
|
payloadSize += 5;
|
2022-03-26 23:35:07 +03:00
|
|
|
payloadSize += CreatePaddingBlock (payload + payloadSize, SSU2_MTU - payloadSize);
|
2022-03-22 04:06:14 +03:00
|
|
|
// KDF for Session Confirmed part 1
|
|
|
|
m_NoiseState->MixHash (header.buf, 16); // h = SHA256(h || header)
|
|
|
|
// Encrypt part 1
|
|
|
|
uint8_t part1[48];
|
|
|
|
uint8_t nonce[12];
|
|
|
|
CreateNonce (1, nonce);
|
|
|
|
i2p::crypto::AEADChaCha20Poly1305 (i2p::context.GetSSU2StaticPublicKey (), 32, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, part1, 48, true);
|
|
|
|
m_NoiseState->MixHash (part1, 48); // h = SHA256(h || ciphertext);
|
|
|
|
// KDF for Session Confirmed part 2
|
|
|
|
uint8_t sharedSecret[32];
|
|
|
|
i2p::context.GetSSU2StaticKeys ().Agree (Y, sharedSecret);
|
|
|
|
m_NoiseState->MixKey (sharedSecret);
|
|
|
|
// Encrypt part2
|
|
|
|
memset (nonce, 0, 12);
|
|
|
|
i2p::crypto::AEADChaCha20Poly1305 (payload, payloadSize, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, payload, payloadSize + 16, true);
|
|
|
|
payloadSize += 16;
|
|
|
|
m_NoiseState->MixHash (payload, payloadSize); // h = SHA256(h || ciphertext);
|
|
|
|
// Encrypt header
|
|
|
|
header.ll[0] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 24));
|
|
|
|
header.ll[1] ^= CreateHeaderMask (kh2, payload + (payloadSize - 12));
|
|
|
|
// send
|
|
|
|
m_Server.Send (header.buf, 16, part1, 48, payload, payloadSize, m_RemoteEndpoint);
|
2022-03-26 23:35:07 +03:00
|
|
|
m_SendPacketNum++;
|
2022-03-22 04:06:14 +03:00
|
|
|
}
|
2022-03-23 21:06:55 +03:00
|
|
|
|
|
|
|
bool SSU2Session::ProcessSessionConfirmed (uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
// we are Bob
|
|
|
|
Header header;
|
|
|
|
memcpy (header.buf, buf, 16);
|
|
|
|
header.ll[0] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), buf + (len - 24));
|
|
|
|
uint8_t kh2[32];
|
|
|
|
i2p::crypto::HKDF (m_NoiseState->m_CK, nullptr, 0, "SessionConfirmed", kh2, 32); // k_header_2 = HKDF(chainKey, ZEROLEN, "SessionConfirmed", 32)
|
|
|
|
header.ll[1] ^= CreateHeaderMask (kh2, buf + (len - 12));
|
|
|
|
if (header.h.type != eSSU2SessionConfirmed)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type);
|
|
|
|
return false;
|
|
|
|
}
|
2022-04-10 02:56:57 +03:00
|
|
|
// check if fragmented
|
|
|
|
if ((header.h.flags[0] & 0x0F) > 1)
|
|
|
|
{
|
|
|
|
// fragmented
|
|
|
|
if (!(header.h.flags[0] & 0xF0))
|
|
|
|
{
|
|
|
|
// first fragment
|
2022-04-16 22:42:11 +03:00
|
|
|
m_SessionConfirmedFragment1.reset (new SessionConfirmedFragment);
|
2022-04-10 02:56:57 +03:00
|
|
|
m_SessionConfirmedFragment1->header = header;
|
|
|
|
memcpy (m_SessionConfirmedFragment1->payload, buf + 16, len - 16);
|
2022-04-16 22:42:11 +03:00
|
|
|
m_SessionConfirmedFragment1->payloadSize = len - 16;
|
2022-04-10 02:56:57 +03:00
|
|
|
return true; // wait for second fragment
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// second fragment
|
|
|
|
if (!m_SessionConfirmedFragment1) return false; // out of sequence
|
|
|
|
uint8_t fullMsg[2*SSU2_MTU];
|
|
|
|
header = m_SessionConfirmedFragment1->header;
|
2022-04-16 22:42:11 +03:00
|
|
|
memcpy (fullMsg + 16, m_SessionConfirmedFragment1->payload, m_SessionConfirmedFragment1->payloadSize);
|
|
|
|
memcpy (fullMsg + 16 + m_SessionConfirmedFragment1->payloadSize, buf + 16, len - 16);
|
2022-04-10 02:56:57 +03:00
|
|
|
buf = fullMsg;
|
2022-04-16 22:42:11 +03:00
|
|
|
len += m_SessionConfirmedFragment1->payloadSize;
|
2022-04-10 02:56:57 +03:00
|
|
|
}
|
|
|
|
}
|
2022-03-23 21:06:55 +03:00
|
|
|
// KDF for Session Confirmed part 1
|
|
|
|
m_NoiseState->MixHash (header.buf, 16); // h = SHA256(h || header)
|
|
|
|
// decrypt part1
|
|
|
|
uint8_t nonce[12];
|
|
|
|
CreateNonce (1, nonce);
|
|
|
|
uint8_t S[32];
|
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (buf + 16, 32, m_NoiseState->m_H, 32,
|
|
|
|
m_NoiseState->m_CK + 32, nonce, S, 32, false))
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: SessionConfirmed part 1 AEAD verification failed ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_NoiseState->MixHash (buf + 16, 48); // h = SHA256(h || ciphertext);
|
|
|
|
// KDF for Session Confirmed part 2
|
|
|
|
uint8_t sharedSecret[32];
|
|
|
|
m_EphemeralKeys->Agree (S, sharedSecret);
|
|
|
|
m_NoiseState->MixKey (sharedSecret);
|
|
|
|
// decrypt part2
|
2022-04-04 18:52:14 +03:00
|
|
|
memset (nonce, 0, 12);
|
2022-03-23 21:06:55 +03:00
|
|
|
uint8_t * payload = buf + 64;
|
|
|
|
std::vector<uint8_t> decryptedPayload(len - 80);
|
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 80, m_NoiseState->m_H, 32,
|
|
|
|
m_NoiseState->m_CK + 32, nonce, decryptedPayload.data (), decryptedPayload.size (), false))
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: SessionConfirmed part 2 AEAD verification failed ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_NoiseState->MixHash (payload, len - 64); // h = SHA256(h || ciphertext);
|
|
|
|
// payload
|
|
|
|
// handle RouterInfo block that must be first
|
|
|
|
if (decryptedPayload[0] != eSSU2BlkRouterInfo)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU2: SessionConfirmed unexpected first block type ", (int)decryptedPayload[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size_t riSize = bufbe16toh (decryptedPayload.data () + 1);
|
|
|
|
if (riSize + 3 > decryptedPayload.size ())
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU2: SessionConfirmed RouterInfo block is too long ", riSize);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LogPrint (eLogDebug, "SSU2: RouterInfo in SessionConfirmed");
|
|
|
|
auto ri = ExtractRouterInfo (decryptedPayload.data () + 3, riSize);
|
|
|
|
if (!ri)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU2: SessionConfirmed malformed RouterInfo block");
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-27 16:26:45 +03:00
|
|
|
SetRemoteIdentity (ri->GetRouterIdentity ());
|
2022-04-28 20:11:51 +03:00
|
|
|
m_Server.AddSessionByRouterHash (shared_from_this ()); // we know remote router now
|
2022-03-29 01:03:22 +03:00
|
|
|
m_Address = ri->GetSSU2AddressWithStaticKey (S, m_RemoteEndpoint.address ().is_v6 ());
|
2022-03-27 16:26:45 +03:00
|
|
|
if (!m_Address)
|
2022-03-23 21:06:55 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU2: No SSU2 address with static key found in SessionConfirmed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, ri->GetBuffer (), ri->GetBufferLen ())); // TODO: should insert ri
|
|
|
|
// handle other blocks
|
|
|
|
HandlePayload (decryptedPayload.data () + riSize + 3, decryptedPayload.size () - riSize - 3);
|
2022-03-25 22:34:43 +03:00
|
|
|
KDFDataPhase (m_KeyDataReceive, m_KeyDataSend);
|
2022-03-26 23:35:07 +03:00
|
|
|
Established ();
|
2022-03-27 14:47:25 +03:00
|
|
|
|
|
|
|
SendQuickAck ();
|
2022-03-26 23:35:07 +03:00
|
|
|
|
2022-03-23 21:06:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
2022-03-24 02:13:44 +03:00
|
|
|
|
2022-03-25 22:34:43 +03:00
|
|
|
void SSU2Session::KDFDataPhase (uint8_t * keydata_ab, uint8_t * keydata_ba)
|
|
|
|
{
|
|
|
|
uint8_t keydata[64];
|
|
|
|
i2p::crypto::HKDF (m_NoiseState->m_CK, nullptr, 0, "", keydata); // keydata = HKDF(chainKey, ZEROLEN, "", 64)
|
|
|
|
// ab
|
|
|
|
i2p::crypto::HKDF (keydata, nullptr, 0, "HKDFSSU2DataKeys", keydata_ab); // keydata_ab = HKDF(keydata, ZEROLEN, "HKDFSSU2DataKeys", 64)
|
|
|
|
// ba
|
|
|
|
i2p::crypto::HKDF (keydata + 32, nullptr, 0, "HKDFSSU2DataKeys", keydata_ba); // keydata_ba = HKDF(keydata + 32, ZEROLEN, "HKDFSSU2DataKeys", 64)
|
|
|
|
}
|
|
|
|
|
2022-03-24 02:13:44 +03:00
|
|
|
void SSU2Session::SendTokenRequest ()
|
|
|
|
{
|
|
|
|
// we are Alice
|
|
|
|
Header header;
|
|
|
|
uint8_t h[32], payload[40];
|
|
|
|
// fill packet
|
|
|
|
header.h.connID = m_DestConnID; // dest id
|
2022-04-04 16:58:17 +03:00
|
|
|
RAND_bytes (header.buf + 8, 4); // random packet num
|
2022-03-24 02:13:44 +03:00
|
|
|
header.h.type = eSSU2TokenRequest;
|
|
|
|
header.h.flags[0] = 2; // ver
|
|
|
|
header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID
|
|
|
|
header.h.flags[2] = 0; // flag
|
|
|
|
memcpy (h, header.buf, 16);
|
|
|
|
memcpy (h + 16, &m_SourceConnID, 8); // source id
|
|
|
|
memset (h + 24, 0, 8); // zero token
|
|
|
|
// payload
|
|
|
|
payload[0] = eSSU2BlkDateTime;
|
|
|
|
htobe16buf (payload + 1, 4);
|
|
|
|
htobe32buf (payload + 3, i2p::util::GetSecondsSinceEpoch ());
|
|
|
|
size_t payloadSize = 7;
|
2022-03-27 16:26:45 +03:00
|
|
|
payloadSize += CreatePaddingBlock (payload + payloadSize, 40 - payloadSize, 1);
|
2022-03-24 02:13:44 +03:00
|
|
|
// encrypt
|
2022-04-04 16:58:17 +03:00
|
|
|
uint8_t nonce[12];
|
|
|
|
CreateNonce (be32toh (header.h.packetNum), nonce);
|
2022-03-24 02:13:44 +03:00
|
|
|
i2p::crypto::AEADChaCha20Poly1305 (payload, payloadSize, h, 32, m_Address->i, nonce, payload, payloadSize + 16, true);
|
|
|
|
payloadSize += 16;
|
|
|
|
header.ll[0] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 24));
|
|
|
|
header.ll[1] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 12));
|
2022-04-04 16:58:17 +03:00
|
|
|
memset (nonce, 0, 12);
|
2022-03-24 02:13:44 +03:00
|
|
|
i2p::crypto::ChaCha20 (h + 16, 16, m_Address->i, nonce, h + 16);
|
|
|
|
// send
|
2022-03-27 23:39:58 +03:00
|
|
|
m_Server.AddPendingOutgoingSession (shared_from_this ());
|
2022-03-24 02:13:44 +03:00
|
|
|
m_Server.Send (header.buf, 16, h + 16, 16, payload, payloadSize, m_RemoteEndpoint);
|
|
|
|
}
|
2022-03-24 04:48:41 +03:00
|
|
|
|
|
|
|
void SSU2Session::ProcessTokenRequest (Header& header, uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
// we are Bob
|
2022-04-04 16:58:17 +03:00
|
|
|
uint8_t nonce[12] = {0};
|
2022-03-24 04:48:41 +03:00
|
|
|
uint8_t h[32];
|
|
|
|
memcpy (h, header.buf, 16);
|
|
|
|
i2p::crypto::ChaCha20 (buf + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, h + 16);
|
2022-04-04 16:58:17 +03:00
|
|
|
memcpy (&m_DestConnID, h + 16, 8);
|
2022-03-24 04:48:41 +03:00
|
|
|
// decrypt
|
2022-04-04 16:58:17 +03:00
|
|
|
CreateNonce (be32toh (header.h.packetNum), nonce);
|
2022-03-24 04:48:41 +03:00
|
|
|
uint8_t * payload = buf + 32;
|
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 48, h, 32,
|
|
|
|
i2p::context.GetSSU2IntroKey (), nonce, payload, len - 48, false))
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: TokenRequest AEAD verification failed ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// payload
|
|
|
|
HandlePayload (payload, len - 48);
|
|
|
|
SendRetry ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Session::SendRetry ()
|
|
|
|
{
|
|
|
|
// we are Bob
|
|
|
|
Header header;
|
|
|
|
uint8_t h[32], payload[64];
|
|
|
|
// fill packet
|
|
|
|
header.h.connID = m_DestConnID; // dest id
|
2022-04-04 16:58:17 +03:00
|
|
|
RAND_bytes (header.buf + 8, 4); // random packet num
|
2022-03-24 04:48:41 +03:00
|
|
|
header.h.type = eSSU2Retry;
|
|
|
|
header.h.flags[0] = 2; // ver
|
|
|
|
header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID
|
|
|
|
header.h.flags[2] = 0; // flag
|
|
|
|
memcpy (h, header.buf, 16);
|
|
|
|
memcpy (h + 16, &m_SourceConnID, 8); // source id
|
|
|
|
uint64_t token = m_Server.GetIncomingToken (m_RemoteEndpoint);
|
|
|
|
memcpy (h + 24, &token, 8); // token
|
|
|
|
// payload
|
|
|
|
payload[0] = eSSU2BlkDateTime;
|
|
|
|
htobe16buf (payload + 1, 4);
|
|
|
|
htobe32buf (payload + 3, i2p::util::GetSecondsSinceEpoch ());
|
|
|
|
size_t payloadSize = 7;
|
2022-04-03 20:43:33 +03:00
|
|
|
payloadSize += CreateAddressBlock (m_RemoteEndpoint, payload + payloadSize, 64 - payloadSize);
|
2022-03-26 23:35:07 +03:00
|
|
|
payloadSize += CreatePaddingBlock (payload + payloadSize, 64 - payloadSize);
|
2022-03-24 04:48:41 +03:00
|
|
|
// encrypt
|
2022-04-04 16:58:17 +03:00
|
|
|
uint8_t nonce[12];
|
|
|
|
CreateNonce (be32toh (header.h.packetNum), nonce);
|
2022-03-24 04:48:41 +03:00
|
|
|
i2p::crypto::AEADChaCha20Poly1305 (payload, payloadSize, h, 32, i2p::context.GetSSU2IntroKey (), nonce, payload, payloadSize + 16, true);
|
|
|
|
payloadSize += 16;
|
|
|
|
header.ll[0] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), payload + (payloadSize - 24));
|
|
|
|
header.ll[1] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), payload + (payloadSize - 12));
|
2022-04-04 16:58:17 +03:00
|
|
|
memset (nonce, 0, 12);
|
2022-03-24 04:48:41 +03:00
|
|
|
i2p::crypto::ChaCha20 (h + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, h + 16);
|
|
|
|
// send
|
|
|
|
m_Server.Send (header.buf, 16, h + 16, 16, payload, payloadSize, m_RemoteEndpoint);
|
|
|
|
}
|
2022-03-22 04:06:14 +03:00
|
|
|
|
2022-03-20 22:10:18 +03:00
|
|
|
bool SSU2Session::ProcessRetry (uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
// we are Alice
|
|
|
|
Header header;
|
|
|
|
memcpy (header.buf, buf, 16);
|
|
|
|
header.ll[0] ^= CreateHeaderMask (m_Address->i, buf + (len - 24));
|
|
|
|
header.ll[1] ^= CreateHeaderMask (m_Address->i, buf + (len - 12));
|
|
|
|
if (header.h.type != eSSU2Retry)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type);
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-24 04:48:41 +03:00
|
|
|
uint8_t nonce[12] = {0};
|
2022-03-20 22:10:18 +03:00
|
|
|
uint64_t headerX[2]; // sourceConnID, token
|
|
|
|
i2p::crypto::ChaCha20 (buf + 16, 16, m_Address->i, nonce, (uint8_t *)headerX);
|
2022-03-24 02:13:44 +03:00
|
|
|
m_Server.UpdateOutgoingToken (m_RemoteEndpoint, headerX[1], i2p::util::GetSecondsSinceEpoch () + SSU2_TOKEN_EXPIRATION_TIMEOUT);
|
2022-03-24 04:48:41 +03:00
|
|
|
// decrypt and handle payload
|
|
|
|
uint8_t * payload = buf + 32;
|
|
|
|
CreateNonce (be32toh (header.h.packetNum), nonce);
|
|
|
|
uint8_t h[32];
|
|
|
|
memcpy (h, header.buf, 16);
|
|
|
|
memcpy (h + 16, &headerX, 16);
|
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 48, h, 32,
|
|
|
|
m_Address->i, nonce, payload, len - 48, false))
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: Retry AEAD verification failed ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
HandlePayload (payload, len - 48);
|
|
|
|
|
2022-03-24 02:13:44 +03:00
|
|
|
InitNoiseXKState1 (*m_NoiseState, m_Address->s); // reset Noise TODO: check state
|
2022-03-20 22:10:18 +03:00
|
|
|
SendSessionRequest (headerX[1]);
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-25 22:34:43 +03:00
|
|
|
|
2022-04-16 22:42:11 +03:00
|
|
|
uint32_t SSU2Session::SendData (const uint8_t * buf, size_t len)
|
2022-03-26 23:35:07 +03:00
|
|
|
{
|
2022-03-27 14:47:25 +03:00
|
|
|
if (len < 8)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: Data message payload is too short ", (int)len);
|
2022-04-16 22:42:11 +03:00
|
|
|
return 0;
|
2022-03-27 14:47:25 +03:00
|
|
|
}
|
2022-04-16 22:42:11 +03:00
|
|
|
Header header;
|
2022-03-26 23:35:07 +03:00
|
|
|
header.h.connID = m_DestConnID;
|
|
|
|
header.h.packetNum = htobe32 (m_SendPacketNum);
|
|
|
|
header.h.type = eSSU2Data;
|
|
|
|
memset (header.h.flags, 0, 3);
|
|
|
|
uint8_t nonce[12];
|
|
|
|
CreateNonce (m_SendPacketNum, nonce);
|
2022-04-16 22:42:11 +03:00
|
|
|
uint8_t payload[SSU2_MTU];
|
|
|
|
i2p::crypto::AEADChaCha20Poly1305 (buf, len, header.buf, 16, m_KeyDataSend, nonce, payload, SSU2_MTU, true);
|
|
|
|
header.ll[0] ^= CreateHeaderMask (m_Address->i, payload + (len - 8));
|
|
|
|
header.ll[1] ^= CreateHeaderMask (m_KeyDataSend + 32, payload + (len + 4));
|
|
|
|
m_Server.Send (header.buf, 16, payload, len + 16, m_RemoteEndpoint);
|
2022-03-26 23:35:07 +03:00
|
|
|
m_SendPacketNum++;
|
|
|
|
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch ();
|
2022-04-16 22:42:11 +03:00
|
|
|
m_NumSentBytes += len + 32;
|
|
|
|
return m_SendPacketNum - 1;
|
2022-03-26 23:35:07 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 22:34:43 +03:00
|
|
|
void SSU2Session::ProcessData (uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
Header header;
|
2022-03-26 00:57:59 +03:00
|
|
|
header.ll[0] = m_SourceConnID;
|
|
|
|
memcpy (header.buf + 8, buf + 8, 8);
|
2022-03-25 22:34:43 +03:00
|
|
|
header.ll[1] ^= CreateHeaderMask (m_KeyDataReceive + 32, buf + (len - 12));
|
|
|
|
if (header.h.type != eSSU2Data)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint8_t payload[SSU2_MTU];
|
|
|
|
size_t payloadSize = len - 32;
|
2022-03-26 23:35:07 +03:00
|
|
|
uint32_t packetNum = be32toh (header.h.packetNum);
|
2022-03-25 22:34:43 +03:00
|
|
|
uint8_t nonce[12];
|
2022-03-26 23:35:07 +03:00
|
|
|
CreateNonce (packetNum, nonce);
|
2022-03-25 22:34:43 +03:00
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (buf + 16, payloadSize, header.buf, 16,
|
|
|
|
m_KeyDataReceive, nonce, payload, payloadSize, false))
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: Data AEAD verification failed ");
|
|
|
|
return;
|
|
|
|
}
|
2022-03-26 23:35:07 +03:00
|
|
|
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
m_NumReceivedBytes += len;
|
2022-03-30 22:03:45 +03:00
|
|
|
if (UpdateReceivePacketNum (packetNum))
|
2022-04-12 18:42:51 +03:00
|
|
|
HandlePayload (payload, payloadSize);
|
2022-03-25 22:34:43 +03:00
|
|
|
}
|
2022-03-20 22:10:18 +03:00
|
|
|
|
2022-04-12 18:42:51 +03:00
|
|
|
void SSU2Session::HandlePayload (const uint8_t * buf, size_t len)
|
2022-03-16 03:49:41 +03:00
|
|
|
{
|
|
|
|
size_t offset = 0;
|
|
|
|
while (offset < len)
|
|
|
|
{
|
|
|
|
uint8_t blk = buf[offset];
|
|
|
|
offset++;
|
|
|
|
auto size = bufbe16toh (buf + offset);
|
|
|
|
offset += 2;
|
|
|
|
LogPrint (eLogDebug, "SSU2: Block type ", (int)blk, " of size ", size);
|
|
|
|
if (size > len)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU2: Unexpected block length ", size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (blk)
|
|
|
|
{
|
|
|
|
case eSSU2BlkDateTime:
|
|
|
|
LogPrint (eLogDebug, "SSU2: Datetime");
|
|
|
|
break;
|
|
|
|
case eSSU2BlkOptions:
|
|
|
|
LogPrint (eLogDebug, "SSU2: Options");
|
|
|
|
break;
|
|
|
|
case eSSU2BlkRouterInfo:
|
2022-03-23 21:06:55 +03:00
|
|
|
{
|
|
|
|
// not from SessionConfirmed
|
|
|
|
LogPrint (eLogDebug, "SSU2: RouterInfo");
|
|
|
|
auto ri = ExtractRouterInfo (buf + offset, size);
|
|
|
|
if (ri)
|
|
|
|
i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, ri->GetBuffer (), ri->GetBufferLen ())); // TODO: should insert ri
|
|
|
|
break;
|
|
|
|
}
|
2022-03-16 03:49:41 +03:00
|
|
|
case eSSU2BlkI2NPMessage:
|
2022-03-27 04:59:21 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "SSU2: I2NP message");
|
|
|
|
auto nextMsg = NewI2NPShortMessage ();
|
|
|
|
nextMsg->len = nextMsg->offset + size + 7; // 7 more bytes for full I2NP header
|
|
|
|
memcpy (nextMsg->GetNTCP2Header (), buf + offset, size);
|
|
|
|
nextMsg->FromNTCP2 (); // SSU2 has the same format as NTCP2
|
|
|
|
m_Handler.PutNextMessage (std::move (nextMsg));
|
2022-04-12 18:42:51 +03:00
|
|
|
m_IsDataReceived = true;
|
2022-03-27 04:59:21 +03:00
|
|
|
break;
|
|
|
|
}
|
2022-03-16 03:49:41 +03:00
|
|
|
case eSSU2BlkFirstFragment:
|
2022-04-02 20:05:11 +03:00
|
|
|
LogPrint (eLogDebug, "SSU2: First fragment");
|
|
|
|
HandleFirstFragment (buf + offset, size);
|
2022-04-12 18:42:51 +03:00
|
|
|
m_IsDataReceived = true;
|
2022-03-16 03:49:41 +03:00
|
|
|
break;
|
|
|
|
case eSSU2BlkFollowOnFragment:
|
2022-04-02 20:05:11 +03:00
|
|
|
LogPrint (eLogDebug, "SSU2: Follow-on fragment");
|
|
|
|
HandleFollowOnFragment (buf + offset, size);
|
2022-04-12 18:42:51 +03:00
|
|
|
m_IsDataReceived = true;
|
2022-03-16 03:49:41 +03:00
|
|
|
break;
|
|
|
|
case eSSU2BlkTermination:
|
2022-03-27 23:39:58 +03:00
|
|
|
LogPrint (eLogDebug, "SSU2: Termination");
|
|
|
|
Terminate ();
|
2022-03-16 03:49:41 +03:00
|
|
|
break;
|
|
|
|
case eSSU2BlkRelayRequest:
|
2022-04-21 22:47:36 +03:00
|
|
|
LogPrint (eLogDebug, "SSU2: RelayRequest");
|
|
|
|
HandleRelayRequest (buf + offset, size);
|
2022-03-16 03:49:41 +03:00
|
|
|
break;
|
|
|
|
case eSSU2BlkRelayResponse:
|
2022-04-26 20:59:59 +03:00
|
|
|
LogPrint (eLogDebug, "SSU2: RelayResponse");
|
|
|
|
HandleRelayResponse (buf + offset, size);
|
2022-03-16 03:49:41 +03:00
|
|
|
break;
|
|
|
|
case eSSU2BlkRelayIntro:
|
|
|
|
break;
|
|
|
|
case eSSU2BlkPeerTest:
|
|
|
|
break;
|
|
|
|
case eSSU2BlkNextNonce:
|
|
|
|
break;
|
|
|
|
case eSSU2BlkAck:
|
2022-03-30 22:03:45 +03:00
|
|
|
LogPrint (eLogDebug, "SSU2: Ack");
|
|
|
|
HandleAck (buf + offset, size);
|
2022-03-16 03:49:41 +03:00
|
|
|
break;
|
|
|
|
case eSSU2BlkAddress:
|
2022-03-16 20:13:31 +03:00
|
|
|
{
|
|
|
|
boost::asio::ip::udp::endpoint ep;
|
|
|
|
if (ExtractEndpoint (buf + offset, size, ep))
|
|
|
|
LogPrint (eLogInfo, "SSU2: Our external address is ", ep);
|
|
|
|
break;
|
|
|
|
}
|
2022-03-16 03:49:41 +03:00
|
|
|
case eSSU2BlkIntroKey:
|
|
|
|
break;
|
|
|
|
case eSSU2BlkRelayTagRequest:
|
2022-04-21 22:47:36 +03:00
|
|
|
LogPrint (eLogDebug, "SSU2: RelayTagRequest");
|
|
|
|
HandleRelayRequest (buf + offset, size);
|
|
|
|
if (!m_RelayTag)
|
|
|
|
{
|
|
|
|
RAND_bytes ((uint8_t *)&m_RelayTag, 4);
|
|
|
|
m_Server.AddRelay (m_RelayTag, shared_from_this ());
|
|
|
|
}
|
2022-03-16 03:49:41 +03:00
|
|
|
break;
|
|
|
|
case eSSU2BlkRelayTag:
|
2022-04-22 22:03:49 +03:00
|
|
|
LogPrint (eLogDebug, "SSU2: RelayTag");
|
|
|
|
m_RelayTag = bufbe32toh (buf + offset);
|
2022-03-16 03:49:41 +03:00
|
|
|
break;
|
|
|
|
case eSSU2BlkNewToken:
|
2022-03-22 04:06:14 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "SSU2: New token");
|
2022-03-24 02:13:44 +03:00
|
|
|
uint64_t token;
|
|
|
|
memcpy (&token, buf + offset + 4, 8);
|
|
|
|
m_Server.UpdateOutgoingToken (m_RemoteEndpoint, token, bufbe32toh (buf + offset));
|
2022-03-22 04:06:14 +03:00
|
|
|
break;
|
|
|
|
}
|
2022-03-16 03:49:41 +03:00
|
|
|
case eSSU2BlkPathChallenge:
|
|
|
|
break;
|
|
|
|
case eSSU2BlkPathResponse:
|
|
|
|
break;
|
|
|
|
case eSSU2BlkFirstPacketNumber:
|
|
|
|
break;
|
|
|
|
case eSSU2BlkPadding:
|
|
|
|
LogPrint (eLogDebug, "SSU2: Padding");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LogPrint (eLogWarning, "SSU2: Unknown block type ", (int)blk);
|
|
|
|
}
|
|
|
|
offset += size;
|
|
|
|
}
|
|
|
|
}
|
2022-03-16 20:13:31 +03:00
|
|
|
|
2022-03-30 22:03:45 +03:00
|
|
|
void SSU2Session::HandleAck (const uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
if (m_SentPackets.empty ()) return;
|
2022-04-01 22:09:35 +03:00
|
|
|
if (len < 5) return;
|
|
|
|
// acnt
|
2022-03-30 22:03:45 +03:00
|
|
|
uint32_t ackThrough = bufbe32toh (buf);
|
2022-04-01 22:09:35 +03:00
|
|
|
uint32_t firstPacketNum = ackThrough > buf[4] ? ackThrough - buf[4] : 0;
|
|
|
|
HandleAckRange (firstPacketNum, ackThrough); // acnt
|
|
|
|
// ranges
|
|
|
|
len -= 5;
|
|
|
|
const uint8_t * ranges = buf + 5;
|
|
|
|
while (len > 0 && firstPacketNum)
|
|
|
|
{
|
|
|
|
uint32_t lastPacketNum = firstPacketNum - 1;
|
|
|
|
if (*ranges > lastPacketNum) break;
|
|
|
|
lastPacketNum -= *ranges; ranges++; // nacks
|
|
|
|
if (*ranges > lastPacketNum) break;
|
2022-04-23 18:11:49 +03:00
|
|
|
firstPacketNum = lastPacketNum - *ranges; ranges++; // acks
|
2022-04-01 22:09:35 +03:00
|
|
|
len -= 2;
|
|
|
|
HandleAckRange (firstPacketNum, lastPacketNum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Session::HandleAckRange (uint32_t firstPacketNum, uint32_t lastPacketNum)
|
|
|
|
{
|
2022-04-23 18:11:49 +03:00
|
|
|
if (firstPacketNum > lastPacketNum) return;
|
2022-03-31 22:35:55 +03:00
|
|
|
auto it = m_SentPackets.begin ();
|
|
|
|
while (it != m_SentPackets.end () && it->first < firstPacketNum) it++; // find first acked packet
|
|
|
|
if (it == m_SentPackets.end ()) return; // not found
|
2022-03-30 22:03:45 +03:00
|
|
|
auto it1 = it;
|
2022-04-01 22:09:35 +03:00
|
|
|
while (it1 != m_SentPackets.end () && it1->first <= lastPacketNum) it1++;
|
2022-04-17 00:01:06 +03:00
|
|
|
if (it1 != m_SentPackets.end ()) it1--;
|
2022-03-31 22:35:55 +03:00
|
|
|
m_SentPackets.erase (it, it1);
|
2022-04-01 22:09:35 +03:00
|
|
|
}
|
2022-04-02 20:05:11 +03:00
|
|
|
|
|
|
|
void SSU2Session::HandleFirstFragment (const uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
uint32_t msgID; memcpy (&msgID, buf + 1, 4);
|
|
|
|
auto msg = NewI2NPMessage ();
|
|
|
|
// same format as I2NP message block
|
|
|
|
msg->len = msg->offset + len + 7;
|
|
|
|
memcpy (msg->GetNTCP2Header (), buf, len);
|
|
|
|
std::shared_ptr<SSU2IncompleteMessage> m;
|
|
|
|
bool found = false;
|
|
|
|
auto it = m_IncompleteMessages.find (msgID);
|
|
|
|
if (it != m_IncompleteMessages.end ())
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
m = it->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m = std::make_shared<SSU2IncompleteMessage>();
|
|
|
|
m_IncompleteMessages.emplace (msgID, m);
|
|
|
|
}
|
|
|
|
m->msg = msg;
|
|
|
|
m->nextFragmentNum = 1;
|
|
|
|
m->lastFragmentInsertTime = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
if (found && ConcatOutOfSequenceFragments (m))
|
|
|
|
{
|
|
|
|
// we have all follow-on fragments already
|
2022-04-04 20:25:08 +03:00
|
|
|
m->msg->FromNTCP2 ();
|
2022-04-02 20:05:11 +03:00
|
|
|
m_Handler.PutNextMessage (std::move (m->msg));
|
|
|
|
m_IncompleteMessages.erase (it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Session::HandleFollowOnFragment (const uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
if (len < 5) return;
|
|
|
|
uint8_t fragmentNum = buf[0] >> 1;
|
|
|
|
bool isLast = buf[0] & 0x01;
|
|
|
|
uint32_t msgID; memcpy (&msgID, buf + 1, 4);
|
|
|
|
auto it = m_IncompleteMessages.find (msgID);
|
|
|
|
if (it != m_IncompleteMessages.end ())
|
|
|
|
{
|
|
|
|
if (it->second->nextFragmentNum == fragmentNum && it->second->msg)
|
|
|
|
{
|
|
|
|
// in sequence
|
|
|
|
it->second->msg->Concat (buf + 5, len - 5);
|
|
|
|
if (isLast)
|
|
|
|
{
|
2022-04-04 20:25:08 +03:00
|
|
|
it->second->msg->FromNTCP2 ();
|
2022-04-02 20:05:11 +03:00
|
|
|
m_Handler.PutNextMessage (std::move (it->second->msg));
|
|
|
|
m_IncompleteMessages.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it->second->nextFragmentNum++;
|
|
|
|
if (ConcatOutOfSequenceFragments (it->second))
|
|
|
|
{
|
|
|
|
m_Handler.PutNextMessage (std::move (it->second->msg));
|
|
|
|
m_IncompleteMessages.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
it->second->lastFragmentInsertTime = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// follow-on fragment before first fragment
|
|
|
|
auto msg = std::make_shared<SSU2IncompleteMessage> ();
|
|
|
|
msg->nextFragmentNum = 0;
|
|
|
|
it = m_IncompleteMessages.emplace (msgID, msg).first;
|
|
|
|
}
|
|
|
|
// insert out of sequence fragment
|
|
|
|
auto fragment = std::make_shared<SSU2IncompleteMessage::Fragment> ();
|
|
|
|
memcpy (fragment->buf, buf + 5, len -5);
|
|
|
|
fragment->len = len - 5;
|
|
|
|
fragment->isLast = isLast;
|
|
|
|
it->second->outOfSequenceFragments.emplace (fragmentNum, fragment);
|
|
|
|
it->second->lastFragmentInsertTime = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SSU2Session::ConcatOutOfSequenceFragments (std::shared_ptr<SSU2IncompleteMessage> m)
|
|
|
|
{
|
|
|
|
if (!m) return false;
|
|
|
|
bool isLast = false;
|
|
|
|
for (auto it = m->outOfSequenceFragments.begin (); it != m->outOfSequenceFragments.end ();)
|
|
|
|
if (it->first == m->nextFragmentNum)
|
|
|
|
{
|
|
|
|
m->msg->Concat (it->second->buf, it->second->len);
|
|
|
|
isLast = it->second->isLast;
|
|
|
|
it = m->outOfSequenceFragments.erase (it);
|
|
|
|
m->nextFragmentNum++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
return isLast;
|
|
|
|
}
|
2022-04-21 22:47:36 +03:00
|
|
|
|
|
|
|
void SSU2Session::HandleRelayRequest (const uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
// we are Bob
|
|
|
|
uint32_t relayTag = bufbe32toh (buf + 5); // relay tag
|
|
|
|
auto session = m_Server.FindRelaySession (relayTag);
|
|
|
|
if (!session)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: Session with relay tag ", relayTag, " not found");
|
|
|
|
return; // TODO: send relay response
|
|
|
|
}
|
2022-04-26 20:59:59 +03:00
|
|
|
session->m_RelaySessions.emplace (bufbe32toh (buf + 1), // nonce
|
|
|
|
std::make_pair (shared_from_this (), i2p::util::GetSecondsSinceEpoch ()) );
|
|
|
|
|
2022-04-21 22:47:36 +03:00
|
|
|
// send relay intro to Charlie
|
|
|
|
uint8_t payload[SSU2_MTU];
|
|
|
|
size_t payloadSize = CreateRelayIntroBlock (payload, SSU2_MTU, buf + 1, len -1);
|
|
|
|
payloadSize += CreatePaddingBlock (payload + payloadSize, SSU2_MTU - payloadSize);
|
|
|
|
session->SendData (payload, payloadSize);
|
|
|
|
}
|
2022-04-22 22:03:49 +03:00
|
|
|
|
|
|
|
void SSU2Session::HandleRelayIntro (const uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
// we are Charlie
|
2022-04-23 03:34:19 +03:00
|
|
|
auto r = i2p::data::netdb.FindRouter (buf + 1); // Alice
|
|
|
|
if (!r)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU2: RelayIntro unknown router to introduce");
|
|
|
|
return;
|
|
|
|
}
|
2022-04-22 22:03:49 +03:00
|
|
|
SignedData s;
|
|
|
|
s.Insert ((const uint8_t *)"RelayRequestData", 16); // prologue
|
|
|
|
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
|
|
|
s.Insert (i2p::context.GetIdentHash (), 32); // chash
|
|
|
|
s.Insert (buf + 33, 14); // nonce, relay tag, timestamp, ver, asz
|
|
|
|
uint8_t asz = buf[46];
|
|
|
|
s.Insert (buf + 47, asz); // Alice Port, Alice IP
|
2022-04-23 03:34:19 +03:00
|
|
|
if (!s.Verify (r->GetIdentity (), buf + 47 + asz))
|
2022-04-22 22:03:49 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: RelayIntro signature verification failed");
|
|
|
|
return; // TODO: send relay response
|
|
|
|
}
|
|
|
|
|
2022-04-23 03:34:19 +03:00
|
|
|
// send relay response to Bob
|
|
|
|
uint8_t payload[SSU2_MTU];
|
2022-04-26 20:59:59 +03:00
|
|
|
size_t payloadSize = CreateRelayResponseBlock (payload, SSU2_MTU, bufbe32toh (buf + 33));
|
2022-04-23 03:34:19 +03:00
|
|
|
payloadSize += CreatePaddingBlock (payload + payloadSize, SSU2_MTU - payloadSize);
|
|
|
|
SendData (payload, payloadSize);
|
|
|
|
|
|
|
|
// send HolePunch
|
2022-04-22 22:03:49 +03:00
|
|
|
boost::asio::ip::udp::endpoint ep;
|
|
|
|
if (ExtractEndpoint (buf + 47, asz, ep))
|
|
|
|
m_Server.SendHolePunch (ep);
|
|
|
|
}
|
2022-04-26 20:59:59 +03:00
|
|
|
|
|
|
|
void SSU2Session::HandleRelayResponse (const uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
auto it = m_RelaySessions.find (bufbe32toh (buf + 2)); // nonce
|
|
|
|
if (it != m_RelaySessions.end ())
|
|
|
|
{
|
|
|
|
if (it->second.first && it->second.first->IsEstablished ())
|
|
|
|
// we are Bob, message from Charlie
|
|
|
|
it->second.first->SendData (buf, len); // forward to Alice as is
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we are Alice, message from Bob
|
2022-04-28 18:43:33 +03:00
|
|
|
// update Charlie's endpoint and connect
|
|
|
|
if (ExtractEndpoint (buf + 12, buf[11], it->second.first->m_RemoteEndpoint))
|
2022-04-28 01:52:44 +03:00
|
|
|
{
|
|
|
|
it->second.first->m_State = eSSU2SessionStateUnknown;
|
|
|
|
it->second.first->Connect ();
|
|
|
|
}
|
2022-04-26 20:59:59 +03:00
|
|
|
}
|
|
|
|
m_RelaySessions.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint (eLogWarning, "SSU2: RelayResponse unknown nonce ", bufbe32toh (buf + 2));
|
|
|
|
}
|
2022-03-30 22:03:45 +03:00
|
|
|
|
2022-03-16 20:13:31 +03:00
|
|
|
bool SSU2Session::ExtractEndpoint (const uint8_t * buf, size_t size, boost::asio::ip::udp::endpoint& ep)
|
|
|
|
{
|
|
|
|
if (size < 2) return false;
|
|
|
|
int port = bufbe16toh (buf);
|
|
|
|
if (size == 6)
|
|
|
|
{
|
|
|
|
boost::asio::ip::address_v4::bytes_type bytes;
|
|
|
|
memcpy (bytes.data (), buf + 2, 4);
|
|
|
|
ep = boost::asio::ip::udp::endpoint (boost::asio::ip::address_v4 (bytes), port);
|
|
|
|
}
|
|
|
|
else if (size == 18)
|
|
|
|
{
|
|
|
|
boost::asio::ip::address_v6::bytes_type bytes;
|
|
|
|
memcpy (bytes.data (), buf + 2, 16);
|
|
|
|
ep = boost::asio::ip::udp::endpoint (boost::asio::ip::address_v6 (bytes), port);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: Address size ", int(size), " is not supported");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-23 03:34:19 +03:00
|
|
|
size_t SSU2Session::CreateEndpoint (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& ep)
|
2022-03-16 20:13:31 +03:00
|
|
|
{
|
2022-04-23 03:34:19 +03:00
|
|
|
if (len < 6) return 0;
|
|
|
|
htobe16buf (buf, ep.port ());
|
2022-03-16 20:13:31 +03:00
|
|
|
size_t size = 0;
|
|
|
|
if (ep.address ().is_v4 ())
|
|
|
|
{
|
2022-04-23 03:34:19 +03:00
|
|
|
memcpy (buf + 2, ep.address ().to_v4 ().to_bytes ().data (), 4);
|
2022-03-16 20:13:31 +03:00
|
|
|
size = 6;
|
|
|
|
}
|
|
|
|
else if (ep.address ().is_v6 ())
|
|
|
|
{
|
2022-04-23 03:34:19 +03:00
|
|
|
if (len < 18) return 0;
|
|
|
|
memcpy (buf + 2, ep.address ().to_v6 ().to_bytes ().data (), 16);
|
2022-03-16 20:13:31 +03:00
|
|
|
size = 18;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: Wrong address type ", ep.address ().to_string ());
|
|
|
|
return 0;
|
2022-04-23 03:34:19 +03:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SSU2Session::CreateAddressBlock (const boost::asio::ip::udp::endpoint& ep, uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
if (len < 9) return 0;
|
|
|
|
buf[0] = eSSU2BlkAddress;
|
|
|
|
size_t size = CreateEndpoint (buf + 3, len - 3, ep);
|
|
|
|
if (!size) return 0;
|
2022-03-16 20:13:31 +03:00
|
|
|
htobe16buf (buf + 1, size);
|
|
|
|
return size + 3;
|
|
|
|
}
|
2022-03-22 04:06:14 +03:00
|
|
|
|
2022-03-26 23:35:07 +03:00
|
|
|
size_t SSU2Session::CreateAckBlock (uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
if (len < 8) return 0;
|
|
|
|
buf[0] = eSSU2BlkAck;
|
2022-03-30 19:31:24 +03:00
|
|
|
uint32_t ackThrough = m_OutOfSequencePackets.empty () ? m_ReceivePacketNum : *m_OutOfSequencePackets.rbegin ();
|
|
|
|
htobe32buf (buf + 3, ackThrough); // Ack Through
|
|
|
|
uint8_t acnt = 0;
|
2022-04-15 23:26:44 +03:00
|
|
|
int numRanges = 0;
|
2022-03-30 19:31:24 +03:00
|
|
|
if (ackThrough)
|
2022-04-06 01:23:52 +03:00
|
|
|
{
|
|
|
|
if (m_OutOfSequencePackets.empty ())
|
|
|
|
acnt = std::min ((int)ackThrough, 255); // no gaps
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto it = m_OutOfSequencePackets.rbegin (); it++; // prev packet num
|
|
|
|
while (it != m_OutOfSequencePackets.rend () && *it == ackThrough - acnt - 1)
|
|
|
|
{
|
|
|
|
acnt++;
|
|
|
|
it++;
|
|
|
|
}
|
2022-04-15 23:26:44 +03:00
|
|
|
// ranges
|
|
|
|
uint32_t lastNum = ackThrough - acnt;
|
|
|
|
it++;
|
|
|
|
while (it != m_OutOfSequencePackets.rend () && lastNum > m_ReceivePacketNum && numRanges < 8)
|
|
|
|
{
|
|
|
|
if (lastNum - (*it) < 255)
|
|
|
|
{
|
|
|
|
buf[7 + numRanges*2] = lastNum - (*it); // NACKs
|
|
|
|
lastNum = *it;
|
|
|
|
uint8_t numAcks = 0;
|
|
|
|
while (it != m_OutOfSequencePackets.rend () && numAcks < 255 && lastNum > m_ReceivePacketNum && *it == lastNum - 1)
|
|
|
|
{
|
|
|
|
numAcks++; lastNum--;
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
buf[7 + numRanges*2 + 1] = numAcks; // Acks
|
|
|
|
numRanges++; it++;
|
|
|
|
if (numAcks == 255) break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2022-04-06 01:23:52 +03:00
|
|
|
}
|
|
|
|
}
|
2022-03-30 19:31:24 +03:00
|
|
|
buf[7] = acnt; // acnt
|
2022-04-15 23:26:44 +03:00
|
|
|
htobe16buf (buf + 1, 5 + numRanges*2);
|
2022-03-26 23:35:07 +03:00
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
2022-03-27 16:26:45 +03:00
|
|
|
size_t SSU2Session::CreatePaddingBlock (uint8_t * buf, size_t len, size_t minSize)
|
2022-03-26 23:35:07 +03:00
|
|
|
{
|
2022-03-27 16:26:45 +03:00
|
|
|
if (len < minSize) return 0;
|
2022-03-26 23:35:07 +03:00
|
|
|
uint8_t paddingSize = rand () & 0x0F; // 0 - 15
|
|
|
|
if (paddingSize > len) paddingSize = len;
|
2022-03-27 16:26:45 +03:00
|
|
|
else if (paddingSize < minSize) paddingSize = minSize;
|
2022-03-26 23:35:07 +03:00
|
|
|
if (paddingSize)
|
|
|
|
{
|
|
|
|
buf[0] = eSSU2BlkPadding;
|
|
|
|
htobe16buf (buf + 1, paddingSize);
|
|
|
|
memset (buf + 3, 0, paddingSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
return paddingSize + 3;
|
|
|
|
}
|
2022-03-31 01:04:12 +03:00
|
|
|
|
|
|
|
size_t SSU2Session::CreateI2NPBlock (uint8_t * buf, size_t len, std::shared_ptr<I2NPMessage>&& msg)
|
|
|
|
{
|
|
|
|
msg->ToNTCP2 ();
|
|
|
|
auto msgBuf = msg->GetNTCP2Header ();
|
|
|
|
auto msgLen = msg->GetNTCP2Length ();
|
|
|
|
if (msgLen + 3 > len) msgLen = len - 3;
|
|
|
|
buf[0] = eSSU2BlkI2NPMessage;
|
|
|
|
htobe16buf (buf + 1, msgLen); // size
|
|
|
|
memcpy (buf + 3, msgBuf, msgLen);
|
|
|
|
return msgLen + 3;
|
|
|
|
}
|
2022-04-09 18:42:34 +03:00
|
|
|
|
|
|
|
size_t SSU2Session::CreateFirstFragmentBlock (uint8_t * buf, size_t len, std::shared_ptr<I2NPMessage> msg)
|
|
|
|
{
|
|
|
|
if (len < 12) return 0;
|
|
|
|
msg->ToNTCP2 ();
|
|
|
|
auto msgBuf = msg->GetNTCP2Header ();
|
|
|
|
auto msgLen = msg->GetNTCP2Length ();
|
|
|
|
if (msgLen + 3 <= len) return 0;
|
|
|
|
msgLen = len - 3;
|
|
|
|
buf[0] = eSSU2BlkFirstFragment;
|
|
|
|
htobe16buf (buf + 1, msgLen); // size
|
|
|
|
memcpy (buf + 3, msgBuf, msgLen);
|
|
|
|
msg->offset = (msgBuf - buf) + msgLen;
|
|
|
|
return msgLen + 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SSU2Session::CreateFollowOnFragmentBlock (uint8_t * buf, size_t len, std::shared_ptr<I2NPMessage> msg, uint8_t& fragmentNum, uint32_t msgID)
|
|
|
|
{
|
|
|
|
if (len < 8) return 0;
|
|
|
|
bool isLast = true;
|
|
|
|
auto msgLen = msg->len - msg->offset;
|
|
|
|
if (msgLen + 8 > len)
|
|
|
|
{
|
|
|
|
msgLen = len - 8;
|
|
|
|
isLast = false;
|
|
|
|
}
|
|
|
|
buf[0] = eSSU2BlkFollowOnFragment;
|
|
|
|
htobe16buf (buf + 1, msgLen); // size
|
|
|
|
fragmentNum++;
|
|
|
|
buf[3] = fragmentNum << 1;
|
|
|
|
if (isLast) buf[3] |= 0x01;
|
|
|
|
memcpy (buf + 4, &msgID, 4);
|
|
|
|
memcpy (buf + 8, msg->buf + msg->offset, msgLen);
|
|
|
|
msg->offset += msgLen;
|
|
|
|
return msgLen + 8;
|
|
|
|
}
|
2022-04-21 22:47:36 +03:00
|
|
|
|
|
|
|
size_t SSU2Session::CreateRelayIntroBlock (uint8_t * buf, size_t len, const uint8_t * introData, size_t introDataLen)
|
|
|
|
{
|
|
|
|
buf[0] = eSSU2BlkRelayIntro;
|
|
|
|
size_t payloadSize = 1/* flag */ + 32/* Alice router hash */ + introDataLen;
|
|
|
|
if (payloadSize + 3 > len) return 0;
|
|
|
|
htobe16buf (buf + 1, payloadSize); // size
|
|
|
|
buf[3] = 0; // flag
|
|
|
|
memcpy (buf + 4, GetRemoteIdentity ()->GetIdentHash (), 32); // Alice router hash
|
|
|
|
memcpy (buf + 36, introData, introDataLen);
|
|
|
|
return payloadSize + 3;
|
|
|
|
}
|
2022-04-23 03:34:19 +03:00
|
|
|
|
2022-04-26 20:59:59 +03:00
|
|
|
size_t SSU2Session::CreateRelayResponseBlock (uint8_t * buf, size_t len, uint32_t nonce)
|
2022-04-23 03:34:19 +03:00
|
|
|
{
|
|
|
|
buf[0] = eSSU2BlkRelayResponse;
|
|
|
|
buf[3] = 0; // flag
|
|
|
|
buf[4] = 0; // code, accept
|
|
|
|
htobe32buf (buf + 5, nonce); // nonce
|
2022-04-26 20:59:59 +03:00
|
|
|
htobe32buf (buf + 9, i2p::util::GetSecondsSinceEpoch ()); // timestamp
|
|
|
|
buf[13] = 2; // ver
|
|
|
|
size_t csz = CreateEndpoint (buf + 15, len - 15, boost::asio::ip::udp::endpoint (m_Address->host, m_Address->port));
|
2022-04-23 03:34:19 +03:00
|
|
|
if (!csz) return 0;
|
2022-04-26 20:59:59 +03:00
|
|
|
buf[14] = csz; // csz
|
2022-04-23 03:34:19 +03:00
|
|
|
// signature
|
|
|
|
SignedData s;
|
|
|
|
s.Insert ((const uint8_t *)"RelayAgreementOK", 16); // prologue
|
|
|
|
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
2022-04-26 20:59:59 +03:00
|
|
|
s.Insert (buf + 5, 10 + csz); // nonce, timestamp, ver, csz and Charlie's endpoint
|
|
|
|
s.Sign (i2p::context.GetPrivateKeys (), buf + 15 + csz);
|
|
|
|
size_t payloadSize = 12 + csz + i2p::context.GetIdentity ()->GetSignatureLen ();
|
2022-04-23 03:34:19 +03:00
|
|
|
htobe16buf (buf + 1, payloadSize); // size
|
|
|
|
return payloadSize + 3;
|
|
|
|
}
|
2022-03-26 23:35:07 +03:00
|
|
|
|
2022-03-23 21:06:55 +03:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> SSU2Session::ExtractRouterInfo (const uint8_t * buf, size_t size)
|
|
|
|
{
|
|
|
|
if (size < 2) return nullptr;
|
|
|
|
// TODO: handle frag
|
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> ri;
|
|
|
|
if (buf[0] & SSU2_ROUTER_INFO_FLAG_GZIP)
|
|
|
|
{
|
|
|
|
i2p::data::GzipInflator inflator;
|
|
|
|
uint8_t uncompressed[i2p::data::MAX_RI_BUFFER_SIZE];
|
|
|
|
size_t uncompressedSize = inflator.Inflate (buf + 2, size - 2, uncompressed, i2p::data::MAX_RI_BUFFER_SIZE);
|
|
|
|
if (uncompressedSize && uncompressedSize < i2p::data::MAX_RI_BUFFER_SIZE)
|
|
|
|
ri = std::make_shared<i2p::data::RouterInfo>(uncompressed, uncompressedSize);
|
|
|
|
else
|
|
|
|
LogPrint (eLogInfo, "SSU2: RouterInfo decompression failed ", uncompressedSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ri = std::make_shared<i2p::data::RouterInfo>(buf + 2, size - 2);
|
|
|
|
return ri;
|
|
|
|
}
|
|
|
|
|
2022-03-22 04:06:14 +03:00
|
|
|
void SSU2Session::CreateNonce (uint64_t seqn, uint8_t * nonce)
|
|
|
|
{
|
|
|
|
memset (nonce, 0, 4);
|
|
|
|
htole64buf (nonce + 4, seqn);
|
|
|
|
}
|
2022-03-26 23:35:07 +03:00
|
|
|
|
2022-03-30 22:03:45 +03:00
|
|
|
bool SSU2Session::UpdateReceivePacketNum (uint32_t packetNum)
|
2022-03-30 19:31:24 +03:00
|
|
|
{
|
2022-03-30 22:03:45 +03:00
|
|
|
if (packetNum <= m_ReceivePacketNum) return false; // duplicate
|
2022-03-30 19:31:24 +03:00
|
|
|
if (packetNum == m_ReceivePacketNum + 1)
|
|
|
|
{
|
|
|
|
for (auto it = m_OutOfSequencePackets.begin (); it != m_OutOfSequencePackets.end ();)
|
|
|
|
{
|
|
|
|
if (*it == packetNum + 1)
|
|
|
|
{
|
|
|
|
packetNum++;
|
|
|
|
it = m_OutOfSequencePackets.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_ReceivePacketNum = packetNum;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_OutOfSequencePackets.insert (packetNum);
|
2022-03-30 22:03:45 +03:00
|
|
|
return true;
|
2022-03-30 19:31:24 +03:00
|
|
|
}
|
|
|
|
|
2022-03-26 23:35:07 +03:00
|
|
|
void SSU2Session::SendQuickAck ()
|
|
|
|
{
|
|
|
|
uint8_t payload[SSU2_MTU];
|
|
|
|
size_t payloadSize = CreateAckBlock (payload, SSU2_MTU);
|
|
|
|
payloadSize += CreatePaddingBlock (payload + payloadSize, SSU2_MTU - payloadSize);
|
|
|
|
SendData (payload, payloadSize);
|
|
|
|
}
|
2022-03-27 23:39:58 +03:00
|
|
|
|
|
|
|
void SSU2Session::SendTermination ()
|
|
|
|
{
|
|
|
|
uint8_t payload[32];
|
|
|
|
size_t payloadSize = 12;
|
|
|
|
payload[0] = eSSU2BlkTermination;
|
|
|
|
htobe16buf (payload + 1, 9);
|
|
|
|
memset (payload + 3, 0, 9);
|
|
|
|
payloadSize += CreatePaddingBlock (payload + payloadSize, 32 - payloadSize);
|
|
|
|
SendData (payload, payloadSize);
|
|
|
|
}
|
2022-04-02 20:05:11 +03:00
|
|
|
|
|
|
|
void SSU2Session::CleanUp (uint64_t ts)
|
|
|
|
{
|
|
|
|
for (auto it = m_IncompleteMessages.begin (); it != m_IncompleteMessages.end ();)
|
|
|
|
{
|
|
|
|
if (ts > it->second->lastFragmentInsertTime + SSU2_INCOMPLETE_MESSAGES_CLEANUP_TIMEOUT)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: message ", it->first, " was not completed in ", SSU2_INCOMPLETE_MESSAGES_CLEANUP_TIMEOUT, " seconds, deleted");
|
|
|
|
it = m_IncompleteMessages.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
2022-04-06 01:23:52 +03:00
|
|
|
if (m_OutOfSequencePackets.size () > 255)
|
|
|
|
{
|
|
|
|
m_ReceivePacketNum = *m_OutOfSequencePackets.rbegin ();
|
|
|
|
m_OutOfSequencePackets.clear ();
|
|
|
|
}
|
2022-04-26 20:59:59 +03:00
|
|
|
for (auto it = m_RelaySessions.begin (); it != m_RelaySessions.end ();)
|
|
|
|
{
|
|
|
|
if (ts > it->second.second + SSU2_RELAY_NONCE_EXPIRATION_TIMEOUT)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU2: noce ", it->first, " was not responded in ", SSU2_RELAY_NONCE_EXPIRATION_TIMEOUT, " seconds, deleted");
|
|
|
|
it = m_RelaySessions.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
2022-04-02 20:05:11 +03:00
|
|
|
}
|
2022-04-12 18:42:51 +03:00
|
|
|
|
|
|
|
void SSU2Session::FlushData ()
|
|
|
|
{
|
2022-04-18 22:47:35 +03:00
|
|
|
bool sent = SendQueue (); // if we have something to send
|
2022-04-12 18:42:51 +03:00
|
|
|
if (m_IsDataReceived)
|
|
|
|
{
|
2022-04-18 22:47:35 +03:00
|
|
|
if (!sent) SendQuickAck ();
|
2022-04-12 18:42:51 +03:00
|
|
|
m_Handler.Flush ();
|
|
|
|
m_IsDataReceived = false;
|
|
|
|
}
|
|
|
|
}
|
2022-03-16 03:49:41 +03:00
|
|
|
|
2022-03-12 00:17:44 +03:00
|
|
|
SSU2Server::SSU2Server ():
|
2022-04-05 23:27:52 +03:00
|
|
|
RunnableServiceWithWork ("SSU2"), m_ReceiveService ("SSU2r"),
|
|
|
|
m_SocketV4 (m_ReceiveService.GetService ()), m_SocketV6 (m_ReceiveService.GetService ()),
|
2022-03-31 22:35:55 +03:00
|
|
|
m_TerminationTimer (GetService ()), m_ResendTimer (GetService ())
|
2022-03-01 05:46:00 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-12 00:17:44 +03:00
|
|
|
void SSU2Server::Start ()
|
|
|
|
{
|
|
|
|
if (!IsRunning ())
|
|
|
|
{
|
|
|
|
StartIOService ();
|
2022-04-05 23:27:52 +03:00
|
|
|
bool found = false;
|
2022-03-12 00:17:44 +03:00
|
|
|
auto& addresses = i2p::context.GetRouterInfo ().GetAddresses ();
|
|
|
|
for (const auto& address: addresses)
|
|
|
|
{
|
|
|
|
if (!address) continue;
|
2022-03-14 04:34:11 +03:00
|
|
|
if (address->transportStyle == i2p::data::RouterInfo::eTransportSSU2)
|
2022-03-12 00:17:44 +03:00
|
|
|
{
|
2022-03-18 01:45:14 +03:00
|
|
|
auto port = address->port;
|
2022-03-14 04:34:11 +03:00
|
|
|
if (!port)
|
|
|
|
{
|
|
|
|
uint16_t ssu2Port; i2p::config::GetOption ("ssu2.port", ssu2Port);
|
|
|
|
if (ssu2Port) port = ssu2Port;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint16_t p; i2p::config::GetOption ("port", p);
|
|
|
|
if (p) port = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (port)
|
2022-03-15 02:25:59 +03:00
|
|
|
{
|
2022-03-18 01:45:14 +03:00
|
|
|
if (address->IsV4 ())
|
2022-04-05 23:14:13 +03:00
|
|
|
{
|
2022-04-05 23:27:52 +03:00
|
|
|
found = true;
|
2022-04-05 23:14:13 +03:00
|
|
|
OpenSocket (boost::asio::ip::udp::endpoint (boost::asio::ip::udp::v4(), port));
|
2022-04-05 23:27:52 +03:00
|
|
|
m_ReceiveService.GetService ().post(
|
2022-04-05 23:14:13 +03:00
|
|
|
[this]()
|
|
|
|
{
|
|
|
|
Receive (m_SocketV4);
|
|
|
|
});
|
|
|
|
}
|
2022-03-18 01:45:14 +03:00
|
|
|
if (address->IsV6 ())
|
2022-04-05 23:14:13 +03:00
|
|
|
{
|
2022-04-05 23:27:52 +03:00
|
|
|
found = true;
|
2022-04-05 23:14:13 +03:00
|
|
|
OpenSocket (boost::asio::ip::udp::endpoint (boost::asio::ip::udp::v6(), port));
|
2022-04-05 23:27:52 +03:00
|
|
|
m_ReceiveService.GetService ().post(
|
2022-04-05 23:14:13 +03:00
|
|
|
[this]()
|
|
|
|
{
|
|
|
|
Receive (m_SocketV6);
|
|
|
|
});
|
|
|
|
}
|
2022-03-15 02:25:59 +03:00
|
|
|
}
|
2022-03-14 04:34:11 +03:00
|
|
|
else
|
2022-03-18 01:45:14 +03:00
|
|
|
LogPrint (eLogError, "SSU2: Can't start server because port not specified");
|
2022-03-12 00:17:44 +03:00
|
|
|
}
|
2022-03-19 03:21:31 +03:00
|
|
|
}
|
2022-04-05 23:27:52 +03:00
|
|
|
if (found)
|
|
|
|
m_ReceiveService.Start ();
|
2022-03-19 03:21:31 +03:00
|
|
|
ScheduleTermination ();
|
2022-03-12 00:17:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Server::Stop ()
|
|
|
|
{
|
2022-04-05 23:27:52 +03:00
|
|
|
if (context.SupportsV4 () || context.SupportsV6 ())
|
|
|
|
m_ReceiveService.Stop ();
|
2022-04-05 23:14:13 +03:00
|
|
|
|
2022-03-19 03:21:31 +03:00
|
|
|
if (IsRunning ())
|
|
|
|
m_TerminationTimer.cancel ();
|
|
|
|
|
2022-03-12 00:17:44 +03:00
|
|
|
StopIOService ();
|
|
|
|
}
|
2022-03-15 02:25:59 +03:00
|
|
|
|
2022-03-18 01:45:14 +03:00
|
|
|
boost::asio::ip::udp::socket& SSU2Server::OpenSocket (const boost::asio::ip::udp::endpoint& localEndpoint)
|
2022-03-01 05:46:00 +03:00
|
|
|
{
|
2022-04-05 23:14:13 +03:00
|
|
|
boost::asio::ip::udp::socket& socket = localEndpoint.address ().is_v6 () ? m_SocketV6 : m_SocketV4;
|
2022-03-01 05:46:00 +03:00
|
|
|
try
|
|
|
|
{
|
2022-03-18 01:45:14 +03:00
|
|
|
socket.open (localEndpoint.protocol ());
|
|
|
|
if (localEndpoint.address ().is_v6 ())
|
|
|
|
socket.set_option (boost::asio::ip::v6_only (true));
|
|
|
|
socket.set_option (boost::asio::socket_base::receive_buffer_size (SSU2_SOCKET_RECEIVE_BUFFER_SIZE));
|
|
|
|
socket.set_option (boost::asio::socket_base::send_buffer_size (SSU2_SOCKET_SEND_BUFFER_SIZE));
|
|
|
|
socket.bind (localEndpoint);
|
|
|
|
LogPrint (eLogInfo, "SSU2: Start listening on ", localEndpoint);
|
2022-03-01 05:46:00 +03:00
|
|
|
}
|
|
|
|
catch (std::exception& ex )
|
|
|
|
{
|
2022-03-18 01:45:14 +03:00
|
|
|
LogPrint (eLogError, "SSU2: Failed to bind to ", localEndpoint, ": ", ex.what());
|
|
|
|
ThrowFatal ("Unable to start SSU2 transport on ", localEndpoint, ": ", ex.what ());
|
2022-03-01 05:46:00 +03:00
|
|
|
}
|
2022-03-18 01:45:14 +03:00
|
|
|
return socket;
|
2022-03-01 05:46:00 +03:00
|
|
|
}
|
2022-03-18 01:45:14 +03:00
|
|
|
|
|
|
|
void SSU2Server::Receive (boost::asio::ip::udp::socket& socket)
|
2022-03-15 02:25:59 +03:00
|
|
|
{
|
|
|
|
Packet * packet = m_PacketsPool.AcquireMt ();
|
2022-03-18 01:45:14 +03:00
|
|
|
socket.async_receive_from (boost::asio::buffer (packet->buf, SSU2_MTU), packet->from,
|
|
|
|
std::bind (&SSU2Server::HandleReceivedFrom, this, std::placeholders::_1, std::placeholders::_2, packet, std::ref (socket)));
|
2022-03-15 02:25:59 +03:00
|
|
|
}
|
|
|
|
|
2022-03-18 01:45:14 +03:00
|
|
|
void SSU2Server::HandleReceivedFrom (const boost::system::error_code& ecode, size_t bytes_transferred,
|
|
|
|
Packet * packet, boost::asio::ip::udp::socket& socket)
|
2022-03-15 02:25:59 +03:00
|
|
|
{
|
|
|
|
if (!ecode)
|
|
|
|
{
|
2022-03-26 23:35:07 +03:00
|
|
|
i2p::transport::transports.UpdateReceivedBytes (bytes_transferred);
|
2022-03-15 02:25:59 +03:00
|
|
|
packet->len = bytes_transferred;
|
2022-04-12 18:42:51 +03:00
|
|
|
|
|
|
|
boost::system::error_code ec;
|
|
|
|
size_t moreBytes = socket.available (ec);
|
2022-04-13 19:33:59 +03:00
|
|
|
if (!ec && moreBytes)
|
2022-04-12 18:42:51 +03:00
|
|
|
{
|
2022-04-13 19:33:59 +03:00
|
|
|
std::vector<Packet *> packets;
|
|
|
|
packets.push_back (packet);
|
2022-04-12 18:42:51 +03:00
|
|
|
while (moreBytes && packets.size () < 32)
|
|
|
|
{
|
|
|
|
packet = m_PacketsPool.AcquireMt ();
|
|
|
|
packet->len = socket.receive_from (boost::asio::buffer (packet->buf, SSU2_MTU), packet->from, 0, ec);
|
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
i2p::transport::transports.UpdateReceivedBytes (packet->len);
|
|
|
|
packets.push_back (packet);
|
|
|
|
moreBytes = socket.available(ec);
|
|
|
|
if (ec) break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU2: receive_from error: code ", ec.value(), ": ", ec.message ());
|
|
|
|
m_PacketsPool.ReleaseMt (packet);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 19:33:59 +03:00
|
|
|
GetService ().post (std::bind (&SSU2Server::HandleReceivedPackets, this, packets));
|
2022-04-12 18:42:51 +03:00
|
|
|
}
|
2022-04-13 19:33:59 +03:00
|
|
|
else
|
|
|
|
GetService ().post (std::bind (&SSU2Server::HandleReceivedPacket, this, packet));
|
2022-03-18 01:45:14 +03:00
|
|
|
Receive (socket);
|
2022-03-15 02:25:59 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_PacketsPool.ReleaseMt (packet);
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU2: Receive error: code ", ecode.value(), ": ", ecode.message ());
|
2022-03-18 01:45:14 +03:00
|
|
|
auto ep = socket.local_endpoint ();
|
|
|
|
socket.close ();
|
|
|
|
OpenSocket (ep);
|
|
|
|
Receive (socket);
|
2022-03-15 02:25:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-05 23:14:13 +03:00
|
|
|
|
2022-04-13 19:33:59 +03:00
|
|
|
void SSU2Server::HandleReceivedPacket (Packet * packet)
|
|
|
|
{
|
|
|
|
if (packet)
|
|
|
|
{
|
|
|
|
ProcessNextPacket (packet->buf, packet->len, packet->from);
|
|
|
|
m_PacketsPool.ReleaseMt (packet);
|
|
|
|
if (m_LastSession) m_LastSession->FlushData ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Server::HandleReceivedPackets (std::vector<Packet *> packets)
|
2022-04-05 23:14:13 +03:00
|
|
|
{
|
2022-04-12 18:42:51 +03:00
|
|
|
for (auto& packet: packets)
|
2022-04-05 23:14:13 +03:00
|
|
|
ProcessNextPacket (packet->buf, packet->len, packet->from);
|
2022-04-12 18:42:51 +03:00
|
|
|
m_PacketsPool.ReleaseMt (packets);
|
|
|
|
if (m_LastSession) m_LastSession->FlushData ();
|
2022-04-05 23:14:13 +03:00
|
|
|
}
|
2022-03-01 05:46:00 +03:00
|
|
|
|
2022-03-27 23:39:58 +03:00
|
|
|
void SSU2Server::AddSession (std::shared_ptr<SSU2Session> session)
|
2022-02-28 04:15:14 +03:00
|
|
|
{
|
2022-03-27 23:39:58 +03:00
|
|
|
if (session)
|
2022-04-28 20:11:51 +03:00
|
|
|
{
|
2022-03-27 23:39:58 +03:00
|
|
|
m_Sessions.emplace (session->GetConnID (), session);
|
2022-04-28 20:11:51 +03:00
|
|
|
AddSessionByRouterHash (session);
|
|
|
|
}
|
2022-02-28 04:15:14 +03:00
|
|
|
}
|
|
|
|
|
2022-03-27 23:39:58 +03:00
|
|
|
void SSU2Server::RemoveSession (uint64_t connID)
|
2022-03-01 05:46:00 +03:00
|
|
|
{
|
2022-04-28 20:11:51 +03:00
|
|
|
auto it = m_Sessions.find (connID);
|
|
|
|
if (it != m_Sessions.end ())
|
|
|
|
{
|
|
|
|
auto ident = it->second->GetRemoteIdentity ();
|
|
|
|
if (ident)
|
|
|
|
m_SessionsByRouterHash.erase (ident->GetIdentHash ());
|
|
|
|
m_Sessions.erase (it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Server::AddSessionByRouterHash (std::shared_ptr<SSU2Session> session)
|
|
|
|
{
|
|
|
|
if (session)
|
|
|
|
{
|
|
|
|
auto ident = session->GetRemoteIdentity ();
|
|
|
|
if (ident)
|
|
|
|
{
|
|
|
|
auto ret = m_SessionsByRouterHash.emplace (ident->GetIdentHash (), session);
|
|
|
|
if (!ret.second)
|
|
|
|
{
|
|
|
|
// session already exists
|
|
|
|
LogPrint (eLogWarning, "SSU2: Session to ", ident->GetIdentHash ().ToBase64 (), " aready exists");
|
|
|
|
// terminate existing
|
|
|
|
GetService ().post (std::bind (&SSU2Session::Terminate, ret.first->second));
|
|
|
|
// update session
|
|
|
|
ret.first->second = session;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-27 23:39:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Server::AddPendingOutgoingSession (std::shared_ptr<SSU2Session> session)
|
|
|
|
{
|
|
|
|
if (session)
|
|
|
|
m_PendingOutgoingSessions.emplace (session->GetRemoteEndpoint (), session);
|
2022-03-01 05:46:00 +03:00
|
|
|
}
|
2022-04-21 22:47:36 +03:00
|
|
|
|
|
|
|
void SSU2Server::AddRelay (uint32_t tag, std::shared_ptr<SSU2Session> relay)
|
|
|
|
{
|
|
|
|
m_Relays.emplace (tag, relay);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Server::RemoveRelay (uint32_t tag)
|
|
|
|
{
|
|
|
|
m_Relays.erase (tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<SSU2Session> SSU2Server::FindRelaySession (uint32_t tag)
|
|
|
|
{
|
|
|
|
auto it = m_Relays.find (tag);
|
|
|
|
if (it != m_Relays.end ())
|
|
|
|
{
|
|
|
|
if (it->second->IsEstablished ())
|
|
|
|
return it->second;
|
|
|
|
else
|
|
|
|
m_Relays.erase (it);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-03-01 05:46:00 +03:00
|
|
|
|
|
|
|
void SSU2Server::ProcessNextPacket (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
|
2022-02-28 04:15:14 +03:00
|
|
|
{
|
2022-03-05 05:51:40 +03:00
|
|
|
uint64_t connID;
|
2022-02-28 04:15:14 +03:00
|
|
|
memcpy (&connID, buf, 8);
|
2022-03-05 05:51:40 +03:00
|
|
|
connID ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), buf + (len - 24));
|
2022-04-07 17:57:57 +03:00
|
|
|
if (!m_LastSession || m_LastSession->GetConnID () != connID)
|
2022-02-28 04:15:14 +03:00
|
|
|
{
|
2022-04-12 18:42:51 +03:00
|
|
|
if (m_LastSession) m_LastSession->FlushData ();
|
2022-04-07 17:57:57 +03:00
|
|
|
auto it = m_Sessions.find (connID);
|
|
|
|
if (it != m_Sessions.end ())
|
|
|
|
m_LastSession = it->second;
|
|
|
|
else
|
|
|
|
m_LastSession = nullptr;
|
|
|
|
}
|
|
|
|
if (m_LastSession)
|
|
|
|
{
|
|
|
|
if (m_LastSession->IsEstablished ())
|
|
|
|
m_LastSession->ProcessData (buf, len);
|
2022-03-25 22:34:43 +03:00
|
|
|
else
|
2022-04-07 17:57:57 +03:00
|
|
|
m_LastSession->ProcessSessionConfirmed (buf, len);
|
2022-02-28 04:15:14 +03:00
|
|
|
}
|
2022-03-01 05:46:00 +03:00
|
|
|
else
|
|
|
|
{
|
2022-03-20 22:10:18 +03:00
|
|
|
// check pending sessions if it's SessionCreated or Retry
|
2022-03-01 05:46:00 +03:00
|
|
|
auto it1 = m_PendingOutgoingSessions.find (senderEndpoint);
|
|
|
|
if (it1 != m_PendingOutgoingSessions.end ())
|
|
|
|
{
|
2022-03-21 19:56:02 +03:00
|
|
|
if (it1->second->ProcessSessionCreated (buf, len))
|
|
|
|
m_PendingOutgoingSessions.erase (it1); // we are done with that endpoint
|
|
|
|
else
|
|
|
|
it1->second->ProcessRetry (buf, len);
|
2022-03-01 05:46:00 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// assume new incoming session
|
2022-03-06 02:39:27 +03:00
|
|
|
auto session = std::make_shared<SSU2Session> (*this);
|
2022-03-09 05:33:21 +03:00
|
|
|
session->SetRemoteEndpoint (senderEndpoint);
|
2022-03-24 04:48:41 +03:00
|
|
|
session->ProcessFirstIncomingMessage (connID, buf, len);
|
2022-03-01 05:46:00 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-28 04:15:14 +03:00
|
|
|
}
|
2022-03-01 05:46:00 +03:00
|
|
|
|
2022-03-26 23:35:07 +03:00
|
|
|
void SSU2Server::Send (const uint8_t * header, size_t headerLen, const uint8_t * payload, size_t payloadLen,
|
|
|
|
const boost::asio::ip::udp::endpoint& to)
|
|
|
|
{
|
|
|
|
std::vector<boost::asio::const_buffer> bufs
|
|
|
|
{
|
|
|
|
boost::asio::buffer (header, headerLen),
|
|
|
|
boost::asio::buffer (payload, payloadLen)
|
|
|
|
};
|
|
|
|
boost::system::error_code ec;
|
|
|
|
if (to.address ().is_v6 ())
|
|
|
|
m_SocketV6.send_to (bufs, to, 0, ec);
|
|
|
|
else
|
2022-04-05 23:14:13 +03:00
|
|
|
m_SocketV4.send_to (bufs, to, 0, ec);
|
2022-04-22 22:03:49 +03:00
|
|
|
if (!ec)
|
|
|
|
i2p::transport::transports.UpdateSentBytes (headerLen + payloadLen);
|
|
|
|
else
|
|
|
|
LogPrint (eLogError, "SSU2: Send exception: ", ec.message (), " to ", to);
|
2022-03-26 23:35:07 +03:00
|
|
|
}
|
|
|
|
|
2022-03-01 05:46:00 +03:00
|
|
|
void SSU2Server::Send (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen,
|
|
|
|
const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to)
|
|
|
|
{
|
|
|
|
std::vector<boost::asio::const_buffer> bufs
|
|
|
|
{
|
|
|
|
boost::asio::buffer (header, headerLen),
|
|
|
|
boost::asio::buffer (headerX, headerXLen),
|
|
|
|
boost::asio::buffer (payload, payloadLen)
|
|
|
|
};
|
|
|
|
boost::system::error_code ec;
|
2022-03-18 22:32:32 +03:00
|
|
|
if (to.address ().is_v6 ())
|
|
|
|
m_SocketV6.send_to (bufs, to, 0, ec);
|
|
|
|
else
|
2022-04-05 23:14:13 +03:00
|
|
|
m_SocketV4.send_to (bufs, to, 0, ec);
|
2022-04-22 22:03:49 +03:00
|
|
|
|
|
|
|
if (!ec)
|
|
|
|
i2p::transport::transports.UpdateSentBytes (headerLen + headerXLen + payloadLen);
|
|
|
|
else
|
|
|
|
LogPrint (eLogError, "SSU2: Send exception: ", ec.message (), " to ", to);
|
2022-03-01 05:46:00 +03:00
|
|
|
}
|
2022-03-17 04:11:48 +03:00
|
|
|
|
2022-04-22 22:03:49 +03:00
|
|
|
void SSU2Server::SendHolePunch (const boost::asio::ip::udp::endpoint& to)
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
if (to.address ().is_v6 ())
|
|
|
|
m_SocketV6.send_to (boost::asio::buffer ((uint8_t *)nullptr, 0), to, 0, ec);
|
|
|
|
else
|
|
|
|
m_SocketV4.send_to (boost::asio::buffer ((uint8_t *)nullptr, 0), to, 0, ec);
|
|
|
|
|
|
|
|
if (ec)
|
|
|
|
LogPrint (eLogError, "SSU2: Send exception: ", ec.message (), " to ", to);
|
|
|
|
}
|
|
|
|
|
2022-03-17 04:11:48 +03:00
|
|
|
bool SSU2Server::CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router,
|
|
|
|
std::shared_ptr<const i2p::data::RouterInfo::Address> address)
|
|
|
|
{
|
|
|
|
if (router && address)
|
2022-04-28 18:43:33 +03:00
|
|
|
{
|
|
|
|
if (address->UsesIntroducer ()) return false; // not implemented yet
|
2022-03-17 04:11:48 +03:00
|
|
|
GetService ().post (
|
|
|
|
[this, router, address]()
|
|
|
|
{
|
|
|
|
auto session = std::make_shared<SSU2Session> (*this, router, address);
|
|
|
|
session->Connect ();
|
2022-04-28 18:43:33 +03:00
|
|
|
});
|
|
|
|
}
|
2022-03-17 04:11:48 +03:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-19 03:21:31 +03:00
|
|
|
|
|
|
|
void SSU2Server::ScheduleTermination ()
|
|
|
|
{
|
|
|
|
m_TerminationTimer.expires_from_now (boost::posix_time::seconds(SSU2_TERMINATION_CHECK_TIMEOUT));
|
|
|
|
m_TerminationTimer.async_wait (std::bind (&SSU2Server::HandleTerminationTimer,
|
|
|
|
this, std::placeholders::_1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Server::HandleTerminationTimer (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
for (auto it = m_PendingOutgoingSessions.begin (); it != m_PendingOutgoingSessions.end ();)
|
|
|
|
{
|
|
|
|
if (it->second->IsTerminationTimeoutExpired (ts))
|
|
|
|
{
|
|
|
|
//it->second->Terminate ();
|
|
|
|
it = m_PendingOutgoingSessions.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto it = m_Sessions.begin (); it != m_Sessions.end ();)
|
|
|
|
{
|
|
|
|
if (it->second->IsTerminationTimeoutExpired (ts))
|
|
|
|
{
|
2022-03-27 23:39:58 +03:00
|
|
|
if (it->second->IsEstablished ())
|
|
|
|
it->second->TerminateByTimeout ();
|
2022-04-07 17:57:57 +03:00
|
|
|
if (it->second == m_LastSession)
|
|
|
|
m_LastSession = nullptr;
|
2022-03-19 03:21:31 +03:00
|
|
|
it = m_Sessions.erase (it);
|
|
|
|
}
|
|
|
|
else
|
2022-04-02 20:05:11 +03:00
|
|
|
{
|
|
|
|
it->second->CleanUp (ts);
|
2022-03-19 03:21:31 +03:00
|
|
|
it++;
|
2022-04-02 20:05:11 +03:00
|
|
|
}
|
2022-03-19 03:21:31 +03:00
|
|
|
}
|
2022-03-24 02:13:44 +03:00
|
|
|
|
|
|
|
for (auto it = m_IncomingTokens.begin (); it != m_IncomingTokens.end (); )
|
|
|
|
{
|
|
|
|
if (ts > it->second.second)
|
|
|
|
it = m_IncomingTokens.erase (it);
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto it = m_OutgoingTokens.begin (); it != m_OutgoingTokens.end (); )
|
|
|
|
{
|
|
|
|
if (ts > it->second.second)
|
|
|
|
it = m_OutgoingTokens.erase (it);
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
2022-03-19 03:21:31 +03:00
|
|
|
|
|
|
|
ScheduleTermination ();
|
|
|
|
}
|
2022-03-24 02:13:44 +03:00
|
|
|
}
|
|
|
|
|
2022-03-31 22:35:55 +03:00
|
|
|
void SSU2Server::ScheduleResend ()
|
|
|
|
{
|
|
|
|
m_ResendTimer.expires_from_now (boost::posix_time::seconds(SSU2_RESEND_INTERVAL));
|
|
|
|
m_ResendTimer.async_wait (std::bind (&SSU2Server::HandleResendTimer,
|
|
|
|
this, std::placeholders::_1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSU2Server::HandleResendTimer (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
for (auto it: m_Sessions)
|
|
|
|
it.second->Resend (ts);
|
|
|
|
ScheduleResend ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 02:13:44 +03:00
|
|
|
void SSU2Server::UpdateOutgoingToken (const boost::asio::ip::udp::endpoint& ep, uint64_t token, uint32_t exp)
|
|
|
|
{
|
|
|
|
m_OutgoingTokens[ep] = {token, exp};
|
|
|
|
}
|
|
|
|
|
2022-03-24 04:48:41 +03:00
|
|
|
uint64_t SSU2Server::FindOutgoingToken (const boost::asio::ip::udp::endpoint& ep) const
|
2022-03-24 02:13:44 +03:00
|
|
|
{
|
|
|
|
auto it = m_OutgoingTokens.find (ep);
|
|
|
|
if (it != m_OutgoingTokens.end ())
|
|
|
|
return it->second.first;
|
|
|
|
return 0;
|
2022-03-19 03:21:31 +03:00
|
|
|
}
|
2022-03-24 04:48:41 +03:00
|
|
|
|
|
|
|
uint64_t SSU2Server::GetIncomingToken (const boost::asio::ip::udp::endpoint& ep)
|
|
|
|
{
|
|
|
|
auto it = m_IncomingTokens.find (ep);
|
|
|
|
if (it != m_IncomingTokens.end ())
|
|
|
|
return it->second.first;
|
|
|
|
uint64_t token;
|
|
|
|
RAND_bytes ((uint8_t *)&token, 8);
|
|
|
|
m_IncomingTokens.emplace (ep, std::make_pair (token, i2p::util::GetSecondsSinceEpoch () + SSU2_TOKEN_EXPIRATION_TIMEOUT));
|
|
|
|
return token;
|
|
|
|
}
|
2022-02-04 23:01:18 +03:00
|
|
|
}
|
|
|
|
}
|