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.cpp57
1 files changed, 27 insertions, 30 deletions
diff --git a/lib/e2ee/qolminboundsession.cpp b/lib/e2ee/qolminboundsession.cpp
index 870070c2..a05ddf62 100644
--- a/lib/e2ee/qolminboundsession.cpp
+++ b/lib/e2ee/qolminboundsession.cpp
@@ -2,8 +2,9 @@
//
// SPDX-License-Identifier: LGPL-2.1-or-later
-#include "e2ee/qolminboundsession.h"
-#include "logging.h"
+#include "qolminboundsession.h"
+#include "qolmutils.h"
+#include "../logging.h"
#include <cstring>
#include <iostream>
@@ -37,7 +38,7 @@ std::unique_ptr<QOlmInboundGroupSession> QOlmInboundGroupSession::create(const Q
olmInboundGroupSession,
reinterpret_cast<const uint8_t*>(key.constData()), key.size())
== olm_error()) {
- // FIXME: create QOlmInboundGroupSession earlier and use lastError()
+ // FIXME: create QOlmInboundGroupSession earlier and use lastErrorCode()
throw olm_inbound_group_session_last_error_code(olmInboundGroupSession);
}
@@ -47,11 +48,10 @@ std::unique_ptr<QOlmInboundGroupSession> QOlmInboundGroupSession::create(const Q
std::unique_ptr<QOlmInboundGroupSession> QOlmInboundGroupSession::import(const QByteArray &key)
{
const auto olmInboundGroupSession = olm_inbound_group_session(new uint8_t[olm_inbound_group_session_size()]);
- QByteArray keyBuf = key;
if (olm_import_inbound_group_session(
olmInboundGroupSession,
- reinterpret_cast<const uint8_t*>(keyBuf.data()), keyBuf.size())
+ reinterpret_cast<const uint8_t*>(key.data()), key.size())
== olm_error()) {
// FIXME: create QOlmInboundGroupSession earlier and use lastError()
throw olm_inbound_group_session_last_error_code(olmInboundGroupSession);
@@ -60,19 +60,12 @@ std::unique_ptr<QOlmInboundGroupSession> QOlmInboundGroupSession::import(const Q
return std::make_unique<QOlmInboundGroupSession>(olmInboundGroupSession);
}
-QByteArray toKey(const PicklingMode &mode)
-{
- if (std::holds_alternative<Unencrypted>(mode)) {
- return "";
- }
- return std::get<Encrypted>(mode).key;
-}
-
-QByteArray QOlmInboundGroupSession::pickle(const PicklingMode &mode) const
+QByteArray QOlmInboundGroupSession::pickle(const PicklingMode& mode) const
{
- QByteArray pickledBuf(olm_pickle_inbound_group_session_length(m_groupSession), '0');
- const QByteArray key = toKey(mode);
- if (olm_pickle_inbound_group_session(m_groupSession, key.data(),
+ 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()) {
@@ -82,14 +75,13 @@ QByteArray QOlmInboundGroupSession::pickle(const PicklingMode &mode) const
}
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);
+ auto key = toKey(mode);
if (olm_unpickle_inbound_group_session(groupSession, key.data(),
- key.length(), pickledBuf.data(),
- pickledBuf.size())
+ key.length(), pickled.data(),
+ pickled.size())
== olm_error()) {
// FIXME: create QOlmInboundGroupSession earlier and use lastError()
return olm_inbound_group_session_last_error_code(groupSession);
@@ -107,13 +99,16 @@ 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()),
@@ -123,16 +118,17 @@ QOlmExpected<std::pair<QByteArray, uint32_t>> QOlmInboundGroupSession::decrypt(
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');
+ QByteArray keyBuf(keyLength, '\0');
if (olm_export_inbound_group_session(
m_groupSession, reinterpret_cast<uint8_t*>(keyBuf.data()),
keyLength, messageIndex)
@@ -149,7 +145,8 @@ uint32_t QOlmInboundGroupSession::firstKnownIndex() const
QByteArray QOlmInboundGroupSession::sessionId() const
{
- QByteArray sessionIdBuf(olm_inbound_group_session_id_length(m_groupSession), '0');
+ 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())