21#include <winpr/config.h>
22#include <winpr/assert.h>
23#include <winpr/windows.h>
26#include <winpr/synch.h>
27#include <winpr/sspi.h>
29#include <winpr/print.h>
33#include "sspi_winpr.h"
37#define TAG WINPR_TAG("sspi")
42#include "NTLM/ntlm_export.h"
43#include "CredSSP/credssp.h"
44#include "Kerberos/kerberos.h"
45#include "Negotiate/negotiate.h"
46#include "Schannel/schannel.h"
47#include "TSSSP/tsssp.h"
49static const SecPkgInfoA* SecPkgInfoA_LIST[] = { &NTLM_SecPkgInfoA, &KERBEROS_SecPkgInfoA,
50 &NEGOTIATE_SecPkgInfoA, &CREDSSP_SecPkgInfoA,
51 &SCHANNEL_SecPkgInfoA, &TSSSP_SecPkgInfoA };
53static const SecPkgInfoW* SecPkgInfoW_LIST[] = { &NTLM_SecPkgInfoW, &KERBEROS_SecPkgInfoW,
54 &NEGOTIATE_SecPkgInfoW, &CREDSSP_SecPkgInfoW,
55 &SCHANNEL_SecPkgInfoW, &TSSSP_SecPkgInfoW };
61} SecurityFunctionTableA_NAME;
65 const SEC_WCHAR* Name;
67} SecurityFunctionTableW_NAME;
69static const SecurityFunctionTableA_NAME SecurityFunctionTableA_NAME_LIST[] = {
70 {
"NTLM", &NTLM_SecurityFunctionTableA },
71 {
"Kerberos", &KERBEROS_SecurityFunctionTableA },
72 {
"Negotiate", &NEGOTIATE_SecurityFunctionTableA },
73 {
"CREDSSP", &CREDSSP_SecurityFunctionTableA },
74 {
"Schannel", &SCHANNEL_SecurityFunctionTableA },
75 {
"TSSSP", &TSSSP_SecurityFunctionTableA }
78static WCHAR BUFFER_NAME_LIST_W[6][32] = WINPR_C_ARRAY_INIT;
80static const SecurityFunctionTableW_NAME SecurityFunctionTableW_NAME_LIST[] = {
81 { BUFFER_NAME_LIST_W[0], &NTLM_SecurityFunctionTableW },
82 { BUFFER_NAME_LIST_W[1], &KERBEROS_SecurityFunctionTableW },
83 { BUFFER_NAME_LIST_W[2], &NEGOTIATE_SecurityFunctionTableW },
84 { BUFFER_NAME_LIST_W[3], &CREDSSP_SecurityFunctionTableW },
85 { BUFFER_NAME_LIST_W[4], &SCHANNEL_SecurityFunctionTableW },
86 { BUFFER_NAME_LIST_W[5], &TSSSP_SecurityFunctionTableW }
92 UINT32 allocatorIndex;
93} CONTEXT_BUFFER_ALLOC_ENTRY;
99 CONTEXT_BUFFER_ALLOC_ENTRY* entries;
100} CONTEXT_BUFFER_ALLOC_TABLE;
102static CONTEXT_BUFFER_ALLOC_TABLE ContextBufferAllocTable = WINPR_C_ARRAY_INIT;
105static CRITICAL_SECTION ContextBufferAllocTableLock = WINPR_C_ARRAY_INIT;
107static int sspi_ContextBufferAllocTableNew(
void)
109 if (!InitializeCriticalSectionEx(&ContextBufferAllocTableLock, 0, 0))
112 ContextBufferAllocTable.entries =
nullptr;
113 ContextBufferAllocTable.cEntries = 0;
114 ContextBufferAllocTable.cMaxEntries = 4;
115 const size_t size =
sizeof(CONTEXT_BUFFER_ALLOC_ENTRY) * ContextBufferAllocTable.cMaxEntries;
116 ContextBufferAllocTable.entries = (CONTEXT_BUFFER_ALLOC_ENTRY*)calloc(1, size);
118 if (!ContextBufferAllocTable.entries)
120 DeleteCriticalSection(&ContextBufferAllocTableLock);
128static int sspi_ContextBufferAllocTableGrow(
void)
130 const UINT32 cMaxEntries = ContextBufferAllocTable.cMaxEntries + 128ull;
131 const size_t size =
sizeof(CONTEXT_BUFFER_ALLOC_ENTRY) * cMaxEntries;
136 CONTEXT_BUFFER_ALLOC_ENTRY* entries =
137 (CONTEXT_BUFFER_ALLOC_ENTRY*)realloc(ContextBufferAllocTable.entries, size);
143 ContextBufferAllocTable.entries = entries;
144 ZeroMemory(&ContextBufferAllocTable.entries[ContextBufferAllocTable.cMaxEntries],
145 cMaxEntries - ContextBufferAllocTable.cMaxEntries);
146 ContextBufferAllocTable.cMaxEntries = cMaxEntries;
150static void sspi_ContextBufferAllocTableFree(
void)
152 if (ContextBufferAllocTable.cEntries != 0)
153 WLog_ERR(TAG,
"ContextBufferAllocTable.entries == %" PRIu32,
154 ContextBufferAllocTable.cEntries);
156 ContextBufferAllocTable.cEntries = ContextBufferAllocTable.cMaxEntries = 0;
157 free(ContextBufferAllocTable.entries);
158 ContextBufferAllocTable.entries =
nullptr;
159 DeleteCriticalSection(&ContextBufferAllocTableLock);
163static void* sspi_ContextBufferAllocLocked(UINT32 allocatorIndex,
size_t size)
165 void* contextBuffer =
nullptr;
167 for (UINT32 index = 0; index < ContextBufferAllocTable.cMaxEntries; index++)
169 if (!ContextBufferAllocTable.entries[index].contextBuffer)
171 contextBuffer = calloc(1, size);
176 ContextBufferAllocTable.cEntries++;
177 ContextBufferAllocTable.entries[index].contextBuffer = contextBuffer;
178 ContextBufferAllocTable.entries[index].allocatorIndex = allocatorIndex;
179 return ContextBufferAllocTable.entries[index].contextBuffer;
185 if (sspi_ContextBufferAllocTableGrow() < 0)
189 return sspi_ContextBufferAllocLocked(allocatorIndex, size);
192void* sspi_ContextBufferAlloc(UINT32 allocatorIndex,
size_t size)
194 EnterCriticalSection(&ContextBufferAllocTableLock);
195 void* contextBuffer = sspi_ContextBufferAllocLocked(allocatorIndex, size);
196 LeaveCriticalSection(&ContextBufferAllocTableLock);
197 return contextBuffer;
206 credentials->ntlmSettingsV2 = sspi_AllocSecNtlmSettings();
207 if (!credentials->ntlmSettingsV2)
209 sspi_CredentialsFree(credentials);
221 size_t userLength = credentials->identity.UserLength;
222 size_t domainLength = credentials->identity.DomainLength;
223 size_t passwordLength = credentials->identity.PasswordLength;
225 if (credentials->identity.Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
232 if (credentials->identity.User)
233 memset(credentials->identity.User, 0, userLength);
234 if (credentials->identity.Domain)
235 memset(credentials->identity.Domain, 0, domainLength);
236 if (credentials->identity.Password)
237 memset(credentials->identity.Password, 0, passwordLength);
238 free(credentials->identity.User);
239 free(credentials->identity.Domain);
240 free(credentials->identity.Password);
241 sspi_FreeSecNtlmSettings(credentials->ntlmSettingsV2);
246void* sspi_SecBufferAlloc(PSecBuffer
SecBuffer, ULONG size)
260void sspi_SecBufferFree(PSecBuffer
SecBuffer)
280 SecInvalidateHandle(handle);
284void* sspi_SecureHandleGetLowerPointer(
SecHandle* handle)
286 void* pointer =
nullptr;
288 if (!handle || !SecIsValidHandle(handle) || !handle->dwLower)
291 pointer = (
void*)~((
size_t)handle->dwLower);
295void sspi_SecureHandleInvalidate(
SecHandle* handle)
304void sspi_SecureHandleSetLowerPointer(
SecHandle* handle,
void* pointer)
309 handle->dwLower = (ULONG_PTR)(~((
size_t)pointer));
312void* sspi_SecureHandleGetUpperPointer(
SecHandle* handle)
314 void* pointer =
nullptr;
316 if (!handle || !SecIsValidHandle(handle) || !handle->dwUpper)
319 pointer = (
void*)~((
size_t)handle->dwUpper);
323void sspi_SecureHandleSetUpperPointer(
SecHandle* handle,
void* pointer)
328 handle->dwUpper = (ULONG_PTR)(~((
size_t)pointer));
331SSPI_PACKAGE_ID sspi_SecureHandleGetPackageId(
SecHandle* handle)
333 if (!handle || !SecIsValidHandle(handle) || !handle->dwUpper)
334 return SSPI_PACKAGE_NONE;
336 return (SSPI_PACKAGE_ID)(~((size_t)handle->dwUpper));
339void sspi_SecureHandleSetPackageId(
SecHandle* handle, SSPI_PACKAGE_ID
id)
344 handle->dwUpper = (ULONG_PTR)(~((
size_t)
id));
347void sspi_SecureHandleFree(
SecHandle* handle)
352int sspi_SetAuthIdentityW(SEC_WINNT_AUTH_IDENTITY* identity,
const WCHAR* user,
const WCHAR* domain,
353 const WCHAR* password)
355 return sspi_SetAuthIdentityWithLengthW(identity, user, user ? _wcslen(user) : 0, domain,
356 domain ? _wcslen(domain) : 0, password,
357 password ? _wcslen(password) : 0);
360static BOOL copy(WCHAR** dst, ULONG* dstLen,
const WCHAR* what,
size_t len)
363 WINPR_ASSERT(dstLen);
368 if (len > UINT32_MAX)
372 if (!what && (len != 0))
374 if (!what && (len == 0))
377 *dst = calloc(
sizeof(WCHAR), len + 1);
381 memcpy(*dst, what, len *
sizeof(WCHAR));
382 *dstLen = WINPR_ASSERTING_INT_CAST(UINT32, len);
386int sspi_SetAuthIdentityWithLengthW(SEC_WINNT_AUTH_IDENTITY* identity,
const WCHAR* user,
387 size_t userLen,
const WCHAR* domain,
size_t domainLen,
388 const WCHAR* password,
size_t passwordLen)
390 WINPR_ASSERT(identity);
391 sspi_FreeAuthIdentity(identity);
392 identity->Flags &= (uint32_t)~SEC_WINNT_AUTH_IDENTITY_ANSI;
393 identity->Flags |= SEC_WINNT_AUTH_IDENTITY_UNICODE;
395 if (!copy(&identity->User, &identity->UserLength, user, userLen))
398 if (!copy(&identity->Domain, &identity->DomainLength, domain, domainLen))
401 if (!copy(&identity->Password, &identity->PasswordLength, password, passwordLen))
407int sspi_SetAuthIdentityA(SEC_WINNT_AUTH_IDENTITY* identity,
const char* user,
const char* domain,
408 const char* password)
411 size_t unicodeUserLenW = 0;
412 size_t unicodeDomainLenW = 0;
413 size_t unicodePasswordLenW = 0;
414 LPWSTR unicodeUser =
nullptr;
415 LPWSTR unicodeDomain =
nullptr;
416 LPWSTR unicodePassword =
nullptr;
419 unicodeUser = ConvertUtf8ToWCharAlloc(user, &unicodeUserLenW);
422 unicodeDomain = ConvertUtf8ToWCharAlloc(domain, &unicodeDomainLenW);
425 unicodePassword = ConvertUtf8ToWCharAlloc(password, &unicodePasswordLenW);
427 rc = sspi_SetAuthIdentityWithLengthW(identity, unicodeUser, unicodeUserLenW, unicodeDomain,
428 unicodeDomainLenW, unicodePassword, unicodePasswordLenW);
430 winpr_znfree(unicodeUser, unicodeUserLenW *
sizeof(WCHAR));
431 winpr_znfree(unicodeDomain, unicodeDomainLenW *
sizeof(WCHAR));
432 winpr_znfree(unicodePassword, unicodePasswordLenW *
sizeof(WCHAR));
436UINT32 sspi_GetAuthIdentityVersion(
const void* identity)
443 version = *((
const UINT32*)identity);
445 if ((version == SEC_WINNT_AUTH_IDENTITY_VERSION) ||
446 (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2))
454UINT32 sspi_GetAuthIdentityFlags(
const void* identity)
462 version = sspi_GetAuthIdentityVersion(identity);
464 if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
466 flags = ((
const SEC_WINNT_AUTH_IDENTITY_EX*)identity)->Flags;
468 else if (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2)
474 flags = ((
const SEC_WINNT_AUTH_IDENTITY*)identity)->Flags;
480BOOL sspi_GetAuthIdentityUserDomainW(
const void* identity,
const WCHAR** pUser, UINT32* pUserLength,
481 const WCHAR** pDomain, UINT32* pDomainLength)
488 version = sspi_GetAuthIdentityVersion(identity);
490 if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
493 *pUser = (
const WCHAR*)id->User;
494 *pUserLength =
id->UserLength;
495 *pDomain = (
const WCHAR*)id->Domain;
496 *pDomainLength =
id->DomainLength;
498 else if (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2)
501 UINT32 UserOffset =
id->UserOffset;
502 UINT32 DomainOffset =
id->DomainOffset;
503 *pUser = WINPR_PACKED_ALIGN_CAST(
const WCHAR*, &((
const uint8_t*)identity)[UserOffset]);
504 *pUserLength =
id->UserLength / 2;
505 *pDomain = WINPR_PACKED_ALIGN_CAST(
const WCHAR*, &((
const uint8_t*)identity)[DomainOffset]);
506 *pDomainLength =
id->DomainLength / 2;
511 *pUser = (
const WCHAR*)id->User;
512 *pUserLength =
id->UserLength;
513 *pDomain = (
const WCHAR*)id->Domain;
514 *pDomainLength =
id->DomainLength;
520BOOL sspi_GetAuthIdentityUserDomainA(
const void* identity,
const char** pUser, UINT32* pUserLength,
521 const char** pDomain, UINT32* pDomainLength)
528 version = sspi_GetAuthIdentityVersion(identity);
530 if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
533 *pUser = (
const char*)id->User;
534 *pUserLength =
id->UserLength;
535 *pDomain = (
const char*)id->Domain;
536 *pDomainLength =
id->DomainLength;
538 else if (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2)
541 UINT32 UserOffset =
id->UserOffset;
542 UINT32 DomainOffset =
id->DomainOffset;
543 *pUser = (
const char*)&((
const uint8_t*)identity)[UserOffset];
544 *pUserLength =
id->UserLength;
545 *pDomain = (
const char*)&((
const uint8_t*)identity)[DomainOffset];
546 *pDomainLength =
id->DomainLength;
551 *pUser = (
const char*)id->User;
552 *pUserLength =
id->UserLength;
553 *pDomain = (
const char*)id->Domain;
554 *pDomainLength =
id->DomainLength;
560BOOL sspi_GetAuthIdentityPasswordW(
const void* identity,
const WCHAR** pPassword,
561 UINT32* pPasswordLength)
568 version = sspi_GetAuthIdentityVersion(identity);
570 if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
573 *pPassword = (
const WCHAR*)id->Password;
574 *pPasswordLength =
id->PasswordLength;
576 else if (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2)
583 *pPassword = (
const WCHAR*)id->Password;
584 *pPasswordLength =
id->PasswordLength;
590BOOL sspi_GetAuthIdentityPasswordA(
const void* identity,
const char** pPassword,
591 UINT32* pPasswordLength)
598 version = sspi_GetAuthIdentityVersion(identity);
600 if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
603 *pPassword = (
const char*)id->Password;
604 *pPasswordLength =
id->PasswordLength;
606 else if (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2)
613 *pPassword = (
const char*)id->Password;
614 *pPasswordLength =
id->PasswordLength;
621 char** pDomain,
char** pPassword)
623 BOOL success = FALSE;
624 const char* UserA =
nullptr;
625 const char* DomainA =
nullptr;
626 const char* PasswordA =
nullptr;
627 const WCHAR* UserW =
nullptr;
628 const WCHAR* DomainW =
nullptr;
629 const WCHAR* PasswordW =
nullptr;
630 UINT32 UserLength = 0;
631 UINT32 DomainLength = 0;
632 UINT32 PasswordLength = 0;
634 if (!identity || !pUser || !pDomain || !pPassword)
637 *pUser = *pDomain = *pPassword =
nullptr;
639 UINT32 identityFlags = sspi_GetAuthIdentityFlags(identity);
641 if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_ANSI) != 0)
643 if (!sspi_GetAuthIdentityUserDomainA(identity, &UserA, &UserLength, &DomainA,
647 if (!sspi_GetAuthIdentityPasswordA(identity, &PasswordA, &PasswordLength))
650 if (UserA && UserLength)
652 *pUser = _strdup(UserA);
658 if (DomainA && DomainLength)
660 *pDomain = _strdup(DomainA);
666 if (PasswordA && PasswordLength)
668 *pPassword = _strdup(PasswordA);
676 else if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_UNICODE) != 0)
678 if (!sspi_GetAuthIdentityUserDomainW(identity, &UserW, &UserLength, &DomainW,
682 if (!sspi_GetAuthIdentityPasswordW(identity, &PasswordW, &PasswordLength))
685 if (UserW && (UserLength > 0))
687 *pUser = ConvertWCharNToUtf8Alloc(UserW, UserLength,
nullptr);
692 if (DomainW && (DomainLength > 0))
694 *pDomain = ConvertWCharNToUtf8Alloc(DomainW, DomainLength,
nullptr);
699 if (PasswordW && (PasswordLength > 0))
701 *pPassword = ConvertWCharNToUtf8Alloc(PasswordW, PasswordLength,
nullptr);
714 WCHAR** pDomain, WCHAR** pPassword)
716 BOOL success = FALSE;
717 const char* UserA =
nullptr;
718 const char* DomainA =
nullptr;
719 const char* PasswordA =
nullptr;
720 const WCHAR* UserW =
nullptr;
721 const WCHAR* DomainW =
nullptr;
722 const WCHAR* PasswordW =
nullptr;
723 UINT32 UserLength = 0;
724 UINT32 DomainLength = 0;
725 UINT32 PasswordLength = 0;
727 if (!identity || !pUser || !pDomain || !pPassword)
730 *pUser = *pDomain = *pPassword =
nullptr;
732 UINT32 identityFlags = sspi_GetAuthIdentityFlags(identity);
734 if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_ANSI) != 0)
736 if (!sspi_GetAuthIdentityUserDomainA(identity, &UserA, &UserLength, &DomainA,
740 if (!sspi_GetAuthIdentityPasswordA(identity, &PasswordA, &PasswordLength))
743 if (UserA && (UserLength > 0))
745 WCHAR* ptr = ConvertUtf8NToWCharAlloc(UserA, UserLength,
nullptr);
752 if (DomainA && (DomainLength > 0))
754 WCHAR* ptr = ConvertUtf8NToWCharAlloc(DomainA, DomainLength,
nullptr);
760 if (PasswordA && (PasswordLength > 0))
762 WCHAR* ptr = ConvertUtf8NToWCharAlloc(PasswordA, PasswordLength,
nullptr);
771 else if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_UNICODE) != 0)
773 if (!sspi_GetAuthIdentityUserDomainW(identity, &UserW, &UserLength, &DomainW,
777 if (!sspi_GetAuthIdentityPasswordW(identity, &PasswordW, &PasswordLength))
780 if (UserW && UserLength)
782 *pUser = winpr_wcsndup(UserW, UserLength /
sizeof(WCHAR));
788 if (DomainW && DomainLength)
790 *pDomain = winpr_wcsndup(DomainW, DomainLength /
sizeof(WCHAR));
796 if (PasswordW && PasswordLength)
798 *pPassword = winpr_wcsndup(PasswordW, PasswordLength /
sizeof(WCHAR));
814 UINT32 identityFlags = 0;
815 char* PackageList =
nullptr;
816 const char* PackageListA =
nullptr;
817 const WCHAR* PackageListW =
nullptr;
818 UINT32 PackageListLength = 0;
819 UINT32 PackageListOffset = 0;
820 const void* pAuthData = (
const void*)identity;
825 version = sspi_GetAuthIdentityVersion(pAuthData);
826 identityFlags = sspi_GetAuthIdentityFlags(pAuthData);
828 if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_ANSI) != 0)
830 if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
833 PackageListA = (
const char*)ad->PackageList;
834 PackageListLength = ad->PackageListLength;
837 if (PackageListA && PackageListLength)
839 PackageList = _strdup(PackageListA);
842 else if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_UNICODE) != 0)
844 if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
847 PackageListW = (
const WCHAR*)ad->PackageList;
848 PackageListLength = ad->PackageListLength;
850 else if (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2)
853 PackageListOffset = ad->PackageListOffset;
854 PackageListW = WINPR_PACKED_ALIGN_CAST(
const WCHAR*,
855 &((
const uint8_t*)pAuthData)[PackageListOffset]);
856 PackageListLength = ad->PackageListLength / 2;
859 if (PackageListW && (PackageListLength > 0))
860 PackageList = ConvertWCharNToUtf8Alloc(PackageListW, PackageListLength,
nullptr);
865 *pPackageList = PackageList;
872int sspi_CopyAuthIdentity(SEC_WINNT_AUTH_IDENTITY* identity,
876 UINT32 identityFlags = 0;
877 const char* UserA =
nullptr;
878 const char* DomainA =
nullptr;
879 const char* PasswordA =
nullptr;
880 const WCHAR* UserW =
nullptr;
881 const WCHAR* DomainW =
nullptr;
882 const WCHAR* PasswordW =
nullptr;
883 UINT32 UserLength = 0;
884 UINT32 DomainLength = 0;
885 UINT32 PasswordLength = 0;
887 sspi_FreeAuthIdentity(identity);
889 identityFlags = sspi_GetAuthIdentityFlags(srcIdentity);
891 identity->Flags = identityFlags;
893 if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_ANSI) != 0)
895 if (!sspi_GetAuthIdentityUserDomainA(srcIdentity, &UserA, &UserLength, &DomainA,
901 if (!sspi_GetAuthIdentityPasswordA(srcIdentity, &PasswordA, &PasswordLength))
906 status = sspi_SetAuthIdentity(identity, UserA, DomainA, PasswordA);
911 identity->Flags &= (uint32_t)~SEC_WINNT_AUTH_IDENTITY_ANSI;
912 identity->Flags |= SEC_WINNT_AUTH_IDENTITY_UNICODE;
916 identity->Flags |= SEC_WINNT_AUTH_IDENTITY_UNICODE;
918 if (!sspi_GetAuthIdentityUserDomainW(srcIdentity, &UserW, &UserLength, &DomainW, &DomainLength))
923 if (!sspi_GetAuthIdentityPasswordW(srcIdentity, &PasswordW, &PasswordLength))
929 identity->UserLength = UserLength;
931 if (identity->UserLength > 0)
933 identity->User = (UINT16*)calloc((identity->UserLength + 1),
sizeof(WCHAR));
938 CopyMemory(identity->User, UserW, identity->UserLength *
sizeof(WCHAR));
939 identity->User[identity->UserLength] = 0;
942 identity->DomainLength = DomainLength;
944 if (identity->DomainLength > 0)
946 identity->Domain = (UINT16*)calloc((identity->DomainLength + 1),
sizeof(WCHAR));
948 if (!identity->Domain)
951 CopyMemory(identity->Domain, DomainW, identity->DomainLength *
sizeof(WCHAR));
952 identity->Domain[identity->DomainLength] = 0;
955 identity->PasswordLength = PasswordLength;
959 identity->Password = (UINT16*)calloc((identity->PasswordLength + 1),
sizeof(WCHAR));
961 if (!identity->Password)
964 CopyMemory(identity->Password, PasswordW, identity->PasswordLength *
sizeof(WCHAR));
965 identity->Password[identity->PasswordLength] = 0;
972PSecBuffer sspi_FindSecBuffer(PSecBufferDesc pMessage, ULONG BufferType)
974 PSecBuffer pSecBuffer =
nullptr;
976 for (UINT32 index = 0; index < pMessage->cBuffers; index++)
978 if (pMessage->pBuffers[index].BufferType == BufferType)
980 pSecBuffer = &pMessage->pBuffers[index];
988static BOOL WINPR_init(
void)
991 for (
size_t x = 0; x < ARRAYSIZE(SecurityFunctionTableA_NAME_LIST); x++)
993 const SecurityFunctionTableA_NAME* cur = &SecurityFunctionTableA_NAME_LIST[x];
994 InitializeConstWCharFromUtf8(cur->Name, BUFFER_NAME_LIST_W[x],
995 ARRAYSIZE(BUFFER_NAME_LIST_W[x]));
1000static BOOL CALLBACK sspi_init(WINPR_ATTR_UNUSED PINIT_ONCE InitOnce,
1001 WINPR_ATTR_UNUSED PVOID Parameter, WINPR_ATTR_UNUSED PVOID* Context)
1003 if (!winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT))
1005 if (sspi_ContextBufferAllocTableNew() < 0)
1007 if (!SCHANNEL_init())
1009 if (!KERBEROS_init())
1013 if (!CREDSSP_init())
1015 if (!NEGOTIATE_init())
1019 return WINPR_init();
1022void sspi_GlobalInit(
void)
1024 static INIT_ONCE once = INIT_ONCE_STATIC_INIT;
1031 WINPR_STATIC_ASSERT(ARRAYSIZE(SecPkgInfoA_LIST) == SSPI_PACKAGE_COUNT - 1);
1032 WINPR_STATIC_ASSERT(ARRAYSIZE(SecPkgInfoW_LIST) == SSPI_PACKAGE_COUNT - 1);
1033 WINPR_STATIC_ASSERT(ARRAYSIZE(SecurityFunctionTableA_NAME_LIST) == SSPI_PACKAGE_COUNT - 1);
1034 WINPR_STATIC_ASSERT(ARRAYSIZE(SecurityFunctionTableW_NAME_LIST) == SSPI_PACKAGE_COUNT - 1);
1035 WINPR_STATIC_ASSERT(ARRAYSIZE(BUFFER_NAME_LIST_W) == SSPI_PACKAGE_COUNT - 1);
1037 if (!InitOnceExecuteOnce(&once, sspi_init, &flags,
nullptr))
1038 WLog_ERR(TAG,
"InitOnceExecuteOnce failed");
1041void sspi_GlobalFinish(
void)
1043 sspi_ContextBufferAllocTableFree();
1048 size_t cPackages = ARRAYSIZE(SecPkgInfoA_LIST);
1050 for (
size_t index = 0; index < cPackages; index++)
1052 if (strcmp(Name, SecurityFunctionTableA_NAME_LIST[index].Name) == 0)
1054 return SecurityFunctionTableA_NAME_LIST[index].SecurityFunctionTable;
1063 size_t cPackages = ARRAYSIZE(SecPkgInfoW_LIST);
1065 for (
size_t index = 0; index < cPackages; index++)
1067 if (_wcscmp(Name, SecurityFunctionTableW_NAME_LIST[index].Name) == 0)
1069 return SecurityFunctionTableW_NAME_LIST[index].SecurityFunctionTable;
1080sspi_GetSecurityFunctionTableAByHandle(
SecHandle* handle)
1082 const SSPI_PACKAGE_ID
id = sspi_SecureHandleGetPackageId(handle);
1084 if ((
id < SSPI_PACKAGE_NTLM) || (
id > ARRAYSIZE(SecurityFunctionTableA_NAME_LIST)))
1087 return SecurityFunctionTableA_NAME_LIST[
id - 1].SecurityFunctionTable;
1091sspi_GetSecurityFunctionTableWByHandle(
SecHandle* handle)
1093 const SSPI_PACKAGE_ID
id = sspi_SecureHandleGetPackageId(handle);
1095 if ((
id < SSPI_PACKAGE_NTLM) || (
id > ARRAYSIZE(SecurityFunctionTableW_NAME_LIST)))
1098 return SecurityFunctionTableW_NAME_LIST[
id - 1].SecurityFunctionTable;
1101static void FreeContextBuffer_EnumerateSecurityPackages(
void* contextBuffer);
1102static void FreeContextBuffer_QuerySecurityPackageInfo(
void* contextBuffer);
1104void sspi_ContextBufferFree(
void* contextBuffer)
1106 UINT32 allocatorIndex = 0;
1108 EnterCriticalSection(&ContextBufferAllocTableLock);
1110 for (
size_t index = 0; index < ContextBufferAllocTable.cMaxEntries; index++)
1112 if (contextBuffer == ContextBufferAllocTable.entries[index].contextBuffer)
1114 contextBuffer = ContextBufferAllocTable.entries[index].contextBuffer;
1115 allocatorIndex = ContextBufferAllocTable.entries[index].allocatorIndex;
1116 ContextBufferAllocTable.cEntries--;
1117 ContextBufferAllocTable.entries[index].allocatorIndex = 0;
1118 ContextBufferAllocTable.entries[index].contextBuffer =
nullptr;
1120 switch (allocatorIndex)
1122 case EnumerateSecurityPackagesIndex:
1123 FreeContextBuffer_EnumerateSecurityPackages(contextBuffer);
1126 case QuerySecurityPackageInfoIndex:
1127 FreeContextBuffer_QuerySecurityPackageInfo(contextBuffer);
1135 LeaveCriticalSection(&ContextBufferAllocTableLock);
1144static SECURITY_STATUS SEC_ENTRY winpr_EnumerateSecurityPackagesW(ULONG* pcPackages,
1145 PSecPkgInfoW* ppPackageInfo)
1147 const size_t cPackages = ARRAYSIZE(SecPkgInfoW_LIST);
1148 const size_t size =
sizeof(
SecPkgInfoW) * cPackages;
1150 (
SecPkgInfoW*)sspi_ContextBufferAlloc(EnumerateSecurityPackagesIndex, size);
1152 WINPR_ASSERT(cPackages <= UINT32_MAX);
1155 return SEC_E_INSUFFICIENT_MEMORY;
1157 for (
size_t index = 0; index < cPackages; index++)
1159 pPackageInfo[index].fCapabilities = SecPkgInfoW_LIST[index]->fCapabilities;
1160 pPackageInfo[index].wVersion = SecPkgInfoW_LIST[index]->wVersion;
1161 pPackageInfo[index].wRPCID = SecPkgInfoW_LIST[index]->wRPCID;
1162 pPackageInfo[index].cbMaxToken = SecPkgInfoW_LIST[index]->cbMaxToken;
1163 pPackageInfo[index].Name = _wcsdup(SecPkgInfoW_LIST[index]->Name);
1164 pPackageInfo[index].Comment = _wcsdup(SecPkgInfoW_LIST[index]->Comment);
1167 *(pcPackages) = (UINT32)cPackages;
1168 *(ppPackageInfo) = pPackageInfo;
1172static SECURITY_STATUS SEC_ENTRY winpr_EnumerateSecurityPackagesA(ULONG* pcPackages,
1173 PSecPkgInfoA* ppPackageInfo)
1175 const size_t cPackages = ARRAYSIZE(SecPkgInfoA_LIST);
1176 const size_t size =
sizeof(
SecPkgInfoA) * cPackages;
1178 (
SecPkgInfoA*)sspi_ContextBufferAlloc(EnumerateSecurityPackagesIndex, size);
1180 WINPR_ASSERT(cPackages <= UINT32_MAX);
1183 return SEC_E_INSUFFICIENT_MEMORY;
1185 for (
size_t index = 0; index < cPackages; index++)
1187 pPackageInfo[index].fCapabilities = SecPkgInfoA_LIST[index]->fCapabilities;
1188 pPackageInfo[index].wVersion = SecPkgInfoA_LIST[index]->wVersion;
1189 pPackageInfo[index].wRPCID = SecPkgInfoA_LIST[index]->wRPCID;
1190 pPackageInfo[index].cbMaxToken = SecPkgInfoA_LIST[index]->cbMaxToken;
1191 pPackageInfo[index].Name = _strdup(SecPkgInfoA_LIST[index]->Name);
1192 pPackageInfo[index].Comment = _strdup(SecPkgInfoA_LIST[index]->Comment);
1194 if (!pPackageInfo[index].Name || !pPackageInfo[index].Comment)
1196 sspi_ContextBufferFree(pPackageInfo);
1197 return SEC_E_INSUFFICIENT_MEMORY;
1201 *(pcPackages) = (UINT32)cPackages;
1202 *(ppPackageInfo) = pPackageInfo;
1206static void FreeContextBuffer_EnumerateSecurityPackages(
void* contextBuffer)
1209 size_t cPackages = ARRAYSIZE(SecPkgInfoA_LIST);
1214 for (
size_t index = 0; index < cPackages; index++)
1216 free(pPackageInfo[index].Name);
1217 free(pPackageInfo[index].Comment);
1223static SECURITY_STATUS SEC_ENTRY winpr_QuerySecurityPackageInfoW(SEC_WCHAR* pszPackageName,
1224 PSecPkgInfoW* ppPackageInfo)
1226 size_t cPackages = ARRAYSIZE(SecPkgInfoW_LIST);
1228 for (
size_t index = 0; index < cPackages; index++)
1230 if (_wcscmp(pszPackageName, SecPkgInfoW_LIST[index]->Name) == 0)
1234 (
SecPkgInfoW*)sspi_ContextBufferAlloc(QuerySecurityPackageInfoIndex, size);
1237 return SEC_E_INSUFFICIENT_MEMORY;
1239 pPackageInfo->fCapabilities = SecPkgInfoW_LIST[index]->fCapabilities;
1240 pPackageInfo->wVersion = SecPkgInfoW_LIST[index]->wVersion;
1241 pPackageInfo->wRPCID = SecPkgInfoW_LIST[index]->wRPCID;
1242 pPackageInfo->cbMaxToken = SecPkgInfoW_LIST[index]->cbMaxToken;
1243 pPackageInfo->Name = _wcsdup(SecPkgInfoW_LIST[index]->Name);
1244 pPackageInfo->Comment = _wcsdup(SecPkgInfoW_LIST[index]->Comment);
1245 *(ppPackageInfo) = pPackageInfo;
1250 *(ppPackageInfo) =
nullptr;
1251 return SEC_E_SECPKG_NOT_FOUND;
1254static SECURITY_STATUS SEC_ENTRY winpr_QuerySecurityPackageInfoA(SEC_CHAR* pszPackageName,
1255 PSecPkgInfoA* ppPackageInfo)
1257 size_t cPackages = ARRAYSIZE(SecPkgInfoA_LIST);
1259 for (
size_t index = 0; index < cPackages; index++)
1261 if (strcmp(pszPackageName, SecPkgInfoA_LIST[index]->Name) == 0)
1265 (
SecPkgInfoA*)sspi_ContextBufferAlloc(QuerySecurityPackageInfoIndex, size);
1268 return SEC_E_INSUFFICIENT_MEMORY;
1270 pPackageInfo->fCapabilities = SecPkgInfoA_LIST[index]->fCapabilities;
1271 pPackageInfo->wVersion = SecPkgInfoA_LIST[index]->wVersion;
1272 pPackageInfo->wRPCID = SecPkgInfoA_LIST[index]->wRPCID;
1273 pPackageInfo->cbMaxToken = SecPkgInfoA_LIST[index]->cbMaxToken;
1274 pPackageInfo->Name = _strdup(SecPkgInfoA_LIST[index]->Name);
1275 pPackageInfo->Comment = _strdup(SecPkgInfoA_LIST[index]->Comment);
1277 if (!pPackageInfo->Name || !pPackageInfo->Comment)
1279 sspi_ContextBufferFree(pPackageInfo);
1280 return SEC_E_INSUFFICIENT_MEMORY;
1283 *(ppPackageInfo) = pPackageInfo;
1288 *(ppPackageInfo) =
nullptr;
1289 return SEC_E_SECPKG_NOT_FOUND;
1292void FreeContextBuffer_QuerySecurityPackageInfo(
void* contextBuffer)
1294 SecPkgInfo* pPackageInfo = (SecPkgInfo*)contextBuffer;
1299 free(pPackageInfo->Name);
1300 free(pPackageInfo->Comment);
1304#define log_status(what, status) log_status_((what), (status), __FILE__, __func__, __LINE__)
1305static SECURITY_STATUS log_status_(
const char* what, SECURITY_STATUS status,
const char* file,
1306 const char* fkt,
size_t line)
1308 if (IsSecurityStatusError(status))
1310 const DWORD level = WLOG_WARN;
1311 static wLog* log =
nullptr;
1313 log = WLog_Get(TAG);
1315 if (WLog_IsLevelActive(log, level))
1317 WLog_PrintTextMessage(log, level, line, file, fkt,
"%s status %s [0x%08" PRIx32
"]",
1318 what, GetSecurityStatusString(status),
1319 WINPR_CXX_COMPAT_CAST(uint32_t, status));
1327static SECURITY_STATUS SEC_ENTRY winpr_AcquireCredentialsHandleW(
1328 SEC_WCHAR* pszPrincipal, SEC_WCHAR* pszPackage, ULONG fCredentialUse,
void* pvLogonID,
1329 void* pAuthData, SEC_GET_KEY_FN pGetKeyFn,
void* pvGetKeyArgument, PCredHandle phCredential,
1330 PTimeStamp ptsExpiry)
1335 return SEC_E_SECPKG_NOT_FOUND;
1337 if (!table->AcquireCredentialsHandleW)
1339 WLog_WARN(TAG,
"Security module does not provide an implementation");
1340 return SEC_E_UNSUPPORTED_FUNCTION;
1343 SECURITY_STATUS status = table->AcquireCredentialsHandleW(
1344 pszPrincipal, pszPackage, fCredentialUse, pvLogonID, pAuthData, pGetKeyFn, pvGetKeyArgument,
1345 phCredential, ptsExpiry);
1346 return log_status(
"AcquireCredentialsHandleW", status);
1349static SECURITY_STATUS SEC_ENTRY winpr_AcquireCredentialsHandleA(
1350 SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage, ULONG fCredentialUse,
void* pvLogonID,
1351 void* pAuthData, SEC_GET_KEY_FN pGetKeyFn,
void* pvGetKeyArgument, PCredHandle phCredential,
1352 PTimeStamp ptsExpiry)
1357 return SEC_E_SECPKG_NOT_FOUND;
1359 if (!table->AcquireCredentialsHandleA)
1361 WLog_WARN(TAG,
"Security module does not provide an implementation");
1362 return SEC_E_UNSUPPORTED_FUNCTION;
1365 SECURITY_STATUS status = table->AcquireCredentialsHandleA(
1366 pszPrincipal, pszPackage, fCredentialUse, pvLogonID, pAuthData, pGetKeyFn, pvGetKeyArgument,
1367 phCredential, ptsExpiry);
1368 return log_status(
"AcquireCredentialsHandleA", status);
1371static SECURITY_STATUS SEC_ENTRY winpr_ExportSecurityContext(PCtxtHandle phContext, ULONG fFlags,
1372 PSecBuffer pPackedContext,
1378 return SEC_E_SECPKG_NOT_FOUND;
1380 if (!table->ExportSecurityContext)
1382 WLog_WARN(TAG,
"Security module does not provide an implementation");
1383 return SEC_E_UNSUPPORTED_FUNCTION;
1386 SECURITY_STATUS status =
1387 table->ExportSecurityContext(phContext, fFlags, pPackedContext, pToken);
1388 return log_status(
"ExportSecurityContext", status);
1391static SECURITY_STATUS SEC_ENTRY winpr_FreeCredentialsHandle(PCredHandle phCredential)
1396 return SEC_E_SECPKG_NOT_FOUND;
1398 if (!table->FreeCredentialsHandle)
1400 WLog_WARN(TAG,
"Security module does not provide an implementation");
1401 return SEC_E_UNSUPPORTED_FUNCTION;
1404 SECURITY_STATUS status = table->FreeCredentialsHandle(phCredential);
1405 return log_status(
"FreeCredentialsHandle", status);
1408static SECURITY_STATUS SEC_ENTRY winpr_ImportSecurityContextW(SEC_WCHAR* pszPackage,
1409 PSecBuffer pPackedContext,
1410 HANDLE pToken, PCtxtHandle phContext)
1415 return SEC_E_SECPKG_NOT_FOUND;
1417 if (!table->ImportSecurityContextW)
1419 WLog_WARN(TAG,
"Security module does not provide an implementation");
1420 return SEC_E_UNSUPPORTED_FUNCTION;
1423 SECURITY_STATUS status =
1424 table->ImportSecurityContextW(pszPackage, pPackedContext, pToken, phContext);
1425 return log_status(
"ImportSecurityContextW", status);
1428static SECURITY_STATUS SEC_ENTRY winpr_ImportSecurityContextA(SEC_CHAR* pszPackage,
1429 PSecBuffer pPackedContext,
1430 HANDLE pToken, PCtxtHandle phContext)
1435 return SEC_E_SECPKG_NOT_FOUND;
1437 if (!table->ImportSecurityContextA)
1439 WLog_WARN(TAG,
"Security module does not provide an implementation");
1440 return SEC_E_UNSUPPORTED_FUNCTION;
1443 SECURITY_STATUS status =
1444 table->ImportSecurityContextA(pszPackage, pPackedContext, pToken, phContext);
1445 return log_status(
"ImportSecurityContextA", status);
1448static SECURITY_STATUS SEC_ENTRY winpr_QueryCredentialsAttributesW(PCredHandle phCredential,
1449 ULONG ulAttribute,
void* pBuffer)
1454 return SEC_E_SECPKG_NOT_FOUND;
1456 if (!table->QueryCredentialsAttributesW)
1458 WLog_WARN(TAG,
"Security module does not provide an implementation");
1459 return SEC_E_UNSUPPORTED_FUNCTION;
1462 SECURITY_STATUS status = table->QueryCredentialsAttributesW(phCredential, ulAttribute, pBuffer);
1463 return log_status(
"QueryCredentialsAttributesW", status);
1466static SECURITY_STATUS SEC_ENTRY winpr_QueryCredentialsAttributesA(PCredHandle phCredential,
1467 ULONG ulAttribute,
void* pBuffer)
1472 return SEC_E_SECPKG_NOT_FOUND;
1474 if (!table->QueryCredentialsAttributesA)
1476 WLog_WARN(TAG,
"Security module does not provide an implementation");
1477 return SEC_E_UNSUPPORTED_FUNCTION;
1480 SECURITY_STATUS status = table->QueryCredentialsAttributesA(phCredential, ulAttribute, pBuffer);
1481 return log_status(
"QueryCredentialsAttributesA", status);
1484static SECURITY_STATUS SEC_ENTRY winpr_SetCredentialsAttributesW(PCredHandle phCredential,
1485 ULONG ulAttribute,
void* pBuffer,
1491 return SEC_E_SECPKG_NOT_FOUND;
1493 if (!table->SetCredentialsAttributesW)
1495 WLog_WARN(TAG,
"Security module does not provide an implementation");
1496 return SEC_E_UNSUPPORTED_FUNCTION;
1499 SECURITY_STATUS status =
1500 table->SetCredentialsAttributesW(phCredential, ulAttribute, pBuffer, cbBuffer);
1501 return log_status(
"SetCredentialsAttributesW", status);
1504static SECURITY_STATUS SEC_ENTRY winpr_SetCredentialsAttributesA(PCredHandle phCredential,
1505 ULONG ulAttribute,
void* pBuffer,
1511 return SEC_E_SECPKG_NOT_FOUND;
1513 if (!table->SetCredentialsAttributesA)
1515 WLog_WARN(TAG,
"Security module does not provide an implementation");
1516 return SEC_E_UNSUPPORTED_FUNCTION;
1519 SECURITY_STATUS status =
1520 table->SetCredentialsAttributesA(phCredential, ulAttribute, pBuffer, cbBuffer);
1521 return log_status(
"SetCredentialsAttributesA", status);
1526static SECURITY_STATUS SEC_ENTRY
1527winpr_AcceptSecurityContext(PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
1528 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
1529 PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsTimeStamp)
1534 return SEC_E_SECPKG_NOT_FOUND;
1536 if (!table->AcceptSecurityContext)
1538 WLog_WARN(TAG,
"Security module does not provide an implementation");
1539 return SEC_E_UNSUPPORTED_FUNCTION;
1542 SECURITY_STATUS status =
1543 table->AcceptSecurityContext(phCredential, phContext, pInput, fContextReq, TargetDataRep,
1544 phNewContext, pOutput, pfContextAttr, ptsTimeStamp);
1545 return log_status(
"AcceptSecurityContext", status);
1548static SECURITY_STATUS SEC_ENTRY winpr_ApplyControlToken(PCtxtHandle phContext,
1549 PSecBufferDesc pInput)
1554 return SEC_E_SECPKG_NOT_FOUND;
1556 if (!table->ApplyControlToken)
1558 WLog_WARN(TAG,
"Security module does not provide an implementation");
1559 return SEC_E_UNSUPPORTED_FUNCTION;
1562 SECURITY_STATUS status = table->ApplyControlToken(phContext, pInput);
1563 return log_status(
"ApplyControlToken", status);
1566static SECURITY_STATUS SEC_ENTRY winpr_CompleteAuthToken(PCtxtHandle phContext,
1567 PSecBufferDesc pToken)
1572 return SEC_E_SECPKG_NOT_FOUND;
1574 if (!table->CompleteAuthToken)
1576 WLog_WARN(TAG,
"Security module does not provide an implementation");
1577 return SEC_E_UNSUPPORTED_FUNCTION;
1580 SECURITY_STATUS status = table->CompleteAuthToken(phContext, pToken);
1581 return log_status(
"CompleteAuthToken", status);
1584static SECURITY_STATUS SEC_ENTRY winpr_DeleteSecurityContext(PCtxtHandle phContext)
1589 return SEC_E_SECPKG_NOT_FOUND;
1591 if (!table->DeleteSecurityContext)
1593 WLog_WARN(TAG,
"Security module does not provide an implementation");
1594 return SEC_E_UNSUPPORTED_FUNCTION;
1597 const SECURITY_STATUS status = table->DeleteSecurityContext(phContext);
1598 return log_status(
"DeleteSecurityContext", status);
1601static SECURITY_STATUS SEC_ENTRY winpr_FreeContextBuffer(
void* pvContextBuffer)
1603 if (!pvContextBuffer)
1604 return SEC_E_INVALID_HANDLE;
1606 sspi_ContextBufferFree(pvContextBuffer);
1610static SECURITY_STATUS SEC_ENTRY winpr_ImpersonateSecurityContext(PCtxtHandle phContext)
1615 return SEC_E_SECPKG_NOT_FOUND;
1617 if (!table->ImpersonateSecurityContext)
1619 WLog_WARN(TAG,
"Security module does not provide an implementation");
1620 return SEC_E_UNSUPPORTED_FUNCTION;
1623 SECURITY_STATUS status = table->ImpersonateSecurityContext(phContext);
1624 return log_status(
"ImpersonateSecurityContext", status);
1627static SECURITY_STATUS SEC_ENTRY winpr_InitializeSecurityContextW(
1628 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq,
1629 ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
1630 PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
1635 return SEC_E_SECPKG_NOT_FOUND;
1637 if (!table->InitializeSecurityContextW)
1639 WLog_WARN(TAG,
"Security module does not provide an implementation");
1640 return SEC_E_UNSUPPORTED_FUNCTION;
1643 const SECURITY_STATUS status = table->InitializeSecurityContextW(
1644 phCredential, phContext, pszTargetName, fContextReq, Reserved1, TargetDataRep, pInput,
1645 Reserved2, phNewContext, pOutput, pfContextAttr, ptsExpiry);
1646 return log_status(
"InitializeSecurityContextW", status);
1649static SECURITY_STATUS SEC_ENTRY winpr_InitializeSecurityContextA(
1650 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR* pszTargetName, ULONG fContextReq,
1651 ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
1652 PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
1657 return SEC_E_SECPKG_NOT_FOUND;
1659 if (!table->InitializeSecurityContextA)
1661 WLog_WARN(TAG,
"Security module does not provide an implementation");
1662 return SEC_E_UNSUPPORTED_FUNCTION;
1665 SECURITY_STATUS status = table->InitializeSecurityContextA(
1666 phCredential, phContext, pszTargetName, fContextReq, Reserved1, TargetDataRep, pInput,
1667 Reserved2, phNewContext, pOutput, pfContextAttr, ptsExpiry);
1669 return log_status(
"InitializeSecurityContextA", status);
1672static SECURITY_STATUS SEC_ENTRY winpr_QueryContextAttributesW(PCtxtHandle phContext,
1673 ULONG ulAttribute,
void* pBuffer)
1678 return SEC_E_SECPKG_NOT_FOUND;
1680 if (!table->QueryContextAttributesW)
1682 WLog_WARN(TAG,
"Security module does not provide an implementation");
1683 return SEC_E_UNSUPPORTED_FUNCTION;
1686 SECURITY_STATUS status = table->QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1687 return log_status(
"QueryContextAttributesW", status);
1690static SECURITY_STATUS SEC_ENTRY winpr_QueryContextAttributesA(PCtxtHandle phContext,
1691 ULONG ulAttribute,
void* pBuffer)
1696 return SEC_E_SECPKG_NOT_FOUND;
1698 if (!table->QueryContextAttributesA)
1700 WLog_WARN(TAG,
"Security module does not provide an implementation");
1701 return SEC_E_UNSUPPORTED_FUNCTION;
1704 SECURITY_STATUS status = table->QueryContextAttributesA(phContext, ulAttribute, pBuffer);
1705 return log_status(
"QueryContextAttributesA", status);
1708static SECURITY_STATUS SEC_ENTRY winpr_QuerySecurityContextToken(PCtxtHandle phContext,
1714 return SEC_E_SECPKG_NOT_FOUND;
1716 if (!table->QuerySecurityContextToken)
1718 WLog_WARN(TAG,
"Security module does not provide an implementation");
1719 return SEC_E_UNSUPPORTED_FUNCTION;
1722 SECURITY_STATUS status = table->QuerySecurityContextToken(phContext, phToken);
1723 return log_status(
"QuerySecurityContextToken", status);
1726static SECURITY_STATUS SEC_ENTRY winpr_SetContextAttributesW(PCtxtHandle phContext,
1727 ULONG ulAttribute,
void* pBuffer,
1733 return SEC_E_SECPKG_NOT_FOUND;
1735 if (!table->SetContextAttributesW)
1737 WLog_WARN(TAG,
"Security module does not provide an implementation");
1738 return SEC_E_UNSUPPORTED_FUNCTION;
1741 SECURITY_STATUS status =
1742 table->SetContextAttributesW(phContext, ulAttribute, pBuffer, cbBuffer);
1743 return log_status(
"SetContextAttributesW", status);
1746static SECURITY_STATUS SEC_ENTRY winpr_SetContextAttributesA(PCtxtHandle phContext,
1747 ULONG ulAttribute,
void* pBuffer,
1753 return SEC_E_SECPKG_NOT_FOUND;
1755 if (!table->SetContextAttributesA)
1757 WLog_WARN(TAG,
"Security module does not provide an implementation");
1758 return SEC_E_UNSUPPORTED_FUNCTION;
1761 SECURITY_STATUS status =
1762 table->SetContextAttributesA(phContext, ulAttribute, pBuffer, cbBuffer);
1763 return log_status(
"SetContextAttributesA", status);
1766static SECURITY_STATUS SEC_ENTRY winpr_RevertSecurityContext(PCtxtHandle phContext)
1771 return SEC_E_SECPKG_NOT_FOUND;
1773 if (!table->RevertSecurityContext)
1775 WLog_WARN(TAG,
"Security module does not provide an implementation");
1776 return SEC_E_UNSUPPORTED_FUNCTION;
1779 SECURITY_STATUS status = table->RevertSecurityContext(phContext);
1781 return log_status(
"RevertSecurityContext", status);
1786static SECURITY_STATUS SEC_ENTRY winpr_DecryptMessage(PCtxtHandle phContext,
1787 PSecBufferDesc pMessage, ULONG MessageSeqNo,
1793 return SEC_E_SECPKG_NOT_FOUND;
1795 if (!table->DecryptMessage)
1797 WLog_WARN(TAG,
"Security module does not provide an implementation");
1798 return SEC_E_UNSUPPORTED_FUNCTION;
1801 const SECURITY_STATUS status = table->DecryptMessage(phContext, pMessage, MessageSeqNo, pfQOP);
1803 return log_status(
"DecryptMessage", status);
1806static SECURITY_STATUS SEC_ENTRY winpr_EncryptMessage(PCtxtHandle phContext, ULONG fQOP,
1807 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1812 return SEC_E_SECPKG_NOT_FOUND;
1814 if (!table->EncryptMessage)
1816 WLog_WARN(TAG,
"Security module does not provide an implementation");
1817 return SEC_E_UNSUPPORTED_FUNCTION;
1820 const SECURITY_STATUS status = table->EncryptMessage(phContext, fQOP, pMessage, MessageSeqNo);
1821 return log_status(
"EncryptMessage", status);
1824static SECURITY_STATUS SEC_ENTRY winpr_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1825 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1830 return SEC_E_SECPKG_NOT_FOUND;
1832 if (!table->MakeSignature)
1834 WLog_WARN(TAG,
"Security module does not provide an implementation");
1835 return SEC_E_UNSUPPORTED_FUNCTION;
1838 const SECURITY_STATUS status = table->MakeSignature(phContext, fQOP, pMessage, MessageSeqNo);
1839 return log_status(
"MakeSignature", status);
1842static SECURITY_STATUS SEC_ENTRY winpr_VerifySignature(PCtxtHandle phContext,
1843 PSecBufferDesc pMessage, ULONG MessageSeqNo,
1849 return SEC_E_SECPKG_NOT_FOUND;
1851 if (!table->VerifySignature)
1853 WLog_WARN(TAG,
"Security module does not provide an implementation");
1854 return SEC_E_UNSUPPORTED_FUNCTION;
1857 SECURITY_STATUS status = table->VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1859 return log_status(
"VerifySignature", status);
1864 winpr_EnumerateSecurityPackagesA,
1865 winpr_QueryCredentialsAttributesA,
1866 winpr_AcquireCredentialsHandleA,
1867 winpr_FreeCredentialsHandle,
1869 winpr_InitializeSecurityContextA,
1870 winpr_AcceptSecurityContext,
1871 winpr_CompleteAuthToken,
1872 winpr_DeleteSecurityContext,
1873 winpr_ApplyControlToken,
1874 winpr_QueryContextAttributesA,
1875 winpr_ImpersonateSecurityContext,
1876 winpr_RevertSecurityContext,
1877 winpr_MakeSignature,
1878 winpr_VerifySignature,
1879 winpr_FreeContextBuffer,
1880 winpr_QuerySecurityPackageInfoA,
1883 winpr_ExportSecurityContext,
1884 winpr_ImportSecurityContextA,
1887 winpr_QuerySecurityContextToken,
1888 winpr_EncryptMessage,
1889 winpr_DecryptMessage,
1890 winpr_SetContextAttributesA,
1891 winpr_SetCredentialsAttributesA,
1896 winpr_EnumerateSecurityPackagesW,
1897 winpr_QueryCredentialsAttributesW,
1898 winpr_AcquireCredentialsHandleW,
1899 winpr_FreeCredentialsHandle,
1901 winpr_InitializeSecurityContextW,
1902 winpr_AcceptSecurityContext,
1903 winpr_CompleteAuthToken,
1904 winpr_DeleteSecurityContext,
1905 winpr_ApplyControlToken,
1906 winpr_QueryContextAttributesW,
1907 winpr_ImpersonateSecurityContext,
1908 winpr_RevertSecurityContext,
1909 winpr_MakeSignature,
1910 winpr_VerifySignature,
1911 winpr_FreeContextBuffer,
1912 winpr_QuerySecurityPackageInfoW,
1915 winpr_ExportSecurityContext,
1916 winpr_ImportSecurityContextW,
1919 winpr_QuerySecurityContextToken,
1920 winpr_EncryptMessage,
1921 winpr_DecryptMessage,
1922 winpr_SetContextAttributesW,
1923 winpr_SetCredentialsAttributesW,
1928 return &winpr_SecurityFunctionTableW;
1933 return &winpr_SecurityFunctionTableA;
1936SEC_WINPR_NTLM_SETTINGS_V2* sspi_CloneSecNtlmSettings(
const SEC_WINPR_NTLM_SETTINGS_V2* other)
1941 const size_t size =
sizeof(SEC_WINPR_NTLM_SETTINGS_V2);
1942 if (other->size < size)
1945 "Invalid SEC_WINPR_NTLM_SETTINGS_V2 parameter passed, must be of size >= "
1951 SEC_WINPR_NTLM_SETTINGS_V2* clone = sspi_AllocSecNtlmSettings();
1957 if (!sspi_CloneSecSettingsString(&clone->samFile, other->samFile))
1960 clone->hashCallback = other->hashCallback;
1961 clone->hashCallbackArg = other->hashCallbackArg;
1962 if (other->targetName)
1964 if (!sspi_CloneSecSettingsString(&clone->targetName, other->targetName))
1967 if (other->netBiosComputerName)
1969 if (!sspi_CloneSecSettingsString(&clone->netBiosComputerName, other->netBiosComputerName))
1972 if (other->netBiosDomainName)
1974 if (!sspi_CloneSecSettingsString(&clone->netBiosDomainName, other->netBiosDomainName))
1977 if (other->dnsComputerName)
1979 if (!sspi_CloneSecSettingsString(&clone->dnsComputerName, other->dnsComputerName))
1982 if (other->dnsDomainName)
1984 if (!sspi_CloneSecSettingsString(&clone->dnsDomainName, other->dnsDomainName))
1991 sspi_FreeSecNtlmSettings(clone);