Debian Codegraph @

gss/src/main/java/org/globus/gsi/gssapi/net/impl/GSIGssInputStream.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.net.impl;
16
17 import java.io.InputStream;
18 import java.io.IOException;
19 import java.io.EOFException;
20
21 import org.globus.gsi.gssapi.net.GssSocket;
22 import org.globus.gsi.gssapi.net.GssInputStream;
23 import org.globus.gsi.gssapi.SSLUtil;
24
25 import org.ietf.jgss.GSSContext;
26
27 public class GSIGssInputStream extends GssInputStream {
28
29 // 32Mb
30 private static final int MAX_LEN = 32 * 1024 * 1024;
31
32 protected byte [] header;
33 protected int mode;
34
35 public GSIGssInputStream(InputStream in, GSSContext context) {
36 super(in, context);
37 this.header = new byte[5];
38 this.mode = -1;
39 }
40
41 protected void readMsg()
42 throws IOException {
43 do {
44 byte [] token = readToken();
45 if (token == null) {
46 this.buff = null;
47 break;
48 }
49 this.buff = unwrap(token);
50 } while (buff == null);
51 this.index = 0;
52 }
53
54 public int getWrapMode() {
55 return this.mode;
56 }
57
58 public byte[] readHandshakeToken()
59 throws IOException {
60 byte [] token = readToken();
61 if (token == null) {
62 throw new EOFException();
63 }
64 return token;
65 }
66
67 protected byte[] readToken()
68 throws IOException {
69 byte[] buf = null;
70 if (SSLUtil.read(this.in, this.header, 0, this.header.length-1) < 0) {
71 return null;
72 }
73 if (SSLUtil.isSSLv3Packet(this.header)) {
74 this.mode = GssSocket.SSL_MODE;
75 // read the second byte of packet length field
76 if (SSLUtil.read(this.in, this.header, 4, 1) < 0) {
77 return null;
78 }
79 int len = SSLUtil.toUnsignedShort(this.header[3], this.header[4]);
80 buf = new byte[this.header.length + len];
81 System.arraycopy(this.header, 0, buf, 0, this.header.length);
82 if (SSLUtil.read(this.in, buf, this.header.length, len) < 0) {
83 return null;
84 }
85 } else if (SSLUtil.isSSLv2HelloPacket(this.header)) {
86 this.mode = GssSocket.SSL_MODE;
87 // SSLv2 - assume 2-byte header
88 // read extra 2 bytes so subtract it from total len
89 int len = (((header[0] & 0x7f) << 8) | (header[1] & 0xff)) - 2;
90 buf = new byte[this.header.length-1 + len];
91 System.arraycopy(this.header, 0, buf, 0, this.header.length-1);
92 if (SSLUtil.read(this.in, buf, this.header.length-1, len) < 0) {
93 return null;
94 }
95 } else {
96 this.mode = GssSocket.GSI_MODE;
97 int len = SSLUtil.toInt(this.header, 0);
98 if (len > MAX_LEN) {
99 throw new IOException("Token length " + len + " > " + MAX_LEN);
100 } else if (len < 0) {
101 throw new IOException("Token length " + len + " < 0");
102 }
103 buf = new byte[len];
104 if (SSLUtil.read(this.in, buf, 0, buf.length) < 0) {
105 return null;
106 }
107 }
108 return buf;
109 }
110 }