TwoFactorAuth.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. namespace RobThree\Auth;
  3. use RobThree\Auth\Providers\Qr\IQRCodeProvider;
  4. use RobThree\Auth\Providers\Qr\QRServerProvider;
  5. use RobThree\Auth\Providers\Rng\CSRNGProvider;
  6. use RobThree\Auth\Providers\Rng\HashRNGProvider;
  7. use RobThree\Auth\Providers\Rng\IRNGProvider;
  8. use RobThree\Auth\Providers\Rng\MCryptRNGProvider;
  9. use RobThree\Auth\Providers\Rng\OpenSSLRNGProvider;
  10. use RobThree\Auth\Providers\Time\HttpTimeProvider;
  11. use RobThree\Auth\Providers\Time\ITimeProvider;
  12. use RobThree\Auth\Providers\Time\LocalMachineTimeProvider;
  13. use RobThree\Auth\Providers\Time\NTPTimeProvider;
  14. // Based on / inspired by: https://github.com/PHPGangsta/GoogleAuthenticator
  15. // Algorithms, digits, period etc. explained: https://github.com/google/google-authenticator/wiki/Key-Uri-Format
  16. class TwoFactorAuth
  17. {
  18. /** @var string */
  19. private $algorithm;
  20. /** @var int */
  21. private $period;
  22. /** @var int */
  23. private $digits;
  24. /** @var string */
  25. private $issuer;
  26. /** @var ?IQRCodeProvider */
  27. private $qrcodeprovider = null;
  28. /** @var ?IRNGProvider */
  29. private $rngprovider = null;
  30. /** @var ?ITimeProvider */
  31. private $timeprovider = null;
  32. /** @var string */
  33. private static $_base32dict = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=';
  34. /** @var array */
  35. private static $_base32;
  36. /** @var array */
  37. private static $_base32lookup = array();
  38. /** @var array */
  39. private static $_supportedalgos = array('sha1', 'sha256', 'sha512', 'md5');
  40. /**
  41. * @param ?string $issuer
  42. * @param int $digits
  43. * @param int $period
  44. * @param string $algorithm
  45. * @param ?IQRCodeProvider $qrcodeprovider
  46. * @param ?IRNGProvider $rngprovider
  47. * @param ?ITimeProvider $timeprovider
  48. */
  49. public function __construct($issuer = null, $digits = 6, $period = 30, $algorithm = 'sha1', IQRCodeProvider $qrcodeprovider = null, IRNGProvider $rngprovider = null, ITimeProvider $timeprovider = null)
  50. {
  51. $this->issuer = $issuer;
  52. if (!is_int($digits) || $digits <= 0) {
  53. throw new TwoFactorAuthException('Digits must be int > 0');
  54. }
  55. $this->digits = $digits;
  56. if (!is_int($period) || $period <= 0) {
  57. throw new TwoFactorAuthException('Period must be int > 0');
  58. }
  59. $this->period = $period;
  60. $algorithm = strtolower(trim($algorithm));
  61. if (!in_array($algorithm, self::$_supportedalgos)) {
  62. throw new TwoFactorAuthException('Unsupported algorithm: ' . $algorithm);
  63. }
  64. $this->algorithm = $algorithm;
  65. $this->qrcodeprovider = $qrcodeprovider;
  66. $this->rngprovider = $rngprovider;
  67. $this->timeprovider = $timeprovider;
  68. self::$_base32 = str_split(self::$_base32dict);
  69. self::$_base32lookup = array_flip(self::$_base32);
  70. }
  71. /**
  72. * Create a new secret
  73. *
  74. * @param int $bits
  75. * @param bool $requirecryptosecure
  76. *
  77. * @return string
  78. */
  79. public function createSecret($bits = 80, $requirecryptosecure = true)
  80. {
  81. $secret = '';
  82. $bytes = (int) ceil($bits / 5); //We use 5 bits of each byte (since we have a 32-character 'alphabet' / BASE32)
  83. $rngprovider = $this->getRngProvider();
  84. if ($requirecryptosecure && !$rngprovider->isCryptographicallySecure()) {
  85. throw new TwoFactorAuthException('RNG provider is not cryptographically secure');
  86. }
  87. $rnd = $rngprovider->getRandomBytes($bytes);
  88. for ($i = 0; $i < $bytes; $i++) {
  89. $secret .= self::$_base32[ord($rnd[$i]) & 31]; //Mask out left 3 bits for 0-31 values
  90. }
  91. return $secret;
  92. }
  93. /**
  94. * Calculate the code with given secret and point in time
  95. *
  96. * @param string $secret
  97. * @param ?int $time
  98. *
  99. * @return string
  100. */
  101. public function getCode($secret, $time = null)
  102. {
  103. $secretkey = $this->base32Decode($secret);
  104. $timestamp = "\0\0\0\0" . pack('N*', $this->getTimeSlice($this->getTime($time))); // Pack time into binary string
  105. $hashhmac = hash_hmac($this->algorithm, $timestamp, $secretkey, true); // Hash it with users secret key
  106. $hashpart = substr($hashhmac, ord(substr($hashhmac, -1)) & 0x0F, 4); // Use last nibble of result as index/offset and grab 4 bytes of the result
  107. $value = unpack('N', $hashpart); // Unpack binary value
  108. $value = $value[1] & 0x7FFFFFFF; // Drop MSB, keep only 31 bits
  109. return str_pad((string) ($value % pow(10, $this->digits)), $this->digits, '0', STR_PAD_LEFT);
  110. }
  111. /**
  112. * Check if the code is correct. This will accept codes starting from ($discrepancy * $period) sec ago to ($discrepancy * period) sec from now
  113. *
  114. * @param string $secret
  115. * @param string $code
  116. * @param int $discrepancy
  117. * @param ?int $time
  118. * @param int $timeslice
  119. *
  120. * @return bool
  121. */
  122. public function verifyCode($secret, $code, $discrepancy = 1, $time = null, &$timeslice = 0)
  123. {
  124. $timestamp = $this->getTime($time);
  125. $timeslice = 0;
  126. // To keep safe from timing-attacks we iterate *all* possible codes even though we already may have
  127. // verified a code is correct. We use the timeslice variable to hold either 0 (no match) or the timeslice
  128. // of the match. Each iteration we either set the timeslice variable to the timeslice of the match
  129. // or set the value to itself. This is an effort to maintain constant execution time for the code.
  130. for ($i = -$discrepancy; $i <= $discrepancy; $i++) {
  131. $ts = $timestamp + ($i * $this->period);
  132. $slice = $this->getTimeSlice($ts);
  133. $timeslice = $this->codeEquals($this->getCode($secret, $ts), $code) ? $slice : $timeslice;
  134. }
  135. return $timeslice > 0;
  136. }
  137. /**
  138. * Timing-attack safe comparison of 2 codes (see http://blog.ircmaxell.com/2014/11/its-all-about-time.html)
  139. *
  140. * @param string $safe
  141. * @param string $user
  142. *
  143. * @return bool
  144. */
  145. private function codeEquals($safe, $user)
  146. {
  147. if (function_exists('hash_equals')) {
  148. return hash_equals($safe, $user);
  149. }
  150. // In general, it's not possible to prevent length leaks. So it's OK to leak the length. The important part is that
  151. // we don't leak information about the difference of the two strings.
  152. if (strlen($safe) === strlen($user)) {
  153. $result = 0;
  154. for ($i = 0; $i < strlen($safe); $i++) {
  155. $result |= (ord($safe[$i]) ^ ord($user[$i]));
  156. }
  157. return $result === 0;
  158. }
  159. return false;
  160. }
  161. /**
  162. * Get data-uri of QRCode
  163. *
  164. * @param string $label
  165. * @param string $secret
  166. * @param mixed $size
  167. *
  168. * @return string
  169. */
  170. public function getQRCodeImageAsDataUri($label, $secret, $size = 200)
  171. {
  172. if (!is_int($size) || $size <= 0) {
  173. throw new TwoFactorAuthException('Size must be int > 0');
  174. }
  175. $qrcodeprovider = $this->getQrCodeProvider();
  176. return 'data:'
  177. . $qrcodeprovider->getMimeType()
  178. . ';base64,'
  179. . base64_encode($qrcodeprovider->getQRCodeImage($this->getQRText($label, $secret), $size));
  180. }
  181. /**
  182. * Compare default timeprovider with specified timeproviders and ensure the time is within the specified number of seconds (leniency)
  183. * @param ?array $timeproviders
  184. * @param int $leniency
  185. *
  186. * @return void
  187. */
  188. public function ensureCorrectTime(array $timeproviders = null, $leniency = 5)
  189. {
  190. if ($timeproviders === null) {
  191. $timeproviders = array(
  192. new NTPTimeProvider(),
  193. new HttpTimeProvider()
  194. );
  195. }
  196. // Get default time provider
  197. $timeprovider = $this->getTimeProvider();
  198. // Iterate specified time providers
  199. foreach ($timeproviders as $t) {
  200. if (!($t instanceof ITimeProvider)) {
  201. throw new TwoFactorAuthException('Object does not implement ITimeProvider');
  202. }
  203. // Get time from default time provider and compare to specific time provider and throw if time difference is more than specified number of seconds leniency
  204. if (abs($timeprovider->getTime() - $t->getTime()) > $leniency) {
  205. throw new TwoFactorAuthException(sprintf('Time for timeprovider is off by more than %d seconds when compared to %s', $leniency, get_class($t)));
  206. }
  207. }
  208. }
  209. /**
  210. * @param ?int $time
  211. *
  212. * @return int
  213. */
  214. private function getTime($time = null)
  215. {
  216. return ($time === null) ? $this->getTimeProvider()->getTime() : $time;
  217. }
  218. /**
  219. * @param int $time
  220. * @param int $offset
  221. *
  222. * @return int
  223. */
  224. private function getTimeSlice($time = null, $offset = 0)
  225. {
  226. return (int)floor($time / $this->period) + ($offset * $this->period);
  227. }
  228. /**
  229. * Builds a string to be encoded in a QR code
  230. *
  231. * @param string $label
  232. * @param string $secret
  233. *
  234. * @return string
  235. */
  236. public function getQRText($label, $secret)
  237. {
  238. return 'otpauth://totp/' . rawurlencode($label)
  239. . '?secret=' . rawurlencode($secret)
  240. . '&issuer=' . rawurlencode($this->issuer)
  241. . '&period=' . intval($this->period)
  242. . '&algorithm=' . rawurlencode(strtoupper($this->algorithm))
  243. . '&digits=' . intval($this->digits);
  244. }
  245. /**
  246. * @param string $value
  247. * @return string
  248. */
  249. private function base32Decode($value)
  250. {
  251. if (strlen($value) == 0) {
  252. return '';
  253. }
  254. if (preg_match('/[^' . preg_quote(self::$_base32dict) . ']/', $value) !== 0) {
  255. throw new TwoFactorAuthException('Invalid base32 string');
  256. }
  257. $buffer = '';
  258. foreach (str_split($value) as $char) {
  259. if ($char !== '=') {
  260. $buffer .= str_pad(decbin(self::$_base32lookup[$char]), 5, '0', STR_PAD_LEFT);
  261. }
  262. }
  263. $length = strlen($buffer);
  264. $blocks = trim(chunk_split(substr($buffer, 0, $length - ($length % 8)), 8, ' '));
  265. $output = '';
  266. foreach (explode(' ', $blocks) as $block) {
  267. $output .= chr(bindec(str_pad($block, 8, '0', STR_PAD_RIGHT)));
  268. }
  269. return $output;
  270. }
  271. /**
  272. * @return IQRCodeProvider
  273. * @throws TwoFactorAuthException
  274. */
  275. public function getQrCodeProvider()
  276. {
  277. // Set default QR Code provider if none was specified
  278. if (null === $this->qrcodeprovider) {
  279. return $this->qrcodeprovider = new QRServerProvider();
  280. }
  281. return $this->qrcodeprovider;
  282. }
  283. /**
  284. * @return IRNGProvider
  285. * @throws TwoFactorAuthException
  286. */
  287. public function getRngProvider()
  288. {
  289. if (null !== $this->rngprovider) {
  290. return $this->rngprovider;
  291. }
  292. if (function_exists('random_bytes')) {
  293. return $this->rngprovider = new CSRNGProvider();
  294. }
  295. if (function_exists('mcrypt_create_iv')) {
  296. return $this->rngprovider = new MCryptRNGProvider();
  297. }
  298. if (function_exists('openssl_random_pseudo_bytes')) {
  299. return $this->rngprovider = new OpenSSLRNGProvider();
  300. }
  301. if (function_exists('hash')) {
  302. return $this->rngprovider = new HashRNGProvider();
  303. }
  304. throw new TwoFactorAuthException('Unable to find a suited RNGProvider');
  305. }
  306. /**
  307. * @return ITimeProvider
  308. * @throws TwoFactorAuthException
  309. */
  310. public function getTimeProvider()
  311. {
  312. // Set default time provider if none was specified
  313. if (null === $this->timeprovider) {
  314. return $this->timeprovider = new LocalMachineTimeProvider();
  315. }
  316. return $this->timeprovider;
  317. }
  318. }