Coverage Report

Created: 2026-11-10 06:39

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