2 Unix SMB/Netbios implementation.
4 handle NTLMSSP, server side
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett 2001-2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "lib/util/tevent_ntstatus.h"
26 #include "lib/util/time_basic.h"
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "auth/ntlmssp/ntlmssp_private.h"
29 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
30 #include "auth/ntlmssp/ntlmssp_ndr.h"
31 #include "../libcli/auth/libcli_auth.h"
32 #include "../lib/crypto/crypto.h"
33 #include "auth/gensec/gensec.h"
34 #include "auth/gensec/gensec_internal.h"
35 #include "auth/common_auth.h"
36 #include "param/param.h"
37 #include "param/loadparm.h"
38 #include "libcli/security/session.h"
41 * Determine correct target name flags for reply, given server role
42 * and negotiated flags
44 * @param ntlmssp_state NTLMSSP State
45 * @param neg_flags The flags from the packet
46 * @param chal_flags The flags to be set in the reply packet
47 * @return The 'target name' string.
50 const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
51 uint32_t neg_flags, uint32_t *chal_flags)
53 if (neg_flags & NTLMSSP_REQUEST_TARGET) {
54 *chal_flags |= NTLMSSP_NEGOTIATE_TARGET_INFO;
55 *chal_flags |= NTLMSSP_REQUEST_TARGET;
56 if (ntlmssp_state->server.is_standalone) {
57 *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
58 return ntlmssp_state->server.netbios_name;
60 *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
61 return ntlmssp_state->server.netbios_domain;
69 * Next state function for the NTLMSSP Negotiate packet
71 * @param gensec_security GENSEC state
72 * @param out_mem_ctx Memory context for *out
73 * @param in The request, as a DATA_BLOB. reply.data must be NULL
74 * @param out The reply, as an allocated DATA_BLOB, caller to free.
75 * @return Errors or MORE_PROCESSING_REQUIRED if (normal) a reply is required.
78 NTSTATUS gensec_ntlmssp_server_negotiate(struct gensec_security *gensec_security,
79 TALLOC_CTX *out_mem_ctx,
80 const DATA_BLOB request, DATA_BLOB *reply)
82 struct gensec_ntlmssp_context *gensec_ntlmssp =
83 talloc_get_type_abort(gensec_security->private_data,
84 struct gensec_ntlmssp_context);
85 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
86 struct auth4_context *auth_context = gensec_security->auth_context;
87 DATA_BLOB struct_blob;
88 uint32_t neg_flags = 0;
89 uint32_t ntlmssp_command, chal_flags;
91 const char *target_name;
93 struct timeval tv_now = timeval_current();
97 * Windows NT 4.0, windows_2000: use 30 minutes,
98 * Windows XP, Windows Server 2003, Windows Vista,
99 * Windows Server 2008, Windows 7, and Windows Server 2008 R2
102 * Newer systems doesn't check this, likely because the
103 * connectionless NTLMSSP is no longer supported.
105 * As we expect the AUTHENTICATION_MESSAGE to arrive
106 * directly after the NEGOTIATE_MESSAGE (typically less than
107 * as 1 second later). We use a hard timeout of 30 Minutes.
109 * We don't look at AUTHENTICATE_MESSAGE.NtChallengeResponse.TimeStamp
110 * instead we just remember our own time.
112 uint32_t max_lifetime = 30 * 60;
113 struct timeval tv_end = timeval_add(&tv_now, max_lifetime, 0);
115 /* parse the NTLMSSP packet */
117 file_save("ntlmssp_negotiate.dat", request.data, request.length);
120 if (request.length) {
121 if (request.length > UINT16_MAX) {
122 DEBUG(1, ("ntlmssp_server_negotiate: reject large request of length %u\n",
123 (unsigned int)request.length));
124 return NT_STATUS_INVALID_PARAMETER;
127 if ((request.length < 16) || !msrpc_parse(ntlmssp_state, &request, "Cdd",
131 DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
132 (unsigned int)request.length));
133 dump_data(2, request.data, request.length);
134 return NT_STATUS_INVALID_PARAMETER;
136 debug_ntlmssp_flags(neg_flags);
138 if (DEBUGLEVEL >= 10) {
139 struct NEGOTIATE_MESSAGE *negotiate = talloc(
140 ntlmssp_state, struct NEGOTIATE_MESSAGE);
141 if (negotiate != NULL) {
142 status = ntlmssp_pull_NEGOTIATE_MESSAGE(
143 &request, negotiate, negotiate);
144 if (NT_STATUS_IS_OK(status)) {
145 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
148 TALLOC_FREE(negotiate);
153 status = ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, "negotiate");
154 if (!NT_STATUS_IS_OK(status)){
158 /* Ask our caller what challenge they would like in the packet */
159 if (auth_context->get_ntlm_challenge) {
160 status = auth_context->get_ntlm_challenge(auth_context, cryptkey);
161 if (!NT_STATUS_IS_OK(status)) {
162 DEBUG(1, ("gensec_ntlmssp_server_negotiate: failed to get challenge: %s\n",
167 DEBUG(1, ("gensec_ntlmssp_server_negotiate: backend doesn't give a challenge\n"));
168 return NT_STATUS_NOT_IMPLEMENTED;
171 /* The flags we send back are not just the negotiated flags,
172 * they are also 'what is in this packet'. Therfore, we
173 * operate on 'chal_flags' from here on
176 chal_flags = ntlmssp_state->neg_flags;
177 ntlmssp_state->server.challenge_endtime = timeval_to_nttime(&tv_end);
179 /* get the right name to fill in as 'target' */
180 target_name = ntlmssp_target_name(ntlmssp_state,
181 neg_flags, &chal_flags);
182 if (target_name == NULL)
183 return NT_STATUS_INVALID_PARAMETER;
185 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
186 ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state,
189 /* This creates the 'blob' of names that appears at the end of the packet */
190 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
191 enum ndr_err_code err;
192 struct AV_PAIR *pairs = NULL;
195 pairs = talloc_zero_array(ntlmssp_state, struct AV_PAIR, count + 1);
197 return NT_STATUS_NO_MEMORY;
200 pairs[0].AvId = MsvAvNbDomainName;
201 pairs[0].Value.AvNbDomainName = target_name;
203 pairs[1].AvId = MsvAvNbComputerName;
204 pairs[1].Value.AvNbComputerName = ntlmssp_state->server.netbios_name;
206 pairs[2].AvId = MsvAvDnsDomainName;
207 pairs[2].Value.AvDnsDomainName = ntlmssp_state->server.dns_domain;
209 pairs[3].AvId = MsvAvDnsComputerName;
210 pairs[3].Value.AvDnsComputerName= ntlmssp_state->server.dns_name;
212 if (!ntlmssp_state->force_old_spnego) {
213 pairs[4].AvId = MsvAvTimestamp;
214 pairs[4].Value.AvTimestamp =
215 timeval_to_nttime(&tv_now);
218 pairs[5].AvId = MsvAvEOL;
220 pairs[4].AvId = MsvAvEOL;
223 ntlmssp_state->server.av_pair_list.count = count;
224 ntlmssp_state->server.av_pair_list.pair = pairs;
226 err = ndr_push_struct_blob(&struct_blob,
228 &ntlmssp_state->server.av_pair_list,
229 (ndr_push_flags_fn_t)ndr_push_AV_PAIR_LIST);
230 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
231 return NT_STATUS_NO_MEMORY;
234 struct_blob = data_blob_null;
238 /* Marshal the packet in the right format, be it unicode or ASCII */
239 const char *gen_string;
240 const DATA_BLOB version_blob = ntlmssp_version_blob();
242 if (ntlmssp_state->unicode) {
243 gen_string = "CdUdbddBb";
245 gen_string = "CdAdbddBb";
248 status = msrpc_gen(out_mem_ctx, reply, gen_string,
255 struct_blob.data, struct_blob.length,
256 version_blob.data, version_blob.length);
258 if (!NT_STATUS_IS_OK(status)) {
259 data_blob_free(&struct_blob);
263 if (DEBUGLEVEL >= 10) {
264 struct CHALLENGE_MESSAGE *challenge = talloc(
265 ntlmssp_state, struct CHALLENGE_MESSAGE);
266 if (challenge != NULL) {
267 challenge->NegotiateFlags = chal_flags;
268 status = ntlmssp_pull_CHALLENGE_MESSAGE(
269 reply, challenge, challenge);
270 if (NT_STATUS_IS_OK(status)) {
271 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
274 TALLOC_FREE(challenge);
279 data_blob_free(&struct_blob);
281 ntlmssp_state->negotiate_blob = data_blob_dup_talloc(ntlmssp_state,
283 if (ntlmssp_state->negotiate_blob.length != request.length) {
284 return NT_STATUS_NO_MEMORY;
287 ntlmssp_state->challenge_blob = data_blob_dup_talloc(ntlmssp_state,
289 if (ntlmssp_state->challenge_blob.length != reply->length) {
290 return NT_STATUS_NO_MEMORY;
293 ntlmssp_state->expected_state = NTLMSSP_AUTH;
295 return NT_STATUS_MORE_PROCESSING_REQUIRED;
298 struct ntlmssp_server_auth_state {
299 struct auth_usersupplied_info *user_info;
300 DATA_BLOB user_session_key;
301 DATA_BLOB lm_session_key;
302 /* internal variables used by KEY_EXCH (client-supplied user session key */
303 DATA_BLOB encrypted_session_key;
305 /* internal variables used by NTLM2 */
306 uint8_t session_nonce[16];
309 static NTSTATUS ntlmssp_server_preauth(struct gensec_security *gensec_security,
310 struct gensec_ntlmssp_context *gensec_ntlmssp,
311 struct ntlmssp_server_auth_state *state,
312 const DATA_BLOB request);
313 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security,
314 struct gensec_ntlmssp_context *gensec_ntlmssp,
315 struct ntlmssp_server_auth_state *state,
318 struct tevent_req *ntlmssp_server_auth_send(TALLOC_CTX *mem_ctx,
319 struct tevent_context *ev,
320 struct gensec_security *gensec_security,
323 struct gensec_ntlmssp_context *gensec_ntlmssp =
324 talloc_get_type_abort(gensec_security->private_data,
325 struct gensec_ntlmssp_context);
326 struct auth4_context *auth_context = gensec_security->auth_context;
327 struct tevent_req *req = NULL;
328 struct ntlmssp_server_auth_state *state = NULL;
329 uint8_t authoritative = 0;
332 req = tevent_req_create(mem_ctx, &state,
333 struct ntlmssp_server_auth_state);
338 status = ntlmssp_server_preauth(gensec_security,
341 if (tevent_req_nterror(req, status)) {
342 return tevent_req_post(req, ev);
345 if (auth_context->check_ntlm_password == NULL) {
346 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
347 return tevent_req_post(req, ev);
350 status = auth_context->check_ntlm_password(auth_context,
354 &gensec_ntlmssp->server_returned_info,
355 &state->user_session_key,
356 &state->lm_session_key);
357 if (!NT_STATUS_IS_OK(status)) {
358 DBG_INFO("Checking NTLMSSP password for %s\\%s failed: %s\n",
359 state->user_info->client.domain_name,
360 state->user_info->client.account_name,
363 if (tevent_req_nterror(req, status)) {
364 return tevent_req_post(req, ev);
366 talloc_steal(state, state->user_session_key.data);
367 talloc_steal(state, state->lm_session_key.data);
369 status = ntlmssp_server_postauth(gensec_security,
372 if (tevent_req_nterror(req, status)) {
373 return tevent_req_post(req, ev);
376 tevent_req_done(req);
377 return tevent_req_post(req, ev);
381 * Next state function for the Authenticate packet
383 * @param ntlmssp_state NTLMSSP State
384 * @param request The request, as a DATA_BLOB
385 * @return Errors or NT_STATUS_OK.
388 static NTSTATUS ntlmssp_server_preauth(struct gensec_security *gensec_security,
389 struct gensec_ntlmssp_context *gensec_ntlmssp,
390 struct ntlmssp_server_auth_state *state,
391 const DATA_BLOB request)
393 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
394 struct auth4_context *auth_context = gensec_security->auth_context;
395 struct auth_usersupplied_info *user_info = NULL;
396 uint32_t ntlmssp_command, auth_flags;
398 const unsigned int version_len = 8;
399 DATA_BLOB version_blob = data_blob_null;
400 const unsigned int mic_len = NTLMSSP_MIC_SIZE;
401 DATA_BLOB mic_blob = data_blob_null;
402 uint8_t session_nonce_hash[16];
403 const char *parse_string;
405 struct timeval endtime;
406 bool expired = false;
409 file_save("ntlmssp_auth.dat", request.data, request.length);
412 if (ntlmssp_state->unicode) {
413 parse_string = "CdBBUUUBdbb";
415 parse_string = "CdBBAAABdbb";
419 data_blob_free(&ntlmssp_state->session_key);
420 data_blob_free(&ntlmssp_state->lm_resp);
421 data_blob_free(&ntlmssp_state->nt_resp);
423 ntlmssp_state->user = NULL;
424 ntlmssp_state->domain = NULL;
425 ntlmssp_state->client.netbios_name = NULL;
427 /* now the NTLMSSP encoded auth hashes */
428 ok = msrpc_parse(ntlmssp_state, &request, parse_string,
431 &ntlmssp_state->lm_resp,
432 &ntlmssp_state->nt_resp,
433 &ntlmssp_state->domain,
434 &ntlmssp_state->user,
435 &ntlmssp_state->client.netbios_name,
436 &state->encrypted_session_key,
438 &version_blob, version_len,
441 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
442 dump_data(10, request.data, request.length);
444 data_blob_free(&version_blob);
445 data_blob_free(&mic_blob);
447 if (ntlmssp_state->unicode) {
448 parse_string = "CdBBUUUBd";
450 parse_string = "CdBBAAABd";
453 ok = msrpc_parse(ntlmssp_state, &request, parse_string,
456 &ntlmssp_state->lm_resp,
457 &ntlmssp_state->nt_resp,
458 &ntlmssp_state->domain,
459 &ntlmssp_state->user,
460 &ntlmssp_state->client.netbios_name,
461 &state->encrypted_session_key,
466 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
467 dump_data(10, request.data, request.length);
470 data_blob_free(&state->encrypted_session_key);
473 /* Try again with a shorter string (Win9X truncates this packet) */
474 if (ntlmssp_state->unicode) {
475 parse_string = "CdBBUUU";
477 parse_string = "CdBBAAA";
480 /* now the NTLMSSP encoded auth hashes */
481 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
484 &ntlmssp_state->lm_resp,
485 &ntlmssp_state->nt_resp,
486 &ntlmssp_state->domain,
487 &ntlmssp_state->user,
488 &ntlmssp_state->client.netbios_name)) {
489 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
490 dump_data(2, request.data, request.length);
492 return NT_STATUS_INVALID_PARAMETER;
496 talloc_steal(state, state->encrypted_session_key.data);
498 if (auth_flags != 0) {
499 nt_status = ntlmssp_handle_neg_flags(ntlmssp_state,
502 if (!NT_STATUS_IS_OK(nt_status)){
507 if (DEBUGLEVEL >= 10) {
508 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
509 ntlmssp_state, struct AUTHENTICATE_MESSAGE);
510 if (authenticate != NULL) {
512 authenticate->NegotiateFlags = auth_flags;
513 status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
514 &request, authenticate, authenticate);
515 if (NT_STATUS_IS_OK(status)) {
516 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
519 TALLOC_FREE(authenticate);
523 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
524 ntlmssp_state->user, ntlmssp_state->domain,
525 ntlmssp_state->client.netbios_name,
526 (unsigned long)ntlmssp_state->lm_resp.length,
527 (unsigned long)ntlmssp_state->nt_resp.length));
530 file_save("nthash1.dat", &ntlmssp_state->nt_resp.data, &ntlmssp_state->nt_resp.length);
531 file_save("lmhash1.dat", &ntlmssp_state->lm_resp.data, &ntlmssp_state->lm_resp.length);
534 if (ntlmssp_state->nt_resp.length > 24) {
535 struct NTLMv2_RESPONSE v2_resp;
536 enum ndr_err_code err;
539 const struct AV_PAIR *flags = NULL;
540 const struct AV_PAIR *eol = NULL;
541 uint32_t av_flags = 0;
543 err = ndr_pull_struct_blob(&ntlmssp_state->nt_resp,
546 (ndr_pull_flags_fn_t)ndr_pull_NTLMv2_RESPONSE);
547 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
548 nt_status = ndr_map_error2ntstatus(err);
549 DEBUG(1,("%s: failed to parse NTLMv2_RESPONSE of length %zu for "
550 "user=[%s] domain=[%s] workstation=[%s] - %s %s\n",
551 __func__, ntlmssp_state->nt_resp.length,
552 ntlmssp_state->user, ntlmssp_state->domain,
553 ntlmssp_state->client.netbios_name,
554 ndr_errstr(err), nt_errstr(nt_status)));
559 NDR_PRINT_DEBUG(NTLMv2_RESPONSE, &v2_resp);
562 eol = ndr_ntlmssp_find_av(&v2_resp.Challenge.AvPairs,
565 DEBUG(1,("%s: missing MsvAvEOL for "
566 "user=[%s] domain=[%s] workstation=[%s]\n",
567 __func__, ntlmssp_state->user, ntlmssp_state->domain,
568 ntlmssp_state->client.netbios_name));
569 return NT_STATUS_INVALID_PARAMETER;
572 flags = ndr_ntlmssp_find_av(&v2_resp.Challenge.AvPairs,
575 av_flags = flags->Value.AvFlags;
578 if (av_flags & NTLMSSP_AVFLAG_MIC_IN_AUTHENTICATE_MESSAGE) {
579 if (mic_blob.length != NTLMSSP_MIC_SIZE) {
580 DEBUG(1,("%s: mic_blob.length[%u] for "
581 "user=[%s] domain=[%s] workstation=[%s]\n",
583 (unsigned)mic_blob.length,
585 ntlmssp_state->domain,
586 ntlmssp_state->client.netbios_name));
587 return NT_STATUS_INVALID_PARAMETER;
591 (NTLMSSP_MIC_OFFSET + NTLMSSP_MIC_SIZE))
593 DEBUG(1,("%s: missing MIC "
594 "request.length[%u] for "
595 "user=[%s] domain=[%s] workstation=[%s]\n",
597 (unsigned)request.length,
599 ntlmssp_state->domain,
600 ntlmssp_state->client.netbios_name));
601 return NT_STATUS_INVALID_PARAMETER;
604 ntlmssp_state->new_spnego = true;
607 count = ntlmssp_state->server.av_pair_list.count;
608 if (v2_resp.Challenge.AvPairs.count < count) {
609 return NT_STATUS_INVALID_PARAMETER;
612 for (i = 0; i < count; i++) {
613 const struct AV_PAIR *sp =
614 &ntlmssp_state->server.av_pair_list.pair[i];
615 const struct AV_PAIR *cp = NULL;
617 if (sp->AvId == MsvAvEOL) {
621 cp = ndr_ntlmssp_find_av(&v2_resp.Challenge.AvPairs,
624 DEBUG(1,("%s: AvId 0x%x missing for"
625 "user=[%s] domain=[%s] "
626 "workstation=[%s]\n",
630 ntlmssp_state->domain,
631 ntlmssp_state->client.netbios_name));
632 return NT_STATUS_INVALID_PARAMETER;
636 #define CASE_STRING(v) case Msv ## v: do { \
638 if (sp->Value.v == NULL) { \
639 return NT_STATUS_INTERNAL_ERROR; \
641 if (cp->Value.v == NULL) { \
642 DEBUG(1,("%s: invalid %s " \
643 "got[%s] expect[%s] for " \
644 "user=[%s] domain=[%s] workstation=[%s]\n", \
648 ntlmssp_state->user, \
649 ntlmssp_state->domain, \
650 ntlmssp_state->client.netbios_name)); \
651 return NT_STATUS_INVALID_PARAMETER; \
653 cmp = strcmp(cp->Value.v, sp->Value.v); \
655 DEBUG(1,("%s: invalid %s " \
656 "got[%s] expect[%s] for " \
657 "user=[%s] domain=[%s] workstation=[%s]\n", \
661 ntlmssp_state->user, \
662 ntlmssp_state->domain, \
663 ntlmssp_state->client.netbios_name)); \
664 return NT_STATUS_INVALID_PARAMETER; \
667 CASE_STRING(AvNbComputerName);
668 CASE_STRING(AvNbDomainName);
669 CASE_STRING(AvDnsComputerName);
670 CASE_STRING(AvDnsDomainName);
671 CASE_STRING(AvDnsTreeName);
673 if (cp->Value.AvTimestamp != sp->Value.AvTimestamp) {
676 struct timeval_buf tmp1;
677 struct timeval_buf tmp2;
679 nttime_to_timeval(&ct,
680 cp->Value.AvTimestamp);
681 nttime_to_timeval(&st,
682 sp->Value.AvTimestamp);
684 DEBUG(1,("%s: invalid AvTimestamp "
685 "got[%s] expect[%s] for "
686 "user=[%s] domain=[%s] "
687 "workstation=[%s]\n",
689 timeval_str_buf(&ct, false,
691 timeval_str_buf(&st, false,
694 ntlmssp_state->domain,
695 ntlmssp_state->client.netbios_name));
696 return NT_STATUS_INVALID_PARAMETER;
701 * This can't happen as we control
702 * ntlmssp_state->server.av_pair_list
704 return NT_STATUS_INTERNAL_ERROR;
709 nttime_to_timeval(&endtime, ntlmssp_state->server.challenge_endtime);
710 expired = timeval_expired(&endtime);
712 struct timeval_buf tmp;
713 DEBUG(1,("%s: challenge invalid (expired %s) for "
714 "user=[%s] domain=[%s] workstation=[%s]\n",
716 timeval_str_buf(&endtime, false, true, &tmp),
717 ntlmssp_state->user, ntlmssp_state->domain,
718 ntlmssp_state->client.netbios_name));
719 return NT_STATUS_INVALID_PARAMETER;
722 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
725 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
727 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
728 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
729 MD5_CTX md5_session_nonce_ctx;
730 state->doing_ntlm2 = true;
732 memcpy(state->session_nonce, ntlmssp_state->internal_chal.data, 8);
733 memcpy(&state->session_nonce[8], ntlmssp_state->lm_resp.data, 8);
735 SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
737 MD5Init(&md5_session_nonce_ctx);
738 MD5Update(&md5_session_nonce_ctx, state->session_nonce, 16);
739 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
741 /* LM response is no longer useful */
742 data_blob_free(&ntlmssp_state->lm_resp);
744 /* We changed the effective challenge - set it */
745 if (auth_context->set_ntlm_challenge) {
746 nt_status = auth_context->set_ntlm_challenge(auth_context,
748 "NTLMSSP callback (NTLM2)");
749 if (!NT_STATUS_IS_OK(nt_status)) {
750 DEBUG(1, ("gensec_ntlmssp_server_negotiate: failed to get challenge: %s\n",
751 nt_errstr(nt_status)));
755 DEBUG(1, ("gensec_ntlmssp_server_negotiate: backend doesn't have facility for challenge to be set\n"));
757 return NT_STATUS_NOT_IMPLEMENTED;
760 /* LM Key is incompatible. */
761 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
765 user_info = talloc_zero(state, struct auth_usersupplied_info);
767 return NT_STATUS_NO_MEMORY;
770 user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
771 user_info->flags = 0;
772 user_info->mapped_state = false;
773 user_info->client.account_name = ntlmssp_state->user;
774 user_info->client.domain_name = ntlmssp_state->domain;
775 user_info->workstation_name = ntlmssp_state->client.netbios_name;
776 user_info->remote_host = gensec_get_remote_address(gensec_security);
777 user_info->local_host = gensec_get_local_address(gensec_security);
778 user_info->service_description
779 = gensec_get_target_service_description(gensec_security);
782 * This will just be the string "NTLMSSP" from
783 * gensec_ntlmssp_final_auth_type, but ensures it stays in sync
784 * with the same use in the authorization logging triggered by
785 * gensec_session_info() later
787 user_info->auth_description = gensec_final_auth_type(gensec_security);
789 user_info->password_state = AUTH_PASSWORD_RESPONSE;
790 user_info->password.response.lanman = ntlmssp_state->lm_resp;
791 user_info->password.response.nt = ntlmssp_state->nt_resp;
793 state->user_info = user_info;
798 * Next state function for the Authenticate packet
799 * (after authentication - figures out the session keys etc)
801 * @param ntlmssp_state NTLMSSP State
802 * @return Errors or NT_STATUS_OK.
805 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security,
806 struct gensec_ntlmssp_context *gensec_ntlmssp,
807 struct ntlmssp_server_auth_state *state,
810 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
811 struct auth4_context *auth_context = gensec_security->auth_context;
812 DATA_BLOB user_session_key = state->user_session_key;
813 DATA_BLOB lm_session_key = state->lm_session_key;
814 NTSTATUS nt_status = NT_STATUS_OK;
815 DATA_BLOB session_key = data_blob(NULL, 0);
816 struct auth_session_info *session_info = NULL;
818 TALLOC_FREE(state->user_info);
820 if (lpcfg_map_to_guest(gensec_security->settings->lp_ctx) != NEVER_MAP_TO_GUEST
821 && auth_context->generate_session_info != NULL)
826 * We need to check if the auth is anonymous or mapped to guest
828 tmp_status = auth_context->generate_session_info(auth_context, state,
829 gensec_ntlmssp->server_returned_info,
830 gensec_ntlmssp->ntlmssp_state->user,
831 AUTH_SESSION_INFO_SIMPLE_PRIVILEGES,
833 if (!NT_STATUS_IS_OK(tmp_status)) {
835 * We don't care about failures,
836 * the worst result is that we try MIC checking
837 * for a map to guest authentication.
839 TALLOC_FREE(session_info);
843 if (session_info != NULL) {
844 if (security_session_user_level(session_info, NULL) < SECURITY_USER) {
846 * Anonymous and GUEST are not secure anyway.
847 * avoid new_spnego and MIC checking.
849 ntlmssp_state->new_spnego = false;
850 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SIGN;
851 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SEAL;
853 TALLOC_FREE(session_info);
856 dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
857 dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
859 /* Handle the different session key derivation for NTLM2 */
860 if (state->doing_ntlm2) {
861 if (user_session_key.data && user_session_key.length == 16) {
862 session_key = data_blob_talloc(ntlmssp_state,
864 hmac_md5(user_session_key.data, state->session_nonce,
865 sizeof(state->session_nonce), session_key.data);
866 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
867 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
870 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
871 session_key = data_blob_null;
873 } else if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
874 /* Ensure we can never get here on NTLMv2 */
875 && (ntlmssp_state->nt_resp.length == 0 || ntlmssp_state->nt_resp.length == 24)) {
877 if (lm_session_key.data && lm_session_key.length >= 8) {
878 if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
879 session_key = data_blob_talloc(ntlmssp_state,
881 if (session_key.data == NULL) {
882 return NT_STATUS_NO_MEMORY;
884 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
886 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
888 static const uint8_t zeros[24] = {0, };
889 session_key = data_blob_talloc(
890 ntlmssp_state, NULL, 16);
891 if (session_key.data == NULL) {
892 return NT_STATUS_NO_MEMORY;
894 SMBsesskeygen_lm_sess_key(zeros, zeros,
896 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
898 dump_data_pw("LM session key:\n", session_key.data,
901 /* LM Key not selected */
902 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
904 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
905 session_key = data_blob_null;
908 } else if (user_session_key.data) {
909 session_key = user_session_key;
910 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
911 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
913 /* LM Key not selected */
914 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
916 } else if (lm_session_key.data) {
917 /* Very weird to have LM key, but no user session key, but anyway.. */
918 session_key = lm_session_key;
919 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
920 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
922 /* LM Key not selected */
923 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
926 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
927 session_key = data_blob_null;
929 /* LM Key not selected */
930 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
933 /* With KEY_EXCH, the client supplies the proposed session key,
934 but encrypts it with the long-term key */
935 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
936 if (!state->encrypted_session_key.data
937 || state->encrypted_session_key.length != 16) {
938 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
939 (unsigned)state->encrypted_session_key.length));
940 return NT_STATUS_INVALID_PARAMETER;
941 } else if (!session_key.data || session_key.length != 16) {
942 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
943 (unsigned int)session_key.length));
944 ntlmssp_state->session_key = session_key;
945 talloc_steal(ntlmssp_state, session_key.data);
947 dump_data_pw("KEY_EXCH session key (enc):\n",
948 state->encrypted_session_key.data,
949 state->encrypted_session_key.length);
950 arcfour_crypt(state->encrypted_session_key.data,
952 state->encrypted_session_key.length);
953 ntlmssp_state->session_key = data_blob_talloc(ntlmssp_state,
954 state->encrypted_session_key.data,
955 state->encrypted_session_key.length);
956 dump_data_pw("KEY_EXCH session key:\n",
957 state->encrypted_session_key.data,
958 state->encrypted_session_key.length);
961 ntlmssp_state->session_key = session_key;
962 talloc_steal(ntlmssp_state, session_key.data);
965 if (ntlmssp_state->new_spnego) {
967 uint8_t mic_buffer[NTLMSSP_MIC_SIZE] = { 0, };
970 hmac_md5_init_limK_to_64(ntlmssp_state->session_key.data,
971 ntlmssp_state->session_key.length,
974 hmac_md5_update(ntlmssp_state->negotiate_blob.data,
975 ntlmssp_state->negotiate_blob.length,
977 hmac_md5_update(ntlmssp_state->challenge_blob.data,
978 ntlmssp_state->challenge_blob.length,
981 /* checked were we set ntlmssp_state->new_spnego */
982 SMB_ASSERT(request.length >
983 (NTLMSSP_MIC_OFFSET + NTLMSSP_MIC_SIZE));
985 hmac_md5_update(request.data, NTLMSSP_MIC_OFFSET, &ctx);
986 hmac_md5_update(mic_buffer, NTLMSSP_MIC_SIZE, &ctx);
987 hmac_md5_update(request.data +
988 (NTLMSSP_MIC_OFFSET + NTLMSSP_MIC_SIZE),
990 (NTLMSSP_MIC_OFFSET + NTLMSSP_MIC_SIZE),
992 hmac_md5_final(mic_buffer, &ctx);
994 cmp = memcmp(request.data + NTLMSSP_MIC_OFFSET,
995 mic_buffer, NTLMSSP_MIC_SIZE);
997 DEBUG(1,("%s: invalid NTLMSSP_MIC for "
998 "user=[%s] domain=[%s] workstation=[%s]\n",
1000 ntlmssp_state->user,
1001 ntlmssp_state->domain,
1002 ntlmssp_state->client.netbios_name));
1003 dump_data(1, request.data + NTLMSSP_MIC_OFFSET,
1005 dump_data(1, mic_buffer,
1007 return NT_STATUS_INVALID_PARAMETER;
1011 data_blob_free(&ntlmssp_state->negotiate_blob);
1012 data_blob_free(&ntlmssp_state->challenge_blob);
1014 if (gensec_ntlmssp_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
1015 nt_status = ntlmssp_sign_init(ntlmssp_state);
1018 data_blob_clear_free(&ntlmssp_state->internal_chal);
1019 data_blob_clear_free(&ntlmssp_state->chal);
1020 data_blob_clear_free(&ntlmssp_state->lm_resp);
1021 data_blob_clear_free(&ntlmssp_state->nt_resp);
1023 ntlmssp_state->expected_state = NTLMSSP_DONE;
1028 NTSTATUS ntlmssp_server_auth_recv(struct tevent_req *req,
1029 TALLOC_CTX *out_mem_ctx,
1034 *out = data_blob_null;
1036 if (tevent_req_is_nterror(req, &status)) {
1037 tevent_req_received(req);
1041 tevent_req_received(req);
1042 return NT_STATUS_OK;