Debian Codegraph @

src/encryption/DeviceVerificationFlow.h

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #pragma once
6
7 #include <QObject>
8 #include <QQmlEngine>
9
10 #include <mtxclient/crypto/client.hpp>
11
12 #include "CacheCryptoStructs.h"
13
14 class QTimer;
15 class TimelineModel;
16
17 using sas_ptr = std::unique_ptr<mtx::crypto::SAS>;
18
19 // clang-format off
20 /*
21 * Stolen from fluffy chat :D
22 *
23 * State | +-------------+ +-----------+ |
24 * | | AliceDevice | | BobDevice | |
25 * | | (sender) | | | |
26 * | +-------------+ +-----------+ |
27 * promptStartVerify | | | |
28 * | o | (m.key.verification.request) | |
29 * | p |-------------------------------->| (ASK FOR VERIFICATION REQUEST) |
30 * waitForOtherAccept | t | | | promptStartVerify
31 * && | i | (m.key.verification.ready) | |
32 * no commitment | o |<--------------------------------| |
33 * && | n | | |
34 * no canonical_json | a | (m.key.verification.start) | | waitingForKeys
35 * | l |<--------------------------------| Not sending to prevent the glare resolve| && no commitment
36 * | | | (1) | && no canonical_json
37 * | | m.key.verification.start | |
38 * waitForOtherAccept | |-------------------------------->| (IF NOT ALREADY ASKED, |
39 * && | | | ASK FOR VERIFICATION REQUEST) | promptStartVerify, if not accepted
40 * canonical_json | | m.key.verification.accept | |
41 * | |<--------------------------------| |
42 * waitForOtherAccept | | | | waitingForKeys
43 * && | | m.key.verification.key | | && canonical_json
44 * commitment | |-------------------------------->| | && commitment
45 * | | | |
46 * | | m.key.verification.key | |
47 * | |<--------------------------------| |
48 * compareEmoji/Number| | | | compareEmoji/Number
49 * | | COMPARE EMOJI / NUMBERS | |
50 * | | | |
51 * waitingForMac | | m.key.verification.mac | | waitingForMac
52 * | success |<------------------------------->| success |
53 * | | | |
54 * success/fail | | m.key.verification.done | | success/fail
55 * | |<------------------------------->| |
56 *
57 * (1) Sometimes the other side does send this start. In this case we run the glare algorithm and send an accept only if
58 * We are the bigger mxid and deviceid (since we discard our start message). <- GLARE RESOLUTION
59 */
60 // clang-format on
61 class DeviceVerificationFlow final : public QObject
62 {
63 Q_OBJECT
64 QML_ELEMENT
65 QML_UNCREATABLE("")
66
67 Q_PROPERTY(QString state READ state NOTIFY stateChanged)
68 Q_PROPERTY(Error error READ error NOTIFY errorChanged)
69 Q_PROPERTY(QString userId READ getUserId CONSTANT)
70 Q_PROPERTY(QString deviceId READ getDeviceId CONSTANT)
71 Q_PROPERTY(bool sender READ getSender CONSTANT)
72 Q_PROPERTY(std::vector<int> sasList READ getSasList CONSTANT)
73 Q_PROPERTY(bool isDeviceVerification READ isDeviceVerification CONSTANT)
74 Q_PROPERTY(bool isSelfVerification READ isSelfVerification CONSTANT)
75 Q_PROPERTY(bool isMultiDeviceVerification READ isMultiDeviceVerification CONSTANT)
76
77 public:
78 enum State
79 {
80 PromptStartVerification,
81 WaitingForOtherToAccept,
82 WaitingForKeys,
83 CompareEmoji,
84 CompareNumber,
85 WaitingForMac,
86 Success,
87 Failed,
88 };
89 Q_ENUM(State)
90
91 enum Type
92 {
93 ToDevice,
94 RoomMsg
95 };
96
97 enum Error
98 {
99 UnknownMethod,
100 MismatchedCommitment,
101 MismatchedSAS,
102 KeyMismatch,
103 Timeout,
104 User,
105 AcceptedOnOtherDevice,
106 OutOfOrder,
107 };
108 Q_ENUM(Error)
109
110 static QSharedPointer<DeviceVerificationFlow>
111 NewInRoomVerification(QObject *parent_,
112 TimelineModel *timelineModel_,
113 const mtx::events::msg::KeyVerificationRequest &msg,
114 const QString &other_user_,
115 const QString &event_id_);
116 static QSharedPointer<DeviceVerificationFlow>
117 NewToDeviceVerification(QObject *parent_,
118 const mtx::events::msg::KeyVerificationRequest &msg,
119 const QString &other_user_,
120 const QString &txn_id_);
121 static QSharedPointer<DeviceVerificationFlow>
122 NewToDeviceVerification(QObject *parent_,
123 const mtx::events::msg::KeyVerificationStart &msg,
124 const QString &other_user_,
125 const QString &txn_id_);
126 static QSharedPointer<DeviceVerificationFlow>
127 InitiateUserVerification(QObject *parent_,
128 TimelineModel *timelineModel_,
129 const QString &userid);
130 static QSharedPointer<DeviceVerificationFlow>
131 InitiateDeviceVerification(QObject *parent,
132 const QString &userid,
133 const std::vector<QString> &devices);
134
135 // getters
136 QString state();
137 Error error() { return error_; }
138 QString getUserId();
139 QString getDeviceId();
140 bool getSender();
141 std::vector<int> getSasList();
142 QString transactionId() { return QString::fromStdString(this->transaction_id); }
143 // setters
144 void setDeviceId(QString deviceID);
145 void setEventId(const std::string &event_id);
146 bool isDeviceVerification() const
147 {
148 return this->type == DeviceVerificationFlow::Type::ToDevice;
149 }
150 bool isSelfVerification() const;
151 bool isMultiDeviceVerification() const { return deviceIds.size() > 1; }
152
153 public slots:
154 //! unverifies a device
155 void unverify();
156 //! Continues the flow
157 void next();
158 //! Cancel the flow
159 void cancel() { cancelVerification(User); }
160
161 signals:
162 void refreshProfile();
163 void stateChanged();
164 void errorChanged();
165
166 private:
167 DeviceVerificationFlow(QObject *,
168 DeviceVerificationFlow::Type flow_type,
169 TimelineModel *model,
170 const QString &userID,
171 const std::vector<QString> &deviceIds_);
172 void setState(State state)
173 {
174 if (state != state_) {
175 state_ = state;
176 emit stateChanged();
177 }
178 }
179
180 void handleStartMessage(const mtx::events::msg::KeyVerificationStart &msg, std::string);
181 //! sends a verification request
182 void sendVerificationRequest();
183 //! accepts a verification request
184 void sendVerificationReady();
185 //! completes the verification flow();
186 void sendVerificationDone();
187 //! accepts a verification
188 void acceptVerificationRequest();
189 //! starts the verification flow
190 void startVerificationRequest();
191 //! cancels a verification flow
192 void cancelVerification(DeviceVerificationFlow::Error error_code);
193 //! sends the verification key
194 void sendVerificationKey();
195 //! sends the mac of the keys
196 void sendVerificationMac();
197 //! Completes the verification flow
198 void acceptDevice();
199
200 std::string transaction_id;
201
202 bool sender;
203 Type type;
204 mtx::identifiers::User toClient;
205 QString deviceId;
206 std::vector<QString> deviceIds;
207
208 // public part of our master key, when trusted or empty
209 std::string our_trusted_master_key;
210
211 mtx::events::msg::SASMethods method = mtx::events::msg::SASMethods::Emoji;
212 QTimer *timeout = nullptr;
213 sas_ptr sas;
214 std::string mac_method;
215 std::string commitment;
216 std::string canonical_json;
217
218 std::vector<int> sasList;
219 UserKeyCache their_keys;
220 TimelineModel *model_;
221 mtx::common::Relation relation;
222
223 State state_ = PromptStartVerification;
224 Error error_ = UnknownMethod;
225
226 bool isMacVerified = false;
227
228 bool keySent = false, macSent = false, acceptSent = false, startSent = false;
229
230 template<typename T>
231 void send(T msg);
232 };