Rev 6 | Rev 8 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * php_clientchallenge |
||
6 | daniel-mar | 5 | * Copyright 2021-2022 Daniel Marschall, ViaThinkSoft |
2 | daniel-mar | 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 | |||
6 | daniel-mar | 24 | const OPEN_TRANS_DIR = __DIR__.'/cache'; |
25 | |||
7 | daniel-mar | 26 | private static function tryDownloadPhpSha3() { |
27 | // Download file if required (usually composer should do it) |
||
28 | if (file_exists(__DIR__.'/Sha3.php')) include_once __DIR__.'/Sha3.php'; |
||
29 | if (!class_exists('\bb\Sha3\Sha3')) { |
||
30 | $sha3_lib = file_get_contents('https://raw.githubusercontent.com/danielmarschall/php-sha3/master/src/Sha3.php'); |
||
31 | if (@file_put_contents(__DIR__.'/Sha3.php', $sha3_lib)) { |
||
32 | include_once __DIR__.'/Sha3.php'; |
||
33 | } else { |
||
34 | eval('?>'.$sha3_lib); |
||
6 | daniel-mar | 35 | } |
7 | daniel-mar | 36 | } |
2 | daniel-mar | 37 | } |
38 | |||
7 | daniel-mar | 39 | private static function sha3_512($message, $raw_output=false) { |
40 | if (version_compare(PHP_VERSION, '7.1.0') >= 0) { |
||
41 | return hash('sha3-512', $message, $raw_output); |
||
42 | } else { |
||
43 | self::tryDownloadPhpSha3(); |
||
44 | return \bb\Sha3\Sha3::hash($message, 512, $raw_output); |
||
45 | } |
||
46 | } |
||
47 | |||
4 | daniel-mar | 48 | private static function sha3_512_hmac($message, $key, $raw_output=false) { |
49 | // RFC 2104 HMAC |
||
7 | daniel-mar | 50 | if (version_compare(PHP_VERSION, '7.1.0') >= 0) { |
51 | return hash_hmac('sha3-512', $message, $key, $raw_output); |
||
52 | } else { |
||
53 | self::tryDownloadPhpSha3(); |
||
54 | return \bb\Sha3\Sha3::hash_hmac($message, $key, 512, $raw_output); |
||
55 | } |
||
4 | daniel-mar | 56 | } |
57 | |||
6 | daniel-mar | 58 | private static function getOpenTransFileName($ip_target, $random) { |
59 | // Delete challenges which were never completed |
||
60 | $files = glob(self::OPEN_TRANS_DIR.'/*.tmp'); |
||
61 | $expire = strtotime('-3 DAYS'); |
||
62 | foreach ($files as $file) { |
||
63 | if (!is_file($file)) continue; |
||
64 | if (filemtime($file) > $expire) continue; |
||
65 | @unlink($file); |
||
66 | } |
||
4 | daniel-mar | 67 | |
6 | daniel-mar | 68 | return self::OPEN_TRANS_DIR.'/'.self::sha3_512($ip_target.'/'.$random).'.tmp'; |
69 | } |
||
4 | daniel-mar | 70 | |
6 | daniel-mar | 71 | public static function checkValidation($client_response, $max_time=10, $server_secret) { |
72 | list($starttime, $ip_target, $challenge, $answer, $challenge_integrity) = $client_response; |
||
73 | $open_trans_file = self::getOpenTransFileName($ip_target, $answer); |
||
4 | daniel-mar | 74 | |
2 | daniel-mar | 75 | if ($ip_target != $_SERVER['REMOTE_ADDR']) { |
4 | daniel-mar | 76 | throw new \Exception('Wrong IP'); |
2 | daniel-mar | 77 | } else if (time()-$starttime > $max_time) { |
4 | daniel-mar | 78 | throw new \Exception('Challenge expired'); |
79 | } else if ($challenge_integrity != self::sha3_512_hmac($challenge,$server_secret)) { |
||
80 | throw new \Exception('Challenge integrity failed'); |
||
2 | daniel-mar | 81 | } else if ($challenge !== self::sha3_512($starttime.'/'.$ip_target.'/'.$answer)) { |
4 | daniel-mar | 82 | throw new \Exception('Wrong answer'); |
6 | daniel-mar | 83 | } else if (!file_exists($open_trans_file)) { |
84 | throw new \Exception('Challenge submitted twice or transaction missing'); |
||
2 | daniel-mar | 85 | } else { |
6 | daniel-mar | 86 | unlink($open_trans_file); |
2 | daniel-mar | 87 | return true; |
88 | } |
||
89 | } |
||
90 | |||
6 | daniel-mar | 91 | public static function createChallenge($complexity=50000, $server_secret) { |
92 | $offset = 0; // doesn't matter |
||
93 | $min = $offset; |
||
94 | $max = $offset + $complexity; |
||
2 | daniel-mar | 95 | |
96 | $starttime = time(); |
||
97 | |||
98 | $random = rand($min,$max); // TODO: cryptographic rand |
||
99 | |||
100 | $ip_target = $_SERVER['REMOTE_ADDR']; |
||
101 | |||
102 | $challenge = self::sha3_512($starttime.'/'.$ip_target.'/'.$random); |
||
103 | |||
4 | daniel-mar | 104 | $challenge_integrity = self::sha3_512_hmac($challenge,$server_secret); |
2 | daniel-mar | 105 | |
4 | daniel-mar | 106 | $send_to_client = array($starttime, $ip_target, $challenge, $min, $max, $challenge_integrity); |
107 | |||
6 | daniel-mar | 108 | $open_trans_file = self::getOpenTransFileName($ip_target, $random); |
109 | if (@file_put_contents($open_trans_file, '') === false) { |
||
110 | throw new \Exception("Cannot write $open_trans_file"); |
||
111 | } |
||
2 | daniel-mar | 112 | |
6 | daniel-mar | 113 | return $send_to_client; |
2 | daniel-mar | 114 | } |
115 | |||
116 | } |