Search in sources :

Example 16 with SSLProtocolException

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;
}
Also used : SSLProtocolException(javax.net.ssl.SSLProtocolException)

Aggregations

SSLProtocolException (javax.net.ssl.SSLProtocolException)16 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)5 CertificateException (java.security.cert.CertificateException)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 SecureRandom (java.security.SecureRandom)2 CertificateEncodingException (java.security.cert.CertificateEncodingException)2 X509Certificate (java.security.cert.X509Certificate)2 HashSet (java.util.HashSet)2 X509TrustManager (javax.net.ssl.X509TrustManager)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 SSLProtocolExceptionParser (org.jetbrains.idea.svn.networking.SSLProtocolExceptionParser)2 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 SingleInetAddressDns (okhttp3.internal.SingleInetAddressDns)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1