2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/SecretChatActor.h"
|
|
|
|
|
2020-03-16 20:00:44 +01:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/NetQueryCreator.h"
|
|
|
|
#include "td/telegram/secret_api.hpp"
|
2021-10-27 16:32:09 +02:00
|
|
|
#include "td/telegram/ServerMessageId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/telegram_api.hpp"
|
2021-10-27 16:32:09 +02:00
|
|
|
#include "td/telegram/UniqueId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-31 03:23:40 +01:00
|
|
|
#include "td/mtproto/PacketInfo.h"
|
2019-01-31 03:05:40 +01:00
|
|
|
#include "td/mtproto/PacketStorer.h"
|
|
|
|
#include "td/mtproto/Transport.h"
|
|
|
|
#include "td/mtproto/utils.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/db/binlog/BinlogHelper.h"
|
2019-01-06 20:59:17 +01:00
|
|
|
#include "td/db/binlog/BinlogInterface.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/actor/MultiPromise.h"
|
|
|
|
|
2018-12-19 22:57:56 +01:00
|
|
|
#include "td/utils/as.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/crypto.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/ScopeGuard.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2019-02-12 17:48:52 +01:00
|
|
|
#include "td/utils/StorerBase.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
#include "td/utils/tl_parsers.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <tuple>
|
|
|
|
#include <type_traits>
|
|
|
|
|
2019-09-08 02:10:09 +02:00
|
|
|
//#define G GLOBAL_SHOULD_NOT_BE_USED_HERE
|
|
|
|
#undef G
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2018-07-24 00:45:44 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
inline TLObjectStorer<secret_api::Object> create_storer(const secret_api::Object &object) {
|
|
|
|
return TLObjectStorer<secret_api::Object>(object);
|
|
|
|
}
|
2018-07-24 00:45:44 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class SecretImpl {
|
|
|
|
public:
|
|
|
|
explicit SecretImpl(const Storer &data) : data(data) {
|
|
|
|
}
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void do_store(StorerT &storer) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
storer.store_binary(static_cast<int32>(data.size()));
|
|
|
|
storer.store_storer(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Storer &data;
|
|
|
|
};
|
2018-07-24 00:45:44 +02:00
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
SecretChatActor::SecretChatActor(int32 id, unique_ptr<Context> context, bool can_be_empty)
|
2018-12-31 20:04:05 +01:00
|
|
|
: context_(std::move(context)), can_be_empty_(can_be_empty) {
|
|
|
|
auth_state_.id = id;
|
|
|
|
}
|
|
|
|
|
2020-03-15 01:43:42 +01:00
|
|
|
template <class T>
|
|
|
|
NetQueryPtr SecretChatActor::create_net_query(QueryType type, const T &function) {
|
|
|
|
return context_->net_query_creator().create(UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(type)),
|
2022-02-01 17:34:14 +01:00
|
|
|
function, {}, DcId::main(), NetQuery::Type::Common,
|
|
|
|
NetQuery::AuthFlag::On);
|
2020-03-15 01:43:42 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::update_chat(telegram_api::object_ptr<telegram_api::EncryptedChat> chat) {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
check_status(on_update_chat(std::move(chat)));
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:57:26 +02:00
|
|
|
void SecretChatActor::create_chat(UserId user_id, int64 user_access_hash, int32 random_id,
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<SecretChatId> promise) {
|
|
|
|
if (close_flag_) {
|
|
|
|
promise.set_error(Status::Error(400, "Chat is closed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auth_state_.state != State::Empty) {
|
|
|
|
promise.set_error(Status::Error(500, "Bad random_id"));
|
|
|
|
check_status(Status::Error("Unexpected request_chat"));
|
|
|
|
loop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
auto event = make_unique<log_event::CreateSecretChat>();
|
2018-12-31 20:04:05 +01:00
|
|
|
event->user_id = user_id;
|
|
|
|
event->user_access_hash = user_access_hash;
|
|
|
|
event->random_id = random_id;
|
2020-09-22 01:15:09 +02:00
|
|
|
event->set_log_event_id(binlog_add(context_->binlog(), LogEvent::HandlerType::SecretChats, create_storer(*event)));
|
2018-12-31 20:04:05 +01:00
|
|
|
do_create_chat_impl(std::move(event));
|
|
|
|
promise.set_value(SecretChatId(random_id));
|
|
|
|
loop();
|
|
|
|
}
|
2018-07-24 00:45:44 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::on_result_resendable(NetQueryPtr net_query, Promise<NetQueryPtr> promise) {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(INFO) << "In on_result_resendable: " << net_query << " " << close_flag_;
|
2018-07-24 00:45:44 +02:00
|
|
|
if (context_->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto key = UniqueId::extract_key(net_query->id());
|
|
|
|
if (close_flag_) {
|
|
|
|
if (key == static_cast<uint8>(QueryType::DiscardEncryption)) {
|
2021-01-18 17:02:24 +01:00
|
|
|
discard_encryption_promise_.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
check_status([&] {
|
|
|
|
switch (key) {
|
|
|
|
case static_cast<uint8>(QueryType::DhConfig):
|
|
|
|
return on_dh_config(std::move(net_query));
|
|
|
|
case static_cast<uint8>(QueryType::EncryptedChat):
|
|
|
|
return on_update_chat(std::move(net_query));
|
|
|
|
case static_cast<uint8>(QueryType::Message):
|
2018-07-24 00:45:44 +02:00
|
|
|
on_outbound_send_message_result(std::move(net_query), std::move(promise));
|
|
|
|
return Status::OK();
|
2018-04-02 03:14:47 +02:00
|
|
|
case static_cast<uint8>(QueryType::ReadHistory):
|
|
|
|
return on_read_history(std::move(net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
case static_cast<uint8>(QueryType::Ignore):
|
|
|
|
return Status::OK();
|
2021-10-19 17:11:16 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}());
|
|
|
|
|
|
|
|
loop();
|
|
|
|
}
|
2018-07-24 00:45:44 +02:00
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
void SecretChatActor::replay_close_chat(unique_ptr<log_event::CloseSecretChat> event) {
|
2021-01-18 17:02:24 +01:00
|
|
|
do_close_chat_impl(event->delete_history, event->is_already_discarded, event->log_event_id(), Promise<Unit>());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-07-24 00:45:44 +02:00
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
void SecretChatActor::replay_create_chat(unique_ptr<log_event::CreateSecretChat> event) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
do_create_chat_impl(std::move(event));
|
|
|
|
}
|
2018-07-24 00:45:44 +02:00
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
void SecretChatActor::add_inbound_message(unique_ptr<log_event::InboundSecretMessage> message) {
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
if (message) {
|
2020-08-02 21:07:22 +02:00
|
|
|
message->promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auth_state_.state != State::Ready) {
|
|
|
|
LOG(ERROR) << "Ignore unexpected update: " << tag("message", *message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
check_status(do_inbound_message_encrypted(std::move(message)));
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
void SecretChatActor::replay_inbound_message(unique_ptr<log_event::InboundSecretMessage> message) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auth_state_.state != State::Ready) {
|
|
|
|
LOG(ERROR) << "Ignore unexpected replay inbound message: " << tag("message", *message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(!binlog_replay_finish_flag_);
|
|
|
|
CHECK(message->decrypted_message_layer); // from binlog
|
|
|
|
if (message->is_pending) { // wait for gaps?
|
2021-06-08 17:31:29 +02:00
|
|
|
// check_status(do_inbound_message_decrypted_unchecked(std::move(message)), -1);
|
2018-12-31 20:04:05 +01:00
|
|
|
do_inbound_message_decrypted_pending(std::move(message));
|
|
|
|
} else { // just replay
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(message->message_id > last_binlog_message_id_)
|
2018-12-31 20:04:05 +01:00
|
|
|
<< tag("last_binlog_message_id", last_binlog_message_id_) << tag("message_id", message->message_id);
|
|
|
|
last_binlog_message_id_ = message->message_id;
|
|
|
|
check_status(do_inbound_message_decrypted(std::move(message)));
|
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
void SecretChatActor::replay_outbound_message(unique_ptr<log_event::OutboundSecretMessage> message) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auth_state_.state != State::Ready) {
|
|
|
|
LOG(ERROR) << "Ignore unexpected replay outbound message: " << tag("message", *message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(!binlog_replay_finish_flag_);
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(message->message_id > last_binlog_message_id_)
|
2018-12-31 20:04:05 +01:00
|
|
|
<< tag("last_binlog_message_id", last_binlog_message_id_) << tag("message_id", message->message_id);
|
|
|
|
last_binlog_message_id_ = message->message_id;
|
|
|
|
do_outbound_message_impl(std::move(message), Promise<>());
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: my_seq_no is just after message is sent, i.e. my_out_seq_no is already incremented
|
2021-06-08 17:31:29 +02:00
|
|
|
Result<BufferSlice> SecretChatActor::create_encrypted_message(int32 my_in_seq_no, int32 my_out_seq_no,
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<secret_api::DecryptedMessage> &message) {
|
|
|
|
mtproto::AuthKey *auth_key = &pfs_state_.auth_key;
|
|
|
|
auto in_seq_no = my_in_seq_no * 2 + auth_state_.x;
|
|
|
|
auto out_seq_no = my_out_seq_no * 2 - 1 - auth_state_.x;
|
|
|
|
|
2021-06-08 17:31:29 +02:00
|
|
|
auto layer = current_layer();
|
2021-07-19 18:32:56 +02:00
|
|
|
BufferSlice random_bytes(31);
|
2018-12-31 20:04:05 +01:00
|
|
|
Random::secure_bytes(random_bytes.as_slice().ubegin(), random_bytes.size());
|
|
|
|
auto message_with_layer = secret_api::make_object<secret_api::decryptedMessageLayer>(
|
|
|
|
std::move(random_bytes), layer, in_seq_no, out_seq_no, std::move(message));
|
2021-06-08 17:31:29 +02:00
|
|
|
LOG(INFO) << "Create message " << to_string(message_with_layer);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto storer = create_storer(*message_with_layer);
|
|
|
|
auto new_storer = mtproto::PacketStorer<SecretImpl>(storer);
|
|
|
|
mtproto::PacketInfo info;
|
|
|
|
info.type = mtproto::PacketInfo::EndToEnd;
|
2021-06-08 17:31:29 +02:00
|
|
|
info.version = 2;
|
2018-12-31 20:04:05 +01:00
|
|
|
info.is_creator = auth_state_.x == 0;
|
|
|
|
auto packet_writer = BufferWriter{mtproto::Transport::write(new_storer, *auth_key, &info), 0, 0};
|
|
|
|
mtproto::Transport::write(new_storer, *auth_key, &info, packet_writer.as_slice());
|
|
|
|
message = std::move(message_with_layer->message_);
|
|
|
|
return packet_writer.as_buffer_slice();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::send_message(tl_object_ptr<secret_api::DecryptedMessage> message,
|
|
|
|
tl_object_ptr<telegram_api::InputEncryptedFile> file, Promise<> promise) {
|
|
|
|
if (close_flag_) {
|
|
|
|
promise.set_error(Status::Error(400, "Chat is closed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
send_message_impl(std::move(message), std::move(file), SendFlag::External | SendFlag::Push, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::send_message_impl(tl_object_ptr<secret_api::DecryptedMessage> message,
|
|
|
|
tl_object_ptr<telegram_api::InputEncryptedFile> file, int32 flags,
|
|
|
|
Promise<> promise) {
|
|
|
|
if (close_flag_) {
|
|
|
|
promise.set_error(Status::Error(400, "Chat is closed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auth_state_.state != State::Ready) {
|
|
|
|
LOG(ERROR) << "Ignore send_message: " << tag("message", to_string(message)) << tag("file", to_string(file));
|
|
|
|
return promise.set_error(Status::Error(400, "Chat is not accessible"));
|
|
|
|
}
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(binlog_replay_finish_flag_) << "Trying to send message before binlog replay is finished: "
|
|
|
|
<< to_string(*message) << to_string(file);
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 random_id = 0;
|
|
|
|
downcast_call(*message, [&](auto &x) { random_id = x.random_id_; });
|
|
|
|
|
|
|
|
auto it = random_id_to_outbound_message_state_token_.find(random_id);
|
2020-08-09 03:01:30 +02:00
|
|
|
if (it != random_id_to_outbound_message_state_token_.end()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return on_outbound_outer_send_message_promise(it->second, std::move(promise));
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
auto binlog_event = make_unique<log_event::OutboundSecretMessage>();
|
2018-12-31 20:04:05 +01:00
|
|
|
binlog_event->chat_id = auth_state_.id;
|
|
|
|
binlog_event->random_id = random_id;
|
2020-09-22 01:15:09 +02:00
|
|
|
binlog_event->file = log_event::EncryptedInputFile::from_input_encrypted_file(file);
|
2018-12-31 20:04:05 +01:00
|
|
|
binlog_event->message_id = seq_no_state_.message_id + 1;
|
|
|
|
binlog_event->my_in_seq_no = seq_no_state_.my_in_seq_no;
|
|
|
|
binlog_event->my_out_seq_no = seq_no_state_.my_out_seq_no + 1;
|
|
|
|
binlog_event->his_in_seq_no = seq_no_state_.his_in_seq_no;
|
|
|
|
binlog_event->encrypted_message =
|
2021-06-08 17:31:29 +02:00
|
|
|
create_encrypted_message(binlog_event->my_in_seq_no, binlog_event->my_out_seq_no, message).move_as_ok();
|
2020-06-09 13:34:48 +02:00
|
|
|
binlog_event->need_notify_user = (flags & SendFlag::Push) == 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
binlog_event->is_external = (flags & SendFlag::External) != 0;
|
2020-08-21 14:38:23 +02:00
|
|
|
binlog_event->is_silent = (message->get_id() == secret_api::decryptedMessage::ID &&
|
|
|
|
(static_cast<const secret_api::decryptedMessage *>(message.get())->flags_ &
|
|
|
|
secret_api::decryptedMessage::SILENT_MASK) != 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (message->get_id() == secret_api::decryptedMessageService::ID) {
|
|
|
|
binlog_event->is_rewritable = false;
|
|
|
|
auto service_message = move_tl_object_as<secret_api::decryptedMessageService>(message);
|
|
|
|
binlog_event->action = std::move(service_message->action_);
|
|
|
|
} else {
|
|
|
|
binlog_event->is_rewritable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
do_outbound_message_impl(std::move(binlog_event), std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::send_message_action(tl_object_ptr<secret_api::SendMessageAction> action) {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auth_state_.state != State::Ready) {
|
|
|
|
LOG(ERROR) << "Ignore send_message_action: " << tag("message", to_string(action));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool flag = action->get_id() != secret_api::sendMessageCancelAction::ID;
|
2018-03-13 09:08:56 +01:00
|
|
|
|
2020-03-15 01:43:42 +01:00
|
|
|
auto net_query =
|
|
|
|
create_net_query(QueryType::Ignore, telegram_api::messages_setEncryptedTyping(get_input_chat(), flag));
|
2018-03-13 09:08:56 +01:00
|
|
|
if (!set_typing_query_.empty()) {
|
|
|
|
LOG(INFO) << "Cancel previous set typing query";
|
|
|
|
cancel_query(set_typing_query_);
|
|
|
|
}
|
|
|
|
set_typing_query_ = net_query.get_weak();
|
|
|
|
context_->send_net_query(std::move(net_query), actor_shared(this), false);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-06-20 03:02:02 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::send_read_history(int32 date, Promise<> promise) {
|
|
|
|
if (close_flag_) {
|
|
|
|
promise.set_error(Status::Error(400, "Chat is closed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auth_state_.state != State::Ready) {
|
|
|
|
LOG(ERROR) << "Ignore send_read_history: " << tag("date", date);
|
|
|
|
promise.set_error(Status::Error(400, "Can't access the chat"));
|
|
|
|
return;
|
|
|
|
}
|
2018-04-02 03:14:47 +02:00
|
|
|
|
|
|
|
if (date <= last_read_history_date_) {
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read_history_promise_) {
|
|
|
|
LOG(INFO) << "Cancel previous read history request in secret chat " << auth_state_.id;
|
|
|
|
read_history_promise_.set_value(Unit());
|
|
|
|
cancel_query(read_history_query_);
|
|
|
|
}
|
|
|
|
|
2020-03-15 01:43:42 +01:00
|
|
|
auto net_query =
|
|
|
|
create_net_query(QueryType::ReadHistory, telegram_api::messages_readEncryptedHistory(get_input_chat(), date));
|
2018-04-02 03:14:47 +02:00
|
|
|
read_history_query_ = net_query.get_weak();
|
|
|
|
last_read_history_date_ = date;
|
|
|
|
read_history_promise_ = std::move(promise);
|
|
|
|
LOG(INFO) << "Send read history request with date " << date << " in secret chat " << auth_state_.id;
|
|
|
|
context_->send_net_query(std::move(net_query), actor_shared(this), false);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-04-02 03:14:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::send_open_message(int64 random_id, Promise<> promise) {
|
|
|
|
if (close_flag_) {
|
|
|
|
promise.set_error(Status::Error(400, "Chat is closed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auth_state_.state != State::Ready) {
|
|
|
|
promise.set_error(Status::Error(400, "Can't access the chat"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::vector<int64> random_ids{random_id};
|
|
|
|
send_action(make_tl_object<secret_api::decryptedMessageActionReadMessages>(std::move(random_ids)), SendFlag::Push,
|
|
|
|
std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::delete_message(int64 random_id, Promise<> promise) {
|
2018-07-25 15:21:53 +02:00
|
|
|
if (auth_state_.state == State::Closed) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (close_flag_) {
|
|
|
|
promise.set_error(Status::Error(400, "Chat is closed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auth_state_.state != State::Ready) {
|
|
|
|
promise.set_error(Status::Error(400, "Can't access the chat"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return delete_messages(std::vector<int64>{random_id}, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::delete_messages(std::vector<int64> random_ids, Promise<> promise) {
|
2018-07-25 15:21:53 +02:00
|
|
|
if (auth_state_.state == State::Closed) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (close_flag_) {
|
|
|
|
promise.set_error(Status::Error(400, "Chat is closed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auth_state_.state != State::Ready) {
|
|
|
|
promise.set_error(Status::Error(400, "Can't access the chat"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
send_action(make_tl_object<secret_api::decryptedMessageActionDeleteMessages>(std::move(random_ids)), SendFlag::Push,
|
|
|
|
std::move(promise));
|
|
|
|
}
|
|
|
|
void SecretChatActor::delete_all_messages(Promise<> promise) {
|
2018-07-25 15:21:53 +02:00
|
|
|
if (auth_state_.state == State::Closed) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (close_flag_) {
|
|
|
|
promise.set_error(Status::Error(400, "Chat is closed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auth_state_.state != State::Ready) {
|
|
|
|
promise.set_error(Status::Error(400, "Can't access the chat"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
send_action(make_tl_object<secret_api::decryptedMessageActionFlushHistory>(), SendFlag::Push, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::notify_screenshot_taken(Promise<> promise) {
|
|
|
|
if (close_flag_) {
|
|
|
|
promise.set_error(Status::Error(400, "Chat is closed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auth_state_.state != State::Ready) {
|
|
|
|
promise.set_error(Status::Error(400, "Can't access the chat"));
|
|
|
|
return;
|
|
|
|
}
|
2019-11-21 15:53:39 +01:00
|
|
|
send_action(make_tl_object<secret_api::decryptedMessageActionScreenshotMessages>(vector<int64>()), SendFlag::Push,
|
2018-12-31 20:04:05 +01:00
|
|
|
std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::send_set_ttl_message(int32 ttl, int64 random_id, Promise<> promise) {
|
|
|
|
if (close_flag_) {
|
|
|
|
promise.set_error(Status::Error(400, "Chat is closed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auth_state_.state != State::Ready) {
|
|
|
|
promise.set_error(Status::Error(400, "Can't access the chat"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
send_message_impl(secret_api::make_object<secret_api::decryptedMessageService>(
|
|
|
|
random_id, make_tl_object<secret_api::decryptedMessageActionSetMessageTTL>(ttl)),
|
|
|
|
nullptr, SendFlag::External | SendFlag::Push, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::send_action(tl_object_ptr<secret_api::DecryptedMessageAction> action, int32 flags,
|
|
|
|
Promise<> promise) {
|
|
|
|
send_message_impl(
|
|
|
|
secret_api::make_object<secret_api::decryptedMessageService>(Random::secure_int64(), std::move(action)), nullptr,
|
|
|
|
flags, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::binlog_replay_finish() {
|
|
|
|
on_his_in_seq_no_updated();
|
2019-05-13 18:34:23 +02:00
|
|
|
LOG(INFO) << "Binlog replay is finished with SeqNoState " << seq_no_state_;
|
|
|
|
LOG(INFO) << "Binlog replay is finished with PfsState " << pfs_state_;
|
2018-12-31 20:04:05 +01:00
|
|
|
binlog_replay_finish_flag_ = true;
|
|
|
|
if (auth_state_.state == State::Ready) {
|
2021-07-05 18:17:26 +02:00
|
|
|
auto my_layer = static_cast<int32>(SecretChatLayer::Current);
|
2021-07-05 04:42:37 +02:00
|
|
|
if (config_state_.my_layer < my_layer) {
|
|
|
|
send_action(secret_api::make_object<secret_api::decryptedMessageActionNotifyLayer>(my_layer), SendFlag::None,
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::loop() {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!binlog_replay_finish_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
check_status(do_loop());
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SecretChatActor::do_loop() {
|
|
|
|
TRY_STATUS(run_auth());
|
|
|
|
run_pfs();
|
|
|
|
run_fill_gaps();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_send_message_ack(int64 random_id) {
|
|
|
|
context_->on_send_message_ack(random_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SecretChatActor::on_delete_messages(const std::vector<int64> &random_ids) {
|
|
|
|
for (auto random_id : random_ids) {
|
|
|
|
auto it = random_id_to_outbound_message_state_token_.find(random_id);
|
|
|
|
if (it == random_id_to_outbound_message_state_token_.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto state_id = it->second;
|
|
|
|
TRY_STATUS(outbound_rewrite_with_empty(state_id));
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SecretChatActor::on_flush_history(int32 last_message_id) {
|
|
|
|
std::vector<uint64> to_rewrite;
|
|
|
|
outbound_message_states_.for_each([&](auto state_id, auto &state) {
|
|
|
|
if (state.message->message_id < last_message_id && state.message->is_rewritable) {
|
|
|
|
to_rewrite.push_back(state_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for (auto state_id : to_rewrite) {
|
|
|
|
TRY_STATUS(outbound_rewrite_with_empty(state_id));
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SecretChatActor::run_auth() {
|
|
|
|
switch (auth_state_.state) {
|
|
|
|
case State::Empty:
|
|
|
|
return Status::OK();
|
|
|
|
case State::SendRequest: {
|
|
|
|
if (!auth_state_.handshake.has_config()) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
// messages.requestEncryption#f64daf43 user_id:InputUser random_id:int g_a:bytes = EncryptedChat;
|
2020-03-15 01:43:42 +01:00
|
|
|
auto query = create_net_query(QueryType::EncryptedChat, telegram_api::messages_requestEncryption(
|
|
|
|
get_input_user(), auth_state_.random_id,
|
|
|
|
BufferSlice(auth_state_.handshake.get_g_b())));
|
2018-12-31 20:04:05 +01:00
|
|
|
context_->send_net_query(std::move(query), actor_shared(this), false);
|
|
|
|
auth_state_.state = State::WaitRequestResponse;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
case State::SendAccept: {
|
|
|
|
if (!auth_state_.handshake.has_config()) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-08-09 22:41:01 +02:00
|
|
|
TRY_STATUS(auth_state_.handshake.run_checks(true, context_->dh_callback()));
|
2018-12-31 20:04:05 +01:00
|
|
|
auto id_and_key = auth_state_.handshake.gen_key();
|
|
|
|
pfs_state_.auth_key = mtproto::AuthKey(id_and_key.first, std::move(id_and_key.second));
|
|
|
|
calc_key_hash();
|
2019-11-21 15:53:39 +01:00
|
|
|
// messages.acceptEncryption#3dbc0415 peer:InputEncryptedChat g_b:bytes key_fingerprint:long = EncryptedChat;
|
2020-03-15 01:43:42 +01:00
|
|
|
auto query = create_net_query(
|
|
|
|
QueryType::EncryptedChat,
|
|
|
|
telegram_api::messages_acceptEncryption(get_input_chat(), BufferSlice(auth_state_.handshake.get_g_b()),
|
|
|
|
pfs_state_.auth_key.id()));
|
2018-12-31 20:04:05 +01:00
|
|
|
context_->send_net_query(std::move(query), actor_shared(this), false);
|
|
|
|
auth_state_.state = State::WaitAcceptResponse;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::run_fill_gaps() {
|
|
|
|
// replay messages
|
|
|
|
while (true) {
|
|
|
|
if (pending_inbound_messages_.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto begin = pending_inbound_messages_.begin();
|
|
|
|
auto next_seq_no = begin->first;
|
|
|
|
if (next_seq_no <= seq_no_state_.my_in_seq_no) {
|
|
|
|
LOG(INFO) << "Replay pending event: " << tag("seq_no", next_seq_no);
|
|
|
|
auto message = std::move(begin->second);
|
|
|
|
pending_inbound_messages_.erase(begin);
|
2021-06-08 17:31:29 +02:00
|
|
|
check_status(do_inbound_message_decrypted_unchecked(std::move(message), -1));
|
2022-05-01 22:03:06 +02:00
|
|
|
CHECK(pending_inbound_messages_.count(next_seq_no) == 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pending_inbound_messages_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto start_seq_no = seq_no_state_.my_in_seq_no;
|
|
|
|
auto finish_seq_no = pending_inbound_messages_.begin()->first - 1;
|
|
|
|
LOG(INFO) << tag("start_seq_no", start_seq_no) << tag("finish_seq_no", finish_seq_no)
|
|
|
|
<< tag("resend_end_seq_no", seq_no_state_.resend_end_seq_no);
|
|
|
|
CHECK(start_seq_no <= finish_seq_no);
|
|
|
|
if (seq_no_state_.resend_end_seq_no >= finish_seq_no) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(seq_no_state_.resend_end_seq_no < start_seq_no);
|
|
|
|
|
|
|
|
start_seq_no = start_seq_no * 2 + auth_state_.x;
|
|
|
|
finish_seq_no = finish_seq_no * 2 + auth_state_.x;
|
|
|
|
|
|
|
|
send_action(secret_api::make_object<secret_api::decryptedMessageActionResend>(start_seq_no, finish_seq_no),
|
|
|
|
SendFlag::None, Promise<>());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::run_pfs() {
|
|
|
|
while (true) {
|
2021-03-15 01:32:14 +01:00
|
|
|
LOG(INFO) << "Run PFS loop: " << pfs_state_;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (pfs_state_.state == PfsState::Empty &&
|
|
|
|
(pfs_state_.last_message_id + 100 < seq_no_state_.message_id ||
|
|
|
|
pfs_state_.last_timestamp + 60 * 60 * 24 * 7 < Time::now()) &&
|
|
|
|
pfs_state_.other_auth_key.empty()) {
|
|
|
|
LOG(INFO) << "Request new key";
|
|
|
|
request_new_key();
|
|
|
|
}
|
|
|
|
switch (pfs_state_.state) {
|
|
|
|
case PfsState::SendRequest: {
|
|
|
|
// shouldn't wait, pfs_state is already saved explicitly
|
|
|
|
pfs_state_.state = PfsState::WaitSendRequest; // don't save it!
|
|
|
|
send_action(secret_api::make_object<secret_api::decryptedMessageActionRequestKey>(
|
|
|
|
pfs_state_.exchange_id, BufferSlice(pfs_state_.handshake.get_g_b())),
|
|
|
|
SendFlag::None, Promise<>());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PfsState::SendCommit: {
|
2023-01-02 13:38:04 +01:00
|
|
|
// must wait till pfs_state is saved to binlog. Otherwise, we may save ActionCommit to binlog without pfs_state,
|
2018-12-31 20:04:05 +01:00
|
|
|
// which has the new auth_key.
|
|
|
|
if (saved_pfs_state_message_id_ < pfs_state_.wait_message_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: wait till gaps are filled???
|
|
|
|
pfs_state_.state = PfsState::WaitSendCommit; // don't save it
|
|
|
|
send_action(secret_api::make_object<secret_api::decryptedMessageActionCommitKey>(
|
|
|
|
pfs_state_.exchange_id, static_cast<int64>(pfs_state_.other_auth_key.id())),
|
|
|
|
SendFlag::None, Promise<>());
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PfsState::SendAccept: {
|
|
|
|
if (saved_pfs_state_message_id_ < pfs_state_.wait_message_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pfs_state_.state = PfsState::WaitSendAccept; // don't save it
|
|
|
|
send_action(secret_api::make_object<secret_api::decryptedMessageActionAcceptKey>(
|
|
|
|
pfs_state_.exchange_id, BufferSlice(pfs_state_.handshake.get_g_b()),
|
|
|
|
static_cast<int64>(pfs_state_.other_auth_key.id())),
|
|
|
|
SendFlag::None, Promise<>());
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::check_status(Status status) {
|
|
|
|
if (status.is_error()) {
|
|
|
|
if (status.code() == 1) {
|
|
|
|
LOG(WARNING) << "Non-fatal error: " << status;
|
|
|
|
} else {
|
2022-06-09 18:10:38 +02:00
|
|
|
on_fatal_error(std::move(status), false);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 18:10:38 +02:00
|
|
|
void SecretChatActor::on_fatal_error(Status status, bool is_expected) {
|
|
|
|
if (!is_expected) {
|
|
|
|
LOG(ERROR) << "Fatal error: " << status;
|
|
|
|
}
|
2021-01-18 17:02:24 +01:00
|
|
|
cancel_chat(false, false, Promise<>());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:02:24 +01:00
|
|
|
void SecretChatActor::cancel_chat(bool delete_history, bool is_already_discarded, Promise<> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (close_flag_) {
|
2018-07-25 15:21:53 +02:00
|
|
|
promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
close_flag_ = true;
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
std::vector<log_event::LogEvent::Id> to_delete;
|
2018-12-31 20:04:05 +01:00
|
|
|
outbound_message_states_.for_each(
|
2020-09-22 01:15:09 +02:00
|
|
|
[&](auto state_id, auto &state) { to_delete.push_back(state.message->log_event_id()); });
|
|
|
|
inbound_message_states_.for_each([&](auto state_id, auto &state) { to_delete.push_back(state.log_event_id); });
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// TODO: It must be a transaction
|
|
|
|
for (auto id : to_delete) {
|
2018-06-28 15:52:40 +02:00
|
|
|
binlog_erase(context_->binlog(), id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-09-22 01:15:09 +02:00
|
|
|
if (create_log_event_id_ != 0) {
|
|
|
|
binlog_erase(context_->binlog(), create_log_event_id_);
|
|
|
|
create_log_event_id_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
auto event = make_unique<log_event::CloseSecretChat>();
|
2018-12-31 20:04:05 +01:00
|
|
|
event->chat_id = auth_state_.id;
|
2021-01-18 13:04:31 +01:00
|
|
|
auto log_event_id = binlog_add(context_->binlog(), LogEvent::HandlerType::SecretChats, create_storer(*event));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-01-18 17:02:24 +01:00
|
|
|
auto on_sync = PromiseCreator::lambda([actor_id = actor_id(this), delete_history, is_already_discarded, log_event_id,
|
2021-01-18 13:04:31 +01:00
|
|
|
promise = std::move(promise)](Result<Unit> result) mutable {
|
|
|
|
if (result.is_ok()) {
|
2021-01-18 17:02:24 +01:00
|
|
|
send_closure(actor_id, &SecretChatActor::do_close_chat_impl, delete_history, is_already_discarded, log_event_id,
|
|
|
|
std::move(promise));
|
2021-01-18 13:04:31 +01:00
|
|
|
} else {
|
|
|
|
promise.set_error(result.error().clone());
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_promise_error, result.move_as_error(), "cancel_chat");
|
|
|
|
}
|
|
|
|
});
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
context_->binlog()->force_sync(std::move(on_sync));
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:02:24 +01:00
|
|
|
void SecretChatActor::do_close_chat_impl(bool delete_history, bool is_already_discarded, uint64 log_event_id,
|
|
|
|
Promise<Unit> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
close_flag_ = true;
|
2018-07-24 11:43:37 +02:00
|
|
|
auth_state_.state = State::Closed;
|
|
|
|
context_->secret_chat_db()->set_value(auth_state_);
|
2018-12-31 20:04:05 +01:00
|
|
|
context_->secret_chat_db()->erase_value(config_state_);
|
|
|
|
context_->secret_chat_db()->erase_value(pfs_state_);
|
|
|
|
context_->secret_chat_db()->erase_value(seq_no_state_);
|
2021-01-18 17:02:24 +01:00
|
|
|
|
2021-10-27 14:38:09 +02:00
|
|
|
MultiPromiseActorSafe mpas{"CloseSecretChatMultiPromiseActor"};
|
2021-01-18 17:02:24 +01:00
|
|
|
mpas.add_promise(
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), log_event_id, promise = std::move(promise)](Unit) mutable {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_closed, log_event_id, std::move(promise));
|
|
|
|
}));
|
|
|
|
|
|
|
|
auto lock = mpas.get_promise();
|
|
|
|
|
2021-01-18 13:04:31 +01:00
|
|
|
if (delete_history) {
|
2021-01-20 12:49:18 +01:00
|
|
|
context_->on_flush_history(true, MessageId::max(), mpas.get_promise());
|
2021-01-18 13:04:31 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
send_update_secret_chat();
|
|
|
|
|
2021-01-18 17:02:24 +01:00
|
|
|
if (!is_already_discarded) {
|
|
|
|
int32 flags = 0;
|
|
|
|
if (delete_history) {
|
|
|
|
flags |= telegram_api::messages_discardEncryption::DELETE_HISTORY_MASK;
|
|
|
|
}
|
|
|
|
auto query = create_net_query(QueryType::DiscardEncryption,
|
|
|
|
telegram_api::messages_discardEncryption(flags, false /*ignored*/, auth_state_.id));
|
|
|
|
query->total_timeout_limit_ = 60 * 60 * 24 * 365;
|
|
|
|
context_->send_net_query(std::move(query), actor_shared(this), true);
|
|
|
|
discard_encryption_promise_ = mpas.get_promise();
|
|
|
|
}
|
|
|
|
|
|
|
|
lock.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_closed(uint64 log_event_id, Promise<Unit> &&promise) {
|
|
|
|
CHECK(close_flag_);
|
|
|
|
if (context_->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Finish closing";
|
|
|
|
context_->secret_chat_db()->erase_value(auth_state_);
|
|
|
|
binlog_erase(context_->binlog(), log_event_id);
|
|
|
|
promise.set_value(Unit());
|
|
|
|
// skip flush
|
|
|
|
stop();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
void SecretChatActor::do_create_chat_impl(unique_ptr<log_event::CreateSecretChat> event) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << *event;
|
|
|
|
CHECK(event->random_id == auth_state_.id);
|
2020-09-22 01:15:09 +02:00
|
|
|
create_log_event_id_ = event->log_event_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (auth_state_.state == State::Empty) {
|
|
|
|
auth_state_.user_id = event->user_id;
|
|
|
|
auth_state_.user_access_hash = event->user_access_hash;
|
|
|
|
auth_state_.random_id = event->random_id;
|
|
|
|
auth_state_.state = State::SendRequest;
|
|
|
|
auth_state_.x = 0;
|
|
|
|
auth_state_.date = context_->unix_time();
|
|
|
|
send_update_secret_chat();
|
|
|
|
} else if (auth_state_.state == State::SendRequest) {
|
|
|
|
} else if (auth_state_.state == State::WaitRequestResponse) {
|
|
|
|
} else {
|
2020-09-22 01:15:09 +02:00
|
|
|
binlog_erase(context_->binlog(), create_log_event_id_);
|
|
|
|
create_log_event_id_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
telegram_api::object_ptr<telegram_api::inputUser> SecretChatActor::get_input_user() {
|
2021-03-29 10:57:26 +02:00
|
|
|
return telegram_api::make_object<telegram_api::inputUser>(auth_state_.user_id.get(), auth_state_.user_access_hash);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
telegram_api::object_ptr<telegram_api::inputEncryptedChat> SecretChatActor::get_input_chat() {
|
|
|
|
return telegram_api::make_object<telegram_api::inputEncryptedChat>(auth_state_.id, auth_state_.access_hash);
|
|
|
|
}
|
|
|
|
void SecretChatActor::tear_down() {
|
|
|
|
LOG(INFO) << "SecretChatActor: tear_down";
|
|
|
|
// TODO notify send update that we are dead
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<std::tuple<uint64, BufferSlice, int32>> SecretChatActor::decrypt(BufferSlice &encrypted_message) {
|
|
|
|
MutableSlice data = encrypted_message.as_slice();
|
|
|
|
CHECK(is_aligned_pointer<4>(data.data()));
|
|
|
|
TRY_RESULT(auth_key_id, mtproto::Transport::read_auth_key_id(data));
|
|
|
|
mtproto::AuthKey *auth_key = nullptr;
|
|
|
|
if (auth_key_id == pfs_state_.auth_key.id()) {
|
|
|
|
auth_key = &pfs_state_.auth_key;
|
|
|
|
} else if (auth_key_id == pfs_state_.other_auth_key.id()) {
|
|
|
|
auth_key = &pfs_state_.other_auth_key;
|
|
|
|
} else {
|
|
|
|
return Status::Error(1, PSLICE() << "Unknown " << tag("auth_key_id", format::as_hex(auth_key_id))
|
|
|
|
<< tag("crc", crc64(encrypted_message.as_slice())));
|
|
|
|
}
|
|
|
|
|
2021-07-02 02:25:09 +02:00
|
|
|
std::array<int, 2> versions{{2, 1}};
|
2018-12-31 20:04:05 +01:00
|
|
|
BufferSlice encrypted_message_copy;
|
|
|
|
int32 mtproto_version = -1;
|
2018-06-17 19:52:40 +02:00
|
|
|
Result<mtproto::Transport::ReadResult> r_read_result;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (size_t i = 0; i < versions.size(); i++) {
|
|
|
|
encrypted_message_copy = encrypted_message.copy();
|
|
|
|
data = encrypted_message_copy.as_slice();
|
|
|
|
CHECK(is_aligned_pointer<4>(data.data()));
|
|
|
|
|
|
|
|
mtproto::PacketInfo info;
|
|
|
|
info.type = mtproto::PacketInfo::EndToEnd;
|
|
|
|
mtproto_version = versions[i];
|
|
|
|
info.version = mtproto_version;
|
|
|
|
info.is_creator = auth_state_.x == 0;
|
2018-06-17 19:52:40 +02:00
|
|
|
r_read_result = mtproto::Transport::read(data, *auth_key, &info);
|
2021-07-02 02:25:09 +02:00
|
|
|
if (i + 1 != versions.size() && r_read_result.is_error()) {
|
2021-07-19 20:00:41 +02:00
|
|
|
if (config_state_.his_layer >= static_cast<int32>(SecretChatLayer::Mtproto2)) {
|
|
|
|
LOG(WARNING) << tag("mtproto", mtproto_version) << " decryption failed " << r_read_result.error();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-06-17 19:52:40 +02:00
|
|
|
TRY_RESULT(read_result, std::move(r_read_result));
|
|
|
|
switch (read_result.type()) {
|
2018-09-05 20:07:50 +02:00
|
|
|
case mtproto::Transport::ReadResult::Quickack:
|
2018-06-17 19:52:40 +02:00
|
|
|
return Status::Error("Got quickack instead of a message");
|
2018-09-05 20:07:50 +02:00
|
|
|
case mtproto::Transport::ReadResult::Error:
|
2021-06-08 17:31:29 +02:00
|
|
|
return Status::Error(PSLICE() << "Got MTProto error code instead of a message: " << read_result.error());
|
2018-09-05 20:07:50 +02:00
|
|
|
case mtproto::Transport::ReadResult::Nop:
|
2018-06-17 19:52:40 +02:00
|
|
|
return Status::Error("Got nop instead of a message");
|
2018-09-05 20:07:50 +02:00
|
|
|
case mtproto::Transport::ReadResult::Packet:
|
2018-06-17 19:52:40 +02:00
|
|
|
data = read_result.packet();
|
|
|
|
break;
|
2018-09-05 20:07:50 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-06-17 19:52:40 +02:00
|
|
|
|
2018-12-19 23:46:55 +01:00
|
|
|
int32 len = as<int32>(data.begin());
|
2018-12-31 20:04:05 +01:00
|
|
|
data = data.substr(4, len);
|
|
|
|
if (!is_aligned_pointer<4>(data.data())) {
|
|
|
|
return std::make_tuple(auth_key_id, BufferSlice(data), mtproto_version);
|
|
|
|
} else {
|
|
|
|
return std::make_tuple(auth_key_id, encrypted_message_copy.from_slice(data), mtproto_version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
Status SecretChatActor::do_inbound_message_encrypted(unique_ptr<log_event::InboundSecretMessage> message) {
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
if (message) {
|
2020-08-02 21:07:22 +02:00
|
|
|
message->promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
TRY_RESULT(decrypted, decrypt(message->encrypted_message));
|
|
|
|
auto auth_key_id = std::get<0>(decrypted);
|
|
|
|
auto data_buffer = std::move(std::get<1>(decrypted));
|
|
|
|
auto mtproto_version = std::get<2>(decrypted);
|
|
|
|
message->auth_key_id = auth_key_id;
|
|
|
|
|
|
|
|
TlBufferParser parser(&data_buffer);
|
|
|
|
auto id = parser.fetch_int();
|
|
|
|
Status status;
|
|
|
|
if (id == secret_api::decryptedMessageLayer::ID) {
|
|
|
|
auto message_with_layer = secret_api::decryptedMessageLayer::fetch(parser);
|
2019-07-11 02:12:27 +02:00
|
|
|
parser.fetch_end();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!parser.get_error()) {
|
|
|
|
auto layer = message_with_layer->layer_;
|
2021-07-05 18:17:26 +02:00
|
|
|
if (layer < static_cast<int32>(SecretChatLayer::Default) &&
|
2021-07-05 04:42:37 +02:00
|
|
|
false /* old Android app could send such messages */) {
|
2020-03-30 23:55:00 +02:00
|
|
|
LOG(ERROR) << "Layer " << layer << " is not supported, drop message " << to_string(message_with_layer);
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
if (config_state_.his_layer < layer) {
|
|
|
|
config_state_.his_layer = layer;
|
|
|
|
context_->secret_chat_db()->set_value(config_state_);
|
|
|
|
send_update_secret_chat();
|
|
|
|
}
|
2021-07-05 18:17:26 +02:00
|
|
|
if (layer >= static_cast<int32>(SecretChatLayer::Mtproto2) && mtproto_version < 2) {
|
2021-10-06 12:48:29 +02:00
|
|
|
return Status::Error("MTProto 1.0 encryption is forbidden for this layer");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (message_with_layer->in_seq_no_ < 0) {
|
|
|
|
return Status::Error(PSLICE() << "Invalid seq_no: " << to_string(message_with_layer));
|
|
|
|
}
|
|
|
|
message->decrypted_message_layer = std::move(message_with_layer);
|
2021-06-08 17:31:29 +02:00
|
|
|
return do_inbound_message_decrypted_unchecked(std::move(message), mtproto_version);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
status = Status::Error(PSLICE() << parser.get_error() << format::as_hex_dump<4>(data_buffer.as_slice()));
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-30 23:55:00 +02:00
|
|
|
status = Status::Error(PSLICE() << "Unknown constructor " << format::as_hex(id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// support for older layer
|
2020-08-02 00:40:48 +02:00
|
|
|
LOG(WARNING) << "Failed to fetch update: " << status;
|
2021-07-05 04:42:37 +02:00
|
|
|
send_action(secret_api::make_object<secret_api::decryptedMessageActionNotifyLayer>(
|
2021-07-05 18:17:26 +02:00
|
|
|
static_cast<int32>(SecretChatLayer::Current)),
|
2021-07-05 04:42:37 +02:00
|
|
|
SendFlag::None, Promise<>());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (config_state_.his_layer == 8) {
|
|
|
|
TlBufferParser new_parser(&data_buffer);
|
|
|
|
auto message_without_layer = secret_api::DecryptedMessage::fetch(new_parser);
|
2019-07-11 02:12:27 +02:00
|
|
|
parser.fetch_end();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!new_parser.get_error()) {
|
|
|
|
message->decrypted_message_layer = secret_api::make_object<secret_api::decryptedMessageLayer>(
|
|
|
|
BufferSlice(), config_state_.his_layer, -1, -1, std::move(message_without_layer));
|
2021-06-08 17:31:29 +02:00
|
|
|
return do_inbound_message_decrypted_unchecked(std::move(message), mtproto_version);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
LOG(ERROR) << "Failed to fetch update (DecryptedMessage): " << new_parser.get_error()
|
|
|
|
<< format::as_hex_dump<4>(data_buffer.as_slice());
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-06-14 17:00:10 +02:00
|
|
|
Status SecretChatActor::check_seq_no(int in_seq_no, int out_seq_no, int32 his_layer) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (in_seq_no < 0) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
if (in_seq_no % 2 != (1 - auth_state_.x) || out_seq_no % 2 != auth_state_.x) {
|
|
|
|
return Status::Error("Bad seq_no parity");
|
|
|
|
}
|
|
|
|
in_seq_no /= 2;
|
|
|
|
out_seq_no /= 2;
|
|
|
|
if (out_seq_no < seq_no_state_.my_in_seq_no) {
|
|
|
|
return Status::Error(1, "Old seq_no");
|
|
|
|
}
|
|
|
|
if (out_seq_no > seq_no_state_.my_in_seq_no) {
|
|
|
|
return Status::Error(2, "Gap found!");
|
|
|
|
}
|
|
|
|
if (in_seq_no < seq_no_state_.his_in_seq_no) {
|
|
|
|
return Status::Error("in_seq_no is not monotonic");
|
|
|
|
}
|
|
|
|
if (seq_no_state_.my_out_seq_no < in_seq_no) {
|
|
|
|
return Status::Error("in_seq_no is bigger than seq_no_state_.my_out_seq_no");
|
|
|
|
}
|
2018-06-14 17:00:10 +02:00
|
|
|
if (his_layer < seq_no_state_.his_layer) {
|
|
|
|
return Status::Error("his_layer is not monotonic");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2021-06-08 17:31:29 +02:00
|
|
|
Status SecretChatActor::do_inbound_message_decrypted_unchecked(unique_ptr<log_event::InboundSecretMessage> message,
|
|
|
|
int32 mtproto_version) {
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
2020-08-02 21:07:22 +02:00
|
|
|
CHECK(message == nullptr || !message->promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
auto in_seq_no = message->decrypted_message_layer->in_seq_no_;
|
|
|
|
auto out_seq_no = message->decrypted_message_layer->out_seq_no_;
|
2018-06-14 17:00:10 +02:00
|
|
|
auto status = check_seq_no(in_seq_no, out_seq_no, message->his_layer());
|
2018-12-31 20:04:05 +01:00
|
|
|
if (status.is_error() && status.code() != 2 /* not gap found */) {
|
2020-08-02 21:07:22 +02:00
|
|
|
message->promise.set_value(Unit());
|
2020-09-22 01:15:09 +02:00
|
|
|
if (message->log_event_id()) {
|
|
|
|
LOG(INFO) << "Erase binlog event: " << tag("log_event_id", message->log_event_id());
|
|
|
|
binlog_erase(context_->binlog(), message->log_event_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
auto warning_message = PSTRING() << status << tag("seq_no_state_.my_in_seq_no", seq_no_state_.my_in_seq_no)
|
|
|
|
<< tag("seq_no_state_.my_out_seq_no", seq_no_state_.my_out_seq_no)
|
|
|
|
<< tag("seq_no_state_.his_in_seq_no", seq_no_state_.his_in_seq_no)
|
|
|
|
<< tag("in_seq_no", in_seq_no) << tag("out_seq_no", out_seq_no)
|
|
|
|
<< to_string(message->decrypted_message_layer);
|
|
|
|
if (status.code()) {
|
|
|
|
LOG(WARNING) << warning_message;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << warning_message;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2022-05-28 22:44:41 +02:00
|
|
|
LOG(INFO) << "Receive message encrypted with MTProto " << mtproto_version << ": "
|
|
|
|
<< to_string(message->decrypted_message_layer);
|
|
|
|
|
2018-07-24 19:14:04 +02:00
|
|
|
if (message->decrypted_message_layer->message_->get_id() == secret_api::decryptedMessageService8::ID) {
|
|
|
|
auto old = move_tl_object_as<secret_api::decryptedMessageService8>(message->decrypted_message_layer->message_);
|
|
|
|
message->decrypted_message_layer->message_ =
|
|
|
|
secret_api::make_object<secret_api::decryptedMessageService>(old->random_id_, std::move(old->action_));
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
// Process ActionResend.
|
|
|
|
if (message->decrypted_message_layer->message_->get_id() == secret_api::decryptedMessageService::ID) {
|
|
|
|
auto *decrypted_message_service =
|
|
|
|
static_cast<secret_api::decryptedMessageService *>(message->decrypted_message_layer->message_.get());
|
|
|
|
if (decrypted_message_service->action_->get_id() == secret_api::decryptedMessageActionResend::ID) {
|
|
|
|
auto *action_resend =
|
|
|
|
static_cast<secret_api::decryptedMessageActionResend *>(decrypted_message_service->action_.get());
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
auto start_seq_no = static_cast<uint32>(action_resend->start_seq_no_ / 2);
|
|
|
|
auto finish_seq_no = static_cast<uint32>(action_resend->end_seq_no_ / 2);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (start_seq_no + MAX_RESEND_COUNT < finish_seq_no) {
|
2020-08-02 21:07:22 +02:00
|
|
|
message->promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error(PSLICE() << "Won't resend more than " << MAX_RESEND_COUNT << " messages");
|
|
|
|
}
|
|
|
|
LOG(INFO) << "ActionResend: " << tag("start", start_seq_no) << tag("finish_seq_no", finish_seq_no);
|
|
|
|
for (auto seq_no = start_seq_no; seq_no <= finish_seq_no; seq_no++) {
|
|
|
|
auto it = out_seq_no_to_outbound_message_state_token_.find(seq_no);
|
|
|
|
if (it == out_seq_no_to_outbound_message_state_token_.end()) {
|
2020-08-02 21:07:22 +02:00
|
|
|
message->promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error(PSLICE() << "Can't resend query " << tag("seq_no", seq_no));
|
|
|
|
}
|
|
|
|
auto state_id = it->second;
|
|
|
|
outbound_resend(state_id);
|
|
|
|
}
|
|
|
|
// It is ok to replace action with Noop, because it won't be written to binlog before message is marked unsent
|
|
|
|
decrypted_message_service->action_ = secret_api::make_object<secret_api::decryptedMessageActionNoop>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status.is_error()) {
|
|
|
|
CHECK(status.code() == 2); // gap found
|
|
|
|
do_inbound_message_decrypted_pending(std::move(message));
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
message->message_id = seq_no_state_.message_id + 1;
|
|
|
|
if (in_seq_no != -1) {
|
|
|
|
message->my_in_seq_no = out_seq_no / 2 + 1;
|
|
|
|
message->my_out_seq_no = seq_no_state_.my_out_seq_no;
|
|
|
|
message->his_in_seq_no = in_seq_no / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return do_inbound_message_decrypted(std::move(message));
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
void SecretChatActor::do_outbound_message_impl(unique_ptr<log_event::OutboundSecretMessage> binlog_event,
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<> promise) {
|
|
|
|
binlog_event->crc = crc64(binlog_event->encrypted_message.as_slice());
|
|
|
|
LOG(INFO) << "Do outbound message: " << *binlog_event << tag("crc", binlog_event->crc);
|
|
|
|
auto &state_id_ref = random_id_to_outbound_message_state_token_[binlog_event->random_id];
|
2021-03-15 01:32:14 +01:00
|
|
|
LOG_CHECK(state_id_ref == 0) << "Random ID collision";
|
2018-12-31 20:04:05 +01:00
|
|
|
state_id_ref = outbound_message_states_.create();
|
|
|
|
const uint64 state_id = state_id_ref;
|
|
|
|
auto *state = outbound_message_states_.get(state_id);
|
|
|
|
LOG(INFO) << tag("state_id", state_id);
|
|
|
|
CHECK(state);
|
|
|
|
state->message = std::move(binlog_event);
|
|
|
|
|
|
|
|
// OutboundSecretMessage
|
|
|
|
//
|
2020-09-22 01:15:09 +02:00
|
|
|
// 1. [] => Save log_event. [save_log_event]
|
|
|
|
// 2. [save_log_event] => Save SeqNoState [save_changes]
|
|
|
|
// 3. [save_log_event] => Send NetQuery [send_message]
|
2018-12-31 20:04:05 +01:00
|
|
|
// Note: we have to force binlog to flush
|
|
|
|
// 4.0 [send_message]:Fail => rewrite
|
2020-09-22 01:15:09 +02:00
|
|
|
// 4. [save_changes; send_message] => Mark log event as sent [rewrite_log_event]
|
|
|
|
// 5. [save_changes; send_message; ack] => [remove_log_event]
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto message = state->message.get();
|
|
|
|
|
|
|
|
// send_message
|
|
|
|
auto send_message_start = PromiseCreator::lambda([actor_id = actor_id(this), state_id](Result<> result) {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_outbound_send_message_start, state_id);
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_promise_error, result.move_as_error(),
|
2019-09-04 20:30:27 +02:00
|
|
|
"on_outbound_send_message_start");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// update seq_no
|
|
|
|
update_seq_no_state(*message);
|
|
|
|
|
|
|
|
// process action
|
|
|
|
if (message->action) {
|
|
|
|
on_outbound_action(*message->action, message->message_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// save_changes
|
|
|
|
auto save_changes_finish = PromiseCreator::lambda([actor_id = actor_id(this), state_id](Result<> result) {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_outbound_save_changes_finish, state_id);
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_promise_error, result.move_as_error(),
|
|
|
|
"on_outbound_save_chages_finish");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
auto save_changes_start = add_changes(std::move(save_changes_finish));
|
|
|
|
|
|
|
|
// wait for ack
|
|
|
|
auto out_seq_no = state->message->my_out_seq_no - 1;
|
|
|
|
if (out_seq_no < seq_no_state_.his_in_seq_no) {
|
|
|
|
state->ack_flag = true;
|
|
|
|
} else {
|
|
|
|
out_seq_no_to_outbound_message_state_token_[out_seq_no] = state_id;
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
// save_log_event => [send_message; save_changes]
|
|
|
|
auto save_log_event_finish = PromiseCreator::join(std::move(send_message_start), std::move(save_changes_start));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
auto log_event_id = state->message->log_event_id();
|
|
|
|
if (log_event_id == 0) {
|
|
|
|
log_event_id = binlog_add(context_->binlog(), LogEvent::HandlerType::SecretChats, create_storer(*state->message));
|
|
|
|
LOG(INFO) << "Outbound secret message [save_log_event] start " << tag("log_event_id", log_event_id);
|
|
|
|
context_->binlog()->force_sync(std::move(save_log_event_finish));
|
|
|
|
state->message->set_log_event_id(log_event_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Outbound secret message [save_log_event] skip " << tag("log_event_id", log_event_id);
|
|
|
|
save_log_event_finish.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-09-22 01:15:09 +02:00
|
|
|
promise.set_value(Unit()); // log event was sent to binlog
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_his_in_seq_no_updated() {
|
2020-08-09 03:01:30 +02:00
|
|
|
auto it = out_seq_no_to_outbound_message_state_token_.begin();
|
|
|
|
while (it != out_seq_no_to_outbound_message_state_token_.end() && it->first < seq_no_state_.his_in_seq_no) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto token = it->second;
|
|
|
|
it = out_seq_no_to_outbound_message_state_token_.erase(it);
|
|
|
|
on_outbound_ack(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void SecretChatActor::on_seq_no_state_changed() {
|
|
|
|
seq_no_state_changed_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_pfs_state_changed() {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(INFO) << "In on_pfs_state_changed: " << pfs_state_;
|
2018-12-31 20:04:05 +01:00
|
|
|
pfs_state_changed_ = true;
|
|
|
|
}
|
2020-08-09 13:18:08 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<> SecretChatActor::add_changes(Promise<> save_changes_finish) {
|
|
|
|
StateChange change;
|
|
|
|
if (seq_no_state_changed_) {
|
|
|
|
change.seq_no_state_change = SeqNoStateChange(seq_no_state_);
|
|
|
|
seq_no_state_changed_ = false;
|
|
|
|
}
|
|
|
|
if (pfs_state_changed_) {
|
|
|
|
change.pfs_state_change = PfsStateChange(pfs_state_);
|
|
|
|
pfs_state_changed_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
change.save_changes_finish = std::move(save_changes_finish);
|
|
|
|
auto save_changes_start_token = changes_processor_.add(std::move(change));
|
|
|
|
|
|
|
|
return PromiseCreator::lambda([actor_id = actor_id(this), save_changes_start_token](Result<> result) {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_save_changes_start, save_changes_start_token);
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_promise_error, result.move_as_error(), "on_save_changes_start");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void SecretChatActor::update_seq_no_state(const T &new_seq_no_state) {
|
|
|
|
// Some old updates may arrive. Just ignore them
|
|
|
|
if (seq_no_state_.message_id >= new_seq_no_state.message_id &&
|
|
|
|
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 &&
|
|
|
|
seq_no_state_.his_in_seq_no >= new_seq_no_state.his_in_seq_no) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
seq_no_state_.message_id = new_seq_no_state.message_id;
|
|
|
|
if (new_seq_no_state.my_in_seq_no != -1) {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(INFO) << "Have my_in_seq_no: " << seq_no_state_.my_in_seq_no << "--->" << new_seq_no_state.my_in_seq_no;
|
2018-12-31 20:04:05 +01:00
|
|
|
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;
|
|
|
|
|
2018-06-14 17:00:10 +02:00
|
|
|
auto new_his_layer = new_seq_no_state.his_layer();
|
|
|
|
if (new_his_layer != -1) {
|
|
|
|
seq_no_state_.his_layer = new_his_layer;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return on_seq_no_state_changed();
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
void SecretChatActor::do_inbound_message_decrypted_pending(unique_ptr<log_event::InboundSecretMessage> message) {
|
|
|
|
// Just save log event if necessary
|
|
|
|
auto log_event_id = message->log_event_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-01-15 09:05:53 +01:00
|
|
|
// QTS
|
2020-08-02 21:07:22 +02:00
|
|
|
auto qts_promise = std::move(message->promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
if (log_event_id == 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
message->is_pending = true;
|
2020-09-22 01:15:09 +02:00
|
|
|
message->set_log_event_id(binlog_add(context_->binlog(), LogEvent::HandlerType::SecretChats,
|
|
|
|
create_storer(*message), std::move(qts_promise)));
|
|
|
|
LOG(INFO) << "Inbound PENDING secret message [save_log_event] start (do not expect finish) "
|
|
|
|
<< tag("log_event_id", message->log_event_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Inbound PENDING secret message [save_log_event] skip " << tag("log_event_id", log_event_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(!qts_promise);
|
|
|
|
}
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Inbound PENDING secret message start " << tag("log_event_id", log_event_id) << tag("message", *message);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto seq_no = message->decrypted_message_layer->out_seq_no_ / 2;
|
|
|
|
pending_inbound_messages_[seq_no] = std::move(message);
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
Status SecretChatActor::do_inbound_message_decrypted(unique_ptr<log_event::InboundSecretMessage> message) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// InboundSecretMessage
|
|
|
|
//
|
2020-09-22 01:15:09 +02:00
|
|
|
// 1. [] => Add log event. [save_log_event]
|
|
|
|
// 2. [save_log_event] => Save SeqNoState [save_changes]
|
|
|
|
// 3. [save_log_event] => Add message to MessageManager [save_message]
|
2023-01-02 13:38:04 +01:00
|
|
|
// Note: if we are able to add message by random_id, we may not wait for (log event). Otherwise, we should force
|
2018-12-31 20:04:05 +01:00
|
|
|
// binlog flush.
|
2023-01-15 09:05:53 +01:00
|
|
|
// 4. [save_log_event] => Update QTS [qts]
|
2020-09-22 01:15:09 +02:00
|
|
|
// 5. [save_changes; save_message; ?qts) => Remove log event [remove_log_event]
|
2023-01-15 09:05:53 +01:00
|
|
|
// Note: It is easier not to wait for QTS. In the worst case old update will be handled again after restart.
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto state_id = inbound_message_states_.create();
|
|
|
|
InboundMessageState &state = *inbound_message_states_.get(state_id);
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
// save log event
|
|
|
|
auto log_event_id = message->log_event_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
bool need_sync = false;
|
2020-09-22 01:15:09 +02:00
|
|
|
if (log_event_id == 0) {
|
|
|
|
log_event_id = binlog_add(context_->binlog(), LogEvent::HandlerType::SecretChats, create_storer(*message));
|
|
|
|
LOG(INFO) << "Inbound secret message [save_log_event] start " << tag("log_event_id", log_event_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
need_sync = true;
|
|
|
|
} else {
|
|
|
|
if (message->is_pending) {
|
|
|
|
message->is_pending = false;
|
2020-09-22 01:15:09 +02:00
|
|
|
auto old_log_event_id = log_event_id;
|
|
|
|
log_event_id = binlog_add(context_->binlog(), LogEvent::HandlerType::SecretChats, create_storer(*message));
|
|
|
|
binlog_erase(context_->binlog(), old_log_event_id);
|
|
|
|
LOG(INFO) << "Inbound secret message [save_log_event] rewrite (after pending state) "
|
|
|
|
<< tag("log_event_id", log_event_id) << tag("old_log_event_id", old_log_event_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
need_sync = true;
|
|
|
|
} else {
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Inbound secret message [save_log_event] skip " << tag("log_event_id", log_event_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Inbound secret message start " << tag("log_event_id", log_event_id) << tag("message", *message);
|
|
|
|
state.log_event_id = log_event_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// save_message
|
|
|
|
auto save_message_finish = PromiseCreator::lambda([actor_id = actor_id(this), state_id](Result<> result) {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_inbound_save_message_finish, state_id);
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_promise_error, result.move_as_error(),
|
|
|
|
"on_inbound_save_message_finish");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// update seq_no
|
|
|
|
update_seq_no_state(*message);
|
|
|
|
|
|
|
|
// drop old key
|
|
|
|
if (!pfs_state_.other_auth_key.empty() && message->auth_key_id == pfs_state_.auth_key.id() &&
|
|
|
|
pfs_state_.can_forget_other_key) {
|
|
|
|
LOG(INFO) << "Drop old auth key " << tag("auth_key_id", format::as_hex(pfs_state_.other_auth_key.id()));
|
|
|
|
pfs_state_.other_auth_key = mtproto::AuthKey();
|
|
|
|
on_pfs_state_changed();
|
|
|
|
}
|
|
|
|
|
2023-01-15 09:05:53 +01:00
|
|
|
// QTS
|
2020-08-02 21:07:22 +02:00
|
|
|
auto qts_promise = std::move(message->promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// process message
|
|
|
|
if (message->decrypted_message_layer->message_->get_id() == secret_api::decryptedMessage46::ID) {
|
|
|
|
auto old = move_tl_object_as<secret_api::decryptedMessage46>(message->decrypted_message_layer->message_);
|
|
|
|
old->flags_ &= ~secret_api::decryptedMessage::GROUPED_ID_MASK; // just in case
|
|
|
|
message->decrypted_message_layer->message_ = secret_api::make_object<secret_api::decryptedMessage>(
|
2020-08-19 17:15:40 +02:00
|
|
|
old->flags_, false /*ignored*/, old->random_id_, old->ttl_, std::move(old->message_), std::move(old->media_),
|
2018-12-31 20:04:05 +01:00
|
|
|
std::move(old->entities_), std::move(old->via_bot_name_), old->reply_to_random_id_, 0);
|
|
|
|
}
|
2018-07-23 23:12:53 +02:00
|
|
|
if (message->decrypted_message_layer->message_->get_id() == secret_api::decryptedMessageService8::ID) {
|
|
|
|
auto old = move_tl_object_as<secret_api::decryptedMessageService8>(message->decrypted_message_layer->message_);
|
|
|
|
message->decrypted_message_layer->message_ =
|
|
|
|
secret_api::make_object<secret_api::decryptedMessageService>(old->random_id_, std::move(old->action_));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// NB: message is invalid after this 'move_as'
|
|
|
|
// Send update through context_
|
|
|
|
// Note, that update may be sent multiple times and should be somehow protected from replay.
|
|
|
|
// Luckily all updates seems to be idempotent.
|
2020-09-22 01:15:09 +02:00
|
|
|
// We could use ChangesProcessor to mark log event as sent to context_, but I don't see any advantages of this
|
2018-12-31 20:04:05 +01:00
|
|
|
// approach.
|
|
|
|
if (message->decrypted_message_layer->message_->get_id() == secret_api::decryptedMessage::ID) {
|
|
|
|
auto decrypted_message =
|
|
|
|
move_tl_object_as<secret_api::decryptedMessage>(message->decrypted_message_layer->message_);
|
|
|
|
context_->on_inbound_message(get_user_id(), MessageId(ServerMessageId(message->message_id)), message->date,
|
2021-08-01 05:17:51 +02:00
|
|
|
std::move(message->file), std::move(decrypted_message),
|
|
|
|
std::move(save_message_finish));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (message->decrypted_message_layer->message_->get_id() == secret_api::decryptedMessageService::ID) {
|
|
|
|
auto decrypted_message_service =
|
|
|
|
move_tl_object_as<secret_api::decryptedMessageService>(message->decrypted_message_layer->message_);
|
|
|
|
|
|
|
|
auto action = std::move(decrypted_message_service->action_);
|
|
|
|
switch (action->get_id()) {
|
|
|
|
case secret_api::decryptedMessageActionDeleteMessages::ID:
|
2020-09-22 01:15:09 +02:00
|
|
|
// Corresponding log event won't be deleted before promise returned by add_changes is set.
|
2018-12-31 20:04:05 +01:00
|
|
|
context_->on_delete_messages(
|
2019-09-04 19:49:24 +02:00
|
|
|
static_cast<const secret_api::decryptedMessageActionDeleteMessages &>(*action).random_ids_,
|
2018-12-31 20:04:05 +01:00
|
|
|
std::move(save_message_finish));
|
|
|
|
break;
|
|
|
|
case secret_api::decryptedMessageActionFlushHistory::ID:
|
2021-01-20 12:49:18 +01:00
|
|
|
context_->on_flush_history(false, MessageId(ServerMessageId(message->message_id)),
|
|
|
|
std::move(save_message_finish));
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
case secret_api::decryptedMessageActionReadMessages::ID: {
|
2019-09-04 20:30:27 +02:00
|
|
|
const auto &random_ids =
|
|
|
|
static_cast<const secret_api::decryptedMessageActionReadMessages &>(*action).random_ids_;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (random_ids.size() == 1) {
|
|
|
|
context_->on_read_message(random_ids[0], std::move(save_message_finish));
|
|
|
|
} else { // probably never happens
|
2018-12-12 00:48:56 +01:00
|
|
|
MultiPromiseActorSafe mpas{"ReadSecretMessagesMultiPromiseActor"};
|
2018-12-31 20:04:05 +01:00
|
|
|
mpas.add_promise(std::move(save_message_finish));
|
|
|
|
auto lock = mpas.get_promise();
|
|
|
|
for (auto random_id : random_ids) {
|
|
|
|
context_->on_read_message(random_id, mpas.get_promise());
|
|
|
|
}
|
|
|
|
lock.set_value(Unit());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case secret_api::decryptedMessageActionScreenshotMessages::ID:
|
|
|
|
context_->on_screenshot_taken(get_user_id(), MessageId(ServerMessageId(message->message_id)), message->date,
|
|
|
|
decrypted_message_service->random_id_, std::move(save_message_finish));
|
|
|
|
break;
|
|
|
|
case secret_api::decryptedMessageActionSetMessageTTL::ID:
|
|
|
|
context_->on_set_ttl(get_user_id(), MessageId(ServerMessageId(message->message_id)), message->date,
|
2019-09-04 19:49:24 +02:00
|
|
|
static_cast<const secret_api::decryptedMessageActionSetMessageTTL &>(*action).ttl_seconds_,
|
2018-12-31 20:04:05 +01:00
|
|
|
decrypted_message_service->random_id_, std::move(save_message_finish));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
save_message_finish.set_value(Unit());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.message_id = message->message_id;
|
|
|
|
TRY_STATUS(on_inbound_action(*action, message->message_id));
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "INGORE MESSAGE: " << to_string(message->decrypted_message_layer);
|
|
|
|
save_message_finish.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
// save_changes
|
|
|
|
auto save_changes_finish = PromiseCreator::lambda([actor_id = actor_id(this), state_id](Result<> result) {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_inbound_save_changes_finish, state_id);
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_promise_error, result.move_as_error(),
|
|
|
|
"on_inbound_save_changes_finish");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
auto save_changes_start = add_changes(std::move(save_changes_finish));
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
// save_log_event
|
|
|
|
auto save_log_event_finish = PromiseCreator::join(std::move(save_changes_start), std::move(qts_promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (need_sync) {
|
|
|
|
// TODO: lazy sync is enough
|
2020-09-22 01:15:09 +02:00
|
|
|
context_->binlog()->force_sync(std::move(save_log_event_finish));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2020-09-22 01:15:09 +02:00
|
|
|
save_log_event_finish.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_save_changes_start(ChangesProcessor<StateChange>::Id save_changes_token) {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SeqNoStateChange seq_no_state_change;
|
|
|
|
PfsStateChange pfs_state_change;
|
|
|
|
std::vector<Promise<Unit>> save_changes_finish_promises;
|
|
|
|
changes_processor_.finish(save_changes_token, [&](StateChange &&change) {
|
|
|
|
save_changes_finish_promises.emplace_back(std::move(change.save_changes_finish));
|
|
|
|
if (change.seq_no_state_change) {
|
|
|
|
seq_no_state_change = std::move(change.seq_no_state_change);
|
|
|
|
}
|
|
|
|
if (change.pfs_state_change) {
|
|
|
|
pfs_state_change = std::move(change.pfs_state_change);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (seq_no_state_change) {
|
|
|
|
LOG(INFO) << "SAVE SeqNoState " << seq_no_state_change;
|
|
|
|
context_->secret_chat_db()->set_value(seq_no_state_change);
|
|
|
|
}
|
|
|
|
if (pfs_state_change) {
|
|
|
|
LOG(INFO) << "SAVE PfsState " << pfs_state_change;
|
|
|
|
saved_pfs_state_message_id_ = pfs_state_change.message_id;
|
|
|
|
context_->secret_chat_db()->set_value(pfs_state_change);
|
|
|
|
}
|
2018-12-13 23:48:36 +01:00
|
|
|
// NB: we may not wait till database is flushed, because every other change will be in the same binlog
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &save_changes_finish : save_changes_finish_promises) {
|
|
|
|
save_changes_finish.set_value(Unit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_inbound_save_message_finish(uint64 state_id) {
|
2021-10-07 14:28:52 +02:00
|
|
|
if (close_flag_ || context_->close_flag()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto *state = inbound_message_states_.get(state_id);
|
|
|
|
CHECK(state);
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Inbound message [save_message] finish " << tag("log_event_id", state->log_event_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
state->save_message_finish = true;
|
|
|
|
inbound_loop(state, state_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_inbound_save_changes_finish(uint64 state_id) {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto *state = inbound_message_states_.get(state_id);
|
|
|
|
CHECK(state);
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Inbound message [save_changes] finish " << tag("log_event_id", state->log_event_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
state->save_changes_finish = true;
|
|
|
|
inbound_loop(state, state_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::inbound_loop(InboundMessageState *state, uint64 state_id) {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!state->save_changes_finish || !state->save_message_finish) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Inbound message [remove_log_event] start " << tag("log_event_id", state->log_event_id);
|
|
|
|
binlog_erase(context_->binlog(), state->log_event_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
inbound_message_states_.erase(state_id);
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
NetQueryPtr SecretChatActor::create_net_query(const log_event::OutboundSecretMessage &message) {
|
2018-12-31 20:04:05 +01:00
|
|
|
NetQueryPtr query;
|
2020-06-09 13:34:48 +02:00
|
|
|
if (message.need_notify_user) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(message.file.empty());
|
2020-03-15 01:43:42 +01:00
|
|
|
query = create_net_query(QueryType::Message,
|
|
|
|
telegram_api::messages_sendEncryptedService(get_input_chat(), message.random_id,
|
|
|
|
message.encrypted_message.clone()));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (message.file.empty()) {
|
2020-08-21 14:38:23 +02:00
|
|
|
int32 flags = 0;
|
|
|
|
if (message.is_silent) {
|
|
|
|
flags |= telegram_api::messages_sendEncrypted::SILENT_MASK;
|
|
|
|
}
|
2020-03-15 01:43:42 +01:00
|
|
|
query = create_net_query(
|
2020-08-21 14:38:23 +02:00
|
|
|
QueryType::Message, telegram_api::messages_sendEncrypted(flags, false /*ignored*/, get_input_chat(),
|
2020-07-31 04:50:11 +02:00
|
|
|
message.random_id, message.encrypted_message.clone()));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2020-08-21 14:38:23 +02:00
|
|
|
int32 flags = 0;
|
|
|
|
if (message.is_silent) {
|
|
|
|
flags |= telegram_api::messages_sendEncryptedFile::SILENT_MASK;
|
|
|
|
}
|
2020-07-31 04:50:11 +02:00
|
|
|
query = create_net_query(QueryType::Message,
|
|
|
|
telegram_api::messages_sendEncryptedFile(
|
2020-08-21 14:38:23 +02:00
|
|
|
flags, false /*ignored*/, get_input_chat(), message.random_id,
|
2020-07-31 04:50:11 +02:00
|
|
|
message.encrypted_message.clone(), message.file.as_input_encrypted_file()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-06-09 13:34:48 +02:00
|
|
|
if (!message.is_rewritable) {
|
2020-06-10 01:44:54 +02:00
|
|
|
query->total_timeout_limit_ = 1000000000; // inf. We will re-sent it immediately anyway
|
2020-06-09 13:34:48 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (message.is_external && context_->get_config_option_boolean("use_quick_ack")) {
|
|
|
|
query->quick_ack_promise_ =
|
2022-06-26 16:11:27 +02:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), random_id = message.random_id](Result<Unit> result) {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_send_message_ack, random_id);
|
|
|
|
}
|
|
|
|
});
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_outbound_send_message_start(uint64 state_id) {
|
|
|
|
auto *state = outbound_message_states_.get(state_id);
|
|
|
|
if (state == nullptr) {
|
|
|
|
LOG(INFO) << "Outbound message [send_message] start ignored (unknown state_id) " << tag("state_id", state_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *message = state->message.get();
|
|
|
|
|
|
|
|
if (!message->is_sent) {
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Outbound message [send_message] start " << tag("log_event_id", state->message->log_event_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
auto query = create_net_query(*message);
|
|
|
|
state->net_query_id = query->id();
|
|
|
|
state->net_query_ref = query.get_weak();
|
|
|
|
state->net_query_may_fail = state->message->is_rewritable;
|
|
|
|
context_->send_net_query(std::move(query), actor_shared(this, state_id), true);
|
|
|
|
} else {
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Outbound message [send_message] start dummy " << tag("log_event_id", state->message->log_event_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
on_outbound_send_message_finish(state_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::outbound_resend(uint64 state_id) {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto *state = outbound_message_states_.get(state_id);
|
|
|
|
CHECK(state);
|
|
|
|
|
|
|
|
state->message->is_sent = false;
|
|
|
|
state->net_query_id = 0;
|
|
|
|
state->net_query_ref = NetQueryRef();
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Outbound message [resend] " << tag("log_event_id", state->message->log_event_id())
|
2018-12-31 20:04:05 +01:00
|
|
|
<< tag("state_id", state_id);
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
binlog_rewrite(context_->binlog(), state->message->log_event_id(), LogEvent::HandlerType::SecretChats,
|
2018-06-28 21:20:48 +02:00
|
|
|
create_storer(*state->message));
|
2018-12-31 20:04:05 +01:00
|
|
|
auto send_message_start = PromiseCreator::lambda([actor_id = actor_id(this), state_id](Result<> result) {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_outbound_send_message_start, state_id);
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_promise_error, result.move_as_error(),
|
|
|
|
"on_outbound_send_message_start");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
context_->binlog()->force_sync(std::move(send_message_start));
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SecretChatActor::outbound_rewrite_with_empty(uint64 state_id) {
|
|
|
|
if (close_flag_) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
auto *state = outbound_message_states_.get(state_id);
|
|
|
|
if (state == nullptr || !state->message->is_rewritable) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
cancel_query(state->net_query_ref);
|
|
|
|
|
|
|
|
MutableSlice data = state->message->encrypted_message.as_slice();
|
|
|
|
CHECK(is_aligned_pointer<4>(data.data()));
|
|
|
|
|
2020-06-05 07:16:38 +02:00
|
|
|
// Rewrite with delete itself
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<secret_api::DecryptedMessage> message = secret_api::make_object<secret_api::decryptedMessageService>(
|
|
|
|
state->message->random_id, secret_api::make_object<secret_api::decryptedMessageActionDeleteMessages>(
|
|
|
|
std::vector<int64>{static_cast<int64>(state->message->random_id)}));
|
|
|
|
|
2021-06-08 17:31:29 +02:00
|
|
|
TRY_RESULT(encrypted_message,
|
|
|
|
create_encrypted_message(state->message->my_in_seq_no, state->message->my_out_seq_no, message));
|
2018-12-31 20:04:05 +01:00
|
|
|
state->message->encrypted_message = std::move(encrypted_message);
|
|
|
|
LOG(INFO) << tag("crc", crc64(state->message->encrypted_message.as_slice()));
|
|
|
|
state->message->is_rewritable = false;
|
|
|
|
state->message->is_external = false;
|
2020-06-09 13:34:48 +02:00
|
|
|
state->message->need_notify_user = false;
|
2020-08-21 14:38:23 +02:00
|
|
|
state->message->is_silent = true;
|
2022-05-18 15:59:15 +02:00
|
|
|
state->message->file = log_event::EncryptedInputFile();
|
2020-09-22 01:15:09 +02:00
|
|
|
binlog_rewrite(context_->binlog(), state->message->log_event_id(), LogEvent::HandlerType::SecretChats,
|
2018-06-28 21:20:48 +02:00
|
|
|
create_storer(*state->message));
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_outbound_send_message_result(NetQueryPtr query, Promise<NetQueryPtr> resend_promise) {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto state_id = get_link_token();
|
|
|
|
auto *state = outbound_message_states_.get(state_id);
|
|
|
|
if (!state) {
|
|
|
|
LOG(INFO) << "Ignore old net query result " << tag("state_id", state_id);
|
|
|
|
query->clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(state);
|
|
|
|
if (state->net_query_id != query->id()) {
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Ignore old net query result " << tag("log_event_id", state->message->log_event_id())
|
2018-12-31 20:04:05 +01:00
|
|
|
<< tag("query_id", query->id()) << tag("state_query_id", state->net_query_id) << query;
|
|
|
|
query->clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->net_query_id = 0;
|
|
|
|
state->net_query_ref = NetQueryRef();
|
|
|
|
|
|
|
|
auto r_result = fetch_result<telegram_api::messages_sendEncrypted>(std::move(query));
|
|
|
|
if (r_result.is_error()) {
|
|
|
|
auto error = r_result.move_as_error();
|
|
|
|
|
|
|
|
auto send_message_error_promise =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), state_id, error = error.clone(),
|
|
|
|
resend_promise = std::move(resend_promise)](Result<> result) mutable {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_outbound_send_message_error, state_id, std::move(error),
|
|
|
|
std::move(resend_promise));
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_promise_error, result.move_as_error(),
|
|
|
|
"on_outbound_send_message_error");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (state->message->is_external) {
|
|
|
|
LOG(INFO) << "Outbound secret message [send_message] failed, rewrite it with dummy "
|
2020-09-22 01:15:09 +02:00
|
|
|
<< tag("log_event_id", state->message->log_event_id()) << tag("error", error);
|
2018-01-31 16:14:43 +01:00
|
|
|
state->send_result_ = [this, random_id = state->message->random_id, error_code = error.code(),
|
2021-10-23 20:17:00 +02:00
|
|
|
error_message = error.message().str()](Promise<> promise) {
|
|
|
|
context_->on_send_message_error(random_id, Status::Error(error_code, error_message), std::move(promise));
|
2018-01-31 16:14:43 +01:00
|
|
|
};
|
|
|
|
state->send_result_(std::move(send_message_error_promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
// Just resend.
|
|
|
|
LOG(INFO) << "Outbound secret message [send_message] failed, resend it "
|
2020-09-22 01:15:09 +02:00
|
|
|
<< tag("log_event_id", state->message->log_event_id()) << tag("error", error);
|
2018-12-31 20:04:05 +01:00
|
|
|
send_message_error_promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = r_result.move_as_ok();
|
|
|
|
LOG(INFO) << "Got messages_sendEncrypted result: " << tag("message_id", state->message->message_id)
|
|
|
|
<< tag("random_id", state->message->random_id) << to_string(*result);
|
|
|
|
|
|
|
|
auto send_message_finish_promise = PromiseCreator::lambda([actor_id = actor_id(this), state_id](Result<> result) {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_outbound_send_message_finish, state_id);
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_promise_error, result.move_as_error(),
|
|
|
|
"on_outbound_send_message_finish");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (state->message->is_external) {
|
|
|
|
switch (result->get_id()) {
|
|
|
|
case telegram_api::messages_sentEncryptedMessage::ID: {
|
|
|
|
auto sent = move_tl_object_as<telegram_api::messages_sentEncryptedMessage>(result);
|
2018-01-31 16:14:43 +01:00
|
|
|
state->send_result_ = [this, random_id = state->message->random_id,
|
|
|
|
message_id = MessageId(ServerMessageId(state->message->message_id)),
|
|
|
|
date = sent->date_](Promise<> promise) {
|
2021-10-23 20:17:00 +02:00
|
|
|
context_->on_send_message_ok(random_id, message_id, date, nullptr, std::move(promise));
|
2018-01-31 16:14:43 +01:00
|
|
|
};
|
|
|
|
state->send_result_(std::move(send_message_finish_promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
case telegram_api::messages_sentEncryptedFile::ID: {
|
|
|
|
auto sent = move_tl_object_as<telegram_api::messages_sentEncryptedFile>(result);
|
2021-08-01 05:17:51 +02:00
|
|
|
auto file = EncryptedFile::get_encrypted_file(std::move(sent->file_));
|
|
|
|
if (file == nullptr) {
|
2022-05-18 15:59:15 +02:00
|
|
|
state->message->file = log_event::EncryptedInputFile();
|
2021-08-01 05:17:51 +02:00
|
|
|
state->send_result_ = [this, random_id = state->message->random_id,
|
|
|
|
message_id = MessageId(ServerMessageId(state->message->message_id)),
|
|
|
|
date = sent->date_](Promise<> promise) {
|
2021-10-23 20:17:00 +02:00
|
|
|
context_->on_send_message_ok(random_id, message_id, date, nullptr, std::move(promise));
|
2021-08-01 05:17:51 +02:00
|
|
|
};
|
|
|
|
} else {
|
2022-05-18 15:59:15 +02:00
|
|
|
state->message->file = {log_event::EncryptedInputFile::Location, file->id_, file->access_hash_, 0, 0};
|
2021-08-01 05:17:51 +02:00
|
|
|
state->send_result_ = [this, random_id = state->message->random_id,
|
|
|
|
message_id = MessageId(ServerMessageId(state->message->message_id)),
|
|
|
|
date = sent->date_, file = *file](Promise<> promise) {
|
2021-10-23 20:17:00 +02:00
|
|
|
context_->on_send_message_ok(random_id, message_id, date, make_unique<EncryptedFile>(file),
|
|
|
|
std::move(promise));
|
2021-08-01 05:17:51 +02:00
|
|
|
};
|
|
|
|
}
|
2018-01-31 16:14:43 +01:00
|
|
|
state->send_result_(std::move(send_message_finish_promise));
|
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
send_message_finish_promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_outbound_send_message_error(uint64 state_id, Status error,
|
|
|
|
Promise<NetQueryPtr> resend_promise) {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-23 23:12:53 +02:00
|
|
|
if (context_->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
auto *state = outbound_message_states_.get(state_id);
|
|
|
|
if (!state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool need_sync = false;
|
|
|
|
if (state->net_query_may_fail) {
|
|
|
|
// message could be already non-rewritable, if it was deleted during NetQuery execution.
|
|
|
|
if (state->message->is_rewritable) {
|
|
|
|
delete_message(state->message->random_id, Promise<>());
|
|
|
|
// state pointer may be invalidated
|
|
|
|
state = outbound_message_states_.get(state_id);
|
|
|
|
need_sync = true;
|
|
|
|
}
|
2022-06-09 18:10:38 +02:00
|
|
|
} else if (error.code() != 429) {
|
|
|
|
return on_fatal_error(std::move(error),
|
|
|
|
(error.code() == 400 && error.message() == "ENCRYPTION_DECLINED") || error.code() == 403);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
auto query = create_net_query(*state->message);
|
|
|
|
state->net_query_id = query->id();
|
|
|
|
|
|
|
|
CHECK(resend_promise);
|
|
|
|
auto send_message_start =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), resend_promise = std::move(resend_promise),
|
|
|
|
query = std::move(query)](Result<> result) mutable {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
resend_promise.set_value(std::move(query));
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &SecretChatActor::on_promise_error, result.move_as_error(), "resend_query");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (need_sync) {
|
|
|
|
context_->binlog()->force_sync(std::move(send_message_start));
|
|
|
|
} else {
|
|
|
|
send_message_start.set_value(Unit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_outbound_send_message_finish(uint64 state_id) {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto *state = outbound_message_states_.get(state_id);
|
|
|
|
if (!state) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Outbound secret message [send_message] finish " << tag("log_event_id", state->message->log_event_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
state->send_message_finish_flag = true;
|
|
|
|
state->outer_send_message_finish.set_value(Unit());
|
|
|
|
|
|
|
|
outbound_loop(state, state_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_outbound_save_changes_finish(uint64 state_id) {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto *state = outbound_message_states_.get(state_id);
|
|
|
|
CHECK(state);
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Outbound secret message [save_changes] finish " << tag("log_event_id", state->message->log_event_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
state->save_changes_finish_flag = true;
|
|
|
|
outbound_loop(state, state_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_outbound_ack(uint64 state_id) {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto *state = outbound_message_states_.get(state_id);
|
|
|
|
CHECK(state);
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Outbound secret message [ack] finish " << tag("log_event_id", state->message->log_event_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
state->ack_flag = true;
|
|
|
|
outbound_loop(state, state_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_outbound_outer_send_message_promise(uint64 state_id, Promise<> promise) {
|
|
|
|
if (close_flag_) {
|
|
|
|
promise.set_error(Status::Error(400, "Chat is closed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto *state = outbound_message_states_.get(state_id);
|
|
|
|
CHECK(state);
|
2021-06-08 17:31:29 +02:00
|
|
|
LOG(INFO) << "Outbound secret message " << tag("log_event_id", state->message->log_event_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(Unit()); // Seems like this message is at least stored to binlog already
|
2018-01-31 16:14:43 +01:00
|
|
|
if (state->send_result_) {
|
|
|
|
state->send_result_({});
|
2020-08-18 17:24:53 +02:00
|
|
|
} else if (state->message->is_sent) {
|
2018-01-31 19:20:40 +01:00
|
|
|
context_->on_send_message_error(state->message->random_id, Status::Error(400, "Message has already been sent"),
|
|
|
|
Auto());
|
2018-01-31 16:14:43 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-31 10:18:40 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::outbound_loop(OutboundMessageState *state, uint64 state_id) {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (state->save_changes_finish_flag /*&& state->send_message_finish_flag*/ && state->ack_flag) {
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Outbound message [remove_log_event] start " << tag("log_event_id", state->message->log_event_id());
|
|
|
|
binlog_erase(context_->binlog(), state->message->log_event_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
random_id_to_outbound_message_state_token_.erase(state->message->random_id);
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Outbound message finish (lazy) " << tag("log_event_id", state->message->log_event_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
outbound_message_states_.erase(state_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->save_changes_finish_flag && state->send_message_finish_flag &&
|
2020-09-22 01:15:09 +02:00
|
|
|
!state->message->is_sent) { // [rewrite_log_event]
|
|
|
|
LOG(INFO) << "Outbound message [rewrite_log_event] start " << tag("log_event_id", state->message->log_event_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
state->message->is_sent = true;
|
2020-09-22 01:15:09 +02:00
|
|
|
binlog_rewrite(context_->binlog(), state->message->log_event_id(), LogEvent::HandlerType::SecretChats,
|
2018-06-28 21:20:48 +02:00
|
|
|
create_storer(*state->message));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
Status SecretChatActor::save_common_info(T &update) {
|
|
|
|
if (auth_state_.id != update.id_) {
|
|
|
|
return Status::Error(PSLICE() << "chat_id mismatch: " << tag("mine", auth_state_.id) << tag("outer", update.id_));
|
|
|
|
}
|
|
|
|
auth_state_.id = update.id_;
|
|
|
|
auth_state_.access_hash = update.access_hash_;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SecretChatActor::on_update_chat(telegram_api::encryptedChatRequested &update) {
|
|
|
|
if (auth_state_.state != State::Empty) {
|
2022-05-21 22:07:28 +02:00
|
|
|
LOG(INFO) << "Unexpected encryptedChatRequested ignored: " << to_string(update);
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
auth_state_.state = State::SendAccept;
|
|
|
|
auth_state_.x = 1;
|
2021-03-29 10:57:26 +02:00
|
|
|
auth_state_.user_id = UserId(update.admin_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
auth_state_.date = context_->unix_time();
|
|
|
|
TRY_STATUS(save_common_info(update));
|
|
|
|
auth_state_.handshake.set_g_a(update.g_a_.as_slice());
|
2020-06-30 11:11:22 +02:00
|
|
|
if ((update.flags_ & telegram_api::encryptedChatRequested::FOLDER_ID_MASK) != 0) {
|
|
|
|
auth_state_.initial_folder_id = FolderId(update.folder_id_);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
send_update_secret_chat();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
Status SecretChatActor::on_update_chat(telegram_api::encryptedChatEmpty &update) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
Status SecretChatActor::on_update_chat(telegram_api::encryptedChatWaiting &update) {
|
|
|
|
if (auth_state_.state != State::WaitRequestResponse && auth_state_.state != State::WaitAcceptResponse) {
|
2022-05-21 22:07:28 +02:00
|
|
|
LOG(INFO) << "Unexpected encryptedChatWaiting ignored";
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
TRY_STATUS(save_common_info(update));
|
|
|
|
send_update_secret_chat();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
Status SecretChatActor::on_update_chat(telegram_api::encryptedChat &update) {
|
|
|
|
if (auth_state_.state != State::WaitRequestResponse && auth_state_.state != State::WaitAcceptResponse) {
|
2022-05-21 22:07:28 +02:00
|
|
|
LOG(INFO) << "Unexpected encryptedChat ignored";
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
TRY_STATUS(save_common_info(update));
|
|
|
|
if (auth_state_.state == State::WaitRequestResponse) {
|
|
|
|
auth_state_.handshake.set_g_a(update.g_a_or_b_.as_slice());
|
2018-08-09 22:41:01 +02:00
|
|
|
TRY_STATUS(auth_state_.handshake.run_checks(true, context_->dh_callback()));
|
2018-12-31 20:04:05 +01:00
|
|
|
auto id_and_key = auth_state_.handshake.gen_key();
|
|
|
|
pfs_state_.auth_key = mtproto::AuthKey(id_and_key.first, std::move(id_and_key.second));
|
|
|
|
calc_key_hash();
|
|
|
|
}
|
|
|
|
if (static_cast<int64>(pfs_state_.auth_key.id()) != update.key_fingerprint_) {
|
|
|
|
return Status::Error("Key fingerprint mismatch");
|
|
|
|
}
|
|
|
|
auth_state_.state = State::Ready;
|
2020-09-22 01:15:09 +02:00
|
|
|
if (create_log_event_id_ != 0) {
|
|
|
|
binlog_erase(context_->binlog(), create_log_event_id_);
|
|
|
|
create_log_event_id_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NB: order is important
|
|
|
|
context_->secret_chat_db()->set_value(pfs_state_);
|
|
|
|
context_->secret_chat_db()->set_value(auth_state_);
|
|
|
|
send_update_secret_chat();
|
2021-07-05 04:42:37 +02:00
|
|
|
send_action(secret_api::make_object<secret_api::decryptedMessageActionNotifyLayer>(
|
2021-07-05 18:17:26 +02:00
|
|
|
static_cast<int32>(SecretChatLayer::Current)),
|
2021-07-05 04:42:37 +02:00
|
|
|
SendFlag::None, Promise<>());
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
Status SecretChatActor::on_update_chat(telegram_api::encryptedChatDiscarded &update) {
|
2021-01-18 17:02:24 +01:00
|
|
|
cancel_chat(update.history_deleted_, true, Promise<Unit>());
|
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status SecretChatActor::on_update_chat(NetQueryPtr query) {
|
|
|
|
static_assert(std::is_same<telegram_api::messages_requestEncryption::ReturnType,
|
|
|
|
telegram_api::messages_acceptEncryption::ReturnType>::value,
|
|
|
|
"");
|
|
|
|
TRY_RESULT(config, fetch_result<telegram_api::messages_requestEncryption>(std::move(query)));
|
|
|
|
TRY_STATUS(on_update_chat(std::move(config)));
|
|
|
|
if (auth_state_.state == State::WaitRequestResponse) {
|
|
|
|
context_->secret_chat_db()->set_value(auth_state_);
|
|
|
|
context_->binlog()->force_sync(Promise<>());
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SecretChatActor::on_update_chat(telegram_api::object_ptr<telegram_api::EncryptedChat> chat) {
|
|
|
|
Status res;
|
|
|
|
downcast_call(*chat, [&](auto &obj) { res = this->on_update_chat(obj); });
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-04-02 03:14:47 +02:00
|
|
|
Status SecretChatActor::on_read_history(NetQueryPtr query) {
|
|
|
|
if (query.generation() == read_history_query_.generation()) {
|
|
|
|
read_history_query_ = NetQueryRef();
|
|
|
|
read_history_promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::start_up() {
|
|
|
|
LOG(INFO) << "SecretChatActor: start_up";
|
|
|
|
// auto start = Time::now();
|
|
|
|
auto r_auth_state = context_->secret_chat_db()->get_value<AuthState>();
|
|
|
|
if (r_auth_state.is_ok()) {
|
|
|
|
auth_state_ = r_auth_state.move_as_ok();
|
|
|
|
}
|
|
|
|
if (!can_be_empty_ && auth_state_.state == State::Empty) {
|
2021-03-29 20:26:31 +02:00
|
|
|
LOG(INFO) << "Skip creation of empty secret chat " << auth_state_.id;
|
2018-12-31 20:04:05 +01:00
|
|
|
return stop();
|
|
|
|
}
|
2018-07-24 11:43:37 +02:00
|
|
|
if (auth_state_.state == State::Closed) {
|
|
|
|
close_flag_ = true;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
auto r_seq_no_state = context_->secret_chat_db()->get_value<SeqNoState>();
|
|
|
|
if (r_seq_no_state.is_ok()) {
|
|
|
|
seq_no_state_ = r_seq_no_state.move_as_ok();
|
|
|
|
}
|
|
|
|
auto r_config_state = context_->secret_chat_db()->get_value<ConfigState>();
|
|
|
|
if (r_config_state.is_ok()) {
|
|
|
|
config_state_ = r_config_state.move_as_ok();
|
|
|
|
}
|
|
|
|
auto r_pfs_state = context_->secret_chat_db()->get_value<PfsState>();
|
|
|
|
if (r_pfs_state.is_ok()) {
|
|
|
|
pfs_state_ = r_pfs_state.move_as_ok();
|
|
|
|
}
|
|
|
|
saved_pfs_state_message_id_ = pfs_state_.message_id;
|
|
|
|
pfs_state_.last_timestamp = Time::now();
|
|
|
|
|
|
|
|
send_update_secret_chat();
|
|
|
|
get_dh_config();
|
|
|
|
|
|
|
|
// auto end = Time::now();
|
|
|
|
// CHECK(end - start < 0.2);
|
2019-05-13 18:34:23 +02:00
|
|
|
LOG(INFO) << "In start_up with SeqNoState " << seq_no_state_;
|
|
|
|
LOG(INFO) << "In start_up with PfsState " << pfs_state_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::get_dh_config() {
|
|
|
|
if (auth_state_.state != State::Empty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto dh_config = context_->dh_config();
|
|
|
|
if (dh_config) {
|
|
|
|
auth_state_.dh_config = *dh_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto version = auth_state_.dh_config.version;
|
2021-07-19 19:19:44 +02:00
|
|
|
int32 random_length = 256; // ignored server-side, always returns 256 random bytes
|
2020-03-15 01:43:42 +01:00
|
|
|
auto query = create_net_query(QueryType::DhConfig, telegram_api::messages_getDhConfig(version, random_length));
|
2018-12-31 20:04:05 +01:00
|
|
|
context_->send_net_query(std::move(query), actor_shared(this), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SecretChatActor::on_dh_config(NetQueryPtr query) {
|
2021-07-19 19:19:44 +02:00
|
|
|
LOG(INFO) << "Got DH config";
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_RESULT(config, fetch_result<telegram_api::messages_getDhConfig>(std::move(query)));
|
|
|
|
downcast_call(*config, [&](auto &obj) { this->on_dh_config(obj); });
|
2021-07-05 20:04:23 +02:00
|
|
|
TRY_STATUS(mtproto::DhHandshake::check_config(auth_state_.dh_config.g, auth_state_.dh_config.prime,
|
|
|
|
context_->dh_callback()));
|
2018-12-31 20:04:05 +01:00
|
|
|
auth_state_.handshake.set_config(auth_state_.dh_config.g, auth_state_.dh_config.prime);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-08-07 21:43:29 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::on_dh_config(telegram_api::messages_dhConfigNotModified &dh_not_modified) {
|
2018-08-07 21:43:29 +02:00
|
|
|
Random::add_seed(dh_not_modified.random_.as_slice());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-08-07 21:43:29 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::on_dh_config(telegram_api::messages_dhConfig &dh) {
|
|
|
|
auto dh_config = std::make_shared<DhConfig>();
|
|
|
|
dh_config->version = dh.version_;
|
|
|
|
dh_config->prime = dh.p_.as_slice().str();
|
|
|
|
dh_config->g = dh.g_;
|
2018-08-07 21:43:29 +02:00
|
|
|
Random::add_seed(dh.random_.as_slice());
|
2018-12-31 20:04:05 +01:00
|
|
|
auth_state_.dh_config = *dh_config;
|
|
|
|
context_->set_dh_config(dh_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::calc_key_hash() {
|
|
|
|
unsigned char sha1_buf[20];
|
|
|
|
auto sha1_slice = Slice(sha1_buf, 20);
|
|
|
|
sha1(pfs_state_.auth_key.key(), sha1_buf);
|
|
|
|
|
|
|
|
unsigned char sha256_buf[32];
|
|
|
|
auto sha256_slice = MutableSlice(sha256_buf, 32);
|
|
|
|
sha256(pfs_state_.auth_key.key(), sha256_slice);
|
|
|
|
|
2021-02-04 13:54:12 +01:00
|
|
|
auth_state_.key_hash = PSTRING() << sha1_slice.substr(0, 16) << sha256_slice.substr(0, 20);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::send_update_secret_chat() {
|
|
|
|
if (auth_state_.state == State::Empty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SecretChatState state;
|
|
|
|
if (auth_state_.state == State::Ready) {
|
|
|
|
state = SecretChatState::Active;
|
|
|
|
} else if (auth_state_.state == State::Closed) {
|
|
|
|
state = SecretChatState::Closed;
|
|
|
|
} else {
|
|
|
|
state = SecretChatState::Waiting;
|
|
|
|
}
|
|
|
|
context_->on_update_secret_chat(auth_state_.access_hash, get_user_id(), state, auth_state_.x == 0, config_state_.ttl,
|
2020-06-30 11:11:22 +02:00
|
|
|
auth_state_.date, auth_state_.key_hash, current_layer(),
|
|
|
|
auth_state_.initial_folder_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionSetMessageTTL &set_ttl) {
|
|
|
|
config_state_.ttl = set_ttl.ttl_seconds_;
|
|
|
|
context_->secret_chat_db()->set_value(config_state_);
|
|
|
|
send_update_secret_chat();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionReadMessages &read_messages) {
|
|
|
|
// TODO
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionDeleteMessages &delete_messages) {
|
2020-09-22 01:15:09 +02:00
|
|
|
// Corresponding log event won't be deleted before promise returned by add_changes is set.
|
2018-12-31 20:04:05 +01:00
|
|
|
on_delete_messages(delete_messages.random_ids_).ensure();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionScreenshotMessages &screenshot) {
|
2021-10-19 17:11:16 +02:00
|
|
|
// nothing to do
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionFlushHistory &flush_history) {
|
|
|
|
on_flush_history(pfs_state_.message_id).ensure();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionResend &resend) {
|
|
|
|
if (seq_no_state_.resend_end_seq_no < resend.end_seq_no_ / 2) { // replay protection
|
|
|
|
seq_no_state_.resend_end_seq_no = resend.end_seq_no_ / 2;
|
|
|
|
on_seq_no_state_changed();
|
|
|
|
}
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionNotifyLayer ¬ify_layer) {
|
|
|
|
config_state_.my_layer = notify_layer.layer_;
|
|
|
|
context_->secret_chat_db()->set_value(config_state_);
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionTyping &typing) {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionSetMessageTTL &set_ttl) {
|
|
|
|
config_state_.ttl = set_ttl.ttl_seconds_;
|
|
|
|
context_->secret_chat_db()->set_value(config_state_);
|
|
|
|
send_update_secret_chat();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionReadMessages &read_messages) {
|
|
|
|
// TODO
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionDeleteMessages &delete_messages) {
|
|
|
|
return on_delete_messages(delete_messages.random_ids_);
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionScreenshotMessages &screenshot) {
|
|
|
|
// TODO
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionFlushHistory &screenshot) {
|
|
|
|
return on_flush_history(pfs_state_.message_id);
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionResend &resend) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionNotifyLayer ¬ify_layer) {
|
|
|
|
if (notify_layer.layer_ > config_state_.his_layer) {
|
|
|
|
config_state_.his_layer = notify_layer.layer_;
|
|
|
|
context_->secret_chat_db()->set_value(config_state_);
|
|
|
|
send_update_secret_chat();
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionTyping &typing) {
|
|
|
|
// noop
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perfect Forward Secrecy
|
|
|
|
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionRequestKey &request_key) {
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(pfs_state_.state == PfsState::WaitSendRequest || pfs_state_.state == PfsState::SendRequest) << pfs_state_;
|
2018-12-31 20:04:05 +01:00
|
|
|
pfs_state_.state = PfsState::WaitRequestResponse;
|
|
|
|
on_pfs_state_changed();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
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;
|
2021-07-05 20:04:23 +02:00
|
|
|
pfs_state_.handshake = mtproto::DhHandshake();
|
2018-12-31 20:04:05 +01:00
|
|
|
on_pfs_state_changed();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionAbortKey &abort_key) {
|
|
|
|
// TODO
|
|
|
|
LOG(FATAL) << "TODO";
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionCommitKey &commit_key) {
|
|
|
|
CHECK(pfs_state_.state == PfsState::WaitSendCommit || pfs_state_.state == PfsState::SendCommit);
|
|
|
|
|
|
|
|
CHECK(static_cast<int64>(pfs_state_.other_auth_key.id()) == commit_key.key_fingerprint_);
|
|
|
|
std::swap(pfs_state_.auth_key, pfs_state_.other_auth_key);
|
|
|
|
pfs_state_.can_forget_other_key = true;
|
|
|
|
|
|
|
|
pfs_state_.state = PfsState::Empty;
|
|
|
|
pfs_state_.last_message_id = pfs_state_.message_id;
|
|
|
|
pfs_state_.last_timestamp = Time::now();
|
|
|
|
pfs_state_.last_out_seq_no = seq_no_state_.my_out_seq_no;
|
|
|
|
|
|
|
|
on_pfs_state_changed();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionNoop &noop) {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionRequestKey &request_key) {
|
|
|
|
if (pfs_state_.state == PfsState::WaitRequestResponse || pfs_state_.state == PfsState::SendRequest) {
|
|
|
|
if (pfs_state_.exchange_id > request_key.exchange_id_) {
|
2019-07-26 21:38:55 +02:00
|
|
|
LOG(INFO) << "RequestKey: silently abort their request";
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
} else {
|
|
|
|
pfs_state_.state = PfsState::Empty;
|
|
|
|
if (pfs_state_.exchange_id == request_key.exchange_id_) {
|
|
|
|
context_->secret_chat_db()->set_value(pfs_state_);
|
|
|
|
LOG(WARNING) << "RequestKey: silently abort both requests (almost impossible)";
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pfs_state_.state != PfsState::Empty) {
|
|
|
|
return Status::Error("Unexpected RequestKey");
|
|
|
|
}
|
2020-06-29 18:29:17 +02:00
|
|
|
if (!pfs_state_.other_auth_key.empty()) {
|
|
|
|
LOG_CHECK(pfs_state_.can_forget_other_key) << "TODO: got requestKey, before old key is dropped";
|
|
|
|
return Status::Error("Unexpected RequestKey (old key is used)");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
pfs_state_.state = PfsState::SendAccept;
|
2021-07-05 20:04:23 +02:00
|
|
|
pfs_state_.handshake = mtproto::DhHandshake();
|
2018-12-31 20:04:05 +01:00
|
|
|
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());
|
2018-08-09 22:41:01 +02:00
|
|
|
TRY_STATUS(pfs_state_.handshake.run_checks(true, context_->dh_callback()));
|
2018-12-31 20:04:05 +01:00
|
|
|
auto id_and_key = pfs_state_.handshake.gen_key();
|
|
|
|
|
|
|
|
pfs_state_.other_auth_key = mtproto::AuthKey(id_and_key.first, std::move(id_and_key.second));
|
|
|
|
pfs_state_.can_forget_other_key = false;
|
|
|
|
pfs_state_.wait_message_id = pfs_state_.message_id;
|
|
|
|
|
|
|
|
on_pfs_state_changed();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionAcceptKey &accept_key) {
|
|
|
|
if (pfs_state_.state != PfsState::WaitRequestResponse) {
|
|
|
|
return Status::Error("AcceptKey: unexpected");
|
|
|
|
}
|
|
|
|
if (pfs_state_.exchange_id != accept_key.exchange_id_) {
|
|
|
|
return Status::Error("AcceptKey: exchange_id mismatch");
|
|
|
|
}
|
|
|
|
pfs_state_.handshake.set_g_a(accept_key.g_b_.as_slice());
|
2018-08-09 22:41:01 +02:00
|
|
|
TRY_STATUS(pfs_state_.handshake.run_checks(true, context_->dh_callback()));
|
2018-12-31 20:04:05 +01:00
|
|
|
auto id_and_key = pfs_state_.handshake.gen_key();
|
|
|
|
if (static_cast<int64>(id_and_key.first) != accept_key.key_fingerprint_) {
|
|
|
|
return Status::Error("AcceptKey: key_fingerprint mismatch");
|
|
|
|
}
|
|
|
|
pfs_state_.state = PfsState::SendCommit;
|
2021-07-05 20:04:23 +02:00
|
|
|
pfs_state_.handshake = mtproto::DhHandshake();
|
2018-12-31 20:04:05 +01:00
|
|
|
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;
|
|
|
|
pfs_state_.wait_message_id = pfs_state_.message_id;
|
|
|
|
|
|
|
|
on_pfs_state_changed();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionAbortKey &abort_key) {
|
|
|
|
if (pfs_state_.exchange_id != abort_key.exchange_id_) {
|
|
|
|
LOG(INFO) << "AbortKey: exchange_id mismatch: " << tag("my exchange_id", pfs_state_.exchange_id)
|
|
|
|
<< to_string(abort_key);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
if (pfs_state_.state != PfsState::WaitRequestResponse) {
|
|
|
|
return Status::Error("AbortKey: unexpected");
|
|
|
|
}
|
|
|
|
pfs_state_.state = PfsState::Empty;
|
2021-07-05 20:04:23 +02:00
|
|
|
pfs_state_.handshake = mtproto::DhHandshake();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
on_pfs_state_changed();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionCommitKey &commit_key) {
|
|
|
|
if (pfs_state_.state != PfsState::WaitAcceptResponse) {
|
|
|
|
return Status::Error("CommitKey: unexpected");
|
|
|
|
}
|
|
|
|
if (pfs_state_.exchange_id != commit_key.exchange_id_) {
|
|
|
|
return Status::Error("CommitKey: exchange_id mismatch ");
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(!pfs_state_.can_forget_other_key);
|
|
|
|
if (static_cast<int64>(pfs_state_.other_auth_key.id()) != commit_key.key_fingerprint_) {
|
|
|
|
return Status::Error("CommitKey: fingerprint mismatch");
|
|
|
|
}
|
|
|
|
std::swap(pfs_state_.auth_key, pfs_state_.other_auth_key);
|
|
|
|
pfs_state_.can_forget_other_key = true;
|
|
|
|
|
|
|
|
pfs_state_.state = PfsState::Empty;
|
|
|
|
pfs_state_.last_message_id = pfs_state_.message_id;
|
|
|
|
pfs_state_.last_timestamp = Time::now();
|
|
|
|
pfs_state_.last_out_seq_no = seq_no_state_.my_out_seq_no;
|
|
|
|
|
|
|
|
on_pfs_state_changed();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::decryptedMessageActionNoop &noop) {
|
|
|
|
// noop
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SecretChatActor::on_inbound_action(secret_api::DecryptedMessageAction &action, int32 message_id) {
|
|
|
|
// Action may be not about PFS, but we still can use pfs_state_.message_id
|
|
|
|
if (message_id <= pfs_state_.message_id) { // replay protection
|
|
|
|
LOG(INFO) << "Drop old inbound DecryptedMessageAction: " << to_string(action) << tag("message_id", message_id)
|
|
|
|
<< tag("known_message_id", pfs_state_.message_id);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
// if message_id < seq_no_state_.message_id, then SeqNoState with message_id bigger than current message_id is already saved.
|
|
|
|
// And event corresponding to message_id is saved too.
|
|
|
|
//
|
|
|
|
// Also, if SeqNoState with message_id greater than current message_id is not saved, then corresponding action will be
|
|
|
|
// replayed.
|
|
|
|
//
|
2021-03-15 01:32:14 +01:00
|
|
|
// This works only for TTL, not for PFS. Same TTL action may be processed twice.
|
2018-12-31 20:04:05 +01:00
|
|
|
if (message_id < seq_no_state_.message_id) {
|
2021-03-15 01:32:14 +01:00
|
|
|
LOG(INFO) << "Drop old inbound DecryptedMessageAction (non-PFS action): " << to_string(action);
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
pfs_state_.message_id = message_id; // replay protection
|
|
|
|
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(INFO) << "In on_inbound_action: " << to_string(action);
|
2018-12-31 20:04:05 +01:00
|
|
|
Status res;
|
|
|
|
downcast_call(action, [&](auto &obj) { res = this->on_inbound_action(obj); });
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_outbound_action(secret_api::DecryptedMessageAction &action, int32 message_id) {
|
|
|
|
// Action may be not about PFS, but we still can use pfs_state_.message_id
|
|
|
|
if (message_id <= pfs_state_.message_id) { // replay protection
|
|
|
|
LOG(INFO) << "Drop old outbound DecryptedMessageAction: " << to_string(action);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// see comment in on_inbound_action
|
|
|
|
if (message_id < seq_no_state_.message_id) {
|
2021-03-15 01:32:14 +01:00
|
|
|
LOG(INFO) << "Drop old outbound DecryptedMessageAction (non-PFS action): " << to_string(action);
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
pfs_state_.message_id = message_id; // replay protection
|
|
|
|
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(INFO) << "In on_outbound_action: " << to_string(action);
|
2018-12-31 20:04:05 +01:00
|
|
|
downcast_call(action, [&](auto &obj) { this->on_outbound_action(obj); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::request_new_key() {
|
|
|
|
CHECK(!auth_state_.dh_config.empty());
|
|
|
|
|
|
|
|
pfs_state_.state = PfsState::SendRequest;
|
2021-07-05 20:04:23 +02:00
|
|
|
pfs_state_.handshake = mtproto::DhHandshake();
|
2018-12-31 20:04:05 +01:00
|
|
|
pfs_state_.handshake.set_config(auth_state_.dh_config.g, auth_state_.dh_config.prime);
|
|
|
|
pfs_state_.exchange_id = Random::secure_int64();
|
|
|
|
|
|
|
|
// NB: must save explicitly
|
|
|
|
LOG(INFO) << "SAVE PfsState " << pfs_state_;
|
|
|
|
context_->secret_chat_db()->set_value(pfs_state_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecretChatActor::on_promise_error(Status error, string desc) {
|
|
|
|
if (context_->close_flag()) {
|
2018-07-23 23:12:53 +02:00
|
|
|
LOG(DEBUG) << "Ignore " << tag("promise", desc) << error;
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG(FATAL) << "Failed: " << tag("promise", desc) << error;
|
|
|
|
}
|
|
|
|
|
2018-04-23 20:41:34 +02:00
|
|
|
constexpr int32 SecretChatActor::MAX_RESEND_COUNT;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|