create LeaseSet from local tunnel pool

This commit is contained in:
orignal 2014-07-29 13:44:54 -04:00
parent 4236299879
commit ee2297c851
8 changed files with 72 additions and 25 deletions

View File

@ -153,9 +153,11 @@ namespace data
virtual ~LocalDestination() {};
virtual const IdentHash& GetIdentHash () const = 0;
virtual const Identity& GetIdentity () const = 0;
virtual const uint8_t * GetEncryptionPrivateKey () const = 0;
virtual const uint8_t * GetEncryptionPublicKey () const = 0;
virtual void UpdateLeaseSet () = 0; // LeaseSet must be updated
virtual void Sign (const uint8_t * buf, int len, uint8_t * signature) const = 0;
};
}
}

View File

@ -4,6 +4,7 @@
#include "Log.h"
#include "Timestamp.h"
#include "NetDb.h"
#include "TunnelPool.h"
#include "LeaseSet.h"
namespace i2p
@ -13,35 +14,60 @@ namespace data
LeaseSet::LeaseSet (const uint8_t * buf, int len)
{
ReadFromBuffer (buf, len);
memcpy (m_Buffer, buf, len);
m_BufferLen = len;
ReadFromBuffer ();
}
LeaseSet::LeaseSet (const i2p::tunnel::TunnelPool& pool)
{
m_BufferLen = 0;
// header
const i2p::data::LocalDestination& localDestination = pool.GetLocalDestination ();
LeaseSetHeader * header = (LeaseSetHeader *)m_Buffer;
header->destination = localDestination.GetIdentity ();
memcpy (header->encryptionKey, localDestination.GetEncryptionPublicKey (), 256);
memset (header->signingKey, 0, 128);
auto tunnels = pool.GetInboundTunnels (5); // 5 tunnels maximum
header->num = tunnels.size (); // num leases
m_BufferLen += sizeof (LeaseSetHeader);
// leases
for (auto it: tunnels)
{
Lease * lease = (Lease *)(m_Buffer + m_BufferLen);
memcpy (lease->tunnelGateway, it->GetNextIdentHash (), 32);
lease->tunnelID = htobe32 (it->GetNextTunnelID ());
uint64_t ts = it->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT - 60; // 1 minute before expiration
ts *= 1000; // in milliseconds
lease->endDate = htobe64 (ts);
m_BufferLen += sizeof (Lease);
}
// signature
localDestination.Sign (m_Buffer, m_BufferLen, m_Buffer + m_BufferLen);
m_BufferLen += 40;
LogPrint ("Local LeaseSet of ", tunnels.size (), " leases created");
ReadFromBuffer ();
}
void LeaseSet::Update (const uint8_t * buf, int len)
{
m_Leases.clear ();
ReadFromBuffer (buf, len);
memcpy (m_Buffer, buf, len);
m_BufferLen = len;
ReadFromBuffer ();
}
void LeaseSet::ReadFromBuffer (const uint8_t * buf, int len)
void LeaseSet::ReadFromBuffer ()
{
#pragma pack(1)
struct H
{
Identity destination;
uint8_t encryptionKey[256];
uint8_t signingKey[128];
uint8_t num;
};
#pragma pack ()
const H * header = (const H *)buf;
const LeaseSetHeader * header = (const LeaseSetHeader *)m_Buffer;
m_Identity = header->destination;
m_IdentHash = m_Identity.Hash();
memcpy (m_EncryptionKey, header->encryptionKey, 256);
LogPrint ("LeaseSet num=", (int)header->num);
// process leases
const uint8_t * leases = buf + sizeof (H);
const uint8_t * leases = m_Buffer + sizeof (LeaseSetHeader);
for (int i = 0; i < header->num; i++)
{
Lease lease = *(Lease *)leases;
@ -64,7 +90,7 @@ namespace data
pubKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
CryptoPP::Integer (m_Identity.signingKey, 128));
CryptoPP::DSA::Verifier verifier (pubKey);
if (!verifier.VerifyMessage (buf, leases - buf, leases, 40))
if (!verifier.VerifyMessage (m_Buffer, leases - m_Buffer, leases, 40))
LogPrint ("LeaseSet verification failed");
}

View File

@ -8,6 +8,12 @@
namespace i2p
{
namespace tunnel
{
class TunnelPool;
}
namespace data
{
@ -28,14 +34,24 @@ namespace data
}
};
struct LeaseSetHeader
{
Identity destination;
uint8_t encryptionKey[256];
uint8_t signingKey[128];
uint8_t num;
};
#pragma pack()
const int MAX_LS_BUFFER_SIZE = 2048;
class LeaseSet: public RoutingDestination
{
public:
LeaseSet (const uint8_t * buf, int len);
LeaseSet (const LeaseSet& ) = default;
LeaseSet (const i2p::tunnel::TunnelPool& pool);
LeaseSet& operator=(const LeaseSet& ) = default;
void Update (const uint8_t * buf, int len);
@ -51,7 +67,7 @@ namespace data
private:
void ReadFromBuffer (const uint8_t * buf, int len);
void ReadFromBuffer ();
private:
@ -59,6 +75,8 @@ namespace data
Identity m_Identity;
IdentHash m_IdentHash;
uint8_t m_EncryptionKey[256];
uint8_t m_Buffer[MAX_LS_BUFFER_SIZE];
size_t m_BufferLen;
};
}
}

View File

@ -64,10 +64,10 @@ namespace i2p
m_RouterInfo.CreateBuffer ();
}
void RouterContext::Sign (uint8_t * buf, int len, uint8_t * signature)
void RouterContext::Sign (const uint8_t * buf, int len, uint8_t * signature) const
{
CryptoPP::DSA::Signer signer (m_SigningPrivateKey);
signer.SignMessage (m_Rnd, buf, len, signature);
signer.SignMessage (i2p::context.GetRandomNumberGenerator (), buf, len, signature);
}
bool RouterContext::Load ()

View File

@ -23,8 +23,6 @@ namespace i2p
const uint8_t * GetSigningPrivateKey () const { return m_Keys.signingPrivateKey; };
const i2p::data::Identity& GetRouterIdentity () const { return m_RouterInfo.GetRouterIdentity (); };
CryptoPP::RandomNumberGenerator& GetRandomNumberGenerator () { return m_Rnd; };
void Sign (uint8_t * buf, int len, uint8_t * signature);
void OverrideNTCPAddress (const char * host, int port); // temporary
void UpdateAddress (const char * host); // called from SSU
@ -32,8 +30,10 @@ namespace i2p
// implements LocalDestination
void UpdateLeaseSet () {};
const i2p::data::IdentHash& GetIdentHash () const { return m_RouterInfo.GetIdentHash (); };
const i2p::data::Identity& GetIdentity () const { return GetRouterIdentity (); };
const uint8_t * GetEncryptionPrivateKey () const { return GetPrivateKey (); };
const uint8_t * GetEncryptionPublicKey () const { return m_Keys.publicKey; };
void Sign (const uint8_t * buf, int len, uint8_t * signature) const;
private:

View File

@ -463,7 +463,7 @@ namespace stream
return m;
}
void StreamingDestination::Sign (uint8_t * buf, int len, uint8_t * signature) const
void StreamingDestination::Sign (const uint8_t * buf, int len, uint8_t * signature) const
{
CryptoPP::DSA::Signer signer (m_SigningPrivateKey);
signer.SignMessage (i2p::context.GetRandomNumberGenerator (), buf, len, signature);

View File

@ -124,10 +124,8 @@ namespace stream
~StreamingDestination ();
const i2p::data::PrivateKeys& GetKeys () const { return m_Keys; };
const i2p::data::Identity& GetIdentity () const { return m_Keys.pub; };
const I2NPMessage * GetLeaseSet ();
i2p::tunnel::TunnelPool * GetTunnelPool () const { return m_Pool; };
void Sign (uint8_t * buf, int len, uint8_t * signature) const;
i2p::tunnel::TunnelPool * GetTunnelPool () const { return m_Pool; };
Stream * CreateNewStream (boost::asio::io_service& service, const i2p::data::LeaseSet& remote);
void DeleteStream (Stream * stream);
@ -136,8 +134,10 @@ namespace stream
// implements LocalDestination
void UpdateLeaseSet ();
const i2p::data::IdentHash& GetIdentHash () const { return m_IdentHash; };
const i2p::data::Identity& GetIdentity () const { return m_Keys.pub; };
const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; };
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionPublicKey; };
void Sign (const uint8_t * buf, int len, uint8_t * signature) const;
private:

View File

@ -28,6 +28,7 @@ namespace tunnel
const uint8_t * GetEncryptionPrivateKey () const { return m_LocalDestination.GetEncryptionPrivateKey (); };
const uint8_t * GetEncryptionPublicKey () const { return m_LocalDestination.GetEncryptionPublicKey (); };
const i2p::data::LocalDestination& GetLocalDestination () const { return m_LocalDestination; };
bool IsExploratory () const { return m_LocalDestination.GetIdentHash () == i2p::context.GetIdentHash (); };
void CreateTunnels ();