2014-10-30 22:13:29 +03:00
|
|
|
#include <boost/bind.hpp>
|
2015-11-03 17:15:49 +03:00
|
|
|
#include <openssl/dh.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/rand.h>
|
2014-10-30 22:13:29 +03:00
|
|
|
#include "Log.h"
|
|
|
|
#include "Timestamp.h"
|
|
|
|
#include "RouterContext.h"
|
|
|
|
#include "Transports.h"
|
|
|
|
#include "SSU.h"
|
|
|
|
#include "SSUSession.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace transport
|
|
|
|
{
|
|
|
|
SSUSession::SSUSession (SSUServer& server, boost::asio::ip::udp::endpoint& remoteEndpoint,
|
2014-11-21 20:34:17 +03:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest ): TransportSession (router),
|
2015-02-08 16:50:05 +03:00
|
|
|
m_Server (server), m_RemoteEndpoint (remoteEndpoint), m_Timer (GetService ()),
|
2015-11-03 17:15:49 +03:00
|
|
|
m_IsPeerTest (peerTest),m_State (eSessionStateUnknown), m_IsSessionKey (false),
|
2015-03-17 01:57:22 +03:00
|
|
|
m_RelayTag (0),m_Data (*this), m_IsDataReceived (false)
|
2015-11-03 17:15:49 +03:00
|
|
|
{
|
|
|
|
if (router)
|
|
|
|
{
|
|
|
|
// we are client
|
|
|
|
auto address = router->GetSSUAddress ();
|
|
|
|
if (address) m_IntroKey = address->key;
|
|
|
|
m_Data.AdjustPacketSize (router); // mtu
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we are server
|
|
|
|
auto address = i2p::context.GetRouterInfo ().GetSSUAddress ();
|
|
|
|
if (address) m_IntroKey = address->key;
|
|
|
|
}
|
2014-10-30 22:13:29 +03:00
|
|
|
m_CreationTime = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
}
|
|
|
|
|
|
|
|
SSUSession::~SSUSession ()
|
|
|
|
{
|
|
|
|
}
|
2015-02-07 23:25:06 +03:00
|
|
|
|
|
|
|
boost::asio::io_service& SSUSession::GetService ()
|
|
|
|
{
|
|
|
|
return IsV6 () ? m_Server.GetServiceV6 () : m_Server.GetService ();
|
|
|
|
}
|
2014-10-30 22:13:29 +03:00
|
|
|
|
|
|
|
void SSUSession::CreateAESandMacKey (const uint8_t * pubKey)
|
|
|
|
{
|
|
|
|
uint8_t sharedKey[256];
|
2015-11-03 17:15:49 +03:00
|
|
|
m_DHKeysPair->Agree (pubKey, sharedKey);
|
2014-10-30 22:13:29 +03:00
|
|
|
|
2014-11-01 21:56:13 +03:00
|
|
|
uint8_t * sessionKey = m_SessionKey, * macKey = m_MacKey;
|
2014-10-30 22:13:29 +03:00
|
|
|
if (sharedKey[0] & 0x80)
|
|
|
|
{
|
2014-11-01 21:56:13 +03:00
|
|
|
sessionKey[0] = 0;
|
|
|
|
memcpy (sessionKey + 1, sharedKey, 31);
|
|
|
|
memcpy (macKey, sharedKey + 31, 32);
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
else if (sharedKey[0])
|
|
|
|
{
|
2014-11-01 21:56:13 +03:00
|
|
|
memcpy (sessionKey, sharedKey, 32);
|
|
|
|
memcpy (macKey, sharedKey + 32, 32);
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// find first non-zero byte
|
|
|
|
uint8_t * nonZero = sharedKey + 1;
|
|
|
|
while (!*nonZero)
|
|
|
|
{
|
|
|
|
nonZero++;
|
|
|
|
if (nonZero - sharedKey > 32)
|
|
|
|
{
|
|
|
|
LogPrint ("First 32 bytes of shared key is all zeros. Ignored");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-01 21:56:13 +03:00
|
|
|
memcpy (sessionKey, nonZero, 32);
|
2015-11-03 17:15:49 +03:00
|
|
|
SHA256(nonZero, 64 - (nonZero - sharedKey), macKey);
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
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;
|
2015-03-17 02:33:59 +03:00
|
|
|
i2p::transport::transports.UpdateReceivedBytes (len);
|
2014-10-30 22:13:29 +03:00
|
|
|
if (m_State == eSessionStateIntroduced)
|
|
|
|
{
|
|
|
|
// HolePunch received
|
|
|
|
LogPrint ("SSU HolePunch of ", len, " bytes received");
|
|
|
|
m_State = eSessionStateUnknown;
|
|
|
|
Connect ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!len) return; // ignore zero-length packets
|
|
|
|
if (m_State == eSessionStateEstablished)
|
|
|
|
ScheduleTermination ();
|
|
|
|
|
|
|
|
if (m_IsSessionKey && Validate (buf, len, m_MacKey)) // try session key first
|
|
|
|
DecryptSessionKey (buf, len);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// try intro key depending on side
|
2015-11-03 17:15:49 +03:00
|
|
|
if (Validate (buf, len, m_IntroKey))
|
|
|
|
Decrypt (buf, len, m_IntroKey);
|
2014-10-30 22:13:29 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// try own intro key
|
|
|
|
auto address = i2p::context.GetRouterInfo ().GetSSUAddress ();
|
|
|
|
if (!address)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU is not supported");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Validate (buf, len, address->key))
|
|
|
|
Decrypt (buf, len, address->key);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "MAC verification failed ", len, " bytes from ", senderEndpoint);
|
2014-11-24 20:26:11 +03:00
|
|
|
m_Server.DeleteSession (shared_from_this ());
|
2014-10-30 22:13:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// successfully decrypted
|
|
|
|
ProcessMessage (buf, len, senderEndpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::ProcessMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
|
|
|
|
{
|
2015-03-30 20:10:36 +03:00
|
|
|
len -= (len & 0x0F); // %16, delete extra padding
|
|
|
|
if (len <= sizeof (SSUHeader)) return; // drop empty message
|
2014-12-31 17:14:53 +03:00
|
|
|
//TODO: since we are accessing a uint8_t this is unlikely to crash due to alignment but should be improved
|
2014-10-30 22:13:29 +03:00
|
|
|
SSUHeader * header = (SSUHeader *)buf;
|
|
|
|
switch (header->GetPayloadType ())
|
|
|
|
{
|
|
|
|
case PAYLOAD_TYPE_DATA:
|
|
|
|
ProcessData (buf + sizeof (SSUHeader), len - sizeof (SSUHeader));
|
|
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_SESSION_REQUEST:
|
|
|
|
ProcessSessionRequest (buf, len, senderEndpoint);
|
|
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_SESSION_CREATED:
|
|
|
|
ProcessSessionCreated (buf, len);
|
|
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_SESSION_CONFIRMED:
|
|
|
|
ProcessSessionConfirmed (buf, len);
|
|
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_PEER_TEST:
|
|
|
|
LogPrint (eLogDebug, "SSU peer test received");
|
|
|
|
ProcessPeerTest (buf + sizeof (SSUHeader), len - sizeof (SSUHeader), senderEndpoint);
|
|
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_SESSION_DESTROYED:
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "SSU session destroy received");
|
2014-11-24 20:26:11 +03:00
|
|
|
m_Server.DeleteSession (shared_from_this ());
|
2014-10-30 22:13:29 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PAYLOAD_TYPE_RELAY_RESPONSE:
|
|
|
|
ProcessRelayResponse (buf, len);
|
|
|
|
if (m_State != eSessionStateEstablished)
|
2014-11-24 20:26:11 +03:00
|
|
|
m_Server.DeleteSession (shared_from_this ());
|
2014-10-30 22:13:29 +03:00
|
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_RELAY_REQUEST:
|
|
|
|
LogPrint (eLogDebug, "SSU relay request received");
|
|
|
|
ProcessRelayRequest (buf + sizeof (SSUHeader), len - sizeof (SSUHeader), senderEndpoint);
|
|
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_RELAY_INTRO:
|
|
|
|
LogPrint (eLogDebug, "SSU relay intro received");
|
|
|
|
ProcessRelayIntro (buf + sizeof (SSUHeader), len - sizeof (SSUHeader));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LogPrint (eLogWarning, "Unexpected SSU payload type ", (int)header->GetPayloadType ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::ProcessSessionRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "Session request received");
|
|
|
|
m_RemoteEndpoint = senderEndpoint;
|
|
|
|
if (!m_DHKeysPair)
|
|
|
|
m_DHKeysPair = transports.GetNextDHKeysPair ();
|
|
|
|
CreateAESandMacKey (buf + sizeof (SSUHeader));
|
|
|
|
SendSessionCreated (buf + sizeof (SSUHeader));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::ProcessSessionCreated (uint8_t * buf, size_t len)
|
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
if (!IsOutgoing () || !m_DHKeysPair)
|
2014-10-30 22:13:29 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "Unsolicited session created message");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogPrint (eLogDebug, "Session created received");
|
|
|
|
m_Timer.cancel (); // connect timer
|
|
|
|
SignedData s; // x,y, our IP, our port, remote IP, remote port, relayTag, signed on time
|
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
|
|
|
uint8_t * y = payload;
|
|
|
|
CreateAESandMacKey (y);
|
2015-11-03 17:15:49 +03:00
|
|
|
s.Insert (m_DHKeysPair->GetPublicKey (), 256); // x
|
2014-10-30 22:13:29 +03:00
|
|
|
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
|
2014-12-30 01:04:02 +03:00
|
|
|
uint16_t ourPort = bufbe16toh (payload);
|
2014-10-30 22:13:29 +03:00
|
|
|
s.Insert (payload, 2); // our port
|
|
|
|
payload += 2; // port
|
|
|
|
LogPrint ("Our external address is ", ourIP.to_string (), ":", ourPort);
|
|
|
|
i2p::context.UpdateAddress (ourIP);
|
|
|
|
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
|
2015-02-25 06:17:46 +03:00
|
|
|
s.Insert<uint16_t> (htobe16 (m_RemoteEndpoint.port ())); // remote port
|
2014-10-30 22:13:29 +03:00
|
|
|
s.Insert (payload, 8); // relayTag and signed on time
|
2014-12-30 01:04:02 +03:00
|
|
|
m_RelayTag = bufbe32toh (payload);
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 4; // relayTag
|
|
|
|
payload += 4; // signed on time
|
|
|
|
// decrypt signature
|
2015-11-03 17:15:49 +03:00
|
|
|
size_t signatureLen = m_RemoteIdentity->GetSignatureLen ();
|
2014-10-30 22:13:29 +03:00
|
|
|
size_t paddingSize = signatureLen & 0x0F; // %16
|
|
|
|
if (paddingSize > 0) signatureLen += (16 - paddingSize);
|
2014-12-31 17:14:53 +03:00
|
|
|
//TODO: since we are accessing a uint8_t this is unlikely to crash due to alignment but should be improved
|
2014-10-30 22:13:29 +03:00
|
|
|
m_SessionKeyDecryption.SetIV (((SSUHeader *)buf)->iv);
|
|
|
|
m_SessionKeyDecryption.Decrypt (payload, signatureLen, payload);
|
|
|
|
// verify
|
|
|
|
if (!s.Verify (m_RemoteIdentity, payload))
|
2015-11-03 17:15:49 +03:00
|
|
|
LogPrint (eLogError, "Session created SSU signature verification failed");
|
2014-10-30 22:13:29 +03:00
|
|
|
|
|
|
|
SendSessionConfirmed (y, ourAddress, addressSize + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::ProcessSessionConfirmed (uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "Session confirmed received");
|
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
|
|
|
payload++; // identity fragment info
|
2014-12-30 01:04:02 +03:00
|
|
|
uint16_t identitySize = bufbe16toh (payload);
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 2; // size of identity fragment
|
2015-11-03 17:15:49 +03:00
|
|
|
SetRemoteIdentity (std::make_shared<i2p::data::IdentityEx> (payload, identitySize));
|
|
|
|
m_Data.UpdatePacketSize (m_RemoteIdentity->GetIdentHash ());
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += identitySize; // identity
|
2015-11-03 17:15:49 +03:00
|
|
|
if (m_SignedData)
|
|
|
|
m_SignedData->Insert (payload, 4); // insert Alice's signed on time
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 4; // signed-on time
|
2015-11-03 17:15:49 +03:00
|
|
|
size_t paddingSize = (payload - buf) + m_RemoteIdentity->GetSignatureLen ();
|
2014-10-30 22:13:29 +03:00
|
|
|
paddingSize &= 0x0F; // %16
|
|
|
|
if (paddingSize > 0) paddingSize = 16 - paddingSize;
|
|
|
|
payload += paddingSize;
|
2015-11-03 17:15:49 +03:00
|
|
|
// verify
|
|
|
|
if (m_SignedData && !m_SignedData->Verify (m_RemoteIdentity, payload))
|
|
|
|
LogPrint (eLogError, "Session confirmed SSU signature verification failed");
|
2015-06-24 17:45:58 +03:00
|
|
|
m_Data.Send (CreateDeliveryStatusMsg (0));
|
2014-10-30 22:13:29 +03:00
|
|
|
Established ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::SendSessionRequest ()
|
2015-11-03 17:15:49 +03:00
|
|
|
{
|
2014-10-30 22:13:29 +03:00
|
|
|
uint8_t buf[320 + 18]; // 304 bytes for ipv4, 320 for ipv6
|
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
2015-11-03 17:15:49 +03:00
|
|
|
memcpy (payload, m_DHKeysPair->GetPublicKey (), 256); // x
|
2014-10-30 22:13:29 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t iv[16];
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes (iv, 16); // random iv
|
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_REQUEST, buf, isV4 ? 304 : 320, m_IntroKey, iv, m_IntroKey);
|
2014-10-30 22:13:29 +03:00
|
|
|
m_Server.Send (buf, isV4 ? 304 : 320, m_RemoteEndpoint);
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
void SSUSession::SendRelayRequest (const i2p::data::RouterInfo::Introducer& introducer)
|
2014-10-30 22:13:29 +03:00
|
|
|
{
|
|
|
|
auto address = i2p::context.GetRouterInfo ().GetSSUAddress ();
|
|
|
|
if (!address)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU is not supported");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t buf[96 + 18];
|
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
2015-11-03 17:15:49 +03:00
|
|
|
htobe32buf (payload, introducer.iTag);
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 4;
|
|
|
|
*payload = 0; // no address
|
|
|
|
payload++;
|
2014-12-30 17:37:24 +03:00
|
|
|
htobuf16(payload, 0); // port = 0
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 2;
|
|
|
|
*payload = 0; // challenge
|
|
|
|
payload++;
|
|
|
|
memcpy (payload, (const uint8_t *)address->key, 32);
|
|
|
|
payload += 32;
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes (payload, 4); // nonce
|
2014-10-30 22:13:29 +03:00
|
|
|
|
|
|
|
uint8_t iv[16];
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes (iv, 16); // random iv
|
2014-10-30 22:13:29 +03:00
|
|
|
if (m_State == eSessionStateEstablished)
|
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_RELAY_REQUEST, buf, 96, m_SessionKey, iv, m_MacKey);
|
|
|
|
else
|
2015-11-03 17:15:49 +03:00
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_RELAY_REQUEST, buf, 96, introducer.iKey, iv, introducer.iKey);
|
2014-10-30 22:13:29 +03:00
|
|
|
m_Server.Send (buf, 96, m_RemoteEndpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::SendSessionCreated (const uint8_t * x)
|
|
|
|
{
|
|
|
|
auto address = IsV6 () ? i2p::context.GetRouterInfo ().GetSSUV6Address () :
|
|
|
|
i2p::context.GetRouterInfo ().GetSSUAddress (true); //v4 only
|
2015-11-03 17:15:49 +03:00
|
|
|
if (!address)
|
2014-10-30 22:13:29 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU is not supported");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SignedData s; // x,y, remote IP, remote port, our IP, our port, relayTag, signed on time
|
|
|
|
s.Insert (x, 256); // x
|
|
|
|
|
|
|
|
uint8_t buf[384 + 18];
|
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
2015-11-03 17:15:49 +03:00
|
|
|
memcpy (payload, m_DHKeysPair->GetPublicKey (), 256);
|
2014-10-30 22:13:29 +03:00
|
|
|
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;
|
|
|
|
}
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe16buf (payload, m_RemoteEndpoint.port ());
|
2014-10-30 22:13:29 +03:00
|
|
|
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
|
2015-02-25 06:17:46 +03:00
|
|
|
s.Insert<uint16_t> (htobe16 (address->port)); // our port
|
2014-10-30 22:13:29 +03:00
|
|
|
uint32_t relayTag = 0;
|
|
|
|
if (i2p::context.GetRouterInfo ().IsIntroducer ())
|
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes((uint8_t *)&relayTag, 4);
|
2014-10-30 22:13:29 +03:00
|
|
|
if (!relayTag) relayTag = 1;
|
|
|
|
m_Server.AddRelay (relayTag, m_RemoteEndpoint);
|
|
|
|
}
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (payload, relayTag);
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 4; // relay tag
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (payload, i2p::util::GetSecondsSinceEpoch ()); // signed on time
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 4;
|
2015-11-03 17:15:49 +03:00
|
|
|
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
|
2014-10-30 22:13:29 +03:00
|
|
|
s.Sign (i2p::context.GetPrivateKeys (), payload); // DSA signature
|
|
|
|
// TODO: fill padding with random data
|
|
|
|
|
|
|
|
uint8_t iv[16];
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes (iv, 16); // random iv
|
2014-10-30 22:13:29 +03:00
|
|
|
// encrypt signature and padding with newly created session key
|
2015-11-03 17:15:49 +03:00
|
|
|
size_t signatureLen = i2p::context.GetIdentity ()->GetSignatureLen ();
|
2014-10-30 22:13:29 +03:00
|
|
|
size_t paddingSize = signatureLen & 0x0F; // %16
|
|
|
|
if (paddingSize > 0) signatureLen += (16 - paddingSize);
|
|
|
|
m_SessionKeyEncryption.SetIV (iv);
|
|
|
|
m_SessionKeyEncryption.Encrypt (payload, signatureLen, payload);
|
|
|
|
payload += signatureLen;
|
|
|
|
size_t msgLen = payload - buf;
|
|
|
|
|
|
|
|
// encrypt message with intro key
|
2015-11-03 17:15:49 +03:00
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_CREATED, buf, msgLen, m_IntroKey, iv, m_IntroKey);
|
2014-10-30 22:13:29 +03:00
|
|
|
Send (buf, msgLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress, size_t ourAddressLen)
|
|
|
|
{
|
|
|
|
uint8_t buf[512 + 18];
|
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
|
|
|
*payload = 1; // 1 fragment
|
|
|
|
payload++; // info
|
2015-11-03 17:15:49 +03:00
|
|
|
size_t identLen = i2p::context.GetIdentity ()->GetFullLen (); // 387+ bytes
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe16buf (payload, identLen);
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 2; // cursize
|
2015-11-03 17:15:49 +03:00
|
|
|
i2p::context.GetIdentity ()->ToBuffer (payload, identLen);
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += identLen;
|
|
|
|
uint32_t signedOnTime = i2p::util::GetSecondsSinceEpoch ();
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (payload, signedOnTime); // signed on time
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 4;
|
2015-11-03 17:15:49 +03:00
|
|
|
auto signatureLen = i2p::context.GetIdentity ()->GetSignatureLen ();
|
2014-10-30 22:13:29 +03:00
|
|
|
size_t paddingSize = ((payload - buf) + signatureLen)%16;
|
|
|
|
if (paddingSize > 0) paddingSize = 16 - paddingSize;
|
|
|
|
// TODO: fill padding
|
|
|
|
payload += paddingSize; // padding size
|
|
|
|
|
|
|
|
// signature
|
|
|
|
SignedData s; // x,y, our IP, our port, remote IP, remote port, relayTag, our signed on time
|
2015-11-03 17:15:49 +03:00
|
|
|
s.Insert (m_DHKeysPair->GetPublicKey (), 256); // x
|
2014-10-30 22:13:29 +03:00
|
|
|
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
|
2015-02-25 06:17:46 +03:00
|
|
|
s.Insert<uint16_t> (htobe16 (m_RemoteEndpoint.port ())); // remote port
|
2014-10-30 22:13:29 +03:00
|
|
|
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];
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes (iv, 16); // random iv
|
2014-10-30 22:13:29 +03:00
|
|
|
// encrypt message with session key
|
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_CONFIRMED, buf, msgLen, m_SessionKey, iv, m_MacKey);
|
|
|
|
Send (buf, msgLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::ProcessRelayRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& from)
|
|
|
|
{
|
2014-12-30 01:04:02 +03:00
|
|
|
uint32_t relayTag = bufbe32toh (buf);
|
2014-10-30 22:13:29 +03:00
|
|
|
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;
|
|
|
|
uint8_t * introKey = buf;
|
|
|
|
buf += 32; // introkey
|
2014-12-30 01:04:02 +03:00
|
|
|
uint32_t nonce = bufbe32toh (buf);
|
2014-10-30 22:13:29 +03:00
|
|
|
SendRelayResponse (nonce, from, introKey, session->m_RemoteEndpoint);
|
2014-11-24 20:26:11 +03:00
|
|
|
SendRelayIntro (session.get (), from);
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::SendRelayResponse (uint32_t nonce, const boost::asio::ip::udp::endpoint& from,
|
|
|
|
const uint8_t * introKey, const boost::asio::ip::udp::endpoint& to)
|
|
|
|
{
|
|
|
|
uint8_t buf[80 + 18]; // 64 Alice's ipv4 and 80 Alice's ipv6
|
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
|
|
|
// Charlie's address always v4
|
|
|
|
if (!to.address ().is_v4 ())
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Charlie's IP must be v4");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*payload = 4;
|
|
|
|
payload++; // size
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (payload, to.address ().to_v4 ().to_ulong ()); // Charlie's IP
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 4; // address
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe16buf (payload, to.port ()); // Charlie's port
|
2014-10-30 22:13:29 +03:00
|
|
|
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
|
|
|
|
}
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe16buf (payload, from.port ()); // Alice's port
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 2; // port
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (payload, nonce);
|
2014-10-30 22:13:29 +03:00
|
|
|
|
|
|
|
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];
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes (iv, 16); // random iv
|
2014-10-30 22:13:29 +03:00
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_RELAY_RESPONSE, buf, isV4 ? 64 : 80, introKey, iv, introKey);
|
|
|
|
m_Server.Send (buf, isV4 ? 64 : 80, from);
|
|
|
|
}
|
|
|
|
LogPrint (eLogDebug, "SSU relay response sent");
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::SendRelayIntro (SSUSession * session, const boost::asio::ip::udp::endpoint& from)
|
|
|
|
{
|
|
|
|
if (!session) return;
|
|
|
|
// Alice's address always v4
|
|
|
|
if (!from.address ().is_v4 ())
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Alice's IP must be v4");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint8_t buf[48 + 18];
|
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
|
|
|
*payload = 4;
|
|
|
|
payload++; // size
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (payload, from.address ().to_v4 ().to_ulong ()); // Alice's IP
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 4; // address
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe16buf (payload, from.port ()); // Alice's port
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 2; // port
|
|
|
|
*payload = 0; // challenge size
|
|
|
|
uint8_t iv[16];
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes (iv, 16); // random iv
|
2014-10-30 22:13:29 +03:00
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_RELAY_INTRO, buf, 48, session->m_SessionKey, iv, session->m_MacKey);
|
|
|
|
m_Server.Send (buf, 48, session->m_RemoteEndpoint);
|
|
|
|
LogPrint (eLogDebug, "SSU relay intro sent");
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::ProcessRelayResponse (uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "Relay response received");
|
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
|
|
|
uint8_t remoteSize = *payload;
|
|
|
|
payload++; // remote size
|
2014-12-30 01:04:02 +03:00
|
|
|
//boost::asio::ip::address_v4 remoteIP (bufbe32toh (payload));
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += remoteSize; // remote address
|
2014-12-30 01:04:02 +03:00
|
|
|
//uint16_t remotePort = bufbe16toh (payload);
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 2; // remote port
|
|
|
|
uint8_t ourSize = *payload;
|
|
|
|
payload++; // our size
|
|
|
|
boost::asio::ip::address ourIP;
|
|
|
|
if (ourSize == 4)
|
|
|
|
{
|
|
|
|
boost::asio::ip::address_v4::bytes_type bytes;
|
|
|
|
memcpy (bytes.data (), payload, 4);
|
|
|
|
ourIP = boost::asio::ip::address_v4 (bytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
boost::asio::ip::address_v6::bytes_type bytes;
|
|
|
|
memcpy (bytes.data (), payload, 16);
|
|
|
|
ourIP = boost::asio::ip::address_v6 (bytes);
|
|
|
|
}
|
|
|
|
payload += ourSize; // our address
|
2014-12-30 01:04:02 +03:00
|
|
|
uint16_t ourPort = bufbe16toh (payload);
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 2; // our port
|
|
|
|
LogPrint ("Our external address is ", ourIP.to_string (), ":", ourPort);
|
|
|
|
i2p::context.UpdateAddress (ourIP);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::ProcessRelayIntro (uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
uint8_t size = *buf;
|
|
|
|
if (size == 4)
|
|
|
|
{
|
|
|
|
buf++; // size
|
2014-12-30 01:04:02 +03:00
|
|
|
boost::asio::ip::address_v4 address (bufbe32toh (buf));
|
2014-10-30 22:13:29 +03:00
|
|
|
buf += 4; // address
|
2014-12-30 01:04:02 +03:00
|
|
|
uint16_t port = bufbe16toh (buf);
|
2014-10-30 22:13:29 +03:00
|
|
|
// send hole punch of 1 byte
|
|
|
|
m_Server.Send (buf, 0, boost::asio::ip::udp::endpoint (address, port));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint (eLogWarning, "Address size ", size, " is not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len,
|
2015-11-03 17:15:49 +03:00
|
|
|
const i2p::crypto::AESKey& aesKey, const uint8_t * iv, const i2p::crypto::MACKey& macKey)
|
2014-10-30 22:13:29 +03:00
|
|
|
{
|
|
|
|
if (len < sizeof (SSUHeader))
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Unexpected SSU packet length ", len);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-31 17:14:53 +03:00
|
|
|
//TODO: we are using a dirty solution here but should work for now
|
2014-10-30 22:13:29 +03:00
|
|
|
SSUHeader * header = (SSUHeader *)buf;
|
|
|
|
memcpy (header->iv, iv, 16);
|
|
|
|
header->flag = payloadType << 4; // MSB is 0
|
2014-12-31 17:14:53 +03:00
|
|
|
htobe32buf (&(header->time), i2p::util::GetSecondsSinceEpoch ());
|
2014-10-30 22:13:29 +03:00
|
|
|
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);
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe16buf (buf + len + 16, encryptedLen);
|
2014-10-30 22:13:29 +03:00
|
|
|
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))
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Unexpected SSU packet length ", len);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-31 17:14:53 +03:00
|
|
|
//TODO: we are using a dirty solution here but should work for now
|
2014-10-30 22:13:29 +03:00
|
|
|
SSUHeader * header = (SSUHeader *)buf;
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes (header->iv, 16); // random iv
|
2014-10-30 22:13:29 +03:00
|
|
|
m_SessionKeyEncryption.SetIV (header->iv);
|
|
|
|
header->flag = payloadType << 4; // MSB is 0
|
2014-12-31 17:14:53 +03:00
|
|
|
htobe32buf (&(header->time), i2p::util::GetSecondsSinceEpoch ());
|
2014-10-30 22:13:29 +03:00
|
|
|
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);
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe16buf (buf + len + 16, encryptedLen);
|
2014-10-30 22:13:29 +03:00
|
|
|
i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, m_MacKey, header->mac);
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
void SSUSession::Decrypt (uint8_t * buf, size_t len, const i2p::crypto::AESKey& aesKey)
|
2014-10-30 22:13:29 +03:00
|
|
|
{
|
|
|
|
if (len < sizeof (SSUHeader))
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Unexpected SSU packet length ", len);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-31 17:14:53 +03:00
|
|
|
//TODO: since we are accessing a uint8_t this is unlikely to crash due to alignment but should be improved
|
2014-10-30 22:13:29 +03:00
|
|
|
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))
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Unexpected SSU packet length ", len);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-31 17:14:53 +03:00
|
|
|
//TODO: since we are accessing a uint8_t this is unlikely to crash due to alignment but should be improved
|
2014-10-30 22:13:29 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
bool SSUSession::Validate (uint8_t * buf, size_t len, const i2p::crypto::MACKey& macKey)
|
2014-10-30 22:13:29 +03:00
|
|
|
{
|
|
|
|
if (len < sizeof (SSUHeader))
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Unexpected SSU packet length ", len);
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-31 17:14:53 +03:00
|
|
|
//TODO: since we are accessing a uint8_t this is unlikely to crash due to alignment but should be improved
|
2014-10-30 22:13:29 +03:00
|
|
|
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);
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe16buf (buf + len + 16, encryptedLen);
|
2014-10-30 22:13:29 +03:00
|
|
|
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 ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-24 19:18:12 +03:00
|
|
|
void SSUSession::WaitForConnect ()
|
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
if (!IsOutgoing ()) // incoming session
|
2014-11-24 19:18:12 +03:00
|
|
|
ScheduleConnectTimer ();
|
|
|
|
else
|
|
|
|
LogPrint (eLogError, "SSU wait for connect for outgoing session");
|
|
|
|
}
|
|
|
|
|
2014-10-30 22:13:29 +03:00
|
|
|
void SSUSession::ScheduleConnectTimer ()
|
|
|
|
{
|
|
|
|
m_Timer.cancel ();
|
|
|
|
m_Timer.expires_from_now (boost::posix_time::seconds(SSU_CONNECT_TIMEOUT));
|
2014-11-24 20:26:11 +03:00
|
|
|
m_Timer.async_wait (std::bind (&SSUSession::HandleConnectTimer,
|
|
|
|
shared_from_this (), std::placeholders::_1));
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::HandleConnectTimer (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (!ecode)
|
|
|
|
{
|
|
|
|
// timeout expired
|
|
|
|
LogPrint ("SSU session was not established after ", SSU_CONNECT_TIMEOUT, " second");
|
|
|
|
Failed ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
void SSUSession::Introduce (const i2p::data::RouterInfo::Introducer& introducer)
|
2014-10-30 22:13:29 +03:00
|
|
|
{
|
|
|
|
if (m_State == eSessionStateUnknown)
|
|
|
|
{
|
|
|
|
// set connect timer
|
|
|
|
m_Timer.expires_from_now (boost::posix_time::seconds(SSU_CONNECT_TIMEOUT));
|
2014-11-24 20:26:11 +03:00
|
|
|
m_Timer.async_wait (std::bind (&SSUSession::HandleConnectTimer,
|
|
|
|
shared_from_this (), std::placeholders::_1));
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
2015-11-03 17:15:49 +03:00
|
|
|
SendRelayRequest (introducer);
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::WaitForIntroduction ()
|
|
|
|
{
|
|
|
|
m_State = eSessionStateIntroduced;
|
|
|
|
// set connect timer
|
|
|
|
m_Timer.expires_from_now (boost::posix_time::seconds(SSU_CONNECT_TIMEOUT));
|
2014-11-24 20:26:11 +03:00
|
|
|
m_Timer.async_wait (std::bind (&SSUSession::HandleConnectTimer,
|
|
|
|
shared_from_this (), std::placeholders::_1));
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::Close ()
|
|
|
|
{
|
2015-02-07 23:25:06 +03:00
|
|
|
m_State = eSessionStateClosed;
|
2014-10-30 22:13:29 +03:00
|
|
|
SendSesionDestroyed ();
|
2015-01-13 06:53:35 +03:00
|
|
|
transports.PeerDisconnected (shared_from_this ());
|
2015-02-08 01:58:29 +03:00
|
|
|
m_Data.Stop ();
|
2015-02-07 23:25:06 +03:00
|
|
|
m_Timer.cancel ();
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
|
2015-02-07 04:53:48 +03:00
|
|
|
void SSUSession::Done ()
|
2015-02-07 00:03:47 +03:00
|
|
|
{
|
2015-02-07 23:25:06 +03:00
|
|
|
GetService ().post (std::bind (&SSUSession::Failed, shared_from_this ()));
|
2015-02-07 00:03:47 +03:00
|
|
|
}
|
|
|
|
|
2014-10-30 22:13:29 +03:00
|
|
|
void SSUSession::Established ()
|
|
|
|
{
|
|
|
|
m_State = eSessionStateEstablished;
|
2015-11-03 17:15:49 +03:00
|
|
|
m_DHKeysPair = nullptr;
|
|
|
|
m_SignedData = nullptr;
|
2015-02-08 16:50:05 +03:00
|
|
|
m_Data.Start ();
|
2015-11-03 17:15:49 +03:00
|
|
|
m_Data.Send (CreateDatabaseStoreMsg ());
|
2015-01-13 06:53:35 +03:00
|
|
|
transports.PeerConnected (shared_from_this ());
|
2015-11-03 17:15:49 +03:00
|
|
|
if (m_IsPeerTest)
|
2014-10-30 22:13:29 +03:00
|
|
|
SendPeerTest ();
|
|
|
|
ScheduleTermination ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::Failed ()
|
|
|
|
{
|
|
|
|
if (m_State != eSessionStateFailed)
|
|
|
|
{
|
|
|
|
m_State = eSessionStateFailed;
|
2014-11-24 20:26:11 +03:00
|
|
|
m_Server.DeleteSession (shared_from_this ());
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::ScheduleTermination ()
|
|
|
|
{
|
|
|
|
m_Timer.cancel ();
|
|
|
|
m_Timer.expires_from_now (boost::posix_time::seconds(SSU_TERMINATION_TIMEOUT));
|
2014-11-24 20:26:11 +03:00
|
|
|
m_Timer.async_wait (std::bind (&SSUSession::HandleTerminationTimer,
|
|
|
|
shared_from_this (), std::placeholders::_1));
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::HandleTerminationTimer (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
LogPrint ("SSU no activity fo ", SSU_TERMINATION_TIMEOUT, " seconds");
|
|
|
|
Failed ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-17 17:47:26 +03:00
|
|
|
void SSUSession::SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs)
|
2015-01-21 05:05:57 +03:00
|
|
|
{
|
2015-02-07 23:25:06 +03:00
|
|
|
GetService ().post (std::bind (&SSUSession::PostI2NPMessages, shared_from_this (), msgs));
|
2015-01-21 05:05:57 +03:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:47:26 +03:00
|
|
|
void SSUSession::PostI2NPMessages (std::vector<std::shared_ptr<I2NPMessage> > msgs)
|
2015-01-21 05:05:57 +03:00
|
|
|
{
|
2015-02-10 00:33:52 +03:00
|
|
|
if (m_State == eSessionStateEstablished)
|
|
|
|
{
|
|
|
|
for (auto it: msgs)
|
|
|
|
if (it) m_Data.Send (it);
|
|
|
|
}
|
2015-01-21 05:05:57 +03:00
|
|
|
}
|
|
|
|
|
2014-10-30 22:13:29 +03:00
|
|
|
void SSUSession::ProcessData (uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
m_Data.ProcessMessage (buf, len);
|
2015-02-15 22:17:55 +03:00
|
|
|
m_IsDataReceived = true;
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
|
2015-02-15 22:17:55 +03:00
|
|
|
void SSUSession::FlushData ()
|
|
|
|
{
|
|
|
|
if (m_IsDataReceived)
|
|
|
|
{
|
|
|
|
m_Data.FlushReceivedMessage ();
|
|
|
|
m_IsDataReceived = false;
|
|
|
|
}
|
|
|
|
}
|
2014-10-30 22:13:29 +03:00
|
|
|
|
2015-03-26 21:35:20 +03:00
|
|
|
void SSUSession::ProcessPeerTest (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
|
2014-10-30 22:13:29 +03:00
|
|
|
{
|
2015-03-26 22:05:52 +03:00
|
|
|
uint32_t nonce = bufbe32toh (buf); // 4 bytes
|
|
|
|
uint8_t size = buf[4]; // 1 byte
|
|
|
|
uint32_t address = (size == 4) ? buf32toh(buf + 5) : 0; // big endian, size bytes
|
|
|
|
uint16_t port = buf16toh(buf + size + 5); // big endian, 2 bytes
|
|
|
|
const uint8_t * introKey = buf + size + 7;
|
2014-10-30 22:13:29 +03:00
|
|
|
if (port && !address)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "Address of ", size, " bytes not supported");
|
|
|
|
return;
|
|
|
|
}
|
2015-02-26 05:56:51 +03:00
|
|
|
switch (m_Server.GetPeerTestParticipant (nonce))
|
|
|
|
{
|
2015-02-26 00:39:48 +03:00
|
|
|
// existing test
|
2015-02-26 05:56:51 +03:00
|
|
|
case ePeerTestParticipantAlice1:
|
|
|
|
{
|
2015-02-26 00:39:48 +03:00
|
|
|
if (m_State == eSessionStateEstablished)
|
2015-02-26 21:44:18 +03:00
|
|
|
{
|
2015-02-26 00:39:48 +03:00
|
|
|
LogPrint (eLogDebug, "SSU peer test from Bob. We are Alice");
|
2015-02-26 21:44:18 +03:00
|
|
|
if (i2p::context.GetStatus () == eRouterStatusTesting) // still not OK
|
|
|
|
i2p::context.SetStatus (eRouterStatusFirewalled);
|
|
|
|
}
|
2015-02-26 00:39:48 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "SSU first peer test from Charlie. We are Alice");
|
2015-02-26 21:44:18 +03:00
|
|
|
i2p::context.SetStatus (eRouterStatusOK);
|
2015-02-26 05:56:51 +03:00
|
|
|
m_Server.UpdatePeerTest (nonce, ePeerTestParticipantAlice2);
|
2015-02-26 00:39:48 +03:00
|
|
|
SendPeerTest (nonce, senderEndpoint.address ().to_v4 ().to_ulong (),
|
|
|
|
senderEndpoint.port (), introKey, true, false); // to Charlie
|
|
|
|
}
|
2015-02-26 05:56:51 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ePeerTestParticipantAlice2:
|
|
|
|
{
|
|
|
|
if (m_State == eSessionStateEstablished)
|
|
|
|
LogPrint (eLogDebug, "SSU peer test from Bob. We are Alice");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// peer test successive
|
|
|
|
LogPrint (eLogDebug, "SSU second peer test from Charlie. We are Alice");
|
2015-02-26 21:44:18 +03:00
|
|
|
i2p::context.SetStatus (eRouterStatusOK);
|
2015-02-26 05:56:51 +03:00
|
|
|
m_Server.RemovePeerTest (nonce);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ePeerTestParticipantBob:
|
2014-10-30 22:13:29 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "SSU peer test from Charlie. We are Bob");
|
2015-03-26 23:10:52 +03:00
|
|
|
auto session = m_Server.GetPeerTestSession (nonce); // session with Alice from PeerTest
|
|
|
|
if (session && session->m_State == eSessionStateEstablished)
|
2015-03-26 22:05:52 +03:00
|
|
|
session->Send (PAYLOAD_TYPE_PEER_TEST, buf, len); // back to Alice
|
2015-03-26 21:35:20 +03:00
|
|
|
m_Server.RemovePeerTest (nonce); // nonce has been used
|
2015-02-26 05:56:51 +03:00
|
|
|
break;
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
2015-02-26 05:56:51 +03:00
|
|
|
case ePeerTestParticipantCharlie:
|
|
|
|
{
|
2014-10-30 22:13:29 +03:00
|
|
|
LogPrint (eLogDebug, "SSU peer test from Alice. We are Charlie");
|
|
|
|
SendPeerTest (nonce, senderEndpoint.address ().to_v4 ().to_ulong (),
|
2015-02-26 00:39:48 +03:00
|
|
|
senderEndpoint.port (), introKey); // to Alice with her actual address
|
2015-03-26 21:35:20 +03:00
|
|
|
m_Server.RemovePeerTest (nonce); // nonce has been used
|
2015-02-26 05:56:51 +03:00
|
|
|
break;
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
2015-02-26 05:56:51 +03:00
|
|
|
// test not found
|
|
|
|
case ePeerTestParticipantUnknown:
|
2014-10-30 22:13:29 +03:00
|
|
|
{
|
2015-02-26 05:56:51 +03:00
|
|
|
if (m_State == eSessionStateEstablished)
|
2014-10-30 22:13:29 +03:00
|
|
|
{
|
2015-02-26 05:56:51 +03:00
|
|
|
// new test
|
|
|
|
if (port)
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "SSU peer test from Bob. We are Charlie");
|
|
|
|
m_Server.NewPeerTest (nonce, ePeerTestParticipantCharlie);
|
2015-03-26 22:05:52 +03:00
|
|
|
Send (PAYLOAD_TYPE_PEER_TEST, buf, len); // back to Bob
|
2015-02-26 05:56:51 +03:00
|
|
|
SendPeerTest (nonce, be32toh (address), be16toh (port), introKey); // to Alice with her address received from Bob
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "SSU peer test from Alice. We are Bob");
|
|
|
|
auto session = m_Server.GetRandomEstablishedSession (shared_from_this ()); // Charlie
|
|
|
|
if (session)
|
|
|
|
{
|
2015-03-26 23:10:52 +03:00
|
|
|
m_Server.NewPeerTest (nonce, ePeerTestParticipantBob, shared_from_this ());
|
2015-02-26 05:56:51 +03:00
|
|
|
session->SendPeerTest (nonce, senderEndpoint.address ().to_v4 ().to_ulong (),
|
|
|
|
senderEndpoint.port (), introKey, false); // to Charlie with Alice's actual address
|
|
|
|
}
|
|
|
|
}
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
else
|
2015-02-26 05:56:51 +03:00
|
|
|
LogPrint (eLogError, "SSU unexpected peer test");
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::SendPeerTest (uint32_t nonce, uint32_t address, uint16_t port,
|
2015-02-26 00:39:48 +03:00
|
|
|
const uint8_t * introKey, bool toAddress, bool sendAddress)
|
|
|
|
// toAddress is true for Alice<->Chalie communications only
|
|
|
|
// sendAddress is false if message comes from Alice
|
2014-10-30 22:13:29 +03:00
|
|
|
{
|
|
|
|
uint8_t buf[80 + 18];
|
|
|
|
uint8_t iv[16];
|
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (payload, nonce);
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 4; // nonce
|
2015-02-26 00:39:48 +03:00
|
|
|
// address and port
|
|
|
|
if (sendAddress && address)
|
2014-10-30 22:13:29 +03:00
|
|
|
{
|
|
|
|
*payload = 4;
|
|
|
|
payload++; // size
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (payload, address);
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 4; // address
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*payload = 0;
|
|
|
|
payload++; //size
|
|
|
|
}
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe16buf (payload, port);
|
2014-10-30 22:13:29 +03:00
|
|
|
payload += 2; // port
|
2015-02-26 00:39:48 +03:00
|
|
|
// intro key
|
|
|
|
if (toAddress)
|
|
|
|
{
|
|
|
|
// send our intro key to address instead it's own
|
|
|
|
auto addr = i2p::context.GetRouterInfo ().GetSSUAddress ();
|
|
|
|
if (addr)
|
|
|
|
memcpy (payload, addr->key, 32); // intro key
|
|
|
|
else
|
|
|
|
LogPrint (eLogError, "SSU is not supported. Can't send peer test");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
memcpy (payload, introKey, 32); // intro key
|
2014-10-30 22:13:29 +03:00
|
|
|
|
2015-02-26 00:39:48 +03:00
|
|
|
// send
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes (iv, 16); // random iv
|
2014-10-30 22:13:29 +03:00
|
|
|
if (toAddress)
|
|
|
|
{
|
|
|
|
// encrypt message with specified intro key
|
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_PEER_TEST, buf, 80, introKey, iv, introKey);
|
|
|
|
boost::asio::ip::udp::endpoint e (boost::asio::ip::address_v4 (address), port);
|
|
|
|
m_Server.Send (buf, 80, e);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// encrypt message with session key
|
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_PEER_TEST, buf, 80);
|
|
|
|
Send (buf, 80);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::SendPeerTest ()
|
|
|
|
{
|
2015-02-25 23:26:06 +03:00
|
|
|
// we are Alice
|
2014-10-30 22:13:29 +03:00
|
|
|
LogPrint (eLogDebug, "SSU sending peer test");
|
|
|
|
auto address = i2p::context.GetRouterInfo ().GetSSUAddress ();
|
|
|
|
if (!address)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU is not supported. Can't send peer test");
|
|
|
|
return;
|
|
|
|
}
|
2015-11-03 17:15:49 +03:00
|
|
|
uint32_t nonce;
|
|
|
|
RAND_bytes ((uint8_t *)&nonce, 4);
|
2014-10-30 22:13:29 +03:00
|
|
|
if (!nonce) nonce = 1;
|
2015-11-03 17:15:49 +03:00
|
|
|
m_IsPeerTest = false;
|
2015-02-26 05:56:51 +03:00
|
|
|
m_Server.NewPeerTest (nonce, ePeerTestParticipantAlice1);
|
2015-02-26 00:39:48 +03:00
|
|
|
SendPeerTest (nonce, 0, 0, address->key, false, false); // address and port always zero for Alice
|
2014-10-30 22:13:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::SendKeepAlive ()
|
|
|
|
{
|
|
|
|
if (m_State == eSessionStateEstablished)
|
|
|
|
{
|
|
|
|
uint8_t buf[48 + 18];
|
|
|
|
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);
|
|
|
|
LogPrint (eLogDebug, "SSU keep-alive sent");
|
|
|
|
ScheduleTermination ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::SendSesionDestroyed ()
|
|
|
|
{
|
|
|
|
if (m_IsSessionKey)
|
|
|
|
{
|
|
|
|
uint8_t buf[48 + 18];
|
|
|
|
// encrypt message with session key
|
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_DESTROYED, buf, 48);
|
2015-02-27 21:07:32 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Send (buf, 48);
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SSU send session destoriyed exception ", ex.what ());
|
|
|
|
}
|
2014-10-30 22:13:29 +03:00
|
|
|
LogPrint (eLogDebug, "SSU session destroyed sent");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSUSession::Send (uint8_t type, const uint8_t * payload, size_t len)
|
|
|
|
{
|
|
|
|
uint8_t buf[SSU_MTU_V4 + 18];
|
|
|
|
size_t msgSize = len + sizeof (SSUHeader);
|
2015-03-25 16:04:19 +03:00
|
|
|
size_t paddingSize = msgSize & 0x0F; // %16
|
2015-03-18 20:30:38 +03:00
|
|
|
if (paddingSize > 0) msgSize += (16 - paddingSize);
|
2014-10-30 22:13:29 +03:00
|
|
|
if (msgSize > SSU_MTU_V4)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "SSU payload size ", msgSize, " exceeds MTU");
|
|
|
|
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;
|
2015-03-17 02:33:59 +03:00
|
|
|
i2p::transport::transports.UpdateSentBytes (size);
|
2014-10-30 22:13:29 +03:00
|
|
|
m_Server.Send (buf, size, m_RemoteEndpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|