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.
31 #include "secur32_priv.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ntlm
);
38 #define NTLM_MAX_BUF 1904
39 #define MIN_NTLM_AUTH_MAJOR_VERSION 3
40 #define MIN_NTLM_AUTH_MINOR_VERSION 0
41 #define MIN_NTLM_AUTH_MICRO_VERSION 25
43 static CHAR ntlm_auth
[] = "ntlm_auth";
45 typedef struct _NtlmCredentials
49 /* these are all in the Unix codepage */
52 char *password
; /* not nul-terminated */
54 } NtlmCredentials
, *PNtlmCredentials
;
56 /***********************************************************************
57 * QueryCredentialsAttributesA
59 static SECURITY_STATUS SEC_ENTRY
ntlm_QueryCredentialsAttributesA(
60 PCredHandle phCredential
, ULONG ulAttribute
, PVOID pBuffer
)
64 TRACE("(%p, %d, %p)\n", phCredential
, ulAttribute
, pBuffer
);
66 if(ulAttribute
== SECPKG_ATTR_NAMES
)
68 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
69 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
72 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
77 /***********************************************************************
78 * QueryCredentialsAttributesW
80 static SECURITY_STATUS SEC_ENTRY
ntlm_QueryCredentialsAttributesW(
81 PCredHandle phCredential
, ULONG ulAttribute
, PVOID pBuffer
)
85 TRACE("(%p, %d, %p)\n", phCredential
, ulAttribute
, pBuffer
);
87 if(ulAttribute
== SECPKG_ATTR_NAMES
)
89 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
90 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
93 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
98 static char *ntlm_GetUsernameArg(LPCWSTR userW
, INT userW_length
)
100 static const char username_arg
[] = "--username=";
104 unixcp_size
= WideCharToMultiByte(CP_UNIXCP
, WC_NO_BEST_FIT_CHARS
,
105 userW
, userW_length
, NULL
, 0, NULL
, NULL
) + sizeof(username_arg
);
106 user
= HeapAlloc(GetProcessHeap(), 0, unixcp_size
);
107 if (!user
) return NULL
;
108 memcpy(user
, username_arg
, sizeof(username_arg
) - 1);
109 WideCharToMultiByte(CP_UNIXCP
, WC_NO_BEST_FIT_CHARS
, userW
, userW_length
,
110 user
+ sizeof(username_arg
) - 1,
111 unixcp_size
- sizeof(username_arg
) + 1, NULL
, NULL
);
112 user
[unixcp_size
- 1] = '\0';
116 static char *ntlm_GetDomainArg(LPCWSTR domainW
, INT domainW_length
)
118 static const char domain_arg
[] = "--domain=";
122 unixcp_size
= WideCharToMultiByte(CP_UNIXCP
, WC_NO_BEST_FIT_CHARS
,
123 domainW
, domainW_length
, NULL
, 0, NULL
, NULL
) + sizeof(domain_arg
);
124 domain
= HeapAlloc(GetProcessHeap(), 0, unixcp_size
);
125 if (!domain
) return NULL
;
126 memcpy(domain
, domain_arg
, sizeof(domain_arg
) - 1);
127 WideCharToMultiByte(CP_UNIXCP
, WC_NO_BEST_FIT_CHARS
, domainW
,
128 domainW_length
, domain
+ sizeof(domain_arg
) - 1,
129 unixcp_size
- sizeof(domain
) + 1, NULL
, NULL
);
130 domain
[unixcp_size
- 1] = '\0';
134 /***********************************************************************
135 * AcquireCredentialsHandleW
137 static SECURITY_STATUS SEC_ENTRY
ntlm_AcquireCredentialsHandleW(
138 SEC_WCHAR
*pszPrincipal
, SEC_WCHAR
*pszPackage
, ULONG fCredentialUse
,
139 PLUID pLogonID
, PVOID pAuthData
, SEC_GET_KEY_FN pGetKeyFn
,
140 PVOID pGetKeyArgument
, PCredHandle phCredential
, PTimeStamp ptsExpiry
)
143 PNtlmCredentials ntlm_cred
= NULL
;
144 SEC_WCHAR
*username
= NULL
, *domain
= NULL
;
146 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
147 debugstr_w(pszPrincipal
), debugstr_w(pszPackage
), fCredentialUse
,
148 pLogonID
, pAuthData
, pGetKeyFn
, pGetKeyArgument
, phCredential
, ptsExpiry
);
150 switch(fCredentialUse
)
152 case SECPKG_CRED_INBOUND
:
153 ntlm_cred
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred
));
155 ret
= SEC_E_INSUFFICIENT_MEMORY
;
158 ntlm_cred
->mode
= NTLM_SERVER
;
159 ntlm_cred
->username_arg
= NULL
;
160 ntlm_cred
->domain_arg
= NULL
;
161 ntlm_cred
->password
= NULL
;
162 ntlm_cred
->pwlen
= 0;
163 phCredential
->dwUpper
= fCredentialUse
;
164 phCredential
->dwLower
= (ULONG_PTR
)ntlm_cred
;
168 case SECPKG_CRED_OUTBOUND
:
170 ntlm_cred
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred
));
173 ret
= SEC_E_INSUFFICIENT_MEMORY
;
176 ntlm_cred
->mode
= NTLM_CLIENT
;
177 ntlm_cred
->username_arg
= NULL
;
178 ntlm_cred
->domain_arg
= NULL
;
179 ntlm_cred
->password
= NULL
;
180 ntlm_cred
->pwlen
= 0;
182 if(pAuthData
!= NULL
)
184 PSEC_WINNT_AUTH_IDENTITY_W auth_data
= pAuthData
;
186 TRACE("Username is %s\n", debugstr_wn(auth_data
->User
, auth_data
->UserLength
));
187 TRACE("Domain name is %s\n", debugstr_wn(auth_data
->Domain
, auth_data
->DomainLength
));
189 ntlm_cred
->username_arg
= ntlm_GetUsernameArg(auth_data
->User
, auth_data
->UserLength
);
190 ntlm_cred
->domain_arg
= ntlm_GetDomainArg(auth_data
->Domain
, auth_data
->DomainLength
);
192 if(auth_data
->PasswordLength
!= 0)
194 ntlm_cred
->pwlen
= WideCharToMultiByte(CP_UNIXCP
,
195 WC_NO_BEST_FIT_CHARS
, auth_data
->Password
,
196 auth_data
->PasswordLength
, NULL
, 0, NULL
,
199 ntlm_cred
->password
= HeapAlloc(GetProcessHeap(), 0,
202 WideCharToMultiByte(CP_UNIXCP
, WC_NO_BEST_FIT_CHARS
,
203 auth_data
->Password
, auth_data
->PasswordLength
,
204 ntlm_cred
->password
, ntlm_cred
->pwlen
, NULL
, NULL
);
208 phCredential
->dwUpper
= fCredentialUse
;
209 phCredential
->dwLower
= (ULONG_PTR
)ntlm_cred
;
210 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
211 phCredential
->dwUpper
, phCredential
->dwLower
);
215 case SECPKG_CRED_BOTH
:
216 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
217 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
222 ret
= SEC_E_UNKNOWN_CREDENTIALS
;
225 HeapFree(GetProcessHeap(), 0, username
);
226 HeapFree(GetProcessHeap(), 0, domain
);
231 /***********************************************************************
232 * AcquireCredentialsHandleA
234 static SECURITY_STATUS SEC_ENTRY
ntlm_AcquireCredentialsHandleA(
235 SEC_CHAR
*pszPrincipal
, SEC_CHAR
*pszPackage
, ULONG fCredentialUse
,
236 PLUID pLogonID
, PVOID pAuthData
, SEC_GET_KEY_FN pGetKeyFn
,
237 PVOID pGetKeyArgument
, PCredHandle phCredential
, PTimeStamp ptsExpiry
)
240 int user_sizeW
, domain_sizeW
, passwd_sizeW
;
242 SEC_WCHAR
*user
= NULL
, *domain
= NULL
, *passwd
= NULL
, *package
= NULL
;
244 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW
= NULL
;
245 PSEC_WINNT_AUTH_IDENTITY_A identity
= NULL
;
247 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
248 debugstr_a(pszPrincipal
), debugstr_a(pszPackage
), fCredentialUse
,
249 pLogonID
, pAuthData
, pGetKeyFn
, pGetKeyArgument
, phCredential
, ptsExpiry
);
251 if(pszPackage
!= NULL
)
253 int package_sizeW
= MultiByteToWideChar(CP_ACP
, 0, pszPackage
, -1,
256 package
= HeapAlloc(GetProcessHeap(), 0, package_sizeW
*
258 MultiByteToWideChar(CP_ACP
, 0, pszPackage
, -1, package
, package_sizeW
);
262 if(pAuthData
!= NULL
)
264 identity
= pAuthData
;
266 if(identity
->Flags
== SEC_WINNT_AUTH_IDENTITY_ANSI
)
268 pAuthDataW
= HeapAlloc(GetProcessHeap(), 0,
269 sizeof(SEC_WINNT_AUTH_IDENTITY_W
));
271 if(identity
->UserLength
!= 0)
273 user_sizeW
= MultiByteToWideChar(CP_ACP
, 0,
274 (LPCSTR
)identity
->User
, identity
->UserLength
, NULL
, 0);
275 user
= HeapAlloc(GetProcessHeap(), 0, user_sizeW
*
277 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)identity
->User
,
278 identity
->UserLength
, user
, user_sizeW
);
285 if(identity
->DomainLength
!= 0)
287 domain_sizeW
= MultiByteToWideChar(CP_ACP
, 0,
288 (LPCSTR
)identity
->Domain
, identity
->DomainLength
, NULL
, 0);
289 domain
= HeapAlloc(GetProcessHeap(), 0, domain_sizeW
290 * sizeof(SEC_WCHAR
));
291 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)identity
->Domain
,
292 identity
->DomainLength
, domain
, domain_sizeW
);
299 if(identity
->PasswordLength
!= 0)
301 passwd_sizeW
= MultiByteToWideChar(CP_ACP
, 0,
302 (LPCSTR
)identity
->Password
, identity
->PasswordLength
,
304 passwd
= HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
305 * sizeof(SEC_WCHAR
));
306 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)identity
->Password
,
307 identity
->PasswordLength
, passwd
, passwd_sizeW
);
314 pAuthDataW
->Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
315 pAuthDataW
->User
= user
;
316 pAuthDataW
->UserLength
= user_sizeW
;
317 pAuthDataW
->Domain
= domain
;
318 pAuthDataW
->DomainLength
= domain_sizeW
;
319 pAuthDataW
->Password
= passwd
;
320 pAuthDataW
->PasswordLength
= passwd_sizeW
;
324 pAuthDataW
= (PSEC_WINNT_AUTH_IDENTITY_W
)identity
;
328 ret
= ntlm_AcquireCredentialsHandleW(NULL
, package
, fCredentialUse
,
329 pLogonID
, pAuthDataW
, pGetKeyFn
, pGetKeyArgument
, phCredential
,
332 HeapFree(GetProcessHeap(), 0, package
);
333 HeapFree(GetProcessHeap(), 0, user
);
334 HeapFree(GetProcessHeap(), 0, domain
);
335 HeapFree(GetProcessHeap(), 0, passwd
);
336 if(pAuthDataW
!= (PSEC_WINNT_AUTH_IDENTITY_W
)identity
)
337 HeapFree(GetProcessHeap(), 0, pAuthDataW
);
342 /*************************************************************************
343 * ntlm_GetTokenBufferIndex
344 * Calculates the index of the secbuffer with BufferType == SECBUFFER_TOKEN
345 * Returns index if found or -1 if not found.
347 static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage
)
351 TRACE("%p\n", pMessage
);
353 for( i
= 0; i
< pMessage
->cBuffers
; ++i
)
355 if(pMessage
->pBuffers
[i
].BufferType
== SECBUFFER_TOKEN
)
362 /*************************************************************************
363 * ntlm_GetDataBufferIndex
364 * Calculates the index of the first secbuffer with BufferType == SECBUFFER_DATA
365 * Returns index if found or -1 if not found.
367 static int ntlm_GetDataBufferIndex(PSecBufferDesc pMessage
)
371 TRACE("%p\n", pMessage
);
373 for( i
= 0; i
< pMessage
->cBuffers
; ++i
)
375 if(pMessage
->pBuffers
[i
].BufferType
== SECBUFFER_DATA
)
382 static BOOL
ntlm_GetCachedCredential(const SEC_WCHAR
*pszTargetName
, PCREDENTIALW
*cred
)
392 /* try to get the start of the hostname from service principal name (SPN) */
393 pszHost
= strchrW(pszTargetName
, '/');
396 /* skip slash character */
399 /* find end of host by detecting start of instance port or start of referrer */
400 p
= strchrW(pszHost
, ':');
402 p
= strchrW(pszHost
, '/');
404 p
= pszHost
+ strlenW(pszHost
);
406 else /* otherwise not an SPN, just a host */
408 pszHost
= pszTargetName
;
409 p
= pszHost
+ strlenW(pszHost
);
412 pszHostOnly
= HeapAlloc(GetProcessHeap(), 0, (p
- pszHost
+ 1) * sizeof(WCHAR
));
416 memcpy(pszHostOnly
, pszHost
, (p
- pszHost
) * sizeof(WCHAR
));
417 pszHostOnly
[p
- pszHost
] = '\0';
419 ret
= CredReadW(pszHostOnly
, CRED_TYPE_DOMAIN_PASSWORD
, 0, cred
);
421 HeapFree(GetProcessHeap(), 0, pszHostOnly
);
425 /***********************************************************************
426 * InitializeSecurityContextW
428 static SECURITY_STATUS SEC_ENTRY
ntlm_InitializeSecurityContextW(
429 PCredHandle phCredential
, PCtxtHandle phContext
, SEC_WCHAR
*pszTargetName
,
430 ULONG fContextReq
, ULONG Reserved1
, ULONG TargetDataRep
,
431 PSecBufferDesc pInput
, ULONG Reserved2
, PCtxtHandle phNewContext
,
432 PSecBufferDesc pOutput
, ULONG
*pfContextAttr
, PTimeStamp ptsExpiry
)
435 PNtlmCredentials ntlm_cred
= NULL
;
436 PNegoHelper helper
= NULL
;
438 char* buffer
, *want_flags
= NULL
;
440 int buffer_len
, bin_len
, max_len
= NTLM_MAX_BUF
;
442 SEC_CHAR
*username
= NULL
;
443 SEC_CHAR
*domain
= NULL
;
444 SEC_CHAR
*password
= NULL
;
446 TRACE("%p %p %s 0x%08x %d %d %p %d %p %p %p %p\n", phCredential
, phContext
,
447 debugstr_w(pszTargetName
), fContextReq
, Reserved1
, TargetDataRep
, pInput
,
448 Reserved1
, phNewContext
, pOutput
, pfContextAttr
, ptsExpiry
);
450 /****************************************
451 * When communicating with the client, there can be the
452 * following reply packets:
453 * YR <base64 blob> should be sent to the server
454 * PW should be sent back to helper with
455 * base64 encoded password
456 * AF <base64 blob> client is done, blob should be
457 * sent to server with KK prefixed
458 * GF <string list> A string list of negotiated flags
459 * GK <base64 blob> base64 encoded session key
460 * BH <char reason> something broke
462 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
464 if(TargetDataRep
== SECURITY_NETWORK_DREP
){
465 TRACE("Setting SECURITY_NETWORK_DREP\n");
468 buffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF
);
469 bin
= HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE
) * NTLM_MAX_BUF
);
471 if((phContext
== NULL
) && (pInput
== NULL
))
473 static char helper_protocol
[] = "--helper-protocol=ntlmssp-client-1";
474 static CHAR credentials_argv
[] = "--use-cached-creds";
475 SEC_CHAR
*client_argv
[5];
478 TRACE("First time in ISC()\n");
482 ret
= SEC_E_INVALID_HANDLE
;
486 /* As the server side of sspi never calls this, make sure that
487 * the handler is a client handler.
489 ntlm_cred
= (PNtlmCredentials
)phCredential
->dwLower
;
490 if(ntlm_cred
->mode
!= NTLM_CLIENT
)
492 TRACE("Cred mode = %d\n", ntlm_cred
->mode
);
493 ret
= SEC_E_INVALID_HANDLE
;
497 client_argv
[0] = ntlm_auth
;
498 client_argv
[1] = helper_protocol
;
499 if (!ntlm_cred
->username_arg
&& !ntlm_cred
->domain_arg
)
501 LPWKSTA_USER_INFO_1 ui
= NULL
;
502 NET_API_STATUS status
;
505 if (ntlm_GetCachedCredential(pszTargetName
, &cred
))
508 p
= strchrW(cred
->UserName
, '\\');
511 domain
= ntlm_GetDomainArg(cred
->UserName
, p
- cred
->UserName
);
516 domain
= ntlm_GetDomainArg(NULL
, 0);
520 username
= ntlm_GetUsernameArg(p
, -1);
522 if(cred
->CredentialBlobSize
!= 0)
524 pwlen
= WideCharToMultiByte(CP_UNIXCP
,
525 WC_NO_BEST_FIT_CHARS
, (LPWSTR
)cred
->CredentialBlob
,
526 cred
->CredentialBlobSize
/ sizeof(WCHAR
), NULL
, 0,
529 password
= HeapAlloc(GetProcessHeap(), 0, pwlen
);
531 WideCharToMultiByte(CP_UNIXCP
, WC_NO_BEST_FIT_CHARS
,
532 (LPWSTR
)cred
->CredentialBlob
,
533 cred
->CredentialBlobSize
/ sizeof(WCHAR
),
534 password
, pwlen
, NULL
, NULL
);
539 client_argv
[2] = username
;
540 client_argv
[3] = domain
;
541 client_argv
[4] = NULL
;
545 status
= NetWkstaUserGetInfo(NULL
, 1, (LPBYTE
*)&ui
);
546 if (status
!= NERR_Success
|| ui
== NULL
)
548 ret
= SEC_E_NO_CREDENTIALS
;
551 username
= ntlm_GetUsernameArg(ui
->wkui1_username
, -1);
552 NetApiBufferFree(ui
);
554 TRACE("using cached credentials\n");
556 client_argv
[2] = username
;
557 client_argv
[3] = credentials_argv
;
558 client_argv
[4] = NULL
;
563 client_argv
[2] = ntlm_cred
->username_arg
;
564 client_argv
[3] = ntlm_cred
->domain_arg
;
565 client_argv
[4] = NULL
;
568 if((ret
= fork_helper(&helper
, ntlm_auth
, client_argv
)) != SEC_E_OK
)
571 helper
->mode
= NTLM_CLIENT
;
572 helper
->session_key
= HeapAlloc(GetProcessHeap(), 0, 16);
573 if (!helper
->session_key
)
575 cleanup_helper(helper
);
576 ret
= SEC_E_INSUFFICIENT_MEMORY
;
580 /* Generate the dummy session key = MD4(MD4(password))*/
581 if(password
|| ntlm_cred
->password
)
583 SEC_WCHAR
*unicode_password
;
586 TRACE("Converting password to unicode.\n");
587 passwd_lenW
= MultiByteToWideChar(CP_ACP
, 0,
588 password
? password
: ntlm_cred
->password
,
589 password
? pwlen
: ntlm_cred
->pwlen
,
591 unicode_password
= HeapAlloc(GetProcessHeap(), 0,
592 passwd_lenW
* sizeof(SEC_WCHAR
));
593 MultiByteToWideChar(CP_ACP
, 0, password
? password
: ntlm_cred
->password
,
594 password
? pwlen
: ntlm_cred
->pwlen
, unicode_password
, passwd_lenW
);
596 SECUR32_CreateNTLM1SessionKey((PBYTE
)unicode_password
,
597 passwd_lenW
* sizeof(SEC_WCHAR
), helper
->session_key
);
599 HeapFree(GetProcessHeap(), 0, unicode_password
);
602 memset(helper
->session_key
, 0, 16);
604 /* Allocate space for a maximal string of
605 * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
606 * NTLMSSP_FEATURE_SESSION_KEY"
608 want_flags
= HeapAlloc(GetProcessHeap(), 0, 73);
609 if(want_flags
== NULL
)
611 cleanup_helper(helper
);
612 ret
= SEC_E_INSUFFICIENT_MEMORY
;
615 lstrcpyA(want_flags
, "SF");
616 if(fContextReq
& ISC_REQ_CONFIDENTIALITY
)
618 if(strstr(want_flags
, "NTLMSSP_FEATURE_SEAL") == NULL
)
619 lstrcatA(want_flags
, " NTLMSSP_FEATURE_SEAL");
621 if(fContextReq
& ISC_REQ_CONNECTION
)
622 ctxt_attr
|= ISC_RET_CONNECTION
;
623 if(fContextReq
& ISC_REQ_EXTENDED_ERROR
)
624 ctxt_attr
|= ISC_RET_EXTENDED_ERROR
;
625 if(fContextReq
& ISC_REQ_INTEGRITY
)
627 if(strstr(want_flags
, "NTLMSSP_FEATURE_SIGN") == NULL
)
628 lstrcatA(want_flags
, " NTLMSSP_FEATURE_SIGN");
630 if(fContextReq
& ISC_REQ_MUTUAL_AUTH
)
631 ctxt_attr
|= ISC_RET_MUTUAL_AUTH
;
632 if(fContextReq
& ISC_REQ_REPLAY_DETECT
)
634 if(strstr(want_flags
, "NTLMSSP_FEATURE_SIGN") == NULL
)
635 lstrcatA(want_flags
, " NTLMSSP_FEATURE_SIGN");
637 if(fContextReq
& ISC_REQ_SEQUENCE_DETECT
)
639 if(strstr(want_flags
, "NTLMSSP_FEATURE_SIGN") == NULL
)
640 lstrcatA(want_flags
, " NTLMSSP_FEATURE_SIGN");
642 if(fContextReq
& ISC_REQ_STREAM
)
643 FIXME("ISC_REQ_STREAM\n");
644 if(fContextReq
& ISC_REQ_USE_DCE_STYLE
)
645 ctxt_attr
|= ISC_RET_USED_DCE_STYLE
;
646 if(fContextReq
& ISC_REQ_DELEGATE
)
647 ctxt_attr
|= ISC_RET_DELEGATE
;
649 /* If no password is given, try to use cached credentials. Fall back to an empty
650 * password if this failed. */
651 if(!password
&& !ntlm_cred
->password
)
653 lstrcpynA(buffer
, "OK", max_len
-1);
654 if((ret
= run_helper(helper
, buffer
, max_len
, &buffer_len
)) != SEC_E_OK
)
656 cleanup_helper(helper
);
659 /* If the helper replied with "PW", using cached credentials failed */
660 if(!strncmp(buffer
, "PW", 2))
662 TRACE("Using cached credentials failed.\n");
663 lstrcpynA(buffer
, "PW AA==", max_len
-1);
665 else /* Just do a noop on the next run */
666 lstrcpynA(buffer
, "OK", max_len
-1);
670 lstrcpynA(buffer
, "PW ", max_len
-1);
671 if((ret
= encodeBase64(password
? (unsigned char *)password
: (unsigned char *)ntlm_cred
->password
,
672 password
? pwlen
: ntlm_cred
->pwlen
, buffer
+3,
673 max_len
-3, &buffer_len
)) != SEC_E_OK
)
675 cleanup_helper(helper
);
681 TRACE("Sending to helper: %s\n", debugstr_a(buffer
));
682 if((ret
= run_helper(helper
, buffer
, max_len
, &buffer_len
)) != SEC_E_OK
)
684 cleanup_helper(helper
);
688 TRACE("Helper returned %s\n", debugstr_a(buffer
));
690 if(lstrlenA(want_flags
) > 2)
692 TRACE("Want flags are %s\n", debugstr_a(want_flags
));
693 lstrcpynA(buffer
, want_flags
, max_len
-1);
694 if((ret
= run_helper(helper
, buffer
, max_len
, &buffer_len
))
697 if(!strncmp(buffer
, "BH", 2))
698 ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
701 lstrcpynA(buffer
, "YR", max_len
-1);
703 if((ret
= run_helper(helper
, buffer
, max_len
, &buffer_len
)) != SEC_E_OK
)
705 cleanup_helper(helper
);
709 TRACE("%s\n", buffer
);
711 if(strncmp(buffer
, "YR ", 3) != 0)
713 /* Something borked */
714 TRACE("Helper returned %c%c\n", buffer
[0], buffer
[1]);
715 ret
= SEC_E_INTERNAL_ERROR
;
716 cleanup_helper(helper
);
719 if((ret
= decodeBase64(buffer
+3, buffer_len
-3, bin
,
720 max_len
-1, &bin_len
)) != SEC_E_OK
)
722 cleanup_helper(helper
);
726 /* put the decoded client blob into the out buffer */
728 phNewContext
->dwUpper
= ctxt_attr
;
729 phNewContext
->dwLower
= (ULONG_PTR
)helper
;
731 ret
= SEC_I_CONTINUE_NEEDED
;
737 /* handle second call here */
738 /* encode server data to base64 */
739 if (!pInput
|| ((input_token_idx
= ntlm_GetTokenBufferIndex(pInput
)) == -1))
741 ret
= SEC_E_INVALID_TOKEN
;
747 ret
= SEC_E_INVALID_HANDLE
;
751 /* As the server side of sspi never calls this, make sure that
752 * the handler is a client handler.
754 helper
= (PNegoHelper
)phContext
->dwLower
;
755 if(helper
->mode
!= NTLM_CLIENT
)
757 TRACE("Helper mode = %d\n", helper
->mode
);
758 ret
= SEC_E_INVALID_HANDLE
;
762 if (!pInput
->pBuffers
[input_token_idx
].pvBuffer
)
764 ret
= SEC_E_INTERNAL_ERROR
;
768 if(pInput
->pBuffers
[input_token_idx
].cbBuffer
> max_len
)
770 TRACE("pInput->pBuffers[%d].cbBuffer is: %d\n",
772 pInput
->pBuffers
[input_token_idx
].cbBuffer
);
773 ret
= SEC_E_INVALID_TOKEN
;
777 bin_len
= pInput
->pBuffers
[input_token_idx
].cbBuffer
;
779 memcpy(bin
, pInput
->pBuffers
[input_token_idx
].pvBuffer
, bin_len
);
781 lstrcpynA(buffer
, "TT ", max_len
-1);
783 if((ret
= encodeBase64(bin
, bin_len
, buffer
+3,
784 max_len
-3, &buffer_len
)) != SEC_E_OK
)
787 TRACE("Server sent: %s\n", debugstr_a(buffer
));
789 /* send TT base64 blob to ntlm_auth */
790 if((ret
= run_helper(helper
, buffer
, max_len
, &buffer_len
)) != SEC_E_OK
)
793 TRACE("Helper replied: %s\n", debugstr_a(buffer
));
795 if( (strncmp(buffer
, "KK ", 3) != 0) &&
796 (strncmp(buffer
, "AF ", 3) !=0))
798 TRACE("Helper returned %c%c\n", buffer
[0], buffer
[1]);
799 ret
= SEC_E_INVALID_TOKEN
;
803 /* decode the blob and send it to server */
804 if((ret
= decodeBase64(buffer
+3, buffer_len
-3, bin
, max_len
,
805 &bin_len
)) != SEC_E_OK
)
810 phNewContext
->dwUpper
= ctxt_attr
;
811 phNewContext
->dwLower
= (ULONG_PTR
)helper
;
816 /* put the decoded client blob into the out buffer */
818 if (!pOutput
|| ((token_idx
= ntlm_GetTokenBufferIndex(pOutput
)) == -1))
820 TRACE("no SECBUFFER_TOKEN buffer could be found\n");
821 ret
= SEC_E_BUFFER_TOO_SMALL
;
822 if ((phContext
== NULL
) && (pInput
== NULL
))
824 HeapFree(GetProcessHeap(), 0, helper
->session_key
);
825 cleanup_helper(helper
);
826 phNewContext
->dwUpper
= 0;
827 phNewContext
->dwLower
= 0;
832 if (fContextReq
& ISC_REQ_ALLOCATE_MEMORY
)
834 pOutput
->pBuffers
[token_idx
].pvBuffer
= HeapAlloc(GetProcessHeap(), 0, bin_len
);
835 pOutput
->pBuffers
[token_idx
].cbBuffer
= bin_len
;
837 else if (pOutput
->pBuffers
[token_idx
].cbBuffer
< bin_len
)
839 TRACE("out buffer is NULL or has not enough space\n");
840 ret
= SEC_E_BUFFER_TOO_SMALL
;
841 if ((phContext
== NULL
) && (pInput
== NULL
))
843 HeapFree(GetProcessHeap(), 0, helper
->session_key
);
844 cleanup_helper(helper
);
845 phNewContext
->dwUpper
= 0;
846 phNewContext
->dwLower
= 0;
851 if (!pOutput
->pBuffers
[token_idx
].pvBuffer
)
853 TRACE("out buffer is NULL\n");
854 ret
= SEC_E_INTERNAL_ERROR
;
855 if ((phContext
== NULL
) && (pInput
== NULL
))
857 HeapFree(GetProcessHeap(), 0, helper
->session_key
);
858 cleanup_helper(helper
);
859 phNewContext
->dwUpper
= 0;
860 phNewContext
->dwLower
= 0;
865 pOutput
->pBuffers
[token_idx
].cbBuffer
= bin_len
;
866 memcpy(pOutput
->pBuffers
[token_idx
].pvBuffer
, bin
, bin_len
);
870 TRACE("Getting negotiated flags\n");
871 lstrcpynA(buffer
, "GF", max_len
- 1);
872 if((ret
= run_helper(helper
, buffer
, max_len
, &buffer_len
)) != SEC_E_OK
)
877 TRACE("No flags negotiated.\n");
878 helper
->neg_flags
= 0l;
882 TRACE("Negotiated %s\n", debugstr_a(buffer
));
883 sscanf(buffer
+ 3, "%x", &(helper
->neg_flags
));
884 TRACE("Stored 0x%08x as flags\n", helper
->neg_flags
);
887 TRACE("Getting session key\n");
888 lstrcpynA(buffer
, "GK", max_len
- 1);
889 if((ret
= run_helper(helper
, buffer
, max_len
, &buffer_len
)) != SEC_E_OK
)
892 if(strncmp(buffer
, "BH", 2) == 0)
893 TRACE("No key negotiated.\n");
894 else if(strncmp(buffer
, "GK ", 3) == 0)
896 if((ret
= decodeBase64(buffer
+3, buffer_len
-3, bin
, max_len
,
897 &bin_len
)) != SEC_E_OK
)
899 TRACE("Failed to decode session key\n");
901 TRACE("Session key is %s\n", debugstr_a(buffer
+3));
902 HeapFree(GetProcessHeap(), 0, helper
->session_key
);
903 helper
->session_key
= HeapAlloc(GetProcessHeap(), 0, bin_len
);
904 if(!helper
->session_key
)
906 TRACE("Failed to allocate memory for session key\n");
907 ret
= SEC_E_INTERNAL_ERROR
;
910 memcpy(helper
->session_key
, bin
, bin_len
);
913 helper
->crypt
.ntlm
.a4i
= SECUR32_arc4Alloc();
914 SECUR32_arc4Init(helper
->crypt
.ntlm
.a4i
, helper
->session_key
, 16);
915 helper
->crypt
.ntlm
.seq_num
= 0l;
916 SECUR32_CreateNTLM2SubKeys(helper
);
917 helper
->crypt
.ntlm2
.send_a4i
= SECUR32_arc4Alloc();
918 helper
->crypt
.ntlm2
.recv_a4i
= SECUR32_arc4Alloc();
919 SECUR32_arc4Init(helper
->crypt
.ntlm2
.send_a4i
,
920 helper
->crypt
.ntlm2
.send_seal_key
, 16);
921 SECUR32_arc4Init(helper
->crypt
.ntlm2
.recv_a4i
,
922 helper
->crypt
.ntlm2
.recv_seal_key
, 16);
923 helper
->crypt
.ntlm2
.send_seq_no
= 0l;
924 helper
->crypt
.ntlm2
.recv_seq_no
= 0l;
928 HeapFree(GetProcessHeap(), 0, username
);
929 HeapFree(GetProcessHeap(), 0, domain
);
930 HeapFree(GetProcessHeap(), 0, password
);
931 HeapFree(GetProcessHeap(), 0, want_flags
);
932 HeapFree(GetProcessHeap(), 0, buffer
);
933 HeapFree(GetProcessHeap(), 0, bin
);
937 /***********************************************************************
938 * InitializeSecurityContextA
940 static SECURITY_STATUS SEC_ENTRY
ntlm_InitializeSecurityContextA(
941 PCredHandle phCredential
, PCtxtHandle phContext
, SEC_CHAR
*pszTargetName
,
942 ULONG fContextReq
, ULONG Reserved1
, ULONG TargetDataRep
,
943 PSecBufferDesc pInput
,ULONG Reserved2
, PCtxtHandle phNewContext
,
944 PSecBufferDesc pOutput
, ULONG
*pfContextAttr
, PTimeStamp ptsExpiry
)
947 SEC_WCHAR
*target
= NULL
;
949 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential
, phContext
,
950 debugstr_a(pszTargetName
), fContextReq
, Reserved1
, TargetDataRep
, pInput
,
951 Reserved1
, phNewContext
, pOutput
, pfContextAttr
, ptsExpiry
);
953 if(pszTargetName
!= NULL
)
955 int target_size
= MultiByteToWideChar(CP_ACP
, 0, pszTargetName
,
956 strlen(pszTargetName
)+1, NULL
, 0);
957 target
= HeapAlloc(GetProcessHeap(), 0, target_size
*
959 MultiByteToWideChar(CP_ACP
, 0, pszTargetName
, strlen(pszTargetName
)+1,
960 target
, target_size
);
963 ret
= ntlm_InitializeSecurityContextW(phCredential
, phContext
, target
,
964 fContextReq
, Reserved1
, TargetDataRep
, pInput
, Reserved2
,
965 phNewContext
, pOutput
, pfContextAttr
, ptsExpiry
);
967 HeapFree(GetProcessHeap(), 0, target
);
971 /***********************************************************************
972 * AcceptSecurityContext
974 static SECURITY_STATUS SEC_ENTRY
ntlm_AcceptSecurityContext(
975 PCredHandle phCredential
, PCtxtHandle phContext
, PSecBufferDesc pInput
,
976 ULONG fContextReq
, ULONG TargetDataRep
, PCtxtHandle phNewContext
,
977 PSecBufferDesc pOutput
, ULONG
*pfContextAttr
, PTimeStamp ptsExpiry
)
980 char *buffer
, *want_flags
= NULL
;
982 int buffer_len
, bin_len
, max_len
= NTLM_MAX_BUF
;
985 PNtlmCredentials ntlm_cred
;
987 TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential
, phContext
, pInput
,
988 fContextReq
, TargetDataRep
, phNewContext
, pOutput
, pfContextAttr
,
991 buffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF
);
992 bin
= HeapAlloc(GetProcessHeap(),0, sizeof(BYTE
) * NTLM_MAX_BUF
);
994 if(TargetDataRep
== SECURITY_NETWORK_DREP
){
995 TRACE("Using SECURITY_NETWORK_DREP\n");
998 if(phContext
== NULL
)
1000 static CHAR server_helper_protocol
[] = "--helper-protocol=squid-2.5-ntlmssp";
1001 SEC_CHAR
*server_argv
[] = { ntlm_auth
,
1002 server_helper_protocol
,
1007 ret
= SEC_E_INVALID_HANDLE
;
1011 ntlm_cred
= (PNtlmCredentials
)phCredential
->dwLower
;
1013 if(ntlm_cred
->mode
!= NTLM_SERVER
)
1015 ret
= SEC_E_INVALID_HANDLE
;
1019 /* This is the first call to AcceptSecurityHandle */
1022 ret
= SEC_E_INCOMPLETE_MESSAGE
;
1026 if(pInput
->cBuffers
< 1)
1028 ret
= SEC_E_INCOMPLETE_MESSAGE
;
1032 if(pInput
->pBuffers
[0].cbBuffer
> max_len
)
1034 ret
= SEC_E_INVALID_TOKEN
;
1038 bin_len
= pInput
->pBuffers
[0].cbBuffer
;
1040 if( (ret
= fork_helper(&helper
, ntlm_auth
, server_argv
)) !=
1043 ret
= SEC_E_INTERNAL_ERROR
;
1046 helper
->mode
= NTLM_SERVER
;
1048 /* Handle all the flags */
1049 want_flags
= HeapAlloc(GetProcessHeap(), 0, 73);
1050 if(want_flags
== NULL
)
1052 TRACE("Failed to allocate memory for the want_flags!\n");
1053 ret
= SEC_E_INSUFFICIENT_MEMORY
;
1054 cleanup_helper(helper
);
1057 lstrcpyA(want_flags
, "SF");
1058 if(fContextReq
& ASC_REQ_ALLOCATE_MEMORY
)
1060 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
1062 if(fContextReq
& ASC_REQ_CONFIDENTIALITY
)
1064 lstrcatA(want_flags
, " NTLMSSP_FEATURE_SEAL");
1066 if(fContextReq
& ASC_REQ_CONNECTION
)
1068 /* This is default, so we'll enable it */
1069 lstrcatA(want_flags
, " NTLMSSP_FEATURE_SESSION_KEY");
1070 ctxt_attr
|= ASC_RET_CONNECTION
;
1072 if(fContextReq
& ASC_REQ_EXTENDED_ERROR
)
1074 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
1076 if(fContextReq
& ASC_REQ_INTEGRITY
)
1078 lstrcatA(want_flags
, " NTLMSSP_FEATURE_SIGN");
1080 if(fContextReq
& ASC_REQ_MUTUAL_AUTH
)
1082 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
1084 if(fContextReq
& ASC_REQ_REPLAY_DETECT
)
1086 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
1088 if(fContextReq
& ISC_REQ_SEQUENCE_DETECT
)
1090 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
1092 if(fContextReq
& ISC_REQ_STREAM
)
1094 FIXME("ASC_REQ_STREAM stub\n");
1096 /* Done with the flags */
1098 if(lstrlenA(want_flags
) > 3)
1100 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags
));
1101 lstrcpynA(buffer
, want_flags
, max_len
- 1);
1102 if((ret
= run_helper(helper
, buffer
, max_len
, &buffer_len
)) !=
1105 cleanup_helper(helper
);
1108 if(!strncmp(buffer
, "BH", 2))
1109 TRACE("Helper doesn't understand new command set\n");
1112 /* This is the YR request from the client, encode to base64 */
1114 memcpy(bin
, pInput
->pBuffers
[0].pvBuffer
, bin_len
);
1116 lstrcpynA(buffer
, "YR ", max_len
-1);
1118 if((ret
= encodeBase64(bin
, bin_len
, buffer
+3, max_len
-3,
1119 &buffer_len
)) != SEC_E_OK
)
1121 cleanup_helper(helper
);
1125 TRACE("Client sent: %s\n", debugstr_a(buffer
));
1127 if((ret
= run_helper(helper
, buffer
, max_len
, &buffer_len
)) !=
1130 cleanup_helper(helper
);
1134 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer
));
1135 /* The expected answer is TT <base64 blob> */
1137 if(strncmp(buffer
, "TT ", 3) != 0)
1139 ret
= SEC_E_INTERNAL_ERROR
;
1140 cleanup_helper(helper
);
1144 if((ret
= decodeBase64(buffer
+3, buffer_len
-3, bin
, max_len
,
1145 &bin_len
)) != SEC_E_OK
)
1147 cleanup_helper(helper
);
1151 /* send this to the client */
1154 ret
= SEC_E_INSUFFICIENT_MEMORY
;
1155 cleanup_helper(helper
);
1159 if(pOutput
->cBuffers
< 1)
1161 ret
= SEC_E_INSUFFICIENT_MEMORY
;
1162 cleanup_helper(helper
);
1166 pOutput
->pBuffers
[0].cbBuffer
= bin_len
;
1167 pOutput
->pBuffers
[0].BufferType
= SECBUFFER_DATA
;
1168 memcpy(pOutput
->pBuffers
[0].pvBuffer
, bin
, bin_len
);
1169 ret
= SEC_I_CONTINUE_NEEDED
;
1174 /* we expect a KK request from client */
1177 ret
= SEC_E_INCOMPLETE_MESSAGE
;
1181 if(pInput
->cBuffers
< 1)
1183 ret
= SEC_E_INCOMPLETE_MESSAGE
;
1189 ret
= SEC_E_INVALID_HANDLE
;
1193 helper
= (PNegoHelper
)phContext
->dwLower
;
1195 if(helper
->mode
!= NTLM_SERVER
)
1197 ret
= SEC_E_INVALID_HANDLE
;
1201 if(pInput
->pBuffers
[0].cbBuffer
> max_len
)
1203 ret
= SEC_E_INVALID_TOKEN
;
1207 bin_len
= pInput
->pBuffers
[0].cbBuffer
;
1209 memcpy(bin
, pInput
->pBuffers
[0].pvBuffer
, bin_len
);
1211 lstrcpynA(buffer
, "KK ", max_len
-1);
1213 if((ret
= encodeBase64(bin
, bin_len
, buffer
+3, max_len
-3,
1214 &buffer_len
)) != SEC_E_OK
)
1219 TRACE("Client sent: %s\n", debugstr_a(buffer
));
1221 if((ret
= run_helper(helper
, buffer
, max_len
, &buffer_len
)) !=
1227 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer
));
1229 /* At this point, we get a NA if the user didn't authenticate, but a BH
1230 * if ntlm_auth could not connect to winbindd. Apart from running Wine
1231 * as root, there is no way to fix this for now, so just handle this as
1232 * a failed login. */
1233 if(strncmp(buffer
, "AF ", 3) != 0)
1235 if(strncmp(buffer
, "NA ", 3) == 0)
1237 ret
= SEC_E_LOGON_DENIED
;
1242 size_t ntlm_pipe_err_len
= strlen("BH NT_STATUS_ACCESS_DENIED");
1244 if( (buffer_len
>= ntlm_pipe_err_len
) &&
1245 (strncmp(buffer
, "BH NT_STATUS_ACCESS_DENIED",
1246 ntlm_pipe_err_len
) == 0))
1248 TRACE("Connection to winbindd failed\n");
1249 ret
= SEC_E_LOGON_DENIED
;
1252 ret
= SEC_E_INTERNAL_ERROR
;
1257 pOutput
->pBuffers
[0].cbBuffer
= 0;
1259 TRACE("Getting negotiated flags\n");
1260 lstrcpynA(buffer
, "GF", max_len
- 1);
1261 if((ret
= run_helper(helper
, buffer
, max_len
, &buffer_len
)) != SEC_E_OK
)
1266 TRACE("No flags negotiated, or helper does not support GF command\n");
1270 TRACE("Negotiated %s\n", debugstr_a(buffer
));
1271 sscanf(buffer
+ 3, "%x", &(helper
->neg_flags
));
1272 TRACE("Stored 0x%08x as flags\n", helper
->neg_flags
);
1275 TRACE("Getting session key\n");
1276 lstrcpynA(buffer
, "GK", max_len
- 1);
1277 if((ret
= run_helper(helper
, buffer
, max_len
, &buffer_len
)) != SEC_E_OK
)
1281 TRACE("Helper does not support GK command\n");
1284 if(strncmp(buffer
, "BH ", 3) == 0)
1286 TRACE("Helper sent %s\n", debugstr_a(buffer
+3));
1287 helper
->session_key
= HeapAlloc(GetProcessHeap(), 0, 16);
1288 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1289 memset(helper
->session_key
, 0 , 16);
1291 else if(strncmp(buffer
, "GK ", 3) == 0)
1293 if((ret
= decodeBase64(buffer
+3, buffer_len
-3, bin
, max_len
,
1294 &bin_len
)) != SEC_E_OK
)
1296 TRACE("Failed to decode session key\n");
1298 TRACE("Session key is %s\n", debugstr_a(buffer
+3));
1299 helper
->session_key
= HeapAlloc(GetProcessHeap(), 0, 16);
1300 if(!helper
->session_key
)
1302 TRACE("Failed to allocate memory for session key\n");
1303 ret
= SEC_E_INTERNAL_ERROR
;
1306 memcpy(helper
->session_key
, bin
, 16);
1309 helper
->crypt
.ntlm
.a4i
= SECUR32_arc4Alloc();
1310 SECUR32_arc4Init(helper
->crypt
.ntlm
.a4i
, helper
->session_key
, 16);
1311 helper
->crypt
.ntlm
.seq_num
= 0l;
1314 phNewContext
->dwUpper
= ctxt_attr
;
1315 phNewContext
->dwLower
= (ULONG_PTR
)helper
;
1318 HeapFree(GetProcessHeap(), 0, want_flags
);
1319 HeapFree(GetProcessHeap(), 0, buffer
);
1320 HeapFree(GetProcessHeap(), 0, bin
);
1324 /***********************************************************************
1327 static SECURITY_STATUS SEC_ENTRY
ntlm_CompleteAuthToken(PCtxtHandle phContext
,
1328 PSecBufferDesc pToken
)
1330 /* We never need to call CompleteAuthToken anyway */
1331 TRACE("%p %p\n", phContext
, pToken
);
1333 return SEC_E_INVALID_HANDLE
;
1338 /***********************************************************************
1339 * DeleteSecurityContext
1341 static SECURITY_STATUS SEC_ENTRY
ntlm_DeleteSecurityContext(PCtxtHandle phContext
)
1345 TRACE("%p\n", phContext
);
1347 return SEC_E_INVALID_HANDLE
;
1349 helper
= (PNegoHelper
)phContext
->dwLower
;
1351 phContext
->dwUpper
= 0;
1352 phContext
->dwLower
= 0;
1354 SECUR32_arc4Cleanup(helper
->crypt
.ntlm
.a4i
);
1355 HeapFree(GetProcessHeap(), 0, helper
->session_key
);
1356 SECUR32_arc4Cleanup(helper
->crypt
.ntlm2
.send_a4i
);
1357 SECUR32_arc4Cleanup(helper
->crypt
.ntlm2
.recv_a4i
);
1358 HeapFree(GetProcessHeap(), 0, helper
->crypt
.ntlm2
.send_sign_key
);
1359 HeapFree(GetProcessHeap(), 0, helper
->crypt
.ntlm2
.send_seal_key
);
1360 HeapFree(GetProcessHeap(), 0, helper
->crypt
.ntlm2
.recv_sign_key
);
1361 HeapFree(GetProcessHeap(), 0, helper
->crypt
.ntlm2
.recv_seal_key
);
1363 cleanup_helper(helper
);
1368 /***********************************************************************
1369 * QueryContextAttributesW
1371 static SECURITY_STATUS SEC_ENTRY
ntlm_QueryContextAttributesW(PCtxtHandle phContext
,
1372 ULONG ulAttribute
, void *pBuffer
)
1374 TRACE("%p %d %p\n", phContext
, ulAttribute
, pBuffer
);
1376 return SEC_E_INVALID_HANDLE
;
1380 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1381 _x(SECPKG_ATTR_ACCESS_TOKEN
);
1382 _x(SECPKG_ATTR_AUTHORITY
);
1383 _x(SECPKG_ATTR_DCE_INFO
);
1384 case SECPKG_ATTR_FLAGS
:
1386 PSecPkgContext_Flags spcf
= (PSecPkgContext_Flags
)pBuffer
;
1387 PNegoHelper helper
= (PNegoHelper
)phContext
->dwLower
;
1390 if(helper
->neg_flags
& NTLMSSP_NEGOTIATE_SIGN
)
1391 spcf
->Flags
|= ISC_RET_INTEGRITY
;
1392 if(helper
->neg_flags
& NTLMSSP_NEGOTIATE_SEAL
)
1393 spcf
->Flags
|= ISC_RET_CONFIDENTIALITY
;
1396 _x(SECPKG_ATTR_KEY_INFO
);
1397 _x(SECPKG_ATTR_LIFESPAN
);
1398 _x(SECPKG_ATTR_NAMES
);
1399 _x(SECPKG_ATTR_NATIVE_NAMES
);
1400 _x(SECPKG_ATTR_NEGOTIATION_INFO
);
1401 _x(SECPKG_ATTR_PACKAGE_INFO
);
1402 _x(SECPKG_ATTR_PASSWORD_EXPIRY
);
1403 _x(SECPKG_ATTR_SESSION_KEY
);
1404 case SECPKG_ATTR_SIZES
:
1406 PSecPkgContext_Sizes spcs
= (PSecPkgContext_Sizes
)pBuffer
;
1407 spcs
->cbMaxToken
= NTLM_MAX_BUF
;
1408 spcs
->cbMaxSignature
= 16;
1409 spcs
->cbBlockSize
= 0;
1410 spcs
->cbSecurityTrailer
= 16;
1413 _x(SECPKG_ATTR_STREAM_SIZES
);
1414 _x(SECPKG_ATTR_TARGET_INFORMATION
);
1417 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute
);
1420 return SEC_E_UNSUPPORTED_FUNCTION
;
1423 /***********************************************************************
1424 * QueryContextAttributesA
1426 static SECURITY_STATUS SEC_ENTRY
ntlm_QueryContextAttributesA(PCtxtHandle phContext
,
1427 ULONG ulAttribute
, void *pBuffer
)
1429 return ntlm_QueryContextAttributesW(phContext
, ulAttribute
, pBuffer
);
1432 /***********************************************************************
1433 * ImpersonateSecurityContext
1435 static SECURITY_STATUS SEC_ENTRY
ntlm_ImpersonateSecurityContext(PCtxtHandle phContext
)
1437 SECURITY_STATUS ret
;
1439 TRACE("%p\n", phContext
);
1442 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
1446 ret
= SEC_E_INVALID_HANDLE
;
1451 /***********************************************************************
1452 * RevertSecurityContext
1454 static SECURITY_STATUS SEC_ENTRY
ntlm_RevertSecurityContext(PCtxtHandle phContext
)
1456 SECURITY_STATUS ret
;
1458 TRACE("%p\n", phContext
);
1461 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
1465 ret
= SEC_E_INVALID_HANDLE
;
1470 /***********************************************************************
1471 * ntlm_CreateSignature
1472 * As both MakeSignature and VerifySignature need this, but different keys
1473 * are needed for NTLM2, the logic goes into a helper function.
1474 * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1475 * signing/encrypting and NTLM_RECV for verfying/decrypting. When encrypting,
1476 * the signature is encrypted after the message was encrypted, so
1477 * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1480 static SECURITY_STATUS
ntlm_CreateSignature(PNegoHelper helper
, PSecBufferDesc pMessage
,
1481 int token_idx
, SignDirection direction
, BOOL encrypt_sig
)
1483 ULONG sign_version
= 1;
1486 TRACE("%p, %p, %d, %d, %d\n", helper
, pMessage
, token_idx
, direction
,
1489 sig
= pMessage
->pBuffers
[token_idx
].pvBuffer
;
1491 if(helper
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
&&
1492 helper
->neg_flags
& NTLMSSP_NEGOTIATE_SIGN
)
1496 HMAC_MD5_CTX hmac_md5_ctx
;
1498 TRACE("Signing NTLM2 style\n");
1500 if(direction
== NTLM_SEND
)
1502 seq_no
[0] = (helper
->crypt
.ntlm2
.send_seq_no
>> 0) & 0xff;
1503 seq_no
[1] = (helper
->crypt
.ntlm2
.send_seq_no
>> 8) & 0xff;
1504 seq_no
[2] = (helper
->crypt
.ntlm2
.send_seq_no
>> 16) & 0xff;
1505 seq_no
[3] = (helper
->crypt
.ntlm2
.send_seq_no
>> 24) & 0xff;
1507 ++(helper
->crypt
.ntlm2
.send_seq_no
);
1509 HMACMD5Init(&hmac_md5_ctx
, helper
->crypt
.ntlm2
.send_sign_key
, 16);
1513 seq_no
[0] = (helper
->crypt
.ntlm2
.recv_seq_no
>> 0) & 0xff;
1514 seq_no
[1] = (helper
->crypt
.ntlm2
.recv_seq_no
>> 8) & 0xff;
1515 seq_no
[2] = (helper
->crypt
.ntlm2
.recv_seq_no
>> 16) & 0xff;
1516 seq_no
[3] = (helper
->crypt
.ntlm2
.recv_seq_no
>> 24) & 0xff;
1518 ++(helper
->crypt
.ntlm2
.recv_seq_no
);
1520 HMACMD5Init(&hmac_md5_ctx
, helper
->crypt
.ntlm2
.recv_sign_key
, 16);
1523 HMACMD5Update(&hmac_md5_ctx
, seq_no
, 4);
1524 for( i
= 0; i
< pMessage
->cBuffers
; ++i
)
1526 if(pMessage
->pBuffers
[i
].BufferType
& SECBUFFER_DATA
)
1527 HMACMD5Update(&hmac_md5_ctx
, pMessage
->pBuffers
[i
].pvBuffer
,
1528 pMessage
->pBuffers
[i
].cbBuffer
);
1531 HMACMD5Final(&hmac_md5_ctx
, digest
);
1533 if(encrypt_sig
&& helper
->neg_flags
& NTLMSSP_NEGOTIATE_KEY_EXCHANGE
)
1535 if(direction
== NTLM_SEND
)
1536 SECUR32_arc4Process(helper
->crypt
.ntlm2
.send_a4i
, digest
, 8);
1538 SECUR32_arc4Process(helper
->crypt
.ntlm2
.recv_a4i
, digest
, 8);
1541 /* The NTLM2 signature is the sign version */
1542 sig
[ 0] = (sign_version
>> 0) & 0xff;
1543 sig
[ 1] = (sign_version
>> 8) & 0xff;
1544 sig
[ 2] = (sign_version
>> 16) & 0xff;
1545 sig
[ 3] = (sign_version
>> 24) & 0xff;
1546 /* The first 8 bytes of the digest */
1547 memcpy(sig
+4, digest
, 8);
1548 /* And the sequence number */
1549 memcpy(sig
+12, seq_no
, 4);
1551 pMessage
->pBuffers
[token_idx
].cbBuffer
= 16;
1555 if(helper
->neg_flags
& NTLMSSP_NEGOTIATE_SIGN
)
1558 TRACE("Signing NTLM1 style\n");
1560 for(i
=0; i
< pMessage
->cBuffers
; ++i
)
1562 if(pMessage
->pBuffers
[i
].BufferType
& SECBUFFER_DATA
)
1564 crc
= ComputeCrc32(pMessage
->pBuffers
[i
].pvBuffer
,
1565 pMessage
->pBuffers
[i
].cbBuffer
, crc
);
1569 sig
[ 0] = (sign_version
>> 0) & 0xff;
1570 sig
[ 1] = (sign_version
>> 8) & 0xff;
1571 sig
[ 2] = (sign_version
>> 16) & 0xff;
1572 sig
[ 3] = (sign_version
>> 24) & 0xff;
1573 memset(sig
+4, 0, 4);
1574 sig
[ 8] = (crc
>> 0) & 0xff;
1575 sig
[ 9] = (crc
>> 8) & 0xff;
1576 sig
[10] = (crc
>> 16) & 0xff;
1577 sig
[11] = (crc
>> 24) & 0xff;
1578 sig
[12] = (helper
->crypt
.ntlm
.seq_num
>> 0) & 0xff;
1579 sig
[13] = (helper
->crypt
.ntlm
.seq_num
>> 8) & 0xff;
1580 sig
[14] = (helper
->crypt
.ntlm
.seq_num
>> 16) & 0xff;
1581 sig
[15] = (helper
->crypt
.ntlm
.seq_num
>> 24) & 0xff;
1583 ++(helper
->crypt
.ntlm
.seq_num
);
1586 SECUR32_arc4Process(helper
->crypt
.ntlm
.a4i
, sig
+4, 12);
1590 if(helper
->neg_flags
& NTLMSSP_NEGOTIATE_ALWAYS_SIGN
|| helper
->neg_flags
== 0)
1592 TRACE("Creating a dummy signature.\n");
1593 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1594 memset(pMessage
->pBuffers
[token_idx
].pvBuffer
, 0, 16);
1595 memset(pMessage
->pBuffers
[token_idx
].pvBuffer
, 0x01, 1);
1596 pMessage
->pBuffers
[token_idx
].cbBuffer
= 16;
1600 return SEC_E_UNSUPPORTED_FUNCTION
;
1603 /***********************************************************************
1606 static SECURITY_STATUS SEC_ENTRY
ntlm_MakeSignature(PCtxtHandle phContext
, ULONG fQOP
,
1607 PSecBufferDesc pMessage
, ULONG MessageSeqNo
)
1612 TRACE("%p %d %p %d\n", phContext
, fQOP
, pMessage
, MessageSeqNo
);
1614 return SEC_E_INVALID_HANDLE
;
1617 FIXME("Ignoring fQOP 0x%08x\n", fQOP
);
1620 FIXME("Ignoring MessageSeqNo\n");
1622 if(!pMessage
|| !pMessage
->pBuffers
|| pMessage
->cBuffers
< 2)
1623 return SEC_E_INVALID_TOKEN
;
1625 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1626 if((token_idx
= ntlm_GetTokenBufferIndex(pMessage
)) == -1)
1627 return SEC_E_INVALID_TOKEN
;
1629 if(pMessage
->pBuffers
[token_idx
].cbBuffer
< 16)
1630 return SEC_E_BUFFER_TOO_SMALL
;
1632 helper
= (PNegoHelper
)phContext
->dwLower
;
1633 TRACE("Negotiated flags are: 0x%08x\n", helper
->neg_flags
);
1635 return ntlm_CreateSignature(helper
, pMessage
, token_idx
, NTLM_SEND
, TRUE
);
1638 /***********************************************************************
1641 static SECURITY_STATUS SEC_ENTRY
ntlm_VerifySignature(PCtxtHandle phContext
,
1642 PSecBufferDesc pMessage
, ULONG MessageSeqNo
, PULONG pfQOP
)
1648 SECURITY_STATUS ret
;
1649 SecBufferDesc local_desc
;
1650 PSecBuffer local_buff
;
1653 TRACE("%p %p %d %p\n", phContext
, pMessage
, MessageSeqNo
, pfQOP
);
1655 return SEC_E_INVALID_HANDLE
;
1657 if(!pMessage
|| !pMessage
->pBuffers
|| pMessage
->cBuffers
< 2)
1658 return SEC_E_INVALID_TOKEN
;
1660 if((token_idx
= ntlm_GetTokenBufferIndex(pMessage
)) == -1)
1661 return SEC_E_INVALID_TOKEN
;
1663 if(pMessage
->pBuffers
[token_idx
].cbBuffer
< 16)
1664 return SEC_E_BUFFER_TOO_SMALL
;
1667 FIXME("Ignoring MessageSeqNo\n");
1669 helper
= (PNegoHelper
)phContext
->dwLower
;
1670 TRACE("Negotiated flags: 0x%08x\n", helper
->neg_flags
);
1672 local_buff
= HeapAlloc(GetProcessHeap(), 0, pMessage
->cBuffers
* sizeof(SecBuffer
));
1674 local_desc
.ulVersion
= SECBUFFER_VERSION
;
1675 local_desc
.cBuffers
= pMessage
->cBuffers
;
1676 local_desc
.pBuffers
= local_buff
;
1678 for(i
=0; i
< pMessage
->cBuffers
; ++i
)
1680 if(pMessage
->pBuffers
[i
].BufferType
== SECBUFFER_TOKEN
)
1682 local_buff
[i
].BufferType
= SECBUFFER_TOKEN
;
1683 local_buff
[i
].cbBuffer
= 16;
1684 local_buff
[i
].pvBuffer
= local_sig
;
1688 local_buff
[i
].BufferType
= pMessage
->pBuffers
[i
].BufferType
;
1689 local_buff
[i
].cbBuffer
= pMessage
->pBuffers
[i
].cbBuffer
;
1690 local_buff
[i
].pvBuffer
= pMessage
->pBuffers
[i
].pvBuffer
;
1694 if((ret
= ntlm_CreateSignature(helper
, &local_desc
, token_idx
, NTLM_RECV
, TRUE
)) != SEC_E_OK
)
1697 if(memcmp(((PBYTE
)local_buff
[token_idx
].pvBuffer
) + 8,
1698 ((PBYTE
)pMessage
->pBuffers
[token_idx
].pvBuffer
) + 8, 8))
1699 ret
= SEC_E_MESSAGE_ALTERED
;
1703 HeapFree(GetProcessHeap(), 0, local_buff
);
1710 /***********************************************************************
1711 * FreeCredentialsHandle
1713 static SECURITY_STATUS SEC_ENTRY
ntlm_FreeCredentialsHandle(
1714 PCredHandle phCredential
)
1716 SECURITY_STATUS ret
;
1719 PNtlmCredentials ntlm_cred
= (PNtlmCredentials
) phCredential
->dwLower
;
1720 phCredential
->dwUpper
= 0;
1721 phCredential
->dwLower
= 0;
1722 if (ntlm_cred
->password
)
1723 memset(ntlm_cred
->password
, 0, ntlm_cred
->pwlen
);
1724 HeapFree(GetProcessHeap(), 0, ntlm_cred
->password
);
1725 HeapFree(GetProcessHeap(), 0, ntlm_cred
->username_arg
);
1726 HeapFree(GetProcessHeap(), 0, ntlm_cred
->domain_arg
);
1727 HeapFree(GetProcessHeap(), 0, ntlm_cred
);
1736 /***********************************************************************
1739 static SECURITY_STATUS SEC_ENTRY
ntlm_EncryptMessage(PCtxtHandle phContext
,
1740 ULONG fQOP
, PSecBufferDesc pMessage
, ULONG MessageSeqNo
)
1743 int token_idx
, data_idx
;
1745 TRACE("(%p %d %p %d)\n", phContext
, fQOP
, pMessage
, MessageSeqNo
);
1748 return SEC_E_INVALID_HANDLE
;
1751 FIXME("Ignoring fQOP\n");
1754 FIXME("Ignoring MessageSeqNo\n");
1756 if(!pMessage
|| !pMessage
->pBuffers
|| pMessage
->cBuffers
< 2)
1757 return SEC_E_INVALID_TOKEN
;
1759 if((token_idx
= ntlm_GetTokenBufferIndex(pMessage
)) == -1)
1760 return SEC_E_INVALID_TOKEN
;
1762 if((data_idx
= ntlm_GetDataBufferIndex(pMessage
)) ==-1 )
1763 return SEC_E_INVALID_TOKEN
;
1765 if(pMessage
->pBuffers
[token_idx
].cbBuffer
< 16)
1766 return SEC_E_BUFFER_TOO_SMALL
;
1768 helper
= (PNegoHelper
) phContext
->dwLower
;
1770 if(helper
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
&&
1771 helper
->neg_flags
& NTLMSSP_NEGOTIATE_SEAL
)
1773 ntlm_CreateSignature(helper
, pMessage
, token_idx
, NTLM_SEND
, FALSE
);
1774 SECUR32_arc4Process(helper
->crypt
.ntlm2
.send_a4i
,
1775 pMessage
->pBuffers
[data_idx
].pvBuffer
,
1776 pMessage
->pBuffers
[data_idx
].cbBuffer
);
1778 if(helper
->neg_flags
& NTLMSSP_NEGOTIATE_KEY_EXCHANGE
)
1779 SECUR32_arc4Process(helper
->crypt
.ntlm2
.send_a4i
,
1780 ((BYTE
*)pMessage
->pBuffers
[token_idx
].pvBuffer
)+4, 8);
1790 /* EncryptMessage always produces real signatures, so make sure
1791 * NTLMSSP_NEGOTIATE_SIGN is set*/
1792 save_flags
= helper
->neg_flags
;
1793 helper
->neg_flags
|= NTLMSSP_NEGOTIATE_SIGN
;
1794 ntlm_CreateSignature(helper
, pMessage
, token_idx
, NTLM_SEND
, FALSE
);
1795 helper
->neg_flags
= save_flags
;
1797 sig
= pMessage
->pBuffers
[token_idx
].pvBuffer
;
1799 SECUR32_arc4Process(helper
->crypt
.ntlm
.a4i
,
1800 pMessage
->pBuffers
[data_idx
].pvBuffer
,
1801 pMessage
->pBuffers
[data_idx
].cbBuffer
);
1802 SECUR32_arc4Process(helper
->crypt
.ntlm
.a4i
, sig
+4, 12);
1804 if(helper
->neg_flags
& NTLMSSP_NEGOTIATE_ALWAYS_SIGN
|| helper
->neg_flags
== 0)
1805 memset(sig
+4, 0, 4);
1812 /***********************************************************************
1815 static SECURITY_STATUS SEC_ENTRY
ntlm_DecryptMessage(PCtxtHandle phContext
,
1816 PSecBufferDesc pMessage
, ULONG MessageSeqNo
, PULONG pfQOP
)
1818 SECURITY_STATUS ret
;
1819 ULONG ntlmssp_flags_save
;
1821 int token_idx
, data_idx
;
1822 TRACE("(%p %p %d %p)\n", phContext
, pMessage
, MessageSeqNo
, pfQOP
);
1825 return SEC_E_INVALID_HANDLE
;
1828 FIXME("Ignoring MessageSeqNo\n");
1830 if(!pMessage
|| !pMessage
->pBuffers
|| pMessage
->cBuffers
< 2)
1831 return SEC_E_INVALID_TOKEN
;
1833 if((token_idx
= ntlm_GetTokenBufferIndex(pMessage
)) == -1)
1834 return SEC_E_INVALID_TOKEN
;
1836 if((data_idx
= ntlm_GetDataBufferIndex(pMessage
)) ==-1)
1837 return SEC_E_INVALID_TOKEN
;
1839 if(pMessage
->pBuffers
[token_idx
].cbBuffer
< 16)
1840 return SEC_E_BUFFER_TOO_SMALL
;
1842 helper
= (PNegoHelper
) phContext
->dwLower
;
1844 if(helper
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
&& helper
->neg_flags
& NTLMSSP_NEGOTIATE_SEAL
)
1846 SECUR32_arc4Process(helper
->crypt
.ntlm2
.recv_a4i
,
1847 pMessage
->pBuffers
[data_idx
].pvBuffer
,
1848 pMessage
->pBuffers
[data_idx
].cbBuffer
);
1852 SECUR32_arc4Process(helper
->crypt
.ntlm
.a4i
,
1853 pMessage
->pBuffers
[data_idx
].pvBuffer
,
1854 pMessage
->pBuffers
[data_idx
].cbBuffer
);
1857 /* Make sure we use a session key for the signature check, EncryptMessage
1858 * always does that, even in the dummy case */
1859 ntlmssp_flags_save
= helper
->neg_flags
;
1861 helper
->neg_flags
|= NTLMSSP_NEGOTIATE_SIGN
;
1862 ret
= ntlm_VerifySignature(phContext
, pMessage
, MessageSeqNo
, pfQOP
);
1864 helper
->neg_flags
= ntlmssp_flags_save
;
1869 static const SecurityFunctionTableA ntlmTableA
= {
1871 NULL
, /* EnumerateSecurityPackagesA */
1872 ntlm_QueryCredentialsAttributesA
, /* QueryCredentialsAttributesA */
1873 ntlm_AcquireCredentialsHandleA
, /* AcquireCredentialsHandleA */
1874 ntlm_FreeCredentialsHandle
, /* FreeCredentialsHandle */
1875 NULL
, /* Reserved2 */
1876 ntlm_InitializeSecurityContextA
, /* InitializeSecurityContextA */
1877 ntlm_AcceptSecurityContext
, /* AcceptSecurityContext */
1878 ntlm_CompleteAuthToken
, /* CompleteAuthToken */
1879 ntlm_DeleteSecurityContext
, /* DeleteSecurityContext */
1880 NULL
, /* ApplyControlToken */
1881 ntlm_QueryContextAttributesA
, /* QueryContextAttributesA */
1882 ntlm_ImpersonateSecurityContext
, /* ImpersonateSecurityContext */
1883 ntlm_RevertSecurityContext
, /* RevertSecurityContext */
1884 ntlm_MakeSignature
, /* MakeSignature */
1885 ntlm_VerifySignature
, /* VerifySignature */
1886 FreeContextBuffer
, /* FreeContextBuffer */
1887 NULL
, /* QuerySecurityPackageInfoA */
1888 NULL
, /* Reserved3 */
1889 NULL
, /* Reserved4 */
1890 NULL
, /* ExportSecurityContext */
1891 NULL
, /* ImportSecurityContextA */
1892 NULL
, /* AddCredentialsA */
1893 NULL
, /* Reserved8 */
1894 NULL
, /* QuerySecurityContextToken */
1895 ntlm_EncryptMessage
, /* EncryptMessage */
1896 ntlm_DecryptMessage
, /* DecryptMessage */
1897 NULL
, /* SetContextAttributesA */
1900 static const SecurityFunctionTableW ntlmTableW
= {
1902 NULL
, /* EnumerateSecurityPackagesW */
1903 ntlm_QueryCredentialsAttributesW
, /* QueryCredentialsAttributesW */
1904 ntlm_AcquireCredentialsHandleW
, /* AcquireCredentialsHandleW */
1905 ntlm_FreeCredentialsHandle
, /* FreeCredentialsHandle */
1906 NULL
, /* Reserved2 */
1907 ntlm_InitializeSecurityContextW
, /* InitializeSecurityContextW */
1908 ntlm_AcceptSecurityContext
, /* AcceptSecurityContext */
1909 ntlm_CompleteAuthToken
, /* CompleteAuthToken */
1910 ntlm_DeleteSecurityContext
, /* DeleteSecurityContext */
1911 NULL
, /* ApplyControlToken */
1912 ntlm_QueryContextAttributesW
, /* QueryContextAttributesW */
1913 ntlm_ImpersonateSecurityContext
, /* ImpersonateSecurityContext */
1914 ntlm_RevertSecurityContext
, /* RevertSecurityContext */
1915 ntlm_MakeSignature
, /* MakeSignature */
1916 ntlm_VerifySignature
, /* VerifySignature */
1917 FreeContextBuffer
, /* FreeContextBuffer */
1918 NULL
, /* QuerySecurityPackageInfoW */
1919 NULL
, /* Reserved3 */
1920 NULL
, /* Reserved4 */
1921 NULL
, /* ExportSecurityContext */
1922 NULL
, /* ImportSecurityContextW */
1923 NULL
, /* AddCredentialsW */
1924 NULL
, /* Reserved8 */
1925 NULL
, /* QuerySecurityContextToken */
1926 ntlm_EncryptMessage
, /* EncryptMessage */
1927 ntlm_DecryptMessage
, /* DecryptMessage */
1928 NULL
, /* SetContextAttributesW */
1931 #define NTLM_COMMENT \
1932 { 'N', 'T', 'L', 'M', ' ', \
1933 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1934 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1936 static CHAR ntlm_comment_A
[] = NTLM_COMMENT
;
1937 static WCHAR ntlm_comment_W
[] = NTLM_COMMENT
;
1939 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1941 static char ntlm_name_A
[] = NTLM_NAME
;
1942 static WCHAR ntlm_name_W
[] = NTLM_NAME
;
1944 /* According to Windows, NTLM has the following capabilities. */
1946 SECPKG_FLAG_INTEGRITY | \
1947 SECPKG_FLAG_PRIVACY | \
1948 SECPKG_FLAG_TOKEN_ONLY | \
1949 SECPKG_FLAG_CONNECTION | \
1950 SECPKG_FLAG_MULTI_REQUIRED | \
1951 SECPKG_FLAG_IMPERSONATION | \
1952 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1953 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1955 static const SecPkgInfoW infoW
= {
1964 static const SecPkgInfoA infoA
= {
1973 #define NEGO_COMMENT { 'M', 'i', 'c', 'r', 'o', 's', 'o', 'f', 't', ' ', \
1974 'P', 'a', 'c', 'k', 'a', 'g', 'e', ' ', \
1975 'N', 'e', 'g', 'o', 't', 'i', 'a', 't', 'o', 'r', 0};
1977 static CHAR nego_comment_A
[] = NEGO_COMMENT
;
1978 static WCHAR nego_comment_W
[] = NEGO_COMMENT
;
1980 #define NEGO_NAME {'N', 'e', 'g', 'o', 't', 'i', 'a', 't', 'e', 0}
1982 static CHAR nego_name_A
[] = NEGO_NAME
;
1983 static WCHAR nego_name_W
[] = NEGO_NAME
;
1985 #define NEGO_CAPS (\
1986 SECPKG_FLAG_INTEGRITY | \
1987 SECPKG_FLAG_PRIVACY | \
1988 SECPKG_FLAG_CONNECTION | \
1989 SECPKG_FLAG_MULTI_REQUIRED | \
1990 SECPKG_FLAG_EXTENDED_ERROR | \
1991 SECPKG_FLAG_IMPERSONATION | \
1992 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1993 SECPKG_FLAG_READONLY_WITH_CHECKSUM )
1995 /* Not used for now, just kept here for completeness sake. We need to use the
1996 * NTLM_MAX_BUF value. If the hack works, we might want to refactor the code a
1998 #define NEGO_MAX_TOKEN 12000
2000 static const SecPkgInfoW nego_infoW
= {
2003 RPC_C_AUTHN_GSS_NEGOTIATE
,
2009 static const SecPkgInfoA nego_infoA
= {
2012 RPC_C_AUTHN_GSS_NEGOTIATE
,
2018 void SECUR32_initNTLMSP(void)
2021 static CHAR version
[] = "--version";
2023 SEC_CHAR
*args
[] = {
2028 if(fork_helper(&helper
, ntlm_auth
, args
) != SEC_E_OK
)
2030 /* Cheat and allocate a helper anyway, so cleanup later will work. */
2031 helper
= HeapAlloc(GetProcessHeap(),0, sizeof(NegoHelper
));
2032 helper
->major
= helper
->minor
= helper
->micro
= -1;
2033 helper
->pipe_in
= helper
->pipe_out
= -1;
2036 check_version(helper
);
2038 if( (helper
->major
> MIN_NTLM_AUTH_MAJOR_VERSION
) ||
2039 (helper
->major
== MIN_NTLM_AUTH_MAJOR_VERSION
&&
2040 helper
->minor
> MIN_NTLM_AUTH_MINOR_VERSION
) ||
2041 (helper
->major
== MIN_NTLM_AUTH_MAJOR_VERSION
&&
2042 helper
->minor
== MIN_NTLM_AUTH_MINOR_VERSION
&&
2043 helper
->micro
>= MIN_NTLM_AUTH_MICRO_VERSION
) )
2045 SecureProvider
*provider
= SECUR32_addProvider(&ntlmTableA
, &ntlmTableW
, NULL
);
2046 SecureProvider
*nego_provider
= SECUR32_addProvider(&ntlmTableA
, &ntlmTableW
, NULL
);
2048 SECUR32_addPackages(provider
, 1L, &infoA
, &infoW
);
2049 /* HACK: Also pretend this is the Negotiate provider */
2050 SECUR32_addPackages(nego_provider
, 1L, &nego_infoA
, &nego_infoW
);
2054 ERR("%s was not found or is outdated. "
2055 "Make sure that ntlm_auth >= %d.%d.%d is in your path.\n",
2057 MIN_NTLM_AUTH_MAJOR_VERSION
,
2058 MIN_NTLM_AUTH_MINOR_VERSION
,
2059 MIN_NTLM_AUTH_MICRO_VERSION
);
2060 ERR("Usually, you can find it in the winbind package of your "
2064 cleanup_helper(helper
);