1
0
mirror of https://github.com/Yubico/yubikey-ksm.git synced 2024-11-29 00:24:14 +01:00
yubikey-ksm/ykksm-decrypt.php

116 lines
3.7 KiB
PHP
Raw Normal View History

2009-02-24 22:27:28 +01:00
<?php
# Written by Simon Josefsson <simon@josefsson.org>.
# Copyright (c) 2009 Yubico AB
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
include 'ykksm-config.php';
include 'ykksm-utils.php';
openlog("ykksm", LOG_PID, LOG_AUTHPRIV)
or die("ERR Syslog open error\n");
$otp = $_REQUEST["otp"];
if (!$otp) {
syslog(LOG_INFO, "No OTP provided");
die("ERR No OTP provided\n");
}
if (!preg_match("/^([cbdefghijklnrtuv]{0,16})([cbdefghijklnrtuv]{32})$/",
$otp, $matches)) {
syslog(LOG_INFO, "Invalid OTP format: $otp");
die("ERR Invalid OTP format\n");
}
$id = $matches[1];
$modhex_ciphertext = $matches[2];
$dbconn = mysql_connect($dbhost, $dbuser, $dbpasswd);
if (!$dbconn) {
syslog(LOG_ERR, "Database connect error: " . mysql_error());
die("ERR Database error\n");
}
$db_selected = mysql_select_db($dbname);
if (!$db_selected) {
syslog(LOG_ERR, "Database select error: " . mysql_error());
die("ERR Database error\n");
}
$sql = "SELECT aesKey, internalName FROM yubikeys " .
"WHERE publicName = '$id' AND active";
$result = mysql_query($sql);
if (!$result) {
syslog(LOG_ERR, "Database query error: " . mysql_error());
die("ERR Database error\n");
}
if (mysql_num_rows($result) != 1) {
syslog(LOG_INFO, "Unknown yubikey: " . $otp);
die("ERR Unknown yubikey\n");
}
$row = mysql_fetch_assoc($result);
$aesKey = $row['aesKey'];
$internalName = $row['internalName'];
$ciphertext = modhex2hex($modhex_ciphertext);
$plaintext = aes128ecb_decrypt($aesKey, $ciphertext);
syslog(LOG_INFO, "OTP $otp OUT $plaintext")
or die("ERR Log error\n");
if (!crc_is_good($plaintext)) {
syslog(LOG_ERR, "CRC error: $otp");
die("ERR Corrupt OTP\n");
}
if (strcmp(substr($plaintext, 0, 12), $internalName) != 0) {
syslog(LOG_ERR, "Internal name mismatch: $otp");
die("ERR Corrupt OTP\n");;
}
$sql = "UPDATE yubikeys SET accessed = NOW() " .
"WHERE publicName = '$id'";
$result = mysql_query($sql);
if (!$result) {
syslog(LOG_ERR, "Database update error: " . mysql_error());
die("ERR Database error\n");
}
# Mask out interesting fields
$counter = substr($plaintext, 14, 2) . substr($plaintext, 12, 2);
$tstamphi = substr($plaintext, 18, 2) . substr($plaintext, 16, 2);
$tstamplo = substr($plaintext, 20, 2);
$sessionuse = substr($plaintext, 22, 2);
print "OK counter=$counter tstamphi=$tstamphi tstamplo=$tstamplo sessionuse=$sessionuse\n";
mysql_close()
or syslog(LOG_ERR, "Database close error (otp $otp): " . mysql_error());
?>