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"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
35 #define NTLM_MAX_BUF 1904
38 static CHAR ntlm_auth[] = "ntlm_auth";
40 /***********************************************************************
41 * QueryCredentialsAttributesA
43 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
44 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
48 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
50 if(ulAttribute == SECPKG_ATTR_NAMES)
52 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
53 ret = SEC_E_UNSUPPORTED_FUNCTION;
56 ret = SEC_E_UNSUPPORTED_FUNCTION;
61 /***********************************************************************
62 * QueryCredentialsAttributesW
64 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
65 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
69 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
71 if(ulAttribute == SECPKG_ATTR_NAMES)
73 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
74 ret = SEC_E_UNSUPPORTED_FUNCTION;
77 ret = SEC_E_UNSUPPORTED_FUNCTION;
82 /***********************************************************************
83 * AcquireCredentialsHandleW
85 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
86 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
87 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
88 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
91 PNegoHelper helper = NULL;
92 static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp",
93 credentials_argv[] = "--use-cached-creds";
95 SEC_CHAR *client_user_arg = NULL;
96 SEC_CHAR *client_domain_arg = NULL;
97 SEC_WCHAR *username = NULL, *domain = NULL;
99 SEC_CHAR *client_argv[6];
100 SEC_CHAR *server_argv[] = { ntlm_auth,
101 server_helper_protocol,
104 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
105 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
106 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
109 switch(fCredentialUse)
111 case SECPKG_CRED_INBOUND:
112 if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
120 helper->mode = NTLM_SERVER;
121 phCredential->dwUpper = fCredentialUse;
122 phCredential->dwLower = (ULONG_PTR)helper;
126 case SECPKG_CRED_OUTBOUND:
128 static const char username_arg[] = "--username=";
129 static const char domain_arg[] = "--domain=";
130 static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
133 if(pAuthData == NULL)
135 LPWKSTA_USER_INFO_1 ui = NULL;
136 NET_API_STATUS status;
138 status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
139 if (status != NERR_Success || ui == NULL)
141 ret = SEC_E_NO_CREDENTIALS;
146 username = HeapAlloc(GetProcessHeap(), 0,
147 (lstrlenW(ui->wkui1_username)+1) *
149 lstrcpyW(username, ui->wkui1_username);
151 /* same for the domain */
152 domain = HeapAlloc(GetProcessHeap(), 0,
153 (lstrlenW(ui->wkui1_logon_domain)+1) *
155 lstrcpyW(domain, ui->wkui1_logon_domain);
156 NetApiBufferFree(ui);
160 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
161 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
163 if (!auth_data->UserLength || !auth_data->DomainLength)
165 ret = SEC_E_NO_CREDENTIALS;
169 /* Get username and domain from pAuthData */
170 username = HeapAlloc(GetProcessHeap(), 0,
171 (auth_data->UserLength + 1) * sizeof(SEC_WCHAR));
172 lstrcpyW(username, auth_data->User);
174 domain = HeapAlloc(GetProcessHeap(), 0,
175 (auth_data->DomainLength + 1) * sizeof(SEC_WCHAR));
176 lstrcpyW(domain, auth_data->Domain);
178 TRACE("Username is %s\n", debugstr_w(username));
179 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
180 username, -1, NULL, 0, NULL, NULL) + sizeof(username_arg);
181 client_user_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
182 lstrcpyA(client_user_arg, username_arg);
183 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, username, -1,
184 client_user_arg + sizeof(username_arg) - 1,
185 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
187 TRACE("Domain name is %s\n", debugstr_w(domain));
188 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
189 domain, -1, NULL, 0, NULL, NULL) + sizeof(domain_arg);
190 client_domain_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
191 lstrcpyA(client_domain_arg, domain_arg);
192 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domain,
193 -1, client_domain_arg + sizeof(domain_arg) - 1,
194 unixcp_size - sizeof(domain) + 1, NULL, NULL);
196 client_argv[0] = ntlm_auth;
197 client_argv[1] = helper_protocol;
198 client_argv[2] = client_user_arg;
199 client_argv[3] = client_domain_arg;
200 client_argv[4] = credentials_argv;
201 client_argv[5] = NULL;
203 if((ret = fork_helper(&helper, ntlm_auth, client_argv)) !=
211 helper->mode = NTLM_CLIENT;
213 if(pAuthData != NULL)
215 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
216 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
218 if(auth_data->PasswordLength != 0)
220 helper->pwlen = WideCharToMultiByte(CP_UNIXCP,
221 WC_NO_BEST_FIT_CHARS, auth_data->Password,
222 auth_data->PasswordLength+1, NULL, 0, NULL, NULL);
224 helper->password = HeapAlloc(GetProcessHeap(), 0,
227 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
228 auth_data->Password, auth_data->PasswordLength+1,
229 helper->password, helper->pwlen, NULL, NULL);
233 phCredential->dwUpper = fCredentialUse;
234 phCredential->dwLower = (ULONG_PTR)helper;
235 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
236 phCredential->dwUpper, phCredential->dwLower);
241 case SECPKG_CRED_BOTH:
242 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
243 ret = SEC_E_UNSUPPORTED_FUNCTION;
248 ret = SEC_E_UNKNOWN_CREDENTIALS;
252 HeapFree(GetProcessHeap(), 0, client_user_arg);
253 HeapFree(GetProcessHeap(), 0, client_domain_arg);
254 HeapFree(GetProcessHeap(), 0, username);
255 HeapFree(GetProcessHeap(), 0, domain);
260 /***********************************************************************
261 * AcquireCredentialsHandleA
263 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
264 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
265 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
266 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
269 int user_sizeW, domain_sizeW, passwd_sizeW;
271 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
273 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
274 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
276 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
277 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
278 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
280 if(pszPackage != NULL)
282 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
285 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
287 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
291 if(pAuthData != NULL)
293 identity = (PSEC_WINNT_AUTH_IDENTITY_A)pAuthData;
295 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
297 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
298 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
300 if(identity->UserLength != 0)
302 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
303 (LPCSTR)identity->User, identity->UserLength+1, NULL, 0);
304 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
306 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
307 identity->UserLength+1, user, user_sizeW);
314 if(identity->DomainLength != 0)
316 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
317 (LPCSTR)identity->Domain, identity->DomainLength+1, NULL, 0);
318 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
319 * sizeof(SEC_WCHAR));
320 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
321 identity->DomainLength+1, domain, domain_sizeW);
328 if(identity->PasswordLength != 0)
330 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
331 (LPCSTR)identity->Password, identity->PasswordLength+1,
333 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
334 * sizeof(SEC_WCHAR));
335 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
336 identity->PasswordLength+1, passwd, passwd_sizeW);
343 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
344 pAuthDataW->User = user;
345 pAuthDataW->UserLength = user_sizeW;
346 pAuthDataW->Domain = domain;
347 pAuthDataW->DomainLength = domain_sizeW;
348 pAuthDataW->Password = passwd;
349 pAuthDataW->PasswordLength = passwd_sizeW;
353 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
357 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
358 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
361 HeapFree(GetProcessHeap(), 0, package);
362 HeapFree(GetProcessHeap(), 0, user);
363 HeapFree(GetProcessHeap(), 0, domain);
364 HeapFree(GetProcessHeap(), 0, passwd);
365 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
366 HeapFree(GetProcessHeap(), 0, pAuthDataW);
371 /***********************************************************************
372 * InitializeSecurityContextW
374 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
375 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
376 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
377 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
378 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
383 char* buffer, *want_flags = NULL;
385 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
387 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
388 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
389 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
392 return SEC_E_INVALID_HANDLE;
394 /* As the server side of sspi never calls this, make sure that
395 * the handler is a client handler.
397 helper = (PNegoHelper)phCredential->dwLower;
398 if(helper->mode != NTLM_CLIENT)
400 TRACE("Helper mode = %d\n", helper->mode);
401 return SEC_E_INVALID_HANDLE;
404 /****************************************
405 * When communicating with the client, there can be the
406 * following reply packets:
407 * YR <base64 blob> should be sent to the server
408 * PW should be sent back to helper with
409 * base64 encoded password
410 * AF <base64 blob> client is done, blob should be
411 * sent to server with KK prefixed
412 * GF <string list> A string list of negotiated flags
413 * GK <base64 blob> base64 encoded session key
414 * BH <char reason> something broke
416 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
420 TRACE("According to a MS whitepaper pszTargetName is ignored.\n");
423 if(TargetDataRep == SECURITY_NETWORK_DREP){
424 TRACE("Setting SECURITY_NETWORK_DREP\n");
427 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
428 bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
430 if((phContext == NULL) && (pInput == NULL))
432 TRACE("First time in ISC()\n");
433 /* Allocate space for a maximal string of
434 * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
435 * NTLMSSP_FEATURE_SESSION_KEY"
437 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
438 if(want_flags == NULL)
440 ret = SEC_E_INSUFFICIENT_MEMORY;
443 lstrcpyA(want_flags, "SF");
444 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
445 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
446 if(fContextReq & ISC_REQ_CONNECTION)
448 /* This is default, so we'll enable it */
449 ctxt_attr |= ISC_RET_CONNECTION;
450 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
452 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
453 FIXME("ISC_REQ_EXTENDED_ERROR\n");
454 if(fContextReq & ISC_REQ_INTEGRITY)
455 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
456 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
457 FIXME("ISC_REQ_MUTUAL_AUTH\n");
458 if(fContextReq & ISC_REQ_REPLAY_DETECT)
459 FIXME("ISC_REQ_REPLAY_DETECT\n");
460 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
461 FIXME("ISC_REQ_SEQUENCE_DETECT\n");
462 if(fContextReq & ISC_REQ_STREAM)
463 FIXME("ISC_REQ_STREAM\n");
465 /* If no password is given, try to use cached credentials. Fall back to an empty
466 * password if this failed. */
467 if(helper->password == NULL)
469 lstrcpynA(buffer, "OK", max_len-1);
470 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
472 /* If the helper replied with "PW", using cached credentials failed */
473 if(!strncmp(buffer, "PW", 2))
475 TRACE("Using cached credentials failed. Using empty password.\n");
476 lstrcpynA(buffer, "PW AA==", max_len-1);
478 else /* Just do a noop on the next run */
479 lstrcpynA(buffer, "OK", max_len-1);
483 lstrcpynA(buffer, "PW ", max_len-1);
484 if((ret = encodeBase64((unsigned char*)helper->password,
485 helper->pwlen-2, buffer+3,
486 max_len-3, &buffer_len)) != SEC_E_OK)
488 TRACE("Deleting password!\n");
489 memset(helper->password, 0, helper->pwlen-2);
490 HeapFree(GetProcessHeap(), 0, helper->password);
496 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
497 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
500 TRACE("Helper returned %s\n", debugstr_a(buffer));
502 if(lstrlenA(want_flags) > 2)
504 TRACE("Want flags are '%s'\n", debugstr_a(want_flags));
505 lstrcpynA(buffer, want_flags, max_len-1);
506 if((ret = run_helper(helper, buffer, max_len, &buffer_len))
509 if(!strncmp(buffer, "BH", 2))
510 TRACE("Helper doesn't understand new command set\n");
513 lstrcpynA(buffer, "YR", max_len-1);
515 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
518 TRACE("%s\n", buffer);
520 if(strncmp(buffer, "YR ", 3) != 0)
522 /* Something borked */
523 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
524 ret = SEC_E_INTERNAL_ERROR;
527 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
528 max_len-1, &bin_len)) != SEC_E_OK)
531 /* Mask away the NTLMv2 flag, as well as the key exchange flag */
535 /* put the decoded client blob into the out buffer */
537 ret = SEC_I_CONTINUE_NEEDED;
541 /* handle second call here */
542 /* encode server data to base64 */
543 if (!pInput || !pInput->cBuffers)
545 ret = SEC_E_INCOMPLETE_MESSAGE;
549 if (!pInput->pBuffers[0].pvBuffer)
551 ret = SEC_E_INTERNAL_ERROR;
555 if(pInput->pBuffers[0].cbBuffer > max_len)
557 TRACE("pInput->pBuffers[0].cbBuffer is: %ld\n",
558 pInput->pBuffers[0].cbBuffer);
559 ret = SEC_E_INVALID_TOKEN;
563 bin_len = pInput->pBuffers[0].cbBuffer;
565 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
567 lstrcpynA(buffer, "TT ", max_len-1);
569 if((ret = encodeBase64(bin, bin_len, buffer+3,
570 max_len-3, &buffer_len)) != SEC_E_OK)
573 TRACE("Server sent: %s\n", debugstr_a(buffer));
575 /* send TT base64 blob to ntlm_auth */
576 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
579 TRACE("Helper replied: %s\n", debugstr_a(buffer));
581 if( (strncmp(buffer, "KK ", 3) != 0) &&
582 (strncmp(buffer, "AF ", 3) !=0))
584 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
585 ret = SEC_E_INVALID_TOKEN;
589 /* decode the blob and send it to server */
590 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
591 &bin_len)) != SEC_E_OK)
596 phNewContext->dwUpper = ctxt_attr;
597 phNewContext->dwLower = (ULONG_PTR)helper;
602 /* put the decoded client blob into the out buffer */
604 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
608 pOutput->cBuffers = 1;
609 pOutput->pBuffers[0].pvBuffer = SECUR32_ALLOC(bin_len);
610 pOutput->pBuffers[0].cbBuffer = bin_len;
614 if (!pOutput || !pOutput->cBuffers || pOutput->pBuffers[0].cbBuffer < bin_len)
616 TRACE("out buffer is NULL or has not enough space\n");
617 ret = SEC_E_BUFFER_TOO_SMALL;
621 if (!pOutput->pBuffers[0].pvBuffer)
623 TRACE("out buffer is NULL\n");
624 ret = SEC_E_INTERNAL_ERROR;
628 pOutput->pBuffers[0].cbBuffer = bin_len;
629 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
630 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
634 TRACE("Getting negotiated flags\n");
635 lstrcpynA(buffer, "GF", max_len - 1);
636 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
641 TRACE("No flags negotiated, or helper does not support GF command\n");
642 helper->neg_flags = 0l;
646 TRACE("Negotiated %s\n", debugstr_a(buffer));
647 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
648 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
651 TRACE("Getting session key\n");
652 lstrcpynA(buffer, "GK", max_len - 1);
653 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
656 if(strncmp(buffer, "BH", 2) == 0)
658 TRACE("Helper does not understand command or no key negotiated.\n");
659 helper->valid_session_key = FALSE;
660 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
661 /*Generate the dummy session key = MD4(MD4(password))*/
664 SEC_WCHAR *unicode_password;
667 TRACE("Converting password to unicode.\n");
668 passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
669 (LPCSTR)helper->password, helper->pwlen,
671 unicode_password = HeapAlloc(GetProcessHeap(), 0,
672 passwd_lenW * sizeof(SEC_WCHAR));
673 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)helper->password,
674 helper->pwlen, unicode_password, passwd_lenW);
676 SECUR32_CreateNTLMv1SessionKey((PBYTE)unicode_password,
677 lstrlenW(unicode_password) * sizeof(SEC_WCHAR), helper->session_key);
679 HeapFree(GetProcessHeap(), 0, unicode_password);
682 memset(helper->session_key, 0, 16);
684 else if(strncmp(buffer, "GK ", 3) == 0)
686 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
687 &bin_len)) != SEC_E_OK)
689 TRACE("Failed to decode session key\n");
691 TRACE("Session key is %s\n", debugstr_a(buffer+3));
692 helper->valid_session_key = TRUE;
693 helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
694 if(!helper->session_key)
696 TRACE("Failed to allocate memory for session key\n");
697 ret = SEC_E_INTERNAL_ERROR;
700 memcpy(helper->session_key, bin, bin_len);
703 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
704 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
705 helper->crypt.ntlm.seq_num = 0l;
708 if(ret != SEC_I_CONTINUE_NEEDED)
710 TRACE("Deleting password!\n");
712 memset(helper->password, 0, helper->pwlen-2);
713 HeapFree(GetProcessHeap(), 0, helper->password);
716 HeapFree(GetProcessHeap(), 0, want_flags);
717 HeapFree(GetProcessHeap(), 0, buffer);
718 HeapFree(GetProcessHeap(), 0, bin);
722 /***********************************************************************
723 * InitializeSecurityContextA
725 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
726 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
727 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
728 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
729 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
733 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
734 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
735 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
739 SEC_WCHAR *target = NULL;
740 if(pszTargetName != NULL)
742 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
743 strlen(pszTargetName)+1, NULL, 0);
744 target = HeapAlloc(GetProcessHeap(), 0, target_size *
746 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
747 target, target_size);
750 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
751 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
752 phNewContext, pOutput, pfContextAttr, ptsExpiry);
754 HeapFree(GetProcessHeap(), 0, target);
758 ret = SEC_E_INVALID_HANDLE;
763 /***********************************************************************
764 * AcceptSecurityContext
766 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
767 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
768 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
769 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
772 char *buffer, *want_flags = NULL;
774 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
778 TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
779 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
783 return SEC_E_INVALID_HANDLE;
785 helper = (PNegoHelper)phCredential->dwLower;
787 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
788 bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
790 if(helper->mode != NTLM_SERVER)
792 ret = SEC_E_INVALID_HANDLE;
796 if(TargetDataRep == SECURITY_NETWORK_DREP){
797 TRACE("Using SECURITY_NETWORK_DREP\n");
800 if(phContext == NULL)
802 /* This is the first call to AcceptSecurityHandle */
805 ret = SEC_E_INCOMPLETE_MESSAGE;
809 if(pInput->cBuffers < 1)
811 ret = SEC_E_INCOMPLETE_MESSAGE;
815 if(pInput->pBuffers[0].cbBuffer > max_len)
817 ret = SEC_E_INVALID_TOKEN;
821 bin_len = pInput->pBuffers[0].cbBuffer;
823 /* Handle all the flags */
824 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
825 if(want_flags == NULL)
827 TRACE("Failed to allocate memory for the want_flags!\n");
828 ret = SEC_E_INSUFFICIENT_MEMORY;
831 lstrcpyA(want_flags, "SF");
832 if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
834 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
836 if(fContextReq & ASC_REQ_CONFIDENTIALITY)
838 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
840 if(fContextReq & ASC_REQ_CONNECTION)
842 /* This is default, so we'll enable it */
843 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
844 ctxt_attr |= ASC_RET_CONNECTION;
846 if(fContextReq & ASC_REQ_EXTENDED_ERROR)
848 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
850 if(fContextReq & ASC_REQ_INTEGRITY)
852 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
854 if(fContextReq & ASC_REQ_MUTUAL_AUTH)
856 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
858 if(fContextReq & ASC_REQ_REPLAY_DETECT)
860 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
862 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
864 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
866 if(fContextReq & ISC_REQ_STREAM)
868 FIXME("ASC_REQ_STREAM stub\n");
870 /* Done with the flags */
872 if(lstrlenA(want_flags) > 3)
874 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
875 lstrcpynA(buffer, want_flags, max_len - 1);
876 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
879 if(!strncmp(buffer, "BH", 2))
880 TRACE("Helper doesn't understand new command set\n");
883 /* This is the YR request from the client, encode to base64 */
885 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
887 lstrcpynA(buffer, "YR ", max_len-1);
889 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
890 &buffer_len)) != SEC_E_OK)
895 TRACE("Client sent: %s\n", debugstr_a(buffer));
897 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
903 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
904 /* The expected answer is TT <base64 blob> */
906 if(strncmp(buffer, "TT ", 3) != 0)
908 ret = SEC_E_INTERNAL_ERROR;
912 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
913 &bin_len)) != SEC_E_OK)
918 /* send this to the client */
921 ret = SEC_E_INSUFFICIENT_MEMORY;
925 if(pOutput->cBuffers < 1)
927 ret = SEC_E_INSUFFICIENT_MEMORY;
931 pOutput->pBuffers[0].cbBuffer = bin_len;
932 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
933 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
934 ret = SEC_I_CONTINUE_NEEDED;
939 /* we expect a KK request from client */
942 ret = SEC_E_INCOMPLETE_MESSAGE;
946 if(pInput->cBuffers < 1)
948 ret = SEC_E_INCOMPLETE_MESSAGE;
952 if(pInput->pBuffers[0].cbBuffer > max_len)
954 ret = SEC_E_INVALID_TOKEN;
958 bin_len = pInput->pBuffers[0].cbBuffer;
960 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
962 lstrcpynA(buffer, "KK ", max_len-1);
964 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
965 &buffer_len)) != SEC_E_OK)
970 TRACE("Client sent: %s\n", debugstr_a(buffer));
972 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
978 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
980 if(strncmp(buffer, "AF ", 3) != 0)
982 if(strncmp(buffer, "NA ", 3) == 0)
984 ret = SEC_E_LOGON_DENIED;
989 ret = SEC_E_INTERNAL_ERROR;
993 pOutput->pBuffers[0].cbBuffer = 0;
996 TRACE("Getting negotiated flags\n");
997 lstrcpynA(buffer, "GF", max_len - 1);
998 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1003 TRACE("No flags negotiated, or helper does not support GF command\n");
1007 TRACE("Negotiated %s\n", debugstr_a(buffer));
1008 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
1009 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
1012 TRACE("Getting session key\n");
1013 lstrcpynA(buffer, "GK", max_len - 1);
1014 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1018 TRACE("Helper does not support GK command\n");
1021 if(strncmp(buffer, "BH ", 3) == 0)
1023 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1024 helper->valid_session_key = FALSE;
1025 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1026 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1027 memset(helper->session_key, 0 , 16);
1029 else if(strncmp(buffer, "GK ", 3) == 0)
1031 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1032 &bin_len)) != SEC_E_OK)
1034 TRACE("Failed to decode session key\n");
1036 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1037 helper->valid_session_key = TRUE;
1038 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1039 if(!helper->session_key)
1041 TRACE("Failed to allocate memory for session key\n");
1042 ret = SEC_E_INTERNAL_ERROR;
1045 memcpy(helper->session_key, bin, 16);
1048 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1049 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1050 helper->crypt.ntlm.seq_num = 0l;
1053 phNewContext->dwUpper = ctxt_attr;
1054 phNewContext->dwLower = (ULONG_PTR)helper;
1057 HeapFree(GetProcessHeap(), 0, want_flags);
1058 HeapFree(GetProcessHeap(), 0, buffer);
1059 HeapFree(GetProcessHeap(), 0, bin);
1063 /***********************************************************************
1066 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1067 PSecBufferDesc pToken)
1069 /* We never need to call CompleteAuthToken anyway */
1070 TRACE("%p %p\n", phContext, pToken);
1072 return SEC_E_INVALID_HANDLE;
1077 /***********************************************************************
1078 * DeleteSecurityContext
1080 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1084 TRACE("%p\n", phContext);
1086 return SEC_E_INVALID_HANDLE;
1088 helper = (PNegoHelper)phContext->dwLower;
1090 phContext->dwUpper = 0;
1091 phContext->dwLower = 0;
1093 SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1094 HeapFree(GetProcessHeap(), 0, helper->session_key);
1095 helper->valid_session_key = FALSE;
1100 /***********************************************************************
1101 * QueryContextAttributesW
1103 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1104 ULONG ulAttribute, void *pBuffer)
1106 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1108 return SEC_E_INVALID_HANDLE;
1112 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1113 _x(SECPKG_ATTR_ACCESS_TOKEN);
1114 _x(SECPKG_ATTR_AUTHORITY);
1115 _x(SECPKG_ATTR_DCE_INFO);
1116 case SECPKG_ATTR_FLAGS:
1118 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1119 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1122 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1123 spcf->Flags |= ISC_RET_INTEGRITY;
1124 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1125 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1128 _x(SECPKG_ATTR_KEY_INFO);
1129 _x(SECPKG_ATTR_LIFESPAN);
1130 _x(SECPKG_ATTR_NAMES);
1131 _x(SECPKG_ATTR_NATIVE_NAMES);
1132 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1133 _x(SECPKG_ATTR_PACKAGE_INFO);
1134 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1135 _x(SECPKG_ATTR_SESSION_KEY);
1136 case SECPKG_ATTR_SIZES:
1138 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1139 spcs->cbMaxToken = NTLM_MAX_BUF;
1140 spcs->cbMaxSignature = 16;
1141 spcs->cbBlockSize = 0;
1142 spcs->cbSecurityTrailer = 16;
1145 _x(SECPKG_ATTR_STREAM_SIZES);
1146 _x(SECPKG_ATTR_TARGET_INFORMATION);
1149 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1152 return SEC_E_UNSUPPORTED_FUNCTION;
1155 /***********************************************************************
1156 * QueryContextAttributesA
1158 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1159 ULONG ulAttribute, void *pBuffer)
1161 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1164 /***********************************************************************
1165 * ImpersonateSecurityContext
1167 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1169 SECURITY_STATUS ret;
1171 TRACE("%p\n", phContext);
1174 ret = SEC_E_UNSUPPORTED_FUNCTION;
1178 ret = SEC_E_INVALID_HANDLE;
1183 /***********************************************************************
1184 * RevertSecurityContext
1186 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1188 SECURITY_STATUS ret;
1190 TRACE("%p\n", phContext);
1193 ret = SEC_E_UNSUPPORTED_FUNCTION;
1197 ret = SEC_E_INVALID_HANDLE;
1202 /*************************************************************************
1203 * ntlm_GetTokenBufferIndex
1204 * Calculates the index of the secbuffer with BufferType == SECBUFFER_TOKEN
1205 * Returns index if found or -1 if not found.
1207 static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)
1211 TRACE("%p\n", pMessage);
1213 for( i = 0; i < pMessage->cBuffers; ++i )
1215 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1222 /***********************************************************************
1223 * ntlm_CreateSignature
1224 * As both MakeSignature and VerifySignature need this, but different keys
1225 * are needed for NTLMv2, the logic goes into a helper function.
1227 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1228 int token_idx, SignDirection direction)
1230 ULONG sign_version = 1;
1232 TRACE("%p, %p, %d\n", helper, pMessage, direction);
1234 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1235 helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1237 return SEC_E_UNSUPPORTED_FUNCTION;
1239 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1241 PBYTE sig = pMessage->pBuffers[token_idx].pvBuffer;
1244 for(i=0; i < pMessage->cBuffers; ++i)
1246 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1248 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1249 pMessage->pBuffers[i].cbBuffer, crc);
1253 sig[ 0] = (sign_version >> 0) & 0xff;
1254 sig[ 1] = (sign_version >> 8) & 0xff;
1255 sig[ 2] = (sign_version >> 16) & 0xff;
1256 sig[ 3] = (sign_version >> 24) & 0xff;
1257 memset(sig+4, 0, 4);
1258 sig[ 8] = (crc >> 0) & 0xff;
1259 sig[ 9] = (crc >> 8) & 0xff;
1260 sig[10] = (crc >> 16) & 0xff;
1261 sig[11] = (crc >> 24) & 0xff;
1262 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1263 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1264 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1265 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1267 ++(helper->crypt.ntlm.seq_num);
1269 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1273 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1275 TRACE("Creating a dummy signature.\n");
1276 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1277 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1278 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1279 pMessage->pBuffers[token_idx].cbBuffer = 16;
1283 return SEC_E_UNSUPPORTED_FUNCTION;
1286 /***********************************************************************
1289 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1290 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1295 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1297 return SEC_E_INVALID_HANDLE;
1300 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1303 FIXME("Ignoring MessageSeqNo\n");
1305 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1306 return SEC_E_INVALID_TOKEN;
1308 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1309 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1310 return SEC_E_INVALID_TOKEN;
1312 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1313 return SEC_E_BUFFER_TOO_SMALL;
1315 helper = (PNegoHelper)phContext->dwLower;
1316 TRACE("Negotiated flags are: 0x%08lx\n", helper->neg_flags);
1318 return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND);
1321 /***********************************************************************
1324 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1325 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1331 SECURITY_STATUS ret;
1332 SecBufferDesc local_desc;
1333 PSecBuffer local_buff;
1336 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1338 return SEC_E_INVALID_HANDLE;
1340 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1341 return SEC_E_INVALID_TOKEN;
1343 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1344 return SEC_E_INVALID_TOKEN;
1346 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1347 return SEC_E_BUFFER_TOO_SMALL;
1350 FIXME("Ignoring MessageSeqNo\n");
1352 helper = (PNegoHelper)phContext->dwLower;
1353 TRACE("Negotiated flags: 0x%08lx\n", helper->neg_flags);
1355 local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1357 local_desc.ulVersion = SECBUFFER_VERSION;
1358 local_desc.cBuffers = pMessage->cBuffers;
1359 local_desc.pBuffers = local_buff;
1361 for(i=0; i < pMessage->cBuffers; ++i)
1363 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1365 local_buff[i].BufferType = SECBUFFER_TOKEN;
1366 local_buff[i].cbBuffer = 16;
1367 local_buff[i].pvBuffer = local_sig;
1371 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1372 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1373 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1377 if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV)) != SEC_E_OK)
1380 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1381 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1382 ret = SEC_E_MESSAGE_ALTERED;
1386 HeapFree(GetProcessHeap(), 0, local_buff);
1393 /***********************************************************************
1394 * FreeCredentialsHandle
1396 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1397 PCredHandle phCredential)
1399 SECURITY_STATUS ret;
1402 PNegoHelper helper = (PNegoHelper) phCredential->dwLower;
1403 phCredential->dwUpper = 0;
1404 phCredential->dwLower = 0;
1405 cleanup_helper(helper);
1414 /***********************************************************************
1417 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1418 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1424 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1427 return SEC_E_INVALID_HANDLE;
1430 FIXME("Ignoring fQOP\n");
1433 FIXME("Ignoring MessageSeqNo\n");
1435 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1436 return SEC_E_INVALID_TOKEN;
1438 for(i=0; i < pMessage->cBuffers; ++i)
1440 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1448 return SEC_E_INVALID_TOKEN;
1450 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1451 return SEC_E_BUFFER_TOO_SMALL;
1453 helper = (PNegoHelper) phContext->dwLower;
1455 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
1457 FIXME("Can't handle NTLMv2 encryption yet, aborting\n");
1458 return SEC_E_UNSUPPORTED_FUNCTION;
1462 PBYTE sig = pMessage->pBuffers[token_idx].pvBuffer;
1464 ULONG sign_version = 1l;
1466 for(i=0; i < pMessage->cBuffers; ++i)
1468 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1470 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1471 pMessage->pBuffers[i].cbBuffer, crc);
1475 sig[ 0] = (sign_version >> 0) & 0xff;
1476 sig[ 1] = (sign_version >> 8) & 0xff;
1477 sig[ 2] = (sign_version >> 16) & 0xff;
1478 sig[ 3] = (sign_version >> 24) & 0xff;
1479 memset(sig+4, 0, 4);
1480 sig[ 8] = (crc >> 0) & 0xff;
1481 sig[ 9] = (crc >> 8) & 0xff;
1482 sig[10] = (crc >> 16) & 0xff;
1483 sig[11] = (crc >> 24) & 0xff;
1484 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1485 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1486 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1487 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1489 SECUR32_arc4Process(helper->crypt.ntlm.a4i, pMessage->pBuffers[1].pvBuffer,
1490 pMessage->pBuffers[1].cbBuffer);
1491 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1493 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1494 memset(sig+4, 0, 4);
1496 ++(helper->crypt.ntlm.seq_num);
1503 /***********************************************************************
1506 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1507 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1509 SECURITY_STATUS ret;
1510 ULONG ntlmssp_flags_save;
1514 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1517 return SEC_E_INVALID_HANDLE;
1520 FIXME("Ignoring MessageSeqNo\n");
1522 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1523 return SEC_E_INVALID_TOKEN;
1525 for(i=0; i < pMessage->cBuffers; ++i)
1527 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1534 return SEC_E_INVALID_TOKEN;
1536 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1537 return SEC_E_BUFFER_TOO_SMALL;
1539 helper = (PNegoHelper) phContext->dwLower;
1541 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
1543 FIXME("Can't handle NTLMv2 encryption yet, aborting\n");
1544 return SEC_E_UNSUPPORTED_FUNCTION;
1548 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1549 pMessage->pBuffers[1].pvBuffer, pMessage->pBuffers[1].cbBuffer);
1552 /* Make sure we use a session key for the signature check, EncryptMessage
1553 * always does that, even in the dummy case */
1554 ntlmssp_flags_save = helper->neg_flags;
1556 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1557 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1559 helper->neg_flags = ntlmssp_flags_save;
1564 static SecurityFunctionTableA ntlmTableA = {
1566 NULL, /* EnumerateSecurityPackagesA */
1567 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1568 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1569 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1570 NULL, /* Reserved2 */
1571 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1572 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1573 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1574 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1575 NULL, /* ApplyControlToken */
1576 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1577 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1578 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1579 ntlm_MakeSignature, /* MakeSignature */
1580 ntlm_VerifySignature, /* VerifySignature */
1581 FreeContextBuffer, /* FreeContextBuffer */
1582 NULL, /* QuerySecurityPackageInfoA */
1583 NULL, /* Reserved3 */
1584 NULL, /* Reserved4 */
1585 NULL, /* ExportSecurityContext */
1586 NULL, /* ImportSecurityContextA */
1587 NULL, /* AddCredentialsA */
1588 NULL, /* Reserved8 */
1589 NULL, /* QuerySecurityContextToken */
1590 ntlm_EncryptMessage, /* EncryptMessage */
1591 ntlm_DecryptMessage, /* DecryptMessage */
1592 NULL, /* SetContextAttributesA */
1595 static SecurityFunctionTableW ntlmTableW = {
1597 NULL, /* EnumerateSecurityPackagesW */
1598 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1599 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1600 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1601 NULL, /* Reserved2 */
1602 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1603 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1604 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1605 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1606 NULL, /* ApplyControlToken */
1607 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1608 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1609 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1610 ntlm_MakeSignature, /* MakeSignature */
1611 ntlm_VerifySignature, /* VerifySignature */
1612 FreeContextBuffer, /* FreeContextBuffer */
1613 NULL, /* QuerySecurityPackageInfoW */
1614 NULL, /* Reserved3 */
1615 NULL, /* Reserved4 */
1616 NULL, /* ExportSecurityContext */
1617 NULL, /* ImportSecurityContextW */
1618 NULL, /* AddCredentialsW */
1619 NULL, /* Reserved8 */
1620 NULL, /* QuerySecurityContextToken */
1621 ntlm_EncryptMessage, /* EncryptMessage */
1622 ntlm_DecryptMessage, /* DecryptMessage */
1623 NULL, /* SetContextAttributesW */
1626 #define NTLM_COMMENT \
1627 { 'N', 'T', 'L', 'M', ' ', \
1628 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1629 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1631 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1632 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1634 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1636 static char ntlm_name_A[] = NTLM_NAME;
1637 static WCHAR ntlm_name_W[] = NTLM_NAME;
1639 /* According to Windows, NTLM has the following capabilities. */
1641 SECPKG_FLAG_INTEGRITY | \
1642 SECPKG_FLAG_PRIVACY | \
1643 SECPKG_FLAG_TOKEN_ONLY | \
1644 SECPKG_FLAG_CONNECTION | \
1645 SECPKG_FLAG_MULTI_REQUIRED | \
1646 SECPKG_FLAG_IMPERSONATION | \
1647 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1648 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1650 static const SecPkgInfoW infoW = {
1659 static const SecPkgInfoA infoA = {
1668 void SECUR32_initNTLMSP(void)
1670 SECURITY_STATUS ret;
1672 static CHAR version[] = "--version";
1674 SEC_CHAR *args[] = {
1679 if((ret = fork_helper(&helper, ntlm_auth, args)) != SEC_E_OK)
1681 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1682 helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1683 helper->version = -1;
1686 check_version(helper);
1688 if(helper->version > 2)
1690 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1691 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1695 ERR("%s was not found or is outdated. "
1696 "Make sure that ntlm_auth >= 3.x is in your path.\n",
1699 cleanup_helper(helper);