20#include <winpr/config.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>
30#include <openssl/x509.h>
38#define TAG WINPR_TAG("sspi.CredSSP")
40#define CREDSSP_VERSION 6
41#define CREDSSP_NONCE_LENGTH 32
44#define CREDSSP_MIN_NEGO_TOKEN 0x10000
46static WCHAR NEGOTIATE_NAMEW[] = {
'N',
'e',
'g',
'o',
't',
'i',
'a',
't',
'e',
'\0' };
49static WCHAR CREDSSP_REDIRECT_PACKAGESW[] = {
'k',
'e',
'r',
'b',
'e',
'r',
'o',
's',
50 ',',
'!',
'n',
't',
'l',
'm',
'\0' };
52static PSecurityFunctionTableW credssp_winpr_table(
void)
54 return InitSecurityInterfaceExW(SSPI_INTERFACE_WINPR);
62 WINPR_ASSERT(context);
63 WINPR_ASSERT(context->nego && context->nego->EncryptMessage);
65 const ULONG trailer = context->sizes.cbSecurityTrailer;
66 BYTE* buf = (BYTE*)calloc(1, 1ull * plaintext->cbBuffer + trailer);
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);
80 const SECURITY_STATUS status =
81 context->nego->EncryptMessage(&context->negoCtx, 0, &desc, sequence);
82 if (status != SEC_E_OK)
88 if (buffers[0].cbBuffer < trailer)
90 MoveMemory(buf + buffers[0].cbBuffer, buffers[1].pvBuffer, buffers[1].cbBuffer);
91 context->sizes.cbSecurityTrailer = buffers[0].cbBuffer;
94 sspi_SecBufferFree(out);
95 out->BufferType = SECBUFFER_TOKEN;
97 out->cbBuffer = buffers[0].cbBuffer + buffers[1].cbBuffer;
104 WINPR_ASSERT(context);
105 WINPR_ASSERT(context->nego && context->nego->DecryptMessage);
107 if (ciphertext->cbBuffer < context->sizes.cbSecurityTrailer)
110 BYTE* buf = (BYTE*)malloc(ciphertext->cbBuffer);
113 CopyMemory(buf, ciphertext->pvBuffer, ciphertext->cbBuffer);
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;
126 const SECURITY_STATUS status =
127 context->nego->DecryptMessage(&context->negoCtx, &desc, sequence, &qop);
128 if (status != SEC_E_OK)
134 sspi_SecBufferFree(out);
135 if (!sspi_SecBufferAlloc(out, buffers[1].cbBuffer))
140 CopyMemory(out->pvBuffer, buffers[1].pvBuffer, buffers[1].cbBuffer);
150 context->version = CREDSSP_VERSION;
151 context->state = CREDSSP_STATE_NEGO;
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);
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)
172 if (fCredentialUse != SECPKG_CRED_OUTBOUND)
173 return SEC_E_UNSUPPORTED_FUNCTION;
175 PSecurityFunctionTableW table = credssp_winpr_table();
176 if (!table || !table->AcquireCredentialsHandleW)
177 return SEC_E_SECPKG_NOT_FOUND;
185 flags = credEx->Flags;
191 return SEC_E_INSUFFICIENT_MEMORY;
194 creds->flags = flags;
197 creds->identity = *identity;
198 creds->haveIdentity = TRUE;
205 void* negoAuthData = identity;
207 if (flags & CREDSSP_FLAG_REDIRECT)
209 redirectIdentity.Version = SEC_WINNT_AUTH_IDENTITY_VERSION;
210 redirectIdentity.Length =
sizeof(redirectIdentity);
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;
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;
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)
231 WLog_ERR(TAG,
"inner Negotiate AcquireCredentialsHandle failed: 0x%08" PRIx32,
236 creds->haveNegoCred = TRUE;
238 sspi_SecureHandleSetLowerPointer(phCredential, creds);
239 sspi_SecureHandleSetPackageId(phCredential, SSPI_PACKAGE_CREDSSP);
243static SECURITY_STATUS SEC_ENTRY credssp_FreeCredentialsHandle(PCredHandle phCredential)
246 return SEC_E_INVALID_HANDLE;
250 sspi_SecureHandleInvalidate(phCredential);
252 return SEC_E_INVALID_HANDLE;
254 if (creds->nego && creds->haveNegoCred)
255 (void)creds->nego->FreeCredentialsHandle(&creds->negoCred);
264static BOOL credssp_store_cert_public_key(
CREDSSP_CONTEXT* context,
const BYTE* buffer,
267 wStream sbuffer = WINPR_C_ARRAY_INIT;
268 wStream* s = Stream_StaticConstInit(&sbuffer, buffer, bufferLen);
269 if (!Stream_CheckAndLogRequiredLength(TAG, s,
sizeof(UINT32)))
273 Stream_Read_UINT32(s, derLen);
274 if ((derLen == 0) || !Stream_CheckAndLogRequiredLength(TAG, s, derLen))
276 const BYTE* der = Stream_ConstPointer(s);
279 const unsigned char* p = der;
280 X509* x509 = d2i_X509(
nullptr, &p, (
long)derLen);
283 WLog_ERR(TAG,
"failed to parse the server certificate");
288 EVP_PKEY* pkey = X509_get0_pubkey(x509);
289 int keyLen = pkey ? i2d_PublicKey(pkey,
nullptr) : -1;
292 sspi_SecBufferFree(&context->publicKey);
293 if (sspi_SecBufferAlloc(&context->publicKey, (ULONG)keyLen))
295 BYTE* out = context->publicKey.pvBuffer;
296 rc = (i2d_PublicKey(pkey, &out) == keyLen);
300 WLog_ERR(TAG,
"failed to extract the server public key");
304 WLog_ERR(TAG,
"CredSSP public-key extraction requires OpenSSL");
311static BOOL credssp_store_public_key(
CREDSSP_CONTEXT* context, PSecBufferDesc pInput)
315 for (ULONG i = 0; i < pInput->cBuffers; i++)
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);
325static const SecBuffer* credssp_token_input(PSecBufferDesc pInput)
329 for (ULONG i = 0; i < pInput->cBuffers; i++)
331 const SecBuffer* b = &pInput->pBuffers[i];
332 if ((b->BufferType == SECBUFFER_TOKEN) && b->pvBuffer && (b->cbBuffer > 0))
339static SECURITY_STATUS credssp_set_output(
const SecBuffer* request, PSecBufferDesc pOutput)
341 if (!pOutput || (pOutput->cBuffers < 1))
342 return SEC_E_INVALID_TOKEN;
345 void* buf = sspi_ContextBufferAlloc(InitializeSecurityContextIndex, request->cbBuffer);
347 return SEC_E_INSUFFICIENT_MEMORY;
348 CopyMemory(buf, request->pvBuffer, request->cbBuffer);
349 out->BufferType = SECBUFFER_TOKEN;
351 out->cbBuffer = request->cbBuffer;
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)
363 return SEC_E_INVALID_HANDLE;
366 phContext ? (
CREDSSP_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext) : nullptr;
367 const BOOL fresh = (context ==
nullptr);
370 context = credssp_ContextNew();
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;
380 context->targetName = _wcsdup(pszTargetName);
381 if (!context->targetName)
383 credssp_ContextFree(context);
384 return SEC_E_INSUFFICIENT_MEMORY;
389 if (!credssp_store_public_key(context, pInput))
392 SECURITY_STATUS rc = SEC_E_INTERNAL_ERROR;
397 const SecBuffer* inToken = credssp_token_input(pInput);
400 wStream ss = WINPR_C_ARRAY_INIT;
401 wStream* s = Stream_StaticConstInit(&ss, inToken->pvBuffer, inToken->cbBuffer);
402 if (!winpr_tsrequest_decode(s, &in))
404 rc = SEC_E_INVALID_TOKEN;
407 if (context->peerVersion == 0)
408 context->peerVersion = in.version;
411 out.version = context->version;
413 if (context->state == CREDSSP_STATE_NEGO)
419 PSecPkgInfoW negoInfo =
nullptr;
420 if (context->nego->QuerySecurityPackageInfoW(NEGOTIATE_NAMEW, &negoInfo) != SEC_E_OK)
422 ULONG maxToken = negoInfo->cbMaxToken;
423 (void)context->nego->FreeContextBuffer(negoInfo);
424 if (maxToken < CREDSSP_MIN_NEGO_TOKEN)
425 maxToken = CREDSSP_MIN_NEGO_TOKEN;
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))
435 negoOutBuf.BufferType = SECBUFFER_TOKEN;
436 SecBufferDesc negoOutDesc = { SECBUFFER_VERSION, 1, &negoOutBuf };
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;
445 if ((ns != SEC_E_OK) && (ns != SEC_I_CONTINUE_NEEDED))
447 WLog_ERR(TAG,
"inner Negotiate ISC failed: 0x%08" PRIx32, (UINT32)ns);
448 sspi_SecBufferFree(&negoOutBuf);
453 if (negoOutBuf.cbBuffer > 0)
455 if (!sspi_SecBufferAlloc(&out.negoToken, negoOutBuf.cbBuffer))
457 sspi_SecBufferFree(&negoOutBuf);
458 goto internal_error_cleanup;
460 CopyMemory(out.negoToken.pvBuffer, negoOutBuf.pvBuffer, negoOutBuf.cbBuffer);
462 sspi_SecBufferFree(&negoOutBuf);
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;
472 if (!sspi_SecBufferAlloc(&context->clientNonce, CREDSSP_NONCE_LENGTH) ||
473 (winpr_RAND(context->clientNonce.pvBuffer, CREDSSP_NONCE_LENGTH) != 0))
474 goto internal_error_cleanup;
477 if (!winpr_credssp_public_key_hash(FALSE, &context->clientNonce, &context->publicKey,
479 goto internal_error_cleanup;
481 credssp_encrypt(context, &hash, &out.pubKeyAuth, context->sendSeqNum++);
482 sspi_SecBufferFree(&hash);
484 goto internal_error_cleanup;
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);
491 context->state = CREDSSP_STATE_PUBKEY;
494 if (!winpr_tsrequest_encode(&out, context->peerVersion, FALSE, &request))
495 goto internal_error_cleanup;
496 rc = credssp_set_output(&request, pOutput);
498 rc = SEC_I_CONTINUE_NEEDED;
502 if (context->state == CREDSSP_STATE_PUBKEY)
505 if (in.pubKeyAuth.cbBuffer == 0)
507 rc = SEC_E_INVALID_TOKEN;
511 SecBuffer serverHash = WINPR_C_ARRAY_INIT;
513 if (!credssp_decrypt(context, &in.pubKeyAuth, &serverHash, context->recvSeqNum++))
515 rc = SEC_E_MESSAGE_ALTERED;
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);
526 WLog_ERR(TAG,
"server pubKeyAuth verification failed");
527 rc = SEC_E_MESSAGE_ALTERED;
534 if (context->flags & CREDSSP_FLAG_REDIRECT)
537 if (context->nego->QueryContextAttributesW(
538 &context->negoCtx, SECPKG_CRED_ATTR_TICKET_LOGON, &ticket) == SEC_E_OK)
540 built = winpr_tscredentials_encode_remoteguard_kerb(&ticket, &tsCreds);
541 free(ticket.ServiceTicket);
542 free(ticket.TicketGrantingTicket);
547 built = winpr_tscredentials_encode_password(
548 context->haveIdentity ? &context->identity :
nullptr, &tsCreds);
551 goto internal_error_cleanup;
553 const BOOL enc = credssp_encrypt(context, &tsCreds, &out.authInfo, context->sendSeqNum++);
554 sspi_SecBufferFree(&tsCreds);
556 goto internal_error_cleanup;
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;
565 rc = SEC_E_OUT_OF_SEQUENCE;
568internal_error_cleanup:
569 rc = SEC_E_INTERNAL_ERROR;
574 credssp_ContextFree(context);
575 return SEC_E_INTERNAL_ERROR;
578 sspi_SecureHandleSetLowerPointer(phNewContext, context);
579 sspi_SecureHandleSetPackageId(phNewContext, SSPI_PACKAGE_CREDSSP);
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)
587 sspi_SecureHandleInvalidate(phNewContext);
588 credssp_ContextFree(context);
593static SECURITY_STATUS SEC_ENTRY credssp_DeleteSecurityContext(PCtxtHandle phContext)
596 return SEC_E_INVALID_HANDLE;
598 sspi_SecureHandleInvalidate(phContext);
600 return SEC_E_INVALID_HANDLE;
601 credssp_ContextFree(context);
605static SECURITY_STATUS SEC_ENTRY credssp_QueryContextAttributesW(PCtxtHandle phContext,
606 ULONG ulAttribute,
void* pBuffer)
608 if (!phContext || !pBuffer)
609 return SEC_E_INVALID_HANDLE;
613 return SEC_E_INVALID_HANDLE;
615 if (ulAttribute == SECPKG_ATTR_SIZES)
618 *sizes = context->sizes;
622 if (context->nego && context->nego->QueryContextAttributesW)
623 return context->nego->QueryContextAttributesW(&context->negoCtx, ulAttribute, pBuffer);
624 return SEC_E_UNSUPPORTED_FUNCTION;
631static SECURITY_STATUS SEC_ENTRY credssp_EncryptMessage(PCtxtHandle phContext, ULONG fQOP,
632 PSecBufferDesc pMessage,
633 WINPR_ATTR_UNUSED ULONG MessageSeqNo)
636 if (!context || !context->nego)
637 return SEC_E_INVALID_HANDLE;
638 return context->nego->EncryptMessage(&context->negoCtx, fQOP, pMessage, context->sendSeqNum++);
641static SECURITY_STATUS SEC_ENTRY credssp_DecryptMessage(PCtxtHandle phContext,
642 PSecBufferDesc pMessage,
643 WINPR_ATTR_UNUSED ULONG MessageSeqNo,
647 if (!context || !context->nego)
648 return SEC_E_INVALID_HANDLE;
649 return context->nego->DecryptMessage(&context->negoCtx, pMessage, context->recvSeqNum++, pfQOP);
657 credssp_FreeCredentialsHandle,
662 credssp_DeleteSecurityContext,
678 credssp_EncryptMessage,
679 credssp_DecryptMessage,
688 credssp_AcquireCredentialsHandleW,
689 credssp_FreeCredentialsHandle,
691 credssp_InitializeSecurityContextW,
694 credssp_DeleteSecurityContext,
696 credssp_QueryContextAttributesW,
710 credssp_EncryptMessage,
711 credssp_DecryptMessage,
722 "Microsoft CredSSP Security Provider"
725static WCHAR CREDSSP_SecPkgInfoW_NameBuffer[128] = WINPR_C_ARRAY_INIT;
726static WCHAR CREDSSP_SecPkgInfoW_CommentBuffer[128] = WINPR_C_ARRAY_INIT;
733 CREDSSP_SecPkgInfoW_NameBuffer,
734 CREDSSP_SecPkgInfoW_CommentBuffer
737BOOL CREDSSP_init(
void)
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));
[MS-CSSP] 2.2.1 TSRequest fields the CredSSP exchange carries.