summary refs log tree commit diff
path: root/src/encryption/DeviceVerificationFlow.h
blob: f78f0dbd706789f8a16eea4044d274dc52968401 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include <QObject>
#include <QQmlEngine>

#include <mtxclient/crypto/client.hpp>

#include "CacheCryptoStructs.h"

class QTimer;
class TimelineModel;

using sas_ptr = std::unique_ptr<mtx::crypto::SAS>;

// clang-format off
/*
 * Stolen from fluffy chat :D
 *
 *      State         |   +-------------+                    +-----------+                                  |
 *                    |   | AliceDevice |                    | BobDevice |                                  |
 *                    |   | (sender)    |                    |           |                                  |
 *                    |   +-------------+                    +-----------+                                  |
 * promptStartVerify  |         |                                 |                                         |
 *                    |      o  | (m.key.verification.request)    |                                         |
 *                    |      p  |-------------------------------->| (ASK FOR VERIFICATION REQUEST)          |
 * waitForOtherAccept |      t  |                                 |                                         | promptStartVerify
 * &&                 |      i  |      (m.key.verification.ready) |                                         |
 * no commitment      |      o  |<--------------------------------|                                         |
 * &&                 |      n  |                                 |                                         |
 * no canonical_json  |      a  |      (m.key.verification.start) |                                         | waitingForKeys
 *                    |      l  |<--------------------------------| Not sending to prevent the glare resolve| && no commitment
 *                    |         |                                 |                               (1)       | && no canonical_json
 *                    |         | m.key.verification.start        |                                         |
 * waitForOtherAccept |         |-------------------------------->| (IF NOT ALREADY ASKED,                  |
 * &&                 |         |                                 |  ASK FOR VERIFICATION REQUEST)          | promptStartVerify, if not accepted
 * canonical_json     |         |       m.key.verification.accept |                                         |
 *                    |         |<--------------------------------|                                         |
 * waitForOtherAccept |         |                                 |                                         | waitingForKeys
 * &&                 |         | m.key.verification.key          |                                         | && canonical_json
 * commitment         |         |-------------------------------->|                                         | && commitment
 *                    |         |                                 |                                         |
 *                    |         |          m.key.verification.key |                                         |
 *                    |         |<--------------------------------|                                         |
 * compareEmoji/Number|         |                                 |                                         | compareEmoji/Number
 *                    |         |     COMPARE EMOJI / NUMBERS     |                                         |
 *                    |         |                                 |                                         |
 * waitingForMac      |         |     m.key.verification.mac      |                                         | waitingForMac
 *                    | success |<------------------------------->|  success                                |
 *                    |         |                                 |                                         |
 * success/fail       |         |         m.key.verification.done |                                         | success/fail
 *                    |         |<------------------------------->|                                         |
 *
 *  (1) Sometimes the other side does send this start. In this case we run the glare algorithm and send an accept only if
 *      We are the bigger mxid and deviceid (since we discard our start message). <- GLARE RESOLUTION
 */
// clang-format on
class DeviceVerificationFlow final : public QObject
{
    Q_OBJECT
    QML_ELEMENT
    QML_UNCREATABLE("")

    Q_PROPERTY(QString state READ state NOTIFY stateChanged)
    Q_PROPERTY(Error error READ error NOTIFY errorChanged)
    Q_PROPERTY(QString userId READ getUserId CONSTANT)
    Q_PROPERTY(QString deviceId READ getDeviceId CONSTANT)
    Q_PROPERTY(bool sender READ getSender CONSTANT)
    Q_PROPERTY(std::vector<int> sasList READ getSasList CONSTANT)
    Q_PROPERTY(bool isDeviceVerification READ isDeviceVerification CONSTANT)
    Q_PROPERTY(bool isSelfVerification READ isSelfVerification CONSTANT)
    Q_PROPERTY(bool isMultiDeviceVerification READ isMultiDeviceVerification CONSTANT)

public:
    enum State
    {
        PromptStartVerification,
        WaitingForOtherToAccept,
        WaitingForKeys,
        CompareEmoji,
        CompareNumber,
        WaitingForMac,
        Success,
        Failed,
    };
    Q_ENUM(State)

    enum Type
    {
        ToDevice,
        RoomMsg
    };

    enum Error
    {
        UnknownMethod,
        MismatchedCommitment,
        MismatchedSAS,
        KeyMismatch,
        Timeout,
        User,
        AcceptedOnOtherDevice,
        OutOfOrder,
    };
    Q_ENUM(Error)

    static QSharedPointer<DeviceVerificationFlow>
    NewInRoomVerification(QObject *parent_,
                          TimelineModel *timelineModel_,
                          const mtx::events::msg::KeyVerificationRequest &msg,
                          const QString &other_user_,
                          const QString &event_id_);
    static QSharedPointer<DeviceVerificationFlow>
    NewToDeviceVerification(QObject *parent_,
                            const mtx::events::msg::KeyVerificationRequest &msg,
                            const QString &other_user_,
                            const QString &txn_id_);
    static QSharedPointer<DeviceVerificationFlow>
    NewToDeviceVerification(QObject *parent_,
                            const mtx::events::msg::KeyVerificationStart &msg,
                            const QString &other_user_,
                            const QString &txn_id_);
    static QSharedPointer<DeviceVerificationFlow>
    InitiateUserVerification(QObject *parent_,
                             TimelineModel *timelineModel_,
                             const QString &userid);
    static QSharedPointer<DeviceVerificationFlow>
    InitiateDeviceVerification(QObject *parent,
                               const QString &userid,
                               const std::vector<QString> &devices);

    // getters
    QString state();
    Error error() { return error_; }
    QString getUserId();
    QString getDeviceId();
    bool getSender();
    std::vector<int> getSasList();
    QString transactionId() { return QString::fromStdString(this->transaction_id); }
    // setters
    void setDeviceId(QString deviceID);
    void setEventId(const std::string &event_id);
    bool isDeviceVerification() const
    {
        return this->type == DeviceVerificationFlow::Type::ToDevice;
    }
    bool isSelfVerification() const;
    bool isMultiDeviceVerification() const { return deviceIds.size() > 1; }

public slots:
    //! unverifies a device
    void unverify();
    //! Continues the flow
    void next();
    //! Cancel the flow
    void cancel() { cancelVerification(User); }

signals:
    void refreshProfile();
    void stateChanged();
    void errorChanged();

private:
    DeviceVerificationFlow(QObject *,
                           DeviceVerificationFlow::Type flow_type,
                           TimelineModel *model,
                           const QString &userID,
                           const std::vector<QString> &deviceIds_);
    void setState(State state)
    {
        if (state != state_) {
            state_ = state;
            emit stateChanged();
        }
    }

    void handleStartMessage(const mtx::events::msg::KeyVerificationStart &msg, std::string);
    //! sends a verification request
    void sendVerificationRequest();
    //! accepts a verification request
    void sendVerificationReady();
    //! completes the verification flow();
    void sendVerificationDone();
    //! accepts a verification
    void acceptVerificationRequest();
    //! starts the verification flow
    void startVerificationRequest();
    //! cancels a verification flow
    void cancelVerification(DeviceVerificationFlow::Error error_code);
    //! sends the verification key
    void sendVerificationKey();
    //! sends the mac of the keys
    void sendVerificationMac();
    //! Completes the verification flow
    void acceptDevice();

    std::string transaction_id;

    bool sender;
    Type type;
    mtx::identifiers::User toClient;
    QString deviceId;
    std::vector<QString> deviceIds;

    // public part of our master key, when trusted or empty
    std::string our_trusted_master_key;

    mtx::events::msg::SASMethods method = mtx::events::msg::SASMethods::Emoji;
    QTimer *timeout                     = nullptr;
    sas_ptr sas;
    std::string mac_method;
    std::string commitment;
    std::string canonical_json;

    std::vector<int> sasList;
    UserKeyCache their_keys;
    TimelineModel *model_;
    mtx::common::Relation relation;

    State state_ = PromptStartVerification;
    Error error_ = UnknownMethod;

    bool isMacVerified = false;

    bool keySent = false, macSent = false, acceptSent = false, startSent = false;

    template<typename T>
    void send(T msg);
};