jdk/src/share/classes/com/sun/security/sasl/ntlm/NTLMClient.java
author weijun
Wed, 09 Jul 2014 15:10:42 +0800
changeset 25396 5e73c95f95db
parent 10348 7d1a82029332
permissions -rw-r--r--
7150092: NTLM authentication fail if user specified a different realm Reviewed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 6517
diff changeset
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     4
 *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    10
 *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    15
 * accompanied this code).
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    16
 *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    20
 *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    23
 * questions.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    24
 */
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    25
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    26
package com.sun.security.sasl.ntlm;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    27
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    28
import com.sun.security.ntlm.Client;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    29
import com.sun.security.ntlm.NTLMException;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    30
import java.io.IOException;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    31
import java.net.InetAddress;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    32
import java.net.UnknownHostException;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    33
import java.util.Map;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    34
import java.util.Random;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    35
import javax.security.auth.callback.Callback;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    36
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    37
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    38
import javax.security.sasl.*;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    39
import javax.security.auth.callback.CallbackHandler;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    40
import javax.security.auth.callback.NameCallback;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    41
import javax.security.auth.callback.PasswordCallback;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    42
import javax.security.auth.callback.UnsupportedCallbackException;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    43
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    44
/**
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    45
  * Required callbacks:
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    46
  * - RealmCallback
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    47
  *    handle can provide domain info for authentication, optional
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    48
  * - NameCallback
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    49
  *    handler must enter username to use for authentication
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    50
  * - PasswordCallback
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    51
  *    handler must enter password for username to use for authentication
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    52
  *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    53
  * Environment properties that affect behavior of implementation:
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    54
  *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    55
  * javax.security.sasl.qop
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    56
  *    String, quality of protection; only "auth" is accepted, default "auth"
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    57
  *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    58
  * com.sun.security.sasl.ntlm.version
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    59
  *    String, name a specific version to use; can be:
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    60
  *      LM/NTLM: Original NTLM v1
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    61
  *      LM: Original NTLM v1, LM only
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    62
  *      NTLM: Original NTLM v1, NTLM only
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    63
  *      NTLM2: NTLM v1 with Client Challenge
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    64
  *      LMv2/NTLMv2: NTLM v2
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    65
  *      LMv2: NTLM v2, LM only
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    66
  *      NTLMv2: NTLM v2, NTLM only
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    67
  *    If not specified, use system property "ntlm.version". If
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    68
  *    still not specified, use default value "LMv2/NTLMv2".
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    69
  *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    70
  * com.sun.security.sasl.ntlm.random
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    71
  *    java.util.Random, the nonce source to be used in NTLM v2 or NTLM v1 with
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    72
  *    Client Challenge. Default null, an internal java.util.Random object
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    73
  *    will be used
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    74
  *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    75
  * Negotiated Properties:
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    76
  *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    77
  * javax.security.sasl.qop
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    78
  *    Always "auth"
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    79
  *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    80
  * com.sun.security.sasl.html.domain
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    81
  *    The domain for the user, provided by the server
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    82
  *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    83
  * @see <a href="http://www.ietf.org/rfc/rfc2222.txt">RFC 2222</a>
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    84
  * - Simple Authentication and Security Layer (SASL)
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    85
  *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    86
  */
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    87
final class NTLMClient implements SaslClient {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    88
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    89
    private static final String NTLM_VERSION =
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    90
            "com.sun.security.sasl.ntlm.version";
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    91
    private static final String NTLM_RANDOM =
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    92
            "com.sun.security.sasl.ntlm.random";
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    93
    private final static String NTLM_DOMAIN =
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    94
            "com.sun.security.sasl.ntlm.domain";
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    95
    private final static String NTLM_HOSTNAME =
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    96
            "com.sun.security.sasl.ntlm.hostname";
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    97
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    98
    private final Client client;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    99
    private final String mech;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   100
    private final Random random;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   101
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   102
    private int step = 0;   // 0-start,1-nego,2-auth,3-done
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   103
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   104
    /**
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   105
     * @param mech non-null
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   106
     * @param authorizationId can be null or empty and ignored
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   107
     * @param protocol non-null for Sasl, useless for NTLM
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   108
     * @param serverName non-null for Sasl, but can be null for NTLM
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   109
     * @param props can be null
10348
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 10336
diff changeset
   110
     * @param cbh can be null for Sasl, already null-checked in factory
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   111
     * @throws SaslException
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   112
     */
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   113
    NTLMClient(String mech, String authzid, String protocol, String serverName,
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 6517
diff changeset
   114
            Map<String, ?> props, CallbackHandler cbh) throws SaslException {
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   115
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   116
        this.mech = mech;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   117
        String version = null;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   118
        Random rtmp = null;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   119
        String hostname = null;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   120
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   121
        if (props != null) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   122
            String qop = (String)props.get(Sasl.QOP);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   123
            if (qop != null && !qop.equals("auth")) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   124
                throw new SaslException("NTLM only support auth");
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   125
            }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   126
            version = (String)props.get(NTLM_VERSION);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   127
            rtmp = (Random)props.get(NTLM_RANDOM);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   128
            hostname = (String)props.get(NTLM_HOSTNAME);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   129
        }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   130
        this.random = rtmp != null ? rtmp : new Random();
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   131
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   132
        if (version == null) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   133
            version = System.getProperty("ntlm.version");
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   134
        }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   135
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   136
        RealmCallback dcb = (serverName != null && !serverName.isEmpty())?
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   137
            new RealmCallback("Realm: ", serverName) :
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   138
            new RealmCallback("Realm: ");
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   139
        NameCallback ncb = (authzid != null && !authzid.isEmpty()) ?
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   140
            new NameCallback("User name: ", authzid) :
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   141
            new NameCallback("User name: ");
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   142
        PasswordCallback pcb =
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   143
            new PasswordCallback("Password: ", false);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   144
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   145
        try {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   146
            cbh.handle(new Callback[] {dcb, ncb, pcb});
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   147
        } catch (UnsupportedCallbackException e) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   148
            throw new SaslException("NTLM: Cannot perform callback to " +
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   149
                "acquire realm, username or password", e);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   150
        } catch (IOException e) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   151
            throw new SaslException(
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   152
                "NTLM: Error acquiring realm, username or password", e);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   153
        }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   154
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   155
        if (hostname == null) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   156
            try {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   157
                hostname = InetAddress.getLocalHost().getCanonicalHostName();
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   158
            } catch (UnknownHostException e) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   159
                hostname = "localhost";
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   160
            }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   161
        }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   162
        try {
25396
5e73c95f95db 7150092: NTLM authentication fail if user specified a different realm
weijun
parents: 10348
diff changeset
   163
            String name = ncb.getName();
5e73c95f95db 7150092: NTLM authentication fail if user specified a different realm
weijun
parents: 10348
diff changeset
   164
            if (name == null) {
5e73c95f95db 7150092: NTLM authentication fail if user specified a different realm
weijun
parents: 10348
diff changeset
   165
                name = authzid;
5e73c95f95db 7150092: NTLM authentication fail if user specified a different realm
weijun
parents: 10348
diff changeset
   166
            }
5e73c95f95db 7150092: NTLM authentication fail if user specified a different realm
weijun
parents: 10348
diff changeset
   167
            String domain = dcb.getText();
5e73c95f95db 7150092: NTLM authentication fail if user specified a different realm
weijun
parents: 10348
diff changeset
   168
            if (domain == null) {
5e73c95f95db 7150092: NTLM authentication fail if user specified a different realm
weijun
parents: 10348
diff changeset
   169
                domain = serverName;
5e73c95f95db 7150092: NTLM authentication fail if user specified a different realm
weijun
parents: 10348
diff changeset
   170
            }
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   171
            client = new Client(version, hostname,
25396
5e73c95f95db 7150092: NTLM authentication fail if user specified a different realm
weijun
parents: 10348
diff changeset
   172
                    name,
5e73c95f95db 7150092: NTLM authentication fail if user specified a different realm
weijun
parents: 10348
diff changeset
   173
                    domain,
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   174
                    pcb.getPassword());
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   175
        } catch (NTLMException ne) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   176
            throw new SaslException(
10348
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 10336
diff changeset
   177
                    "NTLM: client creation failure", ne);
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   178
        }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   179
    }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   180
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   181
    @Override
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   182
    public String getMechanismName() {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   183
        return mech;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   184
    }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   185
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   186
    @Override
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   187
    public boolean isComplete() {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   188
        return step >= 2;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   189
    }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   190
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   191
    @Override
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   192
    public byte[] unwrap(byte[] incoming, int offset, int len)
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   193
            throws SaslException {
10348
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 10336
diff changeset
   194
        throw new IllegalStateException("Not supported.");
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   195
    }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   196
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   197
    @Override
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   198
    public byte[] wrap(byte[] outgoing, int offset, int len)
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   199
            throws SaslException {
10348
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 10336
diff changeset
   200
        throw new IllegalStateException("Not supported.");
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   201
    }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   202
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   203
    @Override
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   204
    public Object getNegotiatedProperty(String propName) {
10348
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 10336
diff changeset
   205
        if (!isComplete()) {
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 10336
diff changeset
   206
            throw new IllegalStateException("authentication not complete");
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 10336
diff changeset
   207
        }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 6517
diff changeset
   208
        switch (propName) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 6517
diff changeset
   209
            case Sasl.QOP:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 6517
diff changeset
   210
                return "auth";
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 6517
diff changeset
   211
            case NTLM_DOMAIN:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 6517
diff changeset
   212
                return client.getDomain();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 6517
diff changeset
   213
            default:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 6517
diff changeset
   214
                return null;
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   215
        }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   216
    }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   217
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   218
    @Override
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   219
    public void dispose() throws SaslException {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   220
        client.dispose();
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   221
    }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   222
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   223
    @Override
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   224
    public boolean hasInitialResponse() {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   225
        return true;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   226
    }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   227
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   228
    @Override
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   229
    public byte[] evaluateChallenge(byte[] challenge) throws SaslException {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   230
        step++;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   231
        if (step == 1) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   232
            return client.type1();
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   233
        } else {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   234
            try {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   235
                byte[] nonce = new byte[8];
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   236
                random.nextBytes(nonce);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   237
                return client.type3(challenge, nonce);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   238
            } catch (NTLMException ex) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   239
                throw new SaslException("Type3 creation failed", ex);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   240
            }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   241
        }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   242
    }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   243
}