2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2017-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-10-22 02:40:55 +00:00
|
|
|
#include <algorithm>
|
2022-03-10 05:51:14 +00:00
|
|
|
#include <array>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
2022-10-01 14:12:49 +00:00
|
|
|
#include <chrono>
|
2022-01-31 23:05:35 +00:00
|
|
|
#include <string_view>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-04-01 23:00:51 +00:00
|
|
|
#include <fmt/format.h>
|
2022-03-14 04:43:35 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "transmission.h"
|
2022-07-11 23:29:48 +00:00
|
|
|
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "clients.h"
|
2022-07-15 00:54:10 +00:00
|
|
|
#include "crypto-utils.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "handshake.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "peer-io.h"
|
2022-10-01 14:12:49 +00:00
|
|
|
#include "timer.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2022-10-19 16:42:08 +00:00
|
|
|
#include "tr-buffer.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/* enable LibTransmission extension protocol */
|
2022-01-24 00:53:35 +00:00
|
|
|
#define ENABLE_LTEP
|
2008-12-02 17:10:54 +00:00
|
|
|
/* fast extensions */
|
2022-01-24 00:53:35 +00:00
|
|
|
#define ENABLE_FAST
|
2009-05-19 18:38:26 +00:00
|
|
|
/* DHT */
|
2022-01-24 00:53:35 +00:00
|
|
|
#define ENABLE_DHT
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static auto constexpr HandshakeName = std::array<std::byte, 20>{
|
|
|
|
std::byte{ 19 }, std::byte{ 'B' }, std::byte{ 'i' }, std::byte{ 't' }, std::byte{ 'T' },
|
|
|
|
std::byte{ 'o' }, std::byte{ 'r' }, std::byte{ 'r' }, std::byte{ 'e' }, std::byte{ 'n' },
|
|
|
|
std::byte{ 't' }, std::byte{ ' ' }, std::byte{ 'p' }, std::byte{ 'r' }, std::byte{ 'o' },
|
|
|
|
std::byte{ 't' }, std::byte{ 'o' }, std::byte{ 'c' }, std::byte{ 'o' }, std::byte{ 'l' }
|
|
|
|
};
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
// bittorrent handshake constants
|
2022-09-07 16:04:28 +00:00
|
|
|
static auto constexpr HandshakeFlagsLen = int{ 8 };
|
|
|
|
static auto constexpr HandshakeSize = int{ 68 };
|
|
|
|
static auto constexpr IncomingHandshakeLen = int{ 48 };
|
2021-10-21 00:39:05 +00:00
|
|
|
|
|
|
|
// encryption constants
|
2022-09-07 16:04:28 +00:00
|
|
|
static auto constexpr PadaMaxlen = int{ 512 };
|
|
|
|
static auto constexpr PadbMaxlen = int{ 512 };
|
|
|
|
static auto constexpr PadcMaxlen = int{ 512 };
|
|
|
|
static auto constexpr CryptoProvidePlaintext = int{ 1 };
|
|
|
|
static auto constexpr CryptoProvideCrypto = int{ 2 };
|
2021-10-21 00:39:05 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// "VC is a verification constant that is used to verify whether the
|
|
|
|
// other side knows S and SKEY and thus defeats replay attacks of the
|
2022-10-13 14:35:10 +00:00
|
|
|
// SKEY hash. As of this version VC is a String of 8 bytes set to 0x00."
|
|
|
|
// https://wiki.vuze.com/w/Message_Stream_Encryption
|
2022-07-15 00:54:10 +00:00
|
|
|
using vc_t = std::array<std::byte, 8>;
|
|
|
|
static auto constexpr VC = vc_t{};
|
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
// how long to wait before giving up on a handshake
|
2022-08-08 22:53:20 +00:00
|
|
|
static auto constexpr HandshakeTimeoutSec = 30s;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_LTEP
|
2017-04-19 12:04:45 +00:00
|
|
|
#define HANDSHAKE_HAS_LTEP(bits) (((bits)[5] & 0x10) != 0)
|
|
|
|
#define HANDSHAKE_SET_LTEP(bits) ((bits)[5] |= 0x10)
|
2007-09-20 16:32:01 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
#define HANDSHAKE_HAS_LTEP(bits) (false)
|
|
|
|
#define HANDSHAKE_SET_LTEP(bits) ((void)0)
|
2007-09-20 16:32:01 +00:00
|
|
|
#endif
|
|
|
|
|
2009-08-10 20:04:08 +00:00
|
|
|
#ifdef ENABLE_FAST
|
2017-04-19 12:04:45 +00:00
|
|
|
#define HANDSHAKE_HAS_FASTEXT(bits) (((bits)[7] & 0x04) != 0)
|
|
|
|
#define HANDSHAKE_SET_FASTEXT(bits) ((bits)[7] |= 0x04)
|
2009-08-10 20:04:08 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
#define HANDSHAKE_HAS_FASTEXT(bits) (false)
|
|
|
|
#define HANDSHAKE_SET_FASTEXT(bits) ((void)0)
|
2009-08-10 20:04:08 +00:00
|
|
|
#endif
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
#ifdef ENABLE_DHT
|
2017-04-19 12:04:45 +00:00
|
|
|
#define HANDSHAKE_HAS_DHT(bits) (((bits)[7] & 0x01) != 0)
|
|
|
|
#define HANDSHAKE_SET_DHT(bits) ((bits)[7] |= 0x01)
|
2009-05-19 18:38:26 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
#define HANDSHAKE_HAS_DHT(bits) (false)
|
|
|
|
#define HANDSHAKE_SET_DHT(bits) ((void)0)
|
2009-08-10 20:04:08 +00:00
|
|
|
#endif
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
using DH = tr_message_stream_encryption::DH;
|
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum handshake_state_t
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* incoming */
|
|
|
|
AWAITING_HANDSHAKE,
|
|
|
|
AWAITING_PEER_ID,
|
|
|
|
AWAITING_YA,
|
|
|
|
AWAITING_PAD_A,
|
|
|
|
AWAITING_CRYPTO_PROVIDE,
|
|
|
|
AWAITING_PAD_C,
|
|
|
|
AWAITING_IA,
|
|
|
|
AWAITING_PAYLOAD_STREAM,
|
|
|
|
/* outgoing */
|
|
|
|
AWAITING_YB,
|
|
|
|
AWAITING_VC,
|
|
|
|
AWAITING_CRYPTO_SELECT,
|
|
|
|
AWAITING_PAD_D,
|
|
|
|
/* */
|
|
|
|
N_STATES
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2015-03-15 11:43:32 +00:00
|
|
|
|
|
|
|
struct tr_handshake
|
|
|
|
{
|
2022-08-30 17:38:30 +00:00
|
|
|
tr_handshake(
|
2022-08-31 00:30:47 +00:00
|
|
|
std::unique_ptr<tr_handshake_mediator> mediator_in,
|
2022-08-30 17:38:30 +00:00
|
|
|
std::shared_ptr<tr_peerIo> io_in,
|
|
|
|
tr_encryption_mode encryption_mode_in)
|
2022-07-11 23:29:48 +00:00
|
|
|
: mediator{ std::move(mediator_in) }
|
2022-08-30 17:38:30 +00:00
|
|
|
, io{ std::move(io_in) }
|
2022-07-15 00:54:10 +00:00
|
|
|
, dh{ mediator->privateKey() }
|
2022-08-13 17:11:07 +00:00
|
|
|
, encryption_mode{ encryption_mode_in }
|
2022-07-11 23:29:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-03 17:03:28 +00:00
|
|
|
tr_handshake(tr_handshake&&) = delete;
|
|
|
|
tr_handshake(tr_handshake const&) = delete;
|
|
|
|
tr_handshake& operator=(tr_handshake&&) = delete;
|
|
|
|
tr_handshake& operator=(tr_handshake const&) = delete;
|
2022-08-30 17:38:30 +00:00
|
|
|
~tr_handshake() = default;
|
2022-08-03 17:03:28 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
[[nodiscard]] auto isIncoming() const noexcept
|
2022-05-26 17:17:03 +00:00
|
|
|
{
|
|
|
|
return io->isIncoming();
|
|
|
|
}
|
|
|
|
|
2022-08-31 22:33:51 +00:00
|
|
|
[[nodiscard]] constexpr uint32_t cryptoProvide() const
|
|
|
|
{
|
|
|
|
uint32_t provide = 0;
|
|
|
|
|
|
|
|
switch (encryption_mode)
|
|
|
|
{
|
|
|
|
case TR_ENCRYPTION_REQUIRED:
|
|
|
|
case TR_ENCRYPTION_PREFERRED:
|
2022-09-07 16:04:28 +00:00
|
|
|
provide |= CryptoProvideCrypto;
|
2022-08-31 22:33:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_CLEAR_PREFERRED:
|
2022-09-07 16:04:28 +00:00
|
|
|
provide |= CryptoProvideCrypto | CryptoProvidePlaintext;
|
2022-08-31 22:33:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return provide;
|
|
|
|
}
|
|
|
|
|
2022-08-31 00:30:47 +00:00
|
|
|
std::unique_ptr<tr_handshake_mediator> const mediator;
|
2022-07-11 23:29:48 +00:00
|
|
|
|
2022-07-16 20:25:44 +00:00
|
|
|
bool haveReadAnythingFromPeer = false;
|
|
|
|
bool haveSentBitTorrentHandshake = false;
|
2022-08-30 17:38:30 +00:00
|
|
|
std::shared_ptr<tr_peerIo> const io;
|
2022-07-16 20:25:44 +00:00
|
|
|
DH dh = {};
|
2022-08-13 17:11:07 +00:00
|
|
|
handshake_state_t state = AWAITING_HANDSHAKE;
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_encryption_mode encryption_mode;
|
2022-07-16 20:25:44 +00:00
|
|
|
uint16_t pad_c_len = {};
|
|
|
|
uint16_t pad_d_len = {};
|
|
|
|
uint16_t ia_len = {};
|
|
|
|
uint32_t crypto_select = {};
|
|
|
|
uint32_t crypto_provide = {};
|
2022-08-08 22:53:20 +00:00
|
|
|
std::unique_ptr<libtransmission::Timer> timeout_timer;
|
2021-10-22 02:40:55 +00:00
|
|
|
|
|
|
|
std::optional<tr_peer_id_t> peer_id;
|
|
|
|
|
2022-07-16 20:25:44 +00:00
|
|
|
tr_handshake_done_func done_func = nullptr;
|
|
|
|
void* done_func_user_data = nullptr;
|
2007-09-20 16:32:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
#define tr_logAddTraceHand(handshake, msg) tr_logAddTrace(msg, (handshake)->io->addrStr())
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
static constexpr std::string_view getStateName(handshake_state_t const state)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto state_strings = std::array<std::string_view, N_STATES>{
|
2022-08-25 01:19:21 +00:00
|
|
|
"awaiting handshake"sv, /* AWAITING_HANDSHAKE */
|
|
|
|
"awaiting peer id"sv, /* AWAITING_PEER_ID */
|
|
|
|
"awaiting ya"sv, /* AWAITING_YA */
|
|
|
|
"awaiting pad a"sv, /* AWAITING_PAD_A */
|
|
|
|
"awaiting crypto_provide"sv, /* AWAITING_CRYPTO_PROVIDE */
|
|
|
|
"awaiting pad c"sv, /* AWAITING_PAD_C */
|
|
|
|
"awaiting ia"sv, /* AWAITING_IA */
|
|
|
|
"awaiting payload stream"sv, /* AWAITING_PAYLOAD_STREAM */
|
|
|
|
"awaiting yb"sv, /* AWAITING_YB */
|
|
|
|
"awaiting vc"sv, /* AWAITING_VC */
|
|
|
|
"awaiting crypto select"sv, /* AWAITING_CRYPTO_SELECT */
|
|
|
|
"awaiting pad d"sv /* AWAITING_PAD_D */
|
2017-04-19 12:04:45 +00:00
|
|
|
};
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
return state < N_STATES ? state_strings[state] : "unknown state"sv;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void setState(tr_handshake* handshake, handshake_state_t state)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("setting to state [{}]", getStateName(state)));
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->state = state;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void setReadState(tr_handshake* handshake, handshake_state_t state)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
setState(handshake, state);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 17:52:58 +00:00
|
|
|
static bool buildHandshakeMessage(tr_handshake const* const handshake, uint8_t* buf)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-07-15 00:54:10 +00:00
|
|
|
auto const info_hash = handshake->io->torrentHash();
|
2022-07-11 23:29:48 +00:00
|
|
|
auto const info = info_hash ? handshake->mediator->torrentInfo(*info_hash) : std::nullopt;
|
|
|
|
if (!info)
|
2013-08-05 04:39:43 +00:00
|
|
|
{
|
2022-07-11 23:29:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-07-11 23:29:48 +00:00
|
|
|
uint8_t* walk = buf;
|
2021-10-22 02:40:55 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
walk = std::copy_n(reinterpret_cast<uint8_t const*>(std::data(HandshakeName)), std::size(HandshakeName), walk);
|
2021-10-22 02:40:55 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
std::fill_n(walk, HandshakeFlagsLen, 0);
|
2022-07-11 23:29:48 +00:00
|
|
|
HANDSHAKE_SET_LTEP(walk);
|
|
|
|
HANDSHAKE_SET_FASTEXT(walk);
|
|
|
|
/* Note that this doesn't depend on whether the torrent is private.
|
|
|
|
* We don't accept DHT peers for a private torrent,
|
|
|
|
* but we participate in the DHT regardless. */
|
2022-10-15 13:22:43 +00:00
|
|
|
if (handshake->mediator->allowsDHT())
|
2022-07-11 23:29:48 +00:00
|
|
|
{
|
|
|
|
HANDSHAKE_SET_DHT(walk);
|
2013-08-05 04:39:43 +00:00
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
walk += HandshakeFlagsLen;
|
2013-08-05 04:39:43 +00:00
|
|
|
|
2022-07-11 23:29:48 +00:00
|
|
|
walk = std::copy_n(reinterpret_cast<char const*>(std::data(*info_hash)), std::size(*info_hash), walk);
|
|
|
|
walk = std::copy(std::begin(info->client_peer_id), std::end(info->client_peer_id), walk);
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
TR_ASSERT(walk - buf == HandshakeSize);
|
2022-07-11 23:29:48 +00:00
|
|
|
return true;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
static ReadState tr_handshakeDone(tr_handshake* handshake, bool is_connected);
|
2007-09-22 00:53:11 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum handshake_parse_err_t
|
2007-09-22 03:37:37 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
HANDSHAKE_OK,
|
|
|
|
HANDSHAKE_ENCRYPTION_WRONG,
|
|
|
|
HANDSHAKE_BAD_TORRENT,
|
|
|
|
HANDSHAKE_PEER_IS_SELF,
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2007-09-22 03:37:37 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static handshake_parse_err_t parseHandshake(tr_handshake* handshake, tr_peerIo* peer_io)
|
2007-09-22 03:37:37 +00:00
|
|
|
{
|
2022-09-09 18:12:47 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("payload: need {}, got {}", HandshakeSize, peer_io->readBufferSize()));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
if (peer_io->readBufferSize() < HandshakeSize)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return HANDSHAKE_ENCRYPTION_WRONG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* confirm the protocol */
|
2022-09-09 18:12:47 +00:00
|
|
|
auto name = decltype(HandshakeName){};
|
|
|
|
peer_io->readBytes(std::data(name), std::size(name));
|
|
|
|
if (name != HandshakeName)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return HANDSHAKE_ENCRYPTION_WRONG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read the reserved bytes */
|
2022-09-07 16:04:28 +00:00
|
|
|
auto reserved = std::array<uint8_t, HandshakeFlagsLen>{};
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBytes(std::data(reserved), std::size(reserved));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* torrent hash */
|
2021-12-21 22:14:15 +00:00
|
|
|
auto hash = tr_sha1_digest_t{};
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBytes(std::data(hash), std::size(hash));
|
|
|
|
if (auto const torrent_hash = peer_io->torrentHash(); !torrent_hash || *torrent_hash != hash)
|
2008-06-05 16:25:22 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "peer returned the wrong hash. wtf?");
|
2017-04-19 12:04:45 +00:00
|
|
|
return HANDSHAKE_BAD_TORRENT;
|
2007-09-22 03:37:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 02:40:55 +00:00
|
|
|
// peer_id
|
|
|
|
auto peer_id = tr_peer_id_t{};
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBytes(std::data(peer_id), std::size(peer_id));
|
2021-10-22 02:40:55 +00:00
|
|
|
handshake->peer_id = peer_id;
|
2007-09-22 03:37:37 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* peer id */
|
2022-03-14 04:43:35 +00:00
|
|
|
auto const peer_id_sv = std::string_view{ std::data(peer_id), std::size(peer_id) };
|
|
|
|
tr_logAddTraceHand(handshake, fmt::format("peer-id is '{}'", peer_id_sv));
|
2008-12-10 05:20:28 +00:00
|
|
|
|
2022-07-11 23:29:48 +00:00
|
|
|
if (auto const info = handshake->mediator->torrentInfo(hash); info && info->client_peer_id == peer_id)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "streuth! we've connected to ourselves.");
|
2017-04-19 12:04:45 +00:00
|
|
|
return HANDSHAKE_PEER_IS_SELF;
|
2007-09-22 03:37:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/**
|
|
|
|
*** Extensions
|
|
|
|
**/
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->enableDHT(HANDSHAKE_HAS_DHT(reserved));
|
|
|
|
peer_io->enableLTEP(HANDSHAKE_HAS_LTEP(reserved));
|
|
|
|
peer_io->enableFEXT(HANDSHAKE_HAS_FASTEXT(reserved));
|
2007-10-17 18:53:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return HANDSHAKE_OK;
|
2007-09-22 03:37:37 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** OUTGOING CONNECTIONS
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
template<size_t PadMax>
|
|
|
|
static void sendPublicKeyAndPad(tr_handshake* handshake)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-07-15 00:54:10 +00:00
|
|
|
auto const public_key = handshake->dh.publicKey();
|
|
|
|
auto outbuf = std::array<std::byte, std::size(public_key) + PadMax>{};
|
|
|
|
auto const data = std::data(outbuf);
|
|
|
|
auto walk = data;
|
2022-07-11 02:57:05 +00:00
|
|
|
walk = std::copy(std::begin(public_key), std::end(public_key), walk);
|
2022-07-15 00:54:10 +00:00
|
|
|
walk += handshake->mediator->pad(walk, PadMax);
|
2022-08-29 20:58:18 +00:00
|
|
|
handshake->io->writeBytes(data, walk - data, false);
|
2022-07-15 00:54:10 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// 1 A->B: our public key (Ya) and some padding (PadA)
|
|
|
|
static void sendYa(tr_handshake* handshake)
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
sendPublicKeyAndPad<PadaMaxlen>(handshake);
|
2017-04-19 12:04:45 +00:00
|
|
|
setReadState(handshake, AWAITING_YB);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
static constexpr uint32_t getCryptoSelect(tr_encryption_mode encryption_mode, uint32_t crypto_provide)
|
2007-11-08 21:20:08 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
auto choices = std::array<uint32_t, 2>{};
|
|
|
|
int n_choices = 0;
|
2007-11-08 21:20:08 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
switch (encryption_mode)
|
2007-11-08 21:20:08 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_ENCRYPTION_REQUIRED:
|
2022-09-07 16:04:28 +00:00
|
|
|
choices[n_choices++] = CryptoProvideCrypto;
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_ENCRYPTION_PREFERRED:
|
2022-09-07 16:04:28 +00:00
|
|
|
choices[n_choices++] = CryptoProvideCrypto;
|
|
|
|
choices[n_choices++] = CryptoProvidePlaintext;
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_CLEAR_PREFERRED:
|
2022-09-07 16:04:28 +00:00
|
|
|
choices[n_choices++] = CryptoProvidePlaintext;
|
|
|
|
choices[n_choices++] = CryptoProvideCrypto;
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2007-11-08 21:20:08 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
for (auto const& choice : choices)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
if ((crypto_provide & choice) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
return choice;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-08 21:20:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2007-09-22 00:53:11 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static ReadState readYb(tr_handshake* handshake, tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
if (peer_io->readBufferSize() < std::size(HandshakeName))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
bool const is_encrypted = !peer_io->readBufferStartsWith(HandshakeName);
|
2022-07-15 00:54:10 +00:00
|
|
|
auto peer_public_key = DH::key_bigend_t{};
|
2022-09-09 18:12:47 +00:00
|
|
|
if (is_encrypted && (peer_io->readBufferSize() < std::size(peer_public_key)))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-09-09 18:12:47 +00:00
|
|
|
return READ_LATER;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_logAddTraceHand(handshake, is_encrypted ? "got an encrypted handshake" : "got a plain handshake");
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (!is_encrypted)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
setState(handshake, AWAITING_HANDSHAKE);
|
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->haveReadAnythingFromPeer = true;
|
2010-04-20 21:54:03 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// get the peer's public key
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBytes(std::data(peer_public_key), std::size(peer_public_key));
|
2022-07-15 00:54:10 +00:00
|
|
|
handshake->dh.setPeerPublicKey(peer_public_key);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* now send these: HASH('req1', S), HASH('req2', SKEY) xor HASH('req3', S),
|
|
|
|
* ENCRYPT(VC, crypto_provide, len(PadC), PadC, len(IA)), ENCRYPT(IA) */
|
2022-10-19 16:42:08 +00:00
|
|
|
auto outbuf = libtransmission::Buffer{};
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* HASH('req1', S) */
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.add(tr_sha1::digest("req1"sv, handshake->dh.secret()));
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
auto const info_hash = peer_io->torrentHash();
|
2022-07-15 00:54:10 +00:00
|
|
|
if (!info_hash)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-15 00:54:10 +00:00
|
|
|
tr_logAddTraceHand(handshake, "error while computing req2/req3 hash after Yb");
|
|
|
|
return tr_handshakeDone(handshake, false);
|
|
|
|
}
|
2022-06-01 16:56:59 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
/* HASH('req2', SKEY) xor HASH('req3', S) */
|
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
auto const req2 = tr_sha1::digest("req2"sv, *info_hash);
|
|
|
|
auto const req3 = tr_sha1::digest("req3"sv, handshake->dh.secret());
|
2022-10-19 16:42:08 +00:00
|
|
|
auto x_or = tr_sha1_digest_t{};
|
|
|
|
for (size_t i = 0, n = std::size(x_or); i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
x_or[i] = req2[i] ^ req3[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.add(x_or);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* ENCRYPT(VC, crypto_provide, len(PadC), PadC
|
2017-04-19 12:04:45 +00:00
|
|
|
* PadC is reserved for future extensions to the handshake...
|
|
|
|
* standard practice at this time is for it to be zero-length */
|
2022-10-19 16:42:08 +00:00
|
|
|
peer_io->write(outbuf, false);
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->encryptInit(peer_io->isIncoming(), handshake->dh, *info_hash);
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.add(VC);
|
|
|
|
outbuf.addUint32(handshake->cryptoProvide());
|
|
|
|
outbuf.addUint16(0);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* ENCRYPT len(IA)), ENCRYPT(IA) */
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto msg = std::array<uint8_t, HandshakeSize>{}; buildHandshakeMessage(handshake, std::data(msg)))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.addUint16(std::size(msg));
|
|
|
|
outbuf.add(msg);
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->haveSentBitTorrentHandshake = true;
|
|
|
|
}
|
2022-07-15 00:54:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return tr_handshakeDone(handshake, false);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* send it */
|
|
|
|
setReadState(handshake, AWAITING_VC);
|
2022-10-19 16:42:08 +00:00
|
|
|
peer_io->write(outbuf, false);
|
2022-07-16 20:25:44 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// MSE spec: "Since the length of [PadB is] unknown,
|
|
|
|
// A will be able to resynchronize on ENCRYPT(VC)"
|
2022-09-09 18:12:47 +00:00
|
|
|
static ReadState readVC(tr_handshake* handshake, tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-07-18 18:00:26 +00:00
|
|
|
// find the end of PadB by looking for `ENCRYPT(VC)`
|
|
|
|
auto needle = VC;
|
|
|
|
auto filter = tr_message_stream_encryption::Filter{};
|
2022-09-09 18:12:47 +00:00
|
|
|
filter.encryptInit(true, handshake->dh, *peer_io->torrentHash());
|
2022-07-18 18:00:26 +00:00
|
|
|
filter.encrypt(std::size(needle), std::data(needle));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
for (size_t i = 0; i < PadbMaxlen; ++i)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
if (peer_io->readBufferSize() < std::size(needle))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "not enough bytes... returning read_more");
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_LATER;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
if (peer_io->readBufferStartsWith(needle))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-18 18:00:26 +00:00
|
|
|
tr_logAddTraceHand(handshake, "got it!");
|
|
|
|
// We already know it's a match; now we just need to
|
|
|
|
// consume it from the read buffer.
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->decryptInit(peer_io->isIncoming(), handshake->dh, *peer_io->torrentHash());
|
|
|
|
peer_io->readBytes(std::data(needle), std::size(needle));
|
2022-07-18 18:00:26 +00:00
|
|
|
setState(handshake, AWAITING_CRYPTO_SELECT);
|
|
|
|
return READ_NOW;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBufferDrain(1);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-07-18 18:00:26 +00:00
|
|
|
tr_logAddTraceHand(handshake, "couldn't find ENCRYPT(VC)");
|
|
|
|
return tr_handshakeDone(handshake, false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static ReadState readCryptoSelect(tr_handshake* handshake, tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
static size_t const needlen = sizeof(uint32_t) + sizeof(uint16_t);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
if (peer_io->readBufferSize() < needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
uint32_t crypto_select = 0;
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readUint32(&crypto_select);
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->crypto_select = crypto_select;
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("crypto select is {}", crypto_select));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-08-31 22:33:51 +00:00
|
|
|
if ((crypto_select & handshake->cryptoProvide()) == 0)
|
2007-09-22 00:53:11 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "peer selected an encryption option we didn't offer");
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_handshakeDone(handshake, false);
|
2007-09-22 00:53:11 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
uint16_t pad_d_len = 0;
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readUint16(&pad_d_len);
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("pad_d_len is {}", pad_d_len));
|
2008-01-14 16:17:02 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (pad_d_len > 512)
|
2008-01-14 16:17:02 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "encryption handshake: pad_d_len is too long");
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_handshakeDone(handshake, false);
|
2008-01-14 16:17:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->pad_d_len = pad_d_len;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
setState(handshake, AWAITING_PAD_D);
|
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static ReadState readPadD(tr_handshake* handshake, tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const needlen = handshake->pad_d_len;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("pad d: need {}, got {}", needlen, peer_io->readBufferSize()));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
if (peer_io->readBufferSize() < needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBufferDrain(needlen);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
setState(handshake, AWAITING_HANDSHAKE);
|
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** INCOMING CONNECTIONS
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static ReadState readHandshake(tr_handshake* handshake, tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-09-09 18:12:47 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("payload: need {}, got {}", IncomingHandshakeLen, peer_io->readBufferSize()));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
if (peer_io->readBufferSize() < IncomingHandshakeLen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->haveReadAnythingFromPeer = true;
|
2010-04-20 21:54:03 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
if (peer_io->readBufferStartsWith(HandshakeName)) // unencrypted
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
if (handshake->encryption_mode == TR_ENCRYPTION_REQUIRED)
|
2007-09-26 03:52:30 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "peer is unencrypted, and we're disallowing that");
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_handshakeDone(handshake, false);
|
2007-09-26 03:52:30 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2022-09-09 18:12:47 +00:00
|
|
|
else // either encrypted or corrupt
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-05-26 17:17:03 +00:00
|
|
|
if (handshake->isIncoming())
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "I think peer is sending us an encrypted handshake...");
|
2017-04-19 12:04:45 +00:00
|
|
|
setState(handshake, AWAITING_YA);
|
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
auto name = decltype(HandshakeName){};
|
|
|
|
peer_io->readBytes(std::data(name), std::size(name));
|
|
|
|
if (name != HandshakeName)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return tr_handshakeDone(handshake, false);
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* reserved bytes */
|
2022-09-07 16:04:28 +00:00
|
|
|
auto reserved = std::array<uint8_t, HandshakeFlagsLen>{};
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBytes(std::data(reserved), std::size(reserved));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/**
|
|
|
|
*** Extensions
|
|
|
|
**/
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->enableDHT(HANDSHAKE_HAS_DHT(reserved));
|
|
|
|
peer_io->enableLTEP(HANDSHAKE_HAS_LTEP(reserved));
|
|
|
|
peer_io->enableFEXT(HANDSHAKE_HAS_FASTEXT(reserved));
|
2008-01-01 00:20:07 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* torrent hash */
|
2021-12-21 22:14:15 +00:00
|
|
|
auto hash = tr_sha1_digest_t{};
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBytes(std::data(hash), std::size(hash));
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2022-05-26 17:17:03 +00:00
|
|
|
if (handshake->isIncoming())
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-07-11 23:29:48 +00:00
|
|
|
if (!handshake->mediator->torrentInfo(hash))
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "peer is trying to connect to us for a torrent we don't have.");
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_handshakeDone(handshake, false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->setTorrentHash(hash);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else /* outgoing */
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-09-09 18:12:47 +00:00
|
|
|
auto const torrent_hash = peer_io->torrentHash();
|
2022-07-11 02:57:05 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
if (!torrent_hash || *torrent_hash != hash)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "peer returned the wrong hash. wtf?");
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_handshakeDone(handshake, false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/**
|
|
|
|
*** If it's an incoming message, we need to send a response handshake
|
|
|
|
**/
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!handshake->haveSentBitTorrentHandshake)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto msg = std::array<uint8_t, HandshakeSize>{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
if (!buildHandshakeMessage(handshake, std::data(msg)))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return tr_handshakeDone(handshake, false);
|
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->writeBytes(std::data(msg), std::size(msg), false);
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->haveSentBitTorrentHandshake = true;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
setReadState(handshake, AWAITING_PEER_ID);
|
|
|
|
return READ_NOW;
|
2008-01-01 00:20:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static ReadState readPeerId(tr_handshake* handshake, tr_peerIo* peer_io)
|
2008-01-01 00:20:07 +00:00
|
|
|
{
|
2021-10-22 02:40:55 +00:00
|
|
|
// read the peer_id
|
|
|
|
auto peer_id = tr_peer_id_t{};
|
2022-09-09 18:12:47 +00:00
|
|
|
if (peer_io->readBufferSize() < std::size(peer_id))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBytes(std::data(peer_id), std::size(peer_id));
|
2021-10-22 02:40:55 +00:00
|
|
|
handshake->peer_id = peer_id;
|
2008-01-01 00:20:07 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
auto client = std::array<char, 128>{};
|
|
|
|
tr_clientForId(std::data(client), std::size(client), peer_id);
|
|
|
|
tr_logAddTraceHand(
|
|
|
|
handshake,
|
|
|
|
fmt::format("peer-id is '{}' ... isIncoming is {}", std::data(client), handshake->isIncoming()));
|
2012-12-27 22:03:58 +00:00
|
|
|
|
2021-10-22 02:40:55 +00:00
|
|
|
// if we've somehow connected to ourselves, don't keep the connection
|
2022-09-09 18:12:47 +00:00
|
|
|
auto const hash = peer_io->torrentHash();
|
2022-07-11 23:29:48 +00:00
|
|
|
auto const info = hash ? handshake->mediator->torrentInfo(*hash) : std::nullopt;
|
|
|
|
auto const connected_to_self = info && info->client_peer_id == peer_id;
|
2013-02-02 05:31:43 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_handshakeDone(handshake, !connected_to_self);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static ReadState readYa(tr_handshake* handshake, tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-07-15 00:54:10 +00:00
|
|
|
auto peer_public_key = DH::key_bigend_t{};
|
|
|
|
tr_logAddTraceHand(
|
|
|
|
handshake,
|
2022-09-09 18:12:47 +00:00
|
|
|
fmt::format("in readYa... need {}, have {}", std::size(peer_public_key), peer_io->readBufferSize()));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
if (peer_io->readBufferSize() < std::size(peer_public_key))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read the incoming peer's public key */
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBytes(std::data(peer_public_key), std::size(peer_public_key));
|
2022-07-15 00:54:10 +00:00
|
|
|
handshake->dh.setPeerPublicKey(peer_public_key);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// send our public key to the peer
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "sending B->A: Diffie Hellman Yb, PadB");
|
2022-09-07 16:04:28 +00:00
|
|
|
sendPublicKeyAndPad<PadbMaxlen>(handshake);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
setReadState(handshake, AWAITING_PAD_A);
|
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static ReadState readPadA(tr_handshake* handshake, tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-07-18 18:00:26 +00:00
|
|
|
// find the end of PadA by looking for HASH('req1', S)
|
2022-07-31 20:58:14 +00:00
|
|
|
auto const needle = tr_sha1::digest("req1"sv, handshake->dh.secret());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
for (size_t i = 0; i < PadaMaxlen; ++i)
|
2021-10-14 19:26:38 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
if (peer_io->readBufferSize() < std::size(needle))
|
2022-07-18 18:00:26 +00:00
|
|
|
{
|
|
|
|
tr_logAddTraceHand(handshake, "not enough bytes... returning read_more");
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
if (peer_io->readBufferStartsWith(needle))
|
2022-07-18 18:00:26 +00:00
|
|
|
{
|
|
|
|
tr_logAddTraceHand(handshake, "found it... looking setting to awaiting_crypto_provide");
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBufferDrain(std::size(needle));
|
2022-07-18 18:00:26 +00:00
|
|
|
setState(handshake, AWAITING_CRYPTO_PROVIDE);
|
|
|
|
return READ_NOW;
|
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBufferDrain(1U);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2022-07-18 18:00:26 +00:00
|
|
|
tr_logAddTraceHand(handshake, "couldn't find HASH('req', S)");
|
|
|
|
return tr_handshakeDone(handshake, false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static ReadState readCryptoProvide(tr_handshake* handshake, tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-21 07:40:57 +00:00
|
|
|
/* HASH('req2', SKEY) xor HASH('req3', S), ENCRYPT(VC, crypto_provide, len(PadC)) */
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
uint16_t padc_len = 0;
|
|
|
|
uint32_t crypto_provide = 0;
|
2022-09-05 13:55:17 +00:00
|
|
|
auto obfuscated_hash = tr_sha1_digest_t{};
|
|
|
|
size_t const needlen = sizeof(obfuscated_hash) + /* HASH('req2', SKEY) xor HASH('req3', S) */
|
2022-07-15 00:54:10 +00:00
|
|
|
std::size(VC) + sizeof(crypto_provide) + sizeof(padc_len);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
if (peer_io->readBufferSize() < needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* This next piece is HASH('req2', SKEY) xor HASH('req3', S) ...
|
2022-04-21 14:28:38 +00:00
|
|
|
* we can get the first half of that (the obfuscatedTorrentHash)
|
2017-04-19 12:04:45 +00:00
|
|
|
* by building the latter and xor'ing it with what the peer sent us */
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "reading obfuscated torrent hash...");
|
2021-12-21 22:14:15 +00:00
|
|
|
auto req2 = tr_sha1_digest_t{};
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBytes(std::data(req2), std::size(req2));
|
2021-12-21 22:14:15 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
auto const req3 = tr_sha1::digest("req3"sv, handshake->dh.secret());
|
2021-12-21 22:14:15 +00:00
|
|
|
for (size_t i = 0; i < std::size(obfuscated_hash); ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
obfuscated_hash[i] = req2[i] ^ req3[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 23:29:48 +00:00
|
|
|
if (auto const info = handshake->mediator->torrentInfoFromObfuscated(obfuscated_hash); info)
|
2008-04-17 03:48:56 +00:00
|
|
|
{
|
2022-07-11 23:29:48 +00:00
|
|
|
bool const client_is_seed = info->is_done;
|
2022-09-09 18:12:47 +00:00
|
|
|
bool const peer_is_seed = handshake->mediator->isPeerKnownSeed(info->id, peer_io->address());
|
2022-07-11 23:29:48 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("got INCOMING connection's encrypted handshake for torrent [{}]", info->id));
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->setTorrentHash(info->info_hash);
|
2009-01-07 22:06:40 +00:00
|
|
|
|
2022-07-11 23:29:48 +00:00
|
|
|
if (client_is_seed && peer_is_seed)
|
2008-04-17 03:48:56 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "another seed tried to reconnect to us!");
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_handshakeDone(handshake, false);
|
2008-04-17 03:48:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-04-17 03:48:56 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "can't find that torrent...");
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_handshakeDone(handshake, false);
|
2007-09-26 03:52:30 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* next part: ENCRYPT(VC, crypto_provide, len(PadC), */
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->decryptInit(peer_io->isIncoming(), handshake->dh, *peer_io->torrentHash());
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
auto vc_in = vc_t{};
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBytes(std::data(vc_in), std::size(vc_in));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readUint32(&crypto_provide);
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->crypto_provide = crypto_provide;
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("crypto_provide is {}", crypto_provide));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readUint16(&padc_len);
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("padc is {}", padc_len));
|
2022-09-07 16:04:28 +00:00
|
|
|
if (padc_len > PadcMaxlen)
|
2022-07-28 03:17:05 +00:00
|
|
|
{
|
|
|
|
tr_logAddTraceHand(handshake, "peer's PadC is too big");
|
|
|
|
return tr_handshakeDone(handshake, false);
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->pad_c_len = padc_len;
|
|
|
|
setState(handshake, AWAITING_PAD_C);
|
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static ReadState readPadC(tr_handshake* handshake, tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-09-09 18:12:47 +00:00
|
|
|
if (auto const needlen = handshake->pad_c_len + sizeof(uint16_t); peer_io->readBufferSize() < needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-07-28 03:17:05 +00:00
|
|
|
// read the throwaway padc
|
2022-09-07 16:04:28 +00:00
|
|
|
auto pad_c = std::array<char, PadcMaxlen>{};
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readBytes(std::data(pad_c), handshake->pad_c_len);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* read ia_len */
|
2022-08-29 18:33:08 +00:00
|
|
|
uint16_t ia_len = 0;
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->readUint16(&ia_len);
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("ia_len is {}", ia_len));
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->ia_len = ia_len;
|
|
|
|
setState(handshake, AWAITING_IA);
|
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static ReadState readIA(tr_handshake* handshake, tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const needlen = handshake->ia_len;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("reading IA... have {}, need {}", peer_io->readBufferSize(), needlen));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
if (peer_io->readBufferSize() < needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
*** B->A: ENCRYPT(VC, crypto_select, len(padD), padD), ENCRYPT2(Payload Stream)
|
2017-04-19 12:04:45 +00:00
|
|
|
**/
|
2007-09-26 03:52:30 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
peer_io->encryptInit(peer_io->isIncoming(), handshake->dh, *peer_io->torrentHash());
|
2022-10-19 16:42:08 +00:00
|
|
|
auto outbuf = libtransmission::Buffer{};
|
2007-09-26 03:52:30 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// send VC
|
|
|
|
tr_logAddTraceHand(handshake, "sending vc");
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.add(VC);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* send crypto_select */
|
2022-08-25 01:19:21 +00:00
|
|
|
uint32_t const crypto_select = getCryptoSelect(handshake->encryption_mode, handshake->crypto_provide);
|
2007-09-22 03:37:37 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (crypto_select != 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("selecting crypto mode '{}'", crypto_select));
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.addUint32(crypto_select);
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "peer didn't offer an encryption mode we like.");
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_handshakeDone(handshake, false);
|
2007-09-22 03:37:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "sending pad d");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* ENCRYPT(VC, crypto_provide, len(PadD), PadD
|
2017-04-19 12:04:45 +00:00
|
|
|
* PadD is reserved for future extensions to the handshake...
|
|
|
|
* standard practice at this time is for it to be zero-length */
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.addUint16(0);
|
2007-09-22 03:37:37 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* maybe de-encrypt our connection */
|
2022-09-07 16:04:28 +00:00
|
|
|
if (crypto_select == CryptoProvidePlaintext)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
peer_io->write(outbuf, false);
|
|
|
|
TR_ASSERT(std::empty(outbuf));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-30 21:45:40 +00:00
|
|
|
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "sending handshake");
|
2007-09-22 03:37:37 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* send our handshake */
|
2022-09-07 16:04:28 +00:00
|
|
|
if (auto msg = std::array<uint8_t, HandshakeSize>{}; buildHandshakeMessage(handshake, std::data(msg)))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.add(msg);
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->haveSentBitTorrentHandshake = true;
|
|
|
|
}
|
2022-07-15 00:54:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return tr_handshakeDone(handshake, false);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* send it out */
|
2022-10-19 16:42:08 +00:00
|
|
|
peer_io->write(outbuf, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* now await the handshake */
|
|
|
|
setState(handshake, AWAITING_PAYLOAD_STREAM);
|
|
|
|
return READ_NOW;
|
2008-10-29 20:18:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static ReadState readPayloadStream(tr_handshake* handshake, tr_peerIo* peer_io)
|
2008-10-29 20:18:56 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
size_t const needlen = HandshakeSize;
|
2008-10-29 20:18:56 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceHand(
|
|
|
|
handshake,
|
2022-09-09 18:12:47 +00:00
|
|
|
fmt::format("reading payload stream... have {}, need {}", peer_io->readBufferSize(), needlen));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
if (peer_io->readBufferSize() < needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the handshake ... */
|
2022-09-09 18:12:47 +00:00
|
|
|
handshake_parse_err_t const i = parseHandshake(handshake, peer_io);
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("parseHandshake returned {}", i));
|
2008-10-29 20:18:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (i != HANDSHAKE_OK)
|
|
|
|
{
|
|
|
|
return tr_handshakeDone(handshake, false);
|
|
|
|
}
|
2008-10-29 20:18:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* we've completed the BT handshake... pass the work on to peer-msgs */
|
|
|
|
return tr_handshakeDone(handshake, true);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
****
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
static ReadState canRead(tr_peerIo* peer_io, void* vhandshake, size_t* piece)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-09-09 18:12:47 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(peer_io));
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handshake = static_cast<tr_handshake*>(vhandshake);
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
bool ready_for_more = true;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* no piece data in handshake */
|
|
|
|
*piece = 0;
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("handling canRead; state is [{}]", getStateName(handshake->state)));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
ReadState ret = READ_NOW;
|
2022-09-07 16:04:28 +00:00
|
|
|
while (ready_for_more)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (handshake->state)
|
2008-10-29 19:30:17 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case AWAITING_HANDSHAKE:
|
2022-09-09 18:12:47 +00:00
|
|
|
ret = readHandshake(handshake, peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case AWAITING_PEER_ID:
|
2022-09-09 18:12:47 +00:00
|
|
|
ret = readPeerId(handshake, peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case AWAITING_YA:
|
2022-09-09 18:12:47 +00:00
|
|
|
ret = readYa(handshake, peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case AWAITING_PAD_A:
|
2022-09-09 18:12:47 +00:00
|
|
|
ret = readPadA(handshake, peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case AWAITING_CRYPTO_PROVIDE:
|
2022-09-09 18:12:47 +00:00
|
|
|
ret = readCryptoProvide(handshake, peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case AWAITING_PAD_C:
|
2022-09-09 18:12:47 +00:00
|
|
|
ret = readPadC(handshake, peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case AWAITING_IA:
|
2022-09-09 18:12:47 +00:00
|
|
|
ret = readIA(handshake, peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case AWAITING_PAYLOAD_STREAM:
|
2022-09-09 18:12:47 +00:00
|
|
|
ret = readPayloadStream(handshake, peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-10-29 20:18:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case AWAITING_YB:
|
2022-09-09 18:12:47 +00:00
|
|
|
ret = readYb(handshake, peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case AWAITING_VC:
|
2022-09-09 18:12:47 +00:00
|
|
|
ret = readVC(handshake, peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case AWAITING_CRYPTO_SELECT:
|
2022-09-09 18:12:47 +00:00
|
|
|
ret = readCryptoSelect(handshake, peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case AWAITING_PAD_D:
|
2022-09-09 18:12:47 +00:00
|
|
|
ret = readPadD(handshake, peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2021-09-15 02:23:53 +00:00
|
|
|
#ifdef TR_ENABLE_ASSERTS
|
2022-04-01 23:00:51 +00:00
|
|
|
TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unhandled handshake state {:d}"), handshake->state));
|
2021-09-15 02:23:53 +00:00
|
|
|
#else
|
|
|
|
ret = READ_ERR;
|
|
|
|
break;
|
|
|
|
#endif
|
2008-10-29 19:30:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret != READ_NOW)
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
ready_for_more = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else if (handshake->state == AWAITING_PAD_C)
|
|
|
|
{
|
2022-09-09 18:12:47 +00:00
|
|
|
ready_for_more = peer_io->readBufferSize() >= handshake->pad_c_len;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else if (handshake->state == AWAITING_PAD_D)
|
|
|
|
{
|
2022-09-09 18:12:47 +00:00
|
|
|
ready_for_more = peer_io->readBufferSize() >= handshake->pad_d_len;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else if (handshake->state == AWAITING_IA)
|
|
|
|
{
|
2022-09-09 18:12:47 +00:00
|
|
|
ready_for_more = peer_io->readBufferSize() >= handshake->ia_len;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
static bool fireDoneFunc(tr_handshake* handshake, bool is_connected)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-10-22 02:40:55 +00:00
|
|
|
auto result = tr_handshake_result{};
|
|
|
|
result.handshake = handshake;
|
|
|
|
result.io = handshake->io;
|
|
|
|
result.readAnythingFromPeer = handshake->haveReadAnythingFromPeer;
|
2022-09-07 16:04:28 +00:00
|
|
|
result.isConnected = is_connected;
|
2021-10-22 02:40:55 +00:00
|
|
|
result.userData = handshake->done_func_user_data;
|
|
|
|
result.peer_id = handshake->peer_id;
|
|
|
|
bool const success = (*handshake->done_func)(result);
|
2017-04-19 12:04:45 +00:00
|
|
|
return success;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
static ReadState tr_handshakeDone(tr_handshake* handshake, bool is_connected)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_logAddTraceHand(handshake, is_connected ? "handshakeDone: connected" : "handshakeDone: aborting");
|
2022-08-29 18:33:08 +00:00
|
|
|
handshake->io->setCallbacks(nullptr, nullptr, nullptr, nullptr);
|
2007-09-29 13:47:15 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
bool const success = fireDoneFunc(handshake, is_connected);
|
2022-07-11 23:29:48 +00:00
|
|
|
delete handshake;
|
2017-04-19 12:04:45 +00:00
|
|
|
return success ? READ_LATER : READ_ERR;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_handshakeAbort(tr_handshake* handshake)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (handshake != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_handshakeDone(handshake, false);
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void gotError(tr_peerIo* io, short what, void* vhandshake)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-07-27 14:03:13 +00:00
|
|
|
int const errcode = errno;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handshake = static_cast<tr_handshake*>(vhandshake);
|
2012-12-27 22:03:58 +00:00
|
|
|
|
2022-05-26 17:17:03 +00:00
|
|
|
if (io->socket.type == TR_PEER_SOCKET_TYPE_UTP && !io->isIncoming() && handshake->state == AWAITING_YB)
|
2012-12-27 22:03:58 +00:00
|
|
|
{
|
2022-10-11 15:39:41 +00:00
|
|
|
// the peer probably doesn't speak µTP.
|
2012-12-27 22:03:58 +00:00
|
|
|
|
2022-07-11 02:57:05 +00:00
|
|
|
auto const hash = io->torrentHash();
|
2022-07-11 23:29:48 +00:00
|
|
|
auto const info = hash ? handshake->mediator->torrentInfo(*hash) : std::nullopt;
|
2012-12-27 22:03:58 +00:00
|
|
|
|
2022-10-11 15:39:41 +00:00
|
|
|
/* Don't mark a peer as non-µTP unless it's really a connect failure. */
|
2022-07-11 23:29:48 +00:00
|
|
|
if ((errcode == ETIMEDOUT || errcode == ECONNREFUSED) && info)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-11 23:29:48 +00:00
|
|
|
handshake->mediator->setUTPFailed(*hash, io->address());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-27 22:03:58 +00:00
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
if (handshake->mediator->allowsTCP() && handshake->io->reconnect() == 0)
|
2012-12-27 22:03:58 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto msg = std::array<uint8_t, HandshakeSize>{};
|
2022-08-25 01:19:21 +00:00
|
|
|
buildHandshakeMessage(handshake, std::data(msg));
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->haveSentBitTorrentHandshake = true;
|
|
|
|
setReadState(handshake, AWAITING_HANDSHAKE);
|
2022-08-29 20:58:18 +00:00
|
|
|
handshake->io->writeBytes(std::data(msg), std::size(msg), false);
|
2011-02-18 00:43:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* if the error happened while we were sending a public key, we might
|
|
|
|
* have encountered a peer that doesn't do encryption... reconnect and
|
|
|
|
* try a plaintext handshake */
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((handshake->state == AWAITING_YB || handshake->state == AWAITING_VC) &&
|
2022-08-26 02:27:11 +00:00
|
|
|
handshake->encryption_mode != TR_ENCRYPTION_REQUIRED && handshake->mediator->allowsTCP() &&
|
2022-08-29 18:33:08 +00:00
|
|
|
handshake->io->reconnect() == 0)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto msg = std::array<uint8_t, HandshakeSize>{};
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "handshake failed, trying plaintext...");
|
2022-08-25 01:19:21 +00:00
|
|
|
buildHandshakeMessage(handshake, std::data(msg));
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->haveSentBitTorrentHandshake = true;
|
|
|
|
setReadState(handshake, AWAITING_HANDSHAKE);
|
2022-08-29 20:58:18 +00:00
|
|
|
handshake->io->writeBytes(std::data(msg), std::size(msg), false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(
|
|
|
|
handshake,
|
2022-03-14 04:43:35 +00:00
|
|
|
fmt::format("libevent got an error what=={}, errno={} ({})", what, errcode, tr_strerror(errcode)));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_handshakeDone(handshake, false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2021-10-22 02:40:55 +00:00
|
|
|
tr_handshake* tr_handshakeNew(
|
2022-08-31 00:30:47 +00:00
|
|
|
std::unique_ptr<tr_handshake_mediator> mediator,
|
2022-08-30 17:38:30 +00:00
|
|
|
std::shared_ptr<tr_peerIo> io,
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_encryption_mode encryption_mode,
|
2021-10-22 02:40:55 +00:00
|
|
|
tr_handshake_done_func done_func,
|
|
|
|
void* done_func_user_data)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-30 17:38:30 +00:00
|
|
|
auto* const handshake = new tr_handshake{ std::move(mediator), std::move(io), encryption_mode };
|
2021-10-22 02:40:55 +00:00
|
|
|
handshake->done_func = done_func;
|
|
|
|
handshake->done_func_user_data = done_func_user_data;
|
2022-10-01 14:12:49 +00:00
|
|
|
handshake->timeout_timer = handshake->mediator->timerMaker().create([handshake]() { tr_handshakeAbort(handshake); });
|
2022-08-08 22:53:20 +00:00
|
|
|
handshake->timeout_timer->startSingleShot(HandshakeTimeoutSec);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
handshake->io->setCallbacks(canRead, nullptr, gotError, handshake);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-05-26 17:17:03 +00:00
|
|
|
if (handshake->isIncoming())
|
2012-12-27 22:03:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
setReadState(handshake, AWAITING_HANDSHAKE);
|
2012-12-27 22:03:58 +00:00
|
|
|
}
|
2022-08-03 06:15:37 +00:00
|
|
|
else if (encryption_mode != TR_CLEAR_PREFERRED)
|
2012-12-27 22:03:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
sendYa(handshake);
|
2012-12-27 22:03:58 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto msg = std::array<uint8_t, HandshakeSize>{};
|
2022-08-25 01:19:21 +00:00
|
|
|
buildHandshakeMessage(handshake, std::data(msg));
|
2009-05-30 21:45:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
handshake->haveSentBitTorrentHandshake = true;
|
|
|
|
setReadState(handshake, AWAITING_HANDSHAKE);
|
2022-08-29 20:58:18 +00:00
|
|
|
handshake->io->writeBytes(std::data(msg), std::size(msg), false);
|
2007-11-08 21:20:08 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return handshake;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|