Repository navigation
Expand file tree
/
Copy pathhelpers.cpp
More file actions
277 lines (237 loc) · 9.13 KB
/
Copy pathhelpers.cpp
File metadata and controls
277 lines (237 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "helpers.h"
#include <shlwapi.h>
#include <strsafe.h>
#include <wincred.h>
#define SECURITY_WIN32
#include <security.h>
#pragma comment(lib, "secur32.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "shlwapi.lib")
static bool InitUnicodeString(UNICODE_STRING* pus, PWSTR pwz)
{
// A UNICODE_STRING length is a USHORT (bytes). A longer string is refused
// rather than cut: silently logging on with a truncated password would be
// a confusing failure, and len * sizeof(WCHAR) must never wrap.
size_t len = pwz ? wcslen(pwz) : 0;
const size_t maxChars = (0xFFFF / sizeof(WCHAR)) - 1; // 32766
if (len > maxChars)
return false;
pus->Length = static_cast<USHORT>(len * sizeof(WCHAR));
pus->MaximumLength = static_cast<USHORT>((len + 1) * sizeof(WCHAR));
pus->Buffer = pwz;
return true;
}
// Copies the string to pbDest and stores its offset from pvBase in Buffer.
static void PackUnicodeString(const UNICODE_STRING& src, UNICODE_STRING* pusDst,
BYTE* pbDest, const void* pvBase)
{
pusDst->Length = src.Length;
pusDst->MaximumLength = src.Length;
if (src.Length)
{
CopyMemory(pbDest, src.Buffer, src.Length);
pusDst->Buffer = reinterpret_cast<PWSTR>(
static_cast<ULONG_PTR>(pbDest - static_cast<const BYTE*>(pvBase)));
}
else
{
pusDst->Buffer = nullptr;
}
}
static bool UnpackUnicodeString(void* pvBase, DWORD cb, UNICODE_STRING* pus)
{
if (pus->Length == 0)
{
pus->Buffer = nullptr;
return true;
}
ULONG_PTR offset = reinterpret_cast<ULONG_PTR>(pus->Buffer);
if (offset % sizeof(WCHAR) != 0 || offset > cb || pus->Length > cb - offset)
return false;
pus->Buffer = reinterpret_cast<PWSTR>(static_cast<BYTE*>(pvBase) + offset);
return true;
}
HRESULT SplitDomainAndUsername(PCWSTR pszQualifiedUserName,
PWSTR pszDomain, int cchDomain,
PWSTR pszUsername, int cchUsername)
{
const wchar_t* pchSlash = wcschr(pszQualifiedUserName, L'\\');
if (!pchSlash)
return E_INVALIDARG;
HRESULT hr = StringCchCopyNW(pszDomain, cchDomain, pszQualifiedUserName,
static_cast<size_t>(pchSlash - pszQualifiedUserName));
if (SUCCEEDED(hr))
hr = StringCchCopyW(pszUsername, cchUsername, pchSlash + 1);
return hr;
}
// CredProtectW and CredUnprotectW work on writable buffers and report the
// needed size through a first call that fails with ERROR_INSUFFICIENT_BUFFER.
static HRESULT ProtectCopy(PWSTR pwzWritable, PWSTR* ppwzOut)
{
*ppwzOut = nullptr;
DWORD cch = 0;
const DWORD cchIn = static_cast<DWORD>(wcslen(pwzWritable) + 1);
if (CredProtectW(FALSE, pwzWritable, cchIn, nullptr, &cch, nullptr) ||
GetLastError() != ERROR_INSUFFICIENT_BUFFER || cch == 0)
return E_FAIL;
PWSTR out = static_cast<PWSTR>(CoTaskMemAlloc(cch * sizeof(WCHAR)));
if (!out)
return E_OUTOFMEMORY;
if (!CredProtectW(FALSE, pwzWritable, cchIn, out, &cch, nullptr))
{
HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
SecureZeroMemory(out, cch * sizeof(WCHAR));
CoTaskMemFree(out);
return hr;
}
*ppwzOut = out;
return S_OK;
}
static HRESULT UnprotectCopy(PWSTR pwzWritable, PWSTR* ppwzOut)
{
*ppwzOut = nullptr;
DWORD cch = 0;
const DWORD cchIn = static_cast<DWORD>(wcslen(pwzWritable) + 1);
if (CredUnprotectW(FALSE, pwzWritable, cchIn, nullptr, &cch) ||
GetLastError() != ERROR_INSUFFICIENT_BUFFER || cch == 0)
return E_FAIL;
PWSTR out = static_cast<PWSTR>(CoTaskMemAlloc(cch * sizeof(WCHAR)));
if (!out)
return E_OUTOFMEMORY;
if (!CredUnprotectW(FALSE, pwzWritable, cchIn, out, &cch))
{
HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
SecureZeroMemory(out, cch * sizeof(WCHAR));
CoTaskMemFree(out);
return hr;
}
*ppwzOut = out;
return S_OK;
}
// Shared shape of both directions: work on a private writable copy, decide by
// the protection state, wipe the copy.
static HRESULT CopyPassword(PCWSTR pwzPassword, bool protect,
CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus, PWSTR* ppwzOut)
{
*ppwzOut = nullptr;
if (!pwzPassword || !*pwzPassword)
return SHStrDupW(L"", ppwzOut);
PWSTR copy = nullptr;
HRESULT hr = SHStrDupW(pwzPassword, ©);
if (FAILED(hr))
return hr;
CRED_PROTECTION_TYPE type = CredUnprotected;
if (!CredIsProtectedW(copy, &type))
hr = HRESULT_FROM_WIN32(GetLastError());
else if (protect)
hr = (cpus != CPUS_CREDUI && type == CredUnprotected)
? ProtectCopy(copy, ppwzOut)
: SHStrDupW(copy, ppwzOut);
else
hr = (type == CredUnprotected)
? SHStrDupW(copy, ppwzOut)
: UnprotectCopy(copy, ppwzOut);
SecureZeroMemory(copy, wcslen(copy) * sizeof(WCHAR));
CoTaskMemFree(copy);
return hr;
}
HRESULT ProtectIfNecessaryAndCopyPassword(PCWSTR pwzPassword,
CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus,
PWSTR* ppwzProtectedPassword)
{
return CopyPassword(pwzPassword, true, cpus, ppwzProtectedPassword);
}
HRESULT CopyUnprotectedPassword(PCWSTR pwzPassword, PWSTR* ppwzPlain)
{
return CopyPassword(pwzPassword, false, CPUS_LOGON, ppwzPlain);
}
HRESULT KerbInteractiveUnlockLogonInit(PWSTR pwzDomain, PWSTR pwzUsername, PWSTR pwzPassword,
CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus,
KERB_INTERACTIVE_UNLOCK_LOGON* pkiul)
{
ZeroMemory(pkiul, sizeof(*pkiul));
KERB_INTERACTIVE_LOGON* pkil = &pkiul->Logon;
switch (cpus)
{
case CPUS_LOGON: pkil->MessageType = KerbInteractiveLogon; break;
case CPUS_UNLOCK_WORKSTATION: pkil->MessageType = KerbWorkstationUnlockLogon; break;
default: return E_INVALIDARG;
}
if (!InitUnicodeString(&pkil->LogonDomainName, pwzDomain) ||
!InitUnicodeString(&pkil->UserName, pwzUsername) ||
!InitUnicodeString(&pkil->Password, pwzPassword))
return E_INVALIDARG;
return S_OK;
}
HRESULT KerbInteractiveUnlockLogonPack(const KERB_INTERACTIVE_UNLOCK_LOGON& rkiulIn,
BYTE** prgb, DWORD* pcb)
{
const KERB_INTERACTIVE_LOGON& in = rkiulIn.Logon;
DWORD cb = sizeof(rkiulIn) + in.LogonDomainName.Length + in.UserName.Length + in.Password.Length;
KERB_INTERACTIVE_UNLOCK_LOGON* pkiulOut =
static_cast<KERB_INTERACTIVE_UNLOCK_LOGON*>(CoTaskMemAlloc(cb));
if (!pkiulOut)
return E_OUTOFMEMORY;
ZeroMemory(pkiulOut, sizeof(*pkiulOut));
pkiulOut->Logon.MessageType = in.MessageType;
BYTE* pbData = reinterpret_cast<BYTE*>(pkiulOut) + sizeof(*pkiulOut);
PackUnicodeString(in.LogonDomainName, &pkiulOut->Logon.LogonDomainName, pbData, pkiulOut);
pbData += in.LogonDomainName.Length;
PackUnicodeString(in.UserName, &pkiulOut->Logon.UserName, pbData, pkiulOut);
pbData += in.UserName.Length;
PackUnicodeString(in.Password, &pkiulOut->Logon.Password, pbData, pkiulOut);
*prgb = reinterpret_cast<BYTE*>(pkiulOut);
*pcb = cb;
return S_OK;
}
HRESULT KerbInteractiveUnlockLogonUnpackInPlace(KERB_INTERACTIVE_UNLOCK_LOGON* pkiul, DWORD cb)
{
if (cb < sizeof(*pkiul))
return E_INVALIDARG;
KERB_INTERACTIVE_LOGON* pkil = &pkiul->Logon;
if (UnpackUnicodeString(pkiul, cb, &pkil->LogonDomainName) &&
UnpackUnicodeString(pkiul, cb, &pkil->UserName) &&
UnpackUnicodeString(pkiul, cb, &pkil->Password))
return S_OK;
return E_INVALIDARG;
}
HRESULT RetrieveNegotiateAuthPackage(ULONG* pulAuthPackage)
{
HANDLE hLsa = nullptr;
NTSTATUS status = LsaConnectUntrusted(&hLsa);
if (status != 0)
return HRESULT_FROM_NT(status);
LSA_STRING name;
name.Buffer = const_cast<PCHAR>(NEGOSSP_NAME_A);
name.Length = static_cast<USHORT>(strlen(name.Buffer));
name.MaximumLength = static_cast<USHORT>(name.Length + 1);
ULONG ulPackage = 0;
status = LsaLookupAuthenticationPackage(hLsa, &name, &ulPackage);
LsaDeregisterLogonProcess(hLsa);
if (status != 0)
return HRESULT_FROM_NT(status);
*pulAuthPackage = ulPackage;
return S_OK;
}
HRESULT FieldDescriptorCoAllocCopy(const CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR& rcpfd,
CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR** ppcpfd)
{
*ppcpfd = nullptr;
CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR* pcpfd =
static_cast<CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR*>(
CoTaskMemAlloc(sizeof(CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR)));
if (!pcpfd)
return E_OUTOFMEMORY;
pcpfd->dwFieldID = rcpfd.dwFieldID;
pcpfd->cpft = rcpfd.cpft;
pcpfd->guidFieldType = rcpfd.guidFieldType;
pcpfd->pszLabel = nullptr;
HRESULT hr = S_OK;
if (rcpfd.pszLabel)
hr = SHStrDupW(rcpfd.pszLabel, &pcpfd->pszLabel);
if (SUCCEEDED(hr))
*ppcpfd = pcpfd;
else
CoTaskMemFree(pcpfd);
return hr;
}