From 84e4f321118a11991a34e24dd1729181ad8a1574 Mon Sep 17 00:00:00 2001
From: David Woodhouse <dwmw2@infradead.org>
Date: Fri, 8 Aug 2014 13:21:56 +0100
Subject: secur32: Fix handling of ANSI NTLM credentials

One of many issues covered in bug 37063... we assume that the
credentials are in Unicode, instead of looking at the Flags field.
---
 dlls/secur32/ntlm.c | 69 ++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 58 insertions(+), 11 deletions(-)

diff --git a/dlls/secur32/ntlm.c b/dlls/secur32/ntlm.c
index 0fe64ed..72e9706 100644
--- a/dlls/secur32/ntlm.c
+++ b/dlls/secur32/ntlm.c
@@ -174,27 +174,74 @@ SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
                 if(pAuthData != NULL)
                 {
                     PSEC_WINNT_AUTH_IDENTITY_W auth_data = pAuthData;
+                    LPWSTR domain = NULL, user = NULL, password = NULL;
+                    int domain_len = 0, user_len = 0, password_len = 0;
 
-                    TRACE("Username is %s\n", debugstr_wn(auth_data->User, auth_data->UserLength));
-                    TRACE("Domain name is %s\n", debugstr_wn(auth_data->Domain, auth_data->DomainLength));
+                    if (auth_data->Flags & SEC_WINNT_AUTH_IDENTITY_ANSI)
+                    {
+                        if (auth_data->DomainLength)
+                        {
+                            domain_len = MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->Domain,
+                                                             auth_data->DomainLength, NULL, 0);
+                            domain = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * domain_len);
+                            MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->Domain, auth_data->DomainLength,
+                                                domain, domain_len);
+                        }
+
+                        if (auth_data->UserLength)
+                        {
+                            user_len = MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->User,
+                                                           auth_data->UserLength, NULL, 0);
+                            user = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * user_len);
+                            MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->User, auth_data->UserLength,
+                                                user, user_len);
+                        }
+
+                        if (auth_data->PasswordLength)
+                        {
+                            password_len = MultiByteToWideChar(CP_ACP, 0,(char *)auth_data->Password,
+                                                               auth_data->PasswordLength, NULL, 0);
+                            password = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * password_len);
+                            MultiByteToWideChar(CP_ACP, 0, (char *)auth_data->Password, auth_data->PasswordLength,
+                                                password, password_len);
+                        }
+                    }
+                    else
+                    {
+                        domain = auth_data->Domain;
+                        domain_len = auth_data->DomainLength;
+
+                        user = auth_data->User;
+                        user_len = auth_data->UserLength;
+
+                        password = auth_data->Password;
+                        password_len = auth_data->PasswordLength;
+                    }
+
+                    TRACE("Username is %s\n", debugstr_wn(user, user_len));
+                    TRACE("Domain name is %s\n", debugstr_wn(domain, domain_len));
 
-                    ntlm_cred->username_arg = ntlm_GetUsernameArg(auth_data->User, auth_data->UserLength);
-                    ntlm_cred->domain_arg = ntlm_GetDomainArg(auth_data->Domain, auth_data->DomainLength);
+                    ntlm_cred->username_arg = ntlm_GetUsernameArg(user, user_len);
+                    ntlm_cred->domain_arg = ntlm_GetDomainArg(domain, domain_len);
 
-                    if(auth_data->PasswordLength != 0)
+                    if(password_len != 0)
                     {
-                        ntlm_cred->pwlen = WideCharToMultiByte(CP_UNIXCP,
-                                                               WC_NO_BEST_FIT_CHARS, auth_data->Password,
-                                                               auth_data->PasswordLength, NULL, 0, NULL,
-                                                               NULL);
+                        ntlm_cred->pwlen = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, password,
+                                                               password_len, NULL, 0, NULL, NULL);
 
                         ntlm_cred->password = HeapAlloc(GetProcessHeap(), 0,
                                                         ntlm_cred->pwlen);
 
-                        WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
-                                            auth_data->Password, auth_data->PasswordLength,
+                        WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, password, password_len,
                                             ntlm_cred->password, ntlm_cred->pwlen, NULL, NULL);
                     }
+
+                    if (auth_data->Flags & SEC_WINNT_AUTH_IDENTITY_ANSI)
+                    {
+                        HeapFree(GetProcessHeap(), 0, domain);
+                        HeapFree(GetProcessHeap(), 0, user);
+                        HeapFree(GetProcessHeap(), 0, password);
+                    }
                 }
 
                 phCredential->dwUpper = fCredentialUse;
-- 
2.3.3

