Move remaining MTProto classes to namespace mtproto.

This commit is contained in:
levlam 2021-07-05 21:04:23 +03:00
parent 4404e47f75
commit 61664b4044
23 changed files with 60 additions and 46 deletions

View File

@ -33,7 +33,7 @@ class HandshakeBench final : public Benchmark {
return "Handshake";
}
class FakeDhCallback final : public DhCallback {
class FakeDhCallback final : public mtproto::DhCallback {
public:
int is_good_prime(Slice prime_str) const final {
auto it = cache.find(prime_str.str());
@ -52,10 +52,10 @@ class HandshakeBench final : public Benchmark {
} dh_callback;
void run(int n) final {
DhHandshake a;
DhHandshake b;
mtproto::DhHandshake a;
mtproto::DhHandshake b;
auto prime = base64url_decode(prime_base64).move_as_ok();
DhHandshake::check_config(g, prime, &dh_callback).ensure();
mtproto::DhHandshake::check_config(g, prime, &dh_callback).ensure();
for (int i = 0; i < n; i += 2) {
a.set_config(g, prime);
b.set_config(g, prime);

View File

@ -14,6 +14,7 @@
#include "td/utils/UInt.h"
namespace td {
namespace mtproto {
Status DhHandshake::check_config(Slice prime_str, const BigNum &prime, int32 g_int, BigNumContext &ctx,
DhCallback *callback) {
@ -225,4 +226,5 @@ int64 DhHandshake::calc_key_id(Slice auth_key) {
return as<int64>(auth_key_sha1.raw + 12);
}
} // namespace mtproto
} // namespace td

View File

@ -14,8 +14,8 @@
#include <utility>
namespace td {
namespace mtproto {
/*** DH ***/
class DhCallback {
public:
DhCallback() = default;
@ -132,4 +132,5 @@ class DhHandshake {
BigNumContext ctx_;
};
} // namespace mtproto
} // namespace td

View File

@ -27,6 +27,7 @@
#endif
namespace td {
namespace mtproto {
RSA::RSA(BigNum n, BigNum e) : n_(std::move(n)), e_(std::move(e)) {
}
@ -159,4 +160,5 @@ void RSA::decrypt_signature(Slice from, MutableSlice to) const {
to.copy_from(y.to_binary(256));
}
} // namespace mtproto
} // namespace td

View File

@ -14,6 +14,7 @@
#include <utility>
namespace td {
namespace mtproto {
class RSA {
public:
@ -39,4 +40,5 @@ class PublicRsaKeyInterface {
virtual void drop_keys() = 0;
};
} // namespace mtproto
} // namespace td

View File

@ -9,6 +9,7 @@
#include "td/mtproto/mtproto_api.h"
namespace td {
namespace mtproto {
TLStorer<mtproto_api::Function> create_storer(const mtproto_api::Function &function) {
return TLStorer<mtproto_api::Function>(function);
@ -18,4 +19,5 @@ TLObjectStorer<mtproto_api::Object> create_storer(const mtproto_api::Object &obj
return TLObjectStorer<mtproto_api::Object>(object);
}
} // namespace mtproto
} // namespace td

View File

@ -48,8 +48,11 @@ class Object;
class Function;
} // namespace mtproto_api
namespace mtproto {
TLStorer<mtproto_api::Function> create_storer(const mtproto_api::Function &function);
TLObjectStorer<mtproto_api::Object> create_storer(const mtproto_api::Object &object);
} // namespace mtproto
} // namespace td

View File

@ -557,7 +557,7 @@ void CallActor::on_dh_config(Result<std::shared_ptr<DhConfig>> r_dh_config, bool
}
dh_config_ = r_dh_config.move_as_ok();
auto check_result = DhHandshake::check_config(dh_config_->g, dh_config_->prime, DhCache::instance());
auto check_result = mtproto::DhHandshake::check_config(dh_config_->g, dh_config_->prime, DhCache::instance());
if (check_result.is_error()) {
return on_error(std::move(check_result));
}

View File

@ -110,7 +110,7 @@ class CallActor final : public NetQueryCallback {
ActorShared<> parent_;
Promise<int64> call_id_promise_;
DhHandshake dh_handshake_;
mtproto::DhHandshake dh_handshake_;
std::shared_ptr<DhConfig> dh_config_;
bool dh_config_query_sent_{false};
bool dh_config_ready_{false};

View File

@ -146,7 +146,7 @@ Result<int32> HttpDate::parse_http_date(string slice) {
}
Result<SimpleConfig> decode_config(Slice input) {
static auto rsa = RSA::from_pem_public_key(
static auto rsa = mtproto::RSA::from_pem_public_key(
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIIBCgKCAQEAyr+18Rex2ohtVy8sroGP\n"
"BwXD3DOoKCSpjDqYoXgCqB7ioln4eDCFfOBUlfXUEvM/fnKCpF46VkAftlb4VuPD\n"

View File

@ -255,7 +255,7 @@ void DeviceTokenManager::register_device(tl_object_ptr<td_api::DeviceToken> devi
info.encryption_key.resize(ENCRYPTION_KEY_LENGTH);
while (true) {
Random::secure_bytes(info.encryption_key);
info.encryption_key_id = DhHandshake::calc_key_id(info.encryption_key);
info.encryption_key_id = mtproto::DhHandshake::calc_key_id(info.encryption_key);
if (info.encryption_key_id <= -MIN_ENCRYPTION_KEY_ID || info.encryption_key_id >= MIN_ENCRYPTION_KEY_ID) {
// ensure that encryption key ID never collide with anything
break;

View File

@ -12,13 +12,13 @@
namespace td {
class DhCache final : public DhCallback {
class DhCache final : public mtproto::DhCallback {
public:
int is_good_prime(Slice prime_str) const final;
void add_good_prime(Slice prime_str) const final;
void add_bad_prime(Slice prime_str) const final;
static DhCallback *instance() {
static mtproto::DhCallback *instance() {
static DhCache res;
return &res;
}

View File

@ -55,7 +55,7 @@ BufferSlice PasswordManager::calc_password_hash(Slice password, Slice client_sal
Result<BufferSlice> PasswordManager::calc_password_srp_hash(Slice password, Slice client_salt, Slice server_salt,
int32 g, Slice p) {
LOG(INFO) << "Begin password SRP hash calculation";
TRY_STATUS(DhHandshake::check_config(g, p, DhCache::instance()));
TRY_STATUS(mtproto::DhHandshake::check_config(g, p, DhCache::instance()));
auto hash = calc_password_hash(password, client_salt, server_salt);
auto p_bn = BigNum::from_binary(p);
@ -78,7 +78,7 @@ tl_object_ptr<telegram_api::InputCheckPasswordSRP> PasswordManager::get_input_ch
return make_tl_object<telegram_api::inputCheckPasswordEmpty>();
}
if (DhHandshake::check_config(g, p, DhCache::instance()).is_error()) {
if (mtproto::DhHandshake::check_config(g, p, DhCache::instance()).is_error()) {
LOG(ERROR) << "Receive invalid config " << g << " " << format::escaped(p);
return make_tl_object<telegram_api::inputCheckPasswordEmpty>();
}

View File

@ -1942,7 +1942,8 @@ Status SecretChatActor::on_dh_config(NetQueryPtr query) {
LOG(INFO) << "Got dh config";
TRY_RESULT(config, fetch_result<telegram_api::messages_getDhConfig>(std::move(query)));
downcast_call(*config, [&](auto &obj) { this->on_dh_config(obj); });
TRY_STATUS(DhHandshake::check_config(auth_state_.dh_config.g, auth_state_.dh_config.prime, context_->dh_callback()));
TRY_STATUS(mtproto::DhHandshake::check_config(auth_state_.dh_config.g, auth_state_.dh_config.prime,
context_->dh_callback()));
auth_state_.handshake.set_config(auth_state_.dh_config.g, auth_state_.dh_config.prime);
return Status::OK();
}
@ -2067,7 +2068,7 @@ void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionReque
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionAcceptKey &accept_key) {
CHECK(pfs_state_.state == PfsState::WaitSendAccept || pfs_state_.state == PfsState::SendAccept);
pfs_state_.state = PfsState::WaitAcceptResponse;
pfs_state_.handshake = DhHandshake();
pfs_state_.handshake = mtproto::DhHandshake();
on_pfs_state_changed();
}
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionAbortKey &abort_key) {
@ -2116,7 +2117,7 @@ Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionRequ
return Status::Error("Unexpected RequestKey (old key is used)");
}
pfs_state_.state = PfsState::SendAccept;
pfs_state_.handshake = DhHandshake();
pfs_state_.handshake = mtproto::DhHandshake();
pfs_state_.exchange_id = request_key.exchange_id_;
pfs_state_.handshake.set_config(auth_state_.dh_config.g, auth_state_.dh_config.prime);
pfs_state_.handshake.set_g_a(request_key.g_a_.as_slice());
@ -2146,7 +2147,7 @@ Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionAcce
return Status::Error("AcceptKey: key_fingerprint mismatch");
}
pfs_state_.state = PfsState::SendCommit;
pfs_state_.handshake = DhHandshake();
pfs_state_.handshake = mtproto::DhHandshake();
CHECK(pfs_state_.can_forget_other_key || static_cast<int64>(pfs_state_.other_auth_key.id()) == id_and_key.first);
pfs_state_.other_auth_key = mtproto::AuthKey(id_and_key.first, std::move(id_and_key.second));
pfs_state_.can_forget_other_key = false;
@ -2165,7 +2166,7 @@ Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionAbor
return Status::Error("AbortKey: unexpected");
}
pfs_state_.state = PfsState::Empty;
pfs_state_.handshake = DhHandshake();
pfs_state_.handshake = mtproto::DhHandshake();
on_pfs_state_changed();
return Status::OK();
@ -2248,7 +2249,7 @@ void SecretChatActor::request_new_key() {
CHECK(!auth_state_.dh_config.empty());
pfs_state_.state = PfsState::SendRequest;
pfs_state_.handshake = DhHandshake();
pfs_state_.handshake = mtproto::DhHandshake();
pfs_state_.handshake.set_config(auth_state_.dh_config.g, auth_state_.dh_config.prime);
pfs_state_.exchange_id = Random::secure_int64();

View File

@ -55,7 +55,7 @@ class SecretChatActor final : public NetQueryCallback {
Context(const Context &) = delete;
Context &operator=(const Context &) = delete;
virtual ~Context() = default;
virtual DhCallback *dh_callback() = 0;
virtual mtproto::DhCallback *dh_callback() = 0;
virtual BinlogInterface *binlog() = 0;
virtual SecretChatDb *secret_chat_db() = 0;
@ -242,7 +242,7 @@ class SecretChatActor final : public NetQueryCallback {
int32 last_message_id = 0;
double last_timestamp = 0;
int32 last_out_seq_no = 0;
DhHandshake handshake;
mtproto::DhHandshake handshake;
static Slice key() {
return Slice("pfs_state");
@ -373,7 +373,7 @@ class SecretChatActor final : public NetQueryCallback {
FolderId initial_folder_id;
DhConfig dh_config;
DhHandshake handshake;
mtproto::DhHandshake handshake;
static Slice key() {
return Slice("auth_state");

View File

@ -305,7 +305,7 @@ unique_ptr<SecretChatActor::Context> SecretChatsManager::make_secret_chat_contex
send_closure(std::move(sequence_dispatcher_), &SequenceDispatcher::close_silent);
}
DhCallback *dh_callback() final {
mtproto::DhCallback *dh_callback() final {
return DhCache::instance();
}
NetQueryCreator &net_query_creator() final {

View File

@ -606,10 +606,10 @@ class TestProxyRequest final : public RequestOnceActor {
}
class HandshakeContext final : public mtproto::AuthKeyHandshakeContext {
public:
DhCallback *get_dh_callback() final {
mtproto::DhCallback *get_dh_callback() final {
return nullptr;
}
PublicRsaKeyInterface *get_public_rsa_key_interface() final {
mtproto::PublicRsaKeyInterface *get_public_rsa_key_interface() final {
return &public_rsa_key;
}

View File

@ -22,7 +22,7 @@ PublicRsaKeyShared::PublicRsaKeyShared(DcId dc_id, bool is_test) : dc_id_(dc_id)
return;
}
auto add_pem = [this](CSlice pem) {
auto r_rsa = RSA::from_pem_public_key(pem);
auto r_rsa = mtproto::RSA::from_pem_public_key(pem);
LOG_CHECK(r_rsa.is_ok()) << r_rsa.error() << " " << pem;
if (r_rsa.is_ok()) {
@ -103,7 +103,7 @@ PublicRsaKeyShared::PublicRsaKeyShared(DcId dc_id, bool is_test) : dc_id_(dc_id)
"-----END RSA PUBLIC KEY-----\n");
}
void PublicRsaKeyShared::add_rsa(RSA rsa) {
void PublicRsaKeyShared::add_rsa(mtproto::RSA rsa) {
auto lock = rw_mutex_.lock_write();
auto fingerprint = rsa.get_fingerprint();
auto *has_rsa = get_rsa_locked(fingerprint);
@ -113,7 +113,7 @@ void PublicRsaKeyShared::add_rsa(RSA rsa) {
options_.push_back(RsaOption{fingerprint, std::move(rsa)});
}
Result<std::pair<RSA, int64>> PublicRsaKeyShared::get_rsa(const vector<int64> &fingerprints) {
Result<std::pair<mtproto::RSA, int64>> PublicRsaKeyShared::get_rsa(const vector<int64> &fingerprints) {
auto lock = rw_mutex_.lock_read();
for (auto fingerprint : fingerprints) {
auto *rsa = get_rsa_locked(fingerprint);
@ -144,7 +144,7 @@ void PublicRsaKeyShared::add_listener(unique_ptr<Listener> listener) {
}
}
RSA *PublicRsaKeyShared::get_rsa_locked(int64 fingerprint) {
mtproto::RSA *PublicRsaKeyShared::get_rsa_locked(int64 fingerprint) {
auto it = std::find_if(options_.begin(), options_.end(),
[&](const auto &value) { return value.fingerprint == fingerprint; });
if (it == options_.end()) {

View File

@ -18,7 +18,7 @@
namespace td {
class PublicRsaKeyShared final : public PublicRsaKeyInterface {
class PublicRsaKeyShared final : public mtproto::PublicRsaKeyInterface {
public:
PublicRsaKeyShared(DcId dc_id, bool is_test);
@ -33,8 +33,8 @@ class PublicRsaKeyShared final : public PublicRsaKeyInterface {
virtual bool notify() = 0;
};
void add_rsa(RSA rsa);
Result<std::pair<RSA, int64>> get_rsa(const vector<int64> &fingerprints) final;
void add_rsa(mtproto::RSA rsa);
Result<std::pair<mtproto::RSA, int64>> get_rsa(const vector<int64> &fingerprints) final;
void drop_keys() final;
bool has_keys();
@ -48,13 +48,13 @@ class PublicRsaKeyShared final : public PublicRsaKeyInterface {
DcId dc_id_;
struct RsaOption {
int64 fingerprint;
RSA rsa;
mtproto::RSA rsa;
};
std::vector<RsaOption> options_;
std::vector<unique_ptr<Listener>> listeners_;
RwMutex rw_mutex_;
RSA *get_rsa_locked(int64 fingerprint);
mtproto::RSA *get_rsa_locked(int64 fingerprint);
void notify();
};

View File

@ -108,7 +108,7 @@ void PublicRsaKeyWatchdog::sync_key(std::shared_ptr<PublicRsaKeyShared> &key) {
}
for (auto &config_key : cdn_config_->public_keys_) {
if (key->dc_id().get_raw_id() == config_key->dc_id_) {
auto r_rsa = RSA::from_pem_public_key(config_key->public_key_);
auto r_rsa = mtproto::RSA::from_pem_public_key(config_key->public_key_);
if (r_rsa.is_error()) {
LOG(ERROR) << r_rsa.error();
continue;

View File

@ -1256,19 +1256,20 @@ void Session::create_gen_auth_key_actor(HandshakeId handshake_id) {
}
class AuthKeyHandshakeContext final : public mtproto::AuthKeyHandshakeContext {
public:
AuthKeyHandshakeContext(DhCallback *dh_callback, std::shared_ptr<PublicRsaKeyInterface> public_rsa_key)
AuthKeyHandshakeContext(mtproto::DhCallback *dh_callback,
std::shared_ptr<mtproto::PublicRsaKeyInterface> public_rsa_key)
: dh_callback_(dh_callback), public_rsa_key_(std::move(public_rsa_key)) {
}
DhCallback *get_dh_callback() final {
mtproto::DhCallback *get_dh_callback() final {
return dh_callback_;
}
PublicRsaKeyInterface *get_public_rsa_key_interface() final {
mtproto::PublicRsaKeyInterface *get_public_rsa_key_interface() final {
return public_rsa_key_.get();
}
private:
DhCallback *dh_callback_;
std::shared_ptr<PublicRsaKeyInterface> public_rsa_key_;
mtproto::DhCallback *dh_callback_;
std::shared_ptr<mtproto::PublicRsaKeyInterface> public_rsa_key_;
};
info.actor_ = create_actor<detail::GenAuthKeyActor>(
PSLICE() << get_name() << "::GenAuthKey", get_name(), std::move(info.handshake_),

View File

@ -295,10 +295,10 @@ RegisterTest<Mtproto_ping> mtproto_ping("Mtproto_ping");
class HandshakeContext final : public mtproto::AuthKeyHandshakeContext {
public:
DhCallback *get_dh_callback() final {
mtproto::DhCallback *get_dh_callback() final {
return nullptr;
}
PublicRsaKeyInterface *get_public_rsa_key_interface() final {
mtproto::PublicRsaKeyInterface *get_public_rsa_key_interface() final {
return &public_rsa_key;
}
@ -515,7 +515,7 @@ TEST(Mtproto, notifications) {
auto push = base64url_decode(pushes[i]).move_as_ok();
auto decrypted_payload = base64url_decode(decrypted_payloads[i]).move_as_ok();
auto key_id = DhHandshake::calc_key_id(key);
auto key_id = mtproto::DhHandshake::calc_key_id(key);
ASSERT_EQ(key_id, NotificationManager::get_push_receiver_id(push).ok());
ASSERT_EQ(decrypted_payload, NotificationManager::decrypt_push(key_id, key, push).ok());
}
@ -725,7 +725,7 @@ TEST(Mtproto, RSA) {
"8Ygs/ps8e6ct82jLXbnndC9s8HjEvDvBPH9IPjv5JUlmHMBFZ5vFQIfbpo0u0+1P\n"
"n6bkEi5o7/ifoyVv2pAZTRwppTz0EuXD8QIDAQAB\n"
"-----END RSA PUBLIC KEY-----");
auto rsa = RSA::from_pem_public_key(pem).move_as_ok();
auto rsa = mtproto::RSA::from_pem_public_key(pem).move_as_ok();
ASSERT_EQ(-7596991558377038078, rsa.get_fingerprint());
ASSERT_EQ(256u, rsa.size());

View File

@ -334,7 +334,7 @@ static string prime_base64 =
"WC2xF40WnGvEZbDW_5yjko_vW5rk5Bj8Feg-vqD4f6n_Xu1wBQ3tKEn0e_lZ2VaFDOkphR8NgRX2NbEF7i5OFdBLJFS_b0-t8DSxBAMRnNjjuS_MW"
"w";
class FakeDhCallback final : public DhCallback {
class FakeDhCallback final : public mtproto::DhCallback {
public:
int is_good_prime(Slice prime_str) const final {
auto it = cache.find(prime_str.str());
@ -490,7 +490,7 @@ class FakeSecretChatContext final : public SecretChatActor::Context {
secret_chat_db_ = std::make_shared<SecretChatDb>(key_value_, 1);
net_query_creator_.stop_check(); // :(
}
DhCallback *dh_callback() final {
mtproto::DhCallback *dh_callback() final {
return &fake_dh_callback_;
}
NetQueryCreator &net_query_creator() final {