2012-10-01 04:15:42 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/sspi.h>
|
|
|
|
#include <winpr/winpr.h>
|
|
|
|
|
|
|
|
static const char* test_User = "User";
|
|
|
|
static const char* test_Domain = "Domain";
|
|
|
|
static const char* test_Password = "Password";
|
|
|
|
|
|
|
|
int TestInitializeSecurityContext(int argc, char* argv[])
|
|
|
|
{
|
2017-11-15 11:11:12 +03:00
|
|
|
int rc = -1;
|
2012-10-01 04:15:42 +04:00
|
|
|
UINT32 cbMaxLen;
|
|
|
|
UINT32 fContextReq;
|
2017-11-15 11:11:12 +03:00
|
|
|
void* output_buffer = NULL;
|
2012-10-01 04:15:42 +04:00
|
|
|
CtxtHandle context;
|
|
|
|
ULONG pfContextAttr;
|
|
|
|
SECURITY_STATUS status;
|
2017-12-21 11:34:35 +03:00
|
|
|
CredHandle credentials = { 0 };
|
2012-10-01 04:15:42 +04:00
|
|
|
TimeStamp expiration;
|
|
|
|
PSecPkgInfo pPackageInfo;
|
2017-11-15 11:11:12 +03:00
|
|
|
SEC_WINNT_AUTH_IDENTITY identity = { 0 };
|
2012-10-01 04:15:42 +04:00
|
|
|
SecurityFunctionTable* table;
|
|
|
|
PSecBuffer p_SecBuffer;
|
|
|
|
SecBuffer output_SecBuffer;
|
|
|
|
SecBufferDesc output_SecBuffer_desc;
|
|
|
|
sspi_GlobalInit();
|
|
|
|
table = InitSecurityInterface();
|
2017-07-03 13:47:56 +03:00
|
|
|
status = QuerySecurityPackageInfo(NTLM_SSP_NAME, &pPackageInfo);
|
2012-10-01 04:15:42 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("QuerySecurityPackageInfo status: 0x%08"PRIX32"\n", status);
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2012-10-01 04:15:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
cbMaxLen = pPackageInfo->cbMaxToken;
|
|
|
|
identity.User = (UINT16*) _strdup(test_User);
|
|
|
|
identity.Domain = (UINT16*) _strdup(test_Domain);
|
|
|
|
identity.Password = (UINT16*) _strdup(test_Password);
|
2017-07-03 13:47:56 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!identity.User || !identity.Domain || !identity.Password)
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2015-06-17 23:08:02 +03:00
|
|
|
|
2015-06-22 19:52:13 +03:00
|
|
|
identity.UserLength = strlen(test_User);
|
|
|
|
identity.DomainLength = strlen(test_Domain);
|
|
|
|
identity.PasswordLength = strlen(test_Password);
|
2012-10-01 04:15:42 +04:00
|
|
|
identity.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
|
2017-07-03 13:47:56 +03:00
|
|
|
status = table->AcquireCredentialsHandle(NULL, NTLM_SSP_NAME,
|
|
|
|
SECPKG_CRED_OUTBOUND, NULL, &identity, NULL, NULL, &credentials, &expiration);
|
2012-10-01 04:15:42 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("AcquireCredentialsHandle status: 0x%08"PRIX32"\n", status);
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2012-10-01 04:15:42 +04:00
|
|
|
}
|
|
|
|
|
2017-07-03 13:47:56 +03:00
|
|
|
fContextReq = ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT | ISC_REQ_CONFIDENTIALITY |
|
|
|
|
ISC_REQ_DELEGATE;
|
2012-10-01 04:15:42 +04:00
|
|
|
output_buffer = malloc(cbMaxLen);
|
2017-07-03 13:47:56 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!output_buffer)
|
|
|
|
{
|
|
|
|
printf("Memory allocation failed\n");
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2015-04-03 17:21:01 +03:00
|
|
|
}
|
2012-10-01 04:15:42 +04:00
|
|
|
|
|
|
|
output_SecBuffer_desc.ulVersion = 0;
|
|
|
|
output_SecBuffer_desc.cBuffers = 1;
|
|
|
|
output_SecBuffer_desc.pBuffers = &output_SecBuffer;
|
|
|
|
output_SecBuffer.cbBuffer = cbMaxLen;
|
|
|
|
output_SecBuffer.BufferType = SECBUFFER_TOKEN;
|
|
|
|
output_SecBuffer.pvBuffer = output_buffer;
|
|
|
|
status = table->InitializeSecurityContext(&credentials, NULL, NULL, fContextReq, 0, 0, NULL, 0,
|
2017-07-03 13:47:56 +03:00
|
|
|
&context, &output_SecBuffer_desc, &pfContextAttr, &expiration);
|
2012-10-01 04:15:42 +04:00
|
|
|
|
|
|
|
if (status != SEC_I_CONTINUE_NEEDED)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("InitializeSecurityContext status: 0x%08"PRIX32"\n", status);
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2012-10-01 04:15:42 +04:00
|
|
|
}
|
|
|
|
|
2017-07-03 13:47:56 +03:00
|
|
|
printf("cBuffers: %"PRIu32" ulVersion: %"PRIu32"\n", output_SecBuffer_desc.cBuffers,
|
|
|
|
output_SecBuffer_desc.ulVersion);
|
2012-10-01 04:15:42 +04:00
|
|
|
p_SecBuffer = &output_SecBuffer_desc.pBuffers[0];
|
2017-07-03 13:47:56 +03:00
|
|
|
printf("BufferType: 0x%08"PRIX32" cbBuffer: %"PRIu32"\n", p_SecBuffer->BufferType,
|
|
|
|
p_SecBuffer->cbBuffer);
|
2017-11-15 11:11:12 +03:00
|
|
|
status = table->DeleteSecurityContext(&context);
|
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
|
|
|
printf("DeleteSecurityContext status: 0x%08"PRIX32"\n", status);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
fail:
|
|
|
|
free(identity.User);
|
|
|
|
free(identity.Domain);
|
|
|
|
free(identity.Password);
|
|
|
|
free(output_buffer);
|
|
|
|
|
|
|
|
if (SecIsValidHandle(&credentials))
|
|
|
|
table->FreeCredentialsHandle(&credentials);
|
|
|
|
|
2012-10-01 04:15:42 +04:00
|
|
|
FreeContextBuffer(pPackageInfo);
|
|
|
|
sspi_GlobalFinish();
|
2017-11-15 11:11:12 +03:00
|
|
|
return rc;
|
2012-10-01 04:15:42 +04:00
|
|
|
}
|
|
|
|
|