annotate src/browser/sat_browser/otrjs_wrapper.py @ 982:a78169c1bf77

pages (tickets/new): added reporter_jid in ignore list (if needed, it is filled in backend)
author Goffi <goffi@goffi.org>
date Wed, 15 Nov 2017 09:00:00 +0100
parents fd4eae654182
children f2170536ba23
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
1 #!/usr/bin/python
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
3
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # Libervia wrapper for otr.js
964
fd4eae654182 misc: date update (yes it's a bit late :p )
Goffi <goffi@goffi.org>
parents: 818
diff changeset
5 # Copyright (C) 2009-2017 Jérôme Poisson (goffi@goffi.org)
818
f8a7a046ff9c copyright update
Goffi <goffi@goffi.org>
parents: 685
diff changeset
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
7
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or modify
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
11 # (at your option) any later version.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
12
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
16 # GNU Affero General Public License for more details.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
17
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
20
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
21 """This file is a wrapper for otr.js. It partially reproduces the usage
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
22 (modules, classes and attributes names) and behavior of potr, so you
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
23 can easily adapt some code based on potr to Pyjamas applications.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
24
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
25 potr is released under the GNU LESSER GENERAL PUBLIC LICENSE Version 3
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
26 - https://github.com/python-otr/pure-python-otr/blob/master/LICENSE
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
27
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
28 otr.js is released under the Mozilla Public Licence Version 2.0
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
29 - https://github.com/arlolra/otr/blob/master/license
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
30 """
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
31
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
32 from __pyjamas__ import JS
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
33
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
34 # should you re-use this class outside SàT, you can import __pyjamas__.console as log instead
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
35 from sat.core.log import getLogger
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
36 log = getLogger(__name__)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
37
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
38
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
39 # XXX: pyjamas can't probably import more than one JS file, it messes the order.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
40 # XXX: pyjamas needs the file to be in the compilation directory - no submodule.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
41 # XXX: pyjamas needs the imported file to end with a empty line or semi-column.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
42 # FIXME: fix these bugs upstream in Pyjamas
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
43 import otr.min.js
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
44
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
45
549
cd56f2b0e85b browser_side: disable OTR if the browser is too old to offer crypto.getRandomValues (CSPRNG)
souliane <souliane@mailoo.org>
parents: 537
diff changeset
46 def isSupported():
cd56f2b0e85b browser_side: disable OTR if the browser is too old to offer crypto.getRandomValues (CSPRNG)
souliane <souliane@mailoo.org>
parents: 537
diff changeset
47 JS("""return (typeof OTR !== 'undefined');""")
cd56f2b0e85b browser_side: disable OTR if the browser is too old to offer crypto.getRandomValues (CSPRNG)
souliane <souliane@mailoo.org>
parents: 537
diff changeset
48
cd56f2b0e85b browser_side: disable OTR if the browser is too old to offer crypto.getRandomValues (CSPRNG)
souliane <souliane@mailoo.org>
parents: 537
diff changeset
49
cd56f2b0e85b browser_side: disable OTR if the browser is too old to offer crypto.getRandomValues (CSPRNG)
souliane <souliane@mailoo.org>
parents: 537
diff changeset
50 if not isSupported():
cd56f2b0e85b browser_side: disable OTR if the browser is too old to offer crypto.getRandomValues (CSPRNG)
souliane <souliane@mailoo.org>
parents: 537
diff changeset
51 # see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues#Browser_Compatibility
cd56f2b0e85b browser_side: disable OTR if the browser is too old to offer crypto.getRandomValues (CSPRNG)
souliane <souliane@mailoo.org>
parents: 537
diff changeset
52 log.error('Your browser is not implementing CSPRNG: OTR has been disabled.')
cd56f2b0e85b browser_side: disable OTR if the browser is too old to offer crypto.getRandomValues (CSPRNG)
souliane <souliane@mailoo.org>
parents: 537
diff changeset
53 raise ImportError('CSPRNG is not supported by your browser')
cd56f2b0e85b browser_side: disable OTR if the browser is too old to offer crypto.getRandomValues (CSPRNG)
souliane <souliane@mailoo.org>
parents: 537
diff changeset
54
cd56f2b0e85b browser_side: disable OTR if the browser is too old to offer crypto.getRandomValues (CSPRNG)
souliane <souliane@mailoo.org>
parents: 537
diff changeset
55
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
56 class context(object):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
57
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
58 # Pre-declare these attributes to avoid the pylint "undefined variable" errors
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
59 STATUS_SEND_QUERY = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
60 STATUS_AKE_INIT = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
61 STATUS_AKE_SUCCESS = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
62 STATUS_END_OTR = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
63 STATE_PLAINTEXT = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
64 STATE_ENCRYPTED = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
65 STATE_FINISHED = None
537
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
66 OTR_TAG = None
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
67 OTR_VERSION_2 = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
68 OTR_VERSION_3 = None
537
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
69 WHITESPACE_TAG = None
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
70 WHITESPACE_TAG_V2 = None
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
71 WHITESPACE_TAG_V3 = None
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
72
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
73 JS("""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
74 $cls_definition['STATUS_SEND_QUERY'] = OTR.CONST.STATUS_SEND_QUERY;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
75 $cls_definition['STATUS_AKE_INIT'] = OTR.CONST.STATUS_AKE_INIT;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
76 $cls_definition['STATUS_AKE_SUCCESS'] = OTR.CONST.STATUS_AKE_SUCCESS;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
77 $cls_definition['STATUS_END_OTR'] = OTR.CONST.STATUS_END_OTR;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
78 $cls_definition['STATE_PLAINTEXT'] = OTR.CONST.MSGSTATE_PLAINTEXT;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
79 $cls_definition['STATE_ENCRYPTED'] = OTR.CONST.MSGSTATE_ENCRYPTED;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
80 $cls_definition['STATE_FINISHED'] = OTR.CONST.MSGSTATE_FINISHED;
537
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
81 $cls_definition['OTR_TAG'] = OTR.CONST.OTR_TAG;
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
82 $cls_definition['OTR_VERSION_2'] = OTR.CONST.OTR_VERSION_2;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
83 $cls_definition['OTR_VERSION_3'] = OTR.CONST.OTR_VERSION_3;
537
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
84 $cls_definition['WHITESPACE_TAG'] = OTR.CONST.WHITESPACE_TAG;
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
85 $cls_definition['WHITESPACE_TAG_V2'] = OTR.CONST.WHITESPACE_TAG_V2;
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
86 $cls_definition['WHITESPACE_TAG_V3'] = OTR.CONST.WHITESPACE_TAG_V3;
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
87 """)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
88
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
89 class UnencryptedMessage(Exception):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
90 pass
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
91
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
92 class Context(object):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
93
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
94 def __init__(self, account, peername):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
95 self.user = account
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
96 self.peer = peername
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
97 self.trustName = self.peer
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
98 options = {'fragment_size': 140,
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
99 'send_interval': 200,
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
100 'priv': account.getPrivkey(), # this would generate the account key if it hasn't been done yet
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
101 'debug': False,
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
102 }
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
103 JS("""self.otr = new OTR(options);""")
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
104
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
105 for policy in ('ALLOW_V2', 'ALLOW_V3', 'REQUIRE_ENCRYPTION'):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
106 setattr(self.otr, policy, self.getPolicy(policy))
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
107
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
108 self.otr.on('ui', self.receiveMessageCb)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
109 self.otr.on('io', self.sendMessageCb)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
110 self.otr.on('error', self.messageErrorCb)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
111 self.otr.on('status', lambda status: self.setStateCb(self.otr.msgstate, status))
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
112 self.otr.on('smp', self.smpAuthCb)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
113
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
114 @property
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
115 def state(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
116 return self.otr.msgstate
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
117
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
118 @state.setter
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
119 def state(self, state):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
120 self.otr.msgstate = state
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
121
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
122 def getCurrentKey(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
123 return self.otr.their_priv_pk
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
124
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
125 def setTrust(self, fingerprint, trustLevel):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
126 self.user.setTrust(self.trustName, fingerprint, trustLevel)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
127
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
128 def setCurrentTrust(self, trustLevel):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
129 self.setTrust(self.otr.their_priv_pk.fingerprint(), trustLevel)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
130
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
131 def getTrust(self, fingerprint, default=None):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
132 return self.user.getTrust(self.trustName, fingerprint, default)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
133
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
134 def getCurrentTrust(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
135 # XXX: the docstring of potr for the return value of this method is incorrect
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
136 if self.otr.their_priv_pk is None:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
137 return None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
138 return self.getTrust(self.otr.their_priv_pk.fingerprint(), None)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
139
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
140 def getUsedVersion(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
141 """Return the otr version that is beeing used"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
142 # this method doesn't exist in potr, it has been added for convenience
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
143 try:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
144 return self.otr.ake.otr_version
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
145 except AttributeError:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
146 return None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
147
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
148 def disconnect(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
149 self.otr.endOtr()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
150
530
1735aaeac652 plugin OTR: forces FINISHED state if we are in ENCRYPTED state on contact disconnection
souliane <souliane@mailoo.org>
parents: 523
diff changeset
151 def finish(self):
1735aaeac652 plugin OTR: forces FINISHED state if we are in ENCRYPTED state on contact disconnection
souliane <souliane@mailoo.org>
parents: 523
diff changeset
152 """Finish the session - avoid to send any message and the user has to manually disconnect"""
532
1fa173107fde browser_side (wrapper otr.js): fixes the method to finish the conversation
souliane <souliane@mailoo.org>
parents: 530
diff changeset
153 # it means TLV of type 1 (two first bytes), message length 0 (2 last bytes)
1fa173107fde browser_side (wrapper otr.js): fixes the method to finish the conversation
souliane <souliane@mailoo.org>
parents: 530
diff changeset
154 self.otr.handleTLVs('\x00\x01\x00\x00')
530
1735aaeac652 plugin OTR: forces FINISHED state if we are in ENCRYPTED state on contact disconnection
souliane <souliane@mailoo.org>
parents: 523
diff changeset
155
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
156 def receiveMessage(self, msg):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
157 """Received a message, ask otr.js to (try to) decrypt it"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
158 self.otr.receiveMsg(msg)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
159
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
160 def sendMessage(self, msg):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
161 """Ask otr.js to encrypt a message for sending"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
162 self.otr.sendMsg(msg)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
163
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
164 def sendQueryMessage(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
165 """Start or refresh an encryption communication"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
166 # otr.js offers this method, with potr you have to build the query message yourself
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
167 self.otr.sendQueryMsg()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
168
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
169 def inject(self, msg, appdata=None):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
170 return self.sendMessageCb(msg, appdata)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
171
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
172 def getPolicy(self, key):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
173 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
174
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
175 def smpAuthSecret(self, secret, question=None):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
176 return self.otr.smpSecret(secret, question)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
177
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
178 def smpAuthAbort(self, act=None):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
179 # XXX: dirty hack to let the triggered method know who aborted the
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
180 # authentication. We need it to display the proper feedback and,
523
5add182e7dd5 browser_side (plugin OTR): rename menus and replace 'buddy' with 'correspondent'
souliane <souliane@mailoo.org>
parents: 522
diff changeset
181 # if the correspondent aborted, set the conversation 'unverified'.
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
182 self.otr.sm.init()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
183 JS("""self.otr.sm.sendMsg(OTR.HLP.packTLV(6, ''))""")
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
184 self.smpAuthCb('abort', '', act)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
185
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
186 def sendMessageCb(self, msg, meta):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
187 """Actually send the message after it's been encrypted"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
188 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
189
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
190 def receiveMessageCb(self, msg, encrypted):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
191 """Display the message after it's been eventually decrypted"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
192 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
193
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
194 def messageErrorCb(self, error):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
195 """Message error callback"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
196 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
197
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
198 def setStateCb(self, newstate):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
199 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
200
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
201 def smpAuthCb(self, newstate):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
202 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
203
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
204 class Account(object):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
205
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
206 def __init__(self, host):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
207 self.host = host
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
208 self.privkey = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
209 self.trusts = {}
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
210
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
211 def getPrivkey(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
212 # the return value must have a method serializePrivateKey()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
213 # if the key is not saved yet, call savePrivkey to generate it
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
214 if self.privkey is None:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
215 self.privkey = self.loadPrivkey()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
216 if self.privkey is None:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
217 JS("""self.privkey = new DSA();""")
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
218 self.savePrivkey()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
219 return self.privkey
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
220
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
221 def setTrust(self, key, fingerprint, trustLevel):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
222 if key not in self.trusts:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
223 self.trusts[key] = {}
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
224 self.trusts[key][fingerprint] = trustLevel
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
225 self.saveTrusts()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
226
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
227 def getTrust(self, key, fingerprint, default=None):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
228 if key not in self.trusts:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
229 return default
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
230 return self.trusts[key].get(fingerprint, default)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
231
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
232 def loadPrivkey(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
233 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
234
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
235 def savePrivkey(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
236 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
237
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
238 def saveTrusts(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
239 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
240
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
241
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
242 class crypt(object):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
243
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
244 class PK(object):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
245
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
246 def parsePrivateKey(self, key):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
247 JS("""return DSA.parsePrivate(key);""")
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
248
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
249
537
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
250 class proto(object):
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
251
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
252 @classmethod
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
253 def checkForOTR(cls, body):
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
254 """Helper method to check if the message contains OTR starting tag or whitespace
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
255
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
256 @return:
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
257 - context.OTR_TAG if the message starts with it
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
258 - context.WHITESPACE_TAG if the message contains OTR whitespaces
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
259 - None otherwise
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
260 """
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
261 if body.startswith(context.OTR_TAG):
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
262 return context.OTR_TAG
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
263 index = body.find(context.WHITESPACE_TAG)
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
264 if index < 0:
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
265 return False
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
266 tags = [body[i:i + 8] for i in range(index, len(body), 8)]
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
267 if [True for tag in tags if tag in (context.WHITESPACE_TAG_V2, context.WHITESPACE_TAG_V3)]:
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
268 return context.WHITESPACE_TAG
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
269 return None
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
270
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
271
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
272 # serialazePrivateKey is the method name in potr
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
273 JS("""DSA.serializePrivateKey = DSA.packPrivate;""")