Beispiel #1
0
  /**
   * Return how much bytes can be read out of the encrypted data. Be aware that this method will not
   * increase the readerIndex of the given {@link ByteBuf}.
   *
   * @param buffer The {@link ByteBuf} to read from. Be aware that it must have at least 5 bytes to
   *     read, otherwise it will throw an {@link IllegalArgumentException}.
   * @return length The length of the encrypted packet that is included in the buffer. This will
   *     return {@code -1} if the given {@link ByteBuf} is not encrypted at all.
   * @throws IllegalArgumentException Is thrown if the given {@link ByteBuf} has not at least 5
   *     bytes to read.
   */
  private static int getEncryptedPacketLength(ByteBuf buffer, int offset) {
    int packetLength = 0;

    // SSLv3 or TLS - Check ContentType
    boolean tls;
    switch (buffer.getUnsignedByte(offset)) {
      case 20: // change_cipher_spec
      case 21: // alert
      case 22: // handshake
      case 23: // application_data
        tls = true;
        break;
      default:
        // SSLv2 or bad data
        tls = false;
    }

    if (tls) {
      // SSLv3 or TLS - Check ProtocolVersion
      int majorVersion = buffer.getUnsignedByte(offset + 1);
      if (majorVersion == 3) {
        // SSLv3 or TLS
        packetLength = buffer.getUnsignedShort(offset + 3) + 5;
        if (packetLength <= 5) {
          // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
          tls = false;
        }
      } else {
        // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
        tls = false;
      }
    }

    if (!tls) {
      // SSLv2 or bad data - Check the version
      boolean sslv2 = true;
      int headerLength = (buffer.getUnsignedByte(offset) & 0x80) != 0 ? 2 : 3;
      int majorVersion = buffer.getUnsignedByte(offset + headerLength + 1);
      if (majorVersion == 2 || majorVersion == 3) {
        // SSLv2
        if (headerLength == 2) {
          packetLength = (buffer.getShort(offset) & 0x7FFF) + 2;
        } else {
          packetLength = (buffer.getShort(offset) & 0x3FFF) + 3;
        }
        if (packetLength <= headerLength) {
          sslv2 = false;
        }
      } else {
        sslv2 = false;
      }

      if (!sslv2) {
        return -1;
      }
    }
    return packetLength;
  }
Beispiel #2
0
 /**
  * Returns the {@code short} at position {@code pos} in the Buffer.
  *
  * @throws IndexOutOfBoundsException if the specified {@code pos} is less than {@code 0} or {@code
  *     pos + 2} is greater than the length of the Buffer.
  */
 public short getShort(int pos) {
   return buffer.getShort(pos);
 }
 @Override
 public short getShort(int index) {
   return buf.getShort(index);
 }
 @Override
 public short getShort(int var1) {
   return a.getShort(var1);
 }