1use alloc::borrow::Cow;
2use alloc::boxed::Box;
3use alloc::vec::Vec;
4use core::fmt;
5use core::fmt::{Debug, Formatter};
6use core::ops::{Deref, DerefMut};
7use std::io;
8
9use pki_types::{DnsName, FipsStatus};
10
11use super::config::{ClientHello, ServerConfig};
12use crate::common_state::{
13 CommonState, ConnectionOutputs, EarlyDataEvent, Event, Output, Protocol, SendPath, Side,
14};
15use crate::conn::{
16 Connection, ConnectionCommon, ConnectionCore, KeyingMaterialExporter, Reader, 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 DerefMut for ServerConnection {
204 fn deref_mut(&mut self) -> &mut Self::Target {
205 &mut self.inner
206 }
207}
208
209impl Debug for ServerConnection {
210 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
211 f.debug_struct("ServerConnection")
212 .finish_non_exhaustive()
213 }
214}
215
216pub struct Acceptor {
263 inner: Option<ConnectionCommon<ServerSide>>,
264}
265
266impl Default for Acceptor {
267 fn default() -> Self {
269 Self {
270 inner: Some(ConnectionCommon::new(
271 ConnectionCore::for_acceptor(Protocol::Tcp),
272 FipsStatus::Unvalidated,
273 )),
274 }
275 }
276}
277
278impl Acceptor {
279 pub fn read_tls(&mut self, rd: &mut dyn io::Read) -> Result<usize, io::Error> {
286 match &mut self.inner {
287 Some(conn) => conn.read_tls(rd),
288 None => Err(io::Error::other(
289 "acceptor cannot read after successful acceptance",
290 )),
291 }
292 }
293
294 pub fn accept(&mut self) -> Result<Option<Accepted>, (Error, AcceptedAlert)> {
306 let Some(mut connection) = self.inner.take() else {
307 return Err((
308 ApiMisuse::AcceptorPolledAfterCompletion.into(),
309 AcceptedAlert::empty(),
310 ));
311 };
312
313 if let Err(e) = connection.process_new_packets() {
314 return Err(AcceptedAlert::from_error(e, connection.core.common.send));
315 }
316
317 let Ok(ServerState::ChooseConfig(_)) = connection.core.state else {
318 self.inner = Some(connection);
319 return Ok(None);
320 };
321
322 let Ok(ServerState::ChooseConfig(choose_config)) = core::mem::replace(
323 &mut connection.core.state,
324 Err(Error::Unreachable("Accepted misused state")),
325 ) else {
326 unreachable!(); };
328
329 Ok(Some(Accepted {
330 connection,
331 choose_config,
332 }))
333 }
334}
335
336pub struct AcceptedAlert(ChunkVecBuffer);
341
342impl AcceptedAlert {
343 pub(super) fn from_error(error: Error, mut send: SendPath) -> (Error, Self) {
344 let ErrorWithAlert { error, data } = ErrorWithAlert::new(error, &mut send);
345 let mut output = ChunkVecBuffer::new(None);
346 output.append(data);
347 (error, Self(output))
348 }
349
350 pub(super) fn empty() -> Self {
351 Self(ChunkVecBuffer::new(None))
352 }
353
354 pub fn write(&mut self, wr: &mut dyn io::Write) -> Result<usize, io::Error> {
359 self.0.write_to(wr)
360 }
361
362 pub fn write_all(&mut self, wr: &mut dyn io::Write) -> Result<(), io::Error> {
366 while self.write(wr)? != 0 {}
367 Ok(())
368 }
369}
370
371impl Debug for AcceptedAlert {
372 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
373 f.debug_struct("AcceptedAlert")
374 .finish_non_exhaustive()
375 }
376}
377
378pub struct ReadEarlyData<'a> {
384 common: &'a mut ConnectionCommon<ServerSide>,
385}
386
387impl<'a> ReadEarlyData<'a> {
388 fn new(common: &'a mut ConnectionCommon<ServerSide>) -> Self {
389 ReadEarlyData { common }
390 }
391
392 pub fn exporter(&mut self) -> Result<KeyingMaterialExporter, Error> {
412 self.common.core.early_exporter()
413 }
414}
415
416impl io::Read for ReadEarlyData<'_> {
417 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
418 self.common
419 .core
420 .side
421 .early_data
422 .read(buf)
423 }
424}
425
426pub struct Accepted {
430 connection: ConnectionCommon<ServerSide>,
432 choose_config: ChooseConfig,
433}
434
435impl Accepted {
436 pub fn client_hello(&self) -> ClientHello<'_> {
438 let client_hello = self.choose_config.client_hello();
439 let server_name = client_hello
440 .server_name
441 .as_ref()
442 .and_then(ServerNamePayload::to_dns_name_normalized)
443 .map(Cow::Owned);
444 let ch = ClientHello {
445 server_name,
446 signature_schemes: client_hello
447 .signature_schemes
448 .as_deref()
449 .unwrap_or_default(),
450 alpn: client_hello.protocols.as_ref(),
451 server_cert_types: client_hello
452 .server_certificate_types
453 .as_deref(),
454 client_cert_types: client_hello
455 .client_certificate_types
456 .as_deref(),
457 cipher_suites: &client_hello.cipher_suites,
458 certificate_authorities: client_hello
459 .certificate_authority_names
460 .as_deref(),
461 named_groups: client_hello.named_groups.as_deref(),
462 };
463
464 trace!("Accepted::client_hello(): {ch:#?}");
465 ch
466 }
467
468 pub fn into_connection(
474 mut self,
475 config: Arc<ServerConfig>,
476 ) -> Result<ServerConnection, (Error, AcceptedAlert)> {
477 if let Err(err) = self
478 .connection
479 .send
480 .set_max_fragment_size(config.max_fragment_size)
481 {
482 return Err((err, AcceptedAlert::empty()));
485 }
486 self.connection.fips = config.fips();
487
488 let state = match self.choose_config.use_config(
489 config,
490 ServerExtensionsInput::default(),
491 &mut self.connection.core.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 Debug for EarlyDataState {
569 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
570 match self {
571 Self::New => write!(f, "EarlyDataState::New"),
572 Self::Accepted { received } => write!(
573 f,
574 "EarlyDataState::Accepted {{ received: {} }}",
575 received.len(),
576 ),
577 }
578 }
579}
580
581impl ConnectionCore<ServerSide> {
582 pub(crate) fn for_server(
583 config: Arc<ServerConfig>,
584 extra_exts: ServerExtensionsInput,
585 protocol: Protocol,
586 ) -> Result<Self, Error> {
587 let mut common = CommonState::new(Side::Server, protocol);
588 common
589 .send
590 .set_max_fragment_size(config.max_fragment_size)?;
591 Ok(Self::new(
592 Box::new(ExpectClientHello::new(
593 config,
594 extra_exts,
595 Vec::new(),
596 protocol,
597 ))
598 .into(),
599 ServerConnectionData::default(),
600 common,
601 ))
602 }
603
604 pub(crate) fn for_acceptor(protocol: Protocol) -> Self {
605 Self::new(
606 ReadClientHello::new(protocol).into(),
607 ServerConnectionData::default(),
608 CommonState::new(Side::Server, protocol),
609 )
610 }
611}
612
613#[derive(Debug, Default)]
615pub(crate) struct ServerConnectionData {
616 sni: Option<DnsName<'static>>,
617 received_resumption_data: Option<Vec<u8>>,
618 early_data: EarlyDataState,
619}
620
621impl ServerConnectionData {
622 pub(crate) fn received_resumption_data(&self) -> Option<&[u8]> {
623 self.received_resumption_data.as_deref()
624 }
625
626 pub(crate) fn server_name(&self) -> Option<&DnsName<'static>> {
627 self.sni.as_ref()
628 }
629}
630
631impl Output for ServerConnectionData {
632 fn emit(&mut self, ev: Event<'_>) {
633 match ev {
634 Event::EarlyApplicationData(data) => self
635 .early_data
636 .take_received_plaintext(data),
637 Event::EarlyData(EarlyDataEvent::Accepted) => self.early_data.accept(),
638 Event::ReceivedServerName(sni) => self.sni = sni,
639 Event::ResumptionData(data) => self.received_resumption_data = Some(data),
640 _ => unreachable!(),
641 }
642 }
643}
644
645#[expect(clippy::exhaustive_structs)]
647#[derive(Debug)]
648pub struct ServerSide;
649
650impl crate::conn::SideData for ServerSide {}
651
652impl crate::conn::private::Side for ServerSide {
653 type Data = ServerConnectionData;
654 type State = ServerState;
655}
656
657#[cfg(test)]
658mod tests {
659 use std::format;
660
661 use super::*;
662
663 #[test]
665 fn test_read_in_new_state() {
666 assert_eq!(
667 format!("{:?}", EarlyDataState::default().read(&mut [0u8; 5])),
668 "Err(Kind(BrokenPipe))"
669 );
670 }
671}