Padding in unencrypted packets
GitOrigin-RevId: e547cedd27afebc8023afaca5ef0a7af6e2f3075
This commit is contained in:
parent
b44c7cfcb8
commit
d07c172ec8
@ -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) {
|
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) {
|
if (res_pq->nonce_ != nonce) {
|
||||||
return Status::Error("Nonce mismatch");
|
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) {
|
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()) {
|
switch (server_dh_params->get_id()) {
|
||||||
case mtproto_api::server_DH_params_ok::ID:
|
case mtproto_api::server_DH_params_ok::ID:
|
||||||
break;
|
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) {
|
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()) {
|
switch (answer->get_id()) {
|
||||||
case mtproto_api::dh_gen_ok::ID:
|
case mtproto_api::dh_gen_ok::ID:
|
||||||
state_ = Finish;
|
state_ = Finish;
|
||||||
|
@ -7,22 +7,33 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "td/mtproto/PacketStorer.h"
|
#include "td/mtproto/PacketStorer.h"
|
||||||
|
|
||||||
|
#include "td/utils/Random.h"
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace mtproto {
|
namespace mtproto {
|
||||||
class NoCryptoImpl {
|
class NoCryptoImpl {
|
||||||
public:
|
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>
|
template <class T>
|
||||||
void do_store(T &storer) const {
|
void do_store(T &storer) const {
|
||||||
storer.store_binary(message_id);
|
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_storer(data);
|
||||||
|
storer.store_slice(pad_);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint64 message_id;
|
uint64 message_id;
|
||||||
const Storer &data;
|
const Storer &data;
|
||||||
|
std::string pad_;
|
||||||
};
|
};
|
||||||
} // namespace mtproto
|
} // namespace mtproto
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
@ -31,11 +31,13 @@ struct Query {
|
|||||||
} // namespace mtproto
|
} // namespace mtproto
|
||||||
|
|
||||||
template <class T>
|
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);
|
TlParser parser(message);
|
||||||
auto result = T::fetch_result(parser);
|
auto result = T::fetch_result(parser);
|
||||||
|
|
||||||
parser.fetch_end();
|
if (check_end) {
|
||||||
|
parser.fetch_end();
|
||||||
|
}
|
||||||
const char *error = parser.get_error();
|
const char *error = parser.get_error();
|
||||||
if (error != nullptr) {
|
if (error != nullptr) {
|
||||||
LOG(ERROR) << "Can't parse: " << format::as_hex_dump<4>(message);
|
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>
|
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);
|
TlBufferParser parser(&message);
|
||||||
auto result = T::fetch_result(parser);
|
auto result = T::fetch_result(parser);
|
||||||
|
|
||||||
parser.fetch_end();
|
if (check_end) {
|
||||||
|
parser.fetch_end();
|
||||||
|
}
|
||||||
const char *error = parser.get_error();
|
const char *error = parser.get_error();
|
||||||
if (error != nullptr) {
|
if (error != nullptr) {
|
||||||
LOG(ERROR) << "Can't parse: " << format::as_hex_dump<4>(message.as_slice());
|
LOG(ERROR) << "Can't parse: " << format::as_hex_dump<4>(message.as_slice());
|
||||||
|
Reference in New Issue
Block a user