def create_circuit_hop1(self) -> int: """ The function to setup circuit with the first hop in the circuit. Creates the CREATE/CREATE2 cell and sends it down the socket. It assumes that the open_connection was called on the first node and the socket is connected to the first node :return: Returns a status code. 0 --> Success DH Handshake and -1 --> Means error in processing the cell or the DH Handshake. On error it closes the socket to node 1 """ # First create a CREATE2 Cell. x, x_bytes, gx, gx_bytes = CoreCryptoDH.generate_dh_priv_key() create_cell = Builder.build_create_cell( 'TAP', x_bytes, gx_bytes, self.circ_id, self.node_container[1].onion_key_pub) # Sending a JSON String down the socket self.skt.client_send_data(ComplexStructEncoder.encode(create_cell)) # self.skt.client_send_data(Serialize.obj_to_json(create_cell).encode('utf-8')) # Get the created cell in response and convert it to python Cell Object cell_bytes = self.skt.client_recv_data() created_cell = Parser.parse_encoded_created_cell(cell_bytes) self.session_key01 = Processor.process_created_cell( created_cell, self.circ_id, x_bytes) if self.session_key01 is None: self.skt.close() return -1 return 0
def create_circuit_hop2(self) -> int: """ The function to setup circuit with the second hop in the circuit. Creates the EXTEND/EXTEND2 cell and sends it down the socket. It assumes that the open_connection was called on the first node and the socket is connected to the first node, and that to the second node :return: Returns a status code. 0 --> Success DH Handshake and -1 --> Means error in processing the cell or the DH Handshake. On error it closes the socket to node 2. """ # First create a EXTEND2 Cell. x, x_bytes, gx, gx_bytes = CoreCryptoDH.generate_dh_priv_key() # For hop2 we get its IP:port for LSPEC ==> Link specifier hop2_ip = self.node_container[2].host hop2_port = self.node_container[2].port extend_cell = Builder.build_extend_cell( 'TAP', x_bytes, gx_bytes, self.circ_id, self.node_container[2].onion_key_pub, hop2_ip, hop2_port) print(extend_cell) # Sending a JSON String down the socket self.skt.client_send_data(ComplexStructEncoder.encode(extend_cell)) # Get the extended cell in response and convert it to python Cell Object cell_bytes = self.skt.client_recv_data() extended_cell = Parser.parse_encoded_extended_cell(cell_bytes) self.session_key02 = Processor.process_extended_cell( extended_cell, self.circ_id, x_bytes) if self.session_key02 is None: self.skt.close() return -1 return 0
def handle_create_cell(self, cell_bytes, direction): # Call the Parser for create cell create_cell = Parser.parse_encoded_create_cell(cell_bytes) # Process the create cell y, y_bytes, gy, gy_bytes = CoreCryptoDH.generate_dh_priv_key() gx_bytes, kdf_dict = Processor.process_create_cell(create_cell, self.node.onion_key_pri, y_bytes) # After processing the create cell, we make a created cell # and send it down the socket created_cell = Builder.build_created_cell(y_bytes, gy_bytes, self.circ_id, gx_bytes) print(created_cell) self.conn.sendall(ComplexStructEncoder.encode(created_cell)) print("Created cell sent") self.session_key = kdf_dict return 0