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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
// SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "testolmaccount.h"
#include "crypto/qolmaccount.h"
#include "connection.h"
#include "events/encryptedfile.h"
using namespace Quotient;
void TestOlmAccount::pickleUnpickledTest()
{
QOlmAccount olmAccount(QStringLiteral("@foo:bar.com"), QStringLiteral("QuotientTestDevice"));
olmAccount.createNewAccount();
auto identityKeys = olmAccount.identityKeys();
auto pickled = std::get<QByteArray>(olmAccount.pickle(Unencrypted{}));
QOlmAccount olmAccount2(QStringLiteral("@foo:bar.com"), QStringLiteral("QuotientTestDevice"));
olmAccount2.unpickle(pickled, Unencrypted{});
auto identityKeys2 = olmAccount2.identityKeys();
QCOMPARE(identityKeys.curve25519, identityKeys2.curve25519);
QCOMPARE(identityKeys.ed25519, identityKeys2.ed25519);
}
void TestOlmAccount::identityKeysValid()
{
QOlmAccount olmAccount(QStringLiteral("@foo:bar.com"), QStringLiteral("QuotientTestDevice"));
olmAccount.createNewAccount();
const auto identityKeys = olmAccount.identityKeys();
const auto curve25519 = identityKeys.curve25519;
const auto ed25519 = identityKeys.ed25519;
// verify encoded keys length
QCOMPARE(curve25519.size(), 43);
QCOMPARE(ed25519.size(), 43);
// encoded as valid base64?
QVERIFY(QByteArray::fromBase64Encoding(curve25519).decodingStatus == QByteArray::Base64DecodingStatus::Ok);
QVERIFY(QByteArray::fromBase64Encoding(ed25519).decodingStatus == QByteArray::Base64DecodingStatus::Ok);
}
void TestOlmAccount::signatureValid()
{
QOlmAccount olmAccount(QStringLiteral("@foo:bar.com"), QStringLiteral("QuotientTestDevice"));
olmAccount.createNewAccount();
const auto message = "Hello world!";
const auto signature = olmAccount.sign(message);
QVERIFY(QByteArray::fromBase64Encoding(signature).decodingStatus == QByteArray::Base64DecodingStatus::Ok);
//let utility = OlmUtility::new();
//let identity_keys = olm_account.parsed_identity_keys();
//let ed25519_key = identity_keys.ed25519();
//assert!(utility
// .ed25519_verify(&ed25519_key, message, &signature)
// .unwrap());
}
void TestOlmAccount::oneTimeKeysValid()
{
QOlmAccount olmAccount(QStringLiteral("@foo:bar.com"), QStringLiteral("QuotientTestDevice"));
olmAccount.createNewAccount();
const auto maxNumberOfOneTimeKeys = olmAccount.maxNumberOfOneTimeKeys();
QCOMPARE(100, maxNumberOfOneTimeKeys);
const auto oneTimeKeysEmpty = olmAccount.oneTimeKeys();
QVERIFY(oneTimeKeysEmpty.curve25519().isEmpty());
olmAccount.generateOneTimeKeys(20);
const auto oneTimeKeysFilled = olmAccount.oneTimeKeys();
QCOMPARE(20, oneTimeKeysFilled.curve25519().count());
}
void TestOlmAccount::deviceKeys()
{
// copied from mtxclient
DeviceKeys device1;
device1.userId = "@alice:example.com";
device1.deviceId = "JLAFKJWSCS";
device1.keys = {{"curve25519:JLAFKJWSCS", "3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI"},
{"ed25519:JLAFKJWSCS", "lEuiRJBit0IG6nUf5pUzWTUEsRVVe/HJkoKuEww9ULI"}};
// TODO that should be the default value
device1.algorithms = QStringList {"m.olm.v1.curve25519-aes-sha2",
"m.megolm.v1.aes-sha2"};
device1.signatures = {
{"@alice:example.com",
{{"ed25519:JLAFKJWSCS",
"dSO80A01XiigH3uBiDVx/EjzaoycHcjq9lfQX0uWsqxl2giMIiSPR8a4d291W1ihKJL/"
"a+myXS367WT6NAIcBA"}}}};
QJsonObject j;
JsonObjectConverter<DeviceKeys>::dumpTo(j, device1);
QJsonDocument doc(j);
QCOMPARE(doc.toJson(QJsonDocument::Compact), "{\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"],"
"\"device_id\":\"JLAFKJWSCS\",\"keys\":{\"curve25519:JLAFKJWSCS\":"
"\"3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI\",\"ed25519:JLAFKJWSCS\":"
"\"lEuiRJBit0IG6nUf5pUzWTUEsRVVe/"
"HJkoKuEww9ULI\"},\"signatures\":{\"@alice:example.com\":{\"ed25519:JLAFKJWSCS\":"
"\"dSO80A01XiigH3uBiDVx/EjzaoycHcjq9lfQX0uWsqxl2giMIiSPR8a4d291W1ihKJL/"
"a+myXS367WT6NAIcBA\"}},\"user_id\":\"@alice:example.com\"}");
auto doc2 = QJsonDocument::fromJson(R"({
"user_id": "@alice:example.com",
"device_id": "JLAFKJWSCS",
"algorithms": [
"m.olm.v1.curve25519-aes-sha2",
"m.megolm.v1.aes-sha2"
],
"keys": {
"curve25519:JLAFKJWSCS": "3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI",
"ed25519:JLAFKJWSCS": "lEuiRJBit0IG6nUf5pUzWTUEsRVVe/HJkoKuEww9ULI"
},
"signatures": {
"@alice:example.com": {
"ed25519:JLAFKJWSCS": "dSO80A01XiigH3uBiDVx/EjzaoycHcjq9lfQX0uWsqxl2giMIiSPR8a4d291W1ihKJL/a+myXS367WT6NAIcBA"
}
},
"unsigned": {
"device_display_name": "Alice's mobile phone"
}
})");
DeviceKeys device2;
JsonObjectConverter<DeviceKeys>::fillFrom(doc2.object(), device2);
QCOMPARE(device2.userId, device1.userId);
QCOMPARE(device2.deviceId, device1.deviceId);
QCOMPARE(device2.keys, device1.keys);
QCOMPARE(device2.algorithms, device1.algorithms);
QCOMPARE(device2.signatures, device1.signatures);
// UnsignedDeviceInfo is missing from the generated DeviceKeys object :(
// QCOMPARE(device2.unsignedInfo.deviceDisplayName, "Alice's mobile phone");
}
void TestOlmAccount::encryptedFile()
{
auto doc = QJsonDocument::fromJson(R"({
"url": "mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe",
"v": "v2",
"key": {
"alg": "A256CTR",
"ext": true,
"k": "aWF6-32KGYaC3A_FEUCk1Bt0JA37zP0wrStgmdCaW-0",
"key_ops": ["encrypt","decrypt"],
"kty": "oct"
},
"iv": "w+sE15fzSc0AAAAAAAAAAA",
"hashes": {
"sha256": "fdSLu/YkRx3Wyh3KQabP3rd6+SFiKg5lsJZQHtkSAYA"
}})");
EncryptedFile file;
JsonObjectConverter<EncryptedFile>::fillFrom(doc.object(), file);
QCOMPARE(file.v, "v2");
QCOMPARE(file.iv, "w+sE15fzSc0AAAAAAAAAAA");
QCOMPARE(file.hashes["sha256"], "fdSLu/YkRx3Wyh3KQabP3rd6+SFiKg5lsJZQHtkSAYA");
QCOMPARE(file.key.alg, "A256CTR");
QCOMPARE(file.key.ext, true);
QCOMPARE(file.key.k, "aWF6-32KGYaC3A_FEUCk1Bt0JA37zP0wrStgmdCaW-0");
QCOMPARE(file.key.keyOps.count(), 2);
QCOMPARE(file.key.kty, "oct");
}
#define CREATE_CONNECTION(VAR, USERNAME, SECRET, DEVICE_NAME) \
auto VAR = std::make_shared<Connection>(); \
(VAR) ->resolveServer("@alice:localhost:" + QString::number(443)); \
connect( (VAR) .get(), &Connection::loginFlowsChanged, this, [this, VAR ] () { \
(VAR) ->loginWithPassword( (USERNAME) , SECRET , DEVICE_NAME , ""); \
}); \
connect( (VAR) .get(), &Connection::networkError, [=](QString error, const QString &, int, int) { \
QFAIL("Network error: make sure synapse is running"); \
}); \
connect( (VAR) .get(), &Connection::loginError, [=](QString error, const QString &) { \
QFAIL("Login failed"); \
}); \
QSignalSpy spy ## VAR ((VAR).get(), &Connection::loginFlowsChanged); \
QSignalSpy spy2 ## VAR ((VAR).get(), &Connection::connected); \
QVERIFY(spy ## VAR .wait(10000)); \
QVERIFY(spy2 ## VAR .wait(10000));
void TestOlmAccount::uploadIdentityKey()
{
CREATE_CONNECTION(conn, "alice", "secret", "AlicePhone")
auto olmAccount = conn->olmAccount();
auto idKeys = olmAccount->identityKeys();
QVERIFY(idKeys.curve25519.size() > 10);
OneTimeKeys unused;
auto request = olmAccount->createUploadKeyRequest(unused);
connect(request, &BaseJob::result, this, [request, conn](BaseJob *job) {
auto job2 = static_cast<UploadKeysJob *>(job);
QCOMPARE(job2->oneTimeKeyCounts().size(), 0);
});
connect(request, &BaseJob::failure, this, [] {
QFAIL("upload failed");
});
conn->run(request);
QSignalSpy spy3(request, &BaseJob::result);
QVERIFY(spy3.wait(10000));
}
void TestOlmAccount::uploadOneTimeKeys()
{
CREATE_CONNECTION(conn, "alice", "secret", "AlicePhone")
auto olmAccount = conn->olmAccount();
auto nKeys = olmAccount->generateOneTimeKeys(5);
QCOMPARE(nKeys, 5);
auto oneTimeKeys = olmAccount->oneTimeKeys();
QHash<QString, QVariant> oneTimeKeysHash;
const auto curve = oneTimeKeys.curve25519();
for (const auto &[keyId, key] : asKeyValueRange(curve)) {
oneTimeKeysHash["curve25519:"+keyId] = key;
}
auto request = new UploadKeysJob(none, oneTimeKeysHash);
connect(request, &BaseJob::result, this, [request, conn](BaseJob *job) {
auto job2 = static_cast<UploadKeysJob *>(job);
QCOMPARE(job2->oneTimeKeyCounts().size(), 1);
QCOMPARE(job2->oneTimeKeyCounts()["curve25519"], 5);
});
connect(request, &BaseJob::failure, this, [] {
QFAIL("upload failed");
});
conn->run(request);
QSignalSpy spy3(request, &BaseJob::result);
QVERIFY(spy3.wait(10000));
}
void TestOlmAccount::uploadSignedOneTimeKeys()
{
CREATE_CONNECTION(conn, "alice", "secret", "AlicePhone")
auto olmAccount = conn->olmAccount();
auto nKeys = olmAccount->generateOneTimeKeys(5);
QCOMPARE(nKeys, 5);
auto oneTimeKeys = olmAccount->oneTimeKeys();
QHash<QString, QVariant> oneTimeKeysHash;
const auto signedKey = olmAccount->signOneTimeKeys(oneTimeKeys);
for (const auto &[keyId, key] : asKeyValueRange(signedKey)) {
QVariant var;
var.setValue(key);
oneTimeKeysHash[keyId] = var;
}
auto request = new UploadKeysJob(none, oneTimeKeysHash);
connect(request, &BaseJob::result, this, [request, nKeys, conn](BaseJob *job) {
auto job2 = static_cast<UploadKeysJob *>(job);
QCOMPARE(job2->oneTimeKeyCounts().size(), 1);
QCOMPARE(job2->oneTimeKeyCounts()["signed_curve25519"], nKeys);
});
connect(request, &BaseJob::failure, this, [] {
QFAIL("upload failed");
});
conn->run(request);
QSignalSpy spy3(request, &BaseJob::result);
QVERIFY(spy3.wait(10000));
}
void TestOlmAccount::uploadKeys()
{
CREATE_CONNECTION(conn, "alice", "secret", "AlicePhone")
auto olmAccount = conn->olmAccount();
auto idks = olmAccount->identityKeys();
olmAccount->generateOneTimeKeys(1);
auto otks = olmAccount->oneTimeKeys();
auto request = olmAccount->createUploadKeyRequest(otks);
connect(request, &BaseJob::result, this, [request, conn](BaseJob *job) {
auto job2 = static_cast<UploadKeysJob *>(job);
QCOMPARE(job2->oneTimeKeyCounts().size(), 1);
QCOMPARE(job2->oneTimeKeyCounts()["signed_curve25519"], 1);
});
connect(request, &BaseJob::failure, this, [] {
QFAIL("upload failed");
});
conn->run(request);
QSignalSpy spy3(request, &BaseJob::result);
QVERIFY(spy3.wait(10000));
}
void TestOlmAccount::claimKeys()
{
CREATE_CONNECTION(alice, "alice", "secret", "AlicePhone")
CREATE_CONNECTION(bob, "alice", "secret", "AlicePhone")
// Bob uploads his keys.
auto *bobOlm = bob->olmAccount();
bobOlm->generateOneTimeKeys(1);
auto request = bobOlm->createUploadKeyRequest(bobOlm->oneTimeKeys());
connect(request, &BaseJob::result, this, [request, bob](BaseJob *job) {
auto job2 = static_cast<UploadKeysJob *>(job);
QCOMPARE(job2->oneTimeKeyCounts().size(), 1);
QCOMPARE(job2->oneTimeKeyCounts()["signed_curve25519"], 1);
});
bob->run(request);
QSignalSpy requestSpy(request, &BaseJob::result);
QVERIFY(requestSpy.wait(10000));
// Alice retrieves bob's keys & claims one signed one-time key.
auto *aliceOlm = alice->olmAccount();
QHash<QString, QStringList> deviceKeys;
deviceKeys[bob->userId()] = QStringList();
auto job = alice->callApi<QueryKeysJob>(deviceKeys);
connect(job, &BaseJob::result, this, [bob, alice, aliceOlm, job, this] {
auto bobDevices = job->deviceKeys()[bob->userId()];
QVERIFY(bobDevices.size() > 0);
// Retrieve the identity key for the current device.
auto bobEd25519 =
bobDevices[bob->deviceId()].keys["ed25519:" + bob->deviceId()];
const auto currentDevice = bobDevices[bob->deviceId()];
// Verify signature.
QVERIFY(verifyIdentitySignature(currentDevice, bob->deviceId(), bob->userId()));
QHash<QString, QHash<QString, QString>> oneTimeKeys;
oneTimeKeys[bob->userId()] = QHash<QString, QString>();
oneTimeKeys[bob->userId()][bob->deviceId()] = SignedCurve25519Key;
auto job = alice->callApi<ClaimKeysJob>(oneTimeKeys);
connect(job, &BaseJob::result, this, [aliceOlm, bob, bobEd25519, job] {
const auto userId = bob->userId();
const auto deviceId = bob->deviceId();
// The device exists.
QCOMPARE(job->oneTimeKeys().size(), 1);
QCOMPARE(job->oneTimeKeys()[userId].size(), 1);
// The key is the one bob sent.
auto oneTimeKey = job->oneTimeKeys()[userId][deviceId];
QVERIFY(oneTimeKey.canConvert<QVariantMap>());
QVariantMap varMap = oneTimeKey.toMap();
bool found = false;
for (const auto key : varMap.keys()) {
if (key.startsWith(QStringLiteral("signed_curve25519"))) {
found = true;
}
}
QVERIFY(found);
//auto algo = oneTimeKey.begin().key();
//auto contents = oneTimeKey.begin().value();
});
});
}
void TestOlmAccount::claimMultipleKeys()
{
// Login with alice multiple times
CREATE_CONNECTION(alice, "alice", "secret", "AlicePhone")
CREATE_CONNECTION(alice1, "alice", "secret", "AlicePhone")
CREATE_CONNECTION(alice2, "alice", "secret", "AlicePhone")
auto olm = alice->olmAccount();
olm->generateOneTimeKeys(10);
auto res = olm->createUploadKeyRequest(olm->oneTimeKeys());
alice->run(res);
connect(res, &BaseJob::result, this, [res] {
QCOMPARE(res->oneTimeKeyCounts().size(), 1);
QCOMPARE(res->oneTimeKeyCounts()["signed_curve25519"], 10);
});
QSignalSpy spy(res, &BaseJob::result);
auto olm1 = alice1->olmAccount();
olm1->generateOneTimeKeys(10);
auto res1 = olm1->createUploadKeyRequest(olm1->oneTimeKeys());
alice1->run(res1);
connect(res1, &BaseJob::result, this, [res1] {
QCOMPARE(res1->oneTimeKeyCounts().size(), 1);
QCOMPARE(res1->oneTimeKeyCounts()["signed_curve25519"], 10);
});
QSignalSpy spy1(res1, &BaseJob::result);
auto olm2 = alice2->olmAccount();
olm2->generateOneTimeKeys(10);
auto res2 = olm2->createUploadKeyRequest(olm2->oneTimeKeys());
alice2->run(res2);
connect(res2, &BaseJob::result, this, [res2] {
QCOMPARE(res2->oneTimeKeyCounts().size(), 1);
QCOMPARE(res2->oneTimeKeyCounts()["signed_curve25519"], 10);
});
QSignalSpy spy2(res2, &BaseJob::result);
QVERIFY(spy.wait(10000));
QVERIFY(spy1.wait(10000));
QVERIFY(spy2.wait(10000));
// Bob will claim all keys from alice
CREATE_CONNECTION(bob, "bob", "secret", "BobPhone")
QStringList devices_;
devices_ << alice->deviceId()
<< alice1->deviceId()
<< alice2->deviceId();
QHash<QString, QHash<QString, QString>> oneTimeKeys;
for (const auto &d : devices_) {
oneTimeKeys[alice->userId()] = QHash<QString, QString>();
oneTimeKeys[alice->userId()][d] = SignedCurve25519Key;
}
auto job = bob->callApi<ClaimKeysJob>(oneTimeKeys);
connect(job, &BaseJob::result, this, [bob, job] {
const auto userId = bob->userId();
const auto deviceId = bob->deviceId();
// The device exists.
QCOMPARE(job->oneTimeKeys().size(), 1);
QCOMPARE(job->oneTimeKeys()[userId].size(), 3);
});
}
QTEST_MAIN(TestOlmAccount)
|