Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 <botan/loadstor.h>
14 #include <functional>
15 #include <vector>
16 #include <deque>
17 #include <map>
18 #include <set>
19 #include <utility>
20
21 namespace Botan {
22
23 namespace TLS {
24
25 class Handshake_Message;
26
27 /**
28 * Handshake IO Interface
29 */
30 class Handshake_IO
31 {
32 public:
33 virtual Protocol_Version initial_record_version() const = 0;
34
35 virtual std::vector<uint8_t> send(const Handshake_Message& msg) = 0;
36
37 virtual bool timeout_check() = 0;
38
39 virtual std::vector<uint8_t> format(
40 const std::vector<uint8_t>& handshake_msg,
41 Handshake_Type handshake_type) const = 0;
42
43 virtual void add_record(const std::vector<uint8_t>& record,
44 Record_Type type,
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 */
65 class Stream_Handshake_IO final : public Handshake_IO
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
72 Protocol_Version initial_record_version() const override;
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 std::vector<uint8_t>& record,
83 Record_Type type,
84 uint64_t sequence_number) override;
85
86 std::pair<Handshake_Type, std::vector<uint8_t>>
87 get_next_record(bool expecting_ccs) override;
88 private:
89 std::deque<uint8_t> m_queue;
90 writer_fn m_send_hs;
91 };
92
93 /**
94 * Handshake IO for datagram-based handshakes
95 */
96 class Datagram_Handshake_IO final : public Handshake_IO
97 {
98 public:
99 typedef std::function<void (uint16_t, uint8_t, const std::vector<uint8_t>&)> writer_fn;
100
101 Datagram_Handshake_IO(writer_fn writer,
102 class Connection_Sequence_Numbers& seq,
103 uint16_t mtu, uint64_t initial_timeout_ms, uint64_t max_timeout_ms) :
104 m_seqs(seq),
105 m_flights(1),
106 m_initial_timeout(initial_timeout_ms),
107 m_max_timeout(max_timeout_ms),
108 m_send_hs(writer),
109 m_mtu(mtu)
110 {}
111
112 Protocol_Version initial_record_version() const override;
113
114 bool timeout_check() override;
115
116 std::vector<uint8_t> send(const Handshake_Message& msg) override;
117
118 std::vector<uint8_t> format(
119 const std::vector<uint8_t>& handshake_msg,
120 Handshake_Type handshake_type) const override;
121
122 void add_record(const std::vector<uint8_t>& record,
123 Record_Type type,
124 uint64_t sequence_number) override;
125
126 std::pair<Handshake_Type, std::vector<uint8_t>>
127 get_next_record(bool expecting_ccs) override;
128 private:
129 void retransmit_flight(size_t flight);
130 void retransmit_last_flight();
131
132 std::vector<uint8_t> format_fragment(
133 const uint8_t fragment[],
134 size_t fragment_len,
135 uint16_t frag_offset,
136 uint16_t msg_len,
137 Handshake_Type type,
138 uint16_t msg_sequence) const;
139
140 std::vector<uint8_t> format_w_seq(
141 const std::vector<uint8_t>& handshake_msg,
142 Handshake_Type handshake_type,
143 uint16_t msg_sequence) const;
144
145 std::vector<uint8_t> send_message(uint16_t msg_seq, uint16_t epoch,
146 Handshake_Type msg_type,
147 const std::vector<uint8_t>& msg);
148
149 class Handshake_Reassembly final
150 {
151 public:
152 void add_fragment(const uint8_t fragment[],
153 size_t fragment_length,
154 size_t fragment_offset,
155 uint16_t epoch,
156 uint8_t msg_type,
157 size_t msg_length);
158
159 bool complete() const;
160
161 uint16_t epoch() const { return m_epoch; }
162
163 std::pair<Handshake_Type, std::vector<uint8_t>> message() const;
164 private:
165 uint8_t m_msg_type = HANDSHAKE_NONE;
166 size_t m_msg_length = 0;
167 uint16_t m_epoch = 0;
168
169 // vector<bool> m_seen;
170 // vector<uint8_t> m_fragments
171 std::map<size_t, uint8_t> m_fragments;
172 std::vector<uint8_t> m_message;
173 };
174
175 struct Message_Info final
176 {
177 Message_Info(uint16_t e, Handshake_Type mt, const std::vector<uint8_t>& msg) :
178 epoch(e), msg_type(mt), msg_bits(msg) {}
179
180 Message_Info() : epoch(0xFFFF), msg_type(HANDSHAKE_NONE) {}
181
182 uint16_t epoch;
183 Handshake_Type msg_type;
184 std::vector<uint8_t> msg_bits;
185 };
186
187 class Connection_Sequence_Numbers& m_seqs;
188 std::map<uint16_t, Handshake_Reassembly> m_messages;
189 std::set<uint16_t> m_ccs_epochs;
190 std::vector<std::vector<uint16_t>> m_flights;
191 std::map<uint16_t, Message_Info> m_flight_data;
192
193 uint64_t m_initial_timeout = 0;
194 uint64_t m_max_timeout = 0;
195
196 uint64_t m_last_write = 0;
197 uint64_t m_next_timeout = 0;
198
199 uint16_t m_in_message_seq = 0;
200 uint16_t m_out_message_seq = 0;
201
202 writer_fn m_send_hs;
203 uint16_t m_mtu;
204 };
205
206 }
207
208 }
209
210 #endif
Botan::TLS::HANDSHAKE_NONE
Definition: tls_magic.h:56
Botan::TLS::Handshake_IO::send
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=0
Botan::TLS::Stream_Handshake_IO
Definition: tls_handshake_io.h:65
Botan::TLS::Datagram_Handshake_IO
Definition: tls_handshake_io.h:96
Botan::TLS::Datagram_Handshake_IO::timeout_check
bool timeout_check() override
Definition: tls_handshake_io.cpp:174
Botan::TLS::Stream_Handshake_IO::Stream_Handshake_IO
Stream_Handshake_IO(writer_fn writer)
Definition: tls_handshake_io.h:70
Botan::TLS::Handshake_IO::initial_record_version
virtual Protocol_Version initial_record_version() const =0
Botan::TLS::Datagram_Handshake_IO::initial_record_version
Protocol_Version initial_record_version() const override
Definition: tls_handshake_io.cpp:139
Botan::TLS::Stream_Handshake_IO::initial_record_version
Protocol_Version initial_record_version() const override
Definition: tls_handshake_io.cpp:56
Botan::TLS::Handshake_IO::format
virtual std::vector< uint8_t > format(const std::vector< uint8_t > &handshake_msg, Handshake_Type handshake_type) const =0
Botan::TLS::Stream_Handshake_IO::send
std::vector< uint8_t > send(const Handshake_Message &msg) override
Definition: tls_handshake_io.cpp:124
Botan::TLS::Protocol_Version
Definition: tls_version.h:21
type
MechanismType type
Definition: p11_mechanism.cpp:43
Botan::TLS::Handshake_IO::~Handshake_IO
virtual ~Handshake_IO()=default
Botan::TLS::Datagram_Handshake_IO::send
std::vector< uint8_t > send(const Handshake_Message &msg) override
Definition: tls_handshake_io.cpp:391
Botan::TLS::Stream_Handshake_IO::writer_fn
std::function< void(uint8_t, const std::vector< uint8_t > &)> writer_fn
Definition: tls_handshake_io.h:68
Botan::TLS::Datagram_Handshake_IO::get_next_record
std::pair< Handshake_Type, std::vector< uint8_t > > get_next_record(bool expecting_ccs) override
Definition: tls_handshake_io.cpp:250
Botan::TLS::Datagram_Handshake_IO::writer_fn
std::function< void(uint16_t, uint8_t, const std::vector< uint8_t > &)> writer_fn
Definition: tls_handshake_io.h:99
Botan::TLS::Stream_Handshake_IO::get_next_record
std::pair< Handshake_Type, std::vector< uint8_t > > get_next_record(bool expecting_ccs) override
Definition: tls_handshake_io.cpp:82
Botan::TLS::Stream_Handshake_IO::add_record
void add_record(const std::vector< uint8_t > &record, Record_Type type, uint64_t sequence_number) override
Definition: tls_handshake_io.cpp:61
Botan::TLS::Handshake_IO::Handshake_IO
Handshake_IO()=default
Botan::TLS::Handshake_Message
Definition: tls_handshake_msg.h:26
Botan::TLS::Handshake_IO::get_next_record
virtual std::pair< Handshake_Type, std::vector< uint8_t > > get_next_record(bool expecting_ccs)=0
Botan::TLS::Datagram_Handshake_IO::format
std::vector< uint8_t > format(const std::vector< uint8_t > &handshake_msg, Handshake_Type handshake_type) const override
Definition: tls_handshake_io.cpp:383
Botan::TLS::Stream_Handshake_IO::timeout_check
bool timeout_check() override
Definition: tls_handshake_io.h:74
Botan::TLS::Handshake_IO
Definition: tls_handshake_io.h:30
Botan
Definition: alg_id.cpp:13
Botan::TLS::Connection_Sequence_Numbers
Definition: tls_seq_numbers.h:17
Botan::TLS::Record_Type
Record_Type
Definition: tls_magic.h:29
Botan::TLS::Handshake_IO::timeout_check
virtual bool timeout_check()=0
Botan::TLS::Handshake_IO::add_record
virtual void add_record(const std::vector< uint8_t > &record, Record_Type type, uint64_t sequence_number)=0
Botan::TLS::Stream_Handshake_IO::format
std::vector< uint8_t > format(const std::vector< uint8_t > &handshake_msg, Handshake_Type handshake_type) const override
Definition: tls_handshake_io.cpp:105
Botan::TLS::Datagram_Handshake_IO::add_record
void add_record(const std::vector< uint8_t > &record, Record_Type type, uint64_t sequence_number) override
Definition: tls_handshake_io.cpp:196
Botan::TLS::Datagram_Handshake_IO::Datagram_Handshake_IO
Datagram_Handshake_IO(writer_fn writer, class Connection_Sequence_Numbers &seq, uint16_t mtu, uint64_t initial_timeout_ms, uint64_t max_timeout_ms)
Definition: tls_handshake_io.h:101
Botan::TLS::Handshake_Type
Handshake_Type
Definition: tls_magic.h:38
Botan::TLS::Handshake_IO::operator=
Handshake_IO & operator=(const Handshake_IO &)=delete