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