// Read packet length
decoderLength = decoderBuffer.getInt();
// Check packet length validity
if (decoderLength < 5 || decoderLength > (256 * 1024)) {
log.info("Error decoding packet (invalid length) {}", decoderBuffer.printHex());
throw new SshException(SshConstants.SSH2_DISCONNECT_PROTOCOL_ERROR,
"Invalid packet length: " + decoderLength);
}
// Ok, that's good, we can go to the next step
decoderState = 1;
} else {
// need more data
break;
}
// We have received the beginning of the packet
} else if (decoderState == 1) {
// The read position should always be 4 at this point
assert decoderBuffer.rpos() == 4;
int macSize = inMac != null ? inMac.getBlockSize() : 0;
// Check if the packet has been fully received
if (decoderBuffer.available() >= decoderLength + macSize) {
byte[] data = decoderBuffer.array();
// Decrypt the remaining of the packet
if (inCipher != null){
inCipher.update(data, inCipherSize, decoderLength + 4 - inCipherSize);
}
// Check the mac of the packet
if (inMac != null) {
// Update mac with packet id
inMac.updateUInt(seqi);
// Update mac with packet data
inMac.update(data, 0, decoderLength + 4);
// Compute mac result
inMac.doFinal(inMacResult, 0);
// Check the computed result with the received mac (just after the packet data)
if (!BufferUtils.equals(inMacResult, 0, data, decoderLength + 4, macSize)) {
throw new SshException(SshConstants.SSH2_DISCONNECT_MAC_ERROR, "MAC Error");
}
}
// Increment incoming packet sequence number
seqi = (seqi + 1) & 0xffffffffL;
// Get padding