Botan 3.14.1
Crypto and TLS for C&
tls_handshake_io.cpp
Go to the documentation of this file.
1/*
2* TLS Handshake IO
3* (C) 2012,2014,2015 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/tls_handshake_io.h>
9
10#include <botan/assert.h>
11#include <botan/exceptn.h>
12#include <botan/tls_exceptn.h>
13#include <botan/tls_handshake_msg.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/loadstor.h>
16#include <botan/internal/tls_record.h>
17#include <botan/internal/tls_seq_numbers.h>
18
19namespace Botan::TLS {
20
21namespace {
22
23constexpr size_t DTLS_HANDSHAKE_HEADER_SIZE = 12;
24
25// Bound on peer-cued flight replays when the policy leaves the timer's own retransmission
26// count unlimited. Matches the default value of Policy::dtls_maximum_retransmissions.
27constexpr size_t DEFAULT_PEER_REPLAY_BUDGET = 12;
28
29// How many ClientHellos a server will take before it commits to a handshake by
30// sending a flight. The exchange RFC 6347 4.2.1 describes needs two; the rest
31// absorb a cookie secret rotation. Each one advances the expected sequence, so
32// this is also what keeps that counter from being driven around by
33// unauthenticated input.
34constexpr uint16_t MAX_PRE_COMMIT_CLIENT_HELLOS = 8;
35
36// client_version(2) + random(32) + session_id length(1) + cookie length(1)
37// + cipher_suites length(2) + one suite(2) + compression_methods
38// length(1) + one method(1). RFC 6347 4.2.1 gives the DTLS ClientHello
39// layout. A shorter declared length cannot be a real ClientHello.
40constexpr size_t MIN_CLIENT_HELLO_SIZE = 42;
41
42// HelloRequest and ServerHelloDone are the only DTLS handshake messages with a
43// zero-length encoding; every other type carries a mandatory body. A record
44// declaring msg_len 0 for one of those other types is not a real message: a
45// bare 12-byte header would otherwise reassemble into a "complete" message and
46// be delivered to the state machine as the wrong thing, or park in a slot the
47// genuine message can then no longer reach.
48constexpr bool dtls_handshake_type_requires_body(Handshake_Type type) {
50}
51
52inline size_t load_be24(const uint8_t q[3]) {
53 return make_uint32(0, q[0], q[1], q[2]);
54}
55
56// Reject handshake type values that are internal sentinels, not wire values
57void verify_is_expected_wire_handshake_type(Handshake_Type type) {
58 switch(type) {
63 throw TLS_Exception(Alert::UnexpectedMessage, "Invalid handshake message type");
64 default:
65 break;
66 }
67}
68
69void store_be24(uint8_t out[3], size_t val) {
70 if(val > 0xFFFFFF) {
71 throw Encoding_Error("Handshake message is too large to encode");
72 }
73 out[0] = get_byte<1>(static_cast<uint32_t>(val));
74 out[1] = get_byte<2>(static_cast<uint32_t>(val));
75 out[2] = get_byte<3>(static_cast<uint32_t>(val));
76}
77
78} // namespace
79
81 return Protocol_Version::TLS_V12;
82}
83
84void Stream_Handshake_IO::add_record(const uint8_t record[],
85 size_t record_len,
86 Record_Type record_type,
87 uint64_t /*sequence_number*/) {
88 if(record_type == Record_Type::Handshake) {
89 m_queue.insert(m_queue.end(), record, record + record_len);
90 } else if(record_type == Record_Type::ChangeCipherSpec) {
91 if(record_len != 1 || record[0] != 1) {
92 throw Decoding_Error("Invalid ChangeCipherSpec");
93 }
94
95 // Handshake messages are processed as soon as they are complete, so
96 // anything still queued is a partial message
97 if(m_ccs_received || !m_queue.empty()) {
98 throw TLS_Exception(Alert::UnexpectedMessage, "Unexpected ChangeCipherSpec");
99 }
100
101 m_ccs_received = true;
102 } else {
103 throw Decoding_Error("Unknown message type " + std::to_string(static_cast<size_t>(record_type)) +
104 " in handshake processing");
105 }
106}
107
108std::pair<Handshake_Type, std::vector<uint8_t>> Stream_Handshake_IO::get_next_record(bool expecting_ccs,
109 size_t max_message_size) {
110 if(m_ccs_received) {
111 if(!expecting_ccs) {
112 throw TLS_Exception(Alert::UnexpectedMessage, "Unexpected ChangeCipherSpec");
113 }
114 m_ccs_received = false;
115 return std::make_pair(Handshake_Type::HandshakeCCS, std::vector<uint8_t>());
116 }
117
118 if(m_queue.size() >= 4) {
119 const Handshake_Type type = static_cast<Handshake_Type>(m_queue[0]);
120
121 const size_t rec_length = make_uint32(0, m_queue[1], m_queue[2], m_queue[3]);
122
123 // If we are expecting a CCS but the next queued message is not a CCS,
124 // the peer has skipped the CCS message. This can happen when the peer
125 // sends an encrypted Finished without the preceding CCS, in which case
126 // the encrypted bytes are misinterpreted as a handshake message.
127 if(expecting_ccs) {
128 throw TLS_Exception(Alert::UnexpectedMessage, "Expected ChangeCipherSpec but got a handshake message");
129 }
130
131 verify_is_expected_wire_handshake_type(type);
132
133 if(max_message_size > 0 && rec_length > max_message_size) {
134 throw TLS_Exception(
135 Alert::HandshakeFailure,
136 Botan::fmt("Handshake message is {} bytes, policy maximum is {}", rec_length, max_message_size));
137 }
138
139 const size_t length = 4 + rec_length;
140
141 if(m_queue.size() >= length) {
142 const std::vector<uint8_t> contents(m_queue.begin() + 4, m_queue.begin() + length);
143
144 m_queue.erase(m_queue.begin(), m_queue.begin() + length);
145
146 return std::make_pair(type, contents);
147 }
148 }
149
150 return std::make_pair(Handshake_Type::None, std::vector<uint8_t>());
151}
152
153std::vector<uint8_t> Stream_Handshake_IO::format(const std::vector<uint8_t>& msg, Handshake_Type type) const {
154 std::vector<uint8_t> send_buf(4 + msg.size());
155
156 const size_t buf_size = msg.size();
157
158 send_buf[0] = static_cast<uint8_t>(type);
159
160 store_be24(&send_buf[1], buf_size);
161
162 if(!msg.empty()) {
163 copy_mem(&send_buf[4], msg.data(), msg.size());
164 }
165
166 return send_buf;
167}
168
169std::vector<uint8_t> Stream_Handshake_IO::send_under_epoch(const Handshake_Message& /*msg*/, uint16_t /*epoch*/) {
170 throw Invalid_State("Not possible to send under arbitrary epoch with stream based TLS");
171}
172
173std::vector<uint8_t> Stream_Handshake_IO::send(const Handshake_Message& msg) {
174 const std::vector<uint8_t> msg_bits = msg.serialize();
175
177 m_send_hs(Record_Type::ChangeCipherSpec, msg_bits);
178 return std::vector<uint8_t>(); // not included in handshake hashes
179 }
180
181 auto buf = format(msg_bits, msg.wire_type());
182 m_send_hs(Record_Type::Handshake, buf);
183 return buf;
184}
185
186#if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
187
188std::vector<uint8_t> Stream_Handshake_IO::start_with_client_hello_from_downgrade(
189 const Handshake_Message& client_hello) {
191 "Expected ClientHello message for TLS downgrade");
192
193 // In TLS we don't have to update any internal state, we just need to
194 // format the Client Hello message for absorption into the handshake hash.
195 return format(client_hello.serialize(), client_hello.wire_type());
196}
197
198#endif
199
200namespace {
201
202size_t max_pending_reassembly(size_t policy_hs_max) {
203 /*
204 * Here we set arbitrary but probably more than sufficient bounds on the *overall*
205 * allocation that is allowed across the entire handshake.
206 *
207 * If the policy set a limit on individual handshake message sizes, accept up to 4
208 * times that for the whole handshake. This is more than generous considering most
209 * handshake messages are fixed length or are relatively tightly bounded. The default
210 * per-handshake message bound is 64 KiB so without application intervention this will
211 * top out at 256 KiB for a handshake.
212 *
213 * If the application explicitly disables the per handshake message bound, still apply
214 * an arbitrary upper bound of 16 MiB for the handshake.
215 */
216 constexpr size_t overall_cap = 16 * 1024 * 1024;
217
218 if(policy_hs_max == 0 || policy_hs_max >= overall_cap / 4) {
219 // If disabled or huge just take our max
220 return overall_cap;
221 } else {
222 // Otherwise 4x the per-message max
223 return policy_hs_max * 4;
224 }
225}
226
227} // namespace
228
230 steady_clock_fn steady_clock_ms,
232 uint16_t mtu,
233 uint64_t initial_timeout_ms,
234 uint64_t max_timeout_ms,
235 std::optional<size_t> max_retransmissions,
236 size_t max_handshake_msg_size,
237 bool is_server,
238 uint16_t initial_epoch) :
239 m_seqs(seq),
240 m_flights(1),
241 m_flight_ccs(1),
242 m_initial_timeout(initial_timeout_ms),
243 m_max_timeout(max_timeout_ms),
244 m_max_retransmissions(max_retransmissions),
245 m_initial_epoch(initial_epoch),
246 m_last_delivered_epoch(initial_epoch),
247 m_send_hs(std::move(writer)),
248 m_steady_clock_ms(std::move(steady_clock_ms)),
249 m_mtu(mtu),
250 m_max_handshake_msg_size(max_handshake_msg_size),
251 m_max_pending_reassembly(max_pending_reassembly(m_max_handshake_msg_size)),
252 m_is_server(is_server) {}
253
255 return Protocol_Version::DTLS_V12;
256}
257
258std::optional<size_t> Datagram_Handshake_IO::last_completed_flight_index() const {
259 BOTAN_DEBUG_ASSERT(!m_flights.empty());
260
261 // A non-empty current slot is the flight just compiled (the first ClientHello
262 // lives in m_flights[0] the same way). An empty trailing slot is the WAITING
263 // placeholder get_next_record() adds; retransmit the flight before it.
264 if(!m_flights.rbegin()->empty()) {
265 return m_flights.size() - 1;
266 }
267
268 if(m_flights.size() == 1) {
269 return std::nullopt;
270 }
271
272 const size_t flight_idx = m_flights.size() - 2;
273
274 // A peer can ask us to replay a flight before we have sent one, eg
275 // with a ClientHello whose message_seq is past the reassembly
276 // window. In that case there is nothing to retransmit.
277 if(m_flights[flight_idx].empty()) {
278 return std::nullopt;
279 }
280
281 return flight_idx;
282}
283
284void Datagram_Handshake_IO::retransmit_last_flight() {
285 if(const auto flight_idx = last_completed_flight_index()) {
286 retransmit_flight(*flight_idx);
287 m_last_write = m_steady_clock_ms();
288 }
289}
290
291/*
292* RFC 6347 4.2.4 gives the WAITING state a "read retransmit" transition that
293* replays our flight when the peer retransmits theirs.
294*
295* The cue for it is unauthenticated epoch-zero data, so this deliberately is not
296* retransmit_last_flight(). Re-anchoring m_last_write the way that does means a
297* peer cueing faster than the timeout keeps next_retransmission_timeout() from
298* ever expiring, so m_retransmit_count never advances and the handshake never
299* gives up; and unbounded, each cue draws a whole flight toward whatever address
300* the cue claims to come from.
301*
302* Spending the timer's own budget, reset by the same forward progress, caps a
303* continuously cueing peer at doubling the flight traffic the handshake would
304* emit anyway. Counting rather than rate limiting answers a legitimate peer's
305* retransmit promptly, which BoGo's DTLS-Retransmit-Client-Basic requires.
306*/
307void Datagram_Handshake_IO::replay_last_flight_for_peer() {
308 // An unset m_max_retransmissions means "retransmit on my own timer forever",
309 // which is a different proposition from "replay whenever an unauthenticated
310 // packet asks me to". This path is never unbounded, whatever the policy.
311 const size_t bound = m_max_retransmissions.value_or(DEFAULT_PEER_REPLAY_BUDGET);
312
313 if(m_peer_replay_count >= bound) {
314 return;
315 }
316
317 if(const auto flight_idx = last_completed_flight_index()) {
318 m_peer_replay_count += 1;
319 retransmit_flight(*flight_idx);
320 }
321}
322
323void Datagram_Handshake_IO::retransmit_flight(size_t flight_idx) {
324 const auto& flight = m_flights.at(flight_idx);
325 const auto& ccs_records = m_flight_ccs.at(flight_idx);
326 const std::vector<uint8_t> ccs = {1};
327
328 BOTAN_ASSERT(!flight.empty(), "Nonempty flight to retransmit");
329
330 size_t ccs_idx = 0;
331 for(size_t msg_idx = 0; msg_idx != flight.size(); ++msg_idx) {
332 while(ccs_idx != ccs_records.size() && ccs_records[ccs_idx].first == msg_idx) {
333 m_send_hs(ccs_records[ccs_idx].second, Record_Type::ChangeCipherSpec, ccs);
334 ++ccs_idx;
335 }
336
337 const auto msg_seq = flight[msg_idx];
338 const auto& msg = m_flight_data.at(msg_seq);
339 send_message(msg_seq, msg.epoch, msg.msg_type, msg.msg_bits);
340 }
341
342 while(ccs_idx != ccs_records.size() && ccs_records[ccs_idx].first == flight.size()) {
343 m_send_hs(ccs_records[ccs_idx].second, Record_Type::ChangeCipherSpec, ccs);
344 ++ccs_idx;
345 }
346}
347
349 // Future or incomplete fragments remain buffered, but only a complete
350 // next-in-sequence message is trailing handshake data. A buffered duplicate
351 // ClientHello is not: it carries nothing the peer has not already sent, and
352 // get_next_record only reaches for it when there is no in-sequence message.
353 const auto next = m_messages.find(m_in_message_seq);
354 return next != m_messages.end() && next->second.complete();
355}
356
357void Datagram_Handshake_IO::finalize_handshake(bool retransmit_terminal_flight) {
358 // Keep an empty trailing flight to mean "we are waiting for the peer".
359 // Retransmission then replays the previous, completed flight instead of
360 // appending to it.
361 if(!m_flights.rbegin()->empty()) {
362 m_flights.emplace_back();
363 m_flight_ccs.emplace_back();
364 }
365
366 // RFC 6347 4.2.4: "Once the messages have been sent, the implementation
367 // then enters the FINISHED state if this is the last flight in the
368 // handshake." Keep the flight for reactive replay when the peer
369 // retransmits, but do not arm a proactive retransmission timer.
370 m_finished = true;
371 m_retransmit_terminal_flight = retransmit_terminal_flight;
372
373 // Messages of any later handshake are reassembled by a new Handshake_IO
374 discard_pending_messages();
375}
376
378 const auto timeout = next_retransmission_timeout();
379 if(!timeout || timeout->count() > 0) {
380 return false;
381 }
382
383 // The retransmit timer has expired. Count this attempt and, once the
384 // configured cap is reached, abandon the handshake rather than retransmit
385 // forever. RFC 6347 4.2.4.1 gives the backoff schedule but states no
386 // condition for giving up, so the cap is local policy, not a requirement.
387 // No alert is sent - the peer is by definition unresponsive.
388 m_retransmit_count += 1;
389 if(m_max_retransmissions.has_value() && m_retransmit_count > m_max_retransmissions.value()) {
390 throw TLS_Exception(Alert::None, "DTLS handshake timed out: maximum retransmissions exceeded");
391 }
392
393 // retransmit_last_flight re-anchors m_last_write. Without that, once
394 // m_next_timeout saturates at m_max_timeout the elapsed time keeps growing
395 // and every subsequent poll fires another retransmission.
396 retransmit_last_flight();
397
398 m_next_timeout = std::min(2 * m_next_timeout, m_max_timeout);
399 return true;
400}
401
402std::optional<std::chrono::milliseconds> Datagram_Handshake_IO::next_retransmission_timeout() const {
403 if(m_finished) {
404 return std::nullopt;
405 }
406
407 // Without an outgoing flight there is nothing timeout_check() could retransmit.
408 if(!m_last_write.has_value() || !last_completed_flight_index().has_value()) {
409 return std::nullopt;
410 }
411
412 const uint64_t ms_since_write = m_steady_clock_ms() - m_last_write.value();
413 if(ms_since_write >= m_next_timeout) {
414 return std::chrono::milliseconds(0);
415 }
416
417 // BoringSSL reports a sub-15ms remainder as zero (ssl/d1_lib.cc
418 // DTLSTimer::MicrosecondsRemaining) to absorb divergence with caller
419 // scheduling; BoGo's DTLS-Retransmit-Fudge test requires it. The cap keeps
420 // the fudge from swallowing the very short timers some tests configure.
421 const uint64_t fudge_ms = std::min<uint64_t>(15, m_initial_timeout / 2);
422 const uint64_t remaining_ms = m_next_timeout - ms_since_write;
423
424 return std::chrono::milliseconds(remaining_ms <= fudge_ms ? 0 : remaining_ms);
425}
426
427void Datagram_Handshake_IO::add_record(const uint8_t record[],
428 size_t record_len,
429 Record_Type record_type,
430 uint64_t record_sequence) {
431 add_record(record, record_len, record_type, record_sequence, false);
432}
433
435 size_t record_len,
436 Record_Type record_type,
437 uint64_t record_sequence) {
438 add_record(record, record_len, record_type, record_sequence, true);
439}
440
441bool Datagram_Handshake_IO::reassemble_retransmitted_fragment(const uint8_t fragment[],
442 size_t fragment_length,
443 size_t fragment_offset,
444 uint16_t epoch,
445 Handshake_Type msg_type,
446 size_t msg_length,
447 uint16_t message_seq) {
448 auto [i, inserted] = m_retransmitted_messages.try_emplace(msg_type, message_seq, Handshake_Reassembly{});
449
450 // A slot here holds unauthenticated epoch-zero data until a genuine
451 // retransmission arrives. A partial fragment buffered at a lower epoch than
452 // this one has been superseded: an off-path peer cannot forge the higher
453 // epoch, so start over from it. Without this the header mismatch would throw
454 // in add_fragment, and that throw -- keyed to the authenticated, higher-epoch
455 // record that triggered it rather than to the forged data it collided with --
456 // is not absorbed and tears the established association down.
457 const bool superseded_by_higher_epoch =
458 i->second.second.initialized() && !i->second.second.complete() && epoch > i->second.second.epoch();
459
460 if(!inserted && (i->second.first != message_seq || superseded_by_higher_epoch)) {
461 release_reassembly_bytes(i->second.second);
462 i->second = std::make_pair(message_seq, Handshake_Reassembly());
463 }
464
465 auto& reassembly = i->second.second;
466
467 // These buffers hold unauthenticated input, so they are charged against the
468 // same budget as the main reassembly path rather than growing on their own.
469 if(!charged_add_fragment(reassembly,
470 m_max_pending_reassembly,
471 fragment,
472 fragment_length,
473 fragment_offset,
474 epoch,
475 msg_type,
476 msg_length)) {
477 return false;
478 }
479
480 if(!reassembly.complete()) {
481 return false;
482 }
483
484 release_reassembly_bytes(reassembly);
485 m_retransmitted_messages.erase(i);
486 return true;
487}
488
489bool Datagram_Handshake_IO::charged_add_fragment(Handshake_Reassembly& reassembly,
490 size_t ceiling,
491 const uint8_t fragment[],
492 size_t fragment_length,
493 size_t fragment_offset,
494 uint16_t epoch,
495 Handshake_Type msg_type,
496 size_t msg_length) {
497 // A ClientHello may replace unauthenticated data buffered in its slot (see
498 // Handshake_Reassembly::add_fragment). Where its header differs from what
499 // the slot holds it is a new message: release the slot and admit the
500 // ClientHello like any first fragment, so the budget check comes before
501 // the allocation.
502 if(reassembly.superseded_by_client_hello(epoch, msg_type) &&
503 (msg_type != reassembly.msg_type() || msg_length != reassembly.msg_length())) {
504 release_reassembly_bytes(reassembly);
505 reassembly = Handshake_Reassembly();
506 }
507
508 // We allocate the entire block on the first fragment, so charge it against
509 // the bound at that point. Later fragments must agree with the declared
510 // length, so admission is decided once per slot and retransmissions are
511 // never re-charged.
512 if(!reassembly.initialized()) {
513 if(m_pending_reassembly_bytes + msg_length > ceiling) {
514 return false;
515 }
516 m_pending_reassembly_bytes += msg_length;
517 }
518
519 reassembly.add_fragment(fragment, fragment_length, fragment_offset, epoch, msg_type, msg_length);
520 return true;
521}
522
523void Datagram_Handshake_IO::release_reassembly_bytes(const Handshake_Reassembly& reassembly) {
524 BOTAN_ASSERT_NOMSG(m_pending_reassembly_bytes >= reassembly.msg_length());
525 m_pending_reassembly_bytes -= reassembly.msg_length();
526}
527
528bool Datagram_Handshake_IO::process_previous_handshake_fragment(const uint8_t fragment[],
529 size_t fragment_length,
530 size_t fragment_offset,
531 uint16_t epoch,
532 Handshake_Type msg_type,
533 size_t msg_length,
534 uint16_t message_seq,
535 bool retransmitted_flight) {
536 // Empty fragments of non-empty messages add no information and must not
537 // trigger a flight retransmission. A zero-length message such as
538 // ServerHelloDone is not an empty fragment in this sense.
539 if(fragment_length == 0 && msg_length != 0) {
540 return false;
541 }
542
543 // A record declaring a zero-length body for a type that must carry one is not
544 // a real message, so it cannot be a retransmission of one. Without this a
545 // bare 12-byte header claiming msg_length 0 counts as a fully reassembled
546 // message and buys a whole flight replay.
547 if(msg_length == 0 && dtls_handshake_type_requires_body(msg_type)) {
548 return false;
549 }
550
551 // HelloVerifyRequest has no retransmission timer or cached flight. Feed a
552 // retransmitted initial ClientHello back to the server handshake logic so
553 // it can recreate the stateless cookie response instead.
554 if(msg_type == Handshake_Type::ClientHello) {
555 // A declared length this short cannot be a real ClientHello, so it cannot
556 // be a retransmission of one.
557 if(msg_length < MIN_CLIENT_HELLO_SIZE) {
558 return false;
559 }
560
561 if(!retransmitted_flight && m_awaiting_cookie_client_hello) {
562 if(!m_retransmitted_client_hello.has_value() || m_retransmitted_client_hello->first != message_seq) {
563 if(m_retransmitted_client_hello.has_value()) {
564 release_reassembly_bytes(m_retransmitted_client_hello->second);
565 }
566 m_retransmitted_client_hello = std::make_pair(message_seq, Handshake_Reassembly());
567 }
568
569 // Like any message other than the expected one, this may only use
570 // half of the budget so that the expected ClientHello always fits
571 charged_add_fragment(m_retransmitted_client_hello->second,
572 m_max_pending_reassembly / 2,
573 fragment,
574 fragment_length,
575 fragment_offset,
576 epoch,
577 msg_type,
578 msg_length);
579 return false;
580 }
581
582 // RFC 6347 4.2.4 makes the terminal-flight sender "respond to a retransmit
583 // of the peer's last flight with a retransmit of the last flight". Once our
584 // handshake has completed, the peer's last flight is the one ending in its
585 // Finished, never a ClientHello.
586 if(m_finished) {
587 return false;
588 }
589
590 return reassemble_retransmitted_fragment(
591 fragment, fragment_length, fragment_offset, epoch, msg_type, msg_length, message_seq);
592 }
593
594 // Only an association that has already completed its handshake answers a
595 // retransmitted peer flight here.
596 //
597 // RFC 6347 4.2.4 also gives the WAITING state a "read retransmit" exit that
598 // replays the outgoing flight immediately. Applying that while the handshake
599 // is still pending misfires under fragment reordering: a late duplicate
600 // fragment of an already-consumed message is indistinguishable from a
601 // genuine retransmission, so the peer sees a flight replay it never asked
602 // for (BoGo ReorderHandshakeFragments-Large). Recovery is left to the local
603 // retransmission timer, which costs latency but cannot desynchronise a peer
604 // that was not retransmitting. A retransmitted ClientHello is handled above,
605 // because the stateless cookie response has no timer of its own.
606 if(!retransmitted_flight) {
607 return false;
608 }
609
610 // A genuine Finished follows a ChangeCipherSpec, so it is authenticated
611 // under a non-zero epoch. An unauthenticated epoch-zero record claiming to
612 // be one is spoofable off-path, and must not clear a ChangeCipherSpec we
613 // have legitimately saved to pair against the real Finished.
614 if(msg_type == Handshake_Type::Finished && epoch > 0 &&
615 reassemble_retransmitted_fragment(
616 fragment, fragment_length, fragment_offset, epoch, msg_type, msg_length, message_seq)) {
617 // A final-flight retransmission includes CCS, but UDP may deliver its
618 // records in either order. Wait until both have been observed.
619 if(m_retransmitted_ccs_epoch == epoch) {
620 m_retransmitted_ccs_epoch.reset();
621 return true;
622 }
623
624 // Only the matching pair means anything, so a half seen for some other
625 // epoch is stale. Leaving it set would keep this armed indefinitely for an
626 // epoch an attacker chose, since epoch-zero CCS records are unauthenticated.
627 m_retransmitted_ccs_epoch.reset();
628 m_retransmitted_finished_epoch = epoch;
629 }
630
631 return false;
632}
633
634void Datagram_Handshake_IO::add_record(const uint8_t record[],
635 size_t record_len,
636 Record_Type record_type,
637 uint64_t record_sequence,
638 bool retransmitted_flight) {
639 const uint16_t epoch = static_cast<uint16_t>(record_sequence >> 48);
640
641 // A record under a non-zero epoch authenticated at the record layer, so the
642 // peer is live and is who it claims to be. Restore the peer-cued replay
643 // budget: RFC 6347 4.2.4 requires the terminal-flight sender to answer a
644 // retransmit of the peer's last flight for as long as the association lasts,
645 // and the retained IO never sends a new flight to reset the count otherwise.
646 //
647 // Only once our handshake has finished, though. While it is still pending,
648 // forward progress already resets the count in send_under_epoch, so
649 // restoring it here as well would let a peer alternate a cheap out-of-window
650 // protected record with an epoch-zero cue to draw the pending flight without
651 // bound.
652 if(epoch > 0 && m_finished) {
653 m_peer_replay_count = 0;
654 }
655
656 if(record_type == Record_Type::ChangeCipherSpec) {
657 if(record_len != 1 || record[0] != 1) {
658 throw Decoding_Error("Invalid ChangeCipherSpec");
659 }
660
661 // TODO: check this is otherwise empty
662 m_ccs_epochs.insert(epoch);
663 if(retransmitted_flight) {
664 // Retransmitted final flights cross the epoch boundary: CCS is sent
665 // under the previous epoch and Finished under the newly activated one.
666 // Keep both observations because their datagrams may arrive reordered.
667 const uint16_t finished_epoch = static_cast<uint16_t>(epoch + 1);
668 if(m_retransmitted_finished_epoch == finished_epoch) {
669 m_retransmitted_finished_epoch.reset();
670 if(m_retransmit_terminal_flight) {
671 replay_last_flight_for_peer();
672 }
673 } else {
674 m_retransmitted_finished_epoch.reset();
675 m_retransmitted_ccs_epoch = finished_epoch;
676 }
677 }
678 return;
679 }
680
681 bool retransmit_response = false;
682
683 while(record_len > 0) {
684 if(record_len < DTLS_HANDSHAKE_HEADER_SIZE) {
685 return; // completely bogus? at least degenerate/weird
686 }
687
688 const Handshake_Type msg_type = static_cast<Handshake_Type>(record[0]);
689
690 verify_is_expected_wire_handshake_type(msg_type);
691
692 const size_t msg_len = load_be24(&record[1]);
693
694 if(m_max_handshake_msg_size > 0 && msg_len > m_max_handshake_msg_size) {
695 throw TLS_Exception(
696 Alert::HandshakeFailure,
697 Botan::fmt("Handshake message is {} bytes, policy maximum is {}", msg_len, m_max_handshake_msg_size));
698 }
699
700 const uint16_t message_seq = load_be<uint16_t>(&record[4], 0);
701 const size_t fragment_offset = load_be24(&record[6]);
702 const size_t fragment_length = load_be24(&record[9]);
703
704 const size_t total_size = DTLS_HANDSHAKE_HEADER_SIZE + fragment_length;
705
706 if(record_len < total_size) {
707 throw Decoding_Error("Bad lengths in DTLS header");
708 }
709
710 // Bound the out-of-order reassembly window.
711 constexpr uint16_t reassembly_window = 16;
712
713 if(message_seq >= m_in_message_seq && (message_seq - m_in_message_seq) < reassembly_window) {
714 // A wrapped counter would alias new messages onto long-delivered
715 // sequence numbers. No legitimate handshake gets here, so go quiet.
716 if(m_in_message_seq_wrapped) {
717 record += total_size;
718 record_len -= total_size;
719 continue;
720 }
721
722 // Epochs never decrease within a handshake, so a fragment carrying an
723 // older epoch belongs to an earlier handshake whose records were
724 // delayed into this one. Renegotiation restarts message_seq at zero,
725 // so without this such a record can occupy a slot the current
726 // handshake still needs.
727 if(epoch < m_last_delivered_epoch) {
728 record += total_size;
729 record_len -= total_size;
730 continue;
731 }
732
733 // A bare 12-byte header declaring a zero-length body for a type that
734 // must carry one is not a real message. At a future slot it would
735 // otherwise reassemble as a "complete" message and, never being
736 // replaced, displace the genuine message that later arrives there. Only
737 // future slots are dropped: the same wrong message at the sequence
738 // actually expected is a protocol error and has to be reported as one,
739 // which BoGo's WrongMessageType-ServerHelloDone-DTLS checks.
740 if(msg_len == 0 && message_seq != m_in_message_seq && dtls_handshake_type_requires_body(msg_type)) {
741 record += total_size;
742 record_len -= total_size;
743 continue;
744 }
745
746 /*
747 RFC 6347 4.2.2 keeps message_seq across a retransmission but gives the
748 record a new sequence number, and a rehandshake restarts message_seq at
749 zero. A delayed Finished from the previous handshake therefore arrives
750 with a plausible sequence number and, before this handshake's own
751 ChangeCipherSpec, the same epoch as its pre-CCS records, so neither the
752 sequence nor the epoch floor tells it apart. It cannot be genuine
753 though: a Finished is sent under the epoch its own ChangeCipherSpec
754 installed, which is always above the one this handshake began in.
755
756 Left in, it takes the slot the real Finished needs, and a complete slot
757 is never replaced, so verification fails against the wrong transcript.
758 */
759 if(msg_type == Handshake_Type::Finished && epoch <= m_initial_epoch) {
760 record += total_size;
761 record_len -= total_size;
762 continue;
763 }
764
765 /*
766 RFC 6347 4.2.1 has the cookie exchange exist so that a server need not
767 allocate state before the peer's reachability is proved. Until this
768 endpoint commits to the handshake by sending a flight, the only message
769 worth keeping is the ClientHello it is waiting for.
770
771 Queueing anything else lets one spoofed datagram decide the handshake.
772 A complete message parked in a future slot survives into the real
773 handshake, where have_more_data() reads it as trailing handshake data
774 and the server answers the legitimate client with a fatal alert. RFC
775 6347 4.2.2 permits queueing a future message but equally permits
776 discarding it, and before a cookie there is nothing to weigh against
777 the cost. Only future slots are dropped, and regardless of type: a
778 future ClientHello is no more trustworthy than any other future
779 message, and one parked ahead of the expected sequence produces the
780 same trailing-data teardown. A wrong message type at the sequence
781 actually expected is left alone: that is a protocol error and has to be
782 reported as one, which BoGo's WrongMessageType-ClientHello-DTLS checks.
783
784 The count bounds it in the other direction: each accepted ClientHello
785 advances the expected sequence and draws another HelloVerifyRequest, so
786 without a limit that counter can be walked all the way around.
787 */
788 if(server_awaiting_first_flight() &&
789 (message_seq != m_in_message_seq || m_in_message_seq >= MAX_PRE_COMMIT_CLIENT_HELLOS)) {
790 record += total_size;
791 record_len -= total_size;
792 continue;
793 }
794
795 if(retransmitted_flight) {
796 if(fragment_length == 0) {
797 record += total_size;
798 record_len -= total_size;
799 continue;
800 }
801
802 throw TLS_Exception(Alert::UnexpectedMessage, "Unexpected new DTLS handshake message");
803 }
804
805 // An empty fragment for a non-empty message is garbage; drop it
806 // before it can create and charge a reassembly slot.
807 if(fragment_length == 0 && msg_len > 0) {
808 record += total_size;
809 record_len -= total_size;
810 continue;
811 }
812
813 // Reserve headroom for the message actually being waited on. Otherwise
814 // fragments for the fifteen slots beyond it can consume the whole
815 // budget, after which every fragment of the expected message is
816 // silently dropped and the handshake cannot proceed.
817 const size_t ceiling =
818 (message_seq == m_in_message_seq) ? m_max_pending_reassembly : m_max_pending_reassembly / 2;
819
820 auto [it, inserted] = m_messages.try_emplace(message_seq);
821
822 const bool accepted = charged_add_fragment(it->second,
823 ceiling,
824 &record[DTLS_HANDSHAKE_HEADER_SIZE],
825 fragment_length,
826 fragment_offset,
827 epoch,
828 msg_type,
829 msg_len);
830 if(!accepted && inserted) {
831 m_messages.erase(it);
832 }
833 } else if(message_seq < m_in_message_seq) {
834 retransmit_response |= process_previous_handshake_fragment(&record[DTLS_HANDSHAKE_HEADER_SIZE],
835 fragment_length,
836 fragment_offset,
837 epoch,
838 msg_type,
839 msg_len,
840 message_seq,
841 retransmitted_flight);
842 }
843 // else: beyond the reassembly window is not a retransmission of anything
844 // we have seen, so it must not be able to pull a flight replay out of
845 // us. Drop it silently: the sender has proven nothing at this point.
846
847 record += total_size;
848 record_len -= total_size;
849 }
850
851 if(retransmit_response && (!m_finished || m_retransmit_terminal_flight)) {
852 replay_last_flight_for_peer();
853 }
854}
855
856void Datagram_Handshake_IO::discard_pending_messages() {
857 for(const auto& [seq, reassembly] : m_messages) {
858 release_reassembly_bytes(reassembly);
859 }
860 m_messages.clear();
861
862 if(m_retransmitted_client_hello.has_value()) {
863 release_reassembly_bytes(m_retransmitted_client_hello->second);
864 m_retransmitted_client_hello.reset();
865 }
866}
867
868void Datagram_Handshake_IO::discard_stale_epoch_messages() {
869 for(auto i = m_messages.lower_bound(m_in_message_seq); i != m_messages.end();) {
870 if(i->second.epoch() < m_last_delivered_epoch) {
871 release_reassembly_bytes(i->second);
872 i = m_messages.erase(i);
873 } else {
874 ++i;
875 }
876 }
877}
878
879std::pair<Handshake_Type, std::vector<uint8_t>> Datagram_Handshake_IO::get_next_record(bool expecting_ccs,
880 size_t /*max_message_size*/) {
881 // Expecting a message means the last flight is concluded
882 if(!m_flights.rbegin()->empty()) {
883 m_flights.emplace_back();
884 m_flight_ccs.emplace_back();
885 }
886
887 if(expecting_ccs) {
888 // CCS is expected under the epoch the peer's handshake messages have
889 // been arriving on, and always follows at least one delivered message.
890 if(m_first_delivered_epoch.has_value() && m_ccs_epochs.contains(*m_first_delivered_epoch)) {
891 return std::make_pair(Handshake_Type::HandshakeCCS, std::vector<uint8_t>());
892 }
893 return std::make_pair(Handshake_Type::None, std::vector<uint8_t>());
894 }
895
896 auto i = m_messages.find(m_in_message_seq);
897
898 if(i == m_messages.end() || !i->second.complete()) {
899 // Nothing in sequence to make progress with. A complete ClientHello
900 // buffered from an earlier sequence number is delivered only here, since
901 // delivering it ahead of an in-sequence message makes that message look
902 // like trailing data to the caller's have_more_data() check.
903 if(!m_retransmitted_client_hello.has_value() || !m_retransmitted_client_hello->second.complete()) {
904 return std::make_pair(Handshake_Type::None, std::vector<uint8_t>());
905 }
906
907 /*
908 This ClientHello is either a retransmission of the one the pending
909 HelloVerifyRequest answered or the start of another handshake. Nothing a
910 server has sent so far is retained (RFC 6347 3.2.1 keeps the
911 HelloVerifyRequest stateless), so the exchange simply begins again from
912 it: the sequence numbers are seated at this ClientHello, which also
913 recovers from ClientHellos that never validated having advanced them.
914 Only the slot after it can hold anything of the peer's, namely the
915 cookie-bearing ClientHello it may already be sending; everything else
916 buffered is stale.
917 */
918 const uint16_t seq = m_retransmitted_client_hello->first;
919
920 for(auto s = m_messages.begin(); s != m_messages.end();) {
921 if(s->first != static_cast<uint16_t>(seq + 1)) {
922 release_reassembly_bytes(s->second);
923 s = m_messages.erase(s);
924 } else {
925 ++s;
926 }
927 }
928
929 m_in_message_seq = seq;
930 m_recreating_hello_verify_request = true;
931 i = m_messages.insert_or_assign(seq, std::move(m_retransmitted_client_hello->second)).first;
932 m_retransmitted_client_hello.reset();
933 }
934
935 m_in_message_seq += 1;
936 if(m_in_message_seq == 0) {
937 m_in_message_seq_wrapped = true;
938 }
939
940 if(!m_first_delivered_epoch.has_value()) {
941 m_first_delivered_epoch = i->second.epoch();
942 }
943
944 auto result = i->second.message();
945
946 if(result.first == Handshake_Type::ClientHello) {
947 m_awaiting_cookie_client_hello = false;
948
949 // RFC 6347 4.2.4 (as corrected by erratum 5186): "the server MUST use the
950 // message_seq in the ClientHello as the message_seq in the
951 // HelloVerifyRequest" and "the server MUST use the message_seq in the
952 // ClientHello as the message_seq in its initial ServerHello". Until a
953 // flight has been sent, the outgoing counter therefore follows the
954 // ClientHello just delivered.
955 if(server_awaiting_first_flight()) {
956 m_out_message_seq = static_cast<uint16_t>(m_in_message_seq - 1);
957 }
958
959 // The ClientHello we were waiting for arrived, so a buffered duplicate of
960 // an earlier one has nothing left to tell us.
961 if(m_retransmitted_client_hello.has_value()) {
962 release_reassembly_bytes(m_retransmitted_client_hello->second);
963 m_retransmitted_client_hello.reset();
964 }
965 }
966
967 const uint16_t delivered_epoch = i->second.epoch();
968
969 release_reassembly_bytes(i->second);
970 m_messages.erase(i);
971
972 if(delivered_epoch > m_last_delivered_epoch) {
973 m_last_delivered_epoch = delivered_epoch;
974 discard_stale_epoch_messages();
975 }
976
977 return result;
978}
979
980void Datagram_Handshake_IO::Handshake_Reassembly::add_fragment(const uint8_t fragment[],
981 size_t fragment_length,
982 size_t fragment_offset,
983 uint16_t epoch,
984 Handshake_Type msg_type,
985 size_t msg_length) {
986 if(m_msg_type == Handshake_Type::None) {
987 // First fragment for this message_seq
988 m_epoch = epoch;
989 m_msg_type = msg_type;
990 m_msg_length = msg_length;
991 m_message.resize(msg_length);
992 m_received_mask.assign(msg_length, 0);
993 } else {
994 if(complete()) {
995 // Ignore even if the header fields disagree: a stray or forged
996 // retransmission must not tear down the connection once the
997 // message has already been fully received.
998 return;
999 }
1000
1001 if(msg_type != m_msg_type || msg_length != m_msg_length || epoch != m_epoch) {
1002 throw Decoding_Error("Inconsistent values in fragmented DTLS handshake header");
1003 }
1004 }
1005
1006 if(fragment_offset > m_msg_length) {
1007 throw Decoding_Error("Fragment offset past end of message");
1008 }
1009
1010 if(fragment_offset + fragment_length > m_msg_length) {
1011 throw Decoding_Error("Fragment overlaps past end of message");
1012 }
1013
1014 BOTAN_ASSERT_NOMSG(m_received_mask.size() == m_msg_length);
1015
1016 for(size_t i = 0; i != fragment_length; ++i) {
1017 const size_t off = fragment_offset + i;
1018 if(m_received_mask[off] != 0) {
1019 // RFC 6347 4.2.3 permits overlapping retransmissions, but the
1020 // overlapping bytes must agree.
1021 if(m_message[off] != fragment[i]) {
1022 /*
1023 A ClientHello begins a handshake, so nothing already buffered in its
1024 slot can belong to the same one. At epoch zero neither the buffered
1025 bytes nor the ClientHello is authenticated, and throwing means the
1026 *ClientHello* is what fails: one planted fragment would then sink
1027 every later handshake attempt. Start the slot over from the
1028 ClientHello instead, so a peer that retransmits can always make
1029 progress. An authenticated peer contradicting itself is still an
1030 error. A ClientHello whose header differs from the slot's replaces
1031 it before this point, in charged_add_fragment.
1032 */
1033 if(!superseded_by_client_hello(epoch, msg_type)) {
1034 throw Decoding_Error("Inconsistent overlapping DTLS handshake fragment");
1035 }
1036 m_bytes_received = 0;
1037 std::fill(m_received_mask.begin(), m_received_mask.end(), 0);
1038 add_fragment(fragment, fragment_length, fragment_offset, epoch, msg_type, msg_length);
1039 return;
1040 }
1041 } else {
1042 m_message[off] = fragment[i];
1043 m_received_mask[off] = 1;
1044 ++m_bytes_received;
1045 }
1046 }
1047}
1048
1049bool Datagram_Handshake_IO::Handshake_Reassembly::complete() const {
1050 return (m_msg_type != Handshake_Type::None && m_bytes_received == m_msg_length);
1051}
1052
1053std::pair<Handshake_Type, std::vector<uint8_t>> Datagram_Handshake_IO::Handshake_Reassembly::message() const {
1054 if(!complete()) {
1055 throw Internal_Error("Datagram_Handshake_IO - message not complete");
1056 }
1057
1058 return std::make_pair(m_msg_type, m_message);
1059}
1060
1061std::vector<uint8_t> Datagram_Handshake_IO::format_fragment(const uint8_t fragment[],
1062 size_t frag_len,
1063 uint32_t frag_offset,
1064 uint32_t msg_len,
1065 Handshake_Type type,
1066 uint16_t msg_sequence) const {
1067 std::vector<uint8_t> send_buf(12 + frag_len);
1068
1069 send_buf[0] = static_cast<uint8_t>(type);
1070
1071 store_be24(&send_buf[1], msg_len);
1072
1073 store_be(msg_sequence, &send_buf[4]);
1074
1075 store_be24(&send_buf[6], frag_offset);
1076 store_be24(&send_buf[9], frag_len);
1077
1078 if(frag_len > 0) {
1079 copy_mem(&send_buf[12], fragment, frag_len);
1080 }
1081
1082 return send_buf;
1083}
1084
1085std::vector<uint8_t> Datagram_Handshake_IO::format_w_seq(const std::vector<uint8_t>& msg,
1086 Handshake_Type type,
1087 uint16_t msg_sequence) const {
1088 return format_fragment(msg.data(), msg.size(), 0, static_cast<uint32_t>(msg.size()), type, msg_sequence);
1089}
1090
1091std::vector<uint8_t> Datagram_Handshake_IO::format(const std::vector<uint8_t>& msg, Handshake_Type type) const {
1092 // Formats the message just delivered, so the guard is that one exists, not
1093 // that the counter is non-zero. Those differ once m_in_message_seq wraps,
1094 // where the subtraction wraps to 65535 of its own accord, which is the
1095 // right sequence number for that message.
1096 BOTAN_ASSERT_NOMSG(m_first_delivered_epoch.has_value());
1097 return format_w_seq(msg, type, static_cast<uint16_t>(m_in_message_seq - 1));
1098}
1099
1100std::vector<uint8_t> Datagram_Handshake_IO::send(const Handshake_Message& msg) {
1101 return this->send_under_epoch(msg, m_seqs.current_write_epoch());
1102}
1103
1104std::vector<uint8_t> Datagram_Handshake_IO::send_under_epoch(const Handshake_Message& msg, uint16_t epoch) {
1105 const std::vector<uint8_t> msg_bits = msg.serialize();
1106 const Handshake_Type msg_type = msg.type();
1107
1108 if(msg_type == Handshake_Type::HandshakeCCS) {
1109 m_flight_ccs.rbegin()->emplace_back(m_flights.rbegin()->size(), epoch);
1110 m_send_hs(epoch, Record_Type::ChangeCipherSpec, msg_bits);
1111 return {}; // not included in handshake hashes
1112 } else if(msg_type == Handshake_Type::HelloVerifyRequest) {
1113 // RFC 6347 3.2.1: "Note that timeout and retransmission do not apply to
1114 // the HelloVerifyRequest, because this would require creating state on
1115 // the server." So it is not retained as a flight, and a repeated
1116 // ClientHello simply produces it again. get_next_record seated the
1117 // outgoing counter at the ClientHello being answered, so this carries
1118 // that ClientHello's message_seq (RFC 6347 4.2.4, erratum 5186).
1119 const uint16_t msg_seq = m_out_message_seq++;
1120
1121 // A ClientHello carrying the cookie cannot have been sent yet, so
1122 // anything received so far is useless and must not stay allocated
1123 if(!m_recreating_hello_verify_request) {
1124 discard_pending_messages();
1125 }
1126 m_awaiting_cookie_client_hello = true;
1127 m_recreating_hello_verify_request = false;
1128 send_message(msg_seq, epoch, msg_type, msg_bits);
1129 return {};
1130 }
1131
1132 m_flights.rbegin()->push_back(m_out_message_seq);
1133 m_flight_data.insert_or_assign(m_out_message_seq, Message_Info(epoch, msg_type, msg_bits));
1134
1135 m_out_message_seq += 1;
1136 m_last_write = m_steady_clock_ms();
1137 m_next_timeout = m_initial_timeout;
1138 // Sending a new flight is forward progress: reset the give-up counter so the
1139 // retransmission budget applies per flight, not across the whole handshake.
1140 m_retransmit_count = 0;
1141 m_peer_replay_count = 0;
1142
1143 return send_message(m_out_message_seq - 1, epoch, msg_type, msg_bits);
1144}
1145
1146#if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
1147
1148std::vector<uint8_t> Datagram_Handshake_IO::start_with_client_hello_from_downgrade(
1149 const Handshake_Message& client_hello) {
1151 "Expected ClientHello message for DTLS downgrade");
1152 BOTAN_STATE_CHECK(m_out_message_seq == 0);
1154 return format_w_seq(client_hello.serialize(), client_hello.wire_type(), m_out_message_seq++);
1155}
1156
1157#endif
1158
1159std::vector<uint8_t> Datagram_Handshake_IO::send_message(uint16_t msg_seq,
1160 uint16_t epoch,
1161 Handshake_Type msg_type,
1162 const std::vector<uint8_t>& msg_bits) {
1163 auto no_fragment = format_w_seq(msg_bits, msg_type, msg_seq);
1164
1165 /**
1166 * Since CBC suites are no longer supported/allowed in DTLS, the largest
1167 * possible ciphersuite overhead is 48 bytes, from NULL_WITH_SHA384. The AEAD
1168 * suites add at most 24 bytes (8 byte explicit nonce plus 16 byte tag).
1169 */
1170 const size_t ciphersuite_overhead = (epoch > 0) ? 48 : 0;
1171
1172 if(no_fragment.size() + DTLS_HEADER_SIZE + ciphersuite_overhead <= m_mtu) {
1173 // We think the entire final packet will fit into the MTU
1174 m_send_hs(epoch, Record_Type::Handshake, no_fragment);
1175 } else {
1176 size_t frag_offset = 0;
1177
1178 constexpr size_t DTLS_HANDSHAKE_OVERHEAD = DTLS_HEADER_SIZE + DTLS_HANDSHAKE_HEADER_SIZE;
1179
1180 if(m_mtu <= (DTLS_HANDSHAKE_OVERHEAD + ciphersuite_overhead)) {
1181 throw Invalid_Argument("DTLS MTU is too small to send headers");
1182 }
1183
1184 const size_t max_rec_size = m_mtu - (DTLS_HANDSHAKE_OVERHEAD + ciphersuite_overhead);
1185
1186 while(frag_offset != msg_bits.size()) {
1187 const size_t frag_len = std::min<size_t>(msg_bits.size() - frag_offset, max_rec_size);
1188
1189 const std::vector<uint8_t> frag = format_fragment(&msg_bits[frag_offset],
1190 frag_len,
1191 static_cast<uint32_t>(frag_offset),
1192 static_cast<uint32_t>(msg_bits.size()),
1193 msg_type,
1194 msg_seq);
1195
1196 m_send_hs(epoch, Record_Type::Handshake, frag);
1197
1198 frag_offset += frag_len;
1199 }
1200 }
1201
1202 return no_fragment;
1203}
1204
1205} // namespace Botan::TLS
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:136
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:69
virtual uint16_t current_write_epoch() const =0
std::function< uint64_t()> steady_clock_fn
std::function< void(uint16_t, Record_Type, const std::vector< uint8_t > &)> writer_fn
std::vector< uint8_t > send_under_epoch(const Handshake_Message &msg, uint16_t epoch) override
std::optional< std::chrono::milliseconds > next_retransmission_timeout() const override
std::pair< Handshake_Type, std::vector< uint8_t > > get_next_record(bool expecting_ccs, size_t max_message_size) override
void add_record(const uint8_t record[], size_t record_len, Record_Type type, uint64_t sequence_number) 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
void finalize_handshake(bool retransmit_terminal_flight)
void add_retransmitted_record(const uint8_t record[], size_t record_len, Record_Type type, uint64_t sequence_number)
std::vector< uint8_t > send(const Handshake_Message &msg) override
Datagram_Handshake_IO(writer_fn writer, steady_clock_fn clock_ms, class Connection_Sequence_Numbers &seq, uint16_t mtu, uint64_t initial_timeout_ms, uint64_t max_timeout_ms, std::optional< size_t > max_retransmissions, size_t max_handshake_msg_size, bool is_server, uint16_t initial_epoch=0)
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::vector< uint8_t > send(const Handshake_Message &msg) override
std::pair< Handshake_Type, std::vector< uint8_t > > get_next_record(bool expecting_ccs, size_t max_message_size) override
void add_record(const uint8_t record[], size_t record_len, Record_Type type, uint64_t sequence_number) override
@ DTLS_HEADER_SIZE
Definition tls_magic.h:31
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
constexpr uint32_t make_uint32(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3)
Definition loadstor.h:104
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504