$value){ $str .= "$key=$value "; } } else { $str = $msg; } error_log($str); } // Return eg. 2008-11-21T06:11:55Z0711 // function getUTCTimeStamp() { date_default_timezone_set('UTC'); $tiny = substr(microtime(false), 2, 3); return date('Y-m-d\TH:i:s\Z0', time()) . $tiny; } // Sign a http query string in the array of key-value pairs // return b64 encoded hmac hash function sign($a, $apiKey) { ksort($a); $qs = ''; $n = count($a); $i = 0; foreach (array_keys($a) as $key) { $qs .= trim($key).'='.trim($a[$key]); 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); debug('SIGN: ' . $qs . ' H=' . $hmac); return $hmac; } // sign an array of query string function hex2b64 ($hex_str) { $bin = pack("H*", $hex_str); return base64_encode($bin); } function modhex2b64 ($modhex_str) { $hex_str = strtr ($modhex_str, "cbdefghijklnrtuv", "0123456789abcdef"); return hex2b64($hex_str); } // $otp: A yubikey OTP function decryptOTP($otp, $base_url) { $url = $base_url . $otp; $ch = curl_init($url); curl_setopt($ch, CURLOPT_USERAGENT, "YK-VAL"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); $response = curl_exec($ch); $error = curl_error ($ch); $errno = curl_errno ($ch); debug("YK-KSM response: $response errno: " . $errno . " error: " . $error); $info = curl_getinfo ($ch); debug($info); curl_close($ch); if (sscanf ($response, "OK counter=%04x low=%04x high=%02x use=%02x", $ret["session_counter"], $ret["low"], $ret["high"], $ret["session_use"]) != 4) { return false; } return $ret; } // End decryptOTP // $devId: The first 12 chars from the OTP function getAuthData($conn, $devId) { $tokenId = modhex2b64($devId); $stmt = 'SELECT id, active, client_id, counter, sessionUse, low, high, accessed '. 'FROM yubikeys '. 'WHERE tokenId='.mysql_quote($tokenId); $r = query($conn, $stmt); if (mysql_num_rows($r) > 0) { $row = mysql_fetch_assoc($r); mysql_free_result($r); return $row; } return null; } // End getAuthData function addNewKey($conn, $devId) { $tokenId = modhex2b64($devId); $stmt = 'INSERT INTO yubikeys (client_id, active, created, tokenId, counter) '. 'VALUES (1, true, NOW(), ' . mysql_quote($tokenId) . ', 0)'; $r = query($conn, $stmt); } // $clientId: The decimal client identity function getClientData($conn, $clientId) { $stmt = 'SELECT id, secret, chk_time '. 'FROM clients '. 'WHERE active AND id='.mysql_quote($clientId); $r = query($conn, $stmt); if (mysql_num_rows($r) > 0) { $row = mysql_fetch_assoc($r); mysql_free_result($r); return $row; } return null; } // End getClientData ?>