| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <winpr/crt.h> |
| 4 |
#include <winpr/sspi.h> |
| 5 |
#include <winpr/winpr.h> |
| 6 |
|
| 7 |
static const char* test_User = "User"; |
| 8 |
static const char* test_Domain = "Domain"; |
| 9 |
static const char* test_Password = "Password"; |
| 10 |
|
| 11 |
int TestInitializeSecurityContext(int argc, char* argv[]) |
| 12 |
{ |
| 13 |
int rc = -1; |
| 14 |
UINT32 cbMaxLen = 0; |
| 15 |
UINT32 fContextReq = 0; |
| 16 |
void* output_buffer = nullptr; |
| 17 |
CtxtHandle context; |
| 18 |
ULONG pfContextAttr = 0; |
| 19 |
SECURITY_STATUS status = 0; |
| 20 |
CredHandle credentials = WINPR_C_ARRAY_INIT; |
| 21 |
TimeStamp expiration; |
| 22 |
PSecPkgInfo pPackageInfo = nullptr; |
| 23 |
SEC_WINNT_AUTH_IDENTITY identity = WINPR_C_ARRAY_INIT; |
| 24 |
SecurityFunctionTable* table = nullptr; |
| 25 |
PSecBuffer p_SecBuffer = nullptr; |
| 26 |
SecBuffer output_SecBuffer; |
| 27 |
SecBufferDesc output_SecBuffer_desc; |
| 28 |
|
| 29 |
WINPR_UNUSED(argc); |
| 30 |
WINPR_UNUSED(argv); |
| 31 |
|
| 32 |
sspi_GlobalInit(); |
| 33 |
table = InitSecurityInterfaceEx(0); |
| 34 |
status = table->QuerySecurityPackageInfo(NTLM_SSP_NAME, &pPackageInfo); |
| 35 |
|
| 36 |
if (status != SEC_E_OK) |
| 37 |
{ |
| 38 |
printf("QuerySecurityPackageInfo status: 0x%08" PRIX32 "\n", status); |
| 39 |
goto fail; |
| 40 |
} |
| 41 |
|
| 42 |
cbMaxLen = pPackageInfo->cbMaxToken; |
| 43 |
identity.User = (UINT16*)_strdup(test_User); |
| 44 |
identity.Domain = (UINT16*)_strdup(test_Domain); |
| 45 |
identity.Password = (UINT16*)_strdup(test_Password); |
| 46 |
|
| 47 |
if (!identity.User || !identity.Domain || !identity.Password) |
| 48 |
goto fail; |
| 49 |
|
| 50 |
identity.UserLength = strlen(test_User); |
| 51 |
identity.DomainLength = strlen(test_Domain); |
| 52 |
identity.PasswordLength = strlen(test_Password); |
| 53 |
identity.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI; |
| 54 |
status = |
| 55 |
table->AcquireCredentialsHandle(nullptr, NTLM_SSP_NAME, SECPKG_CRED_OUTBOUND, nullptr, |
| 56 |
&identity, nullptr, nullptr, &credentials, &expiration); |
| 57 |
|
| 58 |
if (status != SEC_E_OK) |
| 59 |
{ |
| 60 |
printf("AcquireCredentialsHandle status: 0x%08" PRIX32 "\n", status); |
| 61 |
goto fail; |
| 62 |
} |
| 63 |
|
| 64 |
fContextReq = ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT | ISC_REQ_CONFIDENTIALITY | |
| 65 |
ISC_REQ_DELEGATE; |
| 66 |
output_buffer = malloc(cbMaxLen); |
| 67 |
|
| 68 |
if (!output_buffer) |
| 69 |
{ |
| 70 |
printf("Memory allocation failed\n"); |
| 71 |
goto fail; |
| 72 |
} |
| 73 |
|
| 74 |
output_SecBuffer_desc.ulVersion = 0; |
| 75 |
output_SecBuffer_desc.cBuffers = 1; |
| 76 |
output_SecBuffer_desc.pBuffers = &output_SecBuffer; |
| 77 |
output_SecBuffer.cbBuffer = cbMaxLen; |
| 78 |
output_SecBuffer.BufferType = SECBUFFER_TOKEN; |
| 79 |
output_SecBuffer.pvBuffer = output_buffer; |
| 80 |
status = table->InitializeSecurityContext(&credentials, nullptr, nullptr, fContextReq, 0, 0, |
| 81 |
nullptr, 0, &context, &output_SecBuffer_desc, |
| 82 |
&pfContextAttr, &expiration); |
| 83 |
|
| 84 |
if (status != SEC_I_CONTINUE_NEEDED) |
| 85 |
{ |
| 86 |
printf("InitializeSecurityContext status: 0x%08" PRIX32 "\n", status); |
| 87 |
goto fail; |
| 88 |
} |
| 89 |
|
| 90 |
printf("cBuffers: %" PRIu32 " ulVersion: %" PRIu32 "\n", output_SecBuffer_desc.cBuffers, |
| 91 |
output_SecBuffer_desc.ulVersion); |
| 92 |
p_SecBuffer = &output_SecBuffer_desc.pBuffers[0]; |
| 93 |
printf("BufferType: 0x%08" PRIX32 " cbBuffer: %" PRIu32 "\n", p_SecBuffer->BufferType, |
| 94 |
p_SecBuffer->cbBuffer); |
| 95 |
status = table->DeleteSecurityContext(&context); |
| 96 |
|
| 97 |
if (status != SEC_E_OK) |
| 98 |
{ |
| 99 |
printf("DeleteSecurityContext status: 0x%08" PRIX32 "\n", status); |
| 100 |
goto fail; |
| 101 |
} |
| 102 |
|
| 103 |
rc = 0; |
| 104 |
fail: |
| 105 |
free(identity.User); |
| 106 |
free(identity.Domain); |
| 107 |
free(identity.Password); |
| 108 |
free(output_buffer); |
| 109 |
|
| 110 |
if (SecIsValidHandle(&credentials)) |
| 111 |
table->FreeCredentialsHandle(&credentials); |
| 112 |
|
| 113 |
table->FreeContextBuffer(pPackageInfo); |
| 114 |
sspi_GlobalFinish(); |
| 115 |
return rc; |
| 116 |
} |