2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/mtproto/TcpTransport.h"
|
|
|
|
|
2018-12-19 22:57:56 +01:00
|
|
|
#include "td/utils/as.h"
|
2019-02-12 21:48:16 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-02-12 10:01:11 +01:00
|
|
|
#include "td/utils/Random.h"
|
2018-02-28 11:25:07 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2018-02-12 10:01:11 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
|
|
|
namespace mtproto {
|
|
|
|
namespace tcp {
|
2019-07-09 05:56:06 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t IntermediateTransport::read_from_stream(ChainBufferReader *stream, BufferSlice *message, uint32 *quick_ack) {
|
|
|
|
CHECK(message);
|
|
|
|
size_t stream_size = stream->size();
|
|
|
|
size_t header_size = 4;
|
|
|
|
if (stream->size() < header_size) {
|
|
|
|
return header_size;
|
|
|
|
}
|
|
|
|
uint32 data_size;
|
|
|
|
auto it = stream->clone();
|
|
|
|
it.advance(header_size, MutableSlice(reinterpret_cast<uint8 *>(&data_size), sizeof(data_size)));
|
|
|
|
if (data_size & (1u << 31)) {
|
|
|
|
if (quick_ack) {
|
|
|
|
*quick_ack = data_size;
|
|
|
|
}
|
|
|
|
stream->advance(header_size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t total_size = data_size + header_size;
|
|
|
|
if (stream_size < total_size) {
|
|
|
|
// optimization
|
|
|
|
// stream->make_solid(total_size);
|
|
|
|
return total_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->advance(header_size);
|
|
|
|
*message = stream->cut_head(data_size).move_as_buffer_slice();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IntermediateTransport::write_prepare_inplace(BufferWriter *message, bool quick_ack) {
|
|
|
|
size_t size = message->size();
|
|
|
|
CHECK(size % 4 == 0);
|
|
|
|
CHECK(size < (1 << 24));
|
|
|
|
if (quick_ack) {
|
|
|
|
size |= static_cast<size_t>(1) << 31;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t prepend_size = 4;
|
|
|
|
MutableSlice prepend = message->prepare_prepend();
|
|
|
|
CHECK(prepend.size() >= prepend_size);
|
|
|
|
message->confirm_prepend(prepend_size);
|
|
|
|
|
2018-06-15 20:54:22 +02:00
|
|
|
size_t append_size = 0;
|
|
|
|
if (with_padding()) {
|
2019-07-04 12:56:10 +02:00
|
|
|
append_size = Random::secure_uint32() % 16;
|
2021-02-04 13:54:12 +01:00
|
|
|
MutableSlice append = message->prepare_append().substr(0, append_size);
|
2018-06-15 20:54:22 +02:00
|
|
|
CHECK(append.size() == append_size);
|
|
|
|
Random::secure_bytes(append);
|
|
|
|
message->confirm_append(append.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
as<uint32>(message->as_slice().begin()) = static_cast<uint32>(size + append_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IntermediateTransport::init_output_stream(ChainBufferWriter *stream) {
|
2018-06-15 20:54:22 +02:00
|
|
|
const uint32 magic = with_padding() ? 0xdddddddd : 0xeeeeeeee;
|
2018-12-31 20:04:05 +01:00
|
|
|
stream->append(Slice(reinterpret_cast<const char *>(&magic), 4));
|
|
|
|
}
|
|
|
|
|
2018-02-12 10:01:11 +01:00
|
|
|
void ObfuscatedTransport::init(ChainBufferReader *input, ChainBufferWriter *output) {
|
|
|
|
input_ = input;
|
|
|
|
output_ = output;
|
|
|
|
|
|
|
|
const size_t header_size = 64;
|
|
|
|
string header(header_size, '\0');
|
|
|
|
MutableSlice header_slice = header;
|
|
|
|
int32 try_cnt = 0;
|
|
|
|
while (true) {
|
|
|
|
try_cnt++;
|
|
|
|
CHECK(try_cnt < 10);
|
|
|
|
Random::secure_bytes(header_slice.ubegin(), header.size());
|
2019-07-20 00:49:22 +02:00
|
|
|
if (secret_.emulate_tls()) {
|
|
|
|
break;
|
|
|
|
}
|
2018-02-12 10:01:11 +01:00
|
|
|
if (as<uint8>(header.data()) == 0xef) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-19 23:46:55 +01:00
|
|
|
uint32 first_int = as<uint32>(header.data());
|
2018-02-12 10:01:11 +01:00
|
|
|
if (first_int == 0x44414548 || first_int == 0x54534f50 || first_int == 0x20544547 || first_int == 0x4954504f ||
|
2019-07-20 00:49:22 +02:00
|
|
|
first_int == 0xdddddddd || first_int == 0xeeeeeeee || first_int == 0x02010316) {
|
2018-02-12 10:01:11 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-19 23:46:55 +01:00
|
|
|
uint32 second_int = as<uint32>(header.data() + sizeof(uint32));
|
2018-02-12 10:01:11 +01:00
|
|
|
if (second_int == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-06-15 20:54:22 +02:00
|
|
|
as<uint32>(header_slice.begin() + 56) = impl_.with_padding() ? 0xdddddddd : 0xeeeeeeee;
|
2018-04-30 19:01:18 +02:00
|
|
|
if (dc_id_ != 0) {
|
|
|
|
as<int16>(header_slice.begin() + 60) = dc_id_;
|
|
|
|
}
|
2018-02-12 10:01:11 +01:00
|
|
|
|
|
|
|
string rheader = header;
|
|
|
|
std::reverse(rheader.begin(), rheader.end());
|
2018-12-19 15:49:13 +01:00
|
|
|
UInt256 key = as<UInt256>(rheader.data() + 8);
|
2019-07-09 04:01:12 +02:00
|
|
|
Slice proxy_secret = secret_.get_proxy_secret();
|
2018-04-30 19:01:18 +02:00
|
|
|
auto fix_key = [&](UInt256 &key) {
|
2019-07-09 04:01:12 +02:00
|
|
|
if (!proxy_secret.empty()) {
|
2018-04-30 19:01:18 +02:00
|
|
|
Sha256State state;
|
2019-07-23 02:20:11 +02:00
|
|
|
state.init();
|
|
|
|
state.feed(as_slice(key));
|
|
|
|
state.feed(proxy_secret);
|
|
|
|
state.extract(as_slice(key));
|
2018-04-30 19:01:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
fix_key(key);
|
|
|
|
aes_ctr_byte_flow_.init(key, as<UInt128>(rheader.data() + 8 + 32));
|
2019-07-09 04:01:12 +02:00
|
|
|
if (secret_.emulate_tls()) {
|
2019-06-26 15:51:48 +02:00
|
|
|
tls_reader_byte_flow_.set_input(input_);
|
|
|
|
tls_reader_byte_flow_ >> aes_ctr_byte_flow_;
|
|
|
|
} else {
|
|
|
|
aes_ctr_byte_flow_.set_input(input_);
|
|
|
|
}
|
2018-02-12 10:01:11 +01:00
|
|
|
aes_ctr_byte_flow_ >> byte_flow_sink_;
|
|
|
|
|
|
|
|
output_key_ = as<UInt256>(header.data() + 8);
|
2018-04-30 19:01:18 +02:00
|
|
|
fix_key(output_key_);
|
2019-08-12 15:53:17 +02:00
|
|
|
output_state_.init(as_slice(output_key_), Slice(header.data() + 8 + 32, 16));
|
2019-07-01 15:34:39 +02:00
|
|
|
header_ = header;
|
2018-02-12 10:01:11 +01:00
|
|
|
output_state_.encrypt(header_slice, header_slice);
|
2019-07-01 15:34:39 +02:00
|
|
|
MutableSlice(header_).substr(56).copy_from(header_slice.substr(56));
|
2018-02-12 10:01:11 +01:00
|
|
|
}
|
|
|
|
|
2019-06-26 15:51:48 +02:00
|
|
|
Result<size_t> ObfuscatedTransport::read_next(BufferSlice *message, uint32 *quick_ack) {
|
2019-07-09 04:01:12 +02:00
|
|
|
if (secret_.emulate_tls()) {
|
2019-06-26 15:51:48 +02:00
|
|
|
tls_reader_byte_flow_.wakeup();
|
|
|
|
} else {
|
|
|
|
aes_ctr_byte_flow_.wakeup();
|
|
|
|
}
|
|
|
|
return impl_.read_from_stream(byte_flow_sink_.get_output(), message, quick_ack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObfuscatedTransport::write(BufferWriter &&message, bool quick_ack) {
|
|
|
|
impl_.write_prepare_inplace(&message, quick_ack);
|
|
|
|
output_state_.encrypt(message.as_slice(), message.as_slice());
|
2019-07-09 04:01:12 +02:00
|
|
|
if (secret_.emulate_tls()) {
|
2019-06-26 15:51:48 +02:00
|
|
|
do_write_tls(std::move(message));
|
|
|
|
} else {
|
2019-07-01 15:34:39 +02:00
|
|
|
do_write_main(std::move(message));
|
2019-06-26 15:51:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 15:34:39 +02:00
|
|
|
void ObfuscatedTransport::do_write_main(BufferWriter &&message) {
|
|
|
|
BufferBuilder builder(std::move(message));
|
|
|
|
if (!header_.empty()) {
|
|
|
|
builder.prepend(header_);
|
|
|
|
header_ = {};
|
|
|
|
}
|
|
|
|
do_write(builder.extract());
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:51:48 +02:00
|
|
|
void ObfuscatedTransport::do_write_tls(BufferWriter &&message) {
|
2019-07-01 15:34:39 +02:00
|
|
|
CHECK(header_.size() <= MAX_TLS_PACKET_LENGTH);
|
|
|
|
if (message.size() + header_.size() > MAX_TLS_PACKET_LENGTH) {
|
2019-06-26 15:51:48 +02:00
|
|
|
auto buffer_slice = message.as_buffer_slice();
|
|
|
|
auto slice = buffer_slice.as_slice();
|
|
|
|
while (!slice.empty()) {
|
2019-07-01 15:34:39 +02:00
|
|
|
auto buf = buffer_slice.from_slice(slice.substr(0, MAX_TLS_PACKET_LENGTH - header_.size()));
|
2019-06-26 15:51:48 +02:00
|
|
|
slice.remove_prefix(buf.size());
|
|
|
|
BufferBuilder builder;
|
|
|
|
builder.append(std::move(buf));
|
|
|
|
do_write_tls(std::move(builder));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferBuilder builder(std::move(message));
|
|
|
|
do_write_tls(std::move(builder));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObfuscatedTransport::do_write_tls(BufferBuilder &&builder) {
|
2019-07-01 15:34:39 +02:00
|
|
|
if (!header_.empty()) {
|
|
|
|
builder.prepend(header_);
|
|
|
|
header_ = {};
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:51:48 +02:00
|
|
|
size_t size = builder.size();
|
2019-07-01 15:18:28 +02:00
|
|
|
CHECK(size <= MAX_TLS_PACKET_LENGTH);
|
2019-06-26 15:51:48 +02:00
|
|
|
|
|
|
|
char buf[] = "\x17\x03\x03\x00\x00";
|
2019-07-01 15:18:28 +02:00
|
|
|
buf[3] = static_cast<char>((size >> 8) & 0xff);
|
|
|
|
buf[4] = static_cast<char>(size & 0xff);
|
2019-06-26 15:51:48 +02:00
|
|
|
builder.prepend(Slice(buf, 5));
|
|
|
|
|
|
|
|
if (is_first_tls_packet_) {
|
|
|
|
is_first_tls_packet_ = false;
|
|
|
|
Slice first_prefix("\x14\x03\x03\x00\x01\x01");
|
|
|
|
builder.prepend(first_prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
do_write(builder.extract());
|
|
|
|
}
|
|
|
|
|
2021-10-18 18:26:14 +02:00
|
|
|
void ObfuscatedTransport::do_write(BufferSlice &&message) {
|
|
|
|
output_->append(std::move(message));
|
2019-06-26 15:51:48 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace tcp
|
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|