1*c2c66affSColin Finck /*
2*c2c66affSColin Finck * Copyright 2005, 2006 Kai Blin
3*c2c66affSColin Finck *
4*c2c66affSColin Finck * This library is free software; you can redistribute it and/or
5*c2c66affSColin Finck * modify it under the terms of the GNU Lesser General Public
6*c2c66affSColin Finck * License as published by the Free Software Foundation; either
7*c2c66affSColin Finck * version 2.1 of the License, or (at your option) any later version.
8*c2c66affSColin Finck *
9*c2c66affSColin Finck * This library is distributed in the hope that it will be useful,
10*c2c66affSColin Finck * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*c2c66affSColin Finck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12*c2c66affSColin Finck * Lesser General Public License for more details.
13*c2c66affSColin Finck *
14*c2c66affSColin Finck * You should have received a copy of the GNU Lesser General Public
15*c2c66affSColin Finck * License along with this library; if not, write to the Free Software
16*c2c66affSColin Finck * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17*c2c66affSColin Finck *
18*c2c66affSColin Finck * This file implements the NTLM security provider.
19*c2c66affSColin Finck */
20*c2c66affSColin Finck
21*c2c66affSColin Finck #include "precomp.h"
22*c2c66affSColin Finck
23*c2c66affSColin Finck #include <assert.h>
24*c2c66affSColin Finck #include <stdio.h>
25*c2c66affSColin Finck #include <wincred.h>
26*c2c66affSColin Finck #include <lm.h>
27*c2c66affSColin Finck
28*c2c66affSColin Finck #include "hmac_md5.h"
29*c2c66affSColin Finck
30*c2c66affSColin Finck #include <wine/unicode.h>
31*c2c66affSColin Finck #include <wine/debug.h>
32*c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(ntlm);
33*c2c66affSColin Finck WINE_DECLARE_DEBUG_CHANNEL(winediag);
34*c2c66affSColin Finck
35*c2c66affSColin Finck #define NTLM_MAX_BUF 1904
36*c2c66affSColin Finck #define MIN_NTLM_AUTH_MAJOR_VERSION 3
37*c2c66affSColin Finck #define MIN_NTLM_AUTH_MINOR_VERSION 0
38*c2c66affSColin Finck #ifndef __REACTOS__
39*c2c66affSColin Finck #define MIN_NTLM_AUTH_MICRO_VERSION 25
40*c2c66affSColin Finck #else
41*c2c66affSColin Finck #define MIN_NTLM_AUTH_MICRO_VERSION 23
42*c2c66affSColin Finck #endif
43*c2c66affSColin Finck
44*c2c66affSColin Finck static CHAR ntlm_auth[] = "ntlm_auth";
45*c2c66affSColin Finck
46*c2c66affSColin Finck /***********************************************************************
47*c2c66affSColin Finck * QueryCredentialsAttributesA
48*c2c66affSColin Finck */
ntlm_QueryCredentialsAttributesA(PCredHandle phCredential,ULONG ulAttribute,PVOID pBuffer)49*c2c66affSColin Finck static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
50*c2c66affSColin Finck PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
51*c2c66affSColin Finck {
52*c2c66affSColin Finck SECURITY_STATUS ret;
53*c2c66affSColin Finck
54*c2c66affSColin Finck TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
55*c2c66affSColin Finck
56*c2c66affSColin Finck if(ulAttribute == SECPKG_ATTR_NAMES)
57*c2c66affSColin Finck {
58*c2c66affSColin Finck FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
59*c2c66affSColin Finck ret = SEC_E_UNSUPPORTED_FUNCTION;
60*c2c66affSColin Finck }
61*c2c66affSColin Finck else
62*c2c66affSColin Finck ret = SEC_E_UNSUPPORTED_FUNCTION;
63*c2c66affSColin Finck
64*c2c66affSColin Finck return ret;
65*c2c66affSColin Finck }
66*c2c66affSColin Finck
67*c2c66affSColin Finck /***********************************************************************
68*c2c66affSColin Finck * QueryCredentialsAttributesW
69*c2c66affSColin Finck */
ntlm_QueryCredentialsAttributesW(PCredHandle phCredential,ULONG ulAttribute,PVOID pBuffer)70*c2c66affSColin Finck static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
71*c2c66affSColin Finck PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
72*c2c66affSColin Finck {
73*c2c66affSColin Finck SECURITY_STATUS ret;
74*c2c66affSColin Finck
75*c2c66affSColin Finck TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
76*c2c66affSColin Finck
77*c2c66affSColin Finck if(ulAttribute == SECPKG_ATTR_NAMES)
78*c2c66affSColin Finck {
79*c2c66affSColin Finck FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
80*c2c66affSColin Finck ret = SEC_E_UNSUPPORTED_FUNCTION;
81*c2c66affSColin Finck }
82*c2c66affSColin Finck else
83*c2c66affSColin Finck ret = SEC_E_UNSUPPORTED_FUNCTION;
84*c2c66affSColin Finck
85*c2c66affSColin Finck return ret;
86*c2c66affSColin Finck }
87*c2c66affSColin Finck
ntlm_GetUsernameArg(LPCWSTR userW,INT userW_length)88*c2c66affSColin Finck static char *ntlm_GetUsernameArg(LPCWSTR userW, INT userW_length)
89*c2c66affSColin Finck {
90*c2c66affSColin Finck static const char username_arg[] = "--username=";
91*c2c66affSColin Finck char *user;
92*c2c66affSColin Finck int unixcp_size;
93*c2c66affSColin Finck
94*c2c66affSColin Finck unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
95*c2c66affSColin Finck userW, userW_length, NULL, 0, NULL, NULL) + sizeof(username_arg);
96*c2c66affSColin Finck user = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
97*c2c66affSColin Finck if (!user) return NULL;
98*c2c66affSColin Finck memcpy(user, username_arg, sizeof(username_arg) - 1);
99*c2c66affSColin Finck WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, userW, userW_length,
100*c2c66affSColin Finck user + sizeof(username_arg) - 1,
101*c2c66affSColin Finck unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
102*c2c66affSColin Finck user[unixcp_size - 1] = '\0';
103*c2c66affSColin Finck return user;
104*c2c66affSColin Finck }
105*c2c66affSColin Finck
ntlm_GetDomainArg(LPCWSTR domainW,INT domainW_length)106*c2c66affSColin Finck static char *ntlm_GetDomainArg(LPCWSTR domainW, INT domainW_length)
107*c2c66affSColin Finck {
108*c2c66affSColin Finck static const char domain_arg[] = "--domain=";
109*c2c66affSColin Finck char *domain;
110*c2c66affSColin Finck int unixcp_size;
111*c2c66affSColin Finck
112*c2c66affSColin Finck unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
113*c2c66affSColin Finck domainW, domainW_length, NULL, 0, NULL, NULL) + sizeof(domain_arg);
114*c2c66affSColin Finck domain = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
115*c2c66affSColin Finck if (!domain) return NULL;
116*c2c66affSColin Finck memcpy(domain, domain_arg, sizeof(domain_arg) - 1);
117*c2c66affSColin Finck WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domainW,
118*c2c66affSColin Finck domainW_length, domain + sizeof(domain_arg) - 1,
119*c2c66affSColin Finck unixcp_size - sizeof(domain) + 1, NULL, NULL);
120*c2c66affSColin Finck domain[unixcp_size - 1] = '\0';
121*c2c66affSColin Finck return domain;
122*c2c66affSColin Finck }
123*c2c66affSColin Finck
124*c2c66affSColin Finck /***********************************************************************
125*c2c66affSColin Finck * AcquireCredentialsHandleW
126*c2c66affSColin Finck */
ntlm_AcquireCredentialsHandleW(SEC_WCHAR * pszPrincipal,SEC_WCHAR * pszPackage,ULONG fCredentialUse,PLUID pLogonID,PVOID pAuthData,SEC_GET_KEY_FN pGetKeyFn,PVOID pGetKeyArgument,PCredHandle phCredential,PTimeStamp ptsExpiry)127*c2c66affSColin Finck SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
128*c2c66affSColin Finck SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
129*c2c66affSColin Finck PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
130*c2c66affSColin Finck PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
131*c2c66affSColin Finck {
132*c2c66affSColin Finck SECURITY_STATUS ret;
133*c2c66affSColin Finck PNtlmCredentials ntlm_cred;
134*c2c66affSColin Finck LPWSTR domain = NULL, user = NULL, password = NULL;
135*c2c66affSColin Finck PSEC_WINNT_AUTH_IDENTITY_W auth_data = NULL;
136*c2c66affSColin Finck
137*c2c66affSColin Finck TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
138*c2c66affSColin Finck debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
139*c2c66affSColin Finck pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
140*c2c66affSColin Finck
141*c2c66affSColin Finck switch(fCredentialUse)
142*c2c66affSColin Finck {
143*c2c66affSColin Finck case SECPKG_CRED_INBOUND:
144*c2c66affSColin Finck ntlm_cred = HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred));
145*c2c66affSColin Finck if (!ntlm_cred)
146*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
147*c2c66affSColin Finck else
148*c2c66affSColin Finck {
149*c2c66affSColin Finck ntlm_cred->mode = NTLM_SERVER;
150*c2c66affSColin Finck ntlm_cred->username_arg = NULL;
151*c2c66affSColin Finck ntlm_cred->domain_arg = NULL;
152*c2c66affSColin Finck ntlm_cred->password = NULL;
153*c2c66affSColin Finck ntlm_cred->pwlen = 0;
154*c2c66affSColin Finck ntlm_cred->no_cached_credentials = 0;
155*c2c66affSColin Finck
156*c2c66affSColin Finck phCredential->dwUpper = fCredentialUse;
157*c2c66affSColin Finck phCredential->dwLower = (ULONG_PTR)ntlm_cred;
158*c2c66affSColin Finck ret = SEC_E_OK;
159*c2c66affSColin Finck }
160*c2c66affSColin Finck break;
161*c2c66affSColin Finck case SECPKG_CRED_OUTBOUND:
162*c2c66affSColin Finck {
163*c2c66affSColin Finck auth_data = pAuthData;
164*c2c66affSColin Finck ntlm_cred = HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred));
165*c2c66affSColin Finck if (!ntlm_cred)
166*c2c66affSColin Finck {
167*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
168*c2c66affSColin Finck break;
169*c2c66affSColin Finck }
170*c2c66affSColin Finck ntlm_cred->mode = NTLM_CLIENT;
171*c2c66affSColin Finck ntlm_cred->username_arg = NULL;
172*c2c66affSColin Finck ntlm_cred->domain_arg = NULL;
173*c2c66affSColin Finck ntlm_cred->password = NULL;
174*c2c66affSColin Finck ntlm_cred->pwlen = 0;
175*c2c66affSColin Finck ntlm_cred->no_cached_credentials = 0;
176*c2c66affSColin Finck
177*c2c66affSColin Finck if(pAuthData != NULL)
178*c2c66affSColin Finck {
179*c2c66affSColin Finck int domain_len = 0, user_len = 0, password_len = 0;
180*c2c66affSColin Finck
181*c2c66affSColin Finck if (auth_data->Flags & SEC_WINNT_AUTH_IDENTITY_ANSI)
182*c2c66affSColin Finck {
183*c2c66affSColin Finck if (auth_data->DomainLength)
184*c2c66affSColin Finck {
185*c2c66affSColin Finck domain_len = MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->Domain,
186*c2c66affSColin Finck auth_data->DomainLength, NULL, 0);
187*c2c66affSColin Finck domain = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * domain_len);
188*c2c66affSColin Finck if(!domain)
189*c2c66affSColin Finck {
190*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
191*c2c66affSColin Finck break;
192*c2c66affSColin Finck }
193*c2c66affSColin Finck MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->Domain, auth_data->DomainLength,
194*c2c66affSColin Finck domain, domain_len);
195*c2c66affSColin Finck }
196*c2c66affSColin Finck
197*c2c66affSColin Finck if (auth_data->UserLength)
198*c2c66affSColin Finck {
199*c2c66affSColin Finck user_len = MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->User,
200*c2c66affSColin Finck auth_data->UserLength, NULL, 0);
201*c2c66affSColin Finck user = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * user_len);
202*c2c66affSColin Finck if(!user)
203*c2c66affSColin Finck {
204*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
205*c2c66affSColin Finck break;
206*c2c66affSColin Finck }
207*c2c66affSColin Finck MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->User, auth_data->UserLength,
208*c2c66affSColin Finck user, user_len);
209*c2c66affSColin Finck }
210*c2c66affSColin Finck
211*c2c66affSColin Finck if (auth_data->PasswordLength)
212*c2c66affSColin Finck {
213*c2c66affSColin Finck password_len = MultiByteToWideChar(CP_ACP, 0,(char *)auth_data->Password,
214*c2c66affSColin Finck auth_data->PasswordLength, NULL, 0);
215*c2c66affSColin Finck password = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * password_len);
216*c2c66affSColin Finck if(!password)
217*c2c66affSColin Finck {
218*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
219*c2c66affSColin Finck break;
220*c2c66affSColin Finck }
221*c2c66affSColin Finck MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->Password, auth_data->PasswordLength,
222*c2c66affSColin Finck password, password_len);
223*c2c66affSColin Finck }
224*c2c66affSColin Finck }
225*c2c66affSColin Finck else
226*c2c66affSColin Finck {
227*c2c66affSColin Finck domain = auth_data->Domain;
228*c2c66affSColin Finck domain_len = auth_data->DomainLength;
229*c2c66affSColin Finck
230*c2c66affSColin Finck user = auth_data->User;
231*c2c66affSColin Finck user_len = auth_data->UserLength;
232*c2c66affSColin Finck
233*c2c66affSColin Finck password = auth_data->Password;
234*c2c66affSColin Finck password_len = auth_data->PasswordLength;
235*c2c66affSColin Finck }
236*c2c66affSColin Finck
237*c2c66affSColin Finck TRACE("Username is %s\n", debugstr_wn(user, user_len));
238*c2c66affSColin Finck TRACE("Domain name is %s\n", debugstr_wn(domain, domain_len));
239*c2c66affSColin Finck
240*c2c66affSColin Finck ntlm_cred->username_arg = ntlm_GetUsernameArg(user, user_len);
241*c2c66affSColin Finck ntlm_cred->domain_arg = ntlm_GetDomainArg(domain, domain_len);
242*c2c66affSColin Finck
243*c2c66affSColin Finck if(password_len != 0)
244*c2c66affSColin Finck {
245*c2c66affSColin Finck ntlm_cred->pwlen = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, password,
246*c2c66affSColin Finck password_len, NULL, 0, NULL, NULL);
247*c2c66affSColin Finck
248*c2c66affSColin Finck ntlm_cred->password = HeapAlloc(GetProcessHeap(), 0,
249*c2c66affSColin Finck ntlm_cred->pwlen);
250*c2c66affSColin Finck if(!ntlm_cred->password)
251*c2c66affSColin Finck {
252*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
253*c2c66affSColin Finck break;
254*c2c66affSColin Finck }
255*c2c66affSColin Finck
256*c2c66affSColin Finck WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, password, password_len,
257*c2c66affSColin Finck ntlm_cred->password, ntlm_cred->pwlen, NULL, NULL);
258*c2c66affSColin Finck }
259*c2c66affSColin Finck }
260*c2c66affSColin Finck
261*c2c66affSColin Finck phCredential->dwUpper = fCredentialUse;
262*c2c66affSColin Finck phCredential->dwLower = (ULONG_PTR)ntlm_cred;
263*c2c66affSColin Finck TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
264*c2c66affSColin Finck phCredential->dwUpper, phCredential->dwLower);
265*c2c66affSColin Finck ret = SEC_E_OK;
266*c2c66affSColin Finck break;
267*c2c66affSColin Finck }
268*c2c66affSColin Finck case SECPKG_CRED_BOTH:
269*c2c66affSColin Finck FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
270*c2c66affSColin Finck ret = SEC_E_UNSUPPORTED_FUNCTION;
271*c2c66affSColin Finck phCredential = NULL;
272*c2c66affSColin Finck break;
273*c2c66affSColin Finck default:
274*c2c66affSColin Finck phCredential = NULL;
275*c2c66affSColin Finck ret = SEC_E_UNKNOWN_CREDENTIALS;
276*c2c66affSColin Finck }
277*c2c66affSColin Finck
278*c2c66affSColin Finck if (auth_data && auth_data->Flags & SEC_WINNT_AUTH_IDENTITY_ANSI)
279*c2c66affSColin Finck {
280*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, domain);
281*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, user);
282*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, password);
283*c2c66affSColin Finck }
284*c2c66affSColin Finck return ret;
285*c2c66affSColin Finck }
286*c2c66affSColin Finck
287*c2c66affSColin Finck /***********************************************************************
288*c2c66affSColin Finck * AcquireCredentialsHandleA
289*c2c66affSColin Finck */
ntlm_AcquireCredentialsHandleA(SEC_CHAR * pszPrincipal,SEC_CHAR * pszPackage,ULONG fCredentialUse,PLUID pLogonID,PVOID pAuthData,SEC_GET_KEY_FN pGetKeyFn,PVOID pGetKeyArgument,PCredHandle phCredential,PTimeStamp ptsExpiry)290*c2c66affSColin Finck static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
291*c2c66affSColin Finck SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
292*c2c66affSColin Finck PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
293*c2c66affSColin Finck PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
294*c2c66affSColin Finck {
295*c2c66affSColin Finck SECURITY_STATUS ret;
296*c2c66affSColin Finck int user_sizeW, domain_sizeW, passwd_sizeW;
297*c2c66affSColin Finck
298*c2c66affSColin Finck SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
299*c2c66affSColin Finck
300*c2c66affSColin Finck PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
301*c2c66affSColin Finck PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
302*c2c66affSColin Finck
303*c2c66affSColin Finck TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
304*c2c66affSColin Finck debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
305*c2c66affSColin Finck pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
306*c2c66affSColin Finck
307*c2c66affSColin Finck if(pszPackage != NULL)
308*c2c66affSColin Finck {
309*c2c66affSColin Finck int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
310*c2c66affSColin Finck NULL, 0);
311*c2c66affSColin Finck
312*c2c66affSColin Finck package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
313*c2c66affSColin Finck sizeof(SEC_WCHAR));
314*c2c66affSColin Finck MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
315*c2c66affSColin Finck }
316*c2c66affSColin Finck
317*c2c66affSColin Finck
318*c2c66affSColin Finck if(pAuthData != NULL)
319*c2c66affSColin Finck {
320*c2c66affSColin Finck identity = pAuthData;
321*c2c66affSColin Finck
322*c2c66affSColin Finck if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
323*c2c66affSColin Finck {
324*c2c66affSColin Finck pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
325*c2c66affSColin Finck sizeof(SEC_WINNT_AUTH_IDENTITY_W));
326*c2c66affSColin Finck
327*c2c66affSColin Finck if(identity->UserLength != 0)
328*c2c66affSColin Finck {
329*c2c66affSColin Finck user_sizeW = MultiByteToWideChar(CP_ACP, 0,
330*c2c66affSColin Finck (LPCSTR)identity->User, identity->UserLength, NULL, 0);
331*c2c66affSColin Finck user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
332*c2c66affSColin Finck sizeof(SEC_WCHAR));
333*c2c66affSColin Finck MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
334*c2c66affSColin Finck identity->UserLength, user, user_sizeW);
335*c2c66affSColin Finck }
336*c2c66affSColin Finck else
337*c2c66affSColin Finck {
338*c2c66affSColin Finck user_sizeW = 0;
339*c2c66affSColin Finck }
340*c2c66affSColin Finck
341*c2c66affSColin Finck if(identity->DomainLength != 0)
342*c2c66affSColin Finck {
343*c2c66affSColin Finck domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
344*c2c66affSColin Finck (LPCSTR)identity->Domain, identity->DomainLength, NULL, 0);
345*c2c66affSColin Finck domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
346*c2c66affSColin Finck * sizeof(SEC_WCHAR));
347*c2c66affSColin Finck MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
348*c2c66affSColin Finck identity->DomainLength, domain, domain_sizeW);
349*c2c66affSColin Finck }
350*c2c66affSColin Finck else
351*c2c66affSColin Finck {
352*c2c66affSColin Finck domain_sizeW = 0;
353*c2c66affSColin Finck }
354*c2c66affSColin Finck
355*c2c66affSColin Finck if(identity->PasswordLength != 0)
356*c2c66affSColin Finck {
357*c2c66affSColin Finck passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
358*c2c66affSColin Finck (LPCSTR)identity->Password, identity->PasswordLength,
359*c2c66affSColin Finck NULL, 0);
360*c2c66affSColin Finck passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
361*c2c66affSColin Finck * sizeof(SEC_WCHAR));
362*c2c66affSColin Finck MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
363*c2c66affSColin Finck identity->PasswordLength, passwd, passwd_sizeW);
364*c2c66affSColin Finck }
365*c2c66affSColin Finck else
366*c2c66affSColin Finck {
367*c2c66affSColin Finck passwd_sizeW = 0;
368*c2c66affSColin Finck }
369*c2c66affSColin Finck
370*c2c66affSColin Finck pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
371*c2c66affSColin Finck pAuthDataW->User = user;
372*c2c66affSColin Finck pAuthDataW->UserLength = user_sizeW;
373*c2c66affSColin Finck pAuthDataW->Domain = domain;
374*c2c66affSColin Finck pAuthDataW->DomainLength = domain_sizeW;
375*c2c66affSColin Finck pAuthDataW->Password = passwd;
376*c2c66affSColin Finck pAuthDataW->PasswordLength = passwd_sizeW;
377*c2c66affSColin Finck }
378*c2c66affSColin Finck else
379*c2c66affSColin Finck {
380*c2c66affSColin Finck pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
381*c2c66affSColin Finck }
382*c2c66affSColin Finck }
383*c2c66affSColin Finck
384*c2c66affSColin Finck ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
385*c2c66affSColin Finck pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
386*c2c66affSColin Finck ptsExpiry);
387*c2c66affSColin Finck
388*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, package);
389*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, user);
390*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, domain);
391*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, passwd);
392*c2c66affSColin Finck if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
393*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, pAuthDataW);
394*c2c66affSColin Finck
395*c2c66affSColin Finck return ret;
396*c2c66affSColin Finck }
397*c2c66affSColin Finck
398*c2c66affSColin Finck /*************************************************************************
399*c2c66affSColin Finck * ntlm_GetTokenBufferIndex
400*c2c66affSColin Finck * Calculates the index of the secbuffer with BufferType == SECBUFFER_TOKEN
401*c2c66affSColin Finck * Returns index if found or -1 if not found.
402*c2c66affSColin Finck */
ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)403*c2c66affSColin Finck static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)
404*c2c66affSColin Finck {
405*c2c66affSColin Finck UINT i;
406*c2c66affSColin Finck
407*c2c66affSColin Finck TRACE("%p\n", pMessage);
408*c2c66affSColin Finck
409*c2c66affSColin Finck for( i = 0; i < pMessage->cBuffers; ++i )
410*c2c66affSColin Finck {
411*c2c66affSColin Finck if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
412*c2c66affSColin Finck return i;
413*c2c66affSColin Finck }
414*c2c66affSColin Finck
415*c2c66affSColin Finck return -1;
416*c2c66affSColin Finck }
417*c2c66affSColin Finck
418*c2c66affSColin Finck /*************************************************************************
419*c2c66affSColin Finck * ntlm_GetDataBufferIndex
420*c2c66affSColin Finck * Calculates the index of the first secbuffer with BufferType == SECBUFFER_DATA
421*c2c66affSColin Finck * Returns index if found or -1 if not found.
422*c2c66affSColin Finck */
ntlm_GetDataBufferIndex(PSecBufferDesc pMessage)423*c2c66affSColin Finck static int ntlm_GetDataBufferIndex(PSecBufferDesc pMessage)
424*c2c66affSColin Finck {
425*c2c66affSColin Finck UINT i;
426*c2c66affSColin Finck
427*c2c66affSColin Finck TRACE("%p\n", pMessage);
428*c2c66affSColin Finck
429*c2c66affSColin Finck for( i = 0; i < pMessage->cBuffers; ++i )
430*c2c66affSColin Finck {
431*c2c66affSColin Finck if(pMessage->pBuffers[i].BufferType == SECBUFFER_DATA)
432*c2c66affSColin Finck return i;
433*c2c66affSColin Finck }
434*c2c66affSColin Finck
435*c2c66affSColin Finck return -1;
436*c2c66affSColin Finck }
437*c2c66affSColin Finck
ntlm_GetCachedCredential(const SEC_WCHAR * pszTargetName,PCREDENTIALW * cred)438*c2c66affSColin Finck static BOOL ntlm_GetCachedCredential(const SEC_WCHAR *pszTargetName, PCREDENTIALW *cred)
439*c2c66affSColin Finck {
440*c2c66affSColin Finck LPCWSTR p;
441*c2c66affSColin Finck LPCWSTR pszHost;
442*c2c66affSColin Finck LPWSTR pszHostOnly;
443*c2c66affSColin Finck BOOL ret;
444*c2c66affSColin Finck
445*c2c66affSColin Finck if (!pszTargetName)
446*c2c66affSColin Finck return FALSE;
447*c2c66affSColin Finck
448*c2c66affSColin Finck /* try to get the start of the hostname from service principal name (SPN) */
449*c2c66affSColin Finck pszHost = strchrW(pszTargetName, '/');
450*c2c66affSColin Finck if (pszHost)
451*c2c66affSColin Finck {
452*c2c66affSColin Finck /* skip slash character */
453*c2c66affSColin Finck pszHost++;
454*c2c66affSColin Finck
455*c2c66affSColin Finck /* find end of host by detecting start of instance port or start of referrer */
456*c2c66affSColin Finck p = strchrW(pszHost, ':');
457*c2c66affSColin Finck if (!p)
458*c2c66affSColin Finck p = strchrW(pszHost, '/');
459*c2c66affSColin Finck if (!p)
460*c2c66affSColin Finck p = pszHost + strlenW(pszHost);
461*c2c66affSColin Finck }
462*c2c66affSColin Finck else /* otherwise not an SPN, just a host */
463*c2c66affSColin Finck {
464*c2c66affSColin Finck pszHost = pszTargetName;
465*c2c66affSColin Finck p = pszHost + strlenW(pszHost);
466*c2c66affSColin Finck }
467*c2c66affSColin Finck
468*c2c66affSColin Finck pszHostOnly = HeapAlloc(GetProcessHeap(), 0, (p - pszHost + 1) * sizeof(WCHAR));
469*c2c66affSColin Finck if (!pszHostOnly)
470*c2c66affSColin Finck return FALSE;
471*c2c66affSColin Finck
472*c2c66affSColin Finck memcpy(pszHostOnly, pszHost, (p - pszHost) * sizeof(WCHAR));
473*c2c66affSColin Finck pszHostOnly[p - pszHost] = '\0';
474*c2c66affSColin Finck
475*c2c66affSColin Finck ret = CredReadW(pszHostOnly, CRED_TYPE_DOMAIN_PASSWORD, 0, cred);
476*c2c66affSColin Finck
477*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, pszHostOnly);
478*c2c66affSColin Finck return ret;
479*c2c66affSColin Finck }
480*c2c66affSColin Finck
481*c2c66affSColin Finck /***********************************************************************
482*c2c66affSColin Finck * InitializeSecurityContextW
483*c2c66affSColin Finck */
ntlm_InitializeSecurityContextW(PCredHandle phCredential,PCtxtHandle phContext,SEC_WCHAR * pszTargetName,ULONG fContextReq,ULONG Reserved1,ULONG TargetDataRep,PSecBufferDesc pInput,ULONG Reserved2,PCtxtHandle phNewContext,PSecBufferDesc pOutput,ULONG * pfContextAttr,PTimeStamp ptsExpiry)484*c2c66affSColin Finck SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
485*c2c66affSColin Finck PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
486*c2c66affSColin Finck ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
487*c2c66affSColin Finck PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
488*c2c66affSColin Finck PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
489*c2c66affSColin Finck {
490*c2c66affSColin Finck SECURITY_STATUS ret;
491*c2c66affSColin Finck PNtlmCredentials ntlm_cred;
492*c2c66affSColin Finck PNegoHelper helper = NULL;
493*c2c66affSColin Finck ULONG ctxt_attr = 0;
494*c2c66affSColin Finck char* buffer, *want_flags = NULL;
495*c2c66affSColin Finck PBYTE bin;
496*c2c66affSColin Finck int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
497*c2c66affSColin Finck int token_idx;
498*c2c66affSColin Finck SEC_CHAR *username = NULL;
499*c2c66affSColin Finck SEC_CHAR *domain = NULL;
500*c2c66affSColin Finck SEC_CHAR *password = NULL;
501*c2c66affSColin Finck
502*c2c66affSColin Finck TRACE("%p %p %s 0x%08x %d %d %p %d %p %p %p %p\n", phCredential, phContext,
503*c2c66affSColin Finck debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
504*c2c66affSColin Finck Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
505*c2c66affSColin Finck
506*c2c66affSColin Finck /****************************************
507*c2c66affSColin Finck * When communicating with the client, there can be the
508*c2c66affSColin Finck * following reply packets:
509*c2c66affSColin Finck * YR <base64 blob> should be sent to the server
510*c2c66affSColin Finck * PW should be sent back to helper with
511*c2c66affSColin Finck * base64 encoded password
512*c2c66affSColin Finck * AF <base64 blob> client is done, blob should be
513*c2c66affSColin Finck * sent to server with KK prefixed
514*c2c66affSColin Finck * GF <string list> A string list of negotiated flags
515*c2c66affSColin Finck * GK <base64 blob> base64 encoded session key
516*c2c66affSColin Finck * BH <char reason> something broke
517*c2c66affSColin Finck */
518*c2c66affSColin Finck /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
519*c2c66affSColin Finck
520*c2c66affSColin Finck if(TargetDataRep == SECURITY_NETWORK_DREP){
521*c2c66affSColin Finck TRACE("Setting SECURITY_NETWORK_DREP\n");
522*c2c66affSColin Finck }
523*c2c66affSColin Finck
524*c2c66affSColin Finck buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
525*c2c66affSColin Finck bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
526*c2c66affSColin Finck
527*c2c66affSColin Finck if((phContext == NULL) && (pInput == NULL))
528*c2c66affSColin Finck {
529*c2c66affSColin Finck static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
530*c2c66affSColin Finck static CHAR credentials_argv[] = "--use-cached-creds";
531*c2c66affSColin Finck SEC_CHAR *client_argv[5];
532*c2c66affSColin Finck int pwlen = 0;
533*c2c66affSColin Finck
534*c2c66affSColin Finck TRACE("First time in ISC()\n");
535*c2c66affSColin Finck
536*c2c66affSColin Finck if(!phCredential)
537*c2c66affSColin Finck {
538*c2c66affSColin Finck ret = SEC_E_INVALID_HANDLE;
539*c2c66affSColin Finck goto isc_end;
540*c2c66affSColin Finck }
541*c2c66affSColin Finck
542*c2c66affSColin Finck /* As the server side of sspi never calls this, make sure that
543*c2c66affSColin Finck * the handler is a client handler.
544*c2c66affSColin Finck */
545*c2c66affSColin Finck ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
546*c2c66affSColin Finck if(ntlm_cred->mode != NTLM_CLIENT)
547*c2c66affSColin Finck {
548*c2c66affSColin Finck TRACE("Cred mode = %d\n", ntlm_cred->mode);
549*c2c66affSColin Finck ret = SEC_E_INVALID_HANDLE;
550*c2c66affSColin Finck goto isc_end;
551*c2c66affSColin Finck }
552*c2c66affSColin Finck
553*c2c66affSColin Finck client_argv[0] = ntlm_auth;
554*c2c66affSColin Finck client_argv[1] = helper_protocol;
555*c2c66affSColin Finck if (!ntlm_cred->username_arg && !ntlm_cred->domain_arg)
556*c2c66affSColin Finck {
557*c2c66affSColin Finck LPWKSTA_USER_INFO_1 ui = NULL;
558*c2c66affSColin Finck NET_API_STATUS status;
559*c2c66affSColin Finck PCREDENTIALW cred;
560*c2c66affSColin Finck
561*c2c66affSColin Finck if (ntlm_GetCachedCredential(pszTargetName, &cred))
562*c2c66affSColin Finck {
563*c2c66affSColin Finck LPWSTR p;
564*c2c66affSColin Finck p = strchrW(cred->UserName, '\\');
565*c2c66affSColin Finck if (p)
566*c2c66affSColin Finck {
567*c2c66affSColin Finck domain = ntlm_GetDomainArg(cred->UserName, p - cred->UserName);
568*c2c66affSColin Finck p++;
569*c2c66affSColin Finck }
570*c2c66affSColin Finck else
571*c2c66affSColin Finck {
572*c2c66affSColin Finck domain = ntlm_GetDomainArg(NULL, 0);
573*c2c66affSColin Finck p = cred->UserName;
574*c2c66affSColin Finck }
575*c2c66affSColin Finck
576*c2c66affSColin Finck username = ntlm_GetUsernameArg(p, -1);
577*c2c66affSColin Finck
578*c2c66affSColin Finck if(cred->CredentialBlobSize != 0)
579*c2c66affSColin Finck {
580*c2c66affSColin Finck pwlen = WideCharToMultiByte(CP_UNIXCP,
581*c2c66affSColin Finck WC_NO_BEST_FIT_CHARS, (LPWSTR)cred->CredentialBlob,
582*c2c66affSColin Finck cred->CredentialBlobSize / sizeof(WCHAR), NULL, 0,
583*c2c66affSColin Finck NULL, NULL);
584*c2c66affSColin Finck
585*c2c66affSColin Finck password = HeapAlloc(GetProcessHeap(), 0, pwlen);
586*c2c66affSColin Finck
587*c2c66affSColin Finck WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
588*c2c66affSColin Finck (LPWSTR)cred->CredentialBlob,
589*c2c66affSColin Finck cred->CredentialBlobSize / sizeof(WCHAR),
590*c2c66affSColin Finck password, pwlen, NULL, NULL);
591*c2c66affSColin Finck }
592*c2c66affSColin Finck
593*c2c66affSColin Finck CredFree(cred);
594*c2c66affSColin Finck
595*c2c66affSColin Finck client_argv[2] = username;
596*c2c66affSColin Finck client_argv[3] = domain;
597*c2c66affSColin Finck client_argv[4] = NULL;
598*c2c66affSColin Finck }
599*c2c66affSColin Finck else
600*c2c66affSColin Finck {
601*c2c66affSColin Finck status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
602*c2c66affSColin Finck if (status != NERR_Success || ui == NULL || ntlm_cred->no_cached_credentials)
603*c2c66affSColin Finck {
604*c2c66affSColin Finck ret = SEC_E_NO_CREDENTIALS;
605*c2c66affSColin Finck goto isc_end;
606*c2c66affSColin Finck }
607*c2c66affSColin Finck username = ntlm_GetUsernameArg(ui->wkui1_username, -1);
608*c2c66affSColin Finck NetApiBufferFree(ui);
609*c2c66affSColin Finck
610*c2c66affSColin Finck TRACE("using cached credentials\n");
611*c2c66affSColin Finck
612*c2c66affSColin Finck client_argv[2] = username;
613*c2c66affSColin Finck client_argv[3] = credentials_argv;
614*c2c66affSColin Finck client_argv[4] = NULL;
615*c2c66affSColin Finck }
616*c2c66affSColin Finck }
617*c2c66affSColin Finck else
618*c2c66affSColin Finck {
619*c2c66affSColin Finck client_argv[2] = ntlm_cred->username_arg;
620*c2c66affSColin Finck client_argv[3] = ntlm_cred->domain_arg;
621*c2c66affSColin Finck client_argv[4] = NULL;
622*c2c66affSColin Finck }
623*c2c66affSColin Finck
624*c2c66affSColin Finck if((ret = fork_helper(&helper, ntlm_auth, client_argv)) != SEC_E_OK)
625*c2c66affSColin Finck goto isc_end;
626*c2c66affSColin Finck
627*c2c66affSColin Finck helper->mode = NTLM_CLIENT;
628*c2c66affSColin Finck helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
629*c2c66affSColin Finck if (!helper->session_key)
630*c2c66affSColin Finck {
631*c2c66affSColin Finck cleanup_helper(helper);
632*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
633*c2c66affSColin Finck goto isc_end;
634*c2c66affSColin Finck }
635*c2c66affSColin Finck
636*c2c66affSColin Finck /* Generate the dummy session key = MD4(MD4(password))*/
637*c2c66affSColin Finck if(password || ntlm_cred->password)
638*c2c66affSColin Finck {
639*c2c66affSColin Finck SEC_WCHAR *unicode_password;
640*c2c66affSColin Finck int passwd_lenW;
641*c2c66affSColin Finck
642*c2c66affSColin Finck TRACE("Converting password to unicode.\n");
643*c2c66affSColin Finck passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
644*c2c66affSColin Finck password ? password : ntlm_cred->password,
645*c2c66affSColin Finck password ? pwlen : ntlm_cred->pwlen,
646*c2c66affSColin Finck NULL, 0);
647*c2c66affSColin Finck unicode_password = HeapAlloc(GetProcessHeap(), 0,
648*c2c66affSColin Finck passwd_lenW * sizeof(SEC_WCHAR));
649*c2c66affSColin Finck MultiByteToWideChar(CP_ACP, 0, password ? password : ntlm_cred->password,
650*c2c66affSColin Finck password ? pwlen : ntlm_cred->pwlen, unicode_password, passwd_lenW);
651*c2c66affSColin Finck
652*c2c66affSColin Finck SECUR32_CreateNTLM1SessionKey((PBYTE)unicode_password,
653*c2c66affSColin Finck passwd_lenW * sizeof(SEC_WCHAR), helper->session_key);
654*c2c66affSColin Finck
655*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, unicode_password);
656*c2c66affSColin Finck }
657*c2c66affSColin Finck else
658*c2c66affSColin Finck memset(helper->session_key, 0, 16);
659*c2c66affSColin Finck
660*c2c66affSColin Finck /* Allocate space for a maximal string of
661*c2c66affSColin Finck * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
662*c2c66affSColin Finck * NTLMSSP_FEATURE_SESSION_KEY"
663*c2c66affSColin Finck */
664*c2c66affSColin Finck want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
665*c2c66affSColin Finck if(want_flags == NULL)
666*c2c66affSColin Finck {
667*c2c66affSColin Finck cleanup_helper(helper);
668*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
669*c2c66affSColin Finck goto isc_end;
670*c2c66affSColin Finck }
671*c2c66affSColin Finck lstrcpyA(want_flags, "SF");
672*c2c66affSColin Finck if(fContextReq & ISC_REQ_CONFIDENTIALITY)
673*c2c66affSColin Finck {
674*c2c66affSColin Finck if(strstr(want_flags, "NTLMSSP_FEATURE_SEAL") == NULL)
675*c2c66affSColin Finck lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
676*c2c66affSColin Finck }
677*c2c66affSColin Finck if(fContextReq & ISC_REQ_CONNECTION)
678*c2c66affSColin Finck ctxt_attr |= ISC_RET_CONNECTION;
679*c2c66affSColin Finck if(fContextReq & ISC_REQ_EXTENDED_ERROR)
680*c2c66affSColin Finck ctxt_attr |= ISC_RET_EXTENDED_ERROR;
681*c2c66affSColin Finck if(fContextReq & ISC_REQ_INTEGRITY)
682*c2c66affSColin Finck {
683*c2c66affSColin Finck if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
684*c2c66affSColin Finck lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
685*c2c66affSColin Finck }
686*c2c66affSColin Finck if(fContextReq & ISC_REQ_MUTUAL_AUTH)
687*c2c66affSColin Finck ctxt_attr |= ISC_RET_MUTUAL_AUTH;
688*c2c66affSColin Finck if(fContextReq & ISC_REQ_REPLAY_DETECT)
689*c2c66affSColin Finck {
690*c2c66affSColin Finck if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
691*c2c66affSColin Finck lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
692*c2c66affSColin Finck }
693*c2c66affSColin Finck if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
694*c2c66affSColin Finck {
695*c2c66affSColin Finck if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
696*c2c66affSColin Finck lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
697*c2c66affSColin Finck }
698*c2c66affSColin Finck if(fContextReq & ISC_REQ_STREAM)
699*c2c66affSColin Finck FIXME("ISC_REQ_STREAM\n");
700*c2c66affSColin Finck if(fContextReq & ISC_REQ_USE_DCE_STYLE)
701*c2c66affSColin Finck ctxt_attr |= ISC_RET_USED_DCE_STYLE;
702*c2c66affSColin Finck if(fContextReq & ISC_REQ_DELEGATE)
703*c2c66affSColin Finck ctxt_attr |= ISC_RET_DELEGATE;
704*c2c66affSColin Finck
705*c2c66affSColin Finck /* If no password is given, try to use cached credentials. Fall back to an empty
706*c2c66affSColin Finck * password if this failed. */
707*c2c66affSColin Finck if(!password && !ntlm_cred->password)
708*c2c66affSColin Finck {
709*c2c66affSColin Finck lstrcpynA(buffer, "OK", max_len-1);
710*c2c66affSColin Finck if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
711*c2c66affSColin Finck {
712*c2c66affSColin Finck cleanup_helper(helper);
713*c2c66affSColin Finck goto isc_end;
714*c2c66affSColin Finck }
715*c2c66affSColin Finck /* If the helper replied with "PW", using cached credentials failed */
716*c2c66affSColin Finck if(!strncmp(buffer, "PW", 2))
717*c2c66affSColin Finck {
718*c2c66affSColin Finck TRACE("Using cached credentials failed.\n");
719*c2c66affSColin Finck lstrcpynA(buffer, "PW AA==", max_len-1);
720*c2c66affSColin Finck }
721*c2c66affSColin Finck else /* Just do a noop on the next run */
722*c2c66affSColin Finck lstrcpynA(buffer, "OK", max_len-1);
723*c2c66affSColin Finck }
724*c2c66affSColin Finck else
725*c2c66affSColin Finck {
726*c2c66affSColin Finck lstrcpynA(buffer, "PW ", max_len-1);
727*c2c66affSColin Finck if((ret = encodeBase64(password ? (unsigned char *)password : (unsigned char *)ntlm_cred->password,
728*c2c66affSColin Finck password ? pwlen : ntlm_cred->pwlen, buffer+3,
729*c2c66affSColin Finck max_len-3, &buffer_len)) != SEC_E_OK)
730*c2c66affSColin Finck {
731*c2c66affSColin Finck cleanup_helper(helper);
732*c2c66affSColin Finck goto isc_end;
733*c2c66affSColin Finck }
734*c2c66affSColin Finck
735*c2c66affSColin Finck }
736*c2c66affSColin Finck
737*c2c66affSColin Finck TRACE("Sending to helper: %s\n", debugstr_a(buffer));
738*c2c66affSColin Finck if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
739*c2c66affSColin Finck {
740*c2c66affSColin Finck cleanup_helper(helper);
741*c2c66affSColin Finck goto isc_end;
742*c2c66affSColin Finck }
743*c2c66affSColin Finck
744*c2c66affSColin Finck TRACE("Helper returned %s\n", debugstr_a(buffer));
745*c2c66affSColin Finck
746*c2c66affSColin Finck if(lstrlenA(want_flags) > 2)
747*c2c66affSColin Finck {
748*c2c66affSColin Finck TRACE("Want flags are %s\n", debugstr_a(want_flags));
749*c2c66affSColin Finck lstrcpynA(buffer, want_flags, max_len-1);
750*c2c66affSColin Finck if((ret = run_helper(helper, buffer, max_len, &buffer_len))
751*c2c66affSColin Finck != SEC_E_OK)
752*c2c66affSColin Finck {
753*c2c66affSColin Finck cleanup_helper(helper);
754*c2c66affSColin Finck goto isc_end;
755*c2c66affSColin Finck }
756*c2c66affSColin Finck if(!strncmp(buffer, "BH", 2))
757*c2c66affSColin Finck ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
758*c2c66affSColin Finck }
759*c2c66affSColin Finck
760*c2c66affSColin Finck lstrcpynA(buffer, "YR", max_len-1);
761*c2c66affSColin Finck
762*c2c66affSColin Finck if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
763*c2c66affSColin Finck {
764*c2c66affSColin Finck cleanup_helper(helper);
765*c2c66affSColin Finck goto isc_end;
766*c2c66affSColin Finck }
767*c2c66affSColin Finck
768*c2c66affSColin Finck TRACE("%s\n", buffer);
769*c2c66affSColin Finck
770*c2c66affSColin Finck if(strncmp(buffer, "YR ", 3) != 0)
771*c2c66affSColin Finck {
772*c2c66affSColin Finck /* Something borked */
773*c2c66affSColin Finck TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
774*c2c66affSColin Finck ret = SEC_E_INTERNAL_ERROR;
775*c2c66affSColin Finck cleanup_helper(helper);
776*c2c66affSColin Finck goto isc_end;
777*c2c66affSColin Finck }
778*c2c66affSColin Finck if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
779*c2c66affSColin Finck max_len-1, &bin_len)) != SEC_E_OK)
780*c2c66affSColin Finck {
781*c2c66affSColin Finck cleanup_helper(helper);
782*c2c66affSColin Finck goto isc_end;
783*c2c66affSColin Finck }
784*c2c66affSColin Finck
785*c2c66affSColin Finck /* put the decoded client blob into the out buffer */
786*c2c66affSColin Finck
787*c2c66affSColin Finck phNewContext->dwUpper = ctxt_attr;
788*c2c66affSColin Finck phNewContext->dwLower = (ULONG_PTR)helper;
789*c2c66affSColin Finck
790*c2c66affSColin Finck ret = SEC_I_CONTINUE_NEEDED;
791*c2c66affSColin Finck }
792*c2c66affSColin Finck else
793*c2c66affSColin Finck {
794*c2c66affSColin Finck int input_token_idx;
795*c2c66affSColin Finck
796*c2c66affSColin Finck /* handle second call here */
797*c2c66affSColin Finck /* encode server data to base64 */
798*c2c66affSColin Finck if (!pInput || ((input_token_idx = ntlm_GetTokenBufferIndex(pInput)) == -1))
799*c2c66affSColin Finck {
800*c2c66affSColin Finck ret = SEC_E_INVALID_TOKEN;
801*c2c66affSColin Finck goto isc_end;
802*c2c66affSColin Finck }
803*c2c66affSColin Finck
804*c2c66affSColin Finck if(!phContext)
805*c2c66affSColin Finck {
806*c2c66affSColin Finck ret = SEC_E_INVALID_HANDLE;
807*c2c66affSColin Finck goto isc_end;
808*c2c66affSColin Finck }
809*c2c66affSColin Finck
810*c2c66affSColin Finck /* As the server side of sspi never calls this, make sure that
811*c2c66affSColin Finck * the handler is a client handler.
812*c2c66affSColin Finck */
813*c2c66affSColin Finck helper = (PNegoHelper)phContext->dwLower;
814*c2c66affSColin Finck if(helper->mode != NTLM_CLIENT)
815*c2c66affSColin Finck {
816*c2c66affSColin Finck TRACE("Helper mode = %d\n", helper->mode);
817*c2c66affSColin Finck ret = SEC_E_INVALID_HANDLE;
818*c2c66affSColin Finck goto isc_end;
819*c2c66affSColin Finck }
820*c2c66affSColin Finck
821*c2c66affSColin Finck if (!pInput->pBuffers[input_token_idx].pvBuffer)
822*c2c66affSColin Finck {
823*c2c66affSColin Finck ret = SEC_E_INTERNAL_ERROR;
824*c2c66affSColin Finck goto isc_end;
825*c2c66affSColin Finck }
826*c2c66affSColin Finck
827*c2c66affSColin Finck if(pInput->pBuffers[input_token_idx].cbBuffer > max_len)
828*c2c66affSColin Finck {
829*c2c66affSColin Finck TRACE("pInput->pBuffers[%d].cbBuffer is: %d\n",
830*c2c66affSColin Finck input_token_idx,
831*c2c66affSColin Finck pInput->pBuffers[input_token_idx].cbBuffer);
832*c2c66affSColin Finck ret = SEC_E_INVALID_TOKEN;
833*c2c66affSColin Finck goto isc_end;
834*c2c66affSColin Finck }
835*c2c66affSColin Finck else
836*c2c66affSColin Finck bin_len = pInput->pBuffers[input_token_idx].cbBuffer;
837*c2c66affSColin Finck
838*c2c66affSColin Finck memcpy(bin, pInput->pBuffers[input_token_idx].pvBuffer, bin_len);
839*c2c66affSColin Finck
840*c2c66affSColin Finck lstrcpynA(buffer, "TT ", max_len-1);
841*c2c66affSColin Finck
842*c2c66affSColin Finck if((ret = encodeBase64(bin, bin_len, buffer+3,
843*c2c66affSColin Finck max_len-3, &buffer_len)) != SEC_E_OK)
844*c2c66affSColin Finck goto isc_end;
845*c2c66affSColin Finck
846*c2c66affSColin Finck TRACE("Server sent: %s\n", debugstr_a(buffer));
847*c2c66affSColin Finck
848*c2c66affSColin Finck /* send TT base64 blob to ntlm_auth */
849*c2c66affSColin Finck if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
850*c2c66affSColin Finck goto isc_end;
851*c2c66affSColin Finck
852*c2c66affSColin Finck TRACE("Helper replied: %s\n", debugstr_a(buffer));
853*c2c66affSColin Finck
854*c2c66affSColin Finck if( (strncmp(buffer, "KK ", 3) != 0) &&
855*c2c66affSColin Finck (strncmp(buffer, "AF ", 3) !=0))
856*c2c66affSColin Finck {
857*c2c66affSColin Finck TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
858*c2c66affSColin Finck ret = SEC_E_INVALID_TOKEN;
859*c2c66affSColin Finck goto isc_end;
860*c2c66affSColin Finck }
861*c2c66affSColin Finck
862*c2c66affSColin Finck /* decode the blob and send it to server */
863*c2c66affSColin Finck if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
864*c2c66affSColin Finck &bin_len)) != SEC_E_OK)
865*c2c66affSColin Finck {
866*c2c66affSColin Finck goto isc_end;
867*c2c66affSColin Finck }
868*c2c66affSColin Finck
869*c2c66affSColin Finck phNewContext->dwUpper = ctxt_attr;
870*c2c66affSColin Finck phNewContext->dwLower = (ULONG_PTR)helper;
871*c2c66affSColin Finck
872*c2c66affSColin Finck ret = SEC_E_OK;
873*c2c66affSColin Finck }
874*c2c66affSColin Finck
875*c2c66affSColin Finck /* put the decoded client blob into the out buffer */
876*c2c66affSColin Finck
877*c2c66affSColin Finck if (!pOutput || ((token_idx = ntlm_GetTokenBufferIndex(pOutput)) == -1))
878*c2c66affSColin Finck {
879*c2c66affSColin Finck TRACE("no SECBUFFER_TOKEN buffer could be found\n");
880*c2c66affSColin Finck ret = SEC_E_BUFFER_TOO_SMALL;
881*c2c66affSColin Finck if ((phContext == NULL) && (pInput == NULL))
882*c2c66affSColin Finck {
883*c2c66affSColin Finck cleanup_helper(helper);
884*c2c66affSColin Finck phNewContext->dwUpper = 0;
885*c2c66affSColin Finck phNewContext->dwLower = 0;
886*c2c66affSColin Finck }
887*c2c66affSColin Finck goto isc_end;
888*c2c66affSColin Finck }
889*c2c66affSColin Finck
890*c2c66affSColin Finck if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
891*c2c66affSColin Finck {
892*c2c66affSColin Finck pOutput->pBuffers[token_idx].pvBuffer = HeapAlloc(GetProcessHeap(), 0, bin_len);
893*c2c66affSColin Finck pOutput->pBuffers[token_idx].cbBuffer = bin_len;
894*c2c66affSColin Finck }
895*c2c66affSColin Finck else if (pOutput->pBuffers[token_idx].cbBuffer < bin_len)
896*c2c66affSColin Finck {
897*c2c66affSColin Finck TRACE("out buffer is NULL or has not enough space\n");
898*c2c66affSColin Finck ret = SEC_E_BUFFER_TOO_SMALL;
899*c2c66affSColin Finck if ((phContext == NULL) && (pInput == NULL))
900*c2c66affSColin Finck {
901*c2c66affSColin Finck cleanup_helper(helper);
902*c2c66affSColin Finck phNewContext->dwUpper = 0;
903*c2c66affSColin Finck phNewContext->dwLower = 0;
904*c2c66affSColin Finck }
905*c2c66affSColin Finck goto isc_end;
906*c2c66affSColin Finck }
907*c2c66affSColin Finck
908*c2c66affSColin Finck if (!pOutput->pBuffers[token_idx].pvBuffer)
909*c2c66affSColin Finck {
910*c2c66affSColin Finck TRACE("out buffer is NULL\n");
911*c2c66affSColin Finck ret = SEC_E_INTERNAL_ERROR;
912*c2c66affSColin Finck if ((phContext == NULL) && (pInput == NULL))
913*c2c66affSColin Finck {
914*c2c66affSColin Finck cleanup_helper(helper);
915*c2c66affSColin Finck phNewContext->dwUpper = 0;
916*c2c66affSColin Finck phNewContext->dwLower = 0;
917*c2c66affSColin Finck }
918*c2c66affSColin Finck goto isc_end;
919*c2c66affSColin Finck }
920*c2c66affSColin Finck
921*c2c66affSColin Finck pOutput->pBuffers[token_idx].cbBuffer = bin_len;
922*c2c66affSColin Finck memcpy(pOutput->pBuffers[token_idx].pvBuffer, bin, bin_len);
923*c2c66affSColin Finck
924*c2c66affSColin Finck if(ret == SEC_E_OK)
925*c2c66affSColin Finck {
926*c2c66affSColin Finck TRACE("Getting negotiated flags\n");
927*c2c66affSColin Finck lstrcpynA(buffer, "GF", max_len - 1);
928*c2c66affSColin Finck if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
929*c2c66affSColin Finck goto isc_end;
930*c2c66affSColin Finck
931*c2c66affSColin Finck if(buffer_len < 3)
932*c2c66affSColin Finck {
933*c2c66affSColin Finck TRACE("No flags negotiated.\n");
934*c2c66affSColin Finck helper->neg_flags = 0l;
935*c2c66affSColin Finck }
936*c2c66affSColin Finck else
937*c2c66affSColin Finck {
938*c2c66affSColin Finck TRACE("Negotiated %s\n", debugstr_a(buffer));
939*c2c66affSColin Finck sscanf(buffer + 3, "%lx", &(helper->neg_flags));
940*c2c66affSColin Finck TRACE("Stored 0x%08x as flags\n", helper->neg_flags);
941*c2c66affSColin Finck }
942*c2c66affSColin Finck
943*c2c66affSColin Finck TRACE("Getting session key\n");
944*c2c66affSColin Finck lstrcpynA(buffer, "GK", max_len - 1);
945*c2c66affSColin Finck if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
946*c2c66affSColin Finck goto isc_end;
947*c2c66affSColin Finck
948*c2c66affSColin Finck if(strncmp(buffer, "BH", 2) == 0)
949*c2c66affSColin Finck TRACE("No key negotiated.\n");
950*c2c66affSColin Finck else if(strncmp(buffer, "GK ", 3) == 0)
951*c2c66affSColin Finck {
952*c2c66affSColin Finck if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
953*c2c66affSColin Finck &bin_len)) != SEC_E_OK)
954*c2c66affSColin Finck {
955*c2c66affSColin Finck TRACE("Failed to decode session key\n");
956*c2c66affSColin Finck }
957*c2c66affSColin Finck TRACE("Session key is %s\n", debugstr_a(buffer+3));
958*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, helper->session_key);
959*c2c66affSColin Finck helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
960*c2c66affSColin Finck if(!helper->session_key)
961*c2c66affSColin Finck {
962*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
963*c2c66affSColin Finck goto isc_end;
964*c2c66affSColin Finck }
965*c2c66affSColin Finck memcpy(helper->session_key, bin, bin_len);
966*c2c66affSColin Finck }
967*c2c66affSColin Finck
968*c2c66affSColin Finck helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
969*c2c66affSColin Finck SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
970*c2c66affSColin Finck helper->crypt.ntlm.seq_num = 0l;
971*c2c66affSColin Finck SECUR32_CreateNTLM2SubKeys(helper);
972*c2c66affSColin Finck helper->crypt.ntlm2.send_a4i = SECUR32_arc4Alloc();
973*c2c66affSColin Finck helper->crypt.ntlm2.recv_a4i = SECUR32_arc4Alloc();
974*c2c66affSColin Finck SECUR32_arc4Init(helper->crypt.ntlm2.send_a4i,
975*c2c66affSColin Finck helper->crypt.ntlm2.send_seal_key, 16);
976*c2c66affSColin Finck SECUR32_arc4Init(helper->crypt.ntlm2.recv_a4i,
977*c2c66affSColin Finck helper->crypt.ntlm2.recv_seal_key, 16);
978*c2c66affSColin Finck helper->crypt.ntlm2.send_seq_no = 0l;
979*c2c66affSColin Finck helper->crypt.ntlm2.recv_seq_no = 0l;
980*c2c66affSColin Finck }
981*c2c66affSColin Finck
982*c2c66affSColin Finck isc_end:
983*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, username);
984*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, domain);
985*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, password);
986*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, want_flags);
987*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, buffer);
988*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, bin);
989*c2c66affSColin Finck return ret;
990*c2c66affSColin Finck }
991*c2c66affSColin Finck
992*c2c66affSColin Finck /***********************************************************************
993*c2c66affSColin Finck * InitializeSecurityContextA
994*c2c66affSColin Finck */
ntlm_InitializeSecurityContextA(PCredHandle phCredential,PCtxtHandle phContext,SEC_CHAR * pszTargetName,ULONG fContextReq,ULONG Reserved1,ULONG TargetDataRep,PSecBufferDesc pInput,ULONG Reserved2,PCtxtHandle phNewContext,PSecBufferDesc pOutput,ULONG * pfContextAttr,PTimeStamp ptsExpiry)995*c2c66affSColin Finck static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
996*c2c66affSColin Finck PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
997*c2c66affSColin Finck ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
998*c2c66affSColin Finck PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
999*c2c66affSColin Finck PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
1000*c2c66affSColin Finck {
1001*c2c66affSColin Finck SECURITY_STATUS ret;
1002*c2c66affSColin Finck SEC_WCHAR *target = NULL;
1003*c2c66affSColin Finck
1004*c2c66affSColin Finck TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
1005*c2c66affSColin Finck debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
1006*c2c66affSColin Finck Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
1007*c2c66affSColin Finck
1008*c2c66affSColin Finck if(pszTargetName != NULL)
1009*c2c66affSColin Finck {
1010*c2c66affSColin Finck int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
1011*c2c66affSColin Finck strlen(pszTargetName)+1, NULL, 0);
1012*c2c66affSColin Finck target = HeapAlloc(GetProcessHeap(), 0, target_size *
1013*c2c66affSColin Finck sizeof(SEC_WCHAR));
1014*c2c66affSColin Finck MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
1015*c2c66affSColin Finck target, target_size);
1016*c2c66affSColin Finck }
1017*c2c66affSColin Finck
1018*c2c66affSColin Finck ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
1019*c2c66affSColin Finck fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
1020*c2c66affSColin Finck phNewContext, pOutput, pfContextAttr, ptsExpiry);
1021*c2c66affSColin Finck
1022*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, target);
1023*c2c66affSColin Finck return ret;
1024*c2c66affSColin Finck }
1025*c2c66affSColin Finck
1026*c2c66affSColin Finck /***********************************************************************
1027*c2c66affSColin Finck * AcceptSecurityContext
1028*c2c66affSColin Finck */
ntlm_AcceptSecurityContext(PCredHandle phCredential,PCtxtHandle phContext,PSecBufferDesc pInput,ULONG fContextReq,ULONG TargetDataRep,PCtxtHandle phNewContext,PSecBufferDesc pOutput,ULONG * pfContextAttr,PTimeStamp ptsExpiry)1029*c2c66affSColin Finck SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
1030*c2c66affSColin Finck PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
1031*c2c66affSColin Finck ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
1032*c2c66affSColin Finck PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
1033*c2c66affSColin Finck {
1034*c2c66affSColin Finck SECURITY_STATUS ret;
1035*c2c66affSColin Finck char *buffer, *want_flags = NULL;
1036*c2c66affSColin Finck PBYTE bin;
1037*c2c66affSColin Finck int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
1038*c2c66affSColin Finck ULONG ctxt_attr = 0;
1039*c2c66affSColin Finck PNegoHelper helper;
1040*c2c66affSColin Finck PNtlmCredentials ntlm_cred;
1041*c2c66affSColin Finck
1042*c2c66affSColin Finck TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
1043*c2c66affSColin Finck fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
1044*c2c66affSColin Finck ptsExpiry);
1045*c2c66affSColin Finck
1046*c2c66affSColin Finck buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
1047*c2c66affSColin Finck bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
1048*c2c66affSColin Finck
1049*c2c66affSColin Finck if(TargetDataRep == SECURITY_NETWORK_DREP){
1050*c2c66affSColin Finck TRACE("Using SECURITY_NETWORK_DREP\n");
1051*c2c66affSColin Finck }
1052*c2c66affSColin Finck
1053*c2c66affSColin Finck if(phContext == NULL)
1054*c2c66affSColin Finck {
1055*c2c66affSColin Finck static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp";
1056*c2c66affSColin Finck SEC_CHAR *server_argv[] = { ntlm_auth,
1057*c2c66affSColin Finck server_helper_protocol,
1058*c2c66affSColin Finck NULL };
1059*c2c66affSColin Finck
1060*c2c66affSColin Finck if (!phCredential)
1061*c2c66affSColin Finck {
1062*c2c66affSColin Finck ret = SEC_E_INVALID_HANDLE;
1063*c2c66affSColin Finck goto asc_end;
1064*c2c66affSColin Finck }
1065*c2c66affSColin Finck
1066*c2c66affSColin Finck ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
1067*c2c66affSColin Finck
1068*c2c66affSColin Finck if(ntlm_cred->mode != NTLM_SERVER)
1069*c2c66affSColin Finck {
1070*c2c66affSColin Finck ret = SEC_E_INVALID_HANDLE;
1071*c2c66affSColin Finck goto asc_end;
1072*c2c66affSColin Finck }
1073*c2c66affSColin Finck
1074*c2c66affSColin Finck /* This is the first call to AcceptSecurityHandle */
1075*c2c66affSColin Finck if(pInput == NULL)
1076*c2c66affSColin Finck {
1077*c2c66affSColin Finck ret = SEC_E_INCOMPLETE_MESSAGE;
1078*c2c66affSColin Finck goto asc_end;
1079*c2c66affSColin Finck }
1080*c2c66affSColin Finck
1081*c2c66affSColin Finck if(pInput->cBuffers < 1)
1082*c2c66affSColin Finck {
1083*c2c66affSColin Finck ret = SEC_E_INCOMPLETE_MESSAGE;
1084*c2c66affSColin Finck goto asc_end;
1085*c2c66affSColin Finck }
1086*c2c66affSColin Finck
1087*c2c66affSColin Finck if(pInput->pBuffers[0].cbBuffer > max_len)
1088*c2c66affSColin Finck {
1089*c2c66affSColin Finck ret = SEC_E_INVALID_TOKEN;
1090*c2c66affSColin Finck goto asc_end;
1091*c2c66affSColin Finck }
1092*c2c66affSColin Finck else
1093*c2c66affSColin Finck bin_len = pInput->pBuffers[0].cbBuffer;
1094*c2c66affSColin Finck
1095*c2c66affSColin Finck if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
1096*c2c66affSColin Finck SEC_E_OK)
1097*c2c66affSColin Finck {
1098*c2c66affSColin Finck ret = SEC_E_INTERNAL_ERROR;
1099*c2c66affSColin Finck goto asc_end;
1100*c2c66affSColin Finck }
1101*c2c66affSColin Finck helper->mode = NTLM_SERVER;
1102*c2c66affSColin Finck
1103*c2c66affSColin Finck /* Handle all the flags */
1104*c2c66affSColin Finck want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
1105*c2c66affSColin Finck if(want_flags == NULL)
1106*c2c66affSColin Finck {
1107*c2c66affSColin Finck TRACE("Failed to allocate memory for the want_flags!\n");
1108*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
1109*c2c66affSColin Finck cleanup_helper(helper);
1110*c2c66affSColin Finck goto asc_end;
1111*c2c66affSColin Finck }
1112*c2c66affSColin Finck lstrcpyA(want_flags, "SF");
1113*c2c66affSColin Finck if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
1114*c2c66affSColin Finck {
1115*c2c66affSColin Finck FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
1116*c2c66affSColin Finck }
1117*c2c66affSColin Finck if(fContextReq & ASC_REQ_CONFIDENTIALITY)
1118*c2c66affSColin Finck {
1119*c2c66affSColin Finck lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
1120*c2c66affSColin Finck }
1121*c2c66affSColin Finck if(fContextReq & ASC_REQ_CONNECTION)
1122*c2c66affSColin Finck {
1123*c2c66affSColin Finck /* This is default, so we'll enable it */
1124*c2c66affSColin Finck lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
1125*c2c66affSColin Finck ctxt_attr |= ASC_RET_CONNECTION;
1126*c2c66affSColin Finck }
1127*c2c66affSColin Finck if(fContextReq & ASC_REQ_EXTENDED_ERROR)
1128*c2c66affSColin Finck {
1129*c2c66affSColin Finck FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
1130*c2c66affSColin Finck }
1131*c2c66affSColin Finck if(fContextReq & ASC_REQ_INTEGRITY)
1132*c2c66affSColin Finck {
1133*c2c66affSColin Finck lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
1134*c2c66affSColin Finck }
1135*c2c66affSColin Finck if(fContextReq & ASC_REQ_MUTUAL_AUTH)
1136*c2c66affSColin Finck {
1137*c2c66affSColin Finck FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
1138*c2c66affSColin Finck }
1139*c2c66affSColin Finck if(fContextReq & ASC_REQ_REPLAY_DETECT)
1140*c2c66affSColin Finck {
1141*c2c66affSColin Finck FIXME("ASC_REQ_REPLAY_DETECT stub\n");
1142*c2c66affSColin Finck }
1143*c2c66affSColin Finck if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
1144*c2c66affSColin Finck {
1145*c2c66affSColin Finck FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
1146*c2c66affSColin Finck }
1147*c2c66affSColin Finck if(fContextReq & ISC_REQ_STREAM)
1148*c2c66affSColin Finck {
1149*c2c66affSColin Finck FIXME("ASC_REQ_STREAM stub\n");
1150*c2c66affSColin Finck }
1151*c2c66affSColin Finck /* Done with the flags */
1152*c2c66affSColin Finck
1153*c2c66affSColin Finck if(lstrlenA(want_flags) > 3)
1154*c2c66affSColin Finck {
1155*c2c66affSColin Finck TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
1156*c2c66affSColin Finck lstrcpynA(buffer, want_flags, max_len - 1);
1157*c2c66affSColin Finck if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1158*c2c66affSColin Finck SEC_E_OK)
1159*c2c66affSColin Finck {
1160*c2c66affSColin Finck cleanup_helper(helper);
1161*c2c66affSColin Finck goto asc_end;
1162*c2c66affSColin Finck }
1163*c2c66affSColin Finck if(!strncmp(buffer, "BH", 2))
1164*c2c66affSColin Finck TRACE("Helper doesn't understand new command set\n");
1165*c2c66affSColin Finck }
1166*c2c66affSColin Finck
1167*c2c66affSColin Finck /* This is the YR request from the client, encode to base64 */
1168*c2c66affSColin Finck
1169*c2c66affSColin Finck memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1170*c2c66affSColin Finck
1171*c2c66affSColin Finck lstrcpynA(buffer, "YR ", max_len-1);
1172*c2c66affSColin Finck
1173*c2c66affSColin Finck if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1174*c2c66affSColin Finck &buffer_len)) != SEC_E_OK)
1175*c2c66affSColin Finck {
1176*c2c66affSColin Finck cleanup_helper(helper);
1177*c2c66affSColin Finck goto asc_end;
1178*c2c66affSColin Finck }
1179*c2c66affSColin Finck
1180*c2c66affSColin Finck TRACE("Client sent: %s\n", debugstr_a(buffer));
1181*c2c66affSColin Finck
1182*c2c66affSColin Finck if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1183*c2c66affSColin Finck SEC_E_OK)
1184*c2c66affSColin Finck {
1185*c2c66affSColin Finck cleanup_helper(helper);
1186*c2c66affSColin Finck goto asc_end;
1187*c2c66affSColin Finck }
1188*c2c66affSColin Finck
1189*c2c66affSColin Finck TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1190*c2c66affSColin Finck /* The expected answer is TT <base64 blob> */
1191*c2c66affSColin Finck
1192*c2c66affSColin Finck if(strncmp(buffer, "TT ", 3) != 0)
1193*c2c66affSColin Finck {
1194*c2c66affSColin Finck ret = SEC_E_INTERNAL_ERROR;
1195*c2c66affSColin Finck cleanup_helper(helper);
1196*c2c66affSColin Finck goto asc_end;
1197*c2c66affSColin Finck }
1198*c2c66affSColin Finck
1199*c2c66affSColin Finck if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1200*c2c66affSColin Finck &bin_len)) != SEC_E_OK)
1201*c2c66affSColin Finck {
1202*c2c66affSColin Finck cleanup_helper(helper);
1203*c2c66affSColin Finck goto asc_end;
1204*c2c66affSColin Finck }
1205*c2c66affSColin Finck
1206*c2c66affSColin Finck /* send this to the client */
1207*c2c66affSColin Finck if(pOutput == NULL)
1208*c2c66affSColin Finck {
1209*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
1210*c2c66affSColin Finck cleanup_helper(helper);
1211*c2c66affSColin Finck goto asc_end;
1212*c2c66affSColin Finck }
1213*c2c66affSColin Finck
1214*c2c66affSColin Finck if(pOutput->cBuffers < 1)
1215*c2c66affSColin Finck {
1216*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
1217*c2c66affSColin Finck cleanup_helper(helper);
1218*c2c66affSColin Finck goto asc_end;
1219*c2c66affSColin Finck }
1220*c2c66affSColin Finck
1221*c2c66affSColin Finck pOutput->pBuffers[0].cbBuffer = bin_len;
1222*c2c66affSColin Finck pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
1223*c2c66affSColin Finck memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
1224*c2c66affSColin Finck ret = SEC_I_CONTINUE_NEEDED;
1225*c2c66affSColin Finck
1226*c2c66affSColin Finck }
1227*c2c66affSColin Finck else
1228*c2c66affSColin Finck {
1229*c2c66affSColin Finck /* we expect a KK request from client */
1230*c2c66affSColin Finck if(pInput == NULL)
1231*c2c66affSColin Finck {
1232*c2c66affSColin Finck ret = SEC_E_INCOMPLETE_MESSAGE;
1233*c2c66affSColin Finck goto asc_end;
1234*c2c66affSColin Finck }
1235*c2c66affSColin Finck
1236*c2c66affSColin Finck if(pInput->cBuffers < 1)
1237*c2c66affSColin Finck {
1238*c2c66affSColin Finck ret = SEC_E_INCOMPLETE_MESSAGE;
1239*c2c66affSColin Finck goto asc_end;
1240*c2c66affSColin Finck }
1241*c2c66affSColin Finck
1242*c2c66affSColin Finck helper = (PNegoHelper)phContext->dwLower;
1243*c2c66affSColin Finck
1244*c2c66affSColin Finck if(helper->mode != NTLM_SERVER)
1245*c2c66affSColin Finck {
1246*c2c66affSColin Finck ret = SEC_E_INVALID_HANDLE;
1247*c2c66affSColin Finck goto asc_end;
1248*c2c66affSColin Finck }
1249*c2c66affSColin Finck
1250*c2c66affSColin Finck if(pInput->pBuffers[0].cbBuffer > max_len)
1251*c2c66affSColin Finck {
1252*c2c66affSColin Finck ret = SEC_E_INVALID_TOKEN;
1253*c2c66affSColin Finck goto asc_end;
1254*c2c66affSColin Finck }
1255*c2c66affSColin Finck else
1256*c2c66affSColin Finck bin_len = pInput->pBuffers[0].cbBuffer;
1257*c2c66affSColin Finck
1258*c2c66affSColin Finck memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1259*c2c66affSColin Finck
1260*c2c66affSColin Finck lstrcpynA(buffer, "KK ", max_len-1);
1261*c2c66affSColin Finck
1262*c2c66affSColin Finck if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1263*c2c66affSColin Finck &buffer_len)) != SEC_E_OK)
1264*c2c66affSColin Finck {
1265*c2c66affSColin Finck goto asc_end;
1266*c2c66affSColin Finck }
1267*c2c66affSColin Finck
1268*c2c66affSColin Finck TRACE("Client sent: %s\n", debugstr_a(buffer));
1269*c2c66affSColin Finck
1270*c2c66affSColin Finck if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1271*c2c66affSColin Finck SEC_E_OK)
1272*c2c66affSColin Finck {
1273*c2c66affSColin Finck goto asc_end;
1274*c2c66affSColin Finck }
1275*c2c66affSColin Finck
1276*c2c66affSColin Finck TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1277*c2c66affSColin Finck
1278*c2c66affSColin Finck /* At this point, we get a NA if the user didn't authenticate, but a BH
1279*c2c66affSColin Finck * if ntlm_auth could not connect to winbindd. Apart from running Wine
1280*c2c66affSColin Finck * as root, there is no way to fix this for now, so just handle this as
1281*c2c66affSColin Finck * a failed login. */
1282*c2c66affSColin Finck if(strncmp(buffer, "AF ", 3) != 0)
1283*c2c66affSColin Finck {
1284*c2c66affSColin Finck if(strncmp(buffer, "NA ", 3) == 0)
1285*c2c66affSColin Finck {
1286*c2c66affSColin Finck ret = SEC_E_LOGON_DENIED;
1287*c2c66affSColin Finck goto asc_end;
1288*c2c66affSColin Finck }
1289*c2c66affSColin Finck else
1290*c2c66affSColin Finck {
1291*c2c66affSColin Finck size_t ntlm_pipe_err_v3_len = strlen("BH NT_STATUS_ACCESS_DENIED");
1292*c2c66affSColin Finck size_t ntlm_pipe_err_v4_len = strlen("BH NT_STATUS_UNSUCCESSFUL");
1293*c2c66affSColin Finck
1294*c2c66affSColin Finck if( (buffer_len >= ntlm_pipe_err_v3_len &&
1295*c2c66affSColin Finck strncmp(buffer, "BH NT_STATUS_ACCESS_DENIED", ntlm_pipe_err_v3_len) == 0) ||
1296*c2c66affSColin Finck (buffer_len >= ntlm_pipe_err_v4_len &&
1297*c2c66affSColin Finck strncmp(buffer, "BH NT_STATUS_UNSUCCESSFUL", ntlm_pipe_err_v4_len) == 0) )
1298*c2c66affSColin Finck {
1299*c2c66affSColin Finck TRACE("Connection to winbindd failed\n");
1300*c2c66affSColin Finck ret = SEC_E_LOGON_DENIED;
1301*c2c66affSColin Finck }
1302*c2c66affSColin Finck else
1303*c2c66affSColin Finck ret = SEC_E_INTERNAL_ERROR;
1304*c2c66affSColin Finck
1305*c2c66affSColin Finck goto asc_end;
1306*c2c66affSColin Finck }
1307*c2c66affSColin Finck }
1308*c2c66affSColin Finck pOutput->pBuffers[0].cbBuffer = 0;
1309*c2c66affSColin Finck
1310*c2c66affSColin Finck TRACE("Getting negotiated flags\n");
1311*c2c66affSColin Finck lstrcpynA(buffer, "GF", max_len - 1);
1312*c2c66affSColin Finck if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1313*c2c66affSColin Finck goto asc_end;
1314*c2c66affSColin Finck
1315*c2c66affSColin Finck if(buffer_len < 3)
1316*c2c66affSColin Finck {
1317*c2c66affSColin Finck TRACE("No flags negotiated, or helper does not support GF command\n");
1318*c2c66affSColin Finck }
1319*c2c66affSColin Finck else
1320*c2c66affSColin Finck {
1321*c2c66affSColin Finck TRACE("Negotiated %s\n", debugstr_a(buffer));
1322*c2c66affSColin Finck sscanf(buffer + 3, "%lx", &(helper->neg_flags));
1323*c2c66affSColin Finck TRACE("Stored 0x%08x as flags\n", helper->neg_flags);
1324*c2c66affSColin Finck }
1325*c2c66affSColin Finck
1326*c2c66affSColin Finck TRACE("Getting session key\n");
1327*c2c66affSColin Finck lstrcpynA(buffer, "GK", max_len - 1);
1328*c2c66affSColin Finck if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1329*c2c66affSColin Finck goto asc_end;
1330*c2c66affSColin Finck
1331*c2c66affSColin Finck if(buffer_len < 3)
1332*c2c66affSColin Finck TRACE("Helper does not support GK command\n");
1333*c2c66affSColin Finck else
1334*c2c66affSColin Finck {
1335*c2c66affSColin Finck if(strncmp(buffer, "BH ", 3) == 0)
1336*c2c66affSColin Finck {
1337*c2c66affSColin Finck TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1338*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, helper->session_key);
1339*c2c66affSColin Finck helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1340*c2c66affSColin Finck if (!helper->session_key)
1341*c2c66affSColin Finck {
1342*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
1343*c2c66affSColin Finck goto asc_end;
1344*c2c66affSColin Finck }
1345*c2c66affSColin Finck /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1346*c2c66affSColin Finck memset(helper->session_key, 0 , 16);
1347*c2c66affSColin Finck }
1348*c2c66affSColin Finck else if(strncmp(buffer, "GK ", 3) == 0)
1349*c2c66affSColin Finck {
1350*c2c66affSColin Finck if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1351*c2c66affSColin Finck &bin_len)) != SEC_E_OK)
1352*c2c66affSColin Finck {
1353*c2c66affSColin Finck TRACE("Failed to decode session key\n");
1354*c2c66affSColin Finck }
1355*c2c66affSColin Finck TRACE("Session key is %s\n", debugstr_a(buffer+3));
1356*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, helper->session_key);
1357*c2c66affSColin Finck helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1358*c2c66affSColin Finck if(!helper->session_key)
1359*c2c66affSColin Finck {
1360*c2c66affSColin Finck ret = SEC_E_INSUFFICIENT_MEMORY;
1361*c2c66affSColin Finck goto asc_end;
1362*c2c66affSColin Finck }
1363*c2c66affSColin Finck memcpy(helper->session_key, bin, 16);
1364*c2c66affSColin Finck }
1365*c2c66affSColin Finck }
1366*c2c66affSColin Finck helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1367*c2c66affSColin Finck SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1368*c2c66affSColin Finck helper->crypt.ntlm.seq_num = 0l;
1369*c2c66affSColin Finck }
1370*c2c66affSColin Finck
1371*c2c66affSColin Finck phNewContext->dwUpper = ctxt_attr;
1372*c2c66affSColin Finck phNewContext->dwLower = (ULONG_PTR)helper;
1373*c2c66affSColin Finck
1374*c2c66affSColin Finck asc_end:
1375*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, want_flags);
1376*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, buffer);
1377*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, bin);
1378*c2c66affSColin Finck return ret;
1379*c2c66affSColin Finck }
1380*c2c66affSColin Finck
1381*c2c66affSColin Finck /***********************************************************************
1382*c2c66affSColin Finck * CompleteAuthToken
1383*c2c66affSColin Finck */
ntlm_CompleteAuthToken(PCtxtHandle phContext,PSecBufferDesc pToken)1384*c2c66affSColin Finck static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1385*c2c66affSColin Finck PSecBufferDesc pToken)
1386*c2c66affSColin Finck {
1387*c2c66affSColin Finck /* We never need to call CompleteAuthToken anyway */
1388*c2c66affSColin Finck TRACE("%p %p\n", phContext, pToken);
1389*c2c66affSColin Finck if (!phContext)
1390*c2c66affSColin Finck return SEC_E_INVALID_HANDLE;
1391*c2c66affSColin Finck
1392*c2c66affSColin Finck return SEC_E_OK;
1393*c2c66affSColin Finck }
1394*c2c66affSColin Finck
1395*c2c66affSColin Finck /***********************************************************************
1396*c2c66affSColin Finck * DeleteSecurityContext
1397*c2c66affSColin Finck */
ntlm_DeleteSecurityContext(PCtxtHandle phContext)1398*c2c66affSColin Finck SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1399*c2c66affSColin Finck {
1400*c2c66affSColin Finck PNegoHelper helper;
1401*c2c66affSColin Finck
1402*c2c66affSColin Finck TRACE("%p\n", phContext);
1403*c2c66affSColin Finck if (!phContext)
1404*c2c66affSColin Finck return SEC_E_INVALID_HANDLE;
1405*c2c66affSColin Finck
1406*c2c66affSColin Finck helper = (PNegoHelper)phContext->dwLower;
1407*c2c66affSColin Finck
1408*c2c66affSColin Finck phContext->dwUpper = 0;
1409*c2c66affSColin Finck phContext->dwLower = 0;
1410*c2c66affSColin Finck
1411*c2c66affSColin Finck SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1412*c2c66affSColin Finck SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1413*c2c66affSColin Finck SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1414*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
1415*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
1416*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
1417*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
1418*c2c66affSColin Finck
1419*c2c66affSColin Finck cleanup_helper(helper);
1420*c2c66affSColin Finck
1421*c2c66affSColin Finck return SEC_E_OK;
1422*c2c66affSColin Finck }
1423*c2c66affSColin Finck
1424*c2c66affSColin Finck /***********************************************************************
1425*c2c66affSColin Finck * QueryContextAttributesW
1426*c2c66affSColin Finck */
ntlm_QueryContextAttributesW(PCtxtHandle phContext,ULONG ulAttribute,void * pBuffer)1427*c2c66affSColin Finck SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1428*c2c66affSColin Finck ULONG ulAttribute, void *pBuffer)
1429*c2c66affSColin Finck {
1430*c2c66affSColin Finck TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1431*c2c66affSColin Finck if (!phContext)
1432*c2c66affSColin Finck return SEC_E_INVALID_HANDLE;
1433*c2c66affSColin Finck
1434*c2c66affSColin Finck switch(ulAttribute)
1435*c2c66affSColin Finck {
1436*c2c66affSColin Finck #define _x(x) case (x) : FIXME(#x" stub\n"); break
1437*c2c66affSColin Finck _x(SECPKG_ATTR_ACCESS_TOKEN);
1438*c2c66affSColin Finck _x(SECPKG_ATTR_AUTHORITY);
1439*c2c66affSColin Finck _x(SECPKG_ATTR_DCE_INFO);
1440*c2c66affSColin Finck case SECPKG_ATTR_FLAGS:
1441*c2c66affSColin Finck {
1442*c2c66affSColin Finck PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1443*c2c66affSColin Finck PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1444*c2c66affSColin Finck
1445*c2c66affSColin Finck spcf->Flags = 0;
1446*c2c66affSColin Finck if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1447*c2c66affSColin Finck spcf->Flags |= ISC_RET_INTEGRITY;
1448*c2c66affSColin Finck if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1449*c2c66affSColin Finck spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1450*c2c66affSColin Finck return SEC_E_OK;
1451*c2c66affSColin Finck }
1452*c2c66affSColin Finck _x(SECPKG_ATTR_KEY_INFO);
1453*c2c66affSColin Finck _x(SECPKG_ATTR_LIFESPAN);
1454*c2c66affSColin Finck _x(SECPKG_ATTR_NAMES);
1455*c2c66affSColin Finck _x(SECPKG_ATTR_NATIVE_NAMES);
1456*c2c66affSColin Finck _x(SECPKG_ATTR_NEGOTIATION_INFO);
1457*c2c66affSColin Finck _x(SECPKG_ATTR_PACKAGE_INFO);
1458*c2c66affSColin Finck _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1459*c2c66affSColin Finck _x(SECPKG_ATTR_SESSION_KEY);
1460*c2c66affSColin Finck case SECPKG_ATTR_SIZES:
1461*c2c66affSColin Finck {
1462*c2c66affSColin Finck PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1463*c2c66affSColin Finck spcs->cbMaxToken = NTLM_MAX_BUF;
1464*c2c66affSColin Finck spcs->cbMaxSignature = 16;
1465*c2c66affSColin Finck spcs->cbBlockSize = 0;
1466*c2c66affSColin Finck spcs->cbSecurityTrailer = 16;
1467*c2c66affSColin Finck return SEC_E_OK;
1468*c2c66affSColin Finck }
1469*c2c66affSColin Finck _x(SECPKG_ATTR_STREAM_SIZES);
1470*c2c66affSColin Finck _x(SECPKG_ATTR_TARGET_INFORMATION);
1471*c2c66affSColin Finck #undef _x
1472*c2c66affSColin Finck default:
1473*c2c66affSColin Finck TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1474*c2c66affSColin Finck }
1475*c2c66affSColin Finck
1476*c2c66affSColin Finck return SEC_E_UNSUPPORTED_FUNCTION;
1477*c2c66affSColin Finck }
1478*c2c66affSColin Finck
1479*c2c66affSColin Finck /***********************************************************************
1480*c2c66affSColin Finck * QueryContextAttributesA
1481*c2c66affSColin Finck */
ntlm_QueryContextAttributesA(PCtxtHandle phContext,ULONG ulAttribute,void * pBuffer)1482*c2c66affSColin Finck SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1483*c2c66affSColin Finck ULONG ulAttribute, void *pBuffer)
1484*c2c66affSColin Finck {
1485*c2c66affSColin Finck return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1486*c2c66affSColin Finck }
1487*c2c66affSColin Finck
1488*c2c66affSColin Finck /***********************************************************************
1489*c2c66affSColin Finck * ImpersonateSecurityContext
1490*c2c66affSColin Finck */
ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)1491*c2c66affSColin Finck static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1492*c2c66affSColin Finck {
1493*c2c66affSColin Finck SECURITY_STATUS ret;
1494*c2c66affSColin Finck
1495*c2c66affSColin Finck TRACE("%p\n", phContext);
1496*c2c66affSColin Finck if (phContext)
1497*c2c66affSColin Finck {
1498*c2c66affSColin Finck ret = SEC_E_UNSUPPORTED_FUNCTION;
1499*c2c66affSColin Finck }
1500*c2c66affSColin Finck else
1501*c2c66affSColin Finck {
1502*c2c66affSColin Finck ret = SEC_E_INVALID_HANDLE;
1503*c2c66affSColin Finck }
1504*c2c66affSColin Finck return ret;
1505*c2c66affSColin Finck }
1506*c2c66affSColin Finck
1507*c2c66affSColin Finck /***********************************************************************
1508*c2c66affSColin Finck * RevertSecurityContext
1509*c2c66affSColin Finck */
ntlm_RevertSecurityContext(PCtxtHandle phContext)1510*c2c66affSColin Finck static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1511*c2c66affSColin Finck {
1512*c2c66affSColin Finck SECURITY_STATUS ret;
1513*c2c66affSColin Finck
1514*c2c66affSColin Finck TRACE("%p\n", phContext);
1515*c2c66affSColin Finck if (phContext)
1516*c2c66affSColin Finck {
1517*c2c66affSColin Finck ret = SEC_E_UNSUPPORTED_FUNCTION;
1518*c2c66affSColin Finck }
1519*c2c66affSColin Finck else
1520*c2c66affSColin Finck {
1521*c2c66affSColin Finck ret = SEC_E_INVALID_HANDLE;
1522*c2c66affSColin Finck }
1523*c2c66affSColin Finck return ret;
1524*c2c66affSColin Finck }
1525*c2c66affSColin Finck
1526*c2c66affSColin Finck /***********************************************************************
1527*c2c66affSColin Finck * ntlm_CreateSignature
1528*c2c66affSColin Finck * As both MakeSignature and VerifySignature need this, but different keys
1529*c2c66affSColin Finck * are needed for NTLM2, the logic goes into a helper function.
1530*c2c66affSColin Finck * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1531*c2c66affSColin Finck * signing/encrypting and NTLM_RECV for verifying/decrypting. When encrypting,
1532*c2c66affSColin Finck * the signature is encrypted after the message was encrypted, so
1533*c2c66affSColin Finck * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1534*c2c66affSColin Finck * false.
1535*c2c66affSColin Finck */
ntlm_CreateSignature(PNegoHelper helper,PSecBufferDesc pMessage,int token_idx,SignDirection direction,BOOL encrypt_sig)1536*c2c66affSColin Finck static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1537*c2c66affSColin Finck int token_idx, SignDirection direction, BOOL encrypt_sig)
1538*c2c66affSColin Finck {
1539*c2c66affSColin Finck ULONG sign_version = 1;
1540*c2c66affSColin Finck UINT i;
1541*c2c66affSColin Finck PBYTE sig;
1542*c2c66affSColin Finck TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1543*c2c66affSColin Finck encrypt_sig);
1544*c2c66affSColin Finck
1545*c2c66affSColin Finck sig = pMessage->pBuffers[token_idx].pvBuffer;
1546*c2c66affSColin Finck
1547*c2c66affSColin Finck if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1548*c2c66affSColin Finck helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1549*c2c66affSColin Finck {
1550*c2c66affSColin Finck BYTE digest[16];
1551*c2c66affSColin Finck BYTE seq_no[4];
1552*c2c66affSColin Finck HMAC_MD5_CTX hmac_md5_ctx;
1553*c2c66affSColin Finck
1554*c2c66affSColin Finck TRACE("Signing NTLM2 style\n");
1555*c2c66affSColin Finck
1556*c2c66affSColin Finck if(direction == NTLM_SEND)
1557*c2c66affSColin Finck {
1558*c2c66affSColin Finck seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
1559*c2c66affSColin Finck seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
1560*c2c66affSColin Finck seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1561*c2c66affSColin Finck seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1562*c2c66affSColin Finck
1563*c2c66affSColin Finck ++(helper->crypt.ntlm2.send_seq_no);
1564*c2c66affSColin Finck
1565*c2c66affSColin Finck HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1566*c2c66affSColin Finck }
1567*c2c66affSColin Finck else
1568*c2c66affSColin Finck {
1569*c2c66affSColin Finck seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
1570*c2c66affSColin Finck seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
1571*c2c66affSColin Finck seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1572*c2c66affSColin Finck seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1573*c2c66affSColin Finck
1574*c2c66affSColin Finck ++(helper->crypt.ntlm2.recv_seq_no);
1575*c2c66affSColin Finck
1576*c2c66affSColin Finck HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1577*c2c66affSColin Finck }
1578*c2c66affSColin Finck
1579*c2c66affSColin Finck HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1580*c2c66affSColin Finck for( i = 0; i < pMessage->cBuffers; ++i )
1581*c2c66affSColin Finck {
1582*c2c66affSColin Finck if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1583*c2c66affSColin Finck HMACMD5Update(&hmac_md5_ctx, pMessage->pBuffers[i].pvBuffer,
1584*c2c66affSColin Finck pMessage->pBuffers[i].cbBuffer);
1585*c2c66affSColin Finck }
1586*c2c66affSColin Finck
1587*c2c66affSColin Finck HMACMD5Final(&hmac_md5_ctx, digest);
1588*c2c66affSColin Finck
1589*c2c66affSColin Finck if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1590*c2c66affSColin Finck {
1591*c2c66affSColin Finck if(direction == NTLM_SEND)
1592*c2c66affSColin Finck SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1593*c2c66affSColin Finck else
1594*c2c66affSColin Finck SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1595*c2c66affSColin Finck }
1596*c2c66affSColin Finck
1597*c2c66affSColin Finck /* The NTLM2 signature is the sign version */
1598*c2c66affSColin Finck sig[ 0] = (sign_version >> 0) & 0xff;
1599*c2c66affSColin Finck sig[ 1] = (sign_version >> 8) & 0xff;
1600*c2c66affSColin Finck sig[ 2] = (sign_version >> 16) & 0xff;
1601*c2c66affSColin Finck sig[ 3] = (sign_version >> 24) & 0xff;
1602*c2c66affSColin Finck /* The first 8 bytes of the digest */
1603*c2c66affSColin Finck memcpy(sig+4, digest, 8);
1604*c2c66affSColin Finck /* And the sequence number */
1605*c2c66affSColin Finck memcpy(sig+12, seq_no, 4);
1606*c2c66affSColin Finck
1607*c2c66affSColin Finck pMessage->pBuffers[token_idx].cbBuffer = 16;
1608*c2c66affSColin Finck
1609*c2c66affSColin Finck return SEC_E_OK;
1610*c2c66affSColin Finck }
1611*c2c66affSColin Finck if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1612*c2c66affSColin Finck {
1613*c2c66affSColin Finck ULONG crc = 0U;
1614*c2c66affSColin Finck TRACE("Signing NTLM1 style\n");
1615*c2c66affSColin Finck
1616*c2c66affSColin Finck for(i=0; i < pMessage->cBuffers; ++i)
1617*c2c66affSColin Finck {
1618*c2c66affSColin Finck if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1619*c2c66affSColin Finck {
1620*c2c66affSColin Finck crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1621*c2c66affSColin Finck pMessage->pBuffers[i].cbBuffer, crc);
1622*c2c66affSColin Finck }
1623*c2c66affSColin Finck }
1624*c2c66affSColin Finck
1625*c2c66affSColin Finck sig[ 0] = (sign_version >> 0) & 0xff;
1626*c2c66affSColin Finck sig[ 1] = (sign_version >> 8) & 0xff;
1627*c2c66affSColin Finck sig[ 2] = (sign_version >> 16) & 0xff;
1628*c2c66affSColin Finck sig[ 3] = (sign_version >> 24) & 0xff;
1629*c2c66affSColin Finck memset(sig+4, 0, 4);
1630*c2c66affSColin Finck sig[ 8] = (crc >> 0) & 0xff;
1631*c2c66affSColin Finck sig[ 9] = (crc >> 8) & 0xff;
1632*c2c66affSColin Finck sig[10] = (crc >> 16) & 0xff;
1633*c2c66affSColin Finck sig[11] = (crc >> 24) & 0xff;
1634*c2c66affSColin Finck sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1635*c2c66affSColin Finck sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1636*c2c66affSColin Finck sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1637*c2c66affSColin Finck sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1638*c2c66affSColin Finck
1639*c2c66affSColin Finck ++(helper->crypt.ntlm.seq_num);
1640*c2c66affSColin Finck
1641*c2c66affSColin Finck if(encrypt_sig)
1642*c2c66affSColin Finck SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1643*c2c66affSColin Finck return SEC_E_OK;
1644*c2c66affSColin Finck }
1645*c2c66affSColin Finck
1646*c2c66affSColin Finck if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1647*c2c66affSColin Finck {
1648*c2c66affSColin Finck TRACE("Creating a dummy signature.\n");
1649*c2c66affSColin Finck /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1650*c2c66affSColin Finck memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1651*c2c66affSColin Finck memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1652*c2c66affSColin Finck pMessage->pBuffers[token_idx].cbBuffer = 16;
1653*c2c66affSColin Finck return SEC_E_OK;
1654*c2c66affSColin Finck }
1655*c2c66affSColin Finck
1656*c2c66affSColin Finck return SEC_E_UNSUPPORTED_FUNCTION;
1657*c2c66affSColin Finck }
1658*c2c66affSColin Finck
1659*c2c66affSColin Finck /***********************************************************************
1660*c2c66affSColin Finck * MakeSignature
1661*c2c66affSColin Finck */
ntlm_MakeSignature(PCtxtHandle phContext,ULONG fQOP,PSecBufferDesc pMessage,ULONG MessageSeqNo)1662*c2c66affSColin Finck SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext,
1663*c2c66affSColin Finck ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1664*c2c66affSColin Finck {
1665*c2c66affSColin Finck PNegoHelper helper;
1666*c2c66affSColin Finck int token_idx;
1667*c2c66affSColin Finck
1668*c2c66affSColin Finck TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1669*c2c66affSColin Finck if (!phContext)
1670*c2c66affSColin Finck return SEC_E_INVALID_HANDLE;
1671*c2c66affSColin Finck
1672*c2c66affSColin Finck if(fQOP)
1673*c2c66affSColin Finck FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1674*c2c66affSColin Finck
1675*c2c66affSColin Finck if(MessageSeqNo)
1676*c2c66affSColin Finck FIXME("Ignoring MessageSeqNo\n");
1677*c2c66affSColin Finck
1678*c2c66affSColin Finck if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1679*c2c66affSColin Finck return SEC_E_INVALID_TOKEN;
1680*c2c66affSColin Finck
1681*c2c66affSColin Finck /* If we didn't find a SECBUFFER_TOKEN type buffer */
1682*c2c66affSColin Finck if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1683*c2c66affSColin Finck return SEC_E_INVALID_TOKEN;
1684*c2c66affSColin Finck
1685*c2c66affSColin Finck if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1686*c2c66affSColin Finck return SEC_E_BUFFER_TOO_SMALL;
1687*c2c66affSColin Finck
1688*c2c66affSColin Finck helper = (PNegoHelper)phContext->dwLower;
1689*c2c66affSColin Finck TRACE("Negotiated flags are: 0x%08x\n", helper->neg_flags);
1690*c2c66affSColin Finck
1691*c2c66affSColin Finck return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1692*c2c66affSColin Finck }
1693*c2c66affSColin Finck
1694*c2c66affSColin Finck /***********************************************************************
1695*c2c66affSColin Finck * VerifySignature
1696*c2c66affSColin Finck */
ntlm_VerifySignature(PCtxtHandle phContext,PSecBufferDesc pMessage,ULONG MessageSeqNo,PULONG pfQOP)1697*c2c66affSColin Finck SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1698*c2c66affSColin Finck PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1699*c2c66affSColin Finck {
1700*c2c66affSColin Finck PNegoHelper helper;
1701*c2c66affSColin Finck UINT i;
1702*c2c66affSColin Finck int token_idx;
1703*c2c66affSColin Finck SECURITY_STATUS ret;
1704*c2c66affSColin Finck SecBufferDesc local_desc;
1705*c2c66affSColin Finck PSecBuffer local_buff;
1706*c2c66affSColin Finck BYTE local_sig[16];
1707*c2c66affSColin Finck
1708*c2c66affSColin Finck TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1709*c2c66affSColin Finck if(!phContext)
1710*c2c66affSColin Finck return SEC_E_INVALID_HANDLE;
1711*c2c66affSColin Finck
1712*c2c66affSColin Finck if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1713*c2c66affSColin Finck return SEC_E_INVALID_TOKEN;
1714*c2c66affSColin Finck
1715*c2c66affSColin Finck if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1716*c2c66affSColin Finck return SEC_E_INVALID_TOKEN;
1717*c2c66affSColin Finck
1718*c2c66affSColin Finck if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1719*c2c66affSColin Finck return SEC_E_BUFFER_TOO_SMALL;
1720*c2c66affSColin Finck
1721*c2c66affSColin Finck if(MessageSeqNo)
1722*c2c66affSColin Finck FIXME("Ignoring MessageSeqNo\n");
1723*c2c66affSColin Finck
1724*c2c66affSColin Finck helper = (PNegoHelper)phContext->dwLower;
1725*c2c66affSColin Finck TRACE("Negotiated flags: 0x%08x\n", helper->neg_flags);
1726*c2c66affSColin Finck
1727*c2c66affSColin Finck local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1728*c2c66affSColin Finck
1729*c2c66affSColin Finck local_desc.ulVersion = SECBUFFER_VERSION;
1730*c2c66affSColin Finck local_desc.cBuffers = pMessage->cBuffers;
1731*c2c66affSColin Finck local_desc.pBuffers = local_buff;
1732*c2c66affSColin Finck
1733*c2c66affSColin Finck for(i=0; i < pMessage->cBuffers; ++i)
1734*c2c66affSColin Finck {
1735*c2c66affSColin Finck if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1736*c2c66affSColin Finck {
1737*c2c66affSColin Finck local_buff[i].BufferType = SECBUFFER_TOKEN;
1738*c2c66affSColin Finck local_buff[i].cbBuffer = 16;
1739*c2c66affSColin Finck local_buff[i].pvBuffer = local_sig;
1740*c2c66affSColin Finck }
1741*c2c66affSColin Finck else
1742*c2c66affSColin Finck {
1743*c2c66affSColin Finck local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1744*c2c66affSColin Finck local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1745*c2c66affSColin Finck local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1746*c2c66affSColin Finck }
1747*c2c66affSColin Finck }
1748*c2c66affSColin Finck
1749*c2c66affSColin Finck if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1750*c2c66affSColin Finck return ret;
1751*c2c66affSColin Finck
1752*c2c66affSColin Finck if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1753*c2c66affSColin Finck ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1754*c2c66affSColin Finck ret = SEC_E_MESSAGE_ALTERED;
1755*c2c66affSColin Finck else
1756*c2c66affSColin Finck ret = SEC_E_OK;
1757*c2c66affSColin Finck
1758*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, local_buff);
1759*c2c66affSColin Finck
1760*c2c66affSColin Finck return ret;
1761*c2c66affSColin Finck
1762*c2c66affSColin Finck }
1763*c2c66affSColin Finck
1764*c2c66affSColin Finck /***********************************************************************
1765*c2c66affSColin Finck * FreeCredentialsHandle
1766*c2c66affSColin Finck */
ntlm_FreeCredentialsHandle(PCredHandle phCredential)1767*c2c66affSColin Finck SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(PCredHandle phCredential)
1768*c2c66affSColin Finck {
1769*c2c66affSColin Finck SECURITY_STATUS ret;
1770*c2c66affSColin Finck
1771*c2c66affSColin Finck if(phCredential){
1772*c2c66affSColin Finck PNtlmCredentials ntlm_cred = (PNtlmCredentials) phCredential->dwLower;
1773*c2c66affSColin Finck phCredential->dwUpper = 0;
1774*c2c66affSColin Finck phCredential->dwLower = 0;
1775*c2c66affSColin Finck if (ntlm_cred->password)
1776*c2c66affSColin Finck memset(ntlm_cred->password, 0, ntlm_cred->pwlen);
1777*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, ntlm_cred->password);
1778*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, ntlm_cred->username_arg);
1779*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, ntlm_cred->domain_arg);
1780*c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, ntlm_cred);
1781*c2c66affSColin Finck ret = SEC_E_OK;
1782*c2c66affSColin Finck }
1783*c2c66affSColin Finck else
1784*c2c66affSColin Finck ret = SEC_E_OK;
1785*c2c66affSColin Finck
1786*c2c66affSColin Finck return ret;
1787*c2c66affSColin Finck }
1788*c2c66affSColin Finck
1789*c2c66affSColin Finck /***********************************************************************
1790*c2c66affSColin Finck * EncryptMessage
1791*c2c66affSColin Finck */
ntlm_EncryptMessage(PCtxtHandle phContext,ULONG fQOP,PSecBufferDesc pMessage,ULONG MessageSeqNo)1792*c2c66affSColin Finck SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1793*c2c66affSColin Finck ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1794*c2c66affSColin Finck {
1795*c2c66affSColin Finck PNegoHelper helper;
1796*c2c66affSColin Finck int token_idx, data_idx;
1797*c2c66affSColin Finck
1798*c2c66affSColin Finck TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1799*c2c66affSColin Finck
1800*c2c66affSColin Finck if(!phContext)
1801*c2c66affSColin Finck return SEC_E_INVALID_HANDLE;
1802*c2c66affSColin Finck
1803*c2c66affSColin Finck if(fQOP)
1804*c2c66affSColin Finck FIXME("Ignoring fQOP\n");
1805*c2c66affSColin Finck
1806*c2c66affSColin Finck if(MessageSeqNo)
1807*c2c66affSColin Finck FIXME("Ignoring MessageSeqNo\n");
1808*c2c66affSColin Finck
1809*c2c66affSColin Finck if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1810*c2c66affSColin Finck return SEC_E_INVALID_TOKEN;
1811*c2c66affSColin Finck
1812*c2c66affSColin Finck if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1813*c2c66affSColin Finck return SEC_E_INVALID_TOKEN;
1814*c2c66affSColin Finck
1815*c2c66affSColin Finck if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1 )
1816*c2c66affSColin Finck return SEC_E_INVALID_TOKEN;
1817*c2c66affSColin Finck
1818*c2c66affSColin Finck if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1819*c2c66affSColin Finck return SEC_E_BUFFER_TOO_SMALL;
1820*c2c66affSColin Finck
1821*c2c66affSColin Finck helper = (PNegoHelper) phContext->dwLower;
1822*c2c66affSColin Finck
1823*c2c66affSColin Finck if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1824*c2c66affSColin Finck helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1825*c2c66affSColin Finck {
1826*c2c66affSColin Finck ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1827*c2c66affSColin Finck SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1828*c2c66affSColin Finck pMessage->pBuffers[data_idx].pvBuffer,
1829*c2c66affSColin Finck pMessage->pBuffers[data_idx].cbBuffer);
1830*c2c66affSColin Finck
1831*c2c66affSColin Finck if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1832*c2c66affSColin Finck SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1833*c2c66affSColin Finck ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1834*c2c66affSColin Finck }
1835*c2c66affSColin Finck else
1836*c2c66affSColin Finck {
1837*c2c66affSColin Finck PBYTE sig;
1838*c2c66affSColin Finck ULONG save_flags;
1839*c2c66affSColin Finck
1840*c2c66affSColin Finck /* EncryptMessage always produces real signatures, so make sure
1841*c2c66affSColin Finck * NTLMSSP_NEGOTIATE_SIGN is set*/
1842*c2c66affSColin Finck save_flags = helper->neg_flags;
1843*c2c66affSColin Finck helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1844*c2c66affSColin Finck ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1845*c2c66affSColin Finck helper->neg_flags = save_flags;
1846*c2c66affSColin Finck
1847*c2c66affSColin Finck sig = pMessage->pBuffers[token_idx].pvBuffer;
1848*c2c66affSColin Finck
1849*c2c66affSColin Finck SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1850*c2c66affSColin Finck pMessage->pBuffers[data_idx].pvBuffer,
1851*c2c66affSColin Finck pMessage->pBuffers[data_idx].cbBuffer);
1852*c2c66affSColin Finck SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1853*c2c66affSColin Finck
1854*c2c66affSColin Finck if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1855*c2c66affSColin Finck memset(sig+4, 0, 4);
1856*c2c66affSColin Finck }
1857*c2c66affSColin Finck return SEC_E_OK;
1858*c2c66affSColin Finck }
1859*c2c66affSColin Finck
1860*c2c66affSColin Finck /***********************************************************************
1861*c2c66affSColin Finck * DecryptMessage
1862*c2c66affSColin Finck */
ntlm_DecryptMessage(PCtxtHandle phContext,PSecBufferDesc pMessage,ULONG MessageSeqNo,PULONG pfQOP)1863*c2c66affSColin Finck SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1864*c2c66affSColin Finck PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1865*c2c66affSColin Finck {
1866*c2c66affSColin Finck SECURITY_STATUS ret;
1867*c2c66affSColin Finck ULONG ntlmssp_flags_save;
1868*c2c66affSColin Finck PNegoHelper helper;
1869*c2c66affSColin Finck int token_idx, data_idx;
1870*c2c66affSColin Finck TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1871*c2c66affSColin Finck
1872*c2c66affSColin Finck if(!phContext)
1873*c2c66affSColin Finck return SEC_E_INVALID_HANDLE;
1874*c2c66affSColin Finck
1875*c2c66affSColin Finck if(MessageSeqNo)
1876*c2c66affSColin Finck FIXME("Ignoring MessageSeqNo\n");
1877*c2c66affSColin Finck
1878*c2c66affSColin Finck if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1879*c2c66affSColin Finck return SEC_E_INVALID_TOKEN;
1880*c2c66affSColin Finck
1881*c2c66affSColin Finck if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1882*c2c66affSColin Finck return SEC_E_INVALID_TOKEN;
1883*c2c66affSColin Finck
1884*c2c66affSColin Finck if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1)
1885*c2c66affSColin Finck return SEC_E_INVALID_TOKEN;
1886*c2c66affSColin Finck
1887*c2c66affSColin Finck if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1888*c2c66affSColin Finck return SEC_E_BUFFER_TOO_SMALL;
1889*c2c66affSColin Finck
1890*c2c66affSColin Finck helper = (PNegoHelper) phContext->dwLower;
1891*c2c66affSColin Finck
1892*c2c66affSColin Finck if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1893*c2c66affSColin Finck {
1894*c2c66affSColin Finck SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1895*c2c66affSColin Finck pMessage->pBuffers[data_idx].pvBuffer,
1896*c2c66affSColin Finck pMessage->pBuffers[data_idx].cbBuffer);
1897*c2c66affSColin Finck }
1898*c2c66affSColin Finck else
1899*c2c66affSColin Finck {
1900*c2c66affSColin Finck SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1901*c2c66affSColin Finck pMessage->pBuffers[data_idx].pvBuffer,
1902*c2c66affSColin Finck pMessage->pBuffers[data_idx].cbBuffer);
1903*c2c66affSColin Finck }
1904*c2c66affSColin Finck
1905*c2c66affSColin Finck /* Make sure we use a session key for the signature check, EncryptMessage
1906*c2c66affSColin Finck * always does that, even in the dummy case */
1907*c2c66affSColin Finck ntlmssp_flags_save = helper->neg_flags;
1908*c2c66affSColin Finck
1909*c2c66affSColin Finck helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1910*c2c66affSColin Finck ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1911*c2c66affSColin Finck
1912*c2c66affSColin Finck helper->neg_flags = ntlmssp_flags_save;
1913*c2c66affSColin Finck
1914*c2c66affSColin Finck return ret;
1915*c2c66affSColin Finck }
1916*c2c66affSColin Finck
1917*c2c66affSColin Finck static const SecurityFunctionTableA ntlmTableA = {
1918*c2c66affSColin Finck 1,
1919*c2c66affSColin Finck NULL, /* EnumerateSecurityPackagesA */
1920*c2c66affSColin Finck ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1921*c2c66affSColin Finck ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1922*c2c66affSColin Finck ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1923*c2c66affSColin Finck NULL, /* Reserved2 */
1924*c2c66affSColin Finck ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1925*c2c66affSColin Finck ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1926*c2c66affSColin Finck ntlm_CompleteAuthToken, /* CompleteAuthToken */
1927*c2c66affSColin Finck ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1928*c2c66affSColin Finck NULL, /* ApplyControlToken */
1929*c2c66affSColin Finck ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1930*c2c66affSColin Finck ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1931*c2c66affSColin Finck ntlm_RevertSecurityContext, /* RevertSecurityContext */
1932*c2c66affSColin Finck ntlm_MakeSignature, /* MakeSignature */
1933*c2c66affSColin Finck ntlm_VerifySignature, /* VerifySignature */
1934*c2c66affSColin Finck FreeContextBuffer, /* FreeContextBuffer */
1935*c2c66affSColin Finck NULL, /* QuerySecurityPackageInfoA */
1936*c2c66affSColin Finck NULL, /* Reserved3 */
1937*c2c66affSColin Finck NULL, /* Reserved4 */
1938*c2c66affSColin Finck NULL, /* ExportSecurityContext */
1939*c2c66affSColin Finck NULL, /* ImportSecurityContextA */
1940*c2c66affSColin Finck NULL, /* AddCredentialsA */
1941*c2c66affSColin Finck NULL, /* Reserved8 */
1942*c2c66affSColin Finck NULL, /* QuerySecurityContextToken */
1943*c2c66affSColin Finck ntlm_EncryptMessage, /* EncryptMessage */
1944*c2c66affSColin Finck ntlm_DecryptMessage, /* DecryptMessage */
1945*c2c66affSColin Finck NULL, /* SetContextAttributesA */
1946*c2c66affSColin Finck };
1947*c2c66affSColin Finck
1948*c2c66affSColin Finck static const SecurityFunctionTableW ntlmTableW = {
1949*c2c66affSColin Finck 1,
1950*c2c66affSColin Finck NULL, /* EnumerateSecurityPackagesW */
1951*c2c66affSColin Finck ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1952*c2c66affSColin Finck ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1953*c2c66affSColin Finck ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1954*c2c66affSColin Finck NULL, /* Reserved2 */
1955*c2c66affSColin Finck ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1956*c2c66affSColin Finck ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1957*c2c66affSColin Finck ntlm_CompleteAuthToken, /* CompleteAuthToken */
1958*c2c66affSColin Finck ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1959*c2c66affSColin Finck NULL, /* ApplyControlToken */
1960*c2c66affSColin Finck ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1961*c2c66affSColin Finck ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1962*c2c66affSColin Finck ntlm_RevertSecurityContext, /* RevertSecurityContext */
1963*c2c66affSColin Finck ntlm_MakeSignature, /* MakeSignature */
1964*c2c66affSColin Finck ntlm_VerifySignature, /* VerifySignature */
1965*c2c66affSColin Finck FreeContextBuffer, /* FreeContextBuffer */
1966*c2c66affSColin Finck NULL, /* QuerySecurityPackageInfoW */
1967*c2c66affSColin Finck NULL, /* Reserved3 */
1968*c2c66affSColin Finck NULL, /* Reserved4 */
1969*c2c66affSColin Finck NULL, /* ExportSecurityContext */
1970*c2c66affSColin Finck NULL, /* ImportSecurityContextW */
1971*c2c66affSColin Finck NULL, /* AddCredentialsW */
1972*c2c66affSColin Finck NULL, /* Reserved8 */
1973*c2c66affSColin Finck NULL, /* QuerySecurityContextToken */
1974*c2c66affSColin Finck ntlm_EncryptMessage, /* EncryptMessage */
1975*c2c66affSColin Finck ntlm_DecryptMessage, /* DecryptMessage */
1976*c2c66affSColin Finck NULL, /* SetContextAttributesW */
1977*c2c66affSColin Finck };
1978*c2c66affSColin Finck
1979*c2c66affSColin Finck #define NTLM_COMMENT \
1980*c2c66affSColin Finck { 'N', 'T', 'L', 'M', ' ', \
1981*c2c66affSColin Finck 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1982*c2c66affSColin Finck 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1983*c2c66affSColin Finck
1984*c2c66affSColin Finck static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1985*c2c66affSColin Finck static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1986*c2c66affSColin Finck
1987*c2c66affSColin Finck #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1988*c2c66affSColin Finck
1989*c2c66affSColin Finck static char ntlm_name_A[] = NTLM_NAME;
1990*c2c66affSColin Finck static WCHAR ntlm_name_W[] = NTLM_NAME;
1991*c2c66affSColin Finck
1992*c2c66affSColin Finck /* According to Windows, NTLM has the following capabilities. */
1993*c2c66affSColin Finck #define CAPS ( \
1994*c2c66affSColin Finck SECPKG_FLAG_INTEGRITY | \
1995*c2c66affSColin Finck SECPKG_FLAG_PRIVACY | \
1996*c2c66affSColin Finck SECPKG_FLAG_TOKEN_ONLY | \
1997*c2c66affSColin Finck SECPKG_FLAG_CONNECTION | \
1998*c2c66affSColin Finck SECPKG_FLAG_MULTI_REQUIRED | \
1999*c2c66affSColin Finck SECPKG_FLAG_IMPERSONATION | \
2000*c2c66affSColin Finck SECPKG_FLAG_ACCEPT_WIN32_NAME | \
2001*c2c66affSColin Finck SECPKG_FLAG_NEGOTIABLE | \
2002*c2c66affSColin Finck SECPKG_FLAG_LOGON | \
2003*c2c66affSColin Finck SECPKG_FLAG_RESTRICTED_TOKENS )
2004*c2c66affSColin Finck
2005*c2c66affSColin Finck static const SecPkgInfoW infoW = {
2006*c2c66affSColin Finck CAPS,
2007*c2c66affSColin Finck 1,
2008*c2c66affSColin Finck RPC_C_AUTHN_WINNT,
2009*c2c66affSColin Finck NTLM_MAX_BUF,
2010*c2c66affSColin Finck ntlm_name_W,
2011*c2c66affSColin Finck ntlm_comment_W
2012*c2c66affSColin Finck };
2013*c2c66affSColin Finck
2014*c2c66affSColin Finck static const SecPkgInfoA infoA = {
2015*c2c66affSColin Finck CAPS,
2016*c2c66affSColin Finck 1,
2017*c2c66affSColin Finck RPC_C_AUTHN_WINNT,
2018*c2c66affSColin Finck NTLM_MAX_BUF,
2019*c2c66affSColin Finck ntlm_name_A,
2020*c2c66affSColin Finck ntlm_comment_A
2021*c2c66affSColin Finck };
2022*c2c66affSColin Finck
2023*c2c66affSColin Finck SecPkgInfoA *ntlm_package_infoA = (SecPkgInfoA *)&infoA;
2024*c2c66affSColin Finck SecPkgInfoW *ntlm_package_infoW = (SecPkgInfoW *)&infoW;
2025*c2c66affSColin Finck
SECUR32_initNTLMSP(void)2026*c2c66affSColin Finck void SECUR32_initNTLMSP(void)
2027*c2c66affSColin Finck {
2028*c2c66affSColin Finck PNegoHelper helper;
2029*c2c66affSColin Finck static CHAR version[] = "--version";
2030*c2c66affSColin Finck
2031*c2c66affSColin Finck SEC_CHAR *args[] = {
2032*c2c66affSColin Finck ntlm_auth,
2033*c2c66affSColin Finck version,
2034*c2c66affSColin Finck NULL };
2035*c2c66affSColin Finck
2036*c2c66affSColin Finck if(fork_helper(&helper, ntlm_auth, args) != SEC_E_OK)
2037*c2c66affSColin Finck helper = NULL;
2038*c2c66affSColin Finck else
2039*c2c66affSColin Finck check_version(helper);
2040*c2c66affSColin Finck
2041*c2c66affSColin Finck if( helper &&
2042*c2c66affSColin Finck ((helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
2043*c2c66affSColin Finck (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
2044*c2c66affSColin Finck helper->minor > MIN_NTLM_AUTH_MINOR_VERSION) ||
2045*c2c66affSColin Finck (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
2046*c2c66affSColin Finck helper->minor == MIN_NTLM_AUTH_MINOR_VERSION &&
2047*c2c66affSColin Finck helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION)) )
2048*c2c66affSColin Finck {
2049*c2c66affSColin Finck SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
2050*c2c66affSColin Finck SECUR32_addPackages(provider, 1L, ntlm_package_infoA, ntlm_package_infoW);
2051*c2c66affSColin Finck }
2052*c2c66affSColin Finck else
2053*c2c66affSColin Finck {
2054*c2c66affSColin Finck ERR_(winediag)("%s was not found or is outdated. "
2055*c2c66affSColin Finck "Make sure that ntlm_auth >= %d.%d.%d is in your path. "
2056*c2c66affSColin Finck "Usually, you can find it in the winbind package of your distribution.\n",
2057*c2c66affSColin Finck ntlm_auth,
2058*c2c66affSColin Finck MIN_NTLM_AUTH_MAJOR_VERSION,
2059*c2c66affSColin Finck MIN_NTLM_AUTH_MINOR_VERSION,
2060*c2c66affSColin Finck MIN_NTLM_AUTH_MICRO_VERSION);
2061*c2c66affSColin Finck
2062*c2c66affSColin Finck }
2063*c2c66affSColin Finck cleanup_helper(helper);
2064*c2c66affSColin Finck }
2065