2010-08-07 09:10:44 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Database.h"
|
|
|
|
|
|
|
|
#include <QtCore/QFile>
|
|
|
|
#include <QtCore/QXmlStreamReader>
|
|
|
|
|
2010-08-13 12:08:06 -04:00
|
|
|
#include "Metadata.h"
|
2011-06-29 10:39:39 -04:00
|
|
|
#include "crypto/Random.h"
|
2011-06-29 10:47:05 -04:00
|
|
|
#include "format/KeePass2.h"
|
2010-08-07 09:10:44 -04:00
|
|
|
|
2010-08-13 12:08:06 -04:00
|
|
|
Database::Database()
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
2010-08-13 12:08:06 -04:00
|
|
|
m_metadata = new Metadata(this);
|
2011-06-29 10:47:05 -04:00
|
|
|
m_rootGroup = 0;
|
|
|
|
|
|
|
|
m_cipher = KeePass2::CIPHER_AES;
|
2011-07-07 06:52:30 -04:00
|
|
|
m_compressionAlgo = CompressionGZip;
|
|
|
|
m_transformRounds = 50000;
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
Group* Database::rootGroup()
|
|
|
|
{
|
|
|
|
return m_rootGroup;
|
|
|
|
}
|
|
|
|
|
2010-08-15 09:03:47 -04:00
|
|
|
const Group* Database::rootGroup() const
|
|
|
|
{
|
|
|
|
return m_rootGroup;
|
|
|
|
}
|
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
void Database::setRootGroup(Group* group)
|
|
|
|
{
|
2010-08-15 06:31:48 -04:00
|
|
|
Q_ASSERT(group == 0 || group->database() == this);
|
2010-08-12 15:38:59 -04:00
|
|
|
m_rootGroup = group;
|
|
|
|
}
|
|
|
|
|
|
|
|
Metadata* Database::metadata()
|
|
|
|
{
|
|
|
|
return m_metadata;
|
|
|
|
}
|
|
|
|
|
2010-09-19 15:22:24 -04:00
|
|
|
const Metadata* Database::metadata() const
|
2010-08-12 15:38:59 -04:00
|
|
|
{
|
2010-09-19 15:22:24 -04:00
|
|
|
return m_metadata;
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Entry* Database::resolveEntry(const Uuid& uuid)
|
|
|
|
{
|
|
|
|
return recFindEntry(uuid, m_rootGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry* Database::recFindEntry(const Uuid& uuid, Group* group)
|
|
|
|
{
|
|
|
|
Q_FOREACH (Entry* entry, group->entries()) {
|
|
|
|
if (entry->uuid() == uuid)
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_FOREACH (Group* child, group->children()) {
|
|
|
|
Entry* result = recFindEntry(uuid, child);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Group* Database::resolveGroup(const Uuid& uuid)
|
|
|
|
{
|
|
|
|
return recFindGroup(uuid, m_rootGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
Group* Database::recFindGroup(const Uuid& uuid, Group* group)
|
|
|
|
{
|
|
|
|
if (group->uuid() == uuid)
|
|
|
|
return group;
|
|
|
|
|
|
|
|
Q_FOREACH (Group* child, group->children()) {
|
2010-09-13 17:16:28 -04:00
|
|
|
Group* result = recFindGroup(uuid, child);
|
|
|
|
if (result)
|
|
|
|
return result;
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-08-25 07:52:59 -04:00
|
|
|
|
|
|
|
QList<DeletedObject> Database::deletedObjects()
|
|
|
|
{
|
|
|
|
return m_deletedObjects;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database::addDeletedObject(const DeletedObject& delObj)
|
|
|
|
{
|
|
|
|
m_deletedObjects.append(delObj);
|
|
|
|
}
|
2010-09-25 06:41:00 -04:00
|
|
|
|
|
|
|
Uuid Database::cipher() const
|
|
|
|
{
|
|
|
|
return m_cipher;
|
|
|
|
}
|
|
|
|
|
|
|
|
Database::CompressionAlgorithm Database::compressionAlgo() const
|
|
|
|
{
|
|
|
|
return m_compressionAlgo;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Database::transformSeed() const
|
|
|
|
{
|
|
|
|
return m_transformSeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 Database::transformRounds() const
|
|
|
|
{
|
|
|
|
return m_transformRounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Database::transformedMasterKey() const
|
|
|
|
{
|
|
|
|
return m_transformedMasterKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database::setCipher(const Uuid& cipher)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!cipher.isNull());
|
|
|
|
|
|
|
|
m_cipher = cipher;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database::setCompressionAlgo(Database::CompressionAlgorithm algo)
|
|
|
|
{
|
|
|
|
Q_ASSERT(static_cast<quint32>(algo) <= CompressionAlgorithmMax);
|
|
|
|
|
|
|
|
m_compressionAlgo = algo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database::setTransformRounds(quint64 rounds)
|
|
|
|
{
|
|
|
|
m_transformRounds = rounds;
|
|
|
|
}
|
|
|
|
|
2011-06-29 10:39:39 -04:00
|
|
|
void Database::setKey(const CompositeKey& key, const QByteArray& transformSeed)
|
2010-09-25 06:41:00 -04:00
|
|
|
{
|
2011-06-29 10:39:39 -04:00
|
|
|
m_transformSeed = transformSeed;
|
|
|
|
m_transformedMasterKey = key.transform(transformSeed, transformRounds());
|
|
|
|
}
|
2010-09-25 06:41:00 -04:00
|
|
|
|
2011-06-29 10:39:39 -04:00
|
|
|
void Database::setKey(const CompositeKey& key)
|
|
|
|
{
|
|
|
|
setKey(key, Random::randomArray(32));
|
2010-09-25 06:41:00 -04:00
|
|
|
}
|