| 1 |
#ifdef _WIN32 |
| 2 |
|
| 3 |
#include "SspiUtils.hh" |
| 4 |
|
| 5 |
#include "MSXException.hh" |
| 6 |
|
| 7 |
#include "xrange.hh" |
| 8 |
|
| 9 |
#include <sddl.h> |
| 10 |
|
| 11 |
#include <bit> |
| 12 |
#include <cassert> |
| 13 |
#include <iostream> |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
namespace openmsx::sspiutils { |
| 20 |
|
| 21 |
SspiPackageBase::SspiPackageBase(StreamWrapper& userStream, const SEC_WCHAR* securityPackage) |
| 22 |
: stream(userStream) |
| 23 |
, cbMaxTokenSize(GetPackageMaxTokenSize(securityPackage)) |
| 24 |
{ |
| 25 |
memset(&hCreds, 0, sizeof(hCreds)); |
| 26 |
memset(&hContext, 0, sizeof(hContext)); |
| 27 |
|
| 28 |
if (!cbMaxTokenSize) { |
| 29 |
throw MSXException("GetPackageMaxTokenSize failed"); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
SspiPackageBase::~SspiPackageBase() |
| 34 |
{ |
| 35 |
DeleteSecurityContext(&hContext); |
| 36 |
FreeCredentialsHandle(&hCreds); |
| 37 |
} |
| 38 |
|
| 39 |
void InitTokenContextBuffer(PSecBufferDesc pSecBufferDesc, PSecBuffer pSecBuffer) |
| 40 |
{ |
| 41 |
pSecBuffer->BufferType = SECBUFFER_TOKEN; |
| 42 |
pSecBuffer->cbBuffer = 0; |
| 43 |
pSecBuffer->pvBuffer = nullptr; |
| 44 |
|
| 45 |
pSecBufferDesc->ulVersion = SECBUFFER_VERSION; |
| 46 |
pSecBufferDesc->cBuffers = 1; |
| 47 |
pSecBufferDesc->pBuffers = pSecBuffer; |
| 48 |
} |
| 49 |
|
| 50 |
void ClearContextBuffers(PSecBufferDesc pSecBufferDesc) |
| 51 |
{ |
| 52 |
for (auto i : xrange(pSecBufferDesc->cBuffers)) { |
| 53 |
FreeContextBuffer(pSecBufferDesc->pBuffers[i].pvBuffer); |
| 54 |
pSecBufferDesc->pBuffers[i].cbBuffer = 0; |
| 55 |
pSecBufferDesc->pBuffers[i].pvBuffer = nullptr; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
void DebugPrintSecurityStatus(const char* context, SECURITY_STATUS ss) |
| 60 |
{ |
| 61 |
(void)&context; |
| 62 |
(void)&ss; |
| 63 |
#if 0 |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
#endif |
| 88 |
} |
| 89 |
|
| 90 |
void DebugPrintSecurityBool(const char* context, BOOL ret) |
| 91 |
{ |
| 92 |
(void)&context; |
| 93 |
(void)&ret; |
| 94 |
#if 0 |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
#endif |
| 101 |
} |
| 102 |
|
| 103 |
void DebugPrintSecurityPackageName(PCtxtHandle phContext) |
| 104 |
{ |
| 105 |
(void)&phContext; |
| 106 |
#if 0 |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
#endif |
| 113 |
} |
| 114 |
|
| 115 |
void DebugPrintSecurityPrincipalName(PCtxtHandle phContext) |
| 116 |
{ |
| 117 |
(void)&phContext; |
| 118 |
#if 0 |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
#endif |
| 125 |
} |
| 126 |
|
| 127 |
void DebugPrintSecurityDescriptor(PSECURITY_DESCRIPTOR psd) |
| 128 |
{ |
| 129 |
(void)&psd; |
| 130 |
#if 0 |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
#endif |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
static PTOKEN_USER GetProcessToken() |
| 149 |
{ |
| 150 |
PTOKEN_USER pToken = nullptr; |
| 151 |
|
| 152 |
HANDLE hProcessToken; |
| 153 |
BOOL ret = OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hProcessToken); |
| 154 |
DebugPrintSecurityBool("OpenProcessToken", ret); |
| 155 |
if (ret) { |
| 156 |
DWORD cbToken; |
| 157 |
ret = GetTokenInformation(hProcessToken, TokenUser, nullptr, 0, &cbToken); |
| 158 |
assert(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER && cbToken); |
| 159 |
|
| 160 |
pToken = static_cast<TOKEN_USER*>(LocalAlloc(LMEM_ZEROINIT, cbToken)); |
| 161 |
if (pToken) { |
| 162 |
ret = GetTokenInformation(hProcessToken, TokenUser, pToken, cbToken, &cbToken); |
| 163 |
DebugPrintSecurityBool("GetTokenInformation", ret); |
| 164 |
if (!ret) { |
| 165 |
LocalFree(pToken); |
| 166 |
pToken = nullptr; |
| 167 |
} |
| 168 |
} |
| 169 |
CloseHandle(hProcessToken); |
| 170 |
} |
| 171 |
return pToken; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
PSECURITY_DESCRIPTOR CreateCurrentUserSecurityDescriptor() |
| 177 |
{ |
| 178 |
PSECURITY_DESCRIPTOR psd = nullptr; |
| 179 |
PTOKEN_USER pToken = GetProcessToken(); |
| 180 |
if (pToken) { |
| 181 |
PSID pUserSid = pToken->User.Sid; |
| 182 |
const DWORD cbEachAce = sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD); |
| 183 |
const DWORD cbACL = sizeof(ACL) + cbEachAce + GetLengthSid(pUserSid); |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
BYTE* buffer = static_cast<BYTE*>(LocalAlloc(LMEM_ZEROINIT, SECURITY_DESCRIPTOR_MIN_LENGTH + cbACL)); |
| 188 |
if (buffer) { |
| 189 |
psd = static_cast<PSECURITY_DESCRIPTOR>(buffer); |
| 190 |
PACL pacl = reinterpret_cast<PACL>(buffer + SECURITY_DESCRIPTOR_MIN_LENGTH); |
| 191 |
PACCESS_ALLOWED_ACE pUserAce; |
| 192 |
if (InitializeSecurityDescriptor(psd, SECURITY_DESCRIPTOR_REVISION) && |
| 193 |
InitializeAcl(pacl, cbACL, ACL_REVISION) && |
| 194 |
AddAccessAllowedAce(pacl, ACL_REVISION, ACCESS_ALL, pUserSid) && |
| 195 |
SetSecurityDescriptorDacl(psd, TRUE, pacl, FALSE) && |
| 196 |
|
| 197 |
GetAce(pacl, 0, std::bit_cast<void**>(&pUserAce)) && |
| 198 |
SetSecurityDescriptorGroup(psd, &pUserAce->SidStart, FALSE) && |
| 199 |
SetSecurityDescriptorOwner(psd, &pUserAce->SidStart, FALSE)) { |
| 200 |
buffer = nullptr; |
| 201 |
} else { |
| 202 |
psd = nullptr; |
| 203 |
} |
| 204 |
LocalFree(buffer); |
| 205 |
} |
| 206 |
LocalFree(pToken); |
| 207 |
} |
| 208 |
|
| 209 |
if (psd) { |
| 210 |
assert(IsValidSecurityDescriptor(psd)); |
| 211 |
DebugPrintSecurityDescriptor(psd); |
| 212 |
} |
| 213 |
return psd; |
| 214 |
} |
| 215 |
|
| 216 |
unsigned long GetPackageMaxTokenSize(const SEC_WCHAR* package) |
| 217 |
{ |
| 218 |
PSecPkgInfoW pkgInfo; |
| 219 |
SECURITY_STATUS ss = QuerySecurityPackageInfoW(const_cast<SEC_WCHAR*>(package), &pkgInfo); |
| 220 |
DebugPrintSecurityStatus("QuerySecurityPackageInfoW", ss); |
| 221 |
if (ss != SEC_E_OK) return 0; |
| 222 |
|
| 223 |
unsigned long cbMaxToken = pkgInfo->cbMaxToken; |
| 224 |
FreeContextBuffer(pkgInfo); |
| 225 |
return cbMaxToken; |
| 226 |
} |
| 227 |
|
| 228 |
static bool Send(StreamWrapper& stream, void* buffer, uint32_t cb) |
| 229 |
{ |
| 230 |
uint32_t sent = 0; |
| 231 |
while (sent < cb) { |
| 232 |
uint32_t ret = stream.Write(static_cast<char*>(buffer) + sent, cb - sent); |
| 233 |
if (ret == STREAM_ERROR) return false; |
| 234 |
sent += ret; |
| 235 |
} |
| 236 |
return true; |
| 237 |
} |
| 238 |
|
| 239 |
bool SendChunk(StreamWrapper& stream, void* buffer, uint32_t cb) |
| 240 |
{ |
| 241 |
uint32_t nl = htonl(cb); |
| 242 |
if (!Send(stream, &nl, sizeof(nl))) { |
| 243 |
return false; |
| 244 |
} |
| 245 |
return Send(stream, buffer, cb); |
| 246 |
} |
| 247 |
|
| 248 |
static bool Recv(StreamWrapper& stream, void* buffer, uint32_t cb) |
| 249 |
{ |
| 250 |
uint32_t recvd = 0; |
| 251 |
while (recvd < cb) { |
| 252 |
uint32_t ret = stream.Read(static_cast<char*>(buffer) + recvd, cb - recvd); |
| 253 |
if (ret == STREAM_ERROR) return false; |
| 254 |
recvd += ret; |
| 255 |
} |
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
static bool RecvChunkSize(StreamWrapper& stream, uint32_t* pcb) |
| 260 |
{ |
| 261 |
uint32_t cb; |
| 262 |
bool ret = Recv(stream, &cb, sizeof(cb)); |
| 263 |
if (ret) { |
| 264 |
*pcb = ntohl(cb); |
| 265 |
} |
| 266 |
return ret; |
| 267 |
} |
| 268 |
|
| 269 |
bool RecvChunk(StreamWrapper& stream, std::vector<char>& buffer, uint32_t cbMaxSize) |
| 270 |
{ |
| 271 |
uint32_t cb; |
| 272 |
if (!RecvChunkSize(stream, &cb) || cb > cbMaxSize) { |
| 273 |
return false; |
| 274 |
} |
| 275 |
buffer.resize(cb); |
| 276 |
if (!Recv(stream, &buffer[0], cb)) { |
| 277 |
return false; |
| 278 |
} |
| 279 |
return true; |
| 280 |
} |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
#endif |