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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/mtproto/AuthKey.h"
|
2020-05-17 16:07:16 +02:00
|
|
|
#include "td/mtproto/RSA.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-06-09 15:38:38 +02:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
2019-02-12 17:48:52 +01:00
|
|
|
#include "td/utils/StorerBase.h"
|
2018-12-19 22:18:53 +01:00
|
|
|
#include "td/utils/UInt.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace mtproto {
|
2018-07-27 02:54:25 +02:00
|
|
|
|
2021-07-05 20:20:07 +02:00
|
|
|
class DhCallback;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class AuthKeyHandshakeContext {
|
|
|
|
public:
|
|
|
|
virtual ~AuthKeyHandshakeContext() = default;
|
|
|
|
virtual DhCallback *get_dh_callback() = 0;
|
|
|
|
virtual PublicRsaKeyInterface *get_public_rsa_key_interface() = 0;
|
|
|
|
};
|
2018-07-27 02:54:25 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class AuthKeyHandshake {
|
|
|
|
public:
|
|
|
|
class Callback {
|
|
|
|
public:
|
|
|
|
Callback() = default;
|
|
|
|
Callback(const Callback &) = delete;
|
|
|
|
Callback &operator=(const Callback &) = delete;
|
|
|
|
virtual ~Callback() = default;
|
|
|
|
virtual void send_no_crypto(const Storer &storer) = 0;
|
|
|
|
};
|
|
|
|
|
2021-07-19 04:09:20 +02:00
|
|
|
AuthKeyHandshake(int32 dc_id, int32 expires_in);
|
2020-01-08 18:30:07 +01:00
|
|
|
|
2021-07-22 18:23:08 +02:00
|
|
|
void set_timeout_in(double timeout_in);
|
|
|
|
|
2020-01-08 18:30:07 +01:00
|
|
|
bool is_ready_for_finish() const;
|
|
|
|
|
2021-07-19 04:01:14 +02:00
|
|
|
void on_finish();
|
2020-01-08 18:30:07 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void resume(Callback *connection);
|
2020-01-08 18:30:07 +01:00
|
|
|
|
|
|
|
Status on_message(Slice message, Callback *connection, AuthKeyHandshakeContext *context) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void clear();
|
|
|
|
|
2020-01-30 02:06:15 +01:00
|
|
|
const AuthKey &get_auth_key() const {
|
|
|
|
return auth_key_;
|
|
|
|
}
|
|
|
|
|
2020-01-08 18:30:07 +01:00
|
|
|
AuthKey release_auth_key() {
|
|
|
|
return std::move(auth_key_);
|
|
|
|
}
|
|
|
|
|
|
|
|
double get_server_time_diff() const {
|
|
|
|
return server_time_diff_;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 get_server_salt() const {
|
|
|
|
return server_salt_;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
2021-07-19 04:09:20 +02:00
|
|
|
enum State : int32 { Start, ResPQ, ServerDHParams, DHGenResponse, Finish };
|
2018-12-31 20:04:05 +01:00
|
|
|
State state_ = Start;
|
2021-07-19 04:09:20 +02:00
|
|
|
enum class Mode : int32 { Main, Temp };
|
|
|
|
Mode mode_ = Mode::Main;
|
2018-10-26 16:11:20 +02:00
|
|
|
int32 dc_id_ = 0;
|
2019-02-10 01:45:15 +01:00
|
|
|
int32 expires_in_ = 0;
|
|
|
|
double expires_at_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-11-27 20:22:51 +01:00
|
|
|
double start_time_ = 0;
|
|
|
|
double timeout_in_ = 0;
|
2021-07-22 18:23:08 +02:00
|
|
|
|
2020-01-08 18:30:07 +01:00
|
|
|
AuthKey auth_key_;
|
|
|
|
double server_time_diff_ = 0;
|
|
|
|
uint64 server_salt_ = 0;
|
|
|
|
|
2021-07-05 20:26:43 +02:00
|
|
|
UInt128 nonce_;
|
|
|
|
UInt128 server_nonce_;
|
|
|
|
UInt256 new_nonce_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-02-21 09:02:10 +01:00
|
|
|
string last_query_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-21 20:31:59 +02:00
|
|
|
template <class T>
|
|
|
|
static string store_object(const T &object);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void send(Callback *connection, const Storer &storer);
|
2021-10-18 18:26:14 +02:00
|
|
|
static void do_send(Callback *connection, const Storer &storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Status on_start(Callback *connection) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_res_pq(Slice message, Callback *connection, PublicRsaKeyInterface *public_rsa_key) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_server_dh_params(Slice message, Callback *connection, DhCallback *dh_callback) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_dh_gen_response(Slice message, Callback *connection) TD_WARN_UNUSED_RESULT;
|
|
|
|
};
|
2018-07-27 02:54:25 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|