jdk/src/share/classes/com/sun/security/sasl/gsskerb/GssKrb5Server.java
author prr
Tue, 15 Jul 2014 11:22:14 -0700
changeset 25522 10d789df41bb
parent 25187 08aff438def8
permissions -rw-r--r--
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes Reviewed-by: psandoz, prr Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 17209
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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 com.sun.security.sasl.gsskerb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.security.sasl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
// JAAS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
// JGSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import org.ietf.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  * Implements the GSSAPI SASL server mechanism for Kerberos V5.
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
    41
  * (<A HREF="http://www.ietf.org/rfc/rfc2222.txt">RFC 2222</A>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  * <a HREF="http://www.ietf.org/internet-drafts/draft-ietf-cat-sasl-gssapi-00.txt">draft-ietf-cat-sasl-gssapi-00.txt</a>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  * Expects thread's Subject to contain server's Kerberos credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  * - If not, underlying KRB5 mech will attempt to acquire Kerberos creds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  *   by logging into Kerberos (via default TextCallbackHandler).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  * - These creds will be used for exchange with client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  * Required callbacks:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  * - AuthorizeCallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  *      handler must verify that authid/authzids are allowed and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  *      authorized ID to be the canonicalized authzid (if applicable).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  * Environment properties that affect behavior of implementation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  * javax.security.sasl.qop
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  * - quality of protection; list of auth, auth-int, auth-conf; default is "auth"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  * javax.security.sasl.maxbuf
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  * - max receive buffer size; default is 65536
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  * javax.security.sasl.sendmaxbuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  * - max send buffer size; default is 65536; (min with client max recv size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
final class GssKrb5Server extends GssKrb5Base implements SaslServer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final String MY_CLASS_NAME = GssKrb5Server.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private int handshakeStage = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private String peer;
14340
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    70
    private String me;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private String authzid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private CallbackHandler cbh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
14340
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    74
    // When serverName is null, the server will be unbound. We need to save and
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    75
    // check the protocol name after the context is established. This value
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    76
    // will be null if serverName is not null.
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    77
    private final String protocolSaved;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Creates a SASL mechanism with server credentials that it needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * to participate in GSS-API/Kerberos v5 authentication exchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * with the client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    GssKrb5Server(String protocol, String serverName,
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
    84
        Map<String, ?> props, CallbackHandler cbh) throws SaslException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        super(props, MY_CLASS_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this.cbh = cbh;
14340
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    89
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    90
        String service;
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    91
        if (serverName == null) {
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    92
            protocolSaved = protocol;
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    93
            service = null;
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    94
        } else {
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    95
            protocolSaved = null;
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    96
            service = protocol + "@" + serverName;
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    97
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        logger.log(Level.FINE, "KRB5SRV01:Using service name: {0}", service);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            GSSManager mgr = GSSManager.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // Create the name for the requested service entity for Krb5 mech
14340
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   105
            GSSName serviceName = service == null ? null:
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   106
                    mgr.createName(service, GSSName.NT_HOSTBASED_SERVICE, KRB5_OID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            GSSCredential cred = mgr.createCredential(serviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                GSSCredential.INDEFINITE_LIFETIME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                KRB5_OID, GSSCredential.ACCEPT_ONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // Create a context using the server's credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            secCtx = mgr.createContext(cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if ((allQop&INTEGRITY_ONLY_PROTECTION) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                // Might need integrity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                secCtx.requestInteg(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if ((allQop&PRIVACY_PROTECTION) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                // Might need privacy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                secCtx.requestConf(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new SaslException("Failure to initialize security context", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        logger.log(Level.FINE, "KRB5SRV02:Initialization complete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
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
     * Processes the response data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * The client sends response data to which the server must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * process using GSS_accept_sec_context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * As per RFC 2222, the GSS authenication completes (GSS_S_COMPLETE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * we do an extra hand shake to determine the negotiated security protection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * and buffer sizes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param responseData A non-null but possible empty byte array containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * response data from the client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @return A non-null byte array containing the challenge to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * sent to the client, or null when no more data is to be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public byte[] evaluateResponse(byte[] responseData) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                "SASL authentication already complete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (logger.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            traceOutput(MY_CLASS_NAME, "evaluateResponse",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                "KRB5SRV03:Response [raw]:", responseData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        switch (handshakeStage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return doHandshake1(responseData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return doHandshake2(responseData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // Security context not established yet; continue with accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                byte[] gssOutToken = secCtx.acceptSecContext(responseData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    0, responseData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (logger.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    traceOutput(MY_CLASS_NAME, "evaluateResponse",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        "KRB5SRV04:Challenge: [after acceptSecCtx]", gssOutToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (secCtx.isEstablished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    handshakeStage = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    peer = secCtx.getSrcName().toString();
14340
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   179
                    me = secCtx.getTargName().toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
14340
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   181
                    logger.log(Level.FINE,
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   182
                            "KRB5SRV05:Peer name is : {0}, my name is : {1}",
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   183
                            new Object[]{peer, me});
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   184
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   185
                    // me might take the form of proto@host or proto/host
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   186
                    if (protocolSaved != null &&
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   187
                            !protocolSaved.equalsIgnoreCase(me.split("[/@]")[0])) {
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   188
                        throw new SaslException(
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   189
                                "GSS context targ name protocol error: " + me);
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   190
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    if (gssOutToken == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        return doHandshake1(EMPTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                return gssOutToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                throw new SaslException("GSS initiate failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private byte[] doHandshake1(byte[] responseData) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // Security context already established. responseData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            // should contain no data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (responseData != null && responseData.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    "Handshake expecting no response data from server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            // Construct 4 octets of data:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            // First octet contains bitmask specifying protections supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // 2nd-4th octets contains max receive buffer of server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            byte[] gssInToken = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            gssInToken[0] = allQop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            intToNetworkByteOrder(recvMaxBufSize, gssInToken, 1, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (logger.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                logger.log(Level.FINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    "KRB5SRV06:Supported protections: {0}; recv max buf size: {1}",
25187
08aff438def8 8048874: Replace uses of 'new Byte', 'new Short' and 'new Character' with appropriate alternative across core classes
prappo
parents: 23010
diff changeset
   224
                    new Object[]{allQop,
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25187
diff changeset
   225
                                 recvMaxBufSize});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            handshakeStage = 2;  // progress to next stage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (logger.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                traceOutput(MY_CLASS_NAME, "doHandshake1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    "KRB5SRV07:Challenge [raw]", gssInToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            byte[] gssOutToken = secCtx.wrap(gssInToken, 0, gssInToken.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                new MessageProp(0 /* gop */, false /* privacy */));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (logger.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                traceOutput(MY_CLASS_NAME, "doHandshake1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    "KRB5SRV08:Challenge [after wrap]", gssOutToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return gssOutToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new SaslException("Problem wrapping handshake1", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    private byte[] doHandshake2(byte[] responseData) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // Expecting 4 octets from client selected protection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // and client's receive buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            byte[] gssOutToken = secCtx.unwrap(responseData, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                responseData.length, new MessageProp(0, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (logger.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                traceOutput(MY_CLASS_NAME, "doHandshake2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    "KRB5SRV09:Response [after unwrap]", gssOutToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            // First octet is a bit-mask specifying the selected protection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            byte selectedQop = gssOutToken[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if ((selectedQop&allQop) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                throw new SaslException("Client selected unsupported protection: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    + selectedQop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if ((selectedQop&PRIVACY_PROTECTION) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                privacy = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                integrity = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            } else if ((selectedQop&INTEGRITY_ONLY_PROTECTION) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                integrity = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            // 2nd-4th octets specifies maximum buffer size expected by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            // client (in network byte order). This is the server's send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            // buffer maximum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            int clntMaxBufSize = networkByteOrderToInt(gssOutToken, 1, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // Determine the max send buffer size based on what the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            // client is able to receive and our specified max
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            sendMaxBufSize = (sendMaxBufSize == 0) ? clntMaxBufSize :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                Math.min(sendMaxBufSize, clntMaxBufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // Update context to limit size of returned buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            rawSendSize = secCtx.getWrapSizeLimit(JGSS_QOP, privacy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                sendMaxBufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (logger.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                logger.log(Level.FINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            "KRB5SRV10:Selected protection: {0}; privacy: {1}; integrity: {2}",
25187
08aff438def8 8048874: Replace uses of 'new Byte', 'new Short' and 'new Character' with appropriate alternative across core classes
prappo
parents: 23010
diff changeset
   291
                    new Object[]{selectedQop,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                 Boolean.valueOf(privacy),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                 Boolean.valueOf(integrity)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                logger.log(Level.FINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
"KRB5SRV11:Client max recv size: {0}; server max send size: {1}; rawSendSize: {2}",
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25187
diff changeset
   296
                    new Object[] {clntMaxBufSize,
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25187
diff changeset
   297
                                  sendMaxBufSize,
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25187
diff changeset
   298
                                  rawSendSize});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // Get authorization identity, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (gssOutToken.length > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    authzid = new String(gssOutToken, 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        gssOutToken.length - 4, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                } catch (UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    throw new SaslException ("Cannot decode authzid", uee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                authzid = peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            logger.log(Level.FINE, "KRB5SRV12:Authzid: {0}", authzid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            AuthorizeCallback acb = new AuthorizeCallback(peer, authzid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            // In Kerberos, realm is embedded in peer name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            cbh.handle(new Callback[] {acb});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (acb.isAuthorized()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                authzid = acb.getAuthorizedID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                completed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                // Authorization failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                throw new SaslException(peer +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    " is not authorized to connect as " + authzid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            throw new SaslException("Final handshake step failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            throw new SaslException("Problem with callback handler", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        } catch (UnsupportedCallbackException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            throw new SaslException("Problem with callback handler", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public String getAuthorizationID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return authzid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throw new IllegalStateException("Authentication incomplete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
14340
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   344
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   345
    public Object getNegotiatedProperty(String propName) {
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   346
        if (!completed) {
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   347
            throw new IllegalStateException("Authentication incomplete");
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   348
        }
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   349
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   350
        Object result;
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   351
        switch (propName) {
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   352
            case Sasl.BOUND_SERVER_NAME:
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   353
                try {
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   354
                    // me might take the form of proto@host or proto/host
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   355
                    result = me.split("[/@]")[1];
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   356
                } catch (Exception e) {
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   357
                    result = null;
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   358
                }
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   359
                break;
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   360
            default:
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   361
                result = super.getNegotiatedProperty(propName);
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   362
        }
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   363
        return result;
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   364
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
}