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>
|
2013-11-25 03:10:27 +04:00
|
|
|
#include <cryptopp/osrng.h>
|
2014-05-12 06:37:33 +04:00
|
|
|
#include "aes.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
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack(1)
|
|
|
|
struct ElGamalBlock
|
|
|
|
{
|
|
|
|
uint8_t sessionKey[32];
|
|
|
|
uint8_t preIV[32];
|
|
|
|
uint8_t padding[158];
|
|
|
|
};
|
|
|
|
#pragma pack()
|
2013-11-25 03:10:27 +04:00
|
|
|
|
2014-10-17 05:11:02 +04:00
|
|
|
const int INCOMING_TAGS_EXPIRATION_TIMEOUT = 900; // 15 minutes
|
|
|
|
const int OUTGOING_TAGS_EXPIRATION_TIMEOUT = 720; // 12 minutes
|
|
|
|
|
2014-10-08 05:08:00 +04:00
|
|
|
typedef i2p::data::Tag<32> SessionTag;
|
|
|
|
class GarlicDestination;
|
2013-11-25 03:10:27 +04:00
|
|
|
class GarlicRoutingSession
|
|
|
|
{
|
2014-10-14 22:48:25 +04: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-25 03:10:27 +04:00
|
|
|
public:
|
|
|
|
|
2014-10-08 05:08:00 +04:00
|
|
|
GarlicRoutingSession (GarlicDestination * owner, const i2p::data::RoutingDestination * destination, int numTags);
|
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 ();
|
2014-10-08 20:11:39 +04:00
|
|
|
I2NPMessage * WrapSingleMessage (I2NPMessage * msg);
|
2014-10-14 22:48:25 +04:00
|
|
|
void TagsConfirmed (uint32_t msgID);
|
2014-10-08 20:11:39 +04:00
|
|
|
|
|
|
|
void SetLeaseSetUpdated () { m_LeaseSetUpdated = true; };
|
2014-01-17 17:12:57 +04:00
|
|
|
|
2013-11-25 03:10:27 +04:00
|
|
|
private:
|
|
|
|
|
2014-10-08 20:11:39 +04:00
|
|
|
size_t CreateAESBlock (uint8_t * buf, const I2NPMessage * msg);
|
2014-10-17 04:12:46 +04:00
|
|
|
size_t CreateGarlicPayload (uint8_t * payload, const I2NPMessage * msg, UnconfirmedTags * newTags);
|
2014-03-20 17:47:02 +04:00
|
|
|
size_t CreateGarlicClove (uint8_t * buf, const I2NPMessage * msg, bool isDestination);
|
2014-01-09 07:47:22 +04:00
|
|
|
size_t CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID);
|
2013-11-25 03:10:27 +04:00
|
|
|
|
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;
|
2014-07-28 19:54:34 +04:00
|
|
|
const i2p::data::RoutingDestination * m_Destination;
|
2013-11-25 03:10:27 +04:00
|
|
|
uint8_t m_SessionKey[32];
|
2014-10-17 04:12:46 +04:00
|
|
|
std::list<SessionTag> m_SessionTags;
|
|
|
|
int m_NumTags;
|
2014-10-14 22:48:25 +04:00
|
|
|
uint32_t m_TagsCreationTime; // seconds since epoch
|
|
|
|
std::map<uint32_t, UnconfirmedTags *> m_UnconfirmedTagsMsgs;
|
2014-10-08 20:11:39 +04:00
|
|
|
bool m_LeaseSetUpdated;
|
2014-08-15 22:34:58 +04:00
|
|
|
|
2014-05-12 06:37:33 +04:00
|
|
|
i2p::crypto::CBCEncryption m_Encryption;
|
2013-11-25 03:10:27 +04:00
|
|
|
CryptoPP::AutoSeededRandomPool m_Rnd;
|
|
|
|
};
|
|
|
|
|
2014-10-08 00:18:13 +04:00
|
|
|
class GarlicDestination: public i2p::data::LocalDestination
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
GarlicDestination () {};
|
|
|
|
~GarlicDestination ();
|
|
|
|
|
|
|
|
GarlicRoutingSession * GetRoutingSession (const i2p::data::RoutingDestination& destination, int numTags);
|
|
|
|
I2NPMessage * WrapMessage (const i2p::data::RoutingDestination& destination,
|
2014-10-08 20:11:39 +04:00
|
|
|
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-10-08 05:47:32 +04:00
|
|
|
void DeliveryStatusSent (GarlicRoutingSession * session, uint32_t msgID);
|
|
|
|
|
2014-10-08 22:17:17 +04:00
|
|
|
virtual void ProcessGarlicMessage (I2NPMessage * msg);
|
|
|
|
virtual void ProcessDeliveryStatusMessage (I2NPMessage * msg);
|
2014-10-08 20:11:39 +04:00
|
|
|
virtual void SetLeaseSetUpdated ();
|
2014-10-12 05:34:18 +04:00
|
|
|
|
2014-10-08 05:08:00 +04:00
|
|
|
virtual const i2p::data::LeaseSet * GetLeaseSet () = 0; // TODO
|
2014-10-14 01:45:07 +04:00
|
|
|
virtual void HandleI2NPMessage (const uint8_t * buf, size_t len, i2p::tunnel::InboundTunnel * from) = 0;
|
2014-10-12 05:34:18 +04:00
|
|
|
|
2014-10-08 22:17:17 +04:00
|
|
|
protected:
|
|
|
|
|
|
|
|
void HandleGarlicMessage (I2NPMessage * msg);
|
|
|
|
void HandleDeliveryStatusMessage (I2NPMessage * msg);
|
|
|
|
|
2014-10-08 00:18:13 +04:00
|
|
|
private:
|
|
|
|
|
|
|
|
void HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr<i2p::crypto::CBCDecryption> decryption,
|
|
|
|
i2p::tunnel::InboundTunnel * from);
|
|
|
|
void HandleGarlicPayload (uint8_t * buf, size_t len, i2p::tunnel::InboundTunnel * from);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// outgoing sessions
|
|
|
|
std::mutex m_SessionsMutex;
|
|
|
|
std::map<i2p::data::IdentHash, GarlicRoutingSession *> m_Sessions;
|
|
|
|
// incoming
|
|
|
|
std::map<SessionTag, std::shared_ptr<i2p::crypto::CBCDecryption>> m_Tags;
|
2014-10-08 05:47:32 +04:00
|
|
|
// DeliveryStatus
|
|
|
|
std::map<uint32_t, GarlicRoutingSession *> m_CreatedSessions; // msgID -> session
|
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
|