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 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/>.
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
28 #include "../libcli/auth/ntlmssp_ndr.h"
30 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
31 DATA_BLOB reply, DATA_BLOB *next_request);
32 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
33 const DATA_BLOB in, DATA_BLOB *out);
34 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
35 const DATA_BLOB reply, DATA_BLOB *next_request);
36 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
37 const DATA_BLOB request, DATA_BLOB *reply);
40 * Callbacks for NTLMSSP - for both client and server operating modes
44 static const struct ntlmssp_callbacks {
45 enum ntlmssp_role role;
46 enum NTLM_MESSAGE_TYPE ntlmssp_command;
47 NTSTATUS (*fn)(struct ntlmssp_state *ntlmssp_state,
48 DATA_BLOB in, DATA_BLOB *out);
49 } ntlmssp_callbacks[] = {
50 {NTLMSSP_CLIENT, NTLMSSP_INITIAL, ntlmssp_client_initial},
51 {NTLMSSP_SERVER, NTLMSSP_NEGOTIATE, ntlmssp_server_negotiate},
52 {NTLMSSP_CLIENT, NTLMSSP_CHALLENGE, ntlmssp_client_challenge},
53 {NTLMSSP_SERVER, NTLMSSP_AUTH, ntlmssp_server_auth},
54 {NTLMSSP_CLIENT, NTLMSSP_UNKNOWN, NULL},
55 {NTLMSSP_SERVER, NTLMSSP_UNKNOWN, NULL}
60 * Print out the NTLMSSP flags for debugging
61 * @param neg_flags The flags from the packet
64 void debug_ntlmssp_flags(uint32 neg_flags)
66 DEBUG(3,("Got NTLMSSP neg_flags=0x%08x\n", neg_flags));
68 if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE)
69 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_UNICODE\n"));
70 if (neg_flags & NTLMSSP_NEGOTIATE_OEM)
71 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM\n"));
72 if (neg_flags & NTLMSSP_REQUEST_TARGET)
73 DEBUGADD(4, (" NTLMSSP_REQUEST_TARGET\n"));
74 if (neg_flags & NTLMSSP_NEGOTIATE_SIGN)
75 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SIGN\n"));
76 if (neg_flags & NTLMSSP_NEGOTIATE_SEAL)
77 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SEAL\n"));
78 if (neg_flags & NTLMSSP_NEGOTIATE_DATAGRAM)
79 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_DATAGRAM\n"));
80 if (neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
81 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_LM_KEY\n"));
82 if (neg_flags & NTLMSSP_NEGOTIATE_NETWARE)
83 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NETWARE\n"));
84 if (neg_flags & NTLMSSP_NEGOTIATE_NTLM)
85 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM\n"));
86 if (neg_flags & NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED)
87 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED\n"));
88 if (neg_flags & NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED)
89 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED\n"));
90 if (neg_flags & NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL)
91 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL\n"));
92 if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)
93 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_ALWAYS_SIGN\n"));
94 if (neg_flags & NTLMSSP_REQUEST_NON_NT_SESSION_KEY)
95 DEBUGADD(4, (" NTLMSSP_REQUEST_NON_NT_SESSION_KEY\n"));
96 if (neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
97 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM2\n"));
98 if (neg_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
99 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_TARGET_INFO\n"));
100 if (neg_flags & NTLMSSP_NEGOTIATE_VERSION)
101 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_VERSION\n"));
102 if (neg_flags & NTLMSSP_NEGOTIATE_128)
103 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_128\n"));
104 if (neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)
105 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_KEY_EXCH\n"));
106 if (neg_flags & NTLMSSP_NEGOTIATE_56)
107 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_56\n"));
111 * Default challenge generation code.
115 static void get_challenge(const struct ntlmssp_state *ntlmssp_state,
118 generate_random_buffer(chal, 8);
122 * Default 'we can set the challenge to anything we like' implementation
126 static bool may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
132 * Default 'we can set the challenge to anything we like' implementation
134 * Does not actually do anything, as the value is always in the structure anyway.
138 static NTSTATUS set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
140 SMB_ASSERT(challenge->length == 8);
145 * Set a username on an NTLMSSP context - ensures it is talloc()ed
149 NTSTATUS ntlmssp_set_username(struct ntlmssp_state *ntlmssp_state, const char *user)
151 ntlmssp_state->user = talloc_strdup(ntlmssp_state, user ? user : "" );
152 if (!ntlmssp_state->user) {
153 return NT_STATUS_NO_MEMORY;
159 * Store NT and LM hashes on an NTLMSSP context - ensures they are talloc()ed
162 NTSTATUS ntlmssp_set_hashes(struct ntlmssp_state *ntlmssp_state,
163 const unsigned char lm_hash[16],
164 const unsigned char nt_hash[16])
166 ntlmssp_state->lm_hash = (unsigned char *)
167 TALLOC_MEMDUP(ntlmssp_state, lm_hash, 16);
168 ntlmssp_state->nt_hash = (unsigned char *)
169 TALLOC_MEMDUP(ntlmssp_state, nt_hash, 16);
170 if (!ntlmssp_state->lm_hash || !ntlmssp_state->nt_hash) {
171 TALLOC_FREE(ntlmssp_state->lm_hash);
172 TALLOC_FREE(ntlmssp_state->nt_hash);
173 return NT_STATUS_NO_MEMORY;
179 * Converts a password to the hashes on an NTLMSSP context.
182 NTSTATUS ntlmssp_set_password(struct ntlmssp_state *ntlmssp_state, const char *password)
185 ntlmssp_state->lm_hash = NULL;
186 ntlmssp_state->nt_hash = NULL;
188 unsigned char lm_hash[16];
189 unsigned char nt_hash[16];
191 E_deshash(password, lm_hash);
192 E_md4hash(password, nt_hash);
193 return ntlmssp_set_hashes(ntlmssp_state, lm_hash, nt_hash);
199 * Set a domain on an NTLMSSP context - ensures it is talloc()ed
202 NTSTATUS ntlmssp_set_domain(struct ntlmssp_state *ntlmssp_state, const char *domain)
204 ntlmssp_state->domain = talloc_strdup(ntlmssp_state,
205 domain ? domain : "" );
206 if (!ntlmssp_state->domain) {
207 return NT_STATUS_NO_MEMORY;
213 * Set a workstation on an NTLMSSP context - ensures it is talloc()ed
216 NTSTATUS ntlmssp_set_workstation(struct ntlmssp_state *ntlmssp_state, const char *workstation)
218 ntlmssp_state->workstation = talloc_strdup(ntlmssp_state, workstation);
219 if (!ntlmssp_state->workstation) {
220 return NT_STATUS_NO_MEMORY;
226 * Store a DATA_BLOB containing an NTLMSSP response, for use later.
227 * This copies the data blob
230 NTSTATUS ntlmssp_store_response(struct ntlmssp_state *ntlmssp_state,
233 ntlmssp_state->stored_response = data_blob_talloc(ntlmssp_state,
240 * Request features for the NTLMSSP negotiation
242 * @param ntlmssp_state NTLMSSP state
243 * @param feature_list List of space seperated features requested from NTLMSSP.
245 void ntlmssp_want_feature_list(struct ntlmssp_state *ntlmssp_state, char *feature_list)
248 * We need to set this to allow a later SetPassword
249 * via the SAMR pipe to succeed. Strange.... We could
250 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
252 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
253 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
255 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
256 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
258 if(in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
259 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
264 * Request a feature for the NTLMSSP negotiation
266 * @param ntlmssp_state NTLMSSP state
267 * @param feature Bit flag specifying the requested feature
269 void ntlmssp_want_feature(struct ntlmssp_state *ntlmssp_state, uint32 feature)
271 /* As per JRA's comment above */
272 if (feature & NTLMSSP_FEATURE_SESSION_KEY) {
273 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
275 if (feature & NTLMSSP_FEATURE_SIGN) {
276 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
278 if (feature & NTLMSSP_FEATURE_SEAL) {
279 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
284 * Next state function for the NTLMSSP state machine
286 * @param ntlmssp_state NTLMSSP State
287 * @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
288 * @param out The reply, as an allocated DATA_BLOB, caller to free.
289 * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
292 NTSTATUS ntlmssp_update(struct ntlmssp_state *ntlmssp_state,
293 const DATA_BLOB in, DATA_BLOB *out)
296 uint32 ntlmssp_command;
299 if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
300 /* Called update after negotiations finished. */
301 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
302 return NT_STATUS_INVALID_PARAMETER;
305 *out = data_blob_null;
307 if (!in.length && ntlmssp_state->stored_response.length) {
308 input = ntlmssp_state->stored_response;
310 /* we only want to read the stored response once - overwrite it */
311 ntlmssp_state->stored_response = data_blob_null;
317 switch (ntlmssp_state->role) {
319 ntlmssp_command = NTLMSSP_INITIAL;
322 /* 'datagram' mode - no neg packet */
323 ntlmssp_command = NTLMSSP_NEGOTIATE;
327 if (!msrpc_parse(ntlmssp_state, &input, "Cd",
330 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
331 dump_data(2, input.data, input.length);
332 return NT_STATUS_INVALID_PARAMETER;
336 if (ntlmssp_command != ntlmssp_state->expected_state) {
337 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
338 return NT_STATUS_INVALID_PARAMETER;
341 for (i=0; ntlmssp_callbacks[i].fn; i++) {
342 if (ntlmssp_callbacks[i].role == ntlmssp_state->role
343 && ntlmssp_callbacks[i].ntlmssp_command == ntlmssp_command) {
344 return ntlmssp_callbacks[i].fn(ntlmssp_state, input, out);
348 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
349 ntlmssp_state->role, ntlmssp_command));
351 return NT_STATUS_INVALID_PARAMETER;
355 * End an NTLMSSP state machine
357 * @param ntlmssp_state NTLMSSP State, free()ed by this function
360 void ntlmssp_end(struct ntlmssp_state **ntlmssp_state)
362 (*ntlmssp_state)->ref_count--;
364 if ((*ntlmssp_state)->ref_count == 0) {
365 data_blob_free(&(*ntlmssp_state)->chal);
366 data_blob_free(&(*ntlmssp_state)->lm_resp);
367 data_blob_free(&(*ntlmssp_state)->nt_resp);
368 TALLOC_FREE(*ntlmssp_state);
371 *ntlmssp_state = NULL;
376 * Determine correct target name flags for reply, given server role
377 * and negotiated flags
379 * @param ntlmssp_state NTLMSSP State
380 * @param neg_flags The flags from the packet
381 * @param chal_flags The flags to be set in the reply packet
382 * @return The 'target name' string.
385 static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
386 uint32 neg_flags, uint32 *chal_flags)
388 if (neg_flags & NTLMSSP_REQUEST_TARGET) {
389 *chal_flags |= NTLMSSP_NEGOTIATE_TARGET_INFO;
390 *chal_flags |= NTLMSSP_REQUEST_TARGET;
391 if (ntlmssp_state->server_role == ROLE_STANDALONE) {
392 *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
393 return ntlmssp_state->get_global_myname();
395 *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
396 return ntlmssp_state->get_domain();
403 static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
404 uint32 neg_flags, bool allow_lm) {
405 if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
406 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
407 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_OEM;
408 ntlmssp_state->unicode = True;
410 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_UNICODE;
411 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
412 ntlmssp_state->unicode = False;
415 if ((neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) && allow_lm) {
416 /* other end forcing us to use LM */
417 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
418 ntlmssp_state->use_ntlmv2 = False;
420 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
423 if (!(neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)) {
424 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
427 if (!(neg_flags & NTLMSSP_NEGOTIATE_NTLM2)) {
428 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
431 if (!(neg_flags & NTLMSSP_NEGOTIATE_128)) {
432 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_128;
435 if (!(neg_flags & NTLMSSP_NEGOTIATE_56)) {
436 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_56;
439 if (!(neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
440 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_KEY_EXCH;
443 if (!(neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
444 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SIGN;
447 if (!(neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
448 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SEAL;
451 /* Woop Woop - unknown flag for Windows compatibility...
452 What does this really do ? JRA. */
453 if (!(neg_flags & NTLMSSP_NEGOTIATE_VERSION)) {
454 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_VERSION;
457 if ((neg_flags & NTLMSSP_REQUEST_TARGET)) {
458 ntlmssp_state->neg_flags |= NTLMSSP_REQUEST_TARGET;
463 Weaken NTLMSSP keys to cope with down-level clients and servers.
465 We probably should have some parameters to control this, but as
466 it only occours for LM_KEY connections, and this is controlled
467 by the client lanman auth/lanman auth parameters, it isn't too bad.
470 DATA_BLOB ntlmssp_weaken_keys(struct ntlmssp_state *ntlmssp_state, TALLOC_CTX *mem_ctx)
472 DATA_BLOB weakened_key = data_blob_talloc(mem_ctx,
473 ntlmssp_state->session_key.data,
474 ntlmssp_state->session_key.length);
476 /* Nothing to weaken. We certainly don't want to 'extend' the length... */
477 if (weakened_key.length < 16) {
478 /* perhaps there was no key? */
482 /* Key weakening not performed on the master key for NTLM2
483 and does not occour for NTLM1. Therefore we only need
484 to do this for the LM_KEY.
487 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
488 /* LM key doesn't support 128 bit crypto, so this is
489 * the best we can do. If you negotiate 128 bit, but
490 * not 56, you end up with 40 bit... */
491 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
492 weakened_key.data[7] = 0xa0;
493 } else { /* forty bits */
494 weakened_key.data[5] = 0xe5;
495 weakened_key.data[6] = 0x38;
496 weakened_key.data[7] = 0xb0;
498 weakened_key.length = 8;
504 * Next state function for the Negotiate packet
506 * @param ntlmssp_state NTLMSSP State
507 * @param request The request, as a DATA_BLOB
508 * @param request The reply, as an allocated DATA_BLOB, caller to free.
509 * @return Errors or MORE_PROCESSING_REQUIRED if a reply is sent.
512 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
513 const DATA_BLOB request, DATA_BLOB *reply)
515 DATA_BLOB struct_blob;
517 char *dnsdomname = NULL;
518 uint32 neg_flags = 0;
519 uint32 ntlmssp_command, chal_flags;
521 const char *target_name;
522 struct NEGOTIATE_MESSAGE negotiate;
523 struct CHALLENGE_MESSAGE challenge;
525 /* parse the NTLMSSP packet */
527 file_save("ntlmssp_negotiate.dat", request.data, request.length);
530 if (request.length) {
531 if ((request.length < 16) || !msrpc_parse(ntlmssp_state, &request, "Cdd",
535 DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
536 (unsigned int)request.length));
537 dump_data(2, request.data, request.length);
538 return NT_STATUS_INVALID_PARAMETER;
540 debug_ntlmssp_flags(neg_flags);
542 if (DEBUGLEVEL >= 10) {
543 if (NT_STATUS_IS_OK(ntlmssp_pull_NEGOTIATE_MESSAGE(&request,
548 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE, &negotiate);
553 ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, lp_lanman_auth());
555 /* Ask our caller what challenge they would like in the packet */
556 ntlmssp_state->get_challenge(ntlmssp_state, cryptkey);
558 /* Check if we may set the challenge */
559 if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
560 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
563 /* The flags we send back are not just the negotiated flags,
564 * they are also 'what is in this packet'. Therfore, we
565 * operate on 'chal_flags' from here on
568 chal_flags = ntlmssp_state->neg_flags;
570 /* get the right name to fill in as 'target' */
571 target_name = ntlmssp_target_name(ntlmssp_state,
572 neg_flags, &chal_flags);
573 if (target_name == NULL)
574 return NT_STATUS_INVALID_PARAMETER;
576 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
577 ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state,
580 /* This should be a 'netbios domain -> DNS domain' mapping */
581 dnsdomname = get_mydnsdomname(ntlmssp_state);
583 dnsdomname = talloc_strdup(ntlmssp_state, "");
586 return NT_STATUS_NO_MEMORY;
588 strlower_m(dnsdomname);
590 dnsname = get_mydnsfullname();
595 /* This creates the 'blob' of names that appears at the end of the packet */
596 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
598 msrpc_gen(ntlmssp_state, &struct_blob, "aaaaa",
599 MsvAvNbDomainName, target_name,
600 MsvAvNbComputerName, ntlmssp_state->get_global_myname(),
601 MsvAvDnsDomainName, dnsdomname,
602 MsvAvDnsComputerName, dnsname,
605 struct_blob = data_blob_null;
609 /* Marshel the packet in the right format, be it unicode or ASCII */
610 const char *gen_string;
611 if (ntlmssp_state->unicode) {
612 gen_string = "CdUdbddB";
614 gen_string = "CdAdbddB";
617 msrpc_gen(ntlmssp_state, reply, gen_string,
624 struct_blob.data, struct_blob.length);
626 if (DEBUGLEVEL >= 10) {
627 if (NT_STATUS_IS_OK(ntlmssp_pull_CHALLENGE_MESSAGE(reply,
632 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE, &challenge);
637 data_blob_free(&struct_blob);
639 ntlmssp_state->expected_state = NTLMSSP_AUTH;
641 return NT_STATUS_MORE_PROCESSING_REQUIRED;
645 * Next state function for the Authenticate packet
647 * @param ntlmssp_state NTLMSSP State
648 * @param request The request, as a DATA_BLOB
649 * @param request The reply, as an allocated DATA_BLOB, caller to free.
650 * @return Errors or NT_STATUS_OK.
653 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
654 const DATA_BLOB request, DATA_BLOB *reply)
656 DATA_BLOB encrypted_session_key = data_blob_null;
657 DATA_BLOB user_session_key = data_blob_null;
658 DATA_BLOB lm_session_key = data_blob_null;
659 DATA_BLOB session_key = data_blob_null;
660 uint32 ntlmssp_command, auth_flags;
661 NTSTATUS nt_status = NT_STATUS_OK;
662 struct AUTHENTICATE_MESSAGE authenticate;
665 bool doing_ntlm2 = False;
667 uchar session_nonce[16];
668 uchar session_nonce_hash[16];
670 const char *parse_string;
672 /* parse the NTLMSSP packet */
673 *reply = data_blob_null;
676 file_save("ntlmssp_auth.dat", request.data, request.length);
679 if (ntlmssp_state->unicode) {
680 parse_string = "CdBBUUUBd";
682 parse_string = "CdBBAAABd";
685 data_blob_free(&ntlmssp_state->lm_resp);
686 data_blob_free(&ntlmssp_state->nt_resp);
688 ntlmssp_state->user = NULL;
689 ntlmssp_state->domain = NULL;
690 ntlmssp_state->workstation = NULL;
692 /* now the NTLMSSP encoded auth hashes */
693 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
696 &ntlmssp_state->lm_resp,
697 &ntlmssp_state->nt_resp,
698 &ntlmssp_state->domain,
699 &ntlmssp_state->user,
700 &ntlmssp_state->workstation,
701 &encrypted_session_key,
705 /* Try again with a shorter string (Win9X truncates this packet) */
706 if (ntlmssp_state->unicode) {
707 parse_string = "CdBBUUU";
709 parse_string = "CdBBAAA";
712 /* now the NTLMSSP encoded auth hashes */
713 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
716 &ntlmssp_state->lm_resp,
717 &ntlmssp_state->nt_resp,
718 &ntlmssp_state->domain,
719 &ntlmssp_state->user,
720 &ntlmssp_state->workstation)) {
721 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
722 dump_data(2, request.data, request.length);
724 return NT_STATUS_INVALID_PARAMETER;
729 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, lp_lanman_auth());
731 if (DEBUGLEVEL >= 10) {
732 if (NT_STATUS_IS_OK(ntlmssp_pull_AUTHENTICATE_MESSAGE(&request,
737 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE, &authenticate);
741 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
742 ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->workstation, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
745 file_save("nthash1.dat", &ntlmssp_state->nt_resp.data, &ntlmssp_state->nt_resp.length);
746 file_save("lmhash1.dat", &ntlmssp_state->lm_resp.data, &ntlmssp_state->lm_resp.length);
749 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
752 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
754 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
755 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
756 struct MD5Context md5_session_nonce_ctx;
757 SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
761 memcpy(session_nonce, ntlmssp_state->internal_chal.data, 8);
762 memcpy(&session_nonce[8], ntlmssp_state->lm_resp.data, 8);
764 MD5Init(&md5_session_nonce_ctx);
765 MD5Update(&md5_session_nonce_ctx, session_nonce, 16);
766 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
768 ntlmssp_state->chal = data_blob_talloc(
769 ntlmssp_state, session_nonce_hash, 8);
771 /* LM response is no longer useful */
772 data_blob_free(&ntlmssp_state->lm_resp);
774 /* We changed the effective challenge - set it */
775 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->set_challenge(ntlmssp_state, &ntlmssp_state->chal))) {
776 data_blob_free(&encrypted_session_key);
780 /* LM Key is incompatible. */
781 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
786 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
787 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
788 * smb.conf file) and no NTLMv2 response was sent then the password check
789 * will fail here. JRA.
792 /* Finally, actually ask if the password is OK */
794 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state,
795 &user_session_key, &lm_session_key))) {
796 data_blob_free(&encrypted_session_key);
800 dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
801 dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
803 /* Handle the different session key derivation for NTLM2 */
805 if (user_session_key.data && user_session_key.length == 16) {
806 session_key = data_blob_talloc(ntlmssp_state,
808 hmac_md5(user_session_key.data, session_nonce,
809 sizeof(session_nonce), session_key.data);
810 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
811 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
814 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
815 session_key = data_blob_null;
817 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
818 if (lm_session_key.data && lm_session_key.length >= 8) {
819 if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
820 session_key = data_blob_talloc(ntlmssp_state,
822 if (session_key.data == NULL) {
823 return NT_STATUS_NO_MEMORY;
825 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
827 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
831 session_key = data_blob_talloc(
832 ntlmssp_state, NULL, 16);
833 if (session_key.data == NULL) {
834 return NT_STATUS_NO_MEMORY;
836 SMBsesskeygen_lm_sess_key(
837 lm_session_key.data, zeros,
840 dump_data_pw("LM session key:\n", session_key.data,
843 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
844 session_key = data_blob_null;
846 } else if (user_session_key.data) {
847 session_key = user_session_key;
848 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
849 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
850 } else if (lm_session_key.data) {
851 session_key = lm_session_key;
852 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
853 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
855 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
856 session_key = data_blob_null;
859 /* With KEY_EXCH, the client supplies the proposed session key,
860 but encrypts it with the long-term key */
861 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
862 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
863 data_blob_free(&encrypted_session_key);
864 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
865 (unsigned int)encrypted_session_key.length));
866 return NT_STATUS_INVALID_PARAMETER;
867 } else if (!session_key.data || session_key.length != 16) {
868 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
869 (unsigned int)session_key.length));
870 ntlmssp_state->session_key = session_key;
872 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
873 arcfour_crypt_blob(encrypted_session_key.data,
874 encrypted_session_key.length,
876 ntlmssp_state->session_key = data_blob_talloc(
877 ntlmssp_state, encrypted_session_key.data,
878 encrypted_session_key.length);
879 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
880 encrypted_session_key.length);
883 ntlmssp_state->session_key = session_key;
886 if (!NT_STATUS_IS_OK(nt_status)) {
887 ntlmssp_state->session_key = data_blob_null;
888 } else if (ntlmssp_state->session_key.length) {
889 nt_status = ntlmssp_sign_init(ntlmssp_state);
892 data_blob_free(&encrypted_session_key);
894 /* Only one authentication allowed per server state. */
895 ntlmssp_state->expected_state = NTLMSSP_DONE;
901 * Create an NTLMSSP state machine
903 * @param ntlmssp_state NTLMSSP State, allocated by this function
906 NTSTATUS ntlmssp_server_start(struct ntlmssp_state **ntlmssp_state)
908 *ntlmssp_state = TALLOC_ZERO_P(NULL, struct ntlmssp_state);
909 if (!*ntlmssp_state) {
910 DEBUG(0,("ntlmssp_server_start: talloc failed!\n"));
911 talloc_destroy(*ntlmssp_state);
912 return NT_STATUS_NO_MEMORY;
915 (*ntlmssp_state)->role = NTLMSSP_SERVER;
917 (*ntlmssp_state)->get_challenge = get_challenge;
918 (*ntlmssp_state)->set_challenge = set_challenge;
919 (*ntlmssp_state)->may_set_challenge = may_set_challenge;
921 (*ntlmssp_state)->get_global_myname = global_myname;
922 (*ntlmssp_state)->get_domain = lp_workgroup;
923 (*ntlmssp_state)->server_role = ROLE_DOMAIN_MEMBER; /* a good default */
925 (*ntlmssp_state)->expected_state = NTLMSSP_NEGOTIATE;
927 (*ntlmssp_state)->ref_count = 1;
929 (*ntlmssp_state)->neg_flags =
930 NTLMSSP_NEGOTIATE_128 |
931 NTLMSSP_NEGOTIATE_56 |
932 NTLMSSP_NEGOTIATE_VERSION |
933 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
934 NTLMSSP_NEGOTIATE_NTLM |
935 NTLMSSP_NEGOTIATE_NTLM2 |
936 NTLMSSP_NEGOTIATE_KEY_EXCH |
937 NTLMSSP_NEGOTIATE_SIGN |
938 NTLMSSP_NEGOTIATE_SEAL;
943 /*********************************************************************
945 *********************************************************************/
948 * Next state function for the Initial packet
950 * @param ntlmssp_state NTLMSSP State
951 * @param request The request, as a DATA_BLOB. reply.data must be NULL
952 * @param request The reply, as an allocated DATA_BLOB, caller to free.
953 * @return Errors or NT_STATUS_OK.
956 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
957 DATA_BLOB reply, DATA_BLOB *next_request)
959 struct NEGOTIATE_MESSAGE negotiate;
961 if (ntlmssp_state->unicode) {
962 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
964 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
967 if (ntlmssp_state->use_ntlmv2) {
968 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
971 /* generate the ntlmssp negotiate packet */
972 msrpc_gen(ntlmssp_state, next_request, "CddAA",
975 ntlmssp_state->neg_flags,
976 ntlmssp_state->get_domain(),
977 ntlmssp_state->get_global_myname());
979 if (DEBUGLEVEL >= 10) {
980 if (NT_STATUS_IS_OK(ntlmssp_pull_NEGOTIATE_MESSAGE(next_request,
985 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE, &negotiate);
989 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
991 return NT_STATUS_MORE_PROCESSING_REQUIRED;
995 * Next state function for the Challenge Packet. Generate an auth packet.
997 * @param ntlmssp_state NTLMSSP State
998 * @param request The request, as a DATA_BLOB. reply.data must be NULL
999 * @param request The reply, as an allocated DATA_BLOB, caller to free.
1000 * @return Errors or NT_STATUS_OK.
1003 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
1004 const DATA_BLOB reply, DATA_BLOB *next_request)
1006 uint32 chal_flags, ntlmssp_command, unkn1, unkn2;
1007 DATA_BLOB server_domain_blob;
1008 DATA_BLOB challenge_blob;
1009 DATA_BLOB struct_blob = data_blob_null;
1010 char *server_domain;
1011 const char *chal_parse_string;
1012 const char *auth_gen_string;
1013 DATA_BLOB lm_response = data_blob_null;
1014 DATA_BLOB nt_response = data_blob_null;
1015 DATA_BLOB session_key = data_blob_null;
1016 DATA_BLOB encrypted_session_key = data_blob_null;
1017 NTSTATUS nt_status = NT_STATUS_OK;
1018 struct CHALLENGE_MESSAGE challenge;
1019 struct AUTHENTICATE_MESSAGE authenticate;
1021 if (!msrpc_parse(ntlmssp_state, &reply, "CdBd",
1024 &server_domain_blob,
1026 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
1027 dump_data(2, reply.data, reply.length);
1029 return NT_STATUS_INVALID_PARAMETER;
1032 if (DEBUGLEVEL >= 10) {
1033 if (NT_STATUS_IS_OK(ntlmssp_pull_CHALLENGE_MESSAGE(&reply,
1038 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE, &challenge);
1042 data_blob_free(&server_domain_blob);
1044 DEBUG(3, ("Got challenge flags:\n"));
1045 debug_ntlmssp_flags(chal_flags);
1047 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
1049 if (ntlmssp_state->unicode) {
1050 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
1051 chal_parse_string = "CdUdbddB";
1053 chal_parse_string = "CdUdbdd";
1055 auth_gen_string = "CdBBUUUBd";
1057 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
1058 chal_parse_string = "CdAdbddB";
1060 chal_parse_string = "CdAdbdd";
1063 auth_gen_string = "CdBBAAABd";
1066 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
1067 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
1069 if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
1077 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
1078 dump_data(2, reply.data, reply.length);
1079 return NT_STATUS_INVALID_PARAMETER;
1082 ntlmssp_state->server_domain = server_domain;
1084 if (challenge_blob.length != 8) {
1085 data_blob_free(&struct_blob);
1086 return NT_STATUS_INVALID_PARAMETER;
1089 if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
1091 /* do nothing - blobs are zero length */
1095 /* session key is all zeros */
1096 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
1098 /* not doing NLTM2 without a password */
1099 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
1100 } else if (ntlmssp_state->use_ntlmv2) {
1101 if (!struct_blob.length) {
1102 /* be lazy, match win2k - we can't do NTLMv2 without it */
1103 DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1104 return NT_STATUS_INVALID_PARAMETER;
1107 /* TODO: if the remote server is standalone, then we should replace 'domain'
1108 with the server name as supplied above */
1110 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
1111 ntlmssp_state->user,
1112 ntlmssp_state->domain,
1113 ntlmssp_state->nt_hash, &challenge_blob,
1115 &lm_response, &nt_response, NULL,
1117 data_blob_free(&challenge_blob);
1118 data_blob_free(&struct_blob);
1119 return NT_STATUS_NO_MEMORY;
1121 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1122 struct MD5Context md5_session_nonce_ctx;
1123 uchar session_nonce[16];
1124 uchar session_nonce_hash[16];
1125 uchar user_session_key[16];
1127 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1128 generate_random_buffer(lm_response.data, 8);
1129 memset(lm_response.data+8, 0, 16);
1131 memcpy(session_nonce, challenge_blob.data, 8);
1132 memcpy(&session_nonce[8], lm_response.data, 8);
1134 MD5Init(&md5_session_nonce_ctx);
1135 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1136 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1137 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1139 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1140 DEBUG(5, ("challenge is: \n"));
1141 dump_data(5, session_nonce_hash, 8);
1143 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1144 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
1148 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1150 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
1151 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1152 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1154 /* lanman auth is insecure, it may be disabled */
1155 if (lp_client_lanman_auth()) {
1156 lm_response = data_blob_talloc(ntlmssp_state,
1158 SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
1162 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1163 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
1166 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1167 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1168 && lp_client_lanman_auth()) {
1169 SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
1171 dump_data_pw("LM session key\n", session_key.data, session_key.length);
1173 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
1174 dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1177 data_blob_free(&struct_blob);
1179 /* Key exchange encryptes a new client-generated session key with
1180 the password-derived key */
1181 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1182 /* Make up a new session key */
1183 uint8 client_session_key[16];
1184 generate_random_buffer(client_session_key, sizeof(client_session_key));
1186 /* Encrypt the new session key with the old one */
1187 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1188 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1189 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
1190 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1192 /* Mark the new session key as the 'real' session key */
1193 data_blob_free(&session_key);
1194 session_key = data_blob_talloc(ntlmssp_state,
1196 sizeof(client_session_key));
1199 /* this generates the actual auth packet */
1200 if (!msrpc_gen(ntlmssp_state, next_request, auth_gen_string,
1203 lm_response.data, lm_response.length,
1204 nt_response.data, nt_response.length,
1205 ntlmssp_state->domain,
1206 ntlmssp_state->user,
1207 ntlmssp_state->get_global_myname(),
1208 encrypted_session_key.data, encrypted_session_key.length,
1209 ntlmssp_state->neg_flags)) {
1211 return NT_STATUS_NO_MEMORY;
1214 if (DEBUGLEVEL >= 10) {
1215 if (NT_STATUS_IS_OK(ntlmssp_pull_AUTHENTICATE_MESSAGE(next_request,
1220 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE, &authenticate);
1224 data_blob_free(&encrypted_session_key);
1226 data_blob_free(&ntlmssp_state->chal);
1228 ntlmssp_state->session_key = session_key;
1230 ntlmssp_state->chal = challenge_blob;
1231 ntlmssp_state->lm_resp = lm_response;
1232 ntlmssp_state->nt_resp = nt_response;
1234 ntlmssp_state->expected_state = NTLMSSP_DONE;
1236 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1237 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1243 NTSTATUS ntlmssp_client_start(struct ntlmssp_state **ntlmssp_state)
1245 *ntlmssp_state = TALLOC_ZERO_P(NULL, struct ntlmssp_state);
1246 if (!*ntlmssp_state) {
1247 DEBUG(0,("ntlmssp_client_start: talloc failed!\n"));
1248 talloc_destroy(*ntlmssp_state);
1249 return NT_STATUS_NO_MEMORY;
1252 (*ntlmssp_state)->role = NTLMSSP_CLIENT;
1254 (*ntlmssp_state)->get_global_myname = global_myname;
1255 (*ntlmssp_state)->get_domain = lp_workgroup;
1257 (*ntlmssp_state)->unicode = True;
1259 (*ntlmssp_state)->use_ntlmv2 = lp_client_ntlmv2_auth();
1261 (*ntlmssp_state)->expected_state = NTLMSSP_INITIAL;
1263 (*ntlmssp_state)->ref_count = 1;
1265 (*ntlmssp_state)->neg_flags =
1266 NTLMSSP_NEGOTIATE_128 |
1267 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
1268 NTLMSSP_NEGOTIATE_NTLM |
1269 NTLMSSP_NEGOTIATE_NTLM2 |
1270 NTLMSSP_NEGOTIATE_KEY_EXCH |
1271 NTLMSSP_REQUEST_TARGET;
1273 return NT_STATUS_OK;