msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / secur32 / ntlm.c
blob38c5134f35bdf4336da36a0ba863431186dc6045
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);
38 #define NTLM_MAX_BUF 1904
39 #define MIN_NTLM_AUTH_MAJOR_VERSION 3
40 #define MIN_NTLM_AUTH_MINOR_VERSION 0
41 #define MIN_NTLM_AUTH_MICRO_VERSION 25
43 static CHAR ntlm_auth[] = "ntlm_auth";
45 typedef struct _NtlmCredentials
47 HelperMode mode;
49 /* these are all in the Unix codepage */
50 char *username_arg;
51 char *domain_arg;
52 char *password; /* not nul-terminated */
53 int pwlen;
54 } NtlmCredentials, *PNtlmCredentials;
56 /***********************************************************************
57 * QueryCredentialsAttributesA
59 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
60 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
62 SECURITY_STATUS ret;
64 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
66 if(ulAttribute == SECPKG_ATTR_NAMES)
68 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
69 ret = SEC_E_UNSUPPORTED_FUNCTION;
71 else
72 ret = SEC_E_UNSUPPORTED_FUNCTION;
74 return ret;
77 /***********************************************************************
78 * QueryCredentialsAttributesW
80 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
81 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
83 SECURITY_STATUS ret;
85 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
87 if(ulAttribute == SECPKG_ATTR_NAMES)
89 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
90 ret = SEC_E_UNSUPPORTED_FUNCTION;
92 else
93 ret = SEC_E_UNSUPPORTED_FUNCTION;
95 return ret;
98 static char *ntlm_GetUsernameArg(LPCWSTR userW, INT userW_length)
100 static const char username_arg[] = "--username=";
101 char *user;
102 int unixcp_size;
104 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
105 userW, userW_length, NULL, 0, NULL, NULL) + sizeof(username_arg);
106 user = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
107 if (!user) return NULL;
108 memcpy(user, username_arg, sizeof(username_arg) - 1);
109 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, userW, userW_length,
110 user + sizeof(username_arg) - 1,
111 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
112 user[unixcp_size - 1] = '\0';
113 return user;
116 static char *ntlm_GetDomainArg(LPCWSTR domainW, INT domainW_length)
118 static const char domain_arg[] = "--domain=";
119 char *domain;
120 int unixcp_size;
122 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
123 domainW, domainW_length, NULL, 0, NULL, NULL) + sizeof(domain_arg);
124 domain = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
125 if (!domain) return NULL;
126 memcpy(domain, domain_arg, sizeof(domain_arg) - 1);
127 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domainW,
128 domainW_length, domain + sizeof(domain_arg) - 1,
129 unixcp_size - sizeof(domain) + 1, NULL, NULL);
130 domain[unixcp_size - 1] = '\0';
131 return domain;
134 /***********************************************************************
135 * AcquireCredentialsHandleW
137 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
138 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
139 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
140 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
142 SECURITY_STATUS ret;
143 PNtlmCredentials ntlm_cred = NULL;
144 SEC_WCHAR *username = NULL, *domain = NULL;
146 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
147 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
148 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
150 switch(fCredentialUse)
152 case SECPKG_CRED_INBOUND:
153 ntlm_cred = HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred));
154 if (!ntlm_cred)
155 ret = SEC_E_INSUFFICIENT_MEMORY;
156 else
158 ntlm_cred->mode = NTLM_SERVER;
159 ntlm_cred->username_arg = NULL;
160 ntlm_cred->domain_arg = NULL;
161 ntlm_cred->password = NULL;
162 ntlm_cred->pwlen = 0;
163 phCredential->dwUpper = fCredentialUse;
164 phCredential->dwLower = (ULONG_PTR)ntlm_cred;
165 ret = SEC_E_OK;
167 break;
168 case SECPKG_CRED_OUTBOUND:
170 ntlm_cred = HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred));
171 if (!ntlm_cred)
173 ret = SEC_E_INSUFFICIENT_MEMORY;
174 break;
176 ntlm_cred->mode = NTLM_CLIENT;
177 ntlm_cred->username_arg = NULL;
178 ntlm_cred->domain_arg = NULL;
179 ntlm_cred->password = NULL;
180 ntlm_cred->pwlen = 0;
182 if(pAuthData != NULL)
184 PSEC_WINNT_AUTH_IDENTITY_W auth_data = pAuthData;
186 TRACE("Username is %s\n", debugstr_wn(auth_data->User, auth_data->UserLength));
187 TRACE("Domain name is %s\n", debugstr_wn(auth_data->Domain, auth_data->DomainLength));
189 ntlm_cred->username_arg = ntlm_GetUsernameArg(auth_data->User, auth_data->UserLength);
190 ntlm_cred->domain_arg = ntlm_GetDomainArg(auth_data->Domain, auth_data->DomainLength);
192 if(auth_data->PasswordLength != 0)
194 ntlm_cred->pwlen = WideCharToMultiByte(CP_UNIXCP,
195 WC_NO_BEST_FIT_CHARS, auth_data->Password,
196 auth_data->PasswordLength, NULL, 0, NULL,
197 NULL);
199 ntlm_cred->password = HeapAlloc(GetProcessHeap(), 0,
200 ntlm_cred->pwlen);
202 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
203 auth_data->Password, auth_data->PasswordLength,
204 ntlm_cred->password, ntlm_cred->pwlen, NULL, NULL);
208 phCredential->dwUpper = fCredentialUse;
209 phCredential->dwLower = (ULONG_PTR)ntlm_cred;
210 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
211 phCredential->dwUpper, phCredential->dwLower);
212 ret = SEC_E_OK;
213 break;
215 case SECPKG_CRED_BOTH:
216 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
217 ret = SEC_E_UNSUPPORTED_FUNCTION;
218 phCredential = NULL;
219 break;
220 default:
221 phCredential = NULL;
222 ret = SEC_E_UNKNOWN_CREDENTIALS;
225 HeapFree(GetProcessHeap(), 0, username);
226 HeapFree(GetProcessHeap(), 0, domain);
228 return ret;
231 /***********************************************************************
232 * AcquireCredentialsHandleA
234 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
235 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
236 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
237 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
239 SECURITY_STATUS ret;
240 int user_sizeW, domain_sizeW, passwd_sizeW;
242 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
244 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
245 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
247 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
248 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
249 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
251 if(pszPackage != NULL)
253 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
254 NULL, 0);
256 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
257 sizeof(SEC_WCHAR));
258 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
262 if(pAuthData != NULL)
264 identity = pAuthData;
266 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
268 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
269 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
271 if(identity->UserLength != 0)
273 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
274 (LPCSTR)identity->User, identity->UserLength, NULL, 0);
275 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
276 sizeof(SEC_WCHAR));
277 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
278 identity->UserLength, user, user_sizeW);
280 else
282 user_sizeW = 0;
285 if(identity->DomainLength != 0)
287 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
288 (LPCSTR)identity->Domain, identity->DomainLength, NULL, 0);
289 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
290 * sizeof(SEC_WCHAR));
291 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
292 identity->DomainLength, domain, domain_sizeW);
294 else
296 domain_sizeW = 0;
299 if(identity->PasswordLength != 0)
301 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
302 (LPCSTR)identity->Password, identity->PasswordLength,
303 NULL, 0);
304 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
305 * sizeof(SEC_WCHAR));
306 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
307 identity->PasswordLength, passwd, passwd_sizeW);
309 else
311 passwd_sizeW = 0;
314 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
315 pAuthDataW->User = user;
316 pAuthDataW->UserLength = user_sizeW;
317 pAuthDataW->Domain = domain;
318 pAuthDataW->DomainLength = domain_sizeW;
319 pAuthDataW->Password = passwd;
320 pAuthDataW->PasswordLength = passwd_sizeW;
322 else
324 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
328 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
329 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
330 ptsExpiry);
332 HeapFree(GetProcessHeap(), 0, package);
333 HeapFree(GetProcessHeap(), 0, user);
334 HeapFree(GetProcessHeap(), 0, domain);
335 HeapFree(GetProcessHeap(), 0, passwd);
336 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
337 HeapFree(GetProcessHeap(), 0, pAuthDataW);
339 return ret;
342 /*************************************************************************
343 * ntlm_GetTokenBufferIndex
344 * Calculates the index of the secbuffer with BufferType == SECBUFFER_TOKEN
345 * Returns index if found or -1 if not found.
347 static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)
349 UINT i;
351 TRACE("%p\n", pMessage);
353 for( i = 0; i < pMessage->cBuffers; ++i )
355 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
356 return i;
359 return -1;
362 /*************************************************************************
363 * ntlm_GetDataBufferIndex
364 * Calculates the index of the first secbuffer with BufferType == SECBUFFER_DATA
365 * Returns index if found or -1 if not found.
367 static int ntlm_GetDataBufferIndex(PSecBufferDesc pMessage)
369 UINT i;
371 TRACE("%p\n", pMessage);
373 for( i = 0; i < pMessage->cBuffers; ++i )
375 if(pMessage->pBuffers[i].BufferType == SECBUFFER_DATA)
376 return i;
379 return -1;
382 static BOOL ntlm_GetCachedCredential(const SEC_WCHAR *pszTargetName, PCREDENTIALW *cred)
384 LPCWSTR p;
385 LPCWSTR pszHost;
386 LPWSTR pszHostOnly;
387 BOOL ret;
389 if (!pszTargetName)
390 return FALSE;
392 /* try to get the start of the hostname from service principal name (SPN) */
393 pszHost = strchrW(pszTargetName, '/');
394 if (pszHost)
396 /* skip slash character */
397 pszHost++;
399 /* find end of host by detecting start of instance port or start of referrer */
400 p = strchrW(pszHost, ':');
401 if (!p)
402 p = strchrW(pszHost, '/');
403 if (!p)
404 p = pszHost + strlenW(pszHost);
406 else /* otherwise not an SPN, just a host */
408 pszHost = pszTargetName;
409 p = pszHost + strlenW(pszHost);
412 pszHostOnly = HeapAlloc(GetProcessHeap(), 0, (p - pszHost + 1) * sizeof(WCHAR));
413 if (!pszHostOnly)
414 return FALSE;
416 memcpy(pszHostOnly, pszHost, (p - pszHost) * sizeof(WCHAR));
417 pszHostOnly[p - pszHost] = '\0';
419 ret = CredReadW(pszHostOnly, CRED_TYPE_DOMAIN_PASSWORD, 0, cred);
421 HeapFree(GetProcessHeap(), 0, pszHostOnly);
422 return ret;
425 /***********************************************************************
426 * InitializeSecurityContextW
428 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
429 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
430 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
431 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
432 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
434 SECURITY_STATUS ret;
435 PNtlmCredentials ntlm_cred = NULL;
436 PNegoHelper helper = NULL;
437 ULONG ctxt_attr = 0;
438 char* buffer, *want_flags = NULL;
439 PBYTE bin;
440 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
441 int token_idx;
442 SEC_CHAR *username = NULL;
443 SEC_CHAR *domain = NULL;
444 SEC_CHAR *password = NULL;
446 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
447 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
448 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
450 /****************************************
451 * When communicating with the client, there can be the
452 * following reply packets:
453 * YR <base64 blob> should be sent to the server
454 * PW should be sent back to helper with
455 * base64 encoded password
456 * AF <base64 blob> client is done, blob should be
457 * sent to server with KK prefixed
458 * GF <string list> A string list of negotiated flags
459 * GK <base64 blob> base64 encoded session key
460 * BH <char reason> something broke
462 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
464 if(TargetDataRep == SECURITY_NETWORK_DREP){
465 TRACE("Setting SECURITY_NETWORK_DREP\n");
468 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
469 bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
471 if((phContext == NULL) && (pInput == NULL))
473 static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
474 static CHAR credentials_argv[] = "--use-cached-creds";
475 SEC_CHAR *client_argv[5];
476 int pwlen = 0;
478 TRACE("First time in ISC()\n");
480 if(!phCredential)
482 ret = SEC_E_INVALID_HANDLE;
483 goto isc_end;
486 /* As the server side of sspi never calls this, make sure that
487 * the handler is a client handler.
489 ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
490 if(ntlm_cred->mode != NTLM_CLIENT)
492 TRACE("Cred mode = %d\n", ntlm_cred->mode);
493 ret = SEC_E_INVALID_HANDLE;
494 goto isc_end;
497 client_argv[0] = ntlm_auth;
498 client_argv[1] = helper_protocol;
499 if (!ntlm_cred->username_arg && !ntlm_cred->domain_arg)
501 LPWKSTA_USER_INFO_1 ui = NULL;
502 NET_API_STATUS status;
503 PCREDENTIALW cred;
505 if (ntlm_GetCachedCredential(pszTargetName, &cred))
507 LPWSTR p;
508 p = strchrW(cred->UserName, '\\');
509 if (p)
511 domain = ntlm_GetDomainArg(cred->UserName, p - cred->UserName);
512 p++;
514 else
516 domain = ntlm_GetDomainArg(NULL, 0);
517 p = cred->UserName;
520 username = ntlm_GetUsernameArg(p, -1);
522 if(cred->CredentialBlobSize != 0)
524 pwlen = WideCharToMultiByte(CP_UNIXCP,
525 WC_NO_BEST_FIT_CHARS, (LPWSTR)cred->CredentialBlob,
526 cred->CredentialBlobSize / sizeof(WCHAR), NULL, 0,
527 NULL, NULL);
529 password = HeapAlloc(GetProcessHeap(), 0, pwlen);
531 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
532 (LPWSTR)cred->CredentialBlob,
533 cred->CredentialBlobSize / sizeof(WCHAR),
534 password, pwlen, NULL, NULL);
537 CredFree(cred);
539 client_argv[2] = username;
540 client_argv[3] = domain;
541 client_argv[4] = NULL;
543 else
545 status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
546 if (status != NERR_Success || ui == NULL)
548 ret = SEC_E_NO_CREDENTIALS;
549 goto isc_end;
551 username = ntlm_GetUsernameArg(ui->wkui1_username, -1);
553 TRACE("using cached credentials\n");
555 client_argv[2] = username;
556 client_argv[3] = credentials_argv;
557 client_argv[4] = NULL;
560 else
562 client_argv[2] = ntlm_cred->username_arg;
563 client_argv[3] = ntlm_cred->domain_arg;
564 client_argv[4] = NULL;
567 if((ret = fork_helper(&helper, ntlm_auth, client_argv)) != SEC_E_OK)
568 goto isc_end;
570 helper->mode = NTLM_CLIENT;
571 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
572 if (!helper->session_key)
574 cleanup_helper(helper);
575 ret = SEC_E_INSUFFICIENT_MEMORY;
576 goto isc_end;
579 /* Generate the dummy session key = MD4(MD4(password))*/
580 if(password || ntlm_cred->password)
582 SEC_WCHAR *unicode_password;
583 int passwd_lenW;
585 TRACE("Converting password to unicode.\n");
586 passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
587 password ? password : ntlm_cred->password,
588 password ? pwlen : ntlm_cred->pwlen,
589 NULL, 0);
590 unicode_password = HeapAlloc(GetProcessHeap(), 0,
591 passwd_lenW * sizeof(SEC_WCHAR));
592 MultiByteToWideChar(CP_ACP, 0, password ? password : ntlm_cred->password,
593 password ? pwlen : ntlm_cred->pwlen, unicode_password, passwd_lenW);
595 SECUR32_CreateNTLMv1SessionKey((PBYTE)unicode_password,
596 passwd_lenW * sizeof(SEC_WCHAR), helper->session_key);
598 HeapFree(GetProcessHeap(), 0, unicode_password);
600 else
601 memset(helper->session_key, 0, 16);
603 /* Allocate space for a maximal string of
604 * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
605 * NTLMSSP_FEATURE_SESSION_KEY"
607 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
608 if(want_flags == NULL)
610 cleanup_helper(helper);
611 ret = SEC_E_INSUFFICIENT_MEMORY;
612 goto isc_end;
614 lstrcpyA(want_flags, "SF");
615 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
617 if(strstr(want_flags, "NTLMSSP_FEATURE_SEAL") == NULL)
618 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
620 if(fContextReq & ISC_REQ_CONNECTION)
621 ctxt_attr |= ISC_RET_CONNECTION;
622 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
623 ctxt_attr |= ISC_RET_EXTENDED_ERROR;
624 if(fContextReq & ISC_REQ_INTEGRITY)
626 if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
627 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
629 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
630 ctxt_attr |= ISC_RET_MUTUAL_AUTH;
631 if(fContextReq & ISC_REQ_REPLAY_DETECT)
633 if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
634 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
636 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
638 if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
639 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
641 if(fContextReq & ISC_REQ_STREAM)
642 FIXME("ISC_REQ_STREAM\n");
643 if(fContextReq & ISC_REQ_USE_DCE_STYLE)
644 ctxt_attr |= ISC_RET_USED_DCE_STYLE;
645 if(fContextReq & ISC_REQ_DELEGATE)
646 ctxt_attr |= ISC_RET_DELEGATE;
648 /* If no password is given, try to use cached credentials. Fall back to an empty
649 * password if this failed. */
650 if(!password && !ntlm_cred->password)
652 lstrcpynA(buffer, "OK", max_len-1);
653 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
655 cleanup_helper(helper);
656 goto isc_end;
658 /* If the helper replied with "PW", using cached credentials failed */
659 if(!strncmp(buffer, "PW", 2))
661 TRACE("Using cached credentials failed.\n");
662 ret = SEC_E_NO_CREDENTIALS;
663 goto isc_end;
665 else /* Just do a noop on the next run */
666 lstrcpynA(buffer, "OK", max_len-1);
668 else
670 lstrcpynA(buffer, "PW ", max_len-1);
671 if((ret = encodeBase64(password ? (unsigned char *)password : (unsigned char *)ntlm_cred->password,
672 password ? pwlen : ntlm_cred->pwlen, buffer+3,
673 max_len-3, &buffer_len)) != SEC_E_OK)
675 cleanup_helper(helper);
676 goto isc_end;
681 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
682 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
684 cleanup_helper(helper);
685 goto isc_end;
688 TRACE("Helper returned %s\n", debugstr_a(buffer));
690 if(lstrlenA(want_flags) > 2)
692 TRACE("Want flags are %s\n", debugstr_a(want_flags));
693 lstrcpynA(buffer, want_flags, max_len-1);
694 if((ret = run_helper(helper, buffer, max_len, &buffer_len))
695 != SEC_E_OK)
696 goto isc_end;
697 if(!strncmp(buffer, "BH", 2))
698 ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
701 lstrcpynA(buffer, "YR", max_len-1);
703 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
705 cleanup_helper(helper);
706 goto isc_end;
709 TRACE("%s\n", buffer);
711 if(strncmp(buffer, "YR ", 3) != 0)
713 /* Something borked */
714 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
715 ret = SEC_E_INTERNAL_ERROR;
716 cleanup_helper(helper);
717 goto isc_end;
719 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
720 max_len-1, &bin_len)) != SEC_E_OK)
722 cleanup_helper(helper);
723 goto isc_end;
726 /* put the decoded client blob into the out buffer */
728 phNewContext->dwUpper = ctxt_attr;
729 phNewContext->dwLower = (ULONG_PTR)helper;
731 ret = SEC_I_CONTINUE_NEEDED;
733 else
735 int input_token_idx;
737 /* handle second call here */
738 /* encode server data to base64 */
739 if (!pInput || ((input_token_idx = ntlm_GetTokenBufferIndex(pInput)) == -1))
741 ret = SEC_E_INVALID_TOKEN;
742 goto isc_end;
745 if(!phContext)
747 ret = SEC_E_INVALID_HANDLE;
748 goto isc_end;
751 /* As the server side of sspi never calls this, make sure that
752 * the handler is a client handler.
754 helper = (PNegoHelper)phContext->dwLower;
755 if(helper->mode != NTLM_CLIENT)
757 TRACE("Helper mode = %d\n", helper->mode);
758 ret = SEC_E_INVALID_HANDLE;
759 goto isc_end;
762 if (!pInput->pBuffers[input_token_idx].pvBuffer)
764 ret = SEC_E_INTERNAL_ERROR;
765 goto isc_end;
768 if(pInput->pBuffers[input_token_idx].cbBuffer > max_len)
770 TRACE("pInput->pBuffers[%d].cbBuffer is: %d\n",
771 input_token_idx,
772 pInput->pBuffers[input_token_idx].cbBuffer);
773 ret = SEC_E_INVALID_TOKEN;
774 goto isc_end;
776 else
777 bin_len = pInput->pBuffers[input_token_idx].cbBuffer;
779 memcpy(bin, pInput->pBuffers[input_token_idx].pvBuffer, bin_len);
781 lstrcpynA(buffer, "TT ", max_len-1);
783 if((ret = encodeBase64(bin, bin_len, buffer+3,
784 max_len-3, &buffer_len)) != SEC_E_OK)
785 goto isc_end;
787 TRACE("Server sent: %s\n", debugstr_a(buffer));
789 /* send TT base64 blob to ntlm_auth */
790 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
791 goto isc_end;
793 TRACE("Helper replied: %s\n", debugstr_a(buffer));
795 if( (strncmp(buffer, "KK ", 3) != 0) &&
796 (strncmp(buffer, "AF ", 3) !=0))
798 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
799 ret = SEC_E_INVALID_TOKEN;
800 goto isc_end;
803 /* decode the blob and send it to server */
804 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
805 &bin_len)) != SEC_E_OK)
807 goto isc_end;
810 phNewContext->dwUpper = ctxt_attr;
811 phNewContext->dwLower = (ULONG_PTR)helper;
813 ret = SEC_E_OK;
816 /* put the decoded client blob into the out buffer */
818 if (!pOutput || ((token_idx = ntlm_GetTokenBufferIndex(pOutput)) == -1))
820 TRACE("no SECBUFFER_TOKEN buffer could be found\n");
821 ret = SEC_E_BUFFER_TOO_SMALL;
822 if ((phContext == NULL) && (pInput == NULL))
824 cleanup_helper(helper);
825 phNewContext->dwUpper = 0;
826 phNewContext->dwLower = 0;
828 goto isc_end;
831 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
833 pOutput->pBuffers[token_idx].pvBuffer = HeapAlloc(GetProcessHeap(), 0, bin_len);
834 pOutput->pBuffers[token_idx].cbBuffer = bin_len;
836 else if (pOutput->pBuffers[token_idx].cbBuffer < bin_len)
838 TRACE("out buffer is NULL or has not enough space\n");
839 ret = SEC_E_BUFFER_TOO_SMALL;
840 if ((phContext == NULL) && (pInput == NULL))
842 cleanup_helper(helper);
843 phNewContext->dwUpper = 0;
844 phNewContext->dwLower = 0;
846 goto isc_end;
849 if (!pOutput->pBuffers[token_idx].pvBuffer)
851 TRACE("out buffer is NULL\n");
852 ret = SEC_E_INTERNAL_ERROR;
853 if ((phContext == NULL) && (pInput == NULL))
855 cleanup_helper(helper);
856 phNewContext->dwUpper = 0;
857 phNewContext->dwLower = 0;
859 goto isc_end;
862 pOutput->pBuffers[token_idx].cbBuffer = bin_len;
863 memcpy(pOutput->pBuffers[token_idx].pvBuffer, bin, bin_len);
865 if(ret == SEC_E_OK)
867 TRACE("Getting negotiated flags\n");
868 lstrcpynA(buffer, "GF", max_len - 1);
869 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
870 goto isc_end;
872 if(buffer_len < 3)
874 TRACE("No flags negotiated.\n");
875 helper->neg_flags = 0l;
877 else
879 TRACE("Negotiated %s\n", debugstr_a(buffer));
880 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
881 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
884 TRACE("Getting session key\n");
885 lstrcpynA(buffer, "GK", max_len - 1);
886 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
887 goto isc_end;
889 if(strncmp(buffer, "BH", 2) == 0)
890 TRACE("No key negotiated.\n");
891 else if(strncmp(buffer, "GK ", 3) == 0)
893 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
894 &bin_len)) != SEC_E_OK)
896 TRACE("Failed to decode session key\n");
898 TRACE("Session key is %s\n", debugstr_a(buffer+3));
899 HeapFree(GetProcessHeap(), 0, helper->session_key);
900 helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
901 if(!helper->session_key)
903 TRACE("Failed to allocate memory for session key\n");
904 ret = SEC_E_INTERNAL_ERROR;
905 goto isc_end;
907 memcpy(helper->session_key, bin, bin_len);
910 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
911 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
912 helper->crypt.ntlm.seq_num = 0l;
913 SECUR32_CreateNTLMv2SubKeys(helper);
914 helper->crypt.ntlm2.send_a4i = SECUR32_arc4Alloc();
915 helper->crypt.ntlm2.recv_a4i = SECUR32_arc4Alloc();
916 SECUR32_arc4Init(helper->crypt.ntlm2.send_a4i,
917 helper->crypt.ntlm2.send_seal_key, 16);
918 SECUR32_arc4Init(helper->crypt.ntlm2.recv_a4i,
919 helper->crypt.ntlm2.recv_seal_key, 16);
920 helper->crypt.ntlm2.send_seq_no = 0l;
921 helper->crypt.ntlm2.recv_seq_no = 0l;
924 isc_end:
925 HeapFree(GetProcessHeap(), 0, username);
926 HeapFree(GetProcessHeap(), 0, domain);
927 HeapFree(GetProcessHeap(), 0, password);
928 HeapFree(GetProcessHeap(), 0, want_flags);
929 HeapFree(GetProcessHeap(), 0, buffer);
930 HeapFree(GetProcessHeap(), 0, bin);
931 return ret;
934 /***********************************************************************
935 * InitializeSecurityContextA
937 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
938 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
939 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
940 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
941 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
943 SECURITY_STATUS ret;
944 SEC_WCHAR *target = NULL;
946 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
947 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
948 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
950 if(pszTargetName != NULL)
952 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
953 strlen(pszTargetName)+1, NULL, 0);
954 target = HeapAlloc(GetProcessHeap(), 0, target_size *
955 sizeof(SEC_WCHAR));
956 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
957 target, target_size);
960 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
961 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
962 phNewContext, pOutput, pfContextAttr, ptsExpiry);
964 HeapFree(GetProcessHeap(), 0, target);
965 return ret;
968 /***********************************************************************
969 * AcceptSecurityContext
971 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
972 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
973 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
974 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
976 SECURITY_STATUS ret;
977 char *buffer, *want_flags = NULL;
978 PBYTE bin;
979 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
980 ULONG ctxt_attr = 0;
981 PNegoHelper helper;
982 PNtlmCredentials ntlm_cred;
984 TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
985 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
986 ptsExpiry);
988 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
989 bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
991 if(TargetDataRep == SECURITY_NETWORK_DREP){
992 TRACE("Using SECURITY_NETWORK_DREP\n");
995 if(phContext == NULL)
997 static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp";
998 SEC_CHAR *server_argv[] = { ntlm_auth,
999 server_helper_protocol,
1000 NULL };
1002 if (!phCredential)
1004 ret = SEC_E_INVALID_HANDLE;
1005 goto asc_end;
1008 ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
1010 if(ntlm_cred->mode != NTLM_SERVER)
1012 ret = SEC_E_INVALID_HANDLE;
1013 goto asc_end;
1016 /* This is the first call to AcceptSecurityHandle */
1017 if(pInput == NULL)
1019 ret = SEC_E_INCOMPLETE_MESSAGE;
1020 goto asc_end;
1023 if(pInput->cBuffers < 1)
1025 ret = SEC_E_INCOMPLETE_MESSAGE;
1026 goto asc_end;
1029 if(pInput->pBuffers[0].cbBuffer > max_len)
1031 ret = SEC_E_INVALID_TOKEN;
1032 goto asc_end;
1034 else
1035 bin_len = pInput->pBuffers[0].cbBuffer;
1037 if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
1038 SEC_E_OK)
1040 ret = SEC_E_INTERNAL_ERROR;
1041 goto asc_end;
1043 helper->mode = NTLM_SERVER;
1045 /* Handle all the flags */
1046 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
1047 if(want_flags == NULL)
1049 TRACE("Failed to allocate memory for the want_flags!\n");
1050 ret = SEC_E_INSUFFICIENT_MEMORY;
1051 cleanup_helper(helper);
1052 goto asc_end;
1054 lstrcpyA(want_flags, "SF");
1055 if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
1057 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
1059 if(fContextReq & ASC_REQ_CONFIDENTIALITY)
1061 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
1063 if(fContextReq & ASC_REQ_CONNECTION)
1065 /* This is default, so we'll enable it */
1066 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
1067 ctxt_attr |= ASC_RET_CONNECTION;
1069 if(fContextReq & ASC_REQ_EXTENDED_ERROR)
1071 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
1073 if(fContextReq & ASC_REQ_INTEGRITY)
1075 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
1077 if(fContextReq & ASC_REQ_MUTUAL_AUTH)
1079 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
1081 if(fContextReq & ASC_REQ_REPLAY_DETECT)
1083 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
1085 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
1087 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
1089 if(fContextReq & ISC_REQ_STREAM)
1091 FIXME("ASC_REQ_STREAM stub\n");
1093 /* Done with the flags */
1095 if(lstrlenA(want_flags) > 3)
1097 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
1098 lstrcpynA(buffer, want_flags, max_len - 1);
1099 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1100 SEC_E_OK)
1102 cleanup_helper(helper);
1103 goto asc_end;
1105 if(!strncmp(buffer, "BH", 2))
1106 TRACE("Helper doesn't understand new command set\n");
1109 /* This is the YR request from the client, encode to base64 */
1111 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1113 lstrcpynA(buffer, "YR ", max_len-1);
1115 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1116 &buffer_len)) != SEC_E_OK)
1118 cleanup_helper(helper);
1119 goto asc_end;
1122 TRACE("Client sent: %s\n", debugstr_a(buffer));
1124 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1125 SEC_E_OK)
1127 cleanup_helper(helper);
1128 goto asc_end;
1131 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1132 /* The expected answer is TT <base64 blob> */
1134 if(strncmp(buffer, "TT ", 3) != 0)
1136 ret = SEC_E_INTERNAL_ERROR;
1137 cleanup_helper(helper);
1138 goto asc_end;
1141 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1142 &bin_len)) != SEC_E_OK)
1144 cleanup_helper(helper);
1145 goto asc_end;
1148 /* send this to the client */
1149 if(pOutput == NULL)
1151 ret = SEC_E_INSUFFICIENT_MEMORY;
1152 cleanup_helper(helper);
1153 goto asc_end;
1156 if(pOutput->cBuffers < 1)
1158 ret = SEC_E_INSUFFICIENT_MEMORY;
1159 cleanup_helper(helper);
1160 goto asc_end;
1163 pOutput->pBuffers[0].cbBuffer = bin_len;
1164 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
1165 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
1166 ret = SEC_I_CONTINUE_NEEDED;
1169 else
1171 /* we expect a KK request from client */
1172 if(pInput == NULL)
1174 ret = SEC_E_INCOMPLETE_MESSAGE;
1175 goto asc_end;
1178 if(pInput->cBuffers < 1)
1180 ret = SEC_E_INCOMPLETE_MESSAGE;
1181 goto asc_end;
1184 if(!phContext)
1186 ret = SEC_E_INVALID_HANDLE;
1187 goto asc_end;
1190 helper = (PNegoHelper)phContext->dwLower;
1192 if(helper->mode != NTLM_SERVER)
1194 ret = SEC_E_INVALID_HANDLE;
1195 goto asc_end;
1198 if(pInput->pBuffers[0].cbBuffer > max_len)
1200 ret = SEC_E_INVALID_TOKEN;
1201 goto asc_end;
1203 else
1204 bin_len = pInput->pBuffers[0].cbBuffer;
1206 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1208 lstrcpynA(buffer, "KK ", max_len-1);
1210 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1211 &buffer_len)) != SEC_E_OK)
1213 goto asc_end;
1216 TRACE("Client sent: %s\n", debugstr_a(buffer));
1218 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1219 SEC_E_OK)
1221 goto asc_end;
1224 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1226 /* At this point, we get a NA if the user didn't authenticate, but a BH
1227 * if ntlm_auth could not connect to winbindd. Apart from running Wine
1228 * as root, there is no way to fix this for now, so just handle this as
1229 * a failed login. */
1230 if(strncmp(buffer, "AF ", 3) != 0)
1232 if(strncmp(buffer, "NA ", 3) == 0)
1234 ret = SEC_E_LOGON_DENIED;
1235 goto asc_end;
1237 else
1239 size_t ntlm_pipe_err_len = strlen("BH NT_STATUS_ACCESS_DENIED");
1241 if( (buffer_len >= ntlm_pipe_err_len) &&
1242 (strncmp(buffer, "BH NT_STATUS_ACCESS_DENIED",
1243 ntlm_pipe_err_len) == 0))
1245 TRACE("Connection to winbindd failed\n");
1246 ret = SEC_E_LOGON_DENIED;
1248 else
1249 ret = SEC_E_INTERNAL_ERROR;
1251 goto asc_end;
1254 pOutput->pBuffers[0].cbBuffer = 0;
1255 ret = SEC_E_OK;
1257 TRACE("Getting negotiated flags\n");
1258 lstrcpynA(buffer, "GF", max_len - 1);
1259 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1260 goto asc_end;
1262 if(buffer_len < 3)
1264 TRACE("No flags negotiated, or helper does not support GF command\n");
1266 else
1268 TRACE("Negotiated %s\n", debugstr_a(buffer));
1269 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
1270 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
1273 TRACE("Getting session key\n");
1274 lstrcpynA(buffer, "GK", max_len - 1);
1275 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1276 goto asc_end;
1278 if(buffer_len < 3)
1279 TRACE("Helper does not support GK command\n");
1280 else
1282 if(strncmp(buffer, "BH ", 3) == 0)
1284 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1285 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1286 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1287 memset(helper->session_key, 0 , 16);
1289 else if(strncmp(buffer, "GK ", 3) == 0)
1291 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1292 &bin_len)) != SEC_E_OK)
1294 TRACE("Failed to decode session key\n");
1296 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1297 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1298 if(!helper->session_key)
1300 TRACE("Failed to allocate memory for session key\n");
1301 ret = SEC_E_INTERNAL_ERROR;
1302 goto asc_end;
1304 memcpy(helper->session_key, bin, 16);
1307 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1308 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1309 helper->crypt.ntlm.seq_num = 0l;
1312 phNewContext->dwUpper = ctxt_attr;
1313 phNewContext->dwLower = (ULONG_PTR)helper;
1315 asc_end:
1316 HeapFree(GetProcessHeap(), 0, want_flags);
1317 HeapFree(GetProcessHeap(), 0, buffer);
1318 HeapFree(GetProcessHeap(), 0, bin);
1319 return ret;
1322 /***********************************************************************
1323 * CompleteAuthToken
1325 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1326 PSecBufferDesc pToken)
1328 /* We never need to call CompleteAuthToken anyway */
1329 TRACE("%p %p\n", phContext, pToken);
1330 if (!phContext)
1331 return SEC_E_INVALID_HANDLE;
1333 return SEC_E_OK;
1336 /***********************************************************************
1337 * DeleteSecurityContext
1339 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1341 PNegoHelper helper;
1343 TRACE("%p\n", phContext);
1344 if (!phContext)
1345 return SEC_E_INVALID_HANDLE;
1347 helper = (PNegoHelper)phContext->dwLower;
1349 phContext->dwUpper = 0;
1350 phContext->dwLower = 0;
1352 SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1353 HeapFree(GetProcessHeap(), 0, helper->session_key);
1354 SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1355 SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1356 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
1357 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
1358 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
1359 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
1361 cleanup_helper(helper);
1363 return SEC_E_OK;
1366 /***********************************************************************
1367 * QueryContextAttributesW
1369 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1370 ULONG ulAttribute, void *pBuffer)
1372 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1373 if (!phContext)
1374 return SEC_E_INVALID_HANDLE;
1376 switch(ulAttribute)
1378 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1379 _x(SECPKG_ATTR_ACCESS_TOKEN);
1380 _x(SECPKG_ATTR_AUTHORITY);
1381 _x(SECPKG_ATTR_DCE_INFO);
1382 case SECPKG_ATTR_FLAGS:
1384 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1385 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1387 spcf->Flags = 0;
1388 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1389 spcf->Flags |= ISC_RET_INTEGRITY;
1390 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1391 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1392 return SEC_E_OK;
1394 _x(SECPKG_ATTR_KEY_INFO);
1395 _x(SECPKG_ATTR_LIFESPAN);
1396 _x(SECPKG_ATTR_NAMES);
1397 _x(SECPKG_ATTR_NATIVE_NAMES);
1398 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1399 _x(SECPKG_ATTR_PACKAGE_INFO);
1400 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1401 _x(SECPKG_ATTR_SESSION_KEY);
1402 case SECPKG_ATTR_SIZES:
1404 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1405 spcs->cbMaxToken = NTLM_MAX_BUF;
1406 spcs->cbMaxSignature = 16;
1407 spcs->cbBlockSize = 0;
1408 spcs->cbSecurityTrailer = 16;
1409 return SEC_E_OK;
1411 _x(SECPKG_ATTR_STREAM_SIZES);
1412 _x(SECPKG_ATTR_TARGET_INFORMATION);
1413 #undef _x
1414 default:
1415 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1418 return SEC_E_UNSUPPORTED_FUNCTION;
1421 /***********************************************************************
1422 * QueryContextAttributesA
1424 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1425 ULONG ulAttribute, void *pBuffer)
1427 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1430 /***********************************************************************
1431 * ImpersonateSecurityContext
1433 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1435 SECURITY_STATUS ret;
1437 TRACE("%p\n", phContext);
1438 if (phContext)
1440 ret = SEC_E_UNSUPPORTED_FUNCTION;
1442 else
1444 ret = SEC_E_INVALID_HANDLE;
1446 return ret;
1449 /***********************************************************************
1450 * RevertSecurityContext
1452 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1454 SECURITY_STATUS ret;
1456 TRACE("%p\n", phContext);
1457 if (phContext)
1459 ret = SEC_E_UNSUPPORTED_FUNCTION;
1461 else
1463 ret = SEC_E_INVALID_HANDLE;
1465 return ret;
1468 /***********************************************************************
1469 * ntlm_CreateSignature
1470 * As both MakeSignature and VerifySignature need this, but different keys
1471 * are needed for NTLMv2, the logic goes into a helper function.
1472 * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1473 * signing/encrypting and NTLM_RECV for verfying/decrypting. When encrypting,
1474 * the signature is encrypted after the message was encrypted, so
1475 * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1476 * false.
1478 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1479 int token_idx, SignDirection direction, BOOL encrypt_sig)
1481 ULONG sign_version = 1;
1482 UINT i;
1483 PBYTE sig;
1484 TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1485 encrypt_sig);
1487 sig = pMessage->pBuffers[token_idx].pvBuffer;
1489 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1490 helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1492 BYTE digest[16];
1493 BYTE seq_no[4];
1494 HMAC_MD5_CTX hmac_md5_ctx;
1496 TRACE("Signing NTLM2 style\n");
1498 if(direction == NTLM_SEND)
1500 seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
1501 seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
1502 seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1503 seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1505 ++(helper->crypt.ntlm2.send_seq_no);
1507 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1509 else
1511 seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
1512 seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
1513 seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1514 seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1516 ++(helper->crypt.ntlm2.recv_seq_no);
1518 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1521 HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1522 for( i = 0; i < pMessage->cBuffers; ++i )
1524 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1525 HMACMD5Update(&hmac_md5_ctx, pMessage->pBuffers[i].pvBuffer,
1526 pMessage->pBuffers[i].cbBuffer);
1529 HMACMD5Final(&hmac_md5_ctx, digest);
1531 if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1533 if(direction == NTLM_SEND)
1534 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1535 else
1536 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1539 /* The NTLM2 signature is the sign version */
1540 sig[ 0] = (sign_version >> 0) & 0xff;
1541 sig[ 1] = (sign_version >> 8) & 0xff;
1542 sig[ 2] = (sign_version >> 16) & 0xff;
1543 sig[ 3] = (sign_version >> 24) & 0xff;
1544 /* The first 8 bytes of the digest */
1545 memcpy(sig+4, digest, 8);
1546 /* And the sequence number */
1547 memcpy(sig+12, seq_no, 4);
1549 pMessage->pBuffers[token_idx].cbBuffer = 16;
1551 return SEC_E_OK;
1553 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1555 ULONG crc = 0U;
1556 TRACE("Signing NTLM1 style\n");
1558 for(i=0; i < pMessage->cBuffers; ++i)
1560 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1562 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1563 pMessage->pBuffers[i].cbBuffer, crc);
1567 sig[ 0] = (sign_version >> 0) & 0xff;
1568 sig[ 1] = (sign_version >> 8) & 0xff;
1569 sig[ 2] = (sign_version >> 16) & 0xff;
1570 sig[ 3] = (sign_version >> 24) & 0xff;
1571 memset(sig+4, 0, 4);
1572 sig[ 8] = (crc >> 0) & 0xff;
1573 sig[ 9] = (crc >> 8) & 0xff;
1574 sig[10] = (crc >> 16) & 0xff;
1575 sig[11] = (crc >> 24) & 0xff;
1576 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1577 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1578 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1579 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1581 ++(helper->crypt.ntlm.seq_num);
1583 if(encrypt_sig)
1584 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1585 return SEC_E_OK;
1588 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1590 TRACE("Creating a dummy signature.\n");
1591 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1592 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1593 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1594 pMessage->pBuffers[token_idx].cbBuffer = 16;
1595 return SEC_E_OK;
1598 return SEC_E_UNSUPPORTED_FUNCTION;
1601 /***********************************************************************
1602 * MakeSignature
1604 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1605 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1607 PNegoHelper helper;
1608 int token_idx;
1610 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1611 if (!phContext)
1612 return SEC_E_INVALID_HANDLE;
1614 if(fQOP)
1615 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1617 if(MessageSeqNo)
1618 FIXME("Ignoring MessageSeqNo\n");
1620 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1621 return SEC_E_INVALID_TOKEN;
1623 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1624 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1625 return SEC_E_INVALID_TOKEN;
1627 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1628 return SEC_E_BUFFER_TOO_SMALL;
1630 helper = (PNegoHelper)phContext->dwLower;
1631 TRACE("Negotiated flags are: 0x%08lx\n", helper->neg_flags);
1633 return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1636 /***********************************************************************
1637 * VerifySignature
1639 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1640 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1642 PNegoHelper helper;
1643 ULONG fQOP = 0;
1644 UINT i;
1645 int token_idx;
1646 SECURITY_STATUS ret;
1647 SecBufferDesc local_desc;
1648 PSecBuffer local_buff;
1649 BYTE local_sig[16];
1651 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1652 if(!phContext)
1653 return SEC_E_INVALID_HANDLE;
1655 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1656 return SEC_E_INVALID_TOKEN;
1658 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1659 return SEC_E_INVALID_TOKEN;
1661 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1662 return SEC_E_BUFFER_TOO_SMALL;
1664 if(MessageSeqNo)
1665 FIXME("Ignoring MessageSeqNo\n");
1667 helper = (PNegoHelper)phContext->dwLower;
1668 TRACE("Negotiated flags: 0x%08lx\n", helper->neg_flags);
1670 local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1672 local_desc.ulVersion = SECBUFFER_VERSION;
1673 local_desc.cBuffers = pMessage->cBuffers;
1674 local_desc.pBuffers = local_buff;
1676 for(i=0; i < pMessage->cBuffers; ++i)
1678 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1680 local_buff[i].BufferType = SECBUFFER_TOKEN;
1681 local_buff[i].cbBuffer = 16;
1682 local_buff[i].pvBuffer = local_sig;
1684 else
1686 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1687 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1688 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1692 if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1693 return ret;
1695 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1696 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1697 ret = SEC_E_MESSAGE_ALTERED;
1698 else
1699 ret = SEC_E_OK;
1701 HeapFree(GetProcessHeap(), 0, local_buff);
1702 pfQOP = &fQOP;
1704 return ret;
1708 /***********************************************************************
1709 * FreeCredentialsHandle
1711 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1712 PCredHandle phCredential)
1714 SECURITY_STATUS ret;
1716 if(phCredential){
1717 PNtlmCredentials ntlm_cred = (PNtlmCredentials) phCredential->dwLower;
1718 phCredential->dwUpper = 0;
1719 phCredential->dwLower = 0;
1720 if (ntlm_cred->password)
1721 memset(ntlm_cred->password, 0, ntlm_cred->pwlen);
1722 HeapFree(GetProcessHeap(), 0, ntlm_cred->password);
1723 HeapFree(GetProcessHeap(), 0, ntlm_cred->username_arg);
1724 HeapFree(GetProcessHeap(), 0, ntlm_cred->domain_arg);
1725 ret = SEC_E_OK;
1727 else
1728 ret = SEC_E_OK;
1730 return ret;
1733 /***********************************************************************
1734 * EncryptMessage
1736 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1737 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1739 PNegoHelper helper;
1740 int token_idx, data_idx;
1742 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1744 if(!phContext)
1745 return SEC_E_INVALID_HANDLE;
1747 if(fQOP)
1748 FIXME("Ignoring fQOP\n");
1750 if(MessageSeqNo)
1751 FIXME("Ignoring MessageSeqNo\n");
1753 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1754 return SEC_E_INVALID_TOKEN;
1756 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1757 return SEC_E_INVALID_TOKEN;
1759 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1 )
1760 return SEC_E_INVALID_TOKEN;
1762 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1763 return SEC_E_BUFFER_TOO_SMALL;
1765 helper = (PNegoHelper) phContext->dwLower;
1767 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1768 helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1770 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1771 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1772 pMessage->pBuffers[data_idx].pvBuffer,
1773 pMessage->pBuffers[data_idx].cbBuffer);
1775 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1776 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1777 ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1780 return SEC_E_OK;
1782 else
1784 PBYTE sig;
1785 ULONG save_flags;
1787 /* EncryptMessage always produces real signatures, so make sure
1788 * NTLMSSP_NEGOTIATE_SIGN is set*/
1789 save_flags = helper->neg_flags;
1790 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1791 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1792 helper->neg_flags = save_flags;
1794 sig = pMessage->pBuffers[token_idx].pvBuffer;
1796 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1797 pMessage->pBuffers[data_idx].pvBuffer,
1798 pMessage->pBuffers[data_idx].cbBuffer);
1799 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1801 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1802 memset(sig+4, 0, 4);
1806 return SEC_E_OK;
1809 /***********************************************************************
1810 * DecryptMessage
1812 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1813 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1815 SECURITY_STATUS ret;
1816 ULONG ntlmssp_flags_save;
1817 PNegoHelper helper;
1818 int token_idx, data_idx;
1819 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1821 if(!phContext)
1822 return SEC_E_INVALID_HANDLE;
1824 if(MessageSeqNo)
1825 FIXME("Ignoring MessageSeqNo\n");
1827 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1828 return SEC_E_INVALID_TOKEN;
1830 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1831 return SEC_E_INVALID_TOKEN;
1833 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1)
1834 return SEC_E_INVALID_TOKEN;
1836 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1837 return SEC_E_BUFFER_TOO_SMALL;
1839 helper = (PNegoHelper) phContext->dwLower;
1841 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1843 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1844 pMessage->pBuffers[data_idx].pvBuffer,
1845 pMessage->pBuffers[data_idx].cbBuffer);
1847 else
1849 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1850 pMessage->pBuffers[data_idx].pvBuffer,
1851 pMessage->pBuffers[data_idx].cbBuffer);
1854 /* Make sure we use a session key for the signature check, EncryptMessage
1855 * always does that, even in the dummy case */
1856 ntlmssp_flags_save = helper->neg_flags;
1858 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1859 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1861 helper->neg_flags = ntlmssp_flags_save;
1863 return ret;
1866 static const SecurityFunctionTableA ntlmTableA = {
1868 NULL, /* EnumerateSecurityPackagesA */
1869 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1870 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1871 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1872 NULL, /* Reserved2 */
1873 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1874 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1875 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1876 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1877 NULL, /* ApplyControlToken */
1878 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1879 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1880 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1881 ntlm_MakeSignature, /* MakeSignature */
1882 ntlm_VerifySignature, /* VerifySignature */
1883 FreeContextBuffer, /* FreeContextBuffer */
1884 NULL, /* QuerySecurityPackageInfoA */
1885 NULL, /* Reserved3 */
1886 NULL, /* Reserved4 */
1887 NULL, /* ExportSecurityContext */
1888 NULL, /* ImportSecurityContextA */
1889 NULL, /* AddCredentialsA */
1890 NULL, /* Reserved8 */
1891 NULL, /* QuerySecurityContextToken */
1892 ntlm_EncryptMessage, /* EncryptMessage */
1893 ntlm_DecryptMessage, /* DecryptMessage */
1894 NULL, /* SetContextAttributesA */
1897 static const SecurityFunctionTableW ntlmTableW = {
1899 NULL, /* EnumerateSecurityPackagesW */
1900 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1901 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1902 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1903 NULL, /* Reserved2 */
1904 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1905 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1906 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1907 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1908 NULL, /* ApplyControlToken */
1909 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1910 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1911 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1912 ntlm_MakeSignature, /* MakeSignature */
1913 ntlm_VerifySignature, /* VerifySignature */
1914 FreeContextBuffer, /* FreeContextBuffer */
1915 NULL, /* QuerySecurityPackageInfoW */
1916 NULL, /* Reserved3 */
1917 NULL, /* Reserved4 */
1918 NULL, /* ExportSecurityContext */
1919 NULL, /* ImportSecurityContextW */
1920 NULL, /* AddCredentialsW */
1921 NULL, /* Reserved8 */
1922 NULL, /* QuerySecurityContextToken */
1923 ntlm_EncryptMessage, /* EncryptMessage */
1924 ntlm_DecryptMessage, /* DecryptMessage */
1925 NULL, /* SetContextAttributesW */
1928 #define NTLM_COMMENT \
1929 { 'N', 'T', 'L', 'M', ' ', \
1930 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1931 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1933 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1934 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1936 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1938 static char ntlm_name_A[] = NTLM_NAME;
1939 static WCHAR ntlm_name_W[] = NTLM_NAME;
1941 /* According to Windows, NTLM has the following capabilities. */
1942 #define CAPS ( \
1943 SECPKG_FLAG_INTEGRITY | \
1944 SECPKG_FLAG_PRIVACY | \
1945 SECPKG_FLAG_TOKEN_ONLY | \
1946 SECPKG_FLAG_CONNECTION | \
1947 SECPKG_FLAG_MULTI_REQUIRED | \
1948 SECPKG_FLAG_IMPERSONATION | \
1949 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1950 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1952 static const SecPkgInfoW infoW = {
1953 CAPS,
1955 RPC_C_AUTHN_WINNT,
1956 NTLM_MAX_BUF,
1957 ntlm_name_W,
1958 ntlm_comment_W
1961 static const SecPkgInfoA infoA = {
1962 CAPS,
1964 RPC_C_AUTHN_WINNT,
1965 NTLM_MAX_BUF,
1966 ntlm_name_A,
1967 ntlm_comment_A
1970 void SECUR32_initNTLMSP(void)
1972 PNegoHelper helper;
1973 static CHAR version[] = "--version";
1975 SEC_CHAR *args[] = {
1976 ntlm_auth,
1977 version,
1978 NULL };
1980 if(fork_helper(&helper, ntlm_auth, args) != SEC_E_OK)
1982 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1983 helper = HeapAlloc(GetProcessHeap(),0, sizeof(NegoHelper));
1984 helper->major = helper->minor = helper->micro = -1;
1986 else
1987 check_version(helper);
1989 if( (helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
1990 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1991 helper->minor > MIN_NTLM_AUTH_MINOR_VERSION) ||
1992 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1993 helper->minor == MIN_NTLM_AUTH_MINOR_VERSION &&
1994 helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION) )
1996 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1997 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1999 else
2001 ERR("%s was not found or is outdated. "
2002 "Make sure that ntlm_auth >= %d.%d.%d is in your path.\n",
2003 ntlm_auth,
2004 MIN_NTLM_AUTH_MAJOR_VERSION,
2005 MIN_NTLM_AUTH_MINOR_VERSION,
2006 MIN_NTLM_AUTH_MICRO_VERSION);
2007 ERR("Usually, you can find it in the winbind package of your "
2008 "distribution.\n");
2011 cleanup_helper(helper);