1use binrw::{BinRead, BinWrite, binrw};
5use std::io::Cursor;
6
7use crate::proto::error::ProtoResult;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15#[repr(u16)]
16pub enum Dialect {
17 Smb202 = 0x0202,
18 Smb210 = 0x0210,
19 Smb300 = 0x0300,
20 Smb302 = 0x0302,
21 Smb311 = 0x0311,
22 Smb2Wildcard = 0x02FF,
25}
26
27impl Dialect {
28 pub fn from_u16(v: u16) -> Option<Self> {
29 Some(match v {
30 0x0202 => Self::Smb202,
31 0x0210 => Self::Smb210,
32 0x0300 => Self::Smb300,
33 0x0302 => Self::Smb302,
34 0x0311 => Self::Smb311,
35 0x02FF => Self::Smb2Wildcard,
36 _ => return None,
37 })
38 }
39
40 pub const fn as_u16(self) -> u16 {
41 self as u16
42 }
43}
44
45#[binrw]
62#[brw(little)]
63#[derive(Debug, Clone, PartialEq, Eq)]
64pub struct NegotiateRequest {
65 pub structure_size: u16,
66 pub dialect_count: u16,
67 pub security_mode: u16,
68 pub reserved: u16,
69 pub capabilities: u32,
70 pub client_guid: [u8; 16],
71 pub negotiate_context_offset_or_client_start_time: u64,
73 #[br(count = dialect_count as usize)]
74 pub dialects: Vec<u16>,
75}
76
77impl NegotiateRequest {
78 pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
79 Ok(Self::read(&mut Cursor::new(buf))?)
80 }
81 pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
82 let mut c = Cursor::new(Vec::new());
83 BinWrite::write(self, &mut c)?;
84 out.extend_from_slice(&c.into_inner());
85 Ok(())
86 }
87}
88
89#[binrw]
104#[brw(little)]
105#[derive(Debug, Clone, PartialEq, Eq)]
106pub struct NegotiateResponse {
107 pub structure_size: u16,
108 pub security_mode: u16,
109 pub dialect_revision: u16,
110 pub negotiate_context_count_or_reserved: u16,
112 pub server_guid: [u8; 16],
113 pub capabilities: u32,
114 pub max_transact_size: u32,
115 pub max_read_size: u32,
116 pub max_write_size: u32,
117 pub system_time: u64,
119 pub server_start_time: u64,
120 pub security_buffer_offset: u16,
121 pub security_buffer_length: u16,
122 pub negotiate_context_offset_or_reserved2: u32,
124 #[br(count = security_buffer_length as usize)]
125 pub security_buffer: Vec<u8>,
126}
127
128impl NegotiateResponse {
129 pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
130 Ok(Self::read(&mut Cursor::new(buf))?)
131 }
132 pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
133 let mut c = Cursor::new(Vec::new());
134 BinWrite::write(self, &mut c)?;
135 out.extend_from_slice(&c.into_inner());
136 Ok(())
137 }
138}
139
140#[binrw]
150#[brw(little)]
151#[derive(Debug, Clone, PartialEq, Eq)]
152pub struct NegotiateContext {
153 pub context_type: u16,
154 pub data_length: u16,
155 pub reserved: u32,
156 #[br(count = data_length as usize)]
157 pub data: Vec<u8>,
158}
159
160impl NegotiateContext {
161 pub const TYPE_PREAUTH_INTEGRITY: u16 = 0x0001;
162 pub const TYPE_ENCRYPTION: u16 = 0x0002;
163 pub const TYPE_COMPRESSION: u16 = 0x0003;
164 pub const TYPE_NETNAME_NEGOTIATE: u16 = 0x0005;
165 pub const TYPE_TRANSPORT_CAPS: u16 = 0x0006;
166 pub const TYPE_RDMA_TRANSFORM: u16 = 0x0007;
167 pub const TYPE_SIGNING: u16 = 0x0008;
168
169 pub fn parse_list(mut buf: &[u8], count: u16) -> ProtoResult<Vec<NegotiateContext>> {
173 let mut out = Vec::with_capacity(count as usize);
174 let mut consumed_total = 0usize;
175 for _ in 0..count {
176 let pad = (8 - (consumed_total % 8)) % 8;
178 if pad > 0 {
179 if buf.len() < pad {
180 return Err(crate::proto::error::ProtoError::Malformed(
181 "negotiate context alignment underflow",
182 ));
183 }
184 buf = &buf[pad..];
185 consumed_total += pad;
186 }
187 let mut c = Cursor::new(buf);
188 let ctx = NegotiateContext::read(&mut c)?;
189 let consumed = c.position() as usize;
190 buf = &buf[consumed..];
191 consumed_total += consumed;
192 out.push(ctx);
193 }
194 Ok(out)
195 }
196
197 pub fn encode_list(list: &[NegotiateContext], out: &mut Vec<u8>) -> ProtoResult<()> {
200 let start = out.len();
201 for (i, ctx) in list.iter().enumerate() {
202 if i > 0 {
203 let pad = (8 - ((out.len() - start) % 8)) % 8;
204 out.extend(std::iter::repeat_n(0u8, pad));
205 }
206 let mut c = Cursor::new(Vec::new());
207 BinWrite::write(ctx, &mut c)?;
208 out.extend_from_slice(&c.into_inner());
209 }
210 Ok(())
211 }
212}
213
214#[derive(Debug, Clone, PartialEq, Eq)]
218pub enum NegotiateContextData {
219 PreauthIntegrity(PreauthIntegrityCapabilities),
220 Encryption(EncryptionCapabilities),
221 Signing(SigningCapabilities),
222 Other {
224 context_type: u16,
225 data: Vec<u8>,
226 },
227}
228
229#[binrw]
231#[brw(little)]
232#[derive(Debug, Clone, PartialEq, Eq)]
233pub struct PreauthIntegrityCapabilities {
234 pub hash_algorithm_count: u16,
235 pub salt_length: u16,
236 #[br(count = hash_algorithm_count as usize)]
237 pub hash_algorithms: Vec<u16>,
238 #[br(count = salt_length as usize)]
239 pub salt: Vec<u8>,
240}
241
242impl PreauthIntegrityCapabilities {
243 pub const HASH_SHA512: u16 = 0x0001;
245}
246
247#[binrw]
249#[brw(little)]
250#[derive(Debug, Clone, PartialEq, Eq)]
251pub struct EncryptionCapabilities {
252 pub cipher_count: u16,
253 #[br(count = cipher_count as usize)]
254 pub ciphers: Vec<u16>,
255}
256
257impl EncryptionCapabilities {
258 pub const CIPHER_AES_128_CCM: u16 = 0x0001;
259 pub const CIPHER_AES_128_GCM: u16 = 0x0002;
260 pub const CIPHER_AES_256_CCM: u16 = 0x0003;
261 pub const CIPHER_AES_256_GCM: u16 = 0x0004;
262}
263
264#[binrw]
266#[brw(little)]
267#[derive(Debug, Clone, PartialEq, Eq)]
268pub struct SigningCapabilities {
269 pub signing_algorithm_count: u16,
270 #[br(count = signing_algorithm_count as usize)]
271 pub signing_algorithms: Vec<u16>,
272}
273
274impl SigningCapabilities {
275 pub const ALGORITHM_HMAC_SHA256: u16 = 0x0000;
276 pub const ALGORITHM_AES_CMAC: u16 = 0x0001;
277 pub const ALGORITHM_AES_GMAC: u16 = 0x0002;
278}
279
280#[cfg(test)]
281mod tests {
282 use super::*;
283
284 #[test]
285 fn negotiate_request_round_trips() {
286 let req = NegotiateRequest {
287 structure_size: 36,
288 dialect_count: 5,
289 security_mode: 0x0001, reserved: 0,
291 capabilities: 0x0000_007F,
292 client_guid: [0xAB; 16],
293 negotiate_context_offset_or_client_start_time: 0x0000_0070_0000_0000,
294 dialects: vec![0x0202, 0x0210, 0x0300, 0x0302, 0x0311],
295 };
296 let mut buf = Vec::new();
297 req.write_to(&mut buf).unwrap();
298 let decoded = NegotiateRequest::parse(&buf).unwrap();
299 assert_eq!(decoded, req);
300 }
301
302 #[test]
303 fn negotiate_response_round_trips() {
304 let resp = NegotiateResponse {
305 structure_size: 65,
306 security_mode: 0x0003,
307 dialect_revision: Dialect::Smb311.as_u16(),
308 negotiate_context_count_or_reserved: 3,
309 server_guid: [0xCD; 16],
310 capabilities: 0x0000_007F,
311 max_transact_size: 0x0010_0000,
312 max_read_size: 0x0010_0000,
313 max_write_size: 0x0010_0000,
314 system_time: 0x01D9_1234_5678_9ABC,
315 server_start_time: 0,
316 security_buffer_offset: 0x80,
317 security_buffer_length: 8,
318 negotiate_context_offset_or_reserved2: 0x100,
319 security_buffer: vec![1, 2, 3, 4, 5, 6, 7, 8],
320 };
321 let mut buf = Vec::new();
322 resp.write_to(&mut buf).unwrap();
323 let decoded = NegotiateResponse::parse(&buf).unwrap();
324 assert_eq!(decoded, resp);
325 }
326
327 #[test]
328 fn dialect_round_trips() {
329 for d in [
330 Dialect::Smb202,
331 Dialect::Smb210,
332 Dialect::Smb300,
333 Dialect::Smb302,
334 Dialect::Smb311,
335 Dialect::Smb2Wildcard,
336 ] {
337 assert_eq!(Dialect::from_u16(d.as_u16()), Some(d));
338 }
339 assert_eq!(Dialect::from_u16(0xBEEF), None);
340 }
341
342 #[test]
343 fn preauth_caps_round_trips() {
344 let p = PreauthIntegrityCapabilities {
345 hash_algorithm_count: 1,
346 salt_length: 32,
347 hash_algorithms: vec![PreauthIntegrityCapabilities::HASH_SHA512],
348 salt: vec![0xAA; 32],
349 };
350 let mut buf = Vec::new();
351 let mut c = Cursor::new(&mut buf);
352 BinWrite::write(&p, &mut c).unwrap();
353 let decoded = PreauthIntegrityCapabilities::read(&mut Cursor::new(&buf)).unwrap();
354 assert_eq!(decoded, p);
355 }
356
357 #[test]
358 fn negotiate_context_list_round_trips() {
359 let list = vec![
360 NegotiateContext {
361 context_type: NegotiateContext::TYPE_PREAUTH_INTEGRITY,
362 data_length: 6,
363 reserved: 0,
364 data: vec![0x01, 0x00, 0x20, 0x00, 0x01, 0x00],
365 },
366 NegotiateContext {
367 context_type: NegotiateContext::TYPE_ENCRYPTION,
368 data_length: 4,
369 reserved: 0,
370 data: vec![0x02, 0x00, 0x02, 0x00],
371 },
372 NegotiateContext {
373 context_type: NegotiateContext::TYPE_SIGNING,
374 data_length: 4,
375 reserved: 0,
376 data: vec![0x01, 0x00, 0x01, 0x00],
377 },
378 ];
379 let mut buf = Vec::new();
380 NegotiateContext::encode_list(&list, &mut buf).unwrap();
381 let parsed = NegotiateContext::parse_list(&buf, 3).unwrap();
382 assert_eq!(parsed, list);
383 }
384}