aboutsummaryrefslogtreecommitdiff
path: root/lib/e2ee/qolmsession.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/e2ee/qolmsession.cpp')
-rw-r--r--lib/e2ee/qolmsession.cpp96
1 files changed, 35 insertions, 61 deletions
diff --git a/lib/e2ee/qolmsession.cpp b/lib/e2ee/qolmsession.cpp
index 771d310d..e252c37f 100644
--- a/lib/e2ee/qolmsession.cpp
+++ b/lib/e2ee/qolmsession.cpp
@@ -76,22 +76,17 @@ QOlmExpected<QOlmSessionPtr> QOlmSession::createInboundSessionFrom(
}
QOlmExpected<QOlmSessionPtr> QOlmSession::createOutboundSession(
- QOlmAccount* account, const QString& theirIdentityKey,
- const QString& theirOneTimeKey)
+ QOlmAccount* account, const QByteArray& theirIdentityKey,
+ const QByteArray& theirOneTimeKey)
{
- auto *olmOutboundSession = create();
- const auto randomLen = olm_create_outbound_session_random_length(olmOutboundSession);
- QByteArray randomBuf = getRandom(randomLen);
+ auto* olmOutboundSession = create();
+ auto randomBuf = getRandom(
+ olm_create_outbound_session_random_length(olmOutboundSession));
- QByteArray theirIdentityKeyBuf = theirIdentityKey.toUtf8();
- QByteArray theirOneTimeKeyBuf = theirOneTimeKey.toUtf8();
if (olm_create_outbound_session(
- olmOutboundSession, account->data(),
- reinterpret_cast<uint8_t*>(theirIdentityKeyBuf.data()),
- theirIdentityKeyBuf.length(),
- reinterpret_cast<uint8_t*>(theirOneTimeKeyBuf.data()),
- theirOneTimeKeyBuf.length(),
- reinterpret_cast<uint8_t*>(randomBuf.data()), randomBuf.length())
+ olmOutboundSession, account->data(), theirIdentityKey.data(),
+ theirIdentityKey.length(), theirOneTimeKey.data(),
+ theirOneTimeKey.length(), randomBuf.data(), randomBuf.length())
== olm_error()) {
// FIXME: the QOlmSession object should be created earlier
const auto lastErr = olm_session_last_error_code(olmOutboundSession);
@@ -107,7 +102,7 @@ QOlmExpected<QOlmSessionPtr> QOlmSession::createOutboundSession(
QOlmExpected<QByteArray> QOlmSession::pickle(const PicklingMode &mode) const
{
- QByteArray pickledBuf(olm_pickle_session_length(m_session), '0');
+ QByteArray pickledBuf(olm_pickle_session_length(m_session), '\0');
QByteArray key = toKey(mode);
if (olm_pickle_session(m_session, key.data(), key.length(),
pickledBuf.data(), pickledBuf.length())
@@ -118,14 +113,13 @@ QOlmExpected<QByteArray> QOlmSession::pickle(const PicklingMode &mode) const
return pickledBuf;
}
-QOlmExpected<QOlmSessionPtr> QOlmSession::unpickle(const QByteArray& pickled,
+QOlmExpected<QOlmSessionPtr> QOlmSession::unpickle(QByteArray&& pickled,
const PicklingMode& mode)
{
- QByteArray pickledBuf = pickled;
auto *olmSession = create();
- QByteArray key = toKey(mode);
+ auto key = toKey(mode);
if (olm_unpickle_session(olmSession, key.data(), key.length(),
- pickledBuf.data(), pickledBuf.length())
+ pickled.data(), pickled.length())
== olm_error()) {
// FIXME: the QOlmSession object should be created earlier
return olm_session_last_error_code(olmSession);
@@ -135,52 +129,48 @@ QOlmExpected<QOlmSessionPtr> QOlmSession::unpickle(const QByteArray& pickled,
return std::make_unique<QOlmSession>(olmSession);
}
-QOlmMessage QOlmSession::encrypt(const QString &plaintext)
+QOlmMessage QOlmSession::encrypt(const QByteArray& 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);
- if (olm_encrypt(m_session, reinterpret_cast<uint8_t*>(plaintextBuf.data()),
- plaintextBuf.length(),
- reinterpret_cast<uint8_t*>(randomBuf.data()),
- randomBuf.length(),
- reinterpret_cast<uint8_t*>(messageBuf.data()),
+ const auto messageMaxLength =
+ olm_encrypt_message_length(m_session, plaintext.length());
+ QByteArray messageBuf(messageMaxLength, '0');
+ // NB: The type has to be calculated before calling olm_encrypt()
+ const auto messageType = olm_encrypt_message_type(m_session);
+ auto randomBuf = getRandom(olm_encrypt_random_length(m_session));
+ if (olm_encrypt(m_session, plaintext.data(), plaintext.length(),
+ randomBuf.data(), randomBuf.length(), messageBuf.data(),
messageBuf.length())
== olm_error()) {
throw lastError();
}
- return QOlmMessage(messageBuf, messageType);
+ randomBuf.clear();
+ return QOlmMessage(messageBuf, QOlmMessage::Type(messageType));
}
QOlmExpected<QByteArray> QOlmSession::decrypt(const QOlmMessage &message) const
{
- const auto messageType = message.type();
const auto ciphertext = message.toCiphertext();
- const auto messageTypeValue = messageType == QOlmMessage::Type::General
- ? OLM_MESSAGE_TYPE_MESSAGE : OLM_MESSAGE_TYPE_PRE_KEY;
+ const auto messageTypeValue = message.type();
// We need to clone the message because
// olm_decrypt_max_plaintext_length destroys the input buffer
- QByteArray messageBuf(ciphertext.length(), '0');
+ QByteArray messageBuf(ciphertext.length(), '\0');
std::copy(message.begin(), message.end(), messageBuf.begin());
- const auto plaintextMaxLen = olm_decrypt_max_plaintext_length(m_session, messageTypeValue,
- reinterpret_cast<uint8_t *>(messageBuf.data()), messageBuf.length());
+ const auto plaintextMaxLen = olm_decrypt_max_plaintext_length(
+ m_session, messageTypeValue, messageBuf.data(), messageBuf.length());
if (plaintextMaxLen == olm_error()) {
return lastError();
}
- QByteArray plaintextBuf(plaintextMaxLen, '0');
- QByteArray messageBuf2(ciphertext.length(), '0');
+ QByteArray plaintextBuf(plaintextMaxLen, '\0');
+ QByteArray messageBuf2(ciphertext.length(), '\0');
std::copy(message.begin(), message.end(), messageBuf2.begin());
- const auto plaintextResultLen = olm_decrypt(m_session, messageTypeValue,
- reinterpret_cast<uint8_t *>(messageBuf2.data()), messageBuf2.length(),
- reinterpret_cast<uint8_t *>(plaintextBuf.data()), plaintextMaxLen);
+ const auto plaintextResultLen =
+ olm_decrypt(m_session, messageTypeValue, messageBuf2.data(),
+ messageBuf2.length(), plaintextBuf.data(), plaintextMaxLen);
if (plaintextResultLen == olm_error()) {
const auto lastErr = lastErrorCode();
if (lastErr == OLM_OUTPUT_BUFFER_TOO_SMALL) {
@@ -188,31 +178,15 @@ QOlmExpected<QByteArray> QOlmSession::decrypt(const QOlmMessage &message) const
}
return lastErr;
}
- QByteArray output(plaintextResultLen, '0');
- std::memcpy(output.data(), plaintextBuf.data(), plaintextResultLen);
- plaintextBuf.clear();
- return output;
-}
-
-QOlmMessage::Type QOlmSession::encryptMessageType()
-{
- const auto messageTypeResult = olm_encrypt_message_type(m_session);
- if (messageTypeResult == olm_error()) {
- throw lastError();
- }
- if (messageTypeResult == OLM_MESSAGE_TYPE_PRE_KEY) {
- return QOlmMessage::PreKey;
- }
- return QOlmMessage::General;
+ plaintextBuf.truncate(plaintextResultLen);
+ return plaintextBuf;
}
QByteArray QOlmSession::sessionId() const
{
const auto idMaxLength = olm_session_id_length(m_session);
QByteArray idBuffer(idMaxLength, '0');
- if (olm_session_id(m_session, reinterpret_cast<uint8_t*>(idBuffer.data()),
- idBuffer.length())
- == olm_error()) {
+ if (olm_session_id(m_session, idBuffer.data(), idMaxLength) == olm_error()) {
throw lastError();
}
return idBuffer;