forked from rxcomm/smmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmmpchat.py
executable file
·491 lines (449 loc) · 18.6 KB
/
smmpchat.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/env python
import re
import binascii
import socket
import threading
import sys
import curses
import gnupg
import hmac
import hashlib
from curses.textpad import Textbox
from random import randint
from contextlib import contextmanager
from time import sleep, time
from smmp import Participant, BummerUndecryptable, BadHMAC, BadDIGEST
from StringIO import StringIO
from getpass import getpass
from bd import BD
"""
Standalone multi-party chat script using AES256 encryption with
SMMP ratchet for key management.
Usage:
1. Start the chat script with
./smmpchat.py
One person will need to serve as Organizer. Once key-agreement
has been reached the Organizer role is abandoned, and all users
are peers.
2. All exchange of data during the key-agreement phase may be done
over an insecure medium.
3. .quit at the chat prompt will quit (don't forget the "dot")
4. To avoid collisions between messages, smmpchat uses a simple TDMA
algorithm. The TDMA slot time (window for sending messages from
a participant) may be adjusted with the .slottime command. For example:
.slottime 2.5
will set the slot time to 2.5 seconds. All group participants should
set the same slot time.
5. If you lose synchronization with a user (Undecryptable message error),
hit <RETURN> and the system should re-sync you. You can also resync
by typing .resync at the chat prompt. Collisions can be minimized by
judicious selection of the slot time. It should be greater than the
round-trip time required for messages.
6. If you receive a Bad HMAC error, either 1) you are badly out of sync
and will need to perform a new key-agreement step, or 2) Somebody
else is spamming your port with garbage packets. Neither is good.
7. If you have the means to distribute keys securely to all users, you can
use the gendata.py utility to generate a keyset and skip the formal
key agreement process.
Port 50000 is the default port, but you can choose your own port as well.
smmpchat requires the SMMP module.
Copyright (C) 2014 by David R. Andersen <[email protected]>
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/>.
"""
@contextmanager
def socketcontext(*args, **kwargs):
s = socket.socket(*args, **kwargs)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
yield s
s.close()
@contextmanager
def participant(group_name, group_size, my_index):
a = Participant(group_name, group_size, my_index)
yield a
@contextmanager
def encFile(file_name, passphrase):
a = StringIO()
yield a
KEYRING = './keyring.gpg'
SECRET_KEYRING = './secring.gpg'
GPGBINARY = 'gpg'
gpg = gnupg.GPG(gnupghome='.', gpgbinary=GPGBINARY, keyring=KEYRING,
secret_keyring=SECRET_KEYRING, options=['--throw-keyids',
'--personal-digest-preferences=sha256','--s2k-digest-algo=sha256'])
gpg.encoding = 'utf-8'
ciphertext = gpg.encrypt(a.getvalue(), recipients=None, symmetric='AES256',
armor=False, always_trust=True, passphrase=passphrase)
a.close()
with open(file_name, 'wb') as f:
f.write(ciphertext.data)
@contextmanager
def decFile(file_name, passphrase):
KEYRING = './keyring.gpg'
SECRET_KEYRING = './secring.gpg'
GPGBINARY = 'gpg'
gpg = gnupg.GPG(gnupghome='.', gpgbinary=GPGBINARY, keyring=KEYRING,
secret_keyring=SECRET_KEYRING, options=['--throw-keyids',
'--personal-digest-preferences=sha256','--s2k-digest-algo=sha256'])
gpg.encoding = 'utf-8'
with open(file_name, 'rb') as f:
ciphertext = f.read()
plaintext = gpg.decrypt(ciphertext, passphrase=passphrase, always_trust=True)
a = StringIO(plaintext)
a.seek(0)
yield a
a.close()
class _Textbox(Textbox):
"""
curses.textpad.Textbox requires users to ^g on completion, which is sort
of annoying for an interactive chat client such as this, which typically only
reuquires an enter. This subclass fixes this problem by signalling completion
on Enter as well as ^g. Also, map <Backspace> key to ^h.
"""
def __init__(*args, **kwargs):
Textbox.__init__(*args, **kwargs)
def do_command(self, ch):
if ch == 10: # Enter
return 0
if ch == 127: # Enter
return 8
return Textbox.do_command(self, ch)
def validator(ch):
"""
Update screen if necessary and release the lock so receiveThread can run
"""
global screen_needs_update
try:
if screen_needs_update:
curses.doupdate()
screen_needs_update = False
return ch
finally:
lock.release()
sleep(0.01) # let receiveThread in if necessary
lock.acquire()
def windows():
stdscr = curses.initscr()
curses.noecho()
curses.start_color()
curses.use_default_colors()
curses.init_pair(1, curses.COLOR_RED, -1)
curses.init_pair(2, curses.COLOR_CYAN, -1)
curses.init_pair(3, curses.COLOR_GREEN, -1)
curses.cbreak()
curses.curs_set(1)
(sizey, sizex) = stdscr.getmaxyx()
input_win = curses.newwin(8, sizex, sizey-8, 0)
output_win = curses.newwin(sizey-9, sizex, 1, 0)
title_win = curses.newwin(1, sizex, 0, 0)
input_win.idlok(1)
input_win.scrollok(1)
input_win.nodelay(1)
input_win.leaveok(0)
input_win.timeout(100)
input_win.attron(curses.color_pair(3))
output_win.idlok(1)
output_win.scrollok(1)
output_win.leaveok(0)
title_win.idlok(1)
title_win.scrollok(1)
title_win.leaveok(0)
return stdscr, input_win, output_win, title_win
def closeWindows(stdscr):
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()
def usage():
print 'Usage: ' + sys.argv[0] + ' -(s,c,g)'
print ' -s: start a chat in server mode'
print ' -c: start a chat in client mode'
print ' -g: generate a key database for a nick'
exit()
def s2l(s):
return long(binascii.hexlify(s), 16)
def l2s(n):
num = '%x' % n
if len(num) % 2:
num = '0' + num
return binascii.unhexlify(num)
def sendThread(sock, mypart, SLOTTIME):
global msg_data
stime = float(SLOTTIME)/4.0
grp = float(SLOTTIME * mypart.group_size)
my = float(SLOTTIME * mypart.state['my_index'])
while abs((time() % grp) - my) >= stime:
sleep(0.01 * SLOTTIME)
try:
data = msg_data
msg_data = ''
ciphertexts = mypart.encrypt(data)
if type(ciphertexts) is dict:
for i, message in ciphertexts.iteritems():
sock.send(message + 'EOP')
else:
sock.send(ciphertexts + 'EOP')
except socket.error:
input_win.addstr('Disconnected')
input_win.refresh()
closeWindows(stdscr)
sys.exit()
def receiveThread(sock, mypart, stdscr, input_win, output_win, title_win):
global screen_needs_update
while True:
data = ''
while data[-3:] != 'EOP':
rcv = sock.recv(1024)
if not rcv:
input_win.move(0, 0)
input_win.addstr('Disconnected - Ctrl-C to exit!')
input_win.refresh()
sys.exit()
data = data + rcv
data_list = data.split('EOP')
lock.acquire()
(cursory, cursorx) = input_win.getyx()
for data in data_list:
if data != '':
if data[:6] == 'SYSMSG':
title_win.clear()
title_win.addstr(0, 0, 'Group: ', curses.color_pair(2))
title_win.addstr(mypart.state['group_name'], curses.color_pair(2))
title_win.addstr(' | ', curses.color_pair(3))
if int(re.search('\d+', data[6:]).group()) == mypart.group_size:
clr = 2
else:
clr = 1
title_win.addstr('Status: ', curses.color_pair(clr))
title_win.addstr(data[6:].strip(), curses.color_pair(clr))
title_win.addstr(' | ', curses.color_pair(3))
title_win.addstr('Users in group: '+str(mypart.group_size), curses.color_pair(clr))
else:
try:
msg = mypart.decrypt(data)
if msg[:31] == 'Ratchet resync message received':
output_win.addstr(msg, curses.color_pair(1))
else:
output_win.addstr(msg)
except (BummerUndecryptable, BadDIGEST, TypeError):
mypart.resync_required = True
output_win.addstr('Undecryptable message\n', curses.color_pair(1))
except BadHMAC:
output_win.addstr('Bad HMAC\n', curses.color_pair(1))
input_win.move(cursory, cursorx)
input_win.cursyncup()
input_win.noutrefresh()
output_win.noutrefresh()
title_win.noutrefresh()
screen_needs_update = True
lock.release()
def chatThread(sock, mypart, myname):
global screen_needs_update
global msg_data
SLOTTIME = 1.0 # TDMA slot time (seconds)
stdscr, input_win, output_win, title_win = windows()
title_win.addstr('Group: ', curses.color_pair(3))
title_win.addstr(mypart.state['group_name'])
title_win.noutrefresh()
input_win.addstr(0, 0, myname+':> ')
textpad = _Textbox(input_win, insert_mode=True)
textpad.stripspaces = True
delay = randint(1, mypart.group_size + 10)
rt = threading.Thread(target=receiveThread, args=(sock,mypart,stdscr,input_win,output_win,title_win))
rt.daemon = True
rt.start()
try:
while True:
lock.acquire()
data = textpad.edit(validator)
if myname+':> .resync' in data or mypart.resync_required:
lock.release()
mypart.resync_required = True
msg = mypart.resyncSend(sock, SLOTTIME)
lock.acquire()
input_win.clear()
input_win.addstr(myname+':> ')
output_win.addstr(msg + '\n', curses.color_pair(1))
output_win.noutrefresh()
input_win.move(0, len(myname) +3)
input_win.cursyncup()
input_win.noutrefresh()
screen_needs_update = True
lock.release()
elif myname+':> .quit' in data:
closeWindows(stdscr)
ans = raw_input('Save the state? Y/n ')
if ans != 'n' and ans != 'N':
saveState(mypart)
sys.exit()
elif myname+':> .slottime' in data:
input_win.clear()
input_win.addstr(myname+':> ')
try:
SLOTTIME = float(data[len(myname+':> .slottime '):])
output_win.addstr('SLOTTIME set to '+str(SLOTTIME)+' seconds\n', curses.color_pair(1))
output_win.noutrefresh()
except ValueError:
output_win.addstr('SLOTTIME not reset\n', curses.color_pair(1))
output_win.noutrefresh()
input_win.move(0, len(myname) +3)
input_win.cursyncup()
input_win.noutrefresh()
screen_needs_update = True
lock.release()
else:
input_win.clear()
input_win.addstr(myname+':> ')
output_win.addstr(data.replace('\n', '') + '\n', curses.color_pair(3))
output_win.noutrefresh()
input_win.move(0, len(myname) +3)
input_win.cursyncup()
input_win.noutrefresh()
screen_needs_update = True
data = data.replace('\n', '') + '\n'
lock.release()
try:
if st.isAlive() and msg_data != '':
msg_data += data
else:
msg_data = data
while st.isAlive():
pass
st = threading.Thread(target=sendThread,args=(sock, mypart, SLOTTIME))
st.daemon = True
st.start()
except UnboundLocalError:
msg_data = data
st = threading.Thread(target=sendThread,args=(sock, mypart, SLOTTIME))
st.daemon = True
st.start()
except KeyboardInterrupt:
closeWindows(stdscr)
ans = raw_input('Save the state? Y/n ')
if ans != 'n' and ans != 'N':
saveState(mypart)
def saveState(mypart):
file_name = raw_input('What file do you want to save the data in? ')
passphrase=''
passphrase1='1'
while passphrase != passphrase1:
passphrase = getpass('What is the passphrase for the file: ')
passphrase1 = getpass('Repeat: ')
if passphrase != passphrase1:
print 'Passphrases did not match'
with encFile(file_name, passphrase) as f:
f.write(binascii.b2a_base64(mypart.state['MK']))
f.write(binascii.b2a_base64(mypart.state['RK']))
f.write(binascii.b2a_base64(mypart.state['v']))
f.write(mypart.state['group_name']+'\n')
f.write(str(mypart.state['my_index'])+'\n')
f.write(str(mypart.group_size)+'\n')
resync_required = '1' if mypart.resync_required else '0'
f.write(resync_required+'\n')
f.write(binascii.b2a_base64(mypart.ratchetKey))
f.write(binascii.b2a_base64(mypart.state['initr']))
f.write(binascii.b2a_base64(mypart.state['digest']))
for key, item in mypart.state['initpubR'].iteritems():
f.write(binascii.b2a_base64(item))
for key, item in mypart.state['R'].iteritems():
f.write(binascii.b2a_base64(item))
def loadState(mypart):
file_name = raw_input('What file do you want to load? ')
passphrase = getpass('What is the passphrase for the file? ')
with decFile(file_name, passphrase) as f:
data = f.read()
data_list = data.split()
mypart.state['MK'] = binascii.a2b_base64(data_list[0])
mypart.state['RK'] = binascii.a2b_base64(data_list[1])
mypart.state['v'] = binascii.a2b_base64(data_list[2])
mypart.state['group_name'] = data_list[3]
mypart.state['my_index'] = int(data_list[4])
mypart.group_size = int(data_list[5])
resync_required = data_list[6]
mypart.resync_required = True if resync_required == '1' else False
mypart.ratchetKey = binascii.a2b_base64(data_list[7])
mypart.state['initr'] = binascii.a2b_base64(data_list[8])
mypart.state['digest'] = binascii.a2b_base64(data_list[9])
mypart.state['R'] = {}
mypart.state['initpubR'] = {}
for i in range(mypart.group_size):
mypart.state['initpubR'][i] = binascii.a2b_base64(data_list[10+i])
for i in range(mypart.group_size):
mypart.state['R'][i] = binascii.a2b_base64(data_list[10+mypart.group_size+i])
if __name__ == '__main__':
lock = threading.Lock()
screen_needs_update = False
HOST = ''
while True:
try:
PORT = raw_input('TCP port (1 for random choice, 50000 is default): ')
PORT = int(PORT)
break
except ValueError:
PORT = 50000
break
if PORT >= 1025 and PORT <= 65535:
pass
elif PORT == 1:
PORT = 1025 + randint(0, 64510)
print 'PORT is ' + str(PORT)
k = {}
x = {}
R = {}
HOST = raw_input('Enter the server: ')
ans = raw_input('Do you want to load a state? y/N ')
if ans == 'y':
with participant('dummy', 0, 0) as mypart:
loadState(mypart)
myname = raw_input('What is your name? ')
print 'Connecting to ' + HOST + '...'
with socketcontext(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.send(str(mypart.state['my_index']).zfill(3) + 'START')
chatThread(s, mypart, myname)
exit()
group_name = raw_input('What is the group name? ')
num_users = int(raw_input('Input total number of participants (including you): '))
my_index = int(raw_input('Input your participant number: '))
with participant(group_name, num_users, my_index) as mypart:
print 'Your public identity key is: '+binascii.b2a_base64(mypart.identityPKey)
#print 'Your public handshake key is: '+binascii.b2a_base64(l2s(mypart.bd.pubKey))
print 'Your public ratchet key is: '+binascii.b2a_base64(mypart.ratchetPKey)
identityKeys = {}
k = {}
R = {}
signatures = {}
identityKeys[my_index] = mypart.identityPKey
k[my_index] = mypart.bd.pubKey
R[my_index] = mypart.ratchetPKey
for i in range(num_users):
if i != my_index:
identityKeys[i] = binascii.a2b_base64(raw_input('Input user '+str(i)+'\'s public identity key: '))
R[i] = binascii.a2b_base64(raw_input('Input user '+str(i)+'\'s public ratchet key: '))
for i in range(num_users):
if i != my_index:
mackey = mypart.genDH(mypart.identityKey, R[i]) + \
mypart.genDH(mypart.ratchetKey, identityKeys[i]) + \
mypart.genDH(mypart.ratchetKey, R[i])
mackey = hashlib.sha256(mackey).digest()
mac = hmac.new(mackey, str(k[my_index]), hashlib.sha256).digest()
print 'Send this signed public handshake key to user '+str(i)+': '+binascii.b2a_base64(l2s(k[my_index])+mac)
for i in range(num_users):
if i != my_index:
k[i] = raw_input('Input user '+str(i)+'\'s signed public handshake key: ')
mypart.initState(group_name, identityKeys, k, R)
myname = raw_input('What is your name? ')
print 'Connecting to ' + HOST + '...'
with socketcontext(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.send(str(mypart.state['my_index']).zfill(3) + 'START')
chatThread(s, mypart, myname)