aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--autotests/testgroupsession.cpp27
-rw-r--r--autotests/testgroupsession.h1
-rw-r--r--lib/olm/qolminboundsession.cpp19
-rw-r--r--lib/olm/qolminboundsession.h4
-rw-r--r--lib/olm/qolmoutboundsession.cpp6
-rw-r--r--lib/olm/qolmoutboundsession.h7
6 files changed, 47 insertions, 17 deletions
diff --git a/autotests/testgroupsession.cpp b/autotests/testgroupsession.cpp
index 1cfe38a9..a99172d7 100644
--- a/autotests/testgroupsession.cpp
+++ b/autotests/testgroupsession.cpp
@@ -13,13 +13,13 @@ using namespace Quotient;
void TestOlmSession::groupSessionPicklingValid()
{
auto ogs = QOlmOutboundGroupSession::create();
- const auto ogsId = std::get<QByteArray>(ogs->sessionId());
+ const auto ogsId = ogs->sessionId();
QVERIFY(QByteArray::fromBase64Encoding(ogsId).decodingStatus == QByteArray::Base64DecodingStatus::Ok);
QCOMPARE(0, ogs->sessionMessageIndex());
auto ogsPickled = std::get<QByteArray>(ogs->pickle(Unencrypted {}));
- auto ogs2 = std::get<std::unique_ptr<QOlmOutboundGroupSession>>(QOlmOutboundGroupSession::unpickle(ogsPickled, Unencrypted {}));
- QCOMPARE(ogsId, std::get<QByteArray>(ogs2->sessionId()));
+ auto ogs2 = std::get<QOlmOutboundGroupSessionPtr>(QOlmOutboundGroupSession::unpickle(ogsPickled, Unencrypted {}));
+ QCOMPARE(ogsId, ogs2->sessionId());
auto igs = QOlmInboundGroupSession::create(std::get<QByteArray>(ogs->sessionKey()));
const auto igsId = igs->sessionId();
@@ -30,9 +30,28 @@ void TestOlmSession::groupSessionPicklingValid()
QCOMPARE(0, igs->firstKnownIndex());
auto igsPickled = igs->pickle(Unencrypted {});
- igs = std::get<std::unique_ptr<QOlmInboundGroupSession>>(QOlmInboundGroupSession::unpickle(igsPickled, Unencrypted {}));
+ igs = std::get<QOlmInboundGroupSessionPtr>(QOlmInboundGroupSession::unpickle(igsPickled, Unencrypted {}));
QCOMPARE(igsId, igs->sessionId());
}
+void TestOlmSession::groupSessionCryptoValid()
+{
+ auto ogs = QOlmOutboundGroupSession::create();
+ auto igs = QOlmInboundGroupSession::create(std::get<QByteArray>(ogs->sessionKey()));
+ QCOMPARE(ogs->sessionId(), igs->sessionId());
+
+ const auto plainText = QStringLiteral("Hello world!");
+ const auto ciphertext = std::get<QByteArray>(ogs->encrypt(plainText));
+ qDebug() << ciphertext;
+ // ciphertext valid base64?
+ QVERIFY(QByteArray::fromBase64Encoding(ciphertext).decodingStatus == QByteArray::Base64DecodingStatus::Ok);
+
+ const auto decryptionResult = std::get<std::pair<QString, uint32_t>>(igs->decrypt(ciphertext));
+
+ //// correct plaintext?
+ QCOMPARE(plainText, decryptionResult.first);
+
+ QCOMPARE(0, decryptionResult.second);
+}
QTEST_MAIN(TestOlmSession)
#endif
diff --git a/autotests/testgroupsession.h b/autotests/testgroupsession.h
index 28ebf4c9..c9192990 100644
--- a/autotests/testgroupsession.h
+++ b/autotests/testgroupsession.h
@@ -11,5 +11,6 @@ class TestOlmSession : public QObject
private Q_SLOTS:
void groupSessionPicklingValid();
+ void groupSessionCryptoValid();
};
#endif
diff --git a/lib/olm/qolminboundsession.cpp b/lib/olm/qolminboundsession.cpp
index d3b98a63..11558f51 100644
--- a/lib/olm/qolminboundsession.cpp
+++ b/lib/olm/qolminboundsession.cpp
@@ -5,6 +5,7 @@
#ifdef Quotient_E2EE_ENABLED
#include "olm/qolminboundsession.h"
#include <iostream>
+#include <cstring>
using namespace Quotient;
OlmError lastError(OlmInboundGroupSession *session) {
@@ -89,22 +90,24 @@ std::variant<std::unique_ptr<QOlmInboundGroupSession>, OlmError> QOlmInboundGrou
return std::make_unique<QOlmInboundGroupSession>(groupSession);
}
-std::variant<std::pair<QString, uint32_t>, OlmError> QOlmInboundGroupSession::decrypt(QString &message)
+std::variant<std::pair<QString, uint32_t>, OlmError> QOlmInboundGroupSession::decrypt(const QByteArray &message)
{
// This is for capturing the output of olm_group_decrypt
uint32_t messageIndex = 0;
// We need to clone the message because
// olm_decrypt_max_plaintext_length destroys the input buffer
- QByteArray messageBuf = message.toUtf8();
+ QByteArray messageBuf(message.length(), '0');
+ std::copy(message.begin(), message.end(), messageBuf.begin());
QByteArray plaintextBuf(olm_group_decrypt_max_plaintext_length(m_groupSession,
reinterpret_cast<uint8_t *>(messageBuf.data()), messageBuf.length()), '0');
- const auto messageLen = messageBuf.length();
- const auto plaintextMaxLen = plaintextBuf.length();
+
+ messageBuf = QByteArray(message.length(), '0');
+ std::copy(message.begin(), message.end(), messageBuf.begin());
const auto plaintextLen = olm_group_decrypt(m_groupSession, reinterpret_cast<uint8_t *>(messageBuf.data()),
- messageLen, reinterpret_cast<uint8_t *>(plaintextBuf.data()), plaintextMaxLen, &messageIndex);
+ messageBuf.length(), reinterpret_cast<uint8_t *>(plaintextBuf.data()), plaintextBuf.length(), &messageIndex);
// Error code or plaintext length is returned
const auto decryptError = plaintextLen;
@@ -113,8 +116,10 @@ std::variant<std::pair<QString, uint32_t>, OlmError> QOlmInboundGroupSession::de
return lastError(m_groupSession);
}
- plaintextBuf.truncate(plaintextLen);
- return std::make_pair<QString, qint32>(QString(plaintextBuf), messageIndex);
+ QByteArray output(plaintextLen, '0');
+ std::memcpy(output.data(), plaintextBuf.data(), plaintextLen);
+
+ return std::make_pair<QString, qint32>(QString(output), messageIndex);
}
std::variant<QByteArray, OlmError> QOlmInboundGroupSession::exportSession(uint32_t messageIndex)
diff --git a/lib/olm/qolminboundsession.h b/lib/olm/qolminboundsession.h
index ccc53ba8..eb698868 100644
--- a/lib/olm/qolminboundsession.h
+++ b/lib/olm/qolminboundsession.h
@@ -31,7 +31,7 @@ public:
//! an `OlmInboundGroupSession`.
static std::variant<std::unique_ptr<QOlmInboundGroupSession>, OlmError> unpickle(QByteArray &picked, const PicklingMode &mode);
//! Decrypts ciphertext received for this group session.
- std::variant<std::pair<QString, uint32_t>, OlmError> decrypt(QString &message);
+ std::variant<std::pair<QString, uint32_t>, OlmError> decrypt(const QByteArray &message);
//! Export the base64-encoded ratchet key for this session, at the given index,
//! in a format which can be used by import.
std::variant<QByteArray, OlmError> exportSession(uint32_t messageIndex);
@@ -44,5 +44,7 @@ public:
private:
OlmInboundGroupSession *m_groupSession;
};
+
+using QOlmInboundGroupSessionPtr = std::unique_ptr<QOlmInboundGroupSession>;
} // namespace Quotient
#endif
diff --git a/lib/olm/qolmoutboundsession.cpp b/lib/olm/qolmoutboundsession.cpp
index ba8be4f6..4f3cc827 100644
--- a/lib/olm/qolmoutboundsession.cpp
+++ b/lib/olm/qolmoutboundsession.cpp
@@ -84,7 +84,7 @@ std::variant<std::unique_ptr<QOlmOutboundGroupSession>, OlmError> QOlmOutboundGr
return std::make_unique<QOlmOutboundGroupSession>(olmOutboundGroupSession);
}
-std::variant<QString, OlmError> QOlmOutboundGroupSession::encrypt(QString &plaintext)
+std::variant<QByteArray, OlmError> QOlmOutboundGroupSession::encrypt(const QString &plaintext)
{
QByteArray plaintextBuf = plaintext.toUtf8();
const auto messageMaxLen = olm_group_encrypt_message_length(m_groupSession, plaintextBuf.length());
@@ -104,14 +104,14 @@ uint32_t QOlmOutboundGroupSession::sessionMessageIndex() const
return olm_outbound_group_session_message_index(m_groupSession);
}
-std::variant<QByteArray, OlmError> QOlmOutboundGroupSession::sessionId() const
+QByteArray QOlmOutboundGroupSession::sessionId() const
{
const auto idMaxLength = olm_outbound_group_session_id_length(m_groupSession);
QByteArray idBuffer(idMaxLength, '0');
const auto error = olm_outbound_group_session_id(m_groupSession, reinterpret_cast<uint8_t *>(idBuffer.data()),
idBuffer.length());
if (error == olm_error()) {
- return lastError(m_groupSession);
+ throw lastError(m_groupSession);
}
return idBuffer;
}
diff --git a/lib/olm/qolmoutboundsession.h b/lib/olm/qolmoutboundsession.h
index 29776a3d..a642f581 100644
--- a/lib/olm/qolmoutboundsession.h
+++ b/lib/olm/qolmoutboundsession.h
@@ -11,6 +11,7 @@
namespace Quotient {
+
//! An out-bound group session is responsible for encrypting outgoing
//! communication in a Megolm session.
class QOlmOutboundGroupSession
@@ -26,7 +27,7 @@ public:
//! pickling a `QOlmOutboundGroupSession`.
static std::variant<std::unique_ptr<QOlmOutboundGroupSession>, OlmError> unpickle(QByteArray &pickled, const PicklingMode &mode);
//! Encrypts a plaintext message using the session.
- std::variant<QString, OlmError> encrypt(QString &plaintext);
+ std::variant<QByteArray, OlmError> encrypt(const QString &plaintext);
//! Get the current message index for this session.
//!
@@ -35,7 +36,7 @@ public:
uint32_t sessionMessageIndex() const;
//! Get a base64-encoded identifier for this session.
- std::variant<QByteArray, OlmError> sessionId() const;
+ QByteArray sessionId() const;
//! Get the base64-encoded current ratchet key for this session.
//!
@@ -46,5 +47,7 @@ public:
private:
OlmOutboundGroupSession *m_groupSession;
};
+
+using QOlmOutboundGroupSessionPtr = std::unique_ptr<QOlmOutboundGroupSession>;
}
#endif