Skip to main content

rustls/
common_state.rs

1use alloc::boxed::Box;
2use alloc::vec::Vec;
3use core::fmt;
4use core::ops::{Deref, DerefMut, Range};
5
6use pki_types::{DnsName, FipsStatus};
7
8use crate::client::EchStatus;
9use crate::conn::{DataKind, Exporter, KeyingMaterialExporter, ReceivePath, SendOutput, SendPath};
10use crate::crypto::cipher::{EncodableVersion, Payload};
11use crate::crypto::kx::SupportedKxGroup;
12use crate::enums::{ApplicationProtocol, ProtocolVersion};
13use crate::error::{AlertDescription, ApiMisuse, Error};
14use crate::hash_hs::HandshakeHash;
15use crate::msgs::{
16    AlertLevel, Codec, Delocator, HandshakeMessagePayload, Locator, Message, MessagePayload,
17};
18use crate::quic::{self, QuicOutput};
19use crate::suites::SupportedCipherSuite;
20use crate::verify::VerifiedIdentity;
21
22/// Connection state common to both client and server connections.
23pub struct CommonState {
24    pub(crate) outputs: ConnectionOutputs,
25    pub(crate) send: SendPath,
26    pub(crate) recv: ReceivePath,
27    pub(crate) fips: FipsStatus,
28}
29
30impl CommonState {
31    pub(crate) fn new(side: Side, fips: FipsStatus) -> Self {
32        Self {
33            outputs: ConnectionOutputs::default(),
34            send: SendPath::default(),
35            recv: ReceivePath::new(side),
36            fips,
37        }
38    }
39
40    pub(crate) fn early_exporter(&mut self) -> Result<KeyingMaterialExporter, Error> {
41        match self.early_exporter.take() {
42            Some(inner) => Ok(KeyingMaterialExporter { inner }),
43            None => Err(ApiMisuse::ExporterAlreadyUsed.into()),
44        }
45    }
46
47    /// Writes a `close_notify` warning alert to into the `tls` buffer.
48    ///
49    /// This informs the peer that the connection is being closed. Does nothing if any
50    /// `close_notify` or fatal alert was already sent.
51    pub fn send_close_notify(&mut self, tls: &mut Vec<u8>) {
52        self.send.send_close_notify(tls)
53    }
54
55    /// Returns true if the connection is currently performing the TLS handshake.
56    ///
57    /// During this time plaintext written to the connection is buffered in memory. After
58    /// [`Connection::read_tls()`] has been called, this might start to return `false`
59    /// while the final handshake packets still need to be extracted from the connection's buffers.
60    ///
61    /// [`Connection::read_tls()`]: crate::Connection::read_tls
62    pub fn is_handshaking(&self) -> bool {
63        !(self.send.may_send_application_data && self.recv.may_receive_application_data)
64    }
65}
66
67impl Deref for CommonState {
68    type Target = ConnectionOutputs;
69
70    fn deref(&self) -> &Self::Target {
71        &self.outputs
72    }
73}
74
75impl DerefMut for CommonState {
76    fn deref_mut(&mut self) -> &mut Self::Target {
77        &mut self.outputs
78    }
79}
80
81impl fmt::Debug for CommonState {
82    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83        f.debug_struct("CommonState")
84            .finish_non_exhaustive()
85    }
86}
87
88/// Facts about the connection learned through the handshake.
89#[derive(Default)]
90pub struct ConnectionOutputs {
91    negotiated_version: Option<ProtocolVersion>,
92    handshake_kind: Option<HandshakeKind>,
93    suite: Option<SupportedCipherSuite>,
94    negotiated_kx_group: Option<&'static dyn SupportedKxGroup>,
95    alpn_protocol: Option<ApplicationProtocol<'static>>,
96    peer_identity: Option<VerifiedIdentity<'static>>,
97    extended_main_secret: Option<bool>,
98    pub(crate) exporter: Option<Box<dyn Exporter>>,
99    pub(crate) early_exporter: Option<Box<dyn Exporter>>,
100}
101
102impl ConnectionOutputs {
103    /// Retrieves the certificate chain or the raw public key used by the peer to authenticate.
104    ///
105    /// This is made available for both full and resumed handshakes.
106    ///
107    /// For clients, this is the identity of the server. For servers, this is the identity of the
108    /// client, if client authentication was completed.
109    ///
110    /// The return value is None until this value is available.
111    pub fn peer_identity(&self) -> Option<&VerifiedIdentity<'static>> {
112        self.peer_identity.as_ref()
113    }
114
115    /// Retrieves the protocol agreed with the peer via ALPN.
116    ///
117    /// A return value of `None` after handshake completion
118    /// means no protocol was agreed (because no protocols
119    /// were offered or accepted by the peer).
120    pub fn alpn_protocol(&self) -> Option<&ApplicationProtocol<'static>> {
121        self.alpn_protocol.as_ref()
122    }
123
124    /// Retrieves the cipher suite agreed with the peer.
125    ///
126    /// This returns None until the cipher suite is agreed.
127    pub fn negotiated_cipher_suite(&self) -> Option<SupportedCipherSuite> {
128        self.suite
129    }
130
131    /// Retrieves the key exchange group agreed with the peer.
132    ///
133    /// This function may return `None` depending on the state of the connection,
134    /// the type of handshake, and the protocol version.
135    ///
136    /// If [`CommonState::is_handshaking()`] is true this function will return `None`.
137    /// Similarly, if the [`ConnectionOutputs::handshake_kind()`] is [`HandshakeKind::Resumed`]
138    /// and the [`ConnectionOutputs::protocol_version()`] is TLS 1.2, then no key exchange will have
139    /// occurred and this function will return `None`.
140    pub fn negotiated_key_exchange_group(&self) -> Option<&'static dyn SupportedKxGroup> {
141        self.negotiated_kx_group
142    }
143
144    /// Retrieves the protocol version agreed with the peer.
145    ///
146    /// This returns `None` until the version is agreed.
147    pub fn protocol_version(&self) -> Option<ProtocolVersion> {
148        self.negotiated_version
149    }
150
151    /// Whether the Extended Main Secret extension was negotiated.
152    ///
153    /// Returns:
154    /// - `None` until the handshake reaches the point where this is known.
155    /// - `None` for TLS 1.3, where the extension does not apply.
156    /// - `Some(true)` for TLS 1.2 if the extension was negotiated.
157    /// - `Some(false)` otherwise.
158    pub fn extended_main_secret(&self) -> Option<bool> {
159        self.extended_main_secret
160    }
161
162    /// Which kind of handshake was performed.
163    ///
164    /// This tells you whether the handshake was a resumption or not.
165    ///
166    /// This will return `None` before it is known which sort of
167    /// handshake occurred.
168    pub fn handshake_kind(&self) -> Option<HandshakeKind> {
169        self.handshake_kind
170    }
171
172    pub(super) fn into_kernel_parts(self) -> Option<(ProtocolVersion, SupportedCipherSuite)> {
173        let Self {
174            negotiated_version,
175            suite,
176            ..
177        } = self;
178
179        match (negotiated_version, suite) {
180            (Some(version), Some(suite)) => Some((version, suite)),
181            _ => None,
182        }
183    }
184}
185
186impl ConnectionOutput for ConnectionOutputs {
187    fn handle(&mut self, ev: OutputEvent<'_>) {
188        match ev {
189            OutputEvent::ApplicationProtocol(protocol) => {
190                self.alpn_protocol = Some(ApplicationProtocol::from(protocol.as_ref()).to_owned())
191            }
192            OutputEvent::CipherSuite(suite) => self.suite = Some(suite),
193            OutputEvent::EarlyExporter(exporter) => self.early_exporter = Some(exporter),
194            OutputEvent::Exporter(exporter) => self.exporter = Some(exporter),
195            OutputEvent::ExtendedMainSecret(ems) => self.extended_main_secret = Some(ems),
196            OutputEvent::HandshakeKind(hk) => {
197                assert!(self.handshake_kind.is_none());
198                self.handshake_kind = Some(hk);
199            }
200            OutputEvent::KeyExchangeGroup(kxg) => {
201                assert!(self.negotiated_kx_group.is_none());
202                self.negotiated_kx_group = Some(kxg);
203            }
204            OutputEvent::PeerIdentity(identity) => self.peer_identity = Some(identity),
205            OutputEvent::ProtocolVersion(ver) => {
206                self.negotiated_version = Some(ver);
207            }
208        }
209    }
210}
211
212impl fmt::Debug for ConnectionOutputs {
213    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
214        let Self {
215            negotiated_version,
216            handshake_kind,
217            suite,
218            negotiated_kx_group,
219            alpn_protocol,
220            peer_identity,
221            extended_main_secret,
222            exporter: _,
223            early_exporter: _,
224        } = self;
225        f.debug_struct("ConnectionOutputs")
226            .field("negotiated_version", negotiated_version)
227            .field("handshake_kind", handshake_kind)
228            .field("suite", suite)
229            .field("negotiated_kx_group", negotiated_kx_group)
230            .field("alpn_protocol", alpn_protocol)
231            .field("peer_identity", peer_identity)
232            .field("extended_main_secret", extended_main_secret)
233            .finish_non_exhaustive()
234    }
235}
236
237/// Send an alert via `output` if `error` specifies one.
238pub(crate) fn maybe_send_fatal_alert(send: &mut dyn SendOutput, error: &Error, tls: &mut Vec<u8>) {
239    let Ok(alert) = AlertDescription::try_from(error) else {
240        return;
241    };
242    send.send_alert(AlertLevel::Fatal, alert, tls);
243}
244
245/// Describes which sort of handshake happened.
246#[derive(Debug, PartialEq, Clone, Copy)]
247#[non_exhaustive]
248pub enum HandshakeKind {
249    /// A full handshake.
250    ///
251    /// This is the typical TLS connection initiation process when resumption is
252    /// not available, and the initial `ClientHello` was accepted by the server.
253    Full,
254
255    /// A full TLS1.3 handshake, with an extra round-trip for a `HelloRetryRequest`.
256    ///
257    /// The server can respond with a `HelloRetryRequest` if the initial `ClientHello`
258    /// is unacceptable for several reasons, the most likely being if no supported key
259    /// shares were offered by the client.
260    FullWithHelloRetryRequest,
261
262    /// A resumed handshake.
263    ///
264    /// Resumed handshakes involve fewer round trips and less cryptography than
265    /// full ones, but can only happen when the peers have previously done a full
266    /// handshake together, and then remember data about it.
267    Resumed,
268
269    /// A resumed handshake, with an extra round-trip for a `HelloRetryRequest`.
270    ///
271    /// The server can respond with a `HelloRetryRequest` if the initial `ClientHello`
272    /// is unacceptable for several reasons, but this does not prevent the client
273    /// from resuming.
274    ResumedWithHelloRetryRequest,
275}
276
277/// The route for handshake state machine to surface determinations about the connection.
278pub(crate) trait Output<'m> {
279    fn emit(&mut self, ev: Event);
280
281    fn output(&mut self, ev: OutputEvent<'_>);
282
283    fn send_msg(&mut self, m: Message<'_>, must_encrypt: bool);
284
285    fn quic(&mut self) -> Option<&mut dyn QuicOutput> {
286        None
287    }
288
289    fn received_plaintext(&mut self, _payload: DataKind<Payload<'m>>) {}
290
291    fn start_traffic(&mut self);
292
293    fn receive(&mut self) -> &mut ReceivePath;
294
295    fn send(&mut self) -> &mut dyn SendOutput;
296}
297
298pub(crate) trait ConnectionOutput {
299    fn handle(&mut self, ev: OutputEvent<'_>);
300}
301
302/// The set of events output by the low-level handshake state machine.
303pub(crate) enum Event {
304    EarlyData(EarlyDataEvent),
305    EchStatus(EchStatus),
306    ReceivedServerName(Option<DnsName<'static>>),
307    ResumptionData(Vec<u8>),
308}
309
310pub(crate) enum OutputEvent<'a> {
311    ApplicationProtocol(ApplicationProtocol<'a>),
312    CipherSuite(SupportedCipherSuite),
313    EarlyExporter(Box<dyn Exporter>),
314    Exporter(Box<dyn Exporter>),
315    ExtendedMainSecret(bool),
316    HandshakeKind(HandshakeKind),
317    KeyExchangeGroup(&'static dyn SupportedKxGroup),
318    PeerIdentity(VerifiedIdentity<'static>),
319    ProtocolVersion(ProtocolVersion),
320}
321
322pub(crate) enum EarlyDataEvent {
323    /// server: we accepted an early_data offer
324    Accepted,
325    /// client: declares the maximum amount of early data that can be sent
326    Enable(usize),
327    /// client: early data can now be sent using the record layer as normal
328    Start,
329    /// client: early data phase has closed after sending EndOfEarlyData
330    Finished,
331    /// client: the server rejected our request for early data
332    Rejected,
333}
334
335/// Lifetime-erased equivalent to [`Payload`]
336///
337/// Stores an index into [`Payload`] buffer enabling in-place decryption
338/// without holding a lifetime to the receive buffer.
339pub(crate) enum UnborrowedPayload {
340    Unborrowed(Range<usize>),
341    Owned(Vec<u8>),
342}
343
344impl UnborrowedPayload {
345    /// Convert [`Payload`] into [`UnborrowedPayload`] which stores a range
346    /// into the [`Payload`] slice without borrowing such that it can be later
347    /// reborrowed.
348    ///
349    /// # Panics
350    ///
351    /// Passed [`Locator`] must have been created from the same slice which
352    /// contains the payload.
353    pub(crate) fn unborrow(locator: &Locator, payload: Payload<'_>) -> Self {
354        match payload {
355            Payload::Borrowed(payload) => Self::Unborrowed(locator.locate(payload)),
356            Payload::Owned(payload) => Self::Owned(payload),
357        }
358    }
359
360    /// Convert [`UnborrowedPayload`] back into [`Payload`]
361    ///
362    /// # Panics
363    ///
364    /// Passed [`Delocator`] must have been created from the same slice that
365    /// [`UnborrowedPayload`] was originally unborrowed from.
366    pub(crate) fn reborrow<'b>(self, delocator: &Delocator<'b>) -> Payload<'b> {
367        match self {
368            Self::Unborrowed(range) => Payload::Borrowed(delocator.slice_from_range(&range)),
369            Self::Owned(payload) => Payload::Owned(payload),
370        }
371    }
372}
373
374/// Side of the connection.
375#[expect(clippy::exhaustive_enums)]
376#[derive(Clone, Copy, Debug, PartialEq)]
377pub enum Side {
378    /// A client initiates the connection.
379    Client,
380    /// A server waits for a client to connect.
381    Server,
382}
383
384/// Transport protocol in use for a connection.
385#[derive(Copy, Clone, Eq, PartialEq, Debug)]
386#[non_exhaustive]
387pub enum Protocol {
388    /// TCP-TLS, standardized in RFC 5246 and RFC 9846
389    Tcp,
390    /// QUIC, standardized in RFC 9001
391    Quic(quic::Version),
392}
393
394impl Protocol {
395    pub(crate) fn is_quic(&self) -> bool {
396        matches!(self, Self::Quic(_))
397    }
398
399    pub(crate) fn supports_version(&self, version: ProtocolVersion) -> bool {
400        match self {
401            Self::Quic(_) => version == ProtocolVersion::TLSv1_3,
402            Self::Tcp => true,
403        }
404    }
405}
406
407pub(crate) struct HandshakeFlight<'a, const TLS13: bool> {
408    pub(crate) transcript: &'a mut HandshakeHash,
409    body: Vec<u8>,
410}
411
412impl<'a, const TLS13: bool> HandshakeFlight<'a, TLS13> {
413    pub(crate) fn new(transcript: &'a mut HandshakeHash) -> Self {
414        Self {
415            transcript,
416            body: Vec::new(),
417        }
418    }
419
420    pub(crate) fn add(&mut self, hs: HandshakeMessagePayload<'_>) {
421        let start_len = self.body.len();
422        hs.encode(&mut self.body);
423        self.transcript
424            .add(&self.body[start_len..]);
425    }
426
427    pub(crate) fn finish(self, output: &mut dyn Output<'_>) {
428        let m = Message {
429            version: EncodableVersion::Legacy(match TLS13 {
430                true => ProtocolVersion::TLSv1_3,
431                false => ProtocolVersion::TLSv1_2,
432            }),
433            payload: MessagePayload::HandshakeFlight(Payload::new(self.body)),
434        };
435
436        output.send_msg(m, TLS13);
437    }
438}
439
440pub(crate) type HandshakeFlightTls12<'a> = HandshakeFlight<'a, false>;
441pub(crate) type HandshakeFlightTls13<'a> = HandshakeFlight<'a, true>;