Skip to content

Commit

Permalink
Shared secret with decimal and emoji works!
Browse files Browse the repository at this point in the history
  • Loading branch information
Chethan2k1 committed Jun 20, 2020
1 parent 8da77bc commit 30aaf8c
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 83 deletions.
13 changes: 5 additions & 8 deletions resources/qml/TimelineView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,15 @@ Page {
id: deviceVerificationDialog
DeviceVerification {}
}
Component{
id: deviceVerificationFlow
DeviceVerificationFlow {}
}
Connections {
target: timelineManager
onNewDeviceVerificationRequest: {
var newFlow = deviceVerificationFlow.createObject(timelineRoot,
{userId : userId,sender: false,deviceId : deviceId,tranId:transactionId});
deviceVerificationList.add(newFlow.tranId);
flow.userId = userId;
flow.sender = false;
flow.deviceId = deviceId;
flow.tranId = transactionId;
var dialog = deviceVerificationDialog.createObject(timelineRoot,
{flow: newFlow,sender: false});
{flow: flow,sender: false});
dialog.show();
}
}
Expand Down
26 changes: 13 additions & 13 deletions resources/qml/device-verification/DeviceVerification.qml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ ApplicationWindow {
onClicked: {
dialog.close();
flow.cancelVerification();
deviceVerificationList.remove(flow.tranId);
// deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Expand Down Expand Up @@ -135,7 +135,7 @@ ApplicationWindow {
onClicked: {
dialog.close();
flow.cancelVerification();
deviceVerificationList.remove(flow.tranId);
// deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ ApplicationWindow {
onClicked: {
dialog.close();
flow.cancelVerification();
deviceVerificationList.remove(flow.tranId);
// deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Expand Down Expand Up @@ -211,15 +211,15 @@ ApplicationWindow {
Layout.alignment: Qt.AlignHCenter
Label {
font.pixelSize: Qt.application.font.pixelSize * 2
text: "1234"
text: flow.sasList[0]
}
Label {
font.pixelSize: Qt.application.font.pixelSize * 2
text: "1234"
text: flow.sasList[1]
}
Label {
font.pixelSize: Qt.application.font.pixelSize * 2
text: "1234"
text: flow.sasList[2]
}
}

Expand All @@ -230,7 +230,7 @@ ApplicationWindow {
onClicked: {
dialog.close();
flow.cancelVerification();
deviceVerificationList.remove(flow.tranId);
// deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Expand Down Expand Up @@ -345,7 +345,7 @@ ApplicationWindow {
ColumnLayout {
id: col
anchors.bottom: parent.bottom
property var emoji: emojis.mapping[Math.floor(Math.random()*64)]
property var emoji: emojis.mapping[flow.sasList[index]]
Label {
//height: font.pixelSize * 2
Layout.alignment: Qt.AlignHCenter
Expand All @@ -369,7 +369,7 @@ ApplicationWindow {
onClicked: {
dialog.close();
flow.cancelVerification();
deviceVerificationList.remove(flow.tranId);
// deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Expand Down Expand Up @@ -413,7 +413,7 @@ ApplicationWindow {
onClicked: {
dialog.close();
flow.cancelVerification();
deviceVerificationList.remove(flow.tranId);
// deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Expand Down Expand Up @@ -451,7 +451,7 @@ ApplicationWindow {
text: "Close"
onClicked: {
dialog.close()
deviceVerificationList.remove(flow.tranId);
// deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Expand Down Expand Up @@ -486,7 +486,7 @@ ApplicationWindow {
text: "Close"
onClicked: {
dialog.close()
deviceVerificationList.remove(flow.tranId);
// deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Expand Down Expand Up @@ -521,7 +521,7 @@ ApplicationWindow {
text: "Close"
onClicked: {
dialog.close()
deviceVerificationList.remove(flow.tranId);
// deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Expand Down
167 changes: 132 additions & 35 deletions src/DeviceVerificationFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,136 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
{
timeout = new QTimer(this);
timeout->setSingleShot(true);
if (this->sender == true)
this->transaction_id = http::client()->generate_txn_id();
this->sas = olm::client()->sas_init();
connect(timeout, &QTimer::timeout, this, [this]() {
emit timedout();
this->deleteLater();
});

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,
&ChatPage::recievedDeviceVerificationStart,
this,
[this](const mtx::events::collections::DeviceEvents &message) {
auto msg =
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationRequest>>(message);
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationStart>>(message);
if (msg.content.transaction_id == this->transaction_id) {
std::cout << "Recieved Event Request" << std::endl;
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);
} else {
this->cancelVerification();
}
}
});
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) {
if ((msg.content.method ==
mtx::events::msg::VerificationMethods::SASv1) &&
(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"))) {
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();
}
}
});
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;
emit verificationCanceled();
}
});
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(
msg.content.key +
this->canonical_json["content"].dump()))) {
emit this->verificationRequestAccepted(this->method);
} else {
this->cancelVerification();
}
}
}
});
connect(ChatPage::instance(),
&ChatPage::recievedDeviceVerificationMac,
this,
Expand Down Expand Up @@ -104,6 +188,12 @@ DeviceVerificationFlow::getSender()
return this->sender;
}

std::vector<int>
DeviceVerificationFlow::getSasList()
{
return this->sasList;
}

void
DeviceVerificationFlow::setTransactionId(QString transaction_id_)
{
Expand Down Expand Up @@ -133,6 +223,8 @@ void
DeviceVerificationFlow::setSender(bool sender_)
{
this->sender = sender_;
if (this->sender == true)
this->transaction_id = http::client()->generate_txn_id();
}

//! accepts a verification
Expand All @@ -147,23 +239,26 @@ DeviceVerificationFlow::acceptVerificationRequest()
req.key_agreement_protocol = "curve25519-hkdf-sha256";
req.hash = "sha256";
req.message_authentication_code = "hkdf-hmac-sha256";
req.short_authentication_string = {mtx::events::msg::SASMethods::Decimal,
mtx::events::msg::SASMethods::Emoji};
req.commitment = "";

emit this->verificationRequestAccepted(this->method);
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()));

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>(
this->transaction_id, body, [this](mtx::http::RequestErr err) {
this->transaction_id, 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);
});
}
//! starts the verification flow
Expand All @@ -183,6 +278,7 @@ DeviceVerificationFlow::startVerificationRequest()
mtx::events::msg::SASMethods::Emoji};

body[this->toClient][this->deviceId.toStdString()] = req;
this->canonical_json = nlohmann::json(req);

http::client()
->send_to_device<mtx::events::msg::KeyVerificationStart,
Expand All @@ -192,7 +288,6 @@ DeviceVerificationFlow::startVerificationRequest()
nhlog::net()->warn("failed to start verification request: {} {}",
err->matrix_error.error,
static_cast<int>(err->status_code));
std::cout << nlohmann::json(body).dump(2) << std::endl;
});
}
//! sends a verification request
Expand Down Expand Up @@ -236,6 +331,8 @@ DeviceVerificationFlow::cancelVerification()

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

emit this->verificationCanceled();

http::client()
->send_to_device<mtx::events::msg::KeyVerificationCancel,
mtx::events::EventType::KeyVerificationCancel>(
Expand All @@ -254,7 +351,7 @@ DeviceVerificationFlow::sendVerificationKey()
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationKey> body;
mtx::events::msg::KeyVerificationKey req;

req.key = "";
req.key = this->sas->public_key();
req.transaction_id = this->transaction_id;

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

0 comments on commit 30aaf8c

Please sign in to comment.