jdk/src/windows/native/sun/net/www/protocol/http/NTLMAuthSequence.c
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 86 a9644392a939
child 910 1f53246fb014
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 86
diff changeset
     2
 * Copyright 2002-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <jni.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <rpc.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <winsock.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <lm.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <stdarg.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <tchar.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    39
#include "jni_util.h"
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#define SECURITY_WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "sspi.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "issperr.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * OS calls loaded from DLL on intialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
static FREE_CREDENTIALS_HANDLE_FN pFreeCredentialsHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
static ACQUIRE_CREDENTIALS_HANDLE_FN pAcquireCredentialsHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
static FREE_CONTEXT_BUFFER_FN pFreeContextBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static INITIALIZE_SECURITY_CONTEXT_FN pInitializeSecurityContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
static COMPLETE_AUTH_TOKEN_FN pCompleteAuthToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
static DELETE_SECURITY_CONTEXT_FN pDeleteSecurityContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
72
f24a98c3df49 6638560: APPCRASH in "SPNEGO_HTTP_AUTH/PROXY_FALLBACK" test case with 64 bit JDK on Win2008 x64, VinVista x64
chegar
parents: 2
diff changeset
    57
static void endSequence (PCredHandle credHand, PCtxtHandle ctxHandle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
static jfieldID ntlm_ctxHandleID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
static jfieldID ntlm_crdHandleID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
static HINSTANCE lib = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
JNIEXPORT void JNICALL Java_sun_net_www_protocol_http_NTLMAuthSequence_initFirst
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    OSVERSIONINFO   version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    UCHAR libName[MAX_PATH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    ntlm_ctxHandleID = (*env)->GetFieldID(env, clazz, "ctxHandle", "J");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    ntlm_crdHandleID = (*env)->GetFieldID(env, clazz, "crdHandle", "J");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    version.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    GetVersionEx (&version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    if (version.dwPlatformId == VER_PLATFORM_WIN32_NT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        strcpy (libName, "security.dll" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    else if (version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        strcpy (libName, "secur32.dll" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    lib = LoadLibrary (libName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    pFreeCredentialsHandle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        = (FREE_CREDENTIALS_HANDLE_FN) GetProcAddress(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        lib, "FreeCredentialsHandle" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    pAcquireCredentialsHandle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        = (ACQUIRE_CREDENTIALS_HANDLE_FN) GetProcAddress(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        lib, "AcquireCredentialsHandleA" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    pFreeContextBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        = (FREE_CONTEXT_BUFFER_FN) GetProcAddress(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        lib, "FreeContextBuffer" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    pInitializeSecurityContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        = (INITIALIZE_SECURITY_CONTEXT_FN) GetProcAddress(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        lib, "InitializeSecurityContextA" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    pCompleteAuthToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        = (COMPLETE_AUTH_TOKEN_FN) GetProcAddress(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        lib, "CompleteAuthToken" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    pDeleteSecurityContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        = (DELETE_SECURITY_CONTEXT_FN) GetProcAddress(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        lib, "DeleteSecurityContext" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * Class:     sun_net_www_protocol_http_NTLMAuthSequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * Method:    getCredentialsHandle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
JNIEXPORT jlong JNICALL Java_sun_net_www_protocol_http_NTLMAuthSequence_getCredentialsHandle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
(JNIEnv *env, jobject this, jstring user, jstring domain, jstring password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    SEC_WINNT_AUTH_IDENTITY   AuthId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    SEC_WINNT_AUTH_IDENTITY * pAuthId;
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   122
    const CHAR        *pUser = 0;
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   123
    const CHAR        *pDomain = 0;
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   124
    const CHAR        *pPassword = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    CredHandle      *pCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    TimeStamp            ltime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    jboolean         isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    SECURITY_STATUS      ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    if (user != 0) {
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   131
        pUser = JNU_GetStringPlatformChars(env, user, &isCopy);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   132
        if (pUser == NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   133
            return 0;  // pending Exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    if (domain != 0) {
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   136
        pDomain = JNU_GetStringPlatformChars(env, domain, &isCopy);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   137
        if (pDomain == NULL) {
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   138
            if (pUser != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   139
                JNU_ReleaseStringPlatformChars(env, user, pUser);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   140
            return 0;  // pending Exception
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   141
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    if (password != 0) {
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   144
        pPassword = JNU_GetStringPlatformChars(env, password, &isCopy);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   145
        if (pPassword == NULL) {
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   146
            if(pUser != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   147
                JNU_ReleaseStringPlatformChars(env, user, pUser);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   148
            if(pDomain != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   149
                JNU_ReleaseStringPlatformChars(env, domain, pDomain);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   150
            return 0;  // pending Exception
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   151
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    pCred = (CredHandle *)malloc(sizeof (CredHandle));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    if ( ((pUser != NULL) || (pPassword != NULL)) || (pDomain != NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        pAuthId = &AuthId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        memset( &AuthId, 0, sizeof( AuthId ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if ( pUser != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            AuthId.User       = (unsigned char *) pUser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            AuthId.UserLength = strlen( pUser );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if ( pPassword != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            AuthId.Password       = (unsigned char *) pPassword;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            AuthId.PasswordLength = strlen( pPassword );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if ( pDomain != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            AuthId.Domain       = (unsigned char *) pDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            AuthId.DomainLength = strlen( pDomain );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        AuthId.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        pAuthId = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    ss = pAcquireCredentialsHandle(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        NULL, "NTLM", SECPKG_CRED_OUTBOUND,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        NULL, pAuthId, NULL, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        pCred, &ltime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   186
    /* Release resources held by JNU_GetStringPlatformChars */
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   187
    if (pUser != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   188
        JNU_ReleaseStringPlatformChars(env, user, pUser);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   189
    if (pPassword != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   190
        JNU_ReleaseStringPlatformChars(env, password, pPassword);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   191
    if (pDomain != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   192
        JNU_ReleaseStringPlatformChars(env, domain, pDomain);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   193
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    if (ss == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return (jlong) pCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
JNIEXPORT jbyteArray JNICALL Java_sun_net_www_protocol_http_NTLMAuthSequence_getNextToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
(JNIEnv *env, jobject this, jlong crdHandle, jbyteArray lastToken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    VOID        *pInput = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    DWORD            inputLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    CHAR         buffOut[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    jboolean         isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    SECURITY_STATUS      ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    SecBufferDesc        OutBuffDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    SecBuffer            OutSecBuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    SecBufferDesc        InBuffDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    SecBuffer            InSecBuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    ULONG                ContextAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    CredHandle      *pCred = (CredHandle *)crdHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    CtxtHandle      *pCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    CtxtHandle      *newContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    TimeStamp            ltime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    jbyteArray       result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    pCtx = (CtxtHandle *) (*env)->GetLongField (env, this, ntlm_ctxHandleID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    if (pCtx == 0) { /* first call */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        newContext = (CtxtHandle *)malloc(sizeof(CtxtHandle));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        (*env)->SetLongField (env, this, ntlm_ctxHandleID, (jlong)newContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        newContext = pCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    OutBuffDesc.ulVersion = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    OutBuffDesc.cBuffers  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    OutBuffDesc.pBuffers  = &OutSecBuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    OutSecBuff.cbBuffer   = 512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    OutSecBuff.BufferType = SECBUFFER_TOKEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    OutSecBuff.pvBuffer   = buffOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *  Prepare our Input buffer - Note the server is expecting the client's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *  negotiation packet on the first call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    if (lastToken != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        pInput = (VOID *)(*env)->GetByteArrayElements(env, lastToken, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        inputLen = (*env)->GetArrayLength(env, lastToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        InBuffDesc.ulVersion = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        InBuffDesc.cBuffers  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        InBuffDesc.pBuffers  = &InSecBuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        InSecBuff.cbBuffer       = inputLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        InSecBuff.BufferType = SECBUFFER_TOKEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        InSecBuff.pvBuffer       = pInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *  will return success when its done but we still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *  need to send the out buffer if there are bytes to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    ss = pInitializeSecurityContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        pCred, pCtx, NULL, 0, 0, SECURITY_NATIVE_DREP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        lastToken ? &InBuffDesc : NULL, 0, newContext, &OutBuffDesc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        &ContextAttributes, &ltime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    if (pInput != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        (*env)->ReleaseByteArrayElements(env, lastToken, pInput, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    if (ss < 0) {
72
f24a98c3df49 6638560: APPCRASH in "SPNEGO_HTTP_AUTH/PROXY_FALLBACK" test case with 64 bit JDK on Win2008 x64, VinVista x64
chegar
parents: 2
diff changeset
   273
        endSequence (pCred, pCtx);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    if ((ss == SEC_I_COMPLETE_NEEDED) || (ss == SEC_I_COMPLETE_AND_CONTINUE) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        ss = pCompleteAuthToken( pCtx, &OutBuffDesc );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (ss < 0) {
72
f24a98c3df49 6638560: APPCRASH in "SPNEGO_HTTP_AUTH/PROXY_FALLBACK" test case with 64 bit JDK on Win2008 x64, VinVista x64
chegar
parents: 2
diff changeset
   281
            endSequence (pCred, pCtx);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    if ( OutSecBuff.cbBuffer > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        jbyteArray ret = (*env)->NewByteArray(env, OutSecBuff.cbBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        (*env)->SetByteArrayRegion(env, ret, 0, OutSecBuff.cbBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                OutSecBuff.pvBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (lastToken != 0) // 2nd stage
72
f24a98c3df49 6638560: APPCRASH in "SPNEGO_HTTP_AUTH/PROXY_FALLBACK" test case with 64 bit JDK on Win2008 x64, VinVista x64
chegar
parents: 2
diff changeset
   291
            endSequence (pCred, pCtx);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        result = ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    if ((ss != SEC_I_CONTINUE_NEEDED) && (ss == SEC_I_COMPLETE_AND_CONTINUE)) {
72
f24a98c3df49 6638560: APPCRASH in "SPNEGO_HTTP_AUTH/PROXY_FALLBACK" test case with 64 bit JDK on Win2008 x64, VinVista x64
chegar
parents: 2
diff changeset
   296
        endSequence (pCred, pCtx);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
72
f24a98c3df49 6638560: APPCRASH in "SPNEGO_HTTP_AUTH/PROXY_FALLBACK" test case with 64 bit JDK on Win2008 x64, VinVista x64
chegar
parents: 2
diff changeset
   302
static void endSequence (PCredHandle credHand, PCtxtHandle ctxHandle) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    if (credHand != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        pFreeCredentialsHandle (credHand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        free (credHand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    if (ctxHandle != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        pDeleteSecurityContext(ctxHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        free (ctxHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}