1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 09:58:59 +01:00

Reorder integrity check

This commit is contained in:
Daniil Gentili 2021-11-23 21:00:32 +01:00
parent 8f5a539f60
commit 045edf81b1
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 7 additions and 7 deletions

View File

@ -156,6 +156,9 @@ class ReadLoop extends SignalLoop
$encrypted_data = \substr($encrypted_data, 0, -$protocol_padding);
}
$decrypted_data = Crypt::igeDecrypt($encrypted_data, $aes_key, $aes_iv);
if ($message_key != \substr(\hash('sha256', \substr($shared->getTempAuthKey()->getAuthKey(), 96, 32).$decrypted_data, true), 8, 16)) {
throw new \danog\MadelineProto\SecurityException('msg_key mismatch');
}
/*
$server_salt = substr($decrypted_data, 0, 8);
if ($server_salt != $shared->getTempAuthKey()->getServerSalt()) {
@ -163,7 +166,7 @@ class ReadLoop extends SignalLoop
}
*/
$session_id = \substr($decrypted_data, 8, 8);
if ($session_id != $connection->session_id) {
if ($session_id !== $connection->session_id) {
$API->logger->logger("Session ID mismatch", Logger::FATAL_ERROR);
$connection->resetSession();
throw new NothingInTheSocketException();
@ -188,9 +191,6 @@ class ReadLoop extends SignalLoop
throw new \danog\MadelineProto\SecurityException('message_data_length not divisible by 4');
}
$message_data = \substr($decrypted_data, 32, $message_data_length);
if ($message_key != \substr(\hash('sha256', \substr($shared->getTempAuthKey()->getAuthKey(), 96, 32).$decrypted_data, true), 8, 16)) {
throw new \danog\MadelineProto\SecurityException('msg_key mismatch');
}
} else {
$API->logger->logger('Got unknown auth_key id', Logger::ERROR);
return -404;

View File

@ -184,14 +184,14 @@ trait MessageHandler
{
list($aes_key, $aes_iv) = Crypt::aesCalculate($message_key, $this->secret_chats[$chat_id][$old ? 'old_key' : 'key']['auth_key'], !$this->secret_chats[$chat_id]['admin']);
$decrypted_data = Crypt::igeDecrypt($encrypted_data, $aes_key, $aes_iv);
if ($message_key != \substr(\hash('sha256', \substr($this->secret_chats[$chat_id][$old ? 'old_key' : 'key']['auth_key'], 88 + ($this->secret_chats[$chat_id]['admin'] ? 8 : 0), 32).$decrypted_data, true), 8, 16)) {
throw new \danog\MadelineProto\SecurityException('Msg_key mismatch');
}
$message_data_length = \unpack('V', \substr($decrypted_data, 0, 4))[1];
$message_data = \substr($decrypted_data, 4, $message_data_length);
if ($message_data_length > \strlen($decrypted_data)) {
throw new \danog\MadelineProto\SecurityException(\danog\MadelineProto\Lang::$current_lang['msg_data_length_too_big']);
}
if ($message_key != \substr(\hash('sha256', \substr($this->secret_chats[$chat_id][$old ? 'old_key' : 'key']['auth_key'], 88 + ($this->secret_chats[$chat_id]['admin'] ? 8 : 0), 32).$decrypted_data, true), 8, 16)) {
throw new \danog\MadelineProto\SecurityException('Msg_key mismatch');
}
if (\strlen($decrypted_data) - 4 - $message_data_length < 12) {
throw new \danog\MadelineProto\SecurityException('padding is too small');
}