FreeRDP
Loading...
Searching...
No Matches
tsssp.c
1
23
24#include <winpr/config.h>
25#include <winpr/platform.h>
26
27#include <winpr/crt.h>
28#include <winpr/synch.h>
29#include <winpr/library.h>
30#include <winpr/sspi.h>
31
32#include "tsssp.h"
33
34#include "../sspi.h"
35#include "../../log.h"
36
37#define TAG WINPR_TAG("sspi.TSSSP")
38
39#if defined(_WIN32) && !defined(_UWP) && defined(WITH_NATIVE_SSPI)
40#define TSSSP_FORWARD_NATIVE 1
41#endif
42
43#if defined(TSSSP_FORWARD_NATIVE)
44
45/* A WinPR credential/context handle wraps the native secur32 handle. */
46typedef struct
47{
48 CredHandle native;
49} TSSSP_CREDENTIALS;
50
51typedef struct
52{
53 CtxtHandle native;
54} TSSSP_CONTEXT;
55
56static INIT_ONCE g_NativeOnce = INIT_ONCE_STATIC_INIT;
57static HMODULE g_NativeModule = nullptr;
58static PSecurityFunctionTableW g_NativeW = nullptr;
59static PSecurityFunctionTableA g_NativeA = nullptr;
60
61static BOOL CALLBACK tsssp_native_init(WINPR_ATTR_UNUSED PINIT_ONCE once,
62 WINPR_ATTR_UNUSED PVOID param, WINPR_ATTR_UNUSED PVOID* ctx)
63{
64 g_NativeModule = LoadLibraryW(L"secur32.dll");
65 if (!g_NativeModule)
66 g_NativeModule = LoadLibraryW(L"sspicli.dll");
67 if (!g_NativeModule)
68 {
69 WLog_ERR(TAG, "failed to load the native SSPI module");
70 return TRUE;
71 }
72
73 INIT_SECURITY_INTERFACE_W pInitW =
74 GetProcAddressAs(g_NativeModule, "InitSecurityInterfaceW", INIT_SECURITY_INTERFACE_W);
75 INIT_SECURITY_INTERFACE_A pInitA =
76 GetProcAddressAs(g_NativeModule, "InitSecurityInterfaceA", INIT_SECURITY_INTERFACE_A);
77
78 if (pInitW)
79 g_NativeW = pInitW();
80 if (pInitA)
81 g_NativeA = pInitA();
82
83 return TRUE;
84}
85
86static PSecurityFunctionTableW tsssp_native_W(void)
87{
88 (void)InitOnceExecuteOnce(&g_NativeOnce, tsssp_native_init, nullptr, nullptr);
89 return g_NativeW;
90}
91
92static PSecurityFunctionTableA tsssp_native_A(void)
93{
94 (void)InitOnceExecuteOnce(&g_NativeOnce, tsssp_native_init, nullptr, nullptr);
95 return g_NativeA;
96}
97
98/* Replace any output SECBUFFER_TOKEN that secur32 allocated (ISC_REQ_ALLOCATE_MEMORY)
99 * with a copy owned by the WinPR context-buffer allocator, so the caller can free it
100 * through the WinPR FreeContextBuffer. */
101static SECURITY_STATUS tsssp_reown_output(PSecurityFunctionTableW nat, PSecBufferDesc pOutput)
102{
103 if (!pOutput || !pOutput->pBuffers)
104 return SEC_E_OK;
105
106 for (ULONG i = 0; i < pOutput->cBuffers; i++)
107 {
108 SecBuffer* b = &pOutput->pBuffers[i];
109 if ((b->BufferType != SECBUFFER_TOKEN) || !b->pvBuffer || (b->cbBuffer == 0))
110 continue;
111
112 void* copy = sspi_ContextBufferAlloc(InitializeSecurityContextIndex, b->cbBuffer);
113 if (!copy)
114 return SEC_E_INSUFFICIENT_MEMORY;
115
116 CopyMemory(copy, b->pvBuffer, b->cbBuffer);
117 if (nat && nat->FreeContextBuffer)
118 (void)nat->FreeContextBuffer(b->pvBuffer);
119 b->pvBuffer = copy;
120 }
121
122 return SEC_E_OK;
123}
124
125static SECURITY_STATUS SEC_ENTRY tsssp_AcquireCredentialsHandleW(
126 SEC_WCHAR* pszPrincipal, SEC_WCHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
127 void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
128 PTimeStamp ptsExpiry)
129{
130 PSecurityFunctionTableW nat = tsssp_native_W();
131 if (!nat || !nat->AcquireCredentialsHandleW)
132 return SEC_E_SECPKG_NOT_FOUND;
133
134 TSSSP_CREDENTIALS* creds = (TSSSP_CREDENTIALS*)calloc(1, sizeof(TSSSP_CREDENTIALS));
135 if (!creds)
136 return SEC_E_INSUFFICIENT_MEMORY;
137
138 const SECURITY_STATUS status = nat->AcquireCredentialsHandleW(
139 pszPrincipal, pszPackage, fCredentialUse, pvLogonID, pAuthData, pGetKeyFn, pvGetKeyArgument,
140 &creds->native, ptsExpiry);
141 if (status != SEC_E_OK)
142 {
143 free(creds);
144 return status;
145 }
146
147 sspi_SecureHandleSetLowerPointer(phCredential, creds);
148 sspi_SecureHandleSetPackageId(phCredential, SSPI_PACKAGE_TSSSP);
149 return SEC_E_OK;
150}
151
152static SECURITY_STATUS SEC_ENTRY tsssp_AcquireCredentialsHandleA(
153 SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
154 void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
155 PTimeStamp ptsExpiry)
156{
157 PSecurityFunctionTableA nat = tsssp_native_A();
158 if (!nat || !nat->AcquireCredentialsHandleA)
159 return SEC_E_SECPKG_NOT_FOUND;
160
161 TSSSP_CREDENTIALS* creds = (TSSSP_CREDENTIALS*)calloc(1, sizeof(TSSSP_CREDENTIALS));
162 if (!creds)
163 return SEC_E_INSUFFICIENT_MEMORY;
164
165 const SECURITY_STATUS status = nat->AcquireCredentialsHandleA(
166 pszPrincipal, pszPackage, fCredentialUse, pvLogonID, pAuthData, pGetKeyFn, pvGetKeyArgument,
167 &creds->native, ptsExpiry);
168 if (status != SEC_E_OK)
169 {
170 free(creds);
171 return status;
172 }
173
174 sspi_SecureHandleSetLowerPointer(phCredential, creds);
175 sspi_SecureHandleSetPackageId(phCredential, SSPI_PACKAGE_TSSSP);
176 return SEC_E_OK;
177}
178
179static SECURITY_STATUS SEC_ENTRY tsssp_FreeCredentialsHandle(PCredHandle phCredential)
180{
181 if (!phCredential)
182 return SEC_E_INVALID_HANDLE;
183
184 TSSSP_CREDENTIALS* creds = (TSSSP_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
185 sspi_SecureHandleInvalidate(phCredential);
186 if (!creds)
187 return SEC_E_INVALID_HANDLE;
188
189 PSecurityFunctionTableW nat = tsssp_native_W();
190 SECURITY_STATUS status = SEC_E_OK;
191 if (nat && nat->FreeCredentialsHandle)
192 status = nat->FreeCredentialsHandle(&creds->native);
193
194 free(creds);
195 return status;
196}
197
198static SECURITY_STATUS tsssp_initialize_context(
199 BOOL unicode, PCredHandle phCredential, PCtxtHandle phContext, void* pszTargetName,
200 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
201 PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
202{
203 PSecurityFunctionTableW natW = tsssp_native_W();
204 PSecurityFunctionTableA natA = tsssp_native_A();
205 if (unicode && (!natW || !natW->InitializeSecurityContextW))
206 return SEC_E_SECPKG_NOT_FOUND;
207 if (!unicode && (!natA || !natA->InitializeSecurityContextA))
208 return SEC_E_SECPKG_NOT_FOUND;
209
210 TSSSP_CREDENTIALS* creds = (TSSSP_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
211 if (!creds)
212 return SEC_E_INVALID_HANDLE;
213
214 TSSSP_CONTEXT* context =
215 phContext ? (TSSSP_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext) : nullptr;
216 const BOOL fresh = (context == nullptr);
217 if (fresh)
218 {
219 context = (TSSSP_CONTEXT*)calloc(1, sizeof(TSSSP_CONTEXT));
220 if (!context)
221 return SEC_E_INSUFFICIENT_MEMORY;
222 }
223
224 PCtxtHandle inNative = fresh ? nullptr : &context->native;
225
226 SECURITY_STATUS status = 0;
227 if (unicode)
228 status = natW->InitializeSecurityContextW(
229 &creds->native, inNative, (SEC_WCHAR*)pszTargetName, fContextReq, Reserved1,
230 TargetDataRep, pInput, Reserved2, &context->native, pOutput, pfContextAttr, ptsExpiry);
231 else
232 status = natA->InitializeSecurityContextA(
233 &creds->native, inNative, (SEC_CHAR*)pszTargetName, fContextReq, Reserved1,
234 TargetDataRep, pInput, Reserved2, &context->native, pOutput, pfContextAttr, ptsExpiry);
235
236 if ((status != SEC_E_OK) && (status != SEC_I_CONTINUE_NEEDED) &&
237 (status != SEC_I_COMPLETE_NEEDED) && (status != SEC_I_COMPLETE_AND_CONTINUE))
238 {
239 if (fresh)
240 free(context);
241 return status;
242 }
243
244 const SECURITY_STATUS reown = tsssp_reown_output(natW, pOutput);
245 if (reown != SEC_E_OK)
246 {
247 if (fresh)
248 {
249 if (natW && natW->DeleteSecurityContext)
250 (void)natW->DeleteSecurityContext(&context->native);
251 free(context);
252 }
253 return reown;
254 }
255
256 sspi_SecureHandleSetLowerPointer(phNewContext, context);
257 sspi_SecureHandleSetPackageId(phNewContext, SSPI_PACKAGE_TSSSP);
258 return status;
259}
260
261static SECURITY_STATUS SEC_ENTRY tsssp_InitializeSecurityContextW(
262 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq,
263 ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
264 PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
265{
266 return tsssp_initialize_context(TRUE, phCredential, phContext, pszTargetName, fContextReq,
267 Reserved1, TargetDataRep, pInput, Reserved2, phNewContext,
268 pOutput, pfContextAttr, ptsExpiry);
269}
270
271static SECURITY_STATUS SEC_ENTRY tsssp_InitializeSecurityContextA(
272 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR* pszTargetName, ULONG fContextReq,
273 ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
274 PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
275{
276 return tsssp_initialize_context(FALSE, phCredential, phContext, pszTargetName, fContextReq,
277 Reserved1, TargetDataRep, pInput, Reserved2, phNewContext,
278 pOutput, pfContextAttr, ptsExpiry);
279}
280
281static SECURITY_STATUS tsssp_query_context(BOOL unicode, PCtxtHandle phContext, ULONG ulAttribute,
282 void* pBuffer)
283{
284 if (!phContext)
285 return SEC_E_INVALID_HANDLE;
286
287 TSSSP_CONTEXT* context = (TSSSP_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
288 if (!context)
289 return SEC_E_INVALID_HANDLE;
290
291 if (ulAttribute == SECPKG_ATTR_WINPR_TSPKG_CONTEXT)
292 {
293 if (!pBuffer)
294 return SEC_E_INSUFFICIENT_MEMORY;
295
296 /* The native TSSSP keeps the TsPkgContext handle in the context's upper word. */
297 *((UINT64*)pBuffer) = (UINT64)context->native.dwUpper;
298 return SEC_E_OK;
299 }
300
301 PSecurityFunctionTableW natW = tsssp_native_W();
302 PSecurityFunctionTableA natA = tsssp_native_A();
303 if (unicode && natW && natW->QueryContextAttributesW)
304 return natW->QueryContextAttributesW(&context->native, ulAttribute, pBuffer);
305 if (!unicode && natA && natA->QueryContextAttributesA)
306 return natA->QueryContextAttributesA(&context->native, ulAttribute, pBuffer);
307 return SEC_E_UNSUPPORTED_FUNCTION;
308}
309
310static SECURITY_STATUS SEC_ENTRY tsssp_QueryContextAttributesW(PCtxtHandle phContext,
311 ULONG ulAttribute, void* pBuffer)
312{
313 return tsssp_query_context(TRUE, phContext, ulAttribute, pBuffer);
314}
315
316static SECURITY_STATUS SEC_ENTRY tsssp_QueryContextAttributesA(PCtxtHandle phContext,
317 ULONG ulAttribute, void* pBuffer)
318{
319 return tsssp_query_context(FALSE, phContext, ulAttribute, pBuffer);
320}
321
322static SECURITY_STATUS SEC_ENTRY tsssp_DeleteSecurityContext(PCtxtHandle phContext)
323{
324 if (!phContext)
325 return SEC_E_INVALID_HANDLE;
326
327 TSSSP_CONTEXT* context = (TSSSP_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
328 sspi_SecureHandleInvalidate(phContext);
329 if (!context)
330 return SEC_E_INVALID_HANDLE;
331
332 PSecurityFunctionTableW nat = tsssp_native_W();
333 SECURITY_STATUS status = SEC_E_OK;
334 if (nat && nat->DeleteSecurityContext)
335 status = nat->DeleteSecurityContext(&context->native);
336
337 free(context);
338 return status;
339}
340
341static SECURITY_STATUS SEC_ENTRY tsssp_SetContextAttributesW(PCtxtHandle phContext,
342 ULONG ulAttribute, void* pBuffer,
343 ULONG cbBuffer)
344{
345 TSSSP_CONTEXT* context = (TSSSP_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
346 if (!context)
347 return SEC_E_INVALID_HANDLE;
348
349 PSecurityFunctionTableW natW = tsssp_native_W();
350 if (natW && natW->SetContextAttributesW)
351 return natW->SetContextAttributesW(&context->native, ulAttribute, pBuffer, cbBuffer);
352 return SEC_E_UNSUPPORTED_FUNCTION;
353}
354
355#else /* !TSSSP_FORWARD_NATIVE: proxy to the lsa4posix daemon */
356
357#include <winpr/credssp.h>
358
359#include "../../sspicli/lsa_proto.h"
360#include "../../sspicli/lsa_transport.h"
361
362/* A credential owns the daemon connection; the context rides the same socket. */
363typedef struct
364{
365 int fd;
366 UINT64 credId;
367 UINT32 reqId;
368} TSSSP_PROXY_CRED;
369
370typedef struct
371{
372 int fd;
373 UINT64 ctxId;
374 UINT32 reqId;
375} TSSSP_PROXY_CTX;
376
377static SECURITY_STATUS SEC_ENTRY tsssp_AcquireCredentialsHandleW(
378 WINPR_ATTR_UNUSED SEC_WCHAR* pszPrincipal, WINPR_ATTR_UNUSED SEC_WCHAR* pszPackage,
379 ULONG fCredentialUse, WINPR_ATTR_UNUSED void* pvLogonID, void* pAuthData,
380 WINPR_ATTR_UNUSED SEC_GET_KEY_FN pGetKeyFn, WINPR_ATTR_UNUSED void* pvGetKeyArgument,
381 PCredHandle phCredential, WINPR_ATTR_UNUSED PTimeStamp ptsExpiry)
382{
383 if (fCredentialUse != SECPKG_CRED_OUTBOUND)
384 return SEC_E_UNSUPPORTED_FUNCTION;
385
386 const int fd = lsa4posix_client_connect();
387 if (fd < 0)
388 {
389 WLog_ERR(TAG, "the lsa4posix daemon is not available");
390 return SEC_E_NO_CREDENTIALS;
391 }
392
393 DWORD flags = 0;
394 SEC_WINNT_AUTH_IDENTITY_W* identity = nullptr;
395 if (pAuthData)
396 {
397 CREDSSP_CRED_EX* credEx = (CREDSSP_CRED_EX*)pAuthData;
398 flags = credEx->Flags;
399 identity = (SEC_WINNT_AUTH_IDENTITY_W*)credEx->Cred.pSpnegoCred;
400 }
401
402 const UINT32 reqId = 1;
403 LSA_MSG_HDR hdr = { SSPI_MSG_ACQUIRE_CRED, 0, reqId };
404 LsaProtoWriter w = WINPR_C_ARRAY_INIT;
405 wStream* request = nullptr;
406 BOOL built = lsa_proto_writer_init(&w, &hdr) && lsa_proto_write_u32(&w, flags) &&
407 lsa_proto_write_u16(&w, identity ? 1 : 0);
408 if (built && identity)
409 built = lsa_proto_write_blob(&w, (const BYTE*)identity->User,
410 identity->UserLength * sizeof(WCHAR)) &&
411 lsa_proto_write_blob(&w, (const BYTE*)identity->Domain,
412 identity->DomainLength * sizeof(WCHAR)) &&
413 lsa_proto_write_blob(&w, (const BYTE*)identity->Password,
414 identity->PasswordLength * sizeof(WCHAR));
415 if (!built || !lsa_proto_writer_finish(&w, &request))
416 {
417 lsa_proto_writer_abort(&w);
418 lsa4posix_client_close(fd);
419 return SEC_E_INTERNAL_ERROR;
420 }
421
422 LsaProtoReader r = WINPR_C_ARRAY_INIT;
423 LSA_MSG_HDR rh = WINPR_C_ARRAY_INIT;
424 wStream* response = nullptr;
425 if (lsa4posix_transact(fd, SSPI_MSG_ACQUIRE_CRED, request, reqId, &r, &rh, &response) !=
426 STATUS_SUCCESS)
427 {
428 lsa4posix_client_close(fd);
429 return SEC_E_NO_CREDENTIALS;
430 }
431
432 UINT32 status = 0;
433 UINT64 credId = 0;
434 const BOOL ok = lsa_proto_read_u32(&r, &status) && lsa_proto_read_u64(&r, &credId);
435 lsa_proto_reader_done(&r);
436 Stream_Free(response, TRUE);
437 if (!ok || (status != SEC_E_OK))
438 {
439 lsa4posix_client_close(fd);
440 return ok ? (SECURITY_STATUS)status : SEC_E_INTERNAL_ERROR;
441 }
442
443 TSSSP_PROXY_CRED* creds = (TSSSP_PROXY_CRED*)calloc(1, sizeof(TSSSP_PROXY_CRED));
444 if (!creds)
445 {
446 lsa4posix_client_close(fd);
447 return SEC_E_INSUFFICIENT_MEMORY;
448 }
449 creds->fd = fd;
450 creds->credId = credId;
451 creds->reqId = 2;
452 sspi_SecureHandleSetLowerPointer(phCredential, creds);
453 sspi_SecureHandleSetPackageId(phCredential, SSPI_PACKAGE_TSSSP);
454 return SEC_E_OK;
455}
456
457static SECURITY_STATUS SEC_ENTRY tsssp_AcquireCredentialsHandleA(
458 WINPR_ATTR_UNUSED SEC_CHAR* pszPrincipal, WINPR_ATTR_UNUSED SEC_CHAR* pszPackage,
459 WINPR_ATTR_UNUSED ULONG fCredentialUse, WINPR_ATTR_UNUSED void* pvLogonID,
460 WINPR_ATTR_UNUSED void* pAuthData, WINPR_ATTR_UNUSED SEC_GET_KEY_FN pGetKeyFn,
461 WINPR_ATTR_UNUSED void* pvGetKeyArgument, WINPR_ATTR_UNUSED PCredHandle phCredential,
462 WINPR_ATTR_UNUSED PTimeStamp ptsExpiry)
463{
464 return SEC_E_UNSUPPORTED_FUNCTION;
465}
466
467static SECURITY_STATUS SEC_ENTRY tsssp_FreeCredentialsHandle(PCredHandle phCredential)
468{
469 if (!phCredential)
470 return SEC_E_INVALID_HANDLE;
471 TSSSP_PROXY_CRED* creds = (TSSSP_PROXY_CRED*)sspi_SecureHandleGetLowerPointer(phCredential);
472 sspi_SecureHandleInvalidate(phCredential);
473 if (!creds)
474 return SEC_E_INVALID_HANDLE;
475
476 LSA_MSG_HDR hdr = { SSPI_MSG_FREE_CRED, 0, creds->reqId++ };
477 LsaProtoWriter w = WINPR_C_ARRAY_INIT;
478 wStream* request = nullptr;
479 if (lsa_proto_writer_init(&w, &hdr) && lsa_proto_write_u64(&w, creds->credId) &&
480 lsa_proto_writer_finish(&w, &request))
481 {
482 LsaProtoReader r = WINPR_C_ARRAY_INIT;
483 LSA_MSG_HDR rh = WINPR_C_ARRAY_INIT;
484 wStream* response = nullptr;
485 if (lsa4posix_transact(creds->fd, SSPI_MSG_FREE_CRED, request, hdr.requestId, &r, &rh,
486 &response) == STATUS_SUCCESS)
487 {
488 lsa_proto_reader_done(&r);
489 Stream_Free(response, TRUE);
490 }
491 }
492 else
493 lsa_proto_writer_abort(&w);
494
495 lsa4posix_client_close(creds->fd);
496 free(creds);
497 return SEC_E_OK;
498}
499
500static SECURITY_STATUS SEC_ENTRY tsssp_InitializeSecurityContextW(
501 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq,
502 WINPR_ATTR_UNUSED ULONG Reserved1, WINPR_ATTR_UNUSED ULONG TargetDataRep, PSecBufferDesc pInput,
503 WINPR_ATTR_UNUSED ULONG Reserved2, PCtxtHandle phNewContext, PSecBufferDesc pOutput,
504 WINPR_ATTR_UNUSED PULONG pfContextAttr, WINPR_ATTR_UNUSED PTimeStamp ptsExpiry)
505{
506 TSSSP_PROXY_CRED* creds = (TSSSP_PROXY_CRED*)sspi_SecureHandleGetLowerPointer(phCredential);
507 if (!creds)
508 return SEC_E_INVALID_HANDLE;
509
510 TSSSP_PROXY_CTX* context =
511 phContext ? (TSSSP_PROXY_CTX*)sspi_SecureHandleGetLowerPointer(phContext) : nullptr;
512 const UINT64 ctxId = context ? context->ctxId : 0;
513
514 const SecBuffer* inToken = nullptr;
515 const SecBuffer* publicKey = nullptr;
516 if (pInput)
517 {
518 for (ULONG i = 0; i < pInput->cBuffers; i++)
519 {
520 const SecBuffer* b = &pInput->pBuffers[i];
521 if ((b->BufferType & ~SECBUFFER_ATTRMASK) != SECBUFFER_TOKEN)
522 continue;
523 if (b->BufferType & SECBUFFER_READONLY)
524 publicKey = b;
525 else if (b->cbBuffer > 0)
526 inToken = b;
527 }
528 }
529
530 /* The target is a UTF-16 SPN (for example TERMSRV/host); send its bytes without the NUL. */
531 const UINT32 targetLen = pszTargetName ? (UINT32)(_wcslen(pszTargetName) * sizeof(WCHAR)) : 0;
532
533 const UINT32 reqId = creds->reqId++;
534 LSA_MSG_HDR hdr = { SSPI_MSG_ISC, 0, reqId };
535 LsaProtoWriter w = WINPR_C_ARRAY_INIT;
536 wStream* request = nullptr;
537 if (!lsa_proto_writer_init(&w, &hdr) || !lsa_proto_write_u64(&w, creds->credId) ||
538 !lsa_proto_write_u64(&w, ctxId) || !lsa_proto_write_u32(&w, fContextReq) ||
539 !lsa_proto_write_blob(&w, (const BYTE*)pszTargetName, targetLen) ||
540 !lsa_proto_write_blob(&w, inToken ? (const BYTE*)inToken->pvBuffer : nullptr,
541 inToken ? inToken->cbBuffer : 0) ||
542 !lsa_proto_write_blob(&w, publicKey ? (const BYTE*)publicKey->pvBuffer : nullptr,
543 publicKey ? publicKey->cbBuffer : 0) ||
544 !lsa_proto_writer_finish(&w, &request))
545 {
546 lsa_proto_writer_abort(&w);
547 return SEC_E_INTERNAL_ERROR;
548 }
549
550 LsaProtoReader r = WINPR_C_ARRAY_INIT;
551 LSA_MSG_HDR rh = WINPR_C_ARRAY_INIT;
552 wStream* response = nullptr;
553 if (lsa4posix_transact(creds->fd, SSPI_MSG_ISC, request, reqId, &r, &rh, &response) !=
554 STATUS_SUCCESS)
555 return SEC_E_INTERNAL_ERROR;
556
557 UINT32 status = 0;
558 UINT64 newCtxId = 0;
559 BYTE* outToken = nullptr;
560 UINT32 outLen = 0;
561 const BOOL ok = lsa_proto_read_u32(&r, &status) && lsa_proto_read_u64(&r, &newCtxId) &&
562 lsa_proto_read_blob(&r, &outToken, &outLen);
563 lsa_proto_reader_done(&r);
564 Stream_Free(response, TRUE);
565 if (!ok)
566 {
567 free(outToken);
568 return SEC_E_INTERNAL_ERROR;
569 }
570
571 const SECURITY_STATUS sstatus = (SECURITY_STATUS)status;
572 if ((sstatus == SEC_E_OK) || (sstatus == SEC_I_CONTINUE_NEEDED))
573 {
574 if (!context)
575 {
576 context = (TSSSP_PROXY_CTX*)calloc(1, sizeof(TSSSP_PROXY_CTX));
577 if (!context)
578 {
579 free(outToken);
580 return SEC_E_INSUFFICIENT_MEMORY;
581 }
582 context->fd = creds->fd;
583 context->reqId = 1;
584 }
585 context->ctxId = newCtxId;
586 sspi_SecureHandleSetLowerPointer(phNewContext, context);
587 sspi_SecureHandleSetPackageId(phNewContext, SSPI_PACKAGE_TSSSP);
588
589 if (pOutput && (pOutput->cBuffers >= 1))
590 {
591 SecBuffer* out = &pOutput->pBuffers[0];
592 out->BufferType = SECBUFFER_TOKEN;
593 out->pvBuffer = nullptr;
594 out->cbBuffer = 0;
595 if (outLen > 0)
596 {
597 void* buf = sspi_ContextBufferAlloc(InitializeSecurityContextIndex, outLen);
598 if (!buf)
599 {
600 free(outToken);
601 return SEC_E_INSUFFICIENT_MEMORY;
602 }
603 CopyMemory(buf, outToken, outLen);
604 out->pvBuffer = buf;
605 out->cbBuffer = outLen;
606 }
607 }
608 }
609
610 free(outToken);
611 return sstatus;
612}
613
614static SECURITY_STATUS SEC_ENTRY tsssp_InitializeSecurityContextA(
615 WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED PCtxtHandle phContext,
616 WINPR_ATTR_UNUSED SEC_CHAR* pszTargetName, WINPR_ATTR_UNUSED ULONG fContextReq,
617 WINPR_ATTR_UNUSED ULONG Reserved1, WINPR_ATTR_UNUSED ULONG TargetDataRep,
618 WINPR_ATTR_UNUSED PSecBufferDesc pInput, WINPR_ATTR_UNUSED ULONG Reserved2,
619 WINPR_ATTR_UNUSED PCtxtHandle phNewContext, WINPR_ATTR_UNUSED PSecBufferDesc pOutput,
620 WINPR_ATTR_UNUSED PULONG pfContextAttr, WINPR_ATTR_UNUSED PTimeStamp ptsExpiry)
621{
622 return SEC_E_UNSUPPORTED_FUNCTION;
623}
624
625static SECURITY_STATUS SEC_ENTRY tsssp_QueryContextAttributesW(PCtxtHandle phContext,
626 ULONG ulAttribute, void* pBuffer)
627{
628 TSSSP_PROXY_CTX* context = (TSSSP_PROXY_CTX*)sspi_SecureHandleGetLowerPointer(phContext);
629 if (!context)
630 return SEC_E_INVALID_HANDLE;
631
632 if (ulAttribute == SECPKG_ATTR_WINPR_TSPKG_CONTEXT)
633 {
634 if (!pBuffer)
635 return SEC_E_INSUFFICIENT_MEMORY;
636 *((UINT64*)pBuffer) = context->ctxId;
637 return SEC_E_OK;
638 }
639 return SEC_E_UNSUPPORTED_FUNCTION;
640}
641
642static SECURITY_STATUS SEC_ENTRY
643tsssp_QueryContextAttributesA(WINPR_ATTR_UNUSED PCtxtHandle phContext,
644 WINPR_ATTR_UNUSED ULONG ulAttribute, WINPR_ATTR_UNUSED void* pBuffer)
645{
646 return SEC_E_UNSUPPORTED_FUNCTION;
647}
648
649static SECURITY_STATUS SEC_ENTRY tsssp_DeleteSecurityContext(PCtxtHandle phContext)
650{
651 if (!phContext)
652 return SEC_E_INVALID_HANDLE;
653 TSSSP_PROXY_CTX* context = (TSSSP_PROXY_CTX*)sspi_SecureHandleGetLowerPointer(phContext);
654 sspi_SecureHandleInvalidate(phContext);
655 if (!context)
656 return SEC_E_INVALID_HANDLE;
657
658 LSA_MSG_HDR hdr = { SSPI_MSG_DELETE_CTX, 0, context->reqId++ };
659 LsaProtoWriter w = WINPR_C_ARRAY_INIT;
660 wStream* request = nullptr;
661 if (lsa_proto_writer_init(&w, &hdr) && lsa_proto_write_u64(&w, context->ctxId) &&
662 lsa_proto_writer_finish(&w, &request))
663 {
664 LsaProtoReader r = WINPR_C_ARRAY_INIT;
665 LSA_MSG_HDR rh = WINPR_C_ARRAY_INIT;
666 wStream* response = nullptr;
667 if (lsa4posix_transact(context->fd, SSPI_MSG_DELETE_CTX, request, hdr.requestId, &r, &rh,
668 &response) == STATUS_SUCCESS)
669 {
670 lsa_proto_reader_done(&r);
671 Stream_Free(response, TRUE);
672 }
673 }
674 else
675 lsa_proto_writer_abort(&w);
676
677 free(context);
678 return SEC_E_OK;
679}
680
681static SECURITY_STATUS SEC_ENTRY tsssp_SetContextAttributesW(PCtxtHandle phContext,
682 ULONG ulAttribute, void* pBuffer,
683 ULONG cbBuffer)
684{
685 WINPR_UNUSED(phContext);
686 WINPR_UNUSED(ulAttribute);
687 WINPR_UNUSED(pBuffer);
688 WINPR_UNUSED(cbBuffer);
689 return SEC_E_UNSUPPORTED_FUNCTION;
690}
691
692#endif /* TSSSP_FORWARD_NATIVE */
693
694const SecurityFunctionTableA TSSSP_SecurityFunctionTableA = {
695 3, /* dwVersion */
696 nullptr, /* EnumerateSecurityPackages */
697 nullptr, /* QueryCredentialsAttributes */
698 tsssp_AcquireCredentialsHandleA, /* AcquireCredentialsHandle */
699 tsssp_FreeCredentialsHandle, /* FreeCredentialsHandle */
700 nullptr, /* Reserved2 */
701 tsssp_InitializeSecurityContextA, /* InitializeSecurityContext */
702 nullptr, /* AcceptSecurityContext */
703 nullptr, /* CompleteAuthToken */
704 tsssp_DeleteSecurityContext, /* DeleteSecurityContext */
705 nullptr, /* ApplyControlToken */
706 tsssp_QueryContextAttributesA, /* QueryContextAttributes */
707 nullptr, /* ImpersonateSecurityContext */
708 nullptr, /* RevertSecurityContext */
709 nullptr, /* MakeSignature */
710 nullptr, /* VerifySignature */
711 nullptr, /* FreeContextBuffer */
712 nullptr, /* QuerySecurityPackageInfo */
713 nullptr, /* Reserved3 */
714 nullptr, /* Reserved4 */
715 nullptr, /* ExportSecurityContext */
716 nullptr, /* ImportSecurityContext */
717 nullptr, /* AddCredentials */
718 nullptr, /* Reserved8 */
719 nullptr, /* QuerySecurityContextToken */
720 nullptr, /* EncryptMessage */
721 nullptr, /* DecryptMessage */
722 tsssp_SetContextAttributesW, /* SetContextAttributes */
723 nullptr, /* SetCredentialsAttributes */
724};
725
726const SecurityFunctionTableW TSSSP_SecurityFunctionTableW = {
727 3, /* dwVersion */
728 nullptr, /* EnumerateSecurityPackages */
729 nullptr, /* QueryCredentialsAttributes */
730 tsssp_AcquireCredentialsHandleW, /* AcquireCredentialsHandle */
731 tsssp_FreeCredentialsHandle, /* FreeCredentialsHandle */
732 nullptr, /* Reserved2 */
733 tsssp_InitializeSecurityContextW, /* InitializeSecurityContext */
734 nullptr, /* AcceptSecurityContext */
735 nullptr, /* CompleteAuthToken */
736 tsssp_DeleteSecurityContext, /* DeleteSecurityContext */
737 nullptr, /* ApplyControlToken */
738 tsssp_QueryContextAttributesW, /* QueryContextAttributes */
739 nullptr, /* ImpersonateSecurityContext */
740 nullptr, /* RevertSecurityContext */
741 nullptr, /* MakeSignature */
742 nullptr, /* VerifySignature */
743 nullptr, /* FreeContextBuffer */
744 nullptr, /* QuerySecurityPackageInfo */
745 nullptr, /* Reserved3 */
746 nullptr, /* Reserved4 */
747 nullptr, /* ExportSecurityContext */
748 nullptr, /* ImportSecurityContext */
749 nullptr, /* AddCredentials */
750 nullptr, /* Reserved8 */
751 nullptr, /* QuerySecurityContextToken */
752 nullptr, /* EncryptMessage */
753 nullptr, /* DecryptMessage */
754 tsssp_SetContextAttributesW, /* SetContextAttributes */
755 nullptr, /* SetCredentialsAttributes */
756};
757
758const SecPkgInfoA TSSSP_SecPkgInfoA = {
759 0x00010000, /* fCapabilities */
760 1, /* wVersion */
761 0xFFFF, /* wRPCID */
762 0x000090A8, /* cbMaxToken */
763 "TSSSP", /* Name */
764 "WinPR TSSSP (Remote Credential Guard)" /* Comment */
765};
766
767static WCHAR TSSSP_SecPkgInfoW_NameBuffer[128] = WINPR_C_ARRAY_INIT;
768static WCHAR TSSSP_SecPkgInfoW_CommentBuffer[128] = WINPR_C_ARRAY_INIT;
769
770const SecPkgInfoW TSSSP_SecPkgInfoW = {
771 0x00010000, /* fCapabilities */
772 1, /* wVersion */
773 0xFFFF, /* wRPCID */
774 0x000090A8, /* cbMaxToken */
775 TSSSP_SecPkgInfoW_NameBuffer, /* Name */
776 TSSSP_SecPkgInfoW_CommentBuffer /* Comment */
777};
778
779BOOL TSSSP_init(void)
780{
781 InitializeConstWCharFromUtf8(TSSSP_SecPkgInfoA.Name, TSSSP_SecPkgInfoW_NameBuffer,
782 ARRAYSIZE(TSSSP_SecPkgInfoW_NameBuffer));
783 InitializeConstWCharFromUtf8(TSSSP_SecPkgInfoA.Comment, TSSSP_SecPkgInfoW_CommentBuffer,
784 ARRAYSIZE(TSSSP_SecPkgInfoW_CommentBuffer));
785 return TRUE;
786}