Skip to content

Commit

Permalink
create LeaseSet from local tunnel pool
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jul 29, 2014
1 parent 4236299 commit ee2297c
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 25 deletions.
2 changes: 2 additions & 0 deletions Identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
}
Expand Down
58 changes: 42 additions & 16 deletions LeaseSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Log.h"
#include "Timestamp.h"
#include "NetDb.h"
#include "TunnelPool.h"
#include "LeaseSet.h"

namespace i2p
Expand All @@ -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;
Expand All @@ -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");
}

Expand Down
20 changes: 19 additions & 1 deletion LeaseSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

namespace i2p
{

namespace tunnel
{
class TunnelPool;
}

namespace data
{

Expand All @@ -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);

Expand All @@ -51,14 +67,16 @@ namespace data

private:

void ReadFromBuffer (const uint8_t * buf, int len);
void ReadFromBuffer ();

private:

std::vector<Lease> m_Leases;
Identity m_Identity;
IdentHash m_IdentHash;
uint8_t m_EncryptionKey[256];
uint8_t m_Buffer[MAX_LS_BUFFER_SIZE];
size_t m_BufferLen;
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions RouterContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
4 changes: 2 additions & 2 deletions RouterContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ 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

// 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:

Expand Down
2 changes: 1 addition & 1 deletion Streaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions Streaming.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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:

Expand Down
1 change: 1 addition & 0 deletions TunnelPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down

0 comments on commit ee2297c

Please sign in to comment.