diff --git a/td/mtproto/DhHandshake.h b/td/mtproto/DhHandshake.h index 8a2a24fc3..bd94a2756 100644 --- a/td/mtproto/DhHandshake.h +++ b/td/mtproto/DhHandshake.h @@ -105,7 +105,7 @@ class DhHandshake { string prime_str_; BigNum prime_; BigNum g_; - int32 g_int_; + int32 g_int_ = 0; BigNum b_; BigNum g_b_; BigNum g_a_; diff --git a/td/mtproto/Handshake.cpp b/td/mtproto/Handshake.cpp index 30d8ed5bc..9b66daa59 100644 --- a/td/mtproto/Handshake.cpp +++ b/td/mtproto/Handshake.cpp @@ -53,7 +53,7 @@ bool AuthKeyHandshake::is_ready_for_start() const { return state_ == Start; } bool AuthKeyHandshake::is_ready_for_message(const UInt128 &message_nonce) const { - return state_ != Finish && state_ != Start && nonce == message_nonce; + return state_ != Finish && state_ != Start && nonce_ == message_nonce; } bool AuthKeyHandshake::is_ready_for_finish() const { return state_ == Finish; @@ -73,11 +73,11 @@ string AuthKeyHandshake::store_object(const mtproto_api::Object &object) { Status AuthKeyHandshake::on_res_pq(Slice message, Callback *connection, PublicRsaKeyInterface *public_rsa_key) { TRY_RESULT(res_pq, fetch_result(message, false)); - if (res_pq->nonce_ != nonce) { + if (res_pq->nonce_ != nonce_) { return Status::Error("Nonce mismatch"); } - server_nonce = res_pq->server_nonce_; + server_nonce_ = res_pq->server_nonce_; auto r_rsa = public_rsa_key->get_rsa(res_pq->server_public_key_fingerprints_); if (r_rsa.is_error()) { @@ -92,16 +92,16 @@ Status AuthKeyHandshake::on_res_pq(Slice message, Callback *connection, PublicRs return Status::Error("Failed to factorize"); } - Random::secure_bytes(new_nonce.raw, sizeof(new_nonce)); + Random::secure_bytes(new_nonce_.raw, sizeof(new_nonce_)); string data; switch (mode_) { case Mode::Main: - data = store_object(mtproto_api::p_q_inner_data_dc(res_pq->pq_, p, q, nonce, server_nonce, new_nonce, dc_id_)); + data = store_object(mtproto_api::p_q_inner_data_dc(res_pq->pq_, p, q, nonce_, server_nonce_, new_nonce_, dc_id_)); break; case Mode::Temp: - 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_)); + 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_)); expires_at_ = Time::now() + expires_in_; break; case Mode::Unknown: @@ -127,7 +127,7 @@ Status AuthKeyHandshake::on_res_pq(Slice message, Callback *connection, PublicRs // req_DH_params#d712e4be nonce:int128 server_nonce:int128 p:string q:string public_key_fingerprint:long // encrypted_data:string = Server_DH_Params - mtproto_api::req_DH_params req_dh_params(nonce, server_nonce, p, q, rsa_fingerprint, encrypted_data); + mtproto_api::req_DH_params req_dh_params(nonce_, server_nonce_, p, q, rsa_fingerprint, encrypted_data); send(connection, create_storer(req_dh_params)); state_ = ServerDHParams; @@ -138,17 +138,19 @@ Status AuthKeyHandshake::on_server_dh_params(Slice message, Callback *connection TRY_RESULT(dh_params, fetch_result(message, false)); // server_DH_params_ok#d0e8075c nonce:int128 server_nonce:int128 encrypted_answer:string = Server_DH_Params; - if (dh_params->nonce_ != nonce) { + if (dh_params->nonce_ != nonce_) { return Status::Error("Nonce mismatch"); } - if (dh_params->server_nonce_ != server_nonce) { + if (dh_params->server_nonce_ != server_nonce_) { return Status::Error("Server nonce mismatch"); } if (dh_params->encrypted_answer_.size() & 15) { return Status::Error("Bad padding for encrypted part"); } - tmp_KDF(server_nonce, new_nonce, &tmp_aes_key, &tmp_aes_iv); + UInt256 tmp_aes_key; + UInt256 tmp_aes_iv; + tmp_KDF(server_nonce_, new_nonce_, &tmp_aes_key, &tmp_aes_iv); 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(dh_params->encrypted_answer_.begin()), dh_params->encrypted_answer_.size()); @@ -179,10 +181,10 @@ Status AuthKeyHandshake::on_server_dh_params(Slice message, Callback *connection return Status::Error("SHA1 mismatch"); } - if (dh_inner_data.nonce_ != nonce) { + if (dh_inner_data.nonce_ != nonce_) { return Status::Error("Nonce mismatch"); } - if (dh_inner_data.server_nonce_ != server_nonce) { + if (dh_inner_data.server_nonce_ != server_nonce_) { return Status::Error("Server nonce mismatch"); } @@ -195,7 +197,7 @@ Status AuthKeyHandshake::on_server_dh_params(Slice message, Callback *connection string g_b = handshake.get_g_b(); auto auth_key_params = handshake.gen_key(); - auto data = store_object(mtproto_api::client_DH_inner_data(nonce, server_nonce, 0, g_b)); + auto data = store_object(mtproto_api::client_DH_inner_data(nonce_, server_nonce_, 0, g_b)); size_t encrypted_data_size = 20 + data.size(); size_t encrypted_data_size_with_pad = (encrypted_data_size + 15) & -16; string encrypted_data_str(encrypted_data_size_with_pad, '\0'); @@ -204,10 +206,10 @@ Status AuthKeyHandshake::on_server_dh_params(Slice message, Callback *connection encrypted_data.substr(20, data.size()).copy_from(data); Random::secure_bytes(encrypted_data.ubegin() + encrypted_data_size, encrypted_data_size_with_pad - encrypted_data_size); - tmp_KDF(server_nonce, new_nonce, &tmp_aes_key, &tmp_aes_iv); + tmp_KDF(server_nonce_, new_nonce_, &tmp_aes_key, &tmp_aes_iv); aes_ige_encrypt(as_slice(tmp_aes_key), as_slice(tmp_aes_iv), encrypted_data, encrypted_data); - mtproto_api::set_client_DH_params set_client_dh_params(nonce, server_nonce, encrypted_data); + mtproto_api::set_client_DH_params set_client_dh_params(nonce_, server_nonce_, encrypted_data); send(connection, create_storer(set_client_dh_params)); auth_key_ = AuthKey(auth_key_params.first, std::move(auth_key_params.second)); @@ -216,7 +218,7 @@ Status AuthKeyHandshake::on_server_dh_params(Slice message, Callback *connection } auth_key_.set_created_at(dh_inner_data.server_time_); - server_salt_ = as(new_nonce.raw) ^ as(server_nonce.raw); + server_salt_ = as(new_nonce_.raw) ^ as(server_nonce_.raw); state_ = DHGenResponse; return Status::OK(); @@ -283,8 +285,8 @@ Status AuthKeyHandshake::on_start(Callback *connection) { clear(); return Status::Error(PSLICE() << "on_start called after start " << tag("state", state_)); } - Random::secure_bytes(nonce.raw, sizeof(nonce)); - send(connection, create_storer(mtproto_api::req_pq_multi(nonce))); + Random::secure_bytes(nonce_.raw, sizeof(nonce_)); + send(connection, create_storer(mtproto_api::req_pq_multi(nonce_))); state_ = ResPQ; return Status::OK(); diff --git a/td/mtproto/Handshake.h b/td/mtproto/Handshake.h index ae94e8853..82debe9f2 100644 --- a/td/mtproto/Handshake.h +++ b/td/mtproto/Handshake.h @@ -113,11 +113,9 @@ class AuthKeyHandshake { double server_time_diff_ = 0; uint64 server_salt_ = 0; - UInt128 nonce; - UInt128 server_nonce; - UInt256 new_nonce; - UInt256 tmp_aes_key; - UInt256 tmp_aes_iv; + UInt128 nonce_; + UInt128 server_nonce_; + UInt256 new_nonce_; BufferSlice last_query_;