2023-02-22 01:48:49 +03:00
|
|
|
// SPDX-FileCopyrightText: Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-08-30 20:06:53 +03:00
|
|
|
#pragma once
|
2020-03-13 23:05:18 +03:00
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
#include <QObject>
|
2024-03-16 05:55:57 +03:00
|
|
|
#include <QQmlEngine>
|
2020-10-02 02:14:42 +03:00
|
|
|
|
2023-10-31 18:38:15 +03:00
|
|
|
#include <mtxclient/crypto/client.hpp>
|
2020-06-17 21:28:35 +03:00
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
#include "CacheCryptoStructs.h"
|
2020-03-13 23:05:18 +03:00
|
|
|
|
|
|
|
class QTimer;
|
2023-10-31 18:38:15 +03:00
|
|
|
class TimelineModel;
|
2020-03-13 23:05:18 +03:00
|
|
|
|
2020-06-20 15:20:43 +03:00
|
|
|
using sas_ptr = std::unique_ptr<mtx::crypto::SAS>;
|
|
|
|
|
2020-10-05 23:12:10 +03:00
|
|
|
// 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
|
2022-06-12 20:09:07 +03:00
|
|
|
* | | | (1) | && no canonical_json
|
2020-10-05 23:12:10 +03:00
|
|
|
* | | 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
|
|
|
|
* | |<------------------------------->| |
|
2022-06-12 20:09:07 +03:00
|
|
|
*
|
|
|
|
* (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
|
2020-10-05 23:12:10 +03:00
|
|
|
*/
|
|
|
|
// clang-format on
|
2022-10-10 15:38:29 +03:00
|
|
|
class DeviceVerificationFlow final : public QObject
|
2020-03-13 23:05:18 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
Q_OBJECT
|
2023-07-16 20:25:37 +03:00
|
|
|
QML_ELEMENT
|
|
|
|
QML_UNCREATABLE("")
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
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)
|
2021-11-02 00:20:15 +03:00
|
|
|
Q_PROPERTY(bool isMultiDeviceVerification READ isMultiDeviceVerification CONSTANT)
|
2020-03-13 23:05:18 +03:00
|
|
|
|
|
|
|
public:
|
2021-09-18 01:22:33 +03:00
|
|
|
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,
|
2024-01-15 04:16:04 +03:00
|
|
|
AcceptedOnOtherDevice,
|
2021-09-18 01:22:33 +03:00
|
|
|
OutOfOrder,
|
|
|
|
};
|
|
|
|
Q_ENUM(Error)
|
|
|
|
|
2021-11-22 02:32:49 +03:00
|
|
|
static QSharedPointer<DeviceVerificationFlow>
|
|
|
|
NewInRoomVerification(QObject *parent_,
|
|
|
|
TimelineModel *timelineModel_,
|
|
|
|
const mtx::events::msg::KeyVerificationRequest &msg,
|
2022-10-04 00:57:30 +03:00
|
|
|
const QString &other_user_,
|
|
|
|
const QString &event_id_);
|
2021-11-22 02:32:49 +03:00
|
|
|
static QSharedPointer<DeviceVerificationFlow>
|
|
|
|
NewToDeviceVerification(QObject *parent_,
|
|
|
|
const mtx::events::msg::KeyVerificationRequest &msg,
|
2022-10-04 00:57:30 +03:00
|
|
|
const QString &other_user_,
|
|
|
|
const QString &txn_id_);
|
2021-11-22 02:32:49 +03:00
|
|
|
static QSharedPointer<DeviceVerificationFlow>
|
|
|
|
NewToDeviceVerification(QObject *parent_,
|
|
|
|
const mtx::events::msg::KeyVerificationStart &msg,
|
2022-10-04 00:57:30 +03:00
|
|
|
const QString &other_user_,
|
|
|
|
const QString &txn_id_);
|
2021-09-18 01:22:33 +03:00
|
|
|
static QSharedPointer<DeviceVerificationFlow>
|
2022-10-04 00:57:30 +03:00
|
|
|
InitiateUserVerification(QObject *parent_,
|
|
|
|
TimelineModel *timelineModel_,
|
|
|
|
const QString &userid);
|
2021-10-30 01:22:47 +03:00
|
|
|
static QSharedPointer<DeviceVerificationFlow>
|
2022-10-04 00:57:30 +03:00
|
|
|
InitiateDeviceVerification(QObject *parent,
|
|
|
|
const QString &userid,
|
|
|
|
const std::vector<QString> &devices);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
// 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);
|
2022-10-04 00:57:30 +03:00
|
|
|
void setEventId(const std::string &event_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
bool isDeviceVerification() const
|
|
|
|
{
|
|
|
|
return this->type == DeviceVerificationFlow::Type::ToDevice;
|
|
|
|
}
|
|
|
|
bool isSelfVerification() const;
|
2021-11-02 00:20:15 +03:00
|
|
|
bool isMultiDeviceVerification() const { return deviceIds.size() > 1; }
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2020-03-13 23:05:18 +03:00
|
|
|
public slots:
|
2021-09-18 01:22:33 +03:00
|
|
|
//! unverifies a device
|
|
|
|
void unverify();
|
|
|
|
//! Continues the flow
|
|
|
|
void next();
|
|
|
|
//! Cancel the flow
|
|
|
|
void cancel() { cancelVerification(User); }
|
2020-10-05 23:12:10 +03:00
|
|
|
|
|
|
|
signals:
|
2021-09-18 01:22:33 +03:00
|
|
|
void refreshProfile();
|
|
|
|
void stateChanged();
|
|
|
|
void errorChanged();
|
2020-10-05 23:12:10 +03:00
|
|
|
|
|
|
|
private:
|
2021-09-18 01:22:33 +03:00
|
|
|
DeviceVerificationFlow(QObject *,
|
|
|
|
DeviceVerificationFlow::Type flow_type,
|
|
|
|
TimelineModel *model,
|
2022-10-04 00:57:30 +03:00
|
|
|
const QString &userID,
|
|
|
|
const std::vector<QString> &deviceIds_);
|
2021-09-18 01:22:33 +03:00
|
|
|
void setState(State state)
|
|
|
|
{
|
|
|
|
if (state != state_) {
|
|
|
|
state_ = state;
|
|
|
|
emit stateChanged();
|
2020-10-05 23:12:10 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-10-30 01:22:47 +03:00
|
|
|
std::vector<QString> deviceIds;
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
// 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;
|
2022-06-18 02:35:30 +03:00
|
|
|
std::string canonical_json;
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
std::vector<int> sasList;
|
|
|
|
UserKeyCache their_keys;
|
|
|
|
TimelineModel *model_;
|
|
|
|
mtx::common::Relation relation;
|
|
|
|
|
|
|
|
State state_ = PromptStartVerification;
|
|
|
|
Error error_ = UnknownMethod;
|
|
|
|
|
|
|
|
bool isMacVerified = false;
|
|
|
|
|
2022-07-22 12:46:22 +03:00
|
|
|
bool keySent = false, macSent = false, acceptSent = false, startSent = false;
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
template<typename T>
|
2023-10-31 18:38:15 +03:00
|
|
|
void send(T msg);
|
2020-03-13 23:05:18 +03:00
|
|
|
};
|