Debian Codegraph @

gss/src/main/java/org/globus/gsi/gssapi/SSLUtil.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.gsi.gssapi;
16
17 import java.io.InputStream;
18 import java.io.IOException;
19 import java.io.EOFException;
20
21 /**
22 * A collection of SSL-protocol related functions.
23 */
24 public class SSLUtil {
25
26 /**
27 * Reads some number of bytes from the input stream.
28 * This function reads maximum data available on the
29 * stream.
30 *
31 * @param in the input stream to read the bytes from.
32 * @param buf the buffer into which read the data is read.
33 * @param off the start offset in array b at which the data is written.
34 * @param len the maximum number of bytes to read.
35 * @exception IOException if I/O error occurs.
36 */
37 public static int read(InputStream in, byte [] buf, int off, int len)
38 throws IOException {
39 int n = 0;
40 while (n < len) {
41 int count = in.read(buf, off + n, len - n);
42 if (count < 0) {
43 return count;
44 }
45 n += count;
46 }
47 return len;
48 }
49
50 /**
51 * Reads some number of bytes from the input stream.
52 * This function blocks until all data is read or an I/O
53 * error occurs.
54 *
55 * @param in the input stream to read the bytes from.
56 * @param buf the buffer into which read the data is read.
57 * @param off the start offset in array b at which the data is written.
58 * @param len the maximum number of bytes to read.
59 * @exception IOException if I/O error occurs.
60 */
61 public static void readFully(InputStream in, byte [] buf, int off, int len)
62 throws IOException {
63 int n = 0;
64 while (n < len) {
65 int count = in.read(buf, off + n, len - n);
66 if (count < 0)
67 throw new EOFException();
68 n += count;
69 }
70 }
71
72 /**
73 * Reads an entire SSL message from the specified
74 * input stream.
75 *
76 * @param in the input stream to read the SSL message
77 * from.
78 * @return the byte array containing the SSL message
79 * @exception IOException if I/O error occurs.
80 */
81 public static byte[] readSslMessage(InputStream in)
82 throws IOException {
83 byte [] header = new byte[5];
84 readFully(in, header, 0, header.length);
85 int length;
86 if (isSSLv3Packet(header))
87 length = toShort(header[3], header[4]);
88 else if (isSSLv2HelloPacket(header))
89 length = (((header[0] & 0x7f) << 8) | (header[1] & 0xff)) - 3;
90 else {
91 throw new IOException("Invalid SSL header");
92 }
93 byte [] inToken = new byte[header.length + length];
94 System.arraycopy(header, 0, inToken, 0, header.length);
95 readFully(in, inToken, header.length, length);
96 return inToken;
97 }
98
99 /**
100 * Determines if a given header is a SSL packet
101 * (has a SSL header)
102 *
103 * @return true if the header is a SSL header. False, otherwise.
104 */
105 public static final boolean isSSLPacket(byte[] header) {
106 return ( isSSLv3Packet(header) || isSSLv2HelloPacket(header) );
107 }
108
109 /**
110 * Determines if a given header is a SSLv3 packet
111 * (has a SSL header) or a backward compatible version of TLS
112 * using the same header format.
113 *
114 * @return true if the header is a SSLv3 header. False, otherwise.
115 */
116 public static final boolean isSSLv3Packet(byte[] header) {
117 return header[0] >= 20 && header[0] <= 26 &&
118 (header[1] == 3 || (header[1] == 2 && header[2] == 0));
119 }
120
121 /**
122 * Determines if a given header is a SSLv2 client or server hello packet
123 *
124 * @return true if the header is such a SSLv2 client or server hello
125 * packet. False, otherwise.
126 */
127 public static final boolean isSSLv2HelloPacket(byte[] header) {
128 return ((header[0] & 0x80) != 0 && (header[2] == 1 || header[2] == 4));
129 }
130
131 /**
132 * Converts 2 bytes to a <code>short</code>.
133 *
134 * @param a byte 1
135 * @param b byte 2
136 * @return the <code>short</code> value of the 2 bytes
137 */
138 public static short toShort(byte a, byte b) {
139 return (short)((a << 8) | (b & 0xff));
140 }
141
142 /**
143 * Converts 2 bytes to a
144 * <code>unsigned short</code>.
145 *
146 * @param a byte 1
147 * @param b byte 2
148 * @return the <code>unsigned short</code> value of the 2 bytes
149 */
150 public static int toUnsignedShort(byte a, byte b) {
151 int n;
152 n = (a & 0xff) << 8;
153 n |= (b & 0xff);
154 return n;
155 }
156
157 /**
158 * Converts 4 bytes to an <code>int</code> at
159 * the specified offset in the given byte array.
160 *
161 * @param buf the byte array containing the 4 bytes
162 * to be converted to an <code>int</code>.
163 * @param off offset in the byte array
164 * @return the <code>int</code> value of the 4 bytes.
165 */
166 public static int toInt(byte[] buf, int off) {
167 int lg = (buf[off] & 0xff) << 24;
168 lg |= (buf[off+1] & 0xff) << 16;
169 lg |= (buf[off+2] & 0xff) << 8;
170 lg |= (buf[off+3] & 0xff);
171 return lg;
172 }
173
174 /**
175 * Converts the specified int value into
176 * 4 bytes. The bytes are put into the
177 * specified byte array at a given offset
178 * location.
179 *
180 * @param v the int value to convert into 4 bytes.
181 * @param buf the byte array to put the resulting
182 * 4 bytes.
183 * @param off offset in the byte array
184 */
185 public static void writeInt(int v, byte[] buf, int off) {
186 buf[off] = (byte)((v >>> 24) & 0xFF);
187 buf[off+1] = (byte)((v >>> 16) & 0xFF);
188 buf[off+2] = (byte)((v >>> 8) & 0xFF);
189 buf[off+3] = (byte)((v >>> 0) & 0xFF);
190 }
191
192 /**
193 * Converts 8 bytes to a <code>long</code> at the
194 * specified offset in the given byte array.
195 *
196 * @param buf the byte array containing the 8 bytes
197 * to be converted to a <code>long</code>.
198 * @param off offset in the byte array
199 * @return the <code>long</code> value of the 8 bytes.
200 */
201 public static long toLong(byte[]buf, int off) {
202 return ((long)(toInt(buf, off)) << 32) + (toInt(buf, off+4) & 0xFFFFFFFFL);
203 }
204
205 }