jdk/src/share/classes/sun/security/krb5/internal/ccache/CCacheInputStream.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 76 87bd6df367b8
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
package sun.security.krb5.internal.ccache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.krb5.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.krb5.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.krb5.internal.util.KrbDataInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This class extends KrbDataInputStream. It is used for parsing FCC-format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * data from file to memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Yanni Zhang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class CCacheInputStream extends KrbDataInputStream implements FileCCacheConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * FCC version 2 contains type information for principals.  FCC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * version 1 does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * FCC version 3 contains keyblock encryption type information, and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * architecture independent.  Previous versions are not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * The code will accept version 1, 2, and 3 ccaches, and depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * what KRB5_FCC_DEFAULT_FVNO is set to, it will create version 1, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * or 3 FCC caches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * The default credentials cache should be type 3 for now (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * init_ctx.c).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /* V4 of the credentials cache format allows for header tags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public CCacheInputStream(InputStream is){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        super(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /* Read tag field introduced in KRB5_FCC_FVNO_4 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // this needs to be public for Kinit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public Tag readTag() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        char[] buf = new char[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        byte[] bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        int tag = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        int taglen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Integer time_offset = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Integer usec_offset = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        len = read(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new IOException("stop.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        bytes = new byte[len + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (len > buf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new IOException("Invalid tag length.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            tag    = read(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            taglen = read(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            case FCC_TAG_DELTATIME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                time_offset = new Integer(read(4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                usec_offset = new Integer(read(4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            len = len - (4 + taglen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Tag result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (tag == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        result = new Tag(len, tag, time_offset, usec_offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * In file-based credential cache, the realm name is stored as part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * principal name at the first place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // made public for KinitOptions to call directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public PrincipalName readPrincipal(int version) throws IOException, RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        int type, length, namelength, kret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        PrincipalName p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        String[] pname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        String realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        /* Read principal type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (version == KRB5_FCC_FVNO_1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            type = KRB5_NT_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            type = read(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        length = read(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        String[] result = new String[length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         * DCE includes the principal's realm in the count; the new format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         * does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (version == KRB5_FCC_FVNO_1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            length--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        for (int i = 0; i <= length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            namelength = read(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (namelength > MAXNAMELENGTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                throw new IOException("Invalid name length in principal name.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            byte[] bytes = new byte[namelength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            read(bytes, 0, namelength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            result[i] = new String(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (isRealm(result[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            realm = result[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            pname = new String[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            System.arraycopy(result, 1, pname, 0, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            p = new PrincipalName(pname, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            p.setRealm(realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        else p = new PrincipalName(result, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * In practice, a realm is named by uppercasing the DNS domain name. we currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * rely on this to determine if the string within the principal identifier is realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    boolean isRealm(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            Realm r = new Realm(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        StringTokenizer st = new StringTokenizer(str, ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            s = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                if (s.charAt(i) >= 141) {
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
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    EncryptionKey readKey(int version) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        int keyType, keyLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        keyType = read(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (version == KRB5_FCC_FVNO_3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            read(2); /* keytype recorded twice in fvno 3 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        keyLen = read(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        byte[] bytes = new byte[keyLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        for (int i = 0; i < keyLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            bytes[i] = (byte)read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return new EncryptionKey(bytes, keyType, new Integer(version));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    long[] readTimes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        long[] times = new long[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        times[0] = (long)read(4) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        times[1] = (long)read(4) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        times[2] = (long)read(4) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        times[3] = (long)read(4) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return times;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    boolean readskey() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (read() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        else return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    HostAddress[] readAddr() throws IOException, KrbApErrException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        int numAddrs, addrType, addrLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        numAddrs = read(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (numAddrs > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            HostAddress[] addrs = new HostAddress[numAddrs];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            for (int i = 0; i < numAddrs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                addrType = read(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                addrLength = read(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                if (!(addrLength == 4 || addrLength == 16)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    System.out.println("Incorrect address format.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                byte[] result = new byte[addrLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                for (int j = 0; j < addrLength; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    result[j] = (byte)read(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                addrs[i] = new HostAddress(addrType, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    AuthorizationDataEntry[] readAuth() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        int num, adtype, adlength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        num = read(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (num > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            AuthorizationDataEntry[] auData = new AuthorizationDataEntry[num];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            byte[] data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            for (int i = 0; i < num; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                adtype = read(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                adlength = read(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                data = new byte[adlength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                for (int j = 0; j < adlength; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    data[j] = (byte)read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                auData[i] = new AuthorizationDataEntry(adtype, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return auData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        else return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    Ticket readData() throws IOException, RealmException, KrbApErrException, Asn1Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        length = read(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            byte[] bytes = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            read(bytes, 0, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            Ticket ticket = new Ticket(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return ticket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        else return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    boolean[] readFlags() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        boolean[] flags = new boolean[Krb5.TKT_OPTS_MAX+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        int ticketFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        ticketFlags = read(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if ((ticketFlags & 0x40000000) == TKT_FLG_FORWARDABLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        flags[1] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if ((ticketFlags & 0x20000000) == TKT_FLG_FORWARDED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        flags[2] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if ((ticketFlags & 0x10000000) == TKT_FLG_PROXIABLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        flags[3] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if ((ticketFlags & 0x08000000) == TKT_FLG_PROXY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        flags[4] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if ((ticketFlags & 0x04000000) == TKT_FLG_MAY_POSTDATE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        flags[5] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if ((ticketFlags & 0x02000000) == TKT_FLG_POSTDATED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        flags[6] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if ((ticketFlags & 0x01000000) == TKT_FLG_INVALID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        flags[7] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if ((ticketFlags & 0x00800000) == TKT_FLG_RENEWABLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        flags[8] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if ((ticketFlags & 0x00400000) == TKT_FLG_INITIAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        flags[9] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if ((ticketFlags & 0x00200000) == TKT_FLG_PRE_AUTH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        flags[10] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if ((ticketFlags & 0x00100000) == TKT_FLG_HW_AUTH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        flags[11] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            String msg = ">>> CCacheInputStream: readFlags() ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (flags[1] == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                msg += " FORWARDABLE;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (flags[2] == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                msg += " FORWARDED;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (flags[3] == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                msg += " PROXIABLE;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (flags[4] == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                msg += " PROXY;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (flags[5] == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                msg += " MAY_POSTDATE;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (flags[6] == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                msg += " POSTDATED;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (flags[7] == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                msg += " INVALID;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (flags[8] == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                msg += " RENEWABLE;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (flags[9] == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                msg += " INITIAL;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (flags[10] == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                msg += " PRE_AUTH;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (flags[11] == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                msg += " HW_AUTH;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    Credentials readCred(int version) throws IOException,RealmException, KrbApErrException, Asn1Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        PrincipalName cpname = readPrincipal(version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            System.out.println(">>>DEBUG <CCacheInputStream>  client principal is " + cpname.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        PrincipalName spname = readPrincipal(version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            System.out.println(">>>DEBUG <CCacheInputStream> server principal is " + spname.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        EncryptionKey key = readKey(version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            System.out.println(">>>DEBUG <CCacheInputStream> key type: " + key.getEType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        long times[] = readTimes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        KerberosTime authtime = new KerberosTime(times[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        KerberosTime starttime = new KerberosTime(times[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        KerberosTime endtime = new KerberosTime(times[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        KerberosTime renewTill = new KerberosTime(times[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            System.out.println(">>>DEBUG <CCacheInputStream> auth time: " + authtime.toDate().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            System.out.println(">>>DEBUG <CCacheInputStream> start time: " + starttime.toDate().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            System.out.println(">>>DEBUG <CCacheInputStream> end time: " + endtime.toDate().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            System.out.println(">>>DEBUG <CCacheInputStream> renew_till time: " + renewTill.toDate().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        boolean skey = readskey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        boolean flags[] = readFlags();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        TicketFlags tFlags = new TicketFlags(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        HostAddress addr[] = readAddr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        HostAddresses addrs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            addrs = new HostAddresses(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        AuthorizationDataEntry[] auDataEntry = readAuth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        AuthorizationData auData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (auData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            auData = new AuthorizationData(auDataEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        Ticket ticket = readData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            System.out.println(">>>DEBUG <CCacheInputStream>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (ticket == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                System.out.println("///ticket is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        Ticket secTicket = readData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        Credentials cred = new Credentials(cpname, spname, key, authtime, starttime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                           endtime, renewTill, skey, tFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                           addrs, auData, ticket, secTicket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return cred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
}