gdi32: Add a helper function to initialize Bresenham parameters for line drawing.
[wine/multimedia.git] / dlls / secur32 / ntlm.c
blobfdd88c0c6352e4434b6c027b0e9866d3f6f2e917
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_len = strlen("BH NT_STATUS_ACCESS_DENIED");
1231 if( (buffer_len >= ntlm_pipe_err_len) &&
1232 (strncmp(buffer, "BH NT_STATUS_ACCESS_DENIED",
1233 ntlm_pipe_err_len) == 0))
1235 TRACE("Connection to winbindd failed\n");
1236 ret = SEC_E_LOGON_DENIED;
1238 else
1239 ret = SEC_E_INTERNAL_ERROR;
1241 goto asc_end;
1244 pOutput->pBuffers[0].cbBuffer = 0;
1246 TRACE("Getting negotiated flags\n");
1247 lstrcpynA(buffer, "GF", max_len - 1);
1248 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1249 goto asc_end;
1251 if(buffer_len < 3)
1253 TRACE("No flags negotiated, or helper does not support GF command\n");
1255 else
1257 TRACE("Negotiated %s\n", debugstr_a(buffer));
1258 sscanf(buffer + 3, "%x", &(helper->neg_flags));
1259 TRACE("Stored 0x%08x as flags\n", helper->neg_flags);
1262 TRACE("Getting session key\n");
1263 lstrcpynA(buffer, "GK", max_len - 1);
1264 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1265 goto asc_end;
1267 if(buffer_len < 3)
1268 TRACE("Helper does not support GK command\n");
1269 else
1271 if(strncmp(buffer, "BH ", 3) == 0)
1273 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1274 HeapFree(GetProcessHeap(), 0, helper->session_key);
1275 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1276 if (!helper->session_key)
1278 ret = SEC_E_INSUFFICIENT_MEMORY;
1279 goto asc_end;
1281 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1282 memset(helper->session_key, 0 , 16);
1284 else if(strncmp(buffer, "GK ", 3) == 0)
1286 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1287 &bin_len)) != SEC_E_OK)
1289 TRACE("Failed to decode session key\n");
1291 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1292 HeapFree(GetProcessHeap(), 0, helper->session_key);
1293 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1294 if(!helper->session_key)
1296 ret = SEC_E_INSUFFICIENT_MEMORY;
1297 goto asc_end;
1299 memcpy(helper->session_key, bin, 16);
1302 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1303 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1304 helper->crypt.ntlm.seq_num = 0l;
1307 phNewContext->dwUpper = ctxt_attr;
1308 phNewContext->dwLower = (ULONG_PTR)helper;
1310 asc_end:
1311 HeapFree(GetProcessHeap(), 0, want_flags);
1312 HeapFree(GetProcessHeap(), 0, buffer);
1313 HeapFree(GetProcessHeap(), 0, bin);
1314 return ret;
1317 /***********************************************************************
1318 * CompleteAuthToken
1320 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1321 PSecBufferDesc pToken)
1323 /* We never need to call CompleteAuthToken anyway */
1324 TRACE("%p %p\n", phContext, pToken);
1325 if (!phContext)
1326 return SEC_E_INVALID_HANDLE;
1328 return SEC_E_OK;
1331 /***********************************************************************
1332 * DeleteSecurityContext
1334 SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1336 PNegoHelper helper;
1338 TRACE("%p\n", phContext);
1339 if (!phContext)
1340 return SEC_E_INVALID_HANDLE;
1342 helper = (PNegoHelper)phContext->dwLower;
1344 phContext->dwUpper = 0;
1345 phContext->dwLower = 0;
1347 SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1348 SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1349 SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1350 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
1351 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
1352 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
1353 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
1355 cleanup_helper(helper);
1357 return SEC_E_OK;
1360 /***********************************************************************
1361 * QueryContextAttributesW
1363 SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1364 ULONG ulAttribute, void *pBuffer)
1366 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1367 if (!phContext)
1368 return SEC_E_INVALID_HANDLE;
1370 switch(ulAttribute)
1372 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1373 _x(SECPKG_ATTR_ACCESS_TOKEN);
1374 _x(SECPKG_ATTR_AUTHORITY);
1375 _x(SECPKG_ATTR_DCE_INFO);
1376 case SECPKG_ATTR_FLAGS:
1378 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1379 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1381 spcf->Flags = 0;
1382 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1383 spcf->Flags |= ISC_RET_INTEGRITY;
1384 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1385 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1386 return SEC_E_OK;
1388 _x(SECPKG_ATTR_KEY_INFO);
1389 _x(SECPKG_ATTR_LIFESPAN);
1390 _x(SECPKG_ATTR_NAMES);
1391 _x(SECPKG_ATTR_NATIVE_NAMES);
1392 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1393 _x(SECPKG_ATTR_PACKAGE_INFO);
1394 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1395 _x(SECPKG_ATTR_SESSION_KEY);
1396 case SECPKG_ATTR_SIZES:
1398 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1399 spcs->cbMaxToken = NTLM_MAX_BUF;
1400 spcs->cbMaxSignature = 16;
1401 spcs->cbBlockSize = 0;
1402 spcs->cbSecurityTrailer = 16;
1403 return SEC_E_OK;
1405 _x(SECPKG_ATTR_STREAM_SIZES);
1406 _x(SECPKG_ATTR_TARGET_INFORMATION);
1407 #undef _x
1408 default:
1409 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1412 return SEC_E_UNSUPPORTED_FUNCTION;
1415 /***********************************************************************
1416 * QueryContextAttributesA
1418 SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1419 ULONG ulAttribute, void *pBuffer)
1421 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1424 /***********************************************************************
1425 * ImpersonateSecurityContext
1427 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1429 SECURITY_STATUS ret;
1431 TRACE("%p\n", phContext);
1432 if (phContext)
1434 ret = SEC_E_UNSUPPORTED_FUNCTION;
1436 else
1438 ret = SEC_E_INVALID_HANDLE;
1440 return ret;
1443 /***********************************************************************
1444 * RevertSecurityContext
1446 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1448 SECURITY_STATUS ret;
1450 TRACE("%p\n", phContext);
1451 if (phContext)
1453 ret = SEC_E_UNSUPPORTED_FUNCTION;
1455 else
1457 ret = SEC_E_INVALID_HANDLE;
1459 return ret;
1462 /***********************************************************************
1463 * ntlm_CreateSignature
1464 * As both MakeSignature and VerifySignature need this, but different keys
1465 * are needed for NTLM2, the logic goes into a helper function.
1466 * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1467 * signing/encrypting and NTLM_RECV for verfying/decrypting. When encrypting,
1468 * the signature is encrypted after the message was encrypted, so
1469 * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1470 * false.
1472 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1473 int token_idx, SignDirection direction, BOOL encrypt_sig)
1475 ULONG sign_version = 1;
1476 UINT i;
1477 PBYTE sig;
1478 TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1479 encrypt_sig);
1481 sig = pMessage->pBuffers[token_idx].pvBuffer;
1483 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1484 helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1486 BYTE digest[16];
1487 BYTE seq_no[4];
1488 HMAC_MD5_CTX hmac_md5_ctx;
1490 TRACE("Signing NTLM2 style\n");
1492 if(direction == NTLM_SEND)
1494 seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
1495 seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
1496 seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1497 seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1499 ++(helper->crypt.ntlm2.send_seq_no);
1501 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1503 else
1505 seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
1506 seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
1507 seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1508 seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1510 ++(helper->crypt.ntlm2.recv_seq_no);
1512 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1515 HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1516 for( i = 0; i < pMessage->cBuffers; ++i )
1518 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1519 HMACMD5Update(&hmac_md5_ctx, pMessage->pBuffers[i].pvBuffer,
1520 pMessage->pBuffers[i].cbBuffer);
1523 HMACMD5Final(&hmac_md5_ctx, digest);
1525 if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1527 if(direction == NTLM_SEND)
1528 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1529 else
1530 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1533 /* The NTLM2 signature is the sign version */
1534 sig[ 0] = (sign_version >> 0) & 0xff;
1535 sig[ 1] = (sign_version >> 8) & 0xff;
1536 sig[ 2] = (sign_version >> 16) & 0xff;
1537 sig[ 3] = (sign_version >> 24) & 0xff;
1538 /* The first 8 bytes of the digest */
1539 memcpy(sig+4, digest, 8);
1540 /* And the sequence number */
1541 memcpy(sig+12, seq_no, 4);
1543 pMessage->pBuffers[token_idx].cbBuffer = 16;
1545 return SEC_E_OK;
1547 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1549 ULONG crc = 0U;
1550 TRACE("Signing NTLM1 style\n");
1552 for(i=0; i < pMessage->cBuffers; ++i)
1554 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1556 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1557 pMessage->pBuffers[i].cbBuffer, crc);
1561 sig[ 0] = (sign_version >> 0) & 0xff;
1562 sig[ 1] = (sign_version >> 8) & 0xff;
1563 sig[ 2] = (sign_version >> 16) & 0xff;
1564 sig[ 3] = (sign_version >> 24) & 0xff;
1565 memset(sig+4, 0, 4);
1566 sig[ 8] = (crc >> 0) & 0xff;
1567 sig[ 9] = (crc >> 8) & 0xff;
1568 sig[10] = (crc >> 16) & 0xff;
1569 sig[11] = (crc >> 24) & 0xff;
1570 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1571 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1572 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1573 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1575 ++(helper->crypt.ntlm.seq_num);
1577 if(encrypt_sig)
1578 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1579 return SEC_E_OK;
1582 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1584 TRACE("Creating a dummy signature.\n");
1585 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1586 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1587 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1588 pMessage->pBuffers[token_idx].cbBuffer = 16;
1589 return SEC_E_OK;
1592 return SEC_E_UNSUPPORTED_FUNCTION;
1595 /***********************************************************************
1596 * MakeSignature
1598 SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext,
1599 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1601 PNegoHelper helper;
1602 int token_idx;
1604 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1605 if (!phContext)
1606 return SEC_E_INVALID_HANDLE;
1608 if(fQOP)
1609 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1611 if(MessageSeqNo)
1612 FIXME("Ignoring MessageSeqNo\n");
1614 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1615 return SEC_E_INVALID_TOKEN;
1617 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1618 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1619 return SEC_E_INVALID_TOKEN;
1621 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1622 return SEC_E_BUFFER_TOO_SMALL;
1624 helper = (PNegoHelper)phContext->dwLower;
1625 TRACE("Negotiated flags are: 0x%08x\n", helper->neg_flags);
1627 return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1630 /***********************************************************************
1631 * VerifySignature
1633 SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1634 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1636 PNegoHelper helper;
1637 ULONG fQOP = 0;
1638 UINT i;
1639 int token_idx;
1640 SECURITY_STATUS ret;
1641 SecBufferDesc local_desc;
1642 PSecBuffer local_buff;
1643 BYTE local_sig[16];
1645 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1646 if(!phContext)
1647 return SEC_E_INVALID_HANDLE;
1649 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1650 return SEC_E_INVALID_TOKEN;
1652 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1653 return SEC_E_INVALID_TOKEN;
1655 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1656 return SEC_E_BUFFER_TOO_SMALL;
1658 if(MessageSeqNo)
1659 FIXME("Ignoring MessageSeqNo\n");
1661 helper = (PNegoHelper)phContext->dwLower;
1662 TRACE("Negotiated flags: 0x%08x\n", helper->neg_flags);
1664 local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1666 local_desc.ulVersion = SECBUFFER_VERSION;
1667 local_desc.cBuffers = pMessage->cBuffers;
1668 local_desc.pBuffers = local_buff;
1670 for(i=0; i < pMessage->cBuffers; ++i)
1672 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1674 local_buff[i].BufferType = SECBUFFER_TOKEN;
1675 local_buff[i].cbBuffer = 16;
1676 local_buff[i].pvBuffer = local_sig;
1678 else
1680 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1681 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1682 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1686 if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1687 return ret;
1689 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1690 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1691 ret = SEC_E_MESSAGE_ALTERED;
1692 else
1693 ret = SEC_E_OK;
1695 HeapFree(GetProcessHeap(), 0, local_buff);
1696 pfQOP = &fQOP;
1698 return ret;
1702 /***********************************************************************
1703 * FreeCredentialsHandle
1705 SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(PCredHandle phCredential)
1707 SECURITY_STATUS ret;
1709 if(phCredential){
1710 PNtlmCredentials ntlm_cred = (PNtlmCredentials) phCredential->dwLower;
1711 phCredential->dwUpper = 0;
1712 phCredential->dwLower = 0;
1713 if (ntlm_cred->password)
1714 memset(ntlm_cred->password, 0, ntlm_cred->pwlen);
1715 HeapFree(GetProcessHeap(), 0, ntlm_cred->password);
1716 HeapFree(GetProcessHeap(), 0, ntlm_cred->username_arg);
1717 HeapFree(GetProcessHeap(), 0, ntlm_cred->domain_arg);
1718 HeapFree(GetProcessHeap(), 0, ntlm_cred);
1719 ret = SEC_E_OK;
1721 else
1722 ret = SEC_E_OK;
1724 return ret;
1727 /***********************************************************************
1728 * EncryptMessage
1730 SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1731 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1733 PNegoHelper helper;
1734 int token_idx, data_idx;
1736 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1738 if(!phContext)
1739 return SEC_E_INVALID_HANDLE;
1741 if(fQOP)
1742 FIXME("Ignoring fQOP\n");
1744 if(MessageSeqNo)
1745 FIXME("Ignoring MessageSeqNo\n");
1747 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1748 return SEC_E_INVALID_TOKEN;
1750 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1751 return SEC_E_INVALID_TOKEN;
1753 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1 )
1754 return SEC_E_INVALID_TOKEN;
1756 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1757 return SEC_E_BUFFER_TOO_SMALL;
1759 helper = (PNegoHelper) phContext->dwLower;
1761 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1762 helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1764 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1765 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1766 pMessage->pBuffers[data_idx].pvBuffer,
1767 pMessage->pBuffers[data_idx].cbBuffer);
1769 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1770 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1771 ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1773 else
1775 PBYTE sig;
1776 ULONG save_flags;
1778 /* EncryptMessage always produces real signatures, so make sure
1779 * NTLMSSP_NEGOTIATE_SIGN is set*/
1780 save_flags = helper->neg_flags;
1781 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1782 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1783 helper->neg_flags = save_flags;
1785 sig = pMessage->pBuffers[token_idx].pvBuffer;
1787 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1788 pMessage->pBuffers[data_idx].pvBuffer,
1789 pMessage->pBuffers[data_idx].cbBuffer);
1790 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1792 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1793 memset(sig+4, 0, 4);
1795 return SEC_E_OK;
1798 /***********************************************************************
1799 * DecryptMessage
1801 SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1802 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1804 SECURITY_STATUS ret;
1805 ULONG ntlmssp_flags_save;
1806 PNegoHelper helper;
1807 int token_idx, data_idx;
1808 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1810 if(!phContext)
1811 return SEC_E_INVALID_HANDLE;
1813 if(MessageSeqNo)
1814 FIXME("Ignoring MessageSeqNo\n");
1816 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1817 return SEC_E_INVALID_TOKEN;
1819 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1820 return SEC_E_INVALID_TOKEN;
1822 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1)
1823 return SEC_E_INVALID_TOKEN;
1825 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1826 return SEC_E_BUFFER_TOO_SMALL;
1828 helper = (PNegoHelper) phContext->dwLower;
1830 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1832 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1833 pMessage->pBuffers[data_idx].pvBuffer,
1834 pMessage->pBuffers[data_idx].cbBuffer);
1836 else
1838 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1839 pMessage->pBuffers[data_idx].pvBuffer,
1840 pMessage->pBuffers[data_idx].cbBuffer);
1843 /* Make sure we use a session key for the signature check, EncryptMessage
1844 * always does that, even in the dummy case */
1845 ntlmssp_flags_save = helper->neg_flags;
1847 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1848 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1850 helper->neg_flags = ntlmssp_flags_save;
1852 return ret;
1855 static const SecurityFunctionTableA ntlmTableA = {
1857 NULL, /* EnumerateSecurityPackagesA */
1858 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1859 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1860 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1861 NULL, /* Reserved2 */
1862 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1863 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1864 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1865 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1866 NULL, /* ApplyControlToken */
1867 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1868 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1869 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1870 ntlm_MakeSignature, /* MakeSignature */
1871 ntlm_VerifySignature, /* VerifySignature */
1872 FreeContextBuffer, /* FreeContextBuffer */
1873 NULL, /* QuerySecurityPackageInfoA */
1874 NULL, /* Reserved3 */
1875 NULL, /* Reserved4 */
1876 NULL, /* ExportSecurityContext */
1877 NULL, /* ImportSecurityContextA */
1878 NULL, /* AddCredentialsA */
1879 NULL, /* Reserved8 */
1880 NULL, /* QuerySecurityContextToken */
1881 ntlm_EncryptMessage, /* EncryptMessage */
1882 ntlm_DecryptMessage, /* DecryptMessage */
1883 NULL, /* SetContextAttributesA */
1886 static const SecurityFunctionTableW ntlmTableW = {
1888 NULL, /* EnumerateSecurityPackagesW */
1889 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1890 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1891 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1892 NULL, /* Reserved2 */
1893 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1894 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1895 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1896 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1897 NULL, /* ApplyControlToken */
1898 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1899 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1900 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1901 ntlm_MakeSignature, /* MakeSignature */
1902 ntlm_VerifySignature, /* VerifySignature */
1903 FreeContextBuffer, /* FreeContextBuffer */
1904 NULL, /* QuerySecurityPackageInfoW */
1905 NULL, /* Reserved3 */
1906 NULL, /* Reserved4 */
1907 NULL, /* ExportSecurityContext */
1908 NULL, /* ImportSecurityContextW */
1909 NULL, /* AddCredentialsW */
1910 NULL, /* Reserved8 */
1911 NULL, /* QuerySecurityContextToken */
1912 ntlm_EncryptMessage, /* EncryptMessage */
1913 ntlm_DecryptMessage, /* DecryptMessage */
1914 NULL, /* SetContextAttributesW */
1917 #define NTLM_COMMENT \
1918 { 'N', 'T', 'L', 'M', ' ', \
1919 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1920 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1922 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1923 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1925 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1927 static char ntlm_name_A[] = NTLM_NAME;
1928 static WCHAR ntlm_name_W[] = NTLM_NAME;
1930 /* According to Windows, NTLM has the following capabilities. */
1931 #define CAPS ( \
1932 SECPKG_FLAG_INTEGRITY | \
1933 SECPKG_FLAG_PRIVACY | \
1934 SECPKG_FLAG_TOKEN_ONLY | \
1935 SECPKG_FLAG_CONNECTION | \
1936 SECPKG_FLAG_MULTI_REQUIRED | \
1937 SECPKG_FLAG_IMPERSONATION | \
1938 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1939 SECPKG_FLAG_NEGOTIABLE | \
1940 SECPKG_FLAG_LOGON | \
1941 SECPKG_FLAG_RESTRICTED_TOKENS )
1943 static const SecPkgInfoW infoW = {
1944 CAPS,
1946 RPC_C_AUTHN_WINNT,
1947 NTLM_MAX_BUF,
1948 ntlm_name_W,
1949 ntlm_comment_W
1952 static const SecPkgInfoA infoA = {
1953 CAPS,
1955 RPC_C_AUTHN_WINNT,
1956 NTLM_MAX_BUF,
1957 ntlm_name_A,
1958 ntlm_comment_A
1961 SecPkgInfoA *ntlm_package_infoA = (SecPkgInfoA *)&infoA;
1962 SecPkgInfoW *ntlm_package_infoW = (SecPkgInfoW *)&infoW;
1964 void SECUR32_initNTLMSP(void)
1966 PNegoHelper helper;
1967 static CHAR version[] = "--version";
1969 SEC_CHAR *args[] = {
1970 ntlm_auth,
1971 version,
1972 NULL };
1974 if(fork_helper(&helper, ntlm_auth, args) != SEC_E_OK)
1975 helper = NULL;
1976 else
1977 check_version(helper);
1979 if( helper &&
1980 ((helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
1981 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1982 helper->minor > MIN_NTLM_AUTH_MINOR_VERSION) ||
1983 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1984 helper->minor == MIN_NTLM_AUTH_MINOR_VERSION &&
1985 helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION)) )
1987 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1988 SECUR32_addPackages(provider, 1L, ntlm_package_infoA, ntlm_package_infoW);
1990 else
1992 ERR_(winediag)("%s was not found or is outdated. "
1993 "Make sure that ntlm_auth >= %d.%d.%d is in your path. "
1994 "Usually, you can find it in the winbind package of your distribution.\n",
1995 ntlm_auth,
1996 MIN_NTLM_AUTH_MAJOR_VERSION,
1997 MIN_NTLM_AUTH_MINOR_VERSION,
1998 MIN_NTLM_AUTH_MICRO_VERSION);
2001 cleanup_helper(helper);