diff --git a/libi2pd/I2NPProtocol.cpp b/libi2pd/I2NPProtocol.cpp index 7f4d1ca3..c98c7a88 100644 --- a/libi2pd/I2NPProtocol.cpp +++ b/libi2pd/I2NPProtocol.cpp @@ -264,7 +264,7 @@ namespace i2p auto m = NewI2NPShortMessage (); uint8_t * payload = m->GetPayload (); memcpy (payload + DATABASE_STORE_KEY_OFFSET, leaseSet->GetIdentHash (), 32); - payload[DATABASE_STORE_TYPE_OFFSET] = 1; // LeaseSet + payload[DATABASE_STORE_TYPE_OFFSET] = leaseSet->GetStoreType (); // 1 for LeaseSet htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, 0); size_t size = DATABASE_STORE_HEADER_SIZE; memcpy (payload + size, leaseSet->GetBuffer (), leaseSet->GetBufferLen ()); diff --git a/libi2pd/LeaseSet.cpp b/libi2pd/LeaseSet.cpp index 52e736f9..470dfc0a 100644 --- a/libi2pd/LeaseSet.cpp +++ b/libi2pd/LeaseSet.cpp @@ -228,21 +228,51 @@ namespace data memcpy (m_Buffer, buf, len); } - LeaseSet2::LeaseSet2 (uint8_t storeType, const uint8_t * buf, size_t len) - { + LeaseSet2::LeaseSet2 (uint8_t storeType, const uint8_t * buf, size_t len): + m_StoreType (storeType) + { SetBuffer (buf, len); - ReadFromBuffer (storeType, buf, len); + ReadFromBuffer (buf, len); } - void LeaseSet2::ReadFromBuffer (uint8_t storeType, const uint8_t * buf, size_t len) + void LeaseSet2::ReadFromBuffer (const uint8_t * buf, size_t len) { auto identity = std::make_shared(buf, len); SetIdentity (identity); size_t offset = identity->GetFullLen (); + if (offset + 10 >= len) return; uint32_t timestamp = bufbe32toh (buf + offset); offset += 4; // published timestamp (seconds) uint16_t expires = bufbe16toh (buf + offset); offset += 2; // expires (seconds) SetExpirationTime ((timestamp + expires)*1000LL); // in milliseconds - SetIsValid (true); // TODO:: verify signature + offset += 2; // flags + // properties + uint16_t propertiesLen = bufbe16toh (buf + offset); offset += 2; + offset += propertiesLen; // skip for now. TODO: implement properties + if (offset + 1 >= len) return; + // key sections + int numKeySections = buf[offset]; offset++; + for (int i = 0; i < numKeySections; i++) + { + // skip key for now. TODO: implement encryption key + offset += 2; // encryption key type + if (offset + 2 >= len) return; + uint16_t encryptionKeyLen = bufbe16toh (buf + offset); offset += 2; + offset += encryptionKeyLen; + if (offset >= len) return; + } + // leases + int numLeases = buf[offset]; offset++; + offset += numLeases*40; // 40 bytes each + // verify signature + if (offset + identity->GetSignatureLen () > len) return; + uint8_t * buf1 = new uint8_t[offset + 1]; + buf1[0] = m_StoreType; + memcpy (buf1 + 1, buf, offset); // TODO: implement it better + bool verified = identity->Verify (buf1, offset + 1, buf + offset); // assume online keys + delete[] buf1; + if (!verified) + LogPrint (eLogWarning, "LeaseSet2: verification failed"); + SetIsValid (verified); } LocalLeaseSet::LocalLeaseSet (std::shared_ptr identity, const uint8_t * encryptionPublicKey, std::vector > tunnels): diff --git a/libi2pd/LeaseSet.h b/libi2pd/LeaseSet.h index 330f3410..b2c96d1d 100644 --- a/libi2pd/LeaseSet.h +++ b/libi2pd/LeaseSet.h @@ -56,7 +56,7 @@ namespace data public: LeaseSet (const uint8_t * buf, size_t len, bool storeLeases = true); - ~LeaseSet () { delete[] m_Buffer; }; + virtual ~LeaseSet () { 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 @@ -73,7 +73,8 @@ namespace data bool ExpiresSoon(const uint64_t dlt=1000 * 5, const uint64_t fudge = 0) const ; bool operator== (const LeaseSet& other) const { return m_BufferLen == other.m_BufferLen && !memcmp (m_Buffer, other.m_Buffer, m_BufferLen); }; - + virtual uint8_t GetStoreType () const { return 1; }; + // implements RoutingDestination std::shared_ptr GetIdentity () const { return m_Identity; }; void Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx) const; @@ -115,10 +116,15 @@ namespace data public: LeaseSet2 (uint8_t storeType, const uint8_t * buf, size_t len); + uint8_t GetStoreType () const { return m_StoreType; }; + + private: + + void ReadFromBuffer (const uint8_t * buf, size_t len); private: - void ReadFromBuffer (uint8_t storeType, const uint8_t * buf, size_t len); + uint8_t m_StoreType; }; class LocalLeaseSet