This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weesodium.py
355 lines (278 loc) · 12 KB
/
weesodium.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# encoding: utf-8
#
# weesodium: encrypt messages in a channel with libsodium
# DO NOT YET RELY ON THIS SCRIPT FOR STRONG SECURITY!
#
# Copyright (c) 2014 mutantmonkey
#
# Portions based upon weechat-otr:
# Copyright (c) 2012-2014 Matthew M. Boedicker <matthewm@boedicker.org>
# Nils Görs <weechatter@arcor.de>
# Daniel "koolfy" Faucon <koolfy@koolfy.be>
# Felix Eckhofer <felix@tribut.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import base64
import hashlib
import libnacl.secret
import shlex
import sys
import weechat
SCRIPT_NAME = 'weesodium'
SCRIPT_AUTHOR = 'mutantmonkey'
SCRIPT_VERSION = '20160321'
SCRIPT_LICENSE = 'GPL3'
SCRIPT_DESC = "encrypt messages in a channel with libsodium"
IRC_SANITIZE_TABLE = dict((ord(char), None) for char in '\n\r\x00')
channel_data = {}
class WeeSodiumChannel(object):
def __init__(self, key):
self.key = hashlib.sha256(key).digest()
self.counter = 0
self.nonces = set()
class NonceError(Exception):
pass
# helper functions {{{
def encrypt(channel, nick, msg, length=None):
# convert message to UTF-8
msg = msg.encode('utf-8', 'replace')
# pad message to length, if one is provided
if length is not None and len(msg) < length:
msg += b'\x00' * (length - len(msg))
box = libnacl.secret.SecretBox(channel.key)
ctxt = box.encrypt(msg)
channel.nonces.add(ctxt[:libnacl.crypto_secretbox_NONCEBYTES])
return base64.b64encode(ctxt)
def decrypt(channel, ctxt):
ctxt = base64.b64decode(ctxt)
nonce = ctxt[:libnacl.crypto_secretbox_NONCEBYTES]
ctxt = ctxt[libnacl.crypto_secretbox_NONCEBYTES:]
if len(nonce) != libnacl.crypto_secretbox_NONCEBYTES:
raise ValueError("Invalid nonce size")
elif nonce in channel.nonces:
raise NonceError(
"Nonce reuse detected; this is either a bug or a replay attack in "
"progress.")
box = libnacl.secret.SecretBox(channel.key)
msg = box.decrypt(ctxt, nonce)
msg = msg.rstrip(b'\x00')
msg = msg.decode('utf-8', 'replace')
channel.nonces.add(nonce)
return msg
def parse_privmsg(message):
weechat_result = weechat.info_get_hashtable('irc_message_parse',
{'message': message})
if weechat_result['command'].upper() == 'PRIVMSG':
args = weechat_result['arguments'].decode('utf-8', 'replace')
target, text = args.split(' ', 1)
if text.startswith(':'):
text = text[1:]
result = {
'from': weechat_result['host'].decode('utf-8', 'replace'),
'to': target,
'text': text,
}
if target[0] in ('#', '&', '!', '+'):
result['to_channel'] = target
result['to_nick'] = None
else:
result['to_channel'] = None
result['to_nick'] = target
return result
else:
raise Exception("Failed parsing PRIVMSG")
def irc_sanitize(msg):
if sys.version_info[0] >= 3:
msg = str(msg)
else:
msg = unicode(msg)
return msg.translate(IRC_SANITIZE_TABLE)
def irc_in_privmsg_build(fromm, to, msg):
return u":{fromm} PRIVMSG {to} :{msg}".format(
fromm=irc_sanitize(fromm),
to=irc_sanitize(to),
msg=irc_sanitize(msg)).encode('utf-8', 'replace')
def irc_out_privmsg_build(to, msg):
return u"PRIVMSG {to} :{msg}".format(
to=irc_sanitize(to),
msg=irc_sanitize(msg)).encode('utf-8', 'replace')
def get_buffer_info(buf):
server = weechat.buffer_get_string(buf, b'localvar_server').decode(
'utf-8', 'replace')
channel = weechat.buffer_get_string(buf, b'localvar_channel').decode(
'utf-8', 'replace')
return server, channel
# }}}
# callbacks {{{
def reload_config_cb(data, config_file):
return weechat.config_reload(config_file)
def keys_read_option_cb(data, config_file, section, option, value):
"""Read option callback: load keys from config options"""
weeopt = weechat.config_new_option(config_file, section, option, 'string',
'key', '', 0, 0, '', value, 0,
'keys_check_option_cb', '', '', '',
'', '')
if not weeopt:
return weechat.WEECHAT_CONFIG_OPTION_SET_ERROR
option_split = option.split('.')
if len(option_split) == 2 and value.startswith('${sec.data.'):
channel_key = weechat.string_eval_expression(value, {}, {}, {})
channel_data[option] = WeeSodiumChannel(channel_key)
return weechat.WEECHAT_CONFIG_OPTION_SET_OK_CHANGED
def keys_create_option_cb(data, config_file, section, option, value):
if not keys_check_option_cb('', option, value):
weechat.prnt('',
"WeeSodium keys must be stored using WeeChat's secured "
"data storage. See /help secure for info on this.")
return weechat.WEECHAT_CONFIG_OPTION_SET_ERROR
option_split = option.split('.')
if len(option_split) != 2:
weechat.prnt('',
"Both a server and a channel are required to be entered "
"as the key for the config option. For example: "
"/set weesodium.keys.example.#weesodium "
"${sec.data.wskey}")
return weechat.WEECHAT_CONFIG_OPTION_SET_ERROR
weeopt = weechat.config_new_option(config_file, section, option, 'string',
'key', '', 0, 0, '', value, 0,
'keys_check_option_cb', '', '', '',
'', '')
if not weeopt:
return weechat.WEECHAT_CONFIG_OPTION_SET_ERROR
option_split = option.split('.')
if len(option_split) == 2 and value.startswith('${sec.data.'):
channel_key = weechat.string_eval_expression(value, {}, {}, {})
channel_data[option] = WeeSodiumChannel(channel_key)
return weechat.WEECHAT_CONFIG_OPTION_SET_OK_CHANGED
def keys_check_option_cb(data, option, value):
if value.startswith('${sec.data.'):
return 1
else:
return 0
def command_cb(data, buf, args):
try:
args = shlex.split(args)
except:
return weechat.WEECHAT_RC_ERROR
if len(args) == 2 and args[0] == b'enable':
server, channel = get_buffer_info(buf)
channel_data[u'{0}.{1}'.format(server, channel)] = WeeSodiumChannel(
args[1])
weechat.prnt(buf, "This conversation is now encrypted.")
weechat.bar_item_update(SCRIPT_NAME)
return weechat.WEECHAT_RC_OK
elif len(args) == 1 and args[0] == b'disable':
server, channel = get_buffer_info(buf)
del channel_data[u'{0}.{1}'.format(server, channel)]
weechat.prnt(buf, "This conversation is no longer encrypted.")
weechat.bar_item_update(SCRIPT_NAME)
return weechat.WEECHAT_RC_OK
else:
return weechat.WEECHAT_RC_ERROR
def in_privmsg_cb(data, modifier, modifier_data, string):
result = parse_privmsg(string)
if result['to_channel'] is not None:
dict_key = u'{0}.{1}'.format(modifier_data, result['to_channel'])
if dict_key in channel_data:
channel = channel_data[dict_key]
try:
result['text'] = decrypt(channel, result['text'])
except NonceError as e:
buf = weechat.info_get('irc_buffer', '{0},{1}'.format(
modifier_data, result['to_channel']))
weechat.prnt(buf, "Error from {fromm}: {err}".format(
fromm=result['from'], err=e))
return ""
except:
result['text'] = "Unable to decrypt: {}".format(result['text'])
return irc_in_privmsg_build(result['from'], result['to'],
result['text'])
return string
def out_privmsg_cb(data, modifier, modifier_data, string):
result = parse_privmsg(string)
if result['to_channel'] is not None:
dict_key = u'{0}.{1}'.format(modifier_data, result['to_channel'])
if dict_key in channel_data:
channel = channel_data[dict_key]
try:
# this should result in messages that are 396 bytes long
max_length = 297 - libnacl.crypto_secretbox_NONCEBYTES \
- libnacl.crypto_secretbox_MACBYTES
if len(result['text']) > max_length:
# segment messages larger than max_length
out = ""
splits = 1 + (len(result['text']) // max_length)
for i in range(0, splits):
msg = encrypt(
channel,
result['from'],
result['text'][
i * max_length:(i + 1) * max_length],
max_length)
out += irc_out_privmsg_build(result['to'], msg)
out += "\r\n"
return out
else:
msg = encrypt(channel, result['from'], result['text'],
max_length)
return irc_out_privmsg_build(result['to'], msg)
except Exception as e:
buf = weechat.info_get('irc_buffer', '{0},{1}'.format(
modifier_data, result['to_channel']))
weechat.prnt(buf, "Error while encrypting: {}".format(e))
return ""
return string
def buffer_closing_cb(data, signal, signal_data):
server, channel = get_buffer_info(signal_data)
if server is not None and channel is not None:
dict_key = u'{0}.{1}'.format(server, channel)
if dict_key in channel_data:
del channel_data[dict_key]
weechat.bar_item_update(SCRIPT_NAME)
return weechat.WEECHAT_RC_OK
return weechat.WEECHAT_RC_ERROR
def statusbar_cb(data, item, window):
if window:
buf = weechat.window_get_pointer(window, 'buffer')
else:
buf = weechat.get_current_buffer()
server, channel = get_buffer_info(buf)
dict_key = u'{0}.{1}'.format(server, channel)
if dict_key in channel_data:
return "ENC"
return ""
# }}}
if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
SCRIPT_DESC, "", "UTF-8"):
# load config
config_file = weechat.config_new(SCRIPT_NAME, 'reload_config_cb', '')
weechat.config_new_section(config_file, 'keys', 1, 1,
'keys_read_option_cb', '',
'', '',
'', '',
'keys_create_option_cb', '',
'', '')
weechat.config_read(config_file)
weechat.hook_command(SCRIPT_NAME,
"change weesodium options",
"[enable KEY] || "
"[disable]",
"",
"enable %-|| "
"disable",
"command_cb",
"")
weechat.hook_modifier('irc_in_privmsg', 'in_privmsg_cb', '')
weechat.hook_modifier('irc_out_privmsg', 'out_privmsg_cb', '')
weechat.hook_signal('buffer_closing', 'buffer_closing_cb', '')
statusbar = weechat.bar_item_new(SCRIPT_NAME, 'statusbar_cb', '')
weechat.bar_item_update(SCRIPT_NAME)