2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
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"
|
|
|
|
#include "td/mtproto/crypto.h"
|
2019-01-31 12:26:06 +01:00
|
|
|
#include "td/mtproto/DhHandshake.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#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
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
using Context = AuthKeyHandshakeContext;
|
|
|
|
enum class Mode { Unknown, Main, Temp };
|
|
|
|
AuthKey auth_key;
|
|
|
|
double server_time_diff = 0;
|
|
|
|
uint64 server_salt = 0;
|
|
|
|
|
2019-07-22 13:22:31 +02:00
|
|
|
bool is_ready_for_start() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
Status start_main(Callback *connection) TD_WARN_UNUSED_RESULT;
|
2019-02-10 01:45:15 +01:00
|
|
|
Status start_tmp(Callback *connection, int32 expires_in) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-07-22 13:22:31 +02:00
|
|
|
bool is_ready_for_message(const UInt128 &message_nonce) const;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-07-22 13:22:31 +02:00
|
|
|
bool is_ready_for_finish() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
void on_finish();
|
|
|
|
|
2019-02-10 01:45:15 +01:00
|
|
|
AuthKeyHandshake(int32 dc_id, int32 expires_in) {
|
2018-05-23 18:49:05 +02:00
|
|
|
dc_id_ = dc_id;
|
2019-02-10 01:45:15 +01:00
|
|
|
if (expires_in == 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
mode_ = Mode::Main;
|
|
|
|
} else {
|
|
|
|
mode_ = Mode::Temp;
|
2019-02-10 01:45:15 +01:00
|
|
|
expires_in_ = expires_in;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void init_main() {
|
|
|
|
clear();
|
|
|
|
mode_ = Mode::Main;
|
|
|
|
}
|
2019-02-10 01:45:15 +01:00
|
|
|
void init_temp(int32 expires_in) {
|
2018-12-31 20:04:05 +01:00
|
|
|
clear();
|
|
|
|
mode_ = Mode::Temp;
|
2019-02-10 01:45:15 +01:00
|
|
|
expires_in_ = expires_in;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
void resume(Callback *connection);
|
|
|
|
Status on_message(Slice message, Callback *connection, Context *context) TD_WARN_UNUSED_RESULT;
|
2019-07-22 13:22:31 +02:00
|
|
|
bool is_ready() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
return is_ready_for_finish();
|
|
|
|
}
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
private:
|
|
|
|
using State = enum { Start, ResPQ, ServerDHParams, DHGenResponse, Finish };
|
|
|
|
State state_ = Start;
|
|
|
|
Mode mode_ = Mode::Unknown;
|
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
|
|
|
|
|
|
|
UInt128 nonce;
|
|
|
|
UInt128 server_nonce;
|
|
|
|
UInt256 new_nonce;
|
|
|
|
UInt256 tmp_aes_key;
|
|
|
|
UInt256 tmp_aes_iv;
|
|
|
|
|
|
|
|
BufferSlice last_query_;
|
|
|
|
|
|
|
|
template <class DataT>
|
|
|
|
Result<size_t> fill_data_with_hash(uint8 *data_with_hash, const DataT &data) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
void send(Callback *connection, const Storer &storer);
|
|
|
|
void do_send(Callback *connection, const Storer &storer);
|
|
|
|
|
|
|
|
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
|