6#include "e2ee/qolmaccount.h"
7#include "e2ee/qolmsession.h"
9#include "events/encryptedevent.h"
18 static QFuture<
bool> setup(Connection* connection,
19 std::unique_ptr<ConnectionEncryptionData>& result,
20 bool clearDatabase =
false);
23 QOlmAccount olmAccount;
25 mutable Database database;
26 std::unordered_map<QByteArray, std::vector<QOlmSession>> olmSessions;
28 QHash<QString, KeyVerificationSession*> verificationSessions{};
29 QSet<QString> trackedUsers{};
30 QSet<QString> outdatedUsers{};
31 QHash<QString, QHash<QString, DeviceKeys>> deviceKeys{};
32 JobHandle<QueryKeysJob> currentQueryKeysJob{};
33 QSet<std::pair<QString, QString>> triedDevices{};
36 bool encryptionUpdateRequired =
false;
37 QHash<QString,
int> oneTimeKeysCount{};
38 std::vector<std::unique_ptr<EncryptedEvent>> pendingEncryptedEvents{};
39 bool isUploadingKeys =
false;
40 bool firstSync =
true;
41 QHash<QString, QHash<QString,
bool>> selfVerifiedDevices;
42 QHash<QString, QHash<QString,
bool>> verifiedDevices;
44 void saveDevicesList();
45 void loadDevicesList();
46 QString curveKeyForUserDevice(
const QString& userId,
47 const QString& device)
const;
48 bool isKnownCurveKey(
const QString& userId,
49 const QString& curveKey)
const;
50 bool hasOlmSession(
const QString &user,
const QString &deviceId)
const;
52 void onSyncSuccess(SyncData &syncResponse);
53 void loadOutdatedUserDevices();
54 void consumeToDeviceEvent(EventPtr toDeviceEvent);
55 void encryptionUpdate(
const QList<QString>& forUsers);
57 bool createOlmSession(
const QString& targetUserId,
58 const QString& targetDeviceId,
59 const OneTimeKeys& oneTimeKeyObject);
60 void saveSession(
const QOlmSession& session,
const QByteArray& senderKey)
62 database.saveOlmSession(senderKey, session,
63 QDateTime::currentDateTime());
65 void saveOlmAccount();
68 std::pair<QByteArray, QByteArray> sessionDecryptMessage(
69 const QJsonObject& personalCipherObject,
70 const QByteArray& senderKey);
71 std::pair<EventPtr, QByteArray> sessionDecryptMessage(
const EncryptedEvent& encryptedEvent);
73 QJsonObject assembleEncryptedContent(
74 QJsonObject payloadJson,
const QString& targetUserId,
75 const QString& targetDeviceId)
const;
76 void sendSessionKeyToDevices(
77 const QString& roomId,
78 const QOlmOutboundGroupSession& outboundSession,
79 const QMultiHash<QString, QString>& devices);
81 template <
typename... ArgTs>
82 KeyVerificationSession* setupKeyVerificationSession(ArgTs&&... sessionArgs)
84 auto session =
new KeyVerificationSession(std::forward<ArgTs>(sessionArgs)...);
85 verificationSessions.insert(session->transactionId(), session);
86 QObject::connect(session, &QObject::destroyed, q,
87 [
this, txnId = session->transactionId()] {
88 verificationSessions.remove(txnId);
90 emit q->newKeyVerificationSession(session);
96 ConnectionEncryptionData(Connection* connection,
97 PicklingKey&& picklingKey);
98 bool hasConflictingDeviceIdsAndCrossSigningKeys(
const QString& userId);
100 void handleQueryKeys(
const QueryKeysJob::Response& keys);
102 void handleMasterKeys(
const QHash<QString, CrossSigningKey>& masterKeys);
103 void handleSelfSigningKeys(
const QHash<QString, CrossSigningKey>& selfSigningKeys);
104 void handleUserSigningKeys(
const QHash<QString, CrossSigningKey>& userSigningKeys);
105 void handleDevicesList(
106 const QHash<QString, QHash<QString, QueryKeysJob::DeviceInformation>>& newDeviceKeys);
107 void checkVerifiedMasterKeys(
const QHash<QString, CrossSigningKey>& masterKeys);
110 void consumeDevicesList(
const DevicesList &devicesList);
111 bool processIfVerificationEvent(
const Event& evt,
bool encrypted);
112 void handleEncryptedToDeviceEvent(
const EncryptedEvent& event);
115 std::pair<QOlmMessage::Type, QByteArray> olmEncryptMessage(
116 const QString& userId,
const QString& device,
117 const QByteArray& message)
const;
119 void doSendSessionKeyToDevices(
const QString& roomId,
const QByteArray& sessionId,
120 const QByteArray &sessionKey, uint32_t messageIndex,
121 const QMultiHash<QString, QString>& devices);