use of javax.net.ssl.SSLProtocolException in project XobotOS by xamarin.
the class ConnectionStateTLS method decrypt.
/**
* Retrieves the fragment of the Plaintext structure of
* the specified type from the provided data representing
* the Generic[Stream|Block]Cipher structure.
* @throws AlertException if alert was occurred.
*/
@Override
protected byte[] decrypt(byte type, byte[] fragment, int offset, int len) {
// plain data of the Generic[Stream|Block]Cipher structure
byte[] data = decCipher.update(fragment, offset, len);
// the 'content' part of the structure
byte[] content;
if (block_size != 0) {
// check padding
int padding_length = data[data.length - 1];
for (int i = 0; i < padding_length; i++) {
if (data[data.length - 2 - i] != padding_length) {
throw new AlertException(AlertProtocol.DECRYPTION_FAILED, new SSLProtocolException("Received message has bad padding"));
}
}
content = new byte[data.length - hash_size - padding_length - 1];
} else {
content = new byte[data.length - hash_size];
}
mac_material_header[0] = type;
mac_material_header[3] = (byte) ((0x00FF00 & content.length) >> 8);
mac_material_header[4] = (byte) (0x0000FF & content.length);
decMac.update(read_seq_num);
decMac.update(mac_material_header);
// mac.update(fragment);
decMac.update(data, 0, content.length);
byte[] mac_value = decMac.doFinal();
if (logger != null) {
logger.println("Decrypted:");
logger.print(data);
//logger.println("MAC Material:");
//logger.print(read_seq_num);
//logger.print(mac_material_header);
//logger.print(data, 0, content.length);
logger.println("Expected mac value:");
logger.print(mac_value);
}
// checking the mac value
for (int i = 0; i < hash_size; i++) {
if (mac_value[i] != data[i + content.length]) {
throw new AlertException(AlertProtocol.BAD_RECORD_MAC, new SSLProtocolException("Bad record MAC"));
}
}
System.arraycopy(data, 0, content, 0, content.length);
incSequenceNumber(read_seq_num);
return content;
}
Aggregations