Beispiel #1
0
    def test_old_local_key_packet_raises_fr(self, *_):
        # Setup
        self.key_list.keysets = []
        new_key               = os.urandom(SYMMETRIC_KEY_LENGTH)
        new_hek               = os.urandom(SYMMETRIC_KEY_LENGTH)
        new_conf_code         = os.urandom(CONFIRM_CODE_LENGTH)
        new_packet            = encrypt_and_sign(new_key + new_hek + new_conf_code, key=self.new_kek)

        # Test
        self.assertIsNone(process_local_key(self.ts, self.packet, *self.args))
        self.assert_fr("Error: Received old local key packet.", process_local_key, self.ts, self.packet, *self.args)
        self.assertIsNone(process_local_key(self.ts, new_packet, *self.args))
Beispiel #2
0
    def test_successful_local_key_processing_existing_bootstrap(self, *_):
        # Setup
        self.key_list.keysets = []

        # Test
        self.assertIsNone(process_local_key(self.ts, self.packet, *self.args))
        self.assertEqual(self.window_list.active_win.uid, WIN_UID_LOCAL)
Beispiel #3
0
def process_local_key_queue(queues: 'queue_dict', window_list: 'WindowList',
                            contact_list: 'ContactList', key_list: 'KeyList',
                            settings: 'Settings', kdk_hashes: List[bytes],
                            packet_hashes: List[bytes]) -> None:
    """Check local key queue for packets.

    This function also checks that local key is installed.
    """
    local_key_queue = queues[LOCAL_KEY_DATAGRAM_HEADER]

    if local_key_queue.qsize():
        ts, packet = local_key_queue.get()
        process_local_key(ts, packet, window_list, contact_list, key_list,
                          settings, kdk_hashes, packet_hashes, local_key_queue)

    if not contact_list.has_local_contact():
        time.sleep(0.1)
        raise SoftError("No local key", output=False)
Beispiel #4
0
    def test_loading_local_key_from_queue(self, *_):
        # Setup
        self.key_list.keysets = []
        new_key               = os.urandom(SYMMETRIC_KEY_LENGTH)
        new_hek               = os.urandom(SYMMETRIC_KEY_LENGTH)
        new_conf_code         = os.urandom(CONFIRM_CODE_LENGTH)
        new_packet            = encrypt_and_sign(new_key + new_hek + new_conf_code, key=self.new_kek)
        next_packet           = os.urandom(len(new_packet))
        first_packet          = os.urandom(len(new_packet))
        self.l_queue.put((datetime.now(), first_packet))
        self.l_queue.put((datetime.now(), new_packet))
        self.l_queue.put((datetime.now(), next_packet))

        # Test
        self.assertEqual(self.l_queue.qsize(), 3)
        self.assertIsNone(process_local_key(self.ts, self.packet, *self.args))
        self.assertEqual(self.l_queue.qsize(), 1)
Beispiel #5
0
 def test_successful_local_key_processing_with_existing_local_key(self, *_):
     self.assert_fr("Error: Incorrect key decryption key.",
                    process_local_key, self.ts, self.packet, *self.args)
     self.assertIsNone(process_local_key(self.ts, self.packet, *self.args))
Beispiel #6
0
def output_loop(queues: Dict[bytes, 'Queue[Any]'],
                gateway: 'Gateway',
                settings: 'Settings',
                contact_list: 'ContactList',
                key_list: 'KeyList',
                group_list: 'GroupList',
                master_key: 'MasterKey',
                stdin_fd: int,
                unit_test: bool = False) -> None:
    """Process packets in message queues according to their priority."""
    local_key_queue = queues[LOCAL_KEY_DATAGRAM_HEADER]
    message_queue = queues[MESSAGE_DATAGRAM_HEADER]
    file_queue = queues[FILE_DATAGRAM_HEADER]
    command_queue = queues[COMMAND_DATAGRAM_HEADER]
    exit_queue = queues[EXIT_QUEUE]

    sys.stdin = os.fdopen(stdin_fd)
    packet_buffer = dict()  # type: Dict[bytes, List[Tuple[datetime, bytes]]]
    file_buffer = dict()  # type: Dict[bytes, Tuple[datetime, bytes]]
    file_keys = dict()  # type: Dict[bytes, bytes]

    kdk_hashes = []  # type: List[bytes]
    packet_hashes = []  # type: List[bytes]

    packet_list = PacketList(settings, contact_list)
    window_list = WindowList(settings, contact_list, group_list, packet_list)

    clear_screen()
    while True:
        try:
            if local_key_queue.qsize() != 0:
                ts, packet = local_key_queue.get()
                process_local_key(ts, packet, window_list, contact_list,
                                  key_list, settings, kdk_hashes,
                                  packet_hashes, local_key_queue)
                continue

            if not contact_list.has_local_contact():
                time.sleep(0.1)
                continue

            # Commands
            if command_queue.qsize() != 0:
                ts, packet = command_queue.get()
                process_command(ts, packet, window_list, packet_list,
                                contact_list, key_list, group_list, settings,
                                master_key, gateway, exit_queue)
                continue

            # File window refresh
            if window_list.active_win is not None and window_list.active_win.uid == WIN_UID_FILE:
                window_list.active_win.redraw_file_win()

            # Cached message packets
            for onion_pub_key in packet_buffer:
                if (contact_list.has_pub_key(onion_pub_key)
                        and key_list.has_rx_mk(onion_pub_key)
                        and packet_buffer[onion_pub_key]):
                    ts, packet = packet_buffer[onion_pub_key].pop(0)
                    process_message(ts, packet, window_list, packet_list,
                                    contact_list, key_list, group_list,
                                    settings, master_key, file_keys)
                    continue

            # New messages
            if message_queue.qsize() != 0:
                ts, packet = message_queue.get()
                onion_pub_key = packet[:ONION_SERVICE_PUBLIC_KEY_LENGTH]

                if contact_list.has_pub_key(
                        onion_pub_key) and key_list.has_rx_mk(onion_pub_key):
                    process_message(ts, packet, window_list, packet_list,
                                    contact_list, key_list, group_list,
                                    settings, master_key, file_keys)
                else:
                    packet_buffer.setdefault(onion_pub_key, []).append(
                        (ts, packet))
                continue

            # Cached files
            if file_buffer:
                for k in file_buffer:
                    key_to_remove = b''
                    try:
                        if k in file_keys:
                            key_to_remove = k
                            ts_, file_ct = file_buffer[k]
                            dec_key = file_keys[k]
                            onion_pub_key = k[:ONION_SERVICE_PUBLIC_KEY_LENGTH]
                            process_file(ts_, onion_pub_key, file_ct, dec_key,
                                         contact_list, window_list, settings)
                    finally:
                        if key_to_remove:
                            file_buffer.pop(k)
                            file_keys.pop(k)
                            break

            # New files
            if file_queue.qsize() != 0:
                ts, packet = file_queue.get()
                new_file(ts, packet, file_keys, file_buffer, contact_list,
                         window_list, settings)

            time.sleep(0.01)

            if unit_test and queues[UNIT_TEST_QUEUE].qsize() != 0:
                break

        except (FunctionReturn, KeyError, KeyboardInterrupt):
            pass