i2pd/NTCPSession.cpp

659 lines
20 KiB
C++
Raw Normal View History

2013-09-10 05:35:46 +04:00
#include <string.h>
#include <stdlib.h>
#include "I2PEndian.h"
2013-09-10 05:35:46 +04:00
#include <cryptopp/dh.h>
#include "base64.h"
#include "Log.h"
2014-01-10 07:26:30 +04:00
#include "Timestamp.h"
2013-09-10 05:35:46 +04:00
#include "CryptoConst.h"
#include "I2NPProtocol.h"
#include "RouterContext.h"
#include "Transports.h"
2014-10-24 23:39:53 +04:00
#include "NetDb.h"
2013-09-10 05:35:46 +04:00
#include "NTCPSession.h"
using namespace i2p::crypto;
namespace i2p
{
namespace transport
2013-09-10 05:35:46 +04:00
{
NTCPSession::NTCPSession (boost::asio::io_service& service, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter):
TransportSession (in_RemoteRouter), m_Socket (service),
m_TerminationTimer (service), m_IsEstablished (false), m_ReceiveBufferOffset (0),
2014-10-21 00:09:59 +04:00
m_NextMessage (nullptr), m_NumSentBytes (0), m_NumReceivedBytes (0)
2013-09-10 05:35:46 +04:00
{
m_DHKeysPair = transports.GetNextDHKeysPair ();
2014-09-12 06:15:20 +04:00
m_Establisher = new Establisher;
2013-09-10 05:35:46 +04:00
}
2013-10-27 19:23:15 +04:00
2014-04-04 21:30:13 +04:00
NTCPSession::~NTCPSession ()
{
2014-09-12 06:15:20 +04:00
delete m_Establisher;
2014-08-27 18:02:23 +04:00
if (m_NextMessage)
i2p::DeleteI2NPMessage (m_NextMessage);
2014-09-22 21:28:46 +04:00
for (auto it :m_DelayedMessages)
i2p::DeleteI2NPMessage (it);
m_DelayedMessages.clear ();
2014-04-04 21:30:13 +04:00
}
2014-11-02 04:53:45 +03:00
void NTCPSession::CreateAESKey (uint8_t * pubKey, i2p::crypto::AESKey& key)
2013-09-10 05:35:46 +04:00
{
CryptoPP::DH dh (elgp, elgg);
2014-04-16 01:44:44 +04:00
uint8_t sharedKey[256];
if (!dh.Agree (sharedKey, m_DHKeysPair->privateKey, pubKey))
2013-09-10 05:35:46 +04:00
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogError, "Couldn't create shared key");
2013-09-10 05:35:46 +04:00
Terminate ();
return;
};
2014-11-02 04:53:45 +03:00
uint8_t * aesKey = key;
if (sharedKey[0] & 0x80)
2013-09-10 05:35:46 +04:00
{
aesKey[0] = 0;
memcpy (aesKey + 1, sharedKey, 31);
2013-09-10 05:35:46 +04:00
}
else if (sharedKey[0])
memcpy (aesKey, sharedKey, 32);
else
{
// find first non-zero byte
uint8_t * nonZero = sharedKey + 1;
while (!*nonZero)
{
nonZero++;
if (nonZero - sharedKey > 32)
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogWarning, "First 32 bytes of shared key is all zeros. Ignored");
return;
}
}
memcpy (aesKey, nonZero, 32);
}
2013-09-10 05:35:46 +04:00
}
void NTCPSession::Terminate ()
{
2013-10-23 06:43:29 +04:00
m_IsEstablished = false;
2013-09-10 05:35:46 +04:00
m_Socket.close ();
int numDelayed = 0;
2013-11-29 16:52:09 +04:00
for (auto it :m_DelayedMessages)
{
// try to send them again
2014-10-24 23:39:53 +04:00
if (m_RemoteRouter)
transports.SendMessage (m_RemoteRouter->GetIdentHash (), it);
numDelayed++;
}
2013-11-29 16:52:09 +04:00
m_DelayedMessages.clear ();
if (numDelayed > 0)
2014-11-25 20:33:51 +03:00
LogPrint (eLogWarning, "NTCP session ", numDelayed, " not sent");
2013-09-10 05:35:46 +04:00
// TODO: notify tunnels
2014-11-26 00:30:15 +03:00
transports.RemoveNTCPSession (shared_from_this ());
2013-10-27 19:23:15 +04:00
LogPrint ("NTCP session terminated");
2013-10-23 06:43:29 +04:00
}
void NTCPSession::Connected ()
{
m_IsEstablished = true;
2013-10-27 19:23:15 +04:00
2014-09-12 06:15:20 +04:00
delete m_Establisher;
m_Establisher = nullptr;
delete m_DHKeysPair;
m_DHKeysPair = nullptr;
2013-10-27 19:23:15 +04:00
SendTimeSyncMessage ();
SendI2NPMessage (CreateDatabaseStoreMsg ()); // we tell immediately who we are
2013-11-29 16:52:09 +04:00
if (!m_DelayedMessages.empty ())
2013-10-27 19:23:15 +04:00
{
2013-11-29 16:52:09 +04:00
for (auto it :m_DelayedMessages)
SendI2NPMessage (it);
m_DelayedMessages.clear ();
2013-10-27 19:23:15 +04:00
}
2013-09-10 05:35:46 +04:00
}
void NTCPSession::ClientLogin ()
{
2014-09-21 04:10:34 +04:00
if (!m_DHKeysPair)
m_DHKeysPair = transports.GetNextDHKeysPair ();
2013-09-10 05:35:46 +04:00
// send Phase1
2014-04-04 21:30:13 +04:00
const uint8_t * x = m_DHKeysPair->publicKey;
2014-09-12 06:15:20 +04:00
memcpy (m_Establisher->phase1.pubKey, x, 256);
CryptoPP::SHA256().CalculateDigest(m_Establisher->phase1.HXxorHI, x, 256);
2014-10-24 23:39:53 +04:00
const uint8_t * ident = m_RemoteIdentity.GetIdentHash ();
2013-09-10 05:35:46 +04:00
for (int i = 0; i < 32; i++)
2014-09-12 06:15:20 +04:00
m_Establisher->phase1.HXxorHI[i] ^= ident[i];
2013-09-10 05:35:46 +04:00
2014-09-12 06:15:20 +04:00
boost::asio::async_write (m_Socket, boost::asio::buffer (&m_Establisher->phase1, sizeof (NTCPPhase1)), boost::asio::transfer_all (),
2014-11-26 00:30:15 +03:00
std::bind(&NTCPSession::HandlePhase1Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
2013-09-10 05:35:46 +04:00
}
void NTCPSession::ServerLogin ()
{
// receive Phase1
boost::asio::async_read (m_Socket, boost::asio::buffer(&m_Establisher->phase1, sizeof (NTCPPhase1)), boost::asio::transfer_all (),
2014-11-26 00:30:15 +03:00
std::bind(&NTCPSession::HandlePhase1Received, shared_from_this (),
std::placeholders::_1, std::placeholders::_2));
2013-09-10 05:35:46 +04:00
}
void NTCPSession::HandlePhase1Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogWarning, "Couldn't send Phase 1 message: ", ecode.message ());
2014-09-22 21:28:46 +04:00
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
2013-09-10 05:35:46 +04:00
}
else
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogDebug, "Phase 1 sent: ", bytes_transferred);
boost::asio::async_read (m_Socket, boost::asio::buffer(&m_Establisher->phase2, sizeof (NTCPPhase2)), boost::asio::transfer_all (),
2014-11-26 00:30:15 +03:00
std::bind(&NTCPSession::HandlePhase2Received, shared_from_this (),
std::placeholders::_1, std::placeholders::_2));
2013-09-10 05:35:46 +04:00
}
}
void NTCPSession::HandlePhase1Received (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogError, "Phase 1 read error: ", ecode.message ());
2014-09-22 21:28:46 +04:00
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
2013-09-10 05:35:46 +04:00
}
else
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogDebug, "Phase 1 received: ", bytes_transferred);
2013-09-10 05:35:46 +04:00
// verify ident
uint8_t digest[32];
2014-09-12 06:15:20 +04:00
CryptoPP::SHA256().CalculateDigest(digest, m_Establisher->phase1.pubKey, 256);
2013-09-10 05:35:46 +04:00
const uint8_t * ident = i2p::context.GetRouterInfo ().GetIdentHash ();
for (int i = 0; i < 32; i++)
{
2014-09-12 06:15:20 +04:00
if ((m_Establisher->phase1.HXxorHI[i] ^ ident[i]) != digest[i])
2013-09-10 05:35:46 +04:00
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogError, "Wrong ident");
2013-09-10 05:35:46 +04:00
Terminate ();
return;
}
}
SendPhase2 ();
}
}
void NTCPSession::SendPhase2 ()
{
2014-09-21 04:10:34 +04:00
if (!m_DHKeysPair)
m_DHKeysPair = transports.GetNextDHKeysPair ();
2014-04-04 21:30:13 +04:00
const uint8_t * y = m_DHKeysPair->publicKey;
2014-09-12 06:15:20 +04:00
memcpy (m_Establisher->phase2.pubKey, y, 256);
2013-09-10 05:35:46 +04:00
uint8_t xy[512];
2014-09-12 06:15:20 +04:00
memcpy (xy, m_Establisher->phase1.pubKey, 256);
2013-09-10 05:35:46 +04:00
memcpy (xy + 256, y, 256);
2014-09-12 06:15:20 +04:00
CryptoPP::SHA256().CalculateDigest(m_Establisher->phase2.encrypted.hxy, xy, 512);
2014-01-10 07:26:30 +04:00
uint32_t tsB = htobe32 (i2p::util::GetSecondsSinceEpoch ());
2014-09-12 06:15:20 +04:00
m_Establisher->phase2.encrypted.timestamp = tsB;
2013-09-10 05:35:46 +04:00
// TODO: fill filler
2014-11-02 04:53:45 +03:00
i2p::crypto::AESKey aesKey;
2014-09-12 06:15:20 +04:00
CreateAESKey (m_Establisher->phase1.pubKey, aesKey);
2014-05-07 06:30:09 +04:00
m_Encryption.SetKey (aesKey);
m_Encryption.SetIV (y + 240);
m_Decryption.SetKey (aesKey);
2014-09-12 06:15:20 +04:00
m_Decryption.SetIV (m_Establisher->phase1.HXxorHI + 16);
2013-09-10 05:35:46 +04:00
2014-09-12 06:15:20 +04:00
m_Encryption.Encrypt ((uint8_t *)&m_Establisher->phase2.encrypted, sizeof(m_Establisher->phase2.encrypted), (uint8_t *)&m_Establisher->phase2.encrypted);
boost::asio::async_write (m_Socket, boost::asio::buffer (&m_Establisher->phase2, sizeof (NTCPPhase2)), boost::asio::transfer_all (),
2014-11-26 00:30:15 +03:00
std::bind(&NTCPSession::HandlePhase2Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, tsB));
2013-09-10 05:35:46 +04:00
}
void NTCPSession::HandlePhase2Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsB)
{
if (ecode)
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogWarning, "Couldn't send Phase 2 message: ", ecode.message ());
2014-09-22 21:28:46 +04:00
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
2013-09-10 05:35:46 +04:00
}
else
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogDebug, "Phase 2 sent: ", bytes_transferred);
boost::asio::async_read (m_Socket, boost::asio::buffer(m_ReceiveBuffer, NTCP_DEFAULT_PHASE3_SIZE), boost::asio::transfer_all (),
2014-11-26 00:30:15 +03:00
std::bind(&NTCPSession::HandlePhase3Received, shared_from_this (),
std::placeholders::_1, std::placeholders::_2, tsB));
2013-09-10 05:35:46 +04:00
}
}
void NTCPSession::HandlePhase2Received (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
2013-12-29 19:48:57 +04:00
LogPrint ("Phase 2 read error: ", ecode.message (), ". Wrong ident assumed");
2014-09-22 21:28:46 +04:00
if (ecode != boost::asio::error::operation_aborted)
{
2014-10-24 23:39:53 +04:00
// this RI is not valid
i2p::data::netdb.SetUnreachable (GetRemoteIdentity ().GetIdentHash (), true);
transports.ReuseDHKeysPair (m_DHKeysPair);
2014-09-22 21:28:46 +04:00
m_DHKeysPair = nullptr;
Terminate ();
}
2013-09-10 05:35:46 +04:00
}
else
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogDebug, "Phase 2 received: ", bytes_transferred);
2013-09-10 05:35:46 +04:00
2014-11-02 04:53:45 +03:00
i2p::crypto::AESKey aesKey;
2014-09-12 06:15:20 +04:00
CreateAESKey (m_Establisher->phase2.pubKey, aesKey);
2014-05-07 06:30:09 +04:00
m_Decryption.SetKey (aesKey);
2014-09-12 06:15:20 +04:00
m_Decryption.SetIV (m_Establisher->phase2.pubKey + 240);
2014-05-07 06:30:09 +04:00
m_Encryption.SetKey (aesKey);
2014-09-12 06:15:20 +04:00
m_Encryption.SetIV (m_Establisher->phase1.HXxorHI + 16);
2013-09-10 05:35:46 +04:00
2014-09-12 06:15:20 +04:00
m_Decryption.Decrypt((uint8_t *)&m_Establisher->phase2.encrypted, sizeof(m_Establisher->phase2.encrypted), (uint8_t *)&m_Establisher->phase2.encrypted);
2013-09-10 05:35:46 +04:00
// verify
2014-12-22 05:32:21 +03:00
uint8_t xy[512];
2014-04-04 21:30:13 +04:00
memcpy (xy, m_DHKeysPair->publicKey, 256);
2014-09-12 06:15:20 +04:00
memcpy (xy + 256, m_Establisher->phase2.pubKey, 256);
2014-12-22 05:32:21 +03:00
if (!CryptoPP::SHA256().VerifyDigest(m_Establisher->phase2.encrypted.hxy, xy, 512))
2013-09-10 05:35:46 +04:00
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogError, "Incorrect hash");
transports.ReuseDHKeysPair (m_DHKeysPair);
2014-09-18 19:11:51 +04:00
m_DHKeysPair = nullptr;
2013-09-10 05:35:46 +04:00
Terminate ();
return ;
}
SendPhase3 ();
}
}
void NTCPSession::SendPhase3 ()
{
2014-11-25 18:59:29 +03:00
auto keys = i2p::context.GetPrivateKeys ();
uint8_t * buf = m_ReceiveBuffer;
htobe16buf (buf, keys.GetPublic ().GetFullLen ());
2014-11-25 18:59:29 +03:00
buf += 2;
buf += i2p::context.GetIdentity ().ToBuffer (buf, NTCP_BUFFER_SIZE);
2014-01-10 07:26:30 +04:00
uint32_t tsA = htobe32 (i2p::util::GetSecondsSinceEpoch ());
htobuf32(buf,tsA);
2014-11-25 18:59:29 +03:00
buf += 4;
size_t signatureLen = keys.GetPublic ().GetSignatureLen ();
size_t len = (buf - m_ReceiveBuffer) + signatureLen;
size_t paddingSize = len & 0x0F; // %16
if (paddingSize > 0)
{
paddingSize = 16 - paddingSize;
// TODO: fill padding with random data
buf += paddingSize;
len += paddingSize;
}
2013-09-10 05:35:46 +04:00
SignedData s;
s.Insert (m_Establisher->phase1.pubKey, 256); // x
s.Insert (m_Establisher->phase2.pubKey, 256); // y
2014-10-28 18:47:28 +03:00
s.Insert (m_RemoteIdentity.GetIdentHash (), 32); // ident
s.Insert (tsA); // tsA
s.Insert (m_Establisher->phase2.encrypted.timestamp); // tsB
2014-11-25 18:59:29 +03:00
s.Sign (keys, buf);
2013-09-10 05:35:46 +04:00
2014-11-25 18:59:29 +03:00
m_Encryption.Encrypt(m_ReceiveBuffer, len, m_ReceiveBuffer);
boost::asio::async_write (m_Socket, boost::asio::buffer (m_ReceiveBuffer, len), boost::asio::transfer_all (),
2014-11-26 00:30:15 +03:00
std::bind(&NTCPSession::HandlePhase3Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, tsA));
2013-09-10 05:35:46 +04:00
}
void NTCPSession::HandlePhase3Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA)
{
if (ecode)
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogWarning, "Couldn't send Phase 3 message: ", ecode.message ());
2014-09-22 21:28:46 +04:00
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
2013-09-10 05:35:46 +04:00
}
else
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogDebug, "Phase 3 sent: ", bytes_transferred);
2014-11-24 23:26:57 +03:00
// wait for phase4
2014-11-25 18:14:18 +03:00
auto signatureLen = m_RemoteIdentity.GetSignatureLen ();
size_t paddingSize = signatureLen & 0x0F; // %16
if (paddingSize > 0) signatureLen += (16 - paddingSize);
boost::asio::async_read (m_Socket, boost::asio::buffer(m_ReceiveBuffer, signatureLen), boost::asio::transfer_all (),
2014-11-26 00:30:15 +03:00
std::bind(&NTCPSession::HandlePhase4Received, shared_from_this (),
std::placeholders::_1, std::placeholders::_2, tsA));
2013-09-10 05:35:46 +04:00
}
}
void NTCPSession::HandlePhase3Received (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsB)
{
if (ecode)
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogError, "Phase 3 read error: ", ecode.message ());
2014-09-22 21:28:46 +04:00
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
2013-09-10 05:35:46 +04:00
}
else
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogDebug, "Phase 3 received: ", bytes_transferred);
m_Decryption.Decrypt (m_ReceiveBuffer, bytes_transferred, m_ReceiveBuffer);
uint8_t * buf = m_ReceiveBuffer;
uint16_t size = bufbe16toh (buf);
2014-11-25 20:33:51 +03:00
m_RemoteIdentity.FromBuffer (buf + 2, size);
size_t expectedSize = size + 2/*size*/ + 4/*timestamp*/ + m_RemoteIdentity.GetSignatureLen ();
size_t paddingLen = expectedSize & 0x0F;
if (paddingLen) paddingLen = (16 - paddingLen);
if (expectedSize > NTCP_DEFAULT_PHASE3_SIZE)
{
// we need more bytes for Phase3
expectedSize += paddingLen;
LogPrint (eLogDebug, "Wait for ", expectedSize, " more bytes for Phase3");
boost::asio::async_read (m_Socket, boost::asio::buffer(m_ReceiveBuffer + NTCP_DEFAULT_PHASE3_SIZE, expectedSize), boost::asio::transfer_all (),
2014-11-26 00:30:15 +03:00
std::bind(&NTCPSession::HandlePhase3ExtraReceived, shared_from_this (),
std::placeholders::_1, std::placeholders::_2, tsB, paddingLen));
2014-11-25 20:33:51 +03:00
}
2014-11-25 22:29:06 +03:00
else
HandlePhase3 (tsB, paddingLen);
2014-11-25 20:33:51 +03:00
}
}
void NTCPSession::HandlePhase3ExtraReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsB, size_t paddingLen)
{
if (ecode)
{
LogPrint (eLogError, "Phase 3 extra read error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted)
2013-09-10 05:35:46 +04:00
Terminate ();
2014-11-25 20:33:51 +03:00
}
else
{
LogPrint (eLogDebug, "Phase 3 extra received: ", bytes_transferred);
m_Decryption.Decrypt (m_ReceiveBuffer + NTCP_DEFAULT_PHASE3_SIZE, bytes_transferred, m_ReceiveBuffer+ NTCP_DEFAULT_PHASE3_SIZE);
HandlePhase3 (tsB, paddingLen);
}
}
void NTCPSession::HandlePhase3 (uint32_t tsB, size_t paddingLen)
{
uint8_t * buf = m_ReceiveBuffer + m_RemoteIdentity.GetFullLen () + 2 /*size*/;
uint32_t tsA = buf32toh(buf);
2014-11-25 20:33:51 +03:00
buf += 4;
buf += paddingLen;
2013-09-10 05:35:46 +04:00
2014-11-25 20:33:51 +03:00
SignedData s;
s.Insert (m_Establisher->phase1.pubKey, 256); // x
s.Insert (m_Establisher->phase2.pubKey, 256); // y
s.Insert (i2p::context.GetRouterInfo ().GetIdentHash (), 32); // ident
s.Insert (tsA); // tsA
s.Insert (tsB); // tsB
if (!s.Verify (m_RemoteIdentity, buf))
{
LogPrint (eLogError, "signature verification failed");
Terminate ();
return;
2013-09-10 05:35:46 +04:00
}
2014-11-25 20:33:51 +03:00
SendPhase4 (tsA, tsB);
2013-09-10 05:35:46 +04:00
}
2014-11-25 18:35:35 +03:00
void NTCPSession::SendPhase4 (uint32_t tsA, uint32_t tsB)
2013-09-10 05:35:46 +04:00
{
SignedData s;
s.Insert (m_Establisher->phase1.pubKey, 256); // x
s.Insert (m_Establisher->phase2.pubKey, 256); // y
s.Insert (m_RemoteIdentity.GetIdentHash (), 32); // ident
2014-11-25 18:35:35 +03:00
s.Insert (tsA); // tsA
s.Insert (tsB); // tsB
2014-11-24 23:26:57 +03:00
auto keys = i2p::context.GetPrivateKeys ();
2014-11-25 18:14:18 +03:00
auto signatureLen = keys.GetPublic ().GetSignatureLen ();
s.Sign (keys, m_ReceiveBuffer);
size_t paddingSize = signatureLen & 0x0F; // %16
if (paddingSize > 0) signatureLen += (16 - paddingSize);
m_Encryption.Encrypt (m_ReceiveBuffer, signatureLen, m_ReceiveBuffer);
2013-09-10 05:35:46 +04:00
2014-11-25 18:14:18 +03:00
boost::asio::async_write (m_Socket, boost::asio::buffer (m_ReceiveBuffer, signatureLen), boost::asio::transfer_all (),
2014-11-26 00:30:15 +03:00
std::bind(&NTCPSession::HandlePhase4Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
2013-09-10 05:35:46 +04:00
}
void NTCPSession::HandlePhase4Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogWarning, "Couldn't send Phase 4 message: ", ecode.message ());
2014-09-22 21:28:46 +04:00
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
2013-09-10 05:35:46 +04:00
}
else
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogDebug, "Phase 4 sent: ", bytes_transferred);
2014-11-25 22:29:06 +03:00
LogPrint ("NTCP server session connected");
2014-11-26 00:30:15 +03:00
transports.AddNTCPSession (shared_from_this ());
2014-11-25 22:29:06 +03:00
2013-10-23 06:43:29 +04:00
Connected ();
2013-09-10 05:35:46 +04:00
m_ReceiveBufferOffset = 0;
2013-10-27 19:23:15 +04:00
m_NextMessage = nullptr;
2013-09-10 05:35:46 +04:00
Receive ();
}
}
void NTCPSession::HandlePhase4Received (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA)
{
if (ecode)
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogError, "Phase 4 read error: ", ecode.message ());
2014-09-22 21:28:46 +04:00
if (ecode != boost::asio::error::operation_aborted)
{
2014-10-24 23:39:53 +04:00
// this router doesn't like us
i2p::data::netdb.SetUnreachable (GetRemoteIdentity ().GetIdentHash (), true);
2014-09-22 21:28:46 +04:00
Terminate ();
}
2013-09-10 05:35:46 +04:00
}
else
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogDebug, "Phase 4 received: ", bytes_transferred);
2014-11-25 18:14:18 +03:00
m_Decryption.Decrypt(m_ReceiveBuffer, bytes_transferred, m_ReceiveBuffer);
2013-09-10 05:35:46 +04:00
// verify signature
SignedData s;
s.Insert (m_Establisher->phase1.pubKey, 256); // x
s.Insert (m_Establisher->phase2.pubKey, 256); // y
s.Insert (i2p::context.GetRouterInfo ().GetIdentHash (), 32); // ident
s.Insert (tsA); // tsA
s.Insert (m_Establisher->phase2.encrypted.timestamp); // tsB
2013-09-10 05:35:46 +04:00
2014-11-25 18:14:18 +03:00
if (!s.Verify (m_RemoteIdentity, m_ReceiveBuffer))
2013-09-10 05:35:46 +04:00
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogError, "signature verification failed");
2013-09-10 05:35:46 +04:00
Terminate ();
return;
}
2014-11-25 22:29:06 +03:00
LogPrint ("NTCP session connected");
2013-10-23 06:43:29 +04:00
Connected ();
2013-10-27 19:23:15 +04:00
2013-09-10 05:35:46 +04:00
m_ReceiveBufferOffset = 0;
2013-10-27 19:23:15 +04:00
m_NextMessage = nullptr;
2013-09-10 05:35:46 +04:00
Receive ();
}
}
void NTCPSession::Receive ()
{
2014-09-12 06:15:20 +04:00
m_Socket.async_read_some (boost::asio::buffer(m_ReceiveBuffer + m_ReceiveBufferOffset, NTCP_BUFFER_SIZE - m_ReceiveBufferOffset),
2014-11-26 00:30:15 +03:00
std::bind(&NTCPSession::HandleReceived, shared_from_this (),
std::placeholders::_1, std::placeholders::_2));
2013-09-10 05:35:46 +04:00
}
void NTCPSession::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogError, "Read error: ", ecode.message ());
2014-11-02 00:15:59 +03:00
//if (ecode != boost::asio::error::operation_aborted)
2014-09-22 21:28:46 +04:00
Terminate ();
2013-09-10 05:35:46 +04:00
}
else
{
m_NumReceivedBytes += bytes_transferred;
2013-09-10 05:35:46 +04:00
m_ReceiveBufferOffset += bytes_transferred;
2013-10-27 19:23:15 +04:00
if (m_ReceiveBufferOffset >= 16)
2013-09-10 05:35:46 +04:00
{
2013-10-27 19:23:15 +04:00
uint8_t * nextBlock = m_ReceiveBuffer;
while (m_ReceiveBufferOffset >= 16)
{
2014-09-18 19:11:51 +04:00
if (!DecryptNextBlock (nextBlock)) // 16 bytes
{
Terminate ();
return;
}
2013-10-27 19:23:15 +04:00
nextBlock += 16;
m_ReceiveBufferOffset -= 16;
2013-09-10 05:35:46 +04:00
}
2013-10-27 19:23:15 +04:00
if (m_ReceiveBufferOffset > 0)
memcpy (m_ReceiveBuffer, nextBlock, m_ReceiveBufferOffset);
2013-09-10 05:35:46 +04:00
}
ScheduleTermination (); // reset termination timer
2013-09-10 05:35:46 +04:00
Receive ();
}
}
2014-09-18 19:11:51 +04:00
bool NTCPSession::DecryptNextBlock (const uint8_t * encrypted) // 16 bytes
2013-09-10 05:35:46 +04:00
{
2013-10-27 19:23:15 +04:00
if (!m_NextMessage) // new message, header expected
2013-09-10 05:35:46 +04:00
{
2013-10-27 19:23:15 +04:00
m_NextMessage = i2p::NewI2NPMessage ();
m_NextMessageOffset = 0;
2014-05-14 22:54:01 +04:00
m_Decryption.Decrypt (encrypted, m_NextMessage->buf);
uint16_t dataSize = bufbe16toh (m_NextMessage->buf);
2013-10-27 19:23:15 +04:00
if (dataSize)
{
// new message
2014-08-27 18:02:23 +04:00
if (dataSize > NTCP_MAX_MESSAGE_SIZE)
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogError, "NTCP data size ", dataSize, " exceeds max size");
2014-08-27 18:02:23 +04:00
i2p::DeleteI2NPMessage (m_NextMessage);
m_NextMessage = nullptr;
2014-09-18 19:11:51 +04:00
return false;
2014-08-27 18:02:23 +04:00
}
m_NextMessageOffset += 16;
m_NextMessage->offset = 2; // size field
m_NextMessage->len = dataSize + 2;
2013-09-10 05:35:46 +04:00
}
2013-10-27 19:23:15 +04:00
else
{
2013-10-27 19:23:15 +04:00
// timestamp
LogPrint ("Timestamp");
i2p::DeleteI2NPMessage (m_NextMessage);
m_NextMessage = nullptr;
2014-09-18 19:11:51 +04:00
return true;
}
2013-10-27 19:23:15 +04:00
}
else // message continues
{
2014-05-14 22:54:01 +04:00
m_Decryption.Decrypt (encrypted, m_NextMessage->buf + m_NextMessageOffset);
2013-10-27 19:23:15 +04:00
m_NextMessageOffset += 16;
}
2013-09-10 05:35:46 +04:00
2013-10-27 19:23:15 +04:00
if (m_NextMessageOffset >= m_NextMessage->len + 4) // +checksum
{
// we have a complete I2NP message
i2p::HandleI2NPMessage (m_NextMessage);
m_NextMessage = nullptr;
2014-09-18 19:11:51 +04:00
}
return true;
2013-10-27 19:23:15 +04:00
}
2013-09-10 05:35:46 +04:00
void NTCPSession::Send (i2p::I2NPMessage * msg)
2013-09-10 05:35:46 +04:00
{
uint8_t * sendBuffer;
int len;
if (msg)
{
// regular I2NP
if (msg->offset < 2)
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogError, "Malformed I2NP message");
i2p::DeleteI2NPMessage (msg);
}
sendBuffer = msg->GetBuffer () - 2;
len = msg->GetLength ();
htobe16buf (sendBuffer, len);
}
else
{
// prepare timestamp
sendBuffer = m_TimeSyncBuffer;
len = 4;
htobuf16(sendBuffer, 0);
htobe32buf (sendBuffer + 2, time (0));
}
int rem = (len + 6) & 0x0F; // %16
2013-09-10 05:35:46 +04:00
int padding = 0;
if (rem > 0) padding = 16 - rem;
// TODO: fill padding
2013-10-23 06:43:29 +04:00
m_Adler.CalculateDigest (sendBuffer + len + 2 + padding, sendBuffer, len + 2+ padding);
2013-09-10 05:35:46 +04:00
int l = len + padding + 6;
2014-05-07 06:30:09 +04:00
m_Encryption.Encrypt(sendBuffer, l, sendBuffer);
2013-09-10 05:35:46 +04:00
2013-10-23 06:43:29 +04:00
boost::asio::async_write (m_Socket, boost::asio::buffer (sendBuffer, l), boost::asio::transfer_all (),
2014-11-26 00:30:15 +03:00
std::bind(&NTCPSession::HandleSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, msg));
2013-09-10 05:35:46 +04:00
}
void NTCPSession::HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, i2p::I2NPMessage * msg)
2013-09-10 05:35:46 +04:00
{
if (msg)
i2p::DeleteI2NPMessage (msg);
2013-09-10 05:35:46 +04:00
if (ecode)
{
2014-11-25 20:33:51 +03:00
LogPrint (eLogWarning, "Couldn't send msg: ", ecode.message ());
2014-02-03 19:51:01 +04:00
// we shouldn't call Terminate () here, because HandleReceive takes care
// TODO: 'delete this' statement in Terminate () must be eliminated later
// Terminate ();
2013-09-10 05:35:46 +04:00
}
else
{
m_NumSentBytes += bytes_transferred;
ScheduleTermination (); // reset termination timer
2013-09-10 05:35:46 +04:00
}
}
void NTCPSession::SendTimeSyncMessage ()
{
Send (nullptr);
2013-09-10 05:35:46 +04:00
}
2013-10-23 06:43:29 +04:00
void NTCPSession::SendI2NPMessage (I2NPMessage * msg)
{
if (msg)
{
2013-10-27 19:23:15 +04:00
if (m_IsEstablished)
Send (msg);
2013-10-27 19:23:15 +04:00
else
2013-11-29 16:52:09 +04:00
m_DelayedMessages.push_back (msg);
}
}
void NTCPSession::ScheduleTermination ()
{
m_TerminationTimer.cancel ();
2014-04-08 05:40:28 +04:00
m_TerminationTimer.expires_from_now (boost::posix_time::seconds(NTCP_TERMINATION_TIMEOUT));
2014-11-26 00:30:15 +03:00
m_TerminationTimer.async_wait (std::bind (&NTCPSession::HandleTerminationTimer,
shared_from_this (), std::placeholders::_1));
2013-11-29 16:52:09 +04:00
}
void NTCPSession::HandleTerminationTimer (const boost::system::error_code& ecode)
{
if (ecode != boost::asio::error::operation_aborted)
{
2014-04-08 05:40:28 +04:00
LogPrint ("No activity fo ", NTCP_TERMINATION_TIMEOUT, " seconds");
2014-11-02 00:15:59 +03:00
//Terminate ();
m_Socket.close ();// invoke Terminate () from HandleReceive
2013-10-23 06:43:29 +04:00
}
}
2013-09-10 05:35:46 +04:00
}
}