aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-05-15 22:08:09 +0200
committerAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-05-16 10:42:10 +0200
commita3486fd0e9786c47564ce8173a2e4039135b10f9 (patch)
tree15a9eb75e4007b078b5e01bd57f3c2e50cbf4f0b
parent1fe8dc00de4d9bb7072ec9677ec7f8e73e4fc769 (diff)
downloadlibquotient-a3486fd0e9786c47564ce8173a2e4039135b10f9.tar.gz
libquotient-a3486fd0e9786c47564ce8173a2e4039135b10f9.zip
Simplify QOlmSession::matchesInboundSession*()
There's no particular use in letting `QOlmError` out, only to confirm that, well, `QOlmError` is just another form of no-match.
-rw-r--r--autotests/testolmsession.cpp2
-rw-r--r--lib/connection.cpp3
-rw-r--r--lib/e2ee/qolmsession.cpp43
3 files changed, 19 insertions, 29 deletions
diff --git a/autotests/testolmsession.cpp b/autotests/testolmsession.cpp
index 5436c392..2674b60b 100644
--- a/autotests/testolmsession.cpp
+++ b/autotests/testolmsession.cpp
@@ -45,7 +45,7 @@ void TestOlmSession::olmEncryptDecrypt()
const auto encrypted = outboundSession->encrypt("Hello world!");
if (encrypted.type() == QOlmMessage::PreKey) {
QOlmMessage m(encrypted); // clone
- QVERIFY(std::get<bool>(inboundSession->matchesInboundSession(m)));
+ QVERIFY(inboundSession->matchesInboundSession(m));
}
const auto decrypted = std::get<QString>(inboundSession->decrypt(encrypted));
diff --git a/lib/connection.cpp b/lib/connection.cpp
index 511b64e2..955b5b1a 100644
--- a/lib/connection.cpp
+++ b/lib/connection.cpp
@@ -226,8 +226,7 @@ public:
{
Q_ASSERT(message.type() == QOlmMessage::PreKey);
for(auto& session : olmSessions[senderKey]) {
- const auto matches = session->matchesInboundSessionFrom(senderKey, message);
- if(std::holds_alternative<bool>(matches) && std::get<bool>(matches)) {
+ if (session->matchesInboundSessionFrom(senderKey, message)) {
qCDebug(E2EE) << "Found inbound session";
const auto result = session->decrypt(message);
if(std::holds_alternative<QString>(result)) {
diff --git a/lib/e2ee/qolmsession.cpp b/lib/e2ee/qolmsession.cpp
index 531a6696..5ccbfd40 100644
--- a/lib/e2ee/qolmsession.cpp
+++ b/lib/e2ee/qolmsession.cpp
@@ -209,42 +209,33 @@ bool QOlmSession::hasReceivedMessage() const
return olm_session_has_received_message(m_session);
}
-std::variant<bool, QOlmError> QOlmSession::matchesInboundSession(const QOlmMessage &preKeyMessage) const
+bool QOlmSession::matchesInboundSession(const QOlmMessage& preKeyMessage) const
{
Q_ASSERT(preKeyMessage.type() == QOlmMessage::Type::PreKey);
QByteArray oneTimeKeyBuf(preKeyMessage.data());
- const auto matchesResult = olm_matches_inbound_session(m_session, oneTimeKeyBuf.data(), oneTimeKeyBuf.length());
+ const auto maybeMatches =
+ olm_matches_inbound_session(m_session, oneTimeKeyBuf.data(),
+ oneTimeKeyBuf.length());
- if (matchesResult == olm_error()) {
+ if (maybeMatches == olm_error()) {
return lastError(m_session);
}
- switch (matchesResult) {
- case 0:
- return false;
- case 1:
- return true;
- default:
- return QOlmError::Unknown;
- }
+ return maybeMatches == 1;
}
-std::variant<bool, QOlmError> QOlmSession::matchesInboundSessionFrom(const QString &theirIdentityKey, const QOlmMessage &preKeyMessage) const
+
+bool QOlmSession::matchesInboundSessionFrom(
+ const QString& theirIdentityKey, const QOlmMessage& preKeyMessage) const
{
const auto theirIdentityKeyBuf = theirIdentityKey.toUtf8();
auto oneTimeKeyMessageBuf = preKeyMessage.toCiphertext();
- const auto error = olm_matches_inbound_session_from(m_session, theirIdentityKeyBuf.data(), theirIdentityKeyBuf.length(),
- oneTimeKeyMessageBuf.data(), oneTimeKeyMessageBuf.length());
-
- if (error == olm_error()) {
- return lastError(m_session);
- }
- switch (error) {
- case 0:
- return false;
- case 1:
- return true;
- default:
- return QOlmError::Unknown;
- }
+ const auto maybeMatches = olm_matches_inbound_session_from(
+ m_session, theirIdentityKeyBuf.data(), theirIdentityKeyBuf.length(),
+ oneTimeKeyMessageBuf.data(), oneTimeKeyMessageBuf.length());
+
+ if (maybeMatches == olm_error())
+ qCWarning(E2EE) << "Error matching an inbound session:"
+ << olm_session_last_error(m_session);
+ return maybeMatches == 1;
}
QOlmSession::QOlmSession(OlmSession *session)