1
0
mirror of https://github.com/Yubico/yubikey-val.git synced 2025-02-01 01:52:18 +01:00
yubikey-val/verifyOTP.php

237 lines
5.8 KiB
PHP
Raw Normal View History

2008-12-03 07:49:32 +00:00
<?php
require_once '../yubiphpbase/appinclude.php';
2009-03-10 13:34:39 +00:00
require_once '../yubiphpbase/yubi_lib.php';
2008-12-03 07:49:32 +00:00
require_once 'common.php';
2008-09-23 01:37:25 +00:00
header("content-type: text/plain");
2008-12-03 07:49:32 +00:00
if (!isset ($trace)) {
$trace = 0;
}
2008-09-20 01:36:24 +00:00
2009-03-10 22:01:46 +00:00
//// Extract values from HTTP request
//
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;
}
2009-03-10 22:01:46 +00:00
$otp = strtolower($otp);
2008-09-18 23:42:35 +00:00
2009-03-10 22:01:46 +00:00
//// Get Client info from DB
2008-09-18 23:42:35 +00:00
//
2009-03-10 22:01:46 +00:00
$cd = getClientData($client);
if ($cd == null) {
debug('Invalid client id ' . $client);
sendResp(S_NO_SUCH_CLIENT, $client);
2008-09-18 23:42:35 +00:00
exit;
}
2009-03-10 22:01:46 +00:00
debug($cd);
2008-09-18 23:42:35 +00:00
2009-03-10 22:01:46 +00:00
//// Check client signature
2008-09-23 07:12:42 +00:00
//
2009-03-10 22:01:46 +00:00
$apiKey = base64_decode($cd['secret']);
2008-12-08 23:08:17 +00:00
$h = getHttpVal('h', '');
2009-03-10 22:01:46 +00:00
if ($cd['chk_sig'] && $h == '') {
2008-12-08 23:08:17 +00:00
sendResp(S_MISSING_PARAMETER, 'h');
debug('Signature missing');
exit;
2009-03-10 22:01:46 +00:00
} else if ($cd['chk_sig'] || $h != '') {
2008-09-23 07:12:42 +00:00
// Create the signature using the API key
2008-12-03 07:49:32 +00:00
$a = array ();
$a['id'] = $client;
$a['otp'] = $otp;
2008-09-27 09:04:49 +00:00
$hmac = sign($a, $apiKey);
2008-12-08 23:08:17 +00:00
// Compare it
if ($hmac != $h) {
sendResp(S_BAD_SIGNATURE);
2008-12-08 23:40:11 +00:00
debug('client hmac=' . $h . ', server hmac=' . $hmac);
2008-09-23 07:12:42 +00:00
exit;
2008-12-08 23:08:17 +00:00
}
2009-03-10 22:01:46 +00:00
debug('signature ok h=' . $h);
2008-09-23 07:12:42 +00:00
}
2009-03-10 22:01:46 +00:00
//// Get Yubikey from DB
2008-09-18 23:42:35 +00:00
//
2009-03-10 22:01:46 +00:00
$devId = substr($otp, 0, 12);
$ad = getAuthData($devId);
2008-09-18 23:42:35 +00:00
2009-03-10 22:01:46 +00:00
if ($ad == null) {
debug('Invalid Yubikey ' . $devId);
2008-09-23 01:37:25 +00:00
sendResp(S_BAD_OTP, $otp);
exit;
2009-03-10 22:01:46 +00:00
} else {
debug($ad);
2008-09-23 01:37:25 +00:00
}
2008-09-18 23:42:35 +00:00
2009-03-10 22:01:46 +00:00
$k = b64ToModhex($ad['secret']);
//debug('aes key in modhex = '.$k);
$key16 = ModHex :: Decode($k);
//debug('aes key in hex = ['.$key16.'], length = '.strlen($key16));
//// Decode OTP from input
2008-09-18 23:42:35 +00:00
//
2009-03-10 22:01:46 +00:00
debug('OTP validation req:');
$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);
2008-12-03 07:49:32 +00:00
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-12-03 07:49:32 +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-12-03 07:49:32 +00:00
debug("Session counter OK (" . $sessionCounter . ")");
2008-09-18 23:42:35 +00:00
}
2009-03-10 22:01:46 +00:00
//// Check the session use
//
$sessionUse = $decoded_token["session_use"]; // From the req
$seenSessionUse = $ad['sessionUse']; // From DB
$sucDiff = $seenSessionUse - $sessionUse;
if ($sucDiff > 0) {
debug("Replayed session use=" . $sessionUse . ', seen=' . $seenSessionUse);
sendResp(S_REPLAYED_OTP);
exit;
} else {
debug("Session counter OK (" . $sessionCounter . ")");
}
2008-12-03 07:49:32 +00:00
//// Check the time stamp
2008-09-23 01:37:25 +00:00
//
2008-12-03 07:49:32 +00:00
if ($scDiff == 0) { // Same use session, check time stamp diff
$ts = $decoded_token['timestamp'];
$seenTs = ($ad['high'] << 16) + $ad['low'];
$tsDiff = $ts - $seenTs;
if ($tsDiff <= 0) {
debug("Replayed time stamp=" . $ts . ', seen=' . $seenTs);
sendResp(S_REPLAYED_OTP);
exit;
} else {
2008-12-03 18:27:48 +00:00
updDB($ad['id'], $decoded_token, $client);
2008-12-03 07:49:32 +00:00
$tsDelta = $tsDiff * TS_SEC;
2008-12-03 18:27:48 +00:00
debug("Timestamp OK (" . $ts . ") delta count=" . $tsDiff .
'-> delta secs=' . $tsDelta);
2008-12-03 07:49:32 +00:00
}
2008-12-03 18:27:48 +00:00
//// Check the real time
//
if ($ad['chk_time']) {
$lastTime = strtotime($ad['accessed']);
2008-12-17 06:43:05 +00:00
debug('Last accessed: '.$ad['accessed'].', '.$lastTime.', '.date("F j, Y, g:i a", $lastTime));
2008-12-03 18:27:48 +00:00
$elapsed = time() - $lastTime;
debug('Elapsed time from last validation: ' . $elapsed . ' secs');
$deviation = abs($elapsed - $tsDelta);
2008-12-03 18:58:25 +00:00
$percent = truncate(100*$deviation/$elapsed, 8) . '%';
2008-12-03 18:27:48 +00:00
debug("Key time deviation vs. elapsed time=".$deviation.' secs ('.
2008-12-03 18:41:02 +00:00
$percent.')');
2008-12-03 18:27:48 +00:00
if ($deviation > TS_TOLERANCE * $elapsed) {
debug("Is the OTP generated from a real crypto key?");
2009-03-10 22:01:46 +00:00
sendResp(S_PHISHED_OTP);
2008-12-03 18:27:48 +00:00
exit;
}
2008-12-03 07:49:32 +00:00
}
2008-12-03 18:27:48 +00:00
} // End check time stamp
2008-09-18 23:42:35 +00:00
2008-12-03 07:49:32 +00:00
//// Check the high counter
//
//$hi = $decoded_token["high"]; // From the req
//$seenHi = $ad['high']; // From DB
//$hiDiff = $seenHi - $hi;
//if ($scDiff == 0 && $hiDiff > 0) {
// debug("Replayed hi counter=".$hi.', seen='.$seenHi);
// sendResp(S_REPLAYED_OTP);
// exit;
//} else {
// debug("Hi counter OK (".$hi.")");
//}
2008-09-23 01:37:25 +00:00
//// Check the low counter
//
2008-12-03 07:49:32 +00:00
//$lo = $decoded_token["low"]; // From the req
//$seenLo = $ad['low']; // From DB
//$loDiff = $seenLo - $lo;
//if ($scDiff == 0 && $hiDiff == 0 && $loDiff >= 0) {
// debug("Replayed low counter=".$lo.', seen='.$seenLo);
// sendResp(S_REPLAYED_OTP);
// exit;
//} else {
// 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-12-03 07:49:32 +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
//////////////////////////
2008-12-03 07:49:32 +00:00
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;
}
2009-03-10 22:01:46 +00:00
$a['status'] = $status;
#$a['info'] = $info;
$a['t'] = getUTCTimeStamp();
2008-09-27 09:04:49 +00:00
$h = sign($a, $apiKey);
2009-03-10 22:01:46 +00:00
echo "h=" . $h . "\r\n";
echo "t=" . ($a['t']) . "\r\n";
echo "status=" . ($a['status']) . "\r\n";
if ($a['info'] != null) {
echo "info=" . ($a['info']) . "\r\n";
}
echo "\r\n";
2008-09-23 01:37:25 +00:00
} // End sendResp
2008-10-10 01:15:22 +00:00
function updDB($keyid, $new, $client) {
2008-12-03 07:49:32 +00:00
$stmt = 'UPDATE yubikeys SET ' .
'accessed=NOW(),' .
'counter=' . $new['session_counter'] . ',' .
2009-03-10 22:01:46 +00:00
'sessionUse=' . $new['session_use'] . ',' .
2008-12-03 07:49:32 +00:00
'low=' . $new['low'] . ',' .
'high=' . $new['high'] .
' WHERE id=' . $keyid;
2008-09-18 23:42:35 +00:00
if (!query($stmt)) {
2008-12-03 07:49:32 +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
2008-12-03 07:49:32 +00:00
addHist(0, $_SERVER['REMOTE_ADDR'], $keyid, $client);
2008-10-10 01:15:22 +00:00
2008-09-18 23:42:35 +00:00
return true;
}
?>