mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-13 17:29:44 +00:00
use a dedicated exception class for throwing exceptions on decrypt failure
This commit is contained in:
parent
5a33dbd4c6
commit
dd37d286f0
@ -36,6 +36,7 @@ use pocketmine\math\Vector3;
|
|||||||
use pocketmine\network\BadPacketException;
|
use pocketmine\network\BadPacketException;
|
||||||
use pocketmine\network\mcpe\compression\CompressBatchPromise;
|
use pocketmine\network\mcpe\compression\CompressBatchPromise;
|
||||||
use pocketmine\network\mcpe\compression\Zlib;
|
use pocketmine\network\mcpe\compression\Zlib;
|
||||||
|
use pocketmine\network\mcpe\encryption\DecryptionException;
|
||||||
use pocketmine\network\mcpe\encryption\NetworkCipher;
|
use pocketmine\network\mcpe\encryption\NetworkCipher;
|
||||||
use pocketmine\network\mcpe\encryption\PrepareEncryptionTask;
|
use pocketmine\network\mcpe\encryption\PrepareEncryptionTask;
|
||||||
use pocketmine\network\mcpe\handler\DeathPacketHandler;
|
use pocketmine\network\mcpe\handler\DeathPacketHandler;
|
||||||
@ -261,7 +262,7 @@ class NetworkSession{
|
|||||||
Timings::$playerNetworkReceiveDecryptTimer->startTiming();
|
Timings::$playerNetworkReceiveDecryptTimer->startTiming();
|
||||||
try{
|
try{
|
||||||
$payload = $this->cipher->decrypt($payload);
|
$payload = $this->cipher->decrypt($payload);
|
||||||
}catch(\UnexpectedValueException $e){
|
}catch(DecryptionException $e){
|
||||||
$this->logger->debug("Encrypted packet: " . base64_encode($payload));
|
$this->logger->debug("Encrypted packet: " . base64_encode($payload));
|
||||||
throw BadPacketException::wrap($e, "Packet decryption error");
|
throw BadPacketException::wrap($e, "Packet decryption error");
|
||||||
}finally{
|
}finally{
|
||||||
|
28
src/network/mcpe/encryption/DecryptionException.php
Normal file
28
src/network/mcpe/encryption/DecryptionException.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\network\mcpe\encryption;
|
||||||
|
|
||||||
|
final class DecryptionException extends \RuntimeException{
|
||||||
|
|
||||||
|
}
|
@ -65,17 +65,17 @@ class NetworkCipher{
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws \UnexpectedValueException
|
* @throws DecryptionException
|
||||||
*/
|
*/
|
||||||
public function decrypt(string $encrypted) : string{
|
public function decrypt(string $encrypted) : string{
|
||||||
if(strlen($encrypted) < 9){
|
if(strlen($encrypted) < 9){
|
||||||
throw new \UnexpectedValueException("Payload is too short");
|
throw new DecryptionException("Payload is too short");
|
||||||
}
|
}
|
||||||
$decrypted = $this->decryptCipher->decryptUpdate($encrypted);
|
$decrypted = $this->decryptCipher->decryptUpdate($encrypted);
|
||||||
$payload = substr($decrypted, 0, -8);
|
$payload = substr($decrypted, 0, -8);
|
||||||
|
|
||||||
if(($expected = $this->calculateChecksum($this->decryptCounter++, $payload)) !== ($actual = substr($decrypted, -8))){
|
if(($expected = $this->calculateChecksum($this->decryptCounter++, $payload)) !== ($actual = substr($decrypted, -8))){
|
||||||
throw new \UnexpectedValueException("Encrypted payload has invalid checksum (expected " . bin2hex($expected) . ", got " . bin2hex($actual) . ")");
|
throw new DecryptionException("Encrypted payload has invalid checksum (expected " . bin2hex($expected) . ", got " . bin2hex($actual) . ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $payload;
|
return $payload;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user