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
|
// SPDX-FileCopyrightText: 2019 Alexey Andreyev <aa13q@ya.ru>
// SPDX-FileCopyrightText: 2019 Kitsune Ral <Kitsune-Ral@users.sf.net>
// SPDX-License-Identifier: LGPL-2.1-or-later
#ifdef Quotient_E2EE_ENABLED
#include "encryptionmanager.h"
#include "connection.h"
#include "crypto/e2ee.h"
#include "events/encryptedfile.h"
#include "csapi/keys.h"
#include <QtCore/QHash>
#include <QtCore/QStringBuilder>
#include <QtCore/QCryptographicHash>
#include "crypto/qolmaccount.h"
#include "crypto/qolmsession.h"
#include "crypto/qolmmessage.h"
#include "crypto/qolmerrors.h"
#include "crypto/qolmutils.h"
#include <functional>
#include <memory>
#include <openssl/evp.h>
using namespace Quotient;
using std::move;
class EncryptionManager::Private {
public:
explicit Private()
{
}
~Private() = default;
EncryptionManager* q;
// A map from senderKey to InboundSession
UnorderedMap<QString, QOlmSessionPtr> sessions;
void updateDeviceKeys(
const QHash<QString,
QHash<QString, QueryKeysJob::DeviceInformation>>& deviceKeys)
{
for (auto userId : deviceKeys.keys()) {
for (auto deviceId : deviceKeys.value(userId).keys()) {
auto info = deviceKeys.value(userId).value(deviceId);
// TODO: ed25519Verify, etc
}
}
}
void loadSessions() {
QFile file { static_cast<Connection *>(q->parent())->e2eeDataDir() % "/olmsessions.json" };
if(!file.exists() || !file.open(QIODevice::ReadOnly)) {
qCDebug(E2EE) << "No sessions cache exists.";
return;
}
auto data = file.readAll();
const auto json = data.startsWith('{')
? QJsonDocument::fromJson(data).object()
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
: QCborValue::fromCbor(data).toJsonValue().toObject()
#else
: QJsonDocument::fromBinaryData(data).object()
#endif
;
if (json.isEmpty()) {
qCWarning(MAIN) << "Sessions cache is empty";
return;
}
for(const auto &senderKey : json["sessions"].toObject().keys()) {
auto pickle = json["sessions"].toObject()[senderKey].toString();
auto sessionResult = QOlmSession::unpickle(pickle.toLatin1(), static_cast<Connection *>(q->parent())->picklingMode());
if(std::holds_alternative<QOlmError>(sessionResult)) {
qCWarning(E2EE) << "Failed to unpickle olm session";
continue;
}
sessions[senderKey] = std::move(std::get<QOlmSessionPtr>(sessionResult));
}
}
void saveSessions() {
QFile outFile { static_cast<Connection *>(q->parent())->e2eeDataDir() + QStringLiteral("/olmsessions.json") };
if (!outFile.open(QFile::WriteOnly)) {
qCWarning(E2EE) << "Error opening" << outFile.fileName() << ":"
<< outFile.errorString();
qCWarning(E2EE) << "Failed to write olm sessions";
return;
}
QJsonObject rootObj {
{ QStringLiteral("cache_version"),
QJsonObject {
{ QStringLiteral("major"), 1 },
{ QStringLiteral("minor"), 0 } } }
};
{
QJsonObject sessionsJson;
for (const auto &session : sessions) {
auto pickleResult = session.second->pickle(static_cast<Connection *>(q->parent())->picklingMode());
if(std::holds_alternative<QOlmError>(pickleResult)) {
qCWarning(E2EE) << "Failed to pickle session";
continue;
}
sessionsJson[session.first] = QString(std::get<QByteArray>(pickleResult));
}
rootObj.insert(QStringLiteral("sessions"), sessionsJson);
}
const auto data = QJsonDocument(rootObj).toJson(QJsonDocument::Compact);
outFile.write(data.data(), data.size());
qCDebug(E2EE) << "Sessions saved to" << outFile.fileName();
}
QString sessionDecryptPrekey(const QOlmMessage& message, const QString &senderKey, std::unique_ptr<QOlmAccount>& olmAccount)
{
Q_ASSERT(message.type() == QOlmMessage::PreKey);
for(auto& session : sessions) {
const auto matches = session.second->matchesInboundSessionFrom(senderKey, message);
if(std::holds_alternative<bool>(matches) && std::get<bool>(matches)) {
qCDebug(E2EE) << "Found inbound session";
const auto result = session.second->decrypt(message);
saveSessions();
if(std::holds_alternative<QString>(result)) {
return std::get<QString>(result);
} else {
qCDebug(E2EE) << "Failed to decrypt prekey message";
return {};
}
}
}
qCDebug(E2EE) << "Creating new inbound session";
auto newSessionResult = olmAccount->createInboundSessionFrom(senderKey.toUtf8(), message);
if(std::holds_alternative<QOlmError>(newSessionResult)) {
qCWarning(E2EE) << "Failed to create inbound session for" << senderKey << std::get<QOlmError>(newSessionResult);
return {};
}
auto newSession = std::move(std::get<QOlmSessionPtr>(newSessionResult));
auto error = olmAccount->removeOneTimeKeys(newSession);
if (error) {
qWarning(E2EE) << "Failed to remove one time key for session" << newSession->sessionId();
}
const auto result = newSession->decrypt(message);
sessions[senderKey] = std::move(newSession);
saveSessions();
if(std::holds_alternative<QString>(result)) {
return std::get<QString>(result);
} else {
qCDebug(E2EE) << "Failed to decrypt prekey message with new session";
return {};
}
}
QString sessionDecryptGeneral(const QOlmMessage& message, const QString &senderKey)
{
Q_ASSERT(message.type() == QOlmMessage::General);
for(auto& session : sessions) {
const auto result = session.second->decrypt(message);
if(std::holds_alternative<QString>(result)) {
saveSessions();
return std::get<QString>(result);
}
}
qCWarning(E2EE) << "Failed to decrypt message";
return {};
}
};
EncryptionManager::EncryptionManager(QObject* parent)
: QObject(parent)
, d(std::make_unique<Private>())
{
d->q = this;
d->loadSessions();
}
EncryptionManager::~EncryptionManager() = default;
QString EncryptionManager::sessionDecryptMessage(
const QJsonObject& personalCipherObject, const QByteArray& senderKey, std::unique_ptr<QOlmAccount>& account)
{
QString decrypted;
int type = personalCipherObject.value(TypeKeyL).toInt(-1);
QByteArray body = personalCipherObject.value(BodyKeyL).toString().toLatin1();
if (type == 0) {
QOlmMessage preKeyMessage(body, QOlmMessage::PreKey);
decrypted = d->sessionDecryptPrekey(preKeyMessage, senderKey, account);
} else if (type == 1) {
QOlmMessage message(body, QOlmMessage::General);
decrypted = d->sessionDecryptGeneral(message, senderKey);
}
return decrypted;
}
QByteArray EncryptionManager::decryptFile(const QByteArray &ciphertext, EncryptedFile* file)
{
const auto key = QByteArray::fromBase64(file->key.k.replace(QLatin1Char('_'), QLatin1Char('/')).replace(QLatin1Char('-'), QLatin1Char('+')).toLatin1());
const auto iv = QByteArray::fromBase64(file->iv.toLatin1());
const auto sha256 = QByteArray::fromBase64(file->hashes["sha256"].toLatin1());
if(sha256 != QCryptographicHash::hash(ciphertext, QCryptographicHash::Sha256)) {
qCWarning(E2EE) << "Hash verification failed for file";
return QByteArray();
}
QByteArray plaintext(ciphertext.size(), 0);
EVP_CIPHER_CTX *ctx;
int length;
ctx = EVP_CIPHER_CTX_new();
EVP_DecryptInit_ex(ctx, EVP_aes_256_ctr(), NULL, (const unsigned char *)key.data(), (const unsigned char *)iv.data());
EVP_DecryptUpdate(ctx, (unsigned char *)plaintext.data(), &length, (const unsigned char *)ciphertext.data(), ciphertext.size());
EVP_DecryptFinal_ex(ctx, (unsigned char *)plaintext.data() + length, &length);
EVP_CIPHER_CTX_free(ctx);
return plaintext;
}
#endif // Quotient_E2EE_ENABLED
|