Skip to main content

smb2_client/
header.rs

1//! SMB2 sync header (MS-SMB2 §2.2.1.2) — fixed 64 bytes, little-endian.
2
3use 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/// Build a 64-byte sync header with the signature field zeroed.
20#[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()); // StructureSize
31    h[6..8].copy_from_slice(&1u16.to_le_bytes()); // CreditCharge
32                                                  // 8..12 Status/ChannelSequence = 0
33    h[12..14].copy_from_slice(&command.to_le_bytes());
34    h[14..16].copy_from_slice(&1u16.to_le_bytes()); // CreditRequest
35    let flags = if signed { FLAGS_SIGNED } else { 0 };
36    h[16..20].copy_from_slice(&flags.to_le_bytes());
37    // 20..24 NextCommand = 0
38    h[24..32].copy_from_slice(&message_id.to_le_bytes());
39    // 32..36 Reserved (ProcessId)
40    h[36..40].copy_from_slice(&tree_id.to_le_bytes());
41    h[40..48].copy_from_slice(&session_id.to_le_bytes());
42    // 48..64 Signature = 0
43    h
44}
45
46/// Parsed fields we consume from a response header.
47#[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
70/// SMB 2.x signing: HMAC-SHA256(session_key, message-with-zeroed-sig-and-SIGNED-flag),
71/// truncated to 16 bytes, written back into the Signature field.
72pub 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
84/// SMB 3.0/3.0.2 signing: AES-128-CMAC over the message with the zeroed Signature field and
85/// the SIGNED flag set, truncated to 16 bytes.
86pub 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
98/// SMB 3.0.x signing-key derivation (MS-SMB2 §3.1.4.2): SP800-108 counter-mode KDF with
99/// HMAC-SHA256 over the session key, label "SMB2AESCMAC" and context "SmbSign".
100pub 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()); // counter i
105    input.extend_from_slice(b"SMB2AESCMAC\0"); // label
106    input.extend_from_slice(b"SmbSign\0"); // context
107    input.extend_from_slice(&128u32.to_be_bytes()); // L (bits)
108    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}