Coverage Report

Created: 2026-09-14 06:31

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