2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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/mtproto/Handshake.h"
|
|
|
|
|
2021-07-16 17:58:11 +02:00
|
|
|
#include "td/mtproto/DhCallback.h"
|
2021-07-05 20:20:07 +02:00
|
|
|
#include "td/mtproto/DhHandshake.h"
|
2019-07-26 23:39:39 +02:00
|
|
|
#include "td/mtproto/KDF.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/mtproto_api.h"
|
2021-07-05 19:38:39 +02:00
|
|
|
#include "td/mtproto/utils.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-12-19 22:57:56 +01:00
|
|
|
#include "td/utils/as.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/buffer.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.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/Random.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/Time.h"
|
|
|
|
#include "td/utils/tl_parsers.h"
|
|
|
|
|
2021-07-08 15:39:34 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
|
|
|
namespace mtproto {
|
|
|
|
|
2020-03-15 23:42:24 +01:00
|
|
|
template <class T>
|
|
|
|
static Result<typename T::ReturnType> fetch_result(Slice message, bool check_end = true) {
|
|
|
|
TlParser parser(message);
|
|
|
|
auto result = T::fetch_result(parser);
|
|
|
|
|
|
|
|
if (check_end) {
|
|
|
|
parser.fetch_end();
|
|
|
|
}
|
|
|
|
const char *error = parser.get_error();
|
|
|
|
if (error != nullptr) {
|
|
|
|
LOG(ERROR) << "Can't parse: " << format::as_hex_dump<4>(message);
|
|
|
|
return Status::Error(500, Slice(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
|
2021-07-19 04:09:20 +02:00
|
|
|
AuthKeyHandshake::AuthKeyHandshake(int32 dc_id, int32 expires_in)
|
2021-07-22 18:23:08 +02:00
|
|
|
: mode_(expires_in == 0 ? Mode::Main : Mode::Temp)
|
|
|
|
, dc_id_(dc_id)
|
|
|
|
, expires_in_(expires_in)
|
|
|
|
, timeout_at_(Time::now() + 1e9) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthKeyHandshake::set_timeout_in(double timeout_in) {
|
|
|
|
timeout_at_ = Time::now() + timeout_in;
|
2021-07-19 04:09:20 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void AuthKeyHandshake::clear() {
|
|
|
|
last_query_ = BufferSlice();
|
|
|
|
state_ = Start;
|
2021-07-22 18:23:08 +02:00
|
|
|
timeout_at_ = Time::now() + 1e9;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-22 13:22:31 +02:00
|
|
|
bool AuthKeyHandshake::is_ready_for_finish() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
return state_ == Finish;
|
|
|
|
}
|
2021-07-19 04:01:14 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void AuthKeyHandshake::on_finish() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2021-07-05 19:38:39 +02:00
|
|
|
string AuthKeyHandshake::store_object(const mtproto_api::Object &object) {
|
|
|
|
auto storer = create_storer(object);
|
|
|
|
size_t size = storer.size();
|
|
|
|
string result(size, '\0');
|
|
|
|
auto real_size = storer.store(MutableSlice(result).ubegin());
|
|
|
|
CHECK(real_size == size);
|
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status AuthKeyHandshake::on_res_pq(Slice message, Callback *connection, PublicRsaKeyInterface *public_rsa_key) {
|
2018-06-15 17:11:48 +02:00
|
|
|
TRY_RESULT(res_pq, fetch_result<mtproto_api::req_pq_multi>(message, false));
|
2021-07-05 20:26:43 +02:00
|
|
|
if (res_pq->nonce_ != nonce_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("Nonce mismatch");
|
|
|
|
}
|
|
|
|
|
2021-07-05 20:26:43 +02:00
|
|
|
server_nonce_ = res_pq->server_nonce_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-05 21:09:45 +02:00
|
|
|
auto r_rsa_key = public_rsa_key->get_rsa_key(res_pq->server_public_key_fingerprints_);
|
|
|
|
if (r_rsa_key.is_error()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
public_rsa_key->drop_keys();
|
2021-07-05 21:09:45 +02:00
|
|
|
return r_rsa_key.move_as_error();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-06 17:31:43 +02:00
|
|
|
auto rsa_key = r_rsa_key.move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
string p, q;
|
|
|
|
if (pq_factorize(res_pq->pq_, &p, &q) == -1) {
|
|
|
|
return Status::Error("Failed to factorize");
|
|
|
|
}
|
|
|
|
|
2021-07-05 20:26:43 +02:00
|
|
|
Random::secure_bytes(new_nonce_.raw, sizeof(new_nonce_));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-05 19:38:39 +02:00
|
|
|
string data;
|
2018-12-31 20:04:05 +01:00
|
|
|
switch (mode_) {
|
|
|
|
case Mode::Main:
|
2021-07-05 20:26:43 +02:00
|
|
|
data = store_object(mtproto_api::p_q_inner_data_dc(res_pq->pq_, p, q, nonce_, server_nonce_, new_nonce_, dc_id_));
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
case Mode::Temp:
|
2021-07-05 20:26:43 +02:00
|
|
|
data = store_object(mtproto_api::p_q_inner_data_temp_dc(res_pq->pq_, p, q, nonce_, server_nonce_, new_nonce_,
|
|
|
|
dc_id_, expires_in_));
|
2019-02-10 01:45:15 +01:00
|
|
|
expires_at_ = Time::now() + expires_in_;
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2021-07-05 19:38:39 +02:00
|
|
|
|
2021-07-08 15:39:34 +02:00
|
|
|
string encrypted_data(256, '\0');
|
|
|
|
auto data_size = data.size();
|
|
|
|
if (data_size > 144) {
|
2021-07-05 19:38:39 +02:00
|
|
|
return Status::Error("Too big data");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-05 19:38:39 +02:00
|
|
|
|
2021-07-08 15:39:34 +02:00
|
|
|
data.resize(192);
|
|
|
|
Random::secure_bytes(MutableSlice(data).substr(data_size));
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
string aes_key(32, '\0');
|
|
|
|
Random::secure_bytes(MutableSlice(aes_key));
|
|
|
|
|
|
|
|
string data_with_hash = data + sha256(aes_key + data);
|
|
|
|
std::reverse(data_with_hash.begin(), data_with_hash.begin() + data.size());
|
|
|
|
|
|
|
|
string decrypted_data(256, '\0');
|
|
|
|
string aes_iv(32, '\0');
|
|
|
|
aes_ige_encrypt(aes_key, aes_iv, data_with_hash, MutableSlice(decrypted_data).substr(32));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-08 15:39:34 +02:00
|
|
|
auto hash = sha256(MutableSlice(decrypted_data).substr(32));
|
|
|
|
for (size_t i = 0; i < 32; i++) {
|
|
|
|
decrypted_data[i] = static_cast<char>(aes_key[i] ^ hash[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rsa_key.rsa.encrypt(decrypted_data, encrypted_data)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-06 17:31:43 +02:00
|
|
|
mtproto_api::req_DH_params req_dh_params(nonce_, server_nonce_, p, q, rsa_key.fingerprint, encrypted_data);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
send(connection, create_storer(req_dh_params));
|
|
|
|
state_ = ServerDHParams;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status AuthKeyHandshake::on_server_dh_params(Slice message, Callback *connection, DhCallback *dh_callback) {
|
2021-07-05 18:45:31 +02:00
|
|
|
TRY_RESULT(dh_params, fetch_result<mtproto_api::req_DH_params>(message, false));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// server_DH_params_ok#d0e8075c nonce:int128 server_nonce:int128 encrypted_answer:string = Server_DH_Params;
|
2021-07-05 20:26:43 +02:00
|
|
|
if (dh_params->nonce_ != nonce_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("Nonce mismatch");
|
|
|
|
}
|
2021-07-05 20:26:43 +02:00
|
|
|
if (dh_params->server_nonce_ != server_nonce_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("Server nonce mismatch");
|
|
|
|
}
|
|
|
|
if (dh_params->encrypted_answer_.size() & 15) {
|
|
|
|
return Status::Error("Bad padding for encrypted part");
|
|
|
|
}
|
|
|
|
|
2021-07-05 20:26:43 +02:00
|
|
|
UInt256 tmp_aes_key;
|
|
|
|
UInt256 tmp_aes_iv;
|
|
|
|
tmp_KDF(server_nonce_, new_nonce_, &tmp_aes_key, &tmp_aes_iv);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto save_tmp_aes_iv = tmp_aes_iv;
|
|
|
|
// encrypted_answer := AES256_ige_encrypt (answer_with_hash, tmp_aes_key, tmp_aes_iv);
|
|
|
|
MutableSlice answer(const_cast<char *>(dh_params->encrypted_answer_.begin()), dh_params->encrypted_answer_.size());
|
2019-08-12 15:53:17 +02:00
|
|
|
aes_ige_decrypt(as_slice(tmp_aes_key), as_slice(tmp_aes_iv), answer, answer);
|
2018-12-31 20:04:05 +01:00
|
|
|
tmp_aes_iv = save_tmp_aes_iv;
|
|
|
|
|
|
|
|
// answer_with_hash := SHA1(answer) + answer + (0-15 random bytes)
|
|
|
|
TlParser answer_parser(answer);
|
|
|
|
UInt<160> answer_sha1 = answer_parser.fetch_binary<UInt<160>>();
|
|
|
|
int32 id = answer_parser.fetch_int();
|
|
|
|
if (id != mtproto_api::server_DH_inner_data::ID) {
|
|
|
|
return Status::Error("Failed to fetch server_DH_inner_data");
|
|
|
|
}
|
|
|
|
mtproto_api::server_DH_inner_data dh_inner_data(answer_parser);
|
|
|
|
if (answer_parser.get_error() != nullptr) {
|
|
|
|
return Status::Error("Failed to fetch server_DH_inner_data");
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t pad = answer_parser.get_left_len();
|
|
|
|
if (pad >= 16) {
|
|
|
|
return Status::Error("Too much pad");
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t dh_inner_data_size = answer.size() - pad - 20;
|
|
|
|
UInt<160> answer_real_sha1;
|
2018-11-06 21:25:09 +01:00
|
|
|
sha1(answer.substr(20, dh_inner_data_size), answer_real_sha1.raw);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (answer_sha1 != answer_real_sha1) {
|
|
|
|
return Status::Error("SHA1 mismatch");
|
|
|
|
}
|
|
|
|
|
2021-07-05 20:26:43 +02:00
|
|
|
if (dh_inner_data.nonce_ != nonce_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("Nonce mismatch");
|
|
|
|
}
|
2021-07-05 20:26:43 +02:00
|
|
|
if (dh_inner_data.server_nonce_ != server_nonce_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("Server nonce mismatch");
|
|
|
|
}
|
|
|
|
|
2020-01-08 18:30:07 +01:00
|
|
|
server_time_diff_ = dh_inner_data.server_time_ - Time::now();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-11 12:29:06 +02:00
|
|
|
DhHandshake handshake;
|
|
|
|
handshake.set_config(dh_inner_data.g_, dh_inner_data.dh_prime_);
|
|
|
|
handshake.set_g_a(dh_inner_data.g_a_);
|
|
|
|
TRY_STATUS(handshake.run_checks(false, dh_callback));
|
|
|
|
string g_b = handshake.get_g_b();
|
|
|
|
auto auth_key_params = handshake.gen_key();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-05 20:26:43 +02:00
|
|
|
auto data = store_object(mtproto_api::client_DH_inner_data(nonce_, server_nonce_, 0, g_b));
|
2021-07-05 19:38:39 +02:00
|
|
|
size_t encrypted_data_size = 20 + data.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t encrypted_data_size_with_pad = (encrypted_data_size + 15) & -16;
|
2021-07-05 19:38:39 +02:00
|
|
|
string encrypted_data_str(encrypted_data_size_with_pad, '\0');
|
2018-12-31 20:04:05 +01:00
|
|
|
MutableSlice encrypted_data = encrypted_data_str;
|
2021-07-05 19:38:39 +02:00
|
|
|
sha1(data, encrypted_data.ubegin());
|
|
|
|
encrypted_data.substr(20, data.size()).copy_from(data);
|
2018-12-31 20:04:05 +01:00
|
|
|
Random::secure_bytes(encrypted_data.ubegin() + encrypted_data_size,
|
|
|
|
encrypted_data_size_with_pad - encrypted_data_size);
|
2021-07-05 20:26:43 +02:00
|
|
|
tmp_KDF(server_nonce_, new_nonce_, &tmp_aes_key, &tmp_aes_iv);
|
2019-08-12 15:53:17 +02:00
|
|
|
aes_ige_encrypt(as_slice(tmp_aes_key), as_slice(tmp_aes_iv), encrypted_data, encrypted_data);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-05 20:26:43 +02:00
|
|
|
mtproto_api::set_client_DH_params set_client_dh_params(nonce_, server_nonce_, encrypted_data);
|
2018-12-31 20:04:05 +01:00
|
|
|
send(connection, create_storer(set_client_dh_params));
|
|
|
|
|
2020-01-08 18:30:07 +01:00
|
|
|
auth_key_ = AuthKey(auth_key_params.first, std::move(auth_key_params.second));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (mode_ == Mode::Temp) {
|
2020-01-08 18:30:07 +01:00
|
|
|
auth_key_.set_expires_at(expires_at_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-01-08 18:30:07 +01:00
|
|
|
auth_key_.set_created_at(dh_inner_data.server_time_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-05 20:26:43 +02:00
|
|
|
server_salt_ = as<int64>(new_nonce_.raw) ^ as<int64>(server_nonce_.raw);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
state_ = DHGenResponse;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status AuthKeyHandshake::on_dh_gen_response(Slice message, Callback *connection) {
|
2018-06-15 17:11:48 +02:00
|
|
|
TRY_RESULT(answer, fetch_result<mtproto_api::set_client_DH_params>(message, false));
|
2018-12-31 20:04:05 +01:00
|
|
|
switch (answer->get_id()) {
|
|
|
|
case mtproto_api::dh_gen_ok::ID:
|
|
|
|
state_ = Finish;
|
|
|
|
break;
|
|
|
|
case mtproto_api::dh_gen_fail::ID:
|
|
|
|
return Status::Error("DhGenFail");
|
|
|
|
case mtproto_api::dh_gen_retry::ID:
|
|
|
|
return Status::Error("DhGenRetry");
|
|
|
|
default:
|
|
|
|
return Status::Error("Unknown set_client_DH_params response");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-08-11 12:29:06 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void AuthKeyHandshake::send(Callback *connection, const Storer &storer) {
|
2018-07-06 22:33:11 +02:00
|
|
|
auto size = storer.size();
|
|
|
|
auto writer = BufferWriter{size, 0, 0};
|
|
|
|
auto real_size = storer.store(writer.as_slice().ubegin());
|
|
|
|
CHECK(real_size == size);
|
2018-12-31 20:04:05 +01:00
|
|
|
last_query_ = writer.as_buffer_slice();
|
|
|
|
return do_send(connection, create_storer(last_query_.as_slice()));
|
|
|
|
}
|
2018-08-11 12:29:06 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void AuthKeyHandshake::do_send(Callback *connection, const Storer &storer) {
|
|
|
|
return connection->send_no_crypto(storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthKeyHandshake::resume(Callback *connection) {
|
|
|
|
if (state_ == Start) {
|
|
|
|
return on_start(connection).ignore();
|
|
|
|
}
|
|
|
|
if (state_ == Finish) {
|
|
|
|
LOG(ERROR) << "State is Finish during resume. UNREACHABLE";
|
|
|
|
return clear();
|
|
|
|
}
|
|
|
|
if (last_query_.empty()) {
|
|
|
|
LOG(ERROR) << "Last query empty! UNREACHABLE " << state_;
|
|
|
|
return clear();
|
|
|
|
}
|
2021-07-19 04:01:14 +02:00
|
|
|
LOG(INFO) << "Resume handshake";
|
2018-12-31 20:04:05 +01:00
|
|
|
do_send(connection, create_storer(last_query_.as_slice()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Status AuthKeyHandshake::on_start(Callback *connection) {
|
|
|
|
if (state_ != Start) {
|
|
|
|
clear();
|
|
|
|
return Status::Error(PSLICE() << "on_start called after start " << tag("state", state_));
|
|
|
|
}
|
2021-07-05 20:26:43 +02:00
|
|
|
Random::secure_bytes(nonce_.raw, sizeof(nonce_));
|
|
|
|
send(connection, create_storer(mtproto_api::req_pq_multi(nonce_)));
|
2018-12-31 20:04:05 +01:00
|
|
|
state_ = ResPQ;
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2020-01-08 18:30:07 +01:00
|
|
|
Status AuthKeyHandshake::on_message(Slice message, Callback *connection, AuthKeyHandshakeContext *context) {
|
2018-12-31 20:04:05 +01:00
|
|
|
Status status = [&] {
|
2021-07-22 18:23:08 +02:00
|
|
|
if (Time::now() >= timeout_at_) {
|
|
|
|
return Status::Error("Handshake timeout expired");
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
switch (state_) {
|
|
|
|
case ResPQ:
|
|
|
|
return on_res_pq(message, connection, context->get_public_rsa_key_interface());
|
|
|
|
case ServerDHParams:
|
|
|
|
return on_server_dh_params(message, connection, context->get_dh_callback());
|
|
|
|
case DHGenResponse:
|
|
|
|
return on_dh_gen_response(message, connection);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
if (status.is_error()) {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|