Subversion Repositories php_clientchallenge

Rev

Rev 5 | Rev 7 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * php_clientchallenge
  5.  * Copyright 2021-2022 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. namespace ViaThinkSoft\RateLimitingChallenge;
  21.  
  22. class ClientChallenge {
  23.  
  24.         const OPEN_TRANS_DIR = __DIR__.'/cache';
  25.  
  26.         private static function sha3_512($message, $raw_output=false) {
  27.                 if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
  28.                         return hash('sha3-512', $message, $raw_output);
  29.                 } else {
  30.                         // Download file if required (usually composer should do it)
  31.                         if (file_exists(__DIR__.'/Sha3.php')) include_once __DIR__.'/Sha3.php';
  32.                         if (!class_exists('\bb\Sha3\Sha3')) {
  33.                                 $sha3_lib = file_get_contents('https://raw.githubusercontent.com/danielmarschall/php-sha3/master/src/Sha3.php');
  34.                                 if (file_put_contents(__DIR__.'/Sha3.php', $sha3_lib)) {
  35.                                         include_once __DIR__.'/Sha3.php';
  36.                                 } else {
  37.                                         eval('?>'.$sha3_lib);
  38.                                 }
  39.                         }
  40.                         return \bb\Sha3\Sha3::hash($message, 512, $raw_output);
  41.                 }
  42.         }
  43.  
  44.         private static function sha3_512_hmac($message, $key, $raw_output=false) {
  45.                 // RFC 2104 HMAC
  46.                 if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
  47.                         return hash_hmac('sha3-512', $message, $key, $raw_output);
  48.                 } else {
  49.                         $blocksize = 576; // block size of sha-512!
  50.  
  51.                         if (strlen($key) > ($blocksize/8)) {
  52.                                 $k_ = self::sha3_512($key,true);
  53.                         } else {
  54.                                 $k_ = $key;
  55.                         }
  56.  
  57.                         $k_opad = str_repeat(chr(0x5C),($blocksize/8));
  58.                         $k_ipad = str_repeat(chr(0x36),($blocksize/8));
  59.                         for ($i=0; $i<strlen($k_); $i++) {
  60.                                 $k_opad[$i] = $k_opad[$i] ^ $k_[$i];
  61.                                 $k_ipad[$i] = $k_ipad[$i] ^ $k_[$i];
  62.                         }
  63.  
  64.                         return self::sha3_512($k_opad . self::sha3_512($k_ipad . $message, true));
  65.                 }
  66.         }
  67.  
  68.         private static function getOpenTransFileName($ip_target, $random) {
  69.                 // Delete challenges which were never completed
  70.                 $files = glob(self::OPEN_TRANS_DIR.'/*.tmp');
  71.                 $expire = strtotime('-3 DAYS');
  72.                 foreach ($files as $file) {
  73.                         if (!is_file($file)) continue;
  74.                         if (filemtime($file) > $expire) continue;
  75.                         @unlink($file);
  76.                 }
  77.  
  78.                 return self::OPEN_TRANS_DIR.'/'.self::sha3_512($ip_target.'/'.$random).'.tmp';
  79.         }
  80.  
  81.         public static function checkValidation($client_response, $max_time=10, $server_secret) {
  82.                 list($starttime, $ip_target, $challenge, $answer, $challenge_integrity) = $client_response;
  83.                 $open_trans_file = self::getOpenTransFileName($ip_target, $answer);
  84.  
  85.                 if ($ip_target != $_SERVER['REMOTE_ADDR']) {
  86.                         throw new \Exception('Wrong IP');
  87.                 } else if (time()-$starttime > $max_time) {
  88.                         throw new \Exception('Challenge expired');
  89.                 } else if ($challenge_integrity != self::sha3_512_hmac($challenge,$server_secret)) {
  90.                         throw new \Exception('Challenge integrity failed');
  91.                 } else if ($challenge !== self::sha3_512($starttime.'/'.$ip_target.'/'.$answer)) {
  92.                         throw new \Exception('Wrong answer');
  93.                 } else if (!file_exists($open_trans_file)) {
  94.                         throw new \Exception('Challenge submitted twice or transaction missing');
  95.                 } else {
  96.                         unlink($open_trans_file);
  97.                         return true;
  98.                 }
  99.         }
  100.  
  101.         public static function createChallenge($complexity=50000, $server_secret) {
  102.                 $offset = 0; // doesn't matter
  103.                 $min = $offset;
  104.                 $max = $offset + $complexity;
  105.  
  106.                 $starttime = time();
  107.  
  108.                 $random = rand($min,$max); // TODO: cryptographic rand
  109.  
  110.                 $ip_target = $_SERVER['REMOTE_ADDR'];
  111.  
  112.                 $challenge = self::sha3_512($starttime.'/'.$ip_target.'/'.$random);
  113.  
  114.                 $challenge_integrity = self::sha3_512_hmac($challenge,$server_secret);
  115.  
  116.                 $send_to_client = array($starttime, $ip_target, $challenge, $min, $max, $challenge_integrity);
  117.  
  118.                 $open_trans_file = self::getOpenTransFileName($ip_target, $random);
  119.                 if (@file_put_contents($open_trans_file, '') === false) {
  120.                         throw new \Exception("Cannot write $open_trans_file");
  121.                 }
  122.  
  123.                 return $send_to_client;
  124.         }
  125.  
  126. }
  127.