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)
|
|
|
|
//
|
|
|
|
#pragma once
|
2018-06-26 21:51:00 +02:00
|
|
|
|
2019-01-31 03:23:40 +01:00
|
|
|
#include "td/mtproto/PacketInfo.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-09 21:49:45 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
2020-10-08 17:30:36 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
2019-02-12 17:48:52 +01:00
|
|
|
#include "td/utils/StorerBase.h"
|
2018-12-19 22:18:53 +01:00
|
|
|
#include "td/utils/UInt.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-31 03:27:21 +01:00
|
|
|
#include <utility>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2020-10-05 17:07:23 +02:00
|
|
|
|
|
|
|
extern int VERBOSITY_NAME(raw_mtproto);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace mtproto {
|
2019-01-31 03:05:40 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class AuthKey;
|
|
|
|
|
|
|
|
class Transport {
|
|
|
|
public:
|
2018-06-17 19:52:40 +02:00
|
|
|
class ReadResult {
|
|
|
|
public:
|
|
|
|
enum Type { Packet, Nop, Error, Quickack };
|
|
|
|
|
|
|
|
static ReadResult make_nop() {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
static ReadResult make_error(int32 error_code) {
|
|
|
|
ReadResult res;
|
|
|
|
res.type_ = Error;
|
|
|
|
res.error_code_ = error_code;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
static ReadResult make_packet(MutableSlice packet) {
|
|
|
|
CHECK(!packet.empty());
|
|
|
|
ReadResult res;
|
|
|
|
res.type_ = Packet;
|
|
|
|
res.packet_ = packet;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
static ReadResult make_quick_ack(uint32 quick_ack) {
|
|
|
|
ReadResult res;
|
|
|
|
res.type_ = Quickack;
|
|
|
|
res.quick_ack_ = quick_ack;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type type() const {
|
|
|
|
return type_;
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableSlice packet() const {
|
|
|
|
CHECK(type_ == Packet);
|
|
|
|
return packet_;
|
|
|
|
}
|
|
|
|
uint32 quick_ack() const {
|
|
|
|
CHECK(type_ == Quickack);
|
|
|
|
return quick_ack_;
|
|
|
|
}
|
|
|
|
int32 error() const {
|
|
|
|
CHECK(type_ == Error);
|
|
|
|
return error_code_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Type type_ = Nop;
|
|
|
|
MutableSlice packet_;
|
2018-10-26 16:11:20 +02:00
|
|
|
int32 error_code_ = 0;
|
|
|
|
uint32 quick_ack_ = 0;
|
2018-06-17 19:52:40 +02:00
|
|
|
};
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static Result<uint64> read_auth_key_id(Slice message);
|
|
|
|
|
2021-07-02 02:25:09 +02:00
|
|
|
// Reads MTProto packet from [message] and saves it into [data].
|
2018-12-31 20:04:05 +01:00
|
|
|
// If message is encrypted, [auth_key] is used.
|
|
|
|
// Decryption and unpacking is made inplace, so [data] will be subslice of [message].
|
2021-07-02 02:25:09 +02:00
|
|
|
// Returns size of MTProto packet.
|
2018-12-31 20:04:05 +01:00
|
|
|
// If dest.size() >= size, the packet is also written into [dest].
|
|
|
|
// If auth_key is nonempty, encryption will be used.
|
2023-09-09 23:04:55 +02:00
|
|
|
static Result<ReadResult> read(MutableSlice message, const AuthKey &auth_key,
|
|
|
|
PacketInfo *packet_info) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-09 23:04:55 +02:00
|
|
|
static BufferWriter write(const Storer &storer, const AuthKey &auth_key, PacketInfo *packet_info,
|
|
|
|
size_t prepend_size = 0, size_t append_size = 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-09 21:49:45 +02:00
|
|
|
// public for testing purposes
|
2021-02-10 21:24:42 +01:00
|
|
|
static std::pair<uint32, UInt128> calc_message_key2(const AuthKey &auth_key, int X, Slice to_encrypt);
|
2021-03-26 16:52:45 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
template <class HeaderT>
|
2019-01-31 03:27:21 +01:00
|
|
|
static std::pair<uint32, UInt128> calc_message_ack_and_key(const HeaderT &head, size_t data_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
template <class HeaderT>
|
|
|
|
static size_t calc_crypto_size(size_t data_size);
|
|
|
|
|
|
|
|
template <class HeaderT>
|
2023-09-09 23:04:55 +02:00
|
|
|
static size_t calc_crypto_size2(size_t data_size, PacketInfo *packet_info);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
static size_t calc_no_crypto_size(size_t data_size);
|
|
|
|
|
2023-09-09 23:04:55 +02:00
|
|
|
static Status read_no_crypto(MutableSlice message, PacketInfo *packet_info, MutableSlice *data) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-09 23:04:55 +02:00
|
|
|
static Status read_crypto(MutableSlice message, const AuthKey &auth_key, PacketInfo *packet_info,
|
2018-12-31 20:04:05 +01:00
|
|
|
MutableSlice *data) TD_WARN_UNUSED_RESULT;
|
2023-09-09 21:22:35 +02:00
|
|
|
|
2023-09-09 23:04:55 +02:00
|
|
|
static Status read_e2e_crypto(MutableSlice message, const AuthKey &auth_key, PacketInfo *packet_info,
|
2018-12-31 20:04:05 +01:00
|
|
|
MutableSlice *data) TD_WARN_UNUSED_RESULT;
|
2023-09-09 21:22:35 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class HeaderT, class PrefixT>
|
|
|
|
static Status read_crypto_impl(int X, MutableSlice message, const AuthKey &auth_key, HeaderT **header_ptr,
|
2023-09-09 23:04:55 +02:00
|
|
|
PrefixT **prefix_ptr, MutableSlice *data,
|
|
|
|
PacketInfo *packet_info) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-09 23:04:55 +02:00
|
|
|
static BufferWriter write_no_crypto(const Storer &storer, PacketInfo *packet_info, size_t prepend_size,
|
|
|
|
size_t append_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-09 23:04:55 +02:00
|
|
|
static BufferWriter write_crypto(const Storer &storer, const AuthKey &auth_key, PacketInfo *packet_info,
|
|
|
|
size_t prepend_size, size_t append_size);
|
2023-09-09 21:22:35 +02:00
|
|
|
|
2023-09-09 23:04:55 +02:00
|
|
|
static BufferWriter write_e2e_crypto(const Storer &storer, const AuthKey &auth_key, PacketInfo *packet_info,
|
2023-09-09 22:46:56 +02:00
|
|
|
size_t prepend_size, size_t append_size);
|
2023-09-09 21:22:35 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class HeaderT>
|
2023-09-09 23:04:55 +02:00
|
|
|
static void write_crypto_impl(int X, const Storer &storer, const AuthKey &auth_key, PacketInfo *packet_info,
|
|
|
|
HeaderT *header, size_t data_size, size_t padded_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-06-26 21:51:00 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|