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, ServerName};
7use subtle::ConstantTimeEq;
8
9use crate::CipherSuite::TLS_EMPTY_RENEGOTIATION_INFO_SCSV;
10use crate::client::tls13;
11use crate::crypto::SecureRandom;
12use crate::crypto::hash::Hash;
13use crate::crypto::hpke::{EncapsulatedSecret, Hpke, HpkePublicKey, HpkeSealer, HpkeSuite};
14use crate::hash_hs::{HandshakeHash, HandshakeHashBuffer};
15use crate::log::{debug, trace, warn};
16use crate::msgs::base::{Payload, PayloadU16};
17use crate::msgs::codec::{Codec, Reader};
18use crate::msgs::enums::{ExtensionType, HpkeKem};
19use 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};
25use crate::msgs::message::{Message, MessagePayload};
26use crate::msgs::persist;
27use crate::msgs::persist::Retrieved;
28use crate::tls13::key_schedule::{
29    KeyScheduleEarly, KeyScheduleHandshakeStart, server_ech_hrr_confirmation_secret,
30};
31use 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)]
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) -> 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/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::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        #[cfg_attr(not(feature = "logging"), allow(clippy::unused_enumerate_index))]
112        for (_i, config) in ech_configs.iter().enumerate() {
113            let contents = match config {
114                EchConfigPayload::V18(contents) => contents,
115                EchConfigPayload::Unknown {
116                    version: _version, ..
117                } => {
118                    warn!(
119                        "ECH config {} has unsupported version {:?}",
120                        _i + 1,
121                        _version
122                    );
123                    continue; // Unsupported version.
124                }
125            };
126
127            if contents.has_unknown_mandatory_extension() || contents.has_duplicate_extension() {
128                warn!("ECH config has duplicate, or unknown mandatory extensions: {contents:?}",);
129                continue; // Unsupported, or malformed extensions.
130            }
131
132            let key_config = &contents.key_config;
133            for cipher_suite in &key_config.symmetric_cipher_suites {
134                if cipher_suite.aead_id.tag_len().is_none() {
135                    continue; // Unsupported EXPORT_ONLY AEAD cipher suite.
136                }
137
138                let suite = HpkeSuite {
139                    kem: key_config.kem_id,
140                    sym: *cipher_suite,
141                };
142                if let Some(hpke) = hpke_suites
143                    .iter()
144                    .find(|hpke| hpke.suite() == suite)
145                {
146                    debug!(
147                        "selected ECH config ID {:?} suite {:?} public_name {:?}",
148                        key_config.config_id, suite, contents.public_name
149                    );
150                    return Ok(Self {
151                        config: config.clone(),
152                        suite: *hpke,
153                    });
154                }
155            }
156        }
157
158        Err(EncryptedClientHelloError::NoCompatibleConfig.into())
159    }
160
161    pub(super) fn state(
162        &self,
163        server_name: ServerName<'static>,
164        config: &ClientConfig,
165    ) -> Result<EchState, Error> {
166        EchState::new(
167            self,
168            server_name.clone(),
169            config
170                .client_auth_cert_resolver
171                .has_certs(),
172            config.provider.secure_random,
173            config.enable_sni,
174        )
175    }
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    pub(crate) fn hpke_info(&self) -> Vec<u8> {
181        let mut info = Vec::with_capacity(128);
182        // "tls ech" || 0x00 || ECHConfig
183        info.extend_from_slice(b"tls ech\0");
184        self.config.encode(&mut info);
185        info
186    }
187}
188
189/// Configuration for GREASE Encrypted Client Hello.
190#[derive(Clone, Debug)]
191pub struct EchGreaseConfig {
192    pub(crate) suite: &'static dyn Hpke,
193    pub(crate) placeholder_key: HpkePublicKey,
194}
195
196impl 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    pub fn new(suite: &'static dyn Hpke, placeholder_key: HpkePublicKey) -> Self {
207        Self {
208            suite,
209            placeholder_key,
210        }
211    }
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    pub(crate) fn grease_ext(
218        &self,
219        secure_random: &'static dyn SecureRandom,
220        inner_name: ServerName<'static>,
221        outer_hello: &ClientHelloPayload,
222    ) -> Result<EncryptedClientHello, Error> {
223        trace!("Preparing GREASE ECH extension");
224
225        // Pick a random config id.
226        let mut config_id: [u8; 1] = [0; 1];
227        secure_random.fill(&mut config_id[..])?;
228
229        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        let mut grease_state = EchState::new(
234            &EchConfig {
235                config: EchConfigPayload::V18(EchConfigContents {
236                    key_config: HpkeKeyConfig {
237                        config_id: config_id[0],
238                        kem_id: HpkeKem::DHKEM_P256_HKDF_SHA256,
239                        public_key: PayloadU16::new(self.placeholder_key.0.clone()),
240                        symmetric_cipher_suites: vec![suite.sym],
241                    },
242                    maximum_name_length: 0,
243                    public_name: DnsName::try_from("filler").unwrap(),
244                    extensions: Vec::default(),
245                }),
246                suite: self.suite,
247            },
248            inner_name,
249            false,
250            secure_random,
251            false, // Does not matter if we enable/disable SNI here. Inner hello is not used.
252        )?;
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        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        let payload_len = encoded_inner_hello.len()
260            + suite
261                .sym
262                .aead_id
263                .tag_len()
264                // Safety: we have confirmed the AEAD is supported when building the config. All
265                //  supported AEADs have a tag length.
266                .unwrap();
267        let mut payload = vec![0; payload_len];
268        secure_random.fill(&mut payload)?;
269
270        // Return the GREASE extension.
271        Ok(EncryptedClientHello::Outer(EncryptedClientHelloOuter {
272            cipher_suite: suite.sym,
273            config_id: config_id[0],
274            enc: PayloadU16::new(grease_state.enc.0),
275            payload: PayloadU16::new(payload),
276        }))
277    }
278}
279
280/// An enum representing ECH offer status.
281#[derive(Debug, Clone, Copy, Eq, PartialEq)]
282pub 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).
296pub(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    // Whether a hello retry request accepted our ECH offer, if we received one. RFC 9849
330    // section 6.1.4 makes that decision final.
331    accepted_by_hello_retry_request: Option<bool>,
332}
333
334impl EchState {
335    pub(crate) fn new(
336        config: &EchConfig,
337        inner_name: ServerName<'static>,
338        client_auth_enabled: bool,
339        secure_random: &'static dyn SecureRandom,
340        enable_sni: bool,
341    ) -> Result<Self, Error> {
342        let EchConfigPayload::V18(config_contents) = &config.config else {
343            // the public EchConfig::new() constructor ensures we only have supported
344            // configurations.
345            unreachable!("ECH config version mismatch");
346        };
347        let key_config = &config_contents.key_config;
348
349        // Encapsulate a secret for the server's public key, and set up a sender context
350        // we can use to seal messages.
351        let (enc, sender) = config.suite.setup_sealer(
352            &config.hpke_info(),
353            &HpkePublicKey(key_config.public_key.0.clone()),
354        )?;
355
356        // Start a new transcript buffer for the inner hello.
357        let mut inner_hello_transcript = HandshakeHashBuffer::new();
358        if client_auth_enabled {
359            inner_hello_transcript.set_client_auth_enabled();
360        }
361
362        Ok(Self {
363            secure_random,
364            sender,
365            config_id: key_config.config_id,
366            inner_name,
367            outer_name: config_contents.public_name.clone(),
368            maximum_name_length: config_contents.maximum_name_length,
369            cipher_suite: config.suite.suite().sym,
370            enc,
371            inner_hello_random: Random::new(secure_random)?,
372            inner_hello_transcript,
373            early_data_key_schedule: None,
374            enable_sni,
375            sent_extensions: Vec::new(),
376            accepted_by_hello_retry_request: None,
377        })
378    }
379
380    /// Construct a ClientHelloPayload offering ECH.
381    ///
382    /// An outer hello, with a protected inner hello for the `inner_name` will be returned, and the
383    /// ECH context will be updated to reflect the inner hello that was offered.
384    ///
385    /// If `retry_req` is `Some`, then the outer hello will be constructed for a hello retry request.
386    ///
387    /// If `resuming` is `Some`, then the inner hello will be constructed for a resumption handshake.
388    pub(crate) fn ech_hello(
389        &mut self,
390        mut outer_hello: ClientHelloPayload,
391        retry_req: Option<&HelloRetryRequest>,
392        resuming: &Option<Retrieved<&persist::Tls13ClientSessionValue>>,
393    ) -> Result<ClientHelloPayload, Error> {
394        trace!(
395            "Preparing ECH offer {}",
396            if retry_req.is_some() { "for retry" } else { "" }
397        );
398
399        // Construct the encoded inner hello and update the transcript.
400        let encoded_inner_hello = self.encode_inner_hello(&outer_hello, retry_req, resuming);
401
402        // Complete the ClientHelloOuterAAD with an ech extension, the payload should be a placeholder
403        // of size L, all zeroes. L == length of encrypting encoded client hello inner w/ the selected
404        // HPKE AEAD. (sum of plaintext + tag length, typically).
405        let payload_len = encoded_inner_hello.len()
406            + self
407                .cipher_suite
408                .aead_id
409                .tag_len()
410                // Safety: we've already verified this AEAD is supported when loading the config
411                // that was used to create the ECH context. All supported AEADs have a tag length.
412                .unwrap();
413
414        // Outer hello's created in response to a hello retry request omit the enc value.
415        let enc = match retry_req.is_some() {
416            true => Vec::default(),
417            false => self.enc.0.clone(),
418        };
419
420        fn outer_hello_ext(ctx: &EchState, enc: Vec<u8>, payload: Vec<u8>) -> EncryptedClientHello {
421            EncryptedClientHello::Outer(EncryptedClientHelloOuter {
422                cipher_suite: ctx.cipher_suite,
423                config_id: ctx.config_id,
424                enc: PayloadU16::new(enc),
425                payload: PayloadU16::new(payload),
426            })
427        }
428
429        // The outer handshake is not permitted to resume a session. If we're resuming in the
430        // inner handshake we remove the PSK extension from the outer hello, replacing it
431        // with a GREASE PSK to implement the "ClientHello Malleability Mitigation" mentioned
432        // in 10.12.3.
433        if let Some(psk_offer) = outer_hello.preshared_key_offer.as_mut() {
434            self.grease_psk(psk_offer)?;
435        }
436
437        // To compute the encoded AAD we add a placeholder extension with an empty payload.
438        outer_hello.encrypted_client_hello =
439            Some(outer_hello_ext(self, enc.clone(), vec![0; payload_len]));
440
441        // Next we compute the proper extension payload.
442        let payload = self
443            .sender
444            .seal(&outer_hello.get_encoding(), &encoded_inner_hello)?;
445
446        // And then we replace the placeholder extension with the real one.
447        outer_hello.encrypted_client_hello = Some(outer_hello_ext(self, enc, payload));
448
449        Ok(outer_hello)
450    }
451
452    /// Confirm whether an ECH offer was accepted based on examining the server hello.
453    pub(crate) fn confirm_acceptance(
454        self,
455        ks: &mut KeyScheduleHandshakeStart,
456        server_hello: &ServerHelloPayload,
457        server_hello_encoded: &Payload<'_>,
458        hash: &'static dyn Hash,
459        server_name: &mut ServerName<'static>,
460        common: &mut CommonState,
461    ) -> Result<Option<EchAccepted>, Error> {
462        // After a rejecting hello retry request the handshake continues with the outer hello
463        // without consulting the server hello, see RFC 9849 section 6.1.6.
464        if self.accepted_by_hello_retry_request == Some(false) {
465            trace!("ECH rejected by server in hello retry request");
466            *server_name = ServerName::DnsName(self.outer_name);
467            return Ok(None);
468        }
469
470        // Start the inner transcript hash now that we know the hash algorithm to use.
471        let inner_transcript = self
472            .inner_hello_transcript
473            .start_hash(hash);
474
475        // Fork the transcript that we've started with the inner hello to use for a confirmation step.
476        // We need to preserve the original inner_transcript to use if this confirmation succeeds.
477        let mut confirmation_transcript = inner_transcript.clone();
478
479        // Add the server hello confirmation - this is computed by altering the received
480        // encoding rather than reencoding it.
481        confirmation_transcript
482            .add_message(&Self::server_hello_conf(server_hello, server_hello_encoded));
483
484        // Derive a confirmation secret from the inner hello random and the confirmation transcript.
485        let derived = ks.server_ech_confirmation_secret(
486            self.inner_hello_random.0.as_ref(),
487            confirmation_transcript.current_hash(),
488        );
489
490        // Check that first 8 digits of the derived secret match the last 8 digits of the original
491        // server random. This match signals that the server accepted the ECH offer.
492        // Indexing safety: Random is [0; 32] by construction.
493
494        match ConstantTimeEq::ct_eq(derived.as_ref(), server_hello.random.0[24..].as_ref()).into() {
495            true => {
496                trace!("ECH accepted by server");
497                Ok(Some(EchAccepted {
498                    transcript: inner_transcript,
499                    random: self.inner_hello_random,
500                    sent_extensions: self.sent_extensions,
501                }))
502            }
503            // An accepting hello retry request requires an "illegal_parameter" alert here
504            // -- <https://www.rfc-editor.org/info/rfc9849/#section-6.1.5>
505            false if self.accepted_by_hello_retry_request == Some(true) => Err(common
506                .send_fatal_alert(
507                    AlertDescription::IllegalParameter,
508                    PeerMisbehaved::EchAcceptanceVariedAfterRetry,
509                )),
510            false => {
511                trace!("ECH rejected by server");
512
513                // "If the server rejects ECH, the client proceeds with the handshake, authenticating
514                // for ECHConfig.contents.public_name"
515                // -- <https://www.rfc-editor.org/info/rfc9849/#section-6.1.6>
516                *server_name = ServerName::DnsName(self.outer_name);
517
518                Ok(None)
519            }
520        }
521    }
522
523    pub(crate) fn confirm_hrr_acceptance(
524        &mut self,
525        hrr: &HelloRetryRequest,
526        cs: &Tls13CipherSuite,
527        common: &mut CommonState,
528    ) -> Result<bool, Error> {
529        // The client checks for the "encrypted_client_hello" extension.
530        let ech_conf = match &hrr.encrypted_client_hello {
531            // If none is found, the server has implicitly rejected ECH.
532            None => {
533                self.accepted_by_hello_retry_request = Some(false);
534                return Ok(false);
535            }
536            // Otherwise, if it has a length other than 8, the client aborts the
537            // handshake with a "decode_error" alert.
538            Some(ech_conf) if ech_conf.bytes().len() != 8 => {
539                return Err({
540                    common.send_fatal_alert(
541                        AlertDescription::DecodeError,
542                        PeerMisbehaved::IllegalHelloRetryRequestWithInvalidEch,
543                    )
544                });
545            }
546            Some(ech_conf) => ech_conf,
547        };
548
549        // Otherwise the client computes hrr_accept_confirmation as described in Section
550        // 7.2.1
551        let confirmation_transcript = self.inner_hello_transcript.clone();
552        let mut confirmation_transcript =
553            confirmation_transcript.start_hash(cs.common.hash_provider);
554        confirmation_transcript.rollup_for_hrr();
555        confirmation_transcript.add_message(&Self::hello_retry_request_conf(hrr));
556
557        let derived = server_ech_hrr_confirmation_secret(
558            cs.hkdf_provider,
559            &self.inner_hello_random.0,
560            confirmation_transcript.current_hash(),
561        );
562
563        let accepted = match ConstantTimeEq::ct_eq(derived.as_ref(), ech_conf.bytes()).into() {
564            true => {
565                trace!("ECH accepted by server in hello retry request");
566                true
567            }
568            false => {
569                trace!("ECH rejected by server in hello retry request");
570                false
571            }
572        };
573        self.accepted_by_hello_retry_request = Some(accepted);
574        Ok(accepted)
575    }
576
577    /// Update the ECH context inner hello transcript based on a received hello retry request message.
578    ///
579    /// This will start the in-progress transcript using the given `hash`, convert it into an HRR
580    /// buffer, and then add the hello retry message `m`.
581    pub(crate) fn transcript_hrr_update(&mut self, hash: &'static dyn Hash, m: &Message<'_>) {
582        trace!("Updating ECH inner transcript for HRR");
583
584        let inner_transcript = self
585            .inner_hello_transcript
586            .clone()
587            .start_hash(hash);
588
589        let mut inner_transcript_buffer = inner_transcript.into_hrr_buffer();
590        inner_transcript_buffer.add_message(m);
591        self.inner_hello_transcript = inner_transcript_buffer;
592    }
593
594    // 5.1 "Encoding the ClientHelloInner"
595    fn encode_inner_hello(
596        &mut self,
597        outer_hello: &ClientHelloPayload,
598        retryreq: Option<&HelloRetryRequest>,
599        resuming: &Option<Retrieved<&persist::Tls13ClientSessionValue>>,
600    ) -> Vec<u8> {
601        // Start building an inner hello using the outer_hello as a template.
602        let mut inner_hello = ClientHelloPayload {
603            // Some information is copied over as-is.
604            client_version: outer_hello.client_version,
605            session_id: outer_hello.session_id,
606            compression_methods: outer_hello.compression_methods.clone(),
607
608            // We will build up the included extensions ourselves.
609            extensions: Box::new(ClientExtensions::default()),
610
611            // Set the inner hello random to the one we generated when creating the ECH state.
612            // We hold on to the inner_hello_random in the ECH state to use later for confirming
613            // whether ECH was accepted or not.
614            random: self.inner_hello_random,
615
616            // We remove the empty renegotiation info SCSV from the outer hello's ciphersuite.
617            // Similar to the TLS 1.2 specific extensions we will filter out, this is seen as a
618            // TLS 1.2 only feature by bogo.
619            cipher_suites: outer_hello
620                .cipher_suites
621                .iter()
622                .filter(|cs| **cs != TLS_EMPTY_RENEGOTIATION_INFO_SCSV)
623                .cloned()
624                .collect(),
625        };
626
627        inner_hello.order_seed = outer_hello.order_seed;
628
629        // The inner hello will always have an inner variant of the ECH extension added.
630        // See Section 6.1 rule 4.
631        inner_hello.encrypted_client_hello = Some(EncryptedClientHello::Inner);
632
633        let inner_sni = match &self.inner_name {
634            // The inner hello only gets a SNI value if enable_sni is true and the inner name
635            // is a domain name (not an IP address).
636            ServerName::DnsName(dns_name) if self.enable_sni => Some(dns_name),
637            _ => None,
638        };
639
640        // Now we consider each of the outer hello's extensions - we can either:
641        // 1. Omit the extension if it isn't appropriate (e.g. is a TLS 1.2 extension).
642        // 2. Add the extension to the inner hello as-is.
643        // 3. Compress the extension, by collecting it into a list of to-be-compressed
644        //    extensions we'll handle separately.
645        let outer_extensions = outer_hello.used_extensions_in_encoding_order();
646        let mut compressed_exts = Vec::with_capacity(outer_extensions.len());
647        for ext in outer_extensions {
648            // Some outer hello extensions are only useful in the context where a TLS 1.3
649            // connection allows TLS 1.2. This isn't the case for ECH so we skip adding them
650            // to the inner hello.
651            if matches!(
652                ext,
653                ExtensionType::ExtendedMasterSecret
654                    | ExtensionType::SessionTicket
655                    | ExtensionType::ECPointFormats
656            ) {
657                continue;
658            }
659
660            if ext == ExtensionType::ServerName {
661                // We may want to replace the outer hello SNI with our own inner hello specific SNI.
662                if let Some(sni_value) = inner_sni {
663                    inner_hello.server_name = Some(ServerNamePayload::from(sni_value));
664                }
665                // We don't want to add, or compress, the SNI from the outer hello.
666                continue;
667            }
668
669            // Compressed extensions need to be put aside to include in one contiguous block.
670            // Uncompressed extensions get added directly to the inner hello.
671            if ext.ech_compress() {
672                compressed_exts.push(ext);
673            }
674
675            inner_hello.clone_one(outer_hello, ext);
676        }
677
678        // We've added all the uncompressed extensions. Now we need to add the contiguous
679        // block of to-be-compressed extensions.
680        inner_hello.contiguous_extensions = compressed_exts.clone();
681
682        // Note which extensions we're sending in the inner hello. This may differ from
683        // the outer hello (e.g. the inner hello may omit SNI while the outer hello will
684        // always have the ECH cover name in SNI).
685        self.sent_extensions = inner_hello.collect_used();
686
687        // If we're resuming, we need to update the PSK binder in the inner hello.
688        if let Some(resuming) = resuming.as_ref() {
689            let mut chp = HandshakeMessagePayload(HandshakePayload::ClientHello(inner_hello));
690
691            // Retain the early key schedule we get from processing the binder.
692            self.early_data_key_schedule = Some(tls13::fill_in_psk_binder(
693                resuming,
694                &self.inner_hello_transcript,
695                &mut chp,
696            ));
697
698            // fill_in_psk_binder works on an owned HandshakeMessagePayload, so we need to
699            // extract our inner hello back out of it to retain ownership.
700            inner_hello = match chp.0 {
701                HandshakePayload::ClientHello(chp) => chp,
702                // Safety: we construct the HMP above and know its type unconditionally.
703                _ => unreachable!(),
704            };
705        }
706
707        trace!("ECH Inner Hello: {inner_hello:#?}");
708
709        // Encode the inner hello according to the rules required for ECH. This differs
710        // from the standard encoding in several ways. Notably this is where we will
711        // replace the block of contiguous to-be-compressed extensions with a marker.
712        let mut encoded_hello = inner_hello.ech_inner_encoding(compressed_exts);
713
714        // Calculate padding
715        // max_name_len = L
716        let max_name_len = usize::from(self.maximum_name_length);
717        let max_name_len = if max_name_len > 0 { max_name_len } else { 255 };
718
719        let name_padding_len = match &inner_hello.server_name {
720            Some(ServerNamePayload::SingleDnsName(name)) => {
721                // name.len() = D
722                // max(0, L - D)
723                Ord::max(0, max_name_len.saturating_sub(name.as_ref().len()))
724            }
725            // L + 9
726            // "This is the length of a "server_name" extension with an L-byte name."
727            _ => max_name_len + 9,
728        };
729        encoded_hello.extend(iter::repeat(0).take(name_padding_len));
730
731        // Let L be the length of the EncodedClientHelloInner with all the padding computed so far
732        // Let N = 31 - ((L - 1) % 32) and add N bytes of padding.
733        let padding_len = 31 - ((encoded_hello.len() - 1) % 32);
734        encoded_hello.extend(iter::repeat(0).take(padding_len));
735
736        // Construct the inner hello message that will be used for the transcript.
737        let inner_hello_msg = Message {
738            version: match retryreq {
739                // <https://datatracker.ietf.org/doc/html/rfc8446#section-5.1>:
740                // "This value MUST be set to 0x0303 for all records generated
741                //  by a TLS 1.3 implementation ..."
742                Some(_) => ProtocolVersion::TLSv1_2,
743                // "... other than an initial ClientHello (i.e., one not
744                // generated after a HelloRetryRequest), where it MAY also be
745                // 0x0301 for compatibility purposes"
746                //
747                // (retryreq == None means we're in the "initial ClientHello" case)
748                None => ProtocolVersion::TLSv1_0,
749            },
750            payload: MessagePayload::handshake(HandshakeMessagePayload(
751                HandshakePayload::ClientHello(inner_hello),
752            )),
753        };
754
755        // Update the inner transcript buffer with the inner hello message.
756        self.inner_hello_transcript
757            .add_message(&inner_hello_msg);
758
759        encoded_hello
760    }
761
762    // See https://datatracker.ietf.org/doc/html/draft-ietf-tls-esni-18#name-grease-psk
763    fn grease_psk(&self, psk_offer: &mut PresharedKeyOffer) -> Result<(), Error> {
764        for ident in psk_offer.identities.iter_mut() {
765            // "For each PSK identity advertised in the ClientHelloInner, the
766            // client generates a random PSK identity with the same length."
767            self.secure_random
768                .fill(&mut ident.identity.0)?;
769            // "It also generates a random, 32-bit, unsigned integer to use as
770            // the obfuscated_ticket_age."
771            let mut ticket_age = [0_u8; 4];
772            self.secure_random
773                .fill(&mut ticket_age)?;
774            ident.obfuscated_ticket_age = u32::from_be_bytes(ticket_age);
775        }
776
777        // "Likewise, for each inner PSK binder, the client generates a random string
778        // of the same length."
779        psk_offer.binders = psk_offer
780            .binders
781            .iter()
782            .map(|old_binder| {
783                // We can't access the wrapped binder PresharedKeyBinder's PayloadU8 mutably,
784                // so we construct new PresharedKeyBinder's from scratch with the same length.
785                let mut new_binder = vec![0; old_binder.as_ref().len()];
786                self.secure_random
787                    .fill(&mut new_binder)?;
788                Ok::<PresharedKeyBinder, Error>(PresharedKeyBinder::from(new_binder))
789            })
790            .collect::<Result<_, _>>()?;
791        Ok(())
792    }
793
794    fn server_hello_conf(
795        server_hello: &ServerHelloPayload,
796        server_hello_encoded: &Payload<'_>,
797    ) -> Message<'static> {
798        // The confirmation is computed over the server hello, which has had
799        // its `random` field altered to zero the final 8 bytes.
800        //
801        // nb. we don't require that we can round-trip a `ServerHelloPayload`, to
802        // allow for efficiency in its in-memory representation.  That means
803        // we operate here on the received encoding, as the confirmation needs
804        // to be computed on that.
805        let mut encoded = server_hello_encoded.clone().into_vec();
806        encoded[SERVER_HELLO_ECH_CONFIRMATION_SPAN].fill(0x00);
807
808        Message {
809            version: ProtocolVersion::TLSv1_3,
810            payload: MessagePayload::Handshake {
811                encoded: Payload::Owned(encoded),
812                parsed: HandshakeMessagePayload(HandshakePayload::ServerHello(
813                    server_hello.clone(),
814                )),
815            },
816        }
817    }
818
819    fn hello_retry_request_conf(retry_req: &HelloRetryRequest) -> Message<'_> {
820        Self::ech_conf_message(HandshakeMessagePayload(
821            HandshakePayload::HelloRetryRequest(retry_req.clone()),
822        ))
823    }
824
825    fn ech_conf_message(hmp: HandshakeMessagePayload<'_>) -> Message<'_> {
826        let mut hmp_encoded = Vec::new();
827        hmp.payload_encode(&mut hmp_encoded, Encoding::EchConfirmation);
828        Message {
829            version: ProtocolVersion::TLSv1_3,
830            payload: MessagePayload::Handshake {
831                encoded: Payload::new(hmp_encoded),
832                parsed: hmp,
833            },
834        }
835    }
836}
837
838/// The last eight bytes of the ServerHello's random, taken from a Handshake message containing it.
839///
840/// This has:
841/// - a HandshakeType (1 byte),
842/// - an exterior length (3 bytes),
843/// - the legacy_version (2 bytes), and
844/// - the balance of the random field (24 bytes).
845const SERVER_HELLO_ECH_CONFIRMATION_SPAN: core::ops::Range<usize> =
846    (1 + 3 + 2 + 24)..(1 + 3 + 2 + 32);
847
848/// Returned from EchState::check_acceptance when the server has accepted the ECH offer.
849///
850/// Holds the state required to continue the handshake with the inner hello from the ECH offer.
851pub(crate) struct EchAccepted {
852    pub(crate) transcript: HandshakeHash,
853    pub(crate) random: Random,
854    pub(crate) sent_extensions: Vec<ExtensionType>,
855}
856
857pub(crate) fn fatal_alert_required(
858    retry_configs: Option<Vec<EchConfigPayload>>,
859    common: &mut CommonState,
860) -> Error {
861    common.send_fatal_alert(
862        AlertDescription::EncryptedClientHelloRequired,
863        PeerIncompatible::ServerRejectedEncryptedClientHello(retry_configs),
864    )
865}
866
867#[cfg(test)]
868mod tests {
869    use std::string::String;
870
871    use super::*;
872    use crate::enums::CipherSuite;
873    use crate::msgs::enums::{Compression, HpkeAead, HpkeKdf};
874    use crate::msgs::handshake::{Random, ServerExtensions, SessionId};
875
876    #[test]
877    fn server_hello_conf_alters_server_hello_random() {
878        let server_hello = ServerHelloPayload {
879            legacy_version: ProtocolVersion::TLSv1_2,
880            random: Random([0xffu8; 32]),
881            session_id: SessionId::empty(),
882            cipher_suite: CipherSuite::TLS13_AES_256_GCM_SHA384,
883            compression_method: Compression::Null,
884            extensions: Box::new(ServerExtensions::default()),
885        };
886        let message = Message {
887            version: ProtocolVersion::TLSv1_3,
888            payload: MessagePayload::handshake(HandshakeMessagePayload(
889                HandshakePayload::ServerHello(server_hello.clone()),
890            )),
891        };
892        let Message {
893            payload:
894                MessagePayload::Handshake {
895                    encoded: server_hello_encoded_before,
896                    ..
897                },
898            ..
899        } = &message
900        else {
901            unreachable!("ServerHello is a handshake message");
902        };
903
904        let message = EchState::server_hello_conf(&server_hello, server_hello_encoded_before);
905
906        let Message {
907            payload:
908                MessagePayload::Handshake {
909                    encoded: server_hello_encoded_after,
910                    ..
911                },
912            ..
913        } = &message
914        else {
915            unreachable!("ServerHello is a handshake message");
916        };
917
918        assert_eq!(
919            std::format!("{server_hello_encoded_before:x?}"),
920            "020000280303ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001302000000",
921            "beforehand eight bytes at end of Random should be 0xff here ^^^^^^^^^^^^^^^^            "
922        );
923        assert_eq!(
924            std::format!("{server_hello_encoded_after:x?}"),
925            "020000280303ffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000001302000000",
926            "                          afterwards those bytes are zeroed ^^^^^^^^^^^^^^^^            "
927        );
928    }
929
930    #[test]
931    fn inner_client_hello_length_conceals_inner_name_length() {
932        let base_inner_len = inner_hello_encoding_for_name(dns_name_of_len(1), true).len();
933        assert!(
934            base_inner_len % 32 == 0,
935            "inner hello length must be 32-byte padded"
936        );
937        assert!(
938            base_inner_len >= 256,
939            "inner hello must include inner name and its padding"
940        );
941
942        for inner_name_len in 1..251 {
943            assert_eq!(
944                inner_hello_encoding_for_name(dns_name_of_len(inner_name_len), true).len(),
945                base_inner_len,
946                "all inner hello lengths must be invariant wrt inner name length"
947            );
948        }
949    }
950
951    #[test]
952    fn inner_client_hello_length_does_not_leak_length_of_omitted_inner_name() {
953        let base_inner_len = inner_hello_encoding_for_name(dns_name_of_len(1), false).len();
954        assert!(
955            base_inner_len % 32 == 0,
956            "inner hello length must be 32-byte padded"
957        );
958        assert!(
959            base_inner_len >= 256,
960            "inner hello must include maximum_name_length bytes of padding"
961        );
962
963        for inner_name_len in 1..251 {
964            assert_eq!(
965                inner_hello_encoding_for_name(dns_name_of_len(inner_name_len), false).len(),
966                base_inner_len,
967                "all inner hello lengths must be invariant wrt inner name length"
968            );
969        }
970    }
971
972    #[macro_rules_attribute::apply(test_for_each_provider)]
973    mod hello_retry_request {
974        use super::*;
975        use crate::common_state::Side;
976        use crate::crypto::SharedSecret;
977        use crate::msgs::base::PayloadU16;
978        use crate::msgs::handshake::{HelloRetryRequestExtensions, SupportedProtocolVersions};
979        use crate::tls13::key_schedule::KeySchedulePreHandshake;
980
981        #[test]
982        fn ech_rejection_in_server_hello_after_acceptance_in_hello_retry_request() {
983            let suite = tls13_suite();
984            let hash = suite.common.hash_provider;
985            let mut state = ech_state();
986
987            // the server accepts our offer in its hello retry request...
988            state
989                .ech_hello(outer_hello(), None, &None)
990                .unwrap();
991            let hrr = accepting_hello_retry_request(&state, suite);
992            assert!(
993                state
994                    .confirm_hrr_acceptance(&hrr, suite, &mut CommonState::new(Side::Client))
995                    .unwrap()
996            );
997            state.transcript_hrr_update(hash, &hrr_message(&hrr));
998            state
999                .ech_hello(outer_hello(), Some(&hrr), &None)
1000                .unwrap();
1001
1002            // ...but its server hello does not confirm that acceptance
1003            let server_hello = server_hello(suite);
1004            let mut server_name = inner_name();
1005            assert_eq!(
1006                state
1007                    .confirm_acceptance(
1008                        &mut handshake_key_schedule(suite),
1009                        &server_hello,
1010                        &encode(&server_hello),
1011                        hash,
1012                        &mut server_name,
1013                        &mut CommonState::new(Side::Client),
1014                    )
1015                    .err(),
1016                Some(PeerMisbehaved::EchAcceptanceVariedAfterRetry.into())
1017            );
1018        }
1019
1020        #[test]
1021        fn ech_acceptance_in_server_hello_after_rejection_in_hello_retry_request() {
1022            let suite = tls13_suite();
1023            let hash = suite.common.hash_provider;
1024            let mut key_schedule = handshake_key_schedule(suite);
1025
1026            // without a hello retry request, a confirming server hello accepts ECH
1027            let mut state = ech_state();
1028            state
1029                .ech_hello(outer_hello(), None, &None)
1030                .unwrap();
1031            let server_hello = confirming_server_hello(&state, &mut key_schedule, suite);
1032            let mut server_name = inner_name();
1033            assert!(
1034                state
1035                    .confirm_acceptance(
1036                        &mut key_schedule,
1037                        &server_hello,
1038                        &encode(&server_hello),
1039                        hash,
1040                        &mut server_name,
1041                        &mut CommonState::new(Side::Client),
1042                    )
1043                    .unwrap()
1044                    .is_some()
1045            );
1046            assert_eq!(server_name, inner_name());
1047
1048            // but after a rejecting hello retry request, a confirming server hello cannot revive it
1049            let mut state = ech_state();
1050            state
1051                .ech_hello(outer_hello(), None, &None)
1052                .unwrap();
1053            let hrr = hello_retry_request(suite, None);
1054            assert!(
1055                !state
1056                    .confirm_hrr_acceptance(&hrr, suite, &mut CommonState::new(Side::Client))
1057                    .unwrap()
1058            );
1059            state.transcript_hrr_update(hash, &hrr_message(&hrr));
1060            let server_hello = confirming_server_hello(&state, &mut key_schedule, suite);
1061            let mut server_name = inner_name();
1062            assert!(
1063                state
1064                    .confirm_acceptance(
1065                        &mut key_schedule,
1066                        &server_hello,
1067                        &encode(&server_hello),
1068                        hash,
1069                        &mut server_name,
1070                        &mut CommonState::new(Side::Client),
1071                    )
1072                    .unwrap()
1073                    .is_none()
1074            );
1075            assert_eq!(server_name, ServerName::from(public_name()));
1076        }
1077
1078        fn tls13_suite() -> &'static Tls13CipherSuite {
1079            provider::cipher_suite::TLS13_AES_128_GCM_SHA256
1080                .tls13()
1081                .unwrap()
1082        }
1083
1084        fn ech_state() -> EchState {
1085            let config = EchConfig {
1086                config: EchConfigPayload::V18(EchConfigContents {
1087                    key_config: HpkeKeyConfig {
1088                        config_id: 0,
1089                        kem_id: MockHpke::SUITE.kem,
1090                        public_key: PayloadU16::new(vec![0; 32]),
1091                        symmetric_cipher_suites: vec![MockHpke::SUITE.sym],
1092                    },
1093                    maximum_name_length: 255,
1094                    public_name: public_name(),
1095                    extensions: vec![],
1096                }),
1097                suite: &MockHpke,
1098            };
1099
1100            EchState::new(&config, inner_name(), false, &FixedRandom, true).unwrap()
1101        }
1102
1103        fn inner_name() -> ServerName<'static> {
1104            ServerName::try_from("inner.example.com").unwrap()
1105        }
1106
1107        fn public_name() -> DnsName<'static> {
1108            DnsName::try_from("public.example.com").unwrap()
1109        }
1110
1111        fn outer_hello() -> ClientHelloPayload {
1112            ClientHelloPayload {
1113                client_version: ProtocolVersion::TLSv1_2,
1114                random: Random([0u8; 32]),
1115                session_id: SessionId::empty(),
1116                cipher_suites: vec![tls13_suite().common.suite],
1117                compression_methods: vec![Compression::Null],
1118                extensions: Box::new(ClientExtensions {
1119                    server_name: Some(ServerNamePayload::from(&public_name())),
1120                    supported_versions: Some(SupportedProtocolVersions {
1121                        tls13: true,
1122                        tls12: false,
1123                    }),
1124                    ..Default::default()
1125                }),
1126            }
1127        }
1128
1129        fn hello_retry_request(
1130            suite: &'static Tls13CipherSuite,
1131            encrypted_client_hello: Option<Payload<'static>>,
1132        ) -> HelloRetryRequest {
1133            HelloRetryRequest {
1134                legacy_version: ProtocolVersion::TLSv1_2,
1135                session_id: SessionId::empty(),
1136                cipher_suite: suite.common.suite,
1137                extensions: HelloRetryRequestExtensions {
1138                    cookie: Some(PayloadU16::new(vec![1, 2, 3, 4])),
1139                    supported_versions: Some(ProtocolVersion::TLSv1_3),
1140                    encrypted_client_hello,
1141                    ..HelloRetryRequestExtensions::default()
1142                },
1143            }
1144        }
1145
1146        /// A hello retry request accepting the offer in `state`, see RFC 9849 section 7.2.1.
1147        fn accepting_hello_retry_request(
1148            state: &EchState,
1149            suite: &'static Tls13CipherSuite,
1150        ) -> HelloRetryRequest {
1151            let mut hrr = hello_retry_request(suite, Some(Payload::new(vec![0u8; 8])));
1152            let mut transcript = state
1153                .inner_hello_transcript
1154                .clone()
1155                .start_hash(suite.common.hash_provider);
1156            transcript.rollup_for_hrr();
1157            transcript.add_message(&EchState::hello_retry_request_conf(&hrr));
1158            let confirmation = server_ech_hrr_confirmation_secret(
1159                suite.hkdf_provider,
1160                &state.inner_hello_random.0,
1161                transcript.current_hash(),
1162            );
1163            hrr.encrypted_client_hello = Some(Payload::new(confirmation.to_vec()));
1164            hrr
1165        }
1166
1167        fn hrr_message(hrr: &HelloRetryRequest) -> Message<'static> {
1168            Message {
1169                version: ProtocolVersion::TLSv1_2,
1170                payload: MessagePayload::handshake(HandshakeMessagePayload(
1171                    HandshakePayload::HelloRetryRequest(hrr.clone()),
1172                )),
1173            }
1174        }
1175
1176        fn server_hello(suite: &'static Tls13CipherSuite) -> ServerHelloPayload {
1177            ServerHelloPayload {
1178                legacy_version: ProtocolVersion::TLSv1_2,
1179                random: Random([0x55u8; 32]),
1180                session_id: SessionId::empty(),
1181                cipher_suite: suite.common.suite,
1182                compression_method: Compression::Null,
1183                extensions: Box::new(ServerExtensions::default()),
1184            }
1185        }
1186
1187        /// A server hello accepting the offer in `state`, see RFC 9849 section 7.2.
1188        fn confirming_server_hello(
1189            state: &EchState,
1190            key_schedule: &mut KeyScheduleHandshakeStart,
1191            suite: &'static Tls13CipherSuite,
1192        ) -> ServerHelloPayload {
1193            let mut server_hello = server_hello(suite);
1194            let mut transcript = state
1195                .inner_hello_transcript
1196                .clone()
1197                .start_hash(suite.common.hash_provider);
1198            transcript.add_message(&EchState::server_hello_conf(
1199                &server_hello,
1200                &encode(&server_hello),
1201            ));
1202            let confirmation = key_schedule.server_ech_confirmation_secret(
1203                &state.inner_hello_random.0,
1204                transcript.current_hash(),
1205            );
1206            server_hello.random.0[24..].copy_from_slice(&confirmation);
1207            server_hello
1208        }
1209
1210        fn encode(server_hello: &ServerHelloPayload) -> Payload<'static> {
1211            Payload::new(
1212                HandshakeMessagePayload(HandshakePayload::ServerHello(server_hello.clone()))
1213                    .get_encoding(),
1214            )
1215        }
1216
1217        fn handshake_key_schedule(suite: &'static Tls13CipherSuite) -> KeyScheduleHandshakeStart {
1218            KeySchedulePreHandshake::new(suite).into_handshake(SharedSecret::from(&[0u8; 32][..]))
1219        }
1220    }
1221
1222    fn inner_hello_encoding_for_name(name: DnsName<'static>, enable_sni: bool) -> Vec<u8> {
1223        let config = EchConfig {
1224            config: EchConfigPayload::V18(EchConfigContents {
1225                key_config: HpkeKeyConfig {
1226                    config_id: 0,
1227                    kem_id: MockHpke::SUITE.kem,
1228                    public_key: PayloadU16::new(vec![0; 32]),
1229                    symmetric_cipher_suites: vec![],
1230                },
1231                maximum_name_length: 255,
1232                public_name: DnsName::try_from("public").unwrap(),
1233                extensions: vec![],
1234            }),
1235            suite: &MockHpke,
1236        };
1237
1238        EchState::new(
1239            &config,
1240            ServerName::from(name.clone()),
1241            false,
1242            &FixedRandom,
1243            enable_sni,
1244        )
1245        .unwrap()
1246        .encode_inner_hello(
1247            &ClientHelloPayload {
1248                client_version: ProtocolVersion::TLSv1_3,
1249                random: Random([0u8; 32]),
1250                session_id: SessionId::empty(),
1251                cipher_suites: vec![],
1252                compression_methods: vec![Compression::Null],
1253                extensions: Box::new(ClientExtensions {
1254                    server_name: Some(ServerNamePayload::from(&name)),
1255                    ..Default::default()
1256                }),
1257            },
1258            None,
1259            &None,
1260        )
1261    }
1262
1263    fn dns_name_of_len(mut len: usize) -> DnsName<'static> {
1264        let mut s = String::new();
1265        let labels = len.div_ceil(63);
1266        for _ in 0..labels {
1267            let chars = Ord::min(len, 63);
1268            len -= chars;
1269            for _ in 0..chars {
1270                s.push('a');
1271            }
1272            if len != 0 {
1273                s.push('.');
1274            }
1275        }
1276        DnsName::try_from(s).unwrap()
1277    }
1278
1279    #[derive(Debug)]
1280    struct MockHpke;
1281
1282    impl MockHpke {
1283        const SUITE: HpkeSuite = HpkeSuite {
1284            kem: HpkeKem::DHKEM_P256_HKDF_SHA256,
1285            sym: HpkeSymmetricCipherSuite {
1286                kdf_id: HpkeKdf::HKDF_SHA256,
1287                aead_id: HpkeAead::AES_128_GCM,
1288            },
1289        };
1290    }
1291
1292    impl Hpke for MockHpke {
1293        #[cfg_attr(coverage_nightly, coverage(off))]
1294        fn seal(
1295            &self,
1296            _info: &[u8],
1297            _aad: &[u8],
1298            _plaintext: &[u8],
1299            _pub_key: &HpkePublicKey,
1300        ) -> Result<(EncapsulatedSecret, Vec<u8>), Error> {
1301            todo!()
1302        }
1303
1304        fn setup_sealer(
1305            &self,
1306            _info: &[u8],
1307            _pub_key: &HpkePublicKey,
1308        ) -> Result<(EncapsulatedSecret, Box<dyn HpkeSealer + 'static>), Error> {
1309            Ok((EncapsulatedSecret(vec![]), Box::new(MockHpkeSealer)))
1310        }
1311
1312        #[cfg_attr(coverage_nightly, coverage(off))]
1313        fn open(
1314            &self,
1315            _enc: &EncapsulatedSecret,
1316            _info: &[u8],
1317            _aad: &[u8],
1318            _ciphertext: &[u8],
1319            _secret_key: &crate::crypto::hpke::HpkePrivateKey,
1320        ) -> Result<Vec<u8>, Error> {
1321            todo!()
1322        }
1323
1324        #[cfg_attr(coverage_nightly, coverage(off))]
1325        fn setup_opener(
1326            &self,
1327            _enc: &EncapsulatedSecret,
1328            _info: &[u8],
1329            _secret_key: &crate::crypto::hpke::HpkePrivateKey,
1330        ) -> Result<Box<dyn crate::crypto::hpke::HpkeOpener + 'static>, Error> {
1331            todo!()
1332        }
1333
1334        #[cfg_attr(coverage_nightly, coverage(off))]
1335        fn generate_key_pair(
1336            &self,
1337        ) -> Result<(HpkePublicKey, crate::crypto::hpke::HpkePrivateKey), Error> {
1338            todo!()
1339        }
1340
1341        fn suite(&self) -> HpkeSuite {
1342            Self::SUITE
1343        }
1344    }
1345
1346    #[derive(Debug)]
1347    struct MockHpkeSealer;
1348
1349    impl HpkeSealer for MockHpkeSealer {
1350        fn seal(&mut self, _aad: &[u8], plaintext: &[u8]) -> Result<Vec<u8>, Error> {
1351            // ciphertext length as if sealed with the AEAD named in `MockHpke::SUITE`
1352            Ok(vec![0xff; plaintext.len() + 16])
1353        }
1354    }
1355
1356    #[derive(Debug)]
1357    struct FixedRandom;
1358
1359    impl SecureRandom for FixedRandom {
1360        fn fill(&self, buf: &mut [u8]) -> Result<(), crate::rand::GetRandomFailed> {
1361            buf.fill(0x55);
1362            Ok(())
1363        }
1364    }
1365}