Subversion Repositories php_clientchallenge

Rev

Rev 5 | Rev 7 | 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
 
4 daniel-mar 26
        private static function sha3_512($message, $raw_output=false) {
2 daniel-mar 27
                if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
4 daniel-mar 28
                        return hash('sha3-512', $message, $raw_output);
2 daniel-mar 29
                } else {
6 daniel-mar 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);
2 daniel-mar 41
                }
42
        }
43
 
4 daniel-mar 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!
2 daniel-mar 50
 
4 daniel-mar 51
                        if (strlen($key) > ($blocksize/8)) {
5 daniel-mar 52
                                $k_ = self::sha3_512($key,true);
4 daniel-mar 53
                        } else {
54
                                $k_ = $key;
55
                        }
2 daniel-mar 56
 
4 daniel-mar 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
                        }
2 daniel-mar 63
 
5 daniel-mar 64
                        return self::sha3_512($k_opad . self::sha3_512($k_ipad . $message, true));
4 daniel-mar 65
                }
66
        }
67
 
6 daniel-mar 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
                }
4 daniel-mar 77
 
6 daniel-mar 78
                return self::OPEN_TRANS_DIR.'/'.self::sha3_512($ip_target.'/'.$random).'.tmp';
79
        }
4 daniel-mar 80
 
6 daniel-mar 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);
4 daniel-mar 84
 
2 daniel-mar 85
                if ($ip_target != $_SERVER['REMOTE_ADDR']) {
4 daniel-mar 86
                        throw new \Exception('Wrong IP');
2 daniel-mar 87
                } else if (time()-$starttime > $max_time) {
4 daniel-mar 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');
2 daniel-mar 91
                } else if ($challenge !== self::sha3_512($starttime.'/'.$ip_target.'/'.$answer)) {
4 daniel-mar 92
                        throw new \Exception('Wrong answer');
6 daniel-mar 93
                } else if (!file_exists($open_trans_file)) {
94
                        throw new \Exception('Challenge submitted twice or transaction missing');
2 daniel-mar 95
                } else {
6 daniel-mar 96
                        unlink($open_trans_file);
2 daniel-mar 97
                        return true;
98
                }
99
        }
100
 
6 daniel-mar 101
        public static function createChallenge($complexity=50000, $server_secret) {
102
                $offset = 0; // doesn't matter
103
                $min = $offset;
104
                $max = $offset + $complexity;
2 daniel-mar 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
 
4 daniel-mar 114
                $challenge_integrity = self::sha3_512_hmac($challenge,$server_secret);
2 daniel-mar 115
 
4 daniel-mar 116
                $send_to_client = array($starttime, $ip_target, $challenge, $min, $max, $challenge_integrity);
117
 
6 daniel-mar 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
                }
2 daniel-mar 122
 
6 daniel-mar 123
                return $send_to_client;
2 daniel-mar 124
        }
125
 
126
}