2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © 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>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <cerrno> // ECONNREFUSED, ETIMEDOUT
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2022-01-31 23:05:35 +00:00
|
|
|
#include <string_view>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <tuple>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-03-14 04:43:35 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2022-07-11 23:29:48 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/bitfield.h"
|
|
|
|
#include "libtransmission/clients.h"
|
|
|
|
#include "libtransmission/crypto-utils.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/error.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/handshake.h"
|
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/peer-io.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/peer-mse.h" // tr_message_stream_encryption::DH
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/timer.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/tr-buffer.h"
|
2023-11-21 15:02:03 +00:00
|
|
|
#include "libtransmission/tr-macros.h" // tr_peer_id_t
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
#define tr_logAddTraceHand(handshake, msg) \
|
|
|
|
tr_logAddTrace(msg, fmt::format("handshake {}", (handshake)->peer_io_->display_name()))
|
2022-05-26 17:17:03 +00:00
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
using namespace std::literals;
|
2022-12-13 17:59:21 +00:00
|
|
|
using DH = tr_message_stream_encryption::DH;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Outgoing Connections
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// 1 A->B: our public key (Ya) and some padding (PadA)
|
2022-12-13 17:59:21 +00:00
|
|
|
void tr_handshake::send_ya(tr_peerIo* io)
|
2022-07-15 00:54:10 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, "sending MSE handshake (Ya)");
|
2022-12-13 17:59:21 +00:00
|
|
|
send_public_key_and_pad<PadaMaxlen>(io);
|
|
|
|
set_state(tr_handshake::State::AwaitingYb);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_yb(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_size() < std::size(HandshakeName))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
// Jump to plain handshake
|
|
|
|
if (peer_io->read_buffer_starts_with(HandshakeName))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, "in read_yb... got a plain incoming handshake");
|
|
|
|
set_state(tr_handshake::State::AwaitingHandshake);
|
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
auto peer_public_key = DH::key_bigend_t{};
|
|
|
|
tr_logAddTraceHand(
|
|
|
|
this,
|
|
|
|
fmt::format("in read_yb... need {}, have {}", std::size(peer_public_key), peer_io->read_buffer_size()));
|
|
|
|
if (peer_io->read_buffer_size() < std::size(peer_public_key))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
return READ_LATER;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
have_read_anything_from_peer_ = true;
|
2010-04-20 21:54:03 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// get the peer's public key
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(peer_public_key), std::size(peer_public_key));
|
2022-12-13 17:59:21 +00:00
|
|
|
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) */
|
2023-09-26 17:50:41 +00:00
|
|
|
static auto constexpr BufSize = std::tuple_size_v<tr_sha1_digest_t> * 2 + std::size(VC) + sizeof(crypto_provide_) +
|
|
|
|
sizeof(pad_c_len_) + sizeof(ia_len_) + HandshakeSize;
|
|
|
|
auto outbuf = libtransmission::StackBuffer<BufSize, std::byte>{};
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* HASH('req1', S) */
|
2022-12-13 17:59:21 +00:00
|
|
|
outbuf.add(tr_sha1::digest("req1"sv, dh_.secret()));
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const& info_hash = peer_io->torrent_hash();
|
2022-11-12 15:53:09 +00:00
|
|
|
TR_ASSERT_MSG(info_hash != tr_sha1_digest_t{}, "readYb requires an info_hash");
|
2022-06-01 16:56:59 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
/* HASH('req2', SKEY) xor HASH('req3', S) */
|
|
|
|
{
|
2022-11-12 15:53:09 +00:00
|
|
|
auto const req2 = tr_sha1::digest("req2"sv, info_hash);
|
2022-12-13 17:59:21 +00:00
|
|
|
auto const req3 = tr_sha1::digest("req3"sv, dh_.secret());
|
2023-09-26 17:50:41 +00:00
|
|
|
auto [x_or, n_x_or] = outbuf.reserve_space(std::tuple_size_v<tr_sha1_digest_t>);
|
|
|
|
for (size_t i = 0; i < n_x_or; ++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
|
|
|
}
|
2023-09-26 17:50:41 +00:00
|
|
|
outbuf.commit_space(n_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 */
|
2023-09-26 17:50:41 +00:00
|
|
|
crypto_provide_ = crypto_provide();
|
2022-10-19 16:42:08 +00:00
|
|
|
peer_io->write(outbuf, false);
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->encrypt_init(peer_io->is_incoming(), dh_, info_hash);
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.add(VC);
|
2023-09-26 17:50:41 +00:00
|
|
|
outbuf.add_uint32(crypto_provide_);
|
2023-01-28 02:12:09 +00:00
|
|
|
outbuf.add_uint16(0);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* ENCRYPT len(IA)), ENCRYPT(IA) */
|
2023-09-26 17:50:41 +00:00
|
|
|
outbuf.add_uint16(HandshakeSize);
|
|
|
|
if (build_handshake_message(peer_io, outbuf))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
have_sent_bittorrent_handshake_ = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-07-15 00:54:10 +00:00
|
|
|
else
|
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
return done(false);
|
2022-07-15 00:54:10 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* send it */
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(State::AwaitingVc);
|
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-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_vc(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const info_hash = peer_io->torrent_hash();
|
2023-09-26 17:50:41 +00:00
|
|
|
TR_ASSERT_MSG(info_hash != tr_sha1_digest_t{}, "read_vc requires an info_hash");
|
2022-11-12 15:53:09 +00:00
|
|
|
|
2023-05-14 20:01:04 +00:00
|
|
|
// We need to find the end of PadB by looking for `ENCRYPT(VC)`,
|
|
|
|
// so calculate and cache the value of `ENCRYPT(VC)`.
|
|
|
|
if (!encrypted_vc_)
|
|
|
|
{
|
|
|
|
auto filter = tr_message_stream_encryption::Filter{};
|
2023-05-17 18:57:27 +00:00
|
|
|
filter.encrypt_init(true, dh_, info_hash);
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
encrypted_vc_.emplace();
|
|
|
|
filter.encrypt(std::data(VC), std::size(VC), std::data(*encrypted_vc_));
|
2023-05-14 20:01:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
for (; pad_b_recv_len_ <= PadbMaxlen; ++pad_b_recv_len_)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
static auto constexpr Needlen = std::size(VC);
|
|
|
|
if (peer_io->read_buffer_size() < Needlen)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(
|
|
|
|
this,
|
|
|
|
fmt::format("in read_vc... need {}, read {}, have {}", Needlen, pad_b_recv_len_, peer_io->read_buffer_size()));
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_LATER;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-05-14 20:01:04 +00:00
|
|
|
if (peer_io->read_buffer_starts_with(*encrypted_vc_))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, "found ENCRYPT(VC)!");
|
2022-07-18 18:00:26 +00:00
|
|
|
// We already know it's a match; now we just need to
|
|
|
|
// consume it from the read buffer.
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->decrypt_init(peer_io->is_incoming(), dh_, info_hash);
|
2023-09-26 17:50:41 +00:00
|
|
|
peer_io->read_buffer_discard(Needlen);
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(tr_handshake::State::AwaitingCryptoSelect);
|
2022-07-18 18:00:26 +00:00
|
|
|
return READ_NOW;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
peer_io->read_buffer_discard(1U);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "couldn't find ENCRYPT(VC)");
|
|
|
|
return done(false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_crypto_select(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
if (static auto constexpr NeedLen = sizeof(crypto_select_) + sizeof(pad_d_len_); peer_io->read_buffer_size() < NeedLen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
peer_io->read_uint32(&crypto_select_);
|
|
|
|
tr_logAddTraceHand(this, fmt::format("crypto select is {}", crypto_select_));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
if ((crypto_select_ & crypto_provide_) == 0U)
|
2007-09-22 00:53:11 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "peer selected an encryption option we didn't offer");
|
|
|
|
return done(false);
|
2007-09-22 00:53:11 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
peer_io->read_uint16(&pad_d_len_);
|
|
|
|
tr_logAddTraceHand(this, fmt::format("len(PadD) is {}", pad_d_len_));
|
|
|
|
if (pad_d_len_ > PaddMaxlen)
|
2008-01-14 16:17:02 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, "MSE handshake: len(PadD) is too long");
|
2022-12-13 17:59:21 +00:00
|
|
|
return done(false);
|
2008-01-14 16:17:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(tr_handshake::State::AwaitingPadD);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_pad_d(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("PadD: need {}, got {}", pad_d_len_, peer_io->read_buffer_size()));
|
|
|
|
if (peer_io->read_buffer_size() < pad_d_len_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
peer_io->read_buffer_discard(pad_d_len_);
|
|
|
|
|
|
|
|
/* maybe de-encrypt our connection */
|
|
|
|
if (crypto_select_ == CryptoProvidePlaintext)
|
|
|
|
{
|
|
|
|
peer_io->encrypt_disable();
|
|
|
|
peer_io->decrypt_disable();
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(tr_handshake::State::AwaitingHandshake);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
// --- Incoming and Outgoing Connections
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_handshake(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-14 01:58:39 +00:00
|
|
|
static auto constexpr Needlen = IncomingHandshakeLen;
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("read_handshake: need {}, got {}", Needlen, peer_io->read_buffer_size()));
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_size() < Needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
have_read_anything_from_peer_ = true;
|
2010-04-20 21:54:03 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
if (ia_len_ > 0U)
|
|
|
|
{
|
|
|
|
// do nothing, the check below won't work correctly
|
|
|
|
}
|
|
|
|
else if (peer_io->read_buffer_starts_with(HandshakeName)) // unencrypted
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
if (encryption_mode_ == TR_ENCRYPTION_REQUIRED)
|
2007-09-26 03:52:30 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "peer is unencrypted, and we're disallowing that");
|
|
|
|
return done(false);
|
2007-09-26 03:52:30 +00:00
|
|
|
}
|
2023-09-26 17:50:41 +00:00
|
|
|
if (crypto_select_ == CryptoProvideCrypto)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, "peer is unencrypted, and that does not agree with our handshake");
|
|
|
|
return done(false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-26 17:50:41 +00:00
|
|
|
else if (crypto_select_ == CryptoProvidePlaintext) // encrypted
|
|
|
|
{
|
|
|
|
tr_logAddTraceHand(this, "peer is encrypted, and that does not agree with our handshake");
|
|
|
|
return done(false);
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
auto name = decltype(HandshakeName){};
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(name), std::size(name));
|
2022-09-09 18:12:47 +00:00
|
|
|
if (name != HandshakeName)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, "handshake prefix not correct");
|
2022-12-13 17:59:21 +00:00
|
|
|
return done(false);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
// reserved bytes / flags
|
|
|
|
auto reserved = std::array<uint8_t, HandshakeFlagsBytes>{};
|
|
|
|
auto flags = tr_bitfield{ HandshakeFlagsBits };
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(reserved), std::size(reserved));
|
2023-04-23 01:25:55 +00:00
|
|
|
flags.set_raw(std::data(reserved), std::size(reserved));
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->set_supports_dht(flags.test(DhtFlag));
|
|
|
|
peer_io->set_supports_ltep(flags.test(LtepFlag));
|
|
|
|
peer_io->set_supports_fext(flags.test(FextFlag));
|
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-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(hash), std::size(hash));
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
if (is_incoming() && peer_io->torrent_hash() == tr_sha1_digest_t{}) // incoming plain handshake
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-14 01:58:39 +00:00
|
|
|
if (!mediator_->torrent(hash))
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "peer is trying to connect to us for a torrent we don't have.");
|
|
|
|
return done(false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->set_torrent_hash(hash);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2023-09-26 17:50:41 +00:00
|
|
|
else // outgoing, or incoming MSE handshake
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->torrent_hash() != hash)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "peer returned the wrong hash. wtf?");
|
|
|
|
return done(false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// If it's an incoming message, we need to send a response handshake
|
2022-12-13 17:59:21 +00:00
|
|
|
if (!have_sent_bittorrent_handshake_)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, "sending handshake in reply");
|
|
|
|
if (!send_handshake(peer_io))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
return done(false);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(State::AwaitingPeerId);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2008-01-01 00:20:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_peer_id(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{};
|
2023-09-26 17:50:41 +00:00
|
|
|
static auto constexpr Needlen = std::size(peer_id);
|
|
|
|
tr_logAddTraceHand(this, fmt::format("read_peer_id: need {}, got {}", Needlen, peer_io->read_buffer_size()));
|
|
|
|
if (peer_io->read_buffer_size() < Needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2023-09-26 17:50:41 +00:00
|
|
|
peer_io->read_bytes(std::data(peer_id), Needlen);
|
2022-12-13 17:59:21 +00:00
|
|
|
set_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);
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("peer-id is '{}' ... isIncoming is {}", std::data(client), is_incoming()));
|
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-12-16 07:23:12 +00:00
|
|
|
auto const info_hash = peer_io_->torrent_hash();
|
2022-12-14 01:58:39 +00:00
|
|
|
auto const info = mediator_->torrent(info_hash);
|
2022-07-11 23:29:48 +00:00
|
|
|
auto const connected_to_self = info && info->client_peer_id == peer_id;
|
2013-02-02 05:31:43 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
return done(!connected_to_self);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
// --- Incoming Connections
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_ya(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
if (peer_io->read_buffer_size() < std::size(HandshakeName))
|
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Jump to plain handshake
|
|
|
|
if (peer_io->read_buffer_starts_with(HandshakeName))
|
|
|
|
{
|
|
|
|
tr_logAddTraceHand(this, "in read_ya... got a plain incoming handshake");
|
|
|
|
set_state(tr_handshake::State::AwaitingHandshake);
|
|
|
|
return READ_NOW;
|
|
|
|
}
|
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
auto peer_public_key = DH::key_bigend_t{};
|
|
|
|
tr_logAddTraceHand(
|
2022-12-13 17:59:21 +00:00
|
|
|
this,
|
2023-09-26 17:50:41 +00:00
|
|
|
fmt::format("in read_ya... need {}, have {}", std::size(peer_public_key), peer_io->read_buffer_size()));
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_size() < std::size(peer_public_key))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
have_read_anything_from_peer_ = true;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* read the incoming peer's public key */
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(peer_public_key), std::size(peer_public_key));
|
2022-12-13 17:59:21 +00:00
|
|
|
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-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "sending B->A: Diffie Hellman Yb, PadB");
|
|
|
|
send_public_key_and_pad<PadbMaxlen>(peer_io);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(State::AwaitingPadA);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_pad_a(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-12-13 17:59:21 +00:00
|
|
|
auto const needle = tr_sha1::digest("req1"sv, dh_.secret());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
for (; pad_a_recv_len_ <= PadaMaxlen; ++pad_a_recv_len_)
|
2021-10-14 19:26:38 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
static auto constexpr Needlen = std::size(needle);
|
|
|
|
if (peer_io->read_buffer_size() < Needlen)
|
2022-07-18 18:00:26 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(
|
|
|
|
this,
|
|
|
|
fmt::format(
|
|
|
|
"in read_pad_a... need {}, read {}, have {}",
|
|
|
|
Needlen,
|
|
|
|
pad_a_recv_len_,
|
|
|
|
peer_io->read_buffer_size()));
|
2022-07-18 18:00:26 +00:00
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_starts_with(needle))
|
2022-07-18 18:00:26 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, "found HASH('req1', S)!");
|
|
|
|
peer_io->read_buffer_discard(Needlen);
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(State::AwaitingCryptoProvide);
|
2022-07-18 18:00:26 +00:00
|
|
|
return READ_NOW;
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
peer_io->read_buffer_discard(1U);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, "couldn't find HASH('req1', S)");
|
2022-12-13 17:59:21 +00:00
|
|
|
return done(false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_crypto_provide(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)) */
|
2022-09-05 13:55:17 +00:00
|
|
|
auto obfuscated_hash = tr_sha1_digest_t{};
|
2023-09-26 17:50:41 +00:00
|
|
|
static auto constexpr Needlen = std::size(obfuscated_hash) + /* HASH('req2', SKEY) xor HASH('req3', S) */
|
|
|
|
std::size(VC) + sizeof(crypto_provide_) + sizeof(pad_c_len_);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
if (peer_io->read_buffer_size() < 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-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "reading obfuscated torrent hash...");
|
2023-09-26 17:50:41 +00:00
|
|
|
auto x_or = tr_sha1_digest_t{};
|
|
|
|
peer_io->read_bytes(std::data(x_or), std::size(x_or));
|
2021-12-21 22:14:15 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
auto const req3 = tr_sha1::digest("req3"sv, 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
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
obfuscated_hash[i] = x_or[i] ^ req3[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
if (auto const info = mediator_->torrent_from_obfuscated(obfuscated_hash); info)
|
2008-04-17 03:48:56 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("got INCOMING connection's MSE handshake for torrent [{}]", info->id));
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->set_torrent_hash(info->info_hash);
|
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-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "can't find that torrent...");
|
|
|
|
return done(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), */
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const& info_hash = peer_io->torrent_hash();
|
2023-09-26 17:50:41 +00:00
|
|
|
TR_ASSERT_MSG(info_hash != tr_sha1_digest_t{}, "read_crypto_provide requires an info_hash");
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->decrypt_init(peer_io->is_incoming(), dh_, info_hash);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
auto vc_in = vc_t{};
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(vc_in), std::size(vc_in));
|
2023-09-26 17:50:41 +00:00
|
|
|
if (vc_in != VC)
|
|
|
|
{
|
|
|
|
tr_logAddTraceHand(this, "peer's VC is not all 0");
|
|
|
|
return done(false);
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
peer_io->read_uint32(&crypto_provide_);
|
|
|
|
tr_logAddTraceHand(this, fmt::format("crypto_provide is {}", crypto_provide_));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
peer_io->read_uint16(&pad_c_len_);
|
|
|
|
tr_logAddTraceHand(this, fmt::format("len(PadC) is {}", pad_c_len_));
|
|
|
|
if (pad_c_len_ > PadcMaxlen)
|
2022-07-28 03:17:05 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "peer's PadC is too big");
|
|
|
|
return done(false);
|
2022-07-28 03:17:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(State::AwaitingPadC);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_pad_c(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
if (auto const needlen = pad_c_len_ + sizeof(ia_len_); peer_io->read_buffer_size() < 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
|
2023-09-26 17:50:41 +00:00
|
|
|
peer_io->read_buffer_discard(pad_c_len_);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* read ia_len */
|
2023-09-26 17:50:41 +00:00
|
|
|
peer_io->read_uint16(&ia_len_);
|
|
|
|
tr_logAddTraceHand(this, fmt::format("len(IA) is {}", ia_len_));
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(State::AwaitingIa);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_ia(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
size_t const needlen = ia_len_;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("reading IA... have {}, need {}", peer_io->read_buffer_size(), needlen));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_size() < needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// B->A: ENCRYPT(VC, crypto_select, len(padD), padD), ENCRYPT2(Payload Stream)
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const& info_hash = peer_io->torrent_hash();
|
2023-09-26 17:50:41 +00:00
|
|
|
TR_ASSERT_MSG(info_hash != tr_sha1_digest_t{}, "read_ia requires an info_hash");
|
|
|
|
|
|
|
|
static auto constexpr BufSize = std::size(VC) + sizeof(crypto_select_) + sizeof(pad_d_len_) + HandshakeSize;
|
|
|
|
auto outbuf = libtransmission::StackBuffer<BufSize, std::byte>{};
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->encrypt_init(peer_io->is_incoming(), dh_, info_hash);
|
2007-09-26 03:52:30 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// send VC
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "sending vc");
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.add(VC);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* send crypto_select */
|
2023-09-26 17:50:41 +00:00
|
|
|
crypto_select_ = get_crypto_select(encryption_mode_, crypto_provide_);
|
|
|
|
if (crypto_select_ != 0U)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("selecting crypto mode '{}'", crypto_select_));
|
|
|
|
outbuf.add_uint32(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-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "peer didn't offer an encryption mode we like.");
|
|
|
|
return done(false);
|
2007-09-22 03:37:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "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 */
|
2023-09-26 17:50:41 +00:00
|
|
|
outbuf.add_uint16(0U);
|
2007-09-22 03:37:37 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* maybe de-encrypt our connection */
|
2023-09-26 17:50:41 +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));
|
2009-05-30 21:45:40 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
// All future communications will use ENCRYPT2()
|
|
|
|
peer_io->encrypt_disable();
|
|
|
|
peer_io->decrypt_disable(ia_len_);
|
2022-07-15 00:54:10 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* now await the handshake */
|
2023-09-26 17:50:41 +00:00
|
|
|
set_state(State::AwaitingHandshake);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2008-10-29 20:18:56 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::can_read(tr_peerIo* peer_io, void* vhandshake, size_t* piece)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handshake = static_cast<tr_handshake*>(vhandshake);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* no piece data in handshake */
|
|
|
|
*piece = 0;
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("handling canRead; state is [{}]", handshake->state_string()));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
ReadState ret = READ_NOW;
|
2023-09-26 17:50:41 +00:00
|
|
|
while (ret == READ_NOW)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
switch (handshake->state())
|
2008-10-29 19:30:17 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingHandshake:
|
|
|
|
ret = handshake->read_handshake(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingPeerId:
|
|
|
|
ret = handshake->read_peer_id(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingYa:
|
|
|
|
ret = handshake->read_ya(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingPadA:
|
|
|
|
ret = handshake->read_pad_a(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingCryptoProvide:
|
|
|
|
ret = handshake->read_crypto_provide(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingPadC:
|
|
|
|
ret = handshake->read_pad_c(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingIa:
|
|
|
|
ret = handshake->read_ia(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingYb:
|
|
|
|
ret = handshake->read_yb(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingVc:
|
|
|
|
ret = handshake->read_vc(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingCryptoSelect:
|
|
|
|
ret = handshake->read_crypto_select(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingPadD:
|
|
|
|
ret = handshake->read_pad_d(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:
|
2022-12-13 17:59:21 +00:00
|
|
|
TR_ASSERT_MSG(
|
|
|
|
false,
|
|
|
|
fmt::format(FMT_STRING("unhandled handshake state {:d}"), static_cast<int>(handshake->state())));
|
2021-09-15 02:23:53 +00:00
|
|
|
ret = READ_ERR;
|
|
|
|
break;
|
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-12-16 07:23:12 +00:00
|
|
|
void tr_handshake::on_error(tr_peerIo* io, tr_error const& error, void* vhandshake)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handshake = static_cast<tr_handshake*>(vhandshake);
|
2012-12-27 22:03:58 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (io->is_utp() && !io->is_incoming() && handshake->is_state(State::AwaitingYb))
|
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-12-16 07:23:12 +00:00
|
|
|
auto const info_hash = io->torrent_hash();
|
2022-12-14 01:58:39 +00:00
|
|
|
auto const info = handshake->mediator_->torrent(info_hash);
|
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. */
|
2023-11-04 16:39:41 +00:00
|
|
|
if ((error.code() == ETIMEDOUT || error.code() == ECONNREFUSED) && info)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-06-22 20:21:44 +00:00
|
|
|
handshake->mediator_->set_utp_failed(info_hash, io->socket_address());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-27 22:03:58 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (handshake->mediator_->allows_tcp() && io->reconnect())
|
2012-12-27 22:03:58 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
handshake->send_handshake(io);
|
2022-12-13 17:59:21 +00:00
|
|
|
handshake->set_state(State::AwaitingHandshake);
|
2023-02-18 16:03:59 +00:00
|
|
|
return;
|
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 */
|
2022-12-13 17:59:21 +00:00
|
|
|
if ((handshake->is_state(State::AwaitingYb) || handshake->is_state(State::AwaitingVc)) &&
|
2022-12-16 07:23:12 +00:00
|
|
|
handshake->encryption_mode_ != TR_ENCRYPTION_REQUIRED && handshake->mediator_->allows_tcp() && io->reconnect())
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "handshake failed, trying plaintext...");
|
2023-09-26 17:50:41 +00:00
|
|
|
handshake->send_handshake(io);
|
2022-12-13 17:59:21 +00:00
|
|
|
handshake->set_state(State::AwaitingHandshake);
|
2023-02-18 16:03:59 +00:00
|
|
|
return;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2023-02-18 16:03:59 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("handshake socket err: {:s} ({:d})", error.message(), error.code()));
|
2023-02-18 16:03:59 +00:00
|
|
|
handshake->done(false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
bool tr_handshake::build_handshake_message(tr_peerIo* io, libtransmission::BufferWriter<std::byte>& buf) const
|
|
|
|
{
|
|
|
|
auto const& info_hash = io->torrent_hash();
|
|
|
|
TR_ASSERT_MSG(info_hash != tr_sha1_digest_t{}, "build_handshake_message requires an info_hash");
|
|
|
|
|
|
|
|
auto const info = mediator_->torrent(info_hash);
|
|
|
|
if (!info)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto flags = tr_bitfield{ HandshakeFlagsBits };
|
|
|
|
flags.set(LtepFlag);
|
|
|
|
flags.set(FextFlag);
|
|
|
|
if (mediator_->allows_dht())
|
|
|
|
{
|
|
|
|
flags.set(DhtFlag);
|
|
|
|
}
|
|
|
|
auto const flag_bytes = flags.raw();
|
|
|
|
|
|
|
|
buf.add(HandshakeName);
|
|
|
|
buf.add(flag_bytes);
|
|
|
|
buf.add(info_hash);
|
|
|
|
buf.add(info->client_peer_id);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_handshake::send_handshake(tr_peerIo* io)
|
|
|
|
{
|
|
|
|
auto msg = libtransmission::StackBuffer<HandshakeSize, std::byte>{};
|
|
|
|
if (!build_handshake_message(io, msg))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
TR_ASSERT(std::size(msg) == HandshakeSize);
|
|
|
|
io->write(msg, false);
|
|
|
|
have_sent_bittorrent_handshake_ = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t tr_handshake::crypto_provide() const noexcept
|
|
|
|
{
|
|
|
|
auto provide = uint32_t{};
|
|
|
|
|
|
|
|
switch (encryption_mode_)
|
|
|
|
{
|
|
|
|
case TR_ENCRYPTION_REQUIRED:
|
|
|
|
case TR_ENCRYPTION_PREFERRED:
|
|
|
|
provide |= CryptoProvideCrypto;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_CLEAR_PREFERRED:
|
|
|
|
provide |= CryptoProvideCrypto | CryptoProvidePlaintext;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return provide;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] uint32_t tr_handshake::get_crypto_select(tr_encryption_mode encryption_mode, uint32_t crypto_provide) noexcept
|
|
|
|
{
|
|
|
|
auto choices = std::array<uint32_t, 2>{};
|
|
|
|
int n_choices = 0;
|
|
|
|
|
|
|
|
switch (encryption_mode)
|
|
|
|
{
|
|
|
|
case TR_ENCRYPTION_REQUIRED:
|
|
|
|
choices[n_choices++] = CryptoProvideCrypto;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_ENCRYPTION_PREFERRED:
|
|
|
|
choices[n_choices++] = CryptoProvideCrypto;
|
|
|
|
choices[n_choices++] = CryptoProvidePlaintext;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_CLEAR_PREFERRED:
|
|
|
|
choices[n_choices++] = CryptoProvidePlaintext;
|
|
|
|
choices[n_choices++] = CryptoProvideCrypto;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto const& choice : choices)
|
|
|
|
{
|
|
|
|
if ((crypto_provide & choice) != 0)
|
|
|
|
{
|
|
|
|
return choice;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
bool tr_handshake::fire_done(bool is_connected)
|
|
|
|
{
|
2022-12-19 22:49:26 +00:00
|
|
|
maybe_recycle_dh();
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
if (!on_done_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handshake could get destroyed inside on_done,
|
|
|
|
// so handle all our housekeeping *before* calling it
|
|
|
|
|
|
|
|
auto cb = DoneFunc{};
|
|
|
|
std::swap(cb, on_done_);
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
return (cb)(Result{ peer_io_, peer_id_, have_read_anything_from_peer_, is_connected });
|
2022-12-14 01:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view tr_handshake::state_string(State state) noexcept
|
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case State::AwaitingHandshake:
|
|
|
|
return "awaiting handshake";
|
|
|
|
case State::AwaitingPeerId:
|
|
|
|
return "awaiting peer id";
|
|
|
|
case State::AwaitingYa:
|
|
|
|
return "awaiting ya";
|
|
|
|
case State::AwaitingPadA:
|
|
|
|
return "awaiting pad a";
|
|
|
|
case State::AwaitingCryptoProvide:
|
|
|
|
return "awaiting crypto provide";
|
|
|
|
case State::AwaitingPadC:
|
|
|
|
return "awaiting pad c";
|
|
|
|
case State::AwaitingIa:
|
|
|
|
return "awaiting ia";
|
|
|
|
|
|
|
|
// outgoing
|
|
|
|
case State::AwaitingYb:
|
|
|
|
return "awaiting yb";
|
|
|
|
case State::AwaitingVc:
|
|
|
|
return "awaiting vc";
|
|
|
|
case State::AwaitingCryptoSelect:
|
|
|
|
return "awaiting crypto select";
|
|
|
|
case State::AwaitingPadD:
|
|
|
|
return "awaiting pad d";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown state";
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_handshake::tr_handshake(Mediator* mediator, std::shared_ptr<tr_peerIo> peer_io, tr_encryption_mode mode, DoneFunc on_done)
|
2022-12-19 22:49:26 +00:00
|
|
|
: dh_{ tr_handshake::get_dh(mediator) }
|
2022-12-14 01:58:39 +00:00
|
|
|
, on_done_{ std::move(on_done) }
|
2022-12-13 17:59:21 +00:00
|
|
|
, peer_io_{ std::move(peer_io) }
|
|
|
|
, timeout_timer_{ mediator->timer_maker().create([this]() { fire_done(false); }) }
|
|
|
|
, mediator_{ mediator }
|
2022-12-14 01:58:39 +00:00
|
|
|
, encryption_mode_{ mode }
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
timeout_timer_->start_single_shot(HandshakeTimeoutSec);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io_->set_callbacks(&tr_handshake::can_read, nullptr, &tr_handshake::on_error, this);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
if (is_incoming())
|
2012-12-27 22:03:58 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
set_state(State::AwaitingYa);
|
2012-12-27 22:03:58 +00:00
|
|
|
}
|
2022-12-13 17:59:21 +00:00
|
|
|
else if (encryption_mode_ != TR_CLEAR_PREFERRED)
|
2012-12-27 22:03:58 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
send_ya(peer_io_.get());
|
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
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
tr_logAddTraceHand(this, "sending plain handshake");
|
|
|
|
send_handshake(peer_io_.get());
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(State::AwaitingHandshake);
|
2007-11-08 21:20:08 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|