annotate sat/memory/encryption.py @ 2734:9702025f6dff

core (memory/persistent): new LazyPersistentBinaryDict.remove method
author Goffi <goffi@goffi.org>
date Wed, 02 Jan 2019 18:24:14 +0100
parents e347e32aa07f
children da59ff099b32
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
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
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
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
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
20 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
21 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
22 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
23 from collections import namedtuple
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core.log import getLogger
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 log = getLogger(__name__)
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
26 from sat.tools.common import data_format
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 EncryptionPlugin = namedtuple("EncryptionPlugin", ("instance",
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 "name",
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 "namespace",
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
32 "priority",
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
33 "directed"))
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 class EncryptionHandler(object):
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 """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
38 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
39
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
40 def __init__(self, client):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
41 self.client = client
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self._sessions = {} # bare_jid ==> encryption_data
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
44 @property
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
45 def host(self):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
46 return self.client.host_app
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
47
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 @classmethod
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
49 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
50 """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
51
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 @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
53 it must have the following methods:
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
54 - 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
55 entity(jid.JID): entity to manage
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
56 The returned XMLUI must be a form
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
57
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
58 @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
59 @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
60 @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
61 selected manually
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
62 @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
63 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
64 """
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
65 existing_ns = set()
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
66 existing_names = set()
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
67 for p in cls.plugins:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
68 existing_ns.add(p.namespace.lower())
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
69 existing_names.add(p.name.lower())
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
70 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
71 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
72 if name.lower() in existing_names:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
73 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
74 plugin = EncryptionPlugin(
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 instance=plg_instance,
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 name=name,
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 namespace=namespace,
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
78 priority=priority,
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
79 directed=directed)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
80 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
81 cls.plugins.sort(key=lambda p: p.priority)
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
82 log.info(_(u"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
83
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
84 @classmethod
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
85 def getPlugins(cls):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
86 return cls.plugins
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
87
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
88 @classmethod
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
89 def getPlugin(cls, namespace):
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
90 try:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
91 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
92 except StopIteration:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
93 raise exceptions.NotFound(_(
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
94 u"Can't find requested encryption plugin: {namespace}").format(
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
95 namespace=namespace))
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
96
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
97 @classmethod
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
98 def getNSFromName(cls, name):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
99 """Retrieve plugin namespace from its name
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
100
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
101 @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
102 @return (unicode): namespace of the plugin
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
103 @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
104 """
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
105 for p in cls.plugins:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
106 if p.name.lower() == name.lower():
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
107 return p.namespace
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
108 raise exceptions.NotFound(_(
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
109 u"Can't find a plugin with the name \"{name}\".".format(
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
110 name=name)))
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
111
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
112 def getBridgeData(self, session):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
113 """Retrieve session data serialized for bridge.
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
114
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
115 @param session(dict): encryption session
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
116 @return (unicode): serialized data for bridge
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
117 """
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
118 if session is None:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
119 return u''
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
120 plugin = session[u'plugin']
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
121 bridge_data = {'name': plugin.name,
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
122 'namespace': plugin.namespace}
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
123 if u'directed_devices' in session:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
124 bridge_data[u'directed_devices'] = session[u'directed_devices']
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
125
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
126 return data_format.serialise(bridge_data)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
127
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
128 def start(self, entity, namespace=None, replace=False):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
129 """Start an encryption session with an entity
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
130
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
131 @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
132 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
133 @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
134 to use.
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 None to select automatically an algorithm
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
136 @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
137 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
138 """
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 if not self.plugins:
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 raise exceptions.NotFound(_(u"No encryption plugin is registered, "
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 u"an encryption session can't be started"))
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
142
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 if namespace is None:
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
144 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
145 else:
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
146 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
147
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
148 bare_jid = entity.userhostJID()
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
149 if bare_jid in self._sessions:
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
150 # we have already an encryption session with this contact
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
151 former_plugin = self._sessions[bare_jid]['plugin']
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
152 if former_plugin.namespace == namespace:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
153 log.info(_(u"Session with {bare_jid} is already encrypted with {name}. "
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
154 u"Nothing to do.").format(
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
155 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
156 return
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
157
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
158 if replace:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
159 # there is a conflict, but replacement is requested
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
160 # 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
161 del self._sessions[bare_jid]
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
162 else:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
163 msg = (_(u"Session with {bare_jid} is already encrypted with {name}. "
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
164 u"Please stop encryption session before changing algorithm.")
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
165 .format(bare_jid=bare_jid, name=plugin.name))
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
166 log.warning(msg)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
167 raise exceptions.ConflictError(msg)
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
168
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
169 data = {"plugin": plugin}
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
170 if plugin.directed:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
171 if not entity.resource:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
172 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
173 if not entity.resource:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
174 raise exceptions.NotFound(
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
175 _(u"No resource found for {destinee}, can't encrypt with {name}")
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
176 .format(destinee=entity.full(), name=plugin.name))
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
177 log.info(_(u"No resource specified to encrypt with {name}, using "
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
178 u"{destinee}.").format(destinee=entity.full(),
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
179 name=plugin.name))
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 # indicate that we encrypt only for some devices
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
181 directed_devices = data[u'directed_devices'] = [entity.resource]
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
182 elif entity.resource:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
183 raise ValueError(_(u"{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
184
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 self._sessions[entity.userhostJID()] = data
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
186 log.info(_(u"Encryption session has been set for {entity_jid} with "
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 u"{encryption_name}").format(
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
188 entity_jid=entity.full(), encryption_name=plugin.name))
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
189 self.host.bridge.messageEncryptionStarted(
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
190 entity.full(),
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
191 self.getBridgeData(data),
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
192 self.client.profile)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
193 msg = D_(u"Encryption session started: your messages with {destinee} are "
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
194 u"now end to end encrypted using {name} algorithm.").format(
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
195 destinee=entity.full(), name=plugin.name)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
196 directed_devices = data.get(u'directed_devices')
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
197 if directed_devices:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
198 msg += u"\n" + D_(u"Message are encrypted only for {nb_devices} device(s): "
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
199 u"{devices_list}.").format(
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
200 nb_devices=len(directed_devices),
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
201 devices_list = u', '.join(directed_devices))
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
202
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
203 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
204
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
205 def stop(self, entity, namespace=None):
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
206 """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
207
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
208 @param entity(jid.JID): entity with who the encryption session must be stopped
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
209 must be bare jid is the algorithm encrypt for all devices
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
210 @param namespace(unicode): namespace of the session to stop
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
211 when specified, used to check we stop the right encryption session
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
212 """
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
213 session = self.getSession(entity.userhostJID())
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
214 if not session:
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
215 raise exceptions.NotFound(_(u"There is no encryption session with this "
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
216 u"entity."))
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
217 plugin = session['plugin']
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
218 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
219 raise exceptions.InternalError(_(
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
220 u"The encryption session is not run with the expected plugin: encrypted "
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
221 u"with {current_name} and was expecting {expected_name}").format(
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
222 current_name=session[u'plugin'].namespace,
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
223 expected_name=namespace))
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
224 if entity.resource:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
225 try:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
226 directed_devices = session[u'directed_devices']
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
227 except KeyError:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
228 raise exceptions.NotFound(_(
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
229 u"There is a session for the whole entity (i.e. all devices of the "
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
230 u"entity), not a directed one. Please use bare jid if you want to "
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
231 u"stop the whole encryption with this entity."))
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
232
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
233 try:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
234 directed_devices.remove(entity.resource)
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
235 except ValueError:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
236 raise exceptions.NotFound(_(u"There is no directed session with this "
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
237 u"entity."))
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
238 else:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
239 if not directed_devices:
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
240 del session[u'directed_devices']
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
241 else:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
242 del self._sessions[entity]
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
243
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
244 log.info(_(u"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
245 entity=entity.full()))
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
246 self.host.bridge.messageEncryptionStopped(
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
247 entity.full(),
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
248 {'name': plugin.name,
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
249 'namespace': plugin.namespace,
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
250 },
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
251 self.client.profile)
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
252 msg = D_(u"Encryption session finished: your messages with {destinee} are "
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
253 u"NOT end to end encrypted anymore.\nYour server administrators or "
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
254 u"{destinee} server administrators will be able to read them.").format(
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
255 destinee=entity.full())
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
256
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
257 self.client.feedback(entity, msg)
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
258
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
259 def getSession(self, entity):
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
260 """Get encryption session for this contact
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
261
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
262 @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
263 must be a bare jid
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
264 @return (dict, None): encryption session data
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
265 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
266 """
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
267 if entity.resource:
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
268 raise ValueError(u"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
269 return self._sessions.get(entity)
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
270
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
271 def getTrustUI(self, entity_jid, namespace=None):
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
272 """Retrieve encryption UI
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
273
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
274 @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
275 must be a bare jid
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
276 @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
277 if None use current algorithm
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
278 @return D(xmlui): XMLUI for trust management
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
279 the xmlui is a form
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
280 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
281 @raise exceptions.NotFound: no algorithm/plugin found
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
282 @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
283 """
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
284 if namespace is None:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
285 session = self.getSession(entity_jid)
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
286 if not session:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
287 raise exceptions.NotFound(
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
288 u"No encryption session currently active for {entity_jid}"
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
289 .format(entity_jid=entity_jid.full()))
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
290 plugin = session['plugin']
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
291 else:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
292 plugin = self.getPlugin(namespace)
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
293 try:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
294 get_trust_ui = plugin.instance.getTrustUI
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
295 except AttributeError:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
296 raise NotImplementedError(
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
297 u"Encryption plugin doesn't handle trust management UI")
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
298 else:
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
299 return get_trust_ui(self.client, entity_jid)
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
300
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 ## Triggers ##
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
302
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 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
304 """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
305
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
306 if mess_data["type"] == "groupchat":
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 # FIXME: to change when group chat encryption will be handled
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 return
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
309
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
310 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
311 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
312 if encryption is not None:
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 mess_data[C.MESS_KEY_ENCRYPTION] = encryption
2658
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
314
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
315 ## Misc ##
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
316
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
317 def markAsEncrypted(self, mess_data):
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
318 """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
319
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
320 This should be used in the post_treat workflow of MessageReceived trigger of
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
321 the plugin
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
322 @param mess_data(dict): message data as used in post treat workflow
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
323 """
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
324 mess_data['encrypted'] = True
4e130cc9bfc0 core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents: 2651
diff changeset
325 return mess_data
2733
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
326
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
327 def markAsUntrusted(self, mess_data):
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
328 """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
329
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
330 This should be used in the post_treat workflow of MessageReceived trigger of
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
331 the plugin
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
332 @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
333 """
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
334 mess_data['untrusted'] = True
e347e32aa07f core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents: 2658
diff changeset
335 return mess_data