Skip to content

Commit 2b82b81

Browse files
committed
unbuffered: update TLS 1.3 traffic keys before exhausting them
Previously, when a call to `WriteTraffic::encrypt()` reached the confidentiality limit of the traffic keys, the key update was only sent at the start of the next call, so the current keys were used for more records than allowed. Instead, request the key update before encrypting application data that would take the last sequence number allowed by the current keys, so that the key update takes that sequence number. Like the previous commit, this addresses #3302 for the unbuffered API.
1 parent 74343e7 commit 2b82b81

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎rustls/src/common_state.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,33 @@ impl CommonState {
233233
}
234234
}
235235

236+
/// Ask for a TLS 1.3 key update before encrypting `payload`, if one of its records would
237+
/// take the last sequence number allowed by the current keys.
238+
///
239+
/// The key update then takes that sequence number instead.
240+
pub(crate) fn request_key_update_for(&mut self, payload: OutboundChunks<'_>) {
241+
let records = self
242+
.message_fragmenter
243+
.fragment_payload(
244+
ContentType::ApplicationData,
245+
ProtocolVersion::TLSv1_2,
246+
payload,
247+
)
248+
.len();
249+
250+
if records > 0
251+
&& self.negotiated_version == Some(ProtocolVersion::TLSv1_3)
252+
&& self.record_layer.write_seq() > 0
253+
&& self
254+
.record_layer
255+
.pre_encrypt_action(records as u64)
256+
!= PreEncryptAction::Nothing
257+
{
258+
// driven by caller, as we don't have the `State` here
259+
self.refresh_traffic_keys_pending = true;
260+
}
261+
}
262+
236263
pub(crate) fn write_plaintext(
237264
&mut self,
238265
payload: OutboundChunks<'_>,

‎rustls/src/conn/unbuffered.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,10 @@ impl<Data> WriteTraffic<'_, Data> {
447447
application_data: &[u8],
448448
outgoing_tls: &mut [u8],
449449
) -> Result<usize, EncryptError> {
450+
self.conn
451+
.core
452+
.common_state
453+
.request_key_update_for(application_data.into());
450454
self.conn
451455
.core
452456
.maybe_refresh_traffic_keys();

‎rustls/tests/unbuffered.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ fn refresh_traffic_keys_automatically() {
746746

747747
const KEY_UPDATE_SIZE: usize = encrypted_size(5);
748748
const CONFIDENTIALITY_LIMIT: usize = 1024;
749-
const CONFIDENTIALITY_LIMIT_PLUS_ONE: usize = CONFIDENTIALITY_LIMIT + 1;
749+
const CONFIDENTIALITY_LIMIT_MINUS_ONE: usize = CONFIDENTIALITY_LIMIT - 1;
750750

751751
let client_config = finish_client_config(
752752
KeyType::Rsa2048,
@@ -786,8 +786,10 @@ fn refresh_traffic_keys_automatically() {
786786
assert_eq!(
787787
used,
788788
match i {
789-
// The key_update message triggered by write N appears in write N+1
790-
CONFIDENTIALITY_LIMIT_PLUS_ONE =>
789+
// the key_update message takes the last record allowed under
790+
// the initial key, so it is sent before the message that would
791+
// otherwise take that record
792+
CONFIDENTIALITY_LIMIT_MINUS_ONE =>
791793
KEY_UPDATE_SIZE + encrypted_size(message.len()),
792794
_ => encrypted_size(message.len()),
793795
}

0 commit comments

Comments
 (0)