diff --git a/Destination.cpp b/Destination.cpp index 40c8768e..bb67b601 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -130,10 +130,6 @@ namespace client if (!m_IsRunning) { m_IsRunning = true; - if (m_IsPublic) - PersistTemporaryKeys (); - else - i2p::crypto::GenerateElGamalKeyPair(m_EncryptionPrivateKey, m_EncryptionPublicKey); m_Pool->SetLocalDestination (shared_from_this ()); m_Pool->SetActive (true); m_Thread = new std::thread (std::bind (&LeaseSetDestination::Run, shared_from_this ())); @@ -204,14 +200,21 @@ namespace client return m_LeaseSet; } + void LeaseSetDestination::SetLeaseSet (i2p::data::LocalLeaseSet * newLeaseSet) + { + m_LeaseSet.reset (newLeaseSet); + if (m_IsPublic) + { + m_PublishVerificationTimer.cancel (); + Publish (); + } + } + void LeaseSetDestination::UpdateLeaseSet () { int numTunnels = m_Pool->GetNumInboundTunnels () + 2; // 2 backup tunnels if (numTunnels > i2p::data::MAX_NUM_LEASES) numTunnels = i2p::data::MAX_NUM_LEASES; // 16 tunnels maximum - auto leaseSet = new i2p::data::LocalLeaseSet (GetIdentity (), GetEncryptionPublicKey (), - m_Pool->GetInboundTunnels (numTunnels)); - Sign (leaseSet->GetBuffer (), leaseSet->GetBufferLen () - leaseSet->GetSignatureLen (), leaseSet->GetSignature ()); // TODO - m_LeaseSet.reset (leaseSet); + CreateNewLeaseSet (m_Pool->GetInboundTunnels (numTunnels)); } bool LeaseSetDestination::SubmitSessionKey (const uint8_t * key, const uint8_t * tag) @@ -391,11 +394,6 @@ namespace client { i2p::garlic::GarlicDestination::SetLeaseSetUpdated (); UpdateLeaseSet (); - if (m_IsPublic) - { - m_PublishVerificationTimer.cancel (); - Publish (); - } } void LeaseSetDestination::Publish () @@ -642,36 +640,16 @@ namespace client else it++; } - } - - void LeaseSetDestination::PersistTemporaryKeys () - { - std::string ident = GetIdentHash().ToBase32(); - std::string path = i2p::fs::DataDirPath("destinations", (ident + ".dat")); - std::ifstream f(path, std::ifstream::binary); - - if (f) { - f.read ((char *)m_EncryptionPublicKey, 256); - f.read ((char *)m_EncryptionPrivateKey, 256); - return; - } - - LogPrint (eLogInfo, "Destination: Creating new temporary keys for address ", ident, ".b32.i2p"); - i2p::crypto::GenerateElGamalKeyPair(m_EncryptionPrivateKey, m_EncryptionPublicKey); - - std::ofstream f1 (path, std::ofstream::binary | std::ofstream::out); - if (f1) { - f1.write ((char *)m_EncryptionPublicKey, 256); - f1.write ((char *)m_EncryptionPrivateKey, 256); - return; - } - LogPrint(eLogError, "Destinations: Can't save keys to ", path); } ClientDestination::ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map * params): LeaseSetDestination (isPublic, params), m_Keys (keys), m_DatagramDestination (nullptr) { + if (isPublic) + PersistTemporaryKeys (); + else + i2p::crypto::GenerateElGamalKeyPair(m_EncryptionPrivateKey, m_EncryptionPublicKey); if (isPublic) LogPrint (eLogInfo, "Destination: Local address ", GetIdentHash().ToBase32 (), " created"); } @@ -840,5 +818,37 @@ namespace client ret.push_back (it1.second); return ret; } + + void ClientDestination::PersistTemporaryKeys () + { + std::string ident = GetIdentHash().ToBase32(); + std::string path = i2p::fs::DataDirPath("destinations", (ident + ".dat")); + std::ifstream f(path, std::ifstream::binary); + + if (f) { + f.read ((char *)m_EncryptionPublicKey, 256); + f.read ((char *)m_EncryptionPrivateKey, 256); + return; + } + + LogPrint (eLogInfo, "Destination: Creating new temporary keys for address ", ident, ".b32.i2p"); + i2p::crypto::GenerateElGamalKeyPair(m_EncryptionPrivateKey, m_EncryptionPublicKey); + + std::ofstream f1 (path, std::ofstream::binary | std::ofstream::out); + if (f1) { + f1.write ((char *)m_EncryptionPublicKey, 256); + f1.write ((char *)m_EncryptionPrivateKey, 256); + return; + } + LogPrint(eLogError, "Destinations: Can't save keys to ", path); + } + + void ClientDestination::CreateNewLeaseSet (std::vector > tunnels) + { + auto leaseSet = new i2p::data::LocalLeaseSet (GetIdentity (), m_EncryptionPublicKey, tunnels); + // sign + Sign (leaseSet->GetBuffer (), leaseSet->GetBufferLen () - leaseSet->GetSignatureLen (), leaseSet->GetSignature ()); // TODO + SetLeaseSet (leaseSet); + } } } diff --git a/Destination.h b/Destination.h index e2531699..56c83fb4 100644 --- a/Destination.h +++ b/Destination.h @@ -81,10 +81,6 @@ namespace client bool RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete = nullptr); void CancelDestinationRequest (const i2p::data::IdentHash& dest); - // implements LocalDestination - const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; }; - const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionPublicKey; }; - // implements GarlicDestination std::shared_ptr GetLeaseSet (); std::shared_ptr GetTunnelPool () const { return m_Pool; } @@ -98,8 +94,10 @@ namespace client protected: + void SetLeaseSet (i2p::data::LocalLeaseSet * newLeaseSet); // I2CP virtual void HandleDataMessage (const uint8_t * buf, size_t len) = 0; + virtual void CreateNewLeaseSet (std::vector > tunnels) = 0; private: @@ -117,13 +115,9 @@ namespace client void HandleRequestTimoutTimer (const boost::system::error_code& ecode, const i2p::data::IdentHash& dest); void HandleCleanupTimer (const boost::system::error_code& ecode); void CleanupRemoteLeaseSets (); - - void PersistTemporaryKeys (); private: - uint8_t m_EncryptionPublicKey[256], m_EncryptionPrivateKey[256]; - volatile bool m_IsRunning; std::thread * m_Thread; boost::asio::io_service m_Service; @@ -156,7 +150,8 @@ namespace client bool Stop (); const i2p::data::PrivateKeys& GetPrivateKeys () const { return m_Keys; }; - + void Sign (const uint8_t * buf, int len, uint8_t * signature) const { m_Keys.Sign (buf, len, signature); }; + // streaming std::shared_ptr CreateStreamingDestination (int port, bool gzip = true); // additional std::shared_ptr GetStreamingDestination (int port = 0) const; @@ -166,28 +161,32 @@ namespace client void AcceptStreams (const i2p::stream::StreamingDestination::Acceptor& acceptor); void StopAcceptingStreams (); bool IsAcceptingStreams () const; - + // datagram i2p::datagram::DatagramDestination * GetDatagramDestination () const { return m_DatagramDestination; }; i2p::datagram::DatagramDestination * CreateDatagramDestination (); - // implements LocalDestination - std::shared_ptr GetIdentity () const { return m_Keys.GetPublic (); }; - void Sign (const uint8_t * buf, int len, uint8_t * signature) const { m_Keys.Sign (buf, len, signature); }; + // implements LocalDestination + const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; }; + const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionPublicKey; }; + std::shared_ptr GetIdentity () const { return m_Keys.GetPublic (); }; protected: // I2CP void HandleDataMessage (const uint8_t * buf, size_t len); + void CreateNewLeaseSet (std::vector > tunnels); private: std::shared_ptr GetSharedFromThis () - { return std::static_pointer_cast(shared_from_this ()); } + { return std::static_pointer_cast(shared_from_this ()); } + void PersistTemporaryKeys (); private: i2p::data::PrivateKeys m_Keys; + uint8_t m_EncryptionPublicKey[256], m_EncryptionPrivateKey[256]; std::shared_ptr m_StreamingDestination; // default std::map > m_StreamingDestinationsByPorts; diff --git a/I2CP.cpp b/I2CP.cpp index fbd3e51b..ee329d37 100644 --- a/I2CP.cpp +++ b/I2CP.cpp @@ -152,6 +152,7 @@ namespace client // echo vesrion back PutString (payload + 8, l - 8, version); SendI2CPMessage (I2CP_SET_DATE_MESSAGE, payload, l); + delete[] payload; } void I2CPSession::CreateSessionMessageHandler (const uint8_t * buf, size_t len) diff --git a/I2CP.h b/I2CP.h index c495d34e..f677b9ee 100644 --- a/I2CP.h +++ b/I2CP.h @@ -32,16 +32,19 @@ namespace client protected: // implements LocalDestination + const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; }; + const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionPublicKey; }; std::shared_ptr GetIdentity () const { return m_Identity; }; - void Sign (const uint8_t * buf, int len, uint8_t * signature) const { /* TODO */}; // I2CP - void HandleDataMessage (const uint8_t * buf, size_t len) {}; + void HandleDataMessage (const uint8_t * buf, size_t len) { /* TODO */ }; + void CreateNewLeaseSet (std::vector > tunnels) { /* TODO */ }; private: I2CPSession& m_Owner; std::shared_ptr m_Identity; + uint8_t m_EncryptionPublicKey[256], m_EncryptionPrivateKey[256]; }; class I2CPServer; diff --git a/Identity.h b/Identity.h index 541a7801..2a60ddd3 100644 --- a/Identity.h +++ b/Identity.h @@ -181,7 +181,6 @@ namespace data virtual const uint8_t * GetEncryptionPrivateKey () const = 0; virtual const uint8_t * GetEncryptionPublicKey () const = 0; virtual std::shared_ptr GetIdentity () const = 0; - virtual void Sign (const uint8_t * buf, int len, uint8_t * signature) const = 0; const IdentHash& GetIdentHash () const { return GetIdentity ()->GetIdentHash (); }; };