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/ntlmssp.h"
26 #include "../libcli/auth/ntlmssp_private.h"
27 #include "../libcli/auth/libcli_auth.h"
28 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
29 #include "../libcli/auth/ntlmssp_ndr.h"
30 #include "../lib/crypto/md5.h"
31 #include "../lib/crypto/arcfour.h"
32 #include "../lib/crypto/hmacmd5.h"
34 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
35 DATA_BLOB reply, DATA_BLOB *next_request);
36 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
37 const DATA_BLOB in, DATA_BLOB *out);
38 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
39 const DATA_BLOB reply, DATA_BLOB *next_request);
40 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
41 const DATA_BLOB request, DATA_BLOB *reply);
44 * Callbacks for NTLMSSP - for both client and server operating modes
48 static const struct ntlmssp_callbacks {
49 enum ntlmssp_role role;
50 enum ntlmssp_message_type ntlmssp_command;
51 NTSTATUS (*fn)(struct ntlmssp_state *ntlmssp_state,
52 DATA_BLOB in, DATA_BLOB *out);
53 } ntlmssp_callbacks[] = {
54 {NTLMSSP_CLIENT, NTLMSSP_INITIAL, ntlmssp_client_initial},
55 {NTLMSSP_SERVER, NTLMSSP_NEGOTIATE, ntlmssp_server_negotiate},
56 {NTLMSSP_CLIENT, NTLMSSP_CHALLENGE, ntlmssp_client_challenge},
57 {NTLMSSP_SERVER, NTLMSSP_AUTH, ntlmssp_server_auth},
58 {NTLMSSP_CLIENT, NTLMSSP_UNKNOWN, NULL},
59 {NTLMSSP_SERVER, NTLMSSP_UNKNOWN, NULL}
64 * Default challenge generation code.
68 static NTSTATUS get_challenge(const struct ntlmssp_state *ntlmssp_state,
71 generate_random_buffer(chal, 8);
76 * Default 'we can set the challenge to anything we like' implementation
80 static bool may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
86 * Default 'we can set the challenge to anything we like' implementation
88 * Does not actually do anything, as the value is always in the structure anyway.
92 static NTSTATUS set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
94 SMB_ASSERT(challenge->length == 8);
99 * Set a username on an NTLMSSP context - ensures it is talloc()ed
103 NTSTATUS ntlmssp_set_username(struct ntlmssp_state *ntlmssp_state, const char *user)
105 ntlmssp_state->user = talloc_strdup(ntlmssp_state, user ? user : "" );
106 if (!ntlmssp_state->user) {
107 return NT_STATUS_NO_MEMORY;
113 * Store NT and LM hashes on an NTLMSSP context - ensures they are talloc()ed
116 NTSTATUS ntlmssp_set_hashes(struct ntlmssp_state *ntlmssp_state,
117 const uint8_t lm_hash[16],
118 const uint8_t nt_hash[16])
120 ntlmssp_state->lm_hash = (uint8_t *)
121 TALLOC_MEMDUP(ntlmssp_state, lm_hash, 16);
122 ntlmssp_state->nt_hash = (uint8_t *)
123 TALLOC_MEMDUP(ntlmssp_state, nt_hash, 16);
124 if (!ntlmssp_state->lm_hash || !ntlmssp_state->nt_hash) {
125 TALLOC_FREE(ntlmssp_state->lm_hash);
126 TALLOC_FREE(ntlmssp_state->nt_hash);
127 return NT_STATUS_NO_MEMORY;
133 * Converts a password to the hashes on an NTLMSSP context.
136 NTSTATUS ntlmssp_set_password(struct ntlmssp_state *ntlmssp_state, const char *password)
139 ntlmssp_state->lm_hash = NULL;
140 ntlmssp_state->nt_hash = NULL;
145 E_deshash(password, lm_hash);
146 E_md4hash(password, nt_hash);
147 return ntlmssp_set_hashes(ntlmssp_state, lm_hash, nt_hash);
153 * Set a domain on an NTLMSSP context - ensures it is talloc()ed
156 NTSTATUS ntlmssp_set_domain(struct ntlmssp_state *ntlmssp_state, const char *domain)
158 ntlmssp_state->domain = talloc_strdup(ntlmssp_state,
159 domain ? domain : "" );
160 if (!ntlmssp_state->domain) {
161 return NT_STATUS_NO_MEMORY;
167 * Request features for the NTLMSSP negotiation
169 * @param ntlmssp_state NTLMSSP state
170 * @param feature_list List of space seperated features requested from NTLMSSP.
172 void ntlmssp_want_feature_list(struct ntlmssp_state *ntlmssp_state, char *feature_list)
175 * We need to set this to allow a later SetPassword
176 * via the SAMR pipe to succeed. Strange.... We could
177 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
179 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
180 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
182 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
183 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
185 if(in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
186 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
188 if (in_list("NTLMSSP_FEATURE_CCACHE", feature_list, true)) {
189 ntlmssp_state->use_ccache = true;
194 * Request a feature for the NTLMSSP negotiation
196 * @param ntlmssp_state NTLMSSP state
197 * @param feature Bit flag specifying the requested feature
199 void ntlmssp_want_feature(struct ntlmssp_state *ntlmssp_state, uint32_t feature)
201 /* As per JRA's comment above */
202 if (feature & NTLMSSP_FEATURE_SESSION_KEY) {
203 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
205 if (feature & NTLMSSP_FEATURE_SIGN) {
206 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
208 if (feature & NTLMSSP_FEATURE_SEAL) {
209 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
211 if (feature & NTLMSSP_FEATURE_CCACHE) {
212 ntlmssp_state->use_ccache = true;
217 * Next state function for the NTLMSSP state machine
219 * @param ntlmssp_state NTLMSSP State
220 * @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
221 * @param out The reply, as an allocated DATA_BLOB, caller to free.
222 * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
225 NTSTATUS ntlmssp_update(struct ntlmssp_state *ntlmssp_state,
226 const DATA_BLOB input, DATA_BLOB *out)
228 uint32_t ntlmssp_command;
231 if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
232 /* Called update after negotiations finished. */
233 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
234 return NT_STATUS_INVALID_PARAMETER;
237 *out = data_blob_null;
240 switch (ntlmssp_state->role) {
242 ntlmssp_command = NTLMSSP_INITIAL;
245 /* 'datagram' mode - no neg packet */
246 ntlmssp_command = NTLMSSP_NEGOTIATE;
250 if (!msrpc_parse(ntlmssp_state, &input, "Cd",
253 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
254 dump_data(2, input.data, input.length);
255 return NT_STATUS_INVALID_PARAMETER;
259 if (ntlmssp_command != ntlmssp_state->expected_state) {
260 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
261 return NT_STATUS_INVALID_PARAMETER;
264 for (i=0; ntlmssp_callbacks[i].fn; i++) {
265 if (ntlmssp_callbacks[i].role == ntlmssp_state->role
266 && ntlmssp_callbacks[i].ntlmssp_command == ntlmssp_command) {
267 return ntlmssp_callbacks[i].fn(ntlmssp_state, input, out);
271 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
272 ntlmssp_state->role, ntlmssp_command));
274 return NT_STATUS_INVALID_PARAMETER;
278 * Next state function for the Negotiate packet
280 * @param ntlmssp_state NTLMSSP State
281 * @param request The request, as a DATA_BLOB
282 * @param request The reply, as an allocated DATA_BLOB, caller to free.
283 * @return Errors or MORE_PROCESSING_REQUIRED if a reply is sent.
286 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
287 const DATA_BLOB request, DATA_BLOB *reply)
289 DATA_BLOB struct_blob;
290 uint32_t neg_flags = 0;
291 uint32_t ntlmssp_command, chal_flags;
293 const char *target_name;
296 /* parse the NTLMSSP packet */
298 file_save("ntlmssp_negotiate.dat", request.data, request.length);
301 if (request.length) {
302 if ((request.length < 16) || !msrpc_parse(ntlmssp_state, &request, "Cdd",
306 DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
307 (unsigned int)request.length));
308 dump_data(2, request.data, request.length);
309 return NT_STATUS_INVALID_PARAMETER;
311 debug_ntlmssp_flags(neg_flags);
313 if (DEBUGLEVEL >= 10) {
314 struct NEGOTIATE_MESSAGE *negotiate = talloc(
315 talloc_tos(), struct NEGOTIATE_MESSAGE);
316 if (negotiate != NULL) {
317 status = ntlmssp_pull_NEGOTIATE_MESSAGE(
318 &request, negotiate, negotiate);
319 if (NT_STATUS_IS_OK(status)) {
320 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
323 TALLOC_FREE(negotiate);
328 ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, ntlmssp_state->allow_lm_key);
330 /* Ask our caller what challenge they would like in the packet */
331 status = ntlmssp_state->get_challenge(ntlmssp_state, cryptkey);
332 if (!NT_STATUS_IS_OK(status)) {
336 /* Check if we may set the challenge */
337 if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
338 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
341 /* The flags we send back are not just the negotiated flags,
342 * they are also 'what is in this packet'. Therfore, we
343 * operate on 'chal_flags' from here on
346 chal_flags = ntlmssp_state->neg_flags;
348 /* get the right name to fill in as 'target' */
349 target_name = ntlmssp_target_name(ntlmssp_state,
350 neg_flags, &chal_flags);
351 if (target_name == NULL)
352 return NT_STATUS_INVALID_PARAMETER;
354 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
355 ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state,
358 /* This creates the 'blob' of names that appears at the end of the packet */
359 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
361 msrpc_gen(ntlmssp_state, &struct_blob, "aaaaa",
362 MsvAvNbDomainName, target_name,
363 MsvAvNbComputerName, ntlmssp_state->server.netbios_name,
364 MsvAvDnsDomainName, ntlmssp_state->server.dns_domain,
365 MsvAvDnsComputerName, ntlmssp_state->server.dns_name,
368 struct_blob = data_blob_null;
372 /* Marshal the packet in the right format, be it unicode or ASCII */
373 const char *gen_string;
374 DATA_BLOB version_blob = data_blob_null;
376 if (chal_flags & NTLMSSP_NEGOTIATE_VERSION) {
377 enum ndr_err_code err;
380 /* "What Windows returns" as a version number. */
382 vers.ProductMajorVersion = NTLMSSP_WINDOWS_MAJOR_VERSION_6;
383 vers.ProductMinorVersion = NTLMSSP_WINDOWS_MINOR_VERSION_1;
384 vers.ProductBuild = 0;
385 vers.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
387 err = ndr_push_struct_blob(&version_blob,
390 (ndr_push_flags_fn_t)ndr_push_VERSION);
392 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
393 return NT_STATUS_NO_MEMORY;
397 if (ntlmssp_state->unicode) {
398 gen_string = "CdUdbddBb";
400 gen_string = "CdAdbddBb";
403 msrpc_gen(ntlmssp_state, reply, gen_string,
410 struct_blob.data, struct_blob.length,
411 version_blob.data, version_blob.length);
413 data_blob_free(&version_blob);
415 if (DEBUGLEVEL >= 10) {
416 struct CHALLENGE_MESSAGE *challenge = talloc(
417 talloc_tos(), struct CHALLENGE_MESSAGE);
418 if (challenge != NULL) {
419 challenge->NegotiateFlags = chal_flags;
420 status = ntlmssp_pull_CHALLENGE_MESSAGE(
421 reply, challenge, challenge);
422 if (NT_STATUS_IS_OK(status)) {
423 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
426 TALLOC_FREE(challenge);
431 data_blob_free(&struct_blob);
433 ntlmssp_state->expected_state = NTLMSSP_AUTH;
435 return NT_STATUS_MORE_PROCESSING_REQUIRED;
439 * Next state function for the Authenticate packet
441 * @param ntlmssp_state NTLMSSP State
442 * @param request The request, as a DATA_BLOB
443 * @param request The reply, as an allocated DATA_BLOB, caller to free.
444 * @return Errors or NT_STATUS_OK.
447 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
448 const DATA_BLOB request, DATA_BLOB *reply)
450 DATA_BLOB encrypted_session_key = data_blob_null;
451 DATA_BLOB user_session_key = data_blob_null;
452 DATA_BLOB lm_session_key = data_blob_null;
453 DATA_BLOB session_key = data_blob_null;
454 uint32_t ntlmssp_command, auth_flags;
455 NTSTATUS nt_status = NT_STATUS_OK;
458 bool doing_ntlm2 = False;
460 uint8_t session_nonce[16];
461 uint8_t session_nonce_hash[16];
463 const char *parse_string;
465 /* parse the NTLMSSP packet */
466 *reply = data_blob_null;
469 file_save("ntlmssp_auth.dat", request.data, request.length);
472 if (ntlmssp_state->unicode) {
473 parse_string = "CdBBUUUBd";
475 parse_string = "CdBBAAABd";
478 data_blob_free(&ntlmssp_state->lm_resp);
479 data_blob_free(&ntlmssp_state->nt_resp);
481 ntlmssp_state->user = NULL;
482 ntlmssp_state->domain = NULL;
484 /* now the NTLMSSP encoded auth hashes */
485 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
488 &ntlmssp_state->lm_resp,
489 &ntlmssp_state->nt_resp,
490 &ntlmssp_state->domain,
491 &ntlmssp_state->user,
492 &ntlmssp_state->client.netbios_name,
493 &encrypted_session_key,
497 /* Try again with a shorter string (Win9X truncates this packet) */
498 if (ntlmssp_state->unicode) {
499 parse_string = "CdBBUUU";
501 parse_string = "CdBBAAA";
504 /* now the NTLMSSP encoded auth hashes */
505 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
508 &ntlmssp_state->lm_resp,
509 &ntlmssp_state->nt_resp,
510 &ntlmssp_state->domain,
511 &ntlmssp_state->user,
512 &ntlmssp_state->client.netbios_name)) {
513 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
514 dump_data(2, request.data, request.length);
516 return NT_STATUS_INVALID_PARAMETER;
521 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, ntlmssp_state->allow_lm_key);
523 if (DEBUGLEVEL >= 10) {
524 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
525 talloc_tos(), struct AUTHENTICATE_MESSAGE);
526 if (authenticate != NULL) {
528 authenticate->NegotiateFlags = auth_flags;
529 status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
530 &request, authenticate, authenticate);
531 if (NT_STATUS_IS_OK(status)) {
532 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
535 TALLOC_FREE(authenticate);
539 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
540 ntlmssp_state->user, ntlmssp_state->domain,
541 ntlmssp_state->client.netbios_name,
542 (unsigned long)ntlmssp_state->lm_resp.length,
543 (unsigned long)ntlmssp_state->nt_resp.length));
546 file_save("nthash1.dat", &ntlmssp_state->nt_resp.data, &ntlmssp_state->nt_resp.length);
547 file_save("lmhash1.dat", &ntlmssp_state->lm_resp.data, &ntlmssp_state->lm_resp.length);
550 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
553 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
555 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
556 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
557 struct MD5Context md5_session_nonce_ctx;
558 SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
562 memcpy(session_nonce, ntlmssp_state->internal_chal.data, 8);
563 memcpy(&session_nonce[8], ntlmssp_state->lm_resp.data, 8);
565 MD5Init(&md5_session_nonce_ctx);
566 MD5Update(&md5_session_nonce_ctx, session_nonce, 16);
567 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
569 ntlmssp_state->chal = data_blob_talloc(
570 ntlmssp_state, session_nonce_hash, 8);
572 /* LM response is no longer useful */
573 data_blob_free(&ntlmssp_state->lm_resp);
575 /* We changed the effective challenge - set it */
576 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->set_challenge(ntlmssp_state, &ntlmssp_state->chal))) {
577 data_blob_free(&encrypted_session_key);
581 /* LM Key is incompatible. */
582 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
587 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
588 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
589 * smb.conf file) and no NTLMv2 response was sent then the password check
590 * will fail here. JRA.
593 /* Finally, actually ask if the password is OK */
595 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state,
596 &user_session_key, &lm_session_key))) {
597 data_blob_free(&encrypted_session_key);
601 dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
602 dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
604 /* Handle the different session key derivation for NTLM2 */
606 if (user_session_key.data && user_session_key.length == 16) {
607 session_key = data_blob_talloc(ntlmssp_state,
609 hmac_md5(user_session_key.data, session_nonce,
610 sizeof(session_nonce), session_key.data);
611 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
612 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
615 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
616 session_key = data_blob_null;
618 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
619 if (lm_session_key.data && lm_session_key.length >= 8) {
620 if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
621 session_key = data_blob_talloc(ntlmssp_state,
623 if (session_key.data == NULL) {
624 return NT_STATUS_NO_MEMORY;
626 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
628 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
630 static const uint8_t zeros[24] = {0, };
631 session_key = data_blob_talloc(
632 ntlmssp_state, NULL, 16);
633 if (session_key.data == NULL) {
634 return NT_STATUS_NO_MEMORY;
636 SMBsesskeygen_lm_sess_key(
637 lm_session_key.data, zeros,
640 dump_data_pw("LM session key:\n", session_key.data,
643 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
644 session_key = data_blob_null;
646 } else if (user_session_key.data) {
647 session_key = user_session_key;
648 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
649 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
650 } else if (lm_session_key.data) {
651 session_key = lm_session_key;
652 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
653 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
655 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
656 session_key = data_blob_null;
659 /* With KEY_EXCH, the client supplies the proposed session key,
660 but encrypts it with the long-term key */
661 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
662 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
663 data_blob_free(&encrypted_session_key);
664 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
665 (unsigned int)encrypted_session_key.length));
666 return NT_STATUS_INVALID_PARAMETER;
667 } else if (!session_key.data || session_key.length != 16) {
668 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
669 (unsigned int)session_key.length));
670 ntlmssp_state->session_key = session_key;
672 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
673 arcfour_crypt_blob(encrypted_session_key.data,
674 encrypted_session_key.length,
676 ntlmssp_state->session_key = data_blob_talloc(
677 ntlmssp_state, encrypted_session_key.data,
678 encrypted_session_key.length);
679 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
680 encrypted_session_key.length);
683 ntlmssp_state->session_key = session_key;
686 if (!NT_STATUS_IS_OK(nt_status)) {
687 ntlmssp_state->session_key = data_blob_null;
688 } else if (ntlmssp_state->session_key.length) {
689 nt_status = ntlmssp_sign_init(ntlmssp_state);
692 data_blob_free(&encrypted_session_key);
694 /* Only one authentication allowed per server state. */
695 ntlmssp_state->expected_state = NTLMSSP_DONE;
701 * Create an NTLMSSP state machine
703 * @param ntlmssp_state NTLMSSP State, allocated by this function
706 NTSTATUS ntlmssp_server_start(TALLOC_CTX *mem_ctx,
708 const char *netbios_name,
709 const char *netbios_domain,
710 const char *dns_name,
711 const char *dns_domain,
712 struct ntlmssp_state **_ntlmssp_state)
714 struct ntlmssp_state *ntlmssp_state;
720 if (!netbios_domain) {
732 ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
733 if (!ntlmssp_state) {
734 return NT_STATUS_NO_MEMORY;
737 ntlmssp_state->role = NTLMSSP_SERVER;
739 ntlmssp_state->get_challenge = get_challenge;
740 ntlmssp_state->set_challenge = set_challenge;
741 ntlmssp_state->may_set_challenge = may_set_challenge;
743 ntlmssp_state->server.is_standalone = is_standalone;
745 ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
747 ntlmssp_state->allow_lm_key = lp_lanman_auth();
749 ntlmssp_state->neg_flags =
750 NTLMSSP_NEGOTIATE_128 |
751 NTLMSSP_NEGOTIATE_56 |
752 NTLMSSP_NEGOTIATE_VERSION |
753 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
754 NTLMSSP_NEGOTIATE_NTLM |
755 NTLMSSP_NEGOTIATE_NTLM2 |
756 NTLMSSP_NEGOTIATE_KEY_EXCH |
757 NTLMSSP_NEGOTIATE_SIGN |
758 NTLMSSP_NEGOTIATE_SEAL;
760 ntlmssp_state->server.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
761 if (!ntlmssp_state->server.netbios_name) {
762 talloc_free(ntlmssp_state);
763 return NT_STATUS_NO_MEMORY;
765 ntlmssp_state->server.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
766 if (!ntlmssp_state->server.netbios_domain) {
767 talloc_free(ntlmssp_state);
768 return NT_STATUS_NO_MEMORY;
770 ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state, dns_name);
771 if (!ntlmssp_state->server.dns_name) {
772 talloc_free(ntlmssp_state);
773 return NT_STATUS_NO_MEMORY;
775 ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state, dns_domain);
776 if (!ntlmssp_state->server.dns_domain) {
777 talloc_free(ntlmssp_state);
778 return NT_STATUS_NO_MEMORY;
781 *_ntlmssp_state = ntlmssp_state;
785 /*********************************************************************
787 *********************************************************************/
790 * Next state function for the Initial packet
792 * @param ntlmssp_state NTLMSSP State
793 * @param request The request, as a DATA_BLOB. reply.data must be NULL
794 * @param request The reply, as an allocated DATA_BLOB, caller to free.
795 * @return Errors or NT_STATUS_OK.
798 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
799 DATA_BLOB reply, DATA_BLOB *next_request)
801 if (ntlmssp_state->unicode) {
802 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
804 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
807 if (ntlmssp_state->use_ntlmv2) {
808 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
811 /* generate the ntlmssp negotiate packet */
812 msrpc_gen(ntlmssp_state, next_request, "CddAA",
815 ntlmssp_state->neg_flags,
816 ntlmssp_state->client.netbios_domain,
817 ntlmssp_state->client.netbios_name);
819 if (DEBUGLEVEL >= 10) {
820 struct NEGOTIATE_MESSAGE *negotiate = talloc(
821 talloc_tos(), struct NEGOTIATE_MESSAGE);
822 if (negotiate != NULL) {
824 status = ntlmssp_pull_NEGOTIATE_MESSAGE(
825 next_request, negotiate, negotiate);
826 if (NT_STATUS_IS_OK(status)) {
827 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
830 TALLOC_FREE(negotiate);
834 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
836 return NT_STATUS_MORE_PROCESSING_REQUIRED;
840 * Next state function for the Challenge Packet. Generate an auth packet.
842 * @param ntlmssp_state NTLMSSP State
843 * @param request The request, as a DATA_BLOB. reply.data must be NULL
844 * @param request The reply, as an allocated DATA_BLOB, caller to free.
845 * @return Errors or NT_STATUS_OK.
848 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
849 const DATA_BLOB reply, DATA_BLOB *next_request)
851 uint32_t chal_flags, ntlmssp_command, unkn1, unkn2;
852 DATA_BLOB server_domain_blob;
853 DATA_BLOB challenge_blob;
854 DATA_BLOB struct_blob = data_blob_null;
856 const char *chal_parse_string;
857 const char *auth_gen_string;
858 DATA_BLOB lm_response = data_blob_null;
859 DATA_BLOB nt_response = data_blob_null;
860 DATA_BLOB session_key = data_blob_null;
861 DATA_BLOB encrypted_session_key = data_blob_null;
862 NTSTATUS nt_status = NT_STATUS_OK;
864 if (ntlmssp_state->use_ccache) {
865 struct wbcCredentialCacheParams params;
866 struct wbcCredentialCacheInfo *info = NULL;
867 struct wbcAuthErrorInfo *error = NULL;
868 struct wbcNamedBlob auth_blob;
869 struct wbcBlob *wbc_next = NULL;
870 struct wbcBlob *wbc_session_key = NULL;
874 params.account_name = ntlmssp_state->user;
875 params.domain_name = ntlmssp_state->domain;
876 params.level = WBC_CREDENTIAL_CACHE_LEVEL_NTLMSSP;
878 auth_blob.name = "challenge_blob";
880 auth_blob.blob.data = reply.data;
881 auth_blob.blob.length = reply.length;
882 params.num_blobs = 1;
883 params.blobs = &auth_blob;
885 wbc_status = wbcCredentialCache(¶ms, &info, &error);
887 wbcFreeMemory(error);
889 if (!WBC_ERROR_IS_OK(wbc_status)) {
893 for (i=0; i<info->num_blobs; i++) {
894 if (strequal(info->blobs[i].name, "auth_blob")) {
895 wbc_next = &info->blobs[i].blob;
897 if (strequal(info->blobs[i].name, "session_key")) {
898 wbc_session_key = &info->blobs[i].blob;
901 if ((wbc_next == NULL) || (wbc_session_key == NULL)) {
906 *next_request = data_blob(wbc_next->data, wbc_next->length);
907 ntlmssp_state->session_key = data_blob(
908 wbc_session_key->data, wbc_session_key->length);
916 if (!msrpc_parse(ntlmssp_state, &reply, "CdBd",
921 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
922 dump_data(2, reply.data, reply.length);
924 return NT_STATUS_INVALID_PARAMETER;
927 if (DEBUGLEVEL >= 10) {
928 struct CHALLENGE_MESSAGE *challenge = talloc(
929 talloc_tos(), struct CHALLENGE_MESSAGE);
930 if (challenge != NULL) {
932 challenge->NegotiateFlags = chal_flags;
933 status = ntlmssp_pull_CHALLENGE_MESSAGE(
934 &reply, challenge, challenge);
935 if (NT_STATUS_IS_OK(status)) {
936 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
939 TALLOC_FREE(challenge);
943 data_blob_free(&server_domain_blob);
945 DEBUG(3, ("Got challenge flags:\n"));
946 debug_ntlmssp_flags(chal_flags);
948 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
950 if (ntlmssp_state->unicode) {
951 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
952 chal_parse_string = "CdUdbddB";
954 chal_parse_string = "CdUdbdd";
956 auth_gen_string = "CdBBUUUBd";
958 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
959 chal_parse_string = "CdAdbddB";
961 chal_parse_string = "CdAdbdd";
964 auth_gen_string = "CdBBAAABd";
967 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
968 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
970 if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
978 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
979 dump_data(2, reply.data, reply.length);
980 return NT_STATUS_INVALID_PARAMETER;
983 if (chal_flags & NTLMSSP_TARGET_TYPE_SERVER) {
984 ntlmssp_state->server.is_standalone = true;
986 ntlmssp_state->server.is_standalone = false;
988 /* TODO: parse struct_blob and fill in the rest */
989 ntlmssp_state->server.netbios_name = "";
990 ntlmssp_state->server.netbios_domain = server_domain;
991 ntlmssp_state->server.dns_name = "";
992 ntlmssp_state->server.dns_domain = "";
994 if (challenge_blob.length != 8) {
995 data_blob_free(&struct_blob);
996 return NT_STATUS_INVALID_PARAMETER;
999 if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
1000 static const uint8_t zeros[16] = {0, };
1001 /* do nothing - blobs are zero length */
1003 /* session key is all zeros */
1004 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
1006 /* not doing NLTM2 without a password */
1007 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
1008 } else if (ntlmssp_state->use_ntlmv2) {
1009 if (!struct_blob.length) {
1010 /* be lazy, match win2k - we can't do NTLMv2 without it */
1011 DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1012 return NT_STATUS_INVALID_PARAMETER;
1015 /* TODO: if the remote server is standalone, then we should replace 'domain'
1016 with the server name as supplied above */
1018 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
1019 ntlmssp_state->user,
1020 ntlmssp_state->domain,
1021 ntlmssp_state->nt_hash, &challenge_blob,
1023 &lm_response, &nt_response, NULL,
1025 data_blob_free(&challenge_blob);
1026 data_blob_free(&struct_blob);
1027 return NT_STATUS_NO_MEMORY;
1029 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1030 struct MD5Context md5_session_nonce_ctx;
1031 uint8_t session_nonce[16];
1032 uint8_t session_nonce_hash[16];
1033 uint8_t user_session_key[16];
1035 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1036 generate_random_buffer(lm_response.data, 8);
1037 memset(lm_response.data+8, 0, 16);
1039 memcpy(session_nonce, challenge_blob.data, 8);
1040 memcpy(&session_nonce[8], lm_response.data, 8);
1042 MD5Init(&md5_session_nonce_ctx);
1043 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1044 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1045 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1047 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1048 DEBUG(5, ("challenge is: \n"));
1049 dump_data(5, session_nonce_hash, 8);
1051 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1052 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
1056 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1058 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
1059 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1060 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1062 /* lanman auth is insecure, it may be disabled */
1063 if (lp_client_lanman_auth()) {
1064 lm_response = data_blob_talloc(ntlmssp_state,
1066 SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
1070 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1071 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
1074 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1075 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1076 && lp_client_lanman_auth()) {
1077 SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
1079 dump_data_pw("LM session key\n", session_key.data, session_key.length);
1081 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
1082 dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1085 data_blob_free(&struct_blob);
1087 /* Key exchange encryptes a new client-generated session key with
1088 the password-derived key */
1089 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1090 /* Make up a new session key */
1091 uint8_t client_session_key[16];
1092 generate_random_buffer(client_session_key, sizeof(client_session_key));
1094 /* Encrypt the new session key with the old one */
1095 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1096 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1097 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
1098 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1100 /* Mark the new session key as the 'real' session key */
1101 data_blob_free(&session_key);
1102 session_key = data_blob_talloc(ntlmssp_state,
1104 sizeof(client_session_key));
1107 /* this generates the actual auth packet */
1108 if (!msrpc_gen(ntlmssp_state, next_request, auth_gen_string,
1111 lm_response.data, lm_response.length,
1112 nt_response.data, nt_response.length,
1113 ntlmssp_state->domain,
1114 ntlmssp_state->user,
1115 ntlmssp_state->client.netbios_name,
1116 encrypted_session_key.data, encrypted_session_key.length,
1117 ntlmssp_state->neg_flags)) {
1119 return NT_STATUS_NO_MEMORY;
1122 if (DEBUGLEVEL >= 10) {
1123 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
1124 talloc_tos(), struct AUTHENTICATE_MESSAGE);
1125 if (authenticate != NULL) {
1127 authenticate->NegotiateFlags =
1128 ntlmssp_state->neg_flags;
1129 status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
1130 next_request, authenticate, authenticate);
1131 if (NT_STATUS_IS_OK(status)) {
1132 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
1135 TALLOC_FREE(authenticate);
1139 data_blob_free(&encrypted_session_key);
1141 data_blob_free(&ntlmssp_state->chal);
1143 ntlmssp_state->session_key = session_key;
1145 ntlmssp_state->chal = challenge_blob;
1146 ntlmssp_state->lm_resp = lm_response;
1147 ntlmssp_state->nt_resp = nt_response;
1151 ntlmssp_state->expected_state = NTLMSSP_DONE;
1153 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1154 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1160 NTSTATUS ntlmssp_client_start(TALLOC_CTX *mem_ctx,
1161 const char *netbios_name,
1162 const char *netbios_domain,
1164 struct ntlmssp_state **_ntlmssp_state)
1166 struct ntlmssp_state *ntlmssp_state;
1168 if (!netbios_name) {
1172 if (!netbios_domain) {
1173 netbios_domain = "";
1176 ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
1177 if (!ntlmssp_state) {
1178 return NT_STATUS_NO_MEMORY;
1181 ntlmssp_state->role = NTLMSSP_CLIENT;
1183 ntlmssp_state->unicode = True;
1185 ntlmssp_state->use_ntlmv2 = use_ntlmv2;
1187 ntlmssp_state->expected_state = NTLMSSP_INITIAL;
1189 ntlmssp_state->neg_flags =
1190 NTLMSSP_NEGOTIATE_128 |
1191 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
1192 NTLMSSP_NEGOTIATE_NTLM |
1193 NTLMSSP_NEGOTIATE_NTLM2 |
1194 NTLMSSP_NEGOTIATE_KEY_EXCH |
1195 NTLMSSP_REQUEST_TARGET;
1197 ntlmssp_state->client.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
1198 if (!ntlmssp_state->client.netbios_name) {
1199 talloc_free(ntlmssp_state);
1200 return NT_STATUS_NO_MEMORY;
1202 ntlmssp_state->client.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
1203 if (!ntlmssp_state->client.netbios_domain) {
1204 talloc_free(ntlmssp_state);
1205 return NT_STATUS_NO_MEMORY;
1208 *_ntlmssp_state = ntlmssp_state;
1209 return NT_STATUS_OK;