annotate sat/plugins/plugin_xep_0384.py @ 3541:888109774673

core: various changes and fixes to work with new storage and D-Bus bridge: - fixes coroutines handling in various places - fixes types which are not serialised by Tx DBus - XEP-0384: call storage methods in main thread in XEP: Python OMEMO's Promise use thread which prevent the use of AsyncIO loop. To work around that, callLater is used to launch storage method in main thread. This is a temporary workaround, as Python OMEMO should get rid of Promise implementation and threads soon.
author Goffi <goffi@goffi.org>
date Thu, 03 Jun 2021 15:21:43 +0200
parents be6d91572633
children edc79cefe968
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1 #!/usr/bin/env python3
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # SAT plugin for OMEMO encryption
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3240
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
19 import logging
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
20 import random
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
21 import base64
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
22 from functools import partial
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
23 from xml.sax.saxutils import quoteattr
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
24 from sat.core.i18n import _, D_
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core.constants import Const as C
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.core.log import getLogger
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.core import exceptions
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
28 from twisted.internet import defer, reactor
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from twisted.words.xish import domish
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from twisted.words.protocols.jabber import jid
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
31 from twisted.words.protocols.jabber import error as jabber_error
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from sat.memory import persistent
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
33 from sat.tools import xml_tools
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 import omemo
3084
ffcdd93b61fa plugin XEP-0384: specific warning on missing omemo-backend-signal + fixed encoding following Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
36 from omemo import exceptions as omemo_excpt
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from omemo.extendedpublicbundle import ExtendedPublicBundle
3084
ffcdd93b61fa plugin XEP-0384: specific warning on missing omemo-backend-signal + fixed encoding following Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
38 except ImportError:
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 raise exceptions.MissingModule(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
40 'Missing module omemo, please download/install it. You can use '
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
41 '"pip install omemo"'
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 )
3084
ffcdd93b61fa plugin XEP-0384: specific warning on missing omemo-backend-signal + fixed encoding following Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
43 try:
ffcdd93b61fa plugin XEP-0384: specific warning on missing omemo-backend-signal + fixed encoding following Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
44 from omemo_backend_signal import BACKEND as omemo_backend
ffcdd93b61fa plugin XEP-0384: specific warning on missing omemo-backend-signal + fixed encoding following Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
45 except ImportError:
ffcdd93b61fa plugin XEP-0384: specific warning on missing omemo-backend-signal + fixed encoding following Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
46 raise exceptions.MissingModule(
ffcdd93b61fa plugin XEP-0384: specific warning on missing omemo-backend-signal + fixed encoding following Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
47 'Missing module omemo-backend-signal, please download/install it. You can use '
ffcdd93b61fa plugin XEP-0384: specific warning on missing omemo-backend-signal + fixed encoding following Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
48 '"pip install omemo-backend-signal"'
ffcdd93b61fa plugin XEP-0384: specific warning on missing omemo-backend-signal + fixed encoding following Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
49 )
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 log = getLogger(__name__)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 PLUGIN_INFO = {
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
54 C.PI_NAME: "OMEMO",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
55 C.PI_IMPORT_NAME: "XEP-0384",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
56 C.PI_TYPE: "SEC",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
57 C.PI_PROTOCOLS: ["XEP-0384"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
58 C.PI_DEPENDENCIES: ["XEP-0163", "XEP-0280", "XEP-0334", "XEP-0060"],
3218
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
59 C.PI_RECOMMENDATIONS: ["XEP-0045", "XEP-0359", C.TEXT_CMDS],
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
60 C.PI_MAIN: "OMEMO",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
61 C.PI_HANDLER: "no",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
62 C.PI_DESCRIPTION: _("""Implementation of OMEMO"""),
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 }
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
65 OMEMO_MIN_VER = (0, 11, 0)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 NS_OMEMO = "eu.siacs.conversations.axolotl"
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 NS_OMEMO_DEVICES = NS_OMEMO + ".devicelist"
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 NS_OMEMO_BUNDLE = NS_OMEMO + ".bundles:{device_id}"
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 KEY_STATE = "STATE"
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 KEY_DEVICE_ID = "DEVICE_ID"
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 KEY_SESSION = "SESSION"
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
72 KEY_TRUST = "TRUST"
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
73 # devices which have been automatically trusted by policy like BTBV
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
74 KEY_AUTO_TRUST = "AUTO_TRUST"
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
75 # list of peer bare jids where trust UI has been used at least once
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
76 # this is useful to activate manual trust with BTBV policy
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
77 KEY_MANUAL_TRUST = "MANUAL_TRUST"
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 KEY_ACTIVE_DEVICES = "DEVICES"
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
79 KEY_INACTIVE_DEVICES = "INACTIVE_DEVICES"
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
80 KEY_ALL_JIDS = "ALL_JIDS"
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
81 # time before plaintext cache for MUC is expired
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
82 # expressed in seconds, reset on each new MUC message
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
83 MUC_CACHE_TTL = 60 * 5
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
85 PARAM_CATEGORY = "Security"
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
86 PARAM_NAME = "omemo_policy"
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
87
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
89 # we want to manage log emitted by omemo module ourselves
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
90
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
91 class SatHandler(logging.Handler):
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
92
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
93 def emit(self, record):
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
94 log.log(record.levelname, record.getMessage())
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
95
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
96 @staticmethod
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
97 def install():
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
98 omemo_sm_logger = logging.getLogger("omemo.SessionManager")
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
99 omemo_sm_logger.propagate = False
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
100 omemo_sm_logger.addHandler(SatHandler())
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
101
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
102
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
103 SatHandler.install()
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
104
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
105
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 def b64enc(data):
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
107 return base64.b64encode(bytes(bytearray(data))).decode("US-ASCII")
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
108
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
109
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
110 def promise2Deferred(promise_):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
111 """Create a Deferred and fire it when promise is resolved
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
112
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
113 @param promise_(promise.Promise): promise to convert
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
114 @return (defer.Deferred): deferred instance linked to the promise
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
115 """
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
116 d = defer.Deferred()
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
117 promise_.then(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
118 lambda result: reactor.callLater(0, d.callback, result),
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
119 lambda exc: reactor.callLater(0, d.errback, exc)
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
120 )
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
121 return d
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 class OmemoStorage(omemo.Storage):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
126 def __init__(self, client, device_id, all_jids):
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
127 self.own_bare_jid_s = client.jid.userhost()
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
128 self.device_id = device_id
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
129 self.all_jids = all_jids
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
130 self.data = client._xep_0384_data
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 @property
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 def is_async(self):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 return True
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 def setCb(self, deferred, callback):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 """Associate Deferred and callback
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 callback of omemo.Storage expect a boolean with success state then result
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 Deferred on the other hand use 2 methods for callback and errback
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 This method use partial to call callback with boolean then result when
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 Deferred is called
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 """
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 deferred.addCallback(partial(callback, True))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 deferred.addErrback(partial(callback, False))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
147 def _callMainThread(self, callback, method, *args, check_jid=None):
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
148 d = method(*args)
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
149 if check_jid is not None:
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
150 check_jid_d = self._checkJid(check_jid)
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
151 check_jid_d.addCallback(lambda __: d)
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
152 d = check_jid_d
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
153 if callback is not None:
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
154 d.addCallback(partial(callback, True))
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
155 d.addErrback(partial(callback, False))
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
156
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
157 def _call(self, callback, method, *args, check_jid=None):
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
158 """Create Deferred and add Promise callback to it
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
159
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
160 This method use reactor.callLater to launch Deferred in main thread
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
161 @param check_jid: run self._checkJid before method
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
162 """
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
163 reactor.callLater(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
164 0, self._callMainThread, callback, method, *args, check_jid=check_jid
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
165 )
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
166
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
167 def _checkJid(self, bare_jid):
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
168 """Check if jid is known, and store it if not
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
169
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
170 @param bare_jid(unicode): bare jid to check
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
171 @return (D): Deferred fired when jid is stored
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
172 """
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
173 if bare_jid in self.all_jids:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
174 return defer.succeed(None)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
175 else:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
176 self.all_jids.add(bare_jid)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
177 d = self.data.force(KEY_ALL_JIDS, self.all_jids)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
178 return d
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
179
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
180 def loadOwnData(self, callback):
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
181 callback(True, {'own_bare_jid': self.own_bare_jid_s,
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
182 'own_device_id': self.device_id})
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
183
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
184 def storeOwnData(self, callback, own_bare_jid, own_device_id):
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
185 if own_bare_jid != self.own_bare_jid_s or own_device_id != self.device_id:
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
186 raise exceptions.InternalError('bare jid or device id inconsistency!')
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
187 callback(True, None)
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
188
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 def loadState(self, callback):
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
190 self._call(callback, self.data.get, KEY_STATE)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
192 def storeState(self, callback, state):
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
193 self._call(callback, self.data.force, KEY_STATE, state)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
194
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
195 def loadSession(self, callback, bare_jid, device_id):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
196 key = '\n'.join([KEY_SESSION, bare_jid, str(device_id)])
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
197 self._call(callback, self.data.get, key)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
198
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
199 def storeSession(self, callback, bare_jid, device_id, session):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
200 key = '\n'.join([KEY_SESSION, bare_jid, str(device_id)])
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
201 self._call(callback, self._data.force, key, session)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
202
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
203 def deleteSession(self, callback, bare_jid, device_id):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
204 key = '\n'.join([KEY_SESSION, bare_jid, str(device_id)])
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
205 self._call(callback, self.data.remove, key)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
206
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
207 def loadActiveDevices(self, callback, bare_jid):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
208 key = '\n'.join([KEY_ACTIVE_DEVICES, bare_jid])
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
209 self._call(callback, self.data.get, key, {})
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
210
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
211 def loadInactiveDevices(self, callback, bare_jid):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
212 key = '\n'.join([KEY_INACTIVE_DEVICES, bare_jid])
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
213 self._call(callback, self.data.get, key, {})
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
214
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
215 def storeActiveDevices(self, callback, bare_jid, devices):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
216 key = '\n'.join([KEY_ACTIVE_DEVICES, bare_jid])
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
217 self._call(callback, self.data.force, key, devices, check_jid=bare_jid)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
218
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
219 def storeInactiveDevices(self, callback, bare_jid, devices):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
220 key = '\n'.join([KEY_INACTIVE_DEVICES, bare_jid])
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
221 self._call(callback, self.data.force, key, devices, check_jid=bare_jid)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
222
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
223 def storeTrust(self, callback, bare_jid, device_id, trust):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
224 key = '\n'.join([KEY_TRUST, bare_jid, str(device_id)])
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
225 self._call(callback, self.data.force, key, trust)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
226
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
227 def loadTrust(self, callback, bare_jid, device_id):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
228 key = '\n'.join([KEY_TRUST, bare_jid, str(device_id)])
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
229 self._call(callback, self.data.get, key)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
230
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
231 def listJIDs(self, callback):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
232 if callback is not None:
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
233 callback(True, self.all_jids)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
234
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
235 def _deleteJID_logResults(self, results):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
236 failed = [success for success, __ in results if not success]
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
237 if failed:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
238 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
239 "delete JID failed for {failed_count} on {total_count} operations"
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
240 .format(failed_count=len(failed), total_count=len(results)))
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
241 else:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
242 log.info(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
243 "Delete JID operation succeed ({total_count} operations)."
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
244 .format(total_count=len(results)))
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
245
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
246 def _deleteJID_gotDevices(self, results, bare_jid):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
247 assert len(results) == 2
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
248 active_success, active_devices = results[0]
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
249 inactive_success, inactive_devices = results[0]
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
250 d_list = []
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
251 for success, devices in results:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
252 if not success:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
253 log.warning("Can't retrieve devices for {bare_jid}: {reason}"
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
254 .format(bare_jid=bare_jid, reason=active_devices))
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
255 else:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
256 for device_id in devices:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
257 for key in (KEY_SESSION, KEY_TRUST):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
258 k = '\n'.join([key, bare_jid, str(device_id)])
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
259 d_list.append(self.data.remove(k))
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
260
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
261 d_list.append(self.data.remove(KEY_ACTIVE_DEVICES, bare_jid))
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
262 d_list.append(self.data.remove(KEY_INACTIVE_DEVICES, bare_jid))
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
263 d_list.append(lambda __: self.all_jids.discard(bare_jid))
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
264 # FIXME: there is a risk of race condition here,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
265 # if self.all_jids is modified between discard and force)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
266 d_list.append(lambda __: self.data.force(KEY_ALL_JIDS, self.all_jids))
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
267 d = defer.DeferredList(d_list)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
268 d.addCallback(self._deleteJID_logResults)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
269 return d
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
270
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
271 def _deleteJID(self, callback, bare_jid):
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
272 d_list = []
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
273
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
274 key = '\n'.join([KEY_ACTIVE_DEVICES, bare_jid])
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
275 d_list.append(self.data.get(key, []))
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
276
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
277 key = '\n'.join([KEY_INACTIVE_DEVICES, bare_jid])
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
278 d_inactive = self.data.get(key, {})
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
279 # inactive devices are returned as a dict mapping from devices_id to timestamp
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
280 # but we only need devices ids
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
281 d_inactive.addCallback(lambda devices: [k for k, __ in devices])
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
282
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
283 d_list.append(d_inactive)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
284 d = defer.DeferredList(d_list)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
285 d.addCallback(self._deleteJID_gotDevices, bare_jid)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
286 if callback is not None:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
287 self.setCb(d, callback)
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
288
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
289 def deleteJID(self, callback, bare_jid):
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
290 """Retrieve all (in)actives devices of bare_jid, and delete all related keys"""
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
291 reactor.callLater(0, self._deleteJID, callback, bare_jid)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
292
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
293
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
294 class SatOTPKPolicy(omemo.DefaultOTPKPolicy):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
295 pass
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
296
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
297
3236
9477f3197981 plugin XEP-0384: added resetTrust in OmemoSession
Goffi <goffi@goffi.org>
parents: 3231
diff changeset
298 class OmemoSession:
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 """Wrapper to use omemo.OmemoSession with Deferred"""
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
300
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 def __init__(self, session):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 self._session = session
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
303
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
304 @property
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
305 def republish_bundle(self):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
306 return self._session.republish_bundle
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
307
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
308 @property
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
309 def public_bundle(self):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
310 return self._session.public_bundle
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
311
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 @classmethod
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
313 def create(cls, client, storage, my_device_id = None):
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
314 omemo_session_p = omemo.SessionManager.create(
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
315 storage,
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
316 SatOTPKPolicy,
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
317 omemo_backend,
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
318 client.jid.userhost(),
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
319 my_device_id)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
320 d = promise2Deferred(omemo_session_p)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 d.addCallback(lambda session: cls(session))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 return d
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
323
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
324 def newDeviceList(self, jid, devices):
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
325 jid = jid.userhost()
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
326 new_device_p = self._session.newDeviceList(jid, devices)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
327 return promise2Deferred(new_device_p)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
328
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 def getDevices(self, bare_jid=None):
3218
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
330 bare_jid = bare_jid.userhost()
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 get_devices_p = self._session.getDevices(bare_jid=bare_jid)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
332 return promise2Deferred(get_devices_p)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
333
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
334 def buildSession(self, bare_jid, device, bundle):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 bare_jid = bare_jid.userhost()
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
336 build_session_p = self._session.buildSession(bare_jid, int(device), bundle)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
337 return promise2Deferred(build_session_p)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
338
3218
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
339 def deleteSession(self, bare_jid, device):
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
340 bare_jid = bare_jid.userhost()
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
341 delete_session_p = self._session.deleteSession(
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
342 bare_jid=bare_jid, device=int(device))
3218
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
343 return promise2Deferred(delete_session_p)
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
344
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
345 def encryptMessage(self, bare_jids, message, bundles=None, expect_problems=None):
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
346 """Encrypt a message
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
347
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
348 @param bare_jids(iterable[jid.JID]): destinees of the message
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 @param message(unicode): message to encode
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
350 @param bundles(dict[jid.JID, dict[int, ExtendedPublicBundle]):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
351 entities => devices => bundles map
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
352 @return D(dict): encryption data
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
353 """
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
354 bare_jids = [e.userhost() for e in bare_jids]
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
355 if bundles is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
356 bundles = {e.userhost(): v for e, v in bundles.items()}
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
357 encrypt_mess_p = self._session.encryptMessage(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
358 bare_jids=bare_jids,
3084
ffcdd93b61fa plugin XEP-0384: specific warning on missing omemo-backend-signal + fixed encoding following Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
359 plaintext=message.encode(),
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
360 bundles=bundles,
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
361 expect_problems=expect_problems)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
362 return promise2Deferred(encrypt_mess_p)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
363
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
364 def encryptRatchetForwardingMessage(
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
365 self, bare_jids, bundles=None, expect_problems=None):
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
366 bare_jids = [e.userhost() for e in bare_jids]
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
367 if bundles is not None:
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
368 bundles = {e.userhost(): v for e, v in bundles.items()}
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
369 encrypt_ratchet_fwd_p = self._session.encryptRatchetForwardingMessage(
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
370 bare_jids=bare_jids,
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
371 bundles=bundles,
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
372 expect_problems=expect_problems)
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
373 return promise2Deferred(encrypt_ratchet_fwd_p)
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
374
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
375 def decryptMessage(self, bare_jid, device, iv, message, is_pre_key_message,
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
376 ciphertext, additional_information=None, allow_untrusted=False):
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
377 bare_jid = bare_jid.userhost()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
378 decrypt_mess_p = self._session.decryptMessage(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
379 bare_jid=bare_jid,
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
380 device=int(device),
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
381 iv=iv,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
382 message=message,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
383 is_pre_key_message=is_pre_key_message,
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
384 ciphertext=ciphertext,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
385 additional_information=additional_information,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
386 allow_untrusted=allow_untrusted
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
387 )
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
388 return promise2Deferred(decrypt_mess_p)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
389
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
390 def decryptRatchetForwardingMessage(
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
391 self, bare_jid, device, iv, message, is_pre_key_message,
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
392 additional_information=None, allow_untrusted=False):
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
393 bare_jid = bare_jid.userhost()
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
394 decrypt_ratchet_fwd_p = self._session.decryptRatchetForwardingMessage(
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
395 bare_jid=bare_jid,
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
396 device=int(device),
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
397 iv=iv,
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
398 message=message,
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
399 is_pre_key_message=is_pre_key_message,
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
400 additional_information=additional_information,
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
401 allow_untrusted=allow_untrusted
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
402 )
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
403 return promise2Deferred(decrypt_ratchet_fwd_p)
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
404
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
405 def setTrust(self, bare_jid, device, key, trusted):
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
406 bare_jid = bare_jid.userhost()
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
407 setTrust_p = self._session.setTrust(
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
408 bare_jid=bare_jid,
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
409 device=int(device),
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
410 key=key,
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
411 trusted=trusted,
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
412 )
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
413 return promise2Deferred(setTrust_p)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
414
3236
9477f3197981 plugin XEP-0384: added resetTrust in OmemoSession
Goffi <goffi@goffi.org>
parents: 3231
diff changeset
415 def resetTrust(self, bare_jid, device):
9477f3197981 plugin XEP-0384: added resetTrust in OmemoSession
Goffi <goffi@goffi.org>
parents: 3231
diff changeset
416 bare_jid = bare_jid.userhost()
9477f3197981 plugin XEP-0384: added resetTrust in OmemoSession
Goffi <goffi@goffi.org>
parents: 3231
diff changeset
417 resetTrust_p = self._session.resetTrust(
9477f3197981 plugin XEP-0384: added resetTrust in OmemoSession
Goffi <goffi@goffi.org>
parents: 3231
diff changeset
418 bare_jid=bare_jid,
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
419 device=int(device),
3236
9477f3197981 plugin XEP-0384: added resetTrust in OmemoSession
Goffi <goffi@goffi.org>
parents: 3231
diff changeset
420 )
9477f3197981 plugin XEP-0384: added resetTrust in OmemoSession
Goffi <goffi@goffi.org>
parents: 3231
diff changeset
421 return promise2Deferred(resetTrust_p)
9477f3197981 plugin XEP-0384: added resetTrust in OmemoSession
Goffi <goffi@goffi.org>
parents: 3231
diff changeset
422
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
423 def getTrustForJID(self, bare_jid):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
424 bare_jid = bare_jid.userhost()
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
425 get_trust_p = self._session.getTrustForJID(bare_jid=bare_jid)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
426 return promise2Deferred(get_trust_p)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
427
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
428
3218
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
429 class OMEMO:
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
430
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
431 params = """
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
432 <params>
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
433 <individual>
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
434 <category name="{category_name}" label="{category_label}">
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
435 <param name="{param_name}" label={param_label} type="list" security="3">
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
436 <option value="manual" label={opt_manual_lbl} />
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
437 <option value="btbv" label={opt_btbv_lbl} selected="true" />
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
438 </param>
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
439 </category>
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
440 </individual>
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
441 </params>
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
442 """.format(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
443 category_name=PARAM_CATEGORY,
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
444 category_label=D_("Security"),
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
445 param_name=PARAM_NAME,
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
446 param_label=quoteattr(D_("OMEMO default trust policy")),
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
447 opt_manual_lbl=quoteattr(D_("Manual trust (more secure)")),
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
448 opt_btbv_lbl=quoteattr(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
449 D_("Blind Trust Before Verification (more user friendly)")),
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
450 )
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
451
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
452 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
453 log.info(_("OMEMO plugin initialization (omemo module v{version})").format(
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
454 version=omemo.__version__))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
455 version = tuple(map(int, omemo.__version__.split('.')[:3]))
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
456 if version < OMEMO_MIN_VER:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
457 log.warning(_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
458 "Your version of omemo module is too old: {v[0]}.{v[1]}.{v[2]} is "
3098
518208085dfb plugin XEP-0384: typo
Goffi <goffi@goffi.org>
parents: 3085
diff changeset
459 "minimum required, please update.").format(v=OMEMO_MIN_VER))
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
460 raise exceptions.CancelError("module is too old")
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
461 self.host = host
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
462 host.memory.updateParams(self.params)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
463 self._p_hints = host.plugins["XEP-0334"]
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
464 self._p_carbons = host.plugins["XEP-0280"]
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
465 self._p = host.plugins["XEP-0060"]
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
466 self._m = host.plugins.get("XEP-0045")
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
467 self._sid = host.plugins.get("XEP-0359")
3172
dcebc585c29f core: renamed "MessageReceived" trigger to "messageReceived" for consistency.
Goffi <goffi@goffi.org>
parents: 3142
diff changeset
468 host.trigger.add("messageReceived", self._messageReceivedTrigger, priority=100050)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
469 host.trigger.add("sendMessageData", self._sendMessageDataTrigger)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
470 self.host.registerEncryptionPlugin(self, "OMEMO", NS_OMEMO, 100)
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
471 pep = host.plugins['XEP-0163']
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
472 pep.addPEPEvent(
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
473 "OMEMO_DEVICES", NS_OMEMO_DEVICES,
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
474 lambda itemsEvent, profile: defer.ensureDeferred(
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
475 self.onNewDevices(itemsEvent, profile))
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
476 )
3218
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
477 try:
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
478 self.text_cmds = self.host.plugins[C.TEXT_CMDS]
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
479 except KeyError:
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
480 log.info(_("Text commands not available"))
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
481 else:
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
482 self.text_cmds.registerTextCommands(self)
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
483
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
484 # Text commands #
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
485
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
486 async def cmd_omemo_reset(self, client, mess_data):
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
487 """reset OMEMO session (use only if encryption is broken)
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
488
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
489 @command(one2one):
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
490 """
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
491 if not client.encryption.isEncryptionRequested(mess_data, NS_OMEMO):
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
492 feedback = _(
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
493 "You need to have OMEMO encryption activated to reset the session")
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
494 self.text_cmds.feedBack(client, feedback, mess_data)
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
495 return False
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
496 to_jid = mess_data["to"].userhostJID()
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
497 session = client._xep_0384_session
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
498 devices = await session.getDevices(to_jid)
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
499
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
500 for device in devices['active']:
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
501 log.debug(f"deleting session for device {device}")
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
502 await session.deleteSession(to_jid, device=device)
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
503
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
504 log.debug("Sending an empty message to trigger key exchange")
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
505 await client.sendMessage(to_jid, {'': ''})
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
506
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
507 feedback = _("OMEMO session has been reset")
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
508 self.text_cmds.feedBack(client, feedback, mess_data)
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
509 return False
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
510
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
511 async def trustUICb(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
512 self, xmlui_data, trust_data, expect_problems=None, profile=C.PROF_KEY_NONE):
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
513 if C.bool(xmlui_data.get('cancelled', 'false')):
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
514 return {}
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
515 client = self.host.getClient(profile)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
516 session = client._xep_0384_session
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
517 stored_data = client._xep_0384_data
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
518 manual_trust = await stored_data.get(KEY_MANUAL_TRUST, set())
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
519 auto_trusted_cache = {}
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
520 answer = xml_tools.XMLUIResult2DataFormResult(xmlui_data)
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
521 blind_trust = C.bool(answer.get('blind_trust', C.BOOL_FALSE))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
522 for key, value in answer.items():
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
523 if key.startswith('trust_'):
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
524 trust_id = key[6:]
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
525 else:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
526 continue
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
527 data = trust_data[trust_id]
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
528 if blind_trust:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
529 # user request to restore blind trust for this entity
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
530 # so if the entity is present in manual trust, we remove it
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
531 if data["jid"].full() in manual_trust:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
532 manual_trust.remove(data["jid"].full())
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
533 await stored_data.aset(KEY_MANUAL_TRUST, manual_trust)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
534 elif data["jid"].full() not in manual_trust:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
535 # validating this trust UI implies that we activate manual mode for
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
536 # this entity (used for BTBV policy)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
537 manual_trust.add(data["jid"].full())
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
538 await stored_data.aset(KEY_MANUAL_TRUST, manual_trust)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
539 trust = C.bool(value)
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
540
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
541 if not trust:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
542 # if device is not trusted, we check if it must be removed from auto
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
543 # trusted devices list
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
544 bare_jid_s = data['jid'].userhost()
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
545 key = f"{KEY_AUTO_TRUST}\n{bare_jid_s}"
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
546 if bare_jid_s not in auto_trusted_cache:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
547 auto_trusted_cache[bare_jid_s] = await stored_data.get(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
548 key, default=set())
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
549 auto_trusted = auto_trusted_cache[bare_jid_s]
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
550 if data['device'] in auto_trusted:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
551 # as we don't trust this device anymore, we can remove it from the
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
552 # list of automatically trusted devices
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
553 auto_trusted.remove(data['device'])
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
554 await stored_data.aset(key, auto_trusted)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
555 log.info(D_(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
556 "device {device} from {peer_jid} is not an auto-trusted device "
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
557 "anymore").format(device=data['device'], peer_jid=bare_jid_s))
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
558
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
559 await session.setTrust(
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
560 data["jid"],
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
561 data["device"],
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
562 data["ik"],
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
563 trusted=trust,
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
564 )
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
565 if not trust and expect_problems is not None:
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
566 expect_problems.setdefault(data['jid'].userhost(), set()).add(
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
567 data['device']
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
568 )
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
569 return {}
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
570
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
571 async def getTrustUI(self, client, entity_jid=None, trust_data=None, submit_id=None):
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
572 """Generate a XMLUI to manage trust
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
573
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
574 @param entity_jid(None, jid.JID): jid of entity to manage
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
575 None to use trust_data
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
576 @param trust_data(None, dict): devices data:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
577 None to use entity_jid
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
578 else a dict mapping from trust ids (unicode) to devices data,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
579 where a device data must have the following keys:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
580 - jid(jid.JID): bare jid of the device owner
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
581 - device(int): device id
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
582 - ik(bytes): identity key
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
583 and may have the following key:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
584 - trusted(bool): True if device is trusted
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
585 @param submit_id(None, unicode): submit_id to use
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
586 if None set UI callback to trustUICb
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
587 @return D(xmlui): trust management form
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
588 """
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
589 # we need entity_jid xor trust_data
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
590 assert entity_jid and not trust_data or not entity_jid and trust_data
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
591 if entity_jid and entity_jid.resource:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
592 raise ValueError("A bare jid is expected")
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
593
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
594 session = client._xep_0384_session
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
595 stored_data = client._xep_0384_data
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
596
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
597 if trust_data is None:
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
598 cache = client._xep_0384_cache.setdefault(entity_jid, {})
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
599 trust_data = {}
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
600 if self._m is not None and self._m.isJoinedRoom(client, entity_jid):
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
601 trust_jids = self.getJIDsForRoom(client, entity_jid)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
602 else:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
603 trust_jids = [entity_jid]
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
604 for trust_jid in trust_jids:
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
605 trust_session_data = await session.getTrustForJID(trust_jid)
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
606 bare_jid_s = trust_jid.userhost()
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
607 for device_id, trust_info in trust_session_data['active'].items():
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
608 if trust_info is None:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
609 # device has never been (un)trusted, we have to retrieve its
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
610 # fingerprint (i.e. identity key or "ik") through public bundle
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
611 if device_id not in cache:
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
612 bundles, missing = await self.getBundles(client,
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
613 trust_jid,
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
614 [device_id])
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
615 if device_id not in bundles:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
616 log.warning(_(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
617 "Can't find bundle for device {device_id} of user "
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
618 "{bare_jid}, ignoring").format(device_id=device_id,
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
619 bare_jid=bare_jid_s))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
620 continue
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
621 cache[device_id] = bundles[device_id]
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
622 # TODO: replace False below by None when undecided
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
623 # trusts are handled
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
624 trust_info = {
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
625 "key": cache[device_id].ik,
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
626 "trusted": False
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
627 }
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
628
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
629 ik = trust_info["key"]
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
630 trust_id = str(hash((bare_jid_s, device_id, ik)))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
631 trust_data[trust_id] = {
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
632 "jid": trust_jid,
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
633 "device": device_id,
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
634 "ik": ik,
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
635 "trusted": trust_info["trusted"],
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
636 }
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
637
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
638 if submit_id is None:
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
639 submit_id = self.host.registerCallback(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
640 lambda data, profile: defer.ensureDeferred(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
641 self.trustUICb(data, trust_data=trust_data, profile=profile)),
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
642 with_data=True,
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
643 one_shot=True)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
644 xmlui = xml_tools.XMLUI(
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
645 panel_type = C.XMLUI_FORM,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
646 title = D_("OMEMO trust management"),
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
647 submit_id = submit_id
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
648 )
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
649 xmlui.addText(D_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
650 "This is OMEMO trusting system. You'll see below the devices of your "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
651 "contacts, and a checkbox to trust them or not. A trusted device "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
652 "can read your messages in plain text, so be sure to only validate "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
653 "devices that you are sure are belonging to your contact. It's better "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
654 "to do this when you are next to your contact and her/his device, so "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
655 "you can check the \"fingerprint\" (the number next to the device) "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
656 "yourself. Do *not* validate a device if the fingerprint is wrong!"))
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
657
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
658 xmlui.changeContainer("label")
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
659 xmlui.addLabel(D_("This device ID"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
660 xmlui.addText(str(client._xep_0384_device_id))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
661 xmlui.addLabel(D_("This device fingerprint"))
3084
ffcdd93b61fa plugin XEP-0384: specific warning on missing omemo-backend-signal + fixed encoding following Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
662 ik_hex = session.public_bundle.ik.hex().upper()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
663 fp_human = ' '.join([ik_hex[i:i+8] for i in range(0, len(ik_hex), 8)])
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
664 xmlui.addText(fp_human)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
665 xmlui.addEmpty()
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
666 xmlui.addEmpty()
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
667
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
668 if entity_jid is not None:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
669 omemo_policy = self.host.memory.getParamA(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
670 PARAM_NAME, PARAM_CATEGORY, profile_key=client.profile
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
671 )
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
672 if omemo_policy == 'btbv':
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
673 xmlui.addLabel(D_("Automatically trust new devices?"))
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
674 # blind trust is always disabled when UI is requested
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
675 # as submitting UI is a verification which should disable it.
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
676 xmlui.addBool("blind_trust", value=C.BOOL_FALSE)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
677 xmlui.addEmpty()
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
678 xmlui.addEmpty()
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
679
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
680 auto_trust_cache = {}
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
681
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
682 for trust_id, data in trust_data.items():
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
683 bare_jid_s = data['jid'].userhost()
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
684 if bare_jid_s not in auto_trust_cache:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
685 key = f"{KEY_AUTO_TRUST}\n{bare_jid_s}"
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
686 auto_trust_cache[bare_jid_s] = await stored_data.get(key, set())
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
687 xmlui.addLabel(D_("Contact"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
688 xmlui.addJid(data['jid'])
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
689 xmlui.addLabel(D_("Device ID"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
690 xmlui.addText(str(data['device']))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
691 xmlui.addLabel(D_("Fingerprint"))
3084
ffcdd93b61fa plugin XEP-0384: specific warning on missing omemo-backend-signal + fixed encoding following Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
692 ik_hex = data['ik'].hex().upper()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
693 fp_human = ' '.join([ik_hex[i:i+8] for i in range(0, len(ik_hex), 8)])
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
694 xmlui.addText(fp_human)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
695 xmlui.addLabel(D_("Trust this device?"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
696 xmlui.addBool("trust_{}".format(trust_id),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
697 value=C.boolConst(data.get('trusted', False)))
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
698 if data['device'] in auto_trust_cache[bare_jid_s]:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
699 xmlui.addEmpty()
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
700 xmlui.addLabel(D_("(automatically trusted)"))
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
701
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
702
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
703 xmlui.addEmpty()
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
704 xmlui.addEmpty()
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
705
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
706 return xmlui
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
707
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
708 async def profileConnected(self, client):
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
709 if self._m is not None:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
710 # we keep plain text message for MUC messages we send
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
711 # as we can't encrypt for our own device
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
712 client._xep_0384_muc_cache = {}
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
713 # and we keep them only for some time, in case something goes wrong
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
714 # with the MUC
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
715 client._xep_0384_muc_cache_timer = None
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
716
2925
03a09e16bf28 plugin XEP-0384: wait for client to be ready if session is missing when onNewDevices is called
Goffi <goffi@goffi.org>
parents: 2860
diff changeset
717 # FIXME: is _xep_0384_ready needed? can we use profileConnecting?
03a09e16bf28 plugin XEP-0384: wait for client to be ready if session is missing when onNewDevices is called
Goffi <goffi@goffi.org>
parents: 2860
diff changeset
718 # Workflow should be checked
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
719 client._xep_0384_ready = defer.Deferred()
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
720 # we first need to get devices ids (including our own)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
721 persistent_dict = persistent.LazyPersistentBinaryDict("XEP-0384", client.profile)
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
722 client._xep_0384_data = persistent_dict
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
723 # all known devices of profile
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
724 devices = await self.getDevices(client)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
725 # and our own device id
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
726 device_id = await persistent_dict.get(KEY_DEVICE_ID)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
727 if device_id is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
728 log.info(_("We have no identity for this device yet, let's generate one"))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
729 # we have a new device, we create device_id
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
730 device_id = random.randint(1, 2**31-1)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
731 # we check that it's really unique
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
732 while device_id in devices:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
733 device_id = random.randint(1, 2**31-1)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
734 # and we save it
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
735 await persistent_dict.aset(KEY_DEVICE_ID, device_id)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
736
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
737 log.debug(f"our OMEMO device id is {device_id}")
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
738
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
739 if device_id not in devices:
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
740 log.debug(f"our device id ({device_id}) is not in the list, adding it")
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
741 devices.add(device_id)
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
742 await defer.ensureDeferred(self.setDevices(client, devices))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
743
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
744 all_jids = await persistent_dict.get(KEY_ALL_JIDS, set())
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
745
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
746 omemo_storage = OmemoStorage(client, device_id, all_jids)
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
747 omemo_session = await OmemoSession.create(client, omemo_storage, device_id)
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
748 client._xep_0384_cache = {}
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
749 client._xep_0384_session = omemo_session
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
750 client._xep_0384_device_id = device_id
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
751 await omemo_session.newDeviceList(client.jid, devices)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
752 if omemo_session.republish_bundle:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
753 log.info(_("Saving public bundle for this device ({device_id})").format(
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
754 device_id=device_id))
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
755 await defer.ensureDeferred(
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
756 self.setBundle(client, omemo_session.public_bundle, device_id)
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
757 )
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
758 client._xep_0384_ready.callback(None)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
759 del client._xep_0384_ready
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
760
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
761
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
762 ## XMPP PEP nodes manipulation
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
763
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
764 # devices
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
765
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
766 def parseDevices(self, items):
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
767 """Parse devices found in items
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
768
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
769 @param items(iterable[domish.Element]): items as retrieved by getItems
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
770 @return set[int]: parsed devices
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
771 """
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
772 devices = set()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
773 if len(items) > 1:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
774 log.warning(_("OMEMO devices list is stored in more that one items, "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
775 "this is not expected"))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
776 if items:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
777 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
778 list_elt = next(items[0].elements(NS_OMEMO, 'list'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
779 except StopIteration:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
780 log.warning(_("no list element found in OMEMO devices list"))
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
781 return devices
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
782 for device_elt in list_elt.elements(NS_OMEMO, 'device'):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
783 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
784 device_id = int(device_elt['id'])
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
785 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
786 log.warning(_('device element is missing "id" attribute: {elt}')
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
787 .format(elt=device_elt.toXml()))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
788 except ValueError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
789 log.warning(_('invalid device id: {device_id}').format(
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
790 device_id=device_elt['id']))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
791 else:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
792 devices.add(device_id)
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
793 return devices
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
794
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
795 @defer.inlineCallbacks
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
796 def getDevices(self, client, entity_jid=None):
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
797 """Retrieve list of registered OMEMO devices
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
798
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
799 @param entity_jid(jid.JID, None): get devices from this entity
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
800 None to get our own devices
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
801 @return (set(int)): list of devices
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
802 """
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
803 if entity_jid is not None:
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
804 assert not entity_jid.resource
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
805 try:
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
806 items, metadata = yield self._p.getItems(client, entity_jid, NS_OMEMO_DEVICES)
3142
cd90c24b2836 plugin XEP-0384: fixed exception catching on getItems
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
807 except exceptions.NotFound:
cd90c24b2836 plugin XEP-0384: fixed exception catching on getItems
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
808 log.info(_("there is no node to handle OMEMO devices"))
cd90c24b2836 plugin XEP-0384: fixed exception catching on getItems
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
809 defer.returnValue(set())
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
810
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
811 devices = self.parseDevices(items)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
812 defer.returnValue(devices)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
813
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
814 async def setDevices(self, client, devices):
3218
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
815 log.debug(f"setting devices with {', '.join(str(d) for d in devices)}")
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
816 list_elt = domish.Element((NS_OMEMO, 'list'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
817 for device in devices:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
818 device_elt = list_elt.addElement('device')
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
819 device_elt['id'] = str(device)
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
820 try:
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
821 await self._p.sendItem(
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
822 client, None, NS_OMEMO_DEVICES, list_elt,
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
823 item_id=self._p.ID_SINGLETON,
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
824 extra={
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
825 self._p.EXTRA_PUBLISH_OPTIONS: {self._p.OPT_MAX_ITEMS: 1},
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
826 self._p.EXTRA_ON_PRECOND_NOT_MET: "publish_without_options",
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
827 }
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
828 )
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
829 except Exception as e:
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
830 log.warning(_("Can't set devices: {reason}").format(reason=e))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
831
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
832 # bundles
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
833
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
834 @defer.inlineCallbacks
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
835 def getBundles(self, client, entity_jid, devices_ids):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
836 """Retrieve public bundles of an entity devices
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
837
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
838 @param entity_jid(jid.JID): bare jid of entity
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
839 @param devices_id(iterable[int]): ids of the devices bundles to retrieve
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
840 @return (tuple(dict[int, ExtendedPublicBundle], list(int))):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
841 - bundles collection:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
842 * key is device_id
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
843 * value is parsed bundle
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
844 - set of bundles not found
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
845 """
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
846 assert not entity_jid.resource
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
847 bundles = {}
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
848 missing = set()
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
849 for device_id in devices_ids:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
850 node = NS_OMEMO_BUNDLE.format(device_id=device_id)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
851 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
852 items, metadata = yield self._p.getItems(client, entity_jid, node)
3142
cd90c24b2836 plugin XEP-0384: fixed exception catching on getItems
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
853 except exceptions.NotFound:
cd90c24b2836 plugin XEP-0384: fixed exception catching on getItems
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
854 log.warning(_("Bundle missing for device {device_id}")
cd90c24b2836 plugin XEP-0384: fixed exception catching on getItems
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
855 .format(device_id=device_id))
cd90c24b2836 plugin XEP-0384: fixed exception catching on getItems
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
856 missing.add(device_id)
cd90c24b2836 plugin XEP-0384: fixed exception catching on getItems
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
857 continue
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
858 except jabber_error.StanzaError as e:
3142
cd90c24b2836 plugin XEP-0384: fixed exception catching on getItems
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
859 log.warning(_("Can't get bundle for device {device_id}: {reason}")
cd90c24b2836 plugin XEP-0384: fixed exception catching on getItems
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
860 .format(device_id=device_id, reason=e))
cd90c24b2836 plugin XEP-0384: fixed exception catching on getItems
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
861 continue
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
862 if not items:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
863 log.warning(_("no item found in node {node}, can't get public bundle "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
864 "for device {device_id}").format(node=node,
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
865 device_id=device_id))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
866 continue
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
867 if len(items) > 1:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
868 log.warning(_("more than one item found in {node}, "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
869 "this is not expected").format(node=node))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
870 item = items[0]
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
871 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
872 bundle_elt = next(item.elements(NS_OMEMO, 'bundle'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
873 signedPreKeyPublic_elt = next(bundle_elt.elements(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
874 NS_OMEMO, 'signedPreKeyPublic'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
875 signedPreKeySignature_elt = next(bundle_elt.elements(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
876 NS_OMEMO, 'signedPreKeySignature'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
877 identityKey_elt = next(bundle_elt.elements(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
878 NS_OMEMO, 'identityKey'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
879 prekeys_elt = next(bundle_elt.elements(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
880 NS_OMEMO, 'prekeys'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
881 except StopIteration:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
882 log.warning(_("invalid bundle for device {device_id}, ignoring").format(
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
883 device_id=device_id))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
884 continue
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
885
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
886 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
887 spkPublic = base64.b64decode(str(signedPreKeyPublic_elt))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
888 spkSignature = base64.b64decode(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
889 str(signedPreKeySignature_elt))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
890
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
891 ik = base64.b64decode(str(identityKey_elt))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
892 spk = {
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
893 "key": spkPublic,
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
894 "id": int(signedPreKeyPublic_elt['signedPreKeyId'])
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
895 }
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
896 otpks = []
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
897 for preKeyPublic_elt in prekeys_elt.elements(NS_OMEMO, 'preKeyPublic'):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
898 preKeyPublic = base64.b64decode(str(preKeyPublic_elt))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
899 otpk = {
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
900 "key": preKeyPublic,
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
901 "id": int(preKeyPublic_elt['preKeyId'])
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
902 }
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
903 otpks.append(otpk)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
904
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
905 except Exception as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
906 log.warning(_("error while decoding key for device {device_id}: {msg}")
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
907 .format(device_id=device_id, msg=e))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
908 continue
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
909
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
910 bundles[device_id] = ExtendedPublicBundle.parse(omemo_backend, ik, spk,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
911 spkSignature, otpks)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
912
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
913 defer.returnValue((bundles, missing))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
914
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
915 async def setBundle(self, client, bundle, device_id):
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
916 """Set public bundle for this device.
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
917
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
918 @param bundle(ExtendedPublicBundle): bundle to publish
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
919 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
920 log.debug(_("updating bundle for {device_id}").format(device_id=device_id))
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
921 bundle = bundle.serialize(omemo_backend)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
922 bundle_elt = domish.Element((NS_OMEMO, 'bundle'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
923 signedPreKeyPublic_elt = bundle_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
924 "signedPreKeyPublic",
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
925 content=b64enc(bundle["spk"]['key']))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
926 signedPreKeyPublic_elt['signedPreKeyId'] = str(bundle["spk"]['id'])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
927
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
928 bundle_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
929 "signedPreKeySignature",
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
930 content=b64enc(bundle["spk_signature"]))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
931
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
932 bundle_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
933 "identityKey",
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
934 content=b64enc(bundle["ik"]))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
935
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
936 prekeys_elt = bundle_elt.addElement('prekeys')
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
937 for otpk in bundle["otpks"]:
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
938 preKeyPublic_elt = prekeys_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
939 'preKeyPublic',
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
940 content=b64enc(otpk["key"]))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
941 preKeyPublic_elt['preKeyId'] = str(otpk['id'])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
942
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
943 node = NS_OMEMO_BUNDLE.format(device_id=device_id)
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
944 try:
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
945 await self._p.sendItem(
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
946 client, None, node, bundle_elt, item_id=self._p.ID_SINGLETON,
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
947 extra={
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
948 self._p.EXTRA_PUBLISH_OPTIONS: {self._p.OPT_MAX_ITEMS: 1},
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
949 self._p.EXTRA_ON_PRECOND_NOT_MET: "publish_without_options",
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
950 }
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
951 )
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
952 except Exception as e:
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
953 log.warning(_("Can't set bundle: {reason}").format(reason=e))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
954
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
955 ## PEP node events callbacks
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
956
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
957 async def onNewDevices(self, itemsEvent, profile):
3218
806a7936a591 plugin XEP-0384: added "/omemo_reset" text command:
Goffi <goffi@goffi.org>
parents: 3214
diff changeset
958 log.debug("devices list has been updated")
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
959 client = self.host.getClient(profile)
2925
03a09e16bf28 plugin XEP-0384: wait for client to be ready if session is missing when onNewDevices is called
Goffi <goffi@goffi.org>
parents: 2860
diff changeset
960 try:
03a09e16bf28 plugin XEP-0384: wait for client to be ready if session is missing when onNewDevices is called
Goffi <goffi@goffi.org>
parents: 2860
diff changeset
961 omemo_session = client._xep_0384_session
03a09e16bf28 plugin XEP-0384: wait for client to be ready if session is missing when onNewDevices is called
Goffi <goffi@goffi.org>
parents: 2860
diff changeset
962 except AttributeError:
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
963 await client._xep_0384_ready
2925
03a09e16bf28 plugin XEP-0384: wait for client to be ready if session is missing when onNewDevices is called
Goffi <goffi@goffi.org>
parents: 2860
diff changeset
964 omemo_session = client._xep_0384_session
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
965 entity = itemsEvent.sender
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
966
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
967 devices = self.parseDevices(itemsEvent.items)
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
968 await omemo_session.newDeviceList(entity, devices)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
969
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
970 if entity == client.jid.userhostJID():
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
971 own_device = client._xep_0384_device_id
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
972 if own_device not in devices:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
973 log.warning(_("Our own device is missing from devices list, fixing it"))
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
974 devices.add(own_device)
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
975 await self.setDevices(client, devices)
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
976
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
977 ## triggers
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
978
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
979 async def policyBTBV(self, client, feedback_jid, expect_problems, undecided):
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
980 session = client._xep_0384_session
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
981 stored_data = client._xep_0384_data
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
982 for pb in undecided.values():
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
983 peer_jid = jid.JID(pb.bare_jid)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
984 device = pb.device
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
985 ik = pb.ik
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
986 key = f"{KEY_AUTO_TRUST}\n{pb.bare_jid}"
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
987 auto_trusted = await stored_data.get(key, default=set())
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
988 auto_trusted.add(device)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
989 await stored_data.aset(key, auto_trusted)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
990 await session.setTrust(peer_jid, device, ik, True)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
991
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
992 user_msg = D_(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
993 "Not all destination devices are trusted, unknown devices will be blind "
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
994 "trusted due to the OMEMO Blind Trust Before Verification policy. If you "
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
995 "want a more secure workflow, please activate \"manual\" OMEMO policy in "
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
996 "settings' \"Security\" tab.\nFollowing fingerprint have been automatically "
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
997 "trusted:\n{devices}"
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
998 ).format(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
999 devices = ', '.join(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1000 f"- {pb.device} ({pb.bare_jid}): {pb.ik.hex().upper()}"
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1001 for pb in undecided.values()
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1002 )
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1003 )
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1004 client.feedback(feedback_jid, user_msg)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1005
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1006 async def policyManual(self, client, feedback_jid, expect_problems, undecided):
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1007 trust_data = {}
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1008 for trust_id, data in undecided.items():
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1009 trust_data[trust_id] = {
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1010 'jid': jid.JID(data.bare_jid),
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1011 'device': data.device,
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1012 'ik': data.ik}
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1013
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1014 user_msg = D_("Not all destination devices are trusted, we can't encrypt "
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1015 "message in such a situation. Please indicate if you trust "
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1016 "those devices or not in the trust manager before we can "
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1017 "send this message")
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1018 client.feedback(feedback_jid, user_msg)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1019 xmlui = await self.getTrustUI(client, trust_data=trust_data, submit_id="")
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1020
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1021 answer = await xml_tools.deferXMLUI(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1022 self.host,
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1023 xmlui,
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1024 action_extra={
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1025 "meta_encryption_trust": NS_OMEMO,
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1026 },
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1027 profile=client.profile)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1028 await self.trustUICb(answer, trust_data, expect_problems, client.profile)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1029
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1030 async def handleProblems(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1031 self, client, feedback_jid, bundles, expect_problems, problems):
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1032 """Try to solve problems found by EncryptMessage
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1033
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1034 @param feedback_jid(jid.JID): bare jid where the feedback message must be sent
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1035 @param bundles(dict): bundles data as used in EncryptMessage
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1036 already filled with known bundles, missing bundles
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1037 need to be added to it
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1038 This dict is updated
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1039 @param problems(list): exceptions raised by EncryptMessage
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1040 @param expect_problems(dict): known problems to expect, used in encryptMessage
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1041 This dict will list devices where problems can be ignored
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1042 (those devices won't receive the encrypted data)
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1043 This dict is updated
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1044 """
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1045 # FIXME: not all problems are handled yet
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1046 undecided = {}
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1047 missing_bundles = {}
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1048 found_bundles = None
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1049 cache = client._xep_0384_cache
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1050 for problem in problems:
2857
88f10630d5ea plugin XEP-0384: removed version restriction, it is now compatible with (and require) last version of python-omemo (0.10.4)
Goffi <goffi@goffi.org>
parents: 2823
diff changeset
1051 if isinstance(problem, omemo_excpt.TrustException):
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1052 if problem.problem == 'undecided':
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1053 undecided[str(hash(problem))] = problem
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1054 elif problem.problem == 'untrusted':
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1055 expect_problems.setdefault(problem.bare_jid, set()).add(
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1056 problem.device)
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1057 log.info(_(
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1058 "discarding untrusted device {device_id} with key {device_key} "
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1059 "for {entity}").format(
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1060 device_id=problem.device,
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1061 device_key=problem.ik.hex().upper(),
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1062 entity=problem.bare_jid,
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1063 )
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1064 )
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1065 else:
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1066 log.error(
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1067 f"Unexpected trust problem: {problem.problem!r} for device "
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1068 f"{problem.device} for {problem.bare_jid}, ignoring device")
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1069 expect_problems.setdefault(problem.bare_jid, set()).add(
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1070 problem.device)
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1071 elif isinstance(problem, omemo_excpt.MissingBundleException):
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1072 pb_entity = jid.JID(problem.bare_jid)
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1073 entity_cache = cache.setdefault(pb_entity, {})
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1074 entity_bundles = bundles.setdefault(pb_entity, {})
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1075 if problem.device in entity_cache:
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1076 entity_bundles[problem.device] = entity_cache[problem.device]
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1077 else:
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1078 found_bundles, missing = await self.getBundles(
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1079 client, pb_entity, [problem.device])
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1080 entity_cache.update(bundles)
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1081 entity_bundles.update(found_bundles)
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1082 if problem.device in missing:
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1083 missing_bundles.setdefault(pb_entity, set()).add(
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1084 problem.device)
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1085 expect_problems.setdefault(problem.bare_jid, set()).add(
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1086 problem.device)
2860
851c47cc4ae7 plugin XEP-0384: handle NoEligibleDevicesException
Goffi <goffi@goffi.org>
parents: 2859
diff changeset
1087 elif isinstance(problem, omemo_excpt.NoEligibleDevicesException):
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1088 if undecided or found_bundles:
2860
851c47cc4ae7 plugin XEP-0384: handle NoEligibleDevicesException
Goffi <goffi@goffi.org>
parents: 2859
diff changeset
1089 # we may have new devices after this run, so let's continue for now
851c47cc4ae7 plugin XEP-0384: handle NoEligibleDevicesException
Goffi <goffi@goffi.org>
parents: 2859
diff changeset
1090 continue
851c47cc4ae7 plugin XEP-0384: handle NoEligibleDevicesException
Goffi <goffi@goffi.org>
parents: 2859
diff changeset
1091 else:
851c47cc4ae7 plugin XEP-0384: handle NoEligibleDevicesException
Goffi <goffi@goffi.org>
parents: 2859
diff changeset
1092 raise problem
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1093 else:
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1094 raise problem
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1095
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1096 for peer_jid, devices in missing_bundles.items():
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1097 devices_s = [str(d) for d in devices]
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1098 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1099 _("Can't retrieve bundle for device(s) {devices} of entity {peer}, "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1100 "the message will not be readable on this/those device(s)").format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1101 devices=", ".join(devices_s), peer=peer_jid.full()))
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1102 client.feedback(
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1103 feedback_jid,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1104 D_("You're destinee {peer} has missing encryption data on some of "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1105 "his/her device(s) (bundle on device {devices}), the message won't "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1106 "be readable on this/those device.").format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1107 peer=peer_jid.full(), devices=", ".join(devices_s)))
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1108
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
1109 if undecided:
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1110 omemo_policy = self.host.memory.getParamA(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1111 PARAM_NAME, PARAM_CATEGORY, profile_key=client.profile
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1112 )
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1113 if omemo_policy == 'btbv':
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1114 # we first separate entities which have been trusted manually
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1115 manual_trust = await client._xep_0384_data.get(KEY_MANUAL_TRUST)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1116 if manual_trust:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1117 manual_undecided = {}
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1118 for hash_, pb in undecided.items():
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1119 if pb.bare_jid in manual_trust:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1120 manual_undecided[hash_] = pb
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1121 for hash_ in manual_undecided:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1122 del undecided[hash_]
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1123 else:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1124 manual_undecided = None
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1125
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1126 if undecided:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1127 # we do the automatic trust here
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1128 await self.policyBTBV(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1129 client, feedback_jid, expect_problems, undecided)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1130 if manual_undecided:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1131 # here user has to manually trust new devices from entities already
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1132 # verified
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1133 await self.policyManual(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1134 client, feedback_jid, expect_problems, manual_undecided)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1135 elif omemo_policy == 'manual':
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1136 await self.policyManual(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1137 client, feedback_jid, expect_problems, undecided)
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1138 else:
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1139 raise exceptions.InternalError(f"Unexpected OMEMO policy: {omemo_policy}")
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1140
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1141 async def encryptMessage(self, client, entity_bare_jids, message, feedback_jid=None):
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1142 if feedback_jid is None:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1143 if len(entity_bare_jids) != 1:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1144 log.error(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1145 "feedback_jid must be provided when message is encrypted for more "
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1146 "than one entities")
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1147 feedback_jid = entity_bare_jids[0]
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1148 omemo_session = client._xep_0384_session
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1149 expect_problems = {}
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1150 bundles = {}
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1151 loop_idx = 0
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1152 try:
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1153 while True:
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1154 if loop_idx > 10:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1155 msg = _("Too many iterations in encryption loop")
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1156 log.error(msg)
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1157 raise exceptions.InternalError(msg)
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1158 # encryptMessage may fail, in case of e.g. trust issue or missing bundle
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1159 try:
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1160 if not message:
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1161 encrypted = await omemo_session.encryptRatchetForwardingMessage(
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1162 entity_bare_jids,
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1163 bundles,
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1164 expect_problems = expect_problems)
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1165 else:
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1166 encrypted = await omemo_session.encryptMessage(
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1167 entity_bare_jids,
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1168 message,
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1169 bundles,
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1170 expect_problems = expect_problems)
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1171 except omemo_excpt.EncryptionProblemsException as e:
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1172 # we know the problem to solve, we can try to fix them
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1173 await self.handleProblems(
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1174 client,
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1175 feedback_jid=feedback_jid,
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1176 bundles=bundles,
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1177 expect_problems=expect_problems,
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1178 problems=e.problems)
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1179 loop_idx += 1
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1180 else:
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1181 break
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1182 except Exception as e:
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1183 msg = _("Can't encrypt message for {entities}: {reason}".format(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1184 entities=', '.join(e.full() for e in entity_bare_jids), reason=e))
2859
4e875d9eea48 plugin XEP-0384: give feedback to client when encryption failed
Goffi <goffi@goffi.org>
parents: 2858
diff changeset
1185 log.warning(msg)
4e875d9eea48 plugin XEP-0384: give feedback to client when encryption failed
Goffi <goffi@goffi.org>
parents: 2858
diff changeset
1186 extra = {C.MESS_EXTRA_INFO: C.EXTRA_INFO_ENCR_ERR}
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1187 client.feedback(feedback_jid, msg, extra)
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
1188 raise e
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1189
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1190 defer.returnValue(encrypted)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1191
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1192 @defer.inlineCallbacks
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1193 def _messageReceivedTrigger(self, client, message_elt, post_treat):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1194 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1195 encrypted_elt = next(message_elt.elements(NS_OMEMO, "encrypted"))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1196 except StopIteration:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1197 # no OMEMO message here
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1198 defer.returnValue(True)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1199
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1200 # we have an encrypted message let's decrypt it
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1201
2654
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
1202 from_jid = jid.JID(message_elt['from'])
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1203
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1204 if message_elt.getAttribute("type") == C.MESS_TYPE_GROUPCHAT:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1205 # with group chat, we must get the real jid for decryption
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1206 # and use the room as feedback_jid
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1207
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1208 if self._m is None:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1209 # plugin XEP-0045 (MUC) is not available
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1210 defer.returnValue(True)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1211
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1212 room_jid = from_jid.userhostJID()
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1213 feedback_jid = room_jid
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1214 if self._sid is not None:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1215 mess_id = self._sid.getOriginId(message_elt)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1216 else:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1217 mess_id = None
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1218
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1219 if mess_id is None:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1220 mess_id = message_elt.getAttribute('id')
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1221 cache_key = (room_jid, mess_id)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1222
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1223 try:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1224 room = self._m.getRoom(client, room_jid)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1225 except exceptions.NotFound:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1226 log.warning(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1227 f"Received an OMEMO encrypted msg from a room {room_jid} which has "
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1228 f"not been joined, ignoring")
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1229 defer.returnValue(True)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1230
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1231 user = room.getUser(from_jid.resource)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1232 if user is None:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1233 log.warning(f"Can't find user {user} in room {room_jid}, ignoring")
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1234 defer.returnValue(True)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1235 if not user.entity:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1236 log.warning(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1237 f"Real entity of user {user} in room {room_jid} can't be established,"
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1238 f" OMEMO encrypted message can't be decrypted")
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1239 defer.returnValue(True)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1240
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1241 # now we have real jid of the entity, we use it instead of from_jid
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1242 from_jid = user.entity.userhostJID()
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1243
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1244 else:
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1245 # we have a one2one message, we can user "from" and "to" normally
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1246
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1247 if from_jid.userhostJID() == client.jid.userhostJID():
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1248 feedback_jid = jid.JID(message_elt['to'])
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1249 else:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1250 feedback_jid = from_jid
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1251
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1252
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1253 if (message_elt.getAttribute("type") == C.MESS_TYPE_GROUPCHAT
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1254 and mess_id is not None
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1255 and cache_key in client._xep_0384_muc_cache):
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1256 plaintext = client._xep_0384_muc_cache.pop(cache_key)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1257 if not client._xep_0384_muc_cache:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1258 client._xep_0384_muc_cache_timer.cancel()
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1259 client._xep_0384_muc_cache_timer = None
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1260 else:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1261 try:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1262 omemo_session = client._xep_0384_session
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1263 except AttributeError:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1264 # on startup, message can ve received before session actually exists
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1265 # so we need to synchronise here
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1266 yield client._xep_0384_ready
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1267 omemo_session = client._xep_0384_session
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1268
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1269 device_id = client._xep_0384_device_id
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1270 try:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1271 header_elt = next(encrypted_elt.elements(NS_OMEMO, 'header'))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1272 iv_elt = next(header_elt.elements(NS_OMEMO, 'iv'))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1273 except StopIteration:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1274 log.warning(_("Invalid OMEMO encrypted stanza, ignoring: {xml}")
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1275 .format(xml=message_elt.toXml()))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1276 defer.returnValue(False)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1277 try:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1278 s_device_id = header_elt['sid']
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1279 except KeyError:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1280 log.warning(_("Invalid OMEMO encrypted stanza, missing sender device ID, "
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1281 "ignoring: {xml}")
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1282 .format(xml=message_elt.toXml()))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1283 defer.returnValue(False)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1284 try:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1285 key_elt = next((e for e in header_elt.elements(NS_OMEMO, 'key')
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1286 if int(e['rid']) == device_id))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1287 except StopIteration:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1288 log.warning(_("This OMEMO encrypted stanza has not been encrypted "
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1289 "for our device (device_id: {device_id}, fingerprint: "
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1290 "{fingerprint}): {xml}").format(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1291 device_id=device_id,
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1292 fingerprint=omemo_session.public_bundle.ik.hex().upper(),
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1293 xml=encrypted_elt.toXml()))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1294 user_msg = (D_("An OMEMO message from {sender} has not been encrypted for "
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1295 "our device, we can't decrypt it").format(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1296 sender=from_jid.full()))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1297 extra = {C.MESS_EXTRA_INFO: C.EXTRA_INFO_DECR_ERR}
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1298 client.feedback(feedback_jid, user_msg, extra)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1299 defer.returnValue(False)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1300 except ValueError as e:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1301 log.warning(_("Invalid recipient ID: {msg}".format(msg=e)))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1302 defer.returnValue(False)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1303 is_pre_key = C.bool(key_elt.getAttribute('prekey', 'false'))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1304 payload_elt = next(encrypted_elt.elements(NS_OMEMO, 'payload'), None)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1305 additional_information = {
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1306 "from_storage": bool(message_elt.delay)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1307 }
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1308
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1309 kwargs = {
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1310 "bare_jid": from_jid.userhostJID(),
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1311 "device": s_device_id,
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1312 "iv": base64.b64decode(bytes(iv_elt)),
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1313 "message": base64.b64decode(bytes(key_elt)),
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1314 "is_pre_key_message": is_pre_key,
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1315 "additional_information": additional_information,
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1316 }
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1317
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1318 try:
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1319 if payload_elt is None:
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1320 omemo_session.decryptRatchetForwardingMessage(**kwargs)
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1321 plaintext = None
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1322 else:
3240
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1323 kwargs["ciphertext"] = base64.b64decode(bytes(payload_elt))
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1324 try:
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1325 plaintext = yield omemo_session.decryptMessage(**kwargs)
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1326 except omemo_excpt.TrustException:
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1327 post_treat.addCallback(client.encryption.markAsUntrusted)
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1328 kwargs['allow_untrusted'] = True
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1329 plaintext = yield omemo_session.decryptMessage(**kwargs)
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1330 else:
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1331 post_treat.addCallback(client.encryption.markAsTrusted)
d85b68e44297 plugin XEP-0384: fixed /omemo_reset + device ID type:
Goffi <goffi@goffi.org>
parents: 3237
diff changeset
1332 plaintext = plaintext.decode()
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1333 except Exception as e:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1334 log.warning(_("Can't decrypt message: {reason}\n{xml}").format(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1335 reason=e, xml=message_elt.toXml()))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1336 user_msg = (D_(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1337 "An OMEMO message from {sender} can't be decrypted: {reason}")
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1338 .format(sender=from_jid.full(), reason=e))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1339 extra = {C.MESS_EXTRA_INFO: C.EXTRA_INFO_DECR_ERR}
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1340 client.feedback(feedback_jid, user_msg, extra)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1341 defer.returnValue(False)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1342 finally:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1343 if omemo_session.republish_bundle:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1344 # we don't wait for the Deferred (i.e. no yield) on purpose
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1345 # there is no need to block the whole message workflow while
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1346 # updating the bundle
3214
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
1347 defer.ensureDeferred(
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
1348 self.setBundle(client, omemo_session.public_bundle, device_id)
8d92d4d829fb plugin XEP-0384: use "max_items=1" for devices and bundles nodes:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
1349 )
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1350
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1351 message_elt.children.remove(encrypted_elt)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1352 if plaintext:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1353 message_elt.addElement("body", content=plaintext)
3231
e756e0eb1be4 core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents: 3218
diff changeset
1354 post_treat.addCallback(client.encryption.markAsEncrypted, namespace=NS_OMEMO)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1355 defer.returnValue(True)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1356
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1357 def getJIDsForRoom(self, client, room_jid):
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1358 if self._m is None:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1359 exceptions.InternalError("XEP-0045 plugin missing, can't encrypt for group chat")
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1360 room = self._m.getRoom(client, room_jid)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1361 return [u.entity.userhostJID() for u in room.roster.values()]
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1362
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1363 def _expireMUCCache(self, client):
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1364 client._xep_0384_muc_cache_timer = None
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1365 for (room_jid, uid), msg in client._xep_0384_muc_cache.items():
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1366 client.feedback(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1367 room_jid,
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1368 D_("Our message with UID {uid} has not been received in time, it has "
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1369 "probably been lost. The message was: {msg!r}").format(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1370 uid=uid, msg=str(msg)))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1371
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1372 client._xep_0384_muc_cache.clear()
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1373 log.warning("Cache for OMEMO MUC has expired")
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1374
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1375 @defer.inlineCallbacks
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1376 def _sendMessageDataTrigger(self, client, mess_data):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1377 encryption = mess_data.get(C.MESS_KEY_ENCRYPTION)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1378 if encryption is None or encryption['plugin'].namespace != NS_OMEMO:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1379 return
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1380 message_elt = mess_data["xml"]
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1381 if mess_data['type'] == C.MESS_TYPE_GROUPCHAT:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1382 feedback_jid = room_jid = mess_data['to']
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1383 to_jids = self.getJIDsForRoom(client, room_jid)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1384 else:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1385 feedback_jid = to_jid = mess_data["to"].userhostJID()
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1386 to_jids = [to_jid]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1387 log.debug("encrypting message")
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1388 body = None
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1389 for child in list(message_elt.children):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1390 if child.name == "body":
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1391 # we remove all unencrypted body,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1392 # and will only encrypt the first one
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1393 if body is None:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1394 body = child
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1395 message_elt.children.remove(child)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1396 elif child.name == "html":
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1397 # we don't want any XHTML-IM element
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1398 message_elt.children.remove(child)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1399
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1400 if body is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1401 log.warning("No message found")
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1402 return
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1403
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1404 body = str(body)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1405
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1406 if mess_data['type'] == C.MESS_TYPE_GROUPCHAT:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1407 key = (room_jid, mess_data['uid'])
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1408 # XXX: we can't encrypt message for our own device for security reason
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1409 # so we keep the plain text version in cache until we receive the
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1410 # message. We don't send it directly to bridge to keep a workflow
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1411 # similar to plain text MUC, so when we see it in frontend we know
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1412 # that it has been sent correctly.
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1413 client._xep_0384_muc_cache[key] = body
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1414 timer = client._xep_0384_muc_cache_timer
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1415 if timer is None:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1416 client._xep_0384_muc_cache_timer = reactor.callLater(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1417 MUC_CACHE_TTL, self._expireMUCCache, client)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1418 else:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1419 timer.reset(MUC_CACHE_TTL)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1420 # we use origin-id when possible, to identifiy the message in a stable way
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1421 if self._sid is not None:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1422 self._sid.addOriginId(message_elt, mess_data['uid'])
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1423
3237
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1424 encryption_data = yield defer.ensureDeferred(self.encryptMessage(
b0c57c9a4bd8 plugin XEP-0384: OMEMO trust policy:
Goffi <goffi@goffi.org>
parents: 3236
diff changeset
1425 client, to_jids, body, feedback_jid=feedback_jid))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1426
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1427 encrypted_elt = message_elt.addElement((NS_OMEMO, 'encrypted'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1428 header_elt = encrypted_elt.addElement('header')
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1429 header_elt['sid'] = str(encryption_data['sid'])
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1430
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1431 for to_jid in to_jids:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1432 bare_jid_s = to_jid.userhost()
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1433
3104
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1434 for rid, data in encryption_data['keys'][bare_jid_s].items():
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1435 key_elt = header_elt.addElement(
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1436 'key',
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1437 content=b64enc(data['data']))
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1438 key_elt['rid'] = str(rid)
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1439 if data['pre_key']:
118d91c932a7 plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents: 3098
diff changeset
1440 key_elt['prekey'] = 'true'
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1441
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1442 header_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1443 'iv',
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1444 content=b64enc(encryption_data['iv']))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1445 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1446 encrypted_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1447 'payload',
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1448 content=b64enc(encryption_data['payload']))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1449 except KeyError:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1450 pass