Debian Codegraph @

third_party/rust/neqo-transport/src/connection/tests/keys.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 use neqo_common::{qdebug, Datagram};
8 use test_fixture::now;
9
10 use super::{
11 super::{
12 super::{CloseReason, ERROR_AEAD_LIMIT_REACHED},
13 Connection, ConnectionParameters, Error, Output, State, StreamType,
14 },
15 connect, connect_force_idle, default_client, default_server, maybe_authenticate,
16 send_and_receive, send_something, AT_LEAST_PTO,
17 };
18 use crate::{
19 crypto::{OVERWRITE_INVOCATIONS, UPDATE_WRITE_KEYS_AT},
20 packet::PacketNumber,
21 };
22
23 fn check_discarded(
24 peer: &mut Connection,
25 pkt: &Datagram,
26 response: bool,
27 dropped: usize,
28 dups: usize,
29 ) {
30 // Make sure to flush any saved datagrams before doing this.
31 drop(peer.process_output(now()));
32
33 let before = peer.stats();
34 let out = peer.process(Some(pkt.clone()), now());
35 assert_eq!(out.as_dgram_ref().is_some(), response);
36 let after = peer.stats();
37 assert_eq!(dropped, after.dropped_rx - before.dropped_rx);
38 assert_eq!(dups, after.dups_rx - before.dups_rx);
39 }
40
41 fn assert_update_blocked(c: &mut Connection) {
42 assert_eq!(
43 c.initiate_key_update().unwrap_err(),
44 Error::KeyUpdateBlocked
45 );
46 }
47
48 fn overwrite_invocations(n: PacketNumber) {
49 OVERWRITE_INVOCATIONS.with(|v| {
50 *v.borrow_mut() = Some(n);
51 });
52 }
53
54 #[test]
55 fn discarded_initial_keys() {
56 qdebug!("---- client: generate CH");
57 let mut client = default_client();
58 let init_pkt_c = client.process_output(now()).dgram();
59 let init_pkt_c2 = client.process_output(now()).dgram();
60 assert!(init_pkt_c.is_some() && init_pkt_c2.is_some());
61 assert_eq!(init_pkt_c.as_ref().unwrap().len(), client.plpmtu());
62 assert_eq!(init_pkt_c2.as_ref().unwrap().len(), client.plpmtu());
63
64 qdebug!("---- server: CH -> SH, EE, CERT, CV, FIN");
65 let mut server = default_server();
66 server.process_input(init_pkt_c.clone().unwrap(), now());
67 let init_pkt_s = server.process(init_pkt_c2, now()).dgram();
68 assert!(init_pkt_s.is_some());
69
70 qdebug!("---- client: cert verification");
71 let out = client.process(init_pkt_s.clone(), now()).dgram();
72 assert!(out.is_some());
73
74 let out = server.process(out, now()).dgram();
75 client.process_input(out.unwrap(), now());
76
77 // The client has received a handshake packet. It will remove the Initial keys.
78 // We will check this by processing init_pkt_s a second time.
79 // The initial packet should be dropped. The packet contains a Handshake packet as well, which
80 // will be marked as dup. And it will contain padding, which will be "dropped".
81 // The client will generate a Handshake packet here to avoid stalling.
82 check_discarded(&mut client, &init_pkt_s.unwrap(), true, 1, 0);
83
84 assert!(maybe_authenticate(&mut client));
85
86 // The server has not removed the Initial keys yet, because it has not yet received a Handshake
87 // packet from the client.
88 // We will check this by processing init_pkt_c a second time.
89 // The dropped packet is padding. The Initial packet has been mark dup.
90 check_discarded(&mut server, &init_pkt_c.clone().unwrap(), false, 1, 1);
91
92 qdebug!("---- client: SH..FIN -> FIN");
93 let out = client.process_output(now()).dgram();
94 assert!(out.is_some());
95
96 // The server will process the first Handshake packet.
97 // After this the Initial keys will be dropped.
98 let out = server.process(out, now()).dgram();
99 assert!(out.is_some());
100
101 // Check that the Initial keys are dropped at the server
102 // We will check this by processing init_pkt_c a third time.
103 // The Initial packet has been dropped and padding that follows it.
104 // There is no dups, everything has been dropped.
105 check_discarded(&mut server, &init_pkt_c.unwrap(), false, 1, 0);
106 }
107
108 #[test]
109 fn key_update_client() {
110 let mut client = default_client();
111 let mut server = default_server();
112 connect_force_idle(&mut client, &mut server);
113 let mut now = now();
114
115 assert_eq!(client.get_epochs(), (Some(3), Some(3))); // (write, read)
116 assert_eq!(server.get_epochs(), (Some(3), Some(3)));
117
118 assert!(client.initiate_key_update().is_ok());
119 assert_update_blocked(&mut client);
120
121 // Initiating an update should only increase the write epoch.
122 let idle_timeout = ConnectionParameters::default().get_idle_timeout();
123 assert_eq!(Output::Callback(idle_timeout), client.process_output(now));
124 assert_eq!(client.get_epochs(), (Some(4), Some(3)));
125
126 // Send something to propagate the update.
127 // Note that the server will acknowledge immediately when RTT is zero.
128 assert!(send_and_receive(&mut client, &mut server, now).is_some());
129
130 // The server should now be waiting to discharge read keys.
131 assert_eq!(server.get_epochs(), (Some(4), Some(3)));
132 let res = server.process_output(now);
133 if let Output::Callback(t) = res {
134 assert!(t < idle_timeout);
135 } else {
136 panic!("server should now be waiting to clear keys");
137 }
138
139 // Without having had time to purge old keys, more updates are blocked.
140 // The spec would permits it at this point, but we are more conservative.
141 assert_update_blocked(&mut client);
142 // The server can't update until it receives an ACK for a packet.
143 assert_update_blocked(&mut server);
144
145 // Waiting now for at least a PTO should cause the server to drop old keys.
146 // But at this point the client hasn't received a key update from the server.
147 // It will be stuck with old keys.
148 now += AT_LEAST_PTO;
149 let dgram = client.process_output(now).dgram();
150 assert!(dgram.is_some()); // Drop this packet.
151 assert_eq!(client.get_epochs(), (Some(4), Some(3)));
152 drop(server.process_output(now));
153 assert_eq!(server.get_epochs(), (Some(4), Some(4)));
154
155 // Even though the server has updated, it hasn't received an ACK yet.
156 assert_update_blocked(&mut server);
157
158 // Now get an ACK from the server.
159 // The previous PTO packet (see above) was dropped, so we should get an ACK here.
160 let dgram = send_and_receive(&mut client, &mut server, now);
161 assert!(dgram.is_some());
162 let res = client.process(dgram, now);
163 // This is the first packet that the client has received from the server
164 // with new keys, so its read timer just started.
165 if let Output::Callback(t) = res {
166 assert!(t < ConnectionParameters::default().get_idle_timeout());
167 } else {
168 panic!("client should now be waiting to clear keys");
169 }
170
171 assert_update_blocked(&mut client);
172 assert_eq!(client.get_epochs(), (Some(4), Some(3)));
173 // The server can't update until it gets something from the client.
174 assert_update_blocked(&mut server);
175
176 now += AT_LEAST_PTO;
177 drop(client.process_output(now));
178 assert_eq!(client.get_epochs(), (Some(4), Some(4)));
179 }
180
181 #[test]
182 fn key_update_consecutive() {
183 let mut client = default_client();
184 let mut server = default_server();
185 connect(&mut client, &mut server);
186 let now = now();
187
188 assert!(server.initiate_key_update().is_ok());
189 assert_eq!(server.get_epochs(), (Some(4), Some(3)));
190
191 // Server sends something.
192 // Send twice and drop the first to induce an ACK from the client.
193 drop(send_something(&mut server, now)); // Drop this.
194
195 // Another packet from the server will cause the client to ACK and update keys.
196 let dgram = send_and_receive(&mut server, &mut client, now);
197 assert!(dgram.is_some());
198 assert_eq!(client.get_epochs(), (Some(4), Some(3)));
199
200 // Have the server process the ACK.
201 if let Output::Callback(_) = server.process(dgram, now) {
202 assert_eq!(server.get_epochs(), (Some(4), Some(3)));
203 // Now move the server temporarily into the future so that it
204 // rotates the keys. The client stays in the present.
205 drop(server.process_output(now + AT_LEAST_PTO));
206 assert_eq!(server.get_epochs(), (Some(4), Some(4)));
207 } else {
208 panic!("server should have a timer set");
209 }
210
211 // Now update keys on the server again.
212 assert!(server.initiate_key_update().is_ok());
213 assert_eq!(server.get_epochs(), (Some(5), Some(4)));
214
215 let dgram = send_something(&mut server, now + AT_LEAST_PTO);
216
217 // However, as the server didn't wait long enough to update again, the
218 // client hasn't rotated its keys, so the packet gets dropped.
219 check_discarded(&mut client, &dgram, false, 1, 0);
220 }
221
222 // Key updates can't be initiated too early.
223 #[test]
224 fn key_update_before_confirmed() {
225 let mut client = default_client();
226 assert_update_blocked(&mut client);
227 let mut server = default_server();
228 assert_update_blocked(&mut server);
229
230 // Client Initial
231 let dgram = client.process_output(now()).dgram();
232 let dgram2 = client.process_output(now()).dgram();
233 assert!(dgram.is_some() && dgram2.is_some());
234 assert_update_blocked(&mut client);
235
236 // Server Initial + Handshake
237 server.process_input(dgram.unwrap(), now());
238 let dgram = server.process(dgram2, now()).dgram();
239 assert!(dgram.is_some());
240 assert_update_blocked(&mut server);
241
242 let dgram = client.process(dgram, now()).dgram();
243 assert!(dgram.is_some());
244 assert_update_blocked(&mut client);
245
246 let dgram = server.process(dgram, now()).dgram();
247 assert!(dgram.is_some());
248 assert_update_blocked(&mut server);
249
250 // Client Handshake
251 client.process_input(dgram.unwrap(), now());
252 assert_update_blocked(&mut client);
253
254 assert!(maybe_authenticate(&mut client));
255 assert_update_blocked(&mut client);
256
257 let dgram = client.process_output(now()).dgram();
258 assert!(dgram.is_some());
259 assert_update_blocked(&mut client);
260
261 // Server HANDSHAKE_DONE
262 let dgram = server.process(dgram, now()).dgram();
263 assert!(dgram.is_some());
264 assert!(server.initiate_key_update().is_ok());
265
266 // Client receives HANDSHAKE_DONE
267 let dgram = client.process(dgram, now()).dgram();
268 assert!(dgram.is_none());
269 assert!(client.initiate_key_update().is_ok());
270 }
271
272 #[test]
273 fn exhaust_write_keys() {
274 let mut client = default_client();
275 let mut server = default_server();
276 connect_force_idle(&mut client, &mut server);
277
278 overwrite_invocations(0);
279 let stream_id = client.stream_create(StreamType::UniDi).unwrap();
280 assert!(client.stream_send(stream_id, b"explode!").is_ok());
281 let dgram = client.process_output(now()).dgram();
282 assert!(dgram.is_none());
283 assert!(matches!(
284 client.state(),
285 State::Closed(CloseReason::Transport(Error::KeysExhausted))
286 ));
287 }
288
289 #[test]
290 fn exhaust_read_keys() {
291 let mut client = default_client();
292 let mut server = default_server();
293 connect_force_idle(&mut client, &mut server);
294
295 let dgram = send_something(&mut client, now());
296
297 overwrite_invocations(0);
298 let dgram = server.process(Some(dgram), now()).dgram();
299 assert!(matches!(
300 server.state(),
301 State::Closed(CloseReason::Transport(Error::KeysExhausted))
302 ));
303
304 client.process_input(dgram.unwrap(), now());
305 assert!(matches!(
306 client.state(),
307 State::Draining {
308 error: CloseReason::Transport(Error::PeerError(ERROR_AEAD_LIMIT_REACHED)),
309 ..
310 }
311 ));
312 }
313
314 #[test]
315 fn automatic_update_write_keys() {
316 let mut client = default_client();
317 let mut server = default_server();
318 connect_force_idle(&mut client, &mut server);
319
320 overwrite_invocations(UPDATE_WRITE_KEYS_AT);
321 drop(send_something(&mut client, now()));
322 assert_eq!(client.get_epochs(), (Some(4), Some(3)));
323 }
324
325 #[test]
326 fn automatic_update_write_keys_later() {
327 let mut client = default_client();
328 let mut server = default_server();
329 connect_force_idle(&mut client, &mut server);
330
331 overwrite_invocations(UPDATE_WRITE_KEYS_AT + 2);
332 // No update after the first.
333 drop(send_something(&mut client, now()));
334 assert_eq!(client.get_epochs(), (Some(3), Some(3)));
335 // The second will update though.
336 drop(send_something(&mut client, now()));
337 assert_eq!(client.get_epochs(), (Some(4), Some(3)));
338 }
339
340 #[test]
341 fn automatic_update_write_keys_blocked() {
342 let mut client = default_client();
343 let mut server = default_server();
344 connect_force_idle(&mut client, &mut server);
345
346 // An outstanding key update will block the automatic update.
347 client.initiate_key_update().unwrap();
348
349 overwrite_invocations(UPDATE_WRITE_KEYS_AT);
350 let stream_id = client.stream_create(StreamType::UniDi).unwrap();
351 assert!(client.stream_send(stream_id, b"explode!").is_ok());
352 let dgram = client.process_output(now()).dgram();
353 // Not being able to update is fatal.
354 assert!(dgram.is_none());
355 assert!(matches!(
356 client.state(),
357 State::Closed(CloseReason::Transport(Error::KeysExhausted))
358 ));
359 }