1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
<?php
/**
* TfishYubikeyAuthenticator class file.
*
* @copyright Simon Wilkinson 2013-2017 (https://tuskfish.biz)
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
* @author Simon Wilkinson <[email protected]>
* @version Release: 1.0
* @since 1.0
* @package security
*/
// Enable strict type declaration.
declare(strict_types=1);
if (!defined("TFISH_ROOT_PATH"))
die("TFISH_ERROR_ROOT_PATH_NOT_DEFINED");
/**
* Two-factor authentication class.
*
* Handles two-factor authentication via a Yubikey hardware token, available from yubico.com.
* Set up requires obtaining a Client ID and secret key from Yubico, please refer to the manual for
* instructions on how to set it up.
*
* Note that the Yubikey authentication methods within this class are Copyright Tom Corwine and
* distributed under the GPL (V2). Please see the separate copyright block within the class file.
*
* Do not attempt to use this file without reading the manual.
*
* @copyright Simon Wilkinson 2013-2017 (https://tuskfish.biz)
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
* @author Simon Wilkinson <[email protected]>
* @version Release: 1.0
* @since 1.0
* @package security
*/
class TfishYubikeyAuthenticator
{
// Input.
/** @var int $_id ID of the Yubikey hardware token (first 12 characters of output). */
private $_id;
/** @var string $_signatureKey Yubikey API key obtained from
* https://upgrade.yubico.com/getapikey/ */
private $_signatureKey;
// Output.
/** @var string $_response Response message from last verification attempt */
private $_response;
// Internal.
/** @var array $_curlResult Response from cURL request to Yubico authentication server. */
private $_curlResult;
/** @var string $_curlError Error message. */
private $_curlError;
/** @var int $_timestampTolerance Timeout limit (expiry) for authentication requests. */
private $_timestampTolerance;
/** @var int $_curlTimeout Timeout limit when contacting Yubico authentication server. */
private $_curlTimeout;
/** Initialise default property values and unset unneeded ones. */
public function __construct()
{
if (defined("TFISH_YUBIKEY_ID")) {
$this->_id = (int) TFISH_YUBIKEY_ID;
}
if (defined("TFISH_YUBIKEY_SIGNATURE_KEY")) {
if (mb_strlen(TFISH_YUBIKEY_SIGNATURE_KEY, "UTF-8") === 28) {
$this->_signatureKey = base64_decode(TFISH_YUBIKEY_SIGNATURE_KEY);
}
} else {
trigger_error(TFISH_YUBIKEY_NO_SIGNATURE_KEY, E_USER_ERROR);
return false;
exit;
}
if (defined("TFISH_YUBIKEY_TIMESTAMP_TOLERANCE")) {
$this->_timestampTolerance = (int) TFISH_YUBIKEY_TIMESTAMP_TOLERANCE;
}
if (defined("TFISH_YUBIKEY_CURL_TIMEOUT")) {
$this->_curlTimeout = (int) TFISH_YUBIKEY_CURL_TIMEOUT;
}
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// Yubikey API methods, by Tom Corwine ([email protected])
//
// @license GNU General Public License (GPL) V2
//
// verify(string) - Accepts otp from Yubikey. Returns TRUE for authentication success,
// otherwise FALSE.
// getLastResponse() - Returns response message from verification attempt.
// getTimestampTolerance() - Gets the tolerance (+/-, in seconds) for timestamp
// verification
// setTimestampTolerance(int) - Sets the tolerance (in seconds, 0-86400) - default 600
// (10 minutes). Returns TRUE on success and FALSE on failure.
// getCurlTimeout() - Gets the timeout (in seconds) CURL uses before giving up on contacting
// Yubico's server.
// setCurlTimeout(int) - Sets the CURL timeout (in seconds, 0-600, 0 means indefinitely)
// - default 10.Returns TRUE on success and FALSE on failure.
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
/**
* Returns the timestamp tolerance (seconds).
*
* Timestamp tolerance is how long an authentication request will be accepted after it is
* generated. You need to allow some time for discrepancies between clocks and user delays.
* Default: 10 minutes.
*
* @return int Timestamp tolerance (seconds).
*/
public function getTimestampTolerance()
{
return $this->_timestampTolerance;
}
/**
* Set the timestamp tolerance.
*
* @param int $int Timestamp tolerance (seconds).
* @return bool True on success, false on failure.
*/
public function setTimestampTolerance(int $int)
{
$int = (int) $int;
if ($int > 0 && $int < 86400) {
$this->_timestampTolerance = $int;
return true;
} else {
return false;
}
}
/**
* Get the timeout for cURL requests, in seconds.
*
* @return int cURL timeout (seconds).
*/
public function getCurlTimeout()
{
return $this->_curlTimeout;
}
/**
* Set the cURL timeout.
*
* @param int $int cURL timeout (seconds).
* @return bool True on success, false on failure.
*/
public function setCurlTimeout(int $int)
{
$int = (int) $int;
if ($int > 0 && $int < 600) {
$this->_curlTimeout = $int;
return true;
} else {
return false;
}
}
/**
* Returns response message from last verification attempt.
*
* @return string Last response message.
*/
public function getLastResponse()
{
return $this->_response;
}
/**
* Authenticate using a Yubikey one-time password.
*
* @param string $otp One time password generated by Yubikey hardware token.
* @return bool True for successful authentication, false if fail.
*/
public function verify(string $otp)
{
$otp = TfishFilter::trimString($otp);
unset($this->_response);
unset($this->_curlResult);
unset($this->_curlError);
$otp = mb_strtolower($otp, "UTF-8");
if (!$this->_id) {
$this->_response = "ID NOT SET";
return false;
}
if (!$this->otpIsProperLength($otp)) {
$this->_response = "BAD OTP LENGTH";
return false;
}
if (!$this->otpIsModhex($otp)) {
$this->_response = "OTP NOT MODHEX";
return false;
}
$urlParams = "id=" . $this->_id . "&otp=" . $otp;
$url = $this->createSignedRequest($urlParams);
// Returns 0 on success.
if ($this->curlRequest($url)) {
$this->_response = "ERROR CONNECTING TO YUBICO - " . $this->_curlError;
return false;
}
foreach ($this->_curlResult as $param) {
if (mb_substr($param, 0, 2, "UTF-8") === "h=")
$signature = substr(trim($param), 2);
if (mb_substr($param, 0, 2, "UTF-8") === "t=")
$timestamp = substr(trim($param), 2);
if (mb_substr($param, 0, 7, "UTF-8") === "status=")
$status = substr(trim($param), 7);
}
// Concatenate string for signature verification
$signedMessage = "status=" . $status . "&t=" . $timestamp;
if (!$this->resultSignatureIsGood($signedMessage, $signature)) {
$this->_response = "BAD RESPONSE SIGNATURE";
return false;
}
if (!$this->resultTimestampIsGood($timestamp)) {
$this->_response = "BAD TIMESTAMP";
return false;
}
if ($status != "OK") {
$this->_response = $status;
return false;
}
// Everything went well - We pass
$this->_response = "OK";
return true;
}
/**
* Create URL with embedded and signed authentication request for Yubico authentication server.
*
* @param string $urlParams URL parameters.
* @return string URL to Yubico authentication server with query string parameters attached.
*/
protected function createSignedRequest(string $urlParams)
{
$urlParams = TfishFilter::trimString($urlParams);
if ($this->_signatureKey) {
$hash = urlencode(base64_encode(hash_hmac("sha1", $urlParams, $this->_signatureKey,
true)));
return "https://api.yubico.com/wsapi/verify?" . $urlParams . "&h=" . $hash;
} else {
return "https://api.yubico.com/wsapi/verify?" . $urlParams;
}
}
/**
* Make cURL request.
*
* @param string $url Target URL.
* @return string Error message.
*/
protected function curlRequest(string $url)
{
$url = TfishFilter::trimString($url);
if (!TfishFilter::isUrl($url)) {
trigger_error(TFISH_ERROR_NOT_URL, E_USER_ERROR);
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->_curlTimeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->_curlTimeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$this->_curlResult = explode("\n", curl_exec($ch));
$this->_curlError = curl_error($ch);
$error = curl_errno($ch);
curl_close($ch);
return $error;
}
/**
* Check Yubikey one time password is expected length.
*
* @param string $otp Yubikey one-time password.
* @return bool True if length is ok, otherwise false.
*/
protected function otpIsProperLength(string $otp)
{
$otp = TfishFilter::trimString($otp);
if (mb_strlen($otp, "UTF-8") === 44) {
return true;
} else {
return false;
}
}
/**
* Check Yubikey one time password is modhex encoded.
*
* @param string $otp Yubikey one-time password.
* @return bool True if modhex encoded, otherwise false.
*/
protected function otpIsModhex(string $otp)
{
$otp = TfishFilter::trimString($otp);
$modhexChars = array("c", "b", "d", "e", "f", "g", "h", "i", "j", "k", "l", "n", "r", "t",
"u", "v");
foreach (str_split($otp) as $char) {
if (!in_array($char, $modhexChars))
return false;
}
return true;
}
/**
* Check timestamp is within tolerance.
*
* @param string $timestamp Timestamp to check.
* @return bool True if timestamp is within tolerance, otherwise false.
*/
protected function resultTimestampIsGood(string $timestamp)
{
$timestamp = TfishFilter::trimString($timestamp);
// Turn times into 'seconds since Unix Epoch' for easy comparison
$now = date("U");
$timestampSeconds = (int) (date_format(date_create(mb_substr($timestamp, 0, -4, "UTF-8")), "U"));
// If date() functions above fail for any reason, so do we
if (!$timestamp || !$now)
return false;
if (($timestampSeconds + $this->_timestampTolerance) > $now &&
($timestampSeconds - $this->_timestampTolerance) < $now) {
return true;
} else {
return false;
}
}
/**
* Validate result signature.
*
* @param string $signedMessage Signed message.
* @param string $signature Signature.
* @return bool True if signature is good, otherwise false.
*/
protected function resultSignatureIsGood(string $signedMessage, string $signature)
{
$signedMessage = TfishFilter::trimString($signedMessage);
$signature = TfishFilter::trimString($signature);
if (!$this->_signatureKey)
return true;
if (base64_encode(hash_hmac("sha1", $signedMessage, $this->_signatureKey, true))
=== $signature) {
return true;
} else {
return false;
}
}
///////////////////////////////////////////////////////////////////////
///// END Yubikey API methods by Tom Corwine ([email protected]) /////
///////////////////////////////////////////////////////////////////////
}