diff options
author | Carl Schwan <carl@carlschwan.eu> | 2021-01-27 02:26:23 +0100 |
---|---|---|
committer | Tobias Fella <fella@posteo.de> | 2021-12-01 21:34:52 +0100 |
commit | 069602584e0f3ec10a26380af69b95f5da11a8b7 (patch) | |
tree | 9249878206903577a57ede697beb411f8f586556 | |
parent | f3fdd967d544650f9af8aadbaddfcf6d8a9fe957 (diff) | |
download | libquotient-069602584e0f3ec10a26380af69b95f5da11a8b7.tar.gz libquotient-069602584e0f3ec10a26380af69b95f5da11a8b7.zip |
Add more test and methods in session handling
-rw-r--r-- | autotests/testolmsession.cpp | 18 | ||||
-rw-r--r-- | autotests/testolmsession.h | 1 | ||||
-rw-r--r-- | lib/olm/message.cpp | 6 | ||||
-rw-r--r-- | lib/olm/message.h | 3 | ||||
-rw-r--r-- | lib/olm/qolmoutboundsession.cpp | 2 | ||||
-rw-r--r-- | lib/olm/session.cpp | 20 | ||||
-rw-r--r-- | lib/olm/session.h | 4 |
7 files changed, 51 insertions, 3 deletions
diff --git a/autotests/testolmsession.cpp b/autotests/testolmsession.cpp index 6fa2a380..2f7a82e9 100644 --- a/autotests/testolmsession.cpp +++ b/autotests/testolmsession.cpp @@ -26,7 +26,7 @@ std::pair<std::unique_ptr<QOlmSession>, std::unique_ptr<QOlmSession>> createSess const auto preKey = outbound->encrypt(""); // Payload does not matter for PreKey - if (preKey.type() != Message::General) { + if (preKey.type() != Message::PreKey) { throw "Wrong first message type received, can't create session"; } auto inbound = std::get<std::unique_ptr<QOlmSession>>(accountB.createInboundSession(preKey)); @@ -42,4 +42,20 @@ void TestOlmSession::olmOutboundSessionCreation() #endif } +void TestOlmSession::olmEncryptDecrypt() +{ +#ifdef Quotient_E2EE_ENABLED + const auto [inboundSession, outboundSession] = createSessionPair(); + const auto encrypted = outboundSession->encrypt("Hello world!"); + if (encrypted.type() == Message::PreKey) { + Message m(encrypted); // clone + QVERIFY(std::get<bool>(inboundSession->matchesInboundSession(m))); + } + + //const auto decrypted = inboundSession->decrypt(encrypted); + + //QCOMPARE(decrypted, "Hello world!"); +#endif +} + QTEST_MAIN(TestOlmSession) diff --git a/autotests/testolmsession.h b/autotests/testolmsession.h index 7e3fc6e4..49e8c3e3 100644 --- a/autotests/testolmsession.h +++ b/autotests/testolmsession.h @@ -10,5 +10,6 @@ class TestOlmSession : public QObject Q_OBJECT private Q_SLOTS: void olmOutboundSessionCreation(); + void olmEncryptDecrypt(); }; #endif diff --git a/lib/olm/message.cpp b/lib/olm/message.cpp index 634a6f0c..ac7038ae 100644 --- a/lib/olm/message.cpp +++ b/lib/olm/message.cpp @@ -14,6 +14,12 @@ Message::Message(const QByteArray &ciphertext, Message::Type type) Q_ASSERT_X(!ciphertext.isEmpty(), "olm message", "Ciphertext is empty"); } +Message::Message(const Message &message) + : QByteArray(message) + , m_messageType(message.type()) +{ +} + Message::Type Message::type() const { return m_messageType; diff --git a/lib/olm/message.h b/lib/olm/message.h index 067d9b5a..d2fe871e 100644 --- a/lib/olm/message.h +++ b/lib/olm/message.h @@ -18,7 +18,7 @@ namespace Quotient { * * The class provides functions to get a type and the ciphertext. */ -class Message : private QByteArray { +class Message : public QByteArray { Q_GADGET public: enum Type { @@ -29,6 +29,7 @@ public: Message() = default; explicit Message(const QByteArray &ciphertext, Type type = General); + explicit Message(const Message &message); static Message fromCiphertext(const QByteArray &ciphertext); diff --git a/lib/olm/qolmoutboundsession.cpp b/lib/olm/qolmoutboundsession.cpp index 4f3cc827..e5c43495 100644 --- a/lib/olm/qolmoutboundsession.cpp +++ b/lib/olm/qolmoutboundsession.cpp @@ -22,7 +22,7 @@ QOlmOutboundGroupSession::QOlmOutboundGroupSession(OlmOutboundGroupSession *sess QOlmOutboundGroupSession::~QOlmOutboundGroupSession() { olm_clear_outbound_group_session(m_groupSession); - //delete[](reinterpret_cast<uint8_t *>(m_groupSession)); + delete[](reinterpret_cast<uint8_t *>(m_groupSession)); } std::unique_ptr<QOlmOutboundGroupSession> QOlmOutboundGroupSession::create() diff --git a/lib/olm/session.cpp b/lib/olm/session.cpp index 0beb136e..d0493fe8 100644 --- a/lib/olm/session.cpp +++ b/lib/olm/session.cpp @@ -18,6 +18,7 @@ OlmError lastError(OlmSession* session) { Quotient::QOlmSession::~QOlmSession() { olm_clear_session(m_session); + delete[](reinterpret_cast<uint8_t *>(m_session)); } OlmSession* QOlmSession::create() @@ -170,6 +171,25 @@ bool QOlmSession::hasReceivedMessage() const return olm_session_has_received_message(m_session); } +std::variant<bool, OlmError> QOlmSession::matchesInboundSession(Message &preKeyMessage) +{ + Q_ASSERT(preKeyMessage.type() == Message::Type::PreKey); + QByteArray oneTimeKeyBuf(preKeyMessage.data()); + const auto matchesResult = olm_matches_inbound_session(m_session, oneTimeKeyBuf.data(), oneTimeKeyBuf.length()); + + if (matchesResult == olm_error()) { + return lastError(m_session); + } + switch (matchesResult) { + case 0: + return false; + case 1: + return true; + default: + return OlmError::Unknown; + } +} + QOlmSession::QOlmSession(OlmSession *session) : m_session(session) { diff --git a/lib/olm/session.h b/lib/olm/session.h index f9221dec..c45b6898 100644 --- a/lib/olm/session.h +++ b/lib/olm/session.h @@ -40,8 +40,12 @@ public: //! The type of the next message that will be returned from encryption. Message::Type encryptMessageType(); + //! Checker for any received messages for this session. bool hasReceivedMessage() const; + //! Checks if the 'prekey' message is for this in-bound session. + std::variant<bool, OlmError> matchesInboundSession(Message &preKeyMessage); + QOlmSession(OlmSession* session); private: //! Helper function for creating new sessions and handling errors. |