def pre_dissect(self, s):
        """
        Decrypt, verify and decompress the message,
        i.e. apply the previous methods according to the reading cipher type.
        If the decryption was successful, 'len' will be the length of the
        TLSPlaintext.fragment. Else, it should be the length of the
        _TLSEncryptedContent.
        """
        if len(s) < 5:
            raise Exception("Invalid record: header is too short.")

        msglen = struct.unpack('!H', s[3:5])[0]
        hdr, efrag, r = s[:5], s[5:5 + msglen], s[msglen + 5:]

        iv = mac = pad = b""
        self.padlen = None
        decryption_success = False

        cipher_type = self.tls_session.rcs.cipher.type

        if cipher_type == 'block':
            version = struct.unpack("!H", s[1:3])[0]

            # Decrypt
            try:
                if version >= 0x0302:
                    # Explicit IV for TLS 1.1 and 1.2
                    block_size = self.tls_session.rcs.cipher.block_size
                    iv, efrag = efrag[:block_size], efrag[block_size:]
                    self.tls_session.rcs.cipher.iv = iv
                    pfrag = self._tls_decrypt(efrag)
                else:
                    # Implicit IV for SSLv3 and TLS 1.0
                    pfrag = self._tls_decrypt(efrag)
            except CipherError as e:
                # This will end up dissected as _TLSEncryptedContent.
                cfrag = e.args[0]
            else:
                decryption_success = True
                # Excerpt below better corresponds to TLS 1.1 IV definition,
                # but the result is the same as with TLS 1.2 anyway.
                # This leading *IV* has been decrypted by _tls_decrypt with a
                # random IV, hence it does not correspond to anything.
                # What actually matters is that we got the first encrypted block  # noqa: E501
                # in order to decrypt the second block (first data block).
                # if version >= 0x0302:
                #    block_size = self.tls_session.rcs.cipher.block_size
                #    iv, pfrag = pfrag[:block_size], pfrag[block_size:]
                #    l = struct.unpack('!H', hdr[3:5])[0]
                #    hdr = hdr[:3] + struct.pack('!H', l-block_size)

                # Extract padding ('pad' actually includes the trailing padlen)
                padlen = orb(pfrag[-1]) + 1
                mfrag, pad = pfrag[:-padlen], pfrag[-padlen:]
                self.padlen = padlen

                # Extract MAC
                tmp_len = self.tls_session.rcs.mac_len
                if tmp_len != 0:
                    cfrag, mac = mfrag[:-tmp_len], mfrag[-tmp_len:]
                else:
                    cfrag, mac = mfrag, b""

                # Verify integrity
                chdr = hdr[:3] + struct.pack('!H', len(cfrag))
                is_mac_ok = self._tls_hmac_verify(chdr, cfrag, mac)
                if not is_mac_ok:
                    pkt_info = self.firstlayer().summary()
                    log_runtime.info("TLS: record integrity check failed [%s]", pkt_info)  # noqa: E501

        elif cipher_type == 'stream':
            # Decrypt
            try:
                pfrag = self._tls_decrypt(efrag)
            except CipherError as e:
                # This will end up dissected as _TLSEncryptedContent.
                cfrag = e.args[0]
            else:
                decryption_success = True
                mfrag = pfrag

                # Extract MAC
                tmp_len = self.tls_session.rcs.mac_len
                if tmp_len != 0:
                    cfrag, mac = mfrag[:-tmp_len], mfrag[-tmp_len:]
                else:
                    cfrag, mac = mfrag, b""

                # Verify integrity
                chdr = hdr[:3] + struct.pack('!H', len(cfrag))
                is_mac_ok = self._tls_hmac_verify(chdr, cfrag, mac)
                if not is_mac_ok:
                    pkt_info = self.firstlayer().summary()
                    log_runtime.info("TLS: record integrity check failed [%s]", pkt_info)  # noqa: E501

        elif cipher_type == 'aead':
            # Authenticated encryption
            # crypto/cipher_aead.py prints a warning for integrity failure
            if (conf.crypto_valid_advanced and
                    isinstance(self.tls_session.rcs.cipher, Cipher_CHACHA20_POLY1305)):  # noqa: E501
                iv = b""
                cfrag, mac = self._tls_auth_decrypt(hdr, efrag)
            else:
                iv, cfrag, mac = self._tls_auth_decrypt(hdr, efrag)
            decryption_success = True       # see XXX above

        frag = self._tls_decompress(cfrag)

        if (decryption_success and
                not isinstance(self.tls_session.rcs.cipher, Cipher_NULL)):
            self.deciphered_len = len(frag)
        else:
            self.deciphered_len = None

        reconstructed_body = iv + frag + mac + pad

        return hdr + reconstructed_body + r
def pre_dissect ( self , s ) : if len ( s ) < 5 : raise Exception ( "Invalid record: header is too short." ) msglen = struct . unpack ( '!H' , s [ 3 : 5 ] ) [ 0 ] hdr , efrag , r = s [ : 5 ] , s [ 5 : 5 + msglen ] , s [ msglen + 5 : ] iv = mac = pad = b"" self . padlen = None decryption_success = False cipher_type = self . tls_session . rcs . cipher . type if cipher_type == 'block' : version = struct . unpack ( "!H" , s [ 1 : 3 ] ) [ 0 ] try : if version >= 0x0302 : block_size = self . tls_session . rcs . cipher . block_size iv , efrag = efrag [ : block_size ] , efrag [ block_size : ] self . tls_session . rcs . cipher . iv = iv pfrag = self . _tls_decrypt ( efrag ) else : pfrag = self . _tls_decrypt ( efrag ) except CipherError as e : cfrag = e . args [ 0 ] else : decryption_success = True padlen = orb ( pfrag [ - 1 ] ) + 1 mfrag , pad = pfrag [ : - padlen ] , pfrag [ - padlen : ] self . padlen = padlen tmp_len = self . tls_session . rcs . mac_len if tmp_len != 0 : cfrag , mac = mfrag [ : - tmp_len ] , mfrag [ - tmp_len : ] else : cfrag , mac = mfrag , b"" chdr = hdr [ : 3 ] + struct . pack ( '!H' , len ( cfrag ) ) is_mac_ok = self . _tls_hmac_verify ( chdr , cfrag , mac ) if not is_mac_ok : pkt_info = self . firstlayer ( ) . summary ( ) log_runtime . info ( "TLS: record integrity check failed [%s]" , pkt_info ) elif cipher_type == 'stream' : try : pfrag = self . _tls_decrypt ( efrag ) except CipherError as e : cfrag = e . args [ 0 ] else : decryption_success = True mfrag = pfrag tmp_len = self . tls_session . rcs . mac_len if tmp_len != 0 : cfrag , mac = mfrag [ : - tmp_len ] , mfrag [ - tmp_len : ] else : cfrag , mac = mfrag , b"" chdr = hdr [ : 3 ] + struct . pack ( '!H' , len ( cfrag ) ) is_mac_ok = self . _tls_hmac_verify ( chdr , cfrag , mac ) if not is_mac_ok : pkt_info = self . firstlayer ( ) . summary ( ) log_runtime . info ( "TLS: record integrity check failed [%s]" , pkt_info ) elif cipher_type == 'aead' : if ( conf . crypto_valid_advanced and isinstance ( self . tls_session . rcs . cipher , Cipher_CHACHA20_POLY1305 ) ) : iv = b"" cfrag , mac = self . _tls_auth_decrypt ( hdr , efrag ) else : iv , cfrag , mac = self . _tls_auth_decrypt ( hdr , efrag ) decryption_success = True frag = self . _tls_decompress ( cfrag ) if ( decryption_success and not isinstance ( self . tls_session . rcs . cipher , Cipher_NULL ) ) : self . deciphered_len = len ( frag ) else : self . deciphered_len = None reconstructed_body = iv + frag + mac + pad return hdr + reconstructed_body + r
Decrypt, verify and decompress the message,
        i.e. apply the previous methods according to the reading cipher type.
        If the decryption was successful, 'len' will be the length of the
        TLSPlaintext.fragment. Else, it should be the length of the
        _TLSEncryptedContent.