Coverage Report

Created: 2026-06-15 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/sspi/sspi_winpr.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Security Support Provider Interface (SSPI)
4
 *
5
 * Copyright 2012-2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2017 Dorian Ducournau <dorian.ducournau@gmail.com>
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
#include <winpr/config.h>
22
#include <winpr/assert.h>
23
#include <winpr/windows.h>
24
25
#include <winpr/crt.h>
26
#include <winpr/sspi.h>
27
#include <winpr/ssl.h>
28
#include <winpr/print.h>
29
30
#include "sspi.h"
31
32
#include "sspi_winpr.h"
33
34
#include "../utils.h"
35
#include "../log.h"
36
1
#define TAG WINPR_TAG("sspi")
37
38
/* Authentication Functions: http://msdn.microsoft.com/en-us/library/windows/desktop/aa374731/ */
39
40
#include "NTLM/ntlm.h"
41
#include "NTLM/ntlm_export.h"
42
#include "CredSSP/credssp.h"
43
#include "Kerberos/kerberos.h"
44
#include "Negotiate/negotiate.h"
45
#include "Schannel/schannel.h"
46
47
static const SecPkgInfoA* SecPkgInfoA_LIST[] = { &NTLM_SecPkgInfoA, &KERBEROS_SecPkgInfoA,
48
                                               &NEGOTIATE_SecPkgInfoA, &CREDSSP_SecPkgInfoA,
49
                                               &SCHANNEL_SecPkgInfoA };
50
51
static const SecPkgInfoW* SecPkgInfoW_LIST[] = { &NTLM_SecPkgInfoW, &KERBEROS_SecPkgInfoW,
52
                                               &NEGOTIATE_SecPkgInfoW, &CREDSSP_SecPkgInfoW,
53
                                               &SCHANNEL_SecPkgInfoW };
54
55
typedef struct
56
{
57
  const SEC_CHAR* Name;
58
  const SecurityFunctionTableA* SecurityFunctionTable;
59
} SecurityFunctionTableA_NAME;
60
61
typedef struct
62
{
63
  const SEC_WCHAR* Name;
64
  const SecurityFunctionTableW* SecurityFunctionTable;
65
} SecurityFunctionTableW_NAME;
66
67
static const SecurityFunctionTableA_NAME SecurityFunctionTableA_NAME_LIST[] = {
68
  { "NTLM", &NTLM_SecurityFunctionTableA },
69
  { "Kerberos", &KERBEROS_SecurityFunctionTableA },
70
  { "Negotiate", &NEGOTIATE_SecurityFunctionTableA },
71
  { "CREDSSP", &CREDSSP_SecurityFunctionTableA },
72
  { "Schannel", &SCHANNEL_SecurityFunctionTableA }
73
};
74
75
static WCHAR BUFFER_NAME_LIST_W[5][32] = WINPR_C_ARRAY_INIT;
76
77
static const SecurityFunctionTableW_NAME SecurityFunctionTableW_NAME_LIST[] = {
78
  { BUFFER_NAME_LIST_W[0], &NTLM_SecurityFunctionTableW },
79
  { BUFFER_NAME_LIST_W[1], &KERBEROS_SecurityFunctionTableW },
80
  { BUFFER_NAME_LIST_W[2], &NEGOTIATE_SecurityFunctionTableW },
81
  { BUFFER_NAME_LIST_W[3], &CREDSSP_SecurityFunctionTableW },
82
  { BUFFER_NAME_LIST_W[4], &SCHANNEL_SecurityFunctionTableW }
83
};
84
85
typedef struct
86
{
87
  void* contextBuffer;
88
  UINT32 allocatorIndex;
89
} CONTEXT_BUFFER_ALLOC_ENTRY;
90
91
typedef struct
92
{
93
  UINT32 cEntries;
94
  UINT32 cMaxEntries;
95
  CONTEXT_BUFFER_ALLOC_ENTRY* entries;
96
} CONTEXT_BUFFER_ALLOC_TABLE;
97
98
static CONTEXT_BUFFER_ALLOC_TABLE ContextBufferAllocTable = WINPR_C_ARRAY_INIT;
99
100
static int sspi_ContextBufferAllocTableNew(void)
101
1
{
102
1
  size_t size = 0;
103
1
  ContextBufferAllocTable.entries = nullptr;
104
1
  ContextBufferAllocTable.cEntries = 0;
105
1
  ContextBufferAllocTable.cMaxEntries = 4;
106
1
  size = sizeof(CONTEXT_BUFFER_ALLOC_ENTRY) * ContextBufferAllocTable.cMaxEntries;
107
1
  ContextBufferAllocTable.entries = (CONTEXT_BUFFER_ALLOC_ENTRY*)calloc(1, size);
108
109
1
  if (!ContextBufferAllocTable.entries)
110
0
    return -1;
111
112
1
  return 1;
113
1
}
114
115
static int sspi_ContextBufferAllocTableGrow(void)
116
0
{
117
0
  size_t size = 0;
118
0
  CONTEXT_BUFFER_ALLOC_ENTRY* entries = nullptr;
119
0
  ContextBufferAllocTable.cEntries = 0;
120
0
  ContextBufferAllocTable.cMaxEntries *= 2;
121
0
  size = sizeof(CONTEXT_BUFFER_ALLOC_ENTRY) * ContextBufferAllocTable.cMaxEntries;
122
123
0
  if (!size)
124
0
    return -1;
125
126
0
  entries = (CONTEXT_BUFFER_ALLOC_ENTRY*)realloc(ContextBufferAllocTable.entries, size);
127
128
0
  if (!entries)
129
0
  {
130
0
    free(ContextBufferAllocTable.entries);
131
0
    return -1;
132
0
  }
133
134
0
  ContextBufferAllocTable.entries = entries;
135
0
  ZeroMemory((void*)&ContextBufferAllocTable.entries[ContextBufferAllocTable.cMaxEntries / 2],
136
0
             size / 2);
137
0
  return 1;
138
0
}
139
140
static void sspi_ContextBufferAllocTableFree(void)
141
0
{
142
0
  if (ContextBufferAllocTable.cEntries != 0)
143
0
    WLog_ERR(TAG, "ContextBufferAllocTable.entries == %" PRIu32,
144
0
             ContextBufferAllocTable.cEntries);
145
146
0
  ContextBufferAllocTable.cEntries = ContextBufferAllocTable.cMaxEntries = 0;
147
0
  free(ContextBufferAllocTable.entries);
148
0
  ContextBufferAllocTable.entries = nullptr;
149
0
}
150
151
void* sspi_ContextBufferAlloc(UINT32 allocatorIndex, size_t size)
152
1.94k
{
153
1.94k
  void* contextBuffer = nullptr;
154
155
2.44k
  for (UINT32 index = 0; index < ContextBufferAllocTable.cMaxEntries; index++)
156
2.44k
  {
157
2.44k
    if (!ContextBufferAllocTable.entries[index].contextBuffer)
158
1.94k
    {
159
1.94k
      contextBuffer = calloc(1, size);
160
161
1.94k
      if (!contextBuffer)
162
0
        return nullptr;
163
164
1.94k
      ContextBufferAllocTable.cEntries++;
165
1.94k
      ContextBufferAllocTable.entries[index].contextBuffer = contextBuffer;
166
1.94k
      ContextBufferAllocTable.entries[index].allocatorIndex = allocatorIndex;
167
1.94k
      return ContextBufferAllocTable.entries[index].contextBuffer;
168
1.94k
    }
169
2.44k
  }
170
171
  /* no available entry was found, the table needs to be grown */
172
173
0
  if (sspi_ContextBufferAllocTableGrow() < 0)
174
0
    return nullptr;
175
176
  /* the next call to sspi_ContextBufferAlloc() should now succeed */
177
0
  return sspi_ContextBufferAlloc(allocatorIndex, size);
178
0
}
179
180
SSPI_CREDENTIALS* sspi_CredentialsNew(void)
181
1.94k
{
182
1.94k
  SSPI_CREDENTIALS* credentials = nullptr;
183
1.94k
  credentials = (SSPI_CREDENTIALS*)calloc(1, sizeof(SSPI_CREDENTIALS));
184
1.94k
  return credentials;
185
1.94k
}
186
187
void sspi_CredentialsFree(SSPI_CREDENTIALS* credentials)
188
1.94k
{
189
1.94k
  size_t userLength = 0;
190
1.94k
  size_t domainLength = 0;
191
1.94k
  size_t passwordLength = 0;
192
193
1.94k
  if (!credentials)
194
0
    return;
195
196
1.94k
  if (credentials->ntlmSettings.samFile)
197
0
    free(credentials->ntlmSettings.samFile);
198
199
1.94k
  userLength = credentials->identity.UserLength;
200
1.94k
  domainLength = credentials->identity.DomainLength;
201
1.94k
  passwordLength = credentials->identity.PasswordLength;
202
203
1.94k
  if (credentials->identity.Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
204
1.13k
  {
205
1.13k
    userLength *= 2;
206
1.13k
    domainLength *= 2;
207
1.13k
    passwordLength *= 2;
208
1.13k
  }
209
210
1.94k
  if (credentials->identity.User)
211
1.20k
    memset(credentials->identity.User, 0, userLength);
212
1.94k
  if (credentials->identity.Domain)
213
1.19k
    memset(credentials->identity.Domain, 0, domainLength);
214
1.94k
  if (credentials->identity.Password)
215
1.13k
    memset(credentials->identity.Password, 0, passwordLength);
216
1.94k
  free(credentials->identity.User);
217
1.94k
  free(credentials->identity.Domain);
218
1.94k
  free(credentials->identity.Password);
219
1.94k
  free(credentials);
220
1.94k
}
221
222
void* sspi_SecBufferAlloc(PSecBuffer SecBuffer, ULONG size)
223
21.9k
{
224
21.9k
  if (!SecBuffer)
225
0
    return nullptr;
226
227
21.9k
  SecBuffer->pvBuffer = calloc(1, size);
228
229
21.9k
  if (!SecBuffer->pvBuffer)
230
0
    return nullptr;
231
232
21.9k
  SecBuffer->cbBuffer = size;
233
21.9k
  return SecBuffer->pvBuffer;
234
21.9k
}
235
236
void sspi_SecBufferFree(PSecBuffer SecBuffer)
237
159k
{
238
159k
  if (!SecBuffer)
239
0
    return;
240
241
159k
  if (SecBuffer->pvBuffer)
242
23.5k
    memset(SecBuffer->pvBuffer, 0, SecBuffer->cbBuffer);
243
244
159k
  free(SecBuffer->pvBuffer);
245
159k
  SecBuffer->pvBuffer = nullptr;
246
159k
  SecBuffer->cbBuffer = 0;
247
159k
}
248
249
SecHandle* sspi_SecureHandleAlloc(void)
250
0
{
251
0
  SecHandle* handle = (SecHandle*)calloc(1, sizeof(SecHandle));
252
253
0
  if (!handle)
254
0
    return nullptr;
255
256
0
  SecInvalidateHandle(handle);
257
0
  return handle;
258
0
}
259
260
void* sspi_SecureHandleGetLowerPointer(SecHandle* handle)
261
10.0k
{
262
10.0k
  void* pointer = nullptr;
263
264
10.0k
  if (!handle || !SecIsValidHandle(handle) || !handle->dwLower)
265
1.94k
    return nullptr;
266
267
8.12k
  pointer = (void*)~((size_t)handle->dwLower);
268
8.12k
  return pointer;
269
10.0k
}
270
271
void sspi_SecureHandleInvalidate(SecHandle* handle)
272
3.88k
{
273
3.88k
  if (!handle)
274
0
    return;
275
276
3.88k
  handle->dwLower = 0;
277
3.88k
  handle->dwUpper = 0;
278
3.88k
}
279
280
void sspi_SecureHandleSetLowerPointer(SecHandle* handle, void* pointer)
281
3.88k
{
282
3.88k
  if (!handle)
283
0
    return;
284
285
3.88k
  handle->dwLower = (ULONG_PTR)(~((size_t)pointer));
286
3.88k
}
287
288
void* sspi_SecureHandleGetUpperPointer(SecHandle* handle)
289
7.54k
{
290
7.54k
  void* pointer = nullptr;
291
292
7.54k
  if (!handle || !SecIsValidHandle(handle) || !handle->dwUpper)
293
0
    return nullptr;
294
295
7.54k
  pointer = (void*)~((size_t)handle->dwUpper);
296
7.54k
  return pointer;
297
7.54k
}
298
299
void sspi_SecureHandleSetUpperPointer(SecHandle* handle, void* pointer)
300
3.88k
{
301
3.88k
  if (!handle)
302
0
    return;
303
304
3.88k
  handle->dwUpper = (ULONG_PTR)(~((size_t)pointer));
305
3.88k
}
306
307
void sspi_SecureHandleFree(SecHandle* handle)
308
0
{
309
0
  free(handle);
310
0
}
311
312
int sspi_SetAuthIdentityW(SEC_WINNT_AUTH_IDENTITY* identity, const WCHAR* user, const WCHAR* domain,
313
                          const WCHAR* password)
314
0
{
315
0
  return sspi_SetAuthIdentityWithLengthW(identity, user, user ? _wcslen(user) : 0, domain,
316
0
                                         domain ? _wcslen(domain) : 0, password,
317
0
                                         password ? _wcslen(password) : 0);
318
0
}
319
320
static BOOL copy(WCHAR** dst, ULONG* dstLen, const WCHAR* what, size_t len)
321
3.41k
{
322
3.41k
  WINPR_ASSERT(dst);
323
3.41k
  WINPR_ASSERT(dstLen);
324
325
3.41k
  *dst = nullptr;
326
3.41k
  *dstLen = 0;
327
328
3.41k
  if (len > UINT32_MAX)
329
0
    return FALSE;
330
331
  /* Case what="" and len=0 should allocate an empty string */
332
3.41k
  if (!what && (len != 0))
333
0
    return FALSE;
334
3.41k
  if (!what && (len == 0))
335
0
    return TRUE;
336
337
3.41k
  *dst = calloc(sizeof(WCHAR), len + 1);
338
3.41k
  if (!*dst)
339
0
    return FALSE;
340
341
3.41k
  memcpy(*dst, what, len * sizeof(WCHAR));
342
3.41k
  *dstLen = WINPR_ASSERTING_INT_CAST(UINT32, len);
343
3.41k
  return TRUE;
344
3.41k
}
345
346
int sspi_SetAuthIdentityWithLengthW(SEC_WINNT_AUTH_IDENTITY* identity, const WCHAR* user,
347
                                    size_t userLen, const WCHAR* domain, size_t domainLen,
348
                                    const WCHAR* password, size_t passwordLen)
349
1.13k
{
350
1.13k
  WINPR_ASSERT(identity);
351
1.13k
  sspi_FreeAuthIdentity(identity);
352
1.13k
  identity->Flags &= (uint32_t)~SEC_WINNT_AUTH_IDENTITY_ANSI;
353
1.13k
  identity->Flags |= SEC_WINNT_AUTH_IDENTITY_UNICODE;
354
355
1.13k
  if (!copy(&identity->User, &identity->UserLength, user, userLen))
356
0
    return -1;
357
358
1.13k
  if (!copy(&identity->Domain, &identity->DomainLength, domain, domainLen))
359
0
    return -1;
360
361
1.13k
  if (!copy(&identity->Password, &identity->PasswordLength, password, passwordLen))
362
0
    return -1;
363
364
1.13k
  return 1;
365
1.13k
}
366
367
static void zfree(WCHAR* str, size_t len)
368
3.41k
{
369
3.41k
  if (str)
370
3.41k
    memset(str, 0, len * sizeof(WCHAR));
371
3.41k
  free(str);
372
3.41k
}
373
374
int sspi_SetAuthIdentityA(SEC_WINNT_AUTH_IDENTITY* identity, const char* user, const char* domain,
375
                          const char* password)
376
1.13k
{
377
1.13k
  int rc = 0;
378
1.13k
  size_t unicodeUserLenW = 0;
379
1.13k
  size_t unicodeDomainLenW = 0;
380
1.13k
  size_t unicodePasswordLenW = 0;
381
1.13k
  LPWSTR unicodeUser = nullptr;
382
1.13k
  LPWSTR unicodeDomain = nullptr;
383
1.13k
  LPWSTR unicodePassword = nullptr;
384
385
1.13k
  if (user)
386
1.13k
    unicodeUser = ConvertUtf8ToWCharAlloc(user, &unicodeUserLenW);
387
388
1.13k
  if (domain)
389
1.13k
    unicodeDomain = ConvertUtf8ToWCharAlloc(domain, &unicodeDomainLenW);
390
391
1.13k
  if (password)
392
1.13k
    unicodePassword = ConvertUtf8ToWCharAlloc(password, &unicodePasswordLenW);
393
394
1.13k
  rc = sspi_SetAuthIdentityWithLengthW(identity, unicodeUser, unicodeUserLenW, unicodeDomain,
395
1.13k
                                       unicodeDomainLenW, unicodePassword, unicodePasswordLenW);
396
397
1.13k
  zfree(unicodeUser, unicodeUserLenW);
398
1.13k
  zfree(unicodeDomain, unicodeDomainLenW);
399
1.13k
  zfree(unicodePassword, unicodePasswordLenW);
400
1.13k
  return rc;
401
1.13k
}
402
403
UINT32 sspi_GetAuthIdentityVersion(const void* identity)
404
4.55k
{
405
4.55k
  UINT32 version = 0;
406
407
4.55k
  if (!identity)
408
0
    return 0;
409
410
4.55k
  version = *((const UINT32*)identity);
411
412
4.55k
  if ((version == SEC_WINNT_AUTH_IDENTITY_VERSION) ||
413
4.55k
      (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2))
414
0
  {
415
0
    return version;
416
0
  }
417
418
4.55k
  return 0; // SEC_WINNT_AUTH_IDENTITY (no version)
419
4.55k
}
420
421
UINT32 sspi_GetAuthIdentityFlags(const void* identity)
422
2.27k
{
423
2.27k
  UINT32 version = 0;
424
2.27k
  UINT32 flags = 0;
425
426
2.27k
  if (!identity)
427
0
    return 0;
428
429
2.27k
  version = sspi_GetAuthIdentityVersion(identity);
430
431
2.27k
  if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
432
0
  {
433
0
    flags = ((const SEC_WINNT_AUTH_IDENTITY_EX*)identity)->Flags;
434
0
  }
435
2.27k
  else if (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2)
436
0
  {
437
0
    flags = ((const SEC_WINNT_AUTH_IDENTITY_EX2*)identity)->Flags;
438
0
  }
439
2.27k
  else // SEC_WINNT_AUTH_IDENTITY
440
2.27k
  {
441
2.27k
    flags = ((const SEC_WINNT_AUTH_IDENTITY*)identity)->Flags;
442
2.27k
  }
443
444
2.27k
  return flags;
445
2.27k
}
446
447
BOOL sspi_GetAuthIdentityUserDomainW(const void* identity, const WCHAR** pUser, UINT32* pUserLength,
448
                                     const WCHAR** pDomain, UINT32* pDomainLength)
449
1.13k
{
450
1.13k
  UINT32 version = 0;
451
452
1.13k
  if (!identity)
453
0
    return FALSE;
454
455
1.13k
  version = sspi_GetAuthIdentityVersion(identity);
456
457
1.13k
  if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
458
0
  {
459
0
    const SEC_WINNT_AUTH_IDENTITY_EXW* id = (const SEC_WINNT_AUTH_IDENTITY_EXW*)identity;
460
0
    *pUser = (const WCHAR*)id->User;
461
0
    *pUserLength = id->UserLength;
462
0
    *pDomain = (const WCHAR*)id->Domain;
463
0
    *pDomainLength = id->DomainLength;
464
0
  }
465
1.13k
  else if (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2)
466
0
  {
467
0
    const SEC_WINNT_AUTH_IDENTITY_EX2* id = (const SEC_WINNT_AUTH_IDENTITY_EX2*)identity;
468
0
    UINT32 UserOffset = id->UserOffset;
469
0
    UINT32 DomainOffset = id->DomainOffset;
470
0
    *pUser = (const WCHAR*)&((const uint8_t*)identity)[UserOffset];
471
0
    *pUserLength = id->UserLength / 2;
472
0
    *pDomain = (const WCHAR*)&((const uint8_t*)identity)[DomainOffset];
473
0
    *pDomainLength = id->DomainLength / 2;
474
0
  }
475
1.13k
  else // SEC_WINNT_AUTH_IDENTITY
476
1.13k
  {
477
1.13k
    const SEC_WINNT_AUTH_IDENTITY_W* id = (const SEC_WINNT_AUTH_IDENTITY_W*)identity;
478
1.13k
    *pUser = (const WCHAR*)id->User;
479
1.13k
    *pUserLength = id->UserLength;
480
1.13k
    *pDomain = (const WCHAR*)id->Domain;
481
1.13k
    *pDomainLength = id->DomainLength;
482
1.13k
  }
483
484
1.13k
  return TRUE;
485
1.13k
}
486
487
BOOL sspi_GetAuthIdentityUserDomainA(const void* identity, const char** pUser, UINT32* pUserLength,
488
                                     const char** pDomain, UINT32* pDomainLength)
489
0
{
490
0
  UINT32 version = 0;
491
492
0
  if (!identity)
493
0
    return FALSE;
494
495
0
  version = sspi_GetAuthIdentityVersion(identity);
496
497
0
  if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
498
0
  {
499
0
    const SEC_WINNT_AUTH_IDENTITY_EXA* id = (const SEC_WINNT_AUTH_IDENTITY_EXA*)identity;
500
0
    *pUser = (const char*)id->User;
501
0
    *pUserLength = id->UserLength;
502
0
    *pDomain = (const char*)id->Domain;
503
0
    *pDomainLength = id->DomainLength;
504
0
  }
505
0
  else if (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2)
506
0
  {
507
0
    const SEC_WINNT_AUTH_IDENTITY_EX2* id = (const SEC_WINNT_AUTH_IDENTITY_EX2*)identity;
508
0
    UINT32 UserOffset = id->UserOffset;
509
0
    UINT32 DomainOffset = id->DomainOffset;
510
0
    *pUser = (const char*)&((const uint8_t*)identity)[UserOffset];
511
0
    *pUserLength = id->UserLength;
512
0
    *pDomain = (const char*)&((const uint8_t*)identity)[DomainOffset];
513
0
    *pDomainLength = id->DomainLength;
514
0
  }
515
0
  else // SEC_WINNT_AUTH_IDENTITY
516
0
  {
517
0
    const SEC_WINNT_AUTH_IDENTITY_A* id = (const SEC_WINNT_AUTH_IDENTITY_A*)identity;
518
0
    *pUser = (const char*)id->User;
519
0
    *pUserLength = id->UserLength;
520
0
    *pDomain = (const char*)id->Domain;
521
0
    *pDomainLength = id->DomainLength;
522
0
  }
523
524
0
  return TRUE;
525
0
}
526
527
BOOL sspi_GetAuthIdentityPasswordW(const void* identity, const WCHAR** pPassword,
528
                                   UINT32* pPasswordLength)
529
1.13k
{
530
1.13k
  UINT32 version = 0;
531
532
1.13k
  if (!identity)
533
0
    return FALSE;
534
535
1.13k
  version = sspi_GetAuthIdentityVersion(identity);
536
537
1.13k
  if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
538
0
  {
539
0
    const SEC_WINNT_AUTH_IDENTITY_EXW* id = (const SEC_WINNT_AUTH_IDENTITY_EXW*)identity;
540
0
    *pPassword = (const WCHAR*)id->Password;
541
0
    *pPasswordLength = id->PasswordLength;
542
0
  }
543
1.13k
  else if (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2)
544
0
  {
545
0
    return FALSE; // TODO: packed credentials
546
0
  }
547
1.13k
  else // SEC_WINNT_AUTH_IDENTITY
548
1.13k
  {
549
1.13k
    const SEC_WINNT_AUTH_IDENTITY_W* id = (const SEC_WINNT_AUTH_IDENTITY_W*)identity;
550
1.13k
    *pPassword = (const WCHAR*)id->Password;
551
1.13k
    *pPasswordLength = id->PasswordLength;
552
1.13k
  }
553
554
1.13k
  return TRUE;
555
1.13k
}
556
557
BOOL sspi_GetAuthIdentityPasswordA(const void* identity, const char** pPassword,
558
                                   UINT32* pPasswordLength)
559
0
{
560
0
  UINT32 version = 0;
561
562
0
  if (!identity)
563
0
    return FALSE;
564
565
0
  version = sspi_GetAuthIdentityVersion(identity);
566
567
0
  if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
568
0
  {
569
0
    const SEC_WINNT_AUTH_IDENTITY_EXA* id = (const SEC_WINNT_AUTH_IDENTITY_EXA*)identity;
570
0
    *pPassword = (const char*)id->Password;
571
0
    *pPasswordLength = id->PasswordLength;
572
0
  }
573
0
  else if (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2)
574
0
  {
575
0
    return FALSE; // TODO: packed credentials
576
0
  }
577
0
  else // SEC_WINNT_AUTH_IDENTITY
578
0
  {
579
0
    const SEC_WINNT_AUTH_IDENTITY_A* id = (const SEC_WINNT_AUTH_IDENTITY_A*)identity;
580
0
    *pPassword = (const char*)id->Password;
581
0
    *pPasswordLength = id->PasswordLength;
582
0
  }
583
584
0
  return TRUE;
585
0
}
586
587
BOOL sspi_CopyAuthIdentityFieldsA(const SEC_WINNT_AUTH_IDENTITY_INFO* identity, char** pUser,
588
                                  char** pDomain, char** pPassword)
589
0
{
590
0
  BOOL success = FALSE;
591
0
  const char* UserA = nullptr;
592
0
  const char* DomainA = nullptr;
593
0
  const char* PasswordA = nullptr;
594
0
  const WCHAR* UserW = nullptr;
595
0
  const WCHAR* DomainW = nullptr;
596
0
  const WCHAR* PasswordW = nullptr;
597
0
  UINT32 UserLength = 0;
598
0
  UINT32 DomainLength = 0;
599
0
  UINT32 PasswordLength = 0;
600
601
0
  if (!identity || !pUser || !pDomain || !pPassword)
602
0
    return FALSE;
603
604
0
  *pUser = *pDomain = *pPassword = nullptr;
605
606
0
  UINT32 identityFlags = sspi_GetAuthIdentityFlags(identity);
607
608
0
  if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_ANSI) != 0)
609
0
  {
610
0
    if (!sspi_GetAuthIdentityUserDomainA(identity, &UserA, &UserLength, &DomainA,
611
0
                                         &DomainLength))
612
0
      goto cleanup;
613
614
0
    if (!sspi_GetAuthIdentityPasswordA(identity, &PasswordA, &PasswordLength))
615
0
      goto cleanup;
616
617
0
    if (UserA && UserLength)
618
0
    {
619
0
      *pUser = _strdup(UserA);
620
621
0
      if (!(*pUser))
622
0
        goto cleanup;
623
0
    }
624
625
0
    if (DomainA && DomainLength)
626
0
    {
627
0
      *pDomain = _strdup(DomainA);
628
629
0
      if (!(*pDomain))
630
0
        goto cleanup;
631
0
    }
632
633
0
    if (PasswordA && PasswordLength)
634
0
    {
635
0
      *pPassword = _strdup(PasswordA);
636
637
0
      if (!(*pPassword))
638
0
        goto cleanup;
639
0
    }
640
641
0
    success = TRUE;
642
0
  }
643
0
  else if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_UNICODE) != 0)
644
0
  {
645
0
    if (!sspi_GetAuthIdentityUserDomainW(identity, &UserW, &UserLength, &DomainW,
646
0
                                         &DomainLength))
647
0
      goto cleanup;
648
649
0
    if (!sspi_GetAuthIdentityPasswordW(identity, &PasswordW, &PasswordLength))
650
0
      goto cleanup;
651
652
0
    if (UserW && (UserLength > 0))
653
0
    {
654
0
      *pUser = ConvertWCharNToUtf8Alloc(UserW, UserLength, nullptr);
655
0
      if (!(*pUser))
656
0
        goto cleanup;
657
0
    }
658
659
0
    if (DomainW && (DomainLength > 0))
660
0
    {
661
0
      *pDomain = ConvertWCharNToUtf8Alloc(DomainW, DomainLength, nullptr);
662
0
      if (!(*pDomain))
663
0
        goto cleanup;
664
0
    }
665
666
0
    if (PasswordW && (PasswordLength > 0))
667
0
    {
668
0
      *pPassword = ConvertWCharNToUtf8Alloc(PasswordW, PasswordLength, nullptr);
669
0
      if (!(*pPassword))
670
0
        goto cleanup;
671
0
    }
672
673
0
    success = TRUE;
674
0
  }
675
676
0
cleanup:
677
0
  return success;
678
0
}
679
680
BOOL sspi_CopyAuthIdentityFieldsW(const SEC_WINNT_AUTH_IDENTITY_INFO* identity, WCHAR** pUser,
681
                                  WCHAR** pDomain, WCHAR** pPassword)
682
0
{
683
0
  BOOL success = FALSE;
684
0
  const char* UserA = nullptr;
685
0
  const char* DomainA = nullptr;
686
0
  const char* PasswordA = nullptr;
687
0
  const WCHAR* UserW = nullptr;
688
0
  const WCHAR* DomainW = nullptr;
689
0
  const WCHAR* PasswordW = nullptr;
690
0
  UINT32 UserLength = 0;
691
0
  UINT32 DomainLength = 0;
692
0
  UINT32 PasswordLength = 0;
693
694
0
  if (!identity || !pUser || !pDomain || !pPassword)
695
0
    return FALSE;
696
697
0
  *pUser = *pDomain = *pPassword = nullptr;
698
699
0
  UINT32 identityFlags = sspi_GetAuthIdentityFlags(identity);
700
701
0
  if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_ANSI) != 0)
702
0
  {
703
0
    if (!sspi_GetAuthIdentityUserDomainA(identity, &UserA, &UserLength, &DomainA,
704
0
                                         &DomainLength))
705
0
      goto cleanup;
706
707
0
    if (!sspi_GetAuthIdentityPasswordA(identity, &PasswordA, &PasswordLength))
708
0
      goto cleanup;
709
710
0
    if (UserA && (UserLength > 0))
711
0
    {
712
0
      WCHAR* ptr = ConvertUtf8NToWCharAlloc(UserA, UserLength, nullptr);
713
0
      *pUser = ptr;
714
715
0
      if (!ptr)
716
0
        goto cleanup;
717
0
    }
718
719
0
    if (DomainA && (DomainLength > 0))
720
0
    {
721
0
      WCHAR* ptr = ConvertUtf8NToWCharAlloc(DomainA, DomainLength, nullptr);
722
0
      *pDomain = ptr;
723
0
      if (!ptr)
724
0
        goto cleanup;
725
0
    }
726
727
0
    if (PasswordA && (PasswordLength > 0))
728
0
    {
729
0
      WCHAR* ptr = ConvertUtf8NToWCharAlloc(PasswordA, PasswordLength, nullptr);
730
731
0
      *pPassword = ptr;
732
0
      if (!ptr)
733
0
        goto cleanup;
734
0
    }
735
736
0
    success = TRUE;
737
0
  }
738
0
  else if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_UNICODE) != 0)
739
0
  {
740
0
    if (!sspi_GetAuthIdentityUserDomainW(identity, &UserW, &UserLength, &DomainW,
741
0
                                         &DomainLength))
742
0
      goto cleanup;
743
744
0
    if (!sspi_GetAuthIdentityPasswordW(identity, &PasswordW, &PasswordLength))
745
0
      goto cleanup;
746
747
0
    if (UserW && UserLength)
748
0
    {
749
0
      *pUser = winpr_wcsndup(UserW, UserLength / sizeof(WCHAR));
750
751
0
      if (!(*pUser))
752
0
        goto cleanup;
753
0
    }
754
755
0
    if (DomainW && DomainLength)
756
0
    {
757
0
      *pDomain = winpr_wcsndup(DomainW, DomainLength / sizeof(WCHAR));
758
759
0
      if (!(*pDomain))
760
0
        goto cleanup;
761
0
    }
762
763
0
    if (PasswordW && PasswordLength)
764
0
    {
765
0
      *pPassword = winpr_wcsndup(PasswordW, PasswordLength / sizeof(WCHAR));
766
767
0
      if (!(*pPassword))
768
0
        goto cleanup;
769
0
    }
770
771
0
    success = TRUE;
772
0
  }
773
774
0
cleanup:
775
0
  return success;
776
0
}
777
778
BOOL sspi_CopyAuthPackageListA(const SEC_WINNT_AUTH_IDENTITY_INFO* identity, char** pPackageList)
779
0
{
780
0
  UINT32 version = 0;
781
0
  UINT32 identityFlags = 0;
782
0
  char* PackageList = nullptr;
783
0
  const char* PackageListA = nullptr;
784
0
  const WCHAR* PackageListW = nullptr;
785
0
  UINT32 PackageListLength = 0;
786
0
  UINT32 PackageListOffset = 0;
787
0
  const void* pAuthData = (const void*)identity;
788
789
0
  if (!pAuthData)
790
0
    return FALSE;
791
792
0
  version = sspi_GetAuthIdentityVersion(pAuthData);
793
0
  identityFlags = sspi_GetAuthIdentityFlags(pAuthData);
794
795
0
  if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_ANSI) != 0)
796
0
  {
797
0
    if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
798
0
    {
799
0
      const SEC_WINNT_AUTH_IDENTITY_EXA* ad = (const SEC_WINNT_AUTH_IDENTITY_EXA*)pAuthData;
800
0
      PackageListA = (const char*)ad->PackageList;
801
0
      PackageListLength = ad->PackageListLength;
802
0
    }
803
804
0
    if (PackageListA && PackageListLength)
805
0
    {
806
0
      PackageList = _strdup(PackageListA);
807
0
    }
808
0
  }
809
0
  else if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_UNICODE) != 0)
810
0
  {
811
0
    if (version == SEC_WINNT_AUTH_IDENTITY_VERSION)
812
0
    {
813
0
      const SEC_WINNT_AUTH_IDENTITY_EXW* ad = (const SEC_WINNT_AUTH_IDENTITY_EXW*)pAuthData;
814
0
      PackageListW = (const WCHAR*)ad->PackageList;
815
0
      PackageListLength = ad->PackageListLength;
816
0
    }
817
0
    else if (version == SEC_WINNT_AUTH_IDENTITY_VERSION_2)
818
0
    {
819
0
      const SEC_WINNT_AUTH_IDENTITY_EX2* ad = (const SEC_WINNT_AUTH_IDENTITY_EX2*)pAuthData;
820
0
      PackageListOffset = ad->PackageListOffset;
821
0
      PackageListW = (const WCHAR*)&((const uint8_t*)pAuthData)[PackageListOffset];
822
0
      PackageListLength = ad->PackageListLength / 2;
823
0
    }
824
825
0
    if (PackageListW && (PackageListLength > 0))
826
0
      PackageList = ConvertWCharNToUtf8Alloc(PackageListW, PackageListLength, nullptr);
827
0
  }
828
829
0
  if (PackageList)
830
0
  {
831
0
    *pPackageList = PackageList;
832
0
    return TRUE;
833
0
  }
834
835
0
  return FALSE;
836
0
}
837
838
int sspi_CopyAuthIdentity(SEC_WINNT_AUTH_IDENTITY* identity,
839
                          const SEC_WINNT_AUTH_IDENTITY_INFO* srcIdentity)
840
1.13k
{
841
1.13k
  int status = 0;
842
1.13k
  UINT32 identityFlags = 0;
843
1.13k
  const char* UserA = nullptr;
844
1.13k
  const char* DomainA = nullptr;
845
1.13k
  const char* PasswordA = nullptr;
846
1.13k
  const WCHAR* UserW = nullptr;
847
1.13k
  const WCHAR* DomainW = nullptr;
848
1.13k
  const WCHAR* PasswordW = nullptr;
849
1.13k
  UINT32 UserLength = 0;
850
1.13k
  UINT32 DomainLength = 0;
851
1.13k
  UINT32 PasswordLength = 0;
852
853
1.13k
  sspi_FreeAuthIdentity(identity);
854
855
1.13k
  identityFlags = sspi_GetAuthIdentityFlags(srcIdentity);
856
857
1.13k
  identity->Flags = identityFlags;
858
859
1.13k
  if ((identityFlags & SEC_WINNT_AUTH_IDENTITY_ANSI) != 0)
860
0
  {
861
0
    if (!sspi_GetAuthIdentityUserDomainA(srcIdentity, &UserA, &UserLength, &DomainA,
862
0
                                         &DomainLength))
863
0
    {
864
0
      return -1;
865
0
    }
866
867
0
    if (!sspi_GetAuthIdentityPasswordA(srcIdentity, &PasswordA, &PasswordLength))
868
0
    {
869
0
      return -1;
870
0
    }
871
872
0
    status = sspi_SetAuthIdentity(identity, UserA, DomainA, PasswordA);
873
874
0
    if (status <= 0)
875
0
      return -1;
876
877
0
    identity->Flags &= (uint32_t)~SEC_WINNT_AUTH_IDENTITY_ANSI;
878
0
    identity->Flags |= SEC_WINNT_AUTH_IDENTITY_UNICODE;
879
0
    return 1;
880
0
  }
881
882
1.13k
  identity->Flags |= SEC_WINNT_AUTH_IDENTITY_UNICODE;
883
884
1.13k
  if (!sspi_GetAuthIdentityUserDomainW(srcIdentity, &UserW, &UserLength, &DomainW, &DomainLength))
885
0
  {
886
0
    return -1;
887
0
  }
888
889
1.13k
  if (!sspi_GetAuthIdentityPasswordW(srcIdentity, &PasswordW, &PasswordLength))
890
0
  {
891
0
    return -1;
892
0
  }
893
894
  /* login/password authentication */
895
1.13k
  identity->UserLength = UserLength;
896
897
1.13k
  if (identity->UserLength > 0)
898
1.13k
  {
899
1.13k
    identity->User = (UINT16*)calloc((identity->UserLength + 1), sizeof(WCHAR));
900
901
1.13k
    if (!identity->User)
902
0
      return -1;
903
904
1.13k
    CopyMemory(identity->User, UserW, identity->UserLength * sizeof(WCHAR));
905
1.13k
    identity->User[identity->UserLength] = 0;
906
1.13k
  }
907
908
1.13k
  identity->DomainLength = DomainLength;
909
910
1.13k
  if (identity->DomainLength > 0)
911
1.13k
  {
912
1.13k
    identity->Domain = (UINT16*)calloc((identity->DomainLength + 1), sizeof(WCHAR));
913
914
1.13k
    if (!identity->Domain)
915
0
      return -1;
916
917
1.13k
    CopyMemory(identity->Domain, DomainW, identity->DomainLength * sizeof(WCHAR));
918
1.13k
    identity->Domain[identity->DomainLength] = 0;
919
1.13k
  }
920
921
1.13k
  identity->PasswordLength = PasswordLength;
922
923
1.13k
  if (PasswordW)
924
1.13k
  {
925
1.13k
    identity->Password = (UINT16*)calloc((identity->PasswordLength + 1), sizeof(WCHAR));
926
927
1.13k
    if (!identity->Password)
928
0
      return -1;
929
930
1.13k
    CopyMemory(identity->Password, PasswordW, identity->PasswordLength * sizeof(WCHAR));
931
1.13k
    identity->Password[identity->PasswordLength] = 0;
932
1.13k
  }
933
934
  /* End of login/password authentication */
935
1.13k
  return 1;
936
1.13k
}
937
938
PSecBuffer sspi_FindSecBuffer(PSecBufferDesc pMessage, ULONG BufferType)
939
4.49k
{
940
4.49k
  PSecBuffer pSecBuffer = nullptr;
941
942
5.12k
  for (UINT32 index = 0; index < pMessage->cBuffers; index++)
943
4.49k
  {
944
4.49k
    if (pMessage->pBuffers[index].BufferType == BufferType)
945
3.86k
    {
946
3.86k
      pSecBuffer = &pMessage->pBuffers[index];
947
3.86k
      break;
948
3.86k
    }
949
4.49k
  }
950
951
4.49k
  return pSecBuffer;
952
4.49k
}
953
954
static BOOL WINPR_init(void)
955
1
{
956
957
6
  for (size_t x = 0; x < ARRAYSIZE(SecurityFunctionTableA_NAME_LIST); x++)
958
5
  {
959
5
    const SecurityFunctionTableA_NAME* cur = &SecurityFunctionTableA_NAME_LIST[x];
960
5
    InitializeConstWCharFromUtf8(cur->Name, BUFFER_NAME_LIST_W[x],
961
5
                                 ARRAYSIZE(BUFFER_NAME_LIST_W[x]));
962
5
  }
963
1
  return TRUE;
964
1
}
965
966
static BOOL CALLBACK sspi_init(WINPR_ATTR_UNUSED PINIT_ONCE InitOnce,
967
                               WINPR_ATTR_UNUSED PVOID Parameter, WINPR_ATTR_UNUSED PVOID* Context)
968
1
{
969
1
  if (!winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT))
970
0
    return FALSE;
971
1
  sspi_ContextBufferAllocTableNew();
972
1
  if (!SCHANNEL_init())
973
0
    return FALSE;
974
1
  if (!KERBEROS_init())
975
0
    return FALSE;
976
1
  if (!NTLM_init())
977
0
    return FALSE;
978
1
  if (!CREDSSP_init())
979
0
    return FALSE;
980
1
  if (!NEGOTIATE_init())
981
0
    return FALSE;
982
1
  return WINPR_init();
983
1
}
984
985
void sspi_GlobalInit(void)
986
1
{
987
1
  static INIT_ONCE once = INIT_ONCE_STATIC_INIT;
988
1
  DWORD flags = 0;
989
1
  if (!InitOnceExecuteOnce(&once, sspi_init, &flags, nullptr))
990
0
    WLog_ERR(TAG, "InitOnceExecuteOnce failed");
991
1
}
992
993
void sspi_GlobalFinish(void)
994
0
{
995
0
  sspi_ContextBufferAllocTableFree();
996
0
}
997
998
static const SecurityFunctionTableA* sspi_GetSecurityFunctionTableAByNameA(const SEC_CHAR* Name)
999
9.49k
{
1000
9.49k
  size_t cPackages = ARRAYSIZE(SecPkgInfoA_LIST);
1001
1002
9.49k
  for (size_t index = 0; index < cPackages; index++)
1003
9.49k
  {
1004
9.49k
    if (strcmp(Name, SecurityFunctionTableA_NAME_LIST[index].Name) == 0)
1005
9.49k
    {
1006
9.49k
      return SecurityFunctionTableA_NAME_LIST[index].SecurityFunctionTable;
1007
9.49k
    }
1008
9.49k
  }
1009
1010
0
  return nullptr;
1011
9.49k
}
1012
1013
static const SecurityFunctionTableW* sspi_GetSecurityFunctionTableWByNameW(const SEC_WCHAR* Name)
1014
0
{
1015
0
  size_t cPackages = ARRAYSIZE(SecPkgInfoW_LIST);
1016
1017
0
  for (size_t index = 0; index < cPackages; index++)
1018
0
  {
1019
0
    if (_wcscmp(Name, SecurityFunctionTableW_NAME_LIST[index].Name) == 0)
1020
0
    {
1021
0
      return SecurityFunctionTableW_NAME_LIST[index].SecurityFunctionTable;
1022
0
    }
1023
0
  }
1024
1025
0
  return nullptr;
1026
0
}
1027
1028
static const SecurityFunctionTableW* sspi_GetSecurityFunctionTableWByNameA(const SEC_CHAR* Name)
1029
0
{
1030
0
  if (!Name)
1031
0
    return nullptr;
1032
1033
0
  WCHAR* NameW = ConvertUtf8ToWCharAlloc(Name, nullptr);
1034
1035
0
  if (!NameW)
1036
0
    return nullptr;
1037
1038
0
  const SecurityFunctionTableW* table = sspi_GetSecurityFunctionTableWByNameW(NameW);
1039
0
  free(NameW);
1040
0
  return table;
1041
0
}
1042
1043
static void FreeContextBuffer_EnumerateSecurityPackages(void* contextBuffer);
1044
static void FreeContextBuffer_QuerySecurityPackageInfo(void* contextBuffer);
1045
1046
void sspi_ContextBufferFree(void* contextBuffer)
1047
1.94k
{
1048
1.94k
  UINT32 allocatorIndex = 0;
1049
1050
9.71k
  for (size_t index = 0; index < ContextBufferAllocTable.cMaxEntries; index++)
1051
7.77k
  {
1052
7.77k
    if (contextBuffer == ContextBufferAllocTable.entries[index].contextBuffer)
1053
1.94k
    {
1054
1.94k
      contextBuffer = ContextBufferAllocTable.entries[index].contextBuffer;
1055
1.94k
      allocatorIndex = ContextBufferAllocTable.entries[index].allocatorIndex;
1056
1.94k
      ContextBufferAllocTable.cEntries--;
1057
1.94k
      ContextBufferAllocTable.entries[index].allocatorIndex = 0;
1058
1.94k
      ContextBufferAllocTable.entries[index].contextBuffer = nullptr;
1059
1060
1.94k
      switch (allocatorIndex)
1061
1.94k
      {
1062
0
        case EnumerateSecurityPackagesIndex:
1063
0
          FreeContextBuffer_EnumerateSecurityPackages(contextBuffer);
1064
0
          break;
1065
1066
1.94k
        case QuerySecurityPackageInfoIndex:
1067
1.94k
          FreeContextBuffer_QuerySecurityPackageInfo(contextBuffer);
1068
1.94k
          break;
1069
0
        default:
1070
0
          break;
1071
1.94k
      }
1072
1.94k
    }
1073
7.77k
  }
1074
1.94k
}
1075
1076
/**
1077
 * Standard SSPI API
1078
 */
1079
1080
/* Package Management */
1081
1082
static SECURITY_STATUS SEC_ENTRY winpr_EnumerateSecurityPackagesW(ULONG* pcPackages,
1083
                                                                  PSecPkgInfoW* ppPackageInfo)
1084
0
{
1085
0
  const size_t cPackages = ARRAYSIZE(SecPkgInfoW_LIST);
1086
0
  const size_t size = sizeof(SecPkgInfoW) * cPackages;
1087
0
  SecPkgInfoW* pPackageInfo =
1088
0
      (SecPkgInfoW*)sspi_ContextBufferAlloc(EnumerateSecurityPackagesIndex, size);
1089
1090
0
  WINPR_ASSERT(cPackages <= UINT32_MAX);
1091
1092
0
  if (!pPackageInfo)
1093
0
    return SEC_E_INSUFFICIENT_MEMORY;
1094
1095
0
  for (size_t index = 0; index < cPackages; index++)
1096
0
  {
1097
0
    pPackageInfo[index].fCapabilities = SecPkgInfoW_LIST[index]->fCapabilities;
1098
0
    pPackageInfo[index].wVersion = SecPkgInfoW_LIST[index]->wVersion;
1099
0
    pPackageInfo[index].wRPCID = SecPkgInfoW_LIST[index]->wRPCID;
1100
0
    pPackageInfo[index].cbMaxToken = SecPkgInfoW_LIST[index]->cbMaxToken;
1101
0
    pPackageInfo[index].Name = _wcsdup(SecPkgInfoW_LIST[index]->Name);
1102
0
    pPackageInfo[index].Comment = _wcsdup(SecPkgInfoW_LIST[index]->Comment);
1103
0
  }
1104
1105
0
  *(pcPackages) = (UINT32)cPackages;
1106
0
  *(ppPackageInfo) = pPackageInfo;
1107
0
  return SEC_E_OK;
1108
0
}
1109
1110
static SECURITY_STATUS SEC_ENTRY winpr_EnumerateSecurityPackagesA(ULONG* pcPackages,
1111
                                                                  PSecPkgInfoA* ppPackageInfo)
1112
0
{
1113
0
  const size_t cPackages = ARRAYSIZE(SecPkgInfoA_LIST);
1114
0
  const size_t size = sizeof(SecPkgInfoA) * cPackages;
1115
0
  SecPkgInfoA* pPackageInfo =
1116
0
      (SecPkgInfoA*)sspi_ContextBufferAlloc(EnumerateSecurityPackagesIndex, size);
1117
1118
0
  WINPR_ASSERT(cPackages <= UINT32_MAX);
1119
1120
0
  if (!pPackageInfo)
1121
0
    return SEC_E_INSUFFICIENT_MEMORY;
1122
1123
0
  for (size_t index = 0; index < cPackages; index++)
1124
0
  {
1125
0
    pPackageInfo[index].fCapabilities = SecPkgInfoA_LIST[index]->fCapabilities;
1126
0
    pPackageInfo[index].wVersion = SecPkgInfoA_LIST[index]->wVersion;
1127
0
    pPackageInfo[index].wRPCID = SecPkgInfoA_LIST[index]->wRPCID;
1128
0
    pPackageInfo[index].cbMaxToken = SecPkgInfoA_LIST[index]->cbMaxToken;
1129
0
    pPackageInfo[index].Name = _strdup(SecPkgInfoA_LIST[index]->Name);
1130
0
    pPackageInfo[index].Comment = _strdup(SecPkgInfoA_LIST[index]->Comment);
1131
1132
0
    if (!pPackageInfo[index].Name || !pPackageInfo[index].Comment)
1133
0
    {
1134
0
      sspi_ContextBufferFree(pPackageInfo);
1135
0
      return SEC_E_INSUFFICIENT_MEMORY;
1136
0
    }
1137
0
  }
1138
1139
0
  *(pcPackages) = (UINT32)cPackages;
1140
0
  *(ppPackageInfo) = pPackageInfo;
1141
0
  return SEC_E_OK;
1142
0
}
1143
1144
static void FreeContextBuffer_EnumerateSecurityPackages(void* contextBuffer)
1145
0
{
1146
0
  SecPkgInfoA* pPackageInfo = (SecPkgInfoA*)contextBuffer;
1147
0
  size_t cPackages = ARRAYSIZE(SecPkgInfoA_LIST);
1148
1149
0
  if (!pPackageInfo)
1150
0
    return;
1151
1152
0
  for (size_t index = 0; index < cPackages; index++)
1153
0
  {
1154
0
    free(pPackageInfo[index].Name);
1155
0
    free(pPackageInfo[index].Comment);
1156
0
  }
1157
1158
0
  free(pPackageInfo);
1159
0
}
1160
1161
static SECURITY_STATUS SEC_ENTRY winpr_QuerySecurityPackageInfoW(SEC_WCHAR* pszPackageName,
1162
                                                                 PSecPkgInfoW* ppPackageInfo)
1163
0
{
1164
0
  size_t cPackages = ARRAYSIZE(SecPkgInfoW_LIST);
1165
1166
0
  for (size_t index = 0; index < cPackages; index++)
1167
0
  {
1168
0
    if (_wcscmp(pszPackageName, SecPkgInfoW_LIST[index]->Name) == 0)
1169
0
    {
1170
0
      size_t size = sizeof(SecPkgInfoW);
1171
0
      SecPkgInfoW* pPackageInfo =
1172
0
          (SecPkgInfoW*)sspi_ContextBufferAlloc(QuerySecurityPackageInfoIndex, size);
1173
1174
0
      if (!pPackageInfo)
1175
0
        return SEC_E_INSUFFICIENT_MEMORY;
1176
1177
0
      pPackageInfo->fCapabilities = SecPkgInfoW_LIST[index]->fCapabilities;
1178
0
      pPackageInfo->wVersion = SecPkgInfoW_LIST[index]->wVersion;
1179
0
      pPackageInfo->wRPCID = SecPkgInfoW_LIST[index]->wRPCID;
1180
0
      pPackageInfo->cbMaxToken = SecPkgInfoW_LIST[index]->cbMaxToken;
1181
0
      pPackageInfo->Name = _wcsdup(SecPkgInfoW_LIST[index]->Name);
1182
0
      pPackageInfo->Comment = _wcsdup(SecPkgInfoW_LIST[index]->Comment);
1183
0
      *(ppPackageInfo) = pPackageInfo;
1184
0
      return SEC_E_OK;
1185
0
    }
1186
0
  }
1187
1188
0
  *(ppPackageInfo) = nullptr;
1189
0
  return SEC_E_SECPKG_NOT_FOUND;
1190
0
}
1191
1192
static SECURITY_STATUS SEC_ENTRY winpr_QuerySecurityPackageInfoA(SEC_CHAR* pszPackageName,
1193
                                                                 PSecPkgInfoA* ppPackageInfo)
1194
1.94k
{
1195
1.94k
  size_t cPackages = ARRAYSIZE(SecPkgInfoA_LIST);
1196
1197
1.94k
  for (size_t index = 0; index < cPackages; index++)
1198
1.94k
  {
1199
1.94k
    if (strcmp(pszPackageName, SecPkgInfoA_LIST[index]->Name) == 0)
1200
1.94k
    {
1201
1.94k
      size_t size = sizeof(SecPkgInfoA);
1202
1.94k
      SecPkgInfoA* pPackageInfo =
1203
1.94k
          (SecPkgInfoA*)sspi_ContextBufferAlloc(QuerySecurityPackageInfoIndex, size);
1204
1205
1.94k
      if (!pPackageInfo)
1206
0
        return SEC_E_INSUFFICIENT_MEMORY;
1207
1208
1.94k
      pPackageInfo->fCapabilities = SecPkgInfoA_LIST[index]->fCapabilities;
1209
1.94k
      pPackageInfo->wVersion = SecPkgInfoA_LIST[index]->wVersion;
1210
1.94k
      pPackageInfo->wRPCID = SecPkgInfoA_LIST[index]->wRPCID;
1211
1.94k
      pPackageInfo->cbMaxToken = SecPkgInfoA_LIST[index]->cbMaxToken;
1212
1.94k
      pPackageInfo->Name = _strdup(SecPkgInfoA_LIST[index]->Name);
1213
1.94k
      pPackageInfo->Comment = _strdup(SecPkgInfoA_LIST[index]->Comment);
1214
1215
1.94k
      if (!pPackageInfo->Name || !pPackageInfo->Comment)
1216
0
      {
1217
0
        sspi_ContextBufferFree(pPackageInfo);
1218
0
        return SEC_E_INSUFFICIENT_MEMORY;
1219
0
      }
1220
1221
1.94k
      *(ppPackageInfo) = pPackageInfo;
1222
1.94k
      return SEC_E_OK;
1223
1.94k
    }
1224
1.94k
  }
1225
1226
0
  *(ppPackageInfo) = nullptr;
1227
0
  return SEC_E_SECPKG_NOT_FOUND;
1228
1.94k
}
1229
1230
void FreeContextBuffer_QuerySecurityPackageInfo(void* contextBuffer)
1231
1.94k
{
1232
1.94k
  SecPkgInfo* pPackageInfo = (SecPkgInfo*)contextBuffer;
1233
1234
1.94k
  if (!pPackageInfo)
1235
0
    return;
1236
1237
1.94k
  free(pPackageInfo->Name);
1238
1.94k
  free(pPackageInfo->Comment);
1239
1.94k
  free(pPackageInfo);
1240
1.94k
}
1241
1242
9.49k
#define log_status(what, status) log_status_((what), (status), __FILE__, __func__, __LINE__)
1243
static SECURITY_STATUS log_status_(const char* what, SECURITY_STATUS status, const char* file,
1244
                                   const char* fkt, size_t line)
1245
9.49k
{
1246
9.49k
  if (IsSecurityStatusError(status))
1247
1.19k
  {
1248
1.19k
    const DWORD level = WLOG_WARN;
1249
1.19k
    static wLog* log = nullptr;
1250
1.19k
    if (!log)
1251
1
      log = WLog_Get(TAG);
1252
1253
1.19k
    if (WLog_IsLevelActive(log, level))
1254
1.19k
    {
1255
1.19k
      WLog_PrintTextMessage(log, level, line, file, fkt, "%s status %s [0x%08" PRIx32 "]",
1256
1.19k
                            what, GetSecurityStatusString(status),
1257
1.19k
                            WINPR_CXX_COMPAT_CAST(uint32_t, status));
1258
1.19k
    }
1259
1.19k
  }
1260
9.49k
  return status;
1261
9.49k
}
1262
1263
/* Credential Management */
1264
1265
static SECURITY_STATUS SEC_ENTRY winpr_AcquireCredentialsHandleW(
1266
    SEC_WCHAR* pszPrincipal, SEC_WCHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
1267
    void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
1268
    PTimeStamp ptsExpiry)
1269
0
{
1270
0
  const SecurityFunctionTableW* table = sspi_GetSecurityFunctionTableWByNameW(pszPackage);
1271
1272
0
  if (!table)
1273
0
    return SEC_E_SECPKG_NOT_FOUND;
1274
1275
0
  if (!table->AcquireCredentialsHandleW)
1276
0
  {
1277
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1278
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1279
0
  }
1280
1281
0
  SECURITY_STATUS status = table->AcquireCredentialsHandleW(
1282
0
      pszPrincipal, pszPackage, fCredentialUse, pvLogonID, pAuthData, pGetKeyFn, pvGetKeyArgument,
1283
0
      phCredential, ptsExpiry);
1284
0
  return log_status("AcquireCredentialsHandleW", status);
1285
0
}
1286
1287
static SECURITY_STATUS SEC_ENTRY winpr_AcquireCredentialsHandleA(
1288
    SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
1289
    void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
1290
    PTimeStamp ptsExpiry)
1291
1.94k
{
1292
1.94k
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(pszPackage);
1293
1294
1.94k
  if (!table)
1295
0
    return SEC_E_SECPKG_NOT_FOUND;
1296
1297
1.94k
  if (!table->AcquireCredentialsHandleA)
1298
0
  {
1299
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1300
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1301
0
  }
1302
1303
1.94k
  SECURITY_STATUS status = table->AcquireCredentialsHandleA(
1304
1.94k
      pszPrincipal, pszPackage, fCredentialUse, pvLogonID, pAuthData, pGetKeyFn, pvGetKeyArgument,
1305
1.94k
      phCredential, ptsExpiry);
1306
1.94k
  return log_status("AcquireCredentialsHandleA", status);
1307
1.94k
}
1308
1309
static SECURITY_STATUS SEC_ENTRY winpr_ExportSecurityContext(PCtxtHandle phContext, ULONG fFlags,
1310
                                                             PSecBuffer pPackedContext,
1311
                                                             HANDLE* pToken)
1312
0
{
1313
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1314
1315
0
  if (!Name)
1316
0
    return SEC_E_SECPKG_NOT_FOUND;
1317
1318
0
  const SecurityFunctionTableW* table = sspi_GetSecurityFunctionTableWByNameA(Name);
1319
1320
0
  if (!table)
1321
0
    return SEC_E_SECPKG_NOT_FOUND;
1322
1323
0
  if (!table->ExportSecurityContext)
1324
0
  {
1325
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1326
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1327
0
  }
1328
1329
0
  SECURITY_STATUS status =
1330
0
      table->ExportSecurityContext(phContext, fFlags, pPackedContext, pToken);
1331
0
  return log_status("ExportSecurityContext", status);
1332
0
}
1333
1334
static SECURITY_STATUS SEC_ENTRY winpr_FreeCredentialsHandle(PCredHandle phCredential)
1335
1.94k
{
1336
1.94k
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phCredential);
1337
1338
1.94k
  if (!Name)
1339
0
    return SEC_E_SECPKG_NOT_FOUND;
1340
1341
1.94k
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1342
1343
1.94k
  if (!table)
1344
0
    return SEC_E_SECPKG_NOT_FOUND;
1345
1346
1.94k
  if (!table->FreeCredentialsHandle)
1347
0
  {
1348
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1349
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1350
0
  }
1351
1352
1.94k
  SECURITY_STATUS status = table->FreeCredentialsHandle(phCredential);
1353
1.94k
  return log_status("FreeCredentialsHandle", status);
1354
1.94k
}
1355
1356
static SECURITY_STATUS SEC_ENTRY winpr_ImportSecurityContextW(SEC_WCHAR* pszPackage,
1357
                                                              PSecBuffer pPackedContext,
1358
                                                              HANDLE pToken, PCtxtHandle phContext)
1359
0
{
1360
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1361
1362
0
  if (!Name)
1363
0
    return SEC_E_SECPKG_NOT_FOUND;
1364
1365
0
  const SecurityFunctionTableW* table = sspi_GetSecurityFunctionTableWByNameA(Name);
1366
1367
0
  if (!table)
1368
0
    return SEC_E_SECPKG_NOT_FOUND;
1369
1370
0
  if (!table->ImportSecurityContextW)
1371
0
  {
1372
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1373
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1374
0
  }
1375
1376
0
  SECURITY_STATUS status =
1377
0
      table->ImportSecurityContextW(pszPackage, pPackedContext, pToken, phContext);
1378
0
  return log_status("ImportSecurityContextW", status);
1379
0
}
1380
1381
static SECURITY_STATUS SEC_ENTRY winpr_ImportSecurityContextA(SEC_CHAR* pszPackage,
1382
                                                              PSecBuffer pPackedContext,
1383
                                                              HANDLE pToken, PCtxtHandle phContext)
1384
0
{
1385
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1386
1387
0
  if (!Name)
1388
0
    return SEC_E_SECPKG_NOT_FOUND;
1389
1390
0
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1391
1392
0
  if (!table)
1393
0
    return SEC_E_SECPKG_NOT_FOUND;
1394
1395
0
  if (!table->ImportSecurityContextA)
1396
0
  {
1397
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1398
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1399
0
  }
1400
1401
0
  SECURITY_STATUS status =
1402
0
      table->ImportSecurityContextA(pszPackage, pPackedContext, pToken, phContext);
1403
0
  return log_status("ImportSecurityContextA", status);
1404
0
}
1405
1406
static SECURITY_STATUS SEC_ENTRY winpr_QueryCredentialsAttributesW(PCredHandle phCredential,
1407
                                                                   ULONG ulAttribute, void* pBuffer)
1408
0
{
1409
0
  const WCHAR* Name = (const WCHAR*)sspi_SecureHandleGetUpperPointer(phCredential);
1410
1411
0
  if (!Name)
1412
0
    return SEC_E_SECPKG_NOT_FOUND;
1413
1414
0
  const SecurityFunctionTableW* table = sspi_GetSecurityFunctionTableWByNameW(Name);
1415
1416
0
  if (!table)
1417
0
    return SEC_E_SECPKG_NOT_FOUND;
1418
1419
0
  if (!table->QueryCredentialsAttributesW)
1420
0
  {
1421
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1422
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1423
0
  }
1424
1425
0
  SECURITY_STATUS status = table->QueryCredentialsAttributesW(phCredential, ulAttribute, pBuffer);
1426
0
  return log_status("QueryCredentialsAttributesW", status);
1427
0
}
1428
1429
static SECURITY_STATUS SEC_ENTRY winpr_QueryCredentialsAttributesA(PCredHandle phCredential,
1430
                                                                   ULONG ulAttribute, void* pBuffer)
1431
0
{
1432
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phCredential);
1433
1434
0
  if (!Name)
1435
0
    return SEC_E_SECPKG_NOT_FOUND;
1436
1437
0
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1438
1439
0
  if (!table)
1440
0
    return SEC_E_SECPKG_NOT_FOUND;
1441
1442
0
  if (!table->QueryCredentialsAttributesA)
1443
0
  {
1444
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1445
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1446
0
  }
1447
1448
0
  SECURITY_STATUS status = table->QueryCredentialsAttributesA(phCredential, ulAttribute, pBuffer);
1449
0
  return log_status("QueryCredentialsAttributesA", status);
1450
0
}
1451
1452
static SECURITY_STATUS SEC_ENTRY winpr_SetCredentialsAttributesW(PCredHandle phCredential,
1453
                                                                 ULONG ulAttribute, void* pBuffer,
1454
                                                                 ULONG cbBuffer)
1455
0
{
1456
0
  const WCHAR* Name = (const WCHAR*)sspi_SecureHandleGetUpperPointer(phCredential);
1457
1458
0
  if (!Name)
1459
0
    return SEC_E_SECPKG_NOT_FOUND;
1460
1461
0
  const SecurityFunctionTableW* table = sspi_GetSecurityFunctionTableWByNameW(Name);
1462
1463
0
  if (!table)
1464
0
    return SEC_E_SECPKG_NOT_FOUND;
1465
1466
0
  if (!table->SetCredentialsAttributesW)
1467
0
  {
1468
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1469
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1470
0
  }
1471
1472
0
  SECURITY_STATUS status =
1473
0
      table->SetCredentialsAttributesW(phCredential, ulAttribute, pBuffer, cbBuffer);
1474
0
  return log_status("SetCredentialsAttributesW", status);
1475
0
}
1476
1477
static SECURITY_STATUS SEC_ENTRY winpr_SetCredentialsAttributesA(PCredHandle phCredential,
1478
                                                                 ULONG ulAttribute, void* pBuffer,
1479
                                                                 ULONG cbBuffer)
1480
0
{
1481
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phCredential);
1482
1483
0
  if (!Name)
1484
0
    return SEC_E_SECPKG_NOT_FOUND;
1485
1486
0
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1487
1488
0
  if (!table)
1489
0
    return SEC_E_SECPKG_NOT_FOUND;
1490
1491
0
  if (!table->SetCredentialsAttributesA)
1492
0
  {
1493
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1494
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1495
0
  }
1496
1497
0
  SECURITY_STATUS status =
1498
0
      table->SetCredentialsAttributesA(phCredential, ulAttribute, pBuffer, cbBuffer);
1499
0
  return log_status("SetCredentialsAttributesA", status);
1500
0
}
1501
1502
/* Context Management */
1503
1504
static SECURITY_STATUS SEC_ENTRY
1505
winpr_AcceptSecurityContext(PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
1506
                            ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
1507
                            PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsTimeStamp)
1508
1.31k
{
1509
1.31k
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phCredential);
1510
1511
1.31k
  if (!Name)
1512
0
    return SEC_E_SECPKG_NOT_FOUND;
1513
1514
1.31k
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1515
1516
1.31k
  if (!table)
1517
0
    return SEC_E_SECPKG_NOT_FOUND;
1518
1519
1.31k
  if (!table->AcceptSecurityContext)
1520
0
  {
1521
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1522
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1523
0
  }
1524
1525
1.31k
  SECURITY_STATUS status =
1526
1.31k
      table->AcceptSecurityContext(phCredential, phContext, pInput, fContextReq, TargetDataRep,
1527
1.31k
                                   phNewContext, pOutput, pfContextAttr, ptsTimeStamp);
1528
1.31k
  return log_status("AcceptSecurityContext", status);
1529
1.31k
}
1530
1531
static SECURITY_STATUS SEC_ENTRY winpr_ApplyControlToken(PCtxtHandle phContext,
1532
                                                         PSecBufferDesc pInput)
1533
0
{
1534
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1535
1536
0
  if (!Name)
1537
0
    return SEC_E_SECPKG_NOT_FOUND;
1538
1539
0
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1540
1541
0
  if (!table)
1542
0
    return SEC_E_SECPKG_NOT_FOUND;
1543
1544
0
  if (!table->ApplyControlToken)
1545
0
  {
1546
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1547
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1548
0
  }
1549
1550
0
  SECURITY_STATUS status = table->ApplyControlToken(phContext, pInput);
1551
0
  return log_status("ApplyControlToken", status);
1552
0
}
1553
1554
static SECURITY_STATUS SEC_ENTRY winpr_CompleteAuthToken(PCtxtHandle phContext,
1555
                                                         PSecBufferDesc pToken)
1556
0
{
1557
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1558
1559
0
  if (!Name)
1560
0
    return SEC_E_SECPKG_NOT_FOUND;
1561
1562
0
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1563
1564
0
  if (!table)
1565
0
    return SEC_E_SECPKG_NOT_FOUND;
1566
1567
0
  if (!table->CompleteAuthToken)
1568
0
  {
1569
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1570
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1571
0
  }
1572
1573
0
  SECURITY_STATUS status = table->CompleteAuthToken(phContext, pToken);
1574
0
  return log_status("CompleteAuthToken", status);
1575
0
}
1576
1577
static SECURITY_STATUS SEC_ENTRY winpr_DeleteSecurityContext(PCtxtHandle phContext)
1578
1.94k
{
1579
1.94k
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1580
1581
1.94k
  if (!Name)
1582
0
    return SEC_E_SECPKG_NOT_FOUND;
1583
1584
1.94k
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1585
1586
1.94k
  if (!table)
1587
0
    return SEC_E_SECPKG_NOT_FOUND;
1588
1589
1.94k
  if (!table->DeleteSecurityContext)
1590
0
  {
1591
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1592
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1593
0
  }
1594
1595
1.94k
  const SECURITY_STATUS status = table->DeleteSecurityContext(phContext);
1596
1.94k
  return log_status("DeleteSecurityContext", status);
1597
1.94k
}
1598
1599
static SECURITY_STATUS SEC_ENTRY winpr_FreeContextBuffer(void* pvContextBuffer)
1600
1.94k
{
1601
1.94k
  if (!pvContextBuffer)
1602
0
    return SEC_E_INVALID_HANDLE;
1603
1604
1.94k
  sspi_ContextBufferFree(pvContextBuffer);
1605
1.94k
  return SEC_E_OK;
1606
1.94k
}
1607
1608
static SECURITY_STATUS SEC_ENTRY winpr_ImpersonateSecurityContext(PCtxtHandle phContext)
1609
0
{
1610
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1611
1612
0
  if (!Name)
1613
0
    return SEC_E_SECPKG_NOT_FOUND;
1614
1615
0
  const SecurityFunctionTableW* table = sspi_GetSecurityFunctionTableWByNameA(Name);
1616
1617
0
  if (!table)
1618
0
    return SEC_E_SECPKG_NOT_FOUND;
1619
1620
0
  if (!table->ImpersonateSecurityContext)
1621
0
  {
1622
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1623
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1624
0
  }
1625
1626
0
  SECURITY_STATUS status = table->ImpersonateSecurityContext(phContext);
1627
0
  return log_status("ImpersonateSecurityContext", status);
1628
0
}
1629
1630
static SECURITY_STATUS SEC_ENTRY winpr_InitializeSecurityContextW(
1631
    PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq,
1632
    ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
1633
    PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
1634
0
{
1635
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phCredential);
1636
1637
0
  if (!Name)
1638
0
    return SEC_E_SECPKG_NOT_FOUND;
1639
1640
0
  const SecurityFunctionTableW* table = sspi_GetSecurityFunctionTableWByNameA(Name);
1641
1642
0
  if (!table)
1643
0
    return SEC_E_SECPKG_NOT_FOUND;
1644
1645
0
  if (!table->InitializeSecurityContextW)
1646
0
  {
1647
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1648
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1649
0
  }
1650
1651
0
  const SECURITY_STATUS status = table->InitializeSecurityContextW(
1652
0
      phCredential, phContext, pszTargetName, fContextReq, Reserved1, TargetDataRep, pInput,
1653
0
      Reserved2, phNewContext, pOutput, pfContextAttr, ptsExpiry);
1654
0
  return log_status("InitializeSecurityContextW", status);
1655
0
}
1656
1657
static SECURITY_STATUS SEC_ENTRY winpr_InitializeSecurityContextA(
1658
    PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR* pszTargetName, ULONG fContextReq,
1659
    ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
1660
    PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
1661
1.77k
{
1662
1.77k
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phCredential);
1663
1664
1.77k
  if (!Name)
1665
0
    return SEC_E_SECPKG_NOT_FOUND;
1666
1667
1.77k
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1668
1669
1.77k
  if (!table)
1670
0
    return SEC_E_SECPKG_NOT_FOUND;
1671
1672
1.77k
  if (!table->InitializeSecurityContextA)
1673
0
  {
1674
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1675
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1676
0
  }
1677
1678
1.77k
  SECURITY_STATUS status = table->InitializeSecurityContextA(
1679
1.77k
      phCredential, phContext, pszTargetName, fContextReq, Reserved1, TargetDataRep, pInput,
1680
1.77k
      Reserved2, phNewContext, pOutput, pfContextAttr, ptsExpiry);
1681
1682
1.77k
  return log_status("InitializeSecurityContextA", status);
1683
1.77k
}
1684
1685
static SECURITY_STATUS SEC_ENTRY winpr_QueryContextAttributesW(PCtxtHandle phContext,
1686
                                                               ULONG ulAttribute, void* pBuffer)
1687
0
{
1688
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1689
1690
0
  if (!Name)
1691
0
    return SEC_E_SECPKG_NOT_FOUND;
1692
1693
0
  const SecurityFunctionTableW* table = sspi_GetSecurityFunctionTableWByNameA(Name);
1694
1695
0
  if (!table)
1696
0
    return SEC_E_SECPKG_NOT_FOUND;
1697
1698
0
  if (!table->QueryContextAttributesW)
1699
0
  {
1700
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1701
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1702
0
  }
1703
1704
0
  SECURITY_STATUS status = table->QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1705
0
  return log_status("QueryContextAttributesW", status);
1706
0
}
1707
1708
static SECURITY_STATUS SEC_ENTRY winpr_QueryContextAttributesA(PCtxtHandle phContext,
1709
                                                               ULONG ulAttribute, void* pBuffer)
1710
0
{
1711
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1712
1713
0
  if (!Name)
1714
0
    return SEC_E_SECPKG_NOT_FOUND;
1715
1716
0
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1717
1718
0
  if (!table)
1719
0
    return SEC_E_SECPKG_NOT_FOUND;
1720
1721
0
  if (!table->QueryContextAttributesA)
1722
0
  {
1723
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1724
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1725
0
  }
1726
1727
0
  SECURITY_STATUS status = table->QueryContextAttributesA(phContext, ulAttribute, pBuffer);
1728
0
  return log_status("QueryContextAttributesA", status);
1729
0
}
1730
1731
static SECURITY_STATUS SEC_ENTRY winpr_QuerySecurityContextToken(PCtxtHandle phContext,
1732
                                                                 HANDLE* phToken)
1733
0
{
1734
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1735
1736
0
  if (!Name)
1737
0
    return SEC_E_SECPKG_NOT_FOUND;
1738
1739
0
  const SecurityFunctionTableW* table = sspi_GetSecurityFunctionTableWByNameA(Name);
1740
1741
0
  if (!table)
1742
0
    return SEC_E_SECPKG_NOT_FOUND;
1743
1744
0
  if (!table->QuerySecurityContextToken)
1745
0
  {
1746
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1747
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1748
0
  }
1749
1750
0
  SECURITY_STATUS status = table->QuerySecurityContextToken(phContext, phToken);
1751
0
  return log_status("QuerySecurityContextToken", status);
1752
0
}
1753
1754
static SECURITY_STATUS SEC_ENTRY winpr_SetContextAttributesW(PCtxtHandle phContext,
1755
                                                             ULONG ulAttribute, void* pBuffer,
1756
                                                             ULONG cbBuffer)
1757
0
{
1758
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1759
1760
0
  if (!Name)
1761
0
    return SEC_E_SECPKG_NOT_FOUND;
1762
1763
0
  const SecurityFunctionTableW* table = sspi_GetSecurityFunctionTableWByNameA(Name);
1764
1765
0
  if (!table)
1766
0
    return SEC_E_SECPKG_NOT_FOUND;
1767
1768
0
  if (!table->SetContextAttributesW)
1769
0
  {
1770
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1771
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1772
0
  }
1773
1774
0
  SECURITY_STATUS status =
1775
0
      table->SetContextAttributesW(phContext, ulAttribute, pBuffer, cbBuffer);
1776
0
  return log_status("SetContextAttributesW", status);
1777
0
}
1778
1779
static SECURITY_STATUS SEC_ENTRY winpr_SetContextAttributesA(PCtxtHandle phContext,
1780
                                                             ULONG ulAttribute, void* pBuffer,
1781
                                                             ULONG cbBuffer)
1782
581
{
1783
581
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1784
1785
581
  if (!Name)
1786
0
    return SEC_E_SECPKG_NOT_FOUND;
1787
1788
581
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1789
1790
581
  if (!table)
1791
0
    return SEC_E_SECPKG_NOT_FOUND;
1792
1793
581
  if (!table->SetContextAttributesA)
1794
0
  {
1795
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1796
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1797
0
  }
1798
1799
581
  SECURITY_STATUS status =
1800
581
      table->SetContextAttributesA(phContext, ulAttribute, pBuffer, cbBuffer);
1801
581
  return log_status("SetContextAttributesA", status);
1802
581
}
1803
1804
static SECURITY_STATUS SEC_ENTRY winpr_RevertSecurityContext(PCtxtHandle phContext)
1805
0
{
1806
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1807
1808
0
  if (!Name)
1809
0
    return SEC_E_SECPKG_NOT_FOUND;
1810
1811
0
  const SecurityFunctionTableW* table = sspi_GetSecurityFunctionTableWByNameA(Name);
1812
1813
0
  if (!table)
1814
0
    return SEC_E_SECPKG_NOT_FOUND;
1815
1816
0
  if (!table->RevertSecurityContext)
1817
0
  {
1818
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1819
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1820
0
  }
1821
1822
0
  SECURITY_STATUS status = table->RevertSecurityContext(phContext);
1823
1824
0
  return log_status("RevertSecurityContext", status);
1825
0
}
1826
1827
/* Message Support */
1828
1829
static SECURITY_STATUS SEC_ENTRY winpr_DecryptMessage(PCtxtHandle phContext,
1830
                                                      PSecBufferDesc pMessage, ULONG MessageSeqNo,
1831
                                                      PULONG pfQOP)
1832
0
{
1833
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1834
1835
0
  if (!Name)
1836
0
    return SEC_E_SECPKG_NOT_FOUND;
1837
1838
0
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1839
1840
0
  if (!table)
1841
0
    return SEC_E_SECPKG_NOT_FOUND;
1842
1843
0
  if (!table->DecryptMessage)
1844
0
  {
1845
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1846
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1847
0
  }
1848
1849
0
  const SECURITY_STATUS status = table->DecryptMessage(phContext, pMessage, MessageSeqNo, pfQOP);
1850
1851
0
  return log_status("DecryptMessage", status);
1852
0
}
1853
1854
static SECURITY_STATUS SEC_ENTRY winpr_EncryptMessage(PCtxtHandle phContext, ULONG fQOP,
1855
                                                      PSecBufferDesc pMessage, ULONG MessageSeqNo)
1856
0
{
1857
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1858
1859
0
  if (!Name)
1860
0
    return SEC_E_SECPKG_NOT_FOUND;
1861
1862
0
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1863
1864
0
  if (!table)
1865
0
    return SEC_E_SECPKG_NOT_FOUND;
1866
1867
0
  if (!table->EncryptMessage)
1868
0
  {
1869
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1870
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1871
0
  }
1872
1873
0
  const SECURITY_STATUS status = table->EncryptMessage(phContext, fQOP, pMessage, MessageSeqNo);
1874
0
  return log_status("EncryptMessage", status);
1875
0
}
1876
1877
static SECURITY_STATUS SEC_ENTRY winpr_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1878
                                                     PSecBufferDesc pMessage, ULONG MessageSeqNo)
1879
0
{
1880
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1881
1882
0
  if (!Name)
1883
0
    return SEC_E_SECPKG_NOT_FOUND;
1884
1885
0
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1886
1887
0
  if (!table)
1888
0
    return SEC_E_SECPKG_NOT_FOUND;
1889
1890
0
  if (!table->MakeSignature)
1891
0
  {
1892
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1893
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1894
0
  }
1895
1896
0
  const SECURITY_STATUS status = table->MakeSignature(phContext, fQOP, pMessage, MessageSeqNo);
1897
0
  return log_status("MakeSignature", status);
1898
0
}
1899
1900
static SECURITY_STATUS SEC_ENTRY winpr_VerifySignature(PCtxtHandle phContext,
1901
                                                       PSecBufferDesc pMessage, ULONG MessageSeqNo,
1902
                                                       PULONG pfQOP)
1903
0
{
1904
0
  const char* Name = (const char*)sspi_SecureHandleGetUpperPointer(phContext);
1905
1906
0
  if (!Name)
1907
0
    return SEC_E_SECPKG_NOT_FOUND;
1908
1909
0
  const SecurityFunctionTableA* table = sspi_GetSecurityFunctionTableAByNameA(Name);
1910
1911
0
  if (!table)
1912
0
    return SEC_E_SECPKG_NOT_FOUND;
1913
1914
0
  if (!table->VerifySignature)
1915
0
  {
1916
0
    WLog_WARN(TAG, "Security module does not provide an implementation");
1917
0
    return SEC_E_UNSUPPORTED_FUNCTION;
1918
0
  }
1919
1920
0
  SECURITY_STATUS status = table->VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1921
1922
0
  return log_status("VerifySignature", status);
1923
0
}
1924
1925
static SecurityFunctionTableA winpr_SecurityFunctionTableA = {
1926
  3,                                 /* dwVersion */
1927
  winpr_EnumerateSecurityPackagesA,  /* EnumerateSecurityPackages */
1928
  winpr_QueryCredentialsAttributesA, /* QueryCredentialsAttributes */
1929
  winpr_AcquireCredentialsHandleA,   /* AcquireCredentialsHandle */
1930
  winpr_FreeCredentialsHandle,       /* FreeCredentialsHandle */
1931
  nullptr,                           /* Reserved2 */
1932
  winpr_InitializeSecurityContextA,  /* InitializeSecurityContext */
1933
  winpr_AcceptSecurityContext,       /* AcceptSecurityContext */
1934
  winpr_CompleteAuthToken,           /* CompleteAuthToken */
1935
  winpr_DeleteSecurityContext,       /* DeleteSecurityContext */
1936
  winpr_ApplyControlToken,           /* ApplyControlToken */
1937
  winpr_QueryContextAttributesA,     /* QueryContextAttributes */
1938
  winpr_ImpersonateSecurityContext,  /* ImpersonateSecurityContext */
1939
  winpr_RevertSecurityContext,       /* RevertSecurityContext */
1940
  winpr_MakeSignature,               /* MakeSignature */
1941
  winpr_VerifySignature,             /* VerifySignature */
1942
  winpr_FreeContextBuffer,           /* FreeContextBuffer */
1943
  winpr_QuerySecurityPackageInfoA,   /* QuerySecurityPackageInfo */
1944
  nullptr,                           /* Reserved3 */
1945
  nullptr,                           /* Reserved4 */
1946
  winpr_ExportSecurityContext,       /* ExportSecurityContext */
1947
  winpr_ImportSecurityContextA,      /* ImportSecurityContext */
1948
  nullptr,                           /* AddCredentials */
1949
  nullptr,                           /* Reserved8 */
1950
  winpr_QuerySecurityContextToken,   /* QuerySecurityContextToken */
1951
  winpr_EncryptMessage,              /* EncryptMessage */
1952
  winpr_DecryptMessage,              /* DecryptMessage */
1953
  winpr_SetContextAttributesA,       /* SetContextAttributes */
1954
  winpr_SetCredentialsAttributesA,   /* SetCredentialsAttributes */
1955
};
1956
1957
static SecurityFunctionTableW winpr_SecurityFunctionTableW = {
1958
  3,                                 /* dwVersion */
1959
  winpr_EnumerateSecurityPackagesW,  /* EnumerateSecurityPackages */
1960
  winpr_QueryCredentialsAttributesW, /* QueryCredentialsAttributes */
1961
  winpr_AcquireCredentialsHandleW,   /* AcquireCredentialsHandle */
1962
  winpr_FreeCredentialsHandle,       /* FreeCredentialsHandle */
1963
  nullptr,                           /* Reserved2 */
1964
  winpr_InitializeSecurityContextW,  /* InitializeSecurityContext */
1965
  winpr_AcceptSecurityContext,       /* AcceptSecurityContext */
1966
  winpr_CompleteAuthToken,           /* CompleteAuthToken */
1967
  winpr_DeleteSecurityContext,       /* DeleteSecurityContext */
1968
  winpr_ApplyControlToken,           /* ApplyControlToken */
1969
  winpr_QueryContextAttributesW,     /* QueryContextAttributes */
1970
  winpr_ImpersonateSecurityContext,  /* ImpersonateSecurityContext */
1971
  winpr_RevertSecurityContext,       /* RevertSecurityContext */
1972
  winpr_MakeSignature,               /* MakeSignature */
1973
  winpr_VerifySignature,             /* VerifySignature */
1974
  winpr_FreeContextBuffer,           /* FreeContextBuffer */
1975
  winpr_QuerySecurityPackageInfoW,   /* QuerySecurityPackageInfo */
1976
  nullptr,                           /* Reserved3 */
1977
  nullptr,                           /* Reserved4 */
1978
  winpr_ExportSecurityContext,       /* ExportSecurityContext */
1979
  winpr_ImportSecurityContextW,      /* ImportSecurityContext */
1980
  nullptr,                           /* AddCredentials */
1981
  nullptr,                           /* Reserved8 */
1982
  winpr_QuerySecurityContextToken,   /* QuerySecurityContextToken */
1983
  winpr_EncryptMessage,              /* EncryptMessage */
1984
  winpr_DecryptMessage,              /* DecryptMessage */
1985
  winpr_SetContextAttributesW,       /* SetContextAttributes */
1986
  winpr_SetCredentialsAttributesW,   /* SetCredentialsAttributes */
1987
};
1988
1989
SecurityFunctionTableW* SEC_ENTRY winpr_InitSecurityInterfaceW(void)
1990
1
{
1991
1
  return &winpr_SecurityFunctionTableW;
1992
1
}
1993
1994
SecurityFunctionTableA* SEC_ENTRY winpr_InitSecurityInterfaceA(void)
1995
1
{
1996
1
  return &winpr_SecurityFunctionTableA;
1997
1
}