Browse Source

strong pointer to session for receive tagset

pull/1622/head
orignal 4 years ago
parent
commit
bc4a97774f
  1. 19
      libi2pd/ECIESX25519AEADRatchetSession.cpp
  2. 30
      libi2pd/ECIESX25519AEADRatchetSession.h

19
libi2pd/ECIESX25519AEADRatchetSession.cpp

@ -100,6 +100,11 @@ namespace garlic
m_ExpirationTimestamp = i2p::util::GetSecondsSinceEpoch () + ECIESX25519_PREVIOUS_TAGSET_EXPIRATION_TIMEOUT; 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) bool ReceiveRatchetTagSet::HandleNextMessage (uint8_t * buf, size_t len, int index)
{ {
auto session = GetSession (); auto session = GetSession ();
@ -203,16 +208,13 @@ namespace garlic
return false; return false;
} }
std::shared_ptr<ReceiveRatchetTagSet> ECIESX25519AEADRatchetSession::CreateNewSessionTagset () void ECIESX25519AEADRatchetSession::InitNewSessionTagset (std::shared_ptr<RatchetTagSet> tagsetNsr) const
{ {
uint8_t tagsetKey[32]; uint8_t tagsetKey[32];
i2p::crypto::HKDF (m_CK, nullptr, 0, "SessionReplyTags", tagsetKey, 32); // tagsetKey = HKDF(chainKey, ZEROLEN, "SessionReplyTags", 32) i2p::crypto::HKDF (m_CK, nullptr, 0, "SessionReplyTags", tagsetKey, 32); // tagsetKey = HKDF(chainKey, ZEROLEN, "SessionReplyTags", 32)
// Session Tag Ratchet // Session Tag Ratchet
auto tagsetNsr = (m_State == eSessionStateNewSessionReceived) ? std::make_shared<ReceiveRatchetTagSet>(shared_from_this ()):
std::make_shared<NSRatchetTagSet>(shared_from_this ());
tagsetNsr->DHInitialize (m_CK, tagsetKey); // tagset_nsr = DH_INITIALIZE(chainKey, tagsetKey) tagsetNsr->DHInitialize (m_CK, tagsetKey); // tagset_nsr = DH_INITIALIZE(chainKey, tagsetKey)
tagsetNsr->NextSessionTagRatchet (); tagsetNsr->NextSessionTagRatchet ();
return tagsetNsr;
} }
bool ECIESX25519AEADRatchetSession::HandleNewIncomingSession (const uint8_t * buf, size_t len) 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) MixHash (out + offset, len + 16); // h = SHA256(h || ciphertext)
if (GetOwner ()) if (GetOwner ())
GenerateMoreReceiveTags (CreateNewSessionTagset (), ECIESX25519_NSR_NUM_GENERATED_TAGS); {
auto tagsetNsr = std::make_shared<ReceiveRatchetTagSet>(shared_from_this (), true);
InitNewSessionTagset (tagsetNsr);
GenerateMoreReceiveTags (tagsetNsr, ECIESX25519_NSR_NUM_GENERATED_TAGS);
}
} }
return true; return true;
} }
@ -538,7 +544,8 @@ namespace garlic
bool ECIESX25519AEADRatchetSession::NewSessionReplyMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen) bool ECIESX25519AEADRatchetSession::NewSessionReplyMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen)
{ {
// we are Bob // we are Bob
m_NSRSendTagset = CreateNewSessionTagset (); m_NSRSendTagset = std::make_shared<RatchetTagSet>();
InitNewSessionTagset (m_NSRSendTagset);
uint64_t tag = m_NSRSendTagset->GetNextSessionTag (); uint64_t tag = m_NSRSendTagset->GetNextSessionTag ();
size_t offset = 0; size_t offset = 0;

30
libi2pd/ECIESX25519AEADRatchetSession.h

@ -46,8 +46,6 @@ namespace garlic
RatchetTagSet () {}; RatchetTagSet () {};
virtual ~RatchetTagSet () {}; virtual ~RatchetTagSet () {};
virtual bool IsNS () const { return false; };
void DHInitialize (const uint8_t * rootKey, const uint8_t * k); void DHInitialize (const uint8_t * rootKey, const uint8_t * k);
void NextSessionTagRatchet (); void NextSessionTagRatchet ();
uint64_t GetNextSessionTag (); uint64_t GetNextSessionTag ();
@ -62,7 +60,6 @@ namespace garlic
void Expire (); 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: private:
union union
@ -89,32 +86,21 @@ namespace garlic
{ {
public: public:
ReceiveRatchetTagSet (std::shared_ptr<ECIESX25519AEADRatchetSession> session): m_Session (session) {}; ReceiveRatchetTagSet (std::shared_ptr<ECIESX25519AEADRatchetSession> session, bool isNS = false):
m_Session (session), m_IsNS (isNS) {};
std::shared_ptr<ECIESX25519AEADRatchetSession> GetSession () { return m_Session.lock (); }; bool IsNS () const { return m_IsNS; };
std::shared_ptr<ECIESX25519AEADRatchetSession> GetSession () { return m_Session; };
void SetTrimBehind (int index) { if (index > m_TrimBehindIndex) m_TrimBehindIndex = index; }; 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); virtual bool HandleNextMessage (uint8_t * buf, size_t len, int index);
private: private:
int m_TrimBehindIndex = 0; int m_TrimBehindIndex = 0;
std::weak_ptr<ECIESX25519AEADRatchetSession> m_Session; std::shared_ptr<ECIESX25519AEADRatchetSession> m_Session;
}; bool m_IsNS;
class NSRatchetTagSet: public ReceiveRatchetTagSet
{
public:
NSRatchetTagSet (std::shared_ptr<ECIESX25519AEADRatchetSession> session):
ReceiveRatchetTagSet (session), m_DummySession (session) {};
bool IsNS () const { return true; };
private:
std::shared_ptr<ECIESX25519AEADRatchetSession> m_DummySession; // we need a strong pointer for NS
}; };
class DatabaseLookupTagSet: public ReceiveRatchetTagSet class DatabaseLookupTagSet: public ReceiveRatchetTagSet
@ -202,7 +188,7 @@ namespace garlic
void CreateNonce (uint64_t seqn, uint8_t * nonce); void CreateNonce (uint64_t seqn, uint8_t * nonce);
bool GenerateEphemeralKeysAndEncode (uint8_t * buf); // buf is 32 bytes bool GenerateEphemeralKeysAndEncode (uint8_t * buf); // buf is 32 bytes
std::shared_ptr<ReceiveRatchetTagSet> CreateNewSessionTagset (); void InitNewSessionTagset (std::shared_ptr<RatchetTagSet> tagsetNsr) const;
bool HandleNewIncomingSession (const uint8_t * buf, size_t len); bool HandleNewIncomingSession (const uint8_t * buf, size_t len);
bool HandleNewOutgoingSessionReply (uint8_t * buf, size_t len); bool HandleNewOutgoingSessionReply (uint8_t * buf, size_t len);

Loading…
Cancel
Save