Coverage Report

Created: 2026-06-15 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rustls-0.23.40/src/client/ech.rs
Line
Count
Source
1
use alloc::boxed::Box;
2
use alloc::vec;
3
use alloc::vec::Vec;
4
use core::iter;
5
6
use pki_types::{DnsName, EchConfigListBytes, ServerName};
7
use subtle::ConstantTimeEq;
8
9
use crate::CipherSuite::TLS_EMPTY_RENEGOTIATION_INFO_SCSV;
10
use crate::client::tls13;
11
use crate::crypto::SecureRandom;
12
use crate::crypto::hash::Hash;
13
use crate::crypto::hpke::{EncapsulatedSecret, Hpke, HpkePublicKey, HpkeSealer, HpkeSuite};
14
use crate::hash_hs::{HandshakeHash, HandshakeHashBuffer};
15
use crate::log::{debug, trace, warn};
16
use crate::msgs::base::{Payload, PayloadU16};
17
use crate::msgs::codec::{Codec, Reader};
18
use crate::msgs::enums::{ExtensionType, HpkeKem};
19
use crate::msgs::handshake::{
20
    ClientExtensions, ClientHelloPayload, EchConfigContents, EchConfigPayload, Encoding,
21
    EncryptedClientHello, EncryptedClientHelloOuter, HandshakeMessagePayload, HandshakePayload,
22
    HelloRetryRequest, HpkeKeyConfig, HpkeSymmetricCipherSuite, PresharedKeyBinder,
23
    PresharedKeyOffer, Random, ServerHelloPayload, ServerNamePayload,
24
};
25
use crate::msgs::message::{Message, MessagePayload};
26
use crate::msgs::persist;
27
use crate::msgs::persist::Retrieved;
28
use crate::tls13::key_schedule::{
29
    KeyScheduleEarly, KeyScheduleHandshakeStart, server_ech_hrr_confirmation_secret,
30
};
31
use crate::{
32
    AlertDescription, ClientConfig, CommonState, EncryptedClientHelloError, Error,
33
    PeerIncompatible, PeerMisbehaved, ProtocolVersion, Tls13CipherSuite,
34
};
35
36
/// Controls how Encrypted Client Hello (ECH) is used in a client handshake.
37
#[derive(Clone, Debug)]
38
pub enum EchMode {
39
    /// ECH is enabled and the ClientHello will be encrypted based on the provided
40
    /// configuration.
41
    Enable(EchConfig),
42
43
    /// No ECH configuration is available but the client should act as though it were.
44
    ///
45
    /// This is an anti-ossification measure, sometimes referred to as "GREASE"[^0].
46
    /// [^0]: <https://www.rfc-editor.org/rfc/rfc8701>
47
    Grease(EchGreaseConfig),
48
}
49
50
impl EchMode {
51
    /// Returns true if the ECH mode will use a FIPS approved HPKE suite.
52
0
    pub fn fips(&self) -> bool {
53
0
        match self {
54
0
            Self::Enable(ech_config) => ech_config.suite.fips(),
55
0
            Self::Grease(grease_config) => grease_config.suite.fips(),
56
        }
57
0
    }
58
}
59
60
impl From<EchConfig> for EchMode {
61
0
    fn from(config: EchConfig) -> Self {
62
0
        Self::Enable(config)
63
0
    }
64
}
65
66
impl From<EchGreaseConfig> for EchMode {
67
0
    fn from(config: EchGreaseConfig) -> Self {
68
0
        Self::Grease(config)
69
0
    }
70
}
71
72
/// Configuration for performing encrypted client hello.
73
///
74
/// Note: differs from the protocol-encoded EchConfig (`EchConfigMsg`).
75
#[derive(Clone, Debug)]
76
pub struct EchConfig {
77
    /// The selected EchConfig.
78
    pub(crate) config: EchConfigPayload,
79
80
    /// An HPKE instance corresponding to a suite from the `config` we have selected as
81
    /// a compatible choice.
82
    pub(crate) suite: &'static dyn Hpke,
83
}
84
85
impl EchConfig {
86
    /// Construct an EchConfig by selecting a ECH config from the provided bytes that is compatible
87
    /// with one of the given HPKE suites.
88
    ///
89
    /// The config list bytes should be sourced from a DNS-over-HTTPS lookup resolving the `HTTPS`
90
    /// resource record for the host name of the server you wish to connect via ECH,
91
    /// and extracting the ECH configuration from the `ech` parameter. The extracted bytes should
92
    /// be base64 decoded to yield the `EchConfigListBytes` you provide to rustls.
93
    ///
94
    /// One of the provided ECH configurations must be compatible with the HPKE provider's supported
95
    /// suites or an error will be returned.
96
    ///
97
    /// See the [`ech-client.rs`] example for a complete example of fetching ECH configs from DNS.
98
    ///
99
    /// [`ech-client.rs`]: https://github.com/rustls/rustls/blob/main/examples/src/bin/ech-client.rs
100
0
    pub fn new(
101
0
        ech_config_list: EchConfigListBytes<'_>,
102
0
        hpke_suites: &[&'static dyn Hpke],
103
0
    ) -> Result<Self, Error> {
104
0
        let ech_configs = Vec::<EchConfigPayload>::read(&mut Reader::init(&ech_config_list))
105
0
            .map_err(|_| {
106
0
                Error::InvalidEncryptedClientHello(EncryptedClientHelloError::InvalidConfigList)
107
0
            })?;
108
109
        // Note: we name the index var _i because if the log feature is disabled
110
        //       it is unused.
111
        #[cfg_attr(not(feature = "logging"), allow(clippy::unused_enumerate_index))]
112
0
        for (_i, config) in ech_configs.iter().enumerate() {
113
0
            let contents = match config {
114
0
                EchConfigPayload::V18(contents) => contents,
115
                EchConfigPayload::Unknown {
116
0
                    version: _version, ..
117
                } => {
118
0
                    warn!(
119
0
                        "ECH config {} has unsupported version {:?}",
120
0
                        _i + 1,
121
                        _version
122
                    );
123
0
                    continue; // Unsupported version.
124
                }
125
            };
126
127
0
            if contents.has_unknown_mandatory_extension() || contents.has_duplicate_extension() {
128
0
                warn!("ECH config has duplicate, or unknown mandatory extensions: {contents:?}",);
129
0
                continue; // Unsupported, or malformed extensions.
130
0
            }
131
132
0
            let key_config = &contents.key_config;
133
0
            for cipher_suite in &key_config.symmetric_cipher_suites {
134
0
                if cipher_suite.aead_id.tag_len().is_none() {
135
0
                    continue; // Unsupported EXPORT_ONLY AEAD cipher suite.
136
0
                }
137
138
0
                let suite = HpkeSuite {
139
0
                    kem: key_config.kem_id,
140
0
                    sym: *cipher_suite,
141
0
                };
142
0
                if let Some(hpke) = hpke_suites
143
0
                    .iter()
144
0
                    .find(|hpke| hpke.suite() == suite)
145
                {
146
0
                    debug!(
147
0
                        "selected ECH config ID {:?} suite {:?} public_name {:?}",
148
                        key_config.config_id, suite, contents.public_name
149
                    );
150
0
                    return Ok(Self {
151
0
                        config: config.clone(),
152
0
                        suite: *hpke,
153
0
                    });
154
0
                }
155
            }
156
        }
157
158
0
        Err(EncryptedClientHelloError::NoCompatibleConfig.into())
159
0
    }
160
161
0
    pub(super) fn state(
162
0
        &self,
163
0
        server_name: ServerName<'static>,
164
0
        config: &ClientConfig,
165
0
    ) -> Result<EchState, Error> {
166
0
        EchState::new(
167
0
            self,
168
0
            server_name.clone(),
169
0
            config
170
0
                .client_auth_cert_resolver
171
0
                .has_certs(),
172
0
            config.provider.secure_random,
173
0
            config.enable_sni,
174
        )
175
0
    }
176
177
    /// Compute the HPKE `SetupBaseS` `info` parameter for this ECH configuration.
178
    ///
179
    /// See <https://datatracker.ietf.org/doc/html/draft-ietf-tls-esni-17#section-6.1>.
180
0
    pub(crate) fn hpke_info(&self) -> Vec<u8> {
181
0
        let mut info = Vec::with_capacity(128);
182
        // "tls ech" || 0x00 || ECHConfig
183
0
        info.extend_from_slice(b"tls ech\0");
184
0
        self.config.encode(&mut info);
185
0
        info
186
0
    }
187
}
188
189
/// Configuration for GREASE Encrypted Client Hello.
190
#[derive(Clone, Debug)]
191
pub struct EchGreaseConfig {
192
    pub(crate) suite: &'static dyn Hpke,
193
    pub(crate) placeholder_key: HpkePublicKey,
194
}
195
196
impl EchGreaseConfig {
197
    /// Construct a GREASE ECH configuration.
198
    ///
199
    /// This configuration is used when the client wishes to offer ECH to prevent ossification,
200
    /// but doesn't have a real ECH configuration to use for the remote server. In this case
201
    /// a placeholder or "GREASE"[^0] extension is used.
202
    ///
203
    /// Returns an error if the HPKE provider does not support the given suite.
204
    ///
205
    /// [^0]: <https://www.rfc-editor.org/rfc/rfc8701>
206
0
    pub fn new(suite: &'static dyn Hpke, placeholder_key: HpkePublicKey) -> Self {
207
0
        Self {
208
0
            suite,
209
0
            placeholder_key,
210
0
        }
211
0
    }
212
213
    /// Build a GREASE ECH extension based on the placeholder configuration.
214
    ///
215
    /// See <https://datatracker.ietf.org/doc/html/draft-ietf-tls-esni-18#name-grease-ech> for
216
    /// more information.
217
0
    pub(crate) fn grease_ext(
218
0
        &self,
219
0
        secure_random: &'static dyn SecureRandom,
220
0
        inner_name: ServerName<'static>,
221
0
        outer_hello: &ClientHelloPayload,
222
0
    ) -> Result<EncryptedClientHello, Error> {
223
0
        trace!("Preparing GREASE ECH extension");
224
225
        // Pick a random config id.
226
0
        let mut config_id: [u8; 1] = [0; 1];
227
0
        secure_random.fill(&mut config_id[..])?;
228
229
0
        let suite = self.suite.suite();
230
231
        // Construct a dummy ECH state - we don't have a real ECH config from a server since
232
        // this is for GREASE.
233
0
        let mut grease_state = EchState::new(
234
0
            &EchConfig {
235
0
                config: EchConfigPayload::V18(EchConfigContents {
236
0
                    key_config: HpkeKeyConfig {
237
0
                        config_id: config_id[0],
238
0
                        kem_id: HpkeKem::DHKEM_P256_HKDF_SHA256,
239
0
                        public_key: PayloadU16::new(self.placeholder_key.0.clone()),
240
0
                        symmetric_cipher_suites: vec![suite.sym],
241
0
                    },
242
0
                    maximum_name_length: 0,
243
0
                    public_name: DnsName::try_from("filler").unwrap(),
244
0
                    extensions: Vec::default(),
245
0
                }),
246
0
                suite: self.suite,
247
0
            },
248
0
            inner_name,
249
            false,
250
0
            secure_random,
251
            false, // Does not matter if we enable/disable SNI here. Inner hello is not used.
252
0
        )?;
253
254
        // Construct an inner hello using the outer hello - this allows us to know the size of
255
        // dummy payload we should use for the GREASE extension.
256
0
        let encoded_inner_hello = grease_state.encode_inner_hello(outer_hello, None, &None);
257
258
        // Generate a payload of random data equivalent in length to a real inner hello.
259
0
        let payload_len = encoded_inner_hello.len()
260
0
            + suite
261
0
                .sym
262
0
                .aead_id
263
0
                .tag_len()
264
0
                // Safety: we have confirmed the AEAD is supported when building the config. All
265
0
                //  supported AEADs have a tag length.
266
0
                .unwrap();
267
0
        let mut payload = vec![0; payload_len];
268
0
        secure_random.fill(&mut payload)?;
269
270
        // Return the GREASE extension.
271
0
        Ok(EncryptedClientHello::Outer(EncryptedClientHelloOuter {
272
0
            cipher_suite: suite.sym,
273
0
            config_id: config_id[0],
274
0
            enc: PayloadU16::new(grease_state.enc.0),
275
0
            payload: PayloadU16::new(payload),
276
0
        }))
277
0
    }
278
}
279
280
/// An enum representing ECH offer status.
281
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
282
pub enum EchStatus {
283
    /// ECH was not offered - it is a normal TLS handshake.
284
    NotOffered,
285
    /// GREASE ECH was sent. This is not considered offering ECH.
286
    Grease,
287
    /// ECH was offered but we do not yet know whether the offer was accepted or rejected.
288
    Offered,
289
    /// ECH was offered and the server accepted.
290
    Accepted,
291
    /// ECH was offered and the server rejected.
292
    Rejected,
293
}
294
295
/// Contextual data for a TLS client handshake that has offered encrypted client hello (ECH).
296
pub(crate) struct EchState {
297
    // The public DNS name from the ECH configuration we've chosen - this is included as the SNI
298
    // value for the "outer" client hello. It can only be a DnsName, not an IP address.
299
    pub(crate) outer_name: DnsName<'static>,
300
    // If we're resuming in the inner hello, this is the early key schedule to use for encrypting
301
    // early data if the ECH offer is accepted.
302
    pub(crate) early_data_key_schedule: Option<KeyScheduleEarly>,
303
    // A random value we use for the inner hello.
304
    pub(crate) inner_hello_random: Random,
305
    // A transcript buffer maintained for the inner hello. Once ECH is confirmed we switch to
306
    // using this transcript for the handshake.
307
    pub(crate) inner_hello_transcript: HandshakeHashBuffer,
308
    // A source of secure random data.
309
    secure_random: &'static dyn SecureRandom,
310
    // An HPKE sealer context that can be used for encrypting ECH data.
311
    sender: Box<dyn HpkeSealer>,
312
    // The ID of the ECH configuration we've chosen - this is included in the outer ECH extension.
313
    config_id: u8,
314
    // The private server name we'll use for the inner protected hello.
315
    inner_name: ServerName<'static>,
316
    // The advertised maximum name length from the ECH configuration we've chosen - this is used
317
    // for padding calculations.
318
    maximum_name_length: u8,
319
    // A supported symmetric cipher suite from the ECH configuration we've chosen - this is
320
    // included in the outer ECH extension.
321
    cipher_suite: HpkeSymmetricCipherSuite,
322
    // A secret encapsulated to the public key of the remote server. This is included in the
323
    // outer ECH extension for non-retry outer hello messages.
324
    enc: EncapsulatedSecret,
325
    // Whether the inner client hello should contain a server name indication (SNI) extension.
326
    enable_sni: bool,
327
    // The extensions sent in the inner hello.
328
    sent_extensions: Vec<ExtensionType>,
329
}
330
331
impl EchState {
332
0
    pub(crate) fn new(
333
0
        config: &EchConfig,
334
0
        inner_name: ServerName<'static>,
335
0
        client_auth_enabled: bool,
336
0
        secure_random: &'static dyn SecureRandom,
337
0
        enable_sni: bool,
338
0
    ) -> Result<Self, Error> {
339
0
        let EchConfigPayload::V18(config_contents) = &config.config else {
340
            // the public EchConfig::new() constructor ensures we only have supported
341
            // configurations.
342
0
            unreachable!("ECH config version mismatch");
343
        };
344
0
        let key_config = &config_contents.key_config;
345
346
        // Encapsulate a secret for the server's public key, and set up a sender context
347
        // we can use to seal messages.
348
0
        let (enc, sender) = config.suite.setup_sealer(
349
0
            &config.hpke_info(),
350
0
            &HpkePublicKey(key_config.public_key.0.clone()),
351
0
        )?;
352
353
        // Start a new transcript buffer for the inner hello.
354
0
        let mut inner_hello_transcript = HandshakeHashBuffer::new();
355
0
        if client_auth_enabled {
356
0
            inner_hello_transcript.set_client_auth_enabled();
357
0
        }
358
359
        Ok(Self {
360
0
            secure_random,
361
0
            sender,
362
0
            config_id: key_config.config_id,
363
0
            inner_name,
364
0
            outer_name: config_contents.public_name.clone(),
365
0
            maximum_name_length: config_contents.maximum_name_length,
366
0
            cipher_suite: config.suite.suite().sym,
367
0
            enc,
368
0
            inner_hello_random: Random::new(secure_random)?,
369
0
            inner_hello_transcript,
370
0
            early_data_key_schedule: None,
371
0
            enable_sni,
372
0
            sent_extensions: Vec::new(),
373
        })
374
0
    }
375
376
    /// Construct a ClientHelloPayload offering ECH.
377
    ///
378
    /// An outer hello, with a protected inner hello for the `inner_name` will be returned, and the
379
    /// ECH context will be updated to reflect the inner hello that was offered.
380
    ///
381
    /// If `retry_req` is `Some`, then the outer hello will be constructed for a hello retry request.
382
    ///
383
    /// If `resuming` is `Some`, then the inner hello will be constructed for a resumption handshake.
384
0
    pub(crate) fn ech_hello(
385
0
        &mut self,
386
0
        mut outer_hello: ClientHelloPayload,
387
0
        retry_req: Option<&HelloRetryRequest>,
388
0
        resuming: &Option<Retrieved<&persist::Tls13ClientSessionValue>>,
389
0
    ) -> Result<ClientHelloPayload, Error> {
390
0
        trace!(
391
0
            "Preparing ECH offer {}",
392
0
            if retry_req.is_some() { "for retry" } else { "" }
393
        );
394
395
        // Construct the encoded inner hello and update the transcript.
396
0
        let encoded_inner_hello = self.encode_inner_hello(&outer_hello, retry_req, resuming);
397
398
        // Complete the ClientHelloOuterAAD with an ech extension, the payload should be a placeholder
399
        // of size L, all zeroes. L == length of encrypting encoded client hello inner w/ the selected
400
        // HPKE AEAD. (sum of plaintext + tag length, typically).
401
0
        let payload_len = encoded_inner_hello.len()
402
0
            + self
403
0
                .cipher_suite
404
0
                .aead_id
405
0
                .tag_len()
406
0
                // Safety: we've already verified this AEAD is supported when loading the config
407
0
                // that was used to create the ECH context. All supported AEADs have a tag length.
408
0
                .unwrap();
409
410
        // Outer hello's created in response to a hello retry request omit the enc value.
411
0
        let enc = match retry_req.is_some() {
412
0
            true => Vec::default(),
413
0
            false => self.enc.0.clone(),
414
        };
415
416
0
        fn outer_hello_ext(ctx: &EchState, enc: Vec<u8>, payload: Vec<u8>) -> EncryptedClientHello {
417
0
            EncryptedClientHello::Outer(EncryptedClientHelloOuter {
418
0
                cipher_suite: ctx.cipher_suite,
419
0
                config_id: ctx.config_id,
420
0
                enc: PayloadU16::new(enc),
421
0
                payload: PayloadU16::new(payload),
422
0
            })
423
0
        }
424
425
        // The outer handshake is not permitted to resume a session. If we're resuming in the
426
        // inner handshake we remove the PSK extension from the outer hello, replacing it
427
        // with a GREASE PSK to implement the "ClientHello Malleability Mitigation" mentioned
428
        // in 10.12.3.
429
0
        if let Some(psk_offer) = outer_hello.preshared_key_offer.as_mut() {
430
0
            self.grease_psk(psk_offer)?;
431
0
        }
432
433
        // To compute the encoded AAD we add a placeholder extension with an empty payload.
434
0
        outer_hello.encrypted_client_hello =
435
0
            Some(outer_hello_ext(self, enc.clone(), vec![0; payload_len]));
436
437
        // Next we compute the proper extension payload.
438
0
        let payload = self
439
0
            .sender
440
0
            .seal(&outer_hello.get_encoding(), &encoded_inner_hello)?;
441
442
        // And then we replace the placeholder extension with the real one.
443
0
        outer_hello.encrypted_client_hello = Some(outer_hello_ext(self, enc, payload));
444
445
0
        Ok(outer_hello)
446
0
    }
447
448
    /// Confirm whether an ECH offer was accepted based on examining the server hello.
449
0
    pub(crate) fn confirm_acceptance(
450
0
        self,
451
0
        ks: &mut KeyScheduleHandshakeStart,
452
0
        server_hello: &ServerHelloPayload,
453
0
        server_hello_encoded: &Payload<'_>,
454
0
        hash: &'static dyn Hash,
455
0
    ) -> Result<Option<EchAccepted>, Error> {
456
        // Start the inner transcript hash now that we know the hash algorithm to use.
457
0
        let inner_transcript = self
458
0
            .inner_hello_transcript
459
0
            .start_hash(hash);
460
461
        // Fork the transcript that we've started with the inner hello to use for a confirmation step.
462
        // We need to preserve the original inner_transcript to use if this confirmation succeeds.
463
0
        let mut confirmation_transcript = inner_transcript.clone();
464
465
        // Add the server hello confirmation - this is computed by altering the received
466
        // encoding rather than reencoding it.
467
0
        confirmation_transcript
468
0
            .add_message(&Self::server_hello_conf(server_hello, server_hello_encoded));
469
470
        // Derive a confirmation secret from the inner hello random and the confirmation transcript.
471
0
        let derived = ks.server_ech_confirmation_secret(
472
0
            self.inner_hello_random.0.as_ref(),
473
0
            confirmation_transcript.current_hash(),
474
        );
475
476
        // Check that first 8 digits of the derived secret match the last 8 digits of the original
477
        // server random. This match signals that the server accepted the ECH offer.
478
        // Indexing safety: Random is [0; 32] by construction.
479
480
0
        match ConstantTimeEq::ct_eq(derived.as_ref(), server_hello.random.0[24..].as_ref()).into() {
481
            true => {
482
0
                trace!("ECH accepted by server");
483
0
                Ok(Some(EchAccepted {
484
0
                    transcript: inner_transcript,
485
0
                    random: self.inner_hello_random,
486
0
                    sent_extensions: self.sent_extensions,
487
0
                }))
488
            }
489
            false => {
490
0
                trace!("ECH rejected by server");
491
0
                Ok(None)
492
            }
493
        }
494
0
    }
495
496
0
    pub(crate) fn confirm_hrr_acceptance(
497
0
        &self,
498
0
        hrr: &HelloRetryRequest,
499
0
        cs: &Tls13CipherSuite,
500
0
        common: &mut CommonState,
501
0
    ) -> Result<bool, Error> {
502
        // The client checks for the "encrypted_client_hello" extension.
503
0
        let ech_conf = match &hrr.encrypted_client_hello {
504
            // If none is found, the server has implicitly rejected ECH.
505
0
            None => return Ok(false),
506
            // Otherwise, if it has a length other than 8, the client aborts the
507
            // handshake with a "decode_error" alert.
508
0
            Some(ech_conf) if ech_conf.bytes().len() != 8 => {
509
0
                return Err({
510
0
                    common.send_fatal_alert(
511
0
                        AlertDescription::DecodeError,
512
0
                        PeerMisbehaved::IllegalHelloRetryRequestWithInvalidEch,
513
0
                    )
514
0
                });
515
            }
516
0
            Some(ech_conf) => ech_conf,
517
        };
518
519
        // Otherwise the client computes hrr_accept_confirmation as described in Section
520
        // 7.2.1
521
0
        let confirmation_transcript = self.inner_hello_transcript.clone();
522
0
        let mut confirmation_transcript =
523
0
            confirmation_transcript.start_hash(cs.common.hash_provider);
524
0
        confirmation_transcript.rollup_for_hrr();
525
0
        confirmation_transcript.add_message(&Self::hello_retry_request_conf(hrr));
526
527
0
        let derived = server_ech_hrr_confirmation_secret(
528
0
            cs.hkdf_provider,
529
0
            &self.inner_hello_random.0,
530
0
            confirmation_transcript.current_hash(),
531
        );
532
533
0
        match ConstantTimeEq::ct_eq(derived.as_ref(), ech_conf.bytes()).into() {
534
            true => {
535
0
                trace!("ECH accepted by server in hello retry request");
536
0
                Ok(true)
537
            }
538
            false => {
539
0
                trace!("ECH rejected by server in hello retry request");
540
0
                Ok(false)
541
            }
542
        }
543
0
    }
544
545
    /// Update the ECH context inner hello transcript based on a received hello retry request message.
546
    ///
547
    /// This will start the in-progress transcript using the given `hash`, convert it into an HRR
548
    /// buffer, and then add the hello retry message `m`.
549
0
    pub(crate) fn transcript_hrr_update(&mut self, hash: &'static dyn Hash, m: &Message<'_>) {
550
0
        trace!("Updating ECH inner transcript for HRR");
551
552
0
        let inner_transcript = self
553
0
            .inner_hello_transcript
554
0
            .clone()
555
0
            .start_hash(hash);
556
557
0
        let mut inner_transcript_buffer = inner_transcript.into_hrr_buffer();
558
0
        inner_transcript_buffer.add_message(m);
559
0
        self.inner_hello_transcript = inner_transcript_buffer;
560
0
    }
561
562
    // 5.1 "Encoding the ClientHelloInner"
563
0
    fn encode_inner_hello(
564
0
        &mut self,
565
0
        outer_hello: &ClientHelloPayload,
566
0
        retryreq: Option<&HelloRetryRequest>,
567
0
        resuming: &Option<Retrieved<&persist::Tls13ClientSessionValue>>,
568
0
    ) -> Vec<u8> {
569
        // Start building an inner hello using the outer_hello as a template.
570
0
        let mut inner_hello = ClientHelloPayload {
571
            // Some information is copied over as-is.
572
0
            client_version: outer_hello.client_version,
573
0
            session_id: outer_hello.session_id,
574
0
            compression_methods: outer_hello.compression_methods.clone(),
575
576
            // We will build up the included extensions ourselves.
577
0
            extensions: Box::new(ClientExtensions::default()),
578
579
            // Set the inner hello random to the one we generated when creating the ECH state.
580
            // We hold on to the inner_hello_random in the ECH state to use later for confirming
581
            // whether ECH was accepted or not.
582
0
            random: self.inner_hello_random,
583
584
            // We remove the empty renegotiation info SCSV from the outer hello's ciphersuite.
585
            // Similar to the TLS 1.2 specific extensions we will filter out, this is seen as a
586
            // TLS 1.2 only feature by bogo.
587
0
            cipher_suites: outer_hello
588
0
                .cipher_suites
589
0
                .iter()
590
0
                .filter(|cs| **cs != TLS_EMPTY_RENEGOTIATION_INFO_SCSV)
591
0
                .cloned()
592
0
                .collect(),
593
        };
594
595
0
        inner_hello.order_seed = outer_hello.order_seed;
596
597
        // The inner hello will always have an inner variant of the ECH extension added.
598
        // See Section 6.1 rule 4.
599
0
        inner_hello.encrypted_client_hello = Some(EncryptedClientHello::Inner);
600
601
0
        let inner_sni = match &self.inner_name {
602
            // The inner hello only gets a SNI value if enable_sni is true and the inner name
603
            // is a domain name (not an IP address).
604
0
            ServerName::DnsName(dns_name) if self.enable_sni => Some(dns_name),
605
0
            _ => None,
606
        };
607
608
        // Now we consider each of the outer hello's extensions - we can either:
609
        // 1. Omit the extension if it isn't appropriate (e.g. is a TLS 1.2 extension).
610
        // 2. Add the extension to the inner hello as-is.
611
        // 3. Compress the extension, by collecting it into a list of to-be-compressed
612
        //    extensions we'll handle separately.
613
0
        let outer_extensions = outer_hello.used_extensions_in_encoding_order();
614
0
        let mut compressed_exts = Vec::with_capacity(outer_extensions.len());
615
0
        for ext in outer_extensions {
616
            // Some outer hello extensions are only useful in the context where a TLS 1.3
617
            // connection allows TLS 1.2. This isn't the case for ECH so we skip adding them
618
            // to the inner hello.
619
0
            if matches!(
620
0
                ext,
621
                ExtensionType::ExtendedMasterSecret
622
                    | ExtensionType::SessionTicket
623
                    | ExtensionType::ECPointFormats
624
            ) {
625
0
                continue;
626
0
            }
627
628
0
            if ext == ExtensionType::ServerName {
629
                // We may want to replace the outer hello SNI with our own inner hello specific SNI.
630
0
                if let Some(sni_value) = inner_sni {
631
0
                    inner_hello.server_name = Some(ServerNamePayload::from(sni_value));
632
0
                }
633
                // We don't want to add, or compress, the SNI from the outer hello.
634
0
                continue;
635
0
            }
636
637
            // Compressed extensions need to be put aside to include in one contiguous block.
638
            // Uncompressed extensions get added directly to the inner hello.
639
0
            if ext.ech_compress() {
640
0
                compressed_exts.push(ext);
641
0
            }
642
643
0
            inner_hello.clone_one(outer_hello, ext);
644
        }
645
646
        // We've added all the uncompressed extensions. Now we need to add the contiguous
647
        // block of to-be-compressed extensions.
648
0
        inner_hello.contiguous_extensions = compressed_exts.clone();
649
650
        // Note which extensions we're sending in the inner hello. This may differ from
651
        // the outer hello (e.g. the inner hello may omit SNI while the outer hello will
652
        // always have the ECH cover name in SNI).
653
0
        self.sent_extensions = inner_hello.collect_used();
654
655
        // If we're resuming, we need to update the PSK binder in the inner hello.
656
0
        if let Some(resuming) = resuming.as_ref() {
657
0
            let mut chp = HandshakeMessagePayload(HandshakePayload::ClientHello(inner_hello));
658
659
            // Retain the early key schedule we get from processing the binder.
660
0
            self.early_data_key_schedule = Some(tls13::fill_in_psk_binder(
661
0
                resuming,
662
0
                &self.inner_hello_transcript,
663
0
                &mut chp,
664
0
            ));
665
666
            // fill_in_psk_binder works on an owned HandshakeMessagePayload, so we need to
667
            // extract our inner hello back out of it to retain ownership.
668
0
            inner_hello = match chp.0 {
669
0
                HandshakePayload::ClientHello(chp) => chp,
670
                // Safety: we construct the HMP above and know its type unconditionally.
671
0
                _ => unreachable!(),
672
            };
673
0
        }
674
675
0
        trace!("ECH Inner Hello: {inner_hello:#?}");
676
677
        // Encode the inner hello according to the rules required for ECH. This differs
678
        // from the standard encoding in several ways. Notably this is where we will
679
        // replace the block of contiguous to-be-compressed extensions with a marker.
680
0
        let mut encoded_hello = inner_hello.ech_inner_encoding(compressed_exts);
681
682
        // Calculate padding
683
        // max_name_len = L
684
0
        let max_name_len = usize::from(self.maximum_name_length);
685
0
        let max_name_len = if max_name_len > 0 { max_name_len } else { 255 };
686
687
0
        let name_padding_len = match &inner_hello.server_name {
688
0
            Some(ServerNamePayload::SingleDnsName(name)) => {
689
                // name.len() = D
690
                // max(0, L - D)
691
0
                Ord::max(0, max_name_len.saturating_sub(name.as_ref().len()))
692
            }
693
            // L + 9
694
            // "This is the length of a "server_name" extension with an L-byte name."
695
0
            _ => max_name_len + 9,
696
        };
697
0
        encoded_hello.extend(iter::repeat(0).take(name_padding_len));
698
699
        // Let L be the length of the EncodedClientHelloInner with all the padding computed so far
700
        // Let N = 31 - ((L - 1) % 32) and add N bytes of padding.
701
0
        let padding_len = 31 - ((encoded_hello.len() - 1) % 32);
702
0
        encoded_hello.extend(iter::repeat(0).take(padding_len));
703
704
        // Construct the inner hello message that will be used for the transcript.
705
0
        let inner_hello_msg = Message {
706
0
            version: match retryreq {
707
                // <https://datatracker.ietf.org/doc/html/rfc8446#section-5.1>:
708
                // "This value MUST be set to 0x0303 for all records generated
709
                //  by a TLS 1.3 implementation ..."
710
0
                Some(_) => ProtocolVersion::TLSv1_2,
711
                // "... other than an initial ClientHello (i.e., one not
712
                // generated after a HelloRetryRequest), where it MAY also be
713
                // 0x0301 for compatibility purposes"
714
                //
715
                // (retryreq == None means we're in the "initial ClientHello" case)
716
0
                None => ProtocolVersion::TLSv1_0,
717
            },
718
0
            payload: MessagePayload::handshake(HandshakeMessagePayload(
719
0
                HandshakePayload::ClientHello(inner_hello),
720
0
            )),
721
        };
722
723
        // Update the inner transcript buffer with the inner hello message.
724
0
        self.inner_hello_transcript
725
0
            .add_message(&inner_hello_msg);
726
727
0
        encoded_hello
728
0
    }
729
730
    // See https://datatracker.ietf.org/doc/html/draft-ietf-tls-esni-18#name-grease-psk
731
0
    fn grease_psk(&self, psk_offer: &mut PresharedKeyOffer) -> Result<(), Error> {
732
0
        for ident in psk_offer.identities.iter_mut() {
733
            // "For each PSK identity advertised in the ClientHelloInner, the
734
            // client generates a random PSK identity with the same length."
735
0
            self.secure_random
736
0
                .fill(&mut ident.identity.0)?;
737
            // "It also generates a random, 32-bit, unsigned integer to use as
738
            // the obfuscated_ticket_age."
739
0
            let mut ticket_age = [0_u8; 4];
740
0
            self.secure_random
741
0
                .fill(&mut ticket_age)?;
742
0
            ident.obfuscated_ticket_age = u32::from_be_bytes(ticket_age);
743
        }
744
745
        // "Likewise, for each inner PSK binder, the client generates a random string
746
        // of the same length."
747
0
        psk_offer.binders = psk_offer
748
0
            .binders
749
0
            .iter()
750
0
            .map(|old_binder| {
751
                // We can't access the wrapped binder PresharedKeyBinder's PayloadU8 mutably,
752
                // so we construct new PresharedKeyBinder's from scratch with the same length.
753
0
                let mut new_binder = vec![0; old_binder.as_ref().len()];
754
0
                self.secure_random
755
0
                    .fill(&mut new_binder)?;
756
0
                Ok::<PresharedKeyBinder, Error>(PresharedKeyBinder::from(new_binder))
757
0
            })
758
0
            .collect::<Result<_, _>>()?;
759
0
        Ok(())
760
0
    }
761
762
0
    fn server_hello_conf(
763
0
        server_hello: &ServerHelloPayload,
764
0
        server_hello_encoded: &Payload<'_>,
765
0
    ) -> Message<'static> {
766
        // The confirmation is computed over the server hello, which has had
767
        // its `random` field altered to zero the final 8 bytes.
768
        //
769
        // nb. we don't require that we can round-trip a `ServerHelloPayload`, to
770
        // allow for efficiency in its in-memory representation.  That means
771
        // we operate here on the received encoding, as the confirmation needs
772
        // to be computed on that.
773
0
        let mut encoded = server_hello_encoded.clone().into_vec();
774
0
        encoded[SERVER_HELLO_ECH_CONFIRMATION_SPAN].fill(0x00);
775
776
0
        Message {
777
0
            version: ProtocolVersion::TLSv1_3,
778
0
            payload: MessagePayload::Handshake {
779
0
                encoded: Payload::Owned(encoded),
780
0
                parsed: HandshakeMessagePayload(HandshakePayload::ServerHello(
781
0
                    server_hello.clone(),
782
0
                )),
783
0
            },
784
0
        }
785
0
    }
786
787
0
    fn hello_retry_request_conf(retry_req: &HelloRetryRequest) -> Message<'_> {
788
0
        Self::ech_conf_message(HandshakeMessagePayload(
789
0
            HandshakePayload::HelloRetryRequest(retry_req.clone()),
790
0
        ))
791
0
    }
792
793
0
    fn ech_conf_message(hmp: HandshakeMessagePayload<'_>) -> Message<'_> {
794
0
        let mut hmp_encoded = Vec::new();
795
0
        hmp.payload_encode(&mut hmp_encoded, Encoding::EchConfirmation);
796
0
        Message {
797
0
            version: ProtocolVersion::TLSv1_3,
798
0
            payload: MessagePayload::Handshake {
799
0
                encoded: Payload::new(hmp_encoded),
800
0
                parsed: hmp,
801
0
            },
802
0
        }
803
0
    }
804
}
805
806
/// The last eight bytes of the ServerHello's random, taken from a Handshake message containing it.
807
///
808
/// This has:
809
/// - a HandshakeType (1 byte),
810
/// - an exterior length (3 bytes),
811
/// - the legacy_version (2 bytes), and
812
/// - the balance of the random field (24 bytes).
813
const SERVER_HELLO_ECH_CONFIRMATION_SPAN: core::ops::Range<usize> =
814
    (1 + 3 + 2 + 24)..(1 + 3 + 2 + 32);
815
816
/// Returned from EchState::check_acceptance when the server has accepted the ECH offer.
817
///
818
/// Holds the state required to continue the handshake with the inner hello from the ECH offer.
819
pub(crate) struct EchAccepted {
820
    pub(crate) transcript: HandshakeHash,
821
    pub(crate) random: Random,
822
    pub(crate) sent_extensions: Vec<ExtensionType>,
823
}
824
825
0
pub(crate) fn fatal_alert_required(
826
0
    retry_configs: Option<Vec<EchConfigPayload>>,
827
0
    common: &mut CommonState,
828
0
) -> Error {
829
0
    common.send_fatal_alert(
830
0
        AlertDescription::EncryptedClientHelloRequired,
831
0
        PeerIncompatible::ServerRejectedEncryptedClientHello(retry_configs),
832
    )
833
0
}
834
835
#[cfg(test)]
836
mod tests {
837
    use std::string::String;
838
839
    use super::*;
840
    use crate::enums::CipherSuite;
841
    use crate::msgs::enums::{Compression, HpkeAead, HpkeKdf};
842
    use crate::msgs::handshake::{Random, ServerExtensions, SessionId};
843
844
    #[test]
845
    fn server_hello_conf_alters_server_hello_random() {
846
        let server_hello = ServerHelloPayload {
847
            legacy_version: ProtocolVersion::TLSv1_2,
848
            random: Random([0xffu8; 32]),
849
            session_id: SessionId::empty(),
850
            cipher_suite: CipherSuite::TLS13_AES_256_GCM_SHA384,
851
            compression_method: Compression::Null,
852
            extensions: Box::new(ServerExtensions::default()),
853
        };
854
        let message = Message {
855
            version: ProtocolVersion::TLSv1_3,
856
            payload: MessagePayload::handshake(HandshakeMessagePayload(
857
                HandshakePayload::ServerHello(server_hello.clone()),
858
            )),
859
        };
860
        let Message {
861
            payload:
862
                MessagePayload::Handshake {
863
                    encoded: server_hello_encoded_before,
864
                    ..
865
                },
866
            ..
867
        } = &message
868
        else {
869
            unreachable!("ServerHello is a handshake message");
870
        };
871
872
        let message = EchState::server_hello_conf(&server_hello, server_hello_encoded_before);
873
874
        let Message {
875
            payload:
876
                MessagePayload::Handshake {
877
                    encoded: server_hello_encoded_after,
878
                    ..
879
                },
880
            ..
881
        } = &message
882
        else {
883
            unreachable!("ServerHello is a handshake message");
884
        };
885
886
        assert_eq!(
887
            std::format!("{server_hello_encoded_before:x?}"),
888
            "020000280303ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001302000000",
889
            "beforehand eight bytes at end of Random should be 0xff here ^^^^^^^^^^^^^^^^            "
890
        );
891
        assert_eq!(
892
            std::format!("{server_hello_encoded_after:x?}"),
893
            "020000280303ffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000001302000000",
894
            "                          afterwards those bytes are zeroed ^^^^^^^^^^^^^^^^            "
895
        );
896
    }
897
898
    #[test]
899
    fn inner_client_hello_length_conceals_inner_name_length() {
900
        let base_inner_len = inner_hello_encoding_for_name(dns_name_of_len(1), true).len();
901
        assert!(
902
            base_inner_len % 32 == 0,
903
            "inner hello length must be 32-byte padded"
904
        );
905
        assert!(
906
            base_inner_len >= 256,
907
            "inner hello must include inner name and its padding"
908
        );
909
910
        for inner_name_len in 1..251 {
911
            assert_eq!(
912
                inner_hello_encoding_for_name(dns_name_of_len(inner_name_len), true).len(),
913
                base_inner_len,
914
                "all inner hello lengths must be invariant wrt inner name length"
915
            );
916
        }
917
    }
918
919
    #[test]
920
    fn inner_client_hello_length_does_not_leak_length_of_omitted_inner_name() {
921
        let base_inner_len = inner_hello_encoding_for_name(dns_name_of_len(1), false).len();
922
        assert!(
923
            base_inner_len % 32 == 0,
924
            "inner hello length must be 32-byte padded"
925
        );
926
        assert!(
927
            base_inner_len >= 256,
928
            "inner hello must include maximum_name_length bytes of padding"
929
        );
930
931
        for inner_name_len in 1..251 {
932
            assert_eq!(
933
                inner_hello_encoding_for_name(dns_name_of_len(inner_name_len), false).len(),
934
                base_inner_len,
935
                "all inner hello lengths must be invariant wrt inner name length"
936
            );
937
        }
938
    }
939
940
    fn inner_hello_encoding_for_name(name: DnsName<'static>, enable_sni: bool) -> Vec<u8> {
941
        let config = EchConfig {
942
            config: EchConfigPayload::V18(EchConfigContents {
943
                key_config: HpkeKeyConfig {
944
                    config_id: 0,
945
                    kem_id: MockHpke::SUITE.kem,
946
                    public_key: PayloadU16::new(vec![0; 32]),
947
                    symmetric_cipher_suites: vec![],
948
                },
949
                maximum_name_length: 255,
950
                public_name: DnsName::try_from("public").unwrap(),
951
                extensions: vec![],
952
            }),
953
            suite: &MockHpke,
954
        };
955
956
        EchState::new(
957
            &config,
958
            ServerName::from(name.clone()),
959
            false,
960
            &FixedRandom,
961
            enable_sni,
962
        )
963
        .unwrap()
964
        .encode_inner_hello(
965
            &ClientHelloPayload {
966
                client_version: ProtocolVersion::TLSv1_3,
967
                random: Random([0u8; 32]),
968
                session_id: SessionId::empty(),
969
                cipher_suites: vec![],
970
                compression_methods: vec![Compression::Null],
971
                extensions: Box::new(ClientExtensions {
972
                    server_name: Some(ServerNamePayload::from(&name)),
973
                    ..Default::default()
974
                }),
975
            },
976
            None,
977
            &None,
978
        )
979
    }
980
981
    fn dns_name_of_len(mut len: usize) -> DnsName<'static> {
982
        let mut s = String::new();
983
        let labels = len.div_ceil(63);
984
        for _ in 0..labels {
985
            let chars = Ord::min(len, 63);
986
            len -= chars;
987
            for _ in 0..chars {
988
                s.push('a');
989
            }
990
            if len != 0 {
991
                s.push('.');
992
            }
993
        }
994
        DnsName::try_from(s).unwrap()
995
    }
996
997
    #[derive(Debug)]
998
    struct MockHpke;
999
1000
    impl MockHpke {
1001
        const SUITE: HpkeSuite = HpkeSuite {
1002
            kem: HpkeKem::DHKEM_P256_HKDF_SHA256,
1003
            sym: HpkeSymmetricCipherSuite {
1004
                kdf_id: HpkeKdf::HKDF_SHA256,
1005
                aead_id: HpkeAead::AES_128_GCM,
1006
            },
1007
        };
1008
    }
1009
1010
    impl Hpke for MockHpke {
1011
        #[cfg_attr(coverage_nightly, coverage(off))]
1012
        fn seal(
1013
            &self,
1014
            _info: &[u8],
1015
            _aad: &[u8],
1016
            _plaintext: &[u8],
1017
            _pub_key: &HpkePublicKey,
1018
        ) -> Result<(EncapsulatedSecret, Vec<u8>), Error> {
1019
            todo!()
1020
        }
1021
1022
        fn setup_sealer(
1023
            &self,
1024
            _info: &[u8],
1025
            _pub_key: &HpkePublicKey,
1026
        ) -> Result<(EncapsulatedSecret, Box<dyn HpkeSealer + 'static>), Error> {
1027
            Ok((EncapsulatedSecret(vec![]), Box::new(MockHpkeSealer)))
1028
        }
1029
1030
        #[cfg_attr(coverage_nightly, coverage(off))]
1031
        fn open(
1032
            &self,
1033
            _enc: &EncapsulatedSecret,
1034
            _info: &[u8],
1035
            _aad: &[u8],
1036
            _ciphertext: &[u8],
1037
            _secret_key: &crate::crypto::hpke::HpkePrivateKey,
1038
        ) -> Result<Vec<u8>, Error> {
1039
            todo!()
1040
        }
1041
1042
        #[cfg_attr(coverage_nightly, coverage(off))]
1043
        fn setup_opener(
1044
            &self,
1045
            _enc: &EncapsulatedSecret,
1046
            _info: &[u8],
1047
            _secret_key: &crate::crypto::hpke::HpkePrivateKey,
1048
        ) -> Result<Box<dyn crate::crypto::hpke::HpkeOpener + 'static>, Error> {
1049
            todo!()
1050
        }
1051
1052
        #[cfg_attr(coverage_nightly, coverage(off))]
1053
        fn generate_key_pair(
1054
            &self,
1055
        ) -> Result<(HpkePublicKey, crate::crypto::hpke::HpkePrivateKey), Error> {
1056
            todo!()
1057
        }
1058
1059
        fn suite(&self) -> HpkeSuite {
1060
            Self::SUITE
1061
        }
1062
    }
1063
1064
    #[derive(Debug)]
1065
    struct MockHpkeSealer;
1066
1067
    impl HpkeSealer for MockHpkeSealer {
1068
        #[cfg_attr(coverage_nightly, coverage(off))]
1069
        fn seal(&mut self, _aad: &[u8], _plaintext: &[u8]) -> Result<Vec<u8>, Error> {
1070
            todo!()
1071
        }
1072
    }
1073
1074
    #[derive(Debug)]
1075
    struct FixedRandom;
1076
1077
    impl SecureRandom for FixedRandom {
1078
        fn fill(&self, buf: &mut [u8]) -> Result<(), crate::rand::GetRandomFailed> {
1079
            buf.fill(0x55);
1080
            Ok(())
1081
        }
1082
    }
1083
}