Skip to content

Commit 550afb3

Browse files
benlcbBenjamin Barzen
andauthored
feat(s2n-quic-dc): path_secrets_ready callback can cancel handshake (#2648)
Change return type of make_application_data callback registered by application to Result<...>, and canceling the handshake, propagating an error message when an Error is returned. Co-authored-by: Benjamin Barzen <[email protected]>
1 parent 1d44b67 commit 550afb3

File tree

6 files changed

+43
-19
lines changed

6 files changed

+43
-19
lines changed

dc/s2n-quic-dc/src/path/secret/map.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl Map {
215215
receiver::State::new(),
216216
dc::testing::TEST_APPLICATION_PARAMS,
217217
dc::testing::TEST_REHANDSHAKE_PERIOD,
218-
Arc::new(()),
218+
None,
219219
);
220220
let entry = Arc::new(entry);
221221
provider.store.test_insert(entry);
@@ -277,7 +277,7 @@ impl Map {
277277
super::receiver::State::new(),
278278
params,
279279
dc::testing::TEST_REHANDSHAKE_PERIOD,
280-
Arc::new(()),
280+
None,
281281
);
282282
let entry = Arc::new(entry);
283283
map.store.test_insert(entry);
@@ -297,7 +297,11 @@ impl Map {
297297
pub fn register_make_application_data(
298298
&self,
299299
cb: Box<
300-
dyn Fn(&dyn s2n_quic_core::crypto::tls::TlsSession) -> ApplicationData + Send + Sync,
300+
dyn Fn(
301+
&dyn s2n_quic_core::crypto::tls::TlsSession,
302+
) -> Result<Option<ApplicationData>, &'static str>
303+
+ Send
304+
+ Sync,
301305
>,
302306
) {
303307
self.store.register_make_application_data(cb);

dc/s2n-quic-dc/src/path/secret/map/entry.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub struct Entry {
4545
// we store this as a u8 to allow the cleaner to separately "take" accessed for id and addr
4646
// maps while not having two writes and wasting an extra byte of space.
4747
accessed: AtomicU8,
48-
application_data: ApplicationData,
48+
application_data: Option<ApplicationData>,
4949
}
5050

5151
impl SizeOf for Entry {
@@ -73,6 +73,12 @@ impl SizeOf for Entry {
7373
}
7474
}
7575

76+
impl SizeOf for Option<ApplicationData> {
77+
fn size(&self) -> usize {
78+
std::mem::size_of::<ApplicationData>()
79+
}
80+
}
81+
7682
impl SizeOf for ApplicationData {
7783
fn size(&self) -> usize {
7884
std::mem::size_of_val(self)
@@ -91,7 +97,7 @@ impl Entry {
9197
parameters: dc::ApplicationParams,
9298
// FIXME: remove unused parameter
9399
_: Duration,
94-
application_data: ApplicationData,
100+
application_data: Option<ApplicationData>,
95101
) -> Self {
96102
// clamp max datagram size to a well-known value
97103
parameters
@@ -130,7 +136,7 @@ impl Entry {
130136
receiver,
131137
dc::testing::TEST_APPLICATION_PARAMS,
132138
dc::testing::TEST_REHANDSHAKE_PERIOD,
133-
Arc::new(()),
139+
None,
134140
))
135141
}
136142

@@ -273,7 +279,7 @@ impl Entry {
273279
self.secret.control_sealer()
274280
}
275281

276-
pub fn application_data(&self) -> &ApplicationData {
282+
pub fn application_data(&self) -> &Option<ApplicationData> {
277283
&self.application_data
278284
}
279285
}

dc/s2n-quic-dc/src/path/secret/map/handshake.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ impl dc::Path for HandshakingPath {
8484
&mut self,
8585
session: &impl s2n_quic_core::crypto::tls::TlsSession,
8686
) -> Result<Vec<s2n_quic_core::stateless_reset::Token>, s2n_quic_core::transport::Error> {
87-
self.application_data = Some(self.map.store.application_data(session));
87+
match self.map.store.application_data(session) {
88+
Ok(application_data) => {
89+
self.application_data = application_data;
90+
}
91+
Err(msg) => {
92+
return Err(s2n_quic_core::transport::Error::APPLICATION_ERROR.with_reason(msg));
93+
}
94+
};
8895

8996
let mut material = Zeroizing::new([0; TLS_EXPORTER_LENGTH]);
9097
session
@@ -138,7 +145,7 @@ impl dc::Path for HandshakingPath {
138145
receiver,
139146
self.parameters.clone(),
140147
self.map.store.rehandshake_period(),
141-
self.application_data.take().unwrap(),
148+
self.application_data.take(),
142149
);
143150
let entry = Arc::new(entry);
144151
self.entry = Some(entry.clone());

dc/s2n-quic-dc/src/path/secret/map/state.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,14 @@ where
219219
mk_application_data: RwLock<
220220
Option<
221221
Box<
222-
dyn Fn(&dyn s2n_quic_core::crypto::tls::TlsSession) -> ApplicationData
222+
dyn Fn(
223+
&dyn s2n_quic_core::crypto::tls::TlsSession,
224+
) -> Result<Option<ApplicationData>, &'static str>
223225
+ Send
224226
+ Sync,
225227
>,
226228
>,
227229
>,
228-
229-
dummy_application_data: ApplicationData,
230230
}
231231

232232
// Share control sockets -- we only send on these so it doesn't really matter if there's only one
@@ -288,7 +288,6 @@ where
288288
subscriber,
289289
request_handshake: RwLock::new(None),
290290
mk_application_data: RwLock::new(None),
291-
dummy_application_data: Arc::new(()),
292291
};
293292

294293
// Growing to double our maximum inserted entries should ensure that we never grow again, see:
@@ -677,7 +676,11 @@ where
677676
fn register_make_application_data(
678677
&self,
679678
cb: Box<
680-
dyn Fn(&dyn s2n_quic_core::crypto::tls::TlsSession) -> ApplicationData + Send + Sync,
679+
dyn Fn(
680+
&dyn s2n_quic_core::crypto::tls::TlsSession,
681+
) -> Result<Option<ApplicationData>, &'static str>
682+
+ Send
683+
+ Sync,
681684
>,
682685
) {
683686
// FIXME: Maybe panic if already initialized?
@@ -891,15 +894,15 @@ where
891894
fn application_data(
892895
&self,
893896
session: &dyn s2n_quic_core::crypto::tls::TlsSession,
894-
) -> ApplicationData {
897+
) -> Result<Option<ApplicationData>, &'static str> {
895898
if let Some(ctxt) = &*self
896899
.mk_application_data
897900
.read()
898901
.unwrap_or_else(|e| e.into_inner())
899902
{
900903
(ctxt)(session)
901904
} else {
902-
self.dummy_application_data.clone()
905+
Ok(None)
903906
}
904907
}
905908
}

dc/s2n-quic-dc/src/path/secret/map/state/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl Model {
139139
receiver::State::new(),
140140
dc::testing::TEST_APPLICATION_PARAMS,
141141
dc::testing::TEST_REHANDSHAKE_PERIOD,
142-
Arc::new(()),
142+
None,
143143
)));
144144

145145
self.invariants.insert(Invariant::ContainsIp(ip));

dc/s2n-quic-dc/src/path/secret/map/store.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,16 @@ pub trait Store: 'static + Send + Sync {
106106
fn register_make_application_data(
107107
&self,
108108
cb: Box<
109-
dyn Fn(&dyn s2n_quic_core::crypto::tls::TlsSession) -> ApplicationData + Send + Sync,
109+
dyn Fn(
110+
&dyn s2n_quic_core::crypto::tls::TlsSession,
111+
) -> Result<Option<ApplicationData>, &'static str>
112+
+ Send
113+
+ Sync,
110114
>,
111115
);
112116

113117
fn application_data(
114118
&self,
115119
session: &dyn s2n_quic_core::crypto::tls::TlsSession,
116-
) -> ApplicationData;
120+
) -> Result<Option<ApplicationData>, &'static str>;
117121
}

0 commit comments

Comments
 (0)