rustls/
common_state.rs

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