jdk/src/solaris/classes/sun/net/www/protocol/http/NTLMAuthentication.java
author michaelm
Fri, 02 Oct 2009 13:57:41 +0100
changeset 3952 dc329398de30
parent 3859 8b82336dedb3
child 5506 202f599c92aa
permissions -rw-r--r--
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1639
a97859015238 6785258: Update copyright year
xdono
parents: 1334
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.www.protocol.http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    28
import java.io.IOException;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    29
import java.io.UnsupportedEncodingException;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    30
import java.net.InetAddress;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    31
import java.net.PasswordAuthentication;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    32
import java.net.UnknownHostException;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    33
import java.net.URL;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    34
import java.security.GeneralSecurityException;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    35
import java.security.MessageDigest;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    36
import java.security.NoSuchAlgorithmException;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    37
import javax.crypto.Cipher;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    38
import javax.crypto.NoSuchPaddingException;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    39
import javax.crypto.SecretKey;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    40
import javax.crypto.SecretKeyFactory;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
    41
import javax.crypto.spec.DESKeySpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.net.www.HeaderParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * NTLMAuthentication:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Michael McMahon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * NTLM authentication is nominally based on the framework defined in RFC2617,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * but differs from the standard (Basic & Digest) schemes as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * 1. A complete authentication requires three request/response transactions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *    as shown below:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *            REQ ------------------------------->
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *            <---- 401 (signalling NTLM) --------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *            REQ (with type1 NTLM msg) --------->
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *            <---- 401 (with type 2 NTLM msg) ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *            REQ (with type3 NTLM msg) --------->
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *            <---- OK ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * 2. The scope of the authentication is the TCP connection (which must be kept-alive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *    after the type2 response is received. This means that NTLM does not work end-to-end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *    through a proxy, rather between client and proxy, or between client and server (with no proxy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
class NTLMAuthentication extends AuthenticationInfo {
1334
21b652819b97 6746836: java.net exception classes don't specify serialVersionUID
chegar
parents: 2
diff changeset
    72
    private static final long serialVersionUID = -2403849171106437142L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private byte[] type1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private byte[] type3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private SecretKeyFactory fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private Cipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private MessageDigest md4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private String hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static String defaultDomain; /* Domain to use if not specified by user */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        defaultDomain = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            new sun.security.action.GetPropertyAction("http.auth.ntlm.domain",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                                      "domain"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static boolean supportsTransparentAuth () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private void init0() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        type1 = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        type3 = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.arraycopy (new byte[] {'N','T','L','M','S','S','P',0,1}, 0, type1, 0, 9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        type1[12] = (byte) 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        type1[13] = (byte) 0xb2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        type1[28] = (byte) 0x20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        System.arraycopy (new byte[] {'N','T','L','M','S','S','P',0,3}, 0, type3, 0, 9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        type3[12] = (byte) 0x18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        type3[14] = (byte) 0x18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        type3[20] = (byte) 0x18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        type3[22] = (byte) 0x18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        type3[32] = (byte) 0x40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        type3[60] = (byte) 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        type3[61] = (byte) 0x82;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            hostname = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                new java.security.PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    String localhost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        localhost = InetAddress.getLocalHost().getHostName().toUpperCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                         localhost = "localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    return localhost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            int x = hostname.indexOf ('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (x != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                hostname = hostname.substring (0, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            fac = SecretKeyFactory.getInstance ("DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            cipher = Cipher.getInstance ("DES/ECB/NoPadding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            md4 = sun.security.provider.MD4.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        } catch (NoSuchPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    PasswordAuthentication pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    String username;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    String ntdomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    String password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Create a NTLMAuthentication:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Username may be specified as domain<BACKSLASH>username in the application Authenticator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * If this notation is not used, then the domain will be taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * from a system property: "http.auth.ntlm.domain".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public NTLMAuthentication(boolean isProxy, URL url, PasswordAuthentication pw) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   148
        super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   149
                AuthScheme.NTLM,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   150
                url,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   151
                "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        init (pw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private void init (PasswordAuthentication pw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.pw = pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        String s = pw.getUserName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        int i = s.indexOf ('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (i == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            username = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            ntdomain = defaultDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            ntdomain = s.substring (0, i).toUpperCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            username = s.substring (i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        password = new String (pw.getPassword());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        init0();
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,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                PasswordAuthentication pw) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   175
        super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   176
                AuthScheme.NTLM,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   177
                host,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   178
                port,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 1639
diff changeset
   179
                "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        init (pw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @return true if this authentication supports preemptive authorization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    boolean supportsPreemptiveAuthorization() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return the name of the HTTP header this authentication wants set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    String getHeaderName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (type == SERVER_AUTHENTICATION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return "Authorization";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return "Proxy-authorization";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Not supported. Must use the setHeaders() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    String getHeaderValue(URL url, String method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        throw new RuntimeException ("getHeaderValue not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Check if the header indicates that the current auth. parameters are stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * If so, then replace the relevant field with the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * and return true. Otherwise return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * returning true means the request can be retried with the same userid/password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * returning false means we have to go back to the user to ask for a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * username password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    boolean isAuthorizationStale (String header) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return false; /* should not be called for ntlm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Set header(s) on the given connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param conn The connection to apply the header(s) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param p A source of header values for this connection, not used because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *          HeaderParser converts the fields to lower case, use raw instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param raw The raw header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return true if all goes well, false if no headers were set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    synchronized boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw) {
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 void copybytes (byte[] dest, int destpos, String src, String enc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            byte[] x = src.getBytes(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            System.arraycopy (x, 0, dest, destpos, x.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private String buildType1Msg () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        int dlen = ntdomain.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        type1[16]= (byte) (dlen % 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        type1[17]= (byte) (dlen / 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        type1[18] = type1[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        type1[19] = type1[17];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        int hlen = hostname.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        type1[24]= (byte) (hlen % 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        type1[25]= (byte) (hlen / 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        type1[26] = type1[24];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        type1[27] = type1[25];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        copybytes (type1, 32, hostname, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        copybytes (type1, hlen+32, ntdomain, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        type1[20] = (byte) ((hlen+32) % 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        type1[21] = (byte) ((hlen+32) / 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        byte[] msg = new byte [32 + hlen + dlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        System.arraycopy (type1, 0, msg, 0, 32 + hlen + dlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        String result = "NTLM " + (new B64Encoder()).encode (msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /* Convert a 7 byte array to an 8 byte array (for a des key with parity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * input starts at offset off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    private byte[] makeDesKey (byte[] input, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        int[] in = new int [input.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        for (int i=0; i<in.length; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            in[i] = input[i]<0 ? input[i]+256: input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        byte[] out = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        out[0] = (byte)in[off+0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        out[1] = (byte)(((in[off+0] << 7) & 0xFF) | (in[off+1] >> 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        out[2] = (byte)(((in[off+1] << 6) & 0xFF) | (in[off+2] >> 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        out[3] = (byte)(((in[off+2] << 5) & 0xFF) | (in[off+3] >> 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        out[4] = (byte)(((in[off+3] << 4) & 0xFF) | (in[off+4] >> 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        out[5] = (byte)(((in[off+4] << 3) & 0xFF) | (in[off+5] >> 5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        out[6] = (byte)(((in[off+5] << 2) & 0xFF) | (in[off+6] >> 6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        out[7] = (byte)((in[off+6] << 1) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    private byte[] calcLMHash () throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        byte[] magic = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        byte[] pwb = password.toUpperCase ().getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        byte[] pwb1 = new byte [14];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        int len = password.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (len > 14)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            len = 14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        System.arraycopy (pwb, 0, pwb1, 0, len); /* Zero padded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        DESKeySpec dks1 = new DESKeySpec (makeDesKey (pwb1, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        DESKeySpec dks2 = new DESKeySpec (makeDesKey (pwb1, 7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        SecretKey key1 = fac.generateSecret (dks1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        SecretKey key2 = fac.generateSecret (dks2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        cipher.init (Cipher.ENCRYPT_MODE, key1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        byte[] out1 = cipher.doFinal (magic, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        cipher.init (Cipher.ENCRYPT_MODE, key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        byte[] out2 = cipher.doFinal (magic, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        byte[] result = new byte [21];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        System.arraycopy (out1, 0, result, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        System.arraycopy (out2, 0, result, 8, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    private byte[] calcNTHash () throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        byte[] pw = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            pw = password.getBytes ("UnicodeLittleUnmarked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        byte[] out = md4.digest (pw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        byte[] result = new byte [21];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        System.arraycopy (out, 0, result, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /* key is a 21 byte array. Split it into 3 7 byte chunks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Convert each to 8 byte DES keys, encrypt the text arg with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * each key and return the three results in a sequential []
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private byte[] calcResponse (byte[] key, byte[] text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        assert key.length == 21;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        DESKeySpec dks1 = new DESKeySpec (makeDesKey (key, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        DESKeySpec dks2 = new DESKeySpec (makeDesKey (key, 7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        DESKeySpec dks3 = new DESKeySpec (makeDesKey (key, 14));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        SecretKey key1 = fac.generateSecret (dks1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        SecretKey key2 = fac.generateSecret (dks2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        SecretKey key3 = fac.generateSecret (dks3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        cipher.init (Cipher.ENCRYPT_MODE, key1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        byte[] out1 = cipher.doFinal (text, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        cipher.init (Cipher.ENCRYPT_MODE, key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        byte[] out2 = cipher.doFinal (text, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        cipher.init (Cipher.ENCRYPT_MODE, key3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        byte[] out3 = cipher.doFinal (text, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        byte[] result = new byte [24];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        System.arraycopy (out1, 0, result, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        System.arraycopy (out2, 0, result, 8, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        System.arraycopy (out3, 0, result, 16, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private String buildType3Msg (String challenge) throws GeneralSecurityException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                                           IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        /* First decode the type2 message to get the server nonce */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        /* nonce is located at type2[24] for 8 bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        byte[] type2 = (new sun.misc.BASE64Decoder()).decodeBuffer (challenge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        byte[] nonce = new byte [8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        System.arraycopy (type2, 24, nonce, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        int ulen = username.length()*2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        type3[36] = type3[38] = (byte) (ulen % 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        type3[37] = type3[39] = (byte) (ulen / 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        int dlen = ntdomain.length()*2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        type3[28] = type3[30] = (byte) (dlen % 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        type3[29] = type3[31] = (byte) (dlen / 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        int hlen = hostname.length()*2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        type3[44] = type3[46] = (byte) (hlen % 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        type3[45] = type3[47] = (byte) (hlen / 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        int l = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        copybytes (type3, l, ntdomain, "UnicodeLittleUnmarked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        type3[32] = (byte) (l % 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        type3[33] = (byte) (l / 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        l += dlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        copybytes (type3, l, username, "UnicodeLittleUnmarked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        type3[40] = (byte) (l % 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        type3[41] = (byte) (l / 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        l += ulen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        copybytes (type3, l, hostname, "UnicodeLittleUnmarked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        type3[48] = (byte) (l % 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        type3[49] = (byte) (l / 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        l += hlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        byte[] lmhash = calcLMHash();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        byte[] lmresponse = calcResponse (lmhash, nonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        byte[] nthash = calcNTHash();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        byte[] ntresponse = calcResponse (nthash, nonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        System.arraycopy (lmresponse, 0, type3, l, 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        type3[16] = (byte) (l % 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        type3[17] = (byte) (l / 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        l += 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        System.arraycopy (ntresponse, 0, type3, l, 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        type3[24] = (byte) (l % 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        type3[25] = (byte) (l / 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        l += 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        type3[56] = (byte) (l % 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        type3[57] = (byte) (l / 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        byte[] msg = new byte [l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        System.arraycopy (type3, 0, msg, 0, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        String result = "NTLM " + (new B64Encoder()).encode (msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
class B64Encoder extends sun.misc.BASE64Encoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /* to force it to to the entire encoding in one line */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    protected int bytesPerLine () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        return 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
}