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