Skip to content

Commit

Permalink
Upgrade to KDBX 4 if new 4.1 features are used
Browse files Browse the repository at this point in the history
  • Loading branch information
phoerious committed Nov 22, 2021
1 parent 835e31a commit c872e40
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/format/KeePass2Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <QFile>

#include "core/Group.h"
#include "core/Metadata.h"
#include "format/Kdbx3Writer.h"
#include "format/Kdbx4Writer.h"
#include "format/KeePass2Writer.h"
Expand All @@ -42,7 +43,7 @@ bool KeePass2Writer::writeDatabase(const QString& filename, Database* db)
/**
* @return true if the database should upgrade to KDBX4.
*/
bool KeePass2Writer::implicitUpgradeNeeded(Database const* db) const
bool KeePass2Writer::implicitKDBXUpgradeNeeded(Database const* db)
{
if (db->kdf()->uuid() != KeePass2::KDF_AES_KDBX3) {
return false;
Expand All @@ -56,11 +57,23 @@ bool KeePass2Writer::implicitUpgradeNeeded(Database const* db) const
if (group->customData() && !group->customData()->isEmpty()) {
return true;
}
if (!group->tags().isEmpty()) {
return true;
}
if (group->previousParentGroup()) {
return true;
}

for (const auto& entry : group->entries()) {
if (entry->customData() && !entry->customData()->isEmpty()) {
return true;
}
if (entry->excludeFromReports()) {
return true;
}
if (entry->previousParentGroup()) {
return true;
}

for (const auto& historyItem : entry->historyItems()) {
if (historyItem->customData() && !historyItem->customData()->isEmpty()) {
Expand All @@ -70,6 +83,14 @@ bool KeePass2Writer::implicitUpgradeNeeded(Database const* db) const
}
}

const QList<QUuid> customIconsOrder = db->metadata()->customIconsOrder();
for (const QUuid& uuid : customIconsOrder) {
const auto& icon = db->metadata()->customIcon(uuid);
if (!icon.name.isEmpty() || icon.lastModified.isValid()) {
return true;
}
}

return false;
}

Expand All @@ -85,7 +106,7 @@ bool KeePass2Writer::writeDatabase(QIODevice* device, Database* db)
m_error = false;
m_errorStr.clear();

bool upgradeNeeded = implicitUpgradeNeeded(db);
bool upgradeNeeded = implicitKDBXUpgradeNeeded(db);
if (upgradeNeeded) {
// We MUST re-transform the key, because challenge-response hashing has changed in KDBX 4.
// If we forget to re-transform, the database will be saved WITHOUT a challenge-response key component!
Expand Down
2 changes: 1 addition & 1 deletion src/format/KeePass2Writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class KeePass2Writer
bool writeDatabase(const QString& filename, Database* db);
bool writeDatabase(QIODevice* device, Database* db);
void extractDatabase(Database* db, QByteArray& xmlOutput);
static bool implicitKDBXUpgradeNeeded(Database const* db);

QSharedPointer<KdbxWriter> writer() const;
quint32 version() const;
Expand All @@ -42,7 +43,6 @@ class KeePass2Writer

private:
void raiseError(const QString& errorMessage);
bool implicitUpgradeNeeded(Database const* db) const;

bool m_error = false;
QString m_errorStr = "";
Expand Down

0 comments on commit c872e40

Please sign in to comment.