2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 18:05:39 +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
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
#include <algorithm> // for std::copy()
|
|
|
|
#include <array>
|
2022-12-13 17:59:21 +00:00
|
|
|
#include <chrono>
|
2022-12-14 01:58:39 +00:00
|
|
|
#include <cstdint> // for uintX_t
|
|
|
|
#include <cstddef> // for std::byte, size_t
|
2022-12-13 17:59:21 +00:00
|
|
|
#include <functional>
|
2022-07-11 23:29:48 +00:00
|
|
|
#include <memory>
|
2022-12-19 22:49:26 +00:00
|
|
|
#include <mutex>
|
2022-12-13 17:59:21 +00:00
|
|
|
#include <optional>
|
2022-12-14 01:58:39 +00:00
|
|
|
#include <string_view>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <utility>
|
2021-10-22 02:40:55 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/peer-mse.h" // tr_message_stream_encryption::DH
|
|
|
|
#include "libtransmission/peer-io.h"
|
|
|
|
#include "libtransmission/timer.h"
|
|
|
|
#include "libtransmission/tr-macros.h" // tr_sha1_digest_t, tr_peer_id_t
|
|
|
|
|
|
|
|
struct tr_error;
|
2023-11-03 17:03:26 +00:00
|
|
|
struct tr_socket_address;
|
|
|
|
|
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
template<typename value_type>
|
|
|
|
class BufferWriter;
|
|
|
|
}
|
2009-05-29 19:17:12 +00:00
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
// short-term class which manages the handshake phase of a tr_peerIo
|
2022-12-13 17:59:21 +00:00
|
|
|
class tr_handshake
|
2022-07-11 23:29:48 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-12-13 17:59:21 +00:00
|
|
|
using DH = tr_message_stream_encryption::DH;
|
|
|
|
|
|
|
|
struct Result
|
|
|
|
{
|
|
|
|
std::shared_ptr<tr_peerIo> io;
|
|
|
|
std::optional<tr_peer_id_t> peer_id;
|
2022-12-14 01:58:39 +00:00
|
|
|
bool read_anything_from_peer = false;
|
|
|
|
bool is_connected = false;
|
2022-12-13 17:59:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using DoneFunc = std::function<bool(Result const&)>;
|
|
|
|
|
|
|
|
class Mediator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct TorrentInfo
|
|
|
|
{
|
|
|
|
tr_sha1_digest_t info_hash;
|
|
|
|
tr_peer_id_t client_peer_id;
|
|
|
|
tr_torrent_id_t id;
|
|
|
|
bool is_done;
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual ~Mediator() = default;
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
[[nodiscard]] virtual std::optional<TorrentInfo> torrent(tr_sha1_digest_t const& info_hash) const = 0;
|
|
|
|
[[nodiscard]] virtual std::optional<TorrentInfo> torrent_from_obfuscated(tr_sha1_digest_t const& info_hash) const = 0;
|
2022-12-13 17:59:21 +00:00
|
|
|
[[nodiscard]] virtual libtransmission::TimerMaker& timer_maker() = 0;
|
|
|
|
[[nodiscard]] virtual bool allows_dht() const = 0;
|
|
|
|
[[nodiscard]] virtual bool allows_tcp() const = 0;
|
|
|
|
[[nodiscard]] virtual size_t pad(void* setme, size_t max_bytes) const = 0;
|
|
|
|
[[nodiscard]] virtual DH::private_key_bigend_t private_key() const
|
|
|
|
{
|
|
|
|
return DH::randomPrivateKey();
|
|
|
|
}
|
|
|
|
|
2023-07-05 16:33:50 +00:00
|
|
|
virtual void set_utp_failed(tr_sha1_digest_t const& info_hash, tr_socket_address const& socket_address) = 0;
|
2022-12-13 17:59:21 +00:00
|
|
|
};
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
tr_handshake(Mediator* mediator, std::shared_ptr<tr_peerIo> peer_io, tr_encryption_mode mode_in, DoneFunc on_done);
|
2022-12-13 17:59:21 +00:00
|
|
|
|
|
|
|
private:
|
2023-09-26 17:50:41 +00:00
|
|
|
enum class State : uint8_t
|
2022-12-14 01:58:39 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
// incoming and outgoing
|
2022-12-13 17:59:21 +00:00
|
|
|
AwaitingHandshake,
|
|
|
|
AwaitingPeerId,
|
2023-09-26 17:50:41 +00:00
|
|
|
|
|
|
|
// incoming
|
2022-12-13 17:59:21 +00:00
|
|
|
AwaitingYa,
|
|
|
|
AwaitingPadA,
|
|
|
|
AwaitingCryptoProvide,
|
|
|
|
AwaitingPadC,
|
|
|
|
AwaitingIa,
|
|
|
|
|
|
|
|
// outgoing
|
|
|
|
AwaitingYb,
|
|
|
|
AwaitingVc,
|
|
|
|
AwaitingCryptoSelect,
|
|
|
|
AwaitingPadD
|
|
|
|
};
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
///
|
2022-12-13 17:59:21 +00:00
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
ReadState read_crypto_provide(tr_peerIo*);
|
|
|
|
ReadState read_crypto_select(tr_peerIo*);
|
|
|
|
ReadState read_handshake(tr_peerIo*);
|
|
|
|
ReadState read_ia(tr_peerIo*);
|
|
|
|
ReadState read_pad_a(tr_peerIo*);
|
|
|
|
ReadState read_pad_c(tr_peerIo*);
|
|
|
|
ReadState read_pad_d(tr_peerIo*);
|
|
|
|
ReadState read_peer_id(tr_peerIo*);
|
|
|
|
ReadState read_vc(tr_peerIo*);
|
|
|
|
ReadState read_ya(tr_peerIo*);
|
|
|
|
ReadState read_yb(tr_peerIo*);
|
|
|
|
|
|
|
|
void send_ya(tr_peerIo*);
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
void set_peer_id(tr_peer_id_t const& id) noexcept
|
|
|
|
{
|
|
|
|
peer_id_ = id;
|
|
|
|
}
|
2022-07-11 23:29:48 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState done(bool is_connected)
|
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io_->clear_callbacks();
|
2022-12-13 17:59:21 +00:00
|
|
|
return fire_done(is_connected) ? READ_LATER : READ_ERR;
|
|
|
|
}
|
2022-07-11 23:29:48 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
[[nodiscard]] auto is_incoming() const noexcept
|
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
return peer_io_->is_incoming();
|
2022-12-13 17:59:21 +00:00
|
|
|
}
|
2022-08-26 02:27:11 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
[[nodiscard]] constexpr auto state() const noexcept
|
2022-07-15 00:54:10 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
return state_;
|
2022-07-15 00:54:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
[[nodiscard]] constexpr auto is_state(State state) const noexcept
|
|
|
|
{
|
|
|
|
return state_ == state;
|
|
|
|
}
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
constexpr void set_state(State state) noexcept
|
2022-12-13 17:59:21 +00:00
|
|
|
{
|
|
|
|
state_ = state;
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
[[nodiscard]] static std::string_view state_string(State state) noexcept;
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
[[nodiscard]] std::string_view state_string() const noexcept
|
2022-12-13 17:59:21 +00:00
|
|
|
{
|
|
|
|
return state_string(state_);
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
static ReadState can_read(tr_peerIo* peer_io, void* vhandshake, size_t* piece);
|
|
|
|
|
|
|
|
static void on_error(tr_peerIo* io, tr_error const&, void* vhandshake);
|
|
|
|
|
|
|
|
bool build_handshake_message(tr_peerIo* io, libtransmission::BufferWriter<std::byte>& buf) const;
|
|
|
|
|
|
|
|
bool send_handshake(tr_peerIo* io);
|
2022-12-13 17:59:21 +00:00
|
|
|
|
|
|
|
template<size_t PadMax>
|
|
|
|
void send_public_key_and_pad(tr_peerIo* io)
|
|
|
|
{
|
|
|
|
auto const public_key = dh_.publicKey();
|
|
|
|
auto outbuf = std::array<std::byte, std::size(public_key) + PadMax>{};
|
|
|
|
auto const data = std::data(outbuf);
|
|
|
|
auto walk = data;
|
|
|
|
walk = std::copy(std::begin(public_key), std::end(public_key), walk);
|
|
|
|
walk += mediator_->pad(walk, PadMax);
|
2022-12-16 07:23:12 +00:00
|
|
|
io->write_bytes(data, walk - data, false);
|
2022-12-13 17:59:21 +00:00
|
|
|
}
|
2022-07-11 23:29:48 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
[[nodiscard]] uint32_t crypto_provide() const noexcept;
|
|
|
|
|
|
|
|
[[nodiscard]] static uint32_t get_crypto_select(tr_encryption_mode encryption_mode, uint32_t crypto_provide) noexcept;
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
bool fire_done(bool is_connected);
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
static auto constexpr HandshakeTimeoutSec = std::chrono::seconds{ 30 };
|
|
|
|
|
|
|
|
// bittorrent handshake constants
|
|
|
|
// https://www.bittorrent.org/beps/bep_0003.html#peer-protocol
|
|
|
|
// https://wiki.theory.org/BitTorrentSpecification#Handshake
|
|
|
|
|
|
|
|
// > The handshake starts with character ninteen (decimal) followed by the string
|
|
|
|
// > 'BitTorrent protocol'. The leading character is a length prefix.
|
|
|
|
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' }
|
|
|
|
};
|
|
|
|
|
|
|
|
// [Next comes] eight reserved bytes [used for enabling ltep, dht, fext]
|
|
|
|
static auto constexpr HandshakeFlagsBytes = size_t{ 8 };
|
|
|
|
static auto constexpr HandshakeFlagsBits = size_t{ 64 };
|
|
|
|
// https://www.bittorrent.org/beps/bep_0004.html
|
|
|
|
// https://wiki.theory.org/BitTorrentSpecification#Reserved_Bytes
|
|
|
|
static auto constexpr LtepFlag = size_t{ 43U };
|
|
|
|
static auto constexpr FextFlag = size_t{ 61U };
|
|
|
|
static auto constexpr DhtFlag = size_t{ 63U };
|
|
|
|
|
|
|
|
// Next comes the 20 byte sha1 info_hash and the 20-byte peer_id
|
2023-09-26 17:50:41 +00:00
|
|
|
static auto constexpr HandshakeSize = std::size(HandshakeName) + HandshakeFlagsBytes + std::tuple_size_v<tr_sha1_digest_t> +
|
|
|
|
std::tuple_size_v<tr_peer_id_t>;
|
|
|
|
static_assert(HandshakeSize == 68U);
|
2022-12-14 01:58:39 +00:00
|
|
|
|
|
|
|
// Length of handhshake up through the info_hash. From theory.org:
|
|
|
|
// > The recipient may wait for the initiator's handshake... however,
|
|
|
|
// > the recipient must respond as soon as it sees the info_hash part
|
|
|
|
// > of the handshake (the peer id will presumably be sent after the
|
|
|
|
// > recipient sends its own handshake).
|
2023-09-26 17:50:41 +00:00
|
|
|
static auto constexpr IncomingHandshakeLen = std::size(HandshakeName) + HandshakeFlagsBytes +
|
|
|
|
std::tuple_size_v<tr_sha1_digest_t>;
|
|
|
|
static_assert(IncomingHandshakeLen == 48U);
|
2022-12-14 01:58:39 +00:00
|
|
|
|
|
|
|
// MSE constants.
|
|
|
|
// http://wiki.vuze.com/w/Message_Stream_Encryption
|
|
|
|
// > crypto_provide and crypto_select are a 32bit bitfields.
|
|
|
|
// > As of now 0x01 means plaintext, 0x02 means RC4. (see Functions)
|
|
|
|
// > The remaining bits are reserved for future use.
|
2023-09-26 17:50:41 +00:00
|
|
|
static auto constexpr CryptoProvidePlaintext = uint32_t{ 0x01 };
|
|
|
|
static auto constexpr CryptoProvideCrypto = uint32_t{ 0x02 };
|
2022-12-14 01:58:39 +00:00
|
|
|
|
|
|
|
// MSE constants.
|
|
|
|
// http://wiki.vuze.com/w/Message_Stream_Encryption
|
|
|
|
// > PadA, PadB: Random data with a random length of 0 to 512 bytes each
|
|
|
|
// > PadC, PadD: Arbitrary data with a length of 0 to 512 bytes
|
2023-09-26 17:50:41 +00:00
|
|
|
static auto constexpr PadaMaxlen = uint16_t{ 512U };
|
|
|
|
static auto constexpr PadbMaxlen = uint16_t{ 512U };
|
|
|
|
static auto constexpr PadcMaxlen = uint16_t{ 512U };
|
|
|
|
static auto constexpr PaddMaxlen = uint16_t{ 512U };
|
2022-12-14 01:58:39 +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
|
|
|
|
// SKEY hash. As of this version VC is a String of 8 bytes set to 0x00."
|
|
|
|
// https://wiki.vuze.com/w/Message_Stream_Encryption
|
|
|
|
using vc_t = std::array<std::byte, 8>;
|
|
|
|
static auto constexpr VC = vc_t{};
|
|
|
|
|
2023-05-14 20:01:04 +00:00
|
|
|
// Used when resynchronizing in read_vc(). This value is cached to avoid
|
|
|
|
// the cost of recomputing it. MSE spec: "Since the length of [PadB is]
|
|
|
|
// unknown, A will be able to resynchronize on ENCRYPT(VC)".
|
|
|
|
std::optional<vc_t> encrypted_vc_;
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
///
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-19 22:49:26 +00:00
|
|
|
static constexpr auto DhPoolMaxSize = size_t{ 32 };
|
2023-09-26 17:50:41 +00:00
|
|
|
static inline auto dh_pool_size = size_t{};
|
|
|
|
static inline auto dh_pool = std::array<tr_message_stream_encryption::DH, DhPoolMaxSize>{};
|
|
|
|
static inline auto dh_pool_mutex = std::mutex{};
|
2022-12-19 22:49:26 +00:00
|
|
|
|
|
|
|
[[nodiscard]] static DH get_dh(Mediator* mediator)
|
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
auto lock = std::unique_lock(dh_pool_mutex);
|
2022-12-19 22:49:26 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
if (dh_pool_size > 0U)
|
2022-12-19 22:49:26 +00:00
|
|
|
{
|
|
|
|
auto dh = DH{};
|
2023-09-26 17:50:41 +00:00
|
|
|
std::swap(dh, dh_pool[dh_pool_size - 1U]);
|
|
|
|
--dh_pool_size;
|
2022-12-19 22:49:26 +00:00
|
|
|
return dh;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DH{ mediator->private_key() };
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
static void add_dh(DH dh)
|
2022-12-19 22:49:26 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
auto lock = std::unique_lock(dh_pool_mutex);
|
2022-12-19 22:49:26 +00:00
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
if (dh_pool_size < std::size(dh_pool))
|
2022-12-19 22:49:26 +00:00
|
|
|
{
|
2023-09-26 17:50:41 +00:00
|
|
|
dh_pool[dh_pool_size] = dh;
|
|
|
|
++dh_pool_size;
|
2022-12-19 22:49:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void maybe_recycle_dh()
|
|
|
|
{
|
|
|
|
// keys are expensive to make, so recycle iff the peer was unreachable
|
|
|
|
|
|
|
|
if (have_read_anything_from_peer_)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto dh = DH{};
|
|
|
|
std::swap(dh_, dh);
|
2023-09-26 17:50:41 +00:00
|
|
|
add_dh(dh);
|
2022-12-19 22:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
DH dh_{};
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
DoneFunc on_done_;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
std::optional<tr_peer_id_t> peer_id_;
|
|
|
|
|
|
|
|
std::shared_ptr<tr_peerIo> peer_io_;
|
|
|
|
|
|
|
|
std::unique_ptr<libtransmission::Timer> timeout_timer_;
|
|
|
|
|
|
|
|
Mediator* mediator_ = nullptr;
|
|
|
|
|
|
|
|
State state_ = State::AwaitingHandshake;
|
|
|
|
|
|
|
|
tr_encryption_mode encryption_mode_;
|
|
|
|
|
|
|
|
uint32_t crypto_select_ = {};
|
|
|
|
uint32_t crypto_provide_ = {};
|
|
|
|
uint16_t pad_c_len_ = {};
|
|
|
|
uint16_t pad_d_len_ = {};
|
|
|
|
uint16_t ia_len_ = {};
|
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
uint16_t pad_a_recv_len_ = {};
|
|
|
|
uint16_t pad_b_recv_len_ = {};
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
bool have_read_anything_from_peer_ = false;
|
|
|
|
|
|
|
|
bool have_sent_bittorrent_handshake_ = false;
|
|
|
|
};
|