comparison sat/plugins/plugin_xep_0384.py @ 3969:8e7d5796fb23

plugin XEP-0391: implement XEP-0391 (Jingle Encrypted Transports) + XEP-0396 (JET-OMEMO): rel 378
author Goffi <goffi@goffi.org>
date Mon, 31 Oct 2022 04:09:34 +0100
parents f461f11ea176
children 5fbdf986670c
comparison
equal deleted inserted replaced
3968:0dd79c6cc1d2 3969:8e7d5796fb23
1628 ) -> None: 1628 ) -> None:
1629 """ 1629 """
1630 @param client: The client. 1630 @param client: The client.
1631 """ 1631 """
1632 1632
1633 await self.__prepare_for_profile(cast(str, client.profile)) 1633 await self.get_session_manager(
1634 cast(str, client.profile)
1635 )
1634 1636
1635 async def cmd_omemo_reset( 1637 async def cmd_omemo_reset(
1636 self, 1638 self,
1637 client: SatXMPPClient, 1639 client: SatXMPPClient,
1638 mess_data: MessageData 1640 mess_data: MessageData
1663 ) 1665 )
1664 return False 1666 return False
1665 1667
1666 bare_jid = mess_data["to"].userhost() 1668 bare_jid = mess_data["to"].userhost()
1667 1669
1668 session_manager = await self.__prepare_for_profile(client.profile) 1670 session_manager = await self.get_session_manager(client.profile)
1669 devices = await session_manager.get_device_information(bare_jid) 1671 devices = await session_manager.get_device_information(bare_jid)
1670 1672
1671 for device in devices: 1673 for device in devices:
1672 log.debug(f"Replacing sessions with device {device}") 1674 log.debug(f"Replacing sessions with device {device}")
1673 await session_manager.replace_sessions(device) 1675 await session_manager.replace_sessions(device)
1699 if self.__xep_0045 is not None and self.__xep_0045.isJoinedRoom(client, entity): 1701 if self.__xep_0045 is not None and self.__xep_0045.isJoinedRoom(client, entity):
1700 bare_jids = self.__get_joined_muc_users(client, self.__xep_0045, entity) 1702 bare_jids = self.__get_joined_muc_users(client, self.__xep_0045, entity)
1701 else: 1703 else:
1702 bare_jids = { entity.userhost() } 1704 bare_jids = { entity.userhost() }
1703 1705
1704 session_manager = await self.__prepare_for_profile(client.profile) 1706 session_manager = await self.get_session_manager(client.profile)
1705 1707
1706 # At least sort the devices by bare JID such that they aren't listed completely 1708 # At least sort the devices by bare JID such that they aren't listed completely
1707 # random 1709 # random
1708 devices = sorted(cast(Set[omemo.DeviceInformation], set()).union(*[ 1710 devices = sorted(cast(Set[omemo.DeviceInformation], set()).union(*[
1709 await session_manager.get_device_information(bare_jid) 1711 await session_manager.get_device_information(bare_jid)
1894 1896
1895 bare_jids.add(entity.jid.userhost()) 1897 bare_jids.add(entity.jid.userhost())
1896 1898
1897 return bare_jids 1899 return bare_jids
1898 1900
1899 async def __prepare_for_profile(self, profile: str) -> omemo.SessionManager: 1901 async def get_session_manager(self, profile: str) -> omemo.SessionManager:
1900 """ 1902 """
1901 @param profile: The profile to prepare for. 1903 @param profile: The profile to prepare for.
1902 @return: A session manager instance for this profile. Creates a new instance if 1904 @return: A session manager instance for this profile. Creates a new instance if
1903 none was prepared before. 1905 none was prepared before.
1904 """ 1906 """
2196 message_elt.elements(oldmemo.oldmemo.NAMESPACE, "encrypted"), 2198 message_elt.elements(oldmemo.oldmemo.NAMESPACE, "encrypted"),
2197 None 2199 None
2198 )) 2200 ))
2199 2201
2200 try: 2202 try:
2201 session_manager = await self.__prepare_for_profile(cast(str, client.profile)) 2203 session_manager = await self.get_session_manager(cast(str, client.profile))
2202 except Exception as e: 2204 except Exception as e:
2203 log.error(f"error while preparing profile for {client.profile}: {e}") 2205 log.error(f"error while preparing profile for {client.profile}: {e}")
2204 # we don't want to block the workflow 2206 # we don't want to block the workflow
2205 return True 2207 return True
2206 2208
2263 2265
2264 # Since this message was sent by us, use the own device information here 2266 # Since this message was sent by us, use the own device information here
2265 device_information, __ = await session_manager.get_own_device_information() 2267 device_information, __ = await session_manager.get_own_device_information()
2266 else: 2268 else:
2267 try: 2269 try:
2268 plaintext, device_information = await session_manager.decrypt(message) 2270 plaintext, device_information, __ = await session_manager.decrypt(message)
2269 except omemo.MessageNotForUs: 2271 except omemo.MessageNotForUs:
2270 # The difference between this being a debug or a warning is whether there 2272 # The difference between this being a debug or a warning is whether there
2271 # is a body included in the message. Without a body, we can assume that 2273 # is a body included in the message. Without a body, we can assume that
2272 # it's an empty OMEMO message used for protocol stability reasons, which 2274 # it's an empty OMEMO message used for protocol stability reasons, which
2273 # is not expected to be sent to all devices of all recipients. If a body 2275 # is not expected to be sent to all devices of all recipients. If a body
2582 if plaintext is None: 2584 if plaintext is None:
2583 return 2585 return
2584 2586
2585 log.debug(f"Plaintext to encrypt: {plaintext}") 2587 log.debug(f"Plaintext to encrypt: {plaintext}")
2586 2588
2587 session_manager = await self.__prepare_for_profile(client.profile) 2589 session_manager = await self.get_session_manager(client.profile)
2588 2590
2589 try: 2591 try:
2590 messages, encryption_errors = await session_manager.encrypt( 2592 messages, encryption_errors = await session_manager.encrypt(
2591 frozenset(recipient_bare_jids), 2593 frozenset(recipient_bare_jids),
2592 { namespace: plaintext }, 2594 { namespace: plaintext },
2700 f"Malformed device list update item:" 2702 f"Malformed device list update item:"
2701 f" {ET.tostring(item_elt, encoding='unicode')}" 2703 f" {ET.tostring(item_elt, encoding='unicode')}"
2702 ) 2704 )
2703 return 2705 return
2704 2706
2705 session_manager = await self.__prepare_for_profile(profile) 2707 session_manager = await self.get_session_manager(profile)
2706 2708
2707 await session_manager.update_device_list( 2709 await session_manager.update_device_list(
2708 namespace, 2710 namespace,
2709 sender.userhost(), 2711 sender.userhost(),
2710 device_list 2712 device_list