DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ntlmssp.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file ntlmssp.h
6 * @brief NTLMSSP message codec (MS-NLMP §2.2.1) for the SMB2 client (DETWS_ENABLE_SMB).
7 *
8 * The three-message NTLM handshake tokens that carry the NTLMv2 response (ntlm.h) inside SMB2
9 * SESSION_SETUP: the client sends a NEGOTIATE (type 1), the server replies with a CHALLENGE
10 * (type 2) carrying the 8-byte server challenge + the target-info AV_PAIRs, and the client sends
11 * an AUTHENTICATE (type 3) carrying the NtChallengeResponse and the user/domain. All fields
12 * little-endian; text is UTF-16LE. Pure, zero heap. This builds the raw NTLMSSP tokens; the
13 * SPNEGO/GSS wrapping + the SESSION_SETUP framing are the next increment.
14 *
15 * @author Douglas Quigg (dstroy0)
16 * @date 2026
17 */
18
19#ifndef DETERMINISTICESPASYNCWEBSERVER_NTLMSSP_H
20#define DETERMINISTICESPASYNCWEBSERVER_NTLMSSP_H
21
22#include "ServerConfig.h"
23
24#if DETWS_ENABLE_SMB
25
26#include <stddef.h>
27#include <stdint.h>
28
29/** @brief NTLMSSP NegotiateFlags (MS-NLMP §2.2.2.5), the subset a basic NTLMv2 client uses.
30 *
31 * A flags word is OR'd/AND'd, so these are integer constants in a namespacing struct, not an enum
32 * class (which would force a cast at every | / &). */
33struct NtlmsspFlags
34{
35 static constexpr uint32_t NTLMSSP_NEGOTIATE_UNICODE = 0x00000001;
36 static constexpr uint32_t NTLMSSP_REQUEST_TARGET = 0x00000004;
37 static constexpr uint32_t NTLMSSP_NEGOTIATE_NTLM = 0x00000200;
38 static constexpr uint32_t NTLMSSP_NEGOTIATE_ALWAYS_SIGN = 0x00008000;
39 static constexpr uint32_t NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY = 0x00080000;
40 static constexpr uint32_t NTLMSSP_NEGOTIATE_TARGET_INFO = 0x00800000;
41 static constexpr uint32_t NTLMSSP_NEGOTIATE_128 = 0x20000000;
42 static constexpr uint32_t NTLMSSP_NEGOTIATE_56 = 0x80000000;
43 // the default NEGOTIATE flag set for an NTLMv2 client
44 static constexpr uint32_t NTLMSSP_CLIENT_DEFAULT_FLAGS = NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_REQUEST_TARGET |
45 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
46 NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY;
47};
48
49/** @brief Parsed CHALLENGE_MESSAGE (type 2). @ref target_info points INTO the source message. */
50struct NtlmChallenge
51{
52 uint32_t flags;
53 uint8_t server_challenge[8];
54 const uint8_t *target_info; ///< the AV_PAIR blob, or nullptr if absent
55 uint16_t target_info_len;
56};
57
58/**
59 * @brief Build a NEGOTIATE_MESSAGE (type 1) with @p flags and empty domain/workstation.
60 * @return message length (32), or 0 if @p cap < 32.
61 */
62size_t ntlmssp_build_negotiate(uint8_t *buf, size_t cap, uint32_t flags);
63
64/**
65 * @brief Parse a CHALLENGE_MESSAGE (type 2): extract the flags, the 8-byte server challenge, and
66 * the target-info AV_PAIRs (bounds-checked into @p msg).
67 * @return true on a valid CHALLENGE; false on a bad signature / type / truncation / out-of-bounds
68 * target info.
69 */
70bool ntlmssp_parse_challenge(const uint8_t *msg, size_t len, NtlmChallenge *out);
71
72/**
73 * @brief Build an AUTHENTICATE_MESSAGE (type 3) carrying the LM + NT responses and the identity.
74 *
75 * @param lm_resp / lm_len the LM(v2) response (may be null/0).
76 * @param nt_resp / nt_len the NtChallengeResponse from ntlm_v2_response.
77 * @param domain / user / workstation ASCII/UTF-8 identity strings (encoded UTF-16LE); user
78 * and domain are typically required, workstation is optional (may be null).
79 * @param flags the NegotiateFlags to echo (usually the server's from the CHALLENGE).
80 * @return total message length, or 0 on overflow. No Version, no MIC, no session-key exchange.
81 */
82size_t ntlmssp_build_authenticate(uint8_t *buf, size_t cap, const uint8_t *lm_resp, size_t lm_len,
83 const uint8_t *nt_resp, size_t nt_len, const char *domain, const char *user,
84 const char *workstation, uint32_t flags);
85
86#endif // DETWS_ENABLE_SMB
87
88#endif // DETERMINISTICESPASYNCWEBSERVER_NTLMSSP_H
User-facing configuration for DeterministicESPAsyncWebServer.