def build_extend_cell(handshake_type: str, x_bytes: bytes, gx_bytes: bytes, circ_id: int, onion_key, hop2_ip, hop2_port) -> Cell: """ The method used to build a Extend/Extend2 cell :param handshake_type: The handshake type. TAP or ntor. :param x_bytes: The diffie hellman private key as a bytes object :param gx_bytes: The diffie hellman public key as a bytes object :param circ_id: The circuit ID :param onion_key: The onion key of the next hop used in hybrid_encrypt method :param lspec: The link specifier as a string :return: The extend Cell object """ client_h_data = CoreCryptoRSA.hybrid_encrypt(gx_bytes, onion_key) nspec = 1 # Always keep this 1 to avoid going to hell lspec = bytearray(IPv4Address(hop2_ip).packed) + pack('!H', int(hop2_port)) extend_cell_payload = RelayExtendPayload(nspec, RelayExtendPayload.LSTYPE_ENUM['TLS_TCP_IPV4'], RelayExtendPayload.LSTYPE_LSLEN_ENUM['TLS_TCP_IPV4'], lspec, CreateCellPayload.CREATE_HANDSHAKE_TYPE[handshake_type], CreateCellPayload.CREATE_HANDSHAKE_LEN[handshake_type], client_h_data) relay_cell_payload = RelayCellPayload(RelayCellPayload.RELAY_CMD_ENUM['RELAY_EXTEND2'], 0, 0, b'', Cell.PAYLOAD_LEN - 11, extend_cell_payload) relay_extend_cell = Cell(circ_id, Cell.CMD_ENUM['RELAY'], Cell.PAYLOAD_LEN, relay_cell_payload) return relay_extend_cell
def parse_encoded_extend_cell(cell_bytes: bytes) -> Cell: cell_tuple = Parser.parse_basic_cell(cell_bytes) relay_cell_payload_tuple = Parser.parse_encoded_relay_cell(cell_bytes) part_fmt_str1 = '=BBB' part_tuple1 = unpack(part_fmt_str1, relay_cell_payload_tuple[5][0:3]) part_fmt_str2 = '=BBB' + str(part_tuple1[2]) + 's' + 'HH' part_tuple2 = unpack( part_fmt_str2, relay_cell_payload_tuple[5][0:(3 + part_tuple1[2] + 2 + 2)]) fmt_str = '=BBB' + str(part_tuple1[2]) + 's' + 'HH' + str( part_tuple2[5]) + 's' relay_extend_payload_tuple = unpack( fmt_str, relay_cell_payload_tuple[5][0:(3 + part_tuple1[2] + 2 + 2 + part_tuple2[5])]) h_data_fmt_str = '=' + str(CC.PK_PAD_LEN) + 's' + str( CC.KEY_LEN) + 's' + str( CC.PK_ENC_LEN - CC.PK_PAD_LEN - CC.KEY_LEN) + 's' + str( CC.DH_LEN - (CC.PK_ENC_LEN - CC.PK_PAD_LEN - CC.KEY_LEN)) + 's' h_data_tuple = unpack(h_data_fmt_str, relay_extend_payload_tuple[6]) h_data = TapCHData(h_data_tuple[0], h_data_tuple[1], h_data_tuple[2], h_data_tuple[3]) relay_extend_payload = RelayExtendPayload( relay_extend_payload_tuple[0], relay_extend_payload_tuple[1], relay_extend_payload_tuple[2], relay_extend_payload_tuple[3], relay_extend_payload_tuple[4], relay_extend_payload_tuple[5], h_data) relay_cell_payload = RelayCellPayload(relay_cell_payload_tuple[0], relay_cell_payload_tuple[1], relay_cell_payload_tuple[2], relay_cell_payload_tuple[3], relay_cell_payload_tuple[4], relay_extend_payload) extend_cell = Cell(cell_tuple[0], cell_tuple[1], cell_tuple[2], relay_cell_payload) return extend_cell