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 * Store NT and LM hashes on an NTLMSSP context - ensures they are talloc()ed
161 NTSTATUS ntlmssp_set_hashes(NTLMSSP_STATE *ntlmssp_state,
162 const unsigned char lm_hash[16],
163 const unsigned char nt_hash[16])
165 ntlmssp_state->lm_hash = TALLOC_MEMDUP(ntlmssp_state->mem_ctx, lm_hash, 16);
166 ntlmssp_state->nt_hash = TALLOC_MEMDUP(ntlmssp_state->mem_ctx, nt_hash, 16);
167 if (!ntlmssp_state->lm_hash || !ntlmssp_state->nt_hash) {
168 return NT_STATUS_NO_MEMORY;
174 * Converts a password to the hashes on an NTLMSSP context.
177 NTSTATUS ntlmssp_set_password(NTLMSSP_STATE *ntlmssp_state, const char *password)
180 ntlmssp_state->lm_hash = NULL;
181 ntlmssp_state->nt_hash = NULL;
183 unsigned char lm_hash[16];
184 unsigned char nt_hash[16];
186 E_deshash(password, lm_hash);
187 E_md4hash(password, nt_hash);
188 return ntlmssp_set_hashes(ntlmssp_state, lm_hash, nt_hash);
194 * Set a domain on an NTLMSSP context - ensures it is talloc()ed
197 NTSTATUS ntlmssp_set_domain(NTLMSSP_STATE *ntlmssp_state, const char *domain)
199 ntlmssp_state->domain = talloc_strdup(ntlmssp_state->mem_ctx, domain ? domain : "" );
200 if (!ntlmssp_state->domain) {
201 return NT_STATUS_NO_MEMORY;
207 * Set a workstation on an NTLMSSP context - ensures it is talloc()ed
210 NTSTATUS ntlmssp_set_workstation(NTLMSSP_STATE *ntlmssp_state, const char *workstation)
212 ntlmssp_state->workstation = talloc_strdup(ntlmssp_state->mem_ctx, workstation);
213 if (!ntlmssp_state->workstation) {
214 return NT_STATUS_NO_MEMORY;
220 * Store a DATA_BLOB containing an NTLMSSP response, for use later.
221 * This copies the data blob
224 NTSTATUS ntlmssp_store_response(NTLMSSP_STATE *ntlmssp_state,
227 ntlmssp_state->stored_response = data_blob_talloc(ntlmssp_state->mem_ctx,
228 response.data, response.length);
233 * Request features for the NTLMSSP negotiation
235 * @param ntlmssp_state NTLMSSP state
236 * @param feature_list List of space seperated features requested from NTLMSSP.
238 void ntlmssp_want_feature_list(NTLMSSP_STATE *ntlmssp_state, char *feature_list)
241 * We need to set this to allow a later SetPassword
242 * via the SAMR pipe to succeed. Strange.... We could
243 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
245 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
246 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
248 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
249 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
251 if(in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
252 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
257 * Request a feature for the NTLMSSP negotiation
259 * @param ntlmssp_state NTLMSSP state
260 * @param feature Bit flag specifying the requested feature
262 void ntlmssp_want_feature(NTLMSSP_STATE *ntlmssp_state, uint32 feature)
264 /* As per JRA's comment above */
265 if (feature & NTLMSSP_FEATURE_SESSION_KEY) {
266 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
268 if (feature & NTLMSSP_FEATURE_SIGN) {
269 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
271 if (feature & NTLMSSP_FEATURE_SEAL) {
272 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
277 * Next state function for the NTLMSSP state machine
279 * @param ntlmssp_state NTLMSSP State
280 * @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
281 * @param out The reply, as an allocated DATA_BLOB, caller to free.
282 * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
285 NTSTATUS ntlmssp_update(NTLMSSP_STATE *ntlmssp_state,
286 const DATA_BLOB in, DATA_BLOB *out)
289 uint32 ntlmssp_command;
292 if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
293 /* Called update after negotiations finished. */
294 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
295 return NT_STATUS_INVALID_PARAMETER;
298 *out = data_blob(NULL, 0);
300 if (!in.length && ntlmssp_state->stored_response.length) {
301 input = ntlmssp_state->stored_response;
303 /* we only want to read the stored response once - overwrite it */
304 ntlmssp_state->stored_response = data_blob(NULL, 0);
310 switch (ntlmssp_state->role) {
312 ntlmssp_command = NTLMSSP_INITIAL;
315 /* 'datagram' mode - no neg packet */
316 ntlmssp_command = NTLMSSP_NEGOTIATE;
320 if (!msrpc_parse(&input, "Cd",
323 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
324 dump_data(2, (const char *)input.data, input.length);
325 return NT_STATUS_INVALID_PARAMETER;
329 if (ntlmssp_command != ntlmssp_state->expected_state) {
330 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
331 return NT_STATUS_INVALID_PARAMETER;
334 for (i=0; ntlmssp_callbacks[i].fn; i++) {
335 if (ntlmssp_callbacks[i].role == ntlmssp_state->role
336 && ntlmssp_callbacks[i].ntlmssp_command == ntlmssp_command) {
337 return ntlmssp_callbacks[i].fn(ntlmssp_state, input, out);
341 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
342 ntlmssp_state->role, ntlmssp_command));
344 return NT_STATUS_INVALID_PARAMETER;
348 * End an NTLMSSP state machine
350 * @param ntlmssp_state NTLMSSP State, free()ed by this function
353 void ntlmssp_end(NTLMSSP_STATE **ntlmssp_state)
355 TALLOC_CTX *mem_ctx = (*ntlmssp_state)->mem_ctx;
357 (*ntlmssp_state)->ref_count--;
359 if ((*ntlmssp_state)->ref_count == 0) {
360 data_blob_free(&(*ntlmssp_state)->chal);
361 data_blob_free(&(*ntlmssp_state)->lm_resp);
362 data_blob_free(&(*ntlmssp_state)->nt_resp);
364 talloc_destroy(mem_ctx);
367 *ntlmssp_state = NULL;
372 * Determine correct target name flags for reply, given server role
373 * and negotiated flags
375 * @param ntlmssp_state NTLMSSP State
376 * @param neg_flags The flags from the packet
377 * @param chal_flags The flags to be set in the reply packet
378 * @return The 'target name' string.
381 static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
382 uint32 neg_flags, uint32 *chal_flags)
384 if (neg_flags & NTLMSSP_REQUEST_TARGET) {
385 *chal_flags |= NTLMSSP_CHAL_TARGET_INFO;
386 *chal_flags |= NTLMSSP_REQUEST_TARGET;
387 if (ntlmssp_state->server_role == ROLE_STANDALONE) {
388 *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
389 return ntlmssp_state->get_global_myname();
391 *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
392 return ntlmssp_state->get_domain();
399 static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
400 uint32 neg_flags, BOOL allow_lm) {
401 if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
402 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
403 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_OEM;
404 ntlmssp_state->unicode = True;
406 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_UNICODE;
407 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
408 ntlmssp_state->unicode = False;
411 if ((neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) && allow_lm) {
412 /* other end forcing us to use LM */
413 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
414 ntlmssp_state->use_ntlmv2 = False;
416 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
419 if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN) {
420 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
423 if (!(neg_flags & NTLMSSP_NEGOTIATE_NTLM2)) {
424 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
427 if (!(neg_flags & NTLMSSP_NEGOTIATE_128)) {
428 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_128;
431 if (!(neg_flags & NTLMSSP_NEGOTIATE_56)) {
432 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_56;
435 if (!(neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
436 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_KEY_EXCH;
439 if (!(neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
440 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SIGN;
443 if (!(neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
444 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SEAL;
447 /* Woop Woop - unknown flag for Windows compatibility...
448 What does this really do ? JRA. */
449 if (!(neg_flags & NTLMSSP_UNKNOWN_02000000)) {
450 ntlmssp_state->neg_flags &= ~NTLMSSP_UNKNOWN_02000000;
453 if ((neg_flags & NTLMSSP_REQUEST_TARGET)) {
454 ntlmssp_state->neg_flags |= NTLMSSP_REQUEST_TARGET;
459 Weaken NTLMSSP keys to cope with down-level clients and servers.
461 We probably should have some parameters to control this, but as
462 it only occours for LM_KEY connections, and this is controlled
463 by the client lanman auth/lanman auth parameters, it isn't too bad.
466 DATA_BLOB ntlmssp_weaken_keys(NTLMSSP_STATE *ntlmssp_state, TALLOC_CTX *mem_ctx)
468 DATA_BLOB weakened_key = data_blob_talloc(mem_ctx,
469 ntlmssp_state->session_key.data,
470 ntlmssp_state->session_key.length);
472 /* Nothing to weaken. We certainly don't want to 'extend' the length... */
473 if (weakened_key.length < 16) {
474 /* perhaps there was no key? */
478 /* Key weakening not performed on the master key for NTLM2
479 and does not occour for NTLM1. Therefore we only need
480 to do this for the LM_KEY.
483 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
484 /* LM key doesn't support 128 bit crypto, so this is
485 * the best we can do. If you negotiate 128 bit, but
486 * not 56, you end up with 40 bit... */
487 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
488 weakened_key.data[7] = 0xa0;
489 } else { /* forty bits */
490 weakened_key.data[5] = 0xe5;
491 weakened_key.data[6] = 0x38;
492 weakened_key.data[7] = 0xb0;
494 weakened_key.length = 8;
500 * Next state function for the Negotiate packet
502 * @param ntlmssp_state NTLMSSP State
503 * @param request The request, as a DATA_BLOB
504 * @param request The reply, as an allocated DATA_BLOB, caller to free.
505 * @return Errors or MORE_PROCESSING_REQUIRED if a reply is sent.
508 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
509 const DATA_BLOB request, DATA_BLOB *reply)
511 DATA_BLOB struct_blob;
512 fstring dnsname, dnsdomname;
513 uint32 neg_flags = 0;
514 uint32 ntlmssp_command, chal_flags;
515 const uint8 *cryptkey;
516 const char *target_name;
518 /* parse the NTLMSSP packet */
520 file_save("ntlmssp_negotiate.dat", request.data, request.length);
523 if (request.length) {
524 if ((request.length < 16) || !msrpc_parse(&request, "Cdd",
528 DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
529 (unsigned int)request.length));
530 dump_data(2, (const char *)request.data, request.length);
531 return NT_STATUS_INVALID_PARAMETER;
533 debug_ntlmssp_flags(neg_flags);
536 ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, lp_lanman_auth());
538 /* Ask our caller what challenge they would like in the packet */
539 cryptkey = ntlmssp_state->get_challenge(ntlmssp_state);
541 /* Check if we may set the challenge */
542 if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
543 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
546 /* The flags we send back are not just the negotiated flags,
547 * they are also 'what is in this packet'. Therfore, we
548 * operate on 'chal_flags' from here on
551 chal_flags = ntlmssp_state->neg_flags;
553 /* get the right name to fill in as 'target' */
554 target_name = ntlmssp_target_name(ntlmssp_state,
555 neg_flags, &chal_flags);
556 if (target_name == NULL)
557 return NT_STATUS_INVALID_PARAMETER;
559 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state->mem_ctx, cryptkey, 8);
560 ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state->mem_ctx, cryptkey, 8);
563 /* This should be a 'netbios domain -> DNS domain' mapping */
564 dnsdomname[0] = '\0';
565 get_mydnsdomname(dnsdomname);
566 strlower_m(dnsdomname);
569 get_mydnsfullname(dnsname);
571 /* This creates the 'blob' of names that appears at the end of the packet */
572 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO)
574 msrpc_gen(&struct_blob, "aaaaa",
575 NTLMSSP_NAME_TYPE_DOMAIN, target_name,
576 NTLMSSP_NAME_TYPE_SERVER, ntlmssp_state->get_global_myname(),
577 NTLMSSP_NAME_TYPE_DOMAIN_DNS, dnsdomname,
578 NTLMSSP_NAME_TYPE_SERVER_DNS, dnsname,
581 struct_blob = data_blob(NULL, 0);
585 /* Marshel the packet in the right format, be it unicode or ASCII */
586 const char *gen_string;
587 if (ntlmssp_state->unicode) {
588 gen_string = "CdUdbddB";
590 gen_string = "CdAdbddB";
593 msrpc_gen(reply, gen_string,
600 struct_blob.data, struct_blob.length);
603 data_blob_free(&struct_blob);
605 ntlmssp_state->expected_state = NTLMSSP_AUTH;
607 return NT_STATUS_MORE_PROCESSING_REQUIRED;
611 * Next state function for the Authenticate packet
613 * @param ntlmssp_state NTLMSSP State
614 * @param request The request, as a DATA_BLOB
615 * @param request The reply, as an allocated DATA_BLOB, caller to free.
616 * @return Errors or NT_STATUS_OK.
619 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
620 const DATA_BLOB request, DATA_BLOB *reply)
622 DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
623 DATA_BLOB user_session_key = data_blob(NULL, 0);
624 DATA_BLOB lm_session_key = data_blob(NULL, 0);
625 DATA_BLOB session_key = data_blob(NULL, 0);
626 uint32 ntlmssp_command, auth_flags;
627 NTSTATUS nt_status = NT_STATUS_OK;
630 BOOL doing_ntlm2 = False;
632 uchar session_nonce[16];
633 uchar session_nonce_hash[16];
635 const char *parse_string;
638 char *workstation = NULL;
640 /* parse the NTLMSSP packet */
641 *reply = data_blob(NULL, 0);
644 file_save("ntlmssp_auth.dat", request.data, request.length);
647 if (ntlmssp_state->unicode) {
648 parse_string = "CdBBUUUBd";
650 parse_string = "CdBBAAABd";
653 data_blob_free(&ntlmssp_state->lm_resp);
654 data_blob_free(&ntlmssp_state->nt_resp);
656 ntlmssp_state->user = NULL;
657 ntlmssp_state->domain = NULL;
658 ntlmssp_state->workstation = NULL;
660 /* now the NTLMSSP encoded auth hashes */
661 if (!msrpc_parse(&request, parse_string,
664 &ntlmssp_state->lm_resp,
665 &ntlmssp_state->nt_resp,
669 &encrypted_session_key,
673 SAFE_FREE(workstation);
674 data_blob_free(&encrypted_session_key);
677 /* Try again with a shorter string (Win9X truncates this packet) */
678 if (ntlmssp_state->unicode) {
679 parse_string = "CdBBUUU";
681 parse_string = "CdBBAAA";
684 /* now the NTLMSSP encoded auth hashes */
685 if (!msrpc_parse(&request, parse_string,
688 &ntlmssp_state->lm_resp,
689 &ntlmssp_state->nt_resp,
693 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
694 dump_data(2, (const char *)request.data, request.length);
697 SAFE_FREE(workstation);
699 return NT_STATUS_INVALID_PARAMETER;
704 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, lp_lanman_auth());
706 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(ntlmssp_state, domain))) {
709 SAFE_FREE(workstation);
710 data_blob_free(&encrypted_session_key);
714 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(ntlmssp_state, user))) {
717 SAFE_FREE(workstation);
718 data_blob_free(&encrypted_session_key);
722 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_workstation(ntlmssp_state, workstation))) {
725 SAFE_FREE(workstation);
726 data_blob_free(&encrypted_session_key);
732 SAFE_FREE(workstation);
734 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
735 ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->workstation, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
738 file_save("nthash1.dat", &ntlmssp_state->nt_resp.data, &ntlmssp_state->nt_resp.length);
739 file_save("lmhash1.dat", &ntlmssp_state->lm_resp.data, &ntlmssp_state->lm_resp.length);
742 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
745 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
747 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
748 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
749 struct MD5Context md5_session_nonce_ctx;
750 SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
754 memcpy(session_nonce, ntlmssp_state->internal_chal.data, 8);
755 memcpy(&session_nonce[8], ntlmssp_state->lm_resp.data, 8);
757 MD5Init(&md5_session_nonce_ctx);
758 MD5Update(&md5_session_nonce_ctx, session_nonce, 16);
759 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
761 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state->mem_ctx, session_nonce_hash, 8);
763 /* LM response is no longer useful */
764 data_blob_free(&ntlmssp_state->lm_resp);
766 /* We changed the effective challenge - set it */
767 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->set_challenge(ntlmssp_state, &ntlmssp_state->chal))) {
768 data_blob_free(&encrypted_session_key);
772 /* LM Key is incompatible. */
773 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
778 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
779 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
780 * smb.conf file) and no NTLMv2 response was sent then the password check
781 * will fail here. JRA.
784 /* Finally, actually ask if the password is OK */
786 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state,
787 &user_session_key, &lm_session_key))) {
788 data_blob_free(&encrypted_session_key);
792 dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
793 dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
795 /* Handle the different session key derivation for NTLM2 */
797 if (user_session_key.data && user_session_key.length == 16) {
798 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
799 hmac_md5(user_session_key.data, session_nonce,
800 sizeof(session_nonce), session_key.data);
801 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
802 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
805 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
806 session_key = data_blob(NULL, 0);
808 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
809 if (lm_session_key.data && lm_session_key.length >= 8) {
810 if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
811 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
812 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
814 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
815 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
817 /* use the key unmodified - it's
818 * probably a NULL key from the guest
820 session_key = lm_session_key;
823 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
824 session_key = data_blob(NULL, 0);
826 } else if (user_session_key.data) {
827 session_key = user_session_key;
828 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
829 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
830 } else if (lm_session_key.data) {
831 session_key = lm_session_key;
832 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
833 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
835 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
836 session_key = data_blob(NULL, 0);
839 /* With KEY_EXCH, the client supplies the proposed session key,
840 but encrypts it with the long-term key */
841 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
842 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
843 data_blob_free(&encrypted_session_key);
844 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
845 (unsigned int)encrypted_session_key.length));
846 return NT_STATUS_INVALID_PARAMETER;
847 } else if (!session_key.data || session_key.length != 16) {
848 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
849 (unsigned int)session_key.length));
850 ntlmssp_state->session_key = session_key;
852 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
853 SamOEMhash(encrypted_session_key.data,
855 encrypted_session_key.length);
856 ntlmssp_state->session_key = data_blob_talloc(ntlmssp_state->mem_ctx,
857 encrypted_session_key.data,
858 encrypted_session_key.length);
859 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
860 encrypted_session_key.length);
863 ntlmssp_state->session_key = session_key;
866 if (!NT_STATUS_IS_OK(nt_status)) {
867 ntlmssp_state->session_key = data_blob(NULL, 0);
868 } else if (ntlmssp_state->session_key.length) {
869 nt_status = ntlmssp_sign_init(ntlmssp_state);
872 data_blob_free(&encrypted_session_key);
874 /* Only one authentication allowed per server state. */
875 ntlmssp_state->expected_state = NTLMSSP_DONE;
881 * Create an NTLMSSP state machine
883 * @param ntlmssp_state NTLMSSP State, allocated by this function
886 NTSTATUS ntlmssp_server_start(NTLMSSP_STATE **ntlmssp_state)
890 mem_ctx = talloc_init("NTLMSSP context");
892 *ntlmssp_state = TALLOC_ZERO_P(mem_ctx, NTLMSSP_STATE);
893 if (!*ntlmssp_state) {
894 DEBUG(0,("ntlmssp_server_start: talloc failed!\n"));
895 talloc_destroy(mem_ctx);
896 return NT_STATUS_NO_MEMORY;
899 (*ntlmssp_state)->role = NTLMSSP_SERVER;
901 (*ntlmssp_state)->mem_ctx = mem_ctx;
902 (*ntlmssp_state)->get_challenge = get_challenge;
903 (*ntlmssp_state)->set_challenge = set_challenge;
904 (*ntlmssp_state)->may_set_challenge = may_set_challenge;
906 (*ntlmssp_state)->get_global_myname = global_myname;
907 (*ntlmssp_state)->get_domain = lp_workgroup;
908 (*ntlmssp_state)->server_role = ROLE_DOMAIN_MEMBER; /* a good default */
910 (*ntlmssp_state)->expected_state = NTLMSSP_NEGOTIATE;
912 (*ntlmssp_state)->ref_count = 1;
914 (*ntlmssp_state)->neg_flags =
915 NTLMSSP_NEGOTIATE_128 |
916 NTLMSSP_NEGOTIATE_56 |
917 NTLMSSP_UNKNOWN_02000000 |
918 NTLMSSP_NEGOTIATE_NTLM |
919 NTLMSSP_NEGOTIATE_NTLM2 |
920 NTLMSSP_NEGOTIATE_KEY_EXCH |
921 NTLMSSP_NEGOTIATE_SIGN |
922 NTLMSSP_NEGOTIATE_SEAL;
927 /*********************************************************************
929 *********************************************************************/
932 * Next state function for the Initial packet
934 * @param ntlmssp_state NTLMSSP State
935 * @param request The request, as a DATA_BLOB. reply.data must be NULL
936 * @param request The reply, as an allocated DATA_BLOB, caller to free.
937 * @return Errors or NT_STATUS_OK.
940 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
941 DATA_BLOB reply, DATA_BLOB *next_request)
943 if (ntlmssp_state->unicode) {
944 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
946 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
949 if (ntlmssp_state->use_ntlmv2) {
950 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
953 /* generate the ntlmssp negotiate packet */
954 msrpc_gen(next_request, "CddAA",
957 ntlmssp_state->neg_flags,
958 ntlmssp_state->get_domain(),
959 ntlmssp_state->get_global_myname());
961 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
963 return NT_STATUS_MORE_PROCESSING_REQUIRED;
967 * Next state function for the Challenge Packet. Generate an auth packet.
969 * @param ntlmssp_state NTLMSSP State
970 * @param request The request, as a DATA_BLOB. reply.data must be NULL
971 * @param request The reply, as an allocated DATA_BLOB, caller to free.
972 * @return Errors or NT_STATUS_OK.
975 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
976 const DATA_BLOB reply, DATA_BLOB *next_request)
978 uint32 chal_flags, ntlmssp_command, unkn1, unkn2;
979 DATA_BLOB server_domain_blob;
980 DATA_BLOB challenge_blob;
981 DATA_BLOB struct_blob = data_blob(NULL, 0);
983 const char *chal_parse_string;
984 const char *auth_gen_string;
985 DATA_BLOB lm_response = data_blob(NULL, 0);
986 DATA_BLOB nt_response = data_blob(NULL, 0);
987 DATA_BLOB session_key = data_blob(NULL, 0);
988 DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
989 NTSTATUS nt_status = NT_STATUS_OK;
991 if (!msrpc_parse(&reply, "CdBd",
996 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
997 dump_data(2, (const char *)reply.data, reply.length);
999 return NT_STATUS_INVALID_PARAMETER;
1002 data_blob_free(&server_domain_blob);
1004 DEBUG(3, ("Got challenge flags:\n"));
1005 debug_ntlmssp_flags(chal_flags);
1007 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
1009 if (ntlmssp_state->unicode) {
1010 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
1011 chal_parse_string = "CdUdbddB";
1013 chal_parse_string = "CdUdbdd";
1015 auth_gen_string = "CdBBUUUBd";
1017 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
1018 chal_parse_string = "CdAdbddB";
1020 chal_parse_string = "CdAdbdd";
1023 auth_gen_string = "CdBBAAABd";
1026 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
1027 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
1029 if (!msrpc_parse(&reply, chal_parse_string,
1037 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
1038 dump_data(2, (const char *)reply.data, reply.length);
1039 return NT_STATUS_INVALID_PARAMETER;
1042 ntlmssp_state->server_domain = talloc_strdup(ntlmssp_state->mem_ctx,
1045 SAFE_FREE(server_domain);
1046 if (challenge_blob.length != 8) {
1047 data_blob_free(&struct_blob);
1048 return NT_STATUS_INVALID_PARAMETER;
1051 if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
1052 static const uchar zeros[16];
1053 /* do nothing - blobs are zero length */
1055 /* session key is all zeros */
1056 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, zeros, 16);
1058 /* not doing NLTM2 without a password */
1059 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
1060 } else if (ntlmssp_state->use_ntlmv2) {
1062 if (!struct_blob.length) {
1063 /* be lazy, match win2k - we can't do NTLMv2 without it */
1064 DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1065 return NT_STATUS_INVALID_PARAMETER;
1068 /* TODO: if the remote server is standalone, then we should replace 'domain'
1069 with the server name as supplied above */
1071 if (!SMBNTLMv2encrypt_hash(ntlmssp_state->user,
1072 ntlmssp_state->domain,
1073 ntlmssp_state->nt_hash, &challenge_blob,
1075 &lm_response, &nt_response, &session_key)) {
1076 data_blob_free(&challenge_blob);
1077 data_blob_free(&struct_blob);
1078 return NT_STATUS_NO_MEMORY;
1080 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1081 struct MD5Context md5_session_nonce_ctx;
1082 uchar session_nonce[16];
1083 uchar session_nonce_hash[16];
1084 uchar user_session_key[16];
1086 lm_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1087 generate_random_buffer(lm_response.data, 8);
1088 memset(lm_response.data+8, 0, 16);
1090 memcpy(session_nonce, challenge_blob.data, 8);
1091 memcpy(&session_nonce[8], lm_response.data, 8);
1093 MD5Init(&md5_session_nonce_ctx);
1094 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1095 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1096 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1098 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1099 DEBUG(5, ("challenge is: \n"));
1100 dump_data(5, (const char *)session_nonce_hash, 8);
1102 nt_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1103 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
1107 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
1109 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, NULL, user_session_key);
1110 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1111 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1113 /* lanman auth is insecure, it may be disabled */
1114 if (lp_client_lanman_auth()) {
1115 lm_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1116 SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
1120 nt_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1121 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
1124 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
1125 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1126 && lp_client_lanman_auth()) {
1127 SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
1129 dump_data_pw("LM session key\n", session_key.data, session_key.length);
1131 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, NULL, session_key.data);
1132 dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1135 data_blob_free(&struct_blob);
1137 /* Key exchange encryptes a new client-generated session key with
1138 the password-derived key */
1139 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1140 /* Make up a new session key */
1141 uint8 client_session_key[16];
1142 generate_random_buffer(client_session_key, sizeof(client_session_key));
1144 /* Encrypt the new session key with the old one */
1145 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1146 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1147 SamOEMhash(encrypted_session_key.data, session_key.data, encrypted_session_key.length);
1148 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1150 /* Mark the new session key as the 'real' session key */
1151 data_blob_free(&session_key);
1152 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, client_session_key, sizeof(client_session_key));
1155 /* this generates the actual auth packet */
1156 if (!msrpc_gen(next_request, auth_gen_string,
1159 lm_response.data, lm_response.length,
1160 nt_response.data, nt_response.length,
1161 ntlmssp_state->domain,
1162 ntlmssp_state->user,
1163 ntlmssp_state->get_global_myname(),
1164 encrypted_session_key.data, encrypted_session_key.length,
1165 ntlmssp_state->neg_flags)) {
1167 return NT_STATUS_NO_MEMORY;
1170 data_blob_free(&encrypted_session_key);
1172 data_blob_free(&ntlmssp_state->chal);
1174 ntlmssp_state->session_key = session_key;
1176 ntlmssp_state->chal = challenge_blob;
1177 ntlmssp_state->lm_resp = lm_response;
1178 ntlmssp_state->nt_resp = nt_response;
1180 ntlmssp_state->expected_state = NTLMSSP_DONE;
1182 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1183 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1189 NTSTATUS ntlmssp_client_start(NTLMSSP_STATE **ntlmssp_state)
1191 TALLOC_CTX *mem_ctx;
1193 mem_ctx = talloc_init("NTLMSSP Client context");
1195 *ntlmssp_state = TALLOC_ZERO_P(mem_ctx, NTLMSSP_STATE);
1196 if (!*ntlmssp_state) {
1197 DEBUG(0,("ntlmssp_client_start: talloc failed!\n"));
1198 talloc_destroy(mem_ctx);
1199 return NT_STATUS_NO_MEMORY;
1202 (*ntlmssp_state)->role = NTLMSSP_CLIENT;
1204 (*ntlmssp_state)->mem_ctx = mem_ctx;
1206 (*ntlmssp_state)->get_global_myname = global_myname;
1207 (*ntlmssp_state)->get_domain = lp_workgroup;
1209 (*ntlmssp_state)->unicode = True;
1211 (*ntlmssp_state)->use_ntlmv2 = lp_client_ntlmv2_auth();
1213 (*ntlmssp_state)->expected_state = NTLMSSP_INITIAL;
1215 (*ntlmssp_state)->ref_count = 1;
1217 (*ntlmssp_state)->neg_flags =
1218 NTLMSSP_NEGOTIATE_128 |
1219 NTLMSSP_NEGOTIATE_NTLM |
1220 NTLMSSP_NEGOTIATE_NTLM2 |
1221 NTLMSSP_NEGOTIATE_KEY_EXCH |
1222 NTLMSSP_REQUEST_TARGET;
1224 return NT_STATUS_OK;