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 @@ -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 @@ -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 @@ -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 @@ -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<I2NPMessage> ECIESX25519AEADRatchetSession::WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg)
{
auto m = NewI2NPMessage ();

8
libi2pd/ECIESX25519AEADRatchetSession.h

@ -53,7 +53,8 @@ namespace garlic @@ -53,7 +53,8 @@ namespace garlic
enum SessionState
{
eSessionStateNew =0,
eSessionStateNewSessionReceived
eSessionStateNewSessionReceived,
eSessionStateNewSessionSent
};
public:
@ -63,10 +64,9 @@ namespace garlic @@ -63,10 +64,9 @@ namespace garlic
ECIESX25519AEADRatchetSession (GarlicDestination * owner);
~ECIESX25519AEADRatchetSession ();
bool HandleNextMessage (const uint8_t * buf, size_t len, CloveHandler handleClove);
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; }
void SetRemoteStaticKey (const uint8_t * key) { memcpy (m_RemoteStaticKey, key, 32); }
@ -77,6 +77,8 @@ namespace garlic @@ -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);

23
libi2pd/Garlic.cpp

@ -864,21 +864,18 @@ namespace garlic @@ -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<ECIESX25519AEADRatchetSession> (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<ECIESX25519AEADRatchetSession> (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)

Loading…
Cancel
Save