Appendix N — Sockets and TLS Records Answers

Completed Check

Read exactly five bytes for the TLS record header, unpack the content type, version, and length, then keep receiving until you’ve collected the promised payload length.

from fixtures.local_targets import HeartbleedFixtureSocket, parse_tls_record_header, recvall, tls_client_hello

sock = HeartbleedFixtureSocket()
sock.send(tls_client_hello())
content_type, version, length = parse_tls_record_header(sock.recv(5))
payload = recvall(sock, length)
print(content_type, hex(version), payload[:1])
22 0x302 b'\x0e'

The solution reads the fixed five-byte TLS header first, then reads exactly the payload length promised by that header. That pattern matters because TCP gives you a stream of bytes, not tidy message-sized chunks.

Here’s what’s happening: the header tells us how much more to read. If we pretend one recv() always returns the whole payload, the code may work in a toy case and then betray us in real protocol work.

References: Python socket, struct.unpack(), and RFC 5246.