2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// 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>
|
|
|
|
|
|
|
|
#include <mtx/responses/crypto.hpp>
|
2020-10-27 19:45:28 +03:00
|
|
|
#include <nlohmann/json.hpp>
|
2020-06-17 21:28:35 +03:00
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
#include "CacheCryptoStructs.h"
|
2020-10-05 23:12:10 +03:00
|
|
|
#include "Logging.h"
|
2020-07-17 23:16:30 +03:00
|
|
|
#include "MatrixClient.h"
|
2020-10-02 02:14:42 +03:00
|
|
|
#include "Olm.h"
|
2020-10-05 23:12:10 +03:00
|
|
|
#include "timeline/TimelineModel.h"
|
2020-03-13 23:05:18 +03:00
|
|
|
|
|
|
|
class QTimer;
|
|
|
|
|
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
|
|
|
|
* | | | | && 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
|
|
|
|
* | |<------------------------------->| |
|
|
|
|
*/
|
|
|
|
// clang-format on
|
2020-03-13 23:05:18 +03:00
|
|
|
class DeviceVerificationFlow : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2020-10-05 23:12:10 +03:00
|
|
|
Q_PROPERTY(QString state READ state NOTIFY stateChanged)
|
2020-10-05 23:58:07 +03:00
|
|
|
Q_PROPERTY(Error error READ error NOTIFY errorChanged)
|
2020-10-05 23:12:10 +03:00
|
|
|
Q_PROPERTY(QString userId READ getUserId CONSTANT)
|
|
|
|
Q_PROPERTY(QString deviceId READ getDeviceId CONSTANT)
|
|
|
|
Q_PROPERTY(bool sender READ getSender CONSTANT)
|
2020-07-17 23:16:30 +03:00
|
|
|
Q_PROPERTY(std::vector<int> sasList READ getSasList CONSTANT)
|
2021-01-12 15:32:12 +03:00
|
|
|
Q_PROPERTY(bool isDeviceVerification READ isDeviceVerification CONSTANT)
|
|
|
|
Q_PROPERTY(bool isSelfVerification READ isSelfVerification CONSTANT)
|
2020-03-13 23:05:18 +03:00
|
|
|
|
|
|
|
public:
|
2020-10-05 23:12:10 +03:00
|
|
|
enum State
|
2020-07-17 23:16:30 +03:00
|
|
|
{
|
2020-10-05 23:12:10 +03:00
|
|
|
PromptStartVerification,
|
|
|
|
WaitingForOtherToAccept,
|
|
|
|
WaitingForKeys,
|
|
|
|
CompareEmoji,
|
|
|
|
CompareNumber,
|
|
|
|
WaitingForMac,
|
|
|
|
Success,
|
|
|
|
Failed,
|
2020-07-17 23:16:30 +03:00
|
|
|
};
|
2020-10-05 23:12:10 +03:00
|
|
|
Q_ENUM(State)
|
2020-07-17 23:16:30 +03:00
|
|
|
|
2020-10-05 23:12:10 +03:00
|
|
|
enum Type
|
2020-03-13 23:05:18 +03:00
|
|
|
{
|
2020-10-05 23:12:10 +03:00
|
|
|
ToDevice,
|
|
|
|
RoomMsg
|
2020-03-13 23:05:18 +03:00
|
|
|
};
|
2020-07-17 23:16:30 +03:00
|
|
|
|
2020-06-26 12:40:37 +03:00
|
|
|
enum Error
|
|
|
|
{
|
|
|
|
UnknownMethod,
|
|
|
|
MismatchedCommitment,
|
|
|
|
MismatchedSAS,
|
|
|
|
KeyMismatch,
|
|
|
|
Timeout,
|
2020-10-05 23:12:10 +03:00
|
|
|
User,
|
|
|
|
OutOfOrder,
|
2020-06-26 12:40:37 +03:00
|
|
|
};
|
|
|
|
Q_ENUM(Error)
|
2020-03-13 23:05:18 +03:00
|
|
|
|
2020-10-05 23:12:10 +03:00
|
|
|
static QSharedPointer<DeviceVerificationFlow> NewInRoomVerification(
|
|
|
|
QObject *parent_,
|
|
|
|
TimelineModel *timelineModel_,
|
|
|
|
const mtx::events::msg::KeyVerificationRequest &msg,
|
|
|
|
QString other_user_,
|
|
|
|
QString event_id_);
|
|
|
|
static QSharedPointer<DeviceVerificationFlow> NewToDeviceVerification(
|
|
|
|
QObject *parent_,
|
|
|
|
const mtx::events::msg::KeyVerificationRequest &msg,
|
|
|
|
QString other_user_,
|
|
|
|
QString txn_id_);
|
|
|
|
static QSharedPointer<DeviceVerificationFlow> NewToDeviceVerification(
|
|
|
|
QObject *parent_,
|
|
|
|
const mtx::events::msg::KeyVerificationStart &msg,
|
|
|
|
QString other_user_,
|
|
|
|
QString txn_id_);
|
|
|
|
static QSharedPointer<DeviceVerificationFlow>
|
|
|
|
InitiateUserVerification(QObject *parent_, TimelineModel *timelineModel_, QString userid);
|
|
|
|
static QSharedPointer<DeviceVerificationFlow> InitiateDeviceVerification(QObject *parent,
|
|
|
|
QString userid,
|
|
|
|
QString device);
|
|
|
|
|
2020-07-29 00:55:47 +03:00
|
|
|
// getters
|
2020-10-05 23:12:10 +03:00
|
|
|
QString state();
|
|
|
|
Error error() { return error_; }
|
2020-06-07 14:35:32 +03:00
|
|
|
QString getUserId();
|
|
|
|
QString getDeviceId();
|
2020-06-17 21:28:35 +03:00
|
|
|
bool getSender();
|
2020-10-05 23:12:10 +03:00
|
|
|
std::vector<int> getSasList();
|
|
|
|
QString transactionId() { return QString::fromStdString(this->transaction_id); }
|
2020-07-29 00:55:47 +03:00
|
|
|
// setters
|
2020-06-07 14:35:32 +03:00
|
|
|
void setDeviceId(QString deviceID);
|
2020-07-29 00:55:47 +03:00
|
|
|
void setEventId(std::string event_id);
|
2021-01-12 15:32:12 +03:00
|
|
|
bool isDeviceVerification() const
|
|
|
|
{
|
|
|
|
return this->type == DeviceVerificationFlow::Type::ToDevice;
|
|
|
|
}
|
|
|
|
bool isSelfVerification() const;
|
2020-07-29 00:55:47 +03:00
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
void callback_fn(const UserKeyCache &res, mtx::http::RequestErr err, std::string user_id);
|
2020-03-13 23:05:18 +03:00
|
|
|
|
|
|
|
public slots:
|
2020-10-05 23:12:10 +03:00
|
|
|
//! 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,
|
|
|
|
QString userID,
|
|
|
|
QString deviceId_);
|
|
|
|
void setState(State state)
|
|
|
|
{
|
|
|
|
if (state != state_) {
|
|
|
|
state_ = state;
|
|
|
|
emit stateChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleStartMessage(const mtx::events::msg::KeyVerificationStart &msg, std::string);
|
2020-06-04 16:44:15 +03:00
|
|
|
//! sends a verification request
|
|
|
|
void sendVerificationRequest();
|
2020-06-23 01:05:56 +03:00
|
|
|
//! accepts a verification request
|
|
|
|
void sendVerificationReady();
|
|
|
|
//! completes the verification flow();
|
|
|
|
void sendVerificationDone();
|
2020-06-04 16:44:15 +03:00
|
|
|
//! accepts a verification
|
2020-03-13 23:05:18 +03:00
|
|
|
void acceptVerificationRequest();
|
2020-06-04 16:44:15 +03:00
|
|
|
//! starts the verification flow
|
|
|
|
void startVerificationRequest();
|
2020-03-13 23:05:18 +03:00
|
|
|
//! cancels a verification flow
|
2020-06-26 12:40:37 +03:00
|
|
|
void cancelVerification(DeviceVerificationFlow::Error error_code);
|
2020-06-09 19:36:41 +03:00
|
|
|
//! sends the verification key
|
|
|
|
void sendVerificationKey();
|
|
|
|
//! sends the mac of the keys
|
|
|
|
void sendVerificationMac();
|
2020-03-13 23:05:18 +03:00
|
|
|
//! Completes the verification flow
|
|
|
|
void acceptDevice();
|
|
|
|
|
2020-10-05 23:12:10 +03:00
|
|
|
std::string transaction_id;
|
2020-03-13 23:05:18 +03:00
|
|
|
|
2020-06-17 21:28:35 +03:00
|
|
|
bool sender;
|
2020-10-05 23:12:10 +03:00
|
|
|
Type type;
|
|
|
|
mtx::identifiers::User toClient;
|
|
|
|
QString deviceId;
|
2020-10-08 19:30:31 +03:00
|
|
|
|
|
|
|
// public part of our master key, when trusted or empty
|
|
|
|
std::string our_trusted_master_key;
|
2020-10-05 23:12:10 +03:00
|
|
|
|
|
|
|
mtx::events::msg::SASMethods method = mtx::events::msg::SASMethods::Emoji;
|
|
|
|
QTimer *timeout = nullptr;
|
2020-06-20 15:20:43 +03:00
|
|
|
sas_ptr sas;
|
|
|
|
std::string mac_method;
|
|
|
|
std::string commitment;
|
2020-10-05 23:12:10 +03:00
|
|
|
nlohmann::json canonical_json;
|
|
|
|
|
2020-06-20 15:20:43 +03:00
|
|
|
std::vector<int> sasList;
|
2020-10-06 18:02:41 +03:00
|
|
|
UserKeyCache their_keys;
|
2020-08-09 06:05:15 +03:00
|
|
|
TimelineModel *model_;
|
2021-01-27 00:36:35 +03:00
|
|
|
mtx::common::Relation relation;
|
2020-10-05 23:12:10 +03:00
|
|
|
|
|
|
|
State state_ = PromptStartVerification;
|
2020-10-05 23:58:07 +03:00
|
|
|
Error error_ = UnknownMethod;
|
2020-10-05 23:12:10 +03:00
|
|
|
|
|
|
|
bool isMacVerified = false;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void send(T msg)
|
|
|
|
{
|
|
|
|
if (this->type == DeviceVerificationFlow::Type::ToDevice) {
|
|
|
|
mtx::requests::ToDeviceMessages<T> body;
|
|
|
|
msg.transaction_id = this->transaction_id;
|
|
|
|
body[this->toClient][deviceId.toStdString()] = msg;
|
|
|
|
|
|
|
|
http::client()->send_to_device<T>(
|
2020-10-08 19:16:30 +03:00
|
|
|
this->transaction_id, body, [](mtx::http::RequestErr err) {
|
2020-10-05 23:12:10 +03:00
|
|
|
if (err)
|
|
|
|
nhlog::net()->warn(
|
|
|
|
"failed to send verification to_device message: {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
});
|
|
|
|
} else if (this->type == DeviceVerificationFlow::Type::RoomMsg && model_) {
|
2021-01-27 00:36:35 +03:00
|
|
|
if constexpr (!std::is_same_v<T,
|
|
|
|
mtx::events::msg::KeyVerificationRequest>) {
|
|
|
|
msg.relations.relations.push_back(this->relation);
|
|
|
|
// Set synthesized to surpress the nheko relation extensions
|
|
|
|
msg.relations.synthesized = true;
|
|
|
|
}
|
2020-10-05 23:12:10 +03:00
|
|
|
(model_)->sendMessageEvent(msg, mtx::events::to_device_content_to_type<T>);
|
|
|
|
}
|
|
|
|
|
|
|
|
nhlog::net()->debug(
|
|
|
|
"Sent verification step: {} in state: {}",
|
|
|
|
mtx::events::to_string(mtx::events::to_device_content_to_type<T>),
|
|
|
|
state().toStdString());
|
|
|
|
}
|
2020-03-13 23:05:18 +03:00
|
|
|
};
|