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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
use std::{fs, path::Path, thread::sleep, time::Duration};
use bevy::{app::ScheduleRunnerPlugin, prelude::App};
use bevy_quinnet::{
client::{
self,
certificate::{CertVerificationStatus, CertificateVerificationMode},
Client, QuinnetClientPlugin, DEFAULT_KNOWN_HOSTS_FILE,
},
server::{
certificate::CertificateRetrievalMode, QuinnetServerPlugin, Server, ServerConfiguration,
},
};
// https://github.com/rust-lang/rust/issues/46379
pub use utils::*;
mod utils;
///////////////////////////////////////////////////////////
/// ///
/// Test ///
/// ///
///////////////////////////////////////////////////////////
const TEST_CERT_FILE: &str = "assets/tests/cert.pem.test";
const TEST_KEY_FILE: &str = "assets/tests/key.pem.test";
const TEST_CERT_FINGERPRINT_B64: &str = "sieQJ9J6DIrQP37HAlUFk2hYhLZDY9G5OZQpqzkWlKo=";
#[test]
fn trust_on_first_use() {
// TOFU With default parameters
// Server listens with a cert loaded from a file
// Client connects with empty cert store
// -> The server's certificate is treatead as Unknown by the client, which stores it and continues the connection
// Clients disconnects
// Client reconnects with the updated cert store
// -> The server's certificate is treatead as Trusted by the client, which continues the connection
// Clients disconnects
// Server reboots, and generates a new self-signed certificate
// Client reconnects with its cert store
// -> The server's certificate is treatead as Untrusted by the client, which requests a client action
// We receive the client action request and ask to abort the connection
let port = 6004; // TODO Use port 0 and retrieve the port used by the server.
if Path::new(DEFAULT_KNOWN_HOSTS_FILE).exists() {
fs::remove_file(DEFAULT_KNOWN_HOSTS_FILE)
.expect("Failed to remove default known hosts file");
}
let mut client_app = App::new();
client_app
.add_plugin(ScheduleRunnerPlugin::default())
.add_plugin(QuinnetClientPlugin::default())
.insert_resource(ClientTestData::default())
.add_system(handle_client_events);
let mut server_app = App::new();
server_app
.add_plugin(ScheduleRunnerPlugin::default())
.add_plugin(QuinnetServerPlugin::default())
.insert_resource(ServerTestData::default())
.add_system(handle_server_events);
// Startup
client_app.update();
server_app.update();
// Server listens with a cert loaded from a file
{
let mut server = server_app.world.resource_mut::<Server>();
let (server_cert, _) = server
.start_endpoint(
ServerConfiguration::from_ip("0.0.0.0".parse().unwrap(), port),
CertificateRetrievalMode::LoadFromFile {
cert_file: TEST_CERT_FILE.to_string(),
key_file: TEST_KEY_FILE.to_string(),
},
)
.unwrap();
assert_eq!(
TEST_CERT_FINGERPRINT_B64.to_string(),
server_cert.fingerprint.to_base64(),
"The loaded cert fingerprint should match the known test fingerprint"
);
}
// Client connects with empty cert store
{
let mut client = client_app.world.resource_mut::<Client>();
client
.open_connection(
default_client_configuration(port),
CertificateVerificationMode::TrustOnFirstUse(
client::certificate::TrustOnFirstUseConfig {
..Default::default()
},
),
)
.unwrap();
}
// Let the async runtime connection connect.
sleep(Duration::from_secs_f32(0.1));
// Connection & event propagation
server_app.update();
client_app.update();
// The server's certificate is treatead as Unknown by the client, which stores it and continues the connection
{
let mut client_test_data = client_app.world.resource_mut::<ClientTestData>();
assert_eq!(
client_test_data.cert_trust_update_events_received, 1,
"The client should have received exactly 1 certificate trust update event"
);
let cert_info = client_test_data
.last_trusted_cert_info
.as_mut()
.expect("A certificate trust update should have happened");
assert_eq!(
cert_info.fingerprint.to_base64(),
TEST_CERT_FINGERPRINT_B64.to_string(),
"The certificate rceived by the client should match the known test certificate"
);
assert!(
cert_info.known_fingerprint.is_none(),
"The client should not have any previous certificate fingerprint for this server"
);
assert_eq!(
cert_info.server_name.to_string(),
SERVER_IP.to_string(),
"The server name should match the one we configured"
);
let mut client = client_app.world.resource_mut::<Client>();
assert!(
client.connection().is_connected(),
"The default connection should be connected to the server"
);
// Clients disconnects
// Client reconnects with the updated cert store
client
.close_all_connections()
.expect("Failed to close connections on the client");
client
.open_connection(
default_client_configuration(port),
CertificateVerificationMode::TrustOnFirstUse(
client::certificate::TrustOnFirstUseConfig {
..Default::default()
},
),
)
.unwrap();
}
// Let the async runtime connection connect.
sleep(Duration::from_secs_f32(0.1));
// Connection & event propagation
server_app.update();
client_app.update();
{
assert!(
client_app
.world
.resource_mut::<Client>()
.connection()
.is_connected(),
"The default connection should be connected to the server"
);
let client_test_data = client_app.world.resource::<ClientTestData>();
assert_eq!(client_test_data.cert_trust_update_events_received, 1, "The client should still have only 1 certificate trust update event after his reconnection");
// Clients disconnects
client_app
.world
.resource_mut::<Client>()
.close_all_connections()
.expect("Failed to close connections on the client");
}
// Server reboots, and generates a new self-signed certificate
server_app
.world
.resource_mut::<Server>()
.stop_endpoint()
.unwrap();
// Let the endpoint fully stop.
sleep(Duration::from_secs_f32(0.1));
let (server_cert, _) = server_app
.world
.resource_mut::<Server>()
.start_endpoint(
ServerConfiguration::from_ip(LOCAL_BIND_IP, port),
CertificateRetrievalMode::GenerateSelfSigned {
server_hostname: SERVER_IP.to_string(),
},
)
.unwrap();
// Client reconnects with its cert store containing the previously store certificate fingerprint
{
let mut client = client_app.world.resource_mut::<Client>();
client
.open_connection(
default_client_configuration(port),
CertificateVerificationMode::TrustOnFirstUse(
client::certificate::TrustOnFirstUseConfig {
..Default::default()
},
),
)
.unwrap();
}
// Let the async runtime connection connect.
sleep(Duration::from_secs_f32(0.1));
// Connection & event propagation: certificate interaction event
server_app.update();
client_app.update();
// Let the async runtime process the certificate action & connection.
sleep(Duration::from_secs_f32(0.1));
// Connection abort event
client_app.update();
// The server's certificate is treatead as Untrusted by the client, which requests a client action
// We received the client action request and asked to abort the connection
{
let mut client_test_data = client_app.world.resource_mut::<ClientTestData>();
assert_eq!(
client_test_data.cert_interactions_received, 1,
"The client should have received exactly 1 certificate interaction event"
);
assert_eq!(
client_test_data.cert_verif_connection_abort_events_received, 1,
"The client should have received exactly 1 certificate connection abort event"
);
// Verify the cert info in the certificate interaction event
let interaction_cert_info = client_test_data
.last_cert_interactions_info
.as_mut()
.expect(
"A certificate interaction event should have happened during certificate verification",
);
assert_eq!(
interaction_cert_info.fingerprint.to_base64(),
server_cert.fingerprint.to_base64(),
"The fingerprint received by the client should match the one generated by the server"
);
// Verify the known fingerprint
assert_eq!(
interaction_cert_info
.known_fingerprint
.as_mut()
.expect("There should be a known fingerprint in the store")
.to_base64(),
TEST_CERT_FINGERPRINT_B64.to_string(),
"The previously known fingeprint for this server should be the test fingerprint"
);
assert_eq!(
interaction_cert_info.server_name.to_string(),
SERVER_IP.to_string(),
"The server name in the certificate interaction event should be the server we want to connect to"
);
assert_eq!(
client_test_data.last_cert_interactions_status,
Some(CertVerificationStatus::UntrustedCertificate),
"The certificate verification status in the certificate interaction event should be `Untrusted`"
);
// Verify the cert info in the connection abort event
assert_eq!(
client_test_data.last_abort_cert_info,
client_test_data.last_cert_interactions_info,
"The certificate info in the connection abort event should match those of the certificate interaction event"
);
assert_eq!(
client_test_data.last_abort_cert_status,
Some(CertVerificationStatus::UntrustedCertificate),
"The certificate verification status in the connection abort event should be `Untrusted`"
);
let client = client_app.world.resource::<Client>();
assert!(
client.connection().is_connected() == false,
"The default connection should not be connected to the server"
);
}
// Leave the workspace clean
fs::remove_file(DEFAULT_KNOWN_HOSTS_FILE).expect("Failed to remove default known hosts file");
}
|