1use alloc::borrow::Cow;
2use alloc::boxed::Box;
3use alloc::vec::Vec;
4use core::fmt;
5use core::fmt::{Debug, Formatter};
6use core::ops::Deref;
7use std::io;
8
9use pki_types::{DnsName, FipsStatus};
10
11use super::config::{ClientHello, ServerConfig};
12use crate::common_state::{CommonState, ConnectionOutputs, EarlyDataEvent, Event, Protocol, Side};
13use crate::conn::private::SideOutput;
14use crate::conn::{
15 Connection, ConnectionCommon, ConnectionCore, KeyingMaterialExporter, Reader, SendPath,
16 SideCommonOutput, Writer,
17};
18#[cfg(doc)]
19use crate::crypto;
20use crate::crypto::cipher::Payload;
21use crate::error::{ApiMisuse, Error, ErrorWithAlert};
22use crate::log::trace;
23use crate::msgs::{ServerExtensionsInput, ServerNamePayload};
24use crate::server::hs::{ChooseConfig, ExpectClientHello, ReadClientHello, ServerState};
25use crate::suites::ExtractedSecrets;
26use crate::sync::Arc;
27use crate::vecbuf::ChunkVecBuffer;
28
29pub struct ServerConnection {
34 pub(super) inner: ConnectionCommon<ServerSide>,
35}
36
37impl ServerConnection {
38 pub fn new(config: Arc<ServerConfig>) -> Result<Self, Error> {
41 let fips = config.fips();
42 Ok(Self {
43 inner: ConnectionCommon::new(
44 ConnectionCore::for_server(
45 config,
46 ServerExtensionsInput::default(),
47 Protocol::Tcp,
48 )?,
49 fips,
50 ),
51 })
52 }
53
54 pub fn server_name(&self) -> Option<&DnsName<'_>> {
70 self.inner.core.side.server_name()
71 }
72
73 pub fn received_resumption_data(&self) -> Option<&[u8]> {
79 self.inner
80 .core
81 .side
82 .received_resumption_data()
83 }
84
85 pub fn set_resumption_data(&mut self, data: &[u8]) -> Result<(), Error> {
94 assert!(data.len() < 2usize.pow(15));
95 match &mut self.inner.core.state {
96 Ok(st) => st.set_resumption_data(data),
97 Err(e) => Err(e.clone()),
98 }
99 }
100
101 pub fn early_data(&mut self) -> Option<ReadEarlyData<'_>> {
112 if self
113 .inner
114 .core
115 .side
116 .early_data
117 .was_accepted()
118 {
119 Some(ReadEarlyData::new(&mut self.inner))
120 } else {
121 None
122 }
123 }
124
125 pub fn dangerous_extract_secrets(self) -> Result<ExtractedSecrets, Error> {
128 self.inner.dangerous_extract_secrets()
129 }
130}
131
132impl Connection for ServerConnection {
133 fn read_tls(&mut self, rd: &mut dyn io::Read) -> Result<usize, io::Error> {
134 self.inner.read_tls(rd)
135 }
136
137 fn write_tls(&mut self, wr: &mut dyn io::Write) -> Result<usize, io::Error> {
138 self.inner.write_tls(wr)
139 }
140
141 fn wants_read(&self) -> bool {
142 self.inner.wants_read()
143 }
144
145 fn wants_write(&self) -> bool {
146 self.inner.wants_write()
147 }
148
149 fn reader(&mut self) -> Reader<'_> {
150 self.inner.reader()
151 }
152
153 fn writer(&mut self) -> Writer<'_> {
154 self.inner.writer()
155 }
156
157 fn process_new_packets(&mut self) -> Result<crate::IoState, Error> {
158 self.inner.process_new_packets()
159 }
160
161 fn exporter(&mut self) -> Result<KeyingMaterialExporter, Error> {
162 self.inner.exporter()
163 }
164
165 fn dangerous_extract_secrets(self) -> Result<ExtractedSecrets, Error> {
166 self.inner.dangerous_extract_secrets()
167 }
168
169 fn set_buffer_limit(&mut self, limit: Option<usize>) {
170 self.inner.set_buffer_limit(limit)
171 }
172
173 fn set_plaintext_buffer_limit(&mut self, limit: Option<usize>) {
174 self.inner
175 .set_plaintext_buffer_limit(limit)
176 }
177
178 fn refresh_traffic_keys(&mut self) -> Result<(), Error> {
179 self.inner.refresh_traffic_keys()
180 }
181
182 fn send_close_notify(&mut self) {
183 self.inner.send_close_notify();
184 }
185
186 fn is_handshaking(&self) -> bool {
187 self.inner.is_handshaking()
188 }
189
190 fn fips(&self) -> FipsStatus {
191 self.inner.fips
192 }
193}
194
195impl Deref for ServerConnection {
196 type Target = ConnectionOutputs;
197
198 fn deref(&self) -> &Self::Target {
199 &self.inner
200 }
201}
202
203impl Debug for ServerConnection {
204 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
205 f.debug_struct("ServerConnection")
206 .finish_non_exhaustive()
207 }
208}
209
210pub struct Acceptor {
257 inner: Option<ConnectionCommon<ServerSide>>,
258}
259
260impl Default for Acceptor {
261 fn default() -> Self {
263 Self {
264 inner: Some(ConnectionCommon::new(
265 ConnectionCore::for_acceptor(Protocol::Tcp),
266 FipsStatus::Unvalidated,
267 )),
268 }
269 }
270}
271
272impl Acceptor {
273 pub fn read_tls(&mut self, rd: &mut dyn io::Read) -> Result<usize, io::Error> {
280 match &mut self.inner {
281 Some(conn) => conn.read_tls(rd),
282 None => Err(io::Error::other(
283 "acceptor cannot read after successful acceptance",
284 )),
285 }
286 }
287
288 pub fn accept(&mut self) -> Result<Option<Accepted>, (Error, AcceptedAlert)> {
300 let Some(mut connection) = self.inner.take() else {
301 return Err((
302 ApiMisuse::AcceptorPolledAfterCompletion.into(),
303 AcceptedAlert::empty(),
304 ));
305 };
306
307 if let Err(e) = connection.process_new_packets() {
308 return Err(AcceptedAlert::from_error(e, connection.core.common.send));
309 }
310
311 let Ok(ServerState::ChooseConfig(_)) = connection.core.state else {
312 self.inner = Some(connection);
313 return Ok(None);
314 };
315
316 let Ok(ServerState::ChooseConfig(choose_config)) = core::mem::replace(
317 &mut connection.core.state,
318 Err(Error::Unreachable("Accepted misused state")),
319 ) else {
320 unreachable!(); };
322
323 Ok(Some(Accepted {
324 connection,
325 choose_config,
326 }))
327 }
328}
329
330pub struct AcceptedAlert(ChunkVecBuffer);
335
336impl AcceptedAlert {
337 pub(super) fn from_error(error: Error, mut send: SendPath) -> (Error, Self) {
338 let ErrorWithAlert { error, data } = ErrorWithAlert::new(error, &mut send);
339 let mut output = ChunkVecBuffer::new(None);
340 output.append(data);
341 (error, Self(output))
342 }
343
344 pub(super) fn empty() -> Self {
345 Self(ChunkVecBuffer::new(None))
346 }
347
348 pub fn write(&mut self, wr: &mut dyn io::Write) -> Result<usize, io::Error> {
353 self.0.write_to(wr)
354 }
355
356 pub fn write_all(&mut self, wr: &mut dyn io::Write) -> Result<(), io::Error> {
360 while self.write(wr)? != 0 {}
361 Ok(())
362 }
363}
364
365impl Debug for AcceptedAlert {
366 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
367 f.debug_struct("AcceptedAlert")
368 .finish_non_exhaustive()
369 }
370}
371
372pub struct ReadEarlyData<'a> {
378 common: &'a mut ConnectionCommon<ServerSide>,
379}
380
381impl<'a> ReadEarlyData<'a> {
382 fn new(common: &'a mut ConnectionCommon<ServerSide>) -> Self {
383 ReadEarlyData { common }
384 }
385
386 pub fn exporter(&mut self) -> Result<KeyingMaterialExporter, Error> {
406 self.common.core.early_exporter()
407 }
408}
409
410impl io::Read for ReadEarlyData<'_> {
411 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
412 self.common
413 .core
414 .side
415 .early_data
416 .read(buf)
417 }
418}
419
420pub struct Accepted {
424 connection: ConnectionCommon<ServerSide>,
426 choose_config: Box<ChooseConfig>,
427}
428
429impl Accepted {
430 pub fn client_hello(&self) -> ClientHello<'_> {
432 let client_hello = self.choose_config.client_hello();
433 let server_name = client_hello
434 .server_name
435 .as_ref()
436 .and_then(ServerNamePayload::to_dns_name_normalized)
437 .map(Cow::Owned);
438 let ch = ClientHello {
439 server_name,
440 signature_schemes: client_hello
441 .signature_schemes
442 .as_deref()
443 .unwrap_or_default(),
444 alpn: client_hello.protocols.as_ref(),
445 server_cert_types: client_hello
446 .server_certificate_types
447 .as_deref(),
448 client_cert_types: client_hello
449 .client_certificate_types
450 .as_deref(),
451 cipher_suites: &client_hello.cipher_suites,
452 certificate_authorities: client_hello
453 .certificate_authority_names
454 .as_deref(),
455 named_groups: client_hello.named_groups.as_deref(),
456 };
457
458 trace!("Accepted::client_hello(): {ch:#?}");
459 ch
460 }
461
462 pub fn into_connection(
468 mut self,
469 config: Arc<ServerConfig>,
470 ) -> Result<ServerConnection, (Error, AcceptedAlert)> {
471 if let Err(err) = self
472 .connection
473 .send
474 .set_max_fragment_size(config.max_fragment_size)
475 {
476 return Err((err, AcceptedAlert::empty()));
479 }
480 self.connection.fips = config.fips();
481
482 let mut output = SideCommonOutput {
483 side: &mut self.connection.core.side,
484 quic: None,
485 common: &mut self.connection.core.common,
486 };
487
488 let state = match self.choose_config.use_config(
489 config,
490 ServerExtensionsInput::default(),
491 &mut output,
492 ) {
493 Ok(state) => state,
494 Err(err) => {
495 return Err(AcceptedAlert::from_error(
496 err,
497 self.connection.core.common.send,
498 ));
499 }
500 };
501 self.connection.core.state = Ok(state);
502
503 Ok(ServerConnection {
504 inner: self.connection,
505 })
506 }
507}
508
509impl Debug for Accepted {
510 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
511 f.debug_struct("Accepted")
512 .finish_non_exhaustive()
513 }
514}
515
516#[derive(Default)]
517pub(super) enum EarlyDataState {
518 #[default]
519 New,
520 Accepted {
521 received: ChunkVecBuffer,
522 },
523}
524
525impl EarlyDataState {
526 fn accept(&mut self) {
527 *self = Self::Accepted {
528 received: ChunkVecBuffer::new(None),
529 };
530 }
531
532 fn was_accepted(&self) -> bool {
533 matches!(self, Self::Accepted { .. })
534 }
535
536 #[expect(dead_code)]
537 fn peek(&self) -> Option<&[u8]> {
538 match self {
539 Self::Accepted { received, .. } => received.peek(),
540 _ => None,
541 }
542 }
543
544 #[expect(dead_code)]
545 fn pop(&mut self) -> Option<Vec<u8>> {
546 match self {
547 Self::Accepted { received, .. } => received.pop(),
548 _ => None,
549 }
550 }
551
552 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
553 match self {
554 Self::Accepted { received, .. } => received.read(buf),
555 _ => Err(io::Error::from(io::ErrorKind::BrokenPipe)),
556 }
557 }
558
559 fn take_received_plaintext(&mut self, bytes: Payload<'_>) {
560 let Self::Accepted { received } = self else {
561 return;
562 };
563
564 received.append(bytes.into_vec());
565 }
566}
567
568impl ConnectionCore<ServerSide> {
569 pub(crate) fn for_server(
570 config: Arc<ServerConfig>,
571 extra_exts: ServerExtensionsInput,
572 protocol: Protocol,
573 ) -> Result<Self, Error> {
574 let mut common = CommonState::new(Side::Server);
575 common
576 .send
577 .set_max_fragment_size(config.max_fragment_size)?;
578 Ok(Self::new(
579 Box::new(ExpectClientHello::new(
580 config,
581 extra_exts,
582 Vec::new(),
583 protocol,
584 ))
585 .into(),
586 ServerConnectionData::default(),
587 common,
588 ))
589 }
590
591 pub(crate) fn for_acceptor(protocol: Protocol) -> Self {
592 Self::new(
593 ReadClientHello::new(protocol).into(),
594 ServerConnectionData::default(),
595 CommonState::new(Side::Server),
596 )
597 }
598}
599
600#[derive(Default)]
602pub(crate) struct ServerConnectionData {
603 sni: Option<DnsName<'static>>,
604 received_resumption_data: Option<Vec<u8>>,
605 early_data: EarlyDataState,
606}
607
608impl ServerConnectionData {
609 pub(crate) fn received_resumption_data(&self) -> Option<&[u8]> {
610 self.received_resumption_data.as_deref()
611 }
612
613 pub(crate) fn server_name(&self) -> Option<&DnsName<'static>> {
614 self.sni.as_ref()
615 }
616}
617
618impl SideOutput for ServerConnectionData {
619 fn emit(&mut self, ev: Event<'_>) {
620 match ev {
621 Event::EarlyApplicationData(data) => self
622 .early_data
623 .take_received_plaintext(data),
624 Event::EarlyData(EarlyDataEvent::Accepted) => self.early_data.accept(),
625 Event::ReceivedServerName(sni) => self.sni = sni,
626 Event::ResumptionData(data) => self.received_resumption_data = Some(data),
627 _ => unreachable!(),
628 }
629 }
630}
631
632#[expect(clippy::exhaustive_structs)]
634#[derive(Debug)]
635pub struct ServerSide;
636
637impl crate::conn::SideData for ServerSide {}
638
639impl crate::conn::private::Side for ServerSide {
640 type Data = ServerConnectionData;
641 type State = ServerState;
642}
643
644#[cfg(test)]
645mod tests {
646 use std::format;
647
648 use super::*;
649
650 #[test]
652 fn test_read_in_new_state() {
653 assert_eq!(
654 format!("{:?}", EarlyDataState::default().read(&mut [0u8; 5])),
655 "Err(Kind(BrokenPipe))"
656 );
657 }
658}