Botan  2.6.0
Crypto and TLS for C++11
tls_handshake_io.h
Go to the documentation of this file.
1 /*
2 * TLS Handshake Serialization
3 * (C) 2012,2014 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_TLS_HANDSHAKE_IO_H_
9 #define BOTAN_TLS_HANDSHAKE_IO_H_
10 
11 #include <botan/tls_magic.h>
12 #include <botan/tls_version.h>
13 #include <functional>
14 #include <vector>
15 #include <deque>
16 #include <map>
17 #include <set>
18 #include <utility>
19 
20 namespace Botan {
21 
22 namespace TLS {
23 
24 class Handshake_Message;
25 
26 /**
27 * Handshake IO Interface
28 */
30  {
31  public:
32  virtual Protocol_Version initial_record_version() const = 0;
33 
34  virtual std::vector<uint8_t> send(const Handshake_Message& msg) = 0;
35 
36  virtual bool timeout_check() = 0;
37 
38  virtual std::vector<uint8_t> format(
39  const std::vector<uint8_t>& handshake_msg,
40  Handshake_Type handshake_type) const = 0;
41 
42  virtual void add_record(const std::vector<uint8_t>& record,
44  uint64_t sequence_number) = 0;
45 
46  /**
47  * Returns (HANDSHAKE_NONE, std::vector<>()) if no message currently available
48  */
49  virtual std::pair<Handshake_Type, std::vector<uint8_t>>
50  get_next_record(bool expecting_ccs) = 0;
51 
52  Handshake_IO() = default;
53 
54  Handshake_IO(const Handshake_IO&) = delete;
55 
56  Handshake_IO& operator=(const Handshake_IO&) = delete;
57 
58  virtual ~Handshake_IO() = default;
59  };
60 
61 /**
62 * Handshake IO for stream-based handshakes
63 */
64 class Stream_Handshake_IO final : public Handshake_IO
65  {
66  public:
67  typedef std::function<void (uint8_t, const std::vector<uint8_t>&)> writer_fn;
68 
69  explicit Stream_Handshake_IO(writer_fn writer) : m_send_hs(writer) {}
70 
72 
73  bool timeout_check() override { return false; }
74 
75  std::vector<uint8_t> send(const Handshake_Message& msg) override;
76 
77  std::vector<uint8_t> format(
78  const std::vector<uint8_t>& handshake_msg,
79  Handshake_Type handshake_type) const override;
80 
81  void add_record(const std::vector<uint8_t>& record,
83  uint64_t sequence_number) override;
84 
85  std::pair<Handshake_Type, std::vector<uint8_t>>
86  get_next_record(bool expecting_ccs) override;
87  private:
88  std::deque<uint8_t> m_queue;
89  writer_fn m_send_hs;
90  };
91 
92 /**
93 * Handshake IO for datagram-based handshakes
94 */
95 class Datagram_Handshake_IO final : public Handshake_IO
96  {
97  public:
98  typedef std::function<void (uint16_t, uint8_t, const std::vector<uint8_t>&)> writer_fn;
99 
101  class Connection_Sequence_Numbers& seq,
102  uint16_t mtu, uint64_t initial_timeout_ms, uint64_t max_timeout_ms) :
103  m_seqs(seq),
104  m_flights(1),
105  m_initial_timeout(initial_timeout_ms),
106  m_max_timeout(max_timeout_ms),
107  m_send_hs(writer),
108  m_mtu(mtu)
109  {}
110 
111  Protocol_Version initial_record_version() const override;
112 
113  bool timeout_check() override;
114 
115  std::vector<uint8_t> send(const Handshake_Message& msg) override;
116 
117  std::vector<uint8_t> format(
118  const std::vector<uint8_t>& handshake_msg,
119  Handshake_Type handshake_type) const override;
120 
121  void add_record(const std::vector<uint8_t>& record,
123  uint64_t sequence_number) override;
124 
125  std::pair<Handshake_Type, std::vector<uint8_t>>
126  get_next_record(bool expecting_ccs) override;
127  private:
128  void retransmit_flight(size_t flight);
129  void retransmit_last_flight();
130 
131  std::vector<uint8_t> format_fragment(
132  const uint8_t fragment[],
133  size_t fragment_len,
134  uint16_t frag_offset,
135  uint16_t msg_len,
137  uint16_t msg_sequence) const;
138 
139  std::vector<uint8_t> format_w_seq(
140  const std::vector<uint8_t>& handshake_msg,
141  Handshake_Type handshake_type,
142  uint16_t msg_sequence) const;
143 
144  std::vector<uint8_t> send_message(uint16_t msg_seq, uint16_t epoch,
145  Handshake_Type msg_type,
146  const std::vector<uint8_t>& msg);
147 
148  class Handshake_Reassembly final
149  {
150  public:
151  void add_fragment(const uint8_t fragment[],
152  size_t fragment_length,
153  size_t fragment_offset,
154  uint16_t epoch,
155  uint8_t msg_type,
156  size_t msg_length);
157 
158  bool complete() const;
159 
160  uint16_t epoch() const { return m_epoch; }
161 
162  std::pair<Handshake_Type, std::vector<uint8_t>> message() const;
163  private:
164  uint8_t m_msg_type = HANDSHAKE_NONE;
165  size_t m_msg_length = 0;
166  uint16_t m_epoch = 0;
167 
168  // vector<bool> m_seen;
169  // vector<uint8_t> m_fragments
170  std::map<size_t, uint8_t> m_fragments;
171  std::vector<uint8_t> m_message;
172  };
173 
174  struct Message_Info final
175  {
176  Message_Info(uint16_t e, Handshake_Type mt, const std::vector<uint8_t>& msg) :
177  epoch(e), msg_type(mt), msg_bits(msg) {}
178 
179  Message_Info() : epoch(0xFFFF), msg_type(HANDSHAKE_NONE) {}
180 
181  uint16_t epoch;
182  Handshake_Type msg_type;
183  std::vector<uint8_t> msg_bits;
184  };
185 
186  class Connection_Sequence_Numbers& m_seqs;
187  std::map<uint16_t, Handshake_Reassembly> m_messages;
188  std::set<uint16_t> m_ccs_epochs;
189  std::vector<std::vector<uint16_t>> m_flights;
190  std::map<uint16_t, Message_Info> m_flight_data;
191 
192  uint64_t m_initial_timeout = 0;
193  uint64_t m_max_timeout = 0;
194 
195  uint64_t m_last_write = 0;
196  uint64_t m_next_timeout = 0;
197 
198  uint16_t m_in_message_seq = 0;
199  uint16_t m_out_message_seq = 0;
200 
201  writer_fn m_send_hs;
202  uint16_t m_mtu;
203  };
204 
205 }
206 
207 }
208 
209 #endif
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=0
Stream_Handshake_IO(writer_fn writer)
virtual Protocol_Version initial_record_version() const =0
Protocol_Version initial_record_version() const override
Protocol_Version initial_record_version() const override
virtual std::vector< uint8_t > format(const std::vector< uint8_t > &handshake_msg, Handshake_Type handshake_type) const =0
std::vector< uint8_t > send(const Handshake_Message &msg) override
MechanismType type
virtual ~Handshake_IO()=default
std::vector< uint8_t > send(const Handshake_Message &msg) override
std::function< void(uint8_t, const std::vector< uint8_t > &)> writer_fn
std::pair< Handshake_Type, std::vector< uint8_t > > get_next_record(bool expecting_ccs) override
std::function< void(uint16_t, uint8_t, const std::vector< uint8_t > &)> writer_fn
std::pair< Handshake_Type, std::vector< uint8_t > > get_next_record(bool expecting_ccs) override
void add_record(const std::vector< uint8_t > &record, Record_Type type, uint64_t sequence_number) override
virtual std::pair< Handshake_Type, std::vector< uint8_t > > get_next_record(bool expecting_ccs)=0
std::vector< uint8_t > format(const std::vector< uint8_t > &handshake_msg, Handshake_Type handshake_type) const override
Definition: alg_id.cpp:13
virtual bool timeout_check()=0
virtual void add_record(const std::vector< uint8_t > &record, Record_Type type, uint64_t sequence_number)=0
std::vector< uint8_t > format(const std::vector< uint8_t > &handshake_msg, Handshake_Type handshake_type) const override
void add_record(const std::vector< uint8_t > &record, Record_Type type, uint64_t sequence_number) override
Datagram_Handshake_IO(writer_fn writer, class Connection_Sequence_Numbers &seq, uint16_t mtu, uint64_t initial_timeout_ms, uint64_t max_timeout_ms)
Handshake_IO & operator=(const Handshake_IO &)=delete