2 * Copyright 2005, 2006 Kai Blin
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 * This file implements the NTLM security provider.
30 #include "secur32_priv.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
35 #define NTLM_MAX_BUF 1904
38 /***********************************************************************
39 * QueryCredentialsAttributesA
41 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
42 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
46 TRACE("(%p, %ld, %p)\n", phCredential, ulAttribute, pBuffer);
48 if(ulAttribute == SECPKG_ATTR_NAMES)
50 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
51 ret = SEC_E_UNSUPPORTED_FUNCTION;
54 ret = SEC_E_UNSUPPORTED_FUNCTION;
59 /***********************************************************************
60 * QueryCredentialsAttributesW
62 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
63 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
67 TRACE("(%p, %ld, %p)\n", phCredential, ulAttribute, pBuffer);
69 if(ulAttribute == SECPKG_ATTR_NAMES)
71 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
72 ret = SEC_E_UNSUPPORTED_FUNCTION;
75 ret = SEC_E_UNSUPPORTED_FUNCTION;
80 /***********************************************************************
81 * AcquireCredentialsHandleW
83 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
84 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
85 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
86 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
89 PNegoHelper helper = NULL;
90 static CHAR ntlm_auth[] = "ntlm_auth",
91 helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp";
93 SEC_CHAR *client_user_arg = NULL;
94 SEC_CHAR *client_domain_arg = NULL;
95 SEC_WCHAR *username = NULL, *domain = NULL;
97 SEC_CHAR *client_argv[5];
98 SEC_CHAR *server_argv[] = { ntlm_auth,
102 TRACE("(%s, %s, 0x%08lx, %p, %p, %p, %p, %p, %p)\n",
103 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
104 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
107 switch(fCredentialUse)
109 case SECPKG_CRED_INBOUND:
110 if( (ret = fork_helper(&helper, "ntlm_auth", server_argv)) !=
118 helper->mode = NTLM_SERVER;
119 phCredential->dwUpper = fCredentialUse;
120 phCredential->dwLower = (ULONG_PTR)helper;
124 case SECPKG_CRED_OUTBOUND:
126 static const char username_arg[] = "--username=";
127 static const char domain_arg[] = "--domain=";
128 static char ntlm_auth[] = "ntlm_auth";
129 static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
132 if(pAuthData == NULL)
134 LPWKSTA_USER_INFO_1 ui = NULL;
135 NET_API_STATUS status;
137 status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
138 if (status != NERR_Success || ui == NULL)
140 ret = SEC_E_NO_CREDENTIALS;
145 username = HeapAlloc(GetProcessHeap(), 0,
146 (lstrlenW(ui->wkui1_username)+1) *
148 lstrcpyW(username, ui->wkui1_username);
150 /* same for the domain */
151 domain = HeapAlloc(GetProcessHeap(), 0,
152 (lstrlenW(ui->wkui1_logon_domain)+1) *
154 lstrcpyW(domain, ui->wkui1_logon_domain);
155 NetApiBufferFree(ui);
159 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
160 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
162 if (!auth_data->UserLength || !auth_data->DomainLength)
164 ret = SEC_E_NO_CREDENTIALS;
168 /* Get username and domain from pAuthData */
169 username = HeapAlloc(GetProcessHeap(), 0,
170 (auth_data->UserLength + 1) * sizeof(SEC_WCHAR));
171 lstrcpyW(username, auth_data->User);
173 domain = HeapAlloc(GetProcessHeap(), 0,
174 (auth_data->DomainLength + 1) * sizeof(SEC_WCHAR));
175 lstrcpyW(domain, auth_data->Domain);
177 TRACE("Username is %s\n", debugstr_w(username));
178 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
179 username, -1, NULL, 0, NULL, NULL) + sizeof(username_arg);
180 client_user_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
181 lstrcpyA(client_user_arg, username_arg);
182 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, username, -1,
183 client_user_arg + sizeof(username_arg) - 1,
184 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
186 TRACE("Domain name is %s\n", debugstr_w(domain));
187 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
188 domain, -1, NULL, 0, NULL, NULL) + sizeof(domain_arg);
189 client_domain_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
190 lstrcpyA(client_domain_arg, domain_arg);
191 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domain,
192 -1, client_domain_arg + sizeof(domain_arg) - 1,
193 unixcp_size - sizeof(domain) + 1, NULL, NULL);
195 client_argv[0] = ntlm_auth;
196 client_argv[1] = helper_protocol;
197 client_argv[2] = client_user_arg;
198 client_argv[3] = client_domain_arg;
199 client_argv[4] = NULL;
201 if((ret = fork_helper(&helper, "ntlm_auth", client_argv)) !=
209 helper->mode = NTLM_CLIENT;
211 if(pAuthData != NULL)
213 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
214 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
216 if(auth_data->PasswordLength != 0)
218 helper->pwlen = WideCharToMultiByte(CP_UNIXCP,
219 WC_NO_BEST_FIT_CHARS, auth_data->Password,
220 auth_data->PasswordLength+1, NULL, 0, NULL, NULL);
222 helper->password = HeapAlloc(GetProcessHeap(), 0,
225 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
226 auth_data->Password, auth_data->PasswordLength+1,
227 helper->password, helper->pwlen, NULL, NULL);
231 phCredential->dwUpper = fCredentialUse;
232 phCredential->dwLower = (ULONG_PTR)helper;
233 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
234 phCredential->dwUpper, phCredential->dwLower);
239 case SECPKG_CRED_BOTH:
240 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
241 ret = SEC_E_UNSUPPORTED_FUNCTION;
246 ret = SEC_E_UNKNOWN_CREDENTIALS;
250 HeapFree(GetProcessHeap(), 0, client_user_arg);
251 HeapFree(GetProcessHeap(), 0, client_domain_arg);
252 HeapFree(GetProcessHeap(), 0, username);
253 HeapFree(GetProcessHeap(), 0, domain);
258 /***********************************************************************
259 * AcquireCredentialsHandleA
261 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
262 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
263 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
264 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
267 int user_sizeW, domain_sizeW, passwd_sizeW;
269 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
271 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
272 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
274 TRACE("(%s, %s, 0x%08lx, %p, %p, %p, %p, %p, %p)\n",
275 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
276 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
278 if(pszPackage != NULL)
280 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
283 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
285 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
289 if(pAuthData != NULL)
291 identity = (PSEC_WINNT_AUTH_IDENTITY_A)pAuthData;
293 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
295 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
296 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
298 if(identity->UserLength != 0)
300 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
301 (LPCSTR)identity->User, identity->UserLength+1, NULL, 0);
302 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
304 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
305 identity->UserLength+1, user, user_sizeW);
312 if(identity->DomainLength != 0)
314 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
315 (LPCSTR)identity->Domain, identity->DomainLength+1, NULL, 0);
316 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
317 * sizeof(SEC_WCHAR));
318 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
319 identity->DomainLength+1, domain, domain_sizeW);
326 if(identity->PasswordLength != 0)
328 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
329 (LPCSTR)identity->Password, identity->PasswordLength+1,
331 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
332 * sizeof(SEC_WCHAR));
333 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
334 identity->PasswordLength+1, passwd, passwd_sizeW);
341 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
342 pAuthDataW->User = user;
343 pAuthDataW->UserLength = user_sizeW;
344 pAuthDataW->Domain = domain;
345 pAuthDataW->DomainLength = domain_sizeW;
346 pAuthDataW->Password = passwd;
347 pAuthDataW->PasswordLength = passwd_sizeW;
351 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
355 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
356 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
359 HeapFree(GetProcessHeap(), 0, package);
360 HeapFree(GetProcessHeap(), 0, user);
361 HeapFree(GetProcessHeap(), 0, domain);
362 HeapFree(GetProcessHeap(), 0, passwd);
363 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
364 HeapFree(GetProcessHeap(), 0, pAuthDataW);
369 /***********************************************************************
370 * InitializeSecurityContextW
372 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
373 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
374 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
375 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
376 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
381 char* buffer, *want_flags = NULL;
383 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
385 TRACE("%p %p %s %ld %ld %ld %p %ld %p %p %p %p\n", phCredential, phContext,
386 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
387 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
390 return SEC_E_INVALID_HANDLE;
392 /* As the server side of sspi never calls this, make sure that
393 * the handler is a client handler.
395 helper = (PNegoHelper)phCredential->dwLower;
396 if(helper->mode != NTLM_CLIENT)
398 TRACE("Helper mode = %d\n", helper->mode);
399 return SEC_E_INVALID_HANDLE;
402 /****************************************
403 * When communicating with the client, there can be the
404 * following reply packets:
405 * YR <base64 blob> should be sent to the server
406 * PW should be sent back to helper with
407 * base64 encoded password
408 * AF <base64 blob> client is done, blob should be
409 * sent to server with KK prefixed
410 * GF <string list> A string list of negotiated flags
411 * GK <base64 blob> base64 encoded session key
412 * BH <char reason> something broke
414 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
418 TRACE("According to a MS whitepaper pszTargetName is ignored.\n");
421 if(TargetDataRep == SECURITY_NETWORK_DREP){
422 TRACE("Setting SECURITY_NETWORK_DREP\n");
425 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
426 bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
428 if((phContext == NULL) && (pInput == NULL))
430 TRACE("First time in ISC()\n");
431 /* Allocate space for a maximal string of
432 * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
433 * NTLMSSP_FEATURE_SESSION_KEY"
435 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
436 if(want_flags == NULL)
438 ret = SEC_E_INSUFFICIENT_MEMORY;
441 lstrcpyA(want_flags, "SF");
442 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
443 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
444 if(fContextReq & ISC_REQ_CONNECTION)
446 /* This is default, so we'll enable it */
447 ctxt_attr |= ISC_RET_CONNECTION;
448 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
450 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
451 FIXME("ISC_REQ_EXTENDED_ERROR\n");
452 if(fContextReq & ISC_REQ_INTEGRITY)
453 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
454 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
455 FIXME("ISC_REQ_MUTUAL_AUTH\n");
456 if(fContextReq & ISC_REQ_REPLAY_DETECT)
457 FIXME("ISC_REQ_REPLAY_DETECT\n");
458 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
459 FIXME("ISC_REQ_SEQUENCE_DETECT\n");
460 if(fContextReq & ISC_REQ_STREAM)
461 FIXME("ISC_REQ_STREAM\n");
463 /* Request a challenge request from ntlm_auth */
464 if(helper->password == NULL)
466 FIXME("Using empty password for now.\n");
467 lstrcpynA(buffer, "PW AA==", max_len-1);
471 lstrcpynA(buffer, "PW ", max_len-1);
472 if((ret = encodeBase64((unsigned char*)helper->password,
473 helper->pwlen-2, buffer+3,
474 max_len-3, &buffer_len)) != SEC_E_OK)
476 TRACE("Deleting password!\n");
477 memset(helper->password, 0, helper->pwlen-2);
478 HeapFree(GetProcessHeap(), 0, helper->password);
484 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
485 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
488 TRACE("Helper returned %s\n", debugstr_a(buffer));
490 if(lstrlenA(want_flags) > 2)
492 TRACE("Want flags are '%s'\n", debugstr_a(want_flags));
493 lstrcpynA(buffer, want_flags, max_len-1);
494 if((ret = run_helper(helper, buffer, max_len, &buffer_len))
497 if(!strncmp(buffer, "BH", 2))
498 TRACE("Helper doesn't understand new command set\n");
501 lstrcpynA(buffer, "YR", max_len-1);
503 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
506 TRACE("%s\n", buffer);
508 if(strncmp(buffer, "YR ", 3) != 0)
510 /* Something borked */
511 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
512 ret = SEC_E_INTERNAL_ERROR;
515 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
516 max_len-1, &bin_len)) != SEC_E_OK)
519 /* Mask away the NTLMv2 flag, as well as the key exchange flag */
523 /* put the decoded client blob into the out buffer */
525 ret = SEC_I_CONTINUE_NEEDED;
529 /* handle second call here */
530 /* encode server data to base64 */
531 if (!pInput || !pInput->cBuffers)
533 ret = SEC_E_INCOMPLETE_MESSAGE;
537 if (!pInput->pBuffers[0].pvBuffer)
539 ret = SEC_E_INTERNAL_ERROR;
543 if(pInput->pBuffers[0].cbBuffer > max_len)
545 TRACE("pInput->pBuffers[0].cbBuffer is: %ld\n",
546 pInput->pBuffers[0].cbBuffer);
547 ret = SEC_E_INVALID_TOKEN;
551 bin_len = pInput->pBuffers[0].cbBuffer;
553 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
555 lstrcpynA(buffer, "TT ", max_len-1);
557 if((ret = encodeBase64(bin, bin_len, buffer+3,
558 max_len-3, &buffer_len)) != SEC_E_OK)
561 TRACE("Server sent: %s\n", debugstr_a(buffer));
563 /* send TT base64 blob to ntlm_auth */
564 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
567 TRACE("Helper replied: %s\n", debugstr_a(buffer));
569 if( (strncmp(buffer, "KK ", 3) != 0) &&
570 (strncmp(buffer, "AF ", 3) !=0))
572 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
573 ret = SEC_E_INVALID_TOKEN;
577 /* decode the blob and send it to server */
578 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
579 &bin_len)) != SEC_E_OK)
584 phNewContext->dwUpper = ctxt_attr;
585 phNewContext->dwLower = (ULONG_PTR)helper;
590 /* put the decoded client blob into the out buffer */
592 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
596 pOutput->cBuffers = 1;
597 pOutput->pBuffers[0].pvBuffer = SECUR32_ALLOC(bin_len);
598 pOutput->pBuffers[0].cbBuffer = bin_len;
602 if (!pOutput || !pOutput->cBuffers || pOutput->pBuffers[0].cbBuffer < bin_len)
604 TRACE("out buffer is NULL or has not enough space\n");
605 ret = SEC_E_BUFFER_TOO_SMALL;
609 if (!pOutput->pBuffers[0].pvBuffer)
611 TRACE("out buffer is NULL\n");
612 ret = SEC_E_INTERNAL_ERROR;
616 pOutput->pBuffers[0].cbBuffer = bin_len;
617 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
618 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
622 TRACE("Getting negotiated flags\n");
623 lstrcpynA(buffer, "GF", max_len - 1);
624 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
629 TRACE("No flags negotiated, or helper does not support GF command\n");
633 TRACE("Negotiated %s\n", debugstr_a(buffer));
634 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
635 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
638 TRACE("Getting session key\n");
639 lstrcpynA(buffer, "GK", max_len - 1);
640 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
644 TRACE("Helper does not support GK command\n");
647 if(strncmp(buffer, "BH ", 3) == 0)
649 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
650 helper->valid_session_key = FALSE;
651 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
652 /*Generate the dummy session key = MD4(MD4(password))*/
654 SECUR32_CreateNTLMv1SessionKey(helper->password, helper->session_key);
656 memset(helper->session_key, 0, 16);
658 else if(strncmp(buffer, "GK ", 3) == 0)
660 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
661 &bin_len)) != SEC_E_OK)
663 TRACE("Failed to decode session key\n");
665 TRACE("Session key is %s\n", debugstr_a(buffer+3));
666 helper->valid_session_key = TRUE;
667 if(!helper->session_key)
668 helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
669 if(!helper->session_key)
671 TRACE("Failed to allocate memory for session key\n");
672 ret = SEC_E_INTERNAL_ERROR;
675 memcpy(helper->session_key, bin, bin_len);
678 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
679 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
680 helper->crypt.ntlm.seq_num = 0l;
683 if(ret != SEC_I_CONTINUE_NEEDED)
685 TRACE("Deleting password!\n");
687 memset(helper->password, 0, helper->pwlen-2);
688 HeapFree(GetProcessHeap(), 0, helper->password);
691 HeapFree(GetProcessHeap(), 0, want_flags);
692 HeapFree(GetProcessHeap(), 0, buffer);
693 HeapFree(GetProcessHeap(), 0, bin);
697 /***********************************************************************
698 * InitializeSecurityContextA
700 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
701 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
702 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
703 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
704 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
708 TRACE("%p %p %s %ld %ld %ld %p %ld %p %p %p %p\n", phCredential, phContext,
709 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
710 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
714 SEC_WCHAR *target = NULL;
715 if(pszTargetName != NULL)
717 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
718 strlen(pszTargetName)+1, NULL, 0);
719 target = HeapAlloc(GetProcessHeap(), 0, target_size *
721 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
722 target, target_size);
725 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
726 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
727 phNewContext, pOutput, pfContextAttr, ptsExpiry);
729 HeapFree(GetProcessHeap(), 0, target);
733 ret = SEC_E_INVALID_HANDLE;
738 /***********************************************************************
739 * AcceptSecurityContext
741 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
742 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
743 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
744 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
747 char *buffer, *want_flags = NULL;
749 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
753 TRACE("%p %p %p %ld %ld %p %p %p %p\n", phCredential, phContext, pInput,
754 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
758 return SEC_E_INVALID_HANDLE;
760 helper = (PNegoHelper)phCredential->dwLower;
762 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
763 bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
765 if(helper->mode != NTLM_SERVER)
767 ret = SEC_E_INVALID_HANDLE;
771 if(TargetDataRep == SECURITY_NETWORK_DREP){
772 TRACE("Using SECURITY_NETWORK_DREP\n");
775 if(phContext == NULL)
777 /* This is the first call to AcceptSecurityHandle */
780 ret = SEC_E_INCOMPLETE_MESSAGE;
784 if(pInput->cBuffers < 1)
786 ret = SEC_E_INCOMPLETE_MESSAGE;
790 if(pInput->pBuffers[0].cbBuffer > max_len)
792 ret = SEC_E_INVALID_TOKEN;
796 bin_len = pInput->pBuffers[0].cbBuffer;
798 /* Handle all the flags */
799 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
800 if(want_flags == NULL)
802 TRACE("Failed to allocate memory for the want_flags!\n");
803 ret = SEC_E_INSUFFICIENT_MEMORY;
806 lstrcpyA(want_flags, "SF");
807 if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
809 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
811 if(fContextReq & ASC_REQ_CONFIDENTIALITY)
813 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
815 if(fContextReq & ASC_REQ_CONNECTION)
817 /* This is default, so we'll enable it */
818 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
819 ctxt_attr |= ASC_RET_CONNECTION;
821 if(fContextReq & ASC_REQ_EXTENDED_ERROR)
823 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
825 if(fContextReq & ASC_REQ_INTEGRITY)
827 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
829 if(fContextReq & ASC_REQ_MUTUAL_AUTH)
831 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
833 if(fContextReq & ASC_REQ_REPLAY_DETECT)
835 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
837 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
839 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
841 if(fContextReq & ISC_REQ_STREAM)
843 FIXME("ASC_REQ_STREAM stub\n");
845 /* Done with the flags */
847 if(lstrlenA(want_flags) > 3)
849 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
850 lstrcpynA(buffer, want_flags, max_len - 1);
851 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
854 if(!strncmp(buffer, "BH", 2))
855 TRACE("Helper doesn't understand new command set\n");
858 /* This is the YR request from the client, encode to base64 */
860 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
862 lstrcpynA(buffer, "YR ", max_len-1);
864 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
865 &buffer_len)) != SEC_E_OK)
870 TRACE("Client sent: %s\n", debugstr_a(buffer));
872 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
878 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
879 /* The expected answer is TT <base64 blob> */
881 if(strncmp(buffer, "TT ", 3) != 0)
883 ret = SEC_E_INTERNAL_ERROR;
887 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
888 &bin_len)) != SEC_E_OK)
893 /* send this to the client */
896 ret = SEC_E_INSUFFICIENT_MEMORY;
900 if(pOutput->cBuffers < 1)
902 ret = SEC_E_INSUFFICIENT_MEMORY;
906 pOutput->pBuffers[0].cbBuffer = bin_len;
907 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
908 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
909 ret = SEC_I_CONTINUE_NEEDED;
914 /* we expect a KK request from client */
917 ret = SEC_E_INCOMPLETE_MESSAGE;
921 if(pInput->cBuffers < 1)
923 ret = SEC_E_INCOMPLETE_MESSAGE;
927 if(pInput->pBuffers[0].cbBuffer > max_len)
929 ret = SEC_E_INVALID_TOKEN;
933 bin_len = pInput->pBuffers[0].cbBuffer;
935 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
937 lstrcpynA(buffer, "KK ", max_len-1);
939 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
940 &buffer_len)) != SEC_E_OK)
945 TRACE("Client sent: %s\n", debugstr_a(buffer));
947 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
953 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
955 if(strncmp(buffer, "AF ", 3) != 0)
957 if(strncmp(buffer, "NA ", 3) == 0)
959 ret = SEC_E_LOGON_DENIED;
964 ret = SEC_E_INTERNAL_ERROR;
968 pOutput->pBuffers[0].cbBuffer = 0;
971 TRACE("Getting negotiated flags\n");
972 lstrcpynA(buffer, "GF", max_len - 1);
973 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
978 TRACE("No flags negotiated, or helper does not support GF command\n");
982 TRACE("Negotiated %s\n", debugstr_a(buffer));
983 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
984 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
987 TRACE("Getting session key\n");
988 lstrcpynA(buffer, "GK", max_len - 1);
989 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
993 TRACE("Helper does not support GK command\n");
996 if(strncmp(buffer, "BH ", 3) == 0)
998 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
999 helper->valid_session_key = FALSE;
1000 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1001 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1002 memset(helper->session_key, 0 , 16);
1004 else if(strncmp(buffer, "GK ", 3) == 0)
1006 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1007 &bin_len)) != SEC_E_OK)
1009 TRACE("Failed to decode session key\n");
1011 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1012 helper->valid_session_key = TRUE;
1013 if(!helper->session_key)
1014 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1015 if(!helper->session_key)
1017 TRACE("Failed to allocate memory for session key\n");
1018 ret = SEC_E_INTERNAL_ERROR;
1021 memcpy(helper->session_key, bin, 16);
1024 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1025 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1026 helper->crypt.ntlm.seq_num = 0l;
1029 phNewContext->dwUpper = ctxt_attr;
1030 phNewContext->dwLower = (ULONG_PTR)helper;
1033 HeapFree(GetProcessHeap(), 0, want_flags);
1034 HeapFree(GetProcessHeap(), 0, buffer);
1035 HeapFree(GetProcessHeap(), 0, bin);
1039 /***********************************************************************
1042 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1043 PSecBufferDesc pToken)
1045 /* We never need to call CompleteAuthToken anyway */
1046 TRACE("%p %p\n", phContext, pToken);
1048 return SEC_E_INVALID_HANDLE;
1053 /***********************************************************************
1054 * DeleteSecurityContext
1056 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1058 SECURITY_STATUS ret;
1060 TRACE("%p\n", phContext);
1063 phContext->dwUpper = 0;
1064 phContext->dwLower = 0;
1069 ret = SEC_E_INVALID_HANDLE;
1074 /***********************************************************************
1075 * QueryContextAttributesW
1077 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1078 unsigned long ulAttribute, void *pBuffer)
1080 TRACE("%p %ld %p\n", phContext, ulAttribute, pBuffer);
1082 return SEC_E_INVALID_HANDLE;
1086 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1087 _x(SECPKG_ATTR_ACCESS_TOKEN);
1088 _x(SECPKG_ATTR_AUTHORITY);
1089 _x(SECPKG_ATTR_DCE_INFO);
1090 case SECPKG_ATTR_FLAGS:
1092 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1093 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1096 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1097 spcf->Flags |= ISC_RET_INTEGRITY;
1098 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1099 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1102 _x(SECPKG_ATTR_KEY_INFO);
1103 _x(SECPKG_ATTR_LIFESPAN);
1104 _x(SECPKG_ATTR_NAMES);
1105 _x(SECPKG_ATTR_NATIVE_NAMES);
1106 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1107 _x(SECPKG_ATTR_PACKAGE_INFO);
1108 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1109 _x(SECPKG_ATTR_SESSION_KEY);
1110 case SECPKG_ATTR_SIZES:
1112 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1113 spcs->cbMaxToken = NTLM_MAX_BUF;
1114 spcs->cbMaxSignature = 16;
1115 spcs->cbBlockSize = 0;
1116 spcs->cbSecurityTrailer = 16;
1119 _x(SECPKG_ATTR_STREAM_SIZES);
1120 _x(SECPKG_ATTR_TARGET_INFORMATION);
1123 TRACE("Unknown value %ld passed for ulAttribute\n", ulAttribute);
1126 return SEC_E_UNSUPPORTED_FUNCTION;
1129 /***********************************************************************
1130 * QueryContextAttributesA
1132 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1133 unsigned long ulAttribute, void *pBuffer)
1135 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1138 /***********************************************************************
1139 * ImpersonateSecurityContext
1141 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1143 SECURITY_STATUS ret;
1145 TRACE("%p\n", phContext);
1148 ret = SEC_E_UNSUPPORTED_FUNCTION;
1152 ret = SEC_E_INVALID_HANDLE;
1157 /***********************************************************************
1158 * RevertSecurityContext
1160 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1162 SECURITY_STATUS ret;
1164 TRACE("%p\n", phContext);
1167 ret = SEC_E_UNSUPPORTED_FUNCTION;
1171 ret = SEC_E_INVALID_HANDLE;
1176 /***********************************************************************
1179 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1180 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1183 ULONG sign_version = 1;
1185 TRACE("%p %ld %p %ld\n", phContext, fQOP, pMessage, MessageSeqNo);
1187 return SEC_E_INVALID_HANDLE;
1190 FIXME("Ignoring fQOP 0x%08lx", fQOP);
1193 FIXME("Ignoring MessageSeqNo");
1195 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2 ||
1196 pMessage->pBuffers[0].BufferType != SECBUFFER_TOKEN ||
1197 !pMessage->pBuffers[0].pvBuffer)
1198 return SEC_E_INVALID_TOKEN;
1200 if(pMessage->pBuffers[0].cbBuffer < 16)
1201 return SEC_E_BUFFER_TOO_SMALL;
1203 helper = (PNegoHelper)phContext->dwLower;
1204 TRACE("Negotiated flags are: 0x%08lx\n", helper->neg_flags);
1205 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
1207 FIXME("Can't handle NTLMv2 signing yet. Aborting.\n");
1208 return SEC_E_UNSUPPORTED_FUNCTION;
1210 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1212 PBYTE sig = pMessage->pBuffers[0].pvBuffer;
1213 ULONG crc = ComputeCrc32(pMessage->pBuffers[1].pvBuffer,
1214 pMessage->pBuffers[1].cbBuffer);
1216 sig[ 0] = (sign_version >> 0) & 0xff;
1217 sig[ 1] = (sign_version >> 8) & 0xff;
1218 sig[ 2] = (sign_version >> 16) & 0xff;
1219 sig[ 3] = (sign_version >> 24) & 0xff;
1220 memset(sig+4, 0, 4);
1221 sig[ 8] = (crc >> 0) & 0xff;
1222 sig[ 9] = (crc >> 8) & 0xff;
1223 sig[10] = (crc >> 16) & 0xff;
1224 sig[11] = (crc >> 24) & 0xff;
1225 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1226 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1227 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1228 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1230 ++(helper->crypt.ntlm.seq_num);
1232 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1236 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1238 FIXME("Can't handle encrypted session key yet. Aborting.\n");
1239 return SEC_E_UNSUPPORTED_FUNCTION;
1242 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)
1244 TRACE("Generating dummy signature\n");
1245 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1246 memset(pMessage->pBuffers[0].pvBuffer, 0, 16);
1247 memset(pMessage->pBuffers[0].pvBuffer, 0x01, 1);
1248 pMessage->pBuffers[0].cbBuffer = 16;
1252 return SEC_E_UNSUPPORTED_FUNCTION;
1255 /***********************************************************************
1258 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1259 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1264 TRACE("%p %p %ld %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1266 return SEC_E_INVALID_HANDLE;
1268 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2 ||
1269 pMessage->pBuffers[0].BufferType != SECBUFFER_TOKEN ||
1270 !pMessage->pBuffers[0].pvBuffer)
1271 return SEC_E_INVALID_TOKEN;
1273 if(pMessage->pBuffers[0].cbBuffer < 16)
1274 return SEC_E_BUFFER_TOO_SMALL;
1277 FIXME("Ignoring MessageSeqNo\n");
1279 helper = (PNegoHelper)phContext->dwLower;
1280 TRACE("Negotiated flags: 0x%08lx\n", helper->neg_flags);
1282 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
1284 FIXME("Can't handle NTLMv2 signing yet. Aborting.\n");
1285 return SEC_E_UNSUPPORTED_FUNCTION;
1288 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1290 SecBufferDesc local_desc;
1291 SecBuffer local_buff[2];
1294 local_desc.ulVersion = SECBUFFER_VERSION;
1295 local_desc.cBuffers = 2;
1296 local_desc.pBuffers = local_buff;
1297 local_buff[0].BufferType = SECBUFFER_TOKEN;
1298 local_buff[0].cbBuffer = 16;
1299 local_buff[0].pvBuffer = local_sig;
1300 local_buff[1].BufferType = SECBUFFER_DATA;
1301 local_buff[1].cbBuffer = pMessage->pBuffers[1].cbBuffer;
1302 local_buff[1].pvBuffer = pMessage->pBuffers[1].pvBuffer;
1304 ntlm_MakeSignature(phContext, fQOP, &local_desc, MessageSeqNo);
1306 if(memcmp(((PBYTE)local_buff[0].pvBuffer) + 8, ((PBYTE)pMessage->pBuffers[0].pvBuffer) + 8, 8))
1307 return SEC_E_MESSAGE_ALTERED;
1312 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1314 FIXME("Can't handle encrypted session keys yet. Aborting.\n");
1315 return SEC_E_UNSUPPORTED_FUNCTION;
1318 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)
1320 const BYTE dummy_sig[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1321 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1322 TRACE("Assuming dummy signature.\n");
1323 if(memcmp(pMessage->pBuffers[0].pvBuffer, dummy_sig, sizeof(dummy_sig)) != 0)
1325 TRACE("Failed to verify the packet signature. Not a dummy signature?\n");
1326 return SEC_E_MESSAGE_ALTERED;
1335 return SEC_E_UNSUPPORTED_FUNCTION;
1338 /***********************************************************************
1339 * FreeCredentialsHandle
1341 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1342 PCredHandle phCredential)
1344 SECURITY_STATUS ret;
1347 PNegoHelper helper = (PNegoHelper) phCredential->dwLower;
1348 phCredential->dwUpper = 0;
1349 phCredential->dwLower = 0;
1350 HeapFree(GetProcessHeap(), 0, helper->session_key);
1351 cleanup_helper(helper);
1360 /***********************************************************************
1363 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1364 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1366 TRACE("%p %ld %p %ld stub\n", phContext, fQOP, pMessage, MessageSeqNo);
1369 return SEC_E_INVALID_HANDLE;
1371 return SEC_E_UNSUPPORTED_FUNCTION;
1374 /***********************************************************************
1377 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1378 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1380 TRACE("%p %p %ld %p stub\n", phContext, pMessage, MessageSeqNo, pfQOP);
1383 return SEC_E_INVALID_HANDLE;
1385 return SEC_E_UNSUPPORTED_FUNCTION;
1388 static SecurityFunctionTableA ntlmTableA = {
1390 NULL, /* EnumerateSecurityPackagesA */
1391 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1392 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1393 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1394 NULL, /* Reserved2 */
1395 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1396 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1397 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1398 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1399 NULL, /* ApplyControlToken */
1400 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1401 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1402 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1403 ntlm_MakeSignature, /* MakeSignature */
1404 ntlm_VerifySignature, /* VerifySignature */
1405 FreeContextBuffer, /* FreeContextBuffer */
1406 NULL, /* QuerySecurityPackageInfoA */
1407 NULL, /* Reserved3 */
1408 NULL, /* Reserved4 */
1409 NULL, /* ExportSecurityContext */
1410 NULL, /* ImportSecurityContextA */
1411 NULL, /* AddCredentialsA */
1412 NULL, /* Reserved8 */
1413 NULL, /* QuerySecurityContextToken */
1414 ntlm_EncryptMessage, /* EncryptMessage */
1415 ntlm_DecryptMessage, /* DecryptMessage */
1416 NULL, /* SetContextAttributesA */
1419 static SecurityFunctionTableW ntlmTableW = {
1421 NULL, /* EnumerateSecurityPackagesW */
1422 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1423 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1424 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1425 NULL, /* Reserved2 */
1426 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1427 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1428 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1429 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1430 NULL, /* ApplyControlToken */
1431 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1432 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1433 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1434 ntlm_MakeSignature, /* MakeSignature */
1435 ntlm_VerifySignature, /* VerifySignature */
1436 FreeContextBuffer, /* FreeContextBuffer */
1437 NULL, /* QuerySecurityPackageInfoW */
1438 NULL, /* Reserved3 */
1439 NULL, /* Reserved4 */
1440 NULL, /* ExportSecurityContext */
1441 NULL, /* ImportSecurityContextW */
1442 NULL, /* AddCredentialsW */
1443 NULL, /* Reserved8 */
1444 NULL, /* QuerySecurityContextToken */
1445 ntlm_EncryptMessage, /* EncryptMessage */
1446 ntlm_DecryptMessage, /* DecryptMessage */
1447 NULL, /* SetContextAttributesW */
1450 #define NTLM_COMMENT \
1451 { 'N', 'T', 'L', 'M', ' ', \
1452 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1453 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1455 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1456 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1458 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1460 static char ntlm_name_A[] = NTLM_NAME;
1461 static WCHAR ntlm_name_W[] = NTLM_NAME;
1463 /* According to Windows, NTLM has the following capabilities. */
1465 SECPKG_FLAG_INTEGRITY | \
1466 SECPKG_FLAG_PRIVACY | \
1467 SECPKG_FLAG_TOKEN_ONLY | \
1468 SECPKG_FLAG_CONNECTION | \
1469 SECPKG_FLAG_MULTI_REQUIRED | \
1470 SECPKG_FLAG_IMPERSONATION | \
1471 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1472 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1474 static const SecPkgInfoW infoW = {
1483 static const SecPkgInfoA infoA = {
1492 void SECUR32_initNTLMSP(void)
1494 SECURITY_STATUS ret;
1496 static CHAR ntlm_auth[] = "ntlm_auth",
1497 version[] = "--version";
1499 SEC_CHAR *args[] = {
1504 if((ret = fork_helper(&helper, "ntlm_auth", args)) != SEC_E_OK)
1506 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1507 helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1508 helper->version = -1;
1511 check_version(helper);
1513 if(helper->version > 2)
1515 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1516 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1518 cleanup_helper(helper);