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 1904
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;
89 static CHAR ntlm_auth[] = "ntlm_auth",
90 helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp";
92 SEC_CHAR *client_user_arg = NULL;
93 SEC_CHAR *client_domain_arg = NULL;
94 SEC_WCHAR *username = NULL, *domain = NULL;
96 SEC_CHAR *client_argv[5];
97 SEC_CHAR *server_argv[] = { ntlm_auth,
101 TRACE("(%s, %s, 0x%08lx, %p, %p, %p, %p, %p, %p)\n",
102 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
103 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
106 switch(fCredentialUse)
108 case SECPKG_CRED_INBOUND:
109 if( (ret = fork_helper(&helper, "ntlm_auth", server_argv)) !=
117 helper->mode = NTLM_SERVER;
118 phCredential->dwUpper = fCredentialUse;
119 phCredential->dwLower = (DWORD)helper;
123 case SECPKG_CRED_OUTBOUND:
125 static const char username_arg[] = "--username=";
126 static const char domain_arg[] = "--domain=";
127 static char ntlm_auth[] = "ntlm_auth";
128 static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
131 if(pAuthData == NULL)
133 LPWKSTA_USER_INFO_1 ui = NULL;
134 NET_API_STATUS status;
136 status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
137 if (status != NERR_Success || ui == NULL)
139 ret = SEC_E_NO_CREDENTIALS;
144 username = HeapAlloc(GetProcessHeap(), 0,
145 (lstrlenW(ui->wkui1_username)+1) *
147 lstrcpyW(username, ui->wkui1_username);
149 /* same for the domain */
150 domain = HeapAlloc(GetProcessHeap(), 0,
151 (lstrlenW(ui->wkui1_logon_domain)+1) *
153 lstrcpyW(domain, ui->wkui1_logon_domain);
154 NetApiBufferFree(ui);
158 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
159 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
161 if (!auth_data->UserLength || !auth_data->DomainLength)
163 ret = SEC_E_NO_CREDENTIALS;
167 /* Get username and domain from pAuthData */
168 username = HeapAlloc(GetProcessHeap(), 0,
169 (auth_data->UserLength + 1) * sizeof(SEC_WCHAR));
170 lstrcpyW(username, auth_data->User);
172 domain = HeapAlloc(GetProcessHeap(), 0,
173 (auth_data->DomainLength + 1) * sizeof(SEC_WCHAR));
174 lstrcpyW(domain, auth_data->Domain);
176 TRACE("Username is %s\n", debugstr_w(username));
177 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
178 username, -1, NULL, 0, NULL, NULL) + sizeof(username_arg);
179 client_user_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
180 lstrcpyA(client_user_arg, username_arg);
181 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, username, -1,
182 client_user_arg + sizeof(username_arg) - 1,
183 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
185 TRACE("Domain name is %s\n", debugstr_w(domain));
186 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
187 domain, -1, NULL, 0, NULL, NULL) + sizeof(domain_arg);
188 client_domain_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
189 lstrcpyA(client_domain_arg, domain_arg);
190 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domain,
191 -1, client_domain_arg + sizeof(domain_arg) - 1,
192 unixcp_size - sizeof(domain) + 1, NULL, NULL);
194 client_argv[0] = ntlm_auth;
195 client_argv[1] = helper_protocol;
196 client_argv[2] = client_user_arg;
197 client_argv[3] = client_domain_arg;
198 client_argv[4] = NULL;
200 if((ret = fork_helper(&helper, "ntlm_auth", client_argv)) !=
208 helper->mode = NTLM_CLIENT;
210 if(pAuthData != NULL)
212 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
213 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
215 if(auth_data->PasswordLength != 0)
217 helper->pwlen = WideCharToMultiByte(CP_UNIXCP,
218 WC_NO_BEST_FIT_CHARS, auth_data->Password,
219 auth_data->PasswordLength+1, NULL, 0, NULL, NULL);
221 helper->password = HeapAlloc(GetProcessHeap(), 0,
224 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
225 auth_data->Password, auth_data->PasswordLength+1,
226 helper->password, helper->pwlen, NULL, NULL);
230 phCredential->dwUpper = fCredentialUse;
231 phCredential->dwLower = (DWORD)helper;
232 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
233 phCredential->dwUpper, phCredential->dwLower);
238 case SECPKG_CRED_BOTH:
239 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
240 ret = SEC_E_UNSUPPORTED_FUNCTION;
245 ret = SEC_E_UNKNOWN_CREDENTIALS;
249 HeapFree(GetProcessHeap(), 0, client_user_arg);
250 HeapFree(GetProcessHeap(), 0, client_domain_arg);
251 HeapFree(GetProcessHeap(), 0, username);
252 HeapFree(GetProcessHeap(), 0, domain);
257 /***********************************************************************
258 * AcquireCredentialsHandleA
260 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
261 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
262 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
263 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
266 int user_sizeW, domain_sizeW, passwd_sizeW;
268 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
270 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
271 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
273 TRACE("(%s, %s, 0x%08lx, %p, %p, %p, %p, %p, %p)\n",
274 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
275 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
277 if(pszPackage != NULL)
279 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
282 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
284 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
288 if(pAuthData != NULL)
290 identity = (PSEC_WINNT_AUTH_IDENTITY_A)pAuthData;
292 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
294 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
295 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
297 if(identity->UserLength != 0)
299 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
300 (LPCSTR)identity->User, identity->UserLength+1, NULL, 0);
301 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
303 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
304 identity->UserLength+1, user, user_sizeW);
311 if(identity->DomainLength != 0)
313 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
314 (LPCSTR)identity->Domain, identity->DomainLength+1, NULL, 0);
315 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
316 * sizeof(SEC_WCHAR));
317 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
318 identity->DomainLength+1, domain, domain_sizeW);
325 if(identity->PasswordLength != 0)
327 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
328 (LPCSTR)identity->Password, identity->PasswordLength+1,
330 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
331 * sizeof(SEC_WCHAR));
332 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
333 identity->PasswordLength+1, passwd, passwd_sizeW);
340 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
341 pAuthDataW->User = user;
342 pAuthDataW->UserLength = user_sizeW;
343 pAuthDataW->Domain = domain;
344 pAuthDataW->DomainLength = domain_sizeW;
345 pAuthDataW->Password = passwd;
346 pAuthDataW->PasswordLength = passwd_sizeW;
350 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
354 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
355 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
358 HeapFree(GetProcessHeap(), 0, package);
359 HeapFree(GetProcessHeap(), 0, user);
360 HeapFree(GetProcessHeap(), 0, domain);
361 HeapFree(GetProcessHeap(), 0, passwd);
362 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
363 HeapFree(GetProcessHeap(), 0, pAuthDataW);
368 /***********************************************************************
369 * InitializeSecurityContextW
371 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
372 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
373 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
374 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
375 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
382 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
384 TRACE("%p %p %s %ld %ld %ld %p %ld %p %p %p %p\n", phCredential, phContext,
385 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
386 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
389 return SEC_E_INVALID_HANDLE;
391 /* As the server side of sspi never calls this, make sure that
392 * the handler is a client handler.
394 helper = (PNegoHelper)phCredential->dwLower;
395 if(helper->mode != NTLM_CLIENT)
397 TRACE("Helper mode = %d\n", helper->mode);
398 return SEC_E_INVALID_HANDLE;
401 /****************************************
402 * When communicating with the client, there can be the
403 * following reply packets:
404 * YR <base64 blob> should be sent to the server
405 * PW should be sent back to helper with
406 * base64 encoded password
407 * AF <base64 blob> client is done, blob should be
408 * sent to server with KK prefixed
409 * BH <char reason> something broke
411 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
415 TRACE("According to a MS whitepaper pszTargetName is ignored.\n");
417 /* Handle all the flags */
418 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
420 FIXME("InitializeSecurityContext(): ISC_REQ_CONFIDENTIALITY stub\n");
422 if(fContextReq & ISC_REQ_CONNECTION)
424 /* This is default, so we'll enable it */
425 ctxt_attr |= ISC_RET_CONNECTION;
427 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
428 FIXME("ISC_REQ_EXTENDED_ERROR\n");
429 if(fContextReq & ISC_REQ_INTEGRITY)
430 FIXME("ISC_REQ_INTEGRITY\n");
431 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
432 FIXME("ISC_REQ_MUTUAL_AUTH\n");
433 if(fContextReq & ISC_REQ_REPLAY_DETECT)
434 FIXME("ISC_REQ_REPLAY_DETECT\n");
435 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
436 FIXME("ISC_REQ_SEQUENCE_DETECT\n");
437 if(fContextReq & ISC_REQ_STREAM)
438 FIXME("ISC_REQ_STREAM\n");
440 /* Done with the flags */
441 if(TargetDataRep == SECURITY_NETWORK_DREP){
442 TRACE("Setting SECURITY_NETWORK_DREP\n");
445 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
446 bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
448 if((phContext == NULL) && (pInput == NULL))
450 TRACE("First time in ISC()\n");
451 /* Request a challenge request from ntlm_auth */
452 if(helper->password == NULL)
454 FIXME("Using empty password for now.\n");
455 lstrcpynA(buffer, "PW AA==", max_len-1);
459 lstrcpynA(buffer, "PW ", max_len-1);
460 if((ret = encodeBase64((unsigned char*)helper->password,
461 helper->pwlen-2, buffer+3,
462 max_len-3, &buffer_len)) != SEC_E_OK)
464 TRACE("Deleting password!\n");
465 memset(helper->password, 0, helper->pwlen-2);
466 HeapFree(GetProcessHeap(), 0, helper->password);
472 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
473 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
476 TRACE("Helper returned %s\n", debugstr_a(buffer));
477 lstrcpynA(buffer, "YR", max_len-1);
479 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
482 TRACE("%s\n", buffer);
484 if(strncmp(buffer, "YR ", 3) != 0)
486 /* Something borked */
487 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
488 ret = SEC_E_INTERNAL_ERROR;
491 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
492 max_len-1, &bin_len)) != SEC_E_OK)
495 /* put the decoded client blob into the out buffer */
497 ret = SEC_I_CONTINUE_NEEDED;
501 /* handle second call here */
502 /* encode server data to base64 */
503 if (!pInput || !pInput->cBuffers)
505 ret = SEC_E_INCOMPLETE_MESSAGE;
509 if (!pInput->pBuffers[0].pvBuffer)
511 ret = SEC_E_INTERNAL_ERROR;
515 if(pInput->pBuffers[0].cbBuffer > max_len)
517 TRACE("pInput->pBuffers[0].cbBuffer is: %ld\n",
518 pInput->pBuffers[0].cbBuffer);
519 ret = SEC_E_INVALID_TOKEN;
523 bin_len = pInput->pBuffers[0].cbBuffer;
525 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
527 lstrcpynA(buffer, "TT ", max_len-1);
529 if((ret = encodeBase64(bin, bin_len, buffer+3,
530 max_len-3, &buffer_len)) != SEC_E_OK)
533 TRACE("Server sent: %s\n", debugstr_a(buffer));
535 /* send TT base64 blob to ntlm_auth */
536 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
539 TRACE("Helper replied: %s\n", debugstr_a(buffer));
541 if( (strncmp(buffer, "KK ", 3) != 0) &&
542 (strncmp(buffer, "AF ", 3) !=0))
544 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
545 HeapFree(GetProcessHeap(), 0, buffer);
546 HeapFree(GetProcessHeap(), 0, bin);
547 return SEC_E_INVALID_TOKEN;
550 /* decode the blob and send it to server */
551 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
552 &bin_len)) != SEC_E_OK)
554 HeapFree(GetProcessHeap(), 0, buffer);
555 HeapFree(GetProcessHeap(), 0, bin);
559 phNewContext->dwUpper = ctxt_attr;
560 phNewContext->dwLower = ret;
565 /* put the decoded client blob into the out buffer */
567 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
571 pOutput->cBuffers = 1;
572 pOutput->pBuffers[0].pvBuffer = SECUR32_ALLOC(bin_len);
573 pOutput->pBuffers[0].cbBuffer = bin_len;
577 if (!pOutput || !pOutput->cBuffers || pOutput->pBuffers[0].cbBuffer < bin_len)
579 TRACE("out buffer is NULL or has not enough space\n");
580 ret = SEC_E_BUFFER_TOO_SMALL;
584 if (!pOutput->pBuffers[0].pvBuffer)
586 TRACE("out buffer is NULL\n");
587 ret = SEC_E_INTERNAL_ERROR;
591 pOutput->pBuffers[0].cbBuffer = bin_len;
592 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
593 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
595 if(ret != SEC_I_CONTINUE_NEEDED)
597 TRACE("Deleting password!\n");
599 memset(helper->password, 0, helper->pwlen-2);
600 HeapFree(GetProcessHeap(), 0, helper->password);
603 HeapFree(GetProcessHeap(), 0, buffer);
604 HeapFree(GetProcessHeap(), 0, bin);
608 /***********************************************************************
609 * InitializeSecurityContextA
611 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
612 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
613 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
614 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
615 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
619 TRACE("%p %p %s %ld %ld %ld %p %ld %p %p %p %p\n", phCredential, phContext,
620 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
621 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
625 SEC_WCHAR *target = NULL;
626 if(pszTargetName != NULL)
628 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
629 strlen(pszTargetName)+1, NULL, 0);
630 target = HeapAlloc(GetProcessHeap(), 0, target_size *
632 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
633 target, target_size);
636 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
637 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
638 phNewContext, pOutput, pfContextAttr, ptsExpiry);
640 HeapFree(GetProcessHeap(), 0, target);
644 ret = SEC_E_INVALID_HANDLE;
649 /***********************************************************************
650 * AcceptSecurityContext
652 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
653 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
654 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
655 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
659 TRACE("%p %p %p %ld %ld %p %p %p %p\n", phCredential, phContext, pInput,
660 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
664 PNegoHelper helper = (PNegoHelper)phCredential->dwLower;
665 /* Max size of input data is 2010 byte, as that's the maximum size
666 * ntlm_auth will handle*/
667 char *buffer = HeapAlloc(GetProcessHeap(), 0,
668 sizeof(char) * NTLM_MAX_BUF);
669 PBYTE bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
670 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
673 if(helper->mode != NTLM_SERVER)
675 HeapFree(GetProcessHeap(), 0, buffer);
676 HeapFree(GetProcessHeap(), 0, bin);
677 return SEC_E_INVALID_HANDLE;
680 /* Handle all the flags */
681 if(fContextReq & ISC_REQ_ALLOCATE_MEMORY)
683 FIXME("AcceptSecurityContext(): ISC_REQ_ALLOCATE_MEMORY stub\n");
685 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
687 FIXME("AcceptSecurityContext(): ISC_REQ_CONFIDENTIALITY stub\n");
689 if(fContextReq & ISC_REQ_CONNECTION)
691 /* This is default, so we'll enable it */
692 ctxt_attr |= ISC_RET_CONNECTION;
694 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
696 FIXME("AcceptSecurityContext(): ISC_REQ_EXTENDED_ERROR stub\n");
698 if(fContextReq & ISC_REQ_INTEGRITY)
700 FIXME("AcceptSecurityContext(): ISC_REQ_INTEGRITY stub\n");
702 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
704 FIXME("AcceptSecurityContext(): ISC_REQ_MUTUAL_AUTH stub\n");
706 if(fContextReq & ISC_REQ_REPLAY_DETECT)
708 FIXME("AcceptSecurityContext(): ISC_REQ_REPLAY_DETECT stub\n");
710 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
712 FIXME("AcceptSecurityContext(): ISC_REQ_SEQUENCE_DETECT stub\n");
714 if(fContextReq & ISC_REQ_STREAM)
716 FIXME("AcceptSecurityContext(): ISC_REQ_STREAM stub\n");
718 /* Done with the flags */
719 if(TargetDataRep == SECURITY_NETWORK_DREP){
720 TRACE("Using SECURITY_NETWORK_DREP\n");
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)
942 TRACE("%p %ld %p\n", phContext, ulAttribute, pBuffer);
944 return SEC_E_INVALID_HANDLE;
948 #define _x(x) case (x) : FIXME(#x" stub\n"); break
949 _x(SECPKG_ATTR_ACCESS_TOKEN);
950 _x(SECPKG_ATTR_AUTHORITY);
951 _x(SECPKG_ATTR_DCE_INFO);
952 _x(SECPKG_ATTR_FLAGS);
953 _x(SECPKG_ATTR_KEY_INFO);
954 _x(SECPKG_ATTR_LIFESPAN);
955 _x(SECPKG_ATTR_NAMES);
956 _x(SECPKG_ATTR_NATIVE_NAMES);
957 _x(SECPKG_ATTR_NEGOTIATION_INFO);
958 _x(SECPKG_ATTR_PACKAGE_INFO);
959 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
960 _x(SECPKG_ATTR_SESSION_KEY);
961 case SECPKG_ATTR_SIZES:
963 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
964 spcs->cbMaxToken = NTLM_MAX_BUF;
965 spcs->cbMaxSignature = 16;
966 spcs->cbBlockSize = 1;
967 spcs->cbSecurityTrailer = 16;
970 _x(SECPKG_ATTR_STREAM_SIZES);
971 _x(SECPKG_ATTR_TARGET_INFORMATION);
974 TRACE("Unknown value %ld passed for ulAttribute\n", ulAttribute);
977 return SEC_E_UNSUPPORTED_FUNCTION;
980 /***********************************************************************
981 * QueryContextAttributesA
983 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
984 unsigned long ulAttribute, void *pBuffer)
986 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
989 /***********************************************************************
990 * ImpersonateSecurityContext
992 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
996 TRACE("%p\n", phContext);
999 ret = SEC_E_UNSUPPORTED_FUNCTION;
1003 ret = SEC_E_INVALID_HANDLE;
1008 /***********************************************************************
1009 * RevertSecurityContext
1011 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1013 SECURITY_STATUS ret;
1015 TRACE("%p\n", phContext);
1018 ret = SEC_E_UNSUPPORTED_FUNCTION;
1022 ret = SEC_E_INVALID_HANDLE;
1027 /***********************************************************************
1030 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1031 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1033 SECURITY_STATUS ret;
1035 TRACE("%p %ld %p %ld\n", phContext, fQOP, pMessage, MessageSeqNo);
1038 ret = SEC_E_UNSUPPORTED_FUNCTION;
1042 ret = SEC_E_INVALID_HANDLE;
1047 /***********************************************************************
1050 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1051 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1053 SECURITY_STATUS ret;
1055 TRACE("%p %p %ld %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1058 ret = SEC_E_UNSUPPORTED_FUNCTION;
1062 ret = SEC_E_INVALID_HANDLE;
1067 /***********************************************************************
1068 * FreeCredentialsHandle
1070 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1071 PCredHandle phCredential)
1073 SECURITY_STATUS ret;
1076 PNegoHelper helper = (PNegoHelper) phCredential->dwLower;
1077 phCredential->dwUpper = 0;
1078 phCredential->dwLower = 0;
1079 cleanup_helper(helper);
1088 /***********************************************************************
1091 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1092 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1094 TRACE("%p %ld %p %ld stub\n", phContext, fQOP, pMessage, MessageSeqNo);
1097 return SEC_E_INVALID_HANDLE;
1099 return SEC_E_UNSUPPORTED_FUNCTION;
1102 /***********************************************************************
1105 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1106 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1108 TRACE("%p %p %ld %p stub\n", phContext, pMessage, MessageSeqNo, pfQOP);
1111 return SEC_E_INVALID_HANDLE;
1113 return SEC_E_UNSUPPORTED_FUNCTION;
1116 static SecurityFunctionTableA ntlmTableA = {
1118 NULL, /* EnumerateSecurityPackagesA */
1119 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1120 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1121 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1122 NULL, /* Reserved2 */
1123 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1124 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1125 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1126 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1127 NULL, /* ApplyControlToken */
1128 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1129 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1130 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1131 ntlm_MakeSignature, /* MakeSignature */
1132 ntlm_VerifySignature, /* VerifySignature */
1133 FreeContextBuffer, /* FreeContextBuffer */
1134 NULL, /* QuerySecurityPackageInfoA */
1135 NULL, /* Reserved3 */
1136 NULL, /* Reserved4 */
1137 NULL, /* ExportSecurityContext */
1138 NULL, /* ImportSecurityContextA */
1139 NULL, /* AddCredentialsA */
1140 NULL, /* Reserved8 */
1141 NULL, /* QuerySecurityContextToken */
1142 ntlm_EncryptMessage, /* EncryptMessage */
1143 ntlm_DecryptMessage, /* DecryptMessage */
1144 NULL, /* SetContextAttributesA */
1147 static SecurityFunctionTableW ntlmTableW = {
1149 NULL, /* EnumerateSecurityPackagesW */
1150 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1151 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1152 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1153 NULL, /* Reserved2 */
1154 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1155 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1156 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1157 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1158 NULL, /* ApplyControlToken */
1159 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1160 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1161 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1162 ntlm_MakeSignature, /* MakeSignature */
1163 ntlm_VerifySignature, /* VerifySignature */
1164 FreeContextBuffer, /* FreeContextBuffer */
1165 NULL, /* QuerySecurityPackageInfoW */
1166 NULL, /* Reserved3 */
1167 NULL, /* Reserved4 */
1168 NULL, /* ExportSecurityContext */
1169 NULL, /* ImportSecurityContextW */
1170 NULL, /* AddCredentialsW */
1171 NULL, /* Reserved8 */
1172 NULL, /* QuerySecurityContextToken */
1173 ntlm_EncryptMessage, /* EncryptMessage */
1174 ntlm_DecryptMessage, /* DecryptMessage */
1175 NULL, /* SetContextAttributesW */
1178 #define NTLM_COMMENT \
1179 { 'N', 'T', 'L', 'M', ' ', \
1180 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1181 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1183 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1184 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1186 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1188 static char ntlm_name_A[] = NTLM_NAME;
1189 static WCHAR ntlm_name_W[] = NTLM_NAME;
1191 /* According to Windows, NTLM has the following capabilities. */
1193 SECPKG_FLAG_INTEGRITY | \
1194 SECPKG_FLAG_PRIVACY | \
1195 SECPKG_FLAG_TOKEN_ONLY | \
1196 SECPKG_FLAG_CONNECTION | \
1197 SECPKG_FLAG_MULTI_REQUIRED | \
1198 SECPKG_FLAG_IMPERSONATION | \
1199 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1200 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1202 static const SecPkgInfoW infoW = {
1211 static const SecPkgInfoA infoA = {
1220 void SECUR32_initNTLMSP(void)
1222 SECURITY_STATUS ret;
1224 static CHAR ntlm_auth[] = "ntlm_auth",
1225 version[] = "--version";
1227 SEC_CHAR *args[] = {
1232 if((ret = fork_helper(&helper, "ntlm_auth", args)) != SEC_E_OK)
1234 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1235 helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1236 helper->version = -1;
1239 check_version(helper);
1241 if(helper->version > 2)
1243 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1244 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1246 cleanup_helper(helper);