annotate sat/plugins/plugin_xep_0384.py @ 2699:310e41bd6666

core (memory/sqlite): added stanza_id: /!\ database schema change /!\ stanza_id is a new field in history added to prepare the implementation of MAM for messages. A new "last_stanza_id" can be used in filters to retrieve last message with a know stanza id (useful for history synchronisation).
author Goffi <goffi@goffi.org>
date Sat, 01 Dec 2018 10:08:17 +0100
parents 0bef44f8e8ca
children eb58f26ed236
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
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
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from omemo.extendedpublicbundle import ExtendedPublicBundle
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from omemo import wireformat
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 except ImportError:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 raise exceptions.MissingModule(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 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
40 u'"pip install omemo"'
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
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 log = getLogger(__name__)
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"],
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
50 C.PI_DEPENDENCIES: [u"XEP-0163", u"XEP-0280", u"XEP-0334", u"XEP-0060"],
2648
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
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
66 # we want to manage log emitted by omemo module ourseves
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
67
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
68 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
69
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
70 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
71 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
72
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
73 @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
74 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
75 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
76 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
77 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
78
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 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
81
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
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 def b64enc(data):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 return base64.b64encode(bytes(bytearray(data))).decode("ASCII")
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
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 class OmemoStorage(omemo.Storage):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88
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
89 def __init__(self, client, device_id, persistent_dict):
2648
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 @param persistent_dict(persistent.LazyPersistentBinaryDict): object which will
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 store data in SàT database
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 """
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
94 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
95 self.device_id = device_id
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 self.data = persistent_dict
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 @property
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 def is_async(self):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 return True
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 setCb(self, deferred, callback):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 """Associate Deferred and callback
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 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
106 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
107 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
108 Deferred is called
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 """
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 deferred.addCallback(partial(callback, True))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 deferred.addErrback(partial(callback, False))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112
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
113 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
114 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
115 '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
116
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
117 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
118 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
119 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
120 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
121
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 def loadState(self, callback):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 d = self.data.get(KEY_STATE)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125
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
126 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
127 d = self.data.force(KEY_STATE, state)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129
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
130 def loadSession(self, callback, bare_jid, 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
131 key = u'\n'.join([KEY_SESSION, bare_jid, unicode(device_id)])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 d = self.data.get(key)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
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
135 def storeSession(self, callback, bare_jid, device_id, session):
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
136 key = u'\n'.join([KEY_SESSION, bare_jid, unicode(device_id)])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 d = self.data.force(key, session)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139
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
140 def loadActiveDevices(self, callback, bare_jid):
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
141 key = u'\n'.join([KEY_ACTIVE_DEVICES, bare_jid])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 d = self.data.get(key, {})
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144
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
145 def loadInactiveDevices(self, callback, bare_jid):
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
146 key = u'\n'.join([KEY_INACTIVE_DEVICES, bare_jid])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 d = self.data.get(key, {})
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149
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
150 def storeActiveDevices(self, callback, bare_jid, devices):
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 key = u'\n'.join([KEY_ACTIVE_DEVICES, bare_jid])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 d = self.data.force(key, devices)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154
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
155 def storeInactiveDevices(self, callback, bare_jid, devices):
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 key = u'\n'.join([KEY_INACTIVE_DEVICES, bare_jid])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 d = self.data.force(key, devices)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 self.setCb(d, callback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159
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
160 def isTrusted(self, callback, bare_jid, device):
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 trusted = True
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 callback(True, trusted)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 class SatOTPKPolicy(omemo.OTPKPolicy):
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 @staticmethod
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 def decideOTPK(preKeyMessages):
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
169 # always delete
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 return False
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 class OmemoSession(object):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 """Wrapper to use omemo.OmemoSession with Deferred"""
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 def __init__(self, session):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 self._session = session
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
178
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 @property
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 def state(self):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 return self._session.state
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 @staticmethod
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 def promise2Deferred(promise_):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 """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
186
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 @param promise_(promise.Promise): promise to convert
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 @return (defer.Deferred): deferred instance linked to the promise
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 d = defer.Deferred()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 promise_.then(d.callback, d.errback)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 return d
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 @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
195 def create(cls, client, storage, my_device_id = None):
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 omemo_session_p = client._xep_0384_session = 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
197 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
198 SatOTPKPolicy,
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
199 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
200 my_device_id)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 d = cls.promise2Deferred(omemo_session_p)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 d.addCallback(lambda session: cls(session))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 return d
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
204
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
205 def newDeviceList(self, devices, jid):
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
206 jid = jid.userhost()
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 new_device_p = self._session.newDeviceList(devices, jid)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 return self.promise2Deferred(new_device_p)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
209
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 def getDevices(self, bare_jid=None):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 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
212 return self.promise2Deferred(get_devices_p)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
213
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 def buildSession(self, bare_jid, device, bundle):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 bare_jid = bare_jid.userhost()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 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
217 return self.promise2Deferred(build_session_p)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
218
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 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
220 always_trust = False):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 """Encrypt a message
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
222
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 @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
224 @param message(unicode): message to encode
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 @param bundles(dict[jid.JID, dict[int, ExtendedPublicBundle]):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 entities => devices => bundles map
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 @param devices(iterable[int], None): devices to encode for
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 @param always_trust(bool): TODO
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 @return D(dict): encryption data
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 """
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 if isinstance(bare_jids, jid.JID):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 bare_jids = bare_jids.userhost()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 else:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 bare_jids = [e.userhost() for e in bare_jids]
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 if bundles is not None:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 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
237 encrypt_mess_p = self._session.encryptMessage(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 bare_jids=bare_jids,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 plaintext=message.encode('utf-8'),
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 bundles=bundles,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 devices=devices,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 always_trust=always_trust)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 return self.promise2Deferred(encrypt_mess_p)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
244
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 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
246 payload=None, from_storage=False):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 bare_jid = bare_jid.userhost()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 decrypt_mess_p = self._session.decryptMessage(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 bare_jid=bare_jid,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 device=device,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 iv=iv,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 message=message,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 is_pre_key_message=is_pre_key_message,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 payload=payload,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 from_storage=from_storage)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 return self.promise2Deferred(decrypt_mess_p)
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
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 class OMEMO(object):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 def __init__(self, host):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 log.info(_(u"OMEMO plugin initialization"))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 self.host = host
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 self._p_hints = host.plugins[u"XEP-0334"]
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 self._p_carbons = host.plugins[u"XEP-0280"]
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 self._p = host.plugins[u"XEP-0060"]
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 host.trigger.add("MessageReceived", self._messageReceivedTrigger, priority=100050)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 host.trigger.add("sendMessageData", self._sendMessageDataTrigger)
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
268 self.host.registerEncryptionPlugin(self, u"OMEMO", NS_OMEMO, 100)
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
269 pep = host.plugins['XEP-0163']
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
270 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
271
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 @defer.inlineCallbacks
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 def profileConnected(self, client):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 # 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
275 persistent_dict = persistent.LazyPersistentBinaryDict("XEP-0384", client.profile)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 # all known devices of profile
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 devices = yield self.getDevices(client)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 # and our own device id
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 device_id = yield persistent_dict.get(KEY_DEVICE_ID)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 if device_id is None:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 # we have a new device, we create device_id
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 device_id = random.randint(1, 2**31-1)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 # we check that it's really unique
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 while device_id in devices:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 device_id = random.randint(1, 2**31-1)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 # and we save it
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 persistent_dict[KEY_DEVICE_ID] = device_id
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
288
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 if device_id not in devices:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 devices.add(device_id)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 yield self.setDevices(client, devices)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
292
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
293 omemo_storage = OmemoStorage(client, device_id, persistent_dict)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 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
295 client._xep_0384_cache = {}
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 client._xep_0384_session = omemo_session
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 client._xep_0384_device_id = device_id
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
298 yield omemo_session.newDeviceList(devices, client.jid)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 if omemo_session.state.changed:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 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
301 device_id=device_id))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 bundle = omemo_session.state.getPublicBundle()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 yield self.setBundle(client, bundle, device_id)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
304
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
305 ## XMPP PEP nodes manipulation
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
306
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 # devices
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
308
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
309 def parseDevices(self, items):
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
310 """Parse devices found in items
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
311
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
312 @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
313 @return set[int]: parsed devices
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
314 """
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 devices = set()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 if len(items) > 1:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 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
318 u"this is not expected"))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 if items:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 list_elt = next(items[0].elements(NS_OMEMO, 'list'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 except StopIteration:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
323 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
324 return
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 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
326 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
327 device_id = int(device_elt['id'])
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 except KeyError:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 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
330 .format(elt=device_elt.toXml()))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 except ValueError:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
332 log.warning(_(u'invalid device id: {device_id}').format(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 device_id=device_elt['id']))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
334 else:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 devices.add(device_id)
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
336 return devices
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
337
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
338 @defer.inlineCallbacks
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
339 def getDevices(self, client, entity_jid=None):
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
340 """Retrieve list of registered OMEMO devices
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
341
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
342 @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
343 None to get our own devices
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
344 @return (set(int)): list of devices
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
345 """
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
346 if entity_jid is not None:
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
347 assert not entity_jid.resource
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
348 try:
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
349 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
350 except error.StanzaError as e:
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
351 if e.condition == 'item-not-found':
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
352 log.info(_(u"there is no node to handle OMEMO devices"))
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
353 defer.returnValue(set())
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
354
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
355 devices = self.parseDevices(items)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
356 defer.returnValue(devices)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
357
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
358 def setDevicesEb(self, failure_):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
359 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
360
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
361 def setDevices(self, client, devices):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
362 list_elt = domish.Element((NS_OMEMO, 'list'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
363 for device in devices:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
364 device_elt = list_elt.addElement('device')
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 device_elt['id'] = unicode(device)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
366 d = self._p.sendItem(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
367 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
368 d.addErrback(self.setDevicesEb)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
369 return d
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
370
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
371 # bundles
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
372
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
373 @defer.inlineCallbacks
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
374 def getBundles(self, client, entity_jid, devices_ids):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
375 """Retrieve public bundles of an entity devices
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 @param entity_jid(jid.JID): bare jid of entity
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
378 @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
379 @return (dict[int, ExtendedPublicBundle]): bundles collection
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
380 key is device_id
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
381 value is parsed bundle
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
382 """
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
383 assert not entity_jid.resource
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
384 bundles = {}
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
385 for device_id in devices_ids:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
386 node = NS_OMEMO_BUNDLE.format(device_id=device_id)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
387 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 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
389 except Exception as e:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
390 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
391 .format(device_id=device_id, reason=e))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
392 continue
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 if not items:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
394 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
395 u"for device {device_id}").format(node=node,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
396 device_id=device_id))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
397 continue
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
398 if len(items) > 1:
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
399 log.warning(_(u"more than one item found in {node}, "
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
400 u"this is not expected").format(node=node))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
401 item = items[0]
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
402 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
403 bundle_elt = next(item.elements(NS_OMEMO, 'bundle'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
404 signedPreKeyPublic_elt = next(bundle_elt.elements(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
405 NS_OMEMO, 'signedPreKeyPublic'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
406 signedPreKeySignature_elt = next(bundle_elt.elements(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
407 NS_OMEMO, 'signedPreKeySignature'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
408 identityKey_elt = next(bundle_elt.elements(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
409 NS_OMEMO, 'identityKey'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
410 prekeys_elt = next(bundle_elt.elements(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
411 NS_OMEMO, 'prekeys'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
412 except StopIteration:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
413 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
414 device_id=device_id))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
415 continue
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
416
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
417 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
418 spkPublic = base64.b64decode(unicode(signedPreKeyPublic_elt))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
419 spkSignature = base64.b64decode(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
420 unicode(signedPreKeySignature_elt))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
421
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
422 identityKey = base64.b64decode(unicode(identityKey_elt))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
423 spk = {
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
424 "key": wireformat.decodePublicKey(spkPublic),
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
425 "id": int(signedPreKeyPublic_elt['signedPreKeyId'])
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
426 }
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
427 ik = wireformat.decodePublicKey(identityKey)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
428 otpks = []
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
429 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
430 preKeyPublic = base64.b64decode(unicode(preKeyPublic_elt))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
431 otpk = {
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
432 "key": wireformat.decodePublicKey(preKeyPublic),
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
433 "id": int(preKeyPublic_elt['preKeyId'])
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 otpks.append(otpk)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
436
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
437 except Exception as e:
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
438 log.warning(_(u"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
439 .format(device_id=device_id, msg=e))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
440 continue
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 bundles[device_id] = ExtendedPublicBundle(ik, spk, spkSignature, otpks)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
443
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
444 defer.returnValue(bundles)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
445
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
446 def setBundleEb(self, failure_):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
447 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
448
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
449 def setBundle(self, client, bundle, device_id):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
450 """Set public bundle for this device.
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
451
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
452 @param bundle(ExtendedPublicBundle): bundle to publish
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 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
455 bundle_elt = domish.Element((NS_OMEMO, 'bundle'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
456 signedPreKeyPublic_elt = bundle_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
457 "signedPreKeyPublic",
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
458 content=b64enc(wireformat.encodePublicKey(bundle.spk['key'])))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
459 signedPreKeyPublic_elt['signedPreKeyId'] = unicode(bundle.spk['id'])
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
460
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
461 bundle_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
462 "signedPreKeySignature",
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
463 content=b64enc(bundle.spk_signature))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
464
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
465 bundle_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
466 "identityKey",
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
467 content=b64enc(wireformat.encodePublicKey(bundle.ik)))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
468
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
469 prekeys_elt = bundle_elt.addElement('prekeys')
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
470 for otpk in bundle.otpks:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
471 preKeyPublic_elt = prekeys_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
472 'preKeyPublic',
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
473 content=b64enc(wireformat.encodePublicKey(otpk["key"])))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
474 preKeyPublic_elt['preKeyId'] = unicode(otpk['id'])
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
475
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
476 node = NS_OMEMO_BUNDLE.format(device_id=device_id)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
477 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
478 d.addErrback(self.setBundleEb)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
479 return d
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
480
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
481 ## PEP node events callbacks
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
482
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
483 @defer.inlineCallbacks
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
484 def onNewDevices(self, itemsEvent, profile):
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
485 client = self.host.getClient(profile)
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
486 cache = client._xep_0384_cache
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
487 omemo_session = client._xep_0384_session
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
488 entity = itemsEvent.sender
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
489 entity_cache = cache.setdefault(entity, {})
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
490 devices = self.parseDevices(itemsEvent.items)
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
491 omemo_session.newDeviceList(devices, entity)
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
492 missing_devices = devices.difference(entity_cache.keys())
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
493 if missing_devices:
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
494 missing_bundles = yield self.getBundles(client, entity, missing_devices)
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
495 entity_cache.update(missing_bundles)
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
496
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
497 ## triggers
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
498
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
499 @defer.inlineCallbacks
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
500 def encryptMessage(self, client, entity_bare_jid, message):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
501 omemo_session = client._xep_0384_session
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
502 try:
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
503 bundles = client._xep_0384_cache[entity_bare_jid]
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
504 except KeyError:
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
505 raise exceptions.NotFound(_(u"No OMEMO encryption information found for this"
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
506 u"contact ({entity})".format(
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
507 entity=entity_bare_jid.full())))
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
508 encrypted = yield omemo_session.encryptMessage(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
509 entity_bare_jid,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
510 message,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
511 {entity_bare_jid: bundles})
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
512 defer.returnValue(encrypted)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
513
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
514 @defer.inlineCallbacks
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
515 def _messageReceivedTrigger(self, client, message_elt, post_treat):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
516 if message_elt.getAttribute("type") == C.MESS_TYPE_GROUPCHAT:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
517 defer.returnValue(True)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
518 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
519 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
520 except StopIteration:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
521 # no OMEMO message here
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
522 defer.returnValue(True)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
523
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
524 # 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
525 from_jid = jid.JID(message_elt['from'])
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
526 omemo_session = client._xep_0384_session
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
527 device_id = client._xep_0384_device_id
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
528 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
529 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
530 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
531 except StopIteration:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
532 log.warning(_(u"Invalid OMEMO encrypted stanza, ignoring: {xml}")
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
533 .format(xml=message_elt.toXml()))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
534 defer.returnValue(False)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
535 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
536 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
537 except KeyError:
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
538 log.warning(_(u"Invalid OMEMO encrypted stanza, missing sender 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
539 u"ignoring: {xml}")
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
540 .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
541 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
542 try:
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
543 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
544 if int(e[u'rid']) == device_id))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
545 except StopIteration:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
546 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
547 u"for our device ({device_id}): {xml}").format(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
548 device_id=device_id, xml=encrypted_elt.toXml()))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
549 defer.returnValue(False)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
550 except ValueError as e:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
551 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
552 defer.returnValue(False)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
553 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
554 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
555
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
556 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
557 cipher, plaintext = yield omemo_session.decryptMessage(
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
558 bare_jid=from_jid.userhostJID(),
e7bfbded652a plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents: 2648
diff changeset
559 device=s_device_id,
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
560 iv=base64.b64decode(bytes(iv_elt)),
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
561 message=base64.b64decode(bytes(key_elt)),
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
562 is_pre_key_message=is_pre_key,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
563 payload=base64.b64decode(bytes(payload_elt))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
564 if payload_elt is not None else None,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
565 from_storage=False
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
566 )
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
567 except Exception as e:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
568 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
569 reason=e, xml=message_elt.toXml()))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
570 defer.returnValue(False)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
571 if omemo_session.state.changed:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
572 bundle = omemo_session.state.getPublicBundle()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
573 # 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
574 # 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
575 # updating the bundle
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
576 self.setBundle(client, bundle, device_id)
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 message_elt.children.remove(encrypted_elt)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
579 if plaintext:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
580 message_elt.addElement("body", content=plaintext.decode('utf-8'))
2662
0bef44f8e8ca plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents: 2654
diff changeset
581 post_treat.addCallback(client.encryption.markAsEncrypted)
2648
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
582 defer.returnValue(True)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
583
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
584 @defer.inlineCallbacks
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
585 def _sendMessageDataTrigger(self, client, mess_data):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
586 encryption = mess_data.get(C.MESS_KEY_ENCRYPTION)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
587 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
588 return
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
589 message_elt = mess_data["xml"]
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
590 to_jid = mess_data["to"].userhostJID()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
591 log.debug(u"encrypting message")
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
592 body = None
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
593 for child in list(message_elt.children):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
594 if child.name == "body":
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
595 # we remove all unencrypted body,
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
596 # and will only encrypt the first one
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
597 if body is None:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
598 body = child
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
599 message_elt.children.remove(child)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
600 elif child.name == "html":
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
601 # we don't want any XHTML-IM element
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
602 message_elt.children.remove(child)
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
603
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
604 if body is None:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
605 log.warning(u"No message found")
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
606 return
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
607
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
608 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
609
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
610 encrypted_elt = message_elt.addElement((NS_OMEMO, 'encrypted'))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
611 header_elt = encrypted_elt.addElement('header')
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
612 header_elt['sid'] = unicode(encryption_data['sid'])
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
613 bare_jid_s = to_jid.userhost()
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
614
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
615 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
616 if m['bare_jid'] == bare_jid_s):
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
617 key_elt = header_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
618 'key',
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
619 content=b64enc(message['message']))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
620 key_elt['rid'] = unicode(message['rid'])
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
621 if message['pre_key']:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
622 key_elt['prekey'] = 'true'
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
623
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
624 header_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
625 'iv',
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
626 content=b64enc(encryption_data['iv']))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
627 try:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
628 encrypted_elt.addElement(
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
629 'payload',
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
630 content=b64enc(encryption_data['payload']))
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
631 except KeyError:
0f76813afc57 plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
632 pass