|
|
@ -12,31 +12,21 @@ namespace tunnel |
|
|
|
{ |
|
|
|
{ |
|
|
|
TransitTunnel::TransitTunnel (uint32_t receiveTunnelID, |
|
|
|
TransitTunnel::TransitTunnel (uint32_t receiveTunnelID, |
|
|
|
const uint8_t * nextIdent, uint32_t nextTunnelID, |
|
|
|
const uint8_t * nextIdent, uint32_t nextTunnelID, |
|
|
|
const uint8_t * layerKey,const uint8_t * ivKey, |
|
|
|
const uint8_t * layerKey,const uint8_t * ivKey): |
|
|
|
bool isGateway, bool isEndpoint) |
|
|
|
m_TunnelID (receiveTunnelID), m_NextTunnelID (nextTunnelID), m_NextIdent (nextIdent) |
|
|
|
{ |
|
|
|
{ |
|
|
|
memcpy (m_LayerKey, layerKey, 32); |
|
|
|
memcpy (m_LayerKey, layerKey, 32); |
|
|
|
memcpy (m_IVKey, ivKey, 32); |
|
|
|
memcpy (m_IVKey, ivKey, 32); |
|
|
|
memcpy (m_NextIdent, nextIdent, 32); |
|
|
|
|
|
|
|
m_IsGateway = isGateway; |
|
|
|
|
|
|
|
m_IsEndpoint = isEndpoint; |
|
|
|
|
|
|
|
m_TunnelID = receiveTunnelID; |
|
|
|
|
|
|
|
m_NextTunnelID = nextTunnelID; |
|
|
|
|
|
|
|
if (m_IsEndpoint) |
|
|
|
|
|
|
|
LogPrint ("TransitTunnel endpoint: ", m_TunnelID, " created"); |
|
|
|
|
|
|
|
else if (m_IsGateway) |
|
|
|
|
|
|
|
LogPrint ("TransitTunnel gateway: ", m_TunnelID, " created"); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
LogPrint ("TransitTunnel: ",m_TunnelID,"->", m_NextTunnelID, " created"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void TransitTunnel::Encrypt (uint8_t * payload) |
|
|
|
void TransitTunnel::EncryptTunnelMsg (I2NPMessage * tunnelMsg) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
uint8_t * payload = tunnelMsg->GetPayload () + 4; |
|
|
|
m_ECBEncryption.SetKey (m_IVKey, 32); |
|
|
|
m_ECBEncryption.SetKey (m_IVKey, 32); |
|
|
|
m_ECBEncryption.ProcessData(payload, payload, 16); // iv
|
|
|
|
m_ECBEncryption.ProcessData(payload, payload, 16); // iv
|
|
|
|
|
|
|
|
|
|
|
|
m_CBCEncryption.SetKeyWithIV (m_LayerKey, 32, payload); |
|
|
|
m_CBCEncryption.SetKeyWithIV (m_LayerKey, 32, payload); |
|
|
|
m_CBCEncryption.ProcessData(payload + 16, payload + 16, 1008); // payload
|
|
|
|
m_CBCEncryption.ProcessData(payload + 16, payload + 16, TUNNEL_DATA_ENCRYPTED_SIZE); // payload
|
|
|
|
|
|
|
|
|
|
|
|
m_ECBEncryption.SetKey (m_IVKey, 32); |
|
|
|
m_ECBEncryption.SetKey (m_IVKey, 32); |
|
|
|
m_ECBEncryption.ProcessData(payload, payload, 16); // double iv encryption
|
|
|
|
m_ECBEncryption.ProcessData(payload, payload, 16); // double iv encryption
|
|
|
@ -45,40 +35,53 @@ namespace tunnel |
|
|
|
|
|
|
|
|
|
|
|
void TransitTunnel::HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg) |
|
|
|
void TransitTunnel::HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Encrypt (tunnelMsg->GetPayload () + 4); |
|
|
|
EncryptTunnelMsg (tunnelMsg); |
|
|
|
|
|
|
|
|
|
|
|
if (m_IsEndpoint) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint ("TransitTunnel endpoint for ", m_TunnelID); |
|
|
|
|
|
|
|
m_Endpoint.HandleDecryptedTunnelDataMsg (tunnelMsg); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint ("TransitTunnel: ",m_TunnelID,"->", m_NextTunnelID); |
|
|
|
|
|
|
|
*(uint32_t *)(tunnelMsg->GetPayload ()) = htobe32 (m_NextTunnelID); |
|
|
|
|
|
|
|
FillI2NPMessageHeader (tunnelMsg, eI2NPTunnelData); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
i2p::transports.SendMessage (m_NextIdent, tunnelMsg); |
|
|
|
LogPrint ("TransitTunnel: ",m_TunnelID,"->", m_NextTunnelID); |
|
|
|
} |
|
|
|
*(uint32_t *)(tunnelMsg->GetPayload ()) = htobe32 (m_NextTunnelID); |
|
|
|
|
|
|
|
FillI2NPMessageHeader (tunnelMsg, eI2NPTunnelData); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
i2p::transports.SendMessage (m_NextIdent, tunnelMsg); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void TransitTunnel::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg) |
|
|
|
void TransitTunnel::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint ("We are not a gateway for transit tunnel ", m_TunnelID); |
|
|
|
|
|
|
|
i2p::DeleteI2NPMessage (msg); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TransitTunnelGateway::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_Gateway.SendTunnelDataMsg (gwHash, gwTunnel, msg); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TransitTunnelEndpoint::HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
EncryptTunnelMsg (tunnelMsg); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogPrint ("TransitTunnel endpoint for ", GetTunnelID ()); |
|
|
|
|
|
|
|
m_Endpoint.HandleDecryptedTunnelDataMsg (tunnelMsg); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TransitTunnel * CreateTransitTunnel (uint32_t receiveTunnelID, |
|
|
|
|
|
|
|
const uint8_t * nextIdent, uint32_t nextTunnelID, |
|
|
|
|
|
|
|
const uint8_t * layerKey,const uint8_t * ivKey, |
|
|
|
|
|
|
|
bool isGateway, bool isEndpoint) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (m_IsGateway) |
|
|
|
if (isEndpoint) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_Gateway.PutI2NPMsg (gwHash, gwTunnel, msg); |
|
|
|
LogPrint ("TransitTunnel endpoint: ", receiveTunnelID, " created"); |
|
|
|
auto tunnelMsgs = m_Gateway.GetTunnelDataMsgs (m_NextTunnelID); |
|
|
|
return new TransitTunnelEndpoint (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey); |
|
|
|
for (auto tunnelMsg : tunnelMsgs) |
|
|
|
} |
|
|
|
{ |
|
|
|
else if (isGateway) |
|
|
|
Encrypt (tunnelMsg->GetPayload () + 4); |
|
|
|
{ |
|
|
|
FillI2NPMessageHeader (tunnelMsg, eI2NPTunnelData); |
|
|
|
LogPrint ("TransitTunnel gateway: ", receiveTunnelID, " created"); |
|
|
|
i2p::transports.SendMessage (m_NextIdent, tunnelMsg); |
|
|
|
return new TransitTunnelGateway (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint ("We are not a gateway for transit tunnel ", m_TunnelID); |
|
|
|
LogPrint ("TransitTunnel: ", receiveTunnelID, "->", nextTunnelID, " created"); |
|
|
|
i2p::DeleteI2NPMessage (msg); |
|
|
|
return new TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|