jdk/src/share/classes/sun/security/jgss/GSSContextImpl.java
author weijun
Wed, 07 Nov 2012 14:13:01 +0800
changeset 14413 e954df027393
parent 5506 202f599c92aa
permissions -rw-r--r--
6355584: Introduce constrained Kerberos delegation Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4336
diff changeset
     2
 * Copyright (c) 2000, 2009, 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: 4336
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: 4336
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: 4336
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4336
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4336
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import org.ietf.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.security.jgss.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
    36
import com.sun.security.jgss.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class represents the JGSS security context and its associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * operations.  JGSS security contexts are established between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * peers using locally established credentials.  Multiple contexts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * may exist simultaneously between a pair of peers, using the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * or different set of credentials.  The JGSS is independent of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the underlying transport protocols and depends on its callers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * transport the tokens between peers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The context object can be thought of as having 3 implicit states:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * before it is established, during its context establishment, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * after a fully established context exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Before the context establishment phase is initiated, the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * initiator may request specific characteristics desired of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * established context. These can be set using the set methods. After the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * context is established, the caller can check the actual characteristic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * and services offered by the context using the query methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * The context establishment phase begins with the first call to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * initSecContext method by the context initiator. During this phase the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * initSecContext and acceptSecContext methods will produce GSS-API
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * authentication tokens which the calling application needs to send to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * peer. The initSecContext and acceptSecContext methods may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * return a CONTINUE_NEEDED code which indicates that a token is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * from its peer in order to continue the context establishment phase. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * return code of COMPLETE signals that the local end of the context is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * established. This may still require that a token be sent to the peer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * depending if one is produced by GSS-API. The isEstablished method can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * also be used to determine if the local end of the context has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * fully established. During the context establishment phase, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * isProtReady method may be called to determine if the context can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * used for the per-message operations. This allows implementation to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * use per-message operations on contexts which aren't fully established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * After the context has been established or the isProtReady method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * returns "true", the query routines can be invoked to determine the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * characteristics and services of the established context. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * application can also start using the per-message methods of wrap and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * getMIC to obtain cryptographic operations on application supplied data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * When the context is no longer needed, the application should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * dispose to release any system resources the context may be using.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <DL><DT><B>RFC 2078</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *    <DD>This class corresponds to the context level calls together with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * the per message calls of RFC 2078. The gss_init_sec_context and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * gss_accept_sec_context calls have been made simpler by only taking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * required parameters.  The context can have its properties set before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * the first call to initSecContext. The supplementary status codes for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * per-message operations are returned in an instance of the MessageProp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * class, which is used as an argument in these calls.</dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 */
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
    90
class GSSContextImpl implements ExtendedGSSContext {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
    92
    private final GSSManagerImpl gssManager;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
    93
    private final boolean initiator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // private flags for the context state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static final int PRE_INIT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static final int IN_PROGRESS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final int READY = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final int DELETED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // instance variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private int currentState = PRE_INIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private GSSContextSpi mechCtxt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private Oid mechOid = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private ObjectIdentifier objId = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private GSSCredentialImpl myCred = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private GSSNameImpl srcName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private GSSNameImpl targName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private int reqLifetime = INDEFINITE_LIFETIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private ChannelBinding channelBindings = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private boolean reqConfState = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private boolean reqIntegState = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private boolean reqMutualAuthState = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private boolean reqReplayDetState = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private boolean reqSequenceDetState = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private boolean reqCredDelegState = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private boolean reqAnonState = false;
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   123
    private boolean reqDelegPolicyState = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Creates a GSSContextImp on the context initiator's side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public GSSContextImpl(GSSManagerImpl gssManager, GSSName peer, Oid mech,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                          GSSCredential myCred, int lifetime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if ((peer == null) || !(peer instanceof GSSNameImpl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new GSSException(GSSException.BAD_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (mech == null) mech = ProviderList.DEFAULT_MECH_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.gssManager = gssManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this.myCred = (GSSCredentialImpl) myCred;  // XXX Check first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        reqLifetime = lifetime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        targName = (GSSNameImpl)peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.mechOid = mech;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        initiator = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Creates a GSSContextImpl on the context acceptor's side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public GSSContextImpl(GSSManagerImpl gssManager, GSSCredential myCred)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        this.gssManager = gssManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.myCred = (GSSCredentialImpl) myCred; // XXX Check first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        initiator = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Creates a GSSContextImpl out of a previously exported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * GSSContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @see #isTransferable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public GSSContextImpl(GSSManagerImpl gssManager, byte[] interProcessToken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.gssManager = gssManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        mechCtxt = gssManager.getMechanismContext(interProcessToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        initiator = mechCtxt.isInitiator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        this.mechOid = mechCtxt.getMech();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public byte[] initSecContext(byte inputBuf[], int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * Size of ByteArrayOutputStream will double each time that extra
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * bytes are to be written. Usually, without delegation, a GSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * initial token containing the Kerberos AP-REQ is between 400 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * 600 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        ByteArrayOutputStream bos = new ByteArrayOutputStream(600);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        ByteArrayInputStream bin =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            new ByteArrayInputStream(inputBuf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        int size = initSecContext(bin, bos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return (size == 0? null : bos.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public int initSecContext(InputStream inStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                              OutputStream outStream) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (mechCtxt != null && currentState != IN_PROGRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw new GSSExceptionImpl(GSSException.FAILURE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                   "Illegal call to initSecContext");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        GSSHeader gssHeader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        int inTokenLen = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        GSSCredentialSpi credElement = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        boolean firstToken = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (mechCtxt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                if (myCred != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        credElement = myCred.getElement(mechOid, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    } catch (GSSException ge) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        if (GSSUtil.isSpNegoMech(mechOid) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                            ge.getMajor() == GSSException.NO_CRED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                            credElement = myCred.getElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                (myCred.getMechs()[0], true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                            throw ge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                GSSNameSpi nameElement = targName.getElement(mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                mechCtxt = gssManager.getMechanismContext(nameElement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                                          credElement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                                          reqLifetime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                                          mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                mechCtxt.requestConf(reqConfState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                mechCtxt.requestInteg(reqIntegState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                mechCtxt.requestCredDeleg(reqCredDelegState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                mechCtxt.requestMutualAuth(reqMutualAuthState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                mechCtxt.requestReplayDet(reqReplayDetState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                mechCtxt.requestSequenceDet(reqSequenceDetState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                mechCtxt.requestAnonymity(reqAnonState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                mechCtxt.setChannelBinding(channelBindings);
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   224
                mechCtxt.requestDelegPolicy(reqDelegPolicyState);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                objId = new ObjectIdentifier(mechOid.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                currentState = IN_PROGRESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                firstToken = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                if (mechCtxt.getProvider().getName().equals("SunNativeGSS") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    GSSUtil.isSpNegoMech(mechOid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    // do not parse GSS header for native provider or SPNEGO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    // mech
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    // parse GSS header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    gssHeader = new GSSHeader(inStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    if (!gssHeader.getOid().equals((Object) objId))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                        throw new GSSExceptionImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                            (GSSException.DEFECTIVE_TOKEN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                             "Mechanism not equal to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                             mechOid.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                             " in initSecContext token");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    inTokenLen = gssHeader.getMechTokenLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            byte[] obuf = mechCtxt.initSecContext(inStream, inTokenLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            int retVal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (obuf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                retVal = obuf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                if (mechCtxt.getProvider().getName().equals("SunNativeGSS") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    (!firstToken && GSSUtil.isSpNegoMech(mechOid))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    // do not add GSS header for native provider or SPNEGO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    // except for the first SPNEGO token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    // add GSS header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    gssHeader = new GSSHeader(objId, obuf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    retVal += gssHeader.encode(outStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                outStream.write(obuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (mechCtxt.isEstablished())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                currentState = READY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            throw new GSSExceptionImpl(GSSException.DEFECTIVE_TOKEN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                   e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public byte[] acceptSecContext(byte inTok[], int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         * Usually initial GSS token containing a Kerberos AP-REP is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         * than 100 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        ByteArrayOutputStream bos = new ByteArrayOutputStream(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        acceptSecContext(new ByteArrayInputStream(inTok, offset, len),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                         bos);
1574
7aad9fe14378 6733095: Failure when SPNEGO request non-Mutual
weijun
parents: 2
diff changeset
   287
        byte[] out = bos.toByteArray();
7aad9fe14378 6733095: Failure when SPNEGO request non-Mutual
weijun
parents: 2
diff changeset
   288
        return (out.length == 0) ? null : out;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public void acceptSecContext(InputStream inStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                 OutputStream outStream) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (mechCtxt != null && currentState != IN_PROGRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throw new GSSExceptionImpl(GSSException.FAILURE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                       "Illegal call to acceptSecContext");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        GSSHeader gssHeader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        int inTokenLen = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        GSSCredentialSpi credElement = null;
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 (mechCtxt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                // mechOid will be null for an acceptor's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                gssHeader = new GSSHeader(inStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                inTokenLen = gssHeader.getMechTokenLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                 * Convert ObjectIdentifier to Oid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                objId = gssHeader.getOid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                mechOid = new Oid(objId.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                // System.out.println("Entered GSSContextImpl.acceptSecContext"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                //                      + " with mechanism = " + mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                if (myCred != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    credElement = myCred.getElement(mechOid, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                mechCtxt = gssManager.getMechanismContext(credElement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                                          mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                mechCtxt.setChannelBinding(channelBindings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                currentState = IN_PROGRESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                if (mechCtxt.getProvider().getName().equals("SunNativeGSS") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    (GSSUtil.isSpNegoMech(mechOid))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    // do not parse GSS header for native provider and SPNEGO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    // parse GSS Header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    gssHeader = new GSSHeader(inStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    if (!gssHeader.getOid().equals((Object) objId))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        throw new GSSExceptionImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                            (GSSException.DEFECTIVE_TOKEN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                             "Mechanism not equal to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                             mechOid.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                             " in acceptSecContext token");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    inTokenLen = gssHeader.getMechTokenLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            byte[] obuf = mechCtxt.acceptSecContext(inStream, inTokenLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (obuf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                int retVal = obuf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                if (mechCtxt.getProvider().getName().equals("SunNativeGSS") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    (GSSUtil.isSpNegoMech(mechOid))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    // do not add GSS header for native provider and SPNEGO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    // add GSS header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    gssHeader = new GSSHeader(objId, obuf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    retVal += gssHeader.encode(outStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                outStream.write(obuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (mechCtxt.isEstablished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                currentState = READY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            throw new GSSExceptionImpl(GSSException.DEFECTIVE_TOKEN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                   e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public boolean isEstablished() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (mechCtxt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return (currentState == READY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public int getWrapSizeLimit(int qop, boolean confReq,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                int maxTokenSize) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return mechCtxt.getWrapSizeLimit(qop, confReq, maxTokenSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                                  "No mechanism context yet!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public byte[] wrap(byte inBuf[], int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                       MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            return mechCtxt.wrap(inBuf, offset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                   "No mechanism context yet!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public void wrap(InputStream inStream, OutputStream outStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                     MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            mechCtxt.wrap(inStream, outStream, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                                  "No mechanism context yet!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public byte [] unwrap(byte[] inBuf, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                          MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            return mechCtxt.unwrap(inBuf, offset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                  "No mechanism context yet!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public void unwrap(InputStream inStream, OutputStream outStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                       MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            mechCtxt.unwrap(inStream, outStream, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                  "No mechanism context yet!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    public byte[] getMIC(byte []inMsg, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                         MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            return mechCtxt.getMIC(inMsg, offset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                                  "No mechanism context yet!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public void getMIC(InputStream inStream, OutputStream outStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                       MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            mechCtxt.getMIC(inStream, outStream, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                  "No mechanism context yet!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    public void verifyMIC(byte[] inTok, int tokOffset, int tokLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                          byte[] inMsg, int msgOffset, int msgLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                          MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            mechCtxt.verifyMIC(inTok, tokOffset, tokLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                               inMsg, msgOffset, msgLen, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                                  "No mechanism context yet!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public void verifyMIC(InputStream tokStream, InputStream msgStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                          MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            mechCtxt.verifyMIC(tokStream, msgStream, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                  "No mechanism context yet!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public byte[] export() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        // Defaults to null to match old behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        byte[] result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        // Only allow context export from native provider since JGSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // still has not defined its own interprocess token format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (mechCtxt.isTransferable() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            mechCtxt.getProvider().getName().equals("SunNativeGSS")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            result = mechCtxt.export();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public void requestMutualAuth(boolean state) throws GSSException {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   469
        if (mechCtxt == null && initiator)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            reqMutualAuthState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public void requestReplayDet(boolean state) throws GSSException {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   474
        if (mechCtxt == null && initiator)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            reqReplayDetState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public void requestSequenceDet(boolean state) throws GSSException {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   479
        if (mechCtxt == null && initiator)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            reqSequenceDetState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public void requestCredDeleg(boolean state) throws GSSException {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   484
        if (mechCtxt == null && initiator)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            reqCredDelegState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    public void requestAnonymity(boolean state) throws GSSException {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   489
        if (mechCtxt == null && initiator)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            reqAnonState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public void requestConf(boolean state) throws GSSException {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   494
        if (mechCtxt == null && initiator)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            reqConfState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public void requestInteg(boolean state) throws GSSException {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   499
        if (mechCtxt == null && initiator)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            reqIntegState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    public void requestLifetime(int lifetime) throws GSSException {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   504
        if (mechCtxt == null && initiator)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            reqLifetime = lifetime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public void setChannelBinding(ChannelBinding channelBindings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        if (mechCtxt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            this.channelBindings = channelBindings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public boolean getCredDelegState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return mechCtxt.getCredDelegState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            return reqCredDelegState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    public boolean getMutualAuthState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            return mechCtxt.getMutualAuthState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            return reqMutualAuthState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public boolean getReplayDetState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            return mechCtxt.getReplayDetState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return reqReplayDetState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    public boolean getSequenceDetState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            return mechCtxt.getSequenceDetState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            return reqSequenceDetState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    public boolean getAnonymityState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            return mechCtxt.getAnonymityState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            return reqAnonState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    public boolean isTransferable() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            return mechCtxt.isTransferable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    public boolean isProtReady() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            return mechCtxt.isProtReady();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    public boolean getConfState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            return mechCtxt.getConfState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            return reqConfState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    public boolean getIntegState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            return mechCtxt.getIntegState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            return reqIntegState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    public int getLifetime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (mechCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            return mechCtxt.getLifetime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            return reqLifetime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public GSSName getSrcName() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (srcName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            srcName = GSSNameImpl.wrapElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                (gssManager, mechCtxt.getSrcName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        return srcName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    public GSSName getTargName() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        if (targName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            targName = GSSNameImpl.wrapElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                (gssManager, mechCtxt.getTargName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        return targName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public Oid getMech() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (mechCtxt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            return mechCtxt.getMech();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return mechOid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public GSSCredential getDelegCred() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (mechCtxt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                                   "No mechanism context yet!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        GSSCredentialSpi delCredElement = mechCtxt.getDelegCred();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        return (delCredElement == null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            null : new GSSCredentialImpl(gssManager, delCredElement));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    public boolean isInitiator() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        return initiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    public void dispose() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        currentState = DELETED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        if (mechCtxt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            mechCtxt.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            mechCtxt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        myCred = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        srcName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        targName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
   633
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   634
    // ExtendedGSSContext methods:
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   635
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
   636
    @Override
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
   637
    public Object inquireSecContext(InquireType type) throws GSSException {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
   638
        SecurityManager security = System.getSecurityManager();
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
   639
        if (security != null) {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
   640
            security.checkPermission(new InquireSecContextPermission(type.toString()));
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
   641
        }
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
   642
        if (mechCtxt == null) {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
   643
            throw new GSSException(GSSException.NO_CONTEXT);
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
   644
        }
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
   645
        return mechCtxt.inquireSecContext(type);
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1574
diff changeset
   646
    }
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   647
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   648
    @Override
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   649
    public void requestDelegPolicy(boolean state) throws GSSException {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   650
        if (mechCtxt == null && initiator)
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   651
            reqDelegPolicyState = state;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   652
    }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   653
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   654
    @Override
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   655
    public boolean getDelegPolicyState() {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   656
        if (mechCtxt != null)
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   657
            return mechCtxt.getDelegPolicyState();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   658
        else
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   659
            return reqDelegPolicyState;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   660
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
}