portable_rustls/client/
ech.rs

1use alloc::boxed::Box;
2use alloc::vec;
3use alloc::vec::Vec;
4
5use pki_types::{DnsName, EchConfigListBytes, ServerName};
6use subtle::ConstantTimeEq;
7
8use crate::client::tls13;
9use crate::crypto::hash::Hash;
10use crate::crypto::hpke::{EncapsulatedSecret, Hpke, HpkePublicKey, HpkeSealer, HpkeSuite};
11use crate::crypto::SecureRandom;
12use crate::hash_hs::{HandshakeHash, HandshakeHashBuffer};
13use crate::log::{debug, trace, warn};
14use crate::msgs::base::{Payload, PayloadU16};
15use crate::msgs::codec::{Codec, Reader};
16use crate::msgs::enums::{ExtensionType, HpkeKem};
17use crate::msgs::handshake::{
18    ClientExtension, ClientHelloPayload, EchConfigContents, EchConfigPayload, Encoding,
19    EncryptedClientHello, EncryptedClientHelloOuter, HandshakeMessagePayload, HandshakePayload,
20    HelloRetryRequest, HpkeKeyConfig, HpkeSymmetricCipherSuite, PresharedKeyBinder,
21    PresharedKeyOffer, Random, ServerHelloPayload,
22};
23use crate::msgs::message::{Message, MessagePayload};
24use crate::msgs::persist;
25use crate::msgs::persist::Retrieved;
26use crate::tls13::key_schedule::{
27    server_ech_hrr_confirmation_secret, KeyScheduleEarly, KeyScheduleHandshakeStart,
28};
29use crate::CipherSuite::TLS_EMPTY_RENEGOTIATION_INFO_SCSV;
30use crate::{
31    AlertDescription, CommonState, EncryptedClientHelloError, Error, HandshakeType,
32    PeerIncompatible, PeerMisbehaved, ProtocolVersion, Tls13CipherSuite,
33};
34
35/// Controls how Encrypted Client Hello (ECH) is used in a client handshake.
36#[derive(Clone, Debug)]
37pub enum EchMode {
38    /// ECH is enabled and the ClientHello will be encrypted based on the provided
39    /// configuration.
40    Enable(EchConfig),
41
42    /// No ECH configuration is available but the client should act as though it were.
43    ///
44    /// This is an anti-ossification measure, sometimes referred to as "GREASE"[^0].
45    /// [^0]: <https://www.rfc-editor.org/rfc/rfc8701>
46    Grease(EchGreaseConfig),
47}
48
49impl EchMode {
50    /// Returns true if the ECH mode will use a FIPS approved HPKE suite.
51    #[cfg(unstable_api_not_supported)] // [FIPS REMOVED FROM THIS FORK]
52    pub fn fips(&self) -> bool {
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/brody4hire/portable-rustls/blob/main-develop-head/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::init(&ech_config_list))
105            .map_err(|_| {
106                Error::InvalidEncryptedClientHello(EncryptedClientHelloError::InvalidConfigList)
107            })?;
108
109        // Note: we name the index var _i because if the log feature is disabled
110        //       it is unused.
111        // WITH UPDATED FEATURE CONDITION TO AVOID CI CLIPPY ISSUE IN THIS FORK
112        #[cfg_attr(not(feature = "logging"), allow(clippy::unused_enumerate_index))]
113        for (_i, config) in ech_configs.iter().enumerate() {
114            let contents = match config {
115                EchConfigPayload::V18(contents) => contents,
116                EchConfigPayload::Unknown {
117                    version: _version, ..
118                } => {
119                    warn!(
120                        "ECH config {} has unsupported version {:?}",
121                        _i + 1,
122                        _version
123                    );
124                    continue; // Unsupported version.
125                }
126            };
127
128            if contents.has_unknown_mandatory_extension() || contents.has_duplicate_extension() {
129                warn!("ECH config has duplicate, or unknown mandatory extensions: {contents:?}",);
130                continue; // Unsupported, or malformed extensions.
131            }
132
133            let key_config = &contents.key_config;
134            for cipher_suite in &key_config.symmetric_cipher_suites {
135                if cipher_suite.aead_id.tag_len().is_none() {
136                    continue; // Unsupported EXPORT_ONLY AEAD cipher suite.
137                }
138
139                let suite = HpkeSuite {
140                    kem: key_config.kem_id,
141                    sym: *cipher_suite,
142                };
143                if let Some(hpke) = hpke_suites
144                    .iter()
145                    .find(|hpke| hpke.suite() == suite)
146                {
147                    debug!(
148                        "selected ECH config ID {:?} suite {:?} public_name {:?}",
149                        key_config.config_id, suite, contents.public_name
150                    );
151                    return Ok(Self {
152                        config: config.clone(),
153                        suite: *hpke,
154                    });
155                }
156            }
157        }
158
159        Err(EncryptedClientHelloError::NoCompatibleConfig.into())
160    }
161
162    /// Compute the HPKE `SetupBaseS` `info` parameter for this ECH configuration.
163    ///
164    /// See <https://datatracker.ietf.org/doc/html/draft-ietf-tls-esni-17#section-6.1>.
165    pub(crate) fn hpke_info(&self) -> Vec<u8> {
166        let mut info = Vec::with_capacity(128);
167        // "tls ech" || 0x00 || ECHConfig
168        info.extend_from_slice(b"tls ech\0");
169        self.config.encode(&mut info);
170        info
171    }
172}
173
174/// Configuration for GREASE Encrypted Client Hello.
175#[derive(Clone, Debug)]
176pub struct EchGreaseConfig {
177    pub(crate) suite: &'static dyn Hpke,
178    pub(crate) placeholder_key: HpkePublicKey,
179}
180
181impl EchGreaseConfig {
182    /// Construct a GREASE ECH configuration.
183    ///
184    /// This configuration is used when the client wishes to offer ECH to prevent ossification,
185    /// but doesn't have a real ECH configuration to use for the remote server. In this case
186    /// a placeholder or "GREASE"[^0] extension is used.
187    ///
188    /// Returns an error if the HPKE provider does not support the given suite.
189    ///
190    /// [^0]: <https://www.rfc-editor.org/rfc/rfc8701>
191    pub fn new(suite: &'static dyn Hpke, placeholder_key: HpkePublicKey) -> Self {
192        Self {
193            suite,
194            placeholder_key,
195        }
196    }
197
198    /// Build a GREASE ECH extension based on the placeholder configuration.
199    ///
200    /// See <https://datatracker.ietf.org/doc/html/draft-ietf-tls-esni-18#name-grease-ech> for
201    /// more information.
202    pub(crate) fn grease_ext(
203        &self,
204        secure_random: &'static dyn SecureRandom,
205        inner_name: ServerName<'static>,
206        outer_hello: &ClientHelloPayload,
207    ) -> Result<ClientExtension, Error> {
208        trace!("Preparing GREASE ECH extension");
209
210        // Pick a random config id.
211        let mut config_id: [u8; 1] = [0; 1];
212        secure_random.fill(&mut config_id[..])?;
213
214        let suite = self.suite.suite();
215
216        // Construct a dummy ECH state - we don't have a real ECH config from a server since
217        // this is for GREASE.
218        let mut grease_state = EchState::new(
219            &EchConfig {
220                config: EchConfigPayload::V18(EchConfigContents {
221                    key_config: HpkeKeyConfig {
222                        config_id: config_id[0],
223                        kem_id: HpkeKem::DHKEM_P256_HKDF_SHA256,
224                        public_key: PayloadU16(self.placeholder_key.0.clone()),
225                        symmetric_cipher_suites: vec![suite.sym],
226                    },
227                    maximum_name_length: 0,
228                    public_name: DnsName::try_from("filler").unwrap(),
229                    extensions: Vec::default(),
230                }),
231                suite: self.suite,
232            },
233            inner_name,
234            false,
235            secure_random,
236            false, // Does not matter if we enable/disable SNI here. Inner hello is not used.
237        )?;
238
239        // Construct an inner hello using the outer hello - this allows us to know the size of
240        // dummy payload we should use for the GREASE extension.
241        let encoded_inner_hello = grease_state.encode_inner_hello(outer_hello, None, &None);
242
243        // Generate a payload of random data equivalent in length to a real inner hello.
244        let payload_len = encoded_inner_hello.len()
245            + suite
246                .sym
247                .aead_id
248                .tag_len()
249                // Safety: we have confirmed the AEAD is supported when building the config. All
250                //  supported AEADs have a tag length.
251                .unwrap();
252        let mut payload = vec![0; payload_len];
253        secure_random.fill(&mut payload)?;
254
255        // Return the GREASE extension.
256        Ok(ClientExtension::EncryptedClientHello(
257            EncryptedClientHello::Outer(EncryptedClientHelloOuter {
258                cipher_suite: suite.sym,
259                config_id: config_id[0],
260                enc: PayloadU16(grease_state.enc.0),
261                payload: PayloadU16::new(payload),
262            }),
263        ))
264    }
265}
266
267/// An enum representing ECH offer status.
268#[derive(Debug, Clone, Copy, Eq, PartialEq)]
269pub enum EchStatus {
270    /// ECH was not offered - it is a normal TLS handshake.
271    NotOffered,
272    /// GREASE ECH was sent. This is not considered offering ECH.
273    Grease,
274    /// ECH was offered but we do not yet know whether the offer was accepted or rejected.
275    Offered,
276    /// ECH was offered and the server accepted.
277    Accepted,
278    /// ECH was offered and the server rejected.
279    Rejected,
280}
281
282/// Contextual data for a TLS client handshake that has offered encrypted client hello (ECH).
283pub(crate) struct EchState {
284    // The public DNS name from the ECH configuration we've chosen - this is included as the SNI
285    // value for the "outer" client hello. It can only be a DnsName, not an IP address.
286    pub(crate) outer_name: DnsName<'static>,
287    // If we're resuming in the inner hello, this is the early key schedule to use for encrypting
288    // early data if the ECH offer is accepted.
289    pub(crate) early_data_key_schedule: Option<KeyScheduleEarly>,
290    // A random value we use for the inner hello.
291    pub(crate) inner_hello_random: Random,
292    // A transcript buffer maintained for the inner hello. Once ECH is confirmed we switch to
293    // using this transcript for the handshake.
294    pub(crate) inner_hello_transcript: HandshakeHashBuffer,
295    // A source of secure random data.
296    secure_random: &'static dyn SecureRandom,
297    // An HPKE sealer context that can be used for encrypting ECH data.
298    sender: Box<dyn HpkeSealer>,
299    // The ID of the ECH configuration we've chosen - this is included in the outer ECH extension.
300    config_id: u8,
301    // The private server name we'll use for the inner protected hello.
302    inner_name: ServerName<'static>,
303    // The advertised maximum name length from the ECH configuration we've chosen - this is used
304    // for padding calculations.
305    maximum_name_length: u8,
306    // A supported symmetric cipher suite from the ECH configuration we've chosen - this is
307    // included in the outer ECH extension.
308    cipher_suite: HpkeSymmetricCipherSuite,
309    // A secret encapsulated to the public key of the remote server. This is included in the
310    // outer ECH extension for non-retry outer hello messages.
311    enc: EncapsulatedSecret,
312    // Whether the inner client hello should contain a server name indication (SNI) extension.
313    enable_sni: bool,
314    // The extensions sent in the inner hello.
315    sent_extensions: Vec<ExtensionType>,
316}
317
318impl EchState {
319    pub(crate) fn new(
320        config: &EchConfig,
321        inner_name: ServerName<'static>,
322        client_auth_enabled: bool,
323        secure_random: &'static dyn SecureRandom,
324        enable_sni: bool,
325    ) -> Result<Self, Error> {
326        let EchConfigPayload::V18(config_contents) = &config.config else {
327            // the public EchConfig::new() constructor ensures we only have supported
328            // configurations.
329            unreachable!("ECH config version mismatch");
330        };
331        let key_config = &config_contents.key_config;
332
333        // Encapsulate a secret for the server's public key, and set up a sender context
334        // we can use to seal messages.
335        let (enc, sender) = config.suite.setup_sealer(
336            &config.hpke_info(),
337            &HpkePublicKey(key_config.public_key.0.clone()),
338        )?;
339
340        // Start a new transcript buffer for the inner hello.
341        let mut inner_hello_transcript = HandshakeHashBuffer::new();
342        if client_auth_enabled {
343            inner_hello_transcript.set_client_auth_enabled();
344        }
345
346        Ok(Self {
347            secure_random,
348            sender,
349            config_id: key_config.config_id,
350            inner_name,
351            outer_name: config_contents.public_name.clone(),
352            maximum_name_length: config_contents.maximum_name_length,
353            cipher_suite: config.suite.suite().sym,
354            enc,
355            inner_hello_random: Random::new(secure_random)?,
356            inner_hello_transcript,
357            early_data_key_schedule: None,
358            enable_sni,
359            sent_extensions: Vec::new(),
360        })
361    }
362
363    /// Construct a ClientHelloPayload offering ECH.
364    ///
365    /// An outer hello, with a protected inner hello for the `inner_name` will be returned, and the
366    /// ECH context will be updated to reflect the inner hello that was offered.
367    ///
368    /// If `retry_req` is `Some`, then the outer hello will be constructed for a hello retry request.
369    ///
370    /// If `resuming` is `Some`, then the inner hello will be constructed for a resumption handshake.
371    pub(crate) fn ech_hello(
372        &mut self,
373        mut outer_hello: ClientHelloPayload,
374        retry_req: Option<&HelloRetryRequest>,
375        resuming: &Option<Retrieved<&persist::Tls13ClientSessionValue>>,
376    ) -> Result<ClientHelloPayload, Error> {
377        trace!(
378            "Preparing ECH offer {}",
379            if retry_req.is_some() { "for retry" } else { "" }
380        );
381
382        // Construct the encoded inner hello and update the transcript.
383        let encoded_inner_hello = self.encode_inner_hello(&outer_hello, retry_req, resuming);
384
385        // Complete the ClientHelloOuterAAD with an ech extension, the payload should be a placeholder
386        // of size L, all zeroes. L == length of encrypting encoded client hello inner w/ the selected
387        // HPKE AEAD. (sum of plaintext + tag length, typically).
388        let payload_len = encoded_inner_hello.len()
389            + self
390                .cipher_suite
391                .aead_id
392                .tag_len()
393                // Safety: we've already verified this AEAD is supported when loading the config
394                // that was used to create the ECH context. All supported AEADs have a tag length.
395                .unwrap();
396
397        // Outer hello's created in response to a hello retry request omit the enc value.
398        let enc = match retry_req.is_some() {
399            true => Vec::default(),
400            false => self.enc.0.clone(),
401        };
402
403        fn outer_hello_ext(ctx: &EchState, enc: Vec<u8>, payload: Vec<u8>) -> ClientExtension {
404            ClientExtension::EncryptedClientHello(EncryptedClientHello::Outer(
405                EncryptedClientHelloOuter {
406                    cipher_suite: ctx.cipher_suite,
407                    config_id: ctx.config_id,
408                    enc: PayloadU16::new(enc),
409                    payload: PayloadU16::new(payload),
410                },
411            ))
412        }
413
414        // The outer handshake is not permitted to resume a session. If we're resuming in the
415        // inner handshake we remove the PSK extension from the outer hello, replacing it
416        // with a GREASE PSK to implement the "ClientHello Malleability Mitigation" mentioned
417        // in 10.12.3.
418        if let Some(ClientExtension::PresharedKey(psk_offer)) = outer_hello.extensions.last_mut() {
419            self.grease_psk(psk_offer)?;
420        }
421
422        // To compute the encoded AAD we add a placeholder extension with an empty payload.
423        outer_hello
424            .extensions
425            .push(outer_hello_ext(self, enc.clone(), vec![0; payload_len]));
426
427        // Next we compute the proper extension payload.
428        let payload = self
429            .sender
430            .seal(&outer_hello.get_encoding(), &encoded_inner_hello)?;
431
432        // And then we replace the placeholder extension with the real one.
433        outer_hello.extensions.pop();
434        outer_hello
435            .extensions
436            .push(outer_hello_ext(self, enc, payload));
437
438        Ok(outer_hello)
439    }
440
441    /// Confirm whether an ECH offer was accepted based on examining the server hello.
442    pub(crate) fn confirm_acceptance(
443        self,
444        ks: &mut KeyScheduleHandshakeStart,
445        server_hello: &ServerHelloPayload,
446        hash: &'static dyn Hash,
447    ) -> Result<Option<EchAccepted>, Error> {
448        // Start the inner transcript hash now that we know the hash algorithm to use.
449        let inner_transcript = self
450            .inner_hello_transcript
451            .start_hash(hash);
452
453        // Fork the transcript that we've started with the inner hello to use for a confirmation step.
454        // We need to preserve the original inner_transcript to use if this confirmation succeeds.
455        let mut confirmation_transcript = inner_transcript.clone();
456
457        // Add the server hello confirmation - this differs from the standard server hello encoding.
458        confirmation_transcript.add_message(&Self::server_hello_conf(server_hello));
459
460        // Derive a confirmation secret from the inner hello random and the confirmation transcript.
461        let derived = ks.server_ech_confirmation_secret(
462            self.inner_hello_random.0.as_ref(),
463            confirmation_transcript.current_hash(),
464        );
465
466        // Check that first 8 digits of the derived secret match the last 8 digits of the original
467        // server random. This match signals that the server accepted the ECH offer.
468        // Indexing safety: Random is [0; 32] by construction.
469
470        match ConstantTimeEq::ct_eq(derived.as_ref(), server_hello.random.0[24..].as_ref()).into() {
471            true => {
472                trace!("ECH accepted by server");
473                Ok(Some(EchAccepted {
474                    transcript: inner_transcript,
475                    random: self.inner_hello_random,
476                    sent_extensions: self.sent_extensions,
477                }))
478            }
479            false => {
480                trace!("ECH rejected by server");
481                Ok(None)
482            }
483        }
484    }
485
486    pub(crate) fn confirm_hrr_acceptance(
487        &self,
488        hrr: &HelloRetryRequest,
489        cs: &Tls13CipherSuite,
490        common: &mut CommonState,
491    ) -> Result<bool, Error> {
492        // The client checks for the "encrypted_client_hello" extension.
493        let ech_conf = match hrr.ech() {
494            // If none is found, the server has implicitly rejected ECH.
495            None => return Ok(false),
496            // Otherwise, if it has a length other than 8, the client aborts the
497            // handshake with a "decode_error" alert.
498            Some(ech_conf) if ech_conf.len() != 8 => {
499                return Err({
500                    common.send_fatal_alert(
501                        AlertDescription::DecodeError,
502                        PeerMisbehaved::IllegalHelloRetryRequestWithInvalidEch,
503                    )
504                })
505            }
506            Some(ech_conf) => ech_conf,
507        };
508
509        // Otherwise the client computes hrr_accept_confirmation as described in Section
510        // 7.2.1
511        let confirmation_transcript = self.inner_hello_transcript.clone();
512        let mut confirmation_transcript =
513            confirmation_transcript.start_hash(cs.common.hash_provider);
514        confirmation_transcript.rollup_for_hrr();
515        confirmation_transcript.add_message(&Self::hello_retry_request_conf(hrr));
516
517        let derived = server_ech_hrr_confirmation_secret(
518            cs.hkdf_provider,
519            &self.inner_hello_random.0,
520            confirmation_transcript.current_hash(),
521        );
522
523        match ConstantTimeEq::ct_eq(derived.as_ref(), ech_conf).into() {
524            true => {
525                trace!("ECH accepted by server in hello retry request");
526                Ok(true)
527            }
528            false => {
529                trace!("ECH rejected by server in hello retry request");
530                Ok(false)
531            }
532        }
533    }
534
535    /// Update the ECH context inner hello transcript based on a received hello retry request message.
536    ///
537    /// This will start the in-progress transcript using the given `hash`, convert it into an HRR
538    /// buffer, and then add the hello retry message `m`.
539    pub(crate) fn transcript_hrr_update(&mut self, hash: &'static dyn Hash, m: &Message<'_>) {
540        trace!("Updating ECH inner transcript for HRR");
541
542        let inner_transcript = self
543            .inner_hello_transcript
544            .clone()
545            .start_hash(hash);
546
547        let mut inner_transcript_buffer = inner_transcript.into_hrr_buffer();
548        inner_transcript_buffer.add_message(m);
549        self.inner_hello_transcript = inner_transcript_buffer;
550    }
551
552    // 5.1 "Encoding the ClientHelloInner"
553    fn encode_inner_hello(
554        &mut self,
555        outer_hello: &ClientHelloPayload,
556        retryreq: Option<&HelloRetryRequest>,
557        resuming: &Option<Retrieved<&persist::Tls13ClientSessionValue>>,
558    ) -> Vec<u8> {
559        // Start building an inner hello using the outer_hello as a template.
560        let mut inner_hello = ClientHelloPayload {
561            // Some information is copied over as-is.
562            client_version: outer_hello.client_version,
563            session_id: outer_hello.session_id,
564            compression_methods: outer_hello.compression_methods.clone(),
565
566            // We will build up the included extensions ourselves.
567            extensions: vec![],
568
569            // Set the inner hello random to the one we generated when creating the ECH state.
570            // We hold on to the inner_hello_random in the ECH state to use later for confirming
571            // whether ECH was accepted or not.
572            random: self.inner_hello_random,
573
574            // We remove the empty renegotiation info SCSV from the outer hello's ciphersuite.
575            // Similar to the TLS 1.2 specific extensions we will filter out, this is seen as a
576            // TLS 1.2 only feature by bogo.
577            cipher_suites: outer_hello
578                .cipher_suites
579                .iter()
580                .filter(|cs| **cs != TLS_EMPTY_RENEGOTIATION_INFO_SCSV)
581                .cloned()
582                .collect(),
583        };
584
585        // The inner hello will always have an inner variant of the ECH extension added.
586        // See Section 6.1 rule 4.
587        inner_hello
588            .extensions
589            .push(ClientExtension::EncryptedClientHello(
590                EncryptedClientHello::Inner,
591            ));
592
593        let inner_sni = match &self.inner_name {
594            // The inner hello only gets a SNI value if enable_sni is true and the inner name
595            // is a domain name (not an IP address).
596            ServerName::DnsName(dns_name) if self.enable_sni => Some(dns_name),
597            _ => None,
598        };
599
600        // Now we consider each of the outer hello's extensions - we can either:
601        // 1. Omit the extension if it isn't appropriate (e.g. is a TLS 1.2 extension).
602        // 2. Add the extension to the inner hello as-is.
603        // 3. Compress the extension, by collecting it into a list of to-be-compressed
604        //    extensions we'll handle separately.
605        let mut compressed_exts = Vec::with_capacity(outer_hello.extensions.len());
606        let mut compressed_ext_types = Vec::with_capacity(outer_hello.extensions.len());
607        for ext in &outer_hello.extensions {
608            // Some outer hello extensions are only useful in the context where a TLS 1.3
609            // connection allows TLS 1.2. This isn't the case for ECH so we skip adding them
610            // to the inner hello.
611            if matches!(
612                ext.ext_type(),
613                ExtensionType::ExtendedMasterSecret
614                    | ExtensionType::SessionTicket
615                    | ExtensionType::ECPointFormats
616            ) {
617                continue;
618            }
619
620            if ext.ext_type() == ExtensionType::ServerName {
621                // We may want to replace the outer hello SNI with our own inner hello specific SNI.
622                if let Some(sni_value) = inner_sni {
623                    inner_hello
624                        .extensions
625                        .push(ClientExtension::make_sni(&sni_value.borrow()));
626                }
627                // We don't want to add, or compress, the SNI from the outer hello.
628                continue;
629            }
630
631            // Compressed extensions need to be put aside to include in one contiguous block.
632            // Uncompressed extensions get added directly to the inner hello.
633            if ext.ext_type().ech_compress() {
634                compressed_exts.push(ext.clone());
635                compressed_ext_types.push(ext.ext_type());
636            } else {
637                inner_hello.extensions.push(ext.clone());
638            }
639        }
640
641        // We've added all the uncompressed extensions. Now we need to add the contiguous
642        // block of to-be-compressed extensions. Where we do this depends on whether the
643        // last uncompressed extension is a PSK for resumption. In this case we must
644        // add the to-be-compressed extensions _before_ the PSK.
645        let compressed_exts_index =
646            if let Some(ClientExtension::PresharedKey(_)) = inner_hello.extensions.last() {
647                inner_hello.extensions.len() - 1
648            } else {
649                inner_hello.extensions.len()
650            };
651        inner_hello.extensions.splice(
652            compressed_exts_index..compressed_exts_index,
653            compressed_exts,
654        );
655
656        // Note which extensions we're sending in the inner hello. This may differ from
657        // the outer hello (e.g. the inner hello may omit SNI while the outer hello will
658        // always have the ECH cover name in SNI).
659        self.sent_extensions = inner_hello
660            .extensions
661            .iter()
662            .map(|ext| ext.ext_type())
663            .collect();
664
665        // If we're resuming, we need to update the PSK binder in the inner hello.
666        if let Some(resuming) = resuming.as_ref() {
667            let mut chp = HandshakeMessagePayload {
668                typ: HandshakeType::ClientHello,
669                payload: HandshakePayload::ClientHello(inner_hello),
670            };
671
672            // Retain the early key schedule we get from processing the binder.
673            self.early_data_key_schedule = Some(tls13::fill_in_psk_binder(
674                resuming,
675                &self.inner_hello_transcript,
676                &mut chp,
677            ));
678
679            // fill_in_psk_binder works on an owned HandshakeMessagePayload, so we need to
680            // extract our inner hello back out of it to retain ownership.
681            inner_hello = match chp.payload {
682                HandshakePayload::ClientHello(chp) => chp,
683                // Safety: we construct the HMP above and know its type unconditionally.
684                _ => unreachable!(),
685            };
686        }
687
688        trace!("ECH Inner Hello: {:#?}", inner_hello);
689
690        // Encode the inner hello according to the rules required for ECH. This differs
691        // from the standard encoding in several ways. Notably this is where we will
692        // replace the block of contiguous to-be-compressed extensions with a marker.
693        let mut encoded_hello = inner_hello.ech_inner_encoding(compressed_ext_types);
694
695        // Calculate padding
696        // max_name_len = L
697        let max_name_len = self.maximum_name_length;
698        let max_name_len = if max_name_len > 0 { max_name_len } else { 255 };
699
700        let padding_len = match &self.inner_name {
701            ServerName::DnsName(name) => {
702                // name.len() = D
703                // max(0, L - D)
704                core::cmp::max(
705                    0,
706                    max_name_len.saturating_sub(name.as_ref().len() as u8) as usize,
707                )
708            }
709            _ => {
710                // L + 9
711                // "This is the length of a "server_name" extension with an L-byte name."
712                // We widen to usize here to avoid overflowing u8 + u8.
713                max_name_len as usize + 9
714            }
715        };
716
717        // Let L be the length of the EncodedClientHelloInner with all the padding computed so far
718        // Let N = 31 - ((L - 1) % 32) and add N bytes of padding.
719        let padding_len = 31 - ((encoded_hello.len() + padding_len - 1) % 32);
720        encoded_hello.extend(vec![0; padding_len]);
721
722        // Construct the inner hello message that will be used for the transcript.
723        let inner_hello_msg = Message {
724            version: match retryreq {
725                // <https://datatracker.ietf.org/doc/html/rfc8446#section-5.1>:
726                // "This value MUST be set to 0x0303 for all records generated
727                //  by a TLS 1.3 implementation ..."
728                Some(_) => ProtocolVersion::TLSv1_2,
729                // "... other than an initial ClientHello (i.e., one not
730                // generated after a HelloRetryRequest), where it MAY also be
731                // 0x0301 for compatibility purposes"
732                //
733                // (retryreq == None means we're in the "initial ClientHello" case)
734                None => ProtocolVersion::TLSv1_0,
735            },
736            payload: MessagePayload::handshake(HandshakeMessagePayload {
737                typ: HandshakeType::ClientHello,
738                payload: HandshakePayload::ClientHello(inner_hello),
739            }),
740        };
741
742        // Update the inner transcript buffer with the inner hello message.
743        self.inner_hello_transcript
744            .add_message(&inner_hello_msg);
745
746        encoded_hello
747    }
748
749    // See https://datatracker.ietf.org/doc/html/draft-ietf-tls-esni-18#name-grease-psk
750    fn grease_psk(&self, psk_offer: &mut PresharedKeyOffer) -> Result<(), Error> {
751        for ident in psk_offer.identities.iter_mut() {
752            // "For each PSK identity advertised in the ClientHelloInner, the
753            // client generates a random PSK identity with the same length."
754            self.secure_random
755                .fill(&mut ident.identity.0)?;
756            // "It also generates a random, 32-bit, unsigned integer to use as
757            // the obfuscated_ticket_age."
758            let mut ticket_age = [0_u8; 4];
759            self.secure_random
760                .fill(&mut ticket_age)?;
761            ident.obfuscated_ticket_age = u32::from_be_bytes(ticket_age);
762        }
763
764        // "Likewise, for each inner PSK binder, the client generates a random string
765        // of the same length."
766        psk_offer.binders = psk_offer
767            .binders
768            .iter()
769            .map(|old_binder| {
770                // We can't access the wrapped binder PresharedKeyBinder's PayloadU8 mutably,
771                // so we construct new PresharedKeyBinder's from scratch with the same length.
772                let mut new_binder = vec![0; old_binder.as_ref().len()];
773                self.secure_random
774                    .fill(&mut new_binder)?;
775                Ok::<PresharedKeyBinder, Error>(PresharedKeyBinder::from(new_binder))
776            })
777            .collect::<Result<_, _>>()?;
778        Ok(())
779    }
780
781    fn server_hello_conf(server_hello: &ServerHelloPayload) -> Message<'_> {
782        Self::ech_conf_message(HandshakeMessagePayload {
783            typ: HandshakeType::ServerHello,
784            payload: HandshakePayload::ServerHello(server_hello.clone()),
785        })
786    }
787
788    fn hello_retry_request_conf(retry_req: &HelloRetryRequest) -> Message<'_> {
789        Self::ech_conf_message(HandshakeMessagePayload {
790            typ: HandshakeType::HelloRetryRequest,
791            payload: HandshakePayload::HelloRetryRequest(retry_req.clone()),
792        })
793    }
794
795    fn ech_conf_message(hmp: HandshakeMessagePayload<'_>) -> Message<'_> {
796        let mut hmp_encoded = Vec::new();
797        hmp.payload_encode(&mut hmp_encoded, Encoding::EchConfirmation);
798        Message {
799            version: ProtocolVersion::TLSv1_3,
800            payload: MessagePayload::Handshake {
801                encoded: Payload::new(hmp_encoded),
802                parsed: hmp,
803            },
804        }
805    }
806}
807
808/// Returned from EchState::check_acceptance when the server has accepted the ECH offer.
809///
810/// Holds the state required to continue the handshake with the inner hello from the ECH offer.
811pub(crate) struct EchAccepted {
812    pub(crate) transcript: HandshakeHash,
813    pub(crate) random: Random,
814    pub(crate) sent_extensions: Vec<ExtensionType>,
815}
816
817pub(crate) fn fatal_alert_required(
818    retry_configs: Option<Vec<EchConfigPayload>>,
819    common: &mut CommonState,
820) -> Error {
821    common.send_fatal_alert(
822        AlertDescription::EncryptedClientHelloRequired,
823        PeerIncompatible::ServerRejectedEncryptedClientHello(retry_configs),
824    )
825}