20#include <winpr/config.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"
40#define TAG WINPR_TAG("sspi.NTLM")
42#define WINPR_KEY "Software\\" WINPR_VENDOR_STRING "\\" WINPR_PRODUCT_STRING "\\WinPR\\NTLM"
44static char* NTLM_PACKAGE_NAME =
"NTLM";
46#define check_context(ctx) check_context_((ctx), __FILE__, __func__, __LINE__)
47static 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_PrintTextMessage(log, log_level, line, file, fkt,
"invalid context");
61 if (!context->RecvRc4Seal)
63 if (WLog_IsLevelActive(log, log_level))
64 WLog_PrintTextMessage(log, log_level, line, file, fkt,
"invalid context->RecvRc4Seal");
67 if (!context->SendRc4Seal)
69 if (WLog_IsLevelActive(log, log_level))
70 WLog_PrintTextMessage(log, log_level, line, file, fkt,
"invalid context->SendRc4Seal");
74 if (!context->SendSigningKey)
76 if (WLog_IsLevelActive(log, log_level))
77 WLog_PrintTextMessage(log, log_level, line, file, fkt,
78 "invalid context->SendSigningKey");
81 if (!context->RecvSigningKey)
83 if (WLog_IsLevelActive(log, log_level))
84 WLog_PrintTextMessage(log, log_level, line, file, fkt,
85 "invalid context->RecvSigningKey");
88 if (!context->SendSealingKey)
90 if (WLog_IsLevelActive(log, log_level))
91 WLog_PrintTextMessage(log, log_level, line, file, fkt,
92 "invalid context->SendSealingKey");
95 if (!context->RecvSealingKey)
97 if (WLog_IsLevelActive(log, log_level))
98 WLog_PrintTextMessage(log, log_level, line, file, fkt,
99 "invalid context->RecvSealingKey");
105static char* get_name(COMPUTER_NAME_FORMAT type)
109 if (GetComputerNameExA(type, NULL, &nSize))
112 if (GetLastError() != ERROR_MORE_DATA)
115 char* computerName = calloc(1, nSize);
120 if (!GetComputerNameExA(type, computerName, &nSize))
129static int ntlm_SetContextWorkstation(
NTLM_CONTEXT* context,
char* Workstation)
131 char* ws = Workstation;
132 CHAR* computerName = NULL;
134 WINPR_ASSERT(context);
138 computerName = get_name(ComputerNameNetBIOS);
145 context->Workstation.Buffer = ConvertUtf8ToWCharAlloc(ws, &len);
149 if (!context->Workstation.Buffer || (len > UINT16_MAX /
sizeof(WCHAR)))
152 context->Workstation.Length = (USHORT)(len *
sizeof(WCHAR));
156static int ntlm_SetContextServicePrincipalNameW(
NTLM_CONTEXT* context, LPWSTR ServicePrincipalName)
158 WINPR_ASSERT(context);
160 if (!ServicePrincipalName)
162 context->ServicePrincipalName.Buffer = NULL;
163 context->ServicePrincipalName.Length = 0;
167 context->ServicePrincipalName.Length = (USHORT)(_wcslen(ServicePrincipalName) * 2);
168 context->ServicePrincipalName.Buffer = (PWSTR)malloc(context->ServicePrincipalName.Length + 2);
170 if (!context->ServicePrincipalName.Buffer)
173 memcpy(context->ServicePrincipalName.Buffer, ServicePrincipalName,
174 context->ServicePrincipalName.Length + 2);
178static int ntlm_SetContextTargetName(
NTLM_CONTEXT* context,
char* TargetName)
180 char* name = TargetName;
182 CHAR* computerName = NULL;
184 WINPR_ASSERT(context);
188 if (GetComputerNameExA(ComputerNameNetBIOS, NULL, &nSize) ||
189 GetLastError() != ERROR_MORE_DATA)
192 computerName = calloc(nSize,
sizeof(CHAR));
197 if (!GetComputerNameExA(ComputerNameNetBIOS, computerName, &nSize))
203 if (nSize > MAX_COMPUTERNAME_LENGTH)
204 computerName[MAX_COMPUTERNAME_LENGTH] =
'\0';
215 context->TargetName.pvBuffer = ConvertUtf8ToWCharAlloc(name, &len);
217 if (!context->TargetName.pvBuffer || (len > UINT16_MAX /
sizeof(WCHAR)))
219 free(context->TargetName.pvBuffer);
220 context->TargetName.pvBuffer = NULL;
228 context->TargetName.cbBuffer = (USHORT)(len *
sizeof(WCHAR));
248 context->NTLMv2 = TRUE;
249 context->UseMIC = FALSE;
250 context->SendVersionInfo = TRUE;
251 context->SendSingleHostData = FALSE;
252 context->SendWorkstationName = TRUE;
253 context->NegotiateKeyExchange = TRUE;
254 context->UseSamFileDatabase = TRUE;
255 status = RegOpenKeyExA(HKEY_LOCAL_MACHINE, WINPR_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
257 if (status == ERROR_SUCCESS)
259 if (RegQueryValueEx(hKey, _T(
"NTLMv2"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) ==
261 context->NTLMv2 = dwValue ? 1 : 0;
263 if (RegQueryValueEx(hKey, _T(
"UseMIC"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) ==
265 context->UseMIC = dwValue ? 1 : 0;
267 if (RegQueryValueEx(hKey, _T(
"SendVersionInfo"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) ==
269 context->SendVersionInfo = dwValue ? 1 : 0;
271 if (RegQueryValueEx(hKey, _T(
"SendSingleHostData"), NULL, &dwType, (BYTE*)&dwValue,
272 &dwSize) == ERROR_SUCCESS)
273 context->SendSingleHostData = dwValue ? 1 : 0;
275 if (RegQueryValueEx(hKey, _T(
"SendWorkstationName"), NULL, &dwType, (BYTE*)&dwValue,
276 &dwSize) == ERROR_SUCCESS)
277 context->SendWorkstationName = dwValue ? 1 : 0;
279 if (RegQueryValueEx(hKey, _T(
"WorkstationName"), NULL, &dwType, NULL, &dwSize) ==
282 char* workstation = (
char*)malloc(dwSize + 1);
290 status = RegQueryValueExA(hKey,
"WorkstationName", NULL, &dwType, (BYTE*)workstation,
292 if (status != ERROR_SUCCESS)
293 WLog_WARN(TAG,
"Key ''WorkstationName' not found");
294 workstation[dwSize] =
'\0';
296 if (ntlm_SetContextWorkstation(context, workstation) < 0)
313 context->SuppressExtendedProtection = FALSE;
314 status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T(
"System\\CurrentControlSet\\Control\\LSA"), 0,
315 KEY_READ | KEY_WOW64_64KEY, &hKey);
317 if (status == ERROR_SUCCESS)
319 if (RegQueryValueEx(hKey, _T(
"SuppressExtendedProtection"), NULL, &dwType, (BYTE*)&dwValue,
320 &dwSize) == ERROR_SUCCESS)
321 context->SuppressExtendedProtection = dwValue ? 1 : 0;
326 context->NegotiateFlags = 0;
327 context->LmCompatibilityLevel = 3;
328 ntlm_change_state(context, NTLM_STATE_INITIAL);
329 FillMemory(context->MachineID,
sizeof(context->MachineID), 0xAA);
332 context->UseMIC = TRUE;
342 winpr_RC4_Free(context->SendRc4Seal);
343 winpr_RC4_Free(context->RecvRc4Seal);
344 sspi_SecBufferFree(&context->NegotiateMessage);
345 sspi_SecBufferFree(&context->ChallengeMessage);
346 sspi_SecBufferFree(&context->AuthenticateMessage);
347 sspi_SecBufferFree(&context->ChallengeTargetInfo);
348 sspi_SecBufferFree(&context->TargetName);
349 sspi_SecBufferFree(&context->NtChallengeResponse);
350 sspi_SecBufferFree(&context->LmChallengeResponse);
351 free(context->ServicePrincipalName.Buffer);
352 free(context->Workstation.Buffer);
356static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
357 WINPR_ATTR_UNUSED SEC_WCHAR* pszPrincipal, WINPR_ATTR_UNUSED SEC_WCHAR* pszPackage,
358 ULONG fCredentialUse, WINPR_ATTR_UNUSED
void* pvLogonID,
void* pAuthData,
359 SEC_GET_KEY_FN pGetKeyFn,
void* pvGetKeyArgument,
PCredHandle phCredential,
364 if ((fCredentialUse != SECPKG_CRED_OUTBOUND) && (fCredentialUse != SECPKG_CRED_INBOUND) &&
365 (fCredentialUse != SECPKG_CRED_BOTH))
367 return SEC_E_INVALID_PARAMETER;
373 return SEC_E_INTERNAL_ERROR;
375 credentials->fCredentialUse = fCredentialUse;
376 credentials->pGetKeyFn = pGetKeyFn;
377 credentials->pvGetKeyArgument = pvGetKeyArgument;
381 UINT32 identityFlags = sspi_GetAuthIdentityFlags(pAuthData);
383 sspi_CopyAuthIdentity(&(credentials->identity),
386 if (identityFlags & SEC_WINNT_AUTH_IDENTITY_EXTENDED)
392 if (settings->samFile)
394 credentials->ntlmSettings.samFile = _strdup(settings->samFile);
395 if (!credentials->ntlmSettings.samFile)
397 sspi_CredentialsFree(credentials);
398 return SEC_E_INSUFFICIENT_MEMORY;
401 credentials->ntlmSettings.hashCallback = settings->hashCallback;
402 credentials->ntlmSettings.hashCallbackArg = settings->hashCallbackArg;
405 sspi_SecureHandleSetLowerPointer(phCredential, (
void*)credentials);
406 sspi_SecureHandleSetUpperPointer(phCredential, (
void*)NTLM_PACKAGE_NAME);
410static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
411 SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage, ULONG fCredentialUse,
void* pvLogonID,
412 void* pAuthData, SEC_GET_KEY_FN pGetKeyFn,
void* pvGetKeyArgument,
PCredHandle phCredential,
415 SECURITY_STATUS status = SEC_E_INSUFFICIENT_MEMORY;
416 SEC_WCHAR* principal = NULL;
417 SEC_WCHAR*
package = NULL;
421 principal = ConvertUtf8ToWCharAlloc(pszPrincipal, NULL);
427 package = ConvertUtf8ToWCharAlloc(pszPackage, NULL);
433 ntlm_AcquireCredentialsHandleW(principal, package, fCredentialUse, pvLogonID, pAuthData,
434 pGetKeyFn, pvGetKeyArgument, phCredential, ptsExpiry);
443static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
PCredHandle phCredential)
446 return SEC_E_INVALID_HANDLE;
452 return SEC_E_INVALID_HANDLE;
454 sspi_CredentialsFree(credentials);
458static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
459 WINPR_ATTR_UNUSED
PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute,
460 WINPR_ATTR_UNUSED
void* pBuffer)
462 if (ulAttribute == SECPKG_CRED_ATTR_NAMES)
467 WLog_ERR(TAG,
"TODO: Implement");
468 return SEC_E_UNSUPPORTED_FUNCTION;
471static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
PCredHandle phCredential,
472 ULONG ulAttribute,
void* pBuffer)
474 return ntlm_QueryCredentialsAttributesW(phCredential, ulAttribute, pBuffer);
480static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
483 WINPR_ATTR_UNUSED PULONG pfContextAttr, WINPR_ATTR_UNUSED
PTimeStamp ptsTimeStamp)
485 SECURITY_STATUS status = 0;
491 if (phContext && !phContext->dwLower && !phContext->dwUpper)
492 return SEC_E_INVALID_HANDLE;
498 context = ntlm_ContextNew();
501 return SEC_E_INSUFFICIENT_MEMORY;
503 context->server = TRUE;
505 if (fContextReq & ASC_REQ_CONFIDENTIALITY)
506 context->confidentiality = TRUE;
508 credentials = (
SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
509 context->credentials = credentials;
510 context->SamFile = credentials->ntlmSettings.samFile;
511 context->HashCallback = credentials->ntlmSettings.hashCallback;
512 context->HashCallbackArg = credentials->ntlmSettings.hashCallbackArg;
514 ntlm_SetContextTargetName(context, NULL);
515 sspi_SecureHandleSetLowerPointer(phNewContext, context);
516 sspi_SecureHandleSetUpperPointer(phNewContext, (
void*)NTLM_PACKAGE_NAME);
519 switch (ntlm_get_state(context))
521 case NTLM_STATE_INITIAL:
523 ntlm_change_state(context, NTLM_STATE_NEGOTIATE);
526 return SEC_E_INVALID_TOKEN;
528 if (pInput->cBuffers < 1)
529 return SEC_E_INVALID_TOKEN;
531 input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
534 return SEC_E_INVALID_TOKEN;
536 if (input_buffer->cbBuffer < 1)
537 return SEC_E_INVALID_TOKEN;
539 status = ntlm_read_NegotiateMessage(context, input_buffer);
540 if (status != SEC_I_CONTINUE_NEEDED)
543 if (ntlm_get_state(context) == NTLM_STATE_CHALLENGE)
546 return SEC_E_INVALID_TOKEN;
548 if (pOutput->cBuffers < 1)
549 return SEC_E_INVALID_TOKEN;
551 output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
553 if (!output_buffer->BufferType)
554 return SEC_E_INVALID_TOKEN;
556 if (output_buffer->cbBuffer < 1)
557 return SEC_E_INSUFFICIENT_MEMORY;
559 return ntlm_write_ChallengeMessage(context, output_buffer);
562 return SEC_E_OUT_OF_SEQUENCE;
565 case NTLM_STATE_AUTHENTICATE:
568 return SEC_E_INVALID_TOKEN;
570 if (pInput->cBuffers < 1)
571 return SEC_E_INVALID_TOKEN;
573 input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
576 return SEC_E_INVALID_TOKEN;
578 if (input_buffer->cbBuffer < 1)
579 return SEC_E_INVALID_TOKEN;
581 status = ntlm_read_AuthenticateMessage(context, input_buffer);
585 for (ULONG i = 0; i < pOutput->cBuffers; i++)
587 pOutput->pBuffers[i].cbBuffer = 0;
588 pOutput->pBuffers[i].BufferType = SECBUFFER_TOKEN;
596 return SEC_E_OUT_OF_SEQUENCE;
600static SECURITY_STATUS SEC_ENTRY
601ntlm_ImpersonateSecurityContext(WINPR_ATTR_UNUSED
PCtxtHandle phContext)
606static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
608 WINPR_ATTR_UNUSED ULONG Reserved1, WINPR_ATTR_UNUSED ULONG TargetDataRep,
PSecBufferDesc pInput,
610 WINPR_ATTR_UNUSED PULONG pfContextAttr, WINPR_ATTR_UNUSED
PTimeStamp ptsExpiry)
612 SECURITY_STATUS status = 0;
618 if (phContext && !phContext->dwLower && !phContext->dwUpper)
619 return SEC_E_INVALID_HANDLE;
625 input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
630 context = ntlm_ContextNew();
633 return SEC_E_INSUFFICIENT_MEMORY;
635 if (fContextReq & ISC_REQ_CONFIDENTIALITY)
636 context->confidentiality = TRUE;
638 credentials = (
SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
639 context->credentials = credentials;
641 if (context->Workstation.Length < 1)
643 if (ntlm_SetContextWorkstation(context, NULL) < 0)
645 ntlm_ContextFree(context);
646 return SEC_E_INTERNAL_ERROR;
650 if (ntlm_SetContextServicePrincipalNameW(context, pszTargetName) < 0)
652 ntlm_ContextFree(context);
653 return SEC_E_INTERNAL_ERROR;
656 sspi_SecureHandleSetLowerPointer(phNewContext, context);
657 sspi_SecureHandleSetUpperPointer(phNewContext, NTLM_SSP_NAME);
660 if ((!input_buffer) || (ntlm_get_state(context) == NTLM_STATE_AUTHENTICATE))
663 return SEC_E_INVALID_TOKEN;
665 if (pOutput->cBuffers < 1)
666 return SEC_E_INVALID_TOKEN;
668 output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
671 return SEC_E_INVALID_TOKEN;
673 if (output_buffer->cbBuffer < 1)
674 return SEC_E_INVALID_TOKEN;
676 if (ntlm_get_state(context) == NTLM_STATE_INITIAL)
677 ntlm_change_state(context, NTLM_STATE_NEGOTIATE);
679 if (ntlm_get_state(context) == NTLM_STATE_NEGOTIATE)
680 return ntlm_write_NegotiateMessage(context, output_buffer);
682 return SEC_E_OUT_OF_SEQUENCE;
687 return SEC_E_INVALID_TOKEN;
689 if (input_buffer->cbBuffer < 1)
690 return SEC_E_INVALID_TOKEN;
692 PSecBuffer channel_bindings = sspi_FindSecBuffer(pInput, SECBUFFER_CHANNEL_BINDINGS);
694 if (channel_bindings)
696 context->Bindings.BindingsLength = channel_bindings->cbBuffer;
700 if (ntlm_get_state(context) == NTLM_STATE_CHALLENGE)
702 status = ntlm_read_ChallengeMessage(context, input_buffer);
704 if (status != SEC_I_CONTINUE_NEEDED)
708 return SEC_E_INVALID_TOKEN;
710 if (pOutput->cBuffers < 1)
711 return SEC_E_INVALID_TOKEN;
713 output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
716 return SEC_E_INVALID_TOKEN;
718 if (output_buffer->cbBuffer < 1)
719 return SEC_E_INSUFFICIENT_MEMORY;
721 if (ntlm_get_state(context) == NTLM_STATE_AUTHENTICATE)
722 return ntlm_write_AuthenticateMessage(context, output_buffer);
725 return SEC_E_OUT_OF_SEQUENCE;
728 return SEC_E_OUT_OF_SEQUENCE;
734static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
736 ULONG Reserved1, ULONG TargetDataRep,
PSecBufferDesc pInput, ULONG Reserved2,
739 SECURITY_STATUS status = 0;
740 SEC_WCHAR* pszTargetNameW = NULL;
744 pszTargetNameW = ConvertUtf8ToWCharAlloc(pszTargetName, NULL);
746 return SEC_E_INTERNAL_ERROR;
749 status = ntlm_InitializeSecurityContextW(phCredential, phContext, pszTargetNameW, fContextReq,
750 Reserved1, TargetDataRep, pInput, Reserved2,
751 phNewContext, pOutput, pfContextAttr, ptsExpiry);
752 free(pszTargetNameW);
758static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(
PCtxtHandle phContext)
761 ntlm_ContextFree(context);
771 WINPR_ASSERT(ntproof);
773 target = &ntlm->ChallengeTargetInfo;
775 if (!sspi_SecBufferAlloc(ntproof, 36 + target->cbBuffer))
776 return SEC_E_INSUFFICIENT_MEMORY;
778 blob = (BYTE*)ntproof->pvBuffer;
779 CopyMemory(blob, ntlm->ServerChallenge, 8);
783 CopyMemory(&blob[16], ntlm->Timestamp, 8);
784 CopyMemory(&blob[24], ntlm->ClientChallenge, 8);
787 CopyMemory(&blob[36], target->pvBuffer, target->cbBuffer);
797 WINPR_ASSERT(micvalue);
799 msgSize = ntlm->NegotiateMessage.cbBuffer + ntlm->ChallengeMessage.cbBuffer +
800 ntlm->AuthenticateMessage.cbBuffer;
802 if (!sspi_SecBufferAlloc(micvalue, msgSize))
803 return SEC_E_INSUFFICIENT_MEMORY;
805 blob = (BYTE*)micvalue->pvBuffer;
806 CopyMemory(blob, ntlm->NegotiateMessage.pvBuffer, ntlm->NegotiateMessage.cbBuffer);
807 blob += ntlm->NegotiateMessage.cbBuffer;
808 CopyMemory(blob, ntlm->ChallengeMessage.pvBuffer, ntlm->ChallengeMessage.cbBuffer);
809 blob += ntlm->ChallengeMessage.cbBuffer;
810 CopyMemory(blob, ntlm->AuthenticateMessage.pvBuffer, ntlm->AuthenticateMessage.cbBuffer);
811 blob += ntlm->MessageIntegrityCheckOffset;
812 ZeroMemory(blob, 16);
818static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(
PCtxtHandle phContext,
819 ULONG ulAttribute,
void* pBuffer)
822 return SEC_E_INVALID_HANDLE;
825 return SEC_E_INSUFFICIENT_MEMORY;
828 if (!check_context(context))
829 return SEC_E_INVALID_HANDLE;
831 if (ulAttribute == SECPKG_ATTR_SIZES)
834 ContextSizes->cbMaxToken = 2010;
835 ContextSizes->cbMaxSignature = 16;
836 ContextSizes->cbBlockSize = 0;
837 ContextSizes->cbSecurityTrailer = 16;
841 else if (ulAttribute == SECPKG_ATTR_AUTH_IDENTITY)
847 WINPR_ASSERT(AuthIdentity);
848 *AuthIdentity = empty;
850 context->UseSamFileDatabase = FALSE;
851 credentials = context->credentials;
853 if (credentials->identity.UserLength > 0)
855 if (ConvertWCharNToUtf8(credentials->identity.User, credentials->identity.UserLength,
856 AuthIdentity->User, ARRAYSIZE(AuthIdentity->User)) <= 0)
857 return SEC_E_INTERNAL_ERROR;
860 if (credentials->identity.DomainLength > 0)
862 if (ConvertWCharNToUtf8(credentials->identity.Domain,
863 credentials->identity.DomainLength, AuthIdentity->Domain,
864 ARRAYSIZE(AuthIdentity->Domain)) <= 0)
865 return SEC_E_INTERNAL_ERROR;
870 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_NTPROOF_VALUE)
872 return ntlm_computeProofValue(context, (
SecBuffer*)pBuffer);
874 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_RANDKEY)
879 if (!sspi_SecBufferAlloc(randkey, 16))
880 return (SEC_E_INSUFFICIENT_MEMORY);
882 CopyMemory(randkey->pvBuffer, context->EncryptedRandomSessionKey, 16);
885 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_MIC)
890 if (!sspi_SecBufferAlloc(mic, 16))
891 return (SEC_E_INSUFFICIENT_MEMORY);
893 CopyMemory(mic->pvBuffer, message->MessageIntegrityCheck, 16);
896 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_MIC_VALUE)
898 return ntlm_computeMicValue(context, (
SecBuffer*)pBuffer);
901 WLog_ERR(TAG,
"TODO: Implement ulAttribute=0x%08" PRIx32, ulAttribute);
902 return SEC_E_UNSUPPORTED_FUNCTION;
905static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(
PCtxtHandle phContext,
906 ULONG ulAttribute,
void* pBuffer)
908 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
911static SECURITY_STATUS SEC_ENTRY ntlm_SetContextAttributesW(
PCtxtHandle phContext,
912 ULONG ulAttribute,
void* pBuffer,
916 return SEC_E_INVALID_HANDLE;
919 return SEC_E_INVALID_PARAMETER;
923 return SEC_E_INVALID_HANDLE;
925 if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_HASH)
930 return SEC_E_INVALID_PARAMETER;
932 if (AuthNtlmHash->Version == 1)
933 CopyMemory(context->NtlmHash, AuthNtlmHash->NtlmHash, 16);
934 else if (AuthNtlmHash->Version == 2)
935 CopyMemory(context->NtlmV2Hash, AuthNtlmHash->NtlmHash, 16);
939 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_MESSAGE)
944 return SEC_E_INVALID_PARAMETER;
946 if (AuthNtlmMessage->type == 1)
948 sspi_SecBufferFree(&context->NegotiateMessage);
950 if (!sspi_SecBufferAlloc(&context->NegotiateMessage, AuthNtlmMessage->length))
951 return SEC_E_INSUFFICIENT_MEMORY;
953 CopyMemory(context->NegotiateMessage.pvBuffer, AuthNtlmMessage->buffer,
954 AuthNtlmMessage->length);
956 else if (AuthNtlmMessage->type == 2)
958 sspi_SecBufferFree(&context->ChallengeMessage);
960 if (!sspi_SecBufferAlloc(&context->ChallengeMessage, AuthNtlmMessage->length))
961 return SEC_E_INSUFFICIENT_MEMORY;
963 CopyMemory(context->ChallengeMessage.pvBuffer, AuthNtlmMessage->buffer,
964 AuthNtlmMessage->length);
966 else if (AuthNtlmMessage->type == 3)
968 sspi_SecBufferFree(&context->AuthenticateMessage);
970 if (!sspi_SecBufferAlloc(&context->AuthenticateMessage, AuthNtlmMessage->length))
971 return SEC_E_INSUFFICIENT_MEMORY;
973 CopyMemory(context->AuthenticateMessage.pvBuffer, AuthNtlmMessage->buffer,
974 AuthNtlmMessage->length);
979 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_TIMESTAMP)
985 return SEC_E_INVALID_PARAMETER;
987 if (AuthNtlmTimestamp->ChallengeOrResponse)
988 CopyMemory(context->ChallengeTimestamp, AuthNtlmTimestamp->Timestamp, 8);
990 CopyMemory(context->Timestamp, AuthNtlmTimestamp->Timestamp, 8);
994 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_CLIENT_CHALLENGE)
1000 return SEC_E_INVALID_PARAMETER;
1002 CopyMemory(context->ClientChallenge, AuthNtlmClientChallenge->ClientChallenge, 8);
1005 else if (ulAttribute == SECPKG_ATTR_AUTH_NTLM_SERVER_CHALLENGE)
1011 return SEC_E_INVALID_PARAMETER;
1013 CopyMemory(context->ServerChallenge, AuthNtlmServerChallenge->ServerChallenge, 8);
1017 WLog_ERR(TAG,
"TODO: Implement ulAttribute=%08" PRIx32, ulAttribute);
1018 return SEC_E_UNSUPPORTED_FUNCTION;
1021static SECURITY_STATUS SEC_ENTRY ntlm_SetContextAttributesA(
PCtxtHandle phContext,
1022 ULONG ulAttribute,
void* pBuffer,
1025 return ntlm_SetContextAttributesW(phContext, ulAttribute, pBuffer, cbBuffer);
1028static SECURITY_STATUS SEC_ENTRY ntlm_SetCredentialsAttributesW(
1029 WINPR_ATTR_UNUSED
PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute,
1030 WINPR_ATTR_UNUSED
void* pBuffer, WINPR_ATTR_UNUSED ULONG cbBuffer)
1032 return SEC_E_UNSUPPORTED_FUNCTION;
1035static SECURITY_STATUS SEC_ENTRY ntlm_SetCredentialsAttributesA(
1036 WINPR_ATTR_UNUSED
PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute,
1037 WINPR_ATTR_UNUSED
void* pBuffer, WINPR_ATTR_UNUSED ULONG cbBuffer)
1039 return SEC_E_UNSUPPORTED_FUNCTION;
1042static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(WINPR_ATTR_UNUSED
PCtxtHandle phContext)
1047static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(
PCtxtHandle phContext,
1048 WINPR_ATTR_UNUSED ULONG fQOP,
1051 const UINT32 SeqNo = MessageSeqNo;
1053 BYTE digest[WINPR_MD5_DIGEST_LENGTH] = { 0 };
1054 BYTE checksum[8] = { 0 };
1059 if (!check_context(context))
1060 return SEC_E_INVALID_HANDLE;
1062 for (ULONG index = 0; index < pMessage->cBuffers; index++)
1064 SecBuffer* cur = &pMessage->pBuffers[index];
1066 if (cur->BufferType & SECBUFFER_DATA)
1068 else if (cur->BufferType & SECBUFFER_TOKEN)
1069 signature_buffer = cur;
1073 return SEC_E_INVALID_TOKEN;
1075 if (!signature_buffer)
1076 return SEC_E_INVALID_TOKEN;
1079 ULONG length = data_buffer->cbBuffer;
1080 void* data = malloc(length);
1083 return SEC_E_INSUFFICIENT_MEMORY;
1085 CopyMemory(data, data_buffer->pvBuffer, length);
1087 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
1090 winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->SendSigningKey, WINPR_MD5_DIGEST_LENGTH))
1092 winpr_Data_Write_UINT32(&value, SeqNo);
1093 winpr_HMAC_Update(hmac, (
void*)&value, 4);
1094 winpr_HMAC_Update(hmac, data, length);
1095 winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH);
1096 winpr_HMAC_Free(hmac);
1100 winpr_HMAC_Free(hmac);
1102 return SEC_E_INSUFFICIENT_MEMORY;
1106 if ((data_buffer->BufferType & SECBUFFER_READONLY) == 0)
1108 if (context->confidentiality)
1109 winpr_RC4_Update(context->SendRc4Seal, length, (BYTE*)data,
1110 (BYTE*)data_buffer->pvBuffer);
1112 CopyMemory(data_buffer->pvBuffer, data, length);
1115#ifdef WITH_DEBUG_NTLM
1116 WLog_DBG(TAG,
"Data Buffer (length = %" PRIuz
")", length);
1117 winpr_HexDump(TAG, WLOG_DEBUG, data, length);
1118 WLog_DBG(TAG,
"Encrypted Data Buffer (length = %" PRIu32
")", data_buffer->cbBuffer);
1119 winpr_HexDump(TAG, WLOG_DEBUG, data_buffer->pvBuffer, data_buffer->cbBuffer);
1123 winpr_RC4_Update(context->SendRc4Seal, 8, digest, checksum);
1124 if ((signature_buffer->BufferType & SECBUFFER_READONLY) == 0)
1126 BYTE* signature = signature_buffer->pvBuffer;
1128 winpr_Data_Write_UINT32(signature, version);
1129 CopyMemory(&signature[4], (
void*)checksum, 8);
1130 winpr_Data_Write_UINT32(&signature[12], SeqNo);
1132 context->SendSeqNum++;
1133#ifdef WITH_DEBUG_NTLM
1134 WLog_DBG(TAG,
"Signature (length = %" PRIu32
")", signature_buffer->cbBuffer);
1135 winpr_HexDump(TAG, WLOG_DEBUG, signature_buffer->pvBuffer, signature_buffer->cbBuffer);
1142 WINPR_ATTR_UNUSED PULONG pfQOP)
1144 const UINT32 SeqNo = (UINT32)MessageSeqNo;
1146 BYTE digest[WINPR_MD5_DIGEST_LENGTH] = { 0 };
1147 BYTE checksum[8] = { 0 };
1149 BYTE expected_signature[WINPR_MD5_DIGEST_LENGTH] = { 0 };
1153 if (!check_context(context))
1154 return SEC_E_INVALID_HANDLE;
1156 for (ULONG index = 0; index < pMessage->cBuffers; index++)
1158 if (pMessage->pBuffers[index].BufferType == SECBUFFER_DATA)
1159 data_buffer = &pMessage->pBuffers[index];
1160 else if (pMessage->pBuffers[index].BufferType == SECBUFFER_TOKEN)
1161 signature_buffer = &pMessage->pBuffers[index];
1165 return SEC_E_INVALID_TOKEN;
1167 if (!signature_buffer)
1168 return SEC_E_INVALID_TOKEN;
1171 const ULONG length = data_buffer->cbBuffer;
1172 void* data = malloc(length);
1175 return SEC_E_INSUFFICIENT_MEMORY;
1177 CopyMemory(data, data_buffer->pvBuffer, length);
1181 if (context->confidentiality)
1182 winpr_RC4_Update(context->RecvRc4Seal, length, (BYTE*)data, (BYTE*)data_buffer->pvBuffer);
1184 CopyMemory(data_buffer->pvBuffer, data, length);
1187 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
1190 winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->RecvSigningKey, WINPR_MD5_DIGEST_LENGTH))
1192 winpr_Data_Write_UINT32(&value, SeqNo);
1193 winpr_HMAC_Update(hmac, (
void*)&value, 4);
1194 winpr_HMAC_Update(hmac, data_buffer->pvBuffer, data_buffer->cbBuffer);
1195 winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH);
1196 winpr_HMAC_Free(hmac);
1200 winpr_HMAC_Free(hmac);
1202 return SEC_E_INSUFFICIENT_MEMORY;
1205#ifdef WITH_DEBUG_NTLM
1206 WLog_DBG(TAG,
"Encrypted Data Buffer (length = %" PRIuz
")", length);
1207 winpr_HexDump(TAG, WLOG_DEBUG, data, length);
1208 WLog_DBG(TAG,
"Data Buffer (length = %" PRIu32
")", data_buffer->cbBuffer);
1209 winpr_HexDump(TAG, WLOG_DEBUG, data_buffer->pvBuffer, data_buffer->cbBuffer);
1213 winpr_RC4_Update(context->RecvRc4Seal, 8, digest, checksum);
1215 winpr_Data_Write_UINT32(expected_signature, version);
1216 CopyMemory(&expected_signature[4], (
void*)checksum, 8);
1217 winpr_Data_Write_UINT32(&expected_signature[12], SeqNo);
1218 context->RecvSeqNum++;
1220 if (memcmp(signature_buffer->pvBuffer, expected_signature, 16) != 0)
1223 WLog_ERR(TAG,
"signature verification failed, something nasty is going on!");
1224#ifdef WITH_DEBUG_NTLM
1225 WLog_ERR(TAG,
"Expected Signature:");
1226 winpr_HexDump(TAG, WLOG_ERROR, expected_signature, 16);
1227 WLog_ERR(TAG,
"Actual Signature:");
1228 winpr_HexDump(TAG, WLOG_ERROR, (BYTE*)signature_buffer->pvBuffer, 16);
1230 return SEC_E_MESSAGE_ALTERED;
1236static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(
PCtxtHandle phContext,
1237 WINPR_ATTR_UNUSED ULONG fQOP,
1243 BYTE digest[WINPR_MD5_DIGEST_LENGTH] = { 0 };
1244 BYTE checksum[8] = { 0 };
1246 NTLM_CONTEXT* context = sspi_SecureHandleGetLowerPointer(phContext);
1247 if (!check_context(context))
1248 return SEC_E_INVALID_HANDLE;
1250 for (ULONG i = 0; i < pMessage->cBuffers; i++)
1252 if (pMessage->pBuffers[i].BufferType == SECBUFFER_DATA)
1253 data_buffer = &pMessage->pBuffers[i];
1254 else if (pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1255 sig_buffer = &pMessage->pBuffers[i];
1258 if (!data_buffer || !sig_buffer)
1259 return SEC_E_INVALID_TOKEN;
1261 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
1263 if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->SendSigningKey, WINPR_MD5_DIGEST_LENGTH))
1265 winpr_HMAC_Free(hmac);
1266 return SEC_E_INTERNAL_ERROR;
1269 winpr_Data_Write_UINT32(&seq_no, MessageSeqNo);
1270 winpr_HMAC_Update(hmac, (BYTE*)&seq_no, 4);
1271 winpr_HMAC_Update(hmac, data_buffer->pvBuffer, data_buffer->cbBuffer);
1272 winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH);
1273 winpr_HMAC_Free(hmac);
1275 winpr_RC4_Update(context->SendRc4Seal, 8, digest, checksum);
1277 BYTE* signature = sig_buffer->pvBuffer;
1278 winpr_Data_Write_UINT32(signature, 1L);
1279 CopyMemory(&signature[4], checksum, 8);
1280 winpr_Data_Write_UINT32(&signature[12], seq_no);
1281 sig_buffer->cbBuffer = 16;
1286static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(
PCtxtHandle phContext,
1288 WINPR_ATTR_UNUSED PULONG pfQOP)
1293 BYTE digest[WINPR_MD5_DIGEST_LENGTH] = { 0 };
1294 BYTE checksum[8] = { 0 };
1295 BYTE signature[16] = { 0 };
1297 NTLM_CONTEXT* context = sspi_SecureHandleGetLowerPointer(phContext);
1298 if (!check_context(context))
1299 return SEC_E_INVALID_HANDLE;
1301 for (ULONG i = 0; i < pMessage->cBuffers; i++)
1303 if (pMessage->pBuffers[i].BufferType == SECBUFFER_DATA)
1304 data_buffer = &pMessage->pBuffers[i];
1305 else if (pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1306 sig_buffer = &pMessage->pBuffers[i];
1309 if (!data_buffer || !sig_buffer)
1310 return SEC_E_INVALID_TOKEN;
1312 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
1314 if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->RecvSigningKey, WINPR_MD5_DIGEST_LENGTH))
1316 winpr_HMAC_Free(hmac);
1317 return SEC_E_INTERNAL_ERROR;
1320 winpr_Data_Write_UINT32(&seq_no, MessageSeqNo);
1321 winpr_HMAC_Update(hmac, (BYTE*)&seq_no, 4);
1322 winpr_HMAC_Update(hmac, data_buffer->pvBuffer, data_buffer->cbBuffer);
1323 winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH);
1324 winpr_HMAC_Free(hmac);
1326 winpr_RC4_Update(context->RecvRc4Seal, 8, digest, checksum);
1328 winpr_Data_Write_UINT32(signature, 1L);
1329 CopyMemory(&signature[4], checksum, 8);
1330 winpr_Data_Write_UINT32(&signature[12], seq_no);
1332 if (memcmp(sig_buffer->pvBuffer, signature, 16) != 0)
1333 return SEC_E_MESSAGE_ALTERED;
1341 ntlm_QueryCredentialsAttributesA,
1342 ntlm_AcquireCredentialsHandleA,
1343 ntlm_FreeCredentialsHandle,
1345 ntlm_InitializeSecurityContextA,
1346 ntlm_AcceptSecurityContext,
1348 ntlm_DeleteSecurityContext,
1350 ntlm_QueryContextAttributesA,
1351 ntlm_ImpersonateSecurityContext,
1352 ntlm_RevertSecurityContext,
1354 ntlm_VerifySignature,
1364 ntlm_EncryptMessage,
1365 ntlm_DecryptMessage,
1366 ntlm_SetContextAttributesA,
1367 ntlm_SetCredentialsAttributesA,
1373 ntlm_QueryCredentialsAttributesW,
1374 ntlm_AcquireCredentialsHandleW,
1375 ntlm_FreeCredentialsHandle,
1377 ntlm_InitializeSecurityContextW,
1378 ntlm_AcceptSecurityContext,
1380 ntlm_DeleteSecurityContext,
1382 ntlm_QueryContextAttributesW,
1383 ntlm_ImpersonateSecurityContext,
1384 ntlm_RevertSecurityContext,
1386 ntlm_VerifySignature,
1396 ntlm_EncryptMessage,
1397 ntlm_DecryptMessage,
1398 ntlm_SetContextAttributesW,
1399 ntlm_SetCredentialsAttributesW,
1408 "NTLM Security Package"
1411static WCHAR NTLM_SecPkgInfoW_NameBuffer[32] = { 0 };
1412static WCHAR NTLM_SecPkgInfoW_CommentBuffer[32] = { 0 };
1419 NTLM_SecPkgInfoW_NameBuffer,
1420 NTLM_SecPkgInfoW_CommentBuffer
1423char* ntlm_negotiate_flags_string(
char* buffer,
size_t size, UINT32 flags)
1425 if (!buffer || (size == 0))
1428 (void)_snprintf(buffer, size,
"[0x%08" PRIx32
"] ", flags);
1430 for (
int x = 0; x < 31; x++)
1432 const UINT32 mask = 1 << x;
1433 size_t len = strnlen(buffer, size);
1436 const char* str = ntlm_get_negotiate_string(mask);
1437 const size_t flen = strlen(str);
1439 if ((len > 0) && (buffer[len - 1] !=
' '))
1443 winpr_str_append(
"|", buffer, size, NULL);
1447 if (size - len < flen)
1449 winpr_str_append(str, buffer, size, NULL);
1456const char* ntlm_message_type_string(UINT32 messageType)
1458 switch (messageType)
1460 case MESSAGE_TYPE_NEGOTIATE:
1461 return "MESSAGE_TYPE_NEGOTIATE";
1462 case MESSAGE_TYPE_CHALLENGE:
1463 return "MESSAGE_TYPE_CHALLENGE";
1464 case MESSAGE_TYPE_AUTHENTICATE:
1465 return "MESSAGE_TYPE_AUTHENTICATE";
1467 return "MESSAGE_TYPE_UNKNOWN";
1471const char* ntlm_state_string(NTLM_STATE state)
1475 case NTLM_STATE_INITIAL:
1476 return "NTLM_STATE_INITIAL";
1477 case NTLM_STATE_NEGOTIATE:
1478 return "NTLM_STATE_NEGOTIATE";
1479 case NTLM_STATE_CHALLENGE:
1480 return "NTLM_STATE_CHALLENGE";
1481 case NTLM_STATE_AUTHENTICATE:
1482 return "NTLM_STATE_AUTHENTICATE";
1483 case NTLM_STATE_FINAL:
1484 return "NTLM_STATE_FINAL";
1486 return "NTLM_STATE_UNKNOWN";
1489void ntlm_change_state(
NTLM_CONTEXT* ntlm, NTLM_STATE state)
1492 WLog_DBG(TAG,
"change state from %s to %s", ntlm_state_string(ntlm->state),
1493 ntlm_state_string(state));
1494 ntlm->state = state;
1503BOOL ntlm_reset_cipher_state(
PSecHandle phContext)
1505 NTLM_CONTEXT* context = sspi_SecureHandleGetLowerPointer(phContext);
1509 check_context(context);
1510 winpr_RC4_Free(context->SendRc4Seal);
1511 winpr_RC4_Free(context->RecvRc4Seal);
1512 context->SendRc4Seal = winpr_RC4_New(context->RecvSealingKey, 16);
1513 context->RecvRc4Seal = winpr_RC4_New(context->SendSealingKey, 16);
1515 if (!context->SendRc4Seal)
1517 WLog_ERR(TAG,
"Failed to allocate context->SendRc4Seal");
1520 if (!context->RecvRc4Seal)
1522 WLog_ERR(TAG,
"Failed to allocate context->RecvRc4Seal");
1532 InitializeConstWCharFromUtf8(NTLM_SecPkgInfoA.Name, NTLM_SecPkgInfoW_NameBuffer,
1533 ARRAYSIZE(NTLM_SecPkgInfoW_NameBuffer));
1534 InitializeConstWCharFromUtf8(NTLM_SecPkgInfoA.Comment, NTLM_SecPkgInfoW_CommentBuffer,
1535 ARRAYSIZE(NTLM_SecPkgInfoW_CommentBuffer));