winpr/sspi: add possibility to set auth identity with Unicode password
authorKOVACS Krisztian <hidden@balabit.com>
Wed, 6 Dec 2017 15:30:57 +0000 (16:30 +0100)
committerKOVACS Krisztian <hidden@balabit.com>
Wed, 6 Dec 2017 15:30:57 +0000 (16:30 +0100)
To be able to avoid password conversion if the password is already unicode
this change adds the sspi_SetAuthIdentityWithUnicodePassword() function
that is identical to sspi_SetAuthIdentity() except that the password is
used without further conversions in the Unicode identity.

winpr/include/winpr/sspi.h
winpr/libwinpr/sspi/sspi_winpr.c

index a1e0b79..f7695df 100644 (file)
@@ -1134,6 +1134,9 @@ WINPR_API void sspi_SecBufferFree(PSecBuffer SecBuffer);
 
 WINPR_API int sspi_SetAuthIdentity(SEC_WINNT_AUTH_IDENTITY* identity, const char* user,
                                    const char* domain, const char* password);
+WINPR_API int sspi_SetAuthIdentityWithUnicodePassword(SEC_WINNT_AUTH_IDENTITY* identity,
+                                                      const char *user, const char *domain,
+                                                      LPWSTR password, ULONG passwordLength);
 WINPR_API int sspi_CopyAuthIdentity(SEC_WINNT_AUTH_IDENTITY* identity,
                                     SEC_WINNT_AUTH_IDENTITY* srcIdentity);
 
index a8b1d6a..095e9f6 100644 (file)
@@ -347,6 +347,21 @@ void sspi_SecureHandleFree(SecHandle* handle)
 int sspi_SetAuthIdentity(SEC_WINNT_AUTH_IDENTITY* identity, const char* user, const char* domain,
                          const char* password)
 {
+       int unicodePasswordLenW;
+       LPWSTR unicodePassword = NULL;
+
+       unicodePasswordLenW = ConvertToUnicode(CP_UTF8, 0, password, -1, &unicodePassword, 0);
+
+       if (unicodePasswordLenW <= 0)
+               return -1;
+
+       return sspi_SetAuthIdentityWithUnicodePassword(identity, user, domain, unicodePassword,
+                                                      (ULONG)(unicodePasswordLenW - 1));
+}
+
+int sspi_SetAuthIdentityWithUnicodePassword(SEC_WINNT_AUTH_IDENTITY* identity, const char *user,
+                                            const char *domain, LPWSTR password, ULONG passwordLength)
+{
        int status;
        identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
        free(identity->User);
@@ -378,18 +393,12 @@ int sspi_SetAuthIdentity(SEC_WINNT_AUTH_IDENTITY* identity, const char* user, co
        }
 
        free(identity->Password);
-       identity->Password = NULL;
-       identity->PasswordLength = 0;
-
-       if (password)
-       {
-               status = ConvertToUnicode(CP_UTF8, 0, password, -1, (LPWSTR*) & (identity->Password), 0);
-
-               if (status <= 0)
-                       return -1;
+       identity->Password = (UINT16*) calloc(1, (passwordLength + 1) * sizeof(WCHAR));
+       if (!identity->Password)
+               return -1;
 
-               identity->PasswordLength = (ULONG)(status - 1);
-       }
+       CopyMemory(identity->Password, password, passwordLength * sizeof(WCHAR));
+       identity->PasswordLength = passwordLength;
 
        return 1;
 }