Botan 3.0.0
Crypto and TLS for C&
Classes | Public Types | Public Member Functions | List of all members
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 122 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 125 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 127 of file tls_handshake_io.h.

129 :
130 m_seqs(seq),
131 m_flights(1),
132 m_initial_timeout(initial_timeout_ms),
133 m_max_timeout(max_timeout_ms),
134 m_send_hs(writer),
135 m_mtu(mtu)
136 {}

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 201 of file tls_handshake_io.cpp.

205 {
206 const uint16_t epoch = static_cast<uint16_t>(record_sequence >> 48);
207
208 if(record_type == Record_Type::ChangeCipherSpec)
209 {
210 if(record_len != 1 || record[0] != 1)
211 throw Decoding_Error("Invalid ChangeCipherSpec");
212
213 // TODO: check this is otherwise empty
214 m_ccs_epochs.insert(epoch);
215 return;
216 }
217
218 const size_t DTLS_HANDSHAKE_HEADER_LEN = 12;
219
220 while(record_len)
221 {
222 if(record_len < DTLS_HANDSHAKE_HEADER_LEN)
223 return; // completely bogus? at least degenerate/weird
224
225 const Handshake_Type msg_type = static_cast<Handshake_Type>(record[0]);
226 const size_t msg_len = load_be24(&record[1]);
227 const uint16_t message_seq = load_be<uint16_t>(&record[4], 0);
228 const size_t fragment_offset = load_be24(&record[6]);
229 const size_t fragment_length = load_be24(&record[9]);
230
231 const size_t total_size = DTLS_HANDSHAKE_HEADER_LEN + fragment_length;
232
233 if(record_len < total_size)
234 throw Decoding_Error("Bad lengths in DTLS header");
235
236 if(message_seq >= m_in_message_seq)
237 {
238 m_messages[message_seq].add_fragment(&record[DTLS_HANDSHAKE_HEADER_LEN],
239 fragment_length,
240 fragment_offset,
241 epoch,
242 msg_type,
243 msg_len);
244 }
245 else
246 {
247 // TODO: detect retransmitted flight
248 }
249
250 record += total_size;
251 record_len -= total_size;
252 }
253 }
constexpr uint16_t load_be< uint16_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:150

References Botan::TLS::ChangeCipherSpec, and Botan::load_be< uint16_t >().

◆ 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 392 of file tls_handshake_io.cpp.

394 {
395 return format_w_seq(msg, type, m_in_message_seq - 1);
396 }

◆ 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 256 of file tls_handshake_io.cpp.

257 {
258 // Expecting a message means the last flight is concluded
259 if(!m_flights.rbegin()->empty())
260 m_flights.push_back(std::vector<uint16_t>());
261
262 if(expecting_ccs)
263 {
264 if(!m_messages.empty())
265 {
266 const uint16_t current_epoch = m_messages.begin()->second.epoch();
267
268 if(m_ccs_epochs.contains(current_epoch))
269 return std::make_pair(Handshake_Type::HandshakeCCS, std::vector<uint8_t>());
270 }
271 return std::make_pair(Handshake_Type::None, std::vector<uint8_t>());
272 }
273
274 auto i = m_messages.find(m_in_message_seq);
275
276 if(i == m_messages.end() || !i->second.complete())
277 {
278 return std::make_pair(Handshake_Type::None, std::vector<uint8_t>());
279 }
280
281 m_in_message_seq += 1;
282
283 return i->second.message();
284 }

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 174 of file tls_handshake_io.cpp.

175 {
176 return false;
177 }

◆ initial_record_version()

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

Implements Botan::TLS::Handshake_IO.

Definition at line 139 of file tls_handshake_io.cpp.

140 {
141 return Protocol_Version::DTLS_V12;
142 }

◆ send()

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

Implements Botan::TLS::Handshake_IO.

Definition at line 398 of file tls_handshake_io.cpp.

399 {
400 return this->send_under_epoch(msg, m_seqs.current_write_epoch());
401 }
virtual uint16_t current_write_epoch() const =0
std::vector< uint8_t > send_under_epoch(const Handshake_Message &msg, uint16_t epoch) override

References Botan::TLS::Connection_Sequence_Numbers::current_write_epoch(), and 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 404 of file tls_handshake_io.cpp.

405 {
406 const std::vector<uint8_t> msg_bits = msg.serialize();
407 const Handshake_Type msg_type = msg.type();
408
409 if(msg_type == Handshake_Type::HandshakeCCS)
410 {
411 m_send_hs(epoch, Record_Type::ChangeCipherSpec, msg_bits);
412 return std::vector<uint8_t>(); // not included in handshake hashes
413 }
414 else if(msg_type == Handshake_Type::HelloVerifyRequest)
415 {
416 // This message is not included in the handshake hashes
417 send_message(m_out_message_seq, epoch, msg_type, msg_bits);
418 m_out_message_seq += 1;
419 return std::vector<uint8_t>();
420 }
421
422 // Note: not saving CCS, instead we know it was there due to change in epoch
423 m_flights.rbegin()->push_back(m_out_message_seq);
424 m_flight_data[m_out_message_seq] = Message_Info(epoch, msg_type, msg_bits);
425
426 m_out_message_seq += 1;
427 m_last_write = steady_clock_ms();
428 m_next_timeout = m_initial_timeout;
429
430 return send_message(m_out_message_seq - 1, epoch, msg_type, msg_bits);
431 }

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 179 of file tls_handshake_io.cpp.

180 {
181 if(m_last_write == 0 || (m_flights.size() > 1 && !m_flights.rbegin()->empty()))
182 {
183 /*
184 If we haven't written anything yet obviously no timeout.
185 Also no timeout possible if we are mid-flight,
186 */
187 return false;
188 }
189
190 const uint64_t ms_since_write = steady_clock_ms() - m_last_write;
191
192 if(ms_since_write < m_next_timeout)
193 return false;
194
195 retransmit_last_flight();
196
197 m_next_timeout = std::min(2 * m_next_timeout, m_max_timeout);
198 return true;
199 }

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