Subversion Repositories oidplus

Rev

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