diff --git a/scapy/utils.py b/scapy/utils.py index 0879f3322bf..ec7f45436c3 100644 --- a/scapy/utils.py +++ b/scapy/utils.py @@ -1657,6 +1657,7 @@ def __init__(self, filename, fdesc=None, magic=None): # type: ignore } self.endian = "!" # Will be overwritten by first SHB self.process_information = [] # type: List[Dict[str, Any]] + self._tls_state = None # type: Optional[Tuple[Dict[str, bytes], bool]] if magic != b"\x0a\x0d\x0d\x0a": # PcapNg: raise Scapy_Exception( @@ -2016,11 +2017,23 @@ def _read_block_dsb(self, block, size): else: # Note: these attributes are only available when the TLS # layer is loaded. + if self._tls_state is None: + self._tls_state = ( + conf.tls_nss_keys, + conf.tls_session_enable, + ) conf.tls_nss_keys = keys conf.tls_session_enable = True else: warning("PcapNg: Unknown DSB secrets type (0x%x)!", secrets_type) + def close(self): + # type: () -> None + if self._tls_state is not None: + conf.tls_nss_keys, conf.tls_session_enable = self._tls_state + self._tls_state = None + RawPcapReader.close(self) + def _read_block_pib(self, block, _): # type: (bytes, int) -> None """Apple Process Information Block""" diff --git a/test/scapy/layers/tls/tls.uts b/test/scapy/layers/tls/tls.uts index e3d194b2763..fa9a70d0942 100644 --- a/test/scapy/layers/tls/tls.uts +++ b/test/scapy/layers/tls/tls.uts @@ -1633,6 +1633,42 @@ if shutil.which("editcap"): assert b"BEGIN PRIVATE KEY" in packets[28].inner.msg[0].data conf = bck_conf += pcapng Decryption Secrets Block state follows the reader lifecycle + +import io +import struct + +def pcapng_block(block_type, body): + body += b"\x00" * (-len(body) % 4) + length = 12 + len(body) + return ( + struct.pack("