diff --git a/libi2pd/LeaseSet.cpp b/libi2pd/LeaseSet.cpp index 1dc1c64f..3cf06ec0 100644 --- a/libi2pd/LeaseSet.cpp +++ b/libi2pd/LeaseSet.cpp @@ -13,12 +13,13 @@ namespace data { LeaseSet::LeaseSet (bool storeLeases): - m_IsValid (false), m_StoreLeases (storeLeases), m_ExpirationTime (0), m_Buffer (nullptr), m_BufferLen (0) + m_IsValid (false), m_StoreLeases (storeLeases), m_ExpirationTime (0), m_EncryptionKey (nullptr), + m_Buffer (nullptr), m_BufferLen (0) { } LeaseSet::LeaseSet (const uint8_t * buf, size_t len, bool storeLeases): - m_IsValid (true), m_StoreLeases (storeLeases), m_ExpirationTime (0) + m_IsValid (true), m_StoreLeases (storeLeases), m_ExpirationTime (0), m_EncryptionKey (nullptr) { m_Buffer = new uint8_t[len]; memcpy (m_Buffer, buf, len); @@ -56,7 +57,11 @@ namespace data m_IsValid = false; return; } - memcpy (m_EncryptionKey, m_Buffer + size, 256); + if (m_StoreLeases) + { + if (!m_EncryptionKey) m_EncryptionKey = new uint8_t[256]; + memcpy (m_EncryptionKey, m_Buffer + size, 256); + } size += 256; // encryption key size += m_Identity->GetSigningPublicKeyLen (); // unused signing key uint8_t num = m_Buffer[size]; @@ -231,6 +236,7 @@ namespace data void LeaseSet::Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx) const { + if (!m_EncryptionKey) return; auto encryptor = m_Identity->CreateEncryptor (m_EncryptionKey); if (encryptor) encryptor->Encrypt (data, encrypted, ctx, true); @@ -395,6 +401,7 @@ namespace data { size_t offset = 0; // blinded key + if (len < 2) return; uint16_t blindedKeyType = bufbe16toh (buf + offset); offset += 2; std::unique_ptr blindedVerifier (i2p::data::IdentityEx::CreateVerifier (blindedKeyType)); if (!blindedVerifier) return; @@ -440,6 +447,35 @@ namespace data encryptor->Encrypt (data, encrypted, ctx, true); } + uint64_t LeaseSet2::ExtractTimestamp (const uint8_t * buf, size_t len) const + { + if (len < 8) return 0; + if (m_StoreType == NETDB_STORE_TYPE_ENCRYPTED_LEASESET2) + { + // encrypted LS2 + size_t offset = 0; + uint16_t blindedKeyType = bufbe16toh (buf + offset); offset += 2; + std::unique_ptr blindedVerifier (i2p::data::IdentityEx::CreateVerifier (blindedKeyType)); + if (!blindedVerifier) return 0 ; + auto blindedKeyLen = blindedVerifier->GetPublicKeyLen (); + if (offset + blindedKeyLen + 6 >= len) return 0; + offset += blindedKeyLen; + uint32_t timestamp = bufbe32toh (buf + offset); offset += 4; + uint16_t expires = bufbe16toh (buf + offset); offset += 2; + return (timestamp + expires)* 1000LL; + } + else + { + auto identity = GetIdentity (); + if (!identity) return 0; + size_t offset = identity->GetFullLen (); + if (offset + 6 >= len) return 0; + uint32_t timestamp = bufbe32toh (buf + offset); offset += 4; + uint16_t expires = bufbe16toh (buf + offset); offset += 2; + return (timestamp + expires)* 1000LL; + } + } + LocalLeaseSet::LocalLeaseSet (std::shared_ptr identity, const uint8_t * encryptionPublicKey, std::vector > tunnels): m_ExpirationTime (0), m_Identity (identity) { diff --git a/libi2pd/LeaseSet.h b/libi2pd/LeaseSet.h index ad704ee3..e085ecc7 100644 --- a/libi2pd/LeaseSet.h +++ b/libi2pd/LeaseSet.h @@ -59,7 +59,7 @@ namespace data public: LeaseSet (const uint8_t * buf, size_t len, bool storeLeases = true); - virtual ~LeaseSet () { delete[] m_Buffer; }; + virtual ~LeaseSet () { delete[] m_EncryptionKey; delete[] m_Buffer; }; void Update (const uint8_t * buf, size_t len, bool verifySignature = true); bool IsNewer (const uint8_t * buf, size_t len) const; void PopulateLeases (); // from buffer @@ -100,7 +100,7 @@ namespace data private: void ReadFromBuffer (bool readIdentity = true, bool verifySignature = true); - uint64_t ExtractTimestamp (const uint8_t * buf, size_t len) const; // returns max expiration time + virtual uint64_t ExtractTimestamp (const uint8_t * buf, size_t len) const; // returns max expiration time private: @@ -108,7 +108,7 @@ namespace data std::set, LeaseCmp> m_Leases; uint64_t m_ExpirationTime; // in milliseconds std::shared_ptr m_Identity; - uint8_t m_EncryptionKey[256]; + uint8_t * m_EncryptionKey; uint8_t * m_Buffer; size_t m_BufferLen; }; @@ -142,6 +142,8 @@ namespace data template bool VerifySignature (Verifier& verifier, const uint8_t * buf, size_t len, size_t signatureOffset); + uint64_t ExtractTimestamp (const uint8_t * buf, size_t len) const; + private: uint8_t m_StoreType;