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
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::{cell::RefCell, rc::Rc};
use neqo_crypto::{
constants::{HandshakeMessage, TLS_HS_CLIENT_HELLO, TLS_HS_ENCRYPTED_EXTENSIONS},
ext::{ExtensionHandler, ExtensionHandlerResult, ExtensionWriterResult},
Client, Server,
};
use test_fixture::fixture_init;
mod handshake;
use crate::handshake::connect;
struct NoopExtensionHandler;
impl ExtensionHandler for NoopExtensionHandler {}
// This test just handshakes. It doesn't really do anything about capturing the
#[test]
fn noop_extension_handler() {
fixture_init();
let mut client = Client::new("server.example", true).expect("should create client");
let mut server = Server::new(&["key"]).expect("should create server");
client
.extension_handler(0xffff, Rc::new(RefCell::new(NoopExtensionHandler)))
.expect("installed");
server
.extension_handler(0xffff, Rc::new(RefCell::new(NoopExtensionHandler)))
.expect("installed");
connect(&mut client, &mut server);
}
#[derive(Debug, Default)]
struct SimpleExtensionHandler {
written: bool,
handled: bool,
}
impl SimpleExtensionHandler {
#[allow(dead_code)]
pub fn negotiated(&self) -> bool {
self.written && self.handled
}
}
impl ExtensionHandler for SimpleExtensionHandler {
fn write(&mut self, msg: HandshakeMessage, d: &mut [u8]) -> ExtensionWriterResult {
match msg {
TLS_HS_CLIENT_HELLO | TLS_HS_ENCRYPTED_EXTENSIONS => {
self.written = true;
d[0] = 77;
ExtensionWriterResult::Write(1)
}
_ => ExtensionWriterResult::Skip,
}
}
fn handle(&mut self, msg: HandshakeMessage, d: &[u8]) -> ExtensionHandlerResult {
match msg {
TLS_HS_CLIENT_HELLO | TLS_HS_ENCRYPTED_EXTENSIONS => {
self.handled = true;
if d.len() != 1 {
ExtensionHandlerResult::Alert(50) // decode_error
} else if d[0] == 77 {
ExtensionHandlerResult::Ok
} else {
ExtensionHandlerResult::Alert(47) // illegal_parameter
}
}
_ => ExtensionHandlerResult::Alert(110), // unsupported_extension
}
}
}
#[test]
fn simple_extension() {
fixture_init();
let mut client = Client::new("server.example", true).expect("should create client");
let mut server = Server::new(&["key"]).expect("should create server");
let client_handler = Rc::new(RefCell::new(SimpleExtensionHandler::default()));
let ch = Rc::clone(&client_handler);
client
.extension_handler(0xffff, ch)
.expect("client handler installed");
let server_handler = Rc::new(RefCell::new(SimpleExtensionHandler::default()));
let sh = Rc::clone(&server_handler);
server
.extension_handler(0xffff, sh)
.expect("server handler installed");
connect(&mut client, &mut server);
assert!(client_handler.borrow().negotiated());
assert!(server_handler.borrow().negotiated());
}
|