0) { debug("Replayed session counter=".$sessionCounter.', seen='.$seenSessionCounter); sendResp(S_REPLAYED_OTP, 'session counter'); exit; } else { debug("Session counter OK (".$sessionCounter.")"); } //// 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, 'hi counter'); exit; } else { debug("Hi counter OK (".$hi.")"); } //// Check the low counter // $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, 'lo counter'); exit; } else { debug("Lo counter OK (".$lo.")"); } //// Update the DB only upon validation success // if (updDB($ad['id'], $decoded_token)) { debug('Validation database updated'); sendResp(S_OK); } else { debug('Failed to update validation database'); sendResp(S_BACKEND_ERROR); } ////////////////////////// // Functions ////////////////////////// function sendResp($status, $info=null) { global $ad; if ($status == null) { $status = S_BACKEND_ERROR; } date_default_timezone_set('UTC'); $timestamp = date('Y-m-d\TH:i:s\ZZ', time()); //// Prepare the response to the user // $respParams = 'status='.$status.'&t='.$timestamp; // Generate the signature debug('API key: '.$ad['c_secret']); // API key of the client $apiKey = base64_decode($ad['c_secret']); debug('Signing: '.$respParams); // the TRUE at the end states we want the raw value, not hexadecimal form $hmac = hash_hmac('sha1', utf8_encode($respParams), $apiKey, true); //outputToFile('hmac', $hmac, "b"); // now take that byte value and base64 encode it $hmac = base64_encode($hmac); debug('h: '.$hmac); echo 'h='.$hmac.PHP_EOL; echo 't='.$timestamp.PHP_EOL; if ($info != null) { echo 'info='.$info.PHP_EOL; } echo 'status='.$status.PHP_EOL.PHP_EOL; } // End sendResp function debug($msg, $exit=false) { global $trace; if ($trace) { if (is_array($msg)) { print_r($msg); } else { echo 'debug> '.$msg; } echo "\n"; } if ($exit) { die ('

Exit

'); } } function updDB($keyid, $new) { $stmt = 'UPDATE yubikeys SET '. 'accessed=NOW(),'. 'counter='.$new['session_counter'].','. 'low='.$new['low'].','. 'high='.$new['high']. ' WHERE id='.$keyid; if (!query($stmt)) { $err = 'Failed to update validation data of key: '.$keyid.' by '.$stmt; debug($err); writeLog($err); return false; } return true; } function outputToFile($outFname, $content, $mode, $append=false) { $out = fopen($outFname, ($append ? "a" : "w")); fwrite($out, $content); fclose($out); } ?>