aboutsummaryrefslogtreecommitdiff
path: root/lib/e2ee/qolminboundsession.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/e2ee/qolminboundsession.cpp')
-rw-r--r--lib/e2ee/qolminboundsession.cpp147
1 files changed, 84 insertions, 63 deletions
diff --git a/lib/e2ee/qolminboundsession.cpp b/lib/e2ee/qolminboundsession.cpp
index 17f06205..18275dc0 100644
--- a/lib/e2ee/qolminboundsession.cpp
+++ b/lib/e2ee/qolminboundsession.cpp
@@ -2,84 +2,99 @@
//
// SPDX-License-Identifier: LGPL-2.1-or-later
-#include "e2ee/qolminboundsession.h"
-#include <iostream>
+#include "qolminboundsession.h"
+#include "qolmutils.h"
+#include "../logging.h"
+
#include <cstring>
+#include <iostream>
+#include <olm/olm.h>
using namespace Quotient;
-QOlmError lastError(OlmInboundGroupSession *session) {
- return fromString(olm_inbound_group_session_last_error(session));
+OlmErrorCode QOlmInboundGroupSession::lastErrorCode() const {
+ return olm_inbound_group_session_last_error_code(m_groupSession);
}
-QOlmInboundGroupSession::QOlmInboundGroupSession(OlmInboundGroupSession *session)
- : m_groupSession(session)
+const char* QOlmInboundGroupSession::lastError() const
{
+ return olm_inbound_group_session_last_error(m_groupSession);
}
+QOlmInboundGroupSession::QOlmInboundGroupSession(OlmInboundGroupSession *session)
+ : m_groupSession(session)
+{}
+
QOlmInboundGroupSession::~QOlmInboundGroupSession()
{
olm_clear_inbound_group_session(m_groupSession);
//delete[](reinterpret_cast<uint8_t *>(m_groupSession));
}
-std::unique_ptr<QOlmInboundGroupSession> QOlmInboundGroupSession::create(const QByteArray &key)
+QOlmExpected<QOlmInboundGroupSessionPtr> QOlmInboundGroupSession::create(
+ const QByteArray& key)
{
const auto olmInboundGroupSession = olm_inbound_group_session(new uint8_t[olm_inbound_group_session_size()]);
- const auto error = olm_init_inbound_group_session(olmInboundGroupSession,
- reinterpret_cast<const uint8_t *>(key.constData()), key.size());
-
- if (error == olm_error()) {
- throw lastError(olmInboundGroupSession);
+ if (olm_init_inbound_group_session(
+ olmInboundGroupSession,
+ reinterpret_cast<const uint8_t*>(key.constData()), key.size())
+ == olm_error()) {
+ // FIXME: create QOlmInboundGroupSession earlier and use lastErrorCode()
+ qWarning(E2EE) << "Failed to create an inbound group session:"
+ << olm_inbound_group_session_last_error(
+ olmInboundGroupSession);
+ return olm_inbound_group_session_last_error_code(olmInboundGroupSession);
}
return std::make_unique<QOlmInboundGroupSession>(olmInboundGroupSession);
}
-std::unique_ptr<QOlmInboundGroupSession> QOlmInboundGroupSession::import(const QByteArray &key)
+QOlmExpected<QOlmInboundGroupSessionPtr> QOlmInboundGroupSession::importSession(
+ const QByteArray& key)
{
const auto olmInboundGroupSession = olm_inbound_group_session(new uint8_t[olm_inbound_group_session_size()]);
- QByteArray keyBuf = key;
- const auto error = olm_import_inbound_group_session(olmInboundGroupSession,
- reinterpret_cast<const uint8_t *>(keyBuf.data()), keyBuf.size());
- if (error == olm_error()) {
- throw lastError(olmInboundGroupSession);
+ if (olm_import_inbound_group_session(
+ olmInboundGroupSession,
+ reinterpret_cast<const uint8_t*>(key.data()), key.size())
+ == olm_error()) {
+ // FIXME: create QOlmInboundGroupSession earlier and use lastError()
+ qWarning(E2EE) << "Failed to import an inbound group session:"
+ << olm_inbound_group_session_last_error(
+ olmInboundGroupSession);
+ return olm_inbound_group_session_last_error_code(olmInboundGroupSession);
}
return std::make_unique<QOlmInboundGroupSession>(olmInboundGroupSession);
}
-QByteArray toKey(const PicklingMode &mode)
+QByteArray QOlmInboundGroupSession::pickle(const PicklingMode& mode) const
{
- if (std::holds_alternative<Unencrypted>(mode)) {
- return "";
- }
- return std::get<Encrypted>(mode).key;
-}
-
-QByteArray QOlmInboundGroupSession::pickle(const PicklingMode &mode) const
-{
- QByteArray pickledBuf(olm_pickle_inbound_group_session_length(m_groupSession), '0');
- const QByteArray key = toKey(mode);
- const auto error = olm_pickle_inbound_group_session(m_groupSession, key.data(), key.length(), pickledBuf.data(),
- pickledBuf.length());
- if (error == olm_error()) {
- throw lastError(m_groupSession);
+ QByteArray pickledBuf(
+ olm_pickle_inbound_group_session_length(m_groupSession), '\0');
+ if (const auto key = toKey(mode);
+ olm_pickle_inbound_group_session(m_groupSession, key.data(),
+ key.length(), pickledBuf.data(),
+ pickledBuf.length())
+ == olm_error()) {
+ QOLM_INTERNAL_ERROR("Failed to pickle the inbound group session");
}
return pickledBuf;
}
QOlmExpected<QOlmInboundGroupSessionPtr> QOlmInboundGroupSession::unpickle(
- const QByteArray& pickled, const PicklingMode& mode)
+ QByteArray&& pickled, const PicklingMode& mode)
{
- QByteArray pickledBuf = pickled;
const auto groupSession = olm_inbound_group_session(new uint8_t[olm_inbound_group_session_size()]);
- QByteArray key = toKey(mode);
- const auto error = olm_unpickle_inbound_group_session(groupSession, key.data(), key.length(),
- pickledBuf.data(), pickledBuf.size());
- if (error == olm_error()) {
- return lastError(groupSession);
+ auto key = toKey(mode);
+ if (olm_unpickle_inbound_group_session(groupSession, key.data(),
+ key.length(), pickled.data(),
+ pickled.size())
+ == olm_error()) {
+ // FIXME: create QOlmInboundGroupSession earlier and use lastError()
+ qWarning(E2EE) << "Failed to unpickle an inbound group session:"
+ << olm_inbound_group_session_last_error(groupSession);
+ return olm_inbound_group_session_last_error_code(groupSession);
}
key.clear();
@@ -94,39 +109,43 @@ QOlmExpected<std::pair<QByteArray, uint32_t>> QOlmInboundGroupSession::decrypt(
// We need to clone the message because
// olm_decrypt_max_plaintext_length destroys the input buffer
- QByteArray messageBuf(message.length(), '0');
+ 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');
+ QByteArray plaintextBuf(olm_group_decrypt_max_plaintext_length(
+ m_groupSession,
+ reinterpret_cast<uint8_t*>(messageBuf.data()),
+ messageBuf.length()),
+ '\0');
- messageBuf = QByteArray(message.length(), '0');
+ 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()),
messageBuf.length(), reinterpret_cast<uint8_t *>(plaintextBuf.data()), plaintextBuf.length(), &messageIndex);
-
- // Error code or plaintext length is returned
- const auto decryptError = plaintextLen;
-
- if (decryptError == olm_error()) {
- return lastError(m_groupSession);
+ if (plaintextLen == olm_error()) {
+ qWarning(E2EE) << "Failed to decrypt the message:" << lastError();
+ return lastErrorCode();
}
- QByteArray output(plaintextLen, '0');
+ QByteArray output(plaintextLen, '\0');
std::memcpy(output.data(), plaintextBuf.data(), plaintextLen);
return std::make_pair(output, messageIndex);
}
-QOlmExpected<QByteArray> QOlmInboundGroupSession::exportSession(uint32_t messageIndex)
+QOlmExpected<QByteArray> QOlmInboundGroupSession::exportSession(
+ uint32_t messageIndex)
{
const auto keyLength = olm_export_inbound_group_session_length(m_groupSession);
- QByteArray keyBuf(keyLength, '0');
- const auto error = olm_export_inbound_group_session(m_groupSession, reinterpret_cast<uint8_t *>(keyBuf.data()), keyLength, messageIndex);
-
- if (error == olm_error()) {
- return lastError(m_groupSession);
+ QByteArray keyBuf(keyLength, '\0');
+ if (olm_export_inbound_group_session(
+ m_groupSession, reinterpret_cast<uint8_t*>(keyBuf.data()),
+ keyLength, messageIndex)
+ == olm_error()) {
+ QOLM_FAIL_OR_LOG(OLM_OUTPUT_BUFFER_TOO_SMALL,
+ "Failed to export the inbound group session");
+ return lastErrorCode();
}
return keyBuf;
}
@@ -138,12 +157,14 @@ uint32_t QOlmInboundGroupSession::firstKnownIndex() const
QByteArray QOlmInboundGroupSession::sessionId() const
{
- QByteArray sessionIdBuf(olm_inbound_group_session_id_length(m_groupSession), '0');
- const auto error = olm_inbound_group_session_id(m_groupSession, reinterpret_cast<uint8_t *>(sessionIdBuf.data()),
- sessionIdBuf.length());
- if (error == olm_error()) {
- throw lastError(m_groupSession);
- }
+ QByteArray sessionIdBuf(olm_inbound_group_session_id_length(m_groupSession),
+ '\0');
+ if (olm_inbound_group_session_id(
+ m_groupSession, reinterpret_cast<uint8_t*>(sessionIdBuf.data()),
+ sessionIdBuf.length())
+ == olm_error())
+ QOLM_INTERNAL_ERROR("Failed to obtain the group session id");
+
return sessionIdBuf;
}