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>
|
|
|
|
#include <string>
|
2014-03-13 01:13:54 +00:00
|
|
|
#include <thread>
|
2013-11-24 23:10:27 +00:00
|
|
|
#include <cryptopp/modes.h>
|
|
|
|
#include <cryptopp/aes.h>
|
|
|
|
#include <cryptopp/osrng.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 00:13:49 +00:00
|
|
|
#include "Tunnel.h"
|
2014-03-13 01:13:54 +00:00
|
|
|
#include "Queue.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-03-23 13:25:16 +00:00
|
|
|
const int TAGS_EXPIRATION_TIMEOUT = 900; // 15 minutes
|
2013-11-24 23:10:27 +00:00
|
|
|
class GarlicRoutingSession
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-02-20 23:42:55 +00:00
|
|
|
GarlicRoutingSession (const i2p::data::RoutingDestination& destination, int numTags);
|
2013-11-24 23:10:27 +00:00
|
|
|
~GarlicRoutingSession ();
|
2014-03-20 13:47:02 +00:00
|
|
|
I2NPMessage * WrapSingleMessage (I2NPMessage * msg, const I2NPMessage * leaseSet);
|
2014-01-18 15:34:57 +00:00
|
|
|
int GetNextTag () const { return m_NextTag; };
|
2014-01-17 13:12:57 +00:00
|
|
|
uint32_t GetFirstMsgID () const { return m_FirstMsgID; };
|
2013-11-24 23:10:27 +00:00
|
|
|
|
2014-01-17 13:12:57 +00:00
|
|
|
bool IsAcknowledged () const { return m_IsAcknowledged; };
|
|
|
|
void SetAcknowledged (bool acknowledged) { m_IsAcknowledged = acknowledged; };
|
|
|
|
|
2013-11-24 23:10:27 +00:00
|
|
|
private:
|
|
|
|
|
2014-03-20 13:47:02 +00:00
|
|
|
size_t CreateAESBlock (uint8_t * buf, const I2NPMessage * msg, const I2NPMessage * leaseSet);
|
|
|
|
size_t CreateGarlicPayload (uint8_t * payload, const I2NPMessage * msg, const I2NPMessage * leaseSet);
|
|
|
|
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);
|
2013-11-24 23:10:27 +00:00
|
|
|
|
2014-01-18 15:34:57 +00:00
|
|
|
void GenerateSessionTags ();
|
|
|
|
|
2013-11-24 23:10:27 +00:00
|
|
|
private:
|
|
|
|
|
2014-02-20 23:42:55 +00:00
|
|
|
const i2p::data::RoutingDestination& m_Destination;
|
2013-11-24 23:10:27 +00:00
|
|
|
uint8_t m_SessionKey[32];
|
2014-01-17 13:12:57 +00:00
|
|
|
uint32_t m_FirstMsgID; // first message ID
|
|
|
|
bool m_IsAcknowledged;
|
2013-11-24 23:10:27 +00:00
|
|
|
int m_NumTags, m_NextTag;
|
|
|
|
uint8_t * m_SessionTags; // m_NumTags*32 bytes
|
2014-03-22 12:43:38 +00:00
|
|
|
uint32_t m_TagsCreationTime; // seconds since epoch
|
2013-11-24 23:10:27 +00:00
|
|
|
|
|
|
|
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption m_Encryption;
|
|
|
|
CryptoPP::AutoSeededRandomPool m_Rnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
class GarlicRouting
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
GarlicRouting ();
|
|
|
|
~GarlicRouting ();
|
|
|
|
|
2014-03-13 01:13:54 +00:00
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
|
2014-03-13 00:13:49 +00:00
|
|
|
void HandleGarlicMessage (I2NPMessage * msg);
|
2014-01-17 13:12:57 +00:00
|
|
|
void HandleDeliveryStatusMessage (uint8_t * buf, size_t len);
|
2013-12-14 01:07:35 +00:00
|
|
|
|
2014-02-20 23:42:55 +00:00
|
|
|
I2NPMessage * WrapSingleMessage (const i2p::data::RoutingDestination& destination, I2NPMessage * msg);
|
|
|
|
I2NPMessage * WrapMessage (const i2p::data::RoutingDestination& destination,
|
2014-03-20 13:47:02 +00:00
|
|
|
I2NPMessage * msg, const I2NPMessage * leaseSet = nullptr);
|
2013-12-14 01:07:35 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-03-13 01:13:54 +00:00
|
|
|
void Run ();
|
|
|
|
void ProcessGarlicMessage (I2NPMessage * msg);
|
2014-01-10 03:51:33 +00:00
|
|
|
void HandleAESBlock (uint8_t * buf, size_t len, uint8_t * sessionKey);
|
2013-12-14 01:07:35 +00:00
|
|
|
void HandleGarlicPayload (uint8_t * buf, size_t len);
|
2013-11-24 23:10:27 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-03-13 01:13:54 +00:00
|
|
|
bool m_IsRunning;
|
|
|
|
std::thread * m_Thread;
|
|
|
|
i2p::util::Queue<I2NPMessage> m_Queue;
|
2013-12-14 01:07:35 +00:00
|
|
|
// outgoing sessions
|
2013-12-10 13:10:49 +00:00
|
|
|
std::map<i2p::data::IdentHash, GarlicRoutingSession *> m_Sessions;
|
2014-01-17 13:12:57 +00:00
|
|
|
std::map<uint32_t, GarlicRoutingSession *> m_CreatedSessions; // msgID -> session
|
2013-12-14 01:07:35 +00:00
|
|
|
// incoming session
|
2014-01-10 03:51:33 +00:00
|
|
|
std::map<std::string, std::string> m_SessionTags; // tag -> key
|
2013-12-14 01:07:35 +00:00
|
|
|
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption m_Decryption;
|
2013-11-24 23:10:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern GarlicRouting routing;
|
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
|