2013-11-23 21:35:15 +00:00
|
|
|
#ifndef GARLIC_H__
|
|
|
|
#define GARLIC_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2013-11-24 23:10:27 +00:00
|
|
|
#include <map>
|
2014-05-12 02:37:33 +00:00
|
|
|
#include <list>
|
2013-11-24 23:10:27 +00:00
|
|
|
#include <string>
|
2014-03-13 01:13:54 +00:00
|
|
|
#include <thread>
|
2014-08-25 17:07:14 +00:00
|
|
|
#include <mutex>
|
2014-10-07 01:18:20 +00:00
|
|
|
#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"
|
2014-03-13 01:13:54 +00:00
|
|
|
#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;
|
2020-01-16 19:31:01 +00:00
|
|
|
class GarlicRoutingSession
|
2013-11-24 23:10:27 +00:00
|
|
|
{
|
2020-01-16 19:31:01 +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:
|
|
|
|
|
2020-01-16 19:59:19 +00:00
|
|
|
GarlicRoutingSession (GarlicDestination * owner, bool attachLeaseSet);
|
2020-01-16 19:31:01 +00:00
|
|
|
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
|
2014-10-08 16:11:39 +00:00
|
|
|
|
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
|
|
|
};
|
2016-09-07 17:25:11 +00:00
|
|
|
bool IsLeaseSetNonConfirmed () const { return m_LeaseSetUpdateStatus == eLeaseSetSubmitted; };
|
2016-10-23 00:08:15 +00:00
|
|
|
bool IsLeaseSetUpdated () const { return m_LeaseSetUpdateStatus == eLeaseSetUpdated; };
|
2018-01-06 03:48:51 +00:00
|
|
|
uint64_t GetLeaseSetSubmissionTime () const { return m_LeaseSetSubmissionTime; }
|
|
|
|
|
2016-02-11 03:51:08 +00:00
|
|
|
std::shared_ptr<GarlicRoutingPath> GetSharedRoutingPath ();
|
|
|
|
void SetSharedRoutingPath (std::shared_ptr<GarlicRoutingPath> path);
|
2016-08-27 17:17:34 +00:00
|
|
|
|
2020-01-16 19:31:01 +00:00
|
|
|
GarlicDestination * GetOwner () const { return m_Owner; }
|
2016-11-16 19:43:29 +00:00
|
|
|
void SetOwner (GarlicDestination * owner) { m_Owner = owner; }
|
|
|
|
|
2020-01-16 19:31:01 +00:00
|
|
|
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; }
|
2020-01-16 19:59:19 +00:00
|
|
|
void SetLeaseSetSubmissionTime (uint64_t ts) { m_LeaseSetSubmissionTime = ts; }
|
2014-01-18 15:34:57 +00:00
|
|
|
|
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:
|
|
|
|
// for HTTP only
|
2020-01-16 19:31:01 +00:00
|
|
|
virtual size_t GetNumOutgoingTags () const { return 0; };
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
2020-01-16 19:31:01 +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);
|
|
|
|
|
|
|
|
void 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:
|
2020-01-16 19:59:19 +00:00
|
|
|
|
|
|
|
std::shared_ptr<const i2p::data::RoutingDestination> m_Destination;
|
2020-01-16 19:31:01 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-01-16 20:45:22 +00:00
|
|
|
class ECIESX25519AEADRatchetSession;
|
|
|
|
typedef std::shared_ptr<ECIESX25519AEADRatchetSession> ECIESX25519AEADRatchetSessionPtr;
|
|
|
|
|
2014-10-07 20:18:13 +00:00
|
|
|
class GarlicDestination: public i2p::data::LocalDestination
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-03-11 21:48:54 +00:00
|
|
|
GarlicDestination ();
|
2014-10-07 20:18:13 +00:00
|
|
|
~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; };
|
|
|
|
std::shared_ptr<GarlicRoutingSession> GetRoutingSession (std::shared_ptr<const i2p::data::RoutingDestination> destination, bool attachLeaseSet);
|
2016-02-07 22:45:11 +00:00
|
|
|
void CleanupExpiredTags ();
|
2016-01-25 18:34:04 +00:00
|
|
|
void RemoveDeliveryStatusSession (uint32_t msgID);
|
2018-01-06 03:48:51 +00:00
|
|
|
std::shared_ptr<I2NPMessage> WrapMessage (std::shared_ptr<const i2p::data::RoutingDestination> destination,
|
2015-06-22 02:29:50 +00:00
|
|
|
std::shared_ptr<I2NPMessage> msg, bool attachLeaseSet = false);
|
2014-10-07 20:18:13 +00:00
|
|
|
|
|
|
|
void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag
|
2014-12-08 20:36:00 +00:00
|
|
|
virtual bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag); // from different thread
|
2020-01-16 19:31:01 +00:00
|
|
|
void DeliveryStatusSent (ElGamalAESSessionPtr session, uint32_t msgID);
|
2020-01-21 19:40:23 +00:00
|
|
|
void AddECIESx25519SessionTag (uint64_t tag, ECIESX25519AEADRatchetSessionPtr session);
|
2018-01-06 03:48:51 +00:00
|
|
|
|
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);
|
2014-10-08 16:11:39 +00:00
|
|
|
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
|
2015-04-05 16:54:15 +00:00
|
|
|
virtual std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () const = 0;
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2014-10-08 18:17:17 +00:00
|
|
|
protected:
|
|
|
|
|
2020-01-07 20:20:55 +00:00
|
|
|
virtual void HandleI2NPMessage (const uint8_t * buf, size_t len) = 0; // called from clove only
|
2020-01-16 17:47:08 +00:00
|
|
|
virtual bool HandleCloveI2NPMessage (I2NPMessageType typeID, const uint8_t * payload, size_t len) = 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
|
|
|
|
2014-10-07 20:18:13 +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);
|
2014-10-07 20:18:13 +00:00
|
|
|
|
2019-12-17 21:18:40 +00:00
|
|
|
// ECIES-X25519-AEAD-Ratchet
|
|
|
|
void HandleECIESx25519 (const uint8_t * buf, size_t len);
|
2020-01-16 17:47:08 +00:00
|
|
|
void HandleECIESx25519GarlicClove (const uint8_t * buf, size_t len);
|
2019-12-17 21:18:40 +00:00
|
|
|
|
2014-10-07 20:18:13 +00:00
|
|
|
private:
|
2017-03-11 21:48:54 +00:00
|
|
|
|
2017-03-14 16:03:51 +00:00
|
|
|
BN_CTX * m_Ctx; // incoming
|
2014-10-07 20:18:13 +00:00
|
|
|
// outgoing sessions
|
2016-01-24 01:52:21 +00:00
|
|
|
int m_NumTags;
|
2014-10-07 20:18:13 +00:00
|
|
|
std::mutex m_SessionsMutex;
|
2020-01-16 19:31:01 +00:00
|
|
|
std::map<i2p::data::IdentHash, ElGamalAESSessionPtr> m_Sessions;
|
2020-01-21 19:40:23 +00:00
|
|
|
std::map<i2p::data::Tag<32>, ECIESX25519AEADRatchetSessionPtr> m_ECIESx25519Sessions; // static key -> session
|
2014-10-07 20:18:13 +00:00
|
|
|
// incoming
|
2017-04-03 19:05:10 +00:00
|
|
|
std::map<SessionTag, std::shared_ptr<AESDecryption> > m_Tags;
|
2020-01-21 19:40:23 +00:00
|
|
|
std::map<uint64_t, ECIESX25519AEADRatchetSessionPtr> m_ECIESx25519Tags; // session tag -> session
|
2014-10-08 01:47:32 +00:00
|
|
|
// DeliveryStatus
|
2016-11-18 16:16:55 +00:00
|
|
|
std::mutex m_DeliveryStatusSessionsMutex;
|
2020-01-16 19:31:01 +00:00
|
|
|
std::map<uint32_t, ElGamalAESSessionPtr> 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 (); }
|
2016-01-24 03:53:19 +00:00
|
|
|
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
|
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
|