Browse Source

incoming ECIESX25519AEADRatchet messages hanler

pull/1474/head
orignal 5 years ago
parent
commit
48fa10b080
  1. 19
      libi2pd/ECIESX25519AEADRatchetSession.cpp
  2. 8
      libi2pd/ECIESX25519AEADRatchetSession.h
  3. 23
      libi2pd/Garlic.cpp

19
libi2pd/ECIESX25519AEADRatchetSession.cpp

@ -86,7 +86,7 @@ namespace garlic
return tagsetNsr.GetNextSessionTag (); 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; if (!GetOwner ()) return false;
// we are Bob // we are Bob
@ -219,6 +219,7 @@ namespace garlic
} }
MixHash (out + offset, 16); // h = SHA256(h || ciphertext) MixHash (out + offset, 16); // h = SHA256(h || ciphertext)
m_State = eSessionStateNewSessionSent;
if (GetOwner ()) if (GetOwner ())
GetOwner ()->AddECIESx25519SessionTag (CreateNewSessionTag (), shared_from_this ()); GetOwner ()->AddECIESx25519SessionTag (CreateNewSessionTag (), shared_from_this ());
@ -272,7 +273,7 @@ namespace garlic
return true; 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 // we are Alice
LogPrint (eLogDebug, "Garlic: reply received"); LogPrint (eLogDebug, "Garlic: reply received");
@ -323,6 +324,20 @@ namespace garlic
return true; 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<I2NPMessage> ECIESX25519AEADRatchetSession::WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg) std::shared_ptr<I2NPMessage> ECIESX25519AEADRatchetSession::WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg)
{ {
auto m = NewI2NPMessage (); auto m = NewI2NPMessage ();

8
libi2pd/ECIESX25519AEADRatchetSession.h

@ -53,7 +53,8 @@ namespace garlic
enum SessionState enum SessionState
{ {
eSessionStateNew =0, eSessionStateNew =0,
eSessionStateNewSessionReceived eSessionStateNewSessionReceived,
eSessionStateNewSessionSent
}; };
public: public:
@ -63,10 +64,9 @@ namespace garlic
ECIESX25519AEADRatchetSession (GarlicDestination * owner); ECIESX25519AEADRatchetSession (GarlicDestination * owner);
~ECIESX25519AEADRatchetSession (); ~ECIESX25519AEADRatchetSession ();
bool HandleNextMessage (const uint8_t * buf, size_t len, CloveHandler handleClove);
std::shared_ptr<I2NPMessage> WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg); std::shared_ptr<I2NPMessage> WrapSingleMessage (std::shared_ptr<const I2NPMessage> 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; } const uint8_t * GetRemoteStaticKey () const { return m_RemoteStaticKey; }
void SetRemoteStaticKey (const uint8_t * key) { memcpy (m_RemoteStaticKey, key, 32); } 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 bool GenerateEphemeralKeysAndEncode (uint8_t * buf); // buf is 32 bytes
uint64_t CreateNewSessionTag () const; 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); 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); bool NewOutgoingSessionMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen);

23
libi2pd/Garlic.cpp

@ -864,21 +864,18 @@ namespace garlic
this, std::placeholders::_1, std::placeholders::_2); this, std::placeholders::_1, std::placeholders::_2);
uint64_t tag; uint64_t tag;
memcpy (&tag, buf, 8); memcpy (&tag, buf, 8);
ECIESX25519AEADRatchetSessionPtr session;
auto it = m_ECIESx25519Tags.find (tag); auto it = m_ECIESx25519Tags.find (tag);
if (it != m_ECIESx25519Tags.end ()) if (it != m_ECIESx25519Tags.end ())
{ {
// TODO session = it->second;
auto session = it->second; m_ECIESx25519Tags.erase (tag);
if (!session->NewOutgoingSessionReply (buf, len, handleClove)) }
LogPrint (eLogError, "Garlic: can't decrypt ECIES-X25519-AEAD-Ratchet new session reply"); else
m_ECIESx25519Tags.erase (tag); session = std::make_shared<ECIESX25519AEADRatchetSession> (this); // incoming
}
else if (!session->HandleNextMessage (buf, len, handleClove))
{ LogPrint (eLogError, "Garlic: can't handle ECIES-X25519-AEAD-Ratchet message");
auto session = std::make_shared<ECIESX25519AEADRatchetSession> (this);
if (!session->NewIncomingSession (buf, len, handleClove))
LogPrint (eLogError, "Garlic: can't decrypt ECIES-X25519-AEAD-Ratchet new session");
}
} }
void GarlicDestination::HandleECIESx25519GarlicClove (const uint8_t * buf, size_t len) void GarlicDestination::HandleECIESx25519GarlicClove (const uint8_t * buf, size_t len)

Loading…
Cancel
Save