aboutsummaryrefslogtreecommitdiff
path: root/lib/keyverificationsession.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/keyverificationsession.cpp')
-rw-r--r--lib/keyverificationsession.cpp133
1 files changed, 50 insertions, 83 deletions
diff --git a/lib/keyverificationsession.cpp b/lib/keyverificationsession.cpp
index 24fc08e1..cc4428d7 100644
--- a/lib/keyverificationsession.cpp
+++ b/lib/keyverificationsession.cpp
@@ -68,42 +68,6 @@ KeyVerificationSession::KeyVerificationSession(QString userId, QString deviceId,
void KeyVerificationSession::init(milliseconds timeout)
{
- connect(m_connection, &Connection::incomingKeyVerificationReady, this, [this](const KeyVerificationReadyEvent& event) {
- if (event.transactionId() == m_transactionId && event.fromDevice() == m_remoteDeviceId) {
- handleReady(event);
- }
- });
- connect(m_connection, &Connection::incomingKeyVerificationStart, this, [this](const KeyVerificationStartEvent& event) {
- if (event.transactionId() == m_transactionId && event.fromDevice() == m_remoteDeviceId) {
- handleStart(event);
- }
- });
- connect(m_connection, &Connection::incomingKeyVerificationAccept, this, [this](const KeyVerificationAcceptEvent& event) {
- if (event.transactionId() == m_transactionId) {
- handleAccept(event);
- }
- });
- connect(m_connection, &Connection::incomingKeyVerificationKey, this, [this](const KeyVerificationKeyEvent& event) {
- if (event.transactionId() == m_transactionId) {
- handleKey(event);
- }
- });
- connect(m_connection, &Connection::incomingKeyVerificationMac, this, [this](const KeyVerificationMacEvent& event) {
- if (event.transactionId() == m_transactionId) {
- handleMac(event);
- }
- });
- connect(m_connection, &Connection::incomingKeyVerificationDone, this, [this](const KeyVerificationDoneEvent& event) {
- if (event.transactionId() == m_transactionId) {
- handleDone(event);
- }
- });
- connect(m_connection, &Connection::incomingKeyVerificationCancel, this, [this](const KeyVerificationCancelEvent& event) {
- if (event.transactionId() == m_transactionId) {
- handleCancel(event);
- }
- });
-
QTimer::singleShot(timeout, this, [this] { cancelVerification(TIMEOUT); });
m_sas = olm_sas(new std::byte[olm_sas_size()]);
@@ -118,6 +82,53 @@ KeyVerificationSession::~KeyVerificationSession()
delete[] reinterpret_cast<std::byte*>(m_sas);
}
+void KeyVerificationSession::handleEvent(const KeyVerificationEvent& baseEvent)
+{
+ if (!switchOnType(
+ baseEvent,
+ [this](const KeyVerificationCancelEvent& event) {
+ setError(stringToError(event.code()));
+ setState(CANCELED);
+ return true;
+ },
+ [this](const KeyVerificationStartEvent& event) {
+ if (state() != WAITINGFORREADY && state() != READY)
+ return false;
+ handleStart(event);
+ return true;
+ },
+ [this](const KeyVerificationReadyEvent& event) {
+ if (state() == WAITINGFORREADY)
+ handleReady(event);
+ // ACCEPTED is also fine here because it's possible to receive
+ // ready and start in the same sync, in which case start might
+ // be handled before ready.
+ return state() == WAITINGFORREADY || state() == ACCEPTED;
+ },
+ [this](const KeyVerificationAcceptEvent& event) {
+ if (state() != WAITINGFORACCEPT)
+ return false;
+ m_commitment = event.commitment();
+ sendKey();
+ setState(WAITINGFORKEY);
+ return true;
+ },
+ [this](const KeyVerificationKeyEvent& event) {
+ if (state() != ACCEPTED && state() != WAITINGFORKEY)
+ return false;
+ handleKey(event);
+ return true;
+ },
+ [this](const KeyVerificationMacEvent& event) {
+ if (state() != WAITINGFORMAC)
+ return false;
+ handleMac(event);
+ return true;
+ },
+ [this](const KeyVerificationDoneEvent&) { return state() == DONE; }))
+ cancelVerification(UNEXPECTED_MESSAGE);
+}
+
struct EmojiStoreEntry : EmojiEntry {
QHash<QString, QString> translatedDescriptions;
@@ -154,10 +165,6 @@ EmojiEntry emojiForCode(int code, const QString& language)
void KeyVerificationSession::handleKey(const KeyVerificationKeyEvent& event)
{
- if (state() != WAITINGFORKEY && state() != ACCEPTED) {
- cancelVerification(UNEXPECTED_MESSAGE);
- return;
- }
auto eventKey = event.key().toLatin1();
olm_sas_set_their_key(m_sas, eventKey.data(), eventKey.size());
@@ -314,34 +321,18 @@ void KeyVerificationSession::sendStartSas()
void KeyVerificationSession::handleReady(const KeyVerificationReadyEvent& event)
{
- if (state() == ACCEPTED) {
- // It's possible to receive ready and start in the same sync, in which case start might be handled before ready.
- return;
- }
- if (state() != WAITINGFORREADY) {
- cancelVerification(UNEXPECTED_MESSAGE);
- return;
- }
setState(READY);
m_remoteSupportedMethods = event.methods();
auto methods = commonSupportedMethods(m_remoteSupportedMethods);
- if (methods.isEmpty()) {
+ if (methods.isEmpty())
cancelVerification(UNKNOWN_METHOD);
- return;
- }
-
- if (methods.size() == 1) {
- sendStartSas();
- }
+ else if (methods.size() == 1)
+ sendStartSas(); // -> WAITINGFORACCEPT
}
void KeyVerificationSession::handleStart(const KeyVerificationStartEvent& event)
{
- if (state() != READY && state() != WAITINGFORREADY) {
- cancelVerification(UNEXPECTED_MESSAGE);
- return;
- }
if (startSentByUs) {
if (m_remoteUserId > m_connection->userId() || (m_remoteUserId == m_connection->userId() && m_remoteDeviceId > m_connection->deviceId())) {
return;
@@ -362,17 +353,6 @@ void KeyVerificationSession::handleStart(const KeyVerificationStartEvent& event)
setState(ACCEPTED);
}
-void KeyVerificationSession::handleAccept(const KeyVerificationAcceptEvent& event)
-{
- if(state() != WAITINGFORACCEPT) {
- cancelVerification(UNEXPECTED_MESSAGE);
- return;
- }
- m_commitment = event.commitment();
- sendKey();
- setState(WAITINGFORKEY);
-}
-
void KeyVerificationSession::handleMac(const KeyVerificationMacEvent& event)
{
QStringList keys = event.mac().keys();
@@ -402,19 +382,6 @@ void KeyVerificationSession::handleMac(const KeyVerificationMacEvent& event)
}
}
-void KeyVerificationSession::handleDone(const KeyVerificationDoneEvent&)
-{
- if (state() != DONE) {
- cancelVerification(UNEXPECTED_MESSAGE);
- }
-}
-
-void KeyVerificationSession::handleCancel(const KeyVerificationCancelEvent& event)
-{
- setError(stringToError(event.code()));
- setState(CANCELED);
-}
-
QVector<EmojiEntry> KeyVerificationSession::sasEmojis() const
{
return m_sasEmojis;