J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Portions Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * |
| 28 | * (C) Copyright IBM Corp. 1999 All Rights Reserved. |
| 29 | * Copyright 1997 The Open Group Research Institute. All rights reserved. |
| 30 | */ |
| 31 | |
| 32 | package sun.security.krb5.internal.ktab; |
| 33 | |
| 34 | import sun.security.krb5.internal.*; |
| 35 | import sun.security.krb5.PrincipalName; |
| 36 | import sun.security.krb5.Realm; |
| 37 | import sun.security.krb5.RealmException; |
| 38 | import sun.security.krb5.internal.util.KrbDataInputStream; |
| 39 | import java.io.IOException; |
| 40 | import java.io.InputStream; |
| 41 | |
| 42 | /** |
| 43 | * This class implements a buffered input stream. It is used for parsing key table |
| 44 | * data to memory. |
| 45 | * |
| 46 | * @author Yanni Zhang |
| 47 | * |
| 48 | */ |
| 49 | public class KeyTabInputStream extends KrbDataInputStream implements KeyTabConstants { |
| 50 | |
| 51 | boolean DEBUG = Krb5.DEBUG; |
| 52 | static int index; |
| 53 | |
| 54 | public KeyTabInputStream(InputStream is) { |
| 55 | super(is); |
| 56 | } |
| 57 | /** |
| 58 | * Reads the number of bytes this entry data occupy. |
| 59 | */ |
| 60 | int readEntryLength() throws IOException { |
| 61 | return read(4); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | KeyTabEntry readEntry(int entryLen, int ktVersion) throws IOException, RealmException { |
| 66 | index = entryLen; |
| 67 | if (index == 0) { //in native implementation, when the last entry is deleted, a byte 0 is left. |
| 68 | return null; |
| 69 | } |
| 70 | if (index < 0) { //in native implementation, when one of the entries is deleted, the entry length turns to be negative, and |
| 71 | skip(Math.abs(index)); //the fields are left with 0 bytes |
| 72 | return null; |
| 73 | } |
| 74 | int principalNum = read(2); //the number of service names. |
| 75 | index -= 2; |
| 76 | if (ktVersion == KRB5_KT_VNO_1) { //V1 includes realm in the count. |
| 77 | principalNum -= 1; |
| 78 | } |
| 79 | Realm realm = new Realm(readName()); |
| 80 | String[] nameParts = new String[principalNum]; |
| 81 | for (int i = 0; i < principalNum; i++) { |
| 82 | nameParts[i] = readName(); |
| 83 | } |
| 84 | int nameType = read(4); |
| 85 | index -= 4; |
| 86 | PrincipalName service = new PrincipalName(nameParts, nameType); |
| 87 | service.setRealm(realm); |
| 88 | KerberosTime timeStamp = readTimeStamp(); |
| 89 | |
| 90 | int keyVersion = read() & 0xff; |
| 91 | index -= 1; |
| 92 | int keyType = read(2); |
| 93 | index -= 2; |
| 94 | int keyLength = read(2); |
| 95 | index -= 2; |
| 96 | byte[] keyblock = readKey(keyLength); |
| 97 | index -= keyLength; |
| 98 | // There might be a 32 bit kvno here. |
| 99 | // If index is zero, assume that the 8 bit key version number was |
| 100 | // right, otherwise trust the new nonzero value. |
| 101 | if (index >= 4) { |
| 102 | int extKvno = read(4); |
| 103 | if (extKvno != 0) { |
| 104 | keyVersion = extKvno; |
| 105 | } |
| 106 | index -= 4; |
| 107 | } |
| 108 | |
| 109 | // if index is negative, the keytab format must be wrong. |
| 110 | if (index < 0) { |
| 111 | throw new RealmException("Keytab is corrupted"); |
| 112 | } |
| 113 | |
| 114 | // ignore the left bytes. |
| 115 | skip(index); |
| 116 | |
| 117 | return new KeyTabEntry(service, realm, timeStamp, keyVersion, keyType, keyblock); |
| 118 | } |
| 119 | |
| 120 | byte[] readKey(int length) throws IOException { |
| 121 | byte[] bytes = new byte[length]; |
| 122 | read(bytes, 0, length); |
| 123 | return bytes; |
| 124 | } |
| 125 | |
| 126 | KerberosTime readTimeStamp() throws IOException { |
| 127 | index -= 4; |
| 128 | return new KerberosTime((long)read(4) * 1000); |
| 129 | } |
| 130 | |
| 131 | String readName() throws IOException { |
| 132 | String name; |
| 133 | int length = read(2); //length of the realm name or service name |
| 134 | index -= 2; |
| 135 | byte[] bytes = new byte[length]; |
| 136 | read(bytes, 0, length); |
| 137 | index -= length; |
| 138 | name = new String(bytes); |
| 139 | if (DEBUG) { |
| 140 | System.out.println(">>> KeyTabInputStream, readName(): " + name); |
| 141 | } |
| 142 | return name; |
| 143 | } |
| 144 | } |