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)) {
333 DEBUG(1, ("ntlmssp_server_negotiate: backend doesn't give a challenge: %s\n",
338 /* Check if we may set the challenge */
339 if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
340 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
343 /* The flags we send back are not just the negotiated flags,
344 * they are also 'what is in this packet'. Therfore, we
345 * operate on 'chal_flags' from here on
348 chal_flags = ntlmssp_state->neg_flags;
350 /* get the right name to fill in as 'target' */
351 target_name = ntlmssp_target_name(ntlmssp_state,
352 neg_flags, &chal_flags);
353 if (target_name == NULL)
354 return NT_STATUS_INVALID_PARAMETER;
356 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
357 ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state,
360 /* This creates the 'blob' of names that appears at the end of the packet */
361 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
363 msrpc_gen(ntlmssp_state, &struct_blob, "aaaaa",
364 MsvAvNbDomainName, target_name,
365 MsvAvNbComputerName, ntlmssp_state->server.netbios_name,
366 MsvAvDnsDomainName, ntlmssp_state->server.dns_domain,
367 MsvAvDnsComputerName, ntlmssp_state->server.dns_name,
370 struct_blob = data_blob_null;
374 /* Marshal the packet in the right format, be it unicode or ASCII */
375 const char *gen_string;
376 DATA_BLOB version_blob = data_blob_null;
378 if (chal_flags & NTLMSSP_NEGOTIATE_VERSION) {
379 enum ndr_err_code err;
382 /* "What Windows returns" as a version number. */
384 vers.ProductMajorVersion = NTLMSSP_WINDOWS_MAJOR_VERSION_6;
385 vers.ProductMinorVersion = NTLMSSP_WINDOWS_MINOR_VERSION_1;
386 vers.ProductBuild = 0;
387 vers.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
389 err = ndr_push_struct_blob(&version_blob,
392 (ndr_push_flags_fn_t)ndr_push_VERSION);
394 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
395 return NT_STATUS_NO_MEMORY;
399 if (ntlmssp_state->unicode) {
400 gen_string = "CdUdbddBb";
402 gen_string = "CdAdbddBb";
405 msrpc_gen(ntlmssp_state, reply, gen_string,
412 struct_blob.data, struct_blob.length,
413 version_blob.data, version_blob.length);
415 data_blob_free(&version_blob);
417 if (DEBUGLEVEL >= 10) {
418 struct CHALLENGE_MESSAGE *challenge = talloc(
419 ntlmssp_state, struct CHALLENGE_MESSAGE);
420 if (challenge != NULL) {
421 challenge->NegotiateFlags = chal_flags;
422 status = ntlmssp_pull_CHALLENGE_MESSAGE(
423 reply, challenge, challenge);
424 if (NT_STATUS_IS_OK(status)) {
425 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
428 TALLOC_FREE(challenge);
433 data_blob_free(&struct_blob);
435 ntlmssp_state->expected_state = NTLMSSP_AUTH;
437 return NT_STATUS_MORE_PROCESSING_REQUIRED;
441 * Next state function for the Authenticate packet
443 * @param ntlmssp_state NTLMSSP State
444 * @param request The request, as a DATA_BLOB
445 * @param request The reply, as an allocated DATA_BLOB, caller to free.
446 * @return Errors or NT_STATUS_OK.
449 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
450 const DATA_BLOB request, DATA_BLOB *reply)
452 DATA_BLOB encrypted_session_key = data_blob_null;
453 DATA_BLOB user_session_key = data_blob_null;
454 DATA_BLOB lm_session_key = data_blob_null;
455 DATA_BLOB session_key = data_blob_null;
456 uint32_t ntlmssp_command, auth_flags;
457 NTSTATUS nt_status = NT_STATUS_OK;
460 bool doing_ntlm2 = False;
462 uint8_t session_nonce[16];
463 uint8_t session_nonce_hash[16];
465 const char *parse_string;
467 /* parse the NTLMSSP packet */
468 *reply = data_blob_null;
471 file_save("ntlmssp_auth.dat", request.data, request.length);
474 if (ntlmssp_state->unicode) {
475 parse_string = "CdBBUUUBd";
477 parse_string = "CdBBAAABd";
480 data_blob_free(&ntlmssp_state->lm_resp);
481 data_blob_free(&ntlmssp_state->nt_resp);
483 ntlmssp_state->user = NULL;
484 ntlmssp_state->domain = NULL;
486 /* now the NTLMSSP encoded auth hashes */
487 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
490 &ntlmssp_state->lm_resp,
491 &ntlmssp_state->nt_resp,
492 &ntlmssp_state->domain,
493 &ntlmssp_state->user,
494 &ntlmssp_state->client.netbios_name,
495 &encrypted_session_key,
499 /* Try again with a shorter string (Win9X truncates this packet) */
500 if (ntlmssp_state->unicode) {
501 parse_string = "CdBBUUU";
503 parse_string = "CdBBAAA";
506 /* now the NTLMSSP encoded auth hashes */
507 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
510 &ntlmssp_state->lm_resp,
511 &ntlmssp_state->nt_resp,
512 &ntlmssp_state->domain,
513 &ntlmssp_state->user,
514 &ntlmssp_state->client.netbios_name)) {
515 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
516 dump_data(2, request.data, request.length);
518 return NT_STATUS_INVALID_PARAMETER;
523 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, ntlmssp_state->allow_lm_key);
525 if (DEBUGLEVEL >= 10) {
526 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
527 ntlmssp_state, struct AUTHENTICATE_MESSAGE);
528 if (authenticate != NULL) {
530 authenticate->NegotiateFlags = auth_flags;
531 status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
532 &request, authenticate, authenticate);
533 if (NT_STATUS_IS_OK(status)) {
534 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
537 TALLOC_FREE(authenticate);
541 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
542 ntlmssp_state->user, ntlmssp_state->domain,
543 ntlmssp_state->client.netbios_name,
544 (unsigned long)ntlmssp_state->lm_resp.length,
545 (unsigned long)ntlmssp_state->nt_resp.length));
548 file_save("nthash1.dat", &ntlmssp_state->nt_resp.data, &ntlmssp_state->nt_resp.length);
549 file_save("lmhash1.dat", &ntlmssp_state->lm_resp.data, &ntlmssp_state->lm_resp.length);
552 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
555 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
557 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
558 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
559 struct MD5Context md5_session_nonce_ctx;
560 SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
564 memcpy(session_nonce, ntlmssp_state->internal_chal.data, 8);
565 memcpy(&session_nonce[8], ntlmssp_state->lm_resp.data, 8);
567 MD5Init(&md5_session_nonce_ctx);
568 MD5Update(&md5_session_nonce_ctx, session_nonce, 16);
569 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
571 ntlmssp_state->chal = data_blob_talloc(
572 ntlmssp_state, session_nonce_hash, 8);
574 /* LM response is no longer useful */
575 data_blob_free(&ntlmssp_state->lm_resp);
577 /* We changed the effective challenge - set it */
578 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->set_challenge(ntlmssp_state, &ntlmssp_state->chal))) {
579 data_blob_free(&encrypted_session_key);
583 /* LM Key is incompatible. */
584 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
589 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
590 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
591 * smb.conf file) and no NTLMv2 response was sent then the password check
592 * will fail here. JRA.
595 /* Finally, actually ask if the password is OK */
597 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state,
598 &user_session_key, &lm_session_key))) {
599 data_blob_free(&encrypted_session_key);
603 dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
604 dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
606 /* Handle the different session key derivation for NTLM2 */
608 if (user_session_key.data && user_session_key.length == 16) {
609 session_key = data_blob_talloc(ntlmssp_state,
611 hmac_md5(user_session_key.data, session_nonce,
612 sizeof(session_nonce), session_key.data);
613 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
614 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
617 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
618 session_key = data_blob_null;
620 } else if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
621 /* Ensure we can never get here on NTLMv2 */
622 && (ntlmssp_state->nt_resp.length == 0 || ntlmssp_state->nt_resp.length == 24)) {
624 if (lm_session_key.data && lm_session_key.length >= 8) {
625 if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
626 session_key = data_blob_talloc(ntlmssp_state,
628 if (session_key.data == NULL) {
629 return NT_STATUS_NO_MEMORY;
631 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
633 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
635 static const uint8_t zeros[24] = {0, };
636 session_key = data_blob_talloc(
637 ntlmssp_state, NULL, 16);
638 if (session_key.data == NULL) {
639 return NT_STATUS_NO_MEMORY;
641 SMBsesskeygen_lm_sess_key(zeros, zeros,
643 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
645 dump_data_pw("LM session key:\n", session_key.data,
648 /* LM Key not selected */
649 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
651 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
652 session_key = data_blob_null;
654 } else if (user_session_key.data) {
655 session_key = user_session_key;
656 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
657 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
659 /* LM Key not selected */
660 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
662 } else if (lm_session_key.data) {
663 /* Very weird to have LM key, but no user session key, but anyway.. */
664 session_key = lm_session_key;
665 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
666 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
668 /* LM Key not selected */
669 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
672 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
673 session_key = data_blob_null;
675 /* LM Key not selected */
676 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
679 /* With KEY_EXCH, the client supplies the proposed session key,
680 but encrypts it with the long-term key */
681 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
682 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
683 data_blob_free(&encrypted_session_key);
684 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
685 (unsigned int)encrypted_session_key.length));
686 return NT_STATUS_INVALID_PARAMETER;
687 } else if (!session_key.data || session_key.length != 16) {
688 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
689 (unsigned int)session_key.length));
690 ntlmssp_state->session_key = session_key;
692 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
693 arcfour_crypt_blob(encrypted_session_key.data,
694 encrypted_session_key.length,
696 ntlmssp_state->session_key = data_blob_talloc(
697 ntlmssp_state, encrypted_session_key.data,
698 encrypted_session_key.length);
699 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
700 encrypted_session_key.length);
703 ntlmssp_state->session_key = session_key;
706 if (ntlmssp_state->session_key.length) {
707 nt_status = ntlmssp_sign_init(ntlmssp_state);
710 data_blob_free(&encrypted_session_key);
712 /* Only one authentication allowed per server state. */
713 ntlmssp_state->expected_state = NTLMSSP_DONE;
719 * Create an NTLMSSP state machine
721 * @param ntlmssp_state NTLMSSP State, allocated by this function
724 NTSTATUS ntlmssp_server_start(TALLOC_CTX *mem_ctx,
726 const char *netbios_name,
727 const char *netbios_domain,
728 const char *dns_name,
729 const char *dns_domain,
730 struct ntlmssp_state **_ntlmssp_state)
732 struct ntlmssp_state *ntlmssp_state;
738 if (!netbios_domain) {
750 ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
751 if (!ntlmssp_state) {
752 return NT_STATUS_NO_MEMORY;
755 ntlmssp_state->role = NTLMSSP_SERVER;
757 ntlmssp_state->get_challenge = get_challenge;
758 ntlmssp_state->set_challenge = set_challenge;
759 ntlmssp_state->may_set_challenge = may_set_challenge;
761 ntlmssp_state->server.is_standalone = is_standalone;
763 ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
765 ntlmssp_state->allow_lm_key = lp_lanman_auth();
767 ntlmssp_state->neg_flags =
768 NTLMSSP_NEGOTIATE_128 |
769 NTLMSSP_NEGOTIATE_56 |
770 NTLMSSP_NEGOTIATE_VERSION |
771 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
772 NTLMSSP_NEGOTIATE_NTLM |
773 NTLMSSP_NEGOTIATE_NTLM2 |
774 NTLMSSP_NEGOTIATE_KEY_EXCH |
775 NTLMSSP_NEGOTIATE_SIGN |
776 NTLMSSP_NEGOTIATE_SEAL;
778 ntlmssp_state->server.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
779 if (!ntlmssp_state->server.netbios_name) {
780 talloc_free(ntlmssp_state);
781 return NT_STATUS_NO_MEMORY;
783 ntlmssp_state->server.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
784 if (!ntlmssp_state->server.netbios_domain) {
785 talloc_free(ntlmssp_state);
786 return NT_STATUS_NO_MEMORY;
788 ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state, dns_name);
789 if (!ntlmssp_state->server.dns_name) {
790 talloc_free(ntlmssp_state);
791 return NT_STATUS_NO_MEMORY;
793 ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state, dns_domain);
794 if (!ntlmssp_state->server.dns_domain) {
795 talloc_free(ntlmssp_state);
796 return NT_STATUS_NO_MEMORY;
799 *_ntlmssp_state = ntlmssp_state;
803 /*********************************************************************
805 *********************************************************************/
808 * Next state function for the Initial packet
810 * @param ntlmssp_state NTLMSSP State
811 * @param request The request, as a DATA_BLOB. reply.data must be NULL
812 * @param request The reply, as an allocated DATA_BLOB, caller to free.
813 * @return Errors or NT_STATUS_OK.
816 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
817 DATA_BLOB reply, DATA_BLOB *next_request)
819 if (ntlmssp_state->unicode) {
820 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
822 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
825 if (ntlmssp_state->use_ntlmv2) {
826 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
829 /* generate the ntlmssp negotiate packet */
830 msrpc_gen(ntlmssp_state, next_request, "CddAA",
833 ntlmssp_state->neg_flags,
834 ntlmssp_state->client.netbios_domain,
835 ntlmssp_state->client.netbios_name);
837 if (DEBUGLEVEL >= 10) {
838 struct NEGOTIATE_MESSAGE *negotiate = talloc(
839 talloc_tos(), struct NEGOTIATE_MESSAGE);
840 if (negotiate != NULL) {
842 status = ntlmssp_pull_NEGOTIATE_MESSAGE(
843 next_request, negotiate, negotiate);
844 if (NT_STATUS_IS_OK(status)) {
845 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
848 TALLOC_FREE(negotiate);
852 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
854 return NT_STATUS_MORE_PROCESSING_REQUIRED;
858 * Next state function for the Challenge Packet. Generate an auth packet.
860 * @param ntlmssp_state NTLMSSP State
861 * @param request The request, as a DATA_BLOB. reply.data must be NULL
862 * @param request The reply, as an allocated DATA_BLOB, caller to free.
863 * @return Errors or NT_STATUS_OK.
866 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
867 const DATA_BLOB reply, DATA_BLOB *next_request)
869 uint32_t chal_flags, ntlmssp_command, unkn1, unkn2;
870 DATA_BLOB server_domain_blob;
871 DATA_BLOB challenge_blob;
872 DATA_BLOB struct_blob = data_blob_null;
874 const char *chal_parse_string;
875 const char *auth_gen_string;
876 DATA_BLOB lm_response = data_blob_null;
877 DATA_BLOB nt_response = data_blob_null;
878 DATA_BLOB session_key = data_blob_null;
879 DATA_BLOB encrypted_session_key = data_blob_null;
880 NTSTATUS nt_status = NT_STATUS_OK;
882 if (ntlmssp_state->use_ccache) {
883 struct wbcCredentialCacheParams params;
884 struct wbcCredentialCacheInfo *info = NULL;
885 struct wbcAuthErrorInfo *error = NULL;
886 struct wbcNamedBlob auth_blob;
887 struct wbcBlob *wbc_next = NULL;
888 struct wbcBlob *wbc_session_key = NULL;
892 params.account_name = ntlmssp_state->user;
893 params.domain_name = ntlmssp_state->domain;
894 params.level = WBC_CREDENTIAL_CACHE_LEVEL_NTLMSSP;
896 auth_blob.name = "challenge_blob";
898 auth_blob.blob.data = reply.data;
899 auth_blob.blob.length = reply.length;
900 params.num_blobs = 1;
901 params.blobs = &auth_blob;
903 wbc_status = wbcCredentialCache(¶ms, &info, &error);
905 wbcFreeMemory(error);
907 if (!WBC_ERROR_IS_OK(wbc_status)) {
911 for (i=0; i<info->num_blobs; i++) {
912 if (strequal(info->blobs[i].name, "auth_blob")) {
913 wbc_next = &info->blobs[i].blob;
915 if (strequal(info->blobs[i].name, "session_key")) {
916 wbc_session_key = &info->blobs[i].blob;
919 if ((wbc_next == NULL) || (wbc_session_key == NULL)) {
924 *next_request = data_blob(wbc_next->data, wbc_next->length);
925 ntlmssp_state->session_key = data_blob(
926 wbc_session_key->data, wbc_session_key->length);
934 if (!msrpc_parse(ntlmssp_state, &reply, "CdBd",
939 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
940 dump_data(2, reply.data, reply.length);
942 return NT_STATUS_INVALID_PARAMETER;
945 if (DEBUGLEVEL >= 10) {
946 struct CHALLENGE_MESSAGE *challenge = talloc(
947 talloc_tos(), struct CHALLENGE_MESSAGE);
948 if (challenge != NULL) {
950 challenge->NegotiateFlags = chal_flags;
951 status = ntlmssp_pull_CHALLENGE_MESSAGE(
952 &reply, challenge, challenge);
953 if (NT_STATUS_IS_OK(status)) {
954 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
957 TALLOC_FREE(challenge);
961 data_blob_free(&server_domain_blob);
963 DEBUG(3, ("Got challenge flags:\n"));
964 debug_ntlmssp_flags(chal_flags);
966 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
968 if (ntlmssp_state->unicode) {
969 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
970 chal_parse_string = "CdUdbddB";
972 chal_parse_string = "CdUdbdd";
974 auth_gen_string = "CdBBUUUBd";
976 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
977 chal_parse_string = "CdAdbddB";
979 chal_parse_string = "CdAdbdd";
982 auth_gen_string = "CdBBAAABd";
985 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
986 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
988 if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
996 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
997 dump_data(2, reply.data, reply.length);
998 return NT_STATUS_INVALID_PARAMETER;
1001 if (chal_flags & NTLMSSP_TARGET_TYPE_SERVER) {
1002 ntlmssp_state->server.is_standalone = true;
1004 ntlmssp_state->server.is_standalone = false;
1006 /* TODO: parse struct_blob and fill in the rest */
1007 ntlmssp_state->server.netbios_name = "";
1008 ntlmssp_state->server.netbios_domain = server_domain;
1009 ntlmssp_state->server.dns_name = "";
1010 ntlmssp_state->server.dns_domain = "";
1012 if (challenge_blob.length != 8) {
1013 data_blob_free(&struct_blob);
1014 return NT_STATUS_INVALID_PARAMETER;
1017 if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
1018 static const uint8_t zeros[16] = {0, };
1019 /* do nothing - blobs are zero length */
1021 /* session key is all zeros */
1022 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
1024 /* not doing NLTM2 without a password */
1025 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
1026 } else if (ntlmssp_state->use_ntlmv2) {
1027 if (!struct_blob.length) {
1028 /* be lazy, match win2k - we can't do NTLMv2 without it */
1029 DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1030 return NT_STATUS_INVALID_PARAMETER;
1033 /* TODO: if the remote server is standalone, then we should replace 'domain'
1034 with the server name as supplied above */
1036 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
1037 ntlmssp_state->user,
1038 ntlmssp_state->domain,
1039 ntlmssp_state->nt_hash, &challenge_blob,
1041 &lm_response, &nt_response, NULL,
1043 data_blob_free(&challenge_blob);
1044 data_blob_free(&struct_blob);
1045 return NT_STATUS_NO_MEMORY;
1047 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1048 struct MD5Context md5_session_nonce_ctx;
1049 uint8_t session_nonce[16];
1050 uint8_t session_nonce_hash[16];
1051 uint8_t user_session_key[16];
1053 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1054 generate_random_buffer(lm_response.data, 8);
1055 memset(lm_response.data+8, 0, 16);
1057 memcpy(session_nonce, challenge_blob.data, 8);
1058 memcpy(&session_nonce[8], lm_response.data, 8);
1060 MD5Init(&md5_session_nonce_ctx);
1061 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1062 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1063 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1065 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1066 DEBUG(5, ("challenge is: \n"));
1067 dump_data(5, session_nonce_hash, 8);
1069 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1070 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
1074 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1076 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
1077 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1078 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1080 /* lanman auth is insecure, it may be disabled */
1081 if (lp_client_lanman_auth()) {
1082 lm_response = data_blob_talloc(ntlmssp_state,
1084 SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
1088 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1089 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
1092 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1093 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1094 && lp_client_lanman_auth()) {
1095 SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
1097 dump_data_pw("LM session key\n", session_key.data, session_key.length);
1099 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
1100 dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1103 data_blob_free(&struct_blob);
1105 /* Key exchange encryptes a new client-generated session key with
1106 the password-derived key */
1107 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1108 /* Make up a new session key */
1109 uint8_t client_session_key[16];
1110 generate_random_buffer(client_session_key, sizeof(client_session_key));
1112 /* Encrypt the new session key with the old one */
1113 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1114 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1115 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
1116 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1118 /* Mark the new session key as the 'real' session key */
1119 data_blob_free(&session_key);
1120 session_key = data_blob_talloc(ntlmssp_state,
1122 sizeof(client_session_key));
1125 /* this generates the actual auth packet */
1126 if (!msrpc_gen(ntlmssp_state, next_request, auth_gen_string,
1129 lm_response.data, lm_response.length,
1130 nt_response.data, nt_response.length,
1131 ntlmssp_state->domain,
1132 ntlmssp_state->user,
1133 ntlmssp_state->client.netbios_name,
1134 encrypted_session_key.data, encrypted_session_key.length,
1135 ntlmssp_state->neg_flags)) {
1137 return NT_STATUS_NO_MEMORY;
1140 if (DEBUGLEVEL >= 10) {
1141 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
1142 talloc_tos(), struct AUTHENTICATE_MESSAGE);
1143 if (authenticate != NULL) {
1145 authenticate->NegotiateFlags =
1146 ntlmssp_state->neg_flags;
1147 status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
1148 next_request, authenticate, authenticate);
1149 if (NT_STATUS_IS_OK(status)) {
1150 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
1153 TALLOC_FREE(authenticate);
1157 data_blob_free(&encrypted_session_key);
1159 data_blob_free(&ntlmssp_state->chal);
1161 ntlmssp_state->session_key = session_key;
1163 ntlmssp_state->chal = challenge_blob;
1164 ntlmssp_state->lm_resp = lm_response;
1165 ntlmssp_state->nt_resp = nt_response;
1169 ntlmssp_state->expected_state = NTLMSSP_DONE;
1171 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1172 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1178 NTSTATUS ntlmssp_client_start(TALLOC_CTX *mem_ctx,
1179 const char *netbios_name,
1180 const char *netbios_domain,
1182 struct ntlmssp_state **_ntlmssp_state)
1184 struct ntlmssp_state *ntlmssp_state;
1186 if (!netbios_name) {
1190 if (!netbios_domain) {
1191 netbios_domain = "";
1194 ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
1195 if (!ntlmssp_state) {
1196 return NT_STATUS_NO_MEMORY;
1199 ntlmssp_state->role = NTLMSSP_CLIENT;
1201 ntlmssp_state->unicode = True;
1203 ntlmssp_state->use_ntlmv2 = use_ntlmv2;
1205 ntlmssp_state->expected_state = NTLMSSP_INITIAL;
1207 ntlmssp_state->neg_flags =
1208 NTLMSSP_NEGOTIATE_128 |
1209 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
1210 NTLMSSP_NEGOTIATE_NTLM |
1211 NTLMSSP_NEGOTIATE_NTLM2 |
1212 NTLMSSP_NEGOTIATE_KEY_EXCH |
1213 NTLMSSP_REQUEST_TARGET;
1215 ntlmssp_state->client.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
1216 if (!ntlmssp_state->client.netbios_name) {
1217 talloc_free(ntlmssp_state);
1218 return NT_STATUS_NO_MEMORY;
1220 ntlmssp_state->client.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
1221 if (!ntlmssp_state->client.netbios_domain) {
1222 talloc_free(ntlmssp_state);
1223 return NT_STATUS_NO_MEMORY;
1226 *_ntlmssp_state = ntlmssp_state;
1227 return NT_STATUS_OK;