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(ntlm);
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 /* Get username and domain from pAuthData */
167 username = HeapAlloc(GetProcessHeap(), 0,
168 (auth_data->UserLength + 1) * sizeof(SEC_WCHAR));
169 memcpy(username, auth_data->User,
170 auth_data->UserLength * sizeof(SEC_WCHAR));
171 username[auth_data->UserLength] = '\0';
173 domain = HeapAlloc(GetProcessHeap(), 0,
174 (auth_data->DomainLength + 1) * sizeof(SEC_WCHAR));
175 memcpy(domain, auth_data->Domain,
176 auth_data->DomainLength * sizeof(SEC_WCHAR));
177 domain[auth_data->DomainLength] = '\0';
179 TRACE("Username is %s\n", debugstr_w(username));
180 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
181 username, -1, NULL, 0, NULL, NULL) + sizeof(username_arg);
182 client_user_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
183 lstrcpyA(client_user_arg, username_arg);
184 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, username, -1,
185 client_user_arg + sizeof(username_arg) - 1,
186 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
188 TRACE("Domain name is %s\n", debugstr_w(domain));
189 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
190 domain, -1, NULL, 0, NULL, NULL) + sizeof(domain_arg);
191 client_domain_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
192 lstrcpyA(client_domain_arg, domain_arg);
193 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domain,
194 -1, client_domain_arg + sizeof(domain_arg) - 1,
195 unixcp_size - sizeof(domain) + 1, NULL, NULL);
197 client_argv[0] = ntlm_auth;
198 client_argv[1] = helper_protocol;
199 client_argv[2] = client_user_arg;
200 client_argv[3] = client_domain_arg;
201 client_argv[4] = credentials_argv;
202 client_argv[5] = NULL;
204 if((ret = fork_helper(&helper, ntlm_auth, client_argv)) !=
212 helper->mode = NTLM_CLIENT;
214 if(pAuthData != NULL)
216 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
217 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
219 if(auth_data->PasswordLength != 0)
221 helper->pwlen = WideCharToMultiByte(CP_UNIXCP,
222 WC_NO_BEST_FIT_CHARS, auth_data->Password,
223 auth_data->PasswordLength, NULL, 0, NULL,
226 helper->password = HeapAlloc(GetProcessHeap(), 0,
229 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
230 auth_data->Password, auth_data->PasswordLength,
231 helper->password, helper->pwlen, NULL, NULL);
235 phCredential->dwUpper = fCredentialUse;
236 phCredential->dwLower = (ULONG_PTR)helper;
237 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
238 phCredential->dwUpper, phCredential->dwLower);
243 case SECPKG_CRED_BOTH:
244 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
245 ret = SEC_E_UNSUPPORTED_FUNCTION;
250 ret = SEC_E_UNKNOWN_CREDENTIALS;
254 HeapFree(GetProcessHeap(), 0, client_user_arg);
255 HeapFree(GetProcessHeap(), 0, client_domain_arg);
256 HeapFree(GetProcessHeap(), 0, username);
257 HeapFree(GetProcessHeap(), 0, domain);
262 /***********************************************************************
263 * AcquireCredentialsHandleA
265 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
266 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
267 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
268 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
271 int user_sizeW, domain_sizeW, passwd_sizeW;
273 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
275 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
276 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
278 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
279 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
280 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
282 if(pszPackage != NULL)
284 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
287 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
289 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
293 if(pAuthData != NULL)
295 identity = (PSEC_WINNT_AUTH_IDENTITY_A)pAuthData;
297 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
299 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
300 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
302 if(identity->UserLength != 0)
304 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
305 (LPCSTR)identity->User, identity->UserLength, NULL, 0);
306 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
308 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
309 identity->UserLength, user, user_sizeW);
316 if(identity->DomainLength != 0)
318 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
319 (LPCSTR)identity->Domain, identity->DomainLength, NULL, 0);
320 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
321 * sizeof(SEC_WCHAR));
322 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
323 identity->DomainLength, domain, domain_sizeW);
330 if(identity->PasswordLength != 0)
332 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
333 (LPCSTR)identity->Password, identity->PasswordLength,
335 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
336 * sizeof(SEC_WCHAR));
337 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
338 identity->PasswordLength, passwd, passwd_sizeW);
345 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
346 pAuthDataW->User = user;
347 pAuthDataW->UserLength = user_sizeW;
348 pAuthDataW->Domain = domain;
349 pAuthDataW->DomainLength = domain_sizeW;
350 pAuthDataW->Password = passwd;
351 pAuthDataW->PasswordLength = passwd_sizeW;
355 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
359 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
360 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
363 HeapFree(GetProcessHeap(), 0, package);
364 HeapFree(GetProcessHeap(), 0, user);
365 HeapFree(GetProcessHeap(), 0, domain);
366 HeapFree(GetProcessHeap(), 0, passwd);
367 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
368 HeapFree(GetProcessHeap(), 0, pAuthDataW);
373 /***********************************************************************
374 * InitializeSecurityContextW
376 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
377 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
378 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
379 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
380 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
385 char* buffer, *want_flags = NULL;
387 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
389 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
390 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
391 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
394 return SEC_E_INVALID_HANDLE;
396 /* As the server side of sspi never calls this, make sure that
397 * the handler is a client handler.
399 helper = (PNegoHelper)phCredential->dwLower;
400 if(helper->mode != NTLM_CLIENT)
402 TRACE("Helper mode = %d\n", helper->mode);
403 return SEC_E_INVALID_HANDLE;
406 /****************************************
407 * When communicating with the client, there can be the
408 * following reply packets:
409 * YR <base64 blob> should be sent to the server
410 * PW should be sent back to helper with
411 * base64 encoded password
412 * AF <base64 blob> client is done, blob should be
413 * sent to server with KK prefixed
414 * GF <string list> A string list of negotiated flags
415 * GK <base64 blob> base64 encoded session key
416 * BH <char reason> something broke
418 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
422 TRACE("According to a MS whitepaper pszTargetName is ignored.\n");
425 if(TargetDataRep == SECURITY_NETWORK_DREP){
426 TRACE("Setting SECURITY_NETWORK_DREP\n");
429 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
430 bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
432 if((phContext == NULL) && (pInput == NULL))
434 TRACE("First time in ISC()\n");
435 /* Allocate space for a maximal string of
436 * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
437 * NTLMSSP_FEATURE_SESSION_KEY"
439 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
440 if(want_flags == NULL)
442 ret = SEC_E_INSUFFICIENT_MEMORY;
445 lstrcpyA(want_flags, "SF");
446 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
449 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SEAL")) == NULL)
450 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
452 if(fContextReq & ISC_REQ_CONNECTION)
453 ctxt_attr |= ISC_RET_CONNECTION;
454 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
455 ctxt_attr |= ISC_RET_EXTENDED_ERROR;
456 if(fContextReq & ISC_REQ_INTEGRITY)
459 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
460 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
462 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
463 ctxt_attr |= ISC_RET_MUTUAL_AUTH;
464 if(fContextReq & ISC_REQ_REPLAY_DETECT)
467 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
468 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
470 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
473 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
474 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
476 if(fContextReq & ISC_REQ_STREAM)
477 FIXME("ISC_REQ_STREAM\n");
478 if(fContextReq & ISC_REQ_USE_DCE_STYLE)
479 ctxt_attr |= ISC_RET_USED_DCE_STYLE;
480 if(fContextReq & ISC_REQ_DELEGATE)
481 ctxt_attr |= ISC_RET_DELEGATE;
483 /* If no password is given, try to use cached credentials. Fall back to an empty
484 * password if this failed. */
485 if(helper->password == NULL)
487 lstrcpynA(buffer, "OK", max_len-1);
488 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
490 /* If the helper replied with "PW", using cached credentials failed */
491 if(!strncmp(buffer, "PW", 2))
493 TRACE("Using cached credentials failed. Using empty password.\n");
494 lstrcpynA(buffer, "PW AA==", max_len-1);
496 else /* Just do a noop on the next run */
497 lstrcpynA(buffer, "OK", max_len-1);
501 lstrcpynA(buffer, "PW ", max_len-1);
502 if((ret = encodeBase64((unsigned char*)helper->password,
503 helper->pwlen, buffer+3,
504 max_len-3, &buffer_len)) != SEC_E_OK)
506 TRACE("Deleting password!\n");
507 memset(helper->password, 0, helper->pwlen);
508 HeapFree(GetProcessHeap(), 0, helper->password);
514 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
515 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
518 TRACE("Helper returned %s\n", debugstr_a(buffer));
520 if(lstrlenA(want_flags) > 2)
522 TRACE("Want flags are %s\n", debugstr_a(want_flags));
523 lstrcpynA(buffer, want_flags, max_len-1);
524 if((ret = run_helper(helper, buffer, max_len, &buffer_len))
527 if(!strncmp(buffer, "BH", 2))
528 ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
531 lstrcpynA(buffer, "YR", max_len-1);
533 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
536 TRACE("%s\n", buffer);
538 if(strncmp(buffer, "YR ", 3) != 0)
540 /* Something borked */
541 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
542 ret = SEC_E_INTERNAL_ERROR;
545 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
546 max_len-1, &bin_len)) != SEC_E_OK)
549 /* put the decoded client blob into the out buffer */
551 ret = SEC_I_CONTINUE_NEEDED;
555 /* handle second call here */
556 /* encode server data to base64 */
557 if (!pInput || !pInput->cBuffers)
559 ret = SEC_E_INCOMPLETE_MESSAGE;
563 if (!pInput->pBuffers[0].pvBuffer)
565 ret = SEC_E_INTERNAL_ERROR;
569 if(pInput->pBuffers[0].cbBuffer > max_len)
571 TRACE("pInput->pBuffers[0].cbBuffer is: %ld\n",
572 pInput->pBuffers[0].cbBuffer);
573 ret = SEC_E_INVALID_TOKEN;
577 bin_len = pInput->pBuffers[0].cbBuffer;
579 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
581 lstrcpynA(buffer, "TT ", max_len-1);
583 if((ret = encodeBase64(bin, bin_len, buffer+3,
584 max_len-3, &buffer_len)) != SEC_E_OK)
587 TRACE("Server sent: %s\n", debugstr_a(buffer));
589 /* send TT base64 blob to ntlm_auth */
590 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
593 TRACE("Helper replied: %s\n", debugstr_a(buffer));
595 if( (strncmp(buffer, "KK ", 3) != 0) &&
596 (strncmp(buffer, "AF ", 3) !=0))
598 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
599 ret = SEC_E_INVALID_TOKEN;
603 /* decode the blob and send it to server */
604 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
605 &bin_len)) != SEC_E_OK)
610 phNewContext->dwUpper = ctxt_attr;
611 phNewContext->dwLower = (ULONG_PTR)helper;
616 /* put the decoded client blob into the out buffer */
618 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
622 pOutput->cBuffers = 1;
623 pOutput->pBuffers[0].pvBuffer = SECUR32_ALLOC(bin_len);
624 pOutput->pBuffers[0].cbBuffer = bin_len;
628 if (!pOutput || !pOutput->cBuffers || pOutput->pBuffers[0].cbBuffer < bin_len)
630 TRACE("out buffer is NULL or has not enough space\n");
631 ret = SEC_E_BUFFER_TOO_SMALL;
635 if (!pOutput->pBuffers[0].pvBuffer)
637 TRACE("out buffer is NULL\n");
638 ret = SEC_E_INTERNAL_ERROR;
642 pOutput->pBuffers[0].cbBuffer = bin_len;
643 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
644 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
648 TRACE("Getting negotiated flags\n");
649 lstrcpynA(buffer, "GF", max_len - 1);
650 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
655 TRACE("No flags negotiated.\n");
656 helper->neg_flags = 0l;
660 TRACE("Negotiated %s\n", debugstr_a(buffer));
661 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
662 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
665 TRACE("Getting session key\n");
666 lstrcpynA(buffer, "GK", max_len - 1);
667 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
670 if(strncmp(buffer, "BH", 2) == 0)
672 TRACE("No key negotiated.\n");
673 helper->valid_session_key = FALSE;
674 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
675 /*Generate the dummy session key = MD4(MD4(password))*/
678 SEC_WCHAR *unicode_password;
681 TRACE("Converting password to unicode.\n");
682 passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
683 (LPCSTR)helper->password, helper->pwlen,
685 unicode_password = HeapAlloc(GetProcessHeap(), 0,
686 passwd_lenW * sizeof(SEC_WCHAR));
687 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)helper->password,
688 helper->pwlen, unicode_password, passwd_lenW);
690 SECUR32_CreateNTLMv1SessionKey((PBYTE)unicode_password,
691 passwd_lenW * sizeof(SEC_WCHAR), helper->session_key);
693 HeapFree(GetProcessHeap(), 0, unicode_password);
696 memset(helper->session_key, 0, 16);
698 else if(strncmp(buffer, "GK ", 3) == 0)
700 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
701 &bin_len)) != SEC_E_OK)
703 TRACE("Failed to decode session key\n");
705 TRACE("Session key is %s\n", debugstr_a(buffer+3));
706 helper->valid_session_key = TRUE;
707 helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
708 if(!helper->session_key)
710 TRACE("Failed to allocate memory for session key\n");
711 ret = SEC_E_INTERNAL_ERROR;
714 memcpy(helper->session_key, bin, bin_len);
717 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
718 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
719 helper->crypt.ntlm.seq_num = 0l;
720 SECUR32_CreateNTLMv2SubKeys(helper);
721 helper->crypt.ntlm2.send_a4i = SECUR32_arc4Alloc();
722 helper->crypt.ntlm2.recv_a4i = SECUR32_arc4Alloc();
723 SECUR32_arc4Init(helper->crypt.ntlm2.send_a4i,
724 (BYTE *)helper->crypt.ntlm2.send_seal_key, 16);
725 SECUR32_arc4Init(helper->crypt.ntlm2.recv_a4i,
726 (BYTE *)helper->crypt.ntlm2.recv_seal_key, 16);
727 helper->crypt.ntlm2.send_seq_no = 0l;
728 helper->crypt.ntlm2.recv_seq_no = 0l;
731 if(ret != SEC_I_CONTINUE_NEEDED)
733 TRACE("Deleting password!\n");
735 memset(helper->password, 0, helper->pwlen);
736 HeapFree(GetProcessHeap(), 0, helper->password);
739 HeapFree(GetProcessHeap(), 0, want_flags);
740 HeapFree(GetProcessHeap(), 0, buffer);
741 HeapFree(GetProcessHeap(), 0, bin);
745 /***********************************************************************
746 * InitializeSecurityContextA
748 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
749 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
750 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
751 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
752 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
756 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
757 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
758 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
762 SEC_WCHAR *target = NULL;
763 if(pszTargetName != NULL)
765 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
766 strlen(pszTargetName)+1, NULL, 0);
767 target = HeapAlloc(GetProcessHeap(), 0, target_size *
769 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
770 target, target_size);
773 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
774 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
775 phNewContext, pOutput, pfContextAttr, ptsExpiry);
777 HeapFree(GetProcessHeap(), 0, target);
781 ret = SEC_E_INVALID_HANDLE;
786 /***********************************************************************
787 * AcceptSecurityContext
789 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
790 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
791 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
792 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
795 char *buffer, *want_flags = NULL;
797 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
801 TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
802 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
806 return SEC_E_INVALID_HANDLE;
808 helper = (PNegoHelper)phCredential->dwLower;
810 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
811 bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
813 if(helper->mode != NTLM_SERVER)
815 ret = SEC_E_INVALID_HANDLE;
819 if(TargetDataRep == SECURITY_NETWORK_DREP){
820 TRACE("Using SECURITY_NETWORK_DREP\n");
823 if(phContext == NULL)
825 /* This is the first call to AcceptSecurityHandle */
828 ret = SEC_E_INCOMPLETE_MESSAGE;
832 if(pInput->cBuffers < 1)
834 ret = SEC_E_INCOMPLETE_MESSAGE;
838 if(pInput->pBuffers[0].cbBuffer > max_len)
840 ret = SEC_E_INVALID_TOKEN;
844 bin_len = pInput->pBuffers[0].cbBuffer;
846 /* Handle all the flags */
847 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
848 if(want_flags == NULL)
850 TRACE("Failed to allocate memory for the want_flags!\n");
851 ret = SEC_E_INSUFFICIENT_MEMORY;
854 lstrcpyA(want_flags, "SF");
855 if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
857 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
859 if(fContextReq & ASC_REQ_CONFIDENTIALITY)
861 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
863 if(fContextReq & ASC_REQ_CONNECTION)
865 /* This is default, so we'll enable it */
866 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
867 ctxt_attr |= ASC_RET_CONNECTION;
869 if(fContextReq & ASC_REQ_EXTENDED_ERROR)
871 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
873 if(fContextReq & ASC_REQ_INTEGRITY)
875 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
877 if(fContextReq & ASC_REQ_MUTUAL_AUTH)
879 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
881 if(fContextReq & ASC_REQ_REPLAY_DETECT)
883 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
885 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
887 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
889 if(fContextReq & ISC_REQ_STREAM)
891 FIXME("ASC_REQ_STREAM stub\n");
893 /* Done with the flags */
895 if(lstrlenA(want_flags) > 3)
897 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
898 lstrcpynA(buffer, want_flags, max_len - 1);
899 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
902 if(!strncmp(buffer, "BH", 2))
903 TRACE("Helper doesn't understand new command set\n");
906 /* This is the YR request from the client, encode to base64 */
908 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
910 lstrcpynA(buffer, "YR ", max_len-1);
912 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
913 &buffer_len)) != SEC_E_OK)
918 TRACE("Client sent: %s\n", debugstr_a(buffer));
920 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
926 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
927 /* The expected answer is TT <base64 blob> */
929 if(strncmp(buffer, "TT ", 3) != 0)
931 ret = SEC_E_INTERNAL_ERROR;
935 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
936 &bin_len)) != SEC_E_OK)
941 /* send this to the client */
944 ret = SEC_E_INSUFFICIENT_MEMORY;
948 if(pOutput->cBuffers < 1)
950 ret = SEC_E_INSUFFICIENT_MEMORY;
954 pOutput->pBuffers[0].cbBuffer = bin_len;
955 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
956 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
957 ret = SEC_I_CONTINUE_NEEDED;
962 /* we expect a KK request from client */
965 ret = SEC_E_INCOMPLETE_MESSAGE;
969 if(pInput->cBuffers < 1)
971 ret = SEC_E_INCOMPLETE_MESSAGE;
975 if(pInput->pBuffers[0].cbBuffer > max_len)
977 ret = SEC_E_INVALID_TOKEN;
981 bin_len = pInput->pBuffers[0].cbBuffer;
983 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
985 lstrcpynA(buffer, "KK ", max_len-1);
987 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
988 &buffer_len)) != SEC_E_OK)
993 TRACE("Client sent: %s\n", debugstr_a(buffer));
995 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1001 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1003 if(strncmp(buffer, "AF ", 3) != 0)
1005 if(strncmp(buffer, "NA ", 3) == 0)
1007 ret = SEC_E_LOGON_DENIED;
1012 ret = SEC_E_INTERNAL_ERROR;
1016 pOutput->pBuffers[0].cbBuffer = 0;
1019 TRACE("Getting negotiated flags\n");
1020 lstrcpynA(buffer, "GF", max_len - 1);
1021 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1026 TRACE("No flags negotiated, or helper does not support GF command\n");
1030 TRACE("Negotiated %s\n", debugstr_a(buffer));
1031 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
1032 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
1035 TRACE("Getting session key\n");
1036 lstrcpynA(buffer, "GK", max_len - 1);
1037 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1041 TRACE("Helper does not support GK command\n");
1044 if(strncmp(buffer, "BH ", 3) == 0)
1046 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1047 helper->valid_session_key = FALSE;
1048 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1049 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1050 memset(helper->session_key, 0 , 16);
1052 else if(strncmp(buffer, "GK ", 3) == 0)
1054 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1055 &bin_len)) != SEC_E_OK)
1057 TRACE("Failed to decode session key\n");
1059 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1060 helper->valid_session_key = TRUE;
1061 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1062 if(!helper->session_key)
1064 TRACE("Failed to allocate memory for session key\n");
1065 ret = SEC_E_INTERNAL_ERROR;
1068 memcpy(helper->session_key, bin, 16);
1071 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1072 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1073 helper->crypt.ntlm.seq_num = 0l;
1076 phNewContext->dwUpper = ctxt_attr;
1077 phNewContext->dwLower = (ULONG_PTR)helper;
1080 HeapFree(GetProcessHeap(), 0, want_flags);
1081 HeapFree(GetProcessHeap(), 0, buffer);
1082 HeapFree(GetProcessHeap(), 0, bin);
1086 /***********************************************************************
1089 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1090 PSecBufferDesc pToken)
1092 /* We never need to call CompleteAuthToken anyway */
1093 TRACE("%p %p\n", phContext, pToken);
1095 return SEC_E_INVALID_HANDLE;
1100 /***********************************************************************
1101 * DeleteSecurityContext
1103 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1107 TRACE("%p\n", phContext);
1109 return SEC_E_INVALID_HANDLE;
1111 helper = (PNegoHelper)phContext->dwLower;
1113 phContext->dwUpper = 0;
1114 phContext->dwLower = 0;
1116 SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1117 HeapFree(GetProcessHeap(), 0, helper->session_key);
1118 helper->valid_session_key = FALSE;
1119 SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1120 SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1121 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
1122 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
1123 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
1124 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
1129 /***********************************************************************
1130 * QueryContextAttributesW
1132 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1133 ULONG ulAttribute, void *pBuffer)
1135 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1137 return SEC_E_INVALID_HANDLE;
1141 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1142 _x(SECPKG_ATTR_ACCESS_TOKEN);
1143 _x(SECPKG_ATTR_AUTHORITY);
1144 _x(SECPKG_ATTR_DCE_INFO);
1145 case SECPKG_ATTR_FLAGS:
1147 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1148 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1151 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1152 spcf->Flags |= ISC_RET_INTEGRITY;
1153 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1154 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1157 _x(SECPKG_ATTR_KEY_INFO);
1158 _x(SECPKG_ATTR_LIFESPAN);
1159 _x(SECPKG_ATTR_NAMES);
1160 _x(SECPKG_ATTR_NATIVE_NAMES);
1161 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1162 _x(SECPKG_ATTR_PACKAGE_INFO);
1163 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1164 _x(SECPKG_ATTR_SESSION_KEY);
1165 case SECPKG_ATTR_SIZES:
1167 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1168 spcs->cbMaxToken = NTLM_MAX_BUF;
1169 spcs->cbMaxSignature = 16;
1170 spcs->cbBlockSize = 0;
1171 spcs->cbSecurityTrailer = 16;
1174 _x(SECPKG_ATTR_STREAM_SIZES);
1175 _x(SECPKG_ATTR_TARGET_INFORMATION);
1178 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1181 return SEC_E_UNSUPPORTED_FUNCTION;
1184 /***********************************************************************
1185 * QueryContextAttributesA
1187 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1188 ULONG ulAttribute, void *pBuffer)
1190 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1193 /***********************************************************************
1194 * ImpersonateSecurityContext
1196 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1198 SECURITY_STATUS ret;
1200 TRACE("%p\n", phContext);
1203 ret = SEC_E_UNSUPPORTED_FUNCTION;
1207 ret = SEC_E_INVALID_HANDLE;
1212 /***********************************************************************
1213 * RevertSecurityContext
1215 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1217 SECURITY_STATUS ret;
1219 TRACE("%p\n", phContext);
1222 ret = SEC_E_UNSUPPORTED_FUNCTION;
1226 ret = SEC_E_INVALID_HANDLE;
1231 /*************************************************************************
1232 * ntlm_GetTokenBufferIndex
1233 * Calculates the index of the secbuffer with BufferType == SECBUFFER_TOKEN
1234 * Returns index if found or -1 if not found.
1236 static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)
1240 TRACE("%p\n", pMessage);
1242 for( i = 0; i < pMessage->cBuffers; ++i )
1244 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1251 /***********************************************************************
1252 * ntlm_CreateSignature
1253 * As both MakeSignature and VerifySignature need this, but different keys
1254 * are needed for NTLMv2, the logic goes into a helper function.
1255 * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1256 * signing/encrypting and NTLM_RECV for verfying/decrypting. When encrypting,
1257 * the signature is encrypted after the message was encrypted, so
1258 * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1261 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1262 int token_idx, SignDirection direction, BOOL encrypt_sig)
1264 ULONG sign_version = 1;
1267 TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1270 sig = pMessage->pBuffers[token_idx].pvBuffer;
1272 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1273 helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1277 HMAC_MD5_CTX hmac_md5_ctx;
1279 TRACE("Signing NTLM2 style\n");
1281 if(direction == NTLM_SEND)
1283 seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
1284 seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
1285 seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1286 seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1288 ++(helper->crypt.ntlm2.send_seq_no);
1290 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1294 seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
1295 seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
1296 seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1297 seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1299 ++(helper->crypt.ntlm2.recv_seq_no);
1301 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1304 HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1305 for( i = 0; i < pMessage->cBuffers; ++i )
1307 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1308 HMACMD5Update(&hmac_md5_ctx, (BYTE *)pMessage->pBuffers[i].pvBuffer,
1309 pMessage->pBuffers[i].cbBuffer);
1312 HMACMD5Final(&hmac_md5_ctx, digest);
1314 if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1316 if(direction == NTLM_SEND)
1317 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1319 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1322 /* The NTLM2 signature is the sign version */
1323 sig[ 0] = (sign_version >> 0) & 0xff;
1324 sig[ 1] = (sign_version >> 8) & 0xff;
1325 sig[ 2] = (sign_version >> 16) & 0xff;
1326 sig[ 3] = (sign_version >> 24) & 0xff;
1327 /* The first 8 bytes of the digest */
1328 memcpy(sig+4, digest, 8);
1329 /* And the sequence number */
1330 memcpy(sig+12, seq_no, 4);
1332 pMessage->pBuffers[token_idx].cbBuffer = 16;
1336 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1339 TRACE("Signing NTLM1 style\n");
1341 for(i=0; i < pMessage->cBuffers; ++i)
1343 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1345 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1346 pMessage->pBuffers[i].cbBuffer, crc);
1350 sig[ 0] = (sign_version >> 0) & 0xff;
1351 sig[ 1] = (sign_version >> 8) & 0xff;
1352 sig[ 2] = (sign_version >> 16) & 0xff;
1353 sig[ 3] = (sign_version >> 24) & 0xff;
1354 memset(sig+4, 0, 4);
1355 sig[ 8] = (crc >> 0) & 0xff;
1356 sig[ 9] = (crc >> 8) & 0xff;
1357 sig[10] = (crc >> 16) & 0xff;
1358 sig[11] = (crc >> 24) & 0xff;
1359 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1360 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1361 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1362 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1364 ++(helper->crypt.ntlm.seq_num);
1367 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1371 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1373 TRACE("Creating a dummy signature.\n");
1374 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1375 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1376 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1377 pMessage->pBuffers[token_idx].cbBuffer = 16;
1381 return SEC_E_UNSUPPORTED_FUNCTION;
1384 /***********************************************************************
1387 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1388 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1393 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1395 return SEC_E_INVALID_HANDLE;
1398 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1401 FIXME("Ignoring MessageSeqNo\n");
1403 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1404 return SEC_E_INVALID_TOKEN;
1406 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1407 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1408 return SEC_E_INVALID_TOKEN;
1410 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1411 return SEC_E_BUFFER_TOO_SMALL;
1413 helper = (PNegoHelper)phContext->dwLower;
1414 TRACE("Negotiated flags are: 0x%08lx\n", helper->neg_flags);
1416 return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1419 /***********************************************************************
1422 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1423 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1429 SECURITY_STATUS ret;
1430 SecBufferDesc local_desc;
1431 PSecBuffer local_buff;
1434 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1436 return SEC_E_INVALID_HANDLE;
1438 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1439 return SEC_E_INVALID_TOKEN;
1441 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1442 return SEC_E_INVALID_TOKEN;
1444 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1445 return SEC_E_BUFFER_TOO_SMALL;
1448 FIXME("Ignoring MessageSeqNo\n");
1450 helper = (PNegoHelper)phContext->dwLower;
1451 TRACE("Negotiated flags: 0x%08lx\n", helper->neg_flags);
1453 local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1455 local_desc.ulVersion = SECBUFFER_VERSION;
1456 local_desc.cBuffers = pMessage->cBuffers;
1457 local_desc.pBuffers = local_buff;
1459 for(i=0; i < pMessage->cBuffers; ++i)
1461 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1463 local_buff[i].BufferType = SECBUFFER_TOKEN;
1464 local_buff[i].cbBuffer = 16;
1465 local_buff[i].pvBuffer = local_sig;
1469 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1470 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1471 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1475 if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1478 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1479 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1480 ret = SEC_E_MESSAGE_ALTERED;
1484 HeapFree(GetProcessHeap(), 0, local_buff);
1491 /***********************************************************************
1492 * FreeCredentialsHandle
1494 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1495 PCredHandle phCredential)
1497 SECURITY_STATUS ret;
1500 PNegoHelper helper = (PNegoHelper) phCredential->dwLower;
1501 phCredential->dwUpper = 0;
1502 phCredential->dwLower = 0;
1503 cleanup_helper(helper);
1512 /***********************************************************************
1515 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1516 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1521 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1524 return SEC_E_INVALID_HANDLE;
1527 FIXME("Ignoring fQOP\n");
1530 FIXME("Ignoring MessageSeqNo\n");
1532 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1533 return SEC_E_INVALID_TOKEN;
1535 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1536 return SEC_E_INVALID_TOKEN;
1538 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1539 return SEC_E_BUFFER_TOO_SMALL;
1541 helper = (PNegoHelper) phContext->dwLower;
1543 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1544 helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1546 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1547 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1548 (BYTE *)pMessage->pBuffers[1].pvBuffer,
1549 pMessage->pBuffers[1].cbBuffer);
1551 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1552 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1553 ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1563 /* EncryptMessage always produces real signatures, so make sure
1564 * NTLMSSP_NEGOTIATE_SIGN is set*/
1565 save_flags = helper->neg_flags;
1566 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1567 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1568 helper->neg_flags = save_flags;
1570 sig = pMessage->pBuffers[token_idx].pvBuffer;
1572 SECUR32_arc4Process(helper->crypt.ntlm.a4i, pMessage->pBuffers[1].pvBuffer,
1573 pMessage->pBuffers[1].cbBuffer);
1574 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1576 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1577 memset(sig+4, 0, 4);
1584 /***********************************************************************
1587 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1588 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1590 SECURITY_STATUS ret;
1591 ULONG ntlmssp_flags_save;
1594 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1597 return SEC_E_INVALID_HANDLE;
1600 FIXME("Ignoring MessageSeqNo\n");
1602 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1603 return SEC_E_INVALID_TOKEN;
1605 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1606 return SEC_E_INVALID_TOKEN;
1608 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1609 return SEC_E_BUFFER_TOO_SMALL;
1611 helper = (PNegoHelper) phContext->dwLower;
1613 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1615 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1616 pMessage->pBuffers[1].pvBuffer, pMessage->pBuffers[1].cbBuffer);
1620 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1621 pMessage->pBuffers[1].pvBuffer, pMessage->pBuffers[1].cbBuffer);
1624 /* Make sure we use a session key for the signature check, EncryptMessage
1625 * always does that, even in the dummy case */
1626 ntlmssp_flags_save = helper->neg_flags;
1628 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1629 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1631 helper->neg_flags = ntlmssp_flags_save;
1636 static const SecurityFunctionTableA ntlmTableA = {
1638 NULL, /* EnumerateSecurityPackagesA */
1639 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1640 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1641 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1642 NULL, /* Reserved2 */
1643 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1644 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1645 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1646 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1647 NULL, /* ApplyControlToken */
1648 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1649 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1650 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1651 ntlm_MakeSignature, /* MakeSignature */
1652 ntlm_VerifySignature, /* VerifySignature */
1653 FreeContextBuffer, /* FreeContextBuffer */
1654 NULL, /* QuerySecurityPackageInfoA */
1655 NULL, /* Reserved3 */
1656 NULL, /* Reserved4 */
1657 NULL, /* ExportSecurityContext */
1658 NULL, /* ImportSecurityContextA */
1659 NULL, /* AddCredentialsA */
1660 NULL, /* Reserved8 */
1661 NULL, /* QuerySecurityContextToken */
1662 ntlm_EncryptMessage, /* EncryptMessage */
1663 ntlm_DecryptMessage, /* DecryptMessage */
1664 NULL, /* SetContextAttributesA */
1667 static const SecurityFunctionTableW ntlmTableW = {
1669 NULL, /* EnumerateSecurityPackagesW */
1670 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1671 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1672 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1673 NULL, /* Reserved2 */
1674 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1675 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1676 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1677 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1678 NULL, /* ApplyControlToken */
1679 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1680 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1681 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1682 ntlm_MakeSignature, /* MakeSignature */
1683 ntlm_VerifySignature, /* VerifySignature */
1684 FreeContextBuffer, /* FreeContextBuffer */
1685 NULL, /* QuerySecurityPackageInfoW */
1686 NULL, /* Reserved3 */
1687 NULL, /* Reserved4 */
1688 NULL, /* ExportSecurityContext */
1689 NULL, /* ImportSecurityContextW */
1690 NULL, /* AddCredentialsW */
1691 NULL, /* Reserved8 */
1692 NULL, /* QuerySecurityContextToken */
1693 ntlm_EncryptMessage, /* EncryptMessage */
1694 ntlm_DecryptMessage, /* DecryptMessage */
1695 NULL, /* SetContextAttributesW */
1698 #define NTLM_COMMENT \
1699 { 'N', 'T', 'L', 'M', ' ', \
1700 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1701 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1703 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1704 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1706 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1708 static char ntlm_name_A[] = NTLM_NAME;
1709 static WCHAR ntlm_name_W[] = NTLM_NAME;
1711 /* According to Windows, NTLM has the following capabilities. */
1713 SECPKG_FLAG_INTEGRITY | \
1714 SECPKG_FLAG_PRIVACY | \
1715 SECPKG_FLAG_TOKEN_ONLY | \
1716 SECPKG_FLAG_CONNECTION | \
1717 SECPKG_FLAG_MULTI_REQUIRED | \
1718 SECPKG_FLAG_IMPERSONATION | \
1719 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1720 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1722 static const SecPkgInfoW infoW = {
1731 static const SecPkgInfoA infoA = {
1740 void SECUR32_initNTLMSP(void)
1742 SECURITY_STATUS ret;
1744 static CHAR version[] = "--version";
1746 SEC_CHAR *args[] = {
1751 if((ret = fork_helper(&helper, ntlm_auth, args)) != SEC_E_OK)
1753 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1754 helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1755 helper->major = helper->minor = helper->micro = -1;
1758 check_version(helper);
1760 if( (helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
1761 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1762 helper->minor > MIN_NTLM_AUTH_MINOR_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);