mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-03-21 16:49:10 +01:00
process intro key message
This commit is contained in:
parent
45d1571559
commit
64f195868e
2 changed files with 43 additions and 27 deletions
54
SSU.cpp
54
SSU.cpp
|
@ -59,10 +59,10 @@ namespace ssu
|
||||||
// most common case
|
// most common case
|
||||||
ProcessMessage (buf, len, senderEndpoint);
|
ProcessMessage (buf, len, senderEndpoint);
|
||||||
break;
|
break;
|
||||||
// establishing
|
// establishing or testing
|
||||||
case eSessionStateUnknown:
|
case eSessionStateUnknown:
|
||||||
// session request
|
// we must use intro key
|
||||||
ProcessSessionRequest (buf, len, senderEndpoint);
|
ProcessIntroKeyMessage (buf, len, senderEndpoint);
|
||||||
break;
|
break;
|
||||||
case eSessionStateRequestSent:
|
case eSessionStateRequestSent:
|
||||||
// session created
|
// session created
|
||||||
|
@ -98,8 +98,7 @@ namespace ssu
|
||||||
{
|
{
|
||||||
Decrypt (buf, len, m_SessionKey);
|
Decrypt (buf, len, m_SessionKey);
|
||||||
SSUHeader * header = (SSUHeader *)buf;
|
SSUHeader * header = (SSUHeader *)buf;
|
||||||
uint8_t payloadType = header->flag >> 4;
|
switch (header->GetPayloadType ())
|
||||||
switch (payloadType)
|
|
||||||
{
|
{
|
||||||
case PAYLOAD_TYPE_DATA:
|
case PAYLOAD_TYPE_DATA:
|
||||||
LogPrint ("SSU data received");
|
LogPrint ("SSU data received");
|
||||||
|
@ -120,7 +119,7 @@ namespace ssu
|
||||||
// TODO:
|
// TODO:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
LogPrint ("Unexpected SSU payload type ", (int)payloadType);
|
LogPrint ("Unexpected SSU payload type ", (int)header->GetPayloadType ());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -140,17 +139,30 @@ namespace ssu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SSUSession::ProcessSessionRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
|
void SSUSession::ProcessIntroKeyMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
|
||||||
{
|
{
|
||||||
LogPrint ("Process session request");
|
if (ProcessIntroKeyEncryptedMessage (buf, len))
|
||||||
// use our intro key
|
{
|
||||||
if (ProcessIntroKeyEncryptedMessage (PAYLOAD_TYPE_SESSION_REQUEST, buf, len))
|
SSUHeader * header = (SSUHeader *)buf;
|
||||||
|
switch (header->GetPayloadType ())
|
||||||
|
{
|
||||||
|
case PAYLOAD_TYPE_SESSION_REQUEST:
|
||||||
|
ProcessSessionRequest (buf + sizeof (SSUHeader), len - sizeof (SSUHeader), senderEndpoint);
|
||||||
|
break;
|
||||||
|
case PAYLOAD_TYPE_PEER_TEST:
|
||||||
|
// TODO
|
||||||
|
break;
|
||||||
|
default: ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SSUSession::ProcessSessionRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
|
||||||
{
|
{
|
||||||
m_State = eSessionStateRequestReceived;
|
m_State = eSessionStateRequestReceived;
|
||||||
LogPrint ("Session request received");
|
LogPrint ("Session request received");
|
||||||
m_RemoteEndpoint = senderEndpoint;
|
m_RemoteEndpoint = senderEndpoint;
|
||||||
SendSessionCreated (buf + sizeof (SSUHeader));
|
SendSessionCreated (buf);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SSUSession::ProcessSessionCreated (uint8_t * buf, size_t len)
|
void SSUSession::ProcessSessionCreated (uint8_t * buf, size_t len)
|
||||||
|
@ -163,8 +175,14 @@ namespace ssu
|
||||||
}
|
}
|
||||||
|
|
||||||
// use remote intro key
|
// use remote intro key
|
||||||
if (ProcessIntroKeyEncryptedMessage (PAYLOAD_TYPE_SESSION_CREATED, buf, len))
|
if (ProcessIntroKeyEncryptedMessage (buf, len))
|
||||||
{
|
{
|
||||||
|
SSUHeader * header = (SSUHeader *)buf;
|
||||||
|
if (header->GetPayloadType () != PAYLOAD_TYPE_SESSION_CONFIRMED)
|
||||||
|
{
|
||||||
|
LogPrint ("Unexpected payload type ", header->GetPayloadType ());
|
||||||
|
return;
|
||||||
|
}
|
||||||
m_State = eSessionStateCreatedReceived;
|
m_State = eSessionStateCreatedReceived;
|
||||||
LogPrint ("Session created received");
|
LogPrint ("Session created received");
|
||||||
m_Timer.cancel (); // connect timer
|
m_Timer.cancel (); // connect timer
|
||||||
|
@ -210,7 +228,7 @@ namespace ssu
|
||||||
{
|
{
|
||||||
Decrypt (buf, len, m_SessionKey);
|
Decrypt (buf, len, m_SessionKey);
|
||||||
SSUHeader * header = (SSUHeader *)buf;
|
SSUHeader * header = (SSUHeader *)buf;
|
||||||
if ((header->flag >> 4) == PAYLOAD_TYPE_SESSION_CONFIRMED)
|
if (header->GetPayloadType () == PAYLOAD_TYPE_SESSION_CONFIRMED)
|
||||||
{
|
{
|
||||||
m_State = eSessionStateConfirmedReceived;
|
m_State = eSessionStateConfirmedReceived;
|
||||||
LogPrint ("Session confirmed received");
|
LogPrint ("Session confirmed received");
|
||||||
|
@ -407,7 +425,7 @@ namespace ssu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SSUSession::ProcessIntroKeyEncryptedMessage (uint8_t expectedPayloadType, uint8_t * buf, size_t len)
|
bool SSUSession::ProcessIntroKeyEncryptedMessage (uint8_t * buf, size_t len)
|
||||||
{
|
{
|
||||||
auto introKey = GetIntroKey ();
|
auto introKey = GetIntroKey ();
|
||||||
if (introKey)
|
if (introKey)
|
||||||
|
@ -416,15 +434,9 @@ namespace ssu
|
||||||
if (Validate (buf, len, introKey))
|
if (Validate (buf, len, introKey))
|
||||||
{
|
{
|
||||||
Decrypt (buf, len, introKey);
|
Decrypt (buf, len, introKey);
|
||||||
SSUHeader * header = (SSUHeader *)buf;
|
|
||||||
if ((header->flag >> 4) == expectedPayloadType)
|
|
||||||
{
|
|
||||||
CreateAESandMacKey (buf + sizeof (SSUHeader), m_SessionKey, m_MacKey);
|
CreateAESandMacKey (buf + sizeof (SSUHeader), m_SessionKey, m_MacKey);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
LogPrint ("Unexpected payload type ", (int)(header->flag >> 4));
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint ("MAC verification failed");
|
LogPrint ("MAC verification failed");
|
||||||
|
|
6
SSU.h
6
SSU.h
|
@ -23,6 +23,8 @@ namespace ssu
|
||||||
uint8_t iv[16];
|
uint8_t iv[16];
|
||||||
uint8_t flag;
|
uint8_t flag;
|
||||||
uint32_t time;
|
uint32_t time;
|
||||||
|
|
||||||
|
uint8_t GetPayloadType () const { return flag >> 4; };
|
||||||
};
|
};
|
||||||
#pragma pack()
|
#pragma pack()
|
||||||
|
|
||||||
|
@ -87,6 +89,8 @@ namespace ssu
|
||||||
void CreateAESandMacKey (uint8_t * pubKey, uint8_t * aesKey, uint8_t * macKey);
|
void CreateAESandMacKey (uint8_t * pubKey, uint8_t * aesKey, uint8_t * macKey);
|
||||||
|
|
||||||
void ProcessMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint); // call for established session
|
void ProcessMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint); // call for established session
|
||||||
|
void ProcessIntroKeyMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint); // call for non-established session
|
||||||
|
|
||||||
void ProcessSessionRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
|
void ProcessSessionRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
|
||||||
void SendSessionRequest ();
|
void SendSessionRequest ();
|
||||||
void SendRelayRequest (const i2p::data::RouterInfo::Introducer& introducer);
|
void SendRelayRequest (const i2p::data::RouterInfo::Introducer& introducer);
|
||||||
|
@ -106,7 +110,7 @@ namespace ssu
|
||||||
void Send (i2p::I2NPMessage * msg);
|
void Send (i2p::I2NPMessage * msg);
|
||||||
void Send (uint8_t type, const uint8_t * payload, size_t len); // with session key
|
void Send (uint8_t type, const uint8_t * payload, size_t len); // with session key
|
||||||
|
|
||||||
bool ProcessIntroKeyEncryptedMessage (uint8_t expectedPayloadType, uint8_t * buf, size_t len);
|
bool ProcessIntroKeyEncryptedMessage (uint8_t * buf, size_t len);
|
||||||
void FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len, const uint8_t * aesKey, const uint8_t * iv, const uint8_t * macKey);
|
void FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len, const uint8_t * aesKey, const uint8_t * iv, const uint8_t * macKey);
|
||||||
void Decrypt (uint8_t * buf, size_t len, const uint8_t * aesKey);
|
void Decrypt (uint8_t * buf, size_t len, const uint8_t * aesKey);
|
||||||
bool Validate (uint8_t * buf, size_t len, const uint8_t * macKey);
|
bool Validate (uint8_t * buf, size_t len, const uint8_t * macKey);
|
||||||
|
|
Loading…
Add table
Reference in a new issue