comctl32: Merge imagelist definitions into the C file.
[wine/multimedia.git] / dlls / secur32 / ntlm.c
blob4c353c3cf2ed69236aa9d8bfb0e3ba82afe19e9f
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 typedef struct _NtlmCredentials
48 HelperMode mode;
50 /* these are all in the Unix codepage */
51 char *username_arg;
52 char *domain_arg;
53 char *password; /* not nul-terminated */
54 int pwlen;
55 } NtlmCredentials, *PNtlmCredentials;
57 /***********************************************************************
58 * QueryCredentialsAttributesA
60 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
61 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
63 SECURITY_STATUS ret;
65 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
67 if(ulAttribute == SECPKG_ATTR_NAMES)
69 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
70 ret = SEC_E_UNSUPPORTED_FUNCTION;
72 else
73 ret = SEC_E_UNSUPPORTED_FUNCTION;
75 return ret;
78 /***********************************************************************
79 * QueryCredentialsAttributesW
81 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
82 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
84 SECURITY_STATUS ret;
86 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
88 if(ulAttribute == SECPKG_ATTR_NAMES)
90 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
91 ret = SEC_E_UNSUPPORTED_FUNCTION;
93 else
94 ret = SEC_E_UNSUPPORTED_FUNCTION;
96 return ret;
99 static char *ntlm_GetUsernameArg(LPCWSTR userW, INT userW_length)
101 static const char username_arg[] = "--username=";
102 char *user;
103 int unixcp_size;
105 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
106 userW, userW_length, NULL, 0, NULL, NULL) + sizeof(username_arg);
107 user = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
108 if (!user) return NULL;
109 memcpy(user, username_arg, sizeof(username_arg) - 1);
110 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, userW, userW_length,
111 user + sizeof(username_arg) - 1,
112 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
113 user[unixcp_size - 1] = '\0';
114 return user;
117 static char *ntlm_GetDomainArg(LPCWSTR domainW, INT domainW_length)
119 static const char domain_arg[] = "--domain=";
120 char *domain;
121 int unixcp_size;
123 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
124 domainW, domainW_length, NULL, 0, NULL, NULL) + sizeof(domain_arg);
125 domain = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
126 if (!domain) return NULL;
127 memcpy(domain, domain_arg, sizeof(domain_arg) - 1);
128 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domainW,
129 domainW_length, domain + sizeof(domain_arg) - 1,
130 unixcp_size - sizeof(domain) + 1, NULL, NULL);
131 domain[unixcp_size - 1] = '\0';
132 return domain;
135 /***********************************************************************
136 * AcquireCredentialsHandleW
138 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
139 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
140 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
141 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
143 SECURITY_STATUS ret;
144 PNtlmCredentials ntlm_cred = NULL;
145 SEC_WCHAR *username = NULL, *domain = NULL;
147 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
148 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
149 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
151 switch(fCredentialUse)
153 case SECPKG_CRED_INBOUND:
154 ntlm_cred = HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred));
155 if (!ntlm_cred)
156 ret = SEC_E_INSUFFICIENT_MEMORY;
157 else
159 ntlm_cred->mode = NTLM_SERVER;
160 ntlm_cred->username_arg = NULL;
161 ntlm_cred->domain_arg = NULL;
162 ntlm_cred->password = NULL;
163 ntlm_cred->pwlen = 0;
164 phCredential->dwUpper = fCredentialUse;
165 phCredential->dwLower = (ULONG_PTR)ntlm_cred;
166 ret = SEC_E_OK;
168 break;
169 case SECPKG_CRED_OUTBOUND:
171 ntlm_cred = HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred));
172 if (!ntlm_cred)
174 ret = SEC_E_INSUFFICIENT_MEMORY;
175 break;
177 ntlm_cred->mode = NTLM_CLIENT;
178 ntlm_cred->username_arg = NULL;
179 ntlm_cred->domain_arg = NULL;
180 ntlm_cred->password = NULL;
181 ntlm_cred->pwlen = 0;
183 if(pAuthData != NULL)
185 PSEC_WINNT_AUTH_IDENTITY_W auth_data = pAuthData;
187 TRACE("Username is %s\n", debugstr_wn(auth_data->User, auth_data->UserLength));
188 TRACE("Domain name is %s\n", debugstr_wn(auth_data->Domain, auth_data->DomainLength));
190 ntlm_cred->username_arg = ntlm_GetUsernameArg(auth_data->User, auth_data->UserLength);
191 ntlm_cred->domain_arg = ntlm_GetDomainArg(auth_data->Domain, auth_data->DomainLength);
193 if(auth_data->PasswordLength != 0)
195 ntlm_cred->pwlen = WideCharToMultiByte(CP_UNIXCP,
196 WC_NO_BEST_FIT_CHARS, auth_data->Password,
197 auth_data->PasswordLength, NULL, 0, NULL,
198 NULL);
200 ntlm_cred->password = HeapAlloc(GetProcessHeap(), 0,
201 ntlm_cred->pwlen);
203 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
204 auth_data->Password, auth_data->PasswordLength,
205 ntlm_cred->password, ntlm_cred->pwlen, NULL, NULL);
209 phCredential->dwUpper = fCredentialUse;
210 phCredential->dwLower = (ULONG_PTR)ntlm_cred;
211 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
212 phCredential->dwUpper, phCredential->dwLower);
213 ret = SEC_E_OK;
214 break;
216 case SECPKG_CRED_BOTH:
217 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
218 ret = SEC_E_UNSUPPORTED_FUNCTION;
219 phCredential = NULL;
220 break;
221 default:
222 phCredential = NULL;
223 ret = SEC_E_UNKNOWN_CREDENTIALS;
226 HeapFree(GetProcessHeap(), 0, username);
227 HeapFree(GetProcessHeap(), 0, domain);
229 return ret;
232 /***********************************************************************
233 * AcquireCredentialsHandleA
235 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
236 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
237 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
238 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
240 SECURITY_STATUS ret;
241 int user_sizeW, domain_sizeW, passwd_sizeW;
243 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
245 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
246 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
248 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
249 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
250 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
252 if(pszPackage != NULL)
254 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
255 NULL, 0);
257 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
258 sizeof(SEC_WCHAR));
259 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
263 if(pAuthData != NULL)
265 identity = pAuthData;
267 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
269 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
270 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
272 if(identity->UserLength != 0)
274 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
275 (LPCSTR)identity->User, identity->UserLength, NULL, 0);
276 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
277 sizeof(SEC_WCHAR));
278 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
279 identity->UserLength, user, user_sizeW);
281 else
283 user_sizeW = 0;
286 if(identity->DomainLength != 0)
288 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
289 (LPCSTR)identity->Domain, identity->DomainLength, NULL, 0);
290 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
291 * sizeof(SEC_WCHAR));
292 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
293 identity->DomainLength, domain, domain_sizeW);
295 else
297 domain_sizeW = 0;
300 if(identity->PasswordLength != 0)
302 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
303 (LPCSTR)identity->Password, identity->PasswordLength,
304 NULL, 0);
305 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
306 * sizeof(SEC_WCHAR));
307 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
308 identity->PasswordLength, passwd, passwd_sizeW);
310 else
312 passwd_sizeW = 0;
315 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
316 pAuthDataW->User = user;
317 pAuthDataW->UserLength = user_sizeW;
318 pAuthDataW->Domain = domain;
319 pAuthDataW->DomainLength = domain_sizeW;
320 pAuthDataW->Password = passwd;
321 pAuthDataW->PasswordLength = passwd_sizeW;
323 else
325 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
329 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
330 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
331 ptsExpiry);
333 HeapFree(GetProcessHeap(), 0, package);
334 HeapFree(GetProcessHeap(), 0, user);
335 HeapFree(GetProcessHeap(), 0, domain);
336 HeapFree(GetProcessHeap(), 0, passwd);
337 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
338 HeapFree(GetProcessHeap(), 0, pAuthDataW);
340 return ret;
343 /*************************************************************************
344 * ntlm_GetTokenBufferIndex
345 * Calculates the index of the secbuffer with BufferType == SECBUFFER_TOKEN
346 * Returns index if found or -1 if not found.
348 static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)
350 UINT i;
352 TRACE("%p\n", pMessage);
354 for( i = 0; i < pMessage->cBuffers; ++i )
356 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
357 return i;
360 return -1;
363 /*************************************************************************
364 * ntlm_GetDataBufferIndex
365 * Calculates the index of the first secbuffer with BufferType == SECBUFFER_DATA
366 * Returns index if found or -1 if not found.
368 static int ntlm_GetDataBufferIndex(PSecBufferDesc pMessage)
370 UINT i;
372 TRACE("%p\n", pMessage);
374 for( i = 0; i < pMessage->cBuffers; ++i )
376 if(pMessage->pBuffers[i].BufferType == SECBUFFER_DATA)
377 return i;
380 return -1;
383 static BOOL ntlm_GetCachedCredential(const SEC_WCHAR *pszTargetName, PCREDENTIALW *cred)
385 LPCWSTR p;
386 LPCWSTR pszHost;
387 LPWSTR pszHostOnly;
388 BOOL ret;
390 if (!pszTargetName)
391 return FALSE;
393 /* try to get the start of the hostname from service principal name (SPN) */
394 pszHost = strchrW(pszTargetName, '/');
395 if (pszHost)
397 /* skip slash character */
398 pszHost++;
400 /* find end of host by detecting start of instance port or start of referrer */
401 p = strchrW(pszHost, ':');
402 if (!p)
403 p = strchrW(pszHost, '/');
404 if (!p)
405 p = pszHost + strlenW(pszHost);
407 else /* otherwise not an SPN, just a host */
409 pszHost = pszTargetName;
410 p = pszHost + strlenW(pszHost);
413 pszHostOnly = HeapAlloc(GetProcessHeap(), 0, (p - pszHost + 1) * sizeof(WCHAR));
414 if (!pszHostOnly)
415 return FALSE;
417 memcpy(pszHostOnly, pszHost, (p - pszHost) * sizeof(WCHAR));
418 pszHostOnly[p - pszHost] = '\0';
420 ret = CredReadW(pszHostOnly, CRED_TYPE_DOMAIN_PASSWORD, 0, cred);
422 HeapFree(GetProcessHeap(), 0, pszHostOnly);
423 return ret;
426 /***********************************************************************
427 * InitializeSecurityContextW
429 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
430 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
431 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
432 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
433 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
435 SECURITY_STATUS ret;
436 PNtlmCredentials ntlm_cred = NULL;
437 PNegoHelper helper = NULL;
438 ULONG ctxt_attr = 0;
439 char* buffer, *want_flags = NULL;
440 PBYTE bin;
441 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
442 int token_idx;
443 SEC_CHAR *username = NULL;
444 SEC_CHAR *domain = NULL;
445 SEC_CHAR *password = NULL;
447 TRACE("%p %p %s 0x%08x %d %d %p %d %p %p %p %p\n", phCredential, phContext,
448 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
449 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
451 /****************************************
452 * When communicating with the client, there can be the
453 * following reply packets:
454 * YR <base64 blob> should be sent to the server
455 * PW should be sent back to helper with
456 * base64 encoded password
457 * AF <base64 blob> client is done, blob should be
458 * sent to server with KK prefixed
459 * GF <string list> A string list of negotiated flags
460 * GK <base64 blob> base64 encoded session key
461 * BH <char reason> something broke
463 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
465 if(TargetDataRep == SECURITY_NETWORK_DREP){
466 TRACE("Setting SECURITY_NETWORK_DREP\n");
469 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
470 bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
472 if((phContext == NULL) && (pInput == NULL))
474 static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
475 static CHAR credentials_argv[] = "--use-cached-creds";
476 SEC_CHAR *client_argv[5];
477 int pwlen = 0;
479 TRACE("First time in ISC()\n");
481 if(!phCredential)
483 ret = SEC_E_INVALID_HANDLE;
484 goto isc_end;
487 /* As the server side of sspi never calls this, make sure that
488 * the handler is a client handler.
490 ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
491 if(ntlm_cred->mode != NTLM_CLIENT)
493 TRACE("Cred mode = %d\n", ntlm_cred->mode);
494 ret = SEC_E_INVALID_HANDLE;
495 goto isc_end;
498 client_argv[0] = ntlm_auth;
499 client_argv[1] = helper_protocol;
500 if (!ntlm_cred->username_arg && !ntlm_cred->domain_arg)
502 LPWKSTA_USER_INFO_1 ui = NULL;
503 NET_API_STATUS status;
504 PCREDENTIALW cred;
506 if (ntlm_GetCachedCredential(pszTargetName, &cred))
508 LPWSTR p;
509 p = strchrW(cred->UserName, '\\');
510 if (p)
512 domain = ntlm_GetDomainArg(cred->UserName, p - cred->UserName);
513 p++;
515 else
517 domain = ntlm_GetDomainArg(NULL, 0);
518 p = cred->UserName;
521 username = ntlm_GetUsernameArg(p, -1);
523 if(cred->CredentialBlobSize != 0)
525 pwlen = WideCharToMultiByte(CP_UNIXCP,
526 WC_NO_BEST_FIT_CHARS, (LPWSTR)cred->CredentialBlob,
527 cred->CredentialBlobSize / sizeof(WCHAR), NULL, 0,
528 NULL, NULL);
530 password = HeapAlloc(GetProcessHeap(), 0, pwlen);
532 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
533 (LPWSTR)cred->CredentialBlob,
534 cred->CredentialBlobSize / sizeof(WCHAR),
535 password, pwlen, NULL, NULL);
538 CredFree(cred);
540 client_argv[2] = username;
541 client_argv[3] = domain;
542 client_argv[4] = NULL;
544 else
546 status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
547 if (status != NERR_Success || ui == NULL)
549 ret = SEC_E_NO_CREDENTIALS;
550 goto isc_end;
552 username = ntlm_GetUsernameArg(ui->wkui1_username, -1);
553 NetApiBufferFree(ui);
555 TRACE("using cached credentials\n");
557 client_argv[2] = username;
558 client_argv[3] = credentials_argv;
559 client_argv[4] = NULL;
562 else
564 client_argv[2] = ntlm_cred->username_arg;
565 client_argv[3] = ntlm_cred->domain_arg;
566 client_argv[4] = NULL;
569 if((ret = fork_helper(&helper, ntlm_auth, client_argv)) != SEC_E_OK)
570 goto isc_end;
572 helper->mode = NTLM_CLIENT;
573 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
574 if (!helper->session_key)
576 cleanup_helper(helper);
577 ret = SEC_E_INSUFFICIENT_MEMORY;
578 goto isc_end;
581 /* Generate the dummy session key = MD4(MD4(password))*/
582 if(password || ntlm_cred->password)
584 SEC_WCHAR *unicode_password;
585 int passwd_lenW;
587 TRACE("Converting password to unicode.\n");
588 passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
589 password ? password : ntlm_cred->password,
590 password ? pwlen : ntlm_cred->pwlen,
591 NULL, 0);
592 unicode_password = HeapAlloc(GetProcessHeap(), 0,
593 passwd_lenW * sizeof(SEC_WCHAR));
594 MultiByteToWideChar(CP_ACP, 0, password ? password : ntlm_cred->password,
595 password ? pwlen : ntlm_cred->pwlen, unicode_password, passwd_lenW);
597 SECUR32_CreateNTLM1SessionKey((PBYTE)unicode_password,
598 passwd_lenW * sizeof(SEC_WCHAR), helper->session_key);
600 HeapFree(GetProcessHeap(), 0, unicode_password);
602 else
603 memset(helper->session_key, 0, 16);
605 /* Allocate space for a maximal string of
606 * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
607 * NTLMSSP_FEATURE_SESSION_KEY"
609 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
610 if(want_flags == NULL)
612 cleanup_helper(helper);
613 ret = SEC_E_INSUFFICIENT_MEMORY;
614 goto isc_end;
616 lstrcpyA(want_flags, "SF");
617 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
619 if(strstr(want_flags, "NTLMSSP_FEATURE_SEAL") == NULL)
620 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
622 if(fContextReq & ISC_REQ_CONNECTION)
623 ctxt_attr |= ISC_RET_CONNECTION;
624 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
625 ctxt_attr |= ISC_RET_EXTENDED_ERROR;
626 if(fContextReq & ISC_REQ_INTEGRITY)
628 if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
629 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
631 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
632 ctxt_attr |= ISC_RET_MUTUAL_AUTH;
633 if(fContextReq & ISC_REQ_REPLAY_DETECT)
635 if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
636 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
638 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
640 if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
641 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
643 if(fContextReq & ISC_REQ_STREAM)
644 FIXME("ISC_REQ_STREAM\n");
645 if(fContextReq & ISC_REQ_USE_DCE_STYLE)
646 ctxt_attr |= ISC_RET_USED_DCE_STYLE;
647 if(fContextReq & ISC_REQ_DELEGATE)
648 ctxt_attr |= ISC_RET_DELEGATE;
650 /* If no password is given, try to use cached credentials. Fall back to an empty
651 * password if this failed. */
652 if(!password && !ntlm_cred->password)
654 lstrcpynA(buffer, "OK", max_len-1);
655 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
657 cleanup_helper(helper);
658 goto isc_end;
660 /* If the helper replied with "PW", using cached credentials failed */
661 if(!strncmp(buffer, "PW", 2))
663 TRACE("Using cached credentials failed.\n");
664 lstrcpynA(buffer, "PW AA==", max_len-1);
666 else /* Just do a noop on the next run */
667 lstrcpynA(buffer, "OK", max_len-1);
669 else
671 lstrcpynA(buffer, "PW ", max_len-1);
672 if((ret = encodeBase64(password ? (unsigned char *)password : (unsigned char *)ntlm_cred->password,
673 password ? pwlen : ntlm_cred->pwlen, buffer+3,
674 max_len-3, &buffer_len)) != SEC_E_OK)
676 cleanup_helper(helper);
677 goto isc_end;
682 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
683 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
685 cleanup_helper(helper);
686 goto isc_end;
689 TRACE("Helper returned %s\n", debugstr_a(buffer));
691 if(lstrlenA(want_flags) > 2)
693 TRACE("Want flags are %s\n", debugstr_a(want_flags));
694 lstrcpynA(buffer, want_flags, max_len-1);
695 if((ret = run_helper(helper, buffer, max_len, &buffer_len))
696 != SEC_E_OK)
697 goto isc_end;
698 if(!strncmp(buffer, "BH", 2))
699 ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
702 lstrcpynA(buffer, "YR", max_len-1);
704 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
706 cleanup_helper(helper);
707 goto isc_end;
710 TRACE("%s\n", buffer);
712 if(strncmp(buffer, "YR ", 3) != 0)
714 /* Something borked */
715 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
716 ret = SEC_E_INTERNAL_ERROR;
717 cleanup_helper(helper);
718 goto isc_end;
720 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
721 max_len-1, &bin_len)) != SEC_E_OK)
723 cleanup_helper(helper);
724 goto isc_end;
727 /* put the decoded client blob into the out buffer */
729 phNewContext->dwUpper = ctxt_attr;
730 phNewContext->dwLower = (ULONG_PTR)helper;
732 ret = SEC_I_CONTINUE_NEEDED;
734 else
736 int input_token_idx;
738 /* handle second call here */
739 /* encode server data to base64 */
740 if (!pInput || ((input_token_idx = ntlm_GetTokenBufferIndex(pInput)) == -1))
742 ret = SEC_E_INVALID_TOKEN;
743 goto isc_end;
746 if(!phContext)
748 ret = SEC_E_INVALID_HANDLE;
749 goto isc_end;
752 /* As the server side of sspi never calls this, make sure that
753 * the handler is a client handler.
755 helper = (PNegoHelper)phContext->dwLower;
756 if(helper->mode != NTLM_CLIENT)
758 TRACE("Helper mode = %d\n", helper->mode);
759 ret = SEC_E_INVALID_HANDLE;
760 goto isc_end;
763 if (!pInput->pBuffers[input_token_idx].pvBuffer)
765 ret = SEC_E_INTERNAL_ERROR;
766 goto isc_end;
769 if(pInput->pBuffers[input_token_idx].cbBuffer > max_len)
771 TRACE("pInput->pBuffers[%d].cbBuffer is: %d\n",
772 input_token_idx,
773 pInput->pBuffers[input_token_idx].cbBuffer);
774 ret = SEC_E_INVALID_TOKEN;
775 goto isc_end;
777 else
778 bin_len = pInput->pBuffers[input_token_idx].cbBuffer;
780 memcpy(bin, pInput->pBuffers[input_token_idx].pvBuffer, bin_len);
782 lstrcpynA(buffer, "TT ", max_len-1);
784 if((ret = encodeBase64(bin, bin_len, buffer+3,
785 max_len-3, &buffer_len)) != SEC_E_OK)
786 goto isc_end;
788 TRACE("Server sent: %s\n", debugstr_a(buffer));
790 /* send TT base64 blob to ntlm_auth */
791 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
792 goto isc_end;
794 TRACE("Helper replied: %s\n", debugstr_a(buffer));
796 if( (strncmp(buffer, "KK ", 3) != 0) &&
797 (strncmp(buffer, "AF ", 3) !=0))
799 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
800 ret = SEC_E_INVALID_TOKEN;
801 goto isc_end;
804 /* decode the blob and send it to server */
805 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
806 &bin_len)) != SEC_E_OK)
808 goto isc_end;
811 phNewContext->dwUpper = ctxt_attr;
812 phNewContext->dwLower = (ULONG_PTR)helper;
814 ret = SEC_E_OK;
817 /* put the decoded client blob into the out buffer */
819 if (!pOutput || ((token_idx = ntlm_GetTokenBufferIndex(pOutput)) == -1))
821 TRACE("no SECBUFFER_TOKEN buffer could be found\n");
822 ret = SEC_E_BUFFER_TOO_SMALL;
823 if ((phContext == NULL) && (pInput == NULL))
825 HeapFree(GetProcessHeap(), 0, helper->session_key);
826 cleanup_helper(helper);
827 phNewContext->dwUpper = 0;
828 phNewContext->dwLower = 0;
830 goto isc_end;
833 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
835 pOutput->pBuffers[token_idx].pvBuffer = HeapAlloc(GetProcessHeap(), 0, bin_len);
836 pOutput->pBuffers[token_idx].cbBuffer = bin_len;
838 else if (pOutput->pBuffers[token_idx].cbBuffer < bin_len)
840 TRACE("out buffer is NULL or has not enough space\n");
841 ret = SEC_E_BUFFER_TOO_SMALL;
842 if ((phContext == NULL) && (pInput == NULL))
844 HeapFree(GetProcessHeap(), 0, helper->session_key);
845 cleanup_helper(helper);
846 phNewContext->dwUpper = 0;
847 phNewContext->dwLower = 0;
849 goto isc_end;
852 if (!pOutput->pBuffers[token_idx].pvBuffer)
854 TRACE("out buffer is NULL\n");
855 ret = SEC_E_INTERNAL_ERROR;
856 if ((phContext == NULL) && (pInput == NULL))
858 HeapFree(GetProcessHeap(), 0, helper->session_key);
859 cleanup_helper(helper);
860 phNewContext->dwUpper = 0;
861 phNewContext->dwLower = 0;
863 goto isc_end;
866 pOutput->pBuffers[token_idx].cbBuffer = bin_len;
867 memcpy(pOutput->pBuffers[token_idx].pvBuffer, bin, bin_len);
869 if(ret == SEC_E_OK)
871 TRACE("Getting negotiated flags\n");
872 lstrcpynA(buffer, "GF", max_len - 1);
873 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
874 goto isc_end;
876 if(buffer_len < 3)
878 TRACE("No flags negotiated.\n");
879 helper->neg_flags = 0l;
881 else
883 TRACE("Negotiated %s\n", debugstr_a(buffer));
884 sscanf(buffer + 3, "%x", &(helper->neg_flags));
885 TRACE("Stored 0x%08x as flags\n", helper->neg_flags);
888 TRACE("Getting session key\n");
889 lstrcpynA(buffer, "GK", max_len - 1);
890 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
891 goto isc_end;
893 if(strncmp(buffer, "BH", 2) == 0)
894 TRACE("No key negotiated.\n");
895 else if(strncmp(buffer, "GK ", 3) == 0)
897 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
898 &bin_len)) != SEC_E_OK)
900 TRACE("Failed to decode session key\n");
902 TRACE("Session key is %s\n", debugstr_a(buffer+3));
903 HeapFree(GetProcessHeap(), 0, helper->session_key);
904 helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
905 if(!helper->session_key)
907 TRACE("Failed to allocate memory for session key\n");
908 ret = SEC_E_INTERNAL_ERROR;
909 goto isc_end;
911 memcpy(helper->session_key, bin, bin_len);
914 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
915 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
916 helper->crypt.ntlm.seq_num = 0l;
917 SECUR32_CreateNTLM2SubKeys(helper);
918 helper->crypt.ntlm2.send_a4i = SECUR32_arc4Alloc();
919 helper->crypt.ntlm2.recv_a4i = SECUR32_arc4Alloc();
920 SECUR32_arc4Init(helper->crypt.ntlm2.send_a4i,
921 helper->crypt.ntlm2.send_seal_key, 16);
922 SECUR32_arc4Init(helper->crypt.ntlm2.recv_a4i,
923 helper->crypt.ntlm2.recv_seal_key, 16);
924 helper->crypt.ntlm2.send_seq_no = 0l;
925 helper->crypt.ntlm2.recv_seq_no = 0l;
928 isc_end:
929 HeapFree(GetProcessHeap(), 0, username);
930 HeapFree(GetProcessHeap(), 0, domain);
931 HeapFree(GetProcessHeap(), 0, password);
932 HeapFree(GetProcessHeap(), 0, want_flags);
933 HeapFree(GetProcessHeap(), 0, buffer);
934 HeapFree(GetProcessHeap(), 0, bin);
935 return ret;
938 /***********************************************************************
939 * InitializeSecurityContextA
941 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
942 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
943 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
944 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
945 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
947 SECURITY_STATUS ret;
948 SEC_WCHAR *target = NULL;
950 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
951 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
952 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
954 if(pszTargetName != NULL)
956 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
957 strlen(pszTargetName)+1, NULL, 0);
958 target = HeapAlloc(GetProcessHeap(), 0, target_size *
959 sizeof(SEC_WCHAR));
960 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
961 target, target_size);
964 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
965 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
966 phNewContext, pOutput, pfContextAttr, ptsExpiry);
968 HeapFree(GetProcessHeap(), 0, target);
969 return ret;
972 /***********************************************************************
973 * AcceptSecurityContext
975 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
976 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
977 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
978 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
980 SECURITY_STATUS ret;
981 char *buffer, *want_flags = NULL;
982 PBYTE bin;
983 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
984 ULONG ctxt_attr = 0;
985 PNegoHelper helper;
986 PNtlmCredentials ntlm_cred;
988 TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
989 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
990 ptsExpiry);
992 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
993 bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
995 if(TargetDataRep == SECURITY_NETWORK_DREP){
996 TRACE("Using SECURITY_NETWORK_DREP\n");
999 if(phContext == NULL)
1001 static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp";
1002 SEC_CHAR *server_argv[] = { ntlm_auth,
1003 server_helper_protocol,
1004 NULL };
1006 if (!phCredential)
1008 ret = SEC_E_INVALID_HANDLE;
1009 goto asc_end;
1012 ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
1014 if(ntlm_cred->mode != NTLM_SERVER)
1016 ret = SEC_E_INVALID_HANDLE;
1017 goto asc_end;
1020 /* This is the first call to AcceptSecurityHandle */
1021 if(pInput == NULL)
1023 ret = SEC_E_INCOMPLETE_MESSAGE;
1024 goto asc_end;
1027 if(pInput->cBuffers < 1)
1029 ret = SEC_E_INCOMPLETE_MESSAGE;
1030 goto asc_end;
1033 if(pInput->pBuffers[0].cbBuffer > max_len)
1035 ret = SEC_E_INVALID_TOKEN;
1036 goto asc_end;
1038 else
1039 bin_len = pInput->pBuffers[0].cbBuffer;
1041 if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
1042 SEC_E_OK)
1044 ret = SEC_E_INTERNAL_ERROR;
1045 goto asc_end;
1047 helper->mode = NTLM_SERVER;
1049 /* Handle all the flags */
1050 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
1051 if(want_flags == NULL)
1053 TRACE("Failed to allocate memory for the want_flags!\n");
1054 ret = SEC_E_INSUFFICIENT_MEMORY;
1055 cleanup_helper(helper);
1056 goto asc_end;
1058 lstrcpyA(want_flags, "SF");
1059 if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
1061 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
1063 if(fContextReq & ASC_REQ_CONFIDENTIALITY)
1065 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
1067 if(fContextReq & ASC_REQ_CONNECTION)
1069 /* This is default, so we'll enable it */
1070 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
1071 ctxt_attr |= ASC_RET_CONNECTION;
1073 if(fContextReq & ASC_REQ_EXTENDED_ERROR)
1075 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
1077 if(fContextReq & ASC_REQ_INTEGRITY)
1079 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
1081 if(fContextReq & ASC_REQ_MUTUAL_AUTH)
1083 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
1085 if(fContextReq & ASC_REQ_REPLAY_DETECT)
1087 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
1089 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
1091 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
1093 if(fContextReq & ISC_REQ_STREAM)
1095 FIXME("ASC_REQ_STREAM stub\n");
1097 /* Done with the flags */
1099 if(lstrlenA(want_flags) > 3)
1101 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
1102 lstrcpynA(buffer, want_flags, max_len - 1);
1103 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1104 SEC_E_OK)
1106 cleanup_helper(helper);
1107 goto asc_end;
1109 if(!strncmp(buffer, "BH", 2))
1110 TRACE("Helper doesn't understand new command set\n");
1113 /* This is the YR request from the client, encode to base64 */
1115 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1117 lstrcpynA(buffer, "YR ", max_len-1);
1119 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1120 &buffer_len)) != SEC_E_OK)
1122 cleanup_helper(helper);
1123 goto asc_end;
1126 TRACE("Client sent: %s\n", debugstr_a(buffer));
1128 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1129 SEC_E_OK)
1131 cleanup_helper(helper);
1132 goto asc_end;
1135 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1136 /* The expected answer is TT <base64 blob> */
1138 if(strncmp(buffer, "TT ", 3) != 0)
1140 ret = SEC_E_INTERNAL_ERROR;
1141 cleanup_helper(helper);
1142 goto asc_end;
1145 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1146 &bin_len)) != SEC_E_OK)
1148 cleanup_helper(helper);
1149 goto asc_end;
1152 /* send this to the client */
1153 if(pOutput == NULL)
1155 ret = SEC_E_INSUFFICIENT_MEMORY;
1156 cleanup_helper(helper);
1157 goto asc_end;
1160 if(pOutput->cBuffers < 1)
1162 ret = SEC_E_INSUFFICIENT_MEMORY;
1163 cleanup_helper(helper);
1164 goto asc_end;
1167 pOutput->pBuffers[0].cbBuffer = bin_len;
1168 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
1169 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
1170 ret = SEC_I_CONTINUE_NEEDED;
1173 else
1175 /* we expect a KK request from client */
1176 if(pInput == NULL)
1178 ret = SEC_E_INCOMPLETE_MESSAGE;
1179 goto asc_end;
1182 if(pInput->cBuffers < 1)
1184 ret = SEC_E_INCOMPLETE_MESSAGE;
1185 goto asc_end;
1188 if(!phContext)
1190 ret = SEC_E_INVALID_HANDLE;
1191 goto asc_end;
1194 helper = (PNegoHelper)phContext->dwLower;
1196 if(helper->mode != NTLM_SERVER)
1198 ret = SEC_E_INVALID_HANDLE;
1199 goto asc_end;
1202 if(pInput->pBuffers[0].cbBuffer > max_len)
1204 ret = SEC_E_INVALID_TOKEN;
1205 goto asc_end;
1207 else
1208 bin_len = pInput->pBuffers[0].cbBuffer;
1210 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1212 lstrcpynA(buffer, "KK ", max_len-1);
1214 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1215 &buffer_len)) != SEC_E_OK)
1217 goto asc_end;
1220 TRACE("Client sent: %s\n", debugstr_a(buffer));
1222 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1223 SEC_E_OK)
1225 goto asc_end;
1228 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1230 /* At this point, we get a NA if the user didn't authenticate, but a BH
1231 * if ntlm_auth could not connect to winbindd. Apart from running Wine
1232 * as root, there is no way to fix this for now, so just handle this as
1233 * a failed login. */
1234 if(strncmp(buffer, "AF ", 3) != 0)
1236 if(strncmp(buffer, "NA ", 3) == 0)
1238 ret = SEC_E_LOGON_DENIED;
1239 goto asc_end;
1241 else
1243 size_t ntlm_pipe_err_len = strlen("BH NT_STATUS_ACCESS_DENIED");
1245 if( (buffer_len >= ntlm_pipe_err_len) &&
1246 (strncmp(buffer, "BH NT_STATUS_ACCESS_DENIED",
1247 ntlm_pipe_err_len) == 0))
1249 TRACE("Connection to winbindd failed\n");
1250 ret = SEC_E_LOGON_DENIED;
1252 else
1253 ret = SEC_E_INTERNAL_ERROR;
1255 goto asc_end;
1258 pOutput->pBuffers[0].cbBuffer = 0;
1260 TRACE("Getting negotiated flags\n");
1261 lstrcpynA(buffer, "GF", max_len - 1);
1262 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1263 goto asc_end;
1265 if(buffer_len < 3)
1267 TRACE("No flags negotiated, or helper does not support GF command\n");
1269 else
1271 TRACE("Negotiated %s\n", debugstr_a(buffer));
1272 sscanf(buffer + 3, "%x", &(helper->neg_flags));
1273 TRACE("Stored 0x%08x as flags\n", helper->neg_flags);
1276 TRACE("Getting session key\n");
1277 lstrcpynA(buffer, "GK", max_len - 1);
1278 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1279 goto asc_end;
1281 if(buffer_len < 3)
1282 TRACE("Helper does not support GK command\n");
1283 else
1285 if(strncmp(buffer, "BH ", 3) == 0)
1287 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1288 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1289 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1290 memset(helper->session_key, 0 , 16);
1292 else if(strncmp(buffer, "GK ", 3) == 0)
1294 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1295 &bin_len)) != SEC_E_OK)
1297 TRACE("Failed to decode session key\n");
1299 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1300 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1301 if(!helper->session_key)
1303 TRACE("Failed to allocate memory for session key\n");
1304 ret = SEC_E_INTERNAL_ERROR;
1305 goto asc_end;
1307 memcpy(helper->session_key, bin, 16);
1310 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1311 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1312 helper->crypt.ntlm.seq_num = 0l;
1315 phNewContext->dwUpper = ctxt_attr;
1316 phNewContext->dwLower = (ULONG_PTR)helper;
1318 asc_end:
1319 HeapFree(GetProcessHeap(), 0, want_flags);
1320 HeapFree(GetProcessHeap(), 0, buffer);
1321 HeapFree(GetProcessHeap(), 0, bin);
1322 return ret;
1325 /***********************************************************************
1326 * CompleteAuthToken
1328 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1329 PSecBufferDesc pToken)
1331 /* We never need to call CompleteAuthToken anyway */
1332 TRACE("%p %p\n", phContext, pToken);
1333 if (!phContext)
1334 return SEC_E_INVALID_HANDLE;
1336 return SEC_E_OK;
1339 /***********************************************************************
1340 * DeleteSecurityContext
1342 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1344 PNegoHelper helper;
1346 TRACE("%p\n", phContext);
1347 if (!phContext)
1348 return SEC_E_INVALID_HANDLE;
1350 helper = (PNegoHelper)phContext->dwLower;
1352 phContext->dwUpper = 0;
1353 phContext->dwLower = 0;
1355 SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1356 HeapFree(GetProcessHeap(), 0, helper->session_key);
1357 SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1358 SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1359 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
1360 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
1361 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
1362 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
1364 cleanup_helper(helper);
1366 return SEC_E_OK;
1369 /***********************************************************************
1370 * QueryContextAttributesW
1372 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1373 ULONG ulAttribute, void *pBuffer)
1375 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1376 if (!phContext)
1377 return SEC_E_INVALID_HANDLE;
1379 switch(ulAttribute)
1381 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1382 _x(SECPKG_ATTR_ACCESS_TOKEN);
1383 _x(SECPKG_ATTR_AUTHORITY);
1384 _x(SECPKG_ATTR_DCE_INFO);
1385 case SECPKG_ATTR_FLAGS:
1387 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1388 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1390 spcf->Flags = 0;
1391 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1392 spcf->Flags |= ISC_RET_INTEGRITY;
1393 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1394 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1395 return SEC_E_OK;
1397 _x(SECPKG_ATTR_KEY_INFO);
1398 _x(SECPKG_ATTR_LIFESPAN);
1399 _x(SECPKG_ATTR_NAMES);
1400 _x(SECPKG_ATTR_NATIVE_NAMES);
1401 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1402 _x(SECPKG_ATTR_PACKAGE_INFO);
1403 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1404 _x(SECPKG_ATTR_SESSION_KEY);
1405 case SECPKG_ATTR_SIZES:
1407 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1408 spcs->cbMaxToken = NTLM_MAX_BUF;
1409 spcs->cbMaxSignature = 16;
1410 spcs->cbBlockSize = 0;
1411 spcs->cbSecurityTrailer = 16;
1412 return SEC_E_OK;
1414 _x(SECPKG_ATTR_STREAM_SIZES);
1415 _x(SECPKG_ATTR_TARGET_INFORMATION);
1416 #undef _x
1417 default:
1418 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1421 return SEC_E_UNSUPPORTED_FUNCTION;
1424 /***********************************************************************
1425 * QueryContextAttributesA
1427 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1428 ULONG ulAttribute, void *pBuffer)
1430 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1433 /***********************************************************************
1434 * ImpersonateSecurityContext
1436 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1438 SECURITY_STATUS ret;
1440 TRACE("%p\n", phContext);
1441 if (phContext)
1443 ret = SEC_E_UNSUPPORTED_FUNCTION;
1445 else
1447 ret = SEC_E_INVALID_HANDLE;
1449 return ret;
1452 /***********************************************************************
1453 * RevertSecurityContext
1455 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1457 SECURITY_STATUS ret;
1459 TRACE("%p\n", phContext);
1460 if (phContext)
1462 ret = SEC_E_UNSUPPORTED_FUNCTION;
1464 else
1466 ret = SEC_E_INVALID_HANDLE;
1468 return ret;
1471 /***********************************************************************
1472 * ntlm_CreateSignature
1473 * As both MakeSignature and VerifySignature need this, but different keys
1474 * are needed for NTLM2, the logic goes into a helper function.
1475 * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1476 * signing/encrypting and NTLM_RECV for verfying/decrypting. When encrypting,
1477 * the signature is encrypted after the message was encrypted, so
1478 * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1479 * false.
1481 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1482 int token_idx, SignDirection direction, BOOL encrypt_sig)
1484 ULONG sign_version = 1;
1485 UINT i;
1486 PBYTE sig;
1487 TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1488 encrypt_sig);
1490 sig = pMessage->pBuffers[token_idx].pvBuffer;
1492 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1493 helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1495 BYTE digest[16];
1496 BYTE seq_no[4];
1497 HMAC_MD5_CTX hmac_md5_ctx;
1499 TRACE("Signing NTLM2 style\n");
1501 if(direction == NTLM_SEND)
1503 seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
1504 seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
1505 seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1506 seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1508 ++(helper->crypt.ntlm2.send_seq_no);
1510 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1512 else
1514 seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
1515 seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
1516 seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1517 seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1519 ++(helper->crypt.ntlm2.recv_seq_no);
1521 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1524 HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1525 for( i = 0; i < pMessage->cBuffers; ++i )
1527 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1528 HMACMD5Update(&hmac_md5_ctx, pMessage->pBuffers[i].pvBuffer,
1529 pMessage->pBuffers[i].cbBuffer);
1532 HMACMD5Final(&hmac_md5_ctx, digest);
1534 if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1536 if(direction == NTLM_SEND)
1537 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1538 else
1539 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1542 /* The NTLM2 signature is the sign version */
1543 sig[ 0] = (sign_version >> 0) & 0xff;
1544 sig[ 1] = (sign_version >> 8) & 0xff;
1545 sig[ 2] = (sign_version >> 16) & 0xff;
1546 sig[ 3] = (sign_version >> 24) & 0xff;
1547 /* The first 8 bytes of the digest */
1548 memcpy(sig+4, digest, 8);
1549 /* And the sequence number */
1550 memcpy(sig+12, seq_no, 4);
1552 pMessage->pBuffers[token_idx].cbBuffer = 16;
1554 return SEC_E_OK;
1556 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1558 ULONG crc = 0U;
1559 TRACE("Signing NTLM1 style\n");
1561 for(i=0; i < pMessage->cBuffers; ++i)
1563 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1565 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1566 pMessage->pBuffers[i].cbBuffer, crc);
1570 sig[ 0] = (sign_version >> 0) & 0xff;
1571 sig[ 1] = (sign_version >> 8) & 0xff;
1572 sig[ 2] = (sign_version >> 16) & 0xff;
1573 sig[ 3] = (sign_version >> 24) & 0xff;
1574 memset(sig+4, 0, 4);
1575 sig[ 8] = (crc >> 0) & 0xff;
1576 sig[ 9] = (crc >> 8) & 0xff;
1577 sig[10] = (crc >> 16) & 0xff;
1578 sig[11] = (crc >> 24) & 0xff;
1579 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1580 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1581 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1582 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1584 ++(helper->crypt.ntlm.seq_num);
1586 if(encrypt_sig)
1587 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1588 return SEC_E_OK;
1591 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1593 TRACE("Creating a dummy signature.\n");
1594 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1595 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1596 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1597 pMessage->pBuffers[token_idx].cbBuffer = 16;
1598 return SEC_E_OK;
1601 return SEC_E_UNSUPPORTED_FUNCTION;
1604 /***********************************************************************
1605 * MakeSignature
1607 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1608 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1610 PNegoHelper helper;
1611 int token_idx;
1613 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1614 if (!phContext)
1615 return SEC_E_INVALID_HANDLE;
1617 if(fQOP)
1618 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1620 if(MessageSeqNo)
1621 FIXME("Ignoring MessageSeqNo\n");
1623 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1624 return SEC_E_INVALID_TOKEN;
1626 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1627 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1628 return SEC_E_INVALID_TOKEN;
1630 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1631 return SEC_E_BUFFER_TOO_SMALL;
1633 helper = (PNegoHelper)phContext->dwLower;
1634 TRACE("Negotiated flags are: 0x%08x\n", helper->neg_flags);
1636 return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1639 /***********************************************************************
1640 * VerifySignature
1642 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1643 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1645 PNegoHelper helper;
1646 ULONG fQOP = 0;
1647 UINT i;
1648 int token_idx;
1649 SECURITY_STATUS ret;
1650 SecBufferDesc local_desc;
1651 PSecBuffer local_buff;
1652 BYTE local_sig[16];
1654 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1655 if(!phContext)
1656 return SEC_E_INVALID_HANDLE;
1658 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1659 return SEC_E_INVALID_TOKEN;
1661 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1662 return SEC_E_INVALID_TOKEN;
1664 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1665 return SEC_E_BUFFER_TOO_SMALL;
1667 if(MessageSeqNo)
1668 FIXME("Ignoring MessageSeqNo\n");
1670 helper = (PNegoHelper)phContext->dwLower;
1671 TRACE("Negotiated flags: 0x%08x\n", helper->neg_flags);
1673 local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1675 local_desc.ulVersion = SECBUFFER_VERSION;
1676 local_desc.cBuffers = pMessage->cBuffers;
1677 local_desc.pBuffers = local_buff;
1679 for(i=0; i < pMessage->cBuffers; ++i)
1681 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1683 local_buff[i].BufferType = SECBUFFER_TOKEN;
1684 local_buff[i].cbBuffer = 16;
1685 local_buff[i].pvBuffer = local_sig;
1687 else
1689 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1690 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1691 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1695 if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1696 return ret;
1698 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1699 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1700 ret = SEC_E_MESSAGE_ALTERED;
1701 else
1702 ret = SEC_E_OK;
1704 HeapFree(GetProcessHeap(), 0, local_buff);
1705 pfQOP = &fQOP;
1707 return ret;
1711 /***********************************************************************
1712 * FreeCredentialsHandle
1714 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1715 PCredHandle phCredential)
1717 SECURITY_STATUS ret;
1719 if(phCredential){
1720 PNtlmCredentials ntlm_cred = (PNtlmCredentials) phCredential->dwLower;
1721 phCredential->dwUpper = 0;
1722 phCredential->dwLower = 0;
1723 if (ntlm_cred->password)
1724 memset(ntlm_cred->password, 0, ntlm_cred->pwlen);
1725 HeapFree(GetProcessHeap(), 0, ntlm_cred->password);
1726 HeapFree(GetProcessHeap(), 0, ntlm_cred->username_arg);
1727 HeapFree(GetProcessHeap(), 0, ntlm_cred->domain_arg);
1728 HeapFree(GetProcessHeap(), 0, ntlm_cred);
1729 ret = SEC_E_OK;
1731 else
1732 ret = SEC_E_OK;
1734 return ret;
1737 /***********************************************************************
1738 * EncryptMessage
1740 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1741 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1743 PNegoHelper helper;
1744 int token_idx, data_idx;
1746 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1748 if(!phContext)
1749 return SEC_E_INVALID_HANDLE;
1751 if(fQOP)
1752 FIXME("Ignoring fQOP\n");
1754 if(MessageSeqNo)
1755 FIXME("Ignoring MessageSeqNo\n");
1757 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1758 return SEC_E_INVALID_TOKEN;
1760 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1761 return SEC_E_INVALID_TOKEN;
1763 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1 )
1764 return SEC_E_INVALID_TOKEN;
1766 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1767 return SEC_E_BUFFER_TOO_SMALL;
1769 helper = (PNegoHelper) phContext->dwLower;
1771 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1772 helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1774 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1775 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1776 pMessage->pBuffers[data_idx].pvBuffer,
1777 pMessage->pBuffers[data_idx].cbBuffer);
1779 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1780 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1781 ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1784 return SEC_E_OK;
1786 else
1788 PBYTE sig;
1789 ULONG save_flags;
1791 /* EncryptMessage always produces real signatures, so make sure
1792 * NTLMSSP_NEGOTIATE_SIGN is set*/
1793 save_flags = helper->neg_flags;
1794 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1795 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1796 helper->neg_flags = save_flags;
1798 sig = pMessage->pBuffers[token_idx].pvBuffer;
1800 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1801 pMessage->pBuffers[data_idx].pvBuffer,
1802 pMessage->pBuffers[data_idx].cbBuffer);
1803 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1805 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1806 memset(sig+4, 0, 4);
1810 return SEC_E_OK;
1813 /***********************************************************************
1814 * DecryptMessage
1816 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1817 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1819 SECURITY_STATUS ret;
1820 ULONG ntlmssp_flags_save;
1821 PNegoHelper helper;
1822 int token_idx, data_idx;
1823 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1825 if(!phContext)
1826 return SEC_E_INVALID_HANDLE;
1828 if(MessageSeqNo)
1829 FIXME("Ignoring MessageSeqNo\n");
1831 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1832 return SEC_E_INVALID_TOKEN;
1834 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1835 return SEC_E_INVALID_TOKEN;
1837 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1)
1838 return SEC_E_INVALID_TOKEN;
1840 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1841 return SEC_E_BUFFER_TOO_SMALL;
1843 helper = (PNegoHelper) phContext->dwLower;
1845 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1847 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1848 pMessage->pBuffers[data_idx].pvBuffer,
1849 pMessage->pBuffers[data_idx].cbBuffer);
1851 else
1853 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1854 pMessage->pBuffers[data_idx].pvBuffer,
1855 pMessage->pBuffers[data_idx].cbBuffer);
1858 /* Make sure we use a session key for the signature check, EncryptMessage
1859 * always does that, even in the dummy case */
1860 ntlmssp_flags_save = helper->neg_flags;
1862 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1863 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1865 helper->neg_flags = ntlmssp_flags_save;
1867 return ret;
1870 static const SecurityFunctionTableA ntlmTableA = {
1872 NULL, /* EnumerateSecurityPackagesA */
1873 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1874 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1875 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1876 NULL, /* Reserved2 */
1877 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1878 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1879 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1880 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1881 NULL, /* ApplyControlToken */
1882 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1883 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1884 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1885 ntlm_MakeSignature, /* MakeSignature */
1886 ntlm_VerifySignature, /* VerifySignature */
1887 FreeContextBuffer, /* FreeContextBuffer */
1888 NULL, /* QuerySecurityPackageInfoA */
1889 NULL, /* Reserved3 */
1890 NULL, /* Reserved4 */
1891 NULL, /* ExportSecurityContext */
1892 NULL, /* ImportSecurityContextA */
1893 NULL, /* AddCredentialsA */
1894 NULL, /* Reserved8 */
1895 NULL, /* QuerySecurityContextToken */
1896 ntlm_EncryptMessage, /* EncryptMessage */
1897 ntlm_DecryptMessage, /* DecryptMessage */
1898 NULL, /* SetContextAttributesA */
1901 static const SecurityFunctionTableW ntlmTableW = {
1903 NULL, /* EnumerateSecurityPackagesW */
1904 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1905 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1906 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1907 NULL, /* Reserved2 */
1908 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1909 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1910 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1911 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1912 NULL, /* ApplyControlToken */
1913 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1914 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1915 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1916 ntlm_MakeSignature, /* MakeSignature */
1917 ntlm_VerifySignature, /* VerifySignature */
1918 FreeContextBuffer, /* FreeContextBuffer */
1919 NULL, /* QuerySecurityPackageInfoW */
1920 NULL, /* Reserved3 */
1921 NULL, /* Reserved4 */
1922 NULL, /* ExportSecurityContext */
1923 NULL, /* ImportSecurityContextW */
1924 NULL, /* AddCredentialsW */
1925 NULL, /* Reserved8 */
1926 NULL, /* QuerySecurityContextToken */
1927 ntlm_EncryptMessage, /* EncryptMessage */
1928 ntlm_DecryptMessage, /* DecryptMessage */
1929 NULL, /* SetContextAttributesW */
1932 #define NTLM_COMMENT \
1933 { 'N', 'T', 'L', 'M', ' ', \
1934 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1935 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1937 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1938 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1940 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1942 static char ntlm_name_A[] = NTLM_NAME;
1943 static WCHAR ntlm_name_W[] = NTLM_NAME;
1945 /* According to Windows, NTLM has the following capabilities. */
1946 #define CAPS ( \
1947 SECPKG_FLAG_INTEGRITY | \
1948 SECPKG_FLAG_PRIVACY | \
1949 SECPKG_FLAG_TOKEN_ONLY | \
1950 SECPKG_FLAG_CONNECTION | \
1951 SECPKG_FLAG_MULTI_REQUIRED | \
1952 SECPKG_FLAG_IMPERSONATION | \
1953 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1954 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1956 static const SecPkgInfoW infoW = {
1957 CAPS,
1959 RPC_C_AUTHN_WINNT,
1960 NTLM_MAX_BUF,
1961 ntlm_name_W,
1962 ntlm_comment_W
1965 static const SecPkgInfoA infoA = {
1966 CAPS,
1968 RPC_C_AUTHN_WINNT,
1969 NTLM_MAX_BUF,
1970 ntlm_name_A,
1971 ntlm_comment_A
1974 #define NEGO_COMMENT { 'M', 'i', 'c', 'r', 'o', 's', 'o', 'f', 't', ' ', \
1975 'P', 'a', 'c', 'k', 'a', 'g', 'e', ' ', \
1976 'N', 'e', 'g', 'o', 't', 'i', 'a', 't', 'o', 'r', 0}
1978 static CHAR nego_comment_A[] = NEGO_COMMENT;
1979 static WCHAR nego_comment_W[] = NEGO_COMMENT;
1981 #define NEGO_NAME {'N', 'e', 'g', 'o', 't', 'i', 'a', 't', 'e', 0}
1983 static CHAR nego_name_A[] = NEGO_NAME;
1984 static WCHAR nego_name_W[] = NEGO_NAME;
1986 #define NEGO_CAPS (\
1987 SECPKG_FLAG_INTEGRITY | \
1988 SECPKG_FLAG_PRIVACY | \
1989 SECPKG_FLAG_CONNECTION | \
1990 SECPKG_FLAG_MULTI_REQUIRED | \
1991 SECPKG_FLAG_EXTENDED_ERROR | \
1992 SECPKG_FLAG_IMPERSONATION | \
1993 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1994 SECPKG_FLAG_READONLY_WITH_CHECKSUM )
1996 /* Not used for now, just kept here for completeness sake. We need to use the
1997 * NTLM_MAX_BUF value. If the hack works, we might want to refactor the code a
1998 * bit. */
1999 #define NEGO_MAX_TOKEN 12000
2001 static const SecPkgInfoW nego_infoW = {
2002 NEGO_CAPS,
2004 RPC_C_AUTHN_GSS_NEGOTIATE,
2005 NTLM_MAX_BUF,
2006 nego_name_W,
2007 nego_comment_W
2010 static const SecPkgInfoA nego_infoA = {
2011 NEGO_CAPS,
2013 RPC_C_AUTHN_GSS_NEGOTIATE,
2014 NTLM_MAX_BUF,
2015 nego_name_A,
2016 nego_comment_A
2019 void SECUR32_initNTLMSP(void)
2021 PNegoHelper helper;
2022 static CHAR version[] = "--version";
2024 SEC_CHAR *args[] = {
2025 ntlm_auth,
2026 version,
2027 NULL };
2029 if(fork_helper(&helper, ntlm_auth, args) != SEC_E_OK)
2031 /* Cheat and allocate a helper anyway, so cleanup later will work. */
2032 helper = HeapAlloc(GetProcessHeap(),0, sizeof(NegoHelper));
2033 helper->major = helper->minor = helper->micro = -1;
2034 helper->pipe_in = helper->pipe_out = -1;
2036 else
2037 check_version(helper);
2039 if( (helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
2040 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
2041 helper->minor > MIN_NTLM_AUTH_MINOR_VERSION) ||
2042 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
2043 helper->minor == MIN_NTLM_AUTH_MINOR_VERSION &&
2044 helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION) )
2046 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
2047 SecureProvider *nego_provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
2049 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
2050 /* HACK: Also pretend this is the Negotiate provider */
2051 SECUR32_addPackages(nego_provider, 1L, &nego_infoA, &nego_infoW);
2053 else
2055 ERR_(winediag)("%s was not found or is outdated. "
2056 "Make sure that ntlm_auth >= %d.%d.%d is in your path. "
2057 "Usually, you can find it in the winbind package of your distribution.\n",
2058 ntlm_auth,
2059 MIN_NTLM_AUTH_MAJOR_VERSION,
2060 MIN_NTLM_AUTH_MINOR_VERSION,
2061 MIN_NTLM_AUTH_MICRO_VERSION);
2064 cleanup_helper(helper);