From 7c34d2010a7db4fdf470f0b0993397bb7d05db4e Mon Sep 17 00:00:00 2001 From: sniperbeamer Date: Thu, 17 Apr 2008 22:21:05 +0000 Subject: [PATCH] Create default groups when creating new database Increase and randomize default key transformation number Better cleanup when loading database fails git-svn-id: https://svn.code.sf.net/p/keepassx/code/trunk@196 b624d157-de02-0410-bad0-e51aec6abb33 --- src/Kdb3Database.cpp | 51 +++++++++++++++++++++------------- src/forms/EditEntryDlg.ui | 6 ++-- src/lib/AutoTypeTreeWidget.cpp | 2 +- src/lib/AutoTypeTreeWidget.h | 2 +- src/lib/GroupView.cpp | 32 +++++++++++++++++---- src/lib/GroupView.h | 3 +- src/mainwindow.cpp | 2 ++ 7 files changed, 68 insertions(+), 30 deletions(-) diff --git a/src/Kdb3Database.cpp b/src/Kdb3Database.cpp index b6001cc..b69d92e 100644 --- a/src/Kdb3Database.cpp +++ b/src/Kdb3Database.cpp @@ -414,6 +414,12 @@ void Kdb3Database::restoreGroupTreeState(){ } } +#define LOAD_RETURN_CLEANUP \ + delete File; \ + File = NULL; \ + delete[] buffer; \ + return false; + bool Kdb3Database::load(QString filename){ unsigned long total_size,crypto_size; quint32 Signature1,Signature2,Version,NumGroups,NumEntries,Flags; @@ -422,12 +428,12 @@ quint8 FinalRandomSeed[16]; quint8 ContentsHash[32]; quint8 EncryptionIV[16]; -File=new QFile(filename); +File = new QFile(filename); if(!File->open(QIODevice::ReadWrite)){ if(!File->open(QIODevice::ReadOnly)){ error=tr("Could not open file."); delete File; - File=NULL; + File = NULL; return false; } } @@ -437,7 +443,7 @@ File->read(buffer,total_size); if(total_size < DB_HEADER_SIZE){ error=tr("Unexpected file size (DB_TOTAL_SIZE < DB_HEADER_SIZE)"); - return false; + LOAD_RETURN_CLEANUP } memcpyFromLEnd32(&Signature1,buffer); @@ -454,12 +460,12 @@ memcpyFromLEnd32(&KeyTransfRounds,buffer+120); if((Signature1!=PWM_DBSIG_1) || (Signature2!=PWM_DBSIG_2)){ error=tr("Wrong Signature"); - return false; + LOAD_RETURN_CLEANUP } if((Version & 0xFFFFFF00) != (PWM_DBVER_DW & 0xFFFFFF00)){ error=tr("Unsupported File Version."); - return false; + LOAD_RETURN_CLEANUP } if (Flags & PWM_FLAG_RIJNDAEL) @@ -468,7 +474,7 @@ else if (Flags & PWM_FLAG_TWOFISH) Algorithm = Twofish_Cipher; else{ error=tr("Unknown Encryption Algorithm."); - return false; + LOAD_RETURN_CLEANUP } @@ -490,20 +496,22 @@ if(Algorithm == Rijndael_Cipher){ else if(Algorithm == Twofish_Cipher){ CTwofish twofish; if (twofish.init(FinalKey, 32, EncryptionIV) != true) - return false; + LOAD_RETURN_CLEANUP crypto_size = (unsigned long)twofish.padDecrypt((quint8 *)buffer + DB_HEADER_SIZE, total_size - DB_HEADER_SIZE, (quint8 *)buffer + DB_HEADER_SIZE); } if ((crypto_size > 2147483446) || (!crypto_size && NumGroups)){ error=tr("Decryption failed.\nThe key is wrong or the file is damaged."); - return false; + LOAD_RETURN_CLEANUP } SHA256::hashBuffer(buffer+DB_HEADER_SIZE,FinalKey,crypto_size); if(memcmp(ContentsHash, FinalKey, 32) != 0){ - delete buffer; if(PotentialEncodingIssue){ + 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 @@ -515,7 +523,7 @@ if(memcmp(ContentsHash, FinalKey, 32) != 0){ } error=tr("Hash test failed.\nThe key is wrong or the file is damaged."); KeyError=true; - return false; + LOAD_RETURN_CLEANUP } unsigned long pos = DB_HEADER_SIZE; @@ -537,14 +545,14 @@ for(unsigned long CurGroup = 0; CurGroup < NumGroups; ) pField += 2; pos += 2; if (pos >= total_size){ error=tr("Unexpected error: Offset is out of range.").append(" [G1]"); - return false; + LOAD_RETURN_CLEANUP } memcpyFromLEnd32(&FieldSize, pField); pField += 4; pos += 4; if (pos >= (total_size + FieldSize)){ error=tr("Unexpected error: Offset is out of range.").append(" [G2]"); - return false; + LOAD_RETURN_CLEANUP } bRet = readGroupField(&group,Levels, FieldType, FieldSize, (quint8 *)pField); @@ -556,7 +564,7 @@ for(unsigned long CurGroup = 0; CurGroup < NumGroups; ) pos += FieldSize; if (pos >= total_size){ error=tr("Unexpected error: Offset is out of range.").append(" [G1]"); - return false; + LOAD_RETURN_CLEANUP } } @@ -570,14 +578,14 @@ for (unsigned long CurEntry = 0; CurEntry < NumEntries;) pField += 2; pos += 2; if(pos >= total_size){ error=tr("Unexpected error: Offset is out of range.").append(" [E1]"); - return false; + LOAD_RETURN_CLEANUP } memcpyFromLEnd32(&FieldSize, pField); pField += 4; pos += 4; if (pos >= (total_size + FieldSize)){ error=tr("Unexpected error: Offset is out of range.").append(" [E2]"); - return false; + LOAD_RETURN_CLEANUP } bRet = readEntryField(&entry,FieldType,FieldSize,(quint8*)pField); @@ -593,13 +601,13 @@ for (unsigned long CurEntry = 0; CurEntry < NumEntries;) pos += FieldSize; if (pos >= total_size){ error=tr("Unexpected error: Offset is out of range.").append(" [E3]"); - return false; + LOAD_RETURN_CLEANUP } } if(!createGroupTree(Levels)){ error=tr("Invalid group tree."); - return false; + LOAD_RETURN_CLEANUP } delete [] buffer; @@ -1258,7 +1266,8 @@ bool Kdb3Database::save(){ if(twofish.init(FinalKey, 32, EncryptionIV) == false){ UNEXP_ERROR delete [] buffer; - return false;} + return false; + } EncryptedPartSize = (unsigned long)twofish.padEncrypt((quint8*)buffer+DB_HEADER_SIZE, pos - DB_HEADER_SIZE,(quint8*)buffer+DB_HEADER_SIZE); } @@ -1523,6 +1532,8 @@ void Kdb3Database::serializeEntries(QList& EntryList,char* buffer,unsi } bool Kdb3Database::close(){ + if (File!=NULL) + delete File; return true; } @@ -1532,7 +1543,9 @@ void Kdb3Database::create(){ RootGroup.Parent=NULL; RootGroup.Handle=NULL; Algorithm=Rijndael_Cipher; - KeyTransfRounds=6000; + quint8 ran; + randomize(&ran,1); + KeyTransfRounds=10000 + 3*ran; KeyError=false; } diff --git a/src/forms/EditEntryDlg.ui b/src/forms/EditEntryDlg.ui index 85bc0d8..823efb3 100644 --- a/src/forms/EditEntryDlg.ui +++ b/src/forms/EditEntryDlg.ui @@ -484,14 +484,12 @@ - Combo_Group - Button_Icons Edit_Title Edit_UserName Edit_URL Edit_Password - ButtonEchoMode Edit_Password_w + ButtonEchoMode ButtonGenPw Edit_Comment DateTime_Expire @@ -501,6 +499,8 @@ ButtonOpenAttachment ButtonSaveAttachment ButtonDeleteAttachment + Combo_Group + Button_Icons buttonBox diff --git a/src/lib/AutoTypeTreeWidget.cpp b/src/lib/AutoTypeTreeWidget.cpp index 045c941..73ce463 100644 --- a/src/lib/AutoTypeTreeWidget.cpp +++ b/src/lib/AutoTypeTreeWidget.cpp @@ -37,4 +37,4 @@ void AutoTypeTreeWidget::keyPressEvent(QKeyEvent* event){ else { QTreeWidget::keyPressEvent(event); } -} \ No newline at end of file +} diff --git a/src/lib/AutoTypeTreeWidget.h b/src/lib/AutoTypeTreeWidget.h index 1732435..bf40b93 100644 --- a/src/lib/AutoTypeTreeWidget.h +++ b/src/lib/AutoTypeTreeWidget.h @@ -26,4 +26,4 @@ signals: private: void mouseMoveEvent(QMouseEvent*); void keyPressEvent(QKeyEvent*); -}; \ No newline at end of file +}; diff --git a/src/lib/GroupView.cpp b/src/lib/GroupView.cpp index 7683fe5..62e69fd 100644 --- a/src/lib/GroupView.cpp +++ b/src/lib/GroupView.cpp @@ -108,25 +108,47 @@ void KeepassGroupView::OnNewGroup(){ if(parent){ group=db->addGroup(&NewGroup,parent->GroupHandle); Items.append(new GroupViewItem(parent)); - } + } else{ if(topLevelItemCount()){ if(topLevelItem(topLevelItemCount()-1)==SearchResultItem) - Items.append(new GroupViewItem(this,topLevelItem(topLevelItemCount()-2))); + Items.append(new GroupViewItem(this,topLevelItem(topLevelItemCount()-2))); else - Items.append(new GroupViewItem(this,topLevelItem(topLevelItemCount()-1))); + Items.append(new GroupViewItem(this,topLevelItem(topLevelItemCount()-1))); } else - Items.append(new GroupViewItem(this)); + Items.append(new GroupViewItem(this)); group=db->addGroup(&NewGroup,NULL); } Items.back()->GroupHandle=group; Items.back()->setText(0,group->title()); - Items.back()->setIcon(0,db->icon(group->image())); + Items.back()->setIcon(0,db->icon(group->image())); } emit fileModified(); } +void KeepassGroupView::createGroup(const QString& title, quint32 image){ + CGroup NewGroup; + NewGroup.Title = title; + NewGroup.Image = image; + + IGroupHandle* group; + if(topLevelItemCount()){ + if(topLevelItem(topLevelItemCount()-1)==SearchResultItem) + Items.append(new GroupViewItem(this,topLevelItem(topLevelItemCount()-2))); + else + Items.append(new GroupViewItem(this,topLevelItem(topLevelItemCount()-1))); + } + else + Items.append(new GroupViewItem(this)); + + group = db->addGroup(&NewGroup,NULL); + + Items.back()->GroupHandle = group; + Items.back()->setText(0, group->title()); + Items.back()->setIcon(0, db->icon(group->image())); +} + void KeepassGroupView::OnEditGroup(){ GroupViewItem* item=(GroupViewItem*)currentItem(); CEditGroupDialog dlg(db,item->GroupHandle,parentWidget(),true); diff --git a/src/lib/GroupView.h b/src/lib/GroupView.h index d863294..6b77b15 100644 --- a/src/lib/GroupView.h +++ b/src/lib/GroupView.h @@ -36,6 +36,7 @@ class KeepassGroupView:public QTreeWidget{ void createItems(); void showSearchResults(); void setCurrentGroup(IGroupHandle* group); + void createGroup(const QString& title, quint32 image); private: virtual void dragEnterEvent(QDragEnterEvent* event); @@ -47,7 +48,7 @@ class KeepassGroupView:public QTreeWidget{ virtual void mousePressEvent(QMouseEvent *event); virtual void mouseMoveEvent(QMouseEvent *event); virtual void paintEvent ( QPaintEvent * event ); - virtual void contextMenuEvent(QContextMenuEvent *event); + virtual void contextMenuEvent(QContextMenuEvent *event); void addChilds(GroupViewItem* item); QPoint DragStartPos; GroupViewItem* DragItem; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 1e3c2b1..7d997ca 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -519,6 +519,8 @@ void KeepassMainWindow::OnFileNewKdb(){ setupDatabaseConnections(db); setStateGroupSelected(NONE); setStateEntrySelected(NONE); + GroupView->createGroup("Internet", 1); + GroupView->createGroup("eMail", 19); } else{ delete db_new;