forked from keepassxreboot/keepassxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeePass2Writer.cpp
196 lines (171 loc) · 6.16 KB
/
KeePass2Writer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright (C) 2017 KeePassXC Team <[email protected]>
*
* 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 <QFile>
#include "core/Group.h"
#include "core/Metadata.h"
#include "format/Kdbx3Writer.h"
#include "format/Kdbx4Writer.h"
#include "format/KeePass2Writer.h"
/**
* Write a database to a KDBX file.
*
* @param filename output filename
* @param db source database
* @return true on success
*/
bool KeePass2Writer::writeDatabase(const QString& filename, Database* db)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
raiseError(file.errorString());
return false;
}
return writeDatabase(&file, db);
}
#define VERSION_MAX(a, b) \
a = qMax(a, b); \
if (a >= KeePass2::FILE_VERSION_MAX) { \
return a; \
}
/**
* Get the minimum KDBX version required for writing the database.
*/
quint32 KeePass2Writer::kdbxVersionRequired(Database const* db, bool ignoreCurrent, bool ignoreKdf)
{
quint32 version = KeePass2::FILE_VERSION_3_1;
if (!ignoreCurrent) {
VERSION_MAX(version, db->formatVersion())
}
if (!ignoreKdf && !db->kdf().isNull() && !db->kdf()->uuid().isNull()
&& db->kdf()->uuid() != KeePass2::KDF_AES_KDBX3) {
VERSION_MAX(version, KeePass2::FILE_VERSION_4)
}
if (!db->publicCustomData().isEmpty()) {
VERSION_MAX(version, KeePass2::FILE_VERSION_4)
}
for (const auto* group : db->rootGroup()->groupsRecursive(true)) {
if (group->customData() && !group->customData()->isEmpty()) {
VERSION_MAX(version, KeePass2::FILE_VERSION_4)
}
if (!group->tags().isEmpty()) {
VERSION_MAX(version, KeePass2::FILE_VERSION_4_1)
}
if (group->previousParentGroup()) {
VERSION_MAX(version, KeePass2::FILE_VERSION_4_1)
}
for (const auto* entry : group->entries()) {
if (entry->customData() && !entry->customData()->isEmpty()) {
VERSION_MAX(version, KeePass2::FILE_VERSION_4)
}
if (entry->excludeFromReports()) {
VERSION_MAX(version, KeePass2::FILE_VERSION_4_1)
}
if (entry->previousParentGroup()) {
VERSION_MAX(version, KeePass2::FILE_VERSION_4_1)
}
for (const auto* historyItem : entry->historyItems()) {
if (historyItem->customData() && !historyItem->customData()->isEmpty()) {
VERSION_MAX(version, KeePass2::FILE_VERSION_4)
}
}
}
}
const QList<QUuid> customIconsOrder = db->metadata()->customIconsOrder();
for (const QUuid& uuid : customIconsOrder) {
const auto& icon = db->metadata()->customIcon(uuid);
if (!icon.name.isEmpty() || icon.lastModified.isValid()) {
VERSION_MAX(version, KeePass2::FILE_VERSION_4_1)
}
}
return version;
}
/**
* Write a database to a device in KDBX format.
*
* @param device output device
* @param db source database
* @return true on success
*/
bool KeePass2Writer::writeDatabase(QIODevice* device, Database* db)
{
m_error = false;
m_errorStr.clear();
m_version = kdbxVersionRequired(db);
if (db->kdf()->uuid() == KeePass2::KDF_AES_KDBX3 && m_version >= KeePass2::FILE_VERSION_4) {
// We MUST re-transform the key, because challenge-response hashing has changed in KDBX 4.
// If we forget to re-transform, the database will be saved WITHOUT a challenge-response key component!
auto kdf = KeePass2::uuidToKdf(KeePass2::KDF_AES_KDBX4);
kdf->setRounds(db->kdf()->rounds());
db->changeKdf(kdf);
}
db->setFormatVersion(m_version);
if (db->kdf()->uuid() == KeePass2::KDF_AES_KDBX3) {
Q_ASSERT(m_version <= KeePass2::FILE_VERSION_3_1);
m_writer.reset(new Kdbx3Writer());
} else {
Q_ASSERT(m_version >= KeePass2::FILE_VERSION_4);
m_writer.reset(new Kdbx4Writer());
}
return m_writer->writeDatabase(device, db);
}
void KeePass2Writer::extractDatabase(Database* db, QByteArray& xmlOutput)
{
m_error = false;
m_errorStr.clear();
m_version = kdbxVersionRequired(db);
db->setFormatVersion(m_version);
if (db->kdf()->uuid() == KeePass2::KDF_AES_KDBX3) {
Q_ASSERT(m_version <= KeePass2::FILE_VERSION_3_1);
m_writer.reset(new Kdbx3Writer());
} else {
Q_ASSERT(m_version >= KeePass2::FILE_VERSION_4);
m_writer.reset(new Kdbx4Writer());
}
m_writer->extractDatabase(xmlOutput, db);
}
bool KeePass2Writer::hasError() const
{
return m_error || (m_writer && m_writer->hasError());
}
QString KeePass2Writer::errorString() const
{
return m_writer ? m_writer->errorString() : m_errorStr;
}
/**
* Raise an error. Use in case of an unexpected write error.
*
* @param errorMessage error message
*/
void KeePass2Writer::raiseError(const QString& errorMessage)
{
m_error = true;
m_errorStr = errorMessage;
}
/**
* @return KDBX writer used for writing the output file
*/
QSharedPointer<KdbxWriter> KeePass2Writer::writer() const
{
return QSharedPointer<KdbxWriter>();
}
/**
* @return KDBX version used for writing the output file
*/
quint32 KeePass2Writer::version() const
{
return m_version;
}