TLS Record Reassembly¶
pcapkit.foundation.reassembly.tls.reassembly contains
TLS only, which reconstructs
the TLS records of a TCP stream, a record split across segments included
(RFC 9846 Section 5).
Added in version 1.9.0.
- class pcapkit.foundation.reassembly.tls.reassembly.TLS[_PT: Info, _DT: Info, _IT: tuple, _BT: Info](*, strict=True, store=True, timeout=None)[source]¶
Bases:
ReassemblyBase[Packet,Datagram[_AT],tuple[_AT,int,_AT,int],Buffer],Generic[_AT]Reassembly for TLS records.
- Parameters:
strict (
bool) – ifTrue, report a record that is not completely reassembled as the tuple of its fragment’s received runs; otherwise as one contiguous fragment, its holes zero-filledstore (
bool) – if store reassembled datagram in memory, i.e.,self._dtgram(if not, datagram will be discarded after callback)timeout (
float|None) – reassembly timeout in seconds, on the capture’s own clock;Noneselects__timeout__, which for TLS disables expiry
Note
Over TCP a record is delimited by its header alone (RFC 9846 Section 5.1), so each direction of a connection is followed as one octet stream, from a record boundary, and cut into records by
TLS.record_header. Every record is reported, the ones a segment holds whole included, since a boundary is known only from the record before it.The stream is the segments’ octets in sequence order (RFC 9293 Section 3.4). A segment ahead of the octets held waits for those before it, and of octets received twice the first are kept (RFC 9293 Section 3.10). A record is reported once whole. One the stream ends inside – at its FIN, an RST or the end of the capture – is reported incomplete, and so is one a lost segment left a hole in, as the records after it are.
The
Extractorfeeds it TCP reassembly’s reasm.tcp.packet of every segment on a portTCP.__proto__binds to TLS, throughpcapkit.toolkit.pcap.tls_reassembly()and its siblings, andtls_record()decodes what it reassembles. A complete record’spacketholds the same decode, run on first read; both call the protocol’sanalyze_record(), thepacketthat of__protocol_type__.Warning
Where the stream starts. With its SYN captured, the stream is followed from the octet after it, a record boundary. Without it, and again after octets that open no record, the boundary is a guess: the first segment arriving whose payload opens a record header, by
TLS.record_header, is taken to open a record, and the octets before it are not reported. Octets past a hole that covers a record header cannot be cut into records, and are not reported either.What the guess can get wrong. The header’s length may not exceed
2 ** 14 + 2048(RFC 5246 Section 6.2.3), so a segment whose first octets declare a longer record, as17 03 03 ff ffwould, opens no stream. Ciphertext or a record’s tail whose first five octets read as a header within that bound still does: the records cut from it are false, and those of the real stream behind it, up to the next octets that open no record, are lost.Where it ends. A FIN or an RST closes the stream, and a later segment on the same endpoints is ignored unless it is a SYN.
Example
>>> from pcapkit.foundation.reassembly.tls.reassembly import TLS # Initialise instance: >>> tls_reassembly = TLS() # Call reassembly: >>> tls_reassembly(packet_dict) # Fetch result: >>> result = tls_reassembly.datagram
Defaults
- __protocol_type__: Type[ProtocolBase] = <class 'pcapkit.protocols.application.tls.protocol.TLS'>¶
Protocol of current reassembly object. An extension point – a protocol named here need only define
record_headerandanalyze_record, with the signatures ofTLS.record_headerandTLS.analyze_record.
Methods
- reassembly(info)[source]¶
Reassembly procedure.
Note
The records the segment completes are reported before its RST, or the FIN it completes the stream to, releases the buffer. So a callback raising on one of them leaves the buffer in place, holding the octets after them, and nothing is lost.
- submit(buf, *, bufid, expired=False, store=False)[source]¶
Submit reassembled payload.
- Parameters:
- Return type:
- Returns:
Every record still held in
bufwhose header is, in sequence order up to the first hole a header lies in, each reportedCOMPLETEif its fragment is whole, otherwiseTIMEOUTorPARTIAL.
Utilities
- _read_header(data)[source]¶
Read the header of the record
dataopens.- Parameters:
- Returns:
meth:TLS.record_header <pcapkit.protocols.application.tls.protocol.TLS.record_header> of
__protocol_type__returns it.- Return type:
- Raises:
ProtocolError – If the header opens no record, constructed quietly, since the caller handles it.
- static _hold_segment(buf, first, payload, num)[source]¶
Hold the octets of a segment that no earlier segment brought.
- Parameters:
Octets before
boundarywere cut into records already, and octets at or pastfinare not the stream’s, so neither is held. Of octets already held the first received are kept, and only the new parts are processed (RFC 9293 Section 3.10).
- static _hold_octets(runs, first, octets)[source]¶
Add octets no run holds to the runs, merging the runs they adjoin.
- _make_datagram(buf, bufid, start, header, completion, fragment)[source]¶
Build a
Datagramfor a record.- Parameters:
bufid (
tuple[_AT,int,_AT,int]) – buffer identifier it was held understart (
int) – unwrapped sequence number of the record’s first octetheader (
tuple[ContentType,int,int,int]) – the record’s header, as_read_header()reads itcompletion (
Completion) – how completely it was reassembledfragment (
bytes|tuple[bytes,...]) – its fragment, as the record reports it
- Return type:
- Returns:
The record. Its
packetis the decodetls_record()runs, by this reassembly’s ownprotocol, deferred to its first read, if the record is complete, andNoneotherwise, as that function returns.
Algorithm¶
See also
A record over TCP is delimited by its header alone (RFC 9846 Section 5.1), and the stream is the segments’ octets in sequence order (RFC 9293 Section 3.4).
Each direction of a connection is a buffer, which holds the octets past the next record’s boundary as runs in sequence order. A segment’s new octets join the runs, and the records whole from the boundary on are cut off and reported:
flowchart TD
SEG["a segment, as TCP reassembly records it"]
SYN{"SYN?"}
OPEN["a fresh buffer, its boundary<br/>the octet after the SYN"]
HAS{"a buffer?"}
SYNC{"its payload opens<br/>a record header?"}
DROP["ignored"]
HOLD["hold the octets no earlier<br/>segment brought"]
HEAD{"a header held<br/>at the boundary?"}
BAD{"it opens a record?"}
GONE["the buffer is dropped"]
WHOLE{"the record whole?"}
CUT["report it COMPLETE, and<br/>move the boundary past it"]
END{"an RST, or a FIN with<br/>every octet before it?"}
CLOSE["submit the buffer, and<br/>close the stream"]
WAIT["wait for the next segment"]
SEG --> SYN
SYN -->|yes| OPEN --> HOLD
SYN -->|no| HAS
HAS -->|open| HOLD
HAS -->|closed| DROP
HAS -->|none| SYNC
SYNC -->|no| DROP
SYNC -->|"yes: a buffer from it"| HOLD
HOLD --> HEAD
HEAD -->|no| END
HEAD -->|yes| BAD
BAD -->|no| GONE
BAD -->|yes| WHOLE
WHOLE -->|yes| CUT --> HEAD
WHOLE -->|no| END
END -->|yes| CLOSE
END -->|no| WAIT
Without the SYN, the boundary a buffer opens at is a guess, which the length bound of
TLS.record_header
narrows but cannot make safe; the class’s warning above says what it rules out.
submit() reports what a buffer
still holds, record by record from the boundary: one whose fragment is whole
COMPLETE, and one with a hole
PARTIAL, or
TIMEOUT once expired. It stops at
the first hole a header lies in.
Terminology¶
The reassembly is fed reasm.tcp.packet, TCP reassembly’s record of a segment.
- reasm.tls.datagram¶
Data structure for reassembled TLS record (element of
TLS.datagram) isDatagram–completed, the original record identifier, the packet numbers that carried it, the record header’scontent_type,versionandlength,fragment, the reassembled fragment (a contiguousbytes, or – understrict=Trueand an incomplete record – atupleof the octet runs actually received), andpacket, whattls_record()decodes the record to.packetis decoded on first read, as a DTLS message’s is, and isNonefor an incomplete record.- reasm.tls.buffer¶
Data structure for internal buffering (
TLS._buffer) isBuffer– one direction of a TCP connection, keyed by itsBufferID: the next record’s boundary, the octets held past it asRunobjects, the octets each packet added, and where the stream ends.
Data Models¶
- pcapkit.foundation.reassembly.tls.data.BufferID¶
Buffer ID – one direction of a TCP connection, its source address and port and its destination address and port, as for TCP reassembly.
- class pcapkit.foundation.reassembly.tls.data.DatagramID[_AT: (IPv4Address, IPv6Address)](src, dst, seq)[source]¶
-
Data model for TLS original record identifier.
- class pcapkit.foundation.reassembly.tls.data.Datagram[_AT: (IPv4Address, IPv6Address)](completed, id, index, content_type, version, length, fragment, packet)[source]¶
Bases:
DeferredPacket,Info,Generic[_AT]Data model for TLS reassembled record.
- completed: Completion¶
How completely the record was reassembled, and why reassembly stopped; see
Completion.
- id: DatagramID[_AT]¶
Original record identifier.
- content_type: ContentType¶
Record content type.
- packet: ProtocolBase | None¶
Decoded record, as
tls_record()returns it unless the reassembly’s__protocol_type__names another protocol, whoseanalyze_recordthen decodes it. Decoded on first read, andNonefor an incomplete record. ADeferredmay be passed in its place, and reading this attribute then runs it and keeps the result.
- class pcapkit.foundation.reassembly.tls.data.Run(first, raw)[source]¶
Bases:
InfoData model for TLS run of contiguous octets held.
- class pcapkit.foundation.reassembly.tls.data.Buffer(boundary, runs, ind, fin, closed, timestamp)[source]¶
Bases:
InfoData model for TLS buffer entry – one direction of a TCP connection, followed from a record boundary.
- boundary: int¶
Unwrapped sequence number of the next record’s first octet. Sequence numbers here are read modulo
2 ** 32relative to it, so a stream crossing2 ** 32continues past it.
- runs: list[Run]¶
Octets held at or past
boundary, in sequence order, none adjacent to or overlapping another.
- ind: list[tuple[int, int, int]]¶
Octets each packet added. Each entry is
(num, first, stop), its number and the unwrapped sequence range,stopexclusive, in the order they arrived.
Type Variables¶
- pcapkit.foundation.reassembly.tls.data._AT: ipaddress.IPv4Address | ipaddress.IPv6Address¶
IP address type.