@@ -48,24 +48,24 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | protected function _getHash ($secret, $counter) |
| 50 | 50 | { |
| 51 | - // Counter |
|
| 52 | - //the counter value can be more than one byte long, so we need to go multiple times |
|
| 53 | - $cur_counter = array(0,0,0,0,0,0,0,0); |
|
| 54 | - for($i=7;$i>=0;$i--) |
|
| 55 | - { |
|
| 56 | - $cur_counter[$i] = pack ('C*', $counter); |
|
| 57 | - $counter = $counter >> 8; |
|
| 58 | - } |
|
| 59 | - $bin_counter = implode($cur_counter); |
|
| 60 | - // Pad to 8 chars |
|
| 61 | - if (strlen ($bin_counter) < 8) |
|
| 62 | - { |
|
| 63 | - $bin_counter = str_repeat (chr(0), 8 - strlen ($bin_counter)) . $bin_counter; |
|
| 64 | - } |
|
| 51 | + // Counter |
|
| 52 | + //the counter value can be more than one byte long, so we need to go multiple times |
|
| 53 | + $cur_counter = array(0,0,0,0,0,0,0,0); |
|
| 54 | + for($i=7;$i>=0;$i--) |
|
| 55 | + { |
|
| 56 | + $cur_counter[$i] = pack ('C*', $counter); |
|
| 57 | + $counter = $counter >> 8; |
|
| 58 | + } |
|
| 59 | + $bin_counter = implode($cur_counter); |
|
| 60 | + // Pad to 8 chars |
|
| 61 | + if (strlen ($bin_counter) < 8) |
|
| 62 | + { |
|
| 63 | + $bin_counter = str_repeat (chr(0), 8 - strlen ($bin_counter)) . $bin_counter; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - // HMAC |
|
| 67 | - $hash = hash_hmac ('sha1', $bin_counter, $secret); |
|
| 68 | - return $hash; |
|
| 66 | + // HMAC |
|
| 67 | + $hash = hash_hmac ('sha1', $bin_counter, $secret); |
|
| 68 | + return $hash; |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | /** |
@@ -76,22 +76,22 @@ discard block |
||
| 76 | 76 | */ |
| 77 | 77 | protected function _truncate($hash, $length = 6) |
| 78 | 78 | { |
| 79 | - // Convert to dec |
|
| 80 | - foreach(str_split($hash,2) as $hex) |
|
| 81 | - { |
|
| 82 | - $hmac_result[]=hexdec($hex); |
|
| 83 | - } |
|
| 79 | + // Convert to dec |
|
| 80 | + foreach(str_split($hash,2) as $hex) |
|
| 81 | + { |
|
| 82 | + $hmac_result[]=hexdec($hex); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - // Find offset |
|
| 86 | - $offset = $hmac_result[19] & 0xf; |
|
| 85 | + // Find offset |
|
| 86 | + $offset = $hmac_result[19] & 0xf; |
|
| 87 | 87 | |
| 88 | - // Algorithm from RFC |
|
| 89 | - return |
|
| 90 | - ( |
|
| 91 | - (($hmac_result[$offset+0] & 0x7f) << 24 ) | |
|
| 92 | - (($hmac_result[$offset+1] & 0xff) << 16 ) | |
|
| 93 | - (($hmac_result[$offset+2] & 0xff) << 8 ) | |
|
| 94 | - ($hmac_result[$offset+3] & 0xff) |
|
| 95 | - ) % pow(10,$length); |
|
| 88 | + // Algorithm from RFC |
|
| 89 | + return |
|
| 90 | + ( |
|
| 91 | + (($hmac_result[$offset+0] & 0x7f) << 24 ) | |
|
| 92 | + (($hmac_result[$offset+1] & 0xff) << 16 ) | |
|
| 93 | + (($hmac_result[$offset+2] & 0xff) << 8 ) | |
|
| 94 | + ($hmac_result[$offset+3] & 0xff) |
|
| 95 | + ) % pow(10,$length); |
|
| 96 | 96 | } |
| 97 | 97 | } |
@@ -46,8 +46,8 @@ discard block |
||
| 46 | 46 | $keyBytes, |
| 47 | 47 | $text) |
| 48 | 48 | { |
| 49 | - $hash = hash_hmac ($crypto, $text, $keyBytes); |
|
| 50 | - return $hash; |
|
| 49 | + $hash = hash_hmac ($crypto, $text, $keyBytes); |
|
| 50 | + return $hash; |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
@@ -83,12 +83,12 @@ discard block |
||
| 83 | 83 | * {@link truncationDigits} digits |
| 84 | 84 | */ |
| 85 | 85 | static function generateOCRA($ocraSuite, |
| 86 | - $key, |
|
| 87 | - $counter, |
|
| 88 | - $question, |
|
| 89 | - $password, |
|
| 90 | - $sessionInformation, |
|
| 91 | - $timeStamp) |
|
| 86 | + $key, |
|
| 87 | + $counter, |
|
| 88 | + $question, |
|
| 89 | + $password, |
|
| 90 | + $sessionInformation, |
|
| 91 | + $timeStamp) |
|
| 92 | 92 | { |
| 93 | 93 | $codeDigits = 0; |
| 94 | 94 | $crypto = ""; |
@@ -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 |
@@ -2,295 +2,295 @@ |
||
| 2 | 2 | |
| 3 | 3 | class OATH_OCRAParser { |
| 4 | 4 | |
| 5 | - private $key = NULL; |
|
| 6 | - |
|
| 7 | - private $OCRASuite = NULL; |
|
| 8 | - |
|
| 9 | - private $OCRAVersion = NULL; |
|
| 10 | - |
|
| 11 | - private $CryptoFunctionType = NULL; |
|
| 12 | - private $CryptoFunctionHash = NULL; |
|
| 13 | - private $CryptoFunctionHashLength = NULL; |
|
| 14 | - private $CryptoFunctionTruncation = NULL; |
|
| 15 | - |
|
| 16 | - private $C = FALSE; |
|
| 17 | - private $Q = FALSE; |
|
| 18 | - private $QType = 'N'; |
|
| 19 | - private $QLength = 8; |
|
| 20 | - |
|
| 21 | - private $P = FALSE; |
|
| 22 | - private $PType = 'SHA1'; |
|
| 23 | - private $PLength = 20; |
|
| 24 | - |
|
| 25 | - private $S = FALSE; |
|
| 26 | - private $SLength = 64; |
|
| 27 | - |
|
| 28 | - private $T = FALSE; |
|
| 29 | - private $TLength = 60; // 1M |
|
| 30 | - private $TPeriods = array('H' => 3600, 'M' => 60, 'S' => 1); |
|
| 31 | - |
|
| 32 | - private $supportedHashFunctions = array('SHA1' => 20, 'SHA256' => 32, 'SHA512' => 64); |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - public function __construct($ocraSuite) { |
|
| 36 | - $this->parseOCRASuite($ocraSuite); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Inspired by https://github.com/bdauvergne/python-oath |
|
| 41 | - */ |
|
| 42 | - private function parseOCRASuite($ocraSuite) { |
|
| 43 | - if (!is_string($ocraSuite)) { |
|
| 44 | - throw new Exception('OCRASuite not in string format: ' . var_export($ocraSuite, TRUE)); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $ocraSuite = strtoupper($ocraSuite); |
|
| 48 | - $this->OCRASuite = $ocraSuite; |
|
| 49 | - |
|
| 50 | - $s = explode(':', $ocraSuite); |
|
| 51 | - if (count($s) != 3) { |
|
| 52 | - throw new Exception('Invalid OCRASuite format: ' . var_export($ocraSuite, TRUE)); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $algo = explode('-', $s[0]); |
|
| 56 | - if (count($algo) != 2) { |
|
| 57 | - throw new Exception('Invalid OCRA version: ' . var_export($s[0], TRUE)); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - if ($algo[0] !== 'OCRA') { |
|
| 61 | - throw new Exception('Unsupported OCRA algorithm: ' . var_export($algo[0], TRUE)); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - if ($algo[1] !== '1') { |
|
| 65 | - throw new Exception('Unsupported OCRA version: ' . var_export($algo[1], TRUE)); |
|
| 66 | - } |
|
| 67 | - $this->OCRAVersion = $algo[1]; |
|
| 68 | - |
|
| 69 | - $cf = explode('-', $s[1]); |
|
| 70 | - if (count($cf) != 3) { |
|
| 71 | - throw new Exception('Invalid OCRA suite crypto function: ' . var_export($s[1], TRUE)); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - if ($cf[0] !== 'HOTP') { |
|
| 75 | - throw new Exception('Unsupported OCRA suite crypto function: ' . var_export($cf[0], TRUE)); |
|
| 76 | - } |
|
| 77 | - $this->CryptoFunctionType = $cf[0]; |
|
| 78 | - |
|
| 79 | - if (!array_key_exists($cf[1], $this->supportedHashFunctions)) { |
|
| 80 | - throw new Exception('Unsupported hash function in OCRA suite crypto function: ' . var_export($cf[1], TRUE)); |
|
| 81 | - } |
|
| 82 | - $this->CryptoFunctionHash = $cf[1]; |
|
| 83 | - $this->CryptoFunctionHashLength = $this->supportedHashFunctions[$cf[1]]; |
|
| 84 | - |
|
| 85 | - if (!preg_match('/^\d+$/', $cf[2]) || (($cf[2] < 4 || $cf[2] > 10) && $cf[2] != 0)) { |
|
| 86 | - throw new Exception('Invalid OCRA suite crypto function truncation length: ' . var_export($cf[2], TRUE)); |
|
| 87 | - } |
|
| 88 | - $this->CryptoFunctionTruncation = intval($cf[2]); |
|
| 89 | - |
|
| 90 | - $di = explode('-', $s[2]); |
|
| 91 | - if (count($cf) == 0) { |
|
| 92 | - throw new Exception('Invalid OCRA suite data input: ' . var_export($s[2], TRUE)); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - $data_input = array(); |
|
| 96 | - foreach($di as $elem) { |
|
| 97 | - $letter = $elem[0]; |
|
| 98 | - if (array_key_exists($letter, $data_input)) { |
|
| 99 | - throw new Exception('Duplicate field in OCRA suite data input: ' . var_export($elem, TRUE)); |
|
| 100 | - } |
|
| 101 | - $data_input[$letter] = 1; |
|
| 102 | - |
|
| 103 | - if ($letter === 'C' && strlen($elem) == 1) { |
|
| 104 | - $this->C = TRUE; |
|
| 105 | - } elseif ($letter === 'Q') { |
|
| 106 | - if (strlen($elem) == 1) { |
|
| 107 | - $this->Q = TRUE; |
|
| 108 | - } elseif (preg_match('/^Q([AHN])(\d+)$/', $elem, $match)) { |
|
| 109 | - $q_len = intval($match[2]); |
|
| 110 | - if ($q_len < 4 || $q_len > 64) { |
|
| 111 | - throw new Exception('Invalid OCRA suite data input question length: ' . var_export($q_len, TRUE)); |
|
| 112 | - } |
|
| 113 | - $this->Q = TRUE; |
|
| 114 | - $this->QType = $match[1]; |
|
| 115 | - $this->QLength = $q_len; |
|
| 116 | - } else { |
|
| 117 | - throw new Exception('Invalid OCRA suite data input question: ' . var_export($elem, TRUE)); |
|
| 118 | - } |
|
| 119 | - } elseif ($letter === 'P') { |
|
| 120 | - if (strlen($elem) == 1) { |
|
| 121 | - $this->P = TRUE; |
|
| 122 | - } else { |
|
| 123 | - $p_algo = substr($elem, 1); |
|
| 124 | - if (!array_key_exists($p_algo, $this->supportedHashFunctions)) { |
|
| 125 | - throw new Exception('Unsupported OCRA suite PIN hash function: ' . var_export($elem, TRUE)); |
|
| 126 | - } |
|
| 127 | - $this->P = TRUE; |
|
| 128 | - $this->PType = $p_algo; |
|
| 129 | - $this->PLength = $this->supportedHashFunctions[$p_algo]; |
|
| 130 | - } |
|
| 131 | - } elseif ($letter === 'S') { |
|
| 132 | - if (strlen($elem) == 1) { |
|
| 133 | - $this->S = TRUE; |
|
| 134 | - } elseif (preg_match('/^S(\d+)$/', $elem, $match)) { |
|
| 135 | - $s_len = intval($match[1]); |
|
| 136 | - if ($s_len <= 0 || $s_len > 512) { |
|
| 137 | - throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($s_len, TRUE)); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - $this->S = TRUE; |
|
| 141 | - $this->SLength = $s_len; |
|
| 142 | - } else { |
|
| 143 | - throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($elem, TRUE)); |
|
| 144 | - } |
|
| 145 | - } elseif ($letter === 'T') { |
|
| 146 | - if (strlen($elem) == 1) { |
|
| 147 | - $this->T = TRUE; |
|
| 148 | - } elseif (preg_match('/^T(\d+[HMS])+$/', $elem)) { |
|
| 149 | - preg_match_all('/(\d+)([HMS])/', $elem, $match); |
|
| 150 | - |
|
| 151 | - if (count($match[1]) !== count(array_unique($match[2]))) { |
|
| 152 | - throw new Exception('Duplicate definitions in OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $length = 0; |
|
| 156 | - for ($i = 0; $i < count($match[1]); $i++) { |
|
| 157 | - $length += intval($match[1][$i]) * $this->TPeriods[$match[2][$i]]; |
|
| 158 | - } |
|
| 159 | - if ($length <= 0) { |
|
| 160 | - throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $this->T = TRUE; |
|
| 164 | - $this->TLength = $length; |
|
| 165 | - } else { |
|
| 166 | - throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
| 167 | - } |
|
| 168 | - } else { |
|
| 169 | - throw new Exception('Unsupported OCRA suite data input field: ' . var_export($elem, TRUE)); |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - if (!$this->Q) { |
|
| 174 | - throw new Exception('OCRA suite data input question not defined: ' . var_export($s[2], TRUE)); |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - public function generateChallenge() { |
|
| 179 | - $q_length = $this->QLength; |
|
| 180 | - $q_type = $this->QType; |
|
| 181 | - |
|
| 182 | - $bytes = self::generateRandomBytes($q_length); |
|
| 183 | - |
|
| 184 | - switch($q_type) { |
|
| 185 | - case 'A': |
|
| 186 | - $challenge = base64_encode($bytes); |
|
| 187 | - $tr = implode("", unpack('H*', $bytes)); |
|
| 188 | - $challenge = rtrim(strtr($challenge, '+/', $tr), '='); |
|
| 189 | - break; |
|
| 190 | - case 'H': |
|
| 191 | - $challenge = implode("", unpack('H*', $bytes)); |
|
| 192 | - break; |
|
| 193 | - case 'N': |
|
| 194 | - $challenge = implode("", unpack('N*', $bytes)); |
|
| 195 | - break; |
|
| 196 | - default: |
|
| 197 | - throw new Exception('Unsupported OCRASuite challenge type: ' . var_export($q_type, TRUE)); |
|
| 198 | - break; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $challenge = substr($challenge, 0, $q_length); |
|
| 202 | - |
|
| 203 | - return $challenge; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - |
|
| 207 | - public function generateSessionInformation() { |
|
| 208 | - if (!$this->S) { |
|
| 209 | - throw new Exception('Session information not defined in OCRASuite: ' . var_export($this->OCRASuite, TRUE)); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - $s_length = $this->SLength; |
|
| 213 | - $bytes = self::generateRandomBytes($s_length); |
|
| 214 | - |
|
| 215 | - // The OCRA spec doesn't specify that the session data should be hexadecimal. |
|
| 216 | - // However the reference implementation in the RFC does treat it as hex. |
|
| 217 | - $session = bin2hex($bytes); |
|
| 5 | + private $key = NULL; |
|
| 6 | + |
|
| 7 | + private $OCRASuite = NULL; |
|
| 8 | + |
|
| 9 | + private $OCRAVersion = NULL; |
|
| 10 | + |
|
| 11 | + private $CryptoFunctionType = NULL; |
|
| 12 | + private $CryptoFunctionHash = NULL; |
|
| 13 | + private $CryptoFunctionHashLength = NULL; |
|
| 14 | + private $CryptoFunctionTruncation = NULL; |
|
| 15 | + |
|
| 16 | + private $C = FALSE; |
|
| 17 | + private $Q = FALSE; |
|
| 18 | + private $QType = 'N'; |
|
| 19 | + private $QLength = 8; |
|
| 20 | + |
|
| 21 | + private $P = FALSE; |
|
| 22 | + private $PType = 'SHA1'; |
|
| 23 | + private $PLength = 20; |
|
| 24 | + |
|
| 25 | + private $S = FALSE; |
|
| 26 | + private $SLength = 64; |
|
| 27 | + |
|
| 28 | + private $T = FALSE; |
|
| 29 | + private $TLength = 60; // 1M |
|
| 30 | + private $TPeriods = array('H' => 3600, 'M' => 60, 'S' => 1); |
|
| 31 | + |
|
| 32 | + private $supportedHashFunctions = array('SHA1' => 20, 'SHA256' => 32, 'SHA512' => 64); |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + public function __construct($ocraSuite) { |
|
| 36 | + $this->parseOCRASuite($ocraSuite); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Inspired by https://github.com/bdauvergne/python-oath |
|
| 41 | + */ |
|
| 42 | + private function parseOCRASuite($ocraSuite) { |
|
| 43 | + if (!is_string($ocraSuite)) { |
|
| 44 | + throw new Exception('OCRASuite not in string format: ' . var_export($ocraSuite, TRUE)); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $ocraSuite = strtoupper($ocraSuite); |
|
| 48 | + $this->OCRASuite = $ocraSuite; |
|
| 49 | + |
|
| 50 | + $s = explode(':', $ocraSuite); |
|
| 51 | + if (count($s) != 3) { |
|
| 52 | + throw new Exception('Invalid OCRASuite format: ' . var_export($ocraSuite, TRUE)); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $algo = explode('-', $s[0]); |
|
| 56 | + if (count($algo) != 2) { |
|
| 57 | + throw new Exception('Invalid OCRA version: ' . var_export($s[0], TRUE)); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + if ($algo[0] !== 'OCRA') { |
|
| 61 | + throw new Exception('Unsupported OCRA algorithm: ' . var_export($algo[0], TRUE)); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + if ($algo[1] !== '1') { |
|
| 65 | + throw new Exception('Unsupported OCRA version: ' . var_export($algo[1], TRUE)); |
|
| 66 | + } |
|
| 67 | + $this->OCRAVersion = $algo[1]; |
|
| 68 | + |
|
| 69 | + $cf = explode('-', $s[1]); |
|
| 70 | + if (count($cf) != 3) { |
|
| 71 | + throw new Exception('Invalid OCRA suite crypto function: ' . var_export($s[1], TRUE)); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + if ($cf[0] !== 'HOTP') { |
|
| 75 | + throw new Exception('Unsupported OCRA suite crypto function: ' . var_export($cf[0], TRUE)); |
|
| 76 | + } |
|
| 77 | + $this->CryptoFunctionType = $cf[0]; |
|
| 78 | + |
|
| 79 | + if (!array_key_exists($cf[1], $this->supportedHashFunctions)) { |
|
| 80 | + throw new Exception('Unsupported hash function in OCRA suite crypto function: ' . var_export($cf[1], TRUE)); |
|
| 81 | + } |
|
| 82 | + $this->CryptoFunctionHash = $cf[1]; |
|
| 83 | + $this->CryptoFunctionHashLength = $this->supportedHashFunctions[$cf[1]]; |
|
| 84 | + |
|
| 85 | + if (!preg_match('/^\d+$/', $cf[2]) || (($cf[2] < 4 || $cf[2] > 10) && $cf[2] != 0)) { |
|
| 86 | + throw new Exception('Invalid OCRA suite crypto function truncation length: ' . var_export($cf[2], TRUE)); |
|
| 87 | + } |
|
| 88 | + $this->CryptoFunctionTruncation = intval($cf[2]); |
|
| 89 | + |
|
| 90 | + $di = explode('-', $s[2]); |
|
| 91 | + if (count($cf) == 0) { |
|
| 92 | + throw new Exception('Invalid OCRA suite data input: ' . var_export($s[2], TRUE)); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + $data_input = array(); |
|
| 96 | + foreach($di as $elem) { |
|
| 97 | + $letter = $elem[0]; |
|
| 98 | + if (array_key_exists($letter, $data_input)) { |
|
| 99 | + throw new Exception('Duplicate field in OCRA suite data input: ' . var_export($elem, TRUE)); |
|
| 100 | + } |
|
| 101 | + $data_input[$letter] = 1; |
|
| 102 | + |
|
| 103 | + if ($letter === 'C' && strlen($elem) == 1) { |
|
| 104 | + $this->C = TRUE; |
|
| 105 | + } elseif ($letter === 'Q') { |
|
| 106 | + if (strlen($elem) == 1) { |
|
| 107 | + $this->Q = TRUE; |
|
| 108 | + } elseif (preg_match('/^Q([AHN])(\d+)$/', $elem, $match)) { |
|
| 109 | + $q_len = intval($match[2]); |
|
| 110 | + if ($q_len < 4 || $q_len > 64) { |
|
| 111 | + throw new Exception('Invalid OCRA suite data input question length: ' . var_export($q_len, TRUE)); |
|
| 112 | + } |
|
| 113 | + $this->Q = TRUE; |
|
| 114 | + $this->QType = $match[1]; |
|
| 115 | + $this->QLength = $q_len; |
|
| 116 | + } else { |
|
| 117 | + throw new Exception('Invalid OCRA suite data input question: ' . var_export($elem, TRUE)); |
|
| 118 | + } |
|
| 119 | + } elseif ($letter === 'P') { |
|
| 120 | + if (strlen($elem) == 1) { |
|
| 121 | + $this->P = TRUE; |
|
| 122 | + } else { |
|
| 123 | + $p_algo = substr($elem, 1); |
|
| 124 | + if (!array_key_exists($p_algo, $this->supportedHashFunctions)) { |
|
| 125 | + throw new Exception('Unsupported OCRA suite PIN hash function: ' . var_export($elem, TRUE)); |
|
| 126 | + } |
|
| 127 | + $this->P = TRUE; |
|
| 128 | + $this->PType = $p_algo; |
|
| 129 | + $this->PLength = $this->supportedHashFunctions[$p_algo]; |
|
| 130 | + } |
|
| 131 | + } elseif ($letter === 'S') { |
|
| 132 | + if (strlen($elem) == 1) { |
|
| 133 | + $this->S = TRUE; |
|
| 134 | + } elseif (preg_match('/^S(\d+)$/', $elem, $match)) { |
|
| 135 | + $s_len = intval($match[1]); |
|
| 136 | + if ($s_len <= 0 || $s_len > 512) { |
|
| 137 | + throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($s_len, TRUE)); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + $this->S = TRUE; |
|
| 141 | + $this->SLength = $s_len; |
|
| 142 | + } else { |
|
| 143 | + throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($elem, TRUE)); |
|
| 144 | + } |
|
| 145 | + } elseif ($letter === 'T') { |
|
| 146 | + if (strlen($elem) == 1) { |
|
| 147 | + $this->T = TRUE; |
|
| 148 | + } elseif (preg_match('/^T(\d+[HMS])+$/', $elem)) { |
|
| 149 | + preg_match_all('/(\d+)([HMS])/', $elem, $match); |
|
| 150 | + |
|
| 151 | + if (count($match[1]) !== count(array_unique($match[2]))) { |
|
| 152 | + throw new Exception('Duplicate definitions in OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $length = 0; |
|
| 156 | + for ($i = 0; $i < count($match[1]); $i++) { |
|
| 157 | + $length += intval($match[1][$i]) * $this->TPeriods[$match[2][$i]]; |
|
| 158 | + } |
|
| 159 | + if ($length <= 0) { |
|
| 160 | + throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $this->T = TRUE; |
|
| 164 | + $this->TLength = $length; |
|
| 165 | + } else { |
|
| 166 | + throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
| 167 | + } |
|
| 168 | + } else { |
|
| 169 | + throw new Exception('Unsupported OCRA suite data input field: ' . var_export($elem, TRUE)); |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + if (!$this->Q) { |
|
| 174 | + throw new Exception('OCRA suite data input question not defined: ' . var_export($s[2], TRUE)); |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + public function generateChallenge() { |
|
| 179 | + $q_length = $this->QLength; |
|
| 180 | + $q_type = $this->QType; |
|
| 181 | + |
|
| 182 | + $bytes = self::generateRandomBytes($q_length); |
|
| 183 | + |
|
| 184 | + switch($q_type) { |
|
| 185 | + case 'A': |
|
| 186 | + $challenge = base64_encode($bytes); |
|
| 187 | + $tr = implode("", unpack('H*', $bytes)); |
|
| 188 | + $challenge = rtrim(strtr($challenge, '+/', $tr), '='); |
|
| 189 | + break; |
|
| 190 | + case 'H': |
|
| 191 | + $challenge = implode("", unpack('H*', $bytes)); |
|
| 192 | + break; |
|
| 193 | + case 'N': |
|
| 194 | + $challenge = implode("", unpack('N*', $bytes)); |
|
| 195 | + break; |
|
| 196 | + default: |
|
| 197 | + throw new Exception('Unsupported OCRASuite challenge type: ' . var_export($q_type, TRUE)); |
|
| 198 | + break; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $challenge = substr($challenge, 0, $q_length); |
|
| 202 | + |
|
| 203 | + return $challenge; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + |
|
| 207 | + public function generateSessionInformation() { |
|
| 208 | + if (!$this->S) { |
|
| 209 | + throw new Exception('Session information not defined in OCRASuite: ' . var_export($this->OCRASuite, TRUE)); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + $s_length = $this->SLength; |
|
| 213 | + $bytes = self::generateRandomBytes($s_length); |
|
| 214 | + |
|
| 215 | + // The OCRA spec doesn't specify that the session data should be hexadecimal. |
|
| 216 | + // However the reference implementation in the RFC does treat it as hex. |
|
| 217 | + $session = bin2hex($bytes); |
|
| 218 | 218 | |
| 219 | - $session = substr($session, 0, $s_length); |
|
| 219 | + $session = substr($session, 0, $s_length); |
|
| 220 | 220 | |
| 221 | - return $session; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Borrowed from SimpleSAMLPHP http://simplesamlphp.org/ |
|
| 226 | - */ |
|
| 227 | - public static function generateRandomBytesMTrand($length) { |
|
| 228 | - |
|
| 229 | - /* Use mt_rand to generate $length random bytes. */ |
|
| 230 | - $data = ''; |
|
| 231 | - for($i = 0; $i < $length; $i++) { |
|
| 232 | - $data .= chr(mt_rand(0, 255)); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - return $data; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Borrowed from SimpleSAMLPHP http://simplesamlphp.org/ |
|
| 241 | - */ |
|
| 242 | - public static function generateRandomBytes($length, $fallback = TRUE) { |
|
| 243 | - static $fp = NULL; |
|
| 244 | - |
|
| 245 | - if (function_exists('openssl_random_pseudo_bytes')) { |
|
| 246 | - return openssl_random_pseudo_bytes($length); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - if($fp === NULL) { |
|
| 250 | - if (@file_exists('/dev/urandom')) { |
|
| 251 | - $fp = @fopen('/dev/urandom', 'rb'); |
|
| 252 | - } else { |
|
| 253 | - $fp = FALSE; |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - if($fp !== FALSE) { |
|
| 258 | - /* Read random bytes from /dev/urandom. */ |
|
| 259 | - $data = fread($fp, $length); |
|
| 260 | - if($data === FALSE) { |
|
| 261 | - throw new Exception('Error reading random data.'); |
|
| 262 | - } |
|
| 263 | - if(strlen($data) != $length) { |
|
| 264 | - if ($fallback) { |
|
| 265 | - $data = self::generateRandomBytesMTrand($length); |
|
| 266 | - } else { |
|
| 267 | - throw new Exception('Did not get requested number of bytes from random source. Requested (' . $length . ') got (' . strlen($data) . ')'); |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - } else { |
|
| 271 | - /* Use mt_rand to generate $length random bytes. */ |
|
| 272 | - $data = self::generateRandomBytesMTrand($length); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - return $data; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Constant time string comparison, see http://codahale.com/a-lesson-in-timing-attacks/ |
|
| 281 | - */ |
|
| 282 | - public static function constEqual($s1, $s2) { |
|
| 283 | - if (strlen($s1) != strlen($s2)) { |
|
| 284 | - return FALSE; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - $result = TRUE; |
|
| 288 | - $length = strlen($s1); |
|
| 289 | - for ($i = 0; $i < $length; $i++) { |
|
| 290 | - $result &= ($s1[$i] == $s2[$i]); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - return (boolean)$result; |
|
| 294 | - } |
|
| 221 | + return $session; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Borrowed from SimpleSAMLPHP http://simplesamlphp.org/ |
|
| 226 | + */ |
|
| 227 | + public static function generateRandomBytesMTrand($length) { |
|
| 228 | + |
|
| 229 | + /* Use mt_rand to generate $length random bytes. */ |
|
| 230 | + $data = ''; |
|
| 231 | + for($i = 0; $i < $length; $i++) { |
|
| 232 | + $data .= chr(mt_rand(0, 255)); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + return $data; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Borrowed from SimpleSAMLPHP http://simplesamlphp.org/ |
|
| 241 | + */ |
|
| 242 | + public static function generateRandomBytes($length, $fallback = TRUE) { |
|
| 243 | + static $fp = NULL; |
|
| 244 | + |
|
| 245 | + if (function_exists('openssl_random_pseudo_bytes')) { |
|
| 246 | + return openssl_random_pseudo_bytes($length); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + if($fp === NULL) { |
|
| 250 | + if (@file_exists('/dev/urandom')) { |
|
| 251 | + $fp = @fopen('/dev/urandom', 'rb'); |
|
| 252 | + } else { |
|
| 253 | + $fp = FALSE; |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + if($fp !== FALSE) { |
|
| 258 | + /* Read random bytes from /dev/urandom. */ |
|
| 259 | + $data = fread($fp, $length); |
|
| 260 | + if($data === FALSE) { |
|
| 261 | + throw new Exception('Error reading random data.'); |
|
| 262 | + } |
|
| 263 | + if(strlen($data) != $length) { |
|
| 264 | + if ($fallback) { |
|
| 265 | + $data = self::generateRandomBytesMTrand($length); |
|
| 266 | + } else { |
|
| 267 | + throw new Exception('Did not get requested number of bytes from random source. Requested (' . $length . ') got (' . strlen($data) . ')'); |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + } else { |
|
| 271 | + /* Use mt_rand to generate $length random bytes. */ |
|
| 272 | + $data = self::generateRandomBytesMTrand($length); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + return $data; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Constant time string comparison, see http://codahale.com/a-lesson-in-timing-attacks/ |
|
| 281 | + */ |
|
| 282 | + public static function constEqual($s1, $s2) { |
|
| 283 | + if (strlen($s1) != strlen($s2)) { |
|
| 284 | + return FALSE; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + $result = TRUE; |
|
| 288 | + $length = strlen($s1); |
|
| 289 | + for ($i = 0; $i < $length; $i++) { |
|
| 290 | + $result &= ($s1[$i] == $s2[$i]); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + return (boolean)$result; |
|
| 294 | + } |
|
| 295 | 295 | |
| 296 | 296 | } |
@@ -4,127 +4,127 @@ |
||
| 4 | 4 | |
| 5 | 5 | class OATH_OCRATest extends PHPUnit_Framework_TestCase { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * @dataProvider RFCVectorsProvider |
|
| 9 | - */ |
|
| 10 | - public function testRFCVectors($ocrasuite, $key, $datainput, $expected_result) { |
|
| 11 | - $ocra = new OATH_OCRA($ocrasuite, $key, NULL, $datainput['Q']); |
|
| 12 | - $ocra->setKey($key, 'hexstring'); |
|
| 13 | - $ocra->setQuestion($datainput['Q']); |
|
| 14 | - if (isset($datainput['C'])) { |
|
| 15 | - $ocra->setCounter($datainput['C']); |
|
| 16 | - } |
|
| 17 | - if (isset($datainput['P'])) { |
|
| 18 | - $ocra->setPin($datainput['P']); |
|
| 19 | - } elseif (isset($datainput['P:hexdigest'])) { |
|
| 20 | - $ocra->setPin($datainput['P:hexdigest'], 'hexdigest'); |
|
| 21 | - } |
|
| 22 | - if (isset($datainput['T'])) { |
|
| 23 | - $ocra->setTimestamp($datainput['T']); |
|
| 24 | - } |
|
| 25 | - $this->assertTrue($ocra->verifyResponse($expected_result)); |
|
| 26 | - } |
|
| 7 | + /** |
|
| 8 | + * @dataProvider RFCVectorsProvider |
|
| 9 | + */ |
|
| 10 | + public function testRFCVectors($ocrasuite, $key, $datainput, $expected_result) { |
|
| 11 | + $ocra = new OATH_OCRA($ocrasuite, $key, NULL, $datainput['Q']); |
|
| 12 | + $ocra->setKey($key, 'hexstring'); |
|
| 13 | + $ocra->setQuestion($datainput['Q']); |
|
| 14 | + if (isset($datainput['C'])) { |
|
| 15 | + $ocra->setCounter($datainput['C']); |
|
| 16 | + } |
|
| 17 | + if (isset($datainput['P'])) { |
|
| 18 | + $ocra->setPin($datainput['P']); |
|
| 19 | + } elseif (isset($datainput['P:hexdigest'])) { |
|
| 20 | + $ocra->setPin($datainput['P:hexdigest'], 'hexdigest'); |
|
| 21 | + } |
|
| 22 | + if (isset($datainput['T'])) { |
|
| 23 | + $ocra->setTimestamp($datainput['T']); |
|
| 24 | + } |
|
| 25 | + $this->assertTrue($ocra->verifyResponse($expected_result)); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | 28 | |
| 29 | - public function RFCVectorsProvider() { |
|
| 30 | - $pin = '1234'; |
|
| 31 | - $pin_sha1 = '7110eda4d09e062aa5e4a390b0a572ac0d2c0220'; |
|
| 29 | + public function RFCVectorsProvider() { |
|
| 30 | + $pin = '1234'; |
|
| 31 | + $pin_sha1 = '7110eda4d09e062aa5e4a390b0a572ac0d2c0220'; |
|
| 32 | 32 | |
| 33 | - $key20 = '3132333435363738393031323334353637383930'; |
|
| 34 | - $key32 = '3132333435363738393031323334353637383930313233343536373839303132'; |
|
| 35 | - $key64 = '31323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334'; |
|
| 33 | + $key20 = '3132333435363738393031323334353637383930'; |
|
| 34 | + $key32 = '3132333435363738393031323334353637383930313233343536373839303132'; |
|
| 35 | + $key64 = '31323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334'; |
|
| 36 | 36 | |
| 37 | - $tests = array( |
|
| 38 | - array( |
|
| 39 | - 'ocrasuite' => 'OCRA-1:HOTP-SHA1-6:QN08', |
|
| 40 | - 'key' => $key20, |
|
| 41 | - 'vectors' => array( |
|
| 42 | - array('params' => array( 'Q' => '00000000' ), 'result' => '237653' ), |
|
| 43 | - array('params' => array( 'Q' => '11111111' ), 'result' => '243178' ), |
|
| 44 | - array('params' => array( 'Q' => '22222222' ), 'result' => '653583' ), |
|
| 45 | - array('params' => array( 'Q' => '33333333' ), 'result' => '740991' ), |
|
| 46 | - array('params' => array( 'Q' => '44444444' ), 'result' => '608993' ), |
|
| 47 | - array('params' => array( 'Q' => '55555555' ), 'result' => '388898' ), |
|
| 48 | - array('params' => array( 'Q' => '66666666' ), 'result' => '816933' ), |
|
| 49 | - array('params' => array( 'Q' => '77777777' ), 'result' => '224598' ), |
|
| 50 | - array('params' => array( 'Q' => '88888888' ), 'result' => '750600' ), |
|
| 51 | - array('params' => array( 'Q' => '99999999' ), 'result' => '294470' ), |
|
| 52 | - ) |
|
| 53 | - ), |
|
| 54 | - array( |
|
| 55 | - 'ocrasuite' => 'OCRA-1:HOTP-SHA256-8:C-QN08-PSHA1', |
|
| 56 | - 'key' => $key32, |
|
| 57 | - 'pin_sha1' => $pin_sha1, |
|
| 58 | - 'vectors' => array( |
|
| 59 | - array('params' => array( 'C' => 0, 'Q' => '12345678' ), 'result' => '65347737' ), |
|
| 60 | - array('params' => array( 'C' => 1, 'Q' => '12345678' ), 'result' => '86775851' ), |
|
| 61 | - array('params' => array( 'C' => 2, 'Q' => '12345678' ), 'result' => '78192410' ), |
|
| 62 | - array('params' => array( 'C' => 3, 'Q' => '12345678' ), 'result' => '71565254' ), |
|
| 63 | - array('params' => array( 'C' => 4, 'Q' => '12345678' ), 'result' => '10104329' ), |
|
| 64 | - array('params' => array( 'C' => 5, 'Q' => '12345678' ), 'result' => '65983500' ), |
|
| 65 | - array('params' => array( 'C' => 6, 'Q' => '12345678' ), 'result' => '70069104' ), |
|
| 66 | - array('params' => array( 'C' => 7, 'Q' => '12345678' ), 'result' => '91771096' ), |
|
| 67 | - array('params' => array( 'C' => 8, 'Q' => '12345678' ), 'result' => '75011558' ), |
|
| 68 | - array('params' => array( 'C' => 9, 'Q' => '12345678' ), 'result' => '08522129' ), |
|
| 69 | - ) |
|
| 70 | - ), |
|
| 71 | - array( |
|
| 72 | - 'ocrasuite' => 'OCRA-1:HOTP-SHA256-8:QN08-PSHA1', |
|
| 73 | - 'key' => $key32, |
|
| 74 | - 'pin_sha1' => $pin_sha1, |
|
| 75 | - 'vectors' => array( |
|
| 76 | - array('params' => array( 'Q' => '00000000' ), 'result' => '83238735' ), |
|
| 77 | - array('params' => array( 'Q' => '11111111' ), 'result' => '01501458' ), |
|
| 78 | - array('params' => array( 'Q' => '22222222' ), 'result' => '17957585' ), |
|
| 79 | - array('params' => array( 'Q' => '33333333' ), 'result' => '86776967' ), |
|
| 80 | - array('params' => array( 'Q' => '44444444' ), 'result' => '86807031' ), |
|
| 81 | - ) |
|
| 82 | - ), |
|
| 83 | - array( |
|
| 84 | - 'ocrasuite' => 'OCRA-1:HOTP-SHA512-8:C-QN08', |
|
| 85 | - 'key' => $key64, |
|
| 86 | - 'vectors' => array( |
|
| 87 | - array('params' => array( 'C' => '00000', 'Q' => '00000000' ), 'result' => '07016083' ), |
|
| 88 | - array('params' => array( 'C' => '00001', 'Q' => '11111111' ), 'result' => '63947962' ), |
|
| 89 | - array('params' => array( 'C' => '00002', 'Q' => '22222222' ), 'result' => '70123924' ), |
|
| 90 | - array('params' => array( 'C' => '00003', 'Q' => '33333333' ), 'result' => '25341727' ), |
|
| 91 | - array('params' => array( 'C' => '00004', 'Q' => '44444444' ), 'result' => '33203315' ), |
|
| 92 | - array('params' => array( 'C' => '00005', 'Q' => '55555555' ), 'result' => '34205738' ), |
|
| 93 | - array('params' => array( 'C' => '00006', 'Q' => '66666666' ), 'result' => '44343969' ), |
|
| 94 | - array('params' => array( 'C' => '00007', 'Q' => '77777777' ), 'result' => '51946085' ), |
|
| 95 | - array('params' => array( 'C' => '00008', 'Q' => '88888888' ), 'result' => '20403879' ), |
|
| 96 | - array('params' => array( 'C' => '00009', 'Q' => '99999999' ), 'result' => '31409299' ), |
|
| 97 | - ) |
|
| 98 | - ), |
|
| 99 | - array( |
|
| 100 | - 'ocrasuite' => 'OCRA-1:HOTP-SHA512-8:QN08-T1M', |
|
| 101 | - 'key' => $key64, |
|
| 102 | - 'vectors' => array( |
|
| 103 | - array('params' => array( 'Q' => '00000000', 'T' => intval('132d0b6', 16) ), 'result' => '95209754' ), |
|
| 104 | - array('params' => array( 'Q' => '11111111', 'T' => intval('132d0b6', 16) ), 'result' => '55907591' ), |
|
| 105 | - array('params' => array( 'Q' => '22222222', 'T' => intval('132d0b6', 16) ), 'result' => '22048402' ), |
|
| 106 | - array('params' => array( 'Q' => '33333333', 'T' => intval('132d0b6', 16) ), 'result' => '24218844' ), |
|
| 107 | - array('params' => array( 'Q' => '44444444', 'T' => intval('132d0b6', 16) ), 'result' => '36209546' ), |
|
| 108 | - ) |
|
| 109 | - ), |
|
| 110 | - ); |
|
| 37 | + $tests = array( |
|
| 38 | + array( |
|
| 39 | + 'ocrasuite' => 'OCRA-1:HOTP-SHA1-6:QN08', |
|
| 40 | + 'key' => $key20, |
|
| 41 | + 'vectors' => array( |
|
| 42 | + array('params' => array( 'Q' => '00000000' ), 'result' => '237653' ), |
|
| 43 | + array('params' => array( 'Q' => '11111111' ), 'result' => '243178' ), |
|
| 44 | + array('params' => array( 'Q' => '22222222' ), 'result' => '653583' ), |
|
| 45 | + array('params' => array( 'Q' => '33333333' ), 'result' => '740991' ), |
|
| 46 | + array('params' => array( 'Q' => '44444444' ), 'result' => '608993' ), |
|
| 47 | + array('params' => array( 'Q' => '55555555' ), 'result' => '388898' ), |
|
| 48 | + array('params' => array( 'Q' => '66666666' ), 'result' => '816933' ), |
|
| 49 | + array('params' => array( 'Q' => '77777777' ), 'result' => '224598' ), |
|
| 50 | + array('params' => array( 'Q' => '88888888' ), 'result' => '750600' ), |
|
| 51 | + array('params' => array( 'Q' => '99999999' ), 'result' => '294470' ), |
|
| 52 | + ) |
|
| 53 | + ), |
|
| 54 | + array( |
|
| 55 | + 'ocrasuite' => 'OCRA-1:HOTP-SHA256-8:C-QN08-PSHA1', |
|
| 56 | + 'key' => $key32, |
|
| 57 | + 'pin_sha1' => $pin_sha1, |
|
| 58 | + 'vectors' => array( |
|
| 59 | + array('params' => array( 'C' => 0, 'Q' => '12345678' ), 'result' => '65347737' ), |
|
| 60 | + array('params' => array( 'C' => 1, 'Q' => '12345678' ), 'result' => '86775851' ), |
|
| 61 | + array('params' => array( 'C' => 2, 'Q' => '12345678' ), 'result' => '78192410' ), |
|
| 62 | + array('params' => array( 'C' => 3, 'Q' => '12345678' ), 'result' => '71565254' ), |
|
| 63 | + array('params' => array( 'C' => 4, 'Q' => '12345678' ), 'result' => '10104329' ), |
|
| 64 | + array('params' => array( 'C' => 5, 'Q' => '12345678' ), 'result' => '65983500' ), |
|
| 65 | + array('params' => array( 'C' => 6, 'Q' => '12345678' ), 'result' => '70069104' ), |
|
| 66 | + array('params' => array( 'C' => 7, 'Q' => '12345678' ), 'result' => '91771096' ), |
|
| 67 | + array('params' => array( 'C' => 8, 'Q' => '12345678' ), 'result' => '75011558' ), |
|
| 68 | + array('params' => array( 'C' => 9, 'Q' => '12345678' ), 'result' => '08522129' ), |
|
| 69 | + ) |
|
| 70 | + ), |
|
| 71 | + array( |
|
| 72 | + 'ocrasuite' => 'OCRA-1:HOTP-SHA256-8:QN08-PSHA1', |
|
| 73 | + 'key' => $key32, |
|
| 74 | + 'pin_sha1' => $pin_sha1, |
|
| 75 | + 'vectors' => array( |
|
| 76 | + array('params' => array( 'Q' => '00000000' ), 'result' => '83238735' ), |
|
| 77 | + array('params' => array( 'Q' => '11111111' ), 'result' => '01501458' ), |
|
| 78 | + array('params' => array( 'Q' => '22222222' ), 'result' => '17957585' ), |
|
| 79 | + array('params' => array( 'Q' => '33333333' ), 'result' => '86776967' ), |
|
| 80 | + array('params' => array( 'Q' => '44444444' ), 'result' => '86807031' ), |
|
| 81 | + ) |
|
| 82 | + ), |
|
| 83 | + array( |
|
| 84 | + 'ocrasuite' => 'OCRA-1:HOTP-SHA512-8:C-QN08', |
|
| 85 | + 'key' => $key64, |
|
| 86 | + 'vectors' => array( |
|
| 87 | + array('params' => array( 'C' => '00000', 'Q' => '00000000' ), 'result' => '07016083' ), |
|
| 88 | + array('params' => array( 'C' => '00001', 'Q' => '11111111' ), 'result' => '63947962' ), |
|
| 89 | + array('params' => array( 'C' => '00002', 'Q' => '22222222' ), 'result' => '70123924' ), |
|
| 90 | + array('params' => array( 'C' => '00003', 'Q' => '33333333' ), 'result' => '25341727' ), |
|
| 91 | + array('params' => array( 'C' => '00004', 'Q' => '44444444' ), 'result' => '33203315' ), |
|
| 92 | + array('params' => array( 'C' => '00005', 'Q' => '55555555' ), 'result' => '34205738' ), |
|
| 93 | + array('params' => array( 'C' => '00006', 'Q' => '66666666' ), 'result' => '44343969' ), |
|
| 94 | + array('params' => array( 'C' => '00007', 'Q' => '77777777' ), 'result' => '51946085' ), |
|
| 95 | + array('params' => array( 'C' => '00008', 'Q' => '88888888' ), 'result' => '20403879' ), |
|
| 96 | + array('params' => array( 'C' => '00009', 'Q' => '99999999' ), 'result' => '31409299' ), |
|
| 97 | + ) |
|
| 98 | + ), |
|
| 99 | + array( |
|
| 100 | + 'ocrasuite' => 'OCRA-1:HOTP-SHA512-8:QN08-T1M', |
|
| 101 | + 'key' => $key64, |
|
| 102 | + 'vectors' => array( |
|
| 103 | + array('params' => array( 'Q' => '00000000', 'T' => intval('132d0b6', 16) ), 'result' => '95209754' ), |
|
| 104 | + array('params' => array( 'Q' => '11111111', 'T' => intval('132d0b6', 16) ), 'result' => '55907591' ), |
|
| 105 | + array('params' => array( 'Q' => '22222222', 'T' => intval('132d0b6', 16) ), 'result' => '22048402' ), |
|
| 106 | + array('params' => array( 'Q' => '33333333', 'T' => intval('132d0b6', 16) ), 'result' => '24218844' ), |
|
| 107 | + array('params' => array( 'Q' => '44444444', 'T' => intval('132d0b6', 16) ), 'result' => '36209546' ), |
|
| 108 | + ) |
|
| 109 | + ), |
|
| 110 | + ); |
|
| 111 | 111 | |
| 112 | - $data = array(); |
|
| 112 | + $data = array(); |
|
| 113 | 113 | |
| 114 | - foreach($tests as $test) { |
|
| 115 | - $ocrasuite = $test['ocrasuite']; |
|
| 116 | - foreach($test['vectors'] as $vector) { |
|
| 117 | - $datainput = $vector['params']; |
|
| 118 | - if (isset($test['pin'])) { |
|
| 119 | - $datainput['P'] = $test['pin']; |
|
| 120 | - } elseif (isset($test['pin_sha1'])) { |
|
| 121 | - $datainput['P:hexdigest'] = $test['pin_sha1']; |
|
| 122 | - } |
|
| 123 | - $data[] = array($ocrasuite, $test['key'], $datainput, $vector['result']); |
|
| 124 | - } |
|
| 125 | - } |
|
| 114 | + foreach($tests as $test) { |
|
| 115 | + $ocrasuite = $test['ocrasuite']; |
|
| 116 | + foreach($test['vectors'] as $vector) { |
|
| 117 | + $datainput = $vector['params']; |
|
| 118 | + if (isset($test['pin'])) { |
|
| 119 | + $datainput['P'] = $test['pin']; |
|
| 120 | + } elseif (isset($test['pin_sha1'])) { |
|
| 121 | + $datainput['P:hexdigest'] = $test['pin_sha1']; |
|
| 122 | + } |
|
| 123 | + $data[] = array($ocrasuite, $test['key'], $datainput, $vector['result']); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - return $data; |
|
| 128 | - } |
|
| 127 | + return $data; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | 130 | } |
@@ -46,8 +46,8 @@ discard block |
||
| 46 | 46 | $keyBytes, |
| 47 | 47 | $text) |
| 48 | 48 | { |
| 49 | - $hash = hash_hmac ($crypto, $text, $keyBytes); |
|
| 50 | - return $hash; |
|
| 49 | + $hash = hash_hmac ($crypto, $text, $keyBytes); |
|
| 50 | + return $hash; |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
@@ -83,12 +83,12 @@ discard block |
||
| 83 | 83 | * {@link truncationDigits} digits |
| 84 | 84 | */ |
| 85 | 85 | static function generateOCRA($ocraSuite, |
| 86 | - $key, |
|
| 87 | - $counter, |
|
| 88 | - $question, |
|
| 89 | - $password, |
|
| 90 | - $sessionInformation, |
|
| 91 | - $timeStamp) |
|
| 86 | + $key, |
|
| 87 | + $counter, |
|
| 88 | + $question, |
|
| 89 | + $password, |
|
| 90 | + $sessionInformation, |
|
| 91 | + $timeStamp) |
|
| 92 | 92 | { |
| 93 | 93 | $codeDigits = 0; |
| 94 | 94 | $crypto = ""; |
@@ -57,7 +57,7 @@ |
||
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
| 60 | - * Decrypts the given data. |
|
| 60 | + * Decrypts the given data. |
|
| 61 | 61 | * |
| 62 | 62 | * @param String $data Data to decrypt. |
| 63 | 63 | * |
@@ -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) { |
@@ -538,17 +538,17 @@ discard block |
||
| 538 | 538 | } |
| 539 | 539 | |
| 540 | 540 | $metadata = array("service"=> |
| 541 | - array("displayName" => $this->_name, |
|
| 542 | - "identifier" => $this->_identifier, |
|
| 543 | - "logoUrl" => $this->_logoUrl, |
|
| 544 | - "infoUrl" => $this->_infoUrl, |
|
| 545 | - "authenticationUrl" => $authenticationUrl, |
|
| 546 | - "ocraSuite" => $this->_ocraSuite, |
|
| 547 | - "enrollmentUrl" => $enrollmentUrl |
|
| 548 | - ), |
|
| 549 | - "identity"=> |
|
| 550 | - array("identifier" =>$data["userId"], |
|
| 551 | - "displayName"=>$data["displayName"])); |
|
| 541 | + array("displayName" => $this->_name, |
|
| 542 | + "identifier" => $this->_identifier, |
|
| 543 | + "logoUrl" => $this->_logoUrl, |
|
| 544 | + "infoUrl" => $this->_infoUrl, |
|
| 545 | + "authenticationUrl" => $authenticationUrl, |
|
| 546 | + "ocraSuite" => $this->_ocraSuite, |
|
| 547 | + "enrollmentUrl" => $enrollmentUrl |
|
| 548 | + ), |
|
| 549 | + "identity"=> |
|
| 550 | + array("identifier" =>$data["userId"], |
|
| 551 | + "displayName"=>$data["displayName"])); |
|
| 552 | 552 | |
| 553 | 553 | $this->_stateStorage->unsetValue(self::PREFIX_ENROLLMENT . $enrollmentKey); |
| 554 | 554 | |
@@ -571,18 +571,18 @@ discard block |
||
| 571 | 571 | */ |
| 572 | 572 | public function getEnrollmentSecret($enrollmentKey) |
| 573 | 573 | { |
| 574 | - $data = $this->_stateStorage->getValue(self::PREFIX_ENROLLMENT . $enrollmentKey); |
|
| 575 | - $secret = $this->_uniqueSessionKey(self::PREFIX_ENROLLMENT_SECRET); |
|
| 576 | - $enrollmentData = [ |
|
| 577 | - "userId" => $data["userId"], |
|
| 578 | - "sessionId" => $data["sessionId"] |
|
| 579 | - ]; |
|
| 580 | - $this->_stateStorage->setValue( |
|
| 581 | - self::PREFIX_ENROLLMENT_SECRET . $secret, |
|
| 582 | - $enrollmentData, |
|
| 583 | - self::ENROLLMENT_EXPIRE |
|
| 584 | - ); |
|
| 585 | - return $secret; |
|
| 574 | + $data = $this->_stateStorage->getValue(self::PREFIX_ENROLLMENT . $enrollmentKey); |
|
| 575 | + $secret = $this->_uniqueSessionKey(self::PREFIX_ENROLLMENT_SECRET); |
|
| 576 | + $enrollmentData = [ |
|
| 577 | + "userId" => $data["userId"], |
|
| 578 | + "sessionId" => $data["sessionId"] |
|
| 579 | + ]; |
|
| 580 | + $this->_stateStorage->setValue( |
|
| 581 | + self::PREFIX_ENROLLMENT_SECRET . $secret, |
|
| 582 | + $enrollmentData, |
|
| 583 | + self::ENROLLMENT_EXPIRE |
|
| 584 | + ); |
|
| 585 | + return $secret; |
|
| 586 | 586 | } |
| 587 | 587 | |
| 588 | 588 | /** |
@@ -597,13 +597,13 @@ discard block |
||
| 597 | 597 | */ |
| 598 | 598 | public function validateEnrollmentSecret($enrollmentSecret) |
| 599 | 599 | { |
| 600 | - $data = $this->_stateStorage->getValue(self::PREFIX_ENROLLMENT_SECRET.$enrollmentSecret); |
|
| 601 | - if (is_array($data)) { |
|
| 602 | - // Secret is valid, application may accept the user secret. |
|
| 603 | - $this->_setEnrollmentStatus($data["sessionId"], self::ENROLLMENT_STATUS_PROCESSED); |
|
| 604 | - return $data["userId"]; |
|
| 605 | - } |
|
| 606 | - return false; |
|
| 600 | + $data = $this->_stateStorage->getValue(self::PREFIX_ENROLLMENT_SECRET.$enrollmentSecret); |
|
| 601 | + if (is_array($data)) { |
|
| 602 | + // Secret is valid, application may accept the user secret. |
|
| 603 | + $this->_setEnrollmentStatus($data["sessionId"], self::ENROLLMENT_STATUS_PROCESSED); |
|
| 604 | + return $data["userId"]; |
|
| 605 | + } |
|
| 606 | + return false; |
|
| 607 | 607 | } |
| 608 | 608 | |
| 609 | 609 | /** |
@@ -620,13 +620,13 @@ discard block |
||
| 620 | 620 | */ |
| 621 | 621 | public function finalizeEnrollment($enrollmentSecret) |
| 622 | 622 | { |
| 623 | - $data = $this->_stateStorage->getValue(self::PREFIX_ENROLLMENT_SECRET.$enrollmentSecret); |
|
| 624 | - if (is_array($data)) { |
|
| 625 | - // Enrollment is finalized, destroy our session data. |
|
| 626 | - $this->_setEnrollmentStatus($data["sessionId"], self::ENROLLMENT_STATUS_FINALIZED); |
|
| 627 | - $this->_stateStorage->unsetValue(self::PREFIX_ENROLLMENT_SECRET.$enrollmentSecret); |
|
| 628 | - } |
|
| 629 | - return true; |
|
| 623 | + $data = $this->_stateStorage->getValue(self::PREFIX_ENROLLMENT_SECRET.$enrollmentSecret); |
|
| 624 | + if (is_array($data)) { |
|
| 625 | + // Enrollment is finalized, destroy our session data. |
|
| 626 | + $this->_setEnrollmentStatus($data["sessionId"], self::ENROLLMENT_STATUS_FINALIZED); |
|
| 627 | + $this->_stateStorage->unsetValue(self::PREFIX_ENROLLMENT_SECRET.$enrollmentSecret); |
|
| 628 | + } |
|
| 629 | + return true; |
|
| 630 | 630 | } |
| 631 | 631 | |
| 632 | 632 | /** |
@@ -662,7 +662,7 @@ discard block |
||
| 662 | 662 | |
| 663 | 663 | $challengeUserId = NULL; |
| 664 | 664 | if (isset($state["userId"])) { |
| 665 | - $challengeUserId = $state["userId"]; |
|
| 665 | + $challengeUserId = $state["userId"]; |
|
| 666 | 666 | } |
| 667 | 667 | // Check if we're dealing with a second factor |
| 668 | 668 | if ($challengeUserId!=NULL && ($userId != $challengeUserId)) { |
@@ -801,6 +801,6 @@ discard block |
||
| 801 | 801 | */ |
| 802 | 802 | protected function _setEnrollmentStatus($sessionId, $status) |
| 803 | 803 | { |
| 804 | - $this->_stateStorage->setValue("enrollstatus".$sessionId, $status, self::ENROLLMENT_EXPIRE); |
|
| 804 | + $this->_stateStorage->setValue("enrollstatus".$sessionId, $status, self::ENROLLMENT_EXPIRE); |
|
| 805 | 805 | } |
| 806 | 806 | } |