rtci-dtls 0.2.1

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
use crate::alert::{AlertDescription, AlertLevel};
use crate::conn::*;
use crate::content::*;
use shared::error::*;

use log::*;
use std::fmt;
use std::time::Instant;

//use std::io::BufWriter;

// [RFC6347 Section-4.2.4]
//                      +-----------+
//                +---> | PREPARING | <--------------------+
//                |     +-----------+                      |
//                |           |                            |
//                |           | Buffer next flight         |
//                |           |                            |
//                |          \|/                           |
//                |     +-----------+                      |
//                |     |  SENDING  |<------------------+  | Send
//                |     +-----------+                   |  | HelloRequest
//        Receive |           |                         |  |
//           next |           | Send flight             |  | or
//         flight |  +--------+                         |  |
//                |  |        | Set retransmit timer    |  | Receive
//                |  |       \|/                        |  | HelloRequest
//                |  |  +-----------+                   |  | Send
//                +--)--|  WAITING  |-------------------+  | ClientHello
//                |  |  +-----------+   Timer expires   |  |
//                |  |         |                        |  |
//                |  |         +------------------------+  |
//        Receive |  | Send           Read retransmit      |
//           last |  | last                                |
//         flight |  | flight                              |
//                |  |                                     |
//               \|/\|/                                    |
//            +-----------+                                |
//            | FINISHED  | -------------------------------+
//            +-----------+
//                 |  /|\
//                 |   |
//                 +---+
//              Read retransmit
//           Retransmit last flight

#[derive(Copy, Clone, PartialEq)]
pub(crate) enum HandshakeState {
    Errored,
    Preparing,
    Sending,
    Waiting,
    Finished,
}

impl fmt::Display for HandshakeState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            HandshakeState::Errored => write!(f, "Errored"),
            HandshakeState::Preparing => write!(f, "Preparing"),
            HandshakeState::Sending => write!(f, "Sending"),
            HandshakeState::Waiting => write!(f, "Waiting"),
            HandshakeState::Finished => write!(f, "Finished"),
        }
    }
}

pub(crate) fn srv_cli_str(is_client: bool) -> String {
    if is_client {
        return "client".to_owned();
    }
    "server".to_owned()
}

impl DTLSConn {
    /// Advances the handshake state machine as far as it can go now.
    ///
    /// A handshake that errors has given up: a flight failed to parse or generate (a fatal alert
    /// has been queued for the peer where one applies), or the retransmissions ran out. The
    /// association is then failed and stays failed (W3C webrtc 5.5.1 RTCDtlsTransport "failed").
    pub(crate) fn handshake(&mut self, now: Instant) -> Result<()> {
        self.state.clock = Some(now);
        if self.v13.is_some() {
            return self.drive_dtls13(now, false);
        }
        if self.failed {
            return Err(Error::ErrConnClosed);
        }
        // The handshake deadline counts from the first time the handshake is driven.
        self.handshake_started.get_or_insert(now);
        let peer_repeated_flight = std::mem::take(&mut self.peer_repeated_flight);
        let flight_before = self.current_flight.to_string();
        let mut result = self.handshake_steps(now);
        if result.is_ok()
            && peer_repeated_flight
            && self.current_handshake_state == HandshakeState::Waiting
            && self.current_flight.to_string() == flight_before
        {
            result = self.retransmit_for_repeated_flight(now);
        }
        if result.is_err() {
            self.fail();
        }
        result
    }

    /// Advances the DTLS 1.3 association to `now`: sets the timers that what has happened
    /// since armed, and with `timeout` acts on those that are due.
    pub(crate) fn drive_dtls13(&mut self, now: Instant, timeout: bool) -> Result<()> {
        let Some(v13) = &mut self.v13 else {
            return Ok(());
        };
        let result = if timeout {
            v13.handle_timeout(now)
        } else {
            v13.drive(now)
        };
        self.sync_dtls13();
        result
    }

    /// RFC 6347 4.2.4: the peer sent again a flight we had already processed, so the flight we
    /// answered it with was lost. Retransmit that flight now instead of when our timer fires;
    /// sending it restarts the timer. Only while still waiting for the peer's next flight, and
    /// out of the same retransmission budget the timer uses. A spent budget means no answer
    /// here: giving up stays the timer's decision.
    fn retransmit_for_repeated_flight(&mut self, now: Instant) -> Result<()> {
        if !self.retransmit || self.current_retransmit_count >= self.maximum_retransmit_number {
            return Ok(());
        }
        debug!(
            "[handshake:{}] {} peer repeated its flight, retransmitting",
            srv_cli_str(self.state.is_client),
            self.current_flight
        );
        self.current_retransmit_count += 1;
        self.current_handshake_state = HandshakeState::Sending;
        self.handshake_steps(now)
    }

    fn handshake_steps(&mut self, now: Instant) -> Result<()> {
        loop {
            debug!(
                "[handshake:{}] {}: {}",
                srv_cli_str(self.state.is_client),
                self.current_flight,
                self.current_handshake_state
            );

            if self.current_handshake_state == HandshakeState::Finished
                && !self.is_handshake_completed()
            {
                self.set_handshake_completed();
                debug!(
                    "[handshake:{}] is completed",
                    srv_cli_str(self.state.is_client),
                );
                return Ok(());
            }

            let previous_handshake_state = self.current_handshake_state;
            self.current_handshake_state = match previous_handshake_state {
                HandshakeState::Preparing => self.prepare()?,
                HandshakeState::Sending => self.send(now)?,
                HandshakeState::Waiting => self.wait()?,
                HandshakeState::Finished => self.finish()?,
                _ => return Err(Error::ErrInvalidFsmTransition),
            };

            if previous_handshake_state == self.current_handshake_state
                && matches!(
                    previous_handshake_state,
                    HandshakeState::Waiting | HandshakeState::Finished
                )
            {
                // Wait for a timeout or an incoming packet. `Finished` self transitions for the same
                // reason `Waiting` does, and has to terminate for a stronger one: an association
                // re-entered here after completing (the RFC 6347 4.2.4 last flight retransmission)
                // has already passed the loop head guard, which only returns while
                // `!is_handshake_completed()`. `send()` queues the flight and yields `Finished`,
                // `finish()` yields `Finished` again, and without this the loop would spin.
                return Ok(());
            }
        }
    }

    fn prepare(&mut self) -> Result<HandshakeState> {
        self.flights = None;

        // Prepare flights
        self.current_retransmit_count = 0;
        self.early_data_retransmit_done = false;
        self.retransmit = self.current_flight.has_retransmit();

        let result =
            self.current_flight
                .generate(&mut self.state, &self.cache, &self.handshake_config);

        match result {
            Err((a, err)) => {
                if let Some(a) = a {
                    self.notify(a.alert_level, a.alert_description);
                }
                if let Some(err) = err {
                    return Err(err);
                }
            }
            Ok(pkts) => self.flights = Some(pkts),
        };

        let epoch = self.handshake_config.initial_epoch;
        let mut next_epoch = epoch;
        if let Some(pkts) = &mut self.flights {
            for p in pkts {
                p.record.record_layer_header.epoch += epoch;
                if p.record.record_layer_header.epoch > next_epoch {
                    next_epoch = p.record.record_layer_header.epoch;
                }
                if let Content::Handshake(h) = &mut p.record.content {
                    h.handshake_header.message_sequence = self.state.handshake_send_sequence as u16;
                    self.state.handshake_send_sequence += 1;
                }
            }
        }
        if epoch != next_epoch {
            debug!(
                "[handshake:{}] -> changeCipherSpec (epoch: {})",
                srv_cli_str(self.state.is_client),
                next_epoch
            );
            self.set_local_epoch(next_epoch);
        }

        Ok(HandshakeState::Sending)
    }
    fn send(&mut self, now: Instant) -> Result<HandshakeState> {
        // Send flights
        if let Some(pkts) = self.flights.clone() {
            self.write_packets(pkts);
        }

        // A Sending reached after the association has completed is an RFC 6347 4.2.4
        // retransmission of our own last flight (the peer repeated its final flight), so it returns
        // to Finished and never re-arms the receive timer. Only `Flight6` (the server) overrides
        // `is_last_send_flight`, so on its own that check would send the client — whose last flight
        // is `Flight5` — back into Waiting, where a failed `Flight5::parse` would strand a completed
        // association forever. Gating on completion keeps both roles terminal here.
        if self.current_flight.is_last_send_flight() || self.is_handshake_completed() {
            Ok(HandshakeState::Finished)
        } else {
            // RFC 6347 4.2.4.1: the timer's current value, which doubles at each expiry.
            self.current_retransmit_timer = Some(now + self.current_retransmit_interval);
            Ok(HandshakeState::Waiting)
        }
    }

    /// When the handshake is given up, if a limit is configured and it has started.
    pub(crate) fn handshake_deadline(&self) -> Option<Instant> {
        if self.is_handshake_completed() || self.failed {
            return None;
        }
        Some(self.handshake_started? + self.handshake_config.handshake_timeout?)
    }

    /// When this association next needs [`Self::handle_timeout`]: its retransmission timer or,
    /// if sooner, the handshake deadline.
    pub(crate) fn next_timeout(&self) -> Option<Instant> {
        if let Some(v13) = &self.v13 {
            return v13.next_timeout();
        }
        match (self.current_retransmit_timer, self.handshake_deadline()) {
            (Some(timer), Some(deadline)) => Some(timer.min(deadline)),
            (timer, deadline) => timer.or(deadline),
        }
    }

    /// Advances the association's timers to `now`: gives the handshake up at its deadline,
    /// retransmits the current flight when the retransmission timer has expired.
    ///
    /// # Errors
    ///
    /// `ErrHandshakeTimeout` when the handshake did not complete in the configured time. The
    /// association has then failed.
    pub(crate) fn handle_timeout(&mut self, now: Instant) -> Result<()> {
        if self.v13.is_some() {
            return self.drive_dtls13(now, true);
        }
        if self
            .handshake_deadline()
            .is_some_and(|deadline| now >= deadline)
        {
            warn!(
                "[handshake:{}] {} did not complete in time",
                srv_cli_str(self.state.is_client),
                self.current_flight,
            );
            self.current_handshake_state = HandshakeState::Errored;
            self.fail();
            return Err(Error::ErrHandshakeTimeout);
        }
        if self
            .current_retransmit_timer
            .is_some_and(|timer| now >= timer)
        {
            self.current_retransmit_timer = None;
            if !self.is_handshake_completed() {
                self.handshake_timeout(now)?;
            }
        }
        Ok(())
    }
    fn wait(&mut self) -> Result<HandshakeState> {
        if self.handshake_rx.take().is_some() {
            debug!(
                "[handshake:{}] {} received handshake packets",
                srv_cli_str(self.state.is_client),
                self.current_flight
            );
            let result = self.current_flight.parse(
                /*&mut self.handle_queue_tx,*/ &mut self.state,
                &self.cache,
                &self.handshake_config,
            );
            match result {
                Err((alert, err)) => {
                    debug!(
                        "[handshake:{}] {} result alert:{:?}, err:{:?}",
                        srv_cli_str(self.state.is_client),
                        self.current_flight,
                        alert,
                        err
                    );

                    if let Some(alert) = alert {
                        self.notify(alert.alert_level, alert.alert_description);
                    }
                    if let Some(err) = err {
                        // A flight that failed (a fatal alert has been sent) stays failed: it is
                        // never parsed again, however many more handshake records arrive.
                        self.current_retransmit_timer = None;
                        self.current_handshake_state = HandshakeState::Errored;
                        return Err(err);
                    }
                }
                Ok(next_flight) => {
                    // A partial or repeated flight must not cancel retransmission.
                    // Only a complete next flight advances the handshake (RFC 6347 4.2.4).
                    self.current_retransmit_timer = None;
                    // RFC 6347 4.2.4.1: the backed-off timer value is kept until a flight gets
                    // through without loss, and this one did if it was never retransmitted.
                    if self.current_retransmit_count == 0 {
                        self.current_retransmit_interval =
                            self.handshake_config.retransmit_interval;
                    }
                    debug!(
                        "[handshake:{}] {} -> {}",
                        srv_cli_str(self.state.is_client),
                        self.current_flight,
                        next_flight
                    );
                    if next_flight.is_last_recv_flight()
                        && self.current_flight.to_string() == next_flight.to_string()
                    {
                        return Ok(HandshakeState::Finished);
                    }
                    if self.current_flight.verifies_cookie()
                        && !next_flight.verifies_cookie()
                        && let Err(err) = self.on_peer_address_verified()
                    {
                        self.notify(AlertLevel::Fatal, AlertDescription::IllegalParameter);
                        return Err(err);
                    }
                    self.current_flight = next_flight;
                    return Ok(HandshakeState::Preparing);
                }
            }
        }

        Ok(HandshakeState::Waiting)
    }

    /// The client has echoed our cookie, so the address it sends from is its own (RFC 6347
    /// 4.2.1). Only now does a server do the per-association work a spoofed source address
    /// must not be able to cause: its ECDHE key pair.
    fn on_peer_address_verified(&mut self) -> Result<()> {
        self.address_verified = true;
        if self.state.local_keypair.is_none() {
            let keypair = self
                .state
                .named_curve
                .generate_keypair_with_crypto(self.handshake_config.provider().crypto())?;
            self.state.local_keypair = Some(keypair);
        }
        Ok(())
    }

    fn finish(&mut self) -> Result<HandshakeState> {
        if self.handshake_rx.take().is_some() {
            debug!(
                "[handshake:{}] {} received handshake packets",
                srv_cli_str(self.state.is_client),
                self.current_flight
            );
            self.current_retransmit_timer = None;
            let result = self.current_flight.parse(
                /*&mut self.handle_queue_tx,*/ &mut self.state,
                &self.cache,
                &self.handshake_config,
            );
            if let Err((alert, err)) = result {
                if let Some(alert) = alert {
                    self.notify(alert.alert_level, alert.alert_description);
                }
                if let Some(err) = err {
                    return Err(err);
                }
            };
        }

        Ok(HandshakeState::Finished)
    }

    pub(crate) fn handshake_timeout(&mut self, now: Instant) -> Result<()> {
        self.state.clock = Some(now);
        let next_handshake_state = if self.current_handshake_state == HandshakeState::Waiting {
            debug!(
                "[handshake:{}] {} retransmit_timer",
                srv_cli_str(self.state.is_client),
                self.current_flight
            );
            debug!(
                "[handshake:{}] {} current_retransmit_count {} vs maximum_retransmit_number {}",
                srv_cli_str(self.state.is_client),
                self.current_flight,
                self.current_retransmit_count,
                self.maximum_retransmit_number,
            );
            if self.retransmit {
                // RFC 6347 4.2.4.1: retransmit, and double the timer up to its maximum. There
                // is no count of retransmissions to run out of: the handshake deadline
                // (`handle_timeout`) is what gives a handshake up.
                self.current_retransmit_count += 1;
                self.current_retransmit_interval = (self.current_retransmit_interval * 2)
                    .min(self.handshake_config.maximum_retransmit_interval)
                    .max(self.handshake_config.retransmit_interval);
                // RFC 6347 4.1.1.1: when retransmissions go unanswered the records may be too
                // large for the path, so they are cut smaller.
                if self.current_retransmit_count > RETRANSMITS_BEFORE_MTU_BACKOFF {
                    self.handshake_mtu = (self.handshake_mtu / 2)
                        .max(FALLBACK_HANDSHAKE_MTU)
                        .min(self.handshake_mtu);
                }
                Some(HandshakeState::Sending)
            } else {
                self.current_retransmit_timer =
                    Some(now + self.handshake_config.retransmit_interval);
                Some(HandshakeState::Waiting)
            }
        } else if self.current_handshake_state == HandshakeState::Finished {
            // RFC 6347 4.2.4: retransmit our last flight because the peer repeated its own, which
            // means ours was lost. Count it against the same budget as any handshake
            // retransmission, so a peer — or an attacker replaying unauthenticated epoch-0
            // handshake records — cannot make a completed association regenerate its flight (a full
            // Flight5 with certificate, for a client) without limit. Once the budget is spent, stop
            // answering and stay Finished: the association is established, so exhausting
            // retransmissions here must never take it to Errored the way the Waiting branch does —
            // that would tear down a live connection.
            self.current_retransmit_count += 1;
            if self.current_retransmit_count > self.maximum_retransmit_number {
                warn!(
                    "[handshake:{}] {} last-flight retransmit budget spent ({} > {}), staying Finished",
                    srv_cli_str(self.state.is_client),
                    self.current_flight,
                    self.current_retransmit_count,
                    self.maximum_retransmit_number,
                );
                None
            } else {
                Some(HandshakeState::Sending)
            }
        } else {
            None
        };

        if let Some(next_handshake_state) = next_handshake_state {
            self.current_handshake_state = next_handshake_state;
            self.handshake(now)
        } else {
            Ok(())
        }
    }
}