Skip to main content

rustls/client/
ech.rs

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