secur32: Win64 printf format warning fixes.
[wine/dibdrv.git] / dlls / secur32 / ntlm.c
blob95c875845c2cd42d6a7b27acd4e08c8bbaa3db3e
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 /***********************************************************************
39 * QueryCredentialsAttributesA
41 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
42 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
44 SECURITY_STATUS ret;
46 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
48 if(ulAttribute == SECPKG_ATTR_NAMES)
50 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
51 ret = SEC_E_UNSUPPORTED_FUNCTION;
53 else
54 ret = SEC_E_UNSUPPORTED_FUNCTION;
56 return ret;
59 /***********************************************************************
60 * QueryCredentialsAttributesW
62 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
63 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
65 SECURITY_STATUS ret;
67 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
69 if(ulAttribute == SECPKG_ATTR_NAMES)
71 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
72 ret = SEC_E_UNSUPPORTED_FUNCTION;
74 else
75 ret = SEC_E_UNSUPPORTED_FUNCTION;
77 return ret;
80 /***********************************************************************
81 * AcquireCredentialsHandleW
83 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
84 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
85 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
86 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
88 SECURITY_STATUS ret;
89 PNegoHelper helper = NULL;
90 static CHAR ntlm_auth[] = "ntlm_auth",
91 server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp",
92 credentials_argv[] = "--use-cached-creds";
94 SEC_CHAR *client_user_arg = NULL;
95 SEC_CHAR *client_domain_arg = NULL;
96 SEC_WCHAR *username = NULL, *domain = NULL;
98 SEC_CHAR *client_argv[6];
99 SEC_CHAR *server_argv[] = { ntlm_auth,
100 server_helper_protocol,
101 NULL };
103 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
104 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
105 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
108 switch(fCredentialUse)
110 case SECPKG_CRED_INBOUND:
111 if( (ret = fork_helper(&helper, "ntlm_auth", server_argv)) !=
112 SEC_E_OK)
114 phCredential = NULL;
115 break;
117 else
119 helper->mode = NTLM_SERVER;
120 phCredential->dwUpper = fCredentialUse;
121 phCredential->dwLower = (ULONG_PTR)helper;
123 ret = SEC_E_OK;
124 break;
125 case SECPKG_CRED_OUTBOUND:
127 static const char username_arg[] = "--username=";
128 static const char domain_arg[] = "--domain=";
129 static char ntlm_auth[] = "ntlm_auth";
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;
1211 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1212 if (!phContext)
1213 return SEC_E_INVALID_HANDLE;
1215 if(fQOP)
1216 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1218 if(MessageSeqNo)
1219 FIXME("Ignoring MessageSeqNo\n");
1221 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2 ||
1222 pMessage->pBuffers[0].BufferType != SECBUFFER_TOKEN ||
1223 !pMessage->pBuffers[0].pvBuffer)
1224 return SEC_E_INVALID_TOKEN;
1226 if(pMessage->pBuffers[0].cbBuffer < 16)
1227 return SEC_E_BUFFER_TOO_SMALL;
1229 helper = (PNegoHelper)phContext->dwLower;
1230 TRACE("Negotiated flags are: 0x%08lx\n", helper->neg_flags);
1231 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
1233 FIXME("Can't handle NTLMv2 signing yet. Aborting.\n");
1234 return SEC_E_UNSUPPORTED_FUNCTION;
1236 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1238 PBYTE sig = pMessage->pBuffers[0].pvBuffer;
1239 ULONG crc = ComputeCrc32(pMessage->pBuffers[1].pvBuffer,
1240 pMessage->pBuffers[1].cbBuffer);
1242 sig[ 0] = (sign_version >> 0) & 0xff;
1243 sig[ 1] = (sign_version >> 8) & 0xff;
1244 sig[ 2] = (sign_version >> 16) & 0xff;
1245 sig[ 3] = (sign_version >> 24) & 0xff;
1246 memset(sig+4, 0, 4);
1247 sig[ 8] = (crc >> 0) & 0xff;
1248 sig[ 9] = (crc >> 8) & 0xff;
1249 sig[10] = (crc >> 16) & 0xff;
1250 sig[11] = (crc >> 24) & 0xff;
1251 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1252 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1253 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1254 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1256 ++(helper->crypt.ntlm.seq_num);
1258 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1259 return SEC_E_OK;
1262 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1264 FIXME("Can't handle encrypted session key yet. Aborting.\n");
1265 return SEC_E_UNSUPPORTED_FUNCTION;
1268 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1270 TRACE("Generating dummy signature\n");
1271 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1272 memset(pMessage->pBuffers[0].pvBuffer, 0, 16);
1273 memset(pMessage->pBuffers[0].pvBuffer, 0x01, 1);
1274 pMessage->pBuffers[0].cbBuffer = 16;
1275 return SEC_E_OK;
1278 return SEC_E_UNSUPPORTED_FUNCTION;
1281 /***********************************************************************
1282 * VerifySignature
1284 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1285 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1287 PNegoHelper helper;
1288 ULONG fQOP = 0;
1290 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1291 if(!phContext)
1292 return SEC_E_INVALID_HANDLE;
1294 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2 ||
1295 pMessage->pBuffers[0].BufferType != SECBUFFER_TOKEN ||
1296 !pMessage->pBuffers[0].pvBuffer)
1297 return SEC_E_INVALID_TOKEN;
1299 if(pMessage->pBuffers[0].cbBuffer < 16)
1300 return SEC_E_BUFFER_TOO_SMALL;
1302 if(MessageSeqNo)
1303 FIXME("Ignoring MessageSeqNo\n");
1305 helper = (PNegoHelper)phContext->dwLower;
1306 TRACE("Negotiated flags: 0x%08lx\n", helper->neg_flags);
1308 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
1310 FIXME("Can't handle NTLMv2 signing yet. Aborting.\n");
1311 return SEC_E_UNSUPPORTED_FUNCTION;
1314 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1316 SecBufferDesc local_desc;
1317 SecBuffer local_buff[2];
1318 BYTE local_sig[16];
1320 local_desc.ulVersion = SECBUFFER_VERSION;
1321 local_desc.cBuffers = 2;
1322 local_desc.pBuffers = local_buff;
1323 local_buff[0].BufferType = SECBUFFER_TOKEN;
1324 local_buff[0].cbBuffer = 16;
1325 local_buff[0].pvBuffer = local_sig;
1326 local_buff[1].BufferType = SECBUFFER_DATA;
1327 local_buff[1].cbBuffer = pMessage->pBuffers[1].cbBuffer;
1328 local_buff[1].pvBuffer = pMessage->pBuffers[1].pvBuffer;
1330 ntlm_MakeSignature(phContext, fQOP, &local_desc, MessageSeqNo);
1332 if(memcmp(((PBYTE)local_buff[0].pvBuffer) + 8, ((PBYTE)pMessage->pBuffers[0].pvBuffer) + 8, 8))
1333 return SEC_E_MESSAGE_ALTERED;
1335 return SEC_E_OK;
1338 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1340 FIXME("Can't handle encrypted session keys yet. Aborting.\n");
1341 return SEC_E_UNSUPPORTED_FUNCTION;
1344 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1346 const BYTE dummy_sig[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1347 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1348 TRACE("Assuming dummy signature.\n");
1349 if(memcmp(pMessage->pBuffers[0].pvBuffer, dummy_sig, sizeof(dummy_sig)) != 0)
1351 TRACE("Failed to verify the packet signature. Not a dummy signature?\n");
1352 return SEC_E_MESSAGE_ALTERED;
1354 else
1356 pfQOP = &fQOP;
1357 return SEC_E_OK;
1361 return SEC_E_UNSUPPORTED_FUNCTION;
1364 /***********************************************************************
1365 * FreeCredentialsHandle
1367 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1368 PCredHandle phCredential)
1370 SECURITY_STATUS ret;
1372 if(phCredential){
1373 PNegoHelper helper = (PNegoHelper) phCredential->dwLower;
1374 phCredential->dwUpper = 0;
1375 phCredential->dwLower = 0;
1376 HeapFree(GetProcessHeap(), 0, helper->session_key);
1377 cleanup_helper(helper);
1378 ret = SEC_E_OK;
1380 else
1381 ret = SEC_E_OK;
1383 return ret;
1386 /***********************************************************************
1387 * EncryptMessage
1389 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1390 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1392 PNegoHelper helper;
1393 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1395 if(!phContext)
1396 return SEC_E_INVALID_HANDLE;
1398 if(fQOP)
1399 FIXME("Ignoring fQOP\n");
1401 if(MessageSeqNo)
1402 FIXME("Ignoring MessageSeqNo\n");
1404 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2 ||
1405 pMessage->pBuffers[0].BufferType != SECBUFFER_TOKEN ||
1406 !pMessage->pBuffers[0].pvBuffer)
1407 return SEC_E_INVALID_TOKEN;
1409 if(pMessage->pBuffers[0].cbBuffer < 16)
1410 return SEC_E_BUFFER_TOO_SMALL;
1412 helper = (PNegoHelper) phContext->dwLower;
1414 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
1416 FIXME("Can't handle NTLMv2 encryption yet, aborting\n");
1417 return SEC_E_UNSUPPORTED_FUNCTION;
1419 else
1421 PBYTE sig = pMessage->pBuffers[0].pvBuffer;
1422 ULONG crc = ComputeCrc32(pMessage->pBuffers[1].pvBuffer,
1423 pMessage->pBuffers[1].cbBuffer);
1424 ULONG sign_version = 1l;
1426 sig[ 0] = (sign_version >> 0) & 0xff;
1427 sig[ 1] = (sign_version >> 8) & 0xff;
1428 sig[ 2] = (sign_version >> 16) & 0xff;
1429 sig[ 3] = (sign_version >> 24) & 0xff;
1430 memset(sig+4, 0, 4);
1431 sig[ 8] = (crc >> 0) & 0xff;
1432 sig[ 9] = (crc >> 8) & 0xff;
1433 sig[10] = (crc >> 16) & 0xff;
1434 sig[11] = (crc >> 24) & 0xff;
1435 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1436 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1437 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1438 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1440 SECUR32_arc4Process(helper->crypt.ntlm.a4i, pMessage->pBuffers[1].pvBuffer,
1441 pMessage->pBuffers[1].cbBuffer);
1442 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1444 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1445 memset(sig+4, 0, 4);
1447 ++(helper->crypt.ntlm.seq_num);
1451 return SEC_E_OK;
1454 /***********************************************************************
1455 * DecryptMessage
1457 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1458 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1460 SECURITY_STATUS ret;
1461 ULONG ntlmssp_flags_save;
1462 PNegoHelper helper;
1463 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1465 if(!phContext)
1466 return SEC_E_INVALID_HANDLE;
1468 if(MessageSeqNo)
1469 FIXME("Ignoring MessageSeqNo\n");
1471 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2 ||
1472 pMessage->pBuffers[0].BufferType != SECBUFFER_TOKEN ||
1473 !pMessage->pBuffers[0].pvBuffer)
1474 return SEC_E_INVALID_TOKEN;
1476 if(pMessage->pBuffers[0].cbBuffer < 16)
1477 return SEC_E_BUFFER_TOO_SMALL;
1479 helper = (PNegoHelper) phContext->dwLower;
1481 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
1483 FIXME("Can't handle NTLMv2 encryption yet, aborting\n");
1484 return SEC_E_UNSUPPORTED_FUNCTION;
1486 else
1488 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1489 pMessage->pBuffers[1].pvBuffer, pMessage->pBuffers[1].cbBuffer);
1492 /* Make sure we use a session key for the signature check, EncryptMessage
1493 * always does that, even in the dummy case */
1494 ntlmssp_flags_save = helper->neg_flags;
1496 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1497 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1499 helper->neg_flags = ntlmssp_flags_save;
1501 return ret;
1504 static SecurityFunctionTableA ntlmTableA = {
1506 NULL, /* EnumerateSecurityPackagesA */
1507 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1508 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1509 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1510 NULL, /* Reserved2 */
1511 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1512 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1513 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1514 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1515 NULL, /* ApplyControlToken */
1516 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1517 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1518 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1519 ntlm_MakeSignature, /* MakeSignature */
1520 ntlm_VerifySignature, /* VerifySignature */
1521 FreeContextBuffer, /* FreeContextBuffer */
1522 NULL, /* QuerySecurityPackageInfoA */
1523 NULL, /* Reserved3 */
1524 NULL, /* Reserved4 */
1525 NULL, /* ExportSecurityContext */
1526 NULL, /* ImportSecurityContextA */
1527 NULL, /* AddCredentialsA */
1528 NULL, /* Reserved8 */
1529 NULL, /* QuerySecurityContextToken */
1530 ntlm_EncryptMessage, /* EncryptMessage */
1531 ntlm_DecryptMessage, /* DecryptMessage */
1532 NULL, /* SetContextAttributesA */
1535 static SecurityFunctionTableW ntlmTableW = {
1537 NULL, /* EnumerateSecurityPackagesW */
1538 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1539 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1540 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1541 NULL, /* Reserved2 */
1542 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1543 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1544 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1545 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1546 NULL, /* ApplyControlToken */
1547 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1548 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1549 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1550 ntlm_MakeSignature, /* MakeSignature */
1551 ntlm_VerifySignature, /* VerifySignature */
1552 FreeContextBuffer, /* FreeContextBuffer */
1553 NULL, /* QuerySecurityPackageInfoW */
1554 NULL, /* Reserved3 */
1555 NULL, /* Reserved4 */
1556 NULL, /* ExportSecurityContext */
1557 NULL, /* ImportSecurityContextW */
1558 NULL, /* AddCredentialsW */
1559 NULL, /* Reserved8 */
1560 NULL, /* QuerySecurityContextToken */
1561 ntlm_EncryptMessage, /* EncryptMessage */
1562 ntlm_DecryptMessage, /* DecryptMessage */
1563 NULL, /* SetContextAttributesW */
1566 #define NTLM_COMMENT \
1567 { 'N', 'T', 'L', 'M', ' ', \
1568 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1569 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1571 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1572 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1574 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1576 static char ntlm_name_A[] = NTLM_NAME;
1577 static WCHAR ntlm_name_W[] = NTLM_NAME;
1579 /* According to Windows, NTLM has the following capabilities. */
1580 #define CAPS ( \
1581 SECPKG_FLAG_INTEGRITY | \
1582 SECPKG_FLAG_PRIVACY | \
1583 SECPKG_FLAG_TOKEN_ONLY | \
1584 SECPKG_FLAG_CONNECTION | \
1585 SECPKG_FLAG_MULTI_REQUIRED | \
1586 SECPKG_FLAG_IMPERSONATION | \
1587 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1588 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1590 static const SecPkgInfoW infoW = {
1591 CAPS,
1593 RPC_C_AUTHN_WINNT,
1594 NTLM_MAX_BUF,
1595 ntlm_name_W,
1596 ntlm_comment_W
1599 static const SecPkgInfoA infoA = {
1600 CAPS,
1602 RPC_C_AUTHN_WINNT,
1603 NTLM_MAX_BUF,
1604 ntlm_name_A,
1605 ntlm_comment_A
1608 void SECUR32_initNTLMSP(void)
1610 SECURITY_STATUS ret;
1611 PNegoHelper helper;
1612 static CHAR ntlm_auth[] = "ntlm_auth",
1613 version[] = "--version";
1615 SEC_CHAR *args[] = {
1616 ntlm_auth,
1617 version,
1618 NULL };
1620 if((ret = fork_helper(&helper, "ntlm_auth", args)) != SEC_E_OK)
1622 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1623 helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1624 helper->version = -1;
1626 else
1627 check_version(helper);
1629 if(helper->version > 2)
1631 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1632 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1634 else
1636 ERR("ntlm_auth was not found or is outdated. "
1637 "Make sure that ntlm_auth >= 3.x is in your path.\n");
1639 cleanup_helper(helper);