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