FreeRDP
Loading...
Searching...
No Matches
credssp.c
1
19
20#include <winpr/config.h>
21
22#include <winpr/crt.h>
23#include <winpr/assert.h>
24#include <winpr/sspi.h>
25#include <winpr/credssp.h>
26#include <winpr/custom-crypto.h>
27#include <winpr/stream.h>
28
29#ifdef WITH_OPENSSL
30#include <openssl/x509.h>
31#endif
32
33#include "credssp.h"
34
35#include "../sspi.h"
36#include "../../log.h"
37
38#define TAG WINPR_TAG("sspi.CredSSP")
39
40#define CREDSSP_VERSION 6
41#define CREDSSP_NONCE_LENGTH 32
42/* Floor for the inner Negotiate output token: a Kerberos AP-REQ with a PAC can exceed the
43 * Negotiate package's advertised cbMaxToken, so never allocate less than this. */
44#define CREDSSP_MIN_NEGO_TOKEN 0x10000
45
46static WCHAR NEGOTIATE_NAMEW[] = { 'N', 'e', 'g', 'o', 't', 'i', 'a', 't', 'e', '\0' };
47
48/* Negotiate auth package list for Remote Credential Guard: Kerberos (U2U included), no NTLM. */
49static WCHAR CREDSSP_REDIRECT_PACKAGESW[] = { 'k', 'e', 'r', 'b', 'e', 'r', 'o', 's',
50 ',', '!', 'n', 't', 'l', 'm', '\0' };
51
52static PSecurityFunctionTableW credssp_winpr_table(void)
53{
54 return InitSecurityInterfaceExW(SSPI_INTERFACE_WINPR);
55}
56
57/* Wrap plaintext with the Negotiate context: a SECBUFFER_TOKEN signature followed by the
58 * encrypted data, laid out consecutively as the CredSSP TSRequest fields expect. */
59static BOOL credssp_encrypt(CREDSSP_CONTEXT* context, const SecBuffer* plaintext, SecBuffer* out,
60 ULONG sequence)
61{
62 WINPR_ASSERT(context);
63 WINPR_ASSERT(context->nego && context->nego->EncryptMessage);
64
65 const ULONG trailer = context->sizes.cbSecurityTrailer;
66 BYTE* buf = (BYTE*)calloc(1, 1ull * plaintext->cbBuffer + trailer);
67 if (!buf)
68 return FALSE;
69
70 SecBuffer buffers[2] = WINPR_C_ARRAY_INIT;
71 buffers[0].BufferType = SECBUFFER_TOKEN;
72 buffers[0].cbBuffer = trailer;
73 buffers[0].pvBuffer = buf;
74 buffers[1].BufferType = SECBUFFER_DATA;
75 buffers[1].cbBuffer = plaintext->cbBuffer;
76 buffers[1].pvBuffer = buf + trailer;
77 CopyMemory(buffers[1].pvBuffer, plaintext->pvBuffer, plaintext->cbBuffer);
78
79 SecBufferDesc desc = { SECBUFFER_VERSION, 2, buffers };
80 const SECURITY_STATUS status =
81 context->nego->EncryptMessage(&context->negoCtx, 0, &desc, sequence);
82 if (status != SEC_E_OK)
83 {
84 free(buf);
85 return FALSE;
86 }
87
88 if (buffers[0].cbBuffer < trailer)
89 {
90 MoveMemory(buf + buffers[0].cbBuffer, buffers[1].pvBuffer, buffers[1].cbBuffer);
91 context->sizes.cbSecurityTrailer = buffers[0].cbBuffer;
92 }
93
94 sspi_SecBufferFree(out);
95 out->BufferType = SECBUFFER_TOKEN;
96 out->pvBuffer = buf;
97 out->cbBuffer = buffers[0].cbBuffer + buffers[1].cbBuffer;
98 return TRUE;
99}
100
101static BOOL credssp_decrypt(CREDSSP_CONTEXT* context, const SecBuffer* ciphertext, SecBuffer* out,
102 ULONG sequence)
103{
104 WINPR_ASSERT(context);
105 WINPR_ASSERT(context->nego && context->nego->DecryptMessage);
106
107 if (ciphertext->cbBuffer < context->sizes.cbSecurityTrailer)
108 return FALSE;
109
110 BYTE* buf = (BYTE*)malloc(ciphertext->cbBuffer);
111 if (!buf)
112 return FALSE;
113 CopyMemory(buf, ciphertext->pvBuffer, ciphertext->cbBuffer);
114
115 const ULONG trailer = context->sizes.cbSecurityTrailer;
116 SecBuffer buffers[2] = WINPR_C_ARRAY_INIT;
117 buffers[0].BufferType = SECBUFFER_TOKEN;
118 buffers[0].cbBuffer = trailer;
119 buffers[0].pvBuffer = buf;
120 buffers[1].BufferType = SECBUFFER_DATA;
121 buffers[1].cbBuffer = ciphertext->cbBuffer - trailer;
122 buffers[1].pvBuffer = buf + trailer;
123
124 SecBufferDesc desc = { SECBUFFER_VERSION, 2, buffers };
125 ULONG qop = 0;
126 const SECURITY_STATUS status =
127 context->nego->DecryptMessage(&context->negoCtx, &desc, sequence, &qop);
128 if (status != SEC_E_OK)
129 {
130 free(buf);
131 return FALSE;
132 }
133
134 sspi_SecBufferFree(out);
135 if (!sspi_SecBufferAlloc(out, buffers[1].cbBuffer))
136 {
137 free(buf);
138 return FALSE;
139 }
140 CopyMemory(out->pvBuffer, buffers[1].pvBuffer, buffers[1].cbBuffer);
141 free(buf);
142 return TRUE;
143}
144
145CREDSSP_CONTEXT* credssp_ContextNew(void)
146{
147 CREDSSP_CONTEXT* context = (CREDSSP_CONTEXT*)calloc(1, sizeof(CREDSSP_CONTEXT));
148 if (!context)
149 return nullptr;
150 context->version = CREDSSP_VERSION;
151 context->state = CREDSSP_STATE_NEGO;
152 return context;
153}
154
155void credssp_ContextFree(CREDSSP_CONTEXT* context)
156{
157 if (!context)
158 return;
159 if (context->nego && context->haveNegoCtx)
160 (void)context->nego->DeleteSecurityContext(&context->negoCtx);
161 free(context->targetName);
162 sspi_SecBufferFree(&context->publicKey);
163 sspi_SecBufferFree(&context->clientNonce);
164 free(context);
165}
166
167static SECURITY_STATUS SEC_ENTRY credssp_AcquireCredentialsHandleW(
168 WINPR_ATTR_UNUSED SEC_WCHAR* pszPrincipal, WINPR_ATTR_UNUSED SEC_WCHAR* pszPackage,
169 ULONG fCredentialUse, void* pvLogonID, void* pAuthData, SEC_GET_KEY_FN pGetKeyFn,
170 void* pvGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
171{
172 if (fCredentialUse != SECPKG_CRED_OUTBOUND)
173 return SEC_E_UNSUPPORTED_FUNCTION;
174
175 PSecurityFunctionTableW table = credssp_winpr_table();
176 if (!table || !table->AcquireCredentialsHandleW)
177 return SEC_E_SECPKG_NOT_FOUND;
178
179 /* pAuthData is a CREDSSP_CRED_EX; the inner SPNEGO credential is the identity. */
180 SEC_WINNT_AUTH_IDENTITY_W* identity = nullptr;
181 DWORD flags = 0;
182 if (pAuthData)
183 {
184 CREDSSP_CRED_EX* credEx = (CREDSSP_CRED_EX*)pAuthData;
185 flags = credEx->Flags;
186 identity = (SEC_WINNT_AUTH_IDENTITY_W*)credEx->Cred.pSpnegoCred;
187 }
188
189 CREDSSP_CREDENTIALS* creds = (CREDSSP_CREDENTIALS*)calloc(1, sizeof(CREDSSP_CREDENTIALS));
190 if (!creds)
191 return SEC_E_INSUFFICIENT_MEMORY;
192
193 creds->nego = table;
194 creds->flags = flags;
195 if (identity)
196 {
197 creds->identity = *identity;
198 creds->haveIdentity = TRUE;
199 }
200
201 /* Remote Credential Guard is Kerberos-only. Restrict the inner Negotiate to Kerberos so it
202 * does not fall back to NTLM. The restriction travels as an auth package list on an extended
203 * identity, which also carries the explicit credentials when present and otherwise leaves the
204 * user fields empty for ccache SSO. */
205 void* negoAuthData = identity;
206 SEC_WINNT_AUTH_IDENTITY_EXW redirectIdentity = WINPR_C_ARRAY_INIT;
207 if (flags & CREDSSP_FLAG_REDIRECT)
208 {
209 redirectIdentity.Version = SEC_WINNT_AUTH_IDENTITY_VERSION;
210 redirectIdentity.Length = sizeof(redirectIdentity);
211 if (identity)
212 {
213 redirectIdentity.User = identity->User;
214 redirectIdentity.UserLength = identity->UserLength;
215 redirectIdentity.Domain = identity->Domain;
216 redirectIdentity.DomainLength = identity->DomainLength;
217 redirectIdentity.Password = identity->Password;
218 redirectIdentity.PasswordLength = identity->PasswordLength;
219 }
220 redirectIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
221 redirectIdentity.PackageList = (UINT16*)CREDSSP_REDIRECT_PACKAGESW;
222 redirectIdentity.PackageListLength = (UINT32)_wcslen(CREDSSP_REDIRECT_PACKAGESW);
223 negoAuthData = &redirectIdentity;
224 }
225
226 const SECURITY_STATUS status = table->AcquireCredentialsHandleW(
227 pszPrincipal, NEGOTIATE_NAMEW, SECPKG_CRED_OUTBOUND, pvLogonID, negoAuthData, pGetKeyFn,
228 pvGetKeyArgument, &creds->negoCred, ptsExpiry);
229 if (status != SEC_E_OK)
230 {
231 WLog_ERR(TAG, "inner Negotiate AcquireCredentialsHandle failed: 0x%08" PRIx32,
232 (UINT32)status);
233 free(creds);
234 return status;
235 }
236 creds->haveNegoCred = TRUE;
237
238 sspi_SecureHandleSetLowerPointer(phCredential, creds);
239 sspi_SecureHandleSetPackageId(phCredential, SSPI_PACKAGE_CREDSSP);
240 return SEC_E_OK;
241}
242
243static SECURITY_STATUS SEC_ENTRY credssp_FreeCredentialsHandle(PCredHandle phCredential)
244{
245 if (!phCredential)
246 return SEC_E_INVALID_HANDLE;
247
248 CREDSSP_CREDENTIALS* creds =
249 (CREDSSP_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
250 sspi_SecureHandleInvalidate(phCredential);
251 if (!creds)
252 return SEC_E_INVALID_HANDLE;
253
254 if (creds->nego && creds->haveNegoCred)
255 (void)creds->nego->FreeCredentialsHandle(&creds->negoCred);
256 free(creds);
257 return SEC_E_OK;
258}
259
260/* The caller passes the server's leaf certificate as [UINT32 len][DER], matching what the
261 * native tspkg receives. pubKeyAuth is computed over the SubjectPublicKey, so extract the raw
262 * public key (as freerdp_certificate_get_public_key does: X509_get0_pubkey + i2d_PublicKey)
263 * and store that, not the whole certificate. */
264static BOOL credssp_store_cert_public_key(CREDSSP_CONTEXT* context, const BYTE* buffer,
265 size_t bufferLen)
266{
267 wStream sbuffer = WINPR_C_ARRAY_INIT;
268 wStream* s = Stream_StaticConstInit(&sbuffer, buffer, bufferLen);
269 if (!Stream_CheckAndLogRequiredLength(TAG, s, sizeof(UINT32)))
270 return FALSE;
271
272 UINT32 derLen = 0;
273 Stream_Read_UINT32(s, derLen);
274 if ((derLen == 0) || !Stream_CheckAndLogRequiredLength(TAG, s, derLen))
275 return FALSE;
276 const BYTE* der = Stream_ConstPointer(s);
277
278#ifdef WITH_OPENSSL
279 const unsigned char* p = der;
280 X509* x509 = d2i_X509(nullptr, &p, (long)derLen);
281 if (!x509)
282 {
283 WLog_ERR(TAG, "failed to parse the server certificate");
284 return FALSE;
285 }
286
287 BOOL rc = FALSE;
288 EVP_PKEY* pkey = X509_get0_pubkey(x509);
289 int keyLen = pkey ? i2d_PublicKey(pkey, nullptr) : -1;
290 if (keyLen > 0)
291 {
292 sspi_SecBufferFree(&context->publicKey);
293 if (sspi_SecBufferAlloc(&context->publicKey, (ULONG)keyLen))
294 {
295 BYTE* out = context->publicKey.pvBuffer;
296 rc = (i2d_PublicKey(pkey, &out) == keyLen);
297 }
298 }
299 if (!rc)
300 WLog_ERR(TAG, "failed to extract the server public key");
301 X509_free(x509);
302 return rc;
303#else
304 WLog_ERR(TAG, "CredSSP public-key extraction requires OpenSSL");
305 return FALSE;
306#endif
307}
308
309/* Grab the server certificate from the READONLY|TOKEN input buffer, if present, and keep its
310 * SubjectPublicKey for the pubKeyAuth hash. */
311static BOOL credssp_store_public_key(CREDSSP_CONTEXT* context, PSecBufferDesc pInput)
312{
313 if (!pInput)
314 return TRUE;
315 for (ULONG i = 0; i < pInput->cBuffers; i++)
316 {
317 const SecBuffer* b = &pInput->pBuffers[i];
318 if (((b->BufferType & ~SECBUFFER_ATTRMASK) == SECBUFFER_TOKEN) &&
319 (b->BufferType & SECBUFFER_READONLY) && b->pvBuffer && (b->cbBuffer > 0))
320 return credssp_store_cert_public_key(context, b->pvBuffer, b->cbBuffer);
321 }
322 return TRUE;
323}
324
325static const SecBuffer* credssp_token_input(PSecBufferDesc pInput)
326{
327 if (!pInput)
328 return nullptr;
329 for (ULONG i = 0; i < pInput->cBuffers; i++)
330 {
331 const SecBuffer* b = &pInput->pBuffers[i];
332 if ((b->BufferType == SECBUFFER_TOKEN) && b->pvBuffer && (b->cbBuffer > 0))
333 return b;
334 }
335 return nullptr;
336}
337
338/* Hand the finished TSRequest back to the caller through a WinPR context buffer. */
339static SECURITY_STATUS credssp_set_output(const SecBuffer* request, PSecBufferDesc pOutput)
340{
341 if (!pOutput || (pOutput->cBuffers < 1))
342 return SEC_E_INVALID_TOKEN;
343
344 SecBuffer* out = &pOutput->pBuffers[0];
345 void* buf = sspi_ContextBufferAlloc(InitializeSecurityContextIndex, request->cbBuffer);
346 if (!buf)
347 return SEC_E_INSUFFICIENT_MEMORY;
348 CopyMemory(buf, request->pvBuffer, request->cbBuffer);
349 out->BufferType = SECBUFFER_TOKEN;
350 out->pvBuffer = buf;
351 out->cbBuffer = request->cbBuffer;
352 return SEC_E_OK;
353}
354
355static SECURITY_STATUS SEC_ENTRY credssp_InitializeSecurityContextW(
356 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq,
357 ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
358 PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
359{
360 CREDSSP_CREDENTIALS* creds =
361 (CREDSSP_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
362 if (!creds)
363 return SEC_E_INVALID_HANDLE;
364
365 CREDSSP_CONTEXT* context =
366 phContext ? (CREDSSP_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext) : nullptr;
367 const BOOL fresh = (context == nullptr);
368 if (fresh)
369 {
370 context = credssp_ContextNew();
371 if (!context)
372 return SEC_E_INSUFFICIENT_MEMORY;
373 context->nego = creds->nego;
374 context->flags = creds->flags;
375 context->identity = creds->identity;
376 context->haveIdentity = creds->haveIdentity;
377 /* The inner Negotiate needs the target SPN to request the service ticket. */
378 if (pszTargetName)
379 {
380 context->targetName = _wcsdup(pszTargetName);
381 if (!context->targetName)
382 {
383 credssp_ContextFree(context);
384 return SEC_E_INSUFFICIENT_MEMORY;
385 }
386 }
387 }
388
389 if (!credssp_store_public_key(context, pInput))
390 goto internal_error;
391
392 SECURITY_STATUS rc = SEC_E_INTERNAL_ERROR;
393 WINPR_TSREQUEST in = WINPR_C_ARRAY_INIT;
394 WINPR_TSREQUEST out = WINPR_C_ARRAY_INIT;
395 SecBuffer request = WINPR_C_ARRAY_INIT;
396
397 const SecBuffer* inToken = credssp_token_input(pInput);
398 if (inToken)
399 {
400 wStream ss = WINPR_C_ARRAY_INIT;
401 wStream* s = Stream_StaticConstInit(&ss, inToken->pvBuffer, inToken->cbBuffer);
402 if (!winpr_tsrequest_decode(s, &in))
403 {
404 rc = SEC_E_INVALID_TOKEN;
405 goto cleanup;
406 }
407 if (context->peerVersion == 0)
408 context->peerVersion = in.version;
409 }
410
411 out.version = context->version;
412
413 if (context->state == CREDSSP_STATE_NEGO)
414 {
415 /* drive the inner Negotiate exchange. The Kerberos/Negotiate ISC does not honour
416 * ISC_REQ_ALLOCATE_MEMORY for its output token: the caller must hand it a buffer large
417 * enough to hold the result (the same way credssp_auth does in nla.c). Size it from the
418 * package's cbMaxToken, with a floor that accommodates a Kerberos AP-REQ carrying a PAC. */
419 PSecPkgInfoW negoInfo = nullptr;
420 if (context->nego->QuerySecurityPackageInfoW(NEGOTIATE_NAMEW, &negoInfo) != SEC_E_OK)
421 goto internal_error;
422 ULONG maxToken = negoInfo->cbMaxToken;
423 (void)context->nego->FreeContextBuffer(negoInfo);
424 if (maxToken < CREDSSP_MIN_NEGO_TOKEN)
425 maxToken = CREDSSP_MIN_NEGO_TOKEN;
426
427 /* The decoder leaves BufferType unset; the inner Negotiate locates its input by
428 * SECBUFFER_TOKEN, so tag it explicitly. */
429 SecBuffer negoIn = in.negoToken;
430 negoIn.BufferType = SECBUFFER_TOKEN;
431 SecBufferDesc negoInDesc = { SECBUFFER_VERSION, 1, &negoIn };
432 SecBuffer negoOutBuf = { 0, SECBUFFER_TOKEN, nullptr };
433 if (!sspi_SecBufferAlloc(&negoOutBuf, maxToken))
434 goto internal_error;
435 negoOutBuf.BufferType = SECBUFFER_TOKEN;
436 SecBufferDesc negoOutDesc = { SECBUFFER_VERSION, 1, &negoOutBuf };
437
438 CtxtHandle* negoInCtx = context->haveNegoCtx ? &context->negoCtx : nullptr;
439 const SECURITY_STATUS ns = context->nego->InitializeSecurityContextW(
440 &creds->negoCred, negoInCtx, context->targetName, fContextReq, Reserved1, TargetDataRep,
441 inToken ? &negoInDesc : nullptr, Reserved2, &context->negoCtx, &negoOutDesc,
442 pfContextAttr, ptsExpiry);
443 context->haveNegoCtx = TRUE;
444
445 if ((ns != SEC_E_OK) && (ns != SEC_I_CONTINUE_NEEDED))
446 {
447 WLog_ERR(TAG, "inner Negotiate ISC failed: 0x%08" PRIx32, (UINT32)ns);
448 sspi_SecBufferFree(&negoOutBuf);
449 rc = ns;
450 goto cleanup;
451 }
452
453 if (negoOutBuf.cbBuffer > 0)
454 {
455 if (!sspi_SecBufferAlloc(&out.negoToken, negoOutBuf.cbBuffer))
456 {
457 sspi_SecBufferFree(&negoOutBuf);
458 goto internal_error_cleanup;
459 }
460 CopyMemory(out.negoToken.pvBuffer, negoOutBuf.pvBuffer, negoOutBuf.cbBuffer);
461 }
462 sspi_SecBufferFree(&negoOutBuf);
463
464 if (ns == SEC_E_OK)
465 {
466 /* Negotiate is done: capture sizes, nonce, and send pubKeyAuth with this message. */
467 if (context->nego->QueryContextAttributesW(&context->negoCtx, SECPKG_ATTR_SIZES,
468 &context->sizes) != SEC_E_OK)
469 goto internal_error_cleanup;
470 context->haveSizes = TRUE;
471
472 if (!sspi_SecBufferAlloc(&context->clientNonce, CREDSSP_NONCE_LENGTH) ||
473 (winpr_RAND(context->clientNonce.pvBuffer, CREDSSP_NONCE_LENGTH) != 0))
474 goto internal_error_cleanup;
475
476 SecBuffer hash = WINPR_C_ARRAY_INIT;
477 if (!winpr_credssp_public_key_hash(FALSE, &context->clientNonce, &context->publicKey,
478 &hash))
479 goto internal_error_cleanup;
480 const BOOL enc =
481 credssp_encrypt(context, &hash, &out.pubKeyAuth, context->sendSeqNum++);
482 sspi_SecBufferFree(&hash);
483 if (!enc)
484 goto internal_error_cleanup;
485
486 if (!sspi_SecBufferAlloc(&out.clientNonce, CREDSSP_NONCE_LENGTH))
487 goto internal_error_cleanup;
488 CopyMemory(out.clientNonce.pvBuffer, context->clientNonce.pvBuffer,
489 CREDSSP_NONCE_LENGTH);
490
491 context->state = CREDSSP_STATE_PUBKEY;
492 }
493
494 if (!winpr_tsrequest_encode(&out, context->peerVersion, FALSE, &request))
495 goto internal_error_cleanup;
496 rc = credssp_set_output(&request, pOutput);
497 if (rc == SEC_E_OK)
498 rc = SEC_I_CONTINUE_NEEDED;
499 goto finish;
500 }
501
502 if (context->state == CREDSSP_STATE_PUBKEY)
503 {
504 /* verify the server's pubKeyAuth, then send the encrypted TSCredentials */
505 if (in.pubKeyAuth.cbBuffer == 0)
506 {
507 rc = SEC_E_INVALID_TOKEN;
508 goto cleanup;
509 }
510
511 SecBuffer serverHash = WINPR_C_ARRAY_INIT;
512 SecBuffer expected = WINPR_C_ARRAY_INIT;
513 if (!credssp_decrypt(context, &in.pubKeyAuth, &serverHash, context->recvSeqNum++))
514 {
515 rc = SEC_E_MESSAGE_ALTERED;
516 goto cleanup;
517 }
518 const BOOL hok = winpr_credssp_public_key_hash(TRUE, &context->clientNonce,
519 &context->publicKey, &expected) &&
520 (serverHash.cbBuffer == expected.cbBuffer) &&
521 (memcmp(serverHash.pvBuffer, expected.pvBuffer, expected.cbBuffer) == 0);
522 sspi_SecBufferFree(&serverHash);
523 sspi_SecBufferFree(&expected);
524 if (!hok)
525 {
526 WLog_ERR(TAG, "server pubKeyAuth verification failed");
527 rc = SEC_E_MESSAGE_ALTERED;
528 goto cleanup;
529 }
530
531 /* build and encrypt the delegated credentials */
532 SecBuffer tsCreds = WINPR_C_ARRAY_INIT;
533 BOOL built = FALSE;
534 if (context->flags & CREDSSP_FLAG_REDIRECT)
535 {
536 KERB_TICKET_LOGON ticket = WINPR_C_ARRAY_INIT;
537 if (context->nego->QueryContextAttributesW(
538 &context->negoCtx, SECPKG_CRED_ATTR_TICKET_LOGON, &ticket) == SEC_E_OK)
539 {
540 built = winpr_tscredentials_encode_remoteguard_kerb(&ticket, &tsCreds);
541 free(ticket.ServiceTicket);
542 free(ticket.TicketGrantingTicket);
543 }
544 }
545 else
546 {
547 built = winpr_tscredentials_encode_password(
548 context->haveIdentity ? &context->identity : nullptr, &tsCreds);
549 }
550 if (!built)
551 goto internal_error_cleanup;
552
553 const BOOL enc = credssp_encrypt(context, &tsCreds, &out.authInfo, context->sendSeqNum++);
554 sspi_SecBufferFree(&tsCreds);
555 if (!enc)
556 goto internal_error_cleanup;
557
558 if (!winpr_tsrequest_encode(&out, context->peerVersion, FALSE, &request))
559 goto internal_error_cleanup;
560 rc = credssp_set_output(&request, pOutput);
561 context->state = CREDSSP_STATE_DONE;
562 goto finish;
563 }
564
565 rc = SEC_E_OUT_OF_SEQUENCE;
566 goto cleanup;
567
568internal_error_cleanup:
569 rc = SEC_E_INTERNAL_ERROR;
570 goto cleanup;
571
572internal_error:
573 if (fresh)
574 credssp_ContextFree(context);
575 return SEC_E_INTERNAL_ERROR;
576
577finish:
578 sspi_SecureHandleSetLowerPointer(phNewContext, context);
579 sspi_SecureHandleSetPackageId(phNewContext, SSPI_PACKAGE_CREDSSP);
580
581cleanup:
582 winpr_tsrequest_free_contents(&in);
583 winpr_tsrequest_free_contents(&out);
584 sspi_SecBufferFree(&request);
585 if ((rc != SEC_E_OK) && (rc != SEC_I_CONTINUE_NEEDED) && fresh)
586 {
587 sspi_SecureHandleInvalidate(phNewContext);
588 credssp_ContextFree(context);
589 }
590 return rc;
591}
592
593static SECURITY_STATUS SEC_ENTRY credssp_DeleteSecurityContext(PCtxtHandle phContext)
594{
595 if (!phContext)
596 return SEC_E_INVALID_HANDLE;
597 CREDSSP_CONTEXT* context = (CREDSSP_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
598 sspi_SecureHandleInvalidate(phContext);
599 if (!context)
600 return SEC_E_INVALID_HANDLE;
601 credssp_ContextFree(context);
602 return SEC_E_OK;
603}
604
605static SECURITY_STATUS SEC_ENTRY credssp_QueryContextAttributesW(PCtxtHandle phContext,
606 ULONG ulAttribute, void* pBuffer)
607{
608 if (!phContext || !pBuffer)
609 return SEC_E_INVALID_HANDLE;
610
611 CREDSSP_CONTEXT* context = (CREDSSP_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
612 if (!context)
613 return SEC_E_INVALID_HANDLE;
614
615 if (ulAttribute == SECPKG_ATTR_SIZES)
616 {
617 SecPkgContext_Sizes* sizes = (SecPkgContext_Sizes*)pBuffer;
618 *sizes = context->sizes;
619 return SEC_E_OK;
620 }
621
622 if (context->nego && context->nego->QueryContextAttributesW)
623 return context->nego->QueryContextAttributesW(&context->negoCtx, ulAttribute, pBuffer);
624 return SEC_E_UNSUPPORTED_FUNCTION;
625}
626
627/* The message sequence number continues the one established during authentication: the inner
628 * Kerberos context expects remote_seq + MessageSeqNo, so post-authentication traffic (the RDPEAR
629 * redirected calls) must keep counting from where pubKeyAuth/TSCredentials left off. The context
630 * owns that counter, so ignore the caller's MessageSeqNo and advance sendSeqNum/recvSeqNum here. */
631static SECURITY_STATUS SEC_ENTRY credssp_EncryptMessage(PCtxtHandle phContext, ULONG fQOP,
632 PSecBufferDesc pMessage,
633 WINPR_ATTR_UNUSED ULONG MessageSeqNo)
634{
635 CREDSSP_CONTEXT* context = (CREDSSP_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
636 if (!context || !context->nego)
637 return SEC_E_INVALID_HANDLE;
638 return context->nego->EncryptMessage(&context->negoCtx, fQOP, pMessage, context->sendSeqNum++);
639}
640
641static SECURITY_STATUS SEC_ENTRY credssp_DecryptMessage(PCtxtHandle phContext,
642 PSecBufferDesc pMessage,
643 WINPR_ATTR_UNUSED ULONG MessageSeqNo,
644 PULONG pfQOP)
645{
646 CREDSSP_CONTEXT* context = (CREDSSP_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
647 if (!context || !context->nego)
648 return SEC_E_INVALID_HANDLE;
649 return context->nego->DecryptMessage(&context->negoCtx, pMessage, context->recvSeqNum++, pfQOP);
650}
651
652const SecurityFunctionTableA CREDSSP_SecurityFunctionTableA = {
653 3, /* dwVersion */
654 nullptr, /* EnumerateSecurityPackages */
655 nullptr, /* QueryCredentialsAttributes */
656 nullptr, /* AcquireCredentialsHandle */
657 credssp_FreeCredentialsHandle, /* FreeCredentialsHandle */
658 nullptr, /* Reserved2 */
659 nullptr, /* InitializeSecurityContext */
660 nullptr, /* AcceptSecurityContext */
661 nullptr, /* CompleteAuthToken */
662 credssp_DeleteSecurityContext, /* DeleteSecurityContext */
663 nullptr, /* ApplyControlToken */
664 nullptr, /* QueryContextAttributes */
665 nullptr, /* ImpersonateSecurityContext */
666 nullptr, /* RevertSecurityContext */
667 nullptr, /* MakeSignature */
668 nullptr, /* VerifySignature */
669 nullptr, /* FreeContextBuffer */
670 nullptr, /* QuerySecurityPackageInfo */
671 nullptr, /* Reserved3 */
672 nullptr, /* Reserved4 */
673 nullptr, /* ExportSecurityContext */
674 nullptr, /* ImportSecurityContext */
675 nullptr, /* AddCredentials */
676 nullptr, /* Reserved8 */
677 nullptr, /* QuerySecurityContextToken */
678 credssp_EncryptMessage, /* EncryptMessage */
679 credssp_DecryptMessage, /* DecryptMessage */
680 nullptr, /* SetContextAttributes */
681 nullptr, /* SetCredentialsAttributes */
682};
683
684const SecurityFunctionTableW CREDSSP_SecurityFunctionTableW = {
685 3, /* dwVersion */
686 nullptr, /* EnumerateSecurityPackages */
687 nullptr, /* QueryCredentialsAttributes */
688 credssp_AcquireCredentialsHandleW, /* AcquireCredentialsHandle */
689 credssp_FreeCredentialsHandle, /* FreeCredentialsHandle */
690 nullptr, /* Reserved2 */
691 credssp_InitializeSecurityContextW, /* InitializeSecurityContext */
692 nullptr, /* AcceptSecurityContext */
693 nullptr, /* CompleteAuthToken */
694 credssp_DeleteSecurityContext, /* DeleteSecurityContext */
695 nullptr, /* ApplyControlToken */
696 credssp_QueryContextAttributesW, /* QueryContextAttributes */
697 nullptr, /* ImpersonateSecurityContext */
698 nullptr, /* RevertSecurityContext */
699 nullptr, /* MakeSignature */
700 nullptr, /* VerifySignature */
701 nullptr, /* FreeContextBuffer */
702 nullptr, /* QuerySecurityPackageInfo */
703 nullptr, /* Reserved3 */
704 nullptr, /* Reserved4 */
705 nullptr, /* ExportSecurityContext */
706 nullptr, /* ImportSecurityContext */
707 nullptr, /* AddCredentials */
708 nullptr, /* Reserved8 */
709 nullptr, /* QuerySecurityContextToken */
710 credssp_EncryptMessage, /* EncryptMessage */
711 credssp_DecryptMessage, /* DecryptMessage */
712 nullptr, /* SetContextAttributes */
713 nullptr, /* SetCredentialsAttributes */
714};
715
716const SecPkgInfoA CREDSSP_SecPkgInfoA = {
717 0x000110733, /* fCapabilities */
718 1, /* wVersion */
719 0xFFFF, /* wRPCID */
720 0x000090A8, /* cbMaxToken */
721 "CREDSSP", /* Name */
722 "Microsoft CredSSP Security Provider" /* Comment */
723};
724
725static WCHAR CREDSSP_SecPkgInfoW_NameBuffer[128] = WINPR_C_ARRAY_INIT;
726static WCHAR CREDSSP_SecPkgInfoW_CommentBuffer[128] = WINPR_C_ARRAY_INIT;
727
728const SecPkgInfoW CREDSSP_SecPkgInfoW = {
729 0x000110733, /* fCapabilities */
730 1, /* wVersion */
731 0xFFFF, /* wRPCID */
732 0x000090A8, /* cbMaxToken */
733 CREDSSP_SecPkgInfoW_NameBuffer, /* Name */
734 CREDSSP_SecPkgInfoW_CommentBuffer /* Comment */
735};
736
737BOOL CREDSSP_init(void)
738{
739 InitializeConstWCharFromUtf8(CREDSSP_SecPkgInfoA.Name, CREDSSP_SecPkgInfoW_NameBuffer,
740 ARRAYSIZE(CREDSSP_SecPkgInfoW_NameBuffer));
741 InitializeConstWCharFromUtf8(CREDSSP_SecPkgInfoA.Comment, CREDSSP_SecPkgInfoW_CommentBuffer,
742 ARRAYSIZE(CREDSSP_SecPkgInfoW_CommentBuffer));
743 return TRUE;
744}
[MS-CSSP] 2.2.1 TSRequest fields the CredSSP exchange carries.