jdk/src/share/classes/sun/security/jgss/krb5/InitialToken.java
author weijun
Fri, 27 Nov 2009 08:51:28 +0800
changeset 4336 4c792c19266e
parent 3050 c0ce59daa004
child 5506 202f599c92aa
permissions -rw-r--r--
6853328: Support OK-AS-DELEGATE flag Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
     2
 * Copyright 2000-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.jgss.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import org.ietf.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.security.auth.kerberos.DelegationPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.Inet4Address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.Inet6Address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.NoSuchAlgorithmException;
3050
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
    36
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.krb5.*;
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    38
import sun.security.jgss.HttpCaller;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.krb5.internal.Krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
abstract class InitialToken extends Krb5Token {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final int CHECKSUM_TYPE = 0x8003;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final int CHECKSUM_LENGTH_SIZE     = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final int CHECKSUM_BINDINGS_SIZE   = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final int CHECKSUM_FLAGS_SIZE      = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final int CHECKSUM_DELEG_OPT_SIZE  = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int CHECKSUM_DELEG_LGTH_SIZE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int CHECKSUM_DELEG_FLAG    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int CHECKSUM_MUTUAL_FLAG   = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int CHECKSUM_REPLAY_FLAG   = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final int CHECKSUM_SEQUENCE_FLAG = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final int CHECKSUM_CONF_FLAG     = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final int CHECKSUM_INTEG_FLAG    = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final byte[] CHECKSUM_FIRST_BYTES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    {(byte)0x10, (byte)0x00, (byte)0x00, (byte)0x00};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final int CHANNEL_BINDING_AF_INET = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final int CHANNEL_BINDING_AF_INET6 = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final int CHANNEL_BINDING_AF_NULL_ADDR = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final int Inet4_ADDRSZ = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final int Inet6_ADDRSZ = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected class OverloadedChecksum {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        private byte[] checksumBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        private Credentials delegCreds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        private int flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         * Called on the initiator side when creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         * InitSecContextToken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        public OverloadedChecksum(Krb5Context context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                  Credentials tgt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                  Credentials serviceTicket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throws KrbException, IOException, GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            byte[] krbCredMessage = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            int size = CHECKSUM_LENGTH_SIZE + CHECKSUM_BINDINGS_SIZE +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                CHECKSUM_FLAGS_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
    88
            if (!tgt.isForwardable()) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
    89
                context.setCredDelegState(false);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
    90
                context.setDelegPolicyState(false);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
    91
            } else if (context.getCredDelegState()) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
    92
                if (context.getDelegPolicyState()) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
    93
                    if (!serviceTicket.checkDelegate()) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
    94
                        // delegation not permitted by server policy, mark it
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
    95
                        context.setDelegPolicyState(false);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
    96
                    }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
    97
                }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
    98
            } else if (context.getDelegPolicyState()) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
    99
                if (serviceTicket.checkDelegate()) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   100
                    context.setCredDelegState(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                } else {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   102
                    context.setDelegPolicyState(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   106
            if (context.getCredDelegState()) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   107
                KrbCred krbCred = null;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   108
                CipherHelper cipherHelper =
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   109
                    context.getCipherHelper(serviceTicket.getSessionKey());
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   110
                if (useNullKey(cipherHelper)) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   111
                    krbCred = new KrbCred(tgt, serviceTicket,
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   112
                                              EncryptionKey.NULL_KEY);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   113
                } else {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   114
                    krbCred = new KrbCred(tgt, serviceTicket,
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   115
                                    serviceTicket.getSessionKey());
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   116
                }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   117
                krbCredMessage = krbCred.getMessage();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   118
                size += CHECKSUM_DELEG_OPT_SIZE +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   119
                        CHECKSUM_DELEG_LGTH_SIZE +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   120
                        krbCredMessage.length;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   121
            }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   122
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            checksumBytes = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            checksumBytes[pos++] = CHECKSUM_FIRST_BYTES[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            checksumBytes[pos++] = CHECKSUM_FIRST_BYTES[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            checksumBytes[pos++] = CHECKSUM_FIRST_BYTES[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            checksumBytes[pos++] = CHECKSUM_FIRST_BYTES[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            ChannelBinding localBindings = context.getChannelBinding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (localBindings != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                byte[] localBindingsBytes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    computeChannelBinding(context.getChannelBinding());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                System.arraycopy(localBindingsBytes, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                             checksumBytes, pos, localBindingsBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                //              System.out.println("ChannelBinding hash: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                //         + getHexBytes(localBindingsBytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            pos += CHECKSUM_BINDINGS_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (context.getCredDelegState())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                flags |= CHECKSUM_DELEG_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (context.getMutualAuthState())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                flags |= CHECKSUM_MUTUAL_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (context.getReplayDetState())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                flags |= CHECKSUM_REPLAY_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (context.getSequenceDetState())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                flags |= CHECKSUM_SEQUENCE_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (context.getIntegState())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                flags |= CHECKSUM_INTEG_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (context.getConfState())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                flags |= CHECKSUM_CONF_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            byte[] temp = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            writeLittleEndian(flags, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            checksumBytes[pos++] = temp[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            checksumBytes[pos++] = temp[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            checksumBytes[pos++] = temp[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            checksumBytes[pos++] = temp[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (context.getCredDelegState()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                PrincipalName delegateTo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    serviceTicket.getServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                // Cannot use '\"' instead of "\"" in constructor because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                // it is interpreted as suggested length!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                StringBuffer buf = new StringBuffer("\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                buf.append(delegateTo.getName()).append('\"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                String realm = delegateTo.getRealmAsString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                buf.append(" \"krbtgt/").append(realm).append('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                buf.append(realm).append('\"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    DelegationPermission perm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        new DelegationPermission(buf.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    sm.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                 * Write 1 in little endian but in two bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                 * for DlgOpt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                checksumBytes[pos++] = (byte)0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                checksumBytes[pos++] = (byte)0x00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                 * Write the length of the delegated credential in little
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                 * endian but in two bytes for Dlgth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                if (krbCredMessage.length > 0x0000ffff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    throw new GSSException(GSSException.FAILURE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        "Incorrect messsage length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                writeLittleEndian(krbCredMessage.length, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                checksumBytes[pos++] = temp[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                checksumBytes[pos++] = temp[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                System.arraycopy(krbCredMessage, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                 checksumBytes, pos, krbCredMessage.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         * Called on the acceptor side when reading an InitSecContextToken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // XXX Passing in Checksum is not required. byte[] can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // be passed in if this checksum type denotes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // raw_checksum. In that case, make Checksum class krb5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // internal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        public OverloadedChecksum(Krb5Context context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                  Checksum checksum, EncryptionKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throws GSSException, KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            checksumBytes = checksum.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if ((checksumBytes[0] != CHECKSUM_FIRST_BYTES[0]) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                (checksumBytes[1] != CHECKSUM_FIRST_BYTES[1]) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                (checksumBytes[2] != CHECKSUM_FIRST_BYTES[2]) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                (checksumBytes[3] != CHECKSUM_FIRST_BYTES[3])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                throw new GSSException(GSSException.FAILURE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        "Incorrect checksum");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            ChannelBinding localBindings = context.getChannelBinding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
3050
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   232
            // Ignore remote channel binding info when not requested at
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   233
            // local side (RFC 4121 4.1.1.2: the acceptor MAY ignore...).
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   234
            //
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   235
            // All major krb5 implementors implement this "MAY",
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   236
            // and some applications depend on it as a workaround
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   237
            // for not having a way to negotiate the use of channel
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   238
            // binding -- the initiator application always uses CB
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   239
            // and hopes the acceptor will ignore the CB if the
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   240
            // acceptor doesn't support CB.
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   241
            if (localBindings != null) {
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   242
                byte[] remoteBindingBytes = new byte[CHECKSUM_BINDINGS_SIZE];
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   243
                System.arraycopy(checksumBytes, 4, remoteBindingBytes, 0,
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   244
                                 CHECKSUM_BINDINGS_SIZE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
3050
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   246
                byte[] noBindings = new byte[CHECKSUM_BINDINGS_SIZE];
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   247
                if (!Arrays.equals(noBindings, remoteBindingBytes)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    byte[] localBindingsBytes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        computeChannelBinding(localBindings);
3050
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   250
                    if (!Arrays.equals(localBindingsBytes,
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   251
                                                remoteBindingBytes)) {
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   252
                        throw new GSSException(GSSException.BAD_BINDINGS, -1,
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   253
                                               "Bytes mismatch!");
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   254
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                } else {
3050
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   256
                    throw new GSSException(GSSException.BAD_BINDINGS, -1,
c0ce59daa004 6851973: ignore incoming channel binding if acceptor does not set one
weijun
parents: 2942
diff changeset
   257
                                           "Token missing ChannelBinding!");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            flags = readLittleEndian(checksumBytes, 20, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if ((flags & CHECKSUM_DELEG_FLAG) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                 * XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                 * if ((checksumBytes[24] != (byte)0x01) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                 * (checksumBytes[25] != (byte)0x00))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                int credLen = readLittleEndian(checksumBytes, 26, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                byte[] credBytes = new byte[credLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                System.arraycopy(checksumBytes, 28, credBytes, 0, credLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                CipherHelper cipherHelper = context.getCipherHelper(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if (useNullKey(cipherHelper)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    delegCreds =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        new KrbCred(credBytes, EncryptionKey.NULL_KEY).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        getDelegatedCreds()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    delegCreds =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        new KrbCred(credBytes, key).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        getDelegatedCreds()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // check if KRB-CRED message should use NULL_KEY for encryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        private boolean useNullKey(CipherHelper ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            boolean flag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // for "newer" etypes and RC4-HMAC do not use NULL KEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if ((ch.getProto() == 1) || ch.isArcFour()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                flag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        public Checksum getChecksum() throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return new Checksum(checksumBytes, CHECKSUM_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        public Credentials getDelegatedCreds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            return delegCreds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3050
diff changeset
   306
        // Only called by acceptor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        public void setContextFlags(Krb5Context context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                // default for cred delegation is false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if ((flags & CHECKSUM_DELEG_FLAG) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                context.setCredDelegState(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                // default for the following are true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if ((flags & CHECKSUM_MUTUAL_FLAG) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                context.setMutualAuthState(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if ((flags & CHECKSUM_REPLAY_FLAG) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                context.setReplayDetState(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if ((flags & CHECKSUM_SEQUENCE_FLAG) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                context.setSequenceDetState(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if ((flags & CHECKSUM_CONF_FLAG) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                context.setConfState(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if ((flags & CHECKSUM_INTEG_FLAG) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                context.setIntegState(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private int getAddrType(InetAddress addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        int addressType = CHANNEL_BINDING_AF_NULL_ADDR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (addr instanceof Inet4Address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            addressType = CHANNEL_BINDING_AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        else if (addr instanceof Inet6Address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            addressType = CHANNEL_BINDING_AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return (addressType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    private byte[] getAddrBytes(InetAddress addr) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        int addressType = getAddrType(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        byte[] addressBytes = addr.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (addressBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            switch (addressType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                case CHANNEL_BINDING_AF_INET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    if (addressBytes.length != Inet4_ADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        throw new GSSException(GSSException.FAILURE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        "Incorrect AF-INET address length in ChannelBinding.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    return (addressBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                case CHANNEL_BINDING_AF_INET6:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    if (addressBytes.length != Inet6_ADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        throw new GSSException(GSSException.FAILURE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        "Incorrect AF-INET6 address length in ChannelBinding.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    return (addressBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    throw new GSSException(GSSException.FAILURE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    "Cannot handle non AF-INET addresses in ChannelBinding.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private byte[] computeChannelBinding(ChannelBinding channelBinding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        InetAddress initiatorAddress = channelBinding.getInitiatorAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        InetAddress acceptorAddress = channelBinding.getAcceptorAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        int size = 5*4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int initiatorAddressType = getAddrType(initiatorAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        int acceptorAddressType = getAddrType(acceptorAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        byte[] initiatorAddressBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (initiatorAddress != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            initiatorAddressBytes = getAddrBytes(initiatorAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            size += initiatorAddressBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        byte[] acceptorAddressBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (acceptorAddress != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            acceptorAddressBytes = getAddrBytes(acceptorAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            size += acceptorAddressBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        byte[] appDataBytes = channelBinding.getApplicationData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (appDataBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            size += appDataBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        byte[] data = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        writeLittleEndian(initiatorAddressType, data, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (initiatorAddressBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            writeLittleEndian(initiatorAddressBytes.length, data, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            System.arraycopy(initiatorAddressBytes, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                             data, pos, initiatorAddressBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            pos += initiatorAddressBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            // Write length 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        writeLittleEndian(acceptorAddressType, data, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (acceptorAddressBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            writeLittleEndian(acceptorAddressBytes.length, data, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            System.arraycopy(acceptorAddressBytes, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                             data, pos, acceptorAddressBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            pos += acceptorAddressBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            // Write length 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (appDataBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            writeLittleEndian(appDataBytes.length, data, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            System.arraycopy(appDataBytes, 0, data, pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                             appDataBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            pos += appDataBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            // Write 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            MessageDigest md5 = MessageDigest.getInstance("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return md5.digest(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                throw new GSSException(GSSException.FAILURE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                                       "Could not get MD5 Message Digest - "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                                       + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public abstract byte[] encode() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
}