2013-11-24 01:35:15 +04:00
|
|
|
#ifndef GARLIC_H__
|
|
|
|
#define GARLIC_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2013-11-25 03:10:27 +04:00
|
|
|
#include <map>
|
2014-05-12 06:37:33 +04:00
|
|
|
#include <list>
|
2013-11-25 03:10:27 +04:00
|
|
|
#include <string>
|
2014-03-13 05:13:54 +04:00
|
|
|
#include <thread>
|
2014-08-25 21:07:14 +04:00
|
|
|
#include <mutex>
|
2014-10-07 05:18:20 +04:00
|
|
|
#include <memory>
|
2015-11-03 17:15:49 +03:00
|
|
|
#include "Crypto.h"
|
2013-11-24 01:35:15 +04:00
|
|
|
#include "I2NPProtocol.h"
|
2013-11-25 03:10:27 +04:00
|
|
|
#include "LeaseSet.h"
|
2014-03-13 05:13:54 +04:00
|
|
|
#include "Queue.h"
|
2014-10-07 00:49:41 +04:00
|
|
|
#include "Identity.h"
|
2013-11-24 01:35:15 +04:00
|
|
|
|
|
|
|
namespace i2p
|
2016-02-11 06:51:08 +03:00
|
|
|
{
|
|
|
|
namespace tunnel
|
|
|
|
{
|
|
|
|
class OutboundTunnel;
|
|
|
|
}
|
|
|
|
|
2013-11-25 03:10:27 +04:00
|
|
|
namespace garlic
|
2013-11-24 01:35:15 +04:00
|
|
|
{
|
2013-11-25 03:10:27 +04:00
|
|
|
|
2013-11-24 01:35:15 +04:00
|
|
|
enum GarlicDeliveryType
|
|
|
|
{
|
|
|
|
eGarlicDeliveryTypeLocal = 0,
|
|
|
|
eGarlicDeliveryTypeDestination = 1,
|
|
|
|
eGarlicDeliveryTypeRouter = 2,
|
|
|
|
eGarlicDeliveryTypeTunnel = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ElGamalBlock
|
|
|
|
{
|
|
|
|
uint8_t sessionKey[32];
|
|
|
|
uint8_t preIV[32];
|
|
|
|
uint8_t padding[158];
|
|
|
|
};
|
2013-11-25 03:10:27 +04:00
|
|
|
|
2014-10-18 06:15:42 +04:00
|
|
|
const int INCOMING_TAGS_EXPIRATION_TIMEOUT = 960; // 16 minutes
|
2014-10-17 05:11:02 +04:00
|
|
|
const int OUTGOING_TAGS_EXPIRATION_TIMEOUT = 720; // 12 minutes
|
2016-01-26 06:10:06 +03:00
|
|
|
const int OUTGOING_TAGS_CONFIRMATION_TIMEOUT = 10; // 10 seconds
|
2015-03-22 21:59:27 +03:00
|
|
|
const int LEASET_CONFIRMATION_TIMEOUT = 4000; // in milliseconds
|
2016-02-11 06:51:08 +03:00
|
|
|
const int ROUTING_PATH_EXPIRATION_TIMEOUT = 30; // 30 seconds
|
2016-02-15 06:10:56 +03:00
|
|
|
const int ROUTING_PATH_MAX_NUM_TIMES_USED = 100; // how many times might be used
|
2014-10-17 05:11:02 +04:00
|
|
|
|
2014-10-17 19:26:57 +04:00
|
|
|
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
|
|
|
|
};
|
2016-02-11 06:51:08 +03:00
|
|
|
|
|
|
|
struct GarlicRoutingPath
|
|
|
|
{
|
|
|
|
std::shared_ptr<i2p::tunnel::OutboundTunnel> outboundTunnel;
|
|
|
|
std::shared_ptr<const i2p::data::Lease> remoteLease;
|
2016-02-12 06:18:44 +03:00
|
|
|
int rtt; // RTT
|
2016-02-11 06:51:08 +03:00
|
|
|
uint32_t updateTime; // seconds since epoch
|
2016-02-14 07:02:58 +03:00
|
|
|
int numTimesUsed;
|
2016-02-11 06:51:08 +03:00
|
|
|
};
|
|
|
|
|
2014-10-08 05:08:00 +04:00
|
|
|
class GarlicDestination;
|
2015-01-22 23:31:34 +03:00
|
|
|
class GarlicRoutingSession: public std::enable_shared_from_this<GarlicRoutingSession>
|
2013-11-25 03:10:27 +04:00
|
|
|
{
|
2015-03-22 21:59:27 +03:00
|
|
|
enum LeaseSetUpdateStatus
|
|
|
|
{
|
|
|
|
eLeaseSetUpToDate = 0,
|
|
|
|
eLeaseSetUpdated,
|
2015-04-06 03:07:32 +03:00
|
|
|
eLeaseSetSubmitted,
|
|
|
|
eLeaseSetDoNotSend
|
2015-03-22 21:59:27 +03:00
|
|
|
};
|
|
|
|
|
2014-10-14 22:48:25 +04:00
|
|
|
struct UnconfirmedTags
|
|
|
|
{
|
|
|
|
UnconfirmedTags (int n): numTags (n), tagsCreationTime (0) { sessionTags = new SessionTag[numTags]; };
|
|
|
|
~UnconfirmedTags () { delete[] sessionTags; };
|
2016-07-08 05:39:20 +03:00
|
|
|
uint32_t msgID;
|
2014-10-14 22:48:25 +04:00
|
|
|
int numTags;
|
|
|
|
SessionTag * sessionTags;
|
|
|
|
uint32_t tagsCreationTime;
|
|
|
|
};
|
|
|
|
|
2013-11-25 03:10:27 +04:00
|
|
|
public:
|
|
|
|
|
2015-04-06 03:07:32 +03:00
|
|
|
GarlicRoutingSession (GarlicDestination * owner, std::shared_ptr<const i2p::data::RoutingDestination> destination,
|
|
|
|
int numTags, bool attachLeaseSet);
|
2014-07-28 20:02:50 +04:00
|
|
|
GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag); // one time encryption
|
2013-11-25 03:10:27 +04:00
|
|
|
~GarlicRoutingSession ();
|
2015-11-03 17:15:49 +03:00
|
|
|
std::shared_ptr<I2NPMessage> WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg);
|
2015-03-22 21:59:27 +03:00
|
|
|
void MessageConfirmed (uint32_t msgID);
|
2015-01-23 18:07:11 +03:00
|
|
|
bool CleanupExpiredTags (); // returns true if something left
|
2016-07-08 21:17:41 +03:00
|
|
|
bool CleanupUnconfirmedTags (); // returns true if something has been deleted
|
2014-10-08 20:11:39 +04:00
|
|
|
|
2015-04-06 03:07:32 +03:00
|
|
|
void SetLeaseSetUpdated ()
|
|
|
|
{
|
|
|
|
if (m_LeaseSetUpdateStatus != eLeaseSetDoNotSend) m_LeaseSetUpdateStatus = eLeaseSetUpdated;
|
|
|
|
};
|
2016-09-07 20:25:11 +03:00
|
|
|
bool IsLeaseSetNonConfirmed () const { return m_LeaseSetUpdateStatus == eLeaseSetSubmitted; };
|
2016-10-23 03:08:15 +03:00
|
|
|
bool IsLeaseSetUpdated () const { return m_LeaseSetUpdateStatus == eLeaseSetUpdated; };
|
2016-10-25 03:58:25 +03:00
|
|
|
uint64_t GetLeaseSetSubmissionTime () const { return m_LeaseSetSubmissionTime; }
|
2016-10-23 03:08:15 +03:00
|
|
|
|
2016-02-11 06:51:08 +03:00
|
|
|
std::shared_ptr<GarlicRoutingPath> GetSharedRoutingPath ();
|
|
|
|
void SetSharedRoutingPath (std::shared_ptr<GarlicRoutingPath> path);
|
2016-08-27 20:17:34 +03:00
|
|
|
|
2016-11-16 22:43:29 +03:00
|
|
|
const GarlicDestination * GetOwner () const { return m_Owner; }
|
|
|
|
void SetOwner (GarlicDestination * owner) { m_Owner = owner; }
|
|
|
|
|
2013-11-25 03:10:27 +04:00
|
|
|
private:
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
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);
|
2014-01-09 07:47:22 +04:00
|
|
|
size_t CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID);
|
2015-03-22 21:59:27 +03:00
|
|
|
|
|
|
|
void TagsConfirmed (uint32_t msgID);
|
2014-10-17 04:12:46 +04:00
|
|
|
UnconfirmedTags * GenerateSessionTags ();
|
2014-01-18 19:34:57 +04:00
|
|
|
|
2013-11-25 03:10:27 +04:00
|
|
|
private:
|
|
|
|
|
2014-10-08 05:08:00 +04:00
|
|
|
GarlicDestination * m_Owner;
|
2016-12-17 05:23:04 +03:00
|
|
|
std::shared_ptr<const i2p::data::RoutingDestination> m_Destination;
|
2016-12-13 20:45:18 +03:00
|
|
|
|
2014-11-02 04:53:45 +03:00
|
|
|
i2p::crypto::AESKey m_SessionKey;
|
2014-10-17 04:12:46 +04:00
|
|
|
std::list<SessionTag> m_SessionTags;
|
|
|
|
int m_NumTags;
|
2016-11-18 22:50:29 +03:00
|
|
|
std::map<uint32_t, std::unique_ptr<UnconfirmedTags> > m_UnconfirmedTagsMsgs; // msgID->tags
|
2015-03-22 21:59:27 +03:00
|
|
|
|
|
|
|
LeaseSetUpdateStatus m_LeaseSetUpdateStatus;
|
|
|
|
uint32_t m_LeaseSetUpdateMsgID;
|
|
|
|
uint64_t m_LeaseSetSubmissionTime; // in milliseconds
|
|
|
|
|
2014-05-12 06:37:33 +04:00
|
|
|
i2p::crypto::CBCEncryption m_Encryption;
|
2016-01-24 06:53:19 +03:00
|
|
|
|
2016-02-11 06:51:08 +03:00
|
|
|
std::shared_ptr<GarlicRoutingPath> m_SharedRoutingPath;
|
|
|
|
|
2016-01-24 06:53:19 +03:00
|
|
|
public:
|
|
|
|
// for HTTP only
|
|
|
|
size_t GetNumOutgoingTags () const { return m_SessionTags.size (); };
|
2013-11-25 03:10:27 +04:00
|
|
|
};
|
2016-02-04 20:36:54 +03:00
|
|
|
//using GarlicRoutingSessionPtr = std::shared_ptr<GarlicRoutingSession>;
|
|
|
|
typedef std::shared_ptr<GarlicRoutingSession> GarlicRoutingSessionPtr; // TODO: replace to using after switch to 4.8
|
2016-01-25 21:34:04 +03:00
|
|
|
|
2014-10-08 00:18:13 +04:00
|
|
|
class GarlicDestination: public i2p::data::LocalDestination
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-03-12 00:48:54 +03:00
|
|
|
GarlicDestination ();
|
2014-10-08 00:18:13 +04:00
|
|
|
~GarlicDestination ();
|
|
|
|
|
2016-11-29 06:47:37 +03:00
|
|
|
void CleanUp ();
|
2017-03-12 00:48:54 +03:00
|
|
|
void SetNumTags (int numTags) { m_NumTags = numTags; };
|
2015-04-06 03:07:32 +03:00
|
|
|
std::shared_ptr<GarlicRoutingSession> GetRoutingSession (std::shared_ptr<const i2p::data::RoutingDestination> destination, bool attachLeaseSet);
|
2016-02-08 01:45:11 +03:00
|
|
|
void CleanupExpiredTags ();
|
2016-01-25 21:34:04 +03:00
|
|
|
void RemoveDeliveryStatusSession (uint32_t msgID);
|
2015-06-22 05:29:50 +03:00
|
|
|
std::shared_ptr<I2NPMessage> WrapMessage (std::shared_ptr<const i2p::data::RoutingDestination> destination,
|
|
|
|
std::shared_ptr<I2NPMessage> msg, bool attachLeaseSet = false);
|
2014-10-08 00:18:13 +04:00
|
|
|
|
|
|
|
void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag
|
2014-12-08 23:36:00 +03:00
|
|
|
virtual bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag); // from different thread
|
2016-01-25 21:34:04 +03:00
|
|
|
void DeliveryStatusSent (GarlicRoutingSessionPtr session, uint32_t msgID);
|
2014-10-08 05:47:32 +04:00
|
|
|
|
2015-06-16 17:14:14 +03:00
|
|
|
virtual void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg);
|
|
|
|
virtual void ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
|
2014-10-08 20:11:39 +04:00
|
|
|
virtual void SetLeaseSetUpdated ();
|
2014-10-12 05:34:18 +04:00
|
|
|
|
2016-05-25 22:10:28 +03:00
|
|
|
virtual std::shared_ptr<const i2p::data::LocalLeaseSet> GetLeaseSet () = 0; // TODO
|
2015-04-05 19:54:15 +03:00
|
|
|
virtual std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () const = 0;
|
2015-02-06 02:53:43 +03:00
|
|
|
virtual void HandleI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from) = 0;
|
2014-10-12 05:34:18 +04:00
|
|
|
|
2014-10-08 22:17:17 +04:00
|
|
|
protected:
|
|
|
|
|
2015-06-16 17:14:14 +03:00
|
|
|
void HandleGarlicMessage (std::shared_ptr<I2NPMessage> msg);
|
|
|
|
void HandleDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
|
2014-10-08 22:17:17 +04:00
|
|
|
|
2014-10-08 00:18:13 +04:00
|
|
|
private:
|
|
|
|
|
|
|
|
void HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr<i2p::crypto::CBCDecryption> decryption,
|
2015-02-06 02:53:43 +03: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-08 00:18:13 +04:00
|
|
|
|
|
|
|
private:
|
2017-03-12 00:48:54 +03:00
|
|
|
|
2017-03-14 19:03:51 +03:00
|
|
|
BN_CTX * m_Ctx; // incoming
|
2014-10-08 00:18:13 +04:00
|
|
|
// outgoing sessions
|
2016-01-24 04:52:21 +03:00
|
|
|
int m_NumTags;
|
2014-10-08 00:18:13 +04:00
|
|
|
std::mutex m_SessionsMutex;
|
2016-01-25 21:34:04 +03:00
|
|
|
std::map<i2p::data::IdentHash, GarlicRoutingSessionPtr> m_Sessions;
|
2014-10-08 00:18:13 +04:00
|
|
|
// incoming
|
2014-10-18 06:15:42 +04:00
|
|
|
std::map<SessionTag, std::shared_ptr<i2p::crypto::CBCDecryption>> m_Tags;
|
2014-10-08 05:47:32 +04:00
|
|
|
// DeliveryStatus
|
2016-11-18 19:16:55 +03:00
|
|
|
std::mutex m_DeliveryStatusSessionsMutex;
|
2016-01-25 21:34:04 +03:00
|
|
|
std::map<uint32_t, GarlicRoutingSessionPtr> m_DeliveryStatusSessions; // msgID -> session
|
2016-01-24 06:53:19 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// for HTTP only
|
|
|
|
size_t GetNumIncomingTags () const { return m_Tags.size (); }
|
|
|
|
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
|
2014-10-08 00:18:13 +04:00
|
|
|
};
|
2013-11-24 01:35:15 +04:00
|
|
|
}
|
2013-11-25 03:10:27 +04:00
|
|
|
}
|
2013-11-24 01:35:15 +04:00
|
|
|
|
|
|
|
#endif
|