libQuotient
A Qt library for building matrix clients
qolmsession.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 Alexey Andreyev <aa13q@ya.ru>
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 
5 #pragma once
6 
7 #include <Quotient/e2ee/e2ee_common.h>
8 #include <Quotient/e2ee/qolmmessage.h>
9 
10 struct OlmSession;
11 
12 namespace Quotient {
13 
14 class QOlmAccount;
15 
16 //! Either an outbound or inbound session for secure communication.
17 class QUOTIENT_API QOlmSession
18 {
19 public:
20  //! Serialises an `QOlmSession` to encrypted Base64.
21  QByteArray pickle(const PicklingKey& key) const;
22 
23  //! Deserialises from encrypted Base64 previously made with pickle()
24  static QOlmExpected<QOlmSession> unpickle(QByteArray&& pickled,
25  const PicklingKey& key);
26 
27  //! Encrypts a plaintext message using the session.
28  QOlmMessage encrypt(const QByteArray& plaintext) const;
29 
30  //! Decrypts a message using this session. Decoding is lossy, meaning if
31  //! the decrypted plaintext contains invalid UTF-8 symbols, they will
32  //! be returned as `U+FFFD`.
33  QOlmExpected<QByteArray> decrypt(const QOlmMessage &message) const;
34 
35  //! Get a base64-encoded identifier for this session.
36  QByteArray sessionId() const;
37 
38  //! Checker for any received messages for this session.
39  bool hasReceivedMessage() const;
40 
41  //! Checks if the 'prekey' message is for this in-bound session.
42  bool matchesInboundSession(const QOlmMessage& preKeyMessage) const;
43 
44  //! Checks if the 'prekey' message is for this in-bound session.
45  bool matchesInboundSessionFrom(QByteArray theirIdentityKey,
46  const QOlmMessage& preKeyMessage) const;
47 
48  friend bool operator<(const QOlmSession& lhs, const QOlmSession& rhs)
49  {
50  return lhs.sessionId() < rhs.sessionId();
51  }
52 
53  OlmErrorCode lastErrorCode() const;
54  const char* lastError() const;
55 
56 private:
57  QOlmSession();
58  CStructPtr<OlmSession> olmDataHolder;
59  OlmSession* olmData = olmDataHolder.get();
60 
61  friend class QOlmAccount;
62 };
63 } //namespace Quotient