2020-03-13 23:05:18 +03:00
|
|
|
#include "DeviceVerificationFlow.h"
|
2020-06-09 19:36:41 +03:00
|
|
|
#include "ChatPage.h"
|
2020-06-07 14:35:32 +03:00
|
|
|
#include "Logging.h"
|
2020-06-17 21:28:35 +03:00
|
|
|
|
2020-06-04 16:44:15 +03:00
|
|
|
#include <QDateTime>
|
2020-03-13 23:05:18 +03:00
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
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 *)
|
|
|
|
{
|
|
|
|
timeout = new QTimer(this);
|
|
|
|
timeout->setSingleShot(true);
|
2020-06-20 15:20:43 +03:00
|
|
|
this->sas = olm::client()->sas_init();
|
2020-03-13 23:05:18 +03:00
|
|
|
connect(timeout, &QTimer::timeout, this, [this]() {
|
|
|
|
emit timedout();
|
|
|
|
this->deleteLater();
|
|
|
|
});
|
2020-06-20 15:20:43 +03:00
|
|
|
|
2020-06-09 19:36:41 +03:00
|
|
|
connect(ChatPage::instance(),
|
2020-06-20 15:20:43 +03:00
|
|
|
&ChatPage::recievedDeviceVerificationStart,
|
2020-06-09 19:36:41 +03:00
|
|
|
this,
|
|
|
|
[this](const mtx::events::collections::DeviceEvents &message) {
|
|
|
|
auto msg =
|
2020-06-20 15:20:43 +03:00
|
|
|
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationStart>>(message);
|
2020-06-09 19:36:41 +03:00
|
|
|
if (msg.content.transaction_id == this->transaction_id) {
|
2020-06-23 01:05:56 +03:00
|
|
|
if ((std::find(msg.content.key_agreement_protocols.begin(),
|
|
|
|
msg.content.key_agreement_protocols.end(),
|
|
|
|
"curve25519-hkdf-sha256") !=
|
|
|
|
msg.content.key_agreement_protocols.end()) &&
|
|
|
|
(std::find(msg.content.hashes.begin(),
|
|
|
|
msg.content.hashes.end(),
|
|
|
|
"sha256") != msg.content.hashes.end()) &&
|
2020-06-20 15:20:43 +03:00
|
|
|
(std::find(msg.content.message_authentication_codes.begin(),
|
|
|
|
msg.content.message_authentication_codes.end(),
|
|
|
|
"hmac-sha256") !=
|
2020-06-23 01:05:56 +03:00
|
|
|
msg.content.message_authentication_codes.end()) &&
|
|
|
|
((std::find(msg.content.short_authentication_string.begin(),
|
|
|
|
msg.content.short_authentication_string.end(),
|
|
|
|
mtx::events::msg::SASMethods::Decimal) !=
|
|
|
|
msg.content.short_authentication_string.end()) ||
|
|
|
|
(std::find(msg.content.short_authentication_string.begin(),
|
|
|
|
msg.content.short_authentication_string.end(),
|
|
|
|
mtx::events::msg::SASMethods::Emoji) !=
|
|
|
|
msg.content.short_authentication_string.end()))) {
|
|
|
|
this->acceptVerificationRequest();
|
|
|
|
this->canonical_json = nlohmann::json(msg.content);
|
2020-06-20 15:20:43 +03:00
|
|
|
} else {
|
|
|
|
this->cancelVerification();
|
|
|
|
}
|
2020-06-09 19:36:41 +03:00
|
|
|
}
|
|
|
|
});
|
2020-06-20 15:20:43 +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) {
|
2020-06-23 01:05:56 +03:00
|
|
|
if ((msg.content.key_agreement_protocol == "curve25519-hkdf-sha256") &&
|
2020-06-20 15:20:43 +03:00
|
|
|
(msg.content.hash == "sha256") &&
|
2020-06-23 01:05:56 +03:00
|
|
|
(msg.content.message_authentication_code == "hkdf-hmac-sha256")) {
|
2020-06-20 15:20:43 +03:00
|
|
|
this->commitment = msg.content.commitment;
|
|
|
|
if (std::find(msg.content.short_authentication_string.begin(),
|
|
|
|
msg.content.short_authentication_string.end(),
|
|
|
|
mtx::events::msg::SASMethods::Emoji) !=
|
|
|
|
msg.content.short_authentication_string.end()) {
|
|
|
|
this->method = DeviceVerificationFlow::Method::Emoji;
|
|
|
|
} else {
|
|
|
|
this->method = DeviceVerificationFlow::Method::Decimal;
|
|
|
|
}
|
|
|
|
this->mac_method = msg.content.message_authentication_code;
|
|
|
|
this->sendVerificationKey();
|
|
|
|
} else {
|
|
|
|
this->cancelVerification();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-06-09 19:36:41 +03:00
|
|
|
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) {
|
2020-06-20 15:20:43 +03:00
|
|
|
emit verificationCanceled();
|
2020-06-09 19:36:41 +03:00
|
|
|
}
|
|
|
|
});
|
2020-06-20 15:20:43 +03:00
|
|
|
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) {
|
|
|
|
this->sas->set_their_key(msg.content.key);
|
|
|
|
std::string info;
|
|
|
|
if (this->sender == true) {
|
|
|
|
info = "MATRIX_KEY_VERIFICATION_SAS|" +
|
|
|
|
http::client()->user_id().to_string() + "|" +
|
|
|
|
http::client()->device_id() + "|" +
|
|
|
|
this->sas->public_key() + "|" +
|
|
|
|
this->toClient.to_string() + "|" +
|
|
|
|
this->deviceId.toStdString() + "|" + msg.content.key +
|
|
|
|
"|" + this->transaction_id;
|
|
|
|
} else {
|
|
|
|
info = "MATRIX_KEY_VERIFICATION_SAS|" +
|
|
|
|
this->toClient.to_string() + "|" +
|
|
|
|
this->deviceId.toStdString() + "|" + msg.content.key +
|
|
|
|
"|" + http::client()->user_id().to_string() + "|" +
|
|
|
|
http::client()->device_id() + "|" +
|
|
|
|
this->sas->public_key() + "|" + this->transaction_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->method == DeviceVerificationFlow::Method::Emoji) {
|
|
|
|
this->sasList = this->sas->generate_bytes_emoji(info);
|
|
|
|
} else if (this->method == DeviceVerificationFlow::Method::Decimal) {
|
|
|
|
this->sasList = this->sas->generate_bytes_decimal(info);
|
|
|
|
}
|
|
|
|
if (this->sender == false) {
|
|
|
|
emit this->verificationRequestAccepted(this->method);
|
|
|
|
this->sendVerificationKey();
|
|
|
|
} else {
|
|
|
|
if (this->commitment ==
|
|
|
|
mtx::crypto::bin2base64_unpadded(mtx::crypto::sha256(
|
2020-06-23 01:05:56 +03:00
|
|
|
msg.content.key + this->canonical_json.dump()))) {
|
2020-06-20 15:20:43 +03:00
|
|
|
emit this->verificationRequestAccepted(this->method);
|
|
|
|
} else {
|
|
|
|
this->cancelVerification();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-06-09 19:36:41 +03:00
|
|
|
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) {
|
2020-06-23 01:05:56 +03:00
|
|
|
std::string info =
|
|
|
|
"MATRIX_KEY_VERIFICATION_MAC" + this->toClient.to_string() +
|
|
|
|
this->deviceId.toStdString() +
|
|
|
|
http::client()->user_id().to_string() +
|
|
|
|
http::client()->device_id() + this->transaction_id;
|
|
|
|
|
|
|
|
std::vector<std::string> key_list;
|
|
|
|
std::string key_string;
|
|
|
|
for (auto mac : msg.content.mac) {
|
|
|
|
if (mac.second ==
|
|
|
|
this->sas->calculate_mac(this->device_keys[mac.first],
|
|
|
|
info + mac.first)) {
|
|
|
|
key_string += mac.first;
|
|
|
|
} else {
|
|
|
|
this->cancelVerification();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (msg.content.keys ==
|
|
|
|
this->sas->calculate_mac(key_string, info + "KEY_IDS")) {
|
2020-06-23 20:59:00 +03:00
|
|
|
// uncomment this in future to be compatible with the
|
|
|
|
// MSC2366 this->sendVerificationDone(); and remoeve the
|
|
|
|
// below line
|
2020-06-25 20:08:48 +03:00
|
|
|
if (this->isMacVerified == true)
|
|
|
|
emit this->deviceVerified();
|
|
|
|
else
|
|
|
|
this->isMacVerified = true;
|
2020-06-23 01:05:56 +03:00
|
|
|
} else {
|
|
|
|
this->cancelVerification();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connect(ChatPage::instance(),
|
|
|
|
&ChatPage::recievedDeviceVerificationReady,
|
|
|
|
this,
|
|
|
|
[this](const mtx::events::collections::DeviceEvents &message) {
|
|
|
|
auto msg =
|
|
|
|
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationReady>>(message);
|
|
|
|
if (msg.content.transaction_id == this->transaction_id) {
|
|
|
|
this->startVerificationRequest();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connect(ChatPage::instance(),
|
|
|
|
&ChatPage::recievedDeviceVerificationDone,
|
|
|
|
this,
|
|
|
|
[this](const mtx::events::collections::DeviceEvents &message) {
|
|
|
|
auto msg =
|
|
|
|
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationDone>>(message);
|
|
|
|
if (msg.content.transaction_id == this->transaction_id) {
|
|
|
|
emit this->deviceVerified();
|
2020-06-09 19:36:41 +03:00
|
|
|
}
|
|
|
|
});
|
2020-03-13 23:05:18 +03:00
|
|
|
timeout->start(TIMEOUT);
|
|
|
|
}
|
|
|
|
|
2020-06-17 21:28:35 +03:00
|
|
|
QString
|
|
|
|
DeviceVerificationFlow::getTransactionId()
|
|
|
|
{
|
|
|
|
return QString::fromStdString(this->transaction_id);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-17 21:28:35 +03:00
|
|
|
bool
|
|
|
|
DeviceVerificationFlow::getSender()
|
|
|
|
{
|
|
|
|
return this->sender;
|
|
|
|
}
|
|
|
|
|
2020-06-20 15:20:43 +03:00
|
|
|
std::vector<int>
|
|
|
|
DeviceVerificationFlow::getSasList()
|
|
|
|
{
|
|
|
|
return this->sasList;
|
|
|
|
}
|
|
|
|
|
2020-06-17 21:28:35 +03:00
|
|
|
void
|
|
|
|
DeviceVerificationFlow::setTransactionId(QString transaction_id_)
|
|
|
|
{
|
|
|
|
this->transaction_id = transaction_id_.toStdString();
|
|
|
|
}
|
|
|
|
|
2020-06-07 14:35:32 +03:00
|
|
|
void
|
|
|
|
DeviceVerificationFlow::setUserId(QString userID)
|
|
|
|
{
|
2020-06-17 21:28:35 +03:00
|
|
|
this->userId = userID;
|
2020-06-09 19:36:41 +03:00
|
|
|
this->toClient = mtx::identifiers::parse<mtx::identifiers::User>(userID.toStdString());
|
2020-06-23 01:05:56 +03:00
|
|
|
|
|
|
|
mtx::responses::QueryKeys res;
|
|
|
|
mtx::requests::QueryKeys req;
|
|
|
|
req.device_keys[userID.toStdString()] = {};
|
|
|
|
http::client()->query_keys(
|
|
|
|
req,
|
|
|
|
[user_id = userID.toStdString(), this](const mtx::responses::QueryKeys &res,
|
|
|
|
mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
nhlog::net()->warn("failed to query device keys: {},{}",
|
|
|
|
err->matrix_error.errcode,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto x : res.device_keys) {
|
|
|
|
for (auto y : x.second) {
|
|
|
|
auto z = y.second;
|
|
|
|
if (z.user_id == user_id &&
|
|
|
|
z.device_id == this->deviceId.toStdString()) {
|
|
|
|
for (auto a : z.keys) {
|
|
|
|
// TODO: Verify Signatures
|
|
|
|
this->device_keys[a.first] = a.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
2020-06-17 21:28:35 +03:00
|
|
|
void
|
|
|
|
DeviceVerificationFlow::setSender(bool sender_)
|
|
|
|
{
|
|
|
|
this->sender = sender_;
|
2020-06-20 15:20:43 +03:00
|
|
|
if (this->sender == true)
|
|
|
|
this->transaction_id = http::client()->generate_txn_id();
|
2020-06-17 21:28:35 +03:00
|
|
|
}
|
|
|
|
|
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-17 21:28:35 +03:00
|
|
|
req.key_agreement_protocol = "curve25519-hkdf-sha256";
|
2020-06-07 14:35:32 +03:00
|
|
|
req.hash = "sha256";
|
2020-06-17 21:28:35 +03:00
|
|
|
req.message_authentication_code = "hkdf-hmac-sha256";
|
2020-06-20 15:20:43 +03:00
|
|
|
if (this->method == DeviceVerificationFlow::Method::Emoji)
|
|
|
|
req.short_authentication_string = {mtx::events::msg::SASMethods::Emoji};
|
|
|
|
else if (this->method == DeviceVerificationFlow::Method::Decimal)
|
|
|
|
req.short_authentication_string = {mtx::events::msg::SASMethods::Decimal};
|
|
|
|
req.commitment = mtx::crypto::bin2base64_unpadded(
|
|
|
|
mtx::crypto::sha256(this->sas->public_key() + this->canonical_json.dump()));
|
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>(
|
2020-06-20 15:20:43 +03:00
|
|
|
this->transaction_id, body, [](mtx::http::RequestErr err) {
|
2020-06-07 14:35:32 +03:00
|
|
|
if (err)
|
|
|
|
nhlog::net()->warn("failed to accept verification request: {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
});
|
2020-06-04 16:44:15 +03:00
|
|
|
}
|
2020-06-23 01:05:56 +03:00
|
|
|
//! responds verification request
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::sendVerificationReady()
|
|
|
|
{
|
|
|
|
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationReady> body;
|
|
|
|
mtx::events::msg::KeyVerificationReady req;
|
|
|
|
|
|
|
|
req.from_device = http::client()->device_id();
|
|
|
|
req.transaction_id = this->transaction_id;
|
|
|
|
req.methods = {mtx::events::msg::VerificationMethods::SASv1};
|
|
|
|
|
|
|
|
body[this->toClient][this->deviceId.toStdString()] = req;
|
|
|
|
|
|
|
|
http::client()
|
|
|
|
->send_to_device<mtx::events::msg::KeyVerificationReady,
|
|
|
|
mtx::events::EventType::KeyVerificationReady>(
|
|
|
|
this->transaction_id, body, [](mtx::http::RequestErr err) {
|
|
|
|
if (err)
|
|
|
|
nhlog::net()->warn("failed to send verification ready: {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//! accepts a verification
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::sendVerificationDone()
|
|
|
|
{
|
|
|
|
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationDone> body;
|
|
|
|
mtx::events::msg::KeyVerificationDone req;
|
|
|
|
|
|
|
|
req.transaction_id = this->transaction_id;
|
|
|
|
|
|
|
|
body[this->toClient][this->deviceId.toStdString()] = req;
|
|
|
|
|
|
|
|
http::client()
|
|
|
|
->send_to_device<mtx::events::msg::KeyVerificationDone,
|
|
|
|
mtx::events::EventType::KeyVerificationDone>(
|
|
|
|
this->transaction_id, body, [](mtx::http::RequestErr err) {
|
|
|
|
if (err)
|
|
|
|
nhlog::net()->warn("failed to send verification done: {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
});
|
|
|
|
}
|
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;
|
2020-06-17 21:28:35 +03:00
|
|
|
req.key_agreement_protocols = {"curve25519-hkdf-sha256"};
|
|
|
|
req.hashes = {"sha256"};
|
|
|
|
req.message_authentication_codes = {"hkdf-hmac-sha256", "hmac-sha256"};
|
|
|
|
req.short_authentication_string = {mtx::events::msg::SASMethods::Decimal,
|
|
|
|
mtx::events::msg::SASMethods::Emoji};
|
|
|
|
|
2020-06-07 14:35:32 +03:00
|
|
|
body[this->toClient][this->deviceId.toStdString()] = req;
|
2020-06-20 15:20:43 +03:00
|
|
|
this->canonical_json = nlohmann::json(req);
|
2020-06-07 14:35:32 +03:00
|
|
|
|
|
|
|
http::client()
|
|
|
|
->send_to_device<mtx::events::msg::KeyVerificationStart,
|
|
|
|
mtx::events::EventType::KeyVerificationStart>(
|
2020-06-17 21:28:35 +03:00
|
|
|
this->transaction_id, body, [body](mtx::http::RequestErr err) {
|
2020-06-07 14:35:32 +03:00
|
|
|
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
|
|
|
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>(
|
2020-06-17 21:28:35 +03:00
|
|
|
this->transaction_id, body, [](mtx::http::RequestErr err) {
|
2020-06-07 14:35:32 +03:00
|
|
|
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;
|
|
|
|
|
2020-06-20 15:20:43 +03:00
|
|
|
emit this->verificationCanceled();
|
|
|
|
|
2020-06-07 14:35:32 +03:00
|
|
|
http::client()
|
|
|
|
->send_to_device<mtx::events::msg::KeyVerificationCancel,
|
|
|
|
mtx::events::EventType::KeyVerificationCancel>(
|
2020-06-17 21:28:35 +03:00
|
|
|
this->transaction_id, body, [this](mtx::http::RequestErr err) {
|
2020-06-07 14:35:32 +03:00
|
|
|
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;
|
|
|
|
|
2020-06-20 15:20:43 +03:00
|
|
|
req.key = this->sas->public_key();
|
2020-06-09 19:36:41 +03:00
|
|
|
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>(
|
2020-06-17 21:28:35 +03:00
|
|
|
this->transaction_id, body, [](mtx::http::RequestErr err) {
|
2020-06-09 19:36:41 +03:00
|
|
|
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;
|
|
|
|
|
2020-06-23 01:05:56 +03:00
|
|
|
std::string info = "MATRIX_KEY_VERIFICATION_MAC" + http::client()->user_id().to_string() +
|
|
|
|
http::client()->device_id() + this->toClient.to_string() +
|
|
|
|
this->deviceId.toStdString() + this->transaction_id;
|
|
|
|
|
2020-06-09 19:36:41 +03:00
|
|
|
req.transaction_id = this->transaction_id;
|
2020-06-23 01:05:56 +03:00
|
|
|
//! this vector stores the type of the key and the key
|
|
|
|
std::vector<std::pair<std::string, std::string>> key_list;
|
|
|
|
key_list.push_back(make_pair("ed25519", olm::client()->identity_keys().ed25519));
|
|
|
|
std::sort(key_list.begin(), key_list.end());
|
|
|
|
for (auto x : key_list) {
|
|
|
|
req.mac.insert(
|
|
|
|
std::make_pair(x.first + ":" + http::client()->device_id(),
|
|
|
|
this->sas->calculate_mac(
|
|
|
|
x.second, info + x.first + ":" + http::client()->device_id())));
|
|
|
|
req.keys += x.first + ":" + http::client()->device_id() + ",";
|
|
|
|
}
|
|
|
|
|
|
|
|
req.keys =
|
|
|
|
this->sas->calculate_mac(req.keys.substr(0, req.keys.size() - 1), info + "KEY_IDS");
|
2020-06-09 19:36:41 +03:00
|
|
|
|
|
|
|
body[this->toClient][deviceId.toStdString()] = req;
|
|
|
|
|
|
|
|
http::client()
|
|
|
|
->send_to_device<mtx::events::msg::KeyVerificationMac,
|
|
|
|
mtx::events::EventType::KeyVerificationMac>(
|
2020-06-25 20:08:48 +03:00
|
|
|
this->transaction_id, body, [this](mtx::http::RequestErr err) {
|
2020-06-09 19:36:41 +03:00
|
|
|
if (err)
|
|
|
|
nhlog::net()->warn("failed to send verification MAC: {} {}",
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
2020-06-25 20:08:48 +03:00
|
|
|
|
|
|
|
if (this->isMacVerified == true)
|
|
|
|
emit this->deviceVerified();
|
|
|
|
else
|
|
|
|
this->isMacVerified = true;
|
2020-06-09 19:36:41 +03:00
|
|
|
});
|
|
|
|
}
|
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
|
|
|
}
|