From 3c56300afa1f33fbde1223b34a27c08bc05522ef Mon Sep 17 00:00:00 2001 From: KOVACS Krisztian Date: Wed, 6 Dec 2017 16:30:57 +0100 Subject: [PATCH] winpr/sspi: add possibility to set auth identity with Unicode password 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 | 3 +++ winpr/libwinpr/sspi/sspi_winpr.c | 31 ++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/winpr/include/winpr/sspi.h b/winpr/include/winpr/sspi.h index a1e0b79..f7695df 100644 --- a/winpr/include/winpr/sspi.h +++ b/winpr/include/winpr/sspi.h @@ -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); diff --git a/winpr/libwinpr/sspi/sspi_winpr.c b/winpr/libwinpr/sspi/sspi_winpr.c index a8b1d6a..095e9f6 100644 --- a/winpr/libwinpr/sspi/sspi_winpr.c +++ b/winpr/libwinpr/sspi/sspi_winpr.c @@ -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; } -- 2.7.4