File: SshPacket2.java

package info (click to toggle)
jta 2.6%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 1,368 kB
  • ctags: 1,534
  • sloc: java: 12,696; ansic: 1,066; makefile: 243; xml: 72; sh: 7
file content (282 lines) | stat: -rw-r--r-- 8,681 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * This file is part of "JTA - Telnet/SSH for the JAVA(tm) platform".
 *
 * (c) Matthias L. Jugel, Marcus Meißner 1996-2005. All Rights Reserved.
 *
 * Please visit http://javatelnet.org/ for updates and contact.
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * --LICENSE NOTICE--
 *
 */

package de.mud.ssh;


import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Marcus Meissner
 * @version $Id: SshPacket2.java 499 2005-09-29 08:24:54Z leo $
 */
public class SshPacket2 extends SshPacket {

  private final static boolean debug = true;

  //SSH_RECEIVE_PACKET
  private byte[] packet_length_array = new byte[5];				// 4 bytes
  private int packet_length = 0;	// 32-bit sign int
  private int padlen = 0;		// packet length 1 byte unsigned
  private byte[] crc_array = new byte[4];	// 32-bit crc


  private int position 			= 0;
  private int phase_packet 		= 0;
  private final int PHASE_packet_length = 0;
  private final int PHASE_block 	= 1;

  private SshCrypto crypto = null;

  public SshPacket2(SshCrypto _crypto) {
    /* receiving packet */
    position = 0;
    phase_packet = PHASE_packet_length;
    crypto = _crypto;
  }

  public SshPacket2(byte newType) {
    setType(newType);
  }

  /**
   * Return the mp-int at the position offset in the data
   * First 4 bytes are the number of bytes in the integer, msb first
   * (for example, the value 0x00012345 would have 17 bits).  The
   * value zero has zero bits.  It is permissible that the number of
   * bits be larger than the real number of bits.
   * The number of bits is followed by (bits + 7) / 8 bytes of binary
   * data, msb first, giving the value of the integer.
   */
	
  public BigInteger getMpInt() { 
    return new BigInteger(1,getBytes(getInt32()));
  }

  public void putMpInt(BigInteger bi) {
    byte[] mpbytes = bi.toByteArray(), xbytes;
    int i;

    for (i = 0; (i < mpbytes.length) && ( mpbytes[i] == 0 ) ; i++) /* EMPTY */ ;
    xbytes = new byte[mpbytes.length - i];
    System.arraycopy(mpbytes,i,xbytes,0,mpbytes.length - i);

    putInt32(mpbytes.length - i);
    putBytes(xbytes);
  }

  public byte[] getPayLoad(SshCrypto xcrypt, long seqnr)
  throws IOException {
    byte[] data = getData();

    int blocksize = 8;

    // crypted data is: 
    // packet length [ payloadlen + padlen + type + data ]
    packet_length = 4 + 1 + 1;
    if (data!=null)
      packet_length += data.length;

    // pad it up to full blocksize.
    // If not crypto, zeroes, otherwise random. 
    // (zeros because we do not want to tell the attacker the state of our
    //  random generator)
    int padlen = blocksize - (packet_length % blocksize);
    if (padlen < 4) padlen += blocksize;

    byte[] padding = new byte[padlen];
    System.out.println("packet length is "+packet_length+", padlen is "+padlen);
    if (xcrypt == null)
      for(int i=0; i<padlen; i++) padding[i] = 0;
    else
      for(int i=0; i<padlen; i++) padding[i] = SshMisc.getNotZeroRandomByte();

    // [ packetlength, padlength, padding, packet type, data ]
    byte[] block = new byte[ packet_length + padlen ];

    int xlen = padlen + packet_length - 4;
    block[3] = (byte) (xlen & 0xff);
    block[2] = (byte) ((xlen>>8) & 0xff);
    block[1] = (byte) ((xlen>>16) & 0xff);
    block[0] = (byte) ((xlen>>24) & 0xff);

    block[4] = (byte)padlen;
    block[5] = getType();
    System.arraycopy(data,0,block,6,data.length);
    System.arraycopy(padding,0,block,6+data.length,padlen);

    byte[] md5sum;
    if (xcrypt != null) {
      MessageDigest md5 = null;
      try {
        md5 = MessageDigest.getInstance("MD5");
      } catch (NoSuchAlgorithmException e) {
        System.err.println("SshPacket2: unable to load message digest algorithm: "+e);
      }
      byte[] seqint = new byte[4];

      seqint[0] = (byte)((seqnr >> 24) & 0xff);
      seqint[1] = (byte)((seqnr >> 16) & 0xff);
      seqint[2] = (byte)((seqnr >>  8) & 0xff);
      seqint[3] = (byte)((seqnr      ) & 0xff);
      md5.update(seqint,0,4);
      md5.update(block,0,block.length);
      md5sum = md5.digest();
    } else {
      md5sum = new byte[0];
    }

    if (xcrypt != null)
      block = xcrypt.encrypt(block);

    byte[] sendblock = new byte[block.length + md5sum.length];
    System.arraycopy(block,0,sendblock,0,block.length);
    System.arraycopy(md5sum,0,sendblock,block.length,md5sum.length);
    return sendblock;
  };

  private byte block[];

  public byte[] addPayload(byte buff[]) {
    int boffset = 0;
    byte b;
    byte[] newbuf = null;
    int hmaclen = 0;

    if (crypto!=null) hmaclen = 16;

    System.out.println("addPayload2 "+buff.length);

    /*
     * Note: The whole packet is encrypted, except for the MAC.
     *
     * (So I have to rewrite it again).
     */

    while(boffset < buff.length) {
      switch (phase_packet) {
      // 4 bytes
      // Packet length: 32 bit unsigned integer
      // gives the length of the packet, not including the length field
      // and padding.  maximum is 262144 bytes.

      case PHASE_packet_length:
	packet_length_array[position++] = buff[boffset++];
	if (position==5) {
	  packet_length =
	     (packet_length_array[3]&0xff)	+
	    ((packet_length_array[2]&0xff)<<8)	+
	    ((packet_length_array[1]&0xff)<<16)	+
	    ((packet_length_array[0]&0xff)<<24);
	  padlen = packet_length_array[4];
	  position=0;
	  System.out.println("SSH2: packet length "+packet_length);
	  System.out.println("SSH2: padlen "+padlen);
	  packet_length += hmaclen; /* len(md5) */
	  block = new byte[packet_length-1]; /* padlen already done */
	  phase_packet++;
	}
	break; //switch (phase_packet)

      //8*(packet_length/8 +1) bytes

      case PHASE_block  :
	if (position < block.length) {
	  int amount = buff.length - boffset;
	  if (amount > 0) {
	    if (amount > block.length - position)
	      amount = block.length - position;
	    System.arraycopy(buff,boffset,block,position,amount);
	    boffset	+= amount;
	    position	+= amount;
	  }
	}
	if (position==block.length) { //the block is complete
	  if (buff.length>boffset) {
	    newbuf = new byte[buff.length-boffset];
	    System.arraycopy(buff,boffset,newbuf,0,buff.length-boffset);
	  }
	  byte[] decryptedBlock = new byte[block.length - hmaclen];
	  byte[] data;
	  packet_length -= hmaclen;

	  System.arraycopy(block,0,decryptedBlock,0,block.length-hmaclen);

	  if (crypto != null)
	    decryptedBlock = crypto.decrypt(decryptedBlock);

	  for (int i = 0; i < decryptedBlock.length; i++)
		  System.out.print(" "+decryptedBlock[i]);
	  System.out.println("");

	  setType(decryptedBlock[0]);
	  System.err.println("Packet type: "+getType());
	  System.err.println("Packet len: "+packet_length);

	  //data
	  if(packet_length > padlen+1+1)  {
	    data = new byte[packet_length-1-padlen-1];
	    System.arraycopy(decryptedBlock,1,data,0,data.length);
	    putData(data);
	  } else {
	    putData(null);
	  }
	  /* MAC! */
	  return newbuf;
	}
	break;
      } 
    } 
    return null;
  };
  /*

  private boolean checkCrc(){
    byte[] crc_arrayCheck = new byte[4];
    long crcCheck;

    crcCheck = SshMisc.crc32(decryptedBlock, decryptedBlock.length-4);
    crc_arrayCheck[3] = (byte) (crcCheck & 0xff);
    crc_arrayCheck[2] = (byte) ((crcCheck>>8) & 0xff);
    crc_arrayCheck[1] = (byte) ((crcCheck>>16) & 0xff);
    crc_arrayCheck[0] = (byte) ((crcCheck>>24) & 0xff);

    if(debug) {
      System.err.println(crc_arrayCheck[3]+" == "+crc_array[3]);
      System.err.println(crc_arrayCheck[2]+" == "+crc_array[2]);
      System.err.println(crc_arrayCheck[1]+" == "+crc_array[1]);
      System.err.println(crc_arrayCheck[0]+" == "+crc_array[0]);
    }
    if	(crc_arrayCheck[3] != crc_array[3]) return false;
    if	(crc_arrayCheck[2] != crc_array[2]) return false;
    if	(crc_arrayCheck[1] != crc_array[1]) return false;
    if	(crc_arrayCheck[0] != crc_array[0]) return false;
    return true;
  }
  */
}