10using namespace sspiutils;
12SspiNegotiateServer::SspiNegotiateServer(StreamWrapper& serverStream)
13 : SspiPackageBase(serverStream, NEGOSSP_NAME_W)
17 psd = CreateCurrentUserSecurityDescriptor();
19 throw MSXException(
"CreateCurrentUserSecurityDescriptor failed");
23SspiNegotiateServer::~SspiNegotiateServer()
28bool SspiNegotiateServer::Authenticate()
30 TimeStamp tsCredsExpiry;
31 SECURITY_STATUS ss = AcquireCredentialsHandleW(
33 const_cast<SEC_WCHAR*
>(NEGOSSP_NAME_W),
42 DebugPrintSecurityStatus(
"AcquireCredentialsHandleW", ss);
47 SecBufferDesc secClientBufferDesc, secServerBufferDesc;
48 SecBuffer secClientBuffer, secServerBuffer;
49 InitTokenContextBuffer(&secClientBufferDesc, &secClientBuffer);
50 InitTokenContextBuffer(&secServerBufferDesc, &secServerBuffer);
52 std::vector<char> buffer;
53 PCtxtHandle phContext =
nullptr;
56 bool ret = RecvChunk(stream, buffer, cbMaxTokenSize);
57 if (!ret)
return false;
59 secClientBuffer.cbBuffer =
static_cast<unsigned long>(buffer.size());
60 secClientBuffer.pvBuffer = &buffer[0];
63 TimeStamp tsContextExpiry;
64 ss = AcceptSecurityContext(
68 ASC_REQ_ALLOCATE_MEMORY | ASC_REQ_CONNECTION,
69 SECURITY_NETWORK_DREP,
75 DebugPrintSecurityStatus(
"AcceptSecurityContext", ss);
76 if (ss !=
one_of(SEC_E_OK, SEC_I_CONTINUE_NEEDED)) {
81 if (secServerBuffer.cbBuffer) {
82 ret = SendChunk(stream, secServerBuffer.pvBuffer, secServerBuffer.cbBuffer);
83 ClearContextBuffers(&secServerBufferDesc);
84 if (!ret)
return false;
89 DebugPrintSecurityPackageName(&hContext);
90 DebugPrintSecurityPrincipalName(&hContext);
95 phContext = &hContext;
99bool SspiNegotiateServer::Authorize()
105 HMODULE secur32 = GetModuleHandleW(L
"secur32.dll");
109 auto QuerySecurityContextToken =
reinterpret_cast<QUERY_SECURITY_CONTEXT_TOKEN_FN
>(
110 GetProcAddress(secur32,
"QuerySecurityContextToken"));
111 if (!QuerySecurityContextToken) {
117 SECURITY_STATUS ss = QuerySecurityContextToken(&hContext, &hClientToken);
118 DebugPrintSecurityStatus(
"QuerySecurityContextToken", ss);
119 if (ss != SEC_E_OK) {
123 PRIVILEGE_SET privilegeSet;
124 DWORD dwPrivSetSize =
sizeof(privilegeSet);
127 BOOL ret = AccessCheck(
131 const_cast<PGENERIC_MAPPING
>(&mapping),
137 DebugPrintSecurityBool(
"AccessCheck", ret);
138 DebugPrintSecurityPrincipalName(&hContext);
140 CloseHandle(hClientToken);
142 return ret && fAccess;
This file implemented 3 utility functions: