libQuotient
A Qt library for building matrix clients
qolmaccount.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 
5 
6 #pragma once
7 
8 #include <Quotient/e2ee/e2ee_common.h>
9 #include <Quotient/e2ee/qolmmessage.h>
10 
11 #include <Quotient/csapi/keys.h>
12 
13 #include <QtCore/QObject>
14 
15 struct OlmAccount;
16 
17 namespace Quotient {
18 
19 class QOlmSession;
20 
21 //! An olm account manages all cryptographic keys used on a device.
22 //! \code{.cpp}
23 //! const auto olmAccount = new QOlmAccount(this);
24 //! \endcode
25 class QUOTIENT_API QOlmAccount : public QObject
26 {
27  Q_OBJECT
28 public:
29  QOlmAccount(QString userId, QString deviceId, QObject* parent = nullptr);
30 
31  //! Creates a new instance of OlmAccount. During the instantiation
32  //! the Ed25519 fingerprint key pair and the Curve25519 identity key
33  //! pair are generated.
34  //! \sa https://matrix.org/docs/guides/e2e_implementation.html#keys-used-in-end-to-end-encryption
35  //! \note This needs to be called before any other action or use unpickle() instead.
36  void setupNewAccount();
37 
38  //! Deserialises from encrypted Base64 that was previously obtained by pickling a `QOlmAccount`.
39  //! \note This needs to be called before any other action or use setupNewAccount() instead.
40  [[nodiscard]] OlmErrorCode unpickle(QByteArray&& pickled,
41  const PicklingKey& key);
42 
43  //! Serialises an OlmAccount to encrypted Base64.
44  QByteArray pickle(const PicklingKey& key) const;
45 
46  //! Returns the account's public identity keys already formatted as JSON
47  IdentityKeys identityKeys() const;
48 
49  //! Returns the signature of the supplied message.
50  QByteArray sign(const QByteArray &message) const;
51  QByteArray sign(const QJsonObject& message) const;
52 
53  //! Sign identity keys.
54  QByteArray signIdentityKeys() const;
55 
56  //! Maximum number of one time keys that this OlmAccount can
57  //! currently hold.
58  size_t maxNumberOfOneTimeKeys() const;
59 
60  //! Generates the supplied number of one time keys.
61  size_t generateOneTimeKeys(size_t numberOfKeys);
62 
63  //! Gets the OlmAccount's one time keys formatted as JSON.
64  UnsignedOneTimeKeys oneTimeKeys() const;
65 
66  //! Sign all one time keys.
67  OneTimeKeys signOneTimeKeys(const UnsignedOneTimeKeys &keys) const;
68 
69  UploadKeysJob* createUploadKeyRequest(const UnsignedOneTimeKeys& oneTimeKeys) const;
70 
71  DeviceKeys deviceKeys() const;
72 
73  //! Remove the one time key used to create the supplied session.
74  [[nodiscard]] OlmErrorCode removeOneTimeKeys(const QOlmSession& session);
75 
76  //! Creates an inbound session for sending/receiving messages from a received 'prekey' message.
77  //!
78  //! \param preKeyMessage An Olm pre-key message that was encrypted for this account.
79  QOlmExpected<QOlmSession> createInboundSession(
80  const QOlmMessage& preKeyMessage) const;
81 
82  //! Creates an inbound session for sending/receiving messages from a received 'prekey' message.
83  //!
84  //! \param theirIdentityKey - The identity key of the Olm account that
85  //! encrypted this Olm message.
86  QOlmExpected<QOlmSession> createInboundSessionFrom(
87  const QByteArray& theirIdentityKey,
88  const QOlmMessage& preKeyMessage) const;
89 
90  //! Creates an outbound session for sending messages to a specific
91  /// identity and one time key.
92  QOlmExpected<QOlmSession> createOutboundSession(
93  const QByteArray& theirIdentityKey,
94  const QByteArray& theirOneTimeKey) const;
95 
96  void markKeysAsPublished();
97 
98  OlmErrorCode lastErrorCode() const;
99  const char* lastError() const;
100 
101 Q_SIGNALS:
102  void needsSave();
103 
104 private:
105  CStructPtr<OlmAccount> olmDataHolder;
106  QString m_userId;
107  QString m_deviceId;
108  OlmAccount* olmData = olmDataHolder.get();
109 
110  QOlmExpected<QOlmSession> createInbound(QOlmMessage preKeyMessage,
111  const QByteArray &theirIdentityKey = "") const;
112 
113  QString accountId() const;
114 };
115 
116 // TODO, 0.9: Move the two below to qolmutility.h
117 
118 QUOTIENT_API bool verifyIdentitySignature(const DeviceKeys& deviceKeys,
119  const QString& deviceId,
120  const QString& userId);
121 
122 //! checks if the signature is signed by the signing_key
123 QUOTIENT_API bool ed25519VerifySignature(const QString& signingKey,
124  const QJsonObject& obj,
125  const QString& signature);
126 
127 } // namespace Quotient