|
|
@ -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) |
|
|
|
{ |
|
|
|
{ |
|
|
|