annotate sat/memory/encryption.py @ 3652:6e34307319c0

plugin XEP-0353: fix jingle initiation on disco "Service Unavailable" error: When requesting disco info on a bare jid which is not in our roster, server may return "Service Unavailable" (to avoid leaking valid JIDs). In this case, the initiation was failing, this is now fixed by using empty categories in this case.
author Goffi <goffi@goffi.org>
date Wed, 08 Sep 2021 11:16:52 +0200
parents be6d91572633
children 8289ac1b34f4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT: a jabber client
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3235
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
20 import copy
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
21 from functools import partial
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
22 from collections import namedtuple
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
23 from twisted.words.protocols.jabber import jid
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
24 from twisted.internet import defer
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
25 from twisted.python import failure
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
26 from sat.core.i18n import D_, _
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.core.constants import Const as C
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.core import exceptions
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from sat.core.log import getLogger
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
30 from sat.tools.common import data_format
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
31 from sat.tools import utils
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
32 from sat.memory import persistent
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
33
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
35 log = getLogger(__name__)
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 EncryptionPlugin = namedtuple("EncryptionPlugin", ("instance",
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 "name",
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 "namespace",
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
40 "priority",
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
41 "directed"))
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
44 class EncryptionHandler:
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 """Class to handle encryption sessions for a client"""
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 plugins = [] # plugin able to encrypt messages
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
48 def __init__(self, client):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
49 self.client = client
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self._sessions = {} # bare_jid ==> encryption_data
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
51 self._stored_session = persistent.PersistentDict(
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
52 "core:encryption", profile=client.profile)
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
54 @property
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
55 def host(self):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
56 return self.client.host_app
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
57
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
58 async def loadSessions(self):
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
59 """Load persistent sessions"""
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
60 await self._stored_session.load()
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
61 start_d_list = []
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
62 for entity_jid_s, namespace in self._stored_session.items():
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
63 entity = jid.JID(entity_jid_s)
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
64 start_d_list.append(defer.ensureDeferred(self.start(entity, namespace)))
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
65
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
66 if start_d_list:
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
67 result = await defer.DeferredList(start_d_list)
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
68 for idx, (success, err) in enumerate(result):
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
69 if not success:
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
70 entity_jid_s, namespace = list(self._stored_session.items())[idx]
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
71 log.warning(_(
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
72 "Could not restart {namespace!r} encryption with {entity}: {err}"
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
73 ).format(namespace=namespace, entity=entity_jid_s, err=err))
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
74 log.info(_("encryption sessions restored"))
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
75
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 @classmethod
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
77 def registerPlugin(cls, plg_instance, name, namespace, priority=0, directed=False):
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 """Register a plugin handling an encryption algorithm
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 @param plg_instance(object): instance of the plugin
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 it must have the following methods:
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
82 - getTrustUI(entity): return a XMLUI for trust management
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
83 entity(jid.JID): entity to manage
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
84 The returned XMLUI must be a form
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
85 if may have the following methods:
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
86 - startEncryption(entity): start encrypted session
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
87 entity(jid.JID): entity to start encrypted session with
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
88 - stopEncryption(entity): start encrypted session
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
89 entity(jid.JID): entity to stop encrypted session with
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
90 if they don't exists, those 2 methods will be ignored.
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
91
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
92 @param name(unicode): human readable name of the encryption algorithm
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 @param namespace(unicode): namespace of the encryption algorithm
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 @param priority(int): priority of this plugin to encrypt an message when not
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 selected manually
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
96 @param directed(bool): True if this plugin is directed (if it works with one
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
97 device only at a time)
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 """
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
99 existing_ns = set()
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
100 existing_names = set()
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
101 for p in cls.plugins:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
102 existing_ns.add(p.namespace.lower())
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
103 existing_names.add(p.name.lower())
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
104 if namespace.lower() in existing_ns:
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 raise exceptions.ConflictError("A plugin with this namespace already exists!")
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
106 if name.lower() in existing_names:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
107 raise exceptions.ConflictError("A plugin with this name already exists!")
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
108 plugin = EncryptionPlugin(
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 instance=plg_instance,
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 name=name,
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 namespace=namespace,
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
112 priority=priority,
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
113 directed=directed)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
114 cls.plugins.append(plugin)
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 cls.plugins.sort(key=lambda p: p.priority)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
116 log.info(_("Encryption plugin registered: {name}").format(name=name))
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
117
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
118 @classmethod
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
119 def getPlugins(cls):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
120 return cls.plugins
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
121
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
122 @classmethod
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
123 def getPlugin(cls, namespace):
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
124 try:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
125 return next(p for p in cls.plugins if p.namespace == namespace)
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
126 except StopIteration:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
127 raise exceptions.NotFound(_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
128 "Can't find requested encryption plugin: {namespace}").format(
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
129 namespace=namespace))
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
130
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
131 @classmethod
2749
4b8271399f67 core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents: 2743
diff changeset
132 def getNamespaces(cls):
4b8271399f67 core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents: 2743
diff changeset
133 """Get available plugin namespaces"""
4b8271399f67 core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents: 2743
diff changeset
134 return {p.namespace for p in cls.getPlugins()}
4b8271399f67 core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents: 2743
diff changeset
135
4b8271399f67 core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents: 2743
diff changeset
136 @classmethod
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
137 def getNSFromName(cls, name):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
138 """Retrieve plugin namespace from its name
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
139
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
140 @param name(unicode): name of the plugin (case insensitive)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
141 @return (unicode): namespace of the plugin
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
142 @raise exceptions.NotFound: there is not encryption plugin of this name
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
143 """
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
144 for p in cls.plugins:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
145 if p.name.lower() == name.lower():
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
146 return p.namespace
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
147 raise exceptions.NotFound(_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
148 "Can't find a plugin with the name \"{name}\".".format(
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
149 name=name)))
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
150
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
151 def getBridgeData(self, session):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
152 """Retrieve session data serialized for bridge.
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
153
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
154 @param session(dict): encryption session
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
155 @return (unicode): serialized data for bridge
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
156 """
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
157 if session is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
158 return ''
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
159 plugin = session['plugin']
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
160 bridge_data = {'name': plugin.name,
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
161 'namespace': plugin.namespace}
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
162 if 'directed_devices' in session:
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
163 bridge_data['directed_devices'] = session['directed_devices']
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
164
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
165 return data_format.serialise(bridge_data)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
166
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
167 async def _startEncryption(self, plugin, entity):
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
168 """Start encryption with a plugin
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
169
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
170 This method must be called just before adding a plugin session.
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
171 StartEncryptionn method of plugin will be called if it exists.
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
172 """
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
173 if not plugin.directed:
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
174 await self._stored_session.aset(entity.userhost(), plugin.namespace)
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
175 try:
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
176 start_encryption = plugin.instance.startEncryption
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
177 except AttributeError:
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
178 log.debug(f"No startEncryption method found for {plugin.namespace}")
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
179 else:
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
180 # we copy entity to avoid having the resource changed by stop_encryption
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
181 await utils.asDeferred(start_encryption, self.client, copy.copy(entity))
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
182
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
183 async def _stopEncryption(self, plugin, entity):
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
184 """Stop encryption with a plugin
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
185
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
186 This method must be called just before removing a plugin session.
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
187 StopEncryptionn method of plugin will be called if it exists.
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
188 """
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
189 try:
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
190 await self._stored_session.adel(entity.userhost())
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
191 except KeyError:
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
192 pass
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
193 try:
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
194 stop_encryption = plugin.instance.stopEncryption
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
195 except AttributeError:
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
196 log.debug(f"No stopEncryption method found for {plugin.namespace}")
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
197 else:
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
198 # we copy entity to avoid having the resource changed by stop_encryption
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
199 return utils.asDeferred(stop_encryption, self.client, copy.copy(entity))
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
200
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
201 async def start(self, entity, namespace=None, replace=False):
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
202 """Start an encryption session with an entity
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
203
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
204 @param entity(jid.JID): entity to start an encryption session with
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 must be bare jid is the algorithm encrypt for all devices
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
206 @param namespace(unicode, None): namespace of the encryption algorithm
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
207 to use.
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 None to select automatically an algorithm
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
209 @param replace(bool): if True and an encrypted session already exists,
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
210 it will be replaced by the new one
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 """
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 if not self.plugins:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
213 raise exceptions.NotFound(_("No encryption plugin is registered, "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
214 "an encryption session can't be started"))
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
215
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 if namespace is None:
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
217 plugin = self.plugins[0]
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 else:
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
219 plugin = self.getPlugin(namespace)
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
220
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
221 bare_jid = entity.userhostJID()
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
222 if bare_jid in self._sessions:
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
223 # we have already an encryption session with this contact
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
224 former_plugin = self._sessions[bare_jid]["plugin"]
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
225 if former_plugin.namespace == namespace:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
226 log.info(_("Session with {bare_jid} is already encrypted with {name}. "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
227 "Nothing to do.").format(
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
228 bare_jid=bare_jid, name=former_plugin.name))
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
229 return
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
230
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
231 if replace:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
232 # there is a conflict, but replacement is requested
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
233 # so we stop previous encryption to use new one
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
234 del self._sessions[bare_jid]
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
235 await self._stopEncryption(former_plugin, entity)
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
236 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
237 msg = (_("Session with {bare_jid} is already encrypted with {name}. "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
238 "Please stop encryption session before changing algorithm.")
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
239 .format(bare_jid=bare_jid, name=plugin.name))
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
240 log.warning(msg)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
241 raise exceptions.ConflictError(msg)
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
242
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
243 data = {"plugin": plugin}
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
244 if plugin.directed:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
245 if not entity.resource:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
246 entity.resource = self.host.memory.getMainResource(self.client, entity)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
247 if not entity.resource:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
248 raise exceptions.NotFound(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
249 _("No resource found for {destinee}, can't encrypt with {name}")
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
250 .format(destinee=entity.full(), name=plugin.name))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
251 log.info(_("No resource specified to encrypt with {name}, using "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
252 "{destinee}.").format(destinee=entity.full(),
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
253 name=plugin.name))
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 # indicate that we encrypt only for some devices
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
255 directed_devices = data['directed_devices'] = [entity.resource]
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
256 elif entity.resource:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
257 raise ValueError(_("{name} encryption must be used with bare jids."))
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
258
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
259 await self._startEncryption(plugin, entity)
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 self._sessions[entity.userhostJID()] = data
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
261 log.info(_("Encryption session has been set for {entity_jid} with "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
262 "{encryption_name}").format(
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
263 entity_jid=entity.full(), encryption_name=plugin.name))
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
264 self.host.bridge.messageEncryptionStarted(
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
265 entity.full(),
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
266 self.getBridgeData(data),
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
267 self.client.profile)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
268 msg = D_("Encryption session started: your messages with {destinee} are "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
269 "now end to end encrypted using {name} algorithm.").format(
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
270 destinee=entity.full(), name=plugin.name)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
271 directed_devices = data.get('directed_devices')
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
272 if directed_devices:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
273 msg += "\n" + D_("Message are encrypted only for {nb_devices} device(s): "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
274 "{devices_list}.").format(
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
275 nb_devices=len(directed_devices),
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
276 devices_list = ', '.join(directed_devices))
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
277
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
278 self.client.feedback(bare_jid, msg)
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
279
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
280 async def stop(self, entity, namespace=None):
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
281 """Stop an encryption session with an entity
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
282
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
283 @param entity(jid.JID): entity with who the encryption session must be stopped
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
284 must be bare jid if the algorithm encrypt for all devices
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
285 @param namespace(unicode): namespace of the session to stop
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
286 when specified, used to check that we stop the right encryption session
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
287 """
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
288 session = self.getSession(entity.userhostJID())
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
289 if not session:
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
290 raise failure.Failure(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
291 exceptions.NotFound(_("There is no encryption session with this "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
292 "entity.")))
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
293 plugin = session['plugin']
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
294 if namespace is not None and plugin.namespace != namespace:
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
295 raise exceptions.InternalError(_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
296 "The encryption session is not run with the expected plugin: encrypted "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
297 "with {current_name} and was expecting {expected_name}").format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
298 current_name=session['plugin'].namespace,
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
299 expected_name=namespace))
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
300 if entity.resource:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
301 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
302 directed_devices = session['directed_devices']
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
303 except KeyError:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
304 raise exceptions.NotFound(_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
305 "There is a session for the whole entity (i.e. all devices of the "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
306 "entity), not a directed one. Please use bare jid if you want to "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
307 "stop the whole encryption with this entity."))
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
308
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
309 try:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
310 directed_devices.remove(entity.resource)
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
311 except ValueError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
312 raise exceptions.NotFound(_("There is no directed session with this "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
313 "entity."))
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
314 else:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
315 if not directed_devices:
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
316 # if we have no more directed device sessions,
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
317 # we stop the whole session
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
318 # see comment below for deleting session before stopping encryption
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
319 del self._sessions[entity.userhostJID()]
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
320 await self._stopEncryption(plugin, entity)
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
321 else:
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
322 # plugin's stopEncryption may call stop again (that's the case with OTR)
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
323 # so we need to remove plugin from session before calling self._stopEncryption
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
324 del self._sessions[entity.userhostJID()]
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
325 await self._stopEncryption(plugin, entity)
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
326
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
327 log.info(_("encryption session stopped with entity {entity}").format(
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
328 entity=entity.full()))
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
329 self.host.bridge.messageEncryptionStopped(
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
330 entity.full(),
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
331 {'name': plugin.name,
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
332 'namespace': plugin.namespace,
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
333 },
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
334 self.client.profile)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
335 msg = D_("Encryption session finished: your messages with {destinee} are "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
336 "NOT end to end encrypted anymore.\nYour server administrators or "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
337 "{destinee} server administrators will be able to read them.").format(
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
338 destinee=entity.full())
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
339
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
340 self.client.feedback(entity, msg)
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
341
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
342 def getSession(self, entity):
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
343 """Get encryption session for this contact
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
344
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
345 @param entity(jid.JID): get the session for this entity
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
346 must be a bare jid
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
347 @return (dict, None): encryption session data
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
348 None if there is not encryption for this session with this jid
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
349 """
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
350 if entity.resource:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
351 raise ValueError("Full jid given when expecting bare jid")
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
352 return self._sessions.get(entity)
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
353
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
354 def getTrustUI(self, entity_jid, namespace=None):
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
355 """Retrieve encryption UI
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
356
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
357 @param entity_jid(jid.JID): get the UI for this entity
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
358 must be a bare jid
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
359 @param namespace(unicode): namespace of the algorithm to manage
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
360 if None use current algorithm
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
361 @return D(xmlui): XMLUI for trust management
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
362 the xmlui is a form
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
363 None if there is not encryption for this session with this jid
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
364 @raise exceptions.NotFound: no algorithm/plugin found
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
365 @raise NotImplementedError: plugin doesn't handle UI management
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
366 """
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
367 if namespace is None:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
368 session = self.getSession(entity_jid)
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
369 if not session:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
370 raise exceptions.NotFound(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
371 "No encryption session currently active for {entity_jid}"
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
372 .format(entity_jid=entity_jid.full()))
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
373 plugin = session['plugin']
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
374 else:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
375 plugin = self.getPlugin(namespace)
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
376 try:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
377 get_trust_ui = plugin.instance.getTrustUI
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
378 except AttributeError:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
379 raise NotImplementedError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
380 "Encryption plugin doesn't handle trust management UI")
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
381 else:
3235
c161ff21ca7c core (memory/encryption): use utils.asDeferred for trust UI, so async coroutines can be used
Goffi <goffi@goffi.org>
parents: 3231
diff changeset
382 return utils.asDeferred(get_trust_ui, self.client, entity_jid)
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
383
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
384 ## Menus ##
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
385
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
386 @classmethod
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
387 def _importMenus(cls, host):
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
388 host.importMenu(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
389 (D_("Encryption"), D_("unencrypted (plain text)")),
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
390 partial(cls._onMenuUnencrypted, host=host),
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
391 security_limit=0,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
392 help_string=D_("End encrypted session"),
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
393 type_=C.MENU_SINGLE,
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
394 )
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
395 for plg in cls.getPlugins():
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
396 host.importMenu(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
397 (D_("Encryption"), plg.name),
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
398 partial(cls._onMenuName, host=host, plg=plg),
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
399 security_limit=0,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
400 help_string=D_("Start {name} session").format(name=plg.name),
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
401 type_=C.MENU_SINGLE,
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
402 )
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
403 host.importMenu(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
404 (D_("Encryption"), D_("⛨ {name} trust").format(name=plg.name)),
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
405 partial(cls._onMenuTrust, host=host, plg=plg),
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
406 security_limit=0,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
407 help_string=D_("Manage {name} trust").format(name=plg.name),
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
408 type_=C.MENU_SINGLE,
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
409 )
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
410
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
411 @classmethod
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
412 def _onMenuUnencrypted(cls, data, host, profile):
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
413 client = host.getClient(profile)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
414 peer_jid = jid.JID(data['jid']).userhostJID()
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
415 d = defer.ensureDeferred(client.encryption.stop(peer_jid))
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
416 d.addCallback(lambda __: {})
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
417 return d
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
418
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
419 @classmethod
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
420 def _onMenuName(cls, data, host, plg, profile):
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
421 client = host.getClient(profile)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
422 peer_jid = jid.JID(data['jid'])
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
423 if not plg.directed:
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
424 peer_jid = peer_jid.userhostJID()
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
425 d = defer.ensureDeferred(
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
426 client.encryption.start(peer_jid, plg.namespace, replace=True))
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
427 d.addCallback(lambda __: {})
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
428 return d
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
429
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
430 @classmethod
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
431 @defer.inlineCallbacks
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
432 def _onMenuTrust(cls, data, host, plg, profile):
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
433 client = host.getClient(profile)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
434 peer_jid = jid.JID(data['jid']).userhostJID()
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
435 ui = yield client.encryption.getTrustUI(peer_jid, plg.namespace)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
436 defer.returnValue({'xmlui': ui.toXml()})
2810
c161a294fffd core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
437
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
438 ## Triggers ##
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
439
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
440 def setEncryptionFlag(self, mess_data):
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
441 """Set "encryption" key in mess_data if session with destinee is encrypted"""
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
442 to_jid = mess_data['to']
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
443 encryption = self._sessions.get(to_jid.userhostJID())
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
444 if encryption is not None:
3231
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
445 plugin = encryption['plugin']
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
446 if mess_data["type"] == "groupchat" and plugin.directed:
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
447 raise exceptions.InternalError(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
448 f"encryption flag must not be set for groupchat if encryption algorithm "
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
449 f"({encryption['plugin'].name}) is directed!")
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
450 mess_data[C.MESS_KEY_ENCRYPTION] = encryption
3231
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
451 self.markAsEncrypted(mess_data, plugin.namespace)
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
452
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
453 ## Misc ##
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
454
3231
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
455 def markAsEncrypted(self, mess_data, namespace):
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
456 """Helper method to mark a message as having been e2e encrypted.
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
457
3171
d073d82d9044 core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
458 This should be used in the post_treat workflow of messageReceived trigger of
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
459 the plugin
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
460 @param mess_data(dict): message data as used in post treat workflow
3231
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
461 @param namespace(str): namespace of the algorithm used for encrypting the message
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
462 """
3228
cc3fea71c365 core (memory/encryption): set encrypted flag also for outgoing messages and put it in extra:
Goffi <goffi@goffi.org>
parents: 3226
diff changeset
463 mess_data['extra'][C.MESS_KEY_ENCRYPTED] = True
3231
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
464 from_bare_jid = mess_data['from'].userhostJID()
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
465 if from_bare_jid != self.client.jid.userhostJID():
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
466 session = self.getSession(from_bare_jid)
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
467 if session is None:
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
468 # if we are currently unencrypted, we start a session automatically
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
469 # to avoid sending unencrypted messages in an encrypted context
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
470 log.info(_(
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
471 "Starting e2e session with {peer_jid} as we receive encrypted "
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
472 "messages")
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
473 .format(peer_jid=from_bare_jid)
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
474 )
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
475 defer.ensureDeferred(self.start(from_bare_jid, namespace))
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
476
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
477 return mess_data
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
478
3217
0469c53ed5dd core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents: 3180
diff changeset
479 def isEncryptionRequested(self, mess_data, namespace=None):
3180
826bca181226 core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents: 3171
diff changeset
480 """Helper method to check if encryption is requested in an outgoind message
826bca181226 core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents: 3171
diff changeset
481
826bca181226 core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents: 3171
diff changeset
482 @param mess_data(dict): message data for outgoing message
3217
0469c53ed5dd core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents: 3180
diff changeset
483 @param namespace(str, None): if set, check if encryption is requested for the
0469c53ed5dd core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents: 3180
diff changeset
484 algorithm specified
3180
826bca181226 core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents: 3171
diff changeset
485 @return (bool): True if the encryption flag is present
826bca181226 core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents: 3171
diff changeset
486 """
3217
0469c53ed5dd core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents: 3180
diff changeset
487 encryption = mess_data.get(C.MESS_KEY_ENCRYPTION)
0469c53ed5dd core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents: 3180
diff changeset
488 if encryption is None:
0469c53ed5dd core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents: 3180
diff changeset
489 return False
0469c53ed5dd core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents: 3180
diff changeset
490 # we get plugin even if namespace is None to be sure that the key exists
0469c53ed5dd core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents: 3180
diff changeset
491 plugin = encryption['plugin']
0469c53ed5dd core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents: 3180
diff changeset
492 if namespace is None:
0469c53ed5dd core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents: 3180
diff changeset
493 return True
0469c53ed5dd core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents: 3180
diff changeset
494 return plugin.namespace == namespace
3180
826bca181226 core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents: 3171
diff changeset
495
3171
d073d82d9044 core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
496 def isEncrypted(self, mess_data):
3228
cc3fea71c365 core (memory/encryption): set encrypted flag also for outgoing messages and put it in extra:
Goffi <goffi@goffi.org>
parents: 3226
diff changeset
497 """Helper method to check if a message has the e2e encrypted flag
3171
d073d82d9044 core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
498
d073d82d9044 core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
499 @param mess_data(dict): message data
d073d82d9044 core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
500 @return (bool): True if the encrypted flag is present
d073d82d9044 core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
501 """
3228
cc3fea71c365 core (memory/encryption): set encrypted flag also for outgoing messages and put it in extra:
Goffi <goffi@goffi.org>
parents: 3226
diff changeset
502 return mess_data['extra'].get(C.MESS_KEY_ENCRYPTED, False)
3171
d073d82d9044 core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
503
d073d82d9044 core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
504
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
505 def markAsTrusted(self, mess_data):
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
506 """Helper methor to mark a message as sent from a trusted entity.
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
507
3171
d073d82d9044 core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
508 This should be used in the post_treat workflow of messageReceived trigger of
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
509 the plugin
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
510 @param mess_data(dict): message data as used in post treat workflow
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
511 """
2752
1fa615faec8b core (constants): added a constant for "encrypted"
Goffi <goffi@goffi.org>
parents: 2749
diff changeset
512 mess_data[C.MESS_KEY_TRUSTED] = True
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
513 return mess_data
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
514
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
515 def markAsUntrusted(self, mess_data):
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
516 """Helper methor to mark a message as sent from an untrusted entity.
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
517
3171
d073d82d9044 core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
518 This should be used in the post_treat workflow of messageReceived trigger of
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
519 the plugin
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
520 @param mess_data(dict): message data as used in post treat workflow
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
521 """
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
522 mess_data['trusted'] = False
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
523 return mess_data