2 * Copyright 2005 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.
29 #include "secur32_priv.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
34 #define NTLM_MAX_BUF 2010
37 /***********************************************************************
38 * QueryCredentialsAttributesA
40 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
41 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
45 TRACE("(%p, %ld, %p)\n", phCredential, ulAttribute, pBuffer);
47 if(ulAttribute == SECPKG_ATTR_NAMES)
49 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
50 ret = SEC_E_UNSUPPORTED_FUNCTION;
53 ret = SEC_E_UNSUPPORTED_FUNCTION;
58 /***********************************************************************
59 * QueryCredentialsAttributesW
61 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
62 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
66 TRACE("(%p, %ld, %p)\n", phCredential, ulAttribute, pBuffer);
68 if(ulAttribute == SECPKG_ATTR_NAMES)
70 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
71 ret = SEC_E_UNSUPPORTED_FUNCTION;
74 ret = SEC_E_UNSUPPORTED_FUNCTION;
79 /***********************************************************************
80 * AcquireCredentialsHandleW
82 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
83 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
84 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
85 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
88 PNegoHelper helper = NULL;
90 SEC_CHAR *client_user_arg = NULL;
91 SEC_CHAR *client_domain_arg = NULL;
92 SEC_WCHAR *username = NULL, *domain = NULL;
94 SEC_CHAR *client_argv[5];
95 SEC_CHAR *server_argv[] = { "ntlm_auth",
96 "--helper-protocol=squid-2.5-ntlmssp",
99 TRACE("(%s, %s, 0x%08lx, %p, %p, %p, %p, %p, %p)\n",
100 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
101 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
104 switch(fCredentialUse)
106 case SECPKG_CRED_INBOUND:
107 if( (ret = fork_helper(&helper, "ntlm_auth", server_argv)) !=
115 helper->mode = NTLM_SERVER;
116 phCredential->dwUpper = fCredentialUse;
117 phCredential->dwLower = (DWORD)helper;
121 case SECPKG_CRED_OUTBOUND:
123 static const char username_arg[] = "--username=";
124 static const char domain_arg[] = "--domain=";
127 if(pAuthData == NULL)
129 LPWKSTA_USER_INFO_1 ui = NULL;
130 NET_API_STATUS status;
132 status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
133 if (status != NERR_Success || ui == NULL)
135 ret = SEC_E_NO_CREDENTIALS;
140 username = HeapAlloc(GetProcessHeap(), 0,
141 (lstrlenW(ui->wkui1_username)+1) *
143 lstrcpyW(username, ui->wkui1_username);
145 /* same for the domain */
146 domain = HeapAlloc(GetProcessHeap(), 0,
147 (lstrlenW(ui->wkui1_logon_domain)+1) *
149 lstrcpyW(domain, ui->wkui1_logon_domain);
150 NetApiBufferFree(ui);
154 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
155 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
157 if (!auth_data->UserLength || !auth_data->DomainLength)
159 ret = SEC_E_NO_CREDENTIALS;
163 /* Get username and domain from pAuthData */
164 username = HeapAlloc(GetProcessHeap(), 0,
165 (auth_data->UserLength + 1) * sizeof(SEC_WCHAR));
166 lstrcpyW(username, auth_data->User);
168 domain = HeapAlloc(GetProcessHeap(), 0,
169 (auth_data->DomainLength + 1) * sizeof(SEC_WCHAR));
170 lstrcpyW(domain, auth_data->Domain);
172 TRACE("Username is %s\n", debugstr_w(username));
173 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
174 username, -1, NULL, 0, NULL, NULL) + sizeof(username_arg);
175 client_user_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
176 lstrcpyA(client_user_arg, username_arg);
177 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, username, -1,
178 client_user_arg + sizeof(username_arg) - 1,
179 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
181 TRACE("Domain name is %s\n", debugstr_w(domain));
182 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
183 domain, -1, NULL, 0, NULL, NULL) + sizeof(domain_arg);
184 client_domain_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
185 lstrcpyA(client_domain_arg, domain_arg);
186 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domain,
187 -1, client_domain_arg + sizeof(domain_arg) - 1,
188 unixcp_size - sizeof(domain) + 1, NULL, NULL);
190 client_argv[0] = "ntlm_auth";
191 client_argv[1] = "--helper-protocol=ntlmssp-client-1";
192 client_argv[2] = client_user_arg;
193 client_argv[3] = client_domain_arg;
194 client_argv[4] = NULL;
196 if((ret = fork_helper(&helper, "ntlm_auth", client_argv)) !=
204 helper->mode = NTLM_CLIENT;
206 if(pAuthData != NULL)
208 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
209 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
211 if(auth_data->PasswordLength != 0)
213 helper->pwlen = WideCharToMultiByte(CP_UNIXCP,
214 WC_NO_BEST_FIT_CHARS, auth_data->Password,
215 auth_data->PasswordLength+1, NULL, 0, NULL, NULL);
217 helper->password = HeapAlloc(GetProcessHeap(), 0,
220 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
221 auth_data->Password, auth_data->PasswordLength+1,
222 helper->password, helper->pwlen, NULL, NULL);
226 phCredential->dwUpper = fCredentialUse;
227 phCredential->dwLower = (DWORD)helper;
228 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
229 phCredential->dwUpper, phCredential->dwLower);
234 case SECPKG_CRED_BOTH:
235 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
236 ret = SEC_E_UNSUPPORTED_FUNCTION;
241 ret = SEC_E_UNKNOWN_CREDENTIALS;
245 HeapFree(GetProcessHeap(), 0, client_user_arg);
246 HeapFree(GetProcessHeap(), 0, client_domain_arg);
247 HeapFree(GetProcessHeap(), 0, username);
248 HeapFree(GetProcessHeap(), 0, domain);
253 /***********************************************************************
254 * AcquireCredentialsHandleA
256 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
257 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
258 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
259 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
262 int user_sizeW, domain_sizeW, passwd_sizeW;
264 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
266 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
267 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
269 TRACE("(%s, %s, 0x%08lx, %p, %p, %p, %p, %p, %p)\n",
270 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
271 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
273 if(pszPackage != NULL)
275 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
278 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
280 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
284 if(pAuthData != NULL)
286 identity = (PSEC_WINNT_AUTH_IDENTITY_A)pAuthData;
288 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
290 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
291 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
293 if(identity->UserLength != 0)
295 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
296 (LPCSTR)identity->User, identity->UserLength+1, NULL, 0);
297 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
299 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
300 identity->UserLength+1, user, user_sizeW);
307 if(identity->DomainLength != 0)
309 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
310 (LPCSTR)identity->Domain, identity->DomainLength+1, NULL, 0);
311 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
312 * sizeof(SEC_WCHAR));
313 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
314 identity->DomainLength+1, domain, domain_sizeW);
321 if(identity->PasswordLength != 0)
323 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
324 (LPCSTR)identity->Password, identity->PasswordLength+1,
326 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
327 * sizeof(SEC_WCHAR));
328 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
329 identity->PasswordLength+1, passwd, passwd_sizeW);
336 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
337 pAuthDataW->User = user;
338 pAuthDataW->UserLength = user_sizeW;
339 pAuthDataW->Domain = domain;
340 pAuthDataW->DomainLength = domain_sizeW;
341 pAuthDataW->Password = passwd;
342 pAuthDataW->PasswordLength = passwd_sizeW;
346 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
350 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
351 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
354 HeapFree(GetProcessHeap(), 0, package);
355 HeapFree(GetProcessHeap(), 0, user);
356 HeapFree(GetProcessHeap(), 0, domain);
357 HeapFree(GetProcessHeap(), 0, passwd);
358 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
359 HeapFree(GetProcessHeap(), 0, pAuthDataW);
364 /***********************************************************************
365 * InitializeSecurityContextW
367 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
368 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
369 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
370 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
371 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
378 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
380 TRACE("%p %p %s %ld %ld %ld %p %ld %p %p %p %p\n", phCredential, phContext,
381 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
382 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
385 return SEC_E_INVALID_HANDLE;
387 /* As the server side of sspi never calls this, make sure that
388 * the handler is a client handler.
390 helper = (PNegoHelper)phCredential->dwLower;
391 if(helper->mode != NTLM_CLIENT)
393 TRACE("Helper mode = %d\n", helper->mode);
394 return SEC_E_INVALID_HANDLE;
397 /****************************************
398 * When communicating with the client, there can be the
399 * following reply packets:
400 * YR <base64 blob> should be sent to the server
401 * PW should be sent back to helper with
402 * base64 encoded password
403 * AF <base64 blob> client is done, blob should be
404 * sent to server with KK prefixed
405 * BH <char reason> something broke
407 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
411 TRACE("According to a MS whitepaper pszTargetName is ignored.\n");
413 /* Handle all the flags */
414 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
416 FIXME("InitializeSecurityContext(): ISC_REQ_CONFIDENTIALITY stub\n");
418 if(fContextReq & ISC_REQ_CONNECTION)
420 /* This is default, so we'll enable it */
421 ctxt_attr |= ISC_RET_CONNECTION;
423 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
424 FIXME("ISC_REQ_EXTENDED_ERROR\n");
425 if(fContextReq & ISC_REQ_INTEGRITY)
426 FIXME("ISC_REQ_INTEGRITY\n");
427 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
428 FIXME("ISC_REQ_MUTUAL_AUTH\n");
429 if(fContextReq & ISC_REQ_REPLAY_DETECT)
430 FIXME("ISC_REQ_REPLAY_DETECT\n");
431 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
432 FIXME("ISC_REQ_SEQUENCE_DETECT\n");
433 if(fContextReq & ISC_REQ_STREAM)
434 FIXME("ISC_REQ_STREAM\n");
436 /* Done with the flags */
437 if(TargetDataRep == SECURITY_NETWORK_DREP){
438 FIXME("Don't know how to do SECURITY_NETWORK_DREP\n");
439 return SEC_E_UNSUPPORTED_FUNCTION;
442 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
443 bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
445 if((phContext == NULL) && (pInput == NULL))
447 TRACE("First time in ISC()\n");
448 /* Request a challenge request from ntlm_auth */
449 if(helper->password == NULL)
451 FIXME("Using empty password for now.\n");
452 lstrcpynA(buffer, "PW AA==", max_len-1);
456 lstrcpynA(buffer, "PW ", max_len-1);
457 if((ret = encodeBase64((unsigned char*)helper->password,
458 helper->pwlen-2, buffer+3,
459 max_len-3, &buffer_len)) != SEC_E_OK)
461 TRACE("Deleting password!\n");
462 memset(helper->password, 0, helper->pwlen-2);
463 HeapFree(GetProcessHeap(), 0, helper->password);
469 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
470 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
473 TRACE("Helper returned %s\n", debugstr_a(buffer));
474 lstrcpynA(buffer, "YR", max_len-1);
476 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
479 TRACE("%s\n", buffer);
481 if(strncmp(buffer, "YR ", 3) != 0)
483 /* Something borked */
484 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
485 ret = SEC_E_INTERNAL_ERROR;
488 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
489 max_len-1, &bin_len)) != SEC_E_OK)
492 /* put the decoded client blob into the out buffer */
494 ret = SEC_I_CONTINUE_NEEDED;
498 /* handle second call here */
499 /* encode server data to base64 */
500 if (!pInput || !pInput->cBuffers)
502 ret = SEC_E_INCOMPLETE_MESSAGE;
506 if (!pInput->pBuffers[0].pvBuffer)
508 ret = SEC_E_INTERNAL_ERROR;
512 if(pInput->pBuffers[0].cbBuffer > max_len)
514 TRACE("pInput->pBuffers[0].cbBuffer is: %ld\n",
515 pInput->pBuffers[0].cbBuffer);
516 ret = SEC_E_INVALID_TOKEN;
520 bin_len = pInput->pBuffers[0].cbBuffer;
522 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
524 lstrcpynA(buffer, "TT ", max_len-1);
526 if((ret = encodeBase64(bin, bin_len, buffer+3,
527 max_len-3, &buffer_len)) != SEC_E_OK)
530 TRACE("Server sent: %s\n", debugstr_a(buffer));
532 /* send TT base64 blob to ntlm_auth */
533 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
536 TRACE("Helper replied: %s\n", debugstr_a(buffer));
538 if( (strncmp(buffer, "KK ", 3) != 0) &&
539 (strncmp(buffer, "AF ", 3) !=0))
541 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
542 HeapFree(GetProcessHeap(), 0, buffer);
543 HeapFree(GetProcessHeap(), 0, bin);
544 return SEC_E_INVALID_TOKEN;
547 /* decode the blob and send it to server */
548 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
549 &bin_len)) != SEC_E_OK)
551 HeapFree(GetProcessHeap(), 0, buffer);
552 HeapFree(GetProcessHeap(), 0, bin);
556 phNewContext->dwUpper = ctxt_attr;
557 phNewContext->dwLower = ret;
562 /* put the decoded client blob into the out buffer */
564 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
568 pOutput->cBuffers = 1;
569 pOutput->pBuffers[0].pvBuffer = SECUR32_ALLOC(bin_len);
570 pOutput->pBuffers[0].cbBuffer = bin_len;
574 if (!pOutput || !pOutput->cBuffers || pOutput->pBuffers[0].cbBuffer < bin_len)
576 TRACE("out buffer is NULL or has not enough space\n");
577 ret = SEC_E_BUFFER_TOO_SMALL;
581 if (!pOutput->pBuffers[0].pvBuffer)
583 TRACE("out buffer is NULL\n");
584 ret = SEC_E_INTERNAL_ERROR;
588 pOutput->pBuffers[0].cbBuffer = bin_len;
589 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
590 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
592 if(ret != SEC_I_CONTINUE_NEEDED)
594 TRACE("Deleting password!\n");
596 memset(helper->password, 0, helper->pwlen-2);
597 HeapFree(GetProcessHeap(), 0, helper->password);
600 HeapFree(GetProcessHeap(), 0, buffer);
601 HeapFree(GetProcessHeap(), 0, bin);
605 /***********************************************************************
606 * InitializeSecurityContextA
608 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
609 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
610 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
611 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
612 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
616 TRACE("%p %p %s %ld %ld %ld %p %ld %p %p %p %p\n", phCredential, phContext,
617 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
618 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
622 SEC_WCHAR *target = NULL;
623 if(pszTargetName != NULL)
625 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
626 strlen(pszTargetName)+1, NULL, 0);
627 target = HeapAlloc(GetProcessHeap(), 0, target_size *
629 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
630 target, target_size);
633 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
634 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
635 phNewContext, pOutput, pfContextAttr, ptsExpiry);
637 HeapFree(GetProcessHeap(), 0, target);
641 ret = SEC_E_INVALID_HANDLE;
646 /***********************************************************************
647 * AcceptSecurityContext
649 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
650 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
651 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
652 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
656 TRACE("%p %p %p %ld %ld %p %p %p %p\n", phCredential, phContext, pInput,
657 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
661 PNegoHelper helper = (PNegoHelper)phCredential->dwLower;
662 /* Max size of input data is 2010 byte, as that's the maximum size
663 * ntlm_auth will handle*/
664 char *buffer = HeapAlloc(GetProcessHeap(), 0,
665 sizeof(char) * NTLM_MAX_BUF);
666 PBYTE bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
667 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
670 if(helper->mode != NTLM_SERVER)
672 HeapFree(GetProcessHeap(), 0, buffer);
673 HeapFree(GetProcessHeap(), 0, bin);
674 return SEC_E_INVALID_HANDLE;
677 /* Handle all the flags */
678 if(fContextReq & ISC_REQ_ALLOCATE_MEMORY)
680 FIXME("AcceptSecurityContext(): ISC_REQ_ALLOCATE_MEMORY stub\n");
682 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
684 FIXME("AcceptSecurityContext(): ISC_REQ_CONFIDENTIALITY stub\n");
686 if(fContextReq & ISC_REQ_CONNECTION)
688 /* This is default, so we'll enable it */
689 ctxt_attr |= ISC_RET_CONNECTION;
691 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
693 FIXME("AcceptSecurityContext(): ISC_REQ_EXTENDED_ERROR stub\n");
695 if(fContextReq & ISC_REQ_INTEGRITY)
697 FIXME("AcceptSecurityContext(): ISC_REQ_INTEGRITY stub\n");
699 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
701 FIXME("AcceptSecurityContext(): ISC_REQ_MUTUAL_AUTH stub\n");
703 if(fContextReq & ISC_REQ_REPLAY_DETECT)
705 FIXME("AcceptSecurityContext(): ISC_REQ_REPLAY_DETECT stub\n");
707 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
709 FIXME("AcceptSecurityContext(): ISC_REQ_SEQUENCE_DETECT stub\n");
711 if(fContextReq & ISC_REQ_STREAM)
713 FIXME("AcceptSecurityContext(): ISC_REQ_STREAM stub\n");
715 /* Done with the flags */
716 if(TargetDataRep == SECURITY_NETWORK_DREP){
717 FIXME("Don't know how to do SECURITY_NETWORK_DREP\n");
718 HeapFree(GetProcessHeap(), 0, buffer);
719 HeapFree(GetProcessHeap(), 0, bin);
720 return SEC_E_UNSUPPORTED_FUNCTION;
724 if(phContext == NULL)
726 /* This is the first call to AcceptSecurityHandle */
729 HeapFree(GetProcessHeap(), 0, buffer);
730 HeapFree(GetProcessHeap(), 0, bin);
731 return SEC_E_INCOMPLETE_MESSAGE;
734 if(pInput->cBuffers < 1)
736 HeapFree(GetProcessHeap(), 0, buffer);
737 HeapFree(GetProcessHeap(), 0, bin);
738 return SEC_E_INCOMPLETE_MESSAGE;
741 if(pInput->pBuffers[0].cbBuffer > max_len)
743 HeapFree(GetProcessHeap(), 0, buffer);
744 HeapFree(GetProcessHeap(), 0, bin);
745 return SEC_E_INVALID_TOKEN;
748 bin_len = pInput->pBuffers[0].cbBuffer;
750 /* This is the YR request from the client, encode to base64 */
752 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
754 lstrcpynA(buffer, "YR ", max_len-1);
756 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
757 &buffer_len)) != SEC_E_OK)
759 HeapFree(GetProcessHeap(), 0, buffer);
760 HeapFree(GetProcessHeap(), 0, bin);
764 TRACE("Client sent: %s\n", debugstr_a(buffer));
766 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
769 HeapFree(GetProcessHeap(), 0, buffer);
770 HeapFree(GetProcessHeap(), 0, bin);
774 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
775 /* The expected answer is TT <base64 blob> */
777 if(strncmp(buffer, "TT ", 3) != 0)
779 HeapFree(GetProcessHeap(), 0, buffer);
780 HeapFree(GetProcessHeap(), 0, bin);
781 return SEC_E_INVALID_TOKEN;
784 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
785 &bin_len)) != SEC_E_OK)
787 HeapFree(GetProcessHeap(), 0, buffer);
788 HeapFree(GetProcessHeap(), 0, bin);
792 /* send this to the client */
795 HeapFree(GetProcessHeap(), 0, buffer);
796 HeapFree(GetProcessHeap(), 0, bin);
797 return SEC_E_INSUFFICIENT_MEMORY;
800 if(pOutput->cBuffers < 1)
802 HeapFree(GetProcessHeap(), 0, buffer);
803 HeapFree(GetProcessHeap(), 0, bin);
804 return SEC_E_INSUFFICIENT_MEMORY;
807 pOutput->pBuffers[0].cbBuffer = bin_len;
808 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
809 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
810 ret = SEC_I_CONTINUE_NEEDED;
815 /* we expect a KK request from client */
818 HeapFree(GetProcessHeap(), 0, buffer);
819 HeapFree(GetProcessHeap(), 0, bin);
820 return SEC_E_INCOMPLETE_MESSAGE;
823 if(pInput->cBuffers < 1)
825 HeapFree(GetProcessHeap(), 0, buffer);
826 HeapFree(GetProcessHeap(), 0, bin);
827 return SEC_E_INCOMPLETE_MESSAGE;
830 if(pInput->pBuffers[0].cbBuffer > max_len)
832 HeapFree(GetProcessHeap(), 0, buffer);
833 HeapFree(GetProcessHeap(), 0, bin);
834 return SEC_E_INVALID_TOKEN;
837 bin_len = pInput->pBuffers[0].cbBuffer;
839 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
841 lstrcpynA(buffer, "KK ", max_len-1);
843 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
844 &buffer_len)) != SEC_E_OK)
846 HeapFree(GetProcessHeap(), 0, buffer);
847 HeapFree(GetProcessHeap(), 0, bin);
851 TRACE("Client sent: %s\n", debugstr_a(buffer));
853 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
856 HeapFree(GetProcessHeap(), 0, buffer);
857 HeapFree(GetProcessHeap(), 0, bin);
861 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
863 if(strncmp(buffer, "AF ", 3) != 0)
865 if(strncmp(buffer, "NA ", 3) == 0)
867 HeapFree(GetProcessHeap(), 0, buffer);
868 HeapFree(GetProcessHeap(), 0, bin);
869 return SEC_E_LOGON_DENIED;
873 HeapFree(GetProcessHeap(), 0, buffer);
874 HeapFree(GetProcessHeap(), 0, bin);
875 return SEC_E_INVALID_TOKEN;
882 phNewContext->dwUpper = ctxt_attr;
883 phNewContext->dwLower = ret;
884 HeapFree(GetProcessHeap(), 0, buffer);
885 HeapFree(GetProcessHeap(), 0, bin);
890 ret = SEC_E_INVALID_HANDLE;
895 /***********************************************************************
898 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
899 PSecBufferDesc pToken)
903 TRACE("%p %p\n", phContext, pToken);
906 ret = SEC_E_UNSUPPORTED_FUNCTION;
910 ret = SEC_E_INVALID_HANDLE;
915 /***********************************************************************
916 * DeleteSecurityContext
918 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
922 TRACE("%p\n", phContext);
925 phContext->dwUpper = 0;
926 phContext->dwLower = 0;
931 ret = SEC_E_INVALID_HANDLE;
936 /***********************************************************************
937 * QueryContextAttributesW
939 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
940 unsigned long ulAttribute, void *pBuffer)
944 /* FIXME: From reading wrapper.h, I think the dwUpper part of a context is
945 * the SecurePackage part and the dwLower part is the actual context
946 * handle. It should be easy to extract the context attributes from that.
948 TRACE("%p %ld %p\n", phContext, ulAttribute, pBuffer);
951 ret = SEC_E_UNSUPPORTED_FUNCTION;
955 ret = SEC_E_INVALID_HANDLE;
960 /***********************************************************************
961 * QueryContextAttributesA
963 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
964 unsigned long ulAttribute, void *pBuffer)
966 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
969 /***********************************************************************
970 * ImpersonateSecurityContext
972 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
976 TRACE("%p\n", phContext);
979 ret = SEC_E_UNSUPPORTED_FUNCTION;
983 ret = SEC_E_INVALID_HANDLE;
988 /***********************************************************************
989 * RevertSecurityContext
991 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
995 TRACE("%p\n", phContext);
998 ret = SEC_E_UNSUPPORTED_FUNCTION;
1002 ret = SEC_E_INVALID_HANDLE;
1007 /***********************************************************************
1010 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1011 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1013 SECURITY_STATUS ret;
1015 TRACE("%p %ld %p %ld\n", phContext, fQOP, pMessage, MessageSeqNo);
1018 ret = SEC_E_UNSUPPORTED_FUNCTION;
1022 ret = SEC_E_INVALID_HANDLE;
1027 /***********************************************************************
1030 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1031 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1033 SECURITY_STATUS ret;
1035 TRACE("%p %p %ld %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1038 ret = SEC_E_UNSUPPORTED_FUNCTION;
1042 ret = SEC_E_INVALID_HANDLE;
1047 /***********************************************************************
1048 * FreeCredentialsHandle
1050 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1051 PCredHandle phCredential)
1053 SECURITY_STATUS ret;
1056 PNegoHelper helper = (PNegoHelper) phCredential->dwLower;
1057 phCredential->dwUpper = 0;
1058 phCredential->dwLower = 0;
1059 cleanup_helper(helper);
1068 static SecurityFunctionTableA ntlmTableA = {
1070 NULL, /* EnumerateSecurityPackagesA */
1071 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1072 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1073 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1074 NULL, /* Reserved2 */
1075 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1076 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1077 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1078 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1079 NULL, /* ApplyControlToken */
1080 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1081 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1082 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1083 ntlm_MakeSignature, /* MakeSignature */
1084 ntlm_VerifySignature, /* VerifySignature */
1085 FreeContextBuffer, /* FreeContextBuffer */
1086 NULL, /* QuerySecurityPackageInfoA */
1087 NULL, /* Reserved3 */
1088 NULL, /* Reserved4 */
1089 NULL, /* ExportSecurityContext */
1090 NULL, /* ImportSecurityContextA */
1091 NULL, /* AddCredentialsA */
1092 NULL, /* Reserved8 */
1093 NULL, /* QuerySecurityContextToken */
1094 NULL, /* EncryptMessage */
1095 NULL, /* DecryptMessage */
1096 NULL, /* SetContextAttributesA */
1099 static SecurityFunctionTableW ntlmTableW = {
1101 NULL, /* EnumerateSecurityPackagesW */
1102 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1103 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1104 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1105 NULL, /* Reserved2 */
1106 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1107 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1108 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1109 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1110 NULL, /* ApplyControlToken */
1111 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1112 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1113 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1114 ntlm_MakeSignature, /* MakeSignature */
1115 ntlm_VerifySignature, /* VerifySignature */
1116 FreeContextBuffer, /* FreeContextBuffer */
1117 NULL, /* QuerySecurityPackageInfoW */
1118 NULL, /* Reserved3 */
1119 NULL, /* Reserved4 */
1120 NULL, /* ExportSecurityContext */
1121 NULL, /* ImportSecurityContextW */
1122 NULL, /* AddCredentialsW */
1123 NULL, /* Reserved8 */
1124 NULL, /* QuerySecurityContextToken */
1125 NULL, /* EncryptMessage */
1126 NULL, /* DecryptMessage */
1127 NULL, /* SetContextAttributesW */
1130 #define NTLM_COMMENT \
1131 { 'N', 'T', 'L', 'M', ' ', \
1132 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1133 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1135 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1136 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1138 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1140 static char ntlm_name_A[] = NTLM_NAME;
1141 static WCHAR ntlm_name_W[] = NTLM_NAME;
1143 /* According to Windows, NTLM has the following capabilities. */
1145 SECPKG_FLAG_INTEGRITY | \
1146 SECPKG_FLAG_PRIVACY | \
1147 SECPKG_FLAG_TOKEN_ONLY | \
1148 SECPKG_FLAG_CONNECTION | \
1149 SECPKG_FLAG_MULTI_REQUIRED | \
1150 SECPKG_FLAG_IMPERSONATION | \
1151 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1152 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1154 static const SecPkgInfoW infoW = {
1163 static const SecPkgInfoA infoA = {
1172 void SECUR32_initNTLMSP(void)
1174 SECURITY_STATUS ret;
1177 SEC_CHAR *args[] = {
1182 if((ret = fork_helper(&helper, "ntlm_auth", args)) != SEC_E_OK)
1184 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1185 helper = HeapAlloc(GetProcessHeap,0, sizeof(PNegoHelper));
1186 helper->version = -1;
1189 check_version(helper);
1191 if(helper->version > 2)
1193 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1194 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1196 cleanup_helper(helper);