/src/FreeRDP/winpr/libwinpr/sspi/Negotiate/negotiate.c
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Negotiate Security Package |
4 | | * |
5 | | * Copyright 2011-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 | | |
23 | | #include <winpr/crt.h> |
24 | | #include <winpr/wtypes.h> |
25 | | #include <winpr/assert.h> |
26 | | #include <winpr/sspi.h> |
27 | | #include <winpr/tchar.h> |
28 | | #include <winpr/registry.h> |
29 | | #include <winpr/build-config.h> |
30 | | #include <winpr/asn1.h> |
31 | | |
32 | | #include "negotiate.h" |
33 | | |
34 | | #include "../NTLM/ntlm.h" |
35 | | #include "../NTLM/ntlm_export.h" |
36 | | #include "../Kerberos/kerberos.h" |
37 | | #include "../sspi.h" |
38 | | #include "../../utils.h" |
39 | | #include "../../log.h" |
40 | | #define TAG WINPR_TAG("negotiate") |
41 | | |
42 | 0 | #define NEGO_REG_KEY "Software\\%s\\SSPI\\Negotiate" |
43 | | |
44 | | static const char PACKAGE_NAME_DISABLE_ALL[] = "none"; |
45 | | static const char PACKAGE_NAME_NTLM[] = "ntlm"; |
46 | | static const char PACKAGE_NAME_KERBEROS[] = "kerberos"; |
47 | | static const char PACKAGE_NAME_KERBEROS_U2U[] = "u2u"; |
48 | | |
49 | | typedef struct |
50 | | { |
51 | | const TCHAR* name; |
52 | | const SecurityFunctionTableA* table; |
53 | | const SecurityFunctionTableW* table_w; |
54 | | } SecPkg; |
55 | | |
56 | | struct Mech_st |
57 | | { |
58 | | const WinPrAsn1_OID* oid; |
59 | | const SecPkg* pkg; |
60 | | const UINT flags; |
61 | | const BOOL preferred; |
62 | | }; |
63 | | |
64 | | typedef struct |
65 | | { |
66 | | const Mech* mech; |
67 | | CredHandle cred; |
68 | | BOOL valid; |
69 | | } MechCred; |
70 | | |
71 | | const SecPkgInfoA NEGOTIATE_SecPkgInfoA = { |
72 | | 0x00083BB3, /* fCapabilities */ |
73 | | 1, /* wVersion */ |
74 | | 0x0009, /* wRPCID */ |
75 | | 0x00002FE0, /* cbMaxToken */ |
76 | | "Negotiate", /* Name */ |
77 | | "Microsoft Package Negotiator" /* Comment */ |
78 | | }; |
79 | | |
80 | | static WCHAR NEGOTIATE_SecPkgInfoW_NameBuffer[32] = WINPR_C_ARRAY_INIT; |
81 | | static WCHAR NEGOTIATE_SecPkgInfoW_CommentBuffer[32] = WINPR_C_ARRAY_INIT; |
82 | | |
83 | | const SecPkgInfoW NEGOTIATE_SecPkgInfoW = { |
84 | | 0x00083BB3, /* fCapabilities */ |
85 | | 1, /* wVersion */ |
86 | | 0x0009, /* wRPCID */ |
87 | | 0x00002FE0, /* cbMaxToken */ |
88 | | NEGOTIATE_SecPkgInfoW_NameBuffer, /* Name */ |
89 | | NEGOTIATE_SecPkgInfoW_CommentBuffer /* Comment */ |
90 | | }; |
91 | | |
92 | | static const WinPrAsn1_OID spnego_OID = { 6, (BYTE*)"\x2b\x06\x01\x05\x05\x02" }; |
93 | | static const WinPrAsn1_OID kerberos_u2u_OID = { 10, |
94 | | (BYTE*)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x03" }; |
95 | | static const WinPrAsn1_OID kerberos_OID = { 9, (BYTE*)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" }; |
96 | | static const WinPrAsn1_OID kerberos_wrong_OID = { 9, |
97 | | (BYTE*)"\x2a\x86\x48\x82\xf7\x12\x01\x02\x02" }; |
98 | | static const WinPrAsn1_OID ntlm_OID = { 10, (BYTE*)"\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a" }; |
99 | | |
100 | | static const WinPrAsn1_OID negoex_OID = { 10, (BYTE*)"\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x1e" }; |
101 | | |
102 | | #ifdef WITH_KRB5 |
103 | | static const SecPkg SecPkgTable[] = { |
104 | | { KERBEROS_SSP_NAME, &KERBEROS_SecurityFunctionTableA, &KERBEROS_SecurityFunctionTableW }, |
105 | | { KERBEROS_SSP_NAME, &KERBEROS_SecurityFunctionTableA, &KERBEROS_SecurityFunctionTableW }, |
106 | | { NTLM_SSP_NAME, &NTLM_SecurityFunctionTableA, &NTLM_SecurityFunctionTableW } |
107 | | }; |
108 | | |
109 | | static const Mech MechTable[] = { |
110 | | { &kerberos_u2u_OID, &SecPkgTable[0], ISC_REQ_INTEGRITY | ISC_REQ_USE_SESSION_KEY, TRUE }, |
111 | | { &kerberos_OID, &SecPkgTable[1], ISC_REQ_INTEGRITY, TRUE }, |
112 | | { &ntlm_OID, &SecPkgTable[2], 0, FALSE }, |
113 | | }; |
114 | | #else |
115 | | static const SecPkg SecPkgTable[] = { { NTLM_SSP_NAME, &NTLM_SecurityFunctionTableA, |
116 | | &NTLM_SecurityFunctionTableW } }; |
117 | | |
118 | | static const Mech MechTable[] = { |
119 | | { &ntlm_OID, &SecPkgTable[0], 0, FALSE }, |
120 | | }; |
121 | | #endif |
122 | | |
123 | | static const size_t MECH_COUNT = sizeof(MechTable) / sizeof(Mech); |
124 | | |
125 | | enum NegState |
126 | | { |
127 | | NOSTATE = -1, |
128 | | ACCEPT_COMPLETED, |
129 | | ACCEPT_INCOMPLETE, |
130 | | REJECT, |
131 | | REQUEST_MIC |
132 | | }; |
133 | | |
134 | | typedef struct |
135 | | { |
136 | | enum NegState negState; |
137 | | BOOL init; |
138 | | WinPrAsn1_OID supportedMech; |
139 | | SecBuffer mechTypes; |
140 | | SecBuffer mechToken; |
141 | | SecBuffer mic; |
142 | | } NegToken; |
143 | | |
144 | | static const NegToken empty_neg_token = { NOSTATE, FALSE, |
145 | | { 0, nullptr }, { 0, 0, nullptr }, |
146 | | { 0, 0, nullptr }, { 0, 0, nullptr } }; |
147 | | |
148 | | static NEGOTIATE_CONTEXT* negotiate_ContextNew(NEGOTIATE_CONTEXT* init_context) |
149 | 0 | { |
150 | 0 | NEGOTIATE_CONTEXT* context = nullptr; |
151 | |
|
152 | 0 | WINPR_ASSERT(init_context); |
153 | |
|
154 | 0 | context = calloc(1, sizeof(NEGOTIATE_CONTEXT)); |
155 | 0 | if (!context) |
156 | 0 | return nullptr; |
157 | | |
158 | 0 | if (init_context->spnego) |
159 | 0 | { |
160 | 0 | init_context->mechTypes.pvBuffer = malloc(init_context->mechTypes.cbBuffer); |
161 | 0 | if (!init_context->mechTypes.pvBuffer) |
162 | 0 | { |
163 | 0 | free(context); |
164 | 0 | return nullptr; |
165 | 0 | } |
166 | 0 | } |
167 | | |
168 | 0 | *context = *init_context; |
169 | |
|
170 | 0 | return context; |
171 | 0 | } |
172 | | |
173 | | static void negotiate_ContextFree(NEGOTIATE_CONTEXT* context) |
174 | 0 | { |
175 | 0 | WINPR_ASSERT(context); |
176 | |
|
177 | 0 | if (context->mechTypes.pvBuffer) |
178 | 0 | free(context->mechTypes.pvBuffer); |
179 | 0 | free(context); |
180 | 0 | } |
181 | | |
182 | | static const char* negotiate_mech_name(const WinPrAsn1_OID* oid) |
183 | 0 | { |
184 | 0 | if (sspi_gss_oid_compare(oid, &spnego_OID)) |
185 | 0 | return "SPNEGO (1.3.6.1.5.5.2)"; |
186 | 0 | else if (sspi_gss_oid_compare(oid, &kerberos_u2u_OID)) |
187 | 0 | return "Kerberos user to user (1.2.840.113554.1.2.2.3)"; |
188 | 0 | else if (sspi_gss_oid_compare(oid, &kerberos_OID)) |
189 | 0 | return "Kerberos (1.2.840.113554.1.2.2)"; |
190 | 0 | else if (sspi_gss_oid_compare(oid, &kerberos_wrong_OID)) |
191 | 0 | return "Kerberos [wrong OID] (1.2.840.48018.1.2.2)"; |
192 | 0 | else if (sspi_gss_oid_compare(oid, &ntlm_OID)) |
193 | 0 | return "NTLM (1.3.6.1.4.1.311.2.2.10)"; |
194 | 0 | else if (sspi_gss_oid_compare(oid, &negoex_OID)) |
195 | 0 | return "NegoEx (1.3.6.1.4.1.311.2.2.30)"; |
196 | 0 | else |
197 | 0 | return "Unknown mechanism"; |
198 | 0 | } |
199 | | |
200 | | static const Mech* negotiate_GetMechByOID(const WinPrAsn1_OID* oid) |
201 | 0 | { |
202 | 0 | WINPR_ASSERT(oid); |
203 | |
|
204 | 0 | WinPrAsn1_OID testOid = *oid; |
205 | |
|
206 | 0 | if (sspi_gss_oid_compare(&testOid, &kerberos_wrong_OID)) |
207 | 0 | { |
208 | 0 | testOid.len = kerberos_OID.len; |
209 | 0 | testOid.data = kerberos_OID.data; |
210 | 0 | } |
211 | |
|
212 | 0 | for (size_t i = 0; i < MECH_COUNT; i++) |
213 | 0 | { |
214 | 0 | if (sspi_gss_oid_compare(&testOid, MechTable[i].oid)) |
215 | 0 | return &MechTable[i]; |
216 | 0 | } |
217 | 0 | return nullptr; |
218 | 0 | } |
219 | | |
220 | | static PSecHandle negotiate_FindCredential(MechCred* creds, const Mech* mech) |
221 | 0 | { |
222 | 0 | WINPR_ASSERT(creds); |
223 | |
|
224 | 0 | if (!mech) |
225 | 0 | return nullptr; |
226 | | |
227 | 0 | for (size_t i = 0; i < MECH_COUNT; i++) |
228 | 0 | { |
229 | 0 | MechCred* cred = &creds[i]; |
230 | |
|
231 | 0 | if (cred->mech == mech) |
232 | 0 | { |
233 | 0 | if (cred->valid) |
234 | 0 | return &cred->cred; |
235 | 0 | return nullptr; |
236 | 0 | } |
237 | 0 | } |
238 | | |
239 | 0 | return nullptr; |
240 | 0 | } |
241 | | |
242 | | static BOOL negotiate_get_dword(HKEY hKey, const char* subkey, DWORD* pdwValue) |
243 | 0 | { |
244 | 0 | DWORD dwValue = 0; |
245 | 0 | DWORD dwType = 0; |
246 | 0 | DWORD dwSize = sizeof(dwValue); |
247 | 0 | LONG rc = RegQueryValueExA(hKey, subkey, nullptr, &dwType, (BYTE*)&dwValue, &dwSize); |
248 | |
|
249 | 0 | if (rc != ERROR_SUCCESS) |
250 | 0 | return FALSE; |
251 | 0 | if (dwType != REG_DWORD) |
252 | 0 | return FALSE; |
253 | | |
254 | 0 | *pdwValue = dwValue; |
255 | 0 | return TRUE; |
256 | 0 | } |
257 | | |
258 | | static BOOL negotiate_get_config_from_auth_package_list(void* pAuthData, BOOL* kerberos, BOOL* ntlm, |
259 | | BOOL* u2u) |
260 | 0 | { |
261 | 0 | BOOL rc = FALSE; |
262 | 0 | char* tok_ctx = nullptr; |
263 | 0 | char* PackageList = nullptr; |
264 | |
|
265 | 0 | if (!sspi_CopyAuthPackageListA((const SEC_WINNT_AUTH_IDENTITY_INFO*)pAuthData, &PackageList)) |
266 | 0 | return FALSE; |
267 | | |
268 | 0 | char* tok_ptr = strtok_s(PackageList, ",", &tok_ctx); |
269 | |
|
270 | 0 | while (tok_ptr) |
271 | 0 | { |
272 | 0 | const char* PackageName = tok_ptr; |
273 | 0 | BOOL PackageInclude = TRUE; |
274 | |
|
275 | 0 | if (PackageName[0] == '!') |
276 | 0 | { |
277 | 0 | PackageName = &PackageName[1]; |
278 | 0 | PackageInclude = FALSE; |
279 | 0 | } |
280 | |
|
281 | 0 | if (_stricmp(PackageName, PACKAGE_NAME_NTLM) == 0) |
282 | 0 | { |
283 | 0 | *ntlm = PackageInclude; |
284 | 0 | } |
285 | 0 | else if (_stricmp(PackageName, PACKAGE_NAME_KERBEROS) == 0) |
286 | 0 | { |
287 | 0 | *kerberos = PackageInclude; |
288 | 0 | } |
289 | 0 | else if (_stricmp(PackageName, PACKAGE_NAME_KERBEROS_U2U) == 0) |
290 | 0 | { |
291 | 0 | *u2u = PackageInclude; |
292 | 0 | } |
293 | 0 | else if (_stricmp(PackageName, PACKAGE_NAME_DISABLE_ALL) == 0) |
294 | 0 | { |
295 | 0 | *kerberos = FALSE; |
296 | 0 | *ntlm = FALSE; |
297 | 0 | *u2u = FALSE; |
298 | |
|
299 | 0 | if (PackageName != PackageList) |
300 | 0 | { |
301 | 0 | WLog_WARN(TAG, "Special keyword '%s' not first in list, aborting", PackageName); |
302 | 0 | goto fail; |
303 | 0 | } |
304 | 0 | } |
305 | 0 | else |
306 | 0 | { |
307 | 0 | WLog_WARN(TAG, "Unknown authentication package name: %s, ignoring", PackageName); |
308 | 0 | } |
309 | | |
310 | 0 | tok_ptr = strtok_s(nullptr, ",", &tok_ctx); |
311 | 0 | } |
312 | | |
313 | 0 | rc = TRUE; |
314 | 0 | fail: |
315 | 0 | free(PackageList); |
316 | 0 | return rc; |
317 | 0 | } |
318 | | |
319 | | static BOOL negotiate_get_config(void* pAuthData, BOOL* kerberos, BOOL* ntlm, BOOL* u2u) |
320 | 0 | { |
321 | 0 | HKEY hKey = nullptr; |
322 | |
|
323 | 0 | WINPR_ASSERT(kerberos); |
324 | 0 | WINPR_ASSERT(ntlm); |
325 | 0 | WINPR_ASSERT(u2u); |
326 | |
|
327 | 0 | #if !defined(WITH_KRB5_NO_NTLM_FALLBACK) |
328 | 0 | *ntlm = TRUE; |
329 | | #else |
330 | | *ntlm = FALSE; |
331 | | #endif |
332 | 0 | #if defined(WITH_KRB5) |
333 | 0 | *kerberos = TRUE; |
334 | 0 | *u2u = TRUE; |
335 | | #else |
336 | | *kerberos = FALSE; |
337 | | *u2u = FALSE; |
338 | | #endif |
339 | |
|
340 | 0 | if (negotiate_get_config_from_auth_package_list(pAuthData, kerberos, ntlm, u2u)) |
341 | 0 | { |
342 | 0 | return TRUE; // use explicit authentication package list |
343 | 0 | } |
344 | | |
345 | 0 | { |
346 | 0 | char* key = winpr_getApplicatonDetailsRegKey(NEGO_REG_KEY); |
347 | 0 | if (key) |
348 | 0 | { |
349 | 0 | const LONG rc = |
350 | 0 | RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_READ | KEY_WOW64_64KEY, &hKey); |
351 | 0 | free(key); |
352 | 0 | if (rc == ERROR_SUCCESS) |
353 | 0 | { |
354 | 0 | DWORD dwValue = 0; |
355 | |
|
356 | 0 | if (negotiate_get_dword(hKey, PACKAGE_NAME_KERBEROS, &dwValue)) |
357 | 0 | *kerberos = (dwValue != 0); |
358 | |
|
359 | 0 | if (negotiate_get_dword(hKey, PACKAGE_NAME_KERBEROS_U2U, &dwValue)) |
360 | 0 | *u2u = (dwValue != 0); |
361 | |
|
362 | 0 | #if !defined(WITH_KRB5_NO_NTLM_FALLBACK) |
363 | 0 | if (negotiate_get_dword(hKey, PACKAGE_NAME_NTLM, &dwValue)) |
364 | 0 | *ntlm = (dwValue != 0); |
365 | 0 | #endif |
366 | |
|
367 | 0 | RegCloseKey(hKey); |
368 | 0 | } |
369 | 0 | } |
370 | 0 | } |
371 | |
|
372 | 0 | return TRUE; |
373 | 0 | } |
374 | | |
375 | | static BOOL negotiate_write_neg_token(PSecBuffer output_buffer, NegToken* token) |
376 | 0 | { |
377 | 0 | WINPR_ASSERT(output_buffer); |
378 | 0 | WINPR_ASSERT(token); |
379 | |
|
380 | 0 | BOOL ret = FALSE; |
381 | 0 | WinPrAsn1Encoder* enc = nullptr; |
382 | 0 | WinPrAsn1_MemoryChunk mechTypes = { token->mechTypes.cbBuffer, token->mechTypes.pvBuffer }; |
383 | 0 | WinPrAsn1_OctetString mechToken = { token->mechToken.cbBuffer, token->mechToken.pvBuffer }; |
384 | 0 | WinPrAsn1_OctetString mechListMic = { token->mic.cbBuffer, token->mic.pvBuffer }; |
385 | 0 | wStream s; |
386 | 0 | size_t len = 0; |
387 | |
|
388 | 0 | enc = WinPrAsn1Encoder_New(WINPR_ASN1_DER); |
389 | 0 | if (!enc) |
390 | 0 | return FALSE; |
391 | | |
392 | | /* For NegTokenInit wrap in an initialContextToken */ |
393 | 0 | if (token->init) |
394 | 0 | { |
395 | | /* InitialContextToken [APPLICATION 0] IMPLICIT SEQUENCE */ |
396 | 0 | if (!WinPrAsn1EncAppContainer(enc, 0)) |
397 | 0 | goto cleanup; |
398 | | |
399 | | /* thisMech MechType OID */ |
400 | 0 | if (!WinPrAsn1EncOID(enc, &spnego_OID)) |
401 | 0 | goto cleanup; |
402 | 0 | } |
403 | | |
404 | | /* innerContextToken [0] NegTokenInit or [1] NegTokenResp */ |
405 | 0 | if (!WinPrAsn1EncContextualSeqContainer(enc, token->init ? 0 : 1)) |
406 | 0 | goto cleanup; |
407 | | |
408 | 0 | WLog_DBG(TAG, token->init ? "Writing negTokenInit..." : "Writing negTokenResp..."); |
409 | | |
410 | | /* mechTypes [0] MechTypeList (mechTypes already contains the SEQUENCE tag) */ |
411 | 0 | if (token->init) |
412 | 0 | { |
413 | 0 | if (!WinPrAsn1EncContextualRawContent(enc, 0, &mechTypes)) |
414 | 0 | goto cleanup; |
415 | 0 | WLog_DBG(TAG, "\tmechTypes [0] (%" PRIu32 " bytes)", token->mechTypes.cbBuffer); |
416 | 0 | } |
417 | | /* negState [0] ENUMERATED */ |
418 | 0 | else if (token->negState != NOSTATE) |
419 | 0 | { |
420 | 0 | if (!WinPrAsn1EncContextualEnumerated(enc, 0, token->negState)) |
421 | 0 | goto cleanup; |
422 | 0 | WLog_DBG(TAG, "\tnegState [0] (%d)", token->negState); |
423 | 0 | } |
424 | | |
425 | | /* supportedMech [1] OID */ |
426 | 0 | if (token->supportedMech.len) |
427 | 0 | { |
428 | 0 | if (!WinPrAsn1EncContextualOID(enc, 1, &token->supportedMech)) |
429 | 0 | goto cleanup; |
430 | 0 | WLog_DBG(TAG, "\tsupportedMech [1] (%s)", negotiate_mech_name(&token->supportedMech)); |
431 | 0 | } |
432 | | |
433 | | /* mechToken [2] OCTET STRING */ |
434 | 0 | if (token->mechToken.cbBuffer) |
435 | 0 | { |
436 | 0 | if (WinPrAsn1EncContextualOctetString(enc, 2, &mechToken) == 0) |
437 | 0 | goto cleanup; |
438 | 0 | WLog_DBG(TAG, "\tmechToken [2] (%" PRIu32 " bytes)", token->mechToken.cbBuffer); |
439 | 0 | } |
440 | | |
441 | | /* mechListMIC [3] OCTET STRING */ |
442 | 0 | if (token->mic.cbBuffer) |
443 | 0 | { |
444 | 0 | if (WinPrAsn1EncContextualOctetString(enc, 3, &mechListMic) == 0) |
445 | 0 | goto cleanup; |
446 | 0 | WLog_DBG(TAG, "\tmechListMIC [3] (%" PRIu32 " bytes)", token->mic.cbBuffer); |
447 | 0 | } |
448 | | |
449 | | /* NegTokenInit or NegTokenResp */ |
450 | 0 | if (!WinPrAsn1EncEndContainer(enc)) |
451 | 0 | goto cleanup; |
452 | | |
453 | 0 | if (token->init) |
454 | 0 | { |
455 | | /* initialContextToken */ |
456 | 0 | if (!WinPrAsn1EncEndContainer(enc)) |
457 | 0 | goto cleanup; |
458 | 0 | } |
459 | | |
460 | 0 | if (!WinPrAsn1EncStreamSize(enc, &len) || len > output_buffer->cbBuffer) |
461 | 0 | goto cleanup; |
462 | | |
463 | 0 | if (len > UINT32_MAX) |
464 | 0 | goto cleanup; |
465 | | |
466 | 0 | Stream_StaticInit(&s, output_buffer->pvBuffer, len); |
467 | |
|
468 | 0 | if (WinPrAsn1EncToStream(enc, &s)) |
469 | 0 | { |
470 | 0 | output_buffer->cbBuffer = (UINT32)len; |
471 | 0 | ret = TRUE; |
472 | 0 | } |
473 | |
|
474 | 0 | cleanup: |
475 | 0 | WinPrAsn1Encoder_Free(&enc); |
476 | 0 | return ret; |
477 | 0 | } |
478 | | |
479 | | static BOOL negotiate_read_neg_token(PSecBuffer input, NegToken* token) |
480 | 0 | { |
481 | 0 | WinPrAsn1Decoder dec = WinPrAsn1Decoder_init(); |
482 | 0 | WinPrAsn1Decoder dec2 = WinPrAsn1Decoder_init(); |
483 | 0 | WinPrAsn1_OID oid = WINPR_C_ARRAY_INIT; |
484 | 0 | WinPrAsn1_tagId contextual = 0; |
485 | 0 | WinPrAsn1_tag tag = 0; |
486 | 0 | size_t len = 0; |
487 | 0 | WinPrAsn1_OctetString octet_string = WINPR_C_ARRAY_INIT; |
488 | 0 | BOOL err = 0; |
489 | |
|
490 | 0 | WINPR_ASSERT(input); |
491 | 0 | WINPR_ASSERT(token); |
492 | |
|
493 | 0 | WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_DER, input->pvBuffer, input->cbBuffer); |
494 | |
|
495 | 0 | if (!WinPrAsn1DecPeekTag(&dec, &tag)) |
496 | 0 | return FALSE; |
497 | | |
498 | 0 | if (tag == 0x60) |
499 | 0 | { |
500 | | /* initialContextToken [APPLICATION 0] */ |
501 | 0 | if (!WinPrAsn1DecReadApp(&dec, &tag, &dec2) || tag != 0) |
502 | 0 | return FALSE; |
503 | 0 | dec = dec2; |
504 | | |
505 | | /* thisMech OID */ |
506 | 0 | if (!WinPrAsn1DecReadOID(&dec, &oid, FALSE)) |
507 | 0 | return FALSE; |
508 | | |
509 | 0 | if (!sspi_gss_oid_compare(&spnego_OID, &oid)) |
510 | 0 | return FALSE; |
511 | | |
512 | | /* [0] NegTokenInit */ |
513 | 0 | if (!WinPrAsn1DecReadContextualSequence(&dec, 0, &err, &dec2)) |
514 | 0 | return FALSE; |
515 | | |
516 | 0 | token->init = TRUE; |
517 | 0 | } |
518 | | /* [1] NegTokenResp */ |
519 | 0 | else if (!WinPrAsn1DecReadContextualSequence(&dec, 1, &err, &dec2)) |
520 | 0 | return FALSE; |
521 | 0 | dec = dec2; |
522 | |
|
523 | 0 | WLog_DBG(TAG, token->init ? "Reading negTokenInit..." : "Reading negTokenResp..."); |
524 | | |
525 | | /* Read NegTokenResp sequence members */ |
526 | 0 | do |
527 | 0 | { |
528 | 0 | if (!WinPrAsn1DecReadContextualTag(&dec, &contextual, &dec2)) |
529 | 0 | return FALSE; |
530 | | |
531 | 0 | switch (contextual) |
532 | 0 | { |
533 | 0 | case 0: |
534 | 0 | if (token->init) |
535 | 0 | { |
536 | | /* mechTypes [0] MechTypeList */ |
537 | 0 | wStream s = WinPrAsn1DecGetStream(&dec2); |
538 | 0 | token->mechTypes.BufferType = SECBUFFER_TOKEN; |
539 | 0 | const size_t mlen = Stream_Length(&s); |
540 | 0 | if (mlen > UINT32_MAX) |
541 | 0 | return FALSE; |
542 | 0 | token->mechTypes.cbBuffer = (UINT32)mlen; |
543 | 0 | token->mechTypes.pvBuffer = Stream_Buffer(&s); |
544 | 0 | WLog_DBG(TAG, "\tmechTypes [0] (%" PRIu32 " bytes)", token->mechTypes.cbBuffer); |
545 | 0 | } |
546 | 0 | else |
547 | 0 | { |
548 | | /* negState [0] ENUMERATED */ |
549 | 0 | WinPrAsn1_ENUMERATED rd = 0; |
550 | 0 | if (!WinPrAsn1DecReadEnumerated(&dec2, &rd)) |
551 | 0 | return FALSE; |
552 | 0 | switch (rd) |
553 | 0 | { |
554 | 0 | case NOSTATE: |
555 | 0 | case ACCEPT_COMPLETED: |
556 | 0 | case ACCEPT_INCOMPLETE: |
557 | 0 | case REJECT: |
558 | 0 | case REQUEST_MIC: |
559 | 0 | break; |
560 | 0 | default: |
561 | 0 | WLog_ERR(TAG, "Invalid negState enumeration value %d", rd); |
562 | 0 | return FALSE; |
563 | 0 | } |
564 | | |
565 | 0 | token->negState = WINPR_ASSERTING_INT_CAST(enum NegState, rd); |
566 | 0 | WLog_DBG(TAG, "\tnegState [0] (%d)", rd); |
567 | 0 | } |
568 | 0 | break; |
569 | 0 | case 1: |
570 | 0 | if (token->init) |
571 | 0 | { |
572 | | /* reqFlags [1] ContextFlags BIT STRING (ignored) */ |
573 | 0 | if (!WinPrAsn1DecPeekTagAndLen(&dec2, &tag, &len) || (tag != ER_TAG_BIT_STRING)) |
574 | 0 | return FALSE; |
575 | 0 | WLog_DBG(TAG, "\treqFlags [1] (%" PRIuz " bytes)", len); |
576 | 0 | } |
577 | 0 | else |
578 | 0 | { |
579 | | /* supportedMech [1] MechType */ |
580 | 0 | if (!WinPrAsn1DecReadOID(&dec2, &token->supportedMech, FALSE)) |
581 | 0 | return FALSE; |
582 | 0 | WLog_DBG(TAG, "\tsupportedMech [1] (%s)", |
583 | 0 | negotiate_mech_name(&token->supportedMech)); |
584 | 0 | } |
585 | 0 | break; |
586 | 0 | case 2: |
587 | | /* mechToken [2] OCTET STRING */ |
588 | 0 | if (!WinPrAsn1DecReadOctetString(&dec2, &octet_string, FALSE)) |
589 | 0 | return FALSE; |
590 | 0 | if (octet_string.len > UINT32_MAX) |
591 | 0 | return FALSE; |
592 | 0 | token->mechToken.cbBuffer = (UINT32)octet_string.len; |
593 | 0 | token->mechToken.pvBuffer = octet_string.data; |
594 | 0 | token->mechToken.BufferType = SECBUFFER_TOKEN; |
595 | 0 | WLog_DBG(TAG, "\tmechToken [2] (%" PRIuz " bytes)", octet_string.len); |
596 | 0 | break; |
597 | 0 | case 3: |
598 | | /* mechListMic [3] OCTET STRING */ |
599 | 0 | if (!WinPrAsn1DecReadOctetString(&dec2, &octet_string, FALSE)) |
600 | 0 | return FALSE; |
601 | 0 | if (octet_string.len > UINT32_MAX) |
602 | 0 | return FALSE; |
603 | 0 | token->mic.cbBuffer = (UINT32)octet_string.len; |
604 | 0 | token->mic.pvBuffer = octet_string.data; |
605 | 0 | token->mic.BufferType = SECBUFFER_TOKEN; |
606 | 0 | WLog_DBG(TAG, "\tmechListMIC [3] (%" PRIuz " bytes)", octet_string.len); |
607 | 0 | break; |
608 | 0 | default: |
609 | 0 | WLog_ERR(TAG, "unknown contextual item %d", contextual); |
610 | 0 | return FALSE; |
611 | 0 | } |
612 | 0 | } while (WinPrAsn1DecPeekTag(&dec, &tag)); |
613 | | |
614 | 0 | return TRUE; |
615 | 0 | } |
616 | | |
617 | | static SECURITY_STATUS negotiate_mic_exchange(NEGOTIATE_CONTEXT* context, NegToken* input_token, |
618 | | NegToken* output_token, PSecBuffer output_buffer) |
619 | 0 | { |
620 | 0 | SecBuffer mic_buffers[2] = WINPR_C_ARRAY_INIT; |
621 | 0 | SecBufferDesc mic_buffer_desc = { SECBUFFER_VERSION, 2, mic_buffers }; |
622 | 0 | SECURITY_STATUS status = 0; |
623 | |
|
624 | 0 | WINPR_ASSERT(context); |
625 | 0 | WINPR_ASSERT(input_token); |
626 | 0 | WINPR_ASSERT(output_token); |
627 | 0 | WINPR_ASSERT(context->mech); |
628 | 0 | WINPR_ASSERT(context->mech->pkg); |
629 | |
|
630 | 0 | const SecurityFunctionTableA* table = context->mech->pkg->table; |
631 | 0 | WINPR_ASSERT(table); |
632 | |
|
633 | 0 | mic_buffers[0] = context->mechTypes; |
634 | | |
635 | | /* Verify MIC if we received one */ |
636 | 0 | if (input_token->mic.cbBuffer > 0) |
637 | 0 | { |
638 | 0 | mic_buffers[1] = input_token->mic; |
639 | |
|
640 | 0 | status = table->VerifySignature(&context->sub_context, &mic_buffer_desc, 0, nullptr); |
641 | 0 | if (status != SEC_E_OK) |
642 | 0 | return status; |
643 | | |
644 | 0 | output_token->negState = ACCEPT_COMPLETED; |
645 | 0 | } |
646 | | |
647 | | /* If peer expects a MIC then generate it */ |
648 | 0 | if (input_token->negState != ACCEPT_COMPLETED) |
649 | 0 | { |
650 | | /* Store the mic token after the mech token in the output buffer */ |
651 | 0 | output_token->mic.BufferType = SECBUFFER_TOKEN; |
652 | 0 | if (output_buffer) |
653 | 0 | { |
654 | 0 | output_token->mic.cbBuffer = output_buffer->cbBuffer - output_token->mechToken.cbBuffer; |
655 | 0 | output_token->mic.pvBuffer = |
656 | 0 | (BYTE*)output_buffer->pvBuffer + output_token->mechToken.cbBuffer; |
657 | 0 | } |
658 | 0 | mic_buffers[1] = output_token->mic; |
659 | |
|
660 | 0 | status = table->MakeSignature(&context->sub_context, 0, &mic_buffer_desc, 0); |
661 | 0 | if (status != SEC_E_OK) |
662 | 0 | return status; |
663 | | |
664 | 0 | output_token->mic = mic_buffers[1]; |
665 | 0 | } |
666 | | |
667 | | /* When using NTLM cipher states need to be reset after mic exchange */ |
668 | 0 | const TCHAR* name = sspi_SecureHandleGetUpperPointer(&context->sub_context); |
669 | 0 | if (!name) |
670 | 0 | return SEC_E_INTERNAL_ERROR; |
671 | | |
672 | 0 | if (_tcsncmp(name, NTLM_SSP_NAME, ARRAYSIZE(NTLM_SSP_NAME)) == 0) |
673 | 0 | { |
674 | 0 | if (!ntlm_reset_cipher_state(&context->sub_context)) |
675 | 0 | return SEC_E_INTERNAL_ERROR; |
676 | 0 | } |
677 | | |
678 | 0 | return SEC_E_OK; |
679 | 0 | } |
680 | | |
681 | | static SECURITY_STATUS SEC_ENTRY negotiate_InitializeSecurityContextW( |
682 | | PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq, |
683 | | ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2, |
684 | | PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry) |
685 | 0 | { |
686 | 0 | NEGOTIATE_CONTEXT* context = nullptr; |
687 | 0 | NEGOTIATE_CONTEXT init_context = NEGOTIATE_CONTEXT_init(); |
688 | 0 | MechCred* creds = nullptr; |
689 | 0 | PCtxtHandle sub_context = nullptr; |
690 | 0 | PCredHandle sub_cred = nullptr; |
691 | 0 | NegToken input_token = empty_neg_token; |
692 | 0 | NegToken output_token = empty_neg_token; |
693 | 0 | PSecBuffer input_buffer = nullptr; |
694 | 0 | PSecBuffer output_buffer = nullptr; |
695 | 0 | PSecBuffer bindings_buffer = nullptr; |
696 | 0 | SecBuffer mech_input_buffers[2] = WINPR_C_ARRAY_INIT; |
697 | 0 | SecBufferDesc mech_input = { SECBUFFER_VERSION, 2, mech_input_buffers }; |
698 | 0 | SecBufferDesc mech_output = { SECBUFFER_VERSION, 1, &output_token.mechToken }; |
699 | 0 | SECURITY_STATUS status = SEC_E_INTERNAL_ERROR; |
700 | 0 | SECURITY_STATUS sub_status = SEC_E_INTERNAL_ERROR; |
701 | 0 | WinPrAsn1Encoder* enc = nullptr; |
702 | 0 | wStream s; |
703 | 0 | const Mech* mech = nullptr; |
704 | |
|
705 | 0 | if (!phCredential || !SecIsValidHandle(phCredential)) |
706 | 0 | return SEC_E_NO_CREDENTIALS; |
707 | | |
708 | 0 | creds = sspi_SecureHandleGetLowerPointer(phCredential); |
709 | | |
710 | | /* behave like windows SSPIs that don't want empty context */ |
711 | 0 | if (phContext && !phContext->dwLower && !phContext->dwUpper) |
712 | 0 | return SEC_E_INVALID_HANDLE; |
713 | | |
714 | 0 | context = sspi_SecureHandleGetLowerPointer(phContext); |
715 | |
|
716 | 0 | if (pInput) |
717 | 0 | { |
718 | 0 | input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN); |
719 | 0 | bindings_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_CHANNEL_BINDINGS); |
720 | 0 | } |
721 | 0 | if (pOutput) |
722 | 0 | output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN); |
723 | |
|
724 | 0 | if (!context) |
725 | 0 | { |
726 | 0 | enc = WinPrAsn1Encoder_New(WINPR_ASN1_DER); |
727 | 0 | if (!enc) |
728 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
729 | | |
730 | 0 | if (!WinPrAsn1EncSeqContainer(enc)) |
731 | 0 | goto cleanup; |
732 | | |
733 | 0 | for (size_t i = 0; i < MECH_COUNT; i++) |
734 | 0 | { |
735 | 0 | MechCred* cred = &creds[i]; |
736 | 0 | const SecPkg* pkg = MechTable[i].pkg; |
737 | 0 | WINPR_ASSERT(pkg); |
738 | 0 | WINPR_ASSERT(pkg->table_w); |
739 | |
|
740 | 0 | if (!cred->valid) |
741 | 0 | { |
742 | 0 | WLog_DBG(TAG, "Unavailable mechanism: %s", negotiate_mech_name(cred->mech->oid)); |
743 | 0 | continue; |
744 | 0 | } |
745 | | |
746 | | /* Send an optimistic token for the first valid mechanism */ |
747 | 0 | if (!init_context.mech) |
748 | 0 | { |
749 | | /* Use the output buffer to store the optimistic token */ |
750 | 0 | if (!output_buffer) |
751 | 0 | goto cleanup; |
752 | | |
753 | 0 | CopyMemory(&output_token.mechToken, output_buffer, sizeof(SecBuffer)); |
754 | |
|
755 | 0 | if (bindings_buffer) |
756 | 0 | mech_input_buffers[0] = *bindings_buffer; |
757 | |
|
758 | 0 | WINPR_ASSERT(pkg->table_w->InitializeSecurityContextW); |
759 | 0 | sub_status = pkg->table_w->InitializeSecurityContextW( |
760 | 0 | &cred->cred, nullptr, pszTargetName, fContextReq | cred->mech->flags, Reserved1, |
761 | 0 | TargetDataRep, &mech_input, Reserved2, &init_context.sub_context, &mech_output, |
762 | 0 | pfContextAttr, ptsExpiry); |
763 | | |
764 | | /* If the mechanism failed we can't use it; skip */ |
765 | 0 | if (IsSecurityStatusError(sub_status)) |
766 | 0 | { |
767 | 0 | if (SecIsValidHandle(&init_context.sub_context)) |
768 | 0 | { |
769 | 0 | WINPR_ASSERT(pkg->table_w->DeleteSecurityContext); |
770 | 0 | pkg->table_w->DeleteSecurityContext(&init_context.sub_context); |
771 | 0 | } |
772 | 0 | cred->valid = FALSE; |
773 | 0 | continue; |
774 | 0 | } |
775 | | |
776 | 0 | init_context.mech = cred->mech; |
777 | 0 | } |
778 | | |
779 | 0 | if (!WinPrAsn1EncOID(enc, cred->mech->oid)) |
780 | 0 | goto cleanup; |
781 | 0 | WLog_DBG(TAG, "Available mechanism: %s", negotiate_mech_name(cred->mech->oid)); |
782 | 0 | } |
783 | | |
784 | | /* No usable mechanisms were found */ |
785 | 0 | if (!init_context.mech) |
786 | 0 | goto cleanup; |
787 | | |
788 | | /* If the only available mech is NTLM use it directly otherwise use spnego */ |
789 | 0 | if (init_context.mech->oid == &ntlm_OID) |
790 | 0 | { |
791 | 0 | init_context.spnego = FALSE; |
792 | 0 | output_buffer->cbBuffer = output_token.mechToken.cbBuffer; |
793 | 0 | WLog_DBG(TAG, "Using direct NTLM"); |
794 | 0 | } |
795 | 0 | else |
796 | 0 | { |
797 | 0 | init_context.spnego = TRUE; |
798 | 0 | init_context.mechTypes.BufferType = SECBUFFER_DATA; |
799 | 0 | const size_t cb = WinPrAsn1EncEndContainer(enc); |
800 | 0 | WINPR_ASSERT(cb <= UINT32_MAX); |
801 | 0 | init_context.mechTypes.cbBuffer = (UINT32)cb; |
802 | 0 | } |
803 | | |
804 | | /* Allocate memory for the new context */ |
805 | 0 | context = negotiate_ContextNew(&init_context); |
806 | 0 | if (!context) |
807 | 0 | { |
808 | 0 | init_context.mech->pkg->table->DeleteSecurityContext(&init_context.sub_context); |
809 | 0 | WinPrAsn1Encoder_Free(&enc); |
810 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
811 | 0 | } |
812 | | |
813 | 0 | sspi_SecureHandleSetUpperPointer(phNewContext, NEGO_SSP_NAME); |
814 | 0 | sspi_SecureHandleSetLowerPointer(phNewContext, context); |
815 | |
|
816 | 0 | if (!context->spnego) |
817 | 0 | { |
818 | 0 | status = sub_status; |
819 | 0 | goto cleanup; |
820 | 0 | } |
821 | | |
822 | | /* Write mechTypesList */ |
823 | 0 | Stream_StaticInit(&s, context->mechTypes.pvBuffer, context->mechTypes.cbBuffer); |
824 | 0 | if (!WinPrAsn1EncToStream(enc, &s)) |
825 | 0 | goto cleanup; |
826 | | |
827 | 0 | output_token.mechTypes.cbBuffer = context->mechTypes.cbBuffer; |
828 | 0 | output_token.mechTypes.pvBuffer = context->mechTypes.pvBuffer; |
829 | 0 | output_token.init = TRUE; |
830 | |
|
831 | 0 | if (sub_status == SEC_E_OK) |
832 | 0 | context->state = NEGOTIATE_STATE_FINAL_OPTIMISTIC; |
833 | 0 | } |
834 | 0 | else |
835 | 0 | { |
836 | 0 | if (!input_buffer) |
837 | 0 | return SEC_E_INVALID_TOKEN; |
838 | | |
839 | 0 | sub_context = &context->sub_context; |
840 | 0 | sub_cred = negotiate_FindCredential(creds, context->mech); |
841 | |
|
842 | 0 | if (!context->spnego) |
843 | 0 | { |
844 | 0 | return context->mech->pkg->table_w->InitializeSecurityContextW( |
845 | 0 | sub_cred, sub_context, pszTargetName, fContextReq | context->mech->flags, Reserved1, |
846 | 0 | TargetDataRep, pInput, Reserved2, sub_context, pOutput, pfContextAttr, ptsExpiry); |
847 | 0 | } |
848 | | |
849 | 0 | if (!negotiate_read_neg_token(input_buffer, &input_token)) |
850 | 0 | return SEC_E_INVALID_TOKEN; |
851 | | |
852 | | /* On first response check if the server doesn't like out preferred mech */ |
853 | 0 | if (context->state < NEGOTIATE_STATE_NEGORESP && input_token.supportedMech.len && |
854 | 0 | !sspi_gss_oid_compare(&input_token.supportedMech, context->mech->oid)) |
855 | 0 | { |
856 | 0 | mech = negotiate_GetMechByOID(&input_token.supportedMech); |
857 | 0 | if (!mech) |
858 | 0 | return SEC_E_INVALID_TOKEN; |
859 | | |
860 | | /* Make sure the specified mech is supported and get the appropriate credential */ |
861 | 0 | sub_cred = negotiate_FindCredential(creds, mech); |
862 | 0 | if (!sub_cred) |
863 | 0 | return SEC_E_INVALID_TOKEN; |
864 | | |
865 | | /* Clean up the optimistic mech */ |
866 | 0 | context->mech->pkg->table_w->DeleteSecurityContext(&context->sub_context); |
867 | 0 | sub_context = nullptr; |
868 | |
|
869 | 0 | context->mech = mech; |
870 | 0 | context->mic = TRUE; |
871 | 0 | } |
872 | | |
873 | | /* Check neg_state (required on first response) */ |
874 | 0 | if (context->state < NEGOTIATE_STATE_NEGORESP) |
875 | 0 | { |
876 | 0 | switch (input_token.negState) |
877 | 0 | { |
878 | 0 | case NOSTATE: |
879 | 0 | return SEC_E_INVALID_TOKEN; |
880 | 0 | case REJECT: |
881 | 0 | return SEC_E_LOGON_DENIED; |
882 | 0 | case REQUEST_MIC: |
883 | 0 | context->mic = TRUE; |
884 | | /* fallthrough */ |
885 | 0 | WINPR_FALLTHROUGH |
886 | 0 | case ACCEPT_INCOMPLETE: |
887 | 0 | context->state = NEGOTIATE_STATE_NEGORESP; |
888 | 0 | break; |
889 | 0 | case ACCEPT_COMPLETED: |
890 | 0 | if (context->state == NEGOTIATE_STATE_INITIAL) |
891 | 0 | context->state = NEGOTIATE_STATE_NEGORESP; |
892 | 0 | else |
893 | 0 | context->state = NEGOTIATE_STATE_FINAL; |
894 | 0 | break; |
895 | 0 | default: |
896 | 0 | break; |
897 | 0 | } |
898 | | |
899 | 0 | WLog_DBG(TAG, "Negotiated mechanism: %s", negotiate_mech_name(context->mech->oid)); |
900 | 0 | } |
901 | | |
902 | 0 | if (context->state == NEGOTIATE_STATE_NEGORESP) |
903 | 0 | { |
904 | | /* Store the mech token in the output buffer */ |
905 | 0 | if (!output_buffer) |
906 | 0 | goto cleanup; |
907 | 0 | CopyMemory(&output_token.mechToken, output_buffer, sizeof(SecBuffer)); |
908 | |
|
909 | 0 | mech_input_buffers[0] = input_token.mechToken; |
910 | 0 | if (bindings_buffer) |
911 | 0 | mech_input_buffers[1] = *bindings_buffer; |
912 | |
|
913 | 0 | status = context->mech->pkg->table_w->InitializeSecurityContextW( |
914 | 0 | sub_cred, sub_context, pszTargetName, fContextReq | context->mech->flags, Reserved1, |
915 | 0 | TargetDataRep, input_token.mechToken.cbBuffer ? &mech_input : nullptr, Reserved2, |
916 | 0 | &context->sub_context, &mech_output, pfContextAttr, ptsExpiry); |
917 | |
|
918 | 0 | if (IsSecurityStatusError(status)) |
919 | 0 | return status; |
920 | 0 | } |
921 | | |
922 | 0 | if (status == SEC_E_OK) |
923 | 0 | { |
924 | 0 | if (output_token.mechToken.cbBuffer > 0) |
925 | 0 | context->state = NEGOTIATE_STATE_MIC; |
926 | 0 | else |
927 | 0 | context->state = NEGOTIATE_STATE_FINAL; |
928 | 0 | } |
929 | | |
930 | | /* Check if the acceptor sent its final token without a mic */ |
931 | 0 | if (context->state == NEGOTIATE_STATE_FINAL && input_token.mic.cbBuffer == 0) |
932 | 0 | { |
933 | 0 | if (context->mic || input_token.negState != ACCEPT_COMPLETED) |
934 | 0 | return SEC_E_INVALID_TOKEN; |
935 | | |
936 | 0 | if (output_buffer) |
937 | 0 | output_buffer->cbBuffer = 0; |
938 | 0 | return SEC_E_OK; |
939 | 0 | } |
940 | | |
941 | 0 | if ((context->state == NEGOTIATE_STATE_MIC && context->mic) || |
942 | 0 | context->state == NEGOTIATE_STATE_FINAL) |
943 | 0 | { |
944 | 0 | status = negotiate_mic_exchange(context, &input_token, &output_token, output_buffer); |
945 | 0 | if (status != SEC_E_OK) |
946 | 0 | return status; |
947 | 0 | } |
948 | 0 | } |
949 | | |
950 | 0 | if (input_token.negState == ACCEPT_COMPLETED) |
951 | 0 | { |
952 | 0 | if (output_buffer) |
953 | 0 | output_buffer->cbBuffer = 0; |
954 | 0 | return SEC_E_OK; |
955 | 0 | } |
956 | | |
957 | 0 | if (output_token.negState == ACCEPT_COMPLETED) |
958 | 0 | status = SEC_E_OK; |
959 | 0 | else |
960 | 0 | status = SEC_I_CONTINUE_NEEDED; |
961 | |
|
962 | 0 | if (!negotiate_write_neg_token(output_buffer, &output_token)) |
963 | 0 | status = SEC_E_INTERNAL_ERROR; |
964 | |
|
965 | 0 | cleanup: |
966 | 0 | WinPrAsn1Encoder_Free(&enc); |
967 | 0 | return status; |
968 | 0 | } |
969 | | |
970 | | static SECURITY_STATUS SEC_ENTRY negotiate_InitializeSecurityContextA( |
971 | | PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR* pszTargetName, ULONG fContextReq, |
972 | | ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2, |
973 | | PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry) |
974 | 0 | { |
975 | 0 | SECURITY_STATUS status = 0; |
976 | 0 | SEC_WCHAR* pszTargetNameW = nullptr; |
977 | |
|
978 | 0 | if (pszTargetName) |
979 | 0 | { |
980 | 0 | pszTargetNameW = ConvertUtf8ToWCharAlloc(pszTargetName, nullptr); |
981 | 0 | if (!pszTargetNameW) |
982 | 0 | return SEC_E_INTERNAL_ERROR; |
983 | 0 | } |
984 | | |
985 | 0 | status = negotiate_InitializeSecurityContextW( |
986 | 0 | phCredential, phContext, pszTargetNameW, fContextReq, Reserved1, TargetDataRep, pInput, |
987 | 0 | Reserved2, phNewContext, pOutput, pfContextAttr, ptsExpiry); |
988 | 0 | free(pszTargetNameW); |
989 | 0 | return status; |
990 | 0 | } |
991 | | |
992 | | static const Mech* guessMech(PSecBuffer input_buffer, BOOL* spNego, WinPrAsn1_OID* oid) |
993 | 0 | { |
994 | 0 | WinPrAsn1Decoder decoder = WinPrAsn1Decoder_init(); |
995 | 0 | WinPrAsn1Decoder appDecoder = WinPrAsn1Decoder_init(); |
996 | 0 | WinPrAsn1_tagId tag = 0; |
997 | 0 | const char ssp[] = "NTLMSSP"; |
998 | |
|
999 | 0 | *spNego = FALSE; |
1000 | | |
1001 | | /* Check for NTLM token */ |
1002 | 0 | if (input_buffer->cbBuffer >= 8 && strncmp(input_buffer->pvBuffer, ssp, sizeof(ssp)) == 0) |
1003 | 0 | { |
1004 | 0 | *oid = ntlm_OID; |
1005 | 0 | return negotiate_GetMechByOID(&ntlm_OID); |
1006 | 0 | } |
1007 | | |
1008 | | /* Read initialContextToken or raw Kerberos token */ |
1009 | 0 | WinPrAsn1Decoder_InitMem(&decoder, WINPR_ASN1_DER, input_buffer->pvBuffer, |
1010 | 0 | input_buffer->cbBuffer); |
1011 | |
|
1012 | 0 | if (!WinPrAsn1DecReadApp(&decoder, &tag, &appDecoder) || tag != 0) |
1013 | 0 | return nullptr; |
1014 | | |
1015 | 0 | if (!WinPrAsn1DecReadOID(&appDecoder, oid, FALSE)) |
1016 | 0 | return nullptr; |
1017 | | |
1018 | 0 | if (sspi_gss_oid_compare(oid, &spnego_OID)) |
1019 | 0 | { |
1020 | 0 | *spNego = TRUE; |
1021 | 0 | return nullptr; |
1022 | 0 | } |
1023 | | |
1024 | 0 | return negotiate_GetMechByOID(oid); |
1025 | 0 | } |
1026 | | |
1027 | | static SECURITY_STATUS SEC_ENTRY negotiate_AcceptSecurityContext( |
1028 | | PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput, ULONG fContextReq, |
1029 | | ULONG TargetDataRep, PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, |
1030 | | PTimeStamp ptsTimeStamp) |
1031 | 0 | { |
1032 | 0 | NEGOTIATE_CONTEXT* context = nullptr; |
1033 | 0 | NEGOTIATE_CONTEXT init_context = NEGOTIATE_CONTEXT_init(); |
1034 | 0 | MechCred* creds = nullptr; |
1035 | 0 | PCredHandle sub_cred = nullptr; |
1036 | 0 | NegToken input_token = empty_neg_token; |
1037 | 0 | NegToken output_token = empty_neg_token; |
1038 | 0 | PSecBuffer input_buffer = nullptr; |
1039 | 0 | PSecBuffer output_buffer = nullptr; |
1040 | 0 | SecBufferDesc mech_input = { SECBUFFER_VERSION, 1, &input_token.mechToken }; |
1041 | 0 | SecBufferDesc mech_output = { SECBUFFER_VERSION, 1, &output_token.mechToken }; |
1042 | 0 | SECURITY_STATUS status = SEC_E_INTERNAL_ERROR; |
1043 | 0 | WinPrAsn1Decoder dec = WinPrAsn1Decoder_init(); |
1044 | 0 | WinPrAsn1Decoder dec2 = WinPrAsn1Decoder_init(); |
1045 | 0 | WinPrAsn1_tagId tag = 0; |
1046 | 0 | WinPrAsn1_OID oid = WINPR_C_ARRAY_INIT; |
1047 | 0 | const Mech* first_mech = nullptr; |
1048 | |
|
1049 | 0 | if (!phCredential || !SecIsValidHandle(phCredential)) |
1050 | 0 | return SEC_E_NO_CREDENTIALS; |
1051 | | |
1052 | 0 | creds = sspi_SecureHandleGetLowerPointer(phCredential); |
1053 | |
|
1054 | 0 | if (!pInput) |
1055 | 0 | return SEC_E_INVALID_TOKEN; |
1056 | | |
1057 | | /* behave like windows SSPIs that don't want empty context */ |
1058 | 0 | if (phContext && !phContext->dwLower && !phContext->dwUpper) |
1059 | 0 | return SEC_E_INVALID_HANDLE; |
1060 | | |
1061 | 0 | context = sspi_SecureHandleGetLowerPointer(phContext); |
1062 | |
|
1063 | 0 | input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN); |
1064 | 0 | if (pOutput) |
1065 | 0 | output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN); |
1066 | |
|
1067 | 0 | if (!context) |
1068 | 0 | { |
1069 | 0 | init_context.mech = guessMech(input_buffer, &init_context.spnego, &oid); |
1070 | 0 | if (!init_context.mech && !init_context.spnego) |
1071 | 0 | return SEC_E_INVALID_TOKEN; |
1072 | | |
1073 | 0 | WLog_DBG(TAG, "Mechanism: %s", negotiate_mech_name(&oid)); |
1074 | |
|
1075 | 0 | if (init_context.spnego) |
1076 | 0 | { |
1077 | | /* Process spnego token */ |
1078 | 0 | if (!negotiate_read_neg_token(input_buffer, &input_token)) |
1079 | 0 | return SEC_E_INVALID_TOKEN; |
1080 | | |
1081 | | /* First token must be negoTokenInit and must contain a mechList */ |
1082 | 0 | if (!input_token.init || input_token.mechTypes.cbBuffer == 0) |
1083 | 0 | return SEC_E_INVALID_TOKEN; |
1084 | | |
1085 | 0 | init_context.mechTypes.BufferType = SECBUFFER_DATA; |
1086 | 0 | init_context.mechTypes.cbBuffer = input_token.mechTypes.cbBuffer; |
1087 | | |
1088 | | /* Prepare to read mechList */ |
1089 | 0 | WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_DER, input_token.mechTypes.pvBuffer, |
1090 | 0 | input_token.mechTypes.cbBuffer); |
1091 | |
|
1092 | 0 | if (!WinPrAsn1DecReadSequence(&dec, &dec2)) |
1093 | 0 | return SEC_E_INVALID_TOKEN; |
1094 | 0 | dec = dec2; |
1095 | | |
1096 | | /* If an optimistic token was provided pass it into the first mech */ |
1097 | 0 | if (input_token.mechToken.cbBuffer) |
1098 | 0 | { |
1099 | 0 | if (!WinPrAsn1DecReadOID(&dec, &oid, FALSE)) |
1100 | 0 | return SEC_E_INVALID_TOKEN; |
1101 | | |
1102 | 0 | init_context.mech = negotiate_GetMechByOID(&oid); |
1103 | |
|
1104 | 0 | if (init_context.mech) |
1105 | 0 | { |
1106 | 0 | if (output_buffer) |
1107 | 0 | output_token.mechToken = *output_buffer; |
1108 | 0 | WLog_DBG(TAG, "Requested mechanism: %s", |
1109 | 0 | negotiate_mech_name(init_context.mech->oid)); |
1110 | 0 | } |
1111 | 0 | } |
1112 | 0 | } |
1113 | | |
1114 | 0 | if (init_context.mech) |
1115 | 0 | { |
1116 | 0 | sub_cred = negotiate_FindCredential(creds, init_context.mech); |
1117 | |
|
1118 | 0 | status = init_context.mech->pkg->table->AcceptSecurityContext( |
1119 | 0 | sub_cred, nullptr, init_context.spnego ? &mech_input : pInput, fContextReq, |
1120 | 0 | TargetDataRep, &init_context.sub_context, |
1121 | 0 | init_context.spnego ? &mech_output : pOutput, pfContextAttr, ptsTimeStamp); |
1122 | 0 | } |
1123 | |
|
1124 | 0 | if (IsSecurityStatusError(status)) |
1125 | 0 | { |
1126 | 0 | if (!init_context.spnego) |
1127 | 0 | return status; |
1128 | | |
1129 | 0 | init_context.mic = TRUE; |
1130 | 0 | first_mech = init_context.mech; |
1131 | 0 | init_context.mech = nullptr; |
1132 | 0 | output_token.mechToken.cbBuffer = 0; |
1133 | 0 | } |
1134 | | |
1135 | 0 | while (!init_context.mech && WinPrAsn1DecPeekTag(&dec, &tag)) |
1136 | 0 | { |
1137 | | /* Read each mechanism */ |
1138 | 0 | if (!WinPrAsn1DecReadOID(&dec, &oid, FALSE)) |
1139 | 0 | return SEC_E_INVALID_TOKEN; |
1140 | | |
1141 | 0 | init_context.mech = negotiate_GetMechByOID(&oid); |
1142 | 0 | WLog_DBG(TAG, "Requested mechanism: %s", negotiate_mech_name(&oid)); |
1143 | | |
1144 | | /* Microsoft may send two versions of the kerberos OID */ |
1145 | 0 | if (init_context.mech == first_mech) |
1146 | 0 | init_context.mech = nullptr; |
1147 | |
|
1148 | 0 | if (init_context.mech && !negotiate_FindCredential(creds, init_context.mech)) |
1149 | 0 | init_context.mech = nullptr; |
1150 | 0 | } |
1151 | | |
1152 | 0 | if (!init_context.mech) |
1153 | 0 | return SEC_E_INTERNAL_ERROR; |
1154 | | |
1155 | 0 | context = negotiate_ContextNew(&init_context); |
1156 | 0 | if (!context) |
1157 | 0 | { |
1158 | 0 | if (!IsSecurityStatusError(status)) |
1159 | 0 | init_context.mech->pkg->table->DeleteSecurityContext(&init_context.sub_context); |
1160 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1161 | 0 | } |
1162 | | |
1163 | 0 | sspi_SecureHandleSetUpperPointer(phNewContext, NEGO_SSP_NAME); |
1164 | 0 | sspi_SecureHandleSetLowerPointer(phNewContext, context); |
1165 | |
|
1166 | 0 | if (!init_context.spnego) |
1167 | 0 | return status; |
1168 | | |
1169 | 0 | CopyMemory(init_context.mechTypes.pvBuffer, input_token.mechTypes.pvBuffer, |
1170 | 0 | input_token.mechTypes.cbBuffer); |
1171 | |
|
1172 | 0 | if (!context->mech->preferred) |
1173 | 0 | { |
1174 | 0 | output_token.negState = REQUEST_MIC; |
1175 | 0 | context->mic = TRUE; |
1176 | 0 | } |
1177 | 0 | else |
1178 | 0 | { |
1179 | 0 | output_token.negState = ACCEPT_INCOMPLETE; |
1180 | 0 | } |
1181 | |
|
1182 | 0 | if (status == SEC_E_OK) |
1183 | 0 | context->state = NEGOTIATE_STATE_FINAL; |
1184 | 0 | else |
1185 | 0 | context->state = NEGOTIATE_STATE_NEGORESP; |
1186 | |
|
1187 | 0 | output_token.supportedMech = oid; |
1188 | 0 | WLog_DBG(TAG, "Accepted mechanism: %s", negotiate_mech_name(&output_token.supportedMech)); |
1189 | 0 | } |
1190 | 0 | else |
1191 | 0 | { |
1192 | 0 | sub_cred = negotiate_FindCredential(creds, context->mech); |
1193 | 0 | if (!sub_cred) |
1194 | 0 | return SEC_E_NO_CREDENTIALS; |
1195 | | |
1196 | 0 | if (!context->spnego) |
1197 | 0 | { |
1198 | 0 | return context->mech->pkg->table->AcceptSecurityContext( |
1199 | 0 | sub_cred, &context->sub_context, pInput, fContextReq, TargetDataRep, |
1200 | 0 | &context->sub_context, pOutput, pfContextAttr, ptsTimeStamp); |
1201 | 0 | } |
1202 | | |
1203 | 0 | if (!negotiate_read_neg_token(input_buffer, &input_token)) |
1204 | 0 | return SEC_E_INVALID_TOKEN; |
1205 | | |
1206 | | /* Process the mechanism token */ |
1207 | 0 | if (input_token.mechToken.cbBuffer > 0) |
1208 | 0 | { |
1209 | 0 | if (context->state != NEGOTIATE_STATE_NEGORESP) |
1210 | 0 | return SEC_E_INVALID_TOKEN; |
1211 | | |
1212 | | /* Use the output buffer to store the optimistic token */ |
1213 | 0 | if (output_buffer) |
1214 | 0 | CopyMemory(&output_token.mechToken, output_buffer, sizeof(SecBuffer)); |
1215 | |
|
1216 | 0 | status = context->mech->pkg->table->AcceptSecurityContext( |
1217 | 0 | sub_cred, &context->sub_context, &mech_input, fContextReq | context->mech->flags, |
1218 | 0 | TargetDataRep, &context->sub_context, &mech_output, pfContextAttr, ptsTimeStamp); |
1219 | |
|
1220 | 0 | if (IsSecurityStatusError(status)) |
1221 | 0 | return status; |
1222 | | |
1223 | 0 | if (status == SEC_E_OK) |
1224 | 0 | context->state = NEGOTIATE_STATE_FINAL; |
1225 | 0 | } |
1226 | 0 | else if (context->state == NEGOTIATE_STATE_NEGORESP) |
1227 | 0 | return SEC_E_INVALID_TOKEN; |
1228 | 0 | } |
1229 | | |
1230 | 0 | if (context->state == NEGOTIATE_STATE_FINAL) |
1231 | 0 | { |
1232 | | /* Check if initiator sent the last mech token without a mic and a mic was required */ |
1233 | 0 | if (context->mic && output_token.mechToken.cbBuffer == 0 && input_token.mic.cbBuffer == 0) |
1234 | 0 | return SEC_E_INVALID_TOKEN; |
1235 | | |
1236 | 0 | if (context->mic || input_token.mic.cbBuffer > 0) |
1237 | 0 | { |
1238 | 0 | status = negotiate_mic_exchange(context, &input_token, &output_token, output_buffer); |
1239 | 0 | if (status != SEC_E_OK) |
1240 | 0 | return status; |
1241 | 0 | } |
1242 | 0 | else |
1243 | 0 | output_token.negState = ACCEPT_COMPLETED; |
1244 | 0 | } |
1245 | | |
1246 | 0 | if (input_token.negState == ACCEPT_COMPLETED) |
1247 | 0 | { |
1248 | 0 | if (output_buffer) |
1249 | 0 | output_buffer->cbBuffer = 0; |
1250 | 0 | return SEC_E_OK; |
1251 | 0 | } |
1252 | | |
1253 | 0 | if (output_token.negState == ACCEPT_COMPLETED) |
1254 | 0 | status = SEC_E_OK; |
1255 | 0 | else |
1256 | 0 | status = SEC_I_CONTINUE_NEEDED; |
1257 | |
|
1258 | 0 | if (!negotiate_write_neg_token(output_buffer, &output_token)) |
1259 | 0 | return SEC_E_INTERNAL_ERROR; |
1260 | | |
1261 | 0 | return status; |
1262 | 0 | } |
1263 | | |
1264 | | static SECURITY_STATUS SEC_ENTRY negotiate_CompleteAuthToken(PCtxtHandle phContext, |
1265 | | PSecBufferDesc pToken) |
1266 | 0 | { |
1267 | 0 | NEGOTIATE_CONTEXT* context = nullptr; |
1268 | 0 | SECURITY_STATUS status = SEC_E_OK; |
1269 | 0 | context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1270 | |
|
1271 | 0 | if (!context) |
1272 | 0 | return SEC_E_INVALID_HANDLE; |
1273 | | |
1274 | 0 | WINPR_ASSERT(context->mech); |
1275 | 0 | WINPR_ASSERT(context->mech->pkg); |
1276 | 0 | WINPR_ASSERT(context->mech->pkg->table); |
1277 | 0 | if (context->mech->pkg->table->CompleteAuthToken) |
1278 | 0 | status = context->mech->pkg->table->CompleteAuthToken(&context->sub_context, pToken); |
1279 | |
|
1280 | 0 | return status; |
1281 | 0 | } |
1282 | | |
1283 | | static SECURITY_STATUS SEC_ENTRY negotiate_DeleteSecurityContext(PCtxtHandle phContext) |
1284 | 0 | { |
1285 | 0 | NEGOTIATE_CONTEXT* context = nullptr; |
1286 | 0 | SECURITY_STATUS status = SEC_E_OK; |
1287 | 0 | context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1288 | 0 | const SecPkg* pkg = nullptr; |
1289 | |
|
1290 | 0 | if (!context) |
1291 | 0 | return SEC_E_INVALID_HANDLE; |
1292 | | |
1293 | 0 | WINPR_ASSERT(context->mech); |
1294 | 0 | WINPR_ASSERT(context->mech->pkg); |
1295 | 0 | WINPR_ASSERT(context->mech->pkg->table); |
1296 | 0 | pkg = context->mech->pkg; |
1297 | |
|
1298 | 0 | if (pkg->table->DeleteSecurityContext) |
1299 | 0 | status = pkg->table->DeleteSecurityContext(&context->sub_context); |
1300 | |
|
1301 | 0 | negotiate_ContextFree(context); |
1302 | 0 | return status; |
1303 | 0 | } |
1304 | | |
1305 | | static SECURITY_STATUS SEC_ENTRY |
1306 | | negotiate_ImpersonateSecurityContext(WINPR_ATTR_UNUSED PCtxtHandle phContext) |
1307 | 0 | { |
1308 | 0 | return SEC_E_OK; |
1309 | 0 | } |
1310 | | |
1311 | | static SECURITY_STATUS SEC_ENTRY |
1312 | | negotiate_RevertSecurityContext(WINPR_ATTR_UNUSED PCtxtHandle phContext) |
1313 | 0 | { |
1314 | 0 | return SEC_E_OK; |
1315 | 0 | } |
1316 | | |
1317 | | static SECURITY_STATUS SEC_ENTRY negotiate_QueryContextAttributesW(PCtxtHandle phContext, |
1318 | | ULONG ulAttribute, void* pBuffer) |
1319 | 0 | { |
1320 | 0 | NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1321 | |
|
1322 | 0 | if (!context) |
1323 | 0 | return SEC_E_INVALID_HANDLE; |
1324 | | |
1325 | 0 | WINPR_ASSERT(context->mech); |
1326 | 0 | WINPR_ASSERT(context->mech->pkg); |
1327 | 0 | WINPR_ASSERT(context->mech->pkg->table_w); |
1328 | 0 | if (context->mech->pkg->table_w->QueryContextAttributesW) |
1329 | 0 | return context->mech->pkg->table_w->QueryContextAttributesW(&context->sub_context, |
1330 | 0 | ulAttribute, pBuffer); |
1331 | | |
1332 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1333 | 0 | } |
1334 | | |
1335 | | static SECURITY_STATUS SEC_ENTRY negotiate_QueryContextAttributesA(PCtxtHandle phContext, |
1336 | | ULONG ulAttribute, void* pBuffer) |
1337 | 0 | { |
1338 | 0 | NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1339 | |
|
1340 | 0 | if (!context) |
1341 | 0 | return SEC_E_INVALID_HANDLE; |
1342 | | |
1343 | 0 | WINPR_ASSERT(context->mech); |
1344 | 0 | WINPR_ASSERT(context->mech->pkg); |
1345 | 0 | WINPR_ASSERT(context->mech->pkg->table); |
1346 | 0 | if (context->mech->pkg->table->QueryContextAttributesA) |
1347 | 0 | return context->mech->pkg->table->QueryContextAttributesA(&context->sub_context, |
1348 | 0 | ulAttribute, pBuffer); |
1349 | | |
1350 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1351 | 0 | } |
1352 | | |
1353 | | static SECURITY_STATUS SEC_ENTRY negotiate_SetContextAttributesW(PCtxtHandle phContext, |
1354 | | ULONG ulAttribute, void* pBuffer, |
1355 | | ULONG cbBuffer) |
1356 | 0 | { |
1357 | 0 | NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1358 | |
|
1359 | 0 | if (!context) |
1360 | 0 | return SEC_E_INVALID_HANDLE; |
1361 | | |
1362 | 0 | WINPR_ASSERT(context->mech); |
1363 | 0 | WINPR_ASSERT(context->mech->pkg); |
1364 | 0 | WINPR_ASSERT(context->mech->pkg->table_w); |
1365 | 0 | if (context->mech->pkg->table_w->SetContextAttributesW) |
1366 | 0 | return context->mech->pkg->table_w->SetContextAttributesW(&context->sub_context, |
1367 | 0 | ulAttribute, pBuffer, cbBuffer); |
1368 | | |
1369 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1370 | 0 | } |
1371 | | |
1372 | | static SECURITY_STATUS SEC_ENTRY negotiate_SetContextAttributesA(PCtxtHandle phContext, |
1373 | | ULONG ulAttribute, void* pBuffer, |
1374 | | ULONG cbBuffer) |
1375 | 0 | { |
1376 | 0 | NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1377 | |
|
1378 | 0 | if (!context) |
1379 | 0 | return SEC_E_INVALID_HANDLE; |
1380 | | |
1381 | 0 | WINPR_ASSERT(context->mech); |
1382 | 0 | WINPR_ASSERT(context->mech->pkg); |
1383 | 0 | WINPR_ASSERT(context->mech->pkg->table); |
1384 | 0 | if (context->mech->pkg->table->SetContextAttributesA) |
1385 | 0 | return context->mech->pkg->table->SetContextAttributesA(&context->sub_context, ulAttribute, |
1386 | 0 | pBuffer, cbBuffer); |
1387 | | |
1388 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1389 | 0 | } |
1390 | | |
1391 | | static SECURITY_STATUS SEC_ENTRY negotiate_SetCredentialsAttributesW(PCredHandle phCredential, |
1392 | | ULONG ulAttribute, |
1393 | | void* pBuffer, ULONG cbBuffer) |
1394 | 0 | { |
1395 | 0 | MechCred* creds = nullptr; |
1396 | 0 | BOOL success = FALSE; |
1397 | 0 | SECURITY_STATUS secStatus = 0; |
1398 | |
|
1399 | 0 | creds = sspi_SecureHandleGetLowerPointer(phCredential); |
1400 | |
|
1401 | 0 | if (!creds) |
1402 | 0 | return SEC_E_INVALID_HANDLE; |
1403 | | |
1404 | 0 | for (size_t i = 0; i < MECH_COUNT; i++) |
1405 | 0 | { |
1406 | 0 | MechCred* cred = &creds[i]; |
1407 | |
|
1408 | 0 | WINPR_ASSERT(cred->mech); |
1409 | 0 | WINPR_ASSERT(cred->mech->pkg); |
1410 | 0 | WINPR_ASSERT(cred->mech->pkg->table); |
1411 | 0 | WINPR_ASSERT(cred->mech->pkg->table_w->SetCredentialsAttributesW); |
1412 | 0 | secStatus = cred->mech->pkg->table_w->SetCredentialsAttributesW(&cred->cred, ulAttribute, |
1413 | 0 | pBuffer, cbBuffer); |
1414 | |
|
1415 | 0 | if (secStatus == SEC_E_OK) |
1416 | 0 | { |
1417 | 0 | success = TRUE; |
1418 | 0 | } |
1419 | 0 | } |
1420 | | |
1421 | | // return success if at least one submodule accepts the credential attribute |
1422 | 0 | return (success ? SEC_E_OK : SEC_E_UNSUPPORTED_FUNCTION); |
1423 | 0 | } |
1424 | | |
1425 | | static SECURITY_STATUS SEC_ENTRY negotiate_SetCredentialsAttributesA(PCredHandle phCredential, |
1426 | | ULONG ulAttribute, |
1427 | | void* pBuffer, ULONG cbBuffer) |
1428 | 0 | { |
1429 | 0 | MechCred* creds = nullptr; |
1430 | 0 | BOOL success = FALSE; |
1431 | 0 | SECURITY_STATUS secStatus = 0; |
1432 | |
|
1433 | 0 | creds = sspi_SecureHandleGetLowerPointer(phCredential); |
1434 | |
|
1435 | 0 | if (!creds) |
1436 | 0 | return SEC_E_INVALID_HANDLE; |
1437 | | |
1438 | 0 | for (size_t i = 0; i < MECH_COUNT; i++) |
1439 | 0 | { |
1440 | 0 | MechCred* cred = &creds[i]; |
1441 | |
|
1442 | 0 | if (!cred->valid) |
1443 | 0 | continue; |
1444 | | |
1445 | 0 | WINPR_ASSERT(cred->mech); |
1446 | 0 | WINPR_ASSERT(cred->mech->pkg); |
1447 | 0 | WINPR_ASSERT(cred->mech->pkg->table); |
1448 | 0 | WINPR_ASSERT(cred->mech->pkg->table->SetCredentialsAttributesA); |
1449 | 0 | secStatus = cred->mech->pkg->table->SetCredentialsAttributesA(&cred->cred, ulAttribute, |
1450 | 0 | pBuffer, cbBuffer); |
1451 | |
|
1452 | 0 | if (secStatus == SEC_E_OK) |
1453 | 0 | { |
1454 | 0 | success = TRUE; |
1455 | 0 | } |
1456 | 0 | } |
1457 | | |
1458 | | // return success if at least one submodule accepts the credential attribute |
1459 | 0 | return (success ? SEC_E_OK : SEC_E_UNSUPPORTED_FUNCTION); |
1460 | 0 | } |
1461 | | |
1462 | | static SECURITY_STATUS SEC_ENTRY negotiate_AcquireCredentialsHandleW( |
1463 | | SEC_WCHAR* pszPrincipal, SEC_WCHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID, |
1464 | | void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential, |
1465 | | PTimeStamp ptsExpiry) |
1466 | 0 | { |
1467 | 0 | BOOL kerberos = FALSE; |
1468 | 0 | BOOL ntlm = FALSE; |
1469 | 0 | BOOL u2u = FALSE; |
1470 | |
|
1471 | 0 | if (!negotiate_get_config(pAuthData, &kerberos, &ntlm, &u2u)) |
1472 | 0 | return SEC_E_INTERNAL_ERROR; |
1473 | | |
1474 | 0 | MechCred* creds = calloc(MECH_COUNT, sizeof(MechCred)); |
1475 | |
|
1476 | 0 | if (!creds) |
1477 | 0 | return SEC_E_INTERNAL_ERROR; |
1478 | | |
1479 | 0 | for (size_t i = 0; i < MECH_COUNT; i++) |
1480 | 0 | { |
1481 | 0 | MechCred* cred = &creds[i]; |
1482 | 0 | const SecPkg* pkg = MechTable[i].pkg; |
1483 | 0 | cred->mech = &MechTable[i]; |
1484 | |
|
1485 | 0 | if (!kerberos && sspi_gss_oid_compare(MechTable[i].oid, &kerberos_OID)) |
1486 | 0 | continue; |
1487 | 0 | if (!u2u && sspi_gss_oid_compare(MechTable[i].oid, &kerberos_u2u_OID)) |
1488 | 0 | continue; |
1489 | 0 | if (!ntlm && _tcsncmp(SecPkgTable[i].name, NTLM_SSP_NAME, ARRAYSIZE(NTLM_SSP_NAME)) == 0) |
1490 | 0 | continue; |
1491 | | |
1492 | 0 | WINPR_ASSERT(pkg->table_w); |
1493 | 0 | WINPR_ASSERT(pkg->table_w->AcquireCredentialsHandleW); |
1494 | 0 | if (pkg->table_w->AcquireCredentialsHandleW( |
1495 | 0 | pszPrincipal, pszPackage, fCredentialUse, pvLogonID, pAuthData, pGetKeyFn, |
1496 | 0 | pvGetKeyArgument, &cred->cred, ptsExpiry) != SEC_E_OK) |
1497 | 0 | continue; |
1498 | | |
1499 | 0 | cred->valid = TRUE; |
1500 | 0 | } |
1501 | |
|
1502 | 0 | sspi_SecureHandleSetLowerPointer(phCredential, (void*)creds); |
1503 | 0 | sspi_SecureHandleSetUpperPointer(phCredential, (void*)NEGO_SSP_NAME); |
1504 | 0 | return SEC_E_OK; |
1505 | 0 | } |
1506 | | |
1507 | | static SECURITY_STATUS SEC_ENTRY negotiate_AcquireCredentialsHandleA( |
1508 | | SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID, |
1509 | | void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential, |
1510 | | PTimeStamp ptsExpiry) |
1511 | 0 | { |
1512 | 0 | BOOL kerberos = FALSE; |
1513 | 0 | BOOL ntlm = FALSE; |
1514 | 0 | BOOL u2u = FALSE; |
1515 | |
|
1516 | 0 | if (!negotiate_get_config(pAuthData, &kerberos, &ntlm, &u2u)) |
1517 | 0 | return SEC_E_INTERNAL_ERROR; |
1518 | | |
1519 | 0 | MechCred* creds = calloc(MECH_COUNT, sizeof(MechCred)); |
1520 | |
|
1521 | 0 | if (!creds) |
1522 | 0 | return SEC_E_INTERNAL_ERROR; |
1523 | | |
1524 | 0 | for (size_t i = 0; i < MECH_COUNT; i++) |
1525 | 0 | { |
1526 | 0 | const SecPkg* pkg = MechTable[i].pkg; |
1527 | 0 | MechCred* cred = &creds[i]; |
1528 | |
|
1529 | 0 | cred->mech = &MechTable[i]; |
1530 | |
|
1531 | 0 | if (!kerberos && sspi_gss_oid_compare(MechTable[i].oid, &kerberos_OID)) |
1532 | 0 | continue; |
1533 | 0 | if (!u2u && sspi_gss_oid_compare(MechTable[i].oid, &kerberos_u2u_OID)) |
1534 | 0 | continue; |
1535 | 0 | if (!ntlm && _tcsncmp(SecPkgTable[i].name, NTLM_SSP_NAME, ARRAYSIZE(NTLM_SSP_NAME)) == 0) |
1536 | 0 | continue; |
1537 | | |
1538 | 0 | WINPR_ASSERT(pkg->table); |
1539 | 0 | WINPR_ASSERT(pkg->table->AcquireCredentialsHandleA); |
1540 | 0 | if (pkg->table->AcquireCredentialsHandleA(pszPrincipal, pszPackage, fCredentialUse, |
1541 | 0 | pvLogonID, pAuthData, pGetKeyFn, pvGetKeyArgument, |
1542 | 0 | &cred->cred, ptsExpiry) != SEC_E_OK) |
1543 | 0 | continue; |
1544 | | |
1545 | 0 | cred->valid = TRUE; |
1546 | 0 | } |
1547 | |
|
1548 | 0 | sspi_SecureHandleSetLowerPointer(phCredential, (void*)creds); |
1549 | 0 | sspi_SecureHandleSetUpperPointer(phCredential, (void*)NEGO_SSP_NAME); |
1550 | 0 | return SEC_E_OK; |
1551 | 0 | } |
1552 | | |
1553 | | static SECURITY_STATUS SEC_ENTRY negotiate_QueryCredentialsAttributesW( |
1554 | | WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute, |
1555 | | WINPR_ATTR_UNUSED void* pBuffer) |
1556 | 0 | { |
1557 | 0 | WLog_ERR(TAG, "TODO: Implement"); |
1558 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1559 | 0 | } |
1560 | | |
1561 | | static SECURITY_STATUS SEC_ENTRY negotiate_QueryCredentialsAttributesA( |
1562 | | WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute, |
1563 | | WINPR_ATTR_UNUSED void* pBuffer) |
1564 | 0 | { |
1565 | 0 | WLog_ERR(TAG, "TODO: Implement"); |
1566 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1567 | 0 | } |
1568 | | |
1569 | | static SECURITY_STATUS SEC_ENTRY negotiate_FreeCredentialsHandle(PCredHandle phCredential) |
1570 | 0 | { |
1571 | 0 | MechCred* creds = sspi_SecureHandleGetLowerPointer(phCredential); |
1572 | 0 | if (!creds) |
1573 | 0 | return SEC_E_INVALID_HANDLE; |
1574 | | |
1575 | 0 | for (size_t i = 0; i < MECH_COUNT; i++) |
1576 | 0 | { |
1577 | 0 | MechCred* cred = &creds[i]; |
1578 | |
|
1579 | 0 | WINPR_ASSERT(cred->mech); |
1580 | 0 | WINPR_ASSERT(cred->mech->pkg); |
1581 | 0 | WINPR_ASSERT(cred->mech->pkg->table); |
1582 | 0 | WINPR_ASSERT(cred->mech->pkg->table->FreeCredentialsHandle); |
1583 | 0 | cred->mech->pkg->table->FreeCredentialsHandle(&cred->cred); |
1584 | 0 | } |
1585 | 0 | free(creds); |
1586 | |
|
1587 | 0 | return SEC_E_OK; |
1588 | 0 | } |
1589 | | |
1590 | | static SECURITY_STATUS SEC_ENTRY negotiate_EncryptMessage(PCtxtHandle phContext, ULONG fQOP, |
1591 | | PSecBufferDesc pMessage, |
1592 | | ULONG MessageSeqNo) |
1593 | 0 | { |
1594 | 0 | NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1595 | |
|
1596 | 0 | if (!context) |
1597 | 0 | return SEC_E_INVALID_HANDLE; |
1598 | | |
1599 | 0 | if (context->mic) |
1600 | 0 | MessageSeqNo++; |
1601 | |
|
1602 | 0 | WINPR_ASSERT(context->mech); |
1603 | 0 | WINPR_ASSERT(context->mech->pkg); |
1604 | 0 | WINPR_ASSERT(context->mech->pkg->table); |
1605 | 0 | if (context->mech->pkg->table->EncryptMessage) |
1606 | 0 | return context->mech->pkg->table->EncryptMessage(&context->sub_context, fQOP, pMessage, |
1607 | 0 | MessageSeqNo); |
1608 | | |
1609 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1610 | 0 | } |
1611 | | |
1612 | | static SECURITY_STATUS SEC_ENTRY negotiate_DecryptMessage(PCtxtHandle phContext, |
1613 | | PSecBufferDesc pMessage, |
1614 | | ULONG MessageSeqNo, ULONG* pfQOP) |
1615 | 0 | { |
1616 | 0 | NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1617 | |
|
1618 | 0 | if (!context) |
1619 | 0 | return SEC_E_INVALID_HANDLE; |
1620 | | |
1621 | 0 | if (context->mic) |
1622 | 0 | MessageSeqNo++; |
1623 | |
|
1624 | 0 | WINPR_ASSERT(context->mech); |
1625 | 0 | WINPR_ASSERT(context->mech->pkg); |
1626 | 0 | WINPR_ASSERT(context->mech->pkg->table); |
1627 | 0 | if (context->mech->pkg->table->DecryptMessage) |
1628 | 0 | return context->mech->pkg->table->DecryptMessage(&context->sub_context, pMessage, |
1629 | 0 | MessageSeqNo, pfQOP); |
1630 | | |
1631 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1632 | 0 | } |
1633 | | |
1634 | | static SECURITY_STATUS SEC_ENTRY negotiate_MakeSignature(PCtxtHandle phContext, ULONG fQOP, |
1635 | | PSecBufferDesc pMessage, |
1636 | | ULONG MessageSeqNo) |
1637 | 0 | { |
1638 | 0 | NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1639 | |
|
1640 | 0 | if (!context) |
1641 | 0 | return SEC_E_INVALID_HANDLE; |
1642 | | |
1643 | 0 | if (context->mic) |
1644 | 0 | MessageSeqNo++; |
1645 | |
|
1646 | 0 | WINPR_ASSERT(context->mech); |
1647 | 0 | WINPR_ASSERT(context->mech->pkg); |
1648 | 0 | WINPR_ASSERT(context->mech->pkg->table); |
1649 | 0 | if (context->mech->pkg->table->MakeSignature) |
1650 | 0 | return context->mech->pkg->table->MakeSignature(&context->sub_context, fQOP, pMessage, |
1651 | 0 | MessageSeqNo); |
1652 | | |
1653 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1654 | 0 | } |
1655 | | |
1656 | | static SECURITY_STATUS SEC_ENTRY negotiate_VerifySignature(PCtxtHandle phContext, |
1657 | | PSecBufferDesc pMessage, |
1658 | | ULONG MessageSeqNo, ULONG* pfQOP) |
1659 | 0 | { |
1660 | 0 | NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1661 | |
|
1662 | 0 | if (!context) |
1663 | 0 | return SEC_E_INVALID_HANDLE; |
1664 | | |
1665 | 0 | if (context->mic) |
1666 | 0 | MessageSeqNo++; |
1667 | |
|
1668 | 0 | WINPR_ASSERT(context->mech); |
1669 | 0 | WINPR_ASSERT(context->mech->pkg); |
1670 | 0 | WINPR_ASSERT(context->mech->pkg->table); |
1671 | 0 | if (context->mech->pkg->table->VerifySignature) |
1672 | 0 | return context->mech->pkg->table->VerifySignature(&context->sub_context, pMessage, |
1673 | 0 | MessageSeqNo, pfQOP); |
1674 | | |
1675 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1676 | 0 | } |
1677 | | |
1678 | | const SecurityFunctionTableA NEGOTIATE_SecurityFunctionTableA = { |
1679 | | 3, /* dwVersion */ |
1680 | | nullptr, /* EnumerateSecurityPackages */ |
1681 | | negotiate_QueryCredentialsAttributesA, /* QueryCredentialsAttributes */ |
1682 | | negotiate_AcquireCredentialsHandleA, /* AcquireCredentialsHandle */ |
1683 | | negotiate_FreeCredentialsHandle, /* FreeCredentialsHandle */ |
1684 | | nullptr, /* Reserved2 */ |
1685 | | negotiate_InitializeSecurityContextA, /* InitializeSecurityContext */ |
1686 | | negotiate_AcceptSecurityContext, /* AcceptSecurityContext */ |
1687 | | negotiate_CompleteAuthToken, /* CompleteAuthToken */ |
1688 | | negotiate_DeleteSecurityContext, /* DeleteSecurityContext */ |
1689 | | nullptr, /* ApplyControlToken */ |
1690 | | negotiate_QueryContextAttributesA, /* QueryContextAttributes */ |
1691 | | negotiate_ImpersonateSecurityContext, /* ImpersonateSecurityContext */ |
1692 | | negotiate_RevertSecurityContext, /* RevertSecurityContext */ |
1693 | | negotiate_MakeSignature, /* MakeSignature */ |
1694 | | negotiate_VerifySignature, /* VerifySignature */ |
1695 | | nullptr, /* FreeContextBuffer */ |
1696 | | nullptr, /* QuerySecurityPackageInfo */ |
1697 | | nullptr, /* Reserved3 */ |
1698 | | nullptr, /* Reserved4 */ |
1699 | | nullptr, /* ExportSecurityContext */ |
1700 | | nullptr, /* ImportSecurityContext */ |
1701 | | nullptr, /* AddCredentials */ |
1702 | | nullptr, /* Reserved8 */ |
1703 | | nullptr, /* QuerySecurityContextToken */ |
1704 | | negotiate_EncryptMessage, /* EncryptMessage */ |
1705 | | negotiate_DecryptMessage, /* DecryptMessage */ |
1706 | | negotiate_SetContextAttributesA, /* SetContextAttributes */ |
1707 | | negotiate_SetCredentialsAttributesA, /* SetCredentialsAttributes */ |
1708 | | }; |
1709 | | |
1710 | | const SecurityFunctionTableW NEGOTIATE_SecurityFunctionTableW = { |
1711 | | 3, /* dwVersion */ |
1712 | | nullptr, /* EnumerateSecurityPackages */ |
1713 | | negotiate_QueryCredentialsAttributesW, /* QueryCredentialsAttributes */ |
1714 | | negotiate_AcquireCredentialsHandleW, /* AcquireCredentialsHandle */ |
1715 | | negotiate_FreeCredentialsHandle, /* FreeCredentialsHandle */ |
1716 | | nullptr, /* Reserved2 */ |
1717 | | negotiate_InitializeSecurityContextW, /* InitializeSecurityContext */ |
1718 | | negotiate_AcceptSecurityContext, /* AcceptSecurityContext */ |
1719 | | negotiate_CompleteAuthToken, /* CompleteAuthToken */ |
1720 | | negotiate_DeleteSecurityContext, /* DeleteSecurityContext */ |
1721 | | nullptr, /* ApplyControlToken */ |
1722 | | negotiate_QueryContextAttributesW, /* QueryContextAttributes */ |
1723 | | negotiate_ImpersonateSecurityContext, /* ImpersonateSecurityContext */ |
1724 | | negotiate_RevertSecurityContext, /* RevertSecurityContext */ |
1725 | | negotiate_MakeSignature, /* MakeSignature */ |
1726 | | negotiate_VerifySignature, /* VerifySignature */ |
1727 | | nullptr, /* FreeContextBuffer */ |
1728 | | nullptr, /* QuerySecurityPackageInfo */ |
1729 | | nullptr, /* Reserved3 */ |
1730 | | nullptr, /* Reserved4 */ |
1731 | | nullptr, /* ExportSecurityContext */ |
1732 | | nullptr, /* ImportSecurityContext */ |
1733 | | nullptr, /* AddCredentials */ |
1734 | | nullptr, /* Reserved8 */ |
1735 | | nullptr, /* QuerySecurityContextToken */ |
1736 | | negotiate_EncryptMessage, /* EncryptMessage */ |
1737 | | negotiate_DecryptMessage, /* DecryptMessage */ |
1738 | | negotiate_SetContextAttributesW, /* SetContextAttributes */ |
1739 | | negotiate_SetCredentialsAttributesW, /* SetCredentialsAttributes */ |
1740 | | }; |
1741 | | |
1742 | | BOOL NEGOTIATE_init(void) |
1743 | 0 | { |
1744 | 0 | InitializeConstWCharFromUtf8(NEGOTIATE_SecPkgInfoA.Name, NEGOTIATE_SecPkgInfoW_NameBuffer, |
1745 | 0 | ARRAYSIZE(NEGOTIATE_SecPkgInfoW_NameBuffer)); |
1746 | 0 | InitializeConstWCharFromUtf8(NEGOTIATE_SecPkgInfoA.Comment, NEGOTIATE_SecPkgInfoW_CommentBuffer, |
1747 | 0 | ARRAYSIZE(NEGOTIATE_SecPkgInfoW_CommentBuffer)); |
1748 | |
|
1749 | 0 | return TRUE; |
1750 | 0 | } |