* @copyright 2010 Yubico * @license http://opensource.org/licenses/bsd-license.php New BSD License * @link http://www.yubico.com/ * @link http://code.google.com/p/yubikey-val-server-php/ */ class OtpGen { public function __construct($db_dsn, $db_username, $db_password, $db_options, $name='ykval-otpgen', $yk_publicname) { $this->myLog = new Log($name); $this->db=new Db($db_dsn, $db_username, $db_password, $db_options, $name . ':db'); $this->isConnected=$this->db->connect(); // First obtain private ID and AES-key if($yubikey=$this->db->findBy('yubikeys', 'yk_publicname', $yk_publicname, 1)) { $this->yk_internalname=$yubikey['yk_internalname']; $this->yk_aeskey=$yubikey['yk_aeskey']; } else { $this->myLog->log(LOG_WARNING, 'Failed to obtain data for yubikey ' . $yk_publicname); } $this->yk_publicname = $yk_publicname; $this->yk_counter = $this->stepYkCounter(); $this->yk_use = 0; $this->yk_low = rand(0,65535); $this->yk_high = rand(0,255); // Store start time as well so we can step yk_low, yk_high correctly $this->start_time=time(); } public function getOtp() { # TODO. Add the rest of the values to string and execute. ! $execstring=sprintf("ykgenerate %s %s %04x %04x %02x %02x" , $this->yk_aeskey, $this->yk_internalname, $this->yk_counter, $this->yk_low, $this->yk_high, $this->yk_use++); if ($this->yk_use>=256) { $this->yk_use=0; $this->yk_counter=$this->stepYkCounter(); } echo $execstring . "\n"; $otp=system($execstring); return $this->yk_publicname . $otp; } private function stepYkCounter() { if ($this->yk_publicname) { if($yubikey=$this->db->findBy('yubikeys', 'yk_publicname', $this->yk_publicname, 1)) { $new_counter = $yubikey['yk_counter'] + 1; if ($this->db->updateBy('yubikeys', 'yk_publicname', $this->yk_publicname, array('yk_counter'=>$new_counter))) { $this->myLog->log(LOG_NOTICE, "Yubikey " . $this->yk_publicname . " stepped counter value to " . $new_counter); return $new_counter; } else { $this->myLog->log(LOG_WARNING, "Failed to update counter value for yubikey " . $this->yk_publicname); } } else { $this->myLog->log(LOG_WARNING, "Failed to get data for yubikey " . $this->yk_publicname); } } else { $this->myLog->log(LOG_WARNING, "yk_publicname not set up correctly for class ykval-otpgen.php. We shouldn't be here."); } return false; } }