20 #include <winpr/config.h>
25 #include <winpr/crt.h>
26 #include <winpr/assert.h>
27 #include <winpr/print.h>
28 #include <winpr/stream.h>
29 #include <winpr/sysinfo.h>
31 #include "ntlm_compute.h"
33 #include "ntlm_message.h"
35 #include "../../log.h"
36 #define TAG WINPR_TAG("sspi.NTLM")
38 #define NTLM_CheckAndLogRequiredCapacity(tag, s, nmemb, what) \
39 Stream_CheckAndLogRequiredCapacityEx(tag, WLOG_WARN, s, nmemb, 1, "%s(%s:%" PRIuz ") " what, \
40 __func__, __FILE__, (size_t)__LINE__)
42 static const char NTLM_SIGNATURE[8] = {
'N',
'T',
'L',
'M',
'S',
'S',
'P',
'\0' };
46 const char* ntlm_get_negotiate_string(UINT32 flag)
48 if (flag & NTLMSSP_NEGOTIATE_56)
49 return "NTLMSSP_NEGOTIATE_56";
50 if (flag & NTLMSSP_NEGOTIATE_KEY_EXCH)
51 return "NTLMSSP_NEGOTIATE_KEY_EXCH";
52 if (flag & NTLMSSP_NEGOTIATE_128)
53 return "NTLMSSP_NEGOTIATE_128";
54 if (flag & NTLMSSP_RESERVED1)
55 return "NTLMSSP_RESERVED1";
56 if (flag & NTLMSSP_RESERVED2)
57 return "NTLMSSP_RESERVED2";
58 if (flag & NTLMSSP_RESERVED3)
59 return "NTLMSSP_RESERVED3";
60 if (flag & NTLMSSP_NEGOTIATE_VERSION)
61 return "NTLMSSP_NEGOTIATE_VERSION";
62 if (flag & NTLMSSP_RESERVED4)
63 return "NTLMSSP_RESERVED4";
64 if (flag & NTLMSSP_NEGOTIATE_TARGET_INFO)
65 return "NTLMSSP_NEGOTIATE_TARGET_INFO";
66 if (flag & NTLMSSP_REQUEST_NON_NT_SESSION_KEY)
67 return "NTLMSSP_REQUEST_NON_NT_SESSION_KEY";
68 if (flag & NTLMSSP_RESERVED5)
69 return "NTLMSSP_RESERVED5";
70 if (flag & NTLMSSP_NEGOTIATE_IDENTIFY)
71 return "NTLMSSP_NEGOTIATE_IDENTIFY";
72 if (flag & NTLMSSP_NEGOTIATE_EXTENDED_SESSION_SECURITY)
73 return "NTLMSSP_NEGOTIATE_EXTENDED_SESSION_SECURITY";
74 if (flag & NTLMSSP_RESERVED6)
75 return "NTLMSSP_RESERVED6";
76 if (flag & NTLMSSP_TARGET_TYPE_SERVER)
77 return "NTLMSSP_TARGET_TYPE_SERVER";
78 if (flag & NTLMSSP_TARGET_TYPE_DOMAIN)
79 return "NTLMSSP_TARGET_TYPE_DOMAIN";
80 if (flag & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)
81 return "NTLMSSP_NEGOTIATE_ALWAYS_SIGN";
82 if (flag & NTLMSSP_RESERVED7)
83 return "NTLMSSP_RESERVED7";
84 if (flag & NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED)
85 return "NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED";
86 if (flag & NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED)
87 return "NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED";
88 if (flag & NTLMSSP_NEGOTIATE_ANONYMOUS)
89 return "NTLMSSP_NEGOTIATE_ANONYMOUS";
90 if (flag & NTLMSSP_RESERVED8)
91 return "NTLMSSP_RESERVED8";
92 if (flag & NTLMSSP_NEGOTIATE_NTLM)
93 return "NTLMSSP_NEGOTIATE_NTLM";
94 if (flag & NTLMSSP_RESERVED9)
95 return "NTLMSSP_RESERVED9";
96 if (flag & NTLMSSP_NEGOTIATE_LM_KEY)
97 return "NTLMSSP_NEGOTIATE_LM_KEY";
98 if (flag & NTLMSSP_NEGOTIATE_DATAGRAM)
99 return "NTLMSSP_NEGOTIATE_DATAGRAM";
100 if (flag & NTLMSSP_NEGOTIATE_SEAL)
101 return "NTLMSSP_NEGOTIATE_SEAL";
102 if (flag & NTLMSSP_NEGOTIATE_SIGN)
103 return "NTLMSSP_NEGOTIATE_SIGN";
104 if (flag & NTLMSSP_RESERVED10)
105 return "NTLMSSP_RESERVED10";
106 if (flag & NTLMSSP_REQUEST_TARGET)
107 return "NTLMSSP_REQUEST_TARGET";
108 if (flag & NTLMSSP_NEGOTIATE_OEM)
109 return "NTLMSSP_NEGOTIATE_OEM";
110 if (flag & NTLMSSP_NEGOTIATE_UNICODE)
111 return "NTLMSSP_NEGOTIATE_UNICODE";
112 return "NTLMSSP_NEGOTIATE_UNKNOWN";
115 #if defined(WITH_DEBUG_NTLM)
118 WINPR_ASSERT(fields);
121 WLog_VRB(TAG,
"%s (Len: %" PRIu16
" MaxLen: %" PRIu16
" BufferOffset: %" PRIu32
")", name,
122 fields->Len, fields->MaxLen, fields->BufferOffset);
125 winpr_HexDump(TAG, WLOG_TRACE, fields->Buffer, fields->Len);
128 static void ntlm_print_negotiate_flags(UINT32 flags)
130 WLog_VRB(TAG,
"negotiateFlags \"0x%08" PRIX32
"\"", flags);
132 for (
int i = 31; i >= 0; i--)
134 if ((flags >> i) & 1)
136 const char* str = ntlm_get_negotiate_string(1 << i);
137 WLog_VRB(TAG,
"\t%s (%d),", str, (31 - i));
142 static void ntlm_print_negotiate_message(
const SecBuffer* NegotiateMessage,
145 WINPR_ASSERT(NegotiateMessage);
146 WINPR_ASSERT(message);
148 WLog_VRB(TAG,
"NEGOTIATE_MESSAGE (length = %" PRIu32
")", NegotiateMessage->cbBuffer);
149 winpr_HexDump(TAG, WLOG_TRACE, NegotiateMessage->pvBuffer, NegotiateMessage->cbBuffer);
150 ntlm_print_negotiate_flags(message->NegotiateFlags);
152 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION)
153 ntlm_print_version_info(&(message->Version));
156 static void ntlm_print_challenge_message(
const SecBuffer* ChallengeMessage,
160 WINPR_ASSERT(ChallengeMessage);
161 WINPR_ASSERT(message);
163 WLog_VRB(TAG,
"CHALLENGE_MESSAGE (length = %" PRIu32
")", ChallengeMessage->cbBuffer);
164 winpr_HexDump(TAG, WLOG_TRACE, ChallengeMessage->pvBuffer, ChallengeMessage->cbBuffer);
165 ntlm_print_negotiate_flags(message->NegotiateFlags);
167 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION)
168 ntlm_print_version_info(&(message->Version));
170 ntlm_print_message_fields(&(message->TargetName),
"TargetName");
171 ntlm_print_message_fields(&(message->TargetInfo),
"TargetInfo");
173 if (ChallengeTargetInfo && (ChallengeTargetInfo->cbBuffer > 0))
175 WLog_VRB(TAG,
"ChallengeTargetInfo (%" PRIu32
"):", ChallengeTargetInfo->cbBuffer);
176 ntlm_print_av_pair_list(ChallengeTargetInfo->pvBuffer, ChallengeTargetInfo->cbBuffer);
180 static void ntlm_print_authenticate_message(
const SecBuffer* AuthenticateMessage,
184 WINPR_ASSERT(AuthenticateMessage);
185 WINPR_ASSERT(message);
187 WLog_VRB(TAG,
"AUTHENTICATE_MESSAGE (length = %" PRIu32
")", AuthenticateMessage->cbBuffer);
188 winpr_HexDump(TAG, WLOG_TRACE, AuthenticateMessage->pvBuffer, AuthenticateMessage->cbBuffer);
189 ntlm_print_negotiate_flags(message->NegotiateFlags);
191 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION)
192 ntlm_print_version_info(&(message->Version));
194 if (AuthenticateTargetInfo && (AuthenticateTargetInfo->cbBuffer > 0))
196 WLog_VRB(TAG,
"AuthenticateTargetInfo (%" PRIu32
"):", AuthenticateTargetInfo->cbBuffer);
197 ntlm_print_av_pair_list(AuthenticateTargetInfo->pvBuffer, AuthenticateTargetInfo->cbBuffer);
200 ntlm_print_message_fields(&(message->DomainName),
"DomainName");
201 ntlm_print_message_fields(&(message->UserName),
"UserName");
202 ntlm_print_message_fields(&(message->Workstation),
"Workstation");
203 ntlm_print_message_fields(&(message->LmChallengeResponse),
"LmChallengeResponse");
204 ntlm_print_message_fields(&(message->NtChallengeResponse),
"NtChallengeResponse");
205 ntlm_print_message_fields(&(message->EncryptedRandomSessionKey),
"EncryptedRandomSessionKey");
207 if (flags & MSV_AV_FLAGS_MESSAGE_INTEGRITY_CHECK)
209 WLog_VRB(TAG,
"MessageIntegrityCheck (length = 16)");
210 winpr_HexDump(TAG, WLOG_TRACE, message->MessageIntegrityCheck,
211 sizeof(message->MessageIntegrityCheck));
215 static void ntlm_print_authentication_complete(
const NTLM_CONTEXT* context)
217 WINPR_ASSERT(context);
219 WLog_VRB(TAG,
"ClientChallenge");
220 winpr_HexDump(TAG, WLOG_TRACE, context->ClientChallenge, 8);
221 WLog_VRB(TAG,
"ServerChallenge");
222 winpr_HexDump(TAG, WLOG_TRACE, context->ServerChallenge, 8);
223 WLog_VRB(TAG,
"SessionBaseKey");
224 winpr_HexDump(TAG, WLOG_TRACE, context->SessionBaseKey, 16);
225 WLog_VRB(TAG,
"KeyExchangeKey");
226 winpr_HexDump(TAG, WLOG_TRACE, context->KeyExchangeKey, 16);
227 WLog_VRB(TAG,
"ExportedSessionKey");
228 winpr_HexDump(TAG, WLOG_TRACE, context->ExportedSessionKey, 16);
229 WLog_VRB(TAG,
"RandomSessionKey");
230 winpr_HexDump(TAG, WLOG_TRACE, context->RandomSessionKey, 16);
231 WLog_VRB(TAG,
"ClientSigningKey");
232 winpr_HexDump(TAG, WLOG_TRACE, context->ClientSigningKey, 16);
233 WLog_VRB(TAG,
"ClientSealingKey");
234 winpr_HexDump(TAG, WLOG_TRACE, context->ClientSealingKey, 16);
235 WLog_VRB(TAG,
"ServerSigningKey");
236 winpr_HexDump(TAG, WLOG_TRACE, context->ServerSigningKey, 16);
237 WLog_VRB(TAG,
"ServerSealingKey");
238 winpr_HexDump(TAG, WLOG_TRACE, context->ServerSealingKey, 16);
239 WLog_VRB(TAG,
"Timestamp");
240 winpr_HexDump(TAG, WLOG_TRACE, context->Timestamp, 8);
247 WINPR_ASSERT(header);
249 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
252 Stream_Read(s, header->Signature, 8);
253 Stream_Read_UINT32(s, header->MessageType);
255 if (strncmp((
char*)header->Signature, NTLM_SIGNATURE, 8) != 0)
257 char Signature[
sizeof(header->Signature) * 3 + 1] = { 0 };
258 winpr_BinToHexStringBuffer(header->Signature,
sizeof(header->Signature), Signature,
259 sizeof(Signature), TRUE);
261 WLog_ERR(TAG,
"NTLM_MESSAGE_HEADER Invalid signature, got %s, expected %s", Signature,
266 if (header->MessageType != expected)
268 WLog_ERR(TAG,
"NTLM_MESSAGE_HEADER Invalid message tyep, got %s, expected %s",
269 ntlm_message_type_string(header->MessageType), ntlm_message_type_string(expected));
279 WINPR_ASSERT(header);
281 if (!NTLM_CheckAndLogRequiredCapacity(TAG, s,
sizeof(NTLM_SIGNATURE) + 4ull,
282 "NTLM_MESSAGE_HEADER::header"))
285 Stream_Write(s, header->Signature,
sizeof(NTLM_SIGNATURE));
286 Stream_Write_UINT32(s, header->MessageType);
293 WINPR_ASSERT(header);
295 CopyMemory(header->Signature, NTLM_SIGNATURE,
sizeof(NTLM_SIGNATURE));
296 header->MessageType = MessageType;
303 WINPR_ASSERT(fields);
305 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
308 ntlm_free_message_fields_buffer(fields);
310 Stream_Read_UINT16(s, fields->Len);
311 Stream_Read_UINT16(s, fields->MaxLen);
312 Stream_Read_UINT32(s, fields->BufferOffset);
320 WINPR_ASSERT(fields);
322 MaxLen = fields->MaxLen;
323 if (fields->MaxLen < 1)
324 MaxLen = fields->Len;
326 if (!NTLM_CheckAndLogRequiredCapacity(TAG, (s), 8,
"NTLM_MESSAGE_FIELDS::header"))
329 Stream_Write_UINT16(s, fields->Len);
330 Stream_Write_UINT16(s, MaxLen);
331 Stream_Write_UINT32(s, fields->BufferOffset);
338 WINPR_ASSERT(fields);
342 const UINT32 offset = fields->BufferOffset + fields->Len;
344 if (fields->BufferOffset > UINT32_MAX - fields->Len)
347 "NTLM_MESSAGE_FIELDS::BufferOffset %" PRIu32
348 " too large, maximum allowed is %" PRIu32,
349 fields->BufferOffset, UINT32_MAX - fields->Len);
353 if (offset > Stream_Length(s))
356 "NTLM_MESSAGE_FIELDS::Buffer offset %" PRIu32
" beyond received data %" PRIuz,
357 offset, Stream_Length(s));
361 fields->Buffer = (PBYTE)malloc(fields->Len);
365 WLog_ERR(TAG,
"NTLM_MESSAGE_FIELDS::Buffer allocation of %" PRIu16
"bytes failed",
370 Stream_SetPosition(s, fields->BufferOffset);
371 Stream_Read(s, fields->Buffer, fields->Len);
380 WINPR_ASSERT(fields);
384 Stream_SetPosition(s, fields->BufferOffset);
385 if (!NTLM_CheckAndLogRequiredCapacity(TAG, (s), fields->Len,
"NTLM_MESSAGE_FIELDS::Len"))
388 Stream_Write(s, fields->Buffer, fields->Len);
399 free(fields->Buffer);
402 fields->Buffer = NULL;
403 fields->BufferOffset = 0;
408 static BOOL ntlm_read_negotiate_flags(
wStream* s, UINT32* flags, UINT32 required,
const char* name)
410 UINT32 NegotiateFlags = 0;
411 char buffer[1024] = { 0 };
416 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
419 Stream_Read_UINT32(s, NegotiateFlags);
421 if ((NegotiateFlags & required) != required)
423 WLog_ERR(TAG,
"%s::NegotiateFlags invalid flags 0x08%" PRIx32
", 0x%08" PRIx32
" required",
424 name, NegotiateFlags, required);
428 WLog_DBG(TAG,
"Read flags %s",
429 ntlm_negotiate_flags_string(buffer, ARRAYSIZE(buffer), NegotiateFlags));
430 *flags = NegotiateFlags;
434 static BOOL ntlm_write_negotiate_flags(
wStream* s, UINT32 flags,
const char* name)
436 char buffer[1024] = { 0 };
440 if (!Stream_CheckAndLogRequiredCapacityEx(TAG, WLOG_WARN, s, 4ull, 1ull,
441 "%s(%s:%" PRIuz
") %s::NegotiateFlags", __func__,
442 __FILE__, (
size_t)__LINE__, name))
445 WLog_DBG(TAG,
"Write flags %s", ntlm_negotiate_flags_string(buffer, ARRAYSIZE(buffer), flags));
446 Stream_Write_UINT32(s, flags);
450 static BOOL ntlm_read_message_integrity_check(
wStream* s,
size_t* offset, BYTE* data,
size_t size,
454 WINPR_ASSERT(offset);
456 WINPR_ASSERT(size == WINPR_MD5_DIGEST_LENGTH);
459 *offset = Stream_GetPosition(s);
461 if (!Stream_CheckAndLogRequiredLength(TAG, s, size))
464 Stream_Read(s, data, size);
468 static BOOL ntlm_write_message_integrity_check(
wStream* s,
size_t offset,
const BYTE* data,
469 size_t size,
const char* name)
475 WINPR_ASSERT(size == WINPR_MD5_DIGEST_LENGTH);
478 pos = Stream_GetPosition(s);
480 if (!NTLM_CheckAndLogRequiredCapacity(TAG, s, offset,
"MessageIntegrityCheck::offset"))
483 Stream_SetPosition(s, offset);
484 if (!NTLM_CheckAndLogRequiredCapacity(TAG, s, size,
"MessageIntegrityCheck::size"))
487 Stream_Write(s, data, size);
488 Stream_SetPosition(s, pos);
500 WINPR_ASSERT(context);
501 WINPR_ASSERT(buffer);
503 message = &context->NEGOTIATE_MESSAGE;
504 WINPR_ASSERT(message);
508 s = Stream_StaticConstInit(&sbuffer, buffer->pvBuffer, buffer->cbBuffer);
511 return SEC_E_INTERNAL_ERROR;
513 if (!ntlm_read_message_header(s, &message->header, MESSAGE_TYPE_NEGOTIATE))
514 return SEC_E_INVALID_TOKEN;
516 if (!ntlm_read_negotiate_flags(s, &message->NegotiateFlags,
517 NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_NTLM |
518 NTLMSSP_NEGOTIATE_UNICODE,
519 "NTLM_NEGOTIATE_MESSAGE"))
520 return SEC_E_INVALID_TOKEN;
522 context->NegotiateFlags = message->NegotiateFlags;
527 if (!ntlm_read_message_fields(s, &(message->DomainName)))
528 return SEC_E_INVALID_TOKEN;
534 if (!ntlm_read_message_fields(s, &(message->Workstation)))
535 return SEC_E_INVALID_TOKEN;
538 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION)
540 if (!ntlm_read_version_info(s, &(message->Version)))
541 return SEC_E_INVALID_TOKEN;
544 if (!ntlm_read_message_fields_buffer(s, &message->DomainName))
545 return SEC_E_INVALID_TOKEN;
547 if (!ntlm_read_message_fields_buffer(s, &message->Workstation))
548 return SEC_E_INVALID_TOKEN;
550 length = Stream_GetPosition(s);
551 WINPR_ASSERT(length <= UINT32_MAX);
552 buffer->cbBuffer = (ULONG)length;
554 if (!sspi_SecBufferAlloc(&context->NegotiateMessage, (ULONG)length))
555 return SEC_E_INTERNAL_ERROR;
557 CopyMemory(context->NegotiateMessage.pvBuffer, buffer->pvBuffer, buffer->cbBuffer);
558 context->NegotiateMessage.BufferType = buffer->BufferType;
559 #if defined(WITH_DEBUG_NTLM)
560 ntlm_print_negotiate_message(&context->NegotiateMessage, message);
562 ntlm_change_state(context, NTLM_STATE_CHALLENGE);
563 return SEC_I_CONTINUE_NEEDED;
574 WINPR_ASSERT(context);
575 WINPR_ASSERT(buffer);
577 message = &context->NEGOTIATE_MESSAGE;
578 WINPR_ASSERT(message);
582 s = Stream_StaticInit(&sbuffer, buffer->pvBuffer, buffer->cbBuffer);
585 return SEC_E_INTERNAL_ERROR;
587 if (!ntlm_populate_message_header(&message->header, MESSAGE_TYPE_NEGOTIATE))
588 return SEC_E_INTERNAL_ERROR;
592 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_56;
593 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_VERSION;
594 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_LM_KEY;
595 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_OEM;
598 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
599 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_128;
600 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_EXTENDED_SESSION_SECURITY;
601 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
602 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_NTLM;
603 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_SIGN;
604 message->NegotiateFlags |= NTLMSSP_REQUEST_TARGET;
605 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_UNICODE;
607 if (context->confidentiality)
608 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_SEAL;
610 if (context->SendVersionInfo)
611 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_VERSION;
613 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION)
614 ntlm_get_version_info(&(message->Version));
616 context->NegotiateFlags = message->NegotiateFlags;
618 if (!ntlm_write_message_header(s, &message->header))
619 return SEC_E_INTERNAL_ERROR;
621 if (!ntlm_write_negotiate_flags(s, message->NegotiateFlags,
"NTLM_NEGOTIATE_MESSAGE"))
622 return SEC_E_INTERNAL_ERROR;
626 if (!ntlm_write_message_fields(s, &(message->DomainName)))
627 return SEC_E_INTERNAL_ERROR;
631 if (!ntlm_write_message_fields(s, &(message->Workstation)))
632 return SEC_E_INTERNAL_ERROR;
634 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION)
636 if (!ntlm_write_version_info(s, &(message->Version)))
637 return SEC_E_INTERNAL_ERROR;
640 length = Stream_GetPosition(s);
641 WINPR_ASSERT(length <= UINT32_MAX);
642 buffer->cbBuffer = (ULONG)length;
644 if (!sspi_SecBufferAlloc(&context->NegotiateMessage, (ULONG)length))
645 return SEC_E_INTERNAL_ERROR;
647 CopyMemory(context->NegotiateMessage.pvBuffer, buffer->pvBuffer, buffer->cbBuffer);
648 context->NegotiateMessage.BufferType = buffer->BufferType;
649 #if defined(WITH_DEBUG_NTLM)
650 ntlm_print_negotiate_message(&context->NegotiateMessage, message);
652 ntlm_change_state(context, NTLM_STATE_CHALLENGE);
653 return SEC_I_CONTINUE_NEEDED;
658 SECURITY_STATUS status = SEC_E_INVALID_TOKEN;
662 size_t StartOffset = 0;
663 size_t PayloadOffset = 0;
668 if (!context || !buffer)
669 return SEC_E_INTERNAL_ERROR;
671 ntlm_generate_client_challenge(context);
672 message = &context->CHALLENGE_MESSAGE;
673 WINPR_ASSERT(message);
677 s = Stream_StaticConstInit(&sbuffer, buffer->pvBuffer, buffer->cbBuffer);
680 return SEC_E_INTERNAL_ERROR;
682 StartOffset = Stream_GetPosition(s);
684 if (!ntlm_read_message_header(s, &message->header, MESSAGE_TYPE_CHALLENGE))
687 if (!ntlm_read_message_fields(s, &(message->TargetName)))
690 if (!ntlm_read_negotiate_flags(s, &message->NegotiateFlags, 0,
"NTLM_CHALLENGE_MESSAGE"))
693 context->NegotiateFlags = message->NegotiateFlags;
695 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
698 Stream_Read(s, message->ServerChallenge, 8);
699 CopyMemory(context->ServerChallenge, message->ServerChallenge, 8);
700 Stream_Read(s, message->Reserved, 8);
702 if (!ntlm_read_message_fields(s, &(message->TargetInfo)))
705 if (context->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION)
707 if (!ntlm_read_version_info(s, &(message->Version)))
712 PayloadOffset = Stream_GetPosition(s);
714 status = SEC_E_INTERNAL_ERROR;
715 if (message->TargetName.Len > 0)
717 if (!ntlm_read_message_fields_buffer(s, &(message->TargetName)))
721 if (message->TargetInfo.Len > 0)
723 size_t cbAvTimestamp = 0;
725 if (!ntlm_read_message_fields_buffer(s, &(message->TargetInfo)))
728 context->ChallengeTargetInfo.pvBuffer = message->TargetInfo.Buffer;
729 context->ChallengeTargetInfo.cbBuffer = message->TargetInfo.Len;
730 AvTimestamp = ntlm_av_pair_get((
NTLM_AV_PAIR*)message->TargetInfo.Buffer,
731 message->TargetInfo.Len, MsvAvTimestamp, &cbAvTimestamp);
735 PBYTE ptr = ntlm_av_pair_get_value_pointer(AvTimestamp);
741 context->UseMIC = TRUE;
743 CopyMemory(context->ChallengeTimestamp, ptr, 8);
747 length = (PayloadOffset - StartOffset) + message->TargetName.Len + message->TargetInfo.Len;
748 if (length > buffer->cbBuffer)
751 if (!sspi_SecBufferAlloc(&context->ChallengeMessage, (ULONG)length))
754 if (context->ChallengeMessage.pvBuffer)
755 CopyMemory(context->ChallengeMessage.pvBuffer, Stream_Buffer(s) + StartOffset, length);
756 #if defined(WITH_DEBUG_NTLM)
757 ntlm_print_challenge_message(&context->ChallengeMessage, message, NULL);
763 if (!ntlm_construct_authenticate_target_info(context))
766 sspi_SecBufferFree(&context->ChallengeTargetInfo);
767 context->ChallengeTargetInfo.pvBuffer = context->AuthenticateTargetInfo.pvBuffer;
768 context->ChallengeTargetInfo.cbBuffer = context->AuthenticateTargetInfo.cbBuffer;
771 ntlm_generate_timestamp(context);
773 const SECURITY_STATUS rc = ntlm_compute_lm_v2_response(context);
780 const SECURITY_STATUS rc2 = ntlm_compute_ntlm_v2_response(context);
787 ntlm_generate_key_exchange_key(context);
788 ntlm_generate_random_session_key(context);
789 ntlm_generate_exported_session_key(context);
790 ntlm_encrypt_random_session_key(context);
793 status = SEC_E_ENCRYPT_FAILURE;
794 if (!ntlm_generate_client_signing_key(context))
796 if (!ntlm_generate_server_signing_key(context))
799 if (!ntlm_generate_client_sealing_key(context))
801 if (!ntlm_generate_server_sealing_key(context))
804 if (!ntlm_init_rc4_seal_states(context))
806 #if defined(WITH_DEBUG_NTLM)
807 ntlm_print_authentication_complete(context);
809 ntlm_change_state(context, NTLM_STATE_AUTHENTICATE);
810 status = SEC_I_CONTINUE_NEEDED;
812 ntlm_free_message_fields_buffer(&(message->TargetName));
821 UINT32 PayloadOffset = 0;
825 WINPR_ASSERT(context);
826 WINPR_ASSERT(buffer);
828 message = &context->CHALLENGE_MESSAGE;
829 WINPR_ASSERT(message);
833 s = Stream_StaticInit(&sbuffer, buffer->pvBuffer, buffer->cbBuffer);
836 return SEC_E_INTERNAL_ERROR;
838 ntlm_get_version_info(&(message->Version));
839 ntlm_generate_server_challenge(context);
840 ntlm_generate_timestamp(context);
842 if (!ntlm_construct_challenge_target_info(context))
843 return SEC_E_INTERNAL_ERROR;
845 CopyMemory(message->ServerChallenge, context->ServerChallenge, 8);
846 message->NegotiateFlags = context->NegotiateFlags;
847 if (!ntlm_populate_message_header(&message->header, MESSAGE_TYPE_CHALLENGE))
848 return SEC_E_INTERNAL_ERROR;
851 if (!ntlm_write_message_header(s, &message->header))
852 return SEC_E_INTERNAL_ERROR;
854 if (message->NegotiateFlags & NTLMSSP_REQUEST_TARGET)
856 message->TargetName.Len = (UINT16)context->TargetName.cbBuffer;
857 message->TargetName.Buffer = (PBYTE)context->TargetName.pvBuffer;
860 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_TARGET_INFO;
862 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_TARGET_INFO)
864 message->TargetInfo.Len = (UINT16)context->ChallengeTargetInfo.cbBuffer;
865 message->TargetInfo.Buffer = (PBYTE)context->ChallengeTargetInfo.pvBuffer;
870 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION)
873 message->TargetName.BufferOffset = PayloadOffset;
874 message->TargetInfo.BufferOffset = message->TargetName.BufferOffset + message->TargetName.Len;
876 if (!ntlm_write_message_fields(s, &(message->TargetName)))
877 return SEC_E_INTERNAL_ERROR;
879 if (!ntlm_write_negotiate_flags(s, message->NegotiateFlags,
"NTLM_CHALLENGE_MESSAGE"))
880 return SEC_E_INTERNAL_ERROR;
882 if (!NTLM_CheckAndLogRequiredCapacity(TAG, s, 16,
"NTLM_CHALLENGE_MESSAGE::ServerChallenge"))
883 return SEC_E_INTERNAL_ERROR;
885 Stream_Write(s, message->ServerChallenge, 8);
886 Stream_Write(s, message->Reserved, 8);
889 if (!ntlm_write_message_fields(s, &(message->TargetInfo)))
890 return SEC_E_INTERNAL_ERROR;
892 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION)
894 if (!ntlm_write_version_info(s, &(message->Version)))
895 return SEC_E_INTERNAL_ERROR;
899 if (message->NegotiateFlags & NTLMSSP_REQUEST_TARGET)
901 if (!ntlm_write_message_fields_buffer(s, &(message->TargetName)))
902 return SEC_E_INTERNAL_ERROR;
905 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_TARGET_INFO)
907 if (!ntlm_write_message_fields_buffer(s, &(message->TargetInfo)))
908 return SEC_E_INTERNAL_ERROR;
911 length = Stream_GetPosition(s);
912 WINPR_ASSERT(length <= UINT32_MAX);
913 buffer->cbBuffer = (ULONG)length;
915 if (!sspi_SecBufferAlloc(&context->ChallengeMessage, (ULONG)length))
916 return SEC_E_INTERNAL_ERROR;
918 CopyMemory(context->ChallengeMessage.pvBuffer, Stream_Buffer(s), length);
919 #if defined(WITH_DEBUG_NTLM)
920 ntlm_print_challenge_message(&context->ChallengeMessage, message,
921 &context->ChallengeTargetInfo);
923 ntlm_change_state(context, NTLM_STATE_AUTHENTICATE);
924 return SEC_I_CONTINUE_NEEDED;
929 SECURITY_STATUS status = SEC_E_INVALID_TOKEN;
935 size_t PayloadBufferOffset = 0;
940 WINPR_ASSERT(context);
941 WINPR_ASSERT(buffer);
943 credentials = context->credentials;
944 WINPR_ASSERT(credentials);
946 message = &context->AUTHENTICATE_MESSAGE;
947 WINPR_ASSERT(message);
951 s = Stream_StaticConstInit(&sbuffer, buffer->pvBuffer, buffer->cbBuffer);
954 return SEC_E_INTERNAL_ERROR;
956 if (!ntlm_read_message_header(s, &message->header, MESSAGE_TYPE_AUTHENTICATE))
959 if (!ntlm_read_message_fields(
960 s, &(message->LmChallengeResponse)))
963 if (!ntlm_read_message_fields(
964 s, &(message->NtChallengeResponse)))
967 if (!ntlm_read_message_fields(s, &(message->DomainName)))
970 if (!ntlm_read_message_fields(s, &(message->UserName)))
973 if (!ntlm_read_message_fields(s, &(message->Workstation)))
976 if (!ntlm_read_message_fields(
978 &(message->EncryptedRandomSessionKey)))
981 if (!ntlm_read_negotiate_flags(s, &message->NegotiateFlags, 0,
"NTLM_AUTHENTICATE_MESSAGE"))
984 context->NegotiateKeyExchange =
985 (message->NegotiateFlags & NTLMSSP_NEGOTIATE_KEY_EXCH) ? TRUE : FALSE;
987 if ((context->NegotiateKeyExchange && !message->EncryptedRandomSessionKey.Len) ||
988 (!context->NegotiateKeyExchange && message->EncryptedRandomSessionKey.Len))
991 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION)
993 if (!ntlm_read_version_info(s, &(message->Version)))
997 PayloadBufferOffset = Stream_GetPosition(s);
999 status = SEC_E_INTERNAL_ERROR;
1000 if (!ntlm_read_message_fields_buffer(s, &(message->DomainName)))
1003 if (!ntlm_read_message_fields_buffer(s, &(message->UserName)))
1006 if (!ntlm_read_message_fields_buffer(s, &(message->Workstation)))
1009 if (!ntlm_read_message_fields_buffer(s,
1010 &(message->LmChallengeResponse)))
1013 if (!ntlm_read_message_fields_buffer(s,
1014 &(message->NtChallengeResponse)))
1017 if (message->NtChallengeResponse.Len > 0)
1019 size_t cbAvFlags = 0;
1021 wStream* snt = Stream_StaticConstInit(&ssbuffer, message->NtChallengeResponse.Buffer,
1022 message->NtChallengeResponse.Len);
1027 status = SEC_E_INVALID_TOKEN;
1028 if (!ntlm_read_ntlm_v2_response(snt, &(context->NTLMv2Response)))
1030 status = SEC_E_INTERNAL_ERROR;
1032 context->NtChallengeResponse.pvBuffer = message->NtChallengeResponse.Buffer;
1033 context->NtChallengeResponse.cbBuffer = message->NtChallengeResponse.Len;
1034 sspi_SecBufferFree(&(context->ChallengeTargetInfo));
1035 context->ChallengeTargetInfo.pvBuffer = (
void*)context->NTLMv2Response.Challenge.AvPairs;
1036 context->ChallengeTargetInfo.cbBuffer = message->NtChallengeResponse.Len - (28 + 16);
1037 CopyMemory(context->ClientChallenge, context->NTLMv2Response.Challenge.ClientChallenge, 8);
1039 ntlm_av_pair_get(context->NTLMv2Response.Challenge.AvPairs,
1040 context->NTLMv2Response.Challenge.cbAvPairs, MsvAvFlags, &cbAvFlags);
1043 Data_Read_UINT32(ntlm_av_pair_get_value_pointer(AvFlags), flags);
1046 if (!ntlm_read_message_fields_buffer(
1047 s, &(message->EncryptedRandomSessionKey)))
1050 if (message->EncryptedRandomSessionKey.Len > 0)
1052 if (message->EncryptedRandomSessionKey.Len != 16)
1055 CopyMemory(context->EncryptedRandomSessionKey, message->EncryptedRandomSessionKey.Buffer,
1059 length = Stream_GetPosition(s);
1060 WINPR_ASSERT(length <= UINT32_MAX);
1062 if (!sspi_SecBufferAlloc(&context->AuthenticateMessage, (ULONG)length))
1065 CopyMemory(context->AuthenticateMessage.pvBuffer, Stream_Buffer(s), length);
1066 buffer->cbBuffer = (ULONG)length;
1067 Stream_SetPosition(s, PayloadBufferOffset);
1069 if (flags & MSV_AV_FLAGS_MESSAGE_INTEGRITY_CHECK)
1071 status = SEC_E_INVALID_TOKEN;
1072 if (!ntlm_read_message_integrity_check(
1073 s, &context->MessageIntegrityCheckOffset, message->MessageIntegrityCheck,
1074 sizeof(message->MessageIntegrityCheck),
"NTLM_AUTHENTICATE_MESSAGE"))
1078 status = SEC_E_INTERNAL_ERROR;
1080 #if defined(WITH_DEBUG_NTLM)
1081 ntlm_print_authenticate_message(&context->AuthenticateMessage, message, flags, NULL);
1084 if (message->UserName.Len > 0)
1086 credentials->identity.User = (UINT16*)malloc(message->UserName.Len);
1088 if (!credentials->identity.User)
1091 CopyMemory(credentials->identity.User, message->UserName.Buffer, message->UserName.Len);
1092 credentials->identity.UserLength = message->UserName.Len / 2;
1095 if (message->DomainName.Len > 0)
1097 credentials->identity.Domain = (UINT16*)malloc(message->DomainName.Len);
1099 if (!credentials->identity.Domain)
1102 CopyMemory(credentials->identity.Domain, message->DomainName.Buffer,
1103 message->DomainName.Len);
1104 credentials->identity.DomainLength = message->DomainName.Len / 2;
1107 if (context->NegotiateFlags & NTLMSSP_NEGOTIATE_LM_KEY)
1109 const SECURITY_STATUS rc = ntlm_compute_lm_v2_response(context);
1114 const SECURITY_STATUS rc = ntlm_compute_ntlm_v2_response(context);
1119 ntlm_generate_key_exchange_key(context);
1121 ntlm_decrypt_random_session_key(context);
1123 ntlm_generate_exported_session_key(context);
1125 if (flags & MSV_AV_FLAGS_MESSAGE_INTEGRITY_CHECK)
1127 BYTE messageIntegrityCheck[16] = { 0 };
1129 ntlm_compute_message_integrity_check(context, messageIntegrityCheck,
1130 sizeof(messageIntegrityCheck));
1132 &((PBYTE)context->AuthenticateMessage.pvBuffer)[context->MessageIntegrityCheckOffset],
1133 message->MessageIntegrityCheck,
sizeof(message->MessageIntegrityCheck));
1135 if (memcmp(messageIntegrityCheck, message->MessageIntegrityCheck,
1136 sizeof(message->MessageIntegrityCheck)) != 0)
1138 WLog_ERR(TAG,
"Message Integrity Check (MIC) verification failed!");
1139 #ifdef WITH_DEBUG_NTLM
1140 WLog_ERR(TAG,
"Expected MIC:");
1141 winpr_HexDump(TAG, WLOG_ERROR, messageIntegrityCheck,
sizeof(messageIntegrityCheck));
1142 WLog_ERR(TAG,
"Actual MIC:");
1143 winpr_HexDump(TAG, WLOG_ERROR, message->MessageIntegrityCheck,
1144 sizeof(message->MessageIntegrityCheck));
1146 return SEC_E_MESSAGE_ALTERED;
1161 #ifdef WITH_DEBUG_NTLM
1162 WLog_VRB(TAG,
"No MIC present, using NtProofString for verification.");
1165 if (memcmp(context->NTLMv2Response.Response, context->NtProofString, 16) != 0)
1167 WLog_ERR(TAG,
"NtProofString verification failed!");
1168 #ifdef WITH_DEBUG_NTLM
1169 WLog_ERR(TAG,
"Expected NtProofString:");
1170 winpr_HexDump(TAG, WLOG_ERROR, context->NtProofString,
sizeof(context->NtProofString));
1171 WLog_ERR(TAG,
"Actual NtProofString:");
1172 winpr_HexDump(TAG, WLOG_ERROR, context->NTLMv2Response.Response,
1173 sizeof(context->NTLMv2Response));
1175 return SEC_E_LOGON_DENIED;
1180 if (!ntlm_generate_client_signing_key(context))
1181 return SEC_E_INTERNAL_ERROR;
1182 if (!ntlm_generate_server_signing_key(context))
1183 return SEC_E_INTERNAL_ERROR;
1185 if (!ntlm_generate_client_sealing_key(context))
1186 return SEC_E_INTERNAL_ERROR;
1187 if (!ntlm_generate_server_sealing_key(context))
1188 return SEC_E_INTERNAL_ERROR;
1190 if (!ntlm_init_rc4_seal_states(context))
1191 return SEC_E_INTERNAL_ERROR;
1192 #if defined(WITH_DEBUG_NTLM)
1193 ntlm_print_authentication_complete(context);
1195 ntlm_change_state(context, NTLM_STATE_FINAL);
1196 ntlm_free_message_fields_buffer(&(message->DomainName));
1197 ntlm_free_message_fields_buffer(&(message->UserName));
1198 ntlm_free_message_fields_buffer(&(message->Workstation));
1199 ntlm_free_message_fields_buffer(&(message->LmChallengeResponse));
1200 ntlm_free_message_fields_buffer(&(message->NtChallengeResponse));
1201 ntlm_free_message_fields_buffer(&(message->EncryptedRandomSessionKey));
1220 UINT32 PayloadBufferOffset = 0;
1225 WINPR_ASSERT(context);
1226 WINPR_ASSERT(buffer);
1228 credentials = context->credentials;
1229 WINPR_ASSERT(credentials);
1231 message = &context->AUTHENTICATE_MESSAGE;
1232 WINPR_ASSERT(message);
1236 s = Stream_StaticInit(&sbuffer, buffer->pvBuffer, buffer->cbBuffer);
1239 return SEC_E_INTERNAL_ERROR;
1241 if (context->NTLMv2)
1243 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_56;
1245 if (context->SendVersionInfo)
1246 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_VERSION;
1249 if (context->UseMIC)
1250 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_TARGET_INFO;
1252 if (context->SendWorkstationName)
1253 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
1255 if (context->confidentiality)
1256 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_SEAL;
1258 if (context->CHALLENGE_MESSAGE.NegotiateFlags & NTLMSSP_NEGOTIATE_KEY_EXCH)
1259 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
1261 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_128;
1262 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_EXTENDED_SESSION_SECURITY;
1263 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
1264 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_NTLM;
1265 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_SIGN;
1266 message->NegotiateFlags |= NTLMSSP_REQUEST_TARGET;
1267 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_UNICODE;
1269 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION)
1270 ntlm_get_version_info(&(message->Version));
1272 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED)
1274 message->Workstation.Len = context->Workstation.Length;
1275 message->Workstation.Buffer = (BYTE*)context->Workstation.Buffer;
1278 if (credentials->identity.DomainLength > 0)
1280 message->NegotiateFlags |= NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED;
1281 message->DomainName.Len = (UINT16)credentials->identity.DomainLength * 2;
1282 message->DomainName.Buffer = (BYTE*)credentials->identity.Domain;
1285 message->UserName.Len = (UINT16)credentials->identity.UserLength * 2;
1286 message->UserName.Buffer = (BYTE*)credentials->identity.User;
1287 message->LmChallengeResponse.Len = (UINT16)context->LmChallengeResponse.cbBuffer;
1288 message->LmChallengeResponse.Buffer = (BYTE*)context->LmChallengeResponse.pvBuffer;
1289 message->NtChallengeResponse.Len = (UINT16)context->NtChallengeResponse.cbBuffer;
1290 message->NtChallengeResponse.Buffer = (BYTE*)context->NtChallengeResponse.pvBuffer;
1292 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_KEY_EXCH)
1294 message->EncryptedRandomSessionKey.Len = 16;
1295 message->EncryptedRandomSessionKey.Buffer = context->EncryptedRandomSessionKey;
1298 PayloadBufferOffset = 64;
1300 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION)
1301 PayloadBufferOffset += 8;
1303 if (context->UseMIC)
1304 PayloadBufferOffset += 16;
1306 message->DomainName.BufferOffset = PayloadBufferOffset;
1307 message->UserName.BufferOffset = message->DomainName.BufferOffset + message->DomainName.Len;
1308 message->Workstation.BufferOffset = message->UserName.BufferOffset + message->UserName.Len;
1309 message->LmChallengeResponse.BufferOffset =
1310 message->Workstation.BufferOffset + message->Workstation.Len;
1311 message->NtChallengeResponse.BufferOffset =
1312 message->LmChallengeResponse.BufferOffset + message->LmChallengeResponse.Len;
1313 message->EncryptedRandomSessionKey.BufferOffset =
1314 message->NtChallengeResponse.BufferOffset + message->NtChallengeResponse.Len;
1315 if (!ntlm_populate_message_header(&message->header, MESSAGE_TYPE_AUTHENTICATE))
1316 return SEC_E_INVALID_TOKEN;
1317 if (!ntlm_write_message_header(s, &message->header))
1318 return SEC_E_INTERNAL_ERROR;
1319 if (!ntlm_write_message_fields(
1320 s, &(message->LmChallengeResponse)))
1321 return SEC_E_INTERNAL_ERROR;
1322 if (!ntlm_write_message_fields(
1323 s, &(message->NtChallengeResponse)))
1324 return SEC_E_INTERNAL_ERROR;
1325 if (!ntlm_write_message_fields(s, &(message->DomainName)))
1326 return SEC_E_INTERNAL_ERROR;
1327 if (!ntlm_write_message_fields(s, &(message->UserName)))
1328 return SEC_E_INTERNAL_ERROR;
1329 if (!ntlm_write_message_fields(s, &(message->Workstation)))
1330 return SEC_E_INTERNAL_ERROR;
1331 if (!ntlm_write_message_fields(
1333 &(message->EncryptedRandomSessionKey)))
1334 return SEC_E_INTERNAL_ERROR;
1335 if (!ntlm_write_negotiate_flags(s, message->NegotiateFlags,
"NTLM_AUTHENTICATE_MESSAGE"))
1336 return SEC_E_INTERNAL_ERROR;
1338 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION)
1340 if (!ntlm_write_version_info(s, &(message->Version)))
1341 return SEC_E_INTERNAL_ERROR;
1344 if (context->UseMIC)
1346 const BYTE data[WINPR_MD5_DIGEST_LENGTH] = { 0 };
1348 context->MessageIntegrityCheckOffset = Stream_GetPosition(s);
1349 if (!ntlm_write_message_integrity_check(s, Stream_GetPosition(s), data,
sizeof(data),
1350 "NTLM_AUTHENTICATE_MESSAGE"))
1351 return SEC_E_INTERNAL_ERROR;
1354 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED)
1356 if (!ntlm_write_message_fields_buffer(s, &(message->DomainName)))
1357 return SEC_E_INTERNAL_ERROR;
1360 if (!ntlm_write_message_fields_buffer(s, &(message->UserName)))
1361 return SEC_E_INTERNAL_ERROR;
1363 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED)
1365 if (!ntlm_write_message_fields_buffer(s, &(message->Workstation)))
1366 return SEC_E_INTERNAL_ERROR;
1369 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_LM_KEY)
1371 if (!ntlm_write_message_fields_buffer(
1372 s, &(message->LmChallengeResponse)))
1373 return SEC_E_INTERNAL_ERROR;
1375 if (!ntlm_write_message_fields_buffer(
1376 s, &(message->NtChallengeResponse)))
1377 return SEC_E_INTERNAL_ERROR;
1379 if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_KEY_EXCH)
1381 if (!ntlm_write_message_fields_buffer(
1382 s, &(message->EncryptedRandomSessionKey)))
1383 return SEC_E_INTERNAL_ERROR;
1386 length = Stream_GetPosition(s);
1387 WINPR_ASSERT(length <= UINT32_MAX);
1389 if (!sspi_SecBufferAlloc(&context->AuthenticateMessage, (ULONG)length))
1390 return SEC_E_INTERNAL_ERROR;
1392 CopyMemory(context->AuthenticateMessage.pvBuffer, Stream_Buffer(s), length);
1393 buffer->cbBuffer = (ULONG)length;
1395 if (context->UseMIC)
1398 ntlm_compute_message_integrity_check(context, message->MessageIntegrityCheck,
1399 sizeof(message->MessageIntegrityCheck));
1400 if (!ntlm_write_message_integrity_check(
1401 s, context->MessageIntegrityCheckOffset, message->MessageIntegrityCheck,
1402 sizeof(message->MessageIntegrityCheck),
"NTLM_AUTHENTICATE_MESSAGE"))
1403 return SEC_E_INTERNAL_ERROR;
1406 #if defined(WITH_DEBUG_NTLM)
1407 ntlm_print_authenticate_message(&context->AuthenticateMessage, message,
1408 context->UseMIC ? MSV_AV_FLAGS_MESSAGE_INTEGRITY_CHECK : 0,
1409 &context->AuthenticateTargetInfo);
1411 ntlm_change_state(context, NTLM_STATE_FINAL);