From 94d1bb2c624ca9a689a5d7cdca8af6ef5e489150 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Thu, 22 Sep 2022 20:41:26 +0200 Subject: Don't crash in MxcReply if the event is not a RoomMessageEvent --- lib/mxcreply.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/mxcreply.cpp b/lib/mxcreply.cpp index c7547be8..ce833b98 100644 --- a/lib/mxcreply.cpp +++ b/lib/mxcreply.cpp @@ -63,10 +63,11 @@ MxcReply::MxcReply(QNetworkReply* reply, Room* room, const QString &eventId) #ifdef Quotient_E2EE_ENABLED auto eventIt = room->findInTimeline(eventId); if(eventIt != room->historyEdge()) { - auto event = eventIt->viewAs(); - if (auto* efm = std::get_if( - &event->content()->fileInfo()->source)) - d->m_encryptedFile = *efm; + if (auto event = eventIt->viewAs()) { + if (auto* efm = std::get_if( + &event->content()->fileInfo()->source)) + d->m_encryptedFile = *efm; + } } #endif } -- cgit v1.2.3 From f161519740da12c2578d6ecf79a501e10c2debae Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Sat, 24 Sep 2022 11:36:44 +0200 Subject: Fix construction of EncryptedEvents The parent constructor requires full json instead of content json now --- lib/events/encryptedevent.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/events/encryptedevent.cpp b/lib/events/encryptedevent.cpp index 94b44901..9ef3b22a 100644 --- a/lib/events/encryptedevent.cpp +++ b/lib/events/encryptedevent.cpp @@ -8,20 +8,28 @@ using namespace Quotient; EncryptedEvent::EncryptedEvent(const QJsonObject& ciphertexts, const QString& senderKey) - : RoomEvent({ { AlgorithmKeyL, OlmV1Curve25519AesSha2AlgoKey }, - { CiphertextKeyL, ciphertexts }, - { SenderKeyKeyL, senderKey } }) + : RoomEvent({ + {"content", QJsonObject{ + { AlgorithmKeyL, OlmV1Curve25519AesSha2AlgoKey }, + { CiphertextKeyL, ciphertexts }, + { SenderKeyKeyL, senderKey } + }}, + {TypeKeyL, "m.room.encrypted"} + }) {} EncryptedEvent::EncryptedEvent(const QByteArray& ciphertext, const QString& senderKey, const QString& deviceId, const QString& sessionId) : RoomEvent({ - { AlgorithmKeyL, MegolmV1AesSha2AlgoKey }, - { CiphertextKeyL, QString(ciphertext) }, - { DeviceIdKeyL, deviceId }, - { SenderKeyKeyL, senderKey }, - { SessionIdKeyL, sessionId }, + {"content", QJsonObject{ + { AlgorithmKeyL, MegolmV1AesSha2AlgoKey }, + { CiphertextKeyL, QString(ciphertext) }, + { DeviceIdKeyL, deviceId }, + { SenderKeyKeyL, senderKey }, + { SessionIdKeyL, sessionId }, + }}, + {TypeKeyL, "m.room.encrypted"} }) {} -- cgit v1.2.3 From a2b155b10197a4fca4db2af59c2171f82a0eac2b Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Sat, 24 Sep 2022 12:15:54 +0200 Subject: Fix verification Contains two fixes: - When receiving the mac, we can also be in WAITINGFORVERIFICATION state - Ignore all KeyVerificationDone events; we don't do anything with them anyway and sometimes receive them after the session is destructed --- lib/connection.cpp | 4 ++++ lib/keyverificationsession.cpp | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/connection.cpp b/lib/connection.cpp index 8ca76ceb..307c3840 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -1006,6 +1006,10 @@ bool Connection::Private::processIfVerificationEvent(const Event& evt, emit q->newKeyVerificationSession(*sessionIter); return true; }, + [](const KeyVerificationDoneEvent& doneEvt) { + Q_UNUSED(doneEvt) + return true; + }, [this](const KeyVerificationEvent& kvEvt) { if (auto* const session = verificationSessions.value(kvEvt.transactionId())) { diff --git a/lib/keyverificationsession.cpp b/lib/keyverificationsession.cpp index 3f76eac1..f5d49561 100644 --- a/lib/keyverificationsession.cpp +++ b/lib/keyverificationsession.cpp @@ -120,7 +120,7 @@ void KeyVerificationSession::handleEvent(const KeyVerificationEvent& baseEvent) return true; }, [this](const KeyVerificationMacEvent& event) { - if (state() != WAITINGFORMAC) + if (state() != WAITINGFORMAC && state() != WAITINGFORVERIFICATION) return false; handleMac(event); return true; -- cgit v1.2.3 From 4146f33bda60d04db34fbd5614439b22b5d5837f Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Sat, 24 Sep 2022 14:27:16 +0200 Subject: Use basicJson() --- lib/events/encryptedevent.cpp | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) (limited to 'lib') diff --git a/lib/events/encryptedevent.cpp b/lib/events/encryptedevent.cpp index 9ef3b22a..49df25c8 100644 --- a/lib/events/encryptedevent.cpp +++ b/lib/events/encryptedevent.cpp @@ -8,29 +8,19 @@ using namespace Quotient; EncryptedEvent::EncryptedEvent(const QJsonObject& ciphertexts, const QString& senderKey) - : RoomEvent({ - {"content", QJsonObject{ - { AlgorithmKeyL, OlmV1Curve25519AesSha2AlgoKey }, - { CiphertextKeyL, ciphertexts }, - { SenderKeyKeyL, senderKey } - }}, - {TypeKeyL, "m.room.encrypted"} - }) + : RoomEvent(basicJson(TypeId, { { AlgorithmKeyL, OlmV1Curve25519AesSha2AlgoKey }, + { CiphertextKeyL, ciphertexts }, + { SenderKeyKeyL, senderKey } })) {} EncryptedEvent::EncryptedEvent(const QByteArray& ciphertext, const QString& senderKey, const QString& deviceId, const QString& sessionId) - : RoomEvent({ - {"content", QJsonObject{ - { AlgorithmKeyL, MegolmV1AesSha2AlgoKey }, - { CiphertextKeyL, QString(ciphertext) }, - { DeviceIdKeyL, deviceId }, - { SenderKeyKeyL, senderKey }, - { SessionIdKeyL, sessionId }, - }}, - {TypeKeyL, "m.room.encrypted"} - }) + : RoomEvent(basicJson(TypeId, { { AlgorithmKeyL, MegolmV1AesSha2AlgoKey }, + { CiphertextKeyL, QString(ciphertext) }, + { DeviceIdKeyL, deviceId }, + { SenderKeyKeyL, senderKey }, + { SessionIdKeyL, sessionId } })) {} EncryptedEvent::EncryptedEvent(const QJsonObject& obj) -- cgit v1.2.3 From 63d658e798c1ba29d080564db06a613a3d7d5df5 Mon Sep 17 00:00:00 2001 From: Tobias Fella <9750016+TobiasFella@users.noreply.github.com> Date: Sat, 24 Sep 2022 16:22:05 +0200 Subject: Update lib/connection.cpp Co-authored-by: Alexey Rusakov --- lib/connection.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/connection.cpp b/lib/connection.cpp index 307c3840..1048884f 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -1006,8 +1006,7 @@ bool Connection::Private::processIfVerificationEvent(const Event& evt, emit q->newKeyVerificationSession(*sessionIter); return true; }, - [](const KeyVerificationDoneEvent& doneEvt) { - Q_UNUSED(doneEvt) + [](const KeyVerificationDoneEvent&) { return true; }, [this](const KeyVerificationEvent& kvEvt) { -- cgit v1.2.3 From 5904a61c59f0eef00aef07ef998658fd791ff139 Mon Sep 17 00:00:00 2001 From: Alexey Rusakov Date: Sun, 25 Sep 2022 21:37:49 +0200 Subject: QOlmUtility::ed25519Verify: just return bool It's too easy to incorrectly test the previous return type. --- lib/e2ee/qolmaccount.cpp | 3 +-- lib/e2ee/qolmutility.cpp | 23 +++++------------------ lib/e2ee/qolmutility.h | 4 ++-- 3 files changed, 8 insertions(+), 22 deletions(-) (limited to 'lib') diff --git a/lib/e2ee/qolmaccount.cpp b/lib/e2ee/qolmaccount.cpp index ccb191f4..1b04dae7 100644 --- a/lib/e2ee/qolmaccount.cpp +++ b/lib/e2ee/qolmaccount.cpp @@ -264,6 +264,5 @@ bool Quotient::ed25519VerifySignature(const QString& signingKey, QByteArray signingKeyBuf = signingKey.toUtf8(); QOlmUtility utility; auto signatureBuf = signature.toUtf8(); - return utility.ed25519Verify(signingKeyBuf, canonicalJson, signatureBuf) - .value_or(false); + return utility.ed25519Verify(signingKeyBuf, canonicalJson, signatureBuf); } diff --git a/lib/e2ee/qolmutility.cpp b/lib/e2ee/qolmutility.cpp index 84559085..22f9ec0d 100644 --- a/lib/e2ee/qolmutility.cpp +++ b/lib/e2ee/qolmutility.cpp @@ -40,23 +40,10 @@ QString QOlmUtility::sha256Utf8Msg(const QString &message) const return sha256Bytes(message.toUtf8()); } -QOlmExpected QOlmUtility::ed25519Verify(const QByteArray& key, - const QByteArray& message, - const QByteArray& signature) +bool QOlmUtility::ed25519Verify(const QByteArray& key, const QByteArray& message, + QByteArray signature) { - QByteArray signatureBuf(signature.length(), '0'); - std::copy(signature.begin(), signature.end(), signatureBuf.begin()); - - const auto ret = olm_ed25519_verify(m_utility, key.data(), key.size(), - message.data(), message.size(), (void *)signatureBuf.data(), signatureBuf.size()); - - if (ret == olm_error()) { - auto error = lastError(m_utility); - if (error == QOlmError::BadMessageMac) { - return false; - } - return error; - } - - return !ret; // ret == 0 means success + return olm_ed25519_verify(m_utility, key.data(), key.size(), message.data(), + message.size(), signature.data(), signature.size()) + == 0; } diff --git a/lib/e2ee/qolmutility.h b/lib/e2ee/qolmutility.h index 5f6bcdc5..6c1c8624 100644 --- a/lib/e2ee/qolmutility.h +++ b/lib/e2ee/qolmutility.h @@ -29,8 +29,8 @@ public: //! \param key QByteArray The public part of the ed25519 key that signed the message. //! \param message QByteArray The message that was signed. //! \param signature QByteArray The signature of the message. - QOlmExpected ed25519Verify(const QByteArray &key, - const QByteArray &message, const QByteArray &signature); + bool ed25519Verify(const QByteArray &key, + const QByteArray &message, QByteArray signature); private: OlmUtility *m_utility; -- cgit v1.2.3