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::{Exporter, ReceivePath, SendOutput, SendPath};
10use crate::crypto::Identity;
11use crate::crypto::cipher::Payload;
12use crate::crypto::kx::SupportedKxGroup;
13use crate::enums::{ApplicationProtocol, ProtocolVersion};
14use crate::error::{AlertDescription, Error};
15use crate::hash_hs::HandshakeHash;
16use crate::msgs::{
17 AlertLevel, Codec, Delocator, HandshakeMessagePayload, Locator, Message, MessagePayload,
18};
19use crate::quic::{self, QuicOutput};
20use crate::suites::SupportedCipherSuite;
21
22pub 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 fn wants_write(&self) -> bool {
44 !self.send.sendable_tls.is_empty()
45 }
46
47 pub fn send_close_notify(&mut self) {
55 self.send.send_close_notify()
56 }
57
58 pub fn is_handshaking(&self) -> bool {
66 !(self.send.may_send_application_data && self.recv.may_receive_application_data)
67 }
68}
69
70impl Deref for CommonState {
71 type Target = ConnectionOutputs;
72
73 fn deref(&self) -> &Self::Target {
74 &self.outputs
75 }
76}
77
78impl DerefMut for CommonState {
79 fn deref_mut(&mut self) -> &mut Self::Target {
80 &mut self.outputs
81 }
82}
83
84impl fmt::Debug for CommonState {
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86 f.debug_struct("CommonState")
87 .finish_non_exhaustive()
88 }
89}
90
91#[derive(Default)]
93pub struct ConnectionOutputs {
94 negotiated_version: Option<ProtocolVersion>,
95 handshake_kind: Option<HandshakeKind>,
96 suite: Option<SupportedCipherSuite>,
97 negotiated_kx_group: Option<&'static dyn SupportedKxGroup>,
98 alpn_protocol: Option<ApplicationProtocol<'static>>,
99 peer_identity: Option<Identity<'static>>,
100 extended_master_secret: Option<bool>,
101 pub(crate) exporter: Option<Box<dyn Exporter>>,
102 pub(crate) early_exporter: Option<Box<dyn Exporter>>,
103}
104
105impl ConnectionOutputs {
106 pub fn peer_identity(&self) -> Option<&Identity<'static>> {
115 self.peer_identity.as_ref()
116 }
117
118 pub fn alpn_protocol(&self) -> Option<&ApplicationProtocol<'static>> {
124 self.alpn_protocol.as_ref()
125 }
126
127 pub fn negotiated_cipher_suite(&self) -> Option<SupportedCipherSuite> {
131 self.suite
132 }
133
134 pub fn negotiated_key_exchange_group(&self) -> Option<&'static dyn SupportedKxGroup> {
144 self.negotiated_kx_group
145 }
146
147 pub fn protocol_version(&self) -> Option<ProtocolVersion> {
151 self.negotiated_version
152 }
153
154 pub fn extended_master_secret(&self) -> Option<bool> {
162 self.extended_master_secret
163 }
164
165 pub fn handshake_kind(&self) -> Option<HandshakeKind> {
172 self.handshake_kind
173 }
174
175 pub(super) fn into_kernel_parts(self) -> Option<(ProtocolVersion, SupportedCipherSuite)> {
176 let Self {
177 negotiated_version,
178 suite,
179 ..
180 } = self;
181
182 match (negotiated_version, suite) {
183 (Some(version), Some(suite)) => Some((version, suite)),
184 _ => None,
185 }
186 }
187}
188
189impl ConnectionOutput for ConnectionOutputs {
190 fn handle(&mut self, ev: OutputEvent<'_>) {
191 match ev {
192 OutputEvent::ApplicationProtocol(protocol) => {
193 self.alpn_protocol = Some(ApplicationProtocol::from(protocol.as_ref()).to_owned())
194 }
195 OutputEvent::CipherSuite(suite) => self.suite = Some(suite),
196 OutputEvent::EarlyExporter(exporter) => self.early_exporter = Some(exporter),
197 OutputEvent::Exporter(exporter) => self.exporter = Some(exporter),
198 OutputEvent::ExtendedMasterSecret(ems) => self.extended_master_secret = Some(ems),
199 OutputEvent::HandshakeKind(hk) => {
200 assert!(self.handshake_kind.is_none());
201 self.handshake_kind = Some(hk);
202 }
203 OutputEvent::KeyExchangeGroup(kxg) => {
204 assert!(self.negotiated_kx_group.is_none());
205 self.negotiated_kx_group = Some(kxg);
206 }
207 OutputEvent::PeerIdentity(identity) => self.peer_identity = Some(identity),
208 OutputEvent::ProtocolVersion(ver) => {
209 self.negotiated_version = Some(ver);
210 }
211 }
212 }
213}
214
215pub(crate) fn maybe_send_fatal_alert(send: &mut dyn SendOutput, error: &Error) {
217 let Ok(alert) = AlertDescription::try_from(error) else {
218 return;
219 };
220 send.send_alert(AlertLevel::Fatal, alert);
221}
222
223#[derive(Debug, PartialEq, Clone, Copy)]
225#[non_exhaustive]
226pub enum HandshakeKind {
227 Full,
232
233 FullWithHelloRetryRequest,
239
240 Resumed,
246
247 ResumedWithHelloRetryRequest,
253}
254
255pub(crate) trait Output<'m> {
257 fn emit(&mut self, ev: Event<'_>);
258
259 fn output(&mut self, ev: OutputEvent<'_>);
260
261 fn send_msg(&mut self, m: Message<'_>, must_encrypt: bool);
262
263 fn quic(&mut self) -> Option<&mut dyn QuicOutput> {
264 None
265 }
266
267 fn received_plaintext(&mut self, _payload: Payload<'m>) {}
268
269 fn start_traffic(&mut self);
270
271 fn receive(&mut self) -> &mut ReceivePath;
272
273 fn send(&mut self) -> &mut dyn SendOutput;
274}
275
276pub(crate) trait ConnectionOutput {
277 fn handle(&mut self, ev: OutputEvent<'_>);
278}
279
280pub(crate) enum Event<'a> {
282 EarlyApplicationData(Payload<'a>),
283 EarlyData(EarlyDataEvent),
284 EchStatus(EchStatus),
285 ReceivedServerName(Option<DnsName<'static>>),
286 ResumptionData(Vec<u8>),
287}
288
289pub(crate) enum OutputEvent<'a> {
290 ApplicationProtocol(ApplicationProtocol<'a>),
291 CipherSuite(SupportedCipherSuite),
292 EarlyExporter(Box<dyn Exporter>),
293 Exporter(Box<dyn Exporter>),
294 ExtendedMasterSecret(bool),
295 HandshakeKind(HandshakeKind),
296 KeyExchangeGroup(&'static dyn SupportedKxGroup),
297 PeerIdentity(Identity<'static>),
298 ProtocolVersion(ProtocolVersion),
299}
300
301pub(crate) enum EarlyDataEvent {
302 Accepted,
304 Enable(usize),
306 Start,
308 Finished,
310 Rejected,
312}
313
314pub(crate) enum UnborrowedPayload {
319 Unborrowed(Range<usize>),
320 Owned(Vec<u8>),
321}
322
323impl UnborrowedPayload {
324 pub(crate) fn unborrow(locator: &Locator, payload: Payload<'_>) -> Self {
333 match payload {
334 Payload::Borrowed(payload) => Self::Unborrowed(locator.locate(payload)),
335 Payload::Owned(payload) => Self::Owned(payload),
336 }
337 }
338
339 pub(crate) fn reborrow<'b>(self, delocator: &Delocator<'b>) -> Payload<'b> {
346 match self {
347 Self::Unborrowed(range) => Payload::Borrowed(delocator.slice_from_range(&range)),
348 Self::Owned(payload) => Payload::Owned(payload),
349 }
350 }
351}
352
353#[expect(clippy::exhaustive_enums)]
355#[derive(Clone, Copy, Debug, PartialEq)]
356pub enum Side {
357 Client,
359 Server,
361}
362
363#[derive(Copy, Clone, Eq, PartialEq, Debug)]
364pub(crate) enum Protocol {
365 Tcp,
367 Quic(quic::Version),
369}
370
371impl Protocol {
372 pub(crate) fn is_quic(&self) -> bool {
373 matches!(self, Self::Quic(_))
374 }
375}
376
377pub(crate) struct HandshakeFlight<'a, const TLS13: bool> {
378 pub(crate) transcript: &'a mut HandshakeHash,
379 body: Vec<u8>,
380}
381
382impl<'a, const TLS13: bool> HandshakeFlight<'a, TLS13> {
383 pub(crate) fn new(transcript: &'a mut HandshakeHash) -> Self {
384 Self {
385 transcript,
386 body: Vec::new(),
387 }
388 }
389
390 pub(crate) fn add(&mut self, hs: HandshakeMessagePayload<'_>) {
391 let start_len = self.body.len();
392 hs.encode(&mut self.body);
393 self.transcript
394 .add(&self.body[start_len..]);
395 }
396
397 pub(crate) fn finish(self, output: &mut dyn Output<'_>) {
398 let m = Message {
399 version: match TLS13 {
400 true => ProtocolVersion::TLSv1_3,
401 false => ProtocolVersion::TLSv1_2,
402 },
403 payload: MessagePayload::HandshakeFlight(Payload::new(self.body)),
404 };
405
406 output.send_msg(m, TLS13);
407 }
408}
409
410pub(crate) type HandshakeFlightTls12<'a> = HandshakeFlight<'a, false>;
411pub(crate) type HandshakeFlightTls13<'a> = HandshakeFlight<'a, true>;