示例#1
0
def test_advanced_ciphertext_message():
    add_message = AddMessage(index=1337,
                             init_key=os.urandom(32),
                             welcome_info_hash=os.urandom(32))
    group_op = GroupOperation(msg_type=GroupOperationType.ADD,
                              operation=add_message)
    handshake = MLSPlaintextHandshake(confirmation=12,
                                      group_operation=group_op)

    message = MLSPlaintext(group_id=b'helloworld',
                           epoch=1337,
                           content_type=ContentType.HANDSHAKE,
                           sender=42,
                           signature=b'steffensoddemann',
                           content=handshake)

    encrypted_message = MLSCiphertext(group_id=message.group_id,
                                      epoch=message.epoch,
                                      content_type=message.content_type,
                                      sender_data_nounce=b'0',
                                      encrypted_sender_data=b'0',
                                      ciphertext=message.pack())

    box_thingy = MLSCiphertext.from_bytes(encrypted_message.pack())
    assert box_thingy == encrypted_message
    assert MLSPlaintext.from_bytes(box_thingy.ciphertext) == message
def test_double_update():
    alice_store = LocalKeyStoreMock('alice')
    alice_store.register_keypair(b'0', b'0')

    bob_store = LocalKeyStoreMock('bob')
    bob_store.register_keypair(b'1', b'1')

    # setup session
    alice_session = Session.from_empty(alice_store, 'alice', 'test')
    welcome, add = alice_session.add_member('bob', b'1')
    bob_session = Session.from_welcome(welcome, bob_store, 'bob')

    alice_session.process_add(add_message=add)
    bob_session.process_add(add_message=add)

    update_op1 = GroupOperation(msg_type=GroupOperationType.UPDATE, operation=alice_session.update())
    cipher1 = alice_session.encrypt_handshake_message(update_op1)

    my_handler = StubHandler()
    alice_session.process_message(cipher1, my_handler)
    bob_session.process_message(cipher1, my_handler)

    update_op2 = GroupOperation(msg_type=GroupOperationType.UPDATE, operation=alice_session.update())
    cipher2 = alice_session.encrypt_handshake_message(update_op2)

    alice_session.process_message(cipher2, my_handler)
    bob_session.process_message(cipher2, my_handler)

    # compare tree hashses
    assert alice_session.get_state().get_tree().get_tree_hash() == bob_session.get_state().get_tree().get_tree_hash()

    # compare key schedule secrets
    assert alice_session.get_state().get_key_schedule().get_epoch_secret() == \
           bob_session.get_state().get_key_schedule().get_epoch_secret()

    assert alice_session.get_state().get_group_context() == \
           bob_session.get_state().get_group_context()
示例#3
0
def test_plaintext_message():
    add_message = AddMessage(index=1337,
                             init_key=os.urandom(32),
                             welcome_info_hash=os.urandom(32))
    group_op = GroupOperation(msg_type=GroupOperationType.ADD,
                              operation=add_message)
    handshake = MLSPlaintextHandshake(confirmation=12,
                                      group_operation=group_op)

    message = MLSPlaintext(group_id=b'helloworld',
                           epoch=1337,
                           content_type=ContentType.HANDSHAKE,
                           sender=42,
                           signature=b'steffensoddemann',
                           content=handshake)

    assert MLSPlaintext.from_bytes(message.pack()) == message
def test_update_message_with_one_member():
    alice_store = LocalKeyStoreMock('alice')
    alice_store.register_keypair(b'0', b'0')

    alice_session = Session.from_empty(alice_store, 'alice', 'test')

    first_hash = alice_session.get_state().get_tree().get_tree_hash()
    update_op = GroupOperation(msg_type=GroupOperationType.UPDATE, operation=alice_session.update())
    second_hash = alice_session.get_state().get_tree().get_tree_hash()

    assert second_hash != first_hash
    cipher = alice_session.encrypt_handshake_message(update_op)

    my_handler = StubHandler()
    alice_session.process_message(cipher, my_handler)
    third_hash = alice_session.get_state().get_tree().get_tree_hash()

    assert third_hash == second_hash
def test_handshake_processing():
    alice_store = LocalKeyStoreMock('alice')
    alice_store.register_keypair(b'0', b'0')

    bob_store = LocalKeyStoreMock('bob')
    bob_store.register_keypair(b'1', b'1')

    # setup session
    alice_session = Session.from_empty(alice_store, 'alice', 'test')
    welcome, add = alice_session.add_member('bob', b'1')

    welcome = WelcomeInfoMessage.from_bytes(welcome.pack())

    encrypted_add = alice_session.encrypt_handshake_message(GroupOperation.from_instance(add))

    bob_session = Session.from_welcome(welcome, bob_store, 'bob')
    bob_session.process_message(encrypted_add, StubHandler())
    alice_session.process_message(encrypted_add, StubHandler())

    # assert that both sessions have the same state after adds
    assert alice_session.get_state().get_tree().get_num_nodes() == 3
    assert bob_session.get_state().get_tree().get_num_nodes() == 3
示例#6
0
    def group_add(self, group_name: str, user: str):
        """
        Add a member to a group
        Just adding to channel for now
        :return:
        """

        # get init-key of new member

        # broadcast add message

        # update state(every member)

        # send welcome message
        try:
            chat = self.chats[group_name]

            welcome, add = chat.session.add_member(user, b'0')
            group_op = GroupOperation.from_instance(add)

            # add_payload = chat.session.encrypt_handshake_message(add)

            self.send_welcome_to_user(user, welcome)
            chat.users.append(User(user))
            self.send_message_to_group(group_name=group_name,
                                       handshake=group_op)

            # pylint: disable=unexpected-keyword-arg
            name_update_msg = ChatProtocolMessage(
                msg_type=ChatProtocolMessageType.USER_LIST,
                contents=ChatUserListMessage(
                    user_names=[user.name for user in chat.users]))

            self.send_message_to_group(group_name=group_name,
                                       message=name_update_msg.pack())
        except requests.exceptions.ConnectionError:
            raise ConnectionError
示例#7
0
 def group_update(self, chat: Chat):
     update_message = chat.session.update()
     group_op = GroupOperation.from_instance(update_message)
     self.send_message_to_group(group_name=chat.name, handshake=group_op)