Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TestOpVaultReader.cpp « tests - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 15f30f2c9662f4bae34a4e372fc878c2507d15ac (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 *  Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
 *
 *  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 "TestOpVaultReader.h"

#include "config-keepassx-tests.h"
#include "core/Database.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "core/Tools.h"
#include "crypto/Crypto.h"
#include "format/OpVaultReader.h"

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QList>
#include <QPair>
#include <QStringList>
#include <QTest>
#include <QUuid>

QTEST_GUILESS_MAIN(TestOpVaultReader)

QPair<QString, QString>* split1PTextExportKV(QByteArray& line)
{
    const auto eq = line.indexOf('=');
    if (-1 == eq) {
        qWarning() << "Bogus key=value pair: <<" << line << ">>";
        return nullptr;
    }
    auto k = QString::fromUtf8(line.mid(0, eq));
    const auto start = eq + 1;
    auto v = QString::fromUtf8(line.mid(start), (line.size() - 1) - start);
    return new QPair<QString, QString>(k, v);
}

QSharedPointer<QJsonArray> read1PasswordTextExport(QFile& f)
{
    if (!f.open(QIODevice::ReadOnly)) {
        qCritical("Unable to open your text export file for reading");
        return {};
    }

    auto result = QSharedPointer<QJsonArray>::create();
    QJsonObject current;

    while (!f.atEnd()) {
        auto line = f.readLine(1024);

        if (line.size() == 1 and line[0] == '\n') {
            if (!current.isEmpty()) {
                result->append(current);
            }
            current = QJsonObject();
            continue;
        }
        const auto kv = split1PTextExportKV(line);
        if (kv == nullptr) {
            break;
        }
        QString k = kv->first;

        const auto multiLine1 = line.indexOf("=\"\"");
        const auto multiLine2 = line.indexOf("=\"");
        const auto isML1 = -1 != multiLine1;
        const auto isML2 = -1 != multiLine2;
        if (isML1 or isML2) {
            QStringList lines;
            const int skipEQ = isML1 ? (multiLine1 + 3) : (multiLine2 + 2);
            lines.append(QString::fromUtf8(line.mid(skipEQ)));
            while (!f.atEnd()) {
                line = f.readLine(1024);
                const auto endMarker = line.indexOf(isML1 ? "\"\"\n" : "\"\n");
                if (-1 != endMarker) {
                    line[endMarker] = '\n';
                    lines.append(QString::fromUtf8(line.mid(0, endMarker)));
                    break;
                } else {
                    lines.append(QString::fromUtf8(line));
                }
            }
            auto v = lines.join("");
            current[k] = v;
        } else {
            current[k] = kv->second;
        }
        delete kv;
    }
    if (!current.isEmpty()) {
        result->append(current);
    }
    f.close();

    return result;
}

void TestOpVaultReader::initTestCase()
{
    QVERIFY(Crypto::init());

    // https://cache.agilebits.com/security-kb/freddy-2013-12-04.tar.gz
    m_opVaultPath = QString("%1/%2").arg(KEEPASSX_TEST_DATA_DIR, "/freddy-2013-12-04.opvault");
    m_opVaultTextExportPath = QString(m_opVaultPath).replace(".opvault", ".opvault.txt");

    m_password = "freddy";

    QFile testData(m_opVaultTextExportPath);
    auto data = read1PasswordTextExport(testData);
    QVERIFY(data);
    QCOMPARE(data->size(), 27);

    m_categoryMap.insert("001", "Login");
    m_categoryMap.insert("002", "Credit Card");
    m_categoryMap.insert("003", "Secure Note");
    m_categoryMap.insert("004", "Identity");
    m_categoryMap.insert("005", "Password");
    m_categoryMap.insert("099", "Tombstone");
    m_categoryMap.insert("100", "Software License");
    m_categoryMap.insert("101", "Bank Account");
    m_categoryMap.insert("102", "Database");
    m_categoryMap.insert("103", "Driver License");
    m_categoryMap.insert("104", "Outdoor License");
    m_categoryMap.insert("105", "Membership");
    m_categoryMap.insert("106", "Passport");
    m_categoryMap.insert("107", "Rewards");
    m_categoryMap.insert("108", "SSN");
    m_categoryMap.insert("109", "Router");
    m_categoryMap.insert("110", "Server");
    m_categoryMap.insert("111", "Email");
}

void TestOpVaultReader::testReadIntoDatabase()
{
    QDir opVaultDir(m_opVaultPath);

    OpVaultReader reader;
    QScopedPointer<Database> db(reader.readDatabase(opVaultDir, m_password));
    QVERIFY2(!reader.hasError(), qPrintable(reader.errorString()));
    QVERIFY(db);
    QVERIFY(!db->children().isEmpty());

    Group* rootGroup = db->rootGroup();
    QVERIFY(rootGroup);

    QFile testDataFile(m_opVaultTextExportPath);
    auto testData = read1PasswordTextExport(testDataFile);
    QVERIFY(testData);

    QMap<QUuid, QJsonObject> objectsByUuid;
    QMap<QString, QList<QJsonObject>> objectsByCategory;
    for (QJsonArray::const_iterator it = testData->constBegin(); it != testData->constEnd(); ++it) {
        QJsonObject value = (*it).toObject();
        auto cat = value["category"].toString();
        QVERIFY2(m_categoryMap.contains(cat), qPrintable(QString("BOGUS, unmapped category \"%1\"").arg(cat)));

        auto catName = m_categoryMap[cat];
        if (!objectsByCategory.contains(catName)) {
            QList<QJsonObject> theList;
            objectsByCategory[catName] = theList;
        }
        objectsByCategory[catName].append(value);

        QUuid u = Tools::hexToUuid(value["uuid"].toString());
        objectsByUuid[u] = value;
    }
    QCOMPARE(objectsByUuid.size(), 27);

    for (QUuid u : objectsByUuid.keys()) {
        QJsonObject o = objectsByUuid[u];
        const auto e = db->rootGroup()->findEntryByUuid(u);
        QVERIFY2(e, qPrintable(QString("Expected to find UUID %1").arg(u.toString())));

        auto jsonTitle = o["title"].toString();
        QCOMPARE(jsonTitle, e->title());
    }

    for (QString& catName : m_categoryMap.values()) {
        const auto g = rootGroup->findChildByName(catName);
        QVERIFY2(g, qPrintable(QString("Expected to find Group(%1)").arg(catName)));
        for (QJsonObject testEntry : objectsByCategory[catName]) {
            auto uuidStr = testEntry["uuid"].toString();
            auto jsonTitle = testEntry["title"].toString();

            QUuid u = Tools::hexToUuid(uuidStr);
            const auto entry = g->findEntryByUuid(u);
            QVERIFY2(entry, qPrintable(QString("Expected to find Group(%1).entry(%2)").arg(catName).arg(uuidStr)));
            QCOMPARE(entry->title(), jsonTitle);
        }
    }
}

void TestOpVaultReader::testKeyDerivation()
{
    OpVaultReader reader;
    QDir opVaultDir(m_opVaultPath);

    // yes, the reader checks this too, but in our case best to fail early
    QVERIFY(opVaultDir.exists());
    QVERIFY(opVaultDir.isReadable());

    QDir defDir = QDir(opVaultDir);
    defDir.cd("default");
    QFile profileJs(defDir.absoluteFilePath("profile.js"));
    QVERIFY(profileJs.exists());

    auto profileObj = reader.readAndAssertJsonFile(profileJs, "var profile=", ";");

    QByteArray salt = QByteArray::fromBase64(profileObj["salt"].toString().toUtf8());
    unsigned long iter = profileObj["iterations"].toInt();
    const auto derived = reader.deriveKeysFromPassPhrase(salt, m_password, iter);
    QVERIFY(derived);
    QVERIFY(!derived->error);

    QByteArray encHex = derived->encrypt.toHex();
    QByteArray hmacHex = derived->hmac.toHex();
    delete derived;

    QCOMPARE(QString::fromUtf8(encHex),
             QStringLiteral("63b075de858949559d4faa9d348bf10bdaa0e567ad943d7803f2291c9342aaaa"));
    QCOMPARE(QString::fromUtf8(hmacHex),
             QStringLiteral("ff3ab426ce55bf097b252b3f2df1c4ba4312a6960180844d7a625bc0ab40c35e"));
}

void TestOpVaultReader::testBandEntry1()
{
    OpVaultReader reader;
    QByteArray json(R"({"hello": "world"})");
    QJsonDocument doc = QJsonDocument::fromJson(json);
    QJsonObject data;
    QByteArray entryKey;
    QByteArray entryHmacKey;
    QVERIFY(!reader.decryptBandEntry(doc.object(), data, entryKey, entryHmacKey));
}