jdk/src/share/classes/sun/net/www/protocol/http/NegotiatorImpl.java
author weijun
Tue, 09 Jun 2009 14:17:05 +0800
changeset 2942 37d9baeb7518
parent 715 f16baef3a20e
permissions -rw-r--r--
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication() Reviewed-by: chegar, valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
     2
 * Copyright 2005-2009 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.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import org.ietf.jgss.GSSContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import org.ietf.jgss.GSSException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import org.ietf.jgss.GSSName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import org.ietf.jgss.Oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.jgss.GSSManagerImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.jgss.GSSUtil;
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
    37
import sun.security.jgss.HttpCaller;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
    40
 * This class encapsulates all JAAS and JGSS API calls in a separate class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * outside NegotiateAuthentication.java so that J2SE build can go smoothly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * without the presence of it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author weijun.wang@sun.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class NegotiatorImpl extends Negotiator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final boolean DEBUG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
              new sun.security.action.GetBooleanAction("sun.security.krb5.debug"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private GSSContext context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private byte[] oneToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Initialize the object, which includes:<ul>
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
    58
     * <li>Find out what GSS mechanism to use from the system property
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
    59
     * <code>http.negotiate.mechanism.oid</code>, defaults SPNEGO
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * <li>Creating the GSSName for the target host, "HTTP/"+hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * <li>Creating GSSContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * <li>A first call to initSecContext</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
    64
    private void init(HttpCallerInfo hci) throws GSSException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        final Oid oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
    67
        if (hci.scheme.equalsIgnoreCase("Kerberos")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            // we can only use Kerberos mech when the scheme is kerberos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            oid = GSSUtil.GSS_KRB5_MECH_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            String pref = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    new java.security.PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                            return System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                "http.auth.preference",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                "spnego");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (pref.equalsIgnoreCase("kerberos")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                oid = GSSUtil.GSS_KRB5_MECH_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                // currently there is no 3rd mech we can use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                oid = GSSUtil.GSS_SPNEGO_MECH_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        GSSManagerImpl manager = new GSSManagerImpl(
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
    88
                new HttpCaller(hci));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
    90
        // RFC 4559 4.1 uses uppercase service name "HTTP".
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
    91
        // RFC 4120 6.2.1 demands the host be lowercase
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
    92
        String peerName = "HTTP@" + hci.host.toLowerCase();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
288
ea538d348fe8 6670362: HTTP/SPNEGO should work across realms
weijun
parents: 2
diff changeset
    94
        GSSName serverName = manager.createName(peerName,
ea538d348fe8 6670362: HTTP/SPNEGO should work across realms
weijun
parents: 2
diff changeset
    95
                GSSName.NT_HOSTBASED_SERVICE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        context = manager.createContext(serverName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                        oid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                        GSSContext.DEFAULT_LIFETIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // In order to support credential delegation in HTTP/SPNEGO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // we always request it before initSecContext. The current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // implementation will check the OK-AS-DELEGATE flag inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // the service ticket of the web server, and only enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // delegation when this flag is set. This check is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // performed when the GSS caller is CALLER_HTTP_NEGOTIATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // so all other normal GSS-API calls are not affected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        context.requestCredDeleg(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        oneToken = context.initSecContext(new byte[0], 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @throws java.io.IOException If negotiator cannot be constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
   117
    public NegotiatorImpl(HttpCallerInfo hci) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        try {
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
   119
            init(hci);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (DEBUG) {
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
   122
                System.out.println("Negotiate support not initiated, will " +
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 715
diff changeset
   123
                        "fallback to other scheme if allowed. Reason:");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            IOException ioe = new IOException("Negotiate support not initiated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            ioe.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
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
     * Return the first token of GSS, in SPNEGO, it's called NegTokenInit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @return the first token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public byte[] firstToken() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return oneToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Return the rest tokens of GSS, in SPNEGO, it's called NegTokenTarg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param token the token received from server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @return the next token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @throws java.io.IOException if the token cannot be created successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public byte[] nextToken(byte[] token) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return context.initSecContext(token, 0, token.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                System.out.println("Negotiate support cannot continue. Reason:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            IOException ioe = new IOException("Negotiate support cannot continue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            ioe.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}