|
|
@ -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,19 +139,32 @@ 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)) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
m_State = eSessionStateRequestReceived; |
|
|
|
SSUHeader * header = (SSUHeader *)buf; |
|
|
|
LogPrint ("Session request received"); |
|
|
|
switch (header->GetPayloadType ()) |
|
|
|
m_RemoteEndpoint = senderEndpoint; |
|
|
|
{ |
|
|
|
SendSessionCreated (buf + sizeof (SSUHeader)); |
|
|
|
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; |
|
|
|
|
|
|
|
LogPrint ("Session request received"); |
|
|
|
|
|
|
|
m_RemoteEndpoint = senderEndpoint; |
|
|
|
|
|
|
|
SendSessionCreated (buf); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::ProcessSessionCreated (uint8_t * buf, size_t len) |
|
|
|
void SSUSession::ProcessSessionCreated (uint8_t * buf, size_t len) |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint ("Process session created"); |
|
|
|
LogPrint ("Process session created"); |
|
|
@ -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,14 +434,8 @@ namespace ssu |
|
|
|
if (Validate (buf, len, introKey)) |
|
|
|
if (Validate (buf, len, introKey)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Decrypt (buf, len, introKey); |
|
|
|
Decrypt (buf, len, introKey); |
|
|
|
SSUHeader * header = (SSUHeader *)buf; |
|
|
|
CreateAESandMacKey (buf + sizeof (SSUHeader), m_SessionKey, m_MacKey); |
|
|
|
if ((header->flag >> 4) == expectedPayloadType) |
|
|
|
return true; |
|
|
|
{ |
|
|
|
|
|
|
|
CreateAESandMacKey (buf + sizeof (SSUHeader), m_SessionKey, m_MacKey); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
LogPrint ("Unexpected payload type ", (int)(header->flag >> 4)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|