annotate sat/plugins/plugin_xep_0384.py @ 3101:ab7e8ade848a

plugin XEP-0045: added room statuses to metadata: room statuses are now sent with other metadata on bridge signals, and saved in Room instance. They give useful data on the room, for instance they can be used to know if a full jid is used in this room.
author Goffi <goffi@goffi.org>
date Mon, 30 Dec 2019 20:44:02 +0100
parents 518208085dfb
children 118d91c932a7
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
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2757
diff changeset
4 # Copyright (C) 2009-2019 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
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
19 from sat.core.i18n import _, D_
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.constants import Const as C
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.log import getLogger
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core import exceptions
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.internet import defer
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.xish import domish
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import jid
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber import error
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.memory import persistent
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from functools import partial
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
29 from sat.tools import xml_tools
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
30 import logging
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 import random
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 import base64
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 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
35 from omemo import exceptions as omemo_excpt
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 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
37 except ImportError:
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 raise exceptions.MissingModule(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
39 'Missing module omemo, please download/install it. You can use '
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
40 '"pip install omemo"'
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 )
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
42 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
43 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
44 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
45 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
46 '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
47 '"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
48 )
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 log = getLogger(__name__)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 PLUGIN_INFO = {
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
53 C.PI_NAME: "OMEMO",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
54 C.PI_IMPORT_NAME: "XEP-0384",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
55 C.PI_TYPE: "SEC",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
56 C.PI_PROTOCOLS: ["XEP-0384"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
57 C.PI_DEPENDENCIES: ["XEP-0163", "XEP-0280", "XEP-0334", "XEP-0060"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
58 C.PI_MAIN: "OMEMO",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
59 C.PI_HANDLER: "no",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
60 C.PI_DESCRIPTION: _("""Implementation of OMEMO"""),
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 }
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
63 OMEMO_MIN_VER = (0, 11, 0)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 NS_OMEMO = "eu.siacs.conversations.axolotl"
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 NS_OMEMO_DEVICES = NS_OMEMO + ".devicelist"
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 NS_OMEMO_BUNDLE = NS_OMEMO + ".bundles:{device_id}"
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 KEY_STATE = "STATE"
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 KEY_DEVICE_ID = "DEVICE_ID"
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 KEY_SESSION = "SESSION"
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
70 KEY_TRUST = "TRUST"
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 KEY_ACTIVE_DEVICES = "DEVICES"
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
72 KEY_INACTIVE_DEVICES = "INACTIVE_DEVICES"
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
73 KEY_ALL_JIDS = "ALL_JIDS"
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
76 # 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
77
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
78 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
79
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
80 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
81 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
82
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
83 @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
84 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
85 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
86 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
87 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
88
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
89
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 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
91
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
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 def b64enc(data):
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
94 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
95
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
96
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
97 def promise2Deferred(promise_):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
98 """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
99
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
100 @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
101 @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
102 """
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
103 d = defer.Deferred()
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
104 promise_.then(d.callback, d.errback)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
105 return d
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 class OmemoStorage(omemo.Storage):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
110 def __init__(self, client, device_id, all_jids, persistent_dict):
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 """
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 @param persistent_dict(persistent.LazyPersistentBinaryDict): object which will
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 store data in SàT database
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 """
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
115 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
116 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
117 self.all_jids = all_jids
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 self.data = persistent_dict
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 @property
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 def is_async(self):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 return True
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 def setCb(self, deferred, callback):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 """Associate Deferred and callback
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 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
128 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
129 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
130 Deferred is called
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 deferred.addCallback(partial(callback, True))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 deferred.addErrback(partial(callback, False))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
135 def _checkJid(self, bare_jid):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
136 """Check if jid is know, and store it if not
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
137
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
138 @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
139 @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
140 """
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
141 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
142 return defer.succeed(None)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
143 else:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
144 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
145 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
146 return d
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
147
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
148 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
149 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
150 '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
151
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
152 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
153 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
154 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
155 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
156
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 def loadState(self, callback):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 d = self.data.get(KEY_STATE)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160
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
161 def storeState(self, callback, state):
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
162 d = self.data.force(KEY_STATE, state)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164
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
165 def loadSession(self, callback, bare_jid, device_id):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
166 key = '\n'.join([KEY_SESSION, bare_jid, str(device_id)])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 d = self.data.get(key)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169
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
170 def storeSession(self, callback, bare_jid, device_id, session):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
171 key = '\n'.join([KEY_SESSION, bare_jid, str(device_id)])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 d = self.data.force(key, session)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
175 def deleteSession(self, callback, bare_jid, device_id):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
176 key = '\n'.join([KEY_SESSION, 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
177 d = self.data.remove(key)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
178 self.setCb(d, callback)
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 loadActiveDevices(self, callback, bare_jid):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
181 key = '\n'.join([KEY_ACTIVE_DEVICES, bare_jid])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 d = self.data.get(key, {})
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
183 if callback is not None:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
184 self.setCb(d, callback)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
185 return d
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
186
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
187 def loadInactiveDevices(self, callback, bare_jid):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
188 key = '\n'.join([KEY_INACTIVE_DEVICES, bare_jid])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 d = self.data.get(key, {})
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
190 if callback is not None:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
191 self.setCb(d, callback)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
192 return d
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
193
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
194 def storeActiveDevices(self, callback, bare_jid, devices):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
195 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
196 d = self._checkJid(bare_jid)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
197 d.addCallback(lambda _: self.data.force(key, devices))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
199
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
200 def storeInactiveDevices(self, callback, bare_jid, devices):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
201 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
202 d = self._checkJid(bare_jid)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
203 d.addCallback(lambda _: self.data.force(key, devices))
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
204 self.setCb(d, callback)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
205
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
206 def storeTrust(self, callback, bare_jid, device_id, trust):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
207 key = '\n'.join([KEY_TRUST, 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
208 d = self.data.force(key, trust)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
210
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
211 def loadTrust(self, callback, bare_jid, device_id):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
212 key = '\n'.join([KEY_TRUST, 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
213 d = self.data.get(key)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
214 if callback is not None:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
215 self.setCb(d, callback)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
216 return d
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
217
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
218 def listJIDs(self, callback):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
219 d = defer.succeed(self.all_jids)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
220 if callback is not None:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
221 self.setCb(d, callback)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
222 return d
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
223
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
224 def _deleteJID_logResults(self, results):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
225 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
226 if failed:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
227 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
228 "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
229 .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
230 else:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
231 log.info(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
232 "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
233 .format(total_count=len(results)))
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_gotDevices(self, results, bare_jid):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
236 assert len(results) == 2
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
237 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
238 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
239 d_list = []
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
240 for success, devices in results:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
241 if not success:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
242 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
243 .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
244 else:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
245 for device_id in devices:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
246 for key in (KEY_SESSION, KEY_TRUST):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
247 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
248 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
249
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
250 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
251 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
252 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
253 # 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
254 # 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
255 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
256 d = defer.DeferredList(d_list)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
257 d.addCallback(self._deleteJID_logResults)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
258 return d
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
259
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
260 def deleteJID(self, callback, bare_jid):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
261 """Retrieve all (in)actives of bare_jid, and delete all related keys"""
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
262 d_list = []
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
263
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
264 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
265 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
266
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
267 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
268 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
269 # 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
270 # 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
271 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
272
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
273 d_list.append(d_inactive)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
274 d = defer.DeferredList(d_list)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
275 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
276 if callback is not None:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
277 self.setCb(d, callback)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
278 return d
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
279
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
280
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
281 class SatOTPKPolicy(omemo.DefaultOTPKPolicy):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
282 pass
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
283
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
284
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 class OmemoSession(object):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 """Wrapper to use omemo.OmemoSession with Deferred"""
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
287
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 def __init__(self, session):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 self._session = session
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
290
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 @property
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
292 def republish_bundle(self):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
293 return self._session.republish_bundle
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
294
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
295 @property
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
296 def public_bundle(self):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
297 return self._session.public_bundle
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
298
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 @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
300 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
301 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
302 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
303 SatOTPKPolicy,
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
304 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
305 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
306 my_device_id)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
307 d = promise2Deferred(omemo_session_p)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 d.addCallback(lambda session: cls(session))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 return d
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
310
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
311 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
312 jid = jid.userhost()
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
313 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
314 return promise2Deferred(new_device_p)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
315
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 def getDevices(self, bare_jid=None):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 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
318 return promise2Deferred(get_devices_p)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
319
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 def buildSession(self, bare_jid, device, bundle):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 bare_jid = bare_jid.userhost()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 build_session_p = self._session.buildSession(bare_jid, device, bundle)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
323 return promise2Deferred(build_session_p)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
324
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
325 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
326 """Encrypt a message
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
327
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 @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
329 @param message(unicode): message to encode
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
330 @param bundles(dict[jid.JID, dict[int, ExtendedPublicBundle]):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 entities => devices => bundles map
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
332 @return D(dict): encryption data
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 if isinstance(bare_jids, jid.JID):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 bare_jids = bare_jids.userhost()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 else:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
337 bare_jids = [e.userhost() for e in bare_jids]
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 if bundles is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
339 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
340 encrypt_mess_p = self._session.encryptMessage(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
341 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
342 plaintext=message.encode(),
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
343 bundles=bundles,
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
344 expect_problems=expect_problems)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
345 return promise2Deferred(encrypt_mess_p)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
346
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 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
348 ciphertext, additional_information=None, allow_untrusted=False):
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 bare_jid = bare_jid.userhost()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
350 decrypt_mess_p = self._session.decryptMessage(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
351 bare_jid=bare_jid,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
352 device=device,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
353 iv=iv,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
354 message=message,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
355 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
356 ciphertext=ciphertext,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
357 additional_information=additional_information,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
358 allow_untrusted=allow_untrusted
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
359 )
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
360 return promise2Deferred(decrypt_mess_p)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
361
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
362 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
363 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
364 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
365 bare_jid=bare_jid,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
366 device=device,
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
367 key=key,
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
368 trusted=trusted,
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
369 )
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
370 return promise2Deferred(setTrust_p)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
371
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
372 def getTrustForJID(self, bare_jid):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
373 bare_jid = bare_jid.userhost()
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
374 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
375 return promise2Deferred(get_trust_p)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
376
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
377
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
378 class OMEMO(object):
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
379
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
380 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
381 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
382 version=omemo.__version__))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
383 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
384 if version < OMEMO_MIN_VER:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
385 log.warning(_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
386 "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
387 "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
388 raise exceptions.CancelError("module is too old")
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
389 self.host = host
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
390 self._p_hints = host.plugins["XEP-0334"]
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
391 self._p_carbons = host.plugins["XEP-0280"]
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
392 self._p = host.plugins["XEP-0060"]
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 host.trigger.add("MessageReceived", self._messageReceivedTrigger, priority=100050)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
394 host.trigger.add("sendMessageData", self._sendMessageDataTrigger)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
395 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
396 pep = host.plugins['XEP-0163']
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
397 pep.addPEPEvent("OMEMO_DEVICES", NS_OMEMO_DEVICES, self.onNewDevices)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
398
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
399 @defer.inlineCallbacks
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
400 def trustUICb(self, xmlui_data, trust_data, expect_problems=None,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
401 profile=C.PROF_KEY_NONE):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
402 if C.bool(xmlui_data.get('cancelled', 'false')):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
403 defer.returnValue({})
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
404 client = self.host.getClient(profile)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
405 session = client._xep_0384_session
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
406 answer = xml_tools.XMLUIResult2DataFormResult(xmlui_data)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
407 for key, value in answer.items():
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
408 if key.startswith('trust_'):
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
409 trust_id = key[6:]
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
410 else:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
411 continue
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
412 data = trust_data[trust_id]
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
413 trust = C.bool(value)
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
414 yield session.setTrust(
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
415 data["jid"],
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
416 data["device"],
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
417 data["ik"],
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
418 trusted=trust,
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
419 )
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
420 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
421 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
422 data['device']
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
423 )
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
424 defer.returnValue({})
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
425
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
426 @defer.inlineCallbacks
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
427 def getTrustUI(self, client, entity_jid=None, trust_data=None, submit_id=None):
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
428 """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
429
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
430 @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
431 None to use trust_data
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
432 @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
433 None to use entity_jid
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
434 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
435 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
436 - 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
437 - device(int): device id
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
438 - ik(bytes): identity key
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
439 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
440 - 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
441 @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
442 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
443 @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
444 """
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
445 # 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
446 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
447 if entity_jid and entity_jid.resource:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
448 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
449
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
450 session = client._xep_0384_session
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
451
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
452 if trust_data is None:
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
453 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
454 trust_data = {}
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
455 trust_session_data = yield session.getTrustForJID(entity_jid)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
456 bare_jid_s = entity_jid.userhost()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
457 for device_id, trust_info in trust_session_data['active'].items():
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
458 if trust_info is None:
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
459 # device has never been (un)trusted, we have to retrieve its
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
460 # fingerprint (i.e. identity key or "ik") through public bundle
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
461 if device_id not in cache:
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
462 bundles, missing = yield self.getBundles(client,
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
463 entity_jid,
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
464 [device_id])
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
465 if device_id not in bundles:
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
466 log.warning(_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
467 "Can't find bundle for device {device_id} of user "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
468 "{bare_jid}, ignoring").format(device_id=device_id,
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
469 bare_jid=bare_jid_s))
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
470 continue
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
471 cache[device_id] = bundles[device_id]
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
472 # TODO: replace False below by None when undecided
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
473 # trusts are handled
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
474 trust_info = {
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
475 "key": cache[device_id].ik,
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
476 "trusted": False
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
477 }
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
478
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
479 ik = trust_info["key"]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
480 trust_id = str(hash((bare_jid_s, device_id, ik)))
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
481 trust_data[trust_id] = {
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
482 "jid": entity_jid,
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
483 "device": device_id,
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
484 "ik": ik,
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
485 "trusted": trust_info["trusted"],
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
486 }
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
487
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
488 if submit_id is None:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
489 submit_id = self.host.registerCallback(partial(self.trustUICb,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
490 trust_data=trust_data),
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
491 with_data=True,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
492 one_shot=True)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
493 xmlui = xml_tools.XMLUI(
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
494 panel_type = C.XMLUI_FORM,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
495 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
496 submit_id = submit_id
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
497 )
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
498 xmlui.addText(D_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
499 "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
500 "contacts, and a checkbox to trust them or not. A trusted device "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
501 "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
502 "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
503 "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
504 "you can check the \"fingerprint\" (the number next to the device) "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
505 "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
506
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
507 xmlui.changeContainer("label")
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
508 xmlui.addLabel(D_("This device ID"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
509 xmlui.addText(str(client._xep_0384_device_id))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
510 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
511 ik_hex = session.public_bundle.ik.hex().upper()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
512 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
513 xmlui.addText(fp_human)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
514 xmlui.addEmpty()
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
515 xmlui.addEmpty()
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
516
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
517
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
518 for trust_id, data in trust_data.items():
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
519 xmlui.addLabel(D_("Contact"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
520 xmlui.addJid(data['jid'])
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
521 xmlui.addLabel(D_("Device ID"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
522 xmlui.addText(str(data['device']))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
523 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
524 ik_hex = data['ik'].hex().upper()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
525 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
526 xmlui.addText(fp_human)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
527 xmlui.addLabel(D_("Trust this device?"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
528 xmlui.addBool("trust_{}".format(trust_id),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
529 value=C.boolConst(data.get('trusted', False)))
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
530
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
531 xmlui.addEmpty()
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
532 xmlui.addEmpty()
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
533
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
534 defer.returnValue(xmlui)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
535
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
536 @defer.inlineCallbacks
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
537 def profileConnected(self, client):
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
538 # 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
539 # Workflow should be checked
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
540 client._xep_0384_ready = defer.Deferred()
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
541 # 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
542 persistent_dict = persistent.LazyPersistentBinaryDict("XEP-0384", client.profile)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
543 # all known devices of profile
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
544 devices = yield self.getDevices(client)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
545 # and our own device id
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
546 device_id = yield persistent_dict.get(KEY_DEVICE_ID)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
547 if device_id is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
548 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
549 # we have a new device, we create device_id
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
550 device_id = random.randint(1, 2**31-1)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
551 # we check that it's really unique
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
552 while device_id in devices:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
553 device_id = random.randint(1, 2**31-1)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
554 # and we save it
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
555 persistent_dict[KEY_DEVICE_ID] = device_id
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
556
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
557 if device_id not in devices:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
558 devices.add(device_id)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
559 yield self.setDevices(client, devices)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
560
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
561 all_jids = yield persistent_dict.get(KEY_ALL_JIDS, set())
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
562
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
563 omemo_storage = OmemoStorage(client, device_id, all_jids, persistent_dict)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
564 omemo_session = yield 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
565 client._xep_0384_cache = {}
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
566 client._xep_0384_session = omemo_session
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
567 client._xep_0384_device_id = device_id
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
568 yield omemo_session.newDeviceList(client.jid, devices)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
569 if omemo_session.republish_bundle:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
570 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
571 device_id=device_id))
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
572 yield self.setBundle(client, omemo_session.public_bundle, device_id)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
573 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
574 del client._xep_0384_ready
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
575
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
576 ## XMPP PEP nodes manipulation
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
577
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
578 # devices
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
579
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
580 def parseDevices(self, items):
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
581 """Parse devices found in items
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
582
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
583 @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
584 @return set[int]: parsed devices
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
585 """
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
586 devices = set()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
587 if len(items) > 1:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
588 log.warning(_("OMEMO devices list is stored in more that one items, "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
589 "this is not expected"))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
590 if items:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
591 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
592 list_elt = next(items[0].elements(NS_OMEMO, 'list'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
593 except StopIteration:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
594 log.warning(_("no list element found in OMEMO devices list"))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
595 return
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
596 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
597 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
598 device_id = int(device_elt['id'])
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
599 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
600 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
601 .format(elt=device_elt.toXml()))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
602 except ValueError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
603 log.warning(_('invalid device id: {device_id}').format(
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
604 device_id=device_elt['id']))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
605 else:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
606 devices.add(device_id)
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
607 return devices
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
608
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
609 @defer.inlineCallbacks
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
610 def getDevices(self, client, entity_jid=None):
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
611 """Retrieve list of registered OMEMO devices
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
612
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
613 @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
614 None to get our own devices
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
615 @return (set(int)): list of devices
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
616 """
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
617 if entity_jid is not None:
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
618 assert not entity_jid.resource
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
619 try:
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
620 items, metadata = yield self._p.getItems(client, entity_jid, NS_OMEMO_DEVICES)
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
621 except error.StanzaError as e:
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
622 if e.condition == 'item-not-found':
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
623 log.info(_("there is no node to handle OMEMO devices"))
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
624 defer.returnValue(set())
2745
3ee396b2ecf3 plugin XEP-0384: don't ignore StanzaError if it's not item-not-found
Goffi <goffi@goffi.org>
parents: 2744
diff changeset
625 raise e
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
626
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
627 devices = self.parseDevices(items)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
628 defer.returnValue(devices)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
629
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
630 def setDevicesEb(self, failure_):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
631 log.warning(_("Can't set devices: {reason}").format(reason=failure_))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
632
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
633 def setDevices(self, client, devices):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
634 list_elt = domish.Element((NS_OMEMO, 'list'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
635 for device in devices:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
636 device_elt = list_elt.addElement('device')
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
637 device_elt['id'] = str(device)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
638 d = self._p.sendItem(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
639 client, None, NS_OMEMO_DEVICES, list_elt, item_id=self._p.ID_SINGLETON)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
640 d.addErrback(self.setDevicesEb)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
641 return d
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
642
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
643 # bundles
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
644
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
645 @defer.inlineCallbacks
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
646 def getBundles(self, client, entity_jid, devices_ids):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
647 """Retrieve public bundles of an entity devices
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
649 @param entity_jid(jid.JID): bare jid of entity
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
650 @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
651 @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
652 - bundles collection:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
653 * key is device_id
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
654 * value is parsed bundle
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
655 - set of bundles not found
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
656 """
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
657 assert not entity_jid.resource
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
658 bundles = {}
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
659 missing = set()
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
660 for device_id in devices_ids:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
661 node = NS_OMEMO_BUNDLE.format(device_id=device_id)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
662 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
663 items, metadata = yield self._p.getItems(client, entity_jid, node)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
664 except error.StanzaError as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
665 if e.condition == "item-not-found":
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
666 log.warning(_("Bundle missing for device {device_id}")
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
667 .format(device_id=device_id))
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
668 missing.add(device_id)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
669 continue
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
670 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
671 log.warning(_("Can't get bundle for device {device_id}: {reason}")
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
672 .format(device_id=device_id, reason=e))
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
673 continue
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
674 if not items:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
675 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
676 "for device {device_id}").format(node=node,
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
677 device_id=device_id))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
678 continue
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
679 if len(items) > 1:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
680 log.warning(_("more than one item found in {node}, "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
681 "this is not expected").format(node=node))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
682 item = items[0]
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
683 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
684 bundle_elt = next(item.elements(NS_OMEMO, 'bundle'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
685 signedPreKeyPublic_elt = next(bundle_elt.elements(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
686 NS_OMEMO, 'signedPreKeyPublic'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
687 signedPreKeySignature_elt = next(bundle_elt.elements(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
688 NS_OMEMO, 'signedPreKeySignature'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
689 identityKey_elt = next(bundle_elt.elements(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
690 NS_OMEMO, 'identityKey'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
691 prekeys_elt = next(bundle_elt.elements(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
692 NS_OMEMO, 'prekeys'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
693 except StopIteration:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
694 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
695 device_id=device_id))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
696 continue
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
697
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
698 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
699 spkPublic = base64.b64decode(str(signedPreKeyPublic_elt))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
700 spkSignature = base64.b64decode(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
701 str(signedPreKeySignature_elt))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
702
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
703 ik = base64.b64decode(str(identityKey_elt))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
704 spk = {
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
705 "key": spkPublic,
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
706 "id": int(signedPreKeyPublic_elt['signedPreKeyId'])
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
707 }
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
708 otpks = []
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
709 for preKeyPublic_elt in prekeys_elt.elements(NS_OMEMO, 'preKeyPublic'):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
710 preKeyPublic = base64.b64decode(str(preKeyPublic_elt))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
711 otpk = {
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
712 "key": preKeyPublic,
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
713 "id": int(preKeyPublic_elt['preKeyId'])
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
714 }
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
715 otpks.append(otpk)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
716
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
717 except Exception as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
718 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
719 .format(device_id=device_id, msg=e))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
720 continue
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
721
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
722 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
723 spkSignature, otpks)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
724
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
725 defer.returnValue((bundles, missing))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
726
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
727 def setBundleEb(self, failure_):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
728 log.warning(_("Can't set bundle: {reason}").format(reason=failure_))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
729
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
730 def setBundle(self, client, bundle, device_id):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
731 """Set public bundle for this device.
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
732
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
733 @param bundle(ExtendedPublicBundle): bundle to publish
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
734 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
735 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
736 bundle = bundle.serialize(omemo_backend)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
737 bundle_elt = domish.Element((NS_OMEMO, 'bundle'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
738 signedPreKeyPublic_elt = bundle_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
739 "signedPreKeyPublic",
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
740 content=b64enc(bundle["spk"]['key']))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
741 signedPreKeyPublic_elt['signedPreKeyId'] = str(bundle["spk"]['id'])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
742
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
743 bundle_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
744 "signedPreKeySignature",
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
745 content=b64enc(bundle["spk_signature"]))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
746
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
747 bundle_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
748 "identityKey",
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
749 content=b64enc(bundle["ik"]))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
750
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
751 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
752 for otpk in bundle["otpks"]:
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
753 preKeyPublic_elt = prekeys_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
754 'preKeyPublic',
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
755 content=b64enc(otpk["key"]))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
756 preKeyPublic_elt['preKeyId'] = str(otpk['id'])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
757
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
758 node = NS_OMEMO_BUNDLE.format(device_id=device_id)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
759 d = self._p.sendItem(client, None, node, bundle_elt, item_id=self._p.ID_SINGLETON)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
760 d.addErrback(self.setBundleEb)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
761 return d
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
762
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
763 ## PEP node events callbacks
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
764
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
765 @defer.inlineCallbacks
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
766 def onNewDevices(self, itemsEvent, profile):
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
767 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
768 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
769 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
770 except AttributeError:
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
771 yield client._xep_0384_ready
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
772 omemo_session = client._xep_0384_session
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
773 entity = itemsEvent.sender
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
774
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
775 devices = self.parseDevices(itemsEvent.items)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
776 omemo_session.newDeviceList(entity, devices)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
777
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
778 if entity == client.jid.userhostJID():
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
779 own_device = client._xep_0384_device_id
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
780 if own_device not in devices:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
781 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
782 devices.add(own_device)
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
783 yield self.setDevices(client, devices)
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
784
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
785 ## triggers
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
786
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
787 @defer.inlineCallbacks
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
788 def handleProblems(self, client, entity, bundles, expect_problems, problems):
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
789 """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
790
2757
1f612547fb2e plugin XEP-0384: give feedback to user when trust must be handled before sending a message
Goffi <goffi@goffi.org>
parents: 2754
diff changeset
791 @param entity(jid.JID): bare jid of the destinee
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
792 @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
793 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
794 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
795 This dict is updated
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
796 @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
797 @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
798 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
799 (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
800 This dict is updated
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
801 """
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
802 # 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
803 undecided = {}
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
804 missing_bundles = {}
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
805 found_bundles = None
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
806 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
807 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
808 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
809 if problem.problem == 'undecided':
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
810 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
811 elif problem.problem == 'untrusted':
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
812 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
813 problem.device)
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
814 log.info(_(
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
815 "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
816 "for {entity}").format(
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
817 device_id=problem.device,
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
818 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
819 entity=problem.bare_jid,
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
820 )
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
821 )
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
822 else:
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
823 log.error(
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
824 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
825 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
826 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
827 problem.device)
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
828 elif isinstance(problem, omemo_excpt.MissingBundleException):
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
829 pb_entity = jid.JID(problem.bare_jid)
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
830 entity_cache = cache.setdefault(pb_entity, {})
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
831 entity_bundles = bundles.setdefault(pb_entity, {})
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
832 if problem.device in entity_cache:
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
833 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
834 else:
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
835 found_bundles, missing = yield self.getBundles(
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
836 client, pb_entity, [problem.device])
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
837 entity_cache.update(bundles)
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
838 entity_bundles.update(found_bundles)
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
839 if problem.device in missing:
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
840 missing_bundles.setdefault(pb_entity, set()).add(
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
841 problem.device)
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
842 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
843 problem.device)
2860
851c47cc4ae7 plugin XEP-0384: handle NoEligibleDevicesException
Goffi <goffi@goffi.org>
parents: 2859
diff changeset
844 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
845 if undecided or found_bundles:
2860
851c47cc4ae7 plugin XEP-0384: handle NoEligibleDevicesException
Goffi <goffi@goffi.org>
parents: 2859
diff changeset
846 # 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
847 continue
851c47cc4ae7 plugin XEP-0384: handle NoEligibleDevicesException
Goffi <goffi@goffi.org>
parents: 2859
diff changeset
848 else:
851c47cc4ae7 plugin XEP-0384: handle NoEligibleDevicesException
Goffi <goffi@goffi.org>
parents: 2859
diff changeset
849 raise problem
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
850 else:
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
851 raise problem
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
852
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
853 for peer_jid, devices in missing_bundles.items():
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
854 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
855 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
856 _("Can't retrieve bundle for device(s) {devices} of entity {peer}, "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
857 "the message will not be readable on this/those device(s)").format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
858 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
859 client.feedback(
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
860 entity,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
861 D_("You're destinee {peer} has missing encryption data on some of "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
862 "his/her device(s) (bundle on device {devices}), the message won't "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
863 "be readable on this/those device.").format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
864 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
865
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
866 if undecided:
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
867 trust_data = {}
3085
c048fc192739 plugin XEP-0384: update to latest python-omemo + better trust management:
Goffi <goffi@goffi.org>
parents: 3084
diff changeset
868 for trust_id, data in undecided.items():
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
869 trust_data[trust_id] = {
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
870 'jid': jid.JID(data.bare_jid),
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
871 'device': data.device,
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
872 'ik': data.ik}
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
873
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
874 user_msg = D_("Not all destination devices are trusted, we can't encrypt "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
875 "message in such a situation. Please indicate if you trust "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
876 "those devices or not in the trust manager before we can "
2757
1f612547fb2e plugin XEP-0384: give feedback to user when trust must be handled before sending a message
Goffi <goffi@goffi.org>
parents: 2754
diff changeset
877 "send this message")
1f612547fb2e plugin XEP-0384: give feedback to user when trust must be handled before sending a message
Goffi <goffi@goffi.org>
parents: 2754
diff changeset
878 client.feedback(entity, user_msg)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
879 xmlui = yield self.getTrustUI(client, trust_data=trust_data, submit_id="")
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
880
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
881 answer = yield xml_tools.deferXMLUI(
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
882 self.host,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
883 xmlui,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
884 action_extra={
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
885 "meta_encryption_trust": NS_OMEMO,
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
886 },
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
887 profile=client.profile)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
888 yield self.trustUICb(answer, trust_data, expect_problems, client.profile)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
889
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
890 @defer.inlineCallbacks
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
891 def encryptMessage(self, client, entity_bare_jid, message):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
892 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
893 expect_problems = {}
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
894 bundles = {}
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
895 loop_idx = 0
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
896 try:
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
897 while True:
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
898 if loop_idx > 10:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
899 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
900 log.error(msg)
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
901 raise exceptions.InternalError(msg)
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
902 # 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
903 try:
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
904 encrypted = yield omemo_session.encryptMessage(
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
905 entity_bare_jid,
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
906 message,
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
907 bundles,
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
908 expect_problems = expect_problems)
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
909 except omemo_excpt.EncryptionProblemsException as e:
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
910 # we know the problem to solve, we can try to fix them
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
911 yield self.handleProblems(
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
912 client,
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
913 entity=entity_bare_jid,
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
914 bundles=bundles,
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
915 expect_problems=expect_problems,
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
916 problems=e.problems)
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
917 loop_idx += 1
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
918 else:
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
919 break
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
920 except Exception as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
921 msg = _("Can't encrypt message for {entity}: {reason}".format(
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
922 entity=entity_bare_jid.full(), reason=e))
2859
4e875d9eea48 plugin XEP-0384: give feedback to client when encryption failed
Goffi <goffi@goffi.org>
parents: 2858
diff changeset
923 log.warning(msg)
4e875d9eea48 plugin XEP-0384: give feedback to client when encryption failed
Goffi <goffi@goffi.org>
parents: 2858
diff changeset
924 extra = {C.MESS_EXTRA_INFO: C.EXTRA_INFO_ENCR_ERR}
4e875d9eea48 plugin XEP-0384: give feedback to client when encryption failed
Goffi <goffi@goffi.org>
parents: 2858
diff changeset
925 client.feedback(entity_bare_jid, msg, extra)
2858
31a5038cdf79 plugin XEP-0384: small refactoring to encrypt messages + bugfix:
Goffi <goffi@goffi.org>
parents: 2857
diff changeset
926 raise e
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
927
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
928 defer.returnValue(encrypted)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
929
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
930 @defer.inlineCallbacks
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
931 def _messageReceivedTrigger(self, client, message_elt, post_treat):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
932 if message_elt.getAttribute("type") == C.MESS_TYPE_GROUPCHAT:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
933 defer.returnValue(True)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
934 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
935 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
936 except StopIteration:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
937 # no OMEMO message here
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
938 defer.returnValue(True)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
939
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
940 # we have an encrypted message let's decrypt it
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
941 from_jid = jid.JID(message_elt['from'])
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
942 if from_jid.userhostJID() == client.jid.userhostJID():
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
943 feedback_jid = jid.JID(message_elt['to'])
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
944 else:
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
945 feedback_jid = from_jid
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
946 try:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
947 omemo_session = client._xep_0384_session
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
948 except AttributeError:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
949 # on startup, message can ve received before session actually exists
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
950 # so we need to synchronise here
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
951 yield client._xep_0384_ready
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
952 omemo_session = client._xep_0384_session
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
953
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
954 device_id = client._xep_0384_device_id
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
955 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
956 header_elt = next(encrypted_elt.elements(NS_OMEMO, 'header'))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
957 iv_elt = next(header_elt.elements(NS_OMEMO, 'iv'))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
958 except StopIteration:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
959 log.warning(_("Invalid OMEMO encrypted stanza, ignoring: {xml}")
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
960 .format(xml=message_elt.toXml()))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
961 defer.returnValue(False)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
962 try:
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
963 s_device_id = header_elt['sid']
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
964 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
965 log.warning(_("Invalid OMEMO encrypted stanza, missing sender device ID, "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
966 "ignoring: {xml}")
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
967 .format(xml=message_elt.toXml()))
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
968 defer.returnValue(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
969 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
970 key_elt = next((e for e in header_elt.elements(NS_OMEMO, 'key')
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
971 if int(e['rid']) == device_id))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
972 except StopIteration:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
973 log.warning(_("This OMEMO encrypted stanza has not been encrypted "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
974 "for our device (device_id: {device_id}, fingerprint: "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
975 "{fingerprint}): {xml}").format(
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
976 device_id=device_id,
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
977 fingerprint=omemo_session.public_bundle.ik.hex().upper(),
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
978 xml=encrypted_elt.toXml()))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
979 user_msg = (D_("An OMEMO message from {sender} has not been encrypted for "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
980 "our device, we can't decrypt it").format(
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
981 sender=from_jid.full()))
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
982 extra = {C.MESS_EXTRA_INFO: C.EXTRA_INFO_DECR_ERR}
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
983 client.feedback(feedback_jid, user_msg, extra)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
984 defer.returnValue(False)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
985 except ValueError as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
986 log.warning(_("Invalid recipient ID: {msg}".format(msg=e)))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
987 defer.returnValue(False)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
988 is_pre_key = C.bool(key_elt.getAttribute('prekey', 'false'))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
989 payload_elt = next(encrypted_elt.elements(NS_OMEMO, 'payload'), None)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
990 additional_information = {
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
991 "from_storage": bool(message_elt.delay)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
992 }
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
993
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
994 kwargs = {
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
995 "bare_jid": from_jid.userhostJID(),
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
996 "device": s_device_id,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
997 "iv": base64.b64decode(bytes(iv_elt)),
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
998 "message": base64.b64decode(bytes(key_elt)),
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
999 "is_pre_key_message": is_pre_key,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1000 "ciphertext": base64.b64decode(bytes(payload_elt))
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1001 if payload_elt is not None else None,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1002 "additional_information": additional_information,
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1003 }
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1004 try:
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1005 try:
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1006 plaintext = yield omemo_session.decryptMessage(**kwargs)
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
1007 except omemo_excpt.TrustException:
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1008 post_treat.addCallback(client.encryption.markAsUntrusted)
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1009 kwargs['allow_untrusted'] = True
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1010 plaintext = yield omemo_session.decryptMessage(**kwargs)
2744
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
1011 else:
e6716d90c2fe plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents: 2738
diff changeset
1012 post_treat.addCallback(client.encryption.markAsTrusted)
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
1013 plaintext = plaintext.decode()
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1014 except Exception as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1015 log.warning(_("Can't decrypt message: {reason}\n{xml}").format(
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1016 reason=e, xml=message_elt.toXml()))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1017 user_msg = (D_("An OMEMO message from {sender} can't be decrypted: {reason}")
2754
3bea6b5ae972 plugin XEP-0380, XEP-0384: use C.EXTRA_INFO_DECR_ERR as info sub-type when a message can't be decrypted.
Goffi <goffi@goffi.org>
parents: 2750
diff changeset
1018 .format(sender=from_jid.full(), reason=e))
3bea6b5ae972 plugin XEP-0380, XEP-0384: use C.EXTRA_INFO_DECR_ERR as info sub-type when a message can't be decrypted.
Goffi <goffi@goffi.org>
parents: 2750
diff changeset
1019 extra = {C.MESS_EXTRA_INFO: C.EXTRA_INFO_DECR_ERR}
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1020 client.feedback(feedback_jid, user_msg, extra)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1021 defer.returnValue(False)
2817
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1022 finally:
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1023 if omemo_session.republish_bundle:
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1024 # we don't wait for the Deferred (i.e. no yield) on purpose
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1025 # there is no need to block the whole message workflow while
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1026 # updating the bundle
0ab62dd3cf05 plugin XEP-0384: better bundle handling + misc improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1027 self.setBundle(client, omemo_session.public_bundle, device_id)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1028
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1029 message_elt.children.remove(encrypted_elt)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1030 if plaintext:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1031 message_elt.addElement("body", content=plaintext)
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
1032 post_treat.addCallback(client.encryption.markAsEncrypted)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1033 defer.returnValue(True)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1034
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1035 @defer.inlineCallbacks
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1036 def _sendMessageDataTrigger(self, client, mess_data):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1037 encryption = mess_data.get(C.MESS_KEY_ENCRYPTION)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1038 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
1039 return
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1040 message_elt = mess_data["xml"]
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1041 to_jid = mess_data["to"].userhostJID()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1042 log.debug("encrypting message")
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1043 body = None
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1044 for child in list(message_elt.children):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1045 if child.name == "body":
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1046 # we remove all unencrypted body,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1047 # and will only encrypt the first one
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1048 if body is None:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1049 body = child
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1050 message_elt.children.remove(child)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1051 elif child.name == "html":
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1052 # we don't want any XHTML-IM element
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1053 message_elt.children.remove(child)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1054
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1055 if body is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1056 log.warning("No message found")
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1057 return
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1058
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1059 encryption_data = yield self.encryptMessage(client, to_jid, str(body))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1060
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1061 encrypted_elt = message_elt.addElement((NS_OMEMO, 'encrypted'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1062 header_elt = encrypted_elt.addElement('header')
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1063 header_elt['sid'] = str(encryption_data['sid'])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1064 bare_jid_s = to_jid.userhost()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1065
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1066 for rid, data in encryption_data['keys'][bare_jid_s].items():
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1067 key_elt = header_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1068 'key',
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1069 content=b64enc(data['data']))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2925
diff changeset
1070 key_elt['rid'] = str(rid)
2738
eb58f26ed236 plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents: 2662
diff changeset
1071 if data['pre_key']:
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1072 key_elt['prekey'] = 'true'
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1073
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1074 header_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1075 'iv',
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1076 content=b64enc(encryption_data['iv']))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1077 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1078 encrypted_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1079 'payload',
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1080 content=b64enc(encryption_data['payload']))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1081 except KeyError:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1082 pass