1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
use std::mem;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::time::{Duration, Instant};
use super::queue::{QueueRx, QueueTx};
use crate::buffer::{Buf, BufferPool, TmpBuf};
use crate::crypto::{Aad, Iv, Nonce};
use crate::dtls12::context::{AuthMode, CryptoContext};
use crate::dtls12::incoming::{Incoming, Record, RecordHandler};
use crate::dtls12::message::{Body, HashAlgorithm, Header, MessageType, ProtocolVersion, Sequence};
use crate::dtls12::message::{ContentType, DTLSRecord, Dtls12CipherSuite, Handshake};
use crate::error::bounded_error_len;
use crate::timer::ExponentialBackoff;
use crate::window::ReplayWindow;
use crate::{Config, Error, InternalError, Output, SeededRng};
const MAX_DEFRAGMENT_PACKETS: usize = 50;
// Using debug_ignore_primary since CryptoContext doesn't implement Debug
pub struct Engine {
config: Arc<Config>,
/// Seedable random number generator for deterministic testing
pub(crate) rng: SeededRng,
/// Pool of buffers
buffers_free: BufferPool,
/// Counters for sending DTLSRecord during epoch 0.
///
/// This is kept separate since resends might force us to
/// "go back" to these sequence number even if we technically
/// progressed to epoch 1.
sequence_epoch_0: Sequence,
/// Counters for epoch 1 and beyond.
sequence_epoch_n: Sequence,
/// Queue of incoming packets.
queue_rx: QueueRx,
/// Queue of outgoing packets.
queue_tx: QueueTx,
/// The cipher suite in use. Set by ServerHello.
cipher_suite: Option<Dtls12CipherSuite>,
/// Per-record explicit nonce length, cached from the provider suite at
/// `set_cipher_suite` time. 0 for ChaCha20-Poly1305, 8 for AES-GCM.
explicit_nonce_len: usize,
/// Minimum length of a protected record's encrypted fragment, cached from
/// the provider suite. See
/// [`crate::crypto::SupportedDtls12CipherSuite::min_protected_fragment_len`].
/// For AEAD suites this is `explicit_nonce + tag`; used both to reject
/// short incoming records and to size outgoing record overhead.
min_protected_fragment_len: usize,
/// Cryptographic context for handling encryption/decryption
pub(crate) crypto_context: CryptoContext,
/// Whether the remote peer has enabled encryption
peer_encryption_enabled: bool,
/// Whether this engine is for a client (true) or server (false)
is_client: bool,
/// Expected peer handshake sequence number
peer_handshake_seq_no: u16,
/// Next handshake message sequence number for sending
next_handshake_seq_no: u16,
/// Handshakes collected for hash computation.
///
/// NB: pub(crate) because we need to sign it in client.rs
pub(crate) transcript: Buf,
/// Anti-replay window state (per current epoch)
replay: ReplayWindow,
/// The records that have been sent in the current flight.
flight_saved_records: Vec<Entry>,
/// Flight backoff
flight_backoff: ExponentialBackoff,
/// Timeout for the current flight
flight_timeout: Timeout,
/// Global timeout for the entire connect operation.
connect_timeout: Timeout,
/// Whether we are ready to release application data from poll_output.
release_app_data: bool,
/// Whether we have confirmation that the peer completed the handshake, i.e.
/// that the peer received our final flight. Once set, a stale plaintext
/// handshake (which is unauthenticated and replayable after encryption is
/// enabled) no longer triggers a courtesy flight retransmission.
///
/// This is set from two signals, each meaningful for one role:
/// - the client stops its resend timer when it completes, which means the
/// server received our final flight (the client confirms at completion);
/// - receiving authenticated application data, which means the peer is past
/// its handshake (the server's only proof the client received flight 6).
peer_handshake_confirmed: bool,
/// Whether a close_notify alert has been received from the peer.
close_notify_received: bool,
/// Whether [`Output::CloseNotify`] has already been emitted.
close_notify_reported: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Timeout {
Disabled,
Unarmed,
Armed(Instant),
}
#[derive(Debug)]
struct Entry {
content_type: ContentType,
epoch: u16,
fragment: Buf,
}
enum PollOutput<'a> {
Data(&'a [u8]),
BufferTooSmall { needed: usize },
None(&'a mut [u8]),
}
impl Engine {
pub fn new(config: Arc<Config>, auth: AuthMode) -> Self {
let mut rng = SeededRng::new(config.rng_seed());
let flight_backoff =
ExponentialBackoff::new(config.flight_start_rto(), config.flight_retries(), &mut rng);
let crypto_context = CryptoContext::new(auth, Arc::clone(&config));
Self {
config,
rng,
buffers_free: BufferPool::default(),
sequence_epoch_0: Sequence::new(0),
sequence_epoch_n: Sequence::new(1),
queue_rx: QueueRx::new(),
queue_tx: QueueTx::new(),
cipher_suite: None,
explicit_nonce_len: 0,
min_protected_fragment_len: 0,
crypto_context,
peer_encryption_enabled: false,
is_client: false,
peer_handshake_seq_no: 0,
next_handshake_seq_no: 0,
transcript: Buf::new(),
replay: ReplayWindow::new(),
flight_saved_records: Vec::new(),
flight_backoff,
flight_timeout: Timeout::Unarmed,
connect_timeout: Timeout::Unarmed,
release_app_data: false,
peer_handshake_confirmed: false,
close_notify_received: false,
close_notify_reported: false,
}
}
pub fn set_client(&mut self, is_client: bool) {
self.is_client = is_client;
}
/// Set the next outgoing handshake message sequence number.
///
/// Used by `Client::new_from_hybrid` to account for the hybrid
/// ClientHello (message_seq=0) that was already sent outside this engine.
pub fn set_next_handshake_seq_no(&mut self, seq: u16) {
self.next_handshake_seq_no = seq;
}
/// Advance the epoch-0 record sequence number by one.
///
/// Used by `Client::new_from_hybrid` so subsequent epoch-0 records
/// don't reuse the sequence number of the hybrid ClientHello record.
pub fn advance_epoch_0_sequence(&mut self) {
self.sequence_epoch_0.sequence_number += 1;
}
pub fn config(&self) -> &Config {
&self.config
}
/// Get a reference to the cipher suite
pub fn cipher_suite(&self) -> Option<Dtls12CipherSuite> {
self.cipher_suite
}
/// Minimum length of a protected record's encrypted fragment for the
/// negotiated suite. See
/// [`crate::crypto::SupportedDtls12CipherSuite::min_protected_fragment_len`].
pub fn min_protected_fragment_len(&self) -> usize {
self.min_protected_fragment_len
}
/// Is the given cipher suite allowed by configuration
pub fn is_cipher_suite_allowed(&self, suite: Dtls12CipherSuite) -> bool {
self.config
.dtls12_cipher_suites()
.any(|cs| cs.suite() == suite)
}
/// Get a reference to the crypto context
pub fn crypto_context(&self) -> &CryptoContext {
&self.crypto_context
}
/// Get a mutable reference to the crypto context
pub fn crypto_context_mut(&mut self) -> &mut CryptoContext {
&mut self.crypto_context
}
pub fn parse_packet(&mut self, packet: &[u8]) -> Result<(), InternalError> {
let cs = self.cipher_suite;
let incoming = Incoming::parse_packet(packet, self, cs)?;
if let Some(incoming) = incoming {
self.insert_incoming(incoming)?;
}
Ok(())
}
/// Insert a parsed datagram into the receive queue.
fn insert_incoming(&mut self, incoming: Incoming) -> Result<(), Error> {
// Capacity guard before iterating records.
if self.queue_rx.len() >= self.config.max_queue_rx() {
warn!(
"Receive queue full (max {}): {:?}",
self.config.max_queue_rx(),
self.queue_rx
);
return Err(Error::ReceiveQueueFull);
}
// Dispatch to specialized handlers
if incoming.first().first_handshake().is_some() {
self.insert_incoming_handshake(incoming)
} else {
self.insert_incoming_non_handshake(incoming)
}
}
fn insert_incoming_handshake(&mut self, incoming: Incoming) -> Result<(), Error> {
let first_record = incoming.first();
let handshake = first_record
.first_handshake()
.expect("caller ensures handshake");
let key_current = (
handshake.header.message_seq,
handshake.header.fragment_offset,
);
let maybe_dupe_seq = incoming
.records()
.iter()
.filter_map(|r| r.first_handshake())
.filter_map(|h| h.dupe_triggers_resend())
.next();
// Some MessageType when resent, means we must trigger
// an immediate resend of the entire flight. Once the peer is confirmed
// to have completed the handshake, a stale plaintext handshake is just
// unauthenticated noise (or a replay/amplification attempt) and must not
// drive a resend.
if let Some(dupe_seq) = maybe_dupe_seq {
if dupe_seq < self.peer_handshake_seq_no && !self.peer_handshake_confirmed {
self.flight_resend("dupe triggers resend")?;
}
}
// Drop old duplicates we've already processed - don't let them block newer messages.
if handshake.header.message_seq < self.peer_handshake_seq_no {
return Ok(());
}
if self.peer_encryption_enabled && first_record.record().sequence.epoch == 0 {
// Keep old plaintext handshake records available long enough to
// trigger flight resends above, but never queue or process them as
// new messages after peer encryption is enabled.
return Ok(());
}
// Reject new handshakes after initial handshake is complete (renegotiation not supported).
if self.release_app_data && handshake.header.message_seq >= self.peer_handshake_seq_no {
return Err(Error::RenegotiationAttempt);
}
let search_result = self.queue_rx.binary_search_by(|item| {
let key_other = item
.first()
.first_handshake()
.as_ref()
.map(|h| (h.header.message_seq, h.header.fragment_offset))
.unwrap_or((u16::MAX, u32::MAX));
key_other.cmp(&key_current)
});
match search_result {
Err(index) => {
// Insert in order of handshake key
self.queue_rx.insert(index, incoming);
}
Ok(_) => {
// Exact duplicate handshake fragment
}
}
Ok(())
}
fn insert_incoming_non_handshake(&mut self, incoming: Incoming) -> Result<(), Error> {
let first = incoming.first();
let seq_current = first.record().sequence;
if self.peer_encryption_enabled
&& seq_current.epoch == 0
&& first.record().content_type == ContentType::Handshake
{
return Ok(());
}
if self.peer_encryption_enabled {
for record in incoming.records().iter() {
if record.record().sequence.epoch == 0
&& record.record().content_type == ContentType::Handshake
{
if record.handshakes().is_empty() {
record.set_handled();
} else {
for handshake in record.handshakes() {
handshake.set_handled();
}
}
}
}
}
let search_result = self
.queue_rx
.binary_search_by_key(&seq_current, |item| item.first().record().sequence);
match search_result {
Err(index) => self.queue_rx.insert(index, incoming),
Ok(_) => {
// For epoch 0, we can get duplicates due to resends.
// For epoch 1, we have the replay window and there should
// be no duplicates.
assert_eq!(seq_current.epoch, 0);
}
}
Ok(())
}
pub fn handle_timeout(&mut self, now: Instant) -> Result<(), Error> {
if self.connect_timeout == Timeout::Unarmed {
debug!(
"Connect timeout in: {:.03}s",
self.config.handshake_timeout().as_secs_f32()
);
let timeout = now + self.config.handshake_timeout();
self.connect_timeout = Timeout::Armed(timeout);
}
if self.flight_timeout == Timeout::Unarmed {
debug!(
"Flight timeout in: {:.03}s",
self.flight_backoff.rto().as_secs_f32()
);
let timeout = now + self.flight_backoff.rto();
self.flight_timeout = Timeout::Armed(timeout);
}
// The connect timeout is the overall timeout for establishing the connection
if let Timeout::Armed(connect_timeout) = self.connect_timeout {
if now >= connect_timeout {
return Err(Error::Timeout(crate::TimeoutError::Connect));
}
}
// If there is no flight timeout, we have already checked the global connect timeout.
let Timeout::Armed(flight_timeout) = self.flight_timeout else {
return Ok(());
};
if now >= flight_timeout {
if self.flight_backoff.can_retry() {
self.flight_backoff.attempt(&mut self.rng);
debug!(
"Re-arm flight timeout due to resend in {}",
self.flight_backoff.rto().as_secs_f32()
);
let timeout = now + self.flight_backoff.rto();
self.flight_timeout = Timeout::Armed(timeout);
self.flight_resend("flight timeout")?;
} else {
return Err(Error::Timeout(crate::TimeoutError::Handshake));
}
}
Ok(())
}
pub fn poll_output<'a>(&mut self, buf: &'a mut [u8], now: Instant) -> Output<'a> {
// Drain incoming queue of processed records.
self.purge_handled_queue_rx();
let buf = match self.poll_app_data(buf) {
PollOutput::Data(p) => return Output::ApplicationData(p),
PollOutput::BufferTooSmall { needed } => return Output::BufferTooSmall { needed },
PollOutput::None(b) => b,
};
match self.poll_packet_tx(buf) {
PollOutput::Data(p) => return Output::Packet(p),
PollOutput::BufferTooSmall { needed } => return Output::BufferTooSmall { needed },
PollOutput::None(_) => {}
}
if self.close_notify_received && !self.close_notify_reported {
self.close_notify_reported = true;
return Output::CloseNotify;
}
let next_timeout = self.poll_timeout(now);
Output::Timeout(next_timeout)
}
fn poll_app_data<'a>(&mut self, buf: &'a mut [u8]) -> PollOutput<'a> {
if !self.release_app_data {
return PollOutput::None(buf);
}
let mut unhandled = self
.queue_rx
.iter()
.flat_map(|i| i.records().iter())
.filter(|r| r.record().content_type == ContentType::ApplicationData)
.skip_while(|r| r.is_handled());
let Some(next) = unhandled.next() else {
return PollOutput::None(buf);
};
let record_buffer = next.buffer();
let fragment = next.record().fragment(record_buffer);
let len = fragment.len();
if len > buf.len() {
return PollOutput::BufferTooSmall { needed: len };
}
buf[..len].copy_from_slice(fragment);
next.set_handled();
PollOutput::Data(&buf[..len])
}
fn purge_handled_queue_rx(&mut self) {
while let Some(peek) = self.queue_rx.front() {
let fully_handled = peek.records().iter().all(|r| r.is_handled());
if fully_handled {
let incoming = self.queue_rx.pop_front().unwrap();
incoming
.into_records()
.for_each(|r| self.buffers_free.push(r.into_buffer()));
} else {
break;
}
}
}
fn poll_packet_tx<'a>(&mut self, buf: &'a mut [u8]) -> PollOutput<'a> {
let Some(p) = self.queue_tx.front() else {
return PollOutput::None(buf);
};
if p.len() > buf.len() {
return PollOutput::BufferTooSmall { needed: p.len() };
}
let p = self
.queue_tx
.pop_front()
.expect("queue front checked above");
let len = p.len();
buf[..len].copy_from_slice(&p);
PollOutput::Data(&buf[..len])
}
fn poll_timeout(&self, now: Instant) -> Instant {
// No timeouts, return a distant future
if self.connect_timeout == Timeout::Disabled && self.flight_timeout == Timeout::Disabled {
const DISTANT_FUTURE: Duration = Duration::from_secs(10 * 365 * 24 * 60 * 60);
return now + DISTANT_FUTURE;
}
match (self.connect_timeout, self.flight_timeout) {
// Keep this before the `(Armed, _)` arms. Starting a new flight resets its timer to
// `Unarmed`, but leaves the overall connection timer armed. If that mixed state
// returned the connection deadline, the caller would not drive `handle_timeout` to
// arm the flight timer until the whole handshake expired, so the flight would never
// be retransmitted. Returning `now` requests that immediate drive; the next poll sees
// both concrete deadlines and can return the earlier one.
(Timeout::Unarmed, _) | (_, Timeout::Unarmed) => now,
(Timeout::Armed(c), Timeout::Armed(f)) => {
if c < f {
c
} else {
f
}
}
(Timeout::Armed(c), _) => c,
(_, Timeout::Armed(f)) => f,
_ => now,
}
}
pub fn flight_begin(&mut self, flight_no: u8) {
debug!("Begin flight {}", flight_no);
self.flight_backoff.reset(&mut self.rng);
self.flight_clear_resends();
self.flight_timeout = Timeout::Unarmed;
}
pub fn flight_stop_resend_timers(&mut self) {
debug!("Stop connect and flight timeouts");
self.flight_timeout = Timeout::Disabled;
self.connect_timeout = Timeout::Disabled;
// The client stops its resend timer only once it has received the
// server's final flight, which proves the server received the client's
// final flight — so the peer is confirmed. The server, by contrast,
// stops its timer right after sending flight 6, before the client has
// confirmed anything, so it must NOT confirm here (it relies on later
// authenticated application data instead).
if self.is_client {
self.peer_handshake_confirmed = true;
}
}
fn flight_clear_resends(&mut self) {
for entry in self.flight_saved_records.drain(..) {
self.buffers_free.push(entry.fragment);
}
}
fn flight_resend(&mut self, reason: &str) -> Result<(), Error> {
debug!("Resending flight due to {}", reason);
let replace_pending_handshake_output = !self.release_app_data;
if replace_pending_handshake_output {
self.queue_tx.clear();
}
// For lifetime issues, we take the entries out of self
let records = mem::take(&mut self.flight_saved_records);
let mut result = Ok(());
for (index, entry) in records.iter().enumerate() {
result = self.create_record_inner(
entry.content_type,
entry.epoch,
false,
replace_pending_handshake_output && index == 0,
|fragment| {
fragment.extend_from_slice(&entry.fragment);
},
);
if result.is_err() {
break;
}
}
// Put the entries back into self
self.flight_saved_records = records;
result
}
pub fn has_complete_handshake(&mut self, wanted: MessageType) -> bool {
self.has_complete_handshake_with_seq(wanted, self.peer_handshake_seq_no)
}
fn has_complete_handshake_with_seq(&mut self, wanted: MessageType, expected_seq: u16) -> bool {
let mut skip_handled = self
.queue_rx
.iter()
.flat_map(|i| i.records().iter())
.skip_while(|r| r.is_handled())
// Cap to MAX_DEFRAGMENT_PACKETS to avoid misbehaving peers
.take(MAX_DEFRAGMENT_PACKETS)
.flat_map(|r| r.handshakes().iter())
.skip_while(|h| h.is_handled())
.peekable();
let maybe_first_handshake = skip_handled.peek();
let Some(first) = maybe_first_handshake else {
return false;
};
if first.header.message_seq != expected_seq {
return false;
}
if first.header.msg_type != wanted {
return false;
}
let wanted_seq = first.header.message_seq;
let wanted_length = first.header.length;
let mut last_fragment_end = 0;
for h in skip_handled {
// A different seq means we're looking at a different handshake
if wanted_seq != h.header.message_seq {
continue;
}
// Check fragment contiguity
if h.header.fragment_offset != last_fragment_end {
return false;
}
last_fragment_end = h.header.fragment_offset + h.header.fragment_length;
// Found the last fragment to complete the wanted handshake.
if last_fragment_end == wanted_length {
return true;
}
}
false
}
pub fn next_handshake(
&mut self,
wanted: MessageType,
defragment_buffer: &mut Buf,
) -> Result<Option<Handshake>, InternalError> {
if !self.has_complete_handshake(wanted) {
return Ok(None);
}
let iter = self
.queue_rx
.iter()
.flat_map(|i| i.records().iter())
.skip_while(|r| r.is_handled())
.flat_map(|r| r.handshakes().iter().map(move |h| (h, r.buffer())))
.skip_while(|(h, _)| h.is_handled());
// This sets the handled flag on the handshake.
// Passing Some(&mut self.transcript) to have defragment write to transcript
// before creating the handshake, avoiding borrow conflicts.
let handshake = Handshake::defragment(
iter,
defragment_buffer,
self.cipher_suite,
Some(&mut self.transcript),
)?;
// Move the expected seq_no along
self.peer_handshake_seq_no = handshake.header.message_seq + 1;
Ok(Some(handshake))
}
pub(crate) fn next_record(&mut self, ctype: ContentType) -> Option<&Record> {
let record = self
.queue_rx
.iter()
.flat_map(|i| i.records().iter())
.find(|r| !r.is_handled())?;
if record.record().content_type != ctype {
return None;
}
record.set_handled();
Some(record)
}
/// Mark any pending ChangeCipherSpec records as handled and purge them.
/// We can accumulate multiple ChangeCipherSpec due to resends. Since they
/// don't have any Handshake message_seq and each resend gives a new DTLSRecord
/// sequence number, we might have multiple.
pub fn drop_pending_ccs(&mut self) {
for incoming in self.queue_rx.iter() {
for record in incoming.records().iter() {
if record.record().content_type == ContentType::ChangeCipherSpec {
record.set_handled();
}
}
}
}
/// Create a DTLS record and serialize it into a buffer
pub fn create_record<F>(
&mut self,
content_type: ContentType,
epoch: u16,
save_fragment: bool,
f: F,
) -> Result<(), Error>
where
F: FnOnce(&mut Buf),
{
self.create_record_inner(content_type, epoch, save_fragment, false, f)
}
fn create_record_inner<F>(
&mut self,
content_type: ContentType,
epoch: u16,
save_fragment: bool,
force_new_datagram: bool,
f: F,
) -> Result<(), Error>
where
F: FnOnce(&mut Buf),
{
let maybe_suite = if epoch >= 1 {
Some(self.cipher_suite().ok_or(Error::InvalidState(
crate::InvalidStateError::NoCipherSuiteSelected,
))?)
} else {
None
};
// Prepare the plaintext fragment
let mut fragment = self.buffers_free.pop();
// Let the caller fill the fragment (plaintext)
f(&mut fragment);
// Use this as a marker to know whether we are to record fragments for resends.
if save_fragment {
let mut clone = self.buffers_free.pop();
clone.extend_from_slice(&fragment);
self.flight_saved_records.push(Entry {
content_type,
epoch,
fragment: clone,
});
}
// Compute wire length of the record if serialized into a datagram.
// Record header (13) + handshake/change/app data bytes + per-suite
// protection overhead (if epoch >= 1). For AEAD suites the protection
// overhead equals the min-protected-fragment-len.
let overhead = if maybe_suite.is_some() {
self.min_protected_fragment_len()
} else {
0
};
let record_wire_len = DTLSRecord::HEADER_LEN + fragment.len() + overhead;
// Decide whether to append to the existing last datagram or create a new one
let can_append = self
.queue_tx
.back()
.map(|b| !force_new_datagram && b.len() + record_wire_len <= self.config.mtu())
.unwrap_or(false);
// If we cannot append, ensure we have space for a new datagram
if !can_append && self.queue_tx.len() >= self.config.max_queue_tx() {
warn!(
"Transmit queue full (max {}): {:?}",
self.config.max_queue_tx(),
self.queue_tx
);
return Err(Error::TransmitQueueFull);
}
// Sequence number to use for this record
let sequence = if epoch == 0 {
self.sequence_epoch_0
} else {
self.sequence_epoch_n
};
let length = fragment.len() as u16;
// Handle encryption for epochs >= 1
if epoch >= 1 {
let suite = maybe_suite.expect("cipher suite must be set for encrypted epochs");
// Get the fixed part of the IV
let iv = if self.is_client {
self.crypto_context.get_client_write_iv()
} else {
self.crypto_context.get_server_write_iv()
};
let Some(iv) = iv else {
return Err(Error::CryptoError(
crate::CryptoError::WriteIvNotAvailable {
is_client: self.is_client,
},
));
};
let explicit_nonce_len = self.explicit_nonce_len;
let mut explicit_nonce = [0u8; DTLSRecord::EXPLICIT_NONCE_LEN];
let seq64 = ((sequence.epoch as u64) << 48) | sequence.sequence_number;
let nonce = match explicit_nonce_len {
0 => Nonce::xor(iv.as_12_bytes(), seq64),
DTLSRecord::EXPLICIT_NONCE_LEN => {
explicit_nonce = self.rng.random();
Nonce::new(iv, &explicit_nonce)
}
_ => {
return Err(Error::CryptoError(
crate::CryptoError::UnsupportedDtls12RecordIvLen {
len: bounded_error_len(explicit_nonce_len),
suite,
},
));
}
};
// DTLS 1.2 AEAD: AAD uses the plaintext length (DTLSCompressed.length).
let aad = Aad::new_dtls12(content_type, sequence, length);
// Encrypt the fragment in-place
self.encrypt_data(&mut fragment, aad, nonce)?;
let ctext_len = fragment.len();
// For suites with a per-record nonce (e.g. AES-GCM), prefix it on the wire.
if explicit_nonce_len > 0 {
fragment.resize(explicit_nonce_len + ctext_len, 0);
fragment.copy_within(0..ctext_len, explicit_nonce_len);
fragment[..explicit_nonce_len]
.copy_from_slice(&explicit_nonce[..explicit_nonce_len]);
}
}
// Build the record structure referencing the (possibly encrypted) fragment
let record = DTLSRecord {
content_type,
version: ProtocolVersion::DTLS1_2,
sequence,
length: fragment.len() as u16,
fragment_range: 0..fragment.len(),
};
// Increment the sequence number for the next transmission
if epoch == 0 {
self.sequence_epoch_0.sequence_number += 1;
} else {
self.sequence_epoch_n.sequence_number += 1;
}
// Serialize the record into the chosen datagram buffer
if can_append {
let last = self.queue_tx.back_mut().unwrap();
record.serialize(&fragment, last);
} else {
let mut buffer = self.buffers_free.pop();
buffer.clear();
record.serialize(&fragment, &mut buffer);
self.queue_tx.push_back(buffer);
}
// Return the fragment buffer to the pool
self.buffers_free.push(fragment);
Ok(())
}
/// Create a handshake message and wrap it in a DTLS record
pub fn create_handshake<F>(&mut self, msg_type: MessageType, f: F) -> Result<(), Error>
where
F: FnOnce(&mut Buf, &mut Self) -> Result<(), Error>,
{
// Get a buffer for the handshake body
let mut body_buffer = self.buffers_free.pop();
// Let the callback fill the handshake body
f(&mut body_buffer, self)?;
// Create the handshake header with the next sequence number
let handshake_header = Header {
msg_type,
length: body_buffer.len() as u32,
message_seq: self.next_handshake_seq_no,
fragment_offset: 0,
fragment_length: body_buffer.len() as u32,
};
let mut buffer_full = self.buffers_free.pop();
{
let handshake = Handshake {
header: handshake_header,
body: Body::Fragment(0..body_buffer.len()),
handled: AtomicBool::new(false),
};
// Serialize with body_buffer as source
handshake.serialize(&body_buffer, &mut buffer_full);
}
self.transcript.extend_from_slice(&buffer_full);
self.buffers_free.push(buffer_full);
// Increment the sequence number for the next handshake message
self.next_handshake_seq_no += 1;
// We want to pack as much as possible into the outgoing datagram and
// remain within the MTU. Fragment the handshake across records as needed.
let epoch = msg_type.epoch();
let total_len = body_buffer.len();
let mut offset: usize = 0;
// Handshake header is 12 bytes
let handshake_header_len = 12usize;
// Per-record protection overhead on the wire (for AEAD suites this is
// explicit_nonce + tag). Used to size fragments to fit the MTU.
let protection_overhead = if epoch >= 1 {
self.cipher_suite().ok_or(Error::InvalidState(
crate::InvalidStateError::NoCipherSuiteSelected,
))?;
self.min_protected_fragment_len()
} else {
0
};
// At least one record must be created even if total_len == 0
while offset < total_len || (total_len == 0 && offset == 0) {
// How many bytes are already used in the current datagram (if any)?
let already_used_in_current = self.queue_tx.back().map(|b| b.len()).unwrap_or(0);
let available_in_current = self.config.mtu().saturating_sub(already_used_in_current);
// Fixed overhead per handshake record on the wire:
// DTLS record header + handshake header + protection overhead (if epoch >= 1)
let fixed_overhead =
DTLSRecord::HEADER_LEN + handshake_header_len + protection_overhead;
// Prefer to pack into the current datagram. If the current one cannot fit even
// the fixed overhead, we will start a fresh datagram and compute space again.
let available_for_body = if available_in_current > fixed_overhead {
// There is room for at least 1 byte of handshake body in the current datagram
available_in_current - fixed_overhead
} else {
// Not enough space in the current datagram for any body bytes; start a fresh datagram
self.config.mtu().saturating_sub(fixed_overhead)
};
// Remaining bytes from the handshake body we still need to send.
let remaining_body_bytes = total_len.saturating_sub(offset);
// For empty-body handshakes (e.g., ServerHelloDone), we still send a header-only record.
let chunk_len = if total_len == 0 {
0
} else {
remaining_body_bytes.min(available_for_body)
};
let frag_range = if chunk_len == 0 {
0..0
} else {
offset..offset + chunk_len
};
let frag_handshake = Handshake {
header: Header {
msg_type,
length: handshake_header.length,
message_seq: handshake_header.message_seq,
fragment_offset: offset as u32,
fragment_length: chunk_len as u32,
},
body: Body::Fragment(frag_range),
handled: AtomicBool::new(false),
};
// Emit the record; packing into current datagram happens inside create_record
self.create_record(ContentType::Handshake, epoch, true, |fragment| {
// Serialize with body_buffer as source
frag_handshake.serialize(&body_buffer, fragment);
})?;
if total_len == 0 {
// Nothing more to send for empty-body handshake
break;
}
offset += chunk_len;
}
// Return the buffer
self.buffers_free.push(body_buffer);
Ok(())
}
/// Release application data from the incoming queue
pub fn release_application_data(&mut self) {
self.release_app_data = true;
}
/// Whether a close_notify alert has been received from the peer.
pub fn close_notify_received(&self) -> bool {
self.close_notify_received
}
/// Whether a received close_notify still needs to be surfaced.
pub fn close_notify_pending(&self) -> bool {
self.close_notify_received && !self.close_notify_reported
}
/// Whether close-related output remains to be polled.
pub fn has_pending_close_output(&self) -> bool {
!self.queue_tx.is_empty() || self.close_notify_pending()
}
/// Discard all pending outgoing data.
///
/// RFC 5246 §7.2.1: on receiving close_notify, discard any pending writes.
pub fn discard_pending_writes(&mut self) {
self.queue_tx.clear();
}
/// Abort the connection: flush all queued output, retransmission state, and
/// disable timers so that no further packets are emitted.
pub fn abort(&mut self) {
self.queue_tx.clear();
self.flight_saved_records.clear();
self.flight_timeout = Timeout::Disabled;
self.connect_timeout = Timeout::Disabled;
}
/// Pop a buffer from the buffer pool for temporary use
pub(crate) fn pop_buffer(&mut self) -> Buf {
self.buffers_free.pop()
}
/// Return a buffer to the buffer pool
pub(crate) fn push_buffer(&mut self, buf: Buf) {
self.buffers_free.push(buf);
}
/// Encrypt data appropriate for the role (client or server)
fn encrypt_data(&mut self, plaintext: &mut Buf, aad: Aad, nonce: Nonce) -> Result<(), Error> {
if self.is_client {
self.crypto_context
.encrypt_client_to_server(plaintext, aad, nonce)
.map_err(Error::CryptoError)
} else {
self.crypto_context
.encrypt_server_to_client(plaintext, aad, nonce)
.map_err(Error::CryptoError)
}
}
/// Decrypt data appropriate for the role (client or server)
pub fn decrypt_data(
&mut self,
ciphertext: &mut TmpBuf,
aad: Aad,
nonce: Nonce,
) -> Result<(), Error> {
if self.is_client {
self.crypto_context
.decrypt_server_to_client(ciphertext, aad, nonce)
.map_err(Error::CryptoError)
} else {
self.crypto_context
.decrypt_client_to_server(ciphertext, aad, nonce)
.map_err(Error::CryptoError)
}
}
/// Reset server handshake state after sending HelloVerifyRequest.
///
/// Per RFC 6347 §4.2.2, the HelloVerifyRequest exchange is stateless. After sending
/// HVR, the server expects a fresh ClientHello containing the cookie with message_seq=1.
///
/// The message flow per RFC 6347 §4.2.2:
/// ClientHello (seq=0) ------>
/// <------ HelloVerifyRequest (seq=0)
/// ClientHello (seq=1) ------> (with cookie)
/// <------ ServerHello (seq=1)
pub fn reset_server_for_hello_verify_request(&mut self) {
self.transcript.clear();
// Per RFC 6347 §4.2.2, the next ClientHello (with cookie) has message_seq=1.
// We keep peer_handshake_seq_no at 1 (already incremented after first ClientHello).
// Clear queued incoming handshakes so the next ClientHello (with cookie)
// isn't rejected as a duplicate of the first ClientHello (without cookie).
self.queue_rx.clear();
// Note: Don't clear flight_saved_records here - the HelloVerifyRequest should
// still be resendable via timeout until we receive the valid ClientHello with cookie.
// The flight_begin(4) call when processing the cookie-bearing ClientHello will
// clear the old records.
}
/// Reset client handshake state after receiving HelloVerifyRequest.
///
/// Per RFC 6347 §4.2.2, the client sends the next ClientHello (with cookie) using
/// message_seq=1. The transcript is cleared because the initial ClientHello and
/// HelloVerifyRequest are not part of the handshake transcript per RFC 6347 §4.2.1.
///
/// Note: next_handshake_seq_no is already 1 after sending the first ClientHello,
/// so we don't reset it - the next ClientHello will correctly have message_seq=1.
pub fn reset_client_for_hello_verify_request(&mut self) {
self.transcript.clear();
// Note: next_handshake_seq_no stays at 1 - the next ClientHello (with cookie)
// will have message_seq=1 per RFC 6347 §4.2.2.
// Note: peer_handshake_seq_no stays at 1 - the next message from server
// (ServerHello) will have message_seq=1 per RFC 6347 §4.2.2.
}
pub fn transcript_hash(&self, algorithm: HashAlgorithm, out: &mut Buf) {
let mut hash = self.crypto_context.create_hash(algorithm);
hash.update(&self.transcript);
hash.clone_and_finalize(out);
}
pub fn transcript(&self) -> &[u8] {
&self.transcript
}
pub fn set_cipher_suite(&mut self, cipher_suite: Dtls12CipherSuite) {
// Cache AEAD record parameters from the provider suite. The formula
// (explicit_nonce + tag) lives on the suite trait; Engine just stores
// the resolved values for hot-path access.
let provider_suite = self
.crypto_context
.provider()
.cipher_suites
.iter()
.find(|cs| cs.suite() == cipher_suite)
.expect("cipher suite must be in provider");
self.explicit_nonce_len = provider_suite.explicit_nonce_len();
self.min_protected_fragment_len = provider_suite.min_protected_fragment_len();
self.cipher_suite = Some(cipher_suite);
}
pub fn enable_peer_encryption(&mut self) -> Result<(), InternalError> {
debug!("Peer encryption enabled");
self.peer_encryption_enabled = true;
let maybe_index_epoch1 = self
.queue_rx
.iter()
.position(|i| i.records().iter().any(|r| r.record().sequence.epoch == 1));
let Some(index_epoch1) = maybe_index_epoch1 else {
return Ok(());
};
// Now decrypt all entries remaining.
let all = self.queue_rx.split_off(index_epoch1);
for incoming in all {
let unhandled = incoming.into_records().filter(|r| !r.is_handled());
for record in unhandled {
let buf = record.into_buffer();
self.parse_packet(&buf)?;
self.buffers_free.push(buf);
}
}
Ok(())
}
fn peer_iv(&self) -> Iv {
if self.is_client {
self.crypto_context
.get_server_write_iv()
.expect("Server write IV not available - keys not derived yet")
} else {
self.crypto_context
.get_client_write_iv()
.expect("Client write IV not available - keys not derived yet")
}
}
pub fn decryption_aad_and_nonce(&self, dtls: &DTLSRecord, buf: &[u8]) -> (Aad, Nonce) {
// DTLS 1.2 AEAD: AAD uses the plaintext length. Recover plaintext length
// from the record header by subtracting this suite's wire overhead.
let plaintext_len = dtls
.length
.saturating_sub(self.min_protected_fragment_len() as u16);
let aad = Aad::new_dtls12(dtls.content_type, dtls.sequence, plaintext_len);
let iv = self.peer_iv();
let seq64 = ((dtls.sequence.epoch as u64) << 48) | dtls.sequence.sequence_number;
let nonce = match self.explicit_nonce_len {
0 => Nonce::xor(iv.as_12_bytes(), seq64),
DTLSRecord::EXPLICIT_NONCE_LEN => Nonce::new(iv, dtls.nonce(buf)),
len => Nonce::new(iv, dtls.nonce_with_len(buf, len)),
};
(aad, nonce)
}
pub fn generate_verify_data(&mut self, is_client: bool) -> Result<[u8; 12], Error> {
let Some(suite) = self.cipher_suite() else {
return Err(Error::InvalidState(
crate::InvalidStateError::NoCipherSuiteSelected,
));
};
let algorithm = suite.hash_algorithm();
let mut handshake_hash = self.buffers_free.pop();
self.transcript_hash(algorithm, &mut handshake_hash);
let suite_hash = suite.hash_algorithm();
let mut out = self.buffers_free.pop();
let mut scratch = self.buffers_free.pop();
let verify_data_vec = self
.crypto_context()
.generate_verify_data(
&handshake_hash,
is_client,
suite_hash,
&mut out,
&mut scratch,
)
.map_err(Error::CryptoError)?;
if verify_data_vec.len() != 12 {
return Err(Error::CryptoError(
crate::CryptoError::InvalidVerifyDataLength,
));
}
let mut verify_data = [0u8; 12];
verify_data.copy_from_slice(&verify_data_vec);
self.buffers_free.push(handshake_hash);
self.buffers_free.push(out);
self.buffers_free.push(scratch);
Ok(verify_data)
}
}
impl RecordHandler for Engine {
fn classify_record(&mut self, record: Record) -> Result<Option<Record>, Error> {
let epoch = record.record().sequence.epoch;
if record.record().content_type == ContentType::ChangeCipherSpec
&& epoch == 0
&& self.peer_encryption_enabled
{
// DTLS 1.2 peers may retransmit their last handshake flight after
// we have already enabled peer encryption. A late plaintext CCS is
// no longer actionable; queuing it would leave an unhandled control
// record in queue_rx and prevent handled app-data records behind it
// from being purged.
self.push_buffer(record.into_buffer());
return Ok(None);
}
if record.record().content_type == ContentType::Handshake
&& epoch == 0
&& self.peer_encryption_enabled
&& record
.first_handshake()
.and_then(|handshake| handshake.dupe_triggers_resend())
.is_none()
{
// Stale plaintext handshakes must still be visible to
// insert_incoming_handshake when they can trigger final-flight
// retransmission. Other post-encryption epoch-0 handshakes are
// unauthenticated and no longer actionable.
self.push_buffer(record.into_buffer());
return Ok(None);
}
if record.record().content_type == ContentType::Alert {
if epoch == 0 {
if self.peer_encryption_enabled {
// Post-handshake: epoch 0 alerts are unauthenticated, discard.
self.push_buffer(record.into_buffer());
return Ok(None);
}
let fatal_description = {
let fragment = record.record().fragment(record.buffer());
(fragment.len() >= 2 && fragment[0] == 2).then(|| fragment[1])
};
self.push_buffer(record.into_buffer());
if let Some(description) = fatal_description {
return Err(Error::SecurityError(crate::SecurityError::FatalAlert {
description,
}));
}
return Ok(None);
}
if !self.peer_encryption_enabled {
// Epoch >= 1 before peer encryption is enabled must stay queued
// for re-parsing after enable_peer_encryption().
return Ok(Some(record));
}
let alert = {
let fragment = record.record().fragment(record.buffer());
(fragment.len() >= 2).then(|| (fragment[0], fragment[1]))
};
self.push_buffer(record.into_buffer());
if let Some((level, description)) = alert {
if description == 0 {
self.close_notify_received = true;
return Ok(None);
}
if level == 2 {
return Err(Error::SecurityError(crate::SecurityError::FatalAlert {
description,
}));
}
}
return Ok(None);
}
if self.close_notify_received
&& record.record().content_type == ContentType::ApplicationData
{
self.push_buffer(record.into_buffer());
return Ok(None);
}
Ok(Some(record))
}
fn is_peer_encryption_enabled(&self) -> bool {
self.peer_encryption_enabled
}
fn replay_check(&self, seq: Sequence) -> bool {
// Only epoch 1 (encrypted) records reach here; epoch 0 records are
// returned early by the DTLS 1.2 incoming parser.
self.replay.check(seq.sequence_number)
}
fn replay_update(&mut self, seq: Sequence) {
self.replay.update(seq.sequence_number);
}
fn note_decrypted_record(&mut self, content_type: ContentType) {
// A decrypted (so authenticated) application-data record proves the peer
// is past its handshake, which means it received our final flight. Once
// that's known, a stale plaintext handshake (unauthenticated, replayable)
// must no longer drive a courtesy flight retransmission. The client
// confirms separately at its own completion (flight_stop_resend_timers).
if content_type == ContentType::ApplicationData {
self.peer_handshake_confirmed = true;
}
}
fn decryption_aad_and_nonce(&self, dtls: &DTLSRecord, buf: &[u8]) -> (Aad, Nonce) {
Engine::decryption_aad_and_nonce(self, dtls, buf)
}
fn explicit_nonce_len(&self) -> usize {
self.explicit_nonce_len
}
fn min_protected_fragment_len(&self) -> usize {
self.min_protected_fragment_len
}
fn decrypt_data(
&mut self,
ciphertext: &mut TmpBuf,
aad: Aad,
nonce: Nonce,
) -> Result<(), Error> {
Engine::decrypt_data(self, ciphertext, aad, nonce)
}
fn can_discard_bad_protected_record(&self) -> bool {
self.release_app_data
}
}