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.
30 #include "secur32_priv.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
36 #define NTLM_MAX_BUF 1904
37 #define MIN_NTLM_AUTH_MAJOR_VERSION 3
38 #define MIN_NTLM_AUTH_MINOR_VERSION 0
39 #define MIN_NTLM_AUTH_MICRO_VERSION 25
41 static CHAR ntlm_auth[] = "ntlm_auth";
43 /***********************************************************************
44 * QueryCredentialsAttributesA
46 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
47 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
51 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
53 if(ulAttribute == SECPKG_ATTR_NAMES)
55 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
56 ret = SEC_E_UNSUPPORTED_FUNCTION;
59 ret = SEC_E_UNSUPPORTED_FUNCTION;
64 /***********************************************************************
65 * QueryCredentialsAttributesW
67 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
68 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
72 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
74 if(ulAttribute == SECPKG_ATTR_NAMES)
76 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
77 ret = SEC_E_UNSUPPORTED_FUNCTION;
80 ret = SEC_E_UNSUPPORTED_FUNCTION;
85 /***********************************************************************
86 * AcquireCredentialsHandleW
88 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
89 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
90 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
91 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
94 PNegoHelper helper = NULL;
95 static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp",
96 credentials_argv[] = "--use-cached-creds";
98 SEC_CHAR *client_user_arg = NULL;
99 SEC_CHAR *client_domain_arg = NULL;
100 SEC_WCHAR *username = NULL, *domain = NULL;
102 SEC_CHAR *client_argv[6];
103 SEC_CHAR *server_argv[] = { ntlm_auth,
104 server_helper_protocol,
107 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
108 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
109 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
112 switch(fCredentialUse)
114 case SECPKG_CRED_INBOUND:
115 if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
123 helper->mode = NTLM_SERVER;
124 phCredential->dwUpper = fCredentialUse;
125 phCredential->dwLower = (ULONG_PTR)helper;
129 case SECPKG_CRED_OUTBOUND:
131 static const char username_arg[] = "--username=";
132 static const char domain_arg[] = "--domain=";
133 static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
136 if(pAuthData == NULL)
138 LPWKSTA_USER_INFO_1 ui = NULL;
139 NET_API_STATUS status;
141 status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
142 if (status != NERR_Success || ui == NULL)
144 ret = SEC_E_NO_CREDENTIALS;
149 username = HeapAlloc(GetProcessHeap(), 0,
150 (lstrlenW(ui->wkui1_username)+1) *
152 lstrcpyW(username, ui->wkui1_username);
154 /* same for the domain */
155 domain = HeapAlloc(GetProcessHeap(), 0,
156 (lstrlenW(ui->wkui1_logon_domain)+1) *
158 lstrcpyW(domain, ui->wkui1_logon_domain);
159 NetApiBufferFree(ui);
163 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
164 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
166 if (!auth_data->UserLength || !auth_data->DomainLength)
168 ret = SEC_E_NO_CREDENTIALS;
172 /* Get username and domain from pAuthData */
173 username = HeapAlloc(GetProcessHeap(), 0,
174 (auth_data->UserLength + 1) * sizeof(SEC_WCHAR));
175 lstrcpyW(username, auth_data->User);
177 domain = HeapAlloc(GetProcessHeap(), 0,
178 (auth_data->DomainLength + 1) * sizeof(SEC_WCHAR));
179 lstrcpyW(domain, auth_data->Domain);
181 TRACE("Username is %s\n", debugstr_w(username));
182 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
183 username, -1, NULL, 0, NULL, NULL) + sizeof(username_arg);
184 client_user_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
185 lstrcpyA(client_user_arg, username_arg);
186 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, username, -1,
187 client_user_arg + sizeof(username_arg) - 1,
188 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
190 TRACE("Domain name is %s\n", debugstr_w(domain));
191 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
192 domain, -1, NULL, 0, NULL, NULL) + sizeof(domain_arg);
193 client_domain_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
194 lstrcpyA(client_domain_arg, domain_arg);
195 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domain,
196 -1, client_domain_arg + sizeof(domain_arg) - 1,
197 unixcp_size - sizeof(domain) + 1, NULL, NULL);
199 client_argv[0] = ntlm_auth;
200 client_argv[1] = helper_protocol;
201 client_argv[2] = client_user_arg;
202 client_argv[3] = client_domain_arg;
203 client_argv[4] = credentials_argv;
204 client_argv[5] = NULL;
206 if((ret = fork_helper(&helper, ntlm_auth, client_argv)) !=
214 helper->mode = NTLM_CLIENT;
216 if(pAuthData != NULL)
218 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
219 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
221 if(auth_data->PasswordLength != 0)
223 helper->pwlen = WideCharToMultiByte(CP_UNIXCP,
224 WC_NO_BEST_FIT_CHARS, auth_data->Password,
225 auth_data->PasswordLength+1, NULL, 0, NULL,
228 helper->password = HeapAlloc(GetProcessHeap(), 0,
231 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
232 auth_data->Password, auth_data->PasswordLength+1,
233 helper->password, helper->pwlen, NULL, NULL);
237 phCredential->dwUpper = fCredentialUse;
238 phCredential->dwLower = (ULONG_PTR)helper;
239 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
240 phCredential->dwUpper, phCredential->dwLower);
245 case SECPKG_CRED_BOTH:
246 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
247 ret = SEC_E_UNSUPPORTED_FUNCTION;
252 ret = SEC_E_UNKNOWN_CREDENTIALS;
256 HeapFree(GetProcessHeap(), 0, client_user_arg);
257 HeapFree(GetProcessHeap(), 0, client_domain_arg);
258 HeapFree(GetProcessHeap(), 0, username);
259 HeapFree(GetProcessHeap(), 0, domain);
264 /***********************************************************************
265 * AcquireCredentialsHandleA
267 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
268 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
269 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
270 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
273 int user_sizeW, domain_sizeW, passwd_sizeW;
275 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
277 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
278 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
280 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
281 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
282 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
284 if(pszPackage != NULL)
286 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
289 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
291 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
295 if(pAuthData != NULL)
297 identity = (PSEC_WINNT_AUTH_IDENTITY_A)pAuthData;
299 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
301 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
302 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
304 if(identity->UserLength != 0)
306 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
307 (LPCSTR)identity->User, identity->UserLength+1, NULL, 0);
308 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
310 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
311 identity->UserLength+1, user, user_sizeW);
318 if(identity->DomainLength != 0)
320 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
321 (LPCSTR)identity->Domain, identity->DomainLength+1, NULL, 0);
322 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
323 * sizeof(SEC_WCHAR));
324 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
325 identity->DomainLength+1, domain, domain_sizeW);
332 if(identity->PasswordLength != 0)
334 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
335 (LPCSTR)identity->Password, identity->PasswordLength,
337 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
338 * sizeof(SEC_WCHAR));
339 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
340 identity->PasswordLength, passwd, passwd_sizeW);
347 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
348 pAuthDataW->User = user;
349 pAuthDataW->UserLength = user_sizeW;
350 pAuthDataW->Domain = domain;
351 pAuthDataW->DomainLength = domain_sizeW;
352 pAuthDataW->Password = passwd;
353 pAuthDataW->PasswordLength = passwd_sizeW;
357 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
361 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
362 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
365 HeapFree(GetProcessHeap(), 0, package);
366 HeapFree(GetProcessHeap(), 0, user);
367 HeapFree(GetProcessHeap(), 0, domain);
368 HeapFree(GetProcessHeap(), 0, passwd);
369 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
370 HeapFree(GetProcessHeap(), 0, pAuthDataW);
375 /***********************************************************************
376 * InitializeSecurityContextW
378 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
379 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
380 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
381 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
382 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
387 char* buffer, *want_flags = NULL;
389 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
391 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
392 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
393 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
396 return SEC_E_INVALID_HANDLE;
398 /* As the server side of sspi never calls this, make sure that
399 * the handler is a client handler.
401 helper = (PNegoHelper)phCredential->dwLower;
402 if(helper->mode != NTLM_CLIENT)
404 TRACE("Helper mode = %d\n", helper->mode);
405 return SEC_E_INVALID_HANDLE;
408 /****************************************
409 * When communicating with the client, there can be the
410 * following reply packets:
411 * YR <base64 blob> should be sent to the server
412 * PW should be sent back to helper with
413 * base64 encoded password
414 * AF <base64 blob> client is done, blob should be
415 * sent to server with KK prefixed
416 * GF <string list> A string list of negotiated flags
417 * GK <base64 blob> base64 encoded session key
418 * BH <char reason> something broke
420 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
424 TRACE("According to a MS whitepaper pszTargetName is ignored.\n");
427 if(TargetDataRep == SECURITY_NETWORK_DREP){
428 TRACE("Setting SECURITY_NETWORK_DREP\n");
431 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
432 bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
434 if((phContext == NULL) && (pInput == NULL))
436 TRACE("First time in ISC()\n");
437 /* Allocate space for a maximal string of
438 * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
439 * NTLMSSP_FEATURE_SESSION_KEY"
441 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
442 if(want_flags == NULL)
444 ret = SEC_E_INSUFFICIENT_MEMORY;
447 lstrcpyA(want_flags, "SF");
448 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
451 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SEAL")) == NULL)
452 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
454 if(fContextReq & ISC_REQ_CONNECTION)
455 ctxt_attr |= ISC_RET_CONNECTION;
456 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
457 ctxt_attr |= ISC_RET_EXTENDED_ERROR;
458 if(fContextReq & ISC_REQ_INTEGRITY)
461 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
462 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
464 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
465 ctxt_attr |= ISC_RET_MUTUAL_AUTH;
466 if(fContextReq & ISC_REQ_REPLAY_DETECT)
469 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
470 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
472 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
475 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
476 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
478 if(fContextReq & ISC_REQ_STREAM)
479 FIXME("ISC_REQ_STREAM\n");
480 if(fContextReq & ISC_REQ_USE_DCE_STYLE)
481 ctxt_attr |= ISC_RET_USED_DCE_STYLE;
482 if(fContextReq & ISC_REQ_DELEGATE)
483 ctxt_attr |= ISC_RET_DELEGATE;
485 /* If no password is given, try to use cached credentials. Fall back to an empty
486 * password if this failed. */
487 if(helper->password == NULL)
489 lstrcpynA(buffer, "OK", max_len-1);
490 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
492 /* If the helper replied with "PW", using cached credentials failed */
493 if(!strncmp(buffer, "PW", 2))
495 TRACE("Using cached credentials failed. Using empty password.\n");
496 lstrcpynA(buffer, "PW AA==", max_len-1);
498 else /* Just do a noop on the next run */
499 lstrcpynA(buffer, "OK", max_len-1);
503 lstrcpynA(buffer, "PW ", max_len-1);
504 if((ret = encodeBase64((unsigned char*)helper->password,
505 helper->pwlen-2, buffer+3,
506 max_len-3, &buffer_len)) != SEC_E_OK)
508 TRACE("Deleting password!\n");
509 memset(helper->password, 0, helper->pwlen-2);
510 HeapFree(GetProcessHeap(), 0, helper->password);
516 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
517 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
520 TRACE("Helper returned %s\n", debugstr_a(buffer));
522 if(lstrlenA(want_flags) > 2)
524 TRACE("Want flags are %s\n", debugstr_a(want_flags));
525 lstrcpynA(buffer, want_flags, max_len-1);
526 if((ret = run_helper(helper, buffer, max_len, &buffer_len))
529 if(!strncmp(buffer, "BH", 2))
530 ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
533 lstrcpynA(buffer, "YR", max_len-1);
535 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
538 TRACE("%s\n", buffer);
540 if(strncmp(buffer, "YR ", 3) != 0)
542 /* Something borked */
543 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
544 ret = SEC_E_INTERNAL_ERROR;
547 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
548 max_len-1, &bin_len)) != SEC_E_OK)
551 /* put the decoded client blob into the out buffer */
553 ret = SEC_I_CONTINUE_NEEDED;
557 /* handle second call here */
558 /* encode server data to base64 */
559 if (!pInput || !pInput->cBuffers)
561 ret = SEC_E_INCOMPLETE_MESSAGE;
565 if (!pInput->pBuffers[0].pvBuffer)
567 ret = SEC_E_INTERNAL_ERROR;
571 if(pInput->pBuffers[0].cbBuffer > max_len)
573 TRACE("pInput->pBuffers[0].cbBuffer is: %ld\n",
574 pInput->pBuffers[0].cbBuffer);
575 ret = SEC_E_INVALID_TOKEN;
579 bin_len = pInput->pBuffers[0].cbBuffer;
581 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
583 lstrcpynA(buffer, "TT ", max_len-1);
585 if((ret = encodeBase64(bin, bin_len, buffer+3,
586 max_len-3, &buffer_len)) != SEC_E_OK)
589 TRACE("Server sent: %s\n", debugstr_a(buffer));
591 /* send TT base64 blob to ntlm_auth */
592 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
595 TRACE("Helper replied: %s\n", debugstr_a(buffer));
597 if( (strncmp(buffer, "KK ", 3) != 0) &&
598 (strncmp(buffer, "AF ", 3) !=0))
600 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
601 ret = SEC_E_INVALID_TOKEN;
605 /* decode the blob and send it to server */
606 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
607 &bin_len)) != SEC_E_OK)
612 phNewContext->dwUpper = ctxt_attr;
613 phNewContext->dwLower = (ULONG_PTR)helper;
618 /* put the decoded client blob into the out buffer */
620 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
624 pOutput->cBuffers = 1;
625 pOutput->pBuffers[0].pvBuffer = SECUR32_ALLOC(bin_len);
626 pOutput->pBuffers[0].cbBuffer = bin_len;
630 if (!pOutput || !pOutput->cBuffers || pOutput->pBuffers[0].cbBuffer < bin_len)
632 TRACE("out buffer is NULL or has not enough space\n");
633 ret = SEC_E_BUFFER_TOO_SMALL;
637 if (!pOutput->pBuffers[0].pvBuffer)
639 TRACE("out buffer is NULL\n");
640 ret = SEC_E_INTERNAL_ERROR;
644 pOutput->pBuffers[0].cbBuffer = bin_len;
645 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
646 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
650 TRACE("Getting negotiated flags\n");
651 lstrcpynA(buffer, "GF", max_len - 1);
652 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
657 TRACE("No flags negotiated.\n");
658 helper->neg_flags = 0l;
662 TRACE("Negotiated %s\n", debugstr_a(buffer));
663 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
664 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
667 TRACE("Getting session key\n");
668 lstrcpynA(buffer, "GK", max_len - 1);
669 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
672 if(strncmp(buffer, "BH", 2) == 0)
674 TRACE("No key negotiated.\n");
675 helper->valid_session_key = FALSE;
676 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
677 /*Generate the dummy session key = MD4(MD4(password))*/
680 SEC_WCHAR *unicode_password;
683 TRACE("Converting password to unicode.\n");
684 passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
685 (LPCSTR)helper->password, helper->pwlen,
687 unicode_password = HeapAlloc(GetProcessHeap(), 0,
688 passwd_lenW * sizeof(SEC_WCHAR));
689 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)helper->password,
690 helper->pwlen, unicode_password, passwd_lenW);
692 SECUR32_CreateNTLMv1SessionKey((PBYTE)unicode_password,
693 lstrlenW(unicode_password) * sizeof(SEC_WCHAR), helper->session_key);
695 HeapFree(GetProcessHeap(), 0, unicode_password);
698 memset(helper->session_key, 0, 16);
700 else if(strncmp(buffer, "GK ", 3) == 0)
702 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
703 &bin_len)) != SEC_E_OK)
705 TRACE("Failed to decode session key\n");
707 TRACE("Session key is %s\n", debugstr_a(buffer+3));
708 helper->valid_session_key = TRUE;
709 helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
710 if(!helper->session_key)
712 TRACE("Failed to allocate memory for session key\n");
713 ret = SEC_E_INTERNAL_ERROR;
716 memcpy(helper->session_key, bin, bin_len);
719 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
720 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
721 helper->crypt.ntlm.seq_num = 0l;
722 SECUR32_CreateNTLMv2SubKeys(helper);
723 helper->crypt.ntlm2.send_a4i = SECUR32_arc4Alloc();
724 helper->crypt.ntlm2.recv_a4i = SECUR32_arc4Alloc();
725 SECUR32_arc4Init(helper->crypt.ntlm2.send_a4i,
726 (BYTE *)helper->crypt.ntlm2.send_seal_key, 16);
727 SECUR32_arc4Init(helper->crypt.ntlm2.recv_a4i,
728 (BYTE *)helper->crypt.ntlm2.recv_seal_key, 16);
729 helper->crypt.ntlm2.send_seq_no = 0l;
730 helper->crypt.ntlm2.recv_seq_no = 0l;
733 if(ret != SEC_I_CONTINUE_NEEDED)
735 TRACE("Deleting password!\n");
737 memset(helper->password, 0, helper->pwlen-2);
738 HeapFree(GetProcessHeap(), 0, helper->password);
741 HeapFree(GetProcessHeap(), 0, want_flags);
742 HeapFree(GetProcessHeap(), 0, buffer);
743 HeapFree(GetProcessHeap(), 0, bin);
747 /***********************************************************************
748 * InitializeSecurityContextA
750 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
751 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
752 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
753 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
754 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
758 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
759 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
760 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
764 SEC_WCHAR *target = NULL;
765 if(pszTargetName != NULL)
767 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
768 strlen(pszTargetName)+1, NULL, 0);
769 target = HeapAlloc(GetProcessHeap(), 0, target_size *
771 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
772 target, target_size);
775 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
776 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
777 phNewContext, pOutput, pfContextAttr, ptsExpiry);
779 HeapFree(GetProcessHeap(), 0, target);
783 ret = SEC_E_INVALID_HANDLE;
788 /***********************************************************************
789 * AcceptSecurityContext
791 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
792 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
793 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
794 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
797 char *buffer, *want_flags = NULL;
799 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
803 TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
804 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
808 return SEC_E_INVALID_HANDLE;
810 helper = (PNegoHelper)phCredential->dwLower;
812 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
813 bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
815 if(helper->mode != NTLM_SERVER)
817 ret = SEC_E_INVALID_HANDLE;
821 if(TargetDataRep == SECURITY_NETWORK_DREP){
822 TRACE("Using SECURITY_NETWORK_DREP\n");
825 if(phContext == NULL)
827 /* This is the first call to AcceptSecurityHandle */
830 ret = SEC_E_INCOMPLETE_MESSAGE;
834 if(pInput->cBuffers < 1)
836 ret = SEC_E_INCOMPLETE_MESSAGE;
840 if(pInput->pBuffers[0].cbBuffer > max_len)
842 ret = SEC_E_INVALID_TOKEN;
846 bin_len = pInput->pBuffers[0].cbBuffer;
848 /* Handle all the flags */
849 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
850 if(want_flags == NULL)
852 TRACE("Failed to allocate memory for the want_flags!\n");
853 ret = SEC_E_INSUFFICIENT_MEMORY;
856 lstrcpyA(want_flags, "SF");
857 if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
859 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
861 if(fContextReq & ASC_REQ_CONFIDENTIALITY)
863 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
865 if(fContextReq & ASC_REQ_CONNECTION)
867 /* This is default, so we'll enable it */
868 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
869 ctxt_attr |= ASC_RET_CONNECTION;
871 if(fContextReq & ASC_REQ_EXTENDED_ERROR)
873 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
875 if(fContextReq & ASC_REQ_INTEGRITY)
877 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
879 if(fContextReq & ASC_REQ_MUTUAL_AUTH)
881 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
883 if(fContextReq & ASC_REQ_REPLAY_DETECT)
885 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
887 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
889 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
891 if(fContextReq & ISC_REQ_STREAM)
893 FIXME("ASC_REQ_STREAM stub\n");
895 /* Done with the flags */
897 if(lstrlenA(want_flags) > 3)
899 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
900 lstrcpynA(buffer, want_flags, max_len - 1);
901 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
904 if(!strncmp(buffer, "BH", 2))
905 TRACE("Helper doesn't understand new command set\n");
908 /* This is the YR request from the client, encode to base64 */
910 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
912 lstrcpynA(buffer, "YR ", max_len-1);
914 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
915 &buffer_len)) != SEC_E_OK)
920 TRACE("Client sent: %s\n", debugstr_a(buffer));
922 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
928 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
929 /* The expected answer is TT <base64 blob> */
931 if(strncmp(buffer, "TT ", 3) != 0)
933 ret = SEC_E_INTERNAL_ERROR;
937 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
938 &bin_len)) != SEC_E_OK)
943 /* send this to the client */
946 ret = SEC_E_INSUFFICIENT_MEMORY;
950 if(pOutput->cBuffers < 1)
952 ret = SEC_E_INSUFFICIENT_MEMORY;
956 pOutput->pBuffers[0].cbBuffer = bin_len;
957 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
958 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
959 ret = SEC_I_CONTINUE_NEEDED;
964 /* we expect a KK request from client */
967 ret = SEC_E_INCOMPLETE_MESSAGE;
971 if(pInput->cBuffers < 1)
973 ret = SEC_E_INCOMPLETE_MESSAGE;
977 if(pInput->pBuffers[0].cbBuffer > max_len)
979 ret = SEC_E_INVALID_TOKEN;
983 bin_len = pInput->pBuffers[0].cbBuffer;
985 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
987 lstrcpynA(buffer, "KK ", max_len-1);
989 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
990 &buffer_len)) != SEC_E_OK)
995 TRACE("Client sent: %s\n", debugstr_a(buffer));
997 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1003 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1005 if(strncmp(buffer, "AF ", 3) != 0)
1007 if(strncmp(buffer, "NA ", 3) == 0)
1009 ret = SEC_E_LOGON_DENIED;
1014 ret = SEC_E_INTERNAL_ERROR;
1018 pOutput->pBuffers[0].cbBuffer = 0;
1021 TRACE("Getting negotiated flags\n");
1022 lstrcpynA(buffer, "GF", max_len - 1);
1023 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1028 TRACE("No flags negotiated, or helper does not support GF command\n");
1032 TRACE("Negotiated %s\n", debugstr_a(buffer));
1033 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
1034 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
1037 TRACE("Getting session key\n");
1038 lstrcpynA(buffer, "GK", max_len - 1);
1039 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1043 TRACE("Helper does not support GK command\n");
1046 if(strncmp(buffer, "BH ", 3) == 0)
1048 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1049 helper->valid_session_key = FALSE;
1050 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1051 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1052 memset(helper->session_key, 0 , 16);
1054 else if(strncmp(buffer, "GK ", 3) == 0)
1056 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1057 &bin_len)) != SEC_E_OK)
1059 TRACE("Failed to decode session key\n");
1061 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1062 helper->valid_session_key = TRUE;
1063 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1064 if(!helper->session_key)
1066 TRACE("Failed to allocate memory for session key\n");
1067 ret = SEC_E_INTERNAL_ERROR;
1070 memcpy(helper->session_key, bin, 16);
1073 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1074 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1075 helper->crypt.ntlm.seq_num = 0l;
1078 phNewContext->dwUpper = ctxt_attr;
1079 phNewContext->dwLower = (ULONG_PTR)helper;
1082 HeapFree(GetProcessHeap(), 0, want_flags);
1083 HeapFree(GetProcessHeap(), 0, buffer);
1084 HeapFree(GetProcessHeap(), 0, bin);
1088 /***********************************************************************
1091 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1092 PSecBufferDesc pToken)
1094 /* We never need to call CompleteAuthToken anyway */
1095 TRACE("%p %p\n", phContext, pToken);
1097 return SEC_E_INVALID_HANDLE;
1102 /***********************************************************************
1103 * DeleteSecurityContext
1105 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1109 TRACE("%p\n", phContext);
1111 return SEC_E_INVALID_HANDLE;
1113 helper = (PNegoHelper)phContext->dwLower;
1115 phContext->dwUpper = 0;
1116 phContext->dwLower = 0;
1118 SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1119 HeapFree(GetProcessHeap(), 0, helper->session_key);
1120 helper->valid_session_key = FALSE;
1121 SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1122 SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1123 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
1124 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
1125 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
1126 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
1131 /***********************************************************************
1132 * QueryContextAttributesW
1134 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1135 ULONG ulAttribute, void *pBuffer)
1137 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1139 return SEC_E_INVALID_HANDLE;
1143 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1144 _x(SECPKG_ATTR_ACCESS_TOKEN);
1145 _x(SECPKG_ATTR_AUTHORITY);
1146 _x(SECPKG_ATTR_DCE_INFO);
1147 case SECPKG_ATTR_FLAGS:
1149 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1150 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1153 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1154 spcf->Flags |= ISC_RET_INTEGRITY;
1155 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1156 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1159 _x(SECPKG_ATTR_KEY_INFO);
1160 _x(SECPKG_ATTR_LIFESPAN);
1161 _x(SECPKG_ATTR_NAMES);
1162 _x(SECPKG_ATTR_NATIVE_NAMES);
1163 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1164 _x(SECPKG_ATTR_PACKAGE_INFO);
1165 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1166 _x(SECPKG_ATTR_SESSION_KEY);
1167 case SECPKG_ATTR_SIZES:
1169 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1170 spcs->cbMaxToken = NTLM_MAX_BUF;
1171 spcs->cbMaxSignature = 16;
1172 spcs->cbBlockSize = 0;
1173 spcs->cbSecurityTrailer = 16;
1176 _x(SECPKG_ATTR_STREAM_SIZES);
1177 _x(SECPKG_ATTR_TARGET_INFORMATION);
1180 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1183 return SEC_E_UNSUPPORTED_FUNCTION;
1186 /***********************************************************************
1187 * QueryContextAttributesA
1189 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1190 ULONG ulAttribute, void *pBuffer)
1192 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1195 /***********************************************************************
1196 * ImpersonateSecurityContext
1198 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1200 SECURITY_STATUS ret;
1202 TRACE("%p\n", phContext);
1205 ret = SEC_E_UNSUPPORTED_FUNCTION;
1209 ret = SEC_E_INVALID_HANDLE;
1214 /***********************************************************************
1215 * RevertSecurityContext
1217 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1219 SECURITY_STATUS ret;
1221 TRACE("%p\n", phContext);
1224 ret = SEC_E_UNSUPPORTED_FUNCTION;
1228 ret = SEC_E_INVALID_HANDLE;
1233 /*************************************************************************
1234 * ntlm_GetTokenBufferIndex
1235 * Calculates the index of the secbuffer with BufferType == SECBUFFER_TOKEN
1236 * Returns index if found or -1 if not found.
1238 static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)
1242 TRACE("%p\n", pMessage);
1244 for( i = 0; i < pMessage->cBuffers; ++i )
1246 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1253 /***********************************************************************
1254 * ntlm_CreateSignature
1255 * As both MakeSignature and VerifySignature need this, but different keys
1256 * are needed for NTLMv2, the logic goes into a helper function.
1257 * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1258 * signing/encrypting and NTLM_RECV for verfying/decrypting. When encrypting,
1259 * the signature is encrypted after the message was encrypted, so
1260 * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1263 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1264 int token_idx, SignDirection direction, BOOL encrypt_sig)
1266 ULONG sign_version = 1;
1269 TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1272 sig = pMessage->pBuffers[token_idx].pvBuffer;
1274 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1275 helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1279 HMAC_MD5_CTX hmac_md5_ctx;
1281 TRACE("Signing NTLM2 style\n");
1283 if(direction == NTLM_SEND)
1285 seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
1286 seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
1287 seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1288 seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1290 ++(helper->crypt.ntlm2.send_seq_no);
1292 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1296 seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
1297 seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
1298 seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1299 seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1301 ++(helper->crypt.ntlm2.recv_seq_no);
1303 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1306 HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1307 for( i = 0; i < pMessage->cBuffers; ++i )
1309 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1310 HMACMD5Update(&hmac_md5_ctx, (BYTE *)pMessage->pBuffers[i].pvBuffer,
1311 pMessage->pBuffers[i].cbBuffer);
1314 HMACMD5Final(&hmac_md5_ctx, digest);
1316 if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1318 if(direction == NTLM_SEND)
1319 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1321 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1324 /* The NTLM2 signature is the sign version */
1325 sig[ 0] = (sign_version >> 0) & 0xff;
1326 sig[ 1] = (sign_version >> 8) & 0xff;
1327 sig[ 2] = (sign_version >> 16) & 0xff;
1328 sig[ 3] = (sign_version >> 24) & 0xff;
1329 /* The first 8 bytes of the digest */
1330 memcpy(sig+4, digest, 8);
1331 /* And the sequence number */
1332 memcpy(sig+12, seq_no, 4);
1334 pMessage->pBuffers[token_idx].cbBuffer = 16;
1338 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1341 TRACE("Signing NTLM1 style\n");
1343 for(i=0; i < pMessage->cBuffers; ++i)
1345 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1347 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1348 pMessage->pBuffers[i].cbBuffer, crc);
1352 sig[ 0] = (sign_version >> 0) & 0xff;
1353 sig[ 1] = (sign_version >> 8) & 0xff;
1354 sig[ 2] = (sign_version >> 16) & 0xff;
1355 sig[ 3] = (sign_version >> 24) & 0xff;
1356 memset(sig+4, 0, 4);
1357 sig[ 8] = (crc >> 0) & 0xff;
1358 sig[ 9] = (crc >> 8) & 0xff;
1359 sig[10] = (crc >> 16) & 0xff;
1360 sig[11] = (crc >> 24) & 0xff;
1361 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1362 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1363 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1364 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1366 ++(helper->crypt.ntlm.seq_num);
1369 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1373 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1375 TRACE("Creating a dummy signature.\n");
1376 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1377 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1378 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1379 pMessage->pBuffers[token_idx].cbBuffer = 16;
1383 return SEC_E_UNSUPPORTED_FUNCTION;
1386 /***********************************************************************
1389 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1390 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1395 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1397 return SEC_E_INVALID_HANDLE;
1400 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1403 FIXME("Ignoring MessageSeqNo\n");
1405 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1406 return SEC_E_INVALID_TOKEN;
1408 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1409 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1410 return SEC_E_INVALID_TOKEN;
1412 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1413 return SEC_E_BUFFER_TOO_SMALL;
1415 helper = (PNegoHelper)phContext->dwLower;
1416 TRACE("Negotiated flags are: 0x%08lx\n", helper->neg_flags);
1418 return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1421 /***********************************************************************
1424 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1425 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1431 SECURITY_STATUS ret;
1432 SecBufferDesc local_desc;
1433 PSecBuffer local_buff;
1436 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1438 return SEC_E_INVALID_HANDLE;
1440 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1441 return SEC_E_INVALID_TOKEN;
1443 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1444 return SEC_E_INVALID_TOKEN;
1446 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1447 return SEC_E_BUFFER_TOO_SMALL;
1450 FIXME("Ignoring MessageSeqNo\n");
1452 helper = (PNegoHelper)phContext->dwLower;
1453 TRACE("Negotiated flags: 0x%08lx\n", helper->neg_flags);
1455 local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1457 local_desc.ulVersion = SECBUFFER_VERSION;
1458 local_desc.cBuffers = pMessage->cBuffers;
1459 local_desc.pBuffers = local_buff;
1461 for(i=0; i < pMessage->cBuffers; ++i)
1463 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1465 local_buff[i].BufferType = SECBUFFER_TOKEN;
1466 local_buff[i].cbBuffer = 16;
1467 local_buff[i].pvBuffer = local_sig;
1471 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1472 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1473 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1477 if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1480 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1481 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1482 ret = SEC_E_MESSAGE_ALTERED;
1486 HeapFree(GetProcessHeap(), 0, local_buff);
1493 /***********************************************************************
1494 * FreeCredentialsHandle
1496 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1497 PCredHandle phCredential)
1499 SECURITY_STATUS ret;
1502 PNegoHelper helper = (PNegoHelper) phCredential->dwLower;
1503 phCredential->dwUpper = 0;
1504 phCredential->dwLower = 0;
1505 cleanup_helper(helper);
1514 /***********************************************************************
1517 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1518 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1523 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1526 return SEC_E_INVALID_HANDLE;
1529 FIXME("Ignoring fQOP\n");
1532 FIXME("Ignoring MessageSeqNo\n");
1534 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1535 return SEC_E_INVALID_TOKEN;
1537 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1538 return SEC_E_INVALID_TOKEN;
1540 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1541 return SEC_E_BUFFER_TOO_SMALL;
1543 helper = (PNegoHelper) phContext->dwLower;
1545 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1546 helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1548 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1549 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1550 (BYTE *)pMessage->pBuffers[1].pvBuffer,
1551 pMessage->pBuffers[1].cbBuffer);
1553 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1554 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1555 ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1565 /* EncryptMessage always produces real signatures, so make sure
1566 * NTLMSSP_NEGOTIATE_SIGN is set*/
1567 save_flags = helper->neg_flags;
1568 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1569 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1570 helper->neg_flags = save_flags;
1572 sig = pMessage->pBuffers[token_idx].pvBuffer;
1574 SECUR32_arc4Process(helper->crypt.ntlm.a4i, pMessage->pBuffers[1].pvBuffer,
1575 pMessage->pBuffers[1].cbBuffer);
1576 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1578 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1579 memset(sig+4, 0, 4);
1586 /***********************************************************************
1589 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1590 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1592 SECURITY_STATUS ret;
1593 ULONG ntlmssp_flags_save;
1596 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1599 return SEC_E_INVALID_HANDLE;
1602 FIXME("Ignoring MessageSeqNo\n");
1604 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1605 return SEC_E_INVALID_TOKEN;
1607 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1608 return SEC_E_INVALID_TOKEN;
1610 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1611 return SEC_E_BUFFER_TOO_SMALL;
1613 helper = (PNegoHelper) phContext->dwLower;
1615 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1617 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1618 pMessage->pBuffers[1].pvBuffer, pMessage->pBuffers[1].cbBuffer);
1622 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1623 pMessage->pBuffers[1].pvBuffer, pMessage->pBuffers[1].cbBuffer);
1626 /* Make sure we use a session key for the signature check, EncryptMessage
1627 * always does that, even in the dummy case */
1628 ntlmssp_flags_save = helper->neg_flags;
1630 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1631 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1633 helper->neg_flags = ntlmssp_flags_save;
1638 static const SecurityFunctionTableA ntlmTableA = {
1640 NULL, /* EnumerateSecurityPackagesA */
1641 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1642 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1643 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1644 NULL, /* Reserved2 */
1645 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1646 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1647 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1648 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1649 NULL, /* ApplyControlToken */
1650 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1651 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1652 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1653 ntlm_MakeSignature, /* MakeSignature */
1654 ntlm_VerifySignature, /* VerifySignature */
1655 FreeContextBuffer, /* FreeContextBuffer */
1656 NULL, /* QuerySecurityPackageInfoA */
1657 NULL, /* Reserved3 */
1658 NULL, /* Reserved4 */
1659 NULL, /* ExportSecurityContext */
1660 NULL, /* ImportSecurityContextA */
1661 NULL, /* AddCredentialsA */
1662 NULL, /* Reserved8 */
1663 NULL, /* QuerySecurityContextToken */
1664 ntlm_EncryptMessage, /* EncryptMessage */
1665 ntlm_DecryptMessage, /* DecryptMessage */
1666 NULL, /* SetContextAttributesA */
1669 static const SecurityFunctionTableW ntlmTableW = {
1671 NULL, /* EnumerateSecurityPackagesW */
1672 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1673 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1674 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1675 NULL, /* Reserved2 */
1676 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1677 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1678 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1679 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1680 NULL, /* ApplyControlToken */
1681 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1682 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1683 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1684 ntlm_MakeSignature, /* MakeSignature */
1685 ntlm_VerifySignature, /* VerifySignature */
1686 FreeContextBuffer, /* FreeContextBuffer */
1687 NULL, /* QuerySecurityPackageInfoW */
1688 NULL, /* Reserved3 */
1689 NULL, /* Reserved4 */
1690 NULL, /* ExportSecurityContext */
1691 NULL, /* ImportSecurityContextW */
1692 NULL, /* AddCredentialsW */
1693 NULL, /* Reserved8 */
1694 NULL, /* QuerySecurityContextToken */
1695 ntlm_EncryptMessage, /* EncryptMessage */
1696 ntlm_DecryptMessage, /* DecryptMessage */
1697 NULL, /* SetContextAttributesW */
1700 #define NTLM_COMMENT \
1701 { 'N', 'T', 'L', 'M', ' ', \
1702 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1703 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1705 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1706 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1708 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1710 static char ntlm_name_A[] = NTLM_NAME;
1711 static WCHAR ntlm_name_W[] = NTLM_NAME;
1713 /* According to Windows, NTLM has the following capabilities. */
1715 SECPKG_FLAG_INTEGRITY | \
1716 SECPKG_FLAG_PRIVACY | \
1717 SECPKG_FLAG_TOKEN_ONLY | \
1718 SECPKG_FLAG_CONNECTION | \
1719 SECPKG_FLAG_MULTI_REQUIRED | \
1720 SECPKG_FLAG_IMPERSONATION | \
1721 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1722 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1724 static const SecPkgInfoW infoW = {
1733 static const SecPkgInfoA infoA = {
1742 void SECUR32_initNTLMSP(void)
1744 SECURITY_STATUS ret;
1746 static CHAR version[] = "--version";
1748 SEC_CHAR *args[] = {
1753 if((ret = fork_helper(&helper, ntlm_auth, args)) != SEC_E_OK)
1755 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1756 helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1757 helper->major = helper->minor = helper->micro = -1;
1760 check_version(helper);
1762 if( (helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
1763 (helper->major = MIN_NTLM_AUTH_MAJOR_VERSION &&
1764 helper->minor >= MIN_NTLM_AUTH_MINOR_VERSION &&
1765 helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION) )
1767 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1768 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1772 ERR("%s was not found or is outdated. "
1773 "Make sure that ntlm_auth >= %d.%d.%d is in your path.\n",
1775 MIN_NTLM_AUTH_MAJOR_VERSION,
1776 MIN_NTLM_AUTH_MINOR_VERSION,
1777 MIN_NTLM_AUTH_MICRO_VERSION);
1779 cleanup_helper(helper);