annotate sat/plugins/plugin_xep_0380.py @ 2750:ae495f27b316

plugin XEP-0380: Explicit Message Encryption implementation
author Goffi <goffi@goffi.org>
date Fri, 04 Jan 2019 11:14:53 +0100
parents
children 3bea6b5ae972
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2750
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
3
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for Explicit Message Encryption
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
6
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
11
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
16
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
19
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _, D_
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.log import getLogger
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber import jid
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
24
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
25 log = getLogger(__name__)
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
26
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
27 PLUGIN_INFO = {
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
28 C.PI_NAME: u"Explicit Message Encryption",
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
29 C.PI_IMPORT_NAME: u"XEP-0380",
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
30 C.PI_TYPE: u"SEC",
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
31 C.PI_PROTOCOLS: [u"XEP-0380"],
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
32 C.PI_DEPENDENCIES: [],
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
33 C.PI_MAIN: u"XEP_0380",
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
34 C.PI_HANDLER: u"no",
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_DESCRIPTION: _(u"""Implementation of Explicit Message Encryption"""),
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
36 }
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
37
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
38 NS_EME = u"urn:xmpp:eme:0"
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
39 KNOWN_NAMESPACES = {
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
40 u"urn:xmpp:otr:0": u"OTR",
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
41 u"jabber:x:encrypted": u"Legacy OpenPGP",
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
42 u"urn:xmpp:openpgp:0": u"OpenPGP for XMPP",
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
43 }
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
44
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
45
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
46 class XEP_0380(object):
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
47
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
48 def __init__(self, host):
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.host = host
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
50 host.trigger.add("sendMessage", self._sendMessageTrigger)
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
51 host.trigger.add("MessageReceived", self._MessageReceivedTrigger, priority=100)
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
52
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
53 def _addEMEElement(self, mess_data, namespace, name):
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
54 message_elt = mess_data[u'xml']
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
55 encryption_elt = message_elt.addElement((NS_EME, u'encryption'))
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
56 encryption_elt[u'namespace'] = namespace
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
57 if name is not None:
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
58 encryption_elt[u'name'] = name
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
59 return mess_data
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
60
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
61 def _sendMessageTrigger(self, client, mess_data, __, post_xml_treatments):
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
62 encryption = mess_data.get(C.MESS_KEY_ENCRYPTION)
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
63 if encryption is not None:
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
64 namespace = encryption['plugin'].namespace
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
65 if namespace not in KNOWN_NAMESPACES:
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
66 name = encryption[u'plugin'].name
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
67 else:
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
68 name = None
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
69 post_xml_treatments.addCallback(
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self._addEMEElement, namespace=namespace, name=name)
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
71 return True
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
72
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def _MessageReceivedTrigger(self, client, message_elt, post_treat):
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
74 try:
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
75 encryption_elt = next(message_elt.elements(NS_EME, u'encryption'))
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
76 except StopIteration:
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
77 return True
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
78
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
79 namespace = encryption_elt['namespace']
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
80 if namespace in client.encryption.getNamespaces():
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
81 # message is encrypted and we can decrypt it
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
82 return True
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
83
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
84 name = KNOWN_NAMESPACES.get(namespace, encryption_elt.getAttribute(u"name"))
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
85
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
86 # at this point, message is encrypted but we know that we can't decrypt it,
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
87 # we need to notify the user
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
88 sender_s = message_elt[u'from']
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
89 to_jid = jid.JID(message_elt[u'from'])
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
90 algorithm = u"{} [{}]".format(name, namespace) if name else namespace
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
91 log.warning(
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
92 _(u"Message from {sender} is encrypted with {algorithm} and we can't "
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
93 u"decrypt it.".format(sender=message_elt['from'], algorithm=algorithm)))
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
94
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
95 user_msg = D_(
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
96 u"User {sender} sent you an encrypted message (encrypted with {algorithm}), "
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
97 u"and we can't decrypt it.").format(sender=sender_s, algorithm=algorithm)
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
98
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
99 extra = {C.MESS_EXTRA_INFO: u"UNKNOWN_ENCRYPTION"}
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
100 client.feedback(to_jid, user_msg, extra)
ae495f27b316 plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
101 return False