|
|
|
@ -1045,6 +1045,12 @@ namespace transport
@@ -1045,6 +1045,12 @@ namespace transport
|
|
|
|
|
void SSU2Session::HandleRelayIntro (const uint8_t * buf, size_t len) |
|
|
|
|
{ |
|
|
|
|
// we are Charlie
|
|
|
|
|
auto r = i2p::data::netdb.FindRouter (buf + 1); // Alice
|
|
|
|
|
if (!r) |
|
|
|
|
{ |
|
|
|
|
LogPrint (eLogError, "SSU2: RelayIntro unknown router to introduce"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
SignedData s; |
|
|
|
|
s.Insert ((const uint8_t *)"RelayRequestData", 16); // prologue
|
|
|
|
|
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
|
|
|
@ -1052,14 +1058,19 @@ namespace transport
@@ -1052,14 +1058,19 @@ namespace transport
|
|
|
|
|
s.Insert (buf + 33, 14); // nonce, relay tag, timestamp, ver, asz
|
|
|
|
|
uint8_t asz = buf[46]; |
|
|
|
|
s.Insert (buf + 47, asz); // Alice Port, Alice IP
|
|
|
|
|
if (!s.Verify (GetRemoteIdentity (), buf + 47 + asz)) |
|
|
|
|
if (!s.Verify (r->GetIdentity (), buf + 47 + asz)) |
|
|
|
|
{ |
|
|
|
|
LogPrint (eLogWarning, "SSU2: RelayIntro signature verification failed"); |
|
|
|
|
return; // TODO: send relay response
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: send RelayResponse to Bob
|
|
|
|
|
// send relay response to Bob
|
|
|
|
|
uint8_t payload[SSU2_MTU]; |
|
|
|
|
size_t payloadSize = CreateRelayResponseBlock (payload, SSU2_MTU, bufbe32toh (buf + 33), bufbe32toh (buf + 37)); |
|
|
|
|
payloadSize += CreatePaddingBlock (payload + payloadSize, SSU2_MTU - payloadSize); |
|
|
|
|
SendData (payload, payloadSize); |
|
|
|
|
|
|
|
|
|
// send HolePunch
|
|
|
|
|
boost::asio::ip::udp::endpoint ep; |
|
|
|
|
if (ExtractEndpoint (buf + 47, asz, ep)) |
|
|
|
|
m_Server.SendHolePunch (ep); |
|
|
|
@ -1089,21 +1100,20 @@ namespace transport
@@ -1089,21 +1100,20 @@ namespace transport
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
size_t SSU2Session::CreateAddressBlock (const boost::asio::ip::udp::endpoint& ep, uint8_t * buf, size_t len) |
|
|
|
|
size_t SSU2Session::CreateEndpoint (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& ep) |
|
|
|
|
{ |
|
|
|
|
if (len < 9) return 0; |
|
|
|
|
buf[0] = eSSU2BlkAddress; |
|
|
|
|
htobe16buf (buf + 3, ep.port ()); |
|
|
|
|
if (len < 6) return 0; |
|
|
|
|
htobe16buf (buf, ep.port ()); |
|
|
|
|
size_t size = 0; |
|
|
|
|
if (ep.address ().is_v4 ()) |
|
|
|
|
{ |
|
|
|
|
memcpy (buf + 5, ep.address ().to_v4 ().to_bytes ().data (), 4); |
|
|
|
|
memcpy (buf + 2, ep.address ().to_v4 ().to_bytes ().data (), 4); |
|
|
|
|
size = 6; |
|
|
|
|
} |
|
|
|
|
else if (ep.address ().is_v6 ()) |
|
|
|
|
{ |
|
|
|
|
if (len < 21) return 0; |
|
|
|
|
memcpy (buf + 5, ep.address ().to_v6 ().to_bytes ().data (), 16); |
|
|
|
|
if (len < 18) return 0; |
|
|
|
|
memcpy (buf + 2, ep.address ().to_v6 ().to_bytes ().data (), 16); |
|
|
|
|
size = 18; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
@ -1111,6 +1121,15 @@ namespace transport
@@ -1111,6 +1121,15 @@ namespace transport
|
|
|
|
|
LogPrint (eLogWarning, "SSU2: Wrong address type ", ep.address ().to_string ()); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
return size; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
size_t SSU2Session::CreateAddressBlock (const boost::asio::ip::udp::endpoint& ep, uint8_t * buf, size_t len) |
|
|
|
|
{ |
|
|
|
|
if (len < 9) return 0; |
|
|
|
|
buf[0] = eSSU2BlkAddress; |
|
|
|
|
size_t size = CreateEndpoint (buf + 3, len - 3, ep); |
|
|
|
|
if (!size) return 0; |
|
|
|
|
htobe16buf (buf + 1, size); |
|
|
|
|
return size + 3; |
|
|
|
|
} |
|
|
|
@ -1241,6 +1260,29 @@ namespace transport
@@ -1241,6 +1260,29 @@ namespace transport
|
|
|
|
|
return payloadSize + 3; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
size_t SSU2Session::CreateRelayResponseBlock (uint8_t * buf, size_t len, uint32_t nonce, uint32_t relayTag) |
|
|
|
|
{ |
|
|
|
|
buf[0] = eSSU2BlkRelayResponse; |
|
|
|
|
buf[3] = 0; // flag
|
|
|
|
|
buf[4] = 0; // code, accept
|
|
|
|
|
htobe32buf (buf + 5, nonce); // nonce
|
|
|
|
|
htobe32buf (buf + 9, relayTag); // relayTag
|
|
|
|
|
htobe32buf (buf + 13, i2p::util::GetSecondsSinceEpoch ()); // timestamp
|
|
|
|
|
buf[17] = 2; // ver
|
|
|
|
|
size_t csz = CreateEndpoint (buf + 19, len - 19, boost::asio::ip::udp::endpoint (m_Address->host, m_Address->port)); |
|
|
|
|
if (!csz) return 0; |
|
|
|
|
buf[18] = csz; // csz
|
|
|
|
|
// signature
|
|
|
|
|
SignedData s; |
|
|
|
|
s.Insert ((const uint8_t *)"RelayAgreementOK", 16); // prologue
|
|
|
|
|
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
|
|
|
|
s.Insert (buf + 9, 10 + csz); // relay tag, timestamp, ver, csz and Charlie's endpoint
|
|
|
|
|
s.Sign (i2p::context.GetPrivateKeys (), buf + 19 + csz); |
|
|
|
|
size_t payloadSize = 16 + csz + i2p::context.GetIdentity ()->GetSignatureLen (); |
|
|
|
|
htobe16buf (buf + 1, payloadSize); // size
|
|
|
|
|
return payloadSize + 3; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> SSU2Session::ExtractRouterInfo (const uint8_t * buf, size_t size) |
|
|
|
|
{ |
|
|
|
|
if (size < 2) return nullptr; |
|
|
|
|