aboutsummaryrefslogtreecommitdiff
path: root/lib/olm
diff options
context:
space:
mode:
authorCarl Schwan <carl@carlschwan.eu>2021-01-27 02:08:09 +0100
committerTobias Fella <fella@posteo.de>2021-12-01 21:34:52 +0100
commitf3fdd967d544650f9af8aadbaddfcf6d8a9fe957 (patch)
tree15b29046d31bd37ae85adebc6da03366d07eebe0 /lib/olm
parentfe2d5dd577a05e4a0e250d89487cd14025204b02 (diff)
downloadlibquotient-f3fdd967d544650f9af8aadbaddfcf6d8a9fe957.tar.gz
libquotient-f3fdd967d544650f9af8aadbaddfcf6d8a9fe957.zip
Add first session test and it fails :(
Diffstat (limited to 'lib/olm')
-rw-r--r--lib/olm/session.cpp24
-rw-r--r--lib/olm/session.h7
2 files changed, 27 insertions, 4 deletions
diff --git a/lib/olm/session.cpp b/lib/olm/session.cpp
index f6cab650..0beb136e 100644
--- a/lib/olm/session.cpp
+++ b/lib/olm/session.cpp
@@ -121,11 +121,12 @@ std::variant<std::unique_ptr<QOlmSession>, OlmError> QOlmSession::unpickle(QByte
return std::make_unique<QOlmSession>(olmSession);
}
-std::variant<Message, OlmError> QOlmSession::encrypt(const QString &plaintext)
+Message QOlmSession::encrypt(const QString &plaintext)
{
QByteArray plaintextBuf = plaintext.toUtf8();
const auto messageMaxLen = olm_encrypt_message_length(m_session, plaintextBuf.length());
QByteArray messageBuf(messageMaxLen, '0');
+ const auto messageType = encryptMessageType();
const auto randomLen = olm_encrypt_random_length(m_session);
QByteArray randomBuf = getRandom(randomLen);
const auto error = olm_encrypt(m_session,
@@ -134,10 +135,22 @@ std::variant<Message, OlmError> QOlmSession::encrypt(const QString &plaintext)
reinterpret_cast<uint8_t *>(messageBuf.data()), messageBuf.length());
if (error == olm_error()) {
- return lastError(m_session);
+ throw lastError(m_session);
}
- return Message::fromCiphertext(messageBuf);
+ return Message(messageBuf, messageType);
+}
+
+Message::Type QOlmSession::encryptMessageType()
+{
+ const auto messageTypeResult = olm_encrypt_message_type(m_session);
+ if (messageTypeResult == olm_error()) {
+ throw lastError(m_session);
+ }
+ if (messageTypeResult == OLM_MESSAGE_TYPE_PRE_KEY) {
+ return Message::PreKey;
+ }
+ return Message::General;
}
QByteArray QOlmSession::sessionId() const
@@ -152,6 +165,11 @@ QByteArray QOlmSession::sessionId() const
return idBuffer;
}
+bool QOlmSession::hasReceivedMessage() const
+{
+ return olm_session_has_received_message(m_session);
+}
+
QOlmSession::QOlmSession(OlmSession *session)
: m_session(session)
{
diff --git a/lib/olm/session.h b/lib/olm/session.h
index 89f5d822..f9221dec 100644
--- a/lib/olm/session.h
+++ b/lib/olm/session.h
@@ -31,12 +31,17 @@ public:
//! Deserialises from encrypted Base64 that was previously obtained by pickling a `QOlmSession`.
static std::variant<std::unique_ptr<QOlmSession>, OlmError> unpickle(QByteArray &pickled, const PicklingMode &mode);
//! Encrypts a plaintext message using the session.
- std::variant<Message, OlmError> encrypt(const QString &plaintext);
+ Message encrypt(const QString &plaintext);
// TODO: WiP
//! Get a base64-encoded identifier for this session.
QByteArray sessionId() const;
+ //! The type of the next message that will be returned from encryption.
+ Message::Type encryptMessageType();
+
+ bool hasReceivedMessage() const;
+
QOlmSession(OlmSession* session);
private:
//! Helper function for creating new sessions and handling errors.