Coverage Report

Created: 2026-03-04 06:13

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