2020-05-22 16:18:41 +03:00
|
|
|
/*
|
2024-02-16 22:49:12 +03:00
|
|
|
* Copyright (c) 2013-2024, The PurpleI2P Project
|
2020-05-22 16:18:41 +03:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
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>
|
2016-05-11 22:12:38 +03:00
|
|
|
#include "Crypto.h"
|
2013-11-24 01:35:15 +04:00
|
|
|
#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"
|
2016-06-28 16:31:41 +03:00
|
|
|
#include "Transports.h"
|
2013-11-24 01:35:15 +04:00
|
|
|
#include "Timestamp.h"
|
2015-11-03 17:15:49 +03:00
|
|
|
#include "Log.h"
|
2017-04-03 22:05:10 +03:00
|
|
|
#include "FS.h"
|
2020-01-15 23:13:43 +03:00
|
|
|
#include "ECIESX25519AEADRatchetSession.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
|
|
|
{
|
2020-01-16 22:59:19 +03:00
|
|
|
GarlicRoutingSession::GarlicRoutingSession (GarlicDestination * owner, bool attachLeaseSet):
|
|
|
|
m_Owner (owner), m_LeaseSetUpdateStatus (attachLeaseSet ? eLeaseSetUpdated : eLeaseSetDoNotSend),
|
2016-12-17 05:23:04 +03:00
|
|
|
m_LeaseSetUpdateMsgID (0)
|
2013-11-24 01:35:15 +04:00
|
|
|
{
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2013-11-25 03:10:27 +04:00
|
|
|
|
2020-01-16 22:31:01 +03:00
|
|
|
GarlicRoutingSession::GarlicRoutingSession ():
|
|
|
|
m_Owner (nullptr), m_LeaseSetUpdateStatus (eLeaseSetDoNotSend), m_LeaseSetUpdateMsgID (0)
|
2014-07-28 19:54:34 +04:00
|
|
|
{
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-07-28 19:54:34 +04:00
|
|
|
|
2013-11-25 03:10:27 +04:00
|
|
|
GarlicRoutingSession::~GarlicRoutingSession ()
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2013-11-25 03:10:27 +04:00
|
|
|
}
|
2016-02-11 06:51:08 +03:00
|
|
|
|
|
|
|
std::shared_ptr<GarlicRoutingPath> GarlicRoutingSession::GetSharedRoutingPath ()
|
|
|
|
{
|
|
|
|
if (!m_SharedRoutingPath) return nullptr;
|
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2016-02-14 07:02:58 +03:00
|
|
|
if (m_SharedRoutingPath->numTimesUsed >= ROUTING_PATH_MAX_NUM_TIMES_USED ||
|
2020-03-01 13:25:50 +03:00
|
|
|
!m_SharedRoutingPath->outboundTunnel->IsEstablished () ||
|
2016-02-11 06:51:08 +03:00
|
|
|
ts*1000LL > m_SharedRoutingPath->remoteLease->endDate ||
|
2020-03-01 13:25:50 +03:00
|
|
|
ts > m_SharedRoutingPath->updateTime + ROUTING_PATH_EXPIRATION_TIMEOUT)
|
2016-02-11 06:51:08 +03:00
|
|
|
m_SharedRoutingPath = nullptr;
|
2016-02-14 07:02:58 +03:00
|
|
|
if (m_SharedRoutingPath) m_SharedRoutingPath->numTimesUsed++;
|
2016-02-11 06:51:08 +03:00
|
|
|
return m_SharedRoutingPath;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2016-02-11 06:51:08 +03:00
|
|
|
void GarlicRoutingSession::SetSharedRoutingPath (std::shared_ptr<GarlicRoutingPath> path)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
|
|
|
if (path && path->outboundTunnel && path->remoteLease)
|
|
|
|
{
|
2016-02-11 06:51:08 +03:00
|
|
|
path->updateTime = i2p::util::GetSecondsSinceEpoch ();
|
2016-02-14 07:02:58 +03:00
|
|
|
path->numTimesUsed = 0;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2016-02-11 06:51:08 +03:00
|
|
|
else
|
|
|
|
path = nullptr;
|
|
|
|
m_SharedRoutingPath = path;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2020-03-09 01:13:41 +03:00
|
|
|
bool GarlicRoutingSession::MessageConfirmed (uint32_t msgID)
|
|
|
|
{
|
|
|
|
if (msgID == GetLeaseSetUpdateMsgID ())
|
|
|
|
{
|
|
|
|
SetLeaseSetUpdateStatus (eLeaseSetUpToDate);
|
|
|
|
SetLeaseSetUpdateMsgID (0);
|
|
|
|
LogPrint (eLogInfo, "Garlic: LeaseSet update confirmed");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
2020-03-09 01:13:41 +03:00
|
|
|
|
2020-03-14 23:35:34 +03:00
|
|
|
void GarlicRoutingSession::CleanupUnconfirmedLeaseSet (uint64_t ts)
|
|
|
|
{
|
|
|
|
if (m_LeaseSetUpdateMsgID && ts*1000LL > m_LeaseSetSubmissionTime + LEASET_CONFIRMATION_TIMEOUT)
|
|
|
|
{
|
|
|
|
if (GetOwner ())
|
|
|
|
GetOwner ()->RemoveDeliveryStatusSession (m_LeaseSetUpdateMsgID);
|
|
|
|
m_LeaseSetUpdateMsgID = 0;
|
|
|
|
}
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 01:13:41 +03:00
|
|
|
std::shared_ptr<I2NPMessage> GarlicRoutingSession::CreateEncryptedDeliveryStatusMsg (uint32_t msgID)
|
|
|
|
{
|
|
|
|
auto msg = CreateDeliveryStatusMsg (msgID);
|
|
|
|
if (GetOwner ())
|
|
|
|
{
|
|
|
|
//encrypt
|
|
|
|
uint8_t key[32], tag[32];
|
|
|
|
RAND_bytes (key, 32); // random session key
|
|
|
|
RAND_bytes (tag, 32); // random session tag
|
|
|
|
GetOwner ()->SubmitSessionKey (key, tag);
|
|
|
|
ElGamalAESSession garlic (key, tag);
|
|
|
|
msg = garlic.WrapSingleMessage (msg);
|
|
|
|
}
|
|
|
|
return msg;
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ElGamalAESSession::ElGamalAESSession (GarlicDestination * owner,
|
|
|
|
std::shared_ptr<const i2p::data::RoutingDestination> destination, int numTags, bool attachLeaseSet):
|
|
|
|
GarlicRoutingSession (owner, attachLeaseSet),
|
|
|
|
m_Destination (destination), m_NumTags (numTags)
|
2015-01-23 18:07:11 +03:00
|
|
|
{
|
2020-01-16 22:31:01 +03:00
|
|
|
// create new session tags and session key
|
|
|
|
RAND_bytes (m_SessionKey, 32);
|
|
|
|
m_Encryption.SetKey (m_SessionKey);
|
2018-01-06 07:01:44 +03:00
|
|
|
}
|
2016-07-08 21:17:41 +03:00
|
|
|
|
2020-01-17 02:03:51 +03:00
|
|
|
ElGamalAESSession::ElGamalAESSession (const uint8_t * sessionKey, const SessionTag& sessionTag):
|
|
|
|
m_NumTags(1)
|
2016-07-08 21:17:41 +03:00
|
|
|
{
|
2020-01-16 22:31:01 +03:00
|
|
|
memcpy (m_SessionKey, sessionKey, 32);
|
|
|
|
m_Encryption.SetKey (m_SessionKey);
|
|
|
|
m_SessionTags.push_back (sessionTag);
|
|
|
|
m_SessionTags.back ().creationTime = i2p::util::GetSecondsSinceEpoch ();
|
2016-07-08 21:17:41 +03:00
|
|
|
}
|
2014-10-14 22:48:25 +04:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
std::shared_ptr<I2NPMessage> ElGamalAESSession::WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg)
|
2014-01-18 19:34:57 +04:00
|
|
|
{
|
2015-11-24 21:09:12 +03:00
|
|
|
auto 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
|
2018-01-06 06:48:51 +03:00
|
|
|
bool tagFound = false;
|
|
|
|
SessionTag tag;
|
2014-03-22 16:43:38 +04:00
|
|
|
if (m_NumTags > 0)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
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;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-10-17 19:26:57 +04:00
|
|
|
else
|
|
|
|
m_SessionTags.pop_front (); // remove expired tag
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
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
|
|
|
{
|
2016-07-28 17:02:26 +03:00
|
|
|
LogPrint (eLogInfo, "Garlic: No tags available, will use ElGamal");
|
2020-01-16 22:59:19 +03:00
|
|
|
if (!m_Destination)
|
2014-07-28 19:54:34 +04:00
|
|
|
{
|
2015-12-18 09:59:43 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Can't use ElGamal for unknown destination");
|
2014-07-28 19:54:34 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2013-11-25 03:10:27 +04:00
|
|
|
// create ElGamal block
|
|
|
|
ElGamalBlock elGamal;
|
2018-01-06 06:48:51 +03:00
|
|
|
memcpy (elGamal.sessionKey, m_SessionKey, 32);
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes (elGamal.preIV, 32); // Pre-IV
|
2013-11-25 03:10:27 +04:00
|
|
|
uint8_t iv[32]; // IV is first 16 bytes
|
2018-01-06 06:48:51 +03:00
|
|
|
SHA256(elGamal.preIV, 32, iv);
|
2021-09-01 01:51:40 +03:00
|
|
|
m_Destination->Encrypt ((uint8_t *)&elGamal, buf);
|
2014-05-12 06:37:33 +04:00
|
|
|
m_Encryption.SetIV (iv);
|
2014-01-18 19:34:57 +04:00
|
|
|
buf += 514;
|
2018-01-06 06:48:51 +03:00
|
|
|
len += 514;
|
2013-11-25 03:10:27 +04:00
|
|
|
}
|
|
|
|
else // existing session
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2013-11-25 03:10:27 +04:00
|
|
|
// session tag
|
2018-01-06 06:48:51 +03:00
|
|
|
memcpy (buf, tag, 32);
|
2013-11-25 03:10:27 +04:00
|
|
|
uint8_t iv[32]; // IV is first 16 bytes
|
2015-11-03 17:15:49 +03:00
|
|
|
SHA256(tag, 32, iv);
|
2014-05-12 06:37:33 +04:00
|
|
|
m_Encryption.SetIV (iv);
|
2014-01-18 19:34:57 +04:00
|
|
|
buf += 32;
|
2018-01-06 06:48:51 +03:00
|
|
|
len += 32;
|
|
|
|
}
|
2014-01-18 19:34:57 +04:00
|
|
|
// AES block
|
2015-11-03 17:15:49 +03: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;
|
2015-07-03 18:11:07 +03:00
|
|
|
m->FillI2NPMessageHeader (eI2NPGarlic);
|
2013-11-25 03:10:27 +04:00
|
|
|
return m;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2013-11-25 03:10:27 +04:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
size_t ElGamalAESSession::CreateAESBlock (uint8_t * buf, std::shared_ptr<const I2NPMessage> msg)
|
2013-11-25 03:10:27 +04:00
|
|
|
{
|
2013-11-24 01:35:15 +04:00
|
|
|
size_t blockSize = 0;
|
2020-01-16 22:31:01 +03:00
|
|
|
bool createNewTags = GetOwner () && m_NumTags && ((int)m_SessionTags.size () <= m_NumTags*2/3);
|
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
|
2018-01-06 06:48:51 +03: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;
|
|
|
|
}
|
2018-01-06 06:48:51 +03: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);
|
2015-11-03 17:15:49 +03:00
|
|
|
SHA256(buf + blockSize, len, payloadHash);
|
2013-11-24 01:35:15 +04:00
|
|
|
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;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2013-11-24 01:35:15 +04:00
|
|
|
|
2020-01-16 22:31:01 +03:00
|
|
|
size_t ElGamalAESSession::CreateGarlicPayload (uint8_t * payload, std::shared_ptr<const I2NPMessage> msg, UnconfirmedTags * newTags)
|
2013-11-24 01:35:15 +04:00
|
|
|
{
|
2016-09-07 20:25:11 +03:00
|
|
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch ();
|
2015-11-03 17:15:49 +03:00
|
|
|
uint32_t msgID;
|
|
|
|
RAND_bytes ((uint8_t *)&msgID, 4);
|
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
|
|
|
|
2020-01-16 22:31:01 +03:00
|
|
|
if (GetOwner ())
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2015-03-22 21:59:27 +03:00
|
|
|
// resubmit non-confirmed LeaseSet
|
2020-01-16 22:31:01 +03:00
|
|
|
if (GetLeaseSetUpdateStatus () == eLeaseSetSubmitted && ts > GetLeaseSetSubmissionTime () + LEASET_CONFIRMATION_TIMEOUT)
|
2016-09-07 20:25:11 +03:00
|
|
|
{
|
2020-01-16 22:31:01 +03:00
|
|
|
SetLeaseSetUpdateStatus (eLeaseSetUpdated);
|
2016-09-07 20:25:11 +03:00
|
|
|
SetSharedRoutingPath (nullptr); // invalidate path since leaseset was not confirmed
|
|
|
|
}
|
2015-03-22 21:59:27 +03:00
|
|
|
|
|
|
|
// attach DeviveryStatus if necessary
|
2020-01-16 22:31:01 +03:00
|
|
|
if (newTags || GetLeaseSetUpdateStatus () == eLeaseSetUpdated) // new tags created or leaseset updated
|
2014-08-15 22:34:58 +04:00
|
|
|
{
|
2018-01-06 06:48:51 +03:00
|
|
|
// clove is DeliveryStatus
|
2015-06-04 18:31:22 +03:00
|
|
|
auto cloveSize = CreateDeliveryStatusClove (payload + size, msgID);
|
|
|
|
if (cloveSize > 0) // successive?
|
2014-08-15 22:34:58 +04:00
|
|
|
{
|
2015-06-04 18:31:22 +03:00
|
|
|
size += cloveSize;
|
2014-08-15 22:34:58 +04:00
|
|
|
(*numCloves)++;
|
2015-03-22 21:59:27 +03:00
|
|
|
if (newTags) // new tags created
|
2016-07-08 05:39:20 +03:00
|
|
|
{
|
|
|
|
newTags->msgID = msgID;
|
2016-11-20 16:33:33 +03:00
|
|
|
m_UnconfirmedTagsMsgs.insert (std::make_pair(msgID, std::unique_ptr<UnconfirmedTags>(newTags)));
|
2016-11-16 20:10:13 +03:00
|
|
|
newTags = nullptr; // got acquired
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2020-01-16 22:31:01 +03:00
|
|
|
GetOwner ()->DeliveryStatusSent (shared_from_this (), msgID);
|
2014-08-15 22:34:58 +04:00
|
|
|
}
|
|
|
|
else
|
2015-12-18 09:59:43 +03:00
|
|
|
LogPrint (eLogWarning, "Garlic: DeliveryStatus clove was not created");
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2015-03-22 21:59:27 +03:00
|
|
|
// attach LeaseSet
|
2020-01-16 22:31:01 +03:00
|
|
|
if (GetLeaseSetUpdateStatus () == eLeaseSetUpdated)
|
2014-08-15 22:34:58 +04:00
|
|
|
{
|
2020-01-16 22:31:01 +03:00
|
|
|
if (GetLeaseSetUpdateMsgID ()) GetOwner ()->RemoveDeliveryStatusSession (GetLeaseSetUpdateMsgID ()); // remove previous
|
|
|
|
SetLeaseSetUpdateStatus (eLeaseSetSubmitted);
|
|
|
|
SetLeaseSetUpdateMsgID (msgID);
|
|
|
|
SetLeaseSetSubmissionTime (ts);
|
2014-08-15 22:34:58 +04:00
|
|
|
// clove if our leaseSet must be attached
|
2020-01-16 22:31:01 +03:00
|
|
|
auto leaseSet = CreateDatabaseStoreMsg (GetOwner ()->GetLeaseSet ());
|
2018-01-06 06:48:51 +03:00
|
|
|
size += CreateGarlicClove (payload + size, leaseSet, false);
|
2014-08-15 22:34:58 +04:00
|
|
|
(*numCloves)++;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2021-11-12 19:33:51 +03:00
|
|
|
if (msg) // clove message itself if presented
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2020-01-16 22:59:19 +03:00
|
|
|
size += CreateGarlicClove (payload + size, msg, m_Destination ? m_Destination->IsDestination () : false);
|
2013-12-31 05:46:33 +04:00
|
|
|
(*numCloves)++;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2013-12-31 05:46:33 +04:00
|
|
|
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;
|
2016-09-07 20:25:11 +03:00
|
|
|
htobe64buf (payload + size, ts + 8000); // Expiration of message, 8 sec
|
2013-12-31 05:46:33 +04:00
|
|
|
size += 8;
|
2018-01-06 06:48:51 +03:00
|
|
|
|
|
|
|
if (newTags) delete newTags; // not acquired, delete
|
2013-12-31 05:46:33 +04:00
|
|
|
return size;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2013-12-31 05:46:33 +04:00
|
|
|
|
2020-01-16 22:31:01 +03:00
|
|
|
size_t ElGamalAESSession::CreateGarlicClove (uint8_t * buf, std::shared_ptr<const I2NPMessage> msg, bool isDestination)
|
2013-12-31 05:46:33 +04:00
|
|
|
{
|
2016-01-18 18:29:07 +03:00
|
|
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 8000; // 8 sec
|
2013-12-31 05:46:33 +04:00
|
|
|
size_t size = 0;
|
2016-12-13 20:45:18 +03:00
|
|
|
if (isDestination)
|
2013-11-27 05:59:25 +04:00
|
|
|
{
|
2022-05-20 19:56:05 +03:00
|
|
|
buf[size] = eGarlicDeliveryTypeDestination << 5;// delivery instructions flag destination
|
2013-11-27 05:59:25 +04:00
|
|
|
size++;
|
2020-01-16 22:59:19 +03:00
|
|
|
memcpy (buf + size, m_Destination->GetIdentHash (), 32);
|
2013-11-27 05:59:25 +04:00
|
|
|
size += 32;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-20 19:56:05 +03:00
|
|
|
buf[size] = 0;// delivery instructions flag local
|
2013-11-27 05:59:25 +04:00
|
|
|
size++;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
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 ();
|
2015-11-03 17:15:49 +03:00
|
|
|
uint32_t cloveID;
|
|
|
|
RAND_bytes ((uint8_t *)&cloveID, 4);
|
|
|
|
htobe32buf (buf + size, cloveID); // 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;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-01-09 07:47:22 +04:00
|
|
|
|
2020-01-16 22:31:01 +03:00
|
|
|
size_t ElGamalAESSession::CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2014-01-09 07:47:22 +04:00
|
|
|
size_t size = 0;
|
2020-01-16 22:31:01 +03:00
|
|
|
if (GetOwner ())
|
2014-08-15 22:34:58 +04:00
|
|
|
{
|
2020-01-16 22:31:01 +03:00
|
|
|
auto inboundTunnel = GetOwner ()->GetTunnelPool ()->GetNextInboundTunnel ();
|
2015-04-05 19:54:15 +03:00
|
|
|
if (inboundTunnel)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2014-08-15 22:34:58 +04:00
|
|
|
buf[size] = eGarlicDeliveryTypeTunnel << 5; // delivery instructions flag tunnel
|
|
|
|
size++;
|
2018-01-06 06:48:51 +03:00
|
|
|
// hash and tunnelID sequence is reversed for Garlic
|
2015-04-05 19:54:15 +03:00
|
|
|
memcpy (buf + size, inboundTunnel->GetNextIdentHash (), 32); // To Hash
|
2014-08-15 22:34:58 +04:00
|
|
|
size += 32;
|
2015-04-05 19:54:15 +03:00
|
|
|
htobe32buf (buf + size, inboundTunnel->GetNextTunnelID ()); // tunnelID
|
2018-01-06 06:48:51 +03:00
|
|
|
size += 4;
|
|
|
|
// create msg
|
2020-03-09 01:13:41 +03:00
|
|
|
auto msg = CreateEncryptedDeliveryStatusMsg (msgID);
|
|
|
|
if (msg)
|
2020-03-01 13:25:50 +03:00
|
|
|
{
|
2020-03-09 01:13:41 +03:00
|
|
|
memcpy (buf + size, msg->GetBuffer (), msg->GetLength ());
|
|
|
|
size += msg->GetLength ();
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
2014-08-15 22:34:58 +04:00
|
|
|
// fill clove
|
2016-01-18 18:29:07 +03:00
|
|
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 8000; // 8 sec
|
2015-11-03 17:15:49 +03:00
|
|
|
uint32_t cloveID;
|
|
|
|
RAND_bytes ((uint8_t *)&cloveID, 4);
|
|
|
|
htobe32buf (buf + size, cloveID); // 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;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
else
|
2015-12-18 09:59:43 +03:00
|
|
|
LogPrint (eLogError, "Garlic: No inbound tunnels in the pool for DeliveryStatus");
|
2014-01-09 07:47:22 +04:00
|
|
|
}
|
2014-08-15 22:34:58 +04:00
|
|
|
else
|
2015-12-18 09:59:43 +03:00
|
|
|
LogPrint (eLogWarning, "Garlic: Missing local LeaseSet");
|
2014-08-15 22:34:58 +04:00
|
|
|
|
2014-01-09 07:47:22 +04:00
|
|
|
return size;
|
|
|
|
}
|
2017-03-12 00:48:54 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
ElGamalAESSession::UnconfirmedTags * ElGamalAESSession::GenerateSessionTags ()
|
2020-01-16 22:31:01 +03:00
|
|
|
{
|
|
|
|
auto tags = new UnconfirmedTags (m_NumTags);
|
|
|
|
tags->tagsCreationTime = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
for (int i = 0; i < m_NumTags; i++)
|
|
|
|
{
|
|
|
|
RAND_bytes (tags->sessionTags[i], 32);
|
|
|
|
tags->sessionTags[i].creationTime = tags->tagsCreationTime;
|
|
|
|
}
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
|
2020-03-09 01:13:41 +03:00
|
|
|
bool ElGamalAESSession::MessageConfirmed (uint32_t msgID)
|
2020-01-16 22:31:01 +03:00
|
|
|
{
|
|
|
|
TagsConfirmed (msgID);
|
2020-03-09 01:13:41 +03:00
|
|
|
if (!GarlicRoutingSession::MessageConfirmed (msgID))
|
2020-01-16 22:31:01 +03:00
|
|
|
CleanupExpiredTags ();
|
2020-03-09 01:13:41 +03:00
|
|
|
return true;
|
2020-01-16 22:31:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ElGamalAESSession::TagsConfirmed (uint32_t msgID)
|
|
|
|
{
|
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
auto it = m_UnconfirmedTagsMsgs.find (msgID);
|
|
|
|
if (it != m_UnconfirmedTagsMsgs.end ())
|
|
|
|
{
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
m_UnconfirmedTagsMsgs.erase (it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ElGamalAESSession::CleanupExpiredTags ()
|
|
|
|
{
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
for (auto it = m_SessionTags.begin (); it != m_SessionTags.end ();)
|
|
|
|
{
|
|
|
|
if (ts >= it->creationTime + OUTGOING_TAGS_EXPIRATION_TIMEOUT)
|
|
|
|
it = m_SessionTags.erase (it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
CleanupUnconfirmedTags ();
|
2020-03-14 23:35:34 +03:00
|
|
|
CleanupUnconfirmedLeaseSet (ts);
|
2020-01-16 22:31:01 +03:00
|
|
|
return !m_SessionTags.empty () || !m_UnconfirmedTagsMsgs.empty ();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ElGamalAESSession::CleanupUnconfirmedTags ()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
// delete expired unconfirmed tags
|
|
|
|
for (auto it = m_UnconfirmedTagsMsgs.begin (); it != m_UnconfirmedTagsMsgs.end ();)
|
|
|
|
{
|
|
|
|
if (ts >= it->second->tagsCreationTime + OUTGOING_TAGS_CONFIRMATION_TIMEOUT)
|
|
|
|
{
|
|
|
|
if (GetOwner ())
|
|
|
|
GetOwner ()->RemoveDeliveryStatusSession (it->first);
|
|
|
|
it = m_UnconfirmedTagsMsgs.erase (it);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-06-13 03:42:54 +03:00
|
|
|
GarlicDestination::GarlicDestination (): m_NumTags (32), // 32 tags by default
|
2024-02-17 05:30:40 +03:00
|
|
|
m_PayloadBuffer (nullptr), m_NumRatchetInboundTags (0), // 0 means standard
|
|
|
|
m_NumUsedECIESx25519Tags (0)
|
2017-03-12 00:48:54 +03:00
|
|
|
{
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
|
2014-10-08 00:18:13 +04:00
|
|
|
GarlicDestination::~GarlicDestination ()
|
2013-11-25 03:10:27 +04:00
|
|
|
{
|
2021-07-19 01:45:08 +03:00
|
|
|
if (m_PayloadBuffer)
|
|
|
|
delete[] m_PayloadBuffer;
|
2014-10-08 00:18:13 +04:00
|
|
|
}
|
|
|
|
|
2016-11-29 06:47:37 +03:00
|
|
|
void GarlicDestination::CleanUp ()
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2021-01-05 23:56:48 +03:00
|
|
|
for (auto it: m_Sessions)
|
|
|
|
it.second->SetOwner (nullptr);
|
2016-11-29 06:47:37 +03:00
|
|
|
m_Sessions.clear ();
|
|
|
|
m_DeliveryStatusSessions.clear ();
|
|
|
|
m_Tags.clear ();
|
2021-01-05 23:56:48 +03:00
|
|
|
for (auto it: m_ECIESx25519Sessions)
|
|
|
|
{
|
|
|
|
it.second->Terminate ();
|
|
|
|
it.second->SetOwner (nullptr);
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2020-02-05 23:48:51 +03:00
|
|
|
m_ECIESx25519Sessions.clear ();
|
|
|
|
m_ECIESx25519Tags.clear ();
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
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 ();
|
2017-04-03 22:05:10 +03:00
|
|
|
m_Tags[SessionTag(tag, ts)] = std::make_shared<AESDecryption>(key);
|
2014-10-08 00:18:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 04:15:42 +03:00
|
|
|
void GarlicDestination::AddECIESx25519Key (const uint8_t * key, const uint8_t * tag)
|
|
|
|
{
|
|
|
|
uint64_t t;
|
|
|
|
memcpy (&t, tag, 8);
|
2021-07-13 02:40:40 +03:00
|
|
|
AddECIESx25519Key (key, t);
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2021-07-13 02:40:40 +03:00
|
|
|
|
|
|
|
void GarlicDestination::AddECIESx25519Key (const uint8_t * key, uint64_t tag)
|
|
|
|
{
|
2021-05-23 01:41:25 +03:00
|
|
|
auto tagset = std::make_shared<SymmetricKeyTagSet>(this, key);
|
2021-07-13 02:40:40 +03:00
|
|
|
m_ECIESx25519Tags.emplace (tag, ECIESX25519AEADRatchetIndexTagset{0, tagset});
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
|
|
|
|
2018-01-06 06:48:51 +03:00
|
|
|
bool GarlicDestination::SubmitSessionKey (const uint8_t * key, const uint8_t * tag)
|
2014-12-08 23:36:00 +03:00
|
|
|
{
|
|
|
|
AddSessionKey (key, tag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-28 19:15:40 +03:00
|
|
|
void GarlicDestination::SubmitECIESx25519Key (const uint8_t * key, uint64_t tag)
|
|
|
|
{
|
|
|
|
AddECIESx25519Key (key, tag);
|
2022-05-20 19:56:05 +03:00
|
|
|
}
|
|
|
|
|
2015-06-16 17:14:14 +03:00
|
|
|
void GarlicDestination::HandleGarlicMessage (std::shared_ptr<I2NPMessage> msg)
|
2014-10-08 00:18:13 +04:00
|
|
|
{
|
|
|
|
uint8_t * buf = msg->GetPayload ();
|
2014-12-30 01:04:02 +03:00
|
|
|
uint32_t length = bufbe32toh (buf);
|
2015-05-18 02:40:46 +03:00
|
|
|
if (length > msg->GetLength ())
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogWarning, "Garlic: Message length ", length, " exceeds I2NP message length ", msg->GetLength ());
|
2015-05-18 02:40:46 +03:00
|
|
|
return;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2020-04-14 03:00:19 +03:00
|
|
|
auto mod = length & 0x0f; // %16
|
2014-11-19 22:56:47 +03:00
|
|
|
buf += 4; // length
|
2020-12-05 03:15:06 +03:00
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
if (SupportsEncryptionType (i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD))
|
2021-11-27 23:30:35 +03:00
|
|
|
// try ECIESx25519 tag
|
2021-07-07 03:15:55 +03:00
|
|
|
found = HandleECIESx25519TagMessage (buf, length);
|
2020-12-05 03:15:06 +03:00
|
|
|
if (!found)
|
2021-11-27 23:30:35 +03:00
|
|
|
{
|
2020-12-05 03:15:06 +03:00
|
|
|
auto it = !mod ? m_Tags.find (SessionTag(buf)) : m_Tags.end (); // AES block is multiple of 16
|
|
|
|
// AES tag might be used even if encryption type is not ElGamal/AES
|
|
|
|
if (it != m_Tags.end ()) // try AES tag
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2020-12-05 03:15:06 +03:00
|
|
|
// tag found. Use AES
|
|
|
|
auto decryption = it->second;
|
|
|
|
m_Tags.erase (it); // tag might be used only once
|
|
|
|
if (length >= 32)
|
2020-04-21 02:37:46 +03:00
|
|
|
{
|
2020-12-05 03:15:06 +03:00
|
|
|
uint8_t iv[32]; // IV is first 16 bytes
|
|
|
|
SHA256(buf, 32, iv);
|
|
|
|
decryption->SetIV (iv);
|
|
|
|
decryption->Decrypt (buf + 32, length - 32, buf + 32);
|
|
|
|
HandleAESBlock (buf + 32, length - 32, decryption, msg->from);
|
2020-04-21 02:37:46 +03:00
|
|
|
found = true;
|
|
|
|
}
|
2020-12-05 03:15:06 +03:00
|
|
|
else
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogWarning, "Garlic: Message length ", length, " is less than 32 bytes");
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
|
|
|
if (!found) // assume new session
|
2020-04-21 02:37:46 +03:00
|
|
|
{
|
2020-03-01 13:25:50 +03:00
|
|
|
// AES tag not found. Handle depending on encryption type
|
|
|
|
// try ElGamal/AES first if leading block is 514
|
2020-04-21 02:37:46 +03:00
|
|
|
ElGamalBlock elGamal;
|
|
|
|
if (mod == 2 && length >= 514 && SupportsEncryptionType (i2p::data::CRYPTO_KEY_TYPE_ELGAMAL) &&
|
2021-09-01 01:51:40 +03:00
|
|
|
Decrypt (buf, (uint8_t *)&elGamal, i2p::data::CRYPTO_KEY_TYPE_ELGAMAL))
|
2020-04-21 02:37:46 +03:00
|
|
|
{
|
|
|
|
auto decryption = std::make_shared<AESDecryption>(elGamal.sessionKey);
|
|
|
|
uint8_t iv[32]; // IV is first 16 bytes
|
|
|
|
SHA256(elGamal.preIV, 32, iv);
|
|
|
|
decryption->SetIV (iv);
|
|
|
|
decryption->Decrypt(buf + 514, length - 514, buf + 514);
|
|
|
|
HandleAESBlock (buf + 514, length - 514, decryption, msg->from);
|
|
|
|
}
|
2020-11-15 02:28:50 +03:00
|
|
|
else if (SupportsEncryptionType (i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD))
|
2020-03-01 13:25:50 +03:00
|
|
|
{
|
|
|
|
// otherwise ECIESx25519
|
2020-04-21 02:37:46 +03:00
|
|
|
auto session = std::make_shared<ECIESX25519AEADRatchetSession> (this, false); // incoming
|
2020-04-25 04:36:08 +03:00
|
|
|
if (!session->HandleNextMessage (buf, length, nullptr, 0))
|
2020-12-08 23:16:40 +03:00
|
|
|
{
|
2021-11-27 23:30:35 +03:00
|
|
|
// try to generate more tags for last tagset
|
2021-05-11 03:20:25 +03:00
|
|
|
if (m_LastTagset && (m_LastTagset->GetNextIndex () - m_LastTagset->GetTrimBehind () < 3*ECIESX25519_MAX_NUM_GENERATED_TAGS))
|
2020-12-08 23:16:40 +03:00
|
|
|
{
|
2021-07-07 03:15:55 +03:00
|
|
|
uint64_t missingTag; memcpy (&missingTag, buf, 8);
|
2020-12-15 23:04:20 +03:00
|
|
|
auto maxTags = std::max (m_NumRatchetInboundTags, ECIESX25519_MAX_NUM_GENERATED_TAGS);
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogWarning, "Garlic: Trying to generate more ECIES-X25519-AEAD-Ratchet tags");
|
2020-12-15 23:04:20 +03:00
|
|
|
for (int i = 0; i < maxTags; i++)
|
2020-12-08 23:16:40 +03:00
|
|
|
{
|
2020-12-27 01:18:29 +03:00
|
|
|
auto nextTag = AddECIESx25519SessionNextTag (m_LastTagset);
|
|
|
|
if (!nextTag)
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Can't create new ECIES-X25519-AEAD-Ratchet tag for last tagset");
|
2020-12-27 01:18:29 +03:00
|
|
|
break;
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2021-07-07 03:15:55 +03:00
|
|
|
if (nextTag == missingTag)
|
2020-12-08 23:16:40 +03:00
|
|
|
{
|
2020-12-24 22:06:34 +03:00
|
|
|
LogPrint (eLogDebug, "Garlic: Missing ECIES-X25519-AEAD-Ratchet tag was generated");
|
2021-07-07 03:15:55 +03:00
|
|
|
if (m_LastTagset->HandleNextMessage (buf, length, m_ECIESx25519Tags[nextTag].index))
|
2020-12-08 23:16:40 +03:00
|
|
|
found = true;
|
|
|
|
break;
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2020-12-08 23:16:40 +03:00
|
|
|
}
|
|
|
|
if (!found) m_LastTagset = nullptr;
|
|
|
|
}
|
|
|
|
if (!found)
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Can't handle ECIES-X25519-AEAD-Ratchet message");
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
2020-04-21 02:37:46 +03:00
|
|
|
else
|
2020-03-01 13:25:50 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Failed to decrypt message");
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-01-18 19:34:57 +04:00
|
|
|
|
2021-07-07 03:15:55 +03:00
|
|
|
bool GarlicDestination::HandleECIESx25519TagMessage (uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
uint64_t tag;
|
|
|
|
memcpy (&tag, buf, 8);
|
|
|
|
auto it = m_ECIESx25519Tags.find (tag);
|
|
|
|
if (it != m_ECIESx25519Tags.end ())
|
|
|
|
{
|
2024-02-16 22:49:12 +03:00
|
|
|
if (!it->second.tagset) return true; // duplicate
|
|
|
|
if (it->second.tagset->HandleNextMessage (buf, len, it->second.index))
|
|
|
|
{
|
2021-07-07 03:15:55 +03:00
|
|
|
m_LastTagset = it->second.tagset;
|
2024-02-16 22:49:12 +03:00
|
|
|
it->second.tagset = nullptr; // mark as used
|
|
|
|
}
|
2021-11-27 23:30:35 +03:00
|
|
|
else
|
2024-02-16 22:49:12 +03:00
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Can't handle ECIES-X25519-AEAD-Ratchet message");
|
2024-02-16 22:49:12 +03:00
|
|
|
m_ECIESx25519Tags.erase (it);
|
|
|
|
}
|
2024-02-17 05:30:40 +03:00
|
|
|
m_NumUsedECIESx25519Tags++;
|
2021-07-07 03:15:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
|
|
|
|
2017-04-03 22:05:10 +03:00
|
|
|
void GarlicDestination::HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr<AESDecryption> decryption,
|
2015-02-06 02:53:43 +03:00
|
|
|
std::shared_ptr<i2p::tunnel::InboundTunnel> from)
|
2014-10-08 00:18:13 +04:00
|
|
|
{
|
2014-12-30 01:04:02 +03:00
|
|
|
uint16_t tagCount = bufbe16toh (buf);
|
2018-01-06 06:48:51 +03:00
|
|
|
buf += 2; len -= 2;
|
2014-10-08 00:18:13 +04:00
|
|
|
if (tagCount > 0)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
|
|
|
if (tagCount*32 > len)
|
2014-12-08 05:00:19 +03:00
|
|
|
{
|
2015-12-18 09:59:43 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Tag count ", tagCount, " exceeds length ", len);
|
2014-12-08 23:36:00 +03:00
|
|
|
return ;
|
2018-01-06 06:48:51 +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++)
|
2018-01-06 06:48:51 +03: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)
|
|
|
|
{
|
2015-12-18 09:59:43 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Unexpected payload size ", payloadSize);
|
2014-10-08 00:18:13 +04:00
|
|
|
return;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-10-08 00:18:13 +04:00
|
|
|
buf += 4;
|
|
|
|
uint8_t * payloadHash = buf;
|
2018-01-06 06:48:51 +03:00
|
|
|
buf += 32;// payload hash.
|
2014-10-08 00:18:13 +04:00
|
|
|
if (*buf) // session key?
|
|
|
|
buf += 32; // new session key
|
|
|
|
buf++; // flag
|
|
|
|
|
|
|
|
// payload
|
2015-11-03 17:15:49 +03:00
|
|
|
uint8_t digest[32];
|
|
|
|
SHA256 (buf, payloadSize, digest);
|
|
|
|
if (memcmp (payloadHash, digest, 32)) // payload hash doesn't match
|
2014-10-08 00:18:13 +04:00
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Wrong payload hash");
|
2014-10-08 00:18:13 +04:00
|
|
|
return;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-10-08 00:18:13 +04:00
|
|
|
HandleGarlicPayload (buf, payloadSize, from);
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-10-08 00:18:13 +04:00
|
|
|
|
2015-02-06 02:53:43 +03:00
|
|
|
void GarlicDestination::HandleGarlicPayload (uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from)
|
2014-10-08 00:18:13 +04:00
|
|
|
{
|
2017-12-01 20:57:05 +03:00
|
|
|
if (len < 1)
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Payload is too short");
|
2017-12-01 20:57:05 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-10-08 00:18:13 +04:00
|
|
|
int numCloves = buf[0];
|
2015-12-18 09:59:43 +03:00
|
|
|
LogPrint (eLogDebug, "Garlic: ", numCloves," cloves");
|
2017-12-01 20:57:05 +03:00
|
|
|
buf++; len--;
|
2014-10-08 00:18:13 +04:00
|
|
|
for (int i = 0; i < numCloves; i++)
|
|
|
|
{
|
2017-12-01 20:57:05 +03:00
|
|
|
const uint8_t * buf1 = buf;
|
2014-10-08 00:18:13 +04:00
|
|
|
// delivery instructions
|
|
|
|
uint8_t flag = buf[0];
|
|
|
|
buf++; // flag
|
|
|
|
if (flag & 0x80) // encrypted?
|
|
|
|
{
|
|
|
|
// TODO: implement
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogWarning, "Garlic: Clove encrypted");
|
2018-01-06 06:48:51 +03:00
|
|
|
buf += 32;
|
|
|
|
}
|
2017-12-01 20:57:05 +03:00
|
|
|
ptrdiff_t offset = buf - buf1;
|
2014-10-08 00:18:13 +04:00
|
|
|
GarlicDeliveryType deliveryType = (GarlicDeliveryType)((flag >> 5) & 0x03);
|
|
|
|
switch (deliveryType)
|
|
|
|
{
|
|
|
|
case eGarlicDeliveryTypeLocal:
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogDebug, "Garlic: Type local");
|
2017-12-01 20:57:05 +03:00
|
|
|
if (offset > (int)len)
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Message is too short");
|
2017-12-01 20:57:05 +03:00
|
|
|
break;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2020-01-07 23:20:55 +03:00
|
|
|
HandleI2NPMessage (buf, len - offset);
|
2018-01-06 06:48:51 +03:00
|
|
|
break;
|
|
|
|
case eGarlicDeliveryTypeDestination:
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogDebug, "Garlic: Type destination");
|
2014-10-08 00:18:13 +04:00
|
|
|
buf += 32; // destination. check it later or for multiple destinations
|
2018-01-03 20:52:29 +03:00
|
|
|
offset = buf - buf1;
|
2017-12-01 20:57:05 +03:00
|
|
|
if (offset > (int)len)
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Message is too short");
|
2017-12-01 20:57:05 +03:00
|
|
|
break;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2020-01-07 23:20:55 +03:00
|
|
|
HandleI2NPMessage (buf, len - offset);
|
2014-10-12 05:27:55 +04:00
|
|
|
break;
|
2014-10-08 00:18:13 +04:00
|
|
|
case eGarlicDeliveryTypeTunnel:
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogDebug, "Garlic: Type tunnel");
|
2014-10-08 00:18:13 +04:00
|
|
|
// gwHash and gwTunnel sequence is reverted
|
|
|
|
uint8_t * gwHash = buf;
|
|
|
|
buf += 32;
|
2018-01-03 20:52:29 +03:00
|
|
|
offset = buf - buf1;
|
2017-12-01 20:57:05 +03:00
|
|
|
if (offset + 4 > (int)len)
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Message is too short");
|
2017-12-01 20:57:05 +03:00
|
|
|
break;
|
|
|
|
}
|
2014-12-30 01:04:02 +03:00
|
|
|
uint32_t gwTunnel = bufbe32toh (buf);
|
2017-12-01 20:57:05 +03:00
|
|
|
buf += 4; offset += 4;
|
|
|
|
auto msg = CreateI2NPMessage (buf, GetI2NPMessageLength (buf, len - offset), from);
|
2016-06-28 16:31:41 +03:00
|
|
|
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");
|
2019-04-08 22:22:42 +03:00
|
|
|
if (tunnel) // we have sent it through an outbound tunnel
|
2023-04-04 20:48:00 +03:00
|
|
|
tunnel->SendTunnelDataMsgTo (gwHash, gwTunnel, msg);
|
2016-06-28 16:31:41 +03:00
|
|
|
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
|
2014-10-08 00:18:13 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eGarlicDeliveryTypeRouter:
|
2016-06-28 16:31:41 +03:00
|
|
|
{
|
|
|
|
uint8_t * ident = buf;
|
2014-10-08 00:18:13 +04:00
|
|
|
buf += 32;
|
2018-01-06 06:48:51 +03:00
|
|
|
offset = buf - buf1;
|
2016-06-28 16:31:41 +03:00
|
|
|
if (!from) // received directly
|
2017-12-01 20:57:05 +03:00
|
|
|
{
|
|
|
|
if (offset > (int)len)
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Message is too short");
|
2017-12-01 20:57:05 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-06-28 16:31:41 +03:00
|
|
|
i2p::transport::transports.SendMessage (ident,
|
2017-12-01 20:57:05 +03:00
|
|
|
CreateI2NPMessage (buf, GetI2NPMessageLength (buf, len - offset)));
|
|
|
|
}
|
2016-06-28 16:31:41 +03:00
|
|
|
else
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogWarning, "Garlic: Type router for inbound tunnels not supported");
|
2018-01-06 06:48:51 +03:00
|
|
|
break;
|
2016-06-28 16:31:41 +03:00
|
|
|
}
|
2014-10-08 00:18:13 +04:00
|
|
|
default:
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogWarning, "Garlic: Unknown delivery type ", (int)deliveryType);
|
2014-10-08 00:18:13 +04:00
|
|
|
}
|
2017-12-01 20:57:05 +03:00
|
|
|
if (offset > (int)len)
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Message is too short");
|
2017-12-01 20:57:05 +03:00
|
|
|
break;
|
|
|
|
}
|
2022-05-20 19:56:05 +03:00
|
|
|
buf += GetI2NPMessageLength (buf, len - offset); // I2NP
|
2014-10-08 00:18:13 +04:00
|
|
|
buf += 4; // CloveID
|
|
|
|
buf += 8; // Date
|
|
|
|
buf += 3; // Certificate
|
2018-01-03 20:52:29 +03:00
|
|
|
offset = buf - buf1;
|
2017-12-01 20:57:05 +03:00
|
|
|
if (offset > (int)len)
|
2015-04-22 01:59:35 +03:00
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Clove is too long");
|
2015-04-22 01:59:35 +03:00
|
|
|
break;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2017-12-01 20:57:05 +03:00
|
|
|
len -= offset;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 05:21:46 +03:00
|
|
|
std::shared_ptr<I2NPMessage> GarlicDestination::WrapMessageForRouter (std::shared_ptr<const i2p::data::RouterInfo> router,
|
|
|
|
std::shared_ptr<I2NPMessage> msg)
|
2014-10-08 00:18:13 +04:00
|
|
|
{
|
2020-11-15 02:28:50 +03:00
|
|
|
if (router->GetEncryptionType () == i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD)
|
2021-07-10 23:47:28 +03:00
|
|
|
return WrapECIESX25519MessageForRouter (msg, router->GetIdentity ()->GetEncryptionPublicKey ());
|
2020-11-06 05:21:46 +03:00
|
|
|
else
|
2021-11-27 23:30:35 +03:00
|
|
|
{
|
2020-11-06 05:21:46 +03:00
|
|
|
auto session = GetRoutingSession (router, false);
|
|
|
|
return session->WrapSingleMessage (msg);
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2014-10-08 00:18:13 +04:00
|
|
|
}
|
|
|
|
|
2015-01-22 23:31:34 +03:00
|
|
|
std::shared_ptr<GarlicRoutingSession> GarlicDestination::GetRoutingSession (
|
2015-04-06 03:07:32 +03:00
|
|
|
std::shared_ptr<const i2p::data::RoutingDestination> destination, bool attachLeaseSet)
|
2013-11-25 03:10:27 +04:00
|
|
|
{
|
2020-11-15 02:28:50 +03:00
|
|
|
if (destination->GetEncryptionType () == i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD &&
|
|
|
|
SupportsEncryptionType (i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD))
|
2020-03-01 13:25:50 +03:00
|
|
|
{
|
|
|
|
ECIESX25519AEADRatchetSessionPtr session;
|
|
|
|
uint8_t staticKey[32];
|
2021-09-01 01:51:40 +03:00
|
|
|
destination->Encrypt (nullptr, staticKey); // we are supposed to get static key
|
2020-03-01 13:25:50 +03:00
|
|
|
auto it = m_ECIESx25519Sessions.find (staticKey);
|
2020-01-16 23:45:22 +03:00
|
|
|
if (it != m_ECIESx25519Sessions.end ())
|
2021-11-27 23:30:35 +03:00
|
|
|
{
|
2020-03-01 13:25:50 +03:00
|
|
|
session = it->second;
|
2020-05-23 22:58:11 +03:00
|
|
|
if (session->IsInactive (i2p::util::GetSecondsSinceEpoch ()))
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogDebug, "Garlic: Session restarted");
|
2020-05-23 22:58:11 +03:00
|
|
|
session = nullptr;
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
|
|
|
}
|
2020-03-01 13:25:50 +03:00
|
|
|
if (!session)
|
2020-01-17 03:33:00 +03:00
|
|
|
{
|
2020-04-19 22:00:51 +03:00
|
|
|
session = std::make_shared<ECIESX25519AEADRatchetSession> (this, true);
|
2020-01-17 03:33:00 +03:00
|
|
|
session->SetRemoteStaticKey (staticKey);
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
2020-10-22 01:59:16 +03:00
|
|
|
if (destination->IsDestination ())
|
|
|
|
session->SetDestination (destination->GetIdentHash ()); // TODO: remove
|
2020-03-01 13:25:50 +03:00
|
|
|
return session;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ElGamalAESSessionPtr session;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
|
|
|
auto it = m_Sessions.find (destination->GetIdentHash ());
|
|
|
|
if (it != m_Sessions.end ())
|
|
|
|
session = it->second;
|
|
|
|
}
|
|
|
|
if (!session)
|
|
|
|
{
|
|
|
|
session = std::make_shared<ElGamalAESSession> (this, destination,
|
|
|
|
attachLeaseSet ? m_NumTags : 4, attachLeaseSet); // specified num tags for connections and 4 for LS requests
|
|
|
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
|
|
|
m_Sessions[destination->GetIdentHash ()] = session;
|
|
|
|
}
|
|
|
|
return session;
|
|
|
|
}
|
2020-11-15 02:28:50 +03:00
|
|
|
return nullptr;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
|
2016-02-08 01:45:11 +03:00
|
|
|
void GarlicDestination::CleanupExpiredTags ()
|
2015-01-23 18:07:11 +03:00
|
|
|
{
|
2016-02-08 01:45:11 +03:00
|
|
|
// 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);
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2016-02-08 01:45:11 +03:00
|
|
|
else
|
2016-08-05 21:23:54 +03:00
|
|
|
++it;
|
2016-02-08 01:45:11 +03:00
|
|
|
}
|
|
|
|
if (numExpiredTags > 0)
|
|
|
|
LogPrint (eLogDebug, "Garlic: ", numExpiredTags, " tags expired for ", GetIdentHash().ToBase64 ());
|
|
|
|
|
|
|
|
// outgoing
|
2015-01-23 18:07:11 +03:00
|
|
|
{
|
2016-11-16 22:43:29 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
|
|
|
for (auto it = m_Sessions.begin (); it != m_Sessions.end ();)
|
2015-01-23 20:48:25 +03:00
|
|
|
{
|
2016-11-16 22:43:29 +03:00
|
|
|
it->second->GetSharedRoutingPath (); // delete shared path if necessary
|
|
|
|
if (!it->second->CleanupExpiredTags ())
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogInfo, "Garlic: Routing session to ", it->first.ToBase32 (), " deleted");
|
2016-11-16 22:43:29 +03:00
|
|
|
it->second->SetOwner (nullptr);
|
|
|
|
it = m_Sessions.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++it;
|
2015-01-23 20:48:25 +03:00
|
|
|
}
|
2016-11-16 22:43:29 +03:00
|
|
|
}
|
|
|
|
// delivery status sessions
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2016-11-18 19:16:55 +03:00
|
|
|
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;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2020-02-20 23:44:09 +03:00
|
|
|
// ECIESx25519
|
|
|
|
for (auto it = m_ECIESx25519Sessions.begin (); it != m_ECIESx25519Sessions.end ();)
|
|
|
|
{
|
2020-03-14 23:35:34 +03:00
|
|
|
if (it->second->CheckExpired (ts))
|
2020-02-20 23:44:09 +03:00
|
|
|
{
|
2021-01-05 23:56:48 +03:00
|
|
|
it->second->Terminate ();
|
2020-02-20 23:44:09 +03:00
|
|
|
it = m_ECIESx25519Sessions.erase (it);
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
2020-02-20 23:44:09 +03:00
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
2020-09-08 01:45:05 +03:00
|
|
|
|
|
|
|
numExpiredTags = 0;
|
2024-02-17 05:30:40 +03:00
|
|
|
if (m_NumUsedECIESx25519Tags > ECIESX25519_TAGSET_MAX_NUM_TAGS) // too many used tags
|
2020-09-08 01:45:05 +03:00
|
|
|
{
|
2024-02-17 05:30:40 +03:00
|
|
|
std::unordered_map<uint64_t, ECIESX25519AEADRatchetIndexTagset> oldTags;
|
|
|
|
std::swap (m_ECIESx25519Tags, oldTags); // re-create
|
|
|
|
for (auto& it: oldTags)
|
|
|
|
if (it.second.tagset)
|
|
|
|
{
|
|
|
|
if (it.second.tagset->IsExpired (ts) || it.second.tagset->IsIndexExpired (it.second.index))
|
|
|
|
{
|
|
|
|
it.second.tagset->DeleteSymmKey (it.second.index);
|
|
|
|
numExpiredTags++;
|
|
|
|
}
|
|
|
|
else if (it.second.tagset->IsSessionTerminated())
|
|
|
|
numExpiredTags++;
|
|
|
|
else
|
|
|
|
m_ECIESx25519Tags.emplace (it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto it = m_ECIESx25519Tags.begin (); it != m_ECIESx25519Tags.end ();)
|
2021-01-07 22:51:23 +03:00
|
|
|
{
|
2024-02-17 05:30:40 +03:00
|
|
|
if (!it->second.tagset)
|
|
|
|
{
|
|
|
|
// delete used tag
|
|
|
|
it = m_ECIESx25519Tags.erase (it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (it->second.tagset->IsExpired (ts) || it->second.tagset->IsIndexExpired (it->second.index))
|
|
|
|
{
|
|
|
|
it->second.tagset->DeleteSymmKey (it->second.index);
|
|
|
|
it = m_ECIESx25519Tags.erase (it);
|
|
|
|
numExpiredTags++;
|
|
|
|
}
|
|
|
|
else if (it->second.tagset->IsSessionTerminated())
|
2021-11-27 23:30:35 +03:00
|
|
|
{
|
2021-05-12 01:49:17 +03:00
|
|
|
it = m_ECIESx25519Tags.erase (it);
|
|
|
|
numExpiredTags++;
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2021-05-12 01:49:17 +03:00
|
|
|
else
|
|
|
|
++it;
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2024-02-17 05:30:40 +03:00
|
|
|
}
|
|
|
|
m_NumUsedECIESx25519Tags = 0;
|
2020-09-08 01:45:05 +03:00
|
|
|
if (numExpiredTags > 0)
|
|
|
|
LogPrint (eLogDebug, "Garlic: ", numExpiredTags, " ECIESx25519 tags expired for ", GetIdentHash().ToBase64 ());
|
2020-12-08 23:16:40 +03:00
|
|
|
if (m_LastTagset && m_LastTagset->IsExpired (ts))
|
|
|
|
m_LastTagset = nullptr;
|
2015-01-23 18:07:11 +03:00
|
|
|
}
|
2016-07-08 21:17:41 +03:00
|
|
|
|
2016-01-25 21:34:04 +03:00
|
|
|
void GarlicDestination::RemoveDeliveryStatusSession (uint32_t msgID)
|
2015-01-23 18:07:11 +03:00
|
|
|
{
|
2016-11-18 19:16:55 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_DeliveryStatusSessionsMutex);
|
2016-01-25 21:34:04 +03:00
|
|
|
m_DeliveryStatusSessions.erase (msgID);
|
2015-01-23 18:07:11 +03:00
|
|
|
}
|
|
|
|
|
2020-03-14 16:33:48 +03:00
|
|
|
void GarlicDestination::DeliveryStatusSent (GarlicRoutingSessionPtr session, uint32_t msgID)
|
2014-08-30 06:10:00 +04:00
|
|
|
{
|
2016-11-18 19:16:55 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_DeliveryStatusSessionsMutex);
|
2016-01-25 21:34:04 +03:00
|
|
|
m_DeliveryStatusSessions[msgID] = session;
|
2016-08-05 21:23:54 +03:00
|
|
|
}
|
2014-01-17 17:12:57 +04:00
|
|
|
|
2020-01-06 22:37:40 +03:00
|
|
|
void GarlicDestination::HandleDeliveryStatusMessage (uint32_t msgID)
|
2014-01-17 17:12:57 +04:00
|
|
|
{
|
2020-03-09 01:13:41 +03:00
|
|
|
GarlicRoutingSessionPtr session;
|
2014-01-17 17:12:57 +04:00
|
|
|
{
|
2016-11-18 19:16:55 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_DeliveryStatusSessionsMutex);
|
2016-01-25 21:34:04 +03:00
|
|
|
auto it = m_DeliveryStatusSessions.find (msgID);
|
2016-08-05 21:23:54 +03:00
|
|
|
if (it != m_DeliveryStatusSessions.end ())
|
2014-08-31 16:56:03 +04:00
|
|
|
{
|
2016-11-18 19:16:55 +03:00
|
|
|
session = it->second;
|
2016-01-25 21:34:04 +03:00
|
|
|
m_DeliveryStatusSessions.erase (it);
|
2016-08-05 21:23:54 +03:00
|
|
|
}
|
2014-10-08 05:47:32 +04:00
|
|
|
}
|
2016-11-18 19:16:55 +03:00
|
|
|
if (session)
|
|
|
|
{
|
|
|
|
session->MessageConfirmed (msgID);
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogDebug, "Garlic: Message ", msgID, " acknowledged");
|
2016-11-18 19:16:55 +03:00
|
|
|
}
|
2014-10-08 05:47:32 +04:00
|
|
|
}
|
2014-10-08 20:11:39 +04:00
|
|
|
|
|
|
|
void GarlicDestination::SetLeaseSetUpdated ()
|
|
|
|
{
|
2020-03-09 03:58:59 +03:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
|
|
|
for (auto& it: m_Sessions)
|
|
|
|
it.second->SetLeaseSetUpdated ();
|
|
|
|
}
|
|
|
|
for (auto& it: m_ECIESx25519Sessions)
|
2014-10-08 20:11:39 +04:00
|
|
|
it.second->SetLeaseSetUpdated ();
|
|
|
|
}
|
2014-10-08 22:17:17 +04:00
|
|
|
|
2015-06-16 17:14:14 +03:00
|
|
|
void GarlicDestination::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg)
|
2014-10-08 22:17:17 +04:00
|
|
|
{
|
|
|
|
HandleGarlicMessage (msg);
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:14:14 +03:00
|
|
|
void GarlicDestination::ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg)
|
2014-10-08 22:17:17 +04:00
|
|
|
{
|
2020-01-06 22:37:40 +03:00
|
|
|
uint32_t msgID = bufbe32toh (msg->GetPayload () + DELIVERY_STATUS_MSGID_OFFSET);
|
|
|
|
HandleDeliveryStatusMessage (msgID);
|
2014-10-08 22:17:17 +04:00
|
|
|
}
|
|
|
|
|
2017-04-03 22:05:10 +03:00
|
|
|
void GarlicDestination::SaveTags ()
|
|
|
|
{
|
|
|
|
if (m_Tags.empty ()) return;
|
|
|
|
std::string ident = GetIdentHash().ToBase32();
|
|
|
|
std::string path = i2p::fs::DataDirPath("tags", (ident + ".tags"));
|
2018-01-06 06:48:51 +03:00
|
|
|
std::ofstream f (path, std::ofstream::binary | std::ofstream::out | std::ofstream::trunc);
|
2017-04-03 22:05:10 +03:00
|
|
|
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);
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2017-04-03 22:05:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GarlicDestination::LoadTags ()
|
|
|
|
{
|
|
|
|
std::string ident = GetIdentHash().ToBase32();
|
|
|
|
std::string path = i2p::fs::DataDirPath("tags", (ident + ".tags"));
|
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2018-01-06 06:48:51 +03:00
|
|
|
if (ts < i2p::fs::GetLastUpdateTime (path) + INCOMING_TAGS_EXPIRATION_TIMEOUT)
|
2017-04-03 22:05:10 +03:00
|
|
|
{
|
|
|
|
// might contain non-expired tags
|
|
|
|
std::ifstream f (path, std::ifstream::binary);
|
2018-01-06 06:48:51 +03:00
|
|
|
if (f)
|
2017-04-03 22:05:10 +03:00
|
|
|
{
|
|
|
|
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
|
2018-01-06 06:48:51 +03:00
|
|
|
f.seekg (64, std::ios::cur); // skip
|
2017-04-03 22:05:10 +03:00
|
|
|
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));
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
if (!m_Tags.empty ())
|
2020-03-20 18:43:54 +03:00
|
|
|
LogPrint (eLogInfo, "Garlic: ", m_Tags.size (), " tags loaded for ", ident);
|
2017-04-03 22:05:10 +03:00
|
|
|
}
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
i2p::fs::Remove (path);
|
2017-04-03 22:05:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CleanUpTagsFiles ()
|
|
|
|
{
|
2018-01-06 06:48:51 +03:00
|
|
|
std::vector<std::string> files;
|
2017-04-03 22:05:10 +03:00
|
|
|
i2p::fs::ReadDir (i2p::fs::DataDirPath("tags"), files);
|
|
|
|
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2020-03-01 13:25:50 +03:00
|
|
|
for (auto it: files)
|
2017-04-03 22:05:10 +03:00
|
|
|
if (ts >= i2p::fs::GetLastUpdateTime (it) + INCOMING_TAGS_EXPIRATION_TIMEOUT)
|
2022-05-20 19:56:05 +03:00
|
|
|
i2p::fs::Remove (it);
|
2017-04-03 22:05:10 +03:00
|
|
|
}
|
2019-12-18 00:18:40 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
void GarlicDestination::HandleECIESx25519GarlicClove (const uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
const uint8_t * buf1 = buf;
|
2020-01-16 20:47:08 +03:00
|
|
|
uint8_t flag = buf[0]; buf++; // flag
|
|
|
|
GarlicDeliveryType deliveryType = (GarlicDeliveryType)((flag >> 5) & 0x03);
|
|
|
|
switch (deliveryType)
|
|
|
|
{
|
|
|
|
case eGarlicDeliveryTypeDestination:
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogDebug, "Garlic: Type destination");
|
2020-01-16 20:47:08 +03:00
|
|
|
buf += 32; // TODO: check destination
|
2020-03-01 13:25:50 +03:00
|
|
|
#if (__cplusplus >= 201703L) // C++ 17 or higher
|
|
|
|
[[fallthrough]];
|
|
|
|
#endif
|
2020-02-29 17:21:50 +03:00
|
|
|
// no break here
|
2020-01-16 20:47:08 +03:00
|
|
|
case eGarlicDeliveryTypeLocal:
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogDebug, "Garlic: Type local");
|
2020-01-16 20:47:08 +03:00
|
|
|
I2NPMessageType typeID = (I2NPMessageType)(buf[0]); buf++; // typeid
|
2021-07-21 05:00:06 +03:00
|
|
|
int32_t msgID = bufbe32toh (buf); buf += 4; // msgID
|
|
|
|
buf += 4; // expiration
|
2020-01-16 20:47:08 +03:00
|
|
|
ptrdiff_t offset = buf - buf1;
|
|
|
|
if (offset <= (int)len)
|
2021-07-21 05:00:06 +03:00
|
|
|
HandleCloveI2NPMessage (typeID, buf, len - offset, msgID);
|
2020-01-16 20:47:08 +03:00
|
|
|
else
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Clove is too long");
|
2020-01-16 20:47:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eGarlicDeliveryTypeTunnel:
|
2020-03-01 13:25:50 +03:00
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogDebug, "Garlic: Type tunnel");
|
2020-01-16 20:47:08 +03:00
|
|
|
// gwHash and gwTunnel sequence is reverted
|
|
|
|
const uint8_t * gwHash = buf;
|
|
|
|
buf += 32;
|
|
|
|
ptrdiff_t offset = buf - buf1;
|
|
|
|
if (offset + 13 > (int)len)
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "Garlic: Message is too short");
|
2020-01-16 20:47:08 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-03-01 13:25:50 +03:00
|
|
|
uint32_t gwTunnel = bufbe32toh (buf); buf += 4;
|
|
|
|
I2NPMessageType typeID = (I2NPMessageType)(buf[0]); buf++; // typeid
|
2021-07-21 05:00:06 +03:00
|
|
|
uint32_t msgID = bufbe32toh (buf); buf += 4; // msgID
|
|
|
|
buf += 4; // expiration
|
2020-03-01 13:25:50 +03:00
|
|
|
offset += 13;
|
2020-01-16 20:47:08 +03:00
|
|
|
if (GetTunnelPool ())
|
2020-03-01 13:25:50 +03:00
|
|
|
{
|
|
|
|
auto tunnel = GetTunnelPool ()->GetNextOutboundTunnel ();
|
|
|
|
if (tunnel)
|
2023-04-04 20:48:00 +03:00
|
|
|
tunnel->SendTunnelDataMsgTo (gwHash, gwTunnel, CreateI2NPMessage (typeID, buf, len - offset, msgID));
|
2020-01-16 20:47:08 +03:00
|
|
|
else
|
2020-03-01 13:25:50 +03:00
|
|
|
LogPrint (eLogWarning, "Garlic: No outbound tunnels available for garlic clove");
|
|
|
|
}
|
2020-01-16 20:47:08 +03:00
|
|
|
else
|
|
|
|
LogPrint (eLogError, "Garlic: Tunnel pool is not set for inbound tunnel");
|
2020-03-01 13:25:50 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-01-16 20:47:08 +03:00
|
|
|
default:
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogWarning, "Garlic: Unexpected delivery type ", (int)deliveryType);
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
|
|
|
}
|
2020-01-21 22:40:23 +03:00
|
|
|
|
2021-01-05 02:20:16 +03:00
|
|
|
uint64_t GarlicDestination::AddECIESx25519SessionNextTag (ReceiveRatchetTagSetPtr tagset)
|
2020-03-01 13:25:50 +03:00
|
|
|
{
|
2020-04-24 22:46:02 +03:00
|
|
|
auto index = tagset->GetNextIndex ();
|
|
|
|
uint64_t tag = tagset->GetNextSessionTag ();
|
2020-12-27 01:18:29 +03:00
|
|
|
if (tag)
|
|
|
|
m_ECIESx25519Tags.emplace (tag, ECIESX25519AEADRatchetIndexTagset{index, tagset});
|
2020-12-08 23:16:40 +03:00
|
|
|
return tag;
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
2020-01-29 20:54:26 +03:00
|
|
|
|
|
|
|
void GarlicDestination::AddECIESx25519Session (const uint8_t * staticKey, ECIESX25519AEADRatchetSessionPtr session)
|
|
|
|
{
|
2020-02-20 23:44:09 +03:00
|
|
|
i2p::data::Tag<32> staticKeyTag (staticKey);
|
|
|
|
auto it = m_ECIESx25519Sessions.find (staticKeyTag);
|
|
|
|
if (it != m_ECIESx25519Sessions.end ())
|
2020-03-01 13:25:50 +03:00
|
|
|
{
|
2020-02-20 23:44:09 +03:00
|
|
|
if (it->second->CanBeRestarted (i2p::util::GetSecondsSinceEpoch ()))
|
2021-11-27 23:30:35 +03:00
|
|
|
{
|
2021-01-05 23:56:48 +03:00
|
|
|
it->second->Terminate (); // detach
|
2020-02-20 23:44:09 +03:00
|
|
|
m_ECIESx25519Sessions.erase (it);
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2020-02-20 23:44:09 +03:00
|
|
|
else
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogInfo, "Garlic: ECIESx25519 session with static key ", staticKeyTag.ToBase64 (), " already exists");
|
2020-02-20 23:44:09 +03:00
|
|
|
return;
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
2020-02-20 23:44:09 +03:00
|
|
|
}
|
|
|
|
m_ECIESx25519Sessions.emplace (staticKeyTag, session);
|
2020-01-29 20:54:26 +03:00
|
|
|
}
|
|
|
|
|
2020-05-05 20:01:23 +03:00
|
|
|
void GarlicDestination::RemoveECIESx25519Session (const uint8_t * staticKey)
|
|
|
|
{
|
|
|
|
auto it = m_ECIESx25519Sessions.find (staticKey);
|
|
|
|
if (it != m_ECIESx25519Sessions.end ())
|
|
|
|
{
|
2021-01-07 22:51:23 +03:00
|
|
|
it->second->Terminate ();
|
2020-05-05 20:01:23 +03:00
|
|
|
m_ECIESx25519Sessions.erase (it);
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 01:45:08 +03:00
|
|
|
|
|
|
|
uint8_t * GarlicDestination::GetPayloadBuffer ()
|
|
|
|
{
|
|
|
|
if (!m_PayloadBuffer)
|
|
|
|
m_PayloadBuffer = new uint8_t[I2NP_MAX_MESSAGE_SIZE];
|
|
|
|
return m_PayloadBuffer;
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2013-11-25 03:10:27 +04:00
|
|
|
}
|