diff --git a/winpr/libwinpr/sspi/NTLM/ntlm.c b/winpr/libwinpr/sspi/NTLM/ntlm.c index 6246e4fce..b0c638803 100644 --- a/winpr/libwinpr/sspi/NTLM/ntlm.c +++ b/winpr/libwinpr/sspi/NTLM/ntlm.c @@ -49,6 +49,8 @@ static int ntlm_SetContextWorkstation(NTLM_CONTEXT* context, char* Workstation) DWORD nSize = 0; CHAR* computerName; + WINPR_ASSERT(context); + if (!Workstation) { if (GetComputerNameExA(ComputerNameNetBIOS, NULL, &nSize) || @@ -91,6 +93,8 @@ static int ntlm_SetContextWorkstation(NTLM_CONTEXT* context, char* Workstation) static int ntlm_SetContextServicePrincipalNameW(NTLM_CONTEXT* context, LPWSTR ServicePrincipalName) { + WINPR_ASSERT(context); + if (!ServicePrincipalName) { context->ServicePrincipalName.Buffer = NULL; @@ -116,6 +120,8 @@ static int ntlm_SetContextTargetName(NTLM_CONTEXT* context, char* TargetName) DWORD nSize = 0; CHAR* computerName = NULL; + WINPR_ASSERT(context); + if (!name) { if (GetComputerNameExA(ComputerNameNetBIOS, NULL, &nSize) || @@ -254,7 +260,7 @@ static NTLM_CONTEXT* ntlm_ContextNew(void) context->NegotiateFlags = 0; context->LmCompatibilityLevel = 3; - context->state = NTLM_STATE_INITIAL; + ntlm_change_state(context, NTLM_STATE_INITIAL); FillMemory(context->MachineID, sizeof(context->MachineID), 0xAA); if (context->NTLMv2) @@ -414,79 +420,87 @@ ntlm_AcceptSecurityContext(PCredHandle phCredential, PCtxtHandle phContext, PSec sspi_SecureHandleSetUpperPointer(phNewContext, (void*)NTLM_PACKAGE_NAME); } - if (context->state == NTLM_STATE_INITIAL) + switch (ntlm_get_state(context)) { - context->state = NTLM_STATE_NEGOTIATE; - - if (!pInput) - return SEC_E_INVALID_TOKEN; - - if (pInput->cBuffers < 1) - return SEC_E_INVALID_TOKEN; - - input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN); - - if (!input_buffer) - return SEC_E_INVALID_TOKEN; - - if (input_buffer->cbBuffer < 1) - return SEC_E_INVALID_TOKEN; - - status = ntlm_read_NegotiateMessage(context, input_buffer); - - if (context->state == NTLM_STATE_CHALLENGE) + case NTLM_STATE_INITIAL: { - if (!pOutput) + ntlm_change_state(context, NTLM_STATE_NEGOTIATE); + + if (!pInput) return SEC_E_INVALID_TOKEN; - if (pOutput->cBuffers < 1) + if (pInput->cBuffers < 1) return SEC_E_INVALID_TOKEN; - output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN); + input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN); - if (!output_buffer->BufferType) + if (!input_buffer) return SEC_E_INVALID_TOKEN; - if (output_buffer->cbBuffer < 1) - return SEC_E_INSUFFICIENT_MEMORY; + if (input_buffer->cbBuffer < 1) + return SEC_E_INVALID_TOKEN; - return ntlm_write_ChallengeMessage(context, output_buffer); - } + status = ntlm_read_NegotiateMessage(context, input_buffer); + if (status != SEC_I_CONTINUE_NEEDED) + return status; - return SEC_E_OUT_OF_SEQUENCE; - } - else if (context->state == NTLM_STATE_AUTHENTICATE) - { - if (!pInput) - return SEC_E_INVALID_TOKEN; - - if (pInput->cBuffers < 1) - return SEC_E_INVALID_TOKEN; - - input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN); - - if (!input_buffer) - return SEC_E_INVALID_TOKEN; - - if (input_buffer->cbBuffer < 1) - return SEC_E_INVALID_TOKEN; - - status = ntlm_read_AuthenticateMessage(context, input_buffer); - - if (pOutput) - { - ULONG i; - - for (i = 0; i < pOutput->cBuffers; i++) + if (ntlm_get_state(context) == NTLM_STATE_CHALLENGE) { - pOutput->pBuffers[i].cbBuffer = 0; - pOutput->pBuffers[i].BufferType = SECBUFFER_TOKEN; + if (!pOutput) + return SEC_E_INVALID_TOKEN; + + if (pOutput->cBuffers < 1) + return SEC_E_INVALID_TOKEN; + + output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN); + + if (!output_buffer->BufferType) + return SEC_E_INVALID_TOKEN; + + if (output_buffer->cbBuffer < 1) + return SEC_E_INSUFFICIENT_MEMORY; + + return ntlm_write_ChallengeMessage(context, output_buffer); } + + return SEC_E_OUT_OF_SEQUENCE; } + break; + case NTLM_STATE_AUTHENTICATE: + { + if (!pInput) + return SEC_E_INVALID_TOKEN; - return status; + if (pInput->cBuffers < 1) + return SEC_E_INVALID_TOKEN; + + input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN); + + if (!input_buffer) + return SEC_E_INVALID_TOKEN; + + if (input_buffer->cbBuffer < 1) + return SEC_E_INVALID_TOKEN; + + status = ntlm_read_AuthenticateMessage(context, input_buffer); + + if (pOutput) + { + ULONG i; + + for (i = 0; i < pOutput->cBuffers; i++) + { + pOutput->pBuffers[i].cbBuffer = 0; + pOutput->pBuffers[i].BufferType = SECBUFFER_TOKEN; + } + } + + return status; + } + break; + default: + break; } - return SEC_E_OUT_OF_SEQUENCE; } @@ -540,7 +554,7 @@ static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW( sspi_SecureHandleSetUpperPointer(phNewContext, (void*)NTLM_PACKAGE_NAME); } - if ((!pInput) || (context->state == NTLM_STATE_AUTHENTICATE)) + if ((!pInput) || (ntlm_get_state(context) == NTLM_STATE_AUTHENTICATE)) { if (!pOutput) return SEC_E_INVALID_TOKEN; @@ -556,10 +570,10 @@ static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW( if (output_buffer->cbBuffer < 1) return SEC_E_INVALID_TOKEN; - if (context->state == NTLM_STATE_INITIAL) - context->state = NTLM_STATE_NEGOTIATE; + if (ntlm_get_state(context) == NTLM_STATE_INITIAL) + ntlm_change_state(context, NTLM_STATE_NEGOTIATE); - if (context->state == NTLM_STATE_NEGOTIATE) + if (ntlm_get_state(context) == NTLM_STATE_NEGOTIATE) return ntlm_write_NegotiateMessage(context, output_buffer); return SEC_E_OUT_OF_SEQUENCE; @@ -585,7 +599,7 @@ static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW( context->Bindings.Bindings = (SEC_CHANNEL_BINDINGS*)channel_bindings->pvBuffer; } - if (context->state == NTLM_STATE_CHALLENGE) + if (ntlm_get_state(context) == NTLM_STATE_CHALLENGE) { status = ntlm_read_ChallengeMessage(context, input_buffer); @@ -606,7 +620,7 @@ static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW( if (output_buffer->cbBuffer < 1) return SEC_E_INSUFFICIENT_MEMORY; - if (context->state == NTLM_STATE_AUTHENTICATE) + if (ntlm_get_state(context) == NTLM_STATE_AUTHENTICATE) return ntlm_write_AuthenticateMessage(context, output_buffer); } @@ -675,7 +689,12 @@ static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContex SECURITY_STATUS ntlm_computeProofValue(NTLM_CONTEXT* ntlm, SecBuffer* ntproof) { BYTE* blob; - SecBuffer* target = &ntlm->ChallengeTargetInfo; + SecBuffer* target; + + WINPR_ASSERT(ntlm); + WINPR_ASSERT(ntproof); + + target = &ntlm->ChallengeTargetInfo; if (!sspi_SecBufferAlloc(ntproof, 36 + target->cbBuffer)) return SEC_E_INSUFFICIENT_MEMORY; @@ -696,8 +715,13 @@ SECURITY_STATUS ntlm_computeProofValue(NTLM_CONTEXT* ntlm, SecBuffer* ntproof) SECURITY_STATUS ntlm_computeMicValue(NTLM_CONTEXT* ntlm, SecBuffer* micvalue) { BYTE* blob; - ULONG msgSize = ntlm->NegotiateMessage.cbBuffer + ntlm->ChallengeMessage.cbBuffer + - ntlm->AuthenticateMessage.cbBuffer; + ULONG msgSize; + + WINPR_ASSERT(ntlm); + WINPR_ASSERT(micvalue); + + msgSize = ntlm->NegotiateMessage.cbBuffer + ntlm->ChallengeMessage.cbBuffer + + ntlm->AuthenticateMessage.cbBuffer; if (!sspi_SecBufferAlloc(micvalue, msgSize)) return SEC_E_INSUFFICIENT_MEMORY; @@ -975,8 +999,8 @@ static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext, ULON void* data; UINT32 SeqNo; UINT32 value; - BYTE digest[WINPR_MD5_DIGEST_LENGTH]; - BYTE checksum[8]; + BYTE digest[WINPR_MD5_DIGEST_LENGTH] = { 0 }; + BYTE checksum[8] = { 0 }; BYTE* signature; ULONG version = 1; WINPR_HMAC_CTX* hmac; @@ -1072,12 +1096,12 @@ static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext, PSec void* data; UINT32 SeqNo; UINT32 value; - BYTE digest[WINPR_MD5_DIGEST_LENGTH]; - BYTE checksum[8]; + BYTE digest[WINPR_MD5_DIGEST_LENGTH] = { 0 }; + BYTE checksum[8] = { 0 }; UINT32 version = 1; WINPR_HMAC_CTX* hmac; NTLM_CONTEXT* context; - BYTE expected_signature[WINPR_MD5_DIGEST_LENGTH]; + BYTE expected_signature[WINPR_MD5_DIGEST_LENGTH] = { 0 }; PSecBuffer data_buffer = NULL; PSecBuffer signature_buffer = NULL; SeqNo = (UINT32)MessageSeqNo; @@ -1264,3 +1288,81 @@ const SecPkgInfoW NTLM_SecPkgInfoW = { NTLM_SecPkgInfoW_Name, /* Name */ NTLM_SecPkgInfoW_Comment /* Comment */ }; + +char* ntlm_negotiate_flags_string(char* buffer, size_t size, UINT32 flags) +{ + int x; + if (!buffer || (size == 0)) + return buffer; + + for (x = 0; x < 31; x++) + { + size_t len = strnlen(buffer, size); + if (flags & x) + { + const char* str = ntlm_get_negotiate_string(1 << x); + size_t flen = strlen(str); + if (len > 0) + { + if (size - len < 1) + break; + strcat(buffer, "|"); + len++; + } + + if (size - len < flen) + break; + strcat(buffer, str); + } + } + return buffer; +} + +const char* ntlm_message_type_string(UINT32 messageType) +{ + switch (messageType) + { + case MESSAGE_TYPE_NEGOTIATE: + return "MESSAGE_TYPE_NEGOTIATE"; + case MESSAGE_TYPE_CHALLENGE: + return "MESSAGE_TYPE_CHALLENGE"; + case MESSAGE_TYPE_AUTHENTICATE: + return "MESSAGE_TYPE_AUTHENTICATE"; + default: + return "MESSAGE_TYPE_UNKNOWN"; + } +} + +const char* ntlm_state_string(NTLM_STATE state) +{ + switch (state) + { + case NTLM_STATE_INITIAL: + return "NTLM_STATE_INITIAL"; + case NTLM_STATE_NEGOTIATE: + return "NTLM_STATE_NEGOTIATE"; + case NTLM_STATE_CHALLENGE: + return "NTLM_STATE_CHALLENGE"; + case NTLM_STATE_AUTHENTICATE: + return "NTLM_STATE_AUTHENTICATE"; + case NTLM_STATE_COMPLETION: + return "NTLM_STATE_COMPLETION"; + case NTLM_STATE_FINAL: + return "NTLM_STATE_FINAL"; + default: + return "NTLM_STATE_UNKNOWN"; + } +} +void ntlm_change_state(NTLM_CONTEXT* ntlm, NTLM_STATE state) +{ + WINPR_ASSERT(ntlm); + WLog_DBG(TAG, "change state from %s to %s", ntlm_state_string(ntlm->state), + ntlm_state_string(state)); + ntlm->state = state; +} + +NTLM_STATE ntlm_get_state(NTLM_CONTEXT* ntlm) +{ + WINPR_ASSERT(ntlm); + return ntlm->state; +} diff --git a/winpr/libwinpr/sspi/NTLM/ntlm.h b/winpr/libwinpr/sspi/NTLM/ntlm.h index 746517153..53d81b443 100644 --- a/winpr/libwinpr/sspi/NTLM/ntlm.h +++ b/winpr/libwinpr/sspi/NTLM/ntlm.h @@ -186,8 +186,7 @@ typedef struct typedef struct { - BYTE Signature[8]; - UINT32 MessageType; + NTLM_MESSAGE_HEADER header; UINT32 NegotiateFlags; NTLM_VERSION_INFO Version; NTLM_MESSAGE_FIELDS DomainName; @@ -196,8 +195,7 @@ typedef struct typedef struct { - BYTE Signature[8]; - UINT32 MessageType; + NTLM_MESSAGE_HEADER header; UINT32 NegotiateFlags; BYTE ServerChallenge[8]; BYTE Reserved[8]; @@ -208,8 +206,7 @@ typedef struct typedef struct { - BYTE Signature[8]; - UINT32 MessageType; + NTLM_MESSAGE_HEADER header; UINT32 NegotiateFlags; NTLM_VERSION_INFO Version; NTLM_MESSAGE_FIELDS DomainName; @@ -258,7 +255,7 @@ typedef struct NTLM_NEGOTIATE_MESSAGE NEGOTIATE_MESSAGE; NTLM_CHALLENGE_MESSAGE CHALLENGE_MESSAGE; NTLM_AUTHENTICATE_MESSAGE AUTHENTICATE_MESSAGE; - UINT32 MessageIntegrityCheckOffset; + size_t MessageIntegrityCheckOffset; SecBuffer NegotiateMessage; SecBuffer ChallengeMessage; SecBuffer AuthenticateMessage; @@ -286,6 +283,13 @@ typedef struct void* HashCallbackArg; } NTLM_CONTEXT; +char* ntlm_negotiate_flags_string(char* buffer, size_t size, UINT32 flags); +const char* ntlm_message_type_string(UINT32 messageType); + +const char* ntlm_state_string(NTLM_STATE state); +void ntlm_change_state(NTLM_CONTEXT* ntlm, NTLM_STATE state); +NTLM_STATE ntlm_get_state(NTLM_CONTEXT* ntlm); + SECURITY_STATUS ntlm_computeProofValue(NTLM_CONTEXT* ntlm, SecBuffer* ntproof); SECURITY_STATUS ntlm_computeMicValue(NTLM_CONTEXT* ntlm, SecBuffer* micvalue); diff --git a/winpr/libwinpr/sspi/NTLM/ntlm_av_pairs.c b/winpr/libwinpr/sspi/NTLM/ntlm_av_pairs.c index 61d54ee3c..bf7755f6b 100644 --- a/winpr/libwinpr/sspi/NTLM/ntlm_av_pairs.c +++ b/winpr/libwinpr/sspi/NTLM/ntlm_av_pairs.c @@ -25,6 +25,7 @@ #include "../sspi.h" #include +#include #include #include #include @@ -85,11 +86,13 @@ static NTLM_AV_PAIR* ntlm_av_pair_next(NTLM_AV_PAIR* pAvPairList, size_t* pcbAvP static INLINE void ntlm_av_pair_set_id(NTLM_AV_PAIR* pAvPair, UINT16 id) { + WINPR_ASSERT(pAvPair); Data_Write_UINT16(&pAvPair->AvId, id); } static INLINE void ntlm_av_pair_set_len(NTLM_AV_PAIR* pAvPair, UINT16 len) { + WINPR_ASSERT(pAvPair); Data_Write_UINT16(&pAvPair->AvLen, len); } @@ -183,6 +186,7 @@ static ULONG ntlm_av_pair_list_size(ULONG AvPairsCount, ULONG AvPairsValueLength PBYTE ntlm_av_pair_get_value_pointer(NTLM_AV_PAIR* pAvPair) { + WINPR_ASSERT(pAvPair); return (PBYTE)pAvPair + sizeof(NTLM_AV_PAIR); } @@ -301,6 +305,8 @@ static int ntlm_get_target_computer_name(PUNICODE_STRING pName, COMPUTER_NAME_FO DWORD nSize = 0; CHAR* computerName; + WINPR_ASSERT(pName); + if (GetComputerNameExA(ComputerNameNetBIOS, NULL, &nSize) || GetLastError() != ERROR_MORE_DATA) return -1; @@ -398,6 +404,9 @@ static void ntlm_compute_channel_bindings(NTLM_CONTEXT* context) BYTE* ChannelBindingToken; UINT32 ChannelBindingTokenLength; SEC_CHANNEL_BINDINGS* ChannelBindings; + + WINPR_ASSERT(context); + ZeroMemory(context->ChannelBindingsHash, WINPR_MD5_DIGEST_LENGTH); ChannelBindings = context->Bindings.Bindings; @@ -440,6 +449,7 @@ out: static void ntlm_compute_single_host_data(NTLM_CONTEXT* context) { + WINPR_ASSERT(context); /** * The Single_Host_Data structure allows a client to send machine-specific information * within an authentication exchange to services on the same machine. The client can @@ -455,9 +465,9 @@ static void ntlm_compute_single_host_data(NTLM_CONTEXT* context) FillMemory(context->SingleHostData.MachineID, 32, 0xAA); } -int ntlm_construct_challenge_target_info(NTLM_CONTEXT* context) +BOOL ntlm_construct_challenge_target_info(NTLM_CONTEXT* context) { - int rc = -1; + BOOL rc = FALSE; ULONG length; ULONG AvPairsCount; ULONG AvPairsLength; @@ -468,6 +478,8 @@ int ntlm_construct_challenge_target_info(NTLM_CONTEXT* context) UNICODE_STRING DnsDomainName = { 0 }; UNICODE_STRING DnsComputerName = { 0 }; + WINPR_ASSERT(context); + if (ntlm_get_target_computer_name(&NbDomainName, ComputerNameNetBIOS) < 0) goto fail; @@ -520,7 +532,7 @@ int ntlm_construct_challenge_target_info(NTLM_CONTEXT* context) sizeof(context->Timestamp))) goto fail; - rc = 1; + rc = TRUE; fail: ntlm_free_unicode_string(&NbDomainName); ntlm_free_unicode_string(&NbComputerName); @@ -529,7 +541,7 @@ fail: return rc; } -int ntlm_construct_authenticate_target_info(NTLM_CONTEXT* context) +BOOL ntlm_construct_authenticate_target_info(NTLM_CONTEXT* context) { ULONG size; ULONG AvPairsCount; @@ -550,6 +562,9 @@ int ntlm_construct_authenticate_target_info(NTLM_CONTEXT* context) size_t cbAvDnsTreeName; size_t cbChallengeTargetInfo; size_t cbAuthenticateTargetInfo; + + WINPR_ASSERT(context); + AvPairsCount = 1; AvPairsValueLength = 0; ChallengeTargetInfo = (NTLM_AV_PAIR*)context->ChallengeTargetInfo.pvBuffer; @@ -751,8 +766,8 @@ int ntlm_construct_authenticate_target_info(NTLM_CONTEXT* context) ZeroMemory(AvEOL, sizeof(NTLM_AV_PAIR)); } - return 1; + return TRUE; fail: sspi_SecBufferFree(&context->AuthenticateTargetInfo); - return -1; + return FALSE; } diff --git a/winpr/libwinpr/sspi/NTLM/ntlm_av_pairs.h b/winpr/libwinpr/sspi/NTLM/ntlm_av_pairs.h index 9b6cb9930..ab9da4396 100644 --- a/winpr/libwinpr/sspi/NTLM/ntlm_av_pairs.h +++ b/winpr/libwinpr/sspi/NTLM/ntlm_av_pairs.h @@ -36,7 +36,7 @@ PBYTE ntlm_av_pair_get_value_pointer(NTLM_AV_PAIR* pAvPair); NTLM_AV_PAIR* ntlm_av_pair_get(NTLM_AV_PAIR* pAvPairList, size_t cbAvPairList, NTLM_AV_ID AvId, size_t* pcbAvPairListRemaining); -int ntlm_construct_challenge_target_info(NTLM_CONTEXT* context); -int ntlm_construct_authenticate_target_info(NTLM_CONTEXT* context); +BOOL ntlm_construct_challenge_target_info(NTLM_CONTEXT* context); +BOOL ntlm_construct_authenticate_target_info(NTLM_CONTEXT* context); #endif /* WINPR_SSPI_NTLM_AV_PAIRS_H */ diff --git a/winpr/libwinpr/sspi/NTLM/ntlm_compute.c b/winpr/libwinpr/sspi/NTLM/ntlm_compute.c index 2d284fb9c..301309a3b 100644 --- a/winpr/libwinpr/sspi/NTLM/ntlm_compute.c +++ b/winpr/libwinpr/sspi/NTLM/ntlm_compute.c @@ -50,16 +50,21 @@ static const BYTE NTLM_NULL_BUFFER[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0 * @param s */ -void ntlm_get_version_info(NTLM_VERSION_INFO* versionInfo) +BOOL ntlm_get_version_info(NTLM_VERSION_INFO* versionInfo) { - OSVERSIONINFOA osVersionInfo; + OSVERSIONINFOA osVersionInfo = { 0 }; + + WINPR_ASSERT(versionInfo); + osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA); - GetVersionExA(&osVersionInfo); + if (!GetVersionExA(&osVersionInfo)) + return FALSE; versionInfo->ProductMajorVersion = (UINT8)osVersionInfo.dwMajorVersion; versionInfo->ProductMinorVersion = (UINT8)osVersionInfo.dwMinorVersion; versionInfo->ProductBuild = (UINT16)osVersionInfo.dwBuildNumber; ZeroMemory(versionInfo->Reserved, sizeof(versionInfo->Reserved)); versionInfo->NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3; + return TRUE; } /** @@ -68,17 +73,24 @@ void ntlm_get_version_info(NTLM_VERSION_INFO* versionInfo) * @param s */ -int ntlm_read_version_info(wStream* s, NTLM_VERSION_INFO* versionInfo) +BOOL ntlm_read_version_info(wStream* s, NTLM_VERSION_INFO* versionInfo) { + WINPR_ASSERT(s); + WINPR_ASSERT(versionInfo); + if (Stream_GetRemainingLength(s) < 8) - return -1; + { + WLog_ERR(TAG, "NTLM_VERSION_INFO short header %" PRIuz ", expected %" PRIuz, + Stream_GetRemainingLength(s), 8); + return FALSE; + } Stream_Read_UINT8(s, versionInfo->ProductMajorVersion); /* ProductMajorVersion (1 byte) */ Stream_Read_UINT8(s, versionInfo->ProductMinorVersion); /* ProductMinorVersion (1 byte) */ Stream_Read_UINT16(s, versionInfo->ProductBuild); /* ProductBuild (2 bytes) */ Stream_Read(s, versionInfo->Reserved, sizeof(versionInfo->Reserved)); /* Reserved (3 bytes) */ Stream_Read_UINT8(s, versionInfo->NTLMRevisionCurrent); /* NTLMRevisionCurrent (1 byte) */ - return 1; + return TRUE; } /** @@ -87,13 +99,24 @@ int ntlm_read_version_info(wStream* s, NTLM_VERSION_INFO* versionInfo) * @param s */ -void ntlm_write_version_info(wStream* s, NTLM_VERSION_INFO* versionInfo) +BOOL ntlm_write_version_info(wStream* s, const NTLM_VERSION_INFO* versionInfo) { + WINPR_ASSERT(s); + WINPR_ASSERT(versionInfo); + + if (Stream_GetRemainingCapacity(s) < 5 + sizeof(versionInfo->Reserved)) + { + WLog_ERR(TAG, "NTLM_VERSION_INFO short header %" PRIuz ", expected %" PRIuz, + Stream_GetRemainingCapacity(s), 5 + sizeof(versionInfo->Reserved)); + return FALSE; + } + Stream_Write_UINT8(s, versionInfo->ProductMajorVersion); /* ProductMajorVersion (1 byte) */ Stream_Write_UINT8(s, versionInfo->ProductMinorVersion); /* ProductMinorVersion (1 byte) */ Stream_Write_UINT16(s, versionInfo->ProductBuild); /* ProductBuild (2 bytes) */ Stream_Write(s, versionInfo->Reserved, sizeof(versionInfo->Reserved)); /* Reserved (3 bytes) */ Stream_Write_UINT8(s, versionInfo->NTLMRevisionCurrent); /* NTLMRevisionCurrent (1 byte) */ + return TRUE; } /** @@ -102,8 +125,10 @@ void ntlm_write_version_info(wStream* s, NTLM_VERSION_INFO* versionInfo) * @param s */ #ifdef WITH_DEBUG_NTLM -void ntlm_print_version_info(NTLM_VERSION_INFO* versionInfo) +void ntlm_print_version_info(const NTLM_VERSION_INFO* versionInfo) { + WINPR_ASSERT(versionInfo); + WLog_VRB(TAG, "VERSION ={"); WLog_VRB(TAG, "\tProductMajorVersion: %" PRIu8 "", versionInfo->ProductMajorVersion); WLog_VRB(TAG, "\tProductMinorVersion: %" PRIu8 "", versionInfo->ProductMinorVersion); @@ -114,11 +139,18 @@ void ntlm_print_version_info(NTLM_VERSION_INFO* versionInfo) } #endif -static int ntlm_read_ntlm_v2_client_challenge(wStream* s, NTLMv2_CLIENT_CHALLENGE* challenge) +static BOOL ntlm_read_ntlm_v2_client_challenge(wStream* s, NTLMv2_CLIENT_CHALLENGE* challenge) { size_t size; + WINPR_ASSERT(s); + WINPR_ASSERT(challenge); + if (Stream_GetRemainingLength(s) < 28) - return -1; + { + WLog_ERR(TAG, "NTLMv2_CLIENT_CHALLENGE expected 28bytes, got %" PRIuz "bytes", + Stream_GetRemainingLength(s)); + return FALSE; + } Stream_Read_UINT8(s, challenge->RespType); Stream_Read_UINT8(s, challenge->HiRespType); @@ -130,21 +162,39 @@ static int ntlm_read_ntlm_v2_client_challenge(wStream* s, NTLMv2_CLIENT_CHALLENG size = Stream_Length(s) - Stream_GetPosition(s); if (size > UINT32_MAX) - return -1; + { + WLog_ERR(TAG, "NTLMv2_CLIENT_CHALLENGE::cbAvPairs too large, got %" PRIuz "bytes", size); + return FALSE; + } challenge->cbAvPairs = (UINT32)size; challenge->AvPairs = (NTLM_AV_PAIR*)malloc(challenge->cbAvPairs); if (!challenge->AvPairs) - return -1; + { + WLog_ERR(TAG, "NTLMv2_CLIENT_CHALLENGE::AvPairs failed to allocate %" PRIu32 "bytes", + challenge->cbAvPairs); + return FALSE; + } Stream_Read(s, challenge->AvPairs, size); - return 1; + return TRUE; } -static int ntlm_write_ntlm_v2_client_challenge(wStream* s, NTLMv2_CLIENT_CHALLENGE* challenge) +static BOOL ntlm_write_ntlm_v2_client_challenge(wStream* s, + const NTLMv2_CLIENT_CHALLENGE* challenge) { ULONG length; + + WINPR_ASSERT(s); + WINPR_ASSERT(challenge); + + if (Stream_GetRemainingCapacity(s) < 28) + { + WLog_ERR(TAG, "NTLMv2_CLIENT_CHALLENGE expected 28bytes, have %" PRIuz "bytes", + Stream_GetRemainingCapacity(s)); + return FALSE; + } Stream_Write_UINT8(s, challenge->RespType); Stream_Write_UINT8(s, challenge->HiRespType); Stream_Write_UINT16(s, challenge->Reserved1); @@ -153,20 +203,44 @@ static int ntlm_write_ntlm_v2_client_challenge(wStream* s, NTLMv2_CLIENT_CHALLEN Stream_Write(s, challenge->ClientChallenge, 8); Stream_Write_UINT32(s, challenge->Reserved3); length = ntlm_av_pair_list_length(challenge->AvPairs, challenge->cbAvPairs); + if (Stream_GetRemainingCapacity(s) < length) + { + WLog_ERR(TAG, + "NTLMv2_CLIENT_CHALLENGE::AvPairs expected %" PRIu32 "bytes, have %" PRIuz "bytes", + length, Stream_GetRemainingCapacity(s)); + return FALSE; + } Stream_Write(s, challenge->AvPairs, length); - return 1; + return TRUE; } -int ntlm_read_ntlm_v2_response(wStream* s, NTLMv2_RESPONSE* response) +BOOL ntlm_read_ntlm_v2_response(wStream* s, NTLMv2_RESPONSE* response) { + WINPR_ASSERT(s); + WINPR_ASSERT(response); + if (Stream_GetRemainingLength(s) < 16) - return -1; + { + WLog_ERR(TAG, "NTLMv2_RESPONSE expected 16bytes, have %" PRIuz "bytes", + Stream_GetRemainingLength(s)); + + return FALSE; + } Stream_Read(s, response->Response, 16); return ntlm_read_ntlm_v2_client_challenge(s, &(response->Challenge)); } -int ntlm_write_ntlm_v2_response(wStream* s, NTLMv2_RESPONSE* response) +BOOL ntlm_write_ntlm_v2_response(wStream* s, const NTLMv2_RESPONSE* response) { + WINPR_ASSERT(s); + WINPR_ASSERT(response); + + if (Stream_GetRemainingCapacity(s) < 16) + { + WLog_ERR(TAG, "NTLMv2_RESPONSE expected 16bytes, have %" PRIuz "bytes", + Stream_GetRemainingCapacity(s)); + return FALSE; + } Stream_Write(s, response->Response, 16); return ntlm_write_ntlm_v2_client_challenge(s, &(response->Challenge)); } @@ -178,8 +252,11 @@ int ntlm_write_ntlm_v2_response(wStream* s, NTLMv2_RESPONSE* response) void ntlm_current_time(BYTE* timestamp) { - FILETIME filetime; - ULARGE_INTEGER time64; + FILETIME filetime = { 0 }; + ULARGE_INTEGER time64 = { 0 }; + + WINPR_ASSERT(timestamp); + GetSystemTimeAsFileTime(&filetime); time64.u.LowPart = filetime.dwLowDateTime; time64.u.HighPart = filetime.dwHighDateTime; @@ -193,6 +270,8 @@ void ntlm_current_time(BYTE* timestamp) void ntlm_generate_timestamp(NTLM_CONTEXT* context) { + WINPR_ASSERT(context); + if (memcmp(context->ChallengeTimestamp, NTLM_NULL_BUFFER, 8) != 0) CopyMemory(context->Timestamp, context->ChallengeTimestamp, 8); else @@ -203,7 +282,12 @@ static int ntlm_fetch_ntlm_v2_hash(NTLM_CONTEXT* context, BYTE* hash) { WINPR_SAM* sam; WINPR_SAM_ENTRY* entry; - SSPI_CREDENTIALS* credentials = context->credentials; + SSPI_CREDENTIALS* credentials; + + WINPR_ASSERT(context); + WINPR_ASSERT(hash); + + credentials = context->credentials; sam = SamOpen(context->SamFile, TRUE); if (!sam) @@ -257,7 +341,12 @@ static int ntlm_convert_password_hash(NTLM_CONTEXT* context, BYTE* hash) int i; char* PasswordHash = NULL; INT64 PasswordHashLength = 0; - SSPI_CREDENTIALS* credentials = context->credentials; + SSPI_CREDENTIALS* credentials; + + WINPR_ASSERT(context); + WINPR_ASSERT(hash); + + credentials = context->credentials; /* Password contains a password hash of length (PasswordLength - * SSPI_CREDENTIALS_HASH_LENGTH_OFFSET) */ PasswordHashLength = credentials->identity.PasswordLength - SSPI_CREDENTIALS_HASH_LENGTH_OFFSET; @@ -284,9 +373,14 @@ static int ntlm_convert_password_hash(NTLM_CONTEXT* context, BYTE* hash) return 1; } -static int ntlm_compute_ntlm_v2_hash(NTLM_CONTEXT* context, BYTE* hash) +static BOOL ntlm_compute_ntlm_v2_hash(NTLM_CONTEXT* context, BYTE* hash) { - SSPI_CREDENTIALS* credentials = context->credentials; + SSPI_CREDENTIALS* credentials; + + WINPR_ASSERT(context); + WINPR_ASSERT(hash); + + credentials = context->credentials; #ifdef WITH_DEBUG_NTLM if (credentials) @@ -311,10 +405,10 @@ static int ntlm_compute_ntlm_v2_hash(NTLM_CONTEXT* context, BYTE* hash) #endif if (memcmp(context->NtlmV2Hash, NTLM_NULL_BUFFER, 16) != 0) - return 1; + return TRUE; if (!credentials) - return -1; + return FALSE; else if (memcmp(context->NtlmHash, NTLM_NULL_BUFFER, 16) != 0) { NTOWFv2FromHashW(context->NtlmHash, (LPWSTR)credentials->identity.User, @@ -325,7 +419,7 @@ static int ntlm_compute_ntlm_v2_hash(NTLM_CONTEXT* context, BYTE* hash) { /* Special case for WinPR: password hash */ if (ntlm_convert_password_hash(context, context->NtlmHash) < 0) - return -1; + return FALSE; NTOWFv2FromHashW(context->NtlmHash, (LPWSTR)credentials->identity.User, credentials->identity.UserLength * 2, (LPWSTR)credentials->identity.Domain, @@ -344,55 +438,57 @@ static int ntlm_compute_ntlm_v2_hash(NTLM_CONTEXT* context, BYTE* hash) SecBuffer proofValue, micValue; if (ntlm_computeProofValue(context, &proofValue) != SEC_E_OK) - return -1; + return FALSE; if (ntlm_computeMicValue(context, &micValue) != SEC_E_OK) { sspi_SecBufferFree(&proofValue); - return -1; + return FALSE; } ret = context->HashCallback(context->HashCallbackArg, &credentials->identity, &proofValue, context->EncryptedRandomSessionKey, - (&context->AUTHENTICATE_MESSAGE)->MessageIntegrityCheck, - &micValue, hash); + context->AUTHENTICATE_MESSAGE.MessageIntegrityCheck, &micValue, + hash); sspi_SecBufferFree(&proofValue); sspi_SecBufferFree(&micValue); - return ret ? 1 : -1; + return ret ? TRUE : FALSE; } else if (context->UseSamFileDatabase) { return ntlm_fetch_ntlm_v2_hash(context, hash); } - return 1; + return TRUE; } -int ntlm_compute_lm_v2_response(NTLM_CONTEXT* context) +BOOL ntlm_compute_lm_v2_response(NTLM_CONTEXT* context) { BYTE* response; - BYTE value[WINPR_MD5_DIGEST_LENGTH]; + BYTE value[WINPR_MD5_DIGEST_LENGTH] = { 0 }; + + WINPR_ASSERT(context); if (context->LmCompatibilityLevel < 2) { if (!sspi_SecBufferAlloc(&context->LmChallengeResponse, 24)) - return -1; + return FALSE; ZeroMemory(context->LmChallengeResponse.pvBuffer, 24); - return 1; + return TRUE; } /* Compute the NTLMv2 hash */ - if (ntlm_compute_ntlm_v2_hash(context, context->NtlmV2Hash) < 0) - return -1; + if (!ntlm_compute_ntlm_v2_hash(context, context->NtlmV2Hash)) + return FALSE; /* Concatenate the server and client challenges */ CopyMemory(value, context->ServerChallenge, 8); CopyMemory(&value[8], context->ClientChallenge, 8); if (!sspi_SecBufferAlloc(&context->LmChallengeResponse, 24)) - return -1; + return FALSE; response = (BYTE*)context->LmChallengeResponse.pvBuffer; /* Compute the HMAC-MD5 hash of the resulting value using the NTLMv2 hash as the key */ @@ -401,7 +497,7 @@ int ntlm_compute_lm_v2_response(NTLM_CONTEXT* context) /* Concatenate the resulting HMAC-MD5 hash and the client challenge, giving us the LMv2 response * (24 bytes) */ CopyMemory(&response[16], context->ClientChallenge, 8); - return 1; + return TRUE; } /** @@ -411,13 +507,17 @@ int ntlm_compute_lm_v2_response(NTLM_CONTEXT* context) * @param NTLM context */ -int ntlm_compute_ntlm_v2_response(NTLM_CONTEXT* context) +BOOL ntlm_compute_ntlm_v2_response(NTLM_CONTEXT* context) { BYTE* blob; SecBuffer ntlm_v2_temp = { 0 }; SecBuffer ntlm_v2_temp_chal = { 0 }; - PSecBuffer TargetInfo = &context->ChallengeTargetInfo; - int ret = -1; + PSecBuffer TargetInfo; + + WINPR_ASSERT(context); + + TargetInfo = &context->ChallengeTargetInfo; + BOOL ret = FALSE; if (!sspi_SecBufferAlloc(&ntlm_v2_temp, TargetInfo->cbBuffer + 28)) goto exit; @@ -426,7 +526,7 @@ int ntlm_compute_ntlm_v2_response(NTLM_CONTEXT* context) blob = (BYTE*)ntlm_v2_temp.pvBuffer; /* Compute the NTLMv2 hash */ - if (ntlm_compute_ntlm_v2_hash(context, (BYTE*)context->NtlmV2Hash) < 0) + if (!ntlm_compute_ntlm_v2_hash(context, (BYTE*)context->NtlmV2Hash)) goto exit; /* Construct temp */ @@ -467,7 +567,7 @@ int ntlm_compute_ntlm_v2_response(NTLM_CONTEXT* context) winpr_HMAC(WINPR_MD_MD5, (BYTE*)context->NtlmV2Hash, WINPR_MD5_DIGEST_LENGTH, context->NtProofString, WINPR_MD5_DIGEST_LENGTH, context->SessionBaseKey, WINPR_MD5_DIGEST_LENGTH); - ret = 1; + ret = TRUE; exit: sspi_SecBufferFree(&ntlm_v2_temp); sspi_SecBufferFree(&ntlm_v2_temp_chal); @@ -500,9 +600,11 @@ void ntlm_rc4k(BYTE* key, size_t length, BYTE* plaintext, BYTE* ciphertext) void ntlm_generate_client_challenge(NTLM_CONTEXT* context) { + WINPR_ASSERT(context); + /* ClientChallenge is used in computation of LMv2 and NTLMv2 responses */ - if (memcmp(context->ClientChallenge, NTLM_NULL_BUFFER, 8) == 0) - winpr_RAND(context->ClientChallenge, 8); + if (memcmp(context->ClientChallenge, NTLM_NULL_BUFFER, sizeof(context->ClientChallenge)) == 0) + winpr_RAND(context->ClientChallenge, sizeof(context->ClientChallenge)); } /** @@ -512,8 +614,10 @@ void ntlm_generate_client_challenge(NTLM_CONTEXT* context) void ntlm_generate_server_challenge(NTLM_CONTEXT* context) { - if (memcmp(context->ServerChallenge, NTLM_NULL_BUFFER, 8) == 0) - winpr_RAND(context->ServerChallenge, 8); + WINPR_ASSERT(context); + + if (memcmp(context->ServerChallenge, NTLM_NULL_BUFFER, sizeof(context->ServerChallenge)) == 0) + winpr_RAND(context->ServerChallenge, sizeof(context->ServerChallenge)); } /** @@ -524,8 +628,11 @@ void ntlm_generate_server_challenge(NTLM_CONTEXT* context) void ntlm_generate_key_exchange_key(NTLM_CONTEXT* context) { + WINPR_ASSERT(context); + WINPR_ASSERT(sizeof(context->KeyExchangeKey) == sizeof(context->SessionBaseKey)); + /* In NTLMv2, KeyExchangeKey is the 128-bit SessionBaseKey */ - CopyMemory(context->KeyExchangeKey, context->SessionBaseKey, 16); + CopyMemory(context->KeyExchangeKey, context->SessionBaseKey, sizeof(context->KeyExchangeKey)); } /** @@ -535,7 +642,8 @@ void ntlm_generate_key_exchange_key(NTLM_CONTEXT* context) void ntlm_generate_random_session_key(NTLM_CONTEXT* context) { - winpr_RAND(context->RandomSessionKey, 16); + WINPR_ASSERT(context); + winpr_RAND(context->RandomSessionKey, sizeof(context->RandomSessionKey)); } /** @@ -545,7 +653,11 @@ void ntlm_generate_random_session_key(NTLM_CONTEXT* context) void ntlm_generate_exported_session_key(NTLM_CONTEXT* context) { - CopyMemory(context->ExportedSessionKey, context->RandomSessionKey, 16); + WINPR_ASSERT(context); + WINPR_ASSERT(sizeof(context->ExportedSessionKey) == sizeof(context->RandomSessionKey)); + + CopyMemory(context->ExportedSessionKey, context->RandomSessionKey, + sizeof(context->ExportedSessionKey)); } /** @@ -557,6 +669,7 @@ void ntlm_encrypt_random_session_key(NTLM_CONTEXT* context) { /* In NTLMv2, EncryptedRandomSessionKey is the ExportedSessionKey RC4-encrypted with the * KeyExchangeKey */ + WINPR_ASSERT(context); ntlm_rc4k(context->KeyExchangeKey, 16, context->RandomSessionKey, context->EncryptedRandomSessionKey); } @@ -568,6 +681,8 @@ void ntlm_encrypt_random_session_key(NTLM_CONTEXT* context) void ntlm_decrypt_random_session_key(NTLM_CONTEXT* context) { + WINPR_ASSERT(context); + /* In NTLMv2, EncryptedRandomSessionKey is the ExportedSessionKey RC4-encrypted with the * KeyExchangeKey */ @@ -577,10 +692,18 @@ void ntlm_decrypt_random_session_key(NTLM_CONTEXT* context) * AUTHENTICATE_MESSAGE.EncryptedRandomSessionKey) else Set RandomSessionKey to KeyExchangeKey */ if (context->NegotiateKeyExchange) - ntlm_rc4k(context->KeyExchangeKey, 16, context->EncryptedRandomSessionKey, - context->RandomSessionKey); + { + WINPR_ASSERT(sizeof(context->EncryptedRandomSessionKey) == + sizeof(context->RandomSessionKey)); + ntlm_rc4k(context->KeyExchangeKey, sizeof(context->EncryptedRandomSessionKey), + context->EncryptedRandomSessionKey, context->RandomSessionKey); + } else - CopyMemory(context->RandomSessionKey, context->KeyExchangeKey, 16); + { + WINPR_ASSERT(sizeof(context->RandomSessionKey) == sizeof(context->KeyExchangeKey)); + CopyMemory(context->RandomSessionKey, context->KeyExchangeKey, + sizeof(context->RandomSessionKey)); + } } /** @@ -591,29 +714,32 @@ void ntlm_decrypt_random_session_key(NTLM_CONTEXT* context) * @param signing_key Destination signing key */ -static int ntlm_generate_signing_key(BYTE* exported_session_key, const SecBuffer* sign_magic, - BYTE* signing_key) +static BOOL ntlm_generate_signing_key(BYTE* exported_session_key, const SecBuffer* sign_magic, + BYTE* signing_key) { + BOOL rc = FALSE; size_t length; - BYTE* value; + BYTE* value = NULL; + + WINPR_ASSERT(exported_session_key); + WINPR_ASSERT(sign_magic); + WINPR_ASSERT(signing_key); + length = WINPR_MD5_DIGEST_LENGTH + sign_magic->cbBuffer; value = (BYTE*)malloc(length); if (!value) - return -1; + goto out; /* Concatenate ExportedSessionKey with sign magic */ CopyMemory(value, exported_session_key, WINPR_MD5_DIGEST_LENGTH); CopyMemory(&value[WINPR_MD5_DIGEST_LENGTH], sign_magic->pvBuffer, sign_magic->cbBuffer); - if (!winpr_Digest(WINPR_MD_MD5, value, length, signing_key, WINPR_MD5_DIGEST_LENGTH)) - { - free(value); - return -1; - } + rc = winpr_Digest(WINPR_MD_MD5, value, length, signing_key, WINPR_MD5_DIGEST_LENGTH); +out: free(value); - return 1; + return rc; } /** @@ -622,10 +748,13 @@ static int ntlm_generate_signing_key(BYTE* exported_session_key, const SecBuffer * @param NTLM context */ -void ntlm_generate_client_signing_key(NTLM_CONTEXT* context) +BOOL ntlm_generate_client_signing_key(NTLM_CONTEXT* context) { const SecBuffer signMagic = { sizeof(NTLM_CLIENT_SIGN_MAGIC), 0, NTLM_CLIENT_SIGN_MAGIC }; - ntlm_generate_signing_key(context->ExportedSessionKey, &signMagic, context->ClientSigningKey); + + WINPR_ASSERT(context); + return ntlm_generate_signing_key(context->ExportedSessionKey, &signMagic, + context->ClientSigningKey); } /** @@ -634,43 +763,13 @@ void ntlm_generate_client_signing_key(NTLM_CONTEXT* context) * @param NTLM context */ -void ntlm_generate_server_signing_key(NTLM_CONTEXT* context) +BOOL ntlm_generate_server_signing_key(NTLM_CONTEXT* context) { const SecBuffer signMagic = { sizeof(NTLM_SERVER_SIGN_MAGIC), 0, NTLM_SERVER_SIGN_MAGIC }; - ntlm_generate_signing_key(context->ExportedSessionKey, &signMagic, context->ServerSigningKey); -} -/** - * Generate sealing key. - * @msdn{cc236712} - * @param exported_session_key ExportedSessionKey - * @param seal_magic Seal magic string - * @param sealing_key Destination sealing key - */ - -static int ntlm_generate_sealing_key(BYTE* exported_session_key, PSecBuffer seal_magic, - BYTE* sealing_key) -{ - BYTE* p; - SecBuffer buffer; - - if (!sspi_SecBufferAlloc(&buffer, WINPR_MD5_DIGEST_LENGTH + seal_magic->cbBuffer)) - return -1; - - p = (BYTE*)buffer.pvBuffer; - /* Concatenate ExportedSessionKey with seal magic */ - CopyMemory(p, exported_session_key, WINPR_MD5_DIGEST_LENGTH); - CopyMemory(&p[WINPR_MD5_DIGEST_LENGTH], seal_magic->pvBuffer, seal_magic->cbBuffer); - - if (!winpr_Digest(WINPR_MD_MD5, buffer.pvBuffer, buffer.cbBuffer, sealing_key, - WINPR_MD5_DIGEST_LENGTH)) - { - sspi_SecBufferFree(&buffer); - return -1; - } - - sspi_SecBufferFree(&buffer); - return 1; + WINPR_ASSERT(context); + return ntlm_generate_signing_key(context->ExportedSessionKey, &signMagic, + context->ServerSigningKey); } /** @@ -679,10 +778,13 @@ static int ntlm_generate_sealing_key(BYTE* exported_session_key, PSecBuffer seal * @param NTLM context */ -void ntlm_generate_client_sealing_key(NTLM_CONTEXT* context) +BOOL ntlm_generate_client_sealing_key(NTLM_CONTEXT* context) { const SecBuffer sealMagic = { sizeof(NTLM_CLIENT_SEAL_MAGIC), 0, NTLM_CLIENT_SEAL_MAGIC }; - ntlm_generate_signing_key(context->ExportedSessionKey, &sealMagic, context->ClientSealingKey); + + WINPR_ASSERT(context); + return ntlm_generate_signing_key(context->ExportedSessionKey, &sealMagic, + context->ClientSealingKey); } /** @@ -691,10 +793,13 @@ void ntlm_generate_client_sealing_key(NTLM_CONTEXT* context) * @param NTLM context */ -void ntlm_generate_server_sealing_key(NTLM_CONTEXT* context) +BOOL ntlm_generate_server_sealing_key(NTLM_CONTEXT* context) { const SecBuffer sealMagic = { sizeof(NTLM_SERVER_SEAL_MAGIC), 0, NTLM_SERVER_SEAL_MAGIC }; - ntlm_generate_signing_key(context->ExportedSessionKey, &sealMagic, context->ServerSealingKey); + + WINPR_ASSERT(context); + return ntlm_generate_signing_key(context->ExportedSessionKey, &sealMagic, + context->ServerSealingKey); } /** @@ -704,14 +809,17 @@ void ntlm_generate_server_sealing_key(NTLM_CONTEXT* context) void ntlm_init_rc4_seal_states(NTLM_CONTEXT* context) { + WINPR_ASSERT(context); if (context->server) { context->SendSigningKey = context->ServerSigningKey; context->RecvSigningKey = context->ClientSigningKey; context->SendSealingKey = context->ClientSealingKey; context->RecvSealingKey = context->ServerSealingKey; - context->SendRc4Seal = winpr_RC4_New(context->ServerSealingKey, 16); - context->RecvRc4Seal = winpr_RC4_New(context->ClientSealingKey, 16); + context->SendRc4Seal = + winpr_RC4_New(context->ServerSealingKey, sizeof(context->ServerSealingKey)); + context->RecvRc4Seal = + winpr_RC4_New(context->ClientSealingKey, sizeof(context->ClientSealingKey)); } else { @@ -719,22 +827,29 @@ void ntlm_init_rc4_seal_states(NTLM_CONTEXT* context) context->RecvSigningKey = context->ServerSigningKey; context->SendSealingKey = context->ServerSealingKey; context->RecvSealingKey = context->ClientSealingKey; - context->SendRc4Seal = winpr_RC4_New(context->ClientSealingKey, 16); - context->RecvRc4Seal = winpr_RC4_New(context->ServerSealingKey, 16); + context->SendRc4Seal = + winpr_RC4_New(context->ClientSealingKey, sizeof(context->ClientSealingKey)); + context->RecvRc4Seal = + winpr_RC4_New(context->ServerSealingKey, sizeof(context->ServerSealingKey)); } } -void ntlm_compute_message_integrity_check(NTLM_CONTEXT* context, BYTE* mic, UINT32 size) +BOOL ntlm_compute_message_integrity_check(NTLM_CONTEXT* context, BYTE* mic, UINT32 size) { + BOOL rc = FALSE; /* * Compute the HMAC-MD5 hash of ConcatenationOf(NEGOTIATE_MESSAGE, * CHALLENGE_MESSAGE, AUTHENTICATE_MESSAGE) using the ExportedSessionKey */ WINPR_HMAC_CTX* hmac = winpr_HMAC_New(); + + WINPR_ASSERT(context); + WINPR_ASSERT(mic); WINPR_ASSERT(size >= WINPR_MD5_DIGEST_LENGTH); + memset(mic, 0, size); if (!hmac) - return; + return FALSE; if (winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->ExportedSessionKey, WINPR_MD5_DIGEST_LENGTH)) { @@ -742,10 +857,27 @@ void ntlm_compute_message_integrity_check(NTLM_CONTEXT* context, BYTE* mic, UINT context->NegotiateMessage.cbBuffer); winpr_HMAC_Update(hmac, (BYTE*)context->ChallengeMessage.pvBuffer, context->ChallengeMessage.cbBuffer); - winpr_HMAC_Update(hmac, (BYTE*)context->AuthenticateMessage.pvBuffer, - context->AuthenticateMessage.cbBuffer); + + if (context->MessageIntegrityCheckOffset > 0) + { + const BYTE* auth = (BYTE*)context->AuthenticateMessage.pvBuffer; + const BYTE data[WINPR_MD5_DIGEST_LENGTH] = { 0 }; + const size_t rest = context->MessageIntegrityCheckOffset + sizeof(data); + + WINPR_ASSERT(rest <= context->AuthenticateMessage.cbBuffer); + winpr_HMAC_Update(hmac, &auth[0], context->MessageIntegrityCheckOffset); + winpr_HMAC_Update(hmac, data, sizeof(data)); + winpr_HMAC_Update(hmac, &auth[rest], context->AuthenticateMessage.cbBuffer - rest); + } + else + { + winpr_HMAC_Update(hmac, (BYTE*)context->AuthenticateMessage.pvBuffer, + context->AuthenticateMessage.cbBuffer); + } winpr_HMAC_Final(hmac, mic, WINPR_MD5_DIGEST_LENGTH); + rc = TRUE; } winpr_HMAC_Free(hmac); + return rc; } diff --git a/winpr/libwinpr/sspi/NTLM/ntlm_compute.h b/winpr/libwinpr/sspi/NTLM/ntlm_compute.h index 6bcbaa9ee..a5572b504 100644 --- a/winpr/libwinpr/sspi/NTLM/ntlm_compute.h +++ b/winpr/libwinpr/sspi/NTLM/ntlm_compute.h @@ -24,16 +24,16 @@ #include "ntlm_av_pairs.h" -void ntlm_get_version_info(NTLM_VERSION_INFO* versionInfo); -int ntlm_read_version_info(wStream* s, NTLM_VERSION_INFO* versionInfo); -void ntlm_write_version_info(wStream* s, NTLM_VERSION_INFO* versionInfo); +BOOL ntlm_get_version_info(NTLM_VERSION_INFO* versionInfo); +BOOL ntlm_read_version_info(wStream* s, NTLM_VERSION_INFO* versionInfo); +BOOL ntlm_write_version_info(wStream* s, const NTLM_VERSION_INFO* versionInfo); #ifdef WITH_DEBUG_NTLM -void ntlm_print_version_info(NTLM_VERSION_INFO* versionInfo); +void ntlm_print_version_info(const NTLM_VERSION_INFO* versionInfo); #endif -int ntlm_read_ntlm_v2_response(wStream* s, NTLMv2_RESPONSE* response); -int ntlm_write_ntlm_v2_response(wStream* s, NTLMv2_RESPONSE* response); +BOOL ntlm_read_ntlm_v2_response(wStream* s, NTLMv2_RESPONSE* response); +BOOL ntlm_write_ntlm_v2_response(wStream* s, const NTLMv2_RESPONSE* response); void ntlm_output_target_name(NTLM_CONTEXT* context); void ntlm_output_channel_bindings(NTLM_CONTEXT* context); @@ -41,8 +41,8 @@ void ntlm_output_channel_bindings(NTLM_CONTEXT* context); void ntlm_current_time(BYTE* timestamp); void ntlm_generate_timestamp(NTLM_CONTEXT* context); -int ntlm_compute_lm_v2_response(NTLM_CONTEXT* context); -int ntlm_compute_ntlm_v2_response(NTLM_CONTEXT* context); +BOOL ntlm_compute_lm_v2_response(NTLM_CONTEXT* context); +BOOL ntlm_compute_ntlm_v2_response(NTLM_CONTEXT* context); void ntlm_rc4k(BYTE* key, size_t length, BYTE* plaintext, BYTE* ciphertext); void ntlm_generate_client_challenge(NTLM_CONTEXT* context); @@ -53,12 +53,12 @@ void ntlm_generate_exported_session_key(NTLM_CONTEXT* context); void ntlm_encrypt_random_session_key(NTLM_CONTEXT* context); void ntlm_decrypt_random_session_key(NTLM_CONTEXT* context); -void ntlm_generate_client_signing_key(NTLM_CONTEXT* context); -void ntlm_generate_server_signing_key(NTLM_CONTEXT* context); -void ntlm_generate_client_sealing_key(NTLM_CONTEXT* context); -void ntlm_generate_server_sealing_key(NTLM_CONTEXT* context); +BOOL ntlm_generate_client_signing_key(NTLM_CONTEXT* context); +BOOL ntlm_generate_server_signing_key(NTLM_CONTEXT* context); +BOOL ntlm_generate_client_sealing_key(NTLM_CONTEXT* context); +BOOL ntlm_generate_server_sealing_key(NTLM_CONTEXT* context); void ntlm_init_rc4_seal_states(NTLM_CONTEXT* context); -void ntlm_compute_message_integrity_check(NTLM_CONTEXT* context, BYTE* mic, UINT32 size); +BOOL ntlm_compute_message_integrity_check(NTLM_CONTEXT* context, BYTE* mic, UINT32 size); #endif /* WINPR_AUTH_NTLM_COMPUTE_H */ diff --git a/winpr/libwinpr/sspi/NTLM/ntlm_message.c b/winpr/libwinpr/sspi/NTLM/ntlm_message.c index 20aa657c8..981343528 100644 --- a/winpr/libwinpr/sspi/NTLM/ntlm_message.c +++ b/winpr/libwinpr/sspi/NTLM/ntlm_message.c @@ -23,6 +23,7 @@ #include "../sspi.h" #include +#include #include #include #include @@ -36,135 +37,364 @@ static const char NTLM_SIGNATURE[8] = { 'N', 'T', 'L', 'M', 'S', 'S', 'P', '\0' }; -#ifdef WITH_DEBUG_NTLM -static const char* const NTLM_NEGOTIATE_STRINGS[] = { "NTLMSSP_NEGOTIATE_56", - "NTLMSSP_NEGOTIATE_KEY_EXCH", - "NTLMSSP_NEGOTIATE_128", - "NTLMSSP_RESERVED1", - "NTLMSSP_RESERVED2", - "NTLMSSP_RESERVED3", - "NTLMSSP_NEGOTIATE_VERSION", - "NTLMSSP_RESERVED4", - "NTLMSSP_NEGOTIATE_TARGET_INFO", - "NTLMSSP_REQUEST_NON_NT_SESSION_KEY", - "NTLMSSP_RESERVED5", - "NTLMSSP_NEGOTIATE_IDENTIFY", - "NTLMSSP_NEGOTIATE_EXTENDED_SESSION_SECURITY", - "NTLMSSP_RESERVED6", - "NTLMSSP_TARGET_TYPE_SERVER", - "NTLMSSP_TARGET_TYPE_DOMAIN", - "NTLMSSP_NEGOTIATE_ALWAYS_SIGN", - "NTLMSSP_RESERVED7", - "NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED", - "NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED", - "NTLMSSP_NEGOTIATE_ANONYMOUS", - "NTLMSSP_RESERVED8", - "NTLMSSP_NEGOTIATE_NTLM", - "NTLMSSP_RESERVED9", - "NTLMSSP_NEGOTIATE_LM_KEY", - "NTLMSSP_NEGOTIATE_DATAGRAM", - "NTLMSSP_NEGOTIATE_SEAL", - "NTLMSSP_NEGOTIATE_SIGN", - "NTLMSSP_RESERVED10", - "NTLMSSP_REQUEST_TARGET", - "NTLMSSP_NEGOTIATE_OEM", - "NTLMSSP_NEGOTIATE_UNICODE" }; +const char* ntlm_get_negotiate_string(UINT32 flag) +{ + if (flag & NTLMSSP_NEGOTIATE_56) + return "NTLMSSP_NEGOTIATE_56"; + if (flag & NTLMSSP_NEGOTIATE_KEY_EXCH) + return "NTLMSSP_NEGOTIATE_KEY_EXCH"; + if (flag & NTLMSSP_NEGOTIATE_128) + return "NTLMSSP_NEGOTIATE_128"; + if (flag & NTLMSSP_RESERVED1) + return "NTLMSSP_RESERVED1"; + if (flag & NTLMSSP_RESERVED2) + return "NTLMSSP_RESERVED2"; + if (flag & NTLMSSP_RESERVED3) + return "NTLMSSP_RESERVED3"; + if (flag & NTLMSSP_NEGOTIATE_VERSION) + return "NTLMSSP_NEGOTIATE_VERSION"; + if (flag & NTLMSSP_RESERVED4) + return "NTLMSSP_RESERVED4"; + if (flag & NTLMSSP_NEGOTIATE_TARGET_INFO) + return "NTLMSSP_NEGOTIATE_TARGET_INFO"; + if (flag & NTLMSSP_REQUEST_NON_NT_SESSION_KEY) + return "NTLMSSP_REQUEST_NON_NT_SESSION_KEY"; + if (flag & NTLMSSP_RESERVED5) + return "NTLMSSP_RESERVED5"; + if (flag & NTLMSSP_NEGOTIATE_IDENTIFY) + return "NTLMSSP_NEGOTIATE_IDENTIFY"; + if (flag & NTLMSSP_NEGOTIATE_EXTENDED_SESSION_SECURITY) + return "NTLMSSP_NEGOTIATE_EXTENDED_SESSION_SECURITY"; + if (flag & NTLMSSP_RESERVED6) + return "NTLMSSP_RESERVED6"; + if (flag & NTLMSSP_TARGET_TYPE_SERVER) + return "NTLMSSP_TARGET_TYPE_SERVER"; + if (flag & NTLMSSP_TARGET_TYPE_DOMAIN) + return "NTLMSSP_TARGET_TYPE_DOMAIN"; + if (flag & NTLMSSP_NEGOTIATE_ALWAYS_SIGN) + return "NTLMSSP_NEGOTIATE_ALWAYS_SIGN"; + if (flag & NTLMSSP_RESERVED7) + return "NTLMSSP_RESERVED7"; + if (flag & NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED) + return "NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED"; + if (flag & NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED) + return "NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED"; + if (flag & NTLMSSP_NEGOTIATE_ANONYMOUS) + return "NTLMSSP_NEGOTIATE_ANONYMOUS"; + if (flag & NTLMSSP_RESERVED8) + return "NTLMSSP_RESERVED8"; + if (flag & NTLMSSP_NEGOTIATE_NTLM) + return "NTLMSSP_NEGOTIATE_NTLM"; + if (flag & NTLMSSP_RESERVED9) + return "NTLMSSP_RESERVED9"; + if (flag & NTLMSSP_NEGOTIATE_LM_KEY) + return "NTLMSSP_NEGOTIATE_LM_KEY"; + if (flag & NTLMSSP_NEGOTIATE_DATAGRAM) + return "NTLMSSP_NEGOTIATE_DATAGRAM"; + if (flag & NTLMSSP_NEGOTIATE_SEAL) + return "NTLMSSP_NEGOTIATE_SEAL"; + if (flag & NTLMSSP_NEGOTIATE_SIGN) + return "NTLMSSP_NEGOTIATE_SIGN"; + if (flag & NTLMSSP_RESERVED10) + return "NTLMSSP_RESERVED10"; + if (flag & NTLMSSP_REQUEST_TARGET) + return "NTLMSSP_REQUEST_TARGET"; + if (flag & NTLMSSP_NEGOTIATE_OEM) + return "NTLMSSP_NEGOTIATE_OEM"; + if (flag & NTLMSSP_NEGOTIATE_UNICODE) + return "NTLMSSP_NEGOTIATE_UNICODE"; + return "NTLMSSP_NEGOTIATE_UNKNOWN"; +} + +#if defined(WITH_DEBUG_NTLM) +static void ntlm_print_message_fields(const NTLM_MESSAGE_FIELDS* fields, const char* name) +{ + WINPR_ASSERT(fields); + WINPR_ASSERT(name); + + WLog_VRB(TAG, "%s (Len: %" PRIu16 " MaxLen: %" PRIu16 " BufferOffset: %" PRIu32 ")", name, + fields->Len, fields->MaxLen, fields->BufferOffset); + + if (fields->Len > 0) + winpr_HexDump(TAG, WLOG_TRACE, fields->Buffer, fields->Len); +} static void ntlm_print_negotiate_flags(UINT32 flags) { int i; - const char* str; + WLog_VRB(TAG, "negotiateFlags \"0x%08" PRIX32 "\"", flags); for (i = 31; i >= 0; i--) { if ((flags >> i) & 1) { - str = NTLM_NEGOTIATE_STRINGS[(31 - i)]; + const char* str = ntlm_get_negotiate_string(1 << i); WLog_VRB(TAG, "\t%s (%d),", str, (31 - i)); } } } + +static void ntlm_print_negotiate_message(const SecBuffer* NegotiateMessage, + const NTLM_NEGOTIATE_MESSAGE* message) +{ + WINPR_ASSERT(NegotiateMessage); + WINPR_ASSERT(message); + + WLog_VRB(TAG, "NEGOTIATE_MESSAGE (length = %" PRIu32 ")", NegotiateMessage->cbBuffer); + winpr_HexDump(TAG, WLOG_TRACE, NegotiateMessage->pvBuffer, NegotiateMessage->cbBuffer); + ntlm_print_negotiate_flags(message->NegotiateFlags); + + if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) + ntlm_print_version_info(&(message->Version)); +} + +static void ntlm_print_challenge_message(const SecBuffer* ChallengeMessage, + const NTLM_CHALLENGE_MESSAGE* message, + const SecBuffer* ChallengeTargetInfo) +{ + WINPR_ASSERT(ChallengeMessage); + WINPR_ASSERT(message); + + WLog_VRB(TAG, "CHALLENGE_MESSAGE (length = %" PRIu32 ")", ChallengeMessage->cbBuffer); + winpr_HexDump(TAG, WLOG_TRACE, ChallengeMessage->pvBuffer, ChallengeMessage->cbBuffer); + ntlm_print_negotiate_flags(message->NegotiateFlags); + + if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) + ntlm_print_version_info(&(message->Version)); + + ntlm_print_message_fields(&(message->TargetName), "TargetName"); + ntlm_print_message_fields(&(message->TargetInfo), "TargetInfo"); + + if (ChallengeTargetInfo && (ChallengeTargetInfo->cbBuffer > 0)) + { + WLog_VRB(TAG, "ChallengeTargetInfo (%" PRIu32 "):", ChallengeTargetInfo->cbBuffer); + ntlm_print_av_pair_list(ChallengeTargetInfo->pvBuffer, ChallengeTargetInfo->cbBuffer); + } +} + +static void ntlm_print_authenticate_message(const SecBuffer* AuthenticateMessage, + const NTLM_AUTHENTICATE_MESSAGE* message, UINT32 flags, + const SecBuffer* AuthenticateTargetInfo) +{ + WINPR_ASSERT(AuthenticateMessage); + WINPR_ASSERT(message); + + WLog_VRB(TAG, "AUTHENTICATE_MESSAGE (length = %" PRIu32 ")", AuthenticateMessage->cbBuffer); + winpr_HexDump(TAG, WLOG_TRACE, AuthenticateMessage->pvBuffer, AuthenticateMessage->cbBuffer); + ntlm_print_negotiate_flags(message->NegotiateFlags); + + if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) + ntlm_print_version_info(&(message->Version)); + + if (AuthenticateTargetInfo && (AuthenticateTargetInfo->cbBuffer > 0)) + { + WLog_VRB(TAG, "AuthenticateTargetInfo (%" PRIu32 "):", AuthenticateTargetInfo->cbBuffer); + ntlm_print_av_pair_list(AuthenticateTargetInfo->pvBuffer, AuthenticateTargetInfo->cbBuffer); + } + + ntlm_print_message_fields(&(message->DomainName), "DomainName"); + ntlm_print_message_fields(&(message->UserName), "UserName"); + ntlm_print_message_fields(&(message->Workstation), "Workstation"); + ntlm_print_message_fields(&(message->LmChallengeResponse), "LmChallengeResponse"); + ntlm_print_message_fields(&(message->NtChallengeResponse), "NtChallengeResponse"); + ntlm_print_message_fields(&(message->EncryptedRandomSessionKey), "EncryptedRandomSessionKey"); + + if (flags & MSV_AV_FLAGS_MESSAGE_INTEGRITY_CHECK) + { + WLog_VRB(TAG, "MessageIntegrityCheck (length = 16)"); + winpr_HexDump(TAG, WLOG_TRACE, message->MessageIntegrityCheck, + sizeof(message->MessageIntegrityCheck)); + } +} + +static void ntlm_print_authentication_complete(const NTLM_CONTEXT* context) +{ + WINPR_ASSERT(context); + + WLog_VRB(TAG, "ClientChallenge"); + winpr_HexDump(TAG, WLOG_TRACE, context->ClientChallenge, 8); + WLog_VRB(TAG, "ServerChallenge"); + winpr_HexDump(TAG, WLOG_TRACE, context->ServerChallenge, 8); + WLog_VRB(TAG, "SessionBaseKey"); + winpr_HexDump(TAG, WLOG_TRACE, context->SessionBaseKey, 16); + WLog_VRB(TAG, "KeyExchangeKey"); + winpr_HexDump(TAG, WLOG_TRACE, context->KeyExchangeKey, 16); + WLog_VRB(TAG, "ExportedSessionKey"); + winpr_HexDump(TAG, WLOG_TRACE, context->ExportedSessionKey, 16); + WLog_VRB(TAG, "RandomSessionKey"); + winpr_HexDump(TAG, WLOG_TRACE, context->RandomSessionKey, 16); + WLog_VRB(TAG, "ClientSigningKey"); + winpr_HexDump(TAG, WLOG_TRACE, context->ClientSigningKey, 16); + WLog_VRB(TAG, "ClientSealingKey"); + winpr_HexDump(TAG, WLOG_TRACE, context->ClientSealingKey, 16); + WLog_VRB(TAG, "ServerSigningKey"); + winpr_HexDump(TAG, WLOG_TRACE, context->ServerSigningKey, 16); + WLog_VRB(TAG, "ServerSealingKey"); + winpr_HexDump(TAG, WLOG_TRACE, context->ServerSealingKey, 16); + WLog_VRB(TAG, "Timestamp"); + winpr_HexDump(TAG, WLOG_TRACE, context->Timestamp, 8); +} #endif -static int ntlm_read_message_header(wStream* s, NTLM_MESSAGE_HEADER* header) +static BOOL ntlm_read_message_header(wStream* s, NTLM_MESSAGE_HEADER* header, UINT32 expected) { + WINPR_ASSERT(s); + WINPR_ASSERT(header); + if (Stream_GetRemainingLength(s) < 12) - return -1; + { + WLog_ERR(TAG, "Short NTLM_MESSAGE_HEADER::header %" PRIuz ", expected 12", + Stream_GetRemainingLength(s)); + return FALSE; + } Stream_Read(s, header->Signature, 8); Stream_Read_UINT32(s, header->MessageType); if (strncmp((char*)header->Signature, NTLM_SIGNATURE, 8) != 0) - return -1; + { + WLog_ERR(TAG, "NTLM_MESSAGE_HEADER Invalid signature, got %s, expected %s", + header->Signature, NTLM_SIGNATURE); + return FALSE; + } - return 1; + if (header->MessageType != expected) + { + WLog_ERR(TAG, "NTLM_MESSAGE_HEADER Invalid message tyep, got %s, expected %s", + ntlm_message_type_string(header->MessageType), ntlm_message_type_string(expected)); + return FALSE; + } + + return TRUE; } -static void ntlm_write_message_header(wStream* s, NTLM_MESSAGE_HEADER* header) +static BOOL ntlm_write_message_header(wStream* s, const NTLM_MESSAGE_HEADER* header) { + WINPR_ASSERT(s); + WINPR_ASSERT(header); + + if (Stream_GetRemainingCapacity(s) < sizeof(NTLM_SIGNATURE) + 4) + { + WLog_ERR(TAG, "Short NTLM_MESSAGE_HEADER::header %" PRIuz ", expected 12", + Stream_GetRemainingCapacity(s)); + return FALSE; + } + Stream_Write(s, header->Signature, sizeof(NTLM_SIGNATURE)); Stream_Write_UINT32(s, header->MessageType); + + return TRUE; } -static void ntlm_populate_message_header(NTLM_MESSAGE_HEADER* header, UINT32 MessageType) +static BOOL ntlm_populate_message_header(NTLM_MESSAGE_HEADER* header, UINT32 MessageType) { + WINPR_ASSERT(header); + CopyMemory(header->Signature, NTLM_SIGNATURE, sizeof(NTLM_SIGNATURE)); header->MessageType = MessageType; + return TRUE; } -static int ntlm_read_message_fields(wStream* s, NTLM_MESSAGE_FIELDS* fields) +static BOOL ntlm_read_message_fields(wStream* s, NTLM_MESSAGE_FIELDS* fields) { + WINPR_ASSERT(s); + WINPR_ASSERT(fields); + if (Stream_GetRemainingLength(s) < 8) - return -1; + { + WLog_ERR(TAG, "Short NTLM_MESSAGE_FIELDS::header %" PRIuz ", expected %" PRIuz, + Stream_GetRemainingLength(s), 8); + return FALSE; + } Stream_Read_UINT16(s, fields->Len); /* Len (2 bytes) */ Stream_Read_UINT16(s, fields->MaxLen); /* MaxLen (2 bytes) */ Stream_Read_UINT32(s, fields->BufferOffset); /* BufferOffset (4 bytes) */ - return 1; + return TRUE; } -static void ntlm_write_message_fields(wStream* s, NTLM_MESSAGE_FIELDS* fields) +static BOOL ntlm_write_message_fields(wStream* s, const NTLM_MESSAGE_FIELDS* fields) { + UINT16 MaxLen; + WINPR_ASSERT(s); + WINPR_ASSERT(fields); + + MaxLen = fields->MaxLen; if (fields->MaxLen < 1) - fields->MaxLen = fields->Len; + MaxLen = fields->Len; + if (Stream_GetRemainingCapacity(s) < 8) + { + WLog_ERR(TAG, "Short NTLM_MESSAGE_FIELDS::header %" PRIuz ", expected %" PRIuz, + Stream_GetRemainingCapacity(s), 8); + return FALSE; + } Stream_Write_UINT16(s, fields->Len); /* Len (2 bytes) */ - Stream_Write_UINT16(s, fields->MaxLen); /* MaxLen (2 bytes) */ + Stream_Write_UINT16(s, MaxLen); /* MaxLen (2 bytes) */ Stream_Write_UINT32(s, fields->BufferOffset); /* BufferOffset (4 bytes) */ + return TRUE; } -static int ntlm_read_message_fields_buffer(wStream* s, NTLM_MESSAGE_FIELDS* fields) +static BOOL ntlm_read_message_fields_buffer(wStream* s, NTLM_MESSAGE_FIELDS* fields) { + WINPR_ASSERT(s); + WINPR_ASSERT(fields); + if (fields->Len > 0) { const UINT32 offset = fields->BufferOffset + fields->Len; if (fields->BufferOffset > UINT32_MAX - fields->Len) - return -1; + { + WLog_ERR(TAG, + "NTLM_MESSAGE_FIELDS::BufferOffset %" PRIu32 + " too large, maximum allowed is %" PRIu32, + fields->BufferOffset, UINT32_MAX - fields->Len); + return FALSE; + } if (offset > Stream_Length(s)) - return -1; + { + WLog_ERR(TAG, + "NTLM_MESSAGE_FIELDS::Buffer offset %" PRIu32 " beyond received data %" PRIuz, + offset, Stream_Length(s)); + return FALSE; + } fields->Buffer = (PBYTE)malloc(fields->Len); if (!fields->Buffer) - return -1; + { + WLog_ERR(TAG, "NTLM_MESSAGE_FIELDS::Buffer allocation of %" PRIu16 "bytes failed", + fields->Len); + return FALSE; + } Stream_SetPosition(s, fields->BufferOffset); Stream_Read(s, fields->Buffer, fields->Len); } - return 1; + return TRUE; } -static void ntlm_write_message_fields_buffer(wStream* s, NTLM_MESSAGE_FIELDS* fields) +static BOOL ntlm_write_message_fields_buffer(wStream* s, const NTLM_MESSAGE_FIELDS* fields) { + WINPR_ASSERT(s); + WINPR_ASSERT(fields); + if (fields->Len > 0) { Stream_SetPosition(s, fields->BufferOffset); + if (Stream_GetRemainingCapacity(s) < fields->Len) + { + WLog_ERR(TAG, "Short NTLM_MESSAGE_FIELDS::Len %" PRIuz ", expected %" PRIu16, + Stream_GetRemainingCapacity(s), fields->Len); + return FALSE; + } Stream_Write(s, fields->Buffer, fields->Len); } + return TRUE; } static void ntlm_free_message_fields_buffer(NTLM_MESSAGE_FIELDS* fields) @@ -182,81 +412,155 @@ static void ntlm_free_message_fields_buffer(NTLM_MESSAGE_FIELDS* fields) } } -#ifdef WITH_DEBUG_NTLM -static void ntlm_print_message_fields(NTLM_MESSAGE_FIELDS* fields, const char* name) +static BOOL ntlm_read_negotiate_flags(wStream* s, UINT32* flags, UINT32 required, const char* name) { - WLog_VRB(TAG, "%s (Len: %" PRIu16 " MaxLen: %" PRIu16 " BufferOffset: %" PRIu32 ")", name, - fields->Len, fields->MaxLen, fields->BufferOffset); + UINT32 NegotiateFlags = 0; + WINPR_ASSERT(s); + WINPR_ASSERT(flags); + WINPR_ASSERT(name); - if (fields->Len > 0) - winpr_HexDump(TAG, WLOG_TRACE, fields->Buffer, fields->Len); + if (Stream_GetRemainingLength(s) < 4) + { + WLog_ERR(TAG, "%s::NegotiateFlags expected 4bytes, have %" PRIuz "bytes", name, + Stream_GetRemainingLength(s)); + return FALSE; + } + + Stream_Read_UINT32(s, NegotiateFlags); /* NegotiateFlags (4 bytes) */ + + if ((NegotiateFlags & required) != required) + { + WLog_ERR(TAG, "%s::NegotiateFlags invalid flags 0x08%" PRIx32 ", 0x%08" PRIx32 " required", + name, NegotiateFlags, required); + return FALSE; + } + *flags = NegotiateFlags; + return TRUE; +} + +static BOOL ntlm_write_negotiate_flags(wStream* s, UINT32 flags, const char* name) +{ + WINPR_ASSERT(s); + WINPR_ASSERT(name); + + if (Stream_GetRemainingCapacity(s) < 4) + { + WLog_ERR(TAG, "%s::NegotiateFlags expected 4bytes, have %" PRIuz "bytes", name, + Stream_GetRemainingCapacity(s)); + return FALSE; + } + + Stream_Write_UINT32(s, flags); /* NegotiateFlags (4 bytes) */ + return TRUE; +} + +static BOOL ntlm_read_message_integrity_check(wStream* s, size_t* offset, BYTE* data, size_t size, + const char* name) +{ + WINPR_ASSERT(s); + WINPR_ASSERT(offset); + WINPR_ASSERT(data); + WINPR_ASSERT(size == WINPR_MD5_DIGEST_LENGTH); + WINPR_ASSERT(name); + + *offset = Stream_GetPosition(s); + + if (Stream_GetRemainingLength(s) < size) + { + WLog_ERR(TAG, + "%s::MessageIntegrityCheckOffset expected %" PRIuz "bytes, got " + "%" PRIuz "byets", + name, size, Stream_GetRemainingLength(s)); + return FALSE; + } + + Stream_Read(s, data, size); + return TRUE; +} + +static BOOL ntlm_write_message_integrity_check(wStream* s, size_t offset, const BYTE* data, + size_t size, const char* name) +{ + size_t pos; + + WINPR_ASSERT(s); + WINPR_ASSERT(data); + WINPR_ASSERT(size == WINPR_MD5_DIGEST_LENGTH); + WINPR_ASSERT(name); + + pos = Stream_GetPosition(s); + + if (offset + size > Stream_Capacity(s)) + { + WLog_ERR(TAG, + "%s::MessageIntegrityCheck invalid offset[length] %" PRIuz "[%" PRIuz + "], got %" PRIuz, + name, offset, size, Stream_GetRemainingCapacity(s)); + return FALSE; + } + Stream_SetPosition(s, offset); + if (Stream_GetRemainingCapacity(s) < size) + { + WLog_ERR(TAG, "%s::MessageIntegrityCheck expected %" PRIuz "bytes, got %" PRIuz "bytes", + name, size, Stream_GetRemainingCapacity(s)); + return FALSE; + } + + Stream_Write(s, data, size); + Stream_SetPosition(s, pos); + return TRUE; } -#endif SECURITY_STATUS ntlm_read_NegotiateMessage(NTLM_CONTEXT* context, PSecBuffer buffer) { + wStream sbuffer; wStream* s; size_t length; + const NTLM_NEGOTIATE_MESSAGE empty = { 0 }; NTLM_NEGOTIATE_MESSAGE* message; + + WINPR_ASSERT(context); + WINPR_ASSERT(buffer); + message = &context->NEGOTIATE_MESSAGE; - ZeroMemory(message, sizeof(NTLM_NEGOTIATE_MESSAGE)); - s = Stream_New((BYTE*)buffer->pvBuffer, buffer->cbBuffer); + WINPR_ASSERT(message); + + *message = empty; + + s = Stream_StaticConstInit(&sbuffer, buffer->pvBuffer, buffer->cbBuffer); if (!s) return SEC_E_INTERNAL_ERROR; - if (ntlm_read_message_header(s, (NTLM_MESSAGE_HEADER*)message) < 0) - { - Stream_Free(s, FALSE); + if (!ntlm_read_message_header(s, &message->header, MESSAGE_TYPE_NEGOTIATE)) return SEC_E_INVALID_TOKEN; - } - if (message->MessageType != MESSAGE_TYPE_NEGOTIATE) - { - Stream_Free(s, FALSE); + if (!ntlm_read_negotiate_flags(s, &message->NegotiateFlags, + NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_NTLM | + NTLMSSP_NEGOTIATE_UNICODE, + "NTLM_NEGOTIATE_MESSAGE")) return SEC_E_INVALID_TOKEN; - } - - if (Stream_GetRemainingLength(s) < 4) - { - Stream_Free(s, FALSE); - return SEC_E_INVALID_TOKEN; - } - Stream_Read_UINT32(s, message->NegotiateFlags); /* NegotiateFlags (4 bytes) */ - - if (!((message->NegotiateFlags & NTLMSSP_REQUEST_TARGET) && - (message->NegotiateFlags & NTLMSSP_NEGOTIATE_NTLM) && - (message->NegotiateFlags & NTLMSSP_NEGOTIATE_UNICODE))) - { - Stream_Free(s, FALSE); - return SEC_E_INVALID_TOKEN; - } context->NegotiateFlags = message->NegotiateFlags; /* only set if NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED is set */ - - if (ntlm_read_message_fields(s, &(message->DomainName)) < 0) /* DomainNameFields (8 bytes) */ + // if (context->NegotiateFlags & NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED) { - Stream_Free(s, FALSE); - return SEC_E_INVALID_TOKEN; + if (!ntlm_read_message_fields(s, &(message->DomainName))) /* DomainNameFields (8 bytes) */ + return SEC_E_INVALID_TOKEN; } /* only set if NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED is set */ - - if (ntlm_read_message_fields(s, &(message->Workstation)) < 0) /* WorkstationFields (8 bytes) */ + // if (context->NegotiateFlags & NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED) { - Stream_Free(s, FALSE); - return SEC_E_INVALID_TOKEN; + if (!ntlm_read_message_fields(s, &(message->Workstation))) /* WorkstationFields (8 bytes) */ + return SEC_E_INVALID_TOKEN; } if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) { - if (ntlm_read_version_info(s, &(message->Version)) < 0) /* Version (8 bytes) */ - { - Stream_Free(s, FALSE); + if (!ntlm_read_version_info(s, &(message->Version))) /* Version (8 bytes) */ return SEC_E_INVALID_TOKEN; - } } length = Stream_GetPosition(s); @@ -264,41 +568,40 @@ SECURITY_STATUS ntlm_read_NegotiateMessage(NTLM_CONTEXT* context, PSecBuffer buf buffer->cbBuffer = (ULONG)length; if (!sspi_SecBufferAlloc(&context->NegotiateMessage, (ULONG)length)) - { - Stream_Free(s, FALSE); return SEC_E_INTERNAL_ERROR; - } CopyMemory(context->NegotiateMessage.pvBuffer, buffer->pvBuffer, buffer->cbBuffer); context->NegotiateMessage.BufferType = buffer->BufferType; -#ifdef WITH_DEBUG_NTLM - WLog_VRB(TAG, "NEGOTIATE_MESSAGE (length = %" PRIu32 ")", context->NegotiateMessage.cbBuffer); - winpr_HexDump(TAG, WLOG_TRACE, context->NegotiateMessage.pvBuffer, - context->NegotiateMessage.cbBuffer); - ntlm_print_negotiate_flags(message->NegotiateFlags); - - if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) - ntlm_print_version_info(&(message->Version)); - +#if defined(WITH_DEBUG_NTLM) + ntlm_print_negotiate_message(&context->NegotiateMessage, message); #endif - context->state = NTLM_STATE_CHALLENGE; - Stream_Free(s, FALSE); + ntlm_change_state(context, NTLM_STATE_CHALLENGE); return SEC_I_CONTINUE_NEEDED; } SECURITY_STATUS ntlm_write_NegotiateMessage(NTLM_CONTEXT* context, PSecBuffer buffer) { + wStream sbuffer; wStream* s; size_t length; + const NTLM_NEGOTIATE_MESSAGE empty = { 0 }; NTLM_NEGOTIATE_MESSAGE* message; + + WINPR_ASSERT(context); + WINPR_ASSERT(buffer); + message = &context->NEGOTIATE_MESSAGE; - ZeroMemory(message, sizeof(NTLM_NEGOTIATE_MESSAGE)); - s = Stream_New((BYTE*)buffer->pvBuffer, buffer->cbBuffer); + WINPR_ASSERT(message); + + *message = empty; + + s = Stream_StaticInit(&sbuffer, buffer->pvBuffer, buffer->cbBuffer); if (!s) return SEC_E_INTERNAL_ERROR; - ntlm_populate_message_header((NTLM_MESSAGE_HEADER*)message, MESSAGE_TYPE_NEGOTIATE); + if (!ntlm_populate_message_header(&message->header, MESSAGE_TYPE_NEGOTIATE)) + return SEC_E_INTERNAL_ERROR; if (context->NTLMv2) { @@ -328,97 +631,101 @@ SECURITY_STATUS ntlm_write_NegotiateMessage(NTLM_CONTEXT* context, PSecBuffer bu context->NegotiateFlags = message->NegotiateFlags; /* Message Header (12 bytes) */ - ntlm_write_message_header(s, (NTLM_MESSAGE_HEADER*)message); - Stream_Write_UINT32(s, message->NegotiateFlags); /* NegotiateFlags (4 bytes) */ + if (!ntlm_write_message_header(s, &message->header)) + return SEC_E_INTERNAL_ERROR; + + if (!ntlm_write_negotiate_flags(s, message->NegotiateFlags, "NTLM_NEGOTIATE_MESSAGE")) + return SEC_E_INTERNAL_ERROR; + /* only set if NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED is set */ /* DomainNameFields (8 bytes) */ - ntlm_write_message_fields(s, &(message->DomainName)); + if (!ntlm_write_message_fields(s, &(message->DomainName))) + return SEC_E_INTERNAL_ERROR; + /* only set if NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED is set */ /* WorkstationFields (8 bytes) */ - ntlm_write_message_fields(s, &(message->Workstation)); + if (!ntlm_write_message_fields(s, &(message->Workstation))) + return SEC_E_INTERNAL_ERROR; if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) - ntlm_write_version_info(s, &(message->Version)); + { + if (!ntlm_write_version_info(s, &(message->Version))) + return SEC_E_INTERNAL_ERROR; + } length = Stream_GetPosition(s); WINPR_ASSERT(length <= ULONG_MAX); buffer->cbBuffer = (ULONG)length; if (!sspi_SecBufferAlloc(&context->NegotiateMessage, (ULONG)length)) - { - Stream_Free(s, FALSE); return SEC_E_INTERNAL_ERROR; - } CopyMemory(context->NegotiateMessage.pvBuffer, buffer->pvBuffer, buffer->cbBuffer); context->NegotiateMessage.BufferType = buffer->BufferType; -#ifdef WITH_DEBUG_NTLM - WLog_VRB(TAG, "NEGOTIATE_MESSAGE (length = %d)", length); - winpr_HexDump(TAG, WLOG_TRACE, Stream_Buffer(s), length); - - if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) - ntlm_print_version_info(&(message->Version)); - +#if defined(WITH_DEBUG_NTLM) + ntlm_print_negotiate_message(&context->NegotiateMessage, message); #endif - context->state = NTLM_STATE_CHALLENGE; - Stream_Free(s, FALSE); + ntlm_change_state(context, NTLM_STATE_CHALLENGE); return SEC_I_CONTINUE_NEEDED; } SECURITY_STATUS ntlm_read_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer buffer) { SECURITY_STATUS status = SEC_E_INVALID_TOKEN; + wStream sbuffer; wStream* s; size_t length; size_t StartOffset; size_t PayloadOffset; NTLM_AV_PAIR* AvTimestamp; + const NTLM_CHALLENGE_MESSAGE empty = { 0 }; NTLM_CHALLENGE_MESSAGE* message; + if (!context || !buffer) return SEC_E_INTERNAL_ERROR; ntlm_generate_client_challenge(context); message = &context->CHALLENGE_MESSAGE; - ZeroMemory(message, sizeof(NTLM_CHALLENGE_MESSAGE)); - s = Stream_New((BYTE*)buffer->pvBuffer, buffer->cbBuffer); + WINPR_ASSERT(message); + + *message = empty; + + s = Stream_StaticConstInit(&sbuffer, buffer->pvBuffer, buffer->cbBuffer); if (!s) return SEC_E_INTERNAL_ERROR; StartOffset = Stream_GetPosition(s); - if (ntlm_read_message_header(s, (NTLM_MESSAGE_HEADER*)message) < 0) + if (!ntlm_read_message_header(s, &message->header, MESSAGE_TYPE_CHALLENGE)) goto fail; - if (message->MessageType != MESSAGE_TYPE_CHALLENGE) + if (!ntlm_read_message_fields(s, &(message->TargetName))) /* TargetNameFields (8 bytes) */ goto fail; - if (ntlm_read_message_fields(s, &(message->TargetName)) < 0) /* TargetNameFields (8 bytes) */ + if (!ntlm_read_negotiate_flags(s, &message->NegotiateFlags, 0, "NTLM_CHALLENGE_MESSAGE")) goto fail; - if (Stream_GetRemainingLength(s) < 4) - goto fail; - - Stream_Read_UINT32(s, message->NegotiateFlags); /* NegotiateFlags (4 bytes) */ context->NegotiateFlags = message->NegotiateFlags; - if (Stream_GetRemainingLength(s) < 8) + if (Stream_GetRemainingLength(s) < 16) + { + WLog_ERR(TAG, + "NTLM_CHALLENGE_MESSAGE::ServerChallenge expected 16bytes, got %" PRIuz "bytes", + Stream_GetRemainingLength(s)); goto fail; + } Stream_Read(s, message->ServerChallenge, 8); /* ServerChallenge (8 bytes) */ CopyMemory(context->ServerChallenge, message->ServerChallenge, 8); - - if (Stream_GetRemainingLength(s) < 8) - goto fail; - Stream_Read(s, message->Reserved, 8); /* Reserved (8 bytes), should be ignored */ - if (ntlm_read_message_fields(s, &(message->TargetInfo)) < 0) /* TargetInfoFields (8 bytes) */ + if (!ntlm_read_message_fields(s, &(message->TargetInfo))) /* TargetInfoFields (8 bytes) */ goto fail; if (context->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) { - if (ntlm_read_version_info(s, &(message->Version)) < 0) /* Version (8 bytes) */ + if (!ntlm_read_version_info(s, &(message->Version))) /* Version (8 bytes) */ goto fail; } @@ -428,7 +735,7 @@ SECURITY_STATUS ntlm_read_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer buf status = SEC_E_INTERNAL_ERROR; if (message->TargetName.Len > 0) { - if (ntlm_read_message_fields_buffer(s, &(message->TargetName)) < 0) + if (!ntlm_read_message_fields_buffer(s, &(message->TargetName))) goto fail; } @@ -436,7 +743,7 @@ SECURITY_STATUS ntlm_read_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer buf { size_t cbAvTimestamp; - if (ntlm_read_message_fields_buffer(s, &(message->TargetInfo)) < 0) + if (!ntlm_read_message_fields_buffer(s, &(message->TargetInfo))) goto fail; context->ChallengeTargetInfo.pvBuffer = message->TargetInfo.Buffer; @@ -467,31 +774,14 @@ SECURITY_STATUS ntlm_read_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer buf if (context->ChallengeMessage.pvBuffer) CopyMemory(context->ChallengeMessage.pvBuffer, Stream_Buffer(s) + StartOffset, length); -#ifdef WITH_DEBUG_NTLM - WLog_VRB(TAG, "CHALLENGE_MESSAGE (length = %d)", length); - winpr_HexDump(TAG, WLOG_TRACE, context->ChallengeMessage.pvBuffer, - context->ChallengeMessage.cbBuffer); - ntlm_print_negotiate_flags(context->NegotiateFlags); - - if (context->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) - ntlm_print_version_info(&(message->Version)); - - ntlm_print_message_fields(&(message->TargetName), "TargetName"); - ntlm_print_message_fields(&(message->TargetInfo), "TargetInfo"); - - if (context->ChallengeTargetInfo.cbBuffer > 0) - { - WLog_VRB(TAG, "ChallengeTargetInfo (%" PRIu32 "):", context->ChallengeTargetInfo.cbBuffer); - ntlm_print_av_pair_list(context->ChallengeTargetInfo.pvBuffer, - context->ChallengeTargetInfo.cbBuffer); - } - +#if defined(WITH_DEBUG_NTLM) + ntlm_print_challenge_message(&context->ChallengeMessage, message, NULL); #endif /* AV_PAIRs */ if (context->NTLMv2) { - if (ntlm_construct_authenticate_target_info(context) < 0) + if (!ntlm_construct_authenticate_target_info(context)) goto fail; sspi_SecBufferFree(&context->ChallengeTargetInfo); @@ -501,10 +791,10 @@ SECURITY_STATUS ntlm_read_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer buf ntlm_generate_timestamp(context); /* Timestamp */ - if (ntlm_compute_lm_v2_response(context) < 0) /* LmChallengeResponse */ + if (!ntlm_compute_lm_v2_response(context)) /* LmChallengeResponse */ goto fail; - if (ntlm_compute_ntlm_v2_response(context) < 0) /* NtChallengeResponse */ + if (!ntlm_compute_ntlm_v2_response(context)) /* NtChallengeResponse */ goto fail; ntlm_generate_key_exchange_key(context); /* KeyExchangeKey */ @@ -512,54 +802,45 @@ SECURITY_STATUS ntlm_read_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer buf ntlm_generate_exported_session_key(context); /* ExportedSessionKey */ ntlm_encrypt_random_session_key(context); /* EncryptedRandomSessionKey */ /* Generate signing keys */ - ntlm_generate_client_signing_key(context); - ntlm_generate_server_signing_key(context); + if (!ntlm_generate_client_signing_key(context)) + goto fail; + if (!ntlm_generate_server_signing_key(context)) + goto fail; /* Generate sealing keys */ - ntlm_generate_client_sealing_key(context); - ntlm_generate_server_sealing_key(context); + if (!ntlm_generate_client_sealing_key(context)) + goto fail; + if (!ntlm_generate_server_sealing_key(context)) + goto fail; /* Initialize RC4 seal state using client sealing key */ ntlm_init_rc4_seal_states(context); -#ifdef WITH_DEBUG_NTLM - WLog_VRB(TAG, "ClientChallenge"); - winpr_HexDump(TAG, WLOG_TRACE, context->ClientChallenge, 8); - WLog_VRB(TAG, "ServerChallenge"); - winpr_HexDump(TAG, WLOG_TRACE, context->ServerChallenge, 8); - WLog_VRB(TAG, "SessionBaseKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->SessionBaseKey, 16); - WLog_VRB(TAG, "KeyExchangeKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->KeyExchangeKey, 16); - WLog_VRB(TAG, "ExportedSessionKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->ExportedSessionKey, 16); - WLog_VRB(TAG, "RandomSessionKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->RandomSessionKey, 16); - WLog_VRB(TAG, "ClientSigningKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->ClientSigningKey, 16); - WLog_VRB(TAG, "ClientSealingKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->ClientSealingKey, 16); - WLog_VRB(TAG, "ServerSigningKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->ServerSigningKey, 16); - WLog_VRB(TAG, "ServerSealingKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->ServerSealingKey, 16); - WLog_VRB(TAG, "Timestamp"); - winpr_HexDump(TAG, WLOG_TRACE, context->Timestamp, 8); +#if defined(WITH_DEBUG_NTLM) + ntlm_print_authentication_complete(context); #endif - context->state = NTLM_STATE_AUTHENTICATE; + ntlm_change_state(context, NTLM_STATE_AUTHENTICATE); status = SEC_I_CONTINUE_NEEDED; fail: ntlm_free_message_fields_buffer(&(message->TargetName)); - Stream_Free(s, FALSE); return status; } SECURITY_STATUS ntlm_write_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer buffer) { + wStream sbuffer; wStream* s; size_t length; UINT32 PayloadOffset; + const NTLM_CHALLENGE_MESSAGE empty = { 0 }; NTLM_CHALLENGE_MESSAGE* message; + + WINPR_ASSERT(context); + WINPR_ASSERT(buffer); + message = &context->CHALLENGE_MESSAGE; - ZeroMemory(message, sizeof(NTLM_CHALLENGE_MESSAGE)); - s = Stream_New((BYTE*)buffer->pvBuffer, buffer->cbBuffer); + WINPR_ASSERT(message); + + *message = empty; + + s = Stream_StaticInit(&sbuffer, buffer->pvBuffer, buffer->cbBuffer); if (!s) return SEC_E_INTERNAL_ERROR; @@ -568,17 +849,17 @@ SECURITY_STATUS ntlm_write_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer bu ntlm_generate_server_challenge(context); /* Server Challenge */ ntlm_generate_timestamp(context); /* Timestamp */ - if (ntlm_construct_challenge_target_info(context) < 0) /* TargetInfo */ - { - Stream_Free(s, FALSE); + if (!ntlm_construct_challenge_target_info(context)) /* TargetInfo */ return SEC_E_INTERNAL_ERROR; - } CopyMemory(message->ServerChallenge, context->ServerChallenge, 8); /* ServerChallenge */ message->NegotiateFlags = context->NegotiateFlags; - ntlm_populate_message_header((NTLM_MESSAGE_HEADER*)message, MESSAGE_TYPE_CHALLENGE); + if (!ntlm_populate_message_header(&message->header, MESSAGE_TYPE_CHALLENGE)) + return SEC_E_INTERNAL_ERROR; + /* Message Header (12 bytes) */ - ntlm_write_message_header(s, (NTLM_MESSAGE_HEADER*)message); + if (!ntlm_write_message_header(s, &message->header)) + return SEC_E_INTERNAL_ERROR; if (message->NegotiateFlags & NTLMSSP_REQUEST_TARGET) { @@ -602,100 +883,119 @@ SECURITY_STATUS ntlm_write_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer bu message->TargetName.BufferOffset = PayloadOffset; message->TargetInfo.BufferOffset = message->TargetName.BufferOffset + message->TargetName.Len; /* TargetNameFields (8 bytes) */ - ntlm_write_message_fields(s, &(message->TargetName)); - Stream_Write_UINT32(s, message->NegotiateFlags); /* NegotiateFlags (4 bytes) */ + if (!ntlm_write_message_fields(s, &(message->TargetName))) + return SEC_E_INTERNAL_ERROR; + + if (!ntlm_write_negotiate_flags(s, message->NegotiateFlags, "NTLM_CHALLENGE_MESSAGE")) + return SEC_E_INTERNAL_ERROR; + + if (Stream_GetRemainingCapacity(s) < 16) + { + WLog_ERR(TAG, + "NTLM_CHALLENGE_MESSAGE::ServerChallenge expected 16bytes, got %" PRIuz "bytes", + Stream_GetRemainingCapacity(s)); + return SEC_E_INTERNAL_ERROR; + } + Stream_Write(s, message->ServerChallenge, 8); /* ServerChallenge (8 bytes) */ Stream_Write(s, message->Reserved, 8); /* Reserved (8 bytes), should be ignored */ + /* TargetInfoFields (8 bytes) */ - ntlm_write_message_fields(s, &(message->TargetInfo)); + if (!ntlm_write_message_fields(s, &(message->TargetInfo))) + return SEC_E_INTERNAL_ERROR; if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) - ntlm_write_version_info(s, &(message->Version)); /* Version (8 bytes) */ + { + if (!ntlm_write_version_info(s, &(message->Version))) /* Version (8 bytes) */ + return SEC_E_INTERNAL_ERROR; + } /* Payload (variable) */ - if (message->NegotiateFlags & NTLMSSP_REQUEST_TARGET) - ntlm_write_message_fields_buffer(s, &(message->TargetName)); + { + if (!ntlm_write_message_fields_buffer(s, &(message->TargetName))) + return SEC_E_INTERNAL_ERROR; + } if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_TARGET_INFO) - ntlm_write_message_fields_buffer(s, &(message->TargetInfo)); + { + if (!ntlm_write_message_fields_buffer(s, &(message->TargetInfo))) + return SEC_E_INTERNAL_ERROR; + } length = Stream_GetPosition(s); WINPR_ASSERT(length <= ULONG_MAX); buffer->cbBuffer = (ULONG)length; if (!sspi_SecBufferAlloc(&context->ChallengeMessage, (ULONG)length)) - { - Stream_Free(s, FALSE); return SEC_E_INTERNAL_ERROR; - } CopyMemory(context->ChallengeMessage.pvBuffer, Stream_Buffer(s), length); -#ifdef WITH_DEBUG_NTLM - WLog_VRB(TAG, "CHALLENGE_MESSAGE (length = %d)", length); - winpr_HexDump(TAG, WLOG_TRACE, context->ChallengeMessage.pvBuffer, - context->ChallengeMessage.cbBuffer); - ntlm_print_negotiate_flags(message->NegotiateFlags); - - if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) - ntlm_print_version_info(&(message->Version)); - - ntlm_print_message_fields(&(message->TargetName), "TargetName"); - ntlm_print_message_fields(&(message->TargetInfo), "TargetInfo"); +#if defined(WITH_DEBUG_NTLM) + ntlm_print_challenge_message(&context->ChallengeMessage, message, + &context->ChallengeTargetInfo); #endif - context->state = NTLM_STATE_AUTHENTICATE; - Stream_Free(s, FALSE); + ntlm_change_state(context, NTLM_STATE_AUTHENTICATE); return SEC_I_CONTINUE_NEEDED; } SECURITY_STATUS ntlm_read_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer buffer) { SECURITY_STATUS status = SEC_E_INVALID_TOKEN; + wStream sbuffer; wStream* s; size_t length; UINT32 flags = 0; NTLM_AV_PAIR* AvFlags = NULL; size_t PayloadBufferOffset; + const NTLM_AUTHENTICATE_MESSAGE empty = { 0 }; NTLM_AUTHENTICATE_MESSAGE* message; - SSPI_CREDENTIALS* credentials = context->credentials; + SSPI_CREDENTIALS* credentials; + + WINPR_ASSERT(context); + WINPR_ASSERT(buffer); + + credentials = context->credentials; + WINPR_ASSERT(credentials); message = &context->AUTHENTICATE_MESSAGE; - ZeroMemory(message, sizeof(NTLM_AUTHENTICATE_MESSAGE)); - s = Stream_New((BYTE*)buffer->pvBuffer, buffer->cbBuffer); + WINPR_ASSERT(message); + + *message = empty; + + s = Stream_StaticConstInit(&sbuffer, buffer->pvBuffer, buffer->cbBuffer); if (!s) return SEC_E_INTERNAL_ERROR; - if (ntlm_read_message_header(s, (NTLM_MESSAGE_HEADER*)message) < 0) + if (!ntlm_read_message_header(s, &message->header, MESSAGE_TYPE_AUTHENTICATE)) goto fail; - if (message->MessageType != MESSAGE_TYPE_AUTHENTICATE) + if (!ntlm_read_message_fields( + s, &(message->LmChallengeResponse))) /* LmChallengeResponseFields (8 bytes) */ goto fail; - if (ntlm_read_message_fields(s, &(message->LmChallengeResponse)) < - 0) /* LmChallengeResponseFields (8 bytes) */ + if (!ntlm_read_message_fields( + s, &(message->NtChallengeResponse))) /* NtChallengeResponseFields (8 bytes) */ goto fail; - if (ntlm_read_message_fields(s, &(message->NtChallengeResponse)) < - 0) /* NtChallengeResponseFields (8 bytes) */ + if (!ntlm_read_message_fields(s, &(message->DomainName))) /* DomainNameFields (8 bytes) */ goto fail; - if (ntlm_read_message_fields(s, &(message->DomainName)) < 0) /* DomainNameFields (8 bytes) */ + if (!ntlm_read_message_fields(s, &(message->UserName))) /* UserNameFields (8 bytes) */ goto fail; - if (ntlm_read_message_fields(s, &(message->UserName)) < 0) /* UserNameFields (8 bytes) */ + if (!ntlm_read_message_fields(s, &(message->Workstation))) /* WorkstationFields (8 bytes) */ goto fail; - if (ntlm_read_message_fields(s, &(message->Workstation)) < 0) /* WorkstationFields (8 bytes) */ + if (!ntlm_read_message_fields( + s, + &(message->EncryptedRandomSessionKey))) /* EncryptedRandomSessionKeyFields (8 bytes) */ goto fail; - if (ntlm_read_message_fields(s, &(message->EncryptedRandomSessionKey)) < - 0) /* EncryptedRandomSessionKeyFields (8 bytes) */ + if (!ntlm_read_negotiate_flags(s, &message->NegotiateFlags, 0, "NTLM_AUTHENTICATE_MESSAGE")) goto fail; - if (Stream_GetRemainingLength(s) < 4) - goto fail; - Stream_Read_UINT32(s, message->NegotiateFlags); /* NegotiateFlags (4 bytes) */ context->NegotiateKeyExchange = (message->NegotiateFlags & NTLMSSP_NEGOTIATE_KEY_EXCH) ? TRUE : FALSE; @@ -705,44 +1005,48 @@ SECURITY_STATUS ntlm_read_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) { - if (ntlm_read_version_info(s, &(message->Version)) < 0) /* Version (8 bytes) */ + if (!ntlm_read_version_info(s, &(message->Version))) /* Version (8 bytes) */ goto fail; } PayloadBufferOffset = Stream_GetPosition(s); status = SEC_E_INTERNAL_ERROR; - if (ntlm_read_message_fields_buffer(s, &(message->DomainName)) < 0) /* DomainName */ + if (!ntlm_read_message_fields_buffer(s, &(message->DomainName))) /* DomainName */ goto fail; - if (ntlm_read_message_fields_buffer(s, &(message->UserName)) < 0) /* UserName */ + if (!ntlm_read_message_fields_buffer(s, &(message->UserName))) /* UserName */ goto fail; - if (ntlm_read_message_fields_buffer(s, &(message->Workstation)) < 0) /* Workstation */ - goto fail; + if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED) + { + if (!ntlm_read_message_fields_buffer(s, &(message->Workstation))) /* Workstation */ + goto fail; + } - if (ntlm_read_message_fields_buffer(s, &(message->LmChallengeResponse)) < - 0) /* LmChallengeResponse */ - goto fail; + if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_LM_KEY) + { + if (!ntlm_read_message_fields_buffer( + s, &(message->LmChallengeResponse))) /* LmChallengeResponse */ + goto fail; + } - if (ntlm_read_message_fields_buffer(s, &(message->NtChallengeResponse)) < - 0) /* NtChallengeResponse */ + if (!ntlm_read_message_fields_buffer(s, + &(message->NtChallengeResponse))) /* NtChallengeResponse */ goto fail; if (message->NtChallengeResponse.Len > 0) { - int rc; size_t cbAvFlags; - wStream* snt = - Stream_New(message->NtChallengeResponse.Buffer, message->NtChallengeResponse.Len); + wStream ssbuffer; + wStream* snt = Stream_StaticConstInit(&ssbuffer, message->NtChallengeResponse.Buffer, + message->NtChallengeResponse.Len); if (!snt) goto fail; status = SEC_E_INVALID_TOKEN; - rc = ntlm_read_ntlm_v2_response(snt, &(context->NTLMv2Response)); - Stream_Free(snt, FALSE); - if (rc < 0) + if (!ntlm_read_ntlm_v2_response(snt, &(context->NTLMv2Response))) goto fail; status = SEC_E_INTERNAL_ERROR; @@ -760,8 +1064,8 @@ SECURITY_STATUS ntlm_read_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer Data_Read_UINT32(ntlm_av_pair_get_value_pointer(AvFlags), flags); } - if (ntlm_read_message_fields_buffer(s, &(message->EncryptedRandomSessionKey)) < - 0) /* EncryptedRandomSessionKey */ + if (!ntlm_read_message_fields_buffer( + s, &(message->EncryptedRandomSessionKey))) /* EncryptedRandomSessionKey */ goto fail; if (message->EncryptedRandomSessionKey.Len > 0) @@ -785,41 +1089,17 @@ SECURITY_STATUS ntlm_read_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer if (flags & MSV_AV_FLAGS_MESSAGE_INTEGRITY_CHECK) { - context->MessageIntegrityCheckOffset = (UINT32)Stream_GetPosition(s); - status = SEC_E_INVALID_TOKEN; - if (Stream_GetRemainingLength(s) < 16) + if (!ntlm_read_message_integrity_check( + s, &context->MessageIntegrityCheckOffset, message->MessageIntegrityCheck, + sizeof(message->MessageIntegrityCheck), "NTLM_AUTHENTICATE_MESSAGE")) goto fail; - - Stream_Read(s, message->MessageIntegrityCheck, 16); } status = SEC_E_INTERNAL_ERROR; -#ifdef WITH_DEBUG_NTLM - WLog_VRB(TAG, "AUTHENTICATE_MESSAGE (length = %" PRIu32 ")", - context->AuthenticateMessage.cbBuffer); - winpr_HexDump(TAG, WLOG_TRACE, context->AuthenticateMessage.pvBuffer, - context->AuthenticateMessage.cbBuffer); - - if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) - ntlm_print_version_info(&(message->Version)); - - ntlm_print_message_fields(&(message->DomainName), "DomainName"); - ntlm_print_message_fields(&(message->UserName), "UserName"); - ntlm_print_message_fields(&(message->Workstation), "Workstation"); - ntlm_print_message_fields(&(message->LmChallengeResponse), "LmChallengeResponse"); - ntlm_print_message_fields(&(message->NtChallengeResponse), "NtChallengeResponse"); - ntlm_print_message_fields(&(message->EncryptedRandomSessionKey), "EncryptedRandomSessionKey"); - ntlm_print_av_pair_list(context->NTLMv2Response.Challenge.AvPairs, - context->NTLMv2Response.Challenge.cbAvPairs); - - if (flags & MSV_AV_FLAGS_MESSAGE_INTEGRITY_CHECK) - { - WLog_VRB(TAG, "MessageIntegrityCheck:"); - winpr_HexDump(TAG, WLOG_TRACE, message->MessageIntegrityCheck, 16); - } - +#if defined(WITH_DEBUG_NTLM) + ntlm_print_authenticate_message(&context->AuthenticateMessage, message, flags, NULL); #endif if (message->UserName.Len > 0) @@ -845,13 +1125,11 @@ SECURITY_STATUS ntlm_read_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer credentials->identity.DomainLength = message->DomainName.Len / 2; } - Stream_Free(s, FALSE); /* Computations beyond this point require the NTLM hash of the password */ - context->state = NTLM_STATE_COMPLETION; + ntlm_change_state(context, NTLM_STATE_COMPLETION); return SEC_I_COMPLETE_NEEDED; fail: - Stream_Free(s, FALSE); return status; } @@ -864,14 +1142,26 @@ fail: SECURITY_STATUS ntlm_write_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer buffer) { + wStream sbuffer; wStream* s; size_t length; UINT32 PayloadBufferOffset; + const NTLM_AUTHENTICATE_MESSAGE empty = { 0 }; NTLM_AUTHENTICATE_MESSAGE* message; - SSPI_CREDENTIALS* credentials = context->credentials; + SSPI_CREDENTIALS* credentials; + + WINPR_ASSERT(context); + WINPR_ASSERT(buffer); + + credentials = context->credentials; + WINPR_ASSERT(credentials); + message = &context->AUTHENTICATE_MESSAGE; - ZeroMemory(message, sizeof(NTLM_AUTHENTICATE_MESSAGE)); - s = Stream_New((BYTE*)buffer->pvBuffer, buffer->cbBuffer); + WINPR_ASSERT(message); + + *message = empty; + + s = Stream_StaticInit(&sbuffer, buffer->pvBuffer, buffer->cbBuffer); if (!s) return SEC_E_INTERNAL_ERROR; @@ -950,51 +1240,82 @@ SECURITY_STATUS ntlm_write_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer message->LmChallengeResponse.BufferOffset + message->LmChallengeResponse.Len; message->EncryptedRandomSessionKey.BufferOffset = message->NtChallengeResponse.BufferOffset + message->NtChallengeResponse.Len; - ntlm_populate_message_header((NTLM_MESSAGE_HEADER*)message, MESSAGE_TYPE_AUTHENTICATE); - ntlm_write_message_header(s, (NTLM_MESSAGE_HEADER*)message); /* Message Header (12 bytes) */ - ntlm_write_message_fields( - s, &(message->LmChallengeResponse)); /* LmChallengeResponseFields (8 bytes) */ - ntlm_write_message_fields( - s, &(message->NtChallengeResponse)); /* NtChallengeResponseFields (8 bytes) */ - ntlm_write_message_fields(s, &(message->DomainName)); /* DomainNameFields (8 bytes) */ - ntlm_write_message_fields(s, &(message->UserName)); /* UserNameFields (8 bytes) */ - ntlm_write_message_fields(s, &(message->Workstation)); /* WorkstationFields (8 bytes) */ - ntlm_write_message_fields( - s, &(message->EncryptedRandomSessionKey)); /* EncryptedRandomSessionKeyFields (8 bytes) */ - Stream_Write_UINT32(s, message->NegotiateFlags); /* NegotiateFlags (4 bytes) */ + if (!ntlm_populate_message_header(&message->header, MESSAGE_TYPE_AUTHENTICATE)) + return SEC_E_INVALID_TOKEN; + if (!ntlm_write_message_header(s, &message->header)) /* Message Header (12 bytes) */ + return SEC_E_INTERNAL_ERROR; + if (!ntlm_write_message_fields( + s, &(message->LmChallengeResponse))) /* LmChallengeResponseFields (8 bytes) */ + return SEC_E_INTERNAL_ERROR; + if (!ntlm_write_message_fields( + s, &(message->NtChallengeResponse))) /* NtChallengeResponseFields (8 bytes) */ + return SEC_E_INTERNAL_ERROR; + if (!ntlm_write_message_fields(s, &(message->DomainName))) /* DomainNameFields (8 bytes) */ + return SEC_E_INTERNAL_ERROR; + if (!ntlm_write_message_fields(s, &(message->UserName))) /* UserNameFields (8 bytes) */ + return SEC_E_INTERNAL_ERROR; + if (!ntlm_write_message_fields(s, &(message->Workstation))) /* WorkstationFields (8 bytes) */ + return SEC_E_INTERNAL_ERROR; + if (!ntlm_write_message_fields( + s, + &(message->EncryptedRandomSessionKey))) /* EncryptedRandomSessionKeyFields (8 bytes) */ + return SEC_E_INTERNAL_ERROR; + if (!ntlm_write_negotiate_flags(s, message->NegotiateFlags, "NTLM_AUTHENTICATE_MESSAGE")) + return SEC_E_INTERNAL_ERROR; if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) - ntlm_write_version_info(s, &(message->Version)); /* Version (8 bytes) */ + { + if (!ntlm_write_version_info(s, &(message->Version))) /* Version (8 bytes) */ + return SEC_E_INTERNAL_ERROR; + } if (context->UseMIC) { - context->MessageIntegrityCheckOffset = (UINT32)Stream_GetPosition(s); - Stream_Zero(s, 16); /* Message Integrity Check (16 bytes) */ + const BYTE data[WINPR_MD5_DIGEST_LENGTH] = { 0 }; + + context->MessageIntegrityCheckOffset = Stream_GetPosition(s); + if (!ntlm_write_message_integrity_check(s, Stream_GetPosition(s), data, sizeof(data), + "NTLM_AUTHENTICATE_MESSAGE")) + return SEC_E_INTERNAL_ERROR; } if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED) - ntlm_write_message_fields_buffer(s, &(message->DomainName)); /* DomainName */ + { + if (!ntlm_write_message_fields_buffer(s, &(message->DomainName))) /* DomainName */ + return SEC_E_INTERNAL_ERROR; + } - ntlm_write_message_fields_buffer(s, &(message->UserName)); /* UserName */ + if (!ntlm_write_message_fields_buffer(s, &(message->UserName))) /* UserName */ + return SEC_E_INTERNAL_ERROR; if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED) - ntlm_write_message_fields_buffer(s, &(message->Workstation)); /* Workstation */ + { + if (!ntlm_write_message_fields_buffer(s, &(message->Workstation))) /* Workstation */ + return SEC_E_INTERNAL_ERROR; + } - ntlm_write_message_fields_buffer(s, &(message->LmChallengeResponse)); /* LmChallengeResponse */ - ntlm_write_message_fields_buffer(s, &(message->NtChallengeResponse)); /* NtChallengeResponse */ + if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_LM_KEY) + { + if (!ntlm_write_message_fields_buffer( + s, &(message->LmChallengeResponse))) /* LmChallengeResponse */ + return SEC_E_INTERNAL_ERROR; + } + if (!ntlm_write_message_fields_buffer( + s, &(message->NtChallengeResponse))) /* NtChallengeResponse */ + return SEC_E_INTERNAL_ERROR; if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_KEY_EXCH) - ntlm_write_message_fields_buffer( - s, &(message->EncryptedRandomSessionKey)); /* EncryptedRandomSessionKey */ + { + if (!ntlm_write_message_fields_buffer( + s, &(message->EncryptedRandomSessionKey))) /* EncryptedRandomSessionKey */ + return SEC_E_INTERNAL_ERROR; + } length = Stream_GetPosition(s); WINPR_ASSERT(length <= ULONG_MAX); if (!sspi_SecBufferAlloc(&context->AuthenticateMessage, (ULONG)length)) - { - Stream_Free(s, FALSE); return SEC_E_INTERNAL_ERROR; - } CopyMemory(context->AuthenticateMessage.pvBuffer, Stream_Buffer(s), length); buffer->cbBuffer = (ULONG)length; @@ -1002,44 +1323,20 @@ SECURITY_STATUS ntlm_write_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer if (context->UseMIC) { /* Message Integrity Check */ - ntlm_compute_message_integrity_check(context, message->MessageIntegrityCheck, 16); - Stream_SetPosition(s, context->MessageIntegrityCheckOffset); - Stream_Write(s, message->MessageIntegrityCheck, 16); - Stream_SetPosition(s, length); - } - -#ifdef WITH_DEBUG_NTLM - WLog_VRB(TAG, "AUTHENTICATE_MESSAGE (length = %d)", length); - winpr_HexDump(TAG, WLOG_TRACE, Stream_Buffer(s), length); - ntlm_print_negotiate_flags(message->NegotiateFlags); - - if (message->NegotiateFlags & NTLMSSP_NEGOTIATE_VERSION) - ntlm_print_version_info(&(message->Version)); - - if (context->AuthenticateTargetInfo.cbBuffer > 0) - { - WLog_VRB(TAG, - "AuthenticateTargetInfo (%" PRIu32 "):", context->AuthenticateTargetInfo.cbBuffer); - ntlm_print_av_pair_list(context->AuthenticateTargetInfo.pvBuffer, - context->AuthenticateTargetInfo.cbBuffer); - } - - ntlm_print_message_fields(&(message->DomainName), "DomainName"); - ntlm_print_message_fields(&(message->UserName), "UserName"); - ntlm_print_message_fields(&(message->Workstation), "Workstation"); - ntlm_print_message_fields(&(message->LmChallengeResponse), "LmChallengeResponse"); - ntlm_print_message_fields(&(message->NtChallengeResponse), "NtChallengeResponse"); - ntlm_print_message_fields(&(message->EncryptedRandomSessionKey), "EncryptedRandomSessionKey"); - - if (context->UseMIC) - { - WLog_VRB(TAG, "MessageIntegrityCheck (length = 16)"); - winpr_HexDump(TAG, WLOG_TRACE, message->MessageIntegrityCheck, 16); + ntlm_compute_message_integrity_check(context, message->MessageIntegrityCheck, + sizeof(message->MessageIntegrityCheck)); + if (!ntlm_write_message_integrity_check( + s, context->MessageIntegrityCheckOffset, message->MessageIntegrityCheck, + sizeof(message->MessageIntegrityCheck), "NTLM_AUTHENTICATE_MESSAGE")) + return SEC_E_INTERNAL_ERROR; } +#if defined(WITH_DEBUG_NTLM) + ntlm_print_authenticate_message(&context->AuthenticateMessage, message, + context->UseMIC ? MSV_AV_FLAGS_MESSAGE_INTEGRITY_CHECK : 0, + &context->AuthenticateTargetInfo); #endif - context->state = NTLM_STATE_FINAL; - Stream_Free(s, FALSE); + ntlm_change_state(context, NTLM_STATE_FINAL); return SEC_E_OK; } @@ -1049,25 +1346,26 @@ SECURITY_STATUS ntlm_server_AuthenticateComplete(NTLM_CONTEXT* context) size_t cbAvFlags; NTLM_AV_PAIR* AvFlags = NULL; NTLM_AUTHENTICATE_MESSAGE* message; - BYTE messageIntegrityCheck[16]; if (!context) return SEC_E_INVALID_PARAMETER; - if (context->state != NTLM_STATE_COMPLETION) + if (ntlm_get_state(context) != NTLM_STATE_COMPLETION) return SEC_E_OUT_OF_SEQUENCE; message = &context->AUTHENTICATE_MESSAGE; + WINPR_ASSERT(message); + AvFlags = ntlm_av_pair_get(context->NTLMv2Response.Challenge.AvPairs, context->NTLMv2Response.Challenge.cbAvPairs, MsvAvFlags, &cbAvFlags); if (AvFlags) Data_Read_UINT32(ntlm_av_pair_get_value_pointer(AvFlags), flags); - if (ntlm_compute_lm_v2_response(context) < 0) /* LmChallengeResponse */ + if (!ntlm_compute_lm_v2_response(context)) /* LmChallengeResponse */ return SEC_E_INTERNAL_ERROR; - if (ntlm_compute_ntlm_v2_response(context) < 0) /* NtChallengeResponse */ + if (!ntlm_compute_ntlm_v2_response(context)) /* NtChallengeResponse */ return SEC_E_INTERNAL_ERROR; /* KeyExchangeKey */ @@ -1079,16 +1377,16 @@ SECURITY_STATUS ntlm_server_AuthenticateComplete(NTLM_CONTEXT* context) if (flags & MSV_AV_FLAGS_MESSAGE_INTEGRITY_CHECK) { - ZeroMemory( - &((PBYTE)context->AuthenticateMessage.pvBuffer)[context->MessageIntegrityCheckOffset], - 16); + BYTE messageIntegrityCheck[16] = { 0 }; + ntlm_compute_message_integrity_check(context, messageIntegrityCheck, sizeof(messageIntegrityCheck)); CopyMemory( &((PBYTE)context->AuthenticateMessage.pvBuffer)[context->MessageIntegrityCheckOffset], - message->MessageIntegrityCheck, 16); + message->MessageIntegrityCheck, sizeof(message->MessageIntegrityCheck)); - if (memcmp(messageIntegrityCheck, message->MessageIntegrityCheck, 16) != 0) + if (memcmp(messageIntegrityCheck, message->MessageIntegrityCheck, + sizeof(message->MessageIntegrityCheck)) != 0) { WLog_ERR(TAG, "Message Integrity Check (MIC) verification failed!"); #ifdef WITH_DEBUG_NTLM @@ -1132,38 +1430,21 @@ SECURITY_STATUS ntlm_server_AuthenticateComplete(NTLM_CONTEXT* context) } /* Generate signing keys */ - ntlm_generate_client_signing_key(context); - ntlm_generate_server_signing_key(context); + if (!ntlm_generate_client_signing_key(context)) + return SEC_E_INTERNAL_ERROR; + if (!ntlm_generate_server_signing_key(context)) + return SEC_E_INTERNAL_ERROR; /* Generate sealing keys */ - ntlm_generate_client_sealing_key(context); - ntlm_generate_server_sealing_key(context); + if (!ntlm_generate_client_sealing_key(context)) + return SEC_E_INTERNAL_ERROR; + if (!ntlm_generate_server_sealing_key(context)) + return SEC_E_INTERNAL_ERROR; /* Initialize RC4 seal state */ ntlm_init_rc4_seal_states(context); -#ifdef WITH_DEBUG_NTLM - WLog_VRB(TAG, "ClientChallenge"); - winpr_HexDump(TAG, WLOG_TRACE, context->ClientChallenge, 8); - WLog_VRB(TAG, "ServerChallenge"); - winpr_HexDump(TAG, WLOG_TRACE, context->ServerChallenge, 8); - WLog_VRB(TAG, "SessionBaseKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->SessionBaseKey, 16); - WLog_VRB(TAG, "KeyExchangeKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->KeyExchangeKey, 16); - WLog_VRB(TAG, "ExportedSessionKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->ExportedSessionKey, 16); - WLog_VRB(TAG, "RandomSessionKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->RandomSessionKey, 16); - WLog_VRB(TAG, "ClientSigningKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->ClientSigningKey, 16); - WLog_VRB(TAG, "ClientSealingKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->ClientSealingKey, 16); - WLog_VRB(TAG, "ServerSigningKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->ServerSigningKey, 16); - WLog_VRB(TAG, "ServerSealingKey"); - winpr_HexDump(TAG, WLOG_TRACE, context->ServerSealingKey, 16); - WLog_VRB(TAG, "Timestamp"); - winpr_HexDump(TAG, WLOG_TRACE, context->Timestamp, 8); +#if defined(WITH_DEBUG_NTLM) + ntlm_print_authentication_complete(context); #endif - context->state = NTLM_STATE_FINAL; + ntlm_change_state(context, NTLM_STATE_FINAL); ntlm_free_message_fields_buffer(&(message->DomainName)); ntlm_free_message_fields_buffer(&(message->UserName)); ntlm_free_message_fields_buffer(&(message->Workstation)); diff --git a/winpr/libwinpr/sspi/NTLM/ntlm_message.h b/winpr/libwinpr/sspi/NTLM/ntlm_message.h index 100d95a8e..58ff35dd5 100644 --- a/winpr/libwinpr/sspi/NTLM/ntlm_message.h +++ b/winpr/libwinpr/sspi/NTLM/ntlm_message.h @@ -23,12 +23,14 @@ #include "ntlm.h" SECURITY_STATUS ntlm_read_NegotiateMessage(NTLM_CONTEXT* context, PSecBuffer buffer); -SECURITY_STATUS ntlm_write_NegotiateMessage(NTLM_CONTEXT* context, PSecBuffer buffer); +SECURITY_STATUS ntlm_write_NegotiateMessage(NTLM_CONTEXT* context, const PSecBuffer buffer); SECURITY_STATUS ntlm_read_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer buffer); -SECURITY_STATUS ntlm_write_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer buffer); +SECURITY_STATUS ntlm_write_ChallengeMessage(NTLM_CONTEXT* context, const PSecBuffer buffer); SECURITY_STATUS ntlm_read_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer buffer); -SECURITY_STATUS ntlm_write_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer buffer); +SECURITY_STATUS ntlm_write_AuthenticateMessage(NTLM_CONTEXT* context, const PSecBuffer buffer); SECURITY_STATUS ntlm_server_AuthenticateComplete(NTLM_CONTEXT* context); +const char* ntlm_get_negotiate_string(UINT32 flag); + #endif /* WINPR_SSPI_NTLM_MESSAGE_H */ diff --git a/winpr/libwinpr/sspi/test/TestNTLM.c b/winpr/libwinpr/sspi/test/TestNTLM.c index 978829e83..87e60c5a5 100644 --- a/winpr/libwinpr/sspi/test/TestNTLM.c +++ b/winpr/libwinpr/sspi/test/TestNTLM.c @@ -465,18 +465,15 @@ static void test_ntlm_server_free(TEST_NTLM_SERVER* ntlm) free(ntlm); } -int TestNTLM(int argc, char* argv[]) +static BOOL test_default(void) { int status; - int rc = -1; + BOOL rc = FALSE; PSecBuffer pSecBuffer; TEST_NTLM_CLIENT* client = NULL; TEST_NTLM_SERVER* server = NULL; BOOL DynamicTest = TRUE; - WINPR_UNUSED(argc); - WINPR_UNUSED(argv); - /** * Client Initialization */ @@ -682,7 +679,7 @@ int TestNTLM(int argc, char* argv[]) goto fail; } - rc = 0; + rc = TRUE; fail: /** @@ -692,3 +689,13 @@ fail: test_ntlm_server_free(server); return rc; } + +int TestNTLM(int argc, char* argv[]) +{ + WINPR_UNUSED(argc); + WINPR_UNUSED(argv); + + if (!test_default()) + return -1; + return 0; +}