Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 60 handle both firmwares for hotp #64

Merged
merged 5 commits into from
May 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 65 additions & 50 deletions src/ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,11 +929,7 @@ void MainWindow::checkConnection() {

void MainWindow::startTimer() {}

MainWindow::~MainWindow() {
delete validator;

delete ui;
}
MainWindow::~MainWindow() { delete ui; }

void MainWindow::closeEvent(QCloseEvent *event) {
this->hide();
Expand Down Expand Up @@ -1842,22 +1838,40 @@ void MainWindow::generateHOTPConfig(HOTPSlot *slot) {

slot->tokenID[12] = ui->keyboardComboBox->currentIndex() & 0xFF;

bool conversionSuccess = false;
uint64_t counterFromGUI = 0;
if (0 != ui->counterEdit->text().toLatin1().length()) {
counterFromGUI = ui->counterEdit->text().toLatin1().toLongLong(&conversionSuccess);
}

memset(slot->counter, 0, 8);
if (conversionSuccess) {
// FIXME check for little endian/big endian conversion (test on Macintosh)
memcpy(slot->counter, &counterFromGUI, sizeof counterFromGUI);
} else {
csApplet->warningBox(tr("Counter value not copied - there was an error in conversion. Setting to 0. Please retry."));
// Nitrokey Storage needs counter value in text but Pro in binary [#60]
if (cryptostick->activStick20 == false) {
bool conversionSuccess = false;
uint64_t counterFromGUI = 0;
if (0 != ui->counterEdit->text().toLatin1().length()) {
counterFromGUI = ui->counterEdit->text().toLatin1().toULongLong(&conversionSuccess);
}
if (conversionSuccess) {
// FIXME check for little endian/big endian conversion (test on Macintosh)
memcpy(slot->counter, &counterFromGUI, sizeof counterFromGUI);
} else {
csApplet->warningBox(tr("Counter value not copied - there was an error in conversion. "
"Setting counter value to 0. Please retry."));
}
} else { // nitrokey storage version
QByteArray counterFromGUI = QByteArray(ui->counterEdit->text().toLatin1());
int digitsInCounter = counterFromGUI.length();
if (0 < digitsInCounter && digitsInCounter < 8) {
memcpy(slot->counter, counterFromGUI.data(), std::min(counterFromGUI.length(), 7));
// 8th char has to be '\0' since in firmware atoi is used directly on buffer
slot->counter[7] = 0;
} else {
csApplet->warningBox(
tr("Counter value not copied - Nitrokey Storage handles HOTP counter "
"values up to 7 digits. Setting counter value to 0. Please retry."));
}
}
if (DebugingActive) {
if (cryptostick->activStick20)
qDebug() << "HOTP counter value: " << *(char *)slot->counter;
else
qDebug() << "HOTP counter value: " << *(quint64 *)slot->counter;
}
if (DebugingActive)
qDebug() << "HOTP counter value: " << *slot->counter;

slot->config = 0;

if (TRUE == ui->digits8radioButton->isChecked())
Expand Down Expand Up @@ -2022,13 +2036,15 @@ void MainWindow::displayCurrentHotpSlotConfig(uint8_t slotNo) {
ui->base32RadioButton->setChecked(true);
ui->secretEdit->setText(secret); // .toHex());

QByteArray counter((char *)cryptostick->HOTPSlots[slotNo]->counter, 8);

QString TextCount;

TextCount = QString("%1").arg(counter.toInt());
ui->counterEdit->setText(TextCount); // .toHex());

if (cryptostick->activStick20) {
QByteArray counter((char *)cryptostick->HOTPSlots[slotNo]->counter, 8);
QString TextCount;
TextCount = QString("%1").arg(counter.toInt());
ui->counterEdit->setText(TextCount); // .toHex());
} else {
QString TextCount = QString("%1").arg(*(qulonglong *)cryptostick->HOTPSlots[slotNo]->counter);
ui->counterEdit->setText(TextCount);
}
QByteArray omp((char *)cryptostick->HOTPSlots[slotNo]->tokenID, 2);

ui->ompEdit->setText(QString(omp));
Expand Down Expand Up @@ -2138,19 +2154,7 @@ void MainWindow::startConfiguration() {
}
password.clear();
}
} while (QDialog::Accepted == ok && !cryptostick->validPassword); // While
// the
// user
// keeps
// enterning
// a
// pin
// and
// the
// pin
// is
// not
// correct..
} while (QDialog::Accepted == ok && !cryptostick->validPassword);
}

// Start the config dialog
Expand All @@ -2170,6 +2174,9 @@ void MainWindow::startConfiguration() {

QTimer::singleShot(0, this, SLOT(resizeMin()));
}
if (cryptostick->activStick20) {
ui->counterEdit->setMaxLength(7);
}
}

void MainWindow::resizeMin() { resize(minimumSizeHint()); }
Expand Down Expand Up @@ -3121,10 +3128,11 @@ void MainWindow::on_setToZeroButton_clicked() { ui->counterEdit->setText("0"); }

void MainWindow::on_setToRandomButton_clicked() {
quint64 counter;

counter = qrand() & 0xFFFF;
counter *= 16;

counter = qrand();
if (cryptostick->activStick20) {
const int maxDigits = 7;
counter = counter % ((quint64)pow(10, maxDigits));
}
ui->counterEdit->setText(QString(QByteArray::number(counter, 10)));
}

Expand Down Expand Up @@ -3995,13 +4003,20 @@ void MainWindow::on_PWS_ButtonCreatePW_clicked() {
void MainWindow::on_PWS_ButtonEnable_clicked() { PWS_Clicked_EnablePWSAccess(); }

void MainWindow::on_counterEdit_editingFinished() {
uint64_t counterMaxValue;
double counterD = ui->counterEdit->text().toDouble();
counterMaxValue = (1UL << 63) - 1UL;
if (counterMaxValue < counterD) { // FIXME implement proper check is it bigger than long long int
ui->counterEdit->setText(QString("%1").arg(counterMaxValue));
csApplet->warningBox(
tr("Counter must be a value between 0 and 9,223,372,036,854,775,807 (= 2^63 -1)"));
bool conversionSuccess = false;
ui->counterEdit->text().toLatin1().toULongLong(&conversionSuccess);
if (cryptostick->activStick20 == false) {
quint64 counterMaxValue = ULLONG_MAX;
if (!conversionSuccess) {
ui->counterEdit->setText(QString("%1").arg(0));
csApplet->warningBox(tr("Counter must be a value between 0 and %1").arg(counterMaxValue));
}
} else { // for nitrokey storage
if (!conversionSuccess || ui->counterEdit->text().toLatin1().length() > 7) {
ui->counterEdit->setText(QString("%1").arg(0));
csApplet->warningBox(
tr("For Nitrokey Storage counter must be a value between 0 and 9999999"));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "device.h"
#include <QMainWindow>
#include <QSystemTrayIcon>
#include <climits>

#define uint64_t unsigned long long

Expand Down Expand Up @@ -94,7 +95,6 @@ class MainWindow : public QMainWindow {
QMenu *trayMenuHOTPSubMenu;
QMenu *trayMenuSubSpecialConfigure;
QClipboard *clipboard;
QValidator *validator;

unsigned char HOTP_SlotCount;
unsigned char TOTP_SlotCount;
Expand Down