1use alloc::vec::Vec;
2use core::ops::Deref;
3use core::{fmt, mem};
4use std::io;
5
6use pki_types::{FipsStatus, ServerName};
7
8use super::config::ClientConfig;
9use super::hs::ClientHelloInput;
10use crate::client::EchStatus;
11use crate::common_state::{CommonState, ConnectionOutputs, EarlyDataEvent, Event, Protocol, Side};
12use crate::conn::private::SideOutput;
13use crate::conn::{
14 Connection, ConnectionCommon, ConnectionCore, IoState, KeyingMaterialExporter, Reader,
15 SideCommonOutput, Writer,
16};
17#[cfg(doc)]
18use crate::crypto;
19use crate::enums::ApplicationProtocol;
20use crate::error::Error;
21use crate::log::trace;
22use crate::msgs::ClientExtensionsInput;
23use crate::quic::QuicOutput;
24use crate::suites::ExtractedSecrets;
25use crate::sync::Arc;
26
27pub struct ClientConnection {
29 inner: ConnectionCommon<ClientSide>,
30}
31
32impl fmt::Debug for ClientConnection {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 f.debug_struct("ClientConnection")
35 .finish_non_exhaustive()
36 }
37}
38
39impl ClientConnection {
40 pub fn early_data(&mut self) -> Option<WriteEarlyData<'_>> {
59 if self
60 .inner
61 .core
62 .side
63 .early_data
64 .is_enabled()
65 {
66 Some(WriteEarlyData::new(self))
67 } else {
68 None
69 }
70 }
71
72 pub fn is_early_data_accepted(&self) -> bool {
78 self.inner.core.is_early_data_accepted()
79 }
80
81 pub fn ech_status(&self) -> EchStatus {
83 self.inner.core.side.ech_status
84 }
85
86 fn write_early_data(&mut self, data: &[u8]) -> io::Result<usize> {
87 self.inner
88 .core
89 .side
90 .early_data
91 .check_write(data.len())
92 .map(|sz| {
93 self.inner
94 .send
95 .send_early_plaintext(&data[..sz])
96 })
97 }
98
99 pub fn tls13_tickets_received(&self) -> u32 {
101 self.inner
102 .core
103 .common
104 .recv
105 .tls13_tickets_received
106 }
107}
108
109impl Connection for ClientConnection {
110 fn read_tls(&mut self, rd: &mut dyn io::Read) -> Result<usize, io::Error> {
111 self.inner.read_tls(rd)
112 }
113
114 fn write_tls(&mut self, wr: &mut dyn io::Write) -> Result<usize, io::Error> {
115 self.inner.write_tls(wr)
116 }
117
118 fn wants_read(&self) -> bool {
119 self.inner.wants_read()
120 }
121
122 fn wants_write(&self) -> bool {
123 self.inner.wants_write()
124 }
125
126 fn reader(&mut self) -> Reader<'_> {
127 self.inner.reader()
128 }
129
130 fn writer(&mut self) -> Writer<'_> {
131 self.inner.writer()
132 }
133
134 fn process_new_packets(&mut self) -> Result<IoState, Error> {
135 self.inner.process_new_packets()
136 }
137
138 fn exporter(&mut self) -> Result<KeyingMaterialExporter, Error> {
139 self.inner.exporter()
140 }
141
142 fn dangerous_extract_secrets(self) -> Result<ExtractedSecrets, Error> {
143 self.inner.dangerous_extract_secrets()
144 }
145
146 fn set_buffer_limit(&mut self, limit: Option<usize>) {
147 self.inner.set_buffer_limit(limit)
148 }
149
150 fn set_plaintext_buffer_limit(&mut self, limit: Option<usize>) {
151 self.inner
152 .set_plaintext_buffer_limit(limit)
153 }
154
155 fn refresh_traffic_keys(&mut self) -> Result<(), Error> {
156 self.inner.refresh_traffic_keys()
157 }
158
159 fn send_close_notify(&mut self) {
160 self.inner.send_close_notify();
161 }
162
163 fn is_handshaking(&self) -> bool {
164 self.inner.is_handshaking()
165 }
166
167 fn fips(&self) -> FipsStatus {
168 self.inner.fips
169 }
170}
171
172impl Deref for ClientConnection {
173 type Target = ConnectionOutputs;
174
175 fn deref(&self) -> &Self::Target {
176 &self.inner
177 }
178}
179
180pub struct ClientConnectionBuilder {
184 pub(crate) config: Arc<ClientConfig>,
185 pub(crate) name: ServerName<'static>,
186 pub(crate) alpn_protocols: Option<Vec<ApplicationProtocol<'static>>>,
187}
188
189impl ClientConnectionBuilder {
190 pub fn with_alpn(mut self, alpn_protocols: Vec<ApplicationProtocol<'static>>) -> Self {
192 self.alpn_protocols = Some(alpn_protocols);
193 self
194 }
195
196 pub fn build(self) -> Result<ClientConnection, Error> {
198 let Self {
199 config,
200 name,
201 alpn_protocols,
202 } = self;
203
204 let alpn_protocols = alpn_protocols.unwrap_or_else(|| config.alpn_protocols.clone());
205 Ok(ClientConnection {
206 inner: ConnectionCommon::new(ConnectionCore::for_client(
207 config,
208 name,
209 ClientExtensionsInput::from_alpn(alpn_protocols),
210 None,
211 Protocol::Tcp,
212 )?),
213 })
214 }
215}
216
217pub struct WriteEarlyData<'a> {
223 sess: &'a mut ClientConnection,
224}
225
226impl<'a> WriteEarlyData<'a> {
227 fn new(sess: &'a mut ClientConnection) -> Self {
228 WriteEarlyData { sess }
229 }
230
231 pub fn bytes_left(&self) -> usize {
234 self.sess
235 .inner
236 .core
237 .side
238 .early_data
239 .bytes_left()
240 }
241
242 pub fn exporter(&mut self) -> Result<KeyingMaterialExporter, Error> {
262 self.sess.inner.core.early_exporter()
263 }
264}
265
266impl io::Write for WriteEarlyData<'_> {
267 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
268 self.sess.write_early_data(buf)
269 }
270
271 fn flush(&mut self) -> io::Result<()> {
272 Ok(())
273 }
274}
275
276impl ConnectionCore<ClientSide> {
277 pub(crate) fn for_client(
278 config: Arc<ClientConfig>,
279 name: ServerName<'static>,
280 extra_exts: ClientExtensionsInput,
281 quic: Option<&mut dyn QuicOutput>,
282 protocol: Protocol,
283 ) -> Result<Self, Error> {
284 let mut common_state = CommonState::new(Side::Client, config.fips());
285 common_state
286 .send
287 .set_max_fragment_size(config.max_fragment_size)?;
288 let mut data = ClientConnectionData::new();
289
290 let mut output = SideCommonOutput {
291 side: &mut data,
292 quic,
293 common: &mut common_state,
294 };
295
296 let input = ClientHelloInput::new(name, &extra_exts, protocol, &mut output, config)?;
297 let state = input.start_handshake(extra_exts, &mut output)?;
298
299 Ok(Self::new(state, data, common_state))
300 }
301
302 pub(crate) fn is_early_data_accepted(&self) -> bool {
303 self.side.early_data.is_accepted()
304 }
305}
306
307pub(super) struct EarlyData {
308 state: EarlyDataState,
309 left: usize,
310}
311
312impl EarlyData {
313 fn new() -> Self {
314 Self {
315 state: EarlyDataState::Disabled,
316 left: 0,
317 }
318 }
319
320 fn is_enabled(&self) -> bool {
321 matches!(
322 self.state,
323 EarlyDataState::Ready | EarlyDataState::Sending | EarlyDataState::Accepted
324 )
325 }
326
327 fn is_accepted(&self) -> bool {
328 matches!(
329 self.state,
330 EarlyDataState::Accepted | EarlyDataState::AcceptedFinished
331 )
332 }
333
334 fn enable(&mut self, max_data: usize) {
335 assert_eq!(self.state, EarlyDataState::Disabled);
336 self.state = EarlyDataState::Ready;
337 self.left = max_data;
338 }
339
340 fn start(&mut self) {
341 assert_eq!(self.state, EarlyDataState::Ready);
342 self.state = EarlyDataState::Sending;
343 }
344
345 fn rejected(&mut self) {
346 trace!("EarlyData rejected");
347 self.state = EarlyDataState::Rejected;
348 }
349
350 fn accepted(&mut self) {
351 trace!("EarlyData accepted");
352 assert_eq!(self.state, EarlyDataState::Sending);
353 self.state = EarlyDataState::Accepted;
354 }
355
356 pub(super) fn finished(&mut self) {
357 trace!("EarlyData finished");
358 self.state = match self.state {
359 EarlyDataState::Accepted => EarlyDataState::AcceptedFinished,
360 _ => panic!("bad EarlyData state"),
361 }
362 }
363
364 fn check_write(&mut self, sz: usize) -> io::Result<usize> {
365 self.check_write_opt(sz)
366 .ok_or_else(|| io::Error::from(io::ErrorKind::InvalidInput))
367 }
368
369 fn check_write_opt(&mut self, sz: usize) -> Option<usize> {
370 match self.state {
371 EarlyDataState::Disabled => unreachable!(),
372 EarlyDataState::Ready | EarlyDataState::Sending | EarlyDataState::Accepted => {
373 let take = if self.left < sz {
374 mem::replace(&mut self.left, 0)
375 } else {
376 self.left -= sz;
377 sz
378 };
379
380 Some(take)
381 }
382 EarlyDataState::Rejected | EarlyDataState::AcceptedFinished => None,
383 }
384 }
385
386 fn bytes_left(&self) -> usize {
387 self.left
388 }
389}
390
391#[derive(Debug, PartialEq)]
392enum EarlyDataState {
393 Disabled,
394 Ready,
395 Sending,
396 Accepted,
397 AcceptedFinished,
398 Rejected,
399}
400
401pub(crate) struct ClientConnectionData {
402 early_data: EarlyData,
403 ech_status: EchStatus,
404}
405
406impl ClientConnectionData {
407 fn new() -> Self {
408 Self {
409 early_data: EarlyData::new(),
410 ech_status: EchStatus::default(),
411 }
412 }
413}
414
415#[expect(clippy::exhaustive_structs)]
417#[derive(Debug)]
418pub struct ClientSide;
419
420impl crate::conn::SideData for ClientSide {}
421
422impl crate::conn::private::Side for ClientSide {
423 type Data = ClientConnectionData;
424 type State = super::hs::ClientState;
425}
426
427impl SideOutput for ClientConnectionData {
428 fn emit(&mut self, ev: Event<'_>) {
429 match ev {
430 Event::EchStatus(ech) => self.ech_status = ech,
431 Event::EarlyData(EarlyDataEvent::Accepted) => self.early_data.accepted(),
432 Event::EarlyData(EarlyDataEvent::Enable(sz)) => self.early_data.enable(sz),
433 Event::EarlyData(EarlyDataEvent::Finished) => self.early_data.finished(),
434 Event::EarlyData(EarlyDataEvent::Start) => self.early_data.start(),
435 Event::EarlyData(EarlyDataEvent::Rejected) => self.early_data.rejected(),
436 _ => unreachable!(),
437 }
438 }
439}