secur32: Test and fix DecryptMessage for multiple data buffers.
[wine/wine-gecko.git] / dlls / secur32 / ntlm.c
blob8cc206df8c4d1e54eb01689d68d795c054241203
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 "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
35 #define NTLM_MAX_BUF 1904
38 static CHAR ntlm_auth[] = "ntlm_auth";
40 /***********************************************************************
41 * QueryCredentialsAttributesA
43 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
44 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
46 SECURITY_STATUS ret;
48 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
50 if(ulAttribute == SECPKG_ATTR_NAMES)
52 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
53 ret = SEC_E_UNSUPPORTED_FUNCTION;
55 else
56 ret = SEC_E_UNSUPPORTED_FUNCTION;
58 return ret;
61 /***********************************************************************
62 * QueryCredentialsAttributesW
64 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
65 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
67 SECURITY_STATUS ret;
69 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
71 if(ulAttribute == SECPKG_ATTR_NAMES)
73 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
74 ret = SEC_E_UNSUPPORTED_FUNCTION;
76 else
77 ret = SEC_E_UNSUPPORTED_FUNCTION;
79 return ret;
82 /***********************************************************************
83 * AcquireCredentialsHandleW
85 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
86 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
87 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
88 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
90 SECURITY_STATUS ret;
91 PNegoHelper helper = NULL;
92 static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp",
93 credentials_argv[] = "--use-cached-creds";
95 SEC_CHAR *client_user_arg = NULL;
96 SEC_CHAR *client_domain_arg = NULL;
97 SEC_WCHAR *username = NULL, *domain = NULL;
99 SEC_CHAR *client_argv[6];
100 SEC_CHAR *server_argv[] = { ntlm_auth,
101 server_helper_protocol,
102 NULL };
104 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
105 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
106 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
109 switch(fCredentialUse)
111 case SECPKG_CRED_INBOUND:
112 if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
113 SEC_E_OK)
115 phCredential = NULL;
116 break;
118 else
120 helper->mode = NTLM_SERVER;
121 phCredential->dwUpper = fCredentialUse;
122 phCredential->dwLower = (ULONG_PTR)helper;
124 ret = SEC_E_OK;
125 break;
126 case SECPKG_CRED_OUTBOUND:
128 static const char username_arg[] = "--username=";
129 static const char domain_arg[] = "--domain=";
130 static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
131 int unixcp_size;
133 if(pAuthData == NULL)
135 LPWKSTA_USER_INFO_1 ui = NULL;
136 NET_API_STATUS status;
138 status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
139 if (status != NERR_Success || ui == NULL)
141 ret = SEC_E_NO_CREDENTIALS;
142 phCredential = NULL;
143 break;
146 username = HeapAlloc(GetProcessHeap(), 0,
147 (lstrlenW(ui->wkui1_username)+1) *
148 sizeof(SEC_WCHAR));
149 lstrcpyW(username, ui->wkui1_username);
151 /* same for the domain */
152 domain = HeapAlloc(GetProcessHeap(), 0,
153 (lstrlenW(ui->wkui1_logon_domain)+1) *
154 sizeof(SEC_WCHAR));
155 lstrcpyW(domain, ui->wkui1_logon_domain);
156 NetApiBufferFree(ui);
158 else
160 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
161 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
163 if (!auth_data->UserLength || !auth_data->DomainLength)
165 ret = SEC_E_NO_CREDENTIALS;
166 phCredential = NULL;
167 break;
169 /* Get username and domain from pAuthData */
170 username = HeapAlloc(GetProcessHeap(), 0,
171 (auth_data->UserLength + 1) * sizeof(SEC_WCHAR));
172 lstrcpyW(username, auth_data->User);
174 domain = HeapAlloc(GetProcessHeap(), 0,
175 (auth_data->DomainLength + 1) * sizeof(SEC_WCHAR));
176 lstrcpyW(domain, auth_data->Domain);
178 TRACE("Username is %s\n", debugstr_w(username));
179 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
180 username, -1, NULL, 0, NULL, NULL) + sizeof(username_arg);
181 client_user_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
182 lstrcpyA(client_user_arg, username_arg);
183 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, username, -1,
184 client_user_arg + sizeof(username_arg) - 1,
185 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
187 TRACE("Domain name is %s\n", debugstr_w(domain));
188 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
189 domain, -1, NULL, 0, NULL, NULL) + sizeof(domain_arg);
190 client_domain_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
191 lstrcpyA(client_domain_arg, domain_arg);
192 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domain,
193 -1, client_domain_arg + sizeof(domain_arg) - 1,
194 unixcp_size - sizeof(domain) + 1, NULL, NULL);
196 client_argv[0] = ntlm_auth;
197 client_argv[1] = helper_protocol;
198 client_argv[2] = client_user_arg;
199 client_argv[3] = client_domain_arg;
200 client_argv[4] = credentials_argv;
201 client_argv[5] = NULL;
203 if((ret = fork_helper(&helper, ntlm_auth, client_argv)) !=
204 SEC_E_OK)
206 phCredential = NULL;
207 break;
209 else
211 helper->mode = NTLM_CLIENT;
213 if(pAuthData != NULL)
215 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
216 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
218 if(auth_data->PasswordLength != 0)
220 helper->pwlen = WideCharToMultiByte(CP_UNIXCP,
221 WC_NO_BEST_FIT_CHARS, auth_data->Password,
222 auth_data->PasswordLength+1, NULL, 0, NULL, NULL);
224 helper->password = HeapAlloc(GetProcessHeap(), 0,
225 helper->pwlen);
227 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
228 auth_data->Password, auth_data->PasswordLength+1,
229 helper->password, helper->pwlen, NULL, NULL);
233 phCredential->dwUpper = fCredentialUse;
234 phCredential->dwLower = (ULONG_PTR)helper;
235 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
236 phCredential->dwUpper, phCredential->dwLower);
238 ret = SEC_E_OK;
239 break;
241 case SECPKG_CRED_BOTH:
242 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
243 ret = SEC_E_UNSUPPORTED_FUNCTION;
244 phCredential = NULL;
245 break;
246 default:
247 phCredential = NULL;
248 ret = SEC_E_UNKNOWN_CREDENTIALS;
252 HeapFree(GetProcessHeap(), 0, client_user_arg);
253 HeapFree(GetProcessHeap(), 0, client_domain_arg);
254 HeapFree(GetProcessHeap(), 0, username);
255 HeapFree(GetProcessHeap(), 0, domain);
257 return ret;
260 /***********************************************************************
261 * AcquireCredentialsHandleA
263 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
264 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
265 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
266 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
268 SECURITY_STATUS ret;
269 int user_sizeW, domain_sizeW, passwd_sizeW;
271 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
273 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
274 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
276 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
277 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
278 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
280 if(pszPackage != NULL)
282 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
283 NULL, 0);
285 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
286 sizeof(SEC_WCHAR));
287 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
291 if(pAuthData != NULL)
293 identity = (PSEC_WINNT_AUTH_IDENTITY_A)pAuthData;
295 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
297 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
298 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
300 if(identity->UserLength != 0)
302 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
303 (LPCSTR)identity->User, identity->UserLength+1, NULL, 0);
304 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
305 sizeof(SEC_WCHAR));
306 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
307 identity->UserLength+1, user, user_sizeW);
309 else
311 user_sizeW = 0;
314 if(identity->DomainLength != 0)
316 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
317 (LPCSTR)identity->Domain, identity->DomainLength+1, NULL, 0);
318 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
319 * sizeof(SEC_WCHAR));
320 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
321 identity->DomainLength+1, domain, domain_sizeW);
323 else
325 domain_sizeW = 0;
328 if(identity->PasswordLength != 0)
330 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
331 (LPCSTR)identity->Password, identity->PasswordLength+1,
332 NULL, 0);
333 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
334 * sizeof(SEC_WCHAR));
335 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
336 identity->PasswordLength+1, passwd, passwd_sizeW);
338 else
340 passwd_sizeW = 0;
343 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
344 pAuthDataW->User = user;
345 pAuthDataW->UserLength = user_sizeW;
346 pAuthDataW->Domain = domain;
347 pAuthDataW->DomainLength = domain_sizeW;
348 pAuthDataW->Password = passwd;
349 pAuthDataW->PasswordLength = passwd_sizeW;
351 else
353 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
357 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
358 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
359 ptsExpiry);
361 HeapFree(GetProcessHeap(), 0, package);
362 HeapFree(GetProcessHeap(), 0, user);
363 HeapFree(GetProcessHeap(), 0, domain);
364 HeapFree(GetProcessHeap(), 0, passwd);
365 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
366 HeapFree(GetProcessHeap(), 0, pAuthDataW);
368 return ret;
371 /***********************************************************************
372 * InitializeSecurityContextW
374 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
375 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
376 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
377 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
378 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
380 SECURITY_STATUS ret;
381 PNegoHelper helper;
382 ULONG ctxt_attr = 0;
383 char* buffer, *want_flags = NULL;
384 PBYTE bin;
385 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
387 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
388 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
389 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
391 if(!phCredential)
392 return SEC_E_INVALID_HANDLE;
394 /* As the server side of sspi never calls this, make sure that
395 * the handler is a client handler.
397 helper = (PNegoHelper)phCredential->dwLower;
398 if(helper->mode != NTLM_CLIENT)
400 TRACE("Helper mode = %d\n", helper->mode);
401 return SEC_E_INVALID_HANDLE;
404 /****************************************
405 * When communicating with the client, there can be the
406 * following reply packets:
407 * YR <base64 blob> should be sent to the server
408 * PW should be sent back to helper with
409 * base64 encoded password
410 * AF <base64 blob> client is done, blob should be
411 * sent to server with KK prefixed
412 * GF <string list> A string list of negotiated flags
413 * GK <base64 blob> base64 encoded session key
414 * BH <char reason> something broke
416 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
418 if (pszTargetName)
420 TRACE("According to a MS whitepaper pszTargetName is ignored.\n");
423 if(TargetDataRep == SECURITY_NETWORK_DREP){
424 TRACE("Setting SECURITY_NETWORK_DREP\n");
427 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
428 bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
430 if((phContext == NULL) && (pInput == NULL))
432 TRACE("First time in ISC()\n");
433 /* Allocate space for a maximal string of
434 * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
435 * NTLMSSP_FEATURE_SESSION_KEY"
437 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
438 if(want_flags == NULL)
440 ret = SEC_E_INSUFFICIENT_MEMORY;
441 goto isc_end;
443 lstrcpyA(want_flags, "SF");
444 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
445 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
446 if(fContextReq & ISC_REQ_CONNECTION)
448 /* This is default, so we'll enable it */
449 ctxt_attr |= ISC_RET_CONNECTION;
450 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
452 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
453 FIXME("ISC_REQ_EXTENDED_ERROR\n");
454 if(fContextReq & ISC_REQ_INTEGRITY)
455 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
456 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
457 FIXME("ISC_REQ_MUTUAL_AUTH\n");
458 if(fContextReq & ISC_REQ_REPLAY_DETECT)
459 FIXME("ISC_REQ_REPLAY_DETECT\n");
460 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
461 FIXME("ISC_REQ_SEQUENCE_DETECT\n");
462 if(fContextReq & ISC_REQ_STREAM)
463 FIXME("ISC_REQ_STREAM\n");
465 /* If no password is given, try to use cached credentials. Fall back to an empty
466 * password if this failed. */
467 if(helper->password == NULL)
469 lstrcpynA(buffer, "OK", max_len-1);
470 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
471 goto isc_end;
472 /* If the helper replied with "PW", using cached credentials failed */
473 if(!strncmp(buffer, "PW", 2))
475 TRACE("Using cached credentials failed. Using empty password.\n");
476 lstrcpynA(buffer, "PW AA==", max_len-1);
478 else /* Just do a noop on the next run */
479 lstrcpynA(buffer, "OK", max_len-1);
481 else
483 lstrcpynA(buffer, "PW ", max_len-1);
484 if((ret = encodeBase64((unsigned char*)helper->password,
485 helper->pwlen-2, buffer+3,
486 max_len-3, &buffer_len)) != SEC_E_OK)
488 TRACE("Deleting password!\n");
489 memset(helper->password, 0, helper->pwlen-2);
490 HeapFree(GetProcessHeap(), 0, helper->password);
491 goto isc_end;
496 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
497 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
498 goto isc_end;
500 TRACE("Helper returned %s\n", debugstr_a(buffer));
502 if(lstrlenA(want_flags) > 2)
504 TRACE("Want flags are '%s'\n", debugstr_a(want_flags));
505 lstrcpynA(buffer, want_flags, max_len-1);
506 if((ret = run_helper(helper, buffer, max_len, &buffer_len))
507 != SEC_E_OK)
508 goto isc_end;
509 if(!strncmp(buffer, "BH", 2))
510 TRACE("Helper doesn't understand new command set\n");
513 lstrcpynA(buffer, "YR", max_len-1);
515 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
516 goto isc_end;
518 TRACE("%s\n", buffer);
520 if(strncmp(buffer, "YR ", 3) != 0)
522 /* Something borked */
523 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
524 ret = SEC_E_INTERNAL_ERROR;
525 goto isc_end;
527 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
528 max_len-1, &bin_len)) != SEC_E_OK)
529 goto isc_end;
531 /* Mask away the NTLMv2 flag, as well as the key exchange flag */
532 bin[14] &= ~0x08;
533 bin[15] &= ~0x40;
535 /* put the decoded client blob into the out buffer */
537 ret = SEC_I_CONTINUE_NEEDED;
539 else
541 /* handle second call here */
542 /* encode server data to base64 */
543 if (!pInput || !pInput->cBuffers)
545 ret = SEC_E_INCOMPLETE_MESSAGE;
546 goto isc_end;
549 if (!pInput->pBuffers[0].pvBuffer)
551 ret = SEC_E_INTERNAL_ERROR;
552 goto isc_end;
555 if(pInput->pBuffers[0].cbBuffer > max_len)
557 TRACE("pInput->pBuffers[0].cbBuffer is: %ld\n",
558 pInput->pBuffers[0].cbBuffer);
559 ret = SEC_E_INVALID_TOKEN;
560 goto isc_end;
562 else
563 bin_len = pInput->pBuffers[0].cbBuffer;
565 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
567 lstrcpynA(buffer, "TT ", max_len-1);
569 if((ret = encodeBase64(bin, bin_len, buffer+3,
570 max_len-3, &buffer_len)) != SEC_E_OK)
571 goto isc_end;
573 TRACE("Server sent: %s\n", debugstr_a(buffer));
575 /* send TT base64 blob to ntlm_auth */
576 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
577 goto isc_end;
579 TRACE("Helper replied: %s\n", debugstr_a(buffer));
581 if( (strncmp(buffer, "KK ", 3) != 0) &&
582 (strncmp(buffer, "AF ", 3) !=0))
584 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
585 ret = SEC_E_INVALID_TOKEN;
586 goto isc_end;
589 /* decode the blob and send it to server */
590 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
591 &bin_len)) != SEC_E_OK)
593 goto isc_end;
596 phNewContext->dwUpper = ctxt_attr;
597 phNewContext->dwLower = (ULONG_PTR)helper;
599 ret = SEC_E_OK;
602 /* put the decoded client blob into the out buffer */
604 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
606 if (pOutput)
608 pOutput->cBuffers = 1;
609 pOutput->pBuffers[0].pvBuffer = SECUR32_ALLOC(bin_len);
610 pOutput->pBuffers[0].cbBuffer = bin_len;
614 if (!pOutput || !pOutput->cBuffers || pOutput->pBuffers[0].cbBuffer < bin_len)
616 TRACE("out buffer is NULL or has not enough space\n");
617 ret = SEC_E_BUFFER_TOO_SMALL;
618 goto isc_end;
621 if (!pOutput->pBuffers[0].pvBuffer)
623 TRACE("out buffer is NULL\n");
624 ret = SEC_E_INTERNAL_ERROR;
625 goto isc_end;
628 pOutput->pBuffers[0].cbBuffer = bin_len;
629 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
630 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
632 if(ret == SEC_E_OK)
634 TRACE("Getting negotiated flags\n");
635 lstrcpynA(buffer, "GF", max_len - 1);
636 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
637 goto isc_end;
639 if(buffer_len < 3)
641 TRACE("No flags negotiated, or helper does not support GF command\n");
642 helper->neg_flags = 0l;
644 else
646 TRACE("Negotiated %s\n", debugstr_a(buffer));
647 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
648 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
651 TRACE("Getting session key\n");
652 lstrcpynA(buffer, "GK", max_len - 1);
653 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
654 goto isc_end;
656 if(strncmp(buffer, "BH", 2) == 0)
658 TRACE("Helper does not understand command or no key negotiated.\n");
659 helper->valid_session_key = FALSE;
660 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
661 /*Generate the dummy session key = MD4(MD4(password))*/
662 if(helper->password)
664 SEC_WCHAR *unicode_password;
665 int passwd_lenW;
667 TRACE("Converting password to unicode.\n");
668 passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
669 (LPCSTR)helper->password, helper->pwlen,
670 NULL, 0);
671 unicode_password = HeapAlloc(GetProcessHeap(), 0,
672 passwd_lenW * sizeof(SEC_WCHAR));
673 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)helper->password,
674 helper->pwlen, unicode_password, passwd_lenW);
676 SECUR32_CreateNTLMv1SessionKey((PBYTE)unicode_password,
677 lstrlenW(unicode_password) * sizeof(SEC_WCHAR), helper->session_key);
679 HeapFree(GetProcessHeap(), 0, unicode_password);
681 else
682 memset(helper->session_key, 0, 16);
684 else if(strncmp(buffer, "GK ", 3) == 0)
686 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
687 &bin_len)) != SEC_E_OK)
689 TRACE("Failed to decode session key\n");
691 TRACE("Session key is %s\n", debugstr_a(buffer+3));
692 helper->valid_session_key = TRUE;
693 if(!helper->session_key)
694 helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
695 if(!helper->session_key)
697 TRACE("Failed to allocate memory for session key\n");
698 ret = SEC_E_INTERNAL_ERROR;
699 goto isc_end;
701 memcpy(helper->session_key, bin, bin_len);
704 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
705 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
706 helper->crypt.ntlm.seq_num = 0l;
709 if(ret != SEC_I_CONTINUE_NEEDED)
711 TRACE("Deleting password!\n");
712 if(helper->password)
713 memset(helper->password, 0, helper->pwlen-2);
714 HeapFree(GetProcessHeap(), 0, helper->password);
716 isc_end:
717 HeapFree(GetProcessHeap(), 0, want_flags);
718 HeapFree(GetProcessHeap(), 0, buffer);
719 HeapFree(GetProcessHeap(), 0, bin);
720 return ret;
723 /***********************************************************************
724 * InitializeSecurityContextA
726 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
727 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
728 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
729 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
730 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
732 SECURITY_STATUS ret;
734 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
735 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
736 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
738 if (phCredential)
740 SEC_WCHAR *target = NULL;
741 if(pszTargetName != NULL)
743 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
744 strlen(pszTargetName)+1, NULL, 0);
745 target = HeapAlloc(GetProcessHeap(), 0, target_size *
746 sizeof(SEC_WCHAR));
747 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
748 target, target_size);
751 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
752 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
753 phNewContext, pOutput, pfContextAttr, ptsExpiry);
755 HeapFree(GetProcessHeap(), 0, target);
757 else
759 ret = SEC_E_INVALID_HANDLE;
761 return ret;
764 /***********************************************************************
765 * AcceptSecurityContext
767 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
768 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
769 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
770 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
772 SECURITY_STATUS ret;
773 char *buffer, *want_flags = NULL;
774 PBYTE bin;
775 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
776 ULONG ctxt_attr = 0;
777 PNegoHelper helper;
779 TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
780 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
781 ptsExpiry);
783 if (!phCredential)
784 return SEC_E_INVALID_HANDLE;
786 helper = (PNegoHelper)phCredential->dwLower;
788 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
789 bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
791 if(helper->mode != NTLM_SERVER)
793 ret = SEC_E_INVALID_HANDLE;
794 goto asc_end;
797 if(TargetDataRep == SECURITY_NETWORK_DREP){
798 TRACE("Using SECURITY_NETWORK_DREP\n");
801 if(phContext == NULL)
803 /* This is the first call to AcceptSecurityHandle */
804 if(pInput == NULL)
806 ret = SEC_E_INCOMPLETE_MESSAGE;
807 goto asc_end;
810 if(pInput->cBuffers < 1)
812 ret = SEC_E_INCOMPLETE_MESSAGE;
813 goto asc_end;
816 if(pInput->pBuffers[0].cbBuffer > max_len)
818 ret = SEC_E_INVALID_TOKEN;
819 goto asc_end;
821 else
822 bin_len = pInput->pBuffers[0].cbBuffer;
824 /* Handle all the flags */
825 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
826 if(want_flags == NULL)
828 TRACE("Failed to allocate memory for the want_flags!\n");
829 ret = SEC_E_INSUFFICIENT_MEMORY;
830 goto asc_end;
832 lstrcpyA(want_flags, "SF");
833 if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
835 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
837 if(fContextReq & ASC_REQ_CONFIDENTIALITY)
839 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
841 if(fContextReq & ASC_REQ_CONNECTION)
843 /* This is default, so we'll enable it */
844 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
845 ctxt_attr |= ASC_RET_CONNECTION;
847 if(fContextReq & ASC_REQ_EXTENDED_ERROR)
849 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
851 if(fContextReq & ASC_REQ_INTEGRITY)
853 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
855 if(fContextReq & ASC_REQ_MUTUAL_AUTH)
857 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
859 if(fContextReq & ASC_REQ_REPLAY_DETECT)
861 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
863 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
865 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
867 if(fContextReq & ISC_REQ_STREAM)
869 FIXME("ASC_REQ_STREAM stub\n");
871 /* Done with the flags */
873 if(lstrlenA(want_flags) > 3)
875 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
876 lstrcpynA(buffer, want_flags, max_len - 1);
877 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
878 SEC_E_OK)
879 goto asc_end;
880 if(!strncmp(buffer, "BH", 2))
881 TRACE("Helper doesn't understand new command set\n");
884 /* This is the YR request from the client, encode to base64 */
886 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
888 lstrcpynA(buffer, "YR ", max_len-1);
890 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
891 &buffer_len)) != SEC_E_OK)
893 goto asc_end;
896 TRACE("Client sent: %s\n", debugstr_a(buffer));
898 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
899 SEC_E_OK)
901 goto asc_end;
904 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
905 /* The expected answer is TT <base64 blob> */
907 if(strncmp(buffer, "TT ", 3) != 0)
909 ret = SEC_E_INTERNAL_ERROR;
910 goto asc_end;
913 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
914 &bin_len)) != SEC_E_OK)
916 goto asc_end;
919 /* send this to the client */
920 if(pOutput == NULL)
922 ret = SEC_E_INSUFFICIENT_MEMORY;
923 goto asc_end;
926 if(pOutput->cBuffers < 1)
928 ret = SEC_E_INSUFFICIENT_MEMORY;
929 goto asc_end;
932 pOutput->pBuffers[0].cbBuffer = bin_len;
933 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
934 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
935 ret = SEC_I_CONTINUE_NEEDED;
938 else
940 /* we expect a KK request from client */
941 if(pInput == NULL)
943 ret = SEC_E_INCOMPLETE_MESSAGE;
944 goto asc_end;
947 if(pInput->cBuffers < 1)
949 ret = SEC_E_INCOMPLETE_MESSAGE;
950 goto asc_end;
953 if(pInput->pBuffers[0].cbBuffer > max_len)
955 ret = SEC_E_INVALID_TOKEN;
956 goto asc_end;
958 else
959 bin_len = pInput->pBuffers[0].cbBuffer;
961 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
963 lstrcpynA(buffer, "KK ", max_len-1);
965 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
966 &buffer_len)) != SEC_E_OK)
968 goto asc_end;
971 TRACE("Client sent: %s\n", debugstr_a(buffer));
973 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
974 SEC_E_OK)
976 goto asc_end;
979 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
981 if(strncmp(buffer, "AF ", 3) != 0)
983 if(strncmp(buffer, "NA ", 3) == 0)
985 ret = SEC_E_LOGON_DENIED;
986 goto asc_end;
988 else
990 ret = SEC_E_INTERNAL_ERROR;
991 goto asc_end;
994 pOutput->pBuffers[0].cbBuffer = 0;
995 ret = SEC_E_OK;
997 TRACE("Getting negotiated flags\n");
998 lstrcpynA(buffer, "GF", max_len - 1);
999 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1000 goto asc_end;
1002 if(buffer_len < 3)
1004 TRACE("No flags negotiated, or helper does not support GF command\n");
1006 else
1008 TRACE("Negotiated %s\n", debugstr_a(buffer));
1009 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
1010 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
1013 TRACE("Getting session key\n");
1014 lstrcpynA(buffer, "GK", max_len - 1);
1015 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1016 goto asc_end;
1018 if(buffer_len < 3)
1019 TRACE("Helper does not support GK command\n");
1020 else
1022 if(strncmp(buffer, "BH ", 3) == 0)
1024 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1025 helper->valid_session_key = FALSE;
1026 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1027 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1028 memset(helper->session_key, 0 , 16);
1030 else if(strncmp(buffer, "GK ", 3) == 0)
1032 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1033 &bin_len)) != SEC_E_OK)
1035 TRACE("Failed to decode session key\n");
1037 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1038 helper->valid_session_key = TRUE;
1039 if(!helper->session_key)
1040 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1041 if(!helper->session_key)
1043 TRACE("Failed to allocate memory for session key\n");
1044 ret = SEC_E_INTERNAL_ERROR;
1045 goto asc_end;
1047 memcpy(helper->session_key, bin, 16);
1050 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1051 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1052 helper->crypt.ntlm.seq_num = 0l;
1055 phNewContext->dwUpper = ctxt_attr;
1056 phNewContext->dwLower = (ULONG_PTR)helper;
1058 asc_end:
1059 HeapFree(GetProcessHeap(), 0, want_flags);
1060 HeapFree(GetProcessHeap(), 0, buffer);
1061 HeapFree(GetProcessHeap(), 0, bin);
1062 return ret;
1065 /***********************************************************************
1066 * CompleteAuthToken
1068 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1069 PSecBufferDesc pToken)
1071 /* We never need to call CompleteAuthToken anyway */
1072 TRACE("%p %p\n", phContext, pToken);
1073 if (!phContext)
1074 return SEC_E_INVALID_HANDLE;
1076 return SEC_E_OK;
1079 /***********************************************************************
1080 * DeleteSecurityContext
1082 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1084 SECURITY_STATUS ret;
1086 TRACE("%p\n", phContext);
1087 if (phContext)
1089 phContext->dwUpper = 0;
1090 phContext->dwLower = 0;
1091 ret = SEC_E_OK;
1093 else
1095 ret = SEC_E_INVALID_HANDLE;
1097 return ret;
1100 /***********************************************************************
1101 * QueryContextAttributesW
1103 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1104 ULONG ulAttribute, void *pBuffer)
1106 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1107 if (!phContext)
1108 return SEC_E_INVALID_HANDLE;
1110 switch(ulAttribute)
1112 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1113 _x(SECPKG_ATTR_ACCESS_TOKEN);
1114 _x(SECPKG_ATTR_AUTHORITY);
1115 _x(SECPKG_ATTR_DCE_INFO);
1116 case SECPKG_ATTR_FLAGS:
1118 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1119 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1121 spcf->Flags = 0;
1122 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1123 spcf->Flags |= ISC_RET_INTEGRITY;
1124 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1125 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1126 return SEC_E_OK;
1128 _x(SECPKG_ATTR_KEY_INFO);
1129 _x(SECPKG_ATTR_LIFESPAN);
1130 _x(SECPKG_ATTR_NAMES);
1131 _x(SECPKG_ATTR_NATIVE_NAMES);
1132 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1133 _x(SECPKG_ATTR_PACKAGE_INFO);
1134 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1135 _x(SECPKG_ATTR_SESSION_KEY);
1136 case SECPKG_ATTR_SIZES:
1138 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1139 spcs->cbMaxToken = NTLM_MAX_BUF;
1140 spcs->cbMaxSignature = 16;
1141 spcs->cbBlockSize = 0;
1142 spcs->cbSecurityTrailer = 16;
1143 return SEC_E_OK;
1145 _x(SECPKG_ATTR_STREAM_SIZES);
1146 _x(SECPKG_ATTR_TARGET_INFORMATION);
1147 #undef _x
1148 default:
1149 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1152 return SEC_E_UNSUPPORTED_FUNCTION;
1155 /***********************************************************************
1156 * QueryContextAttributesA
1158 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1159 ULONG ulAttribute, void *pBuffer)
1161 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1164 /***********************************************************************
1165 * ImpersonateSecurityContext
1167 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1169 SECURITY_STATUS ret;
1171 TRACE("%p\n", phContext);
1172 if (phContext)
1174 ret = SEC_E_UNSUPPORTED_FUNCTION;
1176 else
1178 ret = SEC_E_INVALID_HANDLE;
1180 return ret;
1183 /***********************************************************************
1184 * RevertSecurityContext
1186 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1188 SECURITY_STATUS ret;
1190 TRACE("%p\n", phContext);
1191 if (phContext)
1193 ret = SEC_E_UNSUPPORTED_FUNCTION;
1195 else
1197 ret = SEC_E_INVALID_HANDLE;
1199 return ret;
1202 /***********************************************************************
1203 * MakeSignature
1205 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1206 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1208 PNegoHelper helper;
1209 ULONG sign_version = 1;
1210 UINT i;
1211 int token_idx = -1;
1213 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1214 if (!phContext)
1215 return SEC_E_INVALID_HANDLE;
1217 if(fQOP)
1218 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1220 if(MessageSeqNo)
1221 FIXME("Ignoring MessageSeqNo\n");
1223 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1224 return SEC_E_INVALID_TOKEN;
1226 for(i=0; i < pMessage->cBuffers; ++i)
1228 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1230 token_idx = i;
1231 break;
1234 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1235 if(token_idx == -1)
1236 return SEC_E_INVALID_TOKEN;
1238 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1239 return SEC_E_BUFFER_TOO_SMALL;
1241 helper = (PNegoHelper)phContext->dwLower;
1242 TRACE("Negotiated flags are: 0x%08lx\n", helper->neg_flags);
1243 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
1245 FIXME("Can't handle NTLMv2 signing yet. Aborting.\n");
1246 return SEC_E_UNSUPPORTED_FUNCTION;
1248 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1250 PBYTE sig = pMessage->pBuffers[token_idx].pvBuffer;
1251 ULONG crc = 0U;
1253 for(i=0; i < pMessage->cBuffers; ++i)
1255 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1257 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1258 pMessage->pBuffers[i].cbBuffer, crc);
1262 sig[ 0] = (sign_version >> 0) & 0xff;
1263 sig[ 1] = (sign_version >> 8) & 0xff;
1264 sig[ 2] = (sign_version >> 16) & 0xff;
1265 sig[ 3] = (sign_version >> 24) & 0xff;
1266 memset(sig+4, 0, 4);
1267 sig[ 8] = (crc >> 0) & 0xff;
1268 sig[ 9] = (crc >> 8) & 0xff;
1269 sig[10] = (crc >> 16) & 0xff;
1270 sig[11] = (crc >> 24) & 0xff;
1271 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1272 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1273 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1274 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1276 ++(helper->crypt.ntlm.seq_num);
1278 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1279 return SEC_E_OK;
1282 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1284 FIXME("Can't handle encrypted session key yet. Aborting.\n");
1285 return SEC_E_UNSUPPORTED_FUNCTION;
1288 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1290 TRACE("Generating dummy signature\n");
1291 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1292 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1293 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1294 pMessage->pBuffers[token_idx].cbBuffer = 16;
1295 return SEC_E_OK;
1298 return SEC_E_UNSUPPORTED_FUNCTION;
1301 /***********************************************************************
1302 * VerifySignature
1304 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1305 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1307 PNegoHelper helper;
1308 ULONG fQOP = 0;
1309 UINT i;
1310 int token_idx = -1;
1311 SECURITY_STATUS ret;
1313 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1314 if(!phContext)
1315 return SEC_E_INVALID_HANDLE;
1317 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1318 return SEC_E_INVALID_TOKEN;
1320 for(i=0; i < pMessage->cBuffers; ++i)
1322 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1324 token_idx = i;
1325 break;
1328 if(token_idx == -1)
1329 return SEC_E_INVALID_TOKEN;
1331 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1332 return SEC_E_BUFFER_TOO_SMALL;
1334 if(MessageSeqNo)
1335 FIXME("Ignoring MessageSeqNo\n");
1337 helper = (PNegoHelper)phContext->dwLower;
1338 TRACE("Negotiated flags: 0x%08lx\n", helper->neg_flags);
1340 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
1342 FIXME("Can't handle NTLMv2 signing yet. Aborting.\n");
1343 return SEC_E_UNSUPPORTED_FUNCTION;
1346 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1348 SecBufferDesc local_desc;
1349 PSecBuffer local_buff;
1350 BYTE local_sig[16];
1352 local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1354 local_desc.ulVersion = SECBUFFER_VERSION;
1355 local_desc.cBuffers = pMessage->cBuffers;
1356 local_desc.pBuffers = local_buff;
1358 for(i=0; i < pMessage->cBuffers; ++i)
1360 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1362 local_buff[i].BufferType = SECBUFFER_TOKEN;
1363 local_buff[i].cbBuffer = 16;
1364 local_buff[i].pvBuffer = local_sig;
1366 else
1368 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1369 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1370 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1374 ntlm_MakeSignature(phContext, fQOP, &local_desc, MessageSeqNo);
1376 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1377 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1378 ret = SEC_E_MESSAGE_ALTERED;
1379 else
1380 ret = SEC_E_OK;
1382 HeapFree(GetProcessHeap(), 0, local_buff);
1384 return ret;
1387 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1389 FIXME("Can't handle encrypted session keys yet. Aborting.\n");
1390 return SEC_E_UNSUPPORTED_FUNCTION;
1393 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1395 const BYTE dummy_sig[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1396 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1397 TRACE("Assuming dummy signature.\n");
1398 if(memcmp(pMessage->pBuffers[token_idx].pvBuffer, dummy_sig, sizeof(dummy_sig)) != 0)
1400 TRACE("Failed to verify the packet signature. Not a dummy signature?\n");
1401 return SEC_E_MESSAGE_ALTERED;
1403 else
1405 pfQOP = &fQOP;
1406 return SEC_E_OK;
1410 return SEC_E_UNSUPPORTED_FUNCTION;
1413 /***********************************************************************
1414 * FreeCredentialsHandle
1416 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1417 PCredHandle phCredential)
1419 SECURITY_STATUS ret;
1421 if(phCredential){
1422 PNegoHelper helper = (PNegoHelper) phCredential->dwLower;
1423 phCredential->dwUpper = 0;
1424 phCredential->dwLower = 0;
1425 cleanup_helper(helper);
1426 ret = SEC_E_OK;
1428 else
1429 ret = SEC_E_OK;
1431 return ret;
1434 /***********************************************************************
1435 * EncryptMessage
1437 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1438 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1440 PNegoHelper helper;
1441 UINT i;
1442 int token_idx = -1;
1444 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1446 if(!phContext)
1447 return SEC_E_INVALID_HANDLE;
1449 if(fQOP)
1450 FIXME("Ignoring fQOP\n");
1452 if(MessageSeqNo)
1453 FIXME("Ignoring MessageSeqNo\n");
1455 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1456 return SEC_E_INVALID_TOKEN;
1458 for(i=0; i < pMessage->cBuffers; ++i)
1460 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1462 token_idx = i;
1463 break;
1467 if(token_idx == -1)
1468 return SEC_E_INVALID_TOKEN;
1470 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1471 return SEC_E_BUFFER_TOO_SMALL;
1473 helper = (PNegoHelper) phContext->dwLower;
1475 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
1477 FIXME("Can't handle NTLMv2 encryption yet, aborting\n");
1478 return SEC_E_UNSUPPORTED_FUNCTION;
1480 else
1482 PBYTE sig = pMessage->pBuffers[token_idx].pvBuffer;
1483 ULONG crc = 0U;
1484 ULONG sign_version = 1l;
1486 for(i=0; i < pMessage->cBuffers; ++i)
1488 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1490 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1491 pMessage->pBuffers[i].cbBuffer, crc);
1495 sig[ 0] = (sign_version >> 0) & 0xff;
1496 sig[ 1] = (sign_version >> 8) & 0xff;
1497 sig[ 2] = (sign_version >> 16) & 0xff;
1498 sig[ 3] = (sign_version >> 24) & 0xff;
1499 memset(sig+4, 0, 4);
1500 sig[ 8] = (crc >> 0) & 0xff;
1501 sig[ 9] = (crc >> 8) & 0xff;
1502 sig[10] = (crc >> 16) & 0xff;
1503 sig[11] = (crc >> 24) & 0xff;
1504 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1505 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1506 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1507 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1509 SECUR32_arc4Process(helper->crypt.ntlm.a4i, pMessage->pBuffers[1].pvBuffer,
1510 pMessage->pBuffers[1].cbBuffer);
1511 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1513 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1514 memset(sig+4, 0, 4);
1516 ++(helper->crypt.ntlm.seq_num);
1520 return SEC_E_OK;
1523 /***********************************************************************
1524 * DecryptMessage
1526 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1527 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1529 SECURITY_STATUS ret;
1530 ULONG ntlmssp_flags_save;
1531 PNegoHelper helper;
1532 UINT i;
1533 int token_idx = -1;
1534 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1536 if(!phContext)
1537 return SEC_E_INVALID_HANDLE;
1539 if(MessageSeqNo)
1540 FIXME("Ignoring MessageSeqNo\n");
1542 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1543 return SEC_E_INVALID_TOKEN;
1545 for(i=0; i < pMessage->cBuffers; ++i)
1547 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1549 token_idx = i;
1550 break;
1553 if(token_idx == -1)
1554 return SEC_E_INVALID_TOKEN;
1556 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1557 return SEC_E_BUFFER_TOO_SMALL;
1559 helper = (PNegoHelper) phContext->dwLower;
1561 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
1563 FIXME("Can't handle NTLMv2 encryption yet, aborting\n");
1564 return SEC_E_UNSUPPORTED_FUNCTION;
1566 else
1568 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1569 pMessage->pBuffers[1].pvBuffer, pMessage->pBuffers[1].cbBuffer);
1572 /* Make sure we use a session key for the signature check, EncryptMessage
1573 * always does that, even in the dummy case */
1574 ntlmssp_flags_save = helper->neg_flags;
1576 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1577 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1579 helper->neg_flags = ntlmssp_flags_save;
1581 return ret;
1584 static SecurityFunctionTableA ntlmTableA = {
1586 NULL, /* EnumerateSecurityPackagesA */
1587 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1588 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1589 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1590 NULL, /* Reserved2 */
1591 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1592 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1593 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1594 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1595 NULL, /* ApplyControlToken */
1596 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1597 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1598 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1599 ntlm_MakeSignature, /* MakeSignature */
1600 ntlm_VerifySignature, /* VerifySignature */
1601 FreeContextBuffer, /* FreeContextBuffer */
1602 NULL, /* QuerySecurityPackageInfoA */
1603 NULL, /* Reserved3 */
1604 NULL, /* Reserved4 */
1605 NULL, /* ExportSecurityContext */
1606 NULL, /* ImportSecurityContextA */
1607 NULL, /* AddCredentialsA */
1608 NULL, /* Reserved8 */
1609 NULL, /* QuerySecurityContextToken */
1610 ntlm_EncryptMessage, /* EncryptMessage */
1611 ntlm_DecryptMessage, /* DecryptMessage */
1612 NULL, /* SetContextAttributesA */
1615 static SecurityFunctionTableW ntlmTableW = {
1617 NULL, /* EnumerateSecurityPackagesW */
1618 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1619 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1620 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1621 NULL, /* Reserved2 */
1622 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1623 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1624 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1625 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1626 NULL, /* ApplyControlToken */
1627 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1628 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1629 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1630 ntlm_MakeSignature, /* MakeSignature */
1631 ntlm_VerifySignature, /* VerifySignature */
1632 FreeContextBuffer, /* FreeContextBuffer */
1633 NULL, /* QuerySecurityPackageInfoW */
1634 NULL, /* Reserved3 */
1635 NULL, /* Reserved4 */
1636 NULL, /* ExportSecurityContext */
1637 NULL, /* ImportSecurityContextW */
1638 NULL, /* AddCredentialsW */
1639 NULL, /* Reserved8 */
1640 NULL, /* QuerySecurityContextToken */
1641 ntlm_EncryptMessage, /* EncryptMessage */
1642 ntlm_DecryptMessage, /* DecryptMessage */
1643 NULL, /* SetContextAttributesW */
1646 #define NTLM_COMMENT \
1647 { 'N', 'T', 'L', 'M', ' ', \
1648 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1649 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1651 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1652 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1654 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1656 static char ntlm_name_A[] = NTLM_NAME;
1657 static WCHAR ntlm_name_W[] = NTLM_NAME;
1659 /* According to Windows, NTLM has the following capabilities. */
1660 #define CAPS ( \
1661 SECPKG_FLAG_INTEGRITY | \
1662 SECPKG_FLAG_PRIVACY | \
1663 SECPKG_FLAG_TOKEN_ONLY | \
1664 SECPKG_FLAG_CONNECTION | \
1665 SECPKG_FLAG_MULTI_REQUIRED | \
1666 SECPKG_FLAG_IMPERSONATION | \
1667 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1668 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1670 static const SecPkgInfoW infoW = {
1671 CAPS,
1673 RPC_C_AUTHN_WINNT,
1674 NTLM_MAX_BUF,
1675 ntlm_name_W,
1676 ntlm_comment_W
1679 static const SecPkgInfoA infoA = {
1680 CAPS,
1682 RPC_C_AUTHN_WINNT,
1683 NTLM_MAX_BUF,
1684 ntlm_name_A,
1685 ntlm_comment_A
1688 void SECUR32_initNTLMSP(void)
1690 SECURITY_STATUS ret;
1691 PNegoHelper helper;
1692 static CHAR version[] = "--version";
1694 SEC_CHAR *args[] = {
1695 ntlm_auth,
1696 version,
1697 NULL };
1699 if((ret = fork_helper(&helper, ntlm_auth, args)) != SEC_E_OK)
1701 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1702 helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1703 helper->version = -1;
1705 else
1706 check_version(helper);
1708 if(helper->version > 2)
1710 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1711 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1713 else
1715 ERR("%s was not found or is outdated. "
1716 "Make sure that ntlm_auth >= 3.x is in your path.\n",
1717 ntlm_auth);
1719 cleanup_helper(helper);