2008-09-26 05:21:11 +02:00
|
|
|
<?php
|
2009-03-11 02:42:23 +01:00
|
|
|
|
2008-09-26 05:21:11 +02:00
|
|
|
define('S_OK', 'OK');
|
|
|
|
define('S_BAD_OTP', 'BAD_OTP');
|
|
|
|
define('S_REPLAYED_OTP', 'REPLAYED_OTP');
|
2009-03-11 01:19:59 +01:00
|
|
|
define('S_DELAYED_OTP', 'DELAYED_OTP');
|
2008-09-26 05:21:11 +02:00
|
|
|
define('S_BAD_SIGNATURE', 'BAD_SIGNATURE');
|
|
|
|
define('S_MISSING_PARAMETER', 'MISSING_PARAMETER');
|
2009-03-10 23:01:46 +01:00
|
|
|
define('S_NO_SUCH_CLIENT', 'NO_SUCH_CLIENT');
|
2008-09-26 05:21:11 +02:00
|
|
|
define('S_OPERATION_NOT_ALLOWED', 'OPERATION_NOT_ALLOWED');
|
|
|
|
define('S_BACKEND_ERROR', 'BACKEND_ERROR');
|
2009-03-11 02:42:23 +01:00
|
|
|
|
|
|
|
define('TS_SEC', 1/8);
|
2009-03-11 01:26:57 +01:00
|
|
|
define('TS_REL_TOLERANCE', 0.3);
|
|
|
|
define('TS_ABS_TOLERANCE', 20);
|
2008-09-26 05:21:11 +02:00
|
|
|
|
2009-03-11 02:42:23 +01:00
|
|
|
define('DEVICE_ID_LEN', 12);
|
2009-03-10 23:50:35 +01:00
|
|
|
|
|
|
|
function unescape($s) {
|
|
|
|
return str_replace('\\', "", $s);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHttpVal($key, $defaultVal) {
|
|
|
|
$val = $defaultVal;
|
|
|
|
if (array_key_exists($key, $_GET)) {
|
|
|
|
$val = $_GET[$key];
|
|
|
|
} else if (array_key_exists($key, $_POST)) {
|
|
|
|
$val = $_POST[$key];
|
|
|
|
}
|
|
|
|
$v = unescape(trim($val));
|
|
|
|
return $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
function query($q) {
|
|
|
|
global $conn;
|
2009-03-11 01:19:59 +01:00
|
|
|
debug('Query: '.$q);
|
2009-03-10 23:50:35 +01:00
|
|
|
$result = mysql_query($q, $conn);
|
|
|
|
if (!$result) {
|
|
|
|
$err = "Invalid query -- $q -- ";
|
|
|
|
writeLog($err);
|
|
|
|
die($err . mysql_error());
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function mysql_quote($value) {
|
|
|
|
return "'" . mysql_real_escape_string($value) . "'";
|
|
|
|
}
|
|
|
|
|
2009-03-11 01:54:19 +01:00
|
|
|
function debug($msg) {
|
|
|
|
if (is_array($msg)) {
|
|
|
|
$str = "";
|
|
|
|
foreach($msg as $key => $value){
|
|
|
|
$str .= "$key=$value ";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$str = $msg;
|
|
|
|
}
|
|
|
|
error_log($str);
|
2008-09-26 05:21:11 +02:00
|
|
|
}
|
|
|
|
|
2009-03-10 20:01:07 +01:00
|
|
|
// Return eg. 2008-11-21T06:11:55Z0711
|
2008-11-21 07:41:13 +01:00
|
|
|
//
|
2008-09-27 11:04:49 +02:00
|
|
|
function getUTCTimeStamp() {
|
|
|
|
date_default_timezone_set('UTC');
|
2009-03-10 20:01:07 +01:00
|
|
|
$tiny = substr(microtime(false), 2, 3);
|
|
|
|
return date('Y-m-d\TH:i:s\Z0', time()) . $tiny;
|
2008-09-27 11:04:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sign a http query string in the array of key-value pairs
|
|
|
|
// return b64 encoded hmac hash
|
2008-10-08 08:29:19 +02:00
|
|
|
function sign($a, $apiKey, $debug=false) {
|
2008-09-27 11:04:49 +02:00
|
|
|
ksort($a);
|
|
|
|
$qs = '';
|
|
|
|
$n = count($a);
|
|
|
|
$i = 0;
|
|
|
|
foreach (array_keys($a) as $key) {
|
2008-11-21 22:41:26 +01:00
|
|
|
$qs .= trim($key).'='.trim($a[$key]);
|
2008-09-27 11:04:49 +02:00
|
|
|
if (++$i < $n) {
|
|
|
|
$qs .= '&';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the TRUE at the end states we want the raw value, not hexadecimal form
|
|
|
|
$hmac = hash_hmac('sha1', utf8_encode($qs), $apiKey, true);
|
|
|
|
$hmac = base64_encode($hmac);
|
2009-03-11 02:53:38 +01:00
|
|
|
|
|
|
|
debug('SIGN: ' . $qs . ' H=' . $hmac);
|
|
|
|
|
2008-09-27 11:04:49 +02:00
|
|
|
return $hmac;
|
|
|
|
|
2008-10-08 08:29:19 +02:00
|
|
|
} // sign an array of query string
|
2008-09-27 11:04:49 +02:00
|
|
|
|
2009-03-11 02:37:07 +01:00
|
|
|
function hex2b64 ($hex_str) {
|
|
|
|
$bin = pack("H*", $hex_str);
|
|
|
|
return base64_encode($bin);
|
2009-03-10 23:50:35 +01:00
|
|
|
}
|
|
|
|
|
2009-03-11 02:37:07 +01:00
|
|
|
function modhex2b64 ($modhex_str) {
|
|
|
|
$hex_str = strtr ($modhex_str, "cbdefghijklnrtuv", "0123456789abcdef");
|
|
|
|
return hex2b64($hex_str);
|
2009-03-10 23:50:35 +01:00
|
|
|
}
|
|
|
|
|
2009-03-11 02:42:23 +01:00
|
|
|
// $otp: A yubikey OTP
|
|
|
|
function decryptOTP($otp, $base_url) {
|
|
|
|
$url = $base_url . $otp;
|
2009-03-11 02:37:07 +01:00
|
|
|
$ch = curl_init($url);
|
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, "YK-VAL");
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
$response = curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
debug($response);
|
|
|
|
|
|
|
|
if (sscanf ($response,
|
|
|
|
"OK counter=%04x high=%02x low=%04x use=%02x",
|
|
|
|
$ret["session_counter"], $ret["high"],
|
|
|
|
$ret["low"], $ret["session_use"]) != 4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
} // End decryptOTP
|
2009-03-10 23:50:35 +01:00
|
|
|
|
|
|
|
// $devId: The first 12 chars from the OTP
|
|
|
|
function getAuthData($devId) {
|
2009-03-11 02:37:07 +01:00
|
|
|
$tokenId = modhex2b64($devId);
|
|
|
|
$stmt = 'SELECT id, client_id, active, counter, '.
|
2009-03-11 01:54:19 +01:00
|
|
|
'sessionUse, low, high, accessed FROM yubikeys WHERE active '.
|
|
|
|
'AND tokenId='.mysql_quote($tokenId);
|
2009-03-10 23:50:35 +01:00
|
|
|
$r = query($stmt);
|
|
|
|
if (mysql_num_rows($r) > 0) {
|
|
|
|
$row = mysql_fetch_assoc($r);
|
|
|
|
mysql_free_result($r);
|
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} // End getAuthData
|
|
|
|
|
|
|
|
// $clientId: The decimal client identity
|
|
|
|
function getClientData($clientId) {
|
2009-03-11 02:53:38 +01:00
|
|
|
$stmt = 'SELECT id, secret, chk_sig, chk_owner, chk_time'.
|
2009-03-11 01:54:19 +01:00
|
|
|
' FROM clients WHERE active AND id='.mysql_quote($clientId);
|
2009-03-10 23:50:35 +01:00
|
|
|
$r = query($stmt);
|
|
|
|
if (mysql_num_rows($r) > 0) {
|
|
|
|
$row = mysql_fetch_assoc($r);
|
|
|
|
mysql_free_result($r);
|
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} // End getClientData
|
2008-09-26 05:21:11 +02:00
|
|
|
?>
|