libkazv
session.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of libkazv.
3  * SPDX-FileCopyrightText: 2021 Tusooa Zhu <tusooa@kazv.moe>
4  * SPDX-License-Identifier: AGPL-3.0-or-later
5  */
6 
7 #pragma once
8 #include <libkazv-config.hpp>
9 
10 #include <memory>
11 #include <tuple>
12 
13 #include <olm/olm.h>
14 
15 #include <maybe.hpp>
16 
17 #include "crypto-util.hpp"
18 
19 namespace Kazv
20 {
21  class Crypto;
22 
23  struct InboundSessionTag {};
24  struct OutboundSessionTag {};
25 
26  struct SessionPrivate;
27  class Session
28  {
32  static std::size_t constructOutboundRandomSize();
33 
34  // Creates an outbound session
35  [[deprecated("Use deterministic variant instead. In the future, this will be removed.")]]
36  explicit Session(OutboundSessionTag,
37  OlmAccount *acc,
38  std::string theirIdentityKey,
39  std::string theirOneTimeKey);
40 
47  explicit Session(OutboundSessionTag,
48  RandomTag,
49  RandomData data,
50  OlmAccount *acc,
51  std::string theirIdentityKey,
52  std::string theirOneTimeKey);
53 
54  // Creates an inbound session
55  explicit Session(InboundSessionTag,
56  OlmAccount *acc,
57  std::string theirIdentityKey,
58  std::string message);
59 
60  public:
61  explicit Session();
62 
63  Session(const Session &that);
64  Session(Session &&that);
65  Session &operator=(const Session &that);
66  Session &operator=(Session &&that);
67  ~Session();
68 
69  bool matches(std::string message);
70 
71  bool valid() const;
72 
73  MaybeString decrypt(int type, std::string message);
74 
78  std::size_t encryptRandomSize() const;
79 
80  [[deprecated("Use deterministic variant instead. In the future, this will be removed.")]]
81  std::pair<int /* type */, std::string /* message */> encrypt(std::string plainText);
82 
92  std::pair<int /* type */, std::string /* message */> encryptWithRandom(
93  RandomData random, std::string plainText);
94 
95  private:
96  friend class Crypto;
97  friend class CryptoPrivate;
98  friend class SessionPrivate;
99 
100  friend void to_json(nlohmann::json &j, const Session &s);
101  friend void from_json(const nlohmann::json &j, Session &s);
102  std::unique_ptr<SessionPrivate> m_d;
103  };
104 }
maybe.hpp
Kazv::Session::matches
bool matches(std::string message)
Definition: session.cpp:201
Kazv::Session
Definition: session.hpp:27
Kazv::RandomData
std::string RandomData
Definition: crypto-util.hpp:35
Kazv::InboundSessionTag
Definition: session.hpp:23
crypto-util.hpp
Kazv::Session::~Session
~Session()
Kazv::Session::encrypt
std::pair< int, std::string > encrypt(std::string plainText)
Definition: session.cpp:246
Kazv::Maybe
Definition: maybe.hpp:24
Kazv
Definition: location.hpp:10
Kazv::SessionPrivate
Definition: session-p.hpp:14
Kazv::Session::to_json
friend void to_json(nlohmann::json &j, const Session &s)
Definition: session.cpp:273
Kazv::Session::decrypt
MaybeString decrypt(int type, std::string message)
Definition: session.cpp:216
Kazv::json
nlohmann::json json
Definition: jsonwrap.hpp:20
Kazv::Session::from_json
friend void from_json(const nlohmann::json &j, Session &s)
Definition: session.cpp:281
Kazv::CryptoPrivate
Definition: crypto-p.hpp:24
Kazv::Crypto
Definition: crypto.hpp:35
Kazv::OutboundSessionTag
Definition: session.hpp:24
Kazv::Session::encryptRandomSize
std::size_t encryptRandomSize() const
Definition: session.cpp:241
libkazv-config.hpp
Kazv::RandomTag
The tag to indicate that a constructor should use user-provided random data.
Definition: crypto-util.hpp:33
Kazv::Session::encryptWithRandom
std::pair< int, std::string > encryptWithRandom(RandomData random, std::string plainText)
Encrypt plainText.
Definition: session.cpp:251
Kazv::Session::Session
Session()
Definition: session.cpp:131
Kazv::Session::operator=
Session & operator=(const Session &that)
Definition: session.cpp:188
Kazv::Session::valid
bool valid() const
Definition: session.cpp:210