comparison src/ch/ethz/ssh2/transport/TransportConnection.java @ 307:071eccdff8ea ganymed

fix java formatting
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:16:58 -0700
parents 91a31873c42a
children 20d0a1356c43
comparison
equal deleted inserted replaced
305:d2b303406d63 307:071eccdff8ea
36 36
37 CipherOutputStream cos; 37 CipherOutputStream cos;
38 38
39 boolean useRandomPadding; 39 boolean useRandomPadding;
40 40
41 /* Depends on current MAC and CIPHER */ 41 /* Depends on current MAC and CIPHER */
42 42
43 MAC send_mac; 43 MAC send_mac;
44 44
45 byte[] send_mac_buffer; 45 byte[] send_mac_buffer;
46 46
62 62
63 byte[] recv_comp_buffer; 63 byte[] recv_comp_buffer;
64 64
65 byte[] send_comp_buffer; 65 byte[] send_comp_buffer;
66 66
67 /* won't change */ 67 /* won't change */
68 68
69 final byte[] send_padding_buffer = new byte[256]; 69 final byte[] send_padding_buffer = new byte[256];
70 70
71 final byte[] send_packet_header_buffer = new byte[5]; 71 final byte[] send_packet_header_buffer = new byte[5];
72 72
90 cis.changeCipher(bc); 90 cis.changeCipher(bc);
91 recv_mac = mac; 91 recv_mac = mac;
92 recv_mac_buffer = (mac != null) ? new byte[mac.size()] : null; 92 recv_mac_buffer = (mac != null) ? new byte[mac.size()] : null;
93 recv_mac_buffer_cmp = (mac != null) ? new byte[mac.size()] : null; 93 recv_mac_buffer_cmp = (mac != null) ? new byte[mac.size()] : null;
94 recv_padd_blocksize = bc.getBlockSize(); 94 recv_padd_blocksize = bc.getBlockSize();
95 if(recv_padd_blocksize < 8) { 95
96 if (recv_padd_blocksize < 8) {
96 recv_padd_blocksize = 8; 97 recv_padd_blocksize = 8;
97 } 98 }
98 } 99 }
99 100
100 public void changeSendCipher(BlockCipher bc, MAC mac) { 101 public void changeSendCipher(BlockCipher bc, MAC mac) {
101 if((bc instanceof NullCipher) == false) { 102 if ((bc instanceof NullCipher) == false) {
102 /* Only use zero byte padding for the first few packets */ 103 /* Only use zero byte padding for the first few packets */
103 useRandomPadding = true; 104 useRandomPadding = true;
104 /* Once we start encrypting, there is no way back */ 105 /* Once we start encrypting, there is no way back */
105 } 106 }
106 107
107 cos.changeCipher(bc); 108 cos.changeCipher(bc);
108 send_mac = mac; 109 send_mac = mac;
109 send_mac_buffer = (mac != null) ? new byte[mac.size()] : null; 110 send_mac_buffer = (mac != null) ? new byte[mac.size()] : null;
110 send_padd_blocksize = bc.getBlockSize(); 111 send_padd_blocksize = bc.getBlockSize();
111 if(send_padd_blocksize < 8) { 112
113 if (send_padd_blocksize < 8) {
112 send_padd_blocksize = 8; 114 send_padd_blocksize = 8;
113 } 115 }
114 } 116 }
115 117
116 public void changeRecvCompression(Compressor comp) { 118 public void changeRecvCompression(Compressor comp) {
117 recv_comp = comp; 119 recv_comp = comp;
118 120
119 if(comp != null) { 121 if (comp != null) {
120 recv_comp_buffer = new byte[comp.getBufferSize()]; 122 recv_comp_buffer = new byte[comp.getBufferSize()];
121 } 123 }
122 } 124 }
123 125
124 public void changeSendCompression(Compressor comp) { 126 public void changeSendCompression(Compressor comp) {
125 send_comp = comp; 127 send_comp = comp;
126 128
127 if(comp != null) { 129 if (comp != null) {
128 send_comp_buffer = new byte[comp.getBufferSize()]; 130 send_comp_buffer = new byte[comp.getBufferSize()];
129 } 131 }
130 } 132 }
131 133
132 public void sendMessage(byte[] message) throws IOException { 134 public void sendMessage(byte[] message) throws IOException {
141 // return an estimate for the paket overhead (for send operations) 143 // return an estimate for the paket overhead (for send operations)
142 return 5 + 4 + (send_padd_blocksize - 1) + send_mac_buffer.length; 144 return 5 + 4 + (send_padd_blocksize - 1) + send_mac_buffer.length;
143 } 145 }
144 146
145 public void sendMessage(byte[] message, int off, int len, int padd) throws IOException { 147 public void sendMessage(byte[] message, int off, int len, int padd) throws IOException {
146 if(padd < 4) { 148 if (padd < 4) {
147 padd = 4; 149 padd = 4;
148 } 150 }
149 else if(padd > 64) { 151 else if (padd > 64) {
150 padd = 64; 152 padd = 64;
151 } 153 }
152 154
153 if(send_comp != null && can_compress) { 155 if (send_comp != null && can_compress) {
154 len = send_comp.compress(message, off, len, send_comp_buffer); 156 len = send_comp.compress(message, off, len, send_comp_buffer);
155 message = send_comp_buffer; 157 message = send_comp_buffer;
156 } 158 }
157 159
158 int packet_len = 5 + len + padd; /* Minimum allowed padding is 4 */ 160 int packet_len = 5 + len + padd; /* Minimum allowed padding is 4 */
159
160 int slack = packet_len % send_padd_blocksize; 161 int slack = packet_len % send_padd_blocksize;
161 162
162 if(slack != 0) { 163 if (slack != 0) {
163 packet_len += (send_padd_blocksize - slack); 164 packet_len += (send_padd_blocksize - slack);
164 } 165 }
165 166
166 if(packet_len < 16) { 167 if (packet_len < 16) {
167 packet_len = 16; 168 packet_len = 16;
168 } 169 }
169 170
170 int padd_len = packet_len - (5 + len); 171 int padd_len = packet_len - (5 + len);
171 172
172 if(useRandomPadding) { 173 if (useRandomPadding) {
173 for(int i = 0; i < padd_len; i = i + 4) { 174 for (int i = 0; i < padd_len; i = i + 4) {
174 /* 175 /*
175 * don't waste calls to rnd.nextInt() (by using only 8bit of the 176 * don't waste calls to rnd.nextInt() (by using only 8bit of the
176 * output). just believe me: even though we may write here up to 3 177 * output). just believe me: even though we may write here up to 3
177 * bytes which won't be used, there is no "buffer overflow" (i.e., 178 * bytes which won't be used, there is no "buffer overflow" (i.e.,
178 * arrayindexoutofbounds). the padding buffer is big enough =) (256 179 * arrayindexoutofbounds). the padding buffer is big enough =) (256
179 * bytes, and that is bigger than any current cipher block size + 64). 180 * bytes, and that is bigger than any current cipher block size + 64).
180 */ 181 */
181
182 int r = rnd.nextInt(); 182 int r = rnd.nextInt();
183 send_padding_buffer[i] = (byte) r; 183 send_padding_buffer[i] = (byte) r;
184 send_padding_buffer[i + 1] = (byte) (r >> 8); 184 send_padding_buffer[i + 1] = (byte)(r >> 8);
185 send_padding_buffer[i + 2] = (byte) (r >> 16); 185 send_padding_buffer[i + 2] = (byte)(r >> 16);
186 send_padding_buffer[i + 3] = (byte) (r >> 24); 186 send_padding_buffer[i + 3] = (byte)(r >> 24);
187 } 187 }
188 } 188 }
189 else { 189 else {
190 /* use zero padding for unencrypted traffic */ 190 /* use zero padding for unencrypted traffic */
191 for(int i = 0; i < padd_len; i++) { 191 for (int i = 0; i < padd_len; i++) {
192 send_padding_buffer[i] = 0; 192 send_padding_buffer[i] = 0;
193 } 193 }
194 /* Actually this code is paranoid: we never filled any 194
195 * bytes into the padding buffer so far, therefore it should 195 /* Actually this code is paranoid: we never filled any
196 * consist of zeros only. 196 * bytes into the padding buffer so far, therefore it should
197 */ 197 * consist of zeros only.
198 } 198 */
199 199 }
200 send_packet_header_buffer[0] = (byte) ((packet_len - 4) >> 24); 200
201 send_packet_header_buffer[1] = (byte) ((packet_len - 4) >> 16); 201 send_packet_header_buffer[0] = (byte)((packet_len - 4) >> 24);
202 send_packet_header_buffer[2] = (byte) ((packet_len - 4) >> 8); 202 send_packet_header_buffer[1] = (byte)((packet_len - 4) >> 16);
203 send_packet_header_buffer[3] = (byte) ((packet_len - 4)); 203 send_packet_header_buffer[2] = (byte)((packet_len - 4) >> 8);
204 send_packet_header_buffer[3] = (byte)((packet_len - 4));
204 send_packet_header_buffer[4] = (byte) padd_len; 205 send_packet_header_buffer[4] = (byte) padd_len;
205
206 cos.write(send_packet_header_buffer, 0, 5); 206 cos.write(send_packet_header_buffer, 0, 5);
207 cos.write(message, off, len); 207 cos.write(message, off, len);
208 cos.write(send_padding_buffer, 0, padd_len); 208 cos.write(send_padding_buffer, 0, padd_len);
209 209
210 if(send_mac != null) { 210 if (send_mac != null) {
211 send_mac.initMac(send_seq_number); 211 send_mac.initMac(send_seq_number);
212 send_mac.update(send_packet_header_buffer, 0, 5); 212 send_mac.update(send_packet_header_buffer, 0, 5);
213 send_mac.update(message, off, len); 213 send_mac.update(message, off, len);
214 send_mac.update(send_padding_buffer, 0, padd_len); 214 send_mac.update(send_padding_buffer, 0, padd_len);
215
216 send_mac.getMac(send_mac_buffer, 0); 215 send_mac.getMac(send_mac_buffer, 0);
217 cos.writePlain(send_mac_buffer, 0, send_mac_buffer.length); 216 cos.writePlain(send_mac_buffer, 0, send_mac_buffer.length);
218 } 217 }
219 218
220 cos.flush(); 219 cos.flush();
221 220
222 if(log.isDebugEnabled()) { 221 if (log.isDebugEnabled()) {
223 log.debug("Sent " + Packets.getMessageName(message[off] & 0xff) + " " + len + " bytes payload"); 222 log.debug("Sent " + Packets.getMessageName(message[off] & 0xff) + " " + len + " bytes payload");
224 } 223 }
225 224
226 send_seq_number++; 225 send_seq_number++;
227 } 226 }
228 227
229 public int peekNextMessageLength() throws IOException { 228 public int peekNextMessageLength() throws IOException {
230 if(recv_packet_header_present == false) { 229 if (recv_packet_header_present == false) {
231 cis.read(recv_packet_header_buffer, 0, 5); 230 cis.read(recv_packet_header_buffer, 0, 5);
232 recv_packet_header_present = true; 231 recv_packet_header_present = true;
233 } 232 }
234 233
235 int packet_length = ((recv_packet_header_buffer[0] & 0xff) << 24) 234 int packet_length = ((recv_packet_header_buffer[0] & 0xff) << 24)
236 | ((recv_packet_header_buffer[1] & 0xff) << 16) | ((recv_packet_header_buffer[2] & 0xff) << 8) 235 | ((recv_packet_header_buffer[1] & 0xff) << 16) | ((recv_packet_header_buffer[2] & 0xff) << 8)
237 | ((recv_packet_header_buffer[3] & 0xff)); 236 | ((recv_packet_header_buffer[3] & 0xff));
238
239 int padding_length = recv_packet_header_buffer[4] & 0xff; 237 int padding_length = recv_packet_header_buffer[4] & 0xff;
240 238
241 if(packet_length > TransportManager.MAX_PACKET_SIZE || packet_length < 12) { 239 if (packet_length > TransportManager.MAX_PACKET_SIZE || packet_length < 12) {
242 throw new PacketFormatException(String.format("Illegal packet size (%d)", packet_length)); 240 throw new PacketFormatException(String.format("Illegal packet size (%d)", packet_length));
243 } 241 }
244 242
245 int payload_length = packet_length - padding_length - 1; 243 int payload_length = packet_length - padding_length - 1;
246 244
247 if(payload_length < 0) { 245 if (payload_length < 0) {
248 throw new PacketFormatException(String.format("Illegal padding_length in packet from remote (%d)", padding_length)); 246 throw new PacketFormatException(String.format("Illegal padding_length in packet from remote (%d)", padding_length));
249 } 247 }
250 248
251 return payload_length; 249 return payload_length;
252 } 250 }
253 251
254 public int receiveMessage(byte buffer[], int off, int len) throws IOException { 252 public int receiveMessage(byte buffer[], int off, int len) throws IOException {
255 if(recv_packet_header_present == false) { 253 if (recv_packet_header_present == false) {
256 cis.read(recv_packet_header_buffer, 0, 5); 254 cis.read(recv_packet_header_buffer, 0, 5);
257 } 255 }
258 else { 256 else {
259 recv_packet_header_present = false; 257 recv_packet_header_present = false;
260 } 258 }
261 259
262 int packet_length = ((recv_packet_header_buffer[0] & 0xff) << 24) 260 int packet_length = ((recv_packet_header_buffer[0] & 0xff) << 24)
263 | ((recv_packet_header_buffer[1] & 0xff) << 16) | ((recv_packet_header_buffer[2] & 0xff) << 8) 261 | ((recv_packet_header_buffer[1] & 0xff) << 16) | ((recv_packet_header_buffer[2] & 0xff) << 8)
264 | ((recv_packet_header_buffer[3] & 0xff)); 262 | ((recv_packet_header_buffer[3] & 0xff));
265
266 int padding_length = recv_packet_header_buffer[4] & 0xff; 263 int padding_length = recv_packet_header_buffer[4] & 0xff;
267 264
268 if(packet_length > TransportManager.MAX_PACKET_SIZE || packet_length < 12) { 265 if (packet_length > TransportManager.MAX_PACKET_SIZE || packet_length < 12) {
269 throw new PacketFormatException(String.format("Illegal packet size (%d)", packet_length)); 266 throw new PacketFormatException(String.format("Illegal packet size (%d)", packet_length));
270 } 267 }
271 268
272 int payload_length = packet_length - padding_length - 1; 269 int payload_length = packet_length - padding_length - 1;
273 270
274 if(payload_length < 0) { 271 if (payload_length < 0) {
275 throw new PacketFormatException(String.format("Illegal padding_length in packet from remote (%d)", padding_length)); 272 throw new PacketFormatException(String.format("Illegal padding_length in packet from remote (%d)", padding_length));
276 } 273 }
277 274
278 if(payload_length >= len) { 275 if (payload_length >= len) {
279 throw new IOException("Receive buffer too small (" + len + ", need " + payload_length + ")"); 276 throw new IOException("Receive buffer too small (" + len + ", need " + payload_length + ")");
280 } 277 }
281 278
282 cis.read(buffer, off, payload_length); 279 cis.read(buffer, off, payload_length);
283 cis.read(recv_padding_buffer, 0, padding_length); 280 cis.read(recv_padding_buffer, 0, padding_length);
284 281
285 if(recv_mac != null) { 282 if (recv_mac != null) {
286 cis.readPlain(recv_mac_buffer, 0, recv_mac_buffer.length); 283 cis.readPlain(recv_mac_buffer, 0, recv_mac_buffer.length);
287
288 recv_mac.initMac(recv_seq_number); 284 recv_mac.initMac(recv_seq_number);
289 recv_mac.update(recv_packet_header_buffer, 0, 5); 285 recv_mac.update(recv_packet_header_buffer, 0, 5);
290 recv_mac.update(buffer, off, payload_length); 286 recv_mac.update(buffer, off, payload_length);
291 recv_mac.update(recv_padding_buffer, 0, padding_length); 287 recv_mac.update(recv_padding_buffer, 0, padding_length);
292 recv_mac.getMac(recv_mac_buffer_cmp, 0); 288 recv_mac.getMac(recv_mac_buffer_cmp, 0);
293 289
294 for(int i = 0; i < recv_mac_buffer.length; i++) { 290 for (int i = 0; i < recv_mac_buffer.length; i++) {
295 if(recv_mac_buffer[i] != recv_mac_buffer_cmp[i]) { 291 if (recv_mac_buffer[i] != recv_mac_buffer_cmp[i]) {
296 throw new IOException("Remote sent corrupt MAC."); 292 throw new IOException("Remote sent corrupt MAC.");
297 } 293 }
298 } 294 }
299 } 295 }
300 296
301 recv_seq_number++; 297 recv_seq_number++;
302 298
303 if(log.isDebugEnabled()) { 299 if (log.isDebugEnabled()) {
304 log.debug("Received " + Packets.getMessageName(buffer[off] & 0xff) + " " + payload_length 300 log.debug("Received " + Packets.getMessageName(buffer[off] & 0xff) + " " + payload_length
305 + " bytes payload"); 301 + " bytes payload");
306 } 302 }
307 303
308 if(recv_comp != null && can_compress) { 304 if (recv_comp != null && can_compress) {
309 int[] uncomp_len = new int[]{payload_length}; 305 int[] uncomp_len = new int[] {payload_length};
310 buffer = recv_comp.uncompress(buffer, off, uncomp_len); 306 buffer = recv_comp.uncompress(buffer, off, uncomp_len);
311 return uncomp_len[0]; 307 return uncomp_len[0];
312 } 308 }
313 else { 309 else {
314 return payload_length; 310 return payload_length;