From 871332ecf0603c46c020482553c6420e1c18943f Mon Sep 17 00:00:00 2001 From: Janek Bevendorff Date: Wed, 27 Dec 2017 14:20:28 +0100 Subject: [PATCH] Add warning when using legacy key file formats. --- src/cli/Extract.cpp | 7 +++++++ src/gui/ChangeMasterKeyWidget.cpp | 8 ++++++++ src/gui/DatabaseOpenWidget.cpp | 17 +++++++++++++++++ src/keys/FileKey.cpp | 21 +++++++++++++++++++-- src/keys/FileKey.h | 10 ++++++++++ 5 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/cli/Extract.cpp b/src/cli/Extract.cpp index 54c8a45ee..477f2b0e2 100644 --- a/src/cli/Extract.cpp +++ b/src/cli/Extract.cpp @@ -82,6 +82,13 @@ int Extract::execute(QStringList arguments) return EXIT_FAILURE; } + if (fileKey.type() != FileKey::Hashed) { + errorTextStream << QObject::tr("WARNING: You are using a legacy key file format which may become\n" + "unsupported in the future.\n\n" + "Please consider generating a new key file."); + errorTextStream << endl; + } + compositeKey.addKey(fileKey); } diff --git a/src/gui/ChangeMasterKeyWidget.cpp b/src/gui/ChangeMasterKeyWidget.cpp index ef4b61ef2..28a0991db 100644 --- a/src/gui/ChangeMasterKeyWidget.cpp +++ b/src/gui/ChangeMasterKeyWidget.cpp @@ -162,6 +162,14 @@ void ChangeMasterKeyWidget::generateKey() tr("Failed to set %1 as the Key file:\n%2").arg(fileKeyName, errorMsg), MessageWidget::Error); return; } + if (fileKey.type() != FileKey::Hashed) { + QMessageBox::warning(this, + tr("Legacy key file format"), + tr("You are using a legacy key file format which may become\n" + "unsupported in the future.\n\n" + "Please consider generating a new key file."), + QMessageBox::Ok); + } m_key.addKey(fileKey); } diff --git a/src/gui/DatabaseOpenWidget.cpp b/src/gui/DatabaseOpenWidget.cpp index 451dc597a..3af0c6e8f 100644 --- a/src/gui/DatabaseOpenWidget.cpp +++ b/src/gui/DatabaseOpenWidget.cpp @@ -218,6 +218,23 @@ QSharedPointer DatabaseOpenWidget::databaseKey() MessageWidget::Error); return QSharedPointer(); } + if (key.type() != FileKey::Hashed && !config()->get("Messages/NoLegacyKeyFileWarning").toBool()) { + QMessageBox legacyWarning; + legacyWarning.setWindowTitle(tr("Legacy key file format")); + legacyWarning.setText(tr("You are using a legacy key file format which may become\n" + "unsupported in the future.\n\n" + "Please consider generating a new key file.")); + legacyWarning.setIcon(QMessageBox::Icon::Warning); + legacyWarning.addButton(QMessageBox::Ok); + legacyWarning.setDefaultButton(QMessageBox::Ok); + legacyWarning.setCheckBox(new QCheckBox(tr("Don't show this warning again"))); + + connect(legacyWarning.checkBox(), &QCheckBox::stateChanged, [](int state){ + config()->set("Messages/NoLegacyKeyFileWarning", state == Qt::CheckState::Checked); + }); + + legacyWarning.exec(); + } masterKey->addKey(key); lastKeyFiles[m_filename] = keyFilename; } else { diff --git a/src/keys/FileKey.cpp b/src/keys/FileKey.cpp index 7f2f8a6ea..26db40d85 100644 --- a/src/keys/FileKey.cpp +++ b/src/keys/FileKey.cpp @@ -45,6 +45,8 @@ */ bool FileKey::load(QIODevice* device) { + m_type = None; + // we may need to read the file multiple times if (device->isSequential()) { return false; @@ -59,6 +61,7 @@ bool FileKey::load(QIODevice* device) return false; } if (loadXml(device)) { + m_type = KeePass2XML; return true; } @@ -66,6 +69,7 @@ bool FileKey::load(QIODevice* device) return false; } if (loadBinary(device)) { + m_type = FixedBinary; return true; } @@ -73,15 +77,20 @@ bool FileKey::load(QIODevice* device) return false; } if (loadHex(device)) { + m_type = FixedBinaryHex; return true; } + // if no legacy format was detected, generate SHA-256 hash of key file if (!device->reset()) { return false; } + if (loadHashed(device)) { + m_type = Hashed; + return true; + } - // if no legacy format was detected, generate SHA-256 hash of key file - return loadHashed(device); + return false; } /** @@ -345,3 +354,11 @@ bool FileKey::loadHashed(QIODevice* device) return true; } + +/** + * @return type of loaded key file + */ +FileKey::Type FileKey::type() const +{ + return m_type; +} diff --git a/src/keys/FileKey.h b/src/keys/FileKey.h index af324e530..2aa48909b 100644 --- a/src/keys/FileKey.h +++ b/src/keys/FileKey.h @@ -28,10 +28,19 @@ class QIODevice; class FileKey: public Key { public: + enum Type { + None, + Hashed, + KeePass2XML, + FixedBinary, + FixedBinaryHex + }; + bool load(QIODevice* device); bool load(const QString& fileName, QString* errorMsg = nullptr); QByteArray rawKey() const override; FileKey* clone() const override; + Type type() const; static void create(QIODevice* device, int size = 128); static bool create(const QString& fileName, QString* errorMsg = nullptr, int size = 128); @@ -44,6 +53,7 @@ private: bool loadHashed(QIODevice* device); QByteArray m_key; + Type m_type = None; }; #endif // KEEPASSX_FILEKEY_H