Subversion Repositories oidplus

Rev

Rev 1023 | Rev 1025 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
709 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1022 daniel-mar 5
 * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft
709 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
if (!defined('INSIDE_OIDPLUS')) die();
21
 
22
class OIDplusCaptchaPluginVtsClientChallenge extends OIDplusCaptchaPlugin {
23
 
24
        public static function id(): string {
25
                return 'ViaThinkSoft Client Challenge';
26
        }
27
 
1016 daniel-mar 28
        public function isVisible(): bool {
709 daniel-mar 29
                return false;
30
        }
31
 
32
        public function csrfUnlock($actionID) {
33
                if ($actionID == 'get_challenge') {
34
                        return true;
35
                }
36
                return false;
37
        }
38
 
39
        public function action($actionID, $params) {
40
                if ($actionID == 'get_challenge') {
1017 daniel-mar 41
                        $complexity=50000; // TODO: make configurable
709 daniel-mar 42
                        $server_secret='VtsClientChallenge:'.OIDplus::baseConfig()->getValue('SERVER_SECRET');
43
 
44
                        $min = 0;
45
                        $max = $complexity;
46
                        $starttime = time();
47
                        $random = rand($min,$max); // TODO: cryptographic rand
48
                        $ip_target = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'unknown';
49
                        $challenge = sha3_512($starttime.'/'.$ip_target.'/'.$random);
50
                        $challenge_integrity = sha3_512_hmac($challenge,$server_secret);
51
                        $send_to_client = array($starttime, $ip_target, $challenge, $min, $max, $challenge_integrity);
52
 
1022 daniel-mar 53
                        $open_trans_file = self::getOpenTransFileName($ip_target, $random);
54
                        if (@file_put_contents($open_trans_file, '') === false) {
55
                                throw new OIDplusException(_L('Cannot write file %1', $open_trans_file));
56
                        }
57
 
709 daniel-mar 58
                        return array("status" => 0, "challenge" => $send_to_client);
59
                }
60
        }
61
 
1022 daniel-mar 62
        private static function getOpenTransFileName($ip_target, $random) {
63
                $dir = OIDplus::localpath().'/userdata/cache';
1024 daniel-mar 64
                $server_secret='VtsClientChallenge:'.OIDplus::baseConfig()->getValue('SERVER_SECRET');
1022 daniel-mar 65
 
66
                // First, delete challenges which were never completed
67
                $files = glob($dir.'/vts_client_challenge_*.tmp');
68
                $expire = strtotime('-3 DAYS');
69
                foreach ($files as $file) {
70
                        if (!is_file($file)) continue;
71
                        if (filemtime($file) > $expire) continue;
72
                        @unlink($file);
73
                }
74
 
1024 daniel-mar 75
                return $dir.'/vts_client_challenge_'.sha3_512_hmac($ip_target.'/'.$random, $server_secret).'.tmp';
1022 daniel-mar 76
        }
77
 
709 daniel-mar 78
        public function captchaGenerate($header_text=null, $footer_text=null) {
79
                return '<noscript>'.
1024 daniel-mar 80
                       '<p><font color="red">'._L('You need to enable JavaScript to solve the CAPTCHA.').'</font></p>'.
81
                       '</noscript>'.
82
                       '<input type="hidden" id="vts_validation_result" name="vts_validation_result" value="">'.
83
                       '<script>'.
84
                       'OIDplusCaptchaPluginVtsClientChallenge.captchaShow('.js_escape(OIDplus::webpath(null,OIDplus::PATH_RELATIVE)).');'.
85
                       '</script>';
709 daniel-mar 86
        }
87
 
88
        public function captchaVerify($params, $fieldname=null) {
89
 
90
                if (is_null($fieldname)) $fieldname = 'vts_validation_result';
91
 
92
                $server_secret='VtsClientChallenge:'.OIDplus::baseConfig()->getValue('SERVER_SECRET');
93
                $max_time = 10*60; // 10min. TODO: make configurable!
94
 
1024 daniel-mar 95
                if (!isset($params[$fieldname])) throw new OIDplusException(_L('No challenge response found').' (A)');
709 daniel-mar 96
 
1022 daniel-mar 97
                $client_response = @json_decode($params[$fieldname], true);
1024 daniel-mar 98
 
99
                if (!is_array($client_response)) throw new OIDplusException(_L('Challenge response is invalid').' (B)');
100
                if (count($client_response) != 5) throw new OIDplusException(_L('Challenge response is invalid').' (C)');
1022 daniel-mar 101
                list($starttime, $ip_target, $challenge, $answer, $challenge_integrity) = $client_response;
1024 daniel-mar 102
                if (!is_numeric($starttime)) throw new OIDplusException(_L('Challenge response is invalid').' (D)');
103
                if (!is_string($ip_target)) throw new OIDplusException(_L('Challenge response is invalid').' (E)');
104
                if (!is_string($challenge)) throw new OIDplusException(_L('Challenge response is invalid').' (F)');
105
                if (!is_numeric($answer)) throw new OIDplusException(_L('Challenge response is invalid').' (G)');
106
                if (!is_string($challenge_integrity)) throw new OIDplusException(_L('Challenge response is invalid').' (H)');
107
 
1022 daniel-mar 108
                $open_trans_file = self::getOpenTransFileName($ip_target, $answer);
709 daniel-mar 109
 
1024 daniel-mar 110
                $current_ip = (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'unknown');
111
                if ($ip_target != $current_ip) {
112
                        throw new OIDplusException(_L('IP address has changed. Please try again. (current IP %1, expected %2)', $current_ip, $ip_target));
709 daniel-mar 113
                } else if (time()-$starttime > $max_time) {
1024 daniel-mar 114
                        throw new OIDplusException(_L('Challenge expired. Please try again.'));
709 daniel-mar 115
                } else if ($challenge_integrity != sha3_512_hmac($challenge,$server_secret)) {
718 daniel-mar 116
                        throw new OIDplusException(_L('Challenge integrity failed'));
709 daniel-mar 117
                } else if ($challenge !== sha3_512($starttime.'/'.$ip_target.'/'.$answer)) {
718 daniel-mar 118
                        throw new OIDplusException(_L('Wrong answer'));
1022 daniel-mar 119
                } else if (!file_exists($open_trans_file)) {
120
                        throw new OIDplusException(_L('Challenge submitted twice or transaction missing'));
121
                } else {
122
                        @unlink($open_trans_file);
709 daniel-mar 123
                }
124
        }
125
 
126
        public static function setupHTML(): string {
127
                return '<div id="CAPTCHAPLUGIN_PARAMS_VtsClientChallenge">'.
128
                       '<p>'._L('ViaThinkSoft Client Challenge lets the client computer solve a cryptographic problem instead of letting the user solve a CAPTCHA. This slows down brute-force attacks.').'</p>'.
129
                       '</div>';
130
        }
131
 
132
}