From d56ffc34e7a7f2d37443b755aeeeba7da24aa2a6 Mon Sep 17 00:00:00 2001 From: Arne Schroeder Date: Sat, 7 Sep 2019 21:18:25 +0200 Subject: [PATCH] removed debug output, add application log, improve UI --- inc/mainwindow.h | 6 ++-- src/integretychecker.cpp | 2 -- src/mainwindow.cpp | 72 ++++++++++++++++++++++++++++++++-------- 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/inc/mainwindow.h b/inc/mainwindow.h index 13534f8..ee105a9 100644 --- a/inc/mainwindow.h +++ b/inc/mainwindow.h @@ -16,13 +16,15 @@ public: public slots: void onAppFolderSelect_click(); - void onPublicKeySelect_click(); void onSign_click(); void onVerify_click(); + void log(const QString &msg); + private: std::shared_ptr ui; - const QString checkPublicKey(const QString &path); + bool publicKeyAvailable(const QString &path); + bool hashFileAvailable(const QString &path); }; diff --git a/src/integretychecker.cpp b/src/integretychecker.cpp index b1e6667..6799307 100644 --- a/src/integretychecker.cpp +++ b/src/integretychecker.cpp @@ -53,7 +53,6 @@ bool IntegretyCheck::loadKeyFile(const std::string &app) { bool IntegretyCheck::saveKeyFile(const std::string &app) { ///@todo https://github.com/noloader/cryptopp-pem fs::path appPath(app); - std::cout << (appPath / KEY_FILE).string() << std::endl; CryptoPP::FileSink output((appPath / KEY_FILE).c_str()); m_publicKey.DEREncode(output); return true; @@ -157,7 +156,6 @@ void IntegretyCheck::signHashList(std::string &hashList) { signature_length = signer.SignMessage(rng, (const CryptoPP::byte *)hashList.c_str(), hashList.length(), signature); - std::cerr << signature_length << std::endl; signature.resize(signature_length); // transfrom binary signature to base64 encoded string diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 9ab3c8c..2f6caa1 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -20,50 +21,93 @@ AppWindow::AppWindow() : QMainWindow(nullptr), ui(new Ui::MainWindow()) { } void AppWindow::onAppFolderSelect_click() { + ui->btnSign->setEnabled(false); + ui->btnVerify->setEnabled(false); auto basePath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first(); QString dir = QFileDialog::getExistingDirectory( this, tr("Open Directory"), basePath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); ui->txtFolderPath->setText(dir); + log("select AppPath '" + dir + "'"); + ui->btnSign->setEnabled(true); + if (publicKeyAvailable(dir) && hashFileAvailable(dir)) { + ui->btnVerify->setEnabled(true); + log("App is already signed, public key and hashfile exist."); } } void AppWindow::onSign_click() { std::string dir = ui->txtFolderPath->text().toStdString(); IntegretyCheck integretyCheck(dir, true); + log("key pair is generated"); integretyCheck.saveKeyFile(dir); + log("public key is saved to '" + + QString::fromStdString(IntegretyCheck::KEY_FILE) + "'"); + std::string hList = integretyCheck.generateHashList(dir); + log("generated hash list"); + integretyCheck.signHashList(hList); - std::cout << hList << std::endl; + log("signed generated hash list"); + integretyCheck.saveHashList(dir, hList); + log("public key is saved to '" + + QString::fromStdString(IntegretyCheck::HASH_FILE) + "'"); + + if (publicKeyAvailable(ui->txtFolderPath->text()) && + hashFileAvailable(ui->txtFolderPath->text())) { + ui->btnVerify->setEnabled(true); + } + log("Application successful signed!"); } void AppWindow::onVerify_click() { + std::string dir = ui->txtFolderPath->text().toStdString(); IntegretyCheck integretyCheck(dir, false); + ///@todo check for key file and create log msg std::string newHashList, hList; hList = integretyCheck.loadHashList(dir); - std::cout << hList << std::endl; + if (hList.empty()) { + log("App modified, unable to load '" + + QString::fromStdString(IntegretyCheck::HASH_FILE) + "'"); + return; + } + log("loaded hash file"); + if (integretyCheck.verifyHashList(hList, newHashList)) { - if (integretyCheck.checkHashList(newHashList, dir)) { - std::cout << "App verified!" << std::endl; - } else { - std::cerr << "/!\\ App modified, one or more hashes invalid!" - << std::endl; - } + log("signature and data verified"); } else { - std::cerr << "/!\\ App modified, signature invalid!" << std::endl; + log("App modified, signature invalid!"); + return; + } + + if (integretyCheck.checkHashList(newHashList, dir)) { + log("App verified!"); + } else { + log("App modified, one or more hashes invalid!"); + return; } } -const QString AppWindow::checkPublicKey(const QString &path) { +void AppWindow::log(const QString &msg) { + QTime t = QTime::currentTime(); + QString logMsg = t.toString("hh:mm:ss.zzz") + " | " + msg; + ui->listWidget->addItem(logMsg); + ui->listWidget->scrollToBottom(); +} + +bool AppWindow::publicKeyAvailable(const QString &path) { QString pubKeyFile = path + QDir::separator() + QString::fromStdString(IntegretyCheck::KEY_FILE); - if (!QFileInfo::exists(pubKeyFile)) { - pubKeyFile = ""; - } - return pubKeyFile; + return QFileInfo::exists(pubKeyFile); +} + +bool AppWindow::hashFileAvailable(const QString &path) { + QString hashFile = path + QDir::separator() + + QString::fromStdString(IntegretyCheck::HASH_FILE); + return QFileInfo::exists(hashFile); }