2020-05-22 13:18:41 +00:00
|
|
|
/*
|
2021-08-23 16:29:55 +00:00
|
|
|
* Copyright (c) 2013-2021, The PurpleI2P Project
|
2020-05-22 13:18:41 +00:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2013-12-07 00:02:49 +00:00
|
|
|
#ifndef TUNNEL_CONFIG_H__
|
|
|
|
#define TUNNEL_CONFIG_H__
|
|
|
|
|
2014-01-09 00:30:47 +00:00
|
|
|
#include <vector>
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "Identity.h"
|
2013-12-07 00:02:49 +00:00
|
|
|
#include "RouterContext.h"
|
2020-10-29 01:53:11 +00:00
|
|
|
#include "Crypto.h"
|
2013-12-07 00:02:49 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace tunnel
|
|
|
|
{
|
2021-07-02 19:41:33 +00:00
|
|
|
struct TunnelHopConfig
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> ident;
|
|
|
|
i2p::data::IdentHash nextIdent;
|
2013-12-07 00:02:49 +00:00
|
|
|
uint32_t tunnelID, nextTunnelID;
|
|
|
|
uint8_t layerKey[32];
|
|
|
|
uint8_t ivKey[32];
|
|
|
|
uint8_t replyKey[32];
|
|
|
|
uint8_t replyIV[16];
|
2018-01-06 03:48:51 +00:00
|
|
|
bool isGateway, isEndpoint;
|
|
|
|
|
2013-12-07 00:02:49 +00:00
|
|
|
TunnelHopConfig * next, * prev;
|
2014-06-18 23:38:21 +00:00
|
|
|
int recordIndex; // record # in tunnel build message
|
2021-08-23 16:29:55 +00:00
|
|
|
|
2020-10-23 01:06:23 +00:00
|
|
|
TunnelHopConfig (std::shared_ptr<const i2p::data::IdentityEx> r);
|
2021-07-02 19:41:33 +00:00
|
|
|
virtual ~TunnelHopConfig () {};
|
2021-08-23 16:29:55 +00:00
|
|
|
|
2020-10-23 01:06:23 +00:00
|
|
|
void SetNextIdent (const i2p::data::IdentHash& ident);
|
|
|
|
void SetReplyHop (uint32_t replyTunnelID, const i2p::data::IdentHash& replyIdent);
|
|
|
|
void SetNext (TunnelHopConfig * n);
|
2021-08-23 16:29:55 +00:00
|
|
|
void SetPrev (TunnelHopConfig * p);
|
2020-10-22 22:34:15 +00:00
|
|
|
|
2021-07-08 20:39:38 +00:00
|
|
|
virtual uint8_t GetRetCode (const uint8_t * records) const = 0;
|
2021-07-08 23:00:25 +00:00
|
|
|
virtual void CreateBuildRequestRecord (uint8_t * records, uint32_t replyMsgID) = 0;
|
2021-07-08 20:39:38 +00:00
|
|
|
virtual bool DecryptBuildResponseRecord (uint8_t * records) const = 0;
|
2021-07-08 01:16:30 +00:00
|
|
|
virtual void DecryptRecord (uint8_t * records, int index) const; // AES
|
2021-07-12 23:40:40 +00:00
|
|
|
virtual uint64_t GetGarlicKey (uint8_t * key) const { return 0; }; // return tag
|
2021-07-02 19:41:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ElGamalTunnelHopConfig: public TunnelHopConfig
|
|
|
|
{
|
|
|
|
ElGamalTunnelHopConfig (std::shared_ptr<const i2p::data::IdentityEx> r):
|
|
|
|
TunnelHopConfig (r) {};
|
2021-08-23 16:29:55 +00:00
|
|
|
uint8_t GetRetCode (const uint8_t * records) const
|
|
|
|
{ return (records + recordIndex*TUNNEL_BUILD_RECORD_SIZE)[BUILD_RESPONSE_RECORD_RET_OFFSET]; };
|
|
|
|
void CreateBuildRequestRecord (uint8_t * records, uint32_t replyMsgID);
|
|
|
|
bool DecryptBuildResponseRecord (uint8_t * records) const;
|
|
|
|
};
|
2021-07-02 19:41:33 +00:00
|
|
|
|
|
|
|
struct ECIESTunnelHopConfig: public TunnelHopConfig, public i2p::crypto::NoiseSymmetricState
|
|
|
|
{
|
|
|
|
ECIESTunnelHopConfig (std::shared_ptr<const i2p::data::IdentityEx> r):
|
|
|
|
TunnelHopConfig (r) {};
|
2021-08-23 16:29:55 +00:00
|
|
|
void EncryptECIES (const uint8_t * clearText, size_t len, uint8_t * encrypted);
|
2021-07-08 01:16:30 +00:00
|
|
|
bool DecryptECIES (const uint8_t * key, const uint8_t * nonce, const uint8_t * encrypted, size_t len, uint8_t * clearText) const;
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
2021-08-23 16:29:55 +00:00
|
|
|
|
2021-07-02 19:41:33 +00:00
|
|
|
struct LongECIESTunnelHopConfig: public ECIESTunnelHopConfig
|
|
|
|
{
|
|
|
|
LongECIESTunnelHopConfig (std::shared_ptr<const i2p::data::IdentityEx> r):
|
|
|
|
ECIESTunnelHopConfig (r) {};
|
2021-08-23 16:29:55 +00:00
|
|
|
uint8_t GetRetCode (const uint8_t * records) const override
|
|
|
|
{ return (records + recordIndex*TUNNEL_BUILD_RECORD_SIZE)[ECIES_BUILD_RESPONSE_RECORD_RET_OFFSET]; };
|
|
|
|
void CreateBuildRequestRecord (uint8_t * records, uint32_t replyMsgID) override;
|
|
|
|
bool DecryptBuildResponseRecord (uint8_t * records) const override;
|
|
|
|
};
|
2021-07-03 02:06:24 +00:00
|
|
|
|
|
|
|
struct ShortECIESTunnelHopConfig: public ECIESTunnelHopConfig
|
|
|
|
{
|
|
|
|
ShortECIESTunnelHopConfig (std::shared_ptr<const i2p::data::IdentityEx> r):
|
|
|
|
ECIESTunnelHopConfig (r) {};
|
2021-08-23 16:29:55 +00:00
|
|
|
uint8_t GetRetCode (const uint8_t * records) const override
|
|
|
|
{ return (records + recordIndex*SHORT_TUNNEL_BUILD_RECORD_SIZE)[SHORT_RESPONSE_RECORD_RET_OFFSET]; };
|
|
|
|
void CreateBuildRequestRecord (uint8_t * records, uint32_t replyMsgID) override;
|
|
|
|
bool DecryptBuildResponseRecord (uint8_t * records) const override;
|
2021-07-08 01:16:30 +00:00
|
|
|
void DecryptRecord (uint8_t * records, int index) const override; // Chacha20
|
2021-08-23 16:29:55 +00:00
|
|
|
uint64_t GetGarlicKey (uint8_t * key) const override;
|
|
|
|
};
|
|
|
|
|
2016-03-02 21:12:02 +00:00
|
|
|
class TunnelConfig
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
2018-01-06 03:48:51 +00:00
|
|
|
public:
|
|
|
|
|
2021-08-23 16:29:55 +00:00
|
|
|
TunnelConfig (const std::vector<std::shared_ptr<const i2p::data::IdentityEx> >& peers,
|
2021-07-20 18:35:02 +00:00
|
|
|
bool isShort = false): // inbound
|
|
|
|
m_IsShort (isShort)
|
2015-11-03 14:15:49 +00:00
|
|
|
{
|
|
|
|
CreatePeers (peers);
|
|
|
|
m_LastHop->SetNextIdent (i2p::context.GetIdentHash ());
|
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
|
2021-07-08 01:16:30 +00:00
|
|
|
TunnelConfig (const std::vector<std::shared_ptr<const i2p::data::IdentityEx> >& peers,
|
2021-07-20 18:35:02 +00:00
|
|
|
uint32_t replyTunnelID, const i2p::data::IdentHash& replyIdent, bool isShort = false): // outbound
|
|
|
|
m_IsShort (isShort)
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
CreatePeers (peers);
|
|
|
|
m_FirstHop->isGateway = false;
|
|
|
|
m_LastHop->SetReplyHop (replyTunnelID, replyIdent);
|
2014-01-09 00:30:47 +00:00
|
|
|
}
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2013-12-07 00:02:49 +00:00
|
|
|
~TunnelConfig ()
|
|
|
|
{
|
|
|
|
TunnelHopConfig * hop = m_FirstHop;
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2013-12-07 00:02:49 +00:00
|
|
|
while (hop)
|
|
|
|
{
|
2014-01-21 20:10:49 +00:00
|
|
|
auto tmp = hop;
|
2013-12-07 00:02:49 +00:00
|
|
|
hop = hop->next;
|
2014-01-21 20:10:49 +00:00
|
|
|
delete tmp;
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
}
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2021-07-09 23:26:14 +00:00
|
|
|
bool IsShort () const { return m_IsShort; }
|
2021-08-23 16:29:55 +00:00
|
|
|
|
2013-12-07 00:02:49 +00:00
|
|
|
TunnelHopConfig * GetFirstHop () const
|
|
|
|
{
|
|
|
|
return m_FirstHop;
|
|
|
|
}
|
|
|
|
|
|
|
|
TunnelHopConfig * GetLastHop () const
|
|
|
|
{
|
|
|
|
return m_LastHop;
|
|
|
|
}
|
|
|
|
|
2014-06-25 23:28:33 +00:00
|
|
|
int GetNumHops () const
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
2014-06-25 23:28:33 +00:00
|
|
|
int num = 0;
|
2018-01-06 03:48:51 +00:00
|
|
|
TunnelHopConfig * hop = m_FirstHop;
|
2013-12-07 00:02:49 +00:00
|
|
|
while (hop)
|
|
|
|
{
|
|
|
|
num++;
|
|
|
|
hop = hop->next;
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2016-06-29 15:26:46 +00:00
|
|
|
bool IsEmpty () const
|
|
|
|
{
|
|
|
|
return !m_FirstHop;
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2016-06-29 15:26:46 +00:00
|
|
|
|
2016-03-02 21:12:02 +00:00
|
|
|
virtual bool IsInbound () const { return m_FirstHop->isGateway; }
|
2015-05-08 18:07:33 +00:00
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
virtual uint32_t GetTunnelID () const
|
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
if (!m_FirstHop) return 0;
|
2018-01-06 03:48:51 +00:00
|
|
|
return IsInbound () ? m_LastHop->nextTunnelID : m_FirstHop->tunnelID;
|
2015-11-03 14:15:49 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
virtual uint32_t GetNextTunnelID () const
|
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
if (!m_FirstHop) return 0;
|
2018-01-06 03:48:51 +00:00
|
|
|
return m_FirstHop->tunnelID;
|
2015-11-03 14:15:49 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
virtual const i2p::data::IdentHash& GetNextIdentHash () const
|
|
|
|
{
|
|
|
|
return m_FirstHop->ident->GetIdentHash ();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const i2p::data::IdentHash& GetLastIdentHash () const
|
|
|
|
{
|
|
|
|
return m_LastHop->ident->GetIdentHash ();
|
|
|
|
}
|
2015-11-03 14:15:49 +00:00
|
|
|
|
|
|
|
std::vector<std::shared_ptr<const i2p::data::IdentityEx> > GetPeers () const
|
2015-05-08 18:07:33 +00:00
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers;
|
2018-01-06 03:48:51 +00:00
|
|
|
TunnelHopConfig * hop = m_FirstHop;
|
2015-05-08 18:07:33 +00:00
|
|
|
while (hop)
|
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
peers.push_back (hop->ident);
|
2015-05-08 18:07:33 +00:00
|
|
|
hop = hop->next;
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2015-05-08 18:07:33 +00:00
|
|
|
return peers;
|
|
|
|
}
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2016-03-02 21:12:02 +00:00
|
|
|
protected:
|
2013-12-07 00:02:49 +00:00
|
|
|
|
|
|
|
// this constructor can't be called from outside
|
2021-07-20 18:35:02 +00:00
|
|
|
TunnelConfig (): m_FirstHop (nullptr), m_LastHop (nullptr), m_IsShort (false)
|
2013-12-07 00:02:49 +00:00
|
|
|
{
|
|
|
|
}
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2016-03-02 21:12:02 +00:00
|
|
|
private:
|
2015-11-03 14:15:49 +00:00
|
|
|
|
2021-07-08 01:16:30 +00:00
|
|
|
void CreatePeers (const std::vector<std::shared_ptr<const i2p::data::IdentityEx> >& peers)
|
2015-11-03 14:15:49 +00:00
|
|
|
{
|
|
|
|
TunnelHopConfig * prev = nullptr;
|
2016-08-08 22:53:37 +00:00
|
|
|
for (const auto& it: peers)
|
2015-11-03 14:15:49 +00:00
|
|
|
{
|
2021-07-02 19:41:33 +00:00
|
|
|
TunnelHopConfig * hop;
|
2021-07-09 23:26:14 +00:00
|
|
|
if (m_IsShort)
|
|
|
|
hop = new ShortECIESTunnelHopConfig (it);
|
2021-07-02 19:41:33 +00:00
|
|
|
else
|
2021-08-23 16:29:55 +00:00
|
|
|
{
|
2021-07-09 23:26:14 +00:00
|
|
|
if (it->GetCryptoKeyType () == i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD)
|
|
|
|
hop = new LongECIESTunnelHopConfig (it);
|
|
|
|
else
|
|
|
|
hop = new ElGamalTunnelHopConfig (it);
|
2021-08-23 16:29:55 +00:00
|
|
|
}
|
2015-11-03 14:15:49 +00:00
|
|
|
if (prev)
|
|
|
|
prev->SetNext (hop);
|
2018-01-06 03:48:51 +00:00
|
|
|
else
|
2015-11-03 14:15:49 +00:00
|
|
|
m_FirstHop = hop;
|
|
|
|
prev = hop;
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2015-11-03 14:15:49 +00:00
|
|
|
m_LastHop = prev;
|
|
|
|
}
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2013-12-07 00:02:49 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
TunnelHopConfig * m_FirstHop, * m_LastHop;
|
2021-07-20 18:35:02 +00:00
|
|
|
bool m_IsShort;
|
2016-03-02 21:12:02 +00:00
|
|
|
};
|
|
|
|
|
2016-03-03 03:41:53 +00:00
|
|
|
class ZeroHopsTunnelConfig: public TunnelConfig
|
2016-03-02 21:12:02 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-03-03 03:41:53 +00:00
|
|
|
ZeroHopsTunnelConfig () { RAND_bytes ((uint8_t *)&m_TunnelID, 4);};
|
2016-03-02 21:12:02 +00:00
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
bool IsInbound () const { return true; }; // TODO:
|
2016-03-02 21:12:02 +00:00
|
|
|
uint32_t GetTunnelID () const { return m_TunnelID; };
|
|
|
|
uint32_t GetNextTunnelID () const { return m_TunnelID; };
|
|
|
|
const i2p::data::IdentHash& GetNextIdentHash () const { return i2p::context.GetIdentHash (); };
|
|
|
|
const i2p::data::IdentHash& GetLastIdentHash () const { return i2p::context.GetIdentHash (); };
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2018-01-06 03:48:51 +00:00
|
|
|
|
|
|
|
uint32_t m_TunnelID;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2013-12-07 00:02:49 +00:00
|
|
|
|
|
|
|
#endif
|