Browse Source

send SessionCreated

pull/1742/head
orignal 3 years ago
parent
commit
7faa732f38
  1. 75
      libi2pd/SSU2.cpp
  2. 19
      libi2pd/SSU2.h

75
libi2pd/SSU2.cpp

@ -30,10 +30,11 @@ namespace transport
m_Server (server), m_Address (addr), m_DestConnID (0), m_SourceConnID (0) m_Server (server), m_Address (addr), m_DestConnID (0), m_SourceConnID (0)
{ {
m_NoiseState.reset (new i2p::crypto::NoiseSymmetricState); m_NoiseState.reset (new i2p::crypto::NoiseSymmetricState);
if (in_RemoteRouter && addr) if (in_RemoteRouter && m_Address)
{ {
// outgoing // outgoing
InitNoiseXKState1 (*m_NoiseState, addr->s); InitNoiseXKState1 (*m_NoiseState, m_Address->s);
m_RemoteEndpoint = boost::asio::ip::udp::endpoint (m_Address->host, m_Address->port);
} }
else else
{ {
@ -48,6 +49,7 @@ namespace transport
void SSU2Session::SendSessionRequest () void SSU2Session::SendSessionRequest ()
{ {
// we are Alice
m_EphemeralKeys = i2p::transport::transports.GetNextX25519KeysPair (); m_EphemeralKeys = i2p::transport::transports.GetNextX25519KeysPair ();
Header header; Header header;
@ -56,11 +58,11 @@ namespace transport
// fill packet // fill packet
RAND_bytes ((uint8_t *)&m_DestConnID, 8); RAND_bytes ((uint8_t *)&m_DestConnID, 8);
header.h.connID = m_DestConnID; // dest id header.h.connID = m_DestConnID; // dest id
memset (header.h.h2.h.packetNum, 0, 4); memset (header.h.packetNum, 0, 4);
header.h.h2.h.type = eSSU2SessionRequest; header.h.type = eSSU2SessionRequest;
header.h.h2.h.flags[0] = 2; // ver header.h.flags[0] = 2; // ver
header.h.h2.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID
header.h.h2.h.flags[2] = 0; // flag header.h.flags[2] = 0; // flag
RAND_bytes ((uint8_t *)&m_SourceConnID, 8); RAND_bytes ((uint8_t *)&m_SourceConnID, 8);
memcpy (headerX, &m_SourceConnID, 8); // source id memcpy (headerX, &m_SourceConnID, 8); // source id
RAND_bytes (headerX + 8, 8); // token RAND_bytes (headerX + 8, 8); // token
@ -75,13 +77,13 @@ namespace transport
// encrypt // encrypt
const uint8_t nonce[12] = {0}; const uint8_t nonce[12] = {0};
i2p::crypto::AEADChaCha20Poly1305 (payload, payloadSize, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, payload, payloadSize + 16, true); i2p::crypto::AEADChaCha20Poly1305 (payload, payloadSize, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, payload, payloadSize + 16, true);
payloadSize += 16;
header.ll[0] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 24)); header.ll[0] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 24));
header.ll[1] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 12)); header.ll[1] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 12));
i2p::crypto::ChaCha20 (headerX, 48, m_Address->i, nonce, headerX); i2p::crypto::ChaCha20 (headerX, 48, m_Address->i, nonce, headerX);
payloadSize += 16;
m_NoiseState->MixHash (payload, 32); // h = SHA256(h || 32 byte encrypted payload from Session Request) for SessionCreated m_NoiseState->MixHash (payload, 32); // h = SHA256(h || 32 byte encrypted payload from Session Request) for SessionCreated
// send // send
m_Server.Send (header.buf, 16, headerX, 48, payload, payloadSize, boost::asio::ip::udp::endpoint (m_Address->host, m_Address->port)); m_Server.Send (header.buf, 16, headerX, 48, payload, payloadSize, m_RemoteEndpoint);
} }
void SSU2Session::ProcessSessionRequest (uint64_t connID, uint8_t * buf, size_t len) void SSU2Session::ProcessSessionRequest (uint64_t connID, uint8_t * buf, size_t len)
@ -90,11 +92,11 @@ namespace transport
m_SourceConnID = connID; m_SourceConnID = connID;
Header header; Header header;
header.h.connID = connID; header.h.connID = connID;
memcpy (header.h.h2.buf, buf, 8); memcpy (header.buf + 8, buf + 8, 8);
header.h.h2.ll ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), buf + (len - 12)); header.ll[1] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), buf + (len - 12));
if (header.h.h2.h.type != eSSU2SessionRequest) if (header.h.type != eSSU2SessionRequest)
{ {
LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.h2.h.type); LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type);
return; return;
} }
const uint8_t nonce[12] = {0}; const uint8_t nonce[12] = {0};
@ -117,6 +119,44 @@ namespace transport
// process payload // process payload
m_Server.AddSession (m_SourceConnID, shared_from_this ()); m_Server.AddSession (m_SourceConnID, shared_from_this ());
SendSessionCreated (headerX + 16);
}
void SSU2Session::SendSessionCreated (const uint8_t * X)
{
// we are Bob
m_EphemeralKeys = i2p::transport::transports.GetNextX25519KeysPair ();
// fill packet
Header header;
uint8_t headerX[48], payload[1200]; // TODO: correct payload size
size_t payloadSize = 8;
header.h.connID = m_DestConnID; // dest id
memset (header.h.packetNum, 0, 4);
header.h.type = eSSU2SessionCreated;
header.h.flags[0] = 2; // ver
header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID
header.h.flags[2] = 0; // flag
memcpy (headerX, &m_SourceConnID, 8); // source id
RAND_bytes (headerX + 8, 8); // token
memcpy (headerX + 16, m_EphemeralKeys->GetPublicKey (), 32); // Y
// KDF for SessionCreated
m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header)
m_NoiseState->MixHash (headerX + 16, 32); // h = SHA256(h || bepk);
uint8_t sharedSecret[32];
m_EphemeralKeys->Agree (X, sharedSecret);
m_NoiseState->MixKey (sharedSecret);
// encrypt
const uint8_t nonce[12] = {0};
i2p::crypto::AEADChaCha20Poly1305 (payload, payloadSize, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, payload, payloadSize + 16, true);
payloadSize += 16;
header.ll[0] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), payload + (payloadSize - 24));
uint8_t kh2[32];
i2p::crypto::HKDF (m_NoiseState->m_CK, nullptr, 0, "SessCreateHeader", kh2, 32); // k_header_2 = HKDF(chainKey, ZEROLEN, "SessCreateHeader", 32)
header.ll[1] ^= CreateHeaderMask (kh2, payload + (payloadSize - 12));
i2p::crypto::ChaCha20 (headerX, 48, kh2, nonce, headerX);
// send
m_Server.Send (header.buf, 16, headerX, 48, payload, payloadSize, m_RemoteEndpoint);
} }
bool SSU2Session::ProcessSessionCreated (uint8_t * buf, size_t len) bool SSU2Session::ProcessSessionCreated (uint8_t * buf, size_t len)
@ -124,13 +164,13 @@ namespace transport
// we are Alice // we are Alice
Header header; Header header;
header.h.connID = m_SourceConnID; header.h.connID = m_SourceConnID;
memcpy (header.h.h2.buf, buf, 8); memcpy (header.buf + 8, buf + 8, 8);
uint8_t kh2[32]; uint8_t kh2[32];
i2p::crypto::HKDF (m_NoiseState->m_CK, nullptr, 0, "SessCreateHeader", kh2, 32); // k_header_2 = HKDF(chainKey, ZEROLEN, "SessCreateHeader", 32) i2p::crypto::HKDF (m_NoiseState->m_CK, nullptr, 0, "SessCreateHeader", kh2, 32); // k_header_2 = HKDF(chainKey, ZEROLEN, "SessCreateHeader", 32)
header.h.h2.ll ^= CreateHeaderMask (kh2, buf + (len - 12)); header.ll[1] ^= CreateHeaderMask (kh2, buf + (len - 12));
if (header.h.h2.h.type != eSSU2SessionCreated) if (header.h.type != eSSU2SessionCreated)
{ {
LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.h2.h.type); LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type);
return false; return false;
} }
const uint8_t nonce[12] = {0}; const uint8_t nonce[12] = {0};
@ -210,6 +250,7 @@ namespace transport
{ {
// assume new incoming session // assume new incoming session
auto session = std::make_shared<SSU2Session> (*this); auto session = std::make_shared<SSU2Session> (*this);
session->SetRemoteEndpoint (senderEndpoint);
session->ProcessSessionRequest (connID, buf, len); session->ProcessSessionRequest (connID, buf, len);
} }
} }

19
libi2pd/SSU2.h

@ -34,17 +34,6 @@ namespace transport
class SSU2Server; class SSU2Server;
class SSU2Session: public TransportSession, public std::enable_shared_from_this<SSU2Session> class SSU2Session: public TransportSession, public std::enable_shared_from_this<SSU2Session>
{ {
union Header2
{
uint64_t ll;
uint8_t buf[8];
struct
{
uint8_t packetNum[4];
uint8_t type;
uint8_t flags[3];
} h;
};
union Header union Header
{ {
uint64_t ll[2]; uint64_t ll[2];
@ -52,7 +41,9 @@ namespace transport
struct struct
{ {
uint64_t connID; uint64_t connID;
Header2 h2; uint8_t packetNum[4];
uint8_t type;
uint8_t flags[3];
} h; } h;
}; };
@ -62,6 +53,8 @@ namespace transport
std::shared_ptr<const i2p::data::RouterInfo::Address> addr = nullptr, bool peerTest = false); std::shared_ptr<const i2p::data::RouterInfo::Address> addr = nullptr, bool peerTest = false);
~SSU2Session (); ~SSU2Session ();
void SetRemoteEndpoint (const boost::asio::ip::udp::endpoint& ep) { m_RemoteEndpoint = ep; };
void Done () override {}; void Done () override {};
void SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs) override {}; void SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs) override {};
@ -71,6 +64,7 @@ namespace transport
private: private:
void SendSessionRequest (); void SendSessionRequest ();
void SendSessionCreated (const uint8_t * X);
private: private:
@ -78,6 +72,7 @@ namespace transport
std::shared_ptr<i2p::crypto::X25519Keys> m_EphemeralKeys; std::shared_ptr<i2p::crypto::X25519Keys> m_EphemeralKeys;
std::unique_ptr<i2p::crypto::NoiseSymmetricState> m_NoiseState; std::unique_ptr<i2p::crypto::NoiseSymmetricState> m_NoiseState;
std::shared_ptr<const i2p::data::RouterInfo::Address> m_Address; std::shared_ptr<const i2p::data::RouterInfo::Address> m_Address;
boost::asio::ip::udp::endpoint m_RemoteEndpoint;
uint64_t m_DestConnID, m_SourceConnID; uint64_t m_DestConnID, m_SourceConnID;
}; };

Loading…
Cancel
Save