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