From 5be497a26c3962b0e271d6551ac58b5e2a6f95e0 Mon Sep 17 00:00:00 2001 From: sniperbeamer Date: Mon, 16 Feb 2009 17:59:51 +0000 Subject: [PATCH] Fix: -min parameter is sometimes ignored (Debian Bug #514414) Fix: Cannot open DB from KeePassX 0.2.2 (Bug #2535569) git-svn-id: https://svn.code.sf.net/p/keepassx/code/trunk@265 b624d157-de02-0410-bad0-e51aec6abb33 --- src/Kdb3Database.cpp | 48 ++++++++++++++++++++++++++++++-------------- src/Kdb3Database.h | 4 +++- src/main.cpp | 4 ++-- src/mainwindow.cpp | 12 +++++------ 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/Kdb3Database.cpp b/src/Kdb3Database.cpp index 768bf45..d451f74 100644 --- a/src/Kdb3Database.cpp +++ b/src/Kdb3Database.cpp @@ -52,7 +52,7 @@ bool Kdb3Database::StdEntryLessThan(const Kdb3Database::StdEntry& This,const Kdb } -Kdb3Database::Kdb3Database() : RawMasterKey(32), RawMasterKey_Latin1(32), MasterKey(32){ +Kdb3Database::Kdb3Database() : RawMasterKey(32), RawMasterKey_Latin1(32), RawMasterKey_UTF8(32), MasterKey(32){ } QString Kdb3Database::getError(){ @@ -591,20 +591,26 @@ bool Kdb3Database::load(QString filename){ SHA256::hashBuffer(buffer+DB_HEADER_SIZE,FinalKey,crypto_size); if(memcmp(ContentsHash, FinalKey, 32) != 0){ - if(PotentialEncodingIssue){ + if(PotentialEncodingIssueLatin1){ delete[] buffer; delete File; File = NULL; - // KeePassX used Latin-1 encoding for passwords until version 0.3.1 - // but KeePass/Win32 uses Windows Codepage 1252. - // Too stay compatible with databases created with KeePassX <= 0.3.1 - // the loading function gives both encodings a try. RawMasterKey.copyData(RawMasterKey_Latin1); - PotentialEncodingIssue=false; + PotentialEncodingIssueLatin1 = false; qDebug("Decryption failed. Retrying with Latin-1."); return load(filename); // second try } + if(PotentialEncodingIssueUTF8){ + delete[] buffer; + delete File; + File = NULL; + + RawMasterKey.copyData(RawMasterKey_UTF8); + PotentialEncodingIssueUTF8 = false; + qDebug("Decryption failed. Retrying with UTF-8."); + return load(filename); // second/third try + } error=tr("Hash test failed.\nThe key is wrong or the file is damaged."); KeyError=true; LOAD_RETURN_CLEANUP @@ -854,28 +860,40 @@ bool Kdb3Database::setKey(const QString& password,const QString& keyfile){ } bool Kdb3Database::setPasswordKey(const QString& Password){ - assert(Password.size()); + Q_ASSERT(Password.size()); QTextCodec* codec=QTextCodec::codecForName("Windows-1252"); QByteArray Password_CP1252 = codec->fromUnicode(Password); RawMasterKey.unlock(); SHA256::hashBuffer(Password_CP1252.data(),*RawMasterKey,Password_CP1252.size()); RawMasterKey.lock(); + QByteArray Password_Latin1 = Password.toLatin1(); - if(Password_Latin1 != Password_CP1252){ + QByteArray Password_UTF8 = Password.toUtf8(); + PotentialEncodingIssueLatin1 = false; + PotentialEncodingIssueUTF8 = false; + + if (Password_Latin1 != Password_CP1252){ // KeePassX used Latin-1 encoding for passwords until version 0.3.1 // but KeePass/Win32 uses Windows Codepage 1252. - // Too stay compatible with databases created with KeePassX <= 0.3.1 + // To stay compatible with databases created with KeePassX <= 0.3.1 // the loading function gives both encodings a try. - PotentialEncodingIssue = true; + PotentialEncodingIssueLatin1 = true; RawMasterKey_Latin1.unlock(); SHA256::hashBuffer(Password_Latin1.data(),*RawMasterKey_Latin1,Password_Latin1.size()); RawMasterKey_Latin1.lock(); } - else { - // If the password does not contain problematic characters we don't need - // to try both encodings. - PotentialEncodingIssue = false; + + if (Password_UTF8 != Password_CP1252){ + // KeePassX used UTF-8 encoding for passwords until version 0.2.2 + // but KeePass/Win32 uses Windows Codepage 1252. + // To stay compatible with databases created with KeePassX <= 0.2.2 + // the loading function gives both encodings a try. + PotentialEncodingIssueUTF8 = true; + RawMasterKey_UTF8.unlock(); + SHA256::hashBuffer(Password_UTF8.data(),*RawMasterKey_UTF8,Password_UTF8.size()); + RawMasterKey_UTF8.lock(); } + return true; } diff --git a/src/Kdb3Database.h b/src/Kdb3Database.h index 82ef9cf..8437b7e 100644 --- a/src/Kdb3Database.h +++ b/src/Kdb3Database.h @@ -234,13 +234,15 @@ private: QFile* File; QString error; bool KeyError; - bool PotentialEncodingIssue; + bool PotentialEncodingIssueLatin1; + bool PotentialEncodingIssueUTF8; QList UnknownMetaStreams; QMap TreeStateMetaStream; unsigned int KeyTransfRounds; CryptAlgorithm Algorithm; SecData RawMasterKey; SecData RawMasterKey_Latin1; + SecData RawMasterKey_UTF8; SecData MasterKey; quint8 TransfRandomSeed[32]; bool hasV4IconMetaStream; diff --git a/src/main.cpp b/src/main.cpp index 337b85c..00e4af2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,7 +52,7 @@ int main(int argc, char **argv) initAppPaths(argc,argv); CmdLineArgs args; if(!args.preparse(argc,argv)){ // searches only for the -cfg parameter - qCritical("%s", CSTR( args.error().append("\n") )); + qCritical("%s\n", CSTR( args.error() )); args.printHelp(); return 1; } @@ -128,7 +128,7 @@ int main(int argc, char **argv) #endif } if ( !args.parse(QApplication::arguments()) ){ - qCritical("%s", CSTR( args.error().append("\n") )); + qCritical("%s\n", CSTR( args.error() )); args.printHelp(); return 1; } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index bb5b203..412b8ce 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -98,11 +98,10 @@ KeepassMainWindow::KeepassMainWindow(const QString& ArgFile,bool ArgMin,bool Arg if (config->lockOnInactivity() && config->lockAfterSec()!=0) inactivityTimer->start(); - bool showWindow=true; + bool showWindow = !ArgMin; FileOpen=false; if(!ArgFile.isEmpty()){ QString f = QDir::cleanPath(QDir::current().absoluteFilePath(ArgFile)); - showWindow = !ArgMin; if (ArgLock) fakeOpenDatabase(f); else @@ -112,8 +111,9 @@ KeepassMainWindow::KeepassMainWindow(const QString& ArgFile,bool ArgMin,bool Arg QFileInfo file(config->lastFile()); if(file.exists()){ QString f = QDir::cleanPath(QDir::current().absoluteFilePath(config->lastFile())); - showWindow = !config->startMinimized(); - if (config->startLocked()) + if (!ArgMin) + showWindow = !config->startMinimized(); + if (ArgLock || config->startLocked()) fakeOpenDatabase(f); else openDatabase(f,true); @@ -121,9 +121,7 @@ KeepassMainWindow::KeepassMainWindow(const QString& ArgFile,bool ArgMin,bool Arg else config->setLastFile(QString()); } - else if (ArgLock){ - showWindow = false; - } + // TODO HelpBrowser /*HelpBrowser = new QAssistantClient(QString(),this); HelpBrowser->setArguments(QStringList()<< "-profile" << "share/keepass/doc/keepassx.adp");*/