2 Unix SMB/Netbios implementation.
4 handle NLTMSSP, client server side parsing
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8 Copyright (C) Stefan Metzmacher 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "auth/ntlmssp/ntlmssp.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "librpc/gen_ndr/ndr_dcerpc.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/gensec/gensec_proto.h"
32 * Callbacks for NTLMSSP - for both client and server operating modes
36 static const struct ntlmssp_callbacks
{
37 enum ntlmssp_role role
;
38 enum ntlmssp_message_type command
;
39 NTSTATUS (*sync_fn
)(struct gensec_security
*gensec_security
,
40 TALLOC_CTX
*out_mem_ctx
,
41 DATA_BLOB in
, DATA_BLOB
*out
);
42 } ntlmssp_callbacks
[] = {
44 .role
= NTLMSSP_CLIENT
,
45 .command
= NTLMSSP_INITIAL
,
46 .sync_fn
= ntlmssp_client_initial
,
48 .role
= NTLMSSP_SERVER
,
49 .command
= NTLMSSP_NEGOTIATE
,
50 .sync_fn
= ntlmssp_server_negotiate
,
52 .role
= NTLMSSP_CLIENT
,
53 .command
= NTLMSSP_CHALLENGE
,
54 .sync_fn
= ntlmssp_client_challenge
,
56 .role
= NTLMSSP_SERVER
,
57 .command
= NTLMSSP_AUTH
,
58 .sync_fn
= ntlmssp_server_auth
,
64 * Print out the NTLMSSP flags for debugging
65 * @param neg_flags The flags from the packet
68 void debug_ntlmssp_flags(uint32_t neg_flags
)
70 DEBUG(3,("Got NTLMSSP neg_flags=0x%08x\n", neg_flags
));
72 if (neg_flags
& NTLMSSP_NEGOTIATE_UNICODE
)
73 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_UNICODE\n"));
74 if (neg_flags
& NTLMSSP_NEGOTIATE_OEM
)
75 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM\n"));
76 if (neg_flags
& NTLMSSP_REQUEST_TARGET
)
77 DEBUGADD(4, (" NTLMSSP_REQUEST_TARGET\n"));
78 if (neg_flags
& NTLMSSP_NEGOTIATE_SIGN
)
79 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SIGN\n"));
80 if (neg_flags
& NTLMSSP_NEGOTIATE_SEAL
)
81 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SEAL\n"));
82 if (neg_flags
& NTLMSSP_NEGOTIATE_DATAGRAM
)
83 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_DATAGRAM\n"));
84 if (neg_flags
& NTLMSSP_NEGOTIATE_LM_KEY
)
85 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_LM_KEY\n"));
86 if (neg_flags
& NTLMSSP_NEGOTIATE_NETWARE
)
87 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NETWARE\n"));
88 if (neg_flags
& NTLMSSP_NEGOTIATE_NTLM
)
89 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM\n"));
90 if (neg_flags
& NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED
)
91 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED\n"));
92 if (neg_flags
& NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED
)
93 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED\n"));
94 if (neg_flags
& NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL
)
95 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL\n"));
96 if (neg_flags
& NTLMSSP_NEGOTIATE_ALWAYS_SIGN
)
97 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_ALWAYS_SIGN\n"));
98 if (neg_flags
& NTLMSSP_REQUEST_NON_NT_SESSION_KEY
)
99 DEBUGADD(4, (" NTLMSSP_REQUEST_NON_NT_SESSION_KEY\n"));
100 if (neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
)
101 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM2\n"));
102 if (neg_flags
& NTLMSSP_NEGOTIATE_TARGET_INFO
)
103 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_TARGET_INFO\n"));
104 if (neg_flags
& NTLMSSP_NEGOTIATE_128
)
105 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_128\n"));
106 if (neg_flags
& NTLMSSP_NEGOTIATE_KEY_EXCH
)
107 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_KEY_EXCH\n"));
108 if (neg_flags
& NTLMSSP_NEGOTIATE_56
)
109 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_56\n"));
112 static NTSTATUS
gensec_ntlmssp_magic(struct gensec_security
*gensec_security
,
113 const DATA_BLOB
*first_packet
)
115 if (first_packet
->length
> 8 && memcmp("NTLMSSP\0", first_packet
->data
, 8) == 0) {
118 return NT_STATUS_INVALID_PARAMETER
;
122 static NTSTATUS
gensec_ntlmssp_update_find(struct ntlmssp_state
*ntlmssp_state
,
123 const DATA_BLOB input
, uint32_t *idx
)
125 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
126 talloc_get_type_abort(ntlmssp_state
->callback_private
,
127 struct gensec_ntlmssp_context
);
128 struct gensec_security
*gensec_security
= gensec_ntlmssp
->gensec_security
;
129 uint32_t ntlmssp_command
;
132 if (ntlmssp_state
->expected_state
== NTLMSSP_DONE
) {
133 /* We are strict here because other modules, which we
134 * don't fully control (such as GSSAPI) are also
135 * strict, but are tested less often */
137 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
138 return NT_STATUS_INVALID_PARAMETER
;
142 switch (ntlmssp_state
->role
) {
144 ntlmssp_command
= NTLMSSP_INITIAL
;
147 if (gensec_security
->want_features
& GENSEC_FEATURE_DATAGRAM_MODE
) {
148 /* 'datagram' mode - no neg packet */
149 ntlmssp_command
= NTLMSSP_NEGOTIATE
;
151 /* This is normal in SPNEGO mech negotiation fallback */
152 DEBUG(2, ("Failed to parse NTLMSSP packet: zero length\n"));
153 return NT_STATUS_INVALID_PARAMETER
;
158 if (!msrpc_parse(ntlmssp_state
,
162 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
163 dump_data(2, input
.data
, input
.length
);
164 return NT_STATUS_INVALID_PARAMETER
;
168 if (ntlmssp_command
!= ntlmssp_state
->expected_state
) {
169 DEBUG(2, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command
, ntlmssp_state
->expected_state
));
170 return NT_STATUS_INVALID_PARAMETER
;
173 for (i
=0; i
< ARRAY_SIZE(ntlmssp_callbacks
); i
++) {
174 if (ntlmssp_callbacks
[i
].role
== ntlmssp_state
->role
&&
175 ntlmssp_callbacks
[i
].command
== ntlmssp_command
) {
181 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
182 ntlmssp_state
->role
, ntlmssp_command
));
184 return NT_STATUS_INVALID_PARAMETER
;
188 * Next state function for the wrapped NTLMSSP state machine
190 * @param gensec_security GENSEC state, initialised to NTLMSSP
191 * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
192 * @param in The request, as a DATA_BLOB
193 * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
194 * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent,
195 * or NT_STATUS_OK if the user is authenticated.
198 static NTSTATUS
gensec_ntlmssp_update(struct gensec_security
*gensec_security
,
199 TALLOC_CTX
*out_mem_ctx
,
200 const DATA_BLOB input
, DATA_BLOB
*out
)
202 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
203 talloc_get_type_abort(gensec_security
->private_data
,
204 struct gensec_ntlmssp_context
);
205 struct ntlmssp_state
*ntlmssp_state
= gensec_ntlmssp
->ntlmssp_state
;
209 *out
= data_blob(NULL
, 0);
212 /* if the caller doesn't want to manage/own the memory,
213 we can put it on our context */
214 out_mem_ctx
= ntlmssp_state
;
217 status
= gensec_ntlmssp_update_find(ntlmssp_state
, input
, &i
);
218 NT_STATUS_NOT_OK_RETURN(status
);
220 status
= ntlmssp_callbacks
[i
].sync_fn(gensec_security
, out_mem_ctx
, input
, out
);
221 NT_STATUS_NOT_OK_RETURN(status
);
227 * Return the NTLMSSP master session key
229 * @param ntlmssp_state NTLMSSP State
232 NTSTATUS
gensec_ntlmssp_session_key(struct gensec_security
*gensec_security
,
233 DATA_BLOB
*session_key
)
235 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
236 talloc_get_type_abort(gensec_security
->private_data
,
237 struct gensec_ntlmssp_context
);
238 struct ntlmssp_state
*ntlmssp_state
= gensec_ntlmssp
->ntlmssp_state
;
240 if (ntlmssp_state
->expected_state
!= NTLMSSP_DONE
) {
241 return NT_STATUS_NO_USER_SESSION_KEY
;
244 if (!ntlmssp_state
->session_key
.data
) {
245 return NT_STATUS_NO_USER_SESSION_KEY
;
247 *session_key
= ntlmssp_state
->session_key
;
252 void ntlmssp_handle_neg_flags(struct ntlmssp_state
*ntlmssp_state
,
253 uint32_t neg_flags
, bool allow_lm
)
255 if (neg_flags
& NTLMSSP_NEGOTIATE_UNICODE
) {
256 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_UNICODE
;
257 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_OEM
;
258 ntlmssp_state
->unicode
= true;
260 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_UNICODE
;
261 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_OEM
;
262 ntlmssp_state
->unicode
= false;
265 if ((neg_flags
& NTLMSSP_NEGOTIATE_LM_KEY
) && allow_lm
&& !ntlmssp_state
->use_ntlmv2
) {
266 /* other end forcing us to use LM */
267 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_LM_KEY
;
268 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_NTLM2
;
270 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_LM_KEY
;
273 if (!(neg_flags
& NTLMSSP_NEGOTIATE_ALWAYS_SIGN
)) {
274 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_ALWAYS_SIGN
;
277 if (!(neg_flags
& NTLMSSP_NEGOTIATE_SIGN
)) {
278 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_SIGN
;
281 if (!(neg_flags
& NTLMSSP_NEGOTIATE_SEAL
)) {
282 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_SEAL
;
285 if (!(neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
)) {
286 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_NTLM2
;
289 if (!(neg_flags
& NTLMSSP_NEGOTIATE_128
)) {
290 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_128
;
293 if (!(neg_flags
& NTLMSSP_NEGOTIATE_56
)) {
294 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_56
;
297 if (!(neg_flags
& NTLMSSP_NEGOTIATE_KEY_EXCH
)) {
298 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_KEY_EXCH
;
301 /* Woop Woop - unknown flag for Windows compatibility...
302 What does this really do ? JRA. */
303 if (!(neg_flags
& NTLMSSP_NEGOTIATE_VERSION
)) {
304 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_VERSION
;
307 if ((neg_flags
& NTLMSSP_REQUEST_TARGET
)) {
308 ntlmssp_state
->neg_flags
|= NTLMSSP_REQUEST_TARGET
;
313 static bool gensec_ntlmssp_have_feature(struct gensec_security
*gensec_security
,
316 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
317 talloc_get_type_abort(gensec_security
->private_data
,
318 struct gensec_ntlmssp_context
);
319 struct ntlmssp_state
*ntlmssp_state
= gensec_ntlmssp
->ntlmssp_state
;
321 if (feature
& GENSEC_FEATURE_SIGN
) {
322 if (!ntlmssp_state
->session_key
.length
) {
325 if (ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_SIGN
) {
329 if (feature
& GENSEC_FEATURE_SEAL
) {
330 if (!ntlmssp_state
->session_key
.length
) {
333 if (ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_SEAL
) {
337 if (feature
& GENSEC_FEATURE_SESSION_KEY
) {
338 if (ntlmssp_state
->session_key
.length
) {
342 if (feature
& GENSEC_FEATURE_DCE_STYLE
) {
345 if (feature
& GENSEC_FEATURE_ASYNC_REPLIES
) {
346 if (ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
) {
353 NTSTATUS
gensec_ntlmssp_start(struct gensec_security
*gensec_security
)
355 struct gensec_ntlmssp_context
*gensec_ntlmssp
;
356 struct ntlmssp_state
*ntlmssp_state
;
358 gensec_ntlmssp
= talloc_zero(gensec_security
,
359 struct gensec_ntlmssp_context
);
360 if (!gensec_ntlmssp
) {
361 return NT_STATUS_NO_MEMORY
;
364 gensec_ntlmssp
->gensec_security
= gensec_security
;
366 ntlmssp_state
= talloc_zero(gensec_ntlmssp
,
367 struct ntlmssp_state
);
368 if (!ntlmssp_state
) {
369 return NT_STATUS_NO_MEMORY
;
372 ntlmssp_state
->callback_private
= gensec_ntlmssp
;
374 gensec_ntlmssp
->ntlmssp_state
= ntlmssp_state
;
376 gensec_security
->private_data
= gensec_ntlmssp
;
380 static const char *gensec_ntlmssp_oids
[] = {
385 static const struct gensec_security_ops gensec_ntlmssp_security_ops
= {
388 .auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
,
389 .oid
= gensec_ntlmssp_oids
,
390 .client_start
= gensec_ntlmssp_client_start
,
391 .server_start
= gensec_ntlmssp_server_start
,
392 .magic
= gensec_ntlmssp_magic
,
393 .update
= gensec_ntlmssp_update
,
394 .sig_size
= gensec_ntlmssp_sig_size
,
395 .sign_packet
= gensec_ntlmssp_sign_packet
,
396 .check_packet
= gensec_ntlmssp_check_packet
,
397 .seal_packet
= gensec_ntlmssp_seal_packet
,
398 .unseal_packet
= gensec_ntlmssp_unseal_packet
,
399 .wrap
= gensec_ntlmssp_wrap
,
400 .unwrap
= gensec_ntlmssp_unwrap
,
401 .session_key
= gensec_ntlmssp_session_key
,
402 .session_info
= gensec_ntlmssp_session_info
,
403 .have_feature
= gensec_ntlmssp_have_feature
,
405 .priority
= GENSEC_NTLMSSP
409 _PUBLIC_ NTSTATUS
gensec_ntlmssp_init(void)
413 ret
= gensec_register(&gensec_ntlmssp_security_ops
);
414 if (!NT_STATUS_IS_OK(ret
)) {
415 DEBUG(0,("Failed to register '%s' gensec backend!\n",
416 gensec_ntlmssp_security_ops
.name
));