src/java.base/windows/native/libnet/NTLMAuthSequence.c
author vtewari
Thu, 22 Nov 2018 13:25:44 +0530
changeset 52651 526b2490c616
parent 47216 71c04702a3d5
permissions -rw-r--r--
8046500: GetIpAddrTable function failed on Pure Ipv6 environment Reviewed-by: 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");
23560
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
    56
    CHECK_NULL(ntlm_ctxHandleID);
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
    57
    ntlm_crdHandleID = (*env)->GetFieldID(env, authseq_clazz, "crdHandle", "J");
23560
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
    58
    CHECK_NULL(ntlm_crdHandleID);
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
    59
    status_seqCompleteID = (*env)->GetFieldID(env, status_clazz, "sequenceComplete", "Z");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Class:     sun_net_www_protocol_http_NTLMAuthSequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Method:    getCredentialsHandle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 910
diff changeset
    68
JNIEXPORT jlong JNICALL Java_sun_net_www_protocol_http_ntlm_NTLMAuthSequence_getCredentialsHandle
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
(JNIEnv *env, jobject this, jstring user, jstring domain, jstring password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    SEC_WINNT_AUTH_IDENTITY   AuthId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    SEC_WINNT_AUTH_IDENTITY * pAuthId;
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    73
    const CHAR        *pUser = 0;
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    74
    const CHAR        *pDomain = 0;
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    75
    const CHAR        *pPassword = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    CredHandle      *pCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    TimeStamp            ltime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    jboolean         isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    SECURITY_STATUS      ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    if (user != 0) {
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    82
        pUser = JNU_GetStringPlatformChars(env, user, &isCopy);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    83
        if (pUser == NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    84
            return 0;  // pending Exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    if (domain != 0) {
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    87
        pDomain = JNU_GetStringPlatformChars(env, domain, &isCopy);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    88
        if (pDomain == NULL) {
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    89
            if (pUser != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    90
                JNU_ReleaseStringPlatformChars(env, user, pUser);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    91
            return 0;  // pending Exception
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    92
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    if (password != 0) {
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    95
        pPassword = JNU_GetStringPlatformChars(env, password, &isCopy);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    96
        if (pPassword == NULL) {
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    97
            if(pUser != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    98
                JNU_ReleaseStringPlatformChars(env, user, pUser);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
    99
            if(pDomain != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   100
                JNU_ReleaseStringPlatformChars(env, domain, pDomain);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   101
            return 0;  // pending Exception
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   102
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    pCred = (CredHandle *)malloc(sizeof (CredHandle));
23560
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   105
    if (pCred == NULL) {
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   106
        JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   107
        if (pUser != NULL)
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   108
            JNU_ReleaseStringPlatformChars(env, user, pUser);
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   109
        if (pPassword != NULL)
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   110
            JNU_ReleaseStringPlatformChars(env, password, pPassword);
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   111
        if (pDomain != NULL)
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   112
            JNU_ReleaseStringPlatformChars(env, domain, pDomain);
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   113
        return NULL;
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   114
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    if ( ((pUser != NULL) || (pPassword != NULL)) || (pDomain != NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        pAuthId = &AuthId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        memset( &AuthId, 0, sizeof( AuthId ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if ( pUser != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            AuthId.User       = (unsigned char *) pUser;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   123
            AuthId.UserLength = (unsigned long) strlen( pUser );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if ( pPassword != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            AuthId.Password       = (unsigned char *) pPassword;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   128
            AuthId.PasswordLength = (unsigned long) strlen( pPassword );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if ( pDomain != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            AuthId.Domain       = (unsigned char *) pDomain;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   133
            AuthId.DomainLength = (unsigned long) strlen( pDomain );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        AuthId.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        pAuthId = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   141
    ss = AcquireCredentialsHandleA(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        NULL, "NTLM", SECPKG_CRED_OUTBOUND,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        NULL, pAuthId, NULL, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        pCred, &ltime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
86
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   147
    /* Release resources held by JNU_GetStringPlatformChars */
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   148
    if (pUser != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   149
        JNU_ReleaseStringPlatformChars(env, user, pUser);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   150
    if (pPassword != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   151
        JNU_ReleaseStringPlatformChars(env, password, pPassword);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   152
    if (pDomain != NULL)
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   153
        JNU_ReleaseStringPlatformChars(env, domain, pDomain);
a9644392a939 6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents: 72
diff changeset
   154
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    if (ss == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return (jlong) pCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   162
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   163
/*
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   164
 * Class:     sun_net_www_protocol_http_ntlm_NTLMAuthSequence
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   165
 * Method:    getNextToken
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   166
 * 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
   167
 */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 910
diff changeset
   168
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
   169
(JNIEnv *env, jobject this, jlong crdHandle, jbyteArray lastToken, jobject status)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    VOID        *pInput = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    DWORD            inputLen;
13033
365efcc2d50c 7176784: Windows authentication not working on some computers
chegar
parents: 9003
diff changeset
   174
    CHAR         buffOut[1024];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    jboolean         isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    SECURITY_STATUS      ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    SecBufferDesc        OutBuffDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    SecBuffer            OutSecBuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    SecBufferDesc        InBuffDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    SecBuffer            InSecBuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    ULONG                ContextAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    CredHandle      *pCred = (CredHandle *)crdHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    CtxtHandle      *pCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    CtxtHandle      *newContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    TimeStamp            ltime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    jbyteArray       result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    pCtx = (CtxtHandle *) (*env)->GetLongField (env, this, ntlm_ctxHandleID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if (pCtx == 0) { /* first call */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        newContext = (CtxtHandle *)malloc(sizeof(CtxtHandle));
23560
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   192
        if (newContext != NULL) {
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   193
            (*env)->SetLongField (env, this, ntlm_ctxHandleID, (jlong)newContext);
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   194
        } else {
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   195
            JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   196
            return NULL;
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   197
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        newContext = pCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    OutBuffDesc.ulVersion = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    OutBuffDesc.cBuffers  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    OutBuffDesc.pBuffers  = &OutSecBuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
13033
365efcc2d50c 7176784: Windows authentication not working on some computers
chegar
parents: 9003
diff changeset
   206
    OutSecBuff.cbBuffer   = 1024;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    OutSecBuff.BufferType = SECBUFFER_TOKEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    OutSecBuff.pvBuffer   = buffOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *  Prepare our Input buffer - Note the server is expecting the client's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *  negotiation packet on the first call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    if (lastToken != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        pInput = (VOID *)(*env)->GetByteArrayElements(env, lastToken, &isCopy);
23560
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   218
        CHECK_NULL_RETURN(pInput, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        inputLen = (*env)->GetArrayLength(env, lastToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        InBuffDesc.ulVersion = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        InBuffDesc.cBuffers  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        InBuffDesc.pBuffers  = &InSecBuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        InSecBuff.cbBuffer       = inputLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        InSecBuff.BufferType = SECBUFFER_TOKEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        InSecBuff.pvBuffer       = pInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *  will return success when its done but we still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *  need to send the out buffer if there are bytes to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   235
    ss = InitializeSecurityContextA(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        pCred, pCtx, NULL, 0, 0, SECURITY_NATIVE_DREP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        lastToken ? &InBuffDesc : NULL, 0, newContext, &OutBuffDesc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        &ContextAttributes, &ltime
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 (pInput != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        (*env)->ReleaseByteArrayElements(env, lastToken, pInput, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    if (ss < 0) {
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
        return 0;
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_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
   251
        ss = CompleteAuthToken( pCtx, &OutBuffDesc );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (ss < 0) {
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   254
            endSequence (pCred, pCtx, env, status);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    if ( OutSecBuff.cbBuffer > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        jbyteArray ret = (*env)->NewByteArray(env, OutSecBuff.cbBuffer);
23560
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   261
        if (ret != NULL) {
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   262
            (*env)->SetByteArrayRegion(env, ret, 0, OutSecBuff.cbBuffer,
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   263
                    OutSecBuff.pvBuffer);
80b1514e7eac 8036600: JNI exception pending in src/jdk/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
msheppar
parents: 23010
diff changeset
   264
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (lastToken != 0) // 2nd stage
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   266
            endSequence (pCred, pCtx, env, status);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        result = ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    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
   271
        endSequence (pCred, pCtx, env, status);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   277
static void endSequence (PCredHandle credHand, PCtxtHandle ctxHandle, JNIEnv *env, jobject status) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    if (credHand != 0) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   279
        FreeCredentialsHandle(credHand);
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   280
        free(credHand);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    if (ctxHandle != 0) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   284
        DeleteSecurityContext(ctxHandle);
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   285
        free(ctxHandle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   287
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   288
    /* Sequence is complete so set flag */
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 14342
diff changeset
   289
    (*env)->SetBooleanField(env, status, status_seqCompleteID, JNI_TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
}