Skip to main content

rustls/
common_state.rs

1use alloc::boxed::Box;
2use alloc::vec::Vec;
3use core::fmt;
4use core::ops::Range;
5
6use pki_types::{DnsName, FipsStatus};
7
8use crate::client::EchStatus;
9use crate::conn::Exporter;
10use crate::conn::kernel::KernelState;
11use crate::crypto::Identity;
12use crate::crypto::cipher::{
13    DecryptionState, EncodedMessage, EncryptionState, MessageDecrypter, MessageEncrypter,
14    OutboundOpaque, OutboundPlain, Payload, PreEncryptAction,
15};
16use crate::crypto::kx::SupportedKxGroup;
17use crate::crypto::tls13::OkmBlock;
18use crate::enums::{ApplicationProtocol, ContentType, HandshakeType, ProtocolVersion};
19use crate::error::{AlertDescription, ApiMisuse, Error, PeerMisbehaved};
20use crate::hash_hs::HandshakeHash;
21use crate::log::{debug, error, trace, warn};
22use crate::msgs::{
23    AlertLevel, AlertMessagePayload, Codec, Delocator, HandshakeAlignedProof,
24    HandshakeMessagePayload, Locator, Message, MessageFragmenter, MessagePayload,
25};
26use crate::suites::{PartiallyExtractedSecrets, SupportedCipherSuite};
27use crate::tls13::key_schedule::KeyScheduleTrafficSend;
28use crate::unbuffered::{EncryptError, InsufficientSizeError};
29use crate::vecbuf::ChunkVecBuffer;
30use crate::{SideData, quic};
31
32pub(crate) fn process_main_protocol<Data: SideData>(
33    msg: EncodedMessage<&'_ [u8]>,
34    aligned_handshake: Option<HandshakeAlignedProof>,
35    state: Box<dyn State<Data>>,
36    plaintext_locator: &Locator,
37    received_plaintext: &mut Option<UnborrowedPayload>,
38    data: &mut Data,
39) -> Result<Box<dyn State<Data>>, Error> {
40    // Drop CCS messages during handshake in TLS1.3
41    if msg.typ == ContentType::ChangeCipherSpec
42        && !data.may_receive_application_data
43        && data.is_tls13()
44    {
45        if !msg.is_valid_ccs() {
46            // "An implementation which receives any other change_cipher_spec value or
47            //  which receives a protected change_cipher_spec record MUST abort the
48            //  handshake with an "unexpected_message" alert."
49            return Err(PeerMisbehaved::IllegalMiddleboxChangeCipherSpec.into());
50        }
51
52        data.temper_counters
53            .received_tls13_change_cipher_spec()?;
54        trace!("Dropping CCS");
55        return Ok(state);
56    }
57
58    // Now we can fully parse the message payload.
59    let msg = Message::try_from(&msg)?;
60
61    // For alerts, we have separate logic.
62    if let MessagePayload::Alert(alert) = &msg.payload {
63        data.process_alert(alert)?;
64        return Ok(state);
65    }
66
67    // For TLS1.2, outside of the handshake, send rejection alerts for
68    // renegotiation requests.  These can occur any time.
69    if data.may_receive_application_data && !data.is_tls13() {
70        let reject_ty = match data.side {
71            Side::Client => HandshakeType::HelloRequest,
72            Side::Server => HandshakeType::ClientHello,
73        };
74
75        if msg.handshake_type() == Some(reject_ty) {
76            data.temper_counters
77                .received_renegotiation_request()?;
78            let desc = AlertDescription::NoRenegotiation;
79            warn!("sending warning alert {desc:?}");
80            data.send_warning_alert_no_log(desc);
81            return Ok(state);
82        }
83    }
84
85    let mut cx = Context {
86        data,
87        plaintext_locator,
88        received_plaintext,
89    };
90
91    state.handle(
92        Input {
93            message: msg,
94            aligned_handshake,
95        },
96        &mut cx,
97    )
98}
99
100/// Connection state common to both client and server connections.
101pub struct CommonState {
102    pub(crate) negotiated_version: Option<ProtocolVersion>,
103    handshake_kind: Option<HandshakeKind>,
104    side: Side,
105    pub(crate) decrypt_state: DecryptionState,
106    pub(crate) encrypt_state: EncryptionState,
107    suite: Option<SupportedCipherSuite>,
108    negotiated_kx_group: Option<&'static dyn SupportedKxGroup>,
109    pub(crate) alpn_protocol: Option<ApplicationProtocol<'static>>,
110    pub(crate) exporter: Option<Box<dyn Exporter>>,
111    pub(crate) early_exporter: Option<Box<dyn Exporter>>,
112    pub(crate) may_send_application_data: bool,
113    may_receive_application_data: bool,
114    has_sent_fatal_alert: bool,
115    /// If we signaled end of stream.
116    pub(crate) has_sent_close_notify: bool,
117    /// If the peer has signaled end of stream.
118    pub(crate) has_received_close_notify: bool,
119    #[cfg(feature = "std")]
120    pub(crate) has_seen_eof: bool,
121    pub(crate) peer_identity: Option<Identity<'static>>,
122    message_fragmenter: MessageFragmenter,
123    pub(crate) received_plaintext: ChunkVecBuffer,
124    pub(crate) sendable_tls: ChunkVecBuffer,
125    queued_key_update_message: Option<Vec<u8>>,
126
127    /// Protocol whose key schedule should be used. Unused for TLS < 1.3.
128    pub(crate) protocol: Protocol,
129    pub(crate) quic: quic::Quic,
130    temper_counters: TemperCounters,
131    pub(crate) refresh_traffic_keys_pending: bool,
132    pub(crate) fips: FipsStatus,
133    pub(crate) tls13_tickets_received: u32,
134}
135
136impl CommonState {
137    pub(crate) fn new(side: Side, protocol: Protocol) -> Self {
138        Self {
139            negotiated_version: None,
140            handshake_kind: None,
141            side,
142            decrypt_state: DecryptionState::new(),
143            encrypt_state: EncryptionState::new(),
144            suite: None,
145            negotiated_kx_group: None,
146            alpn_protocol: None,
147            exporter: None,
148            early_exporter: None,
149            may_send_application_data: false,
150            may_receive_application_data: false,
151            has_sent_fatal_alert: false,
152            has_sent_close_notify: false,
153            has_received_close_notify: false,
154            #[cfg(feature = "std")]
155            has_seen_eof: false,
156            peer_identity: None,
157            message_fragmenter: MessageFragmenter::default(),
158            received_plaintext: ChunkVecBuffer::new(Some(DEFAULT_RECEIVED_PLAINTEXT_LIMIT)),
159            sendable_tls: ChunkVecBuffer::new(Some(DEFAULT_BUFFER_LIMIT)),
160            queued_key_update_message: None,
161            protocol,
162            quic: quic::Quic::default(),
163            temper_counters: TemperCounters::default(),
164            refresh_traffic_keys_pending: false,
165            fips: FipsStatus::Unvalidated,
166            tls13_tickets_received: 0,
167        }
168    }
169
170    /// Returns true if the caller should call [`Connection::write_tls`] as soon as possible.
171    ///
172    /// [`Connection::write_tls`]: crate::Connection::write_tls
173    pub fn wants_write(&self) -> bool {
174        !self.sendable_tls.is_empty()
175    }
176
177    /// Returns true if the connection is currently performing the TLS handshake.
178    ///
179    /// During this time plaintext written to the connection is buffered in memory. After
180    /// [`Connection::process_new_packets()`] has been called, this might start to return `false`
181    /// while the final handshake packets still need to be extracted from the connection's buffers.
182    ///
183    /// [`Connection::process_new_packets()`]: crate::Connection::process_new_packets
184    pub fn is_handshaking(&self) -> bool {
185        !(self.may_send_application_data && self.may_receive_application_data)
186    }
187
188    /// Retrieves the certificate chain or the raw public key used by the peer to authenticate.
189    ///
190    /// This is made available for both full and resumed handshakes.
191    ///
192    /// For clients, this is the identity of the server. For servers, this is the identity of the
193    /// client, if client authentication was completed.
194    ///
195    /// The return value is None until this value is available.
196    pub fn peer_identity(&self) -> Option<&Identity<'static>> {
197        self.peer_identity.as_ref()
198    }
199
200    /// Retrieves the protocol agreed with the peer via ALPN.
201    ///
202    /// A return value of `None` after handshake completion
203    /// means no protocol was agreed (because no protocols
204    /// were offered or accepted by the peer).
205    pub fn alpn_protocol(&self) -> Option<&ApplicationProtocol<'static>> {
206        self.alpn_protocol.as_ref()
207    }
208
209    /// Retrieves the cipher suite agreed with the peer.
210    ///
211    /// This returns None until the cipher suite is agreed.
212    pub fn negotiated_cipher_suite(&self) -> Option<SupportedCipherSuite> {
213        self.suite
214    }
215
216    /// Retrieves the key exchange group agreed with the peer.
217    ///
218    /// This function may return `None` depending on the state of the connection,
219    /// the type of handshake, and the protocol version.
220    ///
221    /// If [`CommonState::is_handshaking()`] is true this function will return `None`.
222    /// Similarly, if the [`CommonState::handshake_kind()`] is [`HandshakeKind::Resumed`]
223    /// and the [`CommonState::protocol_version()`] is TLS 1.2, then no key exchange will have
224    /// occurred and this function will return `None`.
225    pub fn negotiated_key_exchange_group(&self) -> Option<&'static dyn SupportedKxGroup> {
226        self.negotiated_kx_group
227    }
228
229    /// Retrieves the protocol version agreed with the peer.
230    ///
231    /// This returns `None` until the version is agreed.
232    pub fn protocol_version(&self) -> Option<ProtocolVersion> {
233        self.negotiated_version
234    }
235
236    /// Which kind of handshake was performed.
237    ///
238    /// This tells you whether the handshake was a resumption or not.
239    ///
240    /// This will return `None` before it is known which sort of
241    /// handshake occurred.
242    pub fn handshake_kind(&self) -> Option<HandshakeKind> {
243        self.handshake_kind
244    }
245
246    fn is_tls13(&self) -> bool {
247        matches!(self.negotiated_version, Some(ProtocolVersion::TLSv1_3))
248    }
249
250    pub(super) fn into_kernel_parts(self) -> Option<(ProtocolVersion, SupportedCipherSuite)> {
251        let Self {
252            negotiated_version,
253            suite,
254            ..
255        } = self;
256
257        match (negotiated_version, suite) {
258            (Some(version), Some(suite)) => Some((version, suite)),
259            _ => None,
260        }
261    }
262
263    pub(crate) fn maybe_send_fatal_alert(&mut self, error: &Error) {
264        let Ok(alert) = AlertDescription::try_from(error) else {
265            return;
266        };
267        debug_assert!(!self.has_sent_fatal_alert);
268        let m = Message::build_alert(AlertLevel::Fatal, alert);
269        self.send_msg(m, self.encrypt_state.is_encrypting());
270        self.has_sent_fatal_alert = true;
271    }
272
273    pub(crate) fn write_plaintext(
274        &mut self,
275        payload: OutboundPlain<'_>,
276        outgoing_tls: &mut [u8],
277    ) -> Result<usize, EncryptError> {
278        if payload.is_empty() {
279            return Ok(0);
280        }
281
282        let fragments = self
283            .message_fragmenter
284            .fragment_payload(
285                ContentType::ApplicationData,
286                ProtocolVersion::TLSv1_2,
287                payload.clone(),
288            );
289
290        for f in 0..fragments.len() {
291            match self
292                .encrypt_state
293                .pre_encrypt_action(f as u64)
294            {
295                PreEncryptAction::Nothing => {}
296                PreEncryptAction::RefreshOrClose => match self.negotiated_version {
297                    Some(ProtocolVersion::TLSv1_3) => {
298                        // driven by caller, as we don't have the `State` here
299                        self.refresh_traffic_keys_pending = true;
300                    }
301                    _ => {
302                        error!(
303                            "traffic keys exhausted, closing connection to prevent security failure"
304                        );
305                        self.send_close_notify();
306                        return Err(EncryptError::EncryptExhausted);
307                    }
308                },
309                PreEncryptAction::Refuse => {
310                    return Err(EncryptError::EncryptExhausted);
311                }
312            }
313        }
314
315        self.perhaps_write_key_update();
316
317        self.check_required_size(outgoing_tls, fragments)?;
318
319        let fragments = self
320            .message_fragmenter
321            .fragment_payload(
322                ContentType::ApplicationData,
323                ProtocolVersion::TLSv1_2,
324                payload,
325            );
326
327        Ok(self.write_fragments(outgoing_tls, fragments))
328    }
329
330    #[cfg(feature = "std")]
331    pub(crate) fn send_early_plaintext(&mut self, data: &[u8]) -> usize {
332        debug_assert!(self.encrypt_state.is_encrypting());
333
334        // Limit on `sendable_tls` should apply to encrypted data but is enforced
335        // for plaintext data instead which does not include cipher+record overhead.
336        let len = self
337            .sendable_tls
338            .apply_limit(data.len());
339        if len == 0 {
340            // Don't send empty fragments.
341            return 0;
342        }
343
344        self.send_appdata_encrypt(data[..len].into())
345    }
346
347    /// Fragment `m`, encrypt the fragments, and then queue
348    /// the encrypted fragments for sending.
349    fn send_msg_encrypt(&mut self, m: EncodedMessage<Payload<'_>>) {
350        let iter = self
351            .message_fragmenter
352            .fragment_message(&m);
353        for m in iter {
354            self.send_single_fragment(m);
355        }
356    }
357
358    /// Like send_msg_encrypt, but operate on an appdata directly.
359    fn send_appdata_encrypt(&mut self, payload: OutboundPlain<'_>) -> usize {
360        let len = payload.len();
361        let iter = self
362            .message_fragmenter
363            .fragment_payload(
364                ContentType::ApplicationData,
365                ProtocolVersion::TLSv1_2,
366                payload,
367            );
368        for m in iter {
369            self.send_single_fragment(m);
370        }
371
372        len
373    }
374
375    fn send_single_fragment(&mut self, m: EncodedMessage<OutboundPlain<'_>>) {
376        if m.typ == ContentType::Alert {
377            // Alerts are always sendable -- never quashed by a PreEncryptAction.
378            let em = self.encrypt_state.encrypt_outgoing(m);
379            self.queue_tls_message(em);
380            return;
381        }
382
383        match self
384            .encrypt_state
385            .next_pre_encrypt_action()
386        {
387            PreEncryptAction::Nothing => {}
388
389            // Close connection once we start to run out of
390            // sequence space.
391            PreEncryptAction::RefreshOrClose => {
392                match self.negotiated_version {
393                    Some(ProtocolVersion::TLSv1_3) => {
394                        // driven by caller, as we don't have the `State` here
395                        self.refresh_traffic_keys_pending = true;
396                    }
397                    _ => {
398                        error!(
399                            "traffic keys exhausted, closing connection to prevent security failure"
400                        );
401                        self.send_close_notify();
402                        return;
403                    }
404                }
405            }
406
407            // Refuse to wrap counter at all costs.  This
408            // is basically untestable unfortunately.
409            PreEncryptAction::Refuse => {
410                return;
411            }
412        };
413
414        let em = self.encrypt_state.encrypt_outgoing(m);
415        self.queue_tls_message(em);
416    }
417
418    /// Send plaintext application data, fragmenting and
419    /// encrypting it as it goes out.
420    ///
421    /// If internal buffers are too small, this function will not accept
422    /// all the data.
423    #[cfg(feature = "std")]
424    pub(crate) fn buffer_plaintext(
425        &mut self,
426        payload: OutboundPlain<'_>,
427        sendable_plaintext: &mut ChunkVecBuffer,
428    ) -> usize {
429        self.perhaps_write_key_update();
430        if !self.may_send_application_data {
431            // If we haven't completed handshaking, buffer
432            // plaintext to send once we do.
433            return sendable_plaintext.append_limited_copy(payload);
434        }
435
436        // Limit on `sendable_tls` should apply to encrypted data but is enforced
437        // for plaintext data instead which does not include cipher+record overhead.
438        let len = self
439            .sendable_tls
440            .apply_limit(payload.len());
441        if len == 0 {
442            // Don't send empty fragments.
443            return 0;
444        }
445
446        debug_assert!(self.encrypt_state.is_encrypting());
447        self.send_appdata_encrypt(payload.split_at(len).0)
448    }
449
450    pub(crate) fn send_buffered_plaintext(&mut self, plaintext: &mut ChunkVecBuffer) {
451        while let Some(buf) = plaintext.pop() {
452            self.send_appdata_encrypt(buf.as_slice().into());
453        }
454    }
455
456    fn start_traffic(&mut self) {
457        self.may_receive_application_data = true;
458        self.start_outgoing_traffic();
459    }
460
461    fn start_outgoing_traffic(&mut self) {
462        self.may_send_application_data = true;
463        debug_assert!(self.encrypt_state.is_encrypting());
464    }
465
466    // Put m into sendable_tls for writing.
467    fn queue_tls_message(&mut self, m: EncodedMessage<OutboundOpaque>) {
468        self.perhaps_write_key_update();
469        self.sendable_tls.append(m.encode());
470    }
471
472    pub(crate) fn perhaps_write_key_update(&mut self) {
473        if let Some(message) = self.queued_key_update_message.take() {
474            self.sendable_tls.append(message);
475        }
476    }
477
478    /// Send a raw TLS message, fragmenting it if needed.
479    fn send_msg(&mut self, m: Message<'_>, must_encrypt: bool) {
480        if !must_encrypt {
481            let msg = &m.into();
482            let iter = self
483                .message_fragmenter
484                .fragment_message(msg);
485            for m in iter {
486                self.queue_tls_message(m.to_unencrypted_opaque());
487            }
488        } else {
489            self.send_msg_encrypt(m.into());
490        }
491    }
492
493    pub(crate) fn process_alert(&mut self, alert: &AlertMessagePayload) -> Result<(), Error> {
494        // Reject unknown AlertLevels.
495        if let AlertLevel::Unknown(level) = alert.level {
496            return Err(PeerMisbehaved::IllegalAlertLevel(level, alert.description).into());
497        }
498
499        // If we get a CloseNotify, make a note to declare EOF to our
500        // caller.  But do not treat unauthenticated alerts like this.
501        if self.may_receive_application_data && alert.description == AlertDescription::CloseNotify {
502            self.has_received_close_notify = true;
503            return Ok(());
504        }
505
506        // Warnings are nonfatal for TLS1.2, but outlawed in TLS1.3
507        // (except, for no good reason, user_cancelled).
508        let err = Error::AlertReceived(alert.description);
509        if alert.level == AlertLevel::Warning {
510            self.temper_counters
511                .received_warning_alert()?;
512            if self.is_tls13() && alert.description != AlertDescription::UserCanceled {
513                return Err(PeerMisbehaved::IllegalWarningAlert(alert.description).into());
514            }
515
516            // Some implementations send pointless `user_canceled` alerts, don't log them
517            // in release mode (https://bugs.openjdk.org/browse/JDK-8323517).
518            if alert.description != AlertDescription::UserCanceled || cfg!(debug_assertions) {
519                warn!("TLS alert warning received: {alert:?}");
520            }
521
522            return Ok(());
523        }
524
525        Err(err)
526    }
527
528    /// Queues a `close_notify` warning alert to be sent in the next
529    /// [`Connection::write_tls`] call.  This informs the peer that the
530    /// connection is being closed.
531    ///
532    /// Does nothing if any `close_notify` or fatal alert was already sent.
533    ///
534    /// [`Connection::write_tls`]: crate::Connection::write_tls
535    pub fn send_close_notify(&mut self) {
536        if self.has_sent_fatal_alert {
537            return;
538        }
539        debug!("Sending warning alert {:?}", AlertDescription::CloseNotify);
540        self.has_sent_fatal_alert = true;
541        self.has_sent_close_notify = true;
542        self.send_warning_alert_no_log(AlertDescription::CloseNotify);
543    }
544
545    pub(crate) fn eager_send_close_notify(
546        &mut self,
547        outgoing_tls: &mut [u8],
548    ) -> Result<usize, EncryptError> {
549        self.send_close_notify();
550        self.check_required_size(outgoing_tls, [].into_iter())?;
551        Ok(self.write_fragments(outgoing_tls, [].into_iter()))
552    }
553
554    fn send_warning_alert_no_log(&mut self, desc: AlertDescription) {
555        let m = Message::build_alert(AlertLevel::Warning, desc);
556        self.send_msg(m, self.encrypt_state.is_encrypting());
557    }
558
559    fn check_required_size<'a>(
560        &self,
561        outgoing_tls: &[u8],
562        fragments: impl Iterator<Item = EncodedMessage<OutboundPlain<'a>>>,
563    ) -> Result<(), EncryptError> {
564        let mut required_size = self.sendable_tls.len();
565
566        for m in fragments {
567            required_size += m.encoded_len(&self.encrypt_state);
568        }
569
570        if required_size > outgoing_tls.len() {
571            return Err(EncryptError::InsufficientSize(InsufficientSizeError {
572                required_size,
573            }));
574        }
575
576        Ok(())
577    }
578
579    fn write_fragments<'a>(
580        &mut self,
581        outgoing_tls: &mut [u8],
582        fragments: impl Iterator<Item = EncodedMessage<OutboundPlain<'a>>>,
583    ) -> usize {
584        let mut written = 0;
585
586        // Any pre-existing encrypted messages in `sendable_tls` must
587        // be output before encrypting any of the `fragments`.
588        while let Some(message) = self.sendable_tls.pop() {
589            let len = message.len();
590            outgoing_tls[written..written + len].copy_from_slice(&message);
591            written += len;
592        }
593
594        for m in fragments {
595            let em = self
596                .encrypt_state
597                .encrypt_outgoing(m)
598                .encode();
599
600            let len = em.len();
601            outgoing_tls[written..written + len].copy_from_slice(&em);
602            written += len;
603        }
604
605        written
606    }
607
608    pub(crate) fn set_max_fragment_size(&mut self, new: Option<usize>) -> Result<(), Error> {
609        self.message_fragmenter
610            .set_max_fragment_size(new)
611    }
612
613    /// Returns true if the caller should call [`Connection::read_tls`] as soon
614    /// as possible.
615    ///
616    /// If there is pending plaintext data to read with [`Connection::reader`],
617    /// this returns false.  If your application respects this mechanism,
618    /// only one full TLS message will be buffered by rustls.
619    ///
620    /// [`Connection::reader`]: crate::Connection::reader
621    /// [`Connection::read_tls`]: crate::Connection::read_tls
622    pub fn wants_read(&self) -> bool {
623        // We want to read more data all the time, except when we have unprocessed plaintext.
624        // This provides back-pressure to the TCP buffers. We also don't want to read more after
625        // the peer has sent us a close notification.
626        //
627        // In the handshake case we don't have readable plaintext before the handshake has
628        // completed, but also don't want to read if we still have sendable tls.
629        self.received_plaintext.is_empty()
630            && !self.has_received_close_notify
631            && (self.may_send_application_data || self.sendable_tls.is_empty())
632    }
633
634    pub(crate) fn current_io_state(&self) -> IoState {
635        IoState {
636            tls_bytes_to_write: self.sendable_tls.len(),
637            plaintext_bytes_to_read: self.received_plaintext.len(),
638            peer_has_closed: self.has_received_close_notify,
639        }
640    }
641
642    pub(crate) fn ensure_key_update_queued(&mut self) -> bool {
643        if self.queued_key_update_message.is_some() {
644            return false;
645        }
646        let message = EncodedMessage::<Payload<'static>>::from(Message::build_key_update_notify());
647        self.queued_key_update_message = Some(
648            self.encrypt_state
649                .encrypt_outgoing(message.borrow_outbound())
650                .encode(),
651        );
652        true
653    }
654}
655
656impl fmt::Debug for CommonState {
657    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
658        f.debug_struct("CommonState")
659            .finish_non_exhaustive()
660    }
661}
662
663/// Describes which sort of handshake happened.
664#[derive(Debug, PartialEq, Clone, Copy)]
665#[non_exhaustive]
666pub enum HandshakeKind {
667    /// A full handshake.
668    ///
669    /// This is the typical TLS connection initiation process when resumption is
670    /// not yet unavailable, and the initial `ClientHello` was accepted by the server.
671    Full,
672
673    /// A full TLS1.3 handshake, with an extra round-trip for a `HelloRetryRequest`.
674    ///
675    /// The server can respond with a `HelloRetryRequest` if the initial `ClientHello`
676    /// is unacceptable for several reasons, the most likely being if no supported key
677    /// shares were offered by the client.
678    FullWithHelloRetryRequest,
679
680    /// A resumed handshake.
681    ///
682    /// Resumed handshakes involve fewer round trips and less cryptography than
683    /// full ones, but can only happen when the peers have previously done a full
684    /// handshake together, and then remember data about it.
685    Resumed,
686
687    /// A resumed handshake, with an extra round-trip for a `HelloRetryRequest`.
688    ///
689    /// The server can respond with a `HelloRetryRequest` if the initial `ClientHello`
690    /// is unacceptable for several reasons, but this does not prevent the client
691    /// from resuming.
692    ResumedWithHelloRetryRequest,
693}
694
695/// Values of this structure are returned from [`Connection::process_new_packets`]
696/// and tell the caller the current I/O state of the TLS connection.
697///
698/// [`Connection::process_new_packets`]: crate::Connection::process_new_packets
699#[derive(Debug, Eq, PartialEq)]
700pub struct IoState {
701    tls_bytes_to_write: usize,
702    plaintext_bytes_to_read: usize,
703    peer_has_closed: bool,
704}
705
706impl IoState {
707    /// How many bytes could be written by [`Connection::write_tls`] if called
708    /// right now.  A non-zero value implies [`CommonState::wants_write`].
709    ///
710    /// [`Connection::write_tls`]: crate::Connection::write_tls
711    pub fn tls_bytes_to_write(&self) -> usize {
712        self.tls_bytes_to_write
713    }
714
715    /// How many plaintext bytes could be obtained via [`std::io::Read`]
716    /// without further I/O.
717    pub fn plaintext_bytes_to_read(&self) -> usize {
718        self.plaintext_bytes_to_read
719    }
720
721    /// True if the peer has sent us a close_notify alert.  This is
722    /// the TLS mechanism to securely half-close a TLS connection,
723    /// and signifies that the peer will not send any further data
724    /// on this connection.
725    ///
726    /// This is also signalled via returning `Ok(0)` from
727    /// [`std::io::Read`], after all the received bytes have been
728    /// retrieved.
729    pub fn peer_has_closed(&self) -> bool {
730        self.peer_has_closed
731    }
732}
733
734pub(crate) trait State<Side: SideData>: Send + Sync {
735    fn handle<'m>(
736        self: Box<Self>,
737        input: Input<'m>,
738        output: &mut dyn Output,
739    ) -> Result<Box<dyn State<Side>>, Error>;
740
741    fn send_key_update_request(&mut self, _output: &mut dyn Output) -> Result<(), Error> {
742        Err(Error::HandshakeNotComplete)
743    }
744
745    fn handle_decrypt_error(&self) {}
746
747    #[cfg_attr(not(feature = "std"), expect(dead_code))]
748    fn set_resumption_data(&mut self, _resumption_data: &[u8]) -> Result<(), Error> {
749        Err(ApiMisuse::ResumptionDataProvidedTooLate.into())
750    }
751
752    fn into_external_state(
753        self: Box<Self>,
754    ) -> Result<(PartiallyExtractedSecrets, Box<dyn KernelState + 'static>), Error> {
755        Err(Error::HandshakeNotComplete)
756    }
757}
758
759pub(crate) struct Context<'a, Data: SideData> {
760    pub(crate) data: &'a mut Data,
761    /// Store a [`Locator`] initialized from the current receive buffer
762    ///
763    /// Allows received plaintext data to be unborrowed and stored in
764    /// `received_plaintext` for in-place decryption.
765    pub(crate) plaintext_locator: &'a Locator,
766    /// Unborrowed received plaintext data
767    ///
768    /// Set if plaintext data was received.
769    ///
770    /// Plaintext data may be reborrowed using a [`Delocator`] which was
771    /// initialized from the same slice as `plaintext_locator`.
772    pub(crate) received_plaintext: &'a mut Option<UnborrowedPayload>,
773}
774
775impl<Data: SideData> Output for Context<'_, Data> {
776    fn emit(&mut self, ev: Event<'_>) {
777        match ev {
778            Event::ApplicationData(payload) => {
779                // Receive plaintext data [`Payload<'_>`].
780                //
781                // Since [`Context`] does not hold a lifetime to the receive buffer the
782                // passed [`Payload`] will have it's lifetime erased by storing an index
783                // into the receive buffer as an [`UnborrowedPayload`]. This enables the
784                // data to be later reborrowed after it has been decrypted in-place.
785                let previous = self
786                    .received_plaintext
787                    .replace(UnborrowedPayload::unborrow(self.plaintext_locator, payload));
788                debug_assert!(previous.is_none(), "overwrote plaintext data");
789            }
790            Event::ApplicationProtocol(protocol) => {
791                self.data.alpn_protocol =
792                    Some(ApplicationProtocol::from(protocol.as_ref()).to_owned())
793            }
794            Event::CipherSuite(suite) => self.data.suite = Some(suite),
795            Event::EarlyData(_) | Event::EarlyApplicationData(_) => self.data.emit(ev),
796            Event::EarlyExporter(exporter) => self.data.early_exporter = Some(exporter),
797            Event::EchStatus(_) => self.data.emit(ev),
798            Event::EncryptMessage(m) => match self.data.protocol {
799                Protocol::Tcp => self.data.send_msg(m, true),
800                Protocol::Quic(_) => self.data.quic.send_msg(m, true),
801            },
802            Event::Exporter(exporter) => self.data.exporter = Some(exporter),
803            Event::HandshakeKind(hk) => {
804                assert!(self.data.handshake_kind.is_none());
805                self.data.handshake_kind = Some(hk);
806            }
807            Event::KeyExchangeGroup(kxg) => {
808                assert!(self.data.negotiated_kx_group.is_none());
809                self.data.negotiated_kx_group = Some(kxg);
810            }
811            Event::MaybeKeyUpdateRequest(ks) => {
812                if self.data.ensure_key_update_queued() {
813                    ks.update_encrypter_for_key_update(self);
814                }
815            }
816            Event::MessageDecrypter { decrypter, proof } => self
817                .data
818                .decrypt_state
819                .set_message_decrypter(decrypter, &proof),
820            Event::MessageDecrypterWithTrialDecryption {
821                decrypter,
822                max_length,
823                proof,
824            } => self
825                .data
826                .decrypt_state
827                .set_message_decrypter_with_trial_decryption(decrypter, max_length, &proof),
828            Event::MessageEncrypter { encrypter, limit } => self
829                .data
830                .encrypt_state
831                .set_message_encrypter(encrypter, limit),
832            Event::QuicEarlySecret(sec) => self.data.quic.early_secret = sec,
833            Event::QuicHandshakeSecrets(sec) => self.data.quic.hs_secrets = Some(sec),
834            Event::QuicTrafficSecrets(sec) => self.data.quic.traffic_secrets = Some(sec),
835            Event::QuicTransportParameters(params) => self.data.quic.params = Some(params),
836            Event::PeerIdentity(identity) => self.data.peer_identity = Some(identity),
837            Event::PlainMessage(m) => match self.data.protocol {
838                Protocol::Tcp => self.data.send_msg(m, false),
839                Protocol::Quic(_) => self.data.quic.send_msg(m, false),
840            },
841            Event::ProtocolVersion(ver) => self.data.negotiated_version = Some(ver),
842            Event::ReceivedServerName(_) => self.data.emit(ev),
843            Event::ReceivedTicket => {
844                self.data.tls13_tickets_received = self
845                    .data
846                    .tls13_tickets_received
847                    .saturating_add(1)
848            }
849            Event::ResumptionData(_) => self.data.emit(ev),
850            Event::StartOutgoingTraffic => self.data.start_outgoing_traffic(),
851            Event::StartTraffic => self.data.start_traffic(),
852        }
853    }
854}
855
856pub(crate) struct Input<'a> {
857    pub(crate) message: Message<'a>,
858    pub(crate) aligned_handshake: Option<HandshakeAlignedProof>,
859}
860
861impl Input<'_> {
862    // Changing the keys must not span any fragmented handshake
863    // messages.  Otherwise the defragmented messages will have
864    // been protected with two different record layer protections,
865    // which is illegal.  Not mentioned in RFC.
866    pub(crate) fn check_aligned_handshake(&self) -> Result<HandshakeAlignedProof, Error> {
867        self.aligned_handshake
868            .ok_or_else(|| PeerMisbehaved::KeyEpochWithPendingFragment.into())
869    }
870}
871
872/// The route for handshake state machine to surface determinations about the connection.
873pub(crate) trait Output {
874    fn emit(&mut self, ev: Event<'_>);
875}
876
877/// The set of events output by the low-level handshake state machine.
878pub(crate) enum Event<'a> {
879    ApplicationData(Payload<'a>),
880    ApplicationProtocol(ApplicationProtocol<'a>),
881    CipherSuite(SupportedCipherSuite),
882    EarlyApplicationData(Payload<'a>),
883    EarlyData(EarlyDataEvent),
884    EarlyExporter(Box<dyn Exporter>),
885    EchStatus(EchStatus),
886    EncryptMessage(Message<'a>),
887    Exporter(Box<dyn Exporter>),
888    HandshakeKind(HandshakeKind),
889    KeyExchangeGroup(&'static dyn SupportedKxGroup),
890    MaybeKeyUpdateRequest(&'a mut KeyScheduleTrafficSend),
891    MessageDecrypter {
892        decrypter: Box<dyn MessageDecrypter>,
893        proof: HandshakeAlignedProof,
894    },
895    MessageDecrypterWithTrialDecryption {
896        decrypter: Box<dyn MessageDecrypter>,
897        max_length: usize,
898        proof: HandshakeAlignedProof,
899    },
900    MessageEncrypter {
901        encrypter: Box<dyn MessageEncrypter>,
902        limit: u64,
903    },
904    PeerIdentity(Identity<'static>),
905    PlainMessage(Message<'a>),
906    ProtocolVersion(ProtocolVersion),
907    QuicEarlySecret(Option<OkmBlock>),
908    QuicHandshakeSecrets(quic::Secrets),
909    QuicTrafficSecrets(quic::Secrets),
910    QuicTransportParameters(Vec<u8>),
911    ReceivedServerName(Option<DnsName<'static>>),
912    ReceivedTicket,
913    ResumptionData(Vec<u8>),
914    /// Mark the connection as ready to send application data.
915    StartOutgoingTraffic,
916    /// Mark the connection as ready to send and receive application data.
917    StartTraffic,
918}
919
920pub(crate) enum EarlyDataEvent {
921    /// server: we accepted an early_data offer
922    Accepted,
923    /// client: declares the maximum amount of early data that can be sent
924    Enable(usize),
925    /// client: early data can now be sent using the record layer as normal
926    Start,
927    /// client: early data phase has closed after sending EndOfEarlyData
928    Finished,
929    /// client: the server rejected our request for early data
930    Rejected,
931}
932
933/// Lifetime-erased equivalent to [`Payload`]
934///
935/// Stores an index into [`Payload`] buffer enabling in-place decryption
936/// without holding a lifetime to the receive buffer.
937pub(crate) enum UnborrowedPayload {
938    Unborrowed(Range<usize>),
939    Owned(Vec<u8>),
940}
941
942impl UnborrowedPayload {
943    /// Convert [`Payload`] into [`UnborrowedPayload`] which stores a range
944    /// into the [`Payload`] slice without borrowing such that it can be later
945    /// reborrowed.
946    ///
947    /// # Panics
948    ///
949    /// Passed [`Locator`] must have been created from the same slice which
950    /// contains the payload.
951    pub(crate) fn unborrow(locator: &Locator, payload: Payload<'_>) -> Self {
952        match payload {
953            Payload::Borrowed(payload) => Self::Unborrowed(locator.locate(payload)),
954            Payload::Owned(payload) => Self::Owned(payload),
955        }
956    }
957
958    /// Convert [`UnborrowedPayload`] back into [`Payload`]
959    ///
960    /// # Panics
961    ///
962    /// Passed [`Delocator`] must have been created from the same slice that
963    /// [`UnborrowedPayload`] was originally unborrowed from.
964    pub(crate) fn reborrow<'b>(self, delocator: &Delocator<'b>) -> Payload<'b> {
965        match self {
966            Self::Unborrowed(range) => Payload::Borrowed(delocator.slice_from_range(&range)),
967            Self::Owned(payload) => Payload::Owned(payload),
968        }
969    }
970}
971
972/// Side of the connection.
973#[expect(clippy::exhaustive_enums)]
974#[derive(Clone, Copy, Debug, PartialEq)]
975pub enum Side {
976    /// A client initiates the connection.
977    Client,
978    /// A server waits for a client to connect.
979    Server,
980}
981
982#[derive(Copy, Clone, Eq, PartialEq, Debug)]
983pub(crate) enum Protocol {
984    /// TCP-TLS, standardized in RFC5246 and RFC8446
985    Tcp,
986    /// QUIC, standardized in RFC9001
987    #[cfg_attr(not(feature = "std"), expect(dead_code))]
988    Quic(quic::Version),
989}
990
991impl Protocol {
992    pub(crate) fn is_quic(&self) -> bool {
993        matches!(self, Self::Quic(_))
994    }
995}
996
997/// Tracking technically-allowed protocol actions
998/// that we limit to avoid denial-of-service vectors.
999struct TemperCounters {
1000    allowed_warning_alerts: u8,
1001    allowed_renegotiation_requests: u8,
1002    allowed_middlebox_ccs: u8,
1003}
1004
1005impl TemperCounters {
1006    fn received_warning_alert(&mut self) -> Result<(), Error> {
1007        match self.allowed_warning_alerts {
1008            0 => Err(PeerMisbehaved::TooManyWarningAlertsReceived.into()),
1009            _ => {
1010                self.allowed_warning_alerts -= 1;
1011                Ok(())
1012            }
1013        }
1014    }
1015
1016    fn received_renegotiation_request(&mut self) -> Result<(), Error> {
1017        match self.allowed_renegotiation_requests {
1018            0 => Err(PeerMisbehaved::TooManyRenegotiationRequests.into()),
1019            _ => {
1020                self.allowed_renegotiation_requests -= 1;
1021                Ok(())
1022            }
1023        }
1024    }
1025
1026    fn received_tls13_change_cipher_spec(&mut self) -> Result<(), Error> {
1027        match self.allowed_middlebox_ccs {
1028            0 => Err(PeerMisbehaved::IllegalMiddleboxChangeCipherSpec.into()),
1029            _ => {
1030                self.allowed_middlebox_ccs -= 1;
1031                Ok(())
1032            }
1033        }
1034    }
1035}
1036
1037impl Default for TemperCounters {
1038    fn default() -> Self {
1039        Self {
1040            // cf. BoringSSL `kMaxWarningAlerts`
1041            // <https://github.com/google/boringssl/blob/dec5989b793c56ad4dd32173bd2d8595ca78b398/ssl/tls_record.cc#L137-L139>
1042            allowed_warning_alerts: 4,
1043
1044            // we rebuff renegotiation requests with a `NoRenegotiation` warning alerts.
1045            // a second request after this is fatal.
1046            allowed_renegotiation_requests: 1,
1047
1048            // At most two CCS are allowed: one after each ClientHello (recall a second
1049            // ClientHello happens after a HelloRetryRequest).
1050            //
1051            // note BoringSSL allows up to 32.
1052            allowed_middlebox_ccs: 2,
1053        }
1054    }
1055}
1056
1057pub(crate) struct TrafficTemperCounters {
1058    allowed_key_update_requests: u8,
1059}
1060
1061impl TrafficTemperCounters {
1062    pub(crate) fn received_key_update_request(&mut self) -> Result<(), Error> {
1063        match self.allowed_key_update_requests {
1064            0 => Err(PeerMisbehaved::TooManyKeyUpdateRequests.into()),
1065            _ => {
1066                self.allowed_key_update_requests -= 1;
1067                Ok(())
1068            }
1069        }
1070    }
1071
1072    pub(crate) fn received_app_data(&mut self) {
1073        self.allowed_key_update_requests = Self::INITIAL_KEY_UPDATE_REQUESTS;
1074    }
1075
1076    // cf. BoringSSL `kMaxKeyUpdates`
1077    // <https://github.com/google/boringssl/blob/dec5989b793c56ad4dd32173bd2d8595ca78b398/ssl/tls13_both.cc#L35-L38>
1078    const INITIAL_KEY_UPDATE_REQUESTS: u8 = 32;
1079}
1080
1081impl Default for TrafficTemperCounters {
1082    fn default() -> Self {
1083        Self {
1084            allowed_key_update_requests: Self::INITIAL_KEY_UPDATE_REQUESTS,
1085        }
1086    }
1087}
1088
1089pub(crate) struct HandshakeFlight<'a, const TLS13: bool> {
1090    pub(crate) transcript: &'a mut HandshakeHash,
1091    body: Vec<u8>,
1092}
1093
1094impl<'a, const TLS13: bool> HandshakeFlight<'a, TLS13> {
1095    pub(crate) fn new(transcript: &'a mut HandshakeHash) -> Self {
1096        Self {
1097            transcript,
1098            body: Vec::new(),
1099        }
1100    }
1101
1102    pub(crate) fn add(&mut self, hs: HandshakeMessagePayload<'_>) {
1103        let start_len = self.body.len();
1104        hs.encode(&mut self.body);
1105        self.transcript
1106            .add(&self.body[start_len..]);
1107    }
1108
1109    pub(crate) fn finish(self, output: &mut dyn Output) {
1110        let m = Message {
1111            version: match TLS13 {
1112                true => ProtocolVersion::TLSv1_3,
1113                false => ProtocolVersion::TLSv1_2,
1114            },
1115            payload: MessagePayload::HandshakeFlight(Payload::new(self.body)),
1116        };
1117
1118        output.emit(match TLS13 {
1119            true => Event::EncryptMessage(m),
1120            false => Event::PlainMessage(m),
1121        });
1122    }
1123}
1124
1125pub(crate) type HandshakeFlightTls12<'a> = HandshakeFlight<'a, false>;
1126pub(crate) type HandshakeFlightTls13<'a> = HandshakeFlight<'a, true>;
1127
1128const DEFAULT_RECEIVED_PLAINTEXT_LIMIT: usize = 16 * 1024;
1129pub(crate) const DEFAULT_BUFFER_LIMIT: usize = 64 * 1024;