2013-11-24 01:35:15 +04:00
|
|
|
#include <inttypes.h>
|
2014-01-09 23:56:07 +04:00
|
|
|
#include "I2PEndian.h"
|
2013-11-24 01:35:15 +04:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include "RouterContext.h"
|
2014-01-09 07:47:22 +04:00
|
|
|
#include "I2NPProtocol.h"
|
|
|
|
#include "Tunnel.h"
|
2014-03-16 06:09:08 +04:00
|
|
|
#include "TunnelPool.h"
|
2013-11-24 01:35:15 +04:00
|
|
|
#include "Timestamp.h"
|
2014-10-05 16:54:59 +04:00
|
|
|
#include "Destination.h"
|
2013-11-24 01:35:15 +04:00
|
|
|
#include "Garlic.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2013-11-25 03:10:27 +04:00
|
|
|
namespace garlic
|
2014-10-07 05:18:20 +04:00
|
|
|
{
|
2014-10-08 05:08:00 +04:00
|
|
|
GarlicRoutingSession::GarlicRoutingSession (GarlicDestination * owner,
|
|
|
|
const i2p::data::RoutingDestination * destination, int numTags):
|
2014-10-17 04:12:46 +04:00
|
|
|
m_Owner (owner), m_Destination (destination), m_NumTags (numTags),
|
2014-10-17 19:26:57 +04:00
|
|
|
m_LeaseSetUpdated (numTags > 0)
|
2013-11-24 01:35:15 +04:00
|
|
|
{
|
2014-01-17 17:12:57 +04:00
|
|
|
// create new session tags and session key
|
|
|
|
m_Rnd.GenerateBlock (m_SessionKey, 32);
|
2014-05-12 06:37:33 +04:00
|
|
|
m_Encryption.SetKey (m_SessionKey);
|
2013-11-25 03:10:27 +04:00
|
|
|
}
|
|
|
|
|
2014-07-28 20:02:50 +04:00
|
|
|
GarlicRoutingSession::GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag):
|
2014-10-17 04:12:46 +04:00
|
|
|
m_Owner (nullptr), m_Destination (nullptr), m_NumTags (1), m_LeaseSetUpdated (false)
|
2014-07-28 19:54:34 +04:00
|
|
|
{
|
|
|
|
memcpy (m_SessionKey, sessionKey, 32);
|
|
|
|
m_Encryption.SetKey (m_SessionKey);
|
2014-10-17 04:12:46 +04:00
|
|
|
m_SessionTags.push_back (sessionTag);
|
2014-10-17 19:26:57 +04:00
|
|
|
m_SessionTags.back ().creationTime = i2p::util::GetSecondsSinceEpoch ();
|
2014-07-28 19:54:34 +04:00
|
|
|
}
|
|
|
|
|
2013-11-25 03:10:27 +04:00
|
|
|
GarlicRoutingSession::~GarlicRoutingSession ()
|
|
|
|
{
|
2014-10-14 22:48:25 +04:00
|
|
|
for (auto it: m_UnconfirmedTagsMsgs)
|
|
|
|
delete it.second;
|
2014-10-17 04:12:46 +04:00
|
|
|
m_UnconfirmedTagsMsgs.clear ();
|
2013-11-25 03:10:27 +04:00
|
|
|
}
|
2014-01-18 19:34:57 +04:00
|
|
|
|
2014-10-17 04:12:46 +04:00
|
|
|
GarlicRoutingSession::UnconfirmedTags * GarlicRoutingSession::GenerateSessionTags ()
|
2013-11-25 03:10:27 +04:00
|
|
|
{
|
2014-10-17 04:12:46 +04:00
|
|
|
auto tags = new UnconfirmedTags (m_NumTags);
|
2014-10-17 19:26:57 +04:00
|
|
|
tags->tagsCreationTime = i2p::util::GetSecondsSinceEpoch ();
|
2014-10-17 04:12:46 +04:00
|
|
|
for (int i = 0; i < m_NumTags; i++)
|
2014-10-17 19:26:57 +04:00
|
|
|
{
|
2014-10-17 04:12:46 +04:00
|
|
|
m_Rnd.GenerateBlock (tags->sessionTags[i], 32);
|
2014-10-17 19:26:57 +04:00
|
|
|
tags->sessionTags[i].creationTime = tags->tagsCreationTime;
|
|
|
|
}
|
2014-10-17 04:12:46 +04:00
|
|
|
return tags;
|
2014-01-18 19:34:57 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 22:48:25 +04:00
|
|
|
void GarlicRoutingSession::TagsConfirmed (uint32_t msgID)
|
|
|
|
{
|
2014-10-17 05:11:02 +04:00
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2014-10-14 22:48:25 +04:00
|
|
|
auto it = m_UnconfirmedTagsMsgs.find (msgID);
|
|
|
|
if (it != m_UnconfirmedTagsMsgs.end ())
|
|
|
|
{
|
2014-10-17 04:12:46 +04:00
|
|
|
UnconfirmedTags * tags = it->second;
|
2014-10-17 05:11:02 +04:00
|
|
|
if (ts < tags->tagsCreationTime + OUTGOING_TAGS_EXPIRATION_TIMEOUT)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < tags->numTags; i++)
|
|
|
|
m_SessionTags.push_back (tags->sessionTags[i]);
|
|
|
|
}
|
2014-10-17 04:12:46 +04:00
|
|
|
m_UnconfirmedTagsMsgs.erase (it);
|
|
|
|
delete tags;
|
2014-10-14 22:48:25 +04:00
|
|
|
}
|
2014-10-17 05:11:02 +04:00
|
|
|
// delete expired unconfirmed tags
|
|
|
|
for (auto it = m_UnconfirmedTagsMsgs.begin (); it != m_UnconfirmedTagsMsgs.end ();)
|
|
|
|
{
|
|
|
|
if (ts >= it->second->tagsCreationTime + OUTGOING_TAGS_EXPIRATION_TIMEOUT)
|
|
|
|
{
|
|
|
|
delete it->second;
|
|
|
|
it = m_UnconfirmedTagsMsgs.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
2014-10-14 22:48:25 +04:00
|
|
|
}
|
|
|
|
|
2014-10-08 20:11:39 +04:00
|
|
|
I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg)
|
2014-01-18 19:34:57 +04:00
|
|
|
{
|
2013-11-24 01:35:15 +04:00
|
|
|
I2NPMessage * m = NewI2NPMessage ();
|
2014-11-26 19:04:49 +03:00
|
|
|
m->Align (12); // in order to get buf aligned to 16 (12 + 4)
|
2013-11-25 03:10:27 +04:00
|
|
|
size_t len = 0;
|
2013-11-24 01:35:15 +04:00
|
|
|
uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length
|
2014-03-22 16:43:38 +04:00
|
|
|
|
2014-10-17 19:26:57 +04:00
|
|
|
// find non-expired tag
|
|
|
|
bool tagFound = false;
|
|
|
|
SessionTag tag;
|
2014-03-22 16:43:38 +04:00
|
|
|
if (m_NumTags > 0)
|
|
|
|
{
|
2014-10-17 19:26:57 +04:00
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2014-10-17 19:48:07 +04:00
|
|
|
while (!m_SessionTags.empty ())
|
2014-03-22 16:43:38 +04:00
|
|
|
{
|
2014-10-17 19:26:57 +04:00
|
|
|
if (ts < m_SessionTags.front ().creationTime + OUTGOING_TAGS_EXPIRATION_TIMEOUT)
|
|
|
|
{
|
|
|
|
tag = m_SessionTags.front ();
|
|
|
|
m_SessionTags.pop_front (); // use same tag only once
|
|
|
|
tagFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_SessionTags.pop_front (); // remove expired tag
|
|
|
|
}
|
2014-03-22 16:43:38 +04:00
|
|
|
}
|
|
|
|
// create message
|
2014-10-17 19:26:57 +04:00
|
|
|
if (!tagFound) // new session
|
2013-11-25 03:10:27 +04:00
|
|
|
{
|
2014-10-17 04:12:46 +04:00
|
|
|
LogPrint ("No garlic tags available. Use ElGamal");
|
2014-07-28 19:54:34 +04:00
|
|
|
if (!m_Destination)
|
|
|
|
{
|
|
|
|
LogPrint ("Can't use ElGamal for unknown destination");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-11-25 03:10:27 +04: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-07-28 19:54:34 +04:00
|
|
|
m_Destination->GetElGamalEncryption ()->Encrypt ((uint8_t *)&elGamal, sizeof(elGamal), buf, true);
|
2014-05-12 06:37:33 +04:00
|
|
|
m_Encryption.SetIV (iv);
|
2014-01-18 19:34:57 +04:00
|
|
|
buf += 514;
|
|
|
|
len += 514;
|
2013-11-25 03:10:27 +04:00
|
|
|
}
|
|
|
|
else // existing session
|
|
|
|
{
|
|
|
|
// session tag
|
2014-10-17 04:12:46 +04:00
|
|
|
memcpy (buf, tag, 32);
|
2013-11-25 03:10:27 +04:00
|
|
|
uint8_t iv[32]; // IV is first 16 bytes
|
2014-10-17 04:12:46 +04:00
|
|
|
CryptoPP::SHA256().CalculateDigest(iv, tag, 32);
|
2014-05-12 06:37:33 +04:00
|
|
|
m_Encryption.SetIV (iv);
|
2014-01-18 19:34:57 +04:00
|
|
|
buf += 32;
|
2014-10-17 04:12:46 +04:00
|
|
|
len += 32;
|
2013-11-25 03:10:27 +04:00
|
|
|
}
|
2014-01-18 19:34:57 +04:00
|
|
|
// AES block
|
2014-10-08 20:11:39 +04:00
|
|
|
len += CreateAESBlock (buf, msg);
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (m->GetPayload (), len);
|
2013-11-25 03:10:27 +04:00
|
|
|
m->len += len + 4;
|
|
|
|
FillI2NPMessageHeader (m, eI2NPGarlic);
|
2014-01-02 03:19:03 +04:00
|
|
|
if (msg)
|
|
|
|
DeleteI2NPMessage (msg);
|
2013-11-25 03:10:27 +04:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:11:39 +04:00
|
|
|
size_t GarlicRoutingSession::CreateAESBlock (uint8_t * buf, const I2NPMessage * msg)
|
2013-11-25 03:10:27 +04:00
|
|
|
{
|
2013-11-24 01:35:15 +04:00
|
|
|
size_t blockSize = 0;
|
2014-11-16 23:41:54 +03:00
|
|
|
bool createNewTags = m_Owner && m_NumTags && ((int)m_SessionTags.size () <= m_NumTags/2);
|
2014-10-17 04:12:46 +04:00
|
|
|
UnconfirmedTags * newTags = createNewTags ? GenerateSessionTags () : nullptr;
|
2014-12-30 17:37:24 +03:00
|
|
|
htobuf16 (buf, newTags ? htobe16 (newTags->numTags) : 0); // tag count
|
2013-11-24 01:35:15 +04:00
|
|
|
blockSize += 2;
|
2014-10-17 04:12:46 +04:00
|
|
|
if (newTags) // session tags recreated
|
2014-01-17 17:12:57 +04:00
|
|
|
{
|
2014-10-17 04:12:46 +04:00
|
|
|
for (int i = 0; i < newTags->numTags; i++)
|
2014-07-28 20:02:50 +04:00
|
|
|
{
|
2014-10-17 04:12:46 +04:00
|
|
|
memcpy (buf + blockSize, newTags->sessionTags[i], 32); // tags
|
2014-07-28 20:02:50 +04:00
|
|
|
blockSize += 32;
|
|
|
|
}
|
2014-01-17 17:12:57 +04:00
|
|
|
}
|
2013-11-24 01:35:15 +04:00
|
|
|
uint32_t * payloadSize = (uint32_t *)(buf + blockSize);
|
|
|
|
blockSize += 4;
|
|
|
|
uint8_t * payloadHash = buf + blockSize;
|
|
|
|
blockSize += 32;
|
|
|
|
buf[blockSize] = 0; // flag
|
|
|
|
blockSize++;
|
2014-10-17 04:12:46 +04:00
|
|
|
size_t len = CreateGarlicPayload (buf + blockSize, msg, newTags);
|
2014-12-31 17:14:53 +03:00
|
|
|
htobe32buf (payloadSize, len);
|
2013-11-24 01:35:15 +04:00
|
|
|
CryptoPP::SHA256().CalculateDigest(payloadHash, buf + blockSize, len);
|
|
|
|
blockSize += len;
|
|
|
|
size_t rem = blockSize % 16;
|
|
|
|
if (rem)
|
|
|
|
blockSize += (16-rem); //padding
|
2014-05-12 06:37:33 +04:00
|
|
|
m_Encryption.Encrypt(buf, blockSize, buf);
|
2013-11-25 03:10:27 +04:00
|
|
|
return blockSize;
|
2013-11-24 01:35:15 +04:00
|
|
|
}
|
|
|
|
|
2014-10-17 04:12:46 +04:00
|
|
|
size_t GarlicRoutingSession::CreateGarlicPayload (uint8_t * payload, const I2NPMessage * msg, UnconfirmedTags * newTags)
|
2013-11-24 01:35:15 +04:00
|
|
|
{
|
2013-11-25 03:10:27 +04:00
|
|
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
|
2014-01-17 17:12:57 +04:00
|
|
|
uint32_t msgID = m_Rnd.GenerateWord32 ();
|
2013-11-24 01:35:15 +04:00
|
|
|
size_t size = 0;
|
2013-12-31 05:46:33 +04:00
|
|
|
uint8_t * numCloves = payload + size;
|
|
|
|
*numCloves = 0;
|
2013-11-24 01:35:15 +04:00
|
|
|
size++;
|
2014-01-17 17:12:57 +04:00
|
|
|
|
2014-10-08 05:08:00 +04:00
|
|
|
if (m_Owner)
|
2014-08-15 22:34:58 +04:00
|
|
|
{
|
2014-10-17 04:12:46 +04:00
|
|
|
if (newTags) // new session
|
2014-08-15 22:34:58 +04:00
|
|
|
{
|
|
|
|
// clove is DeliveryStatus
|
|
|
|
size += CreateDeliveryStatusClove (payload + size, msgID);
|
|
|
|
if (size > 0) // successive?
|
|
|
|
{
|
|
|
|
(*numCloves)++;
|
2014-10-17 04:12:46 +04:00
|
|
|
m_UnconfirmedTagsMsgs[msgID] = newTags;
|
2014-10-08 05:47:32 +04:00
|
|
|
m_Owner->DeliveryStatusSent (this, msgID);
|
2014-08-15 22:34:58 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("DeliveryStatus clove was not created");
|
|
|
|
}
|
2014-10-08 20:11:39 +04:00
|
|
|
if (m_LeaseSetUpdated)
|
2014-08-15 22:34:58 +04:00
|
|
|
{
|
2014-10-08 20:11:39 +04:00
|
|
|
m_LeaseSetUpdated = false;
|
2014-08-15 22:34:58 +04:00
|
|
|
// clove if our leaseSet must be attached
|
2014-10-08 05:08:00 +04:00
|
|
|
auto leaseSet = CreateDatabaseStoreMsg (m_Owner->GetLeaseSet ());
|
2014-08-15 22:34:58 +04:00
|
|
|
size += CreateGarlicClove (payload + size, leaseSet, false);
|
|
|
|
DeleteI2NPMessage (leaseSet);
|
|
|
|
(*numCloves)++;
|
|
|
|
}
|
2013-12-31 05:46:33 +04:00
|
|
|
}
|
2014-01-09 07:47:22 +04:00
|
|
|
if (msg) // clove message ifself if presented
|
2013-12-31 05:46:33 +04:00
|
|
|
{
|
2014-07-28 19:54:34 +04:00
|
|
|
size += CreateGarlicClove (payload + size, msg, m_Destination ? m_Destination->IsDestination () : false);
|
2013-12-31 05:46:33 +04:00
|
|
|
(*numCloves)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset (payload + size, 0, 3); // certificate of message
|
|
|
|
size += 3;
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (payload + size, msgID); // MessageID
|
2013-12-31 05:46:33 +04:00
|
|
|
size += 4;
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe64buf (payload + size, ts); // Expiration of message
|
2013-12-31 05:46:33 +04:00
|
|
|
size += 8;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2014-03-20 17:47:02 +04:00
|
|
|
size_t GarlicRoutingSession::CreateGarlicClove (uint8_t * buf, const I2NPMessage * msg, bool isDestination)
|
2013-12-31 05:46:33 +04:00
|
|
|
{
|
|
|
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
|
|
|
|
size_t size = 0;
|
2014-07-28 19:54:34 +04:00
|
|
|
if (isDestination && m_Destination)
|
2013-11-27 05:59:25 +04:00
|
|
|
{
|
2013-12-31 05:46:33 +04:00
|
|
|
buf[size] = eGarlicDeliveryTypeDestination << 5;// delivery instructions flag destination
|
2013-11-27 05:59:25 +04:00
|
|
|
size++;
|
2014-07-28 19:54:34 +04:00
|
|
|
memcpy (buf + size, m_Destination->GetIdentHash (), 32);
|
2013-11-27 05:59:25 +04:00
|
|
|
size += 32;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-31 05:46:33 +04:00
|
|
|
buf[size] = 0;// delivery instructions flag local
|
2013-11-27 05:59:25 +04:00
|
|
|
size++;
|
|
|
|
}
|
2013-12-31 05:46:33 +04:00
|
|
|
|
|
|
|
memcpy (buf + size, msg->GetBuffer (), msg->GetLength ());
|
2013-11-24 01:35:15 +04:00
|
|
|
size += msg->GetLength ();
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (buf + size, m_Rnd.GenerateWord32 ()); // CloveID
|
2013-11-24 01:35:15 +04:00
|
|
|
size += 4;
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe64buf (buf + size, ts); // Expiration of clove
|
2013-11-24 01:35:15 +04:00
|
|
|
size += 8;
|
2013-12-31 05:46:33 +04:00
|
|
|
memset (buf + size, 0, 3); // certificate of clove
|
2013-11-24 01:35:15 +04:00
|
|
|
size += 3;
|
|
|
|
return size;
|
|
|
|
}
|
2014-01-09 07:47:22 +04:00
|
|
|
|
|
|
|
size_t GarlicRoutingSession::CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID)
|
|
|
|
{
|
|
|
|
size_t size = 0;
|
2014-10-08 05:08:00 +04:00
|
|
|
if (m_Owner)
|
2014-08-15 22:34:58 +04:00
|
|
|
{
|
2014-10-08 05:08:00 +04:00
|
|
|
auto leases = m_Owner->GetLeaseSet ()->GetNonExpiredLeases ();
|
2014-08-15 22:34:58 +04:00
|
|
|
if (!leases.empty ())
|
|
|
|
{
|
|
|
|
buf[size] = eGarlicDeliveryTypeTunnel << 5; // delivery instructions flag tunnel
|
|
|
|
size++;
|
|
|
|
uint32_t i = m_Rnd.GenerateWord32 (0, leases.size () - 1);
|
|
|
|
// hash and tunnelID sequence is reversed for Garlic
|
|
|
|
memcpy (buf + size, leases[i].tunnelGateway, 32); // To Hash
|
|
|
|
size += 32;
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (buf + size, leases[i].tunnelID); // tunnelID
|
2014-08-15 22:34:58 +04:00
|
|
|
size += 4;
|
|
|
|
// create msg
|
|
|
|
I2NPMessage * msg = CreateDeliveryStatusMsg (msgID);
|
2014-11-19 22:56:47 +03:00
|
|
|
if (m_Owner)
|
|
|
|
{
|
|
|
|
//encrypt
|
|
|
|
uint8_t key[32], tag[32];
|
|
|
|
m_Rnd.GenerateBlock (key, 32); // random session key
|
|
|
|
m_Rnd.GenerateBlock (tag, 32); // random session tag
|
2014-12-09 05:28:11 +03:00
|
|
|
m_Owner->SubmitSessionKey (key, tag);
|
2014-11-19 22:56:47 +03:00
|
|
|
GarlicRoutingSession garlic (key, tag);
|
|
|
|
msg = garlic.WrapSingleMessage (msg);
|
|
|
|
}
|
2014-08-15 22:34:58 +04:00
|
|
|
memcpy (buf + size, msg->GetBuffer (), msg->GetLength ());
|
|
|
|
size += msg->GetLength ();
|
|
|
|
DeleteI2NPMessage (msg);
|
|
|
|
// fill clove
|
|
|
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (buf + size, m_Rnd.GenerateWord32 ()); // CloveID
|
2014-08-15 22:34:58 +04:00
|
|
|
size += 4;
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe64buf (buf + size, ts); // Expiration of clove
|
2014-08-15 22:34:58 +04:00
|
|
|
size += 8;
|
|
|
|
memset (buf + size, 0, 3); // certificate of clove
|
|
|
|
size += 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("All tunnels of local LeaseSet expired");
|
2014-01-09 07:47:22 +04:00
|
|
|
}
|
2014-08-15 22:34:58 +04:00
|
|
|
else
|
|
|
|
LogPrint ("Missing local LeaseSet");
|
|
|
|
|
2014-01-09 07:47:22 +04:00
|
|
|
return size;
|
|
|
|
}
|
2013-11-25 03:10:27 +04:00
|
|
|
|
2014-10-08 00:18:13 +04:00
|
|
|
GarlicDestination::~GarlicDestination ()
|
2013-11-25 03:10:27 +04:00
|
|
|
{
|
|
|
|
for (auto it: m_Sessions)
|
|
|
|
delete it.second;
|
|
|
|
m_Sessions.clear ();
|
2014-10-08 00:18:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void GarlicDestination::AddSessionKey (const uint8_t * key, const uint8_t * tag)
|
|
|
|
{
|
|
|
|
if (key)
|
|
|
|
{
|
2014-11-19 22:56:47 +03:00
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2014-10-08 00:18:13 +04:00
|
|
|
auto decryption = std::make_shared<i2p::crypto::CBCDecryption>();
|
|
|
|
decryption->SetKey (key);
|
2014-11-19 22:56:47 +03:00
|
|
|
m_Tags[SessionTag(tag, ts)] = decryption;
|
2014-10-08 00:18:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 23:36:00 +03:00
|
|
|
bool GarlicDestination::SubmitSessionKey (const uint8_t * key, const uint8_t * tag)
|
|
|
|
{
|
|
|
|
AddSessionKey (key, tag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-08 00:18:13 +04:00
|
|
|
void GarlicDestination::HandleGarlicMessage (I2NPMessage * msg)
|
|
|
|
{
|
|
|
|
uint8_t * buf = msg->GetPayload ();
|
2014-12-30 01:04:02 +03:00
|
|
|
uint32_t length = bufbe32toh (buf);
|
2014-11-19 22:56:47 +03:00
|
|
|
buf += 4; // length
|
2014-10-08 00:18:13 +04:00
|
|
|
auto it = m_Tags.find (SessionTag(buf));
|
|
|
|
if (it != m_Tags.end ())
|
|
|
|
{
|
|
|
|
// tag found. Use AES
|
|
|
|
uint8_t iv[32]; // IV is first 16 bytes
|
|
|
|
CryptoPP::SHA256().CalculateDigest(iv, buf, 32);
|
|
|
|
it->second->SetIV (iv);
|
|
|
|
it->second->Decrypt (buf + 32, length - 32, buf + 32);
|
|
|
|
HandleAESBlock (buf + 32, length - 32, it->second, msg->from);
|
|
|
|
m_Tags.erase (it); // tag might be used only once
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// tag not found. Use ElGamal
|
|
|
|
ElGamalBlock elGamal;
|
|
|
|
if (i2p::crypto::ElGamalDecrypt (GetEncryptionPrivateKey (), buf, (uint8_t *)&elGamal, true))
|
|
|
|
{
|
|
|
|
auto decryption = std::make_shared<i2p::crypto::CBCDecryption>();
|
|
|
|
decryption->SetKey (elGamal.sessionKey);
|
|
|
|
uint8_t iv[32]; // IV is first 16 bytes
|
|
|
|
CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32);
|
|
|
|
decryption->SetIV (iv);
|
|
|
|
decryption->Decrypt(buf + 514, length - 514, buf + 514);
|
|
|
|
HandleAESBlock (buf + 514, length - 514, decryption, msg->from);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("Failed to decrypt garlic");
|
|
|
|
}
|
2014-10-18 06:15:42 +04:00
|
|
|
DeleteI2NPMessage (msg);
|
|
|
|
|
|
|
|
// cleanup expired tags
|
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
if (ts > m_LastTagsCleanupTime + INCOMING_TAGS_EXPIRATION_TIMEOUT)
|
|
|
|
{
|
|
|
|
if (m_LastTagsCleanupTime)
|
|
|
|
{
|
|
|
|
int numExpiredTags = 0;
|
|
|
|
for (auto it = m_Tags.begin (); it != m_Tags.end ();)
|
|
|
|
{
|
|
|
|
if (ts > it->first.creationTime + INCOMING_TAGS_EXPIRATION_TIMEOUT)
|
|
|
|
{
|
|
|
|
numExpiredTags++;
|
|
|
|
it = m_Tags.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
LogPrint (numExpiredTags, " tags expired for ", GetIdentHash().ToBase64 ());
|
|
|
|
}
|
|
|
|
m_LastTagsCleanupTime = ts;
|
|
|
|
}
|
2014-07-08 06:25:32 +04:00
|
|
|
}
|
2014-01-18 19:34:57 +04:00
|
|
|
|
2014-10-08 00:18:13 +04:00
|
|
|
void GarlicDestination::HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr<i2p::crypto::CBCDecryption> decryption,
|
|
|
|
i2p::tunnel::InboundTunnel * from)
|
|
|
|
{
|
2014-12-30 01:04:02 +03:00
|
|
|
uint16_t tagCount = bufbe16toh (buf);
|
2014-12-08 05:00:19 +03:00
|
|
|
buf += 2; len -= 2;
|
2014-10-08 00:18:13 +04:00
|
|
|
if (tagCount > 0)
|
|
|
|
{
|
2014-12-08 05:00:19 +03:00
|
|
|
if (tagCount*32 > len)
|
|
|
|
{
|
2014-12-08 23:36:00 +03:00
|
|
|
LogPrint (eLogError, "Tag count ", tagCount, " exceeds length ", len);
|
|
|
|
return ;
|
2014-12-08 05:00:19 +03:00
|
|
|
}
|
2014-10-17 19:26:57 +04:00
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2014-10-08 00:18:13 +04:00
|
|
|
for (int i = 0; i < tagCount; i++)
|
2014-10-17 19:26:57 +04:00
|
|
|
m_Tags[SessionTag(buf + i*32, ts)] = decryption;
|
2014-10-08 00:18:13 +04:00
|
|
|
}
|
|
|
|
buf += tagCount*32;
|
2014-12-08 05:00:19 +03:00
|
|
|
len -= tagCount*32;
|
2014-12-30 01:04:02 +03:00
|
|
|
uint32_t payloadSize = bufbe32toh (buf);
|
2014-10-08 00:18:13 +04:00
|
|
|
if (payloadSize > len)
|
|
|
|
{
|
2014-12-08 05:00:19 +03:00
|
|
|
LogPrint (eLogError, "Unexpected payload size ", payloadSize);
|
2014-10-08 00:18:13 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf += 4;
|
|
|
|
uint8_t * payloadHash = buf;
|
|
|
|
buf += 32;// payload hash.
|
|
|
|
if (*buf) // session key?
|
|
|
|
buf += 32; // new session key
|
|
|
|
buf++; // flag
|
|
|
|
|
|
|
|
// payload
|
2014-12-22 05:32:21 +03:00
|
|
|
if (!CryptoPP::SHA256().VerifyDigest (payloadHash, buf, payloadSize)) // payload hash doesn't match
|
2014-10-08 00:18:13 +04:00
|
|
|
{
|
|
|
|
LogPrint ("Wrong payload hash");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
HandleGarlicPayload (buf, payloadSize, from);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GarlicDestination::HandleGarlicPayload (uint8_t * buf, size_t len, i2p::tunnel::InboundTunnel * from)
|
|
|
|
{
|
|
|
|
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");
|
2014-10-14 01:45:07 +04:00
|
|
|
HandleI2NPMessage (buf, len, from);
|
2014-10-08 00:18:13 +04:00
|
|
|
break;
|
2014-10-12 05:27:55 +04:00
|
|
|
case eGarlicDeliveryTypeDestination:
|
2014-10-08 00:18:13 +04:00
|
|
|
LogPrint ("Garlic type destination");
|
|
|
|
buf += 32; // destination. check it later or for multiple destinations
|
2014-10-14 01:45:07 +04:00
|
|
|
HandleI2NPMessage (buf, len, from);
|
2014-10-12 05:27:55 +04:00
|
|
|
break;
|
2014-10-08 00:18:13 +04:00
|
|
|
case eGarlicDeliveryTypeTunnel:
|
|
|
|
{
|
|
|
|
LogPrint ("Garlic type tunnel");
|
|
|
|
// gwHash and gwTunnel sequence is reverted
|
|
|
|
uint8_t * gwHash = buf;
|
|
|
|
buf += 32;
|
2014-12-30 01:04:02 +03:00
|
|
|
uint32_t gwTunnel = bufbe32toh (buf);
|
2014-10-08 00:18:13 +04:00
|
|
|
buf += 4;
|
|
|
|
i2p::tunnel::OutboundTunnel * tunnel = nullptr;
|
|
|
|
if (from && from->GetTunnelPool ())
|
|
|
|
tunnel = from->GetTunnelPool ()->GetNextOutboundTunnel ();
|
|
|
|
if (tunnel) // we have send it through an outbound tunnel
|
|
|
|
{
|
|
|
|
I2NPMessage * msg = CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from);
|
|
|
|
tunnel->SendTunnelDataMsg (gwHash, gwTunnel, msg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("No outbound tunnels available for garlic clove");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eGarlicDeliveryTypeRouter:
|
|
|
|
LogPrint ("Garlic type router not supported");
|
|
|
|
buf += 32;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LogPrint ("Unknow garlic delivery type ", (int)deliveryType);
|
|
|
|
}
|
|
|
|
buf += GetI2NPMessageLength (buf); // I2NP
|
|
|
|
buf += 4; // CloveID
|
|
|
|
buf += 8; // Date
|
|
|
|
buf += 3; // Certificate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
I2NPMessage * GarlicDestination::WrapMessage (const i2p::data::RoutingDestination& destination,
|
2014-10-08 20:11:39 +04:00
|
|
|
I2NPMessage * msg, bool attachLeaseSet)
|
2014-10-08 00:18:13 +04:00
|
|
|
{
|
2014-11-16 23:41:54 +03:00
|
|
|
if (attachLeaseSet) // we should maintain this session
|
|
|
|
{
|
|
|
|
auto session = GetRoutingSession (destination, 32); // 32 tags by default
|
|
|
|
return session->WrapSingleMessage (msg);
|
|
|
|
}
|
|
|
|
else // one time session
|
|
|
|
{
|
|
|
|
GarlicRoutingSession session (this, &destination, 0); // don't use tag if no LeaseSet
|
|
|
|
return session.WrapSingleMessage (msg);
|
|
|
|
}
|
2014-10-08 00:18:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GarlicRoutingSession * GarlicDestination::GetRoutingSession (
|
2014-08-30 06:10:00 +04:00
|
|
|
const i2p::data::RoutingDestination& destination, int numTags)
|
2013-11-25 03:10:27 +04:00
|
|
|
{
|
2014-02-21 03:42:55 +04:00
|
|
|
auto it = m_Sessions.find (destination.GetIdentHash ());
|
2013-11-25 03:10:27 +04:00
|
|
|
GarlicRoutingSession * session = nullptr;
|
|
|
|
if (it != m_Sessions.end ())
|
|
|
|
session = it->second;
|
|
|
|
if (!session)
|
|
|
|
{
|
2014-10-08 05:08:00 +04:00
|
|
|
session = new GarlicRoutingSession (this, &destination, numTags);
|
2014-08-25 21:07:14 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
2014-02-21 03:42:55 +04:00
|
|
|
m_Sessions[destination.GetIdentHash ()] = session;
|
2013-11-25 03:10:27 +04:00
|
|
|
}
|
2014-08-30 06:10:00 +04:00
|
|
|
return session;
|
|
|
|
}
|
2014-11-16 23:41:54 +03:00
|
|
|
|
2014-10-08 05:47:32 +04:00
|
|
|
void GarlicDestination::DeliveryStatusSent (GarlicRoutingSession * session, uint32_t msgID)
|
2014-08-30 06:10:00 +04:00
|
|
|
{
|
|
|
|
m_CreatedSessions[msgID] = session;
|
2014-10-08 05:47:32 +04:00
|
|
|
}
|
2014-01-17 17:12:57 +04:00
|
|
|
|
2014-10-08 05:47:32 +04:00
|
|
|
void GarlicDestination::HandleDeliveryStatusMessage (I2NPMessage * msg)
|
2014-01-17 17:12:57 +04:00
|
|
|
{
|
2014-12-30 23:33:11 +03:00
|
|
|
uint32_t msgID = bufbe32toh (msg->GetPayload ());
|
2014-01-17 17:12:57 +04:00
|
|
|
{
|
2014-08-31 16:56:03 +04:00
|
|
|
auto it = m_CreatedSessions.find (msgID);
|
|
|
|
if (it != m_CreatedSessions.end ())
|
|
|
|
{
|
2014-10-14 21:49:40 +04:00
|
|
|
it->second->TagsConfirmed (msgID);
|
2014-08-31 16:56:03 +04:00
|
|
|
m_CreatedSessions.erase (it);
|
|
|
|
LogPrint ("Garlic message ", msgID, " acknowledged");
|
|
|
|
}
|
2014-10-08 05:47:32 +04:00
|
|
|
}
|
2014-08-16 00:15:17 +04:00
|
|
|
DeleteI2NPMessage (msg);
|
2014-10-08 05:47:32 +04:00
|
|
|
}
|
2014-10-08 20:11:39 +04:00
|
|
|
|
|
|
|
void GarlicDestination::SetLeaseSetUpdated ()
|
|
|
|
{
|
2014-10-08 22:17:17 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
2014-10-08 20:11:39 +04:00
|
|
|
for (auto it: m_Sessions)
|
|
|
|
it.second->SetLeaseSetUpdated ();
|
|
|
|
}
|
2014-10-08 22:17:17 +04:00
|
|
|
|
|
|
|
void GarlicDestination::ProcessGarlicMessage (I2NPMessage * msg)
|
|
|
|
{
|
|
|
|
HandleGarlicMessage (msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GarlicDestination::ProcessDeliveryStatusMessage (I2NPMessage * msg)
|
|
|
|
{
|
|
|
|
HandleDeliveryStatusMessage (msg);
|
|
|
|
}
|
|
|
|
|
2013-11-25 03:10:27 +04:00
|
|
|
}
|
|
|
|
}
|