i2pd/libi2pd/Garlic.h

313 lines
11 KiB
C
Raw Normal View History

/*
2024-02-16 19:49:12 +00:00
* Copyright (c) 2013-2024, The PurpleI2P Project
*
* 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-23 21:35:15 +00:00
#ifndef GARLIC_H__
#define GARLIC_H__
#include <inttypes.h>
2020-01-28 15:03:51 +00:00
#include <unordered_map>
2014-05-12 02:37:33 +00:00
#include <list>
2013-11-24 23:10:27 +00:00
#include <string>
#include <thread>
2014-08-25 17:07:14 +00:00
#include <mutex>
#include <memory>
2015-11-03 14:15:49 +00:00
#include "Crypto.h"
2013-11-23 21:35:15 +00:00
#include "I2NPProtocol.h"
2013-11-24 23:10:27 +00:00
#include "LeaseSet.h"
#include "Queue.h"
2014-10-06 20:49:41 +00:00
#include "Identity.h"
2013-11-23 21:35:15 +00:00
namespace i2p
2016-02-11 03:51:08 +00:00
{
namespace tunnel
2018-01-06 03:48:51 +00:00
{
2016-02-11 03:51:08 +00:00
class OutboundTunnel;
}
2018-01-06 03:48:51 +00:00
2013-11-24 23:10:27 +00:00
namespace garlic
2013-11-23 21:35:15 +00:00
{
2018-01-06 03:48:51 +00:00
enum GarlicDeliveryType
{
eGarlicDeliveryTypeLocal = 0,
2013-11-23 21:35:15 +00:00
eGarlicDeliveryTypeDestination = 1,
2018-01-06 03:48:51 +00:00
eGarlicDeliveryTypeRouter = 2,
2013-11-23 21:35:15 +00:00
eGarlicDeliveryTypeTunnel = 3
2018-01-06 03:48:51 +00:00
};
2013-11-23 21:35:15 +00:00
struct ElGamalBlock
{
uint8_t sessionKey[32];
uint8_t preIV[32];
uint8_t padding[158];
2018-01-06 03:48:51 +00:00
};
2013-11-24 23:10:27 +00:00
2018-01-06 03:48:51 +00:00
const int INCOMING_TAGS_EXPIRATION_TIMEOUT = 960; // 16 minutes
2014-10-17 01:11:02 +00:00
const int OUTGOING_TAGS_EXPIRATION_TIMEOUT = 720; // 12 minutes
2018-01-06 03:48:51 +00:00
const int OUTGOING_TAGS_CONFIRMATION_TIMEOUT = 10; // 10 seconds
2015-03-22 18:59:27 +00:00
const int LEASET_CONFIRMATION_TIMEOUT = 4000; // in milliseconds
2018-01-06 03:48:51 +00:00
const int ROUTING_PATH_EXPIRATION_TIMEOUT = 30; // 30 seconds
const int ROUTING_PATH_MAX_NUM_TIMES_USED = 100; // how many times might be used
struct SessionTag: public i2p::data::Tag<32>
2014-10-17 15:26:57 +00:00
{
SessionTag (const uint8_t * buf, uint32_t ts = 0): Tag<32>(buf), creationTime (ts) {};
SessionTag () = default;
SessionTag (const SessionTag& ) = default;
SessionTag& operator= (const SessionTag& ) = default;
#ifndef _WIN32
2018-01-06 03:48:51 +00:00
SessionTag (SessionTag&& ) = default;
SessionTag& operator= (SessionTag&& ) = default;
2014-10-17 15:26:57 +00:00
#endif
2018-01-06 03:48:51 +00:00
uint32_t creationTime; // seconds since epoch
2014-10-17 15:26:57 +00:00
};
2016-02-11 03:51:08 +00:00
2017-04-03 19:05:10 +00:00
// AESDecryption is associated with session tags and store key
class AESDecryption: public i2p::crypto::CBCDecryption
{
public:
AESDecryption (const uint8_t * key): m_Key (key)
{
SetKey (key);
2018-01-06 03:48:51 +00:00
}
2017-04-03 19:05:10 +00:00
const i2p::crypto::AESKey& GetKey () const { return m_Key; };
private:
i2p::crypto::AESKey m_Key;
};
2016-02-11 03:51:08 +00:00
struct GarlicRoutingPath
{
std::shared_ptr<i2p::tunnel::OutboundTunnel> outboundTunnel;
std::shared_ptr<const i2p::data::Lease> remoteLease;
2016-02-12 03:18:44 +00:00
int rtt; // RTT
2016-02-11 03:51:08 +00:00
uint32_t updateTime; // seconds since epoch
2018-01-06 03:48:51 +00:00
int numTimesUsed;
};
2017-04-03 19:05:10 +00:00
2014-10-08 01:08:00 +00:00
class GarlicDestination;
class GarlicRoutingSession
2013-11-24 23:10:27 +00:00
{
protected:
2015-03-22 18:59:27 +00:00
enum LeaseSetUpdateStatus
{
eLeaseSetUpToDate = 0,
eLeaseSetUpdated,
2015-04-06 00:07:32 +00:00
eLeaseSetSubmitted,
eLeaseSetDoNotSend
2015-03-22 18:59:27 +00:00
};
2018-01-06 03:48:51 +00:00
2013-11-24 23:10:27 +00:00
public:
GarlicRoutingSession (GarlicDestination * owner, bool attachLeaseSet);
GarlicRoutingSession ();
virtual ~GarlicRoutingSession ();
virtual std::shared_ptr<I2NPMessage> WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg) = 0;
virtual bool CleanupUnconfirmedTags () { return false; }; // for I2CP, override in ElGamalAESSession
2020-03-08 22:13:41 +00:00
virtual bool MessageConfirmed (uint32_t msgID);
virtual bool IsRatchets () const { return false; };
virtual bool IsReadyToSend () const { return true; };
virtual bool IsTerminated () const { return !GetOwner (); };
virtual uint64_t GetLastActivityTimestamp () const { return 0; }; // non-zero for rathets only
2018-01-06 03:48:51 +00:00
void SetLeaseSetUpdated ()
{
if (m_LeaseSetUpdateStatus != eLeaseSetDoNotSend) m_LeaseSetUpdateStatus = eLeaseSetUpdated;
2015-04-06 00:07:32 +00:00
};
bool IsLeaseSetNonConfirmed () const { return m_LeaseSetUpdateStatus == eLeaseSetSubmitted; };
bool IsLeaseSetUpdated () const { return m_LeaseSetUpdateStatus == eLeaseSetUpdated; };
2018-01-06 03:48:51 +00:00
uint64_t GetLeaseSetSubmissionTime () const { return m_LeaseSetSubmissionTime; }
void CleanupUnconfirmedLeaseSet (uint64_t ts);
2016-02-11 03:51:08 +00:00
std::shared_ptr<GarlicRoutingPath> GetSharedRoutingPath ();
void SetSharedRoutingPath (std::shared_ptr<GarlicRoutingPath> path);
GarlicDestination * GetOwner () const { return m_Owner; }
void SetOwner (GarlicDestination * owner) { m_Owner = owner; }
protected:
LeaseSetUpdateStatus GetLeaseSetUpdateStatus () const { return m_LeaseSetUpdateStatus; }
void SetLeaseSetUpdateStatus (LeaseSetUpdateStatus status) { m_LeaseSetUpdateStatus = status; }
uint32_t GetLeaseSetUpdateMsgID () const { return m_LeaseSetUpdateMsgID; }
void SetLeaseSetUpdateMsgID (uint32_t msgID) { m_LeaseSetUpdateMsgID = msgID; }
void SetLeaseSetSubmissionTime (uint64_t ts) { m_LeaseSetSubmissionTime = ts; }
2014-01-18 15:34:57 +00:00
2020-03-08 22:13:41 +00:00
std::shared_ptr<I2NPMessage> CreateEncryptedDeliveryStatusMsg (uint32_t msgID);
2013-11-24 23:10:27 +00:00
private:
2014-10-08 01:08:00 +00:00
GarlicDestination * m_Owner;
2018-01-06 03:48:51 +00:00
2015-03-22 18:59:27 +00:00
LeaseSetUpdateStatus m_LeaseSetUpdateStatus;
uint32_t m_LeaseSetUpdateMsgID;
uint64_t m_LeaseSetSubmissionTime; // in milliseconds
2018-01-06 03:48:51 +00:00
2016-02-11 03:51:08 +00:00
std::shared_ptr<GarlicRoutingPath> m_SharedRoutingPath;
2018-01-06 03:48:51 +00:00
2016-01-24 03:53:19 +00:00
public:
2016-01-24 03:53:19 +00:00
// for HTTP only
virtual size_t GetNumOutgoingTags () const { return 0; };
2018-01-06 03:48:51 +00:00
};
//using GarlicRoutingSessionPtr = std::shared_ptr<GarlicRoutingSession>;
typedef std::shared_ptr<GarlicRoutingSession> GarlicRoutingSessionPtr; // TODO: replace to using after switch to 4.8
class ElGamalAESSession: public GarlicRoutingSession, public std::enable_shared_from_this<ElGamalAESSession>
{
struct UnconfirmedTags
{
UnconfirmedTags (int n): numTags (n), tagsCreationTime (0) { sessionTags = new SessionTag[numTags]; };
~UnconfirmedTags () { delete[] sessionTags; };
uint32_t msgID;
int numTags;
SessionTag * sessionTags;
uint32_t tagsCreationTime;
};
public:
ElGamalAESSession (GarlicDestination * owner, std::shared_ptr<const i2p::data::RoutingDestination> destination,
int numTags, bool attachLeaseSet);
ElGamalAESSession (const uint8_t * sessionKey, const SessionTag& sessionTag); // one time encryption
~ElGamalAESSession () {};
std::shared_ptr<I2NPMessage> WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg);
bool MessageConfirmed (uint32_t msgID);
bool CleanupExpiredTags (); // returns true if something left
bool CleanupUnconfirmedTags (); // returns true if something has been deleted
private:
size_t CreateAESBlock (uint8_t * buf, std::shared_ptr<const I2NPMessage> msg);
size_t CreateGarlicPayload (uint8_t * payload, std::shared_ptr<const I2NPMessage> msg, UnconfirmedTags * newTags);
size_t CreateGarlicClove (uint8_t * buf, std::shared_ptr<const I2NPMessage> msg, bool isDestination);
size_t CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID);
void TagsConfirmed (uint32_t msgID);
UnconfirmedTags * GenerateSessionTags ();
private:
std::shared_ptr<const i2p::data::RoutingDestination> m_Destination;
i2p::crypto::AESKey m_SessionKey;
std::list<SessionTag> m_SessionTags;
int m_NumTags;
std::map<uint32_t, std::unique_ptr<UnconfirmedTags> > m_UnconfirmedTagsMsgs; // msgID->tags
i2p::crypto::CBCEncryption m_Encryption;
public:
// for HTTP only
size_t GetNumOutgoingTags () const { return m_SessionTags.size (); };
};
typedef std::shared_ptr<ElGamalAESSession> ElGamalAESSessionPtr;
2016-01-25 18:34:04 +00:00
class ECIESX25519AEADRatchetSession;
typedef std::shared_ptr<ECIESX25519AEADRatchetSession> ECIESX25519AEADRatchetSessionPtr;
2021-01-04 23:20:16 +00:00
class ReceiveRatchetTagSet;
typedef std::shared_ptr<ReceiveRatchetTagSet> ReceiveRatchetTagSetPtr;
2020-04-24 19:46:02 +00:00
struct ECIESX25519AEADRatchetIndexTagset
{
int index;
2024-02-16 19:49:12 +00:00
ReceiveRatchetTagSetPtr tagset; // null if used
2020-02-03 21:21:07 +00:00
};
class GarlicDestination: public i2p::data::LocalDestination
{
public:
GarlicDestination ();
~GarlicDestination ();
2016-11-29 03:47:37 +00:00
void CleanUp ();
2018-01-06 03:48:51 +00:00
void SetNumTags (int numTags) { m_NumTags = numTags; };
2020-02-05 20:48:51 +00:00
int GetNumTags () const { return m_NumTags; };
2020-06-13 00:42:54 +00:00
void SetNumRatchetInboundTags (int numTags) { m_NumRatchetInboundTags = numTags; };
int GetNumRatchetInboundTags () const { return m_NumRatchetInboundTags; };
2018-01-06 03:48:51 +00:00
std::shared_ptr<GarlicRoutingSession> GetRoutingSession (std::shared_ptr<const i2p::data::RoutingDestination> destination, bool attachLeaseSet);
void CleanupExpiredTags ();
2016-01-25 18:34:04 +00:00
void RemoveDeliveryStatusSession (uint32_t msgID);
std::shared_ptr<I2NPMessage> WrapMessageForRouter (std::shared_ptr<const i2p::data::RouterInfo> router,
std::shared_ptr<I2NPMessage> msg);
void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag
void AddECIESx25519Key (const uint8_t * key, uint64_t tag); // one tag
virtual bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag); // from different thread
virtual void SubmitECIESx25519Key (const uint8_t * key, uint64_t tag); // from different thread
void DeliveryStatusSent (GarlicRoutingSessionPtr session, uint32_t msgID);
2021-01-04 23:20:16 +00:00
uint64_t AddECIESx25519SessionNextTag (ReceiveRatchetTagSetPtr tagset);
2020-01-29 17:54:26 +00:00
void AddECIESx25519Session (const uint8_t * staticKey, ECIESX25519AEADRatchetSessionPtr session);
2020-05-05 17:01:23 +00:00
void RemoveECIESx25519Session (const uint8_t * staticKey);
2020-02-03 21:21:07 +00:00
void HandleECIESx25519GarlicClove (const uint8_t * buf, size_t len);
uint8_t * GetPayloadBuffer ();
2015-06-16 14:14:14 +00:00
virtual void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg);
2018-01-06 03:48:51 +00:00
virtual void ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
virtual void SetLeaseSetUpdated ();
2018-01-06 03:48:51 +00:00
2016-05-25 19:10:28 +00:00
virtual std::shared_ptr<const i2p::data::LocalLeaseSet> GetLeaseSet () = 0; // TODO
virtual std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () const = 0;
2018-01-06 03:48:51 +00:00
protected:
void AddECIESx25519Key (const uint8_t * key, const uint8_t * tag); // one tag
bool HandleECIESx25519TagMessage (uint8_t * buf, size_t len); // return true if found
2020-01-07 20:20:55 +00:00
virtual void HandleI2NPMessage (const uint8_t * buf, size_t len) = 0; // called from clove only
2021-07-21 02:00:06 +00:00
virtual bool HandleCloveI2NPMessage (I2NPMessageType typeID, const uint8_t * payload, size_t len, uint32_t msgID) = 0;
2015-06-16 14:14:14 +00:00
void HandleGarlicMessage (std::shared_ptr<I2NPMessage> msg);
2020-01-06 19:37:40 +00:00
void HandleDeliveryStatusMessage (uint32_t msgID);
2017-04-03 19:05:10 +00:00
void SaveTags ();
2018-01-06 03:48:51 +00:00
void LoadTags ();
2017-04-03 19:05:10 +00:00
private:
2018-01-06 03:48:51 +00:00
void HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr<AESDecryption> decryption,
2015-02-05 23:53:43 +00:00
std::shared_ptr<i2p::tunnel::InboundTunnel> from);
void HandleGarlicPayload (uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from);
private:
// outgoing sessions
int m_NumTags;
std::mutex m_SessionsMutex;
2020-01-28 15:03:51 +00:00
std::unordered_map<i2p::data::IdentHash, ElGamalAESSessionPtr> m_Sessions;
std::unordered_map<i2p::data::Tag<32>, ECIESX25519AEADRatchetSessionPtr> m_ECIESx25519Sessions; // static key -> session
uint8_t * m_PayloadBuffer; // for ECIESX25519AEADRatchet
// incoming
2020-06-13 00:42:54 +00:00
int m_NumRatchetInboundTags;
2020-01-28 15:31:35 +00:00
std::unordered_map<SessionTag, std::shared_ptr<AESDecryption>, std::hash<i2p::data::Tag<32> > > m_Tags;
std::unordered_map<uint64_t, ECIESX25519AEADRatchetIndexTagset> m_ECIESx25519Tags; // session tag -> session
2021-01-04 23:20:16 +00:00
ReceiveRatchetTagSetPtr m_LastTagset; // tagset last message came for
2014-10-08 01:47:32 +00:00
// DeliveryStatus
2016-11-18 16:16:55 +00:00
std::mutex m_DeliveryStatusSessionsMutex;
2020-03-08 22:13:41 +00:00
std::unordered_map<uint32_t, GarlicRoutingSessionPtr> m_DeliveryStatusSessions; // msgID -> session
2018-01-06 03:48:51 +00:00
2016-01-24 03:53:19 +00:00
public:
// for HTTP only
2018-01-06 03:48:51 +00:00
size_t GetNumIncomingTags () const { return m_Tags.size (); }
2024-03-05 15:30:01 +00:00
size_t GetNumIncomingECIESx25519Tags () const { return m_ECIESx25519Tags.size (); }
2016-01-24 03:53:19 +00:00
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
const decltype(m_ECIESx25519Sessions)& GetECIESx25519Sessions () const { return m_ECIESx25519Sessions; }
2017-04-03 19:05:10 +00:00
};
void CleanUpTagsFiles ();
2018-01-06 03:48:51 +00:00
}
2013-11-24 23:10:27 +00:00
}
2013-11-23 21:35:15 +00:00
#endif