@@ -90,9 +90,9 @@ |
||
| 90 | 90 | */ |
| 91 | 91 | public function verifyResponse($response, $secret, $challenge, $sessionKey) |
| 92 | 92 | { |
| 93 | - $expected = $this->calculateResponse($secret, $challenge, $sessionKey); |
|
| 93 | + $expected = $this->calculateResponse($secret, $challenge, $sessionKey); |
|
| 94 | 94 | |
| 95 | - return ($expected == $response); |
|
| 95 | + return ($expected == $response); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
@@ -107,7 +107,9 @@ |
||
| 107 | 107 | { |
| 108 | 108 | // find the :QN10, -QN10, QH10 etc. bit |
| 109 | 109 | $pos = stripos($ocraSuite, ":q"); |
| 110 | - if ($pos===false) $pos = stripos($ocraSuite, "-q"); |
|
| 110 | + if ($pos===false) { |
|
| 111 | + $pos = stripos($ocraSuite, "-q"); |
|
| 112 | + } |
|
| 111 | 113 | if ($pos===false) { |
| 112 | 114 | // No challenge config specified. Since we only support challenge based OCRA, we fallback to default 10 digit hexadecimal. |
| 113 | 115 | return array("format"=>"H", "length"=>10); |
@@ -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 = ""; |
@@ -101,12 +101,15 @@ discard block |
||
| 101 | 101 | $sessionInformationLength = 0; |
| 102 | 102 | $timeStampLength = 0; |
| 103 | 103 | |
| 104 | - if(stripos($ocraSuite, "sha1")!==false) |
|
| 105 | - $crypto = "sha1"; |
|
| 106 | - if(stripos($ocraSuite, "sha256")!==false) |
|
| 107 | - $crypto = "sha256"; |
|
| 108 | - if(stripos($ocraSuite, "sha512")!==false) |
|
| 109 | - $crypto = "sha512"; |
|
| 104 | + if(stripos($ocraSuite, "sha1")!==false) { |
|
| 105 | + $crypto = "sha1"; |
|
| 106 | + } |
|
| 107 | + if(stripos($ocraSuite, "sha256")!==false) { |
|
| 108 | + $crypto = "sha256"; |
|
| 109 | + } |
|
| 110 | + if(stripos($ocraSuite, "sha512")!==false) { |
|
| 111 | + $crypto = "sha512"; |
|
| 112 | + } |
|
| 110 | 113 | |
| 111 | 114 | // How many digits should we return |
| 112 | 115 | $oS = substr($ocraSuite, strpos($ocraSuite, ":")+1, strpos($ocraSuite, ":", strpos($ocraSuite, ":")+1) -strpos($ocraSuite, ":")-1); |
@@ -116,31 +119,35 @@ discard block |
||
| 116 | 119 | // Counter |
| 117 | 120 | if(stripos($ocraSuite, ":c") !==false) { |
| 118 | 121 | // Fix the length of the HEX string |
| 119 | - while(strlen($counter) < 16) |
|
| 120 | - $counter = "0" . $counter; |
|
| 122 | + while(strlen($counter) < 16) { |
|
| 123 | + $counter = "0" . $counter; |
|
| 124 | + } |
|
| 121 | 125 | $counterLength=8; |
| 122 | 126 | } |
| 123 | 127 | // Question |
| 124 | 128 | if(stripos($ocraSuite, ":q")!==false || |
| 125 | 129 | stripos($ocraSuite, "-q")!==false) { |
| 126 | - while(strlen($question) < 256) |
|
| 127 | - $question = $question . "0"; |
|
| 130 | + while(strlen($question) < 256) { |
|
| 131 | + $question = $question . "0"; |
|
| 132 | + } |
|
| 128 | 133 | $questionLength=128; |
| 129 | 134 | } |
| 130 | 135 | |
| 131 | 136 | // Password |
| 132 | 137 | if(stripos($ocraSuite, ":p")!==false || |
| 133 | 138 | stripos($ocraSuite, "-p") !==false) { |
| 134 | - while(strlen($password) < 40) |
|
| 135 | - $password = "0" . $password; |
|
| 139 | + while(strlen($password) < 40) { |
|
| 140 | + $password = "0" . $password; |
|
| 141 | + } |
|
| 136 | 142 | $passwordLength=20; |
| 137 | 143 | } |
| 138 | 144 | |
| 139 | 145 | // sessionInformation |
| 140 | 146 | if(stripos($ocraSuite, ":s") !==false || |
| 141 | 147 | stripos($ocraSuite, "-s", strpos($ocraSuite, ":", strpos($ocraSuite, ":")+1)) !== false) { |
| 142 | - while(strlen($sessionInformation) < 128) |
|
| 143 | - $sessionInformation = "0" . $sessionInformation; |
|
| 148 | + while(strlen($sessionInformation) < 128) { |
|
| 149 | + $sessionInformation = "0" . $sessionInformation; |
|
| 150 | + } |
|
| 144 | 151 | |
| 145 | 152 | $sessionInformationLength=64; |
| 146 | 153 | } |
@@ -148,8 +155,9 @@ discard block |
||
| 148 | 155 | // TimeStamp |
| 149 | 156 | if(stripos($ocraSuite, ":t") !==false || |
| 150 | 157 | stripos($ocraSuite, "-t") !== false) { |
| 151 | - while(strlen($timeStamp) < 16) |
|
| 152 | - $timeStamp = "0" . $timeStamp; |
|
| 158 | + while(strlen($timeStamp) < 16) { |
|
| 159 | + $timeStamp = "0" . $timeStamp; |
|
| 160 | + } |
|
| 153 | 161 | $timeStampLength=8; |
| 154 | 162 | } |
| 155 | 163 | |
@@ -97,12 +97,15 @@ discard block |
||
| 97 | 97 | $cryptoFunction = $components[1]; |
| 98 | 98 | $dataInput = strtolower($components[2]); // lower here so we can do case insensitive comparisons |
| 99 | 99 | |
| 100 | - if(stripos($cryptoFunction, "sha1")!==false) |
|
| 101 | - $crypto = "sha1"; |
|
| 102 | - if(stripos($cryptoFunction, "sha256")!==false) |
|
| 103 | - $crypto = "sha256"; |
|
| 104 | - if(stripos($cryptoFunction, "sha512")!==false) |
|
| 105 | - $crypto = "sha512"; |
|
| 100 | + if(stripos($cryptoFunction, "sha1")!==false) { |
|
| 101 | + $crypto = "sha1"; |
|
| 102 | + } |
|
| 103 | + if(stripos($cryptoFunction, "sha256")!==false) { |
|
| 104 | + $crypto = "sha256"; |
|
| 105 | + } |
|
| 106 | + if(stripos($cryptoFunction, "sha512")!==false) { |
|
| 107 | + $crypto = "sha512"; |
|
| 108 | + } |
|
| 106 | 109 | |
| 107 | 110 | $codeDigits = substr($cryptoFunction, strrpos($cryptoFunction, "-")+1); |
| 108 | 111 | |
@@ -110,63 +113,73 @@ discard block |
||
| 110 | 113 | // Counter |
| 111 | 114 | if($dataInput[0] == "c" ) { |
| 112 | 115 | // Fix the length of the HEX string |
| 113 | - while(strlen($counter) < 16) |
|
| 114 | - $counter = "0" . $counter; |
|
| 116 | + while(strlen($counter) < 16) { |
|
| 117 | + $counter = "0" . $counter; |
|
| 118 | + } |
|
| 115 | 119 | $counterLength=8; |
| 116 | 120 | } |
| 117 | 121 | // Question |
| 118 | 122 | if($dataInput[0] == "q" || |
| 119 | 123 | stripos($dataInput, "-q")!==false) { |
| 120 | - while(strlen($question) < 256) |
|
| 121 | - $question = $question . "0"; |
|
| 124 | + while(strlen($question) < 256) { |
|
| 125 | + $question = $question . "0"; |
|
| 126 | + } |
|
| 122 | 127 | $questionLength=128; |
| 123 | 128 | } |
| 124 | 129 | |
| 125 | 130 | // Password |
| 126 | 131 | if(stripos($dataInput, "psha1")!==false) { |
| 127 | - while(strlen($password) < 40) |
|
| 128 | - $password = "0" . $password; |
|
| 132 | + while(strlen($password) < 40) { |
|
| 133 | + $password = "0" . $password; |
|
| 134 | + } |
|
| 129 | 135 | $passwordLength=20; |
| 130 | 136 | } |
| 131 | 137 | |
| 132 | 138 | if(stripos($dataInput, "psha256")!==false) { |
| 133 | - while(strlen($password) < 64) |
|
| 134 | - $password = "0" . $password; |
|
| 139 | + while(strlen($password) < 64) { |
|
| 140 | + $password = "0" . $password; |
|
| 141 | + } |
|
| 135 | 142 | $passwordLength=32; |
| 136 | 143 | } |
| 137 | 144 | |
| 138 | 145 | if(stripos($dataInput, "psha512")!==false) { |
| 139 | - while(strlen($password) < 128) |
|
| 140 | - $password = "0" . $password; |
|
| 146 | + while(strlen($password) < 128) { |
|
| 147 | + $password = "0" . $password; |
|
| 148 | + } |
|
| 141 | 149 | $passwordLength=64; |
| 142 | 150 | } |
| 143 | 151 | |
| 144 | 152 | // sessionInformation |
| 145 | 153 | if(stripos($dataInput, "s064") !==false) { |
| 146 | - while(strlen($sessionInformation) < 128) |
|
| 147 | - $sessionInformation = "0" . $sessionInformation; |
|
| 154 | + while(strlen($sessionInformation) < 128) { |
|
| 155 | + $sessionInformation = "0" . $sessionInformation; |
|
| 156 | + } |
|
| 148 | 157 | |
| 149 | 158 | $sessionInformationLength=64; |
| 150 | 159 | } else if(stripos($dataInput, "s128") !==false) { |
| 151 | - while(strlen($sessionInformation) < 256) |
|
| 152 | - $sessionInformation = "0" . $sessionInformation; |
|
| 160 | + while(strlen($sessionInformation) < 256) { |
|
| 161 | + $sessionInformation = "0" . $sessionInformation; |
|
| 162 | + } |
|
| 153 | 163 | |
| 154 | 164 | $sessionInformationLength=128; |
| 155 | 165 | } else if(stripos($dataInput, "s256") !==false) { |
| 156 | - while(strlen($sessionInformation) < 512) |
|
| 157 | - $sessionInformation = "0" . $sessionInformation; |
|
| 166 | + while(strlen($sessionInformation) < 512) { |
|
| 167 | + $sessionInformation = "0" . $sessionInformation; |
|
| 168 | + } |
|
| 158 | 169 | |
| 159 | 170 | $sessionInformationLength=256; |
| 160 | 171 | } else if(stripos($dataInput, "s512") !==false) { |
| 161 | - while(strlen($sessionInformation) < 128) |
|
| 162 | - $sessionInformation = "0" . $sessionInformation; |
|
| 172 | + while(strlen($sessionInformation) < 128) { |
|
| 173 | + $sessionInformation = "0" . $sessionInformation; |
|
| 174 | + } |
|
| 163 | 175 | |
| 164 | 176 | $sessionInformationLength=64; |
| 165 | 177 | } else if (stripos($dataInput, "s") !== false ) { |
| 166 | 178 | // deviation from spec. Officially 's' without a length indicator is not in the reference implementation. |
| 167 | 179 | // RFC is ambigious. However we have supported this in Tiqr since day 1, so we continue to support it. |
| 168 | - while(strlen($sessionInformation) < 128) |
|
| 169 | - $sessionInformation = "0" . $sessionInformation; |
|
| 180 | + while(strlen($sessionInformation) < 128) { |
|
| 181 | + $sessionInformation = "0" . $sessionInformation; |
|
| 182 | + } |
|
| 170 | 183 | |
| 171 | 184 | $sessionInformationLength=64; |
| 172 | 185 | } |
@@ -176,8 +189,9 @@ discard block |
||
| 176 | 189 | // TimeStamp |
| 177 | 190 | if($dataInput[0] == "t" || |
| 178 | 191 | stripos($dataInput, "-t") !== false) { |
| 179 | - while(strlen($timeStamp) < 16) |
|
| 180 | - $timeStamp = "0" . $timeStamp; |
|
| 192 | + while(strlen($timeStamp) < 16) { |
|
| 193 | + $timeStamp = "0" . $timeStamp; |
|
| 194 | + } |
|
| 181 | 195 | $timeStampLength=8; |
| 182 | 196 | } |
| 183 | 197 | |
@@ -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 = ""; |
@@ -453,7 +453,9 @@ |
||
| 453 | 453 | $sessionId = session_id(); |
| 454 | 454 | } |
| 455 | 455 | $status = $this->_stateStorage->getValue("enrollstatus".$sessionId); |
| 456 | - if (is_null($status)) return self::ENROLLMENT_STATUS_IDLE; |
|
| 456 | + if (is_null($status)) { |
|
| 457 | + return self::ENROLLMENT_STATUS_IDLE; |
|
| 458 | + } |
|
| 457 | 459 | return $status; |
| 458 | 460 | } |
| 459 | 461 | |
@@ -552,17 +552,17 @@ discard block |
||
| 552 | 552 | } |
| 553 | 553 | |
| 554 | 554 | $metadata = array("service"=> |
| 555 | - array("displayName" => $this->_name, |
|
| 556 | - "identifier" => $this->_identifier, |
|
| 557 | - "logoUrl" => $this->_logoUrl, |
|
| 558 | - "infoUrl" => $this->_infoUrl, |
|
| 559 | - "authenticationUrl" => $authenticationUrl, |
|
| 560 | - "ocraSuite" => $this->_ocraSuite, |
|
| 561 | - "enrollmentUrl" => $enrollmentUrl |
|
| 562 | - ), |
|
| 563 | - "identity"=> |
|
| 564 | - array("identifier" =>$data["userId"], |
|
| 565 | - "displayName"=>$data["displayName"])); |
|
| 555 | + array("displayName" => $this->_name, |
|
| 556 | + "identifier" => $this->_identifier, |
|
| 557 | + "logoUrl" => $this->_logoUrl, |
|
| 558 | + "infoUrl" => $this->_infoUrl, |
|
| 559 | + "authenticationUrl" => $authenticationUrl, |
|
| 560 | + "ocraSuite" => $this->_ocraSuite, |
|
| 561 | + "enrollmentUrl" => $enrollmentUrl |
|
| 562 | + ), |
|
| 563 | + "identity"=> |
|
| 564 | + array("identifier" =>$data["userId"], |
|
| 565 | + "displayName"=>$data["displayName"])); |
|
| 566 | 566 | |
| 567 | 567 | $this->_stateStorage->unsetValue(self::PREFIX_ENROLLMENT . $enrollmentKey); |
| 568 | 568 | |
@@ -585,18 +585,18 @@ discard block |
||
| 585 | 585 | */ |
| 586 | 586 | public function getEnrollmentSecret($enrollmentKey) |
| 587 | 587 | { |
| 588 | - $data = $this->_stateStorage->getValue(self::PREFIX_ENROLLMENT . $enrollmentKey); |
|
| 589 | - $secret = $this->_uniqueSessionKey(self::PREFIX_ENROLLMENT_SECRET); |
|
| 590 | - $enrollmentData = [ |
|
| 591 | - "userId" => $data["userId"], |
|
| 592 | - "sessionId" => $data["sessionId"] |
|
| 593 | - ]; |
|
| 594 | - $this->_stateStorage->setValue( |
|
| 595 | - self::PREFIX_ENROLLMENT_SECRET . $secret, |
|
| 596 | - $enrollmentData, |
|
| 597 | - self::ENROLLMENT_EXPIRE |
|
| 598 | - ); |
|
| 599 | - return $secret; |
|
| 588 | + $data = $this->_stateStorage->getValue(self::PREFIX_ENROLLMENT . $enrollmentKey); |
|
| 589 | + $secret = $this->_uniqueSessionKey(self::PREFIX_ENROLLMENT_SECRET); |
|
| 590 | + $enrollmentData = [ |
|
| 591 | + "userId" => $data["userId"], |
|
| 592 | + "sessionId" => $data["sessionId"] |
|
| 593 | + ]; |
|
| 594 | + $this->_stateStorage->setValue( |
|
| 595 | + self::PREFIX_ENROLLMENT_SECRET . $secret, |
|
| 596 | + $enrollmentData, |
|
| 597 | + self::ENROLLMENT_EXPIRE |
|
| 598 | + ); |
|
| 599 | + return $secret; |
|
| 600 | 600 | } |
| 601 | 601 | |
| 602 | 602 | /** |
@@ -635,18 +635,18 @@ discard block |
||
| 635 | 635 | */ |
| 636 | 636 | public function finalizeEnrollment($enrollmentSecret) |
| 637 | 637 | { |
| 638 | - $data = $this->_stateStorage->getValue(self::PREFIX_ENROLLMENT_SECRET.$enrollmentSecret); |
|
| 639 | - if (is_array($data)) { |
|
| 640 | - // Enrollment is finalized, destroy our session data. |
|
| 641 | - $this->_setEnrollmentStatus($data["sessionId"], self::ENROLLMENT_STATUS_FINALIZED); |
|
| 642 | - $this->_stateStorage->unsetValue(self::PREFIX_ENROLLMENT_SECRET.$enrollmentSecret); |
|
| 643 | - } else { |
|
| 644 | - $this->logger->error( |
|
| 645 | - 'Enrollment status is not finalized, enrollmentsecret was not found in state storage. ' . |
|
| 646 | - 'Warning! the method will still return "true" as a result.' |
|
| 647 | - ); |
|
| 648 | - } |
|
| 649 | - return true; |
|
| 638 | + $data = $this->_stateStorage->getValue(self::PREFIX_ENROLLMENT_SECRET.$enrollmentSecret); |
|
| 639 | + if (is_array($data)) { |
|
| 640 | + // Enrollment is finalized, destroy our session data. |
|
| 641 | + $this->_setEnrollmentStatus($data["sessionId"], self::ENROLLMENT_STATUS_FINALIZED); |
|
| 642 | + $this->_stateStorage->unsetValue(self::PREFIX_ENROLLMENT_SECRET.$enrollmentSecret); |
|
| 643 | + } else { |
|
| 644 | + $this->logger->error( |
|
| 645 | + 'Enrollment status is not finalized, enrollmentsecret was not found in state storage. ' . |
|
| 646 | + 'Warning! the method will still return "true" as a result.' |
|
| 647 | + ); |
|
| 648 | + } |
|
| 649 | + return true; |
|
| 650 | 650 | } |
| 651 | 651 | |
| 652 | 652 | /** |
@@ -683,7 +683,7 @@ discard block |
||
| 683 | 683 | |
| 684 | 684 | $challengeUserId = NULL; |
| 685 | 685 | if (isset($state["userId"])) { |
| 686 | - $challengeUserId = $state["userId"]; |
|
| 686 | + $challengeUserId = $state["userId"]; |
|
| 687 | 687 | } |
| 688 | 688 | // Check if we're dealing with a second factor |
| 689 | 689 | if ($challengeUserId!=NULL && ($userId != $challengeUserId)) { |
@@ -830,6 +830,6 @@ discard block |
||
| 830 | 830 | */ |
| 831 | 831 | protected function _setEnrollmentStatus($sessionId, $status) |
| 832 | 832 | { |
| 833 | - $this->_stateStorage->setValue("enrollstatus".$sessionId, $status, self::ENROLLMENT_EXPIRE); |
|
| 833 | + $this->_stateStorage->setValue("enrollstatus".$sessionId, $status, self::ENROLLMENT_EXPIRE); |
|
| 834 | 834 | } |
| 835 | 835 | } |
@@ -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) { |
@@ -1,100 +1,100 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | class Tiqr_AutoLoader { |
| 3 | 3 | |
| 4 | - protected static $instance; |
|
| 5 | - |
|
| 6 | - protected $tiqrPath; |
|
| 7 | - protected $qrcodePath; |
|
| 8 | - protected $zendPath; |
|
| 9 | - |
|
| 10 | - protected function __construct($options) { |
|
| 11 | - if ($options !== NULL) { |
|
| 12 | - $this->setOptions($options); |
|
| 13 | - } |
|
| 14 | - spl_autoload_register(array(__CLASS__, 'autoload')); |
|
| 15 | - } |
|
| 16 | - |
|
| 17 | - public static function getInstance($options = NULL) { |
|
| 18 | - if (null === self::$instance) { |
|
| 19 | - self::$instance = new self($options); |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - return self::$instance; |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - public static function autoload($className) { |
|
| 26 | - if($className === NULL) { |
|
| 27 | - return; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - $self = self::getInstance(); |
|
| 31 | - |
|
| 32 | - $substr5 = substr($className, 0, 5); |
|
| 33 | - |
|
| 34 | - if ($substr5 === 'Tiqr_' || $substr5 === 'OATH_') { |
|
| 35 | - $file = $self->tiqrPath . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; |
|
| 36 | - } elseif ($className === 'QRcode') { |
|
| 37 | - $file = $self->qrcodePath . DIRECTORY_SEPARATOR . 'qrlib.php'; |
|
| 38 | - } elseif ($substr5 === 'Zend_') { |
|
| 39 | - $file = $self->zendPath . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; |
|
| 40 | - } else { |
|
| 41 | - return; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - if (file_exists($file)) { |
|
| 45 | - require_once($file); |
|
| 46 | - } |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - public function setOptions($options) { |
|
| 50 | - if (isset($options["tiqr.path"])) { |
|
| 51 | - $tiqr_dir = $options["tiqr.path"]; |
|
| 52 | - $tiqr_path = realpath($tiqr_dir); |
|
| 53 | - } else { |
|
| 54 | - $tiqr_dir = dirname(__FILE__); |
|
| 55 | - $tiqr_path = $tiqr_dir; |
|
| 56 | - } |
|
| 57 | - if (is_dir($tiqr_path)) { |
|
| 58 | - $this->tiqrPath = $tiqr_path; |
|
| 59 | - } else { |
|
| 60 | - throw new Exception('Directory not found: ' . var_export($tiqr_dir, TRUE)); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - if (isset($options["phpqrcode.path"])) { |
|
| 64 | - $qrcode_dir = $options["phpqrcode.path"]; |
|
| 65 | - $qrcode_path = realpath($qrcode_dir); |
|
| 66 | - } else { |
|
| 67 | - $qrcode_dir = dirname(dirname(dirname(__FILE__))) . '/phpqrcode'; |
|
| 68 | - $qrcode_path = $qrcode_dir; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - if (is_dir($qrcode_path)) { |
|
| 72 | - $this->qrcodePath = $qrcode_path; |
|
| 73 | - } else { |
|
| 74 | - throw new Exception('Directory not found: ' . var_export($qrcode_dir, TRUE)); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - if (isset($options["zend.path"])) { |
|
| 78 | - $zend_dir = $options["zend.path"]; |
|
| 79 | - $zend_path = realpath($zend_dir); |
|
| 80 | - } else { |
|
| 81 | - $zend_dir = dirname(dirname(dirname(__FILE__))) . "/zend"; |
|
| 82 | - $zend_path = $zend_dir; |
|
| 83 | - } |
|
| 84 | - if (is_dir($zend_path)) { |
|
| 85 | - $this->zendPath = $zend_path; |
|
| 86 | - } else { |
|
| 87 | - throw new Exception('Directory not found: ' . var_export($zend_dir, TRUE)); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - public function setIncludePath() { |
|
| 93 | - set_include_path(implode(PATH_SEPARATOR, array( |
|
| 94 | - $this->tiqrPath, |
|
| 95 | - $this->zendPath, |
|
| 96 | - $this->qrcodePath, |
|
| 97 | - get_include_path(), |
|
| 98 | - ))); |
|
| 99 | - } |
|
| 4 | + protected static $instance; |
|
| 5 | + |
|
| 6 | + protected $tiqrPath; |
|
| 7 | + protected $qrcodePath; |
|
| 8 | + protected $zendPath; |
|
| 9 | + |
|
| 10 | + protected function __construct($options) { |
|
| 11 | + if ($options !== NULL) { |
|
| 12 | + $this->setOptions($options); |
|
| 13 | + } |
|
| 14 | + spl_autoload_register(array(__CLASS__, 'autoload')); |
|
| 15 | + } |
|
| 16 | + |
|
| 17 | + public static function getInstance($options = NULL) { |
|
| 18 | + if (null === self::$instance) { |
|
| 19 | + self::$instance = new self($options); |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + return self::$instance; |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + public static function autoload($className) { |
|
| 26 | + if($className === NULL) { |
|
| 27 | + return; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + $self = self::getInstance(); |
|
| 31 | + |
|
| 32 | + $substr5 = substr($className, 0, 5); |
|
| 33 | + |
|
| 34 | + if ($substr5 === 'Tiqr_' || $substr5 === 'OATH_') { |
|
| 35 | + $file = $self->tiqrPath . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; |
|
| 36 | + } elseif ($className === 'QRcode') { |
|
| 37 | + $file = $self->qrcodePath . DIRECTORY_SEPARATOR . 'qrlib.php'; |
|
| 38 | + } elseif ($substr5 === 'Zend_') { |
|
| 39 | + $file = $self->zendPath . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; |
|
| 40 | + } else { |
|
| 41 | + return; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + if (file_exists($file)) { |
|
| 45 | + require_once($file); |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + public function setOptions($options) { |
|
| 50 | + if (isset($options["tiqr.path"])) { |
|
| 51 | + $tiqr_dir = $options["tiqr.path"]; |
|
| 52 | + $tiqr_path = realpath($tiqr_dir); |
|
| 53 | + } else { |
|
| 54 | + $tiqr_dir = dirname(__FILE__); |
|
| 55 | + $tiqr_path = $tiqr_dir; |
|
| 56 | + } |
|
| 57 | + if (is_dir($tiqr_path)) { |
|
| 58 | + $this->tiqrPath = $tiqr_path; |
|
| 59 | + } else { |
|
| 60 | + throw new Exception('Directory not found: ' . var_export($tiqr_dir, TRUE)); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + if (isset($options["phpqrcode.path"])) { |
|
| 64 | + $qrcode_dir = $options["phpqrcode.path"]; |
|
| 65 | + $qrcode_path = realpath($qrcode_dir); |
|
| 66 | + } else { |
|
| 67 | + $qrcode_dir = dirname(dirname(dirname(__FILE__))) . '/phpqrcode'; |
|
| 68 | + $qrcode_path = $qrcode_dir; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + if (is_dir($qrcode_path)) { |
|
| 72 | + $this->qrcodePath = $qrcode_path; |
|
| 73 | + } else { |
|
| 74 | + throw new Exception('Directory not found: ' . var_export($qrcode_dir, TRUE)); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + if (isset($options["zend.path"])) { |
|
| 78 | + $zend_dir = $options["zend.path"]; |
|
| 79 | + $zend_path = realpath($zend_dir); |
|
| 80 | + } else { |
|
| 81 | + $zend_dir = dirname(dirname(dirname(__FILE__))) . "/zend"; |
|
| 82 | + $zend_path = $zend_dir; |
|
| 83 | + } |
|
| 84 | + if (is_dir($zend_path)) { |
|
| 85 | + $this->zendPath = $zend_path; |
|
| 86 | + } else { |
|
| 87 | + throw new Exception('Directory not found: ' . var_export($zend_dir, TRUE)); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + public function setIncludePath() { |
|
| 93 | + set_include_path(implode(PATH_SEPARATOR, array( |
|
| 94 | + $this->tiqrPath, |
|
| 95 | + $this->zendPath, |
|
| 96 | + $this->qrcodePath, |
|
| 97 | + get_include_path(), |
|
| 98 | + ))); |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -4,241 +4,241 @@ |
||
| 4 | 4 | |
| 5 | 5 | class OATH_OCRAParser { |
| 6 | 6 | |
| 7 | - private $key = NULL; |
|
| 8 | - |
|
| 9 | - private $OCRASuite = NULL; |
|
| 10 | - |
|
| 11 | - private $OCRAVersion = NULL; |
|
| 12 | - |
|
| 13 | - private $CryptoFunctionType = NULL; |
|
| 14 | - private $CryptoFunctionHash = NULL; |
|
| 15 | - private $CryptoFunctionHashLength = NULL; |
|
| 16 | - private $CryptoFunctionTruncation = NULL; |
|
| 17 | - |
|
| 18 | - private $C = FALSE; |
|
| 19 | - private $Q = FALSE; |
|
| 20 | - private $QType = 'N'; |
|
| 21 | - private $QLength = 8; |
|
| 22 | - |
|
| 23 | - private $P = FALSE; |
|
| 24 | - private $PType = 'SHA1'; |
|
| 25 | - private $PLength = 20; |
|
| 26 | - |
|
| 27 | - private $S = FALSE; |
|
| 28 | - private $SLength = 64; |
|
| 29 | - |
|
| 30 | - private $T = FALSE; |
|
| 31 | - private $TLength = 60; // 1M |
|
| 32 | - private $TPeriods = array('H' => 3600, 'M' => 60, 'S' => 1); |
|
| 33 | - |
|
| 34 | - private $supportedHashFunctions = array('SHA1' => 20, 'SHA256' => 32, 'SHA512' => 64); |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - public function __construct($ocraSuite) { |
|
| 38 | - $this->parseOCRASuite($ocraSuite); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Inspired by https://github.com/bdauvergne/python-oath |
|
| 43 | - */ |
|
| 44 | - private function parseOCRASuite($ocraSuite) { |
|
| 45 | - if (!is_string($ocraSuite)) { |
|
| 46 | - throw new Exception('OCRASuite not in string format: ' . var_export($ocraSuite, TRUE)); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - $ocraSuite = strtoupper($ocraSuite); |
|
| 50 | - $this->OCRASuite = $ocraSuite; |
|
| 51 | - |
|
| 52 | - $s = explode(':', $ocraSuite); |
|
| 53 | - if (count($s) != 3) { |
|
| 54 | - throw new Exception('Invalid OCRASuite format: ' . var_export($ocraSuite, TRUE)); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $algo = explode('-', $s[0]); |
|
| 58 | - if (count($algo) != 2) { |
|
| 59 | - throw new Exception('Invalid OCRA version: ' . var_export($s[0], TRUE)); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - if ($algo[0] !== 'OCRA') { |
|
| 63 | - throw new Exception('Unsupported OCRA algorithm: ' . var_export($algo[0], TRUE)); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - if ($algo[1] !== '1') { |
|
| 67 | - throw new Exception('Unsupported OCRA version: ' . var_export($algo[1], TRUE)); |
|
| 68 | - } |
|
| 69 | - $this->OCRAVersion = $algo[1]; |
|
| 70 | - |
|
| 71 | - $cf = explode('-', $s[1]); |
|
| 72 | - if (count($cf) != 3) { |
|
| 73 | - throw new Exception('Invalid OCRA suite crypto function: ' . var_export($s[1], TRUE)); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - if ($cf[0] !== 'HOTP') { |
|
| 77 | - throw new Exception('Unsupported OCRA suite crypto function: ' . var_export($cf[0], TRUE)); |
|
| 78 | - } |
|
| 79 | - $this->CryptoFunctionType = $cf[0]; |
|
| 80 | - |
|
| 81 | - if (!array_key_exists($cf[1], $this->supportedHashFunctions)) { |
|
| 82 | - throw new Exception('Unsupported hash function in OCRA suite crypto function: ' . var_export($cf[1], TRUE)); |
|
| 83 | - } |
|
| 84 | - $this->CryptoFunctionHash = $cf[1]; |
|
| 85 | - $this->CryptoFunctionHashLength = $this->supportedHashFunctions[$cf[1]]; |
|
| 86 | - |
|
| 87 | - if (!preg_match('/^\d+$/', $cf[2]) || (($cf[2] < 4 || $cf[2] > 10) && $cf[2] != 0)) { |
|
| 88 | - throw new Exception('Invalid OCRA suite crypto function truncation length: ' . var_export($cf[2], TRUE)); |
|
| 89 | - } |
|
| 90 | - $this->CryptoFunctionTruncation = intval($cf[2]); |
|
| 91 | - |
|
| 92 | - $di = explode('-', $s[2]); |
|
| 93 | - if (count($cf) == 0) { |
|
| 94 | - throw new Exception('Invalid OCRA suite data input: ' . var_export($s[2], TRUE)); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $data_input = array(); |
|
| 98 | - foreach($di as $elem) { |
|
| 99 | - $letter = $elem[0]; |
|
| 100 | - if (array_key_exists($letter, $data_input)) { |
|
| 101 | - throw new Exception('Duplicate field in OCRA suite data input: ' . var_export($elem, TRUE)); |
|
| 102 | - } |
|
| 103 | - $data_input[$letter] = 1; |
|
| 104 | - |
|
| 105 | - if ($letter === 'C' && strlen($elem) == 1) { |
|
| 106 | - $this->C = TRUE; |
|
| 107 | - } elseif ($letter === 'Q') { |
|
| 108 | - if (strlen($elem) == 1) { |
|
| 109 | - $this->Q = TRUE; |
|
| 110 | - } elseif (preg_match('/^Q([AHN])(\d+)$/', $elem, $match)) { |
|
| 111 | - $q_len = intval($match[2]); |
|
| 112 | - if ($q_len < 4 || $q_len > 64) { |
|
| 113 | - throw new Exception('Invalid OCRA suite data input question length: ' . var_export($q_len, TRUE)); |
|
| 114 | - } |
|
| 115 | - $this->Q = TRUE; |
|
| 116 | - $this->QType = $match[1]; |
|
| 117 | - $this->QLength = $q_len; |
|
| 118 | - } else { |
|
| 119 | - throw new Exception('Invalid OCRA suite data input question: ' . var_export($elem, TRUE)); |
|
| 120 | - } |
|
| 121 | - } elseif ($letter === 'P') { |
|
| 122 | - if (strlen($elem) == 1) { |
|
| 123 | - $this->P = TRUE; |
|
| 124 | - } else { |
|
| 125 | - $p_algo = substr($elem, 1); |
|
| 126 | - if (!array_key_exists($p_algo, $this->supportedHashFunctions)) { |
|
| 127 | - throw new Exception('Unsupported OCRA suite PIN hash function: ' . var_export($elem, TRUE)); |
|
| 128 | - } |
|
| 129 | - $this->P = TRUE; |
|
| 130 | - $this->PType = $p_algo; |
|
| 131 | - $this->PLength = $this->supportedHashFunctions[$p_algo]; |
|
| 132 | - } |
|
| 133 | - } elseif ($letter === 'S') { |
|
| 134 | - if (strlen($elem) == 1) { |
|
| 135 | - $this->S = TRUE; |
|
| 136 | - } elseif (preg_match('/^S(\d+)$/', $elem, $match)) { |
|
| 137 | - $s_len = intval($match[1]); |
|
| 138 | - if ($s_len <= 0 || $s_len > 512) { |
|
| 139 | - throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($s_len, TRUE)); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $this->S = TRUE; |
|
| 143 | - $this->SLength = $s_len; |
|
| 144 | - } else { |
|
| 145 | - throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($elem, TRUE)); |
|
| 146 | - } |
|
| 147 | - } elseif ($letter === 'T') { |
|
| 148 | - if (strlen($elem) == 1) { |
|
| 149 | - $this->T = TRUE; |
|
| 150 | - } elseif (preg_match('/^T(\d+[HMS])+$/', $elem)) { |
|
| 151 | - preg_match_all('/(\d+)([HMS])/', $elem, $match); |
|
| 152 | - |
|
| 153 | - if (count($match[1]) !== count(array_unique($match[2]))) { |
|
| 154 | - throw new Exception('Duplicate definitions in OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $length = 0; |
|
| 158 | - for ($i = 0; $i < count($match[1]); $i++) { |
|
| 159 | - $length += intval($match[1][$i]) * $this->TPeriods[$match[2][$i]]; |
|
| 160 | - } |
|
| 161 | - if ($length <= 0) { |
|
| 162 | - throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $this->T = TRUE; |
|
| 166 | - $this->TLength = $length; |
|
| 167 | - } else { |
|
| 168 | - throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
| 169 | - } |
|
| 170 | - } else { |
|
| 171 | - throw new Exception('Unsupported OCRA suite data input field: ' . var_export($elem, TRUE)); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - if (!$this->Q) { |
|
| 176 | - throw new Exception('OCRA suite data input question not defined: ' . var_export($s[2], TRUE)); |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - public function generateChallenge() { |
|
| 181 | - $q_length = $this->QLength; |
|
| 182 | - $q_type = $this->QType; |
|
| 7 | + private $key = NULL; |
|
| 8 | + |
|
| 9 | + private $OCRASuite = NULL; |
|
| 10 | + |
|
| 11 | + private $OCRAVersion = NULL; |
|
| 12 | + |
|
| 13 | + private $CryptoFunctionType = NULL; |
|
| 14 | + private $CryptoFunctionHash = NULL; |
|
| 15 | + private $CryptoFunctionHashLength = NULL; |
|
| 16 | + private $CryptoFunctionTruncation = NULL; |
|
| 17 | + |
|
| 18 | + private $C = FALSE; |
|
| 19 | + private $Q = FALSE; |
|
| 20 | + private $QType = 'N'; |
|
| 21 | + private $QLength = 8; |
|
| 22 | + |
|
| 23 | + private $P = FALSE; |
|
| 24 | + private $PType = 'SHA1'; |
|
| 25 | + private $PLength = 20; |
|
| 26 | + |
|
| 27 | + private $S = FALSE; |
|
| 28 | + private $SLength = 64; |
|
| 29 | + |
|
| 30 | + private $T = FALSE; |
|
| 31 | + private $TLength = 60; // 1M |
|
| 32 | + private $TPeriods = array('H' => 3600, 'M' => 60, 'S' => 1); |
|
| 33 | + |
|
| 34 | + private $supportedHashFunctions = array('SHA1' => 20, 'SHA256' => 32, 'SHA512' => 64); |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + public function __construct($ocraSuite) { |
|
| 38 | + $this->parseOCRASuite($ocraSuite); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Inspired by https://github.com/bdauvergne/python-oath |
|
| 43 | + */ |
|
| 44 | + private function parseOCRASuite($ocraSuite) { |
|
| 45 | + if (!is_string($ocraSuite)) { |
|
| 46 | + throw new Exception('OCRASuite not in string format: ' . var_export($ocraSuite, TRUE)); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + $ocraSuite = strtoupper($ocraSuite); |
|
| 50 | + $this->OCRASuite = $ocraSuite; |
|
| 51 | + |
|
| 52 | + $s = explode(':', $ocraSuite); |
|
| 53 | + if (count($s) != 3) { |
|
| 54 | + throw new Exception('Invalid OCRASuite format: ' . var_export($ocraSuite, TRUE)); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $algo = explode('-', $s[0]); |
|
| 58 | + if (count($algo) != 2) { |
|
| 59 | + throw new Exception('Invalid OCRA version: ' . var_export($s[0], TRUE)); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + if ($algo[0] !== 'OCRA') { |
|
| 63 | + throw new Exception('Unsupported OCRA algorithm: ' . var_export($algo[0], TRUE)); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + if ($algo[1] !== '1') { |
|
| 67 | + throw new Exception('Unsupported OCRA version: ' . var_export($algo[1], TRUE)); |
|
| 68 | + } |
|
| 69 | + $this->OCRAVersion = $algo[1]; |
|
| 70 | + |
|
| 71 | + $cf = explode('-', $s[1]); |
|
| 72 | + if (count($cf) != 3) { |
|
| 73 | + throw new Exception('Invalid OCRA suite crypto function: ' . var_export($s[1], TRUE)); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + if ($cf[0] !== 'HOTP') { |
|
| 77 | + throw new Exception('Unsupported OCRA suite crypto function: ' . var_export($cf[0], TRUE)); |
|
| 78 | + } |
|
| 79 | + $this->CryptoFunctionType = $cf[0]; |
|
| 80 | + |
|
| 81 | + if (!array_key_exists($cf[1], $this->supportedHashFunctions)) { |
|
| 82 | + throw new Exception('Unsupported hash function in OCRA suite crypto function: ' . var_export($cf[1], TRUE)); |
|
| 83 | + } |
|
| 84 | + $this->CryptoFunctionHash = $cf[1]; |
|
| 85 | + $this->CryptoFunctionHashLength = $this->supportedHashFunctions[$cf[1]]; |
|
| 86 | + |
|
| 87 | + if (!preg_match('/^\d+$/', $cf[2]) || (($cf[2] < 4 || $cf[2] > 10) && $cf[2] != 0)) { |
|
| 88 | + throw new Exception('Invalid OCRA suite crypto function truncation length: ' . var_export($cf[2], TRUE)); |
|
| 89 | + } |
|
| 90 | + $this->CryptoFunctionTruncation = intval($cf[2]); |
|
| 91 | + |
|
| 92 | + $di = explode('-', $s[2]); |
|
| 93 | + if (count($cf) == 0) { |
|
| 94 | + throw new Exception('Invalid OCRA suite data input: ' . var_export($s[2], TRUE)); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $data_input = array(); |
|
| 98 | + foreach($di as $elem) { |
|
| 99 | + $letter = $elem[0]; |
|
| 100 | + if (array_key_exists($letter, $data_input)) { |
|
| 101 | + throw new Exception('Duplicate field in OCRA suite data input: ' . var_export($elem, TRUE)); |
|
| 102 | + } |
|
| 103 | + $data_input[$letter] = 1; |
|
| 104 | + |
|
| 105 | + if ($letter === 'C' && strlen($elem) == 1) { |
|
| 106 | + $this->C = TRUE; |
|
| 107 | + } elseif ($letter === 'Q') { |
|
| 108 | + if (strlen($elem) == 1) { |
|
| 109 | + $this->Q = TRUE; |
|
| 110 | + } elseif (preg_match('/^Q([AHN])(\d+)$/', $elem, $match)) { |
|
| 111 | + $q_len = intval($match[2]); |
|
| 112 | + if ($q_len < 4 || $q_len > 64) { |
|
| 113 | + throw new Exception('Invalid OCRA suite data input question length: ' . var_export($q_len, TRUE)); |
|
| 114 | + } |
|
| 115 | + $this->Q = TRUE; |
|
| 116 | + $this->QType = $match[1]; |
|
| 117 | + $this->QLength = $q_len; |
|
| 118 | + } else { |
|
| 119 | + throw new Exception('Invalid OCRA suite data input question: ' . var_export($elem, TRUE)); |
|
| 120 | + } |
|
| 121 | + } elseif ($letter === 'P') { |
|
| 122 | + if (strlen($elem) == 1) { |
|
| 123 | + $this->P = TRUE; |
|
| 124 | + } else { |
|
| 125 | + $p_algo = substr($elem, 1); |
|
| 126 | + if (!array_key_exists($p_algo, $this->supportedHashFunctions)) { |
|
| 127 | + throw new Exception('Unsupported OCRA suite PIN hash function: ' . var_export($elem, TRUE)); |
|
| 128 | + } |
|
| 129 | + $this->P = TRUE; |
|
| 130 | + $this->PType = $p_algo; |
|
| 131 | + $this->PLength = $this->supportedHashFunctions[$p_algo]; |
|
| 132 | + } |
|
| 133 | + } elseif ($letter === 'S') { |
|
| 134 | + if (strlen($elem) == 1) { |
|
| 135 | + $this->S = TRUE; |
|
| 136 | + } elseif (preg_match('/^S(\d+)$/', $elem, $match)) { |
|
| 137 | + $s_len = intval($match[1]); |
|
| 138 | + if ($s_len <= 0 || $s_len > 512) { |
|
| 139 | + throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($s_len, TRUE)); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $this->S = TRUE; |
|
| 143 | + $this->SLength = $s_len; |
|
| 144 | + } else { |
|
| 145 | + throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($elem, TRUE)); |
|
| 146 | + } |
|
| 147 | + } elseif ($letter === 'T') { |
|
| 148 | + if (strlen($elem) == 1) { |
|
| 149 | + $this->T = TRUE; |
|
| 150 | + } elseif (preg_match('/^T(\d+[HMS])+$/', $elem)) { |
|
| 151 | + preg_match_all('/(\d+)([HMS])/', $elem, $match); |
|
| 152 | + |
|
| 153 | + if (count($match[1]) !== count(array_unique($match[2]))) { |
|
| 154 | + throw new Exception('Duplicate definitions in OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $length = 0; |
|
| 158 | + for ($i = 0; $i < count($match[1]); $i++) { |
|
| 159 | + $length += intval($match[1][$i]) * $this->TPeriods[$match[2][$i]]; |
|
| 160 | + } |
|
| 161 | + if ($length <= 0) { |
|
| 162 | + throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $this->T = TRUE; |
|
| 166 | + $this->TLength = $length; |
|
| 167 | + } else { |
|
| 168 | + throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
| 169 | + } |
|
| 170 | + } else { |
|
| 171 | + throw new Exception('Unsupported OCRA suite data input field: ' . var_export($elem, TRUE)); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + if (!$this->Q) { |
|
| 176 | + throw new Exception('OCRA suite data input question not defined: ' . var_export($s[2], TRUE)); |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + public function generateChallenge() { |
|
| 181 | + $q_length = $this->QLength; |
|
| 182 | + $q_type = $this->QType; |
|
| 183 | 183 | |
| 184 | 184 | $bytes = Tiqr_Random::randomBytes($q_length); |
| 185 | 185 | |
| 186 | - switch($q_type) { |
|
| 187 | - case 'A': |
|
| 188 | - $challenge = base64_encode($bytes); |
|
| 189 | - $tr = implode("", unpack('H*', $bytes)); |
|
| 190 | - $challenge = rtrim(strtr($challenge, '+/', $tr), '='); |
|
| 191 | - break; |
|
| 192 | - case 'H': |
|
| 193 | - $challenge = implode("", unpack('H*', $bytes)); |
|
| 194 | - break; |
|
| 195 | - case 'N': |
|
| 196 | - $challenge = implode("", unpack('N*', $bytes)); |
|
| 197 | - break; |
|
| 198 | - default: |
|
| 199 | - throw new Exception('Unsupported OCRASuite challenge type: ' . var_export($q_type, TRUE)); |
|
| 200 | - break; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - $challenge = substr($challenge, 0, $q_length); |
|
| 204 | - |
|
| 205 | - return $challenge; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - |
|
| 209 | - public function generateSessionInformation() { |
|
| 210 | - if (!$this->S) { |
|
| 211 | - throw new Exception('Session information not defined in OCRASuite: ' . var_export($this->OCRASuite, TRUE)); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $s_length = $this->SLength; |
|
| 186 | + switch($q_type) { |
|
| 187 | + case 'A': |
|
| 188 | + $challenge = base64_encode($bytes); |
|
| 189 | + $tr = implode("", unpack('H*', $bytes)); |
|
| 190 | + $challenge = rtrim(strtr($challenge, '+/', $tr), '='); |
|
| 191 | + break; |
|
| 192 | + case 'H': |
|
| 193 | + $challenge = implode("", unpack('H*', $bytes)); |
|
| 194 | + break; |
|
| 195 | + case 'N': |
|
| 196 | + $challenge = implode("", unpack('N*', $bytes)); |
|
| 197 | + break; |
|
| 198 | + default: |
|
| 199 | + throw new Exception('Unsupported OCRASuite challenge type: ' . var_export($q_type, TRUE)); |
|
| 200 | + break; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + $challenge = substr($challenge, 0, $q_length); |
|
| 204 | + |
|
| 205 | + return $challenge; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + |
|
| 209 | + public function generateSessionInformation() { |
|
| 210 | + if (!$this->S) { |
|
| 211 | + throw new Exception('Session information not defined in OCRASuite: ' . var_export($this->OCRASuite, TRUE)); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $s_length = $this->SLength; |
|
| 215 | 215 | $bytes = Tiqr_Random::randomBytes($s_length); |
| 216 | 216 | |
| 217 | - // The OCRA spec doesn't specify that the session data should be hexadecimal. |
|
| 218 | - // However the reference implementation in the RFC does treat it as hex. |
|
| 219 | - $session = bin2hex($bytes); |
|
| 217 | + // The OCRA spec doesn't specify that the session data should be hexadecimal. |
|
| 218 | + // However the reference implementation in the RFC does treat it as hex. |
|
| 219 | + $session = bin2hex($bytes); |
|
| 220 | 220 | |
| 221 | - $session = substr($session, 0, $s_length); |
|
| 221 | + $session = substr($session, 0, $s_length); |
|
| 222 | 222 | |
| 223 | - return $session; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Constant time string comparison, see http://codahale.com/a-lesson-in-timing-attacks/ |
|
| 229 | - */ |
|
| 230 | - public static function constEqual($s1, $s2) { |
|
| 231 | - if (strlen($s1) != strlen($s2)) { |
|
| 232 | - return FALSE; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - $result = TRUE; |
|
| 236 | - $length = strlen($s1); |
|
| 237 | - for ($i = 0; $i < $length; $i++) { |
|
| 238 | - $result &= ($s1[$i] == $s2[$i]); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - return (boolean)$result; |
|
| 242 | - } |
|
| 223 | + return $session; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Constant time string comparison, see http://codahale.com/a-lesson-in-timing-attacks/ |
|
| 229 | + */ |
|
| 230 | + public static function constEqual($s1, $s2) { |
|
| 231 | + if (strlen($s1) != strlen($s2)) { |
|
| 232 | + return FALSE; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + $result = TRUE; |
|
| 236 | + $length = strlen($s1); |
|
| 237 | + for ($i = 0; $i < $length; $i++) { |
|
| 238 | + $result &= ($s1[$i] == $s2[$i]); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + return (boolean)$result; |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | 244 | } |