Padding in unencrypted packets

GitOrigin-RevId: e547cedd27afebc8023afaca5ef0a7af6e2f3075
This commit is contained in:
Arseny Smirnov 2018-06-15 18:11:48 +03:00
parent b44c7cfcb8
commit d07c172ec8
3 changed files with 24 additions and 9 deletions

View File

@ -56,7 +56,7 @@ Result<size_t> AuthKeyHandshake::fill_data_with_hash(uint8 *data_with_hash, cons
}
Status AuthKeyHandshake::on_res_pq(Slice message, Callback *connection, PublicRsaKeyInterface *public_rsa_key) {
TRY_RESULT(res_pq, fetch_result<mtproto_api::req_pq_multi>(message));
TRY_RESULT(res_pq, fetch_result<mtproto_api::req_pq_multi>(message, false));
if (res_pq->nonce_ != nonce) {
return Status::Error("Nonce mismatch");
}
@ -116,7 +116,7 @@ Status AuthKeyHandshake::on_res_pq(Slice message, Callback *connection, PublicRs
}
Status AuthKeyHandshake::on_server_dh_params(Slice message, Callback *connection, DhCallback *dh_callback) {
TRY_RESULT(server_dh_params, fetch_result<mtproto_api::req_DH_params>(message));
TRY_RESULT(server_dh_params, fetch_result<mtproto_api::req_DH_params>(message, false));
switch (server_dh_params->get_id()) {
case mtproto_api::server_DH_params_ok::ID:
break;
@ -215,7 +215,7 @@ Status AuthKeyHandshake::on_server_dh_params(Slice message, Callback *connection
}
Status AuthKeyHandshake::on_dh_gen_response(Slice message, Callback *connection) {
TRY_RESULT(answer, fetch_result<mtproto_api::set_client_DH_params>(message));
TRY_RESULT(answer, fetch_result<mtproto_api::set_client_DH_params>(message, false));
switch (answer->get_id()) {
case mtproto_api::dh_gen_ok::ID:
state_ = Finish;

View File

@ -7,22 +7,33 @@
#pragma once
#include "td/mtproto/PacketStorer.h"
#include "td/utils/Random.h"
namespace td {
namespace mtproto {
class NoCryptoImpl {
public:
NoCryptoImpl(uint64 message_id, const Storer &data) : message_id(message_id), data(data) {
NoCryptoImpl(uint64 message_id, const Storer &data, bool need_pad = true) : message_id(message_id), data(data) {
if (need_pad) {
auto data_size = data.size();
auto pad_size = (data_size + 15) / 16 * 16 - data_size;
pad_size += 16 * (static_cast<size_t>(Random::secure_int32()) % 16);
pad_.resize(pad_size);
Random::secure_bytes(pad_);
}
}
template <class T>
void do_store(T &storer) const {
storer.store_binary(message_id);
storer.store_binary(static_cast<int32>(data.size()));
storer.store_binary(static_cast<int32>(data.size() + pad_.size()));
storer.store_storer(data);
storer.store_slice(pad_);
}
private:
uint64 message_id;
const Storer &data;
std::string pad_;
};
} // namespace mtproto
} // namespace td

View File

@ -31,11 +31,13 @@ struct Query {
} // namespace mtproto
template <class T>
Result<typename T::ReturnType> fetch_result(Slice message) {
Result<typename T::ReturnType> fetch_result(Slice message, bool check_end = true) {
TlParser parser(message);
auto result = T::fetch_result(parser);
if (check_end) {
parser.fetch_end();
}
const char *error = parser.get_error();
if (error != nullptr) {
LOG(ERROR) << "Can't parse: " << format::as_hex_dump<4>(message);
@ -46,11 +48,13 @@ Result<typename T::ReturnType> fetch_result(Slice message) {
}
template <class T>
Result<typename T::ReturnType> fetch_result(const BufferSlice &message) {
Result<typename T::ReturnType> fetch_result(const BufferSlice &message, bool check_end = true) {
TlBufferParser parser(&message);
auto result = T::fetch_result(parser);
if (check_end) {
parser.fetch_end();
}
const char *error = parser.get_error();
if (error != nullptr) {
LOG(ERROR) << "Can't parse: " << format::as_hex_dump<4>(message.as_slice());