Add support for parsing KeePass 1 keyfiles.

Refs #2
This commit is contained in:
Felix Geyer 2012-05-10 19:33:16 +02:00
parent cc6f524168
commit 7fd8154ea8
2 changed files with 50 additions and 9 deletions

View File

@ -26,6 +26,7 @@
#include "core/Entry.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "core/Tools.h"
#include "crypto/CryptoHash.h"
#include "format/KeePass1.h"
#include "keys/CompositeKey.h"
@ -314,17 +315,13 @@ bool KeePass1Reader::verifyKey(SymmetricCipherStream* cipherStream)
{
CryptoHash contentHash(CryptoHash::Sha256);
QByteArray buffer;
buffer.resize(16384);
qint64 readResult;
do {
readResult = cipherStream->read(buffer.data(), buffer.size());
if (readResult > 0) {
if (readResult != buffer.size()) {
buffer.resize(readResult);
}
contentHash.addData(buffer);
if (!Tools::readFromDevice(cipherStream, buffer)) {
return false;
}
} while (readResult == buffer.size());
contentHash.addData(buffer);
} while (!buffer.isEmpty());
return contentHash.result() == m_contentHashHeader;
}
@ -791,6 +788,49 @@ bool KeePass1Reader::isMetaStream(const Entry* entry)
&& entry->iconNumber() == 0;
}
QByteArray KeePass1Reader::readKeyfile(QIODevice* device)
{
if (device->size() == 0) {
return QByteArray();
}
if (device->size() == 32) {
QByteArray data = device->read(32);
if (data.size() != 32) {
return QByteArray();
}
return data;
}
if (device->size() == 64) {
QByteArray data = device->read(64);
if (data.size() != 64) {
return QByteArray();
}
if (Tools::isHex(data)) {
return QByteArray::fromHex(data);
}
else {
device->seek(0);
}
}
CryptoHash cryptoHash(CryptoHash::Sha256);
QByteArray buffer;
do {
if (!Tools::readFromDevice(device, buffer)) {
return QByteArray();
}
cryptoHash.addData(buffer);
} while (!buffer.isEmpty());
return cryptoHash.result();
}
QByteArray KeePass1Key::rawKey() const
{

View File

@ -38,6 +38,7 @@ public:
const QByteArray& keyfileData);
Database* readDatabase(const QString& filename, const QString& password,
const QByteArray& keyfileData);
static QByteArray readKeyfile(QIODevice* device);
bool hasError();
QString errorString();