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#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
30#include <winpr/registry.h>
32#include <winpr/endian.h>
33#include <winpr/build-config.h>
34#include <winpr/path.h>
35#include <winpr/config-readers.h>
38#include "ntlm_export.h"
41#include "ntlm_message.h"
43#include "../../utils.h"
46#define TAG WINPR_TAG("sspi.NTLM")
49#define MIN(a, b) ((a) < (b)) ? (a) : (b)
52#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
53#define WINPR_KEY "Software\\%s\\WinPR\\NTLM"
56#define check_context(ctx) check_context_((ctx), __FILE__, __func__, __LINE__)
59static BOOL check_context_(
NTLM_CONTEXT* context,
const char* file,
const char* fkt,
size_t line)
62 wLog* log = WLog_Get(TAG);
63 const DWORD log_level = WLOG_ERROR;
67 if (WLog_IsLevelActive(log, log_level))
68 WLog_PrintTextMessage(log, log_level, line, file, fkt,
"invalid context");
73 if (!context->RecvRc4Seal)
75 if (WLog_IsLevelActive(log, log_level))
76 WLog_PrintTextMessage(log, log_level, line, file, fkt,
"invalid context->RecvRc4Seal");
79 if (!context->SendRc4Seal)
81 if (WLog_IsLevelActive(log, log_level))
82 WLog_PrintTextMessage(log, log_level, line, file, fkt,
"invalid context->SendRc4Seal");
86 if (!context->SendSigningKey)
88 if (WLog_IsLevelActive(log, log_level))
89 WLog_PrintTextMessage(log, log_level, line, file, fkt,
90 "invalid context->SendSigningKey");
93 if (!context->RecvSigningKey)
95 if (WLog_IsLevelActive(log, log_level))
96 WLog_PrintTextMessage(log, log_level, line, file, fkt,
97 "invalid context->RecvSigningKey");
100 if (!context->SendSealingKey)
102 if (WLog_IsLevelActive(log, log_level))
103 WLog_PrintTextMessage(log, log_level, line, file, fkt,
104 "invalid context->SendSealingKey");
107 if (!context->RecvSealingKey)
109 if (WLog_IsLevelActive(log, log_level))
110 WLog_PrintTextMessage(log, log_level, line, file, fkt,
111 "invalid context->RecvSealingKey");
117WINPR_ATTR_MALLOC(free, 1)
118static
char* get_computer_name(COMPUTER_NAME_FORMAT type,
size_t* pSize)
125 if (GetComputerNameExA(type,
nullptr, &nSize))
128 if (GetLastError() != ERROR_MORE_DATA)
131 char* computerName = calloc(1, nSize);
136 if (!GetComputerNameExA(type, computerName, &nSize))
148SECURITY_STATUS ntlm_SetContextWorkstationX(
NTLM_CONTEXT* context, BOOL unicode,
const void* data,
151 WINPR_ASSERT(context);
152 ntlm_free_unicode_string(&context->Workstation);
159 context->Workstation = ntlm_from_unicode_string_w(data, length /
sizeof(WCHAR));
161 context->Workstation = ntlm_from_unicode_string_utf8(data, length);
163 if (ntlm_is_unicode_string_empty(&context->Workstation))
164 return SEC_E_INSUFFICIENT_MEMORY;
170static int ntlm_SetContextWorkstation(
NTLM_CONTEXT* context,
const char* Workstation)
172 const char* ws = Workstation;
173 CHAR* computerName =
nullptr;
177 computerName = get_computer_name(ComputerNameNetBIOS,
nullptr);
183 const size_t len = strlen(ws);
184 const SECURITY_STATUS status = ntlm_SetContextWorkstationX(context, FALSE, ws, len);
187 return (status == SEC_E_OK) ? 1 : -1;
191static int ntlm_SetContextServicePrincipalNameW(
NTLM_CONTEXT* context, LPWSTR ServicePrincipalName)
193 WINPR_ASSERT(context);
195 ntlm_free_unicode_string(&context->ServicePrincipalName);
196 if (!ServicePrincipalName)
199 const size_t len = _wcslen(ServicePrincipalName);
200 context->ServicePrincipalName = ntlm_from_unicode_string_w(ServicePrincipalName, len);
201 if (ntlm_is_unicode_string_empty(&context->ServicePrincipalName))
208static int ntlm_SetContextTargetName(
NTLM_CONTEXT* context,
char* TargetName)
210 char* name = TargetName;
211 WINPR_ASSERT(context);
216 char* computerName = get_computer_name(ComputerNameNetBIOS, &nSize);
221 if (nSize > MAX_COMPUTERNAME_LENGTH)
222 computerName[MAX_COMPUTERNAME_LENGTH] =
'\0';
233 sspi_SecBufferFree(&context->TargetName);
234 context->TargetName.pvBuffer = ConvertUtf8ToWCharAlloc(name, &len);
236 if (!context->TargetName.pvBuffer || (len > UINT16_MAX /
sizeof(WCHAR)))
238 free(context->TargetName.pvBuffer);
239 context->TargetName.pvBuffer =
nullptr;
247 context->TargetName.cbBuffer = (USHORT)(len *
sizeof(WCHAR));
260 winpr_RC4_Free(context->SendRc4Seal);
261 winpr_RC4_Free(context->RecvRc4Seal);
262 sspi_SecBufferFree(&context->NegotiateMessage);
263 sspi_SecBufferFree(&context->ChallengeMessage);
264 sspi_SecBufferFree(&context->AuthenticateMessage);
265 sspi_SecBufferFree(&context->ChallengeTargetInfo);
266 sspi_SecBufferFree(&context->AuthenticateTargetInfo);
267 sspi_SecBufferFree(&context->TargetName);
268 sspi_SecBufferFree(&context->NtChallengeResponse);
269 sspi_SecBufferFree(&context->LmChallengeResponse);
270 ntlm_free_unicode_string(&context->ServicePrincipalName);
271 ntlm_free_unicode_string(&context->Workstation);
272 ntlm_free_unicode_string(&context->NbComputerName);
273 ntlm_free_unicode_string(&context->NbDomainName);
274 ntlm_free_unicode_string(&context->DnsComputerName);
275 ntlm_free_unicode_string(&context->DnsDomainName);
277 ntlm_free_messages(context);
280 memset(context->NtlmHash, 0,
sizeof(context->NtlmHash));
281 memset(context->NtlmV2Hash, 0,
sizeof(context->NtlmV2Hash));
282 memset(context->SessionBaseKey, 0,
sizeof(context->SessionBaseKey));
283 memset(context->KeyExchangeKey, 0,
sizeof(context->KeyExchangeKey));
284 memset(context->RandomSessionKey, 0,
sizeof(context->RandomSessionKey));
285 memset(context->ExportedSessionKey, 0,
sizeof(context->ExportedSessionKey));
286 memset(context->EncryptedRandomSessionKey, 0,
sizeof(context->EncryptedRandomSessionKey));
287 memset(context->NtProofString, 0,
sizeof(context->NtProofString));
292static int ntlm_get_target_computer_name(PUNICODE_STRING pName,
293 WINPR_ATTR_UNUSED COMPUTER_NAME_FORMAT type)
296 ntlm_free_unicode_string(pName);
299 char* name = get_computer_name(ComputerNameNetBIOS, &len);
305 *pName = ntlm_from_unicode_string_utf8(name, len);
308 return !ntlm_is_unicode_string_empty(pName);
312static BOOL ntlm_ContextFillDefaultNames(
NTLM_CONTEXT* context)
314 WINPR_ASSERT(context);
316 if (ntlm_SetContextWorkstation(context,
nullptr) < 0)
319 if (ntlm_get_target_computer_name(&context->NbDomainName, ComputerNameNetBIOS) < 0)
322 if (ntlm_get_target_computer_name(&context->NbComputerName, ComputerNameNetBIOS) < 0)
325 if (ntlm_get_target_computer_name(&context->DnsDomainName, ComputerNameDnsDomain) < 0)
328 if (ntlm_get_target_computer_name(&context->DnsComputerName, ComputerNameDnsHostname) < 0)
333#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
334static BOOL ntlm_try_set_from_registry(HKEY hKey,
const char* key, UNICODE_STRING* ustr)
339 UNICODE_STRING str = WINPR_C_ARRAY_INIT;
341 WCHAR wkey[64] = WINPR_C_ARRAY_INIT;
342 const SSIZE_T res = ConvertUtf8ToWChar(key, wkey, ARRAYSIZE(wkey));
345 WINPR_ASSERT((
size_t)res < ARRAYSIZE(wkey));
349 if (RegQueryValueExW(hKey, wkey,
nullptr, &dwType,
nullptr, &dwSize) != ERROR_SUCCESS)
352 if ((dwSize > UINT16_MAX) || ((dwSize % 2) != 0))
355 str.Buffer = calloc(dwSize /
sizeof(WCHAR) + 1,
sizeof(WCHAR));
358 str.Length = WINPR_ASSERTING_INT_CAST(UINT16, dwSize);
359 str.MaximumLength = WINPR_ASSERTING_INT_CAST(UINT16, dwSize);
361 const LONG rc = RegQueryValueExW(hKey, wkey,
nullptr, &dwType, (BYTE*)str.Buffer, &dwSize);
362 if (rc != ERROR_SUCCESS)
364 ntlm_free_unicode_string(ustr);
369 ntlm_free_unicode_string(&str);
375static SECURITY_STATUS ntml_setUnicodeStringA(UNICODE_STRING* str,
const char* val,
size_t charlen);
377static void apply_string(
const char* config, WINPR_JSON* json,
const char* name,
383 if (!winpr_config_apply_string(config, json, name, &str))
386 const BOOL rc = ntml_setUnicodeStringA(dst, str, strlen(str));
389 WLog_WARN(TAG,
"[%s] Invalid setting %s: must be of type string", config, name);
393static BOOL ntlm_ContextFromConfig(
NTLM_CONTEXT* context)
395 const char config[] =
"ntlm.json";
397 WINPR_ASSERT(context);
399#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
401 char* key = winpr_getApplicatonDetailsRegKey(WINPR_KEY);
407 RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
410 if (status == ERROR_SUCCESS)
413 "HKLM.reg is deprecated since 3.33.0. ATTENTION: Use "
421 if (RegQueryValueEx(hKey, _T(
"NTLMv2"),
nullptr, &dwType, (BYTE*)&dwValue,
422 &dwSize) == ERROR_SUCCESS)
423 context->NTLMv2 = dwValue ? 1 : 0;
425 if (RegQueryValueEx(hKey, _T(
"UseMIC"),
nullptr, &dwType, (BYTE*)&dwValue,
426 &dwSize) == ERROR_SUCCESS)
427 context->UseMIC = dwValue ? 1 : 0;
429 if (RegQueryValueEx(hKey, _T(
"SendVersionInfo"),
nullptr, &dwType, (BYTE*)&dwValue,
430 &dwSize) == ERROR_SUCCESS)
431 context->SendVersionInfo = dwValue ? 1 : 0;
433 if (RegQueryValueEx(hKey, _T(
"SendSingleHostData"),
nullptr, &dwType,
434 (BYTE*)&dwValue, &dwSize) == ERROR_SUCCESS)
435 context->SendSingleHostData = dwValue ? 1 : 0;
437 if (RegQueryValueEx(hKey, _T(
"SendWorkstationName"),
nullptr, &dwType,
438 (BYTE*)&dwValue, &dwSize) == ERROR_SUCCESS)
439 context->SendWorkstationName = dwValue ? 1 : 0;
441 (void)ntlm_try_set_from_registry(hKey,
"WorkstationName", &context->Workstation);
442 (void)ntlm_try_set_from_registry(hKey,
"NbDomainName", &context->NbDomainName);
443 (void)ntlm_try_set_from_registry(hKey,
"NbComputerName", &context->NbComputerName);
444 (void)ntlm_try_set_from_registry(hKey,
"DnsDomainName", &context->DnsDomainName);
445 (void)ntlm_try_set_from_registry(hKey,
"DnsComputerName",
446 &context->DnsComputerName);
454 RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T(
"System\\CurrentControlSet\\Control\\LSA"), 0,
455 KEY_READ | KEY_WOW64_64KEY, &hKey);
457 if (status == ERROR_SUCCESS)
459 WLog_WARN(TAG,
"HKLM.reg is deprecated since 3.33.0. ATTENTION: Use %s instead!", config);
464 if (RegQueryValueEx(hKey, _T(
"SuppressExtendedProtection"),
nullptr, &dwType,
465 (BYTE*)&dwValue, &dwSize) == ERROR_SUCCESS)
466 context->SuppressExtendedProtection = dwValue != 0;
472 WINPR_JSON* json = winpr_GetJSONConfigFile(TRUE, config);
475 winpr_config_apply_bool(config, json,
"NTLMv2", &context->NTLMv2);
476 winpr_config_apply_bool(config, json,
"UseMIC", &context->UseMIC);
477 winpr_config_apply_bool(config, json,
"SendVersionInfo", &context->SendVersionInfo);
478 winpr_config_apply_bool(config, json,
"SendSingleHostData",
479 &context->SendSingleHostData);
480 winpr_config_apply_bool(config, json,
"SendWorkstationName",
481 &context->SendWorkstationName);
482 winpr_config_apply_bool(config, json,
"SuppressExtendedProtection",
483 &context->SuppressExtendedProtection);
485 apply_string(config, json,
"WorkstationName", &context->Workstation);
486 apply_string(config, json,
"NbDomainName", &context->NbDomainName);
487 apply_string(config, json,
"NbComputerName", &context->NbComputerName);
488 apply_string(config, json,
"DnsDomainName", &context->DnsDomainName);
489 apply_string(config, json,
"DnsComputerName", &context->DnsComputerName);
499 context->SuppressExtendedProtection = FALSE;
503WINPR_ATTR_MALLOC(ntlm_ContextFree, 1)
511 context->NTLMv2 = TRUE;
512 context->UseMIC = FALSE;
513 context->SendVersionInfo = TRUE;
514 context->SendSingleHostData = FALSE;
515 context->SendWorkstationName = TRUE;
516 context->NegotiateKeyExchange = TRUE;
517 context->UseSamFileDatabase = TRUE;
519 context->NegotiateFlags = 0;
520 context->LmCompatibilityLevel = 3;
521 ntlm_change_state(context, NTLM_STATE_INITIAL);
522 FillMemory(context->MachineID,
sizeof(context->MachineID), 0xAA);
525 context->UseMIC = TRUE;
527 if (!ntlm_ContextFillDefaultNames(context))
529 if (!ntlm_ContextFromConfig(context))
535 ntlm_ContextFree(context);
540static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
541 WINPR_ATTR_UNUSED SEC_WCHAR* pszPrincipal, WINPR_ATTR_UNUSED SEC_WCHAR* pszPackage,
542 ULONG fCredentialUse, WINPR_ATTR_UNUSED
void* pvLogonID,
void* pAuthData,
543 SEC_GET_KEY_FN pGetKeyFn,
void* pvGetKeyArgument, PCredHandle phCredential,
544 WINPR_ATTR_UNUSED PTimeStamp ptsExpiry)
546 if ((fCredentialUse != SECPKG_CRED_OUTBOUND) && (fCredentialUse != SECPKG_CRED_INBOUND) &&
547 (fCredentialUse != SECPKG_CRED_BOTH))
549 return SEC_E_INVALID_PARAMETER;
555 return SEC_E_INTERNAL_ERROR;
557 credentials->fCredentialUse = fCredentialUse;
558 credentials->pGetKeyFn = pGetKeyFn;
559 credentials->pvGetKeyArgument = pvGetKeyArgument;
561#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
564 SEC_WINPR_NTLM_SETTINGS_V2* settingsV2 =
nullptr;
567 UINT32 identityFlags = sspi_GetAuthIdentityFlags(pAuthData);
569 if (sspi_CopyAuthIdentity(&(credentials->identity),
572 sspi_CredentialsFree(credentials);
573 return SEC_E_INVALID_PARAMETER;
576#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
577 if (identityFlags & SEC_WINNT_AUTH_IDENTITY_EXTENDED)
581 if (identityFlags & SEC_WINNT_AUTH_IDENTITY_EXTENDED_v2)
583 const SEC_WINNT_AUTH_IDENTITY_WINPR_V2* auth =
584 (
const SEC_WINNT_AUTH_IDENTITY_WINPR_V2*)pAuthData;
586 if (auth->version < SEC_WINNT_AUTH_IDENTITY_WINPR_V2_REVISION_1)
587 return SEC_E_INVALID_PARAMETER;
588 settingsV2 = auth->ntlmSettingsV2;
592#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
597 if (!sspi_CloneSecSettingsString(&credentials->ntlmSettingsV2->samFile,
600 sspi_CredentialsFree(credentials);
601 return SEC_E_INSUFFICIENT_MEMORY;
604 credentials->ntlmSettingsV2->hashCallback = settingsV1->
hashCallback;
605 credentials->ntlmSettingsV2->hashCallbackArg = settingsV1->
hashCallbackArg;
611 sspi_FreeSecNtlmSettings(credentials->ntlmSettingsV2);
612 credentials->ntlmSettingsV2 = sspi_CloneSecNtlmSettings(settingsV2);
613 if (!credentials->ntlmSettingsV2)
615 sspi_CredentialsFree(credentials);
616 return SEC_E_INVALID_PARAMETER;
620 sspi_SecureHandleSetLowerPointer(phCredential, (
void*)credentials);
621 sspi_SecureHandleSetPackageId(phCredential, SSPI_PACKAGE_NTLM);
626static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
627 SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage, ULONG fCredentialUse,
void* pvLogonID,
628 void* pAuthData, SEC_GET_KEY_FN pGetKeyFn,
void* pvGetKeyArgument, PCredHandle phCredential,
629 PTimeStamp ptsExpiry)
631 SECURITY_STATUS status = SEC_E_INSUFFICIENT_MEMORY;
632 SEC_WCHAR* principal =
nullptr;
633 SEC_WCHAR*
package = nullptr;
637 principal = ConvertUtf8ToWCharAlloc(pszPrincipal,
nullptr);
643 package = ConvertUtf8ToWCharAlloc(pszPackage, nullptr);
649 ntlm_AcquireCredentialsHandleW(principal, package, fCredentialUse, pvLogonID, pAuthData,
650 pGetKeyFn, pvGetKeyArgument, phCredential, ptsExpiry);
660static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(PCredHandle phCredential)
663 return SEC_E_INVALID_HANDLE;
665 SSPI_CREDENTIALS* credentials =
666 (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
667 sspi_SecureHandleInvalidate(phCredential);
669 return SEC_E_INVALID_HANDLE;
671 sspi_CredentialsFree(credentials);
676static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
677 WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute,
678 WINPR_ATTR_UNUSED
void* pBuffer)
680 if (ulAttribute == SECPKG_CRED_ATTR_NAMES)
685 WLog_ERR(
TAG,
"TODO: Implement");
686 return SEC_E_UNSUPPORTED_FUNCTION;
690static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(PCredHandle phCredential,
691 ULONG ulAttribute,
void* pBuffer)
693 return ntlm_QueryCredentialsAttributesW(phCredential, ulAttribute, pBuffer);
700static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
701 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput, ULONG fContextReq,
702 WINPR_ATTR_UNUSED ULONG TargetDataRep, PCtxtHandle phNewContext, PSecBufferDesc pOutput,
703 WINPR_ATTR_UNUSED PULONG pfContextAttr, WINPR_ATTR_UNUSED PTimeStamp ptsTimeStamp)
705 SECURITY_STATUS status = 0;
706 SSPI_CREDENTIALS* credentials =
nullptr;
707 PSecBuffer input_buffer =
nullptr;
708 PSecBuffer output_buffer =
nullptr;
711 if (phContext && !phContext->dwLower && !phContext->dwUpper)
712 return SEC_E_INVALID_HANDLE;
714 NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
718 context = ntlm_ContextNew();
721 return SEC_E_INSUFFICIENT_MEMORY;
723 context->server = TRUE;
725 if (fContextReq & ASC_REQ_CONFIDENTIALITY)
726 context->confidentiality = TRUE;
728 credentials = (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
729 context->credentials = credentials;
730 context->SamFile = credentials->ntlmSettingsV2->samFile;
731 context->HashCallback = credentials->ntlmSettingsV2->hashCallback;
732 context->HashCallbackArg = credentials->ntlmSettingsV2->hashCallbackArg;
734 if (credentials->ntlmSettingsV2->dnsComputerName)
736 const SECURITY_STATUS rc = ntml_setUnicodeStringA(
737 &context->DnsComputerName, credentials->ntlmSettingsV2->dnsComputerName,
738 strlen(credentials->ntlmSettingsV2->dnsComputerName));
741 ntlm_ContextFree(context);
746 if (credentials->ntlmSettingsV2->dnsDomainName)
748 const SECURITY_STATUS rc = ntml_setUnicodeStringA(
749 &context->DnsDomainName, credentials->ntlmSettingsV2->dnsDomainName,
750 strlen(credentials->ntlmSettingsV2->dnsDomainName));
753 ntlm_ContextFree(context);
758 if (credentials->ntlmSettingsV2->netBiosComputerName)
760 const SECURITY_STATUS rc = ntml_setUnicodeStringA(
761 &context->NbComputerName, credentials->ntlmSettingsV2->netBiosComputerName,
762 strlen(credentials->ntlmSettingsV2->netBiosComputerName));
765 ntlm_ContextFree(context);
770 if (credentials->ntlmSettingsV2->netBiosDomainName)
772 const SECURITY_STATUS rc = ntml_setUnicodeStringA(
773 &context->NbDomainName, credentials->ntlmSettingsV2->netBiosDomainName,
774 strlen(credentials->ntlmSettingsV2->netBiosDomainName));
777 ntlm_ContextFree(context);
782 if (!ntlm_SetContextTargetName(context, credentials->ntlmSettingsV2->targetName))
784 ntlm_ContextFree(context);
785 return SEC_E_INVALID_HANDLE;
787 sspi_SecureHandleSetLowerPointer(phNewContext, context);
788 sspi_SecureHandleSetPackageId(phNewContext, SSPI_PACKAGE_NTLM);
791 switch (ntlm_get_state(context))
793 case NTLM_STATE_INITIAL:
795 ntlm_change_state(context, NTLM_STATE_NEGOTIATE);
798 return SEC_E_INVALID_TOKEN;
800 if (pInput->cBuffers < 1)
801 return SEC_E_INVALID_TOKEN;
803 input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
806 return SEC_E_INVALID_TOKEN;
808 if (input_buffer->cbBuffer < 1)
809 return SEC_E_INVALID_TOKEN;
811 status = ntlm_read_NegotiateMessage(context, input_buffer);
812 if (status != SEC_I_CONTINUE_NEEDED)
815 if (ntlm_get_state(context) == NTLM_STATE_CHALLENGE)
818 return SEC_E_INVALID_TOKEN;
820 if (pOutput->cBuffers < 1)
821 return SEC_E_INVALID_TOKEN;
823 output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
825 if (!output_buffer->BufferType)
826 return SEC_E_INVALID_TOKEN;
828 if (output_buffer->cbBuffer < 1)
829 return SEC_E_INSUFFICIENT_MEMORY;
831 return ntlm_write_ChallengeMessage(context, output_buffer);
834 return SEC_E_OUT_OF_SEQUENCE;
837 case NTLM_STATE_AUTHENTICATE:
840 return SEC_E_INVALID_TOKEN;
842 if (pInput->cBuffers < 1)
843 return SEC_E_INVALID_TOKEN;
845 input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
848 return SEC_E_INVALID_TOKEN;
850 if (input_buffer->cbBuffer < 1)
851 return SEC_E_INVALID_TOKEN;
853 status = ntlm_read_AuthenticateMessage(context, input_buffer);
857 for (ULONG i = 0; i < pOutput->cBuffers; i++)
859 pOutput->pBuffers[i].cbBuffer = 0;
860 pOutput->pBuffers[i].BufferType = SECBUFFER_TOKEN;
868 return SEC_E_OUT_OF_SEQUENCE;
873static SECURITY_STATUS SEC_ENTRY
874ntlm_ImpersonateSecurityContext(WINPR_ATTR_UNUSED PCtxtHandle phContext)
880static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
881 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq,
882 WINPR_ATTR_UNUSED ULONG Reserved1, WINPR_ATTR_UNUSED ULONG TargetDataRep, PSecBufferDesc pInput,
883 WINPR_ATTR_UNUSED ULONG Reserved2, PCtxtHandle phNewContext, PSecBufferDesc pOutput,
884 WINPR_ATTR_UNUSED PULONG pfContextAttr, WINPR_ATTR_UNUSED PTimeStamp ptsExpiry)
886 SECURITY_STATUS status = 0;
887 SSPI_CREDENTIALS* credentials =
nullptr;
888 PSecBuffer input_buffer =
nullptr;
889 PSecBuffer output_buffer =
nullptr;
892 if (phContext && !phContext->dwLower && !phContext->dwUpper)
893 return SEC_E_INVALID_HANDLE;
895 NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
899 input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
904 context = ntlm_ContextNew();
907 return SEC_E_INSUFFICIENT_MEMORY;
909 if (fContextReq & ISC_REQ_CONFIDENTIALITY)
910 context->confidentiality = TRUE;
912 credentials = (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
913 context->credentials = credentials;
915 if (ntlm_SetContextServicePrincipalNameW(context, pszTargetName) < 0)
917 ntlm_ContextFree(context);
918 return SEC_E_INTERNAL_ERROR;
921 sspi_SecureHandleSetLowerPointer(phNewContext, context);
922 sspi_SecureHandleSetPackageId(phNewContext, SSPI_PACKAGE_NTLM);
925 if ((!input_buffer) || (ntlm_get_state(context) == NTLM_STATE_AUTHENTICATE))
928 return SEC_E_INVALID_TOKEN;
930 if (pOutput->cBuffers < 1)
931 return SEC_E_INVALID_TOKEN;
933 output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
936 return SEC_E_INVALID_TOKEN;
938 if (output_buffer->cbBuffer < 1)
939 return SEC_E_INVALID_TOKEN;
941 if (ntlm_get_state(context) == NTLM_STATE_INITIAL)
942 ntlm_change_state(context, NTLM_STATE_NEGOTIATE);
944 if (ntlm_get_state(context) == NTLM_STATE_NEGOTIATE)
945 return ntlm_write_NegotiateMessage(context, output_buffer);
947 return SEC_E_OUT_OF_SEQUENCE;
952 return SEC_E_INVALID_TOKEN;
954 if (input_buffer->cbBuffer < 1)
955 return SEC_E_INVALID_TOKEN;
957 PSecBuffer channel_bindings = sspi_FindSecBuffer(pInput, SECBUFFER_CHANNEL_BINDINGS);
959 if (channel_bindings)
961 context->Bindings.BindingsLength = channel_bindings->cbBuffer;
962 context->Bindings.Bindings = (SEC_CHANNEL_BINDINGS*)channel_bindings->pvBuffer;
965 if (ntlm_get_state(context) == NTLM_STATE_CHALLENGE)
967 status = ntlm_read_ChallengeMessage(context, input_buffer);
969 if (status != SEC_I_CONTINUE_NEEDED)
973 return SEC_E_INVALID_TOKEN;
975 if (pOutput->cBuffers < 1)
976 return SEC_E_INVALID_TOKEN;
978 output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
981 return SEC_E_INVALID_TOKEN;
983 if (output_buffer->cbBuffer < 1)
984 return SEC_E_INSUFFICIENT_MEMORY;
986 if (ntlm_get_state(context) == NTLM_STATE_AUTHENTICATE)
987 return ntlm_write_AuthenticateMessage(context, output_buffer);
990 return SEC_E_OUT_OF_SEQUENCE;
993 return SEC_E_OUT_OF_SEQUENCE;
1000static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
1001 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR* pszTargetName, ULONG fContextReq,
1002 ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
1003 PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
1005 SECURITY_STATUS status = 0;
1006 SEC_WCHAR* pszTargetNameW =
nullptr;
1010 pszTargetNameW = ConvertUtf8ToWCharAlloc(pszTargetName,
nullptr);
1011 if (!pszTargetNameW)
1012 return SEC_E_INTERNAL_ERROR;
1015 status = ntlm_InitializeSecurityContextW(phCredential, phContext, pszTargetNameW, fContextReq,
1016 Reserved1, TargetDataRep, pInput, Reserved2,
1017 phNewContext, pOutput, pfContextAttr, ptsExpiry);
1018 free(pszTargetNameW);
1024static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1026 NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1027 sspi_SecureHandleInvalidate(phContext);
1028 ntlm_ContextFree(context);
1032SECURITY_STATUS ntlm_computeProofValue(NTLM_CONTEXT* ntlm, SecBuffer* ntproof)
1034 BYTE* blob =
nullptr;
1035 SecBuffer* target =
nullptr;
1038 WINPR_ASSERT(ntproof);
1040 target = &ntlm->ChallengeTargetInfo;
1042 if (!sspi_SecBufferAlloc(ntproof, 36 + target->cbBuffer))
1043 return SEC_E_INSUFFICIENT_MEMORY;
1045 blob = (BYTE*)ntproof->pvBuffer;
1046 CopyMemory(blob, ntlm->ServerChallenge, 8);
1050 CopyMemory(&blob[16], ntlm->Timestamp, 8);
1051 CopyMemory(&blob[24], ntlm->ClientChallenge, 8);
1054 CopyMemory(&blob[36], target->pvBuffer, target->cbBuffer);
1058SECURITY_STATUS ntlm_computeMicValue(NTLM_CONTEXT* ntlm, SecBuffer* micvalue)
1061 WINPR_ASSERT(micvalue);
1063 const UINT64 msgSize = 1ull * ntlm->NegotiateMessage.cbBuffer +
1064 ntlm->ChallengeMessage.cbBuffer + ntlm->AuthenticateMessage.cbBuffer;
1065 if (msgSize > UINT32_MAX)
1066 return SEC_E_INSUFFICIENT_MEMORY;
1068 if (!sspi_SecBufferAlloc(micvalue, WINPR_ASSERTING_INT_CAST(ULONG, msgSize)))
1069 return SEC_E_INSUFFICIENT_MEMORY;
1071 if (micvalue->cbBuffer < msgSize)
1072 return SEC_E_INVALID_TOKEN;
1073 if (ntlm->AuthenticateMessage.cbBuffer < ntlm->MessageIntegrityCheckOffset + 16ull)
1074 return SEC_E_INVALID_TOKEN;
1076 BYTE* blob = (BYTE*)micvalue->pvBuffer;
1078 return SEC_E_INVALID_TOKEN;
1080 CopyMemory(blob, ntlm->NegotiateMessage.pvBuffer, ntlm->NegotiateMessage.cbBuffer);
1081 blob += ntlm->NegotiateMessage.cbBuffer;
1082 CopyMemory(blob, ntlm->ChallengeMessage.pvBuffer, ntlm->ChallengeMessage.cbBuffer);
1083 blob += ntlm->ChallengeMessage.cbBuffer;
1084 CopyMemory(blob, ntlm->AuthenticateMessage.pvBuffer, ntlm->AuthenticateMessage.cbBuffer);
1085 blob += ntlm->MessageIntegrityCheckOffset;
1086 ZeroMemory(blob, 16);
1091static bool identityToAuthIdentity(
const SEC_WINNT_AUTH_IDENTITY* identity,
1092 SecPkgContext_AuthIdentity* pAuthIdentity)
1094 WINPR_ASSERT(identity);
1099 const SecPkgContext_AuthIdentity empty = WINPR_C_ARRAY_INIT;
1100 *pAuthIdentity = empty;
1102 if ((identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE) != 0)
1104 if (identity->UserLength > 0)
1106 if (ConvertWCharNToUtf8(identity->User, identity->UserLength, pAuthIdentity->User,
1107 ARRAYSIZE(pAuthIdentity->User)) <= 0)
1111 if (identity->DomainLength > 0)
1113 if (ConvertWCharNToUtf8(identity->Domain, identity->DomainLength, pAuthIdentity->Domain,
1114 ARRAYSIZE(pAuthIdentity->Domain)) <= 0)
1118 else if ((identity->Flags & SEC_WINNT_AUTH_IDENTITY_ANSI) != 0)
1120 if (identity->UserLength > 0)
1122 const size_t len = MIN(ARRAYSIZE(pAuthIdentity->User) - 1, identity->UserLength);
1123 strncpy(pAuthIdentity->User, (
char*)identity->User, len);
1124 pAuthIdentity->User[len] =
'\0';
1127 if (identity->DomainLength > 0)
1129 const size_t len = MIN(ARRAYSIZE(pAuthIdentity->Domain) - 1, identity->DomainLength);
1130 strncpy(pAuthIdentity->Domain, (
char*)identity->Domain, len);
1131 pAuthIdentity->Domain[len] =
'\0';
1140static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesCommon(PCtxtHandle phContext,
1141 ULONG ulAttribute,
void* pBuffer)
1144 return SEC_E_INVALID_HANDLE;
1147 return SEC_E_INSUFFICIENT_MEMORY;
1149 NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1150 if (!check_context(context))
1151 return SEC_E_INVALID_HANDLE;
1153 switch (ulAttribute)
1155 case SECPKG_ATTR_AUTH_IDENTITY:
1157 SecPkgContext_AuthIdentity* AuthIdentity = (SecPkgContext_AuthIdentity*)pBuffer;
1158 SSPI_CREDENTIALS* credentials = context->credentials;
1160 return SEC_E_INTERNAL_ERROR;
1161 if (!identityToAuthIdentity(&credentials->identity, AuthIdentity))
1162 return SEC_E_INTERNAL_ERROR;
1163 context->UseSamFileDatabase = FALSE;
1166 case SECPKG_ATTR_SIZES:
1168 SecPkgContext_Sizes* ContextSizes = (SecPkgContext_Sizes*)pBuffer;
1169 ContextSizes->cbMaxToken = 2010;
1170 ContextSizes->cbMaxSignature = 16;
1171 ContextSizes->cbBlockSize = 0;
1172 ContextSizes->cbSecurityTrailer = 16;
1176 case SECPKG_ATTR_AUTH_NTLM_NTPROOF_VALUE:
1177 return ntlm_computeProofValue(context, (SecBuffer*)pBuffer);
1179 case SECPKG_ATTR_AUTH_NTLM_RANDKEY:
1181 SecBuffer* randkey = (SecBuffer*)pBuffer;
1183 if (!sspi_SecBufferAlloc(randkey, 16))
1184 return (SEC_E_INSUFFICIENT_MEMORY);
1186 CopyMemory(randkey->pvBuffer, context->EncryptedRandomSessionKey, 16);
1190 case SECPKG_ATTR_AUTH_NTLM_MIC:
1192 SecBuffer* mic = (SecBuffer*)pBuffer;
1193 NTLM_AUTHENTICATE_MESSAGE* message = &context->AUTHENTICATE_MESSAGE;
1195 if (!sspi_SecBufferAlloc(mic, 16))
1196 return (SEC_E_INSUFFICIENT_MEMORY);
1198 CopyMemory(mic->pvBuffer, message->MessageIntegrityCheck, 16);
1202 case SECPKG_ATTR_AUTH_NTLM_MIC_VALUE:
1203 return ntlm_computeMicValue(context, (SecBuffer*)pBuffer);
1206 WLog_ERR(
TAG,
"TODO: Implement ulAttribute=0x%08" PRIx32, ulAttribute);
1207 return SEC_E_UNSUPPORTED_FUNCTION;
1213static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1214 ULONG ulAttribute,
void* pBuffer)
1217 return SEC_E_INVALID_HANDLE;
1220 return SEC_E_INSUFFICIENT_MEMORY;
1222 NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1223 if (!check_context(context))
1224 return SEC_E_INVALID_HANDLE;
1226 switch (ulAttribute)
1228 case SECPKG_ATTR_AUTH_NTLM_HOSTNAME:
1230 memcpy(pBuffer, context->Workstation.Buffer, context->Workstation.Length);
1233 case SECPKG_ATTR_AUTH_NTLM_NB_DOMAIN_NAME:
1235 memcpy(pBuffer, context->NbDomainName.Buffer, context->NbDomainName.Length);
1238 case SECPKG_ATTR_AUTH_NTLM_NB_COMPUTER_NAME:
1240 memcpy(pBuffer, context->NbComputerName.Buffer, context->NbComputerName.Length);
1243 case SECPKG_ATTR_AUTH_NTLM_DNS_DOMAIN_NAME:
1245 memcpy(pBuffer, context->DnsDomainName.Buffer, context->DnsDomainName.Length);
1248 case SECPKG_ATTR_AUTH_NTLM_DNS_COMPUTER_NAME:
1250 memcpy(pBuffer, context->DnsComputerName.Buffer, context->DnsComputerName.Length);
1254 case SECPKG_ATTR_PACKAGE_INFO:
1256 SecPkgContext_PackageInfoW* PackageInfo = (SecPkgContext_PackageInfoW*)pBuffer;
1257 size_t size =
sizeof(SecPkgInfoW);
1258 SecPkgInfoW* pPackageInfo =
1259 (SecPkgInfoW*)sspi_ContextBufferAlloc(QuerySecurityPackageInfoIndex, size);
1262 return SEC_E_INSUFFICIENT_MEMORY;
1264 pPackageInfo->fCapabilities = NTLM_SecPkgInfoW.fCapabilities;
1265 pPackageInfo->wVersion = NTLM_SecPkgInfoW.wVersion;
1266 pPackageInfo->wRPCID = NTLM_SecPkgInfoW.wRPCID;
1267 pPackageInfo->cbMaxToken = NTLM_SecPkgInfoW.cbMaxToken;
1268 pPackageInfo->Name = _wcsdup(NTLM_SecPkgInfoW.Name);
1269 pPackageInfo->Comment = _wcsdup(NTLM_SecPkgInfoW.Comment);
1271 if (!pPackageInfo->Name || !pPackageInfo->Comment)
1273 sspi_ContextBufferFree(pPackageInfo);
1274 return SEC_E_INSUFFICIENT_MEMORY;
1276 PackageInfo->PackageInfo = pPackageInfo;
1280 return ntlm_QueryContextAttributesCommon(phContext, ulAttribute, pBuffer);
1285static SECURITY_STATUS utf8len(
const UNICODE_STRING* str,
void* pBuffer)
1288 WINPR_ASSERT(pBuffer);
1289 ULONG* val = (ULONG*)pBuffer;
1290 const size_t wlen = str->Length /
sizeof(WCHAR);
1291 const SSIZE_T rc = ConvertWCharNToUtf8(str->Buffer, wlen,
nullptr, 0);
1293 return SEC_E_INVALID_PARAMETER;
1294 *val = WINPR_ASSERTING_INT_CAST(ULONG, rc);
1299static SECURITY_STATUS utf8str(
const UNICODE_STRING* str,
void* pBuffer)
1302 WINPR_ASSERT(pBuffer);
1305 const SECURITY_STATUS status = utf8len(str, &len);
1306 if (status != SEC_E_OK)
1311 const size_t wlen = str->Length /
sizeof(WCHAR);
1312 const SSIZE_T rc = ConvertWCharNToUtf8(str->Buffer, wlen, pBuffer, (
size_t)len);
1313 return rc < 0 ? SEC_E_INVALID_PARAMETER : SEC_E_OK;
1317static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1318 ULONG ulAttribute,
void* pBuffer)
1321 return SEC_E_INVALID_HANDLE;
1324 return SEC_E_INSUFFICIENT_MEMORY;
1326 NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1328 switch (ulAttribute)
1330 case SECPKG_ATTR_AUTH_NTLM_HOSTNAME_LEN:
1331 return utf8len(&context->Workstation, pBuffer);
1332 case SECPKG_ATTR_AUTH_NTLM_NB_DOMAIN_NAME_LEN:
1333 return utf8len(&context->NbDomainName, pBuffer);
1334 case SECPKG_ATTR_AUTH_NTLM_NB_COMPUTER_NAME_LEN:
1335 return utf8len(&context->NbComputerName, pBuffer);
1336 case SECPKG_ATTR_AUTH_NTLM_DNS_DOMAIN_NAME_LEN:
1337 return utf8len(&context->DnsDomainName, pBuffer);
1338 case SECPKG_ATTR_AUTH_NTLM_DNS_COMPUTER_NAME_LEN:
1339 return utf8len(&context->DnsComputerName, pBuffer);
1340 case SECPKG_ATTR_AUTH_NTLM_HOSTNAME:
1341 return utf8str(&context->Workstation, pBuffer);
1342 case SECPKG_ATTR_AUTH_NTLM_NB_DOMAIN_NAME:
1343 return utf8str(&context->NbDomainName, pBuffer);
1344 case SECPKG_ATTR_AUTH_NTLM_NB_COMPUTER_NAME:
1345 return utf8str(&context->NbComputerName, pBuffer);
1346 case SECPKG_ATTR_AUTH_NTLM_DNS_DOMAIN_NAME:
1347 return utf8str(&context->DnsDomainName, pBuffer);
1348 case SECPKG_ATTR_AUTH_NTLM_DNS_COMPUTER_NAME:
1349 return utf8str(&context->DnsComputerName, pBuffer);
1350 case SECPKG_ATTR_PACKAGE_INFO:
1352 SecPkgContext_PackageInfoA* PackageInfo = (SecPkgContext_PackageInfoA*)pBuffer;
1353 size_t size =
sizeof(SecPkgInfoA);
1354 SecPkgInfoA* pPackageInfo =
1355 (SecPkgInfoA*)sspi_ContextBufferAlloc(QuerySecurityPackageInfoIndex, size);
1358 return SEC_E_INSUFFICIENT_MEMORY;
1360 pPackageInfo->fCapabilities = NTLM_SecPkgInfoA.fCapabilities;
1361 pPackageInfo->wVersion = NTLM_SecPkgInfoA.wVersion;
1362 pPackageInfo->wRPCID = NTLM_SecPkgInfoA.wRPCID;
1363 pPackageInfo->cbMaxToken = NTLM_SecPkgInfoA.cbMaxToken;
1364 pPackageInfo->Name = _strdup(NTLM_SecPkgInfoA.Name);
1365 pPackageInfo->Comment = _strdup(NTLM_SecPkgInfoA.Comment);
1367 if (!pPackageInfo->Name || !pPackageInfo->Comment)
1369 sspi_ContextBufferFree(pPackageInfo);
1370 return SEC_E_INSUFFICIENT_MEMORY;
1372 PackageInfo->PackageInfo = pPackageInfo;
1377 return ntlm_QueryContextAttributesCommon(phContext, ulAttribute, pBuffer);
1382static SECURITY_STATUS SEC_ENTRY ntlm_SetContextAttributesCommon(PCtxtHandle phContext,
1383 ULONG ulAttribute,
void* pBuffer,
1387 return SEC_E_INVALID_HANDLE;
1390 return SEC_E_INVALID_PARAMETER;
1392 NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1394 return SEC_E_INVALID_HANDLE;
1396 switch (ulAttribute)
1398 case SECPKG_ATTR_AUTH_NTLM_HASH:
1400 SecPkgContext_AuthNtlmHash* AuthNtlmHash = (SecPkgContext_AuthNtlmHash*)pBuffer;
1402 if (cbBuffer <
sizeof(SecPkgContext_AuthNtlmHash))
1403 return SEC_E_INVALID_PARAMETER;
1405 if (AuthNtlmHash->Version == 1)
1406 CopyMemory(context->NtlmHash, AuthNtlmHash->NtlmHash, 16);
1407 else if (AuthNtlmHash->Version == 2)
1408 CopyMemory(context->NtlmV2Hash, AuthNtlmHash->NtlmHash, 16);
1413 case SECPKG_ATTR_AUTH_NTLM_MESSAGE:
1415 SecPkgContext_AuthNtlmMessage* AuthNtlmMessage =
1416 (SecPkgContext_AuthNtlmMessage*)pBuffer;
1418 if (cbBuffer <
sizeof(SecPkgContext_AuthNtlmMessage))
1419 return SEC_E_INVALID_PARAMETER;
1421 if (AuthNtlmMessage->type == 1)
1423 if (!ntlm_SecBufferRealloc(&context->NegotiateMessage, AuthNtlmMessage->length))
1424 return SEC_E_INSUFFICIENT_MEMORY;
1426 CopyMemory(context->NegotiateMessage.pvBuffer, AuthNtlmMessage->buffer,
1427 AuthNtlmMessage->length);
1429 else if (AuthNtlmMessage->type == 2)
1431 if (!ntlm_SecBufferRealloc(&context->ChallengeMessage, AuthNtlmMessage->length))
1432 return SEC_E_INSUFFICIENT_MEMORY;
1434 CopyMemory(context->ChallengeMessage.pvBuffer, AuthNtlmMessage->buffer,
1435 AuthNtlmMessage->length);
1437 else if (AuthNtlmMessage->type == 3)
1439 if (!ntlm_SecBufferRealloc(&context->AuthenticateMessage, AuthNtlmMessage->length))
1440 return SEC_E_INSUFFICIENT_MEMORY;
1442 CopyMemory(context->AuthenticateMessage.pvBuffer, AuthNtlmMessage->buffer,
1443 AuthNtlmMessage->length);
1449 case SECPKG_ATTR_AUTH_NTLM_TIMESTAMP:
1451 SecPkgContext_AuthNtlmTimestamp* AuthNtlmTimestamp =
1452 (SecPkgContext_AuthNtlmTimestamp*)pBuffer;
1454 if (cbBuffer <
sizeof(SecPkgContext_AuthNtlmTimestamp))
1455 return SEC_E_INVALID_PARAMETER;
1457 if (AuthNtlmTimestamp->ChallengeOrResponse)
1458 CopyMemory(context->ChallengeTimestamp, AuthNtlmTimestamp->Timestamp, 8);
1460 CopyMemory(context->Timestamp, AuthNtlmTimestamp->Timestamp, 8);
1465 case SECPKG_ATTR_AUTH_NTLM_CLIENT_CHALLENGE:
1467 SecPkgContext_AuthNtlmClientChallenge* AuthNtlmClientChallenge =
1468 (SecPkgContext_AuthNtlmClientChallenge*)pBuffer;
1470 if (cbBuffer <
sizeof(SecPkgContext_AuthNtlmClientChallenge))
1471 return SEC_E_INVALID_PARAMETER;
1473 CopyMemory(context->ClientChallenge, AuthNtlmClientChallenge->ClientChallenge, 8);
1477 case SECPKG_ATTR_AUTH_NTLM_SERVER_CHALLENGE:
1479 SecPkgContext_AuthNtlmServerChallenge* AuthNtlmServerChallenge =
1480 (SecPkgContext_AuthNtlmServerChallenge*)pBuffer;
1482 if (cbBuffer <
sizeof(SecPkgContext_AuthNtlmServerChallenge))
1483 return SEC_E_INVALID_PARAMETER;
1485 CopyMemory(context->ServerChallenge, AuthNtlmServerChallenge->ServerChallenge, 8);
1490 WLog_ERR(
TAG,
"TODO: Implement ulAttribute=%08" PRIx32, ulAttribute);
1491 return SEC_E_UNSUPPORTED_FUNCTION;
1496static SECURITY_STATUS ntml_setUnicodeStringW(UNICODE_STRING* str,
const WCHAR* val,
size_t bytelen)
1499 ntlm_free_unicode_string(str);
1500 *str = ntlm_from_unicode_string_w(val, bytelen /
sizeof(WCHAR));
1501 if (ntlm_is_unicode_string_empty(str))
1502 return SEC_E_INVALID_PARAMETER;
1507static SECURITY_STATUS utf16len(
const UNICODE_STRING* str,
void* pBuffer)
1510 WINPR_ASSERT(pBuffer);
1511 ULONG* val = (ULONG*)pBuffer;
1517static SECURITY_STATUS SEC_ENTRY ntlm_SetContextAttributesW(PCtxtHandle phContext,
1518 ULONG ulAttribute,
void* pBuffer,
1522 return SEC_E_INVALID_HANDLE;
1525 return SEC_E_INVALID_PARAMETER;
1527 NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1529 return SEC_E_INVALID_HANDLE;
1531 switch (ulAttribute)
1533 case SECPKG_ATTR_AUTH_NTLM_HOSTNAME_LEN:
1534 return utf16len(&context->Workstation, pBuffer);
1535 case SECPKG_ATTR_AUTH_NTLM_NB_DOMAIN_NAME_LEN:
1536 return utf16len(&context->NbDomainName, pBuffer);
1537 case SECPKG_ATTR_AUTH_NTLM_NB_COMPUTER_NAME_LEN:
1538 return utf16len(&context->NbComputerName, pBuffer);
1539 case SECPKG_ATTR_AUTH_NTLM_DNS_DOMAIN_NAME_LEN:
1540 return utf16len(&context->DnsDomainName, pBuffer);
1541 case SECPKG_ATTR_AUTH_NTLM_DNS_COMPUTER_NAME_LEN:
1542 return utf16len(&context->DnsComputerName, pBuffer);
1543 case SECPKG_ATTR_AUTH_NTLM_HOSTNAME:
1544 return ntml_setUnicodeStringW(&context->Workstation, pBuffer, cbBuffer);
1545 case SECPKG_ATTR_AUTH_NTLM_NB_DOMAIN_NAME:
1546 return ntml_setUnicodeStringW(&context->NbDomainName, pBuffer, cbBuffer);
1547 case SECPKG_ATTR_AUTH_NTLM_NB_COMPUTER_NAME:
1548 return ntml_setUnicodeStringW(&context->NbComputerName, pBuffer, cbBuffer);
1549 case SECPKG_ATTR_AUTH_NTLM_DNS_DOMAIN_NAME:
1550 return ntml_setUnicodeStringW(&context->DnsDomainName, pBuffer, cbBuffer);
1551 case SECPKG_ATTR_AUTH_NTLM_DNS_COMPUTER_NAME:
1552 return ntml_setUnicodeStringW(&context->DnsComputerName, pBuffer, cbBuffer);
1555 return ntlm_SetContextAttributesCommon(phContext, ulAttribute, pBuffer, cbBuffer);
1559SECURITY_STATUS ntml_setUnicodeStringA(UNICODE_STRING* str,
const char* val,
size_t charlen)
1562 ntlm_free_unicode_string(str);
1563 *str = ntlm_from_unicode_string_utf8(val, charlen);
1564 if (ntlm_is_unicode_string_empty(str))
1565 return SEC_E_INVALID_PARAMETER;
1570static SECURITY_STATUS SEC_ENTRY ntlm_SetContextAttributesA(PCtxtHandle phContext,
1571 ULONG ulAttribute,
void* pBuffer,
1575 return SEC_E_INVALID_HANDLE;
1578 return SEC_E_INVALID_PARAMETER;
1580 NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1582 return SEC_E_INVALID_HANDLE;
1584 switch (ulAttribute)
1586 case SECPKG_ATTR_AUTH_NTLM_HOSTNAME:
1587 return ntml_setUnicodeStringA(&context->Workstation, pBuffer, cbBuffer);
1588 case SECPKG_ATTR_AUTH_NTLM_NB_DOMAIN_NAME:
1589 return ntml_setUnicodeStringA(&context->NbDomainName, pBuffer, cbBuffer);
1590 case SECPKG_ATTR_AUTH_NTLM_NB_COMPUTER_NAME:
1591 return ntml_setUnicodeStringA(&context->NbComputerName, pBuffer, cbBuffer);
1592 case SECPKG_ATTR_AUTH_NTLM_DNS_DOMAIN_NAME:
1593 return ntml_setUnicodeStringA(&context->DnsDomainName, pBuffer, cbBuffer);
1594 case SECPKG_ATTR_AUTH_NTLM_DNS_COMPUTER_NAME:
1595 return ntml_setUnicodeStringA(&context->DnsComputerName, pBuffer, cbBuffer);
1597 return ntlm_SetContextAttributesCommon(phContext, ulAttribute, pBuffer, cbBuffer);
1602static SECURITY_STATUS SEC_ENTRY ntlm_SetCredentialsAttributesW(
1603 WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute,
1604 WINPR_ATTR_UNUSED
void* pBuffer, WINPR_ATTR_UNUSED ULONG cbBuffer)
1606 return SEC_E_UNSUPPORTED_FUNCTION;
1610static SECURITY_STATUS SEC_ENTRY ntlm_SetCredentialsAttributesA(
1611 WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute,
1612 WINPR_ATTR_UNUSED
void* pBuffer, WINPR_ATTR_UNUSED ULONG cbBuffer)
1614 return SEC_E_UNSUPPORTED_FUNCTION;
1618static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(WINPR_ATTR_UNUSED PCtxtHandle phContext)
1624static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1625 WINPR_ATTR_UNUSED ULONG fQOP,
1626 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1628 const UINT32 SeqNo = MessageSeqNo;
1630 BYTE digest[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
1631 BYTE checksum[8] = WINPR_C_ARRAY_INIT;
1633 PSecBuffer data_buffer =
nullptr;
1634 PSecBuffer signature_buffer =
nullptr;
1635 NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1636 if (!check_context(context))
1637 return SEC_E_INVALID_HANDLE;
1639 for (ULONG index = 0; index < pMessage->cBuffers; index++)
1641 SecBuffer* cur = &pMessage->pBuffers[index];
1643 if (cur->BufferType & SECBUFFER_DATA)
1645 else if (cur->BufferType & SECBUFFER_TOKEN)
1646 signature_buffer = cur;
1650 return SEC_E_INVALID_TOKEN;
1652 if (!signature_buffer)
1653 return SEC_E_INVALID_TOKEN;
1655 if (signature_buffer->cbBuffer < 16)
1656 return SEC_E_INSUFFICIENT_MEMORY;
1659 ULONG length = data_buffer->cbBuffer;
1660 void* data = malloc(length);
1663 return SEC_E_INSUFFICIENT_MEMORY;
1665 CopyMemory(data, data_buffer->pvBuffer, length);
1667 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
1669 BOOL success = FALSE;
1673 if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->SendSigningKey, WINPR_MD5_DIGEST_LENGTH))
1676 winpr_Data_Write_UINT32(&value, SeqNo);
1678 if (!winpr_HMAC_Update(hmac, (
void*)&value, 4))
1680 if (!winpr_HMAC_Update(hmac, data, length))
1682 if (!winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH))
1689 winpr_HMAC_Free(hmac);
1693 return SEC_E_INSUFFICIENT_MEMORY;
1697 if ((data_buffer->BufferType & SECBUFFER_READONLY) == 0)
1699 if (context->confidentiality)
1701 if (!winpr_RC4_Update(context->SendRc4Seal, length, (BYTE*)data,
1702 (BYTE*)data_buffer->pvBuffer))
1705 return SEC_E_INSUFFICIENT_MEMORY;
1709 CopyMemory(data_buffer->pvBuffer, data, length);
1712#ifdef WITH_DEBUG_NTLM
1713 WLog_DBG(
TAG,
"Data Buffer (length = %" PRIu32
")", length);
1714 winpr_HexDump(
TAG, WLOG_DEBUG, data, length);
1715 WLog_DBG(
TAG,
"Encrypted Data Buffer (length = %" PRIu32
")", data_buffer->cbBuffer);
1716 winpr_HexDump(
TAG, WLOG_DEBUG, data_buffer->pvBuffer, data_buffer->cbBuffer);
1720 if (!winpr_RC4_Update(context->SendRc4Seal, 8, digest, checksum))
1721 return SEC_E_INSUFFICIENT_MEMORY;
1722 if ((signature_buffer->BufferType & SECBUFFER_READONLY) == 0)
1724 BYTE* signature = signature_buffer->pvBuffer;
1726 winpr_Data_Write_UINT32(signature, version);
1727 CopyMemory(&signature[4], (
void*)checksum, 8);
1728 winpr_Data_Write_UINT32(&signature[12], SeqNo);
1730 context->SendSeqNum++;
1731#ifdef WITH_DEBUG_NTLM
1732 WLog_DBG(
TAG,
"Signature (length = %" PRIu32
")", signature_buffer->cbBuffer);
1733 winpr_HexDump(
TAG, WLOG_DEBUG, signature_buffer->pvBuffer, signature_buffer->cbBuffer);
1738static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext, PSecBufferDesc pMessage,
1740 WINPR_ATTR_UNUSED PULONG pfQOP)
1742 const UINT32 SeqNo = (UINT32)MessageSeqNo;
1744 BYTE digest[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
1745 BYTE checksum[8] = WINPR_C_ARRAY_INIT;
1747 BYTE expected_signature[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
1748 PSecBuffer data_buffer =
nullptr;
1749 PSecBuffer signature_buffer =
nullptr;
1750 NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1751 if (!check_context(context))
1752 return SEC_E_INVALID_HANDLE;
1754 for (ULONG index = 0; index < pMessage->cBuffers; index++)
1756 if (pMessage->pBuffers[index].BufferType == SECBUFFER_DATA)
1757 data_buffer = &pMessage->pBuffers[index];
1758 else if (pMessage->pBuffers[index].BufferType == SECBUFFER_TOKEN)
1759 signature_buffer = &pMessage->pBuffers[index];
1763 return SEC_E_INVALID_TOKEN;
1765 if (!signature_buffer)
1766 return SEC_E_INVALID_TOKEN;
1768 if (signature_buffer->cbBuffer < 16)
1769 return SEC_E_INVALID_TOKEN;
1772 const ULONG length = data_buffer->cbBuffer;
1773 void* data = malloc(length);
1776 return SEC_E_INSUFFICIENT_MEMORY;
1778 CopyMemory(data, data_buffer->pvBuffer, length);
1782 if (context->confidentiality)
1784 if (!winpr_RC4_Update(context->RecvRc4Seal, length, (BYTE*)data,
1785 (BYTE*)data_buffer->pvBuffer))
1788 return SEC_E_INSUFFICIENT_MEMORY;
1792 CopyMemory(data_buffer->pvBuffer, data, length);
1795 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
1797 BOOL success = FALSE;
1802 if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->RecvSigningKey, WINPR_MD5_DIGEST_LENGTH))
1805 winpr_Data_Write_UINT32(&value, SeqNo);
1807 if (!winpr_HMAC_Update(hmac, (
void*)&value, 4))
1809 if (!winpr_HMAC_Update(hmac, data_buffer->pvBuffer, data_buffer->cbBuffer))
1811 if (!winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH))
1817 winpr_HMAC_Free(hmac);
1821 return SEC_E_INSUFFICIENT_MEMORY;
1824#ifdef WITH_DEBUG_NTLM
1825 WLog_DBG(
TAG,
"Encrypted Data Buffer (length = %" PRIu32
")", length);
1826 winpr_HexDump(
TAG, WLOG_DEBUG, data, length);
1827 WLog_DBG(
TAG,
"Data Buffer (length = %" PRIu32
")", data_buffer->cbBuffer);
1828 winpr_HexDump(
TAG, WLOG_DEBUG, data_buffer->pvBuffer, data_buffer->cbBuffer);
1832 if (!winpr_RC4_Update(context->RecvRc4Seal, 8, digest, checksum))
1833 return SEC_E_MESSAGE_ALTERED;
1836 winpr_Data_Write_UINT32(expected_signature, version);
1837 CopyMemory(&expected_signature[4], (
void*)checksum, 8);
1838 winpr_Data_Write_UINT32(&expected_signature[12], SeqNo);
1839 context->RecvSeqNum++;
1841 if (memcmp(signature_buffer->pvBuffer, expected_signature, 16) != 0)
1844 WLog_ERR(
TAG,
"signature verification failed, something nasty is going on!");
1845#ifdef WITH_DEBUG_NTLM
1846 WLog_ERR(
TAG,
"Expected Signature:");
1847 winpr_HexDump(
TAG, WLOG_ERROR, expected_signature, 16);
1848 WLog_ERR(
TAG,
"Actual Signature:");
1849 winpr_HexDump(
TAG, WLOG_ERROR, (BYTE*)signature_buffer->pvBuffer, 16);
1851 return SEC_E_MESSAGE_ALTERED;
1857static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext,
1858 WINPR_ATTR_UNUSED ULONG fQOP,
1859 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1861 SECURITY_STATUS status = SEC_E_INTERNAL_ERROR;
1862 PSecBuffer data_buffer =
nullptr;
1863 PSecBuffer sig_buffer =
nullptr;
1865 BYTE digest[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
1866 BYTE checksum[8] = WINPR_C_ARRAY_INIT;
1868 NTLM_CONTEXT* context = sspi_SecureHandleGetLowerPointer(phContext);
1869 if (!check_context(context))
1870 return SEC_E_INVALID_HANDLE;
1872 for (ULONG i = 0; i < pMessage->cBuffers; i++)
1874 if (pMessage->pBuffers[i].BufferType == SECBUFFER_DATA)
1875 data_buffer = &pMessage->pBuffers[i];
1876 else if (pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1877 sig_buffer = &pMessage->pBuffers[i];
1880 if (!data_buffer || !sig_buffer)
1881 return SEC_E_INVALID_TOKEN;
1883 if (sig_buffer->cbBuffer < 16)
1884 return SEC_E_INSUFFICIENT_MEMORY;
1886 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
1888 if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->SendSigningKey, WINPR_MD5_DIGEST_LENGTH))
1891 winpr_Data_Write_UINT32(&seq_no, MessageSeqNo);
1892 if (!winpr_HMAC_Update(hmac, (BYTE*)&seq_no, 4))
1894 if (!winpr_HMAC_Update(hmac, data_buffer->pvBuffer, data_buffer->cbBuffer))
1896 if (!winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH))
1899 if (!winpr_RC4_Update(context->SendRc4Seal, 8, digest, checksum))
1902 BYTE* signature = sig_buffer->pvBuffer;
1903 winpr_Data_Write_UINT32(signature, 1L);
1904 CopyMemory(&signature[4], checksum, 8);
1905 winpr_Data_Write_UINT32(&signature[12], seq_no);
1906 sig_buffer->cbBuffer = 16;
1911 winpr_HMAC_Free(hmac);
1916static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1917 PSecBufferDesc pMessage, ULONG MessageSeqNo,
1918 WINPR_ATTR_UNUSED PULONG pfQOP)
1920 SECURITY_STATUS status = SEC_E_INTERNAL_ERROR;
1921 PSecBuffer data_buffer =
nullptr;
1922 PSecBuffer sig_buffer =
nullptr;
1924 BYTE digest[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
1925 BYTE checksum[8] = WINPR_C_ARRAY_INIT;
1926 BYTE signature[16] = WINPR_C_ARRAY_INIT;
1928 NTLM_CONTEXT* context = sspi_SecureHandleGetLowerPointer(phContext);
1929 if (!check_context(context))
1930 return SEC_E_INVALID_HANDLE;
1932 for (ULONG i = 0; i < pMessage->cBuffers; i++)
1934 if (pMessage->pBuffers[i].BufferType == SECBUFFER_DATA)
1935 data_buffer = &pMessage->pBuffers[i];
1936 else if (pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1937 sig_buffer = &pMessage->pBuffers[i];
1940 if (!data_buffer || !sig_buffer || (sig_buffer->cbBuffer < 16))
1941 return SEC_E_INVALID_TOKEN;
1943 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
1945 if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->RecvSigningKey, WINPR_MD5_DIGEST_LENGTH))
1948 winpr_Data_Write_UINT32(&seq_no, MessageSeqNo);
1949 if (!winpr_HMAC_Update(hmac, (BYTE*)&seq_no, 4))
1951 if (!winpr_HMAC_Update(hmac, data_buffer->pvBuffer, data_buffer->cbBuffer))
1953 if (!winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH))
1956 if (!winpr_RC4_Update(context->RecvRc4Seal, 8, digest, checksum))
1959 winpr_Data_Write_UINT32(signature, 1L);
1960 CopyMemory(&signature[4], checksum, 8);
1961 winpr_Data_Write_UINT32(&signature[12], seq_no);
1964 if (memcmp(sig_buffer->pvBuffer, signature, 16) != 0)
1965 status = SEC_E_MESSAGE_ALTERED;
1968 winpr_HMAC_Free(hmac);
1972const SecurityFunctionTableA NTLM_SecurityFunctionTableA = {
1975 ntlm_QueryCredentialsAttributesA,
1976 ntlm_AcquireCredentialsHandleA,
1977 ntlm_FreeCredentialsHandle,
1979 ntlm_InitializeSecurityContextA,
1980 ntlm_AcceptSecurityContext,
1982 ntlm_DeleteSecurityContext,
1984 ntlm_QueryContextAttributesA,
1985 ntlm_ImpersonateSecurityContext,
1986 ntlm_RevertSecurityContext,
1988 ntlm_VerifySignature,
1998 ntlm_EncryptMessage,
1999 ntlm_DecryptMessage,
2000 ntlm_SetContextAttributesA,
2001 ntlm_SetCredentialsAttributesA,
2004const SecurityFunctionTableW NTLM_SecurityFunctionTableW = {
2007 ntlm_QueryCredentialsAttributesW,
2008 ntlm_AcquireCredentialsHandleW,
2009 ntlm_FreeCredentialsHandle,
2011 ntlm_InitializeSecurityContextW,
2012 ntlm_AcceptSecurityContext,
2014 ntlm_DeleteSecurityContext,
2016 ntlm_QueryContextAttributesW,
2017 ntlm_ImpersonateSecurityContext,
2018 ntlm_RevertSecurityContext,
2020 ntlm_VerifySignature,
2030 ntlm_EncryptMessage,
2031 ntlm_DecryptMessage,
2032 ntlm_SetContextAttributesW,
2033 ntlm_SetCredentialsAttributesW,
2036const SecPkgInfoA NTLM_SecPkgInfoA = {
2042 "NTLM Security Package"
2045static WCHAR NTLM_SecPkgInfoW_NameBuffer[32] = WINPR_C_ARRAY_INIT;
2046static WCHAR NTLM_SecPkgInfoW_CommentBuffer[32] = WINPR_C_ARRAY_INIT;
2048const SecPkgInfoW NTLM_SecPkgInfoW = {
2053 NTLM_SecPkgInfoW_NameBuffer,
2054 NTLM_SecPkgInfoW_CommentBuffer
2057char* ntlm_negotiate_flags_string(
char* buffer,
size_t size, UINT32 flags)
2059 if (!buffer || (size == 0))
2062 (void)_snprintf(buffer, size,
"[0x%08" PRIx32
"] ", flags);
2064 for (
int x = 0; x < 31; x++)
2066 const UINT32 mask = 1u << x;
2067 size_t len = strnlen(buffer, size);
2070 const char* str = ntlm_get_negotiate_string(mask);
2071 const size_t flen = strlen(str);
2073 if ((len > 0) && (buffer[len - 1] !=
' '))
2077 winpr_str_append(
"|", buffer, size,
nullptr);
2081 if (size - len < flen)
2083 winpr_str_append(str, buffer, size,
nullptr);
2090const char* ntlm_message_type_string(UINT32 messageType)
2092 switch (messageType)
2094 case MESSAGE_TYPE_NEGOTIATE:
2095 return "MESSAGE_TYPE_NEGOTIATE";
2096 case MESSAGE_TYPE_CHALLENGE:
2097 return "MESSAGE_TYPE_CHALLENGE";
2098 case MESSAGE_TYPE_AUTHENTICATE:
2099 return "MESSAGE_TYPE_AUTHENTICATE";
2101 return "MESSAGE_TYPE_UNKNOWN";
2105const char* ntlm_state_string(NTLM_STATE state)
2109 case NTLM_STATE_INITIAL:
2110 return "NTLM_STATE_INITIAL";
2111 case NTLM_STATE_NEGOTIATE:
2112 return "NTLM_STATE_NEGOTIATE";
2113 case NTLM_STATE_CHALLENGE:
2114 return "NTLM_STATE_CHALLENGE";
2115 case NTLM_STATE_AUTHENTICATE:
2116 return "NTLM_STATE_AUTHENTICATE";
2117 case NTLM_STATE_FINAL:
2118 return "NTLM_STATE_FINAL";
2120 return "NTLM_STATE_UNKNOWN";
2123void ntlm_change_state(NTLM_CONTEXT* ntlm, NTLM_STATE state)
2126 WLog_DBG(
TAG,
"change state from %s to %s", ntlm_state_string(ntlm->state),
2127 ntlm_state_string(state));
2128 ntlm->state = state;
2131NTLM_STATE ntlm_get_state(NTLM_CONTEXT* ntlm)
2137BOOL ntlm_reset_cipher_state(PSecHandle phContext)
2139 NTLM_CONTEXT* context = sspi_SecureHandleGetLowerPointer(phContext);
2143 if (!check_context(context))
2146 winpr_RC4_Free(context->SendRc4Seal);
2147 winpr_RC4_Free(context->RecvRc4Seal);
2148 context->SendRc4Seal = winpr_RC4_New(context->RecvSealingKey, 16);
2149 context->RecvRc4Seal = winpr_RC4_New(context->SendSealingKey, 16);
2151 if (!context->SendRc4Seal)
2153 WLog_ERR(
TAG,
"Failed to allocate context->SendRc4Seal");
2156 if (!context->RecvRc4Seal)
2158 WLog_ERR(
TAG,
"Failed to allocate context->RecvRc4Seal");
2168 InitializeConstWCharFromUtf8(NTLM_SecPkgInfoA.Name, NTLM_SecPkgInfoW_NameBuffer,
2169 ARRAYSIZE(NTLM_SecPkgInfoW_NameBuffer));
2170 InitializeConstWCharFromUtf8(NTLM_SecPkgInfoA.Comment, NTLM_SecPkgInfoW_CommentBuffer,
2171 ARRAYSIZE(NTLM_SecPkgInfoW_CommentBuffer));
2176BOOL ntlm_SecBufferRealloc(SecBuffer* buffer, ULONG len)
2178 sspi_SecBufferFree(buffer);
2179 return sspi_SecBufferAlloc(buffer, len) !=
nullptr;
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
WINPR_ATTR_NODISCARD psSspiNtlmHashCallback hashCallback