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
master
sniperbeamer 17 years ago
parent 1905150642
commit 7c34d2010a
  1. 51
      src/Kdb3Database.cpp
  2. 6
      src/forms/EditEntryDlg.ui
  3. 2
      src/lib/AutoTypeTreeWidget.cpp
  4. 2
      src/lib/AutoTypeTreeWidget.h
  5. 32
      src/lib/GroupView.cpp
  6. 3
      src/lib/GroupView.h
  7. 2
      src/mainwindow.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<StdEntry>& 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;
}

@ -484,14 +484,12 @@
</widget>
<layoutdefault spacing="6" margin="11" />
<tabstops>
<tabstop>Combo_Group</tabstop>
<tabstop>Button_Icons</tabstop>
<tabstop>Edit_Title</tabstop>
<tabstop>Edit_UserName</tabstop>
<tabstop>Edit_URL</tabstop>
<tabstop>Edit_Password</tabstop>
<tabstop>ButtonEchoMode</tabstop>
<tabstop>Edit_Password_w</tabstop>
<tabstop>ButtonEchoMode</tabstop>
<tabstop>ButtonGenPw</tabstop>
<tabstop>Edit_Comment</tabstop>
<tabstop>DateTime_Expire</tabstop>
@ -501,6 +499,8 @@
<tabstop>ButtonOpenAttachment</tabstop>
<tabstop>ButtonSaveAttachment</tabstop>
<tabstop>ButtonDeleteAttachment</tabstop>
<tabstop>Combo_Group</tabstop>
<tabstop>Button_Icons</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources/>

@ -37,4 +37,4 @@ void AutoTypeTreeWidget::keyPressEvent(QKeyEvent* event){
else {
QTreeWidget::keyPressEvent(event);
}
}
}

@ -26,4 +26,4 @@ signals:
private:
void mouseMoveEvent(QMouseEvent*);
void keyPressEvent(QKeyEvent*);
};
};

@ -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);

@ -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;

@ -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;