2020-03-13 23:05:18 +03:00
|
|
|
#include "DeviceVerificationFlow.h"
|
2020-06-09 19:36:41 +03:00
|
|
|
#include "ChatPage.h"
|
2020-03-13 23:05:18 +03:00
|
|
|
|
2020-06-07 14:35:32 +03:00
|
|
|
#include "Logging.h"
|
2020-06-04 16:44:15 +03:00
|
|
|
#include <QDateTime>
|
|
|
|
#include <QDebug> // only for debugging
|
2020-03-13 23:05:18 +03:00
|
|
|
#include <QTimer>
|
2020-06-07 14:35:32 +03:00
|
|
|
#include <iostream> // only for debugging
|
2020-03-13 23:05:18 +03:00
|
|
|
|
|
|
|
static constexpr int TIMEOUT = 2 * 60 * 1000; // 2 minutes
|
|
|
|
|
2020-06-09 19:36:41 +03:00
|
|
|
namespace msgs = mtx::events::msg;
|
|
|
|
|
2020-03-13 23:05:18 +03:00
|
|
|
DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
|
|
|
|
{
|
2020-06-09 19:36:41 +03:00
|
|
|
qRegisterMetaType<mtx::events::collections::DeviceEvents>();
|
2020-03-13 23:05:18 +03:00
|
|
|
timeout = new QTimer(this);
|
|
|
|
timeout->setSingleShot(true);
|
|
|
|
connect(timeout, &QTimer::timeout, this, [this]() {
|
|
|
|
emit timedout();
|
|
|
|
this->deleteLater();
|
|
|
|
});
|
2020-06-09 19:36:41 +03:00
|
|
|
connect(ChatPage::instance(),
|
|
|
|
&ChatPage::recievedDeviceVerificationAccept,
|
|
|
|
this,
|
|
|
|
[this](const mtx::events::collections::DeviceEvents &message) {
|
|
|
|
auto msg =
|
|
|
|
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationAccept>>(message);
|
|
|
|
if (msg.content.transaction_id == this->transaction_id) {
|
|
|
|
std::cout << "Recieved Event Accept" << std::endl;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connect(ChatPage::instance(),
|
|
|
|
&ChatPage::recievedDeviceVerificationRequest,
|
|
|
|
this,
|
|
|
|
[this](const mtx::events::collections::DeviceEvents &message) {
|
|
|
|
auto msg =
|
|
|
|
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationRequest>>(message);
|
|
|
|
if (msg.content.transaction_id == this->transaction_id) {
|
|
|
|
std::cout << "Recieved Event Request" << std::endl;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connect(ChatPage::instance(),
|
|
|
|
&ChatPage::recievedDeviceVerificationCancel,
|
|
|
|
this,
|
|
|
|
[this](const mtx::events::collections::DeviceEvents &message) {
|
|
|
|
auto msg =
|
|
|
|
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationCancel>>(message);
|
|
|
|
if (msg.content.transaction_id == this->transaction_id) {
|
|
|
|
std::cout << "Recieved Event Cancel" << std::endl;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connect(ChatPage::instance(),
|
|
|
|
&ChatPage::recievedDeviceVerificationKey,
|
|
|
|
this,
|
|
|
|
[this](const mtx::events::collections::DeviceEvents &message) {
|
|
|
|
auto msg =
|
|
|
|
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationKey>>(message);
|
|
|
|
if (msg.content.transaction_id == this->transaction_id) {
|
|
|
|
std::cout << "Recieved Event Key" << std::endl;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connect(ChatPage::instance(),
|
|
|
|
&ChatPage::recievedDeviceVerificationMac,
|
|
|
|
this,
|
|
|
|
[this](const mtx::events::collections::DeviceEvents &message) {
|
|
|
|
auto msg =
|
|
|
|
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationMac>>(message);
|
|
|
|
if (msg.content.transaction_id == this->transaction_id) {
|
|
|
|
std::cout << "Recieved Event Mac" << std::endl;
|
|
|
|
}
|
|
|
|
});
|
2020-03-13 23:05:18 +03:00
|
|
|
timeout->start(TIMEOUT);
|
|
|
|
}
|
|
|
|
|
2020-06-07 14:35:32 +03:00
|
|
|
QString
|
|
|
|
DeviceVerificationFlow::getUserId()
|
2020-06-04 16:44:15 +03:00
|
|
|
{
|
2020-06-07 14:35:32 +03:00
|
|
|
return this->userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
DeviceVerificationFlow::getDeviceId()
|
|
|
|
{
|
|
|
|
return this->deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceVerificationFlow::Method
|
|
|
|
DeviceVerificationFlow::getMethod()
|
|
|
|
{
|
|
|
|
return this->method;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::setUserId(QString userID)
|
|
|
|
{
|
|
|
|
this->userId = userID;
|
2020-06-09 19:36:41 +03:00
|
|
|
this->toClient = mtx::identifiers::parse<mtx::identifiers::User>(userID.toStdString());
|
2020-06-07 14:35:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::setDeviceId(QString deviceID)
|
|
|
|
{
|
|
|
|
this->deviceId = deviceID;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::setMethod(DeviceVerificationFlow::Method method_)
|
|
|
|
{
|
|
|
|
this->method = method_;
|
2020-06-04 16:44:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//! accepts a verification
|
2020-03-13 23:05:18 +03:00
|
|
|
void
|
|
|
|
DeviceVerificationFlow::acceptVerificationRequest()
|
|
|
|
{
|
2020-06-04 16:44:15 +03:00
|
|
|
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationAccept> body;
|
|
|
|
mtx::events::msg::KeyVerificationAccept req;
|
|
|
|
|
|
|
|
req.transaction_id = this->transaction_id;
|
|
|
|
req.method = mtx::events::msg::VerificationMethods::SASv1;
|
2020-06-07 14:35:32 +03:00
|
|
|
req.key_agreement_protocol = "curve25519";
|
|
|
|
req.hash = "sha256";
|
2020-06-04 16:44:15 +03:00
|
|
|
req.message_authentication_code = "";
|
|
|
|
// req.short_authentication_string = "";
|
|
|
|
req.commitment = "";
|
|
|
|
|
2020-06-07 14:35:32 +03:00
|
|
|
emit this->verificationRequestAccepted(this->method);
|
2020-06-04 16:44:15 +03:00
|
|
|
|
2020-06-07 14:35:32 +03:00
|
|
|
body[this->toClient][this->deviceId.toStdString()] = req;
|
|
|
|
|
|
|
|
http::client()
|
|
|
|
->send_to_device<mtx::events::msg::KeyVerificationAccept,
|
|
|
|
mtx::events::EventType::KeyVerificationAccept>(
|
|
|
|
"m.key.verification.accept", body, [](mtx::http::RequestErr err) {
|
|
|
|
if (err)
|
|
|
|
nhlog::net()->warn("failed to accept verification request: {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
// emit this->verificationRequestAccepted(rand() % 2 ? Emoji : Decimal);
|
|
|
|
});
|
2020-06-04 16:44:15 +03:00
|
|
|
}
|
|
|
|
//! starts the verification flow
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::startVerificationRequest()
|
|
|
|
{
|
2020-06-07 14:35:32 +03:00
|
|
|
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationStart> body;
|
|
|
|
mtx::events::msg::KeyVerificationStart req;
|
|
|
|
|
|
|
|
req.from_device = http::client()->device_id();
|
|
|
|
req.transaction_id = this->transaction_id;
|
|
|
|
req.method = mtx::events::msg::VerificationMethods::SASv1;
|
|
|
|
req.key_agreement_protocols = {};
|
|
|
|
req.hashes = {};
|
|
|
|
req.message_authentication_codes = {};
|
2020-06-04 16:44:15 +03:00
|
|
|
// req.short_authentication_string = "";
|
2020-06-09 19:36:41 +03:00
|
|
|
qDebug()<<"Inside Start Verification";
|
|
|
|
qDebug()<<this->userId;
|
2020-06-07 14:35:32 +03:00
|
|
|
body[this->toClient][this->deviceId.toStdString()] = req;
|
|
|
|
|
|
|
|
http::client()
|
|
|
|
->send_to_device<mtx::events::msg::KeyVerificationStart,
|
|
|
|
mtx::events::EventType::KeyVerificationStart>(
|
|
|
|
"m.key.verification.start", body, [](mtx::http::RequestErr err) {
|
|
|
|
if (err)
|
|
|
|
nhlog::net()->warn("failed to start verification request: {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
});
|
2020-06-04 16:44:15 +03:00
|
|
|
}
|
|
|
|
//! sends a verification request
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::sendVerificationRequest()
|
|
|
|
{
|
|
|
|
QDateTime CurrentTime = QDateTime::currentDateTimeUtc();
|
|
|
|
|
|
|
|
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationRequest> body;
|
|
|
|
mtx::events::msg::KeyVerificationRequest req;
|
|
|
|
|
2020-06-07 14:35:32 +03:00
|
|
|
this->transaction_id = http::client()->generate_txn_id();
|
|
|
|
|
|
|
|
req.from_device = http::client()->device_id();
|
|
|
|
req.transaction_id = this->transaction_id;
|
2020-06-04 16:44:15 +03:00
|
|
|
req.methods.resize(1);
|
|
|
|
req.methods[0] = mtx::events::msg::VerificationMethods::SASv1;
|
|
|
|
req.timestamp = (uint64_t)CurrentTime.toTime_t();
|
|
|
|
|
2020-06-07 14:35:32 +03:00
|
|
|
body[this->toClient][this->deviceId.toStdString()] = req;
|
|
|
|
|
|
|
|
http::client()
|
|
|
|
->send_to_device<mtx::events::msg::KeyVerificationRequest,
|
|
|
|
mtx::events::EventType::KeyVerificationRequest>(
|
|
|
|
"m.key.verification.request", body, [](mtx::http::RequestErr err) {
|
|
|
|
if (err)
|
|
|
|
nhlog::net()->warn("failed to send verification request: {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
});
|
2020-03-13 23:05:18 +03:00
|
|
|
}
|
|
|
|
//! cancels a verification flow
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::cancelVerification()
|
|
|
|
{
|
2020-06-04 16:44:15 +03:00
|
|
|
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationCancel> body;
|
|
|
|
mtx::events::msg::KeyVerificationCancel req;
|
|
|
|
|
|
|
|
req.transaction_id = this->transaction_id;
|
2020-06-07 14:35:32 +03:00
|
|
|
// TODO: Add Proper Error Messages and Code
|
|
|
|
req.reason = "Device Verification Cancelled";
|
|
|
|
req.code = "400";
|
|
|
|
|
|
|
|
body[this->toClient][deviceId.toStdString()] = req;
|
|
|
|
|
|
|
|
http::client()
|
|
|
|
->send_to_device<mtx::events::msg::KeyVerificationCancel,
|
|
|
|
mtx::events::EventType::KeyVerificationCancel>(
|
|
|
|
"m.key.verification.cancel", body, [this](mtx::http::RequestErr err) {
|
|
|
|
if (err)
|
|
|
|
nhlog::net()->warn("failed to cancel verification request: {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
this->deleteLater();
|
|
|
|
});
|
2020-03-13 23:05:18 +03:00
|
|
|
}
|
2020-06-09 19:36:41 +03:00
|
|
|
//! sends the verification key
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::sendVerificationKey()
|
|
|
|
{
|
|
|
|
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationKey> body;
|
|
|
|
mtx::events::msg::KeyVerificationKey req;
|
|
|
|
|
|
|
|
req.key = "";
|
|
|
|
req.transaction_id = this->transaction_id;
|
|
|
|
|
|
|
|
body[this->toClient][deviceId.toStdString()] = req;
|
|
|
|
|
|
|
|
http::client()
|
|
|
|
->send_to_device<mtx::events::msg::KeyVerificationKey,
|
|
|
|
mtx::events::EventType::KeyVerificationKey>(
|
|
|
|
"m.key.verification.cancel", body, [](mtx::http::RequestErr err) {
|
|
|
|
if (err)
|
|
|
|
nhlog::net()->warn("failed to send verification key: {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//! sends the mac of the keys
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::sendVerificationMac()
|
|
|
|
{
|
|
|
|
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationMac> body;
|
|
|
|
mtx::events::msg::KeyVerificationMac req;
|
|
|
|
|
|
|
|
req.transaction_id = this->transaction_id;
|
|
|
|
// req.mac = "";
|
|
|
|
req.keys = "";
|
|
|
|
|
|
|
|
body[this->toClient][deviceId.toStdString()] = req;
|
|
|
|
|
|
|
|
http::client()
|
|
|
|
->send_to_device<mtx::events::msg::KeyVerificationMac,
|
|
|
|
mtx::events::EventType::KeyVerificationMac>(
|
|
|
|
"m.key.verification.cancel", body, [](mtx::http::RequestErr err) {
|
|
|
|
if (err)
|
|
|
|
nhlog::net()->warn("failed to send verification MAC: {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
});
|
|
|
|
}
|
2020-03-13 23:05:18 +03:00
|
|
|
//! Completes the verification flow
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::acceptDevice()
|
|
|
|
{
|
|
|
|
emit deviceVerified();
|
|
|
|
this->deleteLater();
|
2020-06-04 16:44:15 +03:00
|
|
|
|
|
|
|
// Yet to add send to_device message
|
2020-03-13 23:05:18 +03:00
|
|
|
}
|