@@ -29,8 +29,8 @@ |
||
29 | 29 | * @param string $message exception message |
30 | 30 | * @param Exception $parent parent exception |
31 | 31 | */ |
32 | - public function __construct($message, $parent=null) |
|
33 | - { |
|
34 | - parent::__construct($message, 0, $parent); |
|
35 | - } |
|
32 | + public function __construct($message, $parent=null) |
|
33 | + { |
|
34 | + parent::__construct($message, 0, $parent); |
|
35 | + } |
|
36 | 36 | } |
37 | 37 | \ No newline at end of file |
@@ -97,9 +97,9 @@ |
||
97 | 97 | |
98 | 98 | // Wait and retry once in case of a 502 Bad Gateway error |
99 | 99 | if ($statusCode === 502 && !($retry)) { |
100 | - sleep(2); |
|
101 | - $this->_sendFirebase($deviceToken, $alert, $challenge, $apiKey, true); |
|
102 | - return; |
|
100 | + sleep(2); |
|
101 | + $this->_sendFirebase($deviceToken, $alert, $challenge, $apiKey, true); |
|
102 | + return; |
|
103 | 103 | } |
104 | 104 | |
105 | 105 | if ($statusCode !== 200) { |
@@ -36,11 +36,11 @@ discard block |
||
36 | 36 | */ |
37 | 37 | private static function _hmac(string $crypto, string $keyBytes, string $text) : string |
38 | 38 | { |
39 | - $hash = hash_hmac($crypto, $text, $keyBytes); |
|
40 | - if (false === $hash) { |
|
41 | - throw new Exception("calculating hash_hmac failed"); |
|
42 | - } |
|
43 | - return $hash; |
|
39 | + $hash = hash_hmac($crypto, $text, $keyBytes); |
|
40 | + if (false === $hash) { |
|
41 | + throw new Exception("calculating hash_hmac failed"); |
|
42 | + } |
|
43 | + return $hash; |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | /** |
@@ -99,12 +99,12 @@ discard block |
||
99 | 99 | * alternative to "-S064" |
100 | 100 | */ |
101 | 101 | static function generateOCRA(string $ocraSuite, |
102 | - string $key, |
|
103 | - string $counter, |
|
104 | - string $question, |
|
105 | - string $password, |
|
106 | - string $sessionInformation, |
|
107 | - string $timeStamp) : string |
|
102 | + string $key, |
|
103 | + string $counter, |
|
104 | + string $question, |
|
105 | + string $password, |
|
106 | + string $sessionInformation, |
|
107 | + string $timeStamp) : string |
|
108 | 108 | { |
109 | 109 | $codeDigits = 0; |
110 | 110 | $crypto = ""; |
@@ -122,12 +122,15 @@ discard block |
||
122 | 122 | $cryptoFunction = $components[1]; |
123 | 123 | $dataInput = strtolower($components[2]); // lower here so we can do case insensitive comparisons |
124 | 124 | |
125 | - if(stripos($cryptoFunction, "sha1")!==false) |
|
126 | - $crypto = "sha1"; |
|
127 | - if(stripos($cryptoFunction, "sha256")!==false) |
|
128 | - $crypto = "sha256"; |
|
129 | - if(stripos($cryptoFunction, "sha512")!==false) |
|
130 | - $crypto = "sha512"; |
|
125 | + if(stripos($cryptoFunction, "sha1")!==false) { |
|
126 | + $crypto = "sha1"; |
|
127 | + } |
|
128 | + if(stripos($cryptoFunction, "sha256")!==false) { |
|
129 | + $crypto = "sha256"; |
|
130 | + } |
|
131 | + if(stripos($cryptoFunction, "sha512")!==false) { |
|
132 | + $crypto = "sha512"; |
|
133 | + } |
|
131 | 134 | |
132 | 135 | $codeDigits = substr($cryptoFunction, strrpos($cryptoFunction, "-")+1); |
133 | 136 | |
@@ -135,56 +138,65 @@ discard block |
||
135 | 138 | // Counter |
136 | 139 | if($dataInput[0] == "c" ) { |
137 | 140 | // Fix the length of the HEX string |
138 | - while(strlen($counter) < 16) |
|
139 | - $counter = "0" . $counter; |
|
141 | + while(strlen($counter) < 16) { |
|
142 | + $counter = "0" . $counter; |
|
143 | + } |
|
140 | 144 | $counterLength=8; |
141 | 145 | } |
142 | 146 | // Question |
143 | 147 | if($dataInput[0] == "q" || |
144 | 148 | stripos($dataInput, "-q")!==false) { |
145 | - while(strlen($question) < 256) |
|
146 | - $question = $question . "0"; |
|
149 | + while(strlen($question) < 256) { |
|
150 | + $question = $question . "0"; |
|
151 | + } |
|
147 | 152 | $questionLength=128; |
148 | 153 | } |
149 | 154 | |
150 | 155 | // Password |
151 | 156 | if(stripos($dataInput, "psha1")!==false) { |
152 | - while(strlen($password) < 40) |
|
153 | - $password = "0" . $password; |
|
157 | + while(strlen($password) < 40) { |
|
158 | + $password = "0" . $password; |
|
159 | + } |
|
154 | 160 | $passwordLength=20; |
155 | 161 | } |
156 | 162 | |
157 | 163 | if(stripos($dataInput, "psha256")!==false) { |
158 | - while(strlen($password) < 64) |
|
159 | - $password = "0" . $password; |
|
164 | + while(strlen($password) < 64) { |
|
165 | + $password = "0" . $password; |
|
166 | + } |
|
160 | 167 | $passwordLength=32; |
161 | 168 | } |
162 | 169 | |
163 | 170 | if(stripos($dataInput, "psha512")!==false) { |
164 | - while(strlen($password) < 128) |
|
165 | - $password = "0" . $password; |
|
171 | + while(strlen($password) < 128) { |
|
172 | + $password = "0" . $password; |
|
173 | + } |
|
166 | 174 | $passwordLength=64; |
167 | 175 | } |
168 | 176 | |
169 | 177 | // sessionInformation |
170 | 178 | if(stripos($dataInput, "s064") !==false) { |
171 | - while(strlen($sessionInformation) < 128) |
|
172 | - $sessionInformation = "0" . $sessionInformation; |
|
179 | + while(strlen($sessionInformation) < 128) { |
|
180 | + $sessionInformation = "0" . $sessionInformation; |
|
181 | + } |
|
173 | 182 | |
174 | 183 | $sessionInformationLength=64; |
175 | 184 | } else if(stripos($dataInput, "s128") !==false) { |
176 | - while(strlen($sessionInformation) < 256) |
|
177 | - $sessionInformation = "0" . $sessionInformation; |
|
185 | + while(strlen($sessionInformation) < 256) { |
|
186 | + $sessionInformation = "0" . $sessionInformation; |
|
187 | + } |
|
178 | 188 | |
179 | 189 | $sessionInformationLength=128; |
180 | 190 | } else if(stripos($dataInput, "s256") !==false) { |
181 | - while(strlen($sessionInformation) < 512) |
|
182 | - $sessionInformation = "0" . $sessionInformation; |
|
191 | + while(strlen($sessionInformation) < 512) { |
|
192 | + $sessionInformation = "0" . $sessionInformation; |
|
193 | + } |
|
183 | 194 | |
184 | 195 | $sessionInformationLength=256; |
185 | 196 | } else if(stripos($dataInput, "s512") !==false) { |
186 | - while(strlen($sessionInformation) < 128) |
|
187 | - $sessionInformation = "0" . $sessionInformation; |
|
197 | + while(strlen($sessionInformation) < 128) { |
|
198 | + $sessionInformation = "0" . $sessionInformation; |
|
199 | + } |
|
188 | 200 | |
189 | 201 | $sessionInformationLength=64; |
190 | 202 | } else if (stripos($dataInput, "-s") !== false ) { |
@@ -196,8 +208,9 @@ discard block |
||
196 | 208 | // to prevent matching the "s" in the password input e.g. "psha1". |
197 | 209 | // [C] | QFxx | [PH | Snnn | TG] : Challenge-Response computation |
198 | 210 | // [C] | QFxx | [PH | TG] : Plain Signature computation |
199 | - while(strlen($sessionInformation) < 128) |
|
200 | - $sessionInformation = "0" . $sessionInformation; |
|
211 | + while(strlen($sessionInformation) < 128) { |
|
212 | + $sessionInformation = "0" . $sessionInformation; |
|
213 | + } |
|
201 | 214 | |
202 | 215 | $sessionInformationLength=64; |
203 | 216 | } |
@@ -207,8 +220,9 @@ discard block |
||
207 | 220 | // TimeStamp |
208 | 221 | if($dataInput[0] == "t" || |
209 | 222 | stripos($dataInput, "-t") !== false) { |
210 | - while(strlen($timeStamp) < 16) |
|
211 | - $timeStamp = "0" . $timeStamp; |
|
223 | + while(strlen($timeStamp) < 16) { |
|
224 | + $timeStamp = "0" . $timeStamp; |
|
225 | + } |
|
212 | 226 | $timeStampLength=8; |
213 | 227 | } |
214 | 228 |
@@ -86,8 +86,7 @@ discard block |
||
86 | 86 | $sth = $this->handle->prepare('SELECT userid FROM ' . $this->tableName . ' WHERE userid = ?'); |
87 | 87 | $sth->execute(array($userId)); |
88 | 88 | return (false !== $sth->fetchColumn()); |
89 | - } |
|
90 | - catch (Exception $e) { |
|
89 | + } catch (Exception $e) { |
|
91 | 90 | $this->logger->error('PDO error checking user exists', array('exception'=>$e, 'userId'=>$userId)); |
92 | 91 | throw ReadWriteException::fromOriginalException($e); |
93 | 92 | } |
@@ -111,8 +110,7 @@ discard block |
||
111 | 110 | $this->logger->error(sprintf('No result getting secret for user "%s"', $userId)); |
112 | 111 | throw new RuntimeException('User not found'); |
113 | 112 | } |
114 | - } |
|
115 | - catch (Exception $e) { |
|
113 | + } catch (Exception $e) { |
|
116 | 114 | $this->logger->error('PDO error getting user', array('exception' => $e, 'userId' => $userId)); |
117 | 115 | throw ReadWriteException::fromOriginalException($e); |
118 | 116 | } |
@@ -146,8 +144,7 @@ discard block |
||
146 | 144 | $sth = $this->handle->prepare('INSERT INTO ' . $this->tableName . ' (secret,userid) VALUES (?,?)'); |
147 | 145 | } |
148 | 146 | $sth->execute(array($secret, $userId)); |
149 | - } |
|
150 | - catch (Exception $e) { |
|
147 | + } catch (Exception $e) { |
|
151 | 148 | $this->logger->error( |
152 | 149 | sprintf('Unable to persist user secret for user "%s" in user secret storage (PDO)', $userId), |
153 | 150 | array('exception'=>$e) |
@@ -42,7 +42,6 @@ |
||
42 | 42 | userid varchar(30) NOT NULL UNIQUE, |
43 | 43 | secret varchar(128), |
44 | 44 | ); |
45 | - |
|
46 | 45 | * @see Tiqr_UserSecretStorage::getSecretStorage() |
47 | 46 | * @see Tiqr_UserSecretStorage_Interface |
48 | 47 | * |
@@ -4,32 +4,32 @@ discard block |
||
4 | 4 | |
5 | 5 | class OATH_OCRAParser { |
6 | 6 | |
7 | - private $OCRASuite = NULL; |
|
7 | + private $OCRASuite = NULL; |
|
8 | 8 | |
9 | - private $OCRAVersion = NULL; |
|
9 | + private $OCRAVersion = NULL; |
|
10 | 10 | |
11 | - private $CryptoFunctionType = NULL; |
|
12 | - private $CryptoFunctionHash = NULL; |
|
13 | - private $CryptoFunctionHashLength = NULL; |
|
14 | - private $CryptoFunctionTruncation = NULL; |
|
11 | + private $CryptoFunctionType = NULL; |
|
12 | + private $CryptoFunctionHash = NULL; |
|
13 | + private $CryptoFunctionHashLength = NULL; |
|
14 | + private $CryptoFunctionTruncation = NULL; |
|
15 | 15 | |
16 | - private $C = FALSE; |
|
17 | - private $Q = FALSE; |
|
18 | - private $QType = 'N'; |
|
19 | - private $QLength = 8; |
|
16 | + private $C = FALSE; |
|
17 | + private $Q = FALSE; |
|
18 | + private $QType = 'N'; |
|
19 | + private $QLength = 8; |
|
20 | 20 | |
21 | - private $P = FALSE; |
|
22 | - private $PType = 'SHA1'; |
|
23 | - private $PLength = 20; |
|
21 | + private $P = FALSE; |
|
22 | + private $PType = 'SHA1'; |
|
23 | + private $PLength = 20; |
|
24 | 24 | |
25 | - private $S = FALSE; |
|
26 | - private $SLength = 64; |
|
25 | + private $S = FALSE; |
|
26 | + private $SLength = 64; |
|
27 | 27 | |
28 | - private $T = FALSE; |
|
29 | - private $TLength = 60; // 1M |
|
30 | - private $TPeriods = array('H' => 3600, 'M' => 60, 'S' => 1); |
|
28 | + private $T = FALSE; |
|
29 | + private $TLength = 60; // 1M |
|
30 | + private $TPeriods = array('H' => 3600, 'M' => 60, 'S' => 1); |
|
31 | 31 | |
32 | - private $supportedHashFunctions = array('SHA1' => 20, 'SHA256' => 32, 'SHA512' => 64); |
|
32 | + private $supportedHashFunctions = array('SHA1' => 20, 'SHA256' => 32, 'SHA512' => 64); |
|
33 | 33 | |
34 | 34 | |
35 | 35 | /** |
@@ -37,149 +37,149 @@ discard block |
||
37 | 37 | * @throws Exception |
38 | 38 | */ |
39 | 39 | public function __construct(String $ocraSuite) { |
40 | - $this->parseOCRASuite($ocraSuite); |
|
41 | - } |
|
40 | + $this->parseOCRASuite($ocraSuite); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Inspired by https://github.com/bdauvergne/python-oath |
|
43 | + /** |
|
44 | + * Inspired by https://github.com/bdauvergne/python-oath |
|
45 | 45 | * |
46 | 46 | * @throws Exception |
47 | - */ |
|
48 | - private function parseOCRASuite($ocraSuite) { |
|
49 | - if (!is_string($ocraSuite)) { |
|
50 | - throw new Exception('OCRASuite not in string format: ' . var_export($ocraSuite, TRUE)); |
|
51 | - } |
|
52 | - |
|
53 | - $ocraSuite = strtoupper($ocraSuite); |
|
54 | - $this->OCRASuite = $ocraSuite; |
|
55 | - |
|
56 | - $s = explode(':', $ocraSuite); |
|
57 | - if (count($s) != 3) { |
|
58 | - throw new Exception('Invalid OCRASuite format: ' . var_export($ocraSuite, TRUE)); |
|
59 | - } |
|
60 | - |
|
61 | - $algo = explode('-', $s[0]); |
|
62 | - if (count($algo) != 2) { |
|
63 | - throw new Exception('Invalid OCRA version: ' . var_export($s[0], TRUE)); |
|
64 | - } |
|
65 | - |
|
66 | - if ($algo[0] !== 'OCRA') { |
|
67 | - throw new Exception('Unsupported OCRA algorithm: ' . var_export($algo[0], TRUE)); |
|
68 | - } |
|
69 | - |
|
70 | - if ($algo[1] !== '1') { |
|
71 | - throw new Exception('Unsupported OCRA version: ' . var_export($algo[1], TRUE)); |
|
72 | - } |
|
73 | - $this->OCRAVersion = $algo[1]; |
|
74 | - |
|
75 | - $cf = explode('-', $s[1]); |
|
76 | - if (count($cf) != 3) { |
|
77 | - throw new Exception('Invalid OCRA suite crypto function: ' . var_export($s[1], TRUE)); |
|
78 | - } |
|
79 | - |
|
80 | - if ($cf[0] !== 'HOTP') { |
|
81 | - throw new Exception('Unsupported OCRA suite crypto function: ' . var_export($cf[0], TRUE)); |
|
82 | - } |
|
83 | - $this->CryptoFunctionType = $cf[0]; |
|
84 | - |
|
85 | - if (!array_key_exists($cf[1], $this->supportedHashFunctions)) { |
|
86 | - throw new Exception('Unsupported hash function in OCRA suite crypto function: ' . var_export($cf[1], TRUE)); |
|
87 | - } |
|
88 | - $this->CryptoFunctionHash = $cf[1]; |
|
89 | - $this->CryptoFunctionHashLength = $this->supportedHashFunctions[$cf[1]]; |
|
90 | - |
|
91 | - if (!preg_match('/^\d+$/', $cf[2]) || (($cf[2] < 4 || $cf[2] > 10) && $cf[2] != 0)) { |
|
92 | - throw new Exception('Invalid OCRA suite crypto function truncation length: ' . var_export($cf[2], TRUE)); |
|
93 | - } |
|
94 | - $this->CryptoFunctionTruncation = intval($cf[2]); |
|
95 | - |
|
96 | - $di = explode('-', $s[2]); |
|
97 | - if (count($cf) == 0) { |
|
98 | - throw new Exception('Invalid OCRA suite data input: ' . var_export($s[2], TRUE)); |
|
99 | - } |
|
100 | - |
|
101 | - $data_input = array(); |
|
102 | - foreach($di as $elem) { |
|
103 | - $letter = $elem[0]; |
|
104 | - if (array_key_exists($letter, $data_input)) { |
|
105 | - throw new Exception('Duplicate field in OCRA suite data input: ' . var_export($elem, TRUE)); |
|
106 | - } |
|
107 | - $data_input[$letter] = 1; |
|
108 | - |
|
109 | - if ($letter === 'C' && strlen($elem) == 1) { |
|
110 | - $this->C = TRUE; |
|
111 | - } elseif ($letter === 'Q') { |
|
112 | - if (strlen($elem) == 1) { |
|
113 | - $this->Q = TRUE; |
|
114 | - } elseif (preg_match('/^Q([AHN])(\d+)$/', $elem, $match)) { |
|
115 | - $q_len = intval($match[2]); |
|
116 | - if ($q_len < 4 || $q_len > 64) { |
|
117 | - throw new Exception('Invalid OCRA suite data input question length: ' . var_export($q_len, TRUE)); |
|
118 | - } |
|
119 | - $this->Q = TRUE; |
|
120 | - $this->QType = $match[1]; |
|
121 | - $this->QLength = $q_len; |
|
122 | - } else { |
|
123 | - throw new Exception('Invalid OCRA suite data input question: ' . var_export($elem, TRUE)); |
|
124 | - } |
|
125 | - } elseif ($letter === 'P') { |
|
126 | - if (strlen($elem) == 1) { |
|
127 | - $this->P = TRUE; |
|
128 | - } else { |
|
129 | - $p_algo = substr($elem, 1); |
|
130 | - if (!array_key_exists($p_algo, $this->supportedHashFunctions)) { |
|
131 | - throw new Exception('Unsupported OCRA suite PIN hash function: ' . var_export($elem, TRUE)); |
|
132 | - } |
|
133 | - $this->P = TRUE; |
|
134 | - $this->PType = $p_algo; |
|
135 | - $this->PLength = $this->supportedHashFunctions[$p_algo]; |
|
136 | - } |
|
137 | - } elseif ($letter === 'S') { |
|
138 | - if (strlen($elem) == 1) { |
|
139 | - $this->S = TRUE; |
|
140 | - } elseif (preg_match('/^S(\d+)$/', $elem, $match)) { |
|
141 | - $s_len = intval($match[1]); |
|
142 | - if ($s_len <= 0 || $s_len > 512) { |
|
143 | - throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($s_len, TRUE)); |
|
144 | - } |
|
145 | - |
|
146 | - $this->S = TRUE; |
|
147 | - $this->SLength = $s_len; |
|
148 | - } else { |
|
149 | - throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($elem, TRUE)); |
|
150 | - } |
|
151 | - } elseif ($letter === 'T') { |
|
152 | - if (strlen($elem) == 1) { |
|
153 | - $this->T = TRUE; |
|
154 | - } elseif (preg_match('/^T(\d+[HMS])+$/', $elem)) { |
|
155 | - preg_match_all('/(\d+)([HMS])/', $elem, $match); |
|
156 | - |
|
157 | - if (count($match[1]) !== count(array_unique($match[2]))) { |
|
158 | - throw new Exception('Duplicate definitions in OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
159 | - } |
|
160 | - |
|
161 | - $length = 0; |
|
162 | - for ($i = 0; $i < count($match[1]); $i++) { |
|
163 | - $length += intval($match[1][$i]) * $this->TPeriods[$match[2][$i]]; |
|
164 | - } |
|
165 | - if ($length <= 0) { |
|
166 | - throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
167 | - } |
|
168 | - |
|
169 | - $this->T = TRUE; |
|
170 | - $this->TLength = $length; |
|
171 | - } else { |
|
172 | - throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
173 | - } |
|
174 | - } else { |
|
175 | - throw new Exception('Unsupported OCRA suite data input field: ' . var_export($elem, TRUE)); |
|
176 | - } |
|
177 | - } |
|
178 | - |
|
179 | - if (!$this->Q) { |
|
180 | - throw new Exception('OCRA suite data input question not defined: ' . var_export($s[2], TRUE)); |
|
181 | - } |
|
182 | - } |
|
47 | + */ |
|
48 | + private function parseOCRASuite($ocraSuite) { |
|
49 | + if (!is_string($ocraSuite)) { |
|
50 | + throw new Exception('OCRASuite not in string format: ' . var_export($ocraSuite, TRUE)); |
|
51 | + } |
|
52 | + |
|
53 | + $ocraSuite = strtoupper($ocraSuite); |
|
54 | + $this->OCRASuite = $ocraSuite; |
|
55 | + |
|
56 | + $s = explode(':', $ocraSuite); |
|
57 | + if (count($s) != 3) { |
|
58 | + throw new Exception('Invalid OCRASuite format: ' . var_export($ocraSuite, TRUE)); |
|
59 | + } |
|
60 | + |
|
61 | + $algo = explode('-', $s[0]); |
|
62 | + if (count($algo) != 2) { |
|
63 | + throw new Exception('Invalid OCRA version: ' . var_export($s[0], TRUE)); |
|
64 | + } |
|
65 | + |
|
66 | + if ($algo[0] !== 'OCRA') { |
|
67 | + throw new Exception('Unsupported OCRA algorithm: ' . var_export($algo[0], TRUE)); |
|
68 | + } |
|
69 | + |
|
70 | + if ($algo[1] !== '1') { |
|
71 | + throw new Exception('Unsupported OCRA version: ' . var_export($algo[1], TRUE)); |
|
72 | + } |
|
73 | + $this->OCRAVersion = $algo[1]; |
|
74 | + |
|
75 | + $cf = explode('-', $s[1]); |
|
76 | + if (count($cf) != 3) { |
|
77 | + throw new Exception('Invalid OCRA suite crypto function: ' . var_export($s[1], TRUE)); |
|
78 | + } |
|
79 | + |
|
80 | + if ($cf[0] !== 'HOTP') { |
|
81 | + throw new Exception('Unsupported OCRA suite crypto function: ' . var_export($cf[0], TRUE)); |
|
82 | + } |
|
83 | + $this->CryptoFunctionType = $cf[0]; |
|
84 | + |
|
85 | + if (!array_key_exists($cf[1], $this->supportedHashFunctions)) { |
|
86 | + throw new Exception('Unsupported hash function in OCRA suite crypto function: ' . var_export($cf[1], TRUE)); |
|
87 | + } |
|
88 | + $this->CryptoFunctionHash = $cf[1]; |
|
89 | + $this->CryptoFunctionHashLength = $this->supportedHashFunctions[$cf[1]]; |
|
90 | + |
|
91 | + if (!preg_match('/^\d+$/', $cf[2]) || (($cf[2] < 4 || $cf[2] > 10) && $cf[2] != 0)) { |
|
92 | + throw new Exception('Invalid OCRA suite crypto function truncation length: ' . var_export($cf[2], TRUE)); |
|
93 | + } |
|
94 | + $this->CryptoFunctionTruncation = intval($cf[2]); |
|
95 | + |
|
96 | + $di = explode('-', $s[2]); |
|
97 | + if (count($cf) == 0) { |
|
98 | + throw new Exception('Invalid OCRA suite data input: ' . var_export($s[2], TRUE)); |
|
99 | + } |
|
100 | + |
|
101 | + $data_input = array(); |
|
102 | + foreach($di as $elem) { |
|
103 | + $letter = $elem[0]; |
|
104 | + if (array_key_exists($letter, $data_input)) { |
|
105 | + throw new Exception('Duplicate field in OCRA suite data input: ' . var_export($elem, TRUE)); |
|
106 | + } |
|
107 | + $data_input[$letter] = 1; |
|
108 | + |
|
109 | + if ($letter === 'C' && strlen($elem) == 1) { |
|
110 | + $this->C = TRUE; |
|
111 | + } elseif ($letter === 'Q') { |
|
112 | + if (strlen($elem) == 1) { |
|
113 | + $this->Q = TRUE; |
|
114 | + } elseif (preg_match('/^Q([AHN])(\d+)$/', $elem, $match)) { |
|
115 | + $q_len = intval($match[2]); |
|
116 | + if ($q_len < 4 || $q_len > 64) { |
|
117 | + throw new Exception('Invalid OCRA suite data input question length: ' . var_export($q_len, TRUE)); |
|
118 | + } |
|
119 | + $this->Q = TRUE; |
|
120 | + $this->QType = $match[1]; |
|
121 | + $this->QLength = $q_len; |
|
122 | + } else { |
|
123 | + throw new Exception('Invalid OCRA suite data input question: ' . var_export($elem, TRUE)); |
|
124 | + } |
|
125 | + } elseif ($letter === 'P') { |
|
126 | + if (strlen($elem) == 1) { |
|
127 | + $this->P = TRUE; |
|
128 | + } else { |
|
129 | + $p_algo = substr($elem, 1); |
|
130 | + if (!array_key_exists($p_algo, $this->supportedHashFunctions)) { |
|
131 | + throw new Exception('Unsupported OCRA suite PIN hash function: ' . var_export($elem, TRUE)); |
|
132 | + } |
|
133 | + $this->P = TRUE; |
|
134 | + $this->PType = $p_algo; |
|
135 | + $this->PLength = $this->supportedHashFunctions[$p_algo]; |
|
136 | + } |
|
137 | + } elseif ($letter === 'S') { |
|
138 | + if (strlen($elem) == 1) { |
|
139 | + $this->S = TRUE; |
|
140 | + } elseif (preg_match('/^S(\d+)$/', $elem, $match)) { |
|
141 | + $s_len = intval($match[1]); |
|
142 | + if ($s_len <= 0 || $s_len > 512) { |
|
143 | + throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($s_len, TRUE)); |
|
144 | + } |
|
145 | + |
|
146 | + $this->S = TRUE; |
|
147 | + $this->SLength = $s_len; |
|
148 | + } else { |
|
149 | + throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($elem, TRUE)); |
|
150 | + } |
|
151 | + } elseif ($letter === 'T') { |
|
152 | + if (strlen($elem) == 1) { |
|
153 | + $this->T = TRUE; |
|
154 | + } elseif (preg_match('/^T(\d+[HMS])+$/', $elem)) { |
|
155 | + preg_match_all('/(\d+)([HMS])/', $elem, $match); |
|
156 | + |
|
157 | + if (count($match[1]) !== count(array_unique($match[2]))) { |
|
158 | + throw new Exception('Duplicate definitions in OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
159 | + } |
|
160 | + |
|
161 | + $length = 0; |
|
162 | + for ($i = 0; $i < count($match[1]); $i++) { |
|
163 | + $length += intval($match[1][$i]) * $this->TPeriods[$match[2][$i]]; |
|
164 | + } |
|
165 | + if ($length <= 0) { |
|
166 | + throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
167 | + } |
|
168 | + |
|
169 | + $this->T = TRUE; |
|
170 | + $this->TLength = $length; |
|
171 | + } else { |
|
172 | + throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
173 | + } |
|
174 | + } else { |
|
175 | + throw new Exception('Unsupported OCRA suite data input field: ' . var_export($elem, TRUE)); |
|
176 | + } |
|
177 | + } |
|
178 | + |
|
179 | + if (!$this->Q) { |
|
180 | + throw new Exception('OCRA suite data input question not defined: ' . var_export($s[2], TRUE)); |
|
181 | + } |
|
182 | + } |
|
183 | 183 | |
184 | 184 | /** Generate an OCRA challenge question according to the ocra suite specified in the constructor |
185 | 185 | * @return The randomly generated OCRA question |
@@ -192,50 +192,50 @@ discard block |
||
192 | 192 | * Note that the question string is the exact question string a specified in the OCRA strandard (RFC 6287) |
193 | 193 | * The challenge is not yet hex encoded as expected by OCRA::generateOCRA() |
194 | 194 | */ |
195 | - public function generateChallenge() : String { |
|
196 | - $q_length = $this->QLength; |
|
197 | - $q_type = $this->QType; |
|
195 | + public function generateChallenge() : String { |
|
196 | + $q_length = $this->QLength; |
|
197 | + $q_type = $this->QType; |
|
198 | 198 | |
199 | 199 | $bytes = Tiqr_Random::randomBytes($q_length); |
200 | 200 | |
201 | - switch($q_type) { |
|
202 | - case 'A': |
|
203 | - $challenge = base64_encode($bytes); |
|
204 | - $tr = implode("", unpack('H*', $bytes)); |
|
205 | - $challenge = rtrim(strtr($challenge, '+/', $tr), '='); |
|
206 | - break; |
|
207 | - case 'H': |
|
208 | - $challenge = implode("", unpack('H*', $bytes)); |
|
209 | - break; |
|
210 | - case 'N': |
|
211 | - $challenge = implode("", unpack('N*', $bytes)); |
|
212 | - break; |
|
213 | - default: |
|
214 | - throw new Exception('Unsupported OCRASuite challenge type: ' . var_export($q_type, TRUE)); |
|
215 | - break; |
|
216 | - } |
|
217 | - |
|
218 | - $challenge = substr($challenge, 0, $q_length); |
|
219 | - |
|
220 | - return $challenge; |
|
221 | - } |
|
222 | - |
|
223 | - |
|
224 | - /** |
|
225 | - * Constant time string comparison, see http://codahale.com/a-lesson-in-timing-attacks/ |
|
226 | - */ |
|
227 | - public static function constEqual(string $s1, string $s2): bool { |
|
228 | - if (strlen($s1) != strlen($s2)) { |
|
229 | - return FALSE; |
|
230 | - } |
|
231 | - |
|
232 | - $result = TRUE; |
|
233 | - $length = strlen($s1); |
|
234 | - for ($i = 0; $i < $length; $i++) { |
|
235 | - $result &= ($s1[$i] == $s2[$i]); |
|
236 | - } |
|
237 | - |
|
238 | - return (boolean)$result; |
|
239 | - } |
|
201 | + switch($q_type) { |
|
202 | + case 'A': |
|
203 | + $challenge = base64_encode($bytes); |
|
204 | + $tr = implode("", unpack('H*', $bytes)); |
|
205 | + $challenge = rtrim(strtr($challenge, '+/', $tr), '='); |
|
206 | + break; |
|
207 | + case 'H': |
|
208 | + $challenge = implode("", unpack('H*', $bytes)); |
|
209 | + break; |
|
210 | + case 'N': |
|
211 | + $challenge = implode("", unpack('N*', $bytes)); |
|
212 | + break; |
|
213 | + default: |
|
214 | + throw new Exception('Unsupported OCRASuite challenge type: ' . var_export($q_type, TRUE)); |
|
215 | + break; |
|
216 | + } |
|
217 | + |
|
218 | + $challenge = substr($challenge, 0, $q_length); |
|
219 | + |
|
220 | + return $challenge; |
|
221 | + } |
|
222 | + |
|
223 | + |
|
224 | + /** |
|
225 | + * Constant time string comparison, see http://codahale.com/a-lesson-in-timing-attacks/ |
|
226 | + */ |
|
227 | + public static function constEqual(string $s1, string $s2): bool { |
|
228 | + if (strlen($s1) != strlen($s2)) { |
|
229 | + return FALSE; |
|
230 | + } |
|
231 | + |
|
232 | + $result = TRUE; |
|
233 | + $length = strlen($s1); |
|
234 | + for ($i = 0; $i < $length; $i++) { |
|
235 | + $result &= ($s1[$i] == $s2[$i]); |
|
236 | + } |
|
237 | + |
|
238 | + return (boolean)$result; |
|
239 | + } |
|
240 | 240 | |
241 | 241 | } |
@@ -74,7 +74,9 @@ |
||
74 | 74 | */ |
75 | 75 | public function getPath(): string |
76 | 76 | { |
77 | - if (substr($this->path, -1)!="/") return $this->path."/"; |
|
77 | + if (substr($this->path, -1)!="/") { |
|
78 | + return $this->path."/"; |
|
79 | + } |
|
78 | 80 | return $this->path; |
79 | 81 | } |
80 | 82 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | public function createUser(string $userId, string $displayName) : void |
61 | 61 | { |
62 | 62 | $user = array("userId"=>$userId, |
63 | - "displayName"=>$displayName); |
|
63 | + "displayName"=>$displayName); |
|
64 | 64 | $this->_saveUser($userId, $user); |
65 | 65 | } |
66 | 66 | |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | { |
91 | 91 | if ($data = $this->_loadUser($userId)) { |
92 | 92 | if (isset($data["notificationType"])) { |
93 | - return $data["notificationType"]; |
|
93 | + return $data["notificationType"]; |
|
94 | 94 | } |
95 | 95 | } |
96 | 96 | return ''; |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | { |
114 | 114 | if ($data = $this->_loadUser($userId)) { |
115 | 115 | if (isset($data["notificationAddress"])) { |
116 | - return $data["notificationAddress"]; |
|
116 | + return $data["notificationAddress"]; |
|
117 | 117 | } |
118 | 118 | } |
119 | 119 | $this->logger->info('Unable to find notification address for user'); |
@@ -54,8 +54,8 @@ |
||
54 | 54 | } |
55 | 55 | |
56 | 56 | $envelope = array("expire"=>$expire, |
57 | - "createdAt"=>time(), |
|
58 | - "value"=>$value); |
|
57 | + "createdAt"=>time(), |
|
58 | + "value"=>$value); |
|
59 | 59 | $filename = $this->getFilenameByKey($key); |
60 | 60 | |
61 | 61 | if (!file_put_contents($filename, serialize($envelope))) { |
@@ -36,8 +36,7 @@ |
||
36 | 36 | // response as the client calculated. |
37 | 37 | try { |
38 | 38 | $expected = OCRA::generateOCRA($this->_ocraSuite, $userSecret, "", $challenge, "", $sessionInformation, ""); |
39 | - } |
|
40 | - catch (Exception $e) { |
|
39 | + } catch (Exception $e) { |
|
41 | 40 | $this->logger->warning(sprintf('Error calculating OCRA response for user "%s"', $userId), array('exception'=>$e)); |
42 | 41 | return false; |
43 | 42 | } |