From 7aacae30eb3b2299de790ed84e122b0cd601d4da Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 25 Aug 2014 22:47:12 -0400 Subject: [PATCH] moved common code to LocalDestination --- Identity.h | 10 ++++++++-- RouterContext.cpp | 13 ++++--------- RouterContext.h | 6 ++---- RouterInfo.cpp | 6 +++--- RouterInfo.h | 2 +- Streaming.cpp | 5 ----- Streaming.h | 4 +--- TunnelPool.h | 2 +- 8 files changed, 20 insertions(+), 28 deletions(-) diff --git a/Identity.h b/Identity.h index dfef949a..9ff2bcfa 100644 --- a/Identity.h +++ b/Identity.h @@ -230,11 +230,17 @@ namespace data public: virtual ~LocalDestination() {}; - virtual const IdentityEx& GetIdentity () const = 0; + virtual const PrivateKeys& GetPrivateKeys () const = 0; virtual const uint8_t * GetEncryptionPrivateKey () const = 0; virtual const uint8_t * GetEncryptionPublicKey () const = 0; - virtual void Sign (const uint8_t * buf, int len, uint8_t * signature) const = 0; virtual void SetLeaseSetUpdated () = 0; + + const IdentityEx& GetIdentity () const { return GetPrivateKeys ().GetPublic (); }; + const IdentHash& GetIdentHash () const { return GetIdentity ().GetIdentHash (); }; + void Sign (const uint8_t * buf, int len, uint8_t * signature) const + { + GetPrivateKeys ().Sign (buf, len, signature); + }; }; } } diff --git a/RouterContext.cpp b/RouterContext.cpp index 3ecf1de4..2e475fcb 100644 --- a/RouterContext.cpp +++ b/RouterContext.cpp @@ -36,13 +36,13 @@ namespace i2p routerInfo.SetProperty ("netId", "2"); routerInfo.SetProperty ("router.version", I2P_VERSION); routerInfo.SetProperty ("start_uptime", "90m"); - routerInfo.CreateBuffer (); + routerInfo.CreateBuffer (m_Keys); m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ()); } void RouterContext::OverrideNTCPAddress (const char * host, int port) { - m_RouterInfo.CreateBuffer (); + m_RouterInfo.CreateBuffer (m_Keys); auto address = const_cast(m_RouterInfo.GetNTCPAddress ()); if (address) { @@ -50,7 +50,7 @@ namespace i2p address->port = port; } - m_RouterInfo.CreateBuffer (); + m_RouterInfo.CreateBuffer (m_Keys); Save (true); } @@ -58,12 +58,7 @@ namespace i2p { for (auto& address : m_RouterInfo.GetAddresses ()) address.host = boost::asio::ip::address::from_string (host); - m_RouterInfo.CreateBuffer (); - } - - void RouterContext::Sign (const uint8_t * buf, int len, uint8_t * signature) const - { - m_Keys.Sign(buf, len, signature); + m_RouterInfo.CreateBuffer (m_Keys); } bool RouterContext::Load () diff --git a/RouterContext.h b/RouterContext.h index ac4cb04a..d3e28a8f 100644 --- a/RouterContext.h +++ b/RouterContext.h @@ -20,7 +20,6 @@ namespace i2p i2p::data::RouterInfo& GetRouterInfo () { return m_RouterInfo; }; const uint8_t * GetPrivateKey () const { return m_Keys.GetPrivateKey (); }; - const uint8_t * GetSigningPrivateKey () const { return m_Keys.GetSigningPrivateKey (); }; const i2p::data::Identity& GetRouterIdentity () const { return m_RouterInfo.GetRouterIdentity (); }; const i2p::data::IdentHash& GetRouterIdentHash () const { return m_RouterInfo.GetIdentHash (); }; CryptoPP::RandomNumberGenerator& GetRandomNumberGenerator () { return m_Rnd; }; @@ -29,10 +28,9 @@ namespace i2p void UpdateAddress (const char * host); // called from SSU // implements LocalDestination - const i2p::data::IdentityEx& GetIdentity () const { return m_Keys.GetPublic (); }; - const uint8_t * GetEncryptionPrivateKey () const { return GetPrivateKey (); }; + const i2p::data::PrivateKeys& GetPrivateKeys () const { return m_Keys; }; + const uint8_t * GetEncryptionPrivateKey () const { return m_Keys.GetPrivateKey (); }; const uint8_t * GetEncryptionPublicKey () const { return GetIdentity ().GetStandardIdentity ().publicKey; }; - void Sign (const uint8_t * buf, int len, uint8_t * signature) const; void SetLeaseSetUpdated () {}; private: diff --git a/RouterInfo.cpp b/RouterInfo.cpp index 0f752063..c23495fe 100644 --- a/RouterInfo.cpp +++ b/RouterInfo.cpp @@ -363,7 +363,7 @@ namespace data return m_Buffer; } - void RouterInfo::CreateBuffer () + void RouterInfo::CreateBuffer (const PrivateKeys& privateKeys) { m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); // refresh timstamp std::stringstream s; @@ -373,8 +373,8 @@ namespace data m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE]; memcpy (m_Buffer, s.str ().c_str (), m_BufferLen); // signature - i2p::context.Sign ((uint8_t *)m_Buffer, m_BufferLen, (uint8_t *)m_Buffer + m_BufferLen); - m_BufferLen += 40; + privateKeys.Sign ((uint8_t *)m_Buffer, m_BufferLen, (uint8_t *)m_Buffer + m_BufferLen); + m_BufferLen += privateKeys.GetPublic ().GetSignatureLen (); } void RouterInfo::SaveToFile (const std::string& fullPath) diff --git a/RouterInfo.h b/RouterInfo.h index 84e271c8..f1505395 100644 --- a/RouterInfo.h +++ b/RouterInfo.h @@ -103,7 +103,7 @@ namespace data const uint8_t * LoadBuffer (); // load if necessary int GetBufferLen () const { return m_BufferLen; }; - void CreateBuffer (); + void CreateBuffer (const PrivateKeys& privateKeys); void UpdateRoutingKey (); bool IsUpdated () const { return m_IsUpdated; }; diff --git a/Streaming.cpp b/Streaming.cpp index 512f9684..efc64585 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -618,11 +618,6 @@ namespace stream if (m_IsPublic) i2p::data::netdb.PublishLeaseSet (m_LeaseSet, m_Pool); } - - void StreamingDestination::Sign (const uint8_t * buf, int len, uint8_t * signature) const - { - m_Keys.Sign(buf, len, signature); - } StreamingDestinations destinations; void StreamingDestinations::Start () diff --git a/Streaming.h b/Streaming.h index 2eb3ce32..544a1090 100644 --- a/Streaming.h +++ b/Streaming.h @@ -143,7 +143,6 @@ namespace stream StreamingDestination (boost::asio::io_service& service, const std::string& fullPath); ~StreamingDestination (); - const i2p::data::PrivateKeys& GetKeys () const { return m_Keys; }; const i2p::data::LeaseSet * GetLeaseSet (); i2p::tunnel::TunnelPool * GetTunnelPool () const { return m_Pool; }; @@ -153,10 +152,9 @@ namespace stream void HandleNextPacket (Packet * packet); // implements LocalDestination - const i2p::data::IdentityEx& GetIdentity () const { return m_Keys.GetPublic (); }; + const i2p::data::PrivateKeys& GetPrivateKeys () const { return m_Keys; }; 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; void SetLeaseSetUpdated (); private: diff --git a/TunnelPool.h b/TunnelPool.h index cf590ac5..daf9d892 100644 --- a/TunnelPool.h +++ b/TunnelPool.h @@ -40,7 +40,7 @@ namespace tunnel std::vector GetInboundTunnels (int num) const; OutboundTunnel * GetNextOutboundTunnel (OutboundTunnel * suggested = nullptr); InboundTunnel * GetNextInboundTunnel (InboundTunnel * suggested = nullptr); - const i2p::data::IdentHash& GetIdentHash () const { return m_LocalDestination.GetIdentity ().GetIdentHash (); }; + const i2p::data::IdentHash& GetIdentHash () const { return m_LocalDestination.GetIdentHash (); }; void TestTunnels (); void ProcessDeliveryStatus (I2NPMessage * msg);