2 Unix SMB/Netbios implementation.
4 handle NLTMSSP, server side
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett 2001-2003
8 Copyright (C) Andrew Bartlett 2005 (Updated from gensec).
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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 static NTSTATUS
ntlmssp_client_initial(struct ntlmssp_state
*ntlmssp_state
,
28 DATA_BLOB reply
, DATA_BLOB
*next_request
);
29 static NTSTATUS
ntlmssp_server_negotiate(struct ntlmssp_state
*ntlmssp_state
,
30 const DATA_BLOB in
, DATA_BLOB
*out
);
31 static NTSTATUS
ntlmssp_client_challenge(struct ntlmssp_state
*ntlmssp_state
,
32 const DATA_BLOB reply
, DATA_BLOB
*next_request
);
33 static NTSTATUS
ntlmssp_server_auth(struct ntlmssp_state
*ntlmssp_state
,
34 const DATA_BLOB request
, DATA_BLOB
*reply
);
37 * Callbacks for NTLMSSP - for both client and server operating modes
41 static const struct ntlmssp_callbacks
{
42 enum NTLMSSP_ROLE role
;
43 enum NTLM_MESSAGE_TYPE ntlmssp_command
;
44 NTSTATUS (*fn
)(struct ntlmssp_state
*ntlmssp_state
,
45 DATA_BLOB in
, DATA_BLOB
*out
);
46 } ntlmssp_callbacks
[] = {
47 {NTLMSSP_CLIENT
, NTLMSSP_INITIAL
, ntlmssp_client_initial
},
48 {NTLMSSP_SERVER
, NTLMSSP_NEGOTIATE
, ntlmssp_server_negotiate
},
49 {NTLMSSP_CLIENT
, NTLMSSP_CHALLENGE
, ntlmssp_client_challenge
},
50 {NTLMSSP_SERVER
, NTLMSSP_AUTH
, ntlmssp_server_auth
},
51 {NTLMSSP_CLIENT
, NTLMSSP_UNKNOWN
, NULL
},
52 {NTLMSSP_SERVER
, NTLMSSP_UNKNOWN
, NULL
}
57 * Print out the NTLMSSP flags for debugging
58 * @param neg_flags The flags from the packet
61 void debug_ntlmssp_flags(uint32 neg_flags
)
63 DEBUG(3,("Got NTLMSSP neg_flags=0x%08x\n", neg_flags
));
65 if (neg_flags
& NTLMSSP_NEGOTIATE_UNICODE
)
66 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_UNICODE\n"));
67 if (neg_flags
& NTLMSSP_NEGOTIATE_OEM
)
68 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM\n"));
69 if (neg_flags
& NTLMSSP_REQUEST_TARGET
)
70 DEBUGADD(4, (" NTLMSSP_REQUEST_TARGET\n"));
71 if (neg_flags
& NTLMSSP_NEGOTIATE_SIGN
)
72 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SIGN\n"));
73 if (neg_flags
& NTLMSSP_NEGOTIATE_SEAL
)
74 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SEAL\n"));
75 if (neg_flags
& NTLMSSP_NEGOTIATE_DATAGRAM_STYLE
)
76 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_DATAGRAM_STYLE\n"));
77 if (neg_flags
& NTLMSSP_NEGOTIATE_LM_KEY
)
78 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_LM_KEY\n"));
79 if (neg_flags
& NTLMSSP_NEGOTIATE_NETWARE
)
80 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NETWARE\n"));
81 if (neg_flags
& NTLMSSP_NEGOTIATE_NTLM
)
82 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM\n"));
83 if (neg_flags
& NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED
)
84 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED\n"));
85 if (neg_flags
& NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED
)
86 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED\n"));
87 if (neg_flags
& NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL
)
88 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL\n"));
89 if (neg_flags
& NTLMSSP_NEGOTIATE_ALWAYS_SIGN
)
90 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_ALWAYS_SIGN\n"));
91 if (neg_flags
& NTLMSSP_CHAL_ACCEPT_RESPONSE
)
92 DEBUGADD(4, (" NTLMSSP_CHAL_ACCEPT_RESPONSE\n"));
93 if (neg_flags
& NTLMSSP_CHAL_NON_NT_SESSION_KEY
)
94 DEBUGADD(4, (" NTLMSSP_CHAL_NON_NT_SESSION_KEY\n"));
95 if (neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
)
96 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM2\n"));
97 if (neg_flags
& NTLMSSP_CHAL_TARGET_INFO
)
98 DEBUGADD(4, (" NTLMSSP_CHAL_TARGET_INFO\n"));
99 if (neg_flags
& NTLMSSP_NEGOTIATE_128
)
100 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_128\n"));
101 if (neg_flags
& NTLMSSP_NEGOTIATE_KEY_EXCH
)
102 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_KEY_EXCH\n"));
103 if (neg_flags
& NTLMSSP_NEGOTIATE_56
)
104 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_56\n"));
108 * Default challenge generation code.
112 static const uint8
*get_challenge(const struct ntlmssp_state
*ntlmssp_state
)
114 static uchar chal
[8];
115 generate_random_buffer(chal
, sizeof(chal
));
121 * Default 'we can set the challenge to anything we like' implementation
125 static BOOL
may_set_challenge(const struct ntlmssp_state
*ntlmssp_state
)
131 * Default 'we can set the challenge to anything we like' implementation
133 * Does not actually do anything, as the value is always in the structure anyway.
137 static NTSTATUS
set_challenge(struct ntlmssp_state
*ntlmssp_state
, DATA_BLOB
*challenge
)
139 SMB_ASSERT(challenge
->length
== 8);
144 * Set a username on an NTLMSSP context - ensures it is talloc()ed
148 NTSTATUS
ntlmssp_set_username(NTLMSSP_STATE
*ntlmssp_state
, const char *user
)
150 ntlmssp_state
->user
= talloc_strdup(ntlmssp_state
->mem_ctx
, user
? user
: "" );
151 if (!ntlmssp_state
->user
) {
152 return NT_STATUS_NO_MEMORY
;
158 * Set a password on an NTLMSSP context - ensures it is talloc()ed
161 NTSTATUS
ntlmssp_set_password(NTLMSSP_STATE
*ntlmssp_state
, const char *password
)
164 ntlmssp_state
->password
= NULL
;
166 ntlmssp_state
->password
= talloc_strdup(ntlmssp_state
->mem_ctx
, password
);
167 if (!ntlmssp_state
->password
) {
168 return NT_STATUS_NO_MEMORY
;
175 * Set a domain on an NTLMSSP context - ensures it is talloc()ed
178 NTSTATUS
ntlmssp_set_domain(NTLMSSP_STATE
*ntlmssp_state
, const char *domain
)
180 ntlmssp_state
->domain
= talloc_strdup(ntlmssp_state
->mem_ctx
, domain
? domain
: "" );
181 if (!ntlmssp_state
->domain
) {
182 return NT_STATUS_NO_MEMORY
;
188 * Set a workstation on an NTLMSSP context - ensures it is talloc()ed
191 NTSTATUS
ntlmssp_set_workstation(NTLMSSP_STATE
*ntlmssp_state
, const char *workstation
)
193 ntlmssp_state
->workstation
= talloc_strdup(ntlmssp_state
->mem_ctx
, workstation
);
194 if (!ntlmssp_state
->workstation
) {
195 return NT_STATUS_NO_MEMORY
;
201 * Store a DATA_BLOB containing an NTLMSSP response, for use later.
202 * This copies the data blob
205 NTSTATUS
ntlmssp_store_response(NTLMSSP_STATE
*ntlmssp_state
,
208 ntlmssp_state
->stored_response
= data_blob_talloc(ntlmssp_state
->mem_ctx
,
209 response
.data
, response
.length
);
214 * Next state function for the NTLMSSP state machine
216 * @param ntlmssp_state NTLMSSP State
217 * @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
218 * @param out The reply, as an allocated DATA_BLOB, caller to free.
219 * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
222 NTSTATUS
ntlmssp_update(NTLMSSP_STATE
*ntlmssp_state
,
223 const DATA_BLOB in
, DATA_BLOB
*out
)
226 uint32 ntlmssp_command
;
229 if (ntlmssp_state
->expected_state
== NTLMSSP_DONE
) {
230 /* Called update after negotiations finished. */
231 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
232 return NT_STATUS_INVALID_PARAMETER
;
235 *out
= data_blob(NULL
, 0);
237 if (!in
.length
&& ntlmssp_state
->stored_response
.length
) {
238 input
= ntlmssp_state
->stored_response
;
240 /* we only want to read the stored response once - overwrite it */
241 ntlmssp_state
->stored_response
= data_blob(NULL
, 0);
247 switch (ntlmssp_state
->role
) {
249 ntlmssp_command
= NTLMSSP_INITIAL
;
252 /* 'datagram' mode - no neg packet */
253 ntlmssp_command
= NTLMSSP_NEGOTIATE
;
257 if (!msrpc_parse(&input
, "Cd",
260 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
261 dump_data(2, (const char *)input
.data
, input
.length
);
262 return NT_STATUS_INVALID_PARAMETER
;
266 if (ntlmssp_command
!= ntlmssp_state
->expected_state
) {
267 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command
, ntlmssp_state
->expected_state
));
268 return NT_STATUS_INVALID_PARAMETER
;
271 for (i
=0; ntlmssp_callbacks
[i
].fn
; i
++) {
272 if (ntlmssp_callbacks
[i
].role
== ntlmssp_state
->role
273 && ntlmssp_callbacks
[i
].ntlmssp_command
== ntlmssp_command
) {
274 return ntlmssp_callbacks
[i
].fn(ntlmssp_state
, input
, out
);
278 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
279 ntlmssp_state
->role
, ntlmssp_command
));
281 return NT_STATUS_INVALID_PARAMETER
;
285 * End an NTLMSSP state machine
287 * @param ntlmssp_state NTLMSSP State, free()ed by this function
290 void ntlmssp_end(NTLMSSP_STATE
**ntlmssp_state
)
292 TALLOC_CTX
*mem_ctx
= (*ntlmssp_state
)->mem_ctx
;
294 (*ntlmssp_state
)->ref_count
--;
296 if ((*ntlmssp_state
)->ref_count
== 0) {
297 data_blob_free(&(*ntlmssp_state
)->chal
);
298 data_blob_free(&(*ntlmssp_state
)->lm_resp
);
299 data_blob_free(&(*ntlmssp_state
)->nt_resp
);
301 talloc_destroy(mem_ctx
);
304 *ntlmssp_state
= NULL
;
309 * Determine correct target name flags for reply, given server role
310 * and negotiated flags
312 * @param ntlmssp_state NTLMSSP State
313 * @param neg_flags The flags from the packet
314 * @param chal_flags The flags to be set in the reply packet
315 * @return The 'target name' string.
318 static const char *ntlmssp_target_name(struct ntlmssp_state
*ntlmssp_state
,
319 uint32 neg_flags
, uint32
*chal_flags
)
321 if (neg_flags
& NTLMSSP_REQUEST_TARGET
) {
322 *chal_flags
|= NTLMSSP_CHAL_TARGET_INFO
;
323 *chal_flags
|= NTLMSSP_REQUEST_TARGET
;
324 if (ntlmssp_state
->server_role
== ROLE_STANDALONE
) {
325 *chal_flags
|= NTLMSSP_TARGET_TYPE_SERVER
;
326 return ntlmssp_state
->get_global_myname();
328 *chal_flags
|= NTLMSSP_TARGET_TYPE_DOMAIN
;
329 return ntlmssp_state
->get_domain();
336 static void ntlmssp_handle_neg_flags(struct ntlmssp_state
*ntlmssp_state
,
337 uint32 neg_flags
, BOOL allow_lm
) {
338 if (neg_flags
& NTLMSSP_NEGOTIATE_UNICODE
) {
339 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_UNICODE
;
340 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_OEM
;
341 ntlmssp_state
->unicode
= True
;
343 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_UNICODE
;
344 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_OEM
;
345 ntlmssp_state
->unicode
= False
;
348 if ((neg_flags
& NTLMSSP_NEGOTIATE_LM_KEY
) && allow_lm
) {
349 /* other end forcing us to use LM */
350 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_LM_KEY
;
351 ntlmssp_state
->use_ntlmv2
= False
;
353 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_LM_KEY
;
356 if (neg_flags
& NTLMSSP_NEGOTIATE_ALWAYS_SIGN
) {
357 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_ALWAYS_SIGN
;
360 if (!(neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
)) {
361 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_NTLM2
;
364 if (!(neg_flags
& NTLMSSP_NEGOTIATE_128
)) {
365 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_128
;
368 if (!(neg_flags
& NTLMSSP_NEGOTIATE_56
)) {
369 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_56
;
372 if (!(neg_flags
& NTLMSSP_NEGOTIATE_KEY_EXCH
)) {
373 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_KEY_EXCH
;
376 if (!(neg_flags
& NTLMSSP_NEGOTIATE_SIGN
)) {
377 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_SIGN
;
380 if (!(neg_flags
& NTLMSSP_NEGOTIATE_SEAL
)) {
381 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_SEAL
;
384 /* Woop Woop - unknown flag for Windows compatibility...
385 What does this really do ? JRA. */
386 if (!(neg_flags
& NTLMSSP_UNKNOWN_02000000
)) {
387 ntlmssp_state
->neg_flags
&= ~NTLMSSP_UNKNOWN_02000000
;
390 if ((neg_flags
& NTLMSSP_REQUEST_TARGET
)) {
391 ntlmssp_state
->neg_flags
|= NTLMSSP_REQUEST_TARGET
;
396 Weaken NTLMSSP keys to cope with down-level clients and servers.
398 We probably should have some parameters to control this, but as
399 it only occours for LM_KEY connections, and this is controlled
400 by the client lanman auth/lanman auth parameters, it isn't too bad.
403 DATA_BLOB
ntlmssp_weaken_keys(NTLMSSP_STATE
*ntlmssp_state
, TALLOC_CTX
*mem_ctx
)
405 DATA_BLOB weakened_key
= data_blob_talloc(mem_ctx
,
406 ntlmssp_state
->session_key
.data
,
407 ntlmssp_state
->session_key
.length
);
409 /* Nothing to weaken. We certainly don't want to 'extend' the length... */
410 if (weakened_key
.length
< 16) {
411 /* perhaps there was no key? */
415 /* Key weakening not performed on the master key for NTLM2
416 and does not occour for NTLM1. Therefore we only need
417 to do this for the LM_KEY.
420 if (ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_LM_KEY
) {
421 /* LM key doesn't support 128 bit crypto, so this is
422 * the best we can do. If you negotiate 128 bit, but
423 * not 56, you end up with 40 bit... */
424 if (ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_56
) {
425 weakened_key
.data
[7] = 0xa0;
426 } else { /* forty bits */
427 weakened_key
.data
[5] = 0xe5;
428 weakened_key
.data
[6] = 0x38;
429 weakened_key
.data
[7] = 0xb0;
431 weakened_key
.length
= 8;
437 * Next state function for the Negotiate packet
439 * @param ntlmssp_state NTLMSSP State
440 * @param request The request, as a DATA_BLOB
441 * @param request The reply, as an allocated DATA_BLOB, caller to free.
442 * @return Errors or MORE_PROCESSING_REQUIRED if a reply is sent.
445 static NTSTATUS
ntlmssp_server_negotiate(struct ntlmssp_state
*ntlmssp_state
,
446 const DATA_BLOB request
, DATA_BLOB
*reply
)
448 DATA_BLOB struct_blob
;
449 fstring dnsname
, dnsdomname
;
450 uint32 neg_flags
= 0;
451 uint32 ntlmssp_command
, chal_flags
;
452 const uint8
*cryptkey
;
453 const char *target_name
;
455 /* parse the NTLMSSP packet */
457 file_save("ntlmssp_negotiate.dat", request
.data
, request
.length
);
460 if (request
.length
) {
461 if ((request
.length
< 16) || !msrpc_parse(&request
, "Cdd",
465 DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
466 (unsigned int)request
.length
));
467 dump_data(2, (const char *)request
.data
, request
.length
);
468 return NT_STATUS_INVALID_PARAMETER
;
470 debug_ntlmssp_flags(neg_flags
);
473 ntlmssp_handle_neg_flags(ntlmssp_state
, neg_flags
, lp_lanman_auth());
475 /* Ask our caller what challenge they would like in the packet */
476 cryptkey
= ntlmssp_state
->get_challenge(ntlmssp_state
);
478 /* Check if we may set the challenge */
479 if (!ntlmssp_state
->may_set_challenge(ntlmssp_state
)) {
480 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_NTLM2
;
483 /* The flags we send back are not just the negotiated flags,
484 * they are also 'what is in this packet'. Therfore, we
485 * operate on 'chal_flags' from here on
488 chal_flags
= ntlmssp_state
->neg_flags
;
490 /* get the right name to fill in as 'target' */
491 target_name
= ntlmssp_target_name(ntlmssp_state
,
492 neg_flags
, &chal_flags
);
493 if (target_name
== NULL
)
494 return NT_STATUS_INVALID_PARAMETER
;
496 ntlmssp_state
->chal
= data_blob_talloc(ntlmssp_state
->mem_ctx
, cryptkey
, 8);
497 ntlmssp_state
->internal_chal
= data_blob_talloc(ntlmssp_state
->mem_ctx
, cryptkey
, 8);
500 /* This should be a 'netbios domain -> DNS domain' mapping */
501 dnsdomname
[0] = '\0';
502 get_mydnsdomname(dnsdomname
);
503 strlower_m(dnsdomname
);
506 get_mydnsfullname(dnsname
);
508 /* This creates the 'blob' of names that appears at the end of the packet */
509 if (chal_flags
& NTLMSSP_CHAL_TARGET_INFO
)
511 msrpc_gen(&struct_blob
, "aaaaa",
512 NTLMSSP_NAME_TYPE_DOMAIN
, target_name
,
513 NTLMSSP_NAME_TYPE_SERVER
, ntlmssp_state
->get_global_myname(),
514 NTLMSSP_NAME_TYPE_DOMAIN_DNS
, dnsdomname
,
515 NTLMSSP_NAME_TYPE_SERVER_DNS
, dnsname
,
518 struct_blob
= data_blob(NULL
, 0);
522 /* Marshel the packet in the right format, be it unicode or ASCII */
523 const char *gen_string
;
524 if (ntlmssp_state
->unicode
) {
525 gen_string
= "CdUdbddB";
527 gen_string
= "CdAdbddB";
530 msrpc_gen(reply
, gen_string
,
537 struct_blob
.data
, struct_blob
.length
);
540 data_blob_free(&struct_blob
);
542 ntlmssp_state
->expected_state
= NTLMSSP_AUTH
;
544 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
548 * Next state function for the Authenticate packet
550 * @param ntlmssp_state NTLMSSP State
551 * @param request The request, as a DATA_BLOB
552 * @param request The reply, as an allocated DATA_BLOB, caller to free.
553 * @return Errors or NT_STATUS_OK.
556 static NTSTATUS
ntlmssp_server_auth(struct ntlmssp_state
*ntlmssp_state
,
557 const DATA_BLOB request
, DATA_BLOB
*reply
)
559 DATA_BLOB encrypted_session_key
= data_blob(NULL
, 0);
560 DATA_BLOB user_session_key
= data_blob(NULL
, 0);
561 DATA_BLOB lm_session_key
= data_blob(NULL
, 0);
562 DATA_BLOB session_key
= data_blob(NULL
, 0);
563 uint32 ntlmssp_command
, auth_flags
;
564 NTSTATUS nt_status
= NT_STATUS_OK
;
567 BOOL doing_ntlm2
= False
;
569 uchar session_nonce
[16];
570 uchar session_nonce_hash
[16];
572 const char *parse_string
;
575 char *workstation
= NULL
;
577 /* parse the NTLMSSP packet */
578 *reply
= data_blob(NULL
, 0);
581 file_save("ntlmssp_auth.dat", request
.data
, request
.length
);
584 if (ntlmssp_state
->unicode
) {
585 parse_string
= "CdBBUUUBd";
587 parse_string
= "CdBBAAABd";
590 data_blob_free(&ntlmssp_state
->lm_resp
);
591 data_blob_free(&ntlmssp_state
->nt_resp
);
593 ntlmssp_state
->user
= NULL
;
594 ntlmssp_state
->domain
= NULL
;
595 ntlmssp_state
->workstation
= NULL
;
597 /* now the NTLMSSP encoded auth hashes */
598 if (!msrpc_parse(&request
, parse_string
,
601 &ntlmssp_state
->lm_resp
,
602 &ntlmssp_state
->nt_resp
,
606 &encrypted_session_key
,
610 SAFE_FREE(workstation
);
611 data_blob_free(&encrypted_session_key
);
614 /* Try again with a shorter string (Win9X truncates this packet) */
615 if (ntlmssp_state
->unicode
) {
616 parse_string
= "CdBBUUU";
618 parse_string
= "CdBBAAA";
621 /* now the NTLMSSP encoded auth hashes */
622 if (!msrpc_parse(&request
, parse_string
,
625 &ntlmssp_state
->lm_resp
,
626 &ntlmssp_state
->nt_resp
,
630 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
631 dump_data(2, (const char *)request
.data
, request
.length
);
634 SAFE_FREE(workstation
);
636 return NT_STATUS_INVALID_PARAMETER
;
641 ntlmssp_handle_neg_flags(ntlmssp_state
, auth_flags
, lp_lanman_auth());
643 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_set_domain(ntlmssp_state
, domain
))) {
646 SAFE_FREE(workstation
);
647 data_blob_free(&encrypted_session_key
);
651 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_set_username(ntlmssp_state
, user
))) {
654 SAFE_FREE(workstation
);
655 data_blob_free(&encrypted_session_key
);
659 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_set_workstation(ntlmssp_state
, workstation
))) {
662 SAFE_FREE(workstation
);
663 data_blob_free(&encrypted_session_key
);
669 SAFE_FREE(workstation
);
671 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
672 ntlmssp_state
->user
, ntlmssp_state
->domain
, ntlmssp_state
->workstation
, (unsigned long)ntlmssp_state
->lm_resp
.length
, (unsigned long)ntlmssp_state
->nt_resp
.length
));
675 file_save("nthash1.dat", &ntlmssp_state
->nt_resp
.data
, &ntlmssp_state
->nt_resp
.length
);
676 file_save("lmhash1.dat", &ntlmssp_state
->lm_resp
.data
, &ntlmssp_state
->lm_resp
.length
);
679 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
682 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
684 if (ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
) {
685 if (ntlmssp_state
->nt_resp
.length
== 24 && ntlmssp_state
->lm_resp
.length
== 24) {
686 struct MD5Context md5_session_nonce_ctx
;
687 SMB_ASSERT(ntlmssp_state
->internal_chal
.data
&& ntlmssp_state
->internal_chal
.length
== 8);
691 memcpy(session_nonce
, ntlmssp_state
->internal_chal
.data
, 8);
692 memcpy(&session_nonce
[8], ntlmssp_state
->lm_resp
.data
, 8);
694 MD5Init(&md5_session_nonce_ctx
);
695 MD5Update(&md5_session_nonce_ctx
, session_nonce
, 16);
696 MD5Final(session_nonce_hash
, &md5_session_nonce_ctx
);
698 ntlmssp_state
->chal
= data_blob_talloc(ntlmssp_state
->mem_ctx
, session_nonce_hash
, 8);
700 /* LM response is no longer useful */
701 data_blob_free(&ntlmssp_state
->lm_resp
);
703 /* We changed the effective challenge - set it */
704 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_state
->set_challenge(ntlmssp_state
, &ntlmssp_state
->chal
))) {
705 data_blob_free(&encrypted_session_key
);
709 /* LM Key is incompatible. */
710 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_LM_KEY
;
715 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
716 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
717 * smb.conf file) and no NTLMv2 response was sent then the password check
718 * will fail here. JRA.
721 /* Finally, actually ask if the password is OK */
723 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_state
->check_password(ntlmssp_state
,
724 &user_session_key
, &lm_session_key
))) {
725 data_blob_free(&encrypted_session_key
);
729 dump_data_pw("NT session key:\n", user_session_key
.data
, user_session_key
.length
);
730 dump_data_pw("LM first-8:\n", lm_session_key
.data
, lm_session_key
.length
);
732 /* Handle the different session key derivation for NTLM2 */
734 if (user_session_key
.data
&& user_session_key
.length
== 16) {
735 session_key
= data_blob_talloc(ntlmssp_state
->mem_ctx
, NULL
, 16);
736 hmac_md5(user_session_key
.data
, session_nonce
,
737 sizeof(session_nonce
), session_key
.data
);
738 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
739 dump_data_pw("NTLM2 session key:\n", session_key
.data
, session_key
.length
);
742 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
743 session_key
= data_blob(NULL
, 0);
745 } else if (ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_LM_KEY
) {
746 if (lm_session_key
.data
&& lm_session_key
.length
>= 8) {
747 if (ntlmssp_state
->lm_resp
.data
&& ntlmssp_state
->lm_resp
.length
== 24) {
748 session_key
= data_blob_talloc(ntlmssp_state
->mem_ctx
, NULL
, 16);
749 if (session_key
.data
== NULL
) {
750 return NT_STATUS_NO_MEMORY
;
752 SMBsesskeygen_lm_sess_key(lm_session_key
.data
, ntlmssp_state
->lm_resp
.data
,
754 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
756 static const uint8 zeros
[24] = { 0, };
757 session_key
= data_blob_talloc(
758 ntlmssp_state
->mem_ctx
, NULL
, 16);
759 if (session_key
.data
== NULL
) {
760 return NT_STATUS_NO_MEMORY
;
762 SMBsesskeygen_lm_sess_key(
763 lm_session_key
.data
, zeros
,
766 dump_data_pw("LM session key:\n", session_key
.data
,
769 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
770 session_key
= data_blob(NULL
, 0);
772 } else if (user_session_key
.data
) {
773 session_key
= user_session_key
;
774 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
775 dump_data_pw("unmodified session key:\n", session_key
.data
, session_key
.length
);
776 } else if (lm_session_key
.data
) {
777 session_key
= lm_session_key
;
778 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
779 dump_data_pw("unmodified session key:\n", session_key
.data
, session_key
.length
);
781 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
782 session_key
= data_blob(NULL
, 0);
785 /* With KEY_EXCH, the client supplies the proposed session key,
786 but encrypts it with the long-term key */
787 if (ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_KEY_EXCH
) {
788 if (!encrypted_session_key
.data
|| encrypted_session_key
.length
!= 16) {
789 data_blob_free(&encrypted_session_key
);
790 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
791 (unsigned int)encrypted_session_key
.length
));
792 return NT_STATUS_INVALID_PARAMETER
;
793 } else if (!session_key
.data
|| session_key
.length
!= 16) {
794 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
795 (unsigned int)session_key
.length
));
796 ntlmssp_state
->session_key
= session_key
;
798 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key
.data
, encrypted_session_key
.length
);
799 SamOEMhash(encrypted_session_key
.data
,
801 encrypted_session_key
.length
);
802 ntlmssp_state
->session_key
= data_blob_talloc(ntlmssp_state
->mem_ctx
,
803 encrypted_session_key
.data
,
804 encrypted_session_key
.length
);
805 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key
.data
,
806 encrypted_session_key
.length
);
809 ntlmssp_state
->session_key
= session_key
;
812 if (!NT_STATUS_IS_OK(nt_status
)) {
813 ntlmssp_state
->session_key
= data_blob(NULL
, 0);
814 } else if (ntlmssp_state
->session_key
.length
) {
815 nt_status
= ntlmssp_sign_init(ntlmssp_state
);
818 data_blob_free(&encrypted_session_key
);
820 /* Only one authentication allowed per server state. */
821 ntlmssp_state
->expected_state
= NTLMSSP_DONE
;
827 * Create an NTLMSSP state machine
829 * @param ntlmssp_state NTLMSSP State, allocated by this function
832 NTSTATUS
ntlmssp_server_start(NTLMSSP_STATE
**ntlmssp_state
)
836 mem_ctx
= talloc_init("NTLMSSP context");
838 *ntlmssp_state
= TALLOC_ZERO_P(mem_ctx
, NTLMSSP_STATE
);
839 if (!*ntlmssp_state
) {
840 DEBUG(0,("ntlmssp_server_start: talloc failed!\n"));
841 talloc_destroy(mem_ctx
);
842 return NT_STATUS_NO_MEMORY
;
845 (*ntlmssp_state
)->role
= NTLMSSP_SERVER
;
847 (*ntlmssp_state
)->mem_ctx
= mem_ctx
;
848 (*ntlmssp_state
)->get_challenge
= get_challenge
;
849 (*ntlmssp_state
)->set_challenge
= set_challenge
;
850 (*ntlmssp_state
)->may_set_challenge
= may_set_challenge
;
852 (*ntlmssp_state
)->get_global_myname
= global_myname
;
853 (*ntlmssp_state
)->get_domain
= lp_workgroup
;
854 (*ntlmssp_state
)->server_role
= ROLE_DOMAIN_MEMBER
; /* a good default */
856 (*ntlmssp_state
)->expected_state
= NTLMSSP_NEGOTIATE
;
858 (*ntlmssp_state
)->ref_count
= 1;
860 (*ntlmssp_state
)->neg_flags
=
861 NTLMSSP_NEGOTIATE_128
|
862 NTLMSSP_NEGOTIATE_56
|
863 NTLMSSP_UNKNOWN_02000000
|
864 NTLMSSP_NEGOTIATE_NTLM
|
865 NTLMSSP_NEGOTIATE_NTLM2
|
866 NTLMSSP_NEGOTIATE_KEY_EXCH
|
867 NTLMSSP_NEGOTIATE_SIGN
|
868 NTLMSSP_NEGOTIATE_SEAL
;
873 /*********************************************************************
875 *********************************************************************/
878 * Next state function for the Initial packet
880 * @param ntlmssp_state NTLMSSP State
881 * @param request The request, as a DATA_BLOB. reply.data must be NULL
882 * @param request The reply, as an allocated DATA_BLOB, caller to free.
883 * @return Errors or NT_STATUS_OK.
886 static NTSTATUS
ntlmssp_client_initial(struct ntlmssp_state
*ntlmssp_state
,
887 DATA_BLOB reply
, DATA_BLOB
*next_request
)
889 if (ntlmssp_state
->unicode
) {
890 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_UNICODE
;
892 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_OEM
;
895 if (ntlmssp_state
->use_ntlmv2
) {
896 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_NTLM2
;
899 /* generate the ntlmssp negotiate packet */
900 msrpc_gen(next_request
, "CddAA",
903 ntlmssp_state
->neg_flags
,
904 ntlmssp_state
->get_domain(),
905 ntlmssp_state
->get_global_myname());
907 ntlmssp_state
->expected_state
= NTLMSSP_CHALLENGE
;
909 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
913 * Next state function for the Challenge Packet. Generate an auth packet.
915 * @param ntlmssp_state NTLMSSP State
916 * @param request The request, as a DATA_BLOB. reply.data must be NULL
917 * @param request The reply, as an allocated DATA_BLOB, caller to free.
918 * @return Errors or NT_STATUS_OK.
921 static NTSTATUS
ntlmssp_client_challenge(struct ntlmssp_state
*ntlmssp_state
,
922 const DATA_BLOB reply
, DATA_BLOB
*next_request
)
924 uint32 chal_flags
, ntlmssp_command
, unkn1
, unkn2
;
925 DATA_BLOB server_domain_blob
;
926 DATA_BLOB challenge_blob
;
927 DATA_BLOB struct_blob
= data_blob(NULL
, 0);
929 const char *chal_parse_string
;
930 const char *auth_gen_string
;
931 DATA_BLOB lm_response
= data_blob(NULL
, 0);
932 DATA_BLOB nt_response
= data_blob(NULL
, 0);
933 DATA_BLOB session_key
= data_blob(NULL
, 0);
934 DATA_BLOB encrypted_session_key
= data_blob(NULL
, 0);
935 NTSTATUS nt_status
= NT_STATUS_OK
;
937 if (!msrpc_parse(&reply
, "CdBd",
942 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
943 dump_data(2, (const char *)reply
.data
, reply
.length
);
945 return NT_STATUS_INVALID_PARAMETER
;
948 data_blob_free(&server_domain_blob
);
950 DEBUG(3, ("Got challenge flags:\n"));
951 debug_ntlmssp_flags(chal_flags
);
953 ntlmssp_handle_neg_flags(ntlmssp_state
, chal_flags
, lp_client_lanman_auth());
955 if (ntlmssp_state
->unicode
) {
956 if (chal_flags
& NTLMSSP_CHAL_TARGET_INFO
) {
957 chal_parse_string
= "CdUdbddB";
959 chal_parse_string
= "CdUdbdd";
961 auth_gen_string
= "CdBBUUUBd";
963 if (chal_flags
& NTLMSSP_CHAL_TARGET_INFO
) {
964 chal_parse_string
= "CdAdbddB";
966 chal_parse_string
= "CdAdbdd";
969 auth_gen_string
= "CdBBAAABd";
972 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
973 debug_ntlmssp_flags(ntlmssp_state
->neg_flags
);
975 if (!msrpc_parse(&reply
, chal_parse_string
,
983 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
984 dump_data(2, (const char *)reply
.data
, reply
.length
);
985 return NT_STATUS_INVALID_PARAMETER
;
988 ntlmssp_state
->server_domain
= talloc_strdup(ntlmssp_state
->mem_ctx
,
991 SAFE_FREE(server_domain
);
992 if (challenge_blob
.length
!= 8) {
993 data_blob_free(&struct_blob
);
994 return NT_STATUS_INVALID_PARAMETER
;
997 if (!ntlmssp_state
->password
) {
998 static const uchar zeros
[16];
999 /* do nothing - blobs are zero length */
1001 /* session key is all zeros */
1002 session_key
= data_blob_talloc(ntlmssp_state
->mem_ctx
, zeros
, 16);
1004 /* not doing NLTM2 without a password */
1005 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_NTLM2
;
1006 } else if (ntlmssp_state
->use_ntlmv2
) {
1008 if (!struct_blob
.length
) {
1009 /* be lazy, match win2k - we can't do NTLMv2 without it */
1010 DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1011 return NT_STATUS_INVALID_PARAMETER
;
1014 /* TODO: if the remote server is standalone, then we should replace 'domain'
1015 with the server name as supplied above */
1017 if (!SMBNTLMv2encrypt(ntlmssp_state
->user
,
1018 ntlmssp_state
->domain
,
1019 ntlmssp_state
->password
, &challenge_blob
,
1021 &lm_response
, &nt_response
, &session_key
)) {
1022 data_blob_free(&challenge_blob
);
1023 data_blob_free(&struct_blob
);
1024 return NT_STATUS_NO_MEMORY
;
1026 } else if (ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
) {
1027 struct MD5Context md5_session_nonce_ctx
;
1029 uchar session_nonce
[16];
1030 uchar session_nonce_hash
[16];
1031 uchar user_session_key
[16];
1032 E_md4hash(ntlmssp_state
->password
, nt_hash
);
1034 lm_response
= data_blob_talloc(ntlmssp_state
->mem_ctx
, NULL
, 24);
1035 generate_random_buffer(lm_response
.data
, 8);
1036 memset(lm_response
.data
+8, 0, 16);
1038 memcpy(session_nonce
, challenge_blob
.data
, 8);
1039 memcpy(&session_nonce
[8], lm_response
.data
, 8);
1041 MD5Init(&md5_session_nonce_ctx
);
1042 MD5Update(&md5_session_nonce_ctx
, challenge_blob
.data
, 8);
1043 MD5Update(&md5_session_nonce_ctx
, lm_response
.data
, 8);
1044 MD5Final(session_nonce_hash
, &md5_session_nonce_ctx
);
1046 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1047 DEBUG(5, ("challenge is: \n"));
1048 dump_data(5, (const char *)session_nonce_hash
, 8);
1050 nt_response
= data_blob_talloc(ntlmssp_state
->mem_ctx
, NULL
, 24);
1051 SMBNTencrypt(ntlmssp_state
->password
,
1055 session_key
= data_blob_talloc(ntlmssp_state
->mem_ctx
, NULL
, 16);
1057 SMBsesskeygen_ntv1(nt_hash
, NULL
, user_session_key
);
1058 hmac_md5(user_session_key
, session_nonce
, sizeof(session_nonce
), session_key
.data
);
1059 dump_data_pw("NTLM2 session key:\n", session_key
.data
, session_key
.length
);
1063 E_deshash(ntlmssp_state
->password
, lm_hash
);
1064 E_md4hash(ntlmssp_state
->password
, nt_hash
);
1066 /* lanman auth is insecure, it may be disabled */
1067 if (lp_client_lanman_auth()) {
1068 lm_response
= data_blob_talloc(ntlmssp_state
->mem_ctx
, NULL
, 24);
1069 SMBencrypt(ntlmssp_state
->password
,challenge_blob
.data
,
1073 nt_response
= data_blob_talloc(ntlmssp_state
->mem_ctx
, NULL
, 24);
1074 SMBNTencrypt(ntlmssp_state
->password
,challenge_blob
.data
,
1077 session_key
= data_blob_talloc(ntlmssp_state
->mem_ctx
, NULL
, 16);
1078 if ((ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_LM_KEY
)
1079 && lp_client_lanman_auth()) {
1080 SMBsesskeygen_lm_sess_key(lm_hash
, lm_response
.data
,
1082 dump_data_pw("LM session key\n", session_key
.data
, session_key
.length
);
1084 SMBsesskeygen_ntv1(nt_hash
, NULL
, session_key
.data
);
1085 dump_data_pw("NT session key:\n", session_key
.data
, session_key
.length
);
1088 data_blob_free(&struct_blob
);
1090 /* Key exchange encryptes a new client-generated session key with
1091 the password-derived key */
1092 if (ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_KEY_EXCH
) {
1093 /* Make up a new session key */
1094 uint8 client_session_key
[16];
1095 generate_random_buffer(client_session_key
, sizeof(client_session_key
));
1097 /* Encrypt the new session key with the old one */
1098 encrypted_session_key
= data_blob(client_session_key
, sizeof(client_session_key
));
1099 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key
.data
, encrypted_session_key
.length
);
1100 SamOEMhash(encrypted_session_key
.data
, session_key
.data
, encrypted_session_key
.length
);
1101 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key
.data
, encrypted_session_key
.length
);
1103 /* Mark the new session key as the 'real' session key */
1104 data_blob_free(&session_key
);
1105 session_key
= data_blob_talloc(ntlmssp_state
->mem_ctx
, client_session_key
, sizeof(client_session_key
));
1108 /* this generates the actual auth packet */
1109 if (!msrpc_gen(next_request
, auth_gen_string
,
1112 lm_response
.data
, lm_response
.length
,
1113 nt_response
.data
, nt_response
.length
,
1114 ntlmssp_state
->domain
,
1115 ntlmssp_state
->user
,
1116 ntlmssp_state
->get_global_myname(),
1117 encrypted_session_key
.data
, encrypted_session_key
.length
,
1118 ntlmssp_state
->neg_flags
)) {
1120 return NT_STATUS_NO_MEMORY
;
1123 data_blob_free(&encrypted_session_key
);
1125 data_blob_free(&ntlmssp_state
->chal
);
1127 ntlmssp_state
->session_key
= session_key
;
1129 ntlmssp_state
->chal
= challenge_blob
;
1130 ntlmssp_state
->lm_resp
= lm_response
;
1131 ntlmssp_state
->nt_resp
= nt_response
;
1133 ntlmssp_state
->expected_state
= NTLMSSP_DONE
;
1135 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_sign_init(ntlmssp_state
))) {
1136 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status
)));
1142 NTSTATUS
ntlmssp_client_start(NTLMSSP_STATE
**ntlmssp_state
)
1144 TALLOC_CTX
*mem_ctx
;
1146 mem_ctx
= talloc_init("NTLMSSP Client context");
1148 *ntlmssp_state
= TALLOC_ZERO_P(mem_ctx
, NTLMSSP_STATE
);
1149 if (!*ntlmssp_state
) {
1150 DEBUG(0,("ntlmssp_client_start: talloc failed!\n"));
1151 talloc_destroy(mem_ctx
);
1152 return NT_STATUS_NO_MEMORY
;
1155 (*ntlmssp_state
)->role
= NTLMSSP_CLIENT
;
1157 (*ntlmssp_state
)->mem_ctx
= mem_ctx
;
1159 (*ntlmssp_state
)->get_global_myname
= global_myname
;
1160 (*ntlmssp_state
)->get_domain
= lp_workgroup
;
1162 (*ntlmssp_state
)->unicode
= True
;
1164 (*ntlmssp_state
)->use_ntlmv2
= lp_client_ntlmv2_auth();
1166 (*ntlmssp_state
)->expected_state
= NTLMSSP_INITIAL
;
1168 (*ntlmssp_state
)->ref_count
= 1;
1170 (*ntlmssp_state
)->neg_flags
=
1171 NTLMSSP_NEGOTIATE_128
|
1172 NTLMSSP_NEGOTIATE_NTLM
|
1173 NTLMSSP_NEGOTIATE_NTLM2
|
1174 NTLMSSP_NEGOTIATE_KEY_EXCH
|
1176 * We need to set this to allow a later SetPassword
1177 * via the SAMR pipe to succeed. Strange.... We could
1178 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
1180 NTLMSSP_NEGOTIATE_SIGN
|
1181 NTLMSSP_REQUEST_TARGET
;
1183 return NT_STATUS_OK
;