dmime: Reimplement IPersistStream_Load() for DMSegment.
[wine.git] / dlls / secur32 / ntlm.c
blob3280fa1d25ec12f2eb8af6c8db5ef3833d27d70e
1 /*
2 * Copyright 2005, 2006 Kai Blin
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 * This file implements the NTLM security provider.
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "wincred.h"
28 #include "rpc.h"
29 #include "sspi.h"
30 #include "lm.h"
31 #include "secur32_priv.h"
32 #include "hmac_md5.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ntlm);
37 WINE_DECLARE_DEBUG_CHANNEL(winediag);
39 #define NTLM_MAX_BUF 1904
40 #define MIN_NTLM_AUTH_MAJOR_VERSION 3
41 #define MIN_NTLM_AUTH_MINOR_VERSION 0
42 #define MIN_NTLM_AUTH_MICRO_VERSION 25
44 static CHAR ntlm_auth[] = "ntlm_auth";
46 /***********************************************************************
47 * QueryCredentialsAttributesA
49 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
50 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
52 SECURITY_STATUS ret;
54 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
56 if(ulAttribute == SECPKG_ATTR_NAMES)
58 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
59 ret = SEC_E_UNSUPPORTED_FUNCTION;
61 else
62 ret = SEC_E_UNSUPPORTED_FUNCTION;
64 return ret;
67 /***********************************************************************
68 * QueryCredentialsAttributesW
70 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
71 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
73 SECURITY_STATUS ret;
75 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
77 if(ulAttribute == SECPKG_ATTR_NAMES)
79 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
80 ret = SEC_E_UNSUPPORTED_FUNCTION;
82 else
83 ret = SEC_E_UNSUPPORTED_FUNCTION;
85 return ret;
88 static char *ntlm_GetUsernameArg(LPCWSTR userW, INT userW_length)
90 static const char username_arg[] = "--username=";
91 char *user;
92 int unixcp_size;
94 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
95 userW, userW_length, NULL, 0, NULL, NULL) + sizeof(username_arg);
96 if (!(user = heap_alloc(unixcp_size))) return NULL;
97 memcpy(user, username_arg, sizeof(username_arg) - 1);
98 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, userW, userW_length,
99 user + sizeof(username_arg) - 1,
100 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
101 user[unixcp_size - 1] = '\0';
102 return user;
105 static char *ntlm_GetDomainArg(LPCWSTR domainW, INT domainW_length)
107 static const char domain_arg[] = "--domain=";
108 char *domain;
109 int unixcp_size;
111 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
112 domainW, domainW_length, NULL, 0, NULL, NULL) + sizeof(domain_arg);
113 if (!(domain = heap_alloc(unixcp_size))) return NULL;
114 memcpy(domain, domain_arg, sizeof(domain_arg) - 1);
115 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domainW,
116 domainW_length, domain + sizeof(domain_arg) - 1,
117 unixcp_size - sizeof(domain) + 1, NULL, NULL);
118 domain[unixcp_size - 1] = '\0';
119 return domain;
122 /***********************************************************************
123 * AcquireCredentialsHandleW
125 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
126 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
127 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
128 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
130 SECURITY_STATUS ret = SEC_E_INSUFFICIENT_MEMORY;
131 PNtlmCredentials ntlm_cred = NULL;
132 LPWSTR domain = NULL, user = NULL, password = NULL;
133 PSEC_WINNT_AUTH_IDENTITY_W auth_data = NULL;
135 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
136 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
137 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
139 switch (fCredentialUse)
141 case SECPKG_CRED_INBOUND:
142 if (!(ntlm_cred = heap_alloc(sizeof(*ntlm_cred)))) return SEC_E_INSUFFICIENT_MEMORY;
143 ntlm_cred->mode = NTLM_SERVER;
144 ntlm_cred->username_arg = NULL;
145 ntlm_cred->domain_arg = NULL;
146 ntlm_cred->password = NULL;
147 ntlm_cred->pwlen = 0;
148 ntlm_cred->no_cached_credentials = 0;
150 phCredential->dwUpper = fCredentialUse;
151 phCredential->dwLower = (ULONG_PTR)ntlm_cred;
152 ret = SEC_E_OK;
153 break;
155 case SECPKG_CRED_OUTBOUND:
156 auth_data = pAuthData;
157 if (!(ntlm_cred = heap_alloc(sizeof(*ntlm_cred)))) return SEC_E_INSUFFICIENT_MEMORY;
159 ntlm_cred->mode = NTLM_CLIENT;
160 ntlm_cred->username_arg = NULL;
161 ntlm_cred->domain_arg = NULL;
162 ntlm_cred->password = NULL;
163 ntlm_cred->pwlen = 0;
164 ntlm_cred->no_cached_credentials = 0;
166 if (pAuthData)
168 int domain_len = 0, user_len = 0, password_len = 0;
170 if (auth_data->Flags & SEC_WINNT_AUTH_IDENTITY_ANSI)
172 if (auth_data->DomainLength)
174 domain_len = MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->Domain,
175 auth_data->DomainLength, NULL, 0);
176 if (!(domain = heap_alloc(sizeof(WCHAR) * domain_len))) goto done;
177 MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->Domain, auth_data->DomainLength,
178 domain, domain_len);
180 if (auth_data->UserLength)
182 user_len = MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->User,
183 auth_data->UserLength, NULL, 0);
184 if (!(user = heap_alloc(sizeof(WCHAR) * user_len))) goto done;
185 MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->User, auth_data->UserLength,
186 user, user_len);
188 if (auth_data->PasswordLength)
190 password_len = MultiByteToWideChar(CP_ACP, 0,(char *)auth_data->Password,
191 auth_data->PasswordLength, NULL, 0);
192 if (!(password = heap_alloc(sizeof(WCHAR) * password_len))) goto done;
193 MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->Password, auth_data->PasswordLength,
194 password, password_len);
197 else
199 domain = auth_data->Domain;
200 domain_len = auth_data->DomainLength;
202 user = auth_data->User;
203 user_len = auth_data->UserLength;
205 password = auth_data->Password;
206 password_len = auth_data->PasswordLength;
209 TRACE("Username is %s\n", debugstr_wn(user, user_len));
210 TRACE("Domain name is %s\n", debugstr_wn(domain, domain_len));
212 ntlm_cred->username_arg = ntlm_GetUsernameArg(user, user_len);
213 ntlm_cred->domain_arg = ntlm_GetDomainArg(domain, domain_len);
215 if (password_len)
217 ntlm_cred->pwlen = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, password,
218 password_len, NULL, 0, NULL, NULL);
219 if (!(ntlm_cred->password = heap_alloc(ntlm_cred->pwlen))) goto done;
220 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, password, password_len,
221 ntlm_cred->password, ntlm_cred->pwlen, NULL, NULL);
225 phCredential->dwUpper = fCredentialUse;
226 phCredential->dwLower = (ULONG_PTR)ntlm_cred;
227 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n", phCredential->dwUpper,
228 phCredential->dwLower);
229 ret = SEC_E_OK;
230 break;
232 case SECPKG_CRED_BOTH:
233 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
234 ret = SEC_E_UNSUPPORTED_FUNCTION;
235 break;
237 default:
238 ret = SEC_E_UNKNOWN_CREDENTIALS;
241 done:
242 if (auth_data && (auth_data->Flags & SEC_WINNT_AUTH_IDENTITY_ANSI))
244 heap_free(domain);
245 heap_free(user);
246 heap_free(password);
248 if (ret != SEC_E_OK) heap_free( ntlm_cred );
250 return ret;
253 /***********************************************************************
254 * AcquireCredentialsHandleA
256 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
257 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
258 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
259 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
261 SECURITY_STATUS ret = SEC_E_INSUFFICIENT_MEMORY;
262 int user_sizeW, domain_sizeW, passwd_sizeW;
263 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
264 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
265 PSEC_WINNT_AUTH_IDENTITY_A id = NULL;
267 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
268 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
269 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
271 if (pszPackage)
273 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, NULL, 0);
274 if (!(package = heap_alloc(package_sizeW * sizeof(SEC_WCHAR)))) return SEC_E_INSUFFICIENT_MEMORY;
275 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
278 if (pAuthData)
280 id = pAuthData;
281 if (id->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
283 if (!(pAuthDataW = heap_alloc(sizeof(SEC_WINNT_AUTH_IDENTITY_W)))) goto done;
285 if (!id->UserLength) user_sizeW = 0;
286 else
288 user_sizeW = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)id->User, id->UserLength, NULL, 0);
289 if (!(user = heap_alloc(user_sizeW * sizeof(SEC_WCHAR)))) goto done;
290 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)id->User, id->UserLength, user, user_sizeW);
293 if (!id->DomainLength) domain_sizeW = 0;
294 else
296 domain_sizeW = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)id->Domain, id->DomainLength, NULL, 0);
297 if (!(domain = heap_alloc(domain_sizeW * sizeof(SEC_WCHAR)))) goto done;
298 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)id->Domain, id->DomainLength, domain, domain_sizeW);
301 if (!id->PasswordLength) passwd_sizeW = 0;
302 else
304 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)id->Password, id->PasswordLength, NULL, 0);
305 if (!(passwd = heap_alloc(passwd_sizeW * sizeof(SEC_WCHAR)))) goto done;
306 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)id->Password, id->PasswordLength, passwd, passwd_sizeW);
309 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
310 pAuthDataW->User = user;
311 pAuthDataW->UserLength = user_sizeW;
312 pAuthDataW->Domain = domain;
313 pAuthDataW->DomainLength = domain_sizeW;
314 pAuthDataW->Password = passwd;
315 pAuthDataW->PasswordLength = passwd_sizeW;
317 else pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)id;
320 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
321 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
322 ptsExpiry);
324 done:
325 heap_free(package);
326 heap_free(user);
327 heap_free(domain);
328 heap_free(passwd);
329 if (pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)id) heap_free(pAuthDataW);
331 return ret;
334 /*************************************************************************
335 * ntlm_GetTokenBufferIndex
336 * Calculates the index of the secbuffer with BufferType == SECBUFFER_TOKEN
337 * Returns index if found or -1 if not found.
339 static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)
341 UINT i;
343 TRACE("%p\n", pMessage);
345 for( i = 0; i < pMessage->cBuffers; ++i )
347 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
348 return i;
351 return -1;
354 /*************************************************************************
355 * ntlm_GetDataBufferIndex
356 * Calculates the index of the first secbuffer with BufferType == SECBUFFER_DATA
357 * Returns index if found or -1 if not found.
359 static int ntlm_GetDataBufferIndex(PSecBufferDesc pMessage)
361 UINT i;
363 TRACE("%p\n", pMessage);
365 for( i = 0; i < pMessage->cBuffers; ++i )
367 if(pMessage->pBuffers[i].BufferType == SECBUFFER_DATA)
368 return i;
371 return -1;
374 static BOOL ntlm_GetCachedCredential(const SEC_WCHAR *pszTargetName, PCREDENTIALW *cred)
376 LPCWSTR p;
377 LPCWSTR pszHost;
378 LPWSTR pszHostOnly;
379 BOOL ret;
381 if (!pszTargetName)
382 return FALSE;
384 /* try to get the start of the hostname from service principal name (SPN) */
385 pszHost = strchrW(pszTargetName, '/');
386 if (pszHost)
388 /* skip slash character */
389 pszHost++;
391 /* find end of host by detecting start of instance port or start of referrer */
392 p = strchrW(pszHost, ':');
393 if (!p)
394 p = strchrW(pszHost, '/');
395 if (!p)
396 p = pszHost + strlenW(pszHost);
398 else /* otherwise not an SPN, just a host */
400 pszHost = pszTargetName;
401 p = pszHost + strlenW(pszHost);
404 if (!(pszHostOnly = heap_alloc((p - pszHost + 1) * sizeof(WCHAR)))) return FALSE;
405 memcpy(pszHostOnly, pszHost, (p - pszHost) * sizeof(WCHAR));
406 pszHostOnly[p - pszHost] = '\0';
408 ret = CredReadW(pszHostOnly, CRED_TYPE_DOMAIN_PASSWORD, 0, cred);
410 heap_free(pszHostOnly);
411 return ret;
414 /***********************************************************************
415 * InitializeSecurityContextW
417 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
418 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
419 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
420 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
421 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
423 SECURITY_STATUS ret;
424 PNtlmCredentials ntlm_cred;
425 PNegoHelper helper = NULL;
426 ULONG ctxt_attr = 0;
427 char* buffer, *want_flags = NULL;
428 PBYTE bin;
429 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
430 int token_idx;
431 SEC_CHAR *username = NULL;
432 SEC_CHAR *domain = NULL;
433 SEC_CHAR *password = NULL;
435 TRACE("%p %p %s 0x%08x %d %d %p %d %p %p %p %p\n", phCredential, phContext,
436 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
437 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
439 /****************************************
440 * When communicating with the client, there can be the
441 * following reply packets:
442 * YR <base64 blob> should be sent to the server
443 * PW should be sent back to helper with
444 * base64 encoded password
445 * AF <base64 blob> client is done, blob should be
446 * sent to server with KK prefixed
447 * GF <string list> A string list of negotiated flags
448 * GK <base64 blob> base64 encoded session key
449 * BH <char reason> something broke
451 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
453 if(TargetDataRep == SECURITY_NETWORK_DREP){
454 TRACE("Setting SECURITY_NETWORK_DREP\n");
457 buffer = heap_alloc(sizeof(char) * NTLM_MAX_BUF);
458 bin = heap_alloc(sizeof(BYTE) * NTLM_MAX_BUF);
460 if((phContext == NULL) && (pInput == NULL))
462 static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
463 static CHAR credentials_argv[] = "--use-cached-creds";
464 SEC_CHAR *client_argv[5];
465 int pwlen = 0;
467 TRACE("First time in ISC()\n");
469 if(!phCredential)
471 ret = SEC_E_INVALID_HANDLE;
472 goto isc_end;
475 /* As the server side of sspi never calls this, make sure that
476 * the handler is a client handler.
478 ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
479 if(ntlm_cred->mode != NTLM_CLIENT)
481 TRACE("Cred mode = %d\n", ntlm_cred->mode);
482 ret = SEC_E_INVALID_HANDLE;
483 goto isc_end;
486 client_argv[0] = ntlm_auth;
487 client_argv[1] = helper_protocol;
488 if (!ntlm_cred->username_arg && !ntlm_cred->domain_arg)
490 LPWKSTA_USER_INFO_1 ui = NULL;
491 NET_API_STATUS status;
492 PCREDENTIALW cred;
494 if (ntlm_GetCachedCredential(pszTargetName, &cred))
496 LPWSTR p;
497 p = strchrW(cred->UserName, '\\');
498 if (p)
500 domain = ntlm_GetDomainArg(cred->UserName, p - cred->UserName);
501 p++;
503 else
505 domain = ntlm_GetDomainArg(NULL, 0);
506 p = cred->UserName;
509 username = ntlm_GetUsernameArg(p, -1);
511 if(cred->CredentialBlobSize != 0)
513 pwlen = WideCharToMultiByte(CP_UNIXCP,
514 WC_NO_BEST_FIT_CHARS, (LPWSTR)cred->CredentialBlob,
515 cred->CredentialBlobSize / sizeof(WCHAR), NULL, 0,
516 NULL, NULL);
518 password = heap_alloc(pwlen);
519 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
520 (LPWSTR)cred->CredentialBlob,
521 cred->CredentialBlobSize / sizeof(WCHAR),
522 password, pwlen, NULL, NULL);
525 CredFree(cred);
527 client_argv[2] = username;
528 client_argv[3] = domain;
529 client_argv[4] = NULL;
531 else
533 status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
534 if (status != NERR_Success || ui == NULL || ntlm_cred->no_cached_credentials)
536 ret = SEC_E_NO_CREDENTIALS;
537 goto isc_end;
539 username = ntlm_GetUsernameArg(ui->wkui1_username, -1);
540 NetApiBufferFree(ui);
542 TRACE("using cached credentials\n");
544 client_argv[2] = username;
545 client_argv[3] = credentials_argv;
546 client_argv[4] = NULL;
549 else
551 client_argv[2] = ntlm_cred->username_arg;
552 client_argv[3] = ntlm_cred->domain_arg;
553 client_argv[4] = NULL;
556 if((ret = fork_helper(&helper, ntlm_auth, client_argv)) != SEC_E_OK)
557 goto isc_end;
559 helper->mode = NTLM_CLIENT;
560 helper->session_key = heap_alloc(16);
561 if (!helper->session_key)
563 cleanup_helper(helper);
564 ret = SEC_E_INSUFFICIENT_MEMORY;
565 goto isc_end;
568 /* Generate the dummy session key = MD4(MD4(password))*/
569 if(password || ntlm_cred->password)
571 SEC_WCHAR *unicode_password;
572 int passwd_lenW;
574 TRACE("Converting password to unicode.\n");
575 passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
576 password ? password : ntlm_cred->password,
577 password ? pwlen : ntlm_cred->pwlen,
578 NULL, 0);
579 unicode_password = heap_alloc(passwd_lenW * sizeof(SEC_WCHAR));
580 MultiByteToWideChar(CP_ACP, 0, password ? password : ntlm_cred->password,
581 password ? pwlen : ntlm_cred->pwlen, unicode_password, passwd_lenW);
583 SECUR32_CreateNTLM1SessionKey((PBYTE)unicode_password,
584 passwd_lenW * sizeof(SEC_WCHAR), helper->session_key);
585 heap_free(unicode_password);
587 else
588 memset(helper->session_key, 0, 16);
590 /* Allocate space for a maximal string of
591 * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
592 * NTLMSSP_FEATURE_SESSION_KEY"
594 if (!(want_flags = heap_alloc(73)))
596 cleanup_helper(helper);
597 ret = SEC_E_INSUFFICIENT_MEMORY;
598 goto isc_end;
600 lstrcpyA(want_flags, "SF");
601 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
603 if(strstr(want_flags, "NTLMSSP_FEATURE_SEAL") == NULL)
604 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
606 if(fContextReq & ISC_REQ_CONNECTION)
607 ctxt_attr |= ISC_RET_CONNECTION;
608 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
609 ctxt_attr |= ISC_RET_EXTENDED_ERROR;
610 if(fContextReq & ISC_REQ_INTEGRITY)
612 if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
613 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
615 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
616 ctxt_attr |= ISC_RET_MUTUAL_AUTH;
617 if(fContextReq & ISC_REQ_REPLAY_DETECT)
619 if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
620 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
622 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
624 if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
625 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
627 if(fContextReq & ISC_REQ_STREAM)
628 FIXME("ISC_REQ_STREAM\n");
629 if(fContextReq & ISC_REQ_USE_DCE_STYLE)
630 ctxt_attr |= ISC_RET_USED_DCE_STYLE;
631 if(fContextReq & ISC_REQ_DELEGATE)
632 ctxt_attr |= ISC_RET_DELEGATE;
634 /* If no password is given, try to use cached credentials. Fall back to an empty
635 * password if this failed. */
636 if(!password && !ntlm_cred->password)
638 lstrcpynA(buffer, "OK", max_len-1);
639 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
641 cleanup_helper(helper);
642 goto isc_end;
644 /* If the helper replied with "PW", using cached credentials failed */
645 if(!strncmp(buffer, "PW", 2))
647 TRACE("Using cached credentials failed.\n");
648 lstrcpynA(buffer, "PW AA==", max_len-1);
650 else /* Just do a noop on the next run */
651 lstrcpynA(buffer, "OK", max_len-1);
653 else
655 lstrcpynA(buffer, "PW ", max_len-1);
656 if((ret = encodeBase64(password ? (unsigned char *)password : (unsigned char *)ntlm_cred->password,
657 password ? pwlen : ntlm_cred->pwlen, buffer+3,
658 max_len-3, &buffer_len)) != SEC_E_OK)
660 cleanup_helper(helper);
661 goto isc_end;
666 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
667 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
669 cleanup_helper(helper);
670 goto isc_end;
673 TRACE("Helper returned %s\n", debugstr_a(buffer));
675 if(lstrlenA(want_flags) > 2)
677 TRACE("Want flags are %s\n", debugstr_a(want_flags));
678 lstrcpynA(buffer, want_flags, max_len-1);
679 if((ret = run_helper(helper, buffer, max_len, &buffer_len))
680 != SEC_E_OK)
682 cleanup_helper(helper);
683 goto isc_end;
685 if(!strncmp(buffer, "BH", 2))
686 ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
689 lstrcpynA(buffer, "YR", max_len-1);
691 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
693 cleanup_helper(helper);
694 goto isc_end;
697 TRACE("%s\n", buffer);
699 if(strncmp(buffer, "YR ", 3) != 0)
701 /* Something borked */
702 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
703 ret = SEC_E_INTERNAL_ERROR;
704 cleanup_helper(helper);
705 goto isc_end;
707 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
708 max_len-1, &bin_len)) != SEC_E_OK)
710 cleanup_helper(helper);
711 goto isc_end;
714 /* put the decoded client blob into the out buffer */
716 phNewContext->dwUpper = ctxt_attr;
717 phNewContext->dwLower = (ULONG_PTR)helper;
719 ret = SEC_I_CONTINUE_NEEDED;
721 else
723 int input_token_idx;
725 /* handle second call here */
726 /* encode server data to base64 */
727 if (!pInput || ((input_token_idx = ntlm_GetTokenBufferIndex(pInput)) == -1))
729 ret = SEC_E_INVALID_TOKEN;
730 goto isc_end;
733 if(!phContext)
735 ret = SEC_E_INVALID_HANDLE;
736 goto isc_end;
739 /* As the server side of sspi never calls this, make sure that
740 * the handler is a client handler.
742 helper = (PNegoHelper)phContext->dwLower;
743 if(helper->mode != NTLM_CLIENT)
745 TRACE("Helper mode = %d\n", helper->mode);
746 ret = SEC_E_INVALID_HANDLE;
747 goto isc_end;
750 if (!pInput->pBuffers[input_token_idx].pvBuffer)
752 ret = SEC_E_INTERNAL_ERROR;
753 goto isc_end;
756 if(pInput->pBuffers[input_token_idx].cbBuffer > max_len)
758 TRACE("pInput->pBuffers[%d].cbBuffer is: %d\n",
759 input_token_idx,
760 pInput->pBuffers[input_token_idx].cbBuffer);
761 ret = SEC_E_INVALID_TOKEN;
762 goto isc_end;
764 else
765 bin_len = pInput->pBuffers[input_token_idx].cbBuffer;
767 memcpy(bin, pInput->pBuffers[input_token_idx].pvBuffer, bin_len);
769 lstrcpynA(buffer, "TT ", max_len-1);
771 if((ret = encodeBase64(bin, bin_len, buffer+3,
772 max_len-3, &buffer_len)) != SEC_E_OK)
773 goto isc_end;
775 TRACE("Server sent: %s\n", debugstr_a(buffer));
777 /* send TT base64 blob to ntlm_auth */
778 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
779 goto isc_end;
781 TRACE("Helper replied: %s\n", debugstr_a(buffer));
783 if( (strncmp(buffer, "KK ", 3) != 0) &&
784 (strncmp(buffer, "AF ", 3) !=0))
786 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
787 ret = SEC_E_INVALID_TOKEN;
788 goto isc_end;
791 /* decode the blob and send it to server */
792 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
793 &bin_len)) != SEC_E_OK)
795 goto isc_end;
798 phNewContext->dwUpper = ctxt_attr;
799 phNewContext->dwLower = (ULONG_PTR)helper;
801 ret = SEC_E_OK;
804 /* put the decoded client blob into the out buffer */
806 if (!pOutput || ((token_idx = ntlm_GetTokenBufferIndex(pOutput)) == -1))
808 TRACE("no SECBUFFER_TOKEN buffer could be found\n");
809 ret = SEC_E_BUFFER_TOO_SMALL;
810 if ((phContext == NULL) && (pInput == NULL))
812 cleanup_helper(helper);
813 phNewContext->dwUpper = 0;
814 phNewContext->dwLower = 0;
816 goto isc_end;
819 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
821 pOutput->pBuffers[token_idx].pvBuffer = heap_alloc(bin_len);
822 pOutput->pBuffers[token_idx].cbBuffer = bin_len;
824 else if (pOutput->pBuffers[token_idx].cbBuffer < bin_len)
826 TRACE("out buffer is NULL or has not enough space\n");
827 ret = SEC_E_BUFFER_TOO_SMALL;
828 if ((phContext == NULL) && (pInput == NULL))
830 cleanup_helper(helper);
831 phNewContext->dwUpper = 0;
832 phNewContext->dwLower = 0;
834 goto isc_end;
837 if (!pOutput->pBuffers[token_idx].pvBuffer)
839 TRACE("out buffer is NULL\n");
840 ret = SEC_E_INTERNAL_ERROR;
841 if ((phContext == NULL) && (pInput == NULL))
843 cleanup_helper(helper);
844 phNewContext->dwUpper = 0;
845 phNewContext->dwLower = 0;
847 goto isc_end;
850 pOutput->pBuffers[token_idx].cbBuffer = bin_len;
851 memcpy(pOutput->pBuffers[token_idx].pvBuffer, bin, bin_len);
853 if(ret == SEC_E_OK)
855 TRACE("Getting negotiated flags\n");
856 lstrcpynA(buffer, "GF", max_len - 1);
857 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
858 goto isc_end;
860 if(buffer_len < 3)
862 TRACE("No flags negotiated.\n");
863 helper->neg_flags = 0l;
865 else
867 TRACE("Negotiated %s\n", debugstr_a(buffer));
868 sscanf(buffer + 3, "%x", &(helper->neg_flags));
869 TRACE("Stored 0x%08x as flags\n", helper->neg_flags);
872 TRACE("Getting session key\n");
873 lstrcpynA(buffer, "GK", max_len - 1);
874 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
875 goto isc_end;
877 if(strncmp(buffer, "BH", 2) == 0)
878 TRACE("No key negotiated.\n");
879 else if(strncmp(buffer, "GK ", 3) == 0)
881 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
882 &bin_len)) != SEC_E_OK)
884 TRACE("Failed to decode session key\n");
886 TRACE("Session key is %s\n", debugstr_a(buffer+3));
887 heap_free(helper->session_key);
888 if (!(helper->session_key = heap_alloc(bin_len)))
890 ret = SEC_E_INSUFFICIENT_MEMORY;
891 goto isc_end;
893 memcpy(helper->session_key, bin, bin_len);
896 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
897 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
898 helper->crypt.ntlm.seq_num = 0l;
899 SECUR32_CreateNTLM2SubKeys(helper);
900 helper->crypt.ntlm2.send_a4i = SECUR32_arc4Alloc();
901 helper->crypt.ntlm2.recv_a4i = SECUR32_arc4Alloc();
902 SECUR32_arc4Init(helper->crypt.ntlm2.send_a4i,
903 helper->crypt.ntlm2.send_seal_key, 16);
904 SECUR32_arc4Init(helper->crypt.ntlm2.recv_a4i,
905 helper->crypt.ntlm2.recv_seal_key, 16);
906 helper->crypt.ntlm2.send_seq_no = 0l;
907 helper->crypt.ntlm2.recv_seq_no = 0l;
910 isc_end:
911 heap_free(username);
912 heap_free(domain);
913 heap_free(password);
914 heap_free(want_flags);
915 heap_free(buffer);
916 heap_free(bin);
917 return ret;
920 /***********************************************************************
921 * InitializeSecurityContextA
923 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
924 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
925 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
926 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
927 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
929 SECURITY_STATUS ret;
930 SEC_WCHAR *target = NULL;
932 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
933 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
934 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
936 if (pszTargetName)
938 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, NULL, 0);
939 if (!(target = heap_alloc(target_size * sizeof(SEC_WCHAR)))) return SEC_E_INSUFFICIENT_MEMORY;
940 MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, target, target_size);
943 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
944 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
945 phNewContext, pOutput, pfContextAttr, ptsExpiry);
947 heap_free(target);
948 return ret;
951 /***********************************************************************
952 * AcceptSecurityContext
954 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
955 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
956 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
957 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
959 SECURITY_STATUS ret;
960 char *buffer, *want_flags = NULL;
961 PBYTE bin;
962 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
963 ULONG ctxt_attr = 0;
964 PNegoHelper helper;
965 PNtlmCredentials ntlm_cred;
967 TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
968 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
969 ptsExpiry);
971 buffer = heap_alloc(sizeof(char) * NTLM_MAX_BUF);
972 bin = heap_alloc(sizeof(BYTE) * NTLM_MAX_BUF);
974 if(TargetDataRep == SECURITY_NETWORK_DREP){
975 TRACE("Using SECURITY_NETWORK_DREP\n");
978 if(phContext == NULL)
980 static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp";
981 SEC_CHAR *server_argv[] = { ntlm_auth,
982 server_helper_protocol,
983 NULL };
985 if (!phCredential)
987 ret = SEC_E_INVALID_HANDLE;
988 goto asc_end;
991 ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
993 if(ntlm_cred->mode != NTLM_SERVER)
995 ret = SEC_E_INVALID_HANDLE;
996 goto asc_end;
999 /* This is the first call to AcceptSecurityHandle */
1000 if(pInput == NULL)
1002 ret = SEC_E_INCOMPLETE_MESSAGE;
1003 goto asc_end;
1006 if(pInput->cBuffers < 1)
1008 ret = SEC_E_INCOMPLETE_MESSAGE;
1009 goto asc_end;
1012 if(pInput->pBuffers[0].cbBuffer > max_len)
1014 ret = SEC_E_INVALID_TOKEN;
1015 goto asc_end;
1017 else
1018 bin_len = pInput->pBuffers[0].cbBuffer;
1020 if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
1021 SEC_E_OK)
1023 ret = SEC_E_INTERNAL_ERROR;
1024 goto asc_end;
1026 helper->mode = NTLM_SERVER;
1028 /* Handle all the flags */
1029 if (!(want_flags = heap_alloc(73)))
1031 TRACE("Failed to allocate memory for the want_flags!\n");
1032 ret = SEC_E_INSUFFICIENT_MEMORY;
1033 cleanup_helper(helper);
1034 goto asc_end;
1036 lstrcpyA(want_flags, "SF");
1037 if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
1039 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
1041 if(fContextReq & ASC_REQ_CONFIDENTIALITY)
1043 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
1045 if(fContextReq & ASC_REQ_CONNECTION)
1047 /* This is default, so we'll enable it */
1048 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
1049 ctxt_attr |= ASC_RET_CONNECTION;
1051 if(fContextReq & ASC_REQ_EXTENDED_ERROR)
1053 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
1055 if(fContextReq & ASC_REQ_INTEGRITY)
1057 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
1059 if(fContextReq & ASC_REQ_MUTUAL_AUTH)
1061 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
1063 if(fContextReq & ASC_REQ_REPLAY_DETECT)
1065 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
1067 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
1069 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
1071 if(fContextReq & ISC_REQ_STREAM)
1073 FIXME("ASC_REQ_STREAM stub\n");
1075 /* Done with the flags */
1077 if(lstrlenA(want_flags) > 3)
1079 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
1080 lstrcpynA(buffer, want_flags, max_len - 1);
1081 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1082 SEC_E_OK)
1084 cleanup_helper(helper);
1085 goto asc_end;
1087 if(!strncmp(buffer, "BH", 2))
1088 TRACE("Helper doesn't understand new command set\n");
1091 /* This is the YR request from the client, encode to base64 */
1093 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1095 lstrcpynA(buffer, "YR ", max_len-1);
1097 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1098 &buffer_len)) != SEC_E_OK)
1100 cleanup_helper(helper);
1101 goto asc_end;
1104 TRACE("Client sent: %s\n", debugstr_a(buffer));
1106 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1107 SEC_E_OK)
1109 cleanup_helper(helper);
1110 goto asc_end;
1113 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1114 /* The expected answer is TT <base64 blob> */
1116 if(strncmp(buffer, "TT ", 3) != 0)
1118 ret = SEC_E_INTERNAL_ERROR;
1119 cleanup_helper(helper);
1120 goto asc_end;
1123 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1124 &bin_len)) != SEC_E_OK)
1126 cleanup_helper(helper);
1127 goto asc_end;
1130 /* send this to the client */
1131 if(pOutput == NULL)
1133 ret = SEC_E_INSUFFICIENT_MEMORY;
1134 cleanup_helper(helper);
1135 goto asc_end;
1138 if(pOutput->cBuffers < 1)
1140 ret = SEC_E_INSUFFICIENT_MEMORY;
1141 cleanup_helper(helper);
1142 goto asc_end;
1145 pOutput->pBuffers[0].cbBuffer = bin_len;
1146 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
1147 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
1148 ret = SEC_I_CONTINUE_NEEDED;
1151 else
1153 /* we expect a KK request from client */
1154 if(pInput == NULL)
1156 ret = SEC_E_INCOMPLETE_MESSAGE;
1157 goto asc_end;
1160 if(pInput->cBuffers < 1)
1162 ret = SEC_E_INCOMPLETE_MESSAGE;
1163 goto asc_end;
1166 helper = (PNegoHelper)phContext->dwLower;
1168 if(helper->mode != NTLM_SERVER)
1170 ret = SEC_E_INVALID_HANDLE;
1171 goto asc_end;
1174 if(pInput->pBuffers[0].cbBuffer > max_len)
1176 ret = SEC_E_INVALID_TOKEN;
1177 goto asc_end;
1179 else
1180 bin_len = pInput->pBuffers[0].cbBuffer;
1182 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1184 lstrcpynA(buffer, "KK ", max_len-1);
1186 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1187 &buffer_len)) != SEC_E_OK)
1189 goto asc_end;
1192 TRACE("Client sent: %s\n", debugstr_a(buffer));
1194 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1195 SEC_E_OK)
1197 goto asc_end;
1200 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1202 /* At this point, we get a NA if the user didn't authenticate, but a BH
1203 * if ntlm_auth could not connect to winbindd. Apart from running Wine
1204 * as root, there is no way to fix this for now, so just handle this as
1205 * a failed login. */
1206 if(strncmp(buffer, "AF ", 3) != 0)
1208 if(strncmp(buffer, "NA ", 3) == 0)
1210 ret = SEC_E_LOGON_DENIED;
1211 goto asc_end;
1213 else
1215 size_t ntlm_pipe_err_v3_len = strlen("BH NT_STATUS_ACCESS_DENIED");
1216 size_t ntlm_pipe_err_v4_len = strlen("BH NT_STATUS_UNSUCCESSFUL");
1218 if( (buffer_len >= ntlm_pipe_err_v3_len &&
1219 strncmp(buffer, "BH NT_STATUS_ACCESS_DENIED", ntlm_pipe_err_v3_len) == 0) ||
1220 (buffer_len >= ntlm_pipe_err_v4_len &&
1221 strncmp(buffer, "BH NT_STATUS_UNSUCCESSFUL", ntlm_pipe_err_v4_len) == 0) )
1223 TRACE("Connection to winbindd failed\n");
1224 ret = SEC_E_LOGON_DENIED;
1226 else
1227 ret = SEC_E_INTERNAL_ERROR;
1229 goto asc_end;
1232 pOutput->pBuffers[0].cbBuffer = 0;
1234 TRACE("Getting negotiated flags\n");
1235 lstrcpynA(buffer, "GF", max_len - 1);
1236 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1237 goto asc_end;
1239 if(buffer_len < 3)
1241 TRACE("No flags negotiated, or helper does not support GF command\n");
1243 else
1245 TRACE("Negotiated %s\n", debugstr_a(buffer));
1246 sscanf(buffer + 3, "%x", &(helper->neg_flags));
1247 TRACE("Stored 0x%08x as flags\n", helper->neg_flags);
1250 TRACE("Getting session key\n");
1251 lstrcpynA(buffer, "GK", max_len - 1);
1252 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1253 goto asc_end;
1255 if(buffer_len < 3)
1256 TRACE("Helper does not support GK command\n");
1257 else
1259 if(strncmp(buffer, "BH ", 3) == 0)
1261 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1262 heap_free(helper->session_key);
1263 if (!(helper->session_key = heap_alloc(16)))
1265 ret = SEC_E_INSUFFICIENT_MEMORY;
1266 goto asc_end;
1268 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1269 memset(helper->session_key, 0 , 16);
1271 else if(strncmp(buffer, "GK ", 3) == 0)
1273 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1274 &bin_len)) != SEC_E_OK)
1276 TRACE("Failed to decode session key\n");
1278 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1279 heap_free(helper->session_key);
1280 if (!(helper->session_key = heap_alloc(16)))
1282 ret = SEC_E_INSUFFICIENT_MEMORY;
1283 goto asc_end;
1285 memcpy(helper->session_key, bin, 16);
1288 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1289 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1290 helper->crypt.ntlm.seq_num = 0l;
1293 phNewContext->dwUpper = ctxt_attr;
1294 phNewContext->dwLower = (ULONG_PTR)helper;
1296 asc_end:
1297 heap_free(want_flags);
1298 heap_free(buffer);
1299 heap_free(bin);
1300 return ret;
1303 /***********************************************************************
1304 * CompleteAuthToken
1306 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1307 PSecBufferDesc pToken)
1309 /* We never need to call CompleteAuthToken anyway */
1310 TRACE("%p %p\n", phContext, pToken);
1311 if (!phContext)
1312 return SEC_E_INVALID_HANDLE;
1314 return SEC_E_OK;
1317 /***********************************************************************
1318 * DeleteSecurityContext
1320 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1322 PNegoHelper helper;
1324 TRACE("%p\n", phContext);
1325 if (!phContext)
1326 return SEC_E_INVALID_HANDLE;
1328 helper = (PNegoHelper)phContext->dwLower;
1330 phContext->dwUpper = 0;
1331 phContext->dwLower = 0;
1333 SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1334 SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1335 SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1336 heap_free(helper->crypt.ntlm2.send_sign_key);
1337 heap_free(helper->crypt.ntlm2.send_seal_key);
1338 heap_free(helper->crypt.ntlm2.recv_sign_key);
1339 heap_free(helper->crypt.ntlm2.recv_seal_key);
1341 cleanup_helper(helper);
1343 return SEC_E_OK;
1346 #define NTLM_COMMENT \
1347 { 'N', 'T', 'L', 'M', ' ', \
1348 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1349 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1351 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1352 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1354 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1356 static char ntlm_name_A[] = NTLM_NAME;
1357 static WCHAR ntlm_name_W[] = NTLM_NAME;
1359 #define NTLM_CAPS ( \
1360 SECPKG_FLAG_INTEGRITY | \
1361 SECPKG_FLAG_PRIVACY | \
1362 SECPKG_FLAG_TOKEN_ONLY | \
1363 SECPKG_FLAG_CONNECTION | \
1364 SECPKG_FLAG_MULTI_REQUIRED | \
1365 SECPKG_FLAG_IMPERSONATION | \
1366 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1367 SECPKG_FLAG_NEGOTIABLE | \
1368 SECPKG_FLAG_LOGON | \
1369 SECPKG_FLAG_RESTRICTED_TOKENS )
1371 static const SecPkgInfoW infoW = {
1372 NTLM_CAPS,
1374 RPC_C_AUTHN_WINNT,
1375 NTLM_MAX_BUF,
1376 ntlm_name_W,
1377 ntlm_comment_W
1380 static const SecPkgInfoA infoA = {
1381 NTLM_CAPS,
1383 RPC_C_AUTHN_WINNT,
1384 NTLM_MAX_BUF,
1385 ntlm_name_A,
1386 ntlm_comment_A
1389 SecPkgInfoA *ntlm_package_infoA = (SecPkgInfoA *)&infoA;
1390 SecPkgInfoW *ntlm_package_infoW = (SecPkgInfoW *)&infoW;
1392 static SecPkgInfoW *build_package_infoW( const SecPkgInfoW *info )
1394 SecPkgInfoW *ret;
1395 DWORD size_name = (strlenW(info->Name) + 1) * sizeof(WCHAR);
1396 DWORD size_comment = (strlenW(info->Comment) + 1) * sizeof(WCHAR);
1398 if (!(ret = heap_alloc( sizeof(*ret) + size_name + size_comment ))) return NULL;
1399 ret->fCapabilities = info->fCapabilities;
1400 ret->wVersion = info->wVersion;
1401 ret->wRPCID = info->wRPCID;
1402 ret->cbMaxToken = info->cbMaxToken;
1403 ret->Name = (SEC_WCHAR *)(ret + 1);
1404 memcpy( ret->Name, info->Name, size_name );
1405 ret->Comment = (SEC_WCHAR *)((char *)ret->Name + size_name);
1406 memcpy( ret->Comment, info->Comment, size_comment );
1407 return ret;
1410 static SecPkgInfoA *build_package_infoA( const SecPkgInfoA *info )
1412 SecPkgInfoA *ret;
1413 DWORD size_name = strlen(info->Name) + 1, size_comment = strlen(info->Comment) + 1;
1415 if (!(ret = heap_alloc( sizeof(*ret) + size_name + size_comment ))) return NULL;
1416 ret->fCapabilities = info->fCapabilities;
1417 ret->wVersion = info->wVersion;
1418 ret->wRPCID = info->wRPCID;
1419 ret->cbMaxToken = info->cbMaxToken;
1420 ret->Name = (SEC_CHAR *)(ret + 1);
1421 memcpy( ret->Name, info->Name, size_name );
1422 ret->Comment = ret->Name + size_name;
1423 memcpy( ret->Comment, info->Comment, size_comment );
1424 return ret;
1427 /***********************************************************************
1428 * QueryContextAttributesW
1430 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1431 ULONG ulAttribute, void *pBuffer)
1433 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1434 if (!phContext)
1435 return SEC_E_INVALID_HANDLE;
1437 switch(ulAttribute)
1439 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1440 _x(SECPKG_ATTR_ACCESS_TOKEN);
1441 _x(SECPKG_ATTR_AUTHORITY);
1442 _x(SECPKG_ATTR_DCE_INFO);
1443 case SECPKG_ATTR_FLAGS:
1445 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1446 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1448 spcf->Flags = 0;
1449 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1450 spcf->Flags |= ISC_RET_INTEGRITY;
1451 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1452 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1453 return SEC_E_OK;
1455 _x(SECPKG_ATTR_KEY_INFO);
1456 _x(SECPKG_ATTR_LIFESPAN);
1457 _x(SECPKG_ATTR_NAMES);
1458 _x(SECPKG_ATTR_NATIVE_NAMES);
1459 case SECPKG_ATTR_NEGOTIATION_INFO:
1461 SecPkgContext_NegotiationInfoW *info = (SecPkgContext_NegotiationInfoW *)pBuffer;
1462 if (!(info->PackageInfo = build_package_infoW( &infoW ))) return SEC_E_INSUFFICIENT_MEMORY;
1463 info->NegotiationState = SECPKG_NEGOTIATION_COMPLETE;
1464 return SEC_E_OK;
1466 _x(SECPKG_ATTR_PACKAGE_INFO);
1467 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1468 _x(SECPKG_ATTR_SESSION_KEY);
1469 case SECPKG_ATTR_SIZES:
1471 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1472 spcs->cbMaxToken = NTLM_MAX_BUF;
1473 spcs->cbMaxSignature = 16;
1474 spcs->cbBlockSize = 0;
1475 spcs->cbSecurityTrailer = 16;
1476 return SEC_E_OK;
1478 _x(SECPKG_ATTR_STREAM_SIZES);
1479 _x(SECPKG_ATTR_TARGET_INFORMATION);
1480 #undef _x
1481 default:
1482 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1485 return SEC_E_UNSUPPORTED_FUNCTION;
1488 /***********************************************************************
1489 * QueryContextAttributesA
1491 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1492 ULONG ulAttribute, void *pBuffer)
1494 switch(ulAttribute)
1496 case SECPKG_ATTR_NEGOTIATION_INFO:
1498 SecPkgContext_NegotiationInfoA *info = (SecPkgContext_NegotiationInfoA *)pBuffer;
1499 if (!(info->PackageInfo = build_package_infoA( &infoA ))) return SEC_E_INSUFFICIENT_MEMORY;
1500 info->NegotiationState = SECPKG_NEGOTIATION_COMPLETE;
1501 return SEC_E_OK;
1503 default:
1504 return ntlm_QueryContextAttributesW( phContext, ulAttribute, pBuffer );
1508 /***********************************************************************
1509 * ImpersonateSecurityContext
1511 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1513 SECURITY_STATUS ret;
1515 TRACE("%p\n", phContext);
1516 if (phContext)
1518 ret = SEC_E_UNSUPPORTED_FUNCTION;
1520 else
1522 ret = SEC_E_INVALID_HANDLE;
1524 return ret;
1527 /***********************************************************************
1528 * RevertSecurityContext
1530 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1532 SECURITY_STATUS ret;
1534 TRACE("%p\n", phContext);
1535 if (phContext)
1537 ret = SEC_E_UNSUPPORTED_FUNCTION;
1539 else
1541 ret = SEC_E_INVALID_HANDLE;
1543 return ret;
1546 /***********************************************************************
1547 * ntlm_CreateSignature
1548 * As both MakeSignature and VerifySignature need this, but different keys
1549 * are needed for NTLM2, the logic goes into a helper function.
1550 * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1551 * signing/encrypting and NTLM_RECV for verifying/decrypting. When encrypting,
1552 * the signature is encrypted after the message was encrypted, so
1553 * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1554 * false.
1556 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1557 int token_idx, SignDirection direction, BOOL encrypt_sig)
1559 ULONG sign_version = 1;
1560 UINT i;
1561 PBYTE sig;
1562 TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1563 encrypt_sig);
1565 sig = pMessage->pBuffers[token_idx].pvBuffer;
1567 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1568 helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1570 BYTE digest[16];
1571 BYTE seq_no[4];
1572 HMAC_MD5_CTX hmac_md5_ctx;
1574 TRACE("Signing NTLM2 style\n");
1576 if(direction == NTLM_SEND)
1578 seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
1579 seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
1580 seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1581 seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1583 ++(helper->crypt.ntlm2.send_seq_no);
1585 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1587 else
1589 seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
1590 seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
1591 seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1592 seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1594 ++(helper->crypt.ntlm2.recv_seq_no);
1596 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1599 HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1600 for( i = 0; i < pMessage->cBuffers; ++i )
1602 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1603 HMACMD5Update(&hmac_md5_ctx, pMessage->pBuffers[i].pvBuffer,
1604 pMessage->pBuffers[i].cbBuffer);
1607 HMACMD5Final(&hmac_md5_ctx, digest);
1609 if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1611 if(direction == NTLM_SEND)
1612 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1613 else
1614 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1617 /* The NTLM2 signature is the sign version */
1618 sig[ 0] = (sign_version >> 0) & 0xff;
1619 sig[ 1] = (sign_version >> 8) & 0xff;
1620 sig[ 2] = (sign_version >> 16) & 0xff;
1621 sig[ 3] = (sign_version >> 24) & 0xff;
1622 /* The first 8 bytes of the digest */
1623 memcpy(sig+4, digest, 8);
1624 /* And the sequence number */
1625 memcpy(sig+12, seq_no, 4);
1627 pMessage->pBuffers[token_idx].cbBuffer = 16;
1629 return SEC_E_OK;
1631 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1633 ULONG crc = 0U;
1634 TRACE("Signing NTLM1 style\n");
1636 for(i=0; i < pMessage->cBuffers; ++i)
1638 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1640 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1641 pMessage->pBuffers[i].cbBuffer, crc);
1645 sig[ 0] = (sign_version >> 0) & 0xff;
1646 sig[ 1] = (sign_version >> 8) & 0xff;
1647 sig[ 2] = (sign_version >> 16) & 0xff;
1648 sig[ 3] = (sign_version >> 24) & 0xff;
1649 memset(sig+4, 0, 4);
1650 sig[ 8] = (crc >> 0) & 0xff;
1651 sig[ 9] = (crc >> 8) & 0xff;
1652 sig[10] = (crc >> 16) & 0xff;
1653 sig[11] = (crc >> 24) & 0xff;
1654 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1655 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1656 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1657 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1659 ++(helper->crypt.ntlm.seq_num);
1661 if(encrypt_sig)
1662 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1663 return SEC_E_OK;
1666 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1668 TRACE("Creating a dummy signature.\n");
1669 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1670 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1671 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1672 pMessage->pBuffers[token_idx].cbBuffer = 16;
1673 return SEC_E_OK;
1676 return SEC_E_UNSUPPORTED_FUNCTION;
1679 /***********************************************************************
1680 * MakeSignature
1682 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext,
1683 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1685 PNegoHelper helper;
1686 int token_idx;
1688 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1689 if (!phContext)
1690 return SEC_E_INVALID_HANDLE;
1692 if(fQOP)
1693 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1695 if(MessageSeqNo)
1696 FIXME("Ignoring MessageSeqNo\n");
1698 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1699 return SEC_E_INVALID_TOKEN;
1701 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1702 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1703 return SEC_E_INVALID_TOKEN;
1705 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1706 return SEC_E_BUFFER_TOO_SMALL;
1708 helper = (PNegoHelper)phContext->dwLower;
1709 TRACE("Negotiated flags are: 0x%08x\n", helper->neg_flags);
1711 return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1714 /***********************************************************************
1715 * VerifySignature
1717 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1718 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1720 PNegoHelper helper;
1721 UINT i;
1722 int token_idx;
1723 SECURITY_STATUS ret;
1724 SecBufferDesc local_desc;
1725 PSecBuffer local_buff;
1726 BYTE local_sig[16];
1728 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1729 if(!phContext)
1730 return SEC_E_INVALID_HANDLE;
1732 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1733 return SEC_E_INVALID_TOKEN;
1735 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1736 return SEC_E_INVALID_TOKEN;
1738 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1739 return SEC_E_BUFFER_TOO_SMALL;
1741 if(MessageSeqNo)
1742 FIXME("Ignoring MessageSeqNo\n");
1744 helper = (PNegoHelper)phContext->dwLower;
1745 TRACE("Negotiated flags: 0x%08x\n", helper->neg_flags);
1747 local_buff = heap_alloc(pMessage->cBuffers * sizeof(SecBuffer));
1749 local_desc.ulVersion = SECBUFFER_VERSION;
1750 local_desc.cBuffers = pMessage->cBuffers;
1751 local_desc.pBuffers = local_buff;
1753 for(i=0; i < pMessage->cBuffers; ++i)
1755 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1757 local_buff[i].BufferType = SECBUFFER_TOKEN;
1758 local_buff[i].cbBuffer = 16;
1759 local_buff[i].pvBuffer = local_sig;
1761 else
1763 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1764 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1765 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1769 if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1770 return ret;
1772 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1773 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1774 ret = SEC_E_MESSAGE_ALTERED;
1775 else
1776 ret = SEC_E_OK;
1778 heap_free(local_buff);
1779 return ret;
1783 /***********************************************************************
1784 * FreeCredentialsHandle
1786 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(PCredHandle phCredential)
1788 if (phCredential)
1790 PNtlmCredentials ntlm_cred = (PNtlmCredentials) phCredential->dwLower;
1791 phCredential->dwUpper = 0;
1792 phCredential->dwLower = 0;
1793 if (ntlm_cred->password) memset(ntlm_cred->password, 0, ntlm_cred->pwlen);
1794 heap_free(ntlm_cred->password);
1795 heap_free(ntlm_cred->username_arg);
1796 heap_free(ntlm_cred->domain_arg);
1797 heap_free(ntlm_cred);
1800 return SEC_E_OK;
1803 /***********************************************************************
1804 * EncryptMessage
1806 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1807 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1809 PNegoHelper helper;
1810 int token_idx, data_idx;
1812 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1814 if(!phContext)
1815 return SEC_E_INVALID_HANDLE;
1817 if(fQOP)
1818 FIXME("Ignoring fQOP\n");
1820 if(MessageSeqNo)
1821 FIXME("Ignoring MessageSeqNo\n");
1823 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1824 return SEC_E_INVALID_TOKEN;
1826 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1827 return SEC_E_INVALID_TOKEN;
1829 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1 )
1830 return SEC_E_INVALID_TOKEN;
1832 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1833 return SEC_E_BUFFER_TOO_SMALL;
1835 helper = (PNegoHelper) phContext->dwLower;
1837 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1838 helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1840 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1841 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1842 pMessage->pBuffers[data_idx].pvBuffer,
1843 pMessage->pBuffers[data_idx].cbBuffer);
1845 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1846 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1847 ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1849 else
1851 PBYTE sig;
1852 ULONG save_flags;
1854 /* EncryptMessage always produces real signatures, so make sure
1855 * NTLMSSP_NEGOTIATE_SIGN is set*/
1856 save_flags = helper->neg_flags;
1857 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1858 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1859 helper->neg_flags = save_flags;
1861 sig = pMessage->pBuffers[token_idx].pvBuffer;
1863 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1864 pMessage->pBuffers[data_idx].pvBuffer,
1865 pMessage->pBuffers[data_idx].cbBuffer);
1866 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1868 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1869 memset(sig+4, 0, 4);
1871 return SEC_E_OK;
1874 /***********************************************************************
1875 * DecryptMessage
1877 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1878 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1880 SECURITY_STATUS ret;
1881 ULONG ntlmssp_flags_save;
1882 PNegoHelper helper;
1883 int token_idx, data_idx;
1884 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1886 if(!phContext)
1887 return SEC_E_INVALID_HANDLE;
1889 if(MessageSeqNo)
1890 FIXME("Ignoring MessageSeqNo\n");
1892 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1893 return SEC_E_INVALID_TOKEN;
1895 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1896 return SEC_E_INVALID_TOKEN;
1898 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1)
1899 return SEC_E_INVALID_TOKEN;
1901 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1902 return SEC_E_BUFFER_TOO_SMALL;
1904 helper = (PNegoHelper) phContext->dwLower;
1906 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1908 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1909 pMessage->pBuffers[data_idx].pvBuffer,
1910 pMessage->pBuffers[data_idx].cbBuffer);
1912 else
1914 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1915 pMessage->pBuffers[data_idx].pvBuffer,
1916 pMessage->pBuffers[data_idx].cbBuffer);
1919 /* Make sure we use a session key for the signature check, EncryptMessage
1920 * always does that, even in the dummy case */
1921 ntlmssp_flags_save = helper->neg_flags;
1923 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1924 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1926 helper->neg_flags = ntlmssp_flags_save;
1928 return ret;
1931 static const SecurityFunctionTableA ntlmTableA = {
1933 NULL, /* EnumerateSecurityPackagesA */
1934 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1935 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1936 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1937 NULL, /* Reserved2 */
1938 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1939 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1940 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1941 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1942 NULL, /* ApplyControlToken */
1943 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1944 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1945 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1946 ntlm_MakeSignature, /* MakeSignature */
1947 ntlm_VerifySignature, /* VerifySignature */
1948 FreeContextBuffer, /* FreeContextBuffer */
1949 NULL, /* QuerySecurityPackageInfoA */
1950 NULL, /* Reserved3 */
1951 NULL, /* Reserved4 */
1952 NULL, /* ExportSecurityContext */
1953 NULL, /* ImportSecurityContextA */
1954 NULL, /* AddCredentialsA */
1955 NULL, /* Reserved8 */
1956 NULL, /* QuerySecurityContextToken */
1957 ntlm_EncryptMessage, /* EncryptMessage */
1958 ntlm_DecryptMessage, /* DecryptMessage */
1959 NULL, /* SetContextAttributesA */
1962 static const SecurityFunctionTableW ntlmTableW = {
1964 NULL, /* EnumerateSecurityPackagesW */
1965 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1966 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1967 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1968 NULL, /* Reserved2 */
1969 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1970 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1971 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1972 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1973 NULL, /* ApplyControlToken */
1974 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1975 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1976 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1977 ntlm_MakeSignature, /* MakeSignature */
1978 ntlm_VerifySignature, /* VerifySignature */
1979 FreeContextBuffer, /* FreeContextBuffer */
1980 NULL, /* QuerySecurityPackageInfoW */
1981 NULL, /* Reserved3 */
1982 NULL, /* Reserved4 */
1983 NULL, /* ExportSecurityContext */
1984 NULL, /* ImportSecurityContextW */
1985 NULL, /* AddCredentialsW */
1986 NULL, /* Reserved8 */
1987 NULL, /* QuerySecurityContextToken */
1988 ntlm_EncryptMessage, /* EncryptMessage */
1989 ntlm_DecryptMessage, /* DecryptMessage */
1990 NULL, /* SetContextAttributesW */
1993 void SECUR32_initNTLMSP(void)
1995 PNegoHelper helper;
1996 static CHAR version[] = "--version";
1998 SEC_CHAR *args[] = {
1999 ntlm_auth,
2000 version,
2001 NULL };
2003 if(fork_helper(&helper, ntlm_auth, args) != SEC_E_OK)
2004 helper = NULL;
2005 else
2006 check_version(helper);
2008 if( helper &&
2009 ((helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
2010 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
2011 helper->minor > MIN_NTLM_AUTH_MINOR_VERSION) ||
2012 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
2013 helper->minor == MIN_NTLM_AUTH_MINOR_VERSION &&
2014 helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION)) )
2016 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
2017 SECUR32_addPackages(provider, 1L, ntlm_package_infoA, ntlm_package_infoW);
2019 else
2021 ERR_(winediag)("%s was not found or is outdated. "
2022 "Make sure that ntlm_auth >= %d.%d.%d is in your path. "
2023 "Usually, you can find it in the winbind package of your distribution.\n",
2024 ntlm_auth,
2025 MIN_NTLM_AUTH_MAJOR_VERSION,
2026 MIN_NTLM_AUTH_MINOR_VERSION,
2027 MIN_NTLM_AUTH_MICRO_VERSION);
2030 cleanup_helper(helper);