diff --git a/libi2pd/ECIESX25519AEADRatchetSession.cpp b/libi2pd/ECIESX25519AEADRatchetSession.cpp index 2a0f5cb4..a83c9285 100644 --- a/libi2pd/ECIESX25519AEADRatchetSession.cpp +++ b/libi2pd/ECIESX25519AEADRatchetSession.cpp @@ -100,6 +100,11 @@ namespace garlic m_ExpirationTimestamp = i2p::util::GetSecondsSinceEpoch () + ECIESX25519_PREVIOUS_TAGSET_EXPIRATION_TIMEOUT; } + bool ReceiveRatchetTagSet::IsIndexExpired (int index) const + { + return index < m_TrimBehindIndex || !m_Session || !m_Session->GetOwner (); + } + bool ReceiveRatchetTagSet::HandleNextMessage (uint8_t * buf, size_t len, int index) { auto session = GetSession (); @@ -203,16 +208,13 @@ namespace garlic return false; } - std::shared_ptr ECIESX25519AEADRatchetSession::CreateNewSessionTagset () + void ECIESX25519AEADRatchetSession::InitNewSessionTagset (std::shared_ptr tagsetNsr) const { uint8_t tagsetKey[32]; i2p::crypto::HKDF (m_CK, nullptr, 0, "SessionReplyTags", tagsetKey, 32); // tagsetKey = HKDF(chainKey, ZEROLEN, "SessionReplyTags", 32) // Session Tag Ratchet - auto tagsetNsr = (m_State == eSessionStateNewSessionReceived) ? std::make_shared(shared_from_this ()): - std::make_shared(shared_from_this ()); tagsetNsr->DHInitialize (m_CK, tagsetKey); // tagset_nsr = DH_INITIALIZE(chainKey, tagsetKey) tagsetNsr->NextSessionTagRatchet (); - return tagsetNsr; } bool ECIESX25519AEADRatchetSession::HandleNewIncomingSession (const uint8_t * buf, size_t len) @@ -501,7 +503,11 @@ namespace garlic { MixHash (out + offset, len + 16); // h = SHA256(h || ciphertext) if (GetOwner ()) - GenerateMoreReceiveTags (CreateNewSessionTagset (), ECIESX25519_NSR_NUM_GENERATED_TAGS); + { + auto tagsetNsr = std::make_shared(shared_from_this (), true); + InitNewSessionTagset (tagsetNsr); + GenerateMoreReceiveTags (tagsetNsr, ECIESX25519_NSR_NUM_GENERATED_TAGS); + } } return true; } @@ -538,7 +544,8 @@ namespace garlic bool ECIESX25519AEADRatchetSession::NewSessionReplyMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen) { // we are Bob - m_NSRSendTagset = CreateNewSessionTagset (); + m_NSRSendTagset = std::make_shared(); + InitNewSessionTagset (m_NSRSendTagset); uint64_t tag = m_NSRSendTagset->GetNextSessionTag (); size_t offset = 0; diff --git a/libi2pd/ECIESX25519AEADRatchetSession.h b/libi2pd/ECIESX25519AEADRatchetSession.h index 0cc7935e..526f2ea4 100644 --- a/libi2pd/ECIESX25519AEADRatchetSession.h +++ b/libi2pd/ECIESX25519AEADRatchetSession.h @@ -46,8 +46,6 @@ namespace garlic RatchetTagSet () {}; virtual ~RatchetTagSet () {}; - virtual bool IsNS () const { return false; }; - void DHInitialize (const uint8_t * rootKey, const uint8_t * k); void NextSessionTagRatchet (); uint64_t GetNextSessionTag (); @@ -60,8 +58,7 @@ namespace garlic void SetTagSetID (int tagsetID) { m_TagSetID = tagsetID; }; void Expire (); - bool IsExpired (uint64_t ts) const { return m_ExpirationTimestamp && ts > m_ExpirationTimestamp; }; - + bool IsExpired (uint64_t ts) const { return m_ExpirationTimestamp && ts > m_ExpirationTimestamp; }; private: @@ -89,32 +86,21 @@ namespace garlic { public: - ReceiveRatchetTagSet (std::shared_ptr session): m_Session (session) {}; + ReceiveRatchetTagSet (std::shared_ptr session, bool isNS = false): + m_Session (session), m_IsNS (isNS) {}; - std::shared_ptr GetSession () { return m_Session.lock (); }; + bool IsNS () const { return m_IsNS; }; + std::shared_ptr GetSession () { return m_Session; }; void SetTrimBehind (int index) { if (index > m_TrimBehindIndex) m_TrimBehindIndex = index; }; - virtual bool IsIndexExpired (int index) const { return m_Session.expired () || index < m_TrimBehindIndex; }; + virtual bool IsIndexExpired (int index) const; virtual bool HandleNextMessage (uint8_t * buf, size_t len, int index); private: int m_TrimBehindIndex = 0; - std::weak_ptr m_Session; - }; - - class NSRatchetTagSet: public ReceiveRatchetTagSet - { - public: - - NSRatchetTagSet (std::shared_ptr session): - ReceiveRatchetTagSet (session), m_DummySession (session) {}; - - bool IsNS () const { return true; }; - - private: - - std::shared_ptr m_DummySession; // we need a strong pointer for NS + std::shared_ptr m_Session; + bool m_IsNS; }; class DatabaseLookupTagSet: public ReceiveRatchetTagSet @@ -202,7 +188,7 @@ namespace garlic void CreateNonce (uint64_t seqn, uint8_t * nonce); bool GenerateEphemeralKeysAndEncode (uint8_t * buf); // buf is 32 bytes - std::shared_ptr CreateNewSessionTagset (); + void InitNewSessionTagset (std::shared_ptr tagsetNsr) const; bool HandleNewIncomingSession (const uint8_t * buf, size_t len); bool HandleNewOutgoingSessionReply (uint8_t * buf, size_t len);