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_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_VERSION)
100 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_VERSION\n"));
101 if (neg_flags & NTLMSSP_NEGOTIATE_128)
102 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_128\n"));
103 if (neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)
104 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_KEY_EXCH\n"));
105 if (neg_flags & NTLMSSP_NEGOTIATE_56)
106 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_56\n"));
110 * Default challenge generation code.
114 static void get_challenge(const struct ntlmssp_state *ntlmssp_state,
117 generate_random_buffer(chal, 8);
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, 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 = (unsigned char *)
166 TALLOC_MEMDUP(ntlmssp_state, lm_hash, 16);
167 ntlmssp_state->nt_hash = (unsigned char *)
168 TALLOC_MEMDUP(ntlmssp_state, nt_hash, 16);
169 if (!ntlmssp_state->lm_hash || !ntlmssp_state->nt_hash) {
170 TALLOC_FREE(ntlmssp_state->lm_hash);
171 TALLOC_FREE(ntlmssp_state->nt_hash);
172 return NT_STATUS_NO_MEMORY;
178 * Converts a password to the hashes on an NTLMSSP context.
181 NTSTATUS ntlmssp_set_password(NTLMSSP_STATE *ntlmssp_state, const char *password)
184 ntlmssp_state->lm_hash = NULL;
185 ntlmssp_state->nt_hash = NULL;
187 unsigned char lm_hash[16];
188 unsigned char nt_hash[16];
190 E_deshash(password, lm_hash);
191 E_md4hash(password, nt_hash);
192 return ntlmssp_set_hashes(ntlmssp_state, lm_hash, nt_hash);
198 * Set a domain on an NTLMSSP context - ensures it is talloc()ed
201 NTSTATUS ntlmssp_set_domain(NTLMSSP_STATE *ntlmssp_state, const char *domain)
203 ntlmssp_state->domain = talloc_strdup(ntlmssp_state,
204 domain ? domain : "" );
205 if (!ntlmssp_state->domain) {
206 return NT_STATUS_NO_MEMORY;
212 * Set a workstation on an NTLMSSP context - ensures it is talloc()ed
215 NTSTATUS ntlmssp_set_workstation(NTLMSSP_STATE *ntlmssp_state, const char *workstation)
217 ntlmssp_state->workstation = talloc_strdup(ntlmssp_state, workstation);
218 if (!ntlmssp_state->workstation) {
219 return NT_STATUS_NO_MEMORY;
225 * Store a DATA_BLOB containing an NTLMSSP response, for use later.
226 * This copies the data blob
229 NTSTATUS ntlmssp_store_response(NTLMSSP_STATE *ntlmssp_state,
232 ntlmssp_state->stored_response = data_blob_talloc(ntlmssp_state,
239 * Request features for the NTLMSSP negotiation
241 * @param ntlmssp_state NTLMSSP state
242 * @param feature_list List of space seperated features requested from NTLMSSP.
244 void ntlmssp_want_feature_list(NTLMSSP_STATE *ntlmssp_state, char *feature_list)
247 * We need to set this to allow a later SetPassword
248 * via the SAMR pipe to succeed. Strange.... We could
249 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
251 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
252 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
254 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
255 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
257 if(in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
258 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
263 * Request a feature for the NTLMSSP negotiation
265 * @param ntlmssp_state NTLMSSP state
266 * @param feature Bit flag specifying the requested feature
268 void ntlmssp_want_feature(NTLMSSP_STATE *ntlmssp_state, uint32 feature)
270 /* As per JRA's comment above */
271 if (feature & NTLMSSP_FEATURE_SESSION_KEY) {
272 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
274 if (feature & NTLMSSP_FEATURE_SIGN) {
275 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
277 if (feature & NTLMSSP_FEATURE_SEAL) {
278 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
283 * Next state function for the NTLMSSP state machine
285 * @param ntlmssp_state NTLMSSP State
286 * @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
287 * @param out The reply, as an allocated DATA_BLOB, caller to free.
288 * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
291 NTSTATUS ntlmssp_update(NTLMSSP_STATE *ntlmssp_state,
292 const DATA_BLOB in, DATA_BLOB *out)
295 uint32 ntlmssp_command;
298 if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
299 /* Called update after negotiations finished. */
300 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
301 return NT_STATUS_INVALID_PARAMETER;
304 *out = data_blob_null;
306 if (!in.length && ntlmssp_state->stored_response.length) {
307 input = ntlmssp_state->stored_response;
309 /* we only want to read the stored response once - overwrite it */
310 ntlmssp_state->stored_response = data_blob_null;
316 switch (ntlmssp_state->role) {
318 ntlmssp_command = NTLMSSP_INITIAL;
321 /* 'datagram' mode - no neg packet */
322 ntlmssp_command = NTLMSSP_NEGOTIATE;
326 if (!msrpc_parse(NULL, &input, "Cd",
329 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
330 dump_data(2, input.data, input.length);
331 return NT_STATUS_INVALID_PARAMETER;
335 if (ntlmssp_command != ntlmssp_state->expected_state) {
336 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
337 return NT_STATUS_INVALID_PARAMETER;
340 for (i=0; ntlmssp_callbacks[i].fn; i++) {
341 if (ntlmssp_callbacks[i].role == ntlmssp_state->role
342 && ntlmssp_callbacks[i].ntlmssp_command == ntlmssp_command) {
343 return ntlmssp_callbacks[i].fn(ntlmssp_state, input, out);
347 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
348 ntlmssp_state->role, ntlmssp_command));
350 return NT_STATUS_INVALID_PARAMETER;
354 * End an NTLMSSP state machine
356 * @param ntlmssp_state NTLMSSP State, free()ed by this function
359 void ntlmssp_end(NTLMSSP_STATE **ntlmssp_state)
361 (*ntlmssp_state)->ref_count--;
363 if ((*ntlmssp_state)->ref_count == 0) {
364 data_blob_free(&(*ntlmssp_state)->chal);
365 data_blob_free(&(*ntlmssp_state)->lm_resp);
366 data_blob_free(&(*ntlmssp_state)->nt_resp);
367 TALLOC_FREE(*ntlmssp_state);
370 *ntlmssp_state = NULL;
375 * Determine correct target name flags for reply, given server role
376 * and negotiated flags
378 * @param ntlmssp_state NTLMSSP State
379 * @param neg_flags The flags from the packet
380 * @param chal_flags The flags to be set in the reply packet
381 * @return The 'target name' string.
384 static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
385 uint32 neg_flags, uint32 *chal_flags)
387 if (neg_flags & NTLMSSP_REQUEST_TARGET) {
388 *chal_flags |= NTLMSSP_CHAL_TARGET_INFO;
389 *chal_flags |= NTLMSSP_REQUEST_TARGET;
390 if (ntlmssp_state->server_role == ROLE_STANDALONE) {
391 *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
392 return ntlmssp_state->get_global_myname();
394 *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
395 return ntlmssp_state->get_domain();
402 static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
403 uint32 neg_flags, bool allow_lm) {
404 if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
405 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
406 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_OEM;
407 ntlmssp_state->unicode = True;
409 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_UNICODE;
410 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
411 ntlmssp_state->unicode = False;
414 if ((neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) && allow_lm) {
415 /* other end forcing us to use LM */
416 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
417 ntlmssp_state->use_ntlmv2 = False;
419 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
422 if (!(neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)) {
423 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
426 if (!(neg_flags & NTLMSSP_NEGOTIATE_NTLM2)) {
427 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
430 if (!(neg_flags & NTLMSSP_NEGOTIATE_128)) {
431 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_128;
434 if (!(neg_flags & NTLMSSP_NEGOTIATE_56)) {
435 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_56;
438 if (!(neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
439 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_KEY_EXCH;
442 if (!(neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
443 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SIGN;
446 if (!(neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
447 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SEAL;
450 /* Woop Woop - unknown flag for Windows compatibility...
451 What does this really do ? JRA. */
452 if (!(neg_flags & NTLMSSP_NEGOTIATE_VERSION)) {
453 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_VERSION;
456 if ((neg_flags & NTLMSSP_REQUEST_TARGET)) {
457 ntlmssp_state->neg_flags |= NTLMSSP_REQUEST_TARGET;
462 Weaken NTLMSSP keys to cope with down-level clients and servers.
464 We probably should have some parameters to control this, but as
465 it only occours for LM_KEY connections, and this is controlled
466 by the client lanman auth/lanman auth parameters, it isn't too bad.
469 DATA_BLOB ntlmssp_weaken_keys(NTLMSSP_STATE *ntlmssp_state, TALLOC_CTX *mem_ctx)
471 DATA_BLOB weakened_key = data_blob_talloc(mem_ctx,
472 ntlmssp_state->session_key.data,
473 ntlmssp_state->session_key.length);
475 /* Nothing to weaken. We certainly don't want to 'extend' the length... */
476 if (weakened_key.length < 16) {
477 /* perhaps there was no key? */
481 /* Key weakening not performed on the master key for NTLM2
482 and does not occour for NTLM1. Therefore we only need
483 to do this for the LM_KEY.
486 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
487 /* LM key doesn't support 128 bit crypto, so this is
488 * the best we can do. If you negotiate 128 bit, but
489 * not 56, you end up with 40 bit... */
490 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
491 weakened_key.data[7] = 0xa0;
492 } else { /* forty bits */
493 weakened_key.data[5] = 0xe5;
494 weakened_key.data[6] = 0x38;
495 weakened_key.data[7] = 0xb0;
497 weakened_key.length = 8;
503 * Next state function for the Negotiate packet
505 * @param ntlmssp_state NTLMSSP State
506 * @param request The request, as a DATA_BLOB
507 * @param request The reply, as an allocated DATA_BLOB, caller to free.
508 * @return Errors or MORE_PROCESSING_REQUIRED if a reply is sent.
511 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
512 const DATA_BLOB request, DATA_BLOB *reply)
514 DATA_BLOB struct_blob;
516 char *dnsdomname = NULL;
517 uint32 neg_flags = 0;
518 uint32 ntlmssp_command, chal_flags;
520 const char *target_name;
522 /* parse the NTLMSSP packet */
524 file_save("ntlmssp_negotiate.dat", request.data, request.length);
527 if (request.length) {
528 if ((request.length < 16) || !msrpc_parse(NULL, &request, "Cdd",
532 DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
533 (unsigned int)request.length));
534 dump_data(2, request.data, request.length);
535 return NT_STATUS_INVALID_PARAMETER;
537 debug_ntlmssp_flags(neg_flags);
540 ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, lp_lanman_auth());
542 /* Ask our caller what challenge they would like in the packet */
543 ntlmssp_state->get_challenge(ntlmssp_state, cryptkey);
545 /* Check if we may set the challenge */
546 if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
547 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
550 /* The flags we send back are not just the negotiated flags,
551 * they are also 'what is in this packet'. Therfore, we
552 * operate on 'chal_flags' from here on
555 chal_flags = ntlmssp_state->neg_flags;
557 /* get the right name to fill in as 'target' */
558 target_name = ntlmssp_target_name(ntlmssp_state,
559 neg_flags, &chal_flags);
560 if (target_name == NULL)
561 return NT_STATUS_INVALID_PARAMETER;
563 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
564 ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state,
567 /* This should be a 'netbios domain -> DNS domain' mapping */
568 dnsdomname = get_mydnsdomname(ntlmssp_state);
570 dnsdomname = talloc_strdup(ntlmssp_state, "");
573 return NT_STATUS_NO_MEMORY;
575 strlower_m(dnsdomname);
577 dnsname = get_mydnsfullname();
582 /* This creates the 'blob' of names that appears at the end of the packet */
583 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO)
585 msrpc_gen(NULL, &struct_blob, "aaaaa",
586 NTLMSSP_NAME_TYPE_DOMAIN, target_name,
587 NTLMSSP_NAME_TYPE_SERVER, ntlmssp_state->get_global_myname(),
588 NTLMSSP_NAME_TYPE_DOMAIN_DNS, dnsdomname,
589 NTLMSSP_NAME_TYPE_SERVER_DNS, dnsname,
592 struct_blob = data_blob_null;
596 /* Marshel the packet in the right format, be it unicode or ASCII */
597 const char *gen_string;
598 if (ntlmssp_state->unicode) {
599 gen_string = "CdUdbddB";
601 gen_string = "CdAdbddB";
604 msrpc_gen(NULL, reply, gen_string,
611 struct_blob.data, struct_blob.length);
614 data_blob_free(&struct_blob);
616 ntlmssp_state->expected_state = NTLMSSP_AUTH;
618 return NT_STATUS_MORE_PROCESSING_REQUIRED;
622 * Next state function for the Authenticate packet
624 * @param ntlmssp_state NTLMSSP State
625 * @param request The request, as a DATA_BLOB
626 * @param request The reply, as an allocated DATA_BLOB, caller to free.
627 * @return Errors or NT_STATUS_OK.
630 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
631 const DATA_BLOB request, DATA_BLOB *reply)
633 DATA_BLOB encrypted_session_key = data_blob_null;
634 DATA_BLOB user_session_key = data_blob_null;
635 DATA_BLOB lm_session_key = data_blob_null;
636 DATA_BLOB session_key = data_blob_null;
637 uint32 ntlmssp_command, auth_flags;
638 NTSTATUS nt_status = NT_STATUS_OK;
641 bool doing_ntlm2 = False;
643 uchar session_nonce[16];
644 uchar session_nonce_hash[16];
646 const char *parse_string;
648 /* parse the NTLMSSP packet */
649 *reply = data_blob_null;
652 file_save("ntlmssp_auth.dat", request.data, request.length);
655 if (ntlmssp_state->unicode) {
656 parse_string = "CdBBUUUBd";
658 parse_string = "CdBBAAABd";
661 data_blob_free(&ntlmssp_state->lm_resp);
662 data_blob_free(&ntlmssp_state->nt_resp);
664 ntlmssp_state->user = NULL;
665 ntlmssp_state->domain = NULL;
666 ntlmssp_state->workstation = NULL;
668 /* now the NTLMSSP encoded auth hashes */
669 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
672 &ntlmssp_state->lm_resp,
673 &ntlmssp_state->nt_resp,
674 &ntlmssp_state->domain,
675 &ntlmssp_state->user,
676 &ntlmssp_state->workstation,
677 &encrypted_session_key,
681 /* Try again with a shorter string (Win9X truncates this packet) */
682 if (ntlmssp_state->unicode) {
683 parse_string = "CdBBUUU";
685 parse_string = "CdBBAAA";
688 /* now the NTLMSSP encoded auth hashes */
689 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
692 &ntlmssp_state->lm_resp,
693 &ntlmssp_state->nt_resp,
694 &ntlmssp_state->domain,
695 &ntlmssp_state->user,
696 &ntlmssp_state->workstation)) {
697 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
698 dump_data(2, request.data, request.length);
700 return NT_STATUS_INVALID_PARAMETER;
705 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, lp_lanman_auth());
707 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
708 ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->workstation, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
711 file_save("nthash1.dat", &ntlmssp_state->nt_resp.data, &ntlmssp_state->nt_resp.length);
712 file_save("lmhash1.dat", &ntlmssp_state->lm_resp.data, &ntlmssp_state->lm_resp.length);
715 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
718 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
720 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
721 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
722 struct MD5Context md5_session_nonce_ctx;
723 SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
727 memcpy(session_nonce, ntlmssp_state->internal_chal.data, 8);
728 memcpy(&session_nonce[8], ntlmssp_state->lm_resp.data, 8);
730 MD5Init(&md5_session_nonce_ctx);
731 MD5Update(&md5_session_nonce_ctx, session_nonce, 16);
732 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
734 ntlmssp_state->chal = data_blob_talloc(
735 ntlmssp_state, session_nonce_hash, 8);
737 /* LM response is no longer useful */
738 data_blob_free(&ntlmssp_state->lm_resp);
740 /* We changed the effective challenge - set it */
741 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->set_challenge(ntlmssp_state, &ntlmssp_state->chal))) {
742 data_blob_free(&encrypted_session_key);
746 /* LM Key is incompatible. */
747 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
752 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
753 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
754 * smb.conf file) and no NTLMv2 response was sent then the password check
755 * will fail here. JRA.
758 /* Finally, actually ask if the password is OK */
760 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state,
761 &user_session_key, &lm_session_key))) {
762 data_blob_free(&encrypted_session_key);
766 dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
767 dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
769 /* Handle the different session key derivation for NTLM2 */
771 if (user_session_key.data && user_session_key.length == 16) {
772 session_key = data_blob_talloc(ntlmssp_state,
774 hmac_md5(user_session_key.data, session_nonce,
775 sizeof(session_nonce), session_key.data);
776 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
777 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
780 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
781 session_key = data_blob_null;
783 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
784 if (lm_session_key.data && lm_session_key.length >= 8) {
785 if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
786 session_key = data_blob_talloc(ntlmssp_state,
788 if (session_key.data == NULL) {
789 return NT_STATUS_NO_MEMORY;
791 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
793 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
797 session_key = data_blob_talloc(
798 ntlmssp_state, NULL, 16);
799 if (session_key.data == NULL) {
800 return NT_STATUS_NO_MEMORY;
802 SMBsesskeygen_lm_sess_key(
803 lm_session_key.data, zeros,
806 dump_data_pw("LM session key:\n", session_key.data,
809 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
810 session_key = data_blob_null;
812 } else if (user_session_key.data) {
813 session_key = user_session_key;
814 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
815 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
816 } else if (lm_session_key.data) {
817 session_key = lm_session_key;
818 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
819 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
821 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
822 session_key = data_blob_null;
825 /* With KEY_EXCH, the client supplies the proposed session key,
826 but encrypts it with the long-term key */
827 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
828 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
829 data_blob_free(&encrypted_session_key);
830 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
831 (unsigned int)encrypted_session_key.length));
832 return NT_STATUS_INVALID_PARAMETER;
833 } else if (!session_key.data || session_key.length != 16) {
834 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
835 (unsigned int)session_key.length));
836 ntlmssp_state->session_key = session_key;
838 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
839 arcfour_crypt_blob(encrypted_session_key.data,
840 encrypted_session_key.length,
842 ntlmssp_state->session_key = data_blob_talloc(
843 ntlmssp_state, encrypted_session_key.data,
844 encrypted_session_key.length);
845 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
846 encrypted_session_key.length);
849 ntlmssp_state->session_key = session_key;
852 if (!NT_STATUS_IS_OK(nt_status)) {
853 ntlmssp_state->session_key = data_blob_null;
854 } else if (ntlmssp_state->session_key.length) {
855 nt_status = ntlmssp_sign_init(ntlmssp_state);
858 data_blob_free(&encrypted_session_key);
860 /* Only one authentication allowed per server state. */
861 ntlmssp_state->expected_state = NTLMSSP_DONE;
867 * Create an NTLMSSP state machine
869 * @param ntlmssp_state NTLMSSP State, allocated by this function
872 NTSTATUS ntlmssp_server_start(NTLMSSP_STATE **ntlmssp_state)
874 *ntlmssp_state = TALLOC_ZERO_P(NULL, NTLMSSP_STATE);
875 if (!*ntlmssp_state) {
876 DEBUG(0,("ntlmssp_server_start: talloc failed!\n"));
877 talloc_destroy(*ntlmssp_state);
878 return NT_STATUS_NO_MEMORY;
881 (*ntlmssp_state)->role = NTLMSSP_SERVER;
883 (*ntlmssp_state)->get_challenge = get_challenge;
884 (*ntlmssp_state)->set_challenge = set_challenge;
885 (*ntlmssp_state)->may_set_challenge = may_set_challenge;
887 (*ntlmssp_state)->get_global_myname = global_myname;
888 (*ntlmssp_state)->get_domain = lp_workgroup;
889 (*ntlmssp_state)->server_role = ROLE_DOMAIN_MEMBER; /* a good default */
891 (*ntlmssp_state)->expected_state = NTLMSSP_NEGOTIATE;
893 (*ntlmssp_state)->ref_count = 1;
895 (*ntlmssp_state)->neg_flags =
896 NTLMSSP_NEGOTIATE_128 |
897 NTLMSSP_NEGOTIATE_56 |
898 NTLMSSP_NEGOTIATE_VERSION |
899 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
900 NTLMSSP_NEGOTIATE_NTLM |
901 NTLMSSP_NEGOTIATE_NTLM2 |
902 NTLMSSP_NEGOTIATE_KEY_EXCH |
903 NTLMSSP_NEGOTIATE_SIGN |
904 NTLMSSP_NEGOTIATE_SEAL;
909 /*********************************************************************
911 *********************************************************************/
914 * Next state function for the Initial packet
916 * @param ntlmssp_state NTLMSSP State
917 * @param request The request, as a DATA_BLOB. reply.data must be NULL
918 * @param request The reply, as an allocated DATA_BLOB, caller to free.
919 * @return Errors or NT_STATUS_OK.
922 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
923 DATA_BLOB reply, DATA_BLOB *next_request)
925 if (ntlmssp_state->unicode) {
926 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
928 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
931 if (ntlmssp_state->use_ntlmv2) {
932 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
935 /* generate the ntlmssp negotiate packet */
936 msrpc_gen(NULL, next_request, "CddAA",
939 ntlmssp_state->neg_flags,
940 ntlmssp_state->get_domain(),
941 ntlmssp_state->get_global_myname());
943 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
945 return NT_STATUS_MORE_PROCESSING_REQUIRED;
949 * Next state function for the Challenge Packet. Generate an auth packet.
951 * @param ntlmssp_state NTLMSSP State
952 * @param request The request, as a DATA_BLOB. reply.data must be NULL
953 * @param request The reply, as an allocated DATA_BLOB, caller to free.
954 * @return Errors or NT_STATUS_OK.
957 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
958 const DATA_BLOB reply, DATA_BLOB *next_request)
960 uint32 chal_flags, ntlmssp_command, unkn1, unkn2;
961 DATA_BLOB server_domain_blob;
962 DATA_BLOB challenge_blob;
963 DATA_BLOB struct_blob = data_blob_null;
965 const char *chal_parse_string;
966 const char *auth_gen_string;
967 DATA_BLOB lm_response = data_blob_null;
968 DATA_BLOB nt_response = data_blob_null;
969 DATA_BLOB session_key = data_blob_null;
970 DATA_BLOB encrypted_session_key = data_blob_null;
971 NTSTATUS nt_status = NT_STATUS_OK;
973 if (!msrpc_parse(NULL, &reply, "CdBd",
978 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
979 dump_data(2, reply.data, reply.length);
981 return NT_STATUS_INVALID_PARAMETER;
984 data_blob_free(&server_domain_blob);
986 DEBUG(3, ("Got challenge flags:\n"));
987 debug_ntlmssp_flags(chal_flags);
989 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
991 if (ntlmssp_state->unicode) {
992 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
993 chal_parse_string = "CdUdbddB";
995 chal_parse_string = "CdUdbdd";
997 auth_gen_string = "CdBBUUUBd";
999 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
1000 chal_parse_string = "CdAdbddB";
1002 chal_parse_string = "CdAdbdd";
1005 auth_gen_string = "CdBBAAABd";
1008 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
1009 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
1011 if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
1019 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
1020 dump_data(2, reply.data, reply.length);
1021 return NT_STATUS_INVALID_PARAMETER;
1024 ntlmssp_state->server_domain = server_domain;
1026 if (challenge_blob.length != 8) {
1027 data_blob_free(&struct_blob);
1028 return NT_STATUS_INVALID_PARAMETER;
1031 if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
1033 /* do nothing - blobs are zero length */
1037 /* session key is all zeros */
1038 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
1040 /* not doing NLTM2 without a password */
1041 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
1042 } else if (ntlmssp_state->use_ntlmv2) {
1043 if (!struct_blob.length) {
1044 /* be lazy, match win2k - we can't do NTLMv2 without it */
1045 DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1046 return NT_STATUS_INVALID_PARAMETER;
1049 /* TODO: if the remote server is standalone, then we should replace 'domain'
1050 with the server name as supplied above */
1052 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
1053 ntlmssp_state->user,
1054 ntlmssp_state->domain,
1055 ntlmssp_state->nt_hash, &challenge_blob,
1057 &lm_response, &nt_response, NULL,
1059 data_blob_free(&challenge_blob);
1060 data_blob_free(&struct_blob);
1061 return NT_STATUS_NO_MEMORY;
1063 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1064 struct MD5Context md5_session_nonce_ctx;
1065 uchar session_nonce[16];
1066 uchar session_nonce_hash[16];
1067 uchar user_session_key[16];
1069 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1070 generate_random_buffer(lm_response.data, 8);
1071 memset(lm_response.data+8, 0, 16);
1073 memcpy(session_nonce, challenge_blob.data, 8);
1074 memcpy(&session_nonce[8], lm_response.data, 8);
1076 MD5Init(&md5_session_nonce_ctx);
1077 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1078 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1079 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1081 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1082 DEBUG(5, ("challenge is: \n"));
1083 dump_data(5, session_nonce_hash, 8);
1085 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1086 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
1090 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1092 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
1093 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1094 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1096 /* lanman auth is insecure, it may be disabled */
1097 if (lp_client_lanman_auth()) {
1098 lm_response = data_blob_talloc(ntlmssp_state,
1100 SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
1104 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1105 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
1108 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1109 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1110 && lp_client_lanman_auth()) {
1111 SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
1113 dump_data_pw("LM session key\n", session_key.data, session_key.length);
1115 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
1116 dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1119 data_blob_free(&struct_blob);
1121 /* Key exchange encryptes a new client-generated session key with
1122 the password-derived key */
1123 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1124 /* Make up a new session key */
1125 uint8 client_session_key[16];
1126 generate_random_buffer(client_session_key, sizeof(client_session_key));
1128 /* Encrypt the new session key with the old one */
1129 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1130 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1131 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
1132 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1134 /* Mark the new session key as the 'real' session key */
1135 data_blob_free(&session_key);
1136 session_key = data_blob_talloc(ntlmssp_state,
1138 sizeof(client_session_key));
1141 /* this generates the actual auth packet */
1142 if (!msrpc_gen(NULL, next_request, auth_gen_string,
1145 lm_response.data, lm_response.length,
1146 nt_response.data, nt_response.length,
1147 ntlmssp_state->domain,
1148 ntlmssp_state->user,
1149 ntlmssp_state->get_global_myname(),
1150 encrypted_session_key.data, encrypted_session_key.length,
1151 ntlmssp_state->neg_flags)) {
1153 return NT_STATUS_NO_MEMORY;
1156 data_blob_free(&encrypted_session_key);
1158 data_blob_free(&ntlmssp_state->chal);
1160 ntlmssp_state->session_key = session_key;
1162 ntlmssp_state->chal = challenge_blob;
1163 ntlmssp_state->lm_resp = lm_response;
1164 ntlmssp_state->nt_resp = nt_response;
1166 ntlmssp_state->expected_state = NTLMSSP_DONE;
1168 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1169 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1175 NTSTATUS ntlmssp_client_start(NTLMSSP_STATE **ntlmssp_state)
1177 *ntlmssp_state = TALLOC_ZERO_P(NULL, NTLMSSP_STATE);
1178 if (!*ntlmssp_state) {
1179 DEBUG(0,("ntlmssp_client_start: talloc failed!\n"));
1180 talloc_destroy(*ntlmssp_state);
1181 return NT_STATUS_NO_MEMORY;
1184 (*ntlmssp_state)->role = NTLMSSP_CLIENT;
1186 (*ntlmssp_state)->get_global_myname = global_myname;
1187 (*ntlmssp_state)->get_domain = lp_workgroup;
1189 (*ntlmssp_state)->unicode = True;
1191 (*ntlmssp_state)->use_ntlmv2 = lp_client_ntlmv2_auth();
1193 (*ntlmssp_state)->expected_state = NTLMSSP_INITIAL;
1195 (*ntlmssp_state)->ref_count = 1;
1197 (*ntlmssp_state)->neg_flags =
1198 NTLMSSP_NEGOTIATE_128 |
1199 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
1200 NTLMSSP_NEGOTIATE_NTLM |
1201 NTLMSSP_NEGOTIATE_NTLM2 |
1202 NTLMSSP_NEGOTIATE_KEY_EXCH |
1203 NTLMSSP_REQUEST_TARGET;
1205 return NT_STATUS_OK;