2008-09-18 23:42:35 +00:00
|
|
|
<?php require_once '../yms/appinclude.php';
|
|
|
|
require_once '../yms/yubi_lib.php';
|
|
|
|
|
2008-09-23 01:37:25 +00:00
|
|
|
define('S_OK', 'OK');
|
|
|
|
define('S_BAD_OTP', 'BAD_OTP');
|
|
|
|
define('S_BAD_CLIENT', 'BAD_CLIENT'); // New, added by paul 20080920
|
|
|
|
define('S_REPLAYED_OTP', 'REPLAYED_OTP');
|
|
|
|
define('S_BAD_SIGNATURE', 'BAD_SIGNATURE');
|
|
|
|
define('S_MISSING_PARAMETER', 'MISSING_PARAMETER');
|
|
|
|
//define('S_NO_SUCH_CLIENT', 'NO_SUCH_CLIENT'); // Deprecated by paul 20080920
|
|
|
|
define('S_OPERATION_NOT_ALLOWED', 'OPERATION_NOT_ALLOWED');
|
|
|
|
define('S_BACKEND_ERROR', 'BACKEND_ERROR');
|
|
|
|
|
|
|
|
header("content-type: text/plain");
|
|
|
|
|
2008-09-20 01:36:24 +00:00
|
|
|
if (!isset($trace)) { $trace = 0; }
|
|
|
|
|
2008-09-23 01:37:25 +00:00
|
|
|
$client = getHttpVal('id', 0);
|
|
|
|
if ($client <= 0) {
|
|
|
|
debug('Client ID is missing');
|
|
|
|
sendResp(S_MISSING_PARAMETER, 'id');
|
2008-09-18 23:42:35 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$otp = getHttpVal('otp', '');
|
|
|
|
if ($otp == '') {
|
2008-09-23 01:37:25 +00:00
|
|
|
debug('OTP is missing');
|
|
|
|
sendResp(S_MISSING_PARAMETER, 'otp');
|
2008-09-18 23:42:35 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
//// Get Yubikey from DB
|
|
|
|
//
|
|
|
|
$devId = substr($otp, 0, 12);
|
|
|
|
$ad = getAuthData($devId);
|
2008-09-23 01:37:25 +00:00
|
|
|
debug('Auth Data from DB:');
|
2008-09-18 23:42:35 +00:00
|
|
|
|
|
|
|
if ($ad == null) {
|
2008-09-23 01:37:25 +00:00
|
|
|
debug('Invalid Yubikey '.$devId);
|
|
|
|
sendResp(S_BAD_OTP, $otp);
|
2008-09-18 23:42:35 +00:00
|
|
|
exit;
|
|
|
|
} else {
|
|
|
|
debug($ad);
|
|
|
|
}
|
|
|
|
|
2008-09-23 01:37:25 +00:00
|
|
|
//// Check the client ID
|
|
|
|
//
|
|
|
|
if ($ad['client_id'] != $client) {
|
|
|
|
debug('Client-'.$client.' is not the owner of the Yubikey! The key will be suspended with excessive failed attempts.');
|
|
|
|
sendResp(S_BAD_CLIENT, 'Not owner of the Yubikey');
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2008-09-18 23:42:35 +00:00
|
|
|
$k = b64ToModhex($ad['secret']);
|
2008-09-20 01:36:24 +00:00
|
|
|
//debug('aes key in modhex = '.$k);
|
2008-09-18 23:42:35 +00:00
|
|
|
$key16 = ModHex::Decode($k);
|
2008-09-20 01:36:24 +00:00
|
|
|
//debug('aes key in hex = ['.$key16.'], length = '.strlen($key16));
|
2008-09-18 23:42:35 +00:00
|
|
|
|
|
|
|
//// Decode OTP from input
|
|
|
|
//
|
2008-09-23 01:37:25 +00:00
|
|
|
debug('From the OTP validation request:');
|
2008-09-18 23:42:35 +00:00
|
|
|
$decoded_token = Yubikey::Decode($otp, $key16);
|
|
|
|
debug($decoded_token);
|
|
|
|
if ( ! is_array($decoded_token) ) {
|
2008-09-23 01:37:25 +00:00
|
|
|
sendResp(S_BAD_OTP, $otp);
|
|
|
|
exit;
|
2008-09-18 23:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//// Sanity check key status
|
|
|
|
//
|
|
|
|
if ($ad['active'] < 1) {
|
2008-09-23 01:37:25 +00:00
|
|
|
sendResp(S_BAD_OTP, 'Suspended Yubikey');
|
|
|
|
exit;
|
2008-09-18 23:42:35 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 01:37:25 +00:00
|
|
|
//// Sanity check client status
|
2008-09-18 23:42:35 +00:00
|
|
|
//
|
|
|
|
if ($ad['c_active'] < 1) {
|
2008-09-23 01:37:25 +00:00
|
|
|
sendResp(S_BAD_CLIENT, 'Suspended client');
|
|
|
|
exit;
|
2008-09-18 23:42:35 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 01:37:25 +00:00
|
|
|
//// Sanity check token ID
|
2008-09-18 23:42:35 +00:00
|
|
|
//
|
|
|
|
if (strlen($decoded_token["public_id"]) == 12 ) {
|
|
|
|
debug("Token ID OK (".$decoded_token["public_id"].")");
|
2008-09-23 01:37:25 +00:00
|
|
|
} else {
|
|
|
|
debug("TOKEN ID FAILED, ".$decoded_token["public_id"]);
|
|
|
|
sendResp(S_BAD_OTP, $otp);
|
|
|
|
exit;
|
|
|
|
}
|
2008-09-18 23:42:35 +00:00
|
|
|
|
2008-09-23 01:37:25 +00:00
|
|
|
//// Sanity check the OTP
|
2008-09-18 23:42:35 +00:00
|
|
|
//
|
2008-09-22 20:42:19 +00:00
|
|
|
if ( strlen($decoded_token["token"]) != 32) {
|
2008-09-23 01:37:25 +00:00
|
|
|
debug("Wrong OTP length,".strlen($decoded_token["token"]));
|
|
|
|
sendResp(S_BAD_OTP, $otp);
|
|
|
|
exit;
|
2008-09-22 20:42:19 +00:00
|
|
|
}
|
2008-09-18 23:42:35 +00:00
|
|
|
|
2008-09-23 01:37:25 +00:00
|
|
|
//// Check the session counter
|
2008-09-18 23:42:35 +00:00
|
|
|
//
|
2008-09-20 01:36:24 +00:00
|
|
|
$sessionCounter = $decoded_token["session_counter"]; // From the req
|
2008-09-18 23:42:35 +00:00
|
|
|
$seenSessionCounter = $ad['counter']; // From DB
|
|
|
|
$scDiff = $seenSessionCounter - $sessionCounter;
|
|
|
|
if ($scDiff > 0) {
|
2008-09-23 01:37:25 +00:00
|
|
|
debug("Replayed session counter=".$sessionCounter.', seen='.$seenSessionCounter);
|
|
|
|
sendResp(S_REPLAYED_OTP, 'session counter');
|
|
|
|
exit;
|
2008-09-18 23:42:35 +00:00
|
|
|
} else {
|
2008-09-22 20:42:19 +00:00
|
|
|
debug("Session counter OK (".$sessionCounter.")");
|
2008-09-18 23:42:35 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 01:37:25 +00:00
|
|
|
//// Check the high counter
|
|
|
|
//
|
2008-09-20 01:36:24 +00:00
|
|
|
$hi = $decoded_token["high"]; // From the req
|
2008-09-18 23:42:35 +00:00
|
|
|
$seenHi = $ad['high']; // From DB
|
|
|
|
$hiDiff = $seenHi - $hi;
|
|
|
|
if ($scDiff == 0 && $hiDiff > 0) {
|
2008-09-23 01:37:25 +00:00
|
|
|
debug("Replayed hi counter=".$hi.', seen='.$seenHi);
|
|
|
|
sendResp(S_REPLAYED_OTP, 'hi counter');
|
|
|
|
exit;
|
2008-09-18 23:42:35 +00:00
|
|
|
} else {
|
2008-09-23 01:37:25 +00:00
|
|
|
debug("Hi counter OK (".$hi.")");
|
2008-09-18 23:42:35 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 01:37:25 +00:00
|
|
|
//// Check the low counter
|
|
|
|
//
|
2008-09-20 01:36:24 +00:00
|
|
|
$lo = $decoded_token["low"]; // From the req
|
2008-09-18 23:42:35 +00:00
|
|
|
$seenLo = $ad['low']; // From DB
|
|
|
|
$loDiff = $seenLo - $lo;
|
2008-09-22 20:42:19 +00:00
|
|
|
if ($scDiff == 0 && $hiDiff == 0 && $loDiff >= 0) {
|
2008-09-23 01:37:25 +00:00
|
|
|
debug("Replayed low counter=".$lo.', seen='.$seenLo);
|
|
|
|
sendResp(S_REPLAYED_OTP, 'lo counter');
|
|
|
|
exit;
|
2008-09-18 23:42:35 +00:00
|
|
|
} else {
|
2008-09-23 01:37:25 +00:00
|
|
|
debug("Lo counter OK (".$lo.")");
|
2008-09-18 23:42:35 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 01:37:25 +00:00
|
|
|
//// Update the DB only upon validation success
|
|
|
|
//
|
2008-09-18 23:42:35 +00:00
|
|
|
if (updDB($ad['id'], $decoded_token)) {
|
|
|
|
debug('Validation database updated');
|
2008-09-23 01:37:25 +00:00
|
|
|
sendResp(S_OK);
|
|
|
|
} else {
|
|
|
|
debug('Failed to update validation database');
|
|
|
|
sendResp(S_BACKEND_ERROR);
|
2008-09-18 23:42:35 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 01:37:25 +00:00
|
|
|
//////////////////////////
|
|
|
|
// Functions
|
|
|
|
//////////////////////////
|
|
|
|
|
|
|
|
function sendResp($status, $info=null) {
|
|
|
|
global $ad;
|
|
|
|
|
|
|
|
if ($status == null) {
|
|
|
|
$status = S_BACKEND_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
date_default_timezone_set('UTC');
|
|
|
|
$timestamp = date('Y-m-d\TH:i:s\ZZ', time());
|
|
|
|
|
|
|
|
//// Prepare the response to the user
|
|
|
|
//
|
|
|
|
$respParams = 'status='.$status.'&t='.$timestamp;
|
|
|
|
|
|
|
|
// Generate the signature
|
|
|
|
debug('API key: '.$ad['c_secret']); // API key of the client
|
|
|
|
$apiKey = base64_decode($ad['c_secret']);
|
|
|
|
debug('Signing: '.$respParams);
|
|
|
|
// the TRUE at the end states we want the raw value, not hexadecimal form
|
|
|
|
$hmac = hash_hmac('sha1', utf8_encode($respParams), $apiKey, true);
|
|
|
|
//outputToFile('hmac', $hmac, "b");
|
|
|
|
// now take that byte value and base64 encode it
|
|
|
|
$hmac = base64_encode($hmac);
|
|
|
|
debug('h: '.$hmac);
|
|
|
|
|
|
|
|
echo 'h='.$hmac.PHP_EOL;
|
|
|
|
echo 't='.$timestamp.PHP_EOL;
|
|
|
|
if ($info != null) {
|
|
|
|
echo 'info='.$info.PHP_EOL;
|
|
|
|
}
|
|
|
|
echo 'status='.$status.PHP_EOL.PHP_EOL;
|
|
|
|
|
|
|
|
} // End sendResp
|
|
|
|
|
2008-09-18 23:42:35 +00:00
|
|
|
function debug($msg, $exit=false) {
|
|
|
|
global $trace;
|
|
|
|
if ($trace) {
|
|
|
|
if (is_array($msg)) {
|
|
|
|
print_r($msg);
|
|
|
|
} else {
|
2008-09-23 01:37:25 +00:00
|
|
|
echo 'debug> '.$msg;
|
2008-09-18 23:42:35 +00:00
|
|
|
}
|
2008-09-23 01:37:25 +00:00
|
|
|
echo "\n";
|
2008-09-18 23:42:35 +00:00
|
|
|
}
|
|
|
|
if ($exit) {
|
|
|
|
die ('<font color=red><h4>Exit</h4></font>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 01:37:25 +00:00
|
|
|
function updDB($keyid, $new) {
|
2008-09-18 23:42:35 +00:00
|
|
|
$stmt = 'UPDATE yubikeys SET '.
|
|
|
|
'accessed=NOW(),'.
|
2008-09-20 01:36:24 +00:00
|
|
|
'counter='.$new['session_counter'].','.
|
|
|
|
'low='.$new['low'].','.
|
|
|
|
'high='.$new['high'].
|
2008-09-23 01:37:25 +00:00
|
|
|
' WHERE id='.$keyid;
|
2008-09-18 23:42:35 +00:00
|
|
|
if (!query($stmt)) {
|
2008-09-23 01:37:25 +00:00
|
|
|
$err = 'Failed to update validation data of key: '.$keyid.' by '.$stmt;
|
2008-09-18 23:42:35 +00:00
|
|
|
debug($err);
|
|
|
|
writeLog($err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2008-09-23 01:37:25 +00:00
|
|
|
|
|
|
|
function outputToFile($outFname, $content, $mode, $append=false) {
|
|
|
|
$out = fopen($outFname, ($append ? "a" : "w"));
|
|
|
|
fwrite($out, $content);
|
|
|
|
fclose($out);
|
|
|
|
}
|
2008-09-18 23:42:35 +00:00
|
|
|
?>
|