annotate sat/memory/encryption.py @ 3916:40d47cc29ea4

plugin XEP-0300: type hints
author Goffi <goffi@goffi.org>
date Thu, 06 Oct 2022 15:19:08 +0200
parents 8289ac1b34f4
children cc2705225778
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
3911
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3479
diff changeset
22 from typing import Optional
3226
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
3911
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3479
diff changeset
26 from sat.core.core_types import EncryptionPlugin, EncryptionSession, MessageData
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
27 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
28 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
29 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
30 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
31 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
32 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
33 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
34
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
36 log = getLogger(__name__)
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
38
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
39 class EncryptionHandler:
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 """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
41 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
42
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
43 def __init__(self, client):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
44 self.client = client
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 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
46 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
47 "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
48
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
49 @property
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
50 def host(self):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
51 return self.client.host_app
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
52
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
53 async def loadSessions(self):
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
54 """Load persistent sessions"""
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
55 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
56 start_d_list = []
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
57 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
58 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
59 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
60
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
61 if start_d_list:
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
62 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
63 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
64 if not success:
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
65 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
66 log.warning(_(
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
67 "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
68 ).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
69 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
70
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 @classmethod
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
72 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
73 """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
74
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 @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
76 it must have the following methods:
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
77 - 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
78 entity(jid.JID): entity to manage
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
79 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
80 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
81 - startEncryption(entity): start encrypted session
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
82 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
83 - stopEncryption(entity): start encrypted session
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
84 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
85 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
86
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
87 @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
88 @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
89 @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
90 selected manually
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
91 @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
92 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
93 """
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
94 existing_ns = set()
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
95 existing_names = set()
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
96 for p in cls.plugins:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
97 existing_ns.add(p.namespace.lower())
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
98 existing_names.add(p.name.lower())
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
99 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
100 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
101 if name.lower() in existing_names:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
102 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
103 plugin = EncryptionPlugin(
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 instance=plg_instance,
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 name=name,
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 namespace=namespace,
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
107 priority=priority,
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
108 directed=directed)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
109 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
110 cls.plugins.sort(key=lambda p: p.priority)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
111 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
112
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
113 @classmethod
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
114 def getPlugins(cls):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
115 return cls.plugins
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
116
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
117 @classmethod
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
118 def getPlugin(cls, namespace):
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
119 try:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
120 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
121 except StopIteration:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
122 raise exceptions.NotFound(_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
123 "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
124 namespace=namespace))
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
125
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
126 @classmethod
2749
4b8271399f67 core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents: 2743
diff changeset
127 def getNamespaces(cls):
4b8271399f67 core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents: 2743
diff changeset
128 """Get available plugin namespaces"""
4b8271399f67 core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents: 2743
diff changeset
129 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
130
4b8271399f67 core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents: 2743
diff changeset
131 @classmethod
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
132 def getNSFromName(cls, name):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
133 """Retrieve plugin namespace from its name
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
134
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
135 @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
136 @return (unicode): namespace of the plugin
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
137 @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
138 """
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
139 for p in cls.plugins:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
140 if p.name.lower() == name.lower():
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
141 return p.namespace
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
142 raise exceptions.NotFound(_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
143 "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
144 name=name)))
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
145
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
146 def getBridgeData(self, session):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
147 """Retrieve session data serialized for bridge.
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
148
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
149 @param session(dict): encryption session
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
150 @return (unicode): serialized data for bridge
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
151 """
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
152 if session is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
153 return ''
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
154 plugin = session['plugin']
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
155 bridge_data = {'name': plugin.name,
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
156 'namespace': plugin.namespace}
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
157 if 'directed_devices' in session:
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
158 bridge_data['directed_devices'] = session['directed_devices']
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
159
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
160 return data_format.serialise(bridge_data)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
161
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
162 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
163 """Start encryption with a plugin
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
164
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
165 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
166 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
167 """
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
168 if not plugin.directed:
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
169 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
170 try:
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
171 start_encryption = plugin.instance.startEncryption
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
172 except AttributeError:
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
173 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
174 else:
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
175 # 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
176 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
177
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
178 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
179 """Stop encryption with a plugin
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
180
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
181 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
182 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
183 """
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
184 try:
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
185 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
186 except KeyError:
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
187 pass
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
188 try:
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
189 stop_encryption = plugin.instance.stopEncryption
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
190 except AttributeError:
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
191 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
192 else:
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
193 # 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
194 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
195
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
196 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
197 """Start an encryption session with an entity
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
198
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
199 @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
200 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
201 @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
202 to use.
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 None to select automatically an algorithm
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
204 @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
205 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
206 """
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 if not self.plugins:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
208 raise exceptions.NotFound(_("No encryption plugin is registered, "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
209 "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
210
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 if namespace is None:
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
212 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
213 else:
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
214 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
215
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
216 bare_jid = entity.userhostJID()
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
217 if bare_jid in self._sessions:
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
218 # we have already an encryption session with this contact
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
219 former_plugin = self._sessions[bare_jid]["plugin"]
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
220 if former_plugin.namespace == namespace:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
221 log.info(_("Session with {bare_jid} is already encrypted with {name}. "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
222 "Nothing to do.").format(
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
223 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
224 return
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
225
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
226 if replace:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
227 # there is a conflict, but replacement is requested
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
228 # 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
229 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
230 await self._stopEncryption(former_plugin, entity)
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
231 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
232 msg = (_("Session with {bare_jid} is already encrypted with {name}. "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
233 "Please stop encryption session before changing algorithm.")
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
234 .format(bare_jid=bare_jid, name=plugin.name))
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
235 log.warning(msg)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
236 raise exceptions.ConflictError(msg)
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
237
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
238 data = {"plugin": plugin}
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
239 if plugin.directed:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
240 if not entity.resource:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
241 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
242 if not entity.resource:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
243 raise exceptions.NotFound(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
244 _("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
245 .format(destinee=entity.full(), name=plugin.name))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
246 log.info(_("No resource specified to encrypt with {name}, using "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
247 "{destinee}.").format(destinee=entity.full(),
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
248 name=plugin.name))
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 # indicate that we encrypt only for some devices
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
250 directed_devices = data['directed_devices'] = [entity.resource]
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
251 elif entity.resource:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
252 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
253
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
254 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
255 self._sessions[entity.userhostJID()] = data
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
256 log.info(_("Encryption session has been set for {entity_jid} with "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
257 "{encryption_name}").format(
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
258 entity_jid=entity.full(), encryption_name=plugin.name))
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
259 self.host.bridge.messageEncryptionStarted(
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
260 entity.full(),
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
261 self.getBridgeData(data),
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
262 self.client.profile)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
263 msg = D_("Encryption session started: your messages with {destinee} are "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
264 "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
265 destinee=entity.full(), name=plugin.name)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
266 directed_devices = data.get('directed_devices')
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
267 if directed_devices:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
268 msg += "\n" + D_("Message are encrypted only for {nb_devices} device(s): "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
269 "{devices_list}.").format(
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
270 nb_devices=len(directed_devices),
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
271 devices_list = ', '.join(directed_devices))
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
272
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
273 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
274
3226
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
275 async def stop(self, entity, namespace=None):
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
276 """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
277
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
278 @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
279 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
280 @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
281 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
282 """
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
283 session = self.getSession(entity.userhostJID())
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
284 if not session:
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
285 raise failure.Failure(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
286 exceptions.NotFound(_("There is no encryption session with this "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
287 "entity.")))
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
288 plugin = session['plugin']
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
289 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
290 raise exceptions.InternalError(_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
291 "The encryption session is not run with the expected plugin: encrypted "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
292 "with {current_name} and was expecting {expected_name}").format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
293 current_name=session['plugin'].namespace,
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
294 expected_name=namespace))
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
295 if entity.resource:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
296 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
297 directed_devices = session['directed_devices']
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
298 except KeyError:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
299 raise exceptions.NotFound(_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
300 "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
301 "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
302 "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
303
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
304 try:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
305 directed_devices.remove(entity.resource)
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
306 except ValueError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
307 raise exceptions.NotFound(_("There is no directed session with this "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
308 "entity."))
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
309 else:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
310 if not directed_devices:
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
311 # 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
312 # we stop the whole session
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
313 # 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
314 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
315 await self._stopEncryption(plugin, entity)
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
316 else:
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
317 # 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
318 # 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
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
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
322 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
323 entity=entity.full()))
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
324 self.host.bridge.messageEncryptionStopped(
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
325 entity.full(),
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
326 {'name': plugin.name,
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
327 'namespace': plugin.namespace,
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
328 },
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
329 self.client.profile)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
330 msg = D_("Encryption session finished: your messages with {destinee} are "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
331 "NOT end to end encrypted anymore.\nYour server administrators or "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
332 "{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
333 destinee=entity.full())
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
334
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
335 self.client.feedback(entity, msg)
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
336
3911
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3479
diff changeset
337 def getSession(self, entity: jid.JID) -> Optional[EncryptionSession]:
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
338 """Get encryption session for this contact
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
339
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
340 @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
341 must be a bare jid
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
342 @return (dict, None): encryption session data
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
343 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
344 """
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
345 if entity.resource:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
346 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
347 return self._sessions.get(entity)
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
348
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
349 def getTrustUI(self, entity_jid, namespace=None):
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
350 """Retrieve encryption UI
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
351
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
352 @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
353 must be a bare jid
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
354 @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
355 if None use current algorithm
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
356 @return D(xmlui): XMLUI for trust management
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
357 the xmlui is a form
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
358 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
359 @raise exceptions.NotFound: no algorithm/plugin found
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
360 @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
361 """
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
362 if namespace is None:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
363 session = self.getSession(entity_jid)
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
364 if not session:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
365 raise exceptions.NotFound(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
366 "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
367 .format(entity_jid=entity_jid.full()))
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
368 plugin = session['plugin']
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
369 else:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
370 plugin = self.getPlugin(namespace)
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
371 try:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
372 get_trust_ui = plugin.instance.getTrustUI
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
373 except AttributeError:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
374 raise NotImplementedError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
375 "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
376 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
377 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
378
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
379 ## 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
380
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
381 @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
382 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
383 host.importMenu(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
384 (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
385 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
386 security_limit=0,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
387 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
388 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
389 )
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 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
391 host.importMenu(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
392 (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
393 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
394 security_limit=0,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
395 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
396 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
397 )
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 host.importMenu(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
399 (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
400 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
401 security_limit=0,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
402 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
403 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
404 )
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
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 @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
407 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
408 client = host.getClient(profile)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
409 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
410 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
411 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
412 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
413
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
414 @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
415 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
416 client = host.getClient(profile)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
417 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
418 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
419 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
420 d = defer.ensureDeferred(
2f406b762788 core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents: 3217
diff changeset
421 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
422 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
423 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
424
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
425 @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
426 @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
427 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
428 client = host.getClient(profile)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
429 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
430 ui = yield client.encryption.getTrustUI(peer_jid, plg.namespace)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2810
diff changeset
431 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
432
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
433 ## Triggers ##
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
434
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
435 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
436 """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
437 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
438 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
439 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
440 plugin = encryption['plugin']
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
441 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
442 raise exceptions.InternalError(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
443 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
444 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
445 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
446 self.markAsEncrypted(mess_data, plugin.namespace)
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
447
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
448 ## Misc ##
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
449
3231
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
450 def markAsEncrypted(self, mess_data, namespace):
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
451 """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
452
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
453 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
454 the plugin
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
455 @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
456 @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
457 """
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
458 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
459 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
460 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
461 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
462 if session is None:
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
463 # 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
464 # 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
465 log.info(_(
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
466 "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
467 "messages")
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
468 .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
469 )
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3228
diff changeset
470 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
471
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
472 return mess_data
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
473
3911
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3479
diff changeset
474 def isEncryptionRequested(
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3479
diff changeset
475 self,
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3479
diff changeset
476 mess_data: MessageData,
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3479
diff changeset
477 namespace: Optional[str] = None
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3479
diff changeset
478 ) -> bool:
3180
826bca181226 core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents: 3171
diff changeset
479 """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
480
3911
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3479
diff changeset
481 @param mess_data: message data for outgoing message
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3479
diff changeset
482 @param namespace: if set, check if encryption is requested for the algorithm
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3479
diff changeset
483 specified
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3479
diff changeset
484 @return: True if the encryption flag is present
3180
826bca181226 core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents: 3171
diff changeset
485 """
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
486 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
487 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
488 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
489 # 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
490 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
491 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
492 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
493 return plugin.namespace == namespace
3180
826bca181226 core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents: 3171
diff changeset
494
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
495 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
496 """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
497
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 @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
499 @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
500 """
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
501 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
502
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
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
504 def markAsTrusted(self, mess_data):
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
505 """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
506
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
507 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
508 the plugin
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
509 @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
510 """
2752
1fa615faec8b core (constants): added a constant for "encrypted"
Goffi <goffi@goffi.org>
parents: 2749
diff changeset
511 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
512 return mess_data
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
513
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
514 def markAsUntrusted(self, mess_data):
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
515 """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
516
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
517 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
518 the plugin
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
519 @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
520 """
2743
da59ff099b32 core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents: 2733
diff changeset
521 mess_data['trusted'] = False
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
522 return mess_data