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