1
0
mirror of https://github.com/Yubico/yubikey-val.git synced 2024-12-01 15:24:16 +01:00
yubikey-val/ykval-verify.php

202 lines
4.6 KiB
PHP
Raw Normal View History

2009-03-10 23:20:56 +01:00
<?php
2009-05-04 16:41:18 +02:00
require_once 'ykval-common.php';
require_once 'ykval-config.php';
2008-09-17 19:11:16 +02:00
2009-03-10 23:20:56 +01:00
header("content-type: text/plain");
2008-09-17 19:11:16 +02:00
2009-03-11 01:54:19 +01:00
debug("Request: " . $_SERVER['QUERY_STRING']);
2009-03-10 23:20:56 +01:00
2009-03-11 02:42:23 +01:00
$conn = mysql_connect($baseParams['__DB_HOST__'],
$baseParams['__DB_USER__'],
2009-05-06 16:23:04 +02:00
$baseParams['__DB_PW__']);
if (!$conn) {
sendResp(S_BACKEND_ERROR);
exit;
}
if (!mysql_select_db($baseParams['__DB_NAME__'], $conn)) {
sendResp(S_BACKEND_ERROR);
exit;
}
2009-03-11 02:42:23 +01:00
2009-03-10 23:20:56 +01:00
//// Extract values from HTTP request
//
$h = getHttpVal('h', '');
2009-03-10 23:20:56 +01:00
$client = getHttpVal('id', 0);
if ($client <= 0) {
debug('Client ID is missing');
2009-03-11 01:54:19 +01:00
sendResp(S_MISSING_PARAMETER);
2009-03-10 23:20:56 +01:00
exit;
}
$otp = getHttpVal('otp', '');
if ($otp == '') {
debug('OTP is missing');
2009-03-11 01:54:19 +01:00
sendResp(S_MISSING_PARAMETER);
2009-03-10 23:20:56 +01:00
exit;
}
$otp = strtolower($otp);
//// Get Client info from DB
//
2009-03-11 03:06:02 +01:00
$cd = getClientData($conn, $client);
2009-03-10 23:20:56 +01:00
if ($cd == null) {
debug('Invalid client id ' . $client);
2009-03-11 01:54:19 +01:00
sendResp(S_NO_SUCH_CLIENT);
2009-03-10 23:20:56 +01:00
exit;
}
debug("Client data:", $cd);
2009-03-10 23:20:56 +01:00
//// Check client signature
//
$apiKey = base64_decode($cd['secret']);
2009-03-18 16:05:59 +01:00
if ($h != '') {
2009-03-10 23:20:56 +01:00
// Create the signature using the API key
$a = array ();
$a['id'] = $client;
$a['otp'] = $otp;
$hmac = sign($a, $apiKey);
// Compare it
if ($hmac != $h) {
debug('client hmac=' . $h . ', server hmac=' . $hmac);
2009-03-11 01:54:19 +01:00
sendResp(S_BAD_SIGNATURE);
2009-03-10 23:20:56 +01:00
exit;
}
}
//// Sanity check OTP
//
if (strlen($otp) <= TOKEN_LEN) {
debug('Too short OTP: ' . $otp);
sendResp(S_BAD_OTP);
exit;
}
//// Which YK-KSM should we talk to?
//
$urls = otp2ksmurls ($otp, $client);
if (!is_array($urls)) {
sendResp(S_BACKEND_ERROR);
exit;
}
2009-03-10 23:20:56 +01:00
//// Decode OTP from input
//
$otpinfo = KSMdecryptOTP($urls);
2009-03-11 01:19:59 +01:00
if (!is_array($otpinfo)) {
sendResp(S_BACKEND_ERROR);
2009-03-10 23:20:56 +01:00
exit;
}
debug("Decrypted OTP:", $otpinfo);
2009-03-10 23:20:56 +01:00
2009-03-11 12:02:55 +01:00
//// Get Yubikey from DB
//
2009-03-18 12:00:48 +01:00
$devId = substr($otp, 0, strlen ($otp) - TOKEN_LEN);
2009-03-11 12:02:55 +01:00
$ad = getAuthData($conn, $devId);
if (!is_array($ad)) {
debug('Discovered Yubikey ' . $devId);
addNewKey($conn, $devId);
2009-04-01 18:29:07 +02:00
$ad = getAuthData($conn, $devId);
if (!is_array($ad)) {
debug('Invalid Yubikey ' . $devId);
sendResp(S_BACKEND_ERROR);
2009-04-01 18:29:07 +02:00
exit;
}
2009-03-11 12:02:55 +01:00
}
debug("Auth data:", $ad);
if ($ad['active'] != 1) {
debug('De-activated Yubikey ' . $devId);
sendResp(S_BAD_OTP);
exit;
}
2009-03-11 12:02:55 +01:00
2009-03-10 23:20:56 +01:00
//// Check the session counter
//
2009-03-11 01:19:59 +01:00
$sessionCounter = $otpinfo["session_counter"]; // From the req
2009-03-10 23:20:56 +01:00
$seenSessionCounter = $ad['counter']; // From DB
2009-03-11 01:19:59 +01:00
if ($sessionCounter < $seenSessionCounter) {
debug("Replay, session counter, seen=" . $seenSessionCounter .
" this=" . $sessionCounter);
2009-03-10 23:20:56 +01:00
sendResp(S_REPLAYED_OTP);
exit;
}
//// Check the session use
//
2009-03-11 01:19:59 +01:00
$sessionUse = $otpinfo["session_use"]; // From the req
2009-03-10 23:20:56 +01:00
$seenSessionUse = $ad['sessionUse']; // From DB
2009-03-11 01:19:59 +01:00
if ($sessionCounter == $seenSessionCounter && $sessionUse <= $seenSessionUse) {
debug("Replay, session use, seen=" . $seenSessionUse .
' this=' . $sessionUse);
2009-03-10 23:20:56 +01:00
sendResp(S_REPLAYED_OTP);
exit;
}
2009-03-11 01:54:19 +01:00
//// Valid OTP, update database
//
$stmt = 'UPDATE yubikeys SET accessed=NOW()' .
', counter=' .$otpinfo['session_counter'] .
', sessionUse=' . $otpinfo['session_use'] .
', low=' . $otpinfo['low'] .
', high=' . $otpinfo['high'] .
' WHERE id=' . $ad['id'];
2009-03-11 03:06:02 +01:00
query($conn, $stmt);
2009-03-10 23:20:56 +01:00
2009-03-11 01:19:59 +01:00
//// Check the time stamp
2009-03-10 23:20:56 +01:00
//
2009-03-11 01:19:59 +01:00
if ($sessionCounter == $seenSessionCounter && $sessionUse > $seenSessionUse) {
$ts = ($otpinfo['high'] << 16) + $otpinfo['low'];
2009-03-11 01:19:59 +01:00
$seenTs = ($ad['high'] << 16) + $ad['low'];
$tsDiff = $ts - $seenTs;
$tsDelta = $tsDiff * TS_SEC;
//// Check the real time
//
$lastTime = strtotime($ad['accessed']);
$now = time();
$elapsed = $now - $lastTime;
$deviation = abs($elapsed - $tsDelta);
$percent = $deviation/$elapsed;
2009-03-11 01:19:59 +01:00
debug("Timestamp seen=" . $seenTs . " this=" . $ts .
" delta=" . $tsDiff . ' secs=' . $tsDelta .
' accessed=' . $lastTime .' (' . $ad['accessed'] . ') now='
. $now . ' (' . strftime("%Y-%m-%d %H:%M:%S", $now)
. ') elapsed=' . $elapsed .
' deviation=' . $deviation . ' secs or '.
round(100*$percent) . '%');
if ($deviation > TS_ABS_TOLERANCE && $percent > TS_REL_TOLERANCE) {
2009-03-11 01:19:59 +01:00
debug("OTP failed phishing test");
2009-05-06 15:20:40 +02:00
if (0) {
2009-03-11 01:19:59 +01:00
sendResp(S_DELAYED_OTP);
exit;
}
}
2009-03-10 23:20:56 +01:00
}
2009-03-11 01:19:59 +01:00
sendResp(S_OK);
2009-03-10 23:20:56 +01:00
//////////////////////////
// Functions
//////////////////////////
2009-03-11 01:54:19 +01:00
function sendResp($status) {
global $apiKey;
2009-03-10 23:20:56 +01:00
if ($status == null) {
$status = S_BACKEND_ERROR;
}
$a['status'] = $status;
$a['t'] = getUTCTimeStamp();
$h = sign($a, $apiKey);
echo "h=" . $h . "\r\n";
echo "t=" . ($a['t']) . "\r\n";
echo "status=" . ($a['status']) . "\r\n";
echo "\r\n";
} // End sendResp
2008-09-17 19:11:16 +02:00
?>