i2pd/Garlic.cpp

418 lines
12 KiB
C++
Raw Normal View History

2013-11-23 21:35:15 +00:00
#include <inttypes.h>
#include "I2PEndian.h"
2013-11-23 21:35:15 +00:00
#include <map>
#include <string>
2013-11-24 23:10:27 +00:00
#include "ElGamal.h"
2013-11-23 21:35:15 +00:00
#include "RouterContext.h"
2014-01-09 03:47:22 +00:00
#include "I2NPProtocol.h"
#include "Tunnel.h"
2013-11-23 21:35:15 +00:00
#include "Timestamp.h"
2013-12-14 01:07:35 +00:00
#include "Streaming.h"
2013-11-23 21:35:15 +00:00
#include "Garlic.h"
namespace i2p
{
2013-11-24 23:10:27 +00:00
namespace garlic
{
GarlicRoutingSession::GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags):
2014-01-17 13:12:57 +00:00
m_Destination (destination), m_FirstMsgID (0), m_IsAcknowledged (false),
m_NumTags (numTags), m_NextTag (-1), m_SessionTags (0)
2013-11-23 21:35:15 +00:00
{
2014-01-17 13:12:57 +00:00
// create new session tags and session key
m_Rnd.GenerateBlock (m_SessionKey, 32);
if (m_NumTags > 0)
{
m_SessionTags = new uint8_t[m_NumTags*32];
2014-01-18 15:34:57 +00:00
GenerateSessionTags ();
2014-01-17 13:12:57 +00:00
}
else
m_SessionTags = nullptr;
2013-11-24 23:10:27 +00:00
}
GarlicRoutingSession::~GarlicRoutingSession ()
{
delete[] m_SessionTags;
}
2014-01-18 15:34:57 +00:00
void GarlicRoutingSession::GenerateSessionTags ()
2013-11-24 23:10:27 +00:00
{
2014-01-18 15:34:57 +00:00
if (m_SessionTags)
2014-01-17 13:12:57 +00:00
{
2014-01-18 15:34:57 +00:00
for (int i = 0; i < m_NumTags; i++)
m_Rnd.GenerateBlock (m_SessionTags + i*32, 32);
}
}
I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg, I2NPMessage * leaseSet)
{
2013-11-23 21:35:15 +00:00
I2NPMessage * m = NewI2NPMessage ();
2013-11-24 23:10:27 +00:00
size_t len = 0;
2013-11-23 21:35:15 +00:00
uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length
2014-01-18 15:34:57 +00:00
if (m_NextTag < 0 || !m_NumTags) // new session
2013-11-24 23:10:27 +00:00
{
// create ElGamal block
ElGamalBlock elGamal;
memcpy (elGamal.sessionKey, m_SessionKey, 32);
m_Rnd.GenerateBlock (elGamal.preIV, 32); // Pre-IV
uint8_t iv[32]; // IV is first 16 bytes
CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32);
2014-01-18 15:34:57 +00:00
i2p::crypto::ElGamalEncrypt (m_Destination->GetEncryptionPublicKey (), (uint8_t *)&elGamal, sizeof(elGamal), buf, true);
2013-11-24 23:10:27 +00:00
m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv);
2014-01-18 15:34:57 +00:00
buf += 514;
len += 514;
2013-11-24 23:10:27 +00:00
}
else // existing session
{
// session tag
2014-01-18 15:34:57 +00:00
memcpy (buf, m_SessionTags + m_NextTag*32, 32);
2013-11-24 23:10:27 +00:00
uint8_t iv[32]; // IV is first 16 bytes
CryptoPP::SHA256().CalculateDigest(iv, m_SessionTags + m_NextTag*32, 32);
m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv);
2014-01-18 15:34:57 +00:00
buf += 32;
len += 32;
// re-create session tags if necessary
if (m_NextTag >= m_NumTags - 1) // we have used last tag
{
GenerateSessionTags ();
m_NextTag = -1;
}
2013-11-24 23:10:27 +00:00
}
2014-01-18 15:34:57 +00:00
// AES block
len += CreateAESBlock (buf, msg, leaseSet);
2013-11-24 23:10:27 +00:00
m_NextTag++;
*(uint32_t *)(m->GetPayload ()) = htobe32 (len);
m->len += len + 4;
FillI2NPMessageHeader (m, eI2NPGarlic);
2014-01-01 23:19:03 +00:00
if (msg)
DeleteI2NPMessage (msg);
2013-11-24 23:10:27 +00:00
return m;
}
2014-01-18 15:34:57 +00:00
size_t GarlicRoutingSession::CreateAESBlock (uint8_t * buf, I2NPMessage * msg, I2NPMessage * leaseSet)
2013-11-24 23:10:27 +00:00
{
2013-11-23 21:35:15 +00:00
size_t blockSize = 0;
2014-01-18 15:34:57 +00:00
*(uint16_t *)buf = m_NextTag < 0 ? htobe16 (m_NumTags) : 0; // tag count
2013-11-23 21:35:15 +00:00
blockSize += 2;
2014-01-18 15:34:57 +00:00
if (m_NextTag < 0) // session tags recreated
2014-01-17 13:12:57 +00:00
{
memcpy (buf + blockSize, m_SessionTags, m_NumTags*32); // tags
blockSize += m_NumTags*32;
}
2013-11-23 21:35:15 +00:00
uint32_t * payloadSize = (uint32_t *)(buf + blockSize);
blockSize += 4;
uint8_t * payloadHash = buf + blockSize;
blockSize += 32;
buf[blockSize] = 0; // flag
blockSize++;
2014-01-18 15:34:57 +00:00
size_t len = CreateGarlicPayload (buf + blockSize, msg, leaseSet);
2013-11-23 21:35:15 +00:00
*payloadSize = htobe32 (len);
CryptoPP::SHA256().CalculateDigest(payloadHash, buf + blockSize, len);
blockSize += len;
size_t rem = blockSize % 16;
if (rem)
blockSize += (16-rem); //padding
2013-11-24 23:10:27 +00:00
m_Encryption.ProcessData(buf, buf, blockSize);
return blockSize;
2013-11-23 21:35:15 +00:00
}
2014-01-18 15:34:57 +00:00
size_t GarlicRoutingSession::CreateGarlicPayload (uint8_t * payload, I2NPMessage * msg, I2NPMessage * leaseSet)
2013-11-23 21:35:15 +00:00
{
2013-11-24 23:10:27 +00:00
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
2014-01-17 13:12:57 +00:00
uint32_t msgID = m_Rnd.GenerateWord32 ();
2013-11-23 21:35:15 +00:00
size_t size = 0;
2013-12-31 01:46:33 +00:00
uint8_t * numCloves = payload + size;
*numCloves = 0;
2013-11-23 21:35:15 +00:00
size++;
2014-01-17 13:12:57 +00:00
2014-01-18 15:34:57 +00:00
if (m_NextTag < 0) // new session
2014-01-09 03:47:22 +00:00
{
2014-01-17 13:12:57 +00:00
// clove is DeliveryStatus
2014-01-09 03:47:22 +00:00
size += CreateDeliveryStatusClove (payload + size, msgID);
(*numCloves)++;
2014-01-17 13:12:57 +00:00
m_FirstMsgID = msgID;
}
if (leaseSet)
{
2014-01-09 03:47:22 +00:00
// clove is our leaseSet if presented
2013-12-31 01:46:33 +00:00
size += CreateGarlicClove (payload + size, leaseSet, false);
(*numCloves)++;
}
2014-01-09 03:47:22 +00:00
if (msg) // clove message ifself if presented
2013-12-31 01:46:33 +00:00
{
2014-01-03 02:22:48 +00:00
size += CreateGarlicClove (payload + size, msg, m_Destination->IsDestination ());
2013-12-31 01:46:33 +00:00
(*numCloves)++;
}
memset (payload + size, 0, 3); // certificate of message
size += 3;
2014-01-09 03:47:22 +00:00
*(uint32_t *)(payload + size) = htobe32 (msgID); // MessageID
2013-12-31 01:46:33 +00:00
size += 4;
*(uint64_t *)(payload + size) = htobe64 (ts); // Expiration of message
size += 8;
return size;
}
size_t GarlicRoutingSession::CreateGarlicClove (uint8_t * buf, I2NPMessage * msg, bool isDestination)
{
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
size_t size = 0;
if (isDestination)
{
2013-12-31 01:46:33 +00:00
buf[size] = eGarlicDeliveryTypeDestination << 5;// delivery instructions flag destination
size++;
2013-12-31 01:46:33 +00:00
memcpy (buf + size, m_Destination->GetIdentHash (), 32);
size += 32;
}
else
{
2013-12-31 01:46:33 +00:00
buf[size] = 0;// delivery instructions flag local
size++;
}
2013-12-31 01:46:33 +00:00
memcpy (buf + size, msg->GetBuffer (), msg->GetLength ());
2013-11-23 21:35:15 +00:00
size += msg->GetLength ();
2013-12-31 01:46:33 +00:00
*(uint32_t *)(buf + size) = htobe32 (m_Rnd.GenerateWord32 ()); // CloveID
2013-11-23 21:35:15 +00:00
size += 4;
2013-12-31 01:46:33 +00:00
*(uint64_t *)(buf + size) = htobe64 (ts); // Expiration of clove
2013-11-23 21:35:15 +00:00
size += 8;
2013-12-31 01:46:33 +00:00
memset (buf + size, 0, 3); // certificate of clove
2013-11-23 21:35:15 +00:00
size += 3;
return size;
}
2014-01-09 03:47:22 +00:00
size_t GarlicRoutingSession::CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID)
{
size_t size = 0;
auto tunnel = i2p::tunnel::tunnels.GetNextInboundTunnel ();
if (tunnel)
{
buf[size] = eGarlicDeliveryTypeTunnel << 5; // delivery instructions flag tunnel
size++;
2014-01-16 23:21:44 +00:00
// hash and tunnelID sequence is reversed for Garlic
2014-01-09 03:47:22 +00:00
memcpy (buf + size, tunnel->GetNextIdentHash (), 32); // To Hash
size += 32;
2014-01-16 23:21:44 +00:00
*(uint32_t *)(buf + size) = htobe32 (tunnel->GetNextTunnelID ()); // tunnelID
size += 4;
2014-01-09 03:47:22 +00:00
}
else
{
LogPrint ("No reply tunnels for garlic DeliveryStatus found");
buf[size] = 0;// delivery instructions flag local
size++;
}
I2NPMessage * msg = CreateDeliveryStatusMsg (msgID);
memcpy (buf + size, msg->GetBuffer (), msg->GetLength ());
size += msg->GetLength ();
DeleteI2NPMessage (msg);
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
*(uint32_t *)(buf + size) = htobe32 (m_Rnd.GenerateWord32 ()); // CloveID
size += 4;
*(uint64_t *)(buf + size) = htobe64 (ts); // Expiration of clove
size += 8;
memset (buf + size, 0, 3); // certificate of clove
size += 3;
return size;
}
2013-12-31 01:46:33 +00:00
2013-11-24 23:10:27 +00:00
GarlicRouting routing;
GarlicRouting::GarlicRouting ()
{
}
GarlicRouting::~GarlicRouting ()
{
for (auto it: m_Sessions)
delete it.second;
m_Sessions.clear ();
}
2014-01-18 15:34:57 +00:00
I2NPMessage * GarlicRouting::WrapSingleMessage (const i2p::data::RoutingDestination * destination, I2NPMessage * msg)
{
if (!destination) return nullptr;
auto it = m_Sessions.find (destination->GetIdentHash ());
if (it != m_Sessions.end ())
{
delete it->second;
2014-01-25 02:14:14 +00:00
m_Sessions.erase (it);
2014-01-18 15:34:57 +00:00
}
GarlicRoutingSession * session = new GarlicRoutingSession (destination, 0); // not follow-on messages expected
m_Sessions[destination->GetIdentHash ()] = session;
return session->WrapSingleMessage (msg, nullptr);
}
I2NPMessage * GarlicRouting::WrapMessage (const i2p::data::RoutingDestination * destination,
2013-12-31 01:46:33 +00:00
I2NPMessage * msg, I2NPMessage * leaseSet)
2013-11-24 23:10:27 +00:00
{
if (!destination) return nullptr;
2013-12-10 13:10:49 +00:00
auto it = m_Sessions.find (destination->GetIdentHash ());
2013-11-24 23:10:27 +00:00
GarlicRoutingSession * session = nullptr;
if (it != m_Sessions.end ())
session = it->second;
if (!session)
{
2014-01-18 15:34:57 +00:00
session = new GarlicRoutingSession (destination, 4); // TODO: change it later
2013-12-10 13:10:49 +00:00
m_Sessions[destination->GetIdentHash ()] = session;
2013-11-24 23:10:27 +00:00
}
2013-12-31 01:46:33 +00:00
I2NPMessage * ret = session->WrapSingleMessage (msg, leaseSet);
2014-01-18 15:34:57 +00:00
if (!session->GetNextTag ()) // tags have beed recreated
2014-01-17 13:12:57 +00:00
m_CreatedSessions[session->GetFirstMsgID ()] = session;
2013-11-24 23:10:27 +00:00
return ret;
2014-01-18 15:34:57 +00:00
}
2013-12-14 01:07:35 +00:00
void GarlicRouting::HandleGarlicMessage (uint8_t * buf, size_t len, bool isFromTunnel)
2013-12-14 01:07:35 +00:00
{
uint32_t length = be32toh (*(uint32_t *)buf);
buf += 4;
std::string sessionTag((const char *)buf, 32);
auto it = m_SessionTags.find (sessionTag);
if (it != m_SessionTags.end ())
2013-12-14 01:07:35 +00:00
{
// existing session
std::string sessionKey (it->second);
m_SessionTags.erase (it); // tag might be used only once
2013-12-14 01:07:35 +00:00
uint8_t iv[32]; // IV is first 16 bytes
CryptoPP::SHA256().CalculateDigest(iv, buf, 32);
m_Decryption.SetKeyWithIV ((uint8_t *)sessionKey.c_str (), 32, iv); // tag is mapped to 32 bytes key
2013-12-14 01:07:35 +00:00
m_Decryption.ProcessData(buf + 32, buf + 32, length - 32);
HandleAESBlock (buf + 32, length - 32, (uint8_t *)sessionKey.c_str ());
2013-12-14 01:07:35 +00:00
}
else
{
// new session
ElGamalBlock elGamal;
2014-01-18 15:34:57 +00:00
if (i2p::crypto::ElGamalDecrypt (
isFromTunnel ? i2p::context.GetLeaseSetPrivateKey () : i2p::context.GetPrivateKey (),
2014-01-18 15:34:57 +00:00
buf, (uint8_t *)&elGamal, true))
{
uint8_t iv[32]; // IV is first 16 bytes
CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32);
m_Decryption.SetKeyWithIV (elGamal.sessionKey, 32, iv);
m_Decryption.ProcessData(buf + 514, buf + 514, length - 514);
HandleAESBlock (buf + 514, length - 514, elGamal.sessionKey);
}
else
LogPrint ("Failed to decrypt garlic");
2013-12-14 01:07:35 +00:00
}
}
void GarlicRouting::HandleAESBlock (uint8_t * buf, size_t len, uint8_t * sessionKey)
2013-12-14 01:07:35 +00:00
{
uint16_t tagCount = be16toh (*(uint16_t *)buf);
buf += 2;
for (int i = 0; i < tagCount; i++)
m_SessionTags[std::string ((const char *)(buf + i*32), 32)] = std::string ((const char *)sessionKey, 32);
2013-12-14 01:07:35 +00:00
buf += tagCount*32;
uint32_t payloadSize = be32toh (*(uint32_t *)buf);
2014-01-16 13:08:28 +00:00
if (payloadSize > len)
{
2014-01-17 13:12:57 +00:00
LogPrint ("Unexpected payload size ", payloadSize);
2014-01-16 13:08:28 +00:00
return;
}
2013-12-14 01:07:35 +00:00
buf += 4;
2014-01-16 13:08:28 +00:00
uint8_t * payloadHash = buf;
buf += 32;// payload hash.
2013-12-14 01:07:35 +00:00
if (*buf) // session key?
buf += 32; // new session key
buf++; // flag
2014-01-16 13:08:28 +00:00
2013-12-14 01:07:35 +00:00
// payload
2014-01-16 13:08:28 +00:00
uint8_t hash[32];
CryptoPP::SHA256().CalculateDigest(hash, buf, payloadSize);
if (memcmp (hash, payloadHash, 32)) // payload hash doesn't match
{
LogPrint ("Wrong payload hash");
return;
}
2013-12-14 01:07:35 +00:00
HandleGarlicPayload (buf, payloadSize);
}
void GarlicRouting::HandleGarlicPayload (uint8_t * buf, size_t len)
{
int numCloves = buf[0];
LogPrint (numCloves," cloves");
buf++;
for (int i = 0; i < numCloves; i++)
{
// delivery instructions
uint8_t flag = buf[0];
buf++; // flag
if (flag & 0x80) // encrypted?
{
// TODO: implement
LogPrint ("Clove encrypted");
buf += 32;
}
GarlicDeliveryType deliveryType = (GarlicDeliveryType)((flag >> 5) & 0x03);
switch (deliveryType)
{
case eGarlicDeliveryTypeLocal:
LogPrint ("Garlic type local");
i2p::HandleI2NPMessage (buf, len, false);
2013-12-14 01:07:35 +00:00
break;
case eGarlicDeliveryTypeDestination:
{
LogPrint ("Garlic type destination");
i2p::data::IdentHash destination (buf);
buf += 32;
// we assume streaming protocol for destination
// later on we should let destination decide
I2NPHeader * header = (I2NPHeader *)buf;
if (header->typeID == eI2NPData)
i2p::stream::HandleDataMessage (&destination, buf + sizeof (I2NPHeader), be16toh (header->size));
else
LogPrint ("Unexpected I2NP garlic message ", (int)header->typeID);
break;
}
case eGarlicDeliveryTypeTunnel:
{
LogPrint ("Garlic type tunnel");
2014-01-16 23:21:44 +00:00
// gwHash and gwTunnel sequence is reverted
uint8_t * gwHash = buf;
buf += 32;
2014-01-16 23:21:44 +00:00
uint32_t gwTunnel = be32toh (*(uint32_t *)buf);
buf += 4;
auto tunnel = i2p::tunnel::tunnels.GetNextOutboundTunnel ();
if (tunnel) // we have send it through an outbound tunnel
{
I2NPMessage * msg = CreateI2NPMessage (buf, len - 36);
tunnel->SendTunnelDataMsg (gwHash, gwTunnel, msg);
}
else
LogPrint ("No outbound tunnels available for garlic clove");
break;
}
case eGarlicDeliveryTypeRouter:
LogPrint ("Garlic type router not supported");
2013-12-14 01:07:35 +00:00
buf += 32;
break;
2013-12-14 01:07:35 +00:00
default:
LogPrint ("Unknow garlic delivery type ", (int)deliveryType);
}
buf += GetI2NPMessageLength (buf); // I2NP
buf += 4; // CloveID
buf += 8; // Date
buf += 3; // Certificate
}
}
2014-01-17 13:12:57 +00:00
void GarlicRouting::HandleDeliveryStatusMessage (uint8_t * buf, size_t len)
{
I2NPDeliveryStatusMsg * msg = (I2NPDeliveryStatusMsg *)buf;
auto it = m_CreatedSessions.find (be32toh (msg->msgID));
if (it != m_CreatedSessions.end ())
{
it->second->SetAcknowledged (true);
m_CreatedSessions.erase (it);
LogPrint ("Garlic message ", be32toh (msg->msgID), " acknowledged");
}
}
2013-11-24 23:10:27 +00:00
}
}