7 #include "e2ee/qolmaccount.h"
8 #include "e2ee/qolmsession.h"
10 #include "events/encryptedevent.h"
17 class ConnectionEncryptionData {
19 static QFuture<
bool> setup(Connection* connection,
20 std::unique_ptr<ConnectionEncryptionData>& result,
21 bool clearDatabase =
false);
24 QOlmAccount olmAccount;
26 mutable Database database;
27 std::unordered_map<QByteArray, std::vector<QOlmSession>> olmSessions;
29 QHash<QString, KeyVerificationSession*> verificationSessions{};
30 QSet<QString> trackedUsers{};
31 QSet<QString> outdatedUsers{};
32 QHash<QString, QHash<QString, DeviceKeys>> deviceKeys{};
33 JobHandle<QueryKeysJob> currentQueryKeysJob{};
34 QSet<std::pair<QString, QString>> triedDevices{};
37 bool encryptionUpdateRequired =
false;
38 QHash<QString,
int> oneTimeKeysCount{};
39 std::vector<std::unique_ptr<EncryptedEvent>> pendingEncryptedEvents{};
40 bool isUploadingKeys =
false;
41 bool firstSync =
true;
42 QHash<QString, QHash<QString,
bool>> selfVerifiedDevices;
43 QHash<QString, QHash<QString,
bool>> verifiedDevices;
45 void saveDevicesList();
46 void loadDevicesList();
47 QString curveKeyForUserDevice(
const QString& userId,
48 const QString& device)
const;
49 bool isKnownCurveKey(
const QString& userId,
50 const QString& curveKey)
const;
51 bool hasOlmSession(
const QString &user,
const QString &deviceId)
const;
53 void onSyncSuccess(SyncData &syncResponse);
54 void loadOutdatedUserDevices();
55 void consumeToDeviceEvent(EventPtr toDeviceEvent);
56 void encryptionUpdate(
const QList<QString>& forUsers);
58 bool createOlmSession(
const QString& targetUserId,
59 const QString& targetDeviceId,
60 const OneTimeKeys& oneTimeKeyObject);
61 void saveSession(
const QOlmSession& session,
const QByteArray& senderKey)
63 database.saveOlmSession(senderKey, session,
64 QDateTime::currentDateTime());
66 void saveOlmAccount();
69 std::pair<QByteArray, QByteArray> sessionDecryptMessage(
70 const QJsonObject& personalCipherObject,
71 const QByteArray& senderKey);
72 std::pair<EventPtr, QByteArray> sessionDecryptMessage(
const EncryptedEvent& encryptedEvent);
74 QJsonObject assembleEncryptedContent(
75 QJsonObject payloadJson,
const QString& targetUserId,
76 const QString& targetDeviceId)
const;
77 void sendSessionKeyToDevices(
78 const QString& roomId,
79 const QOlmOutboundGroupSession& outboundSession,
80 const QMultiHash<QString, QString>& devices);
82 template <
typename... ArgTs>
83 KeyVerificationSession* setupKeyVerificationSession(
84 ArgTs&&... sessionArgs)
87 new KeyVerificationSession(std::forward<ArgTs>(sessionArgs)...);
88 qCDebug(E2EE) <<
"Incoming key verification session from" << session->remoteDeviceId();
89 verificationSessions.insert(session->transactionId(), session);
90 QObject::connect(session, &QObject::destroyed, q,
91 [
this, txnId = session->transactionId()] {
92 verificationSessions.remove(txnId);
94 emit q->newKeyVerificationSession(session);
100 ConnectionEncryptionData(Connection* connection,
101 PicklingKey&& picklingKey);
102 bool hasConflictingDeviceIdsAndCrossSigningKeys(
const QString& userId);
104 void handleQueryKeys(
const QueryKeysJob::Response& keys);
106 void handleMasterKeys(
const QHash<QString, CrossSigningKey>& masterKeys);
107 void handleSelfSigningKeys(
const QHash<QString, CrossSigningKey>& selfSigningKeys);
108 void handleUserSigningKeys(
const QHash<QString, CrossSigningKey>& userSigningKeys);
109 void handleDevicesList(
110 const QHash<QString, QHash<QString, QueryKeysJob::DeviceInformation>>& newDeviceKeys);
111 void checkVerifiedMasterKeys(
const QHash<QString, CrossSigningKey>& masterKeys);
114 void consumeDevicesList(
const DevicesList &devicesList);
115 bool processIfVerificationEvent(
const Event& evt,
bool encrypted);
116 void handleEncryptedToDeviceEvent(
const EncryptedEvent& event);
119 std::pair<QOlmMessage::Type, QByteArray> olmEncryptMessage(
120 const QString& userId,
const QString& device,
121 const QByteArray& message)
const;
123 void doSendSessionKeyToDevices(
const QString& roomId,
const QByteArray& sessionId,
124 const QByteArray &sessionKey, uint32_t messageIndex,
125 const QMultiHash<QString, QString>& devices);