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 gensec_ntlmssp_state
*gensec_ntlmssp_state
,
123 const DATA_BLOB input
, uint32_t *idx
)
125 struct gensec_security
*gensec_security
= gensec_ntlmssp_state
->gensec_security
;
126 uint32_t ntlmssp_command
;
129 if (gensec_ntlmssp_state
->expected_state
== NTLMSSP_DONE
) {
130 /* We are strict here because other modules, which we
131 * don't fully control (such as GSSAPI) are also
132 * strict, but are tested less often */
134 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
135 return NT_STATUS_INVALID_PARAMETER
;
139 switch (gensec_ntlmssp_state
->role
) {
141 ntlmssp_command
= NTLMSSP_INITIAL
;
144 if (gensec_security
->want_features
& GENSEC_FEATURE_DATAGRAM_MODE
) {
145 /* 'datagram' mode - no neg packet */
146 ntlmssp_command
= NTLMSSP_NEGOTIATE
;
148 /* This is normal in SPNEGO mech negotiation fallback */
149 DEBUG(2, ("Failed to parse NTLMSSP packet: zero length\n"));
150 return NT_STATUS_INVALID_PARAMETER
;
155 if (!msrpc_parse(gensec_ntlmssp_state
,
159 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
160 dump_data(2, input
.data
, input
.length
);
161 return NT_STATUS_INVALID_PARAMETER
;
165 if (ntlmssp_command
!= gensec_ntlmssp_state
->expected_state
) {
166 DEBUG(2, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command
, gensec_ntlmssp_state
->expected_state
));
167 return NT_STATUS_INVALID_PARAMETER
;
170 for (i
=0; i
< ARRAY_SIZE(ntlmssp_callbacks
); i
++) {
171 if (ntlmssp_callbacks
[i
].role
== gensec_ntlmssp_state
->role
&&
172 ntlmssp_callbacks
[i
].command
== ntlmssp_command
) {
178 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
179 gensec_ntlmssp_state
->role
, ntlmssp_command
));
181 return NT_STATUS_INVALID_PARAMETER
;
185 * Next state function for the wrapped NTLMSSP state machine
187 * @param gensec_security GENSEC state, initialised to NTLMSSP
188 * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
189 * @param in The request, as a DATA_BLOB
190 * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
191 * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent,
192 * or NT_STATUS_OK if the user is authenticated.
195 static NTSTATUS
gensec_ntlmssp_update(struct gensec_security
*gensec_security
,
196 TALLOC_CTX
*out_mem_ctx
,
197 const DATA_BLOB input
, DATA_BLOB
*out
)
199 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
= (struct gensec_ntlmssp_state
*)gensec_security
->private_data
;
203 *out
= data_blob(NULL
, 0);
206 /* if the caller doesn't want to manage/own the memory,
207 we can put it on our context */
208 out_mem_ctx
= gensec_ntlmssp_state
;
211 status
= gensec_ntlmssp_update_find(gensec_ntlmssp_state
, input
, &i
);
212 NT_STATUS_NOT_OK_RETURN(status
);
214 status
= ntlmssp_callbacks
[i
].sync_fn(gensec_security
, out_mem_ctx
, input
, out
);
215 NT_STATUS_NOT_OK_RETURN(status
);
221 * Return the NTLMSSP master session key
223 * @param gensec_ntlmssp_state NTLMSSP State
226 NTSTATUS
gensec_ntlmssp_session_key(struct gensec_security
*gensec_security
,
227 DATA_BLOB
*session_key
)
229 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
= (struct gensec_ntlmssp_state
*)gensec_security
->private_data
;
231 if (gensec_ntlmssp_state
->expected_state
!= NTLMSSP_DONE
) {
232 return NT_STATUS_NO_USER_SESSION_KEY
;
235 if (!gensec_ntlmssp_state
->session_key
.data
) {
236 return NT_STATUS_NO_USER_SESSION_KEY
;
238 *session_key
= gensec_ntlmssp_state
->session_key
;
243 void ntlmssp_handle_neg_flags(struct gensec_ntlmssp_state
*gensec_ntlmssp_state
,
244 uint32_t neg_flags
, bool allow_lm
)
246 if (neg_flags
& NTLMSSP_NEGOTIATE_UNICODE
) {
247 gensec_ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_UNICODE
;
248 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_OEM
;
249 gensec_ntlmssp_state
->unicode
= true;
251 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_UNICODE
;
252 gensec_ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_OEM
;
253 gensec_ntlmssp_state
->unicode
= false;
256 if ((neg_flags
& NTLMSSP_NEGOTIATE_LM_KEY
) && allow_lm
&& !gensec_ntlmssp_state
->use_ntlmv2
) {
257 /* other end forcing us to use LM */
258 gensec_ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_LM_KEY
;
259 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_NTLM2
;
261 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_LM_KEY
;
264 if (!(neg_flags
& NTLMSSP_NEGOTIATE_ALWAYS_SIGN
)) {
265 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_ALWAYS_SIGN
;
268 if (!(neg_flags
& NTLMSSP_NEGOTIATE_SIGN
)) {
269 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_SIGN
;
272 if (!(neg_flags
& NTLMSSP_NEGOTIATE_SEAL
)) {
273 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_SEAL
;
276 if (!(neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
)) {
277 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_NTLM2
;
280 if (!(neg_flags
& NTLMSSP_NEGOTIATE_128
)) {
281 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_128
;
284 if (!(neg_flags
& NTLMSSP_NEGOTIATE_56
)) {
285 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_56
;
288 if (!(neg_flags
& NTLMSSP_NEGOTIATE_KEY_EXCH
)) {
289 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_KEY_EXCH
;
292 /* Woop Woop - unknown flag for Windows compatibility...
293 What does this really do ? JRA. */
294 if (!(neg_flags
& NTLMSSP_NEGOTIATE_VERSION
)) {
295 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_VERSION
;
298 if ((neg_flags
& NTLMSSP_REQUEST_TARGET
)) {
299 gensec_ntlmssp_state
->neg_flags
|= NTLMSSP_REQUEST_TARGET
;
305 Weaken NTLMSSP keys to cope with down-level clients and servers.
307 We probably should have some parameters to control this, but as
308 it only occours for LM_KEY connections, and this is controlled
309 by the client lanman auth/lanman auth parameters, it isn't too bad.
312 DATA_BLOB
ntlmssp_weakend_key(struct gensec_ntlmssp_state
*gensec_ntlmssp_state
,
315 DATA_BLOB weakened_key
= data_blob_talloc(mem_ctx
,
316 gensec_ntlmssp_state
->session_key
.data
,
317 gensec_ntlmssp_state
->session_key
.length
);
318 /* Nothing to weaken. We certainly don't want to 'extend' the length... */
319 if (weakened_key
.length
< 16) {
320 /* perhaps there was no key? */
324 /* Key weakening not performed on the master key for NTLM2
325 and does not occour for NTLM1. Therefore we only need
326 to do this for the LM_KEY.
328 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_LM_KEY
) {
329 /* LM key doesn't support 128 bit crypto, so this is
330 * the best we can do. If you negotiate 128 bit, but
331 * not 56, you end up with 40 bit... */
332 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_56
) {
333 weakened_key
.data
[7] = 0xa0;
334 weakened_key
.length
= 8;
335 } else { /* forty bits */
336 weakened_key
.data
[5] = 0xe5;
337 weakened_key
.data
[6] = 0x38;
338 weakened_key
.data
[7] = 0xb0;
339 weakened_key
.length
= 8;
345 static bool gensec_ntlmssp_have_feature(struct gensec_security
*gensec_security
,
348 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
= (struct gensec_ntlmssp_state
*)gensec_security
->private_data
;
349 if (feature
& GENSEC_FEATURE_SIGN
) {
350 if (!gensec_ntlmssp_state
->session_key
.length
) {
353 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_SIGN
) {
357 if (feature
& GENSEC_FEATURE_SEAL
) {
358 if (!gensec_ntlmssp_state
->session_key
.length
) {
361 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_SEAL
) {
365 if (feature
& GENSEC_FEATURE_SESSION_KEY
) {
366 if (gensec_ntlmssp_state
->session_key
.length
) {
370 if (feature
& GENSEC_FEATURE_DCE_STYLE
) {
373 if (feature
& GENSEC_FEATURE_ASYNC_REPLIES
) {
374 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
) {
381 NTSTATUS
gensec_ntlmssp_start(struct gensec_security
*gensec_security
)
383 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
;
385 gensec_ntlmssp_state
= talloc_zero(gensec_security
, struct gensec_ntlmssp_state
);
386 if (!gensec_ntlmssp_state
) {
387 return NT_STATUS_NO_MEMORY
;
390 gensec_ntlmssp_state
->gensec_security
= gensec_security
;
391 gensec_ntlmssp_state
->auth_context
= NULL
;
392 gensec_ntlmssp_state
->server_info
= NULL
;
394 gensec_security
->private_data
= gensec_ntlmssp_state
;
398 static const char *gensec_ntlmssp_oids
[] = {
403 static const struct gensec_security_ops gensec_ntlmssp_security_ops
= {
406 .auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
,
407 .oid
= gensec_ntlmssp_oids
,
408 .client_start
= gensec_ntlmssp_client_start
,
409 .server_start
= gensec_ntlmssp_server_start
,
410 .magic
= gensec_ntlmssp_magic
,
411 .update
= gensec_ntlmssp_update
,
412 .sig_size
= gensec_ntlmssp_sig_size
,
413 .sign_packet
= gensec_ntlmssp_sign_packet
,
414 .check_packet
= gensec_ntlmssp_check_packet
,
415 .seal_packet
= gensec_ntlmssp_seal_packet
,
416 .unseal_packet
= gensec_ntlmssp_unseal_packet
,
417 .wrap
= gensec_ntlmssp_wrap
,
418 .unwrap
= gensec_ntlmssp_unwrap
,
419 .session_key
= gensec_ntlmssp_session_key
,
420 .session_info
= gensec_ntlmssp_session_info
,
421 .have_feature
= gensec_ntlmssp_have_feature
,
423 .priority
= GENSEC_NTLMSSP
427 _PUBLIC_ NTSTATUS
gensec_ntlmssp_init(void)
431 ret
= gensec_register(&gensec_ntlmssp_security_ops
);
432 if (!NT_STATUS_IS_OK(ret
)) {
433 DEBUG(0,("Failed to register '%s' gensec backend!\n",
434 gensec_ntlmssp_security_ops
.name
));