Merge branch '2.0'

This commit is contained in:
Felix Geyer 2016-01-29 17:22:37 +01:00
commit 71d4cb781d
30 changed files with 569 additions and 40 deletions

View file

@ -43,7 +43,7 @@ KeePass2Reader::KeePass2Reader()
{
}
Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& key)
Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& key, bool keepDatabase)
{
QScopedPointer<Database> db(new Database());
m_db = db.data();
@ -178,7 +178,12 @@ Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& ke
if (xmlReader.hasError()) {
raiseError(xmlReader.errorString());
return nullptr;
if (keepDatabase) {
return db.take();
}
else {
return nullptr;
}
}
Q_ASSERT(version < 0x00030001 || !xmlReader.headerHash().isEmpty());
@ -232,6 +237,11 @@ QByteArray KeePass2Reader::xmlData()
return m_xmlData;
}
QByteArray KeePass2Reader::streamKey()
{
return m_protectedStreamKey;
}
void KeePass2Reader::raiseError(const QString& errorMessage)
{
m_error = true;

View file

@ -31,12 +31,13 @@ class KeePass2Reader
public:
KeePass2Reader();
Database* readDatabase(QIODevice* device, const CompositeKey& key);
Database* readDatabase(QIODevice* device, const CompositeKey& key, bool keepDatabase = false);
Database* readDatabase(const QString& filename, const CompositeKey& key);
bool hasError();
QString errorString();
void setSaveXml(bool save);
QByteArray xmlData();
QByteArray streamKey();
private:
void raiseError(const QString& errorMessage);

View file

@ -0,0 +1,107 @@
/*
* Copyright (C) 2016 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 "KeePass2Repair.h"
#include <QBuffer>
#include <QRegExp>
#include "format/KeePass2RandomStream.h"
#include "format/KeePass2Reader.h"
#include "format/KeePass2XmlReader.h"
KeePass2Repair::KeePass2Repair()
: m_db(Q_NULLPTR)
{
}
KeePass2Repair::RepairResult KeePass2Repair::repairDatabase(QIODevice* device, const CompositeKey& key)
{
m_db = Q_NULLPTR;
m_errorStr.clear();
KeePass2Reader reader;
reader.setSaveXml(true);
Database* db = reader.readDatabase(device, key, true);
if (!reader.hasError()) {
delete db;
return NothingTodo;
}
QByteArray xmlData = reader.xmlData();
if (!db || xmlData.isEmpty()) {
delete db;
m_errorStr = reader.errorString();
return UnableToOpen;
}
bool repairAction = false;
QString xmlStart = QString::fromLatin1(xmlData.constData(), qMin(100, xmlData.size()));
QRegExp encodingRegExp("encoding=\"([^\"]+)\"", Qt::CaseInsensitive, QRegExp::RegExp2);
if (encodingRegExp.indexIn(xmlStart) != -1) {
if (encodingRegExp.cap(1).compare("utf-8", Qt::CaseInsensitive) != 0
&& encodingRegExp.cap(1).compare("utf8", Qt::CaseInsensitive) != 0)
{
// database is not utf-8 encoded, we don't support repairing that
delete db;
return RepairFailed;
}
}
// try to fix broken databases because of bug #392
for (int i = (xmlData.size() - 1); i >= 0; i--) {
char ch = xmlData.at(i);
if (ch < 0x20 && ch != 0x09 && ch != 0x0A && ch != 0x0D) {
xmlData.remove(i, 1);
repairAction = true;
}
}
if (!repairAction) {
// we were unable to find the problem
delete db;
return RepairFailed;
}
KeePass2RandomStream randomStream;
randomStream.init(reader.streamKey());
KeePass2XmlReader xmlReader;
QBuffer buffer(&xmlData);
buffer.open(QIODevice::ReadOnly);
xmlReader.readDatabase(&buffer, db, &randomStream);
if (xmlReader.hasError()) {
delete db;
return RepairFailed;
}
else {
m_db = db;
return RepairSuccess;
}
}
Database* KeePass2Repair::database() const
{
return m_db;
}
QString KeePass2Repair::errorString() const
{
return m_errorStr;
}

View file

@ -0,0 +1,50 @@
/*
* Copyright (C) 2016 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/>.
*/
#ifndef KEEPASSX_KEEPASS2REPAIR_H
#define KEEPASSX_KEEPASS2REPAIR_H
#include <QCoreApplication>
#include <QIODevice>
#include "core/Database.h"
#include "keys/CompositeKey.h"
class KeePass2Repair
{
Q_DECLARE_TR_FUNCTIONS(KeePass2Repair)
public:
enum RepairResult
{
NothingTodo,
UnableToOpen,
RepairSuccess,
RepairFailed
};
KeePass2Repair();
RepairResult repairDatabase(QIODevice* device, const CompositeKey& key);
Database* database() const;
QString errorString() const;
private:
Database* m_db;
QString m_errorStr;
};
#endif // KEEPASSX_KEEPASS2REPAIR_H

View file

@ -372,7 +372,7 @@ void KeePass2XmlWriter::writeEntry(const Entry* entry)
}
if (!value.isEmpty()) {
m_xml.writeCharacters(value);
m_xml.writeCharacters(stripInvalidXml10Chars(value));
}
m_xml.writeEndElement();
@ -443,7 +443,7 @@ void KeePass2XmlWriter::writeString(const QString& qualifiedName, const QString&
m_xml.writeEmptyElement(qualifiedName);
}
else {
m_xml.writeTextElement(qualifiedName, string);
m_xml.writeTextElement(qualifiedName, stripInvalidXml10Chars(string));
}
}
@ -547,6 +547,23 @@ QString KeePass2XmlWriter::colorPartToString(int value)
return str;
}
QString KeePass2XmlWriter::stripInvalidXml10Chars(QString str)
{
for (int i = str.size() - 1; i >= 0; i--) {
const ushort uc = str.at(i).unicode();
if ((uc < 0x20 && uc != 0x09 && uc != 0x0A && uc != 0x0D)
|| (uc > 0xD7FF && uc < 0xE000)
|| (uc > 0xFFFD))
{
qWarning("Stripping invalid XML 1.0 codepoint %x", uc);
str.remove(i, 1);
}
}
return str;
}
void KeePass2XmlWriter::raiseError(const QString& errorMessage)
{
m_error = true;

View file

@ -73,6 +73,7 @@ private:
void writeColor(const QString& qualifiedName, const QColor& color);
void writeTriState(const QString& qualifiedName, Group::TriState triState);
QString colorPartToString(int value);
QString stripInvalidXml10Chars(QString str);
void raiseError(const QString& errorMessage);