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