001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.security.jgss;
027:
028: import java.io.InputStream;
029: import java.io.OutputStream;
030: import java.io.IOException;
031: import java.io.EOFException;
032: import sun.security.util.*;
033:
034: /**
035: * Utilities for processing GSS Tokens.
036: *
037: * @version 1.8, 05/05/07
038: */
039:
040: public abstract class GSSToken {
041:
042: /**
043: * Copies an integer value to a byte array in little endian form.
044: * @param value the integer value to write
045: * @param array the byte array into which the integer must be copied. It
046: * is assumed that the array will be large enough to hold the 4 bytes of
047: * the integer.
048: */
049: public static final void writeLittleEndian(int value, byte[] array) {
050: writeLittleEndian(value, array, 0);
051: }
052:
053: /**
054: * Copies an integer value to a byte array in little endian form.
055: * @param value the integer value to write
056: * @param array the byte array into which the integer must be copied. It
057: * is assumed that the array will be large enough to hold the 4 bytes of
058: * the integer.
059: * @param pos the position at which to start writing
060: */
061: public static final void writeLittleEndian(int value, byte[] array,
062: int pos) {
063: array[pos++] = (byte) (value);
064: array[pos++] = (byte) ((value >>> 8));
065: array[pos++] = (byte) ((value >>> 16));
066: array[pos++] = (byte) ((value >>> 24));
067: }
068:
069: public static final void writeBigEndian(int value, byte[] array) {
070: writeBigEndian(value, array, 0);
071: }
072:
073: public static final void writeBigEndian(int value, byte[] array,
074: int pos) {
075: array[pos++] = (byte) ((value >>> 24));
076: array[pos++] = (byte) ((value >>> 16));
077: array[pos++] = (byte) ((value >>> 8));
078: array[pos++] = (byte) (value);
079: }
080:
081: /**
082: * Reads an integer value from a byte array in little endian form. This
083: * method allows the reading of two byte values as well as four bytes
084: * values both of which are needed in the Kerberos v5 GSS-API mechanism.
085: *
086: * @param data the array containing the bytes of the integer value
087: * @param pos the offset in the array
088: * @size the number of bytes to read from the array.
089: * @return the integer value
090: */
091: public static final int readLittleEndian(byte[] data, int pos,
092: int size) {
093: int retVal = 0;
094: int shifter = 0;
095: while (size > 0) {
096: retVal += (data[pos] & 0xff) << shifter;
097: shifter += 8;
098: pos++;
099: size--;
100: }
101: return retVal;
102: }
103:
104: public static final int readBigEndian(byte[] data, int pos, int size) {
105: int retVal = 0;
106: int shifter = (size - 1) * 8;
107: while (size > 0) {
108: retVal += (data[pos] & 0xff) << shifter;
109: shifter -= 8;
110: pos++;
111: size--;
112: }
113: return retVal;
114: }
115:
116: /**
117: * Writes a two byte integer value to a OutputStream.
118: *
119: * @param val the integer value. It will lose the high-order two bytes.
120: * @param os the OutputStream to write to
121: * @throws IOException if an error occurs while writing to the OutputStream
122: */
123: public static final void writeInt(int val, OutputStream os)
124: throws IOException {
125: os.write(val >>> 8);
126: os.write(val);
127: }
128:
129: /**
130: * Writes a two byte integer value to a byte array.
131: *
132: * @param val the integer value. It will lose the high-order two bytes.
133: * @param dest the byte array to write to
134: * @param pos the offset to start writing to
135: */
136: public static final int writeInt(int val, byte[] dest, int pos) {
137: dest[pos++] = (byte) (val >>> 8);
138: dest[pos++] = (byte) val;
139: return pos;
140: }
141:
142: /**
143: * Reads a two byte integer value from an InputStream.
144: *
145: * @param is the InputStream to read from
146: * @returns the integer value
147: * @throws IOException if some errors occurs while reading the integer
148: * bytes.
149: */
150: public static final int readInt(InputStream is) throws IOException {
151: return (((0xFF & is.read()) << 8) | (0xFF & is.read()));
152: }
153:
154: /**
155: * Reads a two byte integer value from a byte array.
156: *
157: * @param src the byte arra to read from
158: * @param pos the offset to start reading from
159: * @returns the integer value
160: */
161: public static final int readInt(byte[] src, int pos) {
162: return ((0xFF & src[pos]) << 8 | (0xFF & src[pos + 1]));
163: }
164:
165: /**
166: * Blocks till the required number of bytes have been read from the
167: * input stream.
168: *
169: * @param is the InputStream to read from
170: * @param buffer the buffer to store the bytes into
171: * @param throws EOFException if EOF is reached before all bytes are
172: * read.
173: * @throws IOException is an error occurs while reading
174: */
175: public static final void readFully(InputStream is, byte[] buffer)
176: throws IOException {
177: readFully(is, buffer, 0, buffer.length);
178: }
179:
180: /**
181: * Blocks till the required number of bytes have been read from the
182: * input stream.
183: *
184: * @param is the InputStream to read from
185: * @param buffer the buffer to store the bytes into
186: * @param offset the offset to start storing at
187: * @param len the number of bytes to read
188: * @param throws EOFException if EOF is reached before all bytes are
189: * read.
190: * @throws IOException is an error occurs while reading
191: */
192: public static final void readFully(InputStream is, byte[] buffer,
193: int offset, int len) throws IOException {
194: int temp;
195: while (len > 0) {
196: temp = is.read(buffer, offset, len);
197: if (temp == -1)
198: throw new EOFException("Cannot read all " + len
199: + " bytes needed to form this token!");
200: offset += temp;
201: len -= temp;
202: }
203: }
204:
205: public static final void debug(String str) {
206: System.err.print(str);
207: }
208:
209: public static final String getHexBytes(byte[] bytes) {
210: return getHexBytes(bytes, 0, bytes.length);
211: }
212:
213: public static final String getHexBytes(byte[] bytes, int len) {
214: return getHexBytes(bytes, 0, len);
215: }
216:
217: public static final String getHexBytes(byte[] bytes, int pos,
218: int len) {
219: StringBuffer sb = new StringBuffer();
220: for (int i = pos; i < (pos + len); i++) {
221: int b1 = (bytes[i] >> 4) & 0x0f;
222: int b2 = bytes[i] & 0x0f;
223:
224: sb.append(Integer.toHexString(b1));
225: sb.append(Integer.toHexString(b2));
226: sb.append(' ');
227: }
228: return sb.toString();
229: }
230:
231: }
|