<commit_before><?php
require_once '../yubiphpbase/appinclude.php';
require_once '../yubiphpbase/yubi_lib.php';
require_once 'common.php';

header("content-type: text/plain");

if (!isset ($trace)) {
	$trace = 0;
}

$client = getHttpVal('id', 0);
if ($client <= 0) {
	debug('Client ID is missing');
	sendResp(S_MISSING_PARAMETER, 'id');
	exit;
}

$otp = getHttpVal('otp', '');
if ($otp == '') {
	debug('OTP is missing');
	sendResp(S_MISSING_PARAMETER, 'otp');
	exit;
} else {
	$otp = strtolower($otp);
}

//// Get Yubikey from DB
//
$devId = substr($otp, 0, 12);
$ad = getAuthData($devId);

if ($ad == null) {
	debug('Invalid Yubikey ' . $devId);
	sendResp(S_BAD_OTP, $otp);
	exit;
} else {
	debug($ad);
}

//// Check the client ID - does the client own the Yubikey?
//

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;
}

$k = b64ToModhex($ad['secret']);
//debug('aes key in modhex = '.$k);
$key16 = ModHex :: Decode($k);
//debug('aes key in hex = ['.$key16.'], length = '.strlen($key16));
$apiKey = base64_decode($ad['c_secret']);

//// Check signature
//
if ($ad['chk_sig']) {
	// Create the signature using the API key
	$a = array ();
	$a['id'] = $client;
	$a['otp'] = $otp;
	$hmac = sign($a, $apiKey);

	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;
		}
}

//// Decode OTP from input
//
debug('From the OTP validation request:');
$decoded_token = Yubikey :: Decode($otp, $key16);
debug($decoded_token);
if (!is_array($decoded_token)) {
	sendResp(S_BAD_OTP, $otp);
	exit;
}

//// Sanity check key status
//
if ($ad['active'] < 1) {
	sendResp(S_BAD_OTP, 'Suspended');
	exit;
}

//// Sanity check client status
//
if ($ad['c_active'] < 1) {
	sendResp(S_BAD_CLIENT);
	exit;
}

//// Sanity check token ID
//
if (strlen($decoded_token["public_id"]) == 12) {
	debug("Token ID OK (" . $decoded_token["public_id"] . ")");
} else {
	debug("TOKEN ID FAILED, " . $decoded_token["public_id"]);
	sendResp(S_BAD_OTP, $otp);
	exit;
}

//// Sanity check the OTP
//
if (strlen($decoded_token["token"]) != 32) {
	debug("Wrong OTP length," . strlen($decoded_token["token"]));
	sendResp(S_BAD_OTP, $otp);
	exit;
}

//// Check the session counter
//
$sessionCounter = $decoded_token["session_counter"]; // From the req
$seenSessionCounter = $ad['counter']; // From DB
$scDiff = $seenSessionCounter - $sessionCounter;
if ($scDiff > 0) {
	debug("Replayed session counter=" . $sessionCounter . ', seen=' . $seenSessionCounter);
	sendResp(S_REPLAYED_OTP);
	exit;
} else {
	debug("Session counter OK (" . $sessionCounter . ")");
}

//// Check the time stamp
//
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 {
		updDB($ad['id'], $decoded_token, $client);
		$tsDelta = $tsDiff * TS_SEC;
		debug("Timestamp OK (" . $ts . ") delta count=" . $tsDiff .
		'-> delta secs=' . $tsDelta);
	}

	//// Check the real time
	//
	
	if ($ad['chk_time']) {
		$lastTime = strtotime($ad['accessed']);
		//$lastAccess = $ad['accessed'];
		//echo 'Last accessed: '.$lastAccess.' '.date("F j, Y, g:i a", $lastTime)."\n";
		$elapsed = time() - $lastTime;
		debug('Elapsed time from last validation: ' . $elapsed . ' secs');
		$deviation = abs($elapsed - $tsDelta);
		$percent = truncate($deviation/$elapsed, 8) . '%';
		debug("Key time deviation vs. elapsed time=".$deviation.' secs ('.
			$percent.')');
		if ($deviation > TS_TOLERANCE * $elapsed) {
			debug("Is the OTP generated from a real crypto key?");
			sendResp(S_SECURITY_ERROR);
			exit;
		}
	}
} // End check time stamp

//// 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.")");
//}
<commit_msg>anti otp phishing<commit_after><?php
require_once '../yubiphpbase/appinclude.php';
require_once '../yubiphpbase/yubi_lib.php';
require_once 'common.php';

header("content-type: text/plain");

if (!isset ($trace)) {
	$trace = 0;
}

$client = getHttpVal('id', 0);
if ($client <= 0) {
	debug('Client ID is missing');
	sendResp(S_MISSING_PARAMETER, 'id');
	exit;
}

$otp = getHttpVal('otp', '');
if ($otp == '') {
	debug('OTP is missing');
	sendResp(S_MISSING_PARAMETER, 'otp');
	exit;
} else {
	$otp = strtolower($otp);
}

//// Get Yubikey from DB
//
$devId = substr($otp, 0, 12);
$ad = getAuthData($devId);

if ($ad == null) {
	debug('Invalid Yubikey ' . $devId);
	sendResp(S_BAD_OTP, $otp);
	exit;
} else {
	debug($ad);
}

//// Check the client ID - does the client own the Yubikey?
//

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;
}

$k = b64ToModhex($ad['secret']);
//debug('aes key in modhex = '.$k);
$key16 = ModHex :: Decode($k);
//debug('aes key in hex = ['.$key16.'], length = '.strlen($key16));
$apiKey = base64_decode($ad['c_secret']);

//// Check signature
//
if ($ad['chk_sig']) {
	// Create the signature using the API key
	$a = array ();
	$a['id'] = $client;
	$a['otp'] = $otp;
	$hmac = sign($a, $apiKey);

	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;
		}
}

//// Decode OTP from input
//
debug('From the OTP validation request:');
$decoded_token = Yubikey :: Decode($otp, $key16);
debug($decoded_token);
if (!is_array($decoded_token)) {
	sendResp(S_BAD_OTP, $otp);
	exit;
}

//// Sanity check key status
//
if ($ad['active'] < 1) {
	sendResp(S_BAD_OTP, 'Suspended');
	exit;
}

//// Sanity check client status
//
if ($ad['c_active'] < 1) {
	sendResp(S_BAD_CLIENT);
	exit;
}

//// Sanity check token ID
//
if (strlen($decoded_token["public_id"]) == 12) {
	debug("Token ID OK (" . $decoded_token["public_id"] . ")");
} else {
	debug("TOKEN ID FAILED, " . $decoded_token["public_id"]);
	sendResp(S_BAD_OTP, $otp);
	exit;
}

//// Sanity check the OTP
//
if (strlen($decoded_token["token"]) != 32) {
	debug("Wrong OTP length," . strlen($decoded_token["token"]));
	sendResp(S_BAD_OTP, $otp);
	exit;
}

//// Check the session counter
//
$sessionCounter = $decoded_token["session_counter"]; // From the req
$seenSessionCounter = $ad['counter']; // From DB
$scDiff = $seenSessionCounter - $sessionCounter;
if ($scDiff > 0) {
	debug("Replayed session counter=" . $sessionCounter . ', seen=' . $seenSessionCounter);
	sendResp(S_REPLAYED_OTP);
	exit;
} else {
	debug("Session counter OK (" . $sessionCounter . ")");
}

//// Check the time stamp
//
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 {
		updDB($ad['id'], $decoded_token, $client);
		$tsDelta = $tsDiff * TS_SEC;
		debug("Timestamp OK (" . $ts . ") delta count=" . $tsDiff .
		'-> delta secs=' . $tsDelta);
	}

	//// Check the real time
	//
	
	if ($ad['chk_time']) {
		$lastTime = strtotime($ad['accessed']);
		//$lastAccess = $ad['accessed'];
		//echo 'Last accessed: '.$lastAccess.' '.date("F j, Y, g:i a", $lastTime)."\n";
		$elapsed = time() - $lastTime;
		debug('Elapsed time from last validation: ' . $elapsed . ' secs');
		$deviation = abs($elapsed - $tsDelta);
		$percent = truncate(100*$deviation/$elapsed, 8) . '%';
		debug("Key time deviation vs. elapsed time=".$deviation.' secs ('.
			$percent.')');
		if ($deviation > TS_TOLERANCE * $elapsed) {
			debug("Is the OTP generated from a real crypto key?");
			sendResp(S_SECURITY_ERROR);
			exit;
		}
	}
} // End check time stamp

//// 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.")");
//}

{'fraction_non_alphanumeric': 0.22220946256316032, 'fraction_numerical': 0.007694074414331649, 'mean_word_length': 3.7616183706943684, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5849398', 'n_tokens_mistral': 3576, 'n_tokens_neox': 3413, 'n_words': 798}