mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
788 lines
24 KiB
788 lines
24 KiB
11 years ago
|
#include <inttypes.h>
|
||
11 years ago
|
#include "I2PEndian.h"
|
||
11 years ago
|
#include <map>
|
||
|
#include <string>
|
||
9 years ago
|
#include "Crypto.h"
|
||
11 years ago
|
#include "RouterContext.h"
|
||
11 years ago
|
#include "I2NPProtocol.h"
|
||
|
#include "Tunnel.h"
|
||
11 years ago
|
#include "TunnelPool.h"
|
||
9 years ago
|
#include "Transports.h"
|
||
11 years ago
|
#include "Timestamp.h"
|
||
9 years ago
|
#include "Log.h"
|
||
8 years ago
|
#include "FS.h"
|
||
11 years ago
|
#include "Garlic.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
11 years ago
|
namespace garlic
|
||
10 years ago
|
{
|
||
10 years ago
|
GarlicRoutingSession::GarlicRoutingSession (GarlicDestination * owner,
|
||
10 years ago
|
std::shared_ptr<const i2p::data::RoutingDestination> destination, int numTags, bool attachLeaseSet):
|
||
8 years ago
|
m_Owner (owner), m_Destination (destination), m_NumTags (numTags),
|
||
9 years ago
|
m_LeaseSetUpdateStatus (attachLeaseSet ? eLeaseSetUpdated : eLeaseSetDoNotSend),
|
||
8 years ago
|
m_LeaseSetUpdateMsgID (0)
|
||
11 years ago
|
{
|
||
11 years ago
|
// create new session tags and session key
|
||
9 years ago
|
RAND_bytes (m_SessionKey, 32);
|
||
11 years ago
|
m_Encryption.SetKey (m_SessionKey);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
GarlicRoutingSession::GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag):
|
||
8 years ago
|
m_Owner (nullptr), m_NumTags (1), m_LeaseSetUpdateStatus (eLeaseSetDoNotSend), m_LeaseSetUpdateMsgID (0)
|
||
11 years ago
|
{
|
||
|
memcpy (m_SessionKey, sessionKey, 32);
|
||
|
m_Encryption.SetKey (m_SessionKey);
|
||
10 years ago
|
m_SessionTags.push_back (sessionTag);
|
||
10 years ago
|
m_SessionTags.back ().creationTime = i2p::util::GetSecondsSinceEpoch ();
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
GarlicRoutingSession::~GarlicRoutingSession ()
|
||
|
{
|
||
|
}
|
||
9 years ago
|
|
||
|
std::shared_ptr<GarlicRoutingPath> GarlicRoutingSession::GetSharedRoutingPath ()
|
||
|
{
|
||
|
if (!m_SharedRoutingPath) return nullptr;
|
||
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
||
9 years ago
|
if (m_SharedRoutingPath->numTimesUsed >= ROUTING_PATH_MAX_NUM_TIMES_USED ||
|
||
|
!m_SharedRoutingPath->outboundTunnel->IsEstablished () ||
|
||
9 years ago
|
ts*1000LL > m_SharedRoutingPath->remoteLease->endDate ||
|
||
|
ts > m_SharedRoutingPath->updateTime + ROUTING_PATH_EXPIRATION_TIMEOUT)
|
||
|
m_SharedRoutingPath = nullptr;
|
||
9 years ago
|
if (m_SharedRoutingPath) m_SharedRoutingPath->numTimesUsed++;
|
||
9 years ago
|
return m_SharedRoutingPath;
|
||
|
}
|
||
|
|
||
|
void GarlicRoutingSession::SetSharedRoutingPath (std::shared_ptr<GarlicRoutingPath> path)
|
||
|
{
|
||
|
if (path && path->outboundTunnel && path->remoteLease)
|
||
9 years ago
|
{
|
||
9 years ago
|
path->updateTime = i2p::util::GetSecondsSinceEpoch ();
|
||
9 years ago
|
path->numTimesUsed = 0;
|
||
|
}
|
||
9 years ago
|
else
|
||
|
path = nullptr;
|
||
|
m_SharedRoutingPath = path;
|
||
|
}
|
||
|
|
||
10 years ago
|
GarlicRoutingSession::UnconfirmedTags * GarlicRoutingSession::GenerateSessionTags ()
|
||
11 years ago
|
{
|
||
10 years ago
|
auto tags = new UnconfirmedTags (m_NumTags);
|
||
10 years ago
|
tags->tagsCreationTime = i2p::util::GetSecondsSinceEpoch ();
|
||
10 years ago
|
for (int i = 0; i < m_NumTags; i++)
|
||
10 years ago
|
{
|
||
9 years ago
|
RAND_bytes (tags->sessionTags[i], 32);
|
||
10 years ago
|
tags->sessionTags[i].creationTime = tags->tagsCreationTime;
|
||
|
}
|
||
10 years ago
|
return tags;
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
void GarlicRoutingSession::MessageConfirmed (uint32_t msgID)
|
||
|
{
|
||
|
TagsConfirmed (msgID);
|
||
|
if (msgID == m_LeaseSetUpdateMsgID)
|
||
|
{
|
||
|
m_LeaseSetUpdateStatus = eLeaseSetUpToDate;
|
||
8 years ago
|
m_LeaseSetUpdateMsgID = 0;
|
||
9 years ago
|
LogPrint (eLogInfo, "Garlic: LeaseSet update confirmed");
|
||
10 years ago
|
}
|
||
|
else
|
||
|
CleanupExpiredTags ();
|
||
|
}
|
||
|
|
||
10 years ago
|
void GarlicRoutingSession::TagsConfirmed (uint32_t msgID)
|
||
|
{
|
||
9 years ago
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
||
8 years ago
|
auto it = m_UnconfirmedTagsMsgs.find (msgID);
|
||
|
if (it != m_UnconfirmedTagsMsgs.end ())
|
||
10 years ago
|
{
|
||
8 years ago
|
auto& tags = it->second;
|
||
|
if (ts < tags->tagsCreationTime + OUTGOING_TAGS_EXPIRATION_TIMEOUT)
|
||
|
{
|
||
|
for (int i = 0; i < tags->numTags; i++)
|
||
|
m_SessionTags.push_back (tags->sessionTags[i]);
|
||
9 years ago
|
}
|
||
8 years ago
|
m_UnconfirmedTagsMsgs.erase (it);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
bool GarlicRoutingSession::CleanupExpiredTags ()
|
||
|
{
|
||
8 years ago
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
||
10 years ago
|
for (auto it = m_SessionTags.begin (); it != m_SessionTags.end ();)
|
||
|
{
|
||
|
if (ts >= it->creationTime + OUTGOING_TAGS_EXPIRATION_TIMEOUT)
|
||
|
it = m_SessionTags.erase (it);
|
||
|
else
|
||
8 years ago
|
++it;
|
||
10 years ago
|
}
|
||
9 years ago
|
CleanupUnconfirmedTags ();
|
||
8 years ago
|
if (m_LeaseSetUpdateMsgID && ts*1000LL > m_LeaseSetSubmissionTime + LEASET_CONFIRMATION_TIMEOUT)
|
||
|
{
|
||
|
if (m_Owner)
|
||
|
m_Owner->RemoveDeliveryStatusSession (m_LeaseSetUpdateMsgID);
|
||
|
m_LeaseSetUpdateMsgID = 0;
|
||
|
}
|
||
9 years ago
|
return !m_SessionTags.empty () || !m_UnconfirmedTagsMsgs.empty ();
|
||
|
}
|
||
|
|
||
|
bool GarlicRoutingSession::CleanupUnconfirmedTags ()
|
||
|
{
|
||
|
bool ret = false;
|
||
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
||
10 years ago
|
// delete expired unconfirmed tags
|
||
|
for (auto it = m_UnconfirmedTagsMsgs.begin (); it != m_UnconfirmedTagsMsgs.end ();)
|
||
|
{
|
||
8 years ago
|
if (ts >= it->second->tagsCreationTime + OUTGOING_TAGS_CONFIRMATION_TIMEOUT)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (m_Owner)
|
||
8 years ago
|
m_Owner->RemoveDeliveryStatusSession (it->first);
|
||
10 years ago
|
it = m_UnconfirmedTagsMsgs.erase (it);
|
||
9 years ago
|
ret = true;
|
||
10 years ago
|
}
|
||
|
else
|
||
8 years ago
|
++it;
|
||
10 years ago
|
}
|
||
9 years ago
|
return ret;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> GarlicRoutingSession::WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg)
|
||
11 years ago
|
{
|
||
9 years ago
|
auto m = NewI2NPMessage ();
|
||
10 years ago
|
m->Align (12); // in order to get buf aligned to 16 (12 + 4)
|
||
11 years ago
|
size_t len = 0;
|
||
11 years ago
|
uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length
|
||
11 years ago
|
|
||
10 years ago
|
// find non-expired tag
|
||
|
bool tagFound = false;
|
||
|
SessionTag tag;
|
||
11 years ago
|
if (m_NumTags > 0)
|
||
|
{
|
||
10 years ago
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
||
10 years ago
|
while (!m_SessionTags.empty ())
|
||
11 years ago
|
{
|
||
10 years ago
|
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
|
||
|
}
|
||
11 years ago
|
}
|
||
|
// create message
|
||
10 years ago
|
if (!tagFound) // new session
|
||
11 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogInfo, "Garlic: No tags available, will use ElGamal");
|
||
8 years ago
|
if (!m_Destination)
|
||
11 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogError, "Garlic: Can't use ElGamal for unknown destination");
|
||
11 years ago
|
return nullptr;
|
||
|
}
|
||
11 years ago
|
// create ElGamal block
|
||
|
ElGamalBlock elGamal;
|
||
|
memcpy (elGamal.sessionKey, m_SessionKey, 32);
|
||
9 years ago
|
RAND_bytes (elGamal.preIV, 32); // Pre-IV
|
||
11 years ago
|
uint8_t iv[32]; // IV is first 16 bytes
|
||
9 years ago
|
SHA256(elGamal.preIV, 32, iv);
|
||
8 years ago
|
BN_CTX * ctx = BN_CTX_new ();
|
||
|
i2p::crypto::ElGamalEncrypt (m_Destination->GetEncryptionPublicKey (), (uint8_t *)&elGamal, buf, ctx, true);
|
||
|
BN_CTX_free (ctx);
|
||
11 years ago
|
m_Encryption.SetIV (iv);
|
||
11 years ago
|
buf += 514;
|
||
|
len += 514;
|
||
11 years ago
|
}
|
||
|
else // existing session
|
||
|
{
|
||
|
// session tag
|
||
10 years ago
|
memcpy (buf, tag, 32);
|
||
11 years ago
|
uint8_t iv[32]; // IV is first 16 bytes
|
||
9 years ago
|
SHA256(tag, 32, iv);
|
||
11 years ago
|
m_Encryption.SetIV (iv);
|
||
11 years ago
|
buf += 32;
|
||
10 years ago
|
len += 32;
|
||
11 years ago
|
}
|
||
11 years ago
|
// AES block
|
||
9 years ago
|
len += CreateAESBlock (buf, msg);
|
||
10 years ago
|
htobe32buf (m->GetPayload (), len);
|
||
11 years ago
|
m->len += len + 4;
|
||
10 years ago
|
m->FillI2NPMessageHeader (eI2NPGarlic);
|
||
11 years ago
|
return m;
|
||
|
}
|
||
|
|
||
9 years ago
|
size_t GarlicRoutingSession::CreateAESBlock (uint8_t * buf, std::shared_ptr<const I2NPMessage> msg)
|
||
11 years ago
|
{
|
||
11 years ago
|
size_t blockSize = 0;
|
||
10 years ago
|
bool createNewTags = m_Owner && m_NumTags && ((int)m_SessionTags.size () <= m_NumTags*2/3);
|
||
10 years ago
|
UnconfirmedTags * newTags = createNewTags ? GenerateSessionTags () : nullptr;
|
||
10 years ago
|
htobuf16 (buf, newTags ? htobe16 (newTags->numTags) : 0); // tag count
|
||
11 years ago
|
blockSize += 2;
|
||
10 years ago
|
if (newTags) // session tags recreated
|
||
11 years ago
|
{
|
||
10 years ago
|
for (int i = 0; i < newTags->numTags; i++)
|
||
11 years ago
|
{
|
||
10 years ago
|
memcpy (buf + blockSize, newTags->sessionTags[i], 32); // tags
|
||
11 years ago
|
blockSize += 32;
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
uint32_t * payloadSize = (uint32_t *)(buf + blockSize);
|
||
|
blockSize += 4;
|
||
|
uint8_t * payloadHash = buf + blockSize;
|
||
|
blockSize += 32;
|
||
|
buf[blockSize] = 0; // flag
|
||
|
blockSize++;
|
||
10 years ago
|
size_t len = CreateGarlicPayload (buf + blockSize, msg, newTags);
|
||
10 years ago
|
htobe32buf (payloadSize, len);
|
||
9 years ago
|
SHA256(buf + blockSize, len, payloadHash);
|
||
11 years ago
|
blockSize += len;
|
||
|
size_t rem = blockSize % 16;
|
||
|
if (rem)
|
||
|
blockSize += (16-rem); //padding
|
||
11 years ago
|
m_Encryption.Encrypt(buf, blockSize, buf);
|
||
11 years ago
|
return blockSize;
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
size_t GarlicRoutingSession::CreateGarlicPayload (uint8_t * payload, std::shared_ptr<const I2NPMessage> msg, UnconfirmedTags * newTags)
|
||
11 years ago
|
{
|
||
8 years ago
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch ();
|
||
9 years ago
|
uint32_t msgID;
|
||
|
RAND_bytes ((uint8_t *)&msgID, 4);
|
||
11 years ago
|
size_t size = 0;
|
||
11 years ago
|
uint8_t * numCloves = payload + size;
|
||
|
*numCloves = 0;
|
||
11 years ago
|
size++;
|
||
11 years ago
|
|
||
10 years ago
|
if (m_Owner)
|
||
10 years ago
|
{
|
||
10 years ago
|
// resubmit non-confirmed LeaseSet
|
||
8 years ago
|
if (m_LeaseSetUpdateStatus == eLeaseSetSubmitted && ts > m_LeaseSetSubmissionTime + LEASET_CONFIRMATION_TIMEOUT)
|
||
|
{
|
||
|
m_LeaseSetUpdateStatus = eLeaseSetUpdated;
|
||
|
SetSharedRoutingPath (nullptr); // invalidate path since leaseset was not confirmed
|
||
|
}
|
||
10 years ago
|
|
||
|
// attach DeviveryStatus if necessary
|
||
|
if (newTags || m_LeaseSetUpdateStatus == eLeaseSetUpdated) // new tags created or leaseset updated
|
||
10 years ago
|
{
|
||
|
// clove is DeliveryStatus
|
||
10 years ago
|
auto cloveSize = CreateDeliveryStatusClove (payload + size, msgID);
|
||
|
if (cloveSize > 0) // successive?
|
||
10 years ago
|
{
|
||
10 years ago
|
size += cloveSize;
|
||
10 years ago
|
(*numCloves)++;
|
||
10 years ago
|
if (newTags) // new tags created
|
||
9 years ago
|
{
|
||
|
newTags->msgID = msgID;
|
||
8 years ago
|
m_UnconfirmedTagsMsgs.insert (std::make_pair(msgID, std::unique_ptr<UnconfirmedTags>(newTags)));
|
||
8 years ago
|
newTags = nullptr; // got acquired
|
||
9 years ago
|
}
|
||
10 years ago
|
m_Owner->DeliveryStatusSent (shared_from_this (), msgID);
|
||
10 years ago
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogWarning, "Garlic: DeliveryStatus clove was not created");
|
||
10 years ago
|
}
|
||
10 years ago
|
// attach LeaseSet
|
||
|
if (m_LeaseSetUpdateStatus == eLeaseSetUpdated)
|
||
10 years ago
|
{
|
||
8 years ago
|
if (m_LeaseSetUpdateMsgID) m_Owner->RemoveDeliveryStatusSession (m_LeaseSetUpdateMsgID); // remove previous
|
||
10 years ago
|
m_LeaseSetUpdateStatus = eLeaseSetSubmitted;
|
||
|
m_LeaseSetUpdateMsgID = msgID;
|
||
8 years ago
|
m_LeaseSetSubmissionTime = ts;
|
||
10 years ago
|
// clove if our leaseSet must be attached
|
||
10 years ago
|
auto leaseSet = CreateDatabaseStoreMsg (m_Owner->GetLeaseSet ());
|
||
9 years ago
|
size += CreateGarlicClove (payload + size, leaseSet, false);
|
||
10 years ago
|
(*numCloves)++;
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
if (msg) // clove message ifself if presented
|
||
11 years ago
|
{
|
||
8 years ago
|
size += CreateGarlicClove (payload + size, msg, m_Destination ? m_Destination->IsDestination () : false);
|
||
11 years ago
|
(*numCloves)++;
|
||
|
}
|
||
|
memset (payload + size, 0, 3); // certificate of message
|
||
|
size += 3;
|
||
10 years ago
|
htobe32buf (payload + size, msgID); // MessageID
|
||
11 years ago
|
size += 4;
|
||
8 years ago
|
htobe64buf (payload + size, ts + 8000); // Expiration of message, 8 sec
|
||
11 years ago
|
size += 8;
|
||
8 years ago
|
|
||
|
if (newTags) delete newTags; // not acquired, delete
|
||
11 years ago
|
return size;
|
||
|
}
|
||
|
|
||
9 years ago
|
size_t GarlicRoutingSession::CreateGarlicClove (uint8_t * buf, std::shared_ptr<const I2NPMessage> msg, bool isDestination)
|
||
11 years ago
|
{
|
||
9 years ago
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 8000; // 8 sec
|
||
11 years ago
|
size_t size = 0;
|
||
8 years ago
|
if (isDestination)
|
||
11 years ago
|
{
|
||
11 years ago
|
buf[size] = eGarlicDeliveryTypeDestination << 5;// delivery instructions flag destination
|
||
11 years ago
|
size++;
|
||
8 years ago
|
memcpy (buf + size, m_Destination->GetIdentHash (), 32);
|
||
11 years ago
|
size += 32;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
11 years ago
|
buf[size] = 0;// delivery instructions flag local
|
||
11 years ago
|
size++;
|
||
|
}
|
||
11 years ago
|
|
||
|
memcpy (buf + size, msg->GetBuffer (), msg->GetLength ());
|
||
11 years ago
|
size += msg->GetLength ();
|
||
9 years ago
|
uint32_t cloveID;
|
||
|
RAND_bytes ((uint8_t *)&cloveID, 4);
|
||
|
htobe32buf (buf + size, cloveID); // CloveID
|
||
11 years ago
|
size += 4;
|
||
10 years ago
|
htobe64buf (buf + size, ts); // Expiration of clove
|
||
11 years ago
|
size += 8;
|
||
11 years ago
|
memset (buf + size, 0, 3); // certificate of clove
|
||
11 years ago
|
size += 3;
|
||
|
return size;
|
||
|
}
|
||
11 years ago
|
|
||
|
size_t GarlicRoutingSession::CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID)
|
||
|
{
|
||
|
size_t size = 0;
|
||
10 years ago
|
if (m_Owner)
|
||
10 years ago
|
{
|
||
10 years ago
|
auto inboundTunnel = m_Owner->GetTunnelPool ()->GetNextInboundTunnel ();
|
||
|
if (inboundTunnel)
|
||
10 years ago
|
{
|
||
|
buf[size] = eGarlicDeliveryTypeTunnel << 5; // delivery instructions flag tunnel
|
||
|
size++;
|
||
|
// hash and tunnelID sequence is reversed for Garlic
|
||
10 years ago
|
memcpy (buf + size, inboundTunnel->GetNextIdentHash (), 32); // To Hash
|
||
10 years ago
|
size += 32;
|
||
10 years ago
|
htobe32buf (buf + size, inboundTunnel->GetNextTunnelID ()); // tunnelID
|
||
10 years ago
|
size += 4;
|
||
|
// create msg
|
||
10 years ago
|
auto msg = CreateDeliveryStatusMsg (msgID);
|
||
10 years ago
|
if (m_Owner)
|
||
|
{
|
||
|
//encrypt
|
||
|
uint8_t key[32], tag[32];
|
||
9 years ago
|
RAND_bytes (key, 32); // random session key
|
||
|
RAND_bytes (tag, 32); // random session tag
|
||
10 years ago
|
m_Owner->SubmitSessionKey (key, tag);
|
||
10 years ago
|
GarlicRoutingSession garlic (key, tag);
|
||
|
msg = garlic.WrapSingleMessage (msg);
|
||
|
}
|
||
10 years ago
|
memcpy (buf + size, msg->GetBuffer (), msg->GetLength ());
|
||
|
size += msg->GetLength ();
|
||
|
// fill clove
|
||
9 years ago
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 8000; // 8 sec
|
||
9 years ago
|
uint32_t cloveID;
|
||
|
RAND_bytes ((uint8_t *)&cloveID, 4);
|
||
|
htobe32buf (buf + size, cloveID); // CloveID
|
||
10 years ago
|
size += 4;
|
||
10 years ago
|
htobe64buf (buf + size, ts); // Expiration of clove
|
||
10 years ago
|
size += 8;
|
||
|
memset (buf + size, 0, 3); // certificate of clove
|
||
|
size += 3;
|
||
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "Garlic: No inbound tunnels in the pool for DeliveryStatus");
|
||
11 years ago
|
}
|
||
10 years ago
|
else
|
||
9 years ago
|
LogPrint (eLogWarning, "Garlic: Missing local LeaseSet");
|
||
10 years ago
|
|
||
11 years ago
|
return size;
|
||
|
}
|
||
8 years ago
|
|
||
|
GarlicDestination::GarlicDestination (): m_NumTags (32) // 32 tags by default
|
||
|
{
|
||
|
m_Ctx = BN_CTX_new ();
|
||
|
}
|
||
|
|
||
10 years ago
|
GarlicDestination::~GarlicDestination ()
|
||
11 years ago
|
{
|
||
8 years ago
|
BN_CTX_free (m_Ctx);
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
void GarlicDestination::CleanUp ()
|
||
|
{
|
||
|
m_Sessions.clear ();
|
||
|
m_DeliveryStatusSessions.clear ();
|
||
|
m_Tags.clear ();
|
||
|
}
|
||
10 years ago
|
void GarlicDestination::AddSessionKey (const uint8_t * key, const uint8_t * tag)
|
||
|
{
|
||
|
if (key)
|
||
|
{
|
||
10 years ago
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
||
8 years ago
|
m_Tags[SessionTag(tag, ts)] = std::make_shared<AESDecryption>(key);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
bool GarlicDestination::SubmitSessionKey (const uint8_t * key, const uint8_t * tag)
|
||
|
{
|
||
|
AddSessionKey (key, tag);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
void GarlicDestination::HandleGarlicMessage (std::shared_ptr<I2NPMessage> msg)
|
||
10 years ago
|
{
|
||
|
uint8_t * buf = msg->GetPayload ();
|
||
10 years ago
|
uint32_t length = bufbe32toh (buf);
|
||
10 years ago
|
if (length > msg->GetLength ())
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "Garlic: message length ", length, " exceeds I2NP message length ", msg->GetLength ());
|
||
10 years ago
|
return;
|
||
|
}
|
||
10 years ago
|
buf += 4; // length
|
||
10 years ago
|
auto it = m_Tags.find (SessionTag(buf));
|
||
|
if (it != m_Tags.end ())
|
||
|
{
|
||
|
// tag found. Use AES
|
||
8 years ago
|
auto decryption = it->second;
|
||
|
m_Tags.erase (it); // tag might be used only once
|
||
10 years ago
|
if (length >= 32)
|
||
|
{
|
||
|
uint8_t iv[32]; // IV is first 16 bytes
|
||
9 years ago
|
SHA256(buf, 32, iv);
|
||
8 years ago
|
decryption->SetIV (iv);
|
||
|
decryption->Decrypt (buf + 32, length - 32, buf + 32);
|
||
|
HandleAESBlock (buf + 32, length - 32, decryption, msg->from);
|
||
10 years ago
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogWarning, "Garlic: message length ", length, " is less than 32 bytes");
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
// tag not found. Use ElGamal
|
||
|
ElGamalBlock elGamal;
|
||
8 years ago
|
if (length >= 514 && i2p::crypto::ElGamalDecrypt (GetEncryptionPrivateKey (), buf, (uint8_t *)&elGamal, m_Ctx, true))
|
||
10 years ago
|
{
|
||
8 years ago
|
auto decryption = std::make_shared<AESDecryption>(elGamal.sessionKey);
|
||
10 years ago
|
uint8_t iv[32]; // IV is first 16 bytes
|
||
9 years ago
|
SHA256(elGamal.preIV, 32, iv);
|
||
10 years ago
|
decryption->SetIV (iv);
|
||
|
decryption->Decrypt(buf + 514, length - 514, buf + 514);
|
||
|
HandleAESBlock (buf + 514, length - 514, decryption, msg->from);
|
||
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "Garlic: Failed to decrypt message");
|
||
10 years ago
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
void GarlicDestination::HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr<AESDecryption> decryption,
|
||
10 years ago
|
std::shared_ptr<i2p::tunnel::InboundTunnel> from)
|
||
10 years ago
|
{
|
||
10 years ago
|
uint16_t tagCount = bufbe16toh (buf);
|
||
10 years ago
|
buf += 2; len -= 2;
|
||
10 years ago
|
if (tagCount > 0)
|
||
|
{
|
||
10 years ago
|
if (tagCount*32 > len)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "Garlic: Tag count ", tagCount, " exceeds length ", len);
|
||
10 years ago
|
return ;
|
||
10 years ago
|
}
|
||
10 years ago
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
||
10 years ago
|
for (int i = 0; i < tagCount; i++)
|
||
10 years ago
|
m_Tags[SessionTag(buf + i*32, ts)] = decryption;
|
||
10 years ago
|
}
|
||
|
buf += tagCount*32;
|
||
10 years ago
|
len -= tagCount*32;
|
||
10 years ago
|
uint32_t payloadSize = bufbe32toh (buf);
|
||
10 years ago
|
if (payloadSize > len)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "Garlic: Unexpected payload size ", payloadSize);
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
buf += 4;
|
||
|
uint8_t * payloadHash = buf;
|
||
|
buf += 32;// payload hash.
|
||
|
if (*buf) // session key?
|
||
|
buf += 32; // new session key
|
||
|
buf++; // flag
|
||
|
|
||
|
// payload
|
||
9 years ago
|
uint8_t digest[32];
|
||
|
SHA256 (buf, payloadSize, digest);
|
||
|
if (memcmp (payloadHash, digest, 32)) // payload hash doesn't match
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogError, "Garlic: wrong payload hash");
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
HandleGarlicPayload (buf, payloadSize, from);
|
||
|
}
|
||
|
|
||
10 years ago
|
void GarlicDestination::HandleGarlicPayload (uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from)
|
||
10 years ago
|
{
|
||
10 years ago
|
const uint8_t * buf1 = buf;
|
||
10 years ago
|
int numCloves = buf[0];
|
||
9 years ago
|
LogPrint (eLogDebug, "Garlic: ", numCloves," cloves");
|
||
10 years ago
|
buf++;
|
||
|
for (int i = 0; i < numCloves; i++)
|
||
|
{
|
||
|
// delivery instructions
|
||
|
uint8_t flag = buf[0];
|
||
|
buf++; // flag
|
||
|
if (flag & 0x80) // encrypted?
|
||
|
{
|
||
|
// TODO: implement
|
||
9 years ago
|
LogPrint (eLogWarning, "Garlic: clove encrypted");
|
||
10 years ago
|
buf += 32;
|
||
|
}
|
||
|
GarlicDeliveryType deliveryType = (GarlicDeliveryType)((flag >> 5) & 0x03);
|
||
|
switch (deliveryType)
|
||
|
{
|
||
|
case eGarlicDeliveryTypeLocal:
|
||
9 years ago
|
LogPrint (eLogDebug, "Garlic: type local");
|
||
10 years ago
|
HandleI2NPMessage (buf, len, from);
|
||
10 years ago
|
break;
|
||
10 years ago
|
case eGarlicDeliveryTypeDestination:
|
||
9 years ago
|
LogPrint (eLogDebug, "Garlic: type destination");
|
||
10 years ago
|
buf += 32; // destination. check it later or for multiple destinations
|
||
10 years ago
|
HandleI2NPMessage (buf, len, from);
|
||
10 years ago
|
break;
|
||
10 years ago
|
case eGarlicDeliveryTypeTunnel:
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "Garlic: type tunnel");
|
||
10 years ago
|
// gwHash and gwTunnel sequence is reverted
|
||
|
uint8_t * gwHash = buf;
|
||
|
buf += 32;
|
||
10 years ago
|
uint32_t gwTunnel = bufbe32toh (buf);
|
||
10 years ago
|
buf += 4;
|
||
9 years ago
|
auto msg = CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from);
|
||
|
if (from) // received through an inbound tunnel
|
||
|
{
|
||
|
std::shared_ptr<i2p::tunnel::OutboundTunnel> tunnel;
|
||
|
if (from->GetTunnelPool ())
|
||
|
tunnel = from->GetTunnelPool ()->GetNextOutboundTunnel ();
|
||
|
else
|
||
|
LogPrint (eLogError, "Garlic: Tunnel pool is not set for inbound tunnel");
|
||
|
if (tunnel) // we have send it through an outbound tunnel
|
||
|
tunnel->SendTunnelDataMsg (gwHash, gwTunnel, msg);
|
||
|
else
|
||
|
LogPrint (eLogWarning, "Garlic: No outbound tunnels available for garlic clove");
|
||
|
}
|
||
|
else // received directly
|
||
|
i2p::transport::transports.SendMessage (gwHash, i2p::CreateTunnelGatewayMsg (gwTunnel, msg)); // send directly
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
case eGarlicDeliveryTypeRouter:
|
||
9 years ago
|
{
|
||
|
uint8_t * ident = buf;
|
||
10 years ago
|
buf += 32;
|
||
9 years ago
|
if (!from) // received directly
|
||
|
i2p::transport::transports.SendMessage (ident,
|
||
|
CreateI2NPMessage (buf, GetI2NPMessageLength (buf)));
|
||
|
else
|
||
|
LogPrint (eLogWarning, "Garlic: type router for inbound tunnels not supported");
|
||
|
break;
|
||
|
}
|
||
10 years ago
|
default:
|
||
9 years ago
|
LogPrint (eLogWarning, "Garlic: unknown delivery type ", (int)deliveryType);
|
||
10 years ago
|
}
|
||
|
buf += GetI2NPMessageLength (buf); // I2NP
|
||
|
buf += 4; // CloveID
|
||
|
buf += 8; // Date
|
||
|
buf += 3; // Certificate
|
||
10 years ago
|
if (buf - buf1 > (int)len)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "Garlic: clove is too long");
|
||
10 years ago
|
break;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
std::shared_ptr<I2NPMessage> GarlicDestination::WrapMessage (std::shared_ptr<const i2p::data::RoutingDestination> destination,
|
||
|
std::shared_ptr<I2NPMessage> msg, bool attachLeaseSet)
|
||
10 years ago
|
{
|
||
9 years ago
|
auto session = GetRoutingSession (destination, attachLeaseSet);
|
||
10 years ago
|
return session->WrapSingleMessage (msg);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
std::shared_ptr<GarlicRoutingSession> GarlicDestination::GetRoutingSession (
|
||
10 years ago
|
std::shared_ptr<const i2p::data::RoutingDestination> destination, bool attachLeaseSet)
|
||
11 years ago
|
{
|
||
9 years ago
|
GarlicRoutingSessionPtr session;
|
||
|
{
|
||
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
||
|
auto it = m_Sessions.find (destination->GetIdentHash ());
|
||
|
if (it != m_Sessions.end ())
|
||
|
session = it->second;
|
||
|
}
|
||
11 years ago
|
if (!session)
|
||
|
{
|
||
10 years ago
|
session = std::make_shared<GarlicRoutingSession> (this, destination,
|
||
9 years ago
|
attachLeaseSet ? m_NumTags : 4, attachLeaseSet); // specified num tags for connections and 4 for LS requests
|
||
10 years ago
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
||
10 years ago
|
m_Sessions[destination->GetIdentHash ()] = session;
|
||
11 years ago
|
}
|
||
10 years ago
|
return session;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
void GarlicDestination::CleanupExpiredTags ()
|
||
10 years ago
|
{
|
||
9 years ago
|
// incoming
|
||
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
||
|
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
|
||
8 years ago
|
++it;
|
||
9 years ago
|
}
|
||
|
if (numExpiredTags > 0)
|
||
|
LogPrint (eLogDebug, "Garlic: ", numExpiredTags, " tags expired for ", GetIdentHash().ToBase64 ());
|
||
|
|
||
|
// outgoing
|
||
10 years ago
|
{
|
||
8 years ago
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
||
|
for (auto it = m_Sessions.begin (); it != m_Sessions.end ();)
|
||
10 years ago
|
{
|
||
8 years ago
|
it->second->GetSharedRoutingPath (); // delete shared path if necessary
|
||
|
if (!it->second->CleanupExpiredTags ())
|
||
|
{
|
||
|
LogPrint (eLogInfo, "Routing session to ", it->first.ToBase32 (), " deleted");
|
||
|
it->second->SetOwner (nullptr);
|
||
|
it = m_Sessions.erase (it);
|
||
|
}
|
||
|
else
|
||
|
++it;
|
||
10 years ago
|
}
|
||
8 years ago
|
}
|
||
|
// delivery status sessions
|
||
8 years ago
|
{
|
||
|
std::unique_lock<std::mutex> l(m_DeliveryStatusSessionsMutex);
|
||
|
for (auto it = m_DeliveryStatusSessions.begin (); it != m_DeliveryStatusSessions.end (); )
|
||
|
{
|
||
|
if (it->second->GetOwner () != this)
|
||
|
it = m_DeliveryStatusSessions.erase (it);
|
||
|
else
|
||
|
++it;
|
||
|
}
|
||
8 years ago
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
void GarlicDestination::RemoveDeliveryStatusSession (uint32_t msgID)
|
||
10 years ago
|
{
|
||
8 years ago
|
std::unique_lock<std::mutex> l(m_DeliveryStatusSessionsMutex);
|
||
9 years ago
|
m_DeliveryStatusSessions.erase (msgID);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void GarlicDestination::DeliveryStatusSent (GarlicRoutingSessionPtr session, uint32_t msgID)
|
||
10 years ago
|
{
|
||
8 years ago
|
std::unique_lock<std::mutex> l(m_DeliveryStatusSessionsMutex);
|
||
9 years ago
|
m_DeliveryStatusSessions[msgID] = session;
|
||
8 years ago
|
}
|
||
11 years ago
|
|
||
10 years ago
|
void GarlicDestination::HandleDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg)
|
||
11 years ago
|
{
|
||
10 years ago
|
uint32_t msgID = bufbe32toh (msg->GetPayload ());
|
||
8 years ago
|
GarlicRoutingSessionPtr session;
|
||
11 years ago
|
{
|
||
8 years ago
|
std::unique_lock<std::mutex> l(m_DeliveryStatusSessionsMutex);
|
||
9 years ago
|
auto it = m_DeliveryStatusSessions.find (msgID);
|
||
8 years ago
|
if (it != m_DeliveryStatusSessions.end ())
|
||
10 years ago
|
{
|
||
8 years ago
|
session = it->second;
|
||
9 years ago
|
m_DeliveryStatusSessions.erase (it);
|
||
8 years ago
|
}
|
||
10 years ago
|
}
|
||
8 years ago
|
if (session)
|
||
|
{
|
||
|
session->MessageConfirmed (msgID);
|
||
|
LogPrint (eLogDebug, "Garlic: message ", msgID, " acknowledged");
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
void GarlicDestination::SetLeaseSetUpdated ()
|
||
|
{
|
||
8 years ago
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
||
|
for (auto& it: m_Sessions)
|
||
10 years ago
|
it.second->SetLeaseSetUpdated ();
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void GarlicDestination::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg)
|
||
10 years ago
|
{
|
||
|
HandleGarlicMessage (msg);
|
||
|
}
|
||
|
|
||
10 years ago
|
void GarlicDestination::ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg)
|
||
10 years ago
|
{
|
||
|
HandleDeliveryStatusMessage (msg);
|
||
|
}
|
||
|
|
||
8 years ago
|
void GarlicDestination::SaveTags ()
|
||
|
{
|
||
|
if (m_Tags.empty ()) return;
|
||
|
std::string ident = GetIdentHash().ToBase32();
|
||
|
std::string path = i2p::fs::DataDirPath("tags", (ident + ".tags"));
|
||
|
std::ofstream f (path, std::ofstream::binary | std::ofstream::out | std::ofstream::trunc);
|
||
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
||
|
// 4 bytes timestamp, 32 bytes tag, 32 bytes key
|
||
|
for (auto it: m_Tags)
|
||
|
{
|
||
|
if (ts < it.first.creationTime + INCOMING_TAGS_EXPIRATION_TIMEOUT)
|
||
|
{
|
||
|
f.write ((char *)&it.first.creationTime, 4);
|
||
|
f.write ((char *)it.first.data (), 32);
|
||
|
f.write ((char *)it.second->GetKey ().data (), 32);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void GarlicDestination::LoadTags ()
|
||
|
{
|
||
|
std::string ident = GetIdentHash().ToBase32();
|
||
|
std::string path = i2p::fs::DataDirPath("tags", (ident + ".tags"));
|
||
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
||
|
if (ts < i2p::fs::GetLastUpdateTime (path) + INCOMING_TAGS_EXPIRATION_TIMEOUT)
|
||
|
{
|
||
|
// might contain non-expired tags
|
||
|
std::ifstream f (path, std::ifstream::binary);
|
||
|
if (f)
|
||
|
{
|
||
|
std::map<i2p::crypto::AESKey, std::shared_ptr<AESDecryption> > keys;
|
||
|
// 4 bytes timestamp, 32 bytes tag, 32 bytes key
|
||
|
while (!f.eof ())
|
||
|
{
|
||
|
uint32_t t;
|
||
|
uint8_t tag[32], key[32];
|
||
|
f.read ((char *)&t, 4); if (f.eof ()) break;
|
||
|
if (ts < t + INCOMING_TAGS_EXPIRATION_TIMEOUT)
|
||
|
{
|
||
|
f.read ((char *)tag, 32);
|
||
|
f.read ((char *)key, 32);
|
||
|
}
|
||
|
else
|
||
|
f.seekg (64, std::ios::cur); // skip
|
||
|
if (f.eof ()) break;
|
||
|
|
||
|
std::shared_ptr<AESDecryption> decryption;
|
||
|
auto it = keys.find (key);
|
||
|
if (it != keys.end ())
|
||
|
decryption = it->second;
|
||
|
else
|
||
|
decryption = std::make_shared<AESDecryption>(key);
|
||
|
m_Tags.insert (std::make_pair (SessionTag (tag, ts), decryption));
|
||
|
}
|
||
|
if (!m_Tags.empty ())
|
||
|
LogPrint (eLogInfo, m_Tags.size (), " loaded for ", ident);
|
||
|
}
|
||
|
}
|
||
|
i2p::fs::Remove (path);
|
||
|
}
|
||
|
|
||
|
void CleanUpTagsFiles ()
|
||
|
{
|
||
|
std::vector<std::string> files;
|
||
|
i2p::fs::ReadDir (i2p::fs::DataDirPath("tags"), files);
|
||
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
||
|
for (auto it: files)
|
||
|
if (ts >= i2p::fs::GetLastUpdateTime (it) + INCOMING_TAGS_EXPIRATION_TIMEOUT)
|
||
|
i2p::fs::Remove (it);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|