Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / secur32 / ntlm.c
blob00c9821bfb5a76ead5bd9d464c0469fb347148ea
1 /*
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.
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnls.h"
28 #include "rpc.h"
29 #include "sspi.h"
30 #include "lm.h"
31 #include "secur32_priv.h"
32 #include "hmac_md5.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
37 #define NTLM_MAX_BUF 1904
38 #define MIN_NTLM_AUTH_MAJOR_VERSION 3
39 #define MIN_NTLM_AUTH_MINOR_VERSION 0
40 #define MIN_NTLM_AUTH_MICRO_VERSION 25
42 static CHAR ntlm_auth[256];
44 /***********************************************************************
45 * QueryCredentialsAttributesA
47 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
48 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
50 SECURITY_STATUS ret;
52 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
54 if(ulAttribute == SECPKG_ATTR_NAMES)
56 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
57 ret = SEC_E_UNSUPPORTED_FUNCTION;
59 else
60 ret = SEC_E_UNSUPPORTED_FUNCTION;
62 return ret;
65 /***********************************************************************
66 * QueryCredentialsAttributesW
68 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
69 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
71 SECURITY_STATUS ret;
73 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
75 if(ulAttribute == SECPKG_ATTR_NAMES)
77 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
78 ret = SEC_E_UNSUPPORTED_FUNCTION;
80 else
81 ret = SEC_E_UNSUPPORTED_FUNCTION;
83 return ret;
86 /***********************************************************************
87 * AcquireCredentialsHandleW
89 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
90 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
91 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
92 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
94 SECURITY_STATUS ret;
95 PNegoHelper helper = NULL;
96 static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp",
97 credentials_argv[] = "--use-cached-creds";
99 SEC_CHAR *client_user_arg = NULL;
100 SEC_CHAR *client_domain_arg = NULL;
101 SEC_WCHAR *username = NULL, *domain = NULL;
103 SEC_CHAR *client_argv[6];
104 SEC_CHAR *server_argv[] = { ntlm_auth,
105 server_helper_protocol,
106 NULL };
108 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
109 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
110 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
113 switch(fCredentialUse)
115 case SECPKG_CRED_INBOUND:
116 if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
117 SEC_E_OK)
119 phCredential = NULL;
120 break;
122 else
124 helper->mode = NTLM_SERVER;
125 phCredential->dwUpper = fCredentialUse;
126 phCredential->dwLower = (ULONG_PTR)helper;
128 ret = SEC_E_OK;
129 break;
130 case SECPKG_CRED_OUTBOUND:
132 static const char username_arg[] = "--username=";
133 static const char domain_arg[] = "--domain=";
134 static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
135 int unixcp_size;
137 if(pAuthData == NULL)
139 LPWKSTA_USER_INFO_1 ui = NULL;
140 NET_API_STATUS status;
142 status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
143 if (status != NERR_Success || ui == NULL)
145 ret = SEC_E_NO_CREDENTIALS;
146 phCredential = NULL;
147 break;
150 username = HeapAlloc(GetProcessHeap(), 0,
151 (lstrlenW(ui->wkui1_username)+1) *
152 sizeof(SEC_WCHAR));
153 lstrcpyW(username, ui->wkui1_username);
155 /* same for the domain */
156 domain = HeapAlloc(GetProcessHeap(), 0,
157 (lstrlenW(ui->wkui1_logon_domain)+1) *
158 sizeof(SEC_WCHAR));
159 lstrcpyW(domain, ui->wkui1_logon_domain);
160 NetApiBufferFree(ui);
162 else
164 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
165 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
167 /* Get username and domain from pAuthData */
168 username = HeapAlloc(GetProcessHeap(), 0,
169 (auth_data->UserLength + 1) * sizeof(SEC_WCHAR));
170 memcpy(username, auth_data->User,
171 auth_data->UserLength * sizeof(SEC_WCHAR));
172 username[auth_data->UserLength] = '\0';
174 domain = HeapAlloc(GetProcessHeap(), 0,
175 (auth_data->DomainLength + 1) * sizeof(SEC_WCHAR));
176 memcpy(domain, auth_data->Domain,
177 auth_data->DomainLength * sizeof(SEC_WCHAR));
178 domain[auth_data->DomainLength] = '\0';
180 TRACE("Username is %s\n", debugstr_w(username));
181 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
182 username, -1, NULL, 0, NULL, NULL) + sizeof(username_arg);
183 client_user_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
184 lstrcpyA(client_user_arg, username_arg);
185 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, username, -1,
186 client_user_arg + sizeof(username_arg) - 1,
187 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
189 TRACE("Domain name is %s\n", debugstr_w(domain));
190 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
191 domain, -1, NULL, 0, NULL, NULL) + sizeof(domain_arg);
192 client_domain_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
193 lstrcpyA(client_domain_arg, domain_arg);
194 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domain,
195 -1, client_domain_arg + sizeof(domain_arg) - 1,
196 unixcp_size - sizeof(domain) + 1, NULL, NULL);
198 client_argv[0] = ntlm_auth;
199 client_argv[1] = helper_protocol;
200 client_argv[2] = client_user_arg;
201 client_argv[3] = client_domain_arg;
202 client_argv[4] = credentials_argv;
203 client_argv[5] = NULL;
205 if((ret = fork_helper(&helper, ntlm_auth, client_argv)) !=
206 SEC_E_OK)
208 phCredential = NULL;
209 break;
211 else
213 helper->mode = NTLM_CLIENT;
215 if(pAuthData != NULL)
217 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
218 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
220 if(auth_data->PasswordLength != 0)
222 helper->pwlen = WideCharToMultiByte(CP_UNIXCP,
223 WC_NO_BEST_FIT_CHARS, auth_data->Password,
224 auth_data->PasswordLength, NULL, 0, NULL,
225 NULL);
227 helper->password = HeapAlloc(GetProcessHeap(), 0,
228 helper->pwlen);
230 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
231 auth_data->Password, auth_data->PasswordLength,
232 helper->password, helper->pwlen, NULL, NULL);
236 phCredential->dwUpper = fCredentialUse;
237 phCredential->dwLower = (ULONG_PTR)helper;
238 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
239 phCredential->dwUpper, phCredential->dwLower);
241 ret = SEC_E_OK;
242 break;
244 case SECPKG_CRED_BOTH:
245 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
246 ret = SEC_E_UNSUPPORTED_FUNCTION;
247 phCredential = NULL;
248 break;
249 default:
250 phCredential = NULL;
251 ret = SEC_E_UNKNOWN_CREDENTIALS;
255 HeapFree(GetProcessHeap(), 0, client_user_arg);
256 HeapFree(GetProcessHeap(), 0, client_domain_arg);
257 HeapFree(GetProcessHeap(), 0, username);
258 HeapFree(GetProcessHeap(), 0, domain);
260 return ret;
263 /***********************************************************************
264 * AcquireCredentialsHandleA
266 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
267 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
268 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
269 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
271 SECURITY_STATUS ret;
272 int user_sizeW, domain_sizeW, passwd_sizeW;
274 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
276 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
277 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
279 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
280 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
281 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
283 if(pszPackage != NULL)
285 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
286 NULL, 0);
288 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
289 sizeof(SEC_WCHAR));
290 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
294 if(pAuthData != NULL)
296 identity = (PSEC_WINNT_AUTH_IDENTITY_A)pAuthData;
298 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
300 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
301 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
303 if(identity->UserLength != 0)
305 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
306 (LPCSTR)identity->User, identity->UserLength, NULL, 0);
307 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
308 sizeof(SEC_WCHAR));
309 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
310 identity->UserLength, user, user_sizeW);
312 else
314 user_sizeW = 0;
317 if(identity->DomainLength != 0)
319 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
320 (LPCSTR)identity->Domain, identity->DomainLength, NULL, 0);
321 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
322 * sizeof(SEC_WCHAR));
323 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
324 identity->DomainLength, domain, domain_sizeW);
326 else
328 domain_sizeW = 0;
331 if(identity->PasswordLength != 0)
333 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
334 (LPCSTR)identity->Password, identity->PasswordLength,
335 NULL, 0);
336 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
337 * sizeof(SEC_WCHAR));
338 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
339 identity->PasswordLength, passwd, passwd_sizeW);
341 else
343 passwd_sizeW = 0;
346 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
347 pAuthDataW->User = user;
348 pAuthDataW->UserLength = user_sizeW;
349 pAuthDataW->Domain = domain;
350 pAuthDataW->DomainLength = domain_sizeW;
351 pAuthDataW->Password = passwd;
352 pAuthDataW->PasswordLength = passwd_sizeW;
354 else
356 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
360 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
361 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
362 ptsExpiry);
364 HeapFree(GetProcessHeap(), 0, package);
365 HeapFree(GetProcessHeap(), 0, user);
366 HeapFree(GetProcessHeap(), 0, domain);
367 HeapFree(GetProcessHeap(), 0, passwd);
368 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
369 HeapFree(GetProcessHeap(), 0, pAuthDataW);
371 return ret;
374 /***********************************************************************
375 * InitializeSecurityContextW
377 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
378 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
379 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
380 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
381 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
383 SECURITY_STATUS ret;
384 PNegoHelper helper;
385 ULONG ctxt_attr = 0;
386 char* buffer, *want_flags = NULL;
387 PBYTE bin;
388 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
390 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
391 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
392 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
394 if(!phCredential)
395 return SEC_E_INVALID_HANDLE;
397 /* As the server side of sspi never calls this, make sure that
398 * the handler is a client handler.
400 helper = (PNegoHelper)phCredential->dwLower;
401 if(helper->mode != NTLM_CLIENT)
403 TRACE("Helper mode = %d\n", helper->mode);
404 return SEC_E_INVALID_HANDLE;
407 /****************************************
408 * When communicating with the client, there can be the
409 * following reply packets:
410 * YR <base64 blob> should be sent to the server
411 * PW should be sent back to helper with
412 * base64 encoded password
413 * AF <base64 blob> client is done, blob should be
414 * sent to server with KK prefixed
415 * GF <string list> A string list of negotiated flags
416 * GK <base64 blob> base64 encoded session key
417 * BH <char reason> something broke
419 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
421 if (pszTargetName)
423 TRACE("According to a MS whitepaper pszTargetName is ignored.\n");
426 if(TargetDataRep == SECURITY_NETWORK_DREP){
427 TRACE("Setting SECURITY_NETWORK_DREP\n");
430 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
431 bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
433 if((phContext == NULL) && (pInput == NULL))
435 TRACE("First time in ISC()\n");
436 /* Allocate space for a maximal string of
437 * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
438 * NTLMSSP_FEATURE_SESSION_KEY"
440 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
441 if(want_flags == NULL)
443 ret = SEC_E_INSUFFICIENT_MEMORY;
444 goto isc_end;
446 lstrcpyA(want_flags, "SF");
447 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
449 char *ptr;
450 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SEAL")) == NULL)
451 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
453 if(fContextReq & ISC_REQ_CONNECTION)
454 ctxt_attr |= ISC_RET_CONNECTION;
455 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
456 ctxt_attr |= ISC_RET_EXTENDED_ERROR;
457 if(fContextReq & ISC_REQ_INTEGRITY)
459 char *ptr;
460 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
461 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
463 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
464 ctxt_attr |= ISC_RET_MUTUAL_AUTH;
465 if(fContextReq & ISC_REQ_REPLAY_DETECT)
467 char *ptr;
468 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
469 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
471 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
473 char *ptr;
474 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
475 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
477 if(fContextReq & ISC_REQ_STREAM)
478 FIXME("ISC_REQ_STREAM\n");
479 if(fContextReq & ISC_REQ_USE_DCE_STYLE)
480 ctxt_attr |= ISC_RET_USED_DCE_STYLE;
481 if(fContextReq & ISC_REQ_DELEGATE)
482 ctxt_attr |= ISC_RET_DELEGATE;
484 /* If no password is given, try to use cached credentials. Fall back to an empty
485 * password if this failed. */
486 if(helper->password == NULL)
488 lstrcpynA(buffer, "OK", max_len-1);
489 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
490 goto isc_end;
491 /* If the helper replied with "PW", using cached credentials failed */
492 if(!strncmp(buffer, "PW", 2))
494 TRACE("Using cached credentials failed. Using empty password.\n");
495 lstrcpynA(buffer, "PW AA==", max_len-1);
497 else /* Just do a noop on the next run */
498 lstrcpynA(buffer, "OK", max_len-1);
500 else
502 lstrcpynA(buffer, "PW ", max_len-1);
503 if((ret = encodeBase64((unsigned char*)helper->password,
504 helper->pwlen, buffer+3,
505 max_len-3, &buffer_len)) != SEC_E_OK)
507 TRACE("Deleting password!\n");
508 memset(helper->password, 0, helper->pwlen);
509 HeapFree(GetProcessHeap(), 0, helper->password);
510 goto isc_end;
515 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
516 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
517 goto isc_end;
519 TRACE("Helper returned %s\n", debugstr_a(buffer));
521 if(lstrlenA(want_flags) > 2)
523 TRACE("Want flags are %s\n", debugstr_a(want_flags));
524 lstrcpynA(buffer, want_flags, max_len-1);
525 if((ret = run_helper(helper, buffer, max_len, &buffer_len))
526 != SEC_E_OK)
527 goto isc_end;
528 if(!strncmp(buffer, "BH", 2))
529 ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
532 lstrcpynA(buffer, "YR", max_len-1);
534 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
535 goto isc_end;
537 TRACE("%s\n", buffer);
539 if(strncmp(buffer, "YR ", 3) != 0)
541 /* Something borked */
542 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
543 ret = SEC_E_INTERNAL_ERROR;
544 goto isc_end;
546 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
547 max_len-1, &bin_len)) != SEC_E_OK)
548 goto isc_end;
550 /* put the decoded client blob into the out buffer */
552 ret = SEC_I_CONTINUE_NEEDED;
554 else
556 /* handle second call here */
557 /* encode server data to base64 */
558 if (!pInput || !pInput->cBuffers)
560 ret = SEC_E_INCOMPLETE_MESSAGE;
561 goto isc_end;
564 if (!pInput->pBuffers[0].pvBuffer)
566 ret = SEC_E_INTERNAL_ERROR;
567 goto isc_end;
570 if(pInput->pBuffers[0].cbBuffer > max_len)
572 TRACE("pInput->pBuffers[0].cbBuffer is: %ld\n",
573 pInput->pBuffers[0].cbBuffer);
574 ret = SEC_E_INVALID_TOKEN;
575 goto isc_end;
577 else
578 bin_len = pInput->pBuffers[0].cbBuffer;
580 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
582 lstrcpynA(buffer, "TT ", max_len-1);
584 if((ret = encodeBase64(bin, bin_len, buffer+3,
585 max_len-3, &buffer_len)) != SEC_E_OK)
586 goto isc_end;
588 TRACE("Server sent: %s\n", debugstr_a(buffer));
590 /* send TT base64 blob to ntlm_auth */
591 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
592 goto isc_end;
594 TRACE("Helper replied: %s\n", debugstr_a(buffer));
596 if( (strncmp(buffer, "KK ", 3) != 0) &&
597 (strncmp(buffer, "AF ", 3) !=0))
599 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
600 ret = SEC_E_INVALID_TOKEN;
601 goto isc_end;
604 /* decode the blob and send it to server */
605 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
606 &bin_len)) != SEC_E_OK)
608 goto isc_end;
611 phNewContext->dwUpper = ctxt_attr;
612 phNewContext->dwLower = (ULONG_PTR)helper;
614 ret = SEC_E_OK;
617 /* put the decoded client blob into the out buffer */
619 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
621 if (pOutput)
623 pOutput->cBuffers = 1;
624 pOutput->pBuffers[0].pvBuffer = SECUR32_ALLOC(bin_len);
625 pOutput->pBuffers[0].cbBuffer = bin_len;
629 if (!pOutput || !pOutput->cBuffers || pOutput->pBuffers[0].cbBuffer < bin_len)
631 TRACE("out buffer is NULL or has not enough space\n");
632 ret = SEC_E_BUFFER_TOO_SMALL;
633 goto isc_end;
636 if (!pOutput->pBuffers[0].pvBuffer)
638 TRACE("out buffer is NULL\n");
639 ret = SEC_E_INTERNAL_ERROR;
640 goto isc_end;
643 pOutput->pBuffers[0].cbBuffer = bin_len;
644 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
645 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
647 if(ret == SEC_E_OK)
649 TRACE("Getting negotiated flags\n");
650 lstrcpynA(buffer, "GF", max_len - 1);
651 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
652 goto isc_end;
654 if(buffer_len < 3)
656 TRACE("No flags negotiated.\n");
657 helper->neg_flags = 0l;
659 else
661 TRACE("Negotiated %s\n", debugstr_a(buffer));
662 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
663 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
666 TRACE("Getting session key\n");
667 lstrcpynA(buffer, "GK", max_len - 1);
668 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
669 goto isc_end;
671 if(strncmp(buffer, "BH", 2) == 0)
673 TRACE("No key negotiated.\n");
674 helper->valid_session_key = FALSE;
675 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
676 /*Generate the dummy session key = MD4(MD4(password))*/
677 if(helper->password)
679 SEC_WCHAR *unicode_password;
680 int passwd_lenW;
682 TRACE("Converting password to unicode.\n");
683 passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
684 (LPCSTR)helper->password, helper->pwlen,
685 NULL, 0);
686 unicode_password = HeapAlloc(GetProcessHeap(), 0,
687 passwd_lenW * sizeof(SEC_WCHAR));
688 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)helper->password,
689 helper->pwlen, unicode_password, passwd_lenW);
691 SECUR32_CreateNTLMv1SessionKey((PBYTE)unicode_password,
692 passwd_lenW * sizeof(SEC_WCHAR), helper->session_key);
694 HeapFree(GetProcessHeap(), 0, unicode_password);
696 else
697 memset(helper->session_key, 0, 16);
699 else if(strncmp(buffer, "GK ", 3) == 0)
701 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
702 &bin_len)) != SEC_E_OK)
704 TRACE("Failed to decode session key\n");
706 TRACE("Session key is %s\n", debugstr_a(buffer+3));
707 helper->valid_session_key = TRUE;
708 helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
709 if(!helper->session_key)
711 TRACE("Failed to allocate memory for session key\n");
712 ret = SEC_E_INTERNAL_ERROR;
713 goto isc_end;
715 memcpy(helper->session_key, bin, bin_len);
718 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
719 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
720 helper->crypt.ntlm.seq_num = 0l;
721 SECUR32_CreateNTLMv2SubKeys(helper);
722 helper->crypt.ntlm2.send_a4i = SECUR32_arc4Alloc();
723 helper->crypt.ntlm2.recv_a4i = SECUR32_arc4Alloc();
724 SECUR32_arc4Init(helper->crypt.ntlm2.send_a4i,
725 (BYTE *)helper->crypt.ntlm2.send_seal_key, 16);
726 SECUR32_arc4Init(helper->crypt.ntlm2.recv_a4i,
727 (BYTE *)helper->crypt.ntlm2.recv_seal_key, 16);
728 helper->crypt.ntlm2.send_seq_no = 0l;
729 helper->crypt.ntlm2.recv_seq_no = 0l;
732 if(ret != SEC_I_CONTINUE_NEEDED)
734 TRACE("Deleting password!\n");
735 if(helper->password)
736 memset(helper->password, 0, helper->pwlen);
737 HeapFree(GetProcessHeap(), 0, helper->password);
739 isc_end:
740 HeapFree(GetProcessHeap(), 0, want_flags);
741 HeapFree(GetProcessHeap(), 0, buffer);
742 HeapFree(GetProcessHeap(), 0, bin);
743 return ret;
746 /***********************************************************************
747 * InitializeSecurityContextA
749 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
750 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
751 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
752 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
753 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
755 SECURITY_STATUS ret;
757 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
758 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
759 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
761 if (phCredential)
763 SEC_WCHAR *target = NULL;
764 if(pszTargetName != NULL)
766 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
767 strlen(pszTargetName)+1, NULL, 0);
768 target = HeapAlloc(GetProcessHeap(), 0, target_size *
769 sizeof(SEC_WCHAR));
770 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
771 target, target_size);
774 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
775 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
776 phNewContext, pOutput, pfContextAttr, ptsExpiry);
778 HeapFree(GetProcessHeap(), 0, target);
780 else
782 ret = SEC_E_INVALID_HANDLE;
784 return ret;
787 /***********************************************************************
788 * AcceptSecurityContext
790 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
791 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
792 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
793 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
795 SECURITY_STATUS ret;
796 char *buffer, *want_flags = NULL;
797 PBYTE bin;
798 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
799 ULONG ctxt_attr = 0;
800 PNegoHelper helper;
802 TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
803 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
804 ptsExpiry);
806 if (!phCredential)
807 return SEC_E_INVALID_HANDLE;
809 helper = (PNegoHelper)phCredential->dwLower;
811 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
812 bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
814 if(helper->mode != NTLM_SERVER)
816 ret = SEC_E_INVALID_HANDLE;
817 goto asc_end;
820 if(TargetDataRep == SECURITY_NETWORK_DREP){
821 TRACE("Using SECURITY_NETWORK_DREP\n");
824 if(phContext == NULL)
826 /* This is the first call to AcceptSecurityHandle */
827 if(pInput == NULL)
829 ret = SEC_E_INCOMPLETE_MESSAGE;
830 goto asc_end;
833 if(pInput->cBuffers < 1)
835 ret = SEC_E_INCOMPLETE_MESSAGE;
836 goto asc_end;
839 if(pInput->pBuffers[0].cbBuffer > max_len)
841 ret = SEC_E_INVALID_TOKEN;
842 goto asc_end;
844 else
845 bin_len = pInput->pBuffers[0].cbBuffer;
847 /* Handle all the flags */
848 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
849 if(want_flags == NULL)
851 TRACE("Failed to allocate memory for the want_flags!\n");
852 ret = SEC_E_INSUFFICIENT_MEMORY;
853 goto asc_end;
855 lstrcpyA(want_flags, "SF");
856 if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
858 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
860 if(fContextReq & ASC_REQ_CONFIDENTIALITY)
862 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
864 if(fContextReq & ASC_REQ_CONNECTION)
866 /* This is default, so we'll enable it */
867 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
868 ctxt_attr |= ASC_RET_CONNECTION;
870 if(fContextReq & ASC_REQ_EXTENDED_ERROR)
872 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
874 if(fContextReq & ASC_REQ_INTEGRITY)
876 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
878 if(fContextReq & ASC_REQ_MUTUAL_AUTH)
880 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
882 if(fContextReq & ASC_REQ_REPLAY_DETECT)
884 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
886 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
888 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
890 if(fContextReq & ISC_REQ_STREAM)
892 FIXME("ASC_REQ_STREAM stub\n");
894 /* Done with the flags */
896 if(lstrlenA(want_flags) > 3)
898 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
899 lstrcpynA(buffer, want_flags, max_len - 1);
900 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
901 SEC_E_OK)
902 goto asc_end;
903 if(!strncmp(buffer, "BH", 2))
904 TRACE("Helper doesn't understand new command set\n");
907 /* This is the YR request from the client, encode to base64 */
909 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
911 lstrcpynA(buffer, "YR ", max_len-1);
913 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
914 &buffer_len)) != SEC_E_OK)
916 goto asc_end;
919 TRACE("Client sent: %s\n", debugstr_a(buffer));
921 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
922 SEC_E_OK)
924 goto asc_end;
927 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
928 /* The expected answer is TT <base64 blob> */
930 if(strncmp(buffer, "TT ", 3) != 0)
932 ret = SEC_E_INTERNAL_ERROR;
933 goto asc_end;
936 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
937 &bin_len)) != SEC_E_OK)
939 goto asc_end;
942 /* send this to the client */
943 if(pOutput == NULL)
945 ret = SEC_E_INSUFFICIENT_MEMORY;
946 goto asc_end;
949 if(pOutput->cBuffers < 1)
951 ret = SEC_E_INSUFFICIENT_MEMORY;
952 goto asc_end;
955 pOutput->pBuffers[0].cbBuffer = bin_len;
956 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
957 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
958 ret = SEC_I_CONTINUE_NEEDED;
961 else
963 /* we expect a KK request from client */
964 if(pInput == NULL)
966 ret = SEC_E_INCOMPLETE_MESSAGE;
967 goto asc_end;
970 if(pInput->cBuffers < 1)
972 ret = SEC_E_INCOMPLETE_MESSAGE;
973 goto asc_end;
976 if(pInput->pBuffers[0].cbBuffer > max_len)
978 ret = SEC_E_INVALID_TOKEN;
979 goto asc_end;
981 else
982 bin_len = pInput->pBuffers[0].cbBuffer;
984 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
986 lstrcpynA(buffer, "KK ", max_len-1);
988 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
989 &buffer_len)) != SEC_E_OK)
991 goto asc_end;
994 TRACE("Client sent: %s\n", debugstr_a(buffer));
996 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
997 SEC_E_OK)
999 goto asc_end;
1002 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1004 if(strncmp(buffer, "AF ", 3) != 0)
1006 if(strncmp(buffer, "NA ", 3) == 0)
1008 ret = SEC_E_LOGON_DENIED;
1009 goto asc_end;
1011 else
1013 ret = SEC_E_INTERNAL_ERROR;
1014 goto asc_end;
1017 pOutput->pBuffers[0].cbBuffer = 0;
1018 ret = SEC_E_OK;
1020 TRACE("Getting negotiated flags\n");
1021 lstrcpynA(buffer, "GF", max_len - 1);
1022 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1023 goto asc_end;
1025 if(buffer_len < 3)
1027 TRACE("No flags negotiated, or helper does not support GF command\n");
1029 else
1031 TRACE("Negotiated %s\n", debugstr_a(buffer));
1032 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
1033 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
1036 TRACE("Getting session key\n");
1037 lstrcpynA(buffer, "GK", max_len - 1);
1038 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1039 goto asc_end;
1041 if(buffer_len < 3)
1042 TRACE("Helper does not support GK command\n");
1043 else
1045 if(strncmp(buffer, "BH ", 3) == 0)
1047 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1048 helper->valid_session_key = FALSE;
1049 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1050 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1051 memset(helper->session_key, 0 , 16);
1053 else if(strncmp(buffer, "GK ", 3) == 0)
1055 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1056 &bin_len)) != SEC_E_OK)
1058 TRACE("Failed to decode session key\n");
1060 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1061 helper->valid_session_key = TRUE;
1062 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1063 if(!helper->session_key)
1065 TRACE("Failed to allocate memory for session key\n");
1066 ret = SEC_E_INTERNAL_ERROR;
1067 goto asc_end;
1069 memcpy(helper->session_key, bin, 16);
1072 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1073 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1074 helper->crypt.ntlm.seq_num = 0l;
1077 phNewContext->dwUpper = ctxt_attr;
1078 phNewContext->dwLower = (ULONG_PTR)helper;
1080 asc_end:
1081 HeapFree(GetProcessHeap(), 0, want_flags);
1082 HeapFree(GetProcessHeap(), 0, buffer);
1083 HeapFree(GetProcessHeap(), 0, bin);
1084 return ret;
1087 /***********************************************************************
1088 * CompleteAuthToken
1090 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1091 PSecBufferDesc pToken)
1093 /* We never need to call CompleteAuthToken anyway */
1094 TRACE("%p %p\n", phContext, pToken);
1095 if (!phContext)
1096 return SEC_E_INVALID_HANDLE;
1098 return SEC_E_OK;
1101 /***********************************************************************
1102 * DeleteSecurityContext
1104 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1106 PNegoHelper helper;
1108 TRACE("%p\n", phContext);
1109 if (!phContext)
1110 return SEC_E_INVALID_HANDLE;
1112 helper = (PNegoHelper)phContext->dwLower;
1114 phContext->dwUpper = 0;
1115 phContext->dwLower = 0;
1117 SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1118 HeapFree(GetProcessHeap(), 0, helper->session_key);
1119 helper->valid_session_key = FALSE;
1120 SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1121 SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1122 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
1123 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
1124 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
1125 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
1127 return SEC_E_OK;
1130 /***********************************************************************
1131 * QueryContextAttributesW
1133 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1134 ULONG ulAttribute, void *pBuffer)
1136 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1137 if (!phContext)
1138 return SEC_E_INVALID_HANDLE;
1140 switch(ulAttribute)
1142 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1143 _x(SECPKG_ATTR_ACCESS_TOKEN);
1144 _x(SECPKG_ATTR_AUTHORITY);
1145 _x(SECPKG_ATTR_DCE_INFO);
1146 case SECPKG_ATTR_FLAGS:
1148 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1149 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1151 spcf->Flags = 0;
1152 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1153 spcf->Flags |= ISC_RET_INTEGRITY;
1154 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1155 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1156 return SEC_E_OK;
1158 _x(SECPKG_ATTR_KEY_INFO);
1159 _x(SECPKG_ATTR_LIFESPAN);
1160 _x(SECPKG_ATTR_NAMES);
1161 _x(SECPKG_ATTR_NATIVE_NAMES);
1162 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1163 _x(SECPKG_ATTR_PACKAGE_INFO);
1164 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1165 _x(SECPKG_ATTR_SESSION_KEY);
1166 case SECPKG_ATTR_SIZES:
1168 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1169 spcs->cbMaxToken = NTLM_MAX_BUF;
1170 spcs->cbMaxSignature = 16;
1171 spcs->cbBlockSize = 0;
1172 spcs->cbSecurityTrailer = 16;
1173 return SEC_E_OK;
1175 _x(SECPKG_ATTR_STREAM_SIZES);
1176 _x(SECPKG_ATTR_TARGET_INFORMATION);
1177 #undef _x
1178 default:
1179 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1182 return SEC_E_UNSUPPORTED_FUNCTION;
1185 /***********************************************************************
1186 * QueryContextAttributesA
1188 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1189 ULONG ulAttribute, void *pBuffer)
1191 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1194 /***********************************************************************
1195 * ImpersonateSecurityContext
1197 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1199 SECURITY_STATUS ret;
1201 TRACE("%p\n", phContext);
1202 if (phContext)
1204 ret = SEC_E_UNSUPPORTED_FUNCTION;
1206 else
1208 ret = SEC_E_INVALID_HANDLE;
1210 return ret;
1213 /***********************************************************************
1214 * RevertSecurityContext
1216 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1218 SECURITY_STATUS ret;
1220 TRACE("%p\n", phContext);
1221 if (phContext)
1223 ret = SEC_E_UNSUPPORTED_FUNCTION;
1225 else
1227 ret = SEC_E_INVALID_HANDLE;
1229 return ret;
1232 /*************************************************************************
1233 * ntlm_GetTokenBufferIndex
1234 * Calculates the index of the secbuffer with BufferType == SECBUFFER_TOKEN
1235 * Returns index if found or -1 if not found.
1237 static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)
1239 UINT i;
1241 TRACE("%p\n", pMessage);
1243 for( i = 0; i < pMessage->cBuffers; ++i )
1245 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1246 return i;
1249 return -1;
1252 /***********************************************************************
1253 * ntlm_CreateSignature
1254 * As both MakeSignature and VerifySignature need this, but different keys
1255 * are needed for NTLMv2, the logic goes into a helper function.
1256 * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1257 * signing/encrypting and NTLM_RECV for verfying/decrypting. When encrypting,
1258 * the signature is encrypted after the message was encrypted, so
1259 * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1260 * false.
1262 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1263 int token_idx, SignDirection direction, BOOL encrypt_sig)
1265 ULONG sign_version = 1;
1266 UINT i;
1267 PBYTE sig;
1268 TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1269 encrypt_sig);
1271 sig = pMessage->pBuffers[token_idx].pvBuffer;
1273 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1274 helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1276 BYTE digest[16];
1277 BYTE seq_no[4];
1278 HMAC_MD5_CTX hmac_md5_ctx;
1280 TRACE("Signing NTLM2 style\n");
1282 if(direction == NTLM_SEND)
1284 seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
1285 seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
1286 seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1287 seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1289 ++(helper->crypt.ntlm2.send_seq_no);
1291 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1293 else
1295 seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
1296 seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
1297 seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1298 seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1300 ++(helper->crypt.ntlm2.recv_seq_no);
1302 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1305 HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1306 for( i = 0; i < pMessage->cBuffers; ++i )
1308 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1309 HMACMD5Update(&hmac_md5_ctx, (BYTE *)pMessage->pBuffers[i].pvBuffer,
1310 pMessage->pBuffers[i].cbBuffer);
1313 HMACMD5Final(&hmac_md5_ctx, digest);
1315 if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1317 if(direction == NTLM_SEND)
1318 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1319 else
1320 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1323 /* The NTLM2 signature is the sign version */
1324 sig[ 0] = (sign_version >> 0) & 0xff;
1325 sig[ 1] = (sign_version >> 8) & 0xff;
1326 sig[ 2] = (sign_version >> 16) & 0xff;
1327 sig[ 3] = (sign_version >> 24) & 0xff;
1328 /* The first 8 bytes of the digest */
1329 memcpy(sig+4, digest, 8);
1330 /* And the sequence number */
1331 memcpy(sig+12, seq_no, 4);
1333 pMessage->pBuffers[token_idx].cbBuffer = 16;
1335 return SEC_E_OK;
1337 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1339 ULONG crc = 0U;
1340 TRACE("Signing NTLM1 style\n");
1342 for(i=0; i < pMessage->cBuffers; ++i)
1344 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1346 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1347 pMessage->pBuffers[i].cbBuffer, crc);
1351 sig[ 0] = (sign_version >> 0) & 0xff;
1352 sig[ 1] = (sign_version >> 8) & 0xff;
1353 sig[ 2] = (sign_version >> 16) & 0xff;
1354 sig[ 3] = (sign_version >> 24) & 0xff;
1355 memset(sig+4, 0, 4);
1356 sig[ 8] = (crc >> 0) & 0xff;
1357 sig[ 9] = (crc >> 8) & 0xff;
1358 sig[10] = (crc >> 16) & 0xff;
1359 sig[11] = (crc >> 24) & 0xff;
1360 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1361 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1362 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1363 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1365 ++(helper->crypt.ntlm.seq_num);
1367 if(encrypt_sig)
1368 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1369 return SEC_E_OK;
1372 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1374 TRACE("Creating a dummy signature.\n");
1375 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1376 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1377 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1378 pMessage->pBuffers[token_idx].cbBuffer = 16;
1379 return SEC_E_OK;
1382 return SEC_E_UNSUPPORTED_FUNCTION;
1385 /***********************************************************************
1386 * MakeSignature
1388 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1389 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1391 PNegoHelper helper;
1392 int token_idx;
1394 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1395 if (!phContext)
1396 return SEC_E_INVALID_HANDLE;
1398 if(fQOP)
1399 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1401 if(MessageSeqNo)
1402 FIXME("Ignoring MessageSeqNo\n");
1404 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1405 return SEC_E_INVALID_TOKEN;
1407 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1408 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1409 return SEC_E_INVALID_TOKEN;
1411 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1412 return SEC_E_BUFFER_TOO_SMALL;
1414 helper = (PNegoHelper)phContext->dwLower;
1415 TRACE("Negotiated flags are: 0x%08lx\n", helper->neg_flags);
1417 return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1420 /***********************************************************************
1421 * VerifySignature
1423 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1424 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1426 PNegoHelper helper;
1427 ULONG fQOP = 0;
1428 UINT i;
1429 int token_idx;
1430 SECURITY_STATUS ret;
1431 SecBufferDesc local_desc;
1432 PSecBuffer local_buff;
1433 BYTE local_sig[16];
1435 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1436 if(!phContext)
1437 return SEC_E_INVALID_HANDLE;
1439 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1440 return SEC_E_INVALID_TOKEN;
1442 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1443 return SEC_E_INVALID_TOKEN;
1445 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1446 return SEC_E_BUFFER_TOO_SMALL;
1448 if(MessageSeqNo)
1449 FIXME("Ignoring MessageSeqNo\n");
1451 helper = (PNegoHelper)phContext->dwLower;
1452 TRACE("Negotiated flags: 0x%08lx\n", helper->neg_flags);
1454 local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1456 local_desc.ulVersion = SECBUFFER_VERSION;
1457 local_desc.cBuffers = pMessage->cBuffers;
1458 local_desc.pBuffers = local_buff;
1460 for(i=0; i < pMessage->cBuffers; ++i)
1462 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1464 local_buff[i].BufferType = SECBUFFER_TOKEN;
1465 local_buff[i].cbBuffer = 16;
1466 local_buff[i].pvBuffer = local_sig;
1468 else
1470 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1471 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1472 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1476 if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1477 return ret;
1479 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1480 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1481 ret = SEC_E_MESSAGE_ALTERED;
1482 else
1483 ret = SEC_E_OK;
1485 HeapFree(GetProcessHeap(), 0, local_buff);
1486 pfQOP = &fQOP;
1488 return ret;
1492 /***********************************************************************
1493 * FreeCredentialsHandle
1495 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1496 PCredHandle phCredential)
1498 SECURITY_STATUS ret;
1500 if(phCredential){
1501 PNegoHelper helper = (PNegoHelper) phCredential->dwLower;
1502 phCredential->dwUpper = 0;
1503 phCredential->dwLower = 0;
1504 cleanup_helper(helper);
1505 ret = SEC_E_OK;
1507 else
1508 ret = SEC_E_OK;
1510 return ret;
1513 /***********************************************************************
1514 * EncryptMessage
1516 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1517 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1519 PNegoHelper helper;
1520 int token_idx;
1522 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1524 if(!phContext)
1525 return SEC_E_INVALID_HANDLE;
1527 if(fQOP)
1528 FIXME("Ignoring fQOP\n");
1530 if(MessageSeqNo)
1531 FIXME("Ignoring MessageSeqNo\n");
1533 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1534 return SEC_E_INVALID_TOKEN;
1536 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1537 return SEC_E_INVALID_TOKEN;
1539 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1540 return SEC_E_BUFFER_TOO_SMALL;
1542 helper = (PNegoHelper) phContext->dwLower;
1544 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1545 helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1547 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1548 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1549 (BYTE *)pMessage->pBuffers[1].pvBuffer,
1550 pMessage->pBuffers[1].cbBuffer);
1552 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1553 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1554 ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1557 return SEC_E_OK;
1559 else
1561 PBYTE sig;
1562 ULONG save_flags;
1564 /* EncryptMessage always produces real signatures, so make sure
1565 * NTLMSSP_NEGOTIATE_SIGN is set*/
1566 save_flags = helper->neg_flags;
1567 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1568 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1569 helper->neg_flags = save_flags;
1571 sig = pMessage->pBuffers[token_idx].pvBuffer;
1573 SECUR32_arc4Process(helper->crypt.ntlm.a4i, pMessage->pBuffers[1].pvBuffer,
1574 pMessage->pBuffers[1].cbBuffer);
1575 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1577 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1578 memset(sig+4, 0, 4);
1582 return SEC_E_OK;
1585 /***********************************************************************
1586 * DecryptMessage
1588 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1589 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1591 SECURITY_STATUS ret;
1592 ULONG ntlmssp_flags_save;
1593 PNegoHelper helper;
1594 int token_idx;
1595 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1597 if(!phContext)
1598 return SEC_E_INVALID_HANDLE;
1600 if(MessageSeqNo)
1601 FIXME("Ignoring MessageSeqNo\n");
1603 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1604 return SEC_E_INVALID_TOKEN;
1606 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1607 return SEC_E_INVALID_TOKEN;
1609 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1610 return SEC_E_BUFFER_TOO_SMALL;
1612 helper = (PNegoHelper) phContext->dwLower;
1614 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1616 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1617 pMessage->pBuffers[1].pvBuffer, pMessage->pBuffers[1].cbBuffer);
1619 else
1621 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1622 pMessage->pBuffers[1].pvBuffer, pMessage->pBuffers[1].cbBuffer);
1625 /* Make sure we use a session key for the signature check, EncryptMessage
1626 * always does that, even in the dummy case */
1627 ntlmssp_flags_save = helper->neg_flags;
1629 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1630 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1632 helper->neg_flags = ntlmssp_flags_save;
1634 return ret;
1637 static const SecurityFunctionTableA ntlmTableA = {
1639 NULL, /* EnumerateSecurityPackagesA */
1640 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1641 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1642 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1643 NULL, /* Reserved2 */
1644 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1645 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1646 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1647 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1648 NULL, /* ApplyControlToken */
1649 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1650 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1651 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1652 ntlm_MakeSignature, /* MakeSignature */
1653 ntlm_VerifySignature, /* VerifySignature */
1654 FreeContextBuffer, /* FreeContextBuffer */
1655 NULL, /* QuerySecurityPackageInfoA */
1656 NULL, /* Reserved3 */
1657 NULL, /* Reserved4 */
1658 NULL, /* ExportSecurityContext */
1659 NULL, /* ImportSecurityContextA */
1660 NULL, /* AddCredentialsA */
1661 NULL, /* Reserved8 */
1662 NULL, /* QuerySecurityContextToken */
1663 ntlm_EncryptMessage, /* EncryptMessage */
1664 ntlm_DecryptMessage, /* DecryptMessage */
1665 NULL, /* SetContextAttributesA */
1668 static const SecurityFunctionTableW ntlmTableW = {
1670 NULL, /* EnumerateSecurityPackagesW */
1671 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1672 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1673 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1674 NULL, /* Reserved2 */
1675 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1676 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1677 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1678 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1679 NULL, /* ApplyControlToken */
1680 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1681 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1682 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1683 ntlm_MakeSignature, /* MakeSignature */
1684 ntlm_VerifySignature, /* VerifySignature */
1685 FreeContextBuffer, /* FreeContextBuffer */
1686 NULL, /* QuerySecurityPackageInfoW */
1687 NULL, /* Reserved3 */
1688 NULL, /* Reserved4 */
1689 NULL, /* ExportSecurityContext */
1690 NULL, /* ImportSecurityContextW */
1691 NULL, /* AddCredentialsW */
1692 NULL, /* Reserved8 */
1693 NULL, /* QuerySecurityContextToken */
1694 ntlm_EncryptMessage, /* EncryptMessage */
1695 ntlm_DecryptMessage, /* DecryptMessage */
1696 NULL, /* SetContextAttributesW */
1699 #define NTLM_COMMENT \
1700 { 'N', 'T', 'L', 'M', ' ', \
1701 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1702 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1704 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1705 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1707 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1709 static char ntlm_name_A[] = NTLM_NAME;
1710 static WCHAR ntlm_name_W[] = NTLM_NAME;
1712 /* According to Windows, NTLM has the following capabilities. */
1713 #define CAPS ( \
1714 SECPKG_FLAG_INTEGRITY | \
1715 SECPKG_FLAG_PRIVACY | \
1716 SECPKG_FLAG_TOKEN_ONLY | \
1717 SECPKG_FLAG_CONNECTION | \
1718 SECPKG_FLAG_MULTI_REQUIRED | \
1719 SECPKG_FLAG_IMPERSONATION | \
1720 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1721 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1723 static const SecPkgInfoW infoW = {
1724 CAPS,
1726 RPC_C_AUTHN_WINNT,
1727 NTLM_MAX_BUF,
1728 ntlm_name_W,
1729 ntlm_comment_W
1732 static const SecPkgInfoA infoA = {
1733 CAPS,
1735 RPC_C_AUTHN_WINNT,
1736 NTLM_MAX_BUF,
1737 ntlm_name_A,
1738 ntlm_comment_A
1741 void SECUR32_initNTLMSP(void)
1743 SECURITY_STATUS ret;
1744 PNegoHelper helper;
1745 static CHAR version[] = "--version";
1747 SEC_CHAR *args[] = {
1748 ntlm_auth,
1749 version,
1750 NULL };
1752 strcpy(ntlm_auth, "ntlm_auth");
1754 if((ret = fork_helper(&helper, ntlm_auth, args)) != SEC_E_OK)
1756 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1757 helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1758 helper->major = helper->minor = helper->micro = -1;
1760 else
1761 check_version(helper);
1763 /* CodeWeavers feature: fall back to our shipped ntlm_auth if a suitable
1764 * system version isn't found */
1765 if( ((helper->major < MIN_NTLM_AUTH_MAJOR_VERSION) ||
1766 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1767 helper->minor < MIN_NTLM_AUTH_MINOR_VERSION) ||
1768 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1769 helper->minor == MIN_NTLM_AUTH_MINOR_VERSION &&
1770 helper->micro < MIN_NTLM_AUTH_MICRO_VERSION)) &&
1771 getenv("CX_ROOT") )
1773 cleanup_helper(helper);
1775 TRACE("falling back to CrossOver version of ntlm_auth\n");
1777 strcpy(ntlm_auth, getenv("CX_ROOT"));
1778 strcat(ntlm_auth, "/bin/cxntlm_auth");
1780 if((ret = fork_helper(&helper, ntlm_auth, args)) != SEC_E_OK)
1782 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1783 helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1784 helper->major = helper->minor = helper->micro = -1;
1786 else
1787 check_version(helper);
1790 if( (helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
1791 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1792 helper->minor > MIN_NTLM_AUTH_MINOR_VERSION) ||
1793 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1794 helper->minor == MIN_NTLM_AUTH_MINOR_VERSION &&
1795 helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION) )
1797 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1798 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1800 else
1802 ERR("%s was not found or is outdated. "
1803 "Make sure that ntlm_auth >= %d.%d.%d is in your path.\n",
1804 ntlm_auth,
1805 MIN_NTLM_AUTH_MAJOR_VERSION,
1806 MIN_NTLM_AUTH_MINOR_VERSION,
1807 MIN_NTLM_AUTH_MICRO_VERSION);
1809 cleanup_helper(helper);