rtci-dtls 0.3.0

Sans-I/O DTLS 1.2 and 1.3 (RFC 6347, RFC 9147) with DTLS-SRTP key export, for WebRTC
Documentation
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
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
//! A DTLS 1.3 association (RFC 9147): the record layer with its epochs, the reliability of
//! the handshake (flights, retransmission, ACKs: sections 5.7, 5.8 and 7), key updates
//! (section 8), connection id updates (section 9) and alerts. The handshake messages
//! themselves are in [`handshake`](super::handshake).

use super::hello::{ClientOffer, RetryCookie, RetryKeys};
use super::key_schedule::{KeySchedule, Transcript};
use super::messages::*;
use super::record::*;
use super::suite::Suite13;
use crate::config::HandshakeConfig;
use crate::conn::{
    FALLBACK_HANDSHAKE_MTU, MAX_PRE_HANDSHAKE_APPLICATION_DATA_BYTES,
    MAX_PRE_HANDSHAKE_APPLICATION_DATA_PACKETS, RETRANSMITS_BEFORE_MTU_BACKOFF,
};
use crate::extension::extension_use_srtp::SrtpProtectionProfile;
use crate::signature_hash_algorithm::SignatureHashAlgorithm;
use bytes::BytesMut;
use crypto::{RTCCryptoProvider, SecretVec};
use log::{debug, warn};
use shared::error::{Error, Result};
use shared::replay_detector::{ReplayDetector, SlidingWindowDetector};
use std::collections::{BTreeMap, VecDeque};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// The epoch of 0-RTT data (RFC 9147 section 6.1).
pub(crate) const EPOCH_EARLY: u64 = 1;
/// The epoch of the handshake traffic keys (RFC 9147 section 6.1).
pub(crate) const EPOCH_HANDSHAKE: u64 = 2;
/// The epoch of the first application traffic keys.
pub(crate) const EPOCH_APPLICATION: u64 = 3;
/// The highest epoch a sender moves to (RFC 9147 section 8).
pub(crate) const MAX_SEND_EPOCH: u64 = (1 << 48) - 1;
/// The most records one transmission of a flight holds (RFC 9147 section 5.8.3).
pub(crate) const MAX_RECORDS_PER_TRANSMISSION: usize = 10;
/// How long the keys of the handshake epoch are kept after the handshake, so that a
/// retransmission of the peer's last flight can be read and answered: twice the maximum
/// segment lifetime of RFC 793 (RFC 9147 section 5.8.1).
pub(crate) const HANDSHAKE_KEYS_LINGER: Duration = Duration::from_secs(240);
/// The least the retransmission timer is set to from a measured round trip (RFC 9147 section
/// 5.8.2 sets none; a handshake over loopback would otherwise retransmit at once).
pub(crate) const MINIMUM_MEASURED_RETRANSMIT_INTERVAL: Duration = Duration::from_millis(100);
/// The longest handshake message taken from plaintext records: the hellos.
const MAX_PLAINTEXT_MESSAGE: usize = 16 * 1024;
/// The longest handshake message taken at all (a certificate chain).
const MAX_MESSAGE: usize = 256 * 1024;
/// How far ahead of the next expected message_seq a message is buffered.
const MESSAGE_SEQ_WINDOW: u16 = 8;
/// The most bytes of handshake messages under reassembly at once.
const MAX_REASSEMBLY_BYTES: usize = 512 * 1024;
/// The most records an outgoing flight remembers having been sent in, for matching ACKs.
const MAX_SENT_RECORDS: usize = 512;
/// The most record numbers kept to acknowledge.
const MAX_ACK_RECORDS: usize = 64;
/// The most protected records held because their keys are not there yet.
const MAX_EARLY_RECORDS: usize = 16;
/// The most RequestConnectionId messages an association answers (RFC 9147 section 9): one
/// more is an excessive number, and ends it with too_many_cids_requested.
pub(crate) const MAX_CONNECTION_ID_REQUESTS: u32 = 32;
/// How many times the current timer value a quiet period lasts before the retransmission
/// timer goes back to its initial value (RFC 9147 section 5.8.2: no less than 10).
const IDLE_TIMER_RESET_FACTOR: u32 = 10;
/// The most spare connection ids kept from the peer's NewConnectionId messages.
const MAX_SPARE_CONNECTION_IDS: usize = 8;

/// Where the handshake stands: what this side waits for.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum HandshakeState {
    ClientWaitServerHello,
    ClientWaitEncryptedExtensions,
    ClientWaitCertificateOrRequest,
    ClientWaitCertificate,
    ClientWaitCertificateVerify,
    ClientWaitFinished,
    /// The client has sent its certificate and Finished and waits to learn that the server
    /// took them: the ACK of that flight, or anything under the server's application keys.
    ClientWaitConfirmation,
    ServerWaitClientHello,
    ServerWaitCertificate,
    ServerWaitCertificateVerify,
    ServerWaitFinished,
    Established,
}

/// A set of byte ranges, kept sorted and merged.
#[derive(Clone, Debug, Default)]
pub(crate) struct Ranges(Vec<(usize, usize)>);

impl Ranges {
    pub(crate) fn add(&mut self, start: usize, end: usize) {
        if start >= end {
            return;
        }
        self.0.push((start, end));
        self.0.sort_unstable();
        let mut merged: Vec<(usize, usize)> = Vec::with_capacity(self.0.len());
        for (start, end) in self.0.drain(..) {
            match merged.last_mut() {
                Some(last) if start <= last.1 => last.1 = last.1.max(end),
                _ => merged.push((start, end)),
            }
        }
        self.0 = merged;
    }

    pub(crate) fn covers(&self, length: usize) -> bool {
        length == 0 || self.0 == [(0, length)]
    }

    /// The parts of `0..length` not in the set.
    pub(crate) fn gaps(&self, length: usize) -> Vec<(usize, usize)> {
        let mut gaps = vec![];
        let mut at = 0;
        for (start, end) in &self.0 {
            if *start > at {
                gaps.push((at, (*start).min(length)));
            }
            at = at.max(*end);
        }
        if at < length {
            gaps.push((at, length));
        }
        gaps
    }

    pub(crate) fn contains(&self, start: usize, end: usize) -> bool {
        self.0.iter().any(|(s, e)| *s <= start && end <= *e)
    }
}

/// A handshake message being put together from its fragments (RFC 9147 section 5.5).
struct Reassembly {
    handshake_type: u8,
    data: Vec<u8>,
    received: Ranges,
}

/// One message of an outgoing flight.
pub(crate) struct OutMessage {
    pub(crate) message_seq: u16,
    /// The epoch it is sent in, and sent again in (RFC 9147 section 4.2.1).
    pub(crate) epoch: u64,
    pub(crate) handshake_type: u8,
    pub(crate) body: Vec<u8>,
    /// The bytes of the body the peer has acknowledged.
    acknowledged: Ranges,
}

/// What an outgoing flight is, which decides what ends its retransmission.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum FlightKind {
    /// A flight of the handshake that the peer's next flight answers.
    Handshake,
    /// The client's last flight, which only an ACK answers (RFC 9147 section 7.1).
    ClientFinal,
    KeyUpdate,
    NewConnectionId,
    RequestConnectionId,
    NewSessionTicket,
    /// A server's post-handshake CertificateRequest.
    CertificateRequest,
    /// A client's answer to one: Certificate, CertificateVerify, Finished.
    Authentication,
    /// A server's HelloRetryRequest. It has no timer (the client's ClientHello is what is
    /// retransmitted, RFC 9147 section 5.8.1) and nothing acknowledges it; it is sent again
    /// each time the ClientHello it answered is repeated, as often as it is (section 5.1: a
    /// ClientHello without a valid cookie is answered with a HelloRetryRequest every time),
    /// until the second ClientHello answers it.
    HelloRetryRequest,
}

/// Messages sent together and retransmitted until acknowledged (RFC 9147 section 5.7).
pub(crate) struct Flight {
    pub(crate) kind: FlightKind,
    pub(crate) messages: Vec<OutMessage>,
    /// Which bytes of which message went out in which record, newest last.
    sent: VecDeque<(RecordNumber, usize, usize, usize)>,
    /// Where the next transmission starts when one transmission cannot hold the flight.
    cursor: usize,
    timer: Option<Instant>,
    /// A transmission was made and the timer is to be set from the next `now`.
    arm: bool,
    /// When the flight first went out, if it has not been sent again since.
    first_sent: Option<Instant>,
    retransmissions: usize,
}

impl Flight {
    pub(crate) fn new(kind: FlightKind) -> Self {
        Self {
            kind,
            messages: vec![],
            sent: VecDeque::new(),
            cursor: 0,
            timer: None,
            arm: false,
            first_sent: None,
            retransmissions: 0,
        }
    }

    pub(crate) fn push(&mut self, message_seq: u16, epoch: u64, handshake_type: u8, body: Vec<u8>) {
        self.messages.push(OutMessage {
            message_seq,
            epoch,
            handshake_type,
            body,
            acknowledged: Ranges::default(),
        });
    }

    fn acknowledged(&self) -> bool {
        self.messages
            .iter()
            .all(|message| message.acknowledged.covers(message.body.len()))
    }
}

/// The keys this side protects records of one epoch with.
struct WriteEpoch {
    epoch: u64,
    keys: RecordKeys,
    next_sequence: u64,
}

/// The keys this side reads records of one epoch with.
struct ReadEpoch {
    epoch: u64,
    keys: RecordKeys,
    replay: SlidingWindowDetector,
    /// One more than the highest sequence number deprotected (RFC 9147 section 4.2.2).
    next_expected: u64,
    /// How many records failed authentication under these keys (section 4.5.3).
    failures: u64,
    /// The traffic secret the keys were made from: the base key of the Finished of a
    /// post-handshake authentication that arrives in this epoch (RFC 8446 section 4.4).
    secret: SecretVec,
}

/// What became of a record handed to the handshake layer.
#[derive(Default)]
struct RecordOutcome {
    /// A fragment of it was processed or buffered: the record may be acknowledged.
    kept: bool,
    /// It repeated a message already processed: the peer is retransmitting.
    repeated: bool,
    /// It held something other than the next expected piece.
    out_of_order: bool,
}

/// A DTLS 1.3 association.
pub(crate) struct Conn13 {
    pub(crate) cfg: Arc<HandshakeConfig>,
    pub(crate) provider: Arc<dyn RTCCryptoProvider>,
    pub(crate) is_client: bool,

    // What was negotiated.
    pub(crate) suite: Option<Suite13>,
    pub(crate) schedule: Option<KeySchedule>,
    pub(crate) transcript: Transcript,
    pub(crate) srtp_profile: SrtpProtectionProfile,
    /// The application protocol ALPN selected (RFC 7301), if either side used it.
    pub(crate) application_protocol: Option<String>,
    pub(crate) peer_certificates: Vec<Vec<u8>>,
    pub(crate) server_name: String,
    pub(crate) client_random: Vec<u8>,
    pub(crate) exporter_secret: Option<SecretVec>,
    /// The PSK the key schedule starts from, when the handshake uses one (RFC 8446 section
    /// 2.2), and whether it came without an (EC)DHE exchange (psk_ke).
    pub(crate) psk: Option<SecretVec>,
    pub(crate) psk_without_key_exchange: bool,
    /// The handshake was made with a PSK, so neither side sent a certificate.
    pub(crate) resumed: bool,
    /// That PSK is an external one (RFC 8446 section 2.2), not a ticket of an earlier
    /// handshake: it never carries certificates, and authenticates by itself.
    pub(crate) external_psk: bool,
    /// `resumption_master_secret`, kept while tickets are to be made or taken from it.
    pub(crate) resumption_secret: Option<SecretVec>,
    /// Server: the PSK key exchange modes the client offered; tickets are sent only to a
    /// client that offered one this server accepts (RFC 8446 section 4.2.9).
    pub(crate) client_psk_modes: Vec<u8>,
    /// How many tickets this association has issued: their nonces.
    pub(crate) tickets_issued: u8,
    /// The latest `now` this association was given (it reads no clock).
    pub(crate) clock: Option<Instant>,
    /// Server: the post-handshake CertificateRequests not yet answered, by context, each with
    /// the transcript its answer continues (RFC 8446 section 4.6.2).
    pub(crate) pending_certificate_requests: Vec<(Vec<u8>, Transcript)>,
    /// Server: the answer to one of them, while it comes in.
    pub(crate) client_authentication: Option<super::handshake::ClientAuthentication>,
    /// Server: the client's certificates from its latest post-handshake answer, until the
    /// association's state has taken them.
    pub(crate) post_handshake_peer_certificates: Option<Vec<Vec<u8>>>,
    /// Server: how many post-handshake answers have been verified.
    pub(crate) post_handshake_authentications: u64,
    /// Whether 0-RTT data was accepted (RFC 8446 section 4.2.10); `None` when none was
    /// offered.
    pub(crate) early_data_accepted: Option<bool>,
    /// The PSK the server selected, by its index in the client's list.
    pub(crate) psk_index: Option<u16>,
    /// Server: the 0-RTT data received, until the endpoint takes it.
    pub(crate) early_data: VecDeque<BytesMut>,
    /// Server: how much 0-RTT data it takes, and has taken.
    pub(crate) early_data_limit: usize,
    early_data_received: usize,

    // The record layer.
    plaintext_send_sequence: u64,
    plaintext_replay: SlidingWindowDetector,
    write: Vec<WriteEpoch>,
    read: Vec<ReadEpoch>,
    /// The application traffic secrets the current keys of each direction came from, from
    /// which a KeyUpdate derives the next.
    local_traffic_secret: Option<SecretVec>,
    remote_traffic_secret: Option<SecretVec>,
    /// The connection id the peer puts in records to this side (empty: none).
    pub(crate) local_connection_id: Vec<u8>,
    /// The connection id this side puts in records to the peer (empty: none).
    pub(crate) remote_connection_id: Vec<u8>,
    /// Every connection id this side has handed out and still takes records under: the one
    /// of the handshake and those of its NewConnectionId messages.
    accepted_connection_ids: VecDeque<Vec<u8>>,
    /// Both sides sent the connection_id extension.
    pub(crate) connection_ids_negotiated: bool,
    /// The last datagram read held a record that authenticated and was newer than any
    /// before it: what RFC 9146 section 6 asks before a peer's address is updated.
    pub(crate) newest_record_read: bool,
    spare_connection_ids: VecDeque<Vec<u8>>,
    early_records: VecDeque<Vec<u8>>,
    pub(crate) authenticated_records: u64,
    /// The peer has shown it holds the handshake keys, so nothing unprotected is its word
    /// any more.
    peer_uses_keys: bool,

    // Receiving the handshake.
    pub(crate) state: HandshakeState,
    next_receive_seq: u16,
    reassembly: BTreeMap<u16, Reassembly>,

    // Sending the handshake.
    pub(crate) next_send_seq: u16,
    pub(crate) flight: Option<Flight>,
    pub(crate) post_handshake: Vec<Flight>,
    retransmit_interval: Duration,
    handshake_mtu: usize,
    handshake_started: Option<Instant>,
    /// The peer repeated a flight this side has answered: the answer is sent again.
    peer_retransmitted: bool,
    /// Application data has arrived ahead of the peer's Finished, and the flight has been
    /// sent again for it.
    early_data_seen: bool,
    /// A flight was answered or acknowledged: (when it first went out, if never again).
    answered: Option<Option<Instant>>,

    // Acknowledging.
    /// The records of the peer's current flight that were processed or buffered.
    received_records: Vec<RecordNumber>,
    ack_now: bool,
    ack_delayed: bool,
    ack_timer: Option<Instant>,

    // Application data and the association's end.
    incoming: VecDeque<BytesMut>,
    held_application_data: VecDeque<BytesMut>,
    queued_writes: VecDeque<Vec<u8>>,
    pending_records: Vec<Vec<u8>>,
    datagrams: VecDeque<BytesMut>,
    pub(crate) established: bool,
    established_at: Option<Instant>,
    handshake_keys_deadline: Option<Instant>,
    pub(crate) failed: bool,
    pub(crate) closed: bool,
    pub(crate) closed_by_peer: bool,
    fatal_alert_sent: Option<(u8, usize)>,
    /// The error alert the peer ended the association with, if it did.
    fatal_alert_received: Option<u8>,
    /// A KeyUpdate of this side is waiting for its ACK, and whether another was asked for
    /// meanwhile.
    key_update_requested_by_peer: bool,
    connection_id_request_outstanding: bool,
    connection_id_requests_received: u32,
    /// When a flight was last sent, answered or acknowledged.
    last_flight_activity: Option<Instant>,
    flight_activity: bool,
    /// How many records were discarded as invalid: malformed, or failing authentication.
    pub(crate) invalid_records: u64,

    // The client's hello, until the server has answered it.
    pub(crate) offer: Option<ClientOffer>,
    pub(crate) client_hello: Vec<u8>,
    pub(crate) hello_retry_seen: bool,
    pub(crate) offered_extensions: Vec<u16>,
    pub(crate) certificate_requested: Option<(Vec<u16>, Vec<super::certificate_checks::OidFilter>)>,
    pub(crate) peer_signature_scheme: Option<SignatureHashAlgorithm>,
    /// The server's: the secret of the cookies of a cookie exchange it runs itself, and the
    /// cookie a caller has already verified for it.
    pub(crate) cookie_secret: Option<SecretVec>,
    /// Server: the endpoint's cookie secrets and the client's address, when the endpoint
    /// left the cookie exchange to this association (see [`RetryKeys`]).
    pub(crate) retry_keys: Option<RetryKeys>,
    pub(crate) verified_retry: Option<(Vec<u8>, RetryCookie)>,
    pub(crate) address_verified: bool,
    pub(crate) client_certificate_expected: bool,
    /// Limits of the AEAD, which a test lowers to reach them.
    pub(crate) confidentiality_limit: Option<u64>,
    pub(crate) integrity_limit: Option<u64>,
    /// The epoch of the record that completed the handshake message being processed.
    pub(crate) message_epoch: u64,
}

impl Drop for Conn13 {
    fn drop(&mut self) {
        self.forget_secrets();
    }
}

/// How many bytes a protected DTLS 1.3 record, as this stack writes it (the full unified
/// header of RFC 9147 section 4 with a 16-bit sequence number and the length, and
/// `connection_id_len` bytes of the peer's connection id), adds to its content under `suite`:
/// the header, the inner content type and the tag.
pub(crate) fn protected_record_overhead(suite: Suite13, connection_id_len: usize) -> usize {
    HeaderForm::FULL.header_len(connection_id_len) + 1 + suite.tag_len()
}

impl Conn13 {
    pub(crate) fn new(cfg: Arc<HandshakeConfig>, is_client: bool, state: HandshakeState) -> Self {
        let provider = cfg.provider().clone();
        Self {
            provider,
            is_client,
            suite: None,
            schedule: None,
            transcript: Transcript::default(),
            srtp_profile: SrtpProtectionProfile::Unsupported,
            application_protocol: None,
            peer_certificates: vec![],
            server_name: String::new(),
            client_random: vec![],
            exporter_secret: None,
            psk: None,
            psk_without_key_exchange: false,
            resumed: false,
            external_psk: false,
            resumption_secret: None,
            client_psk_modes: vec![],
            tickets_issued: 0,
            clock: None,
            pending_certificate_requests: vec![],
            client_authentication: None,
            post_handshake_peer_certificates: None,
            post_handshake_authentications: 0,
            early_data_accepted: None,
            psk_index: None,
            early_data: VecDeque::new(),
            early_data_limit: 0,
            early_data_received: 0,
            plaintext_send_sequence: 0,
            plaintext_replay: SlidingWindowDetector::new(
                cfg.replay_protection_window,
                MAX_SEQUENCE_NUMBER,
            ),
            write: vec![],
            read: vec![],
            local_traffic_secret: None,
            remote_traffic_secret: None,
            local_connection_id: vec![],
            remote_connection_id: vec![],
            accepted_connection_ids: VecDeque::new(),
            connection_ids_negotiated: false,
            newest_record_read: false,
            spare_connection_ids: VecDeque::new(),
            early_records: VecDeque::new(),
            authenticated_records: 0,
            peer_uses_keys: false,
            state,
            next_receive_seq: 0,
            reassembly: BTreeMap::new(),
            next_send_seq: 0,
            flight: None,
            post_handshake: vec![],
            retransmit_interval: cfg.retransmit_interval,
            handshake_mtu: cfg.maximum_transmission_unit,
            handshake_started: None,
            peer_retransmitted: false,
            early_data_seen: false,
            answered: None,
            received_records: vec![],
            ack_now: false,
            ack_delayed: false,
            ack_timer: None,
            incoming: VecDeque::new(),
            held_application_data: VecDeque::new(),
            queued_writes: VecDeque::new(),
            pending_records: vec![],
            datagrams: VecDeque::new(),
            established: false,
            established_at: None,
            handshake_keys_deadline: None,
            failed: false,
            closed: false,
            closed_by_peer: false,
            fatal_alert_sent: None,
            fatal_alert_received: None,
            key_update_requested_by_peer: false,
            connection_id_request_outstanding: false,
            connection_id_requests_received: 0,
            last_flight_activity: None,
            flight_activity: false,
            invalid_records: 0,
            offer: None,
            client_hello: vec![],
            hello_retry_seen: false,
            offered_extensions: vec![],
            certificate_requested: None,
            peer_signature_scheme: None,
            cookie_secret: None,
            retry_keys: None,
            verified_retry: None,
            address_verified: false,
            client_certificate_expected: false,
            confidentiality_limit: None,
            integrity_limit: None,
            message_epoch: 0,
            cfg,
        }
    }

    pub(crate) fn set_next_receive_seq(&mut self, message_seq: u16) {
        self.next_receive_seq = message_seq;
    }

    pub(crate) fn set_plaintext_send_sequence(&mut self, sequence: u64) {
        self.plaintext_send_sequence = sequence;
    }

    /// The sequence number of the next plaintext (epoch 0) record this side sends.
    pub(crate) fn plaintext_send_sequence(&self) -> u64 {
        self.plaintext_send_sequence
    }

    pub(crate) fn suite(&self) -> Result<Suite13> {
        self.suite.ok_or(Error::ErrCipherSuiteUnset)
    }

    // ------------------------------------------------------------------------------------
    // Keys and epochs.
    // ------------------------------------------------------------------------------------

    fn record_keys(&self, secret: &[u8]) -> Result<RecordKeys> {
        let schedule = self.schedule.as_ref().ok_or(Error::ErrCipherSuiteUnset)?;
        let keys = schedule.traffic_keys(self.provider.crypto(), secret)?;
        RecordKeys::new(self.provider.crypto(), self.suite()?, &keys)
    }

    /// Starts protecting records of `epoch` with the keys of `secret`.
    pub(crate) fn install_write_epoch(&mut self, epoch: u64, secret: &[u8]) -> Result<()> {
        let keys = self.record_keys(secret)?;
        self.write.push(WriteEpoch {
            epoch,
            keys,
            next_sequence: 0,
        });
        Ok(())
    }

    /// Starts reading records of `epoch` with the keys of `secret`.
    pub(crate) fn install_read_epoch(&mut self, epoch: u64, secret: &[u8]) -> Result<()> {
        let keys = self.record_keys(secret)?;
        self.read.push(ReadEpoch {
            epoch,
            keys,
            replay: SlidingWindowDetector::new(
                self.cfg.replay_protection_window,
                MAX_SEQUENCE_NUMBER,
            ),
            next_expected: 0,
            failures: 0,
            secret: SecretVec::new(secret.to_vec()),
        });
        Ok(())
    }

    /// The peer's traffic secret of read epoch `epoch`, while this side still reads it.
    pub(crate) fn read_epoch_secret(&self, epoch: u64) -> Option<&SecretVec> {
        self.read
            .iter()
            .find(|read| read.epoch == epoch)
            .map(|read| &read.secret)
    }

    /// This side's current application traffic secret (`..._application_traffic_secret_N`).
    pub(crate) fn local_application_secret(&self) -> Option<&SecretVec> {
        self.local_traffic_secret.as_ref()
    }

    /// The peer's current application traffic secret.
    pub(crate) fn remote_application_secret(&self) -> Option<&SecretVec> {
        self.remote_traffic_secret.as_ref()
    }

    pub(crate) fn set_application_secrets(&mut self, local: SecretVec, remote: SecretVec) {
        self.local_traffic_secret = Some(local);
        self.remote_traffic_secret = Some(remote);
    }

    /// The highest epoch this side can send in: what ACKs, alerts, application data and
    /// post-handshake messages go out in (RFC 9147 section 7).
    fn send_epoch(&self) -> u64 {
        self.write
            .iter()
            .map(|write| write.epoch)
            .max()
            .unwrap_or(0)
    }

    fn drop_write_epoch(&mut self, epoch: u64) {
        self.write.retain(|write| write.epoch != epoch);
    }

    /// Whether this side holds keys to read records of `epoch`.
    #[cfg(test)]
    pub(crate) fn has_read_epoch(&self, epoch: u64) -> bool {
        self.read.iter().any(|read| read.epoch == epoch)
    }

    fn drop_read_epoch(&mut self, epoch: u64) {
        self.read.retain(|read| read.epoch != epoch);
    }

    /// Drops the write keys of application epochs older than the current one that no
    /// unacknowledged message still has to be sent again in.
    fn prune_write_epochs(&mut self) {
        let current = self.send_epoch();
        let in_use: Vec<u64> = self
            .post_handshake
            .iter()
            .chain(self.flight.iter())
            .flat_map(|flight| flight.messages.iter().map(|message| message.epoch))
            .collect();
        self.write.retain(|write| {
            write.epoch == current
                || write.epoch < EPOCH_APPLICATION
                || in_use.contains(&write.epoch)
        });
    }

    pub(crate) fn forget_secrets(&mut self) {
        if let Some(schedule) = &mut self.schedule {
            schedule.forget();
        }
        self.exporter_secret = None;
        self.psk = None;
        self.resumption_secret = None;
        self.local_traffic_secret = None;
        self.remote_traffic_secret = None;
        self.cookie_secret = None;
        self.retry_keys = None;
        self.offer = None;
    }

    // ------------------------------------------------------------------------------------
    // Writing records.
    // ------------------------------------------------------------------------------------

    /// How many bytes a protected record adds to its content, once the suite is known.
    pub(crate) fn protected_record_overhead(&self) -> Option<usize> {
        self.suite?;
        Some(self.record_overhead(1))
    }

    /// How many bytes a record of `epoch` adds to its content.
    fn record_overhead(&self, epoch: u64) -> usize {
        if epoch == 0 {
            PLAINTEXT_HEADER_LEN
        } else {
            match self.suite {
                Some(suite) => protected_record_overhead(suite, self.remote_connection_id.len()),
                None => HeaderForm::FULL.header_len(self.remote_connection_id.len()) + 1 + 16,
            }
        }
    }

    /// Writes one record of `epoch` and queues it. Returns its record number.
    fn write_record(
        &mut self,
        epoch: u64,
        content_type: u8,
        content: &[u8],
    ) -> Result<RecordNumber> {
        if epoch == 0 {
            let sequence = self.plaintext_send_sequence;
            // RFC 9147 section 4: a plaintext record's sequence number stays within 48 bits.
            if sequence > MAX_SEQUENCE_NUMBER {
                return Err(Error::ErrSequenceNumberOverflow);
            }
            self.plaintext_send_sequence += 1;
            self.pending_records
                .push(plaintext_record(content_type, sequence, content));
            return Ok(RecordNumber { epoch, sequence });
        }
        let limit = self
            .confidentiality_limit
            .or(self.suite.map(|suite| suite.confidentiality_limit()))
            .unwrap_or(u64::MAX)
            .min(MAX_SEQUENCE_NUMBER);
        let connection_id = self.remote_connection_id.clone();
        let write = self
            .write
            .iter_mut()
            .find(|write| write.epoch == epoch)
            .ok_or(Error::ErrCipherSuiteUnset)?;
        // RFC 9147 sections 4.2.1 and 4.5.3: no more records under one key than its AEAD
        // allows, and never a sequence number that wraps.
        if write.next_sequence >= limit {
            return Err(Error::ErrSequenceNumberOverflow);
        }
        let number = RecordNumber {
            epoch,
            sequence: write.next_sequence,
        };
        write.next_sequence += 1;
        let record = write.keys.seal(
            number,
            &connection_id,
            HeaderForm::FULL,
            content_type,
            content,
            0,
        )?;
        self.pending_records.push(record);
        Ok(number)
    }

    /// One record of `content_type` holding `content`, protected under this side's current
    /// keys and not queued: for a fuzz harness to hand to the peer.
    #[cfg(any(fuzzing, test))]
    pub(crate) fn seal_for_fuzzing(&mut self, content_type: u8, content: &[u8]) -> Option<Vec<u8>> {
        let epoch = self.send_epoch();
        if epoch == 0 {
            return None;
        }
        self.write_record(epoch, content_type, content).ok()?;
        self.pending_records.pop()
    }

    /// A protected record whose content is longer than RFC 8446 section 5.1 allows, under
    /// this side's current keys and not queued.
    #[cfg(test)]
    pub(crate) fn seal_oversized_for_test(&mut self, content_type: u8, content: &[u8]) -> Vec<u8> {
        let epoch = self.send_epoch();
        let connection_id_len = self.remote_connection_id.len();
        assert_eq!(
            connection_id_len, 0,
            "written for associations without connection ids"
        );
        let write = self
            .write
            .iter_mut()
            .find(|write| write.epoch == epoch)
            .expect("keys to write with");
        let number = RecordNumber {
            epoch,
            sequence: write.next_sequence,
        };
        write.next_sequence += 1;
        write
            .keys
            .seal_oversized(number, content_type, content)
            .expect("the record is sealed")
    }

    /// The message_seq of the next handshake message this side expects.
    #[cfg(test)]
    pub(crate) fn next_receive_seq_for_test(&self) -> u16 {
        self.next_receive_seq
    }

    /// The connection ids this side takes records under, in the order they were handed out:
    /// the handshake's first, then those of its NewConnectionId messages (empty until the
    /// first NewConnectionId).
    #[cfg(test)]
    pub(crate) fn accepted_connection_ids_for_test(&self) -> Vec<Vec<u8>> {
        self.accepted_connection_ids.iter().cloned().collect()
    }

    /// The records an ACK of this side's would list now (RFC 9147 section 7).
    #[cfg(test)]
    pub(crate) fn acknowledgeable_records(&self) -> &[RecordNumber] {
        &self.received_records
    }

    /// Moves this side's current sending epoch to `epoch` (same keys): for the tests of
    /// what happens at the last epoch, 2^48-1, which no test can reach by key updates.
    #[cfg(test)]
    pub(crate) fn set_send_epoch_for_test(&mut self, epoch: u64) {
        let current = self.send_epoch();
        if let Some(write) = self.write.iter_mut().find(|write| write.epoch == current) {
            write.epoch = epoch;
        }
    }

    /// Renumbers this side's newest reading epoch to `epoch` (same keys): with
    /// [`Self::set_send_epoch_for_test`] on the peer, for the tests of what a receiver does
    /// at the last epoch (RFC 9147 section 8).
    #[cfg(test)]
    pub(crate) fn set_read_epoch_for_test(&mut self, epoch: u64) {
        let newest = self.read.iter().map(|read| read.epoch).max();
        if let Some(read) = self.read.iter_mut().find(|read| Some(read.epoch) == newest) {
            read.epoch = epoch;
        }
    }

    /// Packs the queued records into datagrams no larger than the MTU, in order.
    fn flush(&mut self) {
        let mtu = self.cfg.maximum_transmission_unit;
        let mut datagram = BytesMut::new();
        for record in self.pending_records.drain(..) {
            if !datagram.is_empty() && datagram.len() + record.len() > mtu {
                self.datagrams.push_back(std::mem::take(&mut datagram));
            }
            datagram.extend_from_slice(&record);
        }
        if !datagram.is_empty() {
            self.datagrams.push_back(datagram);
        }
    }

    pub(crate) fn poll_datagram(&mut self) -> Option<BytesMut> {
        self.flush();
        self.datagrams.pop_front()
    }

    pub(crate) fn poll_application_data(&mut self) -> Option<BytesMut> {
        if self.established {
            self.incoming.pop_front()
        } else {
            None
        }
    }

    /// Queues application data: one record, in the current epoch, once the handshake is
    /// complete.
    pub(crate) fn write(&mut self, data: &[u8]) -> Result<()> {
        if self.closed || self.failed {
            return Err(Error::ErrConnClosed);
        }
        if data.len() > MAX_PLAINTEXT {
            return Err(Error::ErrRecordOverflow);
        }
        if !self.established {
            self.queued_writes.push_back(data.to_vec());
            return Ok(());
        }
        self.write_application_data(data)
    }

    fn write_application_data(&mut self, data: &[u8]) -> Result<()> {
        let epoch = self.send_epoch();
        self.write_record(epoch, CONTENT_TYPE_APPLICATION_DATA, data)?;
        self.rekey_before_the_limit();
        Ok(())
    }

    /// RFC 9147 section 4.5.3: a key update is started before the keys reach the number of
    /// records their AEAD allows.
    fn rekey_before_the_limit(&mut self) {
        let Some(suite) = self.suite else { return };
        let limit = self
            .confidentiality_limit
            .unwrap_or(suite.confidentiality_limit());
        let epoch = self.send_epoch();
        let used = self
            .write
            .iter()
            .find(|write| write.epoch == epoch)
            .map_or(0, |write| write.next_sequence);
        if used >= limit - limit / 8 && !self.key_update_outstanding() {
            if epoch >= MAX_SEND_EPOCH {
                // RFC 9147 sections 4.2.1 and 6.1: the epoch would have to wrap. The
                // association ends; a new one is the way on.
                warn!("dtls 1.3: the last epoch is used up; closing the association");
                self.close();
                return;
            }
            let _ = self.send_key_update(false);
        }
    }

    // ------------------------------------------------------------------------------------
    // Flights: sending, retransmitting, acknowledging (RFC 9147 sections 5.8 and 7).
    // ------------------------------------------------------------------------------------

    /// The pieces of `flight` still to be acknowledged, each small enough for one record of
    /// a datagram of the handshake MTU: (message, offset, length).
    fn unacknowledged_pieces(&self, flight: &Flight) -> Vec<(usize, usize, usize)> {
        let mut pieces = vec![];
        for (index, message) in flight.messages.iter().enumerate() {
            let room = self
                .handshake_mtu
                .saturating_sub(self.record_overhead(message.epoch) + HANDSHAKE_HEADER_LEN)
                .max(1);
            if message.body.is_empty() && !message.acknowledged.covers(1) {
                pieces.push((index, 0, 0));
            }
            for (mut start, end) in message.acknowledged.gaps(message.body.len()) {
                while start < end {
                    let length = room.min(end - start);
                    pieces.push((index, start, length));
                    start += length;
                }
            }
        }
        pieces
    }

    /// Sends what `flight` still has unacknowledged, at most ten records of it (RFC 9147
    /// section 5.8.3): a later transmission goes on where this one stopped.
    fn transmit(&mut self, flight: &mut Flight) -> Result<()> {
        let pieces = self.unacknowledged_pieces(flight);
        if pieces.is_empty() {
            return Ok(());
        }
        let start = if pieces.len() > MAX_RECORDS_PER_TRANSMISSION {
            flight.cursor % pieces.len()
        } else {
            0
        };
        let count = pieces.len().min(MAX_RECORDS_PER_TRANSMISSION);
        for step in 0..count {
            let (index, offset, length) = pieces[(start + step) % pieces.len()];
            let message = &flight.messages[index];
            let fragment = handshake_fragment(
                message.handshake_type,
                message.body.len(),
                message.message_seq,
                offset,
                &message.body[offset..offset + length],
            );
            let number = self.write_record(message.epoch, CONTENT_TYPE_HANDSHAKE, &fragment)?;
            flight.sent.push_back((number, index, offset, length));
            if flight.sent.len() > MAX_SENT_RECORDS {
                flight.sent.pop_front();
            }
        }
        flight.cursor = if pieces.len() > MAX_RECORDS_PER_TRANSMISSION {
            (start + count) % pieces.len()
        } else {
            0
        };
        flight.arm = true;
        Ok(())
    }

    /// Sends a new flight of the handshake, which replaces the one before it.
    pub(crate) fn send_flight(&mut self, mut flight: Flight) -> Result<()> {
        self.transmit(&mut flight)?;
        self.flight = Some(flight);
        // RFC 9147 section 7: what was received of the peer's flight is answered by this
        // one, and the list of records to acknowledge starts again.
        self.received_records.clear();
        self.ack_now = false;
        self.ack_delayed = false;
        self.ack_timer = None;
        Ok(())
    }

    /// The flight the peer was waiting on has been answered by its next one, which
    /// acknowledges all of it (RFC 9147 section 7.2).
    pub(crate) fn flight_answered(&mut self) {
        if let Some(flight) = &self.flight
            && matches!(
                flight.kind,
                FlightKind::Handshake | FlightKind::HelloRetryRequest
            )
        {
            self.answered = Some(flight.first_sent);
            self.flight = None;
        }
    }

    fn retransmit_handshake_flight(&mut self) -> Result<()> {
        let Some(mut flight) = self.flight.take() else {
            return Ok(());
        };
        flight.retransmissions += 1;
        flight.first_sent = None;
        let result = self.transmit(&mut flight);
        self.flight = Some(flight);
        result
    }

    /// Takes an ACK: the bytes that went out in the records it names are acknowledged and
    /// are not sent again (RFC 9147 section 7.2). `ack_epoch` is the epoch of the record
    /// that carried it, which bounds what it can acknowledge (section 7).
    fn handle_ack(&mut self, ack_epoch: u64, content: &[u8]) -> std::result::Result<(), Refusal> {
        let numbers = match decode_ack(content) {
            Ok(numbers) => numbers,
            Err(err) if ack_epoch == 0 => {
                debug!("discarded malformed plaintext ACK: {err}");
                return Ok(());
            }
            Err(err) => return Err(decode_error(err)),
        };
        // RFC 9147 section 7: during the handshake an ACK travels in an epoch no lower than
        // the records it names, so a plaintext one cannot acknowledge protected records.
        // After it the epochs of the two directions move apart, and there is no such order.
        let numbers: Vec<RecordNumber> = numbers
            .into_iter()
            .filter(|number| number.epoch <= ack_epoch || ack_epoch >= EPOCH_APPLICATION)
            .collect();

        if let Some(mut flight) = self
            .flight
            .take_if(|flight| flight.kind != FlightKind::HelloRetryRequest)
        {
            let progressed = Self::acknowledge(&mut flight, &numbers);
            if flight.acknowledged() {
                self.answered = Some(flight.first_sent);
                self.on_flight_acknowledged(flight.kind);
            } else {
                // A partial ACK, or an empty one (the peer could read nothing of what
                // arrived): what is still missing goes out again at once (section 5.8.1),
                // out of the flight's budget of retransmissions.
                let asks = progressed || numbers.is_empty();
                if asks && flight.retransmissions < self.cfg.maximum_retransmit_number {
                    flight.retransmissions += 1;
                    flight.first_sent = None;
                    let _ = self.transmit(&mut flight);
                }
                self.flight = Some(flight);
            }
        }

        let mut finished = vec![];
        let mut flights = std::mem::take(&mut self.post_handshake);
        for flight in &mut flights {
            let progressed = Self::acknowledge(flight, &numbers);
            if flight.acknowledged() {
                finished.push(flight.kind);
            } else if progressed && flight.retransmissions < self.cfg.maximum_retransmit_number {
                flight.retransmissions += 1;
                let _ = self.transmit(flight);
            }
        }
        flights.retain(|flight| !flight.acknowledged());
        self.post_handshake = flights;
        for kind in finished {
            self.on_flight_acknowledged(kind);
        }
        Ok(())
    }

    /// Marks what `numbers` acknowledge in `flight`. Whether anything new was.
    fn acknowledge(flight: &mut Flight, numbers: &[RecordNumber]) -> bool {
        let mut progressed = false;
        for (number, index, offset, length) in &flight.sent {
            if !numbers.contains(number) {
                continue;
            }
            let message = &mut flight.messages[*index];
            let end = if message.body.is_empty() {
                1
            } else {
                offset + length
            };
            if !message.acknowledged.contains(*offset, end) {
                message.acknowledged.add(*offset, end);
                progressed = true;
            }
        }
        progressed
    }

    fn on_flight_acknowledged(&mut self, kind: FlightKind) {
        self.flight_activity = true;
        match kind {
            FlightKind::Handshake => {}
            FlightKind::ClientFinal => {
                debug!("client: final flight acknowledged");
                self.confirm();
                // Nothing of the handshake epoch is sent or expected any more.
                self.drop_write_epoch(EPOCH_HANDSHAKE);
                self.drop_read_epoch(EPOCH_HANDSHAKE);
            }
            FlightKind::KeyUpdate => {
                // RFC 9147 section 8: the new keys are used once the KeyUpdate is
                // acknowledged, and not before.
                if let Err(err) = self.advance_write_keys() {
                    warn!("key update failed: {err}");
                }
                if std::mem::take(&mut self.key_update_requested_by_peer) {
                    let _ = self.send_key_update(false);
                }
            }
            FlightKind::NewConnectionId
            | FlightKind::RequestConnectionId
            | FlightKind::NewSessionTicket
            | FlightKind::CertificateRequest
            | FlightKind::Authentication
            | FlightKind::HelloRetryRequest => {}
        }
        self.prune_write_epochs();
    }

    /// Queues an ACK of the records in `received_records` (RFC 9147 section 7), in the
    /// highest epoch this side can send in.
    fn send_ack(&mut self) {
        self.ack_now = false;
        self.ack_delayed = false;
        self.ack_timer = None;
        let epoch = self.send_epoch();
        self.received_records.sort_unstable();
        self.received_records.dedup();
        // RFC 9147 section 7: an ACK travels in an epoch no lower than the records it names.
        let room = self
            .cfg
            .maximum_transmission_unit
            .saturating_sub(self.record_overhead(epoch) + 2)
            / 16;
        let mut numbers: Vec<RecordNumber> = self
            .received_records
            .iter()
            .filter(|number| number.epoch <= epoch || epoch >= EPOCH_APPLICATION)
            .copied()
            .collect();
        // The most recent ones, if they do not all fit.
        let most = room.clamp(1, MAX_ACK_RECORD_NUMBERS);
        if numbers.len() > most {
            numbers.drain(..numbers.len() - most);
        }
        if let Err(err) = self.write_record(epoch, CONTENT_TYPE_ACK, &encode_ack(&numbers)) {
            debug!("could not send an ACK: {err}");
        }
        if self.established {
            // After the handshake each record is acknowledged once (section 7). A server
            // keeps the records of the client's last flight, to acknowledge them again
            // when the client repeats it (section 5.8.1).
            let keep_handshake = !self.is_client;
            self.received_records
                .retain(|number| keep_handshake && number.epoch == EPOCH_HANDSHAKE);
        }
    }

    /// An ACK goes out with the next `drive`.
    pub(crate) fn acknowledge_now(&mut self) {
        self.ack_now = true;
    }

    fn note_received_record(&mut self, number: RecordNumber) {
        if !self.received_records.contains(&number) {
            self.received_records.push(number);
            if self.received_records.len() > MAX_ACK_RECORDS {
                self.received_records.remove(0);
            }
        }
    }

    // ------------------------------------------------------------------------------------
    // Timers. The association reads no clock: `drive` is given the time after anything
    // that may have armed a timer, and `handle_timeout` when one is due.
    // ------------------------------------------------------------------------------------

    /// Takes over the retransmission timer of the handshake so far (a client's ClientHello is
    /// sent by the version-neutral path until the ServerHello says DTLS 1.3): a value backed
    /// off by its retransmissions is kept until a flight gets through the first time (RFC
    /// 9147 section 5.8.2).
    pub(crate) fn continue_retransmit_interval(&mut self, interval: Duration) {
        self.retransmit_interval = interval
            .max(self.cfg.retransmit_interval)
            .min(self.cfg.maximum_retransmit_interval);
    }

    /// Sets the timers that what happened since the last call armed, and sends what is due
    /// at once.
    pub(crate) fn drive(&mut self, now: Instant) -> Result<()> {
        if self.failed {
            return Err(Error::ErrConnClosed);
        }
        self.clock = Some(now);
        self.handshake_started.get_or_insert(now);

        // RFC 9147 section 5.8.2: when a flight got through without being sent again, the
        // timer becomes one and a half times the round trip measured for it; otherwise it
        // keeps its backed-off value.
        if let Some(Some(first_sent)) = self.answered.take() {
            let measured = now.saturating_duration_since(first_sent);
            self.retransmit_interval = (measured + measured / 2)
                .max(MINIMUM_MEASURED_RETRANSMIT_INTERVAL)
                .min(self.cfg.maximum_retransmit_interval);
        }

        if std::mem::take(&mut self.peer_retransmitted)
            && self.flight.as_ref().is_some_and(|flight| {
                flight.kind == FlightKind::HelloRetryRequest
                    || flight.retransmissions < self.cfg.maximum_retransmit_number
            })
        {
            // RFC 9147 section 5.8.1: the peer sent its flight again, so ours was lost.
            self.retransmit_handshake_flight()?;
        }

        if self.established && self.established_at.is_none() {
            self.established_at = Some(now);
            // A server keeps the handshake keys to read, and acknowledge again, a client's
            // last flight that is sent again (RFC 9147 section 5.8.1). A client keeps them
            // while its own last flight is unacknowledged.
            if !self.is_client {
                self.handshake_keys_deadline = Some(now + HANDSHAKE_KEYS_LINGER);
            }
        }

        // RFC 9147 section 5.8.2: after a quiet period of ten times the timer's value, the
        // backed-off timer goes back to where it starts.
        let idle = self
            .flight
            .iter()
            .chain(self.post_handshake.iter())
            .all(|flight| flight.timer.is_none() && flight.retransmissions == 0);
        if idle
            && !self.flight_activity
            && self.last_flight_activity.is_some_and(|last| {
                now.saturating_duration_since(last)
                    >= self.retransmit_interval * IDLE_TIMER_RESET_FACTOR
            })
        {
            self.retransmit_interval = self.cfg.retransmit_interval;
        }
        if std::mem::take(&mut self.flight_activity)
            || self.flight.is_some()
            || !self.post_handshake.is_empty()
        {
            self.last_flight_activity = Some(now);
        }

        let interval = self.retransmit_interval;
        for flight in self.flight.iter_mut().chain(self.post_handshake.iter_mut()) {
            if std::mem::take(&mut flight.arm) && flight.kind != FlightKind::HelloRetryRequest {
                flight.timer = Some(now + interval);
                if flight.retransmissions == 0 && flight.first_sent.is_none() {
                    flight.first_sent = Some(now);
                }
            }
        }

        if self.ack_now {
            self.send_ack();
        } else if self.ack_delayed && self.ack_timer.is_none() {
            // RFC 9147 section 7.1: part of a flight arrived; if the rest does not follow,
            // say so after a quarter of the retransmission timer.
            self.ack_timer = Some(now + interval / 4);
        }
        Ok(())
    }

    fn handshake_deadline(&self) -> Option<Instant> {
        if self.established || self.failed {
            return None;
        }
        Some(self.handshake_started? + self.cfg.handshake_timeout?)
    }

    /// How often the handshake flight under way has been sent again (by its timer or for a
    /// repeat of the peer's flight).
    pub(crate) fn handshake_retransmissions(&self) -> usize {
        self.flight
            .as_ref()
            .map_or(0, |flight| flight.retransmissions)
    }

    pub(crate) fn next_timeout(&self) -> Option<Instant> {
        if self.failed {
            return None;
        }
        self.flight
            .iter()
            .chain(self.post_handshake.iter())
            .filter_map(|flight| flight.timer)
            .chain(self.ack_timer)
            .chain(self.handshake_deadline())
            .chain(self.handshake_keys_deadline)
            .min()
    }

    /// Advances the association to `now`: gives a handshake up at its deadline, sends again
    /// what has waited a whole timer for its answer (RFC 9147 section 5.8.1), sends a
    /// delayed ACK, and lets go of the handshake keys.
    pub(crate) fn handle_timeout(&mut self, now: Instant) -> Result<()> {
        if self.failed {
            return Err(Error::ErrConnClosed);
        }
        if self
            .handshake_deadline()
            .is_some_and(|deadline| now >= deadline)
        {
            warn!("dtls 1.3 handshake did not complete in time");
            self.fail();
            return Err(Error::ErrHandshakeTimeout);
        }
        if self.handshake_keys_deadline.is_some_and(|at| now >= at) {
            self.handshake_keys_deadline = None;
            self.drop_read_epoch(EPOCH_HANDSHAKE);
            if !self.is_client {
                self.received_records.clear();
            }
        }
        if self.ack_timer.is_some_and(|at| now >= at) {
            self.send_ack();
        }

        let maximum = self.cfg.maximum_retransmit_interval;
        let floor = self.cfg.retransmit_interval;
        let mut expired = false;
        if let Some(mut flight) = self.flight.take() {
            if flight.timer.is_some_and(|at| now >= at) {
                expired = true;
                flight.timer = None;
                flight.retransmissions += 1;
                flight.first_sent = None;
                // RFC 9147 section 4.4: unanswered retransmissions may mean the records are
                // too large for the path, so they are cut smaller.
                if flight.retransmissions > RETRANSMITS_BEFORE_MTU_BACKOFF {
                    self.handshake_mtu = (self.handshake_mtu / 2)
                        .max(FALLBACK_HANDSHAKE_MTU)
                        .min(self.handshake_mtu);
                }
                self.transmit(&mut flight)?;
            }
            if flight.kind == FlightKind::ClientFinal
                && self.state == HandshakeState::Established
                && flight.retransmissions > self.cfg.maximum_retransmit_number
            {
                // The handshake is complete for this client and nothing has acknowledged
                // its last flight: it is not sent for ever. The server either has it or
                // will fail its own handshake.
                self.on_flight_acknowledged(FlightKind::ClientFinal);
            } else {
                self.flight = Some(flight);
            }
        }
        let mut flights = std::mem::take(&mut self.post_handshake);
        for flight in &mut flights {
            if flight.timer.is_some_and(|at| now >= at) {
                flight.timer = None;
                // A post-handshake message is sent again as often as a flight would be and
                // then left: the association goes on without what it asked for.
                if flight.retransmissions < self.cfg.maximum_retransmit_number {
                    expired = true;
                    flight.retransmissions += 1;
                    self.transmit(flight)?;
                }
            }
        }
        self.post_handshake = flights;
        if expired {
            // RFC 9147 section 5.8.2: the timer doubles at each retransmission.
            self.retransmit_interval = (self.retransmit_interval * 2).min(maximum).max(floor);
        }
        self.drive(now)
    }

    // ------------------------------------------------------------------------------------
    // Reading records.
    // ------------------------------------------------------------------------------------

    /// Processes one datagram.
    ///
    /// # Errors
    ///
    /// `ErrAlertFatalOrClose` when the datagram ended the association (a fatal alert, sent
    /// or received, or the peer's close_notify), `ErrConnClosed` when it was over already.
    pub(crate) fn read(&mut self, datagram: &[u8]) -> Result<()> {
        if self.failed {
            self.repeat_fatal_alert(datagram);
            return Err(Error::ErrConnClosed);
        }
        if self.closed_by_peer {
            // RFC 9147 section 5.10: nothing after the peer's closure alert counts.
            return Err(Error::ErrConnClosed);
        }
        self.newest_record_read = false;
        let mut rest = datagram;
        while !rest.is_empty() {
            let (record, taken) = match next_record(rest, self.local_connection_id.len()) {
                Ok(next) => next,
                Err(err) => {
                    // RFC 9147 sections 4.1 and 4.5.2: an invalid record is discarded, and
                    // with it what follows, which cannot be delimited.
                    debug!("discarded {} bytes of a datagram: {err}", rest.len());
                    self.note_invalid_record();
                    break;
                }
            };
            let outcome = match record {
                DatagramRecord::Plaintext {
                    content_type,
                    sequence,
                    fragment,
                } => self.read_plaintext(content_type, sequence, fragment),
                DatagramRecord::Ciphertext { header, record } => {
                    // RFC 9147 section 4: a record for another connection id ends the
                    // datagram.
                    let connection_id = &record[header.connection_id.clone()];
                    // 0-RTT records come before the client knows this side's connection id.
                    let early =
                        header.epoch_bits == 1 && connection_id.is_empty() && !self.established;
                    if !early
                        && connection_id != &self.local_connection_id[..]
                        && !self
                            .accepted_connection_ids
                            .iter()
                            .any(|accepted| accepted == connection_id)
                    {
                        debug!("discarded a record with another connection id");
                        break;
                    }
                    self.read_ciphertext(&header, record)
                }
            };
            if let Err(refusal) = outcome {
                return Err(self.refuse(refusal));
            }
            if self.failed || self.closed_by_peer {
                return Err(Error::ErrAlertFatalOrClose);
            }
            rest = &rest[taken..];
        }
        Ok(())
    }

    fn read_plaintext(
        &mut self,
        content_type: u8,
        sequence: u64,
        fragment: &[u8],
    ) -> std::result::Result<(), Refusal> {
        // Anti-replay for the unprotected epoch: the window moves only for a record that
        // delivered something, since anyone can send one (RFC 9147 section 4.5.1).
        if !self.plaintext_replay.check(sequence) {
            return Ok(());
        }
        let number = RecordNumber { epoch: 0, sequence };
        match content_type {
            CONTENT_TYPE_HANDSHAKE => {
                // Unprotected handshake messages are the hellos, and they come first.
                if self.peer_uses_keys && self.established {
                    return Ok(());
                }
                match self.read_handshake_record(number, fragment, false) {
                    Ok(outcome) => {
                        if outcome.kept || outcome.repeated {
                            self.plaintext_replay.accept();
                        }
                        if outcome.kept {
                            self.note_received_record(number);
                        }
                        if outcome.repeated {
                            // The peer sent its hello again: our answer was lost.
                            self.peer_retransmitted = true;
                        }
                        Ok(())
                    }
                    Err(refusal) => Err(refusal),
                }
            }
            CONTENT_TYPE_ALERT => {
                // An unprotected alert is the peer's word only until it has shown that it
                // holds the handshake keys: from then on anyone else's.
                if self.peer_uses_keys || self.established {
                    return Ok(());
                }
                if let [_level, description] = fragment
                    && *description != alert::CLOSE_NOTIFY
                    && *description != alert::USER_CANCELED
                {
                    self.plaintext_replay.accept();
                    debug!("<- plaintext alert {description}");
                    self.fatal_alert_received = Some(*description);
                    self.fail();
                }
                Ok(())
            }
            CONTENT_TYPE_ACK => {
                if self.peer_uses_keys {
                    return Ok(());
                }
                self.handle_ack(0, fragment)
            }
            _ => Ok(()),
        }
    }

    fn read_ciphertext(
        &mut self,
        header: &UnifiedHeader,
        record: &[u8],
    ) -> std::result::Result<(), Refusal> {
        // RFC 9147 section 4.2.2: the keys are those of the most recent epoch whose low
        // bits are the record's.
        let Some(index) = self
            .read
            .iter()
            .enumerate()
            .filter(|(_, read)| (read.epoch & 3) as u8 == header.epoch_bits)
            .max_by_key(|(_, read)| read.epoch)
            .map(|(index, _)| index)
        else {
            // No keys for it (yet): a record that arrived ahead of the message that makes
            // them is held, a few of them, and tried again.
            if !self.established && self.early_records.len() < MAX_EARLY_RECORDS {
                self.early_records.push_back(record.to_vec());
                // RFC 9147 section 7.1: say that nothing of it could be read.
                if self.is_client && self.state == HandshakeState::ClientWaitServerHello {
                    self.ack_delayed = true;
                }
            }
            return Ok(());
        };
        let integrity_limit = self
            .integrity_limit
            .or(self.suite.map(|suite| suite.integrity_limit()))
            .unwrap_or(u64::MAX);
        let read = &mut self.read[index];
        let epoch = read.epoch;
        let (sequence, content_type, content) =
            match read.keys.open(header, record, read.next_expected) {
                Ok(opened) => opened,
                Err(Error::ErrInvalidMac) => {
                    // RFC 9147 section 4.5.3: failures are counted per key, and at the
                    // AEAD's limit the association is closed.
                    read.failures += 1;
                    let failures = read.failures;
                    self.note_invalid_record();
                    if self.failed {
                        return Ok(());
                    }
                    if failures >= integrity_limit {
                        warn!("too many records failed authentication in epoch {epoch}");
                        self.fail();
                    } else if failures == integrity_limit / 2
                        && self.established
                        && !self.key_update_outstanding()
                    {
                        let _ = self.send_key_update(true);
                    }
                    return Ok(());
                }
                // Authenticated, and malformed inside (RFC 8446 section 5.4).
                Err(Error::ErrRecordOverflow) => {
                    return Err(refuse(alert::RECORD_OVERFLOW, Error::ErrRecordOverflow));
                }
                Err(Error::ErrInvalidContentType) => {
                    return Err(refuse(
                        alert::UNEXPECTED_MESSAGE,
                        Error::ErrInvalidContentType,
                    ));
                }
                Err(_) => return Ok(()),
            };
        // RFC 9147 section 4.5.1: the window is consulted, and moved, only for a record
        // that has been deprotected.
        if !read.replay.check(sequence) {
            return Ok(());
        }
        read.replay.accept();
        let newest_epoch = self.read.iter().map(|read| read.epoch).max() == Some(epoch);
        let read = &mut self.read[index];
        if sequence >= read.next_expected && newest_epoch {
            self.newest_record_read = true;
        }
        read.next_expected = read.next_expected.max(sequence + 1);
        self.authenticated_records += 1;
        self.peer_uses_keys = true;
        if epoch >= EPOCH_APPLICATION {
            // A server protects nothing under its application keys before it has taken the
            // client's Finished.
            self.confirm();
            // RFC 9147 section 5.6: no 0-RTT records once the peer's application data
            // arrives.
            self.drop_read_epoch(EPOCH_EARLY);
        }
        if epoch > EPOCH_APPLICATION {
            // RFC 9147 section 8: the keys before an update "MUST" be kept until a record
            // under the new ones has been read, and records of an older epoch, late through
            // loss or reordering, "SHOULD" be processed with it. The epoch before this one
            // stays readable until a record of the next arrives; anything older goes (the
            // peer cannot send in two epochs past it before an update is acknowledged, so
            // the epoch bits of section 4.2.2 still tell them apart).
            self.read.retain(|read| {
                read.epoch < EPOCH_APPLICATION || read.epoch.saturating_add(1) >= epoch
            });
        }

        let number = RecordNumber { epoch, sequence };
        match content_type {
            CONTENT_TYPE_HANDSHAKE => {
                let outcome = self.read_handshake_record(number, &content, true)?;
                if outcome.kept || (outcome.repeated && self.established) {
                    self.note_received_record(number);
                }
                if self.failed {
                    return Ok(());
                }
                if outcome.repeated {
                    if self.established && !self.is_client && epoch == EPOCH_HANDSHAKE {
                        // RFC 9147 section 5.8.1: the client sent its last flight again; it
                        // has not seen the ACK.
                        self.ack_now = true;
                    } else if self.established && epoch >= EPOCH_APPLICATION {
                        // Section 7: a post-handshake message is acknowledged each time.
                        self.ack_now = true;
                    } else {
                        self.peer_retransmitted = true;
                    }
                }
                if outcome.out_of_order && !self.received_records.is_empty() {
                    // Section 7.1: a sign of disruption in the peer's flight.
                    self.ack_now = true;
                }
                Ok(())
            }
            CONTENT_TYPE_ALERT => self.read_alert(&content),
            CONTENT_TYPE_ACK => self.handle_ack(epoch, &content),
            CONTENT_TYPE_APPLICATION_DATA => {
                // 0-RTT data (RFC 8446 section 4.2.10), where the server accepted it: given
                // to the application at once, up to the size the ticket allows (section
                // 4.6.1: more is an unexpected_message).
                if epoch == EPOCH_EARLY && !self.is_client && self.early_data_accepted == Some(true)
                {
                    self.early_data_received += content.len();
                    if self.early_data_received > self.early_data_limit {
                        return Err(refuse(
                            alert::UNEXPECTED_MESSAGE,
                            Error::Other("more 0-RTT data than max_early_data_size".to_owned()),
                        ));
                    }
                    self.early_data.push_back(BytesMut::from(&content[..]));
                    return Ok(());
                }
                // RFC 8446 section 5: application data is never protected under the
                // handshake keys.
                if epoch < EPOCH_APPLICATION {
                    return Err(refuse(
                        alert::UNEXPECTED_MESSAGE,
                        Error::ErrApplicationDataEpochZero,
                    ));
                }
                if self.established {
                    self.incoming.push_back(BytesMut::from(&content[..]));
                } else {
                    // RFC 9147 section 5.8.1: held until the peer's Finished has arrived.
                    // The peer is past its Finished, so that was lost or is late: our
                    // flight goes out again at once, which draws it again.
                    if !std::mem::replace(&mut self.early_data_seen, true) {
                        self.peer_retransmitted = true;
                    }
                    let held: usize = self.held_application_data.iter().map(BytesMut::len).sum();
                    if self.held_application_data.len() < MAX_PRE_HANDSHAKE_APPLICATION_DATA_PACKETS
                        && held + content.len() <= MAX_PRE_HANDSHAKE_APPLICATION_DATA_BYTES
                    {
                        self.held_application_data
                            .push_back(BytesMut::from(&content[..]));
                    }
                }
                Ok(())
            }
            _ => Err(refuse(
                alert::UNEXPECTED_MESSAGE,
                Error::ErrInvalidContentType,
            )),
        }
    }

    /// RFC 9147 section 5.10: a peer that keeps sending records that are not valid is
    /// given up on, where the configuration sets a limit
    /// ([`ConfigBuilder::with_invalid_record_limit`](crate::config::ConfigBuilder::with_invalid_record_limit)).
    /// The local state goes without an alert: whoever sends such records is not known to be
    /// the peer.
    fn note_invalid_record(&mut self) {
        self.invalid_records += 1;
        if self
            .cfg
            .invalid_record_limit
            .is_some_and(|limit| self.invalid_records > limit)
        {
            warn!(
                "dtls 1.3: {} invalid records, over the configured limit",
                self.invalid_records
            );
            self.fail();
        }
    }

    /// Tries the records that arrived before their keys again.
    pub(crate) fn replay_early_records(&mut self) -> std::result::Result<(), Refusal> {
        let records: Vec<Vec<u8>> = self.early_records.drain(..).collect();
        for record in records {
            if self.failed {
                break;
            }
            if let Ok(header) = UnifiedHeader::parse(&record, self.local_connection_id.len()) {
                self.read_ciphertext(&header, &record[..header.record_len])?;
            }
        }
        Ok(())
    }

    fn read_alert(&mut self, content: &[u8]) -> std::result::Result<(), Refusal> {
        let [_level, description] = content else {
            return Err(decode_error(Error::ErrBufferTooSmall));
        };
        debug!("<- alert {description}");
        match *description {
            // RFC 8446 section 6.1: both are closure alerts, and "Any data received after a
            // closure alert has been received MUST be ignored" (RFC 9147 section 5.10). A
            // user_canceled ends the reading as a close_notify does (the close_notify that
            // "SHOULD" follow it is not read any more); the answer is this side's own
            // close_notify.
            alert::CLOSE_NOTIFY | alert::USER_CANCELED => {
                self.closed_by_peer = true;
                self.closed = true;
                self.send_alert(alert::CLOSE_NOTIFY);
                Ok(())
            }
            // Section 6.2: every other alert is an error alert, whatever its level says.
            _ => {
                self.fatal_alert_received = Some(*description);
                self.fail();
                Ok(())
            }
        }
    }

    // ------------------------------------------------------------------------------------
    // Handshake messages: fragments, order, reassembly (RFC 9147 sections 5.2 and 5.5).
    // ------------------------------------------------------------------------------------

    fn read_handshake_record(
        &mut self,
        number: RecordNumber,
        content: &[u8],
        authenticated: bool,
    ) -> std::result::Result<RecordOutcome, Refusal> {
        let mut outcome = RecordOutcome::default();
        let fragments = match HandshakeFragment::parse_all(content) {
            Ok(fragments) => fragments,
            Err(err) if authenticated => return Err(decode_error(err)),
            Err(_) => return Ok(outcome),
        };
        for fragment in fragments {
            let limit = if authenticated {
                MAX_MESSAGE
            } else {
                MAX_PLAINTEXT_MESSAGE
            };
            if fragment.length > limit {
                if authenticated {
                    return Err(decode_error(Error::ErrLengthMismatch));
                }
                continue;
            }
            // RFC 9147 section 5.2: an older message is discarded, a later one queued.
            if fragment.message_seq < self.next_receive_seq {
                outcome.repeated = true;
                continue;
            }
            if fragment.message_seq - self.next_receive_seq > MESSAGE_SEQ_WINDOW {
                continue;
            }
            if fragment.message_seq != self.next_receive_seq {
                outcome.out_of_order = true;
            }
            let buffered: usize = self.reassembly.values().map(|r| r.data.len()).sum();
            let entry = match self.reassembly.get_mut(&fragment.message_seq) {
                Some(entry) => entry,
                None => {
                    if buffered + fragment.length > MAX_REASSEMBLY_BYTES {
                        continue;
                    }
                    self.reassembly
                        .entry(fragment.message_seq)
                        .or_insert(Reassembly {
                            handshake_type: fragment.handshake_type,
                            data: vec![0; fragment.length],
                            received: Ranges::default(),
                        })
                }
            };
            if entry.handshake_type != fragment.handshake_type
                || entry.data.len() != fragment.length
            {
                // Not a fragment of the message the others are of.
                if authenticated {
                    return Err(refuse(alert::ILLEGAL_PARAMETER, Error::ErrLengthMismatch));
                }
                continue;
            }
            let start = fragment.fragment_offset;
            let end = start + fragment.fragment.len();
            // RFC 9147 section 5.5: overlapping fragments are taken, and a byte that
            // changes between two of them is an illegal_parameter.
            let conflict = entry.received.0.iter().any(|(have_start, have_end)| {
                let from = start.max(*have_start);
                let to = end.min(*have_end);
                from < to && entry.data[from..to] != fragment.fragment[from - start..to - start]
            });
            if conflict {
                if authenticated {
                    return Err(refuse(alert::ILLEGAL_PARAMETER, Error::ErrLengthMismatch));
                }
                continue;
            }
            if start != 0 && !entry.received.contains(0, start) {
                outcome.out_of_order = true;
            }
            entry.data[start..end].copy_from_slice(fragment.fragment);
            entry.received.add(start, end);
            outcome.kept = true;
            let epoch = number.epoch;

            // Process every message that is now complete and next in order.
            while let Some(entry) = self.reassembly.get(&self.next_receive_seq) {
                if !entry.received.covers(entry.data.len()) {
                    break;
                }
                let Some(message) = self.reassembly.remove(&self.next_receive_seq) else {
                    break;
                };
                let message_seq = self.next_receive_seq;
                self.next_receive_seq = self.next_receive_seq.wrapping_add(1);
                debug!(
                    "<- handshake type {} (seq {message_seq}, epoch {epoch})",
                    message.handshake_type
                );
                match self.on_handshake_message(message.handshake_type, &message.data, epoch) {
                    Ok(()) => {}
                    // An unprotected message that does not hold up is anyone's: it is
                    // dropped and its place kept for the genuine one, unless this side has
                    // nothing to go back to.
                    Err(refusal) if !authenticated && self.forgives_plaintext(&refusal) => {
                        debug!(
                            "discarded an unprotected handshake message: {}",
                            refusal.error
                        );
                        self.next_receive_seq = message_seq;
                        outcome.kept = false;
                        return Ok(outcome);
                    }
                    Err(refusal) => return Err(refusal),
                }
                if self.failed {
                    return Ok(outcome);
                }
            }
            if !self.reassembly.is_empty() && self.state != HandshakeState::Established {
                // Part of a flight is here and the rest is not (section 7.1).
                self.ack_delayed = true;
            }
        }
        let _ = number;
        Ok(outcome)
    }

    /// Whether a refusal of an unprotected message is better not acted on: a message that
    /// does not parse can be a forgery, and answering it with a fatal alert would let anyone
    /// end the handshake. A message that parses and is refused for what it says is answered.
    fn forgives_plaintext(&self, refusal: &Refusal) -> bool {
        refusal.alert == alert::DECODE_ERROR
    }

    // ------------------------------------------------------------------------------------
    // Alerts and the end of the association.
    // ------------------------------------------------------------------------------------

    fn send_alert(&mut self, description: u8) {
        // RFC 8446 section 6: close_notify and user_canceled are warnings, the rest fatal.
        let level = if description == alert::CLOSE_NOTIFY || description == alert::USER_CANCELED {
            1
        } else {
            2
        };
        let epoch = self.send_epoch();
        if let Err(err) = self.write_record(epoch, CONTENT_TYPE_ALERT, &[level, description]) {
            debug!("could not send alert {description}: {err}");
        }
    }

    /// Ends the association over `refusal`: the alert goes out and the error comes back.
    pub(crate) fn refuse(&mut self, refusal: Refusal) -> Error {
        warn!(
            "dtls 1.3 {}: alert {} ({})",
            if self.is_client { "client" } else { "server" },
            refusal.alert,
            refusal.error
        );
        self.send_alert(refusal.alert);
        self.fatal_alert_sent = Some((refusal.alert, self.cfg.maximum_retransmit_number));
        self.fail();
        refusal.error
    }

    /// The association is over: nothing more is read, written or retransmitted, and its
    /// secrets are forgotten (RFC 8446 section 6.2).
    /// The fatal (error) alerts that ended the association: (sent, received).
    pub(crate) fn fatal_alerts(&self) -> (Option<u8>, Option<u8>) {
        (
            self.fatal_alert_sent.map(|(description, _)| description),
            self.fatal_alert_received,
        )
    }

    pub(crate) fn fail(&mut self) {
        self.failed = true;
        self.closed = true;
        self.flight = None;
        self.post_handshake.clear();
        self.ack_timer = None;
        self.handshake_keys_deadline = None;
        self.forget_secrets();
    }

    /// RFC 9147 section 5.10: an alert is not retransmitted, but the offending record
    /// arriving again (the peer has not seen the alert) draws a new one, a bounded number
    /// of times.
    fn repeat_fatal_alert(&mut self, datagram: &[u8]) {
        let Some((description, remaining)) = self.fatal_alert_sent else {
            return;
        };
        let carries_handshake = datagram.first() == Some(&CONTENT_TYPE_HANDSHAKE)
            || datagram
                .first()
                .is_some_and(|first| is_unified_header(*first));
        if remaining == 0 || !carries_handshake {
            return;
        }
        self.fatal_alert_sent = Some((description, remaining - 1));
        self.send_alert(description);
    }

    /// Begins an orderly shutdown: a close_notify.
    pub(crate) fn close(&mut self) {
        if !self.closed {
            self.closed = true;
            self.send_alert(alert::CLOSE_NOTIFY);
        }
    }

    /// A client that authenticated itself learns that the server accepted it: only now is
    /// its handshake complete. Until the server has verified the client's certificate it
    /// can still refuse it, and a client that reported completion at its own Finished would
    /// report a connection the server never had.
    fn confirm(&mut self) {
        if self.state == HandshakeState::ClientWaitConfirmation
            && let Err(err) = self.establish()
        {
            warn!("dtls 1.3 client: {err}");
        }
    }

    /// The handshake is complete: what was held is released.
    pub(crate) fn establish(&mut self) -> Result<()> {
        self.established = true;
        self.state = HandshakeState::Established;
        self.incoming.extend(self.held_application_data.drain(..));
        while let Some(data) = self.queued_writes.pop_front() {
            self.write_application_data(&data)?;
        }
        Ok(())
    }

    // ------------------------------------------------------------------------------------
    // After the handshake: key updates and connection ids (RFC 9147 sections 8 and 9).
    // ------------------------------------------------------------------------------------

    /// How many post-handshake messages of this side wait for their acknowledgement.
    pub(crate) fn unacknowledged_post_handshake_messages(&self) -> usize {
        self.post_handshake.len()
    }

    pub(crate) fn key_update_outstanding(&self) -> bool {
        self.post_handshake
            .iter()
            .any(|flight| flight.kind == FlightKind::KeyUpdate)
    }

    pub(crate) fn send_post_handshake(
        &mut self,
        kind: FlightKind,
        handshake_type: u8,
        body: Vec<u8>,
    ) -> Result<()> {
        let mut flight = Flight::new(kind);
        flight.push(self.next_send_seq, self.send_epoch(), handshake_type, body);
        self.next_send_seq = self.next_send_seq.wrapping_add(1);
        self.transmit(&mut flight)?;
        self.post_handshake.push(flight);
        Ok(())
    }

    /// Sends `messages` as one post-handshake flight: in order, and acknowledged together.
    pub(crate) fn send_post_handshake_messages(
        &mut self,
        kind: FlightKind,
        messages: Vec<(u8, Vec<u8>)>,
    ) -> Result<()> {
        let mut flight = Flight::new(kind);
        let epoch = self.send_epoch();
        for (handshake_type, body) in messages {
            flight.push(self.next_send_seq, epoch, handshake_type, body);
            self.next_send_seq = self.next_send_seq.wrapping_add(1);
        }
        self.transmit(&mut flight)?;
        self.post_handshake.push(flight);
        Ok(())
    }

    /// Sends a KeyUpdate: this side moves to new sending keys once the peer acknowledges
    /// it. `request_update` asks the peer to update its own as well.
    ///
    /// # Errors
    ///
    /// Before the handshake is complete, while an earlier KeyUpdate is unacknowledged (RFC
    /// 9147 section 8), and when the epoch would pass 2^48-1.
    pub(crate) fn send_key_update(&mut self, request_update: bool) -> Result<()> {
        if !self.established || self.closed {
            return Err(Error::ErrConnClosed);
        }
        if self.key_update_outstanding() {
            return Err(Error::Other(
                "a KeyUpdate is waiting for its acknowledgement".to_owned(),
            ));
        }
        if self.send_epoch() >= MAX_SEND_EPOCH {
            return Err(Error::ErrSequenceNumberOverflow);
        }
        self.send_post_handshake(
            FlightKind::KeyUpdate,
            KEY_UPDATE,
            key_update(request_update),
        )
    }

    /// The peer acknowledged this side's KeyUpdate: the next sending keys take over.
    fn advance_write_keys(&mut self) -> Result<()> {
        let schedule = self.schedule.as_ref().ok_or(Error::ErrCipherSuiteUnset)?;
        let current = self
            .local_traffic_secret
            .as_ref()
            .ok_or(Error::ErrCipherSuiteUnset)?;
        let next =
            schedule.next_application_traffic_secret(self.provider.crypto(), current.as_ref())?;
        let epoch = self.send_epoch() + 1;
        self.install_write_epoch(epoch, next.as_ref())?;
        self.local_traffic_secret = Some(next);
        Ok(())
    }

    /// The peer sent a KeyUpdate: its next records are under its next keys.
    fn advance_read_keys(&mut self) -> Result<()> {
        let schedule = self.schedule.as_ref().ok_or(Error::ErrCipherSuiteUnset)?;
        let current = self
            .remote_traffic_secret
            .as_ref()
            .ok_or(Error::ErrCipherSuiteUnset)?;
        let next =
            schedule.next_application_traffic_secret(self.provider.crypto(), current.as_ref())?;
        let epoch = self
            .read
            .iter()
            .map(|read| read.epoch)
            .max()
            .unwrap_or(EPOCH_APPLICATION)
            + 1;
        self.install_read_epoch(epoch, next.as_ref())?;
        self.remote_traffic_secret = Some(next);
        Ok(())
    }

    /// Sends a NewConnectionId with `connection_ids` for the peer to use in records to this
    /// side (RFC 9147 section 9). With `immediate` the peer switches to one of them at
    /// once, and this side expects it from then on.
    pub(crate) fn send_new_connection_id(
        &mut self,
        connection_ids: Vec<Vec<u8>>,
        immediate: bool,
    ) -> Result<()> {
        if !self.established || self.closed {
            return Err(Error::ErrConnClosed);
        }
        // RFC 9147 section 9: not without a negotiated, non-empty connection id, and one
        // NewConnectionId at a time.
        if !self.connection_ids_negotiated || self.local_connection_id.is_empty() {
            return Err(Error::Other("no connection id was negotiated".to_owned()));
        }
        if self
            .post_handshake
            .iter()
            .any(|flight| flight.kind == FlightKind::NewConnectionId)
        {
            return Err(Error::Other(
                "a NewConnectionId is waiting for its acknowledgement".to_owned(),
            ));
        }
        // The length of a connection id is not in a record, so every id this side hands out
        // has the length of the first.
        if connection_ids
            .iter()
            .any(|id| id.len() != self.local_connection_id.len())
        {
            return Err(Error::Other(
                "connection ids of one association have one length".to_owned(),
            ));
        }
        let usage = if immediate { CID_IMMEDIATE } else { CID_SPARE };
        let body = new_connection_id(&connection_ids, usage);
        // Records under the ids handed out so far are still taken: the peer goes on using
        // the old one until this message reaches it.
        if self.accepted_connection_ids.is_empty() {
            self.accepted_connection_ids
                .push_back(self.local_connection_id.clone());
        }
        for connection_id in &connection_ids {
            self.accepted_connection_ids
                .push_back(connection_id.clone());
            if self.accepted_connection_ids.len() > 2 * MAX_SPARE_CONNECTION_IDS {
                self.accepted_connection_ids.pop_front();
            }
        }
        self.send_post_handshake(FlightKind::NewConnectionId, NEW_CONNECTION_ID, body)
    }

    /// Asks the peer for `count` more connection ids (RFC 9147 section 9).
    pub(crate) fn send_request_connection_id(&mut self, count: u8) -> Result<()> {
        if !self.established || self.closed {
            return Err(Error::ErrConnClosed);
        }
        // Not when this side sends an empty connection id, and one request at a time.
        if !self.connection_ids_negotiated || self.remote_connection_id.is_empty() {
            return Err(Error::Other("no connection id was negotiated".to_owned()));
        }
        if self.connection_id_request_outstanding {
            return Err(Error::Other(
                "a RequestConnectionId is still unfulfilled".to_owned(),
            ));
        }
        self.connection_id_request_outstanding = true;
        self.send_post_handshake(
            FlightKind::RequestConnectionId,
            REQUEST_CONNECTION_ID,
            request_connection_id(count),
        )
    }

    /// Whether `datagram` starts with a protected record under a connection id of this
    /// association: how an endpoint finds the association of a datagram from an address it
    /// does not know (RFC 9146 section 6).
    pub(crate) fn owns_connection_id(&self, datagram: &[u8]) -> bool {
        let length = self.local_connection_id.len();
        let Some(connection_id) = datagram.get(1..1 + length) else {
            return false;
        };
        length > 0
            && datagram[0] & 0xf0 == 0x30
            && (connection_id == &self.local_connection_id[..]
                || self
                    .accepted_connection_ids
                    .iter()
                    .any(|accepted| accepted == connection_id))
    }

    /// Switches to the next spare connection id the peer provided, as an endpoint does when
    /// it sends on a new path (RFC 9147 section 9). `false` when there is none.
    pub(crate) fn use_next_connection_id(&mut self) -> bool {
        match self.spare_connection_ids.pop_front() {
            Some(connection_id) => {
                self.remote_connection_id = connection_id;
                true
            }
            None => false,
        }
    }

    /// A handshake message after the handshake (RFC 9147 section 5.8.4).
    pub(crate) fn on_post_handshake_message(
        &mut self,
        handshake_type: u8,
        body: &[u8],
    ) -> std::result::Result<(), Refusal> {
        let unexpected = || refuse(alert::UNEXPECTED_MESSAGE, Error::ErrUnhandledContextType);
        // RFC 8446 section 4.6.2: the messages of a client's answer to a CertificateRequest
        // come together, with nothing of another type in between.
        if self.client_authentication.is_some()
            && !matches!(handshake_type, CERTIFICATE_VERIFY | FINISHED)
        {
            return Err(unexpected());
        }
        match handshake_type {
            // RFC 8446 section 4.6.2: only to a client that sent post_handshake_auth; to any
            // other it is an unexpected_message (the arm below).
            CERTIFICATE_REQUEST if self.is_client && self.cfg.post_handshake_auth => {
                self.on_post_handshake_certificate_request(body)?;
            }
            CERTIFICATE | CERTIFICATE_VERIFY | FINISHED if !self.is_client => {
                self.on_post_handshake_authentication(handshake_type, body)?;
            }
            NEW_SESSION_TICKET if self.is_client => {
                // Kept if this client resumes, dropped otherwise; acknowledged either way,
                // so that the server stops sending it.
                self.on_new_session_ticket(body)?;
            }
            KEY_UPDATE => {
                let update_requested = parse_key_update(body)?;
                self.advance_read_keys()
                    .map_err(|err| refuse(alert::INTERNAL_ERROR, err))?;
                if update_requested {
                    // RFC 9147 section 8: answered with a KeyUpdate of our own, unless one
                    // is on its way already or the epoch has no room.
                    if self.key_update_outstanding() {
                        self.key_update_requested_by_peer = false;
                    } else if self.send_epoch() < MAX_SEND_EPOCH {
                        let _ = self.send_key_update(false);
                    }
                }
            }
            NEW_CONNECTION_ID => {
                // RFC 9147 section 9: only when connection ids were negotiated and the
                // peer is not being sent an empty one.
                if !self.connection_ids_negotiated || self.remote_connection_id.is_empty() {
                    return Err(unexpected());
                }
                let (connection_ids, usage) = parse_new_connection_id(body)?;
                self.connection_id_request_outstanding = false;
                let mut connection_ids: VecDeque<Vec<u8>> = connection_ids.into();
                if usage == CID_IMMEDIATE {
                    // One of the new ones is used for every record from now on.
                    match connection_ids.pop_front() {
                        Some(connection_id) => self.remote_connection_id = connection_id,
                        None => {
                            return Err(refuse(
                                alert::ILLEGAL_PARAMETER,
                                Error::Other("cid_immediate without a connection id".to_owned()),
                            ));
                        }
                    }
                }
                // Spare ones are used in the order given; more than are wanted are dropped.
                for connection_id in connection_ids {
                    if self.spare_connection_ids.len() < MAX_SPARE_CONNECTION_IDS {
                        self.spare_connection_ids.push_back(connection_id);
                    }
                }
            }
            REQUEST_CONNECTION_ID => {
                if !self.connection_ids_negotiated || self.local_connection_id.is_empty() {
                    return Err(unexpected());
                }
                let count = parse_request_connection_id(body)?;
                // RFC 9147 section 9: an excessive number of requests may be answered with
                // too_many_cids_requested.
                self.connection_id_requests_received += 1;
                if self.connection_id_requests_received > MAX_CONNECTION_ID_REQUESTS {
                    return Err(refuse(
                        alert::TOO_MANY_CIDS_REQUESTED,
                        Error::Other("too many RequestConnectionId messages".to_owned()),
                    ));
                }
                // As many as asked for, up to what this side cares to track; none when a
                // NewConnectionId of ours is still unacknowledged.
                let count = usize::from(count).min(MAX_SPARE_CONNECTION_IDS);
                let mut connection_ids = vec![];
                for _ in 0..count {
                    let mut connection_id = vec![0u8; self.local_connection_id.len()];
                    self.provider
                        .random()
                        .fill(&mut connection_id)
                        .map_err(|err| {
                            refuse(alert::INTERNAL_ERROR, Error::Crypto(err.to_string()))
                        })?;
                    connection_ids.push(connection_id);
                }
                let _ = self.send_new_connection_id(connection_ids, false);
            }
            // RFC 8446 section 4.6.2: post-handshake authentication was not offered, so a
            // CertificateRequest now is an unexpected_message; so is anything else.
            _ => return Err(unexpected()),
        }
        self.ack_now = true;
        Ok(())
    }
}