20#include <winpr/config.h>
22#include <winpr/assert.h>
29#include <winpr/ntlm.h>
30#include <winpr/print.h>
31#include <winpr/crypto.h>
32#include <winpr/sysinfo.h>
34#include "ntlm_compute.h"
37#define TAG WINPR_TAG("sspi.NTLM")
39#define NTLM_CheckAndLogRequiredCapacity(tag, s, nmemb, what) \
40 Stream_CheckAndLogRequiredCapacityEx(tag, WLOG_WARN, s, nmemb, 1, "%s(%s:%" PRIuz ") " what, \
41 __func__, __FILE__, (size_t)__LINE__)
43static char NTLM_CLIENT_SIGN_MAGIC[] =
"session key to client-to-server signing key magic constant";
44static char NTLM_SERVER_SIGN_MAGIC[] =
"session key to server-to-client signing key magic constant";
45static char NTLM_CLIENT_SEAL_MAGIC[] =
"session key to client-to-server sealing key magic constant";
46static char NTLM_SERVER_SEAL_MAGIC[] =
"session key to server-to-client sealing key magic constant";
48static const BYTE NTLM_NULL_BUFFER[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
60 WINPR_ASSERT(versionInfo);
62#if defined(WITH_WINPR_DEPRECATED)
63 OSVERSIONINFOA osVersionInfo = { 0 };
64 osVersionInfo.dwOSVersionInfoSize =
sizeof(OSVERSIONINFOA);
65 if (!GetVersionExA(&osVersionInfo))
67 versionInfo->ProductMajorVersion = (UINT8)osVersionInfo.dwMajorVersion;
68 versionInfo->ProductMinorVersion = (UINT8)osVersionInfo.dwMinorVersion;
69 versionInfo->ProductBuild = (UINT16)osVersionInfo.dwBuildNumber;
77 versionInfo->ProductMajorVersion = 10;
78 versionInfo->ProductMinorVersion = 0;
79 versionInfo->ProductBuild = 22631;
81 ZeroMemory(versionInfo->Reserved,
sizeof(versionInfo->Reserved));
82 versionInfo->NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
97 WINPR_ASSERT(versionInfo);
99 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
102 Stream_Read_UINT8(s, versionInfo->ProductMajorVersion);
103 Stream_Read_UINT8(s, versionInfo->ProductMinorVersion);
104 Stream_Read_UINT16(s, versionInfo->ProductBuild);
105 Stream_Read(s, versionInfo->Reserved,
sizeof(versionInfo->Reserved));
106 Stream_Read_UINT8(s, versionInfo->NTLMRevisionCurrent);
121 WINPR_ASSERT(versionInfo);
123 if (!Stream_CheckAndLogRequiredCapacityEx(
124 TAG, WLOG_WARN, s, 5ull +
sizeof(versionInfo->Reserved), 1ull,
125 "%s(%s:%" PRIuz
") NTLM_VERSION_INFO", __func__, __FILE__, (
size_t)__LINE__))
128 Stream_Write_UINT8(s, versionInfo->ProductMajorVersion);
129 Stream_Write_UINT8(s, versionInfo->ProductMinorVersion);
130 Stream_Write_UINT16(s, versionInfo->ProductBuild);
131 Stream_Write(s, versionInfo->Reserved,
sizeof(versionInfo->Reserved));
132 Stream_Write_UINT8(s, versionInfo->NTLMRevisionCurrent);
140#ifdef WITH_DEBUG_NTLM
143 WINPR_ASSERT(versionInfo);
145 WLog_VRB(TAG,
"VERSION ={");
146 WLog_VRB(TAG,
"\tProductMajorVersion: %" PRIu8
"", versionInfo->ProductMajorVersion);
147 WLog_VRB(TAG,
"\tProductMinorVersion: %" PRIu8
"", versionInfo->ProductMinorVersion);
148 WLog_VRB(TAG,
"\tProductBuild: %" PRIu16
"", versionInfo->ProductBuild);
149 WLog_VRB(TAG,
"\tReserved: 0x%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"", versionInfo->Reserved[0],
150 versionInfo->Reserved[1], versionInfo->Reserved[2]);
151 WLog_VRB(TAG,
"\tNTLMRevisionCurrent: 0x%02" PRIX8
"", versionInfo->NTLMRevisionCurrent);
159 WINPR_ASSERT(challenge);
161 if (!Stream_CheckAndLogRequiredLength(TAG, s, 28))
164 Stream_Read_UINT8(s, challenge->RespType);
165 Stream_Read_UINT8(s, challenge->HiRespType);
166 Stream_Read_UINT16(s, challenge->Reserved1);
167 Stream_Read_UINT32(s, challenge->Reserved2);
168 Stream_Read(s, challenge->Timestamp, 8);
169 Stream_Read(s, challenge->ClientChallenge, 8);
170 Stream_Read_UINT32(s, challenge->Reserved3);
171 size = Stream_Length(s) - Stream_GetPosition(s);
173 if (size > UINT32_MAX)
175 WLog_ERR(TAG,
"NTLMv2_CLIENT_CHALLENGE::cbAvPairs too large, got %" PRIuz
"bytes", size);
179 challenge->cbAvPairs = (UINT32)size;
180 challenge->AvPairs = (
NTLM_AV_PAIR*)malloc(challenge->cbAvPairs);
182 if (!challenge->AvPairs)
184 WLog_ERR(TAG,
"NTLMv2_CLIENT_CHALLENGE::AvPairs failed to allocate %" PRIu32
"bytes",
185 challenge->cbAvPairs);
189 Stream_Read(s, challenge->AvPairs, size);
193static BOOL ntlm_write_ntlm_v2_client_challenge(
wStream* s,
199 WINPR_ASSERT(challenge);
201 if (!NTLM_CheckAndLogRequiredCapacity(TAG, s, 28,
"NTLMv2_CLIENT_CHALLENGE"))
204 Stream_Write_UINT8(s, challenge->RespType);
205 Stream_Write_UINT8(s, challenge->HiRespType);
206 Stream_Write_UINT16(s, challenge->Reserved1);
207 Stream_Write_UINT32(s, challenge->Reserved2);
208 Stream_Write(s, challenge->Timestamp, 8);
209 Stream_Write(s, challenge->ClientChallenge, 8);
210 Stream_Write_UINT32(s, challenge->Reserved3);
211 length = ntlm_av_pair_list_length(challenge->AvPairs, challenge->cbAvPairs);
213 if (!Stream_CheckAndLogRequiredLength(TAG, s, length))
216 Stream_Write(s, challenge->AvPairs, length);
223 WINPR_ASSERT(response);
225 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
228 Stream_Read(s, response->Response, 16);
229 return ntlm_read_ntlm_v2_client_challenge(s, &(response->Challenge));
235 WINPR_ASSERT(response);
237 if (!NTLM_CheckAndLogRequiredCapacity(TAG, s, 16ull,
"NTLMv2_RESPONSE"))
240 Stream_Write(s, response->Response, 16);
241 return ntlm_write_ntlm_v2_client_challenge(s, &(response->Challenge));
249void ntlm_current_time(BYTE* timestamp)
253 WINPR_ASSERT(timestamp);
255 GetSystemTimeAsFileTime(&ft);
256 CopyMemory(timestamp, &(ft),
sizeof(ft));
267 WINPR_ASSERT(context);
269 if (memcmp(context->ChallengeTimestamp, NTLM_NULL_BUFFER, 8) != 0)
270 CopyMemory(context->Timestamp, context->ChallengeTimestamp, 8);
272 ntlm_current_time(context->Timestamp);
275static BOOL ntlm_fetch_ntlm_v2_hash(
NTLM_CONTEXT* context, BYTE* hash)
278 WINPR_SAM* sam = NULL;
279 WINPR_SAM_ENTRY* entry = NULL;
282 WINPR_ASSERT(context);
285 credentials = context->credentials;
286 sam = SamOpen(context->SamFile, TRUE);
291 entry = SamLookupUserW(
292 sam, (LPWSTR)credentials->identity.User, credentials->identity.UserLength *
sizeof(WCHAR),
293 (LPWSTR)credentials->identity.Domain, credentials->identity.DomainLength *
sizeof(WCHAR));
297 entry = SamLookupUserW(sam, (LPWSTR)credentials->identity.User,
298 credentials->identity.UserLength *
sizeof(WCHAR), NULL, 0);
304#ifdef WITH_DEBUG_NTLM
305 WLog_VRB(TAG,
"NTLM Hash:");
306 winpr_HexDump(TAG, WLOG_DEBUG, entry->NtHash, 16);
308 NTOWFv2FromHashW(entry->NtHash, (LPWSTR)credentials->identity.User,
309 credentials->identity.UserLength *
sizeof(WCHAR),
310 (LPWSTR)credentials->identity.Domain,
311 credentials->identity.DomainLength *
sizeof(WCHAR), hash);
316 SamFreeEntry(sam, entry);
319 WLog_ERR(TAG,
"Error: Could not find user in SAM database");
324static int hexchar2nibble(WCHAR wc)
326#if defined(__BIG_ENDIAN__)
333 const BYTE b = cnv.b[0];
358 return wc - L
'a' + 10;
365 return wc - L
'A' + 10;
370static int ntlm_convert_password_hash(
NTLM_CONTEXT* context, BYTE* hash,
size_t hashlen)
372 const size_t required_len = 2ull * hashlen;
374 WINPR_ASSERT(context);
380 const ULONG PasswordHashLength = credentials->identity.PasswordLength -
381 SSPI_CREDENTIALS_HASH_LENGTH_OFFSET;
383 if (PasswordHashLength != required_len)
386 "PasswordHash has invalid length %" PRIu32
" must be exactly %" PRIuz
" bytes",
387 PasswordHashLength, required_len);
391 const WCHAR* PasswordHash = credentials->identity.Password;
392 for (
size_t x = 0; x < hashlen; x++)
394 const int hi = hexchar2nibble(PasswordHash[2 * x]);
397 WLog_ERR(TAG,
"PasswordHash has an invalid value at position %" PRIuz, 2 * x);
400 const int lo = hexchar2nibble(PasswordHash[2 * x + 1]);
403 WLog_ERR(TAG,
"PasswordHash has an invalid value at position %" PRIuz, 2 * x + 1);
406 const BYTE val = (BYTE)((hi << 4) | lo);
413static BOOL ntlm_compute_ntlm_v2_hash(
NTLM_CONTEXT* context, BYTE* hash)
417 WINPR_ASSERT(context);
420 credentials = context->credentials;
421#ifdef WITH_DEBUG_NTLM
425 WLog_VRB(TAG,
"Password (length = %" PRIu32
")", credentials->identity.PasswordLength * 2);
426 winpr_HexDump(TAG, WLOG_TRACE, (BYTE*)credentials->identity.Password,
427 credentials->identity.PasswordLength * 2);
428 WLog_VRB(TAG,
"Username (length = %" PRIu32
")", credentials->identity.UserLength * 2);
429 winpr_HexDump(TAG, WLOG_TRACE, (BYTE*)credentials->identity.User,
430 credentials->identity.UserLength * 2);
431 WLog_VRB(TAG,
"Domain (length = %" PRIu32
")", credentials->identity.DomainLength * 2);
432 winpr_HexDump(TAG, WLOG_TRACE, (BYTE*)credentials->identity.Domain,
433 credentials->identity.DomainLength * 2);
436 WLog_VRB(TAG,
"Strange, NTLM_CONTEXT is missing valid credentials...");
438 WLog_VRB(TAG,
"Workstation (length = %" PRIu16
")", context->Workstation.Length);
439 winpr_HexDump(TAG, WLOG_TRACE, (BYTE*)context->Workstation.Buffer, context->Workstation.Length);
440 WLog_VRB(TAG,
"NTOWFv2, NTLMv2 Hash");
441 winpr_HexDump(TAG, WLOG_TRACE, context->NtlmV2Hash, WINPR_MD5_DIGEST_LENGTH);
444 if (memcmp(context->NtlmV2Hash, NTLM_NULL_BUFFER, 16) != 0)
449 else if (memcmp(context->NtlmHash, NTLM_NULL_BUFFER, 16) != 0)
451 NTOWFv2FromHashW(context->NtlmHash, (LPWSTR)credentials->identity.User,
452 credentials->identity.UserLength * 2, (LPWSTR)credentials->identity.Domain,
453 credentials->identity.DomainLength * 2, hash);
455 else if (credentials->identity.PasswordLength > SSPI_CREDENTIALS_HASH_LENGTH_OFFSET)
458 if (ntlm_convert_password_hash(context, context->NtlmHash,
sizeof(context->NtlmHash)) < 0)
461 NTOWFv2FromHashW(context->NtlmHash, (LPWSTR)credentials->identity.User,
462 credentials->identity.UserLength * 2, (LPWSTR)credentials->identity.Domain,
463 credentials->identity.DomainLength * 2, hash);
465 else if (credentials->identity.Password)
467 NTOWFv2W((LPWSTR)credentials->identity.Password, credentials->identity.PasswordLength * 2,
468 (LPWSTR)credentials->identity.User, credentials->identity.UserLength * 2,
469 (LPWSTR)credentials->identity.Domain, credentials->identity.DomainLength * 2,
472 else if (context->HashCallback)
478 if (ntlm_computeProofValue(context, &proofValue) != SEC_E_OK)
481 if (ntlm_computeMicValue(context, &micValue) != SEC_E_OK)
483 sspi_SecBufferFree(&proofValue);
487 ret = context->HashCallback(context->HashCallbackArg, &credentials->identity, &proofValue,
488 context->EncryptedRandomSessionKey,
489 context->AUTHENTICATE_MESSAGE.MessageIntegrityCheck, &micValue,
491 sspi_SecBufferFree(&proofValue);
492 sspi_SecBufferFree(&micValue);
493 return ret ? TRUE : FALSE;
495 else if (context->UseSamFileDatabase)
497 return ntlm_fetch_ntlm_v2_hash(context, hash);
503SECURITY_STATUS ntlm_compute_lm_v2_response(
NTLM_CONTEXT* context)
505 BYTE* response = NULL;
506 BYTE value[WINPR_MD5_DIGEST_LENGTH] = { 0 };
508 WINPR_ASSERT(context);
510 if (context->LmCompatibilityLevel < 2)
512 if (!sspi_SecBufferAlloc(&context->LmChallengeResponse, 24))
513 return SEC_E_INSUFFICIENT_MEMORY;
515 ZeroMemory(context->LmChallengeResponse.pvBuffer, 24);
521 if (!ntlm_compute_ntlm_v2_hash(context, context->NtlmV2Hash))
522 return SEC_E_NO_CREDENTIALS;
525 CopyMemory(value, context->ServerChallenge, 8);
526 CopyMemory(&value[8], context->ClientChallenge, 8);
528 if (!sspi_SecBufferAlloc(&context->LmChallengeResponse, 24))
529 return SEC_E_INSUFFICIENT_MEMORY;
531 response = (BYTE*)context->LmChallengeResponse.pvBuffer;
533 winpr_HMAC(WINPR_MD_MD5, (
void*)context->NtlmV2Hash, WINPR_MD5_DIGEST_LENGTH, (BYTE*)value,
534 WINPR_MD5_DIGEST_LENGTH, response, WINPR_MD5_DIGEST_LENGTH);
537 CopyMemory(&response[16], context->ClientChallenge, 8);
551SECURITY_STATUS ntlm_compute_ntlm_v2_response(
NTLM_CONTEXT* context)
556 WINPR_ASSERT(context);
558 PSecBuffer TargetInfo = &context->ChallengeTargetInfo;
559 SECURITY_STATUS ret = SEC_E_INSUFFICIENT_MEMORY;
561 if (!sspi_SecBufferAlloc(&ntlm_v2_temp, TargetInfo->cbBuffer + 28))
564 ZeroMemory(ntlm_v2_temp.pvBuffer, ntlm_v2_temp.cbBuffer);
566 BYTE* blob = (BYTE*)ntlm_v2_temp.pvBuffer;
569 ret = SEC_E_NO_CREDENTIALS;
570 if (!ntlm_compute_ntlm_v2_hash(context, (BYTE*)context->NtlmV2Hash))
578 CopyMemory(&blob[8], context->Timestamp, 8);
579 CopyMemory(&blob[16], context->ClientChallenge, 8);
581 CopyMemory(&blob[28], TargetInfo->pvBuffer, TargetInfo->cbBuffer);
582#ifdef WITH_DEBUG_NTLM
583 WLog_VRB(TAG,
"NTLMv2 Response Temp Blob");
584 winpr_HexDump(TAG, WLOG_TRACE, ntlm_v2_temp.pvBuffer, ntlm_v2_temp.cbBuffer);
588 ret = SEC_E_INSUFFICIENT_MEMORY;
589 if (!sspi_SecBufferAlloc(&ntlm_v2_temp_chal, ntlm_v2_temp.cbBuffer + 8))
593 BYTE* blob = (BYTE*)ntlm_v2_temp_chal.pvBuffer;
594 CopyMemory(blob, context->ServerChallenge, 8);
595 CopyMemory(&blob[8], ntlm_v2_temp.pvBuffer, ntlm_v2_temp.cbBuffer);
596 winpr_HMAC(WINPR_MD_MD5, (BYTE*)context->NtlmV2Hash, WINPR_MD5_DIGEST_LENGTH,
597 (BYTE*)ntlm_v2_temp_chal.pvBuffer, ntlm_v2_temp_chal.cbBuffer,
598 context->NtProofString, WINPR_MD5_DIGEST_LENGTH);
603 if (!sspi_SecBufferAlloc(&context->NtChallengeResponse, ntlm_v2_temp.cbBuffer + 16))
607 BYTE* blob = (BYTE*)context->NtChallengeResponse.pvBuffer;
608 CopyMemory(blob, context->NtProofString, WINPR_MD5_DIGEST_LENGTH);
609 CopyMemory(&blob[16], ntlm_v2_temp.pvBuffer, ntlm_v2_temp.cbBuffer);
612 winpr_HMAC(WINPR_MD_MD5, (BYTE*)context->NtlmV2Hash, WINPR_MD5_DIGEST_LENGTH,
613 context->NtProofString, WINPR_MD5_DIGEST_LENGTH, context->SessionBaseKey,
614 WINPR_MD5_DIGEST_LENGTH);
617 sspi_SecBufferFree(&ntlm_v2_temp);
618 sspi_SecBufferFree(&ntlm_v2_temp_chal);
630void ntlm_rc4k(BYTE* key,
size_t length, BYTE* plaintext, BYTE* ciphertext)
632 WINPR_RC4_CTX* rc4 = winpr_RC4_New(key, 16);
636 winpr_RC4_Update(rc4, length, plaintext, ciphertext);
646void ntlm_generate_client_challenge(
NTLM_CONTEXT* context)
648 WINPR_ASSERT(context);
651 if (memcmp(context->ClientChallenge, NTLM_NULL_BUFFER,
sizeof(context->ClientChallenge)) == 0)
652 winpr_RAND(context->ClientChallenge,
sizeof(context->ClientChallenge));
660void ntlm_generate_server_challenge(
NTLM_CONTEXT* context)
662 WINPR_ASSERT(context);
664 if (memcmp(context->ServerChallenge, NTLM_NULL_BUFFER,
sizeof(context->ServerChallenge)) == 0)
665 winpr_RAND(context->ServerChallenge,
sizeof(context->ServerChallenge));
673void ntlm_generate_key_exchange_key(
NTLM_CONTEXT* context)
675 WINPR_ASSERT(context);
676 WINPR_ASSERT(
sizeof(context->KeyExchangeKey) ==
sizeof(context->SessionBaseKey));
679 CopyMemory(context->KeyExchangeKey, context->SessionBaseKey,
sizeof(context->KeyExchangeKey));
687void ntlm_generate_random_session_key(
NTLM_CONTEXT* context)
689 WINPR_ASSERT(context);
690 winpr_RAND(context->RandomSessionKey,
sizeof(context->RandomSessionKey));
698void ntlm_generate_exported_session_key(
NTLM_CONTEXT* context)
700 WINPR_ASSERT(context);
702 CopyMemory(context->ExportedSessionKey, context->RandomSessionKey,
703 sizeof(context->ExportedSessionKey));
711void ntlm_encrypt_random_session_key(
NTLM_CONTEXT* context)
715 WINPR_ASSERT(context);
716 ntlm_rc4k(context->KeyExchangeKey, 16, context->RandomSessionKey,
717 context->EncryptedRandomSessionKey);
725void ntlm_decrypt_random_session_key(
NTLM_CONTEXT* context)
727 WINPR_ASSERT(context);
737 if (context->NegotiateKeyExchange)
739 WINPR_ASSERT(
sizeof(context->EncryptedRandomSessionKey) ==
740 sizeof(context->RandomSessionKey));
741 ntlm_rc4k(context->KeyExchangeKey,
sizeof(context->EncryptedRandomSessionKey),
742 context->EncryptedRandomSessionKey, context->RandomSessionKey);
746 WINPR_ASSERT(
sizeof(context->RandomSessionKey) ==
sizeof(context->KeyExchangeKey));
747 CopyMemory(context->RandomSessionKey, context->KeyExchangeKey,
748 sizeof(context->RandomSessionKey));
762static BOOL ntlm_generate_signing_key(BYTE* exported_session_key,
const SecBuffer* sign_magic,
769 WINPR_ASSERT(exported_session_key);
770 WINPR_ASSERT(sign_magic);
771 WINPR_ASSERT(signing_key);
773 length = WINPR_MD5_DIGEST_LENGTH + sign_magic->cbBuffer;
774 value = (BYTE*)malloc(length);
780 CopyMemory(value, exported_session_key, WINPR_MD5_DIGEST_LENGTH);
781 CopyMemory(&value[WINPR_MD5_DIGEST_LENGTH], sign_magic->pvBuffer, sign_magic->cbBuffer);
783 rc = winpr_Digest(WINPR_MD_MD5, value, length, signing_key, WINPR_MD5_DIGEST_LENGTH);
797BOOL ntlm_generate_client_signing_key(
NTLM_CONTEXT* context)
799 const SecBuffer signMagic = {
sizeof(NTLM_CLIENT_SIGN_MAGIC), 0, NTLM_CLIENT_SIGN_MAGIC };
801 WINPR_ASSERT(context);
802 return ntlm_generate_signing_key(context->ExportedSessionKey, &signMagic,
803 context->ClientSigningKey);
813BOOL ntlm_generate_server_signing_key(
NTLM_CONTEXT* context)
815 const SecBuffer signMagic = {
sizeof(NTLM_SERVER_SIGN_MAGIC), 0, NTLM_SERVER_SIGN_MAGIC };
817 WINPR_ASSERT(context);
818 return ntlm_generate_signing_key(context->ExportedSessionKey, &signMagic,
819 context->ServerSigningKey);
829BOOL ntlm_generate_client_sealing_key(
NTLM_CONTEXT* context)
831 const SecBuffer sealMagic = {
sizeof(NTLM_CLIENT_SEAL_MAGIC), 0, NTLM_CLIENT_SEAL_MAGIC };
833 WINPR_ASSERT(context);
834 return ntlm_generate_signing_key(context->ExportedSessionKey, &sealMagic,
835 context->ClientSealingKey);
845BOOL ntlm_generate_server_sealing_key(
NTLM_CONTEXT* context)
847 const SecBuffer sealMagic = {
sizeof(NTLM_SERVER_SEAL_MAGIC), 0, NTLM_SERVER_SEAL_MAGIC };
849 WINPR_ASSERT(context);
850 return ntlm_generate_signing_key(context->ExportedSessionKey, &sealMagic,
851 context->ServerSealingKey);
861 WINPR_ASSERT(context);
864 context->SendSigningKey = context->ServerSigningKey;
865 context->RecvSigningKey = context->ClientSigningKey;
866 context->SendSealingKey = context->ClientSealingKey;
867 context->RecvSealingKey = context->ServerSealingKey;
868 context->SendRc4Seal =
869 winpr_RC4_New(context->ServerSealingKey,
sizeof(context->ServerSealingKey));
870 context->RecvRc4Seal =
871 winpr_RC4_New(context->ClientSealingKey,
sizeof(context->ClientSealingKey));
875 context->SendSigningKey = context->ClientSigningKey;
876 context->RecvSigningKey = context->ServerSigningKey;
877 context->SendSealingKey = context->ServerSealingKey;
878 context->RecvSealingKey = context->ClientSealingKey;
879 context->SendRc4Seal =
880 winpr_RC4_New(context->ClientSealingKey,
sizeof(context->ClientSealingKey));
881 context->RecvRc4Seal =
882 winpr_RC4_New(context->ServerSealingKey,
sizeof(context->ServerSealingKey));
884 if (!context->SendRc4Seal)
886 WLog_ERR(TAG,
"Failed to allocate context->SendRc4Seal");
889 if (!context->RecvRc4Seal)
891 WLog_ERR(TAG,
"Failed to allocate context->RecvRc4Seal");
897BOOL ntlm_compute_message_integrity_check(
NTLM_CONTEXT* context, BYTE* mic, UINT32 size)
904 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
906 WINPR_ASSERT(context);
908 WINPR_ASSERT(size >= WINPR_MD5_DIGEST_LENGTH);
910 memset(mic, 0, size);
914 if (winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->ExportedSessionKey, WINPR_MD5_DIGEST_LENGTH))
916 winpr_HMAC_Update(hmac, (BYTE*)context->NegotiateMessage.pvBuffer,
917 context->NegotiateMessage.cbBuffer);
918 winpr_HMAC_Update(hmac, (BYTE*)context->ChallengeMessage.pvBuffer,
919 context->ChallengeMessage.cbBuffer);
921 if (context->MessageIntegrityCheckOffset > 0)
923 const BYTE* auth = (BYTE*)context->AuthenticateMessage.pvBuffer;
924 const BYTE data[WINPR_MD5_DIGEST_LENGTH] = { 0 };
925 const size_t rest = context->MessageIntegrityCheckOffset +
sizeof(data);
927 WINPR_ASSERT(rest <= context->AuthenticateMessage.cbBuffer);
928 winpr_HMAC_Update(hmac, &auth[0], context->MessageIntegrityCheckOffset);
929 winpr_HMAC_Update(hmac, data,
sizeof(data));
930 winpr_HMAC_Update(hmac, &auth[rest], context->AuthenticateMessage.cbBuffer - rest);
934 winpr_HMAC_Update(hmac, (BYTE*)context->AuthenticateMessage.pvBuffer,
935 context->AuthenticateMessage.cbBuffer);
937 winpr_HMAC_Final(hmac, mic, WINPR_MD5_DIGEST_LENGTH);
941 winpr_HMAC_Free(hmac);