import hashlib import hmac import os import struct import time import unittest from proxy.fake_tls import ( CLIENT_RANDOM_LEN, CLIENT_RANDOM_OFFSET, SESSION_ID_LEN, SESSION_ID_OFFSET, TLS_APPDATA_MAX, TLS_RECORD_HANDSHAKE, build_server_hello, verify_client_hello, wrap_tls_record, ) SECRET = bytes.fromhex('00112233445566778899aabbccddeeff') def _client_hello(secret: bytes = SECRET, timestamp: int = None, session_id: bytes = None) -> bytes: if timestamp is None: timestamp = int(time.time()) if session_id is None: session_id = os.urandom(SESSION_ID_LEN) body = bytearray(517) body[0] = TLS_RECORD_HANDSHAKE body[1:3] = b'\x03\x01' struct.pack_into('>H', body, 3, len(body) - 5) body[5] = 0x01 body[43] = 0x20 body[SESSION_ID_OFFSET:SESSION_ID_OFFSET + SESSION_ID_LEN] = session_id digest = hmac.new(secret, bytes(body), hashlib.sha256).digest() client_random = bytearray(digest[:CLIENT_RANDOM_LEN]) ts_bytes = struct.pack('H', wrapped[offset + 3:offset + 5])[0] self.assertLessEqual(length, TLS_APPDATA_MAX) chunks.append(wrapped[offset + 5:offset + 5 + length]) offset += 5 + length self.assertEqual(len(chunks), 2) self.assertEqual(b''.join(chunks), payload) def test_empty_payload_produces_no_records(self): self.assertEqual(wrap_tls_record(b''), b'') if __name__ == '__main__': unittest.main()