Skip to content

Commit

Permalink
Add some more slots and mac
Browse files Browse the repository at this point in the history
  • Loading branch information
Chethan2k1 committed Jun 22, 2020
1 parent 30aaf8c commit 614ad76
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 45 deletions.
4 changes: 2 additions & 2 deletions resources/qml/device-verification/DeviceVerification.qml
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ ApplicationWindow {
Button {
Layout.alignment: Qt.AlignRight
text: "They match."
onClicked: { stack.replace(awaitingVerificationConfirmation); flow.acceptDevice(); }
onClicked: { stack.replace(awaitingVerificationConfirmation); flow.sendVerificationMac(); }
}
}
}
Expand Down Expand Up @@ -379,7 +379,7 @@ ApplicationWindow {
Button {
Layout.alignment: Qt.AlignRight
text: "They match."
onClicked: { stack.replace(awaitingVerificationConfirmation); flow.acceptDevice(); }
onClicked: { stack.replace(awaitingVerificationConfirmation); flow.sendVerificationMac(); }
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/ChatPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ public slots:
void recievedDeviceVerificationKey(const mtx::events::collections::DeviceEvents &message);
void recievedDeviceVerificationMac(const mtx::events::collections::DeviceEvents &message);
void recievedDeviceVerificationStart(const mtx::events::collections::DeviceEvents &message);
void recievedDeviceVerificationReady(const mtx::events::collections::DeviceEvents &message);
void recievedDeviceVerificationDone(const mtx::events::collections::DeviceEvents &message);

private slots:
void showUnreadMessageNotification(int count);
Expand Down
166 changes: 130 additions & 36 deletions src/DeviceVerificationFlow.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "DeviceVerificationFlow.h"
#include "ChatPage.h"
#include "Logging.h"
#include "Utils.h"

#include <QDateTime>
#include <QDebug> // only for debugging
Expand Down Expand Up @@ -28,32 +29,27 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
auto msg =
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationStart>>(message);
if (msg.content.transaction_id == this->transaction_id) {
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() &&
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()) &&
(std::find(msg.content.message_authentication_codes.begin(),
msg.content.message_authentication_codes.end(),
"hmac-sha256") !=
msg.content.message_authentication_codes.end() ||
std::find(msg.content.message_authentication_codes.begin(),
msg.content.message_authentication_codes.end(),
"hkdf-hmac-sha256") !=
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->sendVerificationKey(); // Not sure about this maybe
// those optional methods
this->canonical_json = nlohmann::json(msg);
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);
} else {
this->cancelVerification();
}
Expand All @@ -67,12 +63,9 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
auto msg =
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationAccept>>(message);
if (msg.content.transaction_id == this->transaction_id) {
if ((msg.content.method ==
mtx::events::msg::VerificationMethods::SASv1) &&
(msg.content.key_agreement_protocol == "curve25519-hkdf-sha256") &&
if ((msg.content.key_agreement_protocol == "curve25519-hkdf-sha256") &&
(msg.content.hash == "sha256") &&
((msg.content.message_authentication_code == "hkdf-hmac-sha256") ||
(msg.content.message_authentication_code == "hmac-sha256"))) {
(msg.content.message_authentication_code == "hkdf-hmac-sha256")) {
this->commitment = msg.content.commitment;
if (std::find(msg.content.short_authentication_string.begin(),
msg.content.short_authentication_string.end(),
Expand Down Expand Up @@ -136,8 +129,7 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
} else {
if (this->commitment ==
mtx::crypto::bin2base64_unpadded(mtx::crypto::sha256(
msg.content.key +
this->canonical_json["content"].dump()))) {
msg.content.key + this->canonical_json.dump()))) {
emit this->verificationRequestAccepted(this->method);
} else {
this->cancelVerification();
Expand All @@ -151,8 +143,55 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
[this](const mtx::events::collections::DeviceEvents &message) {
auto msg =
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationMac>>(message);
std::cout << "Device verification mac method" << std::endl;
std::cout << nlohmann::json(msg) << std::endl;
if (msg.content.transaction_id == this->transaction_id) {
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;
size_t pos = 0;
while ((pos = msg.content.keys.find(",")) != std::string::npos) {
key_list.push_back(msg.content.keys.substr(0, pos));
msg.content.keys.erase(0, pos + 1);
}
key_list.push_back(msg.content.keys);
int count = 0;
for (auto mac : msg.content.mac) {
if (mac.second ==
this->sas->calculate_mac(key_list[count], info)) {
++count;
} else {
this->cancelVerification();
return;
}
}
std::cout << "Reached here" << std::endl;
this->sendVerificationDone();
}
});
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) {
std::cout << "Recieved Event Mac" << std::endl;
this->startVerificationRequest();
emit this->deviceVerified();
}
});
timeout->start(TIMEOUT);
Expand Down Expand Up @@ -248,9 +287,6 @@ DeviceVerificationFlow::acceptVerificationRequest()

body[this->toClient][this->deviceId.toStdString()] = req;

std::cout << "Accepting the Verification" << std::endl;
std::cout << json(body) << std::endl;

http::client()
->send_to_device<mtx::events::msg::KeyVerificationAccept,
mtx::events::EventType::KeyVerificationAccept>(
Expand All @@ -261,6 +297,50 @@ DeviceVerificationFlow::acceptVerificationRequest()
static_cast<int>(err->status_code));
});
}
//! 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));
});
}
//! starts the verification flow
void
DeviceVerificationFlow::startVerificationRequest()
Expand Down Expand Up @@ -373,11 +453,25 @@ DeviceVerificationFlow::sendVerificationMac()
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationMac> body;
mtx::events::msg::KeyVerificationMac req;

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;

req.transaction_id = this->transaction_id;
// req.mac = "";
req.keys = "";
//! 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));

for (auto x : key_list) {
req.mac.insert(std::make_pair(x.first + ":" + http::client()->device_id(),
this->sas->calculate_mac(x.second, info)));
req.keys += x.second + ",";
}

req.keys = req.keys.substr(0, req.keys.size() - 1);

body[this->toClient][deviceId.toStdString()] = req;
std::cout << json(body) << std::endl;

http::client()
->send_to_device<mtx::events::msg::KeyVerificationMac,
Expand Down
4 changes: 4 additions & 0 deletions src/DeviceVerificationFlow.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class DeviceVerificationFlow : public QObject
public slots:
//! 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
Expand Down
11 changes: 4 additions & 7 deletions src/Olm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "MatrixClient.h"
#include "Utils.h"
#include <DeviceVerificationFlow.h>
#include <iostream> // only for debugging

static const std::string STORAGE_SECRET_KEY("secret");
constexpr auto MEGOLM_ALGO = "m.megolm.v1.aes-sha2";
Expand Down Expand Up @@ -79,22 +78,20 @@ handle_to_device_messages(const std::vector<mtx::events::collections::DeviceEven
}
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationAccept)) {
ChatPage::instance()->recievedDeviceVerificationAccept(msg);
std::cout << j_msg.dump(2) << std::endl;
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationRequest)) {
ChatPage::instance()->recievedDeviceVerificationRequest(msg);
std::cout << j_msg.dump(2) << std::endl;
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationCancel)) {
ChatPage::instance()->recievedDeviceVerificationCancel(msg);
std::cout << j_msg.dump(2) << std::endl;
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationKey)) {
ChatPage::instance()->recievedDeviceVerificationKey(msg);
std::cout << j_msg.dump(2) << std::endl;
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationMac)) {
ChatPage::instance()->recievedDeviceVerificationMac(msg);
std::cout << j_msg.dump(2) << std::endl;
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationStart)) {
ChatPage::instance()->recievedDeviceVerificationStart(msg);
std::cout << j_msg.dump(2) << std::endl;
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationReady)) {
ChatPage::instance()->recievedDeviceVerificationReady(msg);
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationDone)) {
ChatPage::instance()->recievedDeviceVerificationDone(msg);
} else {
nhlog::crypto()->warn("unhandled event: {}", j_msg.dump(2));
}
Expand Down
1 change: 1 addition & 0 deletions src/timeline/TimelineViewManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ TimelineViewManager::TimelineViewManager(QSharedPointer<UserSettings> userSettin
msg.content.methods.end(),
mtx::events::msg::VerificationMethods::SASv1) !=
msg.content.methods.end()) {
flow->sendVerificationReady();
emit newDeviceVerificationRequest(
std::move(flow),
QString::fromStdString(msg.content.transaction_id),
Expand Down

0 comments on commit 614ad76

Please sign in to comment.