-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwait_for_invite.py
100 lines (90 loc) · 3.67 KB
/
wait_for_invite.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
#!/usr/bin/env python3
#wait_for_invite.py
import os, sys, getopt
from netsim.netinterface import network_interface
from Crypto.Signature import PKCS1_PSS
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from datetime import datetime
'''
Time = 17 bytes
Cipher = 128 bytes (content = 1 byte (ID) and 16 bytes (public key))
Signature = length varies, index at i=145 until the end
'''
'''
try:
opts, args = getopt.getopt(sys.argv[1:], shortopts='hi:s:', longopts=['help', 'inviter=', 'self='])
except getopt.GetoptError:
print('Usage: python wait_for_invite.py -i <inviter address> -s <self address>')
sys.exit(1)
if len(opts) != 2:
print('Usage: python wait_for_invite.py -i <inviter address> -s <self address>')
sys.exit(1)
for opt, arg in opts:
if opt == '-h' or opt == '--help':
print('Usage: python wait_for_invite.py -i <inviter address> -s <self address>')
sys.exit(0)
elif opt == '-i' or opt == '--inviter':
INVITER_ID = arg
elif opt == '-s' or opt == '--self':
OWN_ADDR = arg
'''
def receive_invite(netif, OWN_ADDR, password):
# import private key of invitee(self) for RSA
prikfile = open("setup/%s-key.pem"%OWN_ADDR,'r')
prikeystr = prikfile.read()
prikfile.close()
prikey = RSA.import_key(prikeystr,passphrase = password)
#NET_PATH = './netsim/network/'
print('Main loop started...')
while True:
status, msg = netif.receive_msg(blocking=True)
timestamp = msg[:17]
ciphertext = msg[17:145]
signature = msg[145:]
print('Verifying time stamp...')
current_time = datetime.now()
current_timestamp = datetime.timestamp(current_time)
print("Current timestamp: " + str(current_timestamp))
print("Received timestamp: " + timestamp.decode('utf-8'))
if (current_timestamp - float(timestamp) <= 10): # verify time stamp
print('Time stamp verified')
cipher = PKCS1_OAEP.new(prikey)
print('Decryption started...')
try:
plaintext = cipher.decrypt(ciphertext)
print('Decryption success.')
print('Group ID is ' + (plaintext[1:2]).decode('utf-8'))
print('Group key is ' + str(plaintext[2:]))
print('Verifying signature...')
INVITER_ID = plaintext[0:1].decode('utf-8')
# RSA PKCS1 PSS SIGNATURE
# import the public key of INVITER_ID
pubkeystr = ''
with open('setup/table%s.txt'%OWN_ADDR) as f:
kfile = f.read()
pubkeys = kfile.split("member:")
pubkeys.pop(0)
for k in pubkeys:
if k[0] == INVITER_ID:
pubkeystr = k.split("key:")[1]
if(pubkeystr == ''):
print('No public key string read!')
sys.exit(1)
msg_to_be_signed = OWN_ADDR.encode('utf-8') + timestamp + ciphertext
h = SHA256.new()
h.update(msg_to_be_signed)
pubkey = RSA.import_key(pubkeystr)
verifier = PKCS1_PSS.new(pubkey)
if verifier.verify(h, signature):
print('Signature verified.')
return(plaintext[2:])
else:
print('Signature is incorrect!')
return
except ValueError:
print('Decryption failed. This may be someone else\'s invitation.')
else:
print('The timestamp has expired.')
return