libQuotient
A Qt library for building matrix clients
Loading...
Searching...
No Matches
connectionencryptiondata_p.h
Go to the documentation of this file.
1#pragma once
2
3#include "connection.h"
4#include "database.h"
5
6#include "e2ee/qolmaccount.h"
7#include "e2ee/qolmsession.h"
8
9#include "events/encryptedevent.h"
10
11namespace Quotient {
12
13struct DevicesList;
14
15namespace _impl {
16 class QUOTIENT_API ConnectionEncryptionData {
17 public:
18 static QFuture<bool> setup(Connection* connection,
19 std::unique_ptr<ConnectionEncryptionData>& result,
20 bool clearDatabase = false);
21
22 Connection* q;
23 QOlmAccount olmAccount;
24 // No easy way in C++ to discern between SQL SELECT from UPDATE, too bad
25 mutable Database database;
26 std::unordered_map<QByteArray, std::vector<QOlmSession>> olmSessions;
27 //! A map from SenderKey to vector of InboundSession
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{};
34 //! An update of internal tracking structures (trackedUsers, e.g.) is
35 //! needed
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;
43
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;
51
52 void onSyncSuccess(SyncData &syncResponse);
53 void loadOutdatedUserDevices();
54 void consumeToDeviceEvent(EventPtr toDeviceEvent);
55 void encryptionUpdate(const QList<QString>& forUsers);
56
57 bool createOlmSession(const QString& targetUserId,
58 const QString& targetDeviceId,
59 const OneTimeKeys& oneTimeKeyObject);
60 void saveSession(const QOlmSession& session, const QByteArray& senderKey)
61 {
62 database.saveOlmSession(senderKey, session,
63 QDateTime::currentDateTime());
64 }
65 void saveOlmAccount();
66 void reloadDevices();
67
68 std::pair<QByteArray, QByteArray> sessionDecryptMessage(
69 const QJsonObject& personalCipherObject,
70 const QByteArray& senderKey);
71 std::pair<EventPtr, QByteArray> sessionDecryptMessage(const EncryptedEvent& encryptedEvent);
72
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);
80
81 template <typename... ArgTs>
82 KeyVerificationSession* setupKeyVerificationSession(ArgTs&&... sessionArgs)
83 {
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);
89 });
90 emit q->newKeyVerificationSession(session);
91 return session;
92 }
93
94 // This is only public to enable std::make_unique; do not use directly,
95 // get an instance from setup() instead
96 ConnectionEncryptionData(Connection* connection,
97 PicklingKey&& picklingKey);
98 bool hasConflictingDeviceIdsAndCrossSigningKeys(const QString& userId);
99
100 void handleQueryKeys(const QueryKeysJob::Response& keys);
101
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);
108
109 private:
110 void consumeDevicesList(const DevicesList &devicesList);
111 bool processIfVerificationEvent(const Event& evt, bool encrypted);
112 void handleEncryptedToDeviceEvent(const EncryptedEvent& event);
113
114 // This function assumes that an olm session with (user, device) exists
115 std::pair<QOlmMessage::Type, QByteArray> olmEncryptMessage(
116 const QString& userId, const QString& device,
117 const QByteArray& message) const;
118
119 void doSendSessionKeyToDevices(const QString& roomId, const QByteArray& sessionId,
120 const QByteArray &sessionKey, uint32_t messageIndex,
121 const QMultiHash<QString, QString>& devices);
122 };
123} // namespace _impl
124} // namespace Quotient
#define QUOTIENT_API