1*a635cb20SAndreas Gohr<?php 2*a635cb20SAndreas Gohr 3*a635cb20SAndreas Gohrnamespace dokuwiki\plugin\twofactor; 4*a635cb20SAndreas Gohr 5*a635cb20SAndreas Gohr/** 6*a635cb20SAndreas Gohr * PHP Class for handling Google Authenticator 2-factor authentication. 7*a635cb20SAndreas Gohr * 8*a635cb20SAndreas Gohr * Namespaced for twofactor DokuWiki Plugin 9*a635cb20SAndreas Gohr * 10*a635cb20SAndreas Gohr * @author Michael Kliewe 11*a635cb20SAndreas Gohr * @copyright 2012 Michael Kliewe 12*a635cb20SAndreas Gohr * @license http://www.opensource.org/licenses/bsd-license.php BSD License 13*a635cb20SAndreas Gohr * 14*a635cb20SAndreas Gohr * @link http://www.phpgangsta.de/ 15*a635cb20SAndreas Gohr * @link https://github.com/PHPGangsta/GoogleAuthenticator 16*a635cb20SAndreas Gohr */ 17*a635cb20SAndreas Gohrclass GoogleAuthenticator 18*a635cb20SAndreas Gohr{ 19*a635cb20SAndreas Gohr protected $_codeLength = 6; 20*a635cb20SAndreas Gohr 21*a635cb20SAndreas Gohr /** 22*a635cb20SAndreas Gohr * Create new secret. 23*a635cb20SAndreas Gohr * 16 characters, randomly chosen from the allowed base32 characters. 24*a635cb20SAndreas Gohr * 25*a635cb20SAndreas Gohr * @param int $secretLength 26*a635cb20SAndreas Gohr * 27*a635cb20SAndreas Gohr * @return string 28*a635cb20SAndreas Gohr * @throws \Exception 29*a635cb20SAndreas Gohr */ 30*a635cb20SAndreas Gohr public function createSecret($secretLength = 16) 31*a635cb20SAndreas Gohr { 32*a635cb20SAndreas Gohr $validChars = $this->_getBase32LookupTable(); 33*a635cb20SAndreas Gohr 34*a635cb20SAndreas Gohr // Valid secret lengths are 80 to 640 bits 35*a635cb20SAndreas Gohr if ($secretLength < 16 || $secretLength > 128) { 36*a635cb20SAndreas Gohr throw new \Exception('Bad secret length'); 37*a635cb20SAndreas Gohr } 38*a635cb20SAndreas Gohr $secret = ''; 39*a635cb20SAndreas Gohr $rnd = false; 40*a635cb20SAndreas Gohr if (function_exists('random_bytes')) { 41*a635cb20SAndreas Gohr $rnd = random_bytes($secretLength); 42*a635cb20SAndreas Gohr } elseif (function_exists('mcrypt_create_iv')) { 43*a635cb20SAndreas Gohr $rnd = mcrypt_create_iv($secretLength, MCRYPT_DEV_URANDOM); 44*a635cb20SAndreas Gohr } elseif (function_exists('openssl_random_pseudo_bytes')) { 45*a635cb20SAndreas Gohr $rnd = openssl_random_pseudo_bytes($secretLength, $cryptoStrong); 46*a635cb20SAndreas Gohr if (!$cryptoStrong) { 47*a635cb20SAndreas Gohr $rnd = false; 48*a635cb20SAndreas Gohr } 49*a635cb20SAndreas Gohr } 50*a635cb20SAndreas Gohr if ($rnd !== false) { 51*a635cb20SAndreas Gohr for ($i = 0; $i < $secretLength; ++$i) { 52*a635cb20SAndreas Gohr $secret .= $validChars[ord($rnd[$i]) & 31]; 53*a635cb20SAndreas Gohr } 54*a635cb20SAndreas Gohr } else { 55*a635cb20SAndreas Gohr throw new \Exception('No source of secure random'); 56*a635cb20SAndreas Gohr } 57*a635cb20SAndreas Gohr 58*a635cb20SAndreas Gohr return $secret; 59*a635cb20SAndreas Gohr } 60*a635cb20SAndreas Gohr 61*a635cb20SAndreas Gohr /** 62*a635cb20SAndreas Gohr * Calculate the code, with given secret and point in time. 63*a635cb20SAndreas Gohr * 64*a635cb20SAndreas Gohr * @param string $secret 65*a635cb20SAndreas Gohr * @param int|null $timeSlice 66*a635cb20SAndreas Gohr * 67*a635cb20SAndreas Gohr * @return string 68*a635cb20SAndreas Gohr */ 69*a635cb20SAndreas Gohr public function getCode($secret, $timeSlice = null) 70*a635cb20SAndreas Gohr { 71*a635cb20SAndreas Gohr if ($timeSlice === null) { 72*a635cb20SAndreas Gohr $timeSlice = floor(time() / 30); 73*a635cb20SAndreas Gohr } 74*a635cb20SAndreas Gohr 75*a635cb20SAndreas Gohr $secretkey = $this->_base32Decode($secret); 76*a635cb20SAndreas Gohr 77*a635cb20SAndreas Gohr // Pack time into binary string 78*a635cb20SAndreas Gohr $time = chr(0).chr(0).chr(0).chr(0).pack('N*', $timeSlice); 79*a635cb20SAndreas Gohr // Hash it with users secret key 80*a635cb20SAndreas Gohr $hm = hash_hmac('SHA1', $time, $secretkey, true); 81*a635cb20SAndreas Gohr // Use last nipple of result as index/offset 82*a635cb20SAndreas Gohr $offset = ord(substr($hm, -1)) & 0x0F; 83*a635cb20SAndreas Gohr // grab 4 bytes of the result 84*a635cb20SAndreas Gohr $hashpart = substr($hm, $offset, 4); 85*a635cb20SAndreas Gohr 86*a635cb20SAndreas Gohr // Unpak binary value 87*a635cb20SAndreas Gohr $value = unpack('N', $hashpart); 88*a635cb20SAndreas Gohr $value = $value[1]; 89*a635cb20SAndreas Gohr // Only 32 bits 90*a635cb20SAndreas Gohr $value = $value & 0x7FFFFFFF; 91*a635cb20SAndreas Gohr 92*a635cb20SAndreas Gohr $modulo = pow(10, $this->_codeLength); 93*a635cb20SAndreas Gohr 94*a635cb20SAndreas Gohr return str_pad($value % $modulo, $this->_codeLength, '0', STR_PAD_LEFT); 95*a635cb20SAndreas Gohr } 96*a635cb20SAndreas Gohr 97*a635cb20SAndreas Gohr /** 98*a635cb20SAndreas Gohr * Get QR-Code URL for image, from google charts. 99*a635cb20SAndreas Gohr * 100*a635cb20SAndreas Gohr * @param string $name 101*a635cb20SAndreas Gohr * @param string $secret 102*a635cb20SAndreas Gohr * @param string $title 103*a635cb20SAndreas Gohr * @param array $params 104*a635cb20SAndreas Gohr * 105*a635cb20SAndreas Gohr * @return string 106*a635cb20SAndreas Gohr */ 107*a635cb20SAndreas Gohr public function getQRCodeGoogleUrl($name, $secret, $title = null, $params = array()) 108*a635cb20SAndreas Gohr { 109*a635cb20SAndreas Gohr $width = !empty($params['width']) && (int) $params['width'] > 0 ? (int) $params['width'] : 200; 110*a635cb20SAndreas Gohr $height = !empty($params['height']) && (int) $params['height'] > 0 ? (int) $params['height'] : 200; 111*a635cb20SAndreas Gohr $level = !empty($params['level']) && array_search($params['level'], array('L', 'M', 'Q', 'H')) !== false ? $params['level'] : 'M'; 112*a635cb20SAndreas Gohr 113*a635cb20SAndreas Gohr $urlencoded = urlencode('otpauth://totp/'.$name.'?secret='.$secret.''); 114*a635cb20SAndreas Gohr if (isset($title)) { 115*a635cb20SAndreas Gohr $urlencoded .= urlencode('&issuer='.urlencode($title)); 116*a635cb20SAndreas Gohr } 117*a635cb20SAndreas Gohr 118*a635cb20SAndreas Gohr return "https://api.qrserver.com/v1/create-qr-code/?data=$urlencoded&size=${width}x${height}&ecc=$level"; 119*a635cb20SAndreas Gohr } 120*a635cb20SAndreas Gohr 121*a635cb20SAndreas Gohr /** 122*a635cb20SAndreas Gohr * Check if the code is correct. This will accept codes starting from $discrepancy*30sec ago to $discrepancy*30sec from now. 123*a635cb20SAndreas Gohr * 124*a635cb20SAndreas Gohr * @param string $secret 125*a635cb20SAndreas Gohr * @param string $code 126*a635cb20SAndreas Gohr * @param int $discrepancy This is the allowed time drift in 30 second units (8 means 4 minutes before or after) 127*a635cb20SAndreas Gohr * @param int|null $currentTimeSlice time slice if we want use other that time() 128*a635cb20SAndreas Gohr * 129*a635cb20SAndreas Gohr * @return bool 130*a635cb20SAndreas Gohr */ 131*a635cb20SAndreas Gohr public function verifyCode($secret, $code, $discrepancy = 1, $currentTimeSlice = null) 132*a635cb20SAndreas Gohr { 133*a635cb20SAndreas Gohr if ($currentTimeSlice === null) { 134*a635cb20SAndreas Gohr $currentTimeSlice = floor(time() / 30); 135*a635cb20SAndreas Gohr } 136*a635cb20SAndreas Gohr 137*a635cb20SAndreas Gohr if (strlen($code) != 6) { 138*a635cb20SAndreas Gohr return false; 139*a635cb20SAndreas Gohr } 140*a635cb20SAndreas Gohr 141*a635cb20SAndreas Gohr for ($i = -$discrepancy; $i <= $discrepancy; ++$i) { 142*a635cb20SAndreas Gohr $calculatedCode = $this->getCode($secret, $currentTimeSlice + $i); 143*a635cb20SAndreas Gohr if ($this->timingSafeEquals($calculatedCode, $code)) { 144*a635cb20SAndreas Gohr return true; 145*a635cb20SAndreas Gohr } 146*a635cb20SAndreas Gohr } 147*a635cb20SAndreas Gohr 148*a635cb20SAndreas Gohr return false; 149*a635cb20SAndreas Gohr } 150*a635cb20SAndreas Gohr 151*a635cb20SAndreas Gohr /** 152*a635cb20SAndreas Gohr * Set the code length, should be >=6. 153*a635cb20SAndreas Gohr * 154*a635cb20SAndreas Gohr * @param int $length 155*a635cb20SAndreas Gohr * 156*a635cb20SAndreas Gohr * @return GoogleAuthenticator 157*a635cb20SAndreas Gohr */ 158*a635cb20SAndreas Gohr public function setCodeLength($length) 159*a635cb20SAndreas Gohr { 160*a635cb20SAndreas Gohr $this->_codeLength = $length; 161*a635cb20SAndreas Gohr 162*a635cb20SAndreas Gohr return $this; 163*a635cb20SAndreas Gohr } 164*a635cb20SAndreas Gohr 165*a635cb20SAndreas Gohr /** 166*a635cb20SAndreas Gohr * Helper class to decode base32. 167*a635cb20SAndreas Gohr * 168*a635cb20SAndreas Gohr * @param $secret 169*a635cb20SAndreas Gohr * 170*a635cb20SAndreas Gohr * @return bool|string 171*a635cb20SAndreas Gohr */ 172*a635cb20SAndreas Gohr protected function _base32Decode($secret) 173*a635cb20SAndreas Gohr { 174*a635cb20SAndreas Gohr if (empty($secret)) { 175*a635cb20SAndreas Gohr return ''; 176*a635cb20SAndreas Gohr } 177*a635cb20SAndreas Gohr 178*a635cb20SAndreas Gohr $base32chars = $this->_getBase32LookupTable(); 179*a635cb20SAndreas Gohr $base32charsFlipped = array_flip($base32chars); 180*a635cb20SAndreas Gohr 181*a635cb20SAndreas Gohr $paddingCharCount = substr_count($secret, $base32chars[32]); 182*a635cb20SAndreas Gohr $allowedValues = array(6, 4, 3, 1, 0); 183*a635cb20SAndreas Gohr if (!in_array($paddingCharCount, $allowedValues)) { 184*a635cb20SAndreas Gohr return false; 185*a635cb20SAndreas Gohr } 186*a635cb20SAndreas Gohr for ($i = 0; $i < 4; ++$i) { 187*a635cb20SAndreas Gohr if ($paddingCharCount == $allowedValues[$i] && 188*a635cb20SAndreas Gohr substr($secret, -($allowedValues[$i])) != str_repeat($base32chars[32], $allowedValues[$i])) { 189*a635cb20SAndreas Gohr return false; 190*a635cb20SAndreas Gohr } 191*a635cb20SAndreas Gohr } 192*a635cb20SAndreas Gohr $secret = str_replace('=', '', $secret); 193*a635cb20SAndreas Gohr $secret = str_split($secret); 194*a635cb20SAndreas Gohr $binaryString = ''; 195*a635cb20SAndreas Gohr for ($i = 0; $i < count($secret); $i = $i + 8) { 196*a635cb20SAndreas Gohr $x = ''; 197*a635cb20SAndreas Gohr if (!in_array($secret[$i], $base32chars)) { 198*a635cb20SAndreas Gohr return false; 199*a635cb20SAndreas Gohr } 200*a635cb20SAndreas Gohr for ($j = 0; $j < 8; ++$j) { 201*a635cb20SAndreas Gohr $x .= str_pad(base_convert(@$base32charsFlipped[@$secret[$i + $j]], 10, 2), 5, '0', STR_PAD_LEFT); 202*a635cb20SAndreas Gohr } 203*a635cb20SAndreas Gohr $eightBits = str_split($x, 8); 204*a635cb20SAndreas Gohr for ($z = 0; $z < count($eightBits); ++$z) { 205*a635cb20SAndreas Gohr $binaryString .= (($y = chr(base_convert($eightBits[$z], 2, 10))) || ord($y) == 48) ? $y : ''; 206*a635cb20SAndreas Gohr } 207*a635cb20SAndreas Gohr } 208*a635cb20SAndreas Gohr 209*a635cb20SAndreas Gohr return $binaryString; 210*a635cb20SAndreas Gohr } 211*a635cb20SAndreas Gohr 212*a635cb20SAndreas Gohr /** 213*a635cb20SAndreas Gohr * Get array with all 32 characters for decoding from/encoding to base32. 214*a635cb20SAndreas Gohr * 215*a635cb20SAndreas Gohr * @return array 216*a635cb20SAndreas Gohr */ 217*a635cb20SAndreas Gohr protected function _getBase32LookupTable() 218*a635cb20SAndreas Gohr { 219*a635cb20SAndreas Gohr return array( 220*a635cb20SAndreas Gohr 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 7 221*a635cb20SAndreas Gohr 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 15 222*a635cb20SAndreas Gohr 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 23 223*a635cb20SAndreas Gohr 'Y', 'Z', '2', '3', '4', '5', '6', '7', // 31 224*a635cb20SAndreas Gohr '=', // padding char 225*a635cb20SAndreas Gohr ); 226*a635cb20SAndreas Gohr } 227*a635cb20SAndreas Gohr 228*a635cb20SAndreas Gohr /** 229*a635cb20SAndreas Gohr * A timing safe equals comparison 230*a635cb20SAndreas Gohr * more info here: http://blog.ircmaxell.com/2014/11/its-all-about-time.html. 231*a635cb20SAndreas Gohr * 232*a635cb20SAndreas Gohr * @param string $safeString The internal (safe) value to be checked 233*a635cb20SAndreas Gohr * @param string $userString The user submitted (unsafe) value 234*a635cb20SAndreas Gohr * 235*a635cb20SAndreas Gohr * @return bool True if the two strings are identical 236*a635cb20SAndreas Gohr */ 237*a635cb20SAndreas Gohr private function timingSafeEquals($safeString, $userString) 238*a635cb20SAndreas Gohr { 239*a635cb20SAndreas Gohr if (function_exists('hash_equals')) { 240*a635cb20SAndreas Gohr return hash_equals($safeString, $userString); 241*a635cb20SAndreas Gohr } 242*a635cb20SAndreas Gohr $safeLen = strlen($safeString); 243*a635cb20SAndreas Gohr $userLen = strlen($userString); 244*a635cb20SAndreas Gohr 245*a635cb20SAndreas Gohr if ($userLen != $safeLen) { 246*a635cb20SAndreas Gohr return false; 247*a635cb20SAndreas Gohr } 248*a635cb20SAndreas Gohr 249*a635cb20SAndreas Gohr $result = 0; 250*a635cb20SAndreas Gohr 251*a635cb20SAndreas Gohr for ($i = 0; $i < $userLen; ++$i) { 252*a635cb20SAndreas Gohr $result |= (ord($safeString[$i]) ^ ord($userString[$i])); 253*a635cb20SAndreas Gohr } 254*a635cb20SAndreas Gohr 255*a635cb20SAndreas Gohr // They are only identical strings if $result is exactly 0... 256*a635cb20SAndreas Gohr return $result === 0; 257*a635cb20SAndreas Gohr } 258*a635cb20SAndreas Gohr} 259