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/>.
25 #include "../libcli/auth/libcli_auth.h"
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)
76 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_DATAGRAM\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_OEM_DOMAIN_SUPPLIED)
84 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED\n"));
85 if (neg_flags & NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED)
86 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM_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_REQUEST_NON_NT_SESSION_KEY)
92 DEBUGADD(4, (" NTLMSSP_REQUEST_NON_NT_SESSION_KEY\n"));
93 if (neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
94 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM2\n"));
95 if (neg_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
96 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_TARGET_INFO\n"));
97 if (neg_flags & NTLMSSP_NEGOTIATE_VERSION)
98 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_VERSION\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 void get_challenge(const struct ntlmssp_state *ntlmssp_state,
115 generate_random_buffer(chal, 8);
119 * Default 'we can set the challenge to anything we like' implementation
123 static bool may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
129 * Default 'we can set the challenge to anything we like' implementation
131 * Does not actually do anything, as the value is always in the structure anyway.
135 static NTSTATUS set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
137 SMB_ASSERT(challenge->length == 8);
142 * Set a username on an NTLMSSP context - ensures it is talloc()ed
146 NTSTATUS ntlmssp_set_username(NTLMSSP_STATE *ntlmssp_state, const char *user)
148 ntlmssp_state->user = talloc_strdup(ntlmssp_state, user ? user : "" );
149 if (!ntlmssp_state->user) {
150 return NT_STATUS_NO_MEMORY;
156 * Store NT and LM hashes on an NTLMSSP context - ensures they are talloc()ed
159 NTSTATUS ntlmssp_set_hashes(NTLMSSP_STATE *ntlmssp_state,
160 const unsigned char lm_hash[16],
161 const unsigned char nt_hash[16])
163 ntlmssp_state->lm_hash = (unsigned char *)
164 TALLOC_MEMDUP(ntlmssp_state, lm_hash, 16);
165 ntlmssp_state->nt_hash = (unsigned char *)
166 TALLOC_MEMDUP(ntlmssp_state, nt_hash, 16);
167 if (!ntlmssp_state->lm_hash || !ntlmssp_state->nt_hash) {
168 TALLOC_FREE(ntlmssp_state->lm_hash);
169 TALLOC_FREE(ntlmssp_state->nt_hash);
170 return NT_STATUS_NO_MEMORY;
176 * Converts a password to the hashes on an NTLMSSP context.
179 NTSTATUS ntlmssp_set_password(NTLMSSP_STATE *ntlmssp_state, const char *password)
182 ntlmssp_state->lm_hash = NULL;
183 ntlmssp_state->nt_hash = NULL;
185 unsigned char lm_hash[16];
186 unsigned char nt_hash[16];
188 E_deshash(password, lm_hash);
189 E_md4hash(password, nt_hash);
190 return ntlmssp_set_hashes(ntlmssp_state, lm_hash, nt_hash);
196 * Set a domain on an NTLMSSP context - ensures it is talloc()ed
199 NTSTATUS ntlmssp_set_domain(NTLMSSP_STATE *ntlmssp_state, const char *domain)
201 ntlmssp_state->domain = talloc_strdup(ntlmssp_state,
202 domain ? domain : "" );
203 if (!ntlmssp_state->domain) {
204 return NT_STATUS_NO_MEMORY;
210 * Set a workstation on an NTLMSSP context - ensures it is talloc()ed
213 NTSTATUS ntlmssp_set_workstation(NTLMSSP_STATE *ntlmssp_state, const char *workstation)
215 ntlmssp_state->workstation = talloc_strdup(ntlmssp_state, workstation);
216 if (!ntlmssp_state->workstation) {
217 return NT_STATUS_NO_MEMORY;
223 * Store a DATA_BLOB containing an NTLMSSP response, for use later.
224 * This copies the data blob
227 NTSTATUS ntlmssp_store_response(NTLMSSP_STATE *ntlmssp_state,
230 ntlmssp_state->stored_response = data_blob_talloc(ntlmssp_state,
237 * Request features for the NTLMSSP negotiation
239 * @param ntlmssp_state NTLMSSP state
240 * @param feature_list List of space seperated features requested from NTLMSSP.
242 void ntlmssp_want_feature_list(NTLMSSP_STATE *ntlmssp_state, char *feature_list)
245 * We need to set this to allow a later SetPassword
246 * via the SAMR pipe to succeed. Strange.... We could
247 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
249 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
250 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
252 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
253 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
255 if(in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
256 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
261 * Request a feature for the NTLMSSP negotiation
263 * @param ntlmssp_state NTLMSSP state
264 * @param feature Bit flag specifying the requested feature
266 void ntlmssp_want_feature(NTLMSSP_STATE *ntlmssp_state, uint32 feature)
268 /* As per JRA's comment above */
269 if (feature & NTLMSSP_FEATURE_SESSION_KEY) {
270 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
272 if (feature & NTLMSSP_FEATURE_SIGN) {
273 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
275 if (feature & NTLMSSP_FEATURE_SEAL) {
276 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
281 * Next state function for the NTLMSSP state machine
283 * @param ntlmssp_state NTLMSSP State
284 * @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
285 * @param out The reply, as an allocated DATA_BLOB, caller to free.
286 * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
289 NTSTATUS ntlmssp_update(NTLMSSP_STATE *ntlmssp_state,
290 const DATA_BLOB in, DATA_BLOB *out)
293 uint32 ntlmssp_command;
296 if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
297 /* Called update after negotiations finished. */
298 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
299 return NT_STATUS_INVALID_PARAMETER;
302 *out = data_blob_null;
304 if (!in.length && ntlmssp_state->stored_response.length) {
305 input = ntlmssp_state->stored_response;
307 /* we only want to read the stored response once - overwrite it */
308 ntlmssp_state->stored_response = data_blob_null;
314 switch (ntlmssp_state->role) {
316 ntlmssp_command = NTLMSSP_INITIAL;
319 /* 'datagram' mode - no neg packet */
320 ntlmssp_command = NTLMSSP_NEGOTIATE;
324 if (!msrpc_parse(ntlmssp_state, &input, "Cd",
327 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
328 dump_data(2, input.data, input.length);
329 return NT_STATUS_INVALID_PARAMETER;
333 if (ntlmssp_command != ntlmssp_state->expected_state) {
334 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
335 return NT_STATUS_INVALID_PARAMETER;
338 for (i=0; ntlmssp_callbacks[i].fn; i++) {
339 if (ntlmssp_callbacks[i].role == ntlmssp_state->role
340 && ntlmssp_callbacks[i].ntlmssp_command == ntlmssp_command) {
341 return ntlmssp_callbacks[i].fn(ntlmssp_state, input, out);
345 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
346 ntlmssp_state->role, ntlmssp_command));
348 return NT_STATUS_INVALID_PARAMETER;
352 * End an NTLMSSP state machine
354 * @param ntlmssp_state NTLMSSP State, free()ed by this function
357 void ntlmssp_end(NTLMSSP_STATE **ntlmssp_state)
359 (*ntlmssp_state)->ref_count--;
361 if ((*ntlmssp_state)->ref_count == 0) {
362 data_blob_free(&(*ntlmssp_state)->chal);
363 data_blob_free(&(*ntlmssp_state)->lm_resp);
364 data_blob_free(&(*ntlmssp_state)->nt_resp);
365 TALLOC_FREE(*ntlmssp_state);
368 *ntlmssp_state = NULL;
373 * Determine correct target name flags for reply, given server role
374 * and negotiated flags
376 * @param ntlmssp_state NTLMSSP State
377 * @param neg_flags The flags from the packet
378 * @param chal_flags The flags to be set in the reply packet
379 * @return The 'target name' string.
382 static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
383 uint32 neg_flags, uint32 *chal_flags)
385 if (neg_flags & NTLMSSP_REQUEST_TARGET) {
386 *chal_flags |= NTLMSSP_NEGOTIATE_TARGET_INFO;
387 *chal_flags |= NTLMSSP_REQUEST_TARGET;
388 if (ntlmssp_state->server_role == ROLE_STANDALONE) {
389 *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
390 return ntlmssp_state->get_global_myname();
392 *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
393 return ntlmssp_state->get_domain();
400 static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
401 uint32 neg_flags, bool allow_lm) {
402 if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
403 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
404 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_OEM;
405 ntlmssp_state->unicode = True;
407 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_UNICODE;
408 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
409 ntlmssp_state->unicode = False;
412 if ((neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) && allow_lm) {
413 /* other end forcing us to use LM */
414 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
415 ntlmssp_state->use_ntlmv2 = False;
417 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
420 if (!(neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)) {
421 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
424 if (!(neg_flags & NTLMSSP_NEGOTIATE_NTLM2)) {
425 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
428 if (!(neg_flags & NTLMSSP_NEGOTIATE_128)) {
429 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_128;
432 if (!(neg_flags & NTLMSSP_NEGOTIATE_56)) {
433 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_56;
436 if (!(neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
437 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_KEY_EXCH;
440 if (!(neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
441 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SIGN;
444 if (!(neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
445 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SEAL;
448 /* Woop Woop - unknown flag for Windows compatibility...
449 What does this really do ? JRA. */
450 if (!(neg_flags & NTLMSSP_NEGOTIATE_VERSION)) {
451 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_VERSION;
454 if ((neg_flags & NTLMSSP_REQUEST_TARGET)) {
455 ntlmssp_state->neg_flags |= NTLMSSP_REQUEST_TARGET;
460 Weaken NTLMSSP keys to cope with down-level clients and servers.
462 We probably should have some parameters to control this, but as
463 it only occours for LM_KEY connections, and this is controlled
464 by the client lanman auth/lanman auth parameters, it isn't too bad.
467 DATA_BLOB ntlmssp_weaken_keys(NTLMSSP_STATE *ntlmssp_state, TALLOC_CTX *mem_ctx)
469 DATA_BLOB weakened_key = data_blob_talloc(mem_ctx,
470 ntlmssp_state->session_key.data,
471 ntlmssp_state->session_key.length);
473 /* Nothing to weaken. We certainly don't want to 'extend' the length... */
474 if (weakened_key.length < 16) {
475 /* perhaps there was no key? */
479 /* Key weakening not performed on the master key for NTLM2
480 and does not occour for NTLM1. Therefore we only need
481 to do this for the LM_KEY.
484 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
485 /* LM key doesn't support 128 bit crypto, so this is
486 * the best we can do. If you negotiate 128 bit, but
487 * not 56, you end up with 40 bit... */
488 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
489 weakened_key.data[7] = 0xa0;
490 } else { /* forty bits */
491 weakened_key.data[5] = 0xe5;
492 weakened_key.data[6] = 0x38;
493 weakened_key.data[7] = 0xb0;
495 weakened_key.length = 8;
501 * Next state function for the Negotiate packet
503 * @param ntlmssp_state NTLMSSP State
504 * @param request The request, as a DATA_BLOB
505 * @param request The reply, as an allocated DATA_BLOB, caller to free.
506 * @return Errors or MORE_PROCESSING_REQUIRED if a reply is sent.
509 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
510 const DATA_BLOB request, DATA_BLOB *reply)
512 DATA_BLOB struct_blob;
514 char *dnsdomname = NULL;
515 uint32 neg_flags = 0;
516 uint32 ntlmssp_command, chal_flags;
518 const char *target_name;
520 /* parse the NTLMSSP packet */
522 file_save("ntlmssp_negotiate.dat", request.data, request.length);
525 if (request.length) {
526 if ((request.length < 16) || !msrpc_parse(ntlmssp_state, &request, "Cdd",
530 DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
531 (unsigned int)request.length));
532 dump_data(2, request.data, request.length);
533 return NT_STATUS_INVALID_PARAMETER;
535 debug_ntlmssp_flags(neg_flags);
538 ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, lp_lanman_auth());
540 /* Ask our caller what challenge they would like in the packet */
541 ntlmssp_state->get_challenge(ntlmssp_state, cryptkey);
543 /* Check if we may set the challenge */
544 if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
545 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
548 /* The flags we send back are not just the negotiated flags,
549 * they are also 'what is in this packet'. Therfore, we
550 * operate on 'chal_flags' from here on
553 chal_flags = ntlmssp_state->neg_flags;
555 /* get the right name to fill in as 'target' */
556 target_name = ntlmssp_target_name(ntlmssp_state,
557 neg_flags, &chal_flags);
558 if (target_name == NULL)
559 return NT_STATUS_INVALID_PARAMETER;
561 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
562 ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state,
565 /* This should be a 'netbios domain -> DNS domain' mapping */
566 dnsdomname = get_mydnsdomname(ntlmssp_state);
568 dnsdomname = talloc_strdup(ntlmssp_state, "");
571 return NT_STATUS_NO_MEMORY;
573 strlower_m(dnsdomname);
575 dnsname = get_mydnsfullname();
580 /* This creates the 'blob' of names that appears at the end of the packet */
581 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
583 msrpc_gen(ntlmssp_state, &struct_blob, "aaaaa",
584 NTLMSSP_NAME_TYPE_DOMAIN, target_name,
585 NTLMSSP_NAME_TYPE_SERVER, ntlmssp_state->get_global_myname(),
586 NTLMSSP_NAME_TYPE_DOMAIN_DNS, dnsdomname,
587 NTLMSSP_NAME_TYPE_SERVER_DNS, dnsname,
590 struct_blob = data_blob_null;
594 /* Marshel the packet in the right format, be it unicode or ASCII */
595 const char *gen_string;
596 if (ntlmssp_state->unicode) {
597 gen_string = "CdUdbddB";
599 gen_string = "CdAdbddB";
602 msrpc_gen(ntlmssp_state, reply, gen_string,
609 struct_blob.data, struct_blob.length);
612 data_blob_free(&struct_blob);
614 ntlmssp_state->expected_state = NTLMSSP_AUTH;
616 return NT_STATUS_MORE_PROCESSING_REQUIRED;
620 * Next state function for the Authenticate packet
622 * @param ntlmssp_state NTLMSSP State
623 * @param request The request, as a DATA_BLOB
624 * @param request The reply, as an allocated DATA_BLOB, caller to free.
625 * @return Errors or NT_STATUS_OK.
628 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
629 const DATA_BLOB request, DATA_BLOB *reply)
631 DATA_BLOB encrypted_session_key = data_blob_null;
632 DATA_BLOB user_session_key = data_blob_null;
633 DATA_BLOB lm_session_key = data_blob_null;
634 DATA_BLOB session_key = data_blob_null;
635 uint32 ntlmssp_command, auth_flags;
636 NTSTATUS nt_status = NT_STATUS_OK;
639 bool doing_ntlm2 = False;
641 uchar session_nonce[16];
642 uchar session_nonce_hash[16];
644 const char *parse_string;
646 /* parse the NTLMSSP packet */
647 *reply = data_blob_null;
650 file_save("ntlmssp_auth.dat", request.data, request.length);
653 if (ntlmssp_state->unicode) {
654 parse_string = "CdBBUUUBd";
656 parse_string = "CdBBAAABd";
659 data_blob_free(&ntlmssp_state->lm_resp);
660 data_blob_free(&ntlmssp_state->nt_resp);
662 ntlmssp_state->user = NULL;
663 ntlmssp_state->domain = NULL;
664 ntlmssp_state->workstation = NULL;
666 /* now the NTLMSSP encoded auth hashes */
667 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
670 &ntlmssp_state->lm_resp,
671 &ntlmssp_state->nt_resp,
672 &ntlmssp_state->domain,
673 &ntlmssp_state->user,
674 &ntlmssp_state->workstation,
675 &encrypted_session_key,
679 /* Try again with a shorter string (Win9X truncates this packet) */
680 if (ntlmssp_state->unicode) {
681 parse_string = "CdBBUUU";
683 parse_string = "CdBBAAA";
686 /* now the NTLMSSP encoded auth hashes */
687 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
690 &ntlmssp_state->lm_resp,
691 &ntlmssp_state->nt_resp,
692 &ntlmssp_state->domain,
693 &ntlmssp_state->user,
694 &ntlmssp_state->workstation)) {
695 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
696 dump_data(2, request.data, request.length);
698 return NT_STATUS_INVALID_PARAMETER;
703 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, lp_lanman_auth());
705 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
706 ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->workstation, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
709 file_save("nthash1.dat", &ntlmssp_state->nt_resp.data, &ntlmssp_state->nt_resp.length);
710 file_save("lmhash1.dat", &ntlmssp_state->lm_resp.data, &ntlmssp_state->lm_resp.length);
713 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
716 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
718 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
719 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
720 struct MD5Context md5_session_nonce_ctx;
721 SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
725 memcpy(session_nonce, ntlmssp_state->internal_chal.data, 8);
726 memcpy(&session_nonce[8], ntlmssp_state->lm_resp.data, 8);
728 MD5Init(&md5_session_nonce_ctx);
729 MD5Update(&md5_session_nonce_ctx, session_nonce, 16);
730 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
732 ntlmssp_state->chal = data_blob_talloc(
733 ntlmssp_state, session_nonce_hash, 8);
735 /* LM response is no longer useful */
736 data_blob_free(&ntlmssp_state->lm_resp);
738 /* We changed the effective challenge - set it */
739 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->set_challenge(ntlmssp_state, &ntlmssp_state->chal))) {
740 data_blob_free(&encrypted_session_key);
744 /* LM Key is incompatible. */
745 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
750 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
751 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
752 * smb.conf file) and no NTLMv2 response was sent then the password check
753 * will fail here. JRA.
756 /* Finally, actually ask if the password is OK */
758 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state,
759 &user_session_key, &lm_session_key))) {
760 data_blob_free(&encrypted_session_key);
764 dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
765 dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
767 /* Handle the different session key derivation for NTLM2 */
769 if (user_session_key.data && user_session_key.length == 16) {
770 session_key = data_blob_talloc(ntlmssp_state,
772 hmac_md5(user_session_key.data, session_nonce,
773 sizeof(session_nonce), session_key.data);
774 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
775 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
778 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
779 session_key = data_blob_null;
781 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
782 if (lm_session_key.data && lm_session_key.length >= 8) {
783 if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
784 session_key = data_blob_talloc(ntlmssp_state,
786 if (session_key.data == NULL) {
787 return NT_STATUS_NO_MEMORY;
789 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
791 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
795 session_key = data_blob_talloc(
796 ntlmssp_state, NULL, 16);
797 if (session_key.data == NULL) {
798 return NT_STATUS_NO_MEMORY;
800 SMBsesskeygen_lm_sess_key(
801 lm_session_key.data, zeros,
804 dump_data_pw("LM session key:\n", session_key.data,
807 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
808 session_key = data_blob_null;
810 } else if (user_session_key.data) {
811 session_key = user_session_key;
812 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
813 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
814 } else if (lm_session_key.data) {
815 session_key = lm_session_key;
816 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
817 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
819 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
820 session_key = data_blob_null;
823 /* With KEY_EXCH, the client supplies the proposed session key,
824 but encrypts it with the long-term key */
825 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
826 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
827 data_blob_free(&encrypted_session_key);
828 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
829 (unsigned int)encrypted_session_key.length));
830 return NT_STATUS_INVALID_PARAMETER;
831 } else if (!session_key.data || session_key.length != 16) {
832 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
833 (unsigned int)session_key.length));
834 ntlmssp_state->session_key = session_key;
836 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
837 arcfour_crypt_blob(encrypted_session_key.data,
838 encrypted_session_key.length,
840 ntlmssp_state->session_key = data_blob_talloc(
841 ntlmssp_state, encrypted_session_key.data,
842 encrypted_session_key.length);
843 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
844 encrypted_session_key.length);
847 ntlmssp_state->session_key = session_key;
850 if (!NT_STATUS_IS_OK(nt_status)) {
851 ntlmssp_state->session_key = data_blob_null;
852 } else if (ntlmssp_state->session_key.length) {
853 nt_status = ntlmssp_sign_init(ntlmssp_state);
856 data_blob_free(&encrypted_session_key);
858 /* Only one authentication allowed per server state. */
859 ntlmssp_state->expected_state = NTLMSSP_DONE;
865 * Create an NTLMSSP state machine
867 * @param ntlmssp_state NTLMSSP State, allocated by this function
870 NTSTATUS ntlmssp_server_start(NTLMSSP_STATE **ntlmssp_state)
872 *ntlmssp_state = TALLOC_ZERO_P(NULL, NTLMSSP_STATE);
873 if (!*ntlmssp_state) {
874 DEBUG(0,("ntlmssp_server_start: talloc failed!\n"));
875 talloc_destroy(*ntlmssp_state);
876 return NT_STATUS_NO_MEMORY;
879 (*ntlmssp_state)->role = NTLMSSP_SERVER;
881 (*ntlmssp_state)->get_challenge = get_challenge;
882 (*ntlmssp_state)->set_challenge = set_challenge;
883 (*ntlmssp_state)->may_set_challenge = may_set_challenge;
885 (*ntlmssp_state)->get_global_myname = global_myname;
886 (*ntlmssp_state)->get_domain = lp_workgroup;
887 (*ntlmssp_state)->server_role = ROLE_DOMAIN_MEMBER; /* a good default */
889 (*ntlmssp_state)->expected_state = NTLMSSP_NEGOTIATE;
891 (*ntlmssp_state)->ref_count = 1;
893 (*ntlmssp_state)->neg_flags =
894 NTLMSSP_NEGOTIATE_128 |
895 NTLMSSP_NEGOTIATE_56 |
896 NTLMSSP_NEGOTIATE_VERSION |
897 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
898 NTLMSSP_NEGOTIATE_NTLM |
899 NTLMSSP_NEGOTIATE_NTLM2 |
900 NTLMSSP_NEGOTIATE_KEY_EXCH |
901 NTLMSSP_NEGOTIATE_SIGN |
902 NTLMSSP_NEGOTIATE_SEAL;
907 /*********************************************************************
909 *********************************************************************/
912 * Next state function for the Initial packet
914 * @param ntlmssp_state NTLMSSP State
915 * @param request The request, as a DATA_BLOB. reply.data must be NULL
916 * @param request The reply, as an allocated DATA_BLOB, caller to free.
917 * @return Errors or NT_STATUS_OK.
920 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
921 DATA_BLOB reply, DATA_BLOB *next_request)
923 if (ntlmssp_state->unicode) {
924 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
926 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
929 if (ntlmssp_state->use_ntlmv2) {
930 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
933 /* generate the ntlmssp negotiate packet */
934 msrpc_gen(ntlmssp_state, next_request, "CddAA",
937 ntlmssp_state->neg_flags,
938 ntlmssp_state->get_domain(),
939 ntlmssp_state->get_global_myname());
941 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
943 return NT_STATUS_MORE_PROCESSING_REQUIRED;
947 * Next state function for the Challenge Packet. Generate an auth packet.
949 * @param ntlmssp_state NTLMSSP State
950 * @param request The request, as a DATA_BLOB. reply.data must be NULL
951 * @param request The reply, as an allocated DATA_BLOB, caller to free.
952 * @return Errors or NT_STATUS_OK.
955 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
956 const DATA_BLOB reply, DATA_BLOB *next_request)
958 uint32 chal_flags, ntlmssp_command, unkn1, unkn2;
959 DATA_BLOB server_domain_blob;
960 DATA_BLOB challenge_blob;
961 DATA_BLOB struct_blob = data_blob_null;
963 const char *chal_parse_string;
964 const char *auth_gen_string;
965 DATA_BLOB lm_response = data_blob_null;
966 DATA_BLOB nt_response = data_blob_null;
967 DATA_BLOB session_key = data_blob_null;
968 DATA_BLOB encrypted_session_key = data_blob_null;
969 NTSTATUS nt_status = NT_STATUS_OK;
971 if (!msrpc_parse(ntlmssp_state, &reply, "CdBd",
976 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
977 dump_data(2, reply.data, reply.length);
979 return NT_STATUS_INVALID_PARAMETER;
982 data_blob_free(&server_domain_blob);
984 DEBUG(3, ("Got challenge flags:\n"));
985 debug_ntlmssp_flags(chal_flags);
987 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
989 if (ntlmssp_state->unicode) {
990 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
991 chal_parse_string = "CdUdbddB";
993 chal_parse_string = "CdUdbdd";
995 auth_gen_string = "CdBBUUUBd";
997 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
998 chal_parse_string = "CdAdbddB";
1000 chal_parse_string = "CdAdbdd";
1003 auth_gen_string = "CdBBAAABd";
1006 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
1007 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
1009 if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
1017 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
1018 dump_data(2, reply.data, reply.length);
1019 return NT_STATUS_INVALID_PARAMETER;
1022 ntlmssp_state->server_domain = server_domain;
1024 if (challenge_blob.length != 8) {
1025 data_blob_free(&struct_blob);
1026 return NT_STATUS_INVALID_PARAMETER;
1029 if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
1031 /* do nothing - blobs are zero length */
1035 /* session key is all zeros */
1036 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
1038 /* not doing NLTM2 without a password */
1039 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
1040 } else if (ntlmssp_state->use_ntlmv2) {
1041 if (!struct_blob.length) {
1042 /* be lazy, match win2k - we can't do NTLMv2 without it */
1043 DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1044 return NT_STATUS_INVALID_PARAMETER;
1047 /* TODO: if the remote server is standalone, then we should replace 'domain'
1048 with the server name as supplied above */
1050 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
1051 ntlmssp_state->user,
1052 ntlmssp_state->domain,
1053 ntlmssp_state->nt_hash, &challenge_blob,
1055 &lm_response, &nt_response, NULL,
1057 data_blob_free(&challenge_blob);
1058 data_blob_free(&struct_blob);
1059 return NT_STATUS_NO_MEMORY;
1061 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1062 struct MD5Context md5_session_nonce_ctx;
1063 uchar session_nonce[16];
1064 uchar session_nonce_hash[16];
1065 uchar user_session_key[16];
1067 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1068 generate_random_buffer(lm_response.data, 8);
1069 memset(lm_response.data+8, 0, 16);
1071 memcpy(session_nonce, challenge_blob.data, 8);
1072 memcpy(&session_nonce[8], lm_response.data, 8);
1074 MD5Init(&md5_session_nonce_ctx);
1075 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1076 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1077 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1079 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1080 DEBUG(5, ("challenge is: \n"));
1081 dump_data(5, session_nonce_hash, 8);
1083 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1084 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
1088 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1090 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
1091 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1092 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1094 /* lanman auth is insecure, it may be disabled */
1095 if (lp_client_lanman_auth()) {
1096 lm_response = data_blob_talloc(ntlmssp_state,
1098 SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
1102 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1103 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
1106 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1107 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1108 && lp_client_lanman_auth()) {
1109 SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
1111 dump_data_pw("LM session key\n", session_key.data, session_key.length);
1113 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
1114 dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1117 data_blob_free(&struct_blob);
1119 /* Key exchange encryptes a new client-generated session key with
1120 the password-derived key */
1121 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1122 /* Make up a new session key */
1123 uint8 client_session_key[16];
1124 generate_random_buffer(client_session_key, sizeof(client_session_key));
1126 /* Encrypt the new session key with the old one */
1127 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1128 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1129 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
1130 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1132 /* Mark the new session key as the 'real' session key */
1133 data_blob_free(&session_key);
1134 session_key = data_blob_talloc(ntlmssp_state,
1136 sizeof(client_session_key));
1139 /* this generates the actual auth packet */
1140 if (!msrpc_gen(ntlmssp_state, next_request, auth_gen_string,
1143 lm_response.data, lm_response.length,
1144 nt_response.data, nt_response.length,
1145 ntlmssp_state->domain,
1146 ntlmssp_state->user,
1147 ntlmssp_state->get_global_myname(),
1148 encrypted_session_key.data, encrypted_session_key.length,
1149 ntlmssp_state->neg_flags)) {
1151 return NT_STATUS_NO_MEMORY;
1154 data_blob_free(&encrypted_session_key);
1156 data_blob_free(&ntlmssp_state->chal);
1158 ntlmssp_state->session_key = session_key;
1160 ntlmssp_state->chal = challenge_blob;
1161 ntlmssp_state->lm_resp = lm_response;
1162 ntlmssp_state->nt_resp = nt_response;
1164 ntlmssp_state->expected_state = NTLMSSP_DONE;
1166 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1167 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1173 NTSTATUS ntlmssp_client_start(NTLMSSP_STATE **ntlmssp_state)
1175 *ntlmssp_state = TALLOC_ZERO_P(NULL, NTLMSSP_STATE);
1176 if (!*ntlmssp_state) {
1177 DEBUG(0,("ntlmssp_client_start: talloc failed!\n"));
1178 talloc_destroy(*ntlmssp_state);
1179 return NT_STATUS_NO_MEMORY;
1182 (*ntlmssp_state)->role = NTLMSSP_CLIENT;
1184 (*ntlmssp_state)->get_global_myname = global_myname;
1185 (*ntlmssp_state)->get_domain = lp_workgroup;
1187 (*ntlmssp_state)->unicode = True;
1189 (*ntlmssp_state)->use_ntlmv2 = lp_client_ntlmv2_auth();
1191 (*ntlmssp_state)->expected_state = NTLMSSP_INITIAL;
1193 (*ntlmssp_state)->ref_count = 1;
1195 (*ntlmssp_state)->neg_flags =
1196 NTLMSSP_NEGOTIATE_128 |
1197 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
1198 NTLMSSP_NEGOTIATE_NTLM |
1199 NTLMSSP_NEGOTIATE_NTLM2 |
1200 NTLMSSP_NEGOTIATE_KEY_EXCH |
1201 NTLMSSP_REQUEST_TARGET;
1203 return NT_STATUS_OK;