1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
#[cfg(feature = "std")]
use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::fmt;
use core::fmt::{Debug, Formatter};
use core::ops::{Deref, DerefMut};
#[cfg(feature = "std")]
use std::io;
use pki_types::DnsName;
#[cfg(feature = "std")]
use super::config::ClientHello;
use super::config::ServerConfig;
use super::hs;
#[cfg(feature = "std")]
use super::hs::ClientHelloInput;
use crate::common_state::{CommonState, EarlyDataEvent, Event, Output, Protocol, Side};
#[cfg(feature = "std")]
use crate::common_state::{Input, State};
#[cfg(feature = "std")]
use crate::conn::ConnectionCommon;
use crate::conn::{ConnectionCore, UnbufferedConnectionCommon};
#[cfg(doc)]
use crate::crypto;
#[cfg(feature = "std")]
use crate::crypto::SignatureScheme;
use crate::crypto::cipher::Payload;
use crate::error::Error;
use crate::kernel::KernelConnection;
#[cfg(feature = "std")]
use crate::log::trace;
use crate::msgs::ServerExtensionsInput;
#[cfg(feature = "std")]
use crate::msgs::{
ClientHelloPayload, HandshakePayload, Locator, Message, MessagePayload, ServerNamePayload,
};
use crate::suites::ExtractedSecrets;
use crate::sync::Arc;
use crate::vecbuf::ChunkVecBuffer;
#[cfg(feature = "std")]
mod buffered {
use alloc::boxed::Box;
use core::fmt;
use core::fmt::{Debug, Formatter};
use core::ops::{Deref, DerefMut};
use std::io;
use pki_types::{DnsName, FipsStatus};
use super::{
Accepted, Accepting, Protocol, ServerConfig, ServerConnectionData, ServerExtensionsInput,
};
use crate::KeyingMaterialExporter;
use crate::common_state::{CommonState, Side};
use crate::conn::private::SideData;
use crate::conn::{ConnectionCommon, ConnectionCore};
use crate::error::{ApiMisuse, Error};
use crate::server::hs::ClientHelloInput;
use crate::suites::ExtractedSecrets;
use crate::sync::Arc;
use crate::vecbuf::ChunkVecBuffer;
/// This represents a single TLS server connection.
///
/// Send TLS-protected data to the peer using the `io::Write` trait implementation.
/// Read data from the peer using the `io::Read` trait implementation.
pub struct ServerConnection {
pub(super) inner: ConnectionCommon<ServerConnectionData>,
}
impl ServerConnection {
/// Make a new ServerConnection. `config` controls how
/// we behave in the TLS protocol.
pub fn new(config: Arc<ServerConfig>) -> Result<Self, Error> {
Ok(Self {
inner: ConnectionCommon::from(ConnectionCore::for_server(
config,
ServerExtensionsInput::default(),
Protocol::Tcp,
)?),
})
}
/// Retrieves the server name, if any, used to select the certificate and
/// private key.
///
/// This returns `None` until some time after the client's server name indication
/// (SNI) extension value is processed during the handshake. It will never be
/// `None` when the connection is ready to send or process application data,
/// unless the client does not support SNI.
///
/// This is useful for application protocols that need to enforce that the
/// server name matches an application layer protocol hostname. For
/// example, HTTP/1.1 servers commonly expect the `Host:` header field of
/// every request on a connection to match the hostname in the SNI extension
/// when the client provides the SNI extension.
///
/// The server name is also used to match sessions during session resumption.
pub fn server_name(&self) -> Option<&DnsName<'_>> {
self.inner.core.side.server_name()
}
/// Application-controlled portion of the resumption ticket supplied by the client, if any.
///
/// Recovered from the prior session's `set_resumption_data`. Integrity is guaranteed by rustls.
///
/// Returns `Some` if and only if a valid resumption ticket has been received from the client.
pub fn received_resumption_data(&self) -> Option<&[u8]> {
self.inner
.core
.side
.received_resumption_data()
}
/// Set the resumption data to embed in future resumption tickets supplied to the client.
///
/// Defaults to the empty byte string. Must be less than 2^15 bytes to allow room for other
/// data. Should be called while `is_handshaking` returns true to ensure all transmitted
/// resumption tickets are affected.
///
/// Integrity will be assured by rustls, but the data will be visible to the client. If secrecy
/// from the client is desired, encrypt the data separately.
pub fn set_resumption_data(&mut self, data: &[u8]) -> Result<(), Error> {
assert!(data.len() < 2usize.pow(15));
match &mut self.core.state {
Ok(st) => st.set_resumption_data(data),
Err(e) => Err(e.clone()),
}
}
/// Returns an `io::Read` implementer you can read bytes from that are
/// received from a client as TLS1.3 0RTT/"early" data, during the handshake.
///
/// This returns `None` in many circumstances, such as :
///
/// - Early data is disabled if [`ServerConfig::max_early_data_size`] is zero (the default).
/// - The session negotiated with the client is not TLS1.3.
/// - The client just doesn't support early data.
/// - The connection doesn't resume an existing session.
/// - The client hasn't sent a full ClientHello yet.
pub fn early_data(&mut self) -> Option<ReadEarlyData<'_>> {
if self
.inner
.core
.side
.early_data
.was_accepted()
{
Some(ReadEarlyData::new(&mut self.inner))
} else {
None
}
}
/// Return the FIPS validation status of the connection's `ServerConfig`.
///
/// This is different from [`crate::crypto::CryptoProvider::fips()`]:
/// it is concerned only with cryptography, whereas this _also_ covers TLS-level
/// configuration that NIST recommends, as well as ECH HPKE suites if applicable.
pub fn fips(&self) -> FipsStatus {
self.inner.core.side.fips
}
/// Extract secrets, so they can be used when configuring kTLS, for example.
/// Should be used with care as it exposes secret key material.
pub fn dangerous_extract_secrets(self) -> Result<ExtractedSecrets, Error> {
self.inner.dangerous_extract_secrets()
}
}
impl Debug for ServerConnection {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("ServerConnection")
.finish()
}
}
impl Deref for ServerConnection {
type Target = ConnectionCommon<ServerConnectionData>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl DerefMut for ServerConnection {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl From<ServerConnection> for crate::Connection {
fn from(conn: ServerConnection) -> Self {
Self::Server(conn)
}
}
/// Handle a server-side connection before configuration is available.
///
/// `Acceptor` allows the caller to choose a [`ServerConfig`] after reading
/// the [`ClientHello`][super::ClientHello] of an incoming connection. This is useful for
/// servers that choose different certificates or cipher suites based on the
/// characteristics of the `ClientHello`. In particular it is useful for
/// servers that need to do some I/O to load a certificate and its private key
/// and don't want to use the blocking interface provided by
/// [`ServerCredentialResolver`][crate::server::ServerCredentialResolver].
///
/// Create an Acceptor with [`Acceptor::default()`].
///
/// # Example
///
/// ```no_run
/// # #[cfg(feature = "aws-lc-rs")] {
/// # fn choose_server_config(
/// # _: rustls::server::ClientHello,
/// # ) -> std::sync::Arc<rustls::ServerConfig> {
/// # unimplemented!();
/// # }
/// # #[allow(unused_variables)]
/// # fn main() {
/// use rustls::server::{Acceptor, ServerConfig};
/// let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
/// for stream in listener.incoming() {
/// let mut stream = stream.unwrap();
/// let mut acceptor = Acceptor::default();
/// let accepted = loop {
/// acceptor.read_tls(&mut stream).unwrap();
/// if let Some(accepted) = acceptor.accept().unwrap() {
/// break accepted;
/// }
/// };
///
/// // For some user-defined choose_server_config:
/// let config = choose_server_config(accepted.client_hello());
/// let conn = accepted
/// .into_connection(config)
/// .unwrap();
///
/// // Proceed with handling the ServerConnection.
/// }
/// # }
/// # }
/// ```
pub struct Acceptor {
inner: Option<ConnectionCommon<ServerConnectionData>>,
}
impl Default for Acceptor {
/// Return an empty Acceptor, ready to receive bytes from a new client connection.
fn default() -> Self {
Self {
inner: Some(
ConnectionCore::new(
Box::new(Accepting),
ServerConnectionData::new(CommonState::new(Side::Server, Protocol::Tcp)),
)
.into(),
),
}
}
}
impl Acceptor {
/// Read TLS content from `rd`.
///
/// Returns an error if this `Acceptor` has already yielded an [`Accepted`]. For more details,
/// refer to [`Connection::read_tls()`].
///
/// [`Connection::read_tls()`]: crate::Connection::read_tls
pub fn read_tls(&mut self, rd: &mut dyn io::Read) -> Result<usize, io::Error> {
match &mut self.inner {
Some(conn) => conn.read_tls(rd),
None => Err(io::Error::other(
"acceptor cannot read after successful acceptance",
)),
}
}
/// Check if a `ClientHello` message has been received.
///
/// Returns `Ok(None)` if the complete `ClientHello` has not yet been received.
/// Do more I/O and then call this function again.
///
/// Returns `Ok(Some(accepted))` if the connection has been accepted. Call
/// `accepted.into_connection()` to continue. Do not call this function again.
///
/// Returns `Err((err, alert))` if an error occurred. If an alert is returned, the
/// application should call `alert.write()` to send the alert to the client. It should
/// not call `accept()` again.
pub fn accept(&mut self) -> Result<Option<Accepted>, (Error, AcceptedAlert)> {
let Some(mut connection) = self.inner.take() else {
return Err((
ApiMisuse::AcceptorPolledAfterCompletion.into(),
AcceptedAlert::empty(),
));
};
let input = match connection.first_handshake_message() {
Ok(Some(msg)) => msg,
Ok(None) => {
self.inner = Some(connection);
return Ok(None);
}
Err(err) => return Err(AcceptedAlert::from_error(err, connection.core.side)),
};
let sig_schemes = match ClientHelloInput::from_input(&input) {
Ok(ClientHelloInput { sig_schemes, .. }) => sig_schemes,
Err(err) => {
return Err(AcceptedAlert::from_error(err, connection.core.side));
}
};
Ok(Some(Accepted {
connection,
input,
sig_schemes,
}))
}
}
/// Represents a TLS alert resulting from handling the client's `ClientHello` message.
///
/// When [`Acceptor::accept()`] returns an error, it yields an `AcceptedAlert` such that the
/// application can communicate failure to the client via [`AcceptedAlert::write()`].
pub struct AcceptedAlert(ChunkVecBuffer);
impl AcceptedAlert {
pub(super) fn from_error(error: Error, side: ServerConnectionData) -> (Error, Self) {
let mut common = side.into_common();
common.maybe_send_fatal_alert(&error);
(error, Self(common.sendable_tls))
}
pub(super) fn empty() -> Self {
Self(ChunkVecBuffer::new(None))
}
/// Send the alert to the client.
///
/// To account for short writes this function should be called repeatedly until it
/// returns `Ok(0)` or an error.
pub fn write(&mut self, wr: &mut dyn io::Write) -> Result<usize, io::Error> {
self.0.write_to(wr)
}
/// Send the alert to the client.
///
/// This function will invoke the writer until the buffer is empty.
pub fn write_all(&mut self, wr: &mut dyn io::Write) -> Result<(), io::Error> {
while self.write(wr)? != 0 {}
Ok(())
}
}
impl Debug for AcceptedAlert {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("AcceptedAlert").finish()
}
}
/// Allows reading of early data in resumed TLS1.3 connections.
///
/// "Early data" is also known as "0-RTT data".
///
/// This type implements [`io::Read`].
pub struct ReadEarlyData<'a> {
common: &'a mut ConnectionCommon<ServerConnectionData>,
}
impl<'a> ReadEarlyData<'a> {
fn new(common: &'a mut ConnectionCommon<ServerConnectionData>) -> Self {
ReadEarlyData { common }
}
/// Returns the "early" exporter that can derive key material for use in early data
///
/// See [RFC5705][] for general details on what exporters are, and [RFC8446 S7.5][] for
/// specific details on the "early" exporter.
///
/// **Beware** that the early exporter requires care, as it is subject to the same
/// potential for replay as early data itself. See [RFC8446 appendix E.5.1][] for
/// more detail.
///
/// This function can be called at most once per connection. This function will error:
/// if called more than once per connection.
///
/// If you are looking for the normal exporter, this is available from
/// [`ConnectionCommon::exporter()`].
///
/// [RFC5705]: https://datatracker.ietf.org/doc/html/rfc5705
/// [RFC8446 S7.5]: https://datatracker.ietf.org/doc/html/rfc8446#section-7.5
/// [RFC8446 appendix E.5.1]: https://datatracker.ietf.org/doc/html/rfc8446#appendix-E.5.1
/// [`ConnectionCommon::exporter()`]: crate::conn::ConnectionCommon::exporter()
pub fn exporter(&mut self) -> Result<KeyingMaterialExporter, Error> {
self.common.core.early_exporter()
}
}
impl io::Read for ReadEarlyData<'_> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.common
.core
.side
.early_data
.read(buf)
}
}
}
#[cfg(feature = "std")]
pub use buffered::{AcceptedAlert, Acceptor, ReadEarlyData, ServerConnection};
/// Unbuffered version of `ServerConnection`
///
/// See the [`crate::unbuffered`] module docs for more details
pub struct UnbufferedServerConnection {
inner: UnbufferedConnectionCommon<ServerConnectionData>,
}
impl UnbufferedServerConnection {
/// Make a new ServerConnection. `config` controls how we behave in the TLS protocol.
pub fn new(config: Arc<ServerConfig>) -> Result<Self, Error> {
Ok(Self {
inner: UnbufferedConnectionCommon::from(ConnectionCore::for_server(
config,
ServerExtensionsInput::default(),
Protocol::Tcp,
)?),
})
}
/// Extract secrets, so they can be used when configuring kTLS, for example.
/// Should be used with care as it exposes secret key material.
#[deprecated = "dangerous_extract_secrets() does not support session tickets or \
key updates, use dangerous_into_kernel_connection() instead"]
pub fn dangerous_extract_secrets(self) -> Result<ExtractedSecrets, Error> {
self.inner.dangerous_extract_secrets()
}
/// Extract secrets and an [`KernelConnection`] object.
///
/// This allows you use rustls to manage keys and then manage encryption and
/// decryption yourself (e.g. for kTLS).
///
/// Should be used with care as it exposes secret key material.
///
/// See the [`crate::kernel`] documentations for details on prerequisites
/// for calling this method.
pub fn dangerous_into_kernel_connection(
self,
) -> Result<(ExtractedSecrets, KernelConnection<ServerConnectionData>), Error> {
self.inner
.core
.dangerous_into_kernel_connection()
}
}
impl Deref for UnbufferedServerConnection {
type Target = UnbufferedConnectionCommon<ServerConnectionData>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl DerefMut for UnbufferedServerConnection {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl UnbufferedConnectionCommon<ServerConnectionData> {
pub(crate) fn pop_early_data(&mut self) -> Option<Vec<u8>> {
self.core.side.early_data.pop()
}
pub(crate) fn peek_early_data(&self) -> Option<&[u8]> {
self.core.side.early_data.peek()
}
}
/// Represents a `ClientHello` message received through the [`Acceptor`].
///
/// Contains the state required to resume the connection through [`Accepted::into_connection()`].
#[cfg(feature = "std")]
pub struct Accepted {
connection: ConnectionCommon<ServerConnectionData>,
input: Input<'static>,
sig_schemes: Vec<SignatureScheme>,
}
#[cfg(feature = "std")]
impl Accepted {
/// Get the [`ClientHello`] for this connection.
pub fn client_hello(&self) -> ClientHello<'_> {
let payload = Self::client_hello_payload(&self.input.message);
let server_name = payload
.server_name
.as_ref()
.and_then(ServerNamePayload::to_dns_name_normalized)
.map(Cow::Owned);
let ch = ClientHello {
server_name,
signature_schemes: &self.sig_schemes,
alpn: payload.protocols.as_ref(),
server_cert_types: payload
.server_certificate_types
.as_deref(),
client_cert_types: payload
.client_certificate_types
.as_deref(),
cipher_suites: &payload.cipher_suites,
certificate_authorities: payload
.certificate_authority_names
.as_deref(),
named_groups: payload.named_groups.as_deref(),
};
trace!("Accepted::client_hello(): {ch:#?}");
ch
}
/// Convert the [`Accepted`] into a [`ServerConnection`].
///
/// Takes the state returned from [`Acceptor::accept()`] as well as the [`ServerConfig`] that
/// should be used for the session. Returns an error if configuration-dependent validation of
/// the received `ClientHello` message fails.
pub fn into_connection(
mut self,
config: Arc<ServerConfig>,
) -> Result<ServerConnection, (Error, AcceptedAlert)> {
use crate::common_state::Context;
if let Err(err) = self
.connection
.set_max_fragment_size(config.max_fragment_size)
{
// We have a connection here, but it won't contain an alert since the error
// is with the fragment size configured in the `ServerConfig`.
return Err((err, AcceptedAlert::empty()));
}
let state =
hs::ExpectClientHello::new(config, ServerExtensionsInput::default(), Protocol::Tcp);
let proof = match self.input.check_aligned_handshake() {
Ok(proof) => proof,
Err(err) => {
return Err(AcceptedAlert::from_error(err, self.connection.core.side));
}
};
let mut cx = Context {
data: &mut self.connection.core.side,
// `ExpectClientHello::with_input` won't read borrowed plaintext
plaintext_locator: &Locator::new(&[]),
received_plaintext: &mut None,
};
let input = ClientHelloInput {
message: &self.input.message,
client_hello: Self::client_hello_payload(&self.input.message),
sig_schemes: self.sig_schemes,
proof,
};
let new = match state.with_input(input, &mut cx) {
Ok(new) => new,
Err(err) => return Err(AcceptedAlert::from_error(err, self.connection.core.side)),
};
debug_assert!(cx.received_plaintext.is_none(), "read plaintext");
self.connection.replace_state(new);
Ok(ServerConnection {
inner: self.connection,
})
}
fn client_hello_payload<'a>(message: &'a Message<'_>) -> &'a ClientHelloPayload {
match &message.payload {
MessagePayload::Handshake { parsed, .. } => match &parsed.0 {
HandshakePayload::ClientHello(ch) => ch,
_ => unreachable!(),
},
_ => unreachable!(),
}
}
}
#[cfg(feature = "std")]
impl Debug for Accepted {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Accepted").finish()
}
}
#[cfg(feature = "std")]
struct Accepting;
#[cfg(feature = "std")]
impl State<ServerConnectionData> for Accepting {
#[cfg_attr(coverage_nightly, coverage(off))]
fn handle<'m>(
self: Box<Self>,
_input: Input<'m>,
_output: &mut dyn Output,
) -> Result<Box<dyn State<ServerConnectionData>>, Error> {
Err(Error::Unreachable("unreachable state"))
}
}
#[derive(Default)]
pub(super) enum EarlyDataState {
#[default]
New,
Accepted {
received: ChunkVecBuffer,
},
}
impl EarlyDataState {
fn accept(&mut self) {
*self = Self::Accepted {
received: ChunkVecBuffer::new(None),
};
}
#[cfg(feature = "std")]
fn was_accepted(&self) -> bool {
matches!(self, Self::Accepted { .. })
}
fn peek(&self) -> Option<&[u8]> {
match self {
Self::Accepted { received, .. } => received.peek(),
_ => None,
}
}
fn pop(&mut self) -> Option<Vec<u8>> {
match self {
Self::Accepted { received, .. } => received.pop(),
_ => None,
}
}
#[cfg(feature = "std")]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self {
Self::Accepted { received, .. } => received.read(buf),
_ => Err(io::Error::from(io::ErrorKind::BrokenPipe)),
}
}
fn take_received_plaintext(&mut self, bytes: Payload<'_>) {
let Self::Accepted { received } = self else {
return;
};
received.append(bytes.into_vec());
}
}
impl Debug for EarlyDataState {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::New => write!(f, "EarlyDataState::New"),
Self::Accepted { received } => write!(
f,
"EarlyDataState::Accepted {{ received: {} }}",
received.len(),
),
}
}
}
impl ConnectionCore<ServerConnectionData> {
pub(crate) fn for_server(
config: Arc<ServerConfig>,
extra_exts: ServerExtensionsInput,
protocol: Protocol,
) -> Result<Self, Error> {
let mut common = CommonState::new(Side::Server, protocol);
common.set_max_fragment_size(config.max_fragment_size)?;
common.fips = config.fips();
Ok(Self::new(
Box::new(hs::ExpectClientHello::new(config, extra_exts, protocol)),
ServerConnectionData::new(common),
))
}
}
/// State associated with a server connection.
#[derive(Debug)]
pub struct ServerConnectionData {
common: CommonState,
sni: Option<DnsName<'static>>,
received_resumption_data: Option<Vec<u8>>,
early_data: EarlyDataState,
}
impl ServerConnectionData {
pub(crate) fn new(common: CommonState) -> Self {
Self {
common,
sni: None,
received_resumption_data: None,
early_data: EarlyDataState::default(),
}
}
#[cfg(feature = "std")]
pub(crate) fn received_resumption_data(&self) -> Option<&[u8]> {
self.received_resumption_data.as_deref()
}
#[cfg(feature = "std")]
pub(crate) fn server_name(&self) -> Option<&DnsName<'static>> {
self.sni.as_ref()
}
}
impl Output for ServerConnectionData {
fn emit(&mut self, ev: Event<'_>) {
match ev {
Event::EarlyApplicationData(data) => self
.early_data
.take_received_plaintext(data),
Event::EarlyData(EarlyDataEvent::Accepted) => self.early_data.accept(),
Event::ReceivedServerName(sni) => self.sni = sni,
Event::ResumptionData(data) => self.received_resumption_data = Some(data),
_ => {}
}
}
}
impl crate::conn::SideData for ServerConnectionData {}
impl crate::conn::private::SideData for ServerConnectionData {
fn into_common(self) -> CommonState {
self.common
}
}
impl Deref for ServerConnectionData {
type Target = CommonState;
fn deref(&self) -> &Self::Target {
&self.common
}
}
impl DerefMut for ServerConnectionData {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.common
}
}
#[cfg(feature = "std")]
#[cfg(test)]
mod tests {
use std::format;
use super::*;
// these branches not reachable externally, unless something else goes wrong.
#[test]
fn test_read_in_new_state() {
assert_eq!(
format!("{:?}", EarlyDataState::default().read(&mut [0u8; 5])),
"Err(Kind(BrokenPipe))"
);
}
}