From 061f7b31d9b35d2f22dc0c310ccb5002e59116f2 Mon Sep 17 00:00:00 2001 From: Stricted Date: Fri, 23 Dec 2016 20:11:41 +0100 Subject: [PATCH] add support for php >= 7.1 (mcrypt extension used by cryptlib is deprecated and AEAD support was added to openssl) --- SpeedportHybrid.class.php | 5 ++-- lib/trait/CryptLib.class.php | 47 +++++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/SpeedportHybrid.class.php b/SpeedportHybrid.class.php index ba390e6..06030a6 100644 --- a/SpeedportHybrid.class.php +++ b/SpeedportHybrid.class.php @@ -115,17 +115,16 @@ class SpeedportHybrid { * @return array */ private function sendRequest ($path, $fields, $cookie = false, $count = 0) { - $url = $this->url.$path.'?lang=en'; + $url = $this->url.$path; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if (!empty($fields)) { + curl_setopt($ch, CURLOPT_POST, true); if (is_array($fields)) { - curl_setopt($ch, CURLOPT_POST, count($fields)); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); } else { - curl_setopt($ch, CURLOPT_POST, $count); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); } } diff --git a/lib/trait/CryptLib.class.php b/lib/trait/CryptLib.class.php index eedecbb..fb89e96 100644 --- a/lib/trait/CryptLib.class.php +++ b/lib/trait/CryptLib.class.php @@ -31,14 +31,22 @@ trait CryptLib { $key = hex2bin($this->derivedk); $enc = hex2bin($data); - $factory = new CryptLib\Cipher\Factory(); - $aes = $factory->getBlockCipher('rijndael-128'); - $aes->setKey($key); - $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]); - - $mode->decrypt($enc); - - return $mode->finish(); + if (PHP_VERSION_ID >= 70100) { + $ciphertext = substr($enc, 0, -8); + $tag = substr($enc, strlen($enc)-8); + + return openssl_decrypt($ciphertext, 'aes-128-ccm', $key, OPENSSL_RAW_DATA, $iv, $tag, $adata); + } + else { + $factory = new CryptLib\Cipher\Factory(); + $aes = $factory->getBlockCipher('rijndael-128'); + $aes->setKey($key); + $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]); + + $mode->decrypt($enc); + + return $mode->finish(); + } } /** @@ -52,12 +60,23 @@ trait CryptLib { $adata = hex2bin(substr($this->challenge, 32, 16)); $key = hex2bin($this->derivedk); - $factory = new CryptLib\Cipher\Factory(); - $aes = $factory->getBlockCipher('rijndael-128'); - $aes->setKey($key); - $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]); - $mode->encrypt($data); + if (empty($data)) { + return $data; + } - return bin2hex($mode->finish()); + if (PHP_VERSION_ID >= 70100) { + $tag = null; + $encdata = openssl_encrypt($data, 'aes-128-ccm', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv, $tag, $adata, 8); + return bin2hex($encdata . $tag); + } + else { + $factory = new CryptLib\Cipher\Factory(); + $aes = $factory->getBlockCipher('rijndael-128'); + $aes->setKey($key); + $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]); + $mode->encrypt($data); + var_dump(bin2hex($mode->finish())); + return bin2hex($mode->finish()); + } } } -- 2.20.1