2008-09-26 00:18:42 +00:00
|
|
|
<?php require_once '../yubiphpbase/appinclude.php';
|
|
|
|
require_once '../yubiphpbase/yubi_lib.php';
|
2008-09-26 03:21:11 +00:00
|
|
|
require_once 'common.php';
|
|
|
|
|
2008-09-23 01:37:25 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
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-10-07 08:24:28 +00:00
|
|
|
//// Check the client ID - does the client own the Yubikey?
|
2008-09-23 01:37:25 +00:00
|
|
|
//
|
2008-09-24 01:52:08 +00:00
|
|
|
|
2008-10-07 08:24:28 +00:00
|
|
|
if ($ad['chk_owner'] && $ad['client_id'] != $client) {
|
|
|
|
debug('Client-'.$client.' is not the owner of the Yubikey!');
|
|
|
|
sendResp(S_BAD_CLIENT, 'Not owner of the Yubikey');
|
|
|
|
exit;
|
|
|
|
}
|
2008-09-23 01:37:25 +00:00
|
|
|
|
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-23 07:12:42 +00:00
|
|
|
$apiKey = base64_decode($ad['c_secret']);
|
|
|
|
|
|
|
|
//// Check signature
|
|
|
|
//
|
|
|
|
if ($ad['chk_sig']) {
|
|
|
|
// Create the signature using the API key
|
2008-09-27 09:04:49 +00:00
|
|
|
$a = array();
|
|
|
|
$a['id']=$client;
|
|
|
|
$a['otp']=$otp;
|
|
|
|
$hmac = sign($a, $apiKey);
|
|
|
|
|
2008-09-23 07:12:42 +00:00
|
|
|
if (($h = getHttpVal('h', '')) == '') {
|
|
|
|
sendResp(S_MISSING_PARAMETER, 'h');
|
|
|
|
debug('signature missing, hmac='.$hmac);
|
|
|
|
exit;
|
|
|
|
} else if ($hmac != $h) {
|
|
|
|
sendResp(S_BAD_SIGNATURE);
|
|
|
|
debug('h='.$h.', hmac='.$hmac);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-27 09:04:49 +00:00
|
|
|
sendResp(S_BAD_OTP, 'Suspended');
|
2008-09-23 01:37:25 +00:00
|
|
|
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-27 09:04:49 +00:00
|
|
|
sendResp(S_BAD_CLIENT);
|
2008-09-23 01:37:25 +00:00
|
|
|
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);
|
2008-09-27 09:04:49 +00:00
|
|
|
sendResp(S_REPLAYED_OTP);
|
2008-09-23 01:37:25 +00:00
|
|
|
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);
|
2008-09-27 09:04:49 +00:00
|
|
|
sendResp(S_REPLAYED_OTP);
|
2008-09-23 01:37:25 +00:00
|
|
|
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);
|
2008-09-27 09:04:49 +00:00
|
|
|
sendResp(S_REPLAYED_OTP);
|
2008-09-23 01:37:25 +00:00
|
|
|
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-10-10 01:15:22 +00:00
|
|
|
if (updDB($ad['id'], $decoded_token, $client)) {
|
2008-09-18 23:42:35 +00:00
|
|
|
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) {
|
2008-09-23 07:12:42 +00:00
|
|
|
global $ad, $apiKey;
|
2008-09-23 01:37:25 +00:00
|
|
|
|
|
|
|
if ($status == null) {
|
|
|
|
$status = S_BACKEND_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-09-27 09:04:49 +00:00
|
|
|
echo 'status='.($a['status'] = $status).PHP_EOL;
|
2008-09-23 01:37:25 +00:00
|
|
|
if ($info != null) {
|
2008-09-27 09:04:49 +00:00
|
|
|
echo 'info='.($a['info'] = $info).PHP_EOL;
|
2008-09-23 01:37:25 +00:00
|
|
|
}
|
2008-09-27 09:04:49 +00:00
|
|
|
echo 't='.($a['t']=getUTCTimeStamp()).PHP_EOL;
|
|
|
|
$h = sign($a, $apiKey);
|
|
|
|
echo 'h='.$h.PHP_EOL;
|
|
|
|
echo PHP_EOL;
|
2008-09-23 01:37:25 +00:00
|
|
|
|
|
|
|
} // End sendResp
|
|
|
|
|
2008-10-10 01:15:22 +00:00
|
|
|
function updDB($keyid, $new, $client) {
|
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;
|
|
|
|
}
|
2008-10-10 01:15:22 +00:00
|
|
|
|
|
|
|
addHist(0, $_SERVER['REMOTE_ADDR'] , $keyid, $client);
|
|
|
|
|
2008-09-18 23:42:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-09-23 01:37:25 +00:00
|
|
|
|
2008-09-18 23:42:35 +00:00
|
|
|
?>
|