1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
#![allow(dead_code)]
use neqo_common::qinfo;
use neqo_crypto::{
AntiReplay, AuthenticationStatus, Client, HandshakeState, RecordList, Res, ResumptionToken,
SecretAgent, Server, ZeroRttCheckResult, ZeroRttChecker,
};
use std::mem;
use std::time::Instant;
use test_fixture::{anti_replay, fixture_init, now};
/// Consume records until the handshake state changes.
pub fn forward_records(
now: Instant,
agent: &mut SecretAgent,
records_in: RecordList,
) -> Res<RecordList> {
let mut expected_state = match agent.state() {
HandshakeState::New => HandshakeState::New,
_ => HandshakeState::InProgress,
};
let mut records_out = RecordList::default();
for record in records_in {
assert_eq!(records_out.len(), 0);
assert_eq!(*agent.state(), expected_state);
records_out = agent.handshake_raw(now, Some(record))?;
expected_state = HandshakeState::InProgress;
}
Ok(records_out)
}
fn handshake(now: Instant, client: &mut SecretAgent, server: &mut SecretAgent) {
let mut a = client;
let mut b = server;
let mut records = a.handshake_raw(now, None).unwrap();
let is_done = |agent: &mut SecretAgent| agent.state().is_final();
while !is_done(b) {
records = if let Ok(r) = forward_records(now, b, records) {
r
} else {
// TODO(mt) take the alert generated by the failed handshake
// and allow it to be sent to the peer.
return;
};
if *b.state() == HandshakeState::AuthenticationPending {
b.authenticated(AuthenticationStatus::Ok);
records = if let Ok(r) = b.handshake_raw(now, None) {
r
} else {
// TODO(mt) - as above.
return;
}
}
mem::swap(&mut a, &mut b);
}
}
pub fn connect_at(now: Instant, client: &mut SecretAgent, server: &mut SecretAgent) {
handshake(now, client, server);
qinfo!("client: {:?}", client.state());
qinfo!("server: {:?}", server.state());
assert!(client.state().is_connected());
assert!(server.state().is_connected());
}
pub fn connect(client: &mut SecretAgent, server: &mut SecretAgent) {
connect_at(now(), client, server);
}
pub fn connect_fail(client: &mut SecretAgent, server: &mut SecretAgent) {
handshake(now(), client, server);
assert!(!client.state().is_connected());
assert!(!server.state().is_connected());
}
#[derive(Clone, Copy, Debug)]
pub enum Resumption {
WithoutZeroRtt,
WithZeroRtt,
}
pub const ZERO_RTT_TOKEN_DATA: &[u8] = b"zero-rtt-token";
#[derive(Debug)]
pub struct PermissiveZeroRttChecker {
resuming: bool,
}
impl Default for PermissiveZeroRttChecker {
fn default() -> Self {
Self { resuming: true }
}
}
impl ZeroRttChecker for PermissiveZeroRttChecker {
fn check(&self, token: &[u8]) -> ZeroRttCheckResult {
if self.resuming {
assert_eq!(ZERO_RTT_TOKEN_DATA, token);
} else {
assert!(token.is_empty());
}
ZeroRttCheckResult::Accept
}
}
fn zero_rtt_setup(
mode: Resumption,
client: &mut Client,
server: &mut Server,
) -> Option<AntiReplay> {
if let Resumption::WithZeroRtt = mode {
client.enable_0rtt().expect("should enable 0-RTT on client");
let anti_replay = anti_replay();
server
.enable_0rtt(
&anti_replay,
0xffff_ffff,
Box::new(PermissiveZeroRttChecker { resuming: false }),
)
.expect("should enable 0-RTT on server");
Some(anti_replay)
} else {
None
}
}
pub fn resumption_setup(mode: Resumption) -> (Option<AntiReplay>, ResumptionToken) {
fixture_init();
let mut client = Client::new("server.example").expect("should create client");
let mut server = Server::new(&["key"]).expect("should create server");
let anti_replay = zero_rtt_setup(mode, &mut client, &mut server);
connect(&mut client, &mut server);
assert!(!client.info().unwrap().resumed());
assert!(!server.info().unwrap().resumed());
assert!(!client.info().unwrap().early_data_accepted());
assert!(!server.info().unwrap().early_data_accepted());
let server_records = server
.send_ticket(now(), ZERO_RTT_TOKEN_DATA)
.expect("ticket sent");
assert_eq!(server_records.len(), 1);
let client_records = client
.handshake_raw(now(), server_records.into_iter().next())
.expect("records ingested");
assert_eq!(client_records.len(), 0);
// `client` is about to go out of scope,
// but we only need to keep the resumption token, so clone it.
let token = client.resumption_token().expect("token is present");
(anti_replay, token)
}
|