20 #include <winpr/config.h>
22 #include <winpr/crt.h>
23 #include <winpr/assert.h>
24 #include <winpr/sspi.h>
25 #include <winpr/print.h>
26 #include <winpr/string.h>
27 #include <winpr/tchar.h>
28 #include <winpr/sysinfo.h>
29 #include <winpr/registry.h>
30 #include <winpr/endian.h>
31 #include <winpr/build-config.h>
34 #include "ntlm_export.h"
37 #include "ntlm_message.h"
39 #include "../../log.h"
40 #define TAG WINPR_TAG("sspi.NTLM")
42 #define WINPR_KEY "Software\\" WINPR_VENDOR_STRING "\\" WINPR_PRODUCT_STRING "\\WinPR\\NTLM"
44 static char* NTLM_PACKAGE_NAME =
"NTLM";
46 #define check_context(ctx) check_context_((ctx), __FILE__, __func__, __LINE__)
47 static BOOL check_context_(
NTLM_CONTEXT* context,
const char* file,
const char* fkt,
size_t line)
50 wLog* log = WLog_Get(TAG);
51 const DWORD log_level = WLOG_ERROR;
55 if (WLog_IsLevelActive(log, log_level))
56 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, log_level, line, file, fkt,
62 if (!context->RecvRc4Seal)
64 if (WLog_IsLevelActive(log, log_level))
65 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, log_level, line, file, fkt,
66 "invalid context->RecvRc4Seal");
69 if (!context->SendRc4Seal)
71 if (WLog_IsLevelActive(log, log_level))
72 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, log_level, line, file, fkt,
73 "invalid context->SendRc4Seal");
77 if (!context->SendSigningKey)
79 if (WLog_IsLevelActive(log, log_level))
80 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, log_level, line, file, fkt,
81 "invalid context->SendSigningKey");
84 if (!context->RecvSigningKey)
86 if (WLog_IsLevelActive(log, log_level))
87 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, log_level, line, file, fkt,
88 "invalid context->RecvSigningKey");
91 if (!context->SendSealingKey)
93 if (WLog_IsLevelActive(log, log_level))
94 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, log_level, line, file, fkt,
95 "invalid context->SendSealingKey");
98 if (!context->RecvSealingKey)
100 if (WLog_IsLevelActive(log, log_level))
101 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, log_level, line, file, fkt,
102 "invalid context->RecvSealingKey");
108 static int ntlm_SetContextWorkstation(
NTLM_CONTEXT* context,
char* Workstation)
110 char* ws = Workstation;
112 CHAR* computerName = NULL;
114 WINPR_ASSERT(context);
118 if (GetComputerNameExA(ComputerNameNetBIOS, NULL, &nSize) ||
119 GetLastError() != ERROR_MORE_DATA)
122 computerName = calloc(nSize,
sizeof(CHAR));
127 if (!GetComputerNameExA(ComputerNameNetBIOS, computerName, &nSize))
133 if (nSize > MAX_COMPUTERNAME_LENGTH)
134 computerName[MAX_COMPUTERNAME_LENGTH] =
'\0';
143 context->Workstation.Buffer = ConvertUtf8ToWCharAlloc(ws, &len);
148 if (!context->Workstation.Buffer || (len > UINT16_MAX /
sizeof(WCHAR)))
151 context->Workstation.Length = (USHORT)(len *
sizeof(WCHAR));
155 static int ntlm_SetContextServicePrincipalNameW(
NTLM_CONTEXT* context, LPWSTR ServicePrincipalName)
157 WINPR_ASSERT(context);
159 if (!ServicePrincipalName)
161 context->ServicePrincipalName.Buffer = NULL;
162 context->ServicePrincipalName.Length = 0;
166 context->ServicePrincipalName.Length = (USHORT)(_wcslen(ServicePrincipalName) * 2);
167 context->ServicePrincipalName.Buffer = (PWSTR)malloc(context->ServicePrincipalName.Length + 2);
169 if (!context->ServicePrincipalName.Buffer)
172 memcpy(context->ServicePrincipalName.Buffer, ServicePrincipalName,
173 context->ServicePrincipalName.Length + 2);
177 static int ntlm_SetContextTargetName(
NTLM_CONTEXT* context,
char* TargetName)
179 char* name = TargetName;
181 CHAR* computerName = NULL;
183 WINPR_ASSERT(context);
187 if (GetComputerNameExA(ComputerNameNetBIOS, NULL, &nSize) ||
188 GetLastError() != ERROR_MORE_DATA)
191 computerName = calloc(nSize,
sizeof(CHAR));
196 if (!GetComputerNameExA(ComputerNameNetBIOS, computerName, &nSize))
202 if (nSize > MAX_COMPUTERNAME_LENGTH)
203 computerName[MAX_COMPUTERNAME_LENGTH] =
'\0';
214 context->TargetName.pvBuffer = ConvertUtf8ToWCharAlloc(name, &len);
216 if (!context->TargetName.pvBuffer || (len > UINT16_MAX /
sizeof(WCHAR)))
218 free(context->TargetName.pvBuffer);
219 context->TargetName.pvBuffer = NULL;
227 context->TargetName.cbBuffer = (USHORT)(len *
sizeof(WCHAR));
247 context->NTLMv2 = TRUE;
248 context->UseMIC = FALSE;
249 context->SendVersionInfo = TRUE;
250 context->SendSingleHostData = FALSE;
251 context->SendWorkstationName = TRUE;
252 context->NegotiateKeyExchange = TRUE;
253 context->UseSamFileDatabase = TRUE;
254 status = RegOpenKeyExA(HKEY_LOCAL_MACHINE, WINPR_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
256 if (status == ERROR_SUCCESS)
258 if (RegQueryValueEx(hKey, _T(
"NTLMv2"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) ==
260 context->NTLMv2 = dwValue ? 1 : 0;
262 if (RegQueryValueEx(hKey, _T(
"UseMIC"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) ==
264 context->UseMIC = dwValue ? 1 : 0;
266 if (RegQueryValueEx(hKey, _T(
"SendVersionInfo"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) ==
268 context->SendVersionInfo = dwValue ? 1 : 0;
270 if (RegQueryValueEx(hKey, _T(
"SendSingleHostData"), NULL, &dwType, (BYTE*)&dwValue,
271 &dwSize) == ERROR_SUCCESS)
272 context->SendSingleHostData = dwValue ? 1 : 0;
274 if (RegQueryValueEx(hKey, _T(
"SendWorkstationName"), NULL, &dwType, (BYTE*)&dwValue,
275 &dwSize) == ERROR_SUCCESS)
276 context->SendWorkstationName = dwValue ? 1 : 0;
278 if (RegQueryValueEx(hKey, _T(
"WorkstationName"), NULL, &dwType, NULL, &dwSize) ==
281 char* workstation = (
char*)malloc(dwSize + 1);
289 status = RegQueryValueExA(hKey,
"WorkstationName", NULL, &dwType, (BYTE*)workstation,
291 if (status != ERROR_SUCCESS)
292 WLog_WARN(TAG,
"Key ''WorkstationName' not found");
293 workstation[dwSize] =
'\0';
295 if (ntlm_SetContextWorkstation(context, workstation) < 0)
312 context->SuppressExtendedProtection = FALSE;
313 status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T(
"System\\CurrentControlSet\\Control\\LSA"), 0,
314 KEY_READ | KEY_WOW64_64KEY, &hKey);
316 if (status == ERROR_SUCCESS)
318 if (RegQueryValueEx(hKey, _T(
"SuppressExtendedProtection"), NULL, &dwType, (BYTE*)&dwValue,
319 &dwSize) == ERROR_SUCCESS)
320 context->SuppressExtendedProtection = dwValue ? 1 : 0;
325 context->NegotiateFlags = 0;
326 context->LmCompatibilityLevel = 3;
327 ntlm_change_state(context, NTLM_STATE_INITIAL);
328 FillMemory(context->MachineID,
sizeof(context->MachineID), 0xAA);
331 context->UseMIC = TRUE;
341 winpr_RC4_Free(context->SendRc4Seal);
342 winpr_RC4_Free(context->RecvRc4Seal);
343 sspi_SecBufferFree(&context->NegotiateMessage);
344 sspi_SecBufferFree(&context->ChallengeMessage);
345 sspi_SecBufferFree(&context->AuthenticateMessage);
346 sspi_SecBufferFree(&context->ChallengeTargetInfo);
347 sspi_SecBufferFree(&context->TargetName);
348 sspi_SecBufferFree(&context->NtChallengeResponse);
349 sspi_SecBufferFree(&context->LmChallengeResponse);
350 free(context->ServicePrincipalName.Buffer);
351 free(context->Workstation.Buffer);
355 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
356 SEC_WCHAR* pszPrincipal, SEC_WCHAR* pszPackage, ULONG fCredentialUse,
void* pvLogonID,
357 void* pAuthData, SEC_GET_KEY_FN pGetKeyFn,
void* pvGetKeyArgument,
PCredHandle phCredential,
362 if ((fCredentialUse != SECPKG_CRED_OUTBOUND) && (fCredentialUse != SECPKG_CRED_INBOUND) &&
363 (fCredentialUse != SECPKG_CRED_BOTH))
365 return SEC_E_INVALID_PARAMETER;
371 return SEC_E_INTERNAL_ERROR;
373 credentials->fCredentialUse = fCredentialUse;
374 credentials->pGetKeyFn = pGetKeyFn;
375 credentials->pvGetKeyArgument = pvGetKeyArgument;
379 UINT32 identityFlags = sspi_GetAuthIdentityFlags(pAuthData);
381 sspi_CopyAuthIdentity(&(credentials->identity),
384 if (identityFlags & SEC_WINNT_AUTH_IDENTITY_EXTENDED)
390 if (settings->samFile)
392 credentials->ntlmSettings.samFile = _strdup(settings->samFile);
393 if (!credentials->ntlmSettings.samFile)
395 sspi_CredentialsFree(credentials);
396 return SEC_E_INSUFFICIENT_MEMORY;
399 credentials->ntlmSettings.hashCallback = settings->hashCallback;
400 credentials->ntlmSettings.hashCallbackArg = settings->hashCallbackArg;
403 sspi_SecureHandleSetLowerPointer(phCredential, (
void*)credentials);
404 sspi_SecureHandleSetUpperPointer(phCredential, (
void*)NTLM_PACKAGE_NAME);
408 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
409 SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage, ULONG fCredentialUse,
void* pvLogonID,
410 void* pAuthData, SEC_GET_KEY_FN pGetKeyFn,
void* pvGetKeyArgument,
PCredHandle phCredential,
413 SECURITY_STATUS status = SEC_E_INSUFFICIENT_MEMORY;
414 SEC_WCHAR* principal = NULL;
415 SEC_WCHAR*
package = NULL;
419 principal = ConvertUtf8ToWCharAlloc(pszPrincipal, NULL);
425 package = ConvertUtf8ToWCharAlloc(pszPackage, NULL);
431 ntlm_AcquireCredentialsHandleW(principal, package, fCredentialUse, pvLogonID, pAuthData,
432 pGetKeyFn, pvGetKeyArgument, phCredential, ptsExpiry);
441 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
PCredHandle phCredential)
444 return SEC_E_INVALID_HANDLE;
450 return SEC_E_INVALID_HANDLE;
452 sspi_CredentialsFree(credentials);
456 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
PCredHandle phCredential,
457 ULONG ulAttribute,
void* pBuffer)
459 if (ulAttribute == SECPKG_CRED_ATTR_NAMES)
464 WLog_ERR(TAG,
"TODO: Implement");
465 return SEC_E_UNSUPPORTED_FUNCTION;
468 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
PCredHandle phCredential,
469 ULONG ulAttribute,
void* pBuffer)
471 return ntlm_QueryCredentialsAttributesW(phCredential, ulAttribute, pBuffer);
477 static SECURITY_STATUS SEC_ENTRY
479 ULONG fContextReq, ULONG TargetDataRep,
PCtxtHandle phNewContext,
482 SECURITY_STATUS status = 0;
488 if (phContext && !phContext->dwLower && !phContext->dwUpper)
489 return SEC_E_INVALID_HANDLE;
495 context = ntlm_ContextNew();
498 return SEC_E_INSUFFICIENT_MEMORY;
500 context->server = TRUE;
502 if (fContextReq & ASC_REQ_CONFIDENTIALITY)
503 context->confidentiality = TRUE;
505 credentials = (
SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
506 context->credentials = credentials;
507 context->SamFile = credentials->ntlmSettings.samFile;
508 context->HashCallback = credentials->ntlmSettings.hashCallback;
509 context->HashCallbackArg = credentials->ntlmSettings.hashCallbackArg;
511 ntlm_SetContextTargetName(context, NULL);
512 sspi_SecureHandleSetLowerPointer(phNewContext, context);
513 sspi_SecureHandleSetUpperPointer(phNewContext, (
void*)NTLM_PACKAGE_NAME);
516 switch (ntlm_get_state(context))
518 case NTLM_STATE_INITIAL:
520 ntlm_change_state(context, NTLM_STATE_NEGOTIATE);
523 return SEC_E_INVALID_TOKEN;
525 if (pInput->cBuffers < 1)
526 return SEC_E_INVALID_TOKEN;
528 input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
531 return SEC_E_INVALID_TOKEN;
533 if (input_buffer->cbBuffer < 1)
534 return SEC_E_INVALID_TOKEN;
536 status = ntlm_read_NegotiateMessage(context, input_buffer);
537 if (status != SEC_I_CONTINUE_NEEDED)
540 if (ntlm_get_state(context) == NTLM_STATE_CHALLENGE)
543 return SEC_E_INVALID_TOKEN;
545 if (pOutput->cBuffers < 1)
546 return SEC_E_INVALID_TOKEN;
548 output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
550 if (!output_buffer->BufferType)
551 return SEC_E_INVALID_TOKEN;
553 if (output_buffer->cbBuffer < 1)
554 return SEC_E_INSUFFICIENT_MEMORY;
556 return ntlm_write_ChallengeMessage(context, output_buffer);
559 return SEC_E_OUT_OF_SEQUENCE;
562 case NTLM_STATE_AUTHENTICATE:
565 return SEC_E_INVALID_TOKEN;
567 if (pInput->cBuffers < 1)
568 return SEC_E_INVALID_TOKEN;
570 input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
573 return SEC_E_INVALID_TOKEN;
575 if (input_buffer->cbBuffer < 1)
576 return SEC_E_INVALID_TOKEN;
578 status = ntlm_read_AuthenticateMessage(context, input_buffer);
582 for (ULONG i = 0; i < pOutput->cBuffers; i++)
584 pOutput->pBuffers[i].cbBuffer = 0;
585 pOutput->pBuffers[i].BufferType = SECBUFFER_TOKEN;
593 return SEC_E_OUT_OF_SEQUENCE;
597 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(
PCtxtHandle phContext)
602 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
604 ULONG Reserved1, ULONG TargetDataRep,
PSecBufferDesc pInput, ULONG Reserved2,
607 SECURITY_STATUS status = 0;
613 if (phContext && !phContext->dwLower && !phContext->dwUpper)
614 return SEC_E_INVALID_HANDLE;
620 input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
625 context = ntlm_ContextNew();
628 return SEC_E_INSUFFICIENT_MEMORY;
630 if (fContextReq & ISC_REQ_CONFIDENTIALITY)
631 context->confidentiality = TRUE;
633 credentials = (
SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
634 context->credentials = credentials;
636 if (context->Workstation.Length < 1)
638 if (ntlm_SetContextWorkstation(context, NULL) < 0)
640 ntlm_ContextFree(context);
641 return SEC_E_INTERNAL_ERROR;
645 if (ntlm_SetContextServicePrincipalNameW(context, pszTargetName) < 0)
647 ntlm_ContextFree(context);
648 return SEC_E_INTERNAL_ERROR;
651 sspi_SecureHandleSetLowerPointer(phNewContext, context);
652 sspi_SecureHandleSetUpperPointer(phNewContext, NTLM_SSP_NAME);
655 if ((!input_buffer) || (ntlm_get_state(context) == NTLM_STATE_AUTHENTICATE))
658 return SEC_E_INVALID_TOKEN;
660 if (pOutput->cBuffers < 1)
661 return SEC_E_INVALID_TOKEN;
663 output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
666 return SEC_E_INVALID_TOKEN;
668 if (output_buffer->cbBuffer < 1)
669 return SEC_E_INVALID_TOKEN;
671 if (ntlm_get_state(context) == NTLM_STATE_INITIAL)
672 ntlm_change_state(context, NTLM_STATE_NEGOTIATE);
674 if (ntlm_get_state(context) == NTLM_STATE_NEGOTIATE)
675 return ntlm_write_NegotiateMessage(context, output_buffer);
677 return SEC_E_OUT_OF_SEQUENCE;
682 return SEC_E_INVALID_TOKEN;
684 if (input_buffer->cbBuffer < 1)
685 return SEC_E_INVALID_TOKEN;
687 PSecBuffer channel_bindings = sspi_FindSecBuffer(pInput, SECBUFFER_CHANNEL_BINDINGS);
689 if (channel_bindings)
691 context->Bindings.BindingsLength = channel_bindings->cbBuffer;
695 if (ntlm_get_state(context) == NTLM_STATE_CHALLENGE)
697 status = ntlm_read_ChallengeMessage(context, input_buffer);
699 if (status != SEC_I_CONTINUE_NEEDED)
703 return SEC_E_INVALID_TOKEN;
705 if (pOutput->cBuffers < 1)
706 return SEC_E_INVALID_TOKEN;
708 output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
711 return SEC_E_INVALID_TOKEN;
713 if (output_buffer->cbBuffer < 1)
714 return SEC_E_INSUFFICIENT_MEMORY;
716 if (ntlm_get_state(context) == NTLM_STATE_AUTHENTICATE)
717 return ntlm_write_AuthenticateMessage(context, output_buffer);
720 return SEC_E_OUT_OF_SEQUENCE;
723 return SEC_E_OUT_OF_SEQUENCE;
729 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
731 ULONG Reserved1, ULONG TargetDataRep,
PSecBufferDesc pInput, ULONG Reserved2,
734 SECURITY_STATUS status = 0;
735 SEC_WCHAR* pszTargetNameW = NULL;
739 pszTargetNameW = ConvertUtf8ToWCharAlloc(pszTargetName, NULL);
741 return SEC_E_INTERNAL_ERROR;
744 status = ntlm_InitializeSecurityContextW(phCredential, phContext, pszTargetNameW, fContextReq,
745 Reserved1, TargetDataRep, pInput, Reserved2,
746 phNewContext, pOutput, pfContextAttr, ptsExpiry);
747 free(pszTargetNameW);
753 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(
PCtxtHandle phContext)
756 ntlm_ContextFree(context);
766 WINPR_ASSERT(ntproof);
768 target = &ntlm->ChallengeTargetInfo;
770 if (!sspi_SecBufferAlloc(ntproof, 36 + target->cbBuffer))
771 return SEC_E_INSUFFICIENT_MEMORY;
773 blob = (BYTE*)ntproof->pvBuffer;
774 CopyMemory(blob, ntlm->ServerChallenge, 8);
778 CopyMemory(&blob[16], ntlm->Timestamp, 8);
779 CopyMemory(&blob[24], ntlm->ClientChallenge, 8);
782 CopyMemory(&blob[36], target->pvBuffer, target->cbBuffer);
792 WINPR_ASSERT(micvalue);
794 msgSize = ntlm->NegotiateMessage.cbBuffer + ntlm->ChallengeMessage.cbBuffer +
795 ntlm->AuthenticateMessage.cbBuffer;
797 if (!sspi_SecBufferAlloc(micvalue, msgSize))
798 return SEC_E_INSUFFICIENT_MEMORY;
800 blob = (BYTE*)micvalue->pvBuffer;
801 CopyMemory(blob, ntlm->NegotiateMessage.pvBuffer, ntlm->NegotiateMessage.cbBuffer);
802 blob += ntlm->NegotiateMessage.cbBuffer;
803 CopyMemory(blob, ntlm->ChallengeMessage.pvBuffer, ntlm->ChallengeMessage.cbBuffer);
804 blob += ntlm->ChallengeMessage.cbBuffer;
805 CopyMemory(blob, ntlm->AuthenticateMessage.pvBuffer, ntlm->AuthenticateMessage.cbBuffer);
806 blob += ntlm->MessageIntegrityCheckOffset;
807 ZeroMemory(blob, 16);
813 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(
PCtxtHandle phContext,
814 ULONG ulAttribute,
void* pBuffer)
817 return SEC_E_INVALID_HANDLE;
820 return SEC_E_INSUFFICIENT_MEMORY;
823 if (!check_context(context))
824 return SEC_E_INVALID_HANDLE;
826 if (ulAttribute == SECPKG_ATTR_SIZES)
829 ContextSizes->cbMaxToken = 2010;
830 ContextSizes->cbMaxSignature = 16;
831 ContextSizes->cbBlockSize = 0;
832 ContextSizes->cbSecurityTrailer = 16;
836 else if (ulAttribute == SECPKG_ATTR_AUTH_IDENTITY)
842 WINPR_ASSERT(AuthIdentity);
843 *AuthIdentity = empty;
845 context->UseSamFileDatabase = FALSE;
846 credentials = context->credentials;
848 if (credentials->identity.UserLength > 0)
850 if (ConvertWCharNToUtf8(credentials->identity.User, credentials->identity.UserLength,
851 AuthIdentity->User, ARRAYSIZE(AuthIdentity->User)) <= 0)
852 return SEC_E_INTERNAL_ERROR;
855 if (credentials->identity.DomainLength > 0)
857 if (ConvertWCharNToUtf8(credentials->identity.Domain,
858 credentials->identity.DomainLength, AuthIdentity->Domain,
859 ARRAYSIZE(AuthIdentity->Domain)) <= 0)
860 return SEC_E_INTERNAL_ERROR;
865 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_NTPROOF_VALUE)
867 return ntlm_computeProofValue(context, (
SecBuffer*)pBuffer);
869 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_RANDKEY)
874 if (!sspi_SecBufferAlloc(randkey, 16))
875 return (SEC_E_INSUFFICIENT_MEMORY);
877 CopyMemory(randkey->pvBuffer, context->EncryptedRandomSessionKey, 16);
880 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_MIC)
885 if (!sspi_SecBufferAlloc(mic, 16))
886 return (SEC_E_INSUFFICIENT_MEMORY);
888 CopyMemory(mic->pvBuffer, message->MessageIntegrityCheck, 16);
891 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_MIC_VALUE)
893 return ntlm_computeMicValue(context, (
SecBuffer*)pBuffer);
896 WLog_ERR(TAG,
"TODO: Implement ulAttribute=0x%08" PRIx32, ulAttribute);
897 return SEC_E_UNSUPPORTED_FUNCTION;
900 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(
PCtxtHandle phContext,
901 ULONG ulAttribute,
void* pBuffer)
903 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
906 static SECURITY_STATUS SEC_ENTRY ntlm_SetContextAttributesW(
PCtxtHandle phContext,
907 ULONG ulAttribute,
void* pBuffer,
911 return SEC_E_INVALID_HANDLE;
914 return SEC_E_INVALID_PARAMETER;
918 return SEC_E_INVALID_HANDLE;
920 if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_HASH)
925 return SEC_E_INVALID_PARAMETER;
927 if (AuthNtlmHash->Version == 1)
928 CopyMemory(context->NtlmHash, AuthNtlmHash->NtlmHash, 16);
929 else if (AuthNtlmHash->Version == 2)
930 CopyMemory(context->NtlmV2Hash, AuthNtlmHash->NtlmHash, 16);
934 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_MESSAGE)
939 return SEC_E_INVALID_PARAMETER;
941 if (AuthNtlmMessage->type == 1)
943 sspi_SecBufferFree(&context->NegotiateMessage);
945 if (!sspi_SecBufferAlloc(&context->NegotiateMessage, AuthNtlmMessage->length))
946 return SEC_E_INSUFFICIENT_MEMORY;
948 CopyMemory(context->NegotiateMessage.pvBuffer, AuthNtlmMessage->buffer,
949 AuthNtlmMessage->length);
951 else if (AuthNtlmMessage->type == 2)
953 sspi_SecBufferFree(&context->ChallengeMessage);
955 if (!sspi_SecBufferAlloc(&context->ChallengeMessage, AuthNtlmMessage->length))
956 return SEC_E_INSUFFICIENT_MEMORY;
958 CopyMemory(context->ChallengeMessage.pvBuffer, AuthNtlmMessage->buffer,
959 AuthNtlmMessage->length);
961 else if (AuthNtlmMessage->type == 3)
963 sspi_SecBufferFree(&context->AuthenticateMessage);
965 if (!sspi_SecBufferAlloc(&context->AuthenticateMessage, AuthNtlmMessage->length))
966 return SEC_E_INSUFFICIENT_MEMORY;
968 CopyMemory(context->AuthenticateMessage.pvBuffer, AuthNtlmMessage->buffer,
969 AuthNtlmMessage->length);
974 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_TIMESTAMP)
980 return SEC_E_INVALID_PARAMETER;
982 if (AuthNtlmTimestamp->ChallengeOrResponse)
983 CopyMemory(context->ChallengeTimestamp, AuthNtlmTimestamp->Timestamp, 8);
985 CopyMemory(context->Timestamp, AuthNtlmTimestamp->Timestamp, 8);
989 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_CLIENT_CHALLENGE)
995 return SEC_E_INVALID_PARAMETER;
997 CopyMemory(context->ClientChallenge, AuthNtlmClientChallenge->ClientChallenge, 8);
1000 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_SERVER_CHALLENGE)
1006 return SEC_E_INVALID_PARAMETER;
1008 CopyMemory(context->ServerChallenge, AuthNtlmServerChallenge->ServerChallenge, 8);
1012 WLog_ERR(TAG,
"TODO: Implement ulAttribute=%08" PRIx32, ulAttribute);
1013 return SEC_E_UNSUPPORTED_FUNCTION;
1016 static SECURITY_STATUS SEC_ENTRY ntlm_SetContextAttributesA(
PCtxtHandle phContext,
1017 ULONG ulAttribute,
void* pBuffer,
1020 return ntlm_SetContextAttributesW(phContext, ulAttribute, pBuffer, cbBuffer);
1023 static SECURITY_STATUS SEC_ENTRY ntlm_SetCredentialsAttributesW(
PCredHandle phCredential,
1024 ULONG ulAttribute,
void* pBuffer,
1027 return SEC_E_UNSUPPORTED_FUNCTION;
1030 static SECURITY_STATUS SEC_ENTRY ntlm_SetCredentialsAttributesA(
PCredHandle phCredential,
1031 ULONG ulAttribute,
void* pBuffer,
1034 return SEC_E_UNSUPPORTED_FUNCTION;
1037 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(
PCtxtHandle phContext)
1042 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(
PCtxtHandle phContext, ULONG fQOP,
1045 const UINT32 SeqNo = MessageSeqNo;
1047 BYTE digest[WINPR_MD5_DIGEST_LENGTH] = { 0 };
1048 BYTE checksum[8] = { 0 };
1053 if (!check_context(context))
1054 return SEC_E_INVALID_HANDLE;
1056 for (ULONG index = 0; index < pMessage->cBuffers; index++)
1058 SecBuffer* cur = &pMessage->pBuffers[index];
1060 if (cur->BufferType & SECBUFFER_DATA)
1062 else if (cur->BufferType & SECBUFFER_TOKEN)
1063 signature_buffer = cur;
1067 return SEC_E_INVALID_TOKEN;
1069 if (!signature_buffer)
1070 return SEC_E_INVALID_TOKEN;
1073 ULONG length = data_buffer->cbBuffer;
1074 void* data = malloc(length);
1077 return SEC_E_INSUFFICIENT_MEMORY;
1079 CopyMemory(data, data_buffer->pvBuffer, length);
1081 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
1084 winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->SendSigningKey, WINPR_MD5_DIGEST_LENGTH))
1086 Data_Write_UINT32(&value, SeqNo);
1087 winpr_HMAC_Update(hmac, (
void*)&value, 4);
1088 winpr_HMAC_Update(hmac, data, length);
1089 winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH);
1090 winpr_HMAC_Free(hmac);
1094 winpr_HMAC_Free(hmac);
1096 return SEC_E_INSUFFICIENT_MEMORY;
1100 if ((data_buffer->BufferType & SECBUFFER_READONLY) == 0)
1102 if (context->confidentiality)
1103 winpr_RC4_Update(context->SendRc4Seal, length, (BYTE*)data,
1104 (BYTE*)data_buffer->pvBuffer);
1106 CopyMemory(data_buffer->pvBuffer, data, length);
1109 #ifdef WITH_DEBUG_NTLM
1110 WLog_DBG(TAG,
"Data Buffer (length = %" PRIuz
")", length);
1111 winpr_HexDump(TAG, WLOG_DEBUG, data, length);
1112 WLog_DBG(TAG,
"Encrypted Data Buffer (length = %" PRIu32
")", data_buffer->cbBuffer);
1113 winpr_HexDump(TAG, WLOG_DEBUG, data_buffer->pvBuffer, data_buffer->cbBuffer);
1117 winpr_RC4_Update(context->SendRc4Seal, 8, digest, checksum);
1118 if ((signature_buffer->BufferType & SECBUFFER_READONLY) == 0)
1120 BYTE* signature = signature_buffer->pvBuffer;
1122 Data_Write_UINT32(signature, version);
1123 CopyMemory(&signature[4], (
void*)checksum, 8);
1124 Data_Write_UINT32(&signature[12], SeqNo);
1126 context->SendSeqNum++;
1127 #ifdef WITH_DEBUG_NTLM
1128 WLog_DBG(TAG,
"Signature (length = %" PRIu32
")", signature_buffer->cbBuffer);
1129 winpr_HexDump(TAG, WLOG_DEBUG, signature_buffer->pvBuffer, signature_buffer->cbBuffer);
1135 ULONG MessageSeqNo, PULONG pfQOP)
1137 const UINT32 SeqNo = (UINT32)MessageSeqNo;
1139 BYTE digest[WINPR_MD5_DIGEST_LENGTH] = { 0 };
1140 BYTE checksum[8] = { 0 };
1142 BYTE expected_signature[WINPR_MD5_DIGEST_LENGTH] = { 0 };
1146 if (!check_context(context))
1147 return SEC_E_INVALID_HANDLE;
1149 for (ULONG index = 0; index < pMessage->cBuffers; index++)
1151 if (pMessage->pBuffers[index].BufferType == SECBUFFER_DATA)
1152 data_buffer = &pMessage->pBuffers[index];
1153 else if (pMessage->pBuffers[index].BufferType == SECBUFFER_TOKEN)
1154 signature_buffer = &pMessage->pBuffers[index];
1158 return SEC_E_INVALID_TOKEN;
1160 if (!signature_buffer)
1161 return SEC_E_INVALID_TOKEN;
1164 const ULONG length = data_buffer->cbBuffer;
1165 void* data = malloc(length);
1168 return SEC_E_INSUFFICIENT_MEMORY;
1170 CopyMemory(data, data_buffer->pvBuffer, length);
1174 if (context->confidentiality)
1175 winpr_RC4_Update(context->RecvRc4Seal, length, (BYTE*)data, (BYTE*)data_buffer->pvBuffer);
1177 CopyMemory(data_buffer->pvBuffer, data, length);
1180 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
1183 winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->RecvSigningKey, WINPR_MD5_DIGEST_LENGTH))
1185 Data_Write_UINT32(&value, SeqNo);
1186 winpr_HMAC_Update(hmac, (
void*)&value, 4);
1187 winpr_HMAC_Update(hmac, data_buffer->pvBuffer, data_buffer->cbBuffer);
1188 winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH);
1189 winpr_HMAC_Free(hmac);
1193 winpr_HMAC_Free(hmac);
1195 return SEC_E_INSUFFICIENT_MEMORY;
1198 #ifdef WITH_DEBUG_NTLM
1199 WLog_DBG(TAG,
"Encrypted Data Buffer (length = %" PRIuz
")", length);
1200 winpr_HexDump(TAG, WLOG_DEBUG, data, length);
1201 WLog_DBG(TAG,
"Data Buffer (length = %" PRIu32
")", data_buffer->cbBuffer);
1202 winpr_HexDump(TAG, WLOG_DEBUG, data_buffer->pvBuffer, data_buffer->cbBuffer);
1206 winpr_RC4_Update(context->RecvRc4Seal, 8, digest, checksum);
1208 Data_Write_UINT32(expected_signature, version);
1209 CopyMemory(&expected_signature[4], (
void*)checksum, 8);
1210 Data_Write_UINT32(&expected_signature[12], SeqNo);
1211 context->RecvSeqNum++;
1213 if (memcmp(signature_buffer->pvBuffer, expected_signature, 16) != 0)
1216 WLog_ERR(TAG,
"signature verification failed, something nasty is going on!");
1217 #ifdef WITH_DEBUG_NTLM
1218 WLog_ERR(TAG,
"Expected Signature:");
1219 winpr_HexDump(TAG, WLOG_ERROR, expected_signature, 16);
1220 WLog_ERR(TAG,
"Actual Signature:");
1221 winpr_HexDump(TAG, WLOG_ERROR, (BYTE*)signature_buffer->pvBuffer, 16);
1223 return SEC_E_MESSAGE_ALTERED;
1229 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(
PCtxtHandle phContext, ULONG fQOP,
1235 BYTE digest[WINPR_MD5_DIGEST_LENGTH] = { 0 };
1236 BYTE checksum[8] = { 0 };
1238 NTLM_CONTEXT* context = sspi_SecureHandleGetLowerPointer(phContext);
1239 if (!check_context(context))
1240 return SEC_E_INVALID_HANDLE;
1242 for (ULONG i = 0; i < pMessage->cBuffers; i++)
1244 if (pMessage->pBuffers[i].BufferType == SECBUFFER_DATA)
1245 data_buffer = &pMessage->pBuffers[i];
1246 else if (pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1247 sig_buffer = &pMessage->pBuffers[i];
1250 if (!data_buffer || !sig_buffer)
1251 return SEC_E_INVALID_TOKEN;
1253 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
1255 if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->SendSigningKey, WINPR_MD5_DIGEST_LENGTH))
1257 winpr_HMAC_Free(hmac);
1258 return SEC_E_INTERNAL_ERROR;
1261 Data_Write_UINT32(&seq_no, MessageSeqNo);
1262 winpr_HMAC_Update(hmac, (BYTE*)&seq_no, 4);
1263 winpr_HMAC_Update(hmac, data_buffer->pvBuffer, data_buffer->cbBuffer);
1264 winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH);
1265 winpr_HMAC_Free(hmac);
1267 winpr_RC4_Update(context->SendRc4Seal, 8, digest, checksum);
1269 BYTE* signature = sig_buffer->pvBuffer;
1270 Data_Write_UINT32(signature, 1L);
1271 CopyMemory(&signature[4], checksum, 8);
1272 Data_Write_UINT32(&signature[12], seq_no);
1273 sig_buffer->cbBuffer = 16;
1278 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(
PCtxtHandle phContext,
1285 BYTE digest[WINPR_MD5_DIGEST_LENGTH] = { 0 };
1286 BYTE checksum[8] = { 0 };
1287 BYTE signature[16] = { 0 };
1289 NTLM_CONTEXT* context = sspi_SecureHandleGetLowerPointer(phContext);
1290 if (!check_context(context))
1291 return SEC_E_INVALID_HANDLE;
1293 for (ULONG i = 0; i < pMessage->cBuffers; i++)
1295 if (pMessage->pBuffers[i].BufferType == SECBUFFER_DATA)
1296 data_buffer = &pMessage->pBuffers[i];
1297 else if (pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1298 sig_buffer = &pMessage->pBuffers[i];
1301 if (!data_buffer || !sig_buffer)
1302 return SEC_E_INVALID_TOKEN;
1304 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
1306 if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->RecvSigningKey, WINPR_MD5_DIGEST_LENGTH))
1308 winpr_HMAC_Free(hmac);
1309 return SEC_E_INTERNAL_ERROR;
1312 Data_Write_UINT32(&seq_no, MessageSeqNo);
1313 winpr_HMAC_Update(hmac, (BYTE*)&seq_no, 4);
1314 winpr_HMAC_Update(hmac, data_buffer->pvBuffer, data_buffer->cbBuffer);
1315 winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH);
1316 winpr_HMAC_Free(hmac);
1318 winpr_RC4_Update(context->RecvRc4Seal, 8, digest, checksum);
1320 Data_Write_UINT32(signature, 1L);
1321 CopyMemory(&signature[4], checksum, 8);
1322 Data_Write_UINT32(&signature[12], seq_no);
1324 if (memcmp(sig_buffer->pvBuffer, signature, 16) != 0)
1325 return SEC_E_MESSAGE_ALTERED;
1333 ntlm_QueryCredentialsAttributesA,
1334 ntlm_AcquireCredentialsHandleA,
1335 ntlm_FreeCredentialsHandle,
1337 ntlm_InitializeSecurityContextA,
1338 ntlm_AcceptSecurityContext,
1340 ntlm_DeleteSecurityContext,
1342 ntlm_QueryContextAttributesA,
1343 ntlm_ImpersonateSecurityContext,
1344 ntlm_RevertSecurityContext,
1346 ntlm_VerifySignature,
1356 ntlm_EncryptMessage,
1357 ntlm_DecryptMessage,
1358 ntlm_SetContextAttributesA,
1359 ntlm_SetCredentialsAttributesA,
1365 ntlm_QueryCredentialsAttributesW,
1366 ntlm_AcquireCredentialsHandleW,
1367 ntlm_FreeCredentialsHandle,
1369 ntlm_InitializeSecurityContextW,
1370 ntlm_AcceptSecurityContext,
1372 ntlm_DeleteSecurityContext,
1374 ntlm_QueryContextAttributesW,
1375 ntlm_ImpersonateSecurityContext,
1376 ntlm_RevertSecurityContext,
1378 ntlm_VerifySignature,
1388 ntlm_EncryptMessage,
1389 ntlm_DecryptMessage,
1390 ntlm_SetContextAttributesW,
1391 ntlm_SetCredentialsAttributesW,
1400 "NTLM Security Package"
1403 static WCHAR NTLM_SecPkgInfoW_NameBuffer[32] = { 0 };
1404 static WCHAR NTLM_SecPkgInfoW_CommentBuffer[32] = { 0 };
1411 NTLM_SecPkgInfoW_NameBuffer,
1412 NTLM_SecPkgInfoW_CommentBuffer
1415 char* ntlm_negotiate_flags_string(
char* buffer,
size_t size, UINT32 flags)
1417 if (!buffer || (size == 0))
1420 (void)_snprintf(buffer, size,
"[0x%08" PRIx32
"] ", flags);
1422 for (
int x = 0; x < 31; x++)
1424 const UINT32 mask = 1 << x;
1425 size_t len = strnlen(buffer, size);
1428 const char* str = ntlm_get_negotiate_string(mask);
1429 const size_t flen = strlen(str);
1431 if ((len > 0) && (buffer[len - 1] !=
' '))
1435 winpr_str_append(
"|", buffer, size, NULL);
1439 if (size - len < flen)
1441 winpr_str_append(str, buffer, size, NULL);
1448 const char* ntlm_message_type_string(UINT32 messageType)
1450 switch (messageType)
1452 case MESSAGE_TYPE_NEGOTIATE:
1453 return "MESSAGE_TYPE_NEGOTIATE";
1454 case MESSAGE_TYPE_CHALLENGE:
1455 return "MESSAGE_TYPE_CHALLENGE";
1456 case MESSAGE_TYPE_AUTHENTICATE:
1457 return "MESSAGE_TYPE_AUTHENTICATE";
1459 return "MESSAGE_TYPE_UNKNOWN";
1463 const char* ntlm_state_string(NTLM_STATE state)
1467 case NTLM_STATE_INITIAL:
1468 return "NTLM_STATE_INITIAL";
1469 case NTLM_STATE_NEGOTIATE:
1470 return "NTLM_STATE_NEGOTIATE";
1471 case NTLM_STATE_CHALLENGE:
1472 return "NTLM_STATE_CHALLENGE";
1473 case NTLM_STATE_AUTHENTICATE:
1474 return "NTLM_STATE_AUTHENTICATE";
1475 case NTLM_STATE_FINAL:
1476 return "NTLM_STATE_FINAL";
1478 return "NTLM_STATE_UNKNOWN";
1481 void ntlm_change_state(
NTLM_CONTEXT* ntlm, NTLM_STATE state)
1484 WLog_DBG(TAG,
"change state from %s to %s", ntlm_state_string(ntlm->state),
1485 ntlm_state_string(state));
1486 ntlm->state = state;
1495 BOOL ntlm_reset_cipher_state(
PSecHandle phContext)
1497 NTLM_CONTEXT* context = sspi_SecureHandleGetLowerPointer(phContext);
1501 check_context(context);
1502 winpr_RC4_Free(context->SendRc4Seal);
1503 winpr_RC4_Free(context->RecvRc4Seal);
1504 context->SendRc4Seal = winpr_RC4_New(context->RecvSealingKey, 16);
1505 context->RecvRc4Seal = winpr_RC4_New(context->SendSealingKey, 16);
1507 if (!context->SendRc4Seal)
1509 WLog_ERR(TAG,
"Failed to allocate context->SendRc4Seal");
1512 if (!context->RecvRc4Seal)
1514 WLog_ERR(TAG,
"Failed to allocate context->RecvRc4Seal");
1522 BOOL NTLM_init(
void)
1524 InitializeConstWCharFromUtf8(NTLM_SecPkgInfoA.Name, NTLM_SecPkgInfoW_NameBuffer,
1525 ARRAYSIZE(NTLM_SecPkgInfoW_NameBuffer));
1526 InitializeConstWCharFromUtf8(NTLM_SecPkgInfoA.Comment, NTLM_SecPkgInfoW_CommentBuffer,
1527 ARRAYSIZE(NTLM_SecPkgInfoW_CommentBuffer));