Botan  2.11.0
Crypto and TLS for C++11
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, uint8_t, 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
 
Protocol_Version initial_record_version () const override
 
std::vector< uint8_t > send (const Handshake_Message &msg) override
 
bool timeout_check () override
 

Detailed Description

Handshake IO for datagram-based handshakes

Definition at line 97 of file tls_handshake_io.h.

Member Typedef Documentation

◆ writer_fn

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

Definition at line 100 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 102 of file tls_handshake_io.h.

104  :
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  {}

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

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

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

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

References type.

378  {
379  return format_w_seq(msg, type, m_in_message_seq - 1);
380  }
MechanismType type

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

References Botan::TLS::HANDSHAKE_CCS, and Botan::TLS::HANDSHAKE_NONE.

244  {
245  // Expecting a message means the last flight is concluded
246  if(!m_flights.rbegin()->empty())
247  m_flights.push_back(std::vector<uint16_t>());
248 
249  if(expecting_ccs)
250  {
251  if(!m_messages.empty())
252  {
253  const uint16_t current_epoch = m_messages.begin()->second.epoch();
254 
255  if(m_ccs_epochs.count(current_epoch))
256  return std::make_pair(HANDSHAKE_CCS, std::vector<uint8_t>());
257  }
258  return std::make_pair(HANDSHAKE_NONE, std::vector<uint8_t>());
259  }
260 
261  auto i = m_messages.find(m_in_message_seq);
262 
263  if(i == m_messages.end() || !i->second.complete())
264  return std::make_pair(HANDSHAKE_NONE, std::vector<uint8_t>());
265 
266  m_in_message_seq += 1;
267 
268  return i->second.message();
269  }

◆ initial_record_version()

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

◆ send()

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

Implements Botan::TLS::Handshake_IO.

Definition at line 383 of file tls_handshake_io.cpp.

References Botan::TLS::CHANGE_CIPHER_SPEC, Botan::TLS::Connection_Sequence_Numbers::current_write_epoch(), Botan::TLS::HANDSHAKE_CCS, Botan::TLS::HELLO_VERIFY_REQUEST, Botan::TLS::Handshake_Message::serialize(), and Botan::TLS::Handshake_Message::type().

384  {
385  const std::vector<uint8_t> msg_bits = msg.serialize();
386  const uint16_t epoch = m_seqs.current_write_epoch();
387  const Handshake_Type msg_type = msg.type();
388 
389  if(msg_type == HANDSHAKE_CCS)
390  {
391  m_send_hs(epoch, CHANGE_CIPHER_SPEC, msg_bits);
392  return std::vector<uint8_t>(); // not included in handshake hashes
393  }
394  else if(msg_type == HELLO_VERIFY_REQUEST)
395  {
396  // This message is not included in the handshake hashes
397  send_message(m_out_message_seq, epoch, msg_type, msg_bits);
398  m_out_message_seq += 1;
399  return std::vector<uint8_t>();
400  }
401 
402  // Note: not saving CCS, instead we know it was there due to change in epoch
403  m_flights.rbegin()->push_back(m_out_message_seq);
404  m_flight_data[m_out_message_seq] = Message_Info(epoch, msg_type, msg_bits);
405 
406  m_out_message_seq += 1;
407  m_last_write = steady_clock_ms();
408  m_next_timeout = m_initial_timeout;
409 
410  return send_message(m_out_message_seq - 1, epoch, msg_type, msg_bits);
411  }
virtual uint16_t current_write_epoch() const =0

◆ timeout_check()

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

Implements Botan::TLS::Handshake_IO.

Definition at line 166 of file tls_handshake_io.cpp.

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

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