Skip to main content

reqtls/message/
server_hello.rs

1use super::super::extend::Extension;
2use super::super::message::HandshakeType;
3use super::super::suite::CipherSuite;
4use super::super::version::Version;
5use crate::bytes::ByteRef;
6use crate::error::RlsResult;
7use crate::extend::alps::ALPS;
8use crate::extend::ExtensionValue;
9use crate::{ClientHello, ExtensionType, WriteExt, ALPN};
10
11#[derive(Debug)]
12pub struct ServerHello<'a> {
13    handshake_type: HandshakeType,
14    len: u32,
15    version: Version,
16    pub(crate) random: ByteRef<'a>,
17    session_id_len: u8,
18    session_id: ByteRef<'a>,
19    pub cipher_suite: CipherSuite,
20    compress_method: u8,
21    extend_len: u16,
22    extensions: Vec<Extension>,
23}
24
25impl<'a> Default for ServerHello<'a> {
26    fn default() -> Self {
27        ServerHello {
28            handshake_type: HandshakeType::ServerHello,
29            len: 0,
30            version: Version::new(0),
31            random: ByteRef::default(),
32            session_id_len: 0,
33            session_id: ByteRef::default(),
34            cipher_suite: CipherSuite::new(0),
35            compress_method: 0,
36            extend_len: 0,
37            extensions: vec![],
38        }
39    }
40}
41
42impl<'a> ServerHello<'a> {
43    pub fn from_bytes(ht: HandshakeType, bytes: &'a [u8]) -> RlsResult<ServerHello<'a>> {
44        let mut res = ServerHello::default();
45        res.handshake_type = ht;
46        res.len = u32::from_be_bytes([0, bytes[1], bytes[2], bytes[3]]);
47        res.version = Version::new(u16::from_be_bytes([bytes[4], bytes[5]]));
48        res.random = ByteRef::new(&bytes[6..38]);
49        res.session_id_len = bytes[38];
50        let index = 39 + res.session_id_len as usize;
51        res.session_id = ByteRef::new(&bytes[39..index]);
52        let v = u16::from_be_bytes([bytes[index], bytes[index + 1]]);
53        res.cipher_suite = CipherSuite::new(v);
54        res.compress_method = bytes[index + 2];
55        res.extend_len = u16::from_be_bytes([bytes[index + 3], bytes[index + 4]]);
56        res.extensions = Extension::from_bytes(&bytes[index + 5..index + 5 + res.extend_len as usize], true)?;
57        Ok(res)
58    }
59
60    pub fn from_client_hello(mut client_hello: ClientHello) -> RlsResult<ServerHello<'a>> {
61        let mut res = ServerHello::default();
62        res.version = Version::TLS_1_2;
63        // res.random = ByteRef::new(random);
64        // res.session_id = ByteRef::new(session_id);
65        res.cipher_suite = CipherSuite::TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256;
66        for extension in client_hello.take_extensions() {
67            match *extension.extension_type() {
68                ExtensionType::SignatureAlgorithms => {
69                    // let mut signature = SignatureAlgorithms::new();
70                    // signature.push_hash(SignatureAlgorithm::RSA_PKCS1_SHA256);
71                    // res.extensions.push(Extension::new(ExtensionType::SignatureAlgorithms, ExtensionValue::SignatureAlgorithms(signature)));
72                }
73                // ExtensionType::SignedCertificateTimestamp => res.extensions.push(extension),
74                ExtensionType::EcPointFormats => {
75                    // let mut ec_point_formats = EcPointFormats::new();
76                    // ec_point_formats.add_format(EcPointFormat::UNCOMPRESSED);
77                    // res.extensions.push(Extension::new(ExtensionType::EcPointFormats, ExtensionValue::EcPointFormats(ec_point_formats)));
78                }
79                ExtensionType::ApplicationLayerProtocolNegotiation => {
80                    let mut alps = ALPS::new();
81                    alps.add_alpn(ALPN::Http11);
82                    res.extensions.push(Extension::new(ExtensionType::ApplicationLayerProtocolNegotiation, ExtensionValue::ApplicationLayerProtocolNegotiation(alps)));
83                }
84                ExtensionType::ExtendMasterSecret => res.extensions.push(extension),
85                ExtensionType::SupportedVersions => {
86                    // let mut version = SupportVersions::new();
87                    // version.push(Version::TLS_1_2);
88                    // res.extensions.push(Extension::new(ExtensionType::SupportedVersions, ExtensionValue::SupportedVersions(version)));
89                }
90                ExtensionType::SupportedGroup => {
91                    // let mut groups = SupportedGroups::new();
92                    // groups.add_group(GroupType::X25519);
93                    // res.extensions.push(Extension::new(ExtensionType::SupportedGroup, ExtensionValue::SupportedGroups(groups)));
94                }
95                ExtensionType::RenegotiationInfo => res.extensions.push(extension),
96                ExtensionType::ServerName => {}
97                ExtensionType::StatusRequest => res.extensions.push(extension),
98                ExtensionType::SessionTicket => res.extensions.push(extension),
99                _ => {}
100            }
101        }
102        Ok(res)
103    }
104
105    pub fn use_ems(&self) -> bool {
106        self.extensions.iter().any(|x| x.extension_type() == &ExtensionType::ExtendMasterSecret)
107    }
108
109    pub fn alpn(&self) -> Option<ALPN> {
110        let extend = self.extensions.iter().find(|x| x.alps().is_some())?;
111        let protocol = extend.alps()?;
112        let alpn = protocol.values().first()?.clone();
113        Some(alpn)
114    }
115
116    pub fn len(&self) -> usize {
117        6 + self.random.len() + 1 + self.session_id.len() + 2 + 1 + 2 +
118            self.extensions.iter().map(|x| x.len(true)).sum::<usize>()
119    }
120
121    pub fn write_to<W: WriteExt>(self, writer: &mut W) {
122        writer.write_u8(self.handshake_type as u8);
123        writer.write_u32(self.len() as u32 - 4, true);
124        writer.write_u16(self.version.into_inner());
125        writer.write_slice(self.random.as_ref());
126        writer.write_u8(self.session_id.len() as u8);
127        writer.write_slice(self.session_id.as_ref());
128        writer.write_u16(self.cipher_suite.into_inner());
129        writer.write_u8(self.compress_method);
130        let len = self.extensions.iter().map(|x| x.len(true)).sum::<usize>();
131        writer.write_u16(len as u16);
132        for extension in self.extensions {
133            extension.write_to(writer, true)
134        }
135    }
136
137    pub fn set_random(&mut self, random: &'a [u8]) {
138        self.random = ByteRef::new(random);
139    }
140
141    pub fn set_session_id(&mut self, session_id: &'a [u8]) {
142        self.session_id = ByteRef::new(session_id);
143    }
144}
145
146#[derive(Debug)]
147pub struct ServerHelloDone {
148    handshake_type: HandshakeType,
149    len: u32,
150}
151
152impl ServerHelloDone {
153    pub fn new() -> ServerHelloDone {
154        ServerHelloDone {
155            handshake_type: HandshakeType::ServerHelloDone,
156            len: 0,
157        }
158    }
159
160    pub fn from_bytes(ht: HandshakeType, bytes: &[u8]) -> RlsResult<ServerHelloDone> {
161        let mut res = ServerHelloDone::new();
162        res.handshake_type = ht;
163        res.len = u32::from_be_bytes([0, bytes[1], bytes[2], bytes[3]]);
164        Ok(res)
165    }
166
167    pub fn len(&self) -> usize {
168        4
169    }
170
171    pub fn write_to<W: WriteExt>(self, writer: &mut W) {
172        writer.write_u8(self.handshake_type as u8);
173        writer.write_u32(self.len, true);
174    }
175}