Botan 3.11.1
Crypto and TLS for C&
Botan::TLS::Datagram_Handshake_IO Class Referencefinal

#include <tls_handshake_io.h>

Inheritance diagram for Botan::TLS::Datagram_Handshake_IO:
Botan::TLS::Handshake_IO

Public Types

typedef std::function< void(uint16_t, Record_Type, const std::vector< uint8_t > &)> writer_fn

Public Member Functions

void add_record (const uint8_t record[], size_t record_len, 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)
std::vector< uint8_t > format (const std::vector< uint8_t > &handshake_msg, Handshake_Type handshake_type) const override
std::pair< Handshake_Type, std::vector< uint8_t > > get_next_record (bool expecting_ccs) override
bool have_more_data () const override
Protocol_Version initial_record_version () const override
std::vector< uint8_t > send (const Handshake_Message &msg) override
std::vector< uint8_t > send_under_epoch (const Handshake_Message &msg, uint16_t epoch) override
bool timeout_check () override

Detailed Description

Handshake IO for datagram-based handshakes

Definition at line 112 of file tls_handshake_io.h.

Member Typedef Documentation

◆ writer_fn

typedef std::function<void(uint16_t, Record_Type, const std::vector<uint8_t>&)> Botan::TLS::Datagram_Handshake_IO::writer_fn

Definition at line 114 of file tls_handshake_io.h.

Constructor & Destructor Documentation

◆ Datagram_Handshake_IO()

Botan::TLS::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 )
inline

Definition at line 116 of file tls_handshake_io.h.

120 :
121 m_seqs(seq),
122 m_flights(1),
123 m_initial_timeout(initial_timeout_ms),
124 m_max_timeout(max_timeout_ms),
125 m_send_hs(std::move(writer)),
126 m_mtu(mtu) {}

Member Function Documentation

◆ add_record()

void Botan::TLS::Datagram_Handshake_IO::add_record ( const uint8_t record[],
size_t record_len,
Record_Type type,
uint64_t sequence_number )
overridevirtual

Implements Botan::TLS::Handshake_IO.

Definition at line 186 of file tls_handshake_io.cpp.

189 {
190 const uint16_t epoch = static_cast<uint16_t>(record_sequence >> 48);
191
192 if(record_type == Record_Type::ChangeCipherSpec) {
193 if(record_len != 1 || record[0] != 1) {
194 throw Decoding_Error("Invalid ChangeCipherSpec");
195 }
196
197 // TODO: check this is otherwise empty
198 m_ccs_epochs.insert(epoch);
199 return;
200 }
201
202 const size_t DTLS_HANDSHAKE_HEADER_LEN = 12;
203
204 while(record_len > 0) {
205 if(record_len < DTLS_HANDSHAKE_HEADER_LEN) {
206 return; // completely bogus? at least degenerate/weird
207 }
208
209 const Handshake_Type msg_type = static_cast<Handshake_Type>(record[0]);
210 const size_t msg_len = load_be24(&record[1]);
211 const uint16_t message_seq = load_be<uint16_t>(&record[4], 0);
212 const size_t fragment_offset = load_be24(&record[6]);
213 const size_t fragment_length = load_be24(&record[9]);
214
215 const size_t total_size = DTLS_HANDSHAKE_HEADER_LEN + fragment_length;
216
217 if(record_len < total_size) {
218 throw Decoding_Error("Bad lengths in DTLS header");
219 }
220
221 if(message_seq >= m_in_message_seq) {
222 m_messages[message_seq].add_fragment(
223 &record[DTLS_HANDSHAKE_HEADER_LEN], fragment_length, fragment_offset, epoch, msg_type, msg_len);
224 } else {
225 // TODO: detect retransmitted flight
226 }
227
228 record += total_size;
229 record_len -= total_size;
230 }
231}
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504

References Botan::TLS::ChangeCipherSpec, and Botan::load_be().

◆ format()

std::vector< uint8_t > Botan::TLS::Datagram_Handshake_IO::format ( const std::vector< uint8_t > & handshake_msg,
Handshake_Type handshake_type ) const
overridevirtual

Implements Botan::TLS::Handshake_IO.

Definition at line 357 of file tls_handshake_io.cpp.

357 {
358 return format_w_seq(msg, type, m_in_message_seq - 1);
359}

◆ get_next_record()

std::pair< Handshake_Type, std::vector< uint8_t > > Botan::TLS::Datagram_Handshake_IO::get_next_record ( bool expecting_ccs)
overridevirtual

Returns (HANDSHAKE_NONE, std::vector<>()) if no message currently available

Implements Botan::TLS::Handshake_IO.

Definition at line 233 of file tls_handshake_io.cpp.

233 {
234 // Expecting a message means the last flight is concluded
235 if(!m_flights.rbegin()->empty()) {
236 m_flights.push_back(std::vector<uint16_t>());
237 }
238
239 if(expecting_ccs) {
240 if(!m_messages.empty()) {
241 const uint16_t current_epoch = m_messages.begin()->second.epoch();
242
243 if(m_ccs_epochs.contains(current_epoch)) {
244 return std::make_pair(Handshake_Type::HandshakeCCS, std::vector<uint8_t>());
245 }
246 }
247 return std::make_pair(Handshake_Type::None, std::vector<uint8_t>());
248 }
249
250 auto i = m_messages.find(m_in_message_seq);
251
252 if(i == m_messages.end() || !i->second.complete()) {
253 return std::make_pair(Handshake_Type::None, std::vector<uint8_t>());
254 }
255
256 m_in_message_seq += 1;
257
258 return i->second.message();
259}

References Botan::TLS::HandshakeCCS, and Botan::TLS::None.

◆ have_more_data()

bool Botan::TLS::Datagram_Handshake_IO::have_more_data ( ) const
overridevirtual

Implements Botan::TLS::Handshake_IO.

Definition at line 161 of file tls_handshake_io.cpp.

161 {
162 return false;
163}

◆ initial_record_version()

Protocol_Version Botan::TLS::Datagram_Handshake_IO::initial_record_version ( ) const
overridevirtual

Implements Botan::TLS::Handshake_IO.

Definition at line 131 of file tls_handshake_io.cpp.

131 {
132 return Protocol_Version::DTLS_V12;
133}

◆ send()

std::vector< uint8_t > Botan::TLS::Datagram_Handshake_IO::send ( const Handshake_Message & msg)
overridevirtual

Implements Botan::TLS::Handshake_IO.

Definition at line 361 of file tls_handshake_io.cpp.

361 {
362 return this->send_under_epoch(msg, m_seqs.current_write_epoch());
363}
std::vector< uint8_t > send_under_epoch(const Handshake_Message &msg, uint16_t epoch) override

References send_under_epoch().

◆ send_under_epoch()

std::vector< uint8_t > Botan::TLS::Datagram_Handshake_IO::send_under_epoch ( const Handshake_Message & msg,
uint16_t epoch )
overridevirtual

Implements Botan::TLS::Handshake_IO.

Definition at line 365 of file tls_handshake_io.cpp.

365 {
366 const std::vector<uint8_t> msg_bits = msg.serialize();
367 const Handshake_Type msg_type = msg.type();
368
369 if(msg_type == Handshake_Type::HandshakeCCS) {
370 m_send_hs(epoch, Record_Type::ChangeCipherSpec, msg_bits);
371 return std::vector<uint8_t>(); // not included in handshake hashes
372 } else if(msg_type == Handshake_Type::HelloVerifyRequest) {
373 // This message is not included in the handshake hashes
374 send_message(m_out_message_seq, epoch, msg_type, msg_bits);
375 m_out_message_seq += 1;
376 return std::vector<uint8_t>();
377 }
378
379 // Note: not saving CCS, instead we know it was there due to change in epoch
380 m_flights.rbegin()->push_back(m_out_message_seq);
381 m_flight_data[m_out_message_seq] = Message_Info(epoch, msg_type, msg_bits);
382
383 m_out_message_seq += 1;
384 m_last_write = steady_clock_ms();
385 m_next_timeout = m_initial_timeout;
386
387 return send_message(m_out_message_seq - 1, epoch, msg_type, msg_bits);
388}

References Botan::TLS::ChangeCipherSpec, Botan::TLS::HandshakeCCS, Botan::TLS::HelloVerifyRequest, Botan::TLS::Handshake_Message::serialize(), and Botan::TLS::Handshake_Message::type().

Referenced by send().

◆ timeout_check()

bool Botan::TLS::Datagram_Handshake_IO::timeout_check ( )
overridevirtual

Implements Botan::TLS::Handshake_IO.

Definition at line 165 of file tls_handshake_io.cpp.

165 {
166 if(m_last_write == 0 || (m_flights.size() > 1 && !m_flights.rbegin()->empty())) {
167 /*
168 If we haven't written anything yet obviously no timeout.
169 Also no timeout possible if we are mid-flight,
170 */
171 return false;
172 }
173
174 const uint64_t ms_since_write = steady_clock_ms() - m_last_write;
175
176 if(ms_since_write < m_next_timeout) {
177 return false;
178 }
179
180 retransmit_last_flight();
181
182 m_next_timeout = std::min(2 * m_next_timeout, m_max_timeout);
183 return true;
184}

The documentation for this class was generated from the following files: