ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
smb2.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#ifndef PROTOCORE_SMB2_H
5#define PROTOCORE_SMB2_H
6
7#include "protocore_config.h" // the entry point: protocore_types.h for the widths
8
9EMBED_BEGIN_DECLS
10
11/**
12 * @file smb2.h
13 * @brief SMB2 client wire codec (MS-SMB2), PROTOCORE_ENABLE_SMB - increment 1: the transport
14frame, the 64-byte sync packet header, and the NEGOTIATE exchange.
15 *
16 * Windows-share program storage is a common CNC file path (Fanuc / Haas / Mazak / Heidenhain
17 * expose one), so a device can read/write `.nc` files over SMB2. This is the pure wire layer:
18 * build the little-endian SMB2 messages and parse the responses; the TCP socket is the
19 * application's. All fields are little-endian (SMB2 is a little-endian protocol).
20 *
21 * A client speaks SMB2 over Direct TCP (port 445): each message is prefixed by a 4-byte transport
22 * header (`0x00` + a 24-bit big-endian length), then the 64-byte SMB2 sync header (MS-SMB2
23 * §2.2.1.2), then the per-command body. The exchange begins with NEGOTIATE (§2.2.3 request /
24 * §2.2.4 response): the client offers a dialect list, the server picks one and returns the SPNEGO
25 * security token that seeds authentication.
26 *
27 * Shipped: the NEGOTIATE exchange; the NTLM crypto (smb_md / ntlm / ntlmssp); the SPNEGO wrapping
28 * (spnego); the SESSION_SETUP request/response framing that carries those tokens; and the
29 * TREE_CONNECT / CREATE / CLOSE / READ / WRITE file commands - the full read/write-a-file-on-a-share
30 * client; **SMB 2.x message signing** (protocore_smb2_sign / protocore_smb2_verify, HMAC-SHA256) wired into the
31 * client's SigningRequired path; and the **SMB 3.1.1 negotiate-context codec** (protocore_smb2_build_negotiate_311
32 * / protocore_smb2_parse_negotiate_contexts - preauth-integrity SHA-512, signing, and encryption capabilities);
33 * and the **SP800-108 counter-mode KDF** (protocore_kdf_ctr_hmac_sha256 in src/crypto/kdf, NIST-CAVP-verified) that
34 * SMB 3.x uses to derive its keys. **SMB 3.1.1 runs end to end:** the client offers 2.0.2 .. 3.1.1, chains
35 * the preauth-integrity hash (protocore_smb_preauth_*) across NEGOTIATE + both SESSION_SETUP rounds, derives the
36 * signing key (derive_signing_key), and signs the session with AES-128-CMAC (protocore_smb2_sign_cmac /
37 * _verify_cmac; crypto/aes_cmac) - the KDF assembly + CMAC cross-checked byte-for-byte against impacket.
38 *
39 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
40 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
41 * a caller drives every namespace the same way.
42 *
43 * @author Douglas Quigg (dstroy0)
44 * @date 2026
45 */
46
47// PROTOCORE_SMB2_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
48// it into its arena. Its size and its offset are each a static_assert, so a feature
49// combination that does not fit fails to compile rather than overrunning at run time.
50
51/** @brief Fixed SMB2 sync header size (MS-SMB2 §2.2.1). */
52#define PROTOCORE_SMB2_HEADER_SIZE 64
53
54/** @brief NEGOTIATE / SESSION_SETUP SecurityMode flags (MS-SMB2 §2.2.3). */
55#define SMB2_NEGOTIATE_SIGNING_ENABLED 0x0001
56#define SMB2_NEGOTIATE_SIGNING_REQUIRED 0x0002
57
58/** @brief SMB2 header Flags field (MS-SMB2 §2.2.1.2). */
59#define SMB2_FLAGS_SERVER_TO_REDIR 0x00000001 ///< set on a response (server -> client)
60#define SMB2_FLAGS_SIGNED 0x00000008 ///< the message carries an HMAC signature
61
62/** @brief SESSION_SETUP response SessionFlags (MS-SMB2 §2.2.6). */
63#define SMB2_SESSION_FLAG_IS_GUEST 0x0001
64#define SMB2_SESSION_FLAG_IS_NULL 0x0002
65#define SMB2_SESSION_FLAG_ENCRYPT_DATA 0x0004
66
67/** @brief NT status values seen in the SMB2 header during the SESSION_SETUP exchange. */
68#define SMB2_STATUS_SUCCESS 0x00000000
69#define SMB2_STATUS_MORE_PROCESSING_REQUIRED 0xC0000016 ///< server wants the next round
70#define SMB2_STATUS_END_OF_FILE 0xC0000011 ///< a READ at/past end of file
71
72/** @brief TREE_CONNECT response ShareType (MS-SMB2 §2.2.10). */
73#define SMB2_SHARE_TYPE_DISK 0x01
74#define SMB2_SHARE_TYPE_PIPE 0x02
75#define SMB2_SHARE_TYPE_PRINT 0x03
76
77/** @brief CREATE DesiredAccess masks (MS-DTYP ACCESS_MASK; the common file rights). */
78#define SMB2_FILE_READ_DATA 0x00000001
79#define SMB2_FILE_WRITE_DATA 0x00000002
80#define SMB2_FILE_APPEND_DATA 0x00000004
81#define SMB2_FILE_READ_ATTRIBUTES 0x00000080
82#define SMB2_FILE_GENERIC_READ 0x00120089 ///< RC|SYNC|READ_ATTR|READ_EA|READ_DATA
83#define SMB2_FILE_GENERIC_WRITE 0x00120116 ///< RC|SYNC|WRITE_ATTR|WRITE_EA|APPEND|WRITE
84
85/** @brief CREATE ShareAccess (MS-SMB2 §2.2.13). */
86#define SMB2_FILE_SHARE_READ 0x01
87#define SMB2_FILE_SHARE_WRITE 0x02
88#define SMB2_FILE_SHARE_DELETE 0x04
89
90/** @brief CREATE CreateDisposition (MS-SMB2 §2.2.13). */
91#define SMB2_FILE_SUPERSEDE 0
92#define SMB2_FILE_OPEN 1 ///< open an existing file, fail if absent
93#define SMB2_FILE_CREATE 2 ///< create, fail if it exists
94#define SMB2_FILE_OPEN_IF 3 ///< open, create if absent
95#define SMB2_FILE_OVERWRITE 4 ///< open + truncate, fail if absent
96#define SMB2_FILE_OVERWRITE_IF 5
97
98/** @brief CREATE CreateOptions (MS-SMB2 §2.2.13; the two we set). */
99#define SMB2_FILE_DIRECTORY_FILE 0x00000001
100#define SMB2_FILE_NON_DIRECTORY_FILE 0x00000040
101
102/** @brief SMB 3.1.1 negotiate-context types (MS-SMB2 §2.2.3.1). */
103#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES 0x0001
104#define SMB2_ENCRYPTION_CAPABILITIES 0x0002
105#define SMB2_COMPRESSION_CAPABILITIES 0x0003
106#define SMB2_NETNAME_NEGOTIATE_CONTEXT_ID 0x0005
107#define SMB2_TRANSPORT_CAPABILITIES 0x0006
108#define SMB2_RDMA_TRANSFORM_CAPABILITIES 0x0007
109#define SMB2_SIGNING_CAPABILITIES 0x0008
110
111/** @brief Preauth-integrity hash algorithm IDs (MS-SMB2 §2.2.3.1.1). */
112#define SMB2_PREAUTH_INTEGRITY_SHA512 0x0001
113
114/** @brief Signing algorithm IDs (MS-SMB2 §2.2.3.1.7). */
115#define SMB2_SIGNING_HMAC_SHA256 0x0000
116#define SMB2_SIGNING_AES_CMAC 0x0001
117#define SMB2_SIGNING_AES_GMAC 0x0002
118
119/** @brief NEGOTIATE request/response Capabilities flags (MS-SMB2 §2.2.3 / §2.2.4). A client that supports
120 * transport encryption MUST advertise SMB2_GLOBAL_CAP_ENCRYPTION here, or a server (e.g. Samba with
121 * `smb encrypt = required`) will not negotiate a cipher and will reject the unencrypted session (§3.2.4.2.2). */
122#define SMB2_GLOBAL_CAP_ENCRYPTION 0x00000040
123
124/** @brief Encryption cipher IDs (MS-SMB2 §2.2.3.1.2). */
125#define SMB2_ENCRYPTION_AES128_CCM 0x0001
126#define SMB2_ENCRYPTION_AES128_GCM 0x0002
127#define SMB2_ENCRYPTION_AES256_CCM 0x0003
128#define SMB2_ENCRYPTION_AES256_GCM 0x0004
129
130/** @brief Max encryption ciphers a NEGOTIATE request can advertise (the four SMB 3.1.1 ciphers). */
131#define PROTOCORE_SMB2_MAX_OFFER_CIPHERS 4
132
133/** @brief Length of the SMB 3.1.1 preauth-integrity hash (SHA-512 digest size). */
134#define PROTOCORE_SMB2_PREAUTH_HASH_LEN 64
135
136/** @brief TREE_CONNECT response ShareFlags of interest (MS-SMB2 §2.2.10). */
137#define SMB2_SHAREFLAG_ENCRYPT_DATA 0x00008000 ///< the share mandates SMB3 encryption
138
139/** @brief TRANSFORM_HEADER size: ProtocolId(4)+Signature(16)+Nonce(16)+OriginalMessageSize(4)+Reserved(2)+
140 * Flags(2)+SessionId(8) = 52 bytes (MS-SMB2 §2.2.41). */
141#define PROTOCORE_SMB2_TRANSFORM_HDR_LEN 52
142
143/** @brief TRANSFORM_HEADER ProtocolId 0xFD 'S' 'M' 'B' as a little-endian u32. */
144#define PROTOCORE_SMB2_TRANSFORM_PROTOCOL_ID 0x424D53FDu
145
146/** @brief The TRANSFORM_HEADER Nonce field width (MS-SMB2 §2.2.41). The AEAD uses the leading
147 * protocore_smb2_cipher_nonce_len() bytes; the rest are zero. */
148#define PROTOCORE_SMB2_NONCE_FIELD_LEN 16
149
150/** @brief AES-GCM nonce length used within the 16-byte Nonce field. */
151#define PROTOCORE_SMB2_GCM_NONCE_LEN 12
152
153/** @brief AES-CCM nonce length used within the 16-byte Nonce field (MS-SMB2 §3.1.4.3). */
154#define PROTOCORE_SMB2_CCM_NONCE_LEN 11
155
156/** @brief Largest cipher key length across the four SMB 3.1.1 ciphers (AES-256), for buffer sizing. */
157#define PROTOCORE_SMB2_MAX_CIPHER_KEY_LEN 32
158
159/** @brief SMB2 command codes (MS-SMB2 §2.2.1.2). */
160EMBED_ENUM_BYTE(Smb2Command, SMB2_NEGOTIATE = 0x0000, SMB2_SESSION_SETUP = 0x0001, SMB2_LOGOFF = 0x0002,
161 SMB2_TREE_CONNECT = 0x0003, SMB2_TREE_DISCONNECT = 0x0004, SMB2_CREATE = 0x0005, SMB2_CLOSE = 0x0006,
162 SMB2_READ = 0x0008, SMB2_WRITE = 0x0009, );
163
164/** @brief SMB2 dialect revision numbers (MS-SMB2 §2.2.4). */
165EMBED_ENUM_BYTE(Smb2Dialect,
166 SMB2_DIALECT_0202 = 0x0202, ///< SMB 2.0.2
167 SMB2_DIALECT_0210 = 0x0210, ///< SMB 2.1
168 SMB2_DIALECT_0300 = 0x0300, ///< SMB 3.0
169 SMB2_DIALECT_0302 = 0x0302, ///< SMB 3.0.2
170 SMB2_DIALECT_0311 = 0x0311, ///< SMB 3.1.1
171);
172
173/** @brief Parsed SMB2 sync header. */
174typedef struct
175{
176 Smb2Command command;
177 uint32_t status; ///< NT status (response); 0 = STATUS_SUCCESS
178 uint32_t flags;
179 uint64_t message_id;
180 uint32_t tree_id;
181 uint64_t session_id;
183} Smb2Header;
184
185/** @brief Parsed SMB 3.1.1 NEGOTIATE-response negotiate contexts (MS-SMB2 §2.2.4 / §2.2.3.1). */
186typedef struct
187{
188 embed_bool have_preauth; ///< a PREAUTH_INTEGRITY_CAPABILITIES context was present
189 uint16_t hash_algorithm; ///< the server's chosen preauth hash (expect SMB2_PREAUTH_INTEGRITY_SHA512)
190 const uint8_t *salt; ///< the preauth-integrity salt (points into msg), or nullptr
191 uint16_t salt_len; ///< length of @ref salt
192 embed_bool have_signing; ///< a SIGNING_CAPABILITIES context was present
193 uint16_t signing_algorithm; ///< the server's chosen signing algorithm
194 embed_bool have_encryption; ///< an ENCRYPTION_CAPABILITIES context was present
195 uint16_t cipher; ///< the server's chosen cipher
197
198/** @brief Parsed NEGOTIATE response (MS-SMB2 §2.2.4). */
199typedef struct
200{
202 uint16_t dialect; ///< the DialectRevision the server chose
203 uint8_t server_guid[16];
204 uint32_t capabilities;
205 uint32_t max_transact;
206 uint32_t max_read;
207 uint32_t max_write;
208 const uint8_t *sec_buf; ///< SPNEGO/NTLM security token (points into @p msg), or nullptr
209 uint16_t sec_buf_len;
211
212/**
213 * @brief The SMB 3.1.1 preauth-integrity hash value (MS-SMB2 §3.1.5.2): a running SHA-512 chained over
214 * every NEGOTIATE and SESSION_SETUP message of the handshake. Its final value binds the whole
215 * pre-authentication exchange and feeds the 3.1.1 signing / encryption key derivation.
216 */
217typedef struct
218{
220} SmbPreauth;
221
222/** @brief Parsed SESSION_SETUP response (MS-SMB2 §2.2.6). */
223typedef struct
224{
226 const uint8_t *sec_buf; ///< the server's SPNEGO/NTLM token (points into @p msg), or nullptr
227 uint16_t sec_buf_len;
229
230/** @brief Parsed TREE_CONNECT response (MS-SMB2 §2.2.10). The TreeId is in the response header. */
231typedef struct
232{
233 uint8_t share_type;
234 uint32_t share_flags;
235 uint32_t capabilities;
238
239/** @brief Parsed CREATE response (MS-SMB2 §2.2.14). */
240typedef struct
241{
242 uint8_t file_id[16]; ///< the open handle (persistent 8 + volatile 8), for READ/WRITE/CLOSE
243 uint64_t end_of_file;
247
248/** @brief Parsed CLOSE response (MS-SMB2 §2.2.16). */
249typedef struct
250{
251 uint64_t end_of_file;
254
255/** @brief Parsed READ response (MS-SMB2 §2.2.20). */
256typedef struct
257{
258 const uint8_t *data; ///< the file bytes read (points into @p msg), or nullptr when DataLength is 0
259 uint32_t data_len;
261
262/** @brief Parsed WRITE response (MS-SMB2 §2.2.22). */
263typedef struct
264{
265 uint32_t count; ///< bytes actually written
267
268/** @brief The per-session message-signing algorithm the client selects from the negotiated dialect. */
269EMBED_ENUM_BYTE(Smb2SignAlgo,
270 SMB2_SIGN_ALGO_HMAC_SHA256 = 0, ///< SMB 2.0.2 / 2.1 (key = the NTLMv2 session key)
271 SMB2_SIGN_ALGO_AES_CMAC = 1, ///< SMB 3.0 / 3.0.2 / 3.1.1 (key = the SP800-108-derived signing key)
272);
273
274/** @brief AES key length in bytes for an SMB2 cipher id: 16 for the -128 ciphers, 32 for the -256 ciphers,
275 * 0 if @p cipher is not a recognized cipher id. */
276static inline size_t protocore_smb2_cipher_key_len(uint16_t cipher)
277{
278 switch (cipher)
279 {
282 return 16;
285 return 32;
286 default:
287 return 0;
288 }
289}
290
291/** @brief AEAD nonce length in bytes for an SMB2 cipher id: 12 for the GCM ciphers, 11 for the CCM ciphers
292 * (MS-SMB2 §3.1.4.3), 0 if unrecognized. Both are written into the 16-byte TRANSFORM_HEADER Nonce
293 * field with the remaining bytes zero. */
294static inline size_t protocore_smb2_cipher_nonce_len(uint16_t cipher)
295{
296 switch (cipher)
297 {
300 return 12;
303 return 11;
304 default:
305 return 0;
306 }
307}
308
309/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
310typedef struct
311{
312 size_t (*transport_frame)(const uint8_t *, uint8_t *, size_t, const uint8_t *, size_t);
313 uint32_t (*transport_len)(const uint8_t *, const uint8_t *, size_t);
314 size_t (*build_header)(const uint8_t *, uint8_t *, size_t, Smb2Command, uint16_t, uint64_t, uint32_t, uint64_t);
315 embed_bool (*parse_header)(const uint8_t *, const uint8_t *, size_t, Smb2Header *);
316 size_t (*build_negotiate)(const uint8_t *, uint8_t *, size_t, const uint8_t *, uint16_t);
317 embed_bool (*parse_negotiate_response)(const uint8_t *, const uint8_t *, size_t, Smb2NegotiateResp *);
318 size_t (*build_negotiate_311)(const uint8_t *, uint8_t *, size_t, const uint8_t *, uint16_t, const uint8_t *,
319 size_t, const uint16_t *, size_t);
320 embed_bool (*parse_negotiate_contexts)(const uint8_t *, const uint8_t *, size_t, Smb2NegotiateContexts *);
321 void (*preauth_init)(const uint8_t *, SmbPreauth *);
322 void (*preauth_update)(const uint8_t *, uint8_t *, SmbPreauth *, const uint8_t *, size_t);
323 size_t (*build_session_setup)(const uint8_t *, uint8_t *, size_t, uint64_t, uint64_t, uint8_t, const uint8_t *,
324 size_t);
325 embed_bool (*parse_session_setup_response)(const uint8_t *, const uint8_t *, size_t, Smb2SessionSetupResp *);
326 size_t (*build_tree_connect)(const uint8_t *, uint8_t *, size_t, uint64_t, uint64_t, const uint8_t *, size_t);
327 embed_bool (*parse_tree_connect_response)(const uint8_t *, const uint8_t *, size_t, Smb2TreeConnectResp *);
328 size_t (*build_create)(const uint8_t *, uint8_t *, size_t, uint64_t, uint64_t, uint32_t, uint32_t, uint32_t,
329 uint32_t, uint32_t, const uint8_t *, size_t);
330 embed_bool (*parse_create_response)(const uint8_t *, const uint8_t *, size_t, Smb2CreateResp *);
331 size_t (*build_close)(const uint8_t *, uint8_t *, size_t, uint64_t, uint64_t, uint32_t, const uint8_t *);
332 embed_bool (*parse_close_response)(const uint8_t *, const uint8_t *, size_t, Smb2CloseResp *);
333 size_t (*build_read)(const uint8_t *, uint8_t *, size_t, uint64_t, uint64_t, uint32_t, const uint8_t *, uint32_t,
334 uint64_t);
335 embed_bool (*parse_read_response)(const uint8_t *, const uint8_t *, size_t, Smb2ReadResp *);
336 size_t (*build_write)(const uint8_t *, uint8_t *, size_t, uint64_t, uint64_t, uint32_t, const uint8_t *,
337 const uint8_t *, size_t, uint64_t);
338 embed_bool (*parse_write_response)(const uint8_t *, const uint8_t *, size_t, Smb2WriteResp *);
339 void (*sign)(const uint8_t *, uint8_t *, const uint8_t *, uint8_t *, size_t);
340 embed_bool (*verify)(const uint8_t *, uint8_t *, const uint8_t *, uint8_t *, size_t);
341 void (*sign_cmac)(const uint8_t *, uint8_t *, const uint8_t *, uint8_t *, size_t);
342 embed_bool (*verify_cmac)(const uint8_t *, uint8_t *, const uint8_t *, uint8_t *, size_t);
343 embed_bool (*derive_signing_key)(const uint8_t *, const uint8_t *, uint16_t, const uint8_t *, uint8_t *);
344 embed_bool (*derive_encryption_keys)(const uint8_t *, const uint8_t *, uint16_t, const uint8_t *, size_t, uint8_t *,
345 uint8_t *);
346 size_t (*encrypt)(const uint8_t *, uint16_t, const uint8_t *, const uint8_t *, uint64_t, const uint8_t *, size_t,
347 uint8_t *, size_t);
348 size_t (*decrypt)(const uint8_t *, uint16_t, const uint8_t *, const uint8_t *, size_t, uint8_t *, size_t);
349} Smb2Ns;
350PROTOCORE_NS_LAYOUT(Smb2Ns, transport_frame, transport_len, build_header, parse_header, build_negotiate,
351 parse_negotiate_response, build_negotiate_311, parse_negotiate_contexts, preauth_init,
352 preauth_update, build_session_setup, parse_session_setup_response, build_tree_connect,
353 parse_tree_connect_response, build_create, parse_create_response, build_close, parse_close_response,
354 build_read, parse_read_response, build_write, parse_write_response, sign, verify, sign_cmac,
355 verify_cmac, derive_signing_key, derive_encryption_keys, encrypt, decrypt);
356
357/**
358 * @brief Prefix an SMB2 message with the 4-byte Direct-TCP transport header .
359 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
360 * @param out Out
361 * @param cap Cap
362 * @param msg Msg
363 * @param msg_len Msg len
364 * @return The size_t.
365 */
366size_t protocore_smb2_transport_frame(const uint8_t *work, uint8_t *out, size_t cap, const uint8_t *msg,
367 size_t msg_len);
368/**
369 * @brief Read the Direct-TCP transport length prefix.
370 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
371 * @param buf Buf
372 * @param len Len
373 * @return The uint32_t.
374 */
375uint32_t protocore_smb2_transport_len(const uint8_t *work, const uint8_t *buf, size_t len);
376/**
377 * @brief Build a 64-byte SMB2 sync header into buf.
378 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
379 * @param buf Buf
380 * @param cap Cap
381 * @param command Command
382 * @param credit_request Credit request
383 * @param message_id Message id
384 * @param tree_id Tree id
385 * @param session_id Session id
386 * @return The size_t.
387 */
388size_t protocore_smb2_build_header(const uint8_t *work, uint8_t *buf, size_t cap, Smb2Command command,
389 uint16_t credit_request, uint64_t message_id, uint32_t tree_id, uint64_t session_id);
390/**
391 * @brief Parse a 64-byte SMB2 sync header (validates ProtocolId + .
392 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
393 * @param buf Buf
394 * @param len Len
395 * @param out Out
396 * @return EMBED_TRUE on success.
397 */
398embed_bool protocore_smb2_parse_header(const uint8_t *work, const uint8_t *buf, size_t len, Smb2Header *out);
399/**
400 * @brief Build a NEGOTIATE request (header + body) offering SMB 2.0.2 / 2.1 .
401 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
402 * @param buf Buf
403 * @param cap Cap
404 * @param client_guid the 16-byte client GUID 16 bytes
405 * @param security_mode SMB2_NEGOTIATE_SIGNING_ENABLED and/or _REQUIRED
406 * @return The size_t.
407 */
408size_t protocore_smb2_build_negotiate(const uint8_t *work, uint8_t *buf, size_t cap, const uint8_t *client_guid,
409 uint16_t security_mode);
410/**
411 * @brief Parse a NEGOTIATE response message (the SMB2 header + §2.2.4 body).
412 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
413 * @param msg the SMB2 message (starting at the sync header, transport prefix already stripped)
414 * @param len Len
415 * @param out Out
416 * @return EMBED_TRUE on success.
417 */
418embed_bool protocore_smb2_parse_negotiate_response(const uint8_t *work, const uint8_t *msg, size_t len,
419 Smb2NegotiateResp *out);
420/**
421 * @brief Build an SMB 3.1.1 NEGOTIATE request: the dialect list SMB 2.0.2 .. .
422 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
423 * @param buf Buf
424 * @param cap Cap
425 * @param client_guid 16 bytes
426 * @param security_mode Security mode
427 * @param salt the preauth-integrity salt (a fresh random blob the client keeps for the hash chain)
428 * @param salt_len salt length in bytes (>= 1); a common choice is 32
429 * @param ciphers cipher ids to offer, most-preferred first (a server picks the first it supports, in this
430 * @param cipher_count number of entries in ciphers (0 .. PROTOCORE_SMB2_MAX_OFFER_CIPHERS)
431 * @return The size_t.
432 */
433size_t protocore_smb2_build_negotiate_311(const uint8_t *work, uint8_t *buf, size_t cap, const uint8_t *client_guid,
434 uint16_t security_mode, const uint8_t *salt, size_t salt_len,
435 const uint16_t *ciphers, size_t cipher_count);
436/**
437 * @brief Walk the negotiate-context list of a 3.1.1 NEGOTIATE response .
438 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
439 * @param msg Msg
440 * @param len Len
441 * @param out Out
442 * @return EMBED_TRUE on success.
443 */
444embed_bool protocore_smb2_parse_negotiate_contexts(const uint8_t *work, const uint8_t *msg, size_t len,
446/**
447 * @brief Seed the preauth-integrity hash with 64 zero bytes (the initial .
448 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
449 * @param p P
450 */
451void protocore_smb2_preauth_init(const uint8_t *work, SmbPreauth *p);
452/**
453 * @brief Fold one handshake message into the preauth-integrity hash: hash = .
454 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
455 * @param crypto_work Crypto work
456 * @param p P
457 * @param msg Msg
458 * @param len Len
459 */
460void protocore_smb2_preauth_update(const uint8_t *work, uint8_t *crypto_work, SmbPreauth *p, const uint8_t *msg,
461 size_t len);
462/**
463 * @brief Build a SESSION_SETUP request (header + §2.2.5 body) carrying a .
464 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
465 * @param buf Buf
466 * @param cap Cap
467 * @param message_id the SMB2 MessageId (increments across the exchange)
468 * @param session_id 0 on the first round; the server-assigned SessionId on the second
469 * @param security_mode SMB2_NEGOTIATE_SIGNING_ENABLED and/or _REQUIRED (one byte on the wire)
470 * @param sec_buf Sec buf
471 * @param sec_len Sec len
472 * @return The size_t.
473 */
474size_t protocore_smb2_build_session_setup(const uint8_t *work, uint8_t *buf, size_t cap, uint64_t message_id,
475 uint64_t session_id, uint8_t security_mode, const uint8_t *sec_buf,
476 size_t sec_len);
477/**
478 * @brief Parse a SESSION_SETUP response message (the SMB2 header + §2.2.6 .
479 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
480 * @param msg the SMB2 message (starting at the sync header, transport prefix already stripped)
481 * @param len Len
482 * @param out Out
483 * @return EMBED_TRUE on success.
484 */
485embed_bool protocore_smb2_parse_session_setup_response(const uint8_t *work, const uint8_t *msg, size_t len,
487/**
488 * @brief Build a TREE_CONNECT request (header + §2.2.9 body) for a share path.
489 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
490 * @param buf Buf
491 * @param cap Cap
492 * @param message_id Message id
493 * @param session_id Session id
494 * @param path_utf16 the UNC path `\\server\share` in UTF-16LE (no NUL); path_len its byte length
495 * @param path_len Path len
496 * @return The size_t.
497 */
498size_t protocore_smb2_build_tree_connect(const uint8_t *work, uint8_t *buf, size_t cap, uint64_t message_id,
499 uint64_t session_id, const uint8_t *path_utf16, size_t path_len);
500/**
501 * @brief Parse a TREE_CONNECT response message (validates command + .
502 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
503 * @param msg Msg
504 * @param len Len
505 * @param out Out
506 * @return EMBED_TRUE on success.
507 */
508embed_bool protocore_smb2_parse_tree_connect_response(const uint8_t *work, const uint8_t *msg, size_t len,
510/**
511 * @brief Build a CREATE request (header + §2.2.13 body) to open/create a .
512 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
513 * @param buf Buf
514 * @param cap Cap
515 * @param message_id Message id
516 * @param session_id Session id
517 * @param tree_id Tree id
518 * @param desired_access e.g. SMB2_FILE_GENERIC_READ / _WRITE
519 * @param share_access SMB2_FILE_SHARE_* bitmask
520 * @param create_disposition SMB2_FILE_OPEN / _CREATE / _OPEN_IF /
521 * @param create_options SMB2_FILE_NON_DIRECTORY_FILE for a regular file
522 * @param name_utf16 the file name relative to the share root in UTF-16LE (no leading backslash, no NUL);
523 * @param name_len Name len
524 * @return The size_t.
525 */
526size_t protocore_smb2_build_create(const uint8_t *work, uint8_t *buf, size_t cap, uint64_t message_id,
527 uint64_t session_id, uint32_t tree_id, uint32_t desired_access,
528 uint32_t share_access, uint32_t create_disposition, uint32_t create_options,
529 const uint8_t *name_utf16, size_t name_len);
530/**
531 * @brief Parse a CREATE response message (validates command + StructureSize .
532 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
533 * @param msg Msg
534 * @param len Len
535 * @param out Out
536 * @return EMBED_TRUE on success.
537 */
538embed_bool protocore_smb2_parse_create_response(const uint8_t *work, const uint8_t *msg, size_t len,
539 Smb2CreateResp *out);
540/**
541 * @brief Build a CLOSE request (header + §2.2.15 body) for an open FileId.
542 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
543 * @param buf Buf
544 * @param cap Cap
545 * @param message_id Message id
546 * @param session_id Session id
547 * @param tree_id Tree id
548 * @param file_id 16 bytes
549 * @return The size_t.
550 */
551size_t protocore_smb2_build_close(const uint8_t *work, uint8_t *buf, size_t cap, uint64_t message_id,
552 uint64_t session_id, uint32_t tree_id, const uint8_t *file_id);
553/**
554 * @brief Parse a CLOSE response message (validates command + StructureSize .
555 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
556 * @param msg Msg
557 * @param len Len
558 * @param out Out
559 * @return EMBED_TRUE on success.
560 */
561embed_bool protocore_smb2_parse_close_response(const uint8_t *work, const uint8_t *msg, size_t len, Smb2CloseResp *out);
562/**
563 * @brief Build a READ request (header + §2.2.19 body) for length bytes at .
564 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
565 * @param buf Buf
566 * @param cap Cap
567 * @param message_id Message id
568 * @param session_id Session id
569 * @param tree_id Tree id
570 * @param file_id 16 bytes
571 * @param length Length
572 * @param offset Offset
573 * @return The size_t.
574 */
575size_t protocore_smb2_build_read(const uint8_t *work, uint8_t *buf, size_t cap, uint64_t message_id,
576 uint64_t session_id, uint32_t tree_id, const uint8_t *file_id, uint32_t length,
577 uint64_t offset);
578/**
579 * @brief Parse a READ response message (validates command + StructureSize .
580 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
581 * @param msg Msg
582 * @param len Len
583 * @param out Out
584 * @return EMBED_TRUE on success.
585 */
586embed_bool protocore_smb2_parse_read_response(const uint8_t *work, const uint8_t *msg, size_t len, Smb2ReadResp *out);
587/**
588 * @brief Build a WRITE request (header + §2.2.21 body) writing data at .
589 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
590 * @param buf Buf
591 * @param cap Cap
592 * @param message_id Message id
593 * @param session_id Session id
594 * @param tree_id Tree id
595 * @param file_id 16 bytes
596 * @param data Data
597 * @param data_len Data len
598 * @param offset Offset
599 * @return The size_t.
600 */
601size_t protocore_smb2_build_write(const uint8_t *work, uint8_t *buf, size_t cap, uint64_t message_id,
602 uint64_t session_id, uint32_t tree_id, const uint8_t *file_id, const uint8_t *data,
603 size_t data_len, uint64_t offset);
604/**
605 * @brief Parse a WRITE response message (validates command + StructureSize .
606 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
607 * @param msg Msg
608 * @param len Len
609 * @param out Out
610 * @return EMBED_TRUE on success.
611 */
612embed_bool protocore_smb2_parse_write_response(const uint8_t *work, const uint8_t *msg, size_t len, Smb2WriteResp *out);
613/**
614 * @brief Sign an SMB2 message in place (MS-SMB2 §3.1.4.1, SMB 2.x). Sets .
615 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
616 * @param crypto_work Crypto work
617 * @param key the session signing key (16 octets; the NTLMv2 session key for SMB 2.x) 16 bytes
618 * @param msg the full message (header + body), modified in place; must be at least a 64-byte header
619 * @param msg_len total message length. A message shorter than the header is left untouched
620 */
621void protocore_smb2_sign(const uint8_t *work, uint8_t *crypto_work, const uint8_t *key, uint8_t *msg, size_t msg_len);
622/**
623 * @brief Verify an SMB2 message's signature (MS-SMB2 §3.1.5.1). Recomputes .
624 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
625 * @param crypto_work Crypto work
626 * @param key 16 bytes
627 * @param msg Msg
628 * @param msg_len Msg len
629 * @return EMBED_TRUE on success.
630 */
631embed_bool protocore_smb2_verify(const uint8_t *work, uint8_t *crypto_work, const uint8_t *key, uint8_t *msg,
632 size_t msg_len);
633/**
634 * @brief Sign an SMB2 message in place with AES-128-CMAC (MS-SMB2 §3.1.4.1, .
635 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
636 * @param crypto_work Crypto work
637 * @param key 16 bytes
638 * @param msg Msg
639 * @param msg_len Msg len
640 */
641void protocore_smb2_sign_cmac(const uint8_t *work, uint8_t *crypto_work, const uint8_t *key, uint8_t *msg,
642 size_t msg_len);
643/**
644 * @brief Verify an AES-128-CMAC-signed SMB2 message (MS-SMB2 §3.1.5.1, SMB .
645 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
646 * @param crypto_work Crypto work
647 * @param key 16 bytes
648 * @param msg Msg
649 * @param msg_len Msg len
650 * @return EMBED_TRUE on success.
651 */
652embed_bool protocore_smb2_verify_cmac(const uint8_t *work, uint8_t *crypto_work, const uint8_t *key, uint8_t *msg,
653 size_t msg_len);
654/**
655 * @brief Derive the 16-byte SMB 3.x signing key from the NTLM session key .
656 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
657 * @param session_key the 16-byte NTLM ExportedSessionKey (SessionBaseKey for NTLMv2 with no key exch) 16 bytes
658 * @param dialect the negotiated DialectRevision (only 3.1.1 vs pre-3.1.1 matters here)
659 * @param preauth the 64-byte final preauth-integrity hash; required iff dialect == 3.1.1, else ignored
660 * @param out_key receives the 16-byte signing key 16 bytes
661 * @return EMBED_TRUE on success.
662 */
663embed_bool protocore_smb2_derive_signing_key(const uint8_t *work, const uint8_t *session_key, uint16_t dialect,
664 const uint8_t *preauth, uint8_t *out_key);
665/**
666 * @brief Derive the two SMB 3.x cipher keys from the NTLM session key .
667 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
668 * @param session_key 16 bytes
669 * @param dialect Dialect
670 * @param preauth Preauth
671 * @param key_len Key len
672 * @param out_c2s client->server key (ENCRYPTS our requests); out_s2c server->client key (DECRYPTS
673 * @param out_s2c Out s2c
674 * @return EMBED_TRUE on success.
675 */
676embed_bool protocore_smb2_derive_encryption_keys(const uint8_t *work, const uint8_t *session_key, uint16_t dialect,
677 const uint8_t *preauth, size_t key_len, uint8_t *out_c2s,
678 uint8_t *out_s2c);
679/**
680 * @brief Encrypt one SMB2 message into a TRANSFORM_HEADER-wrapped blob .
681 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
682 * @param cipher one of Smb2Cipher; selects the key length and AEAD nonce length
683 * @param key cipher key (protocore_smb2_cipher_key_len(cipher) bytes, i.e. the C2S key)
684 * @param nonce the 16-byte Nonce field; the leading nonce-length bytes must be UNIQUE per key (caller
685 * @param session_id echoed into the header; out needs >= PROTOCORE_SMB2_TRANSFORM_HDR_LEN + msg_len
686 * @param msg Msg
687 * @param msg_len Msg len
688 * @param out Out
689 * @param out_cap Out cap
690 * @return The size_t.
691 */
692size_t protocore_smb2_encrypt(const uint8_t *work, uint16_t cipher, const uint8_t *key, const uint8_t *nonce,
693 uint64_t session_id, const uint8_t *msg, size_t msg_len, uint8_t *out, size_t out_cap);
694/**
695 * @brief Decrypt a TRANSFORM_HEADER-wrapped SMB2 message (MS-SMB2 §3.1.4.4): .
696 * @param work PROTOCORE_SMB2_BORROW bytes the caller took. Not held past the call.
697 * @param cipher one of Smb2Cipher; key the S2C cipher key; out needs >= OriginalMessageSize
698 * @param key Key
699 * @param in In
700 * @param in_len In len
701 * @param out Out
702 * @param out_cap Out cap
703 * @return The size_t.
704 */
705size_t protocore_smb2_decrypt(const uint8_t *work, uint16_t cipher, const uint8_t *key, const uint8_t *in,
706 size_t in_len, uint8_t *out, size_t out_cap);
707
708/** @brief Module namespace. */
710 .transport_len = protocore_smb2_transport_len,
711 .build_header = protocore_smb2_build_header,
712 .parse_header = protocore_smb2_parse_header,
713 .build_negotiate = protocore_smb2_build_negotiate,
714 .parse_negotiate_response = protocore_smb2_parse_negotiate_response,
715 .build_negotiate_311 = protocore_smb2_build_negotiate_311,
716 .parse_negotiate_contexts = protocore_smb2_parse_negotiate_contexts,
717 .preauth_init = protocore_smb2_preauth_init,
718 .preauth_update = protocore_smb2_preauth_update,
719 .build_session_setup = protocore_smb2_build_session_setup,
720 .parse_session_setup_response =
722 .build_tree_connect = protocore_smb2_build_tree_connect,
723 .parse_tree_connect_response = protocore_smb2_parse_tree_connect_response,
724 .build_create = protocore_smb2_build_create,
725 .parse_create_response = protocore_smb2_parse_create_response,
726 .build_close = protocore_smb2_build_close,
727 .parse_close_response = protocore_smb2_parse_close_response,
728 .build_read = protocore_smb2_build_read,
729 .parse_read_response = protocore_smb2_parse_read_response,
730 .build_write = protocore_smb2_build_write,
731 .parse_write_response = protocore_smb2_parse_write_response,
732 .sign = protocore_smb2_sign,
733 .verify = protocore_smb2_verify,
734 .sign_cmac = protocore_smb2_sign_cmac,
735 .verify_cmac = protocore_smb2_verify_cmac,
736 .derive_signing_key = protocore_smb2_derive_signing_key,
737 .derive_encryption_keys = protocore_smb2_derive_encryption_keys,
738 .encrypt = protocore_smb2_encrypt,
739 .decrypt = protocore_smb2_decrypt};
740
741EMBED_END_DECLS
742
743#endif // PROTOCORE_SMB2_H
#define PROTOCORE_UNUSED
Suppress the unused warning. Every module ends in a namespace most callers only partly use.
Definition ns_contract.h:36
#define PROTOCORE_NS_LAYOUT(T,...)
Pin every dispatch slot of a table that is nothing but function pointers.
#define PROTOCORE_NS
Storage for a dispatch table. The const is load bearing.
size_t protocore_smb2_build_close(const uint8_t *work, uint8_t *buf, size_t cap, uint64_t message_id, uint64_t session_id, uint32_t tree_id, const uint8_t *file_id)
Build a CLOSE request (header + §2.2.15 body) for an open FileId.
uint32_t protocore_smb2_transport_len(const uint8_t *work, const uint8_t *buf, size_t len)
Read the Direct-TCP transport length prefix.
size_t protocore_smb2_encrypt(const uint8_t *work, uint16_t cipher, const uint8_t *key, const uint8_t *nonce, uint64_t session_id, const uint8_t *msg, size_t msg_len, uint8_t *out, size_t out_cap)
Encrypt one SMB2 message into a TRANSFORM_HEADER-wrapped blob .
size_t protocore_smb2_build_tree_connect(const uint8_t *work, uint8_t *buf, size_t cap, uint64_t message_id, uint64_t session_id, const uint8_t *path_utf16, size_t path_len)
Build a TREE_CONNECT request (header + §2.2.9 body) for a share path.
EMBED_ENUM_BYTE(Smb2Command, SMB2_NEGOTIATE=0x0000, SMB2_SESSION_SETUP=0x0001, SMB2_LOGOFF=0x0002, SMB2_TREE_CONNECT=0x0003, SMB2_TREE_DISCONNECT=0x0004, SMB2_CREATE=0x0005, SMB2_CLOSE=0x0006, SMB2_READ=0x0008, SMB2_WRITE=0x0009,)
SMB2 command codes (MS-SMB2 §2.2.1.2).
embed_bool protocore_smb2_derive_signing_key(const uint8_t *work, const uint8_t *session_key, uint16_t dialect, const uint8_t *preauth, uint8_t *out_key)
Derive the 16-byte SMB 3.x signing key from the NTLM session key .
size_t protocore_smb2_build_read(const uint8_t *work, uint8_t *buf, size_t cap, uint64_t message_id, uint64_t session_id, uint32_t tree_id, const uint8_t *file_id, uint32_t length, uint64_t offset)
Build a READ request (header + §2.2.19 body) for length bytes at .
size_t protocore_smb2_build_negotiate(const uint8_t *work, uint8_t *buf, size_t cap, const uint8_t *client_guid, uint16_t security_mode)
Build a NEGOTIATE request (header + body) offering SMB 2.0.2 / 2.1 .
embed_bool protocore_smb2_verify(const uint8_t *work, uint8_t *crypto_work, const uint8_t *key, uint8_t *msg, size_t msg_len)
Verify an SMB2 message's signature (MS-SMB2 §3.1.5.1). Recomputes .
void protocore_smb2_sign(const uint8_t *work, uint8_t *crypto_work, const uint8_t *key, uint8_t *msg, size_t msg_len)
Sign an SMB2 message in place (MS-SMB2 §3.1.4.1, SMB 2.x). Sets .
#define SMB2_ENCRYPTION_AES128_CCM
Encryption cipher IDs (MS-SMB2 §2.2.3.1.2).
Definition smb2.h:125
size_t protocore_smb2_build_session_setup(const uint8_t *work, uint8_t *buf, size_t cap, uint64_t message_id, uint64_t session_id, uint8_t security_mode, const uint8_t *sec_buf, size_t sec_len)
Build a SESSION_SETUP request (header + §2.2.5 body) carrying a .
embed_bool protocore_smb2_derive_encryption_keys(const uint8_t *work, const uint8_t *session_key, uint16_t dialect, const uint8_t *preauth, size_t key_len, uint8_t *out_c2s, uint8_t *out_s2c)
Derive the two SMB 3.x cipher keys from the NTLM session key .
embed_bool protocore_smb2_verify_cmac(const uint8_t *work, uint8_t *crypto_work, const uint8_t *key, uint8_t *msg, size_t msg_len)
Verify an AES-128-CMAC-signed SMB2 message (MS-SMB2 §3.1.5.1, SMB .
size_t protocore_smb2_build_negotiate_311(const uint8_t *work, uint8_t *buf, size_t cap, const uint8_t *client_guid, uint16_t security_mode, const uint8_t *salt, size_t salt_len, const uint16_t *ciphers, size_t cipher_count)
Build an SMB 3.1.1 NEGOTIATE request: the dialect list SMB 2.0.2 .. .
size_t protocore_smb2_build_header(const uint8_t *work, uint8_t *buf, size_t cap, Smb2Command command, uint16_t credit_request, uint64_t message_id, uint32_t tree_id, uint64_t session_id)
Build a 64-byte SMB2 sync header into buf.
embed_bool protocore_smb2_parse_tree_connect_response(const uint8_t *work, const uint8_t *msg, size_t len, Smb2TreeConnectResp *out)
Parse a TREE_CONNECT response message (validates command + .
#define SMB2_ENCRYPTION_AES256_GCM
Definition smb2.h:128
embed_bool protocore_smb2_parse_read_response(const uint8_t *work, const uint8_t *msg, size_t len, Smb2ReadResp *out)
Parse a READ response message (validates command + StructureSize .
size_t protocore_smb2_build_write(const uint8_t *work, uint8_t *buf, size_t cap, uint64_t message_id, uint64_t session_id, uint32_t tree_id, const uint8_t *file_id, const uint8_t *data, size_t data_len, uint64_t offset)
Build a WRITE request (header + §2.2.21 body) writing data at .
void protocore_smb2_preauth_init(const uint8_t *work, SmbPreauth *p)
Seed the preauth-integrity hash with 64 zero bytes (the initial .
embed_bool protocore_smb2_parse_header(const uint8_t *work, const uint8_t *buf, size_t len, Smb2Header *out)
Parse a 64-byte SMB2 sync header (validates ProtocolId + .
embed_bool protocore_smb2_parse_negotiate_response(const uint8_t *work, const uint8_t *msg, size_t len, Smb2NegotiateResp *out)
Parse a NEGOTIATE response message (the SMB2 header + §2.2.4 body).
embed_bool protocore_smb2_parse_write_response(const uint8_t *work, const uint8_t *msg, size_t len, Smb2WriteResp *out)
Parse a WRITE response message (validates command + StructureSize .
size_t protocore_smb2_decrypt(const uint8_t *work, uint16_t cipher, const uint8_t *key, const uint8_t *in, size_t in_len, uint8_t *out, size_t out_cap)
Decrypt a TRANSFORM_HEADER-wrapped SMB2 message (MS-SMB2 §3.1.4.4): .
embed_bool protocore_smb2_parse_close_response(const uint8_t *work, const uint8_t *msg, size_t len, Smb2CloseResp *out)
Parse a CLOSE response message (validates command + StructureSize .
embed_bool protocore_smb2_parse_create_response(const uint8_t *work, const uint8_t *msg, size_t len, Smb2CreateResp *out)
Parse a CREATE response message (validates command + StructureSize .
#define PROTOCORE_SMB2_PREAUTH_HASH_LEN
Length of the SMB 3.1.1 preauth-integrity hash (SHA-512 digest size).
Definition smb2.h:134
void protocore_smb2_sign_cmac(const uint8_t *work, uint8_t *crypto_work, const uint8_t *key, uint8_t *msg, size_t msg_len)
Sign an SMB2 message in place with AES-128-CMAC (MS-SMB2 §3.1.4.1, .
#define SMB2_ENCRYPTION_AES128_GCM
Definition smb2.h:126
size_t protocore_smb2_build_create(const uint8_t *work, uint8_t *buf, size_t cap, uint64_t message_id, uint64_t session_id, uint32_t tree_id, uint32_t desired_access, uint32_t share_access, uint32_t create_disposition, uint32_t create_options, const uint8_t *name_utf16, size_t name_len)
Build a CREATE request (header + §2.2.13 body) to open/create a .
embed_bool protocore_smb2_parse_session_setup_response(const uint8_t *work, const uint8_t *msg, size_t len, Smb2SessionSetupResp *out)
Parse a SESSION_SETUP response message (the SMB2 header + §2.2.6 .
void protocore_smb2_preauth_update(const uint8_t *work, uint8_t *crypto_work, SmbPreauth *p, const uint8_t *msg, size_t len)
Fold one handshake message into the preauth-integrity hash: hash = .
embed_bool protocore_smb2_parse_negotiate_contexts(const uint8_t *work, const uint8_t *msg, size_t len, Smb2NegotiateContexts *out)
Walk the negotiate-context list of a 3.1.1 NEGOTIATE response .
#define SMB2_ENCRYPTION_AES256_CCM
Definition smb2.h:127
size_t protocore_smb2_transport_frame(const uint8_t *work, uint8_t *out, size_t cap, const uint8_t *msg, size_t msg_len)
Prefix an SMB2 message with the 4-byte Direct-TCP transport header .
Parsed CLOSE response (MS-SMB2 §2.2.16).
Definition smb2.h:250
uint32_t file_attributes
Definition smb2.h:252
uint64_t end_of_file
Definition smb2.h:251
Parsed CREATE response (MS-SMB2 §2.2.14).
Definition smb2.h:241
uint64_t end_of_file
Definition smb2.h:243
uint32_t create_action
Definition smb2.h:244
uint32_t file_attributes
Definition smb2.h:245
uint8_t file_id[16]
the open handle (persistent 8 + volatile 8), for READ/WRITE/CLOSE
Definition smb2.h:242
Parsed SMB2 sync header.
Definition smb2.h:175
uint32_t status
NT status (response); 0 = STATUS_SUCCESS.
Definition smb2.h:177
uint64_t message_id
Definition smb2.h:179
uint64_t session_id
Definition smb2.h:181
uint32_t flags
Definition smb2.h:178
uint32_t tree_id
Definition smb2.h:180
uint16_t credit_response
Definition smb2.h:182
Smb2Command command
Definition smb2.h:176
Parsed SMB 3.1.1 NEGOTIATE-response negotiate contexts (MS-SMB2 §2.2.4 / §2.2.3.1).
Definition smb2.h:187
uint16_t salt_len
length of salt
Definition smb2.h:191
uint16_t hash_algorithm
the server's chosen preauth hash (expect SMB2_PREAUTH_INTEGRITY_SHA512)
Definition smb2.h:189
embed_bool have_encryption
an ENCRYPTION_CAPABILITIES context was present
Definition smb2.h:194
uint16_t signing_algorithm
the server's chosen signing algorithm
Definition smb2.h:193
uint16_t cipher
the server's chosen cipher
Definition smb2.h:195
embed_bool have_signing
a SIGNING_CAPABILITIES context was present
Definition smb2.h:192
embed_bool have_preauth
a PREAUTH_INTEGRITY_CAPABILITIES context was present
Definition smb2.h:188
const uint8_t * salt
the preauth-integrity salt (points into msg), or nullptr
Definition smb2.h:190
Parsed NEGOTIATE response (MS-SMB2 §2.2.4).
Definition smb2.h:200
uint32_t max_write
Definition smb2.h:207
uint32_t capabilities
Definition smb2.h:204
uint16_t dialect
the DialectRevision the server chose
Definition smb2.h:202
uint8_t server_guid[16]
Definition smb2.h:203
uint16_t security_mode
Definition smb2.h:201
const uint8_t * sec_buf
SPNEGO/NTLM security token (points into msg), or nullptr.
Definition smb2.h:208
uint16_t sec_buf_len
Definition smb2.h:209
uint32_t max_transact
Definition smb2.h:205
uint32_t max_read
Definition smb2.h:206
Dispatch table. Addressed by offset, so the layout is asserted below.
Definition smb2.h:311
size_t(* build_tree_connect)(const uint8_t *, uint8_t *, size_t, uint64_t, uint64_t, const uint8_t *, size_t)
Definition smb2.h:326
size_t(* build_session_setup)(const uint8_t *, uint8_t *, size_t, uint64_t, uint64_t, uint8_t, const uint8_t *, size_t)
Definition smb2.h:323
embed_bool(* verify)(const uint8_t *, uint8_t *, const uint8_t *, uint8_t *, size_t)
Definition smb2.h:340
embed_bool(* parse_write_response)(const uint8_t *, const uint8_t *, size_t, Smb2WriteResp *)
Definition smb2.h:338
size_t(* build_read)(const uint8_t *, uint8_t *, size_t, uint64_t, uint64_t, uint32_t, const uint8_t *, uint32_t, uint64_t)
Definition smb2.h:333
embed_bool(* derive_encryption_keys)(const uint8_t *, const uint8_t *, uint16_t, const uint8_t *, size_t, uint8_t *, uint8_t *)
Definition smb2.h:344
size_t(* transport_frame)(const uint8_t *, uint8_t *, size_t, const uint8_t *, size_t)
Definition smb2.h:312
embed_bool(* parse_create_response)(const uint8_t *, const uint8_t *, size_t, Smb2CreateResp *)
Definition smb2.h:330
size_t(* build_close)(const uint8_t *, uint8_t *, size_t, uint64_t, uint64_t, uint32_t, const uint8_t *)
Definition smb2.h:331
size_t(* decrypt)(const uint8_t *, uint16_t, const uint8_t *, const uint8_t *, size_t, uint8_t *, size_t)
Definition smb2.h:348
void(* sign)(const uint8_t *, uint8_t *, const uint8_t *, uint8_t *, size_t)
Definition smb2.h:339
size_t(* build_negotiate_311)(const uint8_t *, uint8_t *, size_t, const uint8_t *, uint16_t, const uint8_t *, size_t, const uint16_t *, size_t)
Definition smb2.h:318
uint32_t(* transport_len)(const uint8_t *, const uint8_t *, size_t)
Definition smb2.h:313
void(* preauth_init)(const uint8_t *, SmbPreauth *)
Definition smb2.h:321
embed_bool(* parse_session_setup_response)(const uint8_t *, const uint8_t *, size_t, Smb2SessionSetupResp *)
Definition smb2.h:325
embed_bool(* parse_close_response)(const uint8_t *, const uint8_t *, size_t, Smb2CloseResp *)
Definition smb2.h:332
size_t(* build_write)(const uint8_t *, uint8_t *, size_t, uint64_t, uint64_t, uint32_t, const uint8_t *, const uint8_t *, size_t, uint64_t)
Definition smb2.h:336
void(* sign_cmac)(const uint8_t *, uint8_t *, const uint8_t *, uint8_t *, size_t)
Definition smb2.h:341
size_t(* build_create)(const uint8_t *, uint8_t *, size_t, uint64_t, uint64_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, const uint8_t *, size_t)
Definition smb2.h:328
embed_bool(* verify_cmac)(const uint8_t *, uint8_t *, const uint8_t *, uint8_t *, size_t)
Definition smb2.h:342
void(* preauth_update)(const uint8_t *, uint8_t *, SmbPreauth *, const uint8_t *, size_t)
Definition smb2.h:322
size_t(* build_header)(const uint8_t *, uint8_t *, size_t, Smb2Command, uint16_t, uint64_t, uint32_t, uint64_t)
Definition smb2.h:314
embed_bool(* derive_signing_key)(const uint8_t *, const uint8_t *, uint16_t, const uint8_t *, uint8_t *)
Definition smb2.h:343
embed_bool(* parse_negotiate_response)(const uint8_t *, const uint8_t *, size_t, Smb2NegotiateResp *)
Definition smb2.h:317
embed_bool(* parse_tree_connect_response)(const uint8_t *, const uint8_t *, size_t, Smb2TreeConnectResp *)
Definition smb2.h:327
embed_bool(* parse_read_response)(const uint8_t *, const uint8_t *, size_t, Smb2ReadResp *)
Definition smb2.h:335
size_t(* build_negotiate)(const uint8_t *, uint8_t *, size_t, const uint8_t *, uint16_t)
Definition smb2.h:316
embed_bool(* parse_header)(const uint8_t *, const uint8_t *, size_t, Smb2Header *)
Definition smb2.h:315
embed_bool(* parse_negotiate_contexts)(const uint8_t *, const uint8_t *, size_t, Smb2NegotiateContexts *)
Definition smb2.h:320
size_t(* encrypt)(const uint8_t *, uint16_t, const uint8_t *, const uint8_t *, uint64_t, const uint8_t *, size_t, uint8_t *, size_t)
Definition smb2.h:346
Parsed READ response (MS-SMB2 §2.2.20).
Definition smb2.h:257
uint32_t data_len
Definition smb2.h:259
const uint8_t * data
the file bytes read (points into msg), or nullptr when DataLength is 0
Definition smb2.h:258
Parsed SESSION_SETUP response (MS-SMB2 §2.2.6).
Definition smb2.h:224
uint16_t session_flags
Definition smb2.h:225
uint16_t sec_buf_len
Definition smb2.h:227
const uint8_t * sec_buf
the server's SPNEGO/NTLM token (points into msg), or nullptr
Definition smb2.h:226
Parsed TREE_CONNECT response (MS-SMB2 §2.2.10). The TreeId is in the response header.
Definition smb2.h:232
uint32_t maximal_access
Definition smb2.h:236
uint8_t share_type
Definition smb2.h:233
uint32_t share_flags
Definition smb2.h:234
uint32_t capabilities
Definition smb2.h:235
Parsed WRITE response (MS-SMB2 §2.2.22).
Definition smb2.h:264
uint32_t count
bytes actually written
Definition smb2.h:265
The SMB 3.1.1 preauth-integrity hash value (MS-SMB2 §3.1.5.2): a running SHA-512 chained over every N...
Definition smb2.h:218
uint8_t hash[PROTOCORE_SMB2_PREAUTH_HASH_LEN]
Definition smb2.h:219