src/java.security.jgss/share/classes/sun/security/jgss/wrapper/NativeGSSContext.java
author weijun
Thu, 01 Nov 2018 22:41:32 +0800
changeset 52370 cb06c2248720
parent 47216 71c04702a3d5
child 58331 e4ce29f6094e
permissions -rw-r--r--
8212217: JGSS: Don't dispose() of creds too eagerly Reviewed-by: mullan, weijun Contributed-by: Nico Williams <nico@twosigma.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
52370
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2018, 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.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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This class is essentially a wrapper class for the gss_ctx_id_t
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * structure of the native GSS library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
class NativeGSSContext implements GSSContextSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final int GSS_C_DELEG_FLAG = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int GSS_C_MUTUAL_FLAG = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int GSS_C_REPLAY_FLAG = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int GSS_C_SEQUENCE_FLAG = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final int GSS_C_CONF_FLAG = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final int GSS_C_INTEG_FLAG = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final int GSS_C_ANON_FLAG = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final int GSS_C_PROT_READY_FLAG = 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final int GSS_C_TRANS_FLAG = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final int NUM_OF_INQUIRE_VALUES = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private long pContext = 0; // Pointer to the gss_ctx_id_t structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private GSSNameElement srcName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private GSSNameElement targetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private GSSCredElement cred;
52370
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
    66
    private GSSCredElement disposeCred;
2
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;
52370
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
    73
    private GSSCredElement disposeDelegatedCred;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private int flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private int lifetime = GSSCredential.DEFAULT_LIFETIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final GSSLibStub cStub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private boolean skipDelegPermCheck;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean skipServicePermCheck;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // Retrieve the (preferred) mech out of SPNEGO tokens, i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // NegTokenInit & NegTokenTarg
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static Oid getMechFromSpNegoToken(byte[] token,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                              boolean isInitiator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        Oid mech = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (isInitiator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            GSSHeader header = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                header = new GSSHeader(new ByteArrayInputStream(token));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            int negTokenLen = header.getMechTokenLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            byte[] negToken = new byte[negTokenLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            System.arraycopy(token, token.length-negTokenLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                             negToken, 0, negToken.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            NegTokenInit ntok = new NegTokenInit(negToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (ntok.getMechToken() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                Oid[] mechList = ntok.getMechTypeList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                mech = mechList[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            NegTokenTarg ntok = new NegTokenTarg(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            mech = ntok.getSupportedMech();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return mech;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // Perform the Service permission check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private void doServicePermCheck() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (System.getSecurityManager() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            String action = (isInitiator? "initiate" : "accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            // Need to check Service permission for accessing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            // initiator cred for SPNEGO during context establishment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (GSSUtil.isSpNegoMech(cStub.getMech()) && isInitiator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                && !isEstablished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                if (srcName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    // Check by creating default initiator KRB5 cred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    GSSCredElement tempCred =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        new GSSCredElement(null, lifetime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                           GSSCredential.INITIATE_ONLY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                           GSSLibStub.getInstance(GSSUtil.GSS_KRB5_MECH_OID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    tempCred.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    String tgsName = Krb5Util.getTGSName(srcName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    Krb5Util.checkServicePermission(tgsName, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            String targetStr = targetName.getKrbName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            Krb5Util.checkServicePermission(targetStr, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            skipServicePermCheck = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // Perform the Delegation permission check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private void doDelegPermCheck() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            String targetStr = targetName.getKrbName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            String tgsStr = Krb5Util.getTGSName(targetName);
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 5506
diff changeset
   143
            StringBuilder sb = new StringBuilder("\"");
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 5506
diff changeset
   144
            sb.append(targetStr).append("\" \"");
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 5506
diff changeset
   145
            sb.append(tgsStr).append('\"');
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 5506
diff changeset
   146
            String krbPrincPair = sb.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            SunNativeProvider.debug("Checking DelegationPermission (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                    krbPrincPair + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            DelegationPermission perm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                new DelegationPermission(krbPrincPair);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            sm.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            skipDelegPermCheck = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private byte[] retrieveToken(InputStream is, int mechTokenLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            byte[] result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (mechTokenLen != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                // Need to add back the GSS header for a complete GSS token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                SunNativeProvider.debug("Precomputed mechToken length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                         mechTokenLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                GSSHeader gssHeader = new GSSHeader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    (new ObjectIdentifier(cStub.getMech().toString()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                     mechTokenLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                ByteArrayOutputStream baos = new ByteArrayOutputStream(600);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                byte[] mechToken = new byte[mechTokenLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                int len = is.read(mechToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                assert(mechTokenLen == len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                gssHeader.encode(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                baos.write(mechToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                result = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                // Must be unparsed GSS token or SPNEGO's NegTokenTarg token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                assert(mechTokenLen == -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                DerValue dv = new DerValue(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                result = dv.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            SunNativeProvider.debug("Complete Token length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                    result.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // Constructor for context initiator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    NativeGSSContext(GSSNameElement peer, GSSCredElement myCred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                     int time, GSSLibStub stub) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (peer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new GSSException(GSSException.FAILURE, 1, "null peer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        cStub = stub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        cred = myCred;
52370
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   197
        disposeCred = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        targetName = peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        isInitiator = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        lifetime = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (GSSUtil.isKerberosMech(cStub.getMech())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            doServicePermCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (cred == null) {
52370
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   205
                disposeCred = cred =
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   206
                    new GSSCredElement(null, lifetime,
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   207
                            GSSCredential.INITIATE_ONLY, cStub);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            srcName = cred.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    // Constructor for context acceptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    NativeGSSContext(GSSCredElement myCred, GSSLibStub stub)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        cStub = stub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        cred = myCred;
52370
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   218
        disposeCred = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (cred != null) targetName = cred.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        isInitiator = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // Defer Service permission check for default acceptor cred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // to acceptSecContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (GSSUtil.isKerberosMech(cStub.getMech()) && targetName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            doServicePermCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // srcName and potentially targetName (when myCred is null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        // will be set in GSSLibStub.acceptContext(...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    // Constructor for imported context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    NativeGSSContext(long pCtxt, GSSLibStub stub) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        assert(pContext != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        pContext = pCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        cStub = stub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // Set everything except cred, cb, delegatedCred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        long[] info = cStub.inquireContext(pContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (info.length != NUM_OF_INQUIRE_VALUES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new RuntimeException("Bug w/ GSSLibStub.inquireContext()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        srcName = new GSSNameElement(info[0], cStub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        targetName = new GSSNameElement(info[1], cStub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        isInitiator = (info[2] != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        isEstablished = (info[3] != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        flags = (int) info[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        lifetime = (int) info[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // Do Service Permission check when importing SPNEGO context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // just to be safe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        Oid mech = cStub.getMech();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (GSSUtil.isSpNegoMech(mech) || GSSUtil.isKerberosMech(mech)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            doServicePermCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
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 Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return SunNativeProvider.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public byte[] initSecContext(InputStream is, int mechTokenLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        byte[] outToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if ((!isEstablished) && (isInitiator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            byte[] inToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            // Ignore the specified input stream on the first call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (pContext != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                inToken = retrieveToken(is, mechTokenLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                SunNativeProvider.debug("initSecContext=> inToken len=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    inToken.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (!getCredDelegState()) skipDelegPermCheck = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (GSSUtil.isKerberosMech(cStub.getMech()) && !skipDelegPermCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                doDelegPermCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            long pCred = (cred == null? 0 : cred.pCred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            outToken = cStub.initContext(pCred, targetName.pName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                         cb, inToken, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            SunNativeProvider.debug("initSecContext=> outToken len=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                (outToken == null ? 0 : outToken.length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // Only inspect the token when the permission check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // has not been performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (GSSUtil.isSpNegoMech(cStub.getMech()) && outToken != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                // WORKAROUND for SEAM bug#6287358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                actualMech = getMechFromSpNegoToken(outToken, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                if (GSSUtil.isKerberosMech(actualMech)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    if (!skipServicePermCheck) doServicePermCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    if (!skipDelegPermCheck) doDelegPermCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (isEstablished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                if (srcName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    srcName = new GSSNameElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        (cStub.getContextName(pContext, true), cStub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                if (cred == null) {
52370
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   305
                    disposeCred = cred =
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   306
                        new GSSCredElement(srcName, lifetime,
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   307
                                GSSCredential.INITIATE_ONLY, cStub);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return outToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public byte[] acceptSecContext(InputStream is, int mechTokenLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        byte[] outToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if ((!isEstablished) && (!isInitiator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            byte[] inToken = retrieveToken(is, mechTokenLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            SunNativeProvider.debug("acceptSecContext=> inToken len=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                    inToken.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            long pCred = (cred == null? 0 : cred.pCred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            outToken = cStub.acceptContext(pCred, cb, inToken, this);
52370
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   323
            disposeDelegatedCred = delegatedCred;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            SunNativeProvider.debug("acceptSecContext=> outToken len=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                    (outToken == null? 0 : outToken.length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (targetName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                targetName = new GSSNameElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    (cStub.getContextName(pContext, false), cStub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                // Replace the current default acceptor cred now that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                // the context acceptor name is available
52370
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   332
                if (disposeCred != null) {
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   333
                    disposeCred.dispose();
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   334
                }
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   335
                disposeCred = cred =
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   336
                    new GSSCredElement(targetName, lifetime,
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   337
                            GSSCredential.ACCEPT_ONLY, cStub);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            // Only inspect token when the permission check has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            // been performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if (GSSUtil.isSpNegoMech(cStub.getMech()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                (outToken != null) && !skipServicePermCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                if (GSSUtil.isKerberosMech(getMechFromSpNegoToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                           (outToken, false))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    doServicePermCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return outToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public boolean isEstablished() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return isEstablished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public void dispose() throws GSSException {
52370
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   358
        if (disposeCred != null) {
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   359
            disposeCred.dispose();
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   360
        }
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   361
        if (disposeDelegatedCred != null) {
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   362
            disposeDelegatedCred.dispose();
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   363
        }
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   364
        disposeDelegatedCred = disposeCred = cred = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        srcName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        targetName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        delegatedCred = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (pContext != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            pContext = cStub.deleteContext(pContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            pContext = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public int getWrapSizeLimit(int qop, boolean confReq,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                int maxTokenSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return cStub.wrapSizeLimit(pContext, (confReq? 1:0), qop,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                   maxTokenSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public byte[] wrap(byte[] inBuf, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                       MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        byte[] data = inBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if ((offset != 0) || (len != inBuf.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            data = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            System.arraycopy(inBuf, offset, data, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return cStub.wrap(pContext, data, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 26629
diff changeset
   390
    public void wrap(byte[] inBuf, int offset, int len,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                     OutputStream os, MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        byte[] result = wrap(inBuf, offset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        os.write(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
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 int wrap(byte[] inBuf, int inOffset, int len, byte[] outBuf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    int outOffset, MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        byte[] result = wrap(inBuf, inOffset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        System.arraycopy(result, 0, outBuf, outOffset, result.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return result.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public void wrap(InputStream inStream, OutputStream outStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                     MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            byte[] data = new byte[inStream.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            int length = inStream.read(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            byte[] token = wrap(data, 0, length, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            outStream.write(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    public byte[] unwrap(byte[] inBuf, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                         MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if ((offset != 0) || (len != inBuf.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            byte[] temp = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            System.arraycopy(inBuf, offset, temp, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return cStub.unwrap(pContext, temp, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return cStub.unwrap(pContext, inBuf, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public int unwrap(byte[] inBuf, int inOffset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                      byte[] outBuf, int outOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                      MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        byte[] result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if ((inOffset != 0) || (len != inBuf.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            byte[] temp = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            System.arraycopy(inBuf, inOffset, temp, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            result = cStub.unwrap(pContext, temp, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            result = cStub.unwrap(pContext, inBuf, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        System.arraycopy(result, 0, outBuf, outOffset, result.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return result.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public void unwrap(InputStream inStream, OutputStream outStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                       MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            byte[] wrapped = new byte[inStream.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            int wLength = inStream.read(wrapped);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            byte[] data = unwrap(wrapped, 0, wLength, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            outStream.write(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            outStream.flush();
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public int unwrap(InputStream inStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                      byte[] outBuf, int outOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                      MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        byte[] wrapped = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        int wLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            wrapped = new byte[inStream.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            wLength = inStream.read(wrapped);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            byte[] result = unwrap(wrapped, 0, wLength, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        byte[] result = unwrap(wrapped, 0, wLength, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        System.arraycopy(result, 0, outBuf, outOffset, result.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        return result.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public byte[] getMIC(byte[] in, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                         MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        int qop = (msgProp == null? 0:msgProp.getQOP());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        byte[] inMsg = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        if ((offset != 0) || (len != in.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            inMsg = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            System.arraycopy(in, offset, inMsg, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        return cStub.getMic(pContext, qop, inMsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public void getMIC(InputStream inStream, OutputStream outStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                       MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            byte[] msg = new byte[inStream.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            length = inStream.read(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            byte[] msgToken = getMIC(msg, 0, length, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if ((msgToken != null) && msgToken.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                outStream.write(msgToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public void verifyMIC(byte[] inToken, int tOffset, int tLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                          byte[] inMsg, int mOffset, int mLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                          MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        byte[] token = inToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        byte[] msg = inMsg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if ((tOffset != 0) || (tLen != inToken.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            token = new byte[tLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            System.arraycopy(inToken, tOffset, token, 0, tLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        if ((mOffset != 0) || (mLen != inMsg.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            msg = new byte[mLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            System.arraycopy(inMsg, mOffset, msg, 0, mLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        cStub.verifyMic(pContext, token, msg, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public void verifyMIC(InputStream tokStream, InputStream msgStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                          MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            byte[] msg = new byte[msgStream.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            int mLength = msgStream.read(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            byte[] tok = new byte[tokStream.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            int tLength = tokStream.read(tok);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            verifyMIC(tok, 0, tLength, msg, 0, mLength, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
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 byte[] export() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        byte[] result = cStub.exportContext(pContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        pContext = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    private void changeFlags(int flagMask, boolean isEnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (isInitiator && pContext == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (isEnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                flags |= flagMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                flags &= ~flagMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    public void requestMutualAuth(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        changeFlags(GSS_C_MUTUAL_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    public void requestReplayDet(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        changeFlags(GSS_C_REPLAY_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    public void requestSequenceDet(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        changeFlags(GSS_C_SEQUENCE_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    public void requestCredDeleg(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        changeFlags(GSS_C_DELEG_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    public void requestAnonymity(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        changeFlags(GSS_C_ANON_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public void requestConf(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        changeFlags(GSS_C_CONF_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    public void requestInteg(boolean state) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        changeFlags(GSS_C_INTEG_FLAG, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   566
    public void requestDelegPolicy(boolean state) throws GSSException {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   567
        // Not supported, ignore
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   568
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    public void requestLifetime(int lifetime) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (isInitiator && pContext == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            this.lifetime = lifetime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public void setChannelBinding(ChannelBinding cb) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if (pContext == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            this.cb = cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    private boolean checkFlags(int flagMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        return ((flags & flagMask) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public boolean getCredDelegState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        return checkFlags(GSS_C_DELEG_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public boolean getMutualAuthState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        return checkFlags(GSS_C_MUTUAL_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public boolean getReplayDetState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        return checkFlags(GSS_C_REPLAY_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    public boolean getSequenceDetState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return checkFlags(GSS_C_SEQUENCE_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public boolean getAnonymityState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        return checkFlags(GSS_C_ANON_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    public boolean isTransferable() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        return checkFlags(GSS_C_TRANS_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    public boolean isProtReady() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return checkFlags(GSS_C_PROT_READY_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    public boolean getConfState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        return checkFlags(GSS_C_CONF_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    public boolean getIntegState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return checkFlags(GSS_C_INTEG_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   610
    public boolean getDelegPolicyState() {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   611
        return false;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3482
diff changeset
   612
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    public int getLifetime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return cStub.getContextTime(pContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    public GSSNameSpi getSrcName() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        return srcName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    public GSSNameSpi getTargName() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        return targetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    public Oid getMech() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (isEstablished && actualMech != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            return actualMech;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            return cStub.getMech();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    public GSSCredentialSpi getDelegCred() throws GSSException {
52370
cb06c2248720 8212217: JGSS: Don't dispose() of creds too eagerly
weijun
parents: 47216
diff changeset
   630
        disposeDelegatedCred = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return delegatedCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    public boolean isInitiator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        return isInitiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31538
diff changeset
   637
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    }
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2
diff changeset
   641
26629
3b9ed8175488 8042900: Allow com.sun.security.jgss to be in different module than org.ietf.jgss
weijun
parents: 25859
diff changeset
   642
    public Object inquireSecContext(String type)
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2
diff changeset
   643
            throws GSSException {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2
diff changeset
   644
        throw new GSSException(GSSException.UNAVAILABLE, -1,
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2
diff changeset
   645
                "Inquire type not supported.");
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2
diff changeset
   646
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
}