2 Unix SMB/Netbios implementation.
4 handle NLTMSSP, server side
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett 2001-2003
8 Copyright (C) Andrew Bartlett 2005 (Updated from gensec).
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
28 DATA_BLOB reply, DATA_BLOB *next_request);
29 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
30 const DATA_BLOB in, DATA_BLOB *out);
31 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
32 const DATA_BLOB reply, DATA_BLOB *next_request);
33 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
34 const DATA_BLOB request, DATA_BLOB *reply);
37 * Callbacks for NTLMSSP - for both client and server operating modes
41 static const struct ntlmssp_callbacks {
42 enum NTLMSSP_ROLE role;
43 enum NTLM_MESSAGE_TYPE ntlmssp_command;
44 NTSTATUS (*fn)(struct ntlmssp_state *ntlmssp_state,
45 DATA_BLOB in, DATA_BLOB *out);
46 } ntlmssp_callbacks[] = {
47 {NTLMSSP_CLIENT, NTLMSSP_INITIAL, ntlmssp_client_initial},
48 {NTLMSSP_SERVER, NTLMSSP_NEGOTIATE, ntlmssp_server_negotiate},
49 {NTLMSSP_CLIENT, NTLMSSP_CHALLENGE, ntlmssp_client_challenge},
50 {NTLMSSP_SERVER, NTLMSSP_AUTH, ntlmssp_server_auth},
51 {NTLMSSP_CLIENT, NTLMSSP_UNKNOWN, NULL},
52 {NTLMSSP_SERVER, NTLMSSP_UNKNOWN, NULL}
57 * Print out the NTLMSSP flags for debugging
58 * @param neg_flags The flags from the packet
61 void debug_ntlmssp_flags(uint32 neg_flags)
63 DEBUG(3,("Got NTLMSSP neg_flags=0x%08x\n", neg_flags));
65 if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE)
66 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_UNICODE\n"));
67 if (neg_flags & NTLMSSP_NEGOTIATE_OEM)
68 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM\n"));
69 if (neg_flags & NTLMSSP_REQUEST_TARGET)
70 DEBUGADD(4, (" NTLMSSP_REQUEST_TARGET\n"));
71 if (neg_flags & NTLMSSP_NEGOTIATE_SIGN)
72 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SIGN\n"));
73 if (neg_flags & NTLMSSP_NEGOTIATE_SEAL)
74 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SEAL\n"));
75 if (neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
76 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_LM_KEY\n"));
77 if (neg_flags & NTLMSSP_NEGOTIATE_NETWARE)
78 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NETWARE\n"));
79 if (neg_flags & NTLMSSP_NEGOTIATE_NTLM)
80 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM\n"));
81 if (neg_flags & NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED)
82 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED\n"));
83 if (neg_flags & NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED)
84 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED\n"));
85 if (neg_flags & NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL)
86 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL\n"));
87 if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)
88 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_ALWAYS_SIGN\n"));
89 if (neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
90 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM2\n"));
91 if (neg_flags & NTLMSSP_CHAL_TARGET_INFO)
92 DEBUGADD(4, (" NTLMSSP_CHAL_TARGET_INFO\n"));
93 if (neg_flags & NTLMSSP_NEGOTIATE_128)
94 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_128\n"));
95 if (neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)
96 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_KEY_EXCH\n"));
100 * Default challenge generation code.
104 static const uint8 *get_challenge(const struct ntlmssp_state *ntlmssp_state)
106 static uchar chal[8];
107 generate_random_buffer(chal, sizeof(chal));
113 * Default 'we can set the challenge to anything we like' implementation
117 static BOOL may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
123 * Default 'we can set the challenge to anything we like' implementation
125 * Does not actually do anything, as the value is always in the structure anyway.
129 static NTSTATUS set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
131 SMB_ASSERT(challenge->length == 8);
136 * Set a username on an NTLMSSP context - ensures it is talloc()ed
140 NTSTATUS ntlmssp_set_username(NTLMSSP_STATE *ntlmssp_state, const char *user)
142 ntlmssp_state->user = talloc_strdup(ntlmssp_state->mem_ctx, user ? user : "" );
143 if (!ntlmssp_state->user) {
144 return NT_STATUS_NO_MEMORY;
150 * Set a password on an NTLMSSP context - ensures it is talloc()ed
153 NTSTATUS ntlmssp_set_password(NTLMSSP_STATE *ntlmssp_state, const char *password)
156 ntlmssp_state->password = NULL;
158 ntlmssp_state->password = talloc_strdup(ntlmssp_state->mem_ctx, password);
159 if (!ntlmssp_state->password) {
160 return NT_STATUS_NO_MEMORY;
167 * Set a domain on an NTLMSSP context - ensures it is talloc()ed
170 NTSTATUS ntlmssp_set_domain(NTLMSSP_STATE *ntlmssp_state, const char *domain)
172 ntlmssp_state->domain = talloc_strdup(ntlmssp_state->mem_ctx, domain ? domain : "" );
173 if (!ntlmssp_state->domain) {
174 return NT_STATUS_NO_MEMORY;
180 * Set a workstation on an NTLMSSP context - ensures it is talloc()ed
183 NTSTATUS ntlmssp_set_workstation(NTLMSSP_STATE *ntlmssp_state, const char *workstation)
185 ntlmssp_state->workstation = talloc_strdup(ntlmssp_state->mem_ctx, workstation);
186 if (!ntlmssp_state->workstation) {
187 return NT_STATUS_NO_MEMORY;
193 * Store a DATA_BLOB containing an NTLMSSP response, for use later.
194 * This copies the data blob
197 NTSTATUS ntlmssp_store_response(NTLMSSP_STATE *ntlmssp_state,
200 ntlmssp_state->stored_response = data_blob_talloc(ntlmssp_state->mem_ctx,
201 response.data, response.length);
206 * Next state function for the NTLMSSP state machine
208 * @param ntlmssp_state NTLMSSP State
209 * @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
210 * @param out The reply, as an allocated DATA_BLOB, caller to free.
211 * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
214 NTSTATUS ntlmssp_update(NTLMSSP_STATE *ntlmssp_state,
215 const DATA_BLOB in, DATA_BLOB *out)
218 uint32 ntlmssp_command;
221 if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
222 /* Called update after negotiations finished. */
223 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
224 return NT_STATUS_INVALID_PARAMETER;
227 *out = data_blob(NULL, 0);
229 if (!in.length && ntlmssp_state->stored_response.length) {
230 input = ntlmssp_state->stored_response;
232 /* we only want to read the stored response once - overwrite it */
233 ntlmssp_state->stored_response = data_blob(NULL, 0);
239 switch (ntlmssp_state->role) {
241 ntlmssp_command = NTLMSSP_INITIAL;
244 /* 'datagram' mode - no neg packet */
245 ntlmssp_command = NTLMSSP_NEGOTIATE;
249 if (!msrpc_parse(&input, "Cd",
252 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
253 dump_data(2, (const char *)input.data, input.length);
254 return NT_STATUS_INVALID_PARAMETER;
258 if (ntlmssp_command != ntlmssp_state->expected_state) {
259 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
260 return NT_STATUS_INVALID_PARAMETER;
263 for (i=0; ntlmssp_callbacks[i].fn; i++) {
264 if (ntlmssp_callbacks[i].role == ntlmssp_state->role
265 && ntlmssp_callbacks[i].ntlmssp_command == ntlmssp_command) {
266 return ntlmssp_callbacks[i].fn(ntlmssp_state, input, out);
270 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
271 ntlmssp_state->role, ntlmssp_command));
273 return NT_STATUS_INVALID_PARAMETER;
277 * End an NTLMSSP state machine
279 * @param ntlmssp_state NTLMSSP State, free()ed by this function
282 void ntlmssp_end(NTLMSSP_STATE **ntlmssp_state)
284 TALLOC_CTX *mem_ctx = (*ntlmssp_state)->mem_ctx;
286 (*ntlmssp_state)->ref_count--;
288 if ((*ntlmssp_state)->ref_count == 0) {
289 data_blob_free(&(*ntlmssp_state)->chal);
290 data_blob_free(&(*ntlmssp_state)->lm_resp);
291 data_blob_free(&(*ntlmssp_state)->nt_resp);
293 talloc_destroy(mem_ctx);
296 *ntlmssp_state = NULL;
301 * Determine correct target name flags for reply, given server role
302 * and negotiated flags
304 * @param ntlmssp_state NTLMSSP State
305 * @param neg_flags The flags from the packet
306 * @param chal_flags The flags to be set in the reply packet
307 * @return The 'target name' string.
310 static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
311 uint32 neg_flags, uint32 *chal_flags)
313 if (neg_flags & NTLMSSP_REQUEST_TARGET) {
314 *chal_flags |= NTLMSSP_CHAL_TARGET_INFO;
315 *chal_flags |= NTLMSSP_REQUEST_TARGET;
316 if (ntlmssp_state->server_role == ROLE_STANDALONE) {
317 *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
318 return ntlmssp_state->get_global_myname();
320 *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
321 return ntlmssp_state->get_domain();
328 static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
329 uint32 neg_flags, BOOL allow_lm) {
330 if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
331 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
332 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_OEM;
333 ntlmssp_state->unicode = True;
335 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_UNICODE;
336 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
337 ntlmssp_state->unicode = False;
340 if ((neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) && allow_lm) {
341 /* other end forcing us to use LM */
342 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
343 ntlmssp_state->use_ntlmv2 = False;
345 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
348 if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN) {
349 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
352 if (!(neg_flags & NTLMSSP_NEGOTIATE_NTLM2)) {
353 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
356 if (!(neg_flags & NTLMSSP_NEGOTIATE_128)) {
357 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_128;
358 if (neg_flags & NTLMSSP_NEGOTIATE_56) {
359 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
363 if (!(neg_flags & NTLMSSP_NEGOTIATE_56)) {
364 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_56;
367 if (!(neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
368 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_KEY_EXCH;
371 if ((neg_flags & NTLMSSP_REQUEST_TARGET)) {
372 ntlmssp_state->neg_flags |= NTLMSSP_REQUEST_TARGET;
378 Weaken NTLMSSP keys to cope with down-level clients and servers.
380 We probably should have some parameters to control this, but as
381 it only occours for LM_KEY connections, and this is controlled
382 by the client lanman auth/lanman auth parameters, it isn't too bad.
385 void ntlmssp_weaken_keys(NTLMSSP_STATE *ntlmssp_state)
387 /* Nothing to weaken. We certainly don't want to 'extend' the length... */
388 if (ntlmssp_state->session_key.length < 8) {
392 /* Key weakening not performed on the master key for NTLM2
393 and does not occour for NTLM1. Therefore we only need
394 to do this for the LM_KEY.
397 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
398 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_128) {
400 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
401 ntlmssp_state->session_key.data[7] = 0xa0;
402 } else { /* forty bits */
403 ntlmssp_state->session_key.data[5] = 0xe5;
404 ntlmssp_state->session_key.data[6] = 0x38;
405 ntlmssp_state->session_key.data[7] = 0xb0;
407 ntlmssp_state->session_key.length = 8;
412 * Next state function for the Negotiate packet
414 * @param ntlmssp_state NTLMSSP State
415 * @param request The request, as a DATA_BLOB
416 * @param request The reply, as an allocated DATA_BLOB, caller to free.
417 * @return Errors or MORE_PROCESSING_REQUIRED if a reply is sent.
420 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
421 const DATA_BLOB request, DATA_BLOB *reply)
423 DATA_BLOB struct_blob;
424 fstring dnsname, dnsdomname;
425 uint32 neg_flags = 0;
426 uint32 ntlmssp_command, chal_flags;
427 const uint8 *cryptkey;
428 const char *target_name;
430 /* parse the NTLMSSP packet */
432 file_save("ntlmssp_negotiate.dat", request.data, request.length);
435 if (request.length) {
436 if ((request.length < 16) || !msrpc_parse(&request, "Cdd",
440 DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
441 (unsigned int)request.length));
442 dump_data(2, (const char *)request.data, request.length);
443 return NT_STATUS_INVALID_PARAMETER;
445 debug_ntlmssp_flags(neg_flags);
448 ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, lp_lanman_auth());
450 /* Ask our caller what challenge they would like in the packet */
451 cryptkey = ntlmssp_state->get_challenge(ntlmssp_state);
453 /* Check if we may set the challenge */
454 if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
455 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
458 /* The flags we send back are not just the negotiated flags,
459 * they are also 'what is in this packet'. Therfore, we
460 * operate on 'chal_flags' from here on
463 chal_flags = ntlmssp_state->neg_flags;
465 /* get the right name to fill in as 'target' */
466 target_name = ntlmssp_target_name(ntlmssp_state,
467 neg_flags, &chal_flags);
468 if (target_name == NULL)
469 return NT_STATUS_INVALID_PARAMETER;
471 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state->mem_ctx, cryptkey, 8);
472 ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state->mem_ctx, cryptkey, 8);
475 /* This should be a 'netbios domain -> DNS domain' mapping */
476 dnsdomname[0] = '\0';
477 get_mydnsdomname(dnsdomname);
478 strlower_m(dnsdomname);
481 get_mydnsfullname(dnsname);
483 /* This creates the 'blob' of names that appears at the end of the packet */
484 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO)
486 msrpc_gen(&struct_blob, "aaaaa",
487 NTLMSSP_NAME_TYPE_DOMAIN, target_name,
488 NTLMSSP_NAME_TYPE_SERVER, ntlmssp_state->get_global_myname(),
489 NTLMSSP_NAME_TYPE_DOMAIN_DNS, dnsdomname,
490 NTLMSSP_NAME_TYPE_SERVER_DNS, dnsname,
493 struct_blob = data_blob(NULL, 0);
497 /* Marshel the packet in the right format, be it unicode or ASCII */
498 const char *gen_string;
499 if (ntlmssp_state->unicode) {
500 gen_string = "CdUdbddB";
502 gen_string = "CdAdbddB";
505 msrpc_gen(reply, gen_string,
512 struct_blob.data, struct_blob.length);
515 data_blob_free(&struct_blob);
517 ntlmssp_state->expected_state = NTLMSSP_AUTH;
519 return NT_STATUS_MORE_PROCESSING_REQUIRED;
523 * Next state function for the Authenticate packet
525 * @param ntlmssp_state NTLMSSP State
526 * @param request The request, as a DATA_BLOB
527 * @param request The reply, as an allocated DATA_BLOB, caller to free.
528 * @return Errors or NT_STATUS_OK.
531 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
532 const DATA_BLOB request, DATA_BLOB *reply)
534 DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
535 DATA_BLOB user_session_key = data_blob(NULL, 0);
536 DATA_BLOB lm_session_key = data_blob(NULL, 0);
537 DATA_BLOB session_key = data_blob(NULL, 0);
538 uint32 ntlmssp_command, auth_flags;
539 NTSTATUS nt_status = NT_STATUS_OK;
542 BOOL doing_ntlm2 = False;
544 uchar session_nonce[16];
545 uchar session_nonce_hash[16];
547 const char *parse_string;
550 char *workstation = NULL;
552 /* parse the NTLMSSP packet */
553 *reply = data_blob(NULL, 0);
556 file_save("ntlmssp_auth.dat", request.data, request.length);
559 if (ntlmssp_state->unicode) {
560 parse_string = "CdBBUUUBd";
562 parse_string = "CdBBAAABd";
565 data_blob_free(&ntlmssp_state->lm_resp);
566 data_blob_free(&ntlmssp_state->nt_resp);
568 ntlmssp_state->user = NULL;
569 ntlmssp_state->domain = NULL;
570 ntlmssp_state->workstation = NULL;
572 /* now the NTLMSSP encoded auth hashes */
573 if (!msrpc_parse(&request, parse_string,
576 &ntlmssp_state->lm_resp,
577 &ntlmssp_state->nt_resp,
581 &encrypted_session_key,
585 SAFE_FREE(workstation);
586 data_blob_free(&encrypted_session_key);
589 /* Try again with a shorter string (Win9X truncates this packet) */
590 if (ntlmssp_state->unicode) {
591 parse_string = "CdBBUUU";
593 parse_string = "CdBBAAA";
596 /* now the NTLMSSP encoded auth hashes */
597 if (!msrpc_parse(&request, parse_string,
600 &ntlmssp_state->lm_resp,
601 &ntlmssp_state->nt_resp,
605 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
606 dump_data(2, (const char *)request.data, request.length);
609 SAFE_FREE(workstation);
611 return NT_STATUS_INVALID_PARAMETER;
616 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, lp_lanman_auth());
618 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(ntlmssp_state, domain))) {
621 SAFE_FREE(workstation);
622 data_blob_free(&encrypted_session_key);
626 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(ntlmssp_state, user))) {
629 SAFE_FREE(workstation);
630 data_blob_free(&encrypted_session_key);
634 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_workstation(ntlmssp_state, workstation))) {
637 SAFE_FREE(workstation);
638 data_blob_free(&encrypted_session_key);
644 SAFE_FREE(workstation);
646 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
647 ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->workstation, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
650 file_save("nthash1.dat", &ntlmssp_state->nt_resp.data, &ntlmssp_state->nt_resp.length);
651 file_save("lmhash1.dat", &ntlmssp_state->lm_resp.data, &ntlmssp_state->lm_resp.length);
654 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
657 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
659 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
660 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
661 struct MD5Context md5_session_nonce_ctx;
662 SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
666 memcpy(session_nonce, ntlmssp_state->internal_chal.data, 8);
667 memcpy(&session_nonce[8], ntlmssp_state->lm_resp.data, 8);
669 MD5Init(&md5_session_nonce_ctx);
670 MD5Update(&md5_session_nonce_ctx, session_nonce, 16);
671 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
673 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state->mem_ctx, session_nonce_hash, 8);
675 /* LM response is no longer useful */
676 data_blob_free(&ntlmssp_state->lm_resp);
678 /* We changed the effective challenge - set it */
679 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->set_challenge(ntlmssp_state, &ntlmssp_state->chal))) {
680 data_blob_free(&encrypted_session_key);
684 /* LM Key is incompatible. */
685 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
690 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
691 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
692 * smb.conf file) and no NTLMv2 response was sent then the password check
693 * will fail here. JRA.
696 /* Finally, actually ask if the password is OK */
698 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state,
699 &user_session_key, &lm_session_key))) {
700 data_blob_free(&encrypted_session_key);
704 dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
705 dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
707 /* Handle the different session key derivation for NTLM2 */
709 if (user_session_key.data && user_session_key.length == 16) {
710 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
711 hmac_md5(user_session_key.data, session_nonce,
712 sizeof(session_nonce), session_key.data);
713 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
714 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
717 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
718 session_key = data_blob(NULL, 0);
720 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
721 if (lm_session_key.data && lm_session_key.length >= 8) {
722 if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
723 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
724 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
726 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
727 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
729 /* use the key unmodified - it's
730 * probably a NULL key from the guest
732 session_key = lm_session_key;
735 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
736 session_key = data_blob(NULL, 0);
738 } else if (user_session_key.data) {
739 session_key = user_session_key;
740 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
741 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
742 } else if (lm_session_key.data) {
743 session_key = lm_session_key;
744 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
745 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
747 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
748 session_key = data_blob(NULL, 0);
751 /* With KEY_EXCH, the client supplies the proposed session key,
752 but encrypts it with the long-term key */
753 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
754 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
755 data_blob_free(&encrypted_session_key);
756 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
757 (unsigned int)encrypted_session_key.length));
758 return NT_STATUS_INVALID_PARAMETER;
759 } else if (!session_key.data || session_key.length != 16) {
760 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
761 (unsigned int)session_key.length));
762 ntlmssp_state->session_key = session_key;
764 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
765 SamOEMhash(encrypted_session_key.data,
767 encrypted_session_key.length);
768 ntlmssp_state->session_key = data_blob_talloc(ntlmssp_state->mem_ctx,
769 encrypted_session_key.data,
770 encrypted_session_key.length);
771 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
772 encrypted_session_key.length);
775 ntlmssp_state->session_key = session_key;
778 /* The client might need us to use a partial-strength session key */
779 ntlmssp_weaken_keys(ntlmssp_state);
781 if (!NT_STATUS_IS_OK(nt_status)) {
782 ntlmssp_state->session_key = data_blob(NULL, 0);
783 } else if (ntlmssp_state->session_key.length) {
784 nt_status = ntlmssp_sign_init(ntlmssp_state);
787 data_blob_free(&encrypted_session_key);
789 /* Only one authentication allowed per server state. */
790 ntlmssp_state->expected_state = NTLMSSP_DONE;
796 * Create an NTLMSSP state machine
798 * @param ntlmssp_state NTLMSSP State, allocated by this function
801 NTSTATUS ntlmssp_server_start(NTLMSSP_STATE **ntlmssp_state)
805 mem_ctx = talloc_init("NTLMSSP context");
807 *ntlmssp_state = TALLOC_ZERO_P(mem_ctx, NTLMSSP_STATE);
808 if (!*ntlmssp_state) {
809 DEBUG(0,("ntlmssp_server_start: talloc failed!\n"));
810 talloc_destroy(mem_ctx);
811 return NT_STATUS_NO_MEMORY;
814 (*ntlmssp_state)->role = NTLMSSP_SERVER;
816 (*ntlmssp_state)->mem_ctx = mem_ctx;
817 (*ntlmssp_state)->get_challenge = get_challenge;
818 (*ntlmssp_state)->set_challenge = set_challenge;
819 (*ntlmssp_state)->may_set_challenge = may_set_challenge;
821 (*ntlmssp_state)->get_global_myname = global_myname;
822 (*ntlmssp_state)->get_domain = lp_workgroup;
823 (*ntlmssp_state)->server_role = ROLE_DOMAIN_MEMBER; /* a good default */
825 (*ntlmssp_state)->expected_state = NTLMSSP_NEGOTIATE;
827 (*ntlmssp_state)->ref_count = 1;
829 (*ntlmssp_state)->neg_flags =
830 NTLMSSP_NEGOTIATE_128 |
831 NTLMSSP_NEGOTIATE_NTLM |
832 NTLMSSP_NEGOTIATE_NTLM2 |
833 NTLMSSP_NEGOTIATE_KEY_EXCH |
834 NTLMSSP_NEGOTIATE_SIGN |
835 NTLMSSP_NEGOTIATE_SEAL;
840 /*********************************************************************
842 *********************************************************************/
845 * Next state function for the Initial packet
847 * @param ntlmssp_state NTLMSSP State
848 * @param request The request, as a DATA_BLOB. reply.data must be NULL
849 * @param request The reply, as an allocated DATA_BLOB, caller to free.
850 * @return Errors or NT_STATUS_OK.
853 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
854 DATA_BLOB reply, DATA_BLOB *next_request)
856 if (ntlmssp_state->unicode) {
857 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
859 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
862 if (ntlmssp_state->use_ntlmv2) {
863 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
866 /* generate the ntlmssp negotiate packet */
867 msrpc_gen(next_request, "CddAA",
870 ntlmssp_state->neg_flags,
871 ntlmssp_state->get_domain(),
872 ntlmssp_state->get_global_myname());
874 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
876 return NT_STATUS_MORE_PROCESSING_REQUIRED;
880 * Next state function for the Challenge Packet. Generate an auth packet.
882 * @param ntlmssp_state NTLMSSP State
883 * @param request The request, as a DATA_BLOB. reply.data must be NULL
884 * @param request The reply, as an allocated DATA_BLOB, caller to free.
885 * @return Errors or NT_STATUS_OK.
888 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
889 const DATA_BLOB reply, DATA_BLOB *next_request)
891 uint32 chal_flags, ntlmssp_command, unkn1, unkn2;
892 DATA_BLOB server_domain_blob;
893 DATA_BLOB challenge_blob;
894 DATA_BLOB struct_blob = data_blob(NULL, 0);
896 const char *chal_parse_string;
897 const char *auth_gen_string;
898 DATA_BLOB lm_response = data_blob(NULL, 0);
899 DATA_BLOB nt_response = data_blob(NULL, 0);
900 DATA_BLOB session_key = data_blob(NULL, 0);
901 DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
902 NTSTATUS nt_status = NT_STATUS_OK;
904 if (!msrpc_parse(&reply, "CdBd",
909 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
910 dump_data(2, (const char *)reply.data, reply.length);
912 return NT_STATUS_INVALID_PARAMETER;
915 data_blob_free(&server_domain_blob);
917 DEBUG(3, ("Got challenge flags:\n"));
918 debug_ntlmssp_flags(chal_flags);
920 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
922 if (ntlmssp_state->unicode) {
923 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
924 chal_parse_string = "CdUdbddB";
926 chal_parse_string = "CdUdbdd";
928 auth_gen_string = "CdBBUUUBd";
930 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
931 chal_parse_string = "CdAdbddB";
933 chal_parse_string = "CdAdbdd";
936 auth_gen_string = "CdBBAAABd";
939 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
940 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
942 if (!msrpc_parse(&reply, chal_parse_string,
950 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
951 dump_data(2, (const char *)reply.data, reply.length);
952 return NT_STATUS_INVALID_PARAMETER;
955 ntlmssp_state->server_domain = talloc_strdup(ntlmssp_state->mem_ctx,
958 SAFE_FREE(server_domain);
959 if (challenge_blob.length != 8) {
960 data_blob_free(&struct_blob);
961 return NT_STATUS_INVALID_PARAMETER;
964 if (!ntlmssp_state->password) {
965 static const uchar zeros[16];
966 /* do nothing - blobs are zero length */
968 /* session key is all zeros */
969 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, zeros, 16);
971 /* not doing NLTM2 without a password */
972 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
973 } else if (ntlmssp_state->use_ntlmv2) {
975 if (!struct_blob.length) {
976 /* be lazy, match win2k - we can't do NTLMv2 without it */
977 DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
978 return NT_STATUS_INVALID_PARAMETER;
981 /* TODO: if the remote server is standalone, then we should replace 'domain'
982 with the server name as supplied above */
984 if (!SMBNTLMv2encrypt(ntlmssp_state->user,
985 ntlmssp_state->domain,
986 ntlmssp_state->password, &challenge_blob,
988 &lm_response, &nt_response, &session_key)) {
989 data_blob_free(&challenge_blob);
990 data_blob_free(&struct_blob);
991 return NT_STATUS_NO_MEMORY;
993 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
994 struct MD5Context md5_session_nonce_ctx;
996 uchar session_nonce[16];
997 uchar session_nonce_hash[16];
998 uchar user_session_key[16];
999 E_md4hash(ntlmssp_state->password, nt_hash);
1001 lm_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1002 generate_random_buffer(lm_response.data, 8);
1003 memset(lm_response.data+8, 0, 16);
1005 memcpy(session_nonce, challenge_blob.data, 8);
1006 memcpy(&session_nonce[8], lm_response.data, 8);
1008 MD5Init(&md5_session_nonce_ctx);
1009 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1010 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1011 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1013 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1014 DEBUG(5, ("challenge is: \n"));
1015 dump_data(5, (const char *)session_nonce_hash, 8);
1017 nt_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1018 SMBNTencrypt(ntlmssp_state->password,
1022 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
1024 SMBsesskeygen_ntv1(nt_hash, NULL, user_session_key);
1025 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1026 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1030 E_deshash(ntlmssp_state->password, lm_hash);
1031 E_md4hash(ntlmssp_state->password, nt_hash);
1033 /* lanman auth is insecure, it may be disabled */
1034 if (lp_client_lanman_auth()) {
1035 lm_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1036 SMBencrypt(ntlmssp_state->password,challenge_blob.data,
1040 nt_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1041 SMBNTencrypt(ntlmssp_state->password,challenge_blob.data,
1044 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
1045 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1046 && lp_client_lanman_auth()) {
1047 SMBsesskeygen_lm_sess_key(lm_hash, lm_response.data,
1049 dump_data_pw("LM session key\n", session_key.data, session_key.length);
1051 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1052 dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1055 data_blob_free(&struct_blob);
1057 /* Key exchange encryptes a new client-generated session key with
1058 the password-derived key */
1059 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1060 /* Make up a new session key */
1061 uint8 client_session_key[16];
1062 generate_random_buffer(client_session_key, sizeof(client_session_key));
1064 /* Encrypt the new session key with the old one */
1065 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1066 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1067 SamOEMhash(encrypted_session_key.data, session_key.data, encrypted_session_key.length);
1068 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1070 /* Mark the new session key as the 'real' session key */
1071 data_blob_free(&session_key);
1072 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, client_session_key, sizeof(client_session_key));
1075 /* this generates the actual auth packet */
1076 if (!msrpc_gen(next_request, auth_gen_string,
1079 lm_response.data, lm_response.length,
1080 nt_response.data, nt_response.length,
1081 ntlmssp_state->domain,
1082 ntlmssp_state->user,
1083 ntlmssp_state->get_global_myname(),
1084 encrypted_session_key.data, encrypted_session_key.length,
1085 ntlmssp_state->neg_flags)) {
1087 return NT_STATUS_NO_MEMORY;
1090 data_blob_free(&encrypted_session_key);
1092 data_blob_free(&ntlmssp_state->chal);
1094 ntlmssp_state->session_key = session_key;
1096 /* The client might be using 56 or 40 bit weakened keys */
1097 ntlmssp_weaken_keys(ntlmssp_state);
1099 ntlmssp_state->chal = challenge_blob;
1100 ntlmssp_state->lm_resp = lm_response;
1101 ntlmssp_state->nt_resp = nt_response;
1103 ntlmssp_state->expected_state = NTLMSSP_DONE;
1105 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1106 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1112 NTSTATUS ntlmssp_client_start(NTLMSSP_STATE **ntlmssp_state)
1114 TALLOC_CTX *mem_ctx;
1116 mem_ctx = talloc_init("NTLMSSP Client context");
1118 *ntlmssp_state = TALLOC_ZERO_P(mem_ctx, NTLMSSP_STATE);
1119 if (!*ntlmssp_state) {
1120 DEBUG(0,("ntlmssp_client_start: talloc failed!\n"));
1121 talloc_destroy(mem_ctx);
1122 return NT_STATUS_NO_MEMORY;
1125 (*ntlmssp_state)->role = NTLMSSP_CLIENT;
1127 (*ntlmssp_state)->mem_ctx = mem_ctx;
1129 (*ntlmssp_state)->get_global_myname = global_myname;
1130 (*ntlmssp_state)->get_domain = lp_workgroup;
1132 (*ntlmssp_state)->unicode = True;
1134 (*ntlmssp_state)->use_ntlmv2 = lp_client_ntlmv2_auth();
1136 (*ntlmssp_state)->expected_state = NTLMSSP_INITIAL;
1138 (*ntlmssp_state)->ref_count = 1;
1140 (*ntlmssp_state)->neg_flags =
1141 NTLMSSP_NEGOTIATE_128 |
1142 NTLMSSP_NEGOTIATE_NTLM |
1143 NTLMSSP_NEGOTIATE_NTLM2 |
1144 NTLMSSP_NEGOTIATE_KEY_EXCH |
1146 * We need to set this to allow a later SetPassword
1147 * via the SAMR pipe to succeed. Strange.... We could
1148 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
1150 NTLMSSP_NEGOTIATE_SIGN |
1151 NTLMSSP_REQUEST_TARGET;
1153 return NT_STATUS_OK;