d3d8: Get rid of the format switching code in d3d8_device_CopyRects().
[wine.git] / dlls / secur32 / ntlm.c
blob0fe64edc8fff31e8a73a42c1650e221a44b04835
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 helper = (PNegoHelper)phContext->dwLower;
1176 if(helper->mode != NTLM_SERVER)
1178 ret = SEC_E_INVALID_HANDLE;
1179 goto asc_end;
1182 if(pInput->pBuffers[0].cbBuffer > max_len)
1184 ret = SEC_E_INVALID_TOKEN;
1185 goto asc_end;
1187 else
1188 bin_len = pInput->pBuffers[0].cbBuffer;
1190 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1192 lstrcpynA(buffer, "KK ", max_len-1);
1194 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1195 &buffer_len)) != SEC_E_OK)
1197 goto asc_end;
1200 TRACE("Client sent: %s\n", debugstr_a(buffer));
1202 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1203 SEC_E_OK)
1205 goto asc_end;
1208 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1210 /* At this point, we get a NA if the user didn't authenticate, but a BH
1211 * if ntlm_auth could not connect to winbindd. Apart from running Wine
1212 * as root, there is no way to fix this for now, so just handle this as
1213 * a failed login. */
1214 if(strncmp(buffer, "AF ", 3) != 0)
1216 if(strncmp(buffer, "NA ", 3) == 0)
1218 ret = SEC_E_LOGON_DENIED;
1219 goto asc_end;
1221 else
1223 size_t ntlm_pipe_err_v3_len = strlen("BH NT_STATUS_ACCESS_DENIED");
1224 size_t ntlm_pipe_err_v4_len = strlen("BH NT_STATUS_UNSUCCESSFUL");
1226 if( (buffer_len >= ntlm_pipe_err_v3_len &&
1227 strncmp(buffer, "BH NT_STATUS_ACCESS_DENIED", ntlm_pipe_err_v3_len) == 0) ||
1228 (buffer_len >= ntlm_pipe_err_v4_len &&
1229 strncmp(buffer, "BH NT_STATUS_UNSUCCESSFUL", ntlm_pipe_err_v4_len) == 0) )
1231 TRACE("Connection to winbindd failed\n");
1232 ret = SEC_E_LOGON_DENIED;
1234 else
1235 ret = SEC_E_INTERNAL_ERROR;
1237 goto asc_end;
1240 pOutput->pBuffers[0].cbBuffer = 0;
1242 TRACE("Getting negotiated flags\n");
1243 lstrcpynA(buffer, "GF", max_len - 1);
1244 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1245 goto asc_end;
1247 if(buffer_len < 3)
1249 TRACE("No flags negotiated, or helper does not support GF command\n");
1251 else
1253 TRACE("Negotiated %s\n", debugstr_a(buffer));
1254 sscanf(buffer + 3, "%x", &(helper->neg_flags));
1255 TRACE("Stored 0x%08x as flags\n", helper->neg_flags);
1258 TRACE("Getting session key\n");
1259 lstrcpynA(buffer, "GK", max_len - 1);
1260 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1261 goto asc_end;
1263 if(buffer_len < 3)
1264 TRACE("Helper does not support GK command\n");
1265 else
1267 if(strncmp(buffer, "BH ", 3) == 0)
1269 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1270 HeapFree(GetProcessHeap(), 0, helper->session_key);
1271 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1272 if (!helper->session_key)
1274 ret = SEC_E_INSUFFICIENT_MEMORY;
1275 goto asc_end;
1277 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1278 memset(helper->session_key, 0 , 16);
1280 else if(strncmp(buffer, "GK ", 3) == 0)
1282 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1283 &bin_len)) != SEC_E_OK)
1285 TRACE("Failed to decode session key\n");
1287 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1288 HeapFree(GetProcessHeap(), 0, helper->session_key);
1289 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1290 if(!helper->session_key)
1292 ret = SEC_E_INSUFFICIENT_MEMORY;
1293 goto asc_end;
1295 memcpy(helper->session_key, bin, 16);
1298 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1299 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1300 helper->crypt.ntlm.seq_num = 0l;
1303 phNewContext->dwUpper = ctxt_attr;
1304 phNewContext->dwLower = (ULONG_PTR)helper;
1306 asc_end:
1307 HeapFree(GetProcessHeap(), 0, want_flags);
1308 HeapFree(GetProcessHeap(), 0, buffer);
1309 HeapFree(GetProcessHeap(), 0, bin);
1310 return ret;
1313 /***********************************************************************
1314 * CompleteAuthToken
1316 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1317 PSecBufferDesc pToken)
1319 /* We never need to call CompleteAuthToken anyway */
1320 TRACE("%p %p\n", phContext, pToken);
1321 if (!phContext)
1322 return SEC_E_INVALID_HANDLE;
1324 return SEC_E_OK;
1327 /***********************************************************************
1328 * DeleteSecurityContext
1330 SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1332 PNegoHelper helper;
1334 TRACE("%p\n", phContext);
1335 if (!phContext)
1336 return SEC_E_INVALID_HANDLE;
1338 helper = (PNegoHelper)phContext->dwLower;
1340 phContext->dwUpper = 0;
1341 phContext->dwLower = 0;
1343 SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1344 SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1345 SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1346 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
1347 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
1348 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
1349 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
1351 cleanup_helper(helper);
1353 return SEC_E_OK;
1356 /***********************************************************************
1357 * QueryContextAttributesW
1359 SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1360 ULONG ulAttribute, void *pBuffer)
1362 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1363 if (!phContext)
1364 return SEC_E_INVALID_HANDLE;
1366 switch(ulAttribute)
1368 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1369 _x(SECPKG_ATTR_ACCESS_TOKEN);
1370 _x(SECPKG_ATTR_AUTHORITY);
1371 _x(SECPKG_ATTR_DCE_INFO);
1372 case SECPKG_ATTR_FLAGS:
1374 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1375 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1377 spcf->Flags = 0;
1378 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1379 spcf->Flags |= ISC_RET_INTEGRITY;
1380 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1381 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1382 return SEC_E_OK;
1384 _x(SECPKG_ATTR_KEY_INFO);
1385 _x(SECPKG_ATTR_LIFESPAN);
1386 _x(SECPKG_ATTR_NAMES);
1387 _x(SECPKG_ATTR_NATIVE_NAMES);
1388 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1389 _x(SECPKG_ATTR_PACKAGE_INFO);
1390 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1391 _x(SECPKG_ATTR_SESSION_KEY);
1392 case SECPKG_ATTR_SIZES:
1394 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1395 spcs->cbMaxToken = NTLM_MAX_BUF;
1396 spcs->cbMaxSignature = 16;
1397 spcs->cbBlockSize = 0;
1398 spcs->cbSecurityTrailer = 16;
1399 return SEC_E_OK;
1401 _x(SECPKG_ATTR_STREAM_SIZES);
1402 _x(SECPKG_ATTR_TARGET_INFORMATION);
1403 #undef _x
1404 default:
1405 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1408 return SEC_E_UNSUPPORTED_FUNCTION;
1411 /***********************************************************************
1412 * QueryContextAttributesA
1414 SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1415 ULONG ulAttribute, void *pBuffer)
1417 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1420 /***********************************************************************
1421 * ImpersonateSecurityContext
1423 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1425 SECURITY_STATUS ret;
1427 TRACE("%p\n", phContext);
1428 if (phContext)
1430 ret = SEC_E_UNSUPPORTED_FUNCTION;
1432 else
1434 ret = SEC_E_INVALID_HANDLE;
1436 return ret;
1439 /***********************************************************************
1440 * RevertSecurityContext
1442 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1444 SECURITY_STATUS ret;
1446 TRACE("%p\n", phContext);
1447 if (phContext)
1449 ret = SEC_E_UNSUPPORTED_FUNCTION;
1451 else
1453 ret = SEC_E_INVALID_HANDLE;
1455 return ret;
1458 /***********************************************************************
1459 * ntlm_CreateSignature
1460 * As both MakeSignature and VerifySignature need this, but different keys
1461 * are needed for NTLM2, the logic goes into a helper function.
1462 * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1463 * signing/encrypting and NTLM_RECV for verfying/decrypting. When encrypting,
1464 * the signature is encrypted after the message was encrypted, so
1465 * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1466 * false.
1468 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1469 int token_idx, SignDirection direction, BOOL encrypt_sig)
1471 ULONG sign_version = 1;
1472 UINT i;
1473 PBYTE sig;
1474 TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1475 encrypt_sig);
1477 sig = pMessage->pBuffers[token_idx].pvBuffer;
1479 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1480 helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1482 BYTE digest[16];
1483 BYTE seq_no[4];
1484 HMAC_MD5_CTX hmac_md5_ctx;
1486 TRACE("Signing NTLM2 style\n");
1488 if(direction == NTLM_SEND)
1490 seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
1491 seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
1492 seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1493 seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1495 ++(helper->crypt.ntlm2.send_seq_no);
1497 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1499 else
1501 seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
1502 seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
1503 seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1504 seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1506 ++(helper->crypt.ntlm2.recv_seq_no);
1508 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1511 HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1512 for( i = 0; i < pMessage->cBuffers; ++i )
1514 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1515 HMACMD5Update(&hmac_md5_ctx, pMessage->pBuffers[i].pvBuffer,
1516 pMessage->pBuffers[i].cbBuffer);
1519 HMACMD5Final(&hmac_md5_ctx, digest);
1521 if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1523 if(direction == NTLM_SEND)
1524 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1525 else
1526 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1529 /* The NTLM2 signature is the sign version */
1530 sig[ 0] = (sign_version >> 0) & 0xff;
1531 sig[ 1] = (sign_version >> 8) & 0xff;
1532 sig[ 2] = (sign_version >> 16) & 0xff;
1533 sig[ 3] = (sign_version >> 24) & 0xff;
1534 /* The first 8 bytes of the digest */
1535 memcpy(sig+4, digest, 8);
1536 /* And the sequence number */
1537 memcpy(sig+12, seq_no, 4);
1539 pMessage->pBuffers[token_idx].cbBuffer = 16;
1541 return SEC_E_OK;
1543 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1545 ULONG crc = 0U;
1546 TRACE("Signing NTLM1 style\n");
1548 for(i=0; i < pMessage->cBuffers; ++i)
1550 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1552 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1553 pMessage->pBuffers[i].cbBuffer, crc);
1557 sig[ 0] = (sign_version >> 0) & 0xff;
1558 sig[ 1] = (sign_version >> 8) & 0xff;
1559 sig[ 2] = (sign_version >> 16) & 0xff;
1560 sig[ 3] = (sign_version >> 24) & 0xff;
1561 memset(sig+4, 0, 4);
1562 sig[ 8] = (crc >> 0) & 0xff;
1563 sig[ 9] = (crc >> 8) & 0xff;
1564 sig[10] = (crc >> 16) & 0xff;
1565 sig[11] = (crc >> 24) & 0xff;
1566 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1567 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1568 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1569 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1571 ++(helper->crypt.ntlm.seq_num);
1573 if(encrypt_sig)
1574 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1575 return SEC_E_OK;
1578 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1580 TRACE("Creating a dummy signature.\n");
1581 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1582 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1583 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1584 pMessage->pBuffers[token_idx].cbBuffer = 16;
1585 return SEC_E_OK;
1588 return SEC_E_UNSUPPORTED_FUNCTION;
1591 /***********************************************************************
1592 * MakeSignature
1594 SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext,
1595 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1597 PNegoHelper helper;
1598 int token_idx;
1600 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1601 if (!phContext)
1602 return SEC_E_INVALID_HANDLE;
1604 if(fQOP)
1605 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1607 if(MessageSeqNo)
1608 FIXME("Ignoring MessageSeqNo\n");
1610 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1611 return SEC_E_INVALID_TOKEN;
1613 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1614 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1615 return SEC_E_INVALID_TOKEN;
1617 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1618 return SEC_E_BUFFER_TOO_SMALL;
1620 helper = (PNegoHelper)phContext->dwLower;
1621 TRACE("Negotiated flags are: 0x%08x\n", helper->neg_flags);
1623 return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1626 /***********************************************************************
1627 * VerifySignature
1629 SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1630 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1632 PNegoHelper helper;
1633 ULONG fQOP = 0;
1634 UINT i;
1635 int token_idx;
1636 SECURITY_STATUS ret;
1637 SecBufferDesc local_desc;
1638 PSecBuffer local_buff;
1639 BYTE local_sig[16];
1641 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1642 if(!phContext)
1643 return SEC_E_INVALID_HANDLE;
1645 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1646 return SEC_E_INVALID_TOKEN;
1648 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1649 return SEC_E_INVALID_TOKEN;
1651 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1652 return SEC_E_BUFFER_TOO_SMALL;
1654 if(MessageSeqNo)
1655 FIXME("Ignoring MessageSeqNo\n");
1657 helper = (PNegoHelper)phContext->dwLower;
1658 TRACE("Negotiated flags: 0x%08x\n", helper->neg_flags);
1660 local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1662 local_desc.ulVersion = SECBUFFER_VERSION;
1663 local_desc.cBuffers = pMessage->cBuffers;
1664 local_desc.pBuffers = local_buff;
1666 for(i=0; i < pMessage->cBuffers; ++i)
1668 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1670 local_buff[i].BufferType = SECBUFFER_TOKEN;
1671 local_buff[i].cbBuffer = 16;
1672 local_buff[i].pvBuffer = local_sig;
1674 else
1676 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1677 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1678 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1682 if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1683 return ret;
1685 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1686 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1687 ret = SEC_E_MESSAGE_ALTERED;
1688 else
1689 ret = SEC_E_OK;
1691 HeapFree(GetProcessHeap(), 0, local_buff);
1692 pfQOP = &fQOP;
1694 return ret;
1698 /***********************************************************************
1699 * FreeCredentialsHandle
1701 SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(PCredHandle phCredential)
1703 SECURITY_STATUS ret;
1705 if(phCredential){
1706 PNtlmCredentials ntlm_cred = (PNtlmCredentials) phCredential->dwLower;
1707 phCredential->dwUpper = 0;
1708 phCredential->dwLower = 0;
1709 if (ntlm_cred->password)
1710 memset(ntlm_cred->password, 0, ntlm_cred->pwlen);
1711 HeapFree(GetProcessHeap(), 0, ntlm_cred->password);
1712 HeapFree(GetProcessHeap(), 0, ntlm_cred->username_arg);
1713 HeapFree(GetProcessHeap(), 0, ntlm_cred->domain_arg);
1714 HeapFree(GetProcessHeap(), 0, ntlm_cred);
1715 ret = SEC_E_OK;
1717 else
1718 ret = SEC_E_OK;
1720 return ret;
1723 /***********************************************************************
1724 * EncryptMessage
1726 SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1727 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1729 PNegoHelper helper;
1730 int token_idx, data_idx;
1732 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1734 if(!phContext)
1735 return SEC_E_INVALID_HANDLE;
1737 if(fQOP)
1738 FIXME("Ignoring fQOP\n");
1740 if(MessageSeqNo)
1741 FIXME("Ignoring MessageSeqNo\n");
1743 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1744 return SEC_E_INVALID_TOKEN;
1746 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1747 return SEC_E_INVALID_TOKEN;
1749 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1 )
1750 return SEC_E_INVALID_TOKEN;
1752 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1753 return SEC_E_BUFFER_TOO_SMALL;
1755 helper = (PNegoHelper) phContext->dwLower;
1757 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1758 helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1760 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1761 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1762 pMessage->pBuffers[data_idx].pvBuffer,
1763 pMessage->pBuffers[data_idx].cbBuffer);
1765 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1766 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1767 ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1769 else
1771 PBYTE sig;
1772 ULONG save_flags;
1774 /* EncryptMessage always produces real signatures, so make sure
1775 * NTLMSSP_NEGOTIATE_SIGN is set*/
1776 save_flags = helper->neg_flags;
1777 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1778 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1779 helper->neg_flags = save_flags;
1781 sig = pMessage->pBuffers[token_idx].pvBuffer;
1783 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1784 pMessage->pBuffers[data_idx].pvBuffer,
1785 pMessage->pBuffers[data_idx].cbBuffer);
1786 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1788 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1789 memset(sig+4, 0, 4);
1791 return SEC_E_OK;
1794 /***********************************************************************
1795 * DecryptMessage
1797 SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1798 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1800 SECURITY_STATUS ret;
1801 ULONG ntlmssp_flags_save;
1802 PNegoHelper helper;
1803 int token_idx, data_idx;
1804 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1806 if(!phContext)
1807 return SEC_E_INVALID_HANDLE;
1809 if(MessageSeqNo)
1810 FIXME("Ignoring MessageSeqNo\n");
1812 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1813 return SEC_E_INVALID_TOKEN;
1815 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1816 return SEC_E_INVALID_TOKEN;
1818 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1)
1819 return SEC_E_INVALID_TOKEN;
1821 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1822 return SEC_E_BUFFER_TOO_SMALL;
1824 helper = (PNegoHelper) phContext->dwLower;
1826 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1828 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1829 pMessage->pBuffers[data_idx].pvBuffer,
1830 pMessage->pBuffers[data_idx].cbBuffer);
1832 else
1834 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1835 pMessage->pBuffers[data_idx].pvBuffer,
1836 pMessage->pBuffers[data_idx].cbBuffer);
1839 /* Make sure we use a session key for the signature check, EncryptMessage
1840 * always does that, even in the dummy case */
1841 ntlmssp_flags_save = helper->neg_flags;
1843 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1844 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1846 helper->neg_flags = ntlmssp_flags_save;
1848 return ret;
1851 static const SecurityFunctionTableA ntlmTableA = {
1853 NULL, /* EnumerateSecurityPackagesA */
1854 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1855 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1856 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1857 NULL, /* Reserved2 */
1858 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1859 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1860 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1861 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1862 NULL, /* ApplyControlToken */
1863 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1864 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1865 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1866 ntlm_MakeSignature, /* MakeSignature */
1867 ntlm_VerifySignature, /* VerifySignature */
1868 FreeContextBuffer, /* FreeContextBuffer */
1869 NULL, /* QuerySecurityPackageInfoA */
1870 NULL, /* Reserved3 */
1871 NULL, /* Reserved4 */
1872 NULL, /* ExportSecurityContext */
1873 NULL, /* ImportSecurityContextA */
1874 NULL, /* AddCredentialsA */
1875 NULL, /* Reserved8 */
1876 NULL, /* QuerySecurityContextToken */
1877 ntlm_EncryptMessage, /* EncryptMessage */
1878 ntlm_DecryptMessage, /* DecryptMessage */
1879 NULL, /* SetContextAttributesA */
1882 static const SecurityFunctionTableW ntlmTableW = {
1884 NULL, /* EnumerateSecurityPackagesW */
1885 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1886 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1887 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1888 NULL, /* Reserved2 */
1889 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1890 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1891 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1892 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1893 NULL, /* ApplyControlToken */
1894 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1895 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1896 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1897 ntlm_MakeSignature, /* MakeSignature */
1898 ntlm_VerifySignature, /* VerifySignature */
1899 FreeContextBuffer, /* FreeContextBuffer */
1900 NULL, /* QuerySecurityPackageInfoW */
1901 NULL, /* Reserved3 */
1902 NULL, /* Reserved4 */
1903 NULL, /* ExportSecurityContext */
1904 NULL, /* ImportSecurityContextW */
1905 NULL, /* AddCredentialsW */
1906 NULL, /* Reserved8 */
1907 NULL, /* QuerySecurityContextToken */
1908 ntlm_EncryptMessage, /* EncryptMessage */
1909 ntlm_DecryptMessage, /* DecryptMessage */
1910 NULL, /* SetContextAttributesW */
1913 #define NTLM_COMMENT \
1914 { 'N', 'T', 'L', 'M', ' ', \
1915 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1916 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1918 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1919 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1921 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1923 static char ntlm_name_A[] = NTLM_NAME;
1924 static WCHAR ntlm_name_W[] = NTLM_NAME;
1926 /* According to Windows, NTLM has the following capabilities. */
1927 #define CAPS ( \
1928 SECPKG_FLAG_INTEGRITY | \
1929 SECPKG_FLAG_PRIVACY | \
1930 SECPKG_FLAG_TOKEN_ONLY | \
1931 SECPKG_FLAG_CONNECTION | \
1932 SECPKG_FLAG_MULTI_REQUIRED | \
1933 SECPKG_FLAG_IMPERSONATION | \
1934 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1935 SECPKG_FLAG_NEGOTIABLE | \
1936 SECPKG_FLAG_LOGON | \
1937 SECPKG_FLAG_RESTRICTED_TOKENS )
1939 static const SecPkgInfoW infoW = {
1940 CAPS,
1942 RPC_C_AUTHN_WINNT,
1943 NTLM_MAX_BUF,
1944 ntlm_name_W,
1945 ntlm_comment_W
1948 static const SecPkgInfoA infoA = {
1949 CAPS,
1951 RPC_C_AUTHN_WINNT,
1952 NTLM_MAX_BUF,
1953 ntlm_name_A,
1954 ntlm_comment_A
1957 SecPkgInfoA *ntlm_package_infoA = (SecPkgInfoA *)&infoA;
1958 SecPkgInfoW *ntlm_package_infoW = (SecPkgInfoW *)&infoW;
1960 void SECUR32_initNTLMSP(void)
1962 PNegoHelper helper;
1963 static CHAR version[] = "--version";
1965 SEC_CHAR *args[] = {
1966 ntlm_auth,
1967 version,
1968 NULL };
1970 if(fork_helper(&helper, ntlm_auth, args) != SEC_E_OK)
1971 helper = NULL;
1972 else
1973 check_version(helper);
1975 if( helper &&
1976 ((helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
1977 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1978 helper->minor > MIN_NTLM_AUTH_MINOR_VERSION) ||
1979 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1980 helper->minor == MIN_NTLM_AUTH_MINOR_VERSION &&
1981 helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION)) )
1983 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1984 SECUR32_addPackages(provider, 1L, ntlm_package_infoA, ntlm_package_infoW);
1986 else
1988 ERR_(winediag)("%s was not found or is outdated. "
1989 "Make sure that ntlm_auth >= %d.%d.%d is in your path. "
1990 "Usually, you can find it in the winbind package of your distribution.\n",
1991 ntlm_auth,
1992 MIN_NTLM_AUTH_MAJOR_VERSION,
1993 MIN_NTLM_AUTH_MINOR_VERSION,
1994 MIN_NTLM_AUTH_MICRO_VERSION);
1997 cleanup_helper(helper);