i2pd/Garlic.h

170 lines
5.4 KiB
C
Raw Normal View History

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>
#include <thread>
2014-08-25 17:07:14 +00:00
#include <mutex>
#include <memory>
2013-11-24 23:10:27 +00:00
#include <cryptopp/osrng.h>
2014-05-12 02:37:33 +00:00
#include "aes.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
2013-11-24 23:10:27 +00:00
{
namespace garlic
2013-11-23 21:35:15 +00:00
{
2013-11-24 23:10:27 +00:00
2013-11-23 21:35:15 +00:00
enum GarlicDeliveryType
{
eGarlicDeliveryTypeLocal = 0,
eGarlicDeliveryTypeDestination = 1,
eGarlicDeliveryTypeRouter = 2,
eGarlicDeliveryTypeTunnel = 3
};
#pragma pack(1)
struct ElGamalBlock
{
uint8_t sessionKey[32];
uint8_t preIV[32];
uint8_t padding[158];
};
#pragma pack()
2013-11-24 23:10:27 +00:00
2014-10-18 02:15:42 +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
2015-03-22 18:59:27 +00:00
const int LEASET_CONFIRMATION_TIMEOUT = 4000; // in milliseconds
2014-10-17 01:11:02 +00:00
2014-10-17 15:26:57 +00: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
};
2014-10-08 01:08:00 +00:00
class GarlicDestination;
2015-01-22 20:31:34 +00:00
class GarlicRoutingSession: public std::enable_shared_from_this<GarlicRoutingSession>
2013-11-24 23:10:27 +00:00
{
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
};
2014-10-14 18:48:25 +00:00
struct UnconfirmedTags
{
UnconfirmedTags (int n): numTags (n), tagsCreationTime (0) { sessionTags = new SessionTag[numTags]; };
~UnconfirmedTags () { delete[] sessionTags; };
int numTags;
SessionTag * sessionTags;
uint32_t tagsCreationTime;
};
2013-11-24 23:10:27 +00:00
public:
2015-04-06 00:07:32 +00:00
GarlicRoutingSession (GarlicDestination * owner, std::shared_ptr<const i2p::data::RoutingDestination> destination,
int numTags, bool attachLeaseSet);
2014-07-28 16:02:50 +00:00
GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag); // one time encryption
2013-11-24 23:10:27 +00:00
~GarlicRoutingSession ();
I2NPMessage * WrapSingleMessage (I2NPMessage * msg);
2015-03-22 18:59:27 +00:00
void MessageConfirmed (uint32_t msgID);
2015-01-23 15:07:11 +00:00
bool CleanupExpiredTags (); // returns true if something left
2015-04-06 00:07:32 +00:00
void SetLeaseSetUpdated ()
{
if (m_LeaseSetUpdateStatus != eLeaseSetDoNotSend) m_LeaseSetUpdateStatus = eLeaseSetUpdated;
};
2014-01-17 13:12:57 +00:00
2013-11-24 23:10:27 +00:00
private:
size_t CreateAESBlock (uint8_t * buf, const I2NPMessage * msg);
2014-10-17 00:12:46 +00:00
size_t CreateGarlicPayload (uint8_t * payload, const I2NPMessage * msg, UnconfirmedTags * newTags);
2014-03-20 13:47:02 +00:00
size_t CreateGarlicClove (uint8_t * buf, const I2NPMessage * msg, bool isDestination);
2014-01-09 03:47:22 +00:00
size_t CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID);
2015-03-22 18:59:27 +00:00
void TagsConfirmed (uint32_t msgID);
2014-10-17 00:12:46 +00:00
UnconfirmedTags * GenerateSessionTags ();
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;
2015-01-29 02:37:08 +00:00
std::shared_ptr<const i2p::data::RoutingDestination> m_Destination;
2014-11-02 01:53:45 +00:00
i2p::crypto::AESKey m_SessionKey;
2014-10-17 00:12:46 +00:00
std::list<SessionTag> m_SessionTags;
int m_NumTags;
2014-10-14 18:48:25 +00:00
std::map<uint32_t, UnconfirmedTags *> m_UnconfirmedTagsMsgs;
2015-03-22 18:59:27 +00:00
LeaseSetUpdateStatus m_LeaseSetUpdateStatus;
uint32_t m_LeaseSetUpdateMsgID;
uint64_t m_LeaseSetSubmissionTime; // in milliseconds
2014-05-12 02:37:33 +00:00
i2p::crypto::CBCEncryption m_Encryption;
2013-11-24 23:10:27 +00:00
CryptoPP::AutoSeededRandomPool m_Rnd;
};
class GarlicDestination: public i2p::data::LocalDestination
{
public:
2014-10-18 02:15:42 +00:00
GarlicDestination (): m_LastTagsCleanupTime (0) {};
~GarlicDestination ();
2015-04-06 00:07:32 +00:00
std::shared_ptr<GarlicRoutingSession> GetRoutingSession (std::shared_ptr<const i2p::data::RoutingDestination> destination, bool attachLeaseSet);
2015-01-23 15:07:11 +00:00
void CleanupRoutingSessions ();
void RemoveCreatedSession (uint32_t msgID);
2015-01-29 02:37:08 +00:00
I2NPMessage * WrapMessage (std::shared_ptr<const i2p::data::RoutingDestination> destination,
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
2015-01-22 20:31:34 +00:00
void DeliveryStatusSent (std::shared_ptr<GarlicRoutingSession> session, uint32_t msgID);
2014-10-08 01:47:32 +00:00
2015-06-16 14:14:14 +00:00
virtual void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg);
virtual void ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
virtual void SetLeaseSetUpdated ();
2015-04-07 19:02:00 +00:00
virtual std::shared_ptr<const i2p::data::LeaseSet> GetLeaseSet () = 0; // TODO
virtual std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () const = 0;
2015-02-05 23:53:43 +00:00
virtual void HandleI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from) = 0;
protected:
2015-06-16 14:14:14 +00:00
void HandleGarlicMessage (std::shared_ptr<I2NPMessage> msg);
void HandleDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
private:
void HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr<i2p::crypto::CBCDecryption> 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
std::mutex m_SessionsMutex;
2015-01-22 20:31:34 +00:00
std::map<i2p::data::IdentHash, std::shared_ptr<GarlicRoutingSession> > m_Sessions;
// incoming
2014-10-18 02:15:42 +00:00
std::map<SessionTag, std::shared_ptr<i2p::crypto::CBCDecryption>> m_Tags;
uint32_t m_LastTagsCleanupTime;
2014-10-08 01:47:32 +00:00
// DeliveryStatus
2015-01-22 20:31:34 +00:00
std::map<uint32_t, std::shared_ptr<GarlicRoutingSession> > m_CreatedSessions; // msgID -> session
};
2013-11-23 21:35:15 +00:00
}
2013-11-24 23:10:27 +00:00
}
2013-11-23 21:35:15 +00:00
#endif