Browse Source

delete expired ECIESX25519AEADRatchet sessions and tags

pull/1474/head
orignal 4 years ago
parent
commit
9c9b723cf5
  1. 1
      libi2pd/ECIESX25519AEADRatchetSession.cpp
  2. 8
      libi2pd/ECIESX25519AEADRatchetSession.h
  3. 36
      libi2pd/Garlic.cpp
  4. 1
      libi2pd/Garlic.h

1
libi2pd/ECIESX25519AEADRatchetSession.cpp

@ -401,6 +401,7 @@ namespace garlic @@ -401,6 +401,7 @@ namespace garlic
bool ECIESX25519AEADRatchetSession::HandleNextMessage (const uint8_t * buf, size_t len, int index)
{
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch ();
switch (m_State)
{
case eSessionStateEstablished:

8
libi2pd/ECIESX25519AEADRatchetSession.h

@ -55,6 +55,10 @@ namespace garlic @@ -55,6 +55,10 @@ namespace garlic
eECIESx25519BlkPadding = 254
};
const int ECIESX25519_RESTART_TIMEOUT = 120; // number of second of inactivity we should restart after
const int ECIESX25519_EXPIRATION_TIMEOUT = 600; // in seconds
class ECIESX25519AEADRatchetSession: public GarlicRoutingSession, public std::enable_shared_from_this<ECIESX25519AEADRatchetSession>
{
enum SessionState
@ -81,6 +85,9 @@ namespace garlic @@ -81,6 +85,9 @@ namespace garlic
if (!m_Destination) m_Destination.reset (new i2p::data::IdentHash (dest));
}
bool IsExpired (uint64_t ts) const { return ts > m_LastActivityTimestamp + ECIESX25519_EXPIRATION_TIMEOUT; }
bool CanBeRestarted (uint64_t ts) const { return ts > m_LastActivityTimestamp + ECIESX25519_RESTART_TIMEOUT; }
private:
void ResetKeys ();
@ -109,6 +116,7 @@ namespace garlic @@ -109,6 +116,7 @@ namespace garlic
uint8_t m_Aepk[32]; // Alice's ephemeral keys TODO: for incoming only
i2p::crypto::X25519Keys m_EphemeralKeys;
SessionState m_State = eSessionStateNew;
uint64_t m_LastActivityTimestamp = 0; // incoming
RatchetTagSet m_SendTagset, m_ReceiveTagset;
int m_NumReceiveTags = 0;
std::unique_ptr<i2p::data::IdentHash> m_Destination;// TODO: might not need it

36
libi2pd/Garlic.cpp

@ -739,7 +739,25 @@ namespace garlic @@ -739,7 +739,25 @@ namespace garlic
++it;
}
}
// TODO: cleanup ECIESx25519
// ECIESx25519
for (auto it = m_ECIESx25519Tags.begin (); it != m_ECIESx25519Tags.end ();)
{
if (ts > it->second.creationTime + INCOMING_TAGS_EXPIRATION_TIMEOUT)
it = m_ECIESx25519Tags.erase (it);
else
++it;
}
for (auto it = m_ECIESx25519Sessions.begin (); it != m_ECIESx25519Sessions.end ();)
{
if (it->second->IsExpired (ts))
{
it->second->SetOwner (nullptr);
it = m_ECIESx25519Sessions.erase (it);
}
else
++it;
}
}
void GarlicDestination::RemoveDeliveryStatusSession (uint32_t msgID)
@ -940,12 +958,24 @@ namespace garlic @@ -940,12 +958,24 @@ namespace garlic
void GarlicDestination::AddECIESx25519SessionTag (int index, uint64_t tag, ECIESX25519AEADRatchetSessionPtr session)
{
m_ECIESx25519Tags.emplace (tag, ECIESX25519AEADRatchetIndexSession{index, session});
m_ECIESx25519Tags.emplace (tag, ECIESX25519AEADRatchetIndexSession{index, session, i2p::util::GetSecondsSinceEpoch ()});
}
void GarlicDestination::AddECIESx25519Session (const uint8_t * staticKey, ECIESX25519AEADRatchetSessionPtr session)
{
m_ECIESx25519Sessions.emplace (staticKey, session);
i2p::data::Tag<32> staticKeyTag (staticKey);
auto it = m_ECIESx25519Sessions.find (staticKeyTag);
if (it != m_ECIESx25519Sessions.end ())
{
if (it->second->CanBeRestarted (i2p::util::GetSecondsSinceEpoch ()))
m_ECIESx25519Sessions.erase (it);
else
{
LogPrint (eLogInfo, "Garlic: ECIESx25519 session with static key ", staticKeyTag.ToBase64 (), " already exists");
return;
}
}
m_ECIESx25519Sessions.emplace (staticKeyTag, session);
}
}

1
libi2pd/Garlic.h

@ -202,6 +202,7 @@ namespace garlic @@ -202,6 +202,7 @@ namespace garlic
{
int index;
ECIESX25519AEADRatchetSessionPtr session;
uint64_t creationTime; // seconds since epoch
};
class GarlicDestination: public i2p::data::LocalDestination

Loading…
Cancel
Save