| @@ -46,7 +46,7 @@ discard block | ||
| 46 | 46 | $keyBytes, | 
| 47 | 47 | $text) | 
| 48 | 48 |      { | 
| 49 | - $hash = hash_hmac ($crypto, $text, $keyBytes); | |
| 49 | + $hash = hash_hmac($crypto, $text, $keyBytes); | |
| 50 | 50 | return $hash; | 
| 51 | 51 | } | 
| 52 | 52 | |
| @@ -57,7 +57,7 @@ discard block | ||
| 57 | 57 | * | 
| 58 | 58 | * @return String a string with raw bytes | 
| 59 | 59 | */ | 
| 60 | -    private static function _hexStr2Bytes($hex){ | |
| 60 | +    private static function _hexStr2Bytes($hex) { | |
| 61 | 61 |          return pack("H*", $hex); | 
| 62 | 62 | } | 
| 63 | 63 | |
| @@ -101,63 +101,63 @@ discard block | ||
| 101 | 101 | $sessionInformationLength = 0; | 
| 102 | 102 | $timeStampLength = 0; | 
| 103 | 103 | |
| 104 | - if(stripos($ocraSuite, "sha1")!==false) | |
| 104 | + if (stripos($ocraSuite, "sha1") !== false) | |
| 105 | 105 | $crypto = "sha1"; | 
| 106 | - if(stripos($ocraSuite, "sha256")!==false) | |
| 106 | + if (stripos($ocraSuite, "sha256") !== false) | |
| 107 | 107 | $crypto = "sha256"; | 
| 108 | - if(stripos($ocraSuite, "sha512")!==false) | |
| 108 | + if (stripos($ocraSuite, "sha512") !== false) | |
| 109 | 109 | $crypto = "sha512"; | 
| 110 | 110 | |
| 111 | 111 | // How many digits should we return | 
| 112 | - $oS = substr($ocraSuite, strpos($ocraSuite, ":")+1, strpos($ocraSuite, ":", strpos($ocraSuite, ":")+1) -strpos($ocraSuite, ":")-1); | |
| 113 | - $codeDigits = substr($oS, strrpos($oS, "-")+1); | |
| 112 | + $oS = substr($ocraSuite, strpos($ocraSuite, ":") + 1, strpos($ocraSuite, ":", strpos($ocraSuite, ":") + 1) - strpos($ocraSuite, ":") - 1); | |
| 113 | + $codeDigits = substr($oS, strrpos($oS, "-") + 1); | |
| 114 | 114 | |
| 115 | 115 | // The size of the byte array message to be encrypted | 
| 116 | 116 | // Counter | 
| 117 | -        if(stripos($ocraSuite, ":c") !==false) { | |
| 117 | +        if (stripos($ocraSuite, ":c") !== false) { | |
| 118 | 118 | // Fix the length of the HEX string | 
| 119 | - while(strlen($counter) < 16) | |
| 120 | - $counter = "0" . $counter; | |
| 121 | - $counterLength=8; | |
| 119 | + while (strlen($counter) < 16) | |
| 120 | + $counter = "0".$counter; | |
| 121 | + $counterLength = 8; | |
| 122 | 122 | } | 
| 123 | 123 | // Question | 
| 124 | - if(stripos($ocraSuite, ":q")!==false || | |
| 125 | -                stripos($ocraSuite, "-q")!==false) { | |
| 126 | - while(strlen($question) < 256) | |
| 127 | - $question = $question . "0"; | |
| 128 | - $questionLength=128; | |
| 124 | + if (stripos($ocraSuite, ":q") !== false || | |
| 125 | +                stripos($ocraSuite, "-q") !== false) { | |
| 126 | + while (strlen($question) < 256) | |
| 127 | + $question = $question."0"; | |
| 128 | + $questionLength = 128; | |
| 129 | 129 | } | 
| 130 | 130 | |
| 131 | 131 | // Password | 
| 132 | - if(stripos($ocraSuite, ":p")!==false || | |
| 133 | -                stripos($ocraSuite, "-p") !==false) { | |
| 134 | - while(strlen($password) < 40) | |
| 135 | - $password = "0" . $password; | |
| 136 | - $passwordLength=20; | |
| 132 | + if (stripos($ocraSuite, ":p") !== false || | |
| 133 | +                stripos($ocraSuite, "-p") !== false) { | |
| 134 | + while (strlen($password) < 40) | |
| 135 | + $password = "0".$password; | |
| 136 | + $passwordLength = 20; | |
| 137 | 137 | } | 
| 138 | 138 | |
| 139 | 139 | // sessionInformation | 
| 140 | - if(stripos($ocraSuite, ":s") !==false || | |
| 141 | -                stripos($ocraSuite, "-s", strpos($ocraSuite, ":", strpos($ocraSuite, ":")+1)) !== false) { | |
| 142 | - while(strlen($sessionInformation) < 128) | |
| 143 | - $sessionInformation = "0" . $sessionInformation; | |
| 140 | + if (stripos($ocraSuite, ":s") !== false || | |
| 141 | +                stripos($ocraSuite, "-s", strpos($ocraSuite, ":", strpos($ocraSuite, ":") + 1)) !== false) { | |
| 142 | + while (strlen($sessionInformation) < 128) | |
| 143 | + $sessionInformation = "0".$sessionInformation; | |
| 144 | 144 | |
| 145 | - $sessionInformationLength=64; | |
| 145 | + $sessionInformationLength = 64; | |
| 146 | 146 | } | 
| 147 | 147 | |
| 148 | 148 | // TimeStamp | 
| 149 | - if(stripos($ocraSuite, ":t") !==false || | |
| 149 | + if (stripos($ocraSuite, ":t") !== false || | |
| 150 | 150 |                  stripos($ocraSuite, "-t") !== false) { | 
| 151 | - while(strlen($timeStamp) < 16) | |
| 152 | - $timeStamp = "0" . $timeStamp; | |
| 153 | - $timeStampLength=8; | |
| 151 | + while (strlen($timeStamp) < 16) | |
| 152 | + $timeStamp = "0".$timeStamp; | |
| 153 | + $timeStampLength = 8; | |
| 154 | 154 | } | 
| 155 | 155 | |
| 156 | 156 | // Put the bytes of "ocraSuite" parameters into the message | 
| 157 | 157 | |
| 158 | - $msg = array_fill(0,$ocraSuiteLength+$counterLength+$questionLength+$passwordLength+$sessionInformationLength+$timeStampLength+1, 0); | |
| 158 | + $msg = array_fill(0, $ocraSuiteLength + $counterLength + $questionLength + $passwordLength + $sessionInformationLength + $timeStampLength + 1, 0); | |
| 159 | 159 | |
| 160 | -        for($i=0;$i<strlen($ocraSuite);$i++) { | |
| 160 | +        for ($i = 0; $i < strlen($ocraSuite); $i++) { | |
| 161 | 161 | $msg[$i] = $ocraSuite[$i]; | 
| 162 | 162 | } | 
| 163 | 163 | |
| @@ -166,9 +166,9 @@ discard block | ||
| 166 | 166 | |
| 167 | 167 | // Put the bytes of "Counter" to the message | 
| 168 | 168 | // Input is HEX encoded | 
| 169 | -        if($counterLength > 0 ) { | |
| 169 | +        if ($counterLength > 0) { | |
| 170 | 170 | $bArray = self::_hexStr2Bytes($counter); | 
| 171 | -            for ($i=0;$i<strlen($bArray);$i++) { | |
| 171 | +            for ($i = 0; $i < strlen($bArray); $i++) { | |
| 172 | 172 | $msg [$i + $ocraSuiteLength + 1] = $bArray[$i]; | 
| 173 | 173 | } | 
| 174 | 174 | } | 
| @@ -176,36 +176,36 @@ discard block | ||
| 176 | 176 | |
| 177 | 177 | // Put the bytes of "question" to the message | 
| 178 | 178 | // Input is text encoded | 
| 179 | -        if($questionLength > 0 ) { | |
| 179 | +        if ($questionLength > 0) { | |
| 180 | 180 | $bArray = self::_hexStr2Bytes($question); | 
| 181 | -            for ($i=0;$i<strlen($bArray);$i++) { | |
| 181 | +            for ($i = 0; $i < strlen($bArray); $i++) { | |
| 182 | 182 | $msg [$i + $ocraSuiteLength + 1 + $counterLength] = $bArray[$i]; | 
| 183 | 183 | } | 
| 184 | 184 | } | 
| 185 | 185 | |
| 186 | 186 | // Put the bytes of "password" to the message | 
| 187 | 187 | // Input is HEX encoded | 
| 188 | -        if($passwordLength > 0){ | |
| 188 | +        if ($passwordLength > 0) { | |
| 189 | 189 | $bArray = self::_hexStr2Bytes($password); | 
| 190 | -            for ($i=0;$i<strlen($bArray);$i++) { | |
| 190 | +            for ($i = 0; $i < strlen($bArray); $i++) { | |
| 191 | 191 | $msg [$i + $ocraSuiteLength + 1 + $counterLength + $questionLength] = $bArray[$i]; | 
| 192 | 192 | } | 
| 193 | 193 | } | 
| 194 | 194 | |
| 195 | 195 | // Put the bytes of "sessionInformation" to the message | 
| 196 | 196 | // Input is text encoded | 
| 197 | -        if($sessionInformationLength > 0 ){ | |
| 197 | +        if ($sessionInformationLength > 0) { | |
| 198 | 198 | $bArray = self::_hexStr2Bytes($sessionInformation); | 
| 199 | -            for ($i=0;$i<strlen($bArray);$i++) { | |
| 199 | +            for ($i = 0; $i < strlen($bArray); $i++) { | |
| 200 | 200 | $msg [$i + $ocraSuiteLength + 1 + $counterLength + $questionLength + $passwordLength] = $bArray[$i]; | 
| 201 | 201 | } | 
| 202 | 202 | } | 
| 203 | 203 | |
| 204 | 204 | // Put the bytes of "time" to the message | 
| 205 | 205 | // Input is text value of minutes | 
| 206 | -        if($timeStampLength > 0){ | |
| 206 | +        if ($timeStampLength > 0) { | |
| 207 | 207 | $bArray = self::_hexStr2Bytes($timestamp); | 
| 208 | -            for ($i=0;$i<strlen($bArray);$i++) { | |
| 208 | +            for ($i = 0; $i < strlen($bArray); $i++) { | |
| 209 | 209 | $msg [$i + $ocraSuiteLength + 1 + $counterLength + $questionLength + $passwordLength + $sessionInformationLength] = $bArray[$i]; | 
| 210 | 210 | } | 
| 211 | 211 | } | 
| @@ -227,9 +227,9 @@ discard block | ||
| 227 | 227 | static function _oath_truncate($hash, $length = 6) | 
| 228 | 228 |      { | 
| 229 | 229 | // Convert to dec | 
| 230 | - foreach(str_split($hash,2) as $hex) | |
| 230 | + foreach (str_split($hash, 2) as $hex) | |
| 231 | 231 |          { | 
| 232 | - $hmac_result[]=hexdec($hex); | |
| 232 | + $hmac_result[] = hexdec($hex); | |
| 233 | 233 | } | 
| 234 | 234 | |
| 235 | 235 | // Find offset | 
| @@ -238,11 +238,11 @@ discard block | ||
| 238 | 238 | // Algorithm from RFC | 
| 239 | 239 | return | 
| 240 | 240 | ( | 
| 241 | - (($hmac_result[$offset+0] & 0x7f) << 24 ) | | |
| 242 | - (($hmac_result[$offset+1] & 0xff) << 16 ) | | |
| 243 | - (($hmac_result[$offset+2] & 0xff) << 8 ) | | |
| 244 | - ($hmac_result[$offset+3] & 0xff) | |
| 245 | - ) % pow(10,$length); | |
| 241 | + (($hmac_result[$offset + 0] & 0x7f) << 24) | | |
| 242 | + (($hmac_result[$offset + 1] & 0xff) << 16) | | |
| 243 | + (($hmac_result[$offset + 2] & 0xff) << 8) | | |
| 244 | + ($hmac_result[$offset + 3] & 0xff) | |
| 245 | + ) % pow(10, $length); | |
| 246 | 246 | } | 
| 247 | 247 | |
| 248 | 248 | } | 
| @@ -37,7 +37,7 @@ discard block | ||
| 37 | 37 | $keyBytes, | 
| 38 | 38 | $text) | 
| 39 | 39 |      { | 
| 40 | - $hash = hash_hmac ($crypto, $text, $keyBytes); | |
| 40 | + $hash = hash_hmac($crypto, $text, $keyBytes); | |
| 41 | 41 | return $hash; | 
| 42 | 42 | } | 
| 43 | 43 | |
| @@ -48,7 +48,7 @@ discard block | ||
| 48 | 48 | * | 
| 49 | 49 | * @return String a string with raw bytes | 
| 50 | 50 | */ | 
| 51 | -    private static function _hexStr2Bytes($hex){ | |
| 51 | +    private static function _hexStr2Bytes($hex) { | |
| 52 | 52 |          return pack("H*", $hex); | 
| 53 | 53 | } | 
| 54 | 54 | |
| @@ -97,95 +97,95 @@ 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) | |
| 100 | + if (stripos($cryptoFunction, "sha1") !== false) | |
| 101 | 101 | $crypto = "sha1"; | 
| 102 | - if(stripos($cryptoFunction, "sha256")!==false) | |
| 102 | + if (stripos($cryptoFunction, "sha256") !== false) | |
| 103 | 103 | $crypto = "sha256"; | 
| 104 | - if(stripos($cryptoFunction, "sha512")!==false) | |
| 104 | + if (stripos($cryptoFunction, "sha512") !== false) | |
| 105 | 105 | $crypto = "sha512"; | 
| 106 | 106 | |
| 107 | - $codeDigits = substr($cryptoFunction, strrpos($cryptoFunction, "-")+1); | |
| 107 | + $codeDigits = substr($cryptoFunction, strrpos($cryptoFunction, "-") + 1); | |
| 108 | 108 | |
| 109 | 109 | // The size of the byte array message to be encrypted | 
| 110 | 110 | // Counter | 
| 111 | -        if($dataInput[0] == "c" ) { | |
| 111 | +        if ($dataInput[0] == "c") { | |
| 112 | 112 | // Fix the length of the HEX string | 
| 113 | - while(strlen($counter) < 16) | |
| 114 | - $counter = "0" . $counter; | |
| 115 | - $counterLength=8; | |
| 113 | + while (strlen($counter) < 16) | |
| 114 | + $counter = "0".$counter; | |
| 115 | + $counterLength = 8; | |
| 116 | 116 | } | 
| 117 | 117 | // Question | 
| 118 | - if($dataInput[0] == "q" || | |
| 119 | -                stripos($dataInput, "-q")!==false) { | |
| 120 | - while(strlen($question) < 256) | |
| 121 | - $question = $question . "0"; | |
| 122 | - $questionLength=128; | |
| 118 | + if ($dataInput[0] == "q" || | |
| 119 | +                stripos($dataInput, "-q") !== false) { | |
| 120 | + while (strlen($question) < 256) | |
| 121 | + $question = $question."0"; | |
| 122 | + $questionLength = 128; | |
| 123 | 123 | } | 
| 124 | 124 | |
| 125 | 125 | // Password | 
| 126 | -        if(stripos($dataInput, "psha1")!==false) { | |
| 127 | - while(strlen($password) < 40) | |
| 128 | - $password = "0" . $password; | |
| 129 | - $passwordLength=20; | |
| 126 | +        if (stripos($dataInput, "psha1") !== false) { | |
| 127 | + while (strlen($password) < 40) | |
| 128 | + $password = "0".$password; | |
| 129 | + $passwordLength = 20; | |
| 130 | 130 | } | 
| 131 | 131 | |
| 132 | -        if(stripos($dataInput, "psha256")!==false) { | |
| 133 | - while(strlen($password) < 64) | |
| 134 | - $password = "0" . $password; | |
| 135 | - $passwordLength=32; | |
| 132 | +        if (stripos($dataInput, "psha256") !== false) { | |
| 133 | + while (strlen($password) < 64) | |
| 134 | + $password = "0".$password; | |
| 135 | + $passwordLength = 32; | |
| 136 | 136 | } | 
| 137 | 137 | |
| 138 | -        if(stripos($dataInput, "psha512")!==false) { | |
| 139 | - while(strlen($password) < 128) | |
| 140 | - $password = "0" . $password; | |
| 141 | - $passwordLength=64; | |
| 138 | +        if (stripos($dataInput, "psha512") !== false) { | |
| 139 | + while (strlen($password) < 128) | |
| 140 | + $password = "0".$password; | |
| 141 | + $passwordLength = 64; | |
| 142 | 142 | } | 
| 143 | 143 | |
| 144 | 144 | // sessionInformation | 
| 145 | -        if(stripos($dataInput, "s064") !==false) { | |
| 146 | - while(strlen($sessionInformation) < 128) | |
| 147 | - $sessionInformation = "0" . $sessionInformation; | |
| 145 | +        if (stripos($dataInput, "s064") !== false) { | |
| 146 | + while (strlen($sessionInformation) < 128) | |
| 147 | + $sessionInformation = "0".$sessionInformation; | |
| 148 | 148 | |
| 149 | - $sessionInformationLength=64; | |
| 150 | -        } else if(stripos($dataInput, "s128") !==false) { | |
| 151 | - while(strlen($sessionInformation) < 256) | |
| 152 | - $sessionInformation = "0" . $sessionInformation; | |
| 149 | + $sessionInformationLength = 64; | |
| 150 | +        } else if (stripos($dataInput, "s128") !== false) { | |
| 151 | + while (strlen($sessionInformation) < 256) | |
| 152 | + $sessionInformation = "0".$sessionInformation; | |
| 153 | 153 | |
| 154 | - $sessionInformationLength=128; | |
| 155 | -        } else if(stripos($dataInput, "s256") !==false) { | |
| 156 | - while(strlen($sessionInformation) < 512) | |
| 157 | - $sessionInformation = "0" . $sessionInformation; | |
| 154 | + $sessionInformationLength = 128; | |
| 155 | +        } else if (stripos($dataInput, "s256") !== false) { | |
| 156 | + while (strlen($sessionInformation) < 512) | |
| 157 | + $sessionInformation = "0".$sessionInformation; | |
| 158 | 158 | |
| 159 | - $sessionInformationLength=256; | |
| 160 | -        } else if(stripos($dataInput, "s512") !==false) { | |
| 161 | - while(strlen($sessionInformation) < 128) | |
| 162 | - $sessionInformation = "0" . $sessionInformation; | |
| 159 | + $sessionInformationLength = 256; | |
| 160 | +        } else if (stripos($dataInput, "s512") !== false) { | |
| 161 | + while (strlen($sessionInformation) < 128) | |
| 162 | + $sessionInformation = "0".$sessionInformation; | |
| 163 | 163 | |
| 164 | - $sessionInformationLength=64; | |
| 165 | -        } else if (stripos($dataInput, "s") !== false ) { | |
| 164 | + $sessionInformationLength = 64; | |
| 165 | +        } else if (stripos($dataInput, "s") !== false) { | |
| 166 | 166 | // deviation from spec. Officially 's' without a length indicator is not in the reference implementation. | 
| 167 | 167 | // 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; | |
| 168 | + while (strlen($sessionInformation) < 128) | |
| 169 | + $sessionInformation = "0".$sessionInformation; | |
| 170 | 170 | |
| 171 | - $sessionInformationLength=64; | |
| 171 | + $sessionInformationLength = 64; | |
| 172 | 172 | } | 
| 173 | 173 | |
| 174 | 174 | |
| 175 | 175 | |
| 176 | 176 | // TimeStamp | 
| 177 | - if($dataInput[0] == "t" || | |
| 177 | + if ($dataInput[0] == "t" || | |
| 178 | 178 |                  stripos($dataInput, "-t") !== false) { | 
| 179 | - while(strlen($timeStamp) < 16) | |
| 180 | - $timeStamp = "0" . $timeStamp; | |
| 181 | - $timeStampLength=8; | |
| 179 | + while (strlen($timeStamp) < 16) | |
| 180 | + $timeStamp = "0".$timeStamp; | |
| 181 | + $timeStampLength = 8; | |
| 182 | 182 | } | 
| 183 | 183 | |
| 184 | 184 | // Put the bytes of "ocraSuite" parameters into the message | 
| 185 | 185 | |
| 186 | - $msg = array_fill(0,$ocraSuiteLength+$counterLength+$questionLength+$passwordLength+$sessionInformationLength+$timeStampLength+1, 0); | |
| 186 | + $msg = array_fill(0, $ocraSuiteLength + $counterLength + $questionLength + $passwordLength + $sessionInformationLength + $timeStampLength + 1, 0); | |
| 187 | 187 | |
| 188 | -        for($i=0;$i<strlen($ocraSuite);$i++) { | |
| 188 | +        for ($i = 0; $i < strlen($ocraSuite); $i++) { | |
| 189 | 189 | $msg[$i] = $ocraSuite[$i]; | 
| 190 | 190 | } | 
| 191 | 191 | |
| @@ -194,9 +194,9 @@ discard block | ||
| 194 | 194 | |
| 195 | 195 | // Put the bytes of "Counter" to the message | 
| 196 | 196 | // Input is HEX encoded | 
| 197 | -        if($counterLength > 0 ) { | |
| 197 | +        if ($counterLength > 0) { | |
| 198 | 198 | $bArray = self::_hexStr2Bytes($counter); | 
| 199 | -            for ($i=0;$i<strlen($bArray);$i++) { | |
| 199 | +            for ($i = 0; $i < strlen($bArray); $i++) { | |
| 200 | 200 | $msg [$i + $ocraSuiteLength + 1] = $bArray[$i]; | 
| 201 | 201 | } | 
| 202 | 202 | } | 
| @@ -204,36 +204,36 @@ discard block | ||
| 204 | 204 | |
| 205 | 205 | // Put the bytes of "question" to the message | 
| 206 | 206 | // Input is text encoded | 
| 207 | -        if($questionLength > 0 ) { | |
| 207 | +        if ($questionLength > 0) { | |
| 208 | 208 | $bArray = self::_hexStr2Bytes($question); | 
| 209 | -            for ($i=0;$i<strlen($bArray);$i++) { | |
| 209 | +            for ($i = 0; $i < strlen($bArray); $i++) { | |
| 210 | 210 | $msg [$i + $ocraSuiteLength + 1 + $counterLength] = $bArray[$i]; | 
| 211 | 211 | } | 
| 212 | 212 | } | 
| 213 | 213 | |
| 214 | 214 | // Put the bytes of "password" to the message | 
| 215 | 215 | // Input is HEX encoded | 
| 216 | -        if($passwordLength > 0){ | |
| 216 | +        if ($passwordLength > 0) { | |
| 217 | 217 | $bArray = self::_hexStr2Bytes($password); | 
| 218 | -            for ($i=0;$i<strlen($bArray);$i++) { | |
| 218 | +            for ($i = 0; $i < strlen($bArray); $i++) { | |
| 219 | 219 | $msg [$i + $ocraSuiteLength + 1 + $counterLength + $questionLength] = $bArray[$i]; | 
| 220 | 220 | } | 
| 221 | 221 | } | 
| 222 | 222 | |
| 223 | 223 | // Put the bytes of "sessionInformation" to the message | 
| 224 | 224 | // Input is text encoded | 
| 225 | -        if($sessionInformationLength > 0 ){ | |
| 225 | +        if ($sessionInformationLength > 0) { | |
| 226 | 226 | $bArray = self::_hexStr2Bytes($sessionInformation); | 
| 227 | -            for ($i=0;$i<strlen($bArray);$i++) { | |
| 227 | +            for ($i = 0; $i < strlen($bArray); $i++) { | |
| 228 | 228 | $msg [$i + $ocraSuiteLength + 1 + $counterLength + $questionLength + $passwordLength] = $bArray[$i]; | 
| 229 | 229 | } | 
| 230 | 230 | } | 
| 231 | 231 | |
| 232 | 232 | // Put the bytes of "time" to the message | 
| 233 | 233 | // Input is text value of minutes | 
| 234 | -        if($timeStampLength > 0){ | |
| 234 | +        if ($timeStampLength > 0) { | |
| 235 | 235 | $bArray = self::_hexStr2Bytes($timeStamp); | 
| 236 | -            for ($i=0;$i<strlen($bArray);$i++) { | |
| 236 | +            for ($i = 0; $i < strlen($bArray); $i++) { | |
| 237 | 237 | $msg [$i + $ocraSuiteLength + 1 + $counterLength + $questionLength + $passwordLength + $sessionInformationLength] = $bArray[$i]; | 
| 238 | 238 | } | 
| 239 | 239 | } | 
| @@ -260,23 +260,23 @@ discard block | ||
| 260 | 260 | static function _oath_truncate($hash, $length = 6) | 
| 261 | 261 |      { | 
| 262 | 262 | // Convert to dec | 
| 263 | - foreach(str_split($hash,2) as $hex) | |
| 263 | + foreach (str_split($hash, 2) as $hex) | |
| 264 | 264 |          { | 
| 265 | - $hmac_result[]=hexdec($hex); | |
| 265 | + $hmac_result[] = hexdec($hex); | |
| 266 | 266 | } | 
| 267 | 267 | |
| 268 | 268 | // Find offset | 
| 269 | 269 | $offset = $hmac_result[count($hmac_result) - 1] & 0xf; | 
| 270 | 270 | |
| 271 | 271 | $v = strval( | 
| 272 | - (($hmac_result[$offset+0] & 0x7f) << 24 ) | | |
| 273 | - (($hmac_result[$offset+1] & 0xff) << 16 ) | | |
| 274 | - (($hmac_result[$offset+2] & 0xff) << 8 ) | | |
| 275 | - ($hmac_result[$offset+3] & 0xff) | |
| 272 | + (($hmac_result[$offset + 0] & 0x7f) << 24) | | |
| 273 | + (($hmac_result[$offset + 1] & 0xff) << 16) | | |
| 274 | + (($hmac_result[$offset + 2] & 0xff) << 8) | | |
| 275 | + ($hmac_result[$offset + 3] & 0xff) | |
| 276 | 276 | ); | 
| 277 | 277 | |
| 278 | 278 | // Prefix truncated string with 0's to ensure it always has the required length | 
| 279 | - $v=str_pad($v, $length, "0", STR_PAD_LEFT); | |
| 279 | + $v = str_pad($v, $length, "0", STR_PAD_LEFT); | |
| 280 | 280 | |
| 281 | 281 | $v = substr($v, strlen($v) - $length); | 
| 282 | 282 | return $v; | 
| @@ -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 | } | 
| @@ -1,6 +1,6 @@ discard block | ||
| 1 | 1 | <?php | 
| 2 | 2 | |
| 3 | -require_once ( __DIR__ . "/../Tiqr/Random.php"); | |
| 3 | +require_once (__DIR__."/../Tiqr/Random.php"); | |
| 4 | 4 | |
| 5 | 5 |  class OATH_OCRAParser { | 
| 6 | 6 | |
| @@ -43,7 +43,7 @@ discard block | ||
| 43 | 43 | */ | 
| 44 | 44 |  	private function parseOCRASuite($ocraSuite) { | 
| 45 | 45 |  		if (!is_string($ocraSuite)) { | 
| 46 | -			throw new Exception('OCRASuite not in string format: ' . var_export($ocraSuite, TRUE)); | |
| 46 | +			throw new Exception('OCRASuite not in string format: '.var_export($ocraSuite, TRUE)); | |
| 47 | 47 | } | 
| 48 | 48 | |
| 49 | 49 | $ocraSuite = strtoupper($ocraSuite); | 
| @@ -51,54 +51,54 @@ discard block | ||
| 51 | 51 | |
| 52 | 52 |  		$s = explode(':', $ocraSuite); | 
| 53 | 53 |  		if (count($s) != 3) { | 
| 54 | -			throw new Exception('Invalid OCRASuite format: ' . var_export($ocraSuite, TRUE)); | |
| 54 | +			throw new Exception('Invalid OCRASuite format: '.var_export($ocraSuite, TRUE)); | |
| 55 | 55 | } | 
| 56 | 56 | |
| 57 | 57 |  		$algo = explode('-', $s[0]); | 
| 58 | 58 |  		if (count($algo) != 2) { | 
| 59 | -			throw new Exception('Invalid OCRA version: ' . var_export($s[0], TRUE)); | |
| 59 | +			throw new Exception('Invalid OCRA version: '.var_export($s[0], TRUE)); | |
| 60 | 60 | } | 
| 61 | 61 | |
| 62 | 62 |  		if ($algo[0] !== 'OCRA') { | 
| 63 | -			throw new Exception('Unsupported OCRA algorithm: ' . var_export($algo[0], TRUE)); | |
| 63 | +			throw new Exception('Unsupported OCRA algorithm: '.var_export($algo[0], TRUE)); | |
| 64 | 64 | } | 
| 65 | 65 | |
| 66 | 66 |  		if ($algo[1] !== '1') { | 
| 67 | -			throw new Exception('Unsupported OCRA version: ' . var_export($algo[1], TRUE)); | |
| 67 | +			throw new Exception('Unsupported OCRA version: '.var_export($algo[1], TRUE)); | |
| 68 | 68 | } | 
| 69 | 69 | $this->OCRAVersion = $algo[1]; | 
| 70 | 70 | |
| 71 | 71 |  		$cf = explode('-', $s[1]); | 
| 72 | 72 |  		if (count($cf) != 3) { | 
| 73 | -			throw new Exception('Invalid OCRA suite crypto function: ' . var_export($s[1], TRUE)); | |
| 73 | +			throw new Exception('Invalid OCRA suite crypto function: '.var_export($s[1], TRUE)); | |
| 74 | 74 | } | 
| 75 | 75 | |
| 76 | 76 |  		if ($cf[0] !== 'HOTP') { | 
| 77 | -			throw new Exception('Unsupported OCRA suite crypto function: ' . var_export($cf[0], TRUE)); | |
| 77 | +			throw new Exception('Unsupported OCRA suite crypto function: '.var_export($cf[0], TRUE)); | |
| 78 | 78 | } | 
| 79 | 79 | $this->CryptoFunctionType = $cf[0]; | 
| 80 | 80 | |
| 81 | 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)); | |
| 82 | +			throw new Exception('Unsupported hash function in OCRA suite crypto function: '.var_export($cf[1], TRUE)); | |
| 83 | 83 | } | 
| 84 | 84 | $this->CryptoFunctionHash = $cf[1]; | 
| 85 | 85 | $this->CryptoFunctionHashLength = $this->supportedHashFunctions[$cf[1]]; | 
| 86 | 86 | |
| 87 | 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)); | |
| 88 | +			throw new Exception('Invalid OCRA suite crypto function truncation length: '.var_export($cf[2], TRUE)); | |
| 89 | 89 | } | 
| 90 | 90 | $this->CryptoFunctionTruncation = intval($cf[2]); | 
| 91 | 91 | |
| 92 | 92 |  		$di = explode('-', $s[2]); | 
| 93 | 93 |  		if (count($cf) == 0) { | 
| 94 | -			throw new Exception('Invalid OCRA suite data input: ' . var_export($s[2], TRUE)); | |
| 94 | +			throw new Exception('Invalid OCRA suite data input: '.var_export($s[2], TRUE)); | |
| 95 | 95 | } | 
| 96 | 96 | |
| 97 | 97 | $data_input = array(); | 
| 98 | -		foreach($di as $elem) { | |
| 98 | +		foreach ($di as $elem) { | |
| 99 | 99 | $letter = $elem[0]; | 
| 100 | 100 |  			if (array_key_exists($letter, $data_input)) { | 
| 101 | -				throw new Exception('Duplicate field in OCRA suite data input: ' . var_export($elem, TRUE)); | |
| 101 | +				throw new Exception('Duplicate field in OCRA suite data input: '.var_export($elem, TRUE)); | |
| 102 | 102 | } | 
| 103 | 103 | $data_input[$letter] = 1; | 
| 104 | 104 | |
| @@ -110,13 +110,13 @@ discard block | ||
| 110 | 110 |  				} elseif (preg_match('/^Q([AHN])(\d+)$/', $elem, $match)) { | 
| 111 | 111 | $q_len = intval($match[2]); | 
| 112 | 112 |  					if ($q_len < 4 || $q_len > 64) { | 
| 113 | -						throw new Exception('Invalid OCRA suite data input question length: ' . var_export($q_len, TRUE)); | |
| 113 | +						throw new Exception('Invalid OCRA suite data input question length: '.var_export($q_len, TRUE)); | |
| 114 | 114 | } | 
| 115 | 115 | $this->Q = TRUE; | 
| 116 | 116 | $this->QType = $match[1]; | 
| 117 | 117 | $this->QLength = $q_len; | 
| 118 | 118 |  				} else { | 
| 119 | -					throw new Exception('Invalid OCRA suite data input question: ' . var_export($elem, TRUE)); | |
| 119 | +					throw new Exception('Invalid OCRA suite data input question: '.var_export($elem, TRUE)); | |
| 120 | 120 | } | 
| 121 | 121 |  			} elseif ($letter === 'P') { | 
| 122 | 122 |  				if (strlen($elem) == 1) { | 
| @@ -124,7 +124,7 @@ discard block | ||
| 124 | 124 |  				} else { | 
| 125 | 125 | $p_algo = substr($elem, 1); | 
| 126 | 126 |  					if (!array_key_exists($p_algo, $this->supportedHashFunctions)) { | 
| 127 | -						throw new Exception('Unsupported OCRA suite PIN hash function: ' . var_export($elem, TRUE)); | |
| 127 | +						throw new Exception('Unsupported OCRA suite PIN hash function: '.var_export($elem, TRUE)); | |
| 128 | 128 | } | 
| 129 | 129 | $this->P = TRUE; | 
| 130 | 130 | $this->PType = $p_algo; | 
| @@ -136,13 +136,13 @@ discard block | ||
| 136 | 136 |  				} elseif (preg_match('/^S(\d+)$/', $elem, $match)) { | 
| 137 | 137 | $s_len = intval($match[1]); | 
| 138 | 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)); | |
| 139 | +						throw new Exception('Invalid OCRA suite data input session information length: '.var_export($s_len, TRUE)); | |
| 140 | 140 | } | 
| 141 | 141 | |
| 142 | 142 | $this->S = TRUE; | 
| 143 | 143 | $this->SLength = $s_len; | 
| 144 | 144 |  				} else { | 
| 145 | -					throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($elem, TRUE)); | |
| 145 | +					throw new Exception('Invalid OCRA suite data input session information length: '.var_export($elem, TRUE)); | |
| 146 | 146 | } | 
| 147 | 147 |  			} elseif ($letter === 'T') { | 
| 148 | 148 |  				if (strlen($elem) == 1) { | 
| @@ -151,7 +151,7 @@ discard block | ||
| 151 | 151 |  					preg_match_all('/(\d+)([HMS])/', $elem, $match); | 
| 152 | 152 | |
| 153 | 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)); | |
| 154 | +						throw new Exception('Duplicate definitions in OCRA suite data input timestamp: '.var_export($elem, TRUE)); | |
| 155 | 155 | } | 
| 156 | 156 | |
| 157 | 157 | $length = 0; | 
| @@ -159,21 +159,21 @@ discard block | ||
| 159 | 159 | $length += intval($match[1][$i]) * $this->TPeriods[$match[2][$i]]; | 
| 160 | 160 | } | 
| 161 | 161 |  					if ($length <= 0) { | 
| 162 | -						throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); | |
| 162 | +						throw new Exception('Invalid OCRA suite data input timestamp: '.var_export($elem, TRUE)); | |
| 163 | 163 | } | 
| 164 | 164 | |
| 165 | 165 | $this->T = TRUE; | 
| 166 | 166 | $this->TLength = $length; | 
| 167 | 167 |  				} else { | 
| 168 | -					throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); | |
| 168 | +					throw new Exception('Invalid OCRA suite data input timestamp: '.var_export($elem, TRUE)); | |
| 169 | 169 | } | 
| 170 | 170 |  			} else { | 
| 171 | -				throw new Exception('Unsupported OCRA suite data input field: ' . var_export($elem, TRUE)); | |
| 171 | +				throw new Exception('Unsupported OCRA suite data input field: '.var_export($elem, TRUE)); | |
| 172 | 172 | } | 
| 173 | 173 | } | 
| 174 | 174 | |
| 175 | 175 |  		if (!$this->Q) { | 
| 176 | -			throw new Exception('OCRA suite data input question not defined: ' . var_export($s[2], TRUE)); | |
| 176 | +			throw new Exception('OCRA suite data input question not defined: '.var_export($s[2], TRUE)); | |
| 177 | 177 | } | 
| 178 | 178 | } | 
| 179 | 179 | |
| @@ -183,7 +183,7 @@ discard block | ||
| 183 | 183 | |
| 184 | 184 | $bytes = Tiqr_Random::randomBytes($q_length); | 
| 185 | 185 | |
| 186 | -		switch($q_type) { | |
| 186 | +		switch ($q_type) { | |
| 187 | 187 | case 'A': | 
| 188 | 188 | $challenge = base64_encode($bytes); | 
| 189 | 189 |  				$tr = implode("", unpack('H*', $bytes)); | 
| @@ -196,7 +196,7 @@ discard block | ||
| 196 | 196 |  				$challenge = implode("", unpack('N*', $bytes)); | 
| 197 | 197 | break; | 
| 198 | 198 | default: | 
| 199 | -				throw new Exception('Unsupported OCRASuite challenge type: ' . var_export($q_type, TRUE)); | |
| 199 | +				throw new Exception('Unsupported OCRASuite challenge type: '.var_export($q_type, TRUE)); | |
| 200 | 200 | break; | 
| 201 | 201 | } | 
| 202 | 202 | |
| @@ -208,7 +208,7 @@ discard block | ||
| 208 | 208 | |
| 209 | 209 |  	public function generateSessionInformation() { | 
| 210 | 210 |  		if (!$this->S) { | 
| 211 | -			throw new Exception('Session information not defined in OCRASuite: ' . var_export($this->OCRASuite, TRUE)); | |
| 211 | +			throw new Exception('Session information not defined in OCRASuite: '.var_export($this->OCRASuite, TRUE)); | |
| 212 | 212 | } | 
| 213 | 213 | |
| 214 | 214 | $s_length = $this->SLength; | 
| @@ -238,7 +238,7 @@ discard block | ||
| 238 | 238 | $result &= ($s1[$i] == $s2[$i]); | 
| 239 | 239 | } | 
| 240 | 240 | |
| 241 | - return (boolean)$result; | |
| 241 | + return (boolean) $result; | |
| 242 | 242 | } | 
| 243 | 243 | |
| 244 | 244 | } | 
| @@ -63,7 +63,7 @@ discard block | ||
| 63 | 63 | */ | 
| 64 | 64 | public function generateSessionKey() | 
| 65 | 65 |      { | 
| 66 | - return bin2hex( Tiqr_Random::randomBytes(self::SESSIONKEY_SIZE) ); | |
| 66 | + return bin2hex(Tiqr_Random::randomBytes(self::SESSIONKEY_SIZE)); | |
| 67 | 67 | } | 
| 68 | 68 | |
| 69 | 69 | /** | 
| @@ -107,19 +107,19 @@ discard block | ||
| 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"); | |
| 111 | -        if ($pos===false) { | |
| 110 | + if ($pos === false) $pos = stripos($ocraSuite, "-q"); | |
| 111 | +        if ($pos === false) { | |
| 112 | 112 | // No challenge config specified. Since we only support challenge based OCRA, we fallback to default 10 digit hexadecimal. | 
| 113 | 113 |              return array("format"=>"H", "length"=>10); | 
| 114 | 114 | } | 
| 115 | - $format = substr($ocraSuite, $pos+2, 1); | |
| 115 | + $format = substr($ocraSuite, $pos + 2, 1); | |
| 116 | 116 |          if (!in_array($format, array("N", "A", "H"))) { | 
| 117 | 117 | $format = "H"; | 
| 118 | 118 | } | 
| 119 | 119 | |
| 120 | - $length = (int)substr($ocraSuite, $pos+3, 2); | |
| 120 | + $length = (int) substr($ocraSuite, $pos + 3, 2); | |
| 121 | 121 | |
| 122 | -        if ($length<=0) { | |
| 122 | +        if ($length <= 0) { | |
| 123 | 123 | $length = 10; | 
| 124 | 124 | } | 
| 125 | 125 | |
| @@ -190,7 +190,7 @@ discard block | ||
| 190 | 190 | */ | 
| 191 | 191 | protected function _calculateResponse($ocraSuite, $secret, $challenge, $sessionKey) | 
| 192 | 192 |      {        | 
| 193 | -        if (strpos(strtolower($ocraSuite), "qn")!==false) { | |
| 193 | +        if (strpos(strtolower($ocraSuite), "qn") !== false) { | |
| 194 | 194 | |
| 195 | 195 | // challenge is decimal, but generateOcra always wants it in hex. | 
| 196 | 196 | $challenge = dechex($challenge); | 
| @@ -39,7 +39,7 @@ | ||
| 39 | 39 | public static function randomBytes($length) | 
| 40 | 40 |      { | 
| 41 | 41 | // Get $length cryptographically secure pseudo-random bytes | 
| 42 | - $rnd=\random_bytes($length); | |
| 42 | + $rnd = \random_bytes($length); | |
| 43 | 43 | |
| 44 | 44 |          if (strlen($rnd) !== $length) { | 
| 45 | 45 |              throw new Exception("random_bytes did not return the requested number of bytes"); | 
| @@ -41,15 +41,15 @@ | ||
| 41 | 41 |      { | 
| 42 | 42 | $url = $this->_options["url"]."?appId=".$this->_options["appid"]; | 
| 43 | 43 | |
| 44 | - $url.= "¬ificationToken=".$notificationToken; | |
| 44 | + $url .= "¬ificationToken=".$notificationToken; | |
| 45 | 45 | |
| 46 | 46 | $output = file_get_contents($url); | 
| 47 | -        if (stripos($output, "not found")!==false) { | |
| 47 | +        if (stripos($output, "not found") !== false) { | |
| 48 | 48 |              $this->logger->error('Token Exchange failed and responded with: not found', ['full output' => $output]); | 
| 49 | 49 | return false; | 
| 50 | 50 | } | 
| 51 | 51 | |
| 52 | -        if (stripos($output, "error")!==false) { | |
| 52 | +        if (stripos($output, "error") !== false) { | |
| 53 | 53 |              $this->logger->error('Token Exchange failed and responded with: error', ['full output' => $output]); | 
| 54 | 54 | return false; | 
| 55 | 55 | } | 
| @@ -58,7 +58,7 @@ | ||
| 58 | 58 | * the Tiqr_DeviceStorage factory will call this for you. | 
| 59 | 59 | * @param array $options The options for the s | 
| 60 | 60 | */ | 
| 61 | - public function __construct($options=array(), LoggerInterface $logger) | |
| 61 | + public function __construct($options = array(), LoggerInterface $logger) | |
| 62 | 62 |      { | 
| 63 | 63 | $this->_options = $options; | 
| 64 | 64 | $this->logger = $logger; | 
| @@ -51,7 +51,7 @@ discard block | ||
| 51 | 51 | * The default configuration | 
| 52 | 52 | */ | 
| 53 | 53 | const DEFAULT_HOST = '127.0.0.1'; | 
| 54 | - const DEFAULT_PORT = 11211; | |
| 54 | + const DEFAULT_PORT = 11211; | |
| 55 | 55 | |
| 56 | 56 | /** | 
| 57 | 57 | * Get the prefix to use for all keys in memcache. | 
| @@ -102,7 +102,7 @@ discard block | ||
| 102 | 102 | * (non-PHPdoc) | 
| 103 | 103 | * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::setValue() | 
| 104 | 104 | */ | 
| 105 | - public function setValue($key, $value, $expire=0) | |
| 105 | + public function setValue($key, $value, $expire = 0) | |
| 106 | 106 |      {   | 
| 107 | 107 | $key = $this->_getKeyPrefix().$key; | 
| 108 | 108 | |
| @@ -43,7 +43,7 @@ discard block | ||
| 43 | 43 | public function createUser($userId, $displayName) | 
| 44 | 44 |      { | 
| 45 | 45 |          $user = array("userId"=>$userId, | 
| 46 | - "displayName"=>$displayName); | |
| 46 | + "displayName"=>$displayName); | |
| 47 | 47 | return $this->_saveUser($userId, $user); | 
| 48 | 48 | } | 
| 49 | 49 | |
| @@ -77,7 +77,7 @@ discard block | ||
| 77 | 77 |      { | 
| 78 | 78 |          if ($data = $this->_loadUser($userId)) { | 
| 79 | 79 |              if (isset($data["notificationType"])) { | 
| 80 | - return $data["notificationType"]; | |
| 80 | + return $data["notificationType"]; | |
| 81 | 81 | } | 
| 82 | 82 | } | 
| 83 | 83 | return NULL; | 
| @@ -102,7 +102,7 @@ discard block | ||
| 102 | 102 |      { | 
| 103 | 103 |          if ($data = $this->_loadUser($userId)) { | 
| 104 | 104 |              if (isset($data["notificationAddress"])) { | 
| 105 | - return $data["notificationAddress"]; | |
| 105 | + return $data["notificationAddress"]; | |
| 106 | 106 | } | 
| 107 | 107 | } | 
| 108 | 108 |          $this->logger->info('Unable to find notification address for user'); |