From ee2297c8515580786277146f450ef25a714a94c9 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 29 Jul 2014 13:44:54 -0400 Subject: [PATCH] create LeaseSet from local tunnel pool --- Identity.h | 2 ++ LeaseSet.cpp | 58 ++++++++++++++++++++++++++++++++++------------- LeaseSet.h | 20 +++++++++++++++- RouterContext.cpp | 4 ++-- RouterContext.h | 4 ++-- Streaming.cpp | 2 +- Streaming.h | 6 ++--- TunnelPool.h | 1 + 8 files changed, 72 insertions(+), 25 deletions(-) diff --git a/Identity.h b/Identity.h index 6345a848..62995a27 100644 --- a/Identity.h +++ b/Identity.h @@ -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; }; } } diff --git a/LeaseSet.cpp b/LeaseSet.cpp index 8e75ff72..8928e761 100644 --- a/LeaseSet.cpp +++ b/LeaseSet.cpp @@ -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"); } diff --git a/LeaseSet.h b/LeaseSet.h index ea1b3b11..027c0157 100644 --- a/LeaseSet.h +++ b/LeaseSet.h @@ -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; }; } } diff --git a/RouterContext.cpp b/RouterContext.cpp index 31d03aa6..f9f684ef 100644 --- a/RouterContext.cpp +++ b/RouterContext.cpp @@ -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 () diff --git a/RouterContext.h b/RouterContext.h index bd5acd4e..e1838e86 100644 --- a/RouterContext.h +++ b/RouterContext.h @@ -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: diff --git a/Streaming.cpp b/Streaming.cpp index 1a3ea927..7f239331 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -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); diff --git a/Streaming.h b/Streaming.h index 89e378b2..4542155d 100644 --- a/Streaming.h +++ b/Streaming.h @@ -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: diff --git a/TunnelPool.h b/TunnelPool.h index 3132769d..21b8f6c5 100644 --- a/TunnelPool.h +++ b/TunnelPool.h @@ -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 ();