Browse Source

LS2 signature verification and store type

pull/1278/head
orignal 6 years ago
parent
commit
6193b06708
  1. 2
      libi2pd/I2NPProtocol.cpp
  2. 38
      libi2pd/LeaseSet.cpp
  3. 10
      libi2pd/LeaseSet.h

2
libi2pd/I2NPProtocol.cpp

@ -264,7 +264,7 @@ namespace i2p @@ -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 ());

38
libi2pd/LeaseSet.cpp

@ -228,21 +228,51 @@ namespace data @@ -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<IdentityEx>(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<const IdentityEx> identity, const uint8_t * encryptionPublicKey, std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels):

10
libi2pd/LeaseSet.h

@ -56,7 +56,7 @@ namespace data @@ -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,6 +73,7 @@ namespace data @@ -73,6 +73,7 @@ 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<const IdentityEx> GetIdentity () const { return m_Identity; };
@ -115,10 +116,15 @@ namespace data @@ -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 (uint8_t storeType, const uint8_t * buf, size_t len);
void ReadFromBuffer (const uint8_t * buf, size_t len);
private:
uint8_t m_StoreType;
};
class LocalLeaseSet

Loading…
Cancel
Save