File: SshPacket1.java

package info (click to toggle)
jta 2.6%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,384 kB
  • sloc: java: 12,691; ansic: 1,066; makefile: 243; xml: 72; sh: 7
file content (247 lines) | stat: -rw-r--r-- 8,408 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
/*
 * 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.math.BigInteger;

/**
 * @author Marcus Meissner
 * @version $Id: SshPacket1.java 499 2005-09-29 08:24:54Z leo $
 */
public class SshPacket1 extends SshPacket {
  private final static boolean debug = false;

  //SSH_RECEIVE_PACKET
  private byte[] packet_length_array = new byte[4];
  private int packet_length = 0;
  private byte[] padding = null;
  private byte[] crc_array = new byte[4];
  private byte[] block = null;
  private byte[] encryptedBlock = null;							// encrypted part (Padding + Type + Data + Check)
  private byte[] decryptedBlock = null;							// decrypted part (Padding + Type + Data + Check)

  private SshCrypto crypto = null;

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

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

  /**
   * Return the mp-int at the position offset in the data
   * First 2 bytes are the number of bits 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 byte[] getMpInt() {
    return getBytes((getInt16() + 7) / 8);
  }

  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);
    putInt16(xbytes.length * 8);
    putBytes(xbytes);
  }

  byte[] getPayLoad(SshCrypto crypto) {
    byte[] data = getData();

    //packet length
    if (data != null)
      packet_length = data.length + 5;
    else
      packet_length = 5;
    packet_length_array[3] = (byte) (packet_length & 0xff);
    packet_length_array[2] = (byte) ((packet_length >> 8) & 0xff);
    packet_length_array[1] = (byte) ((packet_length >> 16) & 0xff);
    packet_length_array[0] = (byte) ((packet_length >> 24) & 0xff);

    //padding
    padding = new byte[(8 - (packet_length % 8))];
    if (crypto == null) {
      for (int i = 0; i < padding.length; i++)
        padding[i] = 0;
    } else {
      for (int i = 0; i < padding.length; i++)
        padding[i] = SshMisc.getNotZeroRandomByte();
    }

    //Compute the crc of [ Padding, Packet type, Data ]

    block = new byte[packet_length + padding.length];
    System.arraycopy(padding, 0, block, 0, padding.length);
    int offset = padding.length;
    block[offset++] = getType();
    if (packet_length > 5) {
      System.arraycopy(data, 0, block, offset, data.length);
      offset += data.length;
    }

    long crc = SshMisc.crc32(block, offset);
    crc_array[3] = (byte) (crc & 0xff);
    crc_array[2] = (byte) ((crc >> 8) & 0xff);
    crc_array[1] = (byte) ((crc >> 16) & 0xff);
    crc_array[0] = (byte) ((crc >> 24) & 0xff);
    System.arraycopy(crc_array, 0, block, offset, 4);

    //encrypt
    if (crypto != null)
      block = crypto.encrypt(block);
    byte[] full = new byte[block.length + 4];
    System.arraycopy(packet_length_array, 0, full, 0, 4);
    System.arraycopy(block, 0, full, 4, block.length);
    return full;
  };

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

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

    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 >= 4) {
            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);
            position = 0;
            phase_packet++;
            block = new byte[8 * (packet_length / 8 + 1)];
          }
          break; //switch (phase_packet)

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

        case PHASE_block:

          if (block.length > position) {
            if (boffset < buff.length) {
              int amount = buff.length - boffset;
              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) {  //there is more than 1 packet in buff
              newbuf = new byte[buff.length - boffset];
              System.arraycopy(buff, boffset, newbuf, 0, buff.length - boffset);
            }
            int blockOffset = 0;
            //padding
            int padding_length = (int) (8 - (packet_length % 8));
            padding = new byte[padding_length];

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

            if (decryptedBlock.length != padding_length + packet_length)
              System.out.println("???");

            for (int i = 0; i < padding.length; i++)
              padding[i] = decryptedBlock[blockOffset++];

            //packet type
            setType(decryptedBlock[blockOffset++]);

            byte[] data;
            //data
            if (packet_length > 5) {
              data = new byte[packet_length - 5];
              System.arraycopy(decryptedBlock, blockOffset, data, 0, packet_length - 5);
              blockOffset += packet_length - 5;
            } else
              data = null;
            putData(data);
            //crc
            for (int i = 0; i < crc_array.length; i++)
              crc_array[i] = decryptedBlock[blockOffset++];
            if (!checkCrc())
              System.err.println("SshPacket1: CRC wrong in received packet!");

            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;
  }
} //class