comparison libervia/backend/plugins/plugin_xep_0380.py @ 4071:4b842c1fb686

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