i2pd/libi2pd/Garlic.h

283 lines
9.7 KiB
C
Raw Normal View History

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
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; }
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
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
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);
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:
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;
2020-02-03 21:21:07 +00:00
struct ECIESX25519AEADRatchetIndexSession
{
int index;
ECIESX25519AEADRatchetSessionPtr session;
};
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; };
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);
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);
void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag
virtual bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag); // from different thread
void DeliveryStatusSent (ElGamalAESSessionPtr session, uint32_t msgID);
2020-02-03 21:21:07 +00:00
void AddECIESx25519SessionTag (uint64_t tag, int index, ECIESX25519AEADRatchetSessionPtr session);
2020-01-29 17:54:26 +00:00
void AddECIESx25519Session (const uint8_t * staticKey, ECIESX25519AEADRatchetSessionPtr session);
2020-02-03 21:21:07 +00:00
void HandleECIESx25519GarlicClove (const uint8_t * buf, size_t len);
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);
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:
2020-01-07 20:20:55 +00:00
virtual void HandleI2NPMessage (const uint8_t * buf, size_t len) = 0; // called from clove only
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
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);
// ECIES-X25519-AEAD-Ratchet
void HandleECIESx25519 (const uint8_t * buf, size_t len);
private:
2017-03-14 16:03:51 +00:00
BN_CTX * m_Ctx; // incoming
// 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
// incoming
2020-01-28 15:31:35 +00:00
std::unordered_map<SessionTag, std::shared_ptr<AESDecryption>, std::hash<i2p::data::Tag<32> > > m_Tags;
2020-02-03 21:21:07 +00:00
std::unordered_map<uint64_t, ECIESX25519AEADRatchetIndexSession> 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-28 15:03:51 +00:00
std::unordered_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