diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index 42b65d2c9..d7012f670 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -442,7 +442,8 @@ static ActorOwn<> get_full_config(DcOption option, Promiseis_test_dc())) { } DcId dc_id() const final { return dc_id_; @@ -492,10 +493,9 @@ static ActorOwn<> get_full_config(DcOption option, Promise public_rsa_key_ = - std::make_shared(G()->is_test_dc()); + std::shared_ptr public_rsa_key_; + vector> auth_key_listeners_; - std::vector> auth_key_listeners_; void notify() { td::remove_if(auth_key_listeners_, [&](auto &listener) { CHECK(listener != nullptr); @@ -506,6 +506,7 @@ static ActorOwn<> get_full_config(DcOption option, Promise public_rsa_key_ = PublicRsaKeySharedMain::create(false); }; auto handshake = make_unique(dc_id_, 3600); auto data = r_data.move_as_ok(); diff --git a/td/telegram/net/NetQueryDispatcher.cpp b/td/telegram/net/NetQueryDispatcher.cpp index 5bcbb3c21..58d7b6d07 100644 --- a/td/telegram/net/NetQueryDispatcher.cpp +++ b/td/telegram/net/NetQueryDispatcher.cpp @@ -154,7 +154,7 @@ Status NetQueryDispatcher::wait_dc_init(DcId dc_id, bool force) { std::shared_ptr public_rsa_key; bool is_cdn = false; if (dc_id.is_internal()) { - public_rsa_key = common_public_rsa_key_; + public_rsa_key = PublicRsaKeySharedMain::create(G()->is_test_dc()); } else { auto public_rsa_key_cdn = std::make_shared(dc_id); send_closure_later(public_rsa_key_watchdog_, &PublicRsaKeyWatchdog::add_public_rsa_key, public_rsa_key_cdn); @@ -301,7 +301,6 @@ NetQueryDispatcher::NetQueryDispatcher(const std::function()> &cre LOG(INFO) << tag("main_dc_id", main_dc_id_.load(std::memory_order_relaxed)); delayer_ = create_actor("NetQueryDelayer", create_reference()); dc_auth_manager_ = create_actor("DcAuthManager", create_reference()); - common_public_rsa_key_ = std::make_shared(G()->is_test_dc()); public_rsa_key_watchdog_ = create_actor("PublicRsaKeyWatchdog", create_reference()); sequence_dispatcher_ = MultiSequenceDispatcher::create("MultiSequenceDispatcher"); diff --git a/td/telegram/net/NetQueryDispatcher.h b/td/telegram/net/NetQueryDispatcher.h index 3f72df969..fb3e10d34 100644 --- a/td/telegram/net/NetQueryDispatcher.h +++ b/td/telegram/net/NetQueryDispatcher.h @@ -27,7 +27,6 @@ namespace td { class DcAuthManager; class MultiSequenceDispatcher; class NetQueryDelayer; -class PublicRsaKeySharedMain; class PublicRsaKeyWatchdog; class SessionMultiProxy; @@ -79,7 +78,6 @@ class NetQueryDispatcher { #else std::atomic main_dc_id_{1}; #endif - std::shared_ptr common_public_rsa_key_; ActorOwn public_rsa_key_watchdog_; std::mutex main_dc_id_mutex_; std::shared_ptr td_guard_; diff --git a/td/telegram/net/PublicRsaKeySharedMain.cpp b/td/telegram/net/PublicRsaKeySharedMain.cpp index 5f25a1aa5..779696b81 100644 --- a/td/telegram/net/PublicRsaKeySharedMain.cpp +++ b/td/telegram/net/PublicRsaKeySharedMain.cpp @@ -7,58 +7,57 @@ #include "td/telegram/net/PublicRsaKeySharedMain.h" #include "td/utils/format.h" -#include "td/utils/logging.h" #include "td/utils/Slice.h" #include "td/utils/SliceBuilder.h" -#include - namespace td { -PublicRsaKeySharedMain::PublicRsaKeySharedMain(bool is_test) { - auto add_pem = [this](CSlice 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()) { - auto rsa = r_rsa.move_as_ok(); - auto fingerprint = rsa.get_fingerprint(); - if (get_rsa_key_unsafe(fingerprint) != nullptr) { - return; - } - keys_.push_back(RsaKey{std::move(rsa), fingerprint}); - } +std::shared_ptr PublicRsaKeySharedMain::create(bool is_test) { + auto add_pem = [](vector &keys, CSlice pem) { + auto rsa = mtproto::RSA::from_pem_public_key(pem).move_as_ok(); + auto fingerprint = rsa.get_fingerprint(); + keys.push_back(RsaKey{std::move(rsa), fingerprint}); }; if (is_test) { - add_pem( - "-----BEGIN RSA PUBLIC KEY-----\n" - "MIIBCgKCAQEAyMEdY1aR+sCR3ZSJrtztKTKqigvO/vBfqACJLZtS7QMgCGXJ6XIR\n" - "yy7mx66W0/sOFa7/1mAZtEoIokDP3ShoqF4fVNb6XeqgQfaUHd8wJpDWHcR2OFwv\n" - "plUUI1PLTktZ9uW2WE23b+ixNwJjJGwBDJPQEQFBE+vfmH0JP503wr5INS1poWg/\n" - "j25sIWeYPHYeOrFp/eXaqhISP6G+q2IeTaWTXpwZj4LzXq5YOpk4bYEQ6mvRq7D1\n" - "aHWfYmlEGepfaYR8Q0YqvvhYtMte3ITnuSJs171+GDqpdKcSwHnd6FudwGO4pcCO\n" - "j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB\n" - "-----END RSA PUBLIC KEY-----"); - return; + static auto test_public_rsa_key = [&] { + vector keys; + add_pem(keys, + "-----BEGIN RSA PUBLIC KEY-----\n" + "MIIBCgKCAQEAyMEdY1aR+sCR3ZSJrtztKTKqigvO/vBfqACJLZtS7QMgCGXJ6XIR\n" + "yy7mx66W0/sOFa7/1mAZtEoIokDP3ShoqF4fVNb6XeqgQfaUHd8wJpDWHcR2OFwv\n" + "plUUI1PLTktZ9uW2WE23b+ixNwJjJGwBDJPQEQFBE+vfmH0JP503wr5INS1poWg/\n" + "j25sIWeYPHYeOrFp/eXaqhISP6G+q2IeTaWTXpwZj4LzXq5YOpk4bYEQ6mvRq7D1\n" + "aHWfYmlEGepfaYR8Q0YqvvhYtMte3ITnuSJs171+GDqpdKcSwHnd6FudwGO4pcCO\n" + "j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB\n" + "-----END RSA PUBLIC KEY-----"); + return std::make_shared(std::move(keys)); + }(); + return test_public_rsa_key; + } else { + static auto main_public_rsa_key = [&] { + vector keys; + add_pem(keys, + "-----BEGIN RSA PUBLIC KEY-----\n" + "MIIBCgKCAQEA6LszBcC1LGzyr992NzE0ieY+BSaOW622Aa9Bd4ZHLl+TuFQ4lo4g\n" + "5nKaMBwK/BIb9xUfg0Q29/2mgIR6Zr9krM7HjuIcCzFvDtr+L0GQjae9H0pRB2OO\n" + "62cECs5HKhT5DZ98K33vmWiLowc621dQuwKWSQKjWf50XYFw42h21P2KXUGyp2y/\n" + "+aEyZ+uVgLLQbRA1dEjSDZ2iGRy12Mk5gpYc397aYp438fsJoHIgJ2lgMv5h7WY9\n" + "t6N/byY9Nw9p21Og3AoXSL2q/2IJ1WRUhebgAdGVMlV1fkuOQoEzR7EdpqtQD9Cs\n" + "5+bfo3Nhmcyvk5ftB0WkJ9z6bNZ7yxrP8wIDAQAB\n" + "-----END RSA PUBLIC KEY-----"); + return std::make_shared(std::move(keys)); + }(); + return main_public_rsa_key; } - - add_pem( - "-----BEGIN RSA PUBLIC KEY-----\n" - "MIIBCgKCAQEA6LszBcC1LGzyr992NzE0ieY+BSaOW622Aa9Bd4ZHLl+TuFQ4lo4g\n" - "5nKaMBwK/BIb9xUfg0Q29/2mgIR6Zr9krM7HjuIcCzFvDtr+L0GQjae9H0pRB2OO\n" - "62cECs5HKhT5DZ98K33vmWiLowc621dQuwKWSQKjWf50XYFw42h21P2KXUGyp2y/\n" - "+aEyZ+uVgLLQbRA1dEjSDZ2iGRy12Mk5gpYc397aYp438fsJoHIgJ2lgMv5h7WY9\n" - "t6N/byY9Nw9p21Og3AoXSL2q/2IJ1WRUhebgAdGVMlV1fkuOQoEzR7EdpqtQD9Cs\n" - "5+bfo3Nhmcyvk5ftB0WkJ9z6bNZ7yxrP8wIDAQAB\n" - "-----END RSA PUBLIC KEY-----"); } Result PublicRsaKeySharedMain::get_rsa_key(const vector &fingerprints) { for (auto fingerprint : fingerprints) { - const auto *rsa_key = get_rsa_key_unsafe(fingerprint); - if (rsa_key != nullptr) { - return RsaKey{rsa_key->rsa.clone(), fingerprint}; + for (const auto &key : keys_) { + if (key.fingerprint == fingerprint) { + return RsaKey{key.rsa.clone(), fingerprint}; + } } } return Status::Error(PSLICE() << "Unknown fingerprints " << format::as_array(fingerprints)); @@ -68,13 +67,4 @@ void PublicRsaKeySharedMain::drop_keys() { // nothing to do } -const mtproto::PublicRsaKeyInterface::RsaKey *PublicRsaKeySharedMain::get_rsa_key_unsafe(int64 fingerprint) const { - auto it = std::find_if(keys_.begin(), keys_.end(), - [fingerprint](const auto &value) { return value.fingerprint == fingerprint; }); - if (it == keys_.end()) { - return nullptr; - } - return &*it; -} - } // namespace td diff --git a/td/telegram/net/PublicRsaKeySharedMain.h b/td/telegram/net/PublicRsaKeySharedMain.h index 5b7adcc88..d06f13f26 100644 --- a/td/telegram/net/PublicRsaKeySharedMain.h +++ b/td/telegram/net/PublicRsaKeySharedMain.h @@ -15,7 +15,10 @@ namespace td { class PublicRsaKeySharedMain final : public mtproto::PublicRsaKeyInterface { public: - explicit PublicRsaKeySharedMain(bool is_test); + explicit PublicRsaKeySharedMain(vector &&keys) : keys_(std::move(keys)) { + } + + static std::shared_ptr create(bool is_test); Result get_rsa_key(const vector &fingerprints) final; @@ -23,8 +26,6 @@ class PublicRsaKeySharedMain final : public mtproto::PublicRsaKeyInterface { private: vector keys_; - - const RsaKey *get_rsa_key_unsafe(int64 fingerprint) const; }; } // namespace td diff --git a/test/mtproto.cpp b/test/mtproto.cpp index 342c9b3cc..1ca5fae1f 100644 --- a/test/mtproto.cpp +++ b/test/mtproto.cpp @@ -46,6 +46,8 @@ #include "td/utils/tests.h" #include "td/utils/Time.h" +#include + TEST(Mtproto, GetHostByNameActor) { int threads_n = 1; td::ConcurrentScheduler sched(threads_n, 0); @@ -300,11 +302,11 @@ class HandshakeContext final : public td::mtproto::AuthKeyHandshakeContext { return nullptr; } td::mtproto::PublicRsaKeyInterface *get_public_rsa_key_interface() final { - return &public_rsa_key; + return public_rsa_key_.get(); } private: - td::PublicRsaKeySharedMain public_rsa_key{true}; + std::shared_ptr public_rsa_key_ = td::PublicRsaKeySharedMain::create(true); }; class HandshakeTestActor final : public td::Actor {