1use crate::{Result, SmbError};
4
5pub mod cmd {
6 pub const NEGOTIATE: u16 = 0x0000;
7 pub const SESSION_SETUP: u16 = 0x0001;
8 pub const TREE_CONNECT: u16 = 0x0003;
9 pub const CREATE: u16 = 0x0005;
10 pub const CLOSE: u16 = 0x0006;
11 pub const READ: u16 = 0x0008;
12 pub const WRITE: u16 = 0x0009;
13 pub const IOCTL: u16 = 0x000B;
14}
15
16pub const FLAGS_SIGNED: u32 = 0x0000_0008;
17const PROTOCOL_ID: [u8; 4] = [0xFE, b'S', b'M', b'B'];
18
19#[allow(clippy::too_many_arguments)]
21pub fn build(
22 command: u16,
23 message_id: u64,
24 session_id: u64,
25 tree_id: u32,
26 signed: bool,
27) -> Vec<u8> {
28 let mut h = vec![0u8; 64];
29 h[0..4].copy_from_slice(&PROTOCOL_ID);
30 h[4..6].copy_from_slice(&64u16.to_le_bytes()); h[6..8].copy_from_slice(&1u16.to_le_bytes()); h[12..14].copy_from_slice(&command.to_le_bytes());
34 h[14..16].copy_from_slice(&1u16.to_le_bytes()); let flags = if signed { FLAGS_SIGNED } else { 0 };
36 h[16..20].copy_from_slice(&flags.to_le_bytes());
37 h[24..32].copy_from_slice(&message_id.to_le_bytes());
39 h[36..40].copy_from_slice(&tree_id.to_le_bytes());
41 h[40..48].copy_from_slice(&session_id.to_le_bytes());
42 h
44}
45
46#[derive(Clone, Copy, Debug)]
48pub struct Parsed {
49 pub command: u16,
50 pub status: u32,
51 pub session_id: u64,
52 pub tree_id: u32,
53}
54
55pub fn parse(buf: &[u8]) -> Result<Parsed> {
56 if buf.len() < 64 {
57 return Err(SmbError::Truncated);
58 }
59 if buf[0..4] != PROTOCOL_ID {
60 return Err(SmbError::BadProtocol);
61 }
62 Ok(Parsed {
63 status: u32::from_le_bytes(buf[8..12].try_into().unwrap()),
64 command: u16::from_le_bytes(buf[12..14].try_into().unwrap()),
65 tree_id: u32::from_le_bytes(buf[36..40].try_into().unwrap()),
66 session_id: u64::from_le_bytes(buf[40..48].try_into().unwrap()),
67 })
68}
69
70pub fn sign(message: &mut [u8], key: &[u8; 16]) {
73 use hmac::{Hmac, Mac};
74 use sha2::Sha256;
75 for b in &mut message[48..64] {
76 *b = 0;
77 }
78 let mut mac = <Hmac<Sha256>>::new_from_slice(key).expect("hmac key");
79 mac.update(message);
80 let sig = mac.finalize().into_bytes();
81 message[48..64].copy_from_slice(&sig[..16]);
82}
83
84pub fn sign_v3(message: &mut [u8], signing_key: &[u8; 16]) {
87 use aes::Aes128;
88 use cmac::{Cmac, Mac};
89 for b in &mut message[48..64] {
90 *b = 0;
91 }
92 let mut mac = <Cmac<Aes128>>::new_from_slice(signing_key).expect("cmac key");
93 mac.update(message);
94 let sig = mac.finalize().into_bytes();
95 message[48..64].copy_from_slice(&sig[..16]);
96}
97
98pub fn kdf_signing_key(session_key: &[u8; 16]) -> [u8; 16] {
101 use hmac::{Hmac, Mac};
102 use sha2::Sha256;
103 let mut input = Vec::new();
104 input.extend_from_slice(&1u32.to_be_bytes()); input.extend_from_slice(b"SMB2AESCMAC\0"); input.extend_from_slice(b"SmbSign\0"); input.extend_from_slice(&128u32.to_be_bytes()); let mut mac = <Hmac<Sha256>>::new_from_slice(session_key).expect("hmac key");
109 mac.update(&input);
110 let out = mac.finalize().into_bytes();
111 let mut k = [0u8; 16];
112 k.copy_from_slice(&out[..16]);
113 k
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
121 fn header_is_64_bytes_and_parses() {
122 let h = build(cmd::NEGOTIATE, 3, 0xAABB, 0x11, true);
123 assert_eq!(h.len(), 64);
124 let p = parse(&h).unwrap();
125 assert_eq!(p.command, cmd::NEGOTIATE);
126 assert_eq!(p.session_id, 0xAABB);
127 assert_eq!(p.tree_id, 0x11);
128 assert_eq!(
129 u32::from_le_bytes(h[16..20].try_into().unwrap()),
130 FLAGS_SIGNED
131 );
132 }
133}