FreeRDP
TestInitializeSecurityContext.c
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 = NULL;
17  CtxtHandle context;
18  ULONG pfContextAttr = 0;
19  SECURITY_STATUS status = 0;
20  CredHandle credentials = { 0 };
21  TimeStamp expiration;
22  PSecPkgInfo pPackageInfo = NULL;
23  SEC_WINNT_AUTH_IDENTITY identity = { 0 };
24  SecurityFunctionTable* table = NULL;
25  PSecBuffer p_SecBuffer = NULL;
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 = table->AcquireCredentialsHandle(NULL, NTLM_SSP_NAME, SECPKG_CRED_OUTBOUND, NULL,
55  &identity, NULL, NULL, &credentials, &expiration);
56 
57  if (status != SEC_E_OK)
58  {
59  printf("AcquireCredentialsHandle status: 0x%08" PRIX32 "\n", status);
60  goto fail;
61  }
62 
63  fContextReq = ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT | ISC_REQ_CONFIDENTIALITY |
64  ISC_REQ_DELEGATE;
65  output_buffer = malloc(cbMaxLen);
66 
67  if (!output_buffer)
68  {
69  printf("Memory allocation failed\n");
70  goto fail;
71  }
72 
73  output_SecBuffer_desc.ulVersion = 0;
74  output_SecBuffer_desc.cBuffers = 1;
75  output_SecBuffer_desc.pBuffers = &output_SecBuffer;
76  output_SecBuffer.cbBuffer = cbMaxLen;
77  output_SecBuffer.BufferType = SECBUFFER_TOKEN;
78  output_SecBuffer.pvBuffer = output_buffer;
79  status = table->InitializeSecurityContext(&credentials, NULL, NULL, fContextReq, 0, 0, NULL, 0,
80  &context, &output_SecBuffer_desc, &pfContextAttr,
81  &expiration);
82 
83  if (status != SEC_I_CONTINUE_NEEDED)
84  {
85  printf("InitializeSecurityContext status: 0x%08" PRIX32 "\n", status);
86  goto fail;
87  }
88 
89  printf("cBuffers: %" PRIu32 " ulVersion: %" PRIu32 "\n", output_SecBuffer_desc.cBuffers,
90  output_SecBuffer_desc.ulVersion);
91  p_SecBuffer = &output_SecBuffer_desc.pBuffers[0];
92  printf("BufferType: 0x%08" PRIX32 " cbBuffer: %" PRIu32 "\n", p_SecBuffer->BufferType,
93  p_SecBuffer->cbBuffer);
94  status = table->DeleteSecurityContext(&context);
95 
96  if (status != SEC_E_OK)
97  {
98  printf("DeleteSecurityContext status: 0x%08" PRIX32 "\n", status);
99  goto fail;
100  }
101 
102  rc = 0;
103 fail:
104  free(identity.User);
105  free(identity.Domain);
106  free(identity.Password);
107  free(output_buffer);
108 
109  if (SecIsValidHandle(&credentials))
110  table->FreeCredentialsHandle(&credentials);
111 
112  table->FreeContextBuffer(pPackageInfo);
113  sspi_GlobalFinish();
114  return rc;
115 }