| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 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 |
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 |
|
| 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 |
|
| 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 |
|
| 155 |
void unverify(); |
| 156 |
|
| 157 |
void next(); |
| 158 |
|
| 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 |
|
| 182 |
void sendVerificationRequest(); |
| 183 |
|
| 184 |
void sendVerificationReady(); |
| 185 |
|
| 186 |
void sendVerificationDone(); |
| 187 |
|
| 188 |
void acceptVerificationRequest(); |
| 189 |
|
| 190 |
void startVerificationRequest(); |
| 191 |
|
| 192 |
void cancelVerification(DeviceVerificationFlow::Error error_code); |
| 193 |
|
| 194 |
void sendVerificationKey(); |
| 195 |
|
| 196 |
void sendVerificationMac(); |
| 197 |
|
| 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 |
|
| 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 |
}; |