Coverage Report

Created: 2024-05-20 06:11

/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
    PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq,
36
    ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
37
    PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
38
0
{
39
0
  WLog_ERR(TAG, "TODO: Implement");
40
0
  return SEC_E_UNSUPPORTED_FUNCTION;
41
0
}
42
43
static SECURITY_STATUS SEC_ENTRY credssp_InitializeSecurityContextA(
44
    PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR* pszTargetName, ULONG fContextReq,
45
    ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
46
    PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
47
0
{
48
0
  CREDSSP_CONTEXT* context = NULL;
49
0
  SSPI_CREDENTIALS* credentials = NULL;
50
51
  /* behave like windows SSPIs that don't want empty context */
52
0
  if (phContext && !phContext->dwLower && !phContext->dwUpper)
53
0
    return SEC_E_INVALID_HANDLE;
54
55
0
  context = (CREDSSP_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
56
57
0
  if (!context)
58
0
  {
59
0
    union
60
0
    {
61
0
      const void* cpv;
62
0
      void* pv;
63
0
    } cnv;
64
0
    context = credssp_ContextNew();
65
66
0
    if (!context)
67
0
      return SEC_E_INSUFFICIENT_MEMORY;
68
69
0
    credentials = (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
70
71
0
    if (!credentials)
72
0
    {
73
0
      credssp_ContextFree(context);
74
0
      return SEC_E_INVALID_HANDLE;
75
0
    }
76
77
0
    sspi_SecureHandleSetLowerPointer(phNewContext, context);
78
79
0
    cnv.cpv = CREDSSP_PACKAGE_NAME;
80
0
    sspi_SecureHandleSetUpperPointer(phNewContext, cnv.pv);
81
0
  }
82
83
0
  return SEC_E_OK;
84
0
}
85
86
CREDSSP_CONTEXT* credssp_ContextNew(void)
87
0
{
88
0
  CREDSSP_CONTEXT* context = NULL;
89
0
  context = (CREDSSP_CONTEXT*)calloc(1, sizeof(CREDSSP_CONTEXT));
90
91
0
  if (!context)
92
0
    return NULL;
93
94
0
  return context;
95
0
}
96
97
void credssp_ContextFree(CREDSSP_CONTEXT* context)
98
0
{
99
0
  free(context);
100
0
}
101
102
static SECURITY_STATUS SEC_ENTRY credssp_QueryContextAttributes(PCtxtHandle phContext,
103
                                                                ULONG ulAttribute, void* pBuffer)
104
0
{
105
0
  if (!phContext)
106
0
    return SEC_E_INVALID_HANDLE;
107
108
0
  if (!pBuffer)
109
0
    return SEC_E_INSUFFICIENT_MEMORY;
110
111
0
  WLog_ERR(TAG, "TODO: Implement");
112
0
  return SEC_E_UNSUPPORTED_FUNCTION;
113
0
}
114
115
static SECURITY_STATUS SEC_ENTRY credssp_AcquireCredentialsHandleW(
116
    SEC_WCHAR* pszPrincipal, SEC_WCHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
117
    void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
118
    PTimeStamp ptsExpiry)
119
0
{
120
0
  WLog_ERR(TAG, "TODO: Implement");
121
0
  return SEC_E_UNSUPPORTED_FUNCTION;
122
0
}
123
124
static SECURITY_STATUS SEC_ENTRY credssp_AcquireCredentialsHandleA(
125
    SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
126
    void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
127
    PTimeStamp ptsExpiry)
128
0
{
129
0
  SSPI_CREDENTIALS* credentials = NULL;
130
0
  SEC_WINNT_AUTH_IDENTITY* identity = NULL;
131
132
0
  if (fCredentialUse == SECPKG_CRED_OUTBOUND)
133
0
  {
134
0
    union
135
0
    {
136
0
      const void* cpv;
137
0
      void* pv;
138
0
    } cnv;
139
0
    credentials = sspi_CredentialsNew();
140
141
0
    if (!credentials)
142
0
      return SEC_E_INSUFFICIENT_MEMORY;
143
144
0
    identity = (SEC_WINNT_AUTH_IDENTITY*)pAuthData;
145
0
    CopyMemory(&(credentials->identity), identity, sizeof(SEC_WINNT_AUTH_IDENTITY));
146
0
    sspi_SecureHandleSetLowerPointer(phCredential, (void*)credentials);
147
148
0
    cnv.cpv = CREDSSP_PACKAGE_NAME;
149
0
    sspi_SecureHandleSetUpperPointer(phCredential, cnv.pv);
150
0
    return SEC_E_OK;
151
0
  }
152
153
0
  WLog_ERR(TAG, "TODO: Implement");
154
0
  return SEC_E_UNSUPPORTED_FUNCTION;
155
0
}
156
157
static SECURITY_STATUS SEC_ENTRY credssp_QueryCredentialsAttributesW(PCredHandle phCredential,
158
                                                                     ULONG ulAttribute,
159
                                                                     void* pBuffer)
160
0
{
161
0
  WLog_ERR(TAG, "TODO: Implement");
162
0
  return SEC_E_UNSUPPORTED_FUNCTION;
163
0
}
164
165
static SECURITY_STATUS SEC_ENTRY credssp_QueryCredentialsAttributesA(PCredHandle phCredential,
166
                                                                     ULONG ulAttribute,
167
                                                                     void* pBuffer)
168
0
{
169
0
  if (ulAttribute == SECPKG_CRED_ATTR_NAMES)
170
0
  {
171
0
    SSPI_CREDENTIALS* credentials =
172
0
        (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
173
174
0
    if (!credentials)
175
0
      return SEC_E_INVALID_HANDLE;
176
177
0
    return SEC_E_OK;
178
0
  }
179
180
0
  WLog_ERR(TAG, "TODO: Implement");
181
0
  return SEC_E_UNSUPPORTED_FUNCTION;
182
0
}
183
184
static SECURITY_STATUS SEC_ENTRY credssp_FreeCredentialsHandle(PCredHandle phCredential)
185
0
{
186
0
  SSPI_CREDENTIALS* credentials = NULL;
187
188
0
  if (!phCredential)
189
0
    return SEC_E_INVALID_HANDLE;
190
191
0
  credentials = (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
192
193
0
  if (!credentials)
194
0
    return SEC_E_INVALID_HANDLE;
195
196
0
  sspi_CredentialsFree(credentials);
197
0
  return SEC_E_OK;
198
0
}
199
200
static SECURITY_STATUS SEC_ENTRY credssp_EncryptMessage(PCtxtHandle phContext, ULONG fQOP,
201
                                                        PSecBufferDesc pMessage, ULONG MessageSeqNo)
202
0
{
203
0
  WLog_ERR(TAG, "TODO: Implement");
204
0
  return SEC_E_UNSUPPORTED_FUNCTION;
205
0
}
206
207
static SECURITY_STATUS SEC_ENTRY credssp_DecryptMessage(PCtxtHandle phContext,
208
                                                        PSecBufferDesc pMessage, ULONG MessageSeqNo,
209
                                                        ULONG* pfQOP)
210
0
{
211
0
  WLog_ERR(TAG, "TODO: Implement");
212
0
  return SEC_E_UNSUPPORTED_FUNCTION;
213
0
}
214
215
static SECURITY_STATUS SEC_ENTRY credssp_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
216
                                                       PSecBufferDesc pMessage, ULONG MessageSeqNo)
217
0
{
218
0
  WLog_ERR(TAG, "TODO: Implement");
219
0
  return SEC_E_UNSUPPORTED_FUNCTION;
220
0
}
221
222
static SECURITY_STATUS SEC_ENTRY credssp_VerifySignature(PCtxtHandle phContext,
223
                                                         PSecBufferDesc pMessage,
224
                                                         ULONG MessageSeqNo, ULONG* pfQOP)
225
0
{
226
0
  WLog_ERR(TAG, "TODO: Implement");
227
0
  return SEC_E_UNSUPPORTED_FUNCTION;
228
0
}
229
230
const SecurityFunctionTableA CREDSSP_SecurityFunctionTableA = {
231
  3,                                   /* dwVersion */
232
  NULL,                                /* EnumerateSecurityPackages */
233
  credssp_QueryCredentialsAttributesA, /* QueryCredentialsAttributes */
234
  credssp_AcquireCredentialsHandleA,   /* AcquireCredentialsHandle */
235
  credssp_FreeCredentialsHandle,       /* FreeCredentialsHandle */
236
  NULL,                                /* Reserved2 */
237
  credssp_InitializeSecurityContextA,  /* InitializeSecurityContext */
238
  NULL,                                /* AcceptSecurityContext */
239
  NULL,                                /* CompleteAuthToken */
240
  NULL,                                /* DeleteSecurityContext */
241
  NULL,                                /* ApplyControlToken */
242
  credssp_QueryContextAttributes,      /* QueryContextAttributes */
243
  NULL,                                /* ImpersonateSecurityContext */
244
  NULL,                                /* RevertSecurityContext */
245
  credssp_MakeSignature,               /* MakeSignature */
246
  credssp_VerifySignature,             /* VerifySignature */
247
  NULL,                                /* FreeContextBuffer */
248
  NULL,                                /* QuerySecurityPackageInfo */
249
  NULL,                                /* Reserved3 */
250
  NULL,                                /* Reserved4 */
251
  NULL,                                /* ExportSecurityContext */
252
  NULL,                                /* ImportSecurityContext */
253
  NULL,                                /* AddCredentials */
254
  NULL,                                /* Reserved8 */
255
  NULL,                                /* QuerySecurityContextToken */
256
  credssp_EncryptMessage,              /* EncryptMessage */
257
  credssp_DecryptMessage,              /* DecryptMessage */
258
  NULL,                                /* SetContextAttributes */
259
  NULL,                                /* SetCredentialsAttributes */
260
};
261
262
const SecurityFunctionTableW CREDSSP_SecurityFunctionTableW = {
263
  3,                                   /* dwVersion */
264
  NULL,                                /* EnumerateSecurityPackages */
265
  credssp_QueryCredentialsAttributesW, /* QueryCredentialsAttributes */
266
  credssp_AcquireCredentialsHandleW,   /* AcquireCredentialsHandle */
267
  credssp_FreeCredentialsHandle,       /* FreeCredentialsHandle */
268
  NULL,                                /* Reserved2 */
269
  credssp_InitializeSecurityContextW,  /* InitializeSecurityContext */
270
  NULL,                                /* AcceptSecurityContext */
271
  NULL,                                /* CompleteAuthToken */
272
  NULL,                                /* DeleteSecurityContext */
273
  NULL,                                /* ApplyControlToken */
274
  credssp_QueryContextAttributes,      /* QueryContextAttributes */
275
  NULL,                                /* ImpersonateSecurityContext */
276
  NULL,                                /* RevertSecurityContext */
277
  credssp_MakeSignature,               /* MakeSignature */
278
  credssp_VerifySignature,             /* VerifySignature */
279
  NULL,                                /* FreeContextBuffer */
280
  NULL,                                /* QuerySecurityPackageInfo */
281
  NULL,                                /* Reserved3 */
282
  NULL,                                /* Reserved4 */
283
  NULL,                                /* ExportSecurityContext */
284
  NULL,                                /* ImportSecurityContext */
285
  NULL,                                /* AddCredentials */
286
  NULL,                                /* Reserved8 */
287
  NULL,                                /* QuerySecurityContextToken */
288
  credssp_EncryptMessage,              /* EncryptMessage */
289
  credssp_DecryptMessage,              /* DecryptMessage */
290
  NULL,                                /* SetContextAttributes */
291
  NULL,                                /* SetCredentialsAttributes */
292
};
293
294
const SecPkgInfoA CREDSSP_SecPkgInfoA = {
295
  0x000110733,                          /* fCapabilities */
296
  1,                                    /* wVersion */
297
  0xFFFF,                               /* wRPCID */
298
  0x000090A8,                           /* cbMaxToken */
299
  "CREDSSP",                            /* Name */
300
  "Microsoft CredSSP Security Provider" /* Comment */
301
};
302
303
static WCHAR CREDSSP_SecPkgInfoW_NameBuffer[128] = { 0 };
304
static WCHAR CREDSSP_SecPkgInfoW_CommentBuffer[128] = { 0 };
305
306
const SecPkgInfoW CREDSSP_SecPkgInfoW = {
307
  0x000110733,                      /* fCapabilities */
308
  1,                                /* wVersion */
309
  0xFFFF,                           /* wRPCID */
310
  0x000090A8,                       /* cbMaxToken */
311
  CREDSSP_SecPkgInfoW_NameBuffer,   /* Name */
312
  CREDSSP_SecPkgInfoW_CommentBuffer /* Comment */
313
};
314
315
BOOL CREDSSP_init(void)
316
0
{
317
0
  InitializeConstWCharFromUtf8(CREDSSP_SecPkgInfoA.Name, CREDSSP_SecPkgInfoW_NameBuffer,
318
0
                               ARRAYSIZE(CREDSSP_SecPkgInfoW_NameBuffer));
319
0
  InitializeConstWCharFromUtf8(CREDSSP_SecPkgInfoA.Comment, CREDSSP_SecPkgInfoW_CommentBuffer,
320
0
                               ARRAYSIZE(CREDSSP_SecPkgInfoW_CommentBuffer));
321
0
  return TRUE;
322
0
}