mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
235 lines
7.5 KiB
235 lines
7.5 KiB
11 years ago
|
#ifndef GARLIC_H__
|
||
|
#define GARLIC_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
11 years ago
|
#include <map>
|
||
11 years ago
|
#include <list>
|
||
11 years ago
|
#include <string>
|
||
11 years ago
|
#include <thread>
|
||
10 years ago
|
#include <mutex>
|
||
10 years ago
|
#include <memory>
|
||
9 years ago
|
#include "Crypto.h"
|
||
11 years ago
|
#include "I2NPProtocol.h"
|
||
11 years ago
|
#include "LeaseSet.h"
|
||
11 years ago
|
#include "Queue.h"
|
||
10 years ago
|
#include "Identity.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
9 years ago
|
{
|
||
|
namespace tunnel
|
||
|
{
|
||
|
class OutboundTunnel;
|
||
|
}
|
||
|
|
||
11 years ago
|
namespace garlic
|
||
11 years ago
|
{
|
||
11 years ago
|
|
||
11 years ago
|
enum GarlicDeliveryType
|
||
|
{
|
||
|
eGarlicDeliveryTypeLocal = 0,
|
||
|
eGarlicDeliveryTypeDestination = 1,
|
||
|
eGarlicDeliveryTypeRouter = 2,
|
||
|
eGarlicDeliveryTypeTunnel = 3
|
||
|
};
|
||
|
|
||
|
struct ElGamalBlock
|
||
|
{
|
||
|
uint8_t sessionKey[32];
|
||
|
uint8_t preIV[32];
|
||
|
uint8_t padding[158];
|
||
|
};
|
||
11 years ago
|
|
||
10 years ago
|
const int INCOMING_TAGS_EXPIRATION_TIMEOUT = 960; // 16 minutes
|
||
10 years ago
|
const int OUTGOING_TAGS_EXPIRATION_TIMEOUT = 720; // 12 minutes
|
||
9 years ago
|
const int OUTGOING_TAGS_CONFIRMATION_TIMEOUT = 10; // 10 seconds
|
||
10 years ago
|
const int LEASET_CONFIRMATION_TIMEOUT = 4000; // in milliseconds
|
||
9 years ago
|
const int ROUTING_PATH_EXPIRATION_TIMEOUT = 30; // 30 seconds
|
||
9 years ago
|
const int ROUTING_PATH_MAX_NUM_TIMES_USED = 100; // how many times might be used
|
||
10 years ago
|
|
||
10 years ago
|
struct SessionTag: public i2p::data::Tag<32>
|
||
|
{
|
||
|
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
|
||
|
SessionTag (SessionTag&& ) = default;
|
||
|
SessionTag& operator= (SessionTag&& ) = default;
|
||
|
#endif
|
||
|
uint32_t creationTime; // seconds since epoch
|
||
|
};
|
||
9 years ago
|
|
||
8 years ago
|
// 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);
|
||
|
}
|
||
|
const i2p::crypto::AESKey& GetKey () const { return m_Key; };
|
||
|
|
||
|
private:
|
||
|
|
||
|
i2p::crypto::AESKey m_Key;
|
||
|
};
|
||
|
|
||
9 years ago
|
struct GarlicRoutingPath
|
||
|
{
|
||
|
std::shared_ptr<i2p::tunnel::OutboundTunnel> outboundTunnel;
|
||
|
std::shared_ptr<const i2p::data::Lease> remoteLease;
|
||
9 years ago
|
int rtt; // RTT
|
||
9 years ago
|
uint32_t updateTime; // seconds since epoch
|
||
9 years ago
|
int numTimesUsed;
|
||
9 years ago
|
};
|
||
8 years ago
|
|
||
10 years ago
|
class GarlicDestination;
|
||
10 years ago
|
class GarlicRoutingSession: public std::enable_shared_from_this<GarlicRoutingSession>
|
||
11 years ago
|
{
|
||
10 years ago
|
enum LeaseSetUpdateStatus
|
||
|
{
|
||
|
eLeaseSetUpToDate = 0,
|
||
|
eLeaseSetUpdated,
|
||
10 years ago
|
eLeaseSetSubmitted,
|
||
|
eLeaseSetDoNotSend
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
struct UnconfirmedTags
|
||
|
{
|
||
|
UnconfirmedTags (int n): numTags (n), tagsCreationTime (0) { sessionTags = new SessionTag[numTags]; };
|
||
|
~UnconfirmedTags () { delete[] sessionTags; };
|
||
9 years ago
|
uint32_t msgID;
|
||
10 years ago
|
int numTags;
|
||
|
SessionTag * sessionTags;
|
||
|
uint32_t tagsCreationTime;
|
||
|
};
|
||
|
|
||
11 years ago
|
public:
|
||
|
|
||
10 years ago
|
GarlicRoutingSession (GarlicDestination * owner, std::shared_ptr<const i2p::data::RoutingDestination> destination,
|
||
|
int numTags, bool attachLeaseSet);
|
||
11 years ago
|
GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag); // one time encryption
|
||
11 years ago
|
~GarlicRoutingSession ();
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg);
|
||
10 years ago
|
void MessageConfirmed (uint32_t msgID);
|
||
10 years ago
|
bool CleanupExpiredTags (); // returns true if something left
|
||
9 years ago
|
bool CleanupUnconfirmedTags (); // returns true if something has been deleted
|
||
10 years ago
|
|
||
10 years ago
|
void SetLeaseSetUpdated ()
|
||
|
{
|
||
|
if (m_LeaseSetUpdateStatus != eLeaseSetDoNotSend) m_LeaseSetUpdateStatus = eLeaseSetUpdated;
|
||
|
};
|
||
8 years ago
|
bool IsLeaseSetNonConfirmed () const { return m_LeaseSetUpdateStatus == eLeaseSetSubmitted; };
|
||
8 years ago
|
bool IsLeaseSetUpdated () const { return m_LeaseSetUpdateStatus == eLeaseSetUpdated; };
|
||
8 years ago
|
uint64_t GetLeaseSetSubmissionTime () const { return m_LeaseSetSubmissionTime; }
|
||
8 years ago
|
|
||
9 years ago
|
std::shared_ptr<GarlicRoutingPath> GetSharedRoutingPath ();
|
||
|
void SetSharedRoutingPath (std::shared_ptr<GarlicRoutingPath> path);
|
||
8 years ago
|
|
||
8 years ago
|
const GarlicDestination * GetOwner () const { return m_Owner; }
|
||
|
void SetOwner (GarlicDestination * owner) { m_Owner = owner; }
|
||
|
|
||
11 years ago
|
private:
|
||
|
|
||
9 years ago
|
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);
|
||
11 years ago
|
size_t CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID);
|
||
10 years ago
|
|
||
|
void TagsConfirmed (uint32_t msgID);
|
||
10 years ago
|
UnconfirmedTags * GenerateSessionTags ();
|
||
11 years ago
|
|
||
11 years ago
|
private:
|
||
|
|
||
10 years ago
|
GarlicDestination * m_Owner;
|
||
8 years ago
|
std::shared_ptr<const i2p::data::RoutingDestination> m_Destination;
|
||
8 years ago
|
|
||
10 years ago
|
i2p::crypto::AESKey m_SessionKey;
|
||
10 years ago
|
std::list<SessionTag> m_SessionTags;
|
||
|
int m_NumTags;
|
||
8 years ago
|
std::map<uint32_t, std::unique_ptr<UnconfirmedTags> > m_UnconfirmedTagsMsgs; // msgID->tags
|
||
10 years ago
|
|
||
|
LeaseSetUpdateStatus m_LeaseSetUpdateStatus;
|
||
|
uint32_t m_LeaseSetUpdateMsgID;
|
||
|
uint64_t m_LeaseSetSubmissionTime; // in milliseconds
|
||
|
|
||
11 years ago
|
i2p::crypto::CBCEncryption m_Encryption;
|
||
9 years ago
|
|
||
9 years ago
|
std::shared_ptr<GarlicRoutingPath> m_SharedRoutingPath;
|
||
|
|
||
9 years ago
|
public:
|
||
|
// for HTTP only
|
||
|
size_t GetNumOutgoingTags () const { return m_SessionTags.size (); };
|
||
11 years ago
|
};
|
||
9 years ago
|
//using GarlicRoutingSessionPtr = std::shared_ptr<GarlicRoutingSession>;
|
||
|
typedef std::shared_ptr<GarlicRoutingSession> GarlicRoutingSessionPtr; // TODO: replace to using after switch to 4.8
|
||
9 years ago
|
|
||
10 years ago
|
class GarlicDestination: public i2p::data::LocalDestination
|
||
|
{
|
||
|
public:
|
||
|
|
||
8 years ago
|
GarlicDestination ();
|
||
10 years ago
|
~GarlicDestination ();
|
||
|
|
||
8 years ago
|
void CleanUp ();
|
||
8 years ago
|
void SetNumTags (int numTags) { m_NumTags = numTags; };
|
||
10 years ago
|
std::shared_ptr<GarlicRoutingSession> GetRoutingSession (std::shared_ptr<const i2p::data::RoutingDestination> destination, bool attachLeaseSet);
|
||
9 years ago
|
void CleanupExpiredTags ();
|
||
9 years ago
|
void RemoveDeliveryStatusSession (uint32_t msgID);
|
||
10 years ago
|
std::shared_ptr<I2NPMessage> WrapMessage (std::shared_ptr<const i2p::data::RoutingDestination> destination,
|
||
|
std::shared_ptr<I2NPMessage> msg, bool attachLeaseSet = false);
|
||
10 years ago
|
|
||
|
void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag
|
||
10 years ago
|
virtual bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag); // from different thread
|
||
9 years ago
|
void DeliveryStatusSent (GarlicRoutingSessionPtr session, uint32_t msgID);
|
||
10 years ago
|
|
||
10 years ago
|
virtual void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg);
|
||
|
virtual void ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
|
||
10 years ago
|
virtual void SetLeaseSetUpdated ();
|
||
10 years ago
|
|
||
9 years ago
|
virtual std::shared_ptr<const i2p::data::LocalLeaseSet> GetLeaseSet () = 0; // TODO
|
||
10 years ago
|
virtual std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () const = 0;
|
||
10 years ago
|
virtual void HandleI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from) = 0;
|
||
10 years ago
|
|
||
10 years ago
|
protected:
|
||
|
|
||
10 years ago
|
void HandleGarlicMessage (std::shared_ptr<I2NPMessage> msg);
|
||
|
void HandleDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
|
||
8 years ago
|
|
||
|
void SaveTags ();
|
||
|
void LoadTags ();
|
||
|
|
||
10 years ago
|
private:
|
||
|
|
||
8 years ago
|
void HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr<AESDecryption> decryption,
|
||
10 years ago
|
std::shared_ptr<i2p::tunnel::InboundTunnel> from);
|
||
|
void HandleGarlicPayload (uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from);
|
||
10 years ago
|
|
||
|
private:
|
||
8 years ago
|
|
||
8 years ago
|
BN_CTX * m_Ctx; // incoming
|
||
10 years ago
|
// outgoing sessions
|
||
9 years ago
|
int m_NumTags;
|
||
10 years ago
|
std::mutex m_SessionsMutex;
|
||
9 years ago
|
std::map<i2p::data::IdentHash, GarlicRoutingSessionPtr> m_Sessions;
|
||
10 years ago
|
// incoming
|
||
8 years ago
|
std::map<SessionTag, std::shared_ptr<AESDecryption> > m_Tags;
|
||
10 years ago
|
// DeliveryStatus
|
||
8 years ago
|
std::mutex m_DeliveryStatusSessionsMutex;
|
||
9 years ago
|
std::map<uint32_t, GarlicRoutingSessionPtr> m_DeliveryStatusSessions; // msgID -> session
|
||
9 years ago
|
|
||
|
public:
|
||
|
|
||
|
// for HTTP only
|
||
|
size_t GetNumIncomingTags () const { return m_Tags.size (); }
|
||
|
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
|
||
8 years ago
|
};
|
||
|
|
||
|
void CleanUpTagsFiles ();
|
||
|
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
#endif
|