ole32/tests: For win9x and winme the size of the data on the clipboard may be larger...
[wine/multimedia.git] / dlls / secur32 / ntlm.c
blob5e2450a47424358a25f501c029948f0a59a63639
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
667 /* Some versions of Samba have a broken ntlm_auth that can
668 * return "BH" here. Catch this and abort. */
669 if(!strncmp(buffer, "BH", 2))
671 ERR("ntlm_auth replied 'BH'. This should not happen. "
672 "Please fix your ntlm_auth install and try again.\n");
673 ret = SEC_E_INTERNAL_ERROR;
674 goto isc_end;
676 /* Otherwise, just do a noop on the next run */
677 lstrcpynA(buffer, "OK", max_len-1);
680 else
682 lstrcpynA(buffer, "PW ", max_len-1);
683 if((ret = encodeBase64(password ? (unsigned char *)password : (unsigned char *)ntlm_cred->password,
684 password ? pwlen : ntlm_cred->pwlen, buffer+3,
685 max_len-3, &buffer_len)) != SEC_E_OK)
687 cleanup_helper(helper);
688 goto isc_end;
693 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
694 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
696 cleanup_helper(helper);
697 goto isc_end;
700 TRACE("Helper returned %s\n", debugstr_a(buffer));
702 if(lstrlenA(want_flags) > 2)
704 TRACE("Want flags are %s\n", debugstr_a(want_flags));
705 lstrcpynA(buffer, want_flags, max_len-1);
706 if((ret = run_helper(helper, buffer, max_len, &buffer_len))
707 != SEC_E_OK)
708 goto isc_end;
709 if(!strncmp(buffer, "BH", 2))
710 ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
713 lstrcpynA(buffer, "YR", max_len-1);
715 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
717 cleanup_helper(helper);
718 goto isc_end;
721 TRACE("%s\n", buffer);
723 if(strncmp(buffer, "YR ", 3) != 0)
725 /* Something borked */
726 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
727 ret = SEC_E_INTERNAL_ERROR;
728 cleanup_helper(helper);
729 goto isc_end;
731 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
732 max_len-1, &bin_len)) != SEC_E_OK)
734 cleanup_helper(helper);
735 goto isc_end;
738 /* put the decoded client blob into the out buffer */
740 phNewContext->dwUpper = ctxt_attr;
741 phNewContext->dwLower = (ULONG_PTR)helper;
743 ret = SEC_I_CONTINUE_NEEDED;
745 else
747 int input_token_idx;
749 /* handle second call here */
750 /* encode server data to base64 */
751 if (!pInput || ((input_token_idx = ntlm_GetTokenBufferIndex(pInput)) == -1))
753 ret = SEC_E_INVALID_TOKEN;
754 goto isc_end;
757 if(!phContext)
759 ret = SEC_E_INVALID_HANDLE;
760 goto isc_end;
763 /* As the server side of sspi never calls this, make sure that
764 * the handler is a client handler.
766 helper = (PNegoHelper)phContext->dwLower;
767 if(helper->mode != NTLM_CLIENT)
769 TRACE("Helper mode = %d\n", helper->mode);
770 ret = SEC_E_INVALID_HANDLE;
771 goto isc_end;
774 if (!pInput->pBuffers[input_token_idx].pvBuffer)
776 ret = SEC_E_INTERNAL_ERROR;
777 goto isc_end;
780 if(pInput->pBuffers[input_token_idx].cbBuffer > max_len)
782 TRACE("pInput->pBuffers[%d].cbBuffer is: %d\n",
783 input_token_idx,
784 pInput->pBuffers[input_token_idx].cbBuffer);
785 ret = SEC_E_INVALID_TOKEN;
786 goto isc_end;
788 else
789 bin_len = pInput->pBuffers[input_token_idx].cbBuffer;
791 memcpy(bin, pInput->pBuffers[input_token_idx].pvBuffer, bin_len);
793 lstrcpynA(buffer, "TT ", max_len-1);
795 if((ret = encodeBase64(bin, bin_len, buffer+3,
796 max_len-3, &buffer_len)) != SEC_E_OK)
797 goto isc_end;
799 TRACE("Server sent: %s\n", debugstr_a(buffer));
801 /* send TT base64 blob to ntlm_auth */
802 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
803 goto isc_end;
805 TRACE("Helper replied: %s\n", debugstr_a(buffer));
807 if( (strncmp(buffer, "KK ", 3) != 0) &&
808 (strncmp(buffer, "AF ", 3) !=0))
810 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
811 ret = SEC_E_INVALID_TOKEN;
812 goto isc_end;
815 /* decode the blob and send it to server */
816 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
817 &bin_len)) != SEC_E_OK)
819 goto isc_end;
822 phNewContext->dwUpper = ctxt_attr;
823 phNewContext->dwLower = (ULONG_PTR)helper;
825 ret = SEC_E_OK;
828 /* put the decoded client blob into the out buffer */
830 if (!pOutput || ((token_idx = ntlm_GetTokenBufferIndex(pOutput)) == -1))
832 TRACE("no SECBUFFER_TOKEN buffer could be found\n");
833 ret = SEC_E_BUFFER_TOO_SMALL;
834 if ((phContext == NULL) && (pInput == NULL))
836 cleanup_helper(helper);
837 phNewContext->dwUpper = 0;
838 phNewContext->dwLower = 0;
840 goto isc_end;
843 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
845 pOutput->pBuffers[token_idx].pvBuffer = HeapAlloc(GetProcessHeap(), 0, bin_len);
846 pOutput->pBuffers[token_idx].cbBuffer = bin_len;
848 else if (pOutput->pBuffers[token_idx].cbBuffer < bin_len)
850 TRACE("out buffer is NULL or has not enough space\n");
851 ret = SEC_E_BUFFER_TOO_SMALL;
852 if ((phContext == NULL) && (pInput == NULL))
854 cleanup_helper(helper);
855 phNewContext->dwUpper = 0;
856 phNewContext->dwLower = 0;
858 goto isc_end;
861 if (!pOutput->pBuffers[token_idx].pvBuffer)
863 TRACE("out buffer is NULL\n");
864 ret = SEC_E_INTERNAL_ERROR;
865 if ((phContext == NULL) && (pInput == NULL))
867 cleanup_helper(helper);
868 phNewContext->dwUpper = 0;
869 phNewContext->dwLower = 0;
871 goto isc_end;
874 pOutput->pBuffers[token_idx].cbBuffer = bin_len;
875 memcpy(pOutput->pBuffers[token_idx].pvBuffer, bin, bin_len);
877 if(ret == SEC_E_OK)
879 TRACE("Getting negotiated flags\n");
880 lstrcpynA(buffer, "GF", max_len - 1);
881 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
882 goto isc_end;
884 if(buffer_len < 3)
886 TRACE("No flags negotiated.\n");
887 helper->neg_flags = 0l;
889 else
891 TRACE("Negotiated %s\n", debugstr_a(buffer));
892 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
893 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
896 TRACE("Getting session key\n");
897 lstrcpynA(buffer, "GK", max_len - 1);
898 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
899 goto isc_end;
901 if(strncmp(buffer, "BH", 2) == 0)
902 TRACE("No key negotiated.\n");
903 else if(strncmp(buffer, "GK ", 3) == 0)
905 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
906 &bin_len)) != SEC_E_OK)
908 TRACE("Failed to decode session key\n");
910 TRACE("Session key is %s\n", debugstr_a(buffer+3));
911 HeapFree(GetProcessHeap(), 0, helper->session_key);
912 helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
913 if(!helper->session_key)
915 TRACE("Failed to allocate memory for session key\n");
916 ret = SEC_E_INTERNAL_ERROR;
917 goto isc_end;
919 memcpy(helper->session_key, bin, bin_len);
922 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
923 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
924 helper->crypt.ntlm.seq_num = 0l;
925 SECUR32_CreateNTLMv2SubKeys(helper);
926 helper->crypt.ntlm2.send_a4i = SECUR32_arc4Alloc();
927 helper->crypt.ntlm2.recv_a4i = SECUR32_arc4Alloc();
928 SECUR32_arc4Init(helper->crypt.ntlm2.send_a4i,
929 helper->crypt.ntlm2.send_seal_key, 16);
930 SECUR32_arc4Init(helper->crypt.ntlm2.recv_a4i,
931 helper->crypt.ntlm2.recv_seal_key, 16);
932 helper->crypt.ntlm2.send_seq_no = 0l;
933 helper->crypt.ntlm2.recv_seq_no = 0l;
936 isc_end:
937 HeapFree(GetProcessHeap(), 0, username);
938 HeapFree(GetProcessHeap(), 0, domain);
939 HeapFree(GetProcessHeap(), 0, password);
940 HeapFree(GetProcessHeap(), 0, want_flags);
941 HeapFree(GetProcessHeap(), 0, buffer);
942 HeapFree(GetProcessHeap(), 0, bin);
943 return ret;
946 /***********************************************************************
947 * InitializeSecurityContextA
949 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
950 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
951 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
952 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
953 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
955 SECURITY_STATUS ret;
956 SEC_WCHAR *target = NULL;
958 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
959 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
960 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
962 if(pszTargetName != NULL)
964 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
965 strlen(pszTargetName)+1, NULL, 0);
966 target = HeapAlloc(GetProcessHeap(), 0, target_size *
967 sizeof(SEC_WCHAR));
968 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
969 target, target_size);
972 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
973 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
974 phNewContext, pOutput, pfContextAttr, ptsExpiry);
976 HeapFree(GetProcessHeap(), 0, target);
977 return ret;
980 /***********************************************************************
981 * AcceptSecurityContext
983 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
984 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
985 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
986 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
988 SECURITY_STATUS ret;
989 char *buffer, *want_flags = NULL;
990 PBYTE bin;
991 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
992 ULONG ctxt_attr = 0;
993 PNegoHelper helper;
994 PNtlmCredentials ntlm_cred;
996 TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
997 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
998 ptsExpiry);
1000 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
1001 bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
1003 if(TargetDataRep == SECURITY_NETWORK_DREP){
1004 TRACE("Using SECURITY_NETWORK_DREP\n");
1007 if(phContext == NULL)
1009 static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp";
1010 SEC_CHAR *server_argv[] = { ntlm_auth,
1011 server_helper_protocol,
1012 NULL };
1014 if (!phCredential)
1016 ret = SEC_E_INVALID_HANDLE;
1017 goto asc_end;
1020 ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
1022 if(ntlm_cred->mode != NTLM_SERVER)
1024 ret = SEC_E_INVALID_HANDLE;
1025 goto asc_end;
1028 /* This is the first call to AcceptSecurityHandle */
1029 if(pInput == NULL)
1031 ret = SEC_E_INCOMPLETE_MESSAGE;
1032 goto asc_end;
1035 if(pInput->cBuffers < 1)
1037 ret = SEC_E_INCOMPLETE_MESSAGE;
1038 goto asc_end;
1041 if(pInput->pBuffers[0].cbBuffer > max_len)
1043 ret = SEC_E_INVALID_TOKEN;
1044 goto asc_end;
1046 else
1047 bin_len = pInput->pBuffers[0].cbBuffer;
1049 if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
1050 SEC_E_OK)
1052 ret = SEC_E_INTERNAL_ERROR;
1053 goto asc_end;
1055 helper->mode = NTLM_SERVER;
1057 /* Handle all the flags */
1058 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
1059 if(want_flags == NULL)
1061 TRACE("Failed to allocate memory for the want_flags!\n");
1062 ret = SEC_E_INSUFFICIENT_MEMORY;
1063 cleanup_helper(helper);
1064 goto asc_end;
1066 lstrcpyA(want_flags, "SF");
1067 if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
1069 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
1071 if(fContextReq & ASC_REQ_CONFIDENTIALITY)
1073 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
1075 if(fContextReq & ASC_REQ_CONNECTION)
1077 /* This is default, so we'll enable it */
1078 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
1079 ctxt_attr |= ASC_RET_CONNECTION;
1081 if(fContextReq & ASC_REQ_EXTENDED_ERROR)
1083 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
1085 if(fContextReq & ASC_REQ_INTEGRITY)
1087 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
1089 if(fContextReq & ASC_REQ_MUTUAL_AUTH)
1091 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
1093 if(fContextReq & ASC_REQ_REPLAY_DETECT)
1095 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
1097 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
1099 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
1101 if(fContextReq & ISC_REQ_STREAM)
1103 FIXME("ASC_REQ_STREAM stub\n");
1105 /* Done with the flags */
1107 if(lstrlenA(want_flags) > 3)
1109 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
1110 lstrcpynA(buffer, want_flags, max_len - 1);
1111 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1112 SEC_E_OK)
1114 cleanup_helper(helper);
1115 goto asc_end;
1117 if(!strncmp(buffer, "BH", 2))
1118 TRACE("Helper doesn't understand new command set\n");
1121 /* This is the YR request from the client, encode to base64 */
1123 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1125 lstrcpynA(buffer, "YR ", max_len-1);
1127 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1128 &buffer_len)) != SEC_E_OK)
1130 cleanup_helper(helper);
1131 goto asc_end;
1134 TRACE("Client sent: %s\n", debugstr_a(buffer));
1136 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1137 SEC_E_OK)
1139 cleanup_helper(helper);
1140 goto asc_end;
1143 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1144 /* The expected answer is TT <base64 blob> */
1146 if(strncmp(buffer, "TT ", 3) != 0)
1148 ret = SEC_E_INTERNAL_ERROR;
1149 cleanup_helper(helper);
1150 goto asc_end;
1153 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1154 &bin_len)) != SEC_E_OK)
1156 cleanup_helper(helper);
1157 goto asc_end;
1160 /* send this to the client */
1161 if(pOutput == NULL)
1163 ret = SEC_E_INSUFFICIENT_MEMORY;
1164 cleanup_helper(helper);
1165 goto asc_end;
1168 if(pOutput->cBuffers < 1)
1170 ret = SEC_E_INSUFFICIENT_MEMORY;
1171 cleanup_helper(helper);
1172 goto asc_end;
1175 pOutput->pBuffers[0].cbBuffer = bin_len;
1176 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
1177 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
1178 ret = SEC_I_CONTINUE_NEEDED;
1181 else
1183 /* we expect a KK request from client */
1184 if(pInput == NULL)
1186 ret = SEC_E_INCOMPLETE_MESSAGE;
1187 goto asc_end;
1190 if(pInput->cBuffers < 1)
1192 ret = SEC_E_INCOMPLETE_MESSAGE;
1193 goto asc_end;
1196 if(!phContext)
1198 ret = SEC_E_INVALID_HANDLE;
1199 goto asc_end;
1202 helper = (PNegoHelper)phContext->dwLower;
1204 if(helper->mode != NTLM_SERVER)
1206 ret = SEC_E_INVALID_HANDLE;
1207 goto asc_end;
1210 if(pInput->pBuffers[0].cbBuffer > max_len)
1212 ret = SEC_E_INVALID_TOKEN;
1213 goto asc_end;
1215 else
1216 bin_len = pInput->pBuffers[0].cbBuffer;
1218 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1220 lstrcpynA(buffer, "KK ", max_len-1);
1222 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1223 &buffer_len)) != SEC_E_OK)
1225 goto asc_end;
1228 TRACE("Client sent: %s\n", debugstr_a(buffer));
1230 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1231 SEC_E_OK)
1233 goto asc_end;
1236 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1238 /* At this point, we get a NA if the user didn't authenticate, but a BH
1239 * if ntlm_auth could not connect to winbindd. Apart from running Wine
1240 * as root, there is no way to fix this for now, so just handle this as
1241 * a failed login. */
1242 if(strncmp(buffer, "AF ", 3) != 0)
1244 if(strncmp(buffer, "NA ", 3) == 0)
1246 ret = SEC_E_LOGON_DENIED;
1247 goto asc_end;
1249 else
1251 size_t ntlm_pipe_err_len = strlen("BH NT_STATUS_ACCESS_DENIED");
1253 if( (buffer_len >= ntlm_pipe_err_len) &&
1254 (strncmp(buffer, "BH NT_STATUS_ACCESS_DENIED",
1255 ntlm_pipe_err_len) == 0))
1257 TRACE("Connection to winbindd failed\n");
1258 ret = SEC_E_LOGON_DENIED;
1260 else
1261 ret = SEC_E_INTERNAL_ERROR;
1263 goto asc_end;
1266 pOutput->pBuffers[0].cbBuffer = 0;
1267 ret = SEC_E_OK;
1269 TRACE("Getting negotiated flags\n");
1270 lstrcpynA(buffer, "GF", max_len - 1);
1271 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1272 goto asc_end;
1274 if(buffer_len < 3)
1276 TRACE("No flags negotiated, or helper does not support GF command\n");
1278 else
1280 TRACE("Negotiated %s\n", debugstr_a(buffer));
1281 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
1282 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
1285 TRACE("Getting session key\n");
1286 lstrcpynA(buffer, "GK", max_len - 1);
1287 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1288 goto asc_end;
1290 if(buffer_len < 3)
1291 TRACE("Helper does not support GK command\n");
1292 else
1294 if(strncmp(buffer, "BH ", 3) == 0)
1296 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1297 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1298 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1299 memset(helper->session_key, 0 , 16);
1301 else if(strncmp(buffer, "GK ", 3) == 0)
1303 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1304 &bin_len)) != SEC_E_OK)
1306 TRACE("Failed to decode session key\n");
1308 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1309 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1310 if(!helper->session_key)
1312 TRACE("Failed to allocate memory for session key\n");
1313 ret = SEC_E_INTERNAL_ERROR;
1314 goto asc_end;
1316 memcpy(helper->session_key, bin, 16);
1319 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1320 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1321 helper->crypt.ntlm.seq_num = 0l;
1324 phNewContext->dwUpper = ctxt_attr;
1325 phNewContext->dwLower = (ULONG_PTR)helper;
1327 asc_end:
1328 HeapFree(GetProcessHeap(), 0, want_flags);
1329 HeapFree(GetProcessHeap(), 0, buffer);
1330 HeapFree(GetProcessHeap(), 0, bin);
1331 return ret;
1334 /***********************************************************************
1335 * CompleteAuthToken
1337 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1338 PSecBufferDesc pToken)
1340 /* We never need to call CompleteAuthToken anyway */
1341 TRACE("%p %p\n", phContext, pToken);
1342 if (!phContext)
1343 return SEC_E_INVALID_HANDLE;
1345 return SEC_E_OK;
1348 /***********************************************************************
1349 * DeleteSecurityContext
1351 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1353 PNegoHelper helper;
1355 TRACE("%p\n", phContext);
1356 if (!phContext)
1357 return SEC_E_INVALID_HANDLE;
1359 helper = (PNegoHelper)phContext->dwLower;
1361 phContext->dwUpper = 0;
1362 phContext->dwLower = 0;
1364 SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1365 HeapFree(GetProcessHeap(), 0, helper->session_key);
1366 SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1367 SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1368 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
1369 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
1370 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
1371 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
1373 cleanup_helper(helper);
1375 return SEC_E_OK;
1378 /***********************************************************************
1379 * QueryContextAttributesW
1381 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1382 ULONG ulAttribute, void *pBuffer)
1384 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1385 if (!phContext)
1386 return SEC_E_INVALID_HANDLE;
1388 switch(ulAttribute)
1390 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1391 _x(SECPKG_ATTR_ACCESS_TOKEN);
1392 _x(SECPKG_ATTR_AUTHORITY);
1393 _x(SECPKG_ATTR_DCE_INFO);
1394 case SECPKG_ATTR_FLAGS:
1396 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1397 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1399 spcf->Flags = 0;
1400 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1401 spcf->Flags |= ISC_RET_INTEGRITY;
1402 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1403 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1404 return SEC_E_OK;
1406 _x(SECPKG_ATTR_KEY_INFO);
1407 _x(SECPKG_ATTR_LIFESPAN);
1408 _x(SECPKG_ATTR_NAMES);
1409 _x(SECPKG_ATTR_NATIVE_NAMES);
1410 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1411 _x(SECPKG_ATTR_PACKAGE_INFO);
1412 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1413 _x(SECPKG_ATTR_SESSION_KEY);
1414 case SECPKG_ATTR_SIZES:
1416 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1417 spcs->cbMaxToken = NTLM_MAX_BUF;
1418 spcs->cbMaxSignature = 16;
1419 spcs->cbBlockSize = 0;
1420 spcs->cbSecurityTrailer = 16;
1421 return SEC_E_OK;
1423 _x(SECPKG_ATTR_STREAM_SIZES);
1424 _x(SECPKG_ATTR_TARGET_INFORMATION);
1425 #undef _x
1426 default:
1427 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1430 return SEC_E_UNSUPPORTED_FUNCTION;
1433 /***********************************************************************
1434 * QueryContextAttributesA
1436 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1437 ULONG ulAttribute, void *pBuffer)
1439 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1442 /***********************************************************************
1443 * ImpersonateSecurityContext
1445 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1447 SECURITY_STATUS ret;
1449 TRACE("%p\n", phContext);
1450 if (phContext)
1452 ret = SEC_E_UNSUPPORTED_FUNCTION;
1454 else
1456 ret = SEC_E_INVALID_HANDLE;
1458 return ret;
1461 /***********************************************************************
1462 * RevertSecurityContext
1464 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1466 SECURITY_STATUS ret;
1468 TRACE("%p\n", phContext);
1469 if (phContext)
1471 ret = SEC_E_UNSUPPORTED_FUNCTION;
1473 else
1475 ret = SEC_E_INVALID_HANDLE;
1477 return ret;
1480 /***********************************************************************
1481 * ntlm_CreateSignature
1482 * As both MakeSignature and VerifySignature need this, but different keys
1483 * are needed for NTLMv2, the logic goes into a helper function.
1484 * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1485 * signing/encrypting and NTLM_RECV for verfying/decrypting. When encrypting,
1486 * the signature is encrypted after the message was encrypted, so
1487 * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1488 * false.
1490 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1491 int token_idx, SignDirection direction, BOOL encrypt_sig)
1493 ULONG sign_version = 1;
1494 UINT i;
1495 PBYTE sig;
1496 TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1497 encrypt_sig);
1499 sig = pMessage->pBuffers[token_idx].pvBuffer;
1501 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1502 helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1504 BYTE digest[16];
1505 BYTE seq_no[4];
1506 HMAC_MD5_CTX hmac_md5_ctx;
1508 TRACE("Signing NTLM2 style\n");
1510 if(direction == NTLM_SEND)
1512 seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
1513 seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
1514 seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1515 seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1517 ++(helper->crypt.ntlm2.send_seq_no);
1519 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1521 else
1523 seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
1524 seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
1525 seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1526 seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1528 ++(helper->crypt.ntlm2.recv_seq_no);
1530 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1533 HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1534 for( i = 0; i < pMessage->cBuffers; ++i )
1536 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1537 HMACMD5Update(&hmac_md5_ctx, pMessage->pBuffers[i].pvBuffer,
1538 pMessage->pBuffers[i].cbBuffer);
1541 HMACMD5Final(&hmac_md5_ctx, digest);
1543 if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1545 if(direction == NTLM_SEND)
1546 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1547 else
1548 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1551 /* The NTLM2 signature is the sign version */
1552 sig[ 0] = (sign_version >> 0) & 0xff;
1553 sig[ 1] = (sign_version >> 8) & 0xff;
1554 sig[ 2] = (sign_version >> 16) & 0xff;
1555 sig[ 3] = (sign_version >> 24) & 0xff;
1556 /* The first 8 bytes of the digest */
1557 memcpy(sig+4, digest, 8);
1558 /* And the sequence number */
1559 memcpy(sig+12, seq_no, 4);
1561 pMessage->pBuffers[token_idx].cbBuffer = 16;
1563 return SEC_E_OK;
1565 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1567 ULONG crc = 0U;
1568 TRACE("Signing NTLM1 style\n");
1570 for(i=0; i < pMessage->cBuffers; ++i)
1572 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1574 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1575 pMessage->pBuffers[i].cbBuffer, crc);
1579 sig[ 0] = (sign_version >> 0) & 0xff;
1580 sig[ 1] = (sign_version >> 8) & 0xff;
1581 sig[ 2] = (sign_version >> 16) & 0xff;
1582 sig[ 3] = (sign_version >> 24) & 0xff;
1583 memset(sig+4, 0, 4);
1584 sig[ 8] = (crc >> 0) & 0xff;
1585 sig[ 9] = (crc >> 8) & 0xff;
1586 sig[10] = (crc >> 16) & 0xff;
1587 sig[11] = (crc >> 24) & 0xff;
1588 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1589 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1590 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1591 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1593 ++(helper->crypt.ntlm.seq_num);
1595 if(encrypt_sig)
1596 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1597 return SEC_E_OK;
1600 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1602 TRACE("Creating a dummy signature.\n");
1603 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1604 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1605 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1606 pMessage->pBuffers[token_idx].cbBuffer = 16;
1607 return SEC_E_OK;
1610 return SEC_E_UNSUPPORTED_FUNCTION;
1613 /***********************************************************************
1614 * MakeSignature
1616 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1617 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1619 PNegoHelper helper;
1620 int token_idx;
1622 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1623 if (!phContext)
1624 return SEC_E_INVALID_HANDLE;
1626 if(fQOP)
1627 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1629 if(MessageSeqNo)
1630 FIXME("Ignoring MessageSeqNo\n");
1632 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1633 return SEC_E_INVALID_TOKEN;
1635 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1636 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1637 return SEC_E_INVALID_TOKEN;
1639 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1640 return SEC_E_BUFFER_TOO_SMALL;
1642 helper = (PNegoHelper)phContext->dwLower;
1643 TRACE("Negotiated flags are: 0x%08lx\n", helper->neg_flags);
1645 return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1648 /***********************************************************************
1649 * VerifySignature
1651 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1652 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1654 PNegoHelper helper;
1655 ULONG fQOP = 0;
1656 UINT i;
1657 int token_idx;
1658 SECURITY_STATUS ret;
1659 SecBufferDesc local_desc;
1660 PSecBuffer local_buff;
1661 BYTE local_sig[16];
1663 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1664 if(!phContext)
1665 return SEC_E_INVALID_HANDLE;
1667 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1668 return SEC_E_INVALID_TOKEN;
1670 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1671 return SEC_E_INVALID_TOKEN;
1673 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1674 return SEC_E_BUFFER_TOO_SMALL;
1676 if(MessageSeqNo)
1677 FIXME("Ignoring MessageSeqNo\n");
1679 helper = (PNegoHelper)phContext->dwLower;
1680 TRACE("Negotiated flags: 0x%08lx\n", helper->neg_flags);
1682 local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1684 local_desc.ulVersion = SECBUFFER_VERSION;
1685 local_desc.cBuffers = pMessage->cBuffers;
1686 local_desc.pBuffers = local_buff;
1688 for(i=0; i < pMessage->cBuffers; ++i)
1690 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1692 local_buff[i].BufferType = SECBUFFER_TOKEN;
1693 local_buff[i].cbBuffer = 16;
1694 local_buff[i].pvBuffer = local_sig;
1696 else
1698 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1699 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1700 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1704 if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1705 return ret;
1707 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1708 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1709 ret = SEC_E_MESSAGE_ALTERED;
1710 else
1711 ret = SEC_E_OK;
1713 HeapFree(GetProcessHeap(), 0, local_buff);
1714 pfQOP = &fQOP;
1716 return ret;
1720 /***********************************************************************
1721 * FreeCredentialsHandle
1723 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1724 PCredHandle phCredential)
1726 SECURITY_STATUS ret;
1728 if(phCredential){
1729 PNtlmCredentials ntlm_cred = (PNtlmCredentials) phCredential->dwLower;
1730 phCredential->dwUpper = 0;
1731 phCredential->dwLower = 0;
1732 if (ntlm_cred->password)
1733 memset(ntlm_cred->password, 0, ntlm_cred->pwlen);
1734 HeapFree(GetProcessHeap(), 0, ntlm_cred->password);
1735 HeapFree(GetProcessHeap(), 0, ntlm_cred->username_arg);
1736 HeapFree(GetProcessHeap(), 0, ntlm_cred->domain_arg);
1737 ret = SEC_E_OK;
1739 else
1740 ret = SEC_E_OK;
1742 return ret;
1745 /***********************************************************************
1746 * EncryptMessage
1748 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1749 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1751 PNegoHelper helper;
1752 int token_idx, data_idx;
1754 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1756 if(!phContext)
1757 return SEC_E_INVALID_HANDLE;
1759 if(fQOP)
1760 FIXME("Ignoring fQOP\n");
1762 if(MessageSeqNo)
1763 FIXME("Ignoring MessageSeqNo\n");
1765 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1766 return SEC_E_INVALID_TOKEN;
1768 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1769 return SEC_E_INVALID_TOKEN;
1771 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1 )
1772 return SEC_E_INVALID_TOKEN;
1774 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1775 return SEC_E_BUFFER_TOO_SMALL;
1777 helper = (PNegoHelper) phContext->dwLower;
1779 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1780 helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1782 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1783 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1784 pMessage->pBuffers[data_idx].pvBuffer,
1785 pMessage->pBuffers[data_idx].cbBuffer);
1787 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1788 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1789 ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1792 return SEC_E_OK;
1794 else
1796 PBYTE sig;
1797 ULONG save_flags;
1799 /* EncryptMessage always produces real signatures, so make sure
1800 * NTLMSSP_NEGOTIATE_SIGN is set*/
1801 save_flags = helper->neg_flags;
1802 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1803 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1804 helper->neg_flags = save_flags;
1806 sig = pMessage->pBuffers[token_idx].pvBuffer;
1808 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1809 pMessage->pBuffers[data_idx].pvBuffer,
1810 pMessage->pBuffers[data_idx].cbBuffer);
1811 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1813 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1814 memset(sig+4, 0, 4);
1818 return SEC_E_OK;
1821 /***********************************************************************
1822 * DecryptMessage
1824 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1825 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1827 SECURITY_STATUS ret;
1828 ULONG ntlmssp_flags_save;
1829 PNegoHelper helper;
1830 int token_idx, data_idx;
1831 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1833 if(!phContext)
1834 return SEC_E_INVALID_HANDLE;
1836 if(MessageSeqNo)
1837 FIXME("Ignoring MessageSeqNo\n");
1839 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1840 return SEC_E_INVALID_TOKEN;
1842 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1843 return SEC_E_INVALID_TOKEN;
1845 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1)
1846 return SEC_E_INVALID_TOKEN;
1848 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1849 return SEC_E_BUFFER_TOO_SMALL;
1851 helper = (PNegoHelper) phContext->dwLower;
1853 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1855 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1856 pMessage->pBuffers[data_idx].pvBuffer,
1857 pMessage->pBuffers[data_idx].cbBuffer);
1859 else
1861 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1862 pMessage->pBuffers[data_idx].pvBuffer,
1863 pMessage->pBuffers[data_idx].cbBuffer);
1866 /* Make sure we use a session key for the signature check, EncryptMessage
1867 * always does that, even in the dummy case */
1868 ntlmssp_flags_save = helper->neg_flags;
1870 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1871 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1873 helper->neg_flags = ntlmssp_flags_save;
1875 return ret;
1878 static const SecurityFunctionTableA ntlmTableA = {
1880 NULL, /* EnumerateSecurityPackagesA */
1881 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1882 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1883 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1884 NULL, /* Reserved2 */
1885 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1886 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1887 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1888 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1889 NULL, /* ApplyControlToken */
1890 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1891 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1892 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1893 ntlm_MakeSignature, /* MakeSignature */
1894 ntlm_VerifySignature, /* VerifySignature */
1895 FreeContextBuffer, /* FreeContextBuffer */
1896 NULL, /* QuerySecurityPackageInfoA */
1897 NULL, /* Reserved3 */
1898 NULL, /* Reserved4 */
1899 NULL, /* ExportSecurityContext */
1900 NULL, /* ImportSecurityContextA */
1901 NULL, /* AddCredentialsA */
1902 NULL, /* Reserved8 */
1903 NULL, /* QuerySecurityContextToken */
1904 ntlm_EncryptMessage, /* EncryptMessage */
1905 ntlm_DecryptMessage, /* DecryptMessage */
1906 NULL, /* SetContextAttributesA */
1909 static const SecurityFunctionTableW ntlmTableW = {
1911 NULL, /* EnumerateSecurityPackagesW */
1912 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1913 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1914 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1915 NULL, /* Reserved2 */
1916 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1917 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1918 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1919 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1920 NULL, /* ApplyControlToken */
1921 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1922 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1923 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1924 ntlm_MakeSignature, /* MakeSignature */
1925 ntlm_VerifySignature, /* VerifySignature */
1926 FreeContextBuffer, /* FreeContextBuffer */
1927 NULL, /* QuerySecurityPackageInfoW */
1928 NULL, /* Reserved3 */
1929 NULL, /* Reserved4 */
1930 NULL, /* ExportSecurityContext */
1931 NULL, /* ImportSecurityContextW */
1932 NULL, /* AddCredentialsW */
1933 NULL, /* Reserved8 */
1934 NULL, /* QuerySecurityContextToken */
1935 ntlm_EncryptMessage, /* EncryptMessage */
1936 ntlm_DecryptMessage, /* DecryptMessage */
1937 NULL, /* SetContextAttributesW */
1940 #define NTLM_COMMENT \
1941 { 'N', 'T', 'L', 'M', ' ', \
1942 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1943 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1945 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1946 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1948 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1950 static char ntlm_name_A[] = NTLM_NAME;
1951 static WCHAR ntlm_name_W[] = NTLM_NAME;
1953 /* According to Windows, NTLM has the following capabilities. */
1954 #define CAPS ( \
1955 SECPKG_FLAG_INTEGRITY | \
1956 SECPKG_FLAG_PRIVACY | \
1957 SECPKG_FLAG_TOKEN_ONLY | \
1958 SECPKG_FLAG_CONNECTION | \
1959 SECPKG_FLAG_MULTI_REQUIRED | \
1960 SECPKG_FLAG_IMPERSONATION | \
1961 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1962 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1964 static const SecPkgInfoW infoW = {
1965 CAPS,
1967 RPC_C_AUTHN_WINNT,
1968 NTLM_MAX_BUF,
1969 ntlm_name_W,
1970 ntlm_comment_W
1973 static const SecPkgInfoA infoA = {
1974 CAPS,
1976 RPC_C_AUTHN_WINNT,
1977 NTLM_MAX_BUF,
1978 ntlm_name_A,
1979 ntlm_comment_A
1982 void SECUR32_initNTLMSP(void)
1984 PNegoHelper helper;
1985 static CHAR version[] = "--version";
1987 SEC_CHAR *args[] = {
1988 ntlm_auth,
1989 version,
1990 NULL };
1992 if(fork_helper(&helper, ntlm_auth, args) != SEC_E_OK)
1994 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1995 helper = HeapAlloc(GetProcessHeap(),0, sizeof(NegoHelper));
1996 helper->major = helper->minor = helper->micro = -1;
1997 helper->pipe_in = helper->pipe_out = -1;
1999 else
2000 check_version(helper);
2002 if( (helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
2003 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
2004 helper->minor > MIN_NTLM_AUTH_MINOR_VERSION) ||
2005 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
2006 helper->minor == MIN_NTLM_AUTH_MINOR_VERSION &&
2007 helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION) )
2009 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
2010 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
2012 else
2014 ERR("%s was not found or is outdated. "
2015 "Make sure that ntlm_auth >= %d.%d.%d is in your path.\n",
2016 ntlm_auth,
2017 MIN_NTLM_AUTH_MAJOR_VERSION,
2018 MIN_NTLM_AUTH_MINOR_VERSION,
2019 MIN_NTLM_AUTH_MICRO_VERSION);
2020 ERR("Usually, you can find it in the winbind package of your "
2021 "distribution.\n");
2024 cleanup_helper(helper);