jdk/src/share/classes/sun/security/jgss/krb5/Krb5Context.java
author weijun
Tue, 11 Aug 2009 12:20:32 +0800
changeset 3483 a16fce1820ef
parent 3482 4aaa66ce712d
child 4336 4c792c19266e
permissions -rw-r--r--
6821190: more InquireType values for ExtendedGSSContext Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
     2
 * Copyright 2000-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.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
    28
import com.sun.security.jgss.InquireType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import org.ietf.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.jgss.GSSUtil;
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    32
import sun.security.jgss.GSSCaller;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.jgss.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.jgss.TokenTracker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.AccessControlContext;
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
    42
import java.security.Key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.crypto.Cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.security.auth.kerberos.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Implements the mechanism specific context class for the Kerberos v5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * GSS-API mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Mayank Upadhyay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Ram Marti
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
class Krb5Context implements GSSContextSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * The different states that this context can be in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final int STATE_NEW = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final int STATE_IN_PROCESS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final int STATE_DONE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final int STATE_DELETED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private int state = STATE_NEW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Optional features that the application can set and their default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private boolean credDelegState  = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private boolean mutualAuthState  = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private boolean replayDetState  = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private boolean sequenceDetState  = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean confState  = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private boolean integState  = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private int mySeqNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private int peerSeqNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private TokenTracker peerTokenTracker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private CipherHelper cipherHelper = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Separate locks for the sequence numbers allow the application to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * receive tokens at the same time that it is sending tokens. Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * that the application must synchronize the generation and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * transmission of tokens such that tokens are processed in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * order that they are generated. This is important when sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * checking of per-message tokens is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private Object mySeqNumberLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private Object peerSeqNumberLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private EncryptionKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private Krb5NameElement myName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private Krb5NameElement peerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private int lifetime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private boolean initiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private ChannelBinding channelBinding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private Krb5CredElement myCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private Krb5CredElement delegatedCred; // Set only on acceptor side
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /* DESCipher instance used by the corresponding GSSContext */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private Cipher desCipher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // XXX See if the required info from these can be extracted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // stored elsewhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private Credentials serviceCreds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private KrbApReq apReq;
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   117
    final private GSSCaller caller;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static final boolean DEBUG = Krb5Util.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Constructor for Krb5Context to be called on the context initiator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   124
    Krb5Context(GSSCaller caller, Krb5NameElement peerName, Krb5CredElement myCred,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                int lifetime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (peerName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new IllegalArgumentException("Cannot have null peer name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.caller = caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.peerName = peerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.myCred = myCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this.lifetime = lifetime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this.initiator = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Constructor for Krb5Context to be called on the context acceptor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   142
    Krb5Context(GSSCaller caller, Krb5CredElement myCred)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        this.caller = caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.myCred = myCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.initiator = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Constructor for Krb5Context to import a previously exported context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   152
    public Krb5Context(GSSCaller caller, byte [] interProcessToken)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        throw new GSSException(GSSException.UNAVAILABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                               -1, "GSS Import Context not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Method to determine if the context can be exported and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * re-imported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public final boolean isTransferable() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * The lifetime remaining for this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public final int getLifetime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // XXX Return service ticket lifetime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return GSSContext.INDEFINITE_LIFETIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Methods that may be invoked by the GSS framework in response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * to an application request for setting/getting these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * These can only be called on the initiator side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Notice that an application can only request these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * properties. The mechanism may or may not support them. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * application must make getXXX calls after context establishment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * to see if the mechanism implementations on both sides support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * these features. requestAnonymity is an exception where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * application will want to call getAnonymityState prior to sending any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * GSS token during context establishment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Also note that the requests can only be placed before context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * establishment starts. i.e. when state is STATE_NEW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Requests the desired lifetime. Can only be used on the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * initiator's side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public void requestLifetime(int lifetime) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (state == STATE_NEW && isInitiator())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            this.lifetime = lifetime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Requests that confidentiality be available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public final void requestConf(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (state == STATE_NEW && isInitiator())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            confState  = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Is confidentiality available?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public final boolean getConfState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return confState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Requests that integrity be available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public final void requestInteg(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (state == STATE_NEW && isInitiator())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            integState  = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Is integrity available?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public final boolean getIntegState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return integState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Requests that credential delegation be done during context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * establishment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public final void requestCredDeleg(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (state == STATE_NEW && isInitiator())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            credDelegState  = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Is credential delegation enabled?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public final boolean getCredDelegState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return credDelegState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Requests that mutual authentication be done during context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * establishment. Since this is fromm the client's perspective, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * essentially requests that the server be authenticated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public final void requestMutualAuth(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (state == STATE_NEW && isInitiator()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            mutualAuthState  = value;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Is mutual authentication enabled? Since this is from the client's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * perspective, it essentially meas that the server is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * authenticated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public final boolean getMutualAuthState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return mutualAuthState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Requests that replay detection be done on the GSS wrap and MIC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public final void requestReplayDet(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (state == STATE_NEW && isInitiator())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            replayDetState  = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Is replay detection enabled on the GSS wrap and MIC tokens?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * We enable replay detection if sequence checking is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public final boolean getReplayDetState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return replayDetState || sequenceDetState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Requests that sequence checking be done on the GSS wrap and MIC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public final void requestSequenceDet(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (state == STATE_NEW && isInitiator())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            sequenceDetState  = value;
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
     * Is sequence checking enabled on the GSS Wrap and MIC tokens?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * We enable sequence checking if replay detection is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public final boolean getSequenceDetState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return sequenceDetState || replayDetState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Anonymity is a little different in that after an application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * requests anonymity it will want to know whether the mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * can support it or not, prior to sending any tokens across for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * context establishment. Since this is from the initiator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * perspective, it essentially requests that the initiator be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * anonymous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public final void requestAnonymity(boolean value) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // Ignore silently. Application will check back with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // getAnonymityState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    // RFC 2853 actually calls for this to be called after context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    // establishment to get the right answer, but that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    // incorrect. The application may not want to send over any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    // tokens if anonymity is not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public final boolean getAnonymityState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Package private methods invoked by other Krb5 plugin classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Get the context specific DESCipher instance, invoked in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * MessageToken.init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    final CipherHelper getCipherHelper(EncryptionKey ckey) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         EncryptionKey cipherKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         if (cipherHelper == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            cipherKey = (getKey() == null) ? ckey: getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            cipherHelper = new CipherHelper(cipherKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         return cipherHelper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    final int incrementMySequenceNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        int retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        synchronized (mySeqNumberLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            retVal = mySeqNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            mySeqNumber = retVal + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    final void resetMySequenceNumber(int seqNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            System.out.println("Krb5Context setting mySeqNumber to: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                               + seqNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        synchronized (mySeqNumberLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            mySeqNumber = seqNumber;
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
    final void resetPeerSequenceNumber(int seqNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            System.out.println("Krb5Context setting peerSeqNumber to: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                               + seqNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        synchronized (peerSeqNumberLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            peerSeqNumber = seqNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            peerTokenTracker = new TokenTracker(peerSeqNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    final void setKey(EncryptionKey key) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // %%% to do: should clear old cipherHelper first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        cipherHelper = new CipherHelper(key);  // Need to use new key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    private final EncryptionKey getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Called on the acceptor side to store the delegated credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * received in the AcceptSecContextToken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    final void setDelegCred(Krb5CredElement delegatedCred) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        this.delegatedCred = delegatedCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * While the application can only request the following features,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * other classes in the package can call the actual set methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * for them. They are called as context establishment tokens are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * received on an acceptor side and the context feature list that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * the initiator wants becomes known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * This method is also called by InitialToken.OverloadedChecksum if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * TGT is not forwardable and the user requested delegation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    final void setCredDelegState(boolean state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        credDelegState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    final void setMutualAuthState(boolean state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        mutualAuthState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    final void setReplayDetState(boolean state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        replayDetState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    final void setSequenceDetState(boolean state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        sequenceDetState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    final void setConfState(boolean state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        confState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    final void setIntegState(boolean state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        integState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * Sets the channel bindings to be used during context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * establishment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public final void setChannelBinding(ChannelBinding channelBinding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        this.channelBinding = channelBinding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    final ChannelBinding getChannelBinding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return channelBinding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Returns the mechanism oid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @return the Oid of this context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public final Oid getMech() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        return (Krb5MechFactory.GSS_KRB5_MECH_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Returns the context initiator name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @return initiator name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @exception GSSException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public final GSSNameSpi getSrcName() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        return (isInitiator()? myName : peerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Returns the context acceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @return context acceptor(target) name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @exception GSSException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public final GSSNameSpi getTargName() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        return (!isInitiator()? myName : peerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Returns the delegated credential for the context. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * is an optional feature of contexts which not all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * mechanisms will support. A context can be requested to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * support credential delegation by using the <b>CRED_DELEG</b>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * This is only valid on the acceptor side of the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @return GSSCredentialSpi object for the delegated credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @exception GSSException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @see GSSContext#getDelegCredState
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public final GSSCredentialSpi getDelegCred() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        if (state != STATE_IN_PROCESS && state != STATE_DONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            throw new GSSException(GSSException.NO_CONTEXT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (delegatedCred == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            throw new GSSException(GSSException.NO_CRED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        return delegatedCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Tests if this is the initiator side of the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return boolean indicating if this is initiator (true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *  or target (false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    public final boolean isInitiator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        return initiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Tests if the context can be used for per-message service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Context may allow the calls to the per-message service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * functions before being fully established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @return boolean indicating if per-message methods can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *  be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    public final boolean isProtReady() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        return (state == STATE_DONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Initiator context establishment call. This method may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * required to be called several times. A CONTINUE_NEEDED return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * call indicates that more calls are needed after the next token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * is received from the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @param is contains the token received from the peer. On the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *  first call it will be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @return any token required to be sent to the peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *    It is responsibility of the caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *    to send the token to its peer for processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @exception GSSException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public final byte[] initSecContext(InputStream is, int mechTokenSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            byte[] retVal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            InitialToken token = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            int errorCode = GSSException.FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                System.out.println("Entered Krb5Context.initSecContext with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                                   "state=" + printState(state));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            if (!isInitiator()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                throw new GSSException(GSSException.FAILURE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                                       "initSecContext on an acceptor " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                                        "GSSContext");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                if (state == STATE_NEW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    state = STATE_IN_PROCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    errorCode = GSSException.NO_CRED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    if (myCred == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        myCred = Krb5InitCredential.getInstance(caller, myName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                                              GSSCredential.DEFAULT_LIFETIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    } else if (!myCred.isInitiatorCredential()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        throw new GSSException(errorCode, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                                           "No TGT available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    myName = (Krb5NameElement) myCred.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    Credentials tgt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    ((Krb5InitCredential) myCred).getKrb5Credentials();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    checkPermission(peerName.getKrb5PrincipalName().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                    "initiate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                     * If useSubjectCredsonly is true then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                     * we check whether we already have the ticket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                     * for this service in the Subject and reuse it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    final AccessControlContext acc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    if (GSSUtil.useSubjectCredsOnly(caller)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        KerberosTicket kerbTicket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                           // get service ticket from caller's subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                           kerbTicket = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                                new PrivilegedExceptionAction<KerberosTicket>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                                public KerberosTicket run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                                    // XXX to be cleaned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                    // highly consider just calling:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                                    // Subject.getSubject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                    // SubjectComber.find
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                                    // instead of Krb5Util.getTicket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                                    return Krb5Util.getTicket(
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   577
                                        GSSCaller.CALLER_UNKNOWN,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                                        // since it's useSubjectCredsOnly here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                                        // don't worry about the null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                        myName.getKrb5PrincipalName().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                                        peerName.getKrb5PrincipalName().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                                        acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                                System.out.println("Attempt to obtain service"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                                        + " ticket from the subject failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                        if (kerbTicket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                                System.out.println("Found service ticket in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                                                   "the subject" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                                                   kerbTicket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                            // convert Ticket to serviceCreds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                            // XXX Should merge these two object types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                            // avoid converting back and forth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                            serviceCreds = Krb5Util.ticketToCreds(kerbTicket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    if (serviceCreds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                        // either we did not find the serviceCreds in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                        // Subject or useSubjectCreds is false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                            System.out.println("Service ticket not found in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                                               "the subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                        // Get Service ticket using the Kerberos protocols
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                        serviceCreds = Credentials.acquireServiceCreds(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                                     peerName.getKrb5PrincipalName().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                                     tgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                        if (GSSUtil.useSubjectCredsOnly(caller)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                            final Subject subject =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                                AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                                new java.security.PrivilegedAction<Subject>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                                    public Subject run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                        return (Subject.getSubject(acc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                            if (subject != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                !subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                             * Store the service credentials as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                             * javax.security.auth.kerberos.KerberosTicket in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                             * the Subject. We could wait till the context is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                             * succesfully established; however it is easier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                             * to do here and there is no harm indoing it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                                final KerberosTicket kt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                    Krb5Util.credsToTicket(serviceCreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                                AccessController.doPrivileged (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                                    new java.security.PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                                      public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                                        subject.getPrivateCredentials().add(kt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                                // log it for debugging purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                                    System.out.println("Subject is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                                        "readOnly;Kerberos Service "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                        "ticket not stored");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    errorCode = GSSException.FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    token = new InitSecContextToken(this, tgt, serviceCreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    apReq = ((InitSecContextToken)token).getKrbApReq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    retVal = token.encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    myCred = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    if (!getMutualAuthState()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                        state = STATE_DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                        System.out.println("Created InitSecContextToken:\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                            new HexDumpEncoder().encodeBuffer(retVal));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                } else if (state == STATE_IN_PROCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    // No need to write anything;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    // just validate the incoming token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    new AcceptSecContextToken(this, serviceCreds, apReq, is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    serviceCreds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    apReq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    state = STATE_DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    // XXX Use logging API?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                        System.out.println(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        new GSSException(errorCode, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        new GSSException(errorCode, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    public final boolean isEstablished() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        return (state == STATE_DONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * Acceptor's context establishment call. This method may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * required to be called several times. A CONTINUE_NEEDED return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * call indicates that more calls are needed after the next token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * is received from the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @param is contains the token received from the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @return any token required to be sent to the peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *    It is responsibility of the caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *    to send the token to its peer for processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @exception GSSException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    public final byte[] acceptSecContext(InputStream is, int mechTokenSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        byte[] retVal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            System.out.println("Entered Krb5Context.acceptSecContext with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                               "state=" +  printState(state));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        if (isInitiator()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            throw new GSSException(GSSException.FAILURE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                                   "acceptSecContext on an initiator " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                                   "GSSContext");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            if (state == STATE_NEW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                state = STATE_IN_PROCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                if (myCred == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    myCred = Krb5AcceptCredential.getInstance(caller, myName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                } else if (!myCred.isAcceptorCredential()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    throw new GSSException(GSSException.NO_CRED, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                                           "No Secret Key available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                myName = (Krb5NameElement) myCred.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                checkPermission(myName.getKrb5PrincipalName().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                                "accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                EncryptionKey[] secretKeys =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                 ((Krb5AcceptCredential) myCred).getKrb5EncryptionKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                InitSecContextToken token = new InitSecContextToken(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                                                    secretKeys, is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                PrincipalName clientName = token.getKrbApReq().getClient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                peerName = Krb5NameElement.getInstance(clientName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                if (getMutualAuthState()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        retVal = new AcceptSecContextToken(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                                          token.getKrbApReq()).encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                myCred = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                state = STATE_DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                // XXX Use logging API?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                    System.out.println(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * Queries the context for largest data size to accomodate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * the specified protection and be <= maxTokSize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * @param qop the quality of protection that the context will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *  asked to provide.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @param confReq a flag indicating whether confidentiality will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *  requested or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @param outputSize the maximum size of the output token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @return the maximum size for the input message that can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *  provided to the wrap() method in order to guarantee that these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *  requirements are met.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @throws GSSException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    public final int getWrapSizeLimit(int qop, boolean confReq,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                                       int maxTokSize) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        int retVal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        if (cipherHelper.getProto() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            retVal = WrapToken.getSizeLimit(qop, confReq, maxTokSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                                        getCipherHelper(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        } else if (cipherHelper.getProto() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            retVal = WrapToken_v2.getSizeLimit(qop, confReq, maxTokSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                                        getCipherHelper(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * Per-message calls depend on the sequence number. The sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * synchronization is at a finer granularity because wrap and getMIC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * care about the local sequence number (mySeqNumber) where are unwrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * and verifyMIC care about the remote sequence number (peerSeqNumber).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    public final byte[] wrap(byte inBuf[], int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                             MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            System.out.println("Krb5Context.wrap: data=["
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                               + getHexBytes(inBuf, offset, len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                               + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (state != STATE_DONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                               "Wrap called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        byte[] encToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            if (cipherHelper.getProto() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                WrapToken token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                        new WrapToken(this, msgProp, inBuf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                encToken = token.encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            } else if (cipherHelper.getProto() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                WrapToken_v2 token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                        new WrapToken_v2(this, msgProp, inBuf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                encToken = token.encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                System.out.println("Krb5Context.wrap: token=["
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                                   + getHexBytes(encToken, 0, encToken.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                                   + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            return encToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            encToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    public final int wrap(byte inBuf[], int inOffset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                          byte[] outBuf, int outOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                          MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        if (state != STATE_DONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                                   "Wrap called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        int retVal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            if (cipherHelper.getProto() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                WrapToken token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                        new WrapToken(this, msgProp, inBuf, inOffset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                retVal = token.encode(outBuf, outOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            } else if (cipherHelper.getProto() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                WrapToken_v2 token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                        new WrapToken_v2(this, msgProp, inBuf, inOffset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                retVal = token.encode(outBuf, outOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                System.out.println("Krb5Context.wrap: token=["
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                                   + getHexBytes(outBuf, outOffset, retVal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                                   + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            retVal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    public final void wrap(byte inBuf[], int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                           OutputStream os, MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        if (state != STATE_DONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                                       "Wrap called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        byte[] encToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            if (cipherHelper.getProto() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                WrapToken token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                        new WrapToken(this, msgProp, inBuf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                token.encode(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    encToken = token.encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            } else if (cipherHelper.getProto() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                WrapToken_v2 token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                        new WrapToken_v2(this, msgProp, inBuf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                token.encode(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                    encToken = token.encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            System.out.println("Krb5Context.wrap: token=["
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                        + getHexBytes(encToken, 0, encToken.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                        + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    public final void wrap(InputStream is, OutputStream os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                            MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            data = new byte[is.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            is.read(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        wrap(data, 0, data.length, os, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    public final byte[] unwrap(byte inBuf[], int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                               MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                System.out.println("Krb5Context.unwrap: token=["
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                                   + getHexBytes(inBuf, offset, len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                                   + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            if (state != STATE_DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                                       " Unwrap called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            byte[] data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            if (cipherHelper.getProto() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                WrapToken token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                        new WrapToken(this, inBuf, offset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                data = token.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                setSequencingAndReplayProps(token, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            } else if (cipherHelper.getProto() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                WrapToken_v2 token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                        new WrapToken_v2(this, inBuf, offset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                data = token.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                setSequencingAndReplayProps(token, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                System.out.println("Krb5Context.unwrap: data=["
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                                   + getHexBytes(data, 0, data.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                                   + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    public final int unwrap(byte inBuf[], int inOffset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                             byte[] outBuf, int outOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                             MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        if (state != STATE_DONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                                   "Unwrap called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        if (cipherHelper.getProto() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            WrapToken token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                        new WrapToken(this, inBuf, inOffset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            len = token.getData(outBuf, outOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            setSequencingAndReplayProps(token, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        } else if (cipherHelper.getProto() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            WrapToken_v2 token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                        new WrapToken_v2(this, inBuf, inOffset, len, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            len = token.getData(outBuf, outOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            setSequencingAndReplayProps(token, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    public final int unwrap(InputStream is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                            byte[] outBuf, int outOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                            MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        if (state != STATE_DONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                                   "Unwrap called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        if (cipherHelper.getProto() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            WrapToken token = new WrapToken(this, is, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            len = token.getData(outBuf, outOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            setSequencingAndReplayProps(token, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        } else if (cipherHelper.getProto() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            WrapToken_v2 token = new WrapToken_v2(this, is, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            len = token.getData(outBuf, outOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            setSequencingAndReplayProps(token, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    public final void unwrap(InputStream is, OutputStream os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                             MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        if (state != STATE_DONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            throw new GSSException(GSSException.NO_CONTEXT, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                                   "Unwrap called in invalid state!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        byte[] data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        if (cipherHelper.getProto() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            WrapToken token = new WrapToken(this, is, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            data = token.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            setSequencingAndReplayProps(token, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        } else if (cipherHelper.getProto() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            WrapToken_v2 token = new WrapToken_v2(this, is, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            data = token.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            setSequencingAndReplayProps(token, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            os.write(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    public final byte[] getMIC(byte []inMsg, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                               MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            byte[] micToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                if (cipherHelper.getProto() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    MicToken token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                        new MicToken(this, msgProp, inMsg, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    micToken = token.encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                } else if (cipherHelper.getProto() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                    MicToken_v2 token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                        new MicToken_v2(this, msgProp, inMsg, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    micToken = token.encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                return micToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                micToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                    new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    private int getMIC(byte []inMsg, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                       byte[] outBuf, int outOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                       MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        int retVal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            if (cipherHelper.getProto() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                MicToken token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                        new MicToken(this, msgProp, inMsg, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                retVal = token.encode(outBuf, outOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            } else if (cipherHelper.getProto() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                MicToken_v2 token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                        new MicToken_v2(this, msgProp, inMsg, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                retVal = token.encode(outBuf, outOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            retVal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * Checksum calculation requires a byte[]. Hence might as well pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * a byte[] into the MicToken constructor. However, writing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * token can be optimized for cases where the application passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * an OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    private void getMIC(byte[] inMsg, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                        OutputStream os, MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            if (cipherHelper.getProto() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                MicToken token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                        new MicToken(this, msgProp, inMsg, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                token.encode(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            } else if (cipherHelper.getProto() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                MicToken_v2 token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                        new MicToken_v2(this, msgProp, inMsg, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                token.encode(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    public final void getMIC(InputStream is, OutputStream os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                              MessageProp msgProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            data = new byte[is.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            is.read(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        getMIC(data, 0, data.length, os, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    public final void verifyMIC(byte []inTok, int tokOffset, int tokLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                                byte[] inMsg, int msgOffset, int msgLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                                MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        if (cipherHelper.getProto() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            MicToken token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                new MicToken(this, inTok, tokOffset, tokLen, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            token.verify(inMsg, msgOffset, msgLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            setSequencingAndReplayProps(token, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        } else if (cipherHelper.getProto() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            MicToken_v2 token =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                new MicToken_v2(this, inTok, tokOffset, tokLen, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            token.verify(inMsg, msgOffset, msgLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            setSequencingAndReplayProps(token, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    private void verifyMIC(InputStream is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                           byte[] inMsg, int msgOffset, int msgLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                           MessageProp msgProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        if (cipherHelper.getProto() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            MicToken token = new MicToken(this, is, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            token.verify(inMsg, msgOffset, msgLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            setSequencingAndReplayProps(token, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        } else if (cipherHelper.getProto() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            MicToken_v2 token = new MicToken_v2(this, is, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            token.verify(inMsg, msgOffset, msgLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            setSequencingAndReplayProps(token, msgProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    public final void verifyMIC(InputStream is, InputStream msgStr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                                 MessageProp mProp) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        byte[] msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            msg = new byte[msgStr.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            msgStr.read(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                new GSSException(GSSException.FAILURE, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            throw gssException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        verifyMIC(is, msg, 0, msg.length, mProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * Produces a token representing this context. After this call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * the context will no longer be usable until an import is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * performed on the returned token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * @param os the output token will be written to this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * @exception GSSException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    public final byte [] export() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        throw new GSSException(GSSException.UNAVAILABLE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                               "GSS Export Context not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * Releases context resources and terminates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * context between 2 peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * @exception GSSException with major codes NO_CONTEXT, FAILURE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    public final void dispose() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        state = STATE_DELETED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        delegatedCred = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        return Krb5MechFactory.PROVIDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * Sets replay and sequencing information for a message token received
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * form the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    private void setSequencingAndReplayProps(MessageToken token,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                                             MessageProp prop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        if (replayDetState || sequenceDetState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            int seqNum = token.getSequenceNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            peerTokenTracker.getProps(seqNum, prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * Sets replay and sequencing information for a message token received
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * form the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    private void setSequencingAndReplayProps(MessageToken_v2 token,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                                             MessageProp prop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        if (replayDetState || sequenceDetState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            int seqNum = token.getSequenceNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            peerTokenTracker.getProps(seqNum, prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    private void checkPermission(String principal, String action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            ServicePermission perm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                new ServicePermission(principal, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            sm.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    private static String getHexBytes(byte[] bytes, int pos, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            int b1 = (bytes[i]>>4) & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            int b2 = bytes[i] & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            sb.append(Integer.toHexString(b1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            sb.append(Integer.toHexString(b2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            sb.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    private static String printState(int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
          case STATE_NEW:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                return ("STATE_NEW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
          case STATE_IN_PROCESS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                return ("STATE_IN_PROCESS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
          case STATE_DONE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                return ("STATE_DONE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
          case STATE_DELETED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                return ("STATE_DELETED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                return ("Unknown state " + state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
  1284
    GSSCaller getCaller() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        // Currently used by InitialToken only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        return caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    }
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1288
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1289
    /**
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1290
     * The session key returned by inquireSecContext(KRB5_INQ_SSPI_SESSION_KEY)
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1291
     */
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1292
    static class KerberosSessionKey implements Key {
3483
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1293
        private final EncryptionKey key;
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1294
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1295
        KerberosSessionKey(EncryptionKey key) {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1296
            this.key = key;
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1297
        }
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1298
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1299
        @Override
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1300
        public String getAlgorithm() {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1301
            return Integer.toString(key.getEType());
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1302
        }
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1303
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1304
        @Override
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1305
        public String getFormat() {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1306
            return "RAW";
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1307
        }
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1308
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1309
        @Override
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1310
        public byte[] getEncoded() {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1311
            return key.getBytes().clone();
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1312
        }
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1313
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1314
        @Override
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1315
        public String toString() {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1316
            return "Kerberos session key: etype: " + key.getEType() + "\n" +
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1317
                    new sun.misc.HexDumpEncoder().encodeBuffer(key.getBytes());
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1318
        }
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1319
    }
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1320
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1321
    /**
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1322
     * Return the mechanism-specific attribute associated with {@code type}.
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1323
     */
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1324
    public Object inquireSecContext(InquireType type)
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1325
            throws GSSException {
3483
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1326
        if (!isEstablished()) {
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1327
             throw new GSSException(GSSException.NO_CONTEXT, -1,
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1328
                     "Security context not established.");
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1329
        }
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1330
        switch (type) {
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1331
            case KRB5_GET_SESSION_KEY:
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1332
                return new KerberosSessionKey(key);
3483
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1333
            case KRB5_GET_TKT_FLAGS:
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1334
                return tktFlags.clone();
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1335
            case KRB5_GET_AUTHZ_DATA:
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1336
                if (isInitiator()) {
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1337
                    throw new GSSException(GSSException.UNAVAILABLE, -1,
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1338
                            "AuthzData not available on initiator side.");
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1339
                } else {
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1340
                    return (authzData==null)?null:authzData.clone();
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1341
                }
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1342
            case KRB5_GET_AUTHTIME:
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1343
                return authTime;
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1344
        }
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1345
        throw new GSSException(GSSException.UNAVAILABLE, -1,
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1346
                "Inquire type not supported.");
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 2942
diff changeset
  1347
    }
3483
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1348
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1349
    // Helpers for inquireSecContext
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1350
    private boolean[] tktFlags;
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1351
    private String authTime;
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1352
    private com.sun.security.jgss.AuthorizationDataEntry[] authzData;
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1353
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1354
    public void setTktFlags(boolean[] tktFlags) {
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1355
        this.tktFlags = tktFlags;
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1356
    }
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1357
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1358
    public void setAuthTime(String authTime) {
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1359
        this.authTime = authTime;
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1360
    }
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1361
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1362
    public void setAuthzData(com.sun.security.jgss.AuthorizationDataEntry[] authzData) {
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1363
        this.authzData = authzData;
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
  1364
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
}