2018-08-16 17:46:59 +00:00
|
|
|
/*
|
2020-02-02 22:05:30 +00:00
|
|
|
* Copyright (c) 2013-2020, The PurpleI2P Project
|
2018-08-16 17:46:59 +00:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-06-05 16:53:13 +00:00
|
|
|
#include <openssl/rand.h>
|
2018-06-05 19:37:08 +00:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/hmac.h>
|
2018-06-06 19:38:18 +00:00
|
|
|
#include <stdlib.h>
|
2018-06-13 20:16:23 +00:00
|
|
|
#include <vector>
|
2018-06-06 19:38:18 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include "I2PEndian.h"
|
2018-06-05 16:53:13 +00:00
|
|
|
#include "Crypto.h"
|
2018-06-21 16:39:24 +00:00
|
|
|
#include "Siphash.h"
|
2018-06-15 18:56:03 +00:00
|
|
|
#include "RouterContext.h"
|
2018-07-13 19:59:28 +00:00
|
|
|
#include "Transports.h"
|
2018-07-18 19:57:18 +00:00
|
|
|
#include "NetDb.hpp"
|
2018-06-05 16:53:13 +00:00
|
|
|
#include "NTCP2.h"
|
2020-03-01 14:35:24 +00:00
|
|
|
#include "HTTP.h"
|
|
|
|
#include "util.h"
|
2018-06-05 16:53:13 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace transport
|
|
|
|
{
|
2018-08-09 16:53:36 +00:00
|
|
|
NTCP2Establisher::NTCP2Establisher ():
|
2020-03-01 14:35:24 +00:00
|
|
|
m_SessionRequestBuffer (nullptr), m_SessionCreatedBuffer (nullptr), m_SessionConfirmedBuffer (nullptr)
|
|
|
|
{
|
2018-07-04 18:15:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 14:35:24 +00:00
|
|
|
NTCP2Establisher::~NTCP2Establisher ()
|
|
|
|
{
|
|
|
|
delete[] m_SessionRequestBuffer;
|
2018-08-09 16:53:36 +00:00
|
|
|
delete[] m_SessionCreatedBuffer;
|
|
|
|
delete[] m_SessionConfirmedBuffer;
|
2018-07-04 18:15:40 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 20:06:39 +00:00
|
|
|
void NTCP2Establisher::MixKey (const uint8_t * inputKeyMaterial)
|
2018-06-14 19:29:36 +00:00
|
|
|
{
|
2020-01-18 19:43:36 +00:00
|
|
|
i2p::crypto::HKDF (m_CK, inputKeyMaterial, 32, "", m_CK);
|
|
|
|
// ck is m_CK[0:31], k is m_CK[32:63]
|
2018-11-01 20:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Establisher::MixHash (const uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
SHA256_CTX ctx;
|
|
|
|
SHA256_Init (&ctx);
|
|
|
|
SHA256_Update (&ctx, m_H, 32);
|
|
|
|
SHA256_Update (&ctx, buf, len);
|
|
|
|
SHA256_Final (m_H, &ctx);
|
2018-06-14 19:29:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 02:08:08 +00:00
|
|
|
void NTCP2Establisher::KeyDerivationFunction1 (const uint8_t * pub, i2p::crypto::X25519Keys& priv, const uint8_t * rs, const uint8_t * epub)
|
2018-06-05 19:37:08 +00:00
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
static const uint8_t protocolNameHash[] =
|
|
|
|
{
|
|
|
|
0x72, 0xe8, 0x42, 0xc5, 0x45, 0xe1, 0x80, 0x80, 0xd3, 0x9c, 0x44, 0x93, 0xbb, 0x91, 0xd7, 0xed,
|
|
|
|
0xf2, 0x28, 0x98, 0x17, 0x71, 0x21, 0x8c, 0x1f, 0x62, 0x4e, 0x20, 0x6f, 0x28, 0xd3, 0x2f, 0x71
|
2018-06-20 20:09:22 +00:00
|
|
|
}; // SHA256 ("Noise_XKaesobfse+hs2+hs3_25519_ChaChaPoly_SHA256")
|
2020-03-01 14:35:24 +00:00
|
|
|
static const uint8_t hh[32] =
|
|
|
|
{
|
|
|
|
0x49, 0xff, 0x48, 0x3f, 0xc4, 0x04, 0xb9, 0xb2, 0x6b, 0x11, 0x94, 0x36, 0x72, 0xff, 0x05, 0xb5,
|
|
|
|
0x61, 0x27, 0x03, 0x31, 0xba, 0x89, 0xb8, 0xfc, 0x33, 0x15, 0x93, 0x87, 0x57, 0xdd, 0x3d, 0x1e
|
2018-06-20 20:09:22 +00:00
|
|
|
}; // SHA256 (protocolNameHash)
|
2020-03-01 14:35:24 +00:00
|
|
|
memcpy (m_CK, protocolNameHash, 32);
|
2018-07-19 13:27:59 +00:00
|
|
|
// h = SHA256(hh || rs)
|
|
|
|
SHA256_CTX ctx;
|
|
|
|
SHA256_Init (&ctx);
|
2020-03-01 14:35:24 +00:00
|
|
|
SHA256_Update (&ctx, hh, 32);
|
|
|
|
SHA256_Update (&ctx, rs, 32);
|
2018-07-19 13:27:59 +00:00
|
|
|
SHA256_Final (m_H, &ctx);
|
2018-07-31 19:41:13 +00:00
|
|
|
// h = SHA256(h || epub)
|
2018-11-01 20:06:39 +00:00
|
|
|
MixHash (epub, 32);
|
2018-09-09 02:08:08 +00:00
|
|
|
// x25519 between pub and priv
|
2018-06-05 19:37:08 +00:00
|
|
|
uint8_t inputKeyMaterial[32];
|
2018-09-09 02:08:08 +00:00
|
|
|
priv.Agree (pub, inputKeyMaterial);
|
2018-11-01 20:06:39 +00:00
|
|
|
MixKey (inputKeyMaterial);
|
2018-07-04 18:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Establisher::KDF1Alice ()
|
|
|
|
{
|
2020-06-30 00:02:09 +00:00
|
|
|
KeyDerivationFunction1 (m_RemoteStaticKey, *m_EphemeralKeys, m_RemoteStaticKey, GetPub ());
|
2018-07-04 18:15:40 +00:00
|
|
|
}
|
2020-03-01 14:35:24 +00:00
|
|
|
|
2018-07-04 18:15:40 +00:00
|
|
|
void NTCP2Establisher::KDF1Bob ()
|
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
KeyDerivationFunction1 (GetRemotePub (), i2p::context.GetStaticKeys (), i2p::context.GetNTCP2StaticPublicKey (), GetRemotePub ());
|
2018-06-13 20:16:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 19:41:13 +00:00
|
|
|
void NTCP2Establisher::KeyDerivationFunction2 (const uint8_t * sessionRequest, size_t sessionRequestLen, const uint8_t * epub)
|
2020-03-01 14:35:24 +00:00
|
|
|
{
|
|
|
|
MixHash (sessionRequest + 32, 32); // encrypted payload
|
2018-07-19 13:27:59 +00:00
|
|
|
|
2018-06-13 20:16:23 +00:00
|
|
|
int paddingLength = sessionRequestLen - 64;
|
|
|
|
if (paddingLength > 0)
|
2018-11-01 20:06:39 +00:00
|
|
|
MixHash (sessionRequest + 64, paddingLength);
|
2020-03-01 14:35:24 +00:00
|
|
|
MixHash (epub, 32);
|
2018-06-14 14:45:25 +00:00
|
|
|
|
2018-09-08 20:52:42 +00:00
|
|
|
// x25519 between remote pub and ephemaral priv
|
2018-06-13 20:16:23 +00:00
|
|
|
uint8_t inputKeyMaterial[32];
|
2020-06-30 00:02:09 +00:00
|
|
|
m_EphemeralKeys->Agree (GetRemotePub (), inputKeyMaterial);
|
2020-03-01 14:35:24 +00:00
|
|
|
|
2018-11-01 20:06:39 +00:00
|
|
|
MixKey (inputKeyMaterial);
|
2018-06-14 19:29:36 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 16:53:36 +00:00
|
|
|
void NTCP2Establisher::KDF2Alice ()
|
2018-07-31 19:41:13 +00:00
|
|
|
{
|
2018-08-09 16:53:36 +00:00
|
|
|
KeyDerivationFunction2 (m_SessionRequestBuffer, m_SessionRequestBufferLen, GetRemotePub ());
|
2018-07-31 19:41:13 +00:00
|
|
|
}
|
2020-03-01 14:35:24 +00:00
|
|
|
|
2018-08-09 16:53:36 +00:00
|
|
|
void NTCP2Establisher::KDF2Bob ()
|
2018-07-31 19:41:13 +00:00
|
|
|
{
|
2020-03-01 10:25:50 +00:00
|
|
|
KeyDerivationFunction2 (m_SessionRequestBuffer, m_SessionRequestBufferLen, GetPub ());
|
2018-07-31 19:41:13 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 19:56:23 +00:00
|
|
|
void NTCP2Establisher::KDF3Alice ()
|
2018-07-04 18:15:40 +00:00
|
|
|
{
|
|
|
|
uint8_t inputKeyMaterial[32];
|
2020-03-01 14:35:24 +00:00
|
|
|
i2p::context.GetStaticKeys ().Agree (GetRemotePub (), inputKeyMaterial);
|
2018-11-01 20:06:39 +00:00
|
|
|
MixKey (inputKeyMaterial);
|
2018-07-09 19:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Establisher::KDF3Bob ()
|
|
|
|
{
|
|
|
|
uint8_t inputKeyMaterial[32];
|
2020-06-30 00:02:09 +00:00
|
|
|
m_EphemeralKeys->Agree (m_RemoteStaticKey, inputKeyMaterial);
|
2018-11-01 20:06:39 +00:00
|
|
|
MixKey (inputKeyMaterial);
|
2018-07-04 18:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Establisher::CreateEphemeralKey ()
|
2018-07-03 20:26:02 +00:00
|
|
|
{
|
2020-06-30 00:02:09 +00:00
|
|
|
m_EphemeralKeys = i2p::transport::transports.GetNextX25519KeysPair ();
|
2018-07-03 20:26:02 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 14:53:34 +00:00
|
|
|
void NTCP2Establisher::CreateSessionRequestMessage ()
|
2018-08-09 16:53:36 +00:00
|
|
|
{
|
2018-08-10 14:53:34 +00:00
|
|
|
// create buffer and fill padding
|
|
|
|
auto paddingLength = rand () % (287 - 64); // message length doesn't exceed 287 bytes
|
|
|
|
m_SessionRequestBufferLen = paddingLength + 64;
|
2018-08-09 16:53:36 +00:00
|
|
|
m_SessionRequestBuffer = new uint8_t[m_SessionRequestBufferLen];
|
2018-08-10 14:53:34 +00:00
|
|
|
RAND_bytes (m_SessionRequestBuffer + 64, paddingLength);
|
|
|
|
// encrypt X
|
|
|
|
i2p::crypto::CBCEncryption encryption;
|
|
|
|
encryption.SetKey (m_RemoteIdentHash);
|
|
|
|
encryption.SetIV (m_IV);
|
|
|
|
encryption.Encrypt (GetPub (), 32, m_SessionRequestBuffer); // X
|
2020-03-01 14:35:24 +00:00
|
|
|
encryption.GetIV (m_IV); // save IV for SessionCreated
|
2018-08-10 14:53:34 +00:00
|
|
|
// encryption key for next block
|
|
|
|
KDF1Alice ();
|
|
|
|
// fill options
|
|
|
|
uint8_t options[32]; // actual options size is 16 bytes
|
|
|
|
memset (options, 0, 16);
|
2019-08-13 18:55:18 +00:00
|
|
|
options[0] = i2p::context.GetNetID (); // network ID
|
2020-03-01 14:35:24 +00:00
|
|
|
options[1] = 2; // ver
|
2018-08-10 14:53:34 +00:00
|
|
|
htobe16buf (options + 2, paddingLength); // padLen
|
2020-03-01 14:35:24 +00:00
|
|
|
// m3p2Len
|
2018-10-26 13:58:18 +00:00
|
|
|
auto bufLen = i2p::context.GetRouterInfo ().GetBufferLen ();
|
2020-03-01 14:35:24 +00:00
|
|
|
m3p2Len = bufLen + 4 + 16; // (RI header + RI + MAC for now) TODO: implement options
|
2018-08-10 14:53:34 +00:00
|
|
|
htobe16buf (options + 4, m3p2Len);
|
2020-03-01 14:35:24 +00:00
|
|
|
// fill m3p2 payload (RouterInfo block)
|
2018-10-26 13:58:18 +00:00
|
|
|
m_SessionConfirmedBuffer = new uint8_t[m3p2Len + 48]; // m3p1 is 48 bytes
|
|
|
|
uint8_t * m3p2 = m_SessionConfirmedBuffer + 48;
|
|
|
|
m3p2[0] = eNTCP2BlkRouterInfo; // block
|
|
|
|
htobe16buf (m3p2 + 1, bufLen + 1); // flag + RI
|
2020-03-01 14:35:24 +00:00
|
|
|
m3p2[3] = 0; // flag
|
2018-10-26 13:58:18 +00:00
|
|
|
memcpy (m3p2 + 4, i2p::context.GetRouterInfo ().GetBuffer (), bufLen); // TODO: own RI should be protected by mutex
|
2018-08-10 14:53:34 +00:00
|
|
|
// 2 bytes reserved
|
|
|
|
htobe32buf (options + 8, i2p::util::GetSecondsSinceEpoch ()); // tsA
|
|
|
|
// 4 bytes reserved
|
2020-03-01 14:35:24 +00:00
|
|
|
// sign and encrypt options, use m_H as AD
|
2018-08-10 14:53:34 +00:00
|
|
|
uint8_t nonce[12];
|
|
|
|
memset (nonce, 0, 12); // set nonce to zero
|
2020-01-18 19:43:36 +00:00
|
|
|
i2p::crypto::AEADChaCha20Poly1305 (options, 16, GetH (), 32, GetK (), nonce, m_SessionRequestBuffer + 32, 32, true); // encrypt
|
2018-08-09 16:53:36 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 14:53:34 +00:00
|
|
|
void NTCP2Establisher::CreateSessionCreatedMessage ()
|
2018-08-09 16:53:36 +00:00
|
|
|
{
|
2018-08-10 14:53:34 +00:00
|
|
|
auto paddingLen = rand () % (287 - 64);
|
2018-08-09 16:53:36 +00:00
|
|
|
m_SessionCreatedBufferLen = paddingLen + 64;
|
2020-03-01 14:35:24 +00:00
|
|
|
m_SessionCreatedBuffer = new uint8_t[m_SessionCreatedBufferLen];
|
2018-08-09 16:53:36 +00:00
|
|
|
RAND_bytes (m_SessionCreatedBuffer + 64, paddingLen);
|
2018-08-10 14:53:34 +00:00
|
|
|
// encrypt Y
|
|
|
|
i2p::crypto::CBCEncryption encryption;
|
|
|
|
encryption.SetKey (i2p::context.GetIdentHash ());
|
|
|
|
encryption.SetIV (m_IV);
|
|
|
|
encryption.Encrypt (GetPub (), 32, m_SessionCreatedBuffer); // Y
|
|
|
|
// encryption key for next block (m_K)
|
2020-03-01 14:35:24 +00:00
|
|
|
KDF2Bob ();
|
2018-08-10 14:53:34 +00:00
|
|
|
uint8_t options[16];
|
|
|
|
memset (options, 0, 16);
|
|
|
|
htobe16buf (options + 2, paddingLen); // padLen
|
|
|
|
htobe32buf (options + 8, i2p::util::GetSecondsSinceEpoch ()); // tsB
|
2020-03-01 14:35:24 +00:00
|
|
|
// sign and encrypt options, use m_H as AD
|
2018-08-10 14:53:34 +00:00
|
|
|
uint8_t nonce[12];
|
|
|
|
memset (nonce, 0, 12); // set nonce to zero
|
2020-01-18 19:43:36 +00:00
|
|
|
i2p::crypto::AEADChaCha20Poly1305 (options, 16, GetH (), 32, GetK (), nonce, m_SessionCreatedBuffer + 32, 32, true); // encrypt
|
2018-08-10 14:53:34 +00:00
|
|
|
|
2018-08-09 16:53:36 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 14:53:34 +00:00
|
|
|
void NTCP2Establisher::CreateSessionConfirmedMessagePart1 (const uint8_t * nonce)
|
|
|
|
{
|
|
|
|
// update AD
|
2018-11-01 20:06:39 +00:00
|
|
|
MixHash (m_SessionCreatedBuffer + 32, 32); // encrypted payload
|
2018-08-10 14:53:34 +00:00
|
|
|
int paddingLength = m_SessionCreatedBufferLen - 64;
|
|
|
|
if (paddingLength > 0)
|
2020-03-01 14:35:24 +00:00
|
|
|
MixHash (m_SessionCreatedBuffer + 64, paddingLength);
|
2018-11-01 20:06:39 +00:00
|
|
|
|
2020-03-01 14:35:24 +00:00
|
|
|
// part1 48 bytes
|
2020-01-18 19:43:36 +00:00
|
|
|
i2p::crypto::AEADChaCha20Poly1305 (i2p::context.GetNTCP2StaticPublicKey (), 32, GetH (), 32, GetK (), nonce, m_SessionConfirmedBuffer, 48, true); // encrypt
|
2018-08-10 14:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Establisher::CreateSessionConfirmedMessagePart2 (const uint8_t * nonce)
|
|
|
|
{
|
|
|
|
// part 2
|
|
|
|
// update AD again
|
2020-03-01 14:35:24 +00:00
|
|
|
MixHash (m_SessionConfirmedBuffer, 48);
|
2018-10-26 13:58:18 +00:00
|
|
|
// encrypt m3p2, it must be filled in SessionRequest
|
2020-03-01 14:35:24 +00:00
|
|
|
KDF3Alice ();
|
2018-10-26 13:58:18 +00:00
|
|
|
uint8_t * m3p2 = m_SessionConfirmedBuffer + 48;
|
2020-03-01 14:35:24 +00:00
|
|
|
i2p::crypto::AEADChaCha20Poly1305 (m3p2, m3p2Len - 16, GetH (), 32, GetK (), nonce, m3p2, m3p2Len, true); // encrypt
|
2018-08-10 14:53:34 +00:00
|
|
|
// update h again
|
2018-11-01 20:06:39 +00:00
|
|
|
MixHash (m3p2, m3p2Len); //h = SHA256(h || ciphertext)
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2018-08-10 14:53:34 +00:00
|
|
|
|
2018-08-14 18:01:04 +00:00
|
|
|
bool NTCP2Establisher::ProcessSessionRequestMessage (uint16_t& paddingLen)
|
|
|
|
{
|
|
|
|
// decrypt X
|
|
|
|
i2p::crypto::CBCDecryption decryption;
|
|
|
|
decryption.SetKey (i2p::context.GetIdentHash ());
|
|
|
|
decryption.SetIV (i2p::context.GetNTCP2IV ());
|
|
|
|
decryption.Decrypt (m_SessionRequestBuffer, 32, GetRemotePub ());
|
2020-03-01 14:35:24 +00:00
|
|
|
decryption.GetIV (m_IV); // save IV for SessionCreated
|
2018-08-14 18:01:04 +00:00
|
|
|
// decryption key for next block
|
|
|
|
KDF1Bob ();
|
|
|
|
// verify MAC and decrypt options block (32 bytes), use m_H as AD
|
|
|
|
uint8_t nonce[12], options[16];
|
|
|
|
memset (nonce, 0, 12); // set nonce to zero
|
2020-01-18 19:43:36 +00:00
|
|
|
if (i2p::crypto::AEADChaCha20Poly1305 (m_SessionRequestBuffer + 32, 16, GetH (), 32, GetK (), nonce, options, 16, false)) // decrypt
|
2018-08-14 18:01:04 +00:00
|
|
|
{
|
2018-08-15 15:42:56 +00:00
|
|
|
// options
|
2019-08-13 18:55:18 +00:00
|
|
|
if (options[0] && options[0] != i2p::context.GetNetID ())
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionRequest networkID ", (int)options[0], " mismatch. Expected ", i2p::context.GetNetID ());
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-01 14:35:24 +00:00
|
|
|
if (options[1] == 2) // ver is always 2
|
2018-08-14 18:01:04 +00:00
|
|
|
{
|
|
|
|
paddingLen = bufbe16toh (options + 2);
|
|
|
|
m_SessionRequestBufferLen = paddingLen + 64;
|
|
|
|
m3p2Len = bufbe16toh (options + 4);
|
2018-08-18 12:27:36 +00:00
|
|
|
if (m3p2Len < 16)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionRequest m3p2len=", m3p2Len, " is too short");
|
2020-03-01 14:35:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-15 15:42:56 +00:00
|
|
|
// check timestamp
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
2020-03-01 14:35:24 +00:00
|
|
|
uint32_t tsA = bufbe32toh (options + 8);
|
2018-08-15 15:42:56 +00:00
|
|
|
if (tsA < ts - NTCP2_CLOCK_SKEW || tsA > ts + NTCP2_CLOCK_SKEW)
|
|
|
|
{
|
2018-08-16 14:13:32 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionRequest time difference ", (int)(ts - tsA), " exceeds clock skew");
|
2018-08-15 15:42:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-14 18:01:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionRequest version mismatch ", (int)options[1]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionRequest AEAD verification failed ");
|
|
|
|
return false;
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
|
|
|
return true;
|
2018-08-14 18:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NTCP2Establisher::ProcessSessionCreatedMessage (uint16_t& paddingLen)
|
|
|
|
{
|
|
|
|
m_SessionCreatedBufferLen = 64;
|
|
|
|
// decrypt Y
|
|
|
|
i2p::crypto::CBCDecryption decryption;
|
|
|
|
decryption.SetKey (m_RemoteIdentHash);
|
|
|
|
decryption.SetIV (m_IV);
|
|
|
|
decryption.Decrypt (m_SessionCreatedBuffer, 32, GetRemotePub ());
|
|
|
|
// decryption key for next block (m_K)
|
|
|
|
KDF2Alice ();
|
|
|
|
// decrypt and verify MAC
|
|
|
|
uint8_t payload[16];
|
|
|
|
uint8_t nonce[12];
|
|
|
|
memset (nonce, 0, 12); // set nonce to zero
|
2020-01-18 19:43:36 +00:00
|
|
|
if (i2p::crypto::AEADChaCha20Poly1305 (m_SessionCreatedBuffer + 32, 16, GetH (), 32, GetK (), nonce, payload, 16, false)) // decrypt
|
2018-08-15 15:42:56 +00:00
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
// options
|
2018-08-14 18:01:04 +00:00
|
|
|
paddingLen = bufbe16toh(payload + 2);
|
2018-08-15 15:42:56 +00:00
|
|
|
// check timestamp
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
2020-03-01 14:35:24 +00:00
|
|
|
uint32_t tsB = bufbe32toh (payload + 8);
|
2018-08-15 15:42:56 +00:00
|
|
|
if (tsB < ts - NTCP2_CLOCK_SKEW || tsB > ts + NTCP2_CLOCK_SKEW)
|
|
|
|
{
|
2018-08-16 14:13:32 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionCreated time difference ", (int)(ts - tsB), " exceeds clock skew");
|
2018-08-15 15:42:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-14 18:01:04 +00:00
|
|
|
}
|
|
|
|
else
|
2020-03-01 14:35:24 +00:00
|
|
|
{
|
2018-08-14 18:01:04 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionCreated AEAD verification failed ");
|
|
|
|
return false;
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2018-08-14 18:01:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NTCP2Establisher::ProcessSessionConfirmedMessagePart1 (const uint8_t * nonce)
|
|
|
|
{
|
|
|
|
// update AD
|
2018-11-01 20:06:39 +00:00
|
|
|
MixHash (m_SessionCreatedBuffer + 32, 32); // encrypted payload
|
2018-08-14 18:01:04 +00:00
|
|
|
int paddingLength = m_SessionCreatedBufferLen - 64;
|
|
|
|
if (paddingLength > 0)
|
2018-11-01 20:06:39 +00:00
|
|
|
MixHash (m_SessionCreatedBuffer + 64, paddingLength);
|
2020-03-01 14:35:24 +00:00
|
|
|
|
2020-01-18 19:43:36 +00:00
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (m_SessionConfirmedBuffer, 32, GetH (), 32, GetK (), nonce, m_RemoteStaticKey, 32, false)) // decrypt S
|
2018-08-14 18:01:04 +00:00
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionConfirmed Part1 AEAD verification failed ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NTCP2Establisher::ProcessSessionConfirmedMessagePart2 (const uint8_t * nonce, uint8_t * m3p2Buf)
|
|
|
|
{
|
|
|
|
// update AD again
|
2020-03-01 14:35:24 +00:00
|
|
|
MixHash (m_SessionConfirmedBuffer, 48);
|
2018-08-14 18:01:04 +00:00
|
|
|
|
2020-03-01 14:35:24 +00:00
|
|
|
KDF3Bob ();
|
2020-01-18 19:43:36 +00:00
|
|
|
if (i2p::crypto::AEADChaCha20Poly1305 (m_SessionConfirmedBuffer + 48, m3p2Len - 16, GetH (), 32, GetK (), nonce, m3p2Buf, m3p2Len - 16, false)) // decrypt
|
2018-08-14 18:01:04 +00:00
|
|
|
// caclulate new h again for KDF data
|
2020-08-03 22:31:03 +00:00
|
|
|
MixHash (m_SessionConfirmedBuffer + 48, m3p2Len); // h = SHA256(h || ciphertext)
|
2018-08-14 18:01:04 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionConfirmed Part2 AEAD verification failed ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-03 20:26:02 +00:00
|
|
|
NTCP2Session::NTCP2Session (NTCP2Server& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter):
|
2020-03-01 14:35:24 +00:00
|
|
|
TransportSession (in_RemoteRouter, NTCP2_ESTABLISH_TIMEOUT),
|
|
|
|
m_Server (server), m_Socket (m_Server.GetService ()),
|
2018-07-03 20:26:02 +00:00
|
|
|
m_IsEstablished (false), m_IsTerminated (false),
|
2018-12-08 00:24:46 +00:00
|
|
|
m_Establisher (new NTCP2Establisher),
|
2018-09-11 17:26:29 +00:00
|
|
|
m_SendSipKey (nullptr), m_ReceiveSipKey (nullptr),
|
|
|
|
#if OPENSSL_SIPHASH
|
|
|
|
m_SendMDCtx(nullptr), m_ReceiveMDCtx (nullptr),
|
|
|
|
#endif
|
2018-08-08 20:23:44 +00:00
|
|
|
m_NextReceivedLen (0), m_NextReceivedBuffer (nullptr), m_NextSendBuffer (nullptr),
|
2018-07-18 15:16:40 +00:00
|
|
|
m_ReceiveSequenceNumber (0), m_SendSequenceNumber (0), m_IsSending (false)
|
2018-07-03 20:26:02 +00:00
|
|
|
{
|
2018-07-31 19:41:13 +00:00
|
|
|
if (in_RemoteRouter) // Alice
|
2018-07-03 20:26:02 +00:00
|
|
|
{
|
2018-08-10 14:53:34 +00:00
|
|
|
m_Establisher->m_RemoteIdentHash = GetRemoteIdentity ()->GetIdentHash ();
|
2018-08-04 12:47:58 +00:00
|
|
|
auto addr = in_RemoteRouter->GetNTCP2Address (true); // we need a published address
|
2018-08-02 17:58:47 +00:00
|
|
|
if (addr)
|
2018-07-31 19:41:13 +00:00
|
|
|
{
|
|
|
|
memcpy (m_Establisher->m_RemoteStaticKey, addr->ntcp2->staticKey, 32);
|
|
|
|
memcpy (m_Establisher->m_IV, addr->ntcp2->iv, 16);
|
|
|
|
}
|
|
|
|
else
|
2020-03-01 14:35:24 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP2: Missing NTCP2 parameters");
|
2018-07-03 20:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NTCP2Session::~NTCP2Session ()
|
|
|
|
{
|
|
|
|
delete[] m_NextReceivedBuffer;
|
|
|
|
delete[] m_NextSendBuffer;
|
2018-09-11 17:26:29 +00:00
|
|
|
#if OPENSSL_SIPHASH
|
|
|
|
if (m_SendSipKey) EVP_PKEY_free (m_SendSipKey);
|
|
|
|
if (m_ReceiveSipKey) EVP_PKEY_free (m_ReceiveSipKey);
|
|
|
|
if (m_SendMDCtx) EVP_MD_CTX_destroy (m_SendMDCtx);
|
|
|
|
if (m_ReceiveMDCtx) EVP_MD_CTX_destroy (m_ReceiveMDCtx);
|
|
|
|
#endif
|
2018-07-03 20:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Session::Terminate ()
|
|
|
|
{
|
|
|
|
if (!m_IsTerminated)
|
|
|
|
{
|
|
|
|
m_IsTerminated = true;
|
|
|
|
m_IsEstablished = false;
|
2019-06-26 14:47:16 +00:00
|
|
|
boost::system::error_code ec;
|
|
|
|
m_Socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
|
|
|
if (ec)
|
|
|
|
LogPrint (eLogDebug, "NTCP2: Couldn't shutdown socket: ", ec.message ());
|
2018-07-03 20:26:02 +00:00
|
|
|
m_Socket.close ();
|
2018-07-18 16:58:29 +00:00
|
|
|
transports.PeerDisconnected (shared_from_this ());
|
|
|
|
m_Server.RemoveNTCP2Session (shared_from_this ());
|
|
|
|
m_SendQueue.clear ();
|
2018-07-03 20:26:02 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP2: session terminated");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 17:10:32 +00:00
|
|
|
void NTCP2Session::TerminateByTimeout ()
|
|
|
|
{
|
|
|
|
SendTerminationAndTerminate (eNTCP2IdleTimeout);
|
|
|
|
}
|
|
|
|
|
2018-07-03 20:26:02 +00:00
|
|
|
void NTCP2Session::Done ()
|
|
|
|
{
|
|
|
|
m_Server.GetService ().post (std::bind (&NTCP2Session::Terminate, shared_from_this ()));
|
|
|
|
}
|
|
|
|
|
2018-07-13 19:59:28 +00:00
|
|
|
void NTCP2Session::Established ()
|
|
|
|
{
|
|
|
|
m_IsEstablished = true;
|
|
|
|
m_Establisher.reset (nullptr);
|
2018-08-03 17:10:32 +00:00
|
|
|
SetTerminationTimeout (NTCP2_TERMINATION_TIMEOUT);
|
2018-08-15 17:23:10 +00:00
|
|
|
transports.PeerConnected (shared_from_this ());
|
2018-07-13 19:59:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 20:26:02 +00:00
|
|
|
void NTCP2Session::CreateNonce (uint64_t seqn, uint8_t * nonce)
|
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
memset (nonce, 0, 4);
|
|
|
|
htole64buf (nonce + 4, seqn);
|
2018-07-03 20:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-21 16:39:24 +00:00
|
|
|
void NTCP2Session::KeyDerivationFunctionDataPhase ()
|
2018-06-20 20:09:22 +00:00
|
|
|
{
|
2019-12-19 01:45:47 +00:00
|
|
|
uint8_t k[64];
|
|
|
|
i2p::crypto::HKDF (m_Establisher->GetCK (), nullptr, 0, "", k); // k_ab, k_ba = HKDF(ck, zerolen)
|
|
|
|
memcpy (m_Kab, k, 32); memcpy (m_Kba, k + 32, 32);
|
|
|
|
uint8_t master[32];
|
|
|
|
i2p::crypto::HKDF (m_Establisher->GetCK (), nullptr, 0, "ask", master, 32); // ask_master = HKDF(ck, zerolen, info="ask")
|
2018-06-21 16:39:24 +00:00
|
|
|
uint8_t h[39];
|
2018-07-03 20:26:02 +00:00
|
|
|
memcpy (h, m_Establisher->GetH (), 32);
|
2018-06-21 16:39:24 +00:00
|
|
|
memcpy (h + 32, "siphash", 7);
|
2019-12-19 01:45:47 +00:00
|
|
|
i2p::crypto::HKDF (master, h, 39, "", master, 32); // sip_master = HKDF(ask_master, h || "siphash")
|
2019-12-19 01:48:30 +00:00
|
|
|
i2p::crypto::HKDF (master, nullptr, 0, "", k); // sipkeys_ab, sipkeys_ba = HKDF(sip_master, zerolen)
|
2020-03-01 14:35:24 +00:00
|
|
|
memcpy (m_Sipkeysab, k, 32); memcpy (m_Sipkeysba, k + 32, 32);
|
2018-06-20 20:09:22 +00:00
|
|
|
}
|
|
|
|
|
2018-06-05 16:53:13 +00:00
|
|
|
|
2018-06-06 19:38:18 +00:00
|
|
|
void NTCP2Session::SendSessionRequest ()
|
2018-06-05 16:53:13 +00:00
|
|
|
{
|
2018-08-10 14:53:34 +00:00
|
|
|
m_Establisher->CreateSessionRequestMessage ();
|
2018-06-06 19:38:18 +00:00
|
|
|
// send message
|
2018-08-09 16:53:36 +00:00
|
|
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_Establisher->m_SessionRequestBuffer, m_Establisher->m_SessionRequestBufferLen), boost::asio::transfer_all (),
|
2020-03-01 14:35:24 +00:00
|
|
|
std::bind(&NTCP2Session::HandleSessionRequestSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
|
|
|
}
|
2018-06-06 19:38:18 +00:00
|
|
|
|
|
|
|
void NTCP2Session::HandleSessionRequestSent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
(void) bytes_transferred;
|
|
|
|
if (ecode)
|
|
|
|
{
|
2018-06-13 18:56:51 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP2: couldn't send SessionRequest message: ", ecode.message ());
|
2018-06-11 16:29:30 +00:00
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-09 16:53:36 +00:00
|
|
|
m_Establisher->m_SessionCreatedBuffer = new uint8_t[287]; // TODO: determine actual max size
|
2018-06-20 20:09:22 +00:00
|
|
|
// we receive first 64 bytes (32 Y, and 32 ChaCha/Poly frame) first
|
2018-08-09 16:53:36 +00:00
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(m_Establisher->m_SessionCreatedBuffer, 64), boost::asio::transfer_all (),
|
2018-06-11 16:29:30 +00:00
|
|
|
std::bind(&NTCP2Session::HandleSessionCreatedReceived, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
2018-06-06 19:38:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 19:43:47 +00:00
|
|
|
void NTCP2Session::HandleSessionRequestReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
(void) bytes_transferred;
|
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionRequest read error: ", ecode.message ());
|
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-14 18:01:04 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP2: SessionRequest received ", bytes_transferred);
|
|
|
|
uint16_t paddingLen = 0;
|
|
|
|
if (m_Establisher->ProcessSessionRequestMessage (paddingLen))
|
2018-06-19 19:43:47 +00:00
|
|
|
{
|
2018-08-14 18:01:04 +00:00
|
|
|
if (paddingLen > 0)
|
2018-06-19 19:43:47 +00:00
|
|
|
{
|
2018-08-14 18:01:04 +00:00
|
|
|
if (paddingLen <= 287 - 64) // session request is 287 bytes max
|
2018-08-09 16:53:36 +00:00
|
|
|
{
|
2018-08-14 18:01:04 +00:00
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(m_Establisher->m_SessionRequestBuffer + 64, paddingLen), boost::asio::transfer_all (),
|
|
|
|
std::bind(&NTCP2Session::HandleSessionRequestPaddingReceived, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
2018-08-09 16:53:36 +00:00
|
|
|
}
|
2018-06-19 19:43:47 +00:00
|
|
|
else
|
2018-08-14 18:01:04 +00:00
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionRequest padding length ", (int)paddingLen, " is too long");
|
|
|
|
Terminate ();
|
|
|
|
}
|
2018-06-19 19:43:47 +00:00
|
|
|
}
|
|
|
|
else
|
2018-08-14 18:01:04 +00:00
|
|
|
SendSessionCreated ();
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2018-06-19 19:43:47 +00:00
|
|
|
else
|
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Session::HandleSessionRequestPaddingReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionRequest padding read error: ", ecode.message ());
|
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SendSessionCreated ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Session::SendSessionCreated ()
|
|
|
|
{
|
2018-08-10 14:53:34 +00:00
|
|
|
m_Establisher->CreateSessionCreatedMessage ();
|
2020-03-01 14:35:24 +00:00
|
|
|
// send message
|
2018-08-09 16:53:36 +00:00
|
|
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_Establisher->m_SessionCreatedBuffer, m_Establisher->m_SessionCreatedBufferLen), boost::asio::transfer_all (),
|
2020-03-01 14:35:24 +00:00
|
|
|
std::bind(&NTCP2Session::HandleSessionCreatedSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
2018-06-19 19:43:47 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 16:29:30 +00:00
|
|
|
void NTCP2Session::HandleSessionCreatedReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
if (ecode)
|
2018-06-13 18:56:51 +00:00
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionCreated read error: ", ecode.message ());
|
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-14 19:29:36 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP2: SessionCreated received ", bytes_transferred);
|
2018-08-14 18:01:04 +00:00
|
|
|
uint16_t paddingLen = 0;
|
|
|
|
if (m_Establisher->ProcessSessionCreatedMessage (paddingLen))
|
|
|
|
{
|
2018-06-14 19:29:36 +00:00
|
|
|
if (paddingLen > 0)
|
|
|
|
{
|
2018-08-11 20:08:21 +00:00
|
|
|
if (paddingLen <= 287 - 64) // session created is 287 bytes max
|
|
|
|
{
|
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(m_Establisher->m_SessionCreatedBuffer + 64, paddingLen), boost::asio::transfer_all (),
|
|
|
|
std::bind(&NTCP2Session::HandleSessionCreatedPaddingReceived, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionCreated padding length ", (int)paddingLen, " is too long");
|
|
|
|
Terminate ();
|
|
|
|
}
|
2018-06-14 19:29:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
SendSessionConfirmed ();
|
2018-06-13 20:16:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Terminate ();
|
2018-06-13 18:56:51 +00:00
|
|
|
}
|
2018-06-11 16:29:30 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 19:29:36 +00:00
|
|
|
void NTCP2Session::HandleSessionCreatedPaddingReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionCreated padding read error: ", ecode.message ());
|
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-09 16:53:36 +00:00
|
|
|
m_Establisher->m_SessionCreatedBufferLen += bytes_transferred;
|
2018-06-14 19:29:36 +00:00
|
|
|
SendSessionConfirmed ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Session::SendSessionConfirmed ()
|
|
|
|
{
|
|
|
|
uint8_t nonce[12];
|
2018-08-10 14:53:34 +00:00
|
|
|
CreateNonce (1, nonce); // set nonce to 1
|
2020-03-01 14:35:24 +00:00
|
|
|
m_Establisher->CreateSessionConfirmedMessagePart1 (nonce);
|
2018-08-10 14:53:34 +00:00
|
|
|
memset (nonce, 0, 12); // set nonce back to 0
|
2020-03-01 14:35:24 +00:00
|
|
|
m_Establisher->CreateSessionConfirmedMessagePart2 (nonce);
|
2018-06-14 19:29:36 +00:00
|
|
|
// send message
|
2018-08-09 16:53:36 +00:00
|
|
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_Establisher->m_SessionConfirmedBuffer, m_Establisher->m3p2Len + 48), boost::asio::transfer_all (),
|
2018-06-14 19:29:36 +00:00
|
|
|
std::bind(&NTCP2Session::HandleSessionConfirmedSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Session::HandleSessionConfirmedSent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
2019-09-19 20:54:23 +00:00
|
|
|
(void) bytes_transferred;
|
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: couldn't send SessionConfirmed message: ", ecode.message ());
|
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
else
|
2020-03-01 14:35:24 +00:00
|
|
|
{
|
2019-09-19 20:54:23 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP2: SessionConfirmed sent");
|
|
|
|
KeyDerivationFunctionDataPhase ();
|
|
|
|
// Alice data phase keys
|
|
|
|
m_SendKey = m_Kab;
|
2020-03-01 14:35:24 +00:00
|
|
|
m_ReceiveKey = m_Kba;
|
2019-09-19 20:54:23 +00:00
|
|
|
SetSipKeys (m_Sipkeysab, m_Sipkeysba);
|
|
|
|
memcpy (m_ReceiveIV.buf, m_Sipkeysba + 16, 8);
|
|
|
|
memcpy (m_SendIV.buf, m_Sipkeysab + 16, 8);
|
|
|
|
Established ();
|
|
|
|
ReceiveLength ();
|
|
|
|
|
|
|
|
// TODO: remove
|
|
|
|
// m_SendQueue.push_back (CreateDeliveryStatusMsg (1));
|
|
|
|
// SendQueue ();
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2018-06-14 19:29:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 19:43:47 +00:00
|
|
|
void NTCP2Session::HandleSessionCreatedSent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
2018-07-09 19:56:23 +00:00
|
|
|
(void) bytes_transferred;
|
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: couldn't send SessionCreated message: ", ecode.message ());
|
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "NTCP2: SessionCreated sent");
|
2020-03-01 14:35:24 +00:00
|
|
|
m_Establisher->m_SessionConfirmedBuffer = new uint8_t[m_Establisher->m3p2Len + 48];
|
2018-08-09 16:53:36 +00:00
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(m_Establisher->m_SessionConfirmedBuffer, m_Establisher->m3p2Len + 48), boost::asio::transfer_all (),
|
2018-07-09 19:56:23 +00:00
|
|
|
std::bind(&NTCP2Session::HandleSessionConfirmedReceived , shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Session::HandleSessionConfirmedReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
if (ecode)
|
|
|
|
{
|
2018-08-09 16:53:36 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP2: SessionConfirmed read error: ", ecode.message ());
|
2018-07-09 19:56:23 +00:00
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-09 16:53:36 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP2: SessionConfirmed received");
|
2018-07-09 19:56:23 +00:00
|
|
|
// part 1
|
|
|
|
uint8_t nonce[12];
|
|
|
|
CreateNonce (1, nonce);
|
2018-08-14 18:01:04 +00:00
|
|
|
if (m_Establisher->ProcessSessionConfirmedMessagePart1 (nonce))
|
2018-07-18 19:57:18 +00:00
|
|
|
{
|
2018-07-31 19:41:13 +00:00
|
|
|
// part 2
|
|
|
|
std::vector<uint8_t> buf(m_Establisher->m3p2Len - 16); // -MAC
|
|
|
|
memset (nonce, 0, 12); // set nonce to 0 again
|
2018-08-14 18:01:04 +00:00
|
|
|
if (m_Establisher->ProcessSessionConfirmedMessagePart2 (nonce, buf.data ()))
|
2018-07-18 19:57:18 +00:00
|
|
|
{
|
2018-07-31 19:41:13 +00:00
|
|
|
KeyDerivationFunctionDataPhase ();
|
2018-08-02 17:58:47 +00:00
|
|
|
// Bob data phase keys
|
2018-07-31 19:41:13 +00:00
|
|
|
m_SendKey = m_Kba;
|
2020-03-01 14:35:24 +00:00
|
|
|
m_ReceiveKey = m_Kab;
|
2018-09-11 17:26:29 +00:00
|
|
|
SetSipKeys (m_Sipkeysba, m_Sipkeysab);
|
2018-08-13 17:43:51 +00:00
|
|
|
memcpy (m_ReceiveIV.buf, m_Sipkeysab + 16, 8);
|
|
|
|
memcpy (m_SendIV.buf, m_Sipkeysba + 16, 8);
|
2018-08-14 18:01:04 +00:00
|
|
|
// payload
|
2018-08-02 17:58:47 +00:00
|
|
|
// process RI
|
|
|
|
if (buf[0] != eNTCP2BlkRouterInfo)
|
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP2: unexpected block ", (int)buf[0], " in SessionConfirmed");
|
|
|
|
Terminate ();
|
2018-08-02 17:58:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto size = bufbe16toh (buf.data () + 1);
|
|
|
|
if (size > buf.size () - 3)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "NTCP2: Unexpected RouterInfo size ", size, " in SessionConfirmed");
|
|
|
|
Terminate ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO: check flag
|
|
|
|
i2p::data::RouterInfo ri (buf.data () + 4, size - 1); // 1 byte block type + 2 bytes size + 1 byte flag
|
|
|
|
if (ri.IsUnreachable ())
|
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
LogPrint (eLogError, "NTCP2: Signature verification failed in SessionConfirmed");
|
|
|
|
SendTerminationAndTerminate (eNTCP2RouterInfoSignatureVerificationFail);
|
2018-08-02 17:58:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-09-21 15:45:04 +00:00
|
|
|
if (i2p::util::GetMillisecondsSinceEpoch () > ri.GetTimestamp () + i2p::data::NETDB_MIN_EXPIRATION_TIMEOUT*1000LL) // 90 minutes
|
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
LogPrint (eLogError, "NTCP2: RouterInfo is too old in SessionConfirmed");
|
|
|
|
SendTerminationAndTerminate (eNTCP2Message3Error);
|
2018-09-21 15:45:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-04 12:47:58 +00:00
|
|
|
auto addr = ri.GetNTCP2Address (false); // any NTCP2 address
|
2018-08-02 17:58:47 +00:00
|
|
|
if (!addr)
|
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
LogPrint (eLogError, "NTCP2: No NTCP2 address found in SessionConfirmed");
|
2018-08-02 17:58:47 +00:00
|
|
|
Terminate ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (memcmp (addr->ntcp2->staticKey, m_Establisher->m_RemoteStaticKey, 32))
|
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
LogPrint (eLogError, "NTCP2: Static key mismatch in SessionConfirmed");
|
|
|
|
SendTerminationAndTerminate (eNTCP2IncorrectSParameter);
|
2018-08-02 17:58:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-11-21 16:23:48 +00:00
|
|
|
i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, buf.data () + 3, size)); // TODO: should insert ri and not parse it twice
|
2018-08-02 17:58:47 +00:00
|
|
|
// TODO: process options
|
2020-03-01 14:35:24 +00:00
|
|
|
|
|
|
|
// ready to communicate
|
2018-08-02 17:58:47 +00:00
|
|
|
auto existing = i2p::data::netdb.FindRouter (ri.GetRouterIdentity ()->GetIdentHash ()); // check if exists already
|
|
|
|
SetRemoteIdentity (existing ? existing->GetRouterIdentity () : ri.GetRouterIdentity ());
|
2020-04-08 22:02:12 +00:00
|
|
|
if (m_Server.AddNTCP2Session (shared_from_this (), true))
|
2020-03-01 10:25:50 +00:00
|
|
|
{
|
2020-04-08 22:02:12 +00:00
|
|
|
Established ();
|
|
|
|
ReceiveLength ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Terminate ();
|
2018-08-14 18:01:04 +00:00
|
|
|
}
|
2018-07-18 19:57:18 +00:00
|
|
|
else
|
2018-07-31 19:41:13 +00:00
|
|
|
Terminate ();
|
2018-07-18 19:57:18 +00:00
|
|
|
}
|
|
|
|
else
|
2018-07-31 19:41:13 +00:00
|
|
|
Terminate ();
|
2018-07-09 19:56:23 +00:00
|
|
|
}
|
2018-06-19 19:43:47 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 17:26:29 +00:00
|
|
|
void NTCP2Session::SetSipKeys (const uint8_t * sendSipKey, const uint8_t * receiveSipKey)
|
|
|
|
{
|
|
|
|
#if OPENSSL_SIPHASH
|
|
|
|
m_SendSipKey = EVP_PKEY_new_raw_private_key (EVP_PKEY_SIPHASH, nullptr, sendSipKey, 16);
|
2020-03-01 14:35:24 +00:00
|
|
|
m_SendMDCtx = EVP_MD_CTX_create ();
|
2018-09-11 17:26:29 +00:00
|
|
|
EVP_PKEY_CTX *ctx = nullptr;
|
|
|
|
EVP_DigestSignInit (m_SendMDCtx, &ctx, nullptr, nullptr, m_SendSipKey);
|
2020-03-01 14:35:24 +00:00
|
|
|
EVP_PKEY_CTX_ctrl (ctx, -1, EVP_PKEY_OP_SIGNCTX, EVP_PKEY_CTRL_SET_DIGEST_SIZE, 8, nullptr);
|
2018-09-11 17:26:29 +00:00
|
|
|
|
|
|
|
m_ReceiveSipKey = EVP_PKEY_new_raw_private_key (EVP_PKEY_SIPHASH, nullptr, receiveSipKey, 16);
|
2020-03-01 14:35:24 +00:00
|
|
|
m_ReceiveMDCtx = EVP_MD_CTX_create ();
|
2018-09-11 17:26:29 +00:00
|
|
|
ctx = nullptr;
|
|
|
|
EVP_DigestSignInit (m_ReceiveMDCtx, &ctx, NULL, NULL, m_ReceiveSipKey);
|
|
|
|
EVP_PKEY_CTX_ctrl (ctx, -1, EVP_PKEY_OP_SIGNCTX, EVP_PKEY_CTRL_SET_DIGEST_SIZE, 8, nullptr);
|
|
|
|
#else
|
|
|
|
m_SendSipKey = sendSipKey;
|
|
|
|
m_ReceiveSipKey = receiveSipKey;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-06-06 19:38:18 +00:00
|
|
|
void NTCP2Session::ClientLogin ()
|
|
|
|
{
|
2018-12-08 00:24:46 +00:00
|
|
|
m_Establisher->CreateEphemeralKey ();
|
2018-06-06 19:38:18 +00:00
|
|
|
SendSessionRequest ();
|
|
|
|
}
|
2018-06-11 16:29:30 +00:00
|
|
|
|
2018-06-19 19:43:47 +00:00
|
|
|
void NTCP2Session::ServerLogin ()
|
|
|
|
{
|
2018-12-08 00:24:46 +00:00
|
|
|
m_Establisher->CreateEphemeralKey ();
|
2018-08-09 16:53:36 +00:00
|
|
|
m_Establisher->m_SessionRequestBuffer = new uint8_t[287]; // 287 bytes max for now
|
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(m_Establisher->m_SessionRequestBuffer, 64), boost::asio::transfer_all (),
|
2018-06-19 19:43:47 +00:00
|
|
|
std::bind(&NTCP2Session::HandleSessionRequestReceived, shared_from_this (),
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:24:19 +00:00
|
|
|
void NTCP2Session::ReceiveLength ()
|
|
|
|
{
|
2018-08-02 16:42:39 +00:00
|
|
|
if (IsTerminated ()) return;
|
2018-06-21 20:24:19 +00:00
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(&m_NextReceivedLen, 2), boost::asio::transfer_all (),
|
|
|
|
std::bind(&NTCP2Session::HandleReceivedLength, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Session::HandleReceivedLength (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
if (ecode)
|
|
|
|
{
|
2018-08-11 20:08:21 +00:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
LogPrint (eLogWarning, "NTCP2: receive length read error: ", ecode.message ());
|
2018-06-21 20:24:19 +00:00
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-11 17:26:29 +00:00
|
|
|
#if OPENSSL_SIPHASH
|
|
|
|
EVP_DigestSignInit (m_ReceiveMDCtx, nullptr, nullptr, nullptr, nullptr);
|
2020-03-01 14:35:24 +00:00
|
|
|
EVP_DigestSignUpdate (m_ReceiveMDCtx, m_ReceiveIV.buf, 8);
|
|
|
|
size_t l = 8;
|
|
|
|
EVP_DigestSignFinal (m_ReceiveMDCtx, m_ReceiveIV.buf, &l);
|
2018-09-11 17:26:29 +00:00
|
|
|
#else
|
2018-08-13 17:43:51 +00:00
|
|
|
i2p::crypto::Siphash<8> (m_ReceiveIV.buf, m_ReceiveIV.buf, 8, m_ReceiveSipKey);
|
2018-09-11 17:26:29 +00:00
|
|
|
#endif
|
2018-08-14 18:01:04 +00:00
|
|
|
// m_NextReceivedLen comes from the network in BigEndian
|
2018-08-13 18:07:57 +00:00
|
|
|
m_NextReceivedLen = be16toh (m_NextReceivedLen) ^ le16toh (m_ReceiveIV.key);
|
2018-06-21 20:24:19 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP2: received length ", m_NextReceivedLen);
|
2018-08-14 18:01:04 +00:00
|
|
|
if (m_NextReceivedLen >= 16)
|
2020-03-01 14:35:24 +00:00
|
|
|
{
|
2018-08-14 18:01:04 +00:00
|
|
|
if (m_NextReceivedBuffer) delete[] m_NextReceivedBuffer;
|
|
|
|
m_NextReceivedBuffer = new uint8_t[m_NextReceivedLen];
|
2018-09-17 17:08:49 +00:00
|
|
|
boost::system::error_code ec;
|
|
|
|
size_t moreBytes = m_Socket.available(ec);
|
|
|
|
if (!ec && moreBytes >= m_NextReceivedLen)
|
|
|
|
{
|
2019-04-08 19:22:42 +00:00
|
|
|
// read and process message immediately if available
|
2018-09-17 17:08:49 +00:00
|
|
|
moreBytes = boost::asio::read (m_Socket, boost::asio::buffer(m_NextReceivedBuffer, m_NextReceivedLen), boost::asio::transfer_all (), ec);
|
|
|
|
HandleReceived (ec, moreBytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Receive ();
|
2018-08-14 18:01:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "NTCP2: received length ", m_NextReceivedLen, " is too short");
|
|
|
|
Terminate ();
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2018-06-21 20:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Session::Receive ()
|
|
|
|
{
|
2018-08-02 16:42:39 +00:00
|
|
|
if (IsTerminated ()) return;
|
2018-06-21 20:24:19 +00:00
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(m_NextReceivedBuffer, m_NextReceivedLen), boost::asio::transfer_all (),
|
|
|
|
std::bind(&NTCP2Session::HandleReceived, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Session::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
if (ecode)
|
|
|
|
{
|
2018-08-11 20:08:21 +00:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
LogPrint (eLogWarning, "NTCP2: receive read error: ", ecode.message ());
|
2018-06-21 20:24:19 +00:00
|
|
|
Terminate ();
|
|
|
|
}
|
2018-06-22 19:02:49 +00:00
|
|
|
else
|
|
|
|
{
|
2018-08-03 17:10:32 +00:00
|
|
|
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch ();
|
2018-07-18 19:57:18 +00:00
|
|
|
m_NumReceivedBytes += bytes_transferred + 2; // + length
|
|
|
|
i2p::transport::transports.UpdateReceivedBytes (bytes_transferred);
|
2018-06-22 19:02:49 +00:00
|
|
|
uint8_t nonce[12];
|
2018-06-25 16:28:07 +00:00
|
|
|
CreateNonce (m_ReceiveSequenceNumber, nonce); m_ReceiveSequenceNumber++;
|
2018-08-09 19:47:02 +00:00
|
|
|
if (i2p::crypto::AEADChaCha20Poly1305 (m_NextReceivedBuffer, m_NextReceivedLen-16, nullptr, 0, m_ReceiveKey, nonce, m_NextReceivedBuffer, m_NextReceivedLen, false))
|
2020-03-01 14:35:24 +00:00
|
|
|
{
|
2018-07-18 18:19:12 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP2: received message decrypted");
|
2018-08-09 19:47:02 +00:00
|
|
|
ProcessNextFrame (m_NextReceivedBuffer, m_NextReceivedLen-16);
|
2018-08-09 16:53:36 +00:00
|
|
|
delete[] m_NextReceivedBuffer; m_NextReceivedBuffer = nullptr; // we don't need received buffer anymore
|
2018-06-22 19:02:49 +00:00
|
|
|
ReceiveLength ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-03 17:10:32 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP2: Received AEAD verification failed ");
|
|
|
|
SendTerminationAndTerminate (eNTCP2DataPhaseAEADFailure);
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2018-06-22 19:02:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Session::ProcessNextFrame (const uint8_t * frame, size_t len)
|
|
|
|
{
|
|
|
|
size_t offset = 0;
|
|
|
|
while (offset < len)
|
|
|
|
{
|
|
|
|
uint8_t blk = frame[offset];
|
|
|
|
offset++;
|
|
|
|
auto size = bufbe16toh (frame + offset);
|
|
|
|
offset += 2;
|
|
|
|
LogPrint (eLogDebug, "NTCP2: Block type ", (int)blk, " of size ", size);
|
|
|
|
if (size > len)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "NTCP2: Unexpected block length ", size);
|
|
|
|
break;
|
|
|
|
}
|
2018-07-17 19:17:05 +00:00
|
|
|
switch (blk)
|
|
|
|
{
|
|
|
|
case eNTCP2BlkDateTime:
|
|
|
|
LogPrint (eLogDebug, "NTCP2: datetime");
|
2020-03-01 14:35:24 +00:00
|
|
|
break;
|
2018-07-17 19:17:05 +00:00
|
|
|
case eNTCP2BlkOptions:
|
|
|
|
LogPrint (eLogDebug, "NTCP2: options");
|
|
|
|
break;
|
|
|
|
case eNTCP2BlkRouterInfo:
|
2018-07-18 19:57:18 +00:00
|
|
|
{
|
2018-07-18 20:27:43 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP2: RouterInfo flag=", (int)frame[offset]);
|
2018-11-21 16:23:48 +00:00
|
|
|
i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, frame + offset, size));
|
2018-07-18 19:57:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-07-17 19:17:05 +00:00
|
|
|
case eNTCP2BlkI2NPMessage:
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "NTCP2: I2NP");
|
2018-10-10 15:31:55 +00:00
|
|
|
if (size > I2NP_MAX_MESSAGE_SIZE)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "NTCP2: I2NP block is too long ", size);
|
|
|
|
break;
|
|
|
|
}
|
2018-07-17 19:17:05 +00:00
|
|
|
auto nextMsg = NewI2NPMessage (size);
|
2018-11-27 15:35:17 +00:00
|
|
|
nextMsg->Align (12); // for possible tunnel msg
|
2018-07-17 19:17:05 +00:00
|
|
|
nextMsg->len = nextMsg->offset + size + 7; // 7 more bytes for full I2NP header
|
|
|
|
memcpy (nextMsg->GetNTCP2Header (), frame + offset, size);
|
|
|
|
nextMsg->FromNTCP2 ();
|
2020-03-01 14:35:24 +00:00
|
|
|
m_Handler.PutNextMessage (nextMsg);
|
2018-07-17 19:17:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eNTCP2BlkTermination:
|
2020-03-01 14:35:24 +00:00
|
|
|
if (size >= 9)
|
2018-08-01 13:43:48 +00:00
|
|
|
{
|
2018-08-02 19:31:15 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP2: termination. reason=", (int)(frame[offset + 8]));
|
2018-08-01 13:43:48 +00:00
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
else
|
2019-04-08 19:22:42 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP2: Unexpected termination block size ", size);
|
2018-07-17 19:17:05 +00:00
|
|
|
break;
|
|
|
|
case eNTCP2BlkPadding:
|
|
|
|
LogPrint (eLogDebug, "NTCP2: padding");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LogPrint (eLogWarning, "NTCP2: Unknown block type ", (int)blk);
|
|
|
|
}
|
2018-06-22 19:02:49 +00:00
|
|
|
offset += size;
|
|
|
|
}
|
2018-07-17 19:17:05 +00:00
|
|
|
m_Handler.Flush ();
|
2018-06-21 20:24:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 19:24:39 +00:00
|
|
|
void NTCP2Session::SetNextSentFrameLength (size_t frameLen, uint8_t * lengthBuf)
|
2018-06-25 16:28:07 +00:00
|
|
|
{
|
2018-12-02 19:24:39 +00:00
|
|
|
#if OPENSSL_SIPHASH
|
2018-09-11 17:26:29 +00:00
|
|
|
EVP_DigestSignInit (m_SendMDCtx, nullptr, nullptr, nullptr, nullptr);
|
2020-03-01 14:35:24 +00:00
|
|
|
EVP_DigestSignUpdate (m_SendMDCtx, m_SendIV.buf, 8);
|
|
|
|
size_t l = 8;
|
|
|
|
EVP_DigestSignFinal (m_SendMDCtx, m_SendIV.buf, &l);
|
2018-09-11 17:26:29 +00:00
|
|
|
#else
|
2018-08-13 17:43:51 +00:00
|
|
|
i2p::crypto::Siphash<8> (m_SendIV.buf, m_SendIV.buf, 8, m_SendSipKey);
|
2018-09-11 17:26:29 +00:00
|
|
|
#endif
|
2018-08-13 17:43:51 +00:00
|
|
|
// length must be in BigEndian
|
2018-12-02 19:24:39 +00:00
|
|
|
htobe16buf (lengthBuf, frameLen ^ le16toh (m_SendIV.key));
|
|
|
|
LogPrint (eLogDebug, "NTCP2: sent length ", frameLen);
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2018-07-18 15:16:40 +00:00
|
|
|
|
2018-12-02 19:24:39 +00:00
|
|
|
void NTCP2Session::SendI2NPMsgs (std::vector<std::shared_ptr<I2NPMessage> >& msgs)
|
|
|
|
{
|
|
|
|
if (msgs.empty () || IsTerminated ()) return;
|
2020-03-01 14:35:24 +00:00
|
|
|
|
2018-12-02 19:24:39 +00:00
|
|
|
size_t totalLen = 0;
|
|
|
|
std::vector<std::pair<uint8_t *, size_t> > encryptBufs;
|
2020-03-01 14:35:24 +00:00
|
|
|
std::vector<boost::asio::const_buffer> bufs;
|
2018-12-02 22:24:31 +00:00
|
|
|
std::shared_ptr<I2NPMessage> first;
|
|
|
|
uint8_t * macBuf = nullptr;
|
2020-03-01 14:35:24 +00:00
|
|
|
for (auto& it: msgs)
|
|
|
|
{
|
2018-12-02 22:24:31 +00:00
|
|
|
it->ToNTCP2 ();
|
2018-12-02 19:24:39 +00:00
|
|
|
auto buf = it->GetNTCP2Header ();
|
|
|
|
auto len = it->GetNTCP2Length ();
|
2018-12-02 22:24:31 +00:00
|
|
|
// block header
|
2020-03-01 14:35:24 +00:00
|
|
|
buf -= 3;
|
2018-12-02 22:24:31 +00:00
|
|
|
buf[0] = eNTCP2BlkI2NPMessage; // blk
|
|
|
|
htobe16buf (buf + 1, len); // size
|
2020-03-01 14:35:24 +00:00
|
|
|
len += 3;
|
|
|
|
totalLen += len;
|
2019-02-28 21:00:26 +00:00
|
|
|
encryptBufs.push_back ( {buf, len} );
|
2018-12-03 23:47:20 +00:00
|
|
|
if (&it == &msgs.front ()) // first message
|
2018-12-02 19:24:39 +00:00
|
|
|
{
|
|
|
|
// allocate two bytes for length
|
|
|
|
buf -= 2; len += 2;
|
|
|
|
first = it;
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2018-12-03 23:47:20 +00:00
|
|
|
if (&it == &msgs.back () && it->len + 16 < it->maxLen) // last message
|
2020-03-01 14:35:24 +00:00
|
|
|
{
|
2018-12-02 19:24:39 +00:00
|
|
|
// if it's long enough we add padding and MAC to it
|
|
|
|
// create padding block
|
2018-12-03 20:54:35 +00:00
|
|
|
auto paddingLen = CreatePaddingBlock (totalLen, buf + len, it->maxLen - it->len - 16);
|
2018-12-04 17:56:49 +00:00
|
|
|
if (paddingLen)
|
|
|
|
{
|
2019-02-28 21:00:26 +00:00
|
|
|
encryptBufs.push_back ( {buf + len, paddingLen} );
|
2018-12-04 17:56:49 +00:00
|
|
|
len += paddingLen;
|
|
|
|
totalLen += paddingLen;
|
|
|
|
}
|
2018-12-02 22:24:31 +00:00
|
|
|
macBuf = buf + len;
|
2018-12-03 17:29:24 +00:00
|
|
|
// allocate 16 bytes for MAC
|
|
|
|
len += 16;
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 19:24:39 +00:00
|
|
|
bufs.push_back (boost::asio::buffer (buf, len));
|
|
|
|
}
|
|
|
|
|
2018-12-02 22:24:31 +00:00
|
|
|
if (!macBuf) // last block was not enough for MAC
|
2018-12-02 19:24:39 +00:00
|
|
|
{
|
|
|
|
// allocate send buffer
|
|
|
|
m_NextSendBuffer = new uint8_t[287]; // can be any size > 16, we just allocate 287 frequently
|
|
|
|
// crate padding block
|
|
|
|
auto paddingLen = CreatePaddingBlock (totalLen, m_NextSendBuffer, 287 - 16);
|
|
|
|
// and padding block to encrypt and send
|
2018-12-04 17:56:49 +00:00
|
|
|
if (paddingLen)
|
2020-03-01 14:35:24 +00:00
|
|
|
encryptBufs.push_back ( {m_NextSendBuffer, paddingLen} );
|
2018-12-02 19:24:39 +00:00
|
|
|
bufs.push_back (boost::asio::buffer (m_NextSendBuffer, paddingLen + 16));
|
2018-12-02 22:24:31 +00:00
|
|
|
macBuf = m_NextSendBuffer + paddingLen;
|
2020-03-01 14:35:24 +00:00
|
|
|
totalLen += paddingLen;
|
2018-12-02 22:24:31 +00:00
|
|
|
}
|
|
|
|
uint8_t nonce[12];
|
|
|
|
CreateNonce (m_SendSequenceNumber, nonce); m_SendSequenceNumber++;
|
|
|
|
i2p::crypto::AEADChaCha20Poly1305Encrypt (encryptBufs, m_SendKey, nonce, macBuf); // encrypt buffers
|
2018-12-03 17:29:24 +00:00
|
|
|
SetNextSentFrameLength (totalLen + 16, first->GetNTCP2Header () - 5); // frame length right before first block
|
2020-03-01 14:35:24 +00:00
|
|
|
|
2018-12-02 19:24:39 +00:00
|
|
|
// send buffers
|
2020-03-01 14:35:24 +00:00
|
|
|
m_IsSending = true;
|
2018-12-02 19:24:39 +00:00
|
|
|
boost::asio::async_write (m_Socket, bufs, boost::asio::transfer_all (),
|
|
|
|
std::bind(&NTCP2Session::HandleI2NPMsgsSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, msgs));
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2018-12-02 19:24:39 +00:00
|
|
|
|
|
|
|
void NTCP2Session::HandleI2NPMsgsSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, std::vector<std::shared_ptr<I2NPMessage> > msgs)
|
|
|
|
{
|
|
|
|
HandleNextFrameSent (ecode, bytes_transferred);
|
|
|
|
// msgs get destroyed here
|
|
|
|
}
|
2020-03-01 14:35:24 +00:00
|
|
|
|
2018-12-04 20:56:55 +00:00
|
|
|
void NTCP2Session::EncryptAndSendNextBuffer (size_t payloadLen)
|
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
if (IsTerminated ())
|
2018-12-04 21:10:52 +00:00
|
|
|
{
|
|
|
|
delete[] m_NextSendBuffer; m_NextSendBuffer = nullptr;
|
2020-03-01 14:35:24 +00:00
|
|
|
return;
|
2018-12-04 21:10:52 +00:00
|
|
|
}
|
2018-12-04 20:56:55 +00:00
|
|
|
// encrypt
|
|
|
|
uint8_t nonce[12];
|
|
|
|
CreateNonce (m_SendSequenceNumber, nonce); m_SendSequenceNumber++;
|
2020-03-01 14:35:24 +00:00
|
|
|
i2p::crypto::AEADChaCha20Poly1305Encrypt ({ {m_NextSendBuffer + 2, payloadLen} }, m_SendKey, nonce, m_NextSendBuffer + payloadLen + 2);
|
2018-12-04 20:56:55 +00:00
|
|
|
SetNextSentFrameLength (payloadLen + 16, m_NextSendBuffer);
|
|
|
|
// send
|
2020-03-01 14:35:24 +00:00
|
|
|
m_IsSending = true;
|
2018-12-04 20:56:55 +00:00
|
|
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_NextSendBuffer, payloadLen + 16 + 2), boost::asio::transfer_all (),
|
|
|
|
std::bind(&NTCP2Session::HandleNextFrameSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
|
|
|
}
|
|
|
|
|
2018-12-04 20:23:43 +00:00
|
|
|
void NTCP2Session::HandleNextFrameSent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
m_IsSending = false;
|
|
|
|
delete[] m_NextSendBuffer; m_NextSendBuffer = nullptr;
|
2020-03-01 14:35:24 +00:00
|
|
|
|
2018-12-04 20:23:43 +00:00
|
|
|
if (ecode)
|
|
|
|
{
|
2019-11-18 19:13:31 +00:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
LogPrint (eLogWarning, "NTCP2: Couldn't send frame ", ecode.message ());
|
|
|
|
Terminate ();
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2018-12-04 20:23:43 +00:00
|
|
|
else
|
2020-03-01 14:35:24 +00:00
|
|
|
{
|
2018-12-04 20:23:43 +00:00
|
|
|
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
m_NumSentBytes += bytes_transferred;
|
|
|
|
i2p::transport::transports.UpdateSentBytes (bytes_transferred);
|
|
|
|
LogPrint (eLogDebug, "NTCP2: Next frame sent ", bytes_transferred);
|
|
|
|
SendQueue ();
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2018-12-04 20:23:43 +00:00
|
|
|
}
|
2020-03-01 14:35:24 +00:00
|
|
|
|
2018-07-18 15:16:40 +00:00
|
|
|
void NTCP2Session::SendQueue ()
|
|
|
|
{
|
|
|
|
if (!m_SendQueue.empty ())
|
|
|
|
{
|
2018-12-03 19:14:36 +00:00
|
|
|
std::vector<std::shared_ptr<I2NPMessage> > msgs;
|
2018-07-18 15:16:40 +00:00
|
|
|
size_t s = 0;
|
|
|
|
while (!m_SendQueue.empty ())
|
|
|
|
{
|
|
|
|
auto msg = m_SendQueue.front ();
|
2020-03-01 14:35:24 +00:00
|
|
|
size_t len = msg->GetNTCP2Length ();
|
2018-07-18 15:16:40 +00:00
|
|
|
if (s + len + 3 <= NTCP2_UNENCRYPTED_FRAME_MAX_SIZE) // 3 bytes block header
|
|
|
|
{
|
2018-12-03 19:14:36 +00:00
|
|
|
msgs.push_back (msg);
|
|
|
|
s += (len + 3);
|
2018-07-18 15:16:40 +00:00
|
|
|
m_SendQueue.pop_front ();
|
|
|
|
}
|
2018-10-10 15:31:55 +00:00
|
|
|
else if (len + 3 > NTCP2_UNENCRYPTED_FRAME_MAX_SIZE)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "NTCP2: I2NP message of size ", len, " can't be sent. Dropped");
|
|
|
|
m_SendQueue.pop_front ();
|
|
|
|
}
|
2018-07-18 15:16:40 +00:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2018-12-03 19:14:36 +00:00
|
|
|
SendI2NPMsgs (msgs);
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2018-07-18 15:16:40 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 19:24:39 +00:00
|
|
|
size_t NTCP2Session::CreatePaddingBlock (size_t msgLen, uint8_t * buf, size_t len)
|
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
if (len < 3) return 0;
|
2018-12-02 19:24:39 +00:00
|
|
|
len -= 3;
|
2018-12-04 20:00:10 +00:00
|
|
|
if (msgLen < 256) msgLen = 256; // for short message padding should not be always zero
|
2018-12-02 19:24:39 +00:00
|
|
|
size_t paddingSize = (msgLen*NTCP2_MAX_PADDING_RATIO)/100;
|
|
|
|
if (msgLen + paddingSize + 3 > NTCP2_UNENCRYPTED_FRAME_MAX_SIZE) paddingSize = NTCP2_UNENCRYPTED_FRAME_MAX_SIZE - msgLen -3;
|
|
|
|
if (paddingSize > len) paddingSize = len;
|
|
|
|
if (paddingSize) paddingSize = rand () % paddingSize;
|
|
|
|
buf[0] = eNTCP2BlkPadding; // blk
|
|
|
|
htobe16buf (buf + 1, paddingSize); // size
|
2020-03-01 14:35:24 +00:00
|
|
|
memset (buf + 3, 0, paddingSize);
|
2018-12-02 19:24:39 +00:00
|
|
|
return paddingSize + 3;
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 16:42:39 +00:00
|
|
|
void NTCP2Session::SendRouterInfo ()
|
|
|
|
{
|
2018-08-23 11:23:50 +00:00
|
|
|
if (!IsEstablished ()) return;
|
2018-08-02 16:42:39 +00:00
|
|
|
auto riLen = i2p::context.GetRouterInfo ().GetBufferLen ();
|
2018-12-04 20:56:55 +00:00
|
|
|
size_t payloadLen = riLen + 4; // 3 bytes block header + 1 byte RI flag
|
|
|
|
m_NextSendBuffer = new uint8_t[payloadLen + 16 + 2 + 64]; // up to 64 bytes padding
|
|
|
|
m_NextSendBuffer[2] = eNTCP2BlkRouterInfo;
|
|
|
|
htobe16buf (m_NextSendBuffer + 3, riLen + 1); // size
|
|
|
|
m_NextSendBuffer[5] = 0; // flag
|
|
|
|
memcpy (m_NextSendBuffer + 6, i2p::context.GetRouterInfo ().GetBuffer (), riLen);
|
|
|
|
// padding block
|
|
|
|
auto paddingSize = CreatePaddingBlock (payloadLen, m_NextSendBuffer + 2 + payloadLen, 64);
|
|
|
|
payloadLen += paddingSize;
|
|
|
|
// encrypt and send
|
|
|
|
EncryptAndSendNextBuffer (payloadLen);
|
2018-08-02 16:42:39 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 19:31:15 +00:00
|
|
|
void NTCP2Session::SendTermination (NTCP2TerminationReason reason)
|
|
|
|
{
|
2018-11-04 23:51:25 +00:00
|
|
|
if (!m_SendKey || !m_SendSipKey) return;
|
2020-03-01 14:35:24 +00:00
|
|
|
m_NextSendBuffer = new uint8_t[49]; // 49 = 12 bytes message + 16 bytes MAC + 2 bytes size + up to 19 padding block
|
2018-12-04 20:23:43 +00:00
|
|
|
// termination block
|
|
|
|
m_NextSendBuffer[2] = eNTCP2BlkTermination;
|
|
|
|
m_NextSendBuffer[3] = 0; m_NextSendBuffer[4] = 9; // 9 bytes block size
|
2020-03-01 14:35:24 +00:00
|
|
|
htobe64buf (m_NextSendBuffer + 5, m_ReceiveSequenceNumber);
|
2018-12-04 20:23:43 +00:00
|
|
|
m_NextSendBuffer[13] = (uint8_t)reason;
|
|
|
|
// padding block
|
|
|
|
auto paddingSize = CreatePaddingBlock (12, m_NextSendBuffer + 14, 19);
|
2018-12-04 20:56:55 +00:00
|
|
|
// encrypt and send
|
|
|
|
EncryptAndSendNextBuffer (paddingSize + 12);
|
2018-08-02 19:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Session::SendTerminationAndTerminate (NTCP2TerminationReason reason)
|
|
|
|
{
|
|
|
|
SendTermination (reason);
|
|
|
|
m_Server.GetService ().post (std::bind (&NTCP2Session::Terminate, shared_from_this ())); // let termination message go
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:16:40 +00:00
|
|
|
void NTCP2Session::SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs)
|
|
|
|
{
|
2018-07-19 16:46:19 +00:00
|
|
|
m_Server.GetService ().post (std::bind (&NTCP2Session::PostI2NPMessages, shared_from_this (), msgs));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Session::PostI2NPMessages (std::vector<std::shared_ptr<I2NPMessage> > msgs)
|
|
|
|
{
|
|
|
|
if (m_IsTerminated) return;
|
2018-07-18 15:16:40 +00:00
|
|
|
for (auto it: msgs)
|
|
|
|
m_SendQueue.push_back (it);
|
2020-03-01 14:35:24 +00:00
|
|
|
if (!m_IsSending)
|
|
|
|
SendQueue ();
|
2018-09-28 13:54:42 +00:00
|
|
|
else if (m_SendQueue.size () > NTCP2_MAX_OUTGOING_QUEUE_SIZE)
|
|
|
|
{
|
2020-05-24 18:14:16 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP2: outgoing messages queue size to ",
|
|
|
|
GetIdentHashBase64(), " exceeds ", NTCP2_MAX_OUTGOING_QUEUE_SIZE);
|
2018-09-28 13:54:42 +00:00
|
|
|
Terminate ();
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2018-06-25 16:28:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 15:27:27 +00:00
|
|
|
void NTCP2Session::SendLocalRouterInfo ()
|
|
|
|
{
|
|
|
|
if (!IsOutgoing ()) // we send it in SessionConfirmed
|
2018-08-22 16:33:43 +00:00
|
|
|
m_Server.GetService ().post (std::bind (&NTCP2Session::SendRouterInfo, shared_from_this ()));
|
2018-08-14 15:27:27 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 16:29:30 +00:00
|
|
|
NTCP2Server::NTCP2Server ():
|
2020-03-01 16:24:18 +00:00
|
|
|
RunnableServiceWithWork ("NTCP2"), m_TerminationTimer (GetService ()),
|
2020-04-30 17:45:26 +00:00
|
|
|
m_ProxyType(eNoProxy), m_Resolver(GetService ())
|
2018-06-11 16:29:30 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NTCP2Server::~NTCP2Server ()
|
|
|
|
{
|
|
|
|
Stop ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Server::Start ()
|
|
|
|
{
|
2020-02-02 22:05:30 +00:00
|
|
|
if (!IsRunning ())
|
2018-06-11 16:29:30 +00:00
|
|
|
{
|
2020-02-04 16:48:56 +00:00
|
|
|
StartIOService ();
|
2020-03-01 14:35:24 +00:00
|
|
|
if(UsingProxy())
|
|
|
|
{
|
2020-04-30 01:44:13 +00:00
|
|
|
LogPrint(eLogInfo, "NTCP2: Using proxy to connect to peers");
|
2020-03-01 14:35:24 +00:00
|
|
|
// TODO: resolve proxy until it is resolved
|
|
|
|
boost::asio::ip::tcp::resolver::query q(m_ProxyAddress, std::to_string(m_ProxyPort));
|
|
|
|
boost::system::error_code e;
|
|
|
|
auto itr = m_Resolver.resolve(q, e);
|
|
|
|
if(e)
|
|
|
|
LogPrint(eLogError, "NTCP2: Failed to resolve proxy ", e.message());
|
|
|
|
else
|
|
|
|
{
|
2020-03-01 16:24:18 +00:00
|
|
|
m_ProxyEndpoint.reset (new boost::asio::ip::tcp::endpoint(*itr));
|
2020-03-01 14:35:24 +00:00
|
|
|
if (m_ProxyEndpoint)
|
2020-04-30 01:44:13 +00:00
|
|
|
LogPrint(eLogDebug, "NTCP2: m_ProxyEndpoint ", *m_ProxyEndpoint);
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2018-07-23 19:30:51 +00:00
|
|
|
{
|
2020-04-30 01:44:13 +00:00
|
|
|
LogPrint(eLogInfo, "NTCP2: Proxy is not used");
|
2020-03-01 14:35:24 +00:00
|
|
|
auto& addresses = context.GetRouterInfo ().GetAddresses ();
|
|
|
|
for (const auto& address: addresses)
|
2018-07-23 19:30:51 +00:00
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
if (!address) continue;
|
|
|
|
if (address->IsPublishedNTCP2 ())
|
2018-07-23 19:30:51 +00:00
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
if (address->host.is_v4())
|
2018-07-23 19:30:51 +00:00
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_NTCP2Acceptor.reset (new boost::asio::ip::tcp::acceptor (GetService (), boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address->port)));
|
|
|
|
}
|
|
|
|
catch ( std::exception & ex )
|
|
|
|
{
|
2020-05-04 23:36:34 +00:00
|
|
|
LogPrint(eLogError, "NTCP2: Failed to bind to v4 port ", address->port, ex.what());
|
2020-05-05 13:35:41 +00:00
|
|
|
ThrowFatal ("Unable to start IPv4 NTCP2 transport at port ", address->port, ": ", ex.what ());
|
2020-03-01 14:35:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
LogPrint (eLogInfo, "NTCP2: Start listening v4 TCP port ", address->port);
|
2020-03-01 14:35:24 +00:00
|
|
|
auto conn = std::make_shared<NTCP2Session>(*this);
|
|
|
|
m_NTCP2Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCP2Server::HandleAccept, this, conn, std::placeholders::_1));
|
2018-07-23 19:30:51 +00:00
|
|
|
}
|
2020-03-01 14:35:24 +00:00
|
|
|
else if (address->host.is_v6() && context.SupportsV6 ())
|
2018-07-23 19:30:51 +00:00
|
|
|
{
|
2020-03-01 14:35:24 +00:00
|
|
|
m_NTCP2V6Acceptor.reset (new boost::asio::ip::tcp::acceptor (GetService ()));
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_NTCP2V6Acceptor->open (boost::asio::ip::tcp::v6());
|
|
|
|
m_NTCP2V6Acceptor->set_option (boost::asio::ip::v6_only (true));
|
|
|
|
m_NTCP2V6Acceptor->set_option (boost::asio::socket_base::reuse_address (true));
|
|
|
|
m_NTCP2V6Acceptor->bind (boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v6(), address->port));
|
|
|
|
m_NTCP2V6Acceptor->listen ();
|
|
|
|
|
2019-05-17 07:28:58 +00:00
|
|
|
LogPrint (eLogInfo, "NTCP2: Start listening v6 TCP port ", address->port);
|
2020-03-01 14:35:24 +00:00
|
|
|
auto conn = std::make_shared<NTCP2Session> (*this);
|
|
|
|
m_NTCP2V6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCP2Server::HandleAcceptV6, this, conn, std::placeholders::_1));
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
|
|
|
catch ( std::exception & ex )
|
2020-05-05 13:35:41 +00:00
|
|
|
{
|
2020-05-04 23:36:34 +00:00
|
|
|
LogPrint(eLogError, "NTCP2: failed to bind to v6 port ", address->port, ": ", ex.what());
|
2020-05-05 13:35:41 +00:00
|
|
|
ThrowFatal ("Unable to start IPv6 NTCP2 transport at port ", address->port, ": ", ex.what ());
|
2020-03-01 14:35:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-07-23 19:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 17:10:32 +00:00
|
|
|
ScheduleTermination ();
|
2018-06-11 16:29:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Server::Stop ()
|
|
|
|
{
|
2018-07-18 16:58:29 +00:00
|
|
|
{
|
|
|
|
// we have to copy it because Terminate changes m_NTCP2Sessions
|
|
|
|
auto ntcpSessions = m_NTCP2Sessions;
|
|
|
|
for (auto& it: ntcpSessions)
|
|
|
|
it.second->Terminate ();
|
2018-07-23 19:30:51 +00:00
|
|
|
for (auto& it: m_PendingIncomingSessions)
|
|
|
|
it->Terminate ();
|
2018-07-18 16:58:29 +00:00
|
|
|
}
|
|
|
|
m_NTCP2Sessions.clear ();
|
|
|
|
|
2020-02-02 22:05:30 +00:00
|
|
|
if (IsRunning ())
|
2020-03-01 14:35:24 +00:00
|
|
|
{
|
2018-08-03 17:19:35 +00:00
|
|
|
m_TerminationTimer.cancel ();
|
2020-03-01 16:24:18 +00:00
|
|
|
m_ProxyEndpoint = nullptr;
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2020-02-04 16:48:56 +00:00
|
|
|
StopIOService ();
|
2018-06-11 16:29:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 01:01:34 +00:00
|
|
|
bool NTCP2Server::AddNTCP2Session (std::shared_ptr<NTCP2Session> session, bool incoming)
|
2018-07-18 16:58:29 +00:00
|
|
|
{
|
2019-11-18 19:13:31 +00:00
|
|
|
if (!session) return false;
|
2020-03-01 14:35:24 +00:00
|
|
|
if (incoming)
|
2019-11-18 19:13:31 +00:00
|
|
|
m_PendingIncomingSessions.remove (session);
|
|
|
|
if (!session->GetRemoteIdentity ()) return false;
|
2018-07-18 16:58:29 +00:00
|
|
|
auto& ident = session->GetRemoteIdentity ()->GetIdentHash ();
|
|
|
|
auto it = m_NTCP2Sessions.find (ident);
|
|
|
|
if (it != m_NTCP2Sessions.end ())
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP2: session to ", ident.ToBase64 (), " already exists");
|
2020-04-09 19:00:38 +00:00
|
|
|
if (incoming)
|
|
|
|
// replace by new session
|
|
|
|
it->second->Terminate ();
|
|
|
|
else
|
|
|
|
return false;
|
2018-07-18 16:58:29 +00:00
|
|
|
}
|
|
|
|
m_NTCP2Sessions.insert (std::make_pair (ident, session));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Server::RemoveNTCP2Session (std::shared_ptr<NTCP2Session> session)
|
|
|
|
{
|
|
|
|
if (session && session->GetRemoteIdentity ())
|
|
|
|
m_NTCP2Sessions.erase (session->GetRemoteIdentity ()->GetIdentHash ());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<NTCP2Session> NTCP2Server::FindNTCP2Session (const i2p::data::IdentHash& ident)
|
|
|
|
{
|
|
|
|
auto it = m_NTCP2Sessions.find (ident);
|
|
|
|
if (it != m_NTCP2Sessions.end ())
|
|
|
|
return it->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-06-11 16:29:30 +00:00
|
|
|
void NTCP2Server::Connect(const boost::asio::ip::address & address, uint16_t port, std::shared_ptr<NTCP2Session> conn)
|
|
|
|
{
|
2018-06-11 18:05:30 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP2: Connecting to ", address ,":", port);
|
2020-03-01 14:35:24 +00:00
|
|
|
GetService ().post([this, address, port, conn]()
|
2018-06-11 16:29:30 +00:00
|
|
|
{
|
2018-07-18 16:58:29 +00:00
|
|
|
if (this->AddNTCP2Session (conn))
|
|
|
|
{
|
2020-02-02 22:05:30 +00:00
|
|
|
auto timer = std::make_shared<boost::asio::deadline_timer>(GetService ());
|
2018-08-03 17:10:32 +00:00
|
|
|
auto timeout = NTCP2_CONNECT_TIMEOUT * 5;
|
|
|
|
conn->SetTerminationTimeout(timeout * 2);
|
|
|
|
timer->expires_from_now (boost::posix_time::seconds(timeout));
|
2020-03-01 14:35:24 +00:00
|
|
|
timer->async_wait ([conn, timeout](const boost::system::error_code& ecode)
|
2018-08-03 17:10:32 +00:00
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "NTCP2: Not connected in ", timeout, " seconds");
|
2020-06-23 02:32:18 +00:00
|
|
|
if (conn->GetRemoteIdentity ())
|
|
|
|
i2p::data::netdb.SetUnreachable (conn->GetRemoteIdentity ()->GetIdentHash (), true);
|
2018-08-03 17:10:32 +00:00
|
|
|
conn->Terminate ();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
conn->GetSocket ().async_connect (boost::asio::ip::tcp::endpoint (address, port), std::bind (&NTCP2Server::HandleConnect, this, std::placeholders::_1, conn, timer));
|
2018-07-18 16:58:29 +00:00
|
|
|
}
|
2020-04-08 22:02:12 +00:00
|
|
|
else
|
|
|
|
conn->Terminate ();
|
2018-06-11 16:29:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-03 17:10:32 +00:00
|
|
|
void NTCP2Server::HandleConnect (const boost::system::error_code& ecode, std::shared_ptr<NTCP2Session> conn, std::shared_ptr<boost::asio::deadline_timer> timer)
|
2018-06-11 16:29:30 +00:00
|
|
|
{
|
2018-08-03 17:10:32 +00:00
|
|
|
timer->cancel ();
|
2018-06-11 16:29:30 +00:00
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "NTCP2: Connect error ", ecode.message ());
|
|
|
|
conn->Terminate ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "NTCP2: Connected to ", conn->GetSocket ().remote_endpoint ());
|
|
|
|
conn->ClientLogin ();
|
|
|
|
}
|
|
|
|
}
|
2018-07-23 19:30:51 +00:00
|
|
|
|
|
|
|
void NTCP2Server::HandleAccept (std::shared_ptr<NTCP2Session> conn, const boost::system::error_code& error)
|
|
|
|
{
|
|
|
|
if (!error)
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
auto ep = conn->GetSocket ().remote_endpoint(ec);
|
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "NTCP2: Connected from ", ep);
|
|
|
|
if (conn)
|
|
|
|
{
|
|
|
|
conn->ServerLogin ();
|
2018-08-03 17:10:32 +00:00
|
|
|
m_PendingIncomingSessions.push_back (conn);
|
2019-11-20 17:05:32 +00:00
|
|
|
conn = nullptr;
|
2018-07-23 19:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint (eLogError, "NTCP2: Connected from error ", ec.message ());
|
|
|
|
}
|
2019-11-20 17:05:32 +00:00
|
|
|
else
|
|
|
|
LogPrint (eLogError, "NTCP2: Accept error ", error.message ());
|
2018-07-23 19:30:51 +00:00
|
|
|
|
|
|
|
if (error != boost::asio::error::operation_aborted)
|
|
|
|
{
|
2019-11-20 18:00:50 +00:00
|
|
|
if (!conn) // connection is used, create new one
|
2019-11-20 17:05:32 +00:00
|
|
|
conn = std::make_shared<NTCP2Session> (*this);
|
2019-11-20 18:00:50 +00:00
|
|
|
else // reuse failed
|
|
|
|
conn->Close ();
|
2018-07-23 19:30:51 +00:00
|
|
|
m_NTCP2Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCP2Server::HandleAccept, this,
|
|
|
|
conn, std::placeholders::_1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Server::HandleAcceptV6 (std::shared_ptr<NTCP2Session> conn, const boost::system::error_code& error)
|
|
|
|
{
|
|
|
|
if (!error)
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
auto ep = conn->GetSocket ().remote_endpoint(ec);
|
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "NTCP2: Connected from ", ep);
|
|
|
|
if (conn)
|
|
|
|
{
|
|
|
|
conn->ServerLogin ();
|
2018-08-03 17:10:32 +00:00
|
|
|
m_PendingIncomingSessions.push_back (conn);
|
2018-07-23 19:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint (eLogError, "NTCP2: Connected from error ", ec.message ());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
conn = std::make_shared<NTCP2Session> (*this);
|
|
|
|
m_NTCP2V6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCP2Server::HandleAcceptV6, this,
|
|
|
|
conn, std::placeholders::_1));
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 17:10:32 +00:00
|
|
|
|
|
|
|
void NTCP2Server::ScheduleTermination ()
|
|
|
|
{
|
|
|
|
m_TerminationTimer.expires_from_now (boost::posix_time::seconds(NTCP2_TERMINATION_CHECK_TIMEOUT));
|
|
|
|
m_TerminationTimer.async_wait (std::bind (&NTCP2Server::HandleTerminationTimer,
|
|
|
|
this, std::placeholders::_1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Server::HandleTerminationTimer (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
// established
|
|
|
|
for (auto& it: m_NTCP2Sessions)
|
|
|
|
if (it.second->IsTerminationTimeoutExpired (ts))
|
|
|
|
{
|
|
|
|
auto session = it.second;
|
|
|
|
LogPrint (eLogDebug, "NTCP2: No activity for ", session->GetTerminationTimeout (), " seconds");
|
|
|
|
session->TerminateByTimeout (); // it doesn't change m_NTCP2Session right a way
|
|
|
|
}
|
|
|
|
// pending
|
|
|
|
for (auto it = m_PendingIncomingSessions.begin (); it != m_PendingIncomingSessions.end ();)
|
|
|
|
{
|
2019-11-20 17:05:32 +00:00
|
|
|
if ((*it)->IsEstablished () || (*it)->IsTerminationTimeoutExpired (ts))
|
2018-08-03 17:10:32 +00:00
|
|
|
{
|
|
|
|
(*it)->Terminate ();
|
2020-04-30 01:59:05 +00:00
|
|
|
it = m_PendingIncomingSessions.erase (it); // established of expired
|
2018-08-03 17:10:32 +00:00
|
|
|
}
|
2019-11-20 17:05:32 +00:00
|
|
|
else if ((*it)->IsTerminated ())
|
|
|
|
it = m_PendingIncomingSessions.erase (it); // already terminated
|
2018-08-03 17:10:32 +00:00
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScheduleTermination ();
|
|
|
|
}
|
|
|
|
}
|
2020-03-02 21:24:00 +00:00
|
|
|
|
2020-04-30 01:44:13 +00:00
|
|
|
void NTCP2Server::ConnectWithProxy (const std::string& host, uint16_t port, RemoteAddressType addrtype, std::shared_ptr<NTCP2Session> conn)
|
|
|
|
{
|
|
|
|
if(!m_ProxyEndpoint) return;
|
|
|
|
GetService().post([this, host, port, addrtype, conn]() {
|
|
|
|
if (this->AddNTCP2Session (conn))
|
|
|
|
{
|
|
|
|
|
|
|
|
auto timer = std::make_shared<boost::asio::deadline_timer>(GetService());
|
|
|
|
auto timeout = NTCP_CONNECT_TIMEOUT * 5;
|
|
|
|
conn->SetTerminationTimeout(timeout * 2);
|
|
|
|
timer->expires_from_now (boost::posix_time::seconds(timeout));
|
|
|
|
timer->async_wait ([conn, timeout](const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "NTCP2: Not connected in ", timeout, " seconds");
|
|
|
|
i2p::data::netdb.SetUnreachable (conn->GetRemoteIdentity ()->GetIdentHash (), true);
|
|
|
|
conn->Terminate ();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
conn->GetSocket ().async_connect (*m_ProxyEndpoint, std::bind (&NTCP2Server::HandleProxyConnect, this, std::placeholders::_1, conn, timer, host, port, addrtype));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-02 21:24:00 +00:00
|
|
|
void NTCP2Server::UseProxy(ProxyType proxytype, const std::string & addr, uint16_t port)
|
|
|
|
{
|
|
|
|
m_ProxyType = proxytype;
|
|
|
|
m_ProxyAddress = addr;
|
|
|
|
m_ProxyPort = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Server::HandleProxyConnect(const boost::system::error_code& ecode, std::shared_ptr<NTCP2Session> conn, std::shared_ptr<boost::asio::deadline_timer> timer, const std::string & host, uint16_t port, RemoteAddressType addrtype)
|
|
|
|
{
|
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint(eLogWarning, "NTCP2: failed to connect to proxy ", ecode.message());
|
|
|
|
timer->cancel();
|
|
|
|
conn->Terminate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (m_ProxyType)
|
2020-04-30 01:44:13 +00:00
|
|
|
{
|
2020-03-02 21:24:00 +00:00
|
|
|
case eSocksProxy:
|
|
|
|
{
|
|
|
|
// TODO: support username/password auth etc
|
|
|
|
static const uint8_t buff[3] = {0x05, 0x01, 0x00};
|
2020-04-30 01:44:13 +00:00
|
|
|
boost::asio::async_write(conn->GetSocket(), boost::asio::buffer(buff, 3), boost::asio::transfer_all(),
|
|
|
|
[] (const boost::system::error_code & ec, std::size_t transferred)
|
|
|
|
{
|
2020-03-02 21:24:00 +00:00
|
|
|
(void) transferred;
|
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
LogPrint(eLogWarning, "NTCP2: socks5 write error ", ec.message());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
auto readbuff = std::make_shared<std::vector<uint8_t> >(2);
|
2020-03-01 10:25:50 +00:00
|
|
|
boost::asio::async_read(conn->GetSocket(), boost::asio::buffer(readbuff->data (), 2),
|
2020-03-02 21:24:00 +00:00
|
|
|
[this, readbuff, timer, conn, host, port, addrtype](const boost::system::error_code & ec, std::size_t transferred)
|
|
|
|
{
|
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
LogPrint(eLogError, "NTCP2: socks5 read error ", ec.message());
|
|
|
|
timer->cancel();
|
|
|
|
conn->Terminate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(transferred == 2)
|
|
|
|
{
|
|
|
|
if((*readbuff)[1] == 0x00)
|
|
|
|
{
|
|
|
|
AfterSocksHandshake(conn, timer, host, port, addrtype);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if ((*readbuff)[1] == 0xff)
|
|
|
|
{
|
|
|
|
LogPrint(eLogError, "NTCP2: socks5 proxy rejected authentication");
|
|
|
|
timer->cancel();
|
|
|
|
conn->Terminate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LogPrint(eLogError, "NTCP2:", (int)(*readbuff)[1]);
|
|
|
|
}
|
|
|
|
LogPrint(eLogError, "NTCP2: socks5 server gave invalid response");
|
|
|
|
timer->cancel();
|
|
|
|
conn->Terminate();
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eHTTPProxy:
|
|
|
|
{
|
|
|
|
i2p::http::HTTPReq req;
|
|
|
|
req.method = "CONNECT";
|
|
|
|
req.version ="HTTP/1.1";
|
|
|
|
if(addrtype == eIP6Address)
|
|
|
|
req.uri = "[" + host + "]:" + std::to_string(port);
|
|
|
|
else
|
|
|
|
req.uri = host + ":" + std::to_string(port);
|
|
|
|
|
|
|
|
boost::asio::streambuf writebuff;
|
|
|
|
std::ostream out(&writebuff);
|
|
|
|
out << req.to_string();
|
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
boost::asio::async_write(conn->GetSocket(), writebuff.data(), boost::asio::transfer_all(),
|
|
|
|
[](const boost::system::error_code & ec, std::size_t transferred)
|
2020-04-30 01:44:13 +00:00
|
|
|
{
|
2020-03-02 21:24:00 +00:00
|
|
|
(void) transferred;
|
|
|
|
if(ec)
|
|
|
|
LogPrint(eLogError, "NTCP2: http proxy write error ", ec.message());
|
|
|
|
});
|
|
|
|
|
|
|
|
boost::asio::streambuf * readbuff = new boost::asio::streambuf;
|
2020-03-01 10:25:50 +00:00
|
|
|
boost::asio::async_read_until(conn->GetSocket(), *readbuff, "\r\n\r\n",
|
|
|
|
[this, readbuff, timer, conn] (const boost::system::error_code & ec, std::size_t transferred)
|
2020-03-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
LogPrint(eLogError, "NTCP2: http proxy read error ", ec.message());
|
|
|
|
timer->cancel();
|
|
|
|
conn->Terminate();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
readbuff->commit(transferred);
|
|
|
|
i2p::http::HTTPRes res;
|
|
|
|
if(res.parse(boost::asio::buffer_cast<const char*>(readbuff->data()), readbuff->size()) > 0)
|
|
|
|
{
|
|
|
|
if(res.code == 200)
|
|
|
|
{
|
|
|
|
timer->cancel();
|
|
|
|
conn->ClientLogin();
|
|
|
|
delete readbuff;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint(eLogError, "NTCP2: http proxy rejected request ", res.code);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint(eLogError, "NTCP2: http proxy gave malformed response");
|
|
|
|
timer->cancel();
|
|
|
|
conn->Terminate();
|
|
|
|
delete readbuff;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
LogPrint(eLogError, "NTCP2: unknown proxy type, invalid state");
|
2020-04-30 01:44:13 +00:00
|
|
|
}
|
2020-03-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCP2Server::AfterSocksHandshake(std::shared_ptr<NTCP2Session> conn, std::shared_ptr<boost::asio::deadline_timer> timer, const std::string & host, uint16_t port, RemoteAddressType addrtype)
|
|
|
|
{
|
|
|
|
// build request
|
2020-04-30 01:44:13 +00:00
|
|
|
size_t sz = 6; // header + port
|
2020-03-02 21:24:00 +00:00
|
|
|
auto buff = std::make_shared<std::vector<int8_t> >(256);
|
|
|
|
auto readbuff = std::make_shared<std::vector<int8_t> >(256);
|
|
|
|
(*buff)[0] = 0x05;
|
|
|
|
(*buff)[1] = 0x01;
|
|
|
|
(*buff)[2] = 0x00;
|
|
|
|
|
|
|
|
if(addrtype == eIP4Address)
|
|
|
|
{
|
|
|
|
(*buff)[3] = 0x01;
|
2020-04-30 01:44:13 +00:00
|
|
|
auto addrbytes = boost::asio::ip::address::from_string(host).to_v4().to_bytes();
|
|
|
|
sz += 4;
|
|
|
|
memcpy(buff->data () + 4, addrbytes.data(), 4);
|
2020-03-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
else if (addrtype == eIP6Address)
|
|
|
|
{
|
|
|
|
(*buff)[3] = 0x04;
|
2020-04-30 01:44:13 +00:00
|
|
|
auto addrbytes = boost::asio::ip::address::from_string(host).to_v6().to_bytes();
|
|
|
|
sz += 16;
|
|
|
|
memcpy(buff->data () + 4, addrbytes.data(), 16);
|
2020-03-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
else if (addrtype == eHostname)
|
|
|
|
{
|
2020-04-30 01:44:13 +00:00
|
|
|
// We mustn't really fall here because all connections are made to IP addresses
|
|
|
|
LogPrint(eLogError, "NTCP2: Tried to connect to domain name via socks proxy");
|
|
|
|
return;
|
2020-03-02 21:24:00 +00:00
|
|
|
}
|
2020-04-30 01:44:13 +00:00
|
|
|
htobe16buf(buff->data () + sz - 2, port);
|
|
|
|
boost::asio::async_write(conn->GetSocket(), boost::asio::buffer(buff->data (), sz), boost::asio::transfer_all(),
|
|
|
|
[buff](const boost::system::error_code & ec, std::size_t written)
|
|
|
|
{
|
2020-03-02 21:24:00 +00:00
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
LogPrint(eLogError, "NTCP2: failed to write handshake to socks proxy ", ec.message());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-30 01:44:13 +00:00
|
|
|
boost::asio::async_read(conn->GetSocket(), boost::asio::buffer(readbuff->data (), 10),
|
|
|
|
[timer, conn, sz, readbuff](const boost::system::error_code & e, std::size_t transferred)
|
|
|
|
{
|
2020-03-02 21:24:00 +00:00
|
|
|
if(e)
|
|
|
|
{
|
|
|
|
LogPrint(eLogError, "NTCP2: socks proxy read error ", e.message());
|
|
|
|
}
|
|
|
|
else if(transferred == sz)
|
|
|
|
{
|
|
|
|
if((*readbuff)[1] == 0x00)
|
|
|
|
{
|
|
|
|
timer->cancel();
|
|
|
|
conn->ClientLogin();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!e)
|
|
|
|
i2p::data::netdb.SetUnreachable (conn->GetRemoteIdentity ()->GetIdentHash (), true);
|
|
|
|
timer->cancel();
|
|
|
|
conn->Terminate();
|
|
|
|
});
|
|
|
|
}
|
2018-06-05 16:53:13 +00:00
|
|
|
}
|
|
|
|
}
|