jdk/src/java.base/windows/classes/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
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: 15276
diff changeset
     2
 * Copyright (c) 2002, 2013, 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
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 2
diff changeset
    26
package sun.net.www.protocol.http.ntlm;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
15276
bbddb82e66ce 8006568: HTTP protocol handler NLTM Authentication should use Base64 API
msheppar
parents: 14681
diff changeset
    29
import java.util.Base64;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Hooks into Windows implementation of NTLM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class will be replaced if a cross-platform version of NTLM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * is implemented in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class NTLMAuthSequence {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private String username;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private String password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private String ntdomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private int state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private long crdHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private long ctxHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static {
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    47
        initFirst(Status.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    50
    // Used by native code to indicate when a particular protocol sequence is completed
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    51
    // and must not be re-used.
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    52
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    53
    class Status {
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    54
        boolean sequenceComplete;
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    55
    }
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    56
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    57
    Status status;
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    58
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    NTLMAuthSequence (String username, String password, String ntdomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.username = username;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.password = password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.ntdomain = ntdomain;
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    65
        this.status = new Status();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        state = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        crdHandle = getCredentialsHandle (username, ntdomain, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (crdHandle == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new IOException ("could not get credentials handle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public String getAuthHeader (String token) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        byte[] input = null;
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    75
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    76
        assert !status.sequenceComplete;
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    77
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (token != null)
15276
bbddb82e66ce 8006568: HTTP protocol handler NLTM Authentication should use Base64 API
msheppar
parents: 14681
diff changeset
    79
            input = Base64.getDecoder().decode(token);
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    80
        byte[] b = getNextToken (crdHandle, input, status);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (b == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new IOException ("Internal authentication error");
15276
bbddb82e66ce 8006568: HTTP protocol handler NLTM Authentication should use Base64 API
msheppar
parents: 14681
diff changeset
    83
        return Base64.getEncoder().encodeToString(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    86
    public boolean isComplete() {
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    87
        return status.sequenceComplete;
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    88
    }
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    89
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    90
    private static native void initFirst (Class<NTLMAuthSequence.Status> clazz);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private native long getCredentialsHandle (String user, String domain, String password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
14681
523a842a7ac1 7200720: crash in net.dll during NTLM authentication
michaelm
parents: 5506
diff changeset
    94
    private native byte[] getNextToken (long crdHandle, byte[] lastToken, Status returned);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96