src/java.base/unix/classes/sun/net/www/protocol/http/ntlm/NTLMAuthentication.java
author dfuchs
Fri, 30 Aug 2019 15:42:27 +0100
branchJDK-8229867-branch
changeset 57968 8595871a5446
parent 53328 dff86e25073f
permissions -rw-r--r--
JDK-8229867: first prototype
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
     2
 * Copyright (c) 2005, 2016, 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: 3952
diff changeset
    26
package sun.net.www.protocol.http.ntlm;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
    28
import com.sun.security.ntlm.Client;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
    29
import com.sun.security.ntlm.NTLMException;
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    30
import java.io.IOException;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    31
import java.net.InetAddress;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    32
import java.net.PasswordAuthentication;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    33
import java.net.UnknownHostException;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    34
import java.net.URL;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    35
import java.security.GeneralSecurityException;
15276
bbddb82e66ce 8006568: HTTP protocol handler NLTM Authentication should use Base64 API
msheppar
parents: 9035
diff changeset
    36
import java.util.Base64;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
    37
import java.util.Objects;
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
    38
import java.util.Properties;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.net.www.HeaderParser;
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    41
import sun.net.www.protocol.http.AuthenticationInfo;
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    42
import sun.net.www.protocol.http.AuthScheme;
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    43
import sun.net.www.protocol.http.HttpURLConnection;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 32842
diff changeset
    44
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * NTLMAuthentication:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Michael McMahon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * NTLM authentication is nominally based on the framework defined in RFC2617,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * but differs from the standard (Basic & Digest) schemes as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * 1. A complete authentication requires three request/response transactions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *    as shown below:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *            REQ ------------------------------->
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *            <---- 401 (signalling NTLM) --------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *            REQ (with type1 NTLM msg) --------->
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *            <---- 401 (with type 2 NTLM msg) ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *            REQ (with type3 NTLM msg) --------->
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *            <---- OK ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * 2. The scope of the authentication is the TCP connection (which must be kept-alive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *    after the type2 response is received. This means that NTLM does not work end-to-end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *    through a proxy, rather between client and proxy, or between client and server (with no proxy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    72
public class NTLMAuthentication extends AuthenticationInfo {
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
    73
    private static final long serialVersionUID = 170L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
8815
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 6517
diff changeset
    75
    private static final NTLMAuthenticationCallback NTLMAuthCallback =
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 6517
diff changeset
    76
        NTLMAuthenticationCallback.getNTLMAuthenticationCallback();
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 6517
diff changeset
    77
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private String hostname;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 32842
diff changeset
    79
    /* Domain to use if not specified by user */
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
    80
    private static final String defaultDomain;
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
    81
    /* Whether cache is enabled for NTLM */
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
    82
    private static final boolean ntlmCache;
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
    83
    static {
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
    84
        Properties props = GetPropertyAction.privilegedGetProperties();
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
    85
        defaultDomain = props.getProperty("http.auth.ntlm.domain", "");
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
    86
        String ntlmCacheProp = props.getProperty("jdk.ntlm.cache", "true");
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
    87
        ntlmCache = Boolean.parseBoolean(ntlmCacheProp);
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
    88
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    90
    public static boolean supportsTransparentAuth () {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
8815
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 6517
diff changeset
    94
    /**
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 6517
diff changeset
    95
     * Returns true if the given site is trusted, i.e. we can try
53328
dff86e25073f 8209094: Improve web server connections
michaelm
parents: 47216
diff changeset
    96
     * transparent Authentication. Shouldn't be called since
dff86e25073f 8209094: Improve web server connections
michaelm
parents: 47216
diff changeset
    97
     * capability not supported on Unix
8815
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 6517
diff changeset
    98
     */
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 6517
diff changeset
    99
    public static boolean isTrustedSite(URL url) {
53328
dff86e25073f 8209094: Improve web server connections
michaelm
parents: 47216
diff changeset
   100
        if (NTLMAuthCallback != null)
dff86e25073f 8209094: Improve web server connections
michaelm
parents: 47216
diff changeset
   101
            return NTLMAuthCallback.isTrustedSite(url);
dff86e25073f 8209094: Improve web server connections
michaelm
parents: 47216
diff changeset
   102
        return false;
8815
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 6517
diff changeset
   103
    }
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 6517
diff changeset
   104
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private void init0() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   107
        hostname = java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   108
            new java.security.PrivilegedAction<>() {
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   109
            public String run() {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   110
                String localhost;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   111
                try {
25396
5e73c95f95db 7150092: NTLM authentication fail if user specified a different realm
weijun
parents: 23010
diff changeset
   112
                    localhost = InetAddress.getLocalHost().getHostName();
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   113
                } catch (UnknownHostException e) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   114
                     localhost = "localhost";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   116
                return localhost;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   118
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    PasswordAuthentication pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   123
    Client client;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Create a NTLMAuthentication:
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 29986
diff changeset
   126
     * Username may be specified as {@literal domain<BACKSLASH>username}
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 29986
diff changeset
   127
     * in the application Authenticator.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * If this notation is not used, then the domain will be taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * from a system property: "http.auth.ntlm.domain".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   131
    public NTLMAuthentication(boolean isProxy, URL url, PasswordAuthentication pw,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   132
                              String authenticatorKey) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   133
        super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   134
                AuthScheme.NTLM,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   135
                url,
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   136
                "",
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   137
                Objects.requireNonNull(authenticatorKey));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        init (pw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private void init (PasswordAuthentication pw) {
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   142
        String username;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   143
        String ntdomain;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   144
        char[] password;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.pw = pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        String s = pw.getUserName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int i = s.indexOf ('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (i == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            username = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            ntdomain = defaultDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            ntdomain = s.substring (0, i).toUpperCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            username = s.substring (i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   155
        password = pw.getPassword();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        init0();
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   157
        try {
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   158
            String version = GetPropertyAction.privilegedGetProperty("ntlm.version");
32842
831e91e26c15 8137174: NTLM impl should use doPrivileged when it reads system properties
asmotrak
parents: 31061
diff changeset
   159
            client = new Client(version, hostname, username, ntdomain, password);
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   160
        } catch (NTLMException ne) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   161
            try {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   162
                client = new Client(null, hostname, username, ntdomain, password);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   163
            } catch (NTLMException ne2) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   164
                // Will never happen
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   165
                throw new AssertionError("Really?");
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   166
            }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   167
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    * Constructor used for proxy entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public NTLMAuthentication(boolean isProxy, String host, int port,
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   174
                              PasswordAuthentication pw,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   175
                              String authenticatorKey) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   176
        super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   177
                AuthScheme.NTLM,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   178
                host,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   179
                port,
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   180
                "",
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   181
                Objects.requireNonNull(authenticatorKey));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        init (pw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   185
    @Override
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   186
    protected boolean useAuthCache() {
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   187
        return ntlmCache && super.useAuthCache();
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   188
    }
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   189
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return true if this authentication supports preemptive authorization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   193
    @Override
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   194
    public boolean supportsPreemptiveAuthorization() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Not supported. Must use the setHeaders() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   201
    @Override
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   202
    public String getHeaderValue(URL url, String method) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        throw new RuntimeException ("getHeaderValue not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Check if the header indicates that the current auth. parameters are stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * If so, then replace the relevant field with the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * and return true. Otherwise return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * returning true means the request can be retried with the same userid/password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * returning false means we have to go back to the user to ask for a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * username password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   214
    @Override
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   215
    public boolean isAuthorizationStale (String header) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return false; /* should not be called for ntlm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Set header(s) on the given connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param conn The connection to apply the header(s) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param p A source of header values for this connection, not used because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *          HeaderParser converts the fields to lower case, use raw instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param raw The raw header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return true if all goes well, false if no headers were set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   227
    @Override
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53328
diff changeset
   228
    public boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            String response;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (raw.length() < 6) { /* NTLM<sp> */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                response = buildType1Msg ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                String msg = raw.substring (5); /* skip NTLM<sp> */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                response = buildType3Msg (msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            conn.setAuthenticationProperty(getHeaderName(), response);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    private String buildType1Msg () {
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   248
        byte[] msg = client.type1();
15276
bbddb82e66ce 8006568: HTTP protocol handler NLTM Authentication should use Base64 API
msheppar
parents: 9035
diff changeset
   249
        String result = "NTLM " + Base64.getEncoder().encodeToString(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    private String buildType3Msg (String challenge) throws GeneralSecurityException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                                           IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        /* First decode the type2 message to get the server nonce */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        /* nonce is located at type2[24] for 8 bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
15276
bbddb82e66ce 8006568: HTTP protocol handler NLTM Authentication should use Base64 API
msheppar
parents: 9035
diff changeset
   258
        byte[] type2 = Base64.getDecoder().decode(challenge);
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   259
        byte[] nonce = new byte[8];
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   260
        new java.util.Random().nextBytes(nonce);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents: 5506
diff changeset
   261
        byte[] msg = client.type3(type2, nonce);
15276
bbddb82e66ce 8006568: HTTP protocol handler NLTM Authentication should use Base64 API
msheppar
parents: 9035
diff changeset
   262
        String result = "NTLM " + Base64.getEncoder().encodeToString(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
}