Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0384.py @ 2648:0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
this is the initial implementation of OMEMO encryption using python omemo module.
/!\ This implementation is not yet working /!\
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 29 Jul 2018 19:24:21 +0200 |
parents | |
children | e7bfbded652a |
rev | line source |
---|---|
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin for OMEMO encryption |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # 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
|
8 # 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
|
9 # 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
|
10 # (at your option) any later version. |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # 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
|
13 # 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
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # 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
|
18 # 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
|
19 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.i18n import _ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core.constants import Const as C |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.log import getLogger |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.core import exceptions |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.internet import defer |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.words.xish import domish |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.words.protocols.jabber import jid |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from twisted.words.protocols.jabber import error |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from sat.memory import persistent |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from functools import partial |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 import random |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 import base64 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 import omemo |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 from omemo.extendedpublicbundle import ExtendedPublicBundle |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 from omemo import wireformat |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 except ImportError: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 raise exceptions.MissingModule( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 u'Missing module omemo, please download/install it. You can use ' |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 u'"pip install omemo"' |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 ) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 log = getLogger(__name__) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 PLUGIN_INFO = { |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 C.PI_NAME: u"OMEMO", |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 C.PI_IMPORT_NAME: u"OMEMO", |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 C.PI_TYPE: u"SEC", |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 C.PI_PROTOCOLS: [u"XEP-0384"], |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 C.PI_DEPENDENCIES: [u"XEP-0280", u"XEP-0334", u"XEP-0060"], |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 C.PI_MAIN: u"OMEMO", |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 C.PI_HANDLER: u"no", |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 C.PI_DESCRIPTION: _(u"""Implementation of OMEMO"""), |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 } |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 NS_OMEMO = "eu.siacs.conversations.axolotl" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 NS_OMEMO_DEVICES = NS_OMEMO + ".devicelist" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 NS_OMEMO_BUNDLE = NS_OMEMO + ".bundles:{device_id}" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 KEY_STATE = "STATE" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 KEY_DEVICE_ID = "DEVICE_ID" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 KEY_SESSION = "SESSION" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 KEY_ACTIVE_DEVICES = "DEVICES" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 KEY_INACTIVE_DEVICES = "DEVICES" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 def b64enc(data): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 return base64.b64encode(bytes(bytearray(data))).decode("ASCII") |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 class OmemoStorage(omemo.Storage): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 def __init__(self, persistent_dict): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 """ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 @param persistent_dict(persistent.LazyPersistentBinaryDict): object which will |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 store data in SàT database |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 """ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 self.data = persistent_dict |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 @property |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 def is_async(self): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 return True |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 def setCb(self, deferred, callback): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 """Associate Deferred and callback |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 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
|
87 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
|
88 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
|
89 Deferred is called |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 """ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 deferred.addCallback(partial(callback, True)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 deferred.addErrback(partial(callback, False)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 def loadState(self, callback): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 d = self.data.get(KEY_STATE) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 self.setCb(d, callback) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 def storeState(self, callback, state, device_id): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 d = self.data.force(KEY_STATE, {'state': state, 'device_id': device_id}) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 self.setCb(d, callback) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 def loadSession(self, callback, jid, device_id): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 key = u'\n'.join([KEY_SESSION, jid, unicode(device_id)]) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 d = self.data.get(key) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 self.setCb(d, callback) |
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 def storeSession(self, callback, jid, device_id, session): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 key = u'\n'.join([KEY_SESSION, jid, unicode(device_id)]) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 d = self.data.force(key, session) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 self.setCb(d, callback) |
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 def loadActiveDevices(self, callback, jid): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 key = u'\n'.join([KEY_ACTIVE_DEVICES, jid]) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 d = self.data.get(key, {}) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 self.setCb(d, callback) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 def loadInactiveDevices(self, callback, jid): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 key = u'\n'.join([KEY_INACTIVE_DEVICES, jid]) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 d = self.data.get(key, {}) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 self.setCb(d, callback) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 def storeActiveDevices(self, callback, jid, devices): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 key = u'\n'.join([KEY_ACTIVE_DEVICES, jid]) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 d = self.data.force(key, devices) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 self.setCb(d, 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 def storeInactiveDevices(self, callback, jid, devices): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 key = u'\n'.join([KEY_INACTIVE_DEVICES, jid]) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 d = self.data.force(key, devices) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 self.setCb(d, callback) |
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 def isTrusted(self, callback, jid, device): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 trusted = True |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 callback(True, trusted) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 class SatOTPKPolicy(omemo.OTPKPolicy): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 @staticmethod |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 def decideOTPK(preKeyMessages): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 # Always just delete the OTPK. |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 # This is the behaviour described in the original X3DH specification. |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 return True |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 class OmemoSession(object): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 """Wrapper to use omemo.OmemoSession with Deferred""" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 def __init__(self, session): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 self._session = session |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 @property |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 def state(self): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 return self._session.state |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 @staticmethod |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 def promise2Deferred(promise_): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 """Create a Deferred and fire it when promise is resolved |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 @param promise_(promise.Promise): promise to convert |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 @return (defer.Deferred): deferred instance linked to the promise |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 """ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 d = defer.Deferred() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 promise_.then(d.callback, d.errback) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 return d |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 @classmethod |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 def create(cls, client, omemo_storage, device_id): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 omemo_session_p = client._xep_0384_session = omemo.SessionManager.create( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 client.jid.userhost(), omemo_storage, SatOTPKPolicy, my_device_id=device_id) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 d = cls.promise2Deferred(omemo_session_p) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 d.addCallback(lambda session: cls(session)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 return d |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 def newDeviceList(self, devices, jid=None): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 if jid is not None: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 jid = jid.userhost() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 new_device_p = self._session.newDeviceList(devices, jid) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 return self.promise2Deferred(new_device_p) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 def getDevices(self, bare_jid=None): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 get_devices_p = self._session.getDevices(bare_jid=bare_jid) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 return self.promise2Deferred(get_devices_p) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 def buildSession(self, bare_jid, device, bundle): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 bare_jid = bare_jid.userhost() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 build_session_p = self._session.buildSession(bare_jid, device, bundle) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 return self.promise2Deferred(build_session_p) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 def encryptMessage(self, bare_jids, message, bundles=None, devices=None, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 always_trust = False): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 """Encrypt a message |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 @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
|
195 @param message(unicode): message to encode |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 @param bundles(dict[jid.JID, dict[int, ExtendedPublicBundle]): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 entities => devices => bundles map |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 @param devices(iterable[int], None): devices to encode for |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 @param always_trust(bool): TODO |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 @return D(dict): encryption data |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 """ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 if isinstance(bare_jids, jid.JID): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 bare_jids = bare_jids.userhost() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 else: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 bare_jids = [e.userhost() for e in bare_jids] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 if bundles is not None: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 bundles = {e.userhost(): v for e, v in bundles.iteritems()} |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 encrypt_mess_p = self._session.encryptMessage( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 bare_jids=bare_jids, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 plaintext=message.encode('utf-8'), |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 bundles=bundles, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 devices=devices, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 always_trust=always_trust) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 return self.promise2Deferred(encrypt_mess_p) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 def decryptMessage(self, bare_jid, device, iv, message, is_pre_key_message, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 payload=None, from_storage=False): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 bare_jid = bare_jid.userhost() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 decrypt_mess_p = self._session.decryptMessage( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 bare_jid=bare_jid, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 device=device, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 iv=iv, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 message=message, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 is_pre_key_message=is_pre_key_message, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 payload=payload, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 from_storage=from_storage) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 return self.promise2Deferred(decrypt_mess_p) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 class OMEMO(object): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 def __init__(self, host): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 log.info(_(u"OMEMO plugin initialization")) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 self.host = host |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 self._p_hints = host.plugins[u"XEP-0334"] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 self._p_carbons = host.plugins[u"XEP-0280"] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 self._p = host.plugins[u"XEP-0060"] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 host.trigger.add("MessageReceived", self._messageReceivedTrigger, priority=100050) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 host.trigger.add("sendMessageData", self._sendMessageDataTrigger) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 self.host.registerEncryptionPlugin(self, "OMEMO", NS_OMEMO, 100) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 @defer.inlineCallbacks |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 def profileConnected(self, client): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 # 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
|
244 persistent_dict = persistent.LazyPersistentBinaryDict("XEP-0384", client.profile) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 # all known devices of profile |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 devices = yield self.getDevices(client) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 # and our own device id |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 device_id = yield persistent_dict.get(KEY_DEVICE_ID) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 if device_id is None: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 # we have a new device, we create device_id |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 device_id = random.randint(1, 2**31-1) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 # we check that it's really unique |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 while device_id in devices: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 device_id = random.randint(1, 2**31-1) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 # and we save it |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 persistent_dict[KEY_DEVICE_ID] = device_id |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
257 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 if device_id not in devices: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
259 devices.add(device_id) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 yield self.setDevices(client, devices) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
261 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
262 omemo_storage = OmemoStorage(persistent_dict) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
263 omemo_session = yield OmemoSession.create(client, omemo_storage, device_id) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 client._xep_0384_session = omemo_session |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 client._xep_0384_device_id = device_id |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
266 yield omemo_session.newDeviceList(devices) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
267 if omemo_session.state.changed: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
268 log.info(_(u"Saving public bundle for this device ({device_id})").format( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
269 device_id=device_id)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
270 bundle = omemo_session.state.getPublicBundle() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 yield self.setBundle(client, bundle, device_id) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 ## XMPP PEP nodes manipulation |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
274 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 # devices |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
276 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 @defer.inlineCallbacks |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
278 def getDevices(self, client, entity_jid=None): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
279 """Retrieve list of registered OMEMO devices |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 @param entity_jid(jid.JID, None): get devices from this entity |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
282 None to get our own devices |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
283 @return (set(int)): list of devices |
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 if entity_jid is not None: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
286 assert not entity_jid.resource |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
287 devices = set() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
288 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
289 items, metadata = yield self._p.getItems(client, entity_jid, NS_OMEMO_DEVICES) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
290 except error.StanzaError as e: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
291 if e.condition == 'item-not-found': |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
292 log.info(_(u"there is no node to handle OMEMO devices")) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
293 defer.returnValue(devices) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
294 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
295 if len(items) > 1: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
296 log.warning(_(u"OMEMO devices list is stored in more that one items, " |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
297 u"this is not expected")) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
298 if items: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
299 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
300 list_elt = next(items[0].elements(NS_OMEMO, 'list')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 except StopIteration: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 log.warning(_(u"no list element found in OMEMO devices list")) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
303 return |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
304 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
|
305 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
306 device_id = int(device_elt['id']) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
307 except KeyError: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
308 log.warning(_(u'device element is missing "id" attribute: {elt}') |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
309 .format(elt=device_elt.toXml())) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
310 except ValueError: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 log.warning(_(u'invalid device id: {device_id}').format( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 device_id=device_elt['id'])) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
313 else: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
314 devices.add(device_id) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 defer.returnValue(devices) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
316 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
317 def setDevicesEb(self, failure_): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
318 log.warning(_(u"Can't set devices: {reason}").format(reason=failure_)) |
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 setDevices(self, client, devices): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 list_elt = domish.Element((NS_OMEMO, 'list')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 for device in devices: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
323 device_elt = list_elt.addElement('device') |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
324 device_elt['id'] = unicode(device) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
325 d = self._p.sendItem( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
326 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
|
327 d.addErrback(self.setDevicesEb) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
328 return d |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
329 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
330 # bundles |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
331 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
332 @defer.inlineCallbacks |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
333 def getBundles(self, client, entity_jid, devices_ids): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
334 """Retrieve public bundles of an entity devices |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
335 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
336 @param entity_jid(jid.JID): bare jid of entity |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
337 @param devices_id(iterable[int]): ids of the devices bundles to retrieve |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
338 @return (dict[int, ExtendedPublicBundle]): bundles collection |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
339 key is device_id |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
340 value is parsed bundle |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
341 """ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
342 assert not entity_jid.resource |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
343 bundles = {} |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
344 for device_id in devices_ids: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
345 node = NS_OMEMO_BUNDLE.format(device_id=device_id) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
346 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
347 items, metadata = yield self._p.getItems(client, entity_jid, node) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
348 except Exception as e: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
349 log.warning(_(u"Can't get bundle for device {device_id}: {reason}") |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
350 .format(device_id=device_id, reason=e)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
351 continue |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
352 if not items: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
353 log.warning(_(u"no item found in node {node}, can't get public bundle " |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
354 u"for device {device_id}").format(node=node, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
355 device_id=device_id)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
356 continue |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
357 if len(items) > 1: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
358 log.warning(_(u"more than one item found in {node}," |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
359 u"this is not expected").format(node=node)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
360 item = items[0] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
361 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
362 bundle_elt = next(item.elements(NS_OMEMO, 'bundle')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
363 signedPreKeyPublic_elt = next(bundle_elt.elements( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
364 NS_OMEMO, 'signedPreKeyPublic')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
365 signedPreKeySignature_elt = next(bundle_elt.elements( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
366 NS_OMEMO, 'signedPreKeySignature')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
367 identityKey_elt = next(bundle_elt.elements( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
368 NS_OMEMO, 'identityKey')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
369 prekeys_elt = next(bundle_elt.elements( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
370 NS_OMEMO, 'prekeys')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
371 except StopIteration: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
372 log.warning(_(u"invalid bundle for device {device_id}, ignoring").format( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
373 device_id=device_id)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
374 continue |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
375 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
376 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
377 spkPublic = base64.b64decode(unicode(signedPreKeyPublic_elt)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
378 spkSignature = base64.b64decode( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
379 unicode(signedPreKeySignature_elt)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
380 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
381 identityKey = base64.b64decode(unicode(identityKey_elt)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
382 spk = { |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
383 "key": wireformat.decodePublicKey(spkPublic), |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
384 "id": int(signedPreKeyPublic_elt['signedPreKeyId']) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
385 } |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
386 ik = wireformat.decodePublicKey(identityKey) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
387 otpks = [] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
388 for preKeyPublic_elt in prekeys_elt.elements(NS_OMEMO, 'preKeyPublic'): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
389 preKeyPublic = base64.b64decode(unicode(preKeyPublic_elt)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
390 otpk = { |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
391 "key": wireformat.decodePublicKey(preKeyPublic), |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
392 "id": int(preKeyPublic_elt['preKeyId']) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
393 } |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
394 otpks.append(otpk) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
395 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
396 except Exception as e: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
397 log.warning(_(u"error while decoding key for device {devide_id}: {msg}") |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
398 .format(device_id=device_id, msg=e)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
399 continue |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
400 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
401 bundles[device_id] = ExtendedPublicBundle(ik, spk, spkSignature, otpks) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
402 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
403 defer.returnValue(bundles) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
404 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
405 def setBundleEb(self, failure_): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
406 log.warning(_(u"Can't set bundle: {reason}").format(reason=failure_)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
407 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
408 def setBundle(self, client, bundle, device_id): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
409 """Set public bundle for this device. |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
410 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
411 @param bundle(ExtendedPublicBundle): bundle to publish |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
412 """ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
413 log.debug(_(u"updating bundle for {device_id}").format(device_id=device_id)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
414 bundle_elt = domish.Element((NS_OMEMO, 'bundle')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
415 signedPreKeyPublic_elt = bundle_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
416 "signedPreKeyPublic", |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
417 content=b64enc(wireformat.encodePublicKey(bundle.spk['key']))) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
418 signedPreKeyPublic_elt['signedPreKeyId'] = unicode(bundle.spk['id']) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
419 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
420 bundle_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
421 "signedPreKeySignature", |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
422 content=b64enc(bundle.spk_signature)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
423 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
424 bundle_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
425 "identityKey", |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
426 content=b64enc(wireformat.encodePublicKey(bundle.ik))) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
427 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
428 prekeys_elt = bundle_elt.addElement('prekeys') |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
429 for otpk in bundle.otpks: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
430 preKeyPublic_elt = prekeys_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
431 'preKeyPublic', |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
432 content=b64enc(wireformat.encodePublicKey(otpk["key"]))) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
433 preKeyPublic_elt['preKeyId'] = unicode(otpk['id']) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
434 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
435 node = NS_OMEMO_BUNDLE.format(device_id=device_id) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
436 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
|
437 d.addErrback(self.setBundleEb) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
438 return d |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
439 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
440 ## triggers |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
441 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
442 @defer.inlineCallbacks |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
443 def encryptMessage(self, client, entity_bare_jid, message): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
444 omemo_session = client._xep_0384_session |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
445 devices = yield self.getDevices(client, entity_bare_jid) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
446 omemo_session.newDeviceList(devices, entity_bare_jid) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
447 bundles = yield self.getBundles(client, entity_bare_jid, devices) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
448 encrypted = yield omemo_session.encryptMessage( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
449 entity_bare_jid, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
450 message, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
451 {entity_bare_jid: bundles}) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
452 defer.returnValue(encrypted) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
453 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
454 @defer.inlineCallbacks |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
455 def _messageReceivedTrigger(self, client, message_elt, post_treat): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
456 if message_elt.getAttribute("type") == C.MESS_TYPE_GROUPCHAT: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
457 defer.returnValue(True) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
458 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
459 encrypted_elt = next(message_elt.elements(NS_OMEMO, u"encrypted")) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
460 except StopIteration: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
461 # no OMEMO message here |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
462 defer.returnValue(True) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
463 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
464 # we have an encrypted message let's decrypt it |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
465 # from_jid = jid.JID(message_elt['from']) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
466 omemo_session = client._xep_0384_session |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
467 device_id = client._xep_0384_device_id |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
468 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
469 header_elt = next(encrypted_elt.elements(NS_OMEMO, u'header')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
470 iv_elt = next(header_elt.elements(NS_OMEMO, u'iv')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
471 except StopIteration: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
472 log.warning(_(u"Invalid OMEMO encrypted stanza, ignoring: {xml}") |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
473 .format(xml=message_elt.toXml())) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
474 defer.returnValue(False) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
475 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
476 key_elt = next((e for e in header_elt.elements(NS_OMEMO, u'key') |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
477 if int(e[u'rid']) == device_id)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
478 except StopIteration: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
479 log.warning(_(u"This OMEMO encrypted stanza has not been encrypted" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
480 u"for our device ({device_id}): {xml}").format( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
481 device_id=device_id, xml=encrypted_elt.toXml())) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
482 defer.returnValue(False) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
483 except ValueError as e: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
484 log.warning(_(u"Invalid recipient ID: {msg}".format(msg=e))) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
485 defer.returnValue(False) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
486 is_pre_key = C.bool(key_elt.getAttribute('prekey', 'false')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
487 payload_elt = next(encrypted_elt.elements(NS_OMEMO, u'payload'), None) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
488 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
489 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
490 cipher, plaintext = yield omemo_session.decryptMessage( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
491 bare_jid=client.jid.userhostJID(), |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
492 device=device_id, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
493 iv=base64.b64decode(bytes(iv_elt)), |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
494 message=base64.b64decode(bytes(key_elt)), |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
495 is_pre_key_message=is_pre_key, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
496 payload=base64.b64decode(bytes(payload_elt)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
497 if payload_elt is not None else None, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
498 from_storage=False |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
499 ) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
500 except Exception as e: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
501 log.error(_(u"Can't decrypt message: {reason}\n{xml}").format( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
502 reason=e, xml=message_elt.toXml())) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
503 defer.returnValue(False) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
504 if omemo_session.state.changed: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
505 bundle = omemo_session.state.getPublicBundle() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
506 # we don't wait for the Deferred (i.e. no yield) on purpose |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
507 # there is no need to block the whole message workflow while |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
508 # updating the bundle |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
509 self.setBundle(client, bundle, device_id) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
510 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
511 message_elt.children.remove(encrypted_elt) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
512 if plaintext: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
513 message_elt.addElement("body", content=plaintext.decode('utf-8')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
514 defer.returnValue(True) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
515 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
516 @defer.inlineCallbacks |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
517 def _sendMessageDataTrigger(self, client, mess_data): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
518 encryption = mess_data.get(C.MESS_KEY_ENCRYPTION) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
519 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
|
520 return |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
521 message_elt = mess_data["xml"] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
522 to_jid = mess_data["to"].userhostJID() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
523 log.debug(u"encrypting message") |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
524 body = None |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
525 for child in list(message_elt.children): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
526 if child.name == "body": |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
527 # we remove all unencrypted body, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
528 # and will only encrypt the first one |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
529 if body is None: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
530 body = child |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
531 message_elt.children.remove(child) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
532 elif child.name == "html": |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
533 # we don't want any XHTML-IM element |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
534 message_elt.children.remove(child) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
535 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
536 if body is None: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
537 log.warning(u"No message found") |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
538 return |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
539 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
540 encryption_data = yield self.encryptMessage(client, to_jid, unicode(body)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
541 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
542 encrypted_elt = message_elt.addElement((NS_OMEMO, 'encrypted')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
543 header_elt = encrypted_elt.addElement('header') |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
544 header_elt['sid'] = unicode(encryption_data['sid']) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
545 bare_jid_s = to_jid.userhost() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
546 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
547 for message in (m for m in encryption_data['messages'] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
548 if m['bare_jid'] == bare_jid_s): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
549 key_elt = header_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
550 'key', |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
551 content=b64enc(message['message'])) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
552 key_elt['rid'] = unicode(message['rid']) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
553 if message['pre_key']: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
554 key_elt['prekey'] = 'true' |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
555 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
556 header_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
557 'iv', |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
558 content=b64enc(encryption_data['iv'])) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
559 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
560 encrypted_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
561 'payload', |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
562 content=b64enc(encryption_data['payload'])) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
563 except KeyError: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
564 pass |