Rev 9 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 9 | Rev 10 | ||
---|---|---|---|
Line 51... | Line 51... | ||
51 | self::tryDownloadPhpSha3(); |
51 | self::tryDownloadPhpSha3(); |
52 | return \bb\Sha3\Sha3::hash_hmac($message, $key, 512, $raw_output); |
52 | return \bb\Sha3\Sha3::hash_hmac($message, $key, 512, $raw_output); |
53 | } |
53 | } |
54 | } |
54 | } |
55 | 55 | ||
56 | private static function getOpenTransFileName($ip_target, $random) { |
56 | private static function getOpenTransFileName($ip_target, $random, $server_secret) { |
57 | $dir = defined('VTS_CS_OPEN_TRANS_DIR') ? VTS_CS_OPEN_TRANS_DIR : __DIR__.'/cache'; |
57 | $dir = defined('VTS_CS_OPEN_TRANS_DIR') ? VTS_CS_OPEN_TRANS_DIR : __DIR__.'/cache'; |
58 | if ($dir == '') $dir = '.'; /** @phpstan-ignore-line */ |
58 | if ($dir == '') $dir = '.'; /** @phpstan-ignore-line */ |
59 | 59 | ||
60 | // First, delete challenges which were never completed |
60 | // First, delete challenges which were never completed |
61 | $files = glob($dir.'/vts_client_challenge_*.tmp'); |
61 | $files = glob($dir.'/vts_client_challenge_*.tmp'); |
Line 64... | Line 64... | ||
64 | if (!is_file($file)) continue; |
64 | if (!is_file($file)) continue; |
65 | if (filemtime($file) > $expire) continue; |
65 | if (filemtime($file) > $expire) continue; |
66 | @unlink($file); |
66 | @unlink($file); |
67 | } |
67 | } |
68 | 68 | ||
69 | return $dir.'/vts_client_challenge_'.self::sha3_512($ip_target.'/'.$random).'.tmp'; |
69 | return $dir.'/vts_client_challenge_'.self::sha3_512_hmac($ip_target.'/'.$random, $server_secret).'.tmp'; |
70 | } |
70 | } |
71 | 71 | ||
72 | public static function checkValidation($client_response, $max_time=10, $server_secret) { |
72 | public static function checkValidation($client_response, $max_time=10, $server_secret) { |
73 | list($starttime, $ip_target, $challenge, $answer, $challenge_integrity) = $client_response; |
73 | list($starttime, $ip_target, $challenge, $answer, $challenge_integrity) = $client_response; |
74 | $open_trans_file = self::getOpenTransFileName($ip_target, $answer); |
74 | $open_trans_file = self::getOpenTransFileName($ip_target, $answer, $server_secret); |
75 | 75 | ||
76 | if ($ip_target != $_SERVER['REMOTE_ADDR']) { |
76 | if ($ip_target != $_SERVER['REMOTE_ADDR']) { |
77 | throw new \Exception('Wrong IP'); |
77 | throw new \Exception('Wrong IP'); |
78 | } else if (time()-$starttime > $max_time) { |
78 | } else if (time()-$starttime > $max_time) { |
79 | throw new \Exception('Challenge expired'); |
79 | throw new \Exception('Challenge expired'); |
Line 104... | Line 104... | ||
104 | 104 | ||
105 | $challenge_integrity = self::sha3_512_hmac($challenge,$server_secret); |
105 | $challenge_integrity = self::sha3_512_hmac($challenge,$server_secret); |
106 | 106 | ||
107 | $send_to_client = array($starttime, $ip_target, $challenge, $min, $max, $challenge_integrity); |
107 | $send_to_client = array($starttime, $ip_target, $challenge, $min, $max, $challenge_integrity); |
108 | 108 | ||
109 | $open_trans_file = self::getOpenTransFileName($ip_target, $random); |
109 | $open_trans_file = self::getOpenTransFileName($ip_target, $random, $server_secret); |
110 | if (@file_put_contents($open_trans_file, '') === false) { |
110 | if (@file_put_contents($open_trans_file, '') === false) { |
111 | throw new \Exception("Cannot write $open_trans_file"); |
111 | throw new \Exception("Cannot write $open_trans_file"); |
112 | } |
112 | } |
113 | 113 | ||
114 | return $send_to_client; |
114 | return $send_to_client; |