Botan  2.11.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 uint8_t record[],
43  size_t record_len,
45  uint64_t sequence_number) = 0;
46 
47  /**
48  * Returns (HANDSHAKE_NONE, std::vector<>()) if no message currently available
49  */
50  virtual std::pair<Handshake_Type, std::vector<uint8_t>>
51  get_next_record(bool expecting_ccs) = 0;
52 
53  Handshake_IO() = default;
54 
55  Handshake_IO(const Handshake_IO&) = delete;
56 
57  Handshake_IO& operator=(const Handshake_IO&) = delete;
58 
59  virtual ~Handshake_IO() = default;
60  };
61 
62 /**
63 * Handshake IO for stream-based handshakes
64 */
66  {
67  public:
68  typedef std::function<void (uint8_t, const std::vector<uint8_t>&)> writer_fn;
69 
70  explicit Stream_Handshake_IO(writer_fn writer) : m_send_hs(writer) {}
71 
73 
74  bool timeout_check() override { return false; }
75 
76  std::vector<uint8_t> send(const Handshake_Message& msg) override;
77 
78  std::vector<uint8_t> format(
79  const std::vector<uint8_t>& handshake_msg,
80  Handshake_Type handshake_type) const override;
81 
82  void add_record(const uint8_t record[],
83  size_t record_len,
85  uint64_t sequence_number) override;
86 
87  std::pair<Handshake_Type, std::vector<uint8_t>>
88  get_next_record(bool expecting_ccs) override;
89  private:
90  std::deque<uint8_t> m_queue;
91  writer_fn m_send_hs;
92  };
93 
94 /**
95 * Handshake IO for datagram-based handshakes
96 */
98  {
99  public:
100  typedef std::function<void (uint16_t, uint8_t, const std::vector<uint8_t>&)> writer_fn;
101 
103  class Connection_Sequence_Numbers& seq,
104  uint16_t mtu, uint64_t initial_timeout_ms, uint64_t max_timeout_ms) :
105  m_seqs(seq),
106  m_flights(1),
107  m_initial_timeout(initial_timeout_ms),
108  m_max_timeout(max_timeout_ms),
109  m_send_hs(writer),
110  m_mtu(mtu)
111  {}
112 
113  Protocol_Version initial_record_version() const override;
114 
115  bool timeout_check() override;
116 
117  std::vector<uint8_t> send(const Handshake_Message& msg) override;
118 
119  std::vector<uint8_t> format(
120  const std::vector<uint8_t>& handshake_msg,
121  Handshake_Type handshake_type) const override;
122 
123  void add_record(const uint8_t record[],
124  size_t record_len,
126  uint64_t sequence_number) override;
127 
128  std::pair<Handshake_Type, std::vector<uint8_t>>
129  get_next_record(bool expecting_ccs) override;
130  private:
131  void retransmit_flight(size_t flight);
132  void retransmit_last_flight();
133 
134  std::vector<uint8_t> format_fragment(
135  const uint8_t fragment[],
136  size_t fragment_len,
137  uint16_t frag_offset,
138  uint16_t msg_len,
140  uint16_t msg_sequence) const;
141 
142  std::vector<uint8_t> format_w_seq(
143  const std::vector<uint8_t>& handshake_msg,
144  Handshake_Type handshake_type,
145  uint16_t msg_sequence) const;
146 
147  std::vector<uint8_t> send_message(uint16_t msg_seq, uint16_t epoch,
148  Handshake_Type msg_type,
149  const std::vector<uint8_t>& msg);
150 
151  class Handshake_Reassembly final
152  {
153  public:
154  void add_fragment(const uint8_t fragment[],
155  size_t fragment_length,
156  size_t fragment_offset,
157  uint16_t epoch,
158  uint8_t msg_type,
159  size_t msg_length);
160 
161  bool complete() const;
162 
163  uint16_t epoch() const { return m_epoch; }
164 
165  std::pair<Handshake_Type, std::vector<uint8_t>> message() const;
166  private:
167  uint8_t m_msg_type = HANDSHAKE_NONE;
168  size_t m_msg_length = 0;
169  uint16_t m_epoch = 0;
170 
171  // vector<bool> m_seen;
172  // vector<uint8_t> m_fragments
173  std::map<size_t, uint8_t> m_fragments;
174  std::vector<uint8_t> m_message;
175  };
176 
177  struct Message_Info final
178  {
179  Message_Info(uint16_t e, Handshake_Type mt, const std::vector<uint8_t>& msg) :
180  epoch(e), msg_type(mt), msg_bits(msg) {}
181 
182  Message_Info() : epoch(0xFFFF), msg_type(HANDSHAKE_NONE) {}
183 
184  uint16_t epoch;
185  Handshake_Type msg_type;
186  std::vector<uint8_t> msg_bits;
187  };
188 
189  class Connection_Sequence_Numbers& m_seqs;
190  std::map<uint16_t, Handshake_Reassembly> m_messages;
191  std::set<uint16_t> m_ccs_epochs;
192  std::vector<std::vector<uint16_t>> m_flights;
193  std::map<uint16_t, Message_Info> m_flight_data;
194 
195  uint64_t m_initial_timeout = 0;
196  uint64_t m_max_timeout = 0;
197 
198  uint64_t m_last_write = 0;
199  uint64_t m_next_timeout = 0;
200 
201  uint16_t m_in_message_seq = 0;
202  uint16_t m_out_message_seq = 0;
203 
204  writer_fn m_send_hs;
205  uint16_t m_mtu;
206  };
207 
208 }
209 
210 }
211 
212 #endif
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=0
void add_record(const uint8_t record[], size_t record_len, Record_Type type, uint64_t sequence_number) override
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
int(* final)(unsigned char *, CTX *)
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
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 uint8_t record[], size_t record_len, Record_Type type, uint64_t sequence_number)=0
void add_record(const uint8_t record[], size_t record_len, Record_Type type, uint64_t sequence_number) override
std::vector< uint8_t > format(const std::vector< uint8_t > &handshake_msg, Handshake_Type handshake_type) const 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