-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathtests.rs
517 lines (437 loc) · 15.9 KB
/
tests.rs
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use crate::{certificate, client, server};
use core::{
sync::atomic::{AtomicBool, AtomicU8, Ordering},
task::Poll,
};
use openssl::{ec::EcKey, ecdsa::EcdsaSig};
use pin_project::pin_project;
use s2n_quic_core::{
crypto::tls::{
self,
testing::certificates::{CERT_PEM, KEY_PEM, UNTRUSTED_CERT_PEM, UNTRUSTED_KEY_PEM},
Endpoint,
},
transport,
};
#[cfg(any(test, feature = "unstable_client_hello"))]
use s2n_tls::callbacks::ClientHelloCallback;
#[cfg(any(test, feature = "unstable_client_hello"))]
use s2n_tls::callbacks::{PrivateKeyCallback, PrivateKeyOperation};
use s2n_tls::{
callbacks::{ConnectionFuture, VerifyHostNameCallback},
connection::Connection,
error::Error,
};
use std::{sync::Arc, time::SystemTime};
pub struct MyCallbackHandler {
done: Arc<AtomicBool>,
wait_counter: Arc<AtomicU8>,
}
impl MyCallbackHandler {
fn new(wait_counter: u8) -> Self {
MyCallbackHandler {
done: Arc::new(AtomicBool::new(false)),
wait_counter: Arc::new(AtomicU8::new(wait_counter)),
}
}
}
#[cfg(any(test, feature = "unstable_client_hello"))]
impl ClientHelloCallback for MyCallbackHandler {
fn on_client_hello(
&self,
_connection: &mut Connection,
) -> Result<Option<std::pin::Pin<Box<dyn s2n_tls::callbacks::ConnectionFuture>>>, Error> {
let fut = MyConnectionFuture {
done: self.done.clone(),
wait_counter: self.wait_counter.clone(),
};
Ok(Some(Box::pin(fut)))
}
}
struct MyConnectionFuture {
done: Arc<AtomicBool>,
wait_counter: Arc<AtomicU8>,
}
impl ConnectionFuture for MyConnectionFuture {
fn poll(
self: std::pin::Pin<&mut Self>,
_connection: &mut Connection,
_ctx: &mut core::task::Context,
) -> Poll<Result<(), Error>> {
if self.wait_counter.fetch_sub(1, Ordering::SeqCst) == 0 {
self.done.store(true, Ordering::SeqCst);
return Poll::Ready(Ok(()));
}
Poll::Pending
}
}
pub static TICKET_KEY: [u8; 16] = [0; 16];
pub static TICKET_KEY_NAME: &[u8] = "keyname".as_bytes();
struct ResumptionConfig;
impl ResumptionConfig {
fn build() -> Result<s2n_tls::config::Config, s2n_tls::error::Error> {
let mut config_builder = s2n_tls::config::Builder::new();
config_builder
.enable_session_tickets(true)?
.add_session_ticket_key(TICKET_KEY_NAME, &TICKET_KEY, SystemTime::now())?
.load_pem(CERT_PEM.as_bytes(), KEY_PEM.as_bytes())?
.set_security_policy(&s2n_tls::security::DEFAULT_TLS13)?
.enable_quic()?
.set_application_protocol_preference([b"h3"])?;
config_builder.build()
}
}
impl crate::ConfigLoader for ResumptionConfig {
fn load(&mut self, _cx: crate::ConnectionContext) -> s2n_tls::config::Config {
Self::build().expect("Config builder failed")
}
}
#[cfg(any(test, feature = "unstable_private_key"))]
impl PrivateKeyCallback for MyCallbackHandler {
fn handle_operation(
&self,
_connection: &mut Connection,
op: PrivateKeyOperation,
) -> Result<Option<std::pin::Pin<Box<dyn s2n_tls::callbacks::ConnectionFuture>>>, Error> {
let future = MyConnectionFuture {
done: self.done.clone(),
wait_counter: self.wait_counter.clone(),
};
let op = Some(op);
let pkey_future = MyPrivateKeyFuture { future, op };
Ok(Some(Box::pin(pkey_future)))
}
}
#[pin_project]
struct MyPrivateKeyFuture {
#[pin]
future: MyConnectionFuture,
#[pin]
op: Option<PrivateKeyOperation>,
}
impl ConnectionFuture for MyPrivateKeyFuture {
fn poll(
self: std::pin::Pin<&mut Self>,
conn: &mut Connection,
ctx: &mut core::task::Context,
) -> Poll<Result<(), Error>> {
let mut this = self.project();
if this.future.poll(conn, ctx).is_pending() {
return Poll::Pending;
}
let op = this.op.take().expect("Missing pkey operation");
let in_buf_size = op.input_size()?;
let mut in_buf = vec![0; in_buf_size];
op.input(&mut in_buf)?;
let key = EcKey::private_key_from_pem(KEY_PEM.as_bytes())
.expect("Failed to create EcKey from pem");
let sig = EcdsaSig::sign(&in_buf, &key).expect("Failed to sign input");
let out = sig.to_der().expect("Failed to convert signature to der");
op.set_output(conn, &out)?;
Poll::Ready(Ok(()))
}
}
pub struct VerifyHostNameClientCertVerifier {
host_name: String,
}
impl VerifyHostNameCallback for VerifyHostNameClientCertVerifier {
fn verify_host_name(&self, host_name: &str) -> bool {
self.host_name == host_name
}
}
impl VerifyHostNameClientCertVerifier {
pub fn new(host_name: impl ToString) -> VerifyHostNameClientCertVerifier {
VerifyHostNameClientCertVerifier {
host_name: host_name.to_string(),
}
}
}
#[derive(Default)]
pub struct RejectAllClientCertificatesHandler {}
impl VerifyHostNameCallback for RejectAllClientCertificatesHandler {
fn verify_host_name(&self, _host_name: &str) -> bool {
false
}
}
fn s2n_client() -> client::Client {
client::Builder::default()
.with_certificate(CERT_PEM)
.unwrap()
.build()
.unwrap()
}
fn s2n_client_with_client_auth() -> Result<client::Client, Error> {
client::Builder::default()
.with_empty_trust_store()?
.with_certificate(CERT_PEM)?
.with_client_identity(CERT_PEM, KEY_PEM)?
.build()
}
fn s2n_client_with_untrusted_client_auth() -> Result<client::Client, Error> {
client::Builder::default()
.with_empty_trust_store()?
.with_certificate(CERT_PEM)?
.with_client_identity(UNTRUSTED_CERT_PEM, UNTRUSTED_KEY_PEM)?
.build()
}
fn s2n_client_with_fixed_hostname_auth(host_name: &str) -> Result<client::Client, Error> {
client::Builder::default()
.with_certificate(CERT_PEM)
.unwrap()
.with_verify_host_name_callback(VerifyHostNameClientCertVerifier::new(host_name))
.unwrap()
.build()
}
fn s2n_client_with_resumption() -> Result<client::Client, Error> {
let mut builder = client::Builder::default().with_certificate(CERT_PEM)?;
struct TicketCallback;
impl s2n_tls::callbacks::SessionTicketCallback for TicketCallback {
fn on_session_ticket(
&self,
_connection: &mut Connection,
_session_ticket: &s2n_tls::callbacks::SessionTicket,
) {
}
}
let cb = TicketCallback;
builder.config_mut().set_session_ticket_callback(cb)?;
builder.config_mut().enable_session_tickets(true)?;
builder.build()
}
fn s2n_server() -> server::Server {
server::Builder::default()
.with_certificate(CERT_PEM, KEY_PEM)
.unwrap()
.build()
.unwrap()
}
fn s2n_server_with_client_auth() -> Result<server::Server, Error> {
server::Builder::default()
.with_empty_trust_store()?
.with_client_authentication()?
.with_verify_host_name_callback(VerifyHostNameClientCertVerifier::new("qlaws.qlaws"))?
.with_certificate(CERT_PEM, KEY_PEM)?
.with_trusted_certificate(CERT_PEM)?
.build()
}
fn s2n_server_with_resumption() -> server::Server<ResumptionConfig> {
server::Server::from_loader(ResumptionConfig)
}
fn s2n_server_with_client_auth_verifier_rejects_client_certs() -> Result<server::Server, Error> {
server::Builder::default()
.with_empty_trust_store()?
.with_client_authentication()?
.with_verify_host_name_callback(RejectAllClientCertificatesHandler::default())?
.with_certificate(CERT_PEM, KEY_PEM)?
.with_trusted_certificate(CERT_PEM)?
.build()
}
#[cfg(any(test, feature = "unstable_client_hello"))]
fn s2n_server_with_client_hello_callback(wait_counter: u8) -> (server::Server, Arc<AtomicBool>) {
let handle = MyCallbackHandler::new(wait_counter);
let done = handle.done.clone();
let tls = server::Builder::default()
.with_certificate(CERT_PEM, KEY_PEM)
.unwrap()
.with_client_hello_handler(handle)
.unwrap()
.build()
.unwrap();
(tls, done)
}
#[cfg(any(test, feature = "unstable_private_key"))]
fn s2n_server_with_private_key_callback(wait_counter: u8) -> (server::Server, Arc<AtomicBool>) {
let handle = MyCallbackHandler::new(wait_counter);
let done = handle.done.clone();
let tls = server::Builder::default()
.with_certificate(CERT_PEM, certificate::OFFLOAD_PRIVATE_KEY)
.unwrap()
.with_private_key_handler(handle)
.unwrap()
.build()
.unwrap();
(tls, done)
}
fn rustls_server() -> s2n_quic_rustls::server::Server {
s2n_quic_rustls::server::Builder::default()
.with_certificate(CERT_PEM, KEY_PEM)
.unwrap()
.build()
.unwrap()
}
fn rustls_client() -> s2n_quic_rustls::client::Client {
s2n_quic_rustls::client::Builder::default()
.with_certificate(CERT_PEM)
.unwrap()
.build()
.unwrap()
}
#[test]
#[cfg_attr(miri, ignore)]
fn s2n_client_s2n_server_ch_callback_test() {
for wait_counter in 0..=10 {
let mut client_endpoint = s2n_client();
let (mut server_endpoint, done) = s2n_server_with_client_hello_callback(wait_counter);
run(&mut server_endpoint, &mut client_endpoint, Some(done));
}
}
#[test]
#[cfg_attr(miri, ignore)]
fn s2n_client_s2n_server_pkey_callback_test() {
for wait_counter in 0..=10 {
let mut client_endpoint = s2n_client();
let (mut server_endpoint, done) = s2n_server_with_private_key_callback(wait_counter);
run(&mut server_endpoint, &mut client_endpoint, Some(done));
}
}
#[test]
#[cfg_attr(miri, ignore)]
fn s2n_client_s2n_server_test() {
let mut client_endpoint = s2n_client();
let mut server_endpoint = s2n_server();
run(&mut server_endpoint, &mut client_endpoint, None);
}
#[test]
#[cfg_attr(miri, ignore)]
fn s2n_client_s2n_server_resumption_test() {
let mut client_endpoint = s2n_client_with_resumption().unwrap();
let mut server_endpoint = s2n_server_with_resumption();
let pair = run_result(&mut server_endpoint, &mut client_endpoint, None).unwrap();
assert!(
!pair.client.context.application.rx.is_empty(),
"expected session ticket message in RX"
);
}
#[test]
#[cfg_attr(miri, ignore)]
fn rustls_client_s2n_server_resumption_test() {
let mut client_endpoint = rustls_client();
let mut server_endpoint = s2n_server_with_resumption();
let pair = run_result(&mut server_endpoint, &mut client_endpoint, None).unwrap();
assert!(
!pair.client.context.application.rx.is_empty(),
"expected session ticket message in RX"
);
}
#[test]
#[cfg_attr(miri, ignore)]
fn rustls_client_s2n_server_test() {
let mut client_endpoint = rustls_client();
let mut server_endpoint = s2n_server();
run(&mut server_endpoint, &mut client_endpoint, None);
}
#[test]
#[cfg_attr(miri, ignore)]
fn s2n_client_rustls_server_test() {
let mut client_endpoint = s2n_client();
let mut server_endpoint = rustls_server();
run(&mut server_endpoint, &mut client_endpoint, None);
}
#[test]
#[cfg_attr(miri, ignore)]
fn s2n_client_s2n_server_client_auth_test() {
let mut client_endpoint = s2n_client_with_client_auth().unwrap();
let mut server_endpoint = s2n_server_with_client_auth().unwrap();
run(&mut server_endpoint, &mut client_endpoint, None);
}
#[test]
#[cfg_attr(miri, ignore)]
fn s2n_client_no_client_auth_s2n_server_requires_client_auth_test() {
let mut client_endpoint = s2n_client();
let mut server_endpoint = s2n_server_with_client_auth().unwrap();
let test_result = run_result(&mut server_endpoint, &mut client_endpoint, None);
// The handshake should fail because the server requires client auth,
// but the client does not support it.
assert!(test_result.is_err());
let e = test_result.unwrap_err();
assert_eq!(e.description().unwrap(), "CERTIFICATE_REQUIRED");
}
#[test]
#[cfg_attr(miri, ignore)]
fn s2n_client_with_client_auth_s2n_server_does_not_require_client_auth_test() {
let mut client_endpoint = s2n_client_with_client_auth().unwrap();
let mut server_endpoint = s2n_server();
let test_result = run_result(&mut server_endpoint, &mut client_endpoint, None);
// The handshake should fail because the client requires client auth,
// but the server does not support it.
assert!(test_result.is_err());
let e = test_result.unwrap_err();
assert_eq!(e.description().unwrap(), "UNEXPECTED_MESSAGE");
}
#[test]
#[cfg_attr(miri, ignore)]
fn s2n_client_with_client_auth_s2n_server_does_not_trust_client_certificate() {
let mut client_endpoint = s2n_client_with_client_auth().unwrap();
let mut server_endpoint = s2n_server_with_client_auth_verifier_rejects_client_certs().unwrap();
let test_result = run_result(&mut server_endpoint, &mut client_endpoint, None);
// The handshake should fail because the certificate presented by the client is rejected by the
// application level host verification check on the cert.
assert!(test_result.is_err());
let e = test_result.unwrap_err();
assert_eq!(e.description().unwrap(), "CERTIFICATE_UNKNOWN");
}
#[test]
#[cfg_attr(miri, ignore)]
fn s2n_client_with_client_auth_s2n_server_does_not_trust_issuer() {
let mut client_endpoint = s2n_client_with_untrusted_client_auth().unwrap();
let mut server_endpoint = s2n_server_with_client_auth().unwrap();
let test_result = run_result(&mut server_endpoint, &mut client_endpoint, None);
// The handshake should fail because the certificate presented by the client is issued
// by a CA that is not in the server trust store, even though the host name is validated.
assert!(test_result.is_err());
let e = test_result.unwrap_err();
assert_eq!(e.description().unwrap(), "CERTIFICATE_UNKNOWN");
}
#[test]
#[cfg_attr(miri, ignore)]
fn s2n_client_with_custom_hostname_auth_rejects_server_name() {
let mut client_endpoint = s2n_client_with_fixed_hostname_auth("not-localhost").unwrap();
let mut server_endpoint = s2n_server();
let test_result = run_result(&mut server_endpoint, &mut client_endpoint, None);
// The handshake should fail because the hostname ("localhost") is not validated
assert!(test_result.is_err());
let e = test_result.unwrap_err();
assert_eq!(e.description().unwrap(), "CERTIFICATE_UNKNOWN");
}
#[test]
#[cfg_attr(miri, ignore)]
fn s2n_client_with_custom_hostname_auth_accepts_server_name() {
let mut client_endpoint = s2n_client_with_fixed_hostname_auth("localhost").unwrap();
let mut server_endpoint = s2n_server();
run_result(&mut server_endpoint, &mut client_endpoint, None).unwrap();
}
/// Executes the handshake to completion
fn run_result<S: Endpoint, C: Endpoint>(
server: &mut S,
client: &mut C,
client_hello_cb_done: Option<Arc<AtomicBool>>,
) -> Result<tls::testing::Pair<S::Session, C::Session>, transport::Error> {
let mut pair = tls::testing::Pair::new(server, client, "localhost".into());
while pair.is_handshaking() {
pair.poll(client_hello_cb_done.as_ref())?;
}
pair.finish();
Ok(pair)
}
/// Executes the handshake to completion
fn run<S: Endpoint, C: Endpoint>(
server: &mut S,
client: &mut C,
client_hello_cb_done: Option<Arc<AtomicBool>>,
) {
run_result(server, client, client_hello_cb_done).unwrap();
}
#[test]
fn config_loader() {
use crate::{ConfigLoader, Server};
let server = Server::default();
// make sure the loader can be a static type
let server: Server<Server> = Server::from_loader(server);
// make sure the loader can be a dynamic type
let server: Box<dyn ConfigLoader> = Box::new(server);
let mut server: Server<Box<dyn ConfigLoader>> = Server::from_loader(server);
// make sure the server can actually create a session
let _ = server.new_server_session(&1);
}