wininet: Added support for INTERNET_COOKIE_HTTPONLY flag to InternetSetCookieEx.
[wine/multimedia.git] / dlls / secur32 / ntlm.c
blob5914fbcc675a8e4d299527982d6a91945ee110b0
1 /*
2 * Copyright 2005, 2006 Kai Blin
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 * This file implements the NTLM security provider.
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "wincred.h"
28 #include "rpc.h"
29 #include "sspi.h"
30 #include "lm.h"
31 #include "secur32_priv.h"
32 #include "hmac_md5.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ntlm);
37 WINE_DECLARE_DEBUG_CHANNEL(winediag);
39 #define NTLM_MAX_BUF 1904
40 #define MIN_NTLM_AUTH_MAJOR_VERSION 3
41 #define MIN_NTLM_AUTH_MINOR_VERSION 0
42 #define MIN_NTLM_AUTH_MICRO_VERSION 25
44 static CHAR ntlm_auth[] = "ntlm_auth";
46 /***********************************************************************
47 * QueryCredentialsAttributesA
49 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
50 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
52 SECURITY_STATUS ret;
54 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
56 if(ulAttribute == SECPKG_ATTR_NAMES)
58 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
59 ret = SEC_E_UNSUPPORTED_FUNCTION;
61 else
62 ret = SEC_E_UNSUPPORTED_FUNCTION;
64 return ret;
67 /***********************************************************************
68 * QueryCredentialsAttributesW
70 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
71 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
73 SECURITY_STATUS ret;
75 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
77 if(ulAttribute == SECPKG_ATTR_NAMES)
79 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
80 ret = SEC_E_UNSUPPORTED_FUNCTION;
82 else
83 ret = SEC_E_UNSUPPORTED_FUNCTION;
85 return ret;
88 static char *ntlm_GetUsernameArg(LPCWSTR userW, INT userW_length)
90 static const char username_arg[] = "--username=";
91 char *user;
92 int unixcp_size;
94 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
95 userW, userW_length, NULL, 0, NULL, NULL) + sizeof(username_arg);
96 user = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
97 if (!user) return NULL;
98 memcpy(user, username_arg, sizeof(username_arg) - 1);
99 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, userW, userW_length,
100 user + sizeof(username_arg) - 1,
101 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
102 user[unixcp_size - 1] = '\0';
103 return user;
106 static char *ntlm_GetDomainArg(LPCWSTR domainW, INT domainW_length)
108 static const char domain_arg[] = "--domain=";
109 char *domain;
110 int unixcp_size;
112 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
113 domainW, domainW_length, NULL, 0, NULL, NULL) + sizeof(domain_arg);
114 domain = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
115 if (!domain) return NULL;
116 memcpy(domain, domain_arg, sizeof(domain_arg) - 1);
117 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domainW,
118 domainW_length, domain + sizeof(domain_arg) - 1,
119 unixcp_size - sizeof(domain) + 1, NULL, NULL);
120 domain[unixcp_size - 1] = '\0';
121 return domain;
124 /***********************************************************************
125 * AcquireCredentialsHandleW
127 SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
128 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
129 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
130 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
132 SECURITY_STATUS ret;
133 PNtlmCredentials ntlm_cred;
135 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
136 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
137 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
139 switch(fCredentialUse)
141 case SECPKG_CRED_INBOUND:
142 ntlm_cred = HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred));
143 if (!ntlm_cred)
144 ret = SEC_E_INSUFFICIENT_MEMORY;
145 else
147 ntlm_cred->mode = NTLM_SERVER;
148 ntlm_cred->username_arg = NULL;
149 ntlm_cred->domain_arg = NULL;
150 ntlm_cred->password = NULL;
151 ntlm_cred->pwlen = 0;
152 ntlm_cred->no_cached_credentials = 0;
154 phCredential->dwUpper = fCredentialUse;
155 phCredential->dwLower = (ULONG_PTR)ntlm_cred;
156 ret = SEC_E_OK;
158 break;
159 case SECPKG_CRED_OUTBOUND:
161 ntlm_cred = HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred));
162 if (!ntlm_cred)
164 ret = SEC_E_INSUFFICIENT_MEMORY;
165 break;
167 ntlm_cred->mode = NTLM_CLIENT;
168 ntlm_cred->username_arg = NULL;
169 ntlm_cred->domain_arg = NULL;
170 ntlm_cred->password = NULL;
171 ntlm_cred->pwlen = 0;
172 ntlm_cred->no_cached_credentials = 0;
174 if(pAuthData != NULL)
176 PSEC_WINNT_AUTH_IDENTITY_W auth_data = pAuthData;
178 TRACE("Username is %s\n", debugstr_wn(auth_data->User, auth_data->UserLength));
179 TRACE("Domain name is %s\n", debugstr_wn(auth_data->Domain, auth_data->DomainLength));
181 ntlm_cred->username_arg = ntlm_GetUsernameArg(auth_data->User, auth_data->UserLength);
182 ntlm_cred->domain_arg = ntlm_GetDomainArg(auth_data->Domain, auth_data->DomainLength);
184 if(auth_data->PasswordLength != 0)
186 ntlm_cred->pwlen = WideCharToMultiByte(CP_UNIXCP,
187 WC_NO_BEST_FIT_CHARS, auth_data->Password,
188 auth_data->PasswordLength, NULL, 0, NULL,
189 NULL);
191 ntlm_cred->password = HeapAlloc(GetProcessHeap(), 0,
192 ntlm_cred->pwlen);
194 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
195 auth_data->Password, auth_data->PasswordLength,
196 ntlm_cred->password, ntlm_cred->pwlen, NULL, NULL);
200 phCredential->dwUpper = fCredentialUse;
201 phCredential->dwLower = (ULONG_PTR)ntlm_cred;
202 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
203 phCredential->dwUpper, phCredential->dwLower);
204 ret = SEC_E_OK;
205 break;
207 case SECPKG_CRED_BOTH:
208 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
209 ret = SEC_E_UNSUPPORTED_FUNCTION;
210 phCredential = NULL;
211 break;
212 default:
213 phCredential = NULL;
214 ret = SEC_E_UNKNOWN_CREDENTIALS;
216 return ret;
219 /***********************************************************************
220 * AcquireCredentialsHandleA
222 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
223 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
224 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
225 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
227 SECURITY_STATUS ret;
228 int user_sizeW, domain_sizeW, passwd_sizeW;
230 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
232 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
233 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
235 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
236 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
237 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
239 if(pszPackage != NULL)
241 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
242 NULL, 0);
244 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
245 sizeof(SEC_WCHAR));
246 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
250 if(pAuthData != NULL)
252 identity = pAuthData;
254 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
256 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
257 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
259 if(identity->UserLength != 0)
261 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
262 (LPCSTR)identity->User, identity->UserLength, NULL, 0);
263 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
264 sizeof(SEC_WCHAR));
265 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
266 identity->UserLength, user, user_sizeW);
268 else
270 user_sizeW = 0;
273 if(identity->DomainLength != 0)
275 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
276 (LPCSTR)identity->Domain, identity->DomainLength, NULL, 0);
277 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
278 * sizeof(SEC_WCHAR));
279 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
280 identity->DomainLength, domain, domain_sizeW);
282 else
284 domain_sizeW = 0;
287 if(identity->PasswordLength != 0)
289 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
290 (LPCSTR)identity->Password, identity->PasswordLength,
291 NULL, 0);
292 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
293 * sizeof(SEC_WCHAR));
294 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
295 identity->PasswordLength, passwd, passwd_sizeW);
297 else
299 passwd_sizeW = 0;
302 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
303 pAuthDataW->User = user;
304 pAuthDataW->UserLength = user_sizeW;
305 pAuthDataW->Domain = domain;
306 pAuthDataW->DomainLength = domain_sizeW;
307 pAuthDataW->Password = passwd;
308 pAuthDataW->PasswordLength = passwd_sizeW;
310 else
312 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
316 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
317 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
318 ptsExpiry);
320 HeapFree(GetProcessHeap(), 0, package);
321 HeapFree(GetProcessHeap(), 0, user);
322 HeapFree(GetProcessHeap(), 0, domain);
323 HeapFree(GetProcessHeap(), 0, passwd);
324 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
325 HeapFree(GetProcessHeap(), 0, pAuthDataW);
327 return ret;
330 /*************************************************************************
331 * ntlm_GetTokenBufferIndex
332 * Calculates the index of the secbuffer with BufferType == SECBUFFER_TOKEN
333 * Returns index if found or -1 if not found.
335 static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)
337 UINT i;
339 TRACE("%p\n", pMessage);
341 for( i = 0; i < pMessage->cBuffers; ++i )
343 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
344 return i;
347 return -1;
350 /*************************************************************************
351 * ntlm_GetDataBufferIndex
352 * Calculates the index of the first secbuffer with BufferType == SECBUFFER_DATA
353 * Returns index if found or -1 if not found.
355 static int ntlm_GetDataBufferIndex(PSecBufferDesc pMessage)
357 UINT i;
359 TRACE("%p\n", pMessage);
361 for( i = 0; i < pMessage->cBuffers; ++i )
363 if(pMessage->pBuffers[i].BufferType == SECBUFFER_DATA)
364 return i;
367 return -1;
370 static BOOL ntlm_GetCachedCredential(const SEC_WCHAR *pszTargetName, PCREDENTIALW *cred)
372 LPCWSTR p;
373 LPCWSTR pszHost;
374 LPWSTR pszHostOnly;
375 BOOL ret;
377 if (!pszTargetName)
378 return FALSE;
380 /* try to get the start of the hostname from service principal name (SPN) */
381 pszHost = strchrW(pszTargetName, '/');
382 if (pszHost)
384 /* skip slash character */
385 pszHost++;
387 /* find end of host by detecting start of instance port or start of referrer */
388 p = strchrW(pszHost, ':');
389 if (!p)
390 p = strchrW(pszHost, '/');
391 if (!p)
392 p = pszHost + strlenW(pszHost);
394 else /* otherwise not an SPN, just a host */
396 pszHost = pszTargetName;
397 p = pszHost + strlenW(pszHost);
400 pszHostOnly = HeapAlloc(GetProcessHeap(), 0, (p - pszHost + 1) * sizeof(WCHAR));
401 if (!pszHostOnly)
402 return FALSE;
404 memcpy(pszHostOnly, pszHost, (p - pszHost) * sizeof(WCHAR));
405 pszHostOnly[p - pszHost] = '\0';
407 ret = CredReadW(pszHostOnly, CRED_TYPE_DOMAIN_PASSWORD, 0, cred);
409 HeapFree(GetProcessHeap(), 0, pszHostOnly);
410 return ret;
413 /***********************************************************************
414 * InitializeSecurityContextW
416 SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
417 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
418 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
419 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
420 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
422 SECURITY_STATUS ret;
423 PNtlmCredentials ntlm_cred;
424 PNegoHelper helper = NULL;
425 ULONG ctxt_attr = 0;
426 char* buffer, *want_flags = NULL;
427 PBYTE bin;
428 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
429 int token_idx;
430 SEC_CHAR *username = NULL;
431 SEC_CHAR *domain = NULL;
432 SEC_CHAR *password = NULL;
434 TRACE("%p %p %s 0x%08x %d %d %p %d %p %p %p %p\n", phCredential, phContext,
435 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
436 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
438 /****************************************
439 * When communicating with the client, there can be the
440 * following reply packets:
441 * YR <base64 blob> should be sent to the server
442 * PW should be sent back to helper with
443 * base64 encoded password
444 * AF <base64 blob> client is done, blob should be
445 * sent to server with KK prefixed
446 * GF <string list> A string list of negotiated flags
447 * GK <base64 blob> base64 encoded session key
448 * BH <char reason> something broke
450 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
452 if(TargetDataRep == SECURITY_NETWORK_DREP){
453 TRACE("Setting SECURITY_NETWORK_DREP\n");
456 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
457 bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
459 if((phContext == NULL) && (pInput == NULL))
461 static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
462 static CHAR credentials_argv[] = "--use-cached-creds";
463 SEC_CHAR *client_argv[5];
464 int pwlen = 0;
466 TRACE("First time in ISC()\n");
468 if(!phCredential)
470 ret = SEC_E_INVALID_HANDLE;
471 goto isc_end;
474 /* As the server side of sspi never calls this, make sure that
475 * the handler is a client handler.
477 ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
478 if(ntlm_cred->mode != NTLM_CLIENT)
480 TRACE("Cred mode = %d\n", ntlm_cred->mode);
481 ret = SEC_E_INVALID_HANDLE;
482 goto isc_end;
485 client_argv[0] = ntlm_auth;
486 client_argv[1] = helper_protocol;
487 if (!ntlm_cred->username_arg && !ntlm_cred->domain_arg)
489 LPWKSTA_USER_INFO_1 ui = NULL;
490 NET_API_STATUS status;
491 PCREDENTIALW cred;
493 if (ntlm_GetCachedCredential(pszTargetName, &cred))
495 LPWSTR p;
496 p = strchrW(cred->UserName, '\\');
497 if (p)
499 domain = ntlm_GetDomainArg(cred->UserName, p - cred->UserName);
500 p++;
502 else
504 domain = ntlm_GetDomainArg(NULL, 0);
505 p = cred->UserName;
508 username = ntlm_GetUsernameArg(p, -1);
510 if(cred->CredentialBlobSize != 0)
512 pwlen = WideCharToMultiByte(CP_UNIXCP,
513 WC_NO_BEST_FIT_CHARS, (LPWSTR)cred->CredentialBlob,
514 cred->CredentialBlobSize / sizeof(WCHAR), NULL, 0,
515 NULL, NULL);
517 password = HeapAlloc(GetProcessHeap(), 0, pwlen);
519 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
520 (LPWSTR)cred->CredentialBlob,
521 cred->CredentialBlobSize / sizeof(WCHAR),
522 password, pwlen, NULL, NULL);
525 CredFree(cred);
527 client_argv[2] = username;
528 client_argv[3] = domain;
529 client_argv[4] = NULL;
531 else
533 status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
534 if (status != NERR_Success || ui == NULL || ntlm_cred->no_cached_credentials)
536 ret = SEC_E_NO_CREDENTIALS;
537 goto isc_end;
539 username = ntlm_GetUsernameArg(ui->wkui1_username, -1);
540 NetApiBufferFree(ui);
542 TRACE("using cached credentials\n");
544 client_argv[2] = username;
545 client_argv[3] = credentials_argv;
546 client_argv[4] = NULL;
549 else
551 client_argv[2] = ntlm_cred->username_arg;
552 client_argv[3] = ntlm_cred->domain_arg;
553 client_argv[4] = NULL;
556 if((ret = fork_helper(&helper, ntlm_auth, client_argv)) != SEC_E_OK)
557 goto isc_end;
559 helper->mode = NTLM_CLIENT;
560 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
561 if (!helper->session_key)
563 cleanup_helper(helper);
564 ret = SEC_E_INSUFFICIENT_MEMORY;
565 goto isc_end;
568 /* Generate the dummy session key = MD4(MD4(password))*/
569 if(password || ntlm_cred->password)
571 SEC_WCHAR *unicode_password;
572 int passwd_lenW;
574 TRACE("Converting password to unicode.\n");
575 passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
576 password ? password : ntlm_cred->password,
577 password ? pwlen : ntlm_cred->pwlen,
578 NULL, 0);
579 unicode_password = HeapAlloc(GetProcessHeap(), 0,
580 passwd_lenW * sizeof(SEC_WCHAR));
581 MultiByteToWideChar(CP_ACP, 0, password ? password : ntlm_cred->password,
582 password ? pwlen : ntlm_cred->pwlen, unicode_password, passwd_lenW);
584 SECUR32_CreateNTLM1SessionKey((PBYTE)unicode_password,
585 passwd_lenW * sizeof(SEC_WCHAR), helper->session_key);
587 HeapFree(GetProcessHeap(), 0, unicode_password);
589 else
590 memset(helper->session_key, 0, 16);
592 /* Allocate space for a maximal string of
593 * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
594 * NTLMSSP_FEATURE_SESSION_KEY"
596 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
597 if(want_flags == NULL)
599 cleanup_helper(helper);
600 ret = SEC_E_INSUFFICIENT_MEMORY;
601 goto isc_end;
603 lstrcpyA(want_flags, "SF");
604 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
606 if(strstr(want_flags, "NTLMSSP_FEATURE_SEAL") == NULL)
607 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
609 if(fContextReq & ISC_REQ_CONNECTION)
610 ctxt_attr |= ISC_RET_CONNECTION;
611 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
612 ctxt_attr |= ISC_RET_EXTENDED_ERROR;
613 if(fContextReq & ISC_REQ_INTEGRITY)
615 if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
616 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
618 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
619 ctxt_attr |= ISC_RET_MUTUAL_AUTH;
620 if(fContextReq & ISC_REQ_REPLAY_DETECT)
622 if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
623 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
625 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
627 if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
628 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
630 if(fContextReq & ISC_REQ_STREAM)
631 FIXME("ISC_REQ_STREAM\n");
632 if(fContextReq & ISC_REQ_USE_DCE_STYLE)
633 ctxt_attr |= ISC_RET_USED_DCE_STYLE;
634 if(fContextReq & ISC_REQ_DELEGATE)
635 ctxt_attr |= ISC_RET_DELEGATE;
637 /* If no password is given, try to use cached credentials. Fall back to an empty
638 * password if this failed. */
639 if(!password && !ntlm_cred->password)
641 lstrcpynA(buffer, "OK", max_len-1);
642 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
644 cleanup_helper(helper);
645 goto isc_end;
647 /* If the helper replied with "PW", using cached credentials failed */
648 if(!strncmp(buffer, "PW", 2))
650 TRACE("Using cached credentials failed.\n");
651 lstrcpynA(buffer, "PW AA==", max_len-1);
653 else /* Just do a noop on the next run */
654 lstrcpynA(buffer, "OK", max_len-1);
656 else
658 lstrcpynA(buffer, "PW ", max_len-1);
659 if((ret = encodeBase64(password ? (unsigned char *)password : (unsigned char *)ntlm_cred->password,
660 password ? pwlen : ntlm_cred->pwlen, buffer+3,
661 max_len-3, &buffer_len)) != SEC_E_OK)
663 cleanup_helper(helper);
664 goto isc_end;
669 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
670 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
672 cleanup_helper(helper);
673 goto isc_end;
676 TRACE("Helper returned %s\n", debugstr_a(buffer));
678 if(lstrlenA(want_flags) > 2)
680 TRACE("Want flags are %s\n", debugstr_a(want_flags));
681 lstrcpynA(buffer, want_flags, max_len-1);
682 if((ret = run_helper(helper, buffer, max_len, &buffer_len))
683 != SEC_E_OK)
685 cleanup_helper(helper);
686 goto isc_end;
688 if(!strncmp(buffer, "BH", 2))
689 ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
692 lstrcpynA(buffer, "YR", max_len-1);
694 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
696 cleanup_helper(helper);
697 goto isc_end;
700 TRACE("%s\n", buffer);
702 if(strncmp(buffer, "YR ", 3) != 0)
704 /* Something borked */
705 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
706 ret = SEC_E_INTERNAL_ERROR;
707 cleanup_helper(helper);
708 goto isc_end;
710 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
711 max_len-1, &bin_len)) != SEC_E_OK)
713 cleanup_helper(helper);
714 goto isc_end;
717 /* put the decoded client blob into the out buffer */
719 phNewContext->dwUpper = ctxt_attr;
720 phNewContext->dwLower = (ULONG_PTR)helper;
722 ret = SEC_I_CONTINUE_NEEDED;
724 else
726 int input_token_idx;
728 /* handle second call here */
729 /* encode server data to base64 */
730 if (!pInput || ((input_token_idx = ntlm_GetTokenBufferIndex(pInput)) == -1))
732 ret = SEC_E_INVALID_TOKEN;
733 goto isc_end;
736 if(!phContext)
738 ret = SEC_E_INVALID_HANDLE;
739 goto isc_end;
742 /* As the server side of sspi never calls this, make sure that
743 * the handler is a client handler.
745 helper = (PNegoHelper)phContext->dwLower;
746 if(helper->mode != NTLM_CLIENT)
748 TRACE("Helper mode = %d\n", helper->mode);
749 ret = SEC_E_INVALID_HANDLE;
750 goto isc_end;
753 if (!pInput->pBuffers[input_token_idx].pvBuffer)
755 ret = SEC_E_INTERNAL_ERROR;
756 goto isc_end;
759 if(pInput->pBuffers[input_token_idx].cbBuffer > max_len)
761 TRACE("pInput->pBuffers[%d].cbBuffer is: %d\n",
762 input_token_idx,
763 pInput->pBuffers[input_token_idx].cbBuffer);
764 ret = SEC_E_INVALID_TOKEN;
765 goto isc_end;
767 else
768 bin_len = pInput->pBuffers[input_token_idx].cbBuffer;
770 memcpy(bin, pInput->pBuffers[input_token_idx].pvBuffer, bin_len);
772 lstrcpynA(buffer, "TT ", max_len-1);
774 if((ret = encodeBase64(bin, bin_len, buffer+3,
775 max_len-3, &buffer_len)) != SEC_E_OK)
776 goto isc_end;
778 TRACE("Server sent: %s\n", debugstr_a(buffer));
780 /* send TT base64 blob to ntlm_auth */
781 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
782 goto isc_end;
784 TRACE("Helper replied: %s\n", debugstr_a(buffer));
786 if( (strncmp(buffer, "KK ", 3) != 0) &&
787 (strncmp(buffer, "AF ", 3) !=0))
789 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
790 ret = SEC_E_INVALID_TOKEN;
791 goto isc_end;
794 /* decode the blob and send it to server */
795 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
796 &bin_len)) != SEC_E_OK)
798 goto isc_end;
801 phNewContext->dwUpper = ctxt_attr;
802 phNewContext->dwLower = (ULONG_PTR)helper;
804 ret = SEC_E_OK;
807 /* put the decoded client blob into the out buffer */
809 if (!pOutput || ((token_idx = ntlm_GetTokenBufferIndex(pOutput)) == -1))
811 TRACE("no SECBUFFER_TOKEN buffer could be found\n");
812 ret = SEC_E_BUFFER_TOO_SMALL;
813 if ((phContext == NULL) && (pInput == NULL))
815 cleanup_helper(helper);
816 phNewContext->dwUpper = 0;
817 phNewContext->dwLower = 0;
819 goto isc_end;
822 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
824 pOutput->pBuffers[token_idx].pvBuffer = HeapAlloc(GetProcessHeap(), 0, bin_len);
825 pOutput->pBuffers[token_idx].cbBuffer = bin_len;
827 else if (pOutput->pBuffers[token_idx].cbBuffer < bin_len)
829 TRACE("out buffer is NULL or has not enough space\n");
830 ret = SEC_E_BUFFER_TOO_SMALL;
831 if ((phContext == NULL) && (pInput == NULL))
833 cleanup_helper(helper);
834 phNewContext->dwUpper = 0;
835 phNewContext->dwLower = 0;
837 goto isc_end;
840 if (!pOutput->pBuffers[token_idx].pvBuffer)
842 TRACE("out buffer is NULL\n");
843 ret = SEC_E_INTERNAL_ERROR;
844 if ((phContext == NULL) && (pInput == NULL))
846 cleanup_helper(helper);
847 phNewContext->dwUpper = 0;
848 phNewContext->dwLower = 0;
850 goto isc_end;
853 pOutput->pBuffers[token_idx].cbBuffer = bin_len;
854 memcpy(pOutput->pBuffers[token_idx].pvBuffer, bin, bin_len);
856 if(ret == SEC_E_OK)
858 TRACE("Getting negotiated flags\n");
859 lstrcpynA(buffer, "GF", max_len - 1);
860 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
861 goto isc_end;
863 if(buffer_len < 3)
865 TRACE("No flags negotiated.\n");
866 helper->neg_flags = 0l;
868 else
870 TRACE("Negotiated %s\n", debugstr_a(buffer));
871 sscanf(buffer + 3, "%x", &(helper->neg_flags));
872 TRACE("Stored 0x%08x as flags\n", helper->neg_flags);
875 TRACE("Getting session key\n");
876 lstrcpynA(buffer, "GK", max_len - 1);
877 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
878 goto isc_end;
880 if(strncmp(buffer, "BH", 2) == 0)
881 TRACE("No key negotiated.\n");
882 else if(strncmp(buffer, "GK ", 3) == 0)
884 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
885 &bin_len)) != SEC_E_OK)
887 TRACE("Failed to decode session key\n");
889 TRACE("Session key is %s\n", debugstr_a(buffer+3));
890 HeapFree(GetProcessHeap(), 0, helper->session_key);
891 helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
892 if(!helper->session_key)
894 ret = SEC_E_INSUFFICIENT_MEMORY;
895 goto isc_end;
897 memcpy(helper->session_key, bin, bin_len);
900 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
901 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
902 helper->crypt.ntlm.seq_num = 0l;
903 SECUR32_CreateNTLM2SubKeys(helper);
904 helper->crypt.ntlm2.send_a4i = SECUR32_arc4Alloc();
905 helper->crypt.ntlm2.recv_a4i = SECUR32_arc4Alloc();
906 SECUR32_arc4Init(helper->crypt.ntlm2.send_a4i,
907 helper->crypt.ntlm2.send_seal_key, 16);
908 SECUR32_arc4Init(helper->crypt.ntlm2.recv_a4i,
909 helper->crypt.ntlm2.recv_seal_key, 16);
910 helper->crypt.ntlm2.send_seq_no = 0l;
911 helper->crypt.ntlm2.recv_seq_no = 0l;
914 isc_end:
915 HeapFree(GetProcessHeap(), 0, username);
916 HeapFree(GetProcessHeap(), 0, domain);
917 HeapFree(GetProcessHeap(), 0, password);
918 HeapFree(GetProcessHeap(), 0, want_flags);
919 HeapFree(GetProcessHeap(), 0, buffer);
920 HeapFree(GetProcessHeap(), 0, bin);
921 return ret;
924 /***********************************************************************
925 * InitializeSecurityContextA
927 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
928 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
929 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
930 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
931 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
933 SECURITY_STATUS ret;
934 SEC_WCHAR *target = NULL;
936 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
937 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
938 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
940 if(pszTargetName != NULL)
942 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
943 strlen(pszTargetName)+1, NULL, 0);
944 target = HeapAlloc(GetProcessHeap(), 0, target_size *
945 sizeof(SEC_WCHAR));
946 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
947 target, target_size);
950 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
951 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
952 phNewContext, pOutput, pfContextAttr, ptsExpiry);
954 HeapFree(GetProcessHeap(), 0, target);
955 return ret;
958 /***********************************************************************
959 * AcceptSecurityContext
961 SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
962 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
963 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
964 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
966 SECURITY_STATUS ret;
967 char *buffer, *want_flags = NULL;
968 PBYTE bin;
969 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
970 ULONG ctxt_attr = 0;
971 PNegoHelper helper;
972 PNtlmCredentials ntlm_cred;
974 TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
975 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
976 ptsExpiry);
978 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
979 bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
981 if(TargetDataRep == SECURITY_NETWORK_DREP){
982 TRACE("Using SECURITY_NETWORK_DREP\n");
985 if(phContext == NULL)
987 static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp";
988 SEC_CHAR *server_argv[] = { ntlm_auth,
989 server_helper_protocol,
990 NULL };
992 if (!phCredential)
994 ret = SEC_E_INVALID_HANDLE;
995 goto asc_end;
998 ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
1000 if(ntlm_cred->mode != NTLM_SERVER)
1002 ret = SEC_E_INVALID_HANDLE;
1003 goto asc_end;
1006 /* This is the first call to AcceptSecurityHandle */
1007 if(pInput == NULL)
1009 ret = SEC_E_INCOMPLETE_MESSAGE;
1010 goto asc_end;
1013 if(pInput->cBuffers < 1)
1015 ret = SEC_E_INCOMPLETE_MESSAGE;
1016 goto asc_end;
1019 if(pInput->pBuffers[0].cbBuffer > max_len)
1021 ret = SEC_E_INVALID_TOKEN;
1022 goto asc_end;
1024 else
1025 bin_len = pInput->pBuffers[0].cbBuffer;
1027 if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
1028 SEC_E_OK)
1030 ret = SEC_E_INTERNAL_ERROR;
1031 goto asc_end;
1033 helper->mode = NTLM_SERVER;
1035 /* Handle all the flags */
1036 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
1037 if(want_flags == NULL)
1039 TRACE("Failed to allocate memory for the want_flags!\n");
1040 ret = SEC_E_INSUFFICIENT_MEMORY;
1041 cleanup_helper(helper);
1042 goto asc_end;
1044 lstrcpyA(want_flags, "SF");
1045 if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
1047 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
1049 if(fContextReq & ASC_REQ_CONFIDENTIALITY)
1051 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
1053 if(fContextReq & ASC_REQ_CONNECTION)
1055 /* This is default, so we'll enable it */
1056 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
1057 ctxt_attr |= ASC_RET_CONNECTION;
1059 if(fContextReq & ASC_REQ_EXTENDED_ERROR)
1061 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
1063 if(fContextReq & ASC_REQ_INTEGRITY)
1065 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
1067 if(fContextReq & ASC_REQ_MUTUAL_AUTH)
1069 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
1071 if(fContextReq & ASC_REQ_REPLAY_DETECT)
1073 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
1075 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
1077 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
1079 if(fContextReq & ISC_REQ_STREAM)
1081 FIXME("ASC_REQ_STREAM stub\n");
1083 /* Done with the flags */
1085 if(lstrlenA(want_flags) > 3)
1087 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
1088 lstrcpynA(buffer, want_flags, max_len - 1);
1089 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1090 SEC_E_OK)
1092 cleanup_helper(helper);
1093 goto asc_end;
1095 if(!strncmp(buffer, "BH", 2))
1096 TRACE("Helper doesn't understand new command set\n");
1099 /* This is the YR request from the client, encode to base64 */
1101 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1103 lstrcpynA(buffer, "YR ", max_len-1);
1105 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1106 &buffer_len)) != SEC_E_OK)
1108 cleanup_helper(helper);
1109 goto asc_end;
1112 TRACE("Client sent: %s\n", debugstr_a(buffer));
1114 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1115 SEC_E_OK)
1117 cleanup_helper(helper);
1118 goto asc_end;
1121 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1122 /* The expected answer is TT <base64 blob> */
1124 if(strncmp(buffer, "TT ", 3) != 0)
1126 ret = SEC_E_INTERNAL_ERROR;
1127 cleanup_helper(helper);
1128 goto asc_end;
1131 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1132 &bin_len)) != SEC_E_OK)
1134 cleanup_helper(helper);
1135 goto asc_end;
1138 /* send this to the client */
1139 if(pOutput == NULL)
1141 ret = SEC_E_INSUFFICIENT_MEMORY;
1142 cleanup_helper(helper);
1143 goto asc_end;
1146 if(pOutput->cBuffers < 1)
1148 ret = SEC_E_INSUFFICIENT_MEMORY;
1149 cleanup_helper(helper);
1150 goto asc_end;
1153 pOutput->pBuffers[0].cbBuffer = bin_len;
1154 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
1155 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
1156 ret = SEC_I_CONTINUE_NEEDED;
1159 else
1161 /* we expect a KK request from client */
1162 if(pInput == NULL)
1164 ret = SEC_E_INCOMPLETE_MESSAGE;
1165 goto asc_end;
1168 if(pInput->cBuffers < 1)
1170 ret = SEC_E_INCOMPLETE_MESSAGE;
1171 goto asc_end;
1174 if(!phContext)
1176 ret = SEC_E_INVALID_HANDLE;
1177 goto asc_end;
1180 helper = (PNegoHelper)phContext->dwLower;
1182 if(helper->mode != NTLM_SERVER)
1184 ret = SEC_E_INVALID_HANDLE;
1185 goto asc_end;
1188 if(pInput->pBuffers[0].cbBuffer > max_len)
1190 ret = SEC_E_INVALID_TOKEN;
1191 goto asc_end;
1193 else
1194 bin_len = pInput->pBuffers[0].cbBuffer;
1196 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1198 lstrcpynA(buffer, "KK ", max_len-1);
1200 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1201 &buffer_len)) != SEC_E_OK)
1203 goto asc_end;
1206 TRACE("Client sent: %s\n", debugstr_a(buffer));
1208 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1209 SEC_E_OK)
1211 goto asc_end;
1214 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1216 /* At this point, we get a NA if the user didn't authenticate, but a BH
1217 * if ntlm_auth could not connect to winbindd. Apart from running Wine
1218 * as root, there is no way to fix this for now, so just handle this as
1219 * a failed login. */
1220 if(strncmp(buffer, "AF ", 3) != 0)
1222 if(strncmp(buffer, "NA ", 3) == 0)
1224 ret = SEC_E_LOGON_DENIED;
1225 goto asc_end;
1227 else
1229 size_t ntlm_pipe_err_v3_len = strlen("BH NT_STATUS_ACCESS_DENIED");
1230 size_t ntlm_pipe_err_v4_len = strlen("BH NT_STATUS_UNSUCCESSFUL");
1232 if( (buffer_len >= ntlm_pipe_err_v3_len &&
1233 strncmp(buffer, "BH NT_STATUS_ACCESS_DENIED", ntlm_pipe_err_v3_len) == 0) ||
1234 (buffer_len >= ntlm_pipe_err_v4_len &&
1235 strncmp(buffer, "BH NT_STATUS_UNSUCCESSFUL", ntlm_pipe_err_v4_len) == 0) )
1237 TRACE("Connection to winbindd failed\n");
1238 ret = SEC_E_LOGON_DENIED;
1240 else
1241 ret = SEC_E_INTERNAL_ERROR;
1243 goto asc_end;
1246 pOutput->pBuffers[0].cbBuffer = 0;
1248 TRACE("Getting negotiated flags\n");
1249 lstrcpynA(buffer, "GF", max_len - 1);
1250 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1251 goto asc_end;
1253 if(buffer_len < 3)
1255 TRACE("No flags negotiated, or helper does not support GF command\n");
1257 else
1259 TRACE("Negotiated %s\n", debugstr_a(buffer));
1260 sscanf(buffer + 3, "%x", &(helper->neg_flags));
1261 TRACE("Stored 0x%08x as flags\n", helper->neg_flags);
1264 TRACE("Getting session key\n");
1265 lstrcpynA(buffer, "GK", max_len - 1);
1266 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1267 goto asc_end;
1269 if(buffer_len < 3)
1270 TRACE("Helper does not support GK command\n");
1271 else
1273 if(strncmp(buffer, "BH ", 3) == 0)
1275 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1276 HeapFree(GetProcessHeap(), 0, helper->session_key);
1277 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1278 if (!helper->session_key)
1280 ret = SEC_E_INSUFFICIENT_MEMORY;
1281 goto asc_end;
1283 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1284 memset(helper->session_key, 0 , 16);
1286 else if(strncmp(buffer, "GK ", 3) == 0)
1288 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1289 &bin_len)) != SEC_E_OK)
1291 TRACE("Failed to decode session key\n");
1293 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1294 HeapFree(GetProcessHeap(), 0, helper->session_key);
1295 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1296 if(!helper->session_key)
1298 ret = SEC_E_INSUFFICIENT_MEMORY;
1299 goto asc_end;
1301 memcpy(helper->session_key, bin, 16);
1304 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1305 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1306 helper->crypt.ntlm.seq_num = 0l;
1309 phNewContext->dwUpper = ctxt_attr;
1310 phNewContext->dwLower = (ULONG_PTR)helper;
1312 asc_end:
1313 HeapFree(GetProcessHeap(), 0, want_flags);
1314 HeapFree(GetProcessHeap(), 0, buffer);
1315 HeapFree(GetProcessHeap(), 0, bin);
1316 return ret;
1319 /***********************************************************************
1320 * CompleteAuthToken
1322 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1323 PSecBufferDesc pToken)
1325 /* We never need to call CompleteAuthToken anyway */
1326 TRACE("%p %p\n", phContext, pToken);
1327 if (!phContext)
1328 return SEC_E_INVALID_HANDLE;
1330 return SEC_E_OK;
1333 /***********************************************************************
1334 * DeleteSecurityContext
1336 SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1338 PNegoHelper helper;
1340 TRACE("%p\n", phContext);
1341 if (!phContext)
1342 return SEC_E_INVALID_HANDLE;
1344 helper = (PNegoHelper)phContext->dwLower;
1346 phContext->dwUpper = 0;
1347 phContext->dwLower = 0;
1349 SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1350 SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1351 SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1352 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
1353 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
1354 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
1355 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
1357 cleanup_helper(helper);
1359 return SEC_E_OK;
1362 /***********************************************************************
1363 * QueryContextAttributesW
1365 SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1366 ULONG ulAttribute, void *pBuffer)
1368 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1369 if (!phContext)
1370 return SEC_E_INVALID_HANDLE;
1372 switch(ulAttribute)
1374 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1375 _x(SECPKG_ATTR_ACCESS_TOKEN);
1376 _x(SECPKG_ATTR_AUTHORITY);
1377 _x(SECPKG_ATTR_DCE_INFO);
1378 case SECPKG_ATTR_FLAGS:
1380 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1381 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1383 spcf->Flags = 0;
1384 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1385 spcf->Flags |= ISC_RET_INTEGRITY;
1386 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1387 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1388 return SEC_E_OK;
1390 _x(SECPKG_ATTR_KEY_INFO);
1391 _x(SECPKG_ATTR_LIFESPAN);
1392 _x(SECPKG_ATTR_NAMES);
1393 _x(SECPKG_ATTR_NATIVE_NAMES);
1394 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1395 _x(SECPKG_ATTR_PACKAGE_INFO);
1396 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1397 _x(SECPKG_ATTR_SESSION_KEY);
1398 case SECPKG_ATTR_SIZES:
1400 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1401 spcs->cbMaxToken = NTLM_MAX_BUF;
1402 spcs->cbMaxSignature = 16;
1403 spcs->cbBlockSize = 0;
1404 spcs->cbSecurityTrailer = 16;
1405 return SEC_E_OK;
1407 _x(SECPKG_ATTR_STREAM_SIZES);
1408 _x(SECPKG_ATTR_TARGET_INFORMATION);
1409 #undef _x
1410 default:
1411 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1414 return SEC_E_UNSUPPORTED_FUNCTION;
1417 /***********************************************************************
1418 * QueryContextAttributesA
1420 SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1421 ULONG ulAttribute, void *pBuffer)
1423 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1426 /***********************************************************************
1427 * ImpersonateSecurityContext
1429 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1431 SECURITY_STATUS ret;
1433 TRACE("%p\n", phContext);
1434 if (phContext)
1436 ret = SEC_E_UNSUPPORTED_FUNCTION;
1438 else
1440 ret = SEC_E_INVALID_HANDLE;
1442 return ret;
1445 /***********************************************************************
1446 * RevertSecurityContext
1448 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1450 SECURITY_STATUS ret;
1452 TRACE("%p\n", phContext);
1453 if (phContext)
1455 ret = SEC_E_UNSUPPORTED_FUNCTION;
1457 else
1459 ret = SEC_E_INVALID_HANDLE;
1461 return ret;
1464 /***********************************************************************
1465 * ntlm_CreateSignature
1466 * As both MakeSignature and VerifySignature need this, but different keys
1467 * are needed for NTLM2, the logic goes into a helper function.
1468 * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1469 * signing/encrypting and NTLM_RECV for verfying/decrypting. When encrypting,
1470 * the signature is encrypted after the message was encrypted, so
1471 * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1472 * false.
1474 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1475 int token_idx, SignDirection direction, BOOL encrypt_sig)
1477 ULONG sign_version = 1;
1478 UINT i;
1479 PBYTE sig;
1480 TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1481 encrypt_sig);
1483 sig = pMessage->pBuffers[token_idx].pvBuffer;
1485 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1486 helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1488 BYTE digest[16];
1489 BYTE seq_no[4];
1490 HMAC_MD5_CTX hmac_md5_ctx;
1492 TRACE("Signing NTLM2 style\n");
1494 if(direction == NTLM_SEND)
1496 seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
1497 seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
1498 seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1499 seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1501 ++(helper->crypt.ntlm2.send_seq_no);
1503 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1505 else
1507 seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
1508 seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
1509 seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1510 seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1512 ++(helper->crypt.ntlm2.recv_seq_no);
1514 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1517 HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1518 for( i = 0; i < pMessage->cBuffers; ++i )
1520 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1521 HMACMD5Update(&hmac_md5_ctx, pMessage->pBuffers[i].pvBuffer,
1522 pMessage->pBuffers[i].cbBuffer);
1525 HMACMD5Final(&hmac_md5_ctx, digest);
1527 if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1529 if(direction == NTLM_SEND)
1530 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1531 else
1532 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1535 /* The NTLM2 signature is the sign version */
1536 sig[ 0] = (sign_version >> 0) & 0xff;
1537 sig[ 1] = (sign_version >> 8) & 0xff;
1538 sig[ 2] = (sign_version >> 16) & 0xff;
1539 sig[ 3] = (sign_version >> 24) & 0xff;
1540 /* The first 8 bytes of the digest */
1541 memcpy(sig+4, digest, 8);
1542 /* And the sequence number */
1543 memcpy(sig+12, seq_no, 4);
1545 pMessage->pBuffers[token_idx].cbBuffer = 16;
1547 return SEC_E_OK;
1549 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1551 ULONG crc = 0U;
1552 TRACE("Signing NTLM1 style\n");
1554 for(i=0; i < pMessage->cBuffers; ++i)
1556 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1558 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1559 pMessage->pBuffers[i].cbBuffer, crc);
1563 sig[ 0] = (sign_version >> 0) & 0xff;
1564 sig[ 1] = (sign_version >> 8) & 0xff;
1565 sig[ 2] = (sign_version >> 16) & 0xff;
1566 sig[ 3] = (sign_version >> 24) & 0xff;
1567 memset(sig+4, 0, 4);
1568 sig[ 8] = (crc >> 0) & 0xff;
1569 sig[ 9] = (crc >> 8) & 0xff;
1570 sig[10] = (crc >> 16) & 0xff;
1571 sig[11] = (crc >> 24) & 0xff;
1572 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1573 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1574 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1575 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1577 ++(helper->crypt.ntlm.seq_num);
1579 if(encrypt_sig)
1580 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1581 return SEC_E_OK;
1584 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1586 TRACE("Creating a dummy signature.\n");
1587 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1588 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1589 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1590 pMessage->pBuffers[token_idx].cbBuffer = 16;
1591 return SEC_E_OK;
1594 return SEC_E_UNSUPPORTED_FUNCTION;
1597 /***********************************************************************
1598 * MakeSignature
1600 SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext,
1601 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1603 PNegoHelper helper;
1604 int token_idx;
1606 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1607 if (!phContext)
1608 return SEC_E_INVALID_HANDLE;
1610 if(fQOP)
1611 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1613 if(MessageSeqNo)
1614 FIXME("Ignoring MessageSeqNo\n");
1616 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1617 return SEC_E_INVALID_TOKEN;
1619 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1620 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1621 return SEC_E_INVALID_TOKEN;
1623 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1624 return SEC_E_BUFFER_TOO_SMALL;
1626 helper = (PNegoHelper)phContext->dwLower;
1627 TRACE("Negotiated flags are: 0x%08x\n", helper->neg_flags);
1629 return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1632 /***********************************************************************
1633 * VerifySignature
1635 SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1636 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1638 PNegoHelper helper;
1639 ULONG fQOP = 0;
1640 UINT i;
1641 int token_idx;
1642 SECURITY_STATUS ret;
1643 SecBufferDesc local_desc;
1644 PSecBuffer local_buff;
1645 BYTE local_sig[16];
1647 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1648 if(!phContext)
1649 return SEC_E_INVALID_HANDLE;
1651 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1652 return SEC_E_INVALID_TOKEN;
1654 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1655 return SEC_E_INVALID_TOKEN;
1657 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1658 return SEC_E_BUFFER_TOO_SMALL;
1660 if(MessageSeqNo)
1661 FIXME("Ignoring MessageSeqNo\n");
1663 helper = (PNegoHelper)phContext->dwLower;
1664 TRACE("Negotiated flags: 0x%08x\n", helper->neg_flags);
1666 local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1668 local_desc.ulVersion = SECBUFFER_VERSION;
1669 local_desc.cBuffers = pMessage->cBuffers;
1670 local_desc.pBuffers = local_buff;
1672 for(i=0; i < pMessage->cBuffers; ++i)
1674 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1676 local_buff[i].BufferType = SECBUFFER_TOKEN;
1677 local_buff[i].cbBuffer = 16;
1678 local_buff[i].pvBuffer = local_sig;
1680 else
1682 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1683 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1684 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1688 if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1689 return ret;
1691 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1692 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1693 ret = SEC_E_MESSAGE_ALTERED;
1694 else
1695 ret = SEC_E_OK;
1697 HeapFree(GetProcessHeap(), 0, local_buff);
1698 pfQOP = &fQOP;
1700 return ret;
1704 /***********************************************************************
1705 * FreeCredentialsHandle
1707 SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(PCredHandle phCredential)
1709 SECURITY_STATUS ret;
1711 if(phCredential){
1712 PNtlmCredentials ntlm_cred = (PNtlmCredentials) phCredential->dwLower;
1713 phCredential->dwUpper = 0;
1714 phCredential->dwLower = 0;
1715 if (ntlm_cred->password)
1716 memset(ntlm_cred->password, 0, ntlm_cred->pwlen);
1717 HeapFree(GetProcessHeap(), 0, ntlm_cred->password);
1718 HeapFree(GetProcessHeap(), 0, ntlm_cred->username_arg);
1719 HeapFree(GetProcessHeap(), 0, ntlm_cred->domain_arg);
1720 HeapFree(GetProcessHeap(), 0, ntlm_cred);
1721 ret = SEC_E_OK;
1723 else
1724 ret = SEC_E_OK;
1726 return ret;
1729 /***********************************************************************
1730 * EncryptMessage
1732 SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1733 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1735 PNegoHelper helper;
1736 int token_idx, data_idx;
1738 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1740 if(!phContext)
1741 return SEC_E_INVALID_HANDLE;
1743 if(fQOP)
1744 FIXME("Ignoring fQOP\n");
1746 if(MessageSeqNo)
1747 FIXME("Ignoring MessageSeqNo\n");
1749 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1750 return SEC_E_INVALID_TOKEN;
1752 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1753 return SEC_E_INVALID_TOKEN;
1755 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1 )
1756 return SEC_E_INVALID_TOKEN;
1758 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1759 return SEC_E_BUFFER_TOO_SMALL;
1761 helper = (PNegoHelper) phContext->dwLower;
1763 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1764 helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1766 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1767 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1768 pMessage->pBuffers[data_idx].pvBuffer,
1769 pMessage->pBuffers[data_idx].cbBuffer);
1771 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1772 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1773 ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1775 else
1777 PBYTE sig;
1778 ULONG save_flags;
1780 /* EncryptMessage always produces real signatures, so make sure
1781 * NTLMSSP_NEGOTIATE_SIGN is set*/
1782 save_flags = helper->neg_flags;
1783 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1784 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1785 helper->neg_flags = save_flags;
1787 sig = pMessage->pBuffers[token_idx].pvBuffer;
1789 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1790 pMessage->pBuffers[data_idx].pvBuffer,
1791 pMessage->pBuffers[data_idx].cbBuffer);
1792 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1794 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1795 memset(sig+4, 0, 4);
1797 return SEC_E_OK;
1800 /***********************************************************************
1801 * DecryptMessage
1803 SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1804 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1806 SECURITY_STATUS ret;
1807 ULONG ntlmssp_flags_save;
1808 PNegoHelper helper;
1809 int token_idx, data_idx;
1810 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1812 if(!phContext)
1813 return SEC_E_INVALID_HANDLE;
1815 if(MessageSeqNo)
1816 FIXME("Ignoring MessageSeqNo\n");
1818 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1819 return SEC_E_INVALID_TOKEN;
1821 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1822 return SEC_E_INVALID_TOKEN;
1824 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1)
1825 return SEC_E_INVALID_TOKEN;
1827 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1828 return SEC_E_BUFFER_TOO_SMALL;
1830 helper = (PNegoHelper) phContext->dwLower;
1832 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1834 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1835 pMessage->pBuffers[data_idx].pvBuffer,
1836 pMessage->pBuffers[data_idx].cbBuffer);
1838 else
1840 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1841 pMessage->pBuffers[data_idx].pvBuffer,
1842 pMessage->pBuffers[data_idx].cbBuffer);
1845 /* Make sure we use a session key for the signature check, EncryptMessage
1846 * always does that, even in the dummy case */
1847 ntlmssp_flags_save = helper->neg_flags;
1849 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1850 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1852 helper->neg_flags = ntlmssp_flags_save;
1854 return ret;
1857 static const SecurityFunctionTableA ntlmTableA = {
1859 NULL, /* EnumerateSecurityPackagesA */
1860 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1861 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1862 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1863 NULL, /* Reserved2 */
1864 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1865 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1866 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1867 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1868 NULL, /* ApplyControlToken */
1869 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1870 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1871 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1872 ntlm_MakeSignature, /* MakeSignature */
1873 ntlm_VerifySignature, /* VerifySignature */
1874 FreeContextBuffer, /* FreeContextBuffer */
1875 NULL, /* QuerySecurityPackageInfoA */
1876 NULL, /* Reserved3 */
1877 NULL, /* Reserved4 */
1878 NULL, /* ExportSecurityContext */
1879 NULL, /* ImportSecurityContextA */
1880 NULL, /* AddCredentialsA */
1881 NULL, /* Reserved8 */
1882 NULL, /* QuerySecurityContextToken */
1883 ntlm_EncryptMessage, /* EncryptMessage */
1884 ntlm_DecryptMessage, /* DecryptMessage */
1885 NULL, /* SetContextAttributesA */
1888 static const SecurityFunctionTableW ntlmTableW = {
1890 NULL, /* EnumerateSecurityPackagesW */
1891 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1892 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1893 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1894 NULL, /* Reserved2 */
1895 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1896 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1897 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1898 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1899 NULL, /* ApplyControlToken */
1900 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1901 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1902 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1903 ntlm_MakeSignature, /* MakeSignature */
1904 ntlm_VerifySignature, /* VerifySignature */
1905 FreeContextBuffer, /* FreeContextBuffer */
1906 NULL, /* QuerySecurityPackageInfoW */
1907 NULL, /* Reserved3 */
1908 NULL, /* Reserved4 */
1909 NULL, /* ExportSecurityContext */
1910 NULL, /* ImportSecurityContextW */
1911 NULL, /* AddCredentialsW */
1912 NULL, /* Reserved8 */
1913 NULL, /* QuerySecurityContextToken */
1914 ntlm_EncryptMessage, /* EncryptMessage */
1915 ntlm_DecryptMessage, /* DecryptMessage */
1916 NULL, /* SetContextAttributesW */
1919 #define NTLM_COMMENT \
1920 { 'N', 'T', 'L', 'M', ' ', \
1921 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1922 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1924 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1925 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1927 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1929 static char ntlm_name_A[] = NTLM_NAME;
1930 static WCHAR ntlm_name_W[] = NTLM_NAME;
1932 /* According to Windows, NTLM has the following capabilities. */
1933 #define CAPS ( \
1934 SECPKG_FLAG_INTEGRITY | \
1935 SECPKG_FLAG_PRIVACY | \
1936 SECPKG_FLAG_TOKEN_ONLY | \
1937 SECPKG_FLAG_CONNECTION | \
1938 SECPKG_FLAG_MULTI_REQUIRED | \
1939 SECPKG_FLAG_IMPERSONATION | \
1940 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1941 SECPKG_FLAG_NEGOTIABLE | \
1942 SECPKG_FLAG_LOGON | \
1943 SECPKG_FLAG_RESTRICTED_TOKENS )
1945 static const SecPkgInfoW infoW = {
1946 CAPS,
1948 RPC_C_AUTHN_WINNT,
1949 NTLM_MAX_BUF,
1950 ntlm_name_W,
1951 ntlm_comment_W
1954 static const SecPkgInfoA infoA = {
1955 CAPS,
1957 RPC_C_AUTHN_WINNT,
1958 NTLM_MAX_BUF,
1959 ntlm_name_A,
1960 ntlm_comment_A
1963 SecPkgInfoA *ntlm_package_infoA = (SecPkgInfoA *)&infoA;
1964 SecPkgInfoW *ntlm_package_infoW = (SecPkgInfoW *)&infoW;
1966 void SECUR32_initNTLMSP(void)
1968 PNegoHelper helper;
1969 static CHAR version[] = "--version";
1971 SEC_CHAR *args[] = {
1972 ntlm_auth,
1973 version,
1974 NULL };
1976 if(fork_helper(&helper, ntlm_auth, args) != SEC_E_OK)
1977 helper = NULL;
1978 else
1979 check_version(helper);
1981 if( helper &&
1982 ((helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
1983 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1984 helper->minor > MIN_NTLM_AUTH_MINOR_VERSION) ||
1985 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1986 helper->minor == MIN_NTLM_AUTH_MINOR_VERSION &&
1987 helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION)) )
1989 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1990 SECUR32_addPackages(provider, 1L, ntlm_package_infoA, ntlm_package_infoW);
1992 else
1994 ERR_(winediag)("%s was not found or is outdated. "
1995 "Make sure that ntlm_auth >= %d.%d.%d is in your path. "
1996 "Usually, you can find it in the winbind package of your distribution.\n",
1997 ntlm_auth,
1998 MIN_NTLM_AUTH_MAJOR_VERSION,
1999 MIN_NTLM_AUTH_MINOR_VERSION,
2000 MIN_NTLM_AUTH_MICRO_VERSION);
2003 cleanup_helper(helper);