aboutsummaryrefslogtreecommitdiff
path: root/lib/crypto/session.h
diff options
context:
space:
mode:
authorCarl Schwan <carl@carlschwan.eu>2021-01-28 21:54:37 +0100
committerTobias Fella <fella@posteo.de>2021-12-01 21:34:52 +0100
commitdd0316ce57bd9256a093d66845e1d40cd9426ba4 (patch)
tree900996974e3bdec3a3ceda2ebdae0377dacc80e4 /lib/crypto/session.h
parent5910689306149cacf3ac644d3ea42e10f889e3fe (diff)
downloadlibquotient-dd0316ce57bd9256a093d66845e1d40cd9426ba4.tar.gz
libquotient-dd0316ce57bd9256a093d66845e1d40cd9426ba4.zip
Move files
Diffstat (limited to 'lib/crypto/session.h')
-rw-r--r--lib/crypto/session.h77
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/crypto/session.h b/lib/crypto/session.h
new file mode 100644
index 00000000..24702564
--- /dev/null
+++ b/lib/crypto/session.h
@@ -0,0 +1,77 @@
+// SPDX-FileCopyrightText: 2021 Alexey Andreyev <aa13q@ya.ru>
+//
+// SPDX-License-Identifier: LGPL-2.1-or-later
+
+#pragma once
+
+#ifdef Quotient_E2EE_ENABLED
+
+#include <QDebug>
+#include <olm/olm.h>
+#include "crypto/e2ee.h"
+#include "crypto/message.h"
+#include "crypto/errors.h"
+#include "crypto/qolmaccount.h"
+
+namespace Quotient {
+
+class QOlmAccount;
+class QOlmSession;
+
+
+//! Either an outbound or inbound session for secure communication.
+class QOlmSession
+{
+public:
+ ~QOlmSession();
+ //! Creates an inbound session for sending/receiving messages from a received 'prekey' message.
+ static std::variant<std::unique_ptr<QOlmSession>, OlmError> createInboundSession(QOlmAccount *account, const Message &preKeyMessage);
+ static std::variant<std::unique_ptr<QOlmSession>, OlmError> createInboundSessionFrom(QOlmAccount *account, const QString &theirIdentityKey, const Message &preKeyMessage);
+ static std::variant<std::unique_ptr<QOlmSession>, OlmError> createOutboundSession(QOlmAccount *account, const QString &theirIdentityKey, const QString &theirOneTimeKey);
+ //! Serialises an `QOlmSession` to encrypted Base64.
+ std::variant<QByteArray, OlmError> pickle(const PicklingMode &mode);
+ //! Deserialises from encrypted Base64 that was previously obtained by pickling a `QOlmSession`.
+ static std::variant<std::unique_ptr<QOlmSession>, OlmError> unpickle(const QByteArray &pickled, const PicklingMode &mode);
+ //! Encrypts a plaintext message using the session.
+ Message encrypt(const QString &plaintext);
+
+ //! Decrypts a message using this session. Decoding is lossy, meaing if
+ //! the decrypted plaintext contains invalid UTF-8 symbols, they will
+ //! be returned as `U+FFFD` (�).
+ std::variant<QString, OlmError> decrypt(const Message &message) const;
+
+ //! Get a base64-encoded identifier for this session.
+ QByteArray sessionId() const;
+
+ //! The type of the next message that will be returned from encryption.
+ Message::Type encryptMessageType();
+
+ //! Checker for any received messages for this session.
+ bool hasReceivedMessage() const;
+
+ //! Checks if the 'prekey' message is for this in-bound session.
+ std::variant<bool, OlmError> matchesInboundSession(Message &preKeyMessage);
+
+ friend bool operator<(const QOlmSession& lhs, const QOlmSession& rhs)
+ {
+ return lhs.sessionId() < rhs.sessionId();
+ }
+
+ friend bool operator<(const std::unique_ptr<QOlmSession> &lhs, const std::unique_ptr<QOlmSession> &rhs) {
+ return *lhs < *rhs;
+ }
+
+ QOlmSession(OlmSession* session);
+private:
+ //! Helper function for creating new sessions and handling errors.
+ static OlmSession* create();
+ static std::variant<std::unique_ptr<QOlmSession>, OlmError> createInbound(QOlmAccount *account, const Message& preKeyMessage, bool from = false, const QString& theirIdentityKey = "");
+ OlmSession* m_session;
+};
+
+
+//using QOlmSessionPtr = std::unique_ptr<QOlmSession>;
+
+} //namespace Quotient
+
+#endif // Quotient_E2EE_ENABLED