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
use crate::{Error, ErrorKind, Result};

// size of SEC_CHANNEL_BINDINGS structure
const SEC_CHANNEL_BINDINGS_SIZE: usize = 32;

/// [SEC_CHANNEL_BINDINGS](https://docs.microsoft.com/en-us/windows/win32/api/sspi/ns-sspi-sec_channel_bindings)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChannelBindings {
    pub initiator_addr_type: u32,
    pub initiator: Vec<u8>,
    pub acceptor_addr_type: u32,
    pub acceptor: Vec<u8>,
    pub application_data: Vec<u8>,
}

impl ChannelBindings {
    pub fn from_bytes<T: AsRef<[u8]>>(data: T) -> Result<Self> {
        let data = data.as_ref();

        if data.len() < SEC_CHANNEL_BINDINGS_SIZE {
            return Err(Error::new(
                ErrorKind::InvalidParameter,
                format!(
                    "Invalid SEC_CHANNEL_BINDINGS buffer: buffer is too short: {}. Minimum len: {}",
                    data.len(),
                    SEC_CHANNEL_BINDINGS_SIZE,
                ),
            ));
        }

        let initiator_addr_type = u32::from_le_bytes(data[0..4].try_into().unwrap());

        let initiator_len = u32::from_le_bytes(data[4..8].try_into().unwrap()) as usize;
        let initiator_offset = u32::from_le_bytes(data[8..12].try_into().unwrap()) as usize;
        if initiator_offset + initiator_len > data.len() {
            return Err(Error::new(
                ErrorKind::InvalidParameter,
                format!(
                    "Invalid SEC_CHANNEL_BINDINGS buffer: initiator offset + len ({}) goes outside the buffer ({})",
                    initiator_offset + initiator_len,
                    data.len()
                ),
            ));
        }

        let initiator = if initiator_len > 0 {
            data[initiator_offset..(initiator_offset + initiator_len)].to_vec()
        } else {
            Vec::new()
        };

        let acceptor_addr_type = u32::from_le_bytes(data[12..16].try_into().unwrap());

        let acceptor_len = u32::from_le_bytes(data[16..20].try_into().unwrap()) as usize;
        let acceptor_offset = u32::from_le_bytes(data[20..24].try_into().unwrap()) as usize;
        if acceptor_offset + acceptor_len > data.len() {
            return Err(Error::new(
                ErrorKind::InvalidParameter,
                format!(
                    "Invalid SEC_CHANNEL_BINDINGS buffer: acceptor offset + len ({}) goes outside the buffer ({})",
                    acceptor_offset + acceptor_len,
                    data.len()
                ),
            ));
        }

        let acceptor = if acceptor_len > 0 {
            data[acceptor_offset..(acceptor_offset + acceptor_len)].to_vec()
        } else {
            Vec::new()
        };

        let application_len = u32::from_le_bytes(data[24..28].try_into().unwrap()) as usize;
        let application_offset = u32::from_le_bytes(data[28..32].try_into().unwrap()) as usize;
        if application_offset + application_len > data.len() {
            return Err(Error::new(
                ErrorKind::InvalidParameter,
                format!(
                    "Invalid SEC_CHANNEL_BINDINGS buffer: application offset + len ({}) goes outside the buffer ({})",
                    application_offset + application_len,
                    data.len()
                ),
            ));
        }

        let application_data = if application_len > 0 {
            data[application_offset..(application_offset + application_len)].to_vec()
        } else {
            Vec::new()
        };

        Ok(Self {
            initiator_addr_type,
            initiator,
            acceptor_addr_type,
            acceptor,
            application_data,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::ChannelBindings;

    #[test]
    fn from_bytes() {
        let expected = ChannelBindings {
            initiator_addr_type: 0,
            initiator: Vec::new(),
            acceptor_addr_type: 0,
            acceptor: Vec::new(),
            application_data: vec![1, 2, 3, 4],
        };

        let channel_bindings_token = [1, 2, 3, 4];
        let application_offset = 32_u32;
        let application_len = channel_bindings_token.len();

        let mut buffer = [0; 36];

        buffer[24..28].copy_from_slice(&(application_len as u32).to_le_bytes());
        buffer[28..32].copy_from_slice(&application_offset.to_le_bytes());
        buffer[32..].copy_from_slice(&channel_bindings_token);

        let channel_bindings = ChannelBindings::from_bytes(buffer).unwrap();

        assert_eq!(channel_bindings, expected);
    }

    #[test]
    fn too_small_buffer() {
        assert!(ChannelBindings::from_bytes([1, 2, 3, 4, 5, 6, 7, 8]).is_err());

        assert!(ChannelBindings::from_bytes([]).is_err());
    }

    #[test]
    fn invalid_len() {
        let channel_bindings_token = [1, 2, 3, 4];
        let application_offset = 32_u32;
        // invalid len
        let application_len = channel_bindings_token.len() + 2;

        let mut buffer = [0; 36];

        buffer[24..28].copy_from_slice(&(application_len as u32).to_le_bytes());
        buffer[28..32].copy_from_slice(&application_offset.to_le_bytes());
        buffer[32..].copy_from_slice(&channel_bindings_token);

        let channel_bindings = ChannelBindings::from_bytes(buffer);

        assert!(channel_bindings.is_err());
    }

    #[test]
    fn invalid_offset() {
        let channel_bindings_token = [1, 2, 3, 4];
        // invalid offset
        let application_offset = 32_u32 + 3;
        let application_len = channel_bindings_token.len();

        let mut buffer = [0; 36];

        buffer[24..28].copy_from_slice(&(application_len as u32).to_le_bytes());
        buffer[28..32].copy_from_slice(&application_offset.to_le_bytes());
        buffer[32..].copy_from_slice(&channel_bindings_token);

        let channel_bindings = ChannelBindings::from_bytes(buffer);

        assert!(channel_bindings.is_err());
    }
}