aboutsummaryrefslogtreecommitdiff
path: root/lib/e2ee/qolmoutboundsession.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/e2ee/qolmoutboundsession.cpp')
-rw-r--r--lib/e2ee/qolmoutboundsession.cpp135
1 files changed, 71 insertions, 64 deletions
diff --git a/lib/e2ee/qolmoutboundsession.cpp b/lib/e2ee/qolmoutboundsession.cpp
index a2eff2c8..1176d790 100644
--- a/lib/e2ee/qolmoutboundsession.cpp
+++ b/lib/e2ee/qolmoutboundsession.cpp
@@ -2,13 +2,22 @@
//
// SPDX-License-Identifier: LGPL-2.1-or-later
-#include "e2ee/qolmoutboundsession.h"
-#include "e2ee/qolmutils.h"
+#include "qolmoutboundsession.h"
+
+#include "logging.h"
+#include "qolmutils.h"
+
+#include <olm/olm.h>
using namespace Quotient;
-QOlmError lastError(OlmOutboundGroupSession *session) {
- return fromString(olm_outbound_group_session_last_error(session));
+OlmErrorCode QOlmOutboundGroupSession::lastErrorCode() const {
+ return olm_outbound_group_session_last_error_code(m_groupSession);
+}
+
+const char* QOlmOutboundGroupSession::lastError() const
+{
+ return olm_outbound_group_session_last_error(m_groupSession);
}
QOlmOutboundGroupSession::QOlmOutboundGroupSession(OlmOutboundGroupSession *session)
@@ -24,72 +33,69 @@ QOlmOutboundGroupSession::~QOlmOutboundGroupSession()
QOlmOutboundGroupSessionPtr QOlmOutboundGroupSession::create()
{
auto *olmOutboundGroupSession = olm_outbound_group_session(new uint8_t[olm_outbound_group_session_size()]);
- const auto randomLength = olm_init_outbound_group_session_random_length(olmOutboundGroupSession);
- QByteArray randomBuf = getRandom(randomLength);
-
- const auto error = olm_init_outbound_group_session(olmOutboundGroupSession,
- reinterpret_cast<uint8_t *>(randomBuf.data()), randomBuf.length());
-
- if (error == olm_error()) {
- throw lastError(olmOutboundGroupSession);
+ if (const auto randomLength = olm_init_outbound_group_session_random_length(
+ olmOutboundGroupSession);
+ olm_init_outbound_group_session(olmOutboundGroupSession,
+ RandomBuffer(randomLength).bytes(),
+ randomLength)
+ == olm_error()) {
+ // FIXME: create the session object earlier
+ QOLM_INTERNAL_ERROR_X("Failed to initialise an outbound group session",
+ olm_outbound_group_session_last_error(
+ olmOutboundGroupSession));
}
- const auto keyMaxLength = olm_outbound_group_session_key_length(olmOutboundGroupSession);
- QByteArray keyBuffer(keyMaxLength, '0');
- olm_outbound_group_session_key(olmOutboundGroupSession, reinterpret_cast<uint8_t *>(keyBuffer.data()),
- keyMaxLength);
-
- randomBuf.clear();
-
return std::make_unique<QOlmOutboundGroupSession>(olmOutboundGroupSession);
}
-QOlmExpected<QByteArray> QOlmOutboundGroupSession::pickle(const PicklingMode &mode) const
+QByteArray QOlmOutboundGroupSession::pickle(const PicklingMode &mode) const
{
- QByteArray pickledBuf(olm_pickle_outbound_group_session_length(m_groupSession), '0');
- QByteArray key = toKey(mode);
- const auto error = olm_pickle_outbound_group_session(m_groupSession, key.data(), key.length(),
- pickledBuf.data(), pickledBuf.length());
-
- if (error == olm_error()) {
- return lastError(m_groupSession);
- }
+ QByteArray pickledBuf(
+ olm_pickle_outbound_group_session_length(m_groupSession), '\0');
+ auto key = toKey(mode);
+ if (olm_pickle_outbound_group_session(m_groupSession, key.data(),
+ key.length(), pickledBuf.data(),
+ pickledBuf.length())
+ == olm_error())
+ QOLM_INTERNAL_ERROR("Failed to pickle the outbound group session");
key.clear();
-
return pickledBuf;
}
-QOlmExpected<QOlmOutboundGroupSessionPtr> QOlmOutboundGroupSession::unpickle(const QByteArray &pickled, const PicklingMode &mode)
+QOlmExpected<QOlmOutboundGroupSessionPtr> QOlmOutboundGroupSession::unpickle(
+ QByteArray&& pickled, const PicklingMode& mode)
{
- QByteArray pickledBuf = pickled;
auto *olmOutboundGroupSession = olm_outbound_group_session(new uint8_t[olm_outbound_group_session_size()]);
- QByteArray key = toKey(mode);
- const auto error = olm_unpickle_outbound_group_session(olmOutboundGroupSession, key.data(), key.length(),
- pickledBuf.data(), pickledBuf.length());
- if (error == olm_error()) {
- return lastError(olmOutboundGroupSession);
+ auto key = toKey(mode);
+ if (olm_unpickle_outbound_group_session(olmOutboundGroupSession, key.data(),
+ key.length(), pickled.data(),
+ pickled.length())
+ == olm_error()) {
+ // FIXME: create the session object earlier and use lastError()
+ qWarning(E2EE) << "Failed to unpickle an outbound group session:"
+ << olm_outbound_group_session_last_error(
+ olmOutboundGroupSession);
+ return olm_outbound_group_session_last_error_code(
+ olmOutboundGroupSession);
}
- const auto idMaxLength = olm_outbound_group_session_id_length(olmOutboundGroupSession);
- QByteArray idBuffer(idMaxLength, '0');
- olm_outbound_group_session_id(olmOutboundGroupSession, reinterpret_cast<uint8_t *>(idBuffer.data()),
- idBuffer.length());
key.clear();
return std::make_unique<QOlmOutboundGroupSession>(olmOutboundGroupSession);
}
-QOlmExpected<QByteArray> QOlmOutboundGroupSession::encrypt(const QString &plaintext) const
+QByteArray QOlmOutboundGroupSession::encrypt(const QByteArray& plaintext) const
{
- QByteArray plaintextBuf = plaintext.toUtf8();
- const auto messageMaxLength = olm_group_encrypt_message_length(m_groupSession, plaintextBuf.length());
- QByteArray messageBuf(messageMaxLength, '0');
- const auto error = olm_group_encrypt(m_groupSession, reinterpret_cast<uint8_t *>(plaintextBuf.data()),
- plaintextBuf.length(), reinterpret_cast<uint8_t *>(messageBuf.data()), messageBuf.length());
-
- if (error == olm_error()) {
- return lastError(m_groupSession);
- }
+ const auto messageMaxLength =
+ olm_group_encrypt_message_length(m_groupSession, plaintext.length());
+ QByteArray messageBuf(messageMaxLength, '\0');
+ if (olm_group_encrypt(m_groupSession,
+ reinterpret_cast<const uint8_t*>(plaintext.data()),
+ plaintext.length(),
+ reinterpret_cast<uint8_t*>(messageBuf.data()),
+ messageBuf.length())
+ == olm_error())
+ QOLM_INTERNAL_ERROR("Failed to encrypt a message");
return messageBuf;
}
@@ -102,25 +108,26 @@ uint32_t QOlmOutboundGroupSession::sessionMessageIndex() 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()) {
- throw lastError(m_groupSession);
- }
+ QByteArray idBuffer(idMaxLength, '\0');
+ if (olm_outbound_group_session_id(
+ m_groupSession, reinterpret_cast<uint8_t*>(idBuffer.data()),
+ idBuffer.length())
+ == olm_error())
+ QOLM_INTERNAL_ERROR("Failed to obtain group session id");
+
return idBuffer;
}
-QOlmExpected<QByteArray> QOlmOutboundGroupSession::sessionKey() const
+QByteArray QOlmOutboundGroupSession::sessionKey() const
{
const auto keyMaxLength = olm_outbound_group_session_key_length(m_groupSession);
- QByteArray keyBuffer(keyMaxLength, '0');
- const auto error = olm_outbound_group_session_key(
- m_groupSession, reinterpret_cast<uint8_t*>(keyBuffer.data()),
- keyMaxLength);
- if (error == olm_error()) {
- return lastError(m_groupSession);
- }
+ QByteArray keyBuffer(keyMaxLength, '\0');
+ if (olm_outbound_group_session_key(
+ m_groupSession, reinterpret_cast<uint8_t*>(keyBuffer.data()),
+ keyMaxLength)
+ == olm_error())
+ QOLM_INTERNAL_ERROR("Failed to obtain group session key");
+
return keyBuffer;
}