secur32: Fix ntlm_auth version number check when registering the NTLM SSP.
[wine/gsoc_dplay.git] / dlls / secur32 / ntlm.c
blobaf5b3f0e4684f0c4967ce5116cde1c63924050ba
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 "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "rpc.h"
28 #include "sspi.h"
29 #include "lm.h"
30 #include "secur32_priv.h"
31 #include "hmac_md5.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
36 #define NTLM_MAX_BUF 1904
37 #define MIN_NTLM_AUTH_MAJOR_VERSION 3
38 #define MIN_NTLM_AUTH_MINOR_VERSION 0
39 #define MIN_NTLM_AUTH_MICRO_VERSION 25
41 static CHAR ntlm_auth[] = "ntlm_auth";
43 /***********************************************************************
44 * QueryCredentialsAttributesA
46 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
47 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
49 SECURITY_STATUS ret;
51 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
53 if(ulAttribute == SECPKG_ATTR_NAMES)
55 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
56 ret = SEC_E_UNSUPPORTED_FUNCTION;
58 else
59 ret = SEC_E_UNSUPPORTED_FUNCTION;
61 return ret;
64 /***********************************************************************
65 * QueryCredentialsAttributesW
67 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
68 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
70 SECURITY_STATUS ret;
72 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
74 if(ulAttribute == SECPKG_ATTR_NAMES)
76 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
77 ret = SEC_E_UNSUPPORTED_FUNCTION;
79 else
80 ret = SEC_E_UNSUPPORTED_FUNCTION;
82 return ret;
85 /***********************************************************************
86 * AcquireCredentialsHandleW
88 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
89 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
90 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
91 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
93 SECURITY_STATUS ret;
94 PNegoHelper helper = NULL;
95 static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp",
96 credentials_argv[] = "--use-cached-creds";
98 SEC_CHAR *client_user_arg = NULL;
99 SEC_CHAR *client_domain_arg = NULL;
100 SEC_WCHAR *username = NULL, *domain = NULL;
102 SEC_CHAR *client_argv[6];
103 SEC_CHAR *server_argv[] = { ntlm_auth,
104 server_helper_protocol,
105 NULL };
107 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
108 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
109 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
112 switch(fCredentialUse)
114 case SECPKG_CRED_INBOUND:
115 if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
116 SEC_E_OK)
118 phCredential = NULL;
119 break;
121 else
123 helper->mode = NTLM_SERVER;
124 phCredential->dwUpper = fCredentialUse;
125 phCredential->dwLower = (ULONG_PTR)helper;
127 ret = SEC_E_OK;
128 break;
129 case SECPKG_CRED_OUTBOUND:
131 static const char username_arg[] = "--username=";
132 static const char domain_arg[] = "--domain=";
133 static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
134 int unixcp_size;
136 if(pAuthData == NULL)
138 LPWKSTA_USER_INFO_1 ui = NULL;
139 NET_API_STATUS status;
141 status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
142 if (status != NERR_Success || ui == NULL)
144 ret = SEC_E_NO_CREDENTIALS;
145 phCredential = NULL;
146 break;
149 username = HeapAlloc(GetProcessHeap(), 0,
150 (lstrlenW(ui->wkui1_username)+1) *
151 sizeof(SEC_WCHAR));
152 lstrcpyW(username, ui->wkui1_username);
154 /* same for the domain */
155 domain = HeapAlloc(GetProcessHeap(), 0,
156 (lstrlenW(ui->wkui1_logon_domain)+1) *
157 sizeof(SEC_WCHAR));
158 lstrcpyW(domain, ui->wkui1_logon_domain);
159 NetApiBufferFree(ui);
161 else
163 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
164 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
166 if (!auth_data->UserLength || !auth_data->DomainLength)
168 ret = SEC_E_NO_CREDENTIALS;
169 phCredential = NULL;
170 break;
172 /* Get username and domain from pAuthData */
173 username = HeapAlloc(GetProcessHeap(), 0,
174 (auth_data->UserLength + 1) * sizeof(SEC_WCHAR));
175 lstrcpyW(username, auth_data->User);
177 domain = HeapAlloc(GetProcessHeap(), 0,
178 (auth_data->DomainLength + 1) * sizeof(SEC_WCHAR));
179 lstrcpyW(domain, auth_data->Domain);
181 TRACE("Username is %s\n", debugstr_w(username));
182 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
183 username, -1, NULL, 0, NULL, NULL) + sizeof(username_arg);
184 client_user_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
185 lstrcpyA(client_user_arg, username_arg);
186 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, username, -1,
187 client_user_arg + sizeof(username_arg) - 1,
188 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
190 TRACE("Domain name is %s\n", debugstr_w(domain));
191 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
192 domain, -1, NULL, 0, NULL, NULL) + sizeof(domain_arg);
193 client_domain_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
194 lstrcpyA(client_domain_arg, domain_arg);
195 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domain,
196 -1, client_domain_arg + sizeof(domain_arg) - 1,
197 unixcp_size - sizeof(domain) + 1, NULL, NULL);
199 client_argv[0] = ntlm_auth;
200 client_argv[1] = helper_protocol;
201 client_argv[2] = client_user_arg;
202 client_argv[3] = client_domain_arg;
203 client_argv[4] = credentials_argv;
204 client_argv[5] = NULL;
206 if((ret = fork_helper(&helper, ntlm_auth, client_argv)) !=
207 SEC_E_OK)
209 phCredential = NULL;
210 break;
212 else
214 helper->mode = NTLM_CLIENT;
216 if(pAuthData != NULL)
218 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
219 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
221 if(auth_data->PasswordLength != 0)
223 helper->pwlen = WideCharToMultiByte(CP_UNIXCP,
224 WC_NO_BEST_FIT_CHARS, auth_data->Password,
225 auth_data->PasswordLength+1, NULL, 0, NULL,
226 NULL) + 1;
228 helper->password = HeapAlloc(GetProcessHeap(), 0,
229 helper->pwlen);
231 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
232 auth_data->Password, auth_data->PasswordLength+1,
233 helper->password, helper->pwlen, NULL, NULL);
237 phCredential->dwUpper = fCredentialUse;
238 phCredential->dwLower = (ULONG_PTR)helper;
239 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
240 phCredential->dwUpper, phCredential->dwLower);
242 ret = SEC_E_OK;
243 break;
245 case SECPKG_CRED_BOTH:
246 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
247 ret = SEC_E_UNSUPPORTED_FUNCTION;
248 phCredential = NULL;
249 break;
250 default:
251 phCredential = NULL;
252 ret = SEC_E_UNKNOWN_CREDENTIALS;
256 HeapFree(GetProcessHeap(), 0, client_user_arg);
257 HeapFree(GetProcessHeap(), 0, client_domain_arg);
258 HeapFree(GetProcessHeap(), 0, username);
259 HeapFree(GetProcessHeap(), 0, domain);
261 return ret;
264 /***********************************************************************
265 * AcquireCredentialsHandleA
267 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
268 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
269 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
270 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
272 SECURITY_STATUS ret;
273 int user_sizeW, domain_sizeW, passwd_sizeW;
275 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
277 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
278 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
280 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
281 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
282 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
284 if(pszPackage != NULL)
286 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
287 NULL, 0);
289 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
290 sizeof(SEC_WCHAR));
291 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
295 if(pAuthData != NULL)
297 identity = (PSEC_WINNT_AUTH_IDENTITY_A)pAuthData;
299 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
301 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
302 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
304 if(identity->UserLength != 0)
306 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
307 (LPCSTR)identity->User, identity->UserLength+1, NULL, 0);
308 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
309 sizeof(SEC_WCHAR));
310 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
311 identity->UserLength+1, user, user_sizeW);
313 else
315 user_sizeW = 0;
318 if(identity->DomainLength != 0)
320 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
321 (LPCSTR)identity->Domain, identity->DomainLength+1, NULL, 0);
322 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
323 * sizeof(SEC_WCHAR));
324 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
325 identity->DomainLength+1, domain, domain_sizeW);
327 else
329 domain_sizeW = 0;
332 if(identity->PasswordLength != 0)
334 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
335 (LPCSTR)identity->Password, identity->PasswordLength,
336 NULL, 0);
337 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
338 * sizeof(SEC_WCHAR));
339 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
340 identity->PasswordLength, passwd, passwd_sizeW);
342 else
344 passwd_sizeW = 0;
347 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
348 pAuthDataW->User = user;
349 pAuthDataW->UserLength = user_sizeW;
350 pAuthDataW->Domain = domain;
351 pAuthDataW->DomainLength = domain_sizeW;
352 pAuthDataW->Password = passwd;
353 pAuthDataW->PasswordLength = passwd_sizeW;
355 else
357 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
361 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
362 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
363 ptsExpiry);
365 HeapFree(GetProcessHeap(), 0, package);
366 HeapFree(GetProcessHeap(), 0, user);
367 HeapFree(GetProcessHeap(), 0, domain);
368 HeapFree(GetProcessHeap(), 0, passwd);
369 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
370 HeapFree(GetProcessHeap(), 0, pAuthDataW);
372 return ret;
375 /***********************************************************************
376 * InitializeSecurityContextW
378 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
379 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
380 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
381 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
382 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
384 SECURITY_STATUS ret;
385 PNegoHelper helper;
386 ULONG ctxt_attr = 0;
387 char* buffer, *want_flags = NULL;
388 PBYTE bin;
389 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
391 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
392 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
393 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
395 if(!phCredential)
396 return SEC_E_INVALID_HANDLE;
398 /* As the server side of sspi never calls this, make sure that
399 * the handler is a client handler.
401 helper = (PNegoHelper)phCredential->dwLower;
402 if(helper->mode != NTLM_CLIENT)
404 TRACE("Helper mode = %d\n", helper->mode);
405 return SEC_E_INVALID_HANDLE;
408 /****************************************
409 * When communicating with the client, there can be the
410 * following reply packets:
411 * YR <base64 blob> should be sent to the server
412 * PW should be sent back to helper with
413 * base64 encoded password
414 * AF <base64 blob> client is done, blob should be
415 * sent to server with KK prefixed
416 * GF <string list> A string list of negotiated flags
417 * GK <base64 blob> base64 encoded session key
418 * BH <char reason> something broke
420 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
422 if (pszTargetName)
424 TRACE("According to a MS whitepaper pszTargetName is ignored.\n");
427 if(TargetDataRep == SECURITY_NETWORK_DREP){
428 TRACE("Setting SECURITY_NETWORK_DREP\n");
431 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
432 bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
434 if((phContext == NULL) && (pInput == NULL))
436 TRACE("First time in ISC()\n");
437 /* Allocate space for a maximal string of
438 * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
439 * NTLMSSP_FEATURE_SESSION_KEY"
441 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
442 if(want_flags == NULL)
444 ret = SEC_E_INSUFFICIENT_MEMORY;
445 goto isc_end;
447 lstrcpyA(want_flags, "SF");
448 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
450 char *ptr;
451 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SEAL")) == NULL)
452 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
454 if(fContextReq & ISC_REQ_CONNECTION)
455 ctxt_attr |= ISC_RET_CONNECTION;
456 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
457 ctxt_attr |= ISC_RET_EXTENDED_ERROR;
458 if(fContextReq & ISC_REQ_INTEGRITY)
460 char *ptr;
461 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
462 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
464 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
465 ctxt_attr |= ISC_RET_MUTUAL_AUTH;
466 if(fContextReq & ISC_REQ_REPLAY_DETECT)
468 char *ptr;
469 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
470 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
472 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
474 char *ptr;
475 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
476 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
478 if(fContextReq & ISC_REQ_STREAM)
479 FIXME("ISC_REQ_STREAM\n");
480 if(fContextReq & ISC_REQ_USE_DCE_STYLE)
481 ctxt_attr |= ISC_RET_USED_DCE_STYLE;
482 if(fContextReq & ISC_REQ_DELEGATE)
483 ctxt_attr |= ISC_RET_DELEGATE;
485 /* If no password is given, try to use cached credentials. Fall back to an empty
486 * password if this failed. */
487 if(helper->password == NULL)
489 lstrcpynA(buffer, "OK", max_len-1);
490 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
491 goto isc_end;
492 /* If the helper replied with "PW", using cached credentials failed */
493 if(!strncmp(buffer, "PW", 2))
495 TRACE("Using cached credentials failed. Using empty password.\n");
496 lstrcpynA(buffer, "PW AA==", max_len-1);
498 else /* Just do a noop on the next run */
499 lstrcpynA(buffer, "OK", max_len-1);
501 else
503 lstrcpynA(buffer, "PW ", max_len-1);
504 if((ret = encodeBase64((unsigned char*)helper->password,
505 helper->pwlen-2, buffer+3,
506 max_len-3, &buffer_len)) != SEC_E_OK)
508 TRACE("Deleting password!\n");
509 memset(helper->password, 0, helper->pwlen-2);
510 HeapFree(GetProcessHeap(), 0, helper->password);
511 goto isc_end;
516 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
517 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
518 goto isc_end;
520 TRACE("Helper returned %s\n", debugstr_a(buffer));
522 if(lstrlenA(want_flags) > 2)
524 TRACE("Want flags are %s\n", debugstr_a(want_flags));
525 lstrcpynA(buffer, want_flags, max_len-1);
526 if((ret = run_helper(helper, buffer, max_len, &buffer_len))
527 != SEC_E_OK)
528 goto isc_end;
529 if(!strncmp(buffer, "BH", 2))
530 ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
533 lstrcpynA(buffer, "YR", max_len-1);
535 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
536 goto isc_end;
538 TRACE("%s\n", buffer);
540 if(strncmp(buffer, "YR ", 3) != 0)
542 /* Something borked */
543 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
544 ret = SEC_E_INTERNAL_ERROR;
545 goto isc_end;
547 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
548 max_len-1, &bin_len)) != SEC_E_OK)
549 goto isc_end;
551 /* put the decoded client blob into the out buffer */
553 ret = SEC_I_CONTINUE_NEEDED;
555 else
557 /* handle second call here */
558 /* encode server data to base64 */
559 if (!pInput || !pInput->cBuffers)
561 ret = SEC_E_INCOMPLETE_MESSAGE;
562 goto isc_end;
565 if (!pInput->pBuffers[0].pvBuffer)
567 ret = SEC_E_INTERNAL_ERROR;
568 goto isc_end;
571 if(pInput->pBuffers[0].cbBuffer > max_len)
573 TRACE("pInput->pBuffers[0].cbBuffer is: %ld\n",
574 pInput->pBuffers[0].cbBuffer);
575 ret = SEC_E_INVALID_TOKEN;
576 goto isc_end;
578 else
579 bin_len = pInput->pBuffers[0].cbBuffer;
581 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
583 lstrcpynA(buffer, "TT ", max_len-1);
585 if((ret = encodeBase64(bin, bin_len, buffer+3,
586 max_len-3, &buffer_len)) != SEC_E_OK)
587 goto isc_end;
589 TRACE("Server sent: %s\n", debugstr_a(buffer));
591 /* send TT base64 blob to ntlm_auth */
592 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
593 goto isc_end;
595 TRACE("Helper replied: %s\n", debugstr_a(buffer));
597 if( (strncmp(buffer, "KK ", 3) != 0) &&
598 (strncmp(buffer, "AF ", 3) !=0))
600 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
601 ret = SEC_E_INVALID_TOKEN;
602 goto isc_end;
605 /* decode the blob and send it to server */
606 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
607 &bin_len)) != SEC_E_OK)
609 goto isc_end;
612 phNewContext->dwUpper = ctxt_attr;
613 phNewContext->dwLower = (ULONG_PTR)helper;
615 ret = SEC_E_OK;
618 /* put the decoded client blob into the out buffer */
620 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
622 if (pOutput)
624 pOutput->cBuffers = 1;
625 pOutput->pBuffers[0].pvBuffer = SECUR32_ALLOC(bin_len);
626 pOutput->pBuffers[0].cbBuffer = bin_len;
630 if (!pOutput || !pOutput->cBuffers || pOutput->pBuffers[0].cbBuffer < bin_len)
632 TRACE("out buffer is NULL or has not enough space\n");
633 ret = SEC_E_BUFFER_TOO_SMALL;
634 goto isc_end;
637 if (!pOutput->pBuffers[0].pvBuffer)
639 TRACE("out buffer is NULL\n");
640 ret = SEC_E_INTERNAL_ERROR;
641 goto isc_end;
644 pOutput->pBuffers[0].cbBuffer = bin_len;
645 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
646 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
648 if(ret == SEC_E_OK)
650 TRACE("Getting negotiated flags\n");
651 lstrcpynA(buffer, "GF", max_len - 1);
652 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
653 goto isc_end;
655 if(buffer_len < 3)
657 TRACE("No flags negotiated.\n");
658 helper->neg_flags = 0l;
660 else
662 TRACE("Negotiated %s\n", debugstr_a(buffer));
663 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
664 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
667 TRACE("Getting session key\n");
668 lstrcpynA(buffer, "GK", max_len - 1);
669 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
670 goto isc_end;
672 if(strncmp(buffer, "BH", 2) == 0)
674 TRACE("No key negotiated.\n");
675 helper->valid_session_key = FALSE;
676 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
677 /*Generate the dummy session key = MD4(MD4(password))*/
678 if(helper->password)
680 SEC_WCHAR *unicode_password;
681 int passwd_lenW;
683 TRACE("Converting password to unicode.\n");
684 passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
685 (LPCSTR)helper->password, helper->pwlen,
686 NULL, 0);
687 unicode_password = HeapAlloc(GetProcessHeap(), 0,
688 passwd_lenW * sizeof(SEC_WCHAR));
689 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)helper->password,
690 helper->pwlen, unicode_password, passwd_lenW);
692 SECUR32_CreateNTLMv1SessionKey((PBYTE)unicode_password,
693 lstrlenW(unicode_password) * sizeof(SEC_WCHAR), helper->session_key);
695 HeapFree(GetProcessHeap(), 0, unicode_password);
697 else
698 memset(helper->session_key, 0, 16);
700 else if(strncmp(buffer, "GK ", 3) == 0)
702 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
703 &bin_len)) != SEC_E_OK)
705 TRACE("Failed to decode session key\n");
707 TRACE("Session key is %s\n", debugstr_a(buffer+3));
708 helper->valid_session_key = TRUE;
709 helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
710 if(!helper->session_key)
712 TRACE("Failed to allocate memory for session key\n");
713 ret = SEC_E_INTERNAL_ERROR;
714 goto isc_end;
716 memcpy(helper->session_key, bin, bin_len);
719 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
720 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
721 helper->crypt.ntlm.seq_num = 0l;
722 SECUR32_CreateNTLMv2SubKeys(helper);
723 helper->crypt.ntlm2.send_a4i = SECUR32_arc4Alloc();
724 helper->crypt.ntlm2.recv_a4i = SECUR32_arc4Alloc();
725 SECUR32_arc4Init(helper->crypt.ntlm2.send_a4i,
726 (BYTE *)helper->crypt.ntlm2.send_seal_key, 16);
727 SECUR32_arc4Init(helper->crypt.ntlm2.recv_a4i,
728 (BYTE *)helper->crypt.ntlm2.recv_seal_key, 16);
729 helper->crypt.ntlm2.send_seq_no = 0l;
730 helper->crypt.ntlm2.recv_seq_no = 0l;
733 if(ret != SEC_I_CONTINUE_NEEDED)
735 TRACE("Deleting password!\n");
736 if(helper->password)
737 memset(helper->password, 0, helper->pwlen-2);
738 HeapFree(GetProcessHeap(), 0, helper->password);
740 isc_end:
741 HeapFree(GetProcessHeap(), 0, want_flags);
742 HeapFree(GetProcessHeap(), 0, buffer);
743 HeapFree(GetProcessHeap(), 0, bin);
744 return ret;
747 /***********************************************************************
748 * InitializeSecurityContextA
750 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
751 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
752 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
753 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
754 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
756 SECURITY_STATUS ret;
758 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
759 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
760 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
762 if (phCredential)
764 SEC_WCHAR *target = NULL;
765 if(pszTargetName != NULL)
767 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
768 strlen(pszTargetName)+1, NULL, 0);
769 target = HeapAlloc(GetProcessHeap(), 0, target_size *
770 sizeof(SEC_WCHAR));
771 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
772 target, target_size);
775 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
776 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
777 phNewContext, pOutput, pfContextAttr, ptsExpiry);
779 HeapFree(GetProcessHeap(), 0, target);
781 else
783 ret = SEC_E_INVALID_HANDLE;
785 return ret;
788 /***********************************************************************
789 * AcceptSecurityContext
791 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
792 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
793 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
794 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
796 SECURITY_STATUS ret;
797 char *buffer, *want_flags = NULL;
798 PBYTE bin;
799 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
800 ULONG ctxt_attr = 0;
801 PNegoHelper helper;
803 TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
804 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
805 ptsExpiry);
807 if (!phCredential)
808 return SEC_E_INVALID_HANDLE;
810 helper = (PNegoHelper)phCredential->dwLower;
812 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
813 bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
815 if(helper->mode != NTLM_SERVER)
817 ret = SEC_E_INVALID_HANDLE;
818 goto asc_end;
821 if(TargetDataRep == SECURITY_NETWORK_DREP){
822 TRACE("Using SECURITY_NETWORK_DREP\n");
825 if(phContext == NULL)
827 /* This is the first call to AcceptSecurityHandle */
828 if(pInput == NULL)
830 ret = SEC_E_INCOMPLETE_MESSAGE;
831 goto asc_end;
834 if(pInput->cBuffers < 1)
836 ret = SEC_E_INCOMPLETE_MESSAGE;
837 goto asc_end;
840 if(pInput->pBuffers[0].cbBuffer > max_len)
842 ret = SEC_E_INVALID_TOKEN;
843 goto asc_end;
845 else
846 bin_len = pInput->pBuffers[0].cbBuffer;
848 /* Handle all the flags */
849 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
850 if(want_flags == NULL)
852 TRACE("Failed to allocate memory for the want_flags!\n");
853 ret = SEC_E_INSUFFICIENT_MEMORY;
854 goto asc_end;
856 lstrcpyA(want_flags, "SF");
857 if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
859 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
861 if(fContextReq & ASC_REQ_CONFIDENTIALITY)
863 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
865 if(fContextReq & ASC_REQ_CONNECTION)
867 /* This is default, so we'll enable it */
868 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
869 ctxt_attr |= ASC_RET_CONNECTION;
871 if(fContextReq & ASC_REQ_EXTENDED_ERROR)
873 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
875 if(fContextReq & ASC_REQ_INTEGRITY)
877 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
879 if(fContextReq & ASC_REQ_MUTUAL_AUTH)
881 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
883 if(fContextReq & ASC_REQ_REPLAY_DETECT)
885 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
887 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
889 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
891 if(fContextReq & ISC_REQ_STREAM)
893 FIXME("ASC_REQ_STREAM stub\n");
895 /* Done with the flags */
897 if(lstrlenA(want_flags) > 3)
899 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
900 lstrcpynA(buffer, want_flags, max_len - 1);
901 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
902 SEC_E_OK)
903 goto asc_end;
904 if(!strncmp(buffer, "BH", 2))
905 TRACE("Helper doesn't understand new command set\n");
908 /* This is the YR request from the client, encode to base64 */
910 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
912 lstrcpynA(buffer, "YR ", max_len-1);
914 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
915 &buffer_len)) != SEC_E_OK)
917 goto asc_end;
920 TRACE("Client sent: %s\n", debugstr_a(buffer));
922 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
923 SEC_E_OK)
925 goto asc_end;
928 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
929 /* The expected answer is TT <base64 blob> */
931 if(strncmp(buffer, "TT ", 3) != 0)
933 ret = SEC_E_INTERNAL_ERROR;
934 goto asc_end;
937 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
938 &bin_len)) != SEC_E_OK)
940 goto asc_end;
943 /* send this to the client */
944 if(pOutput == NULL)
946 ret = SEC_E_INSUFFICIENT_MEMORY;
947 goto asc_end;
950 if(pOutput->cBuffers < 1)
952 ret = SEC_E_INSUFFICIENT_MEMORY;
953 goto asc_end;
956 pOutput->pBuffers[0].cbBuffer = bin_len;
957 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
958 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
959 ret = SEC_I_CONTINUE_NEEDED;
962 else
964 /* we expect a KK request from client */
965 if(pInput == NULL)
967 ret = SEC_E_INCOMPLETE_MESSAGE;
968 goto asc_end;
971 if(pInput->cBuffers < 1)
973 ret = SEC_E_INCOMPLETE_MESSAGE;
974 goto asc_end;
977 if(pInput->pBuffers[0].cbBuffer > max_len)
979 ret = SEC_E_INVALID_TOKEN;
980 goto asc_end;
982 else
983 bin_len = pInput->pBuffers[0].cbBuffer;
985 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
987 lstrcpynA(buffer, "KK ", max_len-1);
989 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
990 &buffer_len)) != SEC_E_OK)
992 goto asc_end;
995 TRACE("Client sent: %s\n", debugstr_a(buffer));
997 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
998 SEC_E_OK)
1000 goto asc_end;
1003 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1005 if(strncmp(buffer, "AF ", 3) != 0)
1007 if(strncmp(buffer, "NA ", 3) == 0)
1009 ret = SEC_E_LOGON_DENIED;
1010 goto asc_end;
1012 else
1014 ret = SEC_E_INTERNAL_ERROR;
1015 goto asc_end;
1018 pOutput->pBuffers[0].cbBuffer = 0;
1019 ret = SEC_E_OK;
1021 TRACE("Getting negotiated flags\n");
1022 lstrcpynA(buffer, "GF", max_len - 1);
1023 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1024 goto asc_end;
1026 if(buffer_len < 3)
1028 TRACE("No flags negotiated, or helper does not support GF command\n");
1030 else
1032 TRACE("Negotiated %s\n", debugstr_a(buffer));
1033 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
1034 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
1037 TRACE("Getting session key\n");
1038 lstrcpynA(buffer, "GK", max_len - 1);
1039 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1040 goto asc_end;
1042 if(buffer_len < 3)
1043 TRACE("Helper does not support GK command\n");
1044 else
1046 if(strncmp(buffer, "BH ", 3) == 0)
1048 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1049 helper->valid_session_key = FALSE;
1050 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1051 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1052 memset(helper->session_key, 0 , 16);
1054 else if(strncmp(buffer, "GK ", 3) == 0)
1056 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1057 &bin_len)) != SEC_E_OK)
1059 TRACE("Failed to decode session key\n");
1061 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1062 helper->valid_session_key = TRUE;
1063 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1064 if(!helper->session_key)
1066 TRACE("Failed to allocate memory for session key\n");
1067 ret = SEC_E_INTERNAL_ERROR;
1068 goto asc_end;
1070 memcpy(helper->session_key, bin, 16);
1073 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1074 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1075 helper->crypt.ntlm.seq_num = 0l;
1078 phNewContext->dwUpper = ctxt_attr;
1079 phNewContext->dwLower = (ULONG_PTR)helper;
1081 asc_end:
1082 HeapFree(GetProcessHeap(), 0, want_flags);
1083 HeapFree(GetProcessHeap(), 0, buffer);
1084 HeapFree(GetProcessHeap(), 0, bin);
1085 return ret;
1088 /***********************************************************************
1089 * CompleteAuthToken
1091 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1092 PSecBufferDesc pToken)
1094 /* We never need to call CompleteAuthToken anyway */
1095 TRACE("%p %p\n", phContext, pToken);
1096 if (!phContext)
1097 return SEC_E_INVALID_HANDLE;
1099 return SEC_E_OK;
1102 /***********************************************************************
1103 * DeleteSecurityContext
1105 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1107 PNegoHelper helper;
1109 TRACE("%p\n", phContext);
1110 if (!phContext)
1111 return SEC_E_INVALID_HANDLE;
1113 helper = (PNegoHelper)phContext->dwLower;
1115 phContext->dwUpper = 0;
1116 phContext->dwLower = 0;
1118 SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1119 HeapFree(GetProcessHeap(), 0, helper->session_key);
1120 helper->valid_session_key = FALSE;
1121 SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1122 SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1123 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
1124 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
1125 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
1126 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
1128 return SEC_E_OK;
1131 /***********************************************************************
1132 * QueryContextAttributesW
1134 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1135 ULONG ulAttribute, void *pBuffer)
1137 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1138 if (!phContext)
1139 return SEC_E_INVALID_HANDLE;
1141 switch(ulAttribute)
1143 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1144 _x(SECPKG_ATTR_ACCESS_TOKEN);
1145 _x(SECPKG_ATTR_AUTHORITY);
1146 _x(SECPKG_ATTR_DCE_INFO);
1147 case SECPKG_ATTR_FLAGS:
1149 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1150 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1152 spcf->Flags = 0;
1153 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1154 spcf->Flags |= ISC_RET_INTEGRITY;
1155 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1156 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1157 return SEC_E_OK;
1159 _x(SECPKG_ATTR_KEY_INFO);
1160 _x(SECPKG_ATTR_LIFESPAN);
1161 _x(SECPKG_ATTR_NAMES);
1162 _x(SECPKG_ATTR_NATIVE_NAMES);
1163 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1164 _x(SECPKG_ATTR_PACKAGE_INFO);
1165 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1166 _x(SECPKG_ATTR_SESSION_KEY);
1167 case SECPKG_ATTR_SIZES:
1169 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1170 spcs->cbMaxToken = NTLM_MAX_BUF;
1171 spcs->cbMaxSignature = 16;
1172 spcs->cbBlockSize = 0;
1173 spcs->cbSecurityTrailer = 16;
1174 return SEC_E_OK;
1176 _x(SECPKG_ATTR_STREAM_SIZES);
1177 _x(SECPKG_ATTR_TARGET_INFORMATION);
1178 #undef _x
1179 default:
1180 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1183 return SEC_E_UNSUPPORTED_FUNCTION;
1186 /***********************************************************************
1187 * QueryContextAttributesA
1189 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1190 ULONG ulAttribute, void *pBuffer)
1192 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1195 /***********************************************************************
1196 * ImpersonateSecurityContext
1198 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1200 SECURITY_STATUS ret;
1202 TRACE("%p\n", phContext);
1203 if (phContext)
1205 ret = SEC_E_UNSUPPORTED_FUNCTION;
1207 else
1209 ret = SEC_E_INVALID_HANDLE;
1211 return ret;
1214 /***********************************************************************
1215 * RevertSecurityContext
1217 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1219 SECURITY_STATUS ret;
1221 TRACE("%p\n", phContext);
1222 if (phContext)
1224 ret = SEC_E_UNSUPPORTED_FUNCTION;
1226 else
1228 ret = SEC_E_INVALID_HANDLE;
1230 return ret;
1233 /*************************************************************************
1234 * ntlm_GetTokenBufferIndex
1235 * Calculates the index of the secbuffer with BufferType == SECBUFFER_TOKEN
1236 * Returns index if found or -1 if not found.
1238 static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)
1240 UINT i;
1242 TRACE("%p\n", pMessage);
1244 for( i = 0; i < pMessage->cBuffers; ++i )
1246 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1247 return i;
1250 return -1;
1253 /***********************************************************************
1254 * ntlm_CreateSignature
1255 * As both MakeSignature and VerifySignature need this, but different keys
1256 * are needed for NTLMv2, the logic goes into a helper function.
1257 * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1258 * signing/encrypting and NTLM_RECV for verfying/decrypting. When encrypting,
1259 * the signature is encrypted after the message was encrypted, so
1260 * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1261 * false.
1263 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1264 int token_idx, SignDirection direction, BOOL encrypt_sig)
1266 ULONG sign_version = 1;
1267 UINT i;
1268 PBYTE sig;
1269 TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1270 encrypt_sig);
1272 sig = pMessage->pBuffers[token_idx].pvBuffer;
1274 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1275 helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1277 BYTE digest[16];
1278 BYTE seq_no[4];
1279 HMAC_MD5_CTX hmac_md5_ctx;
1281 TRACE("Signing NTLM2 style\n");
1283 if(direction == NTLM_SEND)
1285 seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
1286 seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
1287 seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1288 seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1290 ++(helper->crypt.ntlm2.send_seq_no);
1292 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1294 else
1296 seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
1297 seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
1298 seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1299 seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1301 ++(helper->crypt.ntlm2.recv_seq_no);
1303 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1306 HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1307 for( i = 0; i < pMessage->cBuffers; ++i )
1309 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1310 HMACMD5Update(&hmac_md5_ctx, (BYTE *)pMessage->pBuffers[i].pvBuffer,
1311 pMessage->pBuffers[i].cbBuffer);
1314 HMACMD5Final(&hmac_md5_ctx, digest);
1316 if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1318 if(direction == NTLM_SEND)
1319 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1320 else
1321 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1324 /* The NTLM2 signature is the sign version */
1325 sig[ 0] = (sign_version >> 0) & 0xff;
1326 sig[ 1] = (sign_version >> 8) & 0xff;
1327 sig[ 2] = (sign_version >> 16) & 0xff;
1328 sig[ 3] = (sign_version >> 24) & 0xff;
1329 /* The first 8 bytes of the digest */
1330 memcpy(sig+4, digest, 8);
1331 /* And the sequence number */
1332 memcpy(sig+12, seq_no, 4);
1334 pMessage->pBuffers[token_idx].cbBuffer = 16;
1336 return SEC_E_OK;
1338 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1340 ULONG crc = 0U;
1341 TRACE("Signing NTLM1 style\n");
1343 for(i=0; i < pMessage->cBuffers; ++i)
1345 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1347 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1348 pMessage->pBuffers[i].cbBuffer, crc);
1352 sig[ 0] = (sign_version >> 0) & 0xff;
1353 sig[ 1] = (sign_version >> 8) & 0xff;
1354 sig[ 2] = (sign_version >> 16) & 0xff;
1355 sig[ 3] = (sign_version >> 24) & 0xff;
1356 memset(sig+4, 0, 4);
1357 sig[ 8] = (crc >> 0) & 0xff;
1358 sig[ 9] = (crc >> 8) & 0xff;
1359 sig[10] = (crc >> 16) & 0xff;
1360 sig[11] = (crc >> 24) & 0xff;
1361 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1362 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1363 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1364 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1366 ++(helper->crypt.ntlm.seq_num);
1368 if(encrypt_sig)
1369 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1370 return SEC_E_OK;
1373 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1375 TRACE("Creating a dummy signature.\n");
1376 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1377 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1378 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1379 pMessage->pBuffers[token_idx].cbBuffer = 16;
1380 return SEC_E_OK;
1383 return SEC_E_UNSUPPORTED_FUNCTION;
1386 /***********************************************************************
1387 * MakeSignature
1389 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1390 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1392 PNegoHelper helper;
1393 int token_idx;
1395 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1396 if (!phContext)
1397 return SEC_E_INVALID_HANDLE;
1399 if(fQOP)
1400 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1402 if(MessageSeqNo)
1403 FIXME("Ignoring MessageSeqNo\n");
1405 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1406 return SEC_E_INVALID_TOKEN;
1408 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1409 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1410 return SEC_E_INVALID_TOKEN;
1412 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1413 return SEC_E_BUFFER_TOO_SMALL;
1415 helper = (PNegoHelper)phContext->dwLower;
1416 TRACE("Negotiated flags are: 0x%08lx\n", helper->neg_flags);
1418 return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1421 /***********************************************************************
1422 * VerifySignature
1424 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1425 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1427 PNegoHelper helper;
1428 ULONG fQOP = 0;
1429 UINT i;
1430 int token_idx;
1431 SECURITY_STATUS ret;
1432 SecBufferDesc local_desc;
1433 PSecBuffer local_buff;
1434 BYTE local_sig[16];
1436 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1437 if(!phContext)
1438 return SEC_E_INVALID_HANDLE;
1440 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1441 return SEC_E_INVALID_TOKEN;
1443 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1444 return SEC_E_INVALID_TOKEN;
1446 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1447 return SEC_E_BUFFER_TOO_SMALL;
1449 if(MessageSeqNo)
1450 FIXME("Ignoring MessageSeqNo\n");
1452 helper = (PNegoHelper)phContext->dwLower;
1453 TRACE("Negotiated flags: 0x%08lx\n", helper->neg_flags);
1455 local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1457 local_desc.ulVersion = SECBUFFER_VERSION;
1458 local_desc.cBuffers = pMessage->cBuffers;
1459 local_desc.pBuffers = local_buff;
1461 for(i=0; i < pMessage->cBuffers; ++i)
1463 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1465 local_buff[i].BufferType = SECBUFFER_TOKEN;
1466 local_buff[i].cbBuffer = 16;
1467 local_buff[i].pvBuffer = local_sig;
1469 else
1471 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1472 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1473 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1477 if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1478 return ret;
1480 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1481 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1482 ret = SEC_E_MESSAGE_ALTERED;
1483 else
1484 ret = SEC_E_OK;
1486 HeapFree(GetProcessHeap(), 0, local_buff);
1487 pfQOP = &fQOP;
1489 return ret;
1493 /***********************************************************************
1494 * FreeCredentialsHandle
1496 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1497 PCredHandle phCredential)
1499 SECURITY_STATUS ret;
1501 if(phCredential){
1502 PNegoHelper helper = (PNegoHelper) phCredential->dwLower;
1503 phCredential->dwUpper = 0;
1504 phCredential->dwLower = 0;
1505 cleanup_helper(helper);
1506 ret = SEC_E_OK;
1508 else
1509 ret = SEC_E_OK;
1511 return ret;
1514 /***********************************************************************
1515 * EncryptMessage
1517 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1518 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1520 PNegoHelper helper;
1521 int token_idx;
1523 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1525 if(!phContext)
1526 return SEC_E_INVALID_HANDLE;
1528 if(fQOP)
1529 FIXME("Ignoring fQOP\n");
1531 if(MessageSeqNo)
1532 FIXME("Ignoring MessageSeqNo\n");
1534 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1535 return SEC_E_INVALID_TOKEN;
1537 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1538 return SEC_E_INVALID_TOKEN;
1540 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1541 return SEC_E_BUFFER_TOO_SMALL;
1543 helper = (PNegoHelper) phContext->dwLower;
1545 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1546 helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1548 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1549 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1550 (BYTE *)pMessage->pBuffers[1].pvBuffer,
1551 pMessage->pBuffers[1].cbBuffer);
1553 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1554 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1555 ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1558 return SEC_E_OK;
1560 else
1562 PBYTE sig;
1563 ULONG save_flags;
1565 /* EncryptMessage always produces real signatures, so make sure
1566 * NTLMSSP_NEGOTIATE_SIGN is set*/
1567 save_flags = helper->neg_flags;
1568 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1569 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1570 helper->neg_flags = save_flags;
1572 sig = pMessage->pBuffers[token_idx].pvBuffer;
1574 SECUR32_arc4Process(helper->crypt.ntlm.a4i, pMessage->pBuffers[1].pvBuffer,
1575 pMessage->pBuffers[1].cbBuffer);
1576 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1578 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1579 memset(sig+4, 0, 4);
1583 return SEC_E_OK;
1586 /***********************************************************************
1587 * DecryptMessage
1589 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1590 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1592 SECURITY_STATUS ret;
1593 ULONG ntlmssp_flags_save;
1594 PNegoHelper helper;
1595 int token_idx;
1596 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1598 if(!phContext)
1599 return SEC_E_INVALID_HANDLE;
1601 if(MessageSeqNo)
1602 FIXME("Ignoring MessageSeqNo\n");
1604 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1605 return SEC_E_INVALID_TOKEN;
1607 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1608 return SEC_E_INVALID_TOKEN;
1610 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1611 return SEC_E_BUFFER_TOO_SMALL;
1613 helper = (PNegoHelper) phContext->dwLower;
1615 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1617 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1618 pMessage->pBuffers[1].pvBuffer, pMessage->pBuffers[1].cbBuffer);
1620 else
1622 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1623 pMessage->pBuffers[1].pvBuffer, pMessage->pBuffers[1].cbBuffer);
1626 /* Make sure we use a session key for the signature check, EncryptMessage
1627 * always does that, even in the dummy case */
1628 ntlmssp_flags_save = helper->neg_flags;
1630 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1631 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1633 helper->neg_flags = ntlmssp_flags_save;
1635 return ret;
1638 static const SecurityFunctionTableA ntlmTableA = {
1640 NULL, /* EnumerateSecurityPackagesA */
1641 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1642 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1643 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1644 NULL, /* Reserved2 */
1645 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1646 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1647 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1648 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1649 NULL, /* ApplyControlToken */
1650 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1651 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1652 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1653 ntlm_MakeSignature, /* MakeSignature */
1654 ntlm_VerifySignature, /* VerifySignature */
1655 FreeContextBuffer, /* FreeContextBuffer */
1656 NULL, /* QuerySecurityPackageInfoA */
1657 NULL, /* Reserved3 */
1658 NULL, /* Reserved4 */
1659 NULL, /* ExportSecurityContext */
1660 NULL, /* ImportSecurityContextA */
1661 NULL, /* AddCredentialsA */
1662 NULL, /* Reserved8 */
1663 NULL, /* QuerySecurityContextToken */
1664 ntlm_EncryptMessage, /* EncryptMessage */
1665 ntlm_DecryptMessage, /* DecryptMessage */
1666 NULL, /* SetContextAttributesA */
1669 static const SecurityFunctionTableW ntlmTableW = {
1671 NULL, /* EnumerateSecurityPackagesW */
1672 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1673 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1674 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1675 NULL, /* Reserved2 */
1676 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1677 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1678 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1679 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1680 NULL, /* ApplyControlToken */
1681 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1682 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1683 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1684 ntlm_MakeSignature, /* MakeSignature */
1685 ntlm_VerifySignature, /* VerifySignature */
1686 FreeContextBuffer, /* FreeContextBuffer */
1687 NULL, /* QuerySecurityPackageInfoW */
1688 NULL, /* Reserved3 */
1689 NULL, /* Reserved4 */
1690 NULL, /* ExportSecurityContext */
1691 NULL, /* ImportSecurityContextW */
1692 NULL, /* AddCredentialsW */
1693 NULL, /* Reserved8 */
1694 NULL, /* QuerySecurityContextToken */
1695 ntlm_EncryptMessage, /* EncryptMessage */
1696 ntlm_DecryptMessage, /* DecryptMessage */
1697 NULL, /* SetContextAttributesW */
1700 #define NTLM_COMMENT \
1701 { 'N', 'T', 'L', 'M', ' ', \
1702 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1703 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1705 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1706 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1708 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1710 static char ntlm_name_A[] = NTLM_NAME;
1711 static WCHAR ntlm_name_W[] = NTLM_NAME;
1713 /* According to Windows, NTLM has the following capabilities. */
1714 #define CAPS ( \
1715 SECPKG_FLAG_INTEGRITY | \
1716 SECPKG_FLAG_PRIVACY | \
1717 SECPKG_FLAG_TOKEN_ONLY | \
1718 SECPKG_FLAG_CONNECTION | \
1719 SECPKG_FLAG_MULTI_REQUIRED | \
1720 SECPKG_FLAG_IMPERSONATION | \
1721 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1722 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1724 static const SecPkgInfoW infoW = {
1725 CAPS,
1727 RPC_C_AUTHN_WINNT,
1728 NTLM_MAX_BUF,
1729 ntlm_name_W,
1730 ntlm_comment_W
1733 static const SecPkgInfoA infoA = {
1734 CAPS,
1736 RPC_C_AUTHN_WINNT,
1737 NTLM_MAX_BUF,
1738 ntlm_name_A,
1739 ntlm_comment_A
1742 void SECUR32_initNTLMSP(void)
1744 SECURITY_STATUS ret;
1745 PNegoHelper helper;
1746 static CHAR version[] = "--version";
1748 SEC_CHAR *args[] = {
1749 ntlm_auth,
1750 version,
1751 NULL };
1753 if((ret = fork_helper(&helper, ntlm_auth, args)) != SEC_E_OK)
1755 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1756 helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1757 helper->major = helper->minor = helper->micro = -1;
1759 else
1760 check_version(helper);
1762 if( (helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
1763 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1764 helper->minor > MIN_NTLM_AUTH_MINOR_VERSION) ||
1765 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1766 helper->minor == MIN_NTLM_AUTH_MINOR_VERSION &&
1767 helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION) )
1769 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1770 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1772 else
1774 ERR("%s was not found or is outdated. "
1775 "Make sure that ntlm_auth >= %d.%d.%d is in your path.\n",
1776 ntlm_auth,
1777 MIN_NTLM_AUTH_MAJOR_VERSION,
1778 MIN_NTLM_AUTH_MINOR_VERSION,
1779 MIN_NTLM_AUTH_MICRO_VERSION);
1781 cleanup_helper(helper);