jdk/src/share/classes/sun/security/jgss/spnego/SpNegoContext.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 20473 801df8862d7a
child 21591 35320b590d9b
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2011, 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: 4337
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: 4337
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: 4337
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4337
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4337
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 sun.security.jgss.spnego;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
    28
import com.sun.security.jgss.ExtendedGSSContext;
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
    29
import com.sun.security.jgss.InquireType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import org.ietf.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.jgss.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Implements the mechanism specific context class for SPNEGO
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * GSS-API mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Seema Malkani
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class SpNegoContext implements GSSContextSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * The different states that this context can be in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int STATE_NEW = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final int STATE_IN_PROCESS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int STATE_DONE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int STATE_DELETED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int state = STATE_NEW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Optional features that the application can set and their default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean credDelegState = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private boolean mutualAuthState = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private boolean replayDetState = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private boolean sequenceDetState = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private boolean confState = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private boolean integState = true;
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
    66
    private boolean delegPolicyState = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private GSSNameSpi peerName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private GSSNameSpi myName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private SpNegoCredElement myCred = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private GSSContext mechContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private byte[] DER_mechTypes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private int lifetime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private ChannelBinding channelBinding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private boolean initiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // the underlying negotiated mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private Oid internal_mech = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // the SpNegoMechFactory that creates this context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    final private SpNegoMechFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // debug property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static final boolean DEBUG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            new sun.security.action.GetBooleanAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ("sun.security.spnego.debug")).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Constructor for SpNegoContext to be called on the context initiator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public SpNegoContext(SpNegoMechFactory factory, GSSNameSpi peerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        GSSCredentialSpi myCred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        int lifetime) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (peerName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new IllegalArgumentException("Cannot have null peer name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if ((myCred != null) && !(myCred instanceof SpNegoCredElement)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new IllegalArgumentException("Wrong cred element type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.peerName = peerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.myCred = (SpNegoCredElement) myCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.lifetime = lifetime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.initiator = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.factory = factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Constructor for SpNegoContext to be called on the context acceptor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public SpNegoContext(SpNegoMechFactory factory, GSSCredentialSpi myCred)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if ((myCred != null) && !(myCred instanceof SpNegoCredElement)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw new IllegalArgumentException("Wrong cred element type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.myCred = (SpNegoCredElement) myCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.initiator = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.factory = factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Constructor for SpNegoContext to import a previously exported context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public SpNegoContext(SpNegoMechFactory factory, byte [] interProcessToken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        throw new GSSException(GSSException.UNAVAILABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                               -1, "GSS Import Context not available");
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
     * Requests that confidentiality be available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public final void requestConf(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (state == STATE_NEW && isInitiator())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            confState  = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Is confidentiality available?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public final boolean getConfState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return confState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Requests that integrity be available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public final void requestInteg(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (state == STATE_NEW && isInitiator())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            integState  = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   158
     * Requests that deleg policy be respected.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   159
     */
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   160
    public final void requestDelegPolicy(boolean value) throws GSSException {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   161
        if (state == STATE_NEW && isInitiator())
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   162
            delegPolicyState = value;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   163
    }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   164
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   165
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Is integrity available?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public final boolean getIntegState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return integState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   173
     * Is deleg policy respected?
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   174
     */
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   175
    public final boolean getDelegPolicyState() {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   176
        if (isInitiator() && mechContext != null &&
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   177
                mechContext instanceof ExtendedGSSContext &&
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   178
                (state == STATE_IN_PROCESS || state == STATE_DONE)) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   179
            return ((ExtendedGSSContext)mechContext).getDelegPolicyState();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   180
        } else {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   181
            return delegPolicyState;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   182
        }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   183
    }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   184
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   185
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Requests that credential delegation be done during context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * establishment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public final void requestCredDeleg(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (state == STATE_NEW && isInitiator())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            credDelegState  = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Is credential delegation enabled?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public final boolean getCredDelegState() {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   198
        if (isInitiator() && mechContext != null &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                (state == STATE_IN_PROCESS || state == STATE_DONE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return mechContext.getCredDelegState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return credDelegState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Requests that mutual authentication be done during context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * establishment. Since this is fromm the client's perspective, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * essentially requests that the server be authenticated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public final void requestMutualAuth(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (state == STATE_NEW && isInitiator()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            mutualAuthState  = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Is mutual authentication enabled? Since this is from the client's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * perspective, it essentially meas that the server is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * authenticated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public final boolean getMutualAuthState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return mutualAuthState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Returns the mechanism oid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return the Oid of this context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public final Oid getMech() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (isEstablished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return getNegotiatedMech();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return (SpNegoMechFactory.GSS_SPNEGO_MECH_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public final Oid getNegotiatedMech() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return (internal_mech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return SpNegoMechFactory.PROVIDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public final void dispose() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        mechContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        state = STATE_DELETED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Tests if this is the initiator side of the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @return boolean indicating if this is initiator (true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *  or target (false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public final boolean isInitiator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return initiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Tests if the context can be used for per-message service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Context may allow the calls to the per-message service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * functions before being fully established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @return boolean indicating if per-message methods can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *  be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public final boolean isProtReady() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return (state == STATE_DONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Initiator context establishment call. This method may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * required to be called several times. A CONTINUE_NEEDED return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * call indicates that more calls are needed after the next token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * is received from the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param is contains the token received from the peer. On the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *        first call it will be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @return any token required to be sent to the peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * It is responsibility of the caller to send the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * to its peer for processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @exception GSSException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public final byte[] initSecContext(InputStream is, int mechTokenSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        byte[] retVal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        NegTokenInit initToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        byte[] mechToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int errorCode = GSSException.FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            System.out.println("Entered SpNego.initSecContext with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                "state=" + printState(state));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (!isInitiator()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            throw new GSSException(GSSException.FAILURE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                "initSecContext on an acceptor GSSContext");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (state == STATE_NEW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                state = STATE_IN_PROCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                errorCode = GSSException.NO_CRED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                // determine available mech set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                Oid[] mechList = getAvailableMechs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                DER_mechTypes = getEncodedMechs(mechList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                // pull out first mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                internal_mech = mechList[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                // get the token for first mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                mechToken = GSS_initSecContext(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                errorCode = GSSException.DEFECTIVE_TOKEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                // generate SPNEGO token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                initToken = new NegTokenInit(DER_mechTypes, getContextFlags(),
4337
2a6d13ebbbed 6901085: SPNEGO does not works with native program
weijun
parents: 4336
diff changeset
   322
                                        mechToken, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    System.out.println("SpNegoContext.initSecContext: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                "sending token of type = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                SpNegoToken.getTokenName(initToken.getType()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                // get the encoded token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                retVal = initToken.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            } else if (state == STATE_IN_PROCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                errorCode = GSSException.FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    throw new GSSException(errorCode, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                "No token received from peer!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                errorCode = GSSException.DEFECTIVE_TOKEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                byte[] server_token = new byte[is.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                SpNegoToken.readFully(is, server_token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    System.out.println("SpNegoContext.initSecContext: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                        "process received token = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                        SpNegoToken.getHexBytes(server_token));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                // read the SPNEGO token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                // token will be validated when parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                NegTokenTarg targToken = new NegTokenTarg(server_token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    System.out.println("SpNegoContext.initSecContext: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                "received token of type = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                SpNegoToken.getTokenName(targToken.getType()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                // pull out mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                internal_mech = targToken.getSupportedMech();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                if (internal_mech == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    // return wth failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    throw new GSSException(errorCode, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                "supported mechansim from server is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                // get the negotiated result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                SpNegoToken.NegoResult negoResult = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                int result = targToken.getNegotiatedResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                switch (result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        negoResult = SpNegoToken.NegoResult.ACCEPT_COMPLETE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        state = STATE_DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        negoResult = SpNegoToken.NegoResult.ACCEPT_INCOMPLETE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        state = STATE_IN_PROCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        negoResult = SpNegoToken.NegoResult.REJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        state = STATE_DELETED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        state = STATE_DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                errorCode = GSSException.BAD_MECH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                if (negoResult == SpNegoToken.NegoResult.REJECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    throw new GSSException(errorCode, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                        internal_mech.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                errorCode = GSSException.DEFECTIVE_TOKEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if ((negoResult == SpNegoToken.NegoResult.ACCEPT_COMPLETE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    (negoResult == SpNegoToken.NegoResult.ACCEPT_INCOMPLETE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    // pull out the mechanism token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    byte[] accept_token = targToken.getResponseToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    if (accept_token == null) {
1574
7aad9fe14378 6733095: Failure when SPNEGO request non-Mutual
weijun
parents: 2
diff changeset
   402
                        if (!isMechContextEstablished()) {
7aad9fe14378 6733095: Failure when SPNEGO request non-Mutual
weijun
parents: 2
diff changeset
   403
                            // return with failure
7aad9fe14378 6733095: Failure when SPNEGO request non-Mutual
weijun
parents: 2
diff changeset
   404
                            throw new GSSException(errorCode, -1,
7aad9fe14378 6733095: Failure when SPNEGO request non-Mutual
weijun
parents: 2
diff changeset
   405
                                    "mechanism token from server is null");
7aad9fe14378 6733095: Failure when SPNEGO request non-Mutual
weijun
parents: 2
diff changeset
   406
                        }
7aad9fe14378 6733095: Failure when SPNEGO request non-Mutual
weijun
parents: 2
diff changeset
   407
                    } else {
7aad9fe14378 6733095: Failure when SPNEGO request non-Mutual
weijun
parents: 2
diff changeset
   408
                        mechToken = GSS_initSecContext(accept_token);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    // verify MIC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    if (!GSSUtil.useMSInterop()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        byte[] micToken = targToken.getMechListMIC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        if (!verifyMechListMIC(DER_mechTypes, micToken)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                            throw new GSSException(errorCode, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                "verification of MIC on MechList Failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    if (isMechContextEstablished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        state = STATE_DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        retVal = mechToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                            System.out.println("SPNEGO Negotiated Mechanism = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                                + internal_mech + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                                GSSUtil.getMechStr(internal_mech));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        // generate SPNEGO token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        initToken = new NegTokenInit(null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                                                mechToken, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                            System.out.println("SpNegoContext.initSecContext:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                " continue sending token of type = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                SpNegoToken.getTokenName(initToken.getType()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        // get the encoded token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        retVal = initToken.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                // XXX Use logging API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    System.out.println(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                if (retVal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    System.out.println("SNegoContext.initSecContext: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        "sending token = " + SpNegoToken.getHexBytes(retVal));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        new GSSException(errorCode, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Acceptor's context establishment call. This method may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * required to be called several times. A CONTINUE_NEEDED return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * call indicates that more calls are needed after the next token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * is received from the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @param is contains the token received from the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @return any token required to be sent to the peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * It is responsibility of the caller to send the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * to its peer for processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @exception GSSException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public final byte[] acceptSecContext(InputStream is, int mechTokenSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        byte[] retVal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        SpNegoToken.NegoResult negoResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        boolean valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            System.out.println("Entered SpNegoContext.acceptSecContext with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                               "state=" +  printState(state));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (isInitiator()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            throw new GSSException(GSSException.FAILURE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                   "acceptSecContext on an initiator " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                                   "GSSContext");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if (state == STATE_NEW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                state = STATE_IN_PROCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                // read data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                byte[] token = new byte[is.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                SpNegoToken.readFully(is, token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    System.out.println("SpNegoContext.acceptSecContext: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                                        "receiving token = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                                        SpNegoToken.getHexBytes(token));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                // read the SPNEGO token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                // token will be validated when parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                NegTokenInit initToken = new NegTokenInit(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    System.out.println("SpNegoContext.acceptSecContext: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                "received token of type = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                SpNegoToken.getTokenName(initToken.getType()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                Oid[] mechList = initToken.getMechTypeList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                DER_mechTypes = initToken.getMechTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                if (DER_mechTypes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                // get the mechanism token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                byte[] mechToken = initToken.getMechToken();
20473
801df8862d7a 8024861: Incomplete token triggers GSS-API NullPointerException
weijun
parents: 14413
diff changeset
   528
                if (mechToken == null) {
801df8862d7a 8024861: Incomplete token triggers GSS-API NullPointerException
weijun
parents: 14413
diff changeset
   529
                    throw new GSSException(GSSException.FAILURE, -1,
801df8862d7a 8024861: Incomplete token triggers GSS-API NullPointerException
weijun
parents: 14413
diff changeset
   530
                            "mechToken is missing");
801df8862d7a 8024861: Incomplete token triggers GSS-API NullPointerException
weijun
parents: 14413
diff changeset
   531
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                 * Select the best match between the list of mechs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                 * that the initiator requested and the list that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                 * the acceptor will support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                Oid[] supported_mechSet = getAvailableMechs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                Oid mech_wanted =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                        negotiate_mech_type(supported_mechSet, mechList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                if (mech_wanted == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                }
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20473
diff changeset
   544
                // save the desired mechanism
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                internal_mech = mech_wanted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                // get the token for mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                byte[] accept_token = GSS_acceptSecContext(mechToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                // verify MIC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                if (!GSSUtil.useMSInterop() && valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    valid = verifyMechListMIC(DER_mechTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                                initToken.getMechListMIC());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                // determine negotiated result status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                if (valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    if (isMechContextEstablished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                        negoResult = SpNegoToken.NegoResult.ACCEPT_COMPLETE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                        state = STATE_DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        // now set the context flags for acceptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        setContextFlags();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        // print the negotiated mech info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                            System.out.println("SPNEGO Negotiated Mechanism = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                                + internal_mech + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                                GSSUtil.getMechStr(internal_mech));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        negoResult = SpNegoToken.NegoResult.ACCEPT_INCOMPLETE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        state = STATE_IN_PROCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    negoResult = SpNegoToken.NegoResult.REJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    state = STATE_DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    System.out.println("SpNegoContext.acceptSecContext: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                "mechanism wanted = " + mech_wanted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    System.out.println("SpNegoContext.acceptSecContext: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                                "negotiated result = " + negoResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                // generate SPNEGO token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                NegTokenTarg targToken = new NegTokenTarg(negoResult.ordinal(),
4337
2a6d13ebbbed 6901085: SPNEGO does not works with native program
weijun
parents: 4336
diff changeset
   587
                                mech_wanted, accept_token, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    System.out.println("SpNegoContext.acceptSecContext: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                                "sending token of type = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                SpNegoToken.getTokenName(targToken.getType()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                // get the encoded token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                retVal = targToken.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            } else if (state == STATE_IN_PROCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                // read the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                byte[] client_token = new byte[is.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                SpNegoToken.readFully(is, client_token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                byte[] accept_token = GSS_acceptSecContext(client_token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                if (accept_token == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                // determine negotiated result status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                if (valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    if (isMechContextEstablished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        negoResult = SpNegoToken.NegoResult.ACCEPT_COMPLETE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                        state = STATE_DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                        negoResult = SpNegoToken.NegoResult.ACCEPT_INCOMPLETE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                        state = STATE_IN_PROCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    negoResult = SpNegoToken.NegoResult.REJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    state = STATE_DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                // generate SPNEGO token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                NegTokenTarg targToken = new NegTokenTarg(negoResult.ordinal(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                null, accept_token, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    System.out.println("SpNegoContext.acceptSecContext: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                "sending token of type = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                                SpNegoToken.getTokenName(targToken.getType()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                // get the encoded token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                retVal = targToken.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                // XXX Use logging API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    System.out.println("AcceptSecContext: state = " + state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    System.out.println("SpNegoContext.acceptSecContext: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                        "sending token = " + SpNegoToken.getHexBytes(retVal));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   647
        if (state == STATE_DONE) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   648
            // now set the context flags for acceptor
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   649
            setContextFlags();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   650
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * obtain the available mechanisms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    private Oid[] getAvailableMechs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if (myCred != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            Oid[] mechs = new Oid[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            mechs[0] = myCred.getInternalMech();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            return mechs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            return factory.availableMechs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * get ther DER encoded MechList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    private byte[] getEncodedMechs(Oid[] mechSet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        throws IOException, GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        DerOutputStream mech = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        for (int i = 0; i < mechSet.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            byte[] mechType = mechSet[i].getDER();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            mech.write(mechType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        // insert in SEQUENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        DerOutputStream mechTypeList = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        mechTypeList.write(DerValue.tag_Sequence, mech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        byte[] encoded = mechTypeList.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * get the context flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     */
2279
e5639c0d8552 6815182: GSSAPI/SPNEGO does not work with server using MIT Kerberos library
weijun
parents: 1574
diff changeset
   688
    private BitArray getContextFlags() {
e5639c0d8552 6815182: GSSAPI/SPNEGO does not work with server using MIT Kerberos library
weijun
parents: 1574
diff changeset
   689
        BitArray out = new BitArray(7);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
2279
e5639c0d8552 6815182: GSSAPI/SPNEGO does not work with server using MIT Kerberos library
weijun
parents: 1574
diff changeset
   691
        if (getCredDelegState()) out.set(0, true);
e5639c0d8552 6815182: GSSAPI/SPNEGO does not work with server using MIT Kerberos library
weijun
parents: 1574
diff changeset
   692
        if (getMutualAuthState()) out.set(1, true);
e5639c0d8552 6815182: GSSAPI/SPNEGO does not work with server using MIT Kerberos library
weijun
parents: 1574
diff changeset
   693
        if (getReplayDetState()) out.set(2, true);
e5639c0d8552 6815182: GSSAPI/SPNEGO does not work with server using MIT Kerberos library
weijun
parents: 1574
diff changeset
   694
        if (getSequenceDetState()) out.set(3, true);
e5639c0d8552 6815182: GSSAPI/SPNEGO does not work with server using MIT Kerberos library
weijun
parents: 1574
diff changeset
   695
        if (getConfState()) out.set(5, true);
e5639c0d8552 6815182: GSSAPI/SPNEGO does not work with server using MIT Kerberos library
weijun
parents: 1574
diff changeset
   696
        if (getIntegState()) out.set(6, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
2279
e5639c0d8552 6815182: GSSAPI/SPNEGO does not work with server using MIT Kerberos library
weijun
parents: 1574
diff changeset
   698
        return out;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   701
    // Only called on acceptor side. On the initiator side, most flags
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   702
    // are already set at request. For those that might get chanegd,
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   703
    // state from mech below is used.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    private void setContextFlags() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            // default for cred delegation is false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (mechContext.getCredDelegState()) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   709
                credDelegState = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            // default for the following are true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            if (!mechContext.getMutualAuthState()) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   713
                mutualAuthState = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            if (!mechContext.getReplayDetState()) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   716
                replayDetState = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            if (!mechContext.getSequenceDetState()) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   719
                sequenceDetState = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            if (!mechContext.getIntegState()) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   722
                integState = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            if (!mechContext.getConfState()) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   725
                confState = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
4337
2a6d13ebbbed 6901085: SPNEGO does not works with native program
weijun
parents: 4336
diff changeset
   731
     * generate MIC on mechList. Not used at the moment.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     */
4337
2a6d13ebbbed 6901085: SPNEGO does not works with native program
weijun
parents: 4336
diff changeset
   733
    /*private byte[] generateMechListMIC(byte[] mechTypes)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        // sanity check the required input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (mechTypes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                System.out.println("SpNegoContext: no MIC token included");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20473
diff changeset
   744
        // check if mechanism supports integrity
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if (!mechContext.getIntegState()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                System.out.println("SpNegoContext: no MIC token included" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                        " - mechanism does not support integrity");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        // compute MIC on DER encoded mechanism list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        byte[] mic = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            MessageProp prop = new MessageProp(0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            mic = getMIC(mechTypes, 0, mechTypes.length, prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                System.out.println("SpNegoContext: getMIC = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                                        SpNegoToken.getHexBytes(mic));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            mic = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                System.out.println("SpNegoContext: no MIC token included" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                        " - getMIC failed : " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        return mic;
4337
2a6d13ebbbed 6901085: SPNEGO does not works with native program
weijun
parents: 4336
diff changeset
   770
    }*/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * verify MIC on MechList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    private boolean verifyMechListMIC(byte[] mechTypes, byte[] token)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        // sanity check the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if (token == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                System.out.println("SpNegoContext: no MIC token validation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20473
diff changeset
   786
        // check if mechanism supports integrity
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        if (!mechContext.getIntegState()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                System.out.println("SpNegoContext: no MIC token validation" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                        " - mechanism does not support integrity");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        // now verify the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        boolean valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            MessageProp prop = new MessageProp(0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            verifyMIC(token, 0, token.length, mechTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                        0, mechTypes.length, prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                System.out.println("SpNegoContext: MIC validation failed! " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                                        e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        return valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * call gss_init_sec_context for the corresponding underlying mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    private byte[] GSS_initSecContext(byte[] token) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        byte[] tok = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        if (mechContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            // initialize mech context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            GSSName serverName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                factory.manager.createName(peerName.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    peerName.getStringNameType(), internal_mech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            GSSCredential cred = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            if (myCred != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                // create context with provided credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                cred = new GSSCredentialImpl(factory.manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                    myCred.getInternalCred());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            mechContext =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                factory.manager.createContext(serverName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    internal_mech, cred, GSSContext.DEFAULT_LIFETIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            mechContext.requestConf(confState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            mechContext.requestInteg(integState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            mechContext.requestCredDeleg(credDelegState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            mechContext.requestMutualAuth(mutualAuthState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            mechContext.requestReplayDet(replayDetState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            mechContext.requestSequenceDet(sequenceDetState);
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   838
            if (mechContext instanceof ExtendedGSSContext) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   839
                ((ExtendedGSSContext)mechContext).requestDelegPolicy(
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   840
                        delegPolicyState);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   841
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        // pass token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        if (token != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            tok = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            tok = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        // pass token to mechanism initSecContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        byte[] init_token = mechContext.initSecContext(tok, 0, tok.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        return init_token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * call gss_accept_sec_context for the corresponding underlying mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    private byte[] GSS_acceptSecContext(byte[] token) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        if (mechContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            // initialize mech context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            GSSCredential cred = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            if (myCred != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                // create context with provided credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                cred = new GSSCredentialImpl(factory.manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                myCred.getInternalCred());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            mechContext =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                factory.manager.createContext(cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        // pass token to mechanism acceptSecContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        byte[] accept_token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                mechContext.acceptSecContext(token, 0, token.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        return accept_token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * This routine compares the recieved mechset to the mechset that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * this server can support. It looks sequentially through the mechset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * and the first one that matches what the server can support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * chosen as the negotiated mechanism. If one is found, negResult
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * is set to ACCEPT_COMPLETE, otherwise we return NULL and negResult
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * is set to REJECT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    private static Oid negotiate_mech_type(Oid[] supported_mechSet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                                        Oid[] mechSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        for (int i = 0; i < supported_mechSet.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            for (int j = 0; j < mechSet.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                if (mechSet[j].equals(supported_mechSet[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                        System.out.println("SpNegoContext: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                                "negotiated mechanism = " + mechSet[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    return (mechSet[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    public final boolean isEstablished() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        return (state == STATE_DONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    public final boolean isMechContextEstablished() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            return mechContext.isEstablished();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                System.out.println("The underlying mechansim context has " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                                        "not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    public final byte [] export() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        throw new GSSException(GSSException.UNAVAILABLE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                               "GSS Export Context not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * Sets the channel bindings to be used during context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * establishment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    public final void setChannelBinding(ChannelBinding channelBinding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        this.channelBinding = channelBinding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    final ChannelBinding getChannelBinding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        return channelBinding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * Anonymity is a little different in that after an application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * requests anonymity it will want to know whether the mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * can support it or not, prior to sending any tokens across for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * context establishment. Since this is from the initiator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * perspective, it essentially requests that the initiator be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * anonymous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    public final void requestAnonymity(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        // Ignore silently. Application will check back with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        // getAnonymityState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    // RFC 2853 actually calls for this to be called after context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    // establishment to get the right answer, but that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    // incorrect. The application may not want to send over any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    // tokens if anonymity is not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    public final boolean getAnonymityState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Requests the desired lifetime. Can only be used on the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * initiator's side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    public void requestLifetime(int lifetime) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        if (state == STATE_NEW && isInitiator())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            this.lifetime = lifetime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * The lifetime remaining for this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    public final int getLifetime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            return mechContext.getLifetime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            return GSSContext.INDEFINITE_LIFETIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    public final boolean isTransferable() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * Requests that sequence checking be done on the GSS wrap and MIC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    public final void requestSequenceDet(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        if (state == STATE_NEW && isInitiator())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            sequenceDetState  = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * Is sequence checking enabled on the GSS Wrap and MIC tokens?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * We enable sequence checking if replay detection is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    public final boolean getSequenceDetState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        return sequenceDetState || replayDetState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * Requests that replay detection be done on the GSS wrap and MIC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    public final void requestReplayDet(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        if (state == STATE_NEW && isInitiator())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            replayDetState  = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * Is replay detection enabled on the GSS wrap and MIC tokens?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * We enable replay detection if sequence checking is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    public final boolean getReplayDetState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        return replayDetState || sequenceDetState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    public final GSSNameSpi getTargName() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        // fill-in the GSSName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        // get the peer name for the mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            GSSNameImpl targName = (GSSNameImpl)mechContext.getTargName();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1023
            peerName = targName.getElement(internal_mech);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            return peerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                System.out.println("The underlying mechansim context has " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                                        "not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    public final GSSNameSpi getSrcName() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        // fill-in the GSSName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        // get the src name for the mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            GSSNameImpl srcName = (GSSNameImpl)mechContext.getSrcName();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1039
            myName = srcName.getElement(internal_mech);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            return myName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                System.out.println("The underlying mechansim context has " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                                        "not been initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * Returns the delegated credential for the context. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * is an optional feature of contexts which not all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * mechanisms will support. A context can be requested to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * support credential delegation by using the <b>CRED_DELEG</b>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * This is only valid on the acceptor side of the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * @return GSSCredentialSpi object for the delegated credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * @exception GSSException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * @see GSSContext#getDelegCredState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    public final GSSCredentialSpi getDelegCred() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        if (state != STATE_IN_PROCESS && state != STATE_DONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            throw new GSSException(GSSException.NO_CONTEXT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            GSSCredentialImpl delegCred =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                        (GSSCredentialImpl)mechContext.getDelegCred();
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 10336
diff changeset
  1066
            if (delegCred == null) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 10336
diff changeset
  1067
                return null;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 10336
diff changeset
  1068
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            // determine delegated cred element usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            boolean initiate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            if (delegCred.getUsage() == GSSCredential.INITIATE_ONLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                initiate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1074
            GSSCredentialSpi mechCred =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1075
                    delegCred.getElement(internal_mech, initiate);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            SpNegoCredElement cred = new SpNegoCredElement(mechCred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            return cred.getInternalCred();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                                "getDelegCred called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    public final int getWrapSizeLimit(int qop, boolean confReq,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                                       int maxTokSize) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            return mechContext.getWrapSizeLimit(qop, confReq, maxTokSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                                "getWrapSizeLimit called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    public final byte[] wrap(byte inBuf[], int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                             MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            return mechContext.wrap(inBuf, offset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                                "Wrap called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    public final void wrap(InputStream is, OutputStream os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                            MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            mechContext.wrap(is, os, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                                "Wrap called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    public final byte[] unwrap(byte inBuf[], int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                               MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            return mechContext.unwrap(inBuf, offset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                                "UnWrap called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    public final void unwrap(InputStream is, OutputStream os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                             MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            mechContext.unwrap(is, os, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                                "UnWrap called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    public final byte[] getMIC(byte []inMsg, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                               MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            return mechContext.getMIC(inMsg, offset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                                "getMIC called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    public final void getMIC(InputStream is, OutputStream os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                              MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            mechContext.getMIC(is, os, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                                "getMIC called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    public final void verifyMIC(byte []inTok, int tokOffset, int tokLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                                byte[] inMsg, int msgOffset, int msgLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                                MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            mechContext.verifyMIC(inTok, tokOffset, tokLen, inMsg, msgOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                                msgLen,  msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                                "verifyMIC called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    public final void verifyMIC(InputStream is, InputStream msgStr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                                 MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        if (mechContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            mechContext.verifyMIC(is, msgStr, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                                "verifyMIC called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    private static String printState(int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
          case STATE_NEW:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                return ("STATE_NEW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
          case STATE_IN_PROCESS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                return ("STATE_IN_PROCESS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
          case STATE_DONE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                return ("STATE_DONE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
          case STATE_DELETED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                return ("STATE_DELETED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                return ("Unknown state " + state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    }
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1193
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1194
    /**
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1195
     * Retrieve attribute of the context for {@code type}.
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1196
     */
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1197
    public Object inquireSecContext(InquireType type)
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1198
            throws GSSException {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1199
        if (mechContext == null) {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1200
            throw new GSSException(GSSException.NO_CONTEXT, -1,
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1201
                    "Underlying mech not established.");
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1202
        }
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1203
        if (mechContext instanceof ExtendedGSSContext) {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1204
            return ((ExtendedGSSContext)mechContext).inquireSecContext(type);
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1205
        } else {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1206
            throw new GSSException(GSSException.BAD_MECH, -1,
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1207
                    "inquireSecContext not supported by underlying mech.");
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1208
        }
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1209
    }
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
  1210
}
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2279
diff changeset
  1211