Browse Source

extract timestamp for LS2

pull/1288/head
orignal 6 years ago
parent
commit
3712749a94
  1. 40
      libi2pd/LeaseSet.cpp
  2. 8
      libi2pd/LeaseSet.h

40
libi2pd/LeaseSet.cpp

@ -13,12 +13,13 @@ namespace data
{ {
LeaseSet::LeaseSet (bool storeLeases): 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): 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]; m_Buffer = new uint8_t[len];
memcpy (m_Buffer, buf, len); memcpy (m_Buffer, buf, len);
@ -56,7 +57,11 @@ namespace data
m_IsValid = false; m_IsValid = false;
return; return;
} }
if (m_StoreLeases)
{
if (!m_EncryptionKey) m_EncryptionKey = new uint8_t[256];
memcpy (m_EncryptionKey, m_Buffer + size, 256); memcpy (m_EncryptionKey, m_Buffer + size, 256);
}
size += 256; // encryption key size += 256; // encryption key
size += m_Identity->GetSigningPublicKeyLen (); // unused signing key size += m_Identity->GetSigningPublicKeyLen (); // unused signing key
uint8_t num = m_Buffer[size]; 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 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); auto encryptor = m_Identity->CreateEncryptor (m_EncryptionKey);
if (encryptor) if (encryptor)
encryptor->Encrypt (data, encrypted, ctx, true); encryptor->Encrypt (data, encrypted, ctx, true);
@ -395,6 +401,7 @@ namespace data
{ {
size_t offset = 0; size_t offset = 0;
// blinded key // blinded key
if (len < 2) return;
uint16_t blindedKeyType = bufbe16toh (buf + offset); offset += 2; uint16_t blindedKeyType = bufbe16toh (buf + offset); offset += 2;
std::unique_ptr<i2p::crypto::Verifier> blindedVerifier (i2p::data::IdentityEx::CreateVerifier (blindedKeyType)); std::unique_ptr<i2p::crypto::Verifier> blindedVerifier (i2p::data::IdentityEx::CreateVerifier (blindedKeyType));
if (!blindedVerifier) return; if (!blindedVerifier) return;
@ -440,6 +447,35 @@ namespace data
encryptor->Encrypt (data, encrypted, ctx, true); 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<i2p::crypto::Verifier> 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<const IdentityEx> identity, const uint8_t * encryptionPublicKey, std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels): LocalLeaseSet::LocalLeaseSet (std::shared_ptr<const IdentityEx> identity, const uint8_t * encryptionPublicKey, std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels):
m_ExpirationTime (0), m_Identity (identity) m_ExpirationTime (0), m_Identity (identity)
{ {

8
libi2pd/LeaseSet.h

@ -59,7 +59,7 @@ namespace data
public: public:
LeaseSet (const uint8_t * buf, size_t len, bool storeLeases = true); 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); void Update (const uint8_t * buf, size_t len, bool verifySignature = true);
bool IsNewer (const uint8_t * buf, size_t len) const; bool IsNewer (const uint8_t * buf, size_t len) const;
void PopulateLeases (); // from buffer void PopulateLeases (); // from buffer
@ -100,7 +100,7 @@ namespace data
private: private:
void ReadFromBuffer (bool readIdentity = true, bool verifySignature = true); 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: private:
@ -108,7 +108,7 @@ namespace data
std::set<std::shared_ptr<Lease>, LeaseCmp> m_Leases; std::set<std::shared_ptr<Lease>, LeaseCmp> m_Leases;
uint64_t m_ExpirationTime; // in milliseconds uint64_t m_ExpirationTime; // in milliseconds
std::shared_ptr<const IdentityEx> m_Identity; std::shared_ptr<const IdentityEx> m_Identity;
uint8_t m_EncryptionKey[256]; uint8_t * m_EncryptionKey;
uint8_t * m_Buffer; uint8_t * m_Buffer;
size_t m_BufferLen; size_t m_BufferLen;
}; };
@ -142,6 +142,8 @@ namespace data
template<typename Verifier> template<typename Verifier>
bool VerifySignature (Verifier& verifier, const uint8_t * buf, size_t len, size_t signatureOffset); 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: private:
uint8_t m_StoreType; uint8_t m_StoreType;

Loading…
Cancel
Save