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"
|
|
|
|
|
2013-10-03 09:18:16 -04:00
|
|
|
#include <QFile>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QXmlStreamReader>
|
2010-08-07 09:10:44 -04:00
|
|
|
|
2011-07-08 07:57:02 -04:00
|
|
|
#include "core/Group.h"
|
|
|
|
#include "core/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"
|
2017-03-12 13:47:05 -04:00
|
|
|
#include "format/KeePass2Reader.h"
|
2010-08-07 09:10:44 -04:00
|
|
|
|
2012-04-24 18:12:23 -04:00
|
|
|
QHash<Uuid, Database*> Database::m_uuidMap;
|
|
|
|
|
2010-08-13 12:08:06 -04:00
|
|
|
Database::Database()
|
2012-04-23 13:44:43 -04:00
|
|
|
: m_metadata(new Metadata(this))
|
2012-06-24 11:53:01 -04:00
|
|
|
, m_timer(new QTimer(this))
|
|
|
|
, m_emitModified(false)
|
2012-04-24 18:12:23 -04:00
|
|
|
, m_uuid(Uuid::random())
|
2010-08-07 09:10:44 -04:00
|
|
|
{
|
2013-11-22 04:28:11 -05:00
|
|
|
m_data.cipher = KeePass2::CIPHER_AES;
|
|
|
|
m_data.compressionAlgo = CompressionGZip;
|
2014-04-26 12:34:33 -04:00
|
|
|
m_data.transformRounds = 100000;
|
2013-11-22 04:28:11 -05:00
|
|
|
m_data.hasKey = false;
|
|
|
|
|
2011-07-09 15:54:01 -04:00
|
|
|
setRootGroup(new Group());
|
2011-12-27 09:47:06 -05:00
|
|
|
rootGroup()->setUuid(Uuid::random());
|
2012-06-24 11:53:01 -04:00
|
|
|
m_timer->setSingleShot(true);
|
2011-06-29 10:47:05 -04:00
|
|
|
|
2012-04-24 18:12:23 -04:00
|
|
|
m_uuidMap.insert(m_uuid, this);
|
|
|
|
|
2012-06-24 11:53:01 -04:00
|
|
|
connect(m_metadata, SIGNAL(modified()), this, SIGNAL(modifiedImmediate()));
|
2012-05-14 11:04:05 -04:00
|
|
|
connect(m_metadata, SIGNAL(nameTextChanged()), this, SIGNAL(nameTextChanged()));
|
2012-06-24 11:53:01 -04:00
|
|
|
connect(this, SIGNAL(modifiedImmediate()), this, SLOT(startModifiedTimer()));
|
|
|
|
connect(m_timer, SIGNAL(timeout()), SIGNAL(modified()));
|
2010-08-07 09:10:44 -04:00
|
|
|
}
|
|
|
|
|
2012-04-24 18:12:23 -04:00
|
|
|
Database::~Database()
|
|
|
|
{
|
|
|
|
m_uuidMap.remove(m_uuid);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2011-07-09 15:54:01 -04:00
|
|
|
Q_ASSERT(group);
|
2011-07-08 07:57:02 -04:00
|
|
|
|
2010-08-12 15:38:59 -04:00
|
|
|
m_rootGroup = group;
|
2011-07-09 15:54:01 -04:00
|
|
|
m_rootGroup->setParent(this);
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2016-09-02 13:51:51 -04:00
|
|
|
const QList<Entry*> entryList = group->entries();
|
|
|
|
for (Entry* entry : entryList) {
|
2012-04-18 18:25:57 -04:00
|
|
|
if (entry->uuid() == uuid) {
|
2010-08-12 15:38:59 -04:00
|
|
|
return entry;
|
2012-04-18 18:25:57 -04:00
|
|
|
}
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
|
2016-09-02 13:51:51 -04:00
|
|
|
const QList<Group*> children = group->children();
|
|
|
|
for (Group* child : children) {
|
2010-08-12 15:38:59 -04:00
|
|
|
Entry* result = recFindEntry(uuid, child);
|
2012-04-18 18:25:57 -04:00
|
|
|
if (result) {
|
2010-08-12 15:38:59 -04:00
|
|
|
return result;
|
2012-04-18 18:25:57 -04:00
|
|
|
}
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
|
2015-07-24 12:28:12 -04:00
|
|
|
return nullptr;
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Group* Database::resolveGroup(const Uuid& uuid)
|
|
|
|
{
|
|
|
|
return recFindGroup(uuid, m_rootGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
Group* Database::recFindGroup(const Uuid& uuid, Group* group)
|
|
|
|
{
|
2012-04-18 18:25:57 -04:00
|
|
|
if (group->uuid() == uuid) {
|
2010-08-12 15:38:59 -04:00
|
|
|
return group;
|
2012-04-18 18:25:57 -04:00
|
|
|
}
|
2010-08-12 15:38:59 -04:00
|
|
|
|
2016-09-02 13:51:51 -04:00
|
|
|
const QList<Group*> children = group->children();
|
|
|
|
for (Group* child : children) {
|
2010-09-13 17:16:28 -04:00
|
|
|
Group* result = recFindGroup(uuid, child);
|
2012-04-18 18:25:57 -04:00
|
|
|
if (result) {
|
2010-09-13 17:16:28 -04:00
|
|
|
return result;
|
2012-04-18 18:25:57 -04:00
|
|
|
}
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
|
|
|
|
2015-07-24 12:28:12 -04:00
|
|
|
return nullptr;
|
2010-08-12 15:38:59 -04:00
|
|
|
}
|
2010-08-25 07:52:59 -04:00
|
|
|
|
|
|
|
QList<DeletedObject> Database::deletedObjects()
|
|
|
|
{
|
|
|
|
return m_deletedObjects;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database::addDeletedObject(const DeletedObject& delObj)
|
|
|
|
{
|
2012-05-15 18:04:10 -04:00
|
|
|
Q_ASSERT(delObj.deletionTime.timeSpec() == Qt::UTC);
|
2010-08-25 07:52:59 -04:00
|
|
|
m_deletedObjects.append(delObj);
|
|
|
|
}
|
2010-09-25 06:41:00 -04:00
|
|
|
|
2012-04-21 18:29:39 -04:00
|
|
|
void Database::addDeletedObject(const Uuid& uuid)
|
|
|
|
{
|
|
|
|
DeletedObject delObj;
|
2015-07-22 17:48:08 -04:00
|
|
|
delObj.deletionTime = QDateTime::currentDateTimeUtc();
|
2012-04-21 18:29:39 -04:00
|
|
|
delObj.uuid = uuid;
|
|
|
|
|
|
|
|
addDeletedObject(delObj);
|
|
|
|
}
|
|
|
|
|
2010-09-25 06:41:00 -04:00
|
|
|
Uuid Database::cipher() const
|
|
|
|
{
|
2013-11-22 04:28:11 -05:00
|
|
|
return m_data.cipher;
|
2010-09-25 06:41:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Database::CompressionAlgorithm Database::compressionAlgo() const
|
|
|
|
{
|
2013-11-22 04:28:11 -05:00
|
|
|
return m_data.compressionAlgo;
|
2010-09-25 06:41:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Database::transformSeed() const
|
|
|
|
{
|
2013-11-22 04:28:11 -05:00
|
|
|
return m_data.transformSeed;
|
2010-09-25 06:41:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
quint64 Database::transformRounds() const
|
|
|
|
{
|
2013-11-22 04:28:11 -05:00
|
|
|
return m_data.transformRounds;
|
2010-09-25 06:41:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Database::transformedMasterKey() const
|
|
|
|
{
|
2013-11-22 04:28:11 -05:00
|
|
|
return m_data.transformedMasterKey;
|
2010-09-25 06:41:00 -04:00
|
|
|
}
|
|
|
|
|
2014-09-07 19:37:46 -04:00
|
|
|
QByteArray Database::challengeResponseKey() const
|
2014-05-26 04:40:38 -04:00
|
|
|
{
|
2014-09-07 19:37:46 -04:00
|
|
|
return m_data.challengeResponseKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Database::challengeMasterSeed(const QByteArray& masterSeed)
|
|
|
|
{
|
2014-09-07 19:43:04 -04:00
|
|
|
m_data.masterSeed = masterSeed;
|
2014-09-07 19:37:46 -04:00
|
|
|
return m_data.key.challenge(masterSeed, m_data.challengeResponseKey);
|
2014-05-26 04:40:38 -04:00
|
|
|
}
|
|
|
|
|
2010-09-25 06:41:00 -04:00
|
|
|
void Database::setCipher(const Uuid& cipher)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!cipher.isNull());
|
|
|
|
|
2013-11-22 04:28:11 -05:00
|
|
|
m_data.cipher = cipher;
|
2010-09-25 06:41:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Database::setCompressionAlgo(Database::CompressionAlgorithm algo)
|
|
|
|
{
|
|
|
|
Q_ASSERT(static_cast<quint32>(algo) <= CompressionAlgorithmMax);
|
|
|
|
|
2013-11-22 04:28:11 -05:00
|
|
|
m_data.compressionAlgo = algo;
|
2010-09-25 06:41:00 -04:00
|
|
|
}
|
|
|
|
|
2015-05-09 13:47:53 -04:00
|
|
|
bool Database::setTransformRounds(quint64 rounds)
|
2010-09-25 06:41:00 -04:00
|
|
|
{
|
2013-11-22 04:28:11 -05:00
|
|
|
if (m_data.transformRounds != rounds) {
|
2015-05-09 13:47:53 -04:00
|
|
|
quint64 oldRounds = m_data.transformRounds;
|
|
|
|
|
2013-11-22 04:28:11 -05:00
|
|
|
m_data.transformRounds = rounds;
|
2012-07-17 04:31:00 -04:00
|
|
|
|
2013-11-22 04:28:11 -05:00
|
|
|
if (m_data.hasKey) {
|
2015-05-09 13:47:53 -04:00
|
|
|
if (!setKey(m_data.key)) {
|
|
|
|
m_data.transformRounds = oldRounds;
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-17 04:31:00 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-09 13:47:53 -04:00
|
|
|
|
|
|
|
return true;
|
2010-09-25 06:41:00 -04:00
|
|
|
}
|
|
|
|
|
2015-05-09 13:47:53 -04:00
|
|
|
bool Database::setKey(const CompositeKey& key, const QByteArray& transformSeed,
|
|
|
|
bool updateChangedTime)
|
2010-09-25 06:41:00 -04:00
|
|
|
{
|
2015-05-09 13:47:53 -04:00
|
|
|
bool ok;
|
|
|
|
QString errorString;
|
|
|
|
|
|
|
|
QByteArray transformedMasterKey =
|
|
|
|
key.transform(transformSeed, transformRounds(), &ok, &errorString);
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-22 04:28:11 -05:00
|
|
|
m_data.key = key;
|
|
|
|
m_data.transformSeed = transformSeed;
|
2015-05-09 13:47:53 -04:00
|
|
|
m_data.transformedMasterKey = transformedMasterKey;
|
2013-11-22 04:28:11 -05:00
|
|
|
m_data.hasKey = true;
|
2012-04-11 09:57:11 -04:00
|
|
|
if (updateChangedTime) {
|
2015-07-22 17:48:08 -04:00
|
|
|
m_metadata->setMasterKeyChanged(QDateTime::currentDateTimeUtc());
|
2012-04-11 09:57:11 -04:00
|
|
|
}
|
2017-03-10 09:58:42 -05:00
|
|
|
emit modifiedImmediate();
|
2015-05-09 13:47:53 -04:00
|
|
|
|
|
|
|
return true;
|
2011-06-29 10:39:39 -04:00
|
|
|
}
|
2010-09-25 06:41:00 -04:00
|
|
|
|
2015-05-09 13:47:53 -04:00
|
|
|
bool Database::setKey(const CompositeKey& key)
|
2011-06-29 10:39:39 -04:00
|
|
|
{
|
2015-05-09 13:47:53 -04:00
|
|
|
return setKey(key, randomGen()->randomArray(32));
|
2010-09-25 06:41:00 -04:00
|
|
|
}
|
2012-04-16 15:03:35 -04:00
|
|
|
|
2012-10-12 06:10:41 -04:00
|
|
|
bool Database::hasKey() const
|
2012-04-16 15:03:35 -04:00
|
|
|
{
|
2013-11-22 04:28:11 -05:00
|
|
|
return m_data.hasKey;
|
2012-04-16 15:03:35 -04:00
|
|
|
}
|
2012-04-18 15:59:00 -04:00
|
|
|
|
2012-10-12 06:10:41 -04:00
|
|
|
bool Database::verifyKey(const CompositeKey& key) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(hasKey());
|
|
|
|
|
2014-09-07 19:43:04 -04:00
|
|
|
if (!m_data.challengeResponseKey.isEmpty()) {
|
|
|
|
QByteArray result;
|
|
|
|
|
|
|
|
if (!key.challenge(m_data.masterSeed, result)) {
|
2017-02-23 17:52:36 -05:00
|
|
|
// challenge failed, (YubiKey?) removed?
|
2014-09-07 19:43:04 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_data.challengeResponseKey != result) {
|
2017-02-23 17:52:36 -05:00
|
|
|
// wrong response from challenged device(s)
|
2014-09-07 19:43:04 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-22 04:28:11 -05:00
|
|
|
return (m_data.key.rawKey() == key.rawKey());
|
2012-10-12 06:10:41 -04:00
|
|
|
}
|
|
|
|
|
2012-04-21 13:06:28 -04:00
|
|
|
void Database::createRecycleBin()
|
|
|
|
{
|
2012-07-21 05:57:00 -04:00
|
|
|
Group* recycleBin = Group::createRecycleBin();
|
2012-04-21 13:06:28 -04:00
|
|
|
recycleBin->setParent(rootGroup());
|
|
|
|
m_metadata->setRecycleBin(recycleBin);
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:59:00 -04:00
|
|
|
void Database::recycleEntry(Entry* entry)
|
|
|
|
{
|
|
|
|
if (m_metadata->recycleBinEnabled()) {
|
|
|
|
if (!m_metadata->recycleBin()) {
|
2012-04-21 13:06:28 -04:00
|
|
|
createRecycleBin();
|
2012-04-18 15:59:00 -04:00
|
|
|
}
|
2012-04-18 17:17:29 -04:00
|
|
|
entry->setGroup(metadata()->recycleBin());
|
2012-04-18 15:59:00 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
delete entry;
|
|
|
|
}
|
|
|
|
}
|
2012-04-21 13:06:28 -04:00
|
|
|
|
|
|
|
void Database::recycleGroup(Group* group)
|
|
|
|
{
|
|
|
|
if (m_metadata->recycleBinEnabled()) {
|
|
|
|
if (!m_metadata->recycleBin()) {
|
|
|
|
createRecycleBin();
|
|
|
|
}
|
|
|
|
group->setParent(metadata()->recycleBin());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
delete group;
|
2012-06-24 11:53:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 10:33:06 -04:00
|
|
|
void Database::emptyRecycleBin()
|
|
|
|
{
|
|
|
|
if (m_metadata->recycleBinEnabled() && m_metadata->recycleBin()) {
|
|
|
|
// destroying direct entries of the recycle bin
|
|
|
|
QList<Entry*> subEntries = m_metadata->recycleBin()->entries();
|
|
|
|
for (Entry* entry : subEntries) {
|
|
|
|
delete entry;
|
|
|
|
}
|
|
|
|
// destroying direct subgroups of the recycle bin
|
|
|
|
QList<Group*> subGroups = m_metadata->recycleBin()->children();
|
|
|
|
for (Group* group : subGroups) {
|
|
|
|
delete group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 22:37:42 -05:00
|
|
|
void Database::merge(const Database* other)
|
|
|
|
{
|
|
|
|
m_rootGroup->merge(other->rootGroup());
|
2017-03-10 09:58:42 -05:00
|
|
|
emit modified();
|
2016-11-07 22:37:42 -05:00
|
|
|
}
|
|
|
|
|
2012-06-24 11:53:01 -04:00
|
|
|
void Database::setEmitModified(bool value)
|
|
|
|
{
|
|
|
|
if (m_emitModified && !value) {
|
|
|
|
m_timer->stop();
|
2012-04-21 13:06:28 -04:00
|
|
|
}
|
2012-06-24 11:53:01 -04:00
|
|
|
|
|
|
|
m_emitModified = value;
|
2012-04-21 13:06:28 -04:00
|
|
|
}
|
2012-04-24 18:12:23 -04:00
|
|
|
|
2013-11-22 04:28:11 -05:00
|
|
|
void Database::copyAttributesFrom(const Database* other)
|
|
|
|
{
|
|
|
|
m_data = other->m_data;
|
|
|
|
m_metadata->copyAttributesFrom(other->m_metadata);
|
|
|
|
}
|
|
|
|
|
2012-04-24 18:12:23 -04:00
|
|
|
Uuid Database::uuid()
|
|
|
|
{
|
|
|
|
return m_uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Database* Database::databaseByUuid(const Uuid& uuid)
|
|
|
|
{
|
|
|
|
return m_uuidMap.value(uuid, 0);
|
|
|
|
}
|
2012-06-24 11:53:01 -04:00
|
|
|
|
|
|
|
void Database::startModifiedTimer()
|
|
|
|
{
|
|
|
|
if (!m_emitModified) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_timer->isActive()) {
|
|
|
|
m_timer->stop();
|
|
|
|
}
|
|
|
|
m_timer->start(150);
|
|
|
|
}
|
2014-05-03 10:59:41 -04:00
|
|
|
|
2017-05-19 14:04:11 -04:00
|
|
|
const CompositeKey& Database::key() const
|
2014-05-03 10:59:41 -04:00
|
|
|
{
|
|
|
|
return m_data.key;
|
|
|
|
}
|
|
|
|
|
2017-03-12 13:47:05 -04:00
|
|
|
Database* Database::openDatabaseFile(QString fileName, CompositeKey key)
|
|
|
|
{
|
|
|
|
|
|
|
|
QFile dbFile(fileName);
|
|
|
|
if (!dbFile.exists()) {
|
|
|
|
qCritical("File %s does not exist.", qPrintable(fileName));
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!dbFile.open(QIODevice::ReadOnly)) {
|
|
|
|
qCritical("Unable to open file %s.", qPrintable(fileName));
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeePass2Reader reader;
|
|
|
|
Database* db = reader.readDatabase(&dbFile, key);
|
|
|
|
|
|
|
|
if (reader.hasError()) {
|
|
|
|
qCritical("Error while parsing the database: %s", qPrintable(reader.errorString()));
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return db;
|
|
|
|
|
|
|
|
}
|