aboutsummaryrefslogtreecommitdiff
path: root/lib/connection.cpp
diff options
context:
space:
mode:
authorTobias Fella <fella@posteo.de>2022-03-02 00:54:49 +0100
committerTobias Fella <fella@posteo.de>2022-05-16 20:41:50 +0200
commit20bd96ac67c06617e619460c4cd07b5e15cc74d7 (patch)
treef825cd1be86530954ff4c04da2c8b639a981cde3 /lib/connection.cpp
parentff54bf2d0979dc6b9b3b77bba827ae7f3baa9f58 (diff)
downloadlibquotient-20bd96ac67c06617e619460c4cd07b5e15cc74d7.tar.gz
libquotient-20bd96ac67c06617e619460c4cd07b5e15cc74d7.zip
Implement sending encrypted messages
Diffstat (limited to 'lib/connection.cpp')
-rw-r--r--lib/connection.cpp30
1 files changed, 29 insertions, 1 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp
index 2528b70b..a66a4168 100644
--- a/lib/connection.cpp
+++ b/lib/connection.cpp
@@ -1332,7 +1332,7 @@ Connection::sendToDevices(const QString& eventType,
[&jsonUser](const auto& deviceToEvents) {
jsonUser.insert(
deviceToEvents.first,
- deviceToEvents.second.contentJson());
+ deviceToEvents.second->contentJson());
});
});
return callApi<SendToDeviceJob>(BackgroundRequest, eventType,
@@ -2238,4 +2238,32 @@ bool Connection::isKnownCurveKey(const QString& user, const QString& curveKey)
return query.next();
}
+bool Connection::hasOlmSession(User* user, const QString& deviceId) const
+{
+ const auto& curveKey = curveKeyForUserDevice(user->id(), deviceId);
+ return d->olmSessions.contains(curveKey) && d->olmSessions[curveKey].size() > 0;
+}
+
+QPair<QOlmMessage::Type, QByteArray> Connection::olmEncryptMessage(User* user, const QString& device, const QByteArray& message)
+{
+ //TODO be smarter about choosing a session; see e2ee impl guide
+ //TODO create session?
+ const auto& curveKey = curveKeyForUserDevice(user->id(), device);
+ QOlmMessage::Type type = d->olmSessions[curveKey][0]->encryptMessageType();
+ auto result = d->olmSessions[curveKey][0]->encrypt(message);
+ return qMakePair(type, result.toCiphertext());
+}
+
+//TODO be more consistent with curveKey and identityKey
+void Connection::createOlmSession(const QString& theirIdentityKey, const QString& theirOneTimeKey)
+{
+ auto session = QOlmSession::createOutboundSession(olmAccount(), theirIdentityKey, theirOneTimeKey);
+ if (std::holds_alternative<QOlmError>(session)) {
+ //TODO something
+ qCWarning(E2EE) << "Failed to create olm session for " << theirIdentityKey << std::get<QOlmError>(session);
+ }
+ d->saveSession(std::get<std::unique_ptr<QOlmSession>>(session), theirIdentityKey);
+ d->olmSessions[theirIdentityKey].push_back(std::move(std::get<std::unique_ptr<QOlmSession>>(session)));
+}
+
#endif