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 <maybe.hpp>
14 
15 #include "crypto-util.hpp"
16 
17 namespace Kazv
18 {
19  class Crypto;
20 
21  struct InboundSessionTag {};
22  struct OutboundSessionTag {};
23 
24  struct SessionPrivate;
25  struct CryptoPrivate;
26  class Session
27  {
31  static std::size_t constructOutboundRandomSize();
32 
39  explicit Session(OutboundSessionTag,
40  RandomTag,
41  RandomData data,
42  CryptoPrivate &cryptoD,
43  std::string theirIdentityKey,
44  std::string theirOneTimeKey);
45 
46  // Creates an inbound session
47  explicit Session(InboundSessionTag,
48  CryptoPrivate &cryptoD,
49  std::string theirIdentityKey,
50  std::string message);
51 
52  public:
53  explicit Session();
54 
55  Session(const Session &that);
56  Session(Session &&that);
57  Session &operator=(const Session &that);
58  Session &operator=(Session &&that);
60 
61  bool matches(std::string message);
62 
63  bool valid() const;
64 
65  MaybeString decrypt(int type, std::string message);
66 
70  std::size_t encryptRandomSize() const;
71 
81  std::pair<int /* type */, std::string /* message */> encryptWithRandom(
82  RandomData random, std::string plainText);
83 
84  private:
85  friend class Crypto;
86  friend struct CryptoPrivate;
87  friend struct SessionPrivate;
88 
89  friend void to_json(nlohmann::json &j, const Session &s);
90  friend void from_json(const nlohmann::json &j, Session &s);
91  std::unique_ptr<SessionPrivate> m_d;
92  };
93 }
Definition: crypto.hpp:36
Definition: maybe.hpp:25
Definition: session.hpp:27
std::size_t encryptRandomSize() const
Definition: session.cpp:214
MaybeString decrypt(int type, std::string message)
Definition: session.cpp:197
bool valid() const
Definition: session.cpp:191
friend void from_json(const nlohmann::json &j, Session &s)
Definition: session.cpp:245
Session()
Definition: session.cpp:120
std::pair< int, std::string > encryptWithRandom(RandomData random, std::string plainText)
Encrypt plainText.
Definition: session.cpp:219
friend void to_json(nlohmann::json &j, const Session &s)
Definition: session.cpp:236
bool matches(std::string message)
Definition: session.cpp:178
Session & operator=(const Session &that)
Definition: session.cpp:165
Definition: location.hpp:10
std::string RandomData
Definition: crypto-util.hpp:34
nlohmann::json json
Definition: jsonwrap.hpp:20
Definition: crypto-p.hpp:26
Definition: session.hpp:21
Definition: session.hpp:22
The tag to indicate that a constructor should use user-provided random data.
Definition: crypto-util.hpp:32
Definition: session-p.hpp:16