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
29pub 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 pub(crate) has_sent_close_notify: bool,
47 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 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 pub fn wants_write(&self) -> bool {
107 !self.sendable_tls.is_empty()
108 }
109
110 pub fn is_handshaking(&self) -> bool {
118 !(self.may_send_application_data && self.may_receive_application_data)
119 }
120
121 pub fn peer_identity(&self) -> Option<&Identity<'static>> {
130 self.peer_identity.as_ref()
131 }
132
133 pub fn alpn_protocol(&self) -> Option<&[u8]> {
139 self.alpn_protocol
140 .as_ref()
141 .map(AsRef::as_ref)
142 }
143
144 pub fn negotiated_cipher_suite(&self) -> Option<SupportedCipherSuite> {
148 self.suite
149 }
150
151 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 pub fn protocol_version(&self) -> Option<ProtocolVersion> {
171 self.negotiated_version
172 }
173
174 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 if msg.typ == ContentType::ChangeCipherSpec
199 && !self.may_receive_application_data
200 && self.is_tls13()
201 {
202 if !msg.is_valid_ccs() {
203 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 let msg = Message::try_from(&msg)?;
217
218 if let MessagePayload::Alert(alert) = &msg.payload {
220 self.process_alert(alert)?;
221 return Ok(state);
222 }
223
224 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 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 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 let len = self
337 .sendable_tls
338 .apply_limit(data.len());
339 if len == 0 {
340 return 0;
342 }
343
344 self.send_appdata_encrypt(data[..len].into())
345 }
346
347 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 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 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 PreEncryptAction::RefreshOrClose => {
392 match self.negotiated_version {
393 Some(ProtocolVersion::TLSv1_3) => {
394 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 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 #[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 return sendable_plaintext.append_limited_copy(payload);
434 }
435
436 let len = self
439 .sendable_tls
440 .apply_limit(payload.len());
441 if len == 0 {
442 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 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 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 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 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 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 if let AlertLevel::Unknown(level) = alert.level {
542 return Err(PeerMisbehaved::IllegalAlertLevel(level, alert.description).into());
543 }
544
545 if self.may_receive_application_data && alert.description == AlertDescription::CloseNotify {
548 self.has_received_close_notify = true;
549 return Ok(());
550 }
551
552 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 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 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 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 pub fn wants_read(&self) -> bool {
669 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#[derive(Debug, PartialEq, Clone, Copy)]
718#[non_exhaustive]
719pub enum HandshakeKind {
720 Full,
725
726 FullWithHelloRetryRequest,
732
733 Resumed,
739
740 ResumedWithHelloRetryRequest,
746}
747
748#[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 pub fn tls_bytes_to_write(&self) -> usize {
765 self.tls_bytes_to_write
766 }
767
768 pub fn plaintext_bytes_to_read(&self) -> usize {
771 self.plaintext_bytes_to_read
772 }
773
774 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 pub(crate) plaintext_locator: &'a Locator,
815 pub(crate) received_plaintext: &'a mut Option<UnborrowedPayload>,
822 pub(crate) sendable_plaintext: Option<&'a mut ChunkVecBuffer>,
825}
826
827impl<'a, Data> Context<'a, Data> {
828 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
845pub(crate) enum UnborrowedPayload {
850 Unborrowed(Range<usize>),
851 Owned(Vec<u8>),
852}
853
854impl UnborrowedPayload {
855 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 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#[expect(clippy::exhaustive_enums)]
886#[derive(Clone, Copy, Debug, PartialEq)]
887pub enum Side {
888 Client,
890 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
909struct 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 const INITIAL_KEY_UPDATE_REQUESTS: u8 = 32;
966}
967
968impl Default for TemperCounters {
969 fn default() -> Self {
970 Self {
971 allowed_warning_alerts: 4,
974
975 allowed_renegotiation_requests: 1,
978
979 allowed_key_update_requests: Self::INITIAL_KEY_UPDATE_REQUESTS,
980
981 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;