jdk/src/share/classes/sun/security/jgss/wrapper/NativeGSSContext.java
author weijun
Fri, 27 Nov 2009 08:51:28 +0800
changeset 4336 4c792c19266e
parent 3482 4aaa66ce712d
child 5506 202f599c92aa
permissions -rw-r--r--
6853328: Support OK-AS-DELEGATE flag Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2
diff changeset
     2
 * Copyright 2005-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.jgss.wrapper;
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 java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.jgss.GSSHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.jgss.GSSUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.jgss.GSSExceptionImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.jgss.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.jgss.spnego.NegTokenInit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.jgss.spnego.NegTokenTarg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.security.auth.kerberos.DelegationPermission;
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2
diff changeset
    39
import com.sun.security.jgss.InquireType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class is essentially a wrapper class for the gss_ctx_id_t
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * structure of the native GSS library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
class NativeGSSContext implements GSSContextSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int GSS_C_DELEG_FLAG = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int GSS_C_MUTUAL_FLAG = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int GSS_C_REPLAY_FLAG = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final int GSS_C_SEQUENCE_FLAG = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final int GSS_C_CONF_FLAG = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final int GSS_C_INTEG_FLAG = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final int GSS_C_ANON_FLAG = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final int GSS_C_PROT_READY_FLAG = 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final int GSS_C_TRANS_FLAG = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final int NUM_OF_INQUIRE_VALUES = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private long pContext = 0; // Pointer to the gss_ctx_id_t structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private GSSNameElement srcName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private GSSNameElement targetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private GSSCredElement cred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private boolean isInitiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private boolean isEstablished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private Oid actualMech; // Assigned during context establishment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private ChannelBinding cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private GSSCredElement delegatedCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private int flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private int lifetime = GSSCredential.DEFAULT_LIFETIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private final GSSLibStub cStub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private boolean skipDelegPermCheck;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private boolean skipServicePermCheck;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // Retrieve the (preferred) mech out of SPNEGO tokens, i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // NegTokenInit & NegTokenTarg
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static Oid getMechFromSpNegoToken(byte[] token,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                              boolean isInitiator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Oid mech = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (isInitiator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            GSSHeader header = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                header = new GSSHeader(new ByteArrayInputStream(token));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int negTokenLen = header.getMechTokenLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            byte[] negToken = new byte[negTokenLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            System.arraycopy(token, token.length-negTokenLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                             negToken, 0, negToken.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            NegTokenInit ntok = new NegTokenInit(negToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (ntok.getMechToken() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                Oid[] mechList = ntok.getMechTypeList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                mech = mechList[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            NegTokenTarg ntok = new NegTokenTarg(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            mech = ntok.getSupportedMech();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return mech;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // Perform the Service permission check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private void doServicePermCheck() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (System.getSecurityManager() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            String action = (isInitiator? "initiate" : "accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // Need to check Service permission for accessing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            // initiator cred for SPNEGO during context establishment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (GSSUtil.isSpNegoMech(cStub.getMech()) && isInitiator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                && !isEstablished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                if (srcName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    // Check by creating default initiator KRB5 cred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    GSSCredElement tempCred =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        new GSSCredElement(null, lifetime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                           GSSCredential.INITIATE_ONLY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                           GSSLibStub.getInstance(GSSUtil.GSS_KRB5_MECH_OID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    tempCred.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    String tgsName = Krb5Util.getTGSName(srcName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    Krb5Util.checkServicePermission(tgsName, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            String targetStr = targetName.getKrbName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            Krb5Util.checkServicePermission(targetStr, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            skipServicePermCheck = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // Perform the Delegation permission check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private void doDelegPermCheck() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            String targetStr = targetName.getKrbName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            String tgsStr = Krb5Util.getTGSName(targetName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            StringBuffer buf = new StringBuffer("\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            buf.append(targetStr).append("\" \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            buf.append(tgsStr).append('\"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            String krbPrincPair = buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            SunNativeProvider.debug("Checking DelegationPermission (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                    krbPrincPair + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            DelegationPermission perm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                new DelegationPermission(krbPrincPair);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            sm.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            skipDelegPermCheck = true;
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
    private byte[] retrieveToken(InputStream is, int mechTokenLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            byte[] result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (mechTokenLen != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                // Need to add back the GSS header for a complete GSS token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                SunNativeProvider.debug("Precomputed mechToken length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                         mechTokenLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                GSSHeader gssHeader = new GSSHeader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    (new ObjectIdentifier(cStub.getMech().toString()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                     mechTokenLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                ByteArrayOutputStream baos = new ByteArrayOutputStream(600);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                byte[] mechToken = new byte[mechTokenLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                int len = is.read(mechToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                assert(mechTokenLen == len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                gssHeader.encode(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                baos.write(mechToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                result = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                // Must be unparsed GSS token or SPNEGO's NegTokenTarg token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                assert(mechTokenLen == -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                DerValue dv = new DerValue(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                result = dv.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            SunNativeProvider.debug("Complete Token length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                    result.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    // Constructor for context initiator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    NativeGSSContext(GSSNameElement peer, GSSCredElement myCred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                     int time, GSSLibStub stub) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (peer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new GSSException(GSSException.FAILURE, 1, "null peer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        cStub = stub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        cred = myCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        targetName = peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        isInitiator = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        lifetime = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (GSSUtil.isKerberosMech(cStub.getMech())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            doServicePermCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (cred == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                cred = new GSSCredElement(null, lifetime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                          GSSCredential.INITIATE_ONLY, cStub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            srcName = cred.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    // Constructor for context acceptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    NativeGSSContext(GSSCredElement myCred, GSSLibStub stub)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        cStub = stub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        cred = myCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (cred != null) targetName = cred.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        isInitiator = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        // Defer Service permission check for default acceptor cred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // to acceptSecContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (GSSUtil.isKerberosMech(cStub.getMech()) && targetName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            doServicePermCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // srcName and potentially targetName (when myCred is null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // will be set in GSSLibStub.acceptContext(...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    // Constructor for imported context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    NativeGSSContext(long pCtxt, GSSLibStub stub) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        assert(pContext != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        pContext = pCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        cStub = stub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // Set everything except cred, cb, delegatedCred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        long[] info = cStub.inquireContext(pContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (info.length != NUM_OF_INQUIRE_VALUES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new RuntimeException("Bug w/ GSSLibStub.inquireContext()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        srcName = new GSSNameElement(info[0], cStub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        targetName = new GSSNameElement(info[1], cStub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        isInitiator = (info[2] != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        isEstablished = (info[3] != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        flags = (int) info[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        lifetime = (int) info[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // Do Service Permission check when importing SPNEGO context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        // just to be safe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        Oid mech = cStub.getMech();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (GSSUtil.isSpNegoMech(mech) || GSSUtil.isKerberosMech(mech)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            doServicePermCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return SunNativeProvider.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public byte[] initSecContext(InputStream is, int mechTokenLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        byte[] outToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if ((!isEstablished) && (isInitiator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            byte[] inToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // Ignore the specified input stream on the first call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (pContext != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                inToken = retrieveToken(is, mechTokenLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                SunNativeProvider.debug("initSecContext=> inToken len=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    inToken.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (!getCredDelegState()) skipDelegPermCheck = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (GSSUtil.isKerberosMech(cStub.getMech()) && !skipDelegPermCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                doDelegPermCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            long pCred = (cred == null? 0 : cred.pCred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            outToken = cStub.initContext(pCred, targetName.pName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                         cb, inToken, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            SunNativeProvider.debug("initSecContext=> outToken len=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                (outToken == null ? 0 : outToken.length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            // Only inspect the token when the permission check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // has not been performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (GSSUtil.isSpNegoMech(cStub.getMech()) && outToken != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                // WORKAROUND for SEAM bug#6287358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                actualMech = getMechFromSpNegoToken(outToken, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if (GSSUtil.isKerberosMech(actualMech)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    if (!skipServicePermCheck) doServicePermCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    if (!skipDelegPermCheck) doDelegPermCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (isEstablished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                if (srcName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    srcName = new GSSNameElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        (cStub.getContextName(pContext, true), cStub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                if (cred == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    cred = new GSSCredElement(srcName, lifetime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                              GSSCredential.INITIATE_ONLY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                              cStub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return outToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public byte[] acceptSecContext(InputStream is, int mechTokenLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        byte[] outToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if ((!isEstablished) && (!isInitiator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            byte[] inToken = retrieveToken(is, mechTokenLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            SunNativeProvider.debug("acceptSecContext=> inToken len=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                    inToken.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            long pCred = (cred == null? 0 : cred.pCred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            outToken = cStub.acceptContext(pCred, cb, inToken, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            SunNativeProvider.debug("acceptSecContext=> outToken len=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                    (outToken == null? 0 : outToken.length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (targetName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                targetName = new GSSNameElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    (cStub.getContextName(pContext, false), cStub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                // Replace the current default acceptor cred now that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                // the context acceptor name is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                if (cred != null) cred.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                cred = new GSSCredElement(targetName, lifetime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                          GSSCredential.ACCEPT_ONLY, cStub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            // Only inspect token when the permission check has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            // been performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (GSSUtil.isSpNegoMech(cStub.getMech()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                (outToken != null) && !skipServicePermCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (GSSUtil.isKerberosMech(getMechFromSpNegoToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                           (outToken, false))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    doServicePermCheck();
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
        return outToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public boolean isEstablished() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return isEstablished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public void dispose() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        srcName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        targetName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        cred = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        delegatedCred = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (pContext != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            pContext = cStub.deleteContext(pContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            pContext = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public int getWrapSizeLimit(int qop, boolean confReq,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                int maxTokenSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return cStub.wrapSizeLimit(pContext, (confReq? 1:0), qop,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                   maxTokenSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public byte[] wrap(byte[] inBuf, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                       MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        byte[] data = inBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if ((offset != 0) || (len != inBuf.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            data = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            System.arraycopy(inBuf, offset, data, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return cStub.wrap(pContext, data, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public void wrap(byte inBuf[], int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                     OutputStream os, MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        byte[] result = wrap(inBuf, offset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        os.write(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public int wrap(byte[] inBuf, int inOffset, int len, byte[] outBuf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    int outOffset, MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        byte[] result = wrap(inBuf, inOffset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        System.arraycopy(result, 0, outBuf, outOffset, result.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return result.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public void wrap(InputStream inStream, OutputStream outStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                     MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            byte[] data = new byte[inStream.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            int length = inStream.read(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            byte[] token = wrap(data, 0, length, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            outStream.write(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public byte[] unwrap(byte[] inBuf, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                         MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if ((offset != 0) || (len != inBuf.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            byte[] temp = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            System.arraycopy(inBuf, offset, temp, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            return cStub.unwrap(pContext, temp, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return cStub.unwrap(pContext, inBuf, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public int unwrap(byte[] inBuf, int inOffset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                      byte[] outBuf, int outOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                      MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        byte[] result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if ((inOffset != 0) || (len != inBuf.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            byte[] temp = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            System.arraycopy(inBuf, inOffset, temp, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            result = cStub.unwrap(pContext, temp, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            result = cStub.unwrap(pContext, inBuf, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        System.arraycopy(result, 0, outBuf, outOffset, result.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return result.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public void unwrap(InputStream inStream, OutputStream outStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                       MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            byte[] wrapped = new byte[inStream.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            int wLength = inStream.read(wrapped);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            byte[] data = unwrap(wrapped, 0, wLength, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            outStream.write(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            outStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public int unwrap(InputStream inStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                      byte[] outBuf, int outOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                      MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        byte[] wrapped = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        int wLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            wrapped = new byte[inStream.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            wLength = inStream.read(wrapped);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            byte[] result = unwrap(wrapped, 0, wLength, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        byte[] result = unwrap(wrapped, 0, wLength, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        System.arraycopy(result, 0, outBuf, outOffset, result.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return result.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public byte[] getMIC(byte[] in, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                         MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        int qop = (msgProp == null? 0:msgProp.getQOP());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        byte[] inMsg = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if ((offset != 0) || (len != in.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            inMsg = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            System.arraycopy(in, offset, inMsg, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return cStub.getMic(pContext, qop, inMsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    public void getMIC(InputStream inStream, OutputStream outStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                       MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            byte[] msg = new byte[inStream.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            length = inStream.read(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            byte[] msgToken = getMIC(msg, 0, length, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if ((msgToken != null) && msgToken.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                outStream.write(msgToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public void verifyMIC(byte[] inToken, int tOffset, int tLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                          byte[] inMsg, int mOffset, int mLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                          MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        byte[] token = inToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        byte[] msg = inMsg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if ((tOffset != 0) || (tLen != inToken.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            token = new byte[tLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            System.arraycopy(inToken, tOffset, token, 0, tLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if ((mOffset != 0) || (mLen != inMsg.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            msg = new byte[mLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            System.arraycopy(inMsg, mOffset, msg, 0, mLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        cStub.verifyMic(pContext, token, msg, msgProp);
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 verifyMIC(InputStream tokStream, InputStream msgStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                          MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            byte[] msg = new byte[msgStream.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            int mLength = msgStream.read(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            byte[] tok = new byte[tokStream.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            int tLength = tokStream.read(tok);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            verifyMIC(tok, 0, tLength, msg, 0, mLength, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
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 byte[] export() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        byte[] result = cStub.exportContext(pContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        pContext = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    private void changeFlags(int flagMask, boolean isEnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (isInitiator && pContext == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            if (isEnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                flags |= flagMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                flags &= ~flagMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public void requestMutualAuth(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        changeFlags(GSS_C_MUTUAL_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    public void requestReplayDet(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        changeFlags(GSS_C_REPLAY_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    public void requestSequenceDet(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        changeFlags(GSS_C_SEQUENCE_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    public void requestCredDeleg(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        changeFlags(GSS_C_DELEG_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    public void requestAnonymity(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        changeFlags(GSS_C_ANON_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public void requestConf(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        changeFlags(GSS_C_CONF_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    public void requestInteg(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        changeFlags(GSS_C_INTEG_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   552
    public void requestDelegPolicy(boolean state) throws GSSException {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   553
        // Not supported, ignore
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   554
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public void requestLifetime(int lifetime) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (isInitiator && pContext == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            this.lifetime = lifetime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public void setChannelBinding(ChannelBinding cb) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (pContext == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            this.cb = cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    private boolean checkFlags(int flagMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return ((flags & flagMask) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    public boolean getCredDelegState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return checkFlags(GSS_C_DELEG_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    public boolean getMutualAuthState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return checkFlags(GSS_C_MUTUAL_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    public boolean getReplayDetState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        return checkFlags(GSS_C_REPLAY_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    public boolean getSequenceDetState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        return checkFlags(GSS_C_SEQUENCE_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    public boolean getAnonymityState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        return checkFlags(GSS_C_ANON_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    public boolean isTransferable() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        return checkFlags(GSS_C_TRANS_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public boolean isProtReady() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        return checkFlags(GSS_C_PROT_READY_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    public boolean getConfState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        return checkFlags(GSS_C_CONF_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public boolean getIntegState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return checkFlags(GSS_C_INTEG_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   596
    public boolean getDelegPolicyState() {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   597
        return false;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   598
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    public int getLifetime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        return cStub.getContextTime(pContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public GSSNameSpi getSrcName() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        return srcName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public GSSNameSpi getTargName() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return targetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public Oid getMech() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        if (isEstablished && actualMech != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            return actualMech;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            return cStub.getMech();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    public GSSCredentialSpi getDelegCred() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        return delegatedCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    public boolean isInitiator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        return isInitiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2
diff changeset
   625
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2
diff changeset
   626
    public Object inquireSecContext(InquireType type)
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2
diff changeset
   627
            throws GSSException {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2
diff changeset
   628
        throw new GSSException(GSSException.UNAVAILABLE, -1,
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2
diff changeset
   629
                "Inquire type not supported.");
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2
diff changeset
   630
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
}