From f811b19cf1c74cd89803791a29b66eb75cf44940 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 6 Aug 2014 11:09:06 -0400 Subject: [PATCH] store and check remote Identity --- Identity.cpp | 15 +++++++++++---- Identity.h | 16 +++++++++++++++- Streaming.cpp | 31 ++++++++++++++++++++++--------- Streaming.h | 1 + 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/Identity.cpp b/Identity.cpp index 9a67f89b..e63cdb35 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -16,20 +16,27 @@ namespace data { // copy public and signing keys together memcpy (publicKey, keys.publicKey, sizeof (publicKey) + sizeof (signingKey)); - memset (certificate, 0, sizeof (certificate)); + memset (&certificate, 0, sizeof (certificate)); return *this; } bool Identity::FromBase64 (const std::string& s) { - size_t count = Base64ToByteStream (s.c_str(), s.length(), publicKey, sizeof (Identity)); - return count == sizeof(Identity); + size_t count = Base64ToByteStream (s.c_str(), s.length(), publicKey, DEFAULT_IDENTITY_SIZE); + return count == DEFAULT_IDENTITY_SIZE; + } + + size_t Identity::FromBuffer (const uint8_t * buf, size_t len) + { + memcpy (publicKey, buf, DEFAULT_IDENTITY_SIZE); + // TODO: process certificate + return DEFAULT_IDENTITY_SIZE; } IdentHash Identity::Hash() const { IdentHash hash; - CryptoPP::SHA256().CalculateDigest(hash, publicKey, sizeof (Identity)); + CryptoPP::SHA256().CalculateDigest(hash, publicKey, DEFAULT_IDENTITY_SIZE); return hash; } diff --git a/Identity.h b/Identity.h index a5de6173..ee76892b 100644 --- a/Identity.h +++ b/Identity.h @@ -71,14 +71,28 @@ namespace data uint8_t signingKey[128]; }; + + const uint8_t CERTIFICATE_TYPE_NULL = 0; + const uint8_t CERTIFICATE_TYPE_HASHCASH = 1; + const uint8_t CERTIFICATE_TYPE_HIDDEN = 2; + const uint8_t CERTIFICATE_TYPE_SIGNED = 3; + const uint8_t CERTIFICATE_TYPE_MULTIPLE = 4; + const uint8_t CERTIFICATE_TYPE_KEY = 5; + + const size_t DEFAULT_IDENTITY_SIZE = 387; struct Identity { uint8_t publicKey[256]; uint8_t signingKey[128]; - uint8_t certificate[3]; + struct + { + uint8_t type; + uint16_t length; + } certificate; Identity& operator=(const Keys& keys); bool FromBase64(const std::string& ); + size_t FromBuffer (const uint8_t * buf, size_t len); IdentHash Hash() const; }; diff --git a/Streaming.cpp b/Streaming.cpp index 9cd47288..af23d2c9 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -118,14 +118,17 @@ namespace stream if (flags & PACKET_FLAG_FROM_INCLUDED) { LogPrint ("From identity"); - if (!m_RemoteLeaseSet) + optionData += m_RemoteIdentity.FromBuffer (optionData, packet->GetOptionSize ()); + if (m_RemoteLeaseSet) { - i2p::data::Identity * identity = (i2p::data::Identity *)optionData; - LogPrint ("Incoming stream from ", identity->Hash ().ToBase64 ()); - m_RemoteLeaseSet = i2p::data::netdb.FindLeaseSet (identity->Hash ()); - if (!m_RemoteLeaseSet) - LogPrint ("LeaseSet ", identity->Hash ().ToBase64 (), " not found"); + if (m_RemoteIdentity.Hash () != m_RemoteLeaseSet->GetIdentHash ()) // check recieved identity + { + LogPrint ("Unexpected identity ", m_RemoteIdentity.Hash ().ToBase64 (), " ", m_RemoteLeaseSet->GetIdentHash ().ToBase64 (), " expected"); + m_RemoteLeaseSet = nullptr; + } } + else + LogPrint ("Incoming stream from ", m_RemoteIdentity.Hash ().ToBase64 ()); optionData += sizeof (i2p::data::Identity); } @@ -303,11 +306,15 @@ namespace stream } bool Stream::SendPacket (const uint8_t * buf, size_t len) - { + { if (!m_RemoteLeaseSet) { - LogPrint ("Can't send packet. Missing remote LeaseSet"); - return false; + UpdateCurrentRemoteLease (); + if (!m_RemoteLeaseSet) + { + LogPrint ("Can't send packet. Missing remote LeaseSet"); + return false; + } } I2NPMessage * leaseSet = nullptr; @@ -347,6 +354,12 @@ namespace stream void Stream::UpdateCurrentRemoteLease () { + if (!m_RemoteLeaseSet) + { + m_RemoteLeaseSet = i2p::data::netdb.FindLeaseSet (m_RemoteIdentity.Hash ()); + if (!m_RemoteLeaseSet) + LogPrint ("LeaseSet ", m_RemoteIdentity.Hash ().ToBase64 (), " not found"); + } if (m_RemoteLeaseSet) { auto leases = m_RemoteLeaseSet->GetNonExpiredLeases (); diff --git a/Streaming.h b/Streaming.h index a348c641..84611a2a 100644 --- a/Streaming.h +++ b/Streaming.h @@ -112,6 +112,7 @@ namespace stream uint32_t m_SendStreamID, m_RecvStreamID, m_SequenceNumber, m_LastReceivedSequenceNumber; bool m_IsOpen, m_IsOutgoing, m_LeaseSetUpdated; StreamingDestination * m_LocalDestination; + i2p::data::Identity m_RemoteIdentity; const i2p::data::LeaseSet * m_RemoteLeaseSet; i2p::data::Lease m_CurrentRemoteLease; std::queue m_ReceiveQueue;