From e5c90f239f9b765c8fbc3f773a1f36010a349581 Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Thu, 14 Jun 2018 18:00:10 +0300 Subject: [PATCH] Secret chats: check that his_layer is monotonic GitOrigin-RevId: cb6b50e768a90c5afcece185bea2e7209a9939f5 --- td/telegram/SecretChatActor.cpp | 12 ++++++++++-- td/telegram/SecretChatActor.h | 13 +++++++++++-- td/telegram/logevent/SecretChatEvent.h | 8 ++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/td/telegram/SecretChatActor.cpp b/td/telegram/SecretChatActor.cpp index 70813bdd..d6374b43 100644 --- a/td/telegram/SecretChatActor.cpp +++ b/td/telegram/SecretChatActor.cpp @@ -903,7 +903,7 @@ Status SecretChatActor::do_inbound_message_encrypted(std::unique_ptrdecrypted_message_layer->in_seq_no_; auto out_seq_no = message->decrypted_message_layer->out_seq_no_; - auto status = check_seq_no(in_seq_no, out_seq_no); + auto status = check_seq_no(in_seq_no, out_seq_no, message->his_layer()); if (status.is_error() && status.code() != 2 /* not gap found */) { message->qts_ack.set_value(Unit()); if (message->logevent_id()) { @@ -1136,6 +1139,11 @@ void SecretChatActor::update_seq_no_state(const T &new_seq_no_state) { seq_no_state_.my_in_seq_no = new_seq_no_state.my_in_seq_no; seq_no_state_.my_out_seq_no = new_seq_no_state.my_out_seq_no; + auto new_his_layer = new_seq_no_state.his_layer(); + if (new_his_layer != -1) { + seq_no_state_.his_layer = new_his_layer; + } + if (seq_no_state_.his_in_seq_no != new_seq_no_state.his_in_seq_no) { seq_no_state_.his_in_seq_no = new_seq_no_state.his_in_seq_no; on_his_in_seq_no_updated(); diff --git a/td/telegram/SecretChatActor.h b/td/telegram/SecretChatActor.h index d63459c1..ce096b50 100644 --- a/td/telegram/SecretChatActor.h +++ b/td/telegram/SecretChatActor.h @@ -149,6 +149,7 @@ class SecretChatActor : public NetQueryCallback { int32 my_in_seq_no = 0; int32 my_out_seq_no = 0; int32 his_in_seq_no = 0; + int32 his_layer = 0; int32 resend_end_seq_no = -1; @@ -157,11 +158,12 @@ class SecretChatActor : public NetQueryCallback { } template void store(StorerT &storer) const { - storer.store_int(message_id); + storer.store_int(message_id | HAS_LAYER); storer.store_int(my_in_seq_no); storer.store_int(my_out_seq_no); storer.store_int(his_in_seq_no); storer.store_int(resend_end_seq_no); + storer.store_int(his_layer); } template @@ -171,7 +173,14 @@ class SecretChatActor : public NetQueryCallback { my_out_seq_no = parser.fetch_int(); his_in_seq_no = parser.fetch_int(); resend_end_seq_no = parser.fetch_int(); + + bool has_layer = (message_id & HAS_LAYER) != 0; + if (has_layer) { + message_id &= static_cast(~HAS_LAYER); + his_layer = parser.fetch_int(); + } } + static constexpr uint32 HAS_LAYER = 1u << 31; }; struct ConfigState { @@ -516,7 +525,7 @@ class SecretChatActor : public NetQueryCallback { bool seq_no_state_changed_ = false; int32 last_binlog_message_id_ = -1; - Status check_seq_no(int in_seq_no, int out_seq_no) TD_WARN_UNUSED_RESULT; + Status check_seq_no(int in_seq_no, int out_seq_no, int32 his_layer) TD_WARN_UNUSED_RESULT; void on_his_in_seq_no_updated(); void on_seq_no_state_changed(); diff --git a/td/telegram/logevent/SecretChatEvent.h b/td/telegram/logevent/SecretChatEvent.h index 5b12f7b0..539f9fa1 100644 --- a/td/telegram/logevent/SecretChatEvent.h +++ b/td/telegram/logevent/SecretChatEvent.h @@ -205,6 +205,10 @@ class InboundSecretMessage : public LogEventHelperlayer_; + } + EncryptedFileLocation file; bool has_encrypted_file; @@ -292,6 +296,10 @@ class OutboundSecretMessage : public LogEventHelper