jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 14681 523a842a7ac1
child 23560 80b1514e7eac
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 14681
diff changeset
     2
 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4157
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4157
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4157
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4157
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4157
diff changeset
    23
 * questions.
2
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
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
    44
static void endSequence (PCredHandle credHand, PCtxtHandle ctxHandle, JNIEnv *env, jobject status);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
static jfieldID ntlm_ctxHandleID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
static jfieldID ntlm_crdHandleID;
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
    48
static jfieldID status_seqCompleteID;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
static HINSTANCE lib = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 910
diff changeset
    52
JNIEXPORT void JNICALL Java_sun_net_www_protocol_http_ntlm_NTLMAuthSequence_initFirst
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
    53
(JNIEnv *env, jclass authseq_clazz, jclass status_clazz)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
{
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
    55
    ntlm_ctxHandleID = (*env)->GetFieldID(env, authseq_clazz, "ctxHandle", "J");
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
    56
    ntlm_crdHandleID = (*env)->GetFieldID(env, authseq_clazz, "crdHandle", "J");
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
    57
    status_seqCompleteID = (*env)->GetFieldID(env, status_clazz, "sequenceComplete", "Z");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Class:     sun_net_www_protocol_http_NTLMAuthSequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Method:    getCredentialsHandle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 910
diff changeset
    66
JNIEXPORT jlong JNICALL Java_sun_net_www_protocol_http_ntlm_NTLMAuthSequence_getCredentialsHandle
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
(JNIEnv *env, jobject this, jstring user, jstring domain, jstring password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    SEC_WINNT_AUTH_IDENTITY   AuthId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    SEC_WINNT_AUTH_IDENTITY * pAuthId;
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    71
    const CHAR        *pUser = 0;
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    72
    const CHAR        *pDomain = 0;
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    73
    const CHAR        *pPassword = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    CredHandle      *pCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    TimeStamp            ltime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    jboolean         isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    SECURITY_STATUS      ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    if (user != 0) {
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    80
        pUser = JNU_GetStringPlatformChars(env, user, &isCopy);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    81
        if (pUser == NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    82
            return 0;  // pending Exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    if (domain != 0) {
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    85
        pDomain = JNU_GetStringPlatformChars(env, domain, &isCopy);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    86
        if (pDomain == NULL) {
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    87
            if (pUser != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    88
                JNU_ReleaseStringPlatformChars(env, user, pUser);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    89
            return 0;  // pending Exception
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    90
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    if (password != 0) {
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    93
        pPassword = JNU_GetStringPlatformChars(env, password, &isCopy);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    94
        if (pPassword == NULL) {
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    95
            if(pUser != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    96
                JNU_ReleaseStringPlatformChars(env, user, pUser);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    97
            if(pDomain != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    98
                JNU_ReleaseStringPlatformChars(env, domain, pDomain);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    99
            return 0;  // pending Exception
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   100
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    pCred = (CredHandle *)malloc(sizeof (CredHandle));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    if ( ((pUser != NULL) || (pPassword != NULL)) || (pDomain != NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        pAuthId = &AuthId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        memset( &AuthId, 0, sizeof( AuthId ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if ( pUser != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            AuthId.User       = (unsigned char *) pUser;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   111
            AuthId.UserLength = (unsigned long) strlen( pUser );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if ( pPassword != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            AuthId.Password       = (unsigned char *) pPassword;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   116
            AuthId.PasswordLength = (unsigned long) strlen( pPassword );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if ( pDomain != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            AuthId.Domain       = (unsigned char *) pDomain;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   121
            AuthId.DomainLength = (unsigned long) strlen( pDomain );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        AuthId.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        pAuthId = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   129
    ss = AcquireCredentialsHandleA(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        NULL, "NTLM", SECPKG_CRED_OUTBOUND,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        NULL, pAuthId, NULL, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        pCred, &ltime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   135
    /* Release resources held by JNU_GetStringPlatformChars */
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   136
    if (pUser != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   137
        JNU_ReleaseStringPlatformChars(env, user, pUser);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   138
    if (pPassword != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   139
        JNU_ReleaseStringPlatformChars(env, password, pPassword);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   140
    if (pDomain != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   141
        JNU_ReleaseStringPlatformChars(env, domain, pDomain);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   142
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    if (ss == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return (jlong) pCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   150
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   151
/*
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   152
 * Class:     sun_net_www_protocol_http_ntlm_NTLMAuthSequence
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   153
 * Method:    getNextToken
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   154
 * Signature: (J[BLsun/net/www/protocol/http/ntlm/NTLMAuthSequence/Status;)[B
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   155
 */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 910
diff changeset
   156
JNIEXPORT jbyteArray JNICALL Java_sun_net_www_protocol_http_ntlm_NTLMAuthSequence_getNextToken
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   157
(JNIEnv *env, jobject this, jlong crdHandle, jbyteArray lastToken, jobject status)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    VOID        *pInput = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    DWORD            inputLen;
13033
365efcc2d50c 7176784: Windows authentication not working on some computers
chegar
parents: 9003
diff changeset
   162
    CHAR         buffOut[1024];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    jboolean         isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    SECURITY_STATUS      ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    SecBufferDesc        OutBuffDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    SecBuffer            OutSecBuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    SecBufferDesc        InBuffDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    SecBuffer            InSecBuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    ULONG                ContextAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    CredHandle      *pCred = (CredHandle *)crdHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    CtxtHandle      *pCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    CtxtHandle      *newContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    TimeStamp            ltime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    jbyteArray       result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    pCtx = (CtxtHandle *) (*env)->GetLongField (env, this, ntlm_ctxHandleID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    if (pCtx == 0) { /* first call */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        newContext = (CtxtHandle *)malloc(sizeof(CtxtHandle));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        (*env)->SetLongField (env, this, ntlm_ctxHandleID, (jlong)newContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        newContext = pCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    OutBuffDesc.ulVersion = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    OutBuffDesc.cBuffers  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    OutBuffDesc.pBuffers  = &OutSecBuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
13033
365efcc2d50c 7176784: Windows authentication not working on some computers
chegar
parents: 9003
diff changeset
   189
    OutSecBuff.cbBuffer   = 1024;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    OutSecBuff.BufferType = SECBUFFER_TOKEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    OutSecBuff.pvBuffer   = buffOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *  Prepare our Input buffer - Note the server is expecting the client's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *  negotiation packet on the first call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    if (lastToken != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        pInput = (VOID *)(*env)->GetByteArrayElements(env, lastToken, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        inputLen = (*env)->GetArrayLength(env, lastToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        InBuffDesc.ulVersion = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        InBuffDesc.cBuffers  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        InBuffDesc.pBuffers  = &InSecBuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        InSecBuff.cbBuffer       = inputLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        InSecBuff.BufferType = SECBUFFER_TOKEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        InSecBuff.pvBuffer       = pInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *  will return success when its done but we still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *  need to send the out buffer if there are bytes to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   217
    ss = InitializeSecurityContextA(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        pCred, pCtx, NULL, 0, 0, SECURITY_NATIVE_DREP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        lastToken ? &InBuffDesc : NULL, 0, newContext, &OutBuffDesc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        &ContextAttributes, &ltime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    if (pInput != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        (*env)->ReleaseByteArrayElements(env, lastToken, pInput, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    if (ss < 0) {
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   228
        endSequence (pCred, pCtx, env, status);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    if ((ss == SEC_I_COMPLETE_NEEDED) || (ss == SEC_I_COMPLETE_AND_CONTINUE) ) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   233
        ss = CompleteAuthToken( pCtx, &OutBuffDesc );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (ss < 0) {
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   236
            endSequence (pCred, pCtx, env, status);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    if ( OutSecBuff.cbBuffer > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        jbyteArray ret = (*env)->NewByteArray(env, OutSecBuff.cbBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        (*env)->SetByteArrayRegion(env, ret, 0, OutSecBuff.cbBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                OutSecBuff.pvBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (lastToken != 0) // 2nd stage
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   246
            endSequence (pCred, pCtx, env, status);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        result = ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    if ((ss != SEC_I_CONTINUE_NEEDED) && (ss == SEC_I_COMPLETE_AND_CONTINUE)) {
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   251
        endSequence (pCred, pCtx, env, status);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   257
static void endSequence (PCredHandle credHand, PCtxtHandle ctxHandle, JNIEnv *env, jobject status) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    if (credHand != 0) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   259
        FreeCredentialsHandle(credHand);
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   260
        free(credHand);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    if (ctxHandle != 0) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   264
        DeleteSecurityContext(ctxHandle);
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   265
        free(ctxHandle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   267
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   268
    /* Sequence is complete so set flag */
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   269
    (*env)->SetBooleanField(env, status, status_seqCompleteID, JNI_TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
}