rtci-dtls 0.2.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
//! The TLS 1.3 key schedule (RFC 8446 section 7) with the DTLS 1.3 label prefix (RFC 9147
//! section 5.9), and the handshake transcript it is keyed to.

use super::suite::Suite13;
use crypto::{RTCCrypto, SecretVec, hkdf_expand_label, hkdf_extract};
use shared::error::{Error, Result};

/// The label prefix of DTLS 1.3 (RFC 9147 section 5.9): no trailing space, unlike TLS 1.3's.
pub(crate) const DTLS13_LABEL_PREFIX: &str = "dtls13";
/// The label prefix of TLS 1.3 (RFC 8446 section 7.1), which the RFC 8448 traces use.
#[cfg(test)]
pub(crate) const TLS13_LABEL_PREFIX: &str = "tls13 ";

/// The handshake type of the synthetic message that stands for the first ClientHello after a
/// HelloRetryRequest (RFC 8446 section 4.4.1).
pub(crate) const HANDSHAKE_TYPE_MESSAGE_HASH: u8 = 254;

fn crypto_error(error: crypto::CryptoError) -> Error {
    Error::Crypto(error.to_string())
}

/// The handshake transcript: the TLS 1.3 form of every handshake message so far, that is,
/// type, 24-bit length and body, without the DTLS message_seq and fragment fields (RFC 9147
/// section 5.2).
#[derive(Clone, Default)]
pub(crate) struct Transcript {
    bytes: Vec<u8>,
}

impl Transcript {
    /// Appends one whole handshake message.
    pub(crate) fn add(&mut self, handshake_type: u8, body: &[u8]) {
        self.bytes.push(handshake_type);
        self.bytes
            .extend_from_slice(&(body.len() as u32).to_be_bytes()[1..]);
        self.bytes.extend_from_slice(body);
    }

    /// Replaces what has been recorded, which is the first ClientHello, with the synthetic
    /// message_hash message that carries its hash: what a HelloRetryRequest does to the
    /// transcript (RFC 8446 section 4.4.1).
    pub(crate) fn replace_with_message_hash(
        &mut self,
        crypto: &dyn RTCCrypto,
        suite: Suite13,
    ) -> Result<()> {
        let hash = self.hash(crypto, suite)?;
        self.set_message_hash(&hash);
        Ok(())
    }

    /// Starts the transcript from the hash of a first ClientHello this side no longer has:
    /// a server that kept no state for it reads the hash back from its cookie (RFC 9147
    /// section 5.1).
    pub(crate) fn set_message_hash(&mut self, client_hello_hash: &[u8]) {
        self.bytes.clear();
        self.add(HANDSHAKE_TYPE_MESSAGE_HASH, client_hello_hash);
    }

    /// `Transcript-Hash` of everything recorded.
    pub(crate) fn hash(&self, crypto: &dyn RTCCrypto, suite: Suite13) -> Result<Vec<u8>> {
        crypto.hash(suite.hash(), &self.bytes).map_err(crypto_error)
    }

    /// `Transcript-Hash` of everything recorded followed by `more`.
    pub(crate) fn hash_with(
        &self,
        crypto: &dyn RTCCrypto,
        suite: Suite13,
        more: &[u8],
    ) -> Result<Vec<u8>> {
        let mut bytes = self.bytes.clone();
        bytes.extend_from_slice(more);
        crypto.hash(suite.hash(), &bytes).map_err(crypto_error)
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.bytes.is_empty()
    }
}

/// The secrets of one handshake, derived stage by stage as RFC 8446 section 7.1 draws them.
pub(crate) struct KeySchedule {
    suite: Suite13,
    prefix: &'static str,
    /// The Early Secret, then the Handshake Secret, then the Main Secret.
    current: SecretVec,
    pub(crate) client_handshake_traffic: Option<SecretVec>,
    pub(crate) server_handshake_traffic: Option<SecretVec>,
    pub(crate) client_application_traffic: Option<SecretVec>,
    pub(crate) server_application_traffic: Option<SecretVec>,
    pub(crate) exporter: Option<SecretVec>,
}

impl KeySchedule {
    /// Starts the schedule with no PSK: the Early Secret is extracted from a string of zeros.
    pub(crate) fn new(
        crypto: &dyn RTCCrypto,
        suite: Suite13,
        prefix: &'static str,
    ) -> Result<Self> {
        let zeros = vec![0u8; suite.hash_len()];
        Self::with_psk(crypto, suite, prefix, &zeros)
    }

    /// Starts the schedule from a PSK: the Early Secret is `HKDF-Extract(0, PSK)` (RFC 8446
    /// section 7.1).
    pub(crate) fn with_psk(
        crypto: &dyn RTCCrypto,
        suite: Suite13,
        prefix: &'static str,
        psk: &[u8],
    ) -> Result<Self> {
        let early = hkdf_extract(crypto, suite.hmac(), &[], psk).map_err(crypto_error)?;
        Ok(Self {
            suite,
            prefix,
            current: early,
            client_handshake_traffic: None,
            server_handshake_traffic: None,
            client_application_traffic: None,
            server_application_traffic: None,
            exporter: None,
        })
    }

    pub(crate) fn suite(&self) -> Suite13 {
        self.suite
    }

    fn expand_label(
        &self,
        crypto: &dyn RTCCrypto,
        secret: &[u8],
        label: &str,
        context: &[u8],
        length: usize,
    ) -> Result<SecretVec> {
        hkdf_expand_label(
            crypto,
            self.suite.hmac(),
            secret,
            self.prefix,
            label,
            context,
            length,
        )
        .map_err(crypto_error)
    }

    /// `Derive-Secret(Secret, Label, Messages)`, given the transcript hash of the messages.
    fn derive_secret(
        &self,
        crypto: &dyn RTCCrypto,
        secret: &[u8],
        label: &str,
        transcript_hash: &[u8],
    ) -> Result<SecretVec> {
        self.expand_label(
            crypto,
            secret,
            label,
            transcript_hash,
            self.suite.hash_len(),
        )
    }

    /// `binder_key = Derive-Secret(Early Secret, "ext binder" | "res binder", "")` (RFC 8446
    /// section 7.1): for a PSK provisioned out of band, or one from a ticket.
    pub(crate) fn binder_key(&self, crypto: &dyn RTCCrypto, external: bool) -> Result<SecretVec> {
        let empty_hash = crypto.hash(self.suite.hash(), &[]).map_err(crypto_error)?;
        let label = if external { "ext binder" } else { "res binder" };
        self.derive_secret(crypto, self.current.as_ref(), label, &empty_hash)
    }

    /// `client_early_traffic_secret = Derive-Secret(Early Secret, "c e traffic", ClientHello)`
    /// (RFC 8446 section 7.1): the keys of 0-RTT data, epoch 1 (RFC 9147 section 6.1).
    pub(crate) fn client_early_traffic_secret(
        &self,
        crypto: &dyn RTCCrypto,
        client_hello_hash: &[u8],
    ) -> Result<SecretVec> {
        self.derive_secret(
            crypto,
            self.current.as_ref(),
            "c e traffic",
            client_hello_hash,
        )
    }

    /// Moves from the current secret to the next one in the chain:
    /// `HKDF-Extract(Derive-Secret(current, "derived", ""), input)`.
    fn advance(&mut self, crypto: &dyn RTCCrypto, input: &[u8]) -> Result<()> {
        let empty_hash = crypto.hash(self.suite.hash(), &[]).map_err(crypto_error)?;
        let salt = self.derive_secret(crypto, self.current.as_ref(), "derived", &empty_hash)?;
        self.current =
            hkdf_extract(crypto, self.suite.hmac(), salt.as_ref(), input).map_err(crypto_error)?;
        Ok(())
    }

    /// Derives the Handshake Secret from the (EC)DHE shared secret, and from it the two
    /// handshake traffic secrets, over the transcript up to and including the ServerHello.
    pub(crate) fn enter_handshake(
        &mut self,
        crypto: &dyn RTCCrypto,
        shared_secret: &[u8],
        transcript_hash: &[u8],
    ) -> Result<()> {
        self.advance(crypto, shared_secret)?;
        self.client_handshake_traffic = Some(self.derive_secret(
            crypto,
            self.current.as_ref(),
            "c hs traffic",
            transcript_hash,
        )?);
        self.server_handshake_traffic = Some(self.derive_secret(
            crypto,
            self.current.as_ref(),
            "s hs traffic",
            transcript_hash,
        )?);
        Ok(())
    }

    /// Derives the Main Secret, and from it the first application traffic secrets and the
    /// exporter secret, over the transcript up to and including the server's Finished.
    pub(crate) fn enter_application(
        &mut self,
        crypto: &dyn RTCCrypto,
        transcript_hash: &[u8],
    ) -> Result<()> {
        let zeros = vec![0u8; self.suite.hash_len()];
        self.advance(crypto, &zeros)?;
        self.client_application_traffic = Some(self.derive_secret(
            crypto,
            self.current.as_ref(),
            "c ap traffic",
            transcript_hash,
        )?);
        self.server_application_traffic = Some(self.derive_secret(
            crypto,
            self.current.as_ref(),
            "s ap traffic",
            transcript_hash,
        )?);
        self.exporter = Some(self.derive_secret(
            crypto,
            self.current.as_ref(),
            "exp master",
            transcript_hash,
        )?);
        // The Main Secret stays for the resumption master secret, which comes from the
        // transcript through the client's Finished.
        Ok(())
    }

    /// `resumption_master_secret = Derive-Secret(Main Secret, "res master", ClientHello ...
    /// client Finished)` (RFC 8446 section 7.1). The Main Secret has no other use left, so it
    /// is dropped.
    pub(crate) fn take_resumption_master_secret(
        &mut self,
        crypto: &dyn RTCCrypto,
        transcript_hash: &[u8],
    ) -> Result<SecretVec> {
        if self.current.is_empty() || self.exporter.is_none() {
            return Err(Error::ErrCipherSuiteUnset);
        }
        let secret =
            self.derive_secret(crypto, self.current.as_ref(), "res master", transcript_hash);
        self.current = SecretVec::new(vec![]);
        secret
    }

    /// The PSK of one ticket: `HKDF-Expand-Label(resumption_master_secret, "resumption",
    /// ticket_nonce, Hash.length)` (RFC 8446 section 4.6.1).
    pub(crate) fn resumption_psk(
        &self,
        crypto: &dyn RTCCrypto,
        resumption_master_secret: &[u8],
        ticket_nonce: &[u8],
    ) -> Result<SecretVec> {
        self.expand_label(
            crypto,
            resumption_master_secret,
            "resumption",
            ticket_nonce,
            self.suite.hash_len(),
        )
    }

    /// The verify_data of a Finished: `HMAC(finished_key, Transcript-Hash)`, the key expanded
    /// from the sender's handshake traffic secret (RFC 8446 section 4.4.4).
    pub(crate) fn finished_verify_data(
        &self,
        crypto: &dyn RTCCrypto,
        base_key: &[u8],
        transcript_hash: &[u8],
    ) -> Result<Vec<u8>> {
        let finished_key =
            self.expand_label(crypto, base_key, "finished", &[], self.suite.hash_len())?;
        let mut mac = crypto
            .new_hmac(self.suite.hmac(), finished_key.as_ref())
            .map_err(crypto_error)?;
        let mut verify_data = vec![0u8; mac.output_len()];
        mac.sign(&[transcript_hash], &mut verify_data)
            .map_err(crypto_error)?;
        Ok(verify_data)
    }

    /// `application_traffic_secret_N+1` from `application_traffic_secret_N` (RFC 8446
    /// section 7.2): what a KeyUpdate moves a direction to.
    pub(crate) fn next_application_traffic_secret(
        &self,
        crypto: &dyn RTCCrypto,
        secret: &[u8],
    ) -> Result<SecretVec> {
        self.expand_label(crypto, secret, "traffic upd", &[], self.suite.hash_len())
    }

    /// The key, IV and record-number key of a traffic secret (RFC 8446 section 7.3, RFC 9147
    /// section 4.2.3).
    pub(crate) fn traffic_keys(
        &self,
        crypto: &dyn RTCCrypto,
        secret: &[u8],
    ) -> Result<TrafficKeys> {
        Ok(TrafficKeys {
            key: self.expand_label(crypto, secret, "key", &[], self.suite.key_len())?,
            iv: self.expand_label(crypto, secret, "iv", &[], 12)?,
            sequence_number_key: self.expand_label(
                crypto,
                secret,
                "sn",
                &[],
                self.suite.header_mask().key_len(),
            )?,
        })
    }

    /// `TLS-Exporter(label, context_value, key_length)` (RFC 8446 section 7.5).
    pub(crate) fn export(
        &self,
        crypto: &dyn RTCCrypto,
        exporter_secret: &[u8],
        label: &str,
        context: &[u8],
        length: usize,
    ) -> Result<SecretVec> {
        let empty_hash = crypto.hash(self.suite.hash(), &[]).map_err(crypto_error)?;
        let secret = self.derive_secret(crypto, exporter_secret, label, &empty_hash)?;
        let context_hash = crypto
            .hash(self.suite.hash(), context)
            .map_err(crypto_error)?;
        self.expand_label(crypto, secret.as_ref(), "exporter", &context_hash, length)
    }

    /// Overwrites and drops every secret (RFC 8446 section 6.2: a failed connection's
    /// secrets are forgotten).
    /// Whether [`Self::forget`] has run: no secret is left.
    #[cfg(test)]
    pub(crate) fn is_forgotten(&self) -> bool {
        self.current.as_ref().is_empty()
            && self.client_handshake_traffic.is_none()
            && self.server_handshake_traffic.is_none()
            && self.client_application_traffic.is_none()
            && self.server_application_traffic.is_none()
            && self.exporter.is_none()
    }

    /// RFC 8446 section 7.1: "Once all the values which are to be derived from a given secret
    /// have been computed, that secret SHOULD be erased." The handshake traffic secrets give
    /// the handshake epoch's record keys and the two Finished messages: once both Finished are
    /// computed (sent or checked), nothing more comes from them.
    pub(crate) fn forget_handshake_secrets(&mut self) {
        self.client_handshake_traffic = None;
        self.server_handshake_traffic = None;
    }

    /// Whether the handshake traffic secrets are gone ([`Self::forget_handshake_secrets`]).
    #[cfg(test)]
    pub(crate) fn handshake_secrets_forgotten(&self) -> bool {
        self.client_handshake_traffic.is_none() && self.server_handshake_traffic.is_none()
    }

    /// Whether the application traffic secrets were handed over (the association keeps them
    /// for its key updates) and no copy is left here.
    #[cfg(test)]
    pub(crate) fn application_secrets_handed_over(&self) -> bool {
        self.client_application_traffic.is_none() && self.server_application_traffic.is_none()
    }

    pub(crate) fn forget(&mut self) {
        self.current = SecretVec::new(vec![]);
        self.client_handshake_traffic = None;
        self.server_handshake_traffic = None;
        self.client_application_traffic = None;
        self.server_application_traffic = None;
        self.exporter = None;
    }
}

/// The keys of one direction in one epoch.
pub(crate) struct TrafficKeys {
    pub(crate) key: SecretVec,
    pub(crate) iv: SecretVec,
    pub(crate) sequence_number_key: SecretVec,
}

/// What a CertificateVerify signs (RFC 8446 section 4.4.3): 64 spaces, the context string, a
/// zero byte and the transcript hash.
pub(crate) fn certificate_verify_input(from_server: bool, transcript_hash: &[u8]) -> Vec<u8> {
    let context: &[u8] = if from_server {
        b"TLS 1.3, server CertificateVerify"
    } else {
        b"TLS 1.3, client CertificateVerify"
    };
    let mut input = vec![0x20u8; 64];
    input.extend_from_slice(context);
    input.push(0);
    input.extend_from_slice(transcript_hash);
    input
}

/// `TLS-Exporter` for a completed DTLS 1.3 handshake, from its exporter secret.
pub(crate) fn export_keying_material(
    crypto: &dyn RTCCrypto,
    suite: Suite13,
    exporter_secret: &[u8],
    label: &str,
    context: &[u8],
    length: usize,
) -> Result<SecretVec> {
    let empty_hash = crypto.hash(suite.hash(), &[]).map_err(crypto_error)?;
    let expand = |secret: &[u8], label: &str, context: &[u8], length: usize| {
        hkdf_expand_label(
            crypto,
            suite.hmac(),
            secret,
            DTLS13_LABEL_PREFIX,
            label,
            context,
            length,
        )
        .map_err(crypto_error)
    };
    let secret = expand(exporter_secret, label, &empty_hash, suite.hash_len())?;
    let context_hash = crypto.hash(suite.hash(), context).map_err(crypto_error)?;
    expand(secret.as_ref(), "exporter", &context_hash, length)
}