[omemo] Build sessions automatically
This commit is contained in:
@@ -497,6 +497,12 @@ class LiteAxolotlStore(AxolotlStore):
|
|||||||
|
|
||||||
return self._con.execute(query, jids).fetchall()
|
return self._con.execute(query, jids).fetchall()
|
||||||
|
|
||||||
|
def hasUndecidedFingerprints(self, jid):
|
||||||
|
query = '''SELECT public_key as "public_key [pk]" FROM identities
|
||||||
|
WHERE recipient_id = ? AND trust = ?'''
|
||||||
|
result = self._con.execute(query, (jid, Trust.UNDECIDED)).fetchall()
|
||||||
|
return True if result else False
|
||||||
|
|
||||||
def getTrustedFingerprints(self, jid):
|
def getTrustedFingerprints(self, jid):
|
||||||
query = '''SELECT public_key as "public_key [pk]" FROM identities
|
query = '''SELECT public_key as "public_key [pk]" FROM identities
|
||||||
WHERE recipient_id = ? AND trust = ?'''
|
WHERE recipient_id = ? AND trust = ?'''
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ class OmemoState(DeviceManager):
|
|||||||
ik)
|
ik)
|
||||||
|
|
||||||
session.processPreKeyBundle(prekey_bundle)
|
session.processPreKeyBundle(prekey_bundle)
|
||||||
return self._get_session_cipher(jid, device_id)
|
self._get_session_cipher(jid, device_id)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def storage(self):
|
def storage(self):
|
||||||
|
|||||||
@@ -110,6 +110,7 @@ class OMEMO(BaseModule):
|
|||||||
self._omemo_groupchats = set()
|
self._omemo_groupchats = set()
|
||||||
self._muc_temp_store = {}
|
self._muc_temp_store = {}
|
||||||
self._query_for_bundles = []
|
self._query_for_bundles = []
|
||||||
|
self._device_bundle_querys = []
|
||||||
self._query_for_devicelists = []
|
self._query_for_devicelists = []
|
||||||
|
|
||||||
def get_own_jid(self, stripped=False):
|
def get_own_jid(self, stripped=False):
|
||||||
@@ -356,6 +357,14 @@ class OMEMO(BaseModule):
|
|||||||
self._omemo_groupchats.add(room)
|
self._omemo_groupchats.add(room)
|
||||||
log.info('Room config change: non-anonymous')
|
log.info('Room config change: non-anonymous')
|
||||||
|
|
||||||
|
def _check_for_missing_sessions(self, jid):
|
||||||
|
devices_without_session = self.backend.devices_without_sessions(jid)
|
||||||
|
for device_id in devices_without_session:
|
||||||
|
if device_id in self._device_bundle_querys:
|
||||||
|
continue
|
||||||
|
self._device_bundle_querys.append(device_id)
|
||||||
|
self.request_bundle(jid, device_id)
|
||||||
|
|
||||||
def are_keys_missing(self, contact_jid):
|
def are_keys_missing(self, contact_jid):
|
||||||
""" Checks if devicekeys are missing and queries the
|
""" Checks if devicekeys are missing and queries the
|
||||||
bundles
|
bundles
|
||||||
@@ -420,16 +429,19 @@ class OMEMO(BaseModule):
|
|||||||
self._account, jid, device_id, bundle)
|
self._account, jid, device_id, bundle)
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.backend.build_session(jid, device_id, bundle):
|
self.backend.build_session(jid, device_id, bundle)
|
||||||
log.info('%s => session created for: %s',
|
log.info('%s => session created for: %s',
|
||||||
self._account, jid)
|
self._account, jid)
|
||||||
# Trigger dialog to trust new Fingerprints if
|
# TODO: In MUC we should send a groupchat message
|
||||||
# the Chat Window is Open
|
self._send_key_transport_message('chat', jid, [device_id])
|
||||||
ctrl = app.interface.msg_win_mgr.get_control(
|
|
||||||
jid, self._account)
|
# Trigger dialog to trust new Fingerprints if
|
||||||
if ctrl:
|
# the Chat Window is Open
|
||||||
app.nec.push_incoming_event(
|
ctrl = app.interface.msg_win_mgr.get_control(
|
||||||
NetworkEvent('omemo-new-fingerprint', chat_control=ctrl))
|
jid, self._account)
|
||||||
|
if ctrl:
|
||||||
|
app.nec.push_incoming_event(
|
||||||
|
NetworkEvent('omemo-new-fingerprint', chat_control=ctrl))
|
||||||
|
|
||||||
def set_devicelist(self):
|
def set_devicelist(self):
|
||||||
log.info('%s => Publishing own devicelist: %s',
|
log.info('%s => Publishing own devicelist: %s',
|
||||||
@@ -440,24 +452,26 @@ class OMEMO(BaseModule):
|
|||||||
self.backend.update_devicelist(self._own_jid, [self.backend.own_device])
|
self.backend.update_devicelist(self._own_jid, [self.backend.own_device])
|
||||||
self.set_devicelist()
|
self.set_devicelist()
|
||||||
|
|
||||||
def request_devicelist(self, jid=None, fetch_bundle=False):
|
def request_devicelist(self, jid=None):
|
||||||
|
if jid is None:
|
||||||
|
jid = self._own_jid
|
||||||
|
|
||||||
if jid in self._query_for_devicelists:
|
if jid in self._query_for_devicelists:
|
||||||
return
|
return
|
||||||
|
|
||||||
self._nbxmpp('OMEMO').request_devicelist(
|
self._nbxmpp('OMEMO').request_devicelist(
|
||||||
jid,
|
jid,
|
||||||
callback=self._devicelist_received,
|
callback=self._devicelist_received,
|
||||||
user_data=(jid, fetch_bundle))
|
user_data=jid)
|
||||||
self._query_for_devicelists.append(jid)
|
self._query_for_devicelists.append(jid)
|
||||||
|
|
||||||
def _devicelist_received(self, devicelist, user_data):
|
def _devicelist_received(self, devicelist, jid):
|
||||||
jid, fetch_bundle = user_data
|
|
||||||
if is_error_result(devicelist):
|
if is_error_result(devicelist):
|
||||||
log.info('%s => Devicelist request failed: %s %s',
|
log.info('%s => Devicelist request failed: %s %s',
|
||||||
self._account, jid, devicelist)
|
self._account, jid, devicelist)
|
||||||
devicelist = []
|
devicelist = []
|
||||||
|
|
||||||
self._process_devicelist_update(jid, devicelist, fetch_bundle)
|
self._process_devicelist_update(jid, devicelist)
|
||||||
|
|
||||||
@event_node(nbxmpp.NS_OMEMO_TEMP_DL)
|
@event_node(nbxmpp.NS_OMEMO_TEMP_DL)
|
||||||
def _devicelist_notification_received(self, _con, _stanza, properties):
|
def _devicelist_notification_received(self, _con, _stanza, properties):
|
||||||
@@ -465,9 +479,9 @@ class OMEMO(BaseModule):
|
|||||||
if not properties.pubsub_event.empty:
|
if not properties.pubsub_event.empty:
|
||||||
devicelist = properties.pubsub_event.data
|
devicelist = properties.pubsub_event.data
|
||||||
|
|
||||||
self._process_devicelist_update(str(properties.jid), devicelist, False)
|
self._process_devicelist_update(str(properties.jid), devicelist)
|
||||||
|
|
||||||
def _process_devicelist_update(self, jid, devicelist, fetch_bundle):
|
def _process_devicelist_update(self, jid, devicelist):
|
||||||
own_devices = jid is None or self._con.get_own_jid().bareMatch(jid)
|
own_devices = jid is None or self._con.get_own_jid().bareMatch(jid)
|
||||||
if own_devices:
|
if own_devices:
|
||||||
jid = self._own_jid
|
jid = self._own_jid
|
||||||
@@ -485,8 +499,7 @@ class OMEMO(BaseModule):
|
|||||||
# overwritten by some other client
|
# overwritten by some other client
|
||||||
self.set_devicelist()
|
self.set_devicelist()
|
||||||
|
|
||||||
elif fetch_bundle:
|
self._check_for_missing_sessions(jid)
|
||||||
self.are_keys_missing(jid)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _debug_print_stanza(stanza):
|
def _debug_print_stanza(stanza):
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ if not ERROR_MSG:
|
|||||||
class UserMessages(IntEnum):
|
class UserMessages(IntEnum):
|
||||||
QUERY_DEVICES = 0
|
QUERY_DEVICES = 0
|
||||||
NO_FINGERPRINTS = 1
|
NO_FINGERPRINTS = 1
|
||||||
|
UNDECIDED_FINGERPRINTS = 2
|
||||||
|
|
||||||
|
|
||||||
class OmemoPlugin(GajimPlugin):
|
class OmemoPlugin(GajimPlugin):
|
||||||
@@ -258,16 +259,16 @@ class OmemoPlugin(GajimPlugin):
|
|||||||
self.print_message(chat_control, UserMessages.NO_FINGERPRINTS)
|
self.print_message(chat_control, UserMessages.NO_FINGERPRINTS)
|
||||||
else:
|
else:
|
||||||
# check if we have devices for the contact
|
# check if we have devices for the contact
|
||||||
if not omemo.backend.get_devices(contact.jid):
|
if not omemo.backend.get_devices(contact.jid, without_self=True):
|
||||||
omemo.request_devicelist(contact.jid, True)
|
omemo.request_devicelist(contact.jid, True)
|
||||||
self.print_message(chat_control, UserMessages.QUERY_DEVICES)
|
self.print_message(chat_control, UserMessages.QUERY_DEVICES)
|
||||||
chat_control.sendmessage = False
|
chat_control.sendmessage = False
|
||||||
return
|
return
|
||||||
# check if bundles are missing for some devices
|
# check if bundles are missing for some devices
|
||||||
if omemo.are_keys_missing(contact.jid):
|
if omemo.backend.storage.hasUndecidedFingerprints(contact.jid):
|
||||||
log.info('%s => No Trusted Fingerprints for %s',
|
log.info('%s => Undecided Fingerprints for %s',
|
||||||
account, contact.jid)
|
account, contact.jid)
|
||||||
self.print_message(chat_control, UserMessages.NO_FINGERPRINTS)
|
self.print_message(chat_control, UserMessages.UNDECIDED_FINGERPRINTS)
|
||||||
chat_control.sendmessage = False
|
chat_control.sendmessage = False
|
||||||
else:
|
else:
|
||||||
log.debug('%s => Sending Message to %s',
|
log.debug('%s => Sending Message to %s',
|
||||||
@@ -323,6 +324,8 @@ class OmemoPlugin(GajimPlugin):
|
|||||||
elif kind == UserMessages.NO_FINGERPRINTS:
|
elif kind == UserMessages.NO_FINGERPRINTS:
|
||||||
msg = _('To send an encrypted message, you have to '
|
msg = _('To send an encrypted message, you have to '
|
||||||
'first trust the fingerprint of your contact!')
|
'first trust the fingerprint of your contact!')
|
||||||
|
elif kind == UserMessages.UNDECIDED_FINGERPRINTS:
|
||||||
|
msg = _('You have undecided fingerprints')
|
||||||
if msg is None:
|
if msg is None:
|
||||||
return
|
return
|
||||||
chat_control.print_conversation_line(msg, 'status', '', None)
|
chat_control.print_conversation_line(msg, 'status', '', None)
|
||||||
|
|||||||
Reference in New Issue
Block a user