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
#[cfg(test)]
mod extension_server_name_test;

use std::io::{Read, Write};

use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};

use super::*;

const EXTENSION_SERVER_NAME_TYPE_DNSHOST_NAME: u8 = 0;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ExtensionServerName {
    pub(crate) server_name: String,
}

impl ExtensionServerName {
    pub fn extension_value(&self) -> ExtensionValue {
        ExtensionValue::ServerName
    }

    pub fn size(&self) -> usize {
        //TODO: check how to do cryptobyte?
        2 + 2 + 1 + 2 + self.server_name.as_bytes().len()
    }

    pub fn marshal<W: Write>(&self, writer: &mut W) -> Result<()> {
        //TODO: check how to do cryptobyte?
        writer.write_u16::<BigEndian>(2 + 1 + 2 + self.server_name.len() as u16)?;
        writer.write_u16::<BigEndian>(1 + 2 + self.server_name.len() as u16)?;
        writer.write_u8(EXTENSION_SERVER_NAME_TYPE_DNSHOST_NAME)?;
        writer.write_u16::<BigEndian>(self.server_name.len() as u16)?;
        writer.write_all(self.server_name.as_bytes())?;

        Ok(writer.flush()?)
    }

    pub fn unmarshal<R: Read>(reader: &mut R) -> Result<Self> {
        //TODO: check how to do cryptobyte?
        let _ = reader.read_u16::<BigEndian>()? as usize;
        let _ = reader.read_u16::<BigEndian>()? as usize;

        let name_type = reader.read_u8()?;
        if name_type != EXTENSION_SERVER_NAME_TYPE_DNSHOST_NAME {
            return Err(Error::ErrInvalidSniFormat);
        }

        let buf_len = reader.read_u16::<BigEndian>()? as usize;
        let mut buf: Vec<u8> = vec![0u8; buf_len];
        reader.read_exact(&mut buf)?;

        let server_name = String::from_utf8(buf)?;

        Ok(ExtensionServerName { server_name })
    }
}