Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/winpr/libwinpr/sspi/CredSSP/credssp.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * WinPR: Windows Portable Runtime
3
 * Credential Security Support Provider (CredSSP)
4
 *
5
 * Copyright 2010-2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
20
#include <winpr/config.h>
21
22
#include <winpr/crt.h>
23
#include <winpr/sspi.h>
24
25
#include "credssp.h"
26
27
#include "../sspi.h"
28
#include "../../log.h"
29
30
#define TAG WINPR_TAG("sspi.CredSSP")
31
32
static const char* CREDSSP_PACKAGE_NAME = "CredSSP";
33
34
static SECURITY_STATUS SEC_ENTRY credssp_InitializeSecurityContextW(
35
    WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED PCtxtHandle phContext,
36
    WINPR_ATTR_UNUSED SEC_WCHAR* pszTargetName, WINPR_ATTR_UNUSED ULONG fContextReq,
37
    WINPR_ATTR_UNUSED ULONG Reserved1, WINPR_ATTR_UNUSED ULONG TargetDataRep,
38
    WINPR_ATTR_UNUSED PSecBufferDesc pInput, WINPR_ATTR_UNUSED ULONG Reserved2,
39
    WINPR_ATTR_UNUSED PCtxtHandle phNewContext, WINPR_ATTR_UNUSED PSecBufferDesc pOutput,
40
    WINPR_ATTR_UNUSED PULONG pfContextAttr, WINPR_ATTR_UNUSED PTimeStamp ptsExpiry)
41
0
{
42
0
  WLog_ERR(TAG, "TODO: Implement");
43
0
  return SEC_E_UNSUPPORTED_FUNCTION;
44
0
}
45
46
static SECURITY_STATUS SEC_ENTRY credssp_InitializeSecurityContextA(
47
    PCredHandle phCredential, PCtxtHandle phContext, WINPR_ATTR_UNUSED SEC_CHAR* pszTargetName,
48
    WINPR_ATTR_UNUSED ULONG fContextReq, WINPR_ATTR_UNUSED ULONG Reserved1,
49
    WINPR_ATTR_UNUSED ULONG TargetDataRep, WINPR_ATTR_UNUSED PSecBufferDesc pInput,
50
    WINPR_ATTR_UNUSED ULONG Reserved2, PCtxtHandle phNewContext,
51
    WINPR_ATTR_UNUSED PSecBufferDesc pOutput, WINPR_ATTR_UNUSED PULONG pfContextAttr,
52
    WINPR_ATTR_UNUSED PTimeStamp ptsExpiry)
53
0
{
54
0
  CREDSSP_CONTEXT* context = NULL;
55
0
  SSPI_CREDENTIALS* credentials = NULL;
56
57
  /* behave like windows SSPIs that don't want empty context */
58
0
  if (phContext && !phContext->dwLower && !phContext->dwUpper)
59
0
    return SEC_E_INVALID_HANDLE;
60
61
0
  context = (CREDSSP_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
62
63
0
  if (!context)
64
0
  {
65
0
    union
66
0
    {
67
0
      const void* cpv;
68
0
      void* pv;
69
0
    } cnv;
70
0
    context = credssp_ContextNew();
71
72
0
    if (!context)
73
0
      return SEC_E_INSUFFICIENT_MEMORY;
74
75
0
    credentials = (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
76
77
0
    if (!credentials)
78
0
    {
79
0
      credssp_ContextFree(context);
80
0
      return SEC_E_INVALID_HANDLE;
81
0
    }
82
83
0
    sspi_SecureHandleSetLowerPointer(phNewContext, context);
84
85
0
    cnv.cpv = CREDSSP_PACKAGE_NAME;
86
0
    sspi_SecureHandleSetUpperPointer(phNewContext, cnv.pv);
87
0
  }
88
89
0
  return SEC_E_OK;
90
0
}
91
92
CREDSSP_CONTEXT* credssp_ContextNew(void)
93
0
{
94
0
  CREDSSP_CONTEXT* context = NULL;
95
0
  context = (CREDSSP_CONTEXT*)calloc(1, sizeof(CREDSSP_CONTEXT));
96
97
0
  if (!context)
98
0
    return NULL;
99
100
0
  return context;
101
0
}
102
103
void credssp_ContextFree(CREDSSP_CONTEXT* context)
104
0
{
105
0
  free(context);
106
0
}
107
108
static SECURITY_STATUS SEC_ENTRY credssp_QueryContextAttributes(PCtxtHandle phContext,
109
                                                                WINPR_ATTR_UNUSED ULONG ulAttribute,
110
                                                                void* pBuffer)
111
0
{
112
0
  if (!phContext)
113
0
    return SEC_E_INVALID_HANDLE;
114
115
0
  if (!pBuffer)
116
0
    return SEC_E_INSUFFICIENT_MEMORY;
117
118
0
  WLog_ERR(TAG, "TODO: Implement");
119
0
  return SEC_E_UNSUPPORTED_FUNCTION;
120
0
}
121
122
static SECURITY_STATUS SEC_ENTRY credssp_AcquireCredentialsHandleW(
123
    WINPR_ATTR_UNUSED SEC_WCHAR* pszPrincipal, WINPR_ATTR_UNUSED SEC_WCHAR* pszPackage,
124
    WINPR_ATTR_UNUSED ULONG fCredentialUse, WINPR_ATTR_UNUSED void* pvLogonID,
125
    WINPR_ATTR_UNUSED void* pAuthData, WINPR_ATTR_UNUSED SEC_GET_KEY_FN pGetKeyFn,
126
    WINPR_ATTR_UNUSED void* pvGetKeyArgument, WINPR_ATTR_UNUSED PCredHandle phCredential,
127
    WINPR_ATTR_UNUSED PTimeStamp ptsExpiry)
128
0
{
129
0
  WLog_ERR(TAG, "TODO: Implement");
130
0
  return SEC_E_UNSUPPORTED_FUNCTION;
131
0
}
132
133
static SECURITY_STATUS SEC_ENTRY credssp_AcquireCredentialsHandleA(
134
    WINPR_ATTR_UNUSED SEC_CHAR* pszPrincipal, WINPR_ATTR_UNUSED SEC_CHAR* pszPackage,
135
    WINPR_ATTR_UNUSED ULONG fCredentialUse, WINPR_ATTR_UNUSED void* pvLogonID,
136
    WINPR_ATTR_UNUSED void* pAuthData, WINPR_ATTR_UNUSED SEC_GET_KEY_FN pGetKeyFn,
137
    WINPR_ATTR_UNUSED void* pvGetKeyArgument, WINPR_ATTR_UNUSED PCredHandle phCredential,
138
    WINPR_ATTR_UNUSED PTimeStamp ptsExpiry)
139
0
{
140
0
  SSPI_CREDENTIALS* credentials = NULL;
141
0
  SEC_WINNT_AUTH_IDENTITY* identity = NULL;
142
143
0
  if (fCredentialUse == SECPKG_CRED_OUTBOUND)
144
0
  {
145
0
    union
146
0
    {
147
0
      const void* cpv;
148
0
      void* pv;
149
0
    } cnv;
150
0
    credentials = sspi_CredentialsNew();
151
152
0
    if (!credentials)
153
0
      return SEC_E_INSUFFICIENT_MEMORY;
154
155
0
    identity = (SEC_WINNT_AUTH_IDENTITY*)pAuthData;
156
0
    CopyMemory(&(credentials->identity), identity, sizeof(SEC_WINNT_AUTH_IDENTITY));
157
0
    sspi_SecureHandleSetLowerPointer(phCredential, (void*)credentials);
158
159
0
    cnv.cpv = CREDSSP_PACKAGE_NAME;
160
0
    sspi_SecureHandleSetUpperPointer(phCredential, cnv.pv);
161
0
    return SEC_E_OK;
162
0
  }
163
164
0
  WLog_ERR(TAG, "TODO: Implement");
165
0
  return SEC_E_UNSUPPORTED_FUNCTION;
166
0
}
167
168
static SECURITY_STATUS SEC_ENTRY credssp_QueryCredentialsAttributesW(
169
    WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute,
170
    WINPR_ATTR_UNUSED void* pBuffer)
171
0
{
172
0
  WLog_ERR(TAG, "TODO: Implement");
173
0
  return SEC_E_UNSUPPORTED_FUNCTION;
174
0
}
175
176
static SECURITY_STATUS SEC_ENTRY credssp_QueryCredentialsAttributesA(
177
    WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute,
178
    WINPR_ATTR_UNUSED void* pBuffer)
179
0
{
180
0
  if (ulAttribute == SECPKG_CRED_ATTR_NAMES)
181
0
  {
182
0
    SSPI_CREDENTIALS* credentials =
183
0
        (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
184
185
0
    if (!credentials)
186
0
      return SEC_E_INVALID_HANDLE;
187
188
0
    return SEC_E_OK;
189
0
  }
190
191
0
  WLog_ERR(TAG, "TODO: Implement");
192
0
  return SEC_E_UNSUPPORTED_FUNCTION;
193
0
}
194
195
static SECURITY_STATUS SEC_ENTRY credssp_FreeCredentialsHandle(PCredHandle phCredential)
196
0
{
197
0
  SSPI_CREDENTIALS* credentials = NULL;
198
199
0
  if (!phCredential)
200
0
    return SEC_E_INVALID_HANDLE;
201
202
0
  credentials = (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
203
204
0
  if (!credentials)
205
0
    return SEC_E_INVALID_HANDLE;
206
207
0
  sspi_CredentialsFree(credentials);
208
0
  return SEC_E_OK;
209
0
}
210
211
static SECURITY_STATUS SEC_ENTRY credssp_EncryptMessage(WINPR_ATTR_UNUSED PCtxtHandle phContext,
212
                                                        WINPR_ATTR_UNUSED ULONG fQOP,
213
                                                        WINPR_ATTR_UNUSED PSecBufferDesc pMessage,
214
                                                        WINPR_ATTR_UNUSED ULONG MessageSeqNo)
215
0
{
216
0
  WLog_ERR(TAG, "TODO: Implement");
217
0
  return SEC_E_UNSUPPORTED_FUNCTION;
218
0
}
219
220
static SECURITY_STATUS SEC_ENTRY credssp_DecryptMessage(WINPR_ATTR_UNUSED PCtxtHandle phContext,
221
                                                        WINPR_ATTR_UNUSED PSecBufferDesc pMessage,
222
                                                        WINPR_ATTR_UNUSED ULONG MessageSeqNo,
223
                                                        WINPR_ATTR_UNUSED ULONG* pfQOP)
224
0
{
225
0
  WLog_ERR(TAG, "TODO: Implement");
226
0
  return SEC_E_UNSUPPORTED_FUNCTION;
227
0
}
228
229
static SECURITY_STATUS SEC_ENTRY credssp_MakeSignature(WINPR_ATTR_UNUSED PCtxtHandle phContext,
230
                                                       WINPR_ATTR_UNUSED ULONG fQOP,
231
                                                       WINPR_ATTR_UNUSED PSecBufferDesc pMessage,
232
                                                       WINPR_ATTR_UNUSED ULONG MessageSeqNo)
233
0
{
234
0
  WLog_ERR(TAG, "TODO: Implement");
235
0
  return SEC_E_UNSUPPORTED_FUNCTION;
236
0
}
237
238
static SECURITY_STATUS SEC_ENTRY credssp_VerifySignature(WINPR_ATTR_UNUSED PCtxtHandle phContext,
239
                                                         WINPR_ATTR_UNUSED PSecBufferDesc pMessage,
240
                                                         WINPR_ATTR_UNUSED ULONG MessageSeqNo,
241
                                                         WINPR_ATTR_UNUSED ULONG* pfQOP)
242
0
{
243
0
  WLog_ERR(TAG, "TODO: Implement");
244
0
  return SEC_E_UNSUPPORTED_FUNCTION;
245
0
}
246
247
const SecurityFunctionTableA CREDSSP_SecurityFunctionTableA = {
248
  3,                                   /* dwVersion */
249
  NULL,                                /* EnumerateSecurityPackages */
250
  credssp_QueryCredentialsAttributesA, /* QueryCredentialsAttributes */
251
  credssp_AcquireCredentialsHandleA,   /* AcquireCredentialsHandle */
252
  credssp_FreeCredentialsHandle,       /* FreeCredentialsHandle */
253
  NULL,                                /* Reserved2 */
254
  credssp_InitializeSecurityContextA,  /* InitializeSecurityContext */
255
  NULL,                                /* AcceptSecurityContext */
256
  NULL,                                /* CompleteAuthToken */
257
  NULL,                                /* DeleteSecurityContext */
258
  NULL,                                /* ApplyControlToken */
259
  credssp_QueryContextAttributes,      /* QueryContextAttributes */
260
  NULL,                                /* ImpersonateSecurityContext */
261
  NULL,                                /* RevertSecurityContext */
262
  credssp_MakeSignature,               /* MakeSignature */
263
  credssp_VerifySignature,             /* VerifySignature */
264
  NULL,                                /* FreeContextBuffer */
265
  NULL,                                /* QuerySecurityPackageInfo */
266
  NULL,                                /* Reserved3 */
267
  NULL,                                /* Reserved4 */
268
  NULL,                                /* ExportSecurityContext */
269
  NULL,                                /* ImportSecurityContext */
270
  NULL,                                /* AddCredentials */
271
  NULL,                                /* Reserved8 */
272
  NULL,                                /* QuerySecurityContextToken */
273
  credssp_EncryptMessage,              /* EncryptMessage */
274
  credssp_DecryptMessage,              /* DecryptMessage */
275
  NULL,                                /* SetContextAttributes */
276
  NULL,                                /* SetCredentialsAttributes */
277
};
278
279
const SecurityFunctionTableW CREDSSP_SecurityFunctionTableW = {
280
  3,                                   /* dwVersion */
281
  NULL,                                /* EnumerateSecurityPackages */
282
  credssp_QueryCredentialsAttributesW, /* QueryCredentialsAttributes */
283
  credssp_AcquireCredentialsHandleW,   /* AcquireCredentialsHandle */
284
  credssp_FreeCredentialsHandle,       /* FreeCredentialsHandle */
285
  NULL,                                /* Reserved2 */
286
  credssp_InitializeSecurityContextW,  /* InitializeSecurityContext */
287
  NULL,                                /* AcceptSecurityContext */
288
  NULL,                                /* CompleteAuthToken */
289
  NULL,                                /* DeleteSecurityContext */
290
  NULL,                                /* ApplyControlToken */
291
  credssp_QueryContextAttributes,      /* QueryContextAttributes */
292
  NULL,                                /* ImpersonateSecurityContext */
293
  NULL,                                /* RevertSecurityContext */
294
  credssp_MakeSignature,               /* MakeSignature */
295
  credssp_VerifySignature,             /* VerifySignature */
296
  NULL,                                /* FreeContextBuffer */
297
  NULL,                                /* QuerySecurityPackageInfo */
298
  NULL,                                /* Reserved3 */
299
  NULL,                                /* Reserved4 */
300
  NULL,                                /* ExportSecurityContext */
301
  NULL,                                /* ImportSecurityContext */
302
  NULL,                                /* AddCredentials */
303
  NULL,                                /* Reserved8 */
304
  NULL,                                /* QuerySecurityContextToken */
305
  credssp_EncryptMessage,              /* EncryptMessage */
306
  credssp_DecryptMessage,              /* DecryptMessage */
307
  NULL,                                /* SetContextAttributes */
308
  NULL,                                /* SetCredentialsAttributes */
309
};
310
311
const SecPkgInfoA CREDSSP_SecPkgInfoA = {
312
  0x000110733,                          /* fCapabilities */
313
  1,                                    /* wVersion */
314
  0xFFFF,                               /* wRPCID */
315
  0x000090A8,                           /* cbMaxToken */
316
  "CREDSSP",                            /* Name */
317
  "Microsoft CredSSP Security Provider" /* Comment */
318
};
319
320
static WCHAR CREDSSP_SecPkgInfoW_NameBuffer[128] = { 0 };
321
static WCHAR CREDSSP_SecPkgInfoW_CommentBuffer[128] = { 0 };
322
323
const SecPkgInfoW CREDSSP_SecPkgInfoW = {
324
  0x000110733,                      /* fCapabilities */
325
  1,                                /* wVersion */
326
  0xFFFF,                           /* wRPCID */
327
  0x000090A8,                       /* cbMaxToken */
328
  CREDSSP_SecPkgInfoW_NameBuffer,   /* Name */
329
  CREDSSP_SecPkgInfoW_CommentBuffer /* Comment */
330
};
331
332
BOOL CREDSSP_init(void)
333
0
{
334
0
  InitializeConstWCharFromUtf8(CREDSSP_SecPkgInfoA.Name, CREDSSP_SecPkgInfoW_NameBuffer,
335
0
                               ARRAYSIZE(CREDSSP_SecPkgInfoW_NameBuffer));
336
0
  InitializeConstWCharFromUtf8(CREDSSP_SecPkgInfoA.Comment, CREDSSP_SecPkgInfoW_CommentBuffer,
337
0
                               ARRAYSIZE(CREDSSP_SecPkgInfoW_CommentBuffer));
338
0
  return TRUE;
339
0
}