aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Schwan <carl@carlschwan.eu>2021-01-27 00:50:27 +0100
committerTobias Fella <fella@posteo.de>2021-12-01 21:34:52 +0100
commit57a218086d3c687cd26580ee2a0d2135646411dc (patch)
tree6df3c6d79ee8d197c84583cc564fb453e31d81ab
parentd0d8b267753792d0310dd964b0b688d6262e6eb4 (diff)
downloadlibquotient-57a218086d3c687cd26580ee2a0d2135646411dc.tar.gz
libquotient-57a218086d3c687cd26580ee2a0d2135646411dc.zip
Add hehlper functions
-rw-r--r--lib/olm/qolmaccount.cpp15
-rw-r--r--lib/olm/qolmaccount.h20
-rw-r--r--lib/olm/session.cpp26
-rw-r--r--lib/olm/session.h10
4 files changed, 57 insertions, 14 deletions
diff --git a/lib/olm/qolmaccount.cpp b/lib/olm/qolmaccount.cpp
index 9530d675..9c47bc87 100644
--- a/lib/olm/qolmaccount.cpp
+++ b/lib/olm/qolmaccount.cpp
@@ -197,4 +197,19 @@ OlmAccount *Quotient::QOlmAccount::data()
return m_account;
}
+std::variant<std::unique_ptr<QOlmSession>, OlmError> QOlmAccount::createInboundSession(const Message &preKeyMessage)
+{
+ return QOlmSession::createInboundSession(this, preKeyMessage);
+}
+
+std::variant<std::unique_ptr<QOlmSession>, OlmError> QOlmAccount::createInboundSessionFrom(const QByteArray &theirIdentityKey, const Message &preKeyMessage)
+{
+ return QOlmSession::createInboundSessionFrom(this, theirIdentityKey, preKeyMessage);
+}
+
+std::variant<std::unique_ptr<QOlmSession>, OlmError> QOlmAccount::createOutboundSession(const QByteArray &theirIdentityKey, const QByteArray &theirOneTimeKey)
+{
+ return QOlmSession::createOutboundSession(this, theirIdentityKey, theirOneTimeKey);
+}
+
#endif
diff --git a/lib/olm/qolmaccount.h b/lib/olm/qolmaccount.h
index 3260ca71..df5e1be2 100644
--- a/lib/olm/qolmaccount.h
+++ b/lib/olm/qolmaccount.h
@@ -7,12 +7,15 @@
#include "olm/e2ee.h"
#include "olm/errors.h"
#include "olm/olm.h"
+#include "olm/session.h"
#include <QObject>
struct OlmAccount;
namespace Quotient {
+class QOlmSession;
+
//! An olm account manages all cryptographic keys used on a device.
//! \code{.cpp}
//! const auto olmAccount = new QOlmAccount(this);
@@ -63,10 +66,25 @@ public:
QByteArray signOneTimeKey(const QString &key) const;
SignedOneTimeKey signedOneTimeKey(const QByteArray &key, const QString &signature) const;
- OlmAccount *data();
+
+ //! Creates an inbound session for sending/receiving messages from a received 'prekey' message.
+ //!
+ //! \param message An Olm pre-key message that was encrypted for this account.
+ std::variant<std::unique_ptr<QOlmSession>, OlmError> createInboundSession(const Message &preKeyMessage);
+
+ //! Creates an inbound session for sending/receiving messages from a received 'prekey' message.
+ //!
+ //! \param theirIdentityKey - The identity key of an Olm account that
+ //! encrypted this Olm message.
+ std::variant<std::unique_ptr<QOlmSession>, OlmError> createInboundSessionFrom(const QByteArray &theirIdentityKey, const Message &preKeyMessage);
+
+ //! Creates an outbound session for sending messages to a specific
+ /// identity and one time key.
+ std::variant<std::unique_ptr<QOlmSession>, OlmError> createOutboundSession(const QByteArray &theirIdentityKey, const QByteArray &theirOneTimeKey);
// HACK do not use directly
QOlmAccount(OlmAccount *account);
+ OlmAccount *data();
private:
OlmAccount *m_account = nullptr;
QString m_userId;
diff --git a/lib/olm/session.cpp b/lib/olm/session.cpp
index e7a57677..b5cd7b81 100644
--- a/lib/olm/session.cpp
+++ b/lib/olm/session.cpp
@@ -25,7 +25,7 @@ OlmSession* QOlmSession::create()
return olm_session(new uint8_t[olm_session_size()]);
}
-std::unique_ptr<QOlmSession> QOlmSession::createInbound(QOlmAccount &account, const Message &preKeyMessage, bool from, const QString &theirIdentityKey)
+std::variant<std::unique_ptr<QOlmSession>, OlmError> QOlmSession::createInbound(QOlmAccount *account, const Message &preKeyMessage, bool from, const QString &theirIdentityKey)
{
if (preKeyMessage.type() != Message::PreKey) {
qCDebug(E2EE) << "The message is not a pre-key";
@@ -38,29 +38,33 @@ std::unique_ptr<QOlmSession> QOlmSession::createInbound(QOlmAccount &account, co
QByteArray theirIdentityKeyBuf = theirIdentityKey.toUtf8();
size_t error = 0;
if (from) {
- error = olm_create_inbound_session_from(olmSession, account.data(), theirIdentityKeyBuf.data(), theirIdentityKeyBuf.length(), oneTimeKeyMessageBuf.data(), oneTimeKeyMessageBuf.length());
+ error = olm_create_inbound_session_from(olmSession, account->data(), theirIdentityKeyBuf.data(), theirIdentityKeyBuf.length(), oneTimeKeyMessageBuf.data(), oneTimeKeyMessageBuf.length());
} else {
- error = olm_create_inbound_session(olmSession, account.data(), oneTimeKeyMessageBuf.data(), oneTimeKeyMessageBuf.length());
+ error = olm_create_inbound_session(olmSession, account->data(), oneTimeKeyMessageBuf.data(), oneTimeKeyMessageBuf.length());
}
if (error == olm_error()) {
- throw lastError(olmSession);
+ const auto lastErr = lastError(olmSession);
+ if (lastErr == OlmError::NotEnoughRandom) {
+ throw lastErr;
+ }
+ return lastErr;
}
return std::make_unique<QOlmSession>(olmSession);
}
-std::unique_ptr<QOlmSession> QOlmSession::createInboundSession(QOlmAccount& account, const Message &preKeyMessage)
+std::variant<std::unique_ptr<QOlmSession>, OlmError> QOlmSession::createInboundSession(QOlmAccount *account, const Message &preKeyMessage)
{
return createInbound(account, preKeyMessage);
}
-std::unique_ptr<QOlmSession> QOlmSession::createInboundSessionFrom(QOlmAccount &account, const QString &theirIdentityKey, const Message &preKeyMessage)
+std::variant<std::unique_ptr<QOlmSession>, OlmError> QOlmSession::createInboundSessionFrom(QOlmAccount *account, const QString &theirIdentityKey, const Message &preKeyMessage)
{
return createInbound(account, preKeyMessage, true, theirIdentityKey);
}
-std::unique_ptr<QOlmSession> QOlmSession::createOutboundSession(QOlmAccount &account, const QString &theirIdentityKey, const QString &theirOneTimeKey)
+std::variant<std::unique_ptr<QOlmSession>, OlmError> QOlmSession::createOutboundSession(QOlmAccount *account, const QString &theirIdentityKey, const QString &theirOneTimeKey)
{
auto *olmOutboundSession = create();
const auto randomLen = olm_create_outbound_session_random_length(olmOutboundSession);
@@ -69,13 +73,17 @@ std::unique_ptr<QOlmSession> QOlmSession::createOutboundSession(QOlmAccount &acc
QByteArray theirIdentityKeyBuf = theirIdentityKey.toUtf8();
QByteArray theirOneTimeKeyBuf = theirOneTimeKey.toUtf8();
const auto error = olm_create_outbound_session(olmOutboundSession,
- account.data(),
+ 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());
if (error == olm_error()) {
- throw lastError(olmOutboundSession);
+ const auto lastErr = lastError(olmOutboundSession);
+ if (lastErr == OlmError::NotEnoughRandom) {
+ throw lastErr;
+ }
+ return lastErr;
}
randomBuf.clear();
diff --git a/lib/olm/session.h b/lib/olm/session.h
index 76c1df29..e3a52c88 100644
--- a/lib/olm/session.h
+++ b/lib/olm/session.h
@@ -13,15 +13,17 @@
namespace Quotient {
+class QOlmAccount;
+
//! 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::unique_ptr<QOlmSession> createInboundSession(QOlmAccount& account, const Message& preKeyMessage);
- static std::unique_ptr<QOlmSession> createInboundSessionFrom(QOlmAccount& account, const QString& theirIdentityKey, const Message& preKeyMessage);
- static std::unique_ptr<QOlmSession> createOutboundSession(QOlmAccount& account, const QString& theirIdentityKey, const QString& theirOneTimeKey);
+ 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`.
@@ -37,7 +39,7 @@ public:
private:
//! Helper function for creating new sessions and handling errors.
static OlmSession* create();
- static std::unique_ptr<QOlmSession> createInbound(QOlmAccount& account, const Message& preKeyMessage, bool from = false, const QString& theirIdentityKey = "");
+ static std::variant<std::unique_ptr<QOlmSession>, OlmError> createInbound(QOlmAccount *account, const Message& preKeyMessage, bool from = false, const QString& theirIdentityKey = "");
OlmSession* m_session;
};