/rust/registry/src/index.crates.io-6f17d22bba15001f/rustls-0.23.29/src/client/hs.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use alloc::borrow::ToOwned; |
2 | | use alloc::boxed::Box; |
3 | | use alloc::vec; |
4 | | use alloc::vec::Vec; |
5 | | use core::ops::Deref; |
6 | | |
7 | | use pki_types::ServerName; |
8 | | |
9 | | #[cfg(feature = "tls12")] |
10 | | use super::tls12; |
11 | | use super::{ResolvesClientCert, Tls12Resumption}; |
12 | | use crate::SupportedCipherSuite; |
13 | | #[cfg(feature = "logging")] |
14 | | use crate::bs_debug; |
15 | | use crate::check::inappropriate_handshake_message; |
16 | | use crate::client::client_conn::ClientConnectionData; |
17 | | use crate::client::common::ClientHelloDetails; |
18 | | use crate::client::ech::EchState; |
19 | | use crate::client::{ClientConfig, EchMode, EchStatus, tls13}; |
20 | | use crate::common_state::{CommonState, HandshakeKind, KxState, State}; |
21 | | use crate::conn::ConnectionRandoms; |
22 | | use crate::crypto::{ActiveKeyExchange, KeyExchangeAlgorithm}; |
23 | | use crate::enums::{ |
24 | | AlertDescription, CertificateType, CipherSuite, ContentType, HandshakeType, ProtocolVersion, |
25 | | }; |
26 | | use crate::error::{Error, PeerIncompatible, PeerMisbehaved}; |
27 | | use crate::hash_hs::HandshakeHashBuffer; |
28 | | use crate::log::{debug, trace}; |
29 | | use crate::msgs::base::Payload; |
30 | | use crate::msgs::enums::{Compression, ExtensionType}; |
31 | | use crate::msgs::handshake::{ |
32 | | CertificateStatusRequest, ClientExtensions, ClientExtensionsInput, ClientHelloPayload, |
33 | | ClientSessionTicket, EncryptedClientHello, HandshakeMessagePayload, HandshakePayload, |
34 | | HelloRetryRequest, KeyShareEntry, ProtocolName, PskKeyExchangeModes, Random, ServerNamePayload, |
35 | | SessionId, SupportedEcPointFormats, SupportedProtocolVersions, TransportParameters, |
36 | | }; |
37 | | use crate::msgs::message::{Message, MessagePayload}; |
38 | | use crate::msgs::persist; |
39 | | use crate::sync::Arc; |
40 | | use crate::tls13::key_schedule::KeyScheduleEarly; |
41 | | use crate::verify::ServerCertVerifier; |
42 | | |
43 | | pub(super) type NextState<'a> = Box<dyn State<ClientConnectionData> + 'a>; |
44 | | pub(super) type NextStateOrError<'a> = Result<NextState<'a>, Error>; |
45 | | pub(super) type ClientContext<'a> = crate::common_state::Context<'a, ClientConnectionData>; |
46 | | |
47 | | struct ExpectServerHello { |
48 | | input: ClientHelloInput, |
49 | | transcript_buffer: HandshakeHashBuffer, |
50 | | // The key schedule for sending early data. |
51 | | // |
52 | | // If the server accepts the PSK used for early data then |
53 | | // this is used to compute the rest of the key schedule. |
54 | | // Otherwise, it is thrown away. |
55 | | // |
56 | | // If this is `None` then we do not support early data. |
57 | | early_data_key_schedule: Option<KeyScheduleEarly>, |
58 | | offered_key_share: Option<Box<dyn ActiveKeyExchange>>, |
59 | | suite: Option<SupportedCipherSuite>, |
60 | | ech_state: Option<EchState>, |
61 | | } |
62 | | |
63 | | struct ExpectServerHelloOrHelloRetryRequest { |
64 | | next: ExpectServerHello, |
65 | | extra_exts: ClientExtensionsInput<'static>, |
66 | | } |
67 | | |
68 | | pub(super) struct ClientHelloInput { |
69 | | pub(super) config: Arc<ClientConfig>, |
70 | | pub(super) resuming: Option<persist::Retrieved<ClientSessionValue>>, |
71 | | pub(super) random: Random, |
72 | | pub(super) sent_tls13_fake_ccs: bool, |
73 | | pub(super) hello: ClientHelloDetails, |
74 | | pub(super) session_id: SessionId, |
75 | | pub(super) server_name: ServerName<'static>, |
76 | | pub(super) prev_ech_ext: Option<EncryptedClientHello>, |
77 | | } |
78 | | |
79 | | impl ClientHelloInput { |
80 | 0 | pub(super) fn new( |
81 | 0 | server_name: ServerName<'static>, |
82 | 0 | extra_exts: &ClientExtensionsInput<'_>, |
83 | 0 | cx: &mut ClientContext<'_>, |
84 | 0 | config: Arc<ClientConfig>, |
85 | 0 | ) -> Result<Self, Error> { |
86 | 0 | let mut resuming = ClientSessionValue::retrieve(&server_name, &config, cx); |
87 | 0 | let session_id = match &mut resuming { |
88 | 0 | Some(_resuming) => { |
89 | 0 | debug!("Resuming session"); |
90 | 0 | match &mut _resuming.value { |
91 | 0 | #[cfg(feature = "tls12")] |
92 | 0 | ClientSessionValue::Tls12(inner) => { |
93 | 0 | // If we have a ticket, we use the sessionid as a signal that |
94 | 0 | // we're doing an abbreviated handshake. See section 3.4 in |
95 | 0 | // RFC5077. |
96 | 0 | if !inner.ticket().0.is_empty() { |
97 | 0 | inner.session_id = SessionId::random(config.provider.secure_random)?; |
98 | 0 | } |
99 | 0 | Some(inner.session_id) |
100 | 0 | } |
101 | 0 | _ => None, |
102 | | } |
103 | | } |
104 | | _ => { |
105 | 0 | debug!("Not resuming any session"); |
106 | 0 | None |
107 | | } |
108 | | }; |
109 | | |
110 | | // https://tools.ietf.org/html/rfc8446#appendix-D.4 |
111 | | // https://tools.ietf.org/html/draft-ietf-quic-tls-34#section-8.4 |
112 | 0 | let session_id = match session_id { |
113 | 0 | Some(session_id) => session_id, |
114 | 0 | None if cx.common.is_quic() => SessionId::empty(), |
115 | 0 | None if !config.supports_version(ProtocolVersion::TLSv1_3) => SessionId::empty(), |
116 | 0 | None => SessionId::random(config.provider.secure_random)?, |
117 | | }; |
118 | | |
119 | 0 | let hello = ClientHelloDetails::new( |
120 | 0 | extra_exts |
121 | 0 | .protocols |
122 | 0 | .clone() |
123 | 0 | .unwrap_or_default(), |
124 | 0 | crate::rand::random_u16(config.provider.secure_random)?, |
125 | | ); |
126 | | |
127 | | Ok(Self { |
128 | 0 | resuming, |
129 | 0 | random: Random::new(config.provider.secure_random)?, |
130 | | sent_tls13_fake_ccs: false, |
131 | 0 | hello, |
132 | 0 | session_id, |
133 | 0 | server_name, |
134 | 0 | prev_ech_ext: None, |
135 | 0 | config, |
136 | | }) |
137 | 0 | } Unexecuted instantiation: <rustls::client::hs::ClientHelloInput>::new Unexecuted instantiation: <rustls::client::hs::ClientHelloInput>::new |
138 | | |
139 | 0 | pub(super) fn start_handshake( |
140 | 0 | self, |
141 | 0 | extra_exts: ClientExtensionsInput<'static>, |
142 | 0 | cx: &mut ClientContext<'_>, |
143 | 0 | ) -> NextStateOrError<'static> { |
144 | 0 | let mut transcript_buffer = HandshakeHashBuffer::new(); |
145 | 0 | if self |
146 | 0 | .config |
147 | 0 | .client_auth_cert_resolver |
148 | 0 | .has_certs() |
149 | 0 | { |
150 | 0 | transcript_buffer.set_client_auth_enabled(); |
151 | 0 | } |
152 | | |
153 | 0 | let key_share = if self.config.needs_key_share() { |
154 | 0 | Some(tls13::initial_key_share( |
155 | 0 | &self.config, |
156 | 0 | &self.server_name, |
157 | 0 | &mut cx.common.kx_state, |
158 | 0 | )?) |
159 | | } else { |
160 | 0 | None |
161 | | }; |
162 | | |
163 | 0 | let ech_state = match self.config.ech_mode.as_ref() { |
164 | 0 | Some(EchMode::Enable(ech_config)) => { |
165 | 0 | Some(ech_config.state(self.server_name.clone(), &self.config)?) |
166 | | } |
167 | 0 | _ => None, |
168 | | }; |
169 | | |
170 | 0 | emit_client_hello_for_retry( |
171 | 0 | transcript_buffer, |
172 | 0 | None, |
173 | 0 | key_share, |
174 | 0 | extra_exts, |
175 | 0 | None, |
176 | 0 | self, |
177 | 0 | cx, |
178 | 0 | ech_state, |
179 | 0 | ) |
180 | 0 | } Unexecuted instantiation: <rustls::client::hs::ClientHelloInput>::start_handshake Unexecuted instantiation: <rustls::client::hs::ClientHelloInput>::start_handshake |
181 | | } |
182 | | |
183 | | /// Emits the initial ClientHello or a ClientHello in response to |
184 | | /// a HelloRetryRequest. |
185 | | /// |
186 | | /// `retryreq` and `suite` are `None` if this is the initial |
187 | | /// ClientHello. |
188 | 0 | fn emit_client_hello_for_retry( |
189 | 0 | mut transcript_buffer: HandshakeHashBuffer, |
190 | 0 | retryreq: Option<&HelloRetryRequest>, |
191 | 0 | key_share: Option<Box<dyn ActiveKeyExchange>>, |
192 | 0 | extra_exts: ClientExtensionsInput<'static>, |
193 | 0 | suite: Option<SupportedCipherSuite>, |
194 | 0 | mut input: ClientHelloInput, |
195 | 0 | cx: &mut ClientContext<'_>, |
196 | 0 | mut ech_state: Option<EchState>, |
197 | 0 | ) -> NextStateOrError<'static> { |
198 | 0 | let config = &input.config; |
199 | | // Defense in depth: the ECH state should be None if ECH is disabled based on config |
200 | | // builder semantics. |
201 | 0 | let forbids_tls12 = cx.common.is_quic() || ech_state.is_some(); |
202 | | |
203 | 0 | let supported_versions = SupportedProtocolVersions { |
204 | 0 | tls12: config.supports_version(ProtocolVersion::TLSv1_2) && !forbids_tls12, |
205 | 0 | tls13: config.supports_version(ProtocolVersion::TLSv1_3), |
206 | 0 | }; |
207 | 0 |
|
208 | 0 | // should be unreachable thanks to config builder |
209 | 0 | assert!(supported_versions.any(|_| true)); Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#11} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#11} |
210 | | |
211 | 0 | let mut exts = Box::new(ClientExtensions { |
212 | 0 | // offer groups which are usable for any offered version |
213 | 0 | named_groups: Some( |
214 | 0 | config |
215 | 0 | .provider |
216 | 0 | .kx_groups |
217 | 0 | .iter() |
218 | 0 | .filter(|skxg| supported_versions.any(|v| skxg.usable_for_version(v))) Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#0} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#0} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#0}::{closure#0} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#0}::{closure#0} |
219 | 0 | .map(|skxg| skxg.name()) Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#1} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#1} |
220 | 0 | .collect(), |
221 | 0 | ), |
222 | 0 | supported_versions: Some(supported_versions), |
223 | 0 | signature_schemes: Some( |
224 | 0 | config |
225 | 0 | .verifier |
226 | 0 | .supported_verify_schemes(), |
227 | 0 | ), |
228 | 0 | extended_master_secret_request: Some(()), |
229 | 0 | certificate_status_request: Some(CertificateStatusRequest::build_ocsp()), |
230 | 0 | protocols: extra_exts.protocols.clone(), |
231 | 0 | ..Default::default() |
232 | 0 | }); |
233 | 0 |
|
234 | 0 | match extra_exts.transport_parameters.clone() { |
235 | 0 | Some(TransportParameters::Quic(v)) => exts.transport_parameters = Some(v), |
236 | 0 | Some(TransportParameters::QuicDraft(v)) => exts.transport_parameters_draft = Some(v), |
237 | 0 | None => {} |
238 | | }; |
239 | | |
240 | 0 | if supported_versions.tls13 { |
241 | 0 | if let Some(cas_extension) = config.verifier.root_hint_subjects() { |
242 | 0 | exts.certificate_authority_names = Some(cas_extension.to_owned()); |
243 | 0 | } |
244 | 0 | } |
245 | | |
246 | | // Send the ECPointFormat extension only if we are proposing ECDHE |
247 | 0 | if config |
248 | 0 | .provider |
249 | 0 | .kx_groups |
250 | 0 | .iter() |
251 | 0 | .any(|skxg| skxg.name().key_exchange_algorithm() == KeyExchangeAlgorithm::ECDHE) Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#2} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#2} |
252 | 0 | { |
253 | 0 | exts.ec_point_formats = Some(SupportedEcPointFormats::default()); |
254 | 0 | } |
255 | | |
256 | 0 | exts.server_name = match (ech_state.as_ref(), config.enable_sni) { |
257 | | // If we have ECH state we have a "cover name" to send in the outer hello |
258 | | // as the SNI domain name. This happens unconditionally so we ignore the |
259 | | // `enable_sni` value. That will be used later to decide what to do for |
260 | | // the protected inner hello's SNI. |
261 | 0 | (Some(ech_state), _) => Some(ServerNamePayload::from(&ech_state.outer_name)), |
262 | | |
263 | | // If we have no ECH state, and SNI is enabled, try to use the input server_name |
264 | | // for the SNI domain name. |
265 | 0 | (None, true) => match &input.server_name { |
266 | 0 | ServerName::DnsName(dns_name) => Some(ServerNamePayload::from(dns_name)), |
267 | 0 | _ => None, |
268 | | }, |
269 | | |
270 | | // If we have no ECH state, and SNI is not enabled, there's nothing to do. |
271 | 0 | (None, false) => None, |
272 | | }; |
273 | | |
274 | 0 | if let Some(key_share) = &key_share { |
275 | 0 | debug_assert!(supported_versions.tls13); |
276 | 0 | let mut shares = vec![KeyShareEntry::new(key_share.group(), key_share.pub_key())]; |
277 | 0 |
|
278 | 0 | if !retryreq |
279 | 0 | .map(|rr| rr.key_share.is_some()) Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#3} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#3} |
280 | 0 | .unwrap_or_default() |
281 | | { |
282 | | // Only for the initial client hello, or a HRR that does not specify a kx group, |
283 | | // see if we can send a second KeyShare for "free". We only do this if the same |
284 | | // algorithm is also supported separately by our provider for this version |
285 | | // (`find_kx_group` looks that up). |
286 | 0 | if let Some((component_group, component_share)) = |
287 | 0 | key_share |
288 | 0 | .hybrid_component() |
289 | 0 | .filter(|(group, _)| { |
290 | 0 | config |
291 | 0 | .find_kx_group(*group, ProtocolVersion::TLSv1_3) |
292 | 0 | .is_some() |
293 | 0 | }) Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#4} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#4} |
294 | 0 | { |
295 | 0 | shares.push(KeyShareEntry::new(component_group, component_share)); |
296 | 0 | } |
297 | 0 | } |
298 | | |
299 | 0 | exts.key_shares = Some(shares); |
300 | 0 | } |
301 | | |
302 | 0 | if let Some(cookie) = retryreq.and_then(|hrr| hrr.cookie.as_ref()) { Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#5} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#5} |
303 | 0 | exts.cookie = Some(cookie.clone()); |
304 | 0 | } |
305 | | |
306 | 0 | if supported_versions.tls13 { |
307 | 0 | // We could support PSK_KE here too. Such connections don't |
308 | 0 | // have forward secrecy, and are similar to TLS1.2 resumption. |
309 | 0 | exts.preshared_key_modes = Some(PskKeyExchangeModes { |
310 | 0 | psk: false, |
311 | 0 | psk_dhe: true, |
312 | 0 | }); |
313 | 0 | } |
314 | | |
315 | | input.hello.offered_cert_compression = |
316 | 0 | if supported_versions.tls13 && !config.cert_decompressors.is_empty() { |
317 | 0 | exts.certificate_compression_algorithms = Some( |
318 | 0 | config |
319 | 0 | .cert_decompressors |
320 | 0 | .iter() |
321 | 0 | .map(|dec| dec.algorithm()) Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#6} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#6} |
322 | 0 | .collect(), |
323 | 0 | ); |
324 | 0 | true |
325 | | } else { |
326 | 0 | false |
327 | | }; |
328 | | |
329 | 0 | if config |
330 | 0 | .client_auth_cert_resolver |
331 | 0 | .only_raw_public_keys() |
332 | 0 | { |
333 | 0 | exts.client_certificate_types = Some(vec![CertificateType::RawPublicKey]); |
334 | 0 | } |
335 | | |
336 | 0 | if config |
337 | 0 | .verifier |
338 | 0 | .requires_raw_public_keys() |
339 | 0 | { |
340 | 0 | exts.server_certificate_types = Some(vec![CertificateType::RawPublicKey]); |
341 | 0 | } |
342 | | |
343 | | // If this is a second client hello we're constructing in response to an HRR, and |
344 | | // we've rejected ECH or sent GREASE ECH, then we need to carry forward the |
345 | | // exact same ECH extension we used in the first hello. |
346 | 0 | if matches!(cx.data.ech_status, EchStatus::Rejected | EchStatus::Grease) & retryreq.is_some() { |
347 | 0 | if let Some(prev_ech_ext) = input.prev_ech_ext.take() { |
348 | 0 | exts.encrypted_client_hello = Some(prev_ech_ext); |
349 | 0 | } |
350 | 0 | } |
351 | | |
352 | | // Do we have a SessionID or ticket cached for this host? |
353 | 0 | let tls13_session = prepare_resumption(&input.resuming, &mut exts, suite, cx, config); |
354 | 0 |
|
355 | 0 | // Extensions MAY be randomized |
356 | 0 | // but they also need to keep the same order as the previous ClientHello |
357 | 0 | exts.order_seed = input.hello.extension_order_seed; |
358 | 0 |
|
359 | 0 | let mut cipher_suites: Vec<_> = config |
360 | 0 | .provider |
361 | 0 | .cipher_suites |
362 | 0 | .iter() |
363 | 0 | .filter_map(|cs| match cs.usable_for_protocol(cx.common.protocol) { |
364 | 0 | true => Some(cs.suite()), |
365 | 0 | false => None, |
366 | 0 | }) Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#7} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#7} |
367 | 0 | .collect(); |
368 | 0 |
|
369 | 0 | if supported_versions.tls12 { |
370 | 0 | // We don't do renegotiation at all, in fact. |
371 | 0 | cipher_suites.push(CipherSuite::TLS_EMPTY_RENEGOTIATION_INFO_SCSV); |
372 | 0 | } |
373 | | |
374 | 0 | let mut chp_payload = ClientHelloPayload { |
375 | 0 | client_version: ProtocolVersion::TLSv1_2, |
376 | 0 | random: input.random, |
377 | 0 | session_id: input.session_id, |
378 | 0 | cipher_suites, |
379 | 0 | compression_methods: vec![Compression::Null], |
380 | 0 | extensions: exts, |
381 | 0 | }; |
382 | 0 |
|
383 | 0 | let ech_grease_ext = config |
384 | 0 | .ech_mode |
385 | 0 | .as_ref() |
386 | 0 | .and_then(|mode| match mode { |
387 | 0 | EchMode::Grease(cfg) => Some(cfg.grease_ext( |
388 | 0 | config.provider.secure_random, |
389 | 0 | input.server_name.clone(), |
390 | 0 | &chp_payload, |
391 | 0 | )), |
392 | 0 | _ => None, |
393 | 0 | }); Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#8} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#8} |
394 | 0 |
|
395 | 0 | match (cx.data.ech_status, &mut ech_state) { |
396 | | // If we haven't offered ECH, or have offered ECH but got a non-rejecting HRR, then |
397 | | // we need to replace the client hello payload with an ECH client hello payload. |
398 | 0 | (EchStatus::NotOffered | EchStatus::Offered, Some(ech_state)) => { |
399 | | // Replace the client hello payload with an ECH client hello payload. |
400 | 0 | chp_payload = ech_state.ech_hello(chp_payload, retryreq, &tls13_session)?; |
401 | 0 | cx.data.ech_status = EchStatus::Offered; |
402 | 0 | // Store the ECH extension in case we need to carry it forward in a subsequent hello. |
403 | 0 | input.prev_ech_ext = chp_payload |
404 | 0 | .encrypted_client_hello |
405 | 0 | .clone(); |
406 | | } |
407 | | // If we haven't offered ECH, and have no ECH state, then consider whether to use GREASE |
408 | | // ECH. |
409 | | (EchStatus::NotOffered, None) => { |
410 | 0 | if let Some(grease_ext) = ech_grease_ext { |
411 | | // Add the GREASE ECH extension. |
412 | 0 | let grease_ext = grease_ext?; |
413 | 0 | chp_payload.encrypted_client_hello = Some(grease_ext.clone()); |
414 | 0 | cx.data.ech_status = EchStatus::Grease; |
415 | 0 | // Store the GREASE ECH extension in case we need to carry it forward in a |
416 | 0 | // subsequent hello. |
417 | 0 | input.prev_ech_ext = Some(grease_ext); |
418 | 0 | } |
419 | | } |
420 | 0 | _ => {} |
421 | | } |
422 | | |
423 | | // Note what extensions we sent. |
424 | 0 | input.hello.sent_extensions = chp_payload.collect_used(); |
425 | 0 |
|
426 | 0 | let mut chp = HandshakeMessagePayload(HandshakePayload::ClientHello(chp_payload)); |
427 | | |
428 | 0 | let tls13_early_data_key_schedule = match (ech_state.as_mut(), tls13_session) { |
429 | | // If we're performing ECH and resuming, then the PSK binder will have been dealt with |
430 | | // separately, and we need to take the early_data_key_schedule computed for the inner hello. |
431 | 0 | (Some(ech_state), Some(tls13_session)) => ech_state |
432 | 0 | .early_data_key_schedule |
433 | 0 | .take() |
434 | 0 | .map(|schedule| (tls13_session.suite(), schedule)), Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#9} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#9} |
435 | | |
436 | | // When we're not doing ECH and resuming, then the PSK binder need to be filled in as |
437 | | // normal. |
438 | 0 | (_, Some(tls13_session)) => Some(( |
439 | 0 | tls13_session.suite(), |
440 | 0 | tls13::fill_in_psk_binder(&tls13_session, &transcript_buffer, &mut chp), |
441 | 0 | )), |
442 | | |
443 | | // No early key schedule in other cases. |
444 | 0 | _ => None, |
445 | | }; |
446 | | |
447 | 0 | let ch = Message { |
448 | 0 | version: match retryreq { |
449 | | // <https://datatracker.ietf.org/doc/html/rfc8446#section-5.1>: |
450 | | // "This value MUST be set to 0x0303 for all records generated |
451 | | // by a TLS 1.3 implementation ..." |
452 | 0 | Some(_) => ProtocolVersion::TLSv1_2, |
453 | | // "... other than an initial ClientHello (i.e., one not |
454 | | // generated after a HelloRetryRequest), where it MAY also be |
455 | | // 0x0301 for compatibility purposes" |
456 | | // |
457 | | // (retryreq == None means we're in the "initial ClientHello" case) |
458 | 0 | None => ProtocolVersion::TLSv1_0, |
459 | | }, |
460 | 0 | payload: MessagePayload::handshake(chp), |
461 | 0 | }; |
462 | 0 |
|
463 | 0 | if retryreq.is_some() { |
464 | 0 | // send dummy CCS to fool middleboxes prior |
465 | 0 | // to second client hello |
466 | 0 | tls13::emit_fake_ccs(&mut input.sent_tls13_fake_ccs, cx.common); |
467 | 0 | } |
468 | | |
469 | 0 | trace!("Sending ClientHello {ch:#?}"); |
470 | 0 |
|
471 | 0 | transcript_buffer.add_message(&ch); |
472 | 0 | cx.common.send_msg(ch, false); |
473 | 0 |
|
474 | 0 | // Calculate the hash of ClientHello and use it to derive EarlyTrafficSecret |
475 | 0 | let early_data_key_schedule = |
476 | 0 | tls13_early_data_key_schedule.map(|(resuming_suite, schedule)| { |
477 | 0 | if !cx.data.early_data.is_enabled() { |
478 | 0 | return schedule; |
479 | 0 | } |
480 | | |
481 | 0 | let (transcript_buffer, random) = match &ech_state { |
482 | | // When using ECH the early data key schedule is derived based on the inner |
483 | | // hello transcript and random. |
484 | 0 | Some(ech_state) => ( |
485 | 0 | &ech_state.inner_hello_transcript, |
486 | 0 | &ech_state.inner_hello_random.0, |
487 | 0 | ), |
488 | 0 | None => (&transcript_buffer, &input.random.0), |
489 | | }; |
490 | | |
491 | 0 | tls13::derive_early_traffic_secret( |
492 | 0 | &*config.key_log, |
493 | 0 | cx, |
494 | 0 | resuming_suite.common.hash_provider, |
495 | 0 | &schedule, |
496 | 0 | &mut input.sent_tls13_fake_ccs, |
497 | 0 | transcript_buffer, |
498 | 0 | random, |
499 | 0 | ); |
500 | 0 | schedule |
501 | 0 | }); Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#10} Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry::{closure#10} |
502 | 0 |
|
503 | 0 | let next = ExpectServerHello { |
504 | 0 | input, |
505 | 0 | transcript_buffer, |
506 | 0 | early_data_key_schedule, |
507 | 0 | offered_key_share: key_share, |
508 | 0 | suite, |
509 | 0 | ech_state, |
510 | 0 | }; |
511 | 0 |
|
512 | 0 | Ok(if supported_versions.tls13 && retryreq.is_none() { |
513 | 0 | Box::new(ExpectServerHelloOrHelloRetryRequest { |
514 | 0 | next, |
515 | 0 | extra_exts: extra_exts.into_owned(), |
516 | 0 | }) |
517 | | } else { |
518 | 0 | Box::new(next) |
519 | | }) |
520 | 0 | } Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry Unexecuted instantiation: rustls::client::hs::emit_client_hello_for_retry |
521 | | |
522 | | /// Prepares `exts` and `cx` with TLS 1.2 or TLS 1.3 session |
523 | | /// resumption. |
524 | | /// |
525 | | /// - `suite` is `None` if this is the initial ClientHello, or |
526 | | /// `Some` if we're retrying in response to |
527 | | /// a HelloRetryRequest. |
528 | | /// |
529 | | /// This function will push onto `exts` to |
530 | | /// |
531 | | /// (a) request a new ticket if we don't have one, |
532 | | /// (b) send our TLS 1.2 ticket after retrieving an 1.2 session, |
533 | | /// (c) send a request for 1.3 early data if allowed and |
534 | | /// (d) send a 1.3 preshared key if we have one. |
535 | | /// |
536 | | /// It returns the TLS 1.3 PSKs, if any, for further processing. |
537 | 0 | fn prepare_resumption<'a>( |
538 | 0 | resuming: &'a Option<persist::Retrieved<ClientSessionValue>>, |
539 | 0 | exts: &mut ClientExtensions<'_>, |
540 | 0 | suite: Option<SupportedCipherSuite>, |
541 | 0 | cx: &mut ClientContext<'_>, |
542 | 0 | config: &ClientConfig, |
543 | 0 | ) -> Option<persist::Retrieved<&'a persist::Tls13ClientSessionValue>> { |
544 | | // Check whether we're resuming with a non-empty ticket. |
545 | 0 | let resuming = match resuming { |
546 | 0 | Some(resuming) if !resuming.ticket().is_empty() => resuming, |
547 | | _ => { |
548 | 0 | if config.supports_version(ProtocolVersion::TLSv1_2) |
549 | 0 | && config.resumption.tls12_resumption == Tls12Resumption::SessionIdOrTickets |
550 | 0 | { |
551 | 0 | // If we don't have a ticket, request one. |
552 | 0 | exts.session_ticket = Some(ClientSessionTicket::Request); |
553 | 0 | } |
554 | 0 | return None; |
555 | | } |
556 | | }; |
557 | | |
558 | 0 | let Some(tls13) = resuming.map(|csv| csv.tls13()) else { Unexecuted instantiation: rustls::client::hs::prepare_resumption::{closure#0} Unexecuted instantiation: rustls::client::hs::prepare_resumption::{closure#0} |
559 | | // TLS 1.2; send the ticket if we have support this protocol version |
560 | 0 | if config.supports_version(ProtocolVersion::TLSv1_2) |
561 | 0 | && config.resumption.tls12_resumption == Tls12Resumption::SessionIdOrTickets |
562 | 0 | { |
563 | 0 | exts.session_ticket = Some(ClientSessionTicket::Offer(Payload::new(resuming.ticket()))); |
564 | 0 | } |
565 | 0 | return None; // TLS 1.2, so nothing to return here |
566 | | }; |
567 | | |
568 | 0 | if !config.supports_version(ProtocolVersion::TLSv1_3) { |
569 | 0 | return None; |
570 | 0 | } |
571 | | |
572 | | // If the server selected TLS 1.2, we can't resume. |
573 | 0 | let suite = match suite { |
574 | 0 | Some(SupportedCipherSuite::Tls13(suite)) => Some(suite), |
575 | | #[cfg(feature = "tls12")] |
576 | | Some(SupportedCipherSuite::Tls12(_)) => return None, |
577 | 0 | None => None, |
578 | | }; |
579 | | |
580 | | // If the selected cipher suite can't select from the session's, we can't resume. |
581 | 0 | if let Some(suite) = suite { |
582 | 0 | suite.can_resume_from(tls13.suite())?; |
583 | 0 | } |
584 | | |
585 | 0 | tls13::prepare_resumption(config, cx, &tls13, exts, suite.is_some()); |
586 | 0 | Some(tls13) |
587 | 0 | } Unexecuted instantiation: rustls::client::hs::prepare_resumption Unexecuted instantiation: rustls::client::hs::prepare_resumption |
588 | | |
589 | 0 | pub(super) fn process_alpn_protocol( |
590 | 0 | common: &mut CommonState, |
591 | 0 | offered_protocols: &[ProtocolName], |
592 | 0 | selected: Option<&ProtocolName>, |
593 | 0 | ) -> Result<(), Error> { |
594 | 0 | common.alpn_protocol = selected.map(ToOwned::to_owned); |
595 | | |
596 | 0 | if let Some(alpn_protocol) = &common.alpn_protocol { |
597 | 0 | if !offered_protocols.contains(alpn_protocol) { |
598 | 0 | return Err(common.send_fatal_alert( |
599 | 0 | AlertDescription::IllegalParameter, |
600 | 0 | PeerMisbehaved::SelectedUnofferedApplicationProtocol, |
601 | 0 | )); |
602 | 0 | } |
603 | 0 | } |
604 | | |
605 | | // RFC 9001 says: "While ALPN only specifies that servers use this alert, QUIC clients MUST |
606 | | // use error 0x0178 to terminate a connection when ALPN negotiation fails." We judge that |
607 | | // the user intended to use ALPN (rather than some out-of-band protocol negotiation |
608 | | // mechanism) if and only if any ALPN protocols were configured. This defends against badly-behaved |
609 | | // servers which accept a connection that requires an application-layer protocol they do not |
610 | | // understand. |
611 | 0 | if common.is_quic() && common.alpn_protocol.is_none() && !offered_protocols.is_empty() { |
612 | 0 | return Err(common.send_fatal_alert( |
613 | 0 | AlertDescription::NoApplicationProtocol, |
614 | 0 | Error::NoApplicationProtocol, |
615 | 0 | )); |
616 | 0 | } |
617 | 0 |
|
618 | 0 | debug!( |
619 | 0 | "ALPN protocol is {:?}", |
620 | 0 | common |
621 | 0 | .alpn_protocol |
622 | 0 | .as_ref() |
623 | 0 | .map(|v| bs_debug::BsDebug(v.as_ref())) |
624 | 0 | ); |
625 | 0 | Ok(()) |
626 | 0 | } Unexecuted instantiation: rustls::client::hs::process_alpn_protocol Unexecuted instantiation: rustls::client::hs::process_alpn_protocol |
627 | | |
628 | 0 | pub(super) fn process_server_cert_type_extension( |
629 | 0 | common: &mut CommonState, |
630 | 0 | config: &ClientConfig, |
631 | 0 | server_cert_extension: Option<&CertificateType>, |
632 | 0 | ) -> Result<Option<(ExtensionType, CertificateType)>, Error> { |
633 | 0 | process_cert_type_extension( |
634 | 0 | common, |
635 | 0 | config |
636 | 0 | .verifier |
637 | 0 | .requires_raw_public_keys(), |
638 | 0 | server_cert_extension.copied(), |
639 | 0 | ExtensionType::ServerCertificateType, |
640 | 0 | ) |
641 | 0 | } Unexecuted instantiation: rustls::client::hs::process_server_cert_type_extension Unexecuted instantiation: rustls::client::hs::process_server_cert_type_extension |
642 | | |
643 | 0 | pub(super) fn process_client_cert_type_extension( |
644 | 0 | common: &mut CommonState, |
645 | 0 | config: &ClientConfig, |
646 | 0 | client_cert_extension: Option<&CertificateType>, |
647 | 0 | ) -> Result<Option<(ExtensionType, CertificateType)>, Error> { |
648 | 0 | process_cert_type_extension( |
649 | 0 | common, |
650 | 0 | config |
651 | 0 | .client_auth_cert_resolver |
652 | 0 | .only_raw_public_keys(), |
653 | 0 | client_cert_extension.copied(), |
654 | 0 | ExtensionType::ClientCertificateType, |
655 | 0 | ) |
656 | 0 | } Unexecuted instantiation: rustls::client::hs::process_client_cert_type_extension Unexecuted instantiation: rustls::client::hs::process_client_cert_type_extension |
657 | | |
658 | | impl State<ClientConnectionData> for ExpectServerHello { |
659 | 0 | fn handle<'m>( |
660 | 0 | mut self: Box<Self>, |
661 | 0 | cx: &mut ClientContext<'_>, |
662 | 0 | m: Message<'m>, |
663 | 0 | ) -> NextStateOrError<'m> |
664 | 0 | where |
665 | 0 | Self: 'm, |
666 | 0 | { |
667 | 0 | let server_hello = |
668 | 0 | require_handshake_msg!(m, HandshakeType::ServerHello, HandshakePayload::ServerHello)?; |
669 | 0 | trace!("We got ServerHello {server_hello:#?}"); |
670 | | |
671 | | use crate::ProtocolVersion::{TLSv1_2, TLSv1_3}; |
672 | 0 | let config = &self.input.config; |
673 | 0 | let tls13_supported = config.supports_version(TLSv1_3); |
674 | | |
675 | 0 | let server_version = if server_hello.legacy_version == TLSv1_2 { |
676 | 0 | server_hello |
677 | 0 | .selected_version |
678 | 0 | .unwrap_or(server_hello.legacy_version) |
679 | | } else { |
680 | 0 | server_hello.legacy_version |
681 | | }; |
682 | | |
683 | 0 | let version = match server_version { |
684 | 0 | TLSv1_3 if tls13_supported => TLSv1_3, |
685 | 0 | TLSv1_2 if config.supports_version(TLSv1_2) => { |
686 | 0 | if cx.data.early_data.is_enabled() && cx.common.early_traffic { |
687 | | // The client must fail with a dedicated error code if the server |
688 | | // responds with TLS 1.2 when offering 0-RTT. |
689 | 0 | return Err(PeerMisbehaved::OfferedEarlyDataWithOldProtocolVersion.into()); |
690 | 0 | } |
691 | 0 |
|
692 | 0 | if server_hello.selected_version.is_some() { |
693 | 0 | return Err({ |
694 | 0 | cx.common.send_fatal_alert( |
695 | 0 | AlertDescription::IllegalParameter, |
696 | 0 | PeerMisbehaved::SelectedTls12UsingTls13VersionExtension, |
697 | 0 | ) |
698 | 0 | }); |
699 | 0 | } |
700 | 0 |
|
701 | 0 | TLSv1_2 |
702 | | } |
703 | | _ => { |
704 | 0 | let reason = match server_version { |
705 | 0 | TLSv1_2 | TLSv1_3 => PeerIncompatible::ServerTlsVersionIsDisabledByOurConfig, |
706 | 0 | _ => PeerIncompatible::ServerDoesNotSupportTls12Or13, |
707 | | }; |
708 | 0 | return Err(cx |
709 | 0 | .common |
710 | 0 | .send_fatal_alert(AlertDescription::ProtocolVersion, reason)); |
711 | | } |
712 | | }; |
713 | | |
714 | 0 | if server_hello.compression_method != Compression::Null { |
715 | 0 | return Err({ |
716 | 0 | cx.common.send_fatal_alert( |
717 | 0 | AlertDescription::IllegalParameter, |
718 | 0 | PeerMisbehaved::SelectedUnofferedCompression, |
719 | 0 | ) |
720 | 0 | }); |
721 | 0 | } |
722 | 0 |
|
723 | 0 | let allowed_unsolicited = [ExtensionType::RenegotiationInfo]; |
724 | 0 | if self |
725 | 0 | .input |
726 | 0 | .hello |
727 | 0 | .server_sent_unsolicited_extensions(server_hello, &allowed_unsolicited) |
728 | | { |
729 | 0 | return Err(cx.common.send_fatal_alert( |
730 | 0 | AlertDescription::UnsupportedExtension, |
731 | 0 | PeerMisbehaved::UnsolicitedServerHelloExtension, |
732 | 0 | )); |
733 | 0 | } |
734 | 0 |
|
735 | 0 | cx.common.negotiated_version = Some(version); |
736 | 0 |
|
737 | 0 | // Extract ALPN protocol |
738 | 0 | if !cx.common.is_tls13() { |
739 | 0 | process_alpn_protocol( |
740 | 0 | cx.common, |
741 | 0 | &self.input.hello.alpn_protocols, |
742 | 0 | server_hello |
743 | 0 | .selected_protocol |
744 | 0 | .as_ref() |
745 | 0 | .map(|s| s.as_ref()), Unexecuted instantiation: <rustls::client::hs::ExpectServerHello as rustls::common_state::State<rustls::client::client_conn::ClientConnectionData>>::handle::{closure#0} Unexecuted instantiation: <rustls::client::hs::ExpectServerHello as rustls::common_state::State<rustls::client::client_conn::ClientConnectionData>>::handle::{closure#0} |
746 | 0 | )?; |
747 | 0 | } |
748 | | |
749 | | // If ECPointFormats extension is supplied by the server, it must contain |
750 | | // Uncompressed. But it's allowed to be omitted. |
751 | 0 | if let Some(point_fmts) = &server_hello.ec_point_formats { |
752 | 0 | if !point_fmts.uncompressed { |
753 | 0 | return Err(cx.common.send_fatal_alert( |
754 | 0 | AlertDescription::HandshakeFailure, |
755 | 0 | PeerMisbehaved::ServerHelloMustOfferUncompressedEcPoints, |
756 | 0 | )); |
757 | 0 | } |
758 | 0 | } |
759 | | |
760 | 0 | let suite = config |
761 | 0 | .find_cipher_suite(server_hello.cipher_suite) |
762 | 0 | .ok_or_else(|| { |
763 | 0 | cx.common.send_fatal_alert( |
764 | 0 | AlertDescription::HandshakeFailure, |
765 | 0 | PeerMisbehaved::SelectedUnofferedCipherSuite, |
766 | 0 | ) |
767 | 0 | })?; Unexecuted instantiation: <rustls::client::hs::ExpectServerHello as rustls::common_state::State<rustls::client::client_conn::ClientConnectionData>>::handle::{closure#1} Unexecuted instantiation: <rustls::client::hs::ExpectServerHello as rustls::common_state::State<rustls::client::client_conn::ClientConnectionData>>::handle::{closure#1} |
768 | | |
769 | 0 | if version != suite.version().version { |
770 | 0 | return Err({ |
771 | 0 | cx.common.send_fatal_alert( |
772 | 0 | AlertDescription::IllegalParameter, |
773 | 0 | PeerMisbehaved::SelectedUnusableCipherSuiteForVersion, |
774 | 0 | ) |
775 | 0 | }); |
776 | 0 | } |
777 | | |
778 | 0 | match self.suite { |
779 | 0 | Some(prev_suite) if prev_suite != suite => { |
780 | 0 | return Err({ |
781 | 0 | cx.common.send_fatal_alert( |
782 | 0 | AlertDescription::IllegalParameter, |
783 | 0 | PeerMisbehaved::SelectedDifferentCipherSuiteAfterRetry, |
784 | 0 | ) |
785 | 0 | }); |
786 | | } |
787 | 0 | _ => { |
788 | 0 | debug!("Using ciphersuite {suite:?}"); |
789 | 0 | self.suite = Some(suite); |
790 | 0 | cx.common.suite = Some(suite); |
791 | 0 | } |
792 | 0 | } |
793 | 0 |
|
794 | 0 | // Start our handshake hash, and input the server-hello. |
795 | 0 | let mut transcript = self |
796 | 0 | .transcript_buffer |
797 | 0 | .start_hash(suite.hash_provider()); |
798 | 0 | transcript.add_message(&m); |
799 | 0 |
|
800 | 0 | let randoms = ConnectionRandoms::new(self.input.random, server_hello.random); |
801 | 0 | // For TLS1.3, start message encryption using |
802 | 0 | // handshake_traffic_secret. |
803 | 0 | match suite { |
804 | 0 | SupportedCipherSuite::Tls13(suite) => { |
805 | 0 | tls13::handle_server_hello( |
806 | 0 | cx, |
807 | 0 | server_hello, |
808 | 0 | randoms, |
809 | 0 | suite, |
810 | 0 | transcript, |
811 | 0 | self.early_data_key_schedule, |
812 | 0 | // We always send a key share when TLS 1.3 is enabled. |
813 | 0 | self.offered_key_share.unwrap(), |
814 | 0 | &m, |
815 | 0 | self.ech_state, |
816 | 0 | self.input, |
817 | 0 | ) |
818 | | } |
819 | | #[cfg(feature = "tls12")] |
820 | | SupportedCipherSuite::Tls12(suite) => tls12::CompleteServerHelloHandling { |
821 | | randoms, |
822 | | transcript, |
823 | | input: self.input, |
824 | | } |
825 | | .handle_server_hello(cx, suite, server_hello, tls13_supported), |
826 | | } |
827 | 0 | } Unexecuted instantiation: <rustls::client::hs::ExpectServerHello as rustls::common_state::State<rustls::client::client_conn::ClientConnectionData>>::handle Unexecuted instantiation: <rustls::client::hs::ExpectServerHello as rustls::common_state::State<rustls::client::client_conn::ClientConnectionData>>::handle |
828 | | |
829 | 0 | fn into_owned(self: Box<Self>) -> NextState<'static> { |
830 | 0 | self |
831 | 0 | } Unexecuted instantiation: <rustls::client::hs::ExpectServerHello as rustls::common_state::State<rustls::client::client_conn::ClientConnectionData>>::into_owned Unexecuted instantiation: <rustls::client::hs::ExpectServerHello as rustls::common_state::State<rustls::client::client_conn::ClientConnectionData>>::into_owned |
832 | | } |
833 | | |
834 | | impl ExpectServerHelloOrHelloRetryRequest { |
835 | 0 | fn into_expect_server_hello(self) -> NextState<'static> { |
836 | 0 | Box::new(self.next) |
837 | 0 | } Unexecuted instantiation: <rustls::client::hs::ExpectServerHelloOrHelloRetryRequest>::into_expect_server_hello Unexecuted instantiation: <rustls::client::hs::ExpectServerHelloOrHelloRetryRequest>::into_expect_server_hello |
838 | | |
839 | 0 | fn handle_hello_retry_request( |
840 | 0 | mut self, |
841 | 0 | cx: &mut ClientContext<'_>, |
842 | 0 | m: Message<'_>, |
843 | 0 | ) -> NextStateOrError<'static> { |
844 | 0 | let hrr = require_handshake_msg!( |
845 | 0 | m, |
846 | 0 | HandshakeType::HelloRetryRequest, |
847 | 0 | HandshakePayload::HelloRetryRequest |
848 | 0 | )?; |
849 | 0 | trace!("Got HRR {hrr:?}"); |
850 | 0 |
|
851 | 0 | cx.common.check_aligned_handshake()?; |
852 | | |
853 | | // We always send a key share when TLS 1.3 is enabled. |
854 | 0 | let offered_key_share = self.next.offered_key_share.unwrap(); |
855 | 0 |
|
856 | 0 | // A retry request is illegal if it contains no cookie and asks for |
857 | 0 | // retry of a group we already sent. |
858 | 0 | let config = &self.next.input.config; |
859 | | |
860 | 0 | if let (None, Some(req_group)) = (&hrr.cookie, hrr.key_share) { |
861 | 0 | let offered_hybrid = offered_key_share |
862 | 0 | .hybrid_component() |
863 | 0 | .and_then(|(group_name, _)| { |
864 | 0 | config.find_kx_group(group_name, ProtocolVersion::TLSv1_3) |
865 | 0 | }) Unexecuted instantiation: <rustls::client::hs::ExpectServerHelloOrHelloRetryRequest>::handle_hello_retry_request::{closure#0} Unexecuted instantiation: <rustls::client::hs::ExpectServerHelloOrHelloRetryRequest>::handle_hello_retry_request::{closure#0} |
866 | 0 | .map(|skxg| skxg.name()); Unexecuted instantiation: <rustls::client::hs::ExpectServerHelloOrHelloRetryRequest>::handle_hello_retry_request::{closure#1} Unexecuted instantiation: <rustls::client::hs::ExpectServerHelloOrHelloRetryRequest>::handle_hello_retry_request::{closure#1} |
867 | 0 |
|
868 | 0 | if req_group == offered_key_share.group() || Some(req_group) == offered_hybrid { |
869 | 0 | return Err({ |
870 | 0 | cx.common.send_fatal_alert( |
871 | 0 | AlertDescription::IllegalParameter, |
872 | 0 | PeerMisbehaved::IllegalHelloRetryRequestWithOfferedGroup, |
873 | 0 | ) |
874 | 0 | }); |
875 | 0 | } |
876 | 0 | } |
877 | | |
878 | | // Or has an empty cookie. |
879 | 0 | if let Some(cookie) = &hrr.cookie { |
880 | 0 | if cookie.0.is_empty() { |
881 | 0 | return Err({ |
882 | 0 | cx.common.send_fatal_alert( |
883 | 0 | AlertDescription::IllegalParameter, |
884 | 0 | PeerMisbehaved::IllegalHelloRetryRequestWithEmptyCookie, |
885 | 0 | ) |
886 | 0 | }); |
887 | 0 | } |
888 | 0 | } |
889 | | |
890 | | // Or asks us to change nothing. |
891 | 0 | if hrr.cookie.is_none() && hrr.key_share.is_none() { |
892 | 0 | return Err({ |
893 | 0 | cx.common.send_fatal_alert( |
894 | 0 | AlertDescription::IllegalParameter, |
895 | 0 | PeerMisbehaved::IllegalHelloRetryRequestWithNoChanges, |
896 | 0 | ) |
897 | 0 | }); |
898 | 0 | } |
899 | 0 |
|
900 | 0 | // Or does not echo the session_id from our ClientHello: |
901 | 0 | // |
902 | 0 | // > the HelloRetryRequest has the same format as a ServerHello message, |
903 | 0 | // > and the legacy_version, legacy_session_id_echo, cipher_suite, and |
904 | 0 | // > legacy_compression_method fields have the same meaning |
905 | 0 | // <https://www.rfc-editor.org/rfc/rfc8446#section-4.1.4> |
906 | 0 | // |
907 | 0 | // and |
908 | 0 | // |
909 | 0 | // > A client which receives a legacy_session_id_echo field that does not |
910 | 0 | // > match what it sent in the ClientHello MUST abort the handshake with an |
911 | 0 | // > "illegal_parameter" alert. |
912 | 0 | // <https://www.rfc-editor.org/rfc/rfc8446#section-4.1.3> |
913 | 0 | if hrr.session_id != self.next.input.session_id { |
914 | 0 | return Err({ |
915 | 0 | cx.common.send_fatal_alert( |
916 | 0 | AlertDescription::IllegalParameter, |
917 | 0 | PeerMisbehaved::IllegalHelloRetryRequestWithWrongSessionId, |
918 | 0 | ) |
919 | 0 | }); |
920 | 0 | } |
921 | 0 |
|
922 | 0 | // Or asks us to talk a protocol we didn't offer, or doesn't support HRR at all. |
923 | 0 | match hrr.supported_versions { |
924 | 0 | Some(ProtocolVersion::TLSv1_3) => { |
925 | 0 | cx.common.negotiated_version = Some(ProtocolVersion::TLSv1_3); |
926 | 0 | } |
927 | | _ => { |
928 | 0 | return Err({ |
929 | 0 | cx.common.send_fatal_alert( |
930 | 0 | AlertDescription::IllegalParameter, |
931 | 0 | PeerMisbehaved::IllegalHelloRetryRequestWithUnsupportedVersion, |
932 | 0 | ) |
933 | 0 | }); |
934 | | } |
935 | | } |
936 | | |
937 | | // Or asks us to use a ciphersuite we didn't offer. |
938 | 0 | let Some(cs) = config.find_cipher_suite(hrr.cipher_suite) else { |
939 | 0 | return Err({ |
940 | 0 | cx.common.send_fatal_alert( |
941 | 0 | AlertDescription::IllegalParameter, |
942 | 0 | PeerMisbehaved::IllegalHelloRetryRequestWithUnofferedCipherSuite, |
943 | 0 | ) |
944 | 0 | }); |
945 | | }; |
946 | | |
947 | | // Or offers ECH related extensions when we didn't offer ECH. |
948 | 0 | if cx.data.ech_status == EchStatus::NotOffered && hrr.encrypted_client_hello.is_some() { |
949 | 0 | return Err({ |
950 | 0 | cx.common.send_fatal_alert( |
951 | 0 | AlertDescription::UnsupportedExtension, |
952 | 0 | PeerMisbehaved::IllegalHelloRetryRequestWithInvalidEch, |
953 | 0 | ) |
954 | 0 | }); |
955 | 0 | } |
956 | 0 |
|
957 | 0 | // HRR selects the ciphersuite. |
958 | 0 | cx.common.suite = Some(cs); |
959 | 0 | cx.common.handshake_kind = Some(HandshakeKind::FullWithHelloRetryRequest); |
960 | 0 |
|
961 | 0 | // If we offered ECH, we need to confirm that the server accepted it. |
962 | 0 | match (self.next.ech_state.as_ref(), cs.tls13()) { |
963 | 0 | (Some(ech_state), Some(tls13_cs)) => { |
964 | 0 | if !ech_state.confirm_hrr_acceptance(hrr, tls13_cs, cx.common)? { |
965 | 0 | // If the server did not confirm, then note the new ECH status but |
966 | 0 | // continue the handshake. We will abort with an ECH required error |
967 | 0 | // at the end. |
968 | 0 | cx.data.ech_status = EchStatus::Rejected; |
969 | 0 | } |
970 | | } |
971 | | (Some(_), None) => { |
972 | 0 | unreachable!("ECH state should only be set when TLS 1.3 was negotiated") |
973 | | } |
974 | 0 | _ => {} |
975 | | }; |
976 | | |
977 | | // This is the draft19 change where the transcript became a tree |
978 | 0 | let transcript = self |
979 | 0 | .next |
980 | 0 | .transcript_buffer |
981 | 0 | .start_hash(cs.hash_provider()); |
982 | 0 | let mut transcript_buffer = transcript.into_hrr_buffer(); |
983 | 0 | transcript_buffer.add_message(&m); |
984 | | |
985 | | // If we offered ECH and the server accepted, we also need to update the separate |
986 | | // ECH transcript with the hello retry request message. |
987 | 0 | if let Some(ech_state) = self.next.ech_state.as_mut() { |
988 | 0 | ech_state.transcript_hrr_update(cs.hash_provider(), &m); |
989 | 0 | } |
990 | | |
991 | | // Early data is not allowed after HelloRetryrequest |
992 | 0 | if cx.data.early_data.is_enabled() { |
993 | 0 | cx.data.early_data.rejected(); |
994 | 0 | } |
995 | | |
996 | 0 | let key_share = match hrr.key_share { |
997 | 0 | Some(group) if group != offered_key_share.group() => { |
998 | 0 | let Some(skxg) = config.find_kx_group(group, ProtocolVersion::TLSv1_3) else { |
999 | 0 | return Err(cx.common.send_fatal_alert( |
1000 | 0 | AlertDescription::IllegalParameter, |
1001 | 0 | PeerMisbehaved::IllegalHelloRetryRequestWithUnofferedNamedGroup, |
1002 | 0 | )); |
1003 | | }; |
1004 | | |
1005 | 0 | cx.common.kx_state = KxState::Start(skxg); |
1006 | 0 | skxg.start()? |
1007 | | } |
1008 | 0 | _ => offered_key_share, |
1009 | | }; |
1010 | | |
1011 | 0 | emit_client_hello_for_retry( |
1012 | 0 | transcript_buffer, |
1013 | 0 | Some(hrr), |
1014 | 0 | Some(key_share), |
1015 | 0 | self.extra_exts, |
1016 | 0 | Some(cs), |
1017 | 0 | self.next.input, |
1018 | 0 | cx, |
1019 | 0 | self.next.ech_state, |
1020 | 0 | ) |
1021 | 0 | } Unexecuted instantiation: <rustls::client::hs::ExpectServerHelloOrHelloRetryRequest>::handle_hello_retry_request Unexecuted instantiation: <rustls::client::hs::ExpectServerHelloOrHelloRetryRequest>::handle_hello_retry_request |
1022 | | } |
1023 | | |
1024 | | impl State<ClientConnectionData> for ExpectServerHelloOrHelloRetryRequest { |
1025 | 0 | fn handle<'m>( |
1026 | 0 | self: Box<Self>, |
1027 | 0 | cx: &mut ClientContext<'_>, |
1028 | 0 | m: Message<'m>, |
1029 | 0 | ) -> NextStateOrError<'m> |
1030 | 0 | where |
1031 | 0 | Self: 'm, |
1032 | 0 | { |
1033 | 0 | match m.payload { |
1034 | | MessagePayload::Handshake { |
1035 | | parsed: HandshakeMessagePayload(HandshakePayload::ServerHello(..)), |
1036 | | .. |
1037 | 0 | } => self |
1038 | 0 | .into_expect_server_hello() |
1039 | 0 | .handle(cx, m), |
1040 | | MessagePayload::Handshake { |
1041 | | parsed: HandshakeMessagePayload(HandshakePayload::HelloRetryRequest(..)), |
1042 | | .. |
1043 | 0 | } => self.handle_hello_retry_request(cx, m), |
1044 | 0 | payload => Err(inappropriate_handshake_message( |
1045 | 0 | &payload, |
1046 | 0 | &[ContentType::Handshake], |
1047 | 0 | &[HandshakeType::ServerHello, HandshakeType::HelloRetryRequest], |
1048 | 0 | )), |
1049 | | } |
1050 | 0 | } Unexecuted instantiation: <rustls::client::hs::ExpectServerHelloOrHelloRetryRequest as rustls::common_state::State<rustls::client::client_conn::ClientConnectionData>>::handle Unexecuted instantiation: <rustls::client::hs::ExpectServerHelloOrHelloRetryRequest as rustls::common_state::State<rustls::client::client_conn::ClientConnectionData>>::handle |
1051 | | |
1052 | 0 | fn into_owned(self: Box<Self>) -> NextState<'static> { |
1053 | 0 | self |
1054 | 0 | } Unexecuted instantiation: <rustls::client::hs::ExpectServerHelloOrHelloRetryRequest as rustls::common_state::State<rustls::client::client_conn::ClientConnectionData>>::into_owned Unexecuted instantiation: <rustls::client::hs::ExpectServerHelloOrHelloRetryRequest as rustls::common_state::State<rustls::client::client_conn::ClientConnectionData>>::into_owned |
1055 | | } |
1056 | | |
1057 | 0 | fn process_cert_type_extension( |
1058 | 0 | common: &mut CommonState, |
1059 | 0 | client_expects: bool, |
1060 | 0 | server_negotiated: Option<CertificateType>, |
1061 | 0 | extension_type: ExtensionType, |
1062 | 0 | ) -> Result<Option<(ExtensionType, CertificateType)>, Error> { |
1063 | 0 | match (client_expects, server_negotiated) { |
1064 | | (true, Some(CertificateType::RawPublicKey)) => { |
1065 | 0 | Ok(Some((extension_type, CertificateType::RawPublicKey))) |
1066 | | } |
1067 | 0 | (true, _) => Err(common.send_fatal_alert( |
1068 | 0 | AlertDescription::HandshakeFailure, |
1069 | 0 | Error::PeerIncompatible(PeerIncompatible::IncorrectCertificateTypeExtension), |
1070 | 0 | )), |
1071 | | (_, Some(CertificateType::RawPublicKey)) => { |
1072 | 0 | unreachable!("Caught by `PeerMisbehaved::UnsolicitedEncryptedExtension`") |
1073 | | } |
1074 | 0 | (_, _) => Ok(None), |
1075 | | } |
1076 | 0 | } Unexecuted instantiation: rustls::client::hs::process_cert_type_extension Unexecuted instantiation: rustls::client::hs::process_cert_type_extension |
1077 | | |
1078 | | pub(super) enum ClientSessionValue { |
1079 | | Tls13(persist::Tls13ClientSessionValue), |
1080 | | #[cfg(feature = "tls12")] |
1081 | | Tls12(persist::Tls12ClientSessionValue), |
1082 | | } |
1083 | | |
1084 | | impl ClientSessionValue { |
1085 | 0 | fn retrieve( |
1086 | 0 | server_name: &ServerName<'static>, |
1087 | 0 | config: &ClientConfig, |
1088 | 0 | cx: &mut ClientContext<'_>, |
1089 | 0 | ) -> Option<persist::Retrieved<Self>> { |
1090 | 0 | let found = config |
1091 | 0 | .resumption |
1092 | 0 | .store |
1093 | 0 | .take_tls13_ticket(server_name) |
1094 | 0 | .map(ClientSessionValue::Tls13) |
1095 | 0 | .or_else(|| { |
1096 | 0 | #[cfg(feature = "tls12")] |
1097 | 0 | { |
1098 | 0 | config |
1099 | 0 | .resumption |
1100 | 0 | .store |
1101 | 0 | .tls12_session(server_name) |
1102 | 0 | .map(ClientSessionValue::Tls12) |
1103 | 0 | } |
1104 | 0 |
|
1105 | 0 | #[cfg(not(feature = "tls12"))] |
1106 | 0 | None |
1107 | 0 | }) Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve::{closure#0} Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve::{closure#0} |
1108 | 0 | .and_then(|resuming| { |
1109 | 0 | resuming.compatible_config(&config.verifier, &config.client_auth_cert_resolver) |
1110 | 0 | }) Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve::{closure#1} Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve::{closure#1} |
1111 | 0 | .and_then(|resuming| { |
1112 | 0 | let now = config |
1113 | 0 | .current_time() |
1114 | 0 | .map_err(|_err| debug!("Could not get current time: {_err}")) Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve::{closure#2}::{closure#0} Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve::{closure#2}::{closure#0} |
1115 | 0 | .ok()?; |
1116 | | |
1117 | 0 | let retrieved = persist::Retrieved::new(resuming, now); |
1118 | 0 | match retrieved.has_expired() { |
1119 | 0 | false => Some(retrieved), |
1120 | 0 | true => None, |
1121 | | } |
1122 | 0 | }) Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve::{closure#2} Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve::{closure#2} |
1123 | 0 | .or_else(|| { |
1124 | 0 | debug!("No cached session for {server_name:?}"); |
1125 | 0 | None |
1126 | 0 | }); Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve::{closure#3} Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve::{closure#3} |
1127 | | |
1128 | 0 | if let Some(resuming) = &found { |
1129 | 0 | if cx.common.is_quic() { |
1130 | 0 | cx.common.quic.params = resuming |
1131 | 0 | .tls13() |
1132 | 0 | .map(|v| v.quic_params()); Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve::{closure#4} Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve::{closure#4} |
1133 | 0 | } |
1134 | 0 | } |
1135 | | |
1136 | 0 | found |
1137 | 0 | } Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::retrieve |
1138 | | |
1139 | 0 | fn common(&self) -> &persist::ClientSessionCommon { |
1140 | 0 | match self { |
1141 | 0 | Self::Tls13(inner) => &inner.common, |
1142 | 0 | #[cfg(feature = "tls12")] |
1143 | 0 | Self::Tls12(inner) => &inner.common, |
1144 | 0 | } |
1145 | 0 | } Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::common Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::common |
1146 | | |
1147 | 0 | fn tls13(&self) -> Option<&persist::Tls13ClientSessionValue> { |
1148 | 0 | match self { |
1149 | 0 | Self::Tls13(v) => Some(v), |
1150 | 0 | #[cfg(feature = "tls12")] |
1151 | 0 | Self::Tls12(_) => None, |
1152 | 0 | } |
1153 | 0 | } Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::tls13 Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::tls13 |
1154 | | |
1155 | 0 | fn compatible_config( |
1156 | 0 | self, |
1157 | 0 | server_cert_verifier: &Arc<dyn ServerCertVerifier>, |
1158 | 0 | client_creds: &Arc<dyn ResolvesClientCert>, |
1159 | 0 | ) -> Option<Self> { |
1160 | 0 | match &self { |
1161 | 0 | Self::Tls13(v) => v |
1162 | 0 | .compatible_config(server_cert_verifier, client_creds) |
1163 | 0 | .then_some(self), |
1164 | 0 | #[cfg(feature = "tls12")] |
1165 | 0 | Self::Tls12(v) => v |
1166 | 0 | .compatible_config(server_cert_verifier, client_creds) |
1167 | 0 | .then_some(self), |
1168 | 0 | } |
1169 | 0 | } Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::compatible_config Unexecuted instantiation: <rustls::client::hs::ClientSessionValue>::compatible_config |
1170 | | } |
1171 | | |
1172 | | impl Deref for ClientSessionValue { |
1173 | | type Target = persist::ClientSessionCommon; |
1174 | | |
1175 | 0 | fn deref(&self) -> &Self::Target { |
1176 | 0 | self.common() |
1177 | 0 | } Unexecuted instantiation: <rustls::client::hs::ClientSessionValue as core::ops::deref::Deref>::deref Unexecuted instantiation: <rustls::client::hs::ClientSessionValue as core::ops::deref::Deref>::deref |
1178 | | } |