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 talloc_tos(), 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 talloc_tos(), 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 if (lm_session_key.data && lm_session_key.length >= 8) {
622 if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
623 session_key = data_blob_talloc(ntlmssp_state,
625 if (session_key.data == NULL) {
626 return NT_STATUS_NO_MEMORY;
628 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
630 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
632 static const uint8_t zeros[24] = {0, };
633 session_key = data_blob_talloc(
634 ntlmssp_state, NULL, 16);
635 if (session_key.data == NULL) {
636 return NT_STATUS_NO_MEMORY;
638 SMBsesskeygen_lm_sess_key(zeros, zeros,
640 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
642 dump_data_pw("LM session key:\n", session_key.data,
645 /* LM Key not selected */
646 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
648 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
649 session_key = data_blob_null;
651 } else if (user_session_key.data) {
652 session_key = user_session_key;
653 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
654 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
656 /* LM Key not selected */
657 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
659 } else if (lm_session_key.data) {
660 /* Very weird to have LM key, but no user session key, but anyway.. */
661 session_key = lm_session_key;
662 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
663 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
665 /* LM Key not selected */
666 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
669 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
670 session_key = data_blob_null;
672 /* LM Key not selected */
673 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
676 /* With KEY_EXCH, the client supplies the proposed session key,
677 but encrypts it with the long-term key */
678 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
679 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
680 data_blob_free(&encrypted_session_key);
681 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
682 (unsigned int)encrypted_session_key.length));
683 return NT_STATUS_INVALID_PARAMETER;
684 } else if (!session_key.data || session_key.length != 16) {
685 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
686 (unsigned int)session_key.length));
687 ntlmssp_state->session_key = session_key;
689 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
690 arcfour_crypt_blob(encrypted_session_key.data,
691 encrypted_session_key.length,
693 ntlmssp_state->session_key = data_blob_talloc(
694 ntlmssp_state, encrypted_session_key.data,
695 encrypted_session_key.length);
696 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
697 encrypted_session_key.length);
700 ntlmssp_state->session_key = session_key;
703 if (!NT_STATUS_IS_OK(nt_status)) {
704 ntlmssp_state->session_key = data_blob_null;
705 } else if (ntlmssp_state->session_key.length) {
706 nt_status = ntlmssp_sign_init(ntlmssp_state);
709 data_blob_free(&encrypted_session_key);
711 /* Only one authentication allowed per server state. */
712 ntlmssp_state->expected_state = NTLMSSP_DONE;
718 * Create an NTLMSSP state machine
720 * @param ntlmssp_state NTLMSSP State, allocated by this function
723 NTSTATUS ntlmssp_server_start(TALLOC_CTX *mem_ctx,
725 const char *netbios_name,
726 const char *netbios_domain,
727 const char *dns_name,
728 const char *dns_domain,
729 struct ntlmssp_state **_ntlmssp_state)
731 struct ntlmssp_state *ntlmssp_state;
737 if (!netbios_domain) {
749 ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
750 if (!ntlmssp_state) {
751 return NT_STATUS_NO_MEMORY;
754 ntlmssp_state->role = NTLMSSP_SERVER;
756 ntlmssp_state->get_challenge = get_challenge;
757 ntlmssp_state->set_challenge = set_challenge;
758 ntlmssp_state->may_set_challenge = may_set_challenge;
760 ntlmssp_state->server.is_standalone = is_standalone;
762 ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
764 ntlmssp_state->allow_lm_key = lp_lanman_auth();
766 ntlmssp_state->neg_flags =
767 NTLMSSP_NEGOTIATE_128 |
768 NTLMSSP_NEGOTIATE_56 |
769 NTLMSSP_NEGOTIATE_VERSION |
770 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
771 NTLMSSP_NEGOTIATE_NTLM |
772 NTLMSSP_NEGOTIATE_NTLM2 |
773 NTLMSSP_NEGOTIATE_KEY_EXCH |
774 NTLMSSP_NEGOTIATE_SIGN |
775 NTLMSSP_NEGOTIATE_SEAL;
777 ntlmssp_state->server.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
778 if (!ntlmssp_state->server.netbios_name) {
779 talloc_free(ntlmssp_state);
780 return NT_STATUS_NO_MEMORY;
782 ntlmssp_state->server.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
783 if (!ntlmssp_state->server.netbios_domain) {
784 talloc_free(ntlmssp_state);
785 return NT_STATUS_NO_MEMORY;
787 ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state, dns_name);
788 if (!ntlmssp_state->server.dns_name) {
789 talloc_free(ntlmssp_state);
790 return NT_STATUS_NO_MEMORY;
792 ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state, dns_domain);
793 if (!ntlmssp_state->server.dns_domain) {
794 talloc_free(ntlmssp_state);
795 return NT_STATUS_NO_MEMORY;
798 *_ntlmssp_state = ntlmssp_state;
802 /*********************************************************************
804 *********************************************************************/
807 * Next state function for the Initial packet
809 * @param ntlmssp_state NTLMSSP State
810 * @param request The request, as a DATA_BLOB. reply.data must be NULL
811 * @param request The reply, as an allocated DATA_BLOB, caller to free.
812 * @return Errors or NT_STATUS_OK.
815 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
816 DATA_BLOB reply, DATA_BLOB *next_request)
818 if (ntlmssp_state->unicode) {
819 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
821 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
824 if (ntlmssp_state->use_ntlmv2) {
825 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
828 /* generate the ntlmssp negotiate packet */
829 msrpc_gen(ntlmssp_state, next_request, "CddAA",
832 ntlmssp_state->neg_flags,
833 ntlmssp_state->client.netbios_domain,
834 ntlmssp_state->client.netbios_name);
836 if (DEBUGLEVEL >= 10) {
837 struct NEGOTIATE_MESSAGE *negotiate = talloc(
838 talloc_tos(), struct NEGOTIATE_MESSAGE);
839 if (negotiate != NULL) {
841 status = ntlmssp_pull_NEGOTIATE_MESSAGE(
842 next_request, negotiate, negotiate);
843 if (NT_STATUS_IS_OK(status)) {
844 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
847 TALLOC_FREE(negotiate);
851 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
853 return NT_STATUS_MORE_PROCESSING_REQUIRED;
857 * Next state function for the Challenge Packet. Generate an auth packet.
859 * @param ntlmssp_state NTLMSSP State
860 * @param request The request, as a DATA_BLOB. reply.data must be NULL
861 * @param request The reply, as an allocated DATA_BLOB, caller to free.
862 * @return Errors or NT_STATUS_OK.
865 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
866 const DATA_BLOB reply, DATA_BLOB *next_request)
868 uint32_t chal_flags, ntlmssp_command, unkn1, unkn2;
869 DATA_BLOB server_domain_blob;
870 DATA_BLOB challenge_blob;
871 DATA_BLOB struct_blob = data_blob_null;
873 const char *chal_parse_string;
874 const char *auth_gen_string;
875 DATA_BLOB lm_response = data_blob_null;
876 DATA_BLOB nt_response = data_blob_null;
877 DATA_BLOB session_key = data_blob_null;
878 DATA_BLOB encrypted_session_key = data_blob_null;
879 NTSTATUS nt_status = NT_STATUS_OK;
881 if (ntlmssp_state->use_ccache) {
882 struct wbcCredentialCacheParams params;
883 struct wbcCredentialCacheInfo *info = NULL;
884 struct wbcAuthErrorInfo *error = NULL;
885 struct wbcNamedBlob auth_blob;
886 struct wbcBlob *wbc_next = NULL;
887 struct wbcBlob *wbc_session_key = NULL;
891 params.account_name = ntlmssp_state->user;
892 params.domain_name = ntlmssp_state->domain;
893 params.level = WBC_CREDENTIAL_CACHE_LEVEL_NTLMSSP;
895 auth_blob.name = "challenge_blob";
897 auth_blob.blob.data = reply.data;
898 auth_blob.blob.length = reply.length;
899 params.num_blobs = 1;
900 params.blobs = &auth_blob;
902 wbc_status = wbcCredentialCache(¶ms, &info, &error);
904 wbcFreeMemory(error);
906 if (!WBC_ERROR_IS_OK(wbc_status)) {
910 for (i=0; i<info->num_blobs; i++) {
911 if (strequal(info->blobs[i].name, "auth_blob")) {
912 wbc_next = &info->blobs[i].blob;
914 if (strequal(info->blobs[i].name, "session_key")) {
915 wbc_session_key = &info->blobs[i].blob;
918 if ((wbc_next == NULL) || (wbc_session_key == NULL)) {
923 *next_request = data_blob(wbc_next->data, wbc_next->length);
924 ntlmssp_state->session_key = data_blob(
925 wbc_session_key->data, wbc_session_key->length);
933 if (!msrpc_parse(ntlmssp_state, &reply, "CdBd",
938 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
939 dump_data(2, reply.data, reply.length);
941 return NT_STATUS_INVALID_PARAMETER;
944 if (DEBUGLEVEL >= 10) {
945 struct CHALLENGE_MESSAGE *challenge = talloc(
946 talloc_tos(), struct CHALLENGE_MESSAGE);
947 if (challenge != NULL) {
949 challenge->NegotiateFlags = chal_flags;
950 status = ntlmssp_pull_CHALLENGE_MESSAGE(
951 &reply, challenge, challenge);
952 if (NT_STATUS_IS_OK(status)) {
953 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
956 TALLOC_FREE(challenge);
960 data_blob_free(&server_domain_blob);
962 DEBUG(3, ("Got challenge flags:\n"));
963 debug_ntlmssp_flags(chal_flags);
965 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
967 if (ntlmssp_state->unicode) {
968 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
969 chal_parse_string = "CdUdbddB";
971 chal_parse_string = "CdUdbdd";
973 auth_gen_string = "CdBBUUUBd";
975 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
976 chal_parse_string = "CdAdbddB";
978 chal_parse_string = "CdAdbdd";
981 auth_gen_string = "CdBBAAABd";
984 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
985 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
987 if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
995 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
996 dump_data(2, reply.data, reply.length);
997 return NT_STATUS_INVALID_PARAMETER;
1000 if (chal_flags & NTLMSSP_TARGET_TYPE_SERVER) {
1001 ntlmssp_state->server.is_standalone = true;
1003 ntlmssp_state->server.is_standalone = false;
1005 /* TODO: parse struct_blob and fill in the rest */
1006 ntlmssp_state->server.netbios_name = "";
1007 ntlmssp_state->server.netbios_domain = server_domain;
1008 ntlmssp_state->server.dns_name = "";
1009 ntlmssp_state->server.dns_domain = "";
1011 if (challenge_blob.length != 8) {
1012 data_blob_free(&struct_blob);
1013 return NT_STATUS_INVALID_PARAMETER;
1016 if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
1017 static const uint8_t zeros[16] = {0, };
1018 /* do nothing - blobs are zero length */
1020 /* session key is all zeros */
1021 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
1023 /* not doing NLTM2 without a password */
1024 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
1025 } else if (ntlmssp_state->use_ntlmv2) {
1026 if (!struct_blob.length) {
1027 /* be lazy, match win2k - we can't do NTLMv2 without it */
1028 DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1029 return NT_STATUS_INVALID_PARAMETER;
1032 /* TODO: if the remote server is standalone, then we should replace 'domain'
1033 with the server name as supplied above */
1035 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
1036 ntlmssp_state->user,
1037 ntlmssp_state->domain,
1038 ntlmssp_state->nt_hash, &challenge_blob,
1040 &lm_response, &nt_response, NULL,
1042 data_blob_free(&challenge_blob);
1043 data_blob_free(&struct_blob);
1044 return NT_STATUS_NO_MEMORY;
1046 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1047 struct MD5Context md5_session_nonce_ctx;
1048 uint8_t session_nonce[16];
1049 uint8_t session_nonce_hash[16];
1050 uint8_t user_session_key[16];
1052 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1053 generate_random_buffer(lm_response.data, 8);
1054 memset(lm_response.data+8, 0, 16);
1056 memcpy(session_nonce, challenge_blob.data, 8);
1057 memcpy(&session_nonce[8], lm_response.data, 8);
1059 MD5Init(&md5_session_nonce_ctx);
1060 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1061 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1062 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1064 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1065 DEBUG(5, ("challenge is: \n"));
1066 dump_data(5, session_nonce_hash, 8);
1068 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1069 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
1073 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1075 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
1076 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1077 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1079 /* lanman auth is insecure, it may be disabled */
1080 if (lp_client_lanman_auth()) {
1081 lm_response = data_blob_talloc(ntlmssp_state,
1083 SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
1087 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1088 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
1091 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1092 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1093 && lp_client_lanman_auth()) {
1094 SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
1096 dump_data_pw("LM session key\n", session_key.data, session_key.length);
1098 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
1099 dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1102 data_blob_free(&struct_blob);
1104 /* Key exchange encryptes a new client-generated session key with
1105 the password-derived key */
1106 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1107 /* Make up a new session key */
1108 uint8_t client_session_key[16];
1109 generate_random_buffer(client_session_key, sizeof(client_session_key));
1111 /* Encrypt the new session key with the old one */
1112 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1113 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1114 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
1115 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1117 /* Mark the new session key as the 'real' session key */
1118 data_blob_free(&session_key);
1119 session_key = data_blob_talloc(ntlmssp_state,
1121 sizeof(client_session_key));
1124 /* this generates the actual auth packet */
1125 if (!msrpc_gen(ntlmssp_state, next_request, auth_gen_string,
1128 lm_response.data, lm_response.length,
1129 nt_response.data, nt_response.length,
1130 ntlmssp_state->domain,
1131 ntlmssp_state->user,
1132 ntlmssp_state->client.netbios_name,
1133 encrypted_session_key.data, encrypted_session_key.length,
1134 ntlmssp_state->neg_flags)) {
1136 return NT_STATUS_NO_MEMORY;
1139 if (DEBUGLEVEL >= 10) {
1140 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
1141 talloc_tos(), struct AUTHENTICATE_MESSAGE);
1142 if (authenticate != NULL) {
1144 authenticate->NegotiateFlags =
1145 ntlmssp_state->neg_flags;
1146 status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
1147 next_request, authenticate, authenticate);
1148 if (NT_STATUS_IS_OK(status)) {
1149 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
1152 TALLOC_FREE(authenticate);
1156 data_blob_free(&encrypted_session_key);
1158 data_blob_free(&ntlmssp_state->chal);
1160 ntlmssp_state->session_key = session_key;
1162 ntlmssp_state->chal = challenge_blob;
1163 ntlmssp_state->lm_resp = lm_response;
1164 ntlmssp_state->nt_resp = nt_response;
1168 ntlmssp_state->expected_state = NTLMSSP_DONE;
1170 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1171 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1177 NTSTATUS ntlmssp_client_start(TALLOC_CTX *mem_ctx,
1178 const char *netbios_name,
1179 const char *netbios_domain,
1181 struct ntlmssp_state **_ntlmssp_state)
1183 struct ntlmssp_state *ntlmssp_state;
1185 if (!netbios_name) {
1189 if (!netbios_domain) {
1190 netbios_domain = "";
1193 ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
1194 if (!ntlmssp_state) {
1195 return NT_STATUS_NO_MEMORY;
1198 ntlmssp_state->role = NTLMSSP_CLIENT;
1200 ntlmssp_state->unicode = True;
1202 ntlmssp_state->use_ntlmv2 = use_ntlmv2;
1204 ntlmssp_state->expected_state = NTLMSSP_INITIAL;
1206 ntlmssp_state->neg_flags =
1207 NTLMSSP_NEGOTIATE_128 |
1208 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
1209 NTLMSSP_NEGOTIATE_NTLM |
1210 NTLMSSP_NEGOTIATE_NTLM2 |
1211 NTLMSSP_NEGOTIATE_KEY_EXCH |
1212 NTLMSSP_REQUEST_TARGET;
1214 ntlmssp_state->client.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
1215 if (!ntlmssp_state->client.netbios_name) {
1216 talloc_free(ntlmssp_state);
1217 return NT_STATUS_NO_MEMORY;
1219 ntlmssp_state->client.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
1220 if (!ntlmssp_state->client.netbios_domain) {
1221 talloc_free(ntlmssp_state);
1222 return NT_STATUS_NO_MEMORY;
1225 *_ntlmssp_state = ntlmssp_state;
1226 return NT_STATUS_OK;