thunks.c 29.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (C) 2004 Juan Lang
 *
 * This file implements thunks between wide char and multibyte functions for
 * SSPs that only provide one or the other.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
 */
20

21 22 23 24 25 26 27 28
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "winternl.h"
#include "sspi.h"

#include "wine/debug.h"
29 30
#include "secur32_priv.h"
#include "thunks.h"
31 32 33 34 35 36 37 38 39 40

WINE_DEFAULT_DEBUG_CHANNEL(secur32);

SECURITY_STATUS SEC_ENTRY thunk_AcquireCredentialsHandleA(
 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialsUse,
 PLUID pvLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
 PVOID pvGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
{
    SECURITY_STATUS ret;

41
    TRACE("%s %s %d %p %p %p %p %p %p\n", debugstr_a(pszPrincipal),
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
     debugstr_a(pszPackage), fCredentialsUse, pvLogonID, pAuthData, pGetKeyFn,
     pvGetKeyArgument, phCredential, ptsExpiry);
    if (pszPackage)
    {
        UNICODE_STRING principal, package;

        RtlCreateUnicodeStringFromAsciiz(&principal, pszPrincipal);
        RtlCreateUnicodeStringFromAsciiz(&package, pszPackage);
        ret = AcquireCredentialsHandleW(principal.Buffer, package.Buffer,
         fCredentialsUse, pvLogonID, pAuthData, pGetKeyFn, pvGetKeyArgument,
         phCredential, ptsExpiry);
        RtlFreeUnicodeString(&principal);
        RtlFreeUnicodeString(&package);
    }
    else
        ret = SEC_E_SECPKG_NOT_FOUND;
    return ret;
}

61 62 63 64 65 66 67 68 69 70 71 72
static char *strdupWA( const WCHAR *str )
{
    char *ret = NULL;
    if (str)
    {
        int len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
        if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, len )))
            WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
    }
    return ret;
}

73 74 75 76 77 78 79
SECURITY_STATUS SEC_ENTRY thunk_AcquireCredentialsHandleW(
 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialsUse,
 PLUID pvLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
 PVOID pvGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
{
    SECURITY_STATUS ret;

80
    TRACE("%s %s %d %p %p %p %p %p %p\n", debugstr_w(pszPrincipal),
81 82 83 84
     debugstr_w(pszPackage), fCredentialsUse, pvLogonID, pAuthData, pGetKeyFn,
     pvGetKeyArgument, phCredential, ptsExpiry);
    if (pszPackage)
    {
85
        char *principal, *package;
86

87 88
        principal = strdupWA(pszPrincipal);
        package = strdupWA(pszPackage);
89
        ret = AcquireCredentialsHandleA(principal, package, fCredentialsUse,
90 91 92
         pvLogonID, pAuthData, pGetKeyFn, pvGetKeyArgument, phCredential, ptsExpiry);
        RtlFreeHeap(GetProcessHeap(), 0, principal);
        RtlFreeHeap(GetProcessHeap(), 0, package);
93 94 95 96 97 98 99 100 101 102
    }
    else
        ret = SEC_E_SECPKG_NOT_FOUND;
    return ret;
}

/* thunking is pretty dicey for these--the output type depends on ulAttribute,
 * so we have to know about every type the caller does
 */
SECURITY_STATUS SEC_ENTRY thunk_QueryCredentialsAttributesA(
103
 PCredHandle phCredential, ULONG ulAttribute, void *pBuffer)
104 105 106
{
    SECURITY_STATUS ret;

107
    TRACE("%p %d %p\n", phCredential, ulAttribute, pBuffer);
108 109 110 111 112 113 114 115 116
    if (phCredential)
    {
        SecurePackage *package = (SecurePackage *)phCredential->dwUpper;
        PCredHandle cred = (PCredHandle)phCredential->dwLower;

        if (package && package->provider)
        {
            if (package->provider->fnTableW.QueryCredentialsAttributesW)
            {
117
                ret = package->provider->fnTableW.QueryCredentialsAttributesW( cred, ulAttribute, pBuffer);
118 119 120 121 122 123
                if (ret == SEC_E_OK)
                {
                    switch (ulAttribute)
                    {
                        case SECPKG_CRED_ATTR_NAMES:
                        {
124
                            PSecPkgCredentials_NamesW names = (PSecPkgCredentials_NamesW)pBuffer;
125 126 127 128
                            SEC_WCHAR *oldUser = names->sUserName;

                            if (oldUser)
                            {
129 130
                                names->sUserName = (WCHAR *)strdupWA(oldUser);
                                package->provider->fnTableW.FreeContextBuffer(oldUser);
131 132 133 134
                            }
                            break;
                        }
                        default:
135
                            WARN("attribute type %d unknown\n", ulAttribute);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
                            ret = SEC_E_INTERNAL_ERROR;
                    }
                }
            }
            else
                ret = SEC_E_UNSUPPORTED_FUNCTION;
        }
        else
            ret = SEC_E_INVALID_HANDLE;
    }
    else
        ret = SEC_E_INVALID_HANDLE;
    return ret;
}

151 152 153 154 155 156 157 158 159 160 161 162
static WCHAR *strdupAW( const char *str )
{
    WCHAR *ret = NULL;
    if (str)
    {
        int len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
        if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
            MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
    }
    return ret;
}

163
SECURITY_STATUS SEC_ENTRY thunk_QueryCredentialsAttributesW(
164
 PCredHandle phCredential, ULONG ulAttribute, void *pBuffer)
165 166 167
{
    SECURITY_STATUS ret;

168
    TRACE("%p %d %p\n", phCredential, ulAttribute, pBuffer);
169 170 171 172 173 174 175 176 177
    if (phCredential)
    {
        SecurePackage *package = (SecurePackage *)phCredential->dwUpper;
        PCredHandle cred = (PCredHandle)phCredential->dwLower;

        if (package && package->provider)
        {
            if (package->provider->fnTableA.QueryCredentialsAttributesA)
            {
178
                ret = package->provider->fnTableA.QueryCredentialsAttributesA(cred, ulAttribute, pBuffer);
179 180 181 182 183 184
                if (ret == SEC_E_OK)
                {
                    switch (ulAttribute)
                    {
                        case SECPKG_CRED_ATTR_NAMES:
                        {
185
                            PSecPkgCredentials_NamesA names = (PSecPkgCredentials_NamesA)pBuffer;
186 187 188 189
                            SEC_CHAR *oldUser = names->sUserName;

                            if (oldUser)
                            {
190 191
                                names->sUserName = (char *)strdupAW(oldUser);
                                package->provider->fnTableA.FreeContextBuffer(oldUser);
192 193 194 195
                            }
                            break;
                        }
                        default:
196
                            WARN("attribute type %d unknown\n", ulAttribute);
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
                            ret = SEC_E_INTERNAL_ERROR;
                    }
                }
            }
            else
                ret = SEC_E_UNSUPPORTED_FUNCTION;
        }
        else
            ret = SEC_E_INVALID_HANDLE;
    }
    else
        ret = SEC_E_INVALID_HANDLE;
    return ret;
}

SECURITY_STATUS SEC_ENTRY thunk_InitializeSecurityContextA(
 PCredHandle phCredential, PCtxtHandle phContext,
214 215 216 217
 SEC_CHAR *pszTargetName, ULONG fContextReq,
 ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput,
 ULONG Reserved2, PCtxtHandle phNewContext, PSecBufferDesc pOutput,
 ULONG *pfContextAttr, PTimeStamp ptsExpiry)
218 219 220
{
    SECURITY_STATUS ret;

221
    TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
     debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
     Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
    if (phCredential)
    {
        SecurePackage *package = (SecurePackage *)phCredential->dwUpper;

        if (package && package->provider)
        {
            if (package->provider->fnTableW.InitializeSecurityContextW)
            {
                UNICODE_STRING target;

                RtlCreateUnicodeStringFromAsciiz(&target, pszTargetName);
                ret = package->provider->fnTableW.InitializeSecurityContextW(
                 phCredential, phContext, target.Buffer, fContextReq, Reserved1,
                 TargetDataRep, pInput, Reserved2, phNewContext, pOutput,
                 pfContextAttr, ptsExpiry);
                RtlFreeUnicodeString(&target);
            }
            else
                ret = SEC_E_UNSUPPORTED_FUNCTION;
        }
        else
            ret = SEC_E_INVALID_HANDLE;
    }
    else
        ret = SEC_E_INVALID_HANDLE;
    return ret;
}

SECURITY_STATUS SEC_ENTRY thunk_InitializeSecurityContextW(
 PCredHandle phCredential, PCtxtHandle phContext,
254 255 256 257
 SEC_WCHAR *pszTargetName, ULONG fContextReq,
 ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput,
 ULONG Reserved2, PCtxtHandle phNewContext, PSecBufferDesc pOutput,
 ULONG *pfContextAttr, PTimeStamp ptsExpiry)
258 259 260
{
    SECURITY_STATUS ret;

261
    TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
262 263 264 265 266 267 268 269 270 271
     debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
     Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
    if (phCredential)
    {
        SecurePackage *package = (SecurePackage *)phCredential->dwUpper;

        if (package && package->provider)
        {
            if (package->provider->fnTableA.InitializeSecurityContextA)
            {
272
                char *target = strdupWA(pszTargetName);
273 274 275 276 277

                ret = package->provider->fnTableA.InitializeSecurityContextA(
                 phCredential, phContext, target, fContextReq, Reserved1,
                 TargetDataRep, pInput, Reserved2, phNewContext, pOutput,
                 pfContextAttr, ptsExpiry);
278
                RtlFreeHeap(GetProcessHeap(), 0, target);
279 280 281 282 283 284 285 286 287 288 289 290 291
            }
            else
                ret = SEC_E_UNSUPPORTED_FUNCTION;
        }
        else
            ret = SEC_E_INVALID_HANDLE;
    }
    else
        ret = SEC_E_INVALID_HANDLE;
    return ret;
}

SECURITY_STATUS SEC_ENTRY thunk_AddCredentialsA(PCredHandle hCredentials,
292
 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
293 294 295 296 297
 void *pAuthData, SEC_GET_KEY_FN pGetKeyFn, void *pvGetKeyArgument,
 PTimeStamp ptsExpiry)
{
    SECURITY_STATUS ret;

298
    TRACE("%p %s %s %d %p %p %p %p\n", hCredentials, debugstr_a(pszPrincipal),
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
     debugstr_a(pszPackage), fCredentialUse, pAuthData, pGetKeyFn,
     pvGetKeyArgument, ptsExpiry);
    if (hCredentials)
    {
        SecurePackage *package = (SecurePackage *)hCredentials->dwUpper;
        PCredHandle cred = (PCtxtHandle)hCredentials->dwLower;

        if (package && package->provider)
        {
            if (package->provider->fnTableW.AddCredentialsW)
            {
                UNICODE_STRING szPrincipal, szPackage;

                RtlCreateUnicodeStringFromAsciiz(&szPrincipal, pszPrincipal);
                RtlCreateUnicodeStringFromAsciiz(&szPackage, pszPackage);
                ret = package->provider->fnTableW.AddCredentialsW(
                 cred, szPrincipal.Buffer, szPackage.Buffer, fCredentialUse,
                 pAuthData, pGetKeyFn, pvGetKeyArgument, ptsExpiry);
                RtlFreeUnicodeString(&szPrincipal);
                RtlFreeUnicodeString(&szPackage);
            }
            else
                ret = SEC_E_UNSUPPORTED_FUNCTION;
        }
        else
            ret = SEC_E_INVALID_HANDLE;
    }
    else
        ret = SEC_E_INVALID_HANDLE;
    return ret;
}

SECURITY_STATUS SEC_ENTRY thunk_AddCredentialsW(PCredHandle hCredentials,
332
 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
333 334 335 336 337
 void *pAuthData, SEC_GET_KEY_FN pGetKeyFn, void *pvGetKeyArgument,
 PTimeStamp ptsExpiry)
{
    SECURITY_STATUS ret;

338
    TRACE("%p %s %s %d %p %p %p %p\n", hCredentials, debugstr_w(pszPrincipal),
339 340 341 342 343 344 345 346 347 348 349
     debugstr_w(pszPackage), fCredentialUse, pAuthData, pGetKeyFn,
     pvGetKeyArgument, ptsExpiry);
    if (hCredentials)
    {
        SecurePackage *package = (SecurePackage *)hCredentials->dwUpper;
        PCredHandle cred = (PCtxtHandle)hCredentials->dwLower;

        if (package && package->provider)
        {
            if (package->provider->fnTableA.AddCredentialsA)
            {
350 351
                char *szPrincipal = strdupWA(pszPrincipal);
                char *szPackage = strdupWA(pszPackage);
352 353 354 355

                ret = package->provider->fnTableA.AddCredentialsA(
                 cred, szPrincipal, szPackage, fCredentialUse, pAuthData,
                 pGetKeyFn, pvGetKeyArgument, ptsExpiry);
356 357
                RtlFreeHeap(GetProcessHeap(), 0, szPrincipal);
                RtlFreeHeap(GetProcessHeap(), 0, szPackage);
358 359 360 361 362 363 364 365 366 367 368 369
            }
            else
                ret = SEC_E_UNSUPPORTED_FUNCTION;
        }
        else
            ret = SEC_E_INVALID_HANDLE;
    }
    else
        ret = SEC_E_INVALID_HANDLE;
    return ret;
}

370
static PSecPkgInfoA _copyPackageInfoFlatWToA(const SecPkgInfoW *infoW)
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
{
    PSecPkgInfoA ret;

    if (infoW)
    {
        size_t bytesNeeded = sizeof(SecPkgInfoA);
        int nameLen = 0, commentLen = 0;

        if (infoW->Name)
        {
            nameLen = WideCharToMultiByte(CP_ACP, 0, infoW->Name, -1,
             NULL, 0, NULL, NULL);
            bytesNeeded += nameLen;
        }
        if (infoW->Comment)
        {
            commentLen = WideCharToMultiByte(CP_ACP, 0, infoW->Comment, -1,
             NULL, 0, NULL, NULL);
            bytesNeeded += commentLen;
        }
391
        if ((ret = RtlAllocateHeap(GetProcessHeap(), 0, bytesNeeded)))
392
        {
393
            char *nextString = (char *)ret + sizeof(SecPkgInfoA);
394 395 396 397 398

            memcpy(ret, infoW, sizeof(SecPkgInfoA));
            if (infoW->Name)
            {
                ret->Name = nextString;
399
                WideCharToMultiByte(CP_ACP, 0, infoW->Name, -1, nextString, nameLen, NULL, NULL);
400 401 402 403 404 405 406
                nextString += nameLen;
            }
            else
                ret->Name = NULL;
            if (infoW->Comment)
            {
                ret->Comment = nextString;
407
                WideCharToMultiByte(CP_ACP, 0, infoW->Comment, -1, nextString, nameLen, NULL, NULL);
408 409 410 411 412 413 414 415 416 417 418
            }
            else
                ret->Comment = NULL;
        }
    }
    else
        ret = NULL;
    return ret;
}

static SECURITY_STATUS thunk_ContextAttributesWToA(SecurePackage *package,
419
 ULONG ulAttribute, void *pBuffer)
420 421 422 423 424 425 426 427 428 429 430 431 432 433
{
    SECURITY_STATUS ret = SEC_E_OK;

    if (package && pBuffer)
    {
        switch (ulAttribute)
        {
            case SECPKG_ATTR_NAMES:
            {
                PSecPkgContext_NamesW names = (PSecPkgContext_NamesW)pBuffer;
                SEC_WCHAR *oldUser = names->sUserName;

                if (oldUser)
                {
434
                    names->sUserName = (WCHAR *)strdupWA(oldUser);
435 436 437 438 439 440
                    package->provider->fnTableW.FreeContextBuffer(oldUser);
                }
                break;
            }
            case SECPKG_ATTR_AUTHORITY:
            {
441
                PSecPkgContext_AuthorityW names = (PSecPkgContext_AuthorityW)pBuffer;
442 443 444 445
                SEC_WCHAR *oldAuth = names->sAuthorityName;

                if (oldAuth)
                {
446
                    names->sAuthorityName = (WCHAR *)strdupWA(oldAuth);
447 448 449 450 451 452 453 454 455 456 457 458
                    package->provider->fnTableW.FreeContextBuffer(oldAuth);
                }
                break;
            }
            case SECPKG_ATTR_KEY_INFO:
            {
                PSecPkgContext_KeyInfoW info = (PSecPkgContext_KeyInfoW)pBuffer;
                SEC_WCHAR *oldSigAlgName = info->sSignatureAlgorithmName;
                SEC_WCHAR *oldEncAlgName = info->sEncryptAlgorithmName;

                if (oldSigAlgName)
                {
459 460
                    info->sSignatureAlgorithmName = (WCHAR *)strdupWA(oldSigAlgName);
                    package->provider->fnTableW.FreeContextBuffer(oldSigAlgName);
461 462 463
                }
                if (oldEncAlgName)
                {
464 465
                    info->sEncryptAlgorithmName = (WCHAR *)strdupWA(oldEncAlgName);
                    package->provider->fnTableW.FreeContextBuffer(oldEncAlgName);
466 467 468 469 470
                }
                break;
            }
            case SECPKG_ATTR_PACKAGE_INFO:
            {
471
                PSecPkgContext_PackageInfoW info = (PSecPkgContext_PackageInfoW)pBuffer;
472 473 474 475 476 477 478 479 480 481 482 483
                PSecPkgInfoW oldPkgInfo = info->PackageInfo;

                if (oldPkgInfo)
                {
                    info->PackageInfo = (PSecPkgInfoW)
                     _copyPackageInfoFlatWToA(oldPkgInfo);
                    package->provider->fnTableW.FreeContextBuffer(oldPkgInfo);
                }
                break;
            }
            case SECPKG_ATTR_NEGOTIATION_INFO:
            {
484
                PSecPkgContext_NegotiationInfoW info = (PSecPkgContext_NegotiationInfoW)pBuffer;
485 486 487 488 489 490 491 492 493 494 495 496
                PSecPkgInfoW oldPkgInfo = info->PackageInfo;

                if (oldPkgInfo)
                {
                    info->PackageInfo = (PSecPkgInfoW)
                     _copyPackageInfoFlatWToA(oldPkgInfo);
                    package->provider->fnTableW.FreeContextBuffer(oldPkgInfo);
                }
                break;
            }
            case SECPKG_ATTR_NATIVE_NAMES:
            {
497 498 499
                PSecPkgContext_NativeNamesW names = (PSecPkgContext_NativeNamesW)pBuffer;
                WCHAR *oldClient = names->sClientName;
                WCHAR *oldServer = names->sServerName;
500 501 502

                if (oldClient)
                {
503
                    names->sClientName = (WCHAR *)strdupWA(oldClient);
504 505 506 507
                    package->provider->fnTableW.FreeContextBuffer(oldClient);
                }
                if (oldServer)
                {
508
                    names->sServerName = (WCHAR *)strdupWA(oldServer);
509 510 511 512 513 514
                    package->provider->fnTableW.FreeContextBuffer(oldServer);
                }
                break;
            }
            case SECPKG_ATTR_CREDENTIAL_NAME:
            {
515 516
                PSecPkgContext_CredentialNameW name = (PSecPkgContext_CredentialNameW)pBuffer;
                WCHAR *oldCred = name->sCredentialName;
517 518 519

                if (oldCred)
                {
520
                    name->sCredentialName = (WCHAR *)strdupWA(oldCred);
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
                    package->provider->fnTableW.FreeContextBuffer(oldCred);
                }
                break;
            }
            /* no thunking needed: */
            case SECPKG_ATTR_ACCESS_TOKEN:
            case SECPKG_ATTR_DCE_INFO:
            case SECPKG_ATTR_FLAGS:
            case SECPKG_ATTR_LIFESPAN:
            case SECPKG_ATTR_PASSWORD_EXPIRY:
            case SECPKG_ATTR_SESSION_KEY:
            case SECPKG_ATTR_SIZES:
            case SECPKG_ATTR_STREAM_SIZES:
            case SECPKG_ATTR_TARGET_INFORMATION:
                break;
            default:
537
                WARN("attribute type %d unknown\n", ulAttribute);
538 539 540 541 542 543 544 545 546
                ret = SEC_E_INTERNAL_ERROR;
        }
    }
    else
        ret = SEC_E_INVALID_TOKEN;
    return ret;
}

SECURITY_STATUS SEC_ENTRY thunk_QueryContextAttributesA(PCtxtHandle phContext,
547
 ULONG ulAttribute, void *pBuffer)
548 549 550
{
    SECURITY_STATUS ret;

551
    TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
552 553 554 555 556 557 558 559 560
    if (phContext)
    {
        SecurePackage *package = (SecurePackage *)phContext->dwUpper;
        PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;

        if (package && package->provider)
        {
            if (package->provider->fnTableW.QueryContextAttributesW)
            {
561
                ret = package->provider->fnTableW.QueryContextAttributesW( ctxt, ulAttribute, pBuffer);
562
                if (ret == SEC_E_OK)
563
                    ret = thunk_ContextAttributesWToA(package, ulAttribute, pBuffer);
564 565 566 567 568 569 570 571 572 573 574 575
            }
            else
                ret = SEC_E_UNSUPPORTED_FUNCTION;
        }
        else
            ret = SEC_E_INVALID_HANDLE;
    }
    else
        ret = SEC_E_INVALID_HANDLE;
    return ret;
}

576
static PSecPkgInfoW _copyPackageInfoFlatAToW(const SecPkgInfoA *infoA)
577 578 579 580 581 582 583 584 585 586
{
    PSecPkgInfoW ret;

    if (infoA)
    {
        size_t bytesNeeded = sizeof(SecPkgInfoW);
        int nameLen = 0, commentLen = 0;

        if (infoA->Name)
        {
587
            nameLen = MultiByteToWideChar(CP_ACP, 0, infoA->Name, -1, NULL, 0);
588 589 590 591
            bytesNeeded += nameLen * sizeof(WCHAR);
        }
        if (infoA->Comment)
        {
592
            commentLen = MultiByteToWideChar(CP_ACP, 0, infoA->Comment, -1, NULL, 0);
593 594
            bytesNeeded += commentLen * sizeof(WCHAR);
        }
595
        if ((ret = RtlAllocateHeap(GetProcessHeap(), 0, bytesNeeded)))
596
        {
597
            WCHAR *nextString = (WCHAR *)(char *)ret + sizeof(SecPkgInfoW);
598 599 600 601 602

            memcpy(ret, infoA, sizeof(SecPkgInfoA));
            if (infoA->Name)
            {
                ret->Name = nextString;
603
                MultiByteToWideChar(CP_ACP, 0, infoA->Name, -1, nextString, nameLen);
604 605 606 607 608 609 610
                nextString += nameLen;
            }
            else
                ret->Name = NULL;
            if (infoA->Comment)
            {
                ret->Comment = nextString;
611
                MultiByteToWideChar(CP_ACP, 0, infoA->Comment, -1, nextString, commentLen);
612 613 614 615 616 617 618 619 620 621 622
            }
            else
                ret->Comment = NULL;
        }
    }
    else
        ret = NULL;
    return ret;
}

static SECURITY_STATUS thunk_ContextAttributesAToW(SecurePackage *package,
623
 ULONG ulAttribute, void *pBuffer)
624 625 626 627 628 629 630 631 632 633 634 635 636 637
{
    SECURITY_STATUS ret = SEC_E_OK;

    if (package && pBuffer)
    {
        switch (ulAttribute)
        {
            case SECPKG_ATTR_NAMES:
            {
                PSecPkgContext_NamesA names = (PSecPkgContext_NamesA)pBuffer;
                SEC_CHAR *oldUser = names->sUserName;

                if (oldUser)
                {
638
                    names->sUserName = (char *)strdupAW(oldUser);
639 640 641 642 643 644
                    package->provider->fnTableW.FreeContextBuffer(oldUser);
                }
                break;
            }
            case SECPKG_ATTR_AUTHORITY:
            {
645
                PSecPkgContext_AuthorityA names = (PSecPkgContext_AuthorityA)pBuffer;
646 647 648 649
                SEC_CHAR *oldAuth = names->sAuthorityName;

                if (oldAuth)
                {
650
                    names->sAuthorityName = (char *)strdupAW(oldAuth);
651 652 653 654 655 656 657 658 659 660 661 662
                    package->provider->fnTableW.FreeContextBuffer(oldAuth);
                }
                break;
            }
            case SECPKG_ATTR_KEY_INFO:
            {
                PSecPkgContext_KeyInfoA info = (PSecPkgContext_KeyInfoA)pBuffer;
                SEC_CHAR *oldSigAlgName = info->sSignatureAlgorithmName;
                SEC_CHAR *oldEncAlgName = info->sEncryptAlgorithmName;

                if (oldSigAlgName)
                {
663 664
                    info->sSignatureAlgorithmName = (char *)strdupAW(oldSigAlgName);
                    package->provider->fnTableW.FreeContextBuffer(oldSigAlgName);
665 666 667
                }
                if (oldEncAlgName)
                {
668 669
                    info->sEncryptAlgorithmName = (char *)strdupAW(oldEncAlgName);
                    package->provider->fnTableW.FreeContextBuffer(oldEncAlgName);
670 671 672 673 674
                }
                break;
            }
            case SECPKG_ATTR_PACKAGE_INFO:
            {
675
                PSecPkgContext_PackageInfoA info = (PSecPkgContext_PackageInfoA)pBuffer;
676 677 678 679 680 681 682 683 684 685 686 687
                PSecPkgInfoA oldPkgInfo = info->PackageInfo;

                if (oldPkgInfo)
                {
                    info->PackageInfo = (PSecPkgInfoA)
                     _copyPackageInfoFlatAToW(oldPkgInfo);
                    package->provider->fnTableW.FreeContextBuffer(oldPkgInfo);
                }
                break;
            }
            case SECPKG_ATTR_NEGOTIATION_INFO:
            {
688
                PSecPkgContext_NegotiationInfoA info = (PSecPkgContext_NegotiationInfoA)pBuffer;
689 690 691 692 693 694 695 696 697 698 699 700
                PSecPkgInfoA oldPkgInfo = info->PackageInfo;

                if (oldPkgInfo)
                {
                    info->PackageInfo = (PSecPkgInfoA)
                     _copyPackageInfoFlatAToW(oldPkgInfo);
                    package->provider->fnTableW.FreeContextBuffer(oldPkgInfo);
                }
                break;
            }
            case SECPKG_ATTR_NATIVE_NAMES:
            {
701 702 703
                PSecPkgContext_NativeNamesA names = (PSecPkgContext_NativeNamesA)pBuffer;
                char *oldClient = names->sClientName;
                char *oldServer = names->sServerName;
704 705 706

                if (oldClient)
                {
707
                    names->sClientName = (char *)strdupAW(oldClient);
708 709 710 711
                    package->provider->fnTableW.FreeContextBuffer(oldClient);
                }
                if (oldServer)
                {
712
                    names->sServerName = (char *)strdupAW(oldServer);
713 714 715 716 717 718
                    package->provider->fnTableW.FreeContextBuffer(oldServer);
                }
                break;
            }
            case SECPKG_ATTR_CREDENTIAL_NAME:
            {
719 720
                PSecPkgContext_CredentialNameA name = (PSecPkgContext_CredentialNameA)pBuffer;
                char *oldCred = name->sCredentialName;
721 722 723

                if (oldCred)
                {
724
                    name->sCredentialName = (char *)strdupAW(oldCred);
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
                    package->provider->fnTableW.FreeContextBuffer(oldCred);
                }
                break;
            }
            /* no thunking needed: */
            case SECPKG_ATTR_ACCESS_TOKEN:
            case SECPKG_ATTR_DCE_INFO:
            case SECPKG_ATTR_FLAGS:
            case SECPKG_ATTR_LIFESPAN:
            case SECPKG_ATTR_PASSWORD_EXPIRY:
            case SECPKG_ATTR_SESSION_KEY:
            case SECPKG_ATTR_SIZES:
            case SECPKG_ATTR_STREAM_SIZES:
            case SECPKG_ATTR_TARGET_INFORMATION:
                break;
            default:
741
                WARN("attribute type %d unknown\n", ulAttribute);
742 743 744 745 746 747 748 749 750
                ret = SEC_E_INTERNAL_ERROR;
        }
    }
    else
        ret = SEC_E_INVALID_TOKEN;
    return ret;
}

SECURITY_STATUS SEC_ENTRY thunk_QueryContextAttributesW(PCtxtHandle phContext,
751
 ULONG ulAttribute, void *pBuffer)
752 753 754
{
    SECURITY_STATUS ret;

755
    TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
756 757 758 759 760 761 762 763 764
    if (phContext)
    {
        SecurePackage *package = (SecurePackage *)phContext->dwUpper;
        PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;

        if (package && package->provider)
        {
            if (package->provider->fnTableA.QueryContextAttributesA)
            {
765
                ret = package->provider->fnTableA.QueryContextAttributesA(ctxt, ulAttribute, pBuffer);
766
                if (ret == SEC_E_OK)
767
                    ret = thunk_ContextAttributesAToW(package, ulAttribute, pBuffer);
768 769 770 771 772 773 774 775 776 777 778 779 780
            }
            else
                ret = SEC_E_UNSUPPORTED_FUNCTION;
        }
        else
            ret = SEC_E_INVALID_HANDLE;
    }
    else
        ret = SEC_E_INVALID_HANDLE;
    return ret;
}

SECURITY_STATUS SEC_ENTRY thunk_SetContextAttributesA(PCtxtHandle phContext,
781
 ULONG ulAttribute, void *pBuffer, ULONG cbBuffer)
782 783 784
{
    SECURITY_STATUS ret;

785
    TRACE("%p %d %p %d\n", phContext, ulAttribute, pBuffer, cbBuffer);
786 787 788 789 790 791 792 793 794 795
    if (phContext)
    {
        SecurePackage *package = (SecurePackage *)phContext->dwUpper;
        PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;

        if (package && package->provider && pBuffer && cbBuffer)
        {
            if (package->provider->fnTableW.SetContextAttributesW)
            {
                /* TODO: gotta validate size too! */
796
                ret = thunk_ContextAttributesWToA(package, ulAttribute, pBuffer);
797
                if (ret == SEC_E_OK)
798
                    ret = package->provider->fnTableW.SetContextAttributesW(ctxt, ulAttribute, pBuffer, cbBuffer);
799 800 801 802 803 804 805 806 807 808 809 810 811
            }
            else
                ret = SEC_E_UNSUPPORTED_FUNCTION;
        }
        else
            ret = SEC_E_INVALID_HANDLE;
    }
    else
        ret = SEC_E_INVALID_HANDLE;
    return ret;
}

SECURITY_STATUS SEC_ENTRY thunk_SetContextAttributesW(PCtxtHandle phContext,
812
 ULONG ulAttribute, void *pBuffer, ULONG cbBuffer)
813 814 815
{
    SECURITY_STATUS ret;

816
    TRACE("%p %d %p %d\n", phContext, ulAttribute, pBuffer, cbBuffer);
817 818 819 820 821 822 823 824 825 826
    if (phContext)
    {
        SecurePackage *package = (SecurePackage *)phContext->dwUpper;
        PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;

        if (package && package->provider && pBuffer && cbBuffer)
        {
            if (package->provider->fnTableA.SetContextAttributesA)
            {
                /* TODO: gotta validate size too! */
827
                ret = thunk_ContextAttributesAToW(package, ulAttribute, pBuffer);
828
                if (ret == SEC_E_OK)
829
                    ret = package->provider->fnTableA.SetContextAttributesA(ctxt, ulAttribute, pBuffer, cbBuffer);
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
            }
            else
                ret = SEC_E_UNSUPPORTED_FUNCTION;
        }
        else
            ret = SEC_E_INVALID_HANDLE;
    }
    else
        ret = SEC_E_INVALID_HANDLE;
    return ret;
}

SECURITY_STATUS SEC_ENTRY thunk_ImportSecurityContextA(
 SEC_CHAR *pszPackage, PSecBuffer pPackedContext, void *Token,
 PCtxtHandle phContext)
{
    SECURITY_STATUS ret;
    UNICODE_STRING package;

849
    TRACE("%s %p %p %p\n", debugstr_a(pszPackage), pPackedContext, Token, phContext);
850
    RtlCreateUnicodeStringFromAsciiz(&package, pszPackage);
851
    ret = ImportSecurityContextW(package.Buffer, pPackedContext, Token, phContext);
852 853 854 855 856 857 858 859 860
    RtlFreeUnicodeString(&package);
    return ret;
}

SECURITY_STATUS SEC_ENTRY thunk_ImportSecurityContextW(
 SEC_WCHAR *pszPackage, PSecBuffer pPackedContext, void *Token,
 PCtxtHandle phContext)
{
    SECURITY_STATUS ret;
861
    char *package = strdupWA(pszPackage);
862

863
    TRACE("%s %p %p %p\n", debugstr_w(pszPackage), pPackedContext, Token, phContext);
864
    ret = ImportSecurityContextA(package, pPackedContext, Token, phContext);
865
    RtlFreeHeap(GetProcessHeap(), 0, package);
866 867
    return ret;
}