Example #1
0
  public 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] = SshIO.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 = 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;
  }
Example #2
0
  public int addPayload(byte[] buff, int boffset, int length) {
    // byte newbuf[] = null;
    int boffsetend = boffset + length;

    while (boffset < boffsetend) {
      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++;

            if (packet_length > 262144) {
              throw new IllegalStateException("SshPacket1: Invalid packet_length " + packet_length);
            }

            int block_length = 8 * (packet_length / 8 + 1);
            block = new byte[block_length];
            // System.out.println( "PACKET LENGTH " + packet_length + " BLOCK LENGTH " +
            // block_length );
          }
          break; // switch (phase_packet)

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

        case PHASE_block:
          if (block.length > position) {
            if (boffset < boffsetend) {
              int amount = boffsetend - 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
            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) {
              throw new IllegalStateException("SshPacket1: invalid decrypted packet length");
            }

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

            // packet type
            packet_type = 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()) {
              throw new IllegalStateException("SshPacket1: CRC wrong in received packet!");
            }

            return boffset;
          }
          break;
      }
    }
    return boffset;
  }