8#include <botan/internal/tls_handshake_io.h>
10#include <botan/exceptn.h>
11#include <botan/tls_messages.h>
12#include <botan/internal/loadstor.h>
13#include <botan/internal/tls_record.h>
14#include <botan/internal/tls_seq_numbers.h>
21inline size_t load_be24(
const uint8_t q[3]) {
25void store_be24(uint8_t out[3],
size_t val) {
31uint64_t steady_clock_ms() {
32 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch())
39 return Protocol_Version::TLS_V12;
47 m_queue.insert(m_queue.end(), record, record + record_len);
49 if(record_len != 1 || record[0] != 1) {
55 m_queue.insert(m_queue.end(), ccs_hs, ccs_hs +
sizeof(ccs_hs));
57 throw Decoding_Error(
"Unknown message type " + std::to_string(
static_cast<size_t>(record_type)) +
58 " in handshake processing");
63 if(m_queue.size() >= 4) {
64 const size_t length = 4 +
make_uint32(0, m_queue[1], m_queue[2], m_queue[3]);
66 if(m_queue.size() >= length) {
73 std::vector<uint8_t> contents(m_queue.begin() + 4, m_queue.begin() + length);
75 m_queue.erase(m_queue.begin(), m_queue.begin() + length);
77 return std::make_pair(type, contents);
85 std::vector<uint8_t> send_buf(4 + msg.size());
87 const size_t buf_size = msg.size();
89 send_buf[0] =
static_cast<uint8_t
>(type);
91 store_be24(&send_buf[1], buf_size);
94 copy_mem(&send_buf[4], msg.data(), msg.size());
101 throw Invalid_State(
"Not possible to send under arbitrary epoch with stream based TLS");
105 const std::vector<uint8_t> msg_bits = msg.
serialize();
109 return std::vector<uint8_t>();
118 return Protocol_Version::DTLS_V12;
121void Datagram_Handshake_IO::retransmit_last_flight() {
122 const size_t flight_idx = (m_flights.size() == 1) ? 0 : (m_flights.size() - 2);
123 retransmit_flight(flight_idx);
126void Datagram_Handshake_IO::retransmit_flight(
size_t flight_idx) {
127 const std::vector<uint16_t>& flight = m_flights.at(flight_idx);
129 BOTAN_ASSERT(!flight.empty(),
"Nonempty flight to retransmit");
131 uint16_t epoch = m_flight_data[flight[0]].epoch;
133 for(
auto msg_seq : flight) {
134 auto& msg = m_flight_data[msg_seq];
136 if(msg.epoch != epoch) {
138 std::vector<uint8_t> ccs(1, 1);
142 send_message(msg_seq, msg.epoch, msg.msg_type, msg.msg_bits);
152 if(m_last_write == 0 || (m_flights.size() > 1 && !m_flights.rbegin()->empty())) {
160 const uint64_t ms_since_write = steady_clock_ms() - m_last_write;
162 if(ms_since_write < m_next_timeout) {
166 retransmit_last_flight();
168 m_next_timeout = std::min(2 * m_next_timeout, m_max_timeout);
175 uint64_t record_sequence) {
176 const uint16_t epoch =
static_cast<uint16_t
>(record_sequence >> 48);
179 if(record_len != 1 || record[0] != 1) {
184 m_ccs_epochs.insert(epoch);
188 const size_t DTLS_HANDSHAKE_HEADER_LEN = 12;
191 if(record_len < DTLS_HANDSHAKE_HEADER_LEN) {
196 const size_t msg_len = load_be24(&record[1]);
198 const size_t fragment_offset = load_be24(&record[6]);
199 const size_t fragment_length = load_be24(&record[9]);
201 const size_t total_size = DTLS_HANDSHAKE_HEADER_LEN + fragment_length;
203 if(record_len < total_size) {
207 if(message_seq >= m_in_message_seq) {
208 m_messages[message_seq].add_fragment(
209 &record[DTLS_HANDSHAKE_HEADER_LEN], fragment_length, fragment_offset, epoch, msg_type, msg_len);
214 record += total_size;
215 record_len -= total_size;
221 if(!m_flights.rbegin()->empty()) {
222 m_flights.push_back(std::vector<uint16_t>());
226 if(!m_messages.empty()) {
227 const uint16_t current_epoch = m_messages.begin()->second.epoch();
229 if(m_ccs_epochs.contains(current_epoch)) {
236 auto i = m_messages.find(m_in_message_seq);
238 if(i == m_messages.end() || !i->second.complete()) {
242 m_in_message_seq += 1;
244 return i->second.message();
247void Datagram_Handshake_IO::Handshake_Reassembly::add_fragment(
const uint8_t fragment[],
248 size_t fragment_length,
249 size_t fragment_offset,
259 m_msg_type = msg_type;
260 m_msg_length = msg_length;
263 if(msg_type != m_msg_type || msg_length != m_msg_length || epoch != m_epoch) {
264 throw Decoding_Error(
"Inconsistent values in fragmented DTLS handshake header");
267 if(fragment_offset > m_msg_length) {
268 throw Decoding_Error(
"Fragment offset past end of message");
271 if(fragment_offset + fragment_length > m_msg_length) {
272 throw Decoding_Error(
"Fragment overlaps past end of message");
275 if(fragment_offset == 0 && fragment_length == m_msg_length) {
277 m_message.assign(fragment, fragment + fragment_length);
287 for(
size_t i = 0; i != fragment_length; ++i) {
288 m_fragments[fragment_offset + i] = fragment[i];
291 if(m_fragments.size() == m_msg_length) {
292 m_message.resize(m_msg_length);
293 for(
size_t i = 0; i != m_msg_length; ++i) {
294 m_message[i] = m_fragments[i];
301bool Datagram_Handshake_IO::Handshake_Reassembly::complete()
const {
305std::pair<Handshake_Type, std::vector<uint8_t>> Datagram_Handshake_IO::Handshake_Reassembly::message()
const {
307 throw Internal_Error(
"Datagram_Handshake_IO - message not complete");
310 return std::make_pair(m_msg_type, m_message);
313std::vector<uint8_t> Datagram_Handshake_IO::format_fragment(
const uint8_t fragment[],
315 uint16_t frag_offset,
318 uint16_t msg_sequence)
const {
319 std::vector<uint8_t> send_buf(12 + frag_len);
321 send_buf[0] =
static_cast<uint8_t
>(type);
323 store_be24(&send_buf[1], msg_len);
325 store_be(msg_sequence, &send_buf[4]);
327 store_be24(&send_buf[6], frag_offset);
328 store_be24(&send_buf[9], frag_len);
331 copy_mem(&send_buf[12], fragment, frag_len);
337std::vector<uint8_t> Datagram_Handshake_IO::format_w_seq(
const std::vector<uint8_t>& msg,
339 uint16_t msg_sequence)
const {
340 return format_fragment(msg.data(), msg.size(), 0,
static_cast<uint16_t
>(msg.size()), type, msg_sequence);
344 return format_w_seq(msg, type, m_in_message_seq - 1);
352 const std::vector<uint8_t> msg_bits = msg.
serialize();
357 return std::vector<uint8_t>();
360 send_message(m_out_message_seq, epoch, msg_type, msg_bits);
361 m_out_message_seq += 1;
362 return std::vector<uint8_t>();
366 m_flights.rbegin()->push_back(m_out_message_seq);
367 m_flight_data[m_out_message_seq] = Message_Info(epoch, msg_type, msg_bits);
369 m_out_message_seq += 1;
370 m_last_write = steady_clock_ms();
371 m_next_timeout = m_initial_timeout;
373 return send_message(m_out_message_seq - 1, epoch, msg_type, msg_bits);
376std::vector<uint8_t> Datagram_Handshake_IO::send_message(uint16_t msg_seq,
379 const std::vector<uint8_t>& msg_bits) {
380 const size_t DTLS_HANDSHAKE_HEADER_LEN = 12;
382 auto no_fragment = format_w_seq(msg_bits, msg_type, msg_seq);
387 size_t frag_offset = 0;
396 const size_t ciphersuite_overhead = (epoch > 0) ? 128 : 0;
397 const size_t header_overhead =
DTLS_HEADER_SIZE + DTLS_HANDSHAKE_HEADER_LEN;
399 if(m_mtu <= (header_overhead + ciphersuite_overhead)) {
403 const size_t max_rec_size = m_mtu - (header_overhead + ciphersuite_overhead);
405 while(frag_offset != msg_bits.size()) {
406 const size_t frag_len = std::min<size_t>(msg_bits.size() - frag_offset, max_rec_size);
408 const std::vector<uint8_t> frag = format_fragment(&msg_bits[frag_offset],
410 static_cast<uint16_t
>(frag_offset),
411 static_cast<uint16_t
>(msg_bits.size()),
417 frag_offset += frag_len;
#define BOTAN_ASSERT(expr, assertion_made)
virtual uint16_t current_write_epoch() const =0
std::vector< uint8_t > send_under_epoch(const Handshake_Message &msg, uint16_t epoch) override
bool timeout_check() override
void add_record(const uint8_t record[], size_t record_len, Record_Type type, uint64_t sequence_number) override
std::pair< Handshake_Type, std::vector< uint8_t > > get_next_record(bool expecting_ccs) override
std::vector< uint8_t > format(const std::vector< uint8_t > &handshake_msg, Handshake_Type handshake_type) const override
Protocol_Version initial_record_version() const override
std::vector< uint8_t > send(const Handshake_Message &msg) override
bool have_more_data() const override
virtual Handshake_Type type() const =0
virtual std::vector< uint8_t > serialize() const =0
virtual Handshake_Type wire_type() const
std::vector< uint8_t > send_under_epoch(const Handshake_Message &msg, uint16_t epoch) override
std::vector< uint8_t > format(const std::vector< uint8_t > &handshake_msg, Handshake_Type handshake_type) const override
Protocol_Version initial_record_version() const override
std::pair< Handshake_Type, std::vector< uint8_t > > get_next_record(bool expecting_ccs) override
std::vector< uint8_t > send(const Handshake_Message &msg) override
void add_record(const uint8_t record[], size_t record_len, Record_Type type, uint64_t sequence_number) override
constexpr uint8_t get_byte(T input)
constexpr uint32_t make_uint32(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr auto store_be(ParamTs &&... params)
constexpr auto load_be(ParamTs &&... params)