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.
1201 lines
39 KiB
1201 lines
39 KiB
10 years ago
|
#include <boost/bind.hpp>
|
||
9 years ago
|
#include "Crypto.h"
|
||
10 years ago
|
#include "Log.h"
|
||
|
#include "Timestamp.h"
|
||
|
#include "RouterContext.h"
|
||
|
#include "Transports.h"
|
||
8 years ago
|
#include "NetDb.h"
|
||
10 years ago
|
#include "SSU.h"
|
||
|
#include "SSUSession.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace transport
|
||
|
{
|
||
|
SSUSession::SSUSession (SSUServer& server, boost::asio::ip::udp::endpoint& remoteEndpoint,
|
||
8 years ago
|
std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest ):
|
||
|
TransportSession (router, SSU_TERMINATION_TIMEOUT),
|
||
8 years ago
|
m_Server (server), m_RemoteEndpoint (remoteEndpoint), m_ConnectTimer (GetService ()),
|
||
9 years ago
|
m_IsPeerTest (peerTest),m_State (eSessionStateUnknown), m_IsSessionKey (false),
|
||
8 years ago
|
m_RelayTag (0), m_SentRelayTag (0), m_Data (*this), m_IsDataReceived (false)
|
||
9 years ago
|
{
|
||
|
if (router)
|
||
|
{
|
||
|
// we are client
|
||
9 years ago
|
auto address = router->GetSSUAddress (false);
|
||
8 years ago
|
if (address) m_IntroKey = address->ssu->key;
|
||
9 years ago
|
m_Data.AdjustPacketSize (router); // mtu
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// we are server
|
||
9 years ago
|
auto address = i2p::context.GetRouterInfo ().GetSSUAddress (false);
|
||
8 years ago
|
if (address) m_IntroKey = address->ssu->key;
|
||
9 years ago
|
}
|
||
10 years ago
|
m_CreationTime = i2p::util::GetSecondsSinceEpoch ();
|
||
|
}
|
||
|
|
||
|
SSUSession::~SSUSession ()
|
||
|
{
|
||
|
}
|
||
10 years ago
|
|
||
|
boost::asio::io_service& SSUSession::GetService ()
|
||
|
{
|
||
|
return IsV6 () ? m_Server.GetServiceV6 () : m_Server.GetService ();
|
||
|
}
|
||
10 years ago
|
|
||
|
void SSUSession::CreateAESandMacKey (const uint8_t * pubKey)
|
||
|
{
|
||
|
uint8_t sharedKey[256];
|
||
9 years ago
|
m_DHKeysPair->Agree (pubKey, sharedKey);
|
||
10 years ago
|
|
||
10 years ago
|
uint8_t * sessionKey = m_SessionKey, * macKey = m_MacKey;
|
||
10 years ago
|
if (sharedKey[0] & 0x80)
|
||
|
{
|
||
10 years ago
|
sessionKey[0] = 0;
|
||
|
memcpy (sessionKey + 1, sharedKey, 31);
|
||
|
memcpy (macKey, sharedKey + 31, 32);
|
||
10 years ago
|
}
|
||
|
else if (sharedKey[0])
|
||
|
{
|
||
10 years ago
|
memcpy (sessionKey, sharedKey, 32);
|
||
|
memcpy (macKey, sharedKey + 32, 32);
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
// find first non-zero byte
|
||
|
uint8_t * nonZero = sharedKey + 1;
|
||
|
while (!*nonZero)
|
||
|
{
|
||
|
nonZero++;
|
||
|
if (nonZero - sharedKey > 32)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: first 32 bytes of shared key is all zeros. Ignored");
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
memcpy (sessionKey, nonZero, 32);
|
||
9 years ago
|
SHA256(nonZero, 64 - (nonZero - sharedKey), macKey);
|
||
10 years ago
|
}
|
||
|
m_IsSessionKey = true;
|
||
|
m_SessionKeyEncryption.SetKey (m_SessionKey);
|
||
|
m_SessionKeyDecryption.SetKey (m_SessionKey);
|
||
|
}
|
||
|
|
||
|
void SSUSession::ProcessNextMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
|
||
|
{
|
||
|
m_NumReceivedBytes += len;
|
||
10 years ago
|
i2p::transport::transports.UpdateReceivedBytes (len);
|
||
10 years ago
|
if (m_State == eSessionStateIntroduced)
|
||
|
{
|
||
|
// HolePunch received
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: HolePunch of ", len, " bytes received");
|
||
10 years ago
|
m_State = eSessionStateUnknown;
|
||
|
Connect ();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (!len) return; // ignore zero-length packets
|
||
|
if (m_State == eSessionStateEstablished)
|
||
8 years ago
|
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch ();
|
||
10 years ago
|
|
||
|
if (m_IsSessionKey && Validate (buf, len, m_MacKey)) // try session key first
|
||
|
DecryptSessionKey (buf, len);
|
||
|
else
|
||
|
{
|
||
8 years ago
|
if (m_State == eSessionStateEstablished) Reset (); // new session key required
|
||
10 years ago
|
// try intro key depending on side
|
||
9 years ago
|
if (Validate (buf, len, m_IntroKey))
|
||
|
Decrypt (buf, len, m_IntroKey);
|
||
10 years ago
|
else
|
||
|
{
|
||
|
// try own intro key
|
||
9 years ago
|
auto address = i2p::context.GetRouterInfo ().GetSSUAddress (false);
|
||
10 years ago
|
if (!address)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogInfo, "SSU is not supported");
|
||
10 years ago
|
return;
|
||
|
}
|
||
8 years ago
|
if (Validate (buf, len, address->ssu->key))
|
||
|
Decrypt (buf, len, address->ssu->key);
|
||
10 years ago
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: MAC verification failed ", len, " bytes from ", senderEndpoint);
|
||
10 years ago
|
m_Server.DeleteSession (shared_from_this ());
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// successfully decrypted
|
||
|
ProcessMessage (buf, len, senderEndpoint);
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
size_t SSUSession::GetSSUHeaderSize (const uint8_t * buf) const
|
||
9 years ago
|
{
|
||
|
size_t s = sizeof (SSUHeader);
|
||
9 years ago
|
if (((const SSUHeader *)buf)->IsExtendedOptions ())
|
||
9 years ago
|
s += buf[s] + 1; // byte right after header is extended options length
|
||
|
return s;
|
||
|
}
|
||
|
|
||
10 years ago
|
void SSUSession::ProcessMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
|
||
|
{
|
||
10 years ago
|
len -= (len & 0x0F); // %16, delete extra padding
|
||
|
if (len <= sizeof (SSUHeader)) return; // drop empty message
|
||
10 years ago
|
//TODO: since we are accessing a uint8_t this is unlikely to crash due to alignment but should be improved
|
||
9 years ago
|
auto headerSize = GetSSUHeaderSize (buf);
|
||
9 years ago
|
if (headerSize >= len)
|
||
|
{
|
||
|
LogPrint (eLogError, "SSU header size ", headerSize, " exceeds packet length ", len);
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
SSUHeader * header = (SSUHeader *)buf;
|
||
|
switch (header->GetPayloadType ())
|
||
|
{
|
||
|
case PAYLOAD_TYPE_DATA:
|
||
9 years ago
|
ProcessData (buf + headerSize, len - headerSize);
|
||
10 years ago
|
break;
|
||
|
case PAYLOAD_TYPE_SESSION_REQUEST:
|
||
9 years ago
|
ProcessSessionRequest (buf, len, senderEndpoint); // buf with header
|
||
10 years ago
|
break;
|
||
|
case PAYLOAD_TYPE_SESSION_CREATED:
|
||
9 years ago
|
ProcessSessionCreated (buf, len); // buf with header
|
||
10 years ago
|
break;
|
||
|
case PAYLOAD_TYPE_SESSION_CONFIRMED:
|
||
9 years ago
|
ProcessSessionConfirmed (buf, len); // buf with header
|
||
10 years ago
|
break;
|
||
|
case PAYLOAD_TYPE_PEER_TEST:
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: peer test received");
|
||
9 years ago
|
ProcessPeerTest (buf + headerSize, len - headerSize, senderEndpoint);
|
||
10 years ago
|
break;
|
||
|
case PAYLOAD_TYPE_SESSION_DESTROYED:
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: session destroy received");
|
||
10 years ago
|
m_Server.DeleteSession (shared_from_this ());
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
case PAYLOAD_TYPE_RELAY_RESPONSE:
|
||
9 years ago
|
ProcessRelayResponse (buf + headerSize, len - headerSize);
|
||
10 years ago
|
if (m_State != eSessionStateEstablished)
|
||
10 years ago
|
m_Server.DeleteSession (shared_from_this ());
|
||
10 years ago
|
break;
|
||
|
case PAYLOAD_TYPE_RELAY_REQUEST:
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: relay request received");
|
||
9 years ago
|
ProcessRelayRequest (buf + headerSize, len - headerSize, senderEndpoint);
|
||
10 years ago
|
break;
|
||
|
case PAYLOAD_TYPE_RELAY_INTRO:
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: relay intro received");
|
||
9 years ago
|
ProcessRelayIntro (buf + headerSize, len - headerSize);
|
||
10 years ago
|
break;
|
||
|
default:
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: Unexpected payload type ", (int)header->GetPayloadType ());
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void SSUSession::ProcessSessionRequest (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU message: session request");
|
||
9 years ago
|
bool sendRelayTag = true;
|
||
|
auto headerSize = sizeof (SSUHeader);
|
||
|
if (((SSUHeader *)buf)->IsExtendedOptions ())
|
||
|
{
|
||
|
uint8_t extendedOptionsLen = buf[headerSize];
|
||
|
headerSize++;
|
||
|
if (extendedOptionsLen >= 3) // options are presented
|
||
|
{
|
||
|
uint16_t flags = bufbe16toh (buf + headerSize);
|
||
|
sendRelayTag = flags & EXTENDED_OPTIONS_FLAG_REQUEST_RELAY_TAG;
|
||
|
}
|
||
|
headerSize += extendedOptionsLen;
|
||
|
}
|
||
|
if (headerSize >= len)
|
||
|
{
|
||
|
LogPrint (eLogError, "Session reaquest header size ", headerSize, " exceeds packet length ", len);
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
m_RemoteEndpoint = senderEndpoint;
|
||
|
if (!m_DHKeysPair)
|
||
|
m_DHKeysPair = transports.GetNextDHKeysPair ();
|
||
9 years ago
|
CreateAESandMacKey (buf + headerSize);
|
||
|
SendSessionCreated (buf + headerSize, sendRelayTag);
|
||
10 years ago
|
}
|
||
|
|
||
|
void SSUSession::ProcessSessionCreated (uint8_t * buf, size_t len)
|
||
|
{
|
||
9 years ago
|
if (!IsOutgoing () || !m_DHKeysPair)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: Unsolicited session created message");
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU message: session created");
|
||
8 years ago
|
m_ConnectTimer.cancel (); // connect timer
|
||
10 years ago
|
SignedData s; // x,y, our IP, our port, remote IP, remote port, relayTag, signed on time
|
||
9 years ago
|
auto headerSize = GetSSUHeaderSize (buf);
|
||
|
if (headerSize >= len)
|
||
|
{
|
||
|
LogPrint (eLogError, "Session created header size ", headerSize, " exceeds packet length ", len);
|
||
|
return;
|
||
|
}
|
||
|
uint8_t * payload = buf + headerSize;
|
||
10 years ago
|
uint8_t * y = payload;
|
||
|
CreateAESandMacKey (y);
|
||
9 years ago
|
s.Insert (m_DHKeysPair->GetPublicKey (), 256); // x
|
||
10 years ago
|
s.Insert (y, 256); // y
|
||
|
payload += 256;
|
||
|
uint8_t addressSize = *payload;
|
||
|
payload += 1; // size
|
||
|
uint8_t * ourAddress = payload;
|
||
|
boost::asio::ip::address ourIP;
|
||
|
if (addressSize == 4) // v4
|
||
|
{
|
||
|
boost::asio::ip::address_v4::bytes_type bytes;
|
||
|
memcpy (bytes.data (), ourAddress, 4);
|
||
|
ourIP = boost::asio::ip::address_v4 (bytes);
|
||
|
}
|
||
|
else // v6
|
||
|
{
|
||
|
boost::asio::ip::address_v6::bytes_type bytes;
|
||
|
memcpy (bytes.data (), ourAddress, 16);
|
||
|
ourIP = boost::asio::ip::address_v6 (bytes);
|
||
|
}
|
||
|
s.Insert (ourAddress, addressSize); // our IP
|
||
|
payload += addressSize; // address
|
||
10 years ago
|
uint16_t ourPort = bufbe16toh (payload);
|
||
10 years ago
|
s.Insert (payload, 2); // our port
|
||
|
payload += 2; // port
|
||
|
if (m_RemoteEndpoint.address ().is_v4 ())
|
||
|
s.Insert (m_RemoteEndpoint.address ().to_v4 ().to_bytes ().data (), 4); // remote IP v4
|
||
|
else
|
||
|
s.Insert (m_RemoteEndpoint.address ().to_v6 ().to_bytes ().data (), 16); // remote IP v6
|
||
10 years ago
|
s.Insert<uint16_t> (htobe16 (m_RemoteEndpoint.port ())); // remote port
|
||
10 years ago
|
s.Insert (payload, 8); // relayTag and signed on time
|
||
10 years ago
|
m_RelayTag = bufbe32toh (payload);
|
||
10 years ago
|
payload += 4; // relayTag
|
||
8 years ago
|
if (i2p::context.GetStatus () == eRouterStatusTesting)
|
||
|
{
|
||
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
||
|
uint32_t signedOnTime = bufbe32toh(payload);
|
||
|
if (signedOnTime < ts - SSU_CLOCK_SKEW || signedOnTime > ts + SSU_CLOCK_SKEW)
|
||
|
{
|
||
|
LogPrint (eLogError, "SSU: clock skew detected ", (int)ts - signedOnTime, ". Check your clock");
|
||
8 years ago
|
i2p::context.SetError (eRouterErrorClockSkew);
|
||
8 years ago
|
}
|
||
|
}
|
||
10 years ago
|
payload += 4; // signed on time
|
||
|
// decrypt signature
|
||
9 years ago
|
size_t signatureLen = m_RemoteIdentity->GetSignatureLen ();
|
||
10 years ago
|
size_t paddingSize = signatureLen & 0x0F; // %16
|
||
|
if (paddingSize > 0) signatureLen += (16 - paddingSize);
|
||
10 years ago
|
//TODO: since we are accessing a uint8_t this is unlikely to crash due to alignment but should be improved
|
||
10 years ago
|
m_SessionKeyDecryption.SetIV (((SSUHeader *)buf)->iv);
|
||
9 years ago
|
m_SessionKeyDecryption.Decrypt (payload, signatureLen, payload); // TODO: non-const payload
|
||
9 years ago
|
// verify signature
|
||
|
if (s.Verify (m_RemoteIdentity, payload))
|
||
|
{
|
||
|
LogPrint (eLogInfo, "SSU: Our external address is ", ourIP.to_string (), ":", ourPort);
|
||
|
i2p::context.UpdateAddress (ourIP);
|
||
|
SendSessionConfirmed (y, ourAddress, addressSize + 2);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SSU: message 'created' signature verification failed");
|
||
9 years ago
|
Failed ();
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void SSUSession::ProcessSessionConfirmed (const uint8_t * buf, size_t len)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: Session confirmed received");
|
||
9 years ago
|
auto headerSize = GetSSUHeaderSize (buf);
|
||
|
if (headerSize >= len)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SSU: Session confirmed header size ", len, " exceeds packet length ", len);
|
||
9 years ago
|
return;
|
||
|
}
|
||
|
const uint8_t * payload = buf + headerSize;
|
||
10 years ago
|
payload++; // identity fragment info
|
||
10 years ago
|
uint16_t identitySize = bufbe16toh (payload);
|
||
10 years ago
|
payload += 2; // size of identity fragment
|
||
8 years ago
|
auto identity = std::make_shared<i2p::data::IdentityEx> (payload, identitySize);
|
||
|
auto existing = i2p::data::netdb.FindRouter (identity->GetIdentHash ()); // check if exists already
|
||
|
SetRemoteIdentity (existing ? existing->GetRouterIdentity () : identity);
|
||
9 years ago
|
m_Data.UpdatePacketSize (m_RemoteIdentity->GetIdentHash ());
|
||
10 years ago
|
payload += identitySize; // identity
|
||
8 years ago
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
||
|
uint32_t signedOnTime = bufbe32toh(payload);
|
||
|
if (signedOnTime < ts - SSU_CLOCK_SKEW || signedOnTime > ts + SSU_CLOCK_SKEW)
|
||
|
{
|
||
|
LogPrint (eLogError, "SSU message 'confirmed' time difference ", (int)ts - signedOnTime, " exceeds clock skew");
|
||
|
Failed ();
|
||
|
return;
|
||
|
}
|
||
9 years ago
|
if (m_SignedData)
|
||
|
m_SignedData->Insert (payload, 4); // insert Alice's signed on time
|
||
10 years ago
|
payload += 4; // signed-on time
|
||
9 years ago
|
size_t paddingSize = (payload - buf) + m_RemoteIdentity->GetSignatureLen ();
|
||
10 years ago
|
paddingSize &= 0x0F; // %16
|
||
|
if (paddingSize > 0) paddingSize = 16 - paddingSize;
|
||
|
payload += paddingSize;
|
||
9 years ago
|
// verify signature
|
||
|
if (m_SignedData && m_SignedData->Verify (m_RemoteIdentity, payload))
|
||
|
{
|
||
|
m_Data.Send (CreateDeliveryStatusMsg (0));
|
||
|
Established ();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SSU message 'confirmed' signature verification failed");
|
||
9 years ago
|
Failed ();
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
void SSUSession::SendSessionRequest ()
|
||
9 years ago
|
{
|
||
8 years ago
|
uint8_t buf[320 + 18] = {0}; // 304 bytes for ipv4, 320 for ipv6
|
||
10 years ago
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||
9 years ago
|
uint8_t flag = 0;
|
||
9 years ago
|
// fill extended options, 3 bytes extended options don't change message size
|
||
|
if (i2p::context.GetStatus () == eRouterStatusOK) // we don't need relays
|
||
|
{
|
||
|
// tell out peer to now assign relay tag
|
||
9 years ago
|
flag = SSU_HEADER_EXTENDED_OPTIONS_INCLUDED;
|
||
9 years ago
|
*payload = 2; payload++; // 1 byte length
|
||
|
uint16_t flags = 0; // clear EXTENDED_OPTIONS_FLAG_REQUEST_RELAY_TAG
|
||
|
htobe16buf (payload, flags);
|
||
|
payload += 2;
|
||
9 years ago
|
}
|
||
9 years ago
|
// fill payload
|
||
9 years ago
|
memcpy (payload, m_DHKeysPair->GetPublicKey (), 256); // x
|
||
10 years ago
|
bool isV4 = m_RemoteEndpoint.address ().is_v4 ();
|
||
|
if (isV4)
|
||
|
{
|
||
|
payload[256] = 4;
|
||
|
memcpy (payload + 257, m_RemoteEndpoint.address ().to_v4 ().to_bytes ().data(), 4);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
payload[256] = 16;
|
||
|
memcpy (payload + 257, m_RemoteEndpoint.address ().to_v6 ().to_bytes ().data(), 16);
|
||
|
}
|
||
9 years ago
|
// encrypt and send
|
||
10 years ago
|
uint8_t iv[16];
|
||
9 years ago
|
RAND_bytes (iv, 16); // random iv
|
||
9 years ago
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_REQUEST, buf, isV4 ? 304 : 320, m_IntroKey, iv, m_IntroKey, flag);
|
||
10 years ago
|
m_Server.Send (buf, isV4 ? 304 : 320, m_RemoteEndpoint);
|
||
|
}
|
||
|
|
||
9 years ago
|
void SSUSession::SendRelayRequest (const i2p::data::RouterInfo::Introducer& introducer, uint32_t nonce)
|
||
10 years ago
|
{
|
||
9 years ago
|
auto address = i2p::context.GetRouterInfo ().GetSSUAddress (false);
|
||
10 years ago
|
if (!address)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogInfo, "SSU is not supported");
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
|
||
8 years ago
|
uint8_t buf[96 + 18] = {0};
|
||
10 years ago
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||
9 years ago
|
htobe32buf (payload, introducer.iTag);
|
||
10 years ago
|
payload += 4;
|
||
|
*payload = 0; // no address
|
||
|
payload++;
|
||
10 years ago
|
htobuf16(payload, 0); // port = 0
|
||
10 years ago
|
payload += 2;
|
||
|
*payload = 0; // challenge
|
||
|
payload++;
|
||
8 years ago
|
memcpy (payload, (const uint8_t *)address->ssu->key, 32);
|
||
10 years ago
|
payload += 32;
|
||
9 years ago
|
htobe32buf (payload, nonce); // nonce
|
||
10 years ago
|
|
||
|
uint8_t iv[16];
|
||
9 years ago
|
RAND_bytes (iv, 16); // random iv
|
||
10 years ago
|
if (m_State == eSessionStateEstablished)
|
||
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_RELAY_REQUEST, buf, 96, m_SessionKey, iv, m_MacKey);
|
||
|
else
|
||
9 years ago
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_RELAY_REQUEST, buf, 96, introducer.iKey, iv, introducer.iKey);
|
||
10 years ago
|
m_Server.Send (buf, 96, m_RemoteEndpoint);
|
||
|
}
|
||
|
|
||
9 years ago
|
void SSUSession::SendSessionCreated (const uint8_t * x, bool sendRelayTag)
|
||
10 years ago
|
{
|
||
|
auto address = IsV6 () ? i2p::context.GetRouterInfo ().GetSSUV6Address () :
|
||
|
i2p::context.GetRouterInfo ().GetSSUAddress (true); //v4 only
|
||
9 years ago
|
if (!address)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogInfo, "SSU is not supported");
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
SignedData s; // x,y, remote IP, remote port, our IP, our port, relayTag, signed on time
|
||
|
s.Insert (x, 256); // x
|
||
|
|
||
8 years ago
|
uint8_t buf[384 + 18] = {0};
|
||
10 years ago
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||
9 years ago
|
memcpy (payload, m_DHKeysPair->GetPublicKey (), 256);
|
||
10 years ago
|
s.Insert (payload, 256); // y
|
||
|
payload += 256;
|
||
|
if (m_RemoteEndpoint.address ().is_v4 ())
|
||
|
{
|
||
|
// ipv4
|
||
|
*payload = 4;
|
||
|
payload++;
|
||
|
memcpy (payload, m_RemoteEndpoint.address ().to_v4 ().to_bytes ().data(), 4);
|
||
|
s.Insert (payload, 4); // remote endpoint IP V4
|
||
|
payload += 4;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// ipv6
|
||
|
*payload = 16;
|
||
|
payload++;
|
||
|
memcpy (payload, m_RemoteEndpoint.address ().to_v6 ().to_bytes ().data(), 16);
|
||
|
s.Insert (payload, 16); // remote endpoint IP V6
|
||
|
payload += 16;
|
||
|
}
|
||
10 years ago
|
htobe16buf (payload, m_RemoteEndpoint.port ());
|
||
10 years ago
|
s.Insert (payload, 2); // remote port
|
||
|
payload += 2;
|
||
|
if (address->host.is_v4 ())
|
||
|
s.Insert (address->host.to_v4 ().to_bytes ().data (), 4); // our IP V4
|
||
|
else
|
||
|
s.Insert (address->host.to_v6 ().to_bytes ().data (), 16); // our IP V6
|
||
10 years ago
|
s.Insert<uint16_t> (htobe16 (address->port)); // our port
|
||
9 years ago
|
if (sendRelayTag && i2p::context.GetRouterInfo ().IsIntroducer () && !IsV6 ())
|
||
10 years ago
|
{
|
||
8 years ago
|
RAND_bytes((uint8_t *)&m_SentRelayTag, 4);
|
||
|
if (!m_SentRelayTag) m_SentRelayTag = 1;
|
||
10 years ago
|
}
|
||
8 years ago
|
htobe32buf (payload, m_SentRelayTag);
|
||
10 years ago
|
payload += 4; // relay tag
|
||
10 years ago
|
htobe32buf (payload, i2p::util::GetSecondsSinceEpoch ()); // signed on time
|
||
10 years ago
|
payload += 4;
|
||
9 years ago
|
s.Insert (payload - 8, 4); // relayTag
|
||
|
// we have to store this signed data for session confirmed
|
||
|
// same data but signed on time, it will Alice's there
|
||
|
m_SignedData = std::unique_ptr<SignedData>(new SignedData (s));
|
||
|
s.Insert (payload - 4, 4); // BOB's signed on time
|
||
10 years ago
|
s.Sign (i2p::context.GetPrivateKeys (), payload); // DSA signature
|
||
|
|
||
|
uint8_t iv[16];
|
||
9 years ago
|
RAND_bytes (iv, 16); // random iv
|
||
10 years ago
|
// encrypt signature and padding with newly created session key
|
||
9 years ago
|
size_t signatureLen = i2p::context.GetIdentity ()->GetSignatureLen ();
|
||
10 years ago
|
size_t paddingSize = signatureLen & 0x0F; // %16
|
||
8 years ago
|
if (paddingSize > 0)
|
||
|
{
|
||
|
// fill random padding
|
||
|
RAND_bytes(payload + signatureLen, (16 - paddingSize));
|
||
|
signatureLen += (16 - paddingSize);
|
||
|
}
|
||
10 years ago
|
m_SessionKeyEncryption.SetIV (iv);
|
||
|
m_SessionKeyEncryption.Encrypt (payload, signatureLen, payload);
|
||
|
payload += signatureLen;
|
||
|
size_t msgLen = payload - buf;
|
||
|
|
||
|
// encrypt message with intro key
|
||
9 years ago
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_CREATED, buf, msgLen, m_IntroKey, iv, m_IntroKey);
|
||
10 years ago
|
Send (buf, msgLen);
|
||
|
}
|
||
|
|
||
|
void SSUSession::SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress, size_t ourAddressLen)
|
||
|
{
|
||
8 years ago
|
uint8_t buf[512 + 18] = {0};
|
||
10 years ago
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||
|
*payload = 1; // 1 fragment
|
||
|
payload++; // info
|
||
9 years ago
|
size_t identLen = i2p::context.GetIdentity ()->GetFullLen (); // 387+ bytes
|
||
10 years ago
|
htobe16buf (payload, identLen);
|
||
10 years ago
|
payload += 2; // cursize
|
||
9 years ago
|
i2p::context.GetIdentity ()->ToBuffer (payload, identLen);
|
||
10 years ago
|
payload += identLen;
|
||
|
uint32_t signedOnTime = i2p::util::GetSecondsSinceEpoch ();
|
||
10 years ago
|
htobe32buf (payload, signedOnTime); // signed on time
|
||
10 years ago
|
payload += 4;
|
||
9 years ago
|
auto signatureLen = i2p::context.GetIdentity ()->GetSignatureLen ();
|
||
10 years ago
|
size_t paddingSize = ((payload - buf) + signatureLen)%16;
|
||
|
if (paddingSize > 0) paddingSize = 16 - paddingSize;
|
||
8 years ago
|
RAND_bytes(payload, paddingSize); // fill padding with random
|
||
10 years ago
|
payload += paddingSize; // padding size
|
||
|
// signature
|
||
|
SignedData s; // x,y, our IP, our port, remote IP, remote port, relayTag, our signed on time
|
||
9 years ago
|
s.Insert (m_DHKeysPair->GetPublicKey (), 256); // x
|
||
10 years ago
|
s.Insert (y, 256); // y
|
||
|
s.Insert (ourAddress, ourAddressLen); // our address/port as seem by party
|
||
|
if (m_RemoteEndpoint.address ().is_v4 ())
|
||
|
s.Insert (m_RemoteEndpoint.address ().to_v4 ().to_bytes ().data (), 4); // remote IP V4
|
||
|
else
|
||
|
s.Insert (m_RemoteEndpoint.address ().to_v6 ().to_bytes ().data (), 16); // remote IP V6
|
||
10 years ago
|
s.Insert<uint16_t> (htobe16 (m_RemoteEndpoint.port ())); // remote port
|
||
10 years ago
|
s.Insert (htobe32 (m_RelayTag)); // relay tag
|
||
|
s.Insert (htobe32 (signedOnTime)); // signed on time
|
||
|
s.Sign (i2p::context.GetPrivateKeys (), payload); // DSA signature
|
||
|
payload += signatureLen;
|
||
|
|
||
|
size_t msgLen = payload - buf;
|
||
|
uint8_t iv[16];
|
||
9 years ago
|
RAND_bytes (iv, 16); // random iv
|
||
10 years ago
|
// encrypt message with session key
|
||
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_CONFIRMED, buf, msgLen, m_SessionKey, iv, m_MacKey);
|
||
|
Send (buf, msgLen);
|
||
|
}
|
||
|
|
||
9 years ago
|
void SSUSession::ProcessRelayRequest (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& from)
|
||
10 years ago
|
{
|
||
10 years ago
|
uint32_t relayTag = bufbe32toh (buf);
|
||
10 years ago
|
auto session = m_Server.FindRelaySession (relayTag);
|
||
|
if (session)
|
||
|
{
|
||
|
buf += 4; // relay tag
|
||
|
uint8_t size = *buf;
|
||
|
buf++; // size
|
||
|
buf += size; // address
|
||
|
buf += 2; // port
|
||
|
uint8_t challengeSize = *buf;
|
||
|
buf++; // challenge size
|
||
|
buf += challengeSize;
|
||
9 years ago
|
const uint8_t * introKey = buf;
|
||
10 years ago
|
buf += 32; // introkey
|
||
10 years ago
|
uint32_t nonce = bufbe32toh (buf);
|
||
10 years ago
|
SendRelayResponse (nonce, from, introKey, session->m_RemoteEndpoint);
|
||
9 years ago
|
SendRelayIntro (session, from);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void SSUSession::SendRelayResponse (uint32_t nonce, const boost::asio::ip::udp::endpoint& from,
|
||
|
const uint8_t * introKey, const boost::asio::ip::udp::endpoint& to)
|
||
|
{
|
||
|
// Charlie's address always v4
|
||
|
if (!to.address ().is_v4 ())
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: Charlie's IP must be v4");
|
||
10 years ago
|
return;
|
||
|
}
|
||
8 years ago
|
uint8_t buf[80 + 18] = {0}; // 64 Alice's ipv4 and 80 Alice's ipv6
|
||
8 years ago
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||
10 years ago
|
*payload = 4;
|
||
|
payload++; // size
|
||
10 years ago
|
htobe32buf (payload, to.address ().to_v4 ().to_ulong ()); // Charlie's IP
|
||
10 years ago
|
payload += 4; // address
|
||
10 years ago
|
htobe16buf (payload, to.port ()); // Charlie's port
|
||
10 years ago
|
payload += 2; // port
|
||
|
// Alice
|
||
|
bool isV4 = from.address ().is_v4 (); // Alice's
|
||
|
if (isV4)
|
||
|
{
|
||
|
*payload = 4;
|
||
|
payload++; // size
|
||
|
memcpy (payload, from.address ().to_v4 ().to_bytes ().data (), 4); // Alice's IP V4
|
||
|
payload += 4; // address
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
*payload = 16;
|
||
|
payload++; // size
|
||
|
memcpy (payload, from.address ().to_v6 ().to_bytes ().data (), 16); // Alice's IP V6
|
||
|
payload += 16; // address
|
||
|
}
|
||
10 years ago
|
htobe16buf (payload, from.port ()); // Alice's port
|
||
10 years ago
|
payload += 2; // port
|
||
10 years ago
|
htobe32buf (payload, nonce);
|
||
10 years ago
|
|
||
|
if (m_State == eSessionStateEstablished)
|
||
|
{
|
||
|
// encrypt with session key
|
||
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_RELAY_RESPONSE, buf, isV4 ? 64 : 80);
|
||
|
Send (buf, isV4 ? 64 : 80);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// ecrypt with Alice's intro key
|
||
|
uint8_t iv[16];
|
||
9 years ago
|
RAND_bytes (iv, 16); // random iv
|
||
10 years ago
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_RELAY_RESPONSE, buf, isV4 ? 64 : 80, introKey, iv, introKey);
|
||
|
m_Server.Send (buf, isV4 ? 64 : 80, from);
|
||
|
}
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: relay response sent");
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void SSUSession::SendRelayIntro (std::shared_ptr<SSUSession> session, const boost::asio::ip::udp::endpoint& from)
|
||
10 years ago
|
{
|
||
|
if (!session) return;
|
||
|
// Alice's address always v4
|
||
|
if (!from.address ().is_v4 ())
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: Alice's IP must be v4");
|
||
10 years ago
|
return;
|
||
|
}
|
||
8 years ago
|
uint8_t buf[48 + 18] = {0};
|
||
10 years ago
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||
|
*payload = 4;
|
||
|
payload++; // size
|
||
10 years ago
|
htobe32buf (payload, from.address ().to_v4 ().to_ulong ()); // Alice's IP
|
||
10 years ago
|
payload += 4; // address
|
||
10 years ago
|
htobe16buf (payload, from.port ()); // Alice's port
|
||
10 years ago
|
payload += 2; // port
|
||
|
*payload = 0; // challenge size
|
||
|
uint8_t iv[16];
|
||
9 years ago
|
RAND_bytes (iv, 16); // random iv
|
||
10 years ago
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_RELAY_INTRO, buf, 48, session->m_SessionKey, iv, session->m_MacKey);
|
||
|
m_Server.Send (buf, 48, session->m_RemoteEndpoint);
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: relay intro sent");
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void SSUSession::ProcessRelayResponse (const uint8_t * buf, size_t len)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU message: Relay response received");
|
||
9 years ago
|
uint8_t remoteSize = *buf;
|
||
|
buf++; // remote size
|
||
|
boost::asio::ip::address_v4 remoteIP (bufbe32toh (buf));
|
||
|
buf += remoteSize; // remote address
|
||
|
uint16_t remotePort = bufbe16toh (buf);
|
||
|
buf += 2; // remote port
|
||
|
uint8_t ourSize = *buf;
|
||
|
buf++; // our size
|
||
10 years ago
|
boost::asio::ip::address ourIP;
|
||
|
if (ourSize == 4)
|
||
|
{
|
||
|
boost::asio::ip::address_v4::bytes_type bytes;
|
||
9 years ago
|
memcpy (bytes.data (), buf, 4);
|
||
10 years ago
|
ourIP = boost::asio::ip::address_v4 (bytes);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
boost::asio::ip::address_v6::bytes_type bytes;
|
||
9 years ago
|
memcpy (bytes.data (), buf, 16);
|
||
10 years ago
|
ourIP = boost::asio::ip::address_v6 (bytes);
|
||
|
}
|
||
9 years ago
|
buf += ourSize; // our address
|
||
|
uint16_t ourPort = bufbe16toh (buf);
|
||
|
buf += 2; // our port
|
||
9 years ago
|
LogPrint (eLogInfo, "SSU: Our external address is ", ourIP.to_string (), ":", ourPort);
|
||
10 years ago
|
i2p::context.UpdateAddress (ourIP);
|
||
9 years ago
|
uint32_t nonce = bufbe32toh (buf);
|
||
|
buf += 4; // nonce
|
||
9 years ago
|
auto it = m_RelayRequests.find (nonce);
|
||
|
if (it != m_RelayRequests.end ())
|
||
|
{
|
||
|
// check if we are waiting for introduction
|
||
|
boost::asio::ip::udp::endpoint remoteEndpoint (remoteIP, remotePort);
|
||
|
if (!m_Server.FindSession (remoteEndpoint))
|
||
|
{
|
||
|
// we didn't have correct endpoint when sent relay request
|
||
|
// now we do
|
||
9 years ago
|
LogPrint (eLogInfo, "SSU: RelayReponse connecting to endpoint ", remoteEndpoint);
|
||
9 years ago
|
if (i2p::context.GetRouterInfo ().UsesIntroducer ()) // if we are unreachable
|
||
|
m_Server.Send (buf, 0, remoteEndpoint); // send HolePunch
|
||
|
m_Server.CreateDirectSession (it->second, remoteEndpoint, false);
|
||
|
}
|
||
|
// delete request
|
||
|
m_RelayRequests.erase (it);
|
||
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "SSU: Unsolicited RelayResponse, nonce=", nonce);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void SSUSession::ProcessRelayIntro (const uint8_t * buf, size_t len)
|
||
10 years ago
|
{
|
||
|
uint8_t size = *buf;
|
||
|
if (size == 4)
|
||
|
{
|
||
|
buf++; // size
|
||
10 years ago
|
boost::asio::ip::address_v4 address (bufbe32toh (buf));
|
||
10 years ago
|
buf += 4; // address
|
||
10 years ago
|
uint16_t port = bufbe16toh (buf);
|
||
9 years ago
|
// send hole punch of 0 bytes
|
||
10 years ago
|
m_Server.Send (buf, 0, boost::asio::ip::udp::endpoint (address, port));
|
||
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: Address size ", size, " is not supported");
|
||
10 years ago
|
}
|
||
|
|
||
|
void SSUSession::FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len,
|
||
9 years ago
|
const i2p::crypto::AESKey& aesKey, const uint8_t * iv, const i2p::crypto::MACKey& macKey, uint8_t flag)
|
||
10 years ago
|
{
|
||
|
if (len < sizeof (SSUHeader))
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SSU: Unexpected packet length ", len);
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
SSUHeader * header = (SSUHeader *)buf;
|
||
|
memcpy (header->iv, iv, 16);
|
||
9 years ago
|
header->flag = flag | (payloadType << 4); // MSB is 0
|
||
9 years ago
|
htobe32buf (header->time, i2p::util::GetSecondsSinceEpoch ());
|
||
10 years ago
|
uint8_t * encrypted = &header->flag;
|
||
|
uint16_t encryptedLen = len - (encrypted - buf);
|
||
|
i2p::crypto::CBCEncryption encryption;
|
||
|
encryption.SetKey (aesKey);
|
||
|
encryption.SetIV (iv);
|
||
|
encryption.Encrypt (encrypted, encryptedLen, encrypted);
|
||
|
// assume actual buffer size is 18 (16 + 2) bytes more
|
||
|
memcpy (buf + len, iv, 16);
|
||
10 years ago
|
htobe16buf (buf + len + 16, encryptedLen);
|
||
10 years ago
|
i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, macKey, header->mac);
|
||
|
}
|
||
|
|
||
|
void SSUSession::FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len)
|
||
|
{
|
||
|
if (len < sizeof (SSUHeader))
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SSU: Unexpected packet length ", len);
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
SSUHeader * header = (SSUHeader *)buf;
|
||
9 years ago
|
RAND_bytes (header->iv, 16); // random iv
|
||
10 years ago
|
m_SessionKeyEncryption.SetIV (header->iv);
|
||
|
header->flag = payloadType << 4; // MSB is 0
|
||
9 years ago
|
htobe32buf (header->time, i2p::util::GetSecondsSinceEpoch ());
|
||
10 years ago
|
uint8_t * encrypted = &header->flag;
|
||
|
uint16_t encryptedLen = len - (encrypted - buf);
|
||
|
m_SessionKeyEncryption.Encrypt (encrypted, encryptedLen, encrypted);
|
||
|
// assume actual buffer size is 18 (16 + 2) bytes more
|
||
|
memcpy (buf + len, header->iv, 16);
|
||
10 years ago
|
htobe16buf (buf + len + 16, encryptedLen);
|
||
10 years ago
|
i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, m_MacKey, header->mac);
|
||
|
}
|
||
|
|
||
9 years ago
|
void SSUSession::Decrypt (uint8_t * buf, size_t len, const i2p::crypto::AESKey& aesKey)
|
||
10 years ago
|
{
|
||
|
if (len < sizeof (SSUHeader))
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SSU: Unexpected packet length ", len);
|
||
10 years ago
|
return;
|
||
9 years ago
|
}
|
||
10 years ago
|
SSUHeader * header = (SSUHeader *)buf;
|
||
|
uint8_t * encrypted = &header->flag;
|
||
|
uint16_t encryptedLen = len - (encrypted - buf);
|
||
|
i2p::crypto::CBCDecryption decryption;
|
||
|
decryption.SetKey (aesKey);
|
||
|
decryption.SetIV (header->iv);
|
||
|
decryption.Decrypt (encrypted, encryptedLen, encrypted);
|
||
|
}
|
||
|
|
||
|
void SSUSession::DecryptSessionKey (uint8_t * buf, size_t len)
|
||
|
{
|
||
|
if (len < sizeof (SSUHeader))
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SSU: Unexpected packet length ", len);
|
||
10 years ago
|
return;
|
||
9 years ago
|
}
|
||
10 years ago
|
SSUHeader * header = (SSUHeader *)buf;
|
||
|
uint8_t * encrypted = &header->flag;
|
||
|
uint16_t encryptedLen = len - (encrypted - buf);
|
||
|
if (encryptedLen > 0)
|
||
|
{
|
||
|
m_SessionKeyDecryption.SetIV (header->iv);
|
||
|
m_SessionKeyDecryption.Decrypt (encrypted, encryptedLen, encrypted);
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
bool SSUSession::Validate (uint8_t * buf, size_t len, const i2p::crypto::MACKey& macKey)
|
||
10 years ago
|
{
|
||
|
if (len < sizeof (SSUHeader))
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SSU: Unexpected packet length ", len);
|
||
10 years ago
|
return false;
|
||
9 years ago
|
}
|
||
10 years ago
|
SSUHeader * header = (SSUHeader *)buf;
|
||
|
uint8_t * encrypted = &header->flag;
|
||
|
uint16_t encryptedLen = len - (encrypted - buf);
|
||
|
// assume actual buffer size is 18 (16 + 2) bytes more
|
||
|
memcpy (buf + len, header->iv, 16);
|
||
10 years ago
|
htobe16buf (buf + len + 16, encryptedLen);
|
||
10 years ago
|
uint8_t digest[16];
|
||
|
i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, macKey, digest);
|
||
|
return !memcmp (header->mac, digest, 16);
|
||
|
}
|
||
|
|
||
|
void SSUSession::Connect ()
|
||
|
{
|
||
|
if (m_State == eSessionStateUnknown)
|
||
|
{
|
||
|
// set connect timer
|
||
|
ScheduleConnectTimer ();
|
||
|
m_DHKeysPair = transports.GetNextDHKeysPair ();
|
||
|
SendSessionRequest ();
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void SSUSession::WaitForConnect ()
|
||
|
{
|
||
9 years ago
|
if (!IsOutgoing ()) // incoming session
|
||
10 years ago
|
ScheduleConnectTimer ();
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "SSU: wait for connect for outgoing session");
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SSUSession::ScheduleConnectTimer ()
|
||
|
{
|
||
8 years ago
|
m_ConnectTimer.cancel ();
|
||
|
m_ConnectTimer.expires_from_now (boost::posix_time::seconds(SSU_CONNECT_TIMEOUT));
|
||
|
m_ConnectTimer.async_wait (std::bind (&SSUSession::HandleConnectTimer,
|
||
10 years ago
|
shared_from_this (), std::placeholders::_1));
|
||
10 years ago
|
}
|
||
|
|
||
|
void SSUSession::HandleConnectTimer (const boost::system::error_code& ecode)
|
||
|
{
|
||
|
if (!ecode)
|
||
|
{
|
||
|
// timeout expired
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: session with ", m_RemoteEndpoint, " was not established after ", SSU_CONNECT_TIMEOUT, " seconds");
|
||
10 years ago
|
Failed ();
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void SSUSession::Introduce (const i2p::data::RouterInfo::Introducer& introducer,
|
||
|
std::shared_ptr<const i2p::data::RouterInfo> to)
|
||
10 years ago
|
{
|
||
|
if (m_State == eSessionStateUnknown)
|
||
|
{
|
||
|
// set connect timer
|
||
8 years ago
|
m_ConnectTimer.expires_from_now (boost::posix_time::seconds(SSU_CONNECT_TIMEOUT));
|
||
|
m_ConnectTimer.async_wait (std::bind (&SSUSession::HandleConnectTimer,
|
||
10 years ago
|
shared_from_this (), std::placeholders::_1));
|
||
9 years ago
|
}
|
||
|
uint32_t nonce;
|
||
|
RAND_bytes ((uint8_t *)&nonce, 4);
|
||
|
m_RelayRequests[nonce] = to;
|
||
|
SendRelayRequest (introducer, nonce);
|
||
10 years ago
|
}
|
||
|
|
||
|
void SSUSession::WaitForIntroduction ()
|
||
|
{
|
||
|
m_State = eSessionStateIntroduced;
|
||
|
// set connect timer
|
||
8 years ago
|
m_ConnectTimer.expires_from_now (boost::posix_time::seconds(SSU_CONNECT_TIMEOUT));
|
||
|
m_ConnectTimer.async_wait (std::bind (&SSUSession::HandleConnectTimer,
|
||
10 years ago
|
shared_from_this (), std::placeholders::_1));
|
||
10 years ago
|
}
|
||
|
|
||
|
void SSUSession::Close ()
|
||
|
{
|
||
8 years ago
|
SendSessionDestroyed ();
|
||
|
Reset ();
|
||
10 years ago
|
m_State = eSessionStateClosed;
|
||
8 years ago
|
}
|
||
|
|
||
|
void SSUSession::Reset ()
|
||
|
{
|
||
|
m_State = eSessionStateUnknown;
|
||
10 years ago
|
transports.PeerDisconnected (shared_from_this ());
|
||
10 years ago
|
m_Data.Stop ();
|
||
8 years ago
|
m_ConnectTimer.cancel ();
|
||
8 years ago
|
if (m_SentRelayTag)
|
||
8 years ago
|
{
|
||
8 years ago
|
m_Server.RemoveRelay (m_SentRelayTag); // relay tag is not valid anymore
|
||
8 years ago
|
m_SentRelayTag = 0;
|
||
|
}
|
||
|
m_DHKeysPair = nullptr;
|
||
|
m_SignedData = nullptr;
|
||
|
m_IsSessionKey = false;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void SSUSession::Done ()
|
||
10 years ago
|
{
|
||
10 years ago
|
GetService ().post (std::bind (&SSUSession::Failed, shared_from_this ()));
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SSUSession::Established ()
|
||
|
{
|
||
|
m_State = eSessionStateEstablished;
|
||
9 years ago
|
m_DHKeysPair = nullptr;
|
||
|
m_SignedData = nullptr;
|
||
10 years ago
|
m_Data.Start ();
|
||
10 years ago
|
transports.PeerConnected (shared_from_this ());
|
||
9 years ago
|
if (m_IsPeerTest)
|
||
10 years ago
|
SendPeerTest ();
|
||
8 years ago
|
if (m_SentRelayTag)
|
||
|
m_Server.AddRelay (m_SentRelayTag, shared_from_this ());
|
||
8 years ago
|
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch ();
|
||
10 years ago
|
}
|
||
|
|
||
|
void SSUSession::Failed ()
|
||
|
{
|
||
|
if (m_State != eSessionStateFailed)
|
||
|
{
|
||
|
m_State = eSessionStateFailed;
|
||
10 years ago
|
m_Server.DeleteSession (shared_from_this ());
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void SSUSession::SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs)
|
||
10 years ago
|
{
|
||
10 years ago
|
GetService ().post (std::bind (&SSUSession::PostI2NPMessages, shared_from_this (), msgs));
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SSUSession::PostI2NPMessages (std::vector<std::shared_ptr<I2NPMessage> > msgs)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (m_State == eSessionStateEstablished)
|
||
|
{
|
||
8 years ago
|
for (const auto& it: msgs)
|
||
10 years ago
|
if (it) m_Data.Send (it);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SSUSession::ProcessData (uint8_t * buf, size_t len)
|
||
|
{
|
||
|
m_Data.ProcessMessage (buf, len);
|
||
10 years ago
|
m_IsDataReceived = true;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SSUSession::FlushData ()
|
||
|
{
|
||
|
if (m_IsDataReceived)
|
||
|
{
|
||
|
m_Data.FlushReceivedMessage ();
|
||
|
m_IsDataReceived = false;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void SSUSession::ProcessPeerTest (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
|
||
10 years ago
|
{
|
||
10 years ago
|
uint32_t nonce = bufbe32toh (buf); // 4 bytes
|
||
|
uint8_t size = buf[4]; // 1 byte
|
||
8 years ago
|
const uint8_t * address = buf + 5; // big endian, size bytes
|
||
10 years ago
|
uint16_t port = buf16toh(buf + size + 5); // big endian, 2 bytes
|
||
|
const uint8_t * introKey = buf + size + 7;
|
||
8 years ago
|
if (port && (size != 4) && (size != 16))
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: Address of ", size, " bytes not supported");
|
||
10 years ago
|
return;
|
||
|
}
|
||
10 years ago
|
switch (m_Server.GetPeerTestParticipant (nonce))
|
||
|
{
|
||
10 years ago
|
// existing test
|
||
10 years ago
|
case ePeerTestParticipantAlice1:
|
||
|
{
|
||
8 years ago
|
if (m_Server.GetPeerTestSession (nonce) == shared_from_this ()) // Alice-Bob
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: peer test from Bob. We are Alice");
|
||
10 years ago
|
if (i2p::context.GetStatus () == eRouterStatusTesting) // still not OK
|
||
|
i2p::context.SetStatus (eRouterStatusFirewalled);
|
||
|
}
|
||
10 years ago
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: first peer test from Charlie. We are Alice");
|
||
8 years ago
|
if (m_State == eSessionStateEstablished)
|
||
|
LogPrint (eLogWarning, "SSU: first peer test from Charlie through established session. We are Alice");
|
||
10 years ago
|
i2p::context.SetStatus (eRouterStatusOK);
|
||
10 years ago
|
m_Server.UpdatePeerTest (nonce, ePeerTestParticipantAlice2);
|
||
8 years ago
|
SendPeerTest (nonce, senderEndpoint.address (), senderEndpoint.port (), introKey, true, false); // to Charlie
|
||
10 years ago
|
}
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
case ePeerTestParticipantAlice2:
|
||
|
{
|
||
8 years ago
|
if (m_Server.GetPeerTestSession (nonce) == shared_from_this ()) // Alice-Bob
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: peer test from Bob. We are Alice");
|
||
10 years ago
|
else
|
||
|
{
|
||
|
// peer test successive
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: second peer test from Charlie. We are Alice");
|
||
10 years ago
|
i2p::context.SetStatus (eRouterStatusOK);
|
||
10 years ago
|
m_Server.RemovePeerTest (nonce);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
case ePeerTestParticipantBob:
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: peer test from Charlie. We are Bob");
|
||
10 years ago
|
auto session = m_Server.GetPeerTestSession (nonce); // session with Alice from PeerTest
|
||
|
if (session && session->m_State == eSessionStateEstablished)
|
||
10 years ago
|
session->Send (PAYLOAD_TYPE_PEER_TEST, buf, len); // back to Alice
|
||
10 years ago
|
m_Server.RemovePeerTest (nonce); // nonce has been used
|
||
10 years ago
|
break;
|
||
10 years ago
|
}
|
||
10 years ago
|
case ePeerTestParticipantCharlie:
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: peer test from Alice. We are Charlie");
|
||
8 years ago
|
SendPeerTest (nonce, senderEndpoint.address (), senderEndpoint.port (), introKey); // to Alice with her actual address
|
||
10 years ago
|
m_Server.RemovePeerTest (nonce); // nonce has been used
|
||
10 years ago
|
break;
|
||
10 years ago
|
}
|
||
10 years ago
|
// test not found
|
||
|
case ePeerTestParticipantUnknown:
|
||
10 years ago
|
{
|
||
10 years ago
|
if (m_State == eSessionStateEstablished)
|
||
10 years ago
|
{
|
||
10 years ago
|
// new test
|
||
|
if (port)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: peer test from Bob. We are Charlie");
|
||
10 years ago
|
m_Server.NewPeerTest (nonce, ePeerTestParticipantCharlie);
|
||
10 years ago
|
Send (PAYLOAD_TYPE_PEER_TEST, buf, len); // back to Bob
|
||
8 years ago
|
boost::asio::ip::address addr; // Alice's address
|
||
|
if (size == 4) // v4
|
||
|
{
|
||
|
boost::asio::ip::address_v4::bytes_type bytes;
|
||
|
memcpy (bytes.data (), address, 4);
|
||
|
addr = boost::asio::ip::address_v4 (bytes);
|
||
|
}
|
||
|
else // v6
|
||
|
{
|
||
|
boost::asio::ip::address_v6::bytes_type bytes;
|
||
8 years ago
|
memcpy (bytes.data (), address, 16);
|
||
8 years ago
|
addr = boost::asio::ip::address_v6 (bytes);
|
||
|
}
|
||
|
SendPeerTest (nonce, addr, be16toh (port), introKey); // to Alice with her address received from Bob
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: peer test from Alice. We are Bob");
|
||
8 years ago
|
auto session = senderEndpoint.address ().is_v4 () ? m_Server.GetRandomEstablishedV4Session (shared_from_this ()) : m_Server.GetRandomEstablishedV6Session (shared_from_this ()); // Charlie
|
||
10 years ago
|
if (session)
|
||
|
{
|
||
10 years ago
|
m_Server.NewPeerTest (nonce, ePeerTestParticipantBob, shared_from_this ());
|
||
8 years ago
|
session->SendPeerTest (nonce, senderEndpoint.address (), senderEndpoint.port (), introKey, false); // to Charlie with Alice's actual address
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "SSU: unexpected peer test");
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
void SSUSession::SendPeerTest (uint32_t nonce, const boost::asio::ip::address& address, uint16_t port,
|
||
10 years ago
|
const uint8_t * introKey, bool toAddress, bool sendAddress)
|
||
|
// toAddress is true for Alice<->Chalie communications only
|
||
|
// sendAddress is false if message comes from Alice
|
||
10 years ago
|
{
|
||
8 years ago
|
uint8_t buf[80 + 18] = {0};
|
||
10 years ago
|
uint8_t iv[16];
|
||
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||
10 years ago
|
htobe32buf (payload, nonce);
|
||
10 years ago
|
payload += 4; // nonce
|
||
10 years ago
|
// address and port
|
||
8 years ago
|
if (sendAddress)
|
||
|
{
|
||
|
if (address.is_v4 ())
|
||
|
{
|
||
|
*payload = 4;
|
||
|
memcpy (payload + 1, address.to_v4 ().to_bytes ().data (), 4); // our IP V4
|
||
|
}
|
||
|
else if (address.is_v6 ())
|
||
|
{
|
||
8 years ago
|
*payload = 16;
|
||
8 years ago
|
memcpy (payload + 1, address.to_v6 ().to_bytes ().data (), 16); // our IP V6
|
||
|
}
|
||
|
else
|
||
|
*payload = 0;
|
||
|
payload += (payload[0] + 1);
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
*payload = 0;
|
||
|
payload++; //size
|
||
|
}
|
||
10 years ago
|
htobe16buf (payload, port);
|
||
10 years ago
|
payload += 2; // port
|
||
10 years ago
|
// intro key
|
||
|
if (toAddress)
|
||
|
{
|
||
|
// send our intro key to address instead it's own
|
||
|
auto addr = i2p::context.GetRouterInfo ().GetSSUAddress ();
|
||
|
if (addr)
|
||
8 years ago
|
memcpy (payload, addr->ssu->key, 32); // intro key
|
||
10 years ago
|
else
|
||
9 years ago
|
LogPrint (eLogInfo, "SSU is not supported. Can't send peer test");
|
||
10 years ago
|
}
|
||
|
else
|
||
|
memcpy (payload, introKey, 32); // intro key
|
||
10 years ago
|
|
||
10 years ago
|
// send
|
||
9 years ago
|
RAND_bytes (iv, 16); // random iv
|
||
10 years ago
|
if (toAddress)
|
||
|
{
|
||
|
// encrypt message with specified intro key
|
||
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_PEER_TEST, buf, 80, introKey, iv, introKey);
|
||
8 years ago
|
boost::asio::ip::udp::endpoint e (address, port);
|
||
10 years ago
|
m_Server.Send (buf, 80, e);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// encrypt message with session key
|
||
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_PEER_TEST, buf, 80);
|
||
|
Send (buf, 80);
|
||
8 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
void SSUSession::SendPeerTest ()
|
||
|
{
|
||
10 years ago
|
// we are Alice
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: sending peer test");
|
||
8 years ago
|
auto address = i2p::context.GetRouterInfo ().GetSSUAddress (i2p::context.SupportsV4 ());
|
||
10 years ago
|
if (!address)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogInfo, "SSU is not supported. Can't send peer test");
|
||
10 years ago
|
return;
|
||
|
}
|
||
9 years ago
|
uint32_t nonce;
|
||
|
RAND_bytes ((uint8_t *)&nonce, 4);
|
||
10 years ago
|
if (!nonce) nonce = 1;
|
||
9 years ago
|
m_IsPeerTest = false;
|
||
8 years ago
|
m_Server.NewPeerTest (nonce, ePeerTestParticipantAlice1, shared_from_this ());
|
||
8 years ago
|
SendPeerTest (nonce, boost::asio::ip::address(), 0, address->ssu->key, false, false); // address and port always zero for Alice
|
||
10 years ago
|
}
|
||
|
|
||
|
void SSUSession::SendKeepAlive ()
|
||
|
{
|
||
|
if (m_State == eSessionStateEstablished)
|
||
|
{
|
||
8 years ago
|
uint8_t buf[48 + 18] = {0};
|
||
10 years ago
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||
|
*payload = 0; // flags
|
||
|
payload++;
|
||
|
*payload = 0; // num fragments
|
||
|
// encrypt message with session key
|
||
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_DATA, buf, 48);
|
||
|
Send (buf, 48);
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: keep-alive sent");
|
||
8 years ago
|
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch ();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
void SSUSession::SendSessionDestroyed ()
|
||
10 years ago
|
{
|
||
|
if (m_IsSessionKey)
|
||
|
{
|
||
8 years ago
|
uint8_t buf[48 + 18] = {0};
|
||
10 years ago
|
// encrypt message with session key
|
||
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_DESTROYED, buf, 48);
|
||
10 years ago
|
try
|
||
|
{
|
||
|
Send (buf, 48);
|
||
|
}
|
||
|
catch (std::exception& ex)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: exception while sending session destoroyed: ", ex.what ());
|
||
10 years ago
|
}
|
||
9 years ago
|
LogPrint (eLogDebug, "SSU: session destroyed sent");
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void SSUSession::Send (uint8_t type, const uint8_t * payload, size_t len)
|
||
|
{
|
||
8 years ago
|
uint8_t buf[SSU_MTU_V4 + 18] = {0};
|
||
10 years ago
|
size_t msgSize = len + sizeof (SSUHeader);
|
||
10 years ago
|
size_t paddingSize = msgSize & 0x0F; // %16
|
||
10 years ago
|
if (paddingSize > 0) msgSize += (16 - paddingSize);
|
||
10 years ago
|
if (msgSize > SSU_MTU_V4)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "SSU: payload size ", msgSize, " exceeds MTU");
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
memcpy (buf + sizeof (SSUHeader), payload, len);
|
||
|
// encrypt message with session key
|
||
|
FillHeaderAndEncrypt (type, buf, msgSize);
|
||
|
Send (buf, msgSize);
|
||
|
}
|
||
|
|
||
|
void SSUSession::Send (const uint8_t * buf, size_t size)
|
||
|
{
|
||
|
m_NumSentBytes += size;
|
||
10 years ago
|
i2p::transport::transports.UpdateSentBytes (size);
|
||
10 years ago
|
m_Server.Send (buf, size, m_RemoteEndpoint);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|