Browse Source

handle LS2 in destinations

pull/1288/head
orignal 6 years ago
parent
commit
5398b651f7
  1. 8
      libi2pd/Destination.cpp
  2. 15
      libi2pd/Identity.cpp
  3. 3
      libi2pd/Identity.h
  4. 83
      libi2pd/LeaseSet.cpp
  5. 11
      libi2pd/LeaseSet.h
  6. 2
      libi2pd/NetDb.cpp

8
libi2pd/Destination.cpp

@ -357,7 +357,8 @@ namespace client
} }
i2p::data::IdentHash key (buf + DATABASE_STORE_KEY_OFFSET); i2p::data::IdentHash key (buf + DATABASE_STORE_KEY_OFFSET);
std::shared_ptr<i2p::data::LeaseSet> leaseSet; std::shared_ptr<i2p::data::LeaseSet> leaseSet;
if (buf[DATABASE_STORE_TYPE_OFFSET] == 1) // LeaseSet if (buf[DATABASE_STORE_TYPE_OFFSET] == i2p::data::NETDB_STORE_TYPE_LEASESET || // 1
buf[DATABASE_STORE_TYPE_OFFSET] == i2p::data::NETDB_STORE_TYPE_STANDARD_LEASESET2) // 3
{ {
LogPrint (eLogDebug, "Destination: Remote LeaseSet"); LogPrint (eLogDebug, "Destination: Remote LeaseSet");
std::lock_guard<std::mutex> lock(m_RemoteLeaseSetsMutex); std::lock_guard<std::mutex> lock(m_RemoteLeaseSetsMutex);
@ -382,7 +383,10 @@ namespace client
} }
else else
{ {
leaseSet = std::make_shared<i2p::data::LeaseSet> (buf + offset, len - offset); if (buf[DATABASE_STORE_TYPE_OFFSET] == i2p::data::NETDB_STORE_TYPE_LEASESET)
leaseSet = std::make_shared<i2p::data::LeaseSet> (buf + offset, len - offset); // LeaseSet
else
leaseSet = std::make_shared<i2p::data::LeaseSet2> (buf[DATABASE_STORE_TYPE_OFFSET], buf + offset, len - offset); // LeaseSet2
if (leaseSet->IsValid () && leaseSet->GetIdentHash () == key) if (leaseSet->IsValid () && leaseSet->GetIdentHash () == key)
{ {
if (leaseSet->GetIdentHash () != GetIdentHash ()) if (leaseSet->GetIdentHash () != GetIdentHash ())

15
libi2pd/Identity.cpp

@ -318,7 +318,7 @@ namespace data
return CRYPTO_KEY_TYPE_ELGAMAL; return CRYPTO_KEY_TYPE_ELGAMAL;
} }
i2p::crypto::Verifier * IdentityEx::CreateVerifier (uint16_t keyType) i2p::crypto::Verifier * IdentityEx::CreateVerifier (SigningKeyType keyType)
{ {
switch (keyType) switch (keyType)
{ {
@ -401,10 +401,9 @@ namespace data
m_Verifier = nullptr; m_Verifier = nullptr;
} }
std::shared_ptr<i2p::crypto::CryptoKeyEncryptor> IdentityEx::CreateEncryptor (const uint8_t * key) const std::shared_ptr<i2p::crypto::CryptoKeyEncryptor> IdentityEx::CreateEncryptor (CryptoKeyType keyType, const uint8_t * key)
{ {
if (!key) key = GetEncryptionPublicKey (); // use publicKey switch (keyType)
switch (GetCryptoKeyType ())
{ {
case CRYPTO_KEY_TYPE_ELGAMAL: case CRYPTO_KEY_TYPE_ELGAMAL:
return std::make_shared<i2p::crypto::ElGamalEncryptor>(key); return std::make_shared<i2p::crypto::ElGamalEncryptor>(key);
@ -417,11 +416,17 @@ namespace data
return std::make_shared<i2p::crypto::ECIESGOSTR3410Encryptor>(key); return std::make_shared<i2p::crypto::ECIESGOSTR3410Encryptor>(key);
break; break;
default: default:
LogPrint (eLogError, "Identity: Unknown crypto key type ", (int)GetCryptoKeyType ()); LogPrint (eLogError, "Identity: Unknown crypto key type ", (int)keyType);
}; };
return nullptr; return nullptr;
} }
std::shared_ptr<i2p::crypto::CryptoKeyEncryptor> IdentityEx::CreateEncryptor (const uint8_t * key) const
{
if (!key) key = GetEncryptionPublicKey (); // use publicKey
return CreateEncryptor (GetCryptoKeyType (), key);
}
PrivateKeys& PrivateKeys::operator=(const Keys& keys) PrivateKeys& PrivateKeys::operator=(const Keys& keys)
{ {
m_Public = std::make_shared<IdentityEx>(Identity (keys)); m_Public = std::make_shared<IdentityEx>(Identity (keys));

3
libi2pd/Identity.h

@ -110,7 +110,8 @@ namespace data
bool operator == (const IdentityEx & other) const { return GetIdentHash() == other.GetIdentHash(); } bool operator == (const IdentityEx & other) const { return GetIdentHash() == other.GetIdentHash(); }
void RecalculateIdentHash(uint8_t * buff=nullptr); void RecalculateIdentHash(uint8_t * buff=nullptr);
static i2p::crypto::Verifier * CreateVerifier (uint16_t keyType); static i2p::crypto::Verifier * CreateVerifier (SigningKeyType keyType);
static std::shared_ptr<i2p::crypto::CryptoKeyEncryptor> CreateEncryptor (CryptoKeyType keyType, const uint8_t * key);
private: private:

83
libi2pd/LeaseSet.cpp

@ -12,8 +12,8 @@ namespace i2p
namespace data namespace data
{ {
LeaseSet::LeaseSet (): LeaseSet::LeaseSet (bool storeLeases):
m_IsValid (false), m_StoreLeases (false), m_ExpirationTime (0), m_Buffer (nullptr), m_BufferLen (0) m_IsValid (false), m_StoreLeases (storeLeases), m_ExpirationTime (0), m_Buffer (nullptr), m_BufferLen (0)
{ {
} }
@ -69,12 +69,7 @@ namespace data
return; return;
} }
// reset existing leases UpdateLeasesBegin ();
if (m_StoreLeases)
for (auto& it: m_Leases)
it->isUpdated = false;
else
m_Leases.clear ();
// process leases // process leases
m_ExpirationTime = 0; m_ExpirationTime = 0;
@ -98,6 +93,29 @@ namespace data
return; return;
} }
m_ExpirationTime += LEASE_ENDDATE_THRESHOLD; m_ExpirationTime += LEASE_ENDDATE_THRESHOLD;
UpdateLeasesEnd ();
// verify
if (verifySignature && !m_Identity->Verify (m_Buffer, leases - m_Buffer, leases))
{
LogPrint (eLogWarning, "LeaseSet: verification failed");
m_IsValid = false;
}
}
void LeaseSet::UpdateLeasesBegin ()
{
// reset existing leases
if (m_StoreLeases)
for (auto& it: m_Leases)
it->isUpdated = false;
else
m_Leases.clear ();
}
void LeaseSet::UpdateLeasesEnd ()
{
// delete old leases // delete old leases
if (m_StoreLeases) if (m_StoreLeases)
{ {
@ -112,13 +130,6 @@ namespace data
++it; ++it;
} }
} }
// verify
if (verifySignature && !m_Identity->Verify (m_Buffer, leases - m_Buffer, leases))
{
LogPrint (eLogWarning, "LeaseSet: verification failed");
m_IsValid = false;
}
} }
void LeaseSet::UpdateLease (const Lease& lease, uint64_t ts) void LeaseSet::UpdateLease (const Lease& lease, uint64_t ts)
@ -233,8 +244,8 @@ namespace data
memcpy (m_Buffer, buf, len); 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, bool storeLeases):
m_StoreType (storeType) LeaseSet (storeLeases), m_StoreType (storeType)
{ {
SetBuffer (buf, len); SetBuffer (buf, len);
if (storeType == NETDB_STORE_TYPE_ENCRYPTED_LEASESET2) if (storeType == NETDB_STORE_TYPE_ENCRYPTED_LEASESET2)
@ -317,26 +328,37 @@ namespace data
int numKeySections = buf[offset]; offset++; int numKeySections = buf[offset]; offset++;
for (int i = 0; i < numKeySections; i++) for (int i = 0; i < numKeySections; i++)
{ {
// skip key for now. TODO: implement encryption key uint16_t keyType = bufbe16toh (buf + offset); offset += 2; // encryption key type
offset += 2; // encryption key type
if (offset + 2 >= len) return 0; if (offset + 2 >= len) return 0;
uint16_t encryptionKeyLen = bufbe16toh (buf + offset); offset += 2; uint16_t encryptionKeyLen = bufbe16toh (buf + offset); offset += 2;
if (offset + encryptionKeyLen >= len) return 0;
if (!m_Encryptor && IsStoreLeases ()) // create encryptor with leases only, first key
{
auto encryptor = i2p::data::IdentityEx::CreateEncryptor (keyType, buf + offset);
m_Encryptor = encryptor; // TODO: atomic
}
offset += encryptionKeyLen; offset += encryptionKeyLen;
if (offset >= len) return 0;
} }
// leases // leases
if (offset + 1 >= len) return 0; if (offset + 1 >= len) return 0;
int numLeases = buf[offset]; offset++; int numLeases = buf[offset]; offset++;
auto ts = i2p::util::GetMillisecondsSinceEpoch (); auto ts = i2p::util::GetMillisecondsSinceEpoch ();
for (int i = 0; i < numLeases; i++) if (IsStoreLeases ())
{ {
if (offset + 40 > len) return 0; UpdateLeasesBegin ();
Lease lease; for (int i = 0; i < numLeases; i++)
lease.tunnelGateway = buf + offset; offset += 32; // gateway {
lease.tunnelID = bufbe32toh (buf + offset); offset += 4; // tunnel ID if (offset + 40 > len) return 0;
lease.endDate = bufbe32toh (buf + offset)*1000LL; offset += 4; // end date Lease lease;
UpdateLease (lease, ts); lease.tunnelGateway = buf + offset; offset += 32; // gateway
lease.tunnelID = bufbe32toh (buf + offset); offset += 4; // tunnel ID
lease.endDate = bufbe32toh (buf + offset)*1000LL; offset += 4; // end date
UpdateLease (lease, ts);
}
UpdateLeasesEnd ();
} }
else
offset += numLeases*40; // 40 bytes per lease
return offset; return offset;
} }
@ -410,6 +432,13 @@ namespace data
SetIsValid (verified); SetIsValid (verified);
} }
void LeaseSet2::Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx) const
{
auto encryptor = m_Encryptor; // TODO: atomic
if (encryptor)
encryptor->Encrypt (data, encrypted, ctx, true);
}
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)
{ {

11
libi2pd/LeaseSet.h

@ -84,14 +84,17 @@ namespace data
protected: protected:
void UpdateLeasesBegin ();
void UpdateLeasesEnd ();
void UpdateLease (const Lease& lease, uint64_t ts); void UpdateLease (const Lease& lease, uint64_t ts);
// called from LeaseSet2 // called from LeaseSet2
LeaseSet (); LeaseSet (bool storeLeases);
void SetBuffer (const uint8_t * buf, size_t len); void SetBuffer (const uint8_t * buf, size_t len);
void SetIdentity (std::shared_ptr<const IdentityEx> identity) { m_Identity = identity; }; void SetIdentity (std::shared_ptr<const IdentityEx> identity) { m_Identity = identity; };
void SetExpirationTime (uint64_t t) { m_ExpirationTime = t; }; void SetExpirationTime (uint64_t t) { m_ExpirationTime = t; };
void SetIsValid (bool isValid) { m_IsValid = isValid; }; void SetIsValid (bool isValid) { m_IsValid = isValid; };
bool IsStoreLeases () const { return m_StoreLeases; };
private: private:
@ -122,9 +125,12 @@ namespace data
{ {
public: public:
LeaseSet2 (uint8_t storeType, const uint8_t * buf, size_t len); LeaseSet2 (uint8_t storeType, const uint8_t * buf, size_t len, bool storeLeases = true);
uint8_t GetStoreType () const { return m_StoreType; }; uint8_t GetStoreType () const { return m_StoreType; };
// implements RoutingDestination
void Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx) const;
private: private:
void ReadFromBuffer (const uint8_t * buf, size_t len); void ReadFromBuffer (const uint8_t * buf, size_t len);
@ -138,6 +144,7 @@ namespace data
private: private:
uint8_t m_StoreType; uint8_t m_StoreType;
std::shared_ptr<i2p::crypto::CryptoKeyEncryptor> m_Encryptor; // for standardLS2
}; };
class LocalLeaseSet class LocalLeaseSet

2
libi2pd/NetDb.cpp

@ -297,7 +297,7 @@ namespace data
auto it = m_LeaseSets.find(ident); auto it = m_LeaseSets.find(ident);
if (it == m_LeaseSets.end ()) if (it == m_LeaseSets.end ())
{ {
auto leaseSet = std::make_shared<LeaseSet2> (storeType, buf, len); auto leaseSet = std::make_shared<LeaseSet2> (storeType, buf, len, false); // we don't need leases in netdb
m_LeaseSets[ident] = leaseSet; m_LeaseSets[ident] = leaseSet;
return true; return true;
} }

Loading…
Cancel
Save