diff --git a/libi2pd/ECIESX25519AEADRatchetSession.cpp b/libi2pd/ECIESX25519AEADRatchetSession.cpp index 3a732876..ffda9191 100644 --- a/libi2pd/ECIESX25519AEADRatchetSession.cpp +++ b/libi2pd/ECIESX25519AEADRatchetSession.cpp @@ -86,7 +86,7 @@ namespace garlic return tagsetNsr.GetNextSessionTag (); } - bool ECIESX25519AEADRatchetSession::NewIncomingSession (const uint8_t * buf, size_t len, CloveHandler handleClove) + bool ECIESX25519AEADRatchetSession::HandleNewIncomingSession (const uint8_t * buf, size_t len, CloveHandler handleClove) { if (!GetOwner ()) return false; // we are Bob @@ -219,6 +219,7 @@ namespace garlic } MixHash (out + offset, 16); // h = SHA256(h || ciphertext) + m_State = eSessionStateNewSessionSent; if (GetOwner ()) GetOwner ()->AddECIESx25519SessionTag (CreateNewSessionTag (), shared_from_this ()); @@ -272,7 +273,7 @@ namespace garlic return true; } - bool ECIESX25519AEADRatchetSession::NewOutgoingSessionReply (const uint8_t * buf, size_t len, CloveHandler handleClove) + bool ECIESX25519AEADRatchetSession::HandleNewOutgoingSessionReply (const uint8_t * buf, size_t len, CloveHandler handleClove) { // we are Alice LogPrint (eLogDebug, "Garlic: reply received"); @@ -323,6 +324,20 @@ namespace garlic return true; } + bool ECIESX25519AEADRatchetSession::HandleNextMessage (const uint8_t * buf, size_t len, CloveHandler handleClove) + { + switch (m_State) + { + case eSessionStateNew: + return HandleNewIncomingSession (buf, len, handleClove); + case eSessionStateNewSessionSent: + return HandleNewOutgoingSessionReply (buf, len, handleClove); + default: + return false; + } + return true; + } + std::shared_ptr ECIESX25519AEADRatchetSession::WrapSingleMessage (std::shared_ptr msg) { auto m = NewI2NPMessage (); diff --git a/libi2pd/ECIESX25519AEADRatchetSession.h b/libi2pd/ECIESX25519AEADRatchetSession.h index 5777885b..b8988f6e 100644 --- a/libi2pd/ECIESX25519AEADRatchetSession.h +++ b/libi2pd/ECIESX25519AEADRatchetSession.h @@ -53,7 +53,8 @@ namespace garlic enum SessionState { eSessionStateNew =0, - eSessionStateNewSessionReceived + eSessionStateNewSessionReceived, + eSessionStateNewSessionSent }; public: @@ -63,10 +64,9 @@ namespace garlic ECIESX25519AEADRatchetSession (GarlicDestination * owner); ~ECIESX25519AEADRatchetSession (); + bool HandleNextMessage (const uint8_t * buf, size_t len, CloveHandler handleClove); std::shared_ptr WrapSingleMessage (std::shared_ptr msg); - bool NewIncomingSession (const uint8_t * buf, size_t len, CloveHandler handleClove); - bool NewOutgoingSessionReply (const uint8_t * buf, size_t len, CloveHandler handleClove); const uint8_t * GetRemoteStaticKey () const { return m_RemoteStaticKey; } void SetRemoteStaticKey (const uint8_t * key) { memcpy (m_RemoteStaticKey, key, 32); } @@ -77,6 +77,8 @@ namespace garlic bool GenerateEphemeralKeysAndEncode (uint8_t * buf); // buf is 32 bytes uint64_t CreateNewSessionTag () const; + bool HandleNewIncomingSession (const uint8_t * buf, size_t len, CloveHandler handleClove); + bool HandleNewOutgoingSessionReply (const uint8_t * buf, size_t len, CloveHandler handleClove); void HandlePayload (const uint8_t * buf, size_t len, CloveHandler& handleClove); bool NewOutgoingSessionMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen); diff --git a/libi2pd/Garlic.cpp b/libi2pd/Garlic.cpp index b6a870f0..69ca0335 100644 --- a/libi2pd/Garlic.cpp +++ b/libi2pd/Garlic.cpp @@ -864,21 +864,18 @@ namespace garlic this, std::placeholders::_1, std::placeholders::_2); uint64_t tag; memcpy (&tag, buf, 8); + ECIESX25519AEADRatchetSessionPtr session; auto it = m_ECIESx25519Tags.find (tag); if (it != m_ECIESx25519Tags.end ()) - { - // TODO - auto session = it->second; - if (!session->NewOutgoingSessionReply (buf, len, handleClove)) - LogPrint (eLogError, "Garlic: can't decrypt ECIES-X25519-AEAD-Ratchet new session reply"); - m_ECIESx25519Tags.erase (tag); - } - else - { - auto session = std::make_shared (this); - if (!session->NewIncomingSession (buf, len, handleClove)) - LogPrint (eLogError, "Garlic: can't decrypt ECIES-X25519-AEAD-Ratchet new session"); - } + { + session = it->second; + m_ECIESx25519Tags.erase (tag); + } + else + session = std::make_shared (this); // incoming + + if (!session->HandleNextMessage (buf, len, handleClove)) + LogPrint (eLogError, "Garlic: can't handle ECIES-X25519-AEAD-Ratchet message"); } void GarlicDestination::HandleECIESx25519GarlicClove (const uint8_t * buf, size_t len)